@devries-cameron20 / t3code Mirror · read-only / commits / ccae51a814
Fork 0

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 22, 23:15 UTC

fix(codex): settle compaction items when turns end (#17265)

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

luke committed Oct 20, 2026 at 05:53 UTC ccae51a814dfef101fb8d4b97a28bb55549d0592
3 files changed +173 -9
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+137
@@ -3322,6 +3322,143 @@ describe("CodexAdapterV2 post-settle continuation", () => {
3322 ),
3323 );
3324
3325 + it.effect.each(["failed", "interrupted", "Stop"] as const)(
3326 + "settles unfinished compaction on %s without changing completed compaction",
3327 + (outcome) =>
3328 + Effect.scoped(
3329 + Effect.gen(function* () {
3330 + const nativeThreadId = "compact-thread";
3331 + const nativeTurnId = "compact-turn";
3332 + const error = {
3333 + message: "Remote compaction stream disconnected",
3334 + codexErrorInfo: "other",
3335 + additionalDetails: null,
3336 + };
3337 + const transcript = makeCodexReplayTranscript({
3338 + scenario: `automatic-compaction-${outcome}`,
3339 + entries: [
3340 + ...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "Continue." }),
3341 + ...(
3342 + [
3343 + { method: "item/started", id: "completed-item", startedAtMs: 1782622441000 },
3344 + { method: "item/completed", id: "completed-item", completedAtMs: 1782622442000 },
3345 + { method: "item/started", id: "unfinished-item", startedAtMs: 1782622443000 },
3346 + ] as const
3347 + ).map(({ method, id, ...timestamps }) => ({
3348 + type: "emit_inbound" as const,
3349 + label: `${method}/${id}`,
3350 + frame: {
3351 + method,
3352 + params: {
3353 + threadId: nativeThreadId,
3354 + turnId: nativeTurnId,
3355 + item: { type: "contextCompaction", id },
3356 + ...timestamps,
3357 + },
3358 + },
3359 + })),
3360 + ...(outcome === "Stop"
3361 + ? [
3362 + {
3363 + type: "expect_outbound" as const,
3364 + label: "turn/interrupt",
3365 + frame: {
3366 + id: 4,
3367 + method: "turn/interrupt",
3368 + params: { threadId: nativeThreadId, turnId: nativeTurnId },
3369 + },
3370 + },
3371 + {
3372 + type: "emit_inbound" as const,
3373 + label: "turn/interrupt/failed",
3374 + frame: { id: 4, error: { code: -32_000, message: "Interrupt failed" } },
3375 + },
3376 + ]
3377 + : [
3378 + {
3379 + type: "emit_inbound" as const,
3380 + label: "turn/completed",
3381 + frame: {
3382 + method: "turn/completed",
3383 + params: {
3384 + threadId: nativeThreadId,
3385 + turn: {
3386 + ...makeCodexReplayTurn({ id: nativeTurnId, status: outcome }),
3387 + error: outcome === "failed" ? error : null,
3388 + },
3389 + },
3390 + },
3391 + },
3392 + ]),
3393 + ],
3394 + });
3395 + const compactionStarted = yield* Deferred.make<ProviderTurnId>();
3396 + const harness = yield* makeCodexReplayHarness(transcript, (event) =>
3397 + event.type === "turn_item.updated" &&
3398 + event.turnItem.nativeItemRef?.nativeId === "unfinished-item" &&
3399 + event.turnItem.status === "running" &&
3400 + event.turnItem.providerTurnId !== null
3401 + ? Deferred.succeed(compactionStarted, event.turnItem.providerTurnId)
3402 + : Effect.void,
3403 + );
3404 + yield* TestClock.setTime(1782622445000);
3405 + yield* harness.runtime.startTurn(
3406 + makeCodexTestTurnInput({
3407 + threadId: harness.threadId,
3408 + providerThread: harness.providerThread,
3409 + now: yield* DateTime.now,
3410 + attemptId: RunAttemptId.make(`compact-${outcome}-attempt`),
3411 + text: "Continue.",
3412 + }),
3413 + );
3414 + if (outcome === "Stop") {
3415 + const providerTurnId = yield* Deferred.await(compactionStarted);
3416 + const interrupt = yield* harness.runtime
3417 + .interruptTurn({ providerThread: harness.providerThread, providerTurnId })
3418 + .pipe(Effect.exit);
3419 + assert.equal(interrupt._tag, "Failure");
3420 + }
3421 + yield* harness.firstTerminal;
3422 + const items = harness.events.flatMap((event) =>
3423 + event.type === "turn_item.updated" && event.turnItem.type === "compaction"
3424 + ? [event.turnItem]
3425 + : [],
3426 + );
3427 + const completed = items.filter(
3428 + (item) => item.nativeItemRef?.nativeId === "completed-item",
3429 + );
3430 + const unfinished = items.filter(
3431 + (item) => item.nativeItemRef?.nativeId === "unfinished-item",
3432 + );
3433 + const status = outcome === "failed" ? "failed" : "interrupted";
3434 + assert.deepEqual(
3435 + unfinished.map((item) => item.status),
3436 + ["running", status],
3437 + );
3438 + assert.equal(unfinished[0]?.id, unfinished[1]?.id);
3439 + assert.deepEqual(unfinished[1]?.startedAt, DateTime.makeUnsafe(1782622443000));
3440 + assert.deepEqual(
3441 + unfinished[1]?.completedAt,
3442 + DateTime.makeUnsafe(outcome === "Stop" ? 1782622445000 : 1782622450000),
3443 + );
3444 + assert.deepEqual(
3445 + completed.map((item) => item.status),
3446 + ["running", "completed"],
3447 + );
3448 + assert.deepEqual(completed[1]?.startedAt, DateTime.makeUnsafe(1782622441000));
3449 + assert.deepEqual(completed[1]?.completedAt, DateTime.makeUnsafe(1782622442000));
3450 + assert.equal(harness.terminalEvents()[0]?.status, status);
3451 + if (outcome === "failed") {
3452 + assert.equal(harness.terminalEvents()[0]?.failure?.message, error.message);
3453 + }
3454 + }).pipe(
3455 + Effect.provide(
3456 + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3457 + ),
3458 + ),
3459 + ),
3460 + );
3461 +
3462 it.effect("resumes a provider thread without requesting or decoding its history", () =>
3463 Effect.scoped(
3464 Effect.gen(function* () {
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+34 -8
@@ -1157,6 +1157,7 @@ interface ActiveCodexTurnContext {
1157 readonly rootNodeId: OrchestrationV2ExecutionNode["id"];
1158 readonly subagent: CodexSubagentThreadContext | null;
1159 readonly startedAt: DateTime.Utc;
1160 + readonly runningCompactionItemIds: Set<string>;
1161 // Modern approval requests omit paths; keep the kinds without retaining patch bodies.
1162 readonly fileChangeKindsByItem: Map<string, CodexFileChangeKinds>;
1163 readonly itemPositions: Map<
@@ -2037,6 +2038,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
2038 startedAt: input.startedAt,
2039 itemPositions: new Map(),
2040 fileChangeKindsByItem: new Map(),
2041 + runningCompactionItemIds: new Set(),
2042 };
2043 yield* Ref.update(limitedTurnItems, (current) => {
2044 const next = new Map(current);
@@ -2618,6 +2620,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
2620 startedAt: turn.startedAt,
2621 itemPositions: new Map(),
2622 fileChangeKindsByItem: new Map(),
2623 + runningCompactionItemIds: new Set(),
2624 };
2625 beginTurnTokenUsage(activeContext);
2626 endSessionApprovalsOnModeChange(activeContext);
@@ -4627,15 +4630,16 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
4630 const emitCompactionItem = Effect.fn("CodexAdapterV2.emitCompactionItem")(function* (
4631 context: ActiveCodexTurnContext,
4632 nativeItemId: string,
4630 - status: "running" | "completed",
4631 - nativeStartedAt?: DateTime.Utc,
4633 + status: "running" | "completed" | "failed" | "interrupted" | "cancelled",
4634 + updatedAt: DateTime.Utc,
4635 ) {
4633 - const now = yield* DateTime.now;
4636 const { ordinal, startedAt } = yield* resolveItemPosition(
4637 context,
4638 nativeItemId,
4637 - nativeStartedAt,
4639 + updatedAt,
4640 );
4641 + if (status === "running") context.runningCompactionItemIds.add(nativeItemId);
4642 + else context.runningCompactionItemIds.delete(nativeItemId);
4643 yield* emitProviderEvent({
4644 type: "turn_item.updated",
4645 driver: CODEX_PROVIDER,
@@ -4655,10 +4659,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
4659 type: "compaction",
4660 driver: CODEX_PROVIDER,
4661 status,
4658 - title: status === "completed" ? "Context compacted" : "Compacting context",
4662 + title:
4663 + status === "running"
4664 + ? "Compacting context"
4665 + : status === "completed"
4666 + ? "Context compacted"
4667 + : status === "failed"
4668 + ? "Context compaction failed"
4669 + : "Context compaction stopped",
4670 startedAt,
4660 - completedAt: status === "completed" ? now : null,
4661 - updatedAt: now,
4671 + completedAt: status === "running" ? null : updatedAt,
4672 + updatedAt,
4673 },
4674 });
4675 });
@@ -4831,7 +4842,12 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
4842 }
4843
4844 if (payload.item.type === "contextCompaction") {
4834 - yield* emitCompactionItem(context, payload.item.id, "completed");
4845 + yield* emitCompactionItem(
4846 + context,
4847 + payload.item.id,
4848 + "completed",
4849 + DateTime.makeUnsafe(payload.completedAtMs),
4850 + );
4851 return;
4852 }
4853
@@ -5866,6 +5882,15 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
5882 for (const [key, part] of reasoningParts) {
5883 if (part.turnId === input.nativeTurnId) reasoningParts.delete(key);
5884 }
5885 + // Failed compaction can end the turn without item/completed.
5886 + for (const nativeItemId of input.context.runningCompactionItemIds) {
5887 + yield* emitCompactionItem(
5888 + input.context,
5889 + nativeItemId,
5890 + providerTurnStatusToTerminal(input.status),
5891 + input.completedAt,
5892 + );
5893 + }
5894 const nativeThreadId = input.context.providerThread.nativeThreadRef?.nativeId;
5895 const activation =
5896 nativeThreadId == null ? undefined : goalActivations.get(nativeThreadId);
@@ -6118,6 +6143,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6143 startedAt: now,
6144 itemPositions: new Map(),
6145 fileChangeKindsByItem: new Map(),
6146 + runningCompactionItemIds: new Set(),
6147 };
6148 const providerTurn = {
6149 id: context.providerTurnId,
packages/client-runtime/src/work-log/presentation.ts
+2 -1
@@ -1,4 +1,5 @@
1 import {
2 + isOrchestrationV2WorkActive,
3 isToolLifecycleItemType,
4 type AssetResource,
5 type RuntimeItemStatus,
@@ -70,7 +71,7 @@ export function contextCompactionLabel(item: CompactionLabelItem): string {
71 if (item.status === "failed") return "Context compaction failed";
72 if (item.status === "cancelled" || item.status === "interrupted")
73 return "Context compaction stopped";
73 - if (item.status === "running") return "Compacting context";
74 + if (isOrchestrationV2WorkActive(item.status)) return "Compacting context";
75 const tokens = contextCompactionTokenCounts(item);
76 return tokens
77 ? `Context compacted ${tokens.before} → ${tokens.after} tokens`