@devries-cameron20 / t3code Mirror · read-only / commits / aced664ed9
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 21, 06:00 UTC

fix(client-runtime): prefer newer completed compaction over stale liveUsage (#39024)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Helmi Pradita committed Nov 19, 2026 at 16:45 UTC aced664ed93911657dc84da5b438b961e31be4ec
6 files changed +276 -46
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+48
@@ -330,6 +330,7 @@ const resumed = (
330 readonly lastRunOrdinal?: number;
331 readonly pendingBackgroundTasks?: OrchestrationV2ProviderThread["pendingBackgroundTasks"];
332 readonly goal?: OrchestrationV2ProviderThread["goal"];
333 + readonly contextUsage?: OrchestrationV2ProviderThread["contextUsage"];
334 readonly external?: boolean;
335 readonly supervised?: boolean;
336 readonly processExit?: Effect.Effect<void>;
@@ -398,6 +399,7 @@ const resumed = (
399 ? {}
400 : { lastRunOrdinal: options.lastRunOrdinal }),
401 ...(options?.goal === undefined ? {} : { goal: options.goal }),
402 + ...(options?.contextUsage === undefined ? {} : { contextUsage: options.contextUsage }),
403 ...(options?.pendingBackgroundTasks === undefined
404 ? {}
405 : { pendingBackgroundTasks: options.pendingBackgroundTasks }),
@@ -509,6 +511,52 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
511 }),
512 );
513
514 + it.effect("drops context usage carried over from before a completed compaction", () =>
515 + Effect.gen(function* () {
516 + const { runtime, thread } = yield* resumed(
517 + [
518 + out("session.prompt", { sessionID: SESSION, text: "<any>" }),
519 + promptAccepted,
520 + event("session.step.ended", {
521 + sessionID: SESSION,
522 + assistantMessageID: "msg_0eb735d5b001oAFVeY5jz3WD4Z",
523 + finish: "stop",
524 + cost: 0,
525 + tokens: { input: 150_000, output: 40, reasoning: 0, cache: { read: 0, write: 0 } },
526 + }),
527 + event("session.compaction.started", { sessionID: SESSION, reason: "manual", recent: "" }),
528 + event("session.compaction.ended", {
529 + sessionID: SESSION,
530 + reason: "manual",
531 + text: "Summary",
532 + recent: "",
533 + }),
534 + event("session.execution.succeeded", { sessionID: SESSION }),
535 + ],
536 + { contextUsage: { usedTokens: 150_000, maxTokens: 160_000 } },
537 + );
538 + assert.equal(thread.contextUsage?.usedTokens, 150_000);
539 + const collected = yield* runtime.events.pipe(
540 + Stream.takeUntil((event) => event.type === "turn.terminal"),
541 + Stream.runCollect,
542 + Effect.forkScoped,
543 + );
544 + yield* runtime.startTurn(turnInput(thread));
545 + const seen = yield* Fiber.join(collected);
546 + const usage = seen.flatMap((event) =>
547 + event.type === "provider_thread.updated" ? [event.providerThread.contextUsage] : [],
548 + );
549 + assert.isNull(usage.at(-1));
550 + const settled = seen.findLast(
551 + (event) =>
552 + event.type === "provider_turn.updated" && event.providerTurn.completedAt !== null,
553 + );
554 + assert.isUndefined(
555 + settled?.type === "provider_turn.updated" ? settled.providerTurn.tokenUsage : "missing",
556 + );
557 + }),
558 + );
559 +
560 it.effect("never sends a saved variant the catalog model does not list", () =>
561 Effect.gen(function* () {
562 const { runtime, thread } = yield* resumed([
apps/web/src/lib/contextWindow.test.ts
+45
@@ -70,6 +70,51 @@ describe("V2 context window presentation", () => {
70 ?.usedTokens,
71 ).toBe(2_000);
72 });
73 +
74 + it("prefers a completed compaction over usage reported before it", () => {
75 + const usageBeforeCompaction = decodeTokenUsage({
76 + usedTokens: 985_000,
77 + maxTokens: 1_000_000,
78 + updatedAt: "2026-06-19T23:55:00.000Z",
79 + });
80 + const snapshot = deriveLatestContextWindowSnapshot(
81 + [compactionEntry(null)],
82 + usageBeforeCompaction,
83 + );
84 + expect(snapshot?.usedTokens).toBe(2_000);
85 + expect(snapshot?.maxTokens).toBe(1_000_000);
86 +
87 + // A report in the compaction's own millisecond is the provider's post-compaction usage.
88 + const reportedWithCompaction = decodeTokenUsage({
89 + usedTokens: 2_000,
90 + maxTokens: 1_000_000,
91 + autoCompactThreshold: 900_000,
92 + updatedAt: "2026-06-20T00:00:00.000Z",
93 + });
94 + expect(
95 + deriveLatestContextWindowSnapshot([compactionEntry(null)], reportedWithCompaction)
96 + ?.autoCompactThreshold,
97 + ).toBe(900_000);
98 +
99 + const { afterTokenCount: _, ...withoutCount } = compactionEntry(null).item;
100 + expect(
101 + deriveLatestContextWindowSnapshot([{ item: withoutCount }], usageBeforeCompaction),
102 + ).toBeNull();
103 + // A metadata-only provider thread update reports no usage, so an older count stays hidden.
104 + const olderCompaction = {
105 + item: {
106 + ...compactionEntry(null).item,
107 + updatedAt: DateTime.makeUnsafe("2026-06-19T00:00:00.000Z"),
108 + },
109 + };
110 + expect(
111 + deriveLatestContextWindowSnapshot([olderCompaction, { item: withoutCount }], null, {
112 + id: ProviderThreadId.make("thread"),
113 + contextUsage: null,
114 + updatedAt: DateTime.makeUnsafe("2026-06-21T00:00:00.000Z"),
115 + }),
116 + ).toBeNull();
117 + });
118 });
119
120 describe("live provider-turn usage (#8144)", () => {
packages/client-runtime/src/state/contextWindow.ts
+80 -46
@@ -76,6 +76,54 @@ export function latestProviderTurnTokenUsage(
76 );
77 }
78
79 +type CompactionItem = Extract<ThreadStructureTurnItem, { readonly type: "compaction" }>;
80 +
81 +// A retired provider thread's items say nothing about the active conversation.
82 +function isRetiredProviderThreadItem(
83 + item: CompactionItem,
84 + providerThread: Pick<OrchestrationV2ProviderThread, "id"> | null | undefined,
85 +): boolean {
86 + return (
87 + providerThread != null &&
88 + item.providerThreadId != null &&
89 + item.providerThreadId !== providerThread.id
90 + );
91 +}
92 +
93 +function compactionSnapshot(
94 + item: CompactionItem,
95 + maxTokens: number | null,
96 + updatedAt: string,
97 +): ContextWindowSnapshot | null {
98 + const usedTokens = asFiniteNumber(item.afterTokenCount);
99 + if (usedTokens === null || usedTokens < 0) return null;
100 + const usedPercentage =
101 + maxTokens !== null && maxTokens > 0 ? Math.min(100, (usedTokens / maxTokens) * 100) : null;
102 + return {
103 + usedTokens,
104 + totalProcessedTokens: asFiniteNumber(item.beforeTokenCount),
105 + maxTokens,
106 + remainingTokens: maxTokens !== null ? Math.max(0, Math.round(maxTokens - usedTokens)) : null,
107 + usedPercentage,
108 + remainingPercentage: usedPercentage !== null ? Math.max(0, 100 - usedPercentage) : null,
109 + inputTokens: null,
110 + cachedInputTokens: null,
111 + outputTokens: null,
112 + reasoningOutputTokens: null,
113 + lastUsedTokens: null,
114 + lastInputTokens: null,
115 + lastCachedInputTokens: null,
116 + lastOutputTokens: null,
117 + lastReasoningOutputTokens: null,
118 + toolUses: null,
119 + durationMs: null,
120 + compactsAutomatically: true,
121 + autoCompactThreshold: null,
122 + cost: null,
123 + updatedAt,
124 + };
125 +}
126 +
127 export function deriveLatestContextWindowSnapshot(
128 entries: ReadonlyArray<{
129 readonly item: ThreadStructureTurnItem;
@@ -83,6 +131,30 @@ export function deriveLatestContextWindowSnapshot(
131 liveUsage?: OrchestrationV2ProviderTurnTokenUsage | null,
132 providerThread?: Pick<OrchestrationV2ProviderThread, "id" | "contextUsage" | "updatedAt"> | null,
133 ): ContextWindowSnapshot | null {
134 + const latestCompletedCompaction = entries.findLast(
135 + (entry): entry is { readonly item: CompactionItem } =>
136 + entry.item.type === "compaction" &&
137 + entry.item.status === "completed" &&
138 + !isRetiredProviderThreadItem(entry.item, providerThread),
139 + )?.item;
140 + const usageUpdatedAt =
141 + liveUsage?.updatedAt ??
142 + (providerThread?.contextUsage != null ? DateTime.formatIso(providerThread.updatedAt) : null);
143 + if (latestCompletedCompaction !== undefined) {
144 + const compactedAt = DateTime.formatIso(
145 + latestCompletedCompaction.completedAt ?? latestCompletedCompaction.updatedAt,
146 + );
147 + // Usage reported before the compaction describes the context it just discarded; usage at
148 + // the same instant is the provider's own post-compaction report.
149 + if (usageUpdatedAt === null || Date.parse(compactedAt) > Date.parse(usageUpdatedAt)) {
150 + return compactionSnapshot(
151 + latestCompletedCompaction,
152 + liveUsage?.maxTokens ?? asFiniteNumber(providerThread?.contextUsage?.maxTokens),
153 + compactedAt,
154 + );
155 + }
156 + }
157 +
158 if (liveUsage != null) {
159 const usedTokens = Math.max(0, liveUsage.usedTokens);
160 const maxTokens = liveUsage.maxTokens ?? null;
@@ -151,54 +223,16 @@ export function deriveLatestContextWindowSnapshot(
223 };
224 }
225 for (let index = entries.length - 1; index >= 0; index -= 1) {
154 - const entry = entries[index];
155 - if (!entry || entry.item.type !== "compaction") {
156 - continue;
157 - }
158 - const payload = entry.item;
159 - // A retired provider thread's compaction says nothing about the active conversation.
160 - if (
161 - providerThread != null &&
162 - payload.providerThreadId != null &&
163 - payload.providerThreadId !== providerThread.id
164 - ) {
165 - continue;
166 - }
167 - const usedTokens = asFiniteNumber(payload.afterTokenCount);
168 - if (usedTokens === null || usedTokens < 0) {
226 + const item = entries[index]?.item;
227 + if (item?.type !== "compaction" || isRetiredProviderThreadItem(item, providerThread)) {
228 continue;
229 }
171 -
172 - const maxTokens = null;
173 - const usedPercentage =
174 - maxTokens !== null && maxTokens > 0 ? Math.min(100, (usedTokens / maxTokens) * 100) : null;
175 - const remainingTokens =
176 - maxTokens !== null ? Math.max(0, Math.round(maxTokens - usedTokens)) : null;
177 - const remainingPercentage = usedPercentage !== null ? Math.max(0, 100 - usedPercentage) : null;
178 -
179 - return {
180 - usedTokens,
181 - totalProcessedTokens: asFiniteNumber(payload.beforeTokenCount),
182 - maxTokens,
183 - remainingTokens,
184 - usedPercentage,
185 - remainingPercentage,
186 - inputTokens: null,
187 - cachedInputTokens: null,
188 - outputTokens: null,
189 - reasoningOutputTokens: null,
190 - lastUsedTokens: null,
191 - lastInputTokens: null,
192 - lastCachedInputTokens: null,
193 - lastOutputTokens: null,
194 - lastReasoningOutputTokens: null,
195 - toolUses: null,
196 - durationMs: null,
197 - compactsAutomatically: true,
198 - autoCompactThreshold: null,
199 - cost: null,
200 - updatedAt: DateTime.formatIso(payload.startedAt ?? payload.updatedAt),
201 - };
230 + const snapshot = compactionSnapshot(
231 + item,
232 + null,
233 + DateTime.formatIso(item.startedAt ?? item.updatedAt),
234 + );
235 + if (snapshot !== null) return snapshot;
236 }
237
238 return null;
packages/provider-opencode/src/server/adapter.test.ts
+88
@@ -1082,6 +1082,94 @@ describe("OpenCodeAdapterV2", () => {
1082 }).pipe(Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer))),
1083 );
1084
1085 + it.effect("drops context usage carried over from before a completed compaction", () =>
1086 + Effect.gen(function* () {
1087 + const nativeEvents = asyncEventStream();
1088 + let promptId = "";
1089 + const harness = yield* makeOpenCodeRuntimeHarness("compaction-usage", "root", {
1090 + event: { subscribe: async () => ({ stream: nativeEvents.stream }) },
1091 + session: {
1092 + create: async () => ({ data: { id: "root", time: { created: 1, updated: 1 } } }),
1093 + get: async () => ({ data: { id: "root", time: { created: 1, updated: 1 } } }),
1094 + promptAsync: async (input: { messageID: string }) => {
1095 + promptId = input.messageID;
1096 + return { data: true };
1097 + },
1098 + abort: async () => ({ data: true }),
1099 + children: async () => ({ data: [] }),
1100 + },
1101 + });
1102 + const inherited = yield* harness.runtime.resumeThread({
1103 + providerThread: {
1104 + ...harness.providerThread,
1105 + contextUsage: { usedTokens: 150_000, maxTokens: 160_000 },
1106 + },
1107 + });
1108 + const received = yield* harness.runtime.events.pipe(
1109 + Stream.takeUntil((event) => event.type === "turn.terminal"),
1110 + Stream.runCollect,
1111 + Effect.forkScoped,
1112 + );
1113 + yield* harness.startTurn("hello", inherited);
1114 + const status = (type: "busy" | "idle") => ({
1115 + type: "session.status",
1116 + properties: { sessionID: "root", status: { type } },
1117 + });
1118 + yield* Effect.promise(() =>
1119 + nativeEvents.push({
1120 + type: "message.updated",
1121 + properties: {
1122 + sessionID: "root",
1123 + info: { id: promptId, role: "user", time: { created: 1 } },
1124 + },
1125 + }),
1126 + );
1127 + yield* Effect.promise(() => nativeEvents.push(status("busy")));
1128 + yield* Effect.promise(() =>
1129 + nativeEvents.push({
1130 + type: "message.updated",
1131 + properties: {
1132 + sessionID: "root",
1133 + info: { id: "assistant", role: "assistant", time: { created: 1 }, parentID: promptId },
1134 + },
1135 + }),
1136 + );
1137 + yield* Effect.promise(() =>
1138 + nativeEvents.push({
1139 + type: "message.part.updated",
1140 + properties: {
1141 + part: {
1142 + type: "step-finish",
1143 + id: "pre-compaction-step",
1144 + sessionID: "root",
1145 + messageID: "assistant",
1146 + reason: "stop",
1147 + cost: 0,
1148 + tokens: { input: 150_000, output: 40, reasoning: 0, cache: { read: 0, write: 0 } },
1149 + },
1150 + },
1151 + }),
1152 + );
1153 + yield* Effect.promise(() =>
1154 + nativeEvents.push({ type: "session.compacted", properties: { sessionID: "root" } }),
1155 + );
1156 + yield* Effect.promise(() => nativeEvents.push(status("idle")));
1157 +
1158 + const events = Array.from(yield* Fiber.join(received));
1159 + const usage = events.flatMap((event) =>
1160 + event.type === "provider_thread.updated" ? [event.providerThread.contextUsage] : [],
1161 + );
1162 + assert.isNull(usage.at(-1));
1163 + const settled = events.findLast(
1164 + (event) =>
1165 + event.type === "provider_turn.updated" && event.providerTurn.completedAt !== null,
1166 + );
1167 + assert.isUndefined(
1168 + settled?.type === "provider_turn.updated" ? settled.providerTurn.tokenUsage : "missing",
1169 + );
1170 + }).pipe(Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer))),
1171 + );
1172 +
1173 it.effect("fails an explicit compaction that OpenCode reports as a context overflow", () =>
1174 Effect.gen(function* () {
1175 const nativeEvents = asyncEventStream();
packages/provider-opencode/src/server/adapter.ts
+5
@@ -2637,6 +2637,11 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2637 updatedAt: now,
2638 },
2639 });
2640 + // The pre-compaction step would otherwise settle the turn as its newest usage.
2641 + turn.usage.lastStep = null;
2642 + if (state.providerThread.contextUsage !== null) {
2643 + yield* updateProviderThread(state, { contextUsage: null });
2644 + }
2645 });
2646
2647 const handleMessageUpdated = Effect.fnUntraced(function* (
packages/provider-opencode/src/server/v2/adapter.ts
+10
@@ -1499,6 +1499,16 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
1499 ...(summary === undefined || summary.length === 0 ? {} : { summary }),
1500 },
1501 });
1502 + // The pre-compaction step would otherwise settle the turn as its newest usage.
1503 + if (status === "completed") turn.lastStep = undefined;
1504 + if (status === "completed" && state.providerThread.contextUsage !== null) {
1505 + state.providerThread = {
1506 + ...state.providerThread,
1507 + contextUsage: null,
1508 + updatedAt,
1509 + };
1510 + yield* emitProviderThread(state);
1511 + }
1512 if (status !== "running") turn.compaction = undefined;
1513 });
1514