The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 12:15 UTC
fix(codex): settle unfinished web searches when turns end (#32177)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Sudharsan committed
Nov 15, 2026 at 09:44 UTC
6f06f2675d221ac353bd61be35f5ac782482d3a8
2 files changed
+83
-24
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+48
-16
@@ -3312,19 +3312,27 @@ describe("CodexAdapterV2 post-settle continuation", () => {
3312
),
3313
);
3314
3315
- it.effect.each(["failed", "interrupted", "Stop"] as const)(
3316
- "settles unfinished compaction on %s without changing completed compaction",
3317
- (outcome) =>
3315
+ it.effect.each([
3316
+ { itemType: "contextCompaction", outcome: "failed" },
3317
+ { itemType: "contextCompaction", outcome: "interrupted" },
3318
+ { itemType: "contextCompaction", outcome: "Stop" },
3319
+ { itemType: "webSearch", outcome: "completed" },
3320
+ { itemType: "webSearch", outcome: "failed" },
3321
+ { itemType: "webSearch", outcome: "interrupted" },
3322
+ { itemType: "webSearch", outcome: "Stop" },
3323
+ ] as const)(
3324
+ "settles unfinished $itemType on $outcome without changing completed items",
3325
+ ({ itemType, outcome }) =>
3326
Effect.gen(function* () {
3319
- const nativeThreadId = "compact-thread";
3320
- const nativeTurnId = "compact-turn";
3327
+ const nativeThreadId = "unfinished-item-thread";
3328
+ const nativeTurnId = "unfinished-item-turn";
3329
const error = {
3322
- message: "Remote compaction stream disconnected",
3330
+ message: "Remote item stream disconnected",
3331
codexErrorInfo: "other",
3332
additionalDetails: null,
3333
};
3334
const transcript = makeCodexReplayTranscript({
3327
- scenario: `automatic-compaction-${outcome}`,
3335
+ scenario: `unfinished-${itemType}-${outcome}`,
3336
entries: [
3337
...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "Continue." }),
3338
...(
@@ -3341,7 +3349,11 @@ describe("CodexAdapterV2 post-settle continuation", () => {
3349
params: {
3350
threadId: nativeThreadId,
3351
turnId: nativeTurnId,
3344
- item: { type: "contextCompaction", id },
3352
+ item: {
3353
+ type: itemType,
3354
+ id,
3355
+ ...(itemType === "webSearch" ? { query: "T3 Code", action: null } : {}),
3356
+ },
3357
...timestamps,
3358
},
3359
},
@@ -3381,13 +3393,13 @@ describe("CodexAdapterV2 post-settle continuation", () => {
3393
]),
3394
],
3395
});
3384
- const compactionStarted = yield* Deferred.make<ProviderTurnId>();
3396
+ const itemStarted = yield* Deferred.make<ProviderTurnId>();
3397
const harness = yield* makeCodexReplayHarness(transcript, (event) =>
3398
event.type === "turn_item.updated" &&
3399
event.turnItem.nativeItemRef?.nativeId === "unfinished-item" &&
3400
event.turnItem.status === "running" &&
3401
event.turnItem.providerTurnId !== null
3390
- ? Deferred.succeed(compactionStarted, event.turnItem.providerTurnId)
3402
+ ? Deferred.succeed(itemStarted, event.turnItem.providerTurnId)
3403
: Effect.void,
3404
);
3405
yield* TestClock.setTime(1782622445000);
@@ -3396,12 +3408,12 @@ describe("CodexAdapterV2 post-settle continuation", () => {
3408
threadId: harness.threadId,
3409
providerThread: harness.providerThread,
3410
now: yield* DateTime.now,
3399
- attemptId: RunAttemptId.make(`compact-${outcome}-attempt`),
3411
+ attemptId: RunAttemptId.make(`unfinished-${itemType}-${outcome}-attempt`),
3412
text: "Continue.",
3413
}),
3414
);
3415
if (outcome === "Stop") {
3404
- const providerTurnId = yield* Deferred.await(compactionStarted);
3416
+ const providerTurnId = yield* Deferred.await(itemStarted);
3417
const interrupt = yield* harness.runtime
3418
.interruptTurn({ providerThread: harness.providerThread, providerTurnId })
3419
.pipe(Effect.exit);
@@ -3409,7 +3421,8 @@ describe("CodexAdapterV2 post-settle continuation", () => {
3421
}
3422
yield* harness.firstTerminal;
3423
const items = harness.events.flatMap((event) =>
3412
- event.type === "turn_item.updated" && event.turnItem.type === "compaction"
3424
+ event.type === "turn_item.updated" &&
3425
+ event.turnItem.type === (itemType === "webSearch" ? "web_search" : "compaction")
3426
? [event.turnItem]
3427
: [],
3428
);
@@ -3417,13 +3430,26 @@ describe("CodexAdapterV2 post-settle continuation", () => {
3430
const unfinished = items.filter(
3431
(item) => item.nativeItemRef?.nativeId === "unfinished-item",
3432
);
3420
- const status = outcome === "failed" ? "failed" : "interrupted";
3433
+ const status =
3434
+ outcome === "completed" ? "cancelled" : outcome === "failed" ? "failed" : "interrupted";
3435
assert.deepEqual(
3436
unfinished.map((item) => item.status),
3437
["running", status],
3438
);
3439
assert.equal(unfinished[0]?.id, unfinished[1]?.id);
3440
assert.deepEqual(unfinished[1]?.startedAt, DateTime.makeUnsafe(1782622443000));
3441
+ if (unfinished[1]?.type === "web_search") {
3442
+ assert.deepEqual(unfinished[1].patterns, ["T3 Code"]);
3443
+ assert.deepEqual(
3444
+ harness.events.flatMap((event) =>
3445
+ event.type === "node.updated" &&
3446
+ event.node.nativeItemRef?.nativeId === "unfinished-item"
3447
+ ? [event.node.status]
3448
+ : [],
3449
+ ),
3450
+ ["running", status],
3451
+ );
3452
+ }
3453
assert.deepEqual(
3454
unfinished[1]?.completedAt,
3455
DateTime.makeUnsafe(outcome === "Stop" ? 1782622445000 : 1782622450000),
@@ -3433,8 +3459,14 @@ describe("CodexAdapterV2 post-settle continuation", () => {
3459
["running", "completed"],
3460
);
3461
assert.deepEqual(completed[1]?.startedAt, DateTime.makeUnsafe(1782622441000));
3436
- assert.deepEqual(completed[1]?.completedAt, DateTime.makeUnsafe(1782622442000));
3437
- assert.equal(harness.terminalEvents()[0]?.status, status);
3462
+ assert.deepEqual(
3463
+ completed[1]?.completedAt,
3464
+ DateTime.makeUnsafe(itemType === "webSearch" ? 1782622445000 : 1782622442000),
3465
+ );
3466
+ assert.equal(
3467
+ harness.terminalEvents()[0]?.status,
3468
+ outcome === "Stop" ? "interrupted" : outcome,
3469
+ );
3470
if (outcome === "failed") {
3471
assert.equal(harness.terminalEvents()[0]?.failure?.message, error.message);
3472
}
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+35
-8
@@ -1280,6 +1280,7 @@ interface ActiveCodexTurnContext {
1280
readonly subagent: CodexSubagentThreadContext | null;
1281
readonly startedAt: DateTime.Utc;
1282
readonly runningCompactionItemIds: Set<string>;
1283
+ readonly runningWebSearchItems: Map<string, CodexWebSearchItem>;
1284
// Modern approval requests omit paths; keep the kinds without retaining patch bodies.
1285
readonly fileChangeKindsByItem: Map<string, CodexFileChangeKinds>;
1286
readonly messagePhases: Map<string, "commentary" | "partial_answer" | "final_answer">;
@@ -2441,6 +2442,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
2442
fileChangeKindsByItem: new Map(),
2443
messagePhases: new Map(),
2444
runningCompactionItemIds: new Set(),
2445
+ runningWebSearchItems: new Map(),
2446
};
2447
const nativeThreadId = input.turnInput.providerThread.nativeThreadRef?.nativeId;
2448
const continuation =
@@ -3070,6 +3072,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
3072
fileChangeKindsByItem: new Map(),
3073
messagePhases: new Map(),
3074
runningCompactionItemIds: new Set(),
3075
+ runningWebSearchItems: new Map(),
3076
};
3077
beginTurnTokenUsage(activeContext);
3078
endSessionApprovalsOnModeChange(activeContext);
@@ -4114,10 +4117,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
4117
function* (input: {
4118
readonly context: ActiveCodexTurnContext;
4119
readonly item: CodexWebSearchItem;
4117
- readonly completed: boolean;
4120
+ readonly status: "running" | "completed" | "cancelled" | "failed" | "interrupted";
4121
+ readonly updatedAt?: DateTime.Utc;
4122
}) {
4119
- const updatedAt = yield* DateTime.now;
4120
- const completedAt = input.completed ? updatedAt : null;
4123
+ const updatedAt = input.updatedAt ?? (yield* DateTime.now);
4124
+ const completedAt = input.status === "running" ? null : updatedAt;
4125
const nodeId = idAllocator.derive.nodeFromProviderItem({
4126
driver: CODEX_PROVIDER,
4127
nativeItemId: input.item.id,
@@ -4128,7 +4132,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
4132
});
4133
const { ordinal, startedAt } = yield* resolveItemPosition(input.context, input.item.id);
4134
const patterns = webSearchPatterns(input.item);
4131
- const status = input.completed ? "completed" : "running";
4135
+ const { status } = input;
4136
const node: OrchestrationV2ExecutionNode = {
4137
id: nodeId,
4138
threadId: input.context.projectionThreadId,
@@ -5449,10 +5453,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
5453
return;
5454
}
5455
5456
+ context.runningWebSearchItems.set(payload.item.id, payload.item);
5457
const artifacts = yield* buildWebSearchArtifacts({
5458
context,
5459
item: payload.item,
5455
- completed: false,
5460
+ status: "running",
5461
});
5462
yield* emitProviderEvent({
5463
type: "node.updated",
@@ -5633,10 +5638,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
5638
}
5639
5640
if (payload.item.type === "webSearch") {
5641
+ context.runningWebSearchItems.delete(payload.item.id);
5642
const artifacts = yield* buildWebSearchArtifacts({
5643
context,
5644
item: payload.item,
5639
- completed: true,
5645
+ status: "completed",
5646
});
5647
yield* emitProviderEvent({
5648
type: "node.updated",
@@ -6937,14 +6943,34 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6943
);
6944
yield* terminalizeMcpAppCaptures(input.context, input.nativeTurnId);
6945
}
6940
- const dynamicToolStatus: "cancelled" | "interrupted" | "failed" =
6946
+ const unfinishedToolStatus: "cancelled" | "interrupted" | "failed" =
6947
input.status === "interrupted" || input.status === "failed"
6948
? input.status
6949
: "cancelled";
6950
+ // A completed turn does not establish whether an unfinished search succeeded.
6951
+ for (const item of input.context.runningWebSearchItems.values()) {
6952
+ const artifacts = yield* buildWebSearchArtifacts({
6953
+ context: input.context,
6954
+ item,
6955
+ status: unfinishedToolStatus,
6956
+ updatedAt: input.completedAt,
6957
+ });
6958
+ yield* emitProviderEvent({
6959
+ type: "node.updated",
6960
+ driver: CODEX_PROVIDER,
6961
+ node: artifacts.node,
6962
+ });
6963
+ yield* emitProviderEvent({
6964
+ type: "turn_item.updated",
6965
+ driver: CODEX_PROVIDER,
6966
+ turnItem: artifacts.turnItem,
6967
+ });
6968
+ }
6969
+ input.context.runningWebSearchItems.clear();
6970
yield* terminalizeRunningDynamicTools(
6971
input.context,
6972
input.nativeTurnId,
6947
- dynamicToolStatus,
6973
+ unfinishedToolStatus,
6974
input.completedAt,
6975
input.status === "interrupted" || input.status === "failed",
6976
);
@@ -7288,6 +7314,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7314
fileChangeKindsByItem: new Map(),
7315
messagePhases: new Map(),
7316
runningCompactionItemIds: new Set(),
7317
+ runningWebSearchItems: new Map(),
7318
};
7319
const providerTurn = {
7320
id: context.providerTurnId,