@devries-cameron20 / t3code Mirror · read-only / commits / 2b596b8690
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, 14:45 UTC

fix(web): context meter no longer keeps the retired provider's usage (#28581)

Co-authored-by: nikolay.akinshin <nikolay.akinshin@south.rt.ru> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Nikolay Akinshin committed Nov 14, 2026 at 16:21 UTC 2b596b86907ad806f80b16b15b68af018e6a37f1
6 files changed +204 -42
apps/server/src/orchestration-v2/ProjectionStore.ts
+1 -9
@@ -51,6 +51,7 @@ import type {
51 import {
52 IDLE_COMPACTION_TEXT,
53 idleCompactionOf,
54 + isQueuedProviderThreadPlaceholder,
55 OrchestrationV2AppThreadJson as OrchestrationV2AppThreadJsonSchema,
56 OrchestrationV2CheckpointJson as OrchestrationV2CheckpointJsonSchema,
57 OrchestrationV2CheckpointScopeJson as OrchestrationV2CheckpointScopeJsonSchema,
@@ -880,15 +881,6 @@ export function emptyProjection(
881 };
882 }
883
883 -function isQueuedProviderThreadPlaceholder(providerThread: OrchestrationV2ProviderThread): boolean {
884 - return (
885 - providerThread.status === "not_loaded" &&
886 - providerThread.firstRunOrdinal === null &&
887 - providerThread.nativeThreadRef === null &&
888 - providerThread.providerSessionId === null
889 - );
890 -}
891 -
884 export function applyToProjection(
885 projection: OrchestrationV2ThreadProjection,
886 event: OrchestrationV2DomainEvent,
apps/web/src/lib/contextWindow.test.ts
+82 -26
@@ -1,4 +1,5 @@
1 import { describe, expect, it } from "vite-plus/test";
2 +import type { ThreadStructureTurnItem } from "@t3tools/client-runtime/state/shell";
3 import { OrchestrationV2ProviderTurnTokenUsage } from "@t3tools/contracts";
4 import * as Schema from "effect/Schema";
5 import { formatContextWindowCompactionMessage } from "../components/chat/compaction.logic";
@@ -7,6 +8,7 @@ import {
8 ContextTransferId,
9 ProviderDriverKind,
10 ProviderInstanceId,
11 + ProviderThreadId,
12 RunId,
13 ThreadId,
14 } from "@t3tools/contracts";
@@ -19,35 +21,55 @@ import {
21
22 const decodeTokenUsage = Schema.decodeSync(OrchestrationV2ProviderTurnTokenUsage);
23
24 +const compactionEntry = (providerThreadId: string | null) =>
25 + ({
26 + item: {
27 + id: "compaction-1" as never,
28 + threadId: "thread-1" as never,
29 + runId: null,
30 + nodeId: null,
31 + providerThreadId: providerThreadId as never,
32 + providerTurnId: null,
33 + nativeItemRef: null,
34 + parentItemId: null,
35 + ordinal: 1,
36 + status: "completed",
37 + title: null,
38 + startedAt: null,
39 + completedAt: null,
40 + updatedAt: DateTime.makeUnsafe("2026-06-20T00:00:00.000Z"),
41 + type: "compaction",
42 + driver: null,
43 + beforeTokenCount: 10_000,
44 + afterTokenCount: 2_000,
45 + },
46 + }) satisfies { item: ThreadStructureTurnItem };
47 +
48 describe("V2 context window presentation", () => {
49 it("uses retained compaction token data when available", () => {
24 - const snapshot = deriveLatestContextWindowSnapshot([
25 - {
26 - item: {
27 - id: "compaction-1" as never,
28 - threadId: "thread-1" as never,
29 - runId: null,
30 - nodeId: null,
31 - providerThreadId: null,
32 - providerTurnId: null,
33 - nativeItemRef: null,
34 - parentItemId: null,
35 - ordinal: 1,
36 - status: "completed",
37 - title: null,
38 - startedAt: null,
39 - completedAt: null,
40 - updatedAt: DateTime.makeUnsafe("2026-06-20T00:00:00.000Z"),
41 - type: "compaction",
42 - driver: null,
43 - beforeTokenCount: 10_000,
44 - afterTokenCount: 2_000,
45 - },
46 - },
47 - ]);
50 + const snapshot = deriveLatestContextWindowSnapshot([compactionEntry(null)]);
51 expect(snapshot?.usedTokens).toBe(2_000);
52 expect(snapshot?.totalProcessedTokens).toBe(10_000);
53 });
54 +
55 + it("ignores a retired provider thread's compaction after a provider switch", () => {
56 + const activeProviderThread = {
57 + id: ProviderThreadId.make("acp-thread"),
58 + contextUsage: null,
59 + updatedAt: DateTime.makeUnsafe("2026-10-19T12:10:00.000Z"),
60 + };
61 + const retired = compactionEntry(ProviderThreadId.make("pi-thread"));
62 + const current = compactionEntry(ProviderThreadId.make("acp-thread"));
63 + expect(deriveLatestContextWindowSnapshot([retired], null, activeProviderThread)).toBeNull();
64 + expect(
65 + deriveLatestContextWindowSnapshot([current], null, activeProviderThread)?.usedTokens,
66 + ).toBe(2_000);
67 + // Items without a provider thread predate the split and stay visible.
68 + expect(
69 + deriveLatestContextWindowSnapshot([compactionEntry(null)], null, activeProviderThread)
70 + ?.usedTokens,
71 + ).toBe(2_000);
72 + });
73 });
74
75 describe("live provider-turn usage (#8144)", () => {
@@ -75,12 +97,14 @@ describe("live provider-turn usage (#8144)", () => {
97
98 describe("latestProviderTurnTokenUsage", () => {
99 it("picks the newest report, not the last turn in snapshot order", () => {
100 + const providerThreadId = ProviderThreadId.make("provider-thread");
101 const usage = (usedTokens: number, updatedAt: string) => ({
102 + providerThreadId,
103 tokenUsage: { usedTokens, updatedAt },
104 });
105 const latest = latestProviderTurnTokenUsage([
106 usage(300_000, "2026-06-20T12:00:00.000Z"),
83 - {},
107 + { providerThreadId },
108 usage(90_000, "2026-06-20T09:00:00.000Z"),
109 ]);
110 expect(latest?.usedTokens).toBe(300_000);
@@ -143,13 +167,45 @@ describe("latestProviderTurnTokenUsage", () => {
167
168 it("replaces inherited usage with the fork's own report", () => {
169 const tokenUsage = { usedTokens: 42_000, updatedAt: "2026-10-19T12:10:00.000Z" };
146 - expect(latestProviderTurnTokenUsage([{ tokenUsage }], forkProjection())).toEqual(tokenUsage);
170 + expect(
171 + latestProviderTurnTokenUsage(
172 + [{ providerThreadId: ProviderThreadId.make("provider-thread"), tokenUsage }],
173 + forkProjection(),
174 + ),
175 + ).toEqual(tokenUsage);
176 + });
177 +
178 + it("prefers the active provider thread's older report over a newer retired one", () => {
179 + const turns = [
180 + {
181 + providerThreadId: ProviderThreadId.make("pi-thread"),
182 + tokenUsage: decodeTokenUsage({
183 + usedTokens: 180_000,
184 + maxTokens: 200_000,
185 + updatedAt: "2026-10-19T12:10:00.000Z",
186 + }),
187 + },
188 + {
189 + providerThreadId: ProviderThreadId.make("acp-thread"),
190 + tokenUsage: decodeTokenUsage({
191 + usedTokens: 12_000,
192 + maxTokens: 200_000,
193 + updatedAt: "2026-10-19T11:00:00.000Z",
194 + }),
195 + },
196 + ];
197 + const liveUsage = latestProviderTurnTokenUsage(turns, null, {
198 + id: ProviderThreadId.make("acp-thread"),
199 + contextUsage: null,
200 + });
201 + expect(liveUsage?.usedTokens).toBe(12_000);
202 });
203
204 it.each([0, 42_000])(
205 "prefers target provider-thread usage of %d tokens over inherited usage",
206 (usedTokens) => {
207 const providerThread = {
208 + id: ProviderThreadId.make("provider-thread"),
209 contextUsage: {
210 usedTokens,
211 maxTokens: 200_000,
packages/client-runtime/src/state/contextWindow.ts
+17 -5
@@ -42,15 +42,19 @@ export type ContextWindowSnapshot = NullableContextWindowUsage & {
42 readonly updatedAt: string;
43 };
44
45 -/** Newest report by timestamp; snapshots order turns by provider thread. */
45 +/**
46 + * Newest report by timestamp among the active provider thread's turns, since a provider switch leaves
47 + * the previous provider's report behind. Without an active provider thread, every turn is a candidate.
48 + */
49 export function latestProviderTurnTokenUsage(
47 - turns: ReadonlyArray<Pick<OrchestrationV2ProviderTurn, "tokenUsage">>,
50 + turns: ReadonlyArray<Pick<OrchestrationV2ProviderTurn, "providerThreadId" | "tokenUsage">>,
51 projection?: Pick<ThreadStructure, "thread" | "contextTransfers"> | null,
49 - providerThread?: Pick<OrchestrationV2ProviderThread, "contextUsage"> | null,
52 + providerThread?: Pick<OrchestrationV2ProviderThread, "id" | "contextUsage"> | null,
53 ): OrchestrationV2ProviderTurnTokenUsage | null {
54 let latest: OrchestrationV2ProviderTurnTokenUsage | null = null;
52 - for (const { tokenUsage } of turns) {
55 + for (const { providerThreadId, tokenUsage } of turns) {
56 if (tokenUsage === undefined) continue;
57 + if (providerThread != null && providerThreadId !== providerThread.id) continue;
58 // Ties go to the later entry, since live upserts append.
59 if (latest === null || Date.parse(tokenUsage.updatedAt) >= Date.parse(latest.updatedAt)) {
60 latest = tokenUsage;
@@ -77,7 +81,7 @@ export function deriveLatestContextWindowSnapshot(
81 readonly item: ThreadStructureTurnItem;
82 }>,
83 liveUsage?: OrchestrationV2ProviderTurnTokenUsage | null,
80 - providerThread?: Pick<OrchestrationV2ProviderThread, "contextUsage" | "updatedAt"> | null,
84 + providerThread?: Pick<OrchestrationV2ProviderThread, "id" | "contextUsage" | "updatedAt"> | null,
85 ): ContextWindowSnapshot | null {
86 if (liveUsage != null) {
87 const usedTokens = Math.max(0, liveUsage.usedTokens);
@@ -152,6 +156,14 @@ export function deriveLatestContextWindowSnapshot(
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) {
169 continue;
packages/client-runtime/src/state/orchestrationV2Projection.test.ts
+76
@@ -3,6 +3,7 @@ import {
3 type ThreadPullRequestLink,
4 type OrchestrationV2DomainEvent,
5 type OrchestrationV2Run,
6 + type OrchestrationV2ProviderThread,
7 type OrchestrationV2ThreadProjection,
8 type OrchestrationV2TurnItem,
9 MessageId,
@@ -817,3 +818,78 @@ it("restores deduped bodies from the records it already holds, also in a partial
818 );
819 expect(progressed?.subagents).toEqual([{ ...subagent, progress: "step 2" }]);
820 });
821 +
822 +describe("provider-thread.updated", () => {
823 + const providerThread = (
824 + id: string,
825 + overrides: Partial<OrchestrationV2ProviderThread> = {},
826 + ): OrchestrationV2ProviderThread =>
827 + ({
828 + id: ProviderThreadId.make(id),
829 + driver: "codex",
830 + providerInstanceId: ProviderInstanceId.make("codex"),
831 + providerSessionId: null,
832 + appThreadId: threadId,
833 + ownerNodeId: null,
834 + nativeThreadRef: null,
835 + nativeConversationHeadRef: null,
836 + status: "active",
837 + firstRunOrdinal: 1,
838 + lastRunOrdinal: 1,
839 + handoffIds: [],
840 + forkedFrom: null,
841 + createdAt: now,
842 + updatedAt: now,
843 + ...overrides,
844 + }) as OrchestrationV2ProviderThread;
845 + const apply = (payload: unknown) =>
846 + applyOrchestrationV2ProjectionEvent(emptyProjection, {
847 + id: "event-provider-thread",
848 + type: "provider-thread.updated",
849 + threadId,
850 + occurredAt: now,
851 + payload,
852 + } as OrchestrationV2DomainEvent);
853 +
854 + it("moves the thread's active provider thread to the new one", () => {
855 + // The shell already knows the old thread, as it would after a provider switch.
856 + const projection = applyOrchestrationV2ProjectionEvent(emptyProjection, {
857 + id: "event-previous-thread",
858 + type: "provider-thread.updated",
859 + threadId,
860 + occurredAt: now,
861 + payload: providerThread("provider-thread-previous"),
862 + } as OrchestrationV2DomainEvent);
863 + expect(projection?.thread.activeProviderThreadId).toBe(
864 + ProviderThreadId.make("provider-thread-previous"),
865 + );
866 +
867 + const next = applyOrchestrationV2ProjectionEvent(projection, {
868 + id: "event-next-thread",
869 + type: "provider-thread.updated",
870 + threadId,
871 + occurredAt: now,
872 + payload: providerThread("provider-thread-next"),
873 + } as OrchestrationV2DomainEvent);
874 + expect(next?.thread.activeProviderThreadId).toBe(ProviderThreadId.make("provider-thread-next"));
875 + });
876 +
877 + it("keeps the active provider thread for queued placeholders and other threads", () => {
878 + const queued = apply(
879 + providerThread("provider-thread-queued", {
880 + status: "not_loaded",
881 + firstRunOrdinal: null,
882 + providerSessionId: null,
883 + }),
884 + );
885 + expect(queued?.thread.activeProviderThreadId).toBeNull();
886 +
887 + const foreign = apply(
888 + providerThread("provider-thread-foreign", { appThreadId: ThreadId.make("other-thread") }),
889 + );
890 + expect(foreign?.thread.activeProviderThreadId).toBeNull();
891 + expect(foreign?.providerThreads.map((thread) => thread.id)).toEqual([
892 + ProviderThreadId.make("provider-thread-foreign"),
893 + ]);
894 + });
895 +});
packages/client-runtime/src/state/orchestrationV2Projection.ts
+13 -2
@@ -1,5 +1,8 @@
1 import { applyThreadPullRequestLinkUpdate } from "../threadPullRequests.ts";
2 -import { applyOrchestrationV2ThreadVisit } from "@t3tools/contracts";
2 +import {
3 + applyOrchestrationV2ThreadVisit,
4 + isQueuedProviderThreadPlaceholder,
5 +} from "@t3tools/contracts";
6 import type {
7 OrchestrationV2DomainEvent,
8 OrchestrationV2Run,
@@ -382,7 +385,15 @@ export function applyOrchestrationV2ProjectionEvent(
385 ),
386 };
387 case "provider-thread.updated":
385 - return { ...base, providerThreads: upsertEntity(base.providerThreads, event.payload) };
388 + return {
389 + ...base,
390 + thread:
391 + event.payload.appThreadId === base.thread.id &&
392 + !isQueuedProviderThreadPlaceholder(event.payload)
393 + ? { ...base.thread, activeProviderThreadId: event.payload.id }
394 + : base.thread,
395 + providerThreads: upsertEntity(base.providerThreads, event.payload),
396 + };
397 case "provider-turn.updated":
398 return {
399 ...base,
packages/contracts/src/orchestrationV2.ts
+15
@@ -1257,6 +1257,21 @@ export function latestProviderTurnForAttempt<
1257 return latest;
1258 }
1259
1260 +/** A queued provider thread reserves a seat for the next run; it does not own the thread yet. */
1261 +export function isQueuedProviderThreadPlaceholder(
1262 + providerThread: Pick<
1263 + OrchestrationV2ProviderThread,
1264 + "status" | "firstRunOrdinal" | "nativeThreadRef" | "providerSessionId"
1265 + >,
1266 +): boolean {
1267 + return (
1268 + providerThread.status === "not_loaded" &&
1269 + providerThread.firstRunOrdinal === null &&
1270 + providerThread.nativeThreadRef === null &&
1271 + providerThread.providerSessionId === null
1272 + );
1273 +}
1274 +
1275 export const OrchestrationV2RuntimeRequest = Schema.Struct({
1276 id: RuntimeRequestId,
1277 nodeId: NodeId,