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
- An MCP follow-up (`pro_consult` with `continue_task` or `continue_thread`) used up one of the automatic follow-ups (`PRODEX_MAX_AUTO_FOLLOWUPS`, default 5) even when the browser refused it before anything was typed. Measured 2026-11-02 in the browser container: two follow-ups in one conversation refused as `composer_not_ready` were recorded as its follow-ups 3 and 4 of 5, so the agent had one left with no prompt sent. A follow-up is now reserved once its prompt is typed, right before it is submitted. A follow-up the budget no longer covers is still refused before any task is created or the browser is touched, and a send that fails or is uncertain after submitting still counts. If another follow-up uses the last of the budget while this one waits for the browser, it stops before submitting as `awaiting_user`.
- 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
13 changes: 9 additions & 4 deletions docs/clients.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ follow-up checkpoint (integer 0-1000; default 5). For example:
PRODEX_MAX_AUTO_FOLLOWUPS = "8"
```

The initial fresh consult is not a follow-up. Reservations are counted before sends
and persist across reconnects, older task references, and different session keys in
the same bridge and conversation. Failed/uncertain attempts count too. At the limit,
The initial fresh consult is not a follow-up. A follow-up is reserved once its prompt
is typed, right before it is submitted, and reservations persist across reconnects,
older task references, and different session keys in the same bridge and
conversation. A send refused before that point, such as `composer_not_ready`, does
not count; an attempt that fails or is uncertain after submitting does. At the limit,
the tool returns `status: "awaiting_user"`, `task_id: null`, the intended continuation
arguments, and `followup_budget: {limit, used, remaining}` without creating a consult
task or sending a prompt. This response-only status is not a ledger task status.
task or sending a prompt. This response-only status is not a ledger task status. If
another follow-up in the same conversation uses the last of the budget while this one
waits for the browser, this one stops before submitting with the same response, and
its consult task is recorded as blocked with `followup_approval_required`.

Ask the user whether to continue. Only after an explicit user request/approval may
the caller send that continuation with `user_approved: true`. This human-directed
Expand Down
8 changes: 8 additions & 0 deletions src/chatgpt-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ export interface SendChatGptPromptOptions {
diagnosticsCwd?: string;
/** Progress callback so long sends can report phase + elapsed instead of staying silent. */
onProgress?: (event: SendChatGptProgressEvent) => void;
/**
* Runs once the prompt is typed and checked, right before it is submitted.
* Throwing stops the send with nothing submitted; the typed prompt stays in
* the composer, and the next send clears it. The MCP follow-up budget is
* spent here, so a send refused earlier does not use it up.
*/
beforePromptSubmit?: () => Promise<void>;
/**
* Send even when the requested model/effort could not be applied. Off by
* default: an answer from a step nobody asked for is not a lesser version of
Expand Down Expand Up @@ -5178,6 +5185,7 @@ export async function sendChatGptPrompt(options: SendChatGptPromptOptions): Prom
if (last && expandCollapsedUserTurn(last.textNode)) return false;
return Boolean(last && userMessageText(last.textNode).includes(${JSON.stringify(`[prodex-request:${requestId}]`)}));
})()`;
await options.beforePromptSubmit?.();
// A dispatch can reach Chrome even if its acknowledgement is lost.
submissionAttempted = true;
await cdp.send("Input.dispatchKeyEvent", enterKeyEvent("keyDown"));
Expand Down
44 changes: 28 additions & 16 deletions src/cli-pro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ import { getTokenExpiryStatus, loadBrowserDefaults, loadLocalConfig } from "./co
import { sendPacingMarkerPath, withBrowserSendLock } from "./browser-send-lock.js";
import { blockerCause, buildBlockerReport, CATCH_ALL_CODES, type BlockerConsult } from "./blocker-report.js";
import { isChatGptConversationUrl, projectIdFromSidebar, resolveContinuationThread } from "./continue-thread.js";
import { FollowupApprovalRequired, reserveFollowup, resolveMaxAutoFollowups, type FollowupBudget } from "./followup-budget.js";
import { assertFollowupAvailable, FollowupApprovalRequired, reserveFollowup, resolveMaxAutoFollowups, type FollowupBudget } from "./followup-budget.js";
import { readBridgeRoots } from "./registry.js";
import { ProdexRequestIdSchema, SessionKeySchema } from "./schema.js";
import { BridgeStore, MAX_FETCHABLE_RESULT_ARTIFACT_BYTES } from "./store.js";
Expand Down Expand Up @@ -1301,7 +1301,9 @@ export function createBrowserSendProgressPrinter(
export async function runAskProCommand(
rest: string[],
io: CliIO,
beforeSend?: (target: { store: BridgeStore; thread?: string; continuedFrom?: string }) => Promise<void>
// Runs before any task is created. The step it returns runs once the prompt
// is typed, right before it is submitted.
beforeSend?: (target: { store: BridgeStore; thread?: string; continuedFrom?: string }) => Promise<void | (() => Promise<void>)>
): Promise<number> {
const parsedAskPro = parseAskProArgs(rest);
const hasDryRunMode = parsedAskPro.optionArgs.includes("--dry-run");
Expand Down Expand Up @@ -1614,11 +1616,11 @@ export async function runAskProCommand(
if (hasSendMode) {
// MCP approval checkpoints run after target validation but before any
// task creation, pacing, browser recovery, or prompt send.
await beforeSend?.({
const beforePromptSubmit = (await beforeSend?.({
store: targetStore,
...(normalizedTargetUrl ? { thread: normalizedTargetUrl } : {}),
...(continuedFromTaskId ? { continuedFrom: continuedFromTaskId } : {})
});
})) || undefined;
const browserCommandOptions = {
cwd: targetCwd,
port: parsedAskPro.optionArgs.includes("--port") ? browserPort : undefined
Expand Down Expand Up @@ -1699,6 +1701,7 @@ export async function runAskProCommand(
proMode: selectionProMode,
effort: selectionEffort,
...(allowSelectionFallback ? { allowSelectionFallback: true } : {}),
...(beforePromptSubmit ? { beforePromptSubmit } : {}),
onProgress: createBrowserSendProgressPrinter(io.stderr)
});
});
Expand Down Expand Up @@ -1734,12 +1737,16 @@ export async function runAskProCommand(
if (recoveryNotes.length > 0) consult = { ...consult, warnings: [...recoveryNotes, ...consult.warnings] };
}
} catch (error) {
const blocker = await withManualStepForWindowMode(
sourceAwareBrowserBlocker(browserSendBlockerFromError(error), sourceCli, browserCommandOptions),
resolveCdpPort(browserPort),
sourceCli,
browserCommandOptions
);
// Another follow-up spent the budget while this one waited for the
// browser. Nothing was submitted; the caller asks the user instead.
const blocker = error instanceof FollowupApprovalRequired
? { code: "followup_approval_required", message: error.message, retryable: false }
: await withManualStepForWindowMode(
sourceAwareBrowserBlocker(browserSendBlockerFromError(error), sourceCli, browserCommandOptions),
resolveCdpPort(browserPort),
sourceCli,
browserCommandOptions
);
const message = blocker.next_step ? `${blocker.message} Next: ${blocker.next_step}` : errorMessage(error);
// The blocker text can quote the requested/default project name (e.g. a
// "project not found" error). Local stdout/stderr keep it (useful to the
Expand Down Expand Up @@ -1815,6 +1822,7 @@ export async function runAskProCommand(
)
);
}
if (error instanceof FollowupApprovalRequired) throw error;
throw new Error(formatBlockedConsultRecordedMessage(message, task.id, sourceCli, { cwd: targetCwd }));
}
const answerArtifactText = formatProConsultArtifact(consult);
Expand Down Expand Up @@ -2271,12 +2279,16 @@ export async function performBrowserConsultForMcp(
}, async ({ store, thread, continuedFrom }) => {
if (!thread || !continuedFrom) return;
continuationTarget = { taskId: continuedFrom, thread };
followupBudget = await reserveFollowup(store, {
thread,
taskId: continuedFrom,
userApproved: input.user_approved === true,
limit
});
const reservation = { thread, taskId: continuedFrom, userApproved: input.user_approved === true, limit };
await assertFollowupAvailable(store, reservation);
// Spent only when the prompt is about to be submitted, and once: a retry
// after browser recovery is the same follow-up.
let reserved = false;
return async () => {
if (reserved) return;
followupBudget = await reserveFollowup(store, reservation);
reserved = true;
};
});
} catch (error) {
if (error instanceof FollowupApprovalRequired && continuationTarget) {
Expand Down
55 changes: 46 additions & 9 deletions src/followup-budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,38 @@ export function resolveMaxAutoFollowups(
return value;
}

/**
* Refuse a follow-up the budget no longer covers, without spending any of it.
*
* The follow-up is reserved only once its prompt is about to be submitted.
* Measured 2026-11-02 in the browser container: two follow-ups in one
* conversation, refused as composer_not_ready before anything was typed, were
* recorded as its follow-ups 3 and 4 of 5. This check keeps an exhausted budget
* from creating a task or touching the browser at all.
*/
export async function assertFollowupAvailable(
store: BridgeStore,
input: { thread: string; userApproved?: boolean; limit: number }
): Promise<void> {
await withFollowupBudgetLock(store, input, async (conversationKey) => {
await readConversationLedger(store, { conversationKey, ...input });
});
}

export async function reserveFollowup(
store: BridgeStore,
input: { thread: string; taskId: string; userApproved?: boolean; limit: number }
): Promise<FollowupBudget> {
return withFollowupBudgetLock(store, input, async (conversationKey) =>
reserveFollowupUnderLock(store, { ...input, conversationKey })
);
}

async function withFollowupBudgetLock<T>(
store: BridgeStore,
input: { thread: string; limit: number },
run: (conversationKey: string) => Promise<T>
): Promise<T> {
assertFollowupLimit(input.limit);
const conversationKey = conversationKeyFromThread(input.thread);
await store.ensure();
Expand All @@ -73,19 +101,15 @@ export async function reserveFollowup(
"The follow-up budget lock could not be recovered. Stop all prodex processes before removing the lock and its matching .reap claim, then retry."
)
},
async () => reserveFollowupUnderLock(store, { ...input, conversationKey })
async () => run(conversationKey)
);
}

async function reserveFollowupUnderLock(
/** The conversation's ledger so far; throws when the budget does not cover another follow-up. */
async function readConversationLedger(
store: BridgeStore,
input: {
conversationKey: string;
taskId: string;
userApproved?: boolean;
limit: number;
}
): Promise<FollowupBudget> {
input: { conversationKey: string; userApproved?: boolean; limit: number }
): Promise<{ sequence: number; used: number }> {
const reservations = await readTrustedReservations(store);
const conversationReservations = reservations
.filter((reservation) => reservation.metadata.conversation_key === input.conversationKey)
Expand All @@ -99,6 +123,19 @@ async function reserveFollowupUnderLock(
remaining: 0
});
}
return previous;
}

async function reserveFollowupUnderLock(
store: BridgeStore,
input: {
conversationKey: string;
taskId: string;
userApproved?: boolean;
limit: number;
}
): Promise<FollowupBudget> {
const previous = await readConversationLedger(store, input);

const budget: FollowupBudget = input.userApproved === true
? { limit: input.limit, used: 0, remaining: input.limit }
Expand Down
64 changes: 64 additions & 0 deletions tests/chatgpt-browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,70 @@ Show more`;
await expect(send).resolves.toMatchObject({ url: thread, answer: "the requested answer" });
});

// A follow-up's budget pays for a prompt ChatGPT receives. Measured
// 2026-11-02 in the browser container: two follow-ups refused as
// composer_not_ready before anything was typed still counted against it.
it("runs the pre-submit step after the prompt is typed and before it is submitted", async () => {
vi.useFakeTimers();
const thread = "https://chatgpt.com/c/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
const answer = fakeAnswerState(thread, "the requested answer", false);
const evaluations = installFakeChatGptSendCdp(thread, [
{ ...fakeAnswerState(thread, "", false), assistantMessageCount: 0, userMessageCount: 0 },
{ ...fakeAnswerState(thread, "", true), assistantMessageCount: 0, userMessageCount: 1 },
answer,
answer,
answer
]);
const sent = vi.spyOn(FakeCdpWebSocket.prototype, "send");
const enterPresses = () => sent.mock.calls.filter(([data]) => /"key":"Enter"/.test(String(data))).length;
const seen: Array<{ typed: boolean; enterPresses: number }> = [];

const send = sendChatGptPrompt({
port: 19338,
prompt: "answer this",
targetUrl: thread,
timeoutMs: 10_000,
beforePromptSubmit: async () => {
seen.push({ typed: evaluations.some((expression) => expression.includes("actualText: raw.slice")), enterPresses: enterPresses() });
}
});
await vi.advanceTimersByTimeAsync(10_000);

try {
await expect(send).resolves.toMatchObject({ answer: "the requested answer" });
expect(seen).toEqual([{ typed: true, enterPresses: 0 }]);
expect(enterPresses()).toBeGreaterThan(0);
} finally {
sent.mockRestore();
}
});

it("does not submit a prompt whose pre-submit step refuses it", async () => {
vi.useFakeTimers();
const thread = "https://chatgpt.com/c/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
installFakeChatGptSendCdp(thread, [{ ...fakeAnswerState(thread, "", false), assistantMessageCount: 0, userMessageCount: 0 }]);
const sent = vi.spyOn(FakeCdpWebSocket.prototype, "send");
const refusal = new Error("budget spent by another follow-up");

const send = sendChatGptPrompt({
port: 19338,
prompt: "answer this",
targetUrl: thread,
timeoutMs: 10_000,
beforePromptSubmit: async () => {
throw refusal;
}
});
const rejection = expect(send).rejects.toBe(refusal);
await vi.advanceTimersByTimeAsync(10_000);
try {
await rejection;
expect(sent.mock.calls.some(([data]) => /"key":"Enter"/.test(String(data)))).toBe(false);
} finally {
sent.mockRestore();
}
});

it("stops on an answer the page failed to render instead of returning its error text", async () => {
// The prompt has posted and ChatGPT holds the answer, so this must not be
// retried; recovery reloads the thread and reads it again.
Expand Down
Loading
Loading