Skip to content

Commit 82ad58f

Browse files
authored
fix: release the prompt suggestion thread and keep it minimal (#709)
The ephemeral thread that predicts the next prompt ran once per turn and was never released, used the user's default reasoning effort, and carried the full Codex context and tools. - Unsubscribe the thread after every generation, including failed and stale ones. - Run it with `effort: "low"`, the lowest effort `model/list` offers for the title model. - Start it read-only, with approvals off, short base instructions, and `project_doc_max_bytes: 0`.
1 parent ac8d925 commit 82ad58f

3 files changed

Lines changed: 93 additions & 18 deletions

File tree

‎docs/air-extensions.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,7 @@ After a turn ends normally, the adapter sends AIR the prompt the user is most li
14291429
- `suggestions: []` clears the prompt the client holds. The adapter sends it when a new prompt starts and when the new prediction is empty.
14301430
The adapter does not repeat a value it already sent.
14311431
- Codex does not predict prompts, so the adapter asks a cheap model on an ephemeral thread, one extra call per turn.
1432+
The thread is read-only, never asks for approval, uses low reasoning effort and a minimal system prompt, and is released after the call.
14321433
The prediction arrives after the prompt response. A prompt that starts first drops it.
14331434
- A turn that is cancelled or fails produces no prediction.
14341435
- `CODEX_ACP_PROMPT_SUGGESTIONS=false` turns the feature off.

‎src/PromptSuggestionGenerator.ts‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ export const PROMPT_SUGGESTIONS_META_KEY = "promptSuggestions";
88
// Each side of the exchange is cut so that a pasted log or a long answer does not cost its whole size.
99
const SOURCE_MAX_CHARS = 4_000;
1010

11+
// One short line needs no reasoning. "low" is the lowest effort `model/list` offers for TITLE_MODEL.
12+
const SUGGESTION_EFFORT = "low";
13+
14+
// Replaces Codex's large default system prompt, which is about coding with tools.
15+
const BASE_INSTRUCTIONS =
16+
"You are a text-only predictor. Never call tools or run commands. " +
17+
"Answer only with the JSON object the user message asks for.";
18+
1119
const OUTPUT_SCHEMA = {
1220
type: "object",
1321
properties: { suggestion: { type: "string" } },
@@ -84,20 +92,39 @@ export class PromptSuggestionGenerator {
8492
private async generateAndPublish(generation: number, userText: string, agentText: string): Promise<void> {
8593
if (!userText.trim() || !agentText.trim()) return;
8694

87-
const {thread} = await this.client.threadStart({cwd: this.cwd, ephemeral: true});
88-
const result = await this.client.runTurn({
89-
threadId: thread.id,
90-
input: [{
91-
type: "text",
92-
text: `${SYSTEM_PROMPT}\n\nLast user message:\n${head(userText)}\n\nLast assistant reply:\n${head(agentText)}`,
93-
text_elements: [],
94-
}],
95-
outputSchema: OUTPUT_SCHEMA,
96-
model: TITLE_MODEL,
95+
// Read-only and never-approve: a stray tool call can neither change anything nor prompt the user.
96+
// `project_doc_max_bytes: 0` is meant to keep AGENTS.md out of the context.
97+
// The app-server a generation starts on ends it, see `rebindAppServer`.
98+
const client = this.client;
99+
const {thread} = await client.threadStart({
100+
cwd: this.cwd,
101+
ephemeral: true,
102+
sandbox: "read-only",
103+
approvalPolicy: "never",
104+
baseInstructions: BASE_INSTRUCTIONS,
105+
config: {project_doc_max_bytes: 0},
97106
});
98-
99-
if (generation !== this.generation) return;
100-
await this.publishIfChanged(extractSuggestion(result.turn));
107+
try {
108+
const result = await client.runTurn({
109+
threadId: thread.id,
110+
input: [{
111+
type: "text",
112+
text: `${SYSTEM_PROMPT}\n\nLast user message:\n${head(userText)}\n\nLast assistant reply:\n${head(agentText)}`,
113+
text_elements: [],
114+
}],
115+
outputSchema: OUTPUT_SCHEMA,
116+
model: TITLE_MODEL,
117+
effort: SUGGESTION_EFFORT,
118+
});
119+
120+
if (generation !== this.generation) return;
121+
await this.publishIfChanged(extractSuggestion(result.turn));
122+
} finally {
123+
// The ephemeral thread would otherwise stay loaded in the app-server for its lifetime.
124+
await client.threadUnsubscribe({threadId: thread.id}).catch(() => {
125+
// best-effort; must not replace the outcome
126+
});
127+
}
101128
}
102129

103130
private async publishIfChanged(text: string | null): Promise<void> {

‎src/__tests__/PromptSuggestionGenerator.test.ts‎

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ function suggestionTurn(text: string): TurnResult {
1313
function createGenerator(runTurn: ReturnType<typeof vi.fn>) {
1414
const publish = vi.fn().mockResolvedValue(undefined);
1515
const threadStart = vi.fn().mockResolvedValue({thread: {id: "ephemeral"}});
16-
const client = {threadStart, runTurn} as unknown as CodexAppServerClient;
17-
return {generator: new PromptSuggestionGenerator(client, "/test/cwd", publish), publish, threadStart, runTurn};
16+
const threadUnsubscribe = vi.fn().mockResolvedValue({status: "unsubscribed"});
17+
const client = {threadStart, runTurn, threadUnsubscribe} as unknown as CodexAppServerClient;
18+
return {generator: new PromptSuggestionGenerator(client, "/test/cwd", publish), publish, threadStart, runTurn, threadUnsubscribe};
1819
}
1920

2021
async function flush() {
@@ -29,12 +30,57 @@ describe("PromptSuggestionGenerator", () => {
2930
generator.onTurnCompleted("fix the bug", "Fixed it.");
3031
await vi.waitFor(() => expect(publish).toHaveBeenCalledWith("Run the tests"));
3132

32-
expect(threadStart).toHaveBeenCalledWith({cwd: "/test/cwd", ephemeral: true});
33-
expect(runTurn.mock.calls[0]![0]).toMatchObject({threadId: "ephemeral", model: TITLE_MODEL});
33+
expect(threadStart).toHaveBeenCalledWith({
34+
cwd: "/test/cwd",
35+
ephemeral: true,
36+
sandbox: "read-only",
37+
approvalPolicy: "never",
38+
baseInstructions: expect.stringContaining("Never call tools"),
39+
config: {project_doc_max_bytes: 0},
40+
});
41+
expect(runTurn.mock.calls[0]![0]).toMatchObject({threadId: "ephemeral", model: TITLE_MODEL, effort: "low"});
3442
expect(runTurn.mock.calls[0]![0].input[0].text).toContain("fix the bug");
3543
expect(runTurn.mock.calls[0]![0].input[0].text).toContain("Fixed it.");
3644
});
3745

46+
/** The ephemeral thread is released after a successful turn, and a failing unsubscribe keeps the suggestion. */
47+
it("unsubscribes the thread after success", async () => {
48+
const {generator, publish, threadUnsubscribe} = createGenerator(vi.fn().mockResolvedValue(suggestionTurn("Go")));
49+
threadUnsubscribe.mockRejectedValue(new Error("gone"));
50+
51+
generator.onTurnCompleted("a", "b");
52+
await vi.waitFor(() => expect(threadUnsubscribe).toHaveBeenCalled());
53+
54+
expect(threadUnsubscribe).toHaveBeenCalledWith({threadId: "ephemeral"});
55+
expect(publish).toHaveBeenCalledWith("Go");
56+
});
57+
58+
/** The ephemeral thread is released when the turn fails, and a failing unsubscribe does not surface. */
59+
it("unsubscribes the thread after a runTurn failure", async () => {
60+
const {generator, publish, threadUnsubscribe} = createGenerator(vi.fn().mockRejectedValue(new Error("boom")));
61+
threadUnsubscribe.mockRejectedValue(new Error("gone"));
62+
63+
generator.onTurnCompleted("a", "b");
64+
await vi.waitFor(() => expect(threadUnsubscribe).toHaveBeenCalledWith({threadId: "ephemeral"}));
65+
await flush();
66+
67+
expect(publish).not.toHaveBeenCalled();
68+
});
69+
70+
/** A stale generation is dropped, and its thread is still released. */
71+
it("unsubscribes the thread when the generation is stale", async () => {
72+
const turn = deferred<TurnResult>();
73+
const {generator, publish, runTurn, threadUnsubscribe} = createGenerator(vi.fn().mockReturnValue(turn.promise));
74+
75+
generator.onTurnCompleted("a", "b");
76+
await vi.waitFor(() => expect(runTurn).toHaveBeenCalled());
77+
generator.invalidate();
78+
turn.resolve(suggestionTurn("Too late"));
79+
await vi.waitFor(() => expect(threadUnsubscribe).toHaveBeenCalledWith({threadId: "ephemeral"}));
80+
81+
expect(publish).not.toHaveBeenCalled();
82+
});
83+
3884
/** Each side of the exchange is cut to 4000 characters before it reaches the model. */
3985
it("truncates long inputs", async () => {
4086
const {generator, publish, runTurn} = createGenerator(vi.fn().mockResolvedValue(suggestionTurn("Go on")));
@@ -171,8 +217,9 @@ describe("PromptSuggestionGenerator", () => {
171217
const {generator} = createGenerator(vi.fn());
172218
const runTurn = vi.fn().mockResolvedValue(suggestionTurn("Go"));
173219
const threadStart = vi.fn().mockResolvedValue({thread: {id: "new"}});
220+
const threadUnsubscribe = vi.fn().mockResolvedValue({status: "unsubscribed"});
174221

175-
generator.rebindAppServer({threadStart, runTurn} as unknown as CodexAppServerClient);
222+
generator.rebindAppServer({threadStart, runTurn, threadUnsubscribe} as unknown as CodexAppServerClient);
176223
generator.onTurnCompleted("a", "b");
177224
await vi.waitFor(() => expect(runTurn).toHaveBeenCalled());
178225

0 commit comments

Comments
 (0)