Skip to content

Commit cfa76ea

Browse files
authored
fix(web): preserve context usage when forking threads (#22075)
1 parent b999554 commit cfa76ea

8 files changed

Lines changed: 192 additions & 20 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3745,6 +3745,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
37453745
sourceProjection,
37463746
sourceRun,
37473747
sourceProviderThread,
3748+
sourceTokenUsage: providerTurnForRun(sourceProjection, sourceRun)?.tokenUsage,
37483749
canonicalSourcePoint: contextSourcePointForRun(sourceProjection, sourceRun),
37493750
transferId,
37503751
targetThreadId: command.targetThreadId,

‎apps/server/src/orchestration-v2/ThreadForkService.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
OrchestrationV2ContextTransfer,
77
OrchestrationV2CreationSource,
88
OrchestrationV2ProviderThread,
9+
OrchestrationV2ProviderTurnTokenUsage,
910
OrchestrationV2Run,
1011
OrchestrationV2ThreadProjection,
1112
ThreadId,
@@ -54,6 +55,7 @@ export interface ThreadForkServiceV2Shape {
5455
readonly sourceProjection: Pick<OrchestrationV2ThreadProjection, "thread">;
5556
readonly sourceRun: OrchestrationV2Run;
5657
readonly sourceProviderThread: OrchestrationV2ProviderThread | undefined;
58+
readonly sourceTokenUsage?: OrchestrationV2ProviderTurnTokenUsage | undefined;
5759
readonly canonicalSourcePoint: OrchestrationV2ContextSourcePoint;
5860
readonly transferId: ContextTransferId;
5961
readonly targetThreadId: ThreadId;
@@ -119,6 +121,12 @@ export const layer: Layer.Layer<ThreadForkServiceV2> = Layer.succeed(
119121
sourcePoint: input.canonicalSourcePoint,
120122
basePoint: null,
121123
sourceProviderInstanceId: input.sourceRun.providerInstanceId,
124+
// Only native forks retain this context; portable handoffs rebuild it.
125+
sourceTokenUsage:
126+
(input.sourceRun.status === "completed" || input.sourceRun.status === "waiting") &&
127+
input.sourceProviderThread?.nativeThreadRef?.strength === "strong"
128+
? input.sourceTokenUsage
129+
: undefined,
122130
targetProviderInstanceId: null,
123131
targetRunId: null,
124132
status: "pending",

‎apps/server/src/orchestration-v2/testkit/ThreadFork.integration.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ describe("orchestration V2 thread fork", () => {
284284
assert.lengthOf(transfers, 1);
285285
assert.equal(transfers[0]?.status, "consumed");
286286
assert.equal(transfers[0]?.resolution?.strategy, "native_fork");
287+
assert.isUndefined(sourceProjection.providerTurns[0]?.tokenUsage);
288+
assert.isUndefined(transfers[0]?.sourceTokenUsage);
287289
assert.equal(transfers[0]?.targetRunId, targetProjection.runs[0]?.id);
288290

289291
const transferCreatedIndex = result.domainEvents.findIndex(
@@ -452,6 +454,9 @@ describe("orchestration V2 thread fork", () => {
452454
assert.lengthOf(transfers, 1);
453455
assert.equal(transfers[0]?.status, "consumed");
454456
assert.equal(transfers[0]?.resolution?.strategy, "native_fork");
457+
const sourceUsage = sourceProjection.providerTurns[0]?.tokenUsage;
458+
assert.isDefined(sourceUsage);
459+
assert.deepEqual(transfers[0]?.sourceTokenUsage, sourceUsage);
455460
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
456461
);
457462

@@ -780,6 +785,11 @@ describe("orchestration V2 thread fork", () => {
780785
"forking from the first source run must not preserve later source turns in native Claude context",
781786
);
782787
assert.equal(targetProjection.contextTransfers[0]?.resolution?.strategy, "native_fork");
788+
const sourceProjection = result.projections.get(materialized.sourceThreadId);
789+
const sourceUsage = sourceProjection?.providerTurns[0]?.tokenUsage;
790+
assert.isDefined(sourceUsage);
791+
assert.notDeepEqual(sourceUsage, sourceProjection?.providerTurns[1]?.tokenUsage);
792+
assert.deepEqual(targetProjection.contextTransfers[0]?.sourceTokenUsage, sourceUsage);
783793
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
784794
);
785795

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,8 +1151,13 @@ export default function ChatView(props: ChatViewProps) {
11511151
status: threadStatus,
11521152
});
11531153
const threadDetailLoading = threadSyncPhase === "loading";
1154+
const activeProviderThread = serverProjection?.providerThreads.find(
1155+
(thread) => thread.id === serverProjection.thread.activeProviderThreadId,
1156+
);
11541157
const activeThreadLiveTokenUsage = latestProviderTurnTokenUsage(
11551158
serverProjection?.providerTurns ?? [],
1159+
serverProjection,
1160+
activeProviderThread,
11561161
);
11571162
const serverVisibleTurnItems = useThreadVisibleTurnItems(routeThreadDetailRef);
11581163
const serverThreadHistory = useThreadHistory(routeThreadDetailRef);
@@ -2532,9 +2537,7 @@ export default function ChatView(props: ChatViewProps) {
25322537
const activeContextWindow = deriveLatestContextWindowSnapshot(
25332538
serverVisibleTurnItems ?? [],
25342539
activeThreadLiveTokenUsage,
2535-
serverProjection?.providerThreads.find(
2536-
(thread) => thread.id === serverProjection.thread.activeProviderThreadId,
2537-
),
2540+
activeProviderThread,
25382541
);
25392542
const pendingBackgroundTasks = (() => {
25402543
if (serverProjection === null || serverProjection === undefined) {

‎apps/web/src/components/chat/ComposerPrimaryActions.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,12 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
387387
<span>{compactTokens}</span>
388388
</TooltipTrigger>
389389
<TooltipPopup>
390-
{keepFullHistory
391-
? `Next send keeps all ${compactTokens} tokens. Click to compact first`
392-
: `Next send compacts ${compactTokens} tokens first. Click to keep full history`}
390+
<div>
391+
{keepFullHistory
392+
? `Next send keeps all ${compactTokens} tokens.`
393+
: `Next send compacts ${compactTokens} tokens first.`}
394+
</div>
395+
<div>{keepFullHistory ? "Click to compact first" : "Click to keep full history"}</div>
393396
</TooltipPopup>
394397
</Tooltip>
395398
{submit}

‎apps/web/src/lib/contextWindow.test.ts‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { describe, expect, it } from "vite-plus/test";
22
import * as DateTime from "effect/DateTime";
3+
import {
4+
ContextTransferId,
5+
ProviderDriverKind,
6+
ProviderInstanceId,
7+
RunId,
8+
ThreadId,
9+
} from "@t3tools/contracts";
10+
import { makeThreadProjectionFixture } from "../test-fixtures";
11+
import { shouldOfferResumeCompaction } from "../components/chat/ContextWindowMeter.logic";
312
import {
413
deriveLatestContextWindowSnapshot,
514
formatContextWindowTokens,
@@ -108,4 +117,125 @@ describe("latestProviderTurnTokenUsage", () => {
108117
]);
109118
expect(latest?.usedTokens).toBe(300_000);
110119
});
120+
121+
const sourceUsage = {
122+
usedTokens: 513_000,
123+
maxTokens: 1_000_000,
124+
promptCacheTtlMs: 5 * 60_000,
125+
updatedAt: "2026-10-19T12:00:00.000Z",
126+
};
127+
function forkProjection() {
128+
const projection = makeThreadProjectionFixture();
129+
const sourceThreadId = ThreadId.make("parent");
130+
const runId = RunId.make("fork-point");
131+
return {
132+
thread: {
133+
...projection.thread,
134+
forkedFrom: { type: "run" as const, threadId: sourceThreadId, runId },
135+
},
136+
contextTransfers: [
137+
{
138+
id: ContextTransferId.make("fork-transfer"),
139+
type: "fork" as const,
140+
sourceThreadId,
141+
targetThreadId: projection.thread.id,
142+
sourcePoint: { threadId: sourceThreadId, runId },
143+
basePoint: null,
144+
sourceProviderInstanceId: projection.thread.modelSelection.instanceId,
145+
sourceTokenUsage: sourceUsage,
146+
targetProviderInstanceId: null,
147+
targetRunId: null,
148+
status: "pending" as const,
149+
resolution: null,
150+
createdBy: "user" as const,
151+
error: null,
152+
createdAt: projection.updatedAt,
153+
updatedAt: projection.updatedAt,
154+
consumedAt: null,
155+
},
156+
],
157+
};
158+
}
159+
160+
it("keeps fork-point context and its cache expiry before the fork reports usage", () => {
161+
const usage = latestProviderTurnTokenUsage([], forkProjection());
162+
expect(usage).toEqual(sourceUsage);
163+
const snapshot = deriveLatestContextWindowSnapshot([], usage);
164+
expect(snapshot?.usedTokens).toBe(513_000);
165+
expect(
166+
shouldOfferResumeCompaction({
167+
provider: "claudeAgent",
168+
usedTokens: snapshot?.usedTokens,
169+
updatedAt: snapshot?.updatedAt,
170+
promptCacheTtlMs: usage?.promptCacheTtlMs,
171+
now: "2026-10-19T12:06:00.000Z",
172+
}),
173+
).toBe(true);
174+
});
175+
176+
it("replaces inherited usage with the fork's own report", () => {
177+
const tokenUsage = { usedTokens: 42_000, updatedAt: "2026-10-19T12:10:00.000Z" };
178+
expect(latestProviderTurnTokenUsage([{ tokenUsage }], forkProjection())).toEqual(tokenUsage);
179+
});
180+
181+
it.each([0, 42_000])(
182+
"prefers target provider-thread usage of %d tokens over inherited usage",
183+
(usedTokens) => {
184+
const providerThread = {
185+
contextUsage: {
186+
usedTokens,
187+
maxTokens: 200_000,
188+
cost: { amount: 0.42, currency: "USD" },
189+
},
190+
updatedAt: DateTime.makeUnsafe("2026-10-19T12:10:00.000Z"),
191+
};
192+
const usage = latestProviderTurnTokenUsage([], forkProjection(), providerThread);
193+
const snapshot = deriveLatestContextWindowSnapshot([], usage, providerThread);
194+
195+
expect(snapshot).toMatchObject({
196+
usedTokens,
197+
maxTokens: 200_000,
198+
cost: { amount: 0.42, currency: "USD" },
199+
updatedAt: "2026-10-19T12:10:00.000Z",
200+
});
201+
},
202+
);
203+
204+
it("keeps the fallback after a native fork is consumed", () => {
205+
const projection = forkProjection();
206+
const transfer = projection.contextTransfers[0]!;
207+
expect(
208+
latestProviderTurnTokenUsage([], {
209+
...projection,
210+
contextTransfers: [
211+
{
212+
...transfer,
213+
status: "consumed",
214+
resolution: {
215+
strategy: "native_fork",
216+
providerThreadRef: {
217+
driver: ProviderDriverKind.make("claudeAgent"),
218+
nativeId: "fork",
219+
strength: "strong",
220+
},
221+
},
222+
},
223+
],
224+
}),
225+
).toEqual(sourceUsage);
226+
});
227+
228+
it("does not reuse usage for portable handoffs, different providers, or missing history", () => {
229+
const projection = forkProjection();
230+
const transfer = projection.contextTransfers[0]!;
231+
for (const contextTransfers of [
232+
[],
233+
[{ ...transfer, sourceTokenUsage: undefined }],
234+
[{ ...transfer, status: "resolved_portable" as const }],
235+
[{ ...transfer, sourceProviderInstanceId: ProviderInstanceId.make("other") }],
236+
[{ ...transfer, sourcePoint: { ...transfer.sourcePoint, runId: RunId.make("later") } }],
237+
]) {
238+
expect(latestProviderTurnTokenUsage([], { ...projection, contextTransfers })).toBeNull();
239+
}
240+
});
111241
});

‎apps/web/src/lib/contextWindow.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
OrchestrationV2ProviderTurn,
33
OrchestrationV2ProviderTurnTokenUsage,
44
OrchestrationV2ProviderThread,
5+
OrchestrationV2ThreadProjection,
56
OrchestrationV2TurnItem,
67
ThreadTokenUsageSnapshot,
78
} from "@t3tools/contracts";
@@ -28,6 +29,8 @@ export type ContextWindowSnapshot = NullableContextWindowUsage & {
2829
by provider thread, not time, so pick by timestamp. */
2930
export function latestProviderTurnTokenUsage(
3031
turns: ReadonlyArray<Pick<OrchestrationV2ProviderTurn, "tokenUsage">>,
32+
projection?: Pick<OrchestrationV2ThreadProjection, "thread" | "contextTransfers"> | null,
33+
providerThread?: Pick<OrchestrationV2ProviderThread, "contextUsage"> | null,
3134
): OrchestrationV2ProviderTurnTokenUsage | null {
3235
let latest: OrchestrationV2ProviderTurnTokenUsage | null = null;
3336
for (const { tokenUsage } of turns) {
@@ -37,7 +40,20 @@ export function latestProviderTurnTokenUsage(
3740
latest = tokenUsage;
3841
}
3942
}
40-
return latest;
43+
if (latest !== null || projection == null || providerThread?.contextUsage != null) return latest;
44+
const forkedFrom = projection.thread.forkedFrom;
45+
if (forkedFrom?.type !== "run") return null;
46+
return (
47+
projection.contextTransfers.find(
48+
(transfer) =>
49+
transfer.type === "fork" &&
50+
transfer.targetThreadId === projection.thread.id &&
51+
transfer.sourceThreadId === forkedFrom.threadId &&
52+
transfer.sourcePoint.runId === forkedFrom.runId &&
53+
transfer.sourceProviderInstanceId === projection.thread.modelSelection.instanceId &&
54+
(transfer.status === "pending" || transfer.resolution?.strategy === "native_fork"),
55+
)?.sourceTokenUsage ?? null
56+
);
4157
}
4258

4359
export function deriveLatestContextWindowSnapshot(

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ export const OrchestrationV2ContextTransferResolution = Schema.Union([
163163
export type OrchestrationV2ContextTransferResolution =
164164
typeof OrchestrationV2ContextTransferResolution.Type;
165165

166+
export const OrchestrationV2ProviderTurnTokenUsage = Schema.Struct({
167+
usedTokens: NonNegativeInt,
168+
maxTokens: Schema.optional(Schema.NullOr(NonNegativeInt)),
169+
inputTokens: Schema.optional(NonNegativeInt),
170+
cachedInputTokens: Schema.optional(NonNegativeInt),
171+
outputTokens: Schema.optional(NonNegativeInt),
172+
reasoningOutputTokens: Schema.optional(NonNegativeInt),
173+
promptCacheTtlMs: Schema.optional(PositiveInt),
174+
updatedAt: Schema.String,
175+
});
176+
export type OrchestrationV2ProviderTurnTokenUsage =
177+
typeof OrchestrationV2ProviderTurnTokenUsage.Type;
178+
166179
export const OrchestrationV2ContextTransfer = Schema.Struct({
167180
id: ContextTransferId,
168181
type: OrchestrationV2ContextTransferType,
@@ -171,6 +184,7 @@ export const OrchestrationV2ContextTransfer = Schema.Struct({
171184
sourcePoint: OrchestrationV2ContextSourcePoint,
172185
basePoint: Schema.NullOr(OrchestrationV2ContextSourcePoint),
173186
sourceProviderInstanceId: Schema.NullOr(ProviderInstanceId),
187+
sourceTokenUsage: Schema.optional(OrchestrationV2ProviderTurnTokenUsage),
174188
targetProviderInstanceId: Schema.NullOr(ProviderInstanceId),
175189
targetRunId: Schema.NullOr(RunId),
176190
status: Schema.Literals([
@@ -950,19 +964,6 @@ export const OrchestrationV2ContextHandoff = Schema.Struct({
950964
});
951965
export type OrchestrationV2ContextHandoff = typeof OrchestrationV2ContextHandoff.Type;
952966

953-
export const OrchestrationV2ProviderTurnTokenUsage = Schema.Struct({
954-
usedTokens: NonNegativeInt,
955-
maxTokens: Schema.optional(Schema.NullOr(NonNegativeInt)),
956-
inputTokens: Schema.optional(NonNegativeInt),
957-
cachedInputTokens: Schema.optional(NonNegativeInt),
958-
outputTokens: Schema.optional(NonNegativeInt),
959-
reasoningOutputTokens: Schema.optional(NonNegativeInt),
960-
promptCacheTtlMs: Schema.optional(PositiveInt),
961-
updatedAt: Schema.String,
962-
});
963-
export type OrchestrationV2ProviderTurnTokenUsage =
964-
typeof OrchestrationV2ProviderTurnTokenUsage.Type;
965-
966967
export const OrchestrationV2ProviderTurn = Schema.Struct({
967968
id: ProviderTurnId,
968969
providerThreadId: ProviderThreadId,

0 commit comments

Comments
 (0)