Skip to content

Commit d8333f2

Browse files
authored
Merge pull request #87 from youdie006/fix/followup-budget-on-send
fix: spend a follow-up only when its prompt is submitted
2 parents e70e7af + bda939c commit d8333f2

7 files changed

Lines changed: 207 additions & 33 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## Unreleased
88

99
### Fixed
10+
- 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`.
1011
- 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.
1112
- 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.
1213
- 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.

‎docs/clients.md‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,17 @@ follow-up checkpoint (integer 0-1000; default 5). For example:
5252
PRODEX_MAX_AUTO_FOLLOWUPS = "8"
5353
```
5454

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

6267
Ask the user whether to continue. Only after an explicit user request/approval may
6368
the caller send that continuation with `user_approved: true`. This human-directed

‎src/chatgpt-browser.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ export interface SendChatGptPromptOptions {
319319
diagnosticsCwd?: string;
320320
/** Progress callback so long sends can report phase + elapsed instead of staying silent. */
321321
onProgress?: (event: SendChatGptProgressEvent) => void;
322+
/**
323+
* Runs once the prompt is typed and checked, right before it is submitted.
324+
* Throwing stops the send with nothing submitted; the typed prompt stays in
325+
* the composer, and the next send clears it. The MCP follow-up budget is
326+
* spent here, so a send refused earlier does not use it up.
327+
*/
328+
beforePromptSubmit?: () => Promise<void>;
322329
/**
323330
* Send even when the requested model/effort could not be applied. Off by
324331
* default: an answer from a step nobody asked for is not a lesser version of
@@ -5178,6 +5185,7 @@ export async function sendChatGptPrompt(options: SendChatGptPromptOptions): Prom
51785185
if (last && expandCollapsedUserTurn(last.textNode)) return false;
51795186
return Boolean(last && userMessageText(last.textNode).includes(${JSON.stringify(`[prodex-request:${requestId}]`)}));
51805187
})()`;
5188+
await options.beforePromptSubmit?.();
51815189
// A dispatch can reach Chrome even if its acknowledgement is lost.
51825190
submissionAttempted = true;
51835191
await cdp.send("Input.dispatchKeyEvent", enterKeyEvent("keyDown"));

‎src/cli-pro.ts‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ import { getTokenExpiryStatus, loadBrowserDefaults, loadLocalConfig } from "./co
105105
import { sendPacingMarkerPath, withBrowserSendLock } from "./browser-send-lock.js";
106106
import { blockerCause, buildBlockerReport, CATCH_ALL_CODES, type BlockerConsult } from "./blocker-report.js";
107107
import { isChatGptConversationUrl, projectIdFromSidebar, resolveContinuationThread } from "./continue-thread.js";
108-
import { FollowupApprovalRequired, reserveFollowup, resolveMaxAutoFollowups, type FollowupBudget } from "./followup-budget.js";
108+
import { assertFollowupAvailable, FollowupApprovalRequired, reserveFollowup, resolveMaxAutoFollowups, type FollowupBudget } from "./followup-budget.js";
109109
import { readBridgeRoots } from "./registry.js";
110110
import { ProdexRequestIdSchema, SessionKeySchema } from "./schema.js";
111111
import { BridgeStore, MAX_FETCHABLE_RESULT_ARTIFACT_BYTES } from "./store.js";
@@ -1301,7 +1301,9 @@ export function createBrowserSendProgressPrinter(
13011301
export async function runAskProCommand(
13021302
rest: string[],
13031303
io: CliIO,
1304-
beforeSend?: (target: { store: BridgeStore; thread?: string; continuedFrom?: string }) => Promise<void>
1304+
// Runs before any task is created. The step it returns runs once the prompt
1305+
// is typed, right before it is submitted.
1306+
beforeSend?: (target: { store: BridgeStore; thread?: string; continuedFrom?: string }) => Promise<void | (() => Promise<void>)>
13051307
): Promise<number> {
13061308
const parsedAskPro = parseAskProArgs(rest);
13071309
const hasDryRunMode = parsedAskPro.optionArgs.includes("--dry-run");
@@ -1614,11 +1616,11 @@ export async function runAskProCommand(
16141616
if (hasSendMode) {
16151617
// MCP approval checkpoints run after target validation but before any
16161618
// task creation, pacing, browser recovery, or prompt send.
1617-
await beforeSend?.({
1619+
const beforePromptSubmit = (await beforeSend?.({
16181620
store: targetStore,
16191621
...(normalizedTargetUrl ? { thread: normalizedTargetUrl } : {}),
16201622
...(continuedFromTaskId ? { continuedFrom: continuedFromTaskId } : {})
1621-
});
1623+
})) || undefined;
16221624
const browserCommandOptions = {
16231625
cwd: targetCwd,
16241626
port: parsedAskPro.optionArgs.includes("--port") ? browserPort : undefined
@@ -1699,6 +1701,7 @@ export async function runAskProCommand(
16991701
proMode: selectionProMode,
17001702
effort: selectionEffort,
17011703
...(allowSelectionFallback ? { allowSelectionFallback: true } : {}),
1704+
...(beforePromptSubmit ? { beforePromptSubmit } : {}),
17021705
onProgress: createBrowserSendProgressPrinter(io.stderr)
17031706
});
17041707
});
@@ -1734,12 +1737,16 @@ export async function runAskProCommand(
17341737
if (recoveryNotes.length > 0) consult = { ...consult, warnings: [...recoveryNotes, ...consult.warnings] };
17351738
}
17361739
} catch (error) {
1737-
const blocker = await withManualStepForWindowMode(
1738-
sourceAwareBrowserBlocker(browserSendBlockerFromError(error), sourceCli, browserCommandOptions),
1739-
resolveCdpPort(browserPort),
1740-
sourceCli,
1741-
browserCommandOptions
1742-
);
1740+
// Another follow-up spent the budget while this one waited for the
1741+
// browser. Nothing was submitted; the caller asks the user instead.
1742+
const blocker = error instanceof FollowupApprovalRequired
1743+
? { code: "followup_approval_required", message: error.message, retryable: false }
1744+
: await withManualStepForWindowMode(
1745+
sourceAwareBrowserBlocker(browserSendBlockerFromError(error), sourceCli, browserCommandOptions),
1746+
resolveCdpPort(browserPort),
1747+
sourceCli,
1748+
browserCommandOptions
1749+
);
17431750
const message = blocker.next_step ? `${blocker.message} Next: ${blocker.next_step}` : errorMessage(error);
17441751
// The blocker text can quote the requested/default project name (e.g. a
17451752
// "project not found" error). Local stdout/stderr keep it (useful to the
@@ -1815,6 +1822,7 @@ export async function runAskProCommand(
18151822
)
18161823
);
18171824
}
1825+
if (error instanceof FollowupApprovalRequired) throw error;
18181826
throw new Error(formatBlockedConsultRecordedMessage(message, task.id, sourceCli, { cwd: targetCwd }));
18191827
}
18201828
const answerArtifactText = formatProConsultArtifact(consult);
@@ -2271,12 +2279,16 @@ export async function performBrowserConsultForMcp(
22712279
}, async ({ store, thread, continuedFrom }) => {
22722280
if (!thread || !continuedFrom) return;
22732281
continuationTarget = { taskId: continuedFrom, thread };
2274-
followupBudget = await reserveFollowup(store, {
2275-
thread,
2276-
taskId: continuedFrom,
2277-
userApproved: input.user_approved === true,
2278-
limit
2279-
});
2282+
const reservation = { thread, taskId: continuedFrom, userApproved: input.user_approved === true, limit };
2283+
await assertFollowupAvailable(store, reservation);
2284+
// Spent only when the prompt is about to be submitted, and once: a retry
2285+
// after browser recovery is the same follow-up.
2286+
let reserved = false;
2287+
return async () => {
2288+
if (reserved) return;
2289+
followupBudget = await reserveFollowup(store, reservation);
2290+
reserved = true;
2291+
};
22802292
});
22812293
} catch (error) {
22822294
if (error instanceof FollowupApprovalRequired && continuationTarget) {

‎src/followup-budget.ts‎

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,38 @@ export function resolveMaxAutoFollowups(
5151
return value;
5252
}
5353

54+
/**
55+
* Refuse a follow-up the budget no longer covers, without spending any of it.
56+
*
57+
* The follow-up is reserved only once its prompt is about to be submitted.
58+
* Measured 2026-11-02 in the browser container: two follow-ups in one
59+
* conversation, refused as composer_not_ready before anything was typed, were
60+
* recorded as its follow-ups 3 and 4 of 5. This check keeps an exhausted budget
61+
* from creating a task or touching the browser at all.
62+
*/
63+
export async function assertFollowupAvailable(
64+
store: BridgeStore,
65+
input: { thread: string; userApproved?: boolean; limit: number }
66+
): Promise<void> {
67+
await withFollowupBudgetLock(store, input, async (conversationKey) => {
68+
await readConversationLedger(store, { conversationKey, ...input });
69+
});
70+
}
71+
5472
export async function reserveFollowup(
5573
store: BridgeStore,
5674
input: { thread: string; taskId: string; userApproved?: boolean; limit: number }
5775
): Promise<FollowupBudget> {
76+
return withFollowupBudgetLock(store, input, async (conversationKey) =>
77+
reserveFollowupUnderLock(store, { ...input, conversationKey })
78+
);
79+
}
80+
81+
async function withFollowupBudgetLock<T>(
82+
store: BridgeStore,
83+
input: { thread: string; limit: number },
84+
run: (conversationKey: string) => Promise<T>
85+
): Promise<T> {
5886
assertFollowupLimit(input.limit);
5987
const conversationKey = conversationKeyFromThread(input.thread);
6088
await store.ensure();
@@ -73,19 +101,15 @@ export async function reserveFollowup(
73101
"The follow-up budget lock could not be recovered. Stop all prodex processes before removing the lock and its matching .reap claim, then retry."
74102
)
75103
},
76-
async () => reserveFollowupUnderLock(store, { ...input, conversationKey })
104+
async () => run(conversationKey)
77105
);
78106
}
79107

80-
async function reserveFollowupUnderLock(
108+
/** The conversation's ledger so far; throws when the budget does not cover another follow-up. */
109+
async function readConversationLedger(
81110
store: BridgeStore,
82-
input: {
83-
conversationKey: string;
84-
taskId: string;
85-
userApproved?: boolean;
86-
limit: number;
87-
}
88-
): Promise<FollowupBudget> {
111+
input: { conversationKey: string; userApproved?: boolean; limit: number }
112+
): Promise<{ sequence: number; used: number }> {
89113
const reservations = await readTrustedReservations(store);
90114
const conversationReservations = reservations
91115
.filter((reservation) => reservation.metadata.conversation_key === input.conversationKey)
@@ -99,6 +123,19 @@ async function reserveFollowupUnderLock(
99123
remaining: 0
100124
});
101125
}
126+
return previous;
127+
}
128+
129+
async function reserveFollowupUnderLock(
130+
store: BridgeStore,
131+
input: {
132+
conversationKey: string;
133+
taskId: string;
134+
userApproved?: boolean;
135+
limit: number;
136+
}
137+
): Promise<FollowupBudget> {
138+
const previous = await readConversationLedger(store, input);
102139

103140
const budget: FollowupBudget = input.userApproved === true
104141
? { limit: input.limit, used: 0, remaining: input.limit }

‎tests/chatgpt-browser.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,70 @@ Show more`;
218218
await expect(send).resolves.toMatchObject({ url: thread, answer: "the requested answer" });
219219
});
220220

221+
// A follow-up's budget pays for a prompt ChatGPT receives. Measured
222+
// 2026-11-02 in the browser container: two follow-ups refused as
223+
// composer_not_ready before anything was typed still counted against it.
224+
it("runs the pre-submit step after the prompt is typed and before it is submitted", async () => {
225+
vi.useFakeTimers();
226+
const thread = "https://chatgpt.com/c/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
227+
const answer = fakeAnswerState(thread, "the requested answer", false);
228+
const evaluations = installFakeChatGptSendCdp(thread, [
229+
{ ...fakeAnswerState(thread, "", false), assistantMessageCount: 0, userMessageCount: 0 },
230+
{ ...fakeAnswerState(thread, "", true), assistantMessageCount: 0, userMessageCount: 1 },
231+
answer,
232+
answer,
233+
answer
234+
]);
235+
const sent = vi.spyOn(FakeCdpWebSocket.prototype, "send");
236+
const enterPresses = () => sent.mock.calls.filter(([data]) => /"key":"Enter"/.test(String(data))).length;
237+
const seen: Array<{ typed: boolean; enterPresses: number }> = [];
238+
239+
const send = sendChatGptPrompt({
240+
port: 19338,
241+
prompt: "answer this",
242+
targetUrl: thread,
243+
timeoutMs: 10_000,
244+
beforePromptSubmit: async () => {
245+
seen.push({ typed: evaluations.some((expression) => expression.includes("actualText: raw.slice")), enterPresses: enterPresses() });
246+
}
247+
});
248+
await vi.advanceTimersByTimeAsync(10_000);
249+
250+
try {
251+
await expect(send).resolves.toMatchObject({ answer: "the requested answer" });
252+
expect(seen).toEqual([{ typed: true, enterPresses: 0 }]);
253+
expect(enterPresses()).toBeGreaterThan(0);
254+
} finally {
255+
sent.mockRestore();
256+
}
257+
});
258+
259+
it("does not submit a prompt whose pre-submit step refuses it", async () => {
260+
vi.useFakeTimers();
261+
const thread = "https://chatgpt.com/c/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
262+
installFakeChatGptSendCdp(thread, [{ ...fakeAnswerState(thread, "", false), assistantMessageCount: 0, userMessageCount: 0 }]);
263+
const sent = vi.spyOn(FakeCdpWebSocket.prototype, "send");
264+
const refusal = new Error("budget spent by another follow-up");
265+
266+
const send = sendChatGptPrompt({
267+
port: 19338,
268+
prompt: "answer this",
269+
targetUrl: thread,
270+
timeoutMs: 10_000,
271+
beforePromptSubmit: async () => {
272+
throw refusal;
273+
}
274+
});
275+
const rejection = expect(send).rejects.toBe(refusal);
276+
await vi.advanceTimersByTimeAsync(10_000);
277+
try {
278+
await rejection;
279+
expect(sent.mock.calls.some(([data]) => /"key":"Enter"/.test(String(data)))).toBe(false);
280+
} finally {
281+
sent.mockRestore();
282+
}
283+
});
284+
221285
it("stops on an answer the page failed to render instead of returning its error text", async () => {
222286
// The prompt has posted and ChatGPT holds the answer, so this must not be
223287
// retried; recovery reloads the thread and reads it again.

0 commit comments

Comments
 (0)