Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

### Fixed
- A follow-up with `--tool` (or MCP `pro_consult` `tools`) sent after a new chat in the same tab was refused as `Selected "Web search" but the composer never showed it as active.` although the tool was on, and the tool was left on in the composer. A new chat leaves the home page's composer mounted in a hidden workspace ahead of the conversation's composer, and the check read the first editor in the page, which was that hidden one. Measured 2026-11-03 in the browser container on a test conversation: the follow-up was refused as above with the tool showing in the visible composer; the same check reading the composer the prompt is typed into, built on the container branch, sent it. The tools button lookup also skips an open "Edit message" editor, as the model lookup now does.
- A follow-up that named a model or a reasoning step failed as `composer_not_ready` ("model selector button not found") for as long as a message in the conversation was open for editing, and the advice to retry could not help: only reloading the page did. "Edit message" on a user turn opens a form with its own editor inside the transcript, ahead of the composer, and the model lookup took the first editor on the page, whose form has no model selector. Found 2026-11-03 tracing four follow-ups the browser container refused on 2026-11-02 while new chats in between went through; reproduced on a test conversation by opening "Edit message", where this lookup from 0.40.49 refused as above. It now skips editors inside a message, as typing the prompt already did.
- A consult's session id carried the start of its prompt, lowercased: a prompt beginning "Reply with only the word" became `sess_<date>_<time>_<random>-reply-with-only-the-word-...`, its first 31 characters. The session record itself does not keep the prompt, but the id is listed by `prodex sessions`, written into receipts, and returned by MCP `bridge_list_sessions`, which the HTTP MCP server answers for any client holding its token. Found 2026-10-30 reading a session listing. A new session id is the date, the time and a random part only. Ids already recorded are read as before.

Expand Down
12 changes: 9 additions & 3 deletions src/chatgpt-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6811,9 +6811,11 @@ export function composerToolsButtonRectExpression(): string {
return `(() => {${CLICK_POINT_SNIPPET}
const legacy = document.querySelector('[data-testid="composer-plus-btn"]');
if (legacy) return clickPoint(legacy);
// An open "Edit message" editor sits in the transcript ahead of the
// composer, in a form without this button; see modelButtonRectExpression.
const composer = [...document.querySelectorAll('#prompt-textarea,[contenteditable="true"],textarea')].find((el) => {
const r = el.getBoundingClientRect();
return r.width > 0 && r.height > 0;
return r.width > 0 && r.height > 0 && !(el.parentElement && el.parentElement.closest('[data-message-author-role],[data-chatgpt-search-unit-key]'));
});
const form = composer && composer.closest('form');
if (!form) return { ok: false, reason: "composer tools button not found: the composer has not rendered" };
Expand Down Expand Up @@ -6865,11 +6867,15 @@ export function composerToolEntryRectExpression(label: string): string {
/** Tool tokens currently sitting in the composer. */
export function activeComposerToolsExpression(labels: readonly string[]): string {
const labelsJson = JSON.stringify(labels);
return `(() => {
return `(() => {${composerExpressionHelpers()}
// The token normally lands inside the editor, but some builds render it as
// a pill in the surrounding composer form - check both before deciding a
// selection did not take.
const el = document.querySelector('#prompt-textarea,[contenteditable="true"]');
// Read the composer a send types into. Measured 2026-11-03: a new chat
// leaves the home page's composer mounted in a hidden workspace ahead of
// the conversation's, and the first editor in the document was that one,
// so a follow-up's tool was on but read as off.
const el = findChatGptComposerCandidate();
const form = el ? (el.closest("form") || el.parentElement) : null;
const text = ((el ? el.innerText || "" : "") + String.fromCharCode(10) + (form ? form.innerText || "" : "")).toLowerCase();
// Case-insensitively: prodex carries the label as the menu spells it
Expand Down
164 changes: 164 additions & 0 deletions tests/composer-tool-state.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { describe, expect, it } from "vitest";

import { activeComposerToolsExpression, composerToolsButtonRectExpression } from "../src/chatgpt-browser.js";

interface FakeRect {
x: number;
y: number;
width: number;
height: number;
}

class FakeElement {
readonly children: FakeElement[] = [];
parentElement: FakeElement | null = null;
disabled = false;
innerText = "";

constructor(
readonly tagName: string,
private readonly attributes: Record<string, string> = {},
private readonly rect: FakeRect = { x: 0, y: 0, width: 36, height: 36 }
) {}

get textContent(): string {
return this.innerText;
}

append(...nodes: FakeElement[]): this {
for (const node of nodes) {
node.parentElement = this;
this.children.push(node);
}
return this;
}

getClientRects(): FakeRect[] {
return this.rect.width > 0 && this.rect.height > 0 ? [this.rect] : [];
}

getAttribute(name: string): string | null {
return this.attributes[name] ?? null;
}

setAttribute(name: string, value: string): void {
this.attributes[name] = value;
}

removeAttribute(name: string): void {
delete this.attributes[name];
}

getBoundingClientRect(): FakeRect {
return this.rect;
}

scrollIntoView(): void {}

matches(selector: string): boolean {
return selector.split(",").some((part) => this.matchesOne(part.trim()));
}

closest(selector: string): FakeElement | null {
for (let node: FakeElement | null = this; node; node = node.parentElement) {
if (node.matches(selector)) return node;
}
return null;
}

querySelectorAll(selector: string): FakeElement[] {
const descendants = this.children.flatMap((child) => [child, ...child.querySelectorAll("*")]);
return descendants.filter((node) => node.matches(selector));
}

querySelector(selector: string): FakeElement | null {
return this.querySelectorAll(selector)[0] ?? null;
}

private matchesOne(selector: string): boolean {
if (selector === "*") return true;
if (selector === "form") return this.tagName === "FORM";
if (selector === "button") return this.tagName === "BUTTON";
if (selector === "textarea") return this.tagName === "TEXTAREA";
if (selector === '[contenteditable="true"]') return this.getAttribute("contenteditable") === "true";
if (selector === 'div[role="textbox"]') return this.tagName === "DIV" && this.getAttribute("role") === "textbox";
if (selector === '[aria-hidden="true"]') return this.getAttribute("aria-hidden") === "true";
if (selector === 'button[aria-label="Add files and more"]') {
return this.tagName === "BUTTON" && this.getAttribute("aria-label") === "Add files and more";
}
const presence = /^\[([\w-]+)\]$/.exec(selector);
if (presence) return this.getAttribute(presence[1]!) !== null;
return false;
}
}

class FakeDocument {
constructor(readonly roots: FakeElement[]) {}

querySelectorAll(selector: string): FakeElement[] {
const nodes = this.roots.flatMap((root) => [root, ...root.querySelectorAll("*")]);
return nodes.filter((node) => node.matches(selector));
}

querySelector(selector: string): FakeElement | null {
return this.querySelectorAll(selector)[0] ?? null;
}
}

function composer(options: { rect?: FakeRect; editorText?: string; formText?: string; toolsButton?: boolean } = {}): FakeElement {
const rect = options.rect ?? { x: 220, y: 700, width: 768, height: 52 };
const editor = new FakeElement("DIV", { role: "textbox", contenteditable: "true" }, rect);
editor.innerText = options.editorText ?? "";
const form = new FakeElement("FORM", {}, rect).append(editor);
if (options.toolsButton !== false) {
form.append(new FakeElement("BUTTON", { "aria-label": "Add files and more", type: "button", "aria-expanded": "false", "data-state": "closed" }, { ...rect, width: rect.width && 36, height: rect.height && 36 }));
}
form.innerText = options.formText ?? `${editor.innerText} Thinking effort Pro`;
return form;
}

const evaluate = <T>(expression: string, document: FakeDocument): T =>
new Function("document", "getComputedStyle", `return ${expression};`)(document, () => ({ pointerEvents: "auto" })) as T;

// Measured 2026-11-03 in the browser container: a new chat leaves the home
// page's composer mounted in a workspace with display: none, ahead of the
// conversation's composer in the document. A follow-up with --tool "Web search"
// turned the tool on in the visible composer, but the check read the hidden
// one ("Thinking effortPro"), so the send was refused with "Selected "Web
// search" but the composer never showed it as active." and the token was left
// in the composer.
describe("reading which composer tools are on", () => {
it("reads the visible composer, not a hidden workspace's", () => {
const hidden = composer({ rect: { x: 0, y: 0, width: 0, height: 0 }, formText: "Thinking effortPro" });
const visible = composer({ editorText: "Web search ", formText: "Web search Thinking effort Pro" });

const state = evaluate<{ active: string[] }>(activeComposerToolsExpression(["Web search"]), new FakeDocument([hidden, visible]));

expect(state.active).toEqual(["Web search"]);
});

it("does not report a tool that only a hidden workspace's composer shows", () => {
const hidden = composer({ rect: { x: 0, y: 0, width: 0, height: 0 }, editorText: "Web search ", formText: "Web search" });
const visible = composer();

const state = evaluate<{ active: string[] }>(activeComposerToolsExpression(["Web search"]), new FakeDocument([hidden, visible]));

expect(state.active).toEqual([]);
});
});

// "Edit message" on a user turn opens a form with its own editor inside the
// transcript, ahead of the composer, and with no tools button.
describe("finding the composer tools button", () => {
it("skips an open message editor in the transcript", () => {
const editing = composer({ rect: { x: 220, y: 300, width: 766, height: 133 }, toolsButton: false });
const turn = new FakeElement("DIV", { "data-chatgpt-search-unit-key": "fallback-turn-1:0:user" }).append(editing);

const hit = evaluate<{ ok: boolean; reason?: string; y?: number }>(
composerToolsButtonRectExpression(),
new FakeDocument([turn, composer()])
);

expect(hit).toMatchObject({ ok: true, y: 718 });
});
});
Loading