Skip to content

Commit 8466211

Browse files
Alb11747claude
andcommitted
feat(codex): predict the next prompt as composer ghost text
Add an opt-in Codex setting that asks ChatGPT's composer prediction service for the next prompt after each completed root turn and shows it through the shared prompt suggestion channel. Predictions need ChatGPT sign-in, follow custom model providers, and are revoked on the next send, goal command, rollback, or adapter shutdown. Co-Authored-By: Claude Opus 5.5 at medium effort <[email protected]>
1 parent 862a783 commit 8466211

7 files changed

Lines changed: 863 additions & 2 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type OrchestrationV2ProviderTurn,
2020
type OrchestrationV2RuntimeRequest,
2121
type OrchestrationV2TurnItem,
22+
type PromptSuggestion,
2223
ProjectId,
2324
type ProviderApprovalDecision,
2425
ProviderInstanceId,
@@ -41,6 +42,7 @@ import * as DateTime from "effect/DateTime";
4142
import * as Deferred from "effect/Deferred";
4243
import * as Crypto from "effect/Crypto";
4344
import * as Effect from "effect/Effect";
45+
import * as Exit from "effect/Exit";
4446
import * as Predicate from "effect/Predicate";
4547
import * as FileSystem from "effect/FileSystem";
4648
import { MCP_APP_OUTPUT_KEY, readMcpAppReference } from "@t3tools/shared/mcpApp";
@@ -52,7 +54,9 @@ import * as PlatformError from "effect/PlatformError";
5254
import * as Queue from "effect/Queue";
5355
import * as Ref from "effect/Ref";
5456
import * as Schema from "effect/Schema";
57+
import * as Scope from "effect/Scope";
5558
import * as Stream from "effect/Stream";
59+
import { FetchHttpClient } from "effect/http";
5660
import { TestClock } from "effect/testing";
5761
import { ChildProcess, ChildProcessSpawner } from "effect/process";
5862

@@ -2005,6 +2009,12 @@ describe("CodexAdapterV2 post-settle continuation", () => {
20052009
Effect.void,
20062010
onRequest: (method: string, params: unknown) => Effect.Effect<void> = () => Effect.void,
20072011
readChildMetadata?: Parameters<typeof withCodexReplayChildMetadata>[2],
2012+
adapterOptions: Partial<
2013+
Pick<CodexAdapterV2.CodexAdapterV2Options, "settings" | "promptSuggestions">
2014+
> = {},
2015+
decorateClient: (
2016+
client: CodexClient.CodexAppServerClient["Service"],
2017+
) => CodexClient.CodexAppServerClient["Service"] = (client) => client,
20082018
) =>
20092019
Effect.gen(function* () {
20102020
const fileSystem = yield* FileSystem.FileSystem;
@@ -2039,6 +2049,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
20392049
}) satisfies CodexClient.CodexAppServerClient["Service"],
20402050
),
20412051
Effect.provide(context),
2052+
Effect.map(decorateClient),
20422053
),
20432054
),
20442055
),
@@ -2058,6 +2069,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
20582069
continuationRequests.push(request);
20592070
}),
20602071
},
2072+
...adapterOptions,
20612073
});
20622074
const threadId = ThreadId.make(`thread-${transcript.scenario}`);
20632075
const runtime = yield* adapter.openSession({
@@ -2122,6 +2134,188 @@ describe("CodexAdapterV2 post-settle continuation", () => {
21222134
};
21232135
});
21242136

2137+
it.effect.each([
2138+
{ action: "next turn", late: false },
2139+
{ action: "rollback", late: false },
2140+
{ action: "close", late: false },
2141+
{ action: "next turn", late: true },
2142+
{ action: "rollback", late: true },
2143+
{ action: "close", late: true },
2144+
] as const)("revokes composer predictions on $action (late=$late)", ({ action, late }) =>
2145+
Effect.gen(function* () {
2146+
const nativeThreadId = "prediction-thread";
2147+
const nativeTurnId = "prediction-native-turn";
2148+
const prompt = "Implement";
2149+
const nextTurn: Array<CodexReplay.CodexAppServerReplayEntry> = [
2150+
{
2151+
type: "expect_outbound",
2152+
frame: {
2153+
id: 4,
2154+
method: "turn/start",
2155+
params: {
2156+
threadId: nativeThreadId,
2157+
input: [{ type: "text", text: "Next" }],
2158+
cwd: "/workspace",
2159+
model: "gpt-5.4",
2160+
approvalPolicy: "never",
2161+
approvalsReviewer: "user",
2162+
sandboxPolicy: { type: "dangerFullAccess" },
2163+
summary: "detailed",
2164+
},
2165+
},
2166+
},
2167+
{
2168+
type: "emit_inbound",
2169+
frame: {
2170+
id: 4,
2171+
result: { turn: makeCodexReplayTurn({ id: "next-turn", status: "inProgress" }) },
2172+
},
2173+
},
2174+
];
2175+
const transcript = makeCodexReplayTranscript({
2176+
scenario: `prediction-${action}-${late}`,
2177+
entries: [
2178+
...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt }),
2179+
{
2180+
type: "emit_inbound",
2181+
label: "completed",
2182+
frame: {
2183+
method: "turn/completed",
2184+
params: {
2185+
threadId: nativeThreadId,
2186+
turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }),
2187+
},
2188+
},
2189+
},
2190+
...(action === "next turn" ? nextTurn : []),
2191+
],
2192+
});
2193+
const published: Array<PromptSuggestion | null> = [];
2194+
const suggested = yield* Deferred.make<void>();
2195+
const requested = yield* Deferred.make<void>();
2196+
const responseReturned = yield* Deferred.make<void>();
2197+
let resolveResponse!: (response: Response) => void;
2198+
let signal: AbortSignal | null | undefined;
2199+
const fetch: typeof globalThis.fetch = async (_url, init) => {
2200+
if (init?.method === "GET") return Response.json({ is_enabled: true, prompt: "Predict" });
2201+
signal = init?.signal;
2202+
Deferred.doneUnsafe(requested, Effect.void);
2203+
if (!late)
2204+
return new Response(
2205+
'data: {"type":"response.output_text.delta","delta":"{\\"suggestion\\":\\"Add tests\\"}"}\n\ndata: {"type":"response.completed"}\n\n',
2206+
);
2207+
const response = await new Promise<Response>((resolve) => {
2208+
resolveResponse = resolve;
2209+
});
2210+
Deferred.doneUnsafe(responseReturned, Effect.void);
2211+
return response;
2212+
};
2213+
const sessionScope = yield* Scope.make();
2214+
yield* Effect.addFinalizer(() => Scope.close(sessionScope, Exit.void));
2215+
const harness = yield* makeCodexReplayHarness(
2216+
transcript,
2217+
undefined,
2218+
undefined,
2219+
undefined,
2220+
{
2221+
settings: { ...DEFAULT_CODEX_SETTINGS, promptSuggestions: true },
2222+
promptSuggestions: {
2223+
publish: (threadId, suggestion) =>
2224+
Effect.sync(() => {
2225+
assert.equal(threadId, ThreadId.make(`thread-${transcript.scenario}`));
2226+
published.push(suggestion);
2227+
}).pipe(
2228+
Effect.andThen(suggestion ? Deferred.succeed(suggested, undefined) : Effect.void),
2229+
),
2230+
},
2231+
},
2232+
(client) => ({
2233+
...client,
2234+
request: ((method: string, params: unknown) => {
2235+
if (method === "getAuthStatus")
2236+
return Effect.succeed({ authMethod: "chatgpt", authToken: "token" });
2237+
if (method === "config/read") return Effect.succeed({ config: {} });
2238+
if (method === "thread/read") return Effect.succeed({ thread: { turns: [] } });
2239+
return client.raw.request(method, params);
2240+
}) as CodexClient.CodexAppServerClient["Service"]["request"],
2241+
raw: {
2242+
...client.raw,
2243+
request: (method, params) =>
2244+
method === "account/read"
2245+
? Effect.succeed({
2246+
workspaceRouting: {
2247+
backendOrigin: "https://chatgpt.example",
2248+
chatgptAccountId: "account",
2249+
},
2250+
})
2251+
: client.raw.request(method, params),
2252+
},
2253+
}),
2254+
).pipe(
2255+
Effect.provideService(Scope.Scope, sessionScope),
2256+
Effect.provideService(FetchHttpClient.Fetch, fetch),
2257+
);
2258+
const turnInput = makeCodexTestTurnInput({
2259+
threadId: harness.threadId,
2260+
providerThread: harness.providerThread,
2261+
now: yield* DateTime.now,
2262+
attemptId: RunAttemptId.make("prediction-attempt"),
2263+
text: prompt,
2264+
});
2265+
yield* harness.runtime
2266+
.startTurn(turnInput)
2267+
.pipe(Effect.provideService(FetchHttpClient.Fetch, fetch));
2268+
yield* harness.firstTerminal;
2269+
yield* Deferred.await(requested);
2270+
if (!late) {
2271+
yield* Deferred.await(suggested);
2272+
assert.equal(published.at(-1)?.text, "Add tests");
2273+
assert.equal(published.at(-1)?.runId, turnInput.runId);
2274+
assert.match(published.at(-1)!.id, /^[0-9a-f-]{36}$/);
2275+
}
2276+
if (action === "next turn") {
2277+
yield* harness.runtime.startTurn({
2278+
...turnInput,
2279+
message: { ...turnInput.message, text: "Next" },
2280+
runId: RunId.make("next-run"),
2281+
attemptId: RunAttemptId.make("next-attempt"),
2282+
});
2283+
} else if (action === "rollback") {
2284+
yield* harness.runtime.rollbackThread({
2285+
providerThread: harness.providerThread,
2286+
target: {
2287+
type: "thread_start",
2288+
checkpointId: CheckpointId.make("prediction-checkpoint"),
2289+
appRunOrdinal: 0,
2290+
},
2291+
providerThreadTurns: [],
2292+
});
2293+
} else {
2294+
yield* Scope.close(sessionScope, Exit.void);
2295+
}
2296+
assert.isNull(published.at(-1));
2297+
if (late) {
2298+
assert.isTrue(signal?.aborted);
2299+
resolveResponse(
2300+
new Response(
2301+
'data: {"type":"response.output_text.delta","delta":"{\\"suggestion\\":\\"Too late\\"}"}\n\ndata: {"type":"response.completed"}\n\n',
2302+
),
2303+
);
2304+
yield* Deferred.await(responseReturned);
2305+
yield* Effect.yieldNow;
2306+
assert.isTrue(
2307+
published.every((value) => value === null),
2308+
"Late results must not publish",
2309+
);
2310+
}
2311+
}).pipe(
2312+
Effect.scoped,
2313+
Effect.provide(
2314+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
2315+
),
2316+
),
2317+
);
2318+
21252319
it.effect.each(["supported", "unsupported", "invalid"] as const)(
21262320
"delivers native history with %s app-server protocol",
21272321
(response) =>

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { revertCodexThread } from "../../provider/CodexThreadRevert.ts";
2+
import { makeCodexComposerPredictions } from "../../provider/CodexComposerPredictions.ts";
3+
import * as PromptSuggestions from "../../provider/PromptSuggestions.ts";
24
import { historyResponseItems } from "@t3tools/provider-core/server/handoffBudget";
35
import { makeProviderTextDeltaCoalescer } from "./ProviderTextDeltaCoalescer.ts";
46
import {
@@ -91,6 +93,7 @@ import * as Schema from "effect/Schema";
9193
import * as Scope from "effect/Scope";
9294
import * as Semaphore from "effect/Semaphore";
9395
import * as Stream from "effect/Stream";
96+
import { FetchHttpClient } from "effect/http";
9497
import { ChildProcess, ChildProcessSpawner } from "effect/process";
9598

9699
import { resolveAttachmentPath, resolveAttachmentPathById } from "../../attachmentStore.ts";
@@ -1608,6 +1611,7 @@ export const createCodexAdapterV2 = (
16081611
Effect.gen(function* () {
16091612
const clientFactory = yield* CodexAppServerClientFactory;
16101613
const continuationRequests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
1614+
const promptSuggestions = yield* PromptSuggestions.PromptSuggestions;
16111615
const crypto = yield* Crypto.Crypto;
16121616
const fileSystem = yield* FileSystem.FileSystem;
16131617
const hostEnvironment = yield* HostProcess.Environment;
@@ -1644,6 +1648,7 @@ export const createCodexAdapterV2 = (
16441648
idAllocator,
16451649
serverConfig,
16461650
continuationRequests,
1651+
promptSuggestions,
16471652
...hooks,
16481653
});
16491654
});
@@ -1695,6 +1700,7 @@ export interface CodexAdapterV2Options {
16951700
readonly environment: NodeJS.ProcessEnv;
16961701
readonly clientFactory: CodexAppServerClientFactoryShape;
16971702
readonly onUsageLimits?: ManagedServerProvider["applyUsageLimits"];
1703+
readonly promptSuggestions?: Pick<typeof PromptSuggestions.PromptSuggestions.Service, "publish">;
16981704
readonly resolveRuntime?: Effect.Effect<CodexEffectiveRuntime, ProviderSetupError, Scope.Scope>;
16991705
readonly crypto: Crypto.Crypto;
17001706
readonly fileSystem: FileSystem.FileSystem;
@@ -1719,6 +1725,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
17191725
threadId === null ? Effect.succeed(undefined) : mcpSessions.read(threadId);
17201726
const { clientFactory, crypto, fileSystem, idAllocator, serverConfig } = adapterOptions;
17211727
const continuationRequests = adapterOptions.continuationRequests;
1728+
const promptSuggestions = adapterOptions.promptSuggestions ?? { publish: () => Effect.void };
1729+
const composerPredictions = yield* makeCodexComposerPredictions.pipe(
1730+
Effect.provide(FetchHttpClient.layer),
1731+
);
17221732

17231733
return ProviderAdapter.ProviderAdapterV2.of({
17241734
instanceId: adapterOptions.instanceId,
@@ -1834,6 +1844,20 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
18341844
new Map<ProviderThreadId, Extract<OrchestrationV2TurnItem, { type: "error" }>>(),
18351845
);
18361846
const activeTurns = yield* Ref.make(new Map<string, ActiveCodexTurnContext>());
1847+
// Identity guards late predictions even if the HTTP request outlives interruption.
1848+
const predictions = new Map<
1849+
ThreadId,
1850+
{ context: ActiveCodexTurnContext; fiber?: Fiber.Fiber<void> }
1851+
>();
1852+
const revokePrediction = Effect.fnUntraced(function* (threadId: ThreadId) {
1853+
const prediction = predictions.get(threadId);
1854+
predictions.delete(threadId);
1855+
yield* promptSuggestions.publish(threadId, null);
1856+
if (prediction?.fiber) yield* Fiber.interrupt(prediction.fiber);
1857+
});
1858+
yield* Effect.addFinalizer(() =>
1859+
Effect.forEach(predictions.keys(), revokePrediction, { discard: true }),
1860+
);
18371861
const turnTokenUsageByThread = new Map<string, CodexTurnTokenUsageState>();
18381862
const usageStateForThread = (nativeThreadId: string) => {
18391863
let state = turnTokenUsageByThread.get(nativeThreadId);
@@ -2082,6 +2106,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
20822106
});
20832107
beginTurnTokenUsage(context);
20842108
endSessionApprovalsOnModeChange(context);
2109+
yield* revokePrediction(input.turnInput.threadId);
2110+
predictions.set(input.turnInput.threadId, { context });
20852111
yield* Ref.update(activeTurns, (current) => {
20862112
const updated = new Map(current);
20872113
updated.set(input.nativeTurnId, context);
@@ -6256,6 +6282,41 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
62566282
yield* Deferred.succeed(input.context.nativeStartReady, undefined);
62576283
}
62586284
yield* flushReadyRootTerminals();
6285+
const prediction = predictions.get(input.context.input.threadId);
6286+
if (
6287+
adapterOptions.settings.promptSuggestions &&
6288+
input.context.subagent === null &&
6289+
input.status === "completed" &&
6290+
!holdsForGoal &&
6291+
overloadAttempt === undefined &&
6292+
nativeThreadId != null &&
6293+
prediction?.context === input.context
6294+
) {
6295+
prediction.fiber = yield* Effect.gen(function* () {
6296+
const id = yield* crypto.randomUUIDv4();
6297+
const text = yield* composerPredictions.predict({
6298+
client,
6299+
environment: resolvedRuntime?.environment ?? adapterOptions.environment,
6300+
nativeThreadId,
6301+
model: input.context.input.modelSelection.model,
6302+
turnId: id,
6303+
});
6304+
if (text && predictions.get(input.context.input.threadId) === prediction) {
6305+
yield* promptSuggestions.publish(input.context.input.threadId, {
6306+
id,
6307+
runId: input.context.input.runId,
6308+
text,
6309+
});
6310+
}
6311+
}).pipe(
6312+
Effect.catch((cause) =>
6313+
Effect.logDebug("Codex composer prediction unavailable.", {
6314+
reason: cause._tag,
6315+
}),
6316+
),
6317+
Effect.forkIn(scope),
6318+
);
6319+
}
62596320
if (!retainSettledContext && !interruptInProgress) {
62606321
yield* Ref.update(runningCommandItemsByTurn, (current) => {
62616322
if (!current.has(input.nativeTurnId)) {
@@ -6939,8 +7000,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
69397000
turnInput.restartContinuationOfRunId === undefined
69407001
? parseCodexGoalCommand(turnInput.message.text)
69417002
: null;
6942-
if (goalCommand !== null) return runGoalCommand(turnInput, goalCommand);
7003+
if (goalCommand !== null) {
7004+
return revokePrediction(turnInput.threadId).pipe(
7005+
Effect.andThen(runGoalCommand(turnInput, goalCommand)),
7006+
);
7007+
}
69437008
return Effect.gen(function* () {
7009+
yield* revokePrediction(turnInput.threadId);
69447010
yield* startNativeTurn(
69457011
turnInput,
69467012
turnInput.restartContinuationOfRunId === undefined
@@ -7523,6 +7589,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
75237589
),
75247590
rollbackThread: (threadInput) =>
75257591
Effect.gen(function* () {
7592+
if (threadInput.providerThread.appThreadId !== null) {
7593+
yield* revokePrediction(threadInput.providerThread.appThreadId);
7594+
}
75267595
const threadId = yield* getNativeThreadId(threadInput.providerThread);
75277596
const numTurns = yield* resolveCodexRollbackTurnCount(threadInput);
75287597
const nativeConversationHeadRef =

0 commit comments

Comments
 (0)