From d50cd06fed513d8bf3f4315559e72bdac859451c Mon Sep 17 00:00:00 2001 From: TinBane <363069+TinBane@users.noreply.github.com> Date: Wed, 4 Nov 2026 10:47:31 +1100 Subject: [PATCH 1/5] fix(provider-kiro): report honest context usage Kiro reports a session context percentage and compaction threshold, but T3 ignored them and did not know the selected model's window, leaving the handoff planner blind. Map explicit-model context windows and project session/load and live session_info_update percentages into provider-thread telemetry. Every load, whichever call made it, is applied once by the next resume or turn, and live readings only use the model of their own active session. Unknown, default and custom models stay unknown, a load naming no model keeps the saved reading, and a load percentage is never rescaled across a model mismatch. Co-Authored-By: GPT Daybreak Blue Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AcpAdapterV2.test.ts | 542 +++++++++++++++++- .../Adapters/KiroAdapterV2.test.ts | 104 ++++ .../provider_thread_resume/kiro_output.ts | 14 + packages/provider-acp/src/server/adapter.ts | 152 ++++- packages/provider-kiro/src/server/adapter.ts | 121 ++++ packages/provider-kiro/src/testing.ts | 9 +- 6 files changed, 923 insertions(+), 19 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 7234c561b9163..fb3da60aee8f3 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -410,6 +410,133 @@ describe("acpToolCallDiffPatch", () => { }); }); +/** Opens a mock ACP session with context usage hooks, for tests that switch sessions and models. */ +const openContextUsageRuntime = Effect.fnUntraced(function* (input: { + readonly name: string; + readonly initialNativeThreadId?: string; + readonly contextUsageFromSession: NonNullable; + readonly contextUsageFromSessionUpdate?: AcpAdapterV2Flavor["contextUsageFromSessionUpdate"]; + /** Sent while a session activates, as an agent replays history before answering the load. */ + readonly replayOnActivation?: EffectAcpSchema.SessionNotification["update"]; +}) { + const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const path = yield* Path.Path; + const selfInvocation = yield* resolveSelfInvocation(); + const mockAgentPath = yield* path.fromFileUrl( + new URL("../../../scripts/acp-mock-agent.ts", import.meta.url), + ); + const instanceId = ProviderInstanceId.make(`acp-test-${input.name}`); + type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"]; + let handler: Parameters[0] | undefined; + const replay = (sessionId: string) => + Effect.suspend(() => + input.replayOnActivation === undefined || handler === undefined + ? Effect.void + : handler({ sessionId, update: input.replayOnActivation }), + ); + const adapter = yield* makeAcpAdapterV2({ + instanceId, + flavor: { + driver: ACP_TEST_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + contextUsageFromSession: input.contextUsageFromSession, + ...(input.contextUsageFromSessionUpdate === undefined + ? {} + : { contextUsageFromSessionUpdate: input.contextUsageFromSessionUpdate }), + applyModelSelection: ({ modelSelection }) => Effect.succeed(modelSelection.model), + makeRuntime: makeMockRuntime({ + childProcessSpawner, + mockAgentPath, + wrapRuntime: (runtime) => ({ + ...runtime, + handleSessionUpdate: (next) => + Effect.sync(() => { + handler = next; + }).pipe(Effect.andThen(runtime.handleSessionUpdate(next))), + loadSession: (sessionId, options) => + replay(sessionId).pipe(Effect.andThen(runtime.loadSession(sessionId, options))), + resumeSession: (sessionId, options) => + replay(sessionId).pipe(Effect.andThen(runtime.resumeSession(sessionId, options))), + }), + }), + }, + selfInvocation, + }); + const threadId = ThreadId.make(`thread-acp-${input.name}`); + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: process.cwd(), + }); + const selection = (model: string): ModelSelection => ({ instanceId, model }); + const runtime = yield* adapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make(`provider-session-acp-${input.name}`), + modelSelection: selection("small"), + runtimePolicy, + ...(input.initialNativeThreadId === undefined + ? {} + : { initialNativeThreadId: input.initialNativeThreadId }), + }); + const providerThread = yield* runtime.ensureThread({ + threadId, + modelSelection: selection("small"), + runtimePolicy, + }); + const update = (notification: EffectAcpSchema.SessionNotification) => + Effect.suspend(() => handler?.(notification) ?? Effect.void); + return { runtime, threadId, instanceId, runtimePolicy, selection, providerThread, update }; +}); + +const withNativeSession = ( + providerThread: OrchestrationV2ProviderThread, + nativeId: string, +): OrchestrationV2ProviderThread => ({ + ...providerThread, + nativeThreadRef: { driver: ACP_TEST_DRIVER, nativeId, strength: "strong" }, +}); + +const collectUntilTerminal = (runtime: { + readonly events: ProviderAdapter.ProviderAdapterV2SessionRuntime["events"]; +}) => + runtime.events.pipe( + Stream.takeUntil((event) => event.type === "turn.terminal"), + Stream.runCollect, + Effect.map((events) => Array.from(events)), + ); + +const contextUsageAfterRename = ( + runtime: { readonly events: ProviderAdapter.ProviderAdapterV2SessionRuntime["events"] }, + update: (notification: EffectAcpSchema.SessionNotification) => Effect.Effect, + sessionId: string, +) => + update({ sessionId, update: { sessionUpdate: "session_info_update", title: "Renamed" } }).pipe( + Effect.andThen( + runtime.events.pipe( + Stream.filter( + (event) => + event.type === "provider_thread.updated" && + event.providerThread.nativeMetadata?.title === "Renamed", + ), + Stream.take(1), + Stream.runCollect, + ), + ), + Effect.map((events) => { + const renamed = Array.from(events)[0]; + return renamed?.type === "provider_thread.updated" + ? renamed.providerThread.contextUsage + : "none"; + }), + ); + +const activeContextUsage = (events: ReadonlyArray) => { + const active = events.find( + (event) => event.type === "provider_thread.updated" && event.providerThread.status === "active", + ); + return active?.type === "provider_thread.updated" ? active.providerThread.contextUsage : "none"; +}; + function makeMockRuntime(input: { readonly childProcessSpawner: ChildProcessSpawner.ChildProcessSpawner["Service"]; readonly mockAgentPath: string; @@ -3587,15 +3714,29 @@ describe("AcpAdapterV2", () => { ); const protocolEvents = yield* Queue.unbounded(); const instanceId = ProviderInstanceId.make("acp-test-eager-resume"); + let contextUsageLoads = 0; + type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"]; + let sessionUpdateHandler: Parameters[0] | undefined; const adapter = yield* makeAcpAdapterV2({ instanceId, flavor: { driver: ACP_TEST_DRIVER, capabilities: AcpProviderCapabilitiesV2, + contextUsageFromSession: () => { + contextUsageLoads += 1; + return { usedTokens: 100, maxTokens: 1_000 }; + }, makeRuntime: makeMockRuntime({ childProcessSpawner, mockAgentPath, protocolEvents, + wrapRuntime: (runtime) => ({ + ...runtime, + handleSessionUpdate: (handler) => + Effect.sync(() => { + sessionUpdateHandler = handler; + }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))), + }), }), }, selfInvocation, @@ -3626,25 +3767,64 @@ describe("AcpAdapterV2", () => { assert.include(startupMethods, "session/resume"); assert.notInclude(startupMethods, "session/new"); - yield* runtime.resumeThread({ providerThread, modelSelection, runtimePolicy }); + const resumed = yield* runtime.resumeThread({ + providerThread, + modelSelection, + runtimePolicy, + }); + assert.equal(contextUsageLoads, 1, "startup's load telemetry applies on the first resume"); + assert.deepEqual(resumed.contextUsage, { usedTokens: 100, maxTokens: 1_000 }); assert.notInclude(yield* pollProtocolMethods(protocolEvents), "session/resume"); + assert.isDefined(sessionUpdateHandler); + yield* sessionUpdateHandler!({ + sessionId: "persisted-session", + update: { sessionUpdate: "usage_update", used: 900, size: 1_000 }, + }); + yield* runtime.resumeThread({ + providerThread: { + ...providerThread, + nativeThreadRef: { + driver: ACP_TEST_DRIVER, + nativeId: "other-session", + strength: "strong", + }, + }, + modelSelection, + runtimePolicy, + }); + const reloaded = yield* runtime.resumeThread({ + providerThread, + modelSelection, + runtimePolicy, + }); + assert.equal(contextUsageLoads, 3); + assert.deepEqual(reloaded.contextUsage, { usedTokens: 100, maxTokens: 1_000 }); const now = yield* DateTime.now; yield* runtime.startTurn( makeTurnInput({ threadId, - providerThread, + providerThread: reloaded, instanceId, runtimePolicy, now, ordinal: 1, }), ); - const terminal = Array.from( + const events = Array.from( yield* runtime.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, ), - ).find((event) => event.type === "turn.terminal"); + ); + const active = events.find( + (event) => + event.type === "provider_thread.updated" && event.providerThread.status === "active", + ); + assert.deepEqual( + active?.type === "provider_thread.updated" ? active.providerThread.contextUsage : null, + { usedTokens: 100, maxTokens: 1_000 }, + ); + const terminal = events.find((event) => event.type === "turn.terminal"); assert.equal( terminal?.providerTurnId, idAllocator.derive.providerTurn({ @@ -3655,6 +3835,360 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest), Effect.scoped), ); + it.live("applies each load's context usage once, whichever call activated the session", () => + Effect.gen(function* () { + const loads: Array = []; + const { runtime, threadId, instanceId, runtimePolicy, selection, providerThread } = + yield* openContextUsageRuntime({ + name: "load-once", + initialNativeThreadId: "persisted-session", + contextUsageFromSession: ({ session }) => { + loads.push(session.sessionId); + return { + usedTokens: session.sessionId === "persisted-session" ? 100 : 200, + maxTokens: 1_000, + }; + }, + }); + const otherThread = withNativeSession(providerThread, "other-session"); + + const resumed = yield* runtime.resumeThread({ + providerThread, + modelSelection: selection("small"), + runtimePolicy, + }); + assert.deepEqual(resumed.contextUsage, { usedTokens: 100, maxTokens: 1_000 }); + const resumedAgain = yield* runtime.resumeThread({ + providerThread, + modelSelection: selection("small"), + runtimePolicy, + }); + assert.isNull(resumedAgain.contextUsage); + assert.deepEqual(loads, ["persisted-session"]); + + yield* runtime.resumeThread({ + providerThread: otherThread, + modelSelection: selection("small"), + runtimePolicy, + }); + const now = yield* DateTime.now; + yield* runtime.startTurn( + makeTurnInput({ + threadId, + providerThread: { + ...providerThread, + contextUsage: { usedTokens: 999, maxTokens: 1_000 }, + }, + instanceId, + runtimePolicy, + now, + ordinal: 1, + modelSelection: selection("small"), + }), + ); + const events = yield* collectUntilTerminal(runtime); + assert.deepEqual(activeContextUsage(events), { usedTokens: 100, maxTokens: 1_000 }); + assert.deepEqual(loads, ["persisted-session", "other-session", "persisted-session"]); + + yield* runtime.readThreadSnapshot({ providerThread: otherThread }); + assert.lengthOf(loads, 3, "a snapshot has no model to read its load against"); + const resumedAfterSnapshot = yield* runtime.resumeThread({ + providerThread: otherThread, + modelSelection: selection("small"), + runtimePolicy, + }); + assert.deepEqual(resumedAfterSnapshot.contextUsage, { usedTokens: 200, maxTokens: 1_000 }); + assert.lengthOf(loads, 4); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + + it.live("does not read another session's replayed usage against the active model", () => + Effect.gen(function* () { + const contextWindow = (selection: ModelSelection) => + selection.model === "small" ? 1_000 : 2_000; + const { runtime, threadId, instanceId, runtimePolicy, selection, providerThread, update } = + yield* openContextUsageRuntime({ + name: "replay", + contextUsageFromSession: ({ selection }) => ({ + usedTokens: 100, + maxTokens: contextWindow(selection), + }), + contextUsageFromSessionUpdate: ({ selection, notification }) => + notification.update.sessionUpdate === "session_info_update" && + notification.update._meta?.usage === true + ? { usedTokens: 500, maxTokens: contextWindow(selection) } + : undefined, + replayOnActivation: { sessionUpdate: "session_info_update", _meta: { usage: true } }, + }); + const sessionId = providerThread.nativeThreadRef?.nativeId ?? ""; + + yield* runtime.resumeThread({ + providerThread: withNativeSession(providerThread, "large-session"), + modelSelection: selection("large"), + runtimePolicy, + }); + const resumed = yield* runtime.resumeThread({ + providerThread, + modelSelection: selection("small"), + runtimePolicy, + }); + assert.deepEqual(resumed.contextUsage, { usedTokens: 100, maxTokens: 1_000 }); + + // Once this session is active, its own reading uses its own model. + yield* update({ + sessionId, + update: { sessionUpdate: "session_info_update", _meta: { usage: true } }, + }); + const now = yield* DateTime.now; + yield* runtime.startTurn( + makeTurnInput({ + threadId, + providerThread: resumed, + instanceId, + runtimePolicy, + now, + ordinal: 1, + modelSelection: selection("small"), + }), + ); + const usages = (yield* collectUntilTerminal(runtime)).flatMap((event) => + event.type === "provider_thread.updated" && + event.providerThread.nativeThreadRef?.nativeId === sessionId && + event.providerThread.contextUsage != null + ? [event.providerThread.contextUsage] + : [], + ); + assert.deepEqual(usages[0], { usedTokens: 500, maxTokens: 1_000 }); + assert.isEmpty(usages.filter((usage) => usage.maxTokens !== 1_000)); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + + it.live("keeps a load's unknown usage from coming back through later updates", () => + Effect.gen(function* () { + const { runtime, threadId, instanceId, runtimePolicy, selection, providerThread, update } = + yield* openContextUsageRuntime({ + name: "null-load", + contextUsageFromSession: () => null, + }); + const sessionId = providerThread.nativeThreadRef?.nativeId ?? ""; + const reading = { usedTokens: 900, maxTokens: 1_000 }; + const now = yield* DateTime.now; + yield* runtime.startTurn( + makeTurnInput({ + threadId, + providerThread: { ...providerThread, contextUsage: reading }, + instanceId, + runtimePolicy, + now, + ordinal: 1, + modelSelection: selection("small"), + }), + ); + assert.deepEqual(activeContextUsage(yield* collectUntilTerminal(runtime)), reading); + + yield* runtime.resumeThread({ + providerThread: withNativeSession(providerThread, "other-session"), + modelSelection: selection("small"), + runtimePolicy, + }); + const resumed = yield* runtime.resumeThread({ + providerThread: { ...providerThread, contextUsage: reading }, + modelSelection: selection("small"), + runtimePolicy, + }); + assert.isNull(resumed.contextUsage); + + assert.isNull(yield* contextUsageAfterRename(runtime, update, sessionId)); + + yield* runtime.resumeThread({ + providerThread: withNativeSession(providerThread, "other-session"), + modelSelection: selection("small"), + runtimePolicy, + }); + const later = yield* DateTime.now; + yield* runtime.startTurn( + makeTurnInput({ + threadId, + providerThread: { ...providerThread, contextUsage: reading }, + instanceId, + runtimePolicy, + now: later, + ordinal: 2, + modelSelection: selection("small"), + }), + ); + assert.isNull(activeContextUsage(yield* collectUntilTerminal(runtime))); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + + it.live("keeps the saved reading when a load reports nothing", () => + Effect.gen(function* () { + const { runtime, threadId, instanceId, runtimePolicy, selection, providerThread, update } = + yield* openContextUsageRuntime({ + name: "silent-load", + contextUsageFromSession: () => undefined, + }); + const sessionId = "saved-session"; + const savedThread = withNativeSession(providerThread, sessionId); + const saved = { usedTokens: 900, maxTokens: 1_000 }; + const resume = (thread: OrchestrationV2ProviderThread) => + runtime.resumeThread({ + providerThread: thread, + modelSelection: selection("small"), + runtimePolicy, + }); + + yield* resume(savedThread); + yield* update({ + sessionId, + update: { sessionUpdate: "usage_update", used: 900, size: 1_000 }, + }); + yield* resume(providerThread); + const resumed = yield* resume({ ...savedThread, contextUsage: saved }); + assert.deepEqual(resumed.contextUsage, saved); + + yield* resume(providerThread); + const now = yield* DateTime.now; + yield* runtime.startTurn( + makeTurnInput({ + threadId, + providerThread: savedThread, + instanceId, + runtimePolicy, + now, + ordinal: 1, + modelSelection: selection("small"), + }), + ); + assert.deepEqual(activeContextUsage(yield* collectUntilTerminal(runtime)), saved); + + yield* resume(providerThread); + yield* resume({ ...savedThread, contextUsage: saved }); + assert.deepEqual(yield* contextUsageAfterRename(runtime, update, sessionId), saved); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + + it.live("drops a reading measured under another model when the selection changes", () => + Effect.gen(function* () { + const contextWindow = (model: string) => (model === "small" ? 1_000 : 2_000); + const { runtime, threadId, instanceId, runtimePolicy, selection, providerThread, update } = + yield* openContextUsageRuntime({ + name: "model-change", + contextUsageFromSession: () => undefined, + contextUsageFromSessionUpdate: ({ selection, notification }) => + notification.update.sessionUpdate !== "session_info_update" || + typeof notification.update._meta?.usage !== "boolean" + ? undefined + : notification.update._meta.usage + ? { + usedTokens: 500, + maxTokens: contextWindow(selection.model), + compactsAutomatically: true, + autoCompactThreshold: contextWindow(selection.model) * 0.8, + } + : null, + }); + const sessionId = providerThread.nativeThreadRef?.nativeId ?? ""; + const otherThread = withNativeSession(providerThread, "other-session"); + const drainReading = (usedTokens: number) => + runtime.events.pipe( + Stream.filter( + (event) => + event.type === "provider_thread.updated" && + event.providerThread.contextUsage?.usedTokens === usedTokens, + ), + Stream.take(1), + Stream.runDrain, + ); + const liveUsage = (id: string, usage: boolean) => + update({ + sessionId: id, + update: { sessionUpdate: "session_info_update", _meta: { usage } }, + }); + const reading = liveUsage(sessionId, true).pipe(Effect.andThen(drainReading(500))); + const usageUpdate = (id: string, used: number, size: number) => + update({ sessionId: id, update: { sessionUpdate: "usage_update", used, size } }); + const resume = (thread: OrchestrationV2ProviderThread, model: string) => + runtime.resumeThread({ + providerThread: thread, + modelSelection: selection(model), + runtimePolicy, + }); + const turnOn = ( + model: string, + ordinal: number, + thread: OrchestrationV2ProviderThread = providerThread, + ) => + DateTime.now.pipe( + Effect.flatMap((now) => + runtime.startTurn( + makeTurnInput({ + threadId, + providerThread: thread, + instanceId, + runtimePolicy, + now, + ordinal, + modelSelection: selection(model), + }), + ), + ), + Effect.andThen(collectUntilTerminal(runtime)), + ); + const threadUsages = (events: ReadonlyArray) => + events.flatMap((event) => + event.type === "provider_thread.updated" ? [event.providerThread.contextUsage] : [], + ); + + // Under "small", then a load that names no model, then a turn on "large". + yield* reading; + yield* resume(otherThread, "small"); + const afterLoad = threadUsages(yield* turnOn("large", 1)); + assert.isNotEmpty(afterLoad); + assert.isEmpty(afterLoad.filter((usage) => usage != null)); + + // The same model change through resume, which also clears the remembered thread. + yield* reading; + yield* resume(otherThread, "large"); + const resumed = yield* resume(providerThread, "small"); + assert.isNull(resumed.contextUsage); + assert.isNull(yield* contextUsageAfterRename(runtime, update, sessionId)); + + // No load at all: the model changes on the active session. + yield* reading; + const switched = threadUsages(yield* turnOn("large", 2)); + assert.isNotEmpty(switched); + assert.isEmpty(switched.filter((usage) => usage != null)); + + // A first usage_update reading has no earlier record, but still belongs to its model. + const usageThread = withNativeSession(providerThread, "usage-session"); + yield* resume(usageThread, "small"); + yield* usageUpdate("usage-session", 700, 1_000); + const afterFirstReading = threadUsages(yield* turnOn("large", 3, usageThread)); + assert.isNotEmpty(afterFirstReading); + assert.isEmpty(afterFirstReading.filter((usage) => usage != null)); + + // Clearing leaves the record on "large", but the next reading is measured under "small". + yield* liveUsage("usage-session", true); + yield* drainReading(500); + yield* liveUsage("usage-session", false); + yield* resume(otherThread, "small"); + yield* resume(usageThread, "small"); + yield* usageUpdate("usage-session", 300, 1_000); + yield* drainReading(300); + const afterNewerReading = threadUsages(yield* turnOn("small", 4, usageThread)); + assert.deepEqual(afterNewerReading[0], { usedTokens: 300, maxTokens: 1_000 }); + + // Another session is active, so the old "small" record can't vouch for this reading. + yield* resume(otherThread, "small"); + yield* usageUpdate("usage-session", 400, 1_000); + yield* drainReading(400); + const afterUnattributed = threadUsages(yield* turnOn("small", 5, usageThread)); + assert.isNotEmpty(afterUnattributed); + assert.isEmpty(afterUnattributed.filter((usage) => usage != null)); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + it.live("creates a fresh ACP identity when recovering an exhausted target", () => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; diff --git a/apps/server/src/orchestration-v2/Adapters/KiroAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/KiroAdapterV2.test.ts index 30304449c0f06..1d2496b4ce652 100644 --- a/apps/server/src/orchestration-v2/Adapters/KiroAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/KiroAdapterV2.test.ts @@ -56,6 +56,9 @@ import { KiroSettings } from "@t3tools/provider-kiro/settings"; import { KIRO_PROVIDER, kiroApprovalOptions, + kiroContextUsageFromSession, + kiroContextUsageFromSessionUpdate, + kiroModelContextWindow, kiroAutopilotValue, kiroPermissionDisposition, makeKiroAdapterV2, @@ -74,6 +77,107 @@ const testLayer = Layer.mergeAll( const SESSION_ID = "sess_abb5e0cf-d4a2-4360-9f03-2f8f0889a707"; const ENABLED_KIRO_SETTINGS = Schema.decodeSync(KiroSettings)({ enabled: true }); +it("reports context windows only for explicit known Kiro models", () => { + const selection = (model: string): ModelSelection => ({ + instanceId: ProviderInstanceId.make("kiro"), + model, + }); + assert.equal(kiroModelContextWindow(selection("claude-opus-5.5")), 1_000_000); + assert.equal(kiroModelContextWindow(selection("claude-haiku-4.5")), 200_000); + assert.equal(kiroModelContextWindow(selection("gpt-5.6-sol")), 986_000); + assert.isUndefined(kiroModelContextWindow(selection("auto"))); + assert.isUndefined(kiroModelContextWindow(selection("default"))); + assert.isUndefined(kiroModelContextWindow(selection("custom-model"))); +}); + +it("maps only valid context telemetry for the model that Kiro loaded", () => { + const selection = (model: string): ModelSelection => ({ + instanceId: ProviderInstanceId.make("kiro"), + model, + }); + const session = (input: { + readonly model?: string; + readonly contextUsage: unknown; + }): AcpSessionRuntime.AcpSessionRuntimeStartResult => + ({ + sessionId: SESSION_ID, + sessionSetupResult: { + _meta: { contextUsage: input.contextUsage }, + configOptions: + input.model === undefined + ? [] + : [ + { + type: "select", + id: "model", + name: "Model", + currentValue: input.model, + options: [], + }, + ], + }, + }) as unknown as AcpSessionRuntime.AcpSessionRuntimeStartResult; + + const loaded = session({ + model: " claude-opus-5.5 ", + contextUsage: { + usagePercentage: 29.5217, + summarizationThreshold: 80, + truncationThreshold: 95, + }, + }); + assert.deepEqual( + kiroContextUsageFromSession({ selection: selection("claude-opus-5.5"), session: loaded }), + { + usedTokens: 295_217, + maxTokens: 1_000_000, + compactsAutomatically: true, + autoCompactThreshold: 800_000, + }, + ); + assert.isNull( + kiroContextUsageFromSession({ selection: selection("claude-sonnet-4.5"), session: loaded }), + ); + assert.isNull(kiroContextUsageFromSession({ selection: selection("auto"), session: loaded })); + assert.isUndefined( + kiroContextUsageFromSession({ + selection: selection("claude-opus-5.5"), + session: session({ contextUsage: { usagePercentage: 10 } }), + }), + ); + assert.isNull( + kiroContextUsageFromSession({ + selection: selection("claude-opus-5.5"), + session: session({ model: "claude-opus-5.5", contextUsage: { usagePercentage: 101 } }), + }), + ); + + const withoutThreshold = session({ + model: "claude-opus-5.5", + contextUsage: { usagePercentage: 10, summarizationThreshold: 0 }, + }); + assert.deepEqual( + kiroContextUsageFromSession({ + selection: selection("claude-opus-5.5"), + session: withoutThreshold, + }), + { usedTokens: 100_000, maxTokens: 1_000_000 }, + ); + assert.isUndefined( + kiroContextUsageFromSessionUpdate({ + selection: selection("claude-opus-5.5"), + session: loaded, + notification: { + sessionId: SESSION_ID, + update: { + sessionUpdate: "session_info_update", + _meta: { kiro: { contextUsage: { usagePercentage: -1 } } }, + }, + } as EffectAcpSchema.SessionNotification, + }), + ); +}); + type Frame = Record; const outbound = (method: string, params: unknown = "", label = method): Frame => ({ type: "expect_outbound", diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/provider_thread_resume/kiro_output.ts b/apps/server/src/orchestration-v2/testkit/fixtures/provider_thread_resume/kiro_output.ts index fc6ed73ca013f..4950cf3d96663 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/provider_thread_resume/kiro_output.ts +++ b/apps/server/src/orchestration-v2/testkit/fixtures/provider_thread_resume/kiro_output.ts @@ -54,6 +54,20 @@ export function assertKiroProviderThreadResumeOutput( PROVIDER_THREAD_RESUME_SECOND_PROMPT, ]); assert.lengthOf(projection.providerThreads, 1, "resume must keep one provider thread"); + assert.deepEqual(projection.providerThreads[0]?.contextUsage, { + usedTokens: 19_093, + maxTokens: 200_000, + compactsAutomatically: true, + autoCompactThreshold: 160_000, + }); + const usages = result.domainEvents.flatMap((event) => + event.type === "provider-thread.updated" ? [event.payload.contextUsage] : [], + ); + assert.notInclude( + usages.slice(usages.findIndex((usage) => usage != null)), + null, + "the saved reading must survive a load that names no model", + ); const answers = projection.turnItems.flatMap((item) => item.type === "assistant_message" ? [item.text] : [], ); diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index d08ecd8ce87ee..3d731546502a8 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -363,6 +363,16 @@ export interface AcpAdapterV2Flavor { selection: ModelSelection, session: AcpSessionRuntime.AcpSessionRuntimeStartResult, ) => number | undefined; + /** Null clears the saved reading; undefined keeps one measured under the same model. */ + readonly contextUsageFromSession?: (input: { + readonly selection: ModelSelection; + readonly session: AcpSessionRuntime.AcpSessionRuntimeStartResult; + }) => ThreadTokenUsageSnapshot | null | undefined; + readonly contextUsageFromSessionUpdate?: (input: { + readonly selection: ModelSelection; + readonly session: AcpSessionRuntime.AcpSessionRuntimeStartResult; + readonly notification: EffectAcpSchema.SessionNotification; + }) => ThreadTokenUsageSnapshot | null | undefined; readonly applyModelSelection?: (input: { readonly runtime: AcpSessionRuntime.AcpSessionRuntime["Service"]; readonly startResult: AcpSessionRuntime.AcpSessionRuntimeStartResult; @@ -1886,6 +1896,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const contextUsageBySessionId = yield* Ref.make( new Map(), ); + const contextUsageModelBySessionId = new Map(); const nativeMetadataBySessionId = yield* Ref.make( new Map(), ); @@ -1913,6 +1924,70 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( }); const activeSelection = yield* Ref.make(null); const activeInteractionMode = yield* Ref.make(null); + const pendingLoadedSession = yield* Ref.make<{ + readonly setup: AcpSessionRuntime.AcpSessionRuntimeStartResult; + readonly savedUsage: ThreadTokenUsageSnapshot | undefined; + } | null>(null); + /** + * Makes a session T3 loaded, rather than created, the active one. Its load telemetry waits + * for the next resume or turn, which knows the model to read it against. + */ + const adoptLoadedSession = Effect.fnUntraced(function* ( + setup: AcpSessionRuntime.AcpSessionRuntimeStartResult, + ) { + yield* Ref.set(activeSelection, null); + yield* Ref.set(activeSessionId, setup.sessionId); + yield* setActiveSessionSetup(setup); + if (flavor.contextUsageFromSession === undefined) return; + yield* Ref.set(pendingLoadedSession, { + setup, + savedUsage: (yield* Ref.get(contextUsageBySessionId)).get(setup.sessionId), + }); + }); + /** + * Settles a session's reading for the model about to run; undefined leaves it as it is. + * A window and threshold from another or an unknown model never carry over. A reading newer + * than a pending load wins; an older one only outlives a load that reports nothing. + */ + const settleContextUsage = Effect.fnUntraced(function* ( + sessionId: string, + selection: ModelSelection, + ) { + if (flavor.contextUsageFromSession === undefined) return undefined; + const pending = yield* Ref.modify(pendingLoadedSession, (current) => + current?.setup.sessionId === sessionId + ? ([current, null] as const) + : ([null, current] as const), + ); + const cached = (yield* Ref.get(contextUsageBySessionId)).get(sessionId); + const fromOtherModel = + cached !== undefined && contextUsageModelBySessionId.get(sessionId) !== selection.model; + if (pending === null && !fromOtherModel) return undefined; + const loaded = + pending === null + ? undefined + : flavor.contextUsageFromSession({ selection, session: pending.setup }); + const usage = fromOtherModel + ? (loaded ?? null) + : cached !== undefined && cached !== pending?.savedUsage + ? cached + : loaded; + if (usage === undefined) return undefined; + yield* Ref.update(contextUsageBySessionId, (current) => { + const updated = new Map(current); + if (usage === null) updated.delete(sessionId); + else updated.set(sessionId, usage); + return updated; + }); + contextUsageModelBySessionId.set(sessionId, selection.model); + yield* Ref.update(providerThreadByNativeSessionId, (current) => { + const known = current.get(sessionId); + return known === undefined + ? current + : new Map(current).set(sessionId, { ...known, contextUsage: usage }); + }); + return usage; + }); const promptInstructionStates = yield* Ref.make(new Map()); // Unique markers keep delayed prompt completions from clearing later compactions. const compactionMarkers = yield* Ref.make(new Map()); @@ -4882,10 +4957,46 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( (event) => event._tag === "UsageUpdated" || event._tag === "SessionInfoUpdated", ); if (stateEvent === undefined) return; + let contextUsageOverride: ThreadTokenUsageSnapshot | null | undefined; + if ( + stateEvent._tag === "SessionInfoUpdated" && + flavor.contextUsageFromSessionUpdate !== undefined + ) { + const selection = yield* Ref.get(activeSelection); + const session = yield* Ref.get(activeSessionSetup); + // Replay during another session's load isn't this session's model; the load reports it. + if (selection !== null && session?.sessionId === notification.sessionId) { + contextUsageOverride = flavor.contextUsageFromSessionUpdate({ + selection, + session, + notification, + }); + if (contextUsageOverride !== undefined) { + const nextContextUsage = contextUsageOverride; + yield* Ref.update(contextUsageBySessionId, (current) => { + const updated = new Map(current); + if (nextContextUsage === null) updated.delete(notification.sessionId); + else updated.set(notification.sessionId, nextContextUsage); + return updated; + }); + contextUsageModelBySessionId.set(notification.sessionId, selection.model); + if (context?.nativeThreadId === notification.sessionId) { + context.contextUsage = contextUsageOverride; + } + } + } + } if (stateEvent._tag === "UsageUpdated") { yield* Ref.update(contextUsageBySessionId, (current) => new Map(current).set(notification.sessionId, stateEvent.usage), ); + const selection = yield* Ref.get(activeSelection); + const session = yield* Ref.get(activeSessionSetup); + if (selection !== null && session?.sessionId === notification.sessionId) { + contextUsageModelBySessionId.set(notification.sessionId, selection.model); + } else { + contextUsageModelBySessionId.delete(notification.sessionId); + } if (context?.nativeThreadId === notification.sessionId) { context.contextUsage = stateEvent.usage; } @@ -4909,9 +5020,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const providerThread: OrchestrationV2ProviderThread = { ...knownProviderThread, contextUsage: - (yield* Ref.get(contextUsageBySessionId)).get(notification.sessionId) ?? - knownProviderThread.contextUsage ?? - null, + contextUsageOverride !== undefined + ? contextUsageOverride + : ((yield* Ref.get(contextUsageBySessionId)).get(notification.sessionId) ?? + knownProviderThread.contextUsage ?? + null), nativeMetadata: (yield* Ref.get(nativeMetadataBySessionId)).get(notification.sessionId) ?? knownProviderThread.nativeMetadata ?? @@ -7040,8 +7153,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( prepareClaimableTerminalEnvironment(yield* readMcpContext(input.threadId)); return yield* startAcpRuntime(input.threadId); }); - yield* Ref.set(activeSessionId, started.sessionId); - yield* setActiveSessionSetup(started); + if (started.sessionId === input.initialNativeThreadId) { + yield* adoptLoadedSession(started); + } else { + yield* Ref.set(activeSessionId, started.sessionId); + yield* setActiveSessionSetup(started); + } rememberTerminalEnvironment(started.sessionId, yield* readMcpContext(input.threadId)); const capabilities = negotiatedCapabilities( flavor.capabilities, @@ -7871,8 +7988,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( (yield* Ref.get(activeSessionId)) !== requestedSessionId || restartAfterInterrupt; if (needsSessionActivation) { const activated = yield* activateSession(requestedSessionId, turnInput.threadId); - yield* Ref.set(activeSessionId, activated.sessionId); - yield* setActiveSessionSetup(activated); + yield* adoptLoadedSession(activated); yield* configureSession(activated, turnInput.modelSelection, turnInput.runtimePolicy); yield* Ref.set(activeSelection, turnInput.modelSelection); yield* Ref.set(activeInteractionMode, turnInput.runtimePolicy.interactionMode); @@ -7943,6 +8059,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const promptWireSettled = yield* Deferred.make(); const promptDispatched = yield* Deferred.make(); const promptStarted = yield* Deferred.make(); + const settledContextUsage = yield* settleContextUsage( + requestedSessionId, + turnInput.modelSelection, + ); const rememberedContextUsage = (yield* Ref.get(contextUsageBySessionId)).get( requestedSessionId, ); @@ -7976,7 +8096,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( user: { current: null, nextSegment: 0 }, assistant: { current: null, nextSegment: 0 }, reasoning: { current: null, nextSegment: 0 }, - contextUsage: rememberedContextUsage ?? turnInput.providerThread.contextUsage ?? null, + contextUsage: + settledContextUsage !== undefined + ? settledContextUsage + : (rememberedContextUsage ?? turnInput.providerThread.contextUsage ?? null), nativeMetadata: initialNativeMetadata, tools: new Map(), toolUpdatesSkipped: new Map(), @@ -8512,6 +8635,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const restartAfterInterrupt = yield* restartRuntimeAfterTeardownIfRequired( threadInput.providerThread.appThreadId, ).pipe(Effect.tapError(() => restorePreviousItemIdentity)); + const nextSelection = threadInput.modelSelection ?? input.modelSelection; if ((yield* Ref.get(activeSessionId)) !== sessionId || restartAfterInterrupt) { yield* Ref.set(snapshot, { order: [], @@ -8524,18 +8648,20 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( sessionId, threadInput.providerThread.appThreadId, ).pipe(Effect.tapError(() => restorePreviousItemIdentity)); - yield* Ref.set(activeSessionId, activated.sessionId); - yield* setActiveSessionSetup(activated); - const nextSelection = threadInput.modelSelection ?? input.modelSelection; + yield* adoptLoadedSession(activated); const nextRuntimePolicy = threadInput.runtimePolicy ?? input.runtimePolicy; yield* configureSession(activated, nextSelection, nextRuntimePolicy); yield* Ref.set(activeSelection, nextSelection); yield* Ref.set(activeInteractionMode, nextRuntimePolicy.interactionMode); } + const settledContextUsage = yield* settleContextUsage(sessionId, nextSelection); const now = yield* DateTime.now; return { ...threadInput.providerThread, providerSessionId: input.providerSessionId, + ...(settledContextUsage === undefined + ? {} + : { contextUsage: settledContextUsage }), status: "idle" as const, updatedAt: now, }; @@ -9036,9 +9162,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( activated.sessionId, yield* readMcpContext(snapshotInput.providerThread.appThreadId), ); - yield* Ref.set(activeSessionId, activated.sessionId); - yield* setActiveSessionSetup(activated); - yield* Ref.set(activeSelection, null); + yield* adoptLoadedSession(activated); yield* Ref.set(activeInteractionMode, null); } const state = yield* Ref.get(snapshot); diff --git a/packages/provider-kiro/src/server/adapter.ts b/packages/provider-kiro/src/server/adapter.ts index 6815b60e8f227..b017d6a53f6c5 100644 --- a/packages/provider-kiro/src/server/adapter.ts +++ b/packages/provider-kiro/src/server/adapter.ts @@ -4,6 +4,7 @@ import { type ModelSelection, ProviderDriverKind, type OrchestrationV2ProviderCapabilities, + type ThreadTokenUsageSnapshot, } from "@t3tools/contracts"; import { KiroSettings } from "../settings.ts"; import { getModelSelectionStringOptionValue } from "@t3tools/shared/model"; @@ -59,6 +60,123 @@ const KIRO_STEER_METHOD = "_session/steer"; const KIRO_FALLBACK_DEFAULT_MODEL = "auto"; const DEFAULT_KIRO_SETTINGS = Schema.decodeSync(KiroSettings)({}); +/** + * Kiro model ids and context windows documented at https://kiro.dev/docs/models/. + * The docs round GPT-5.6 to 1M; `--list-models --format json` reports its exact window. + */ +const KIRO_MODEL_CONTEXT_WINDOWS: Readonly> = { + "gpt-5.6-sol": 986_000, + "gpt-5.6-terra": 986_000, + "gpt-5.6-luna": 986_000, + "claude-fable-5.1": 1_000_000, + "claude-opus-5.5": 1_000_000, + "claude-sonnet-5.5": 1_000_000, + "claude-haiku-5.5": 1_000_000, + "claude-opus-5": 1_000_000, + "claude-opus-4.8": 1_000_000, + "claude-opus-4.7": 1_000_000, + "claude-opus-4.6": 1_000_000, + "claude-opus-4.5": 200_000, + "claude-sonnet-5": 1_000_000, + "claude-sonnet-4.6": 1_000_000, + "claude-sonnet-4.5": 200_000, + "claude-sonnet-4.0": 200_000, + "claude-haiku-4.5": 200_000, + "kimi-k3": 1_000_000, + "deepseek-3.2": 128_000, + "minimax-m2.5": 200_000, + "glm-5": 200_000, + "minimax-m2.1": 200_000, + "qwen3-coder-next": 256_000, +}; + +export function kiroModelContextWindow(selection: ModelSelection): number | undefined { + const model = selection.model.trim(); + return Object.hasOwn(KIRO_MODEL_CONTEXT_WINDOWS, model) + ? KIRO_MODEL_CONTEXT_WINDOWS[model] + : undefined; +} + +const KiroContextPercentage = Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 100 })); +const decodeKiroSessionContextUsage = Schema.decodeUnknownOption( + Schema.Struct({ + contextUsage: Schema.Struct({ + usagePercentage: KiroContextPercentage, + summarizationThreshold: Schema.optional(KiroContextPercentage), + truncationThreshold: Schema.optional(KiroContextPercentage), + }), + }), +); +const decodeKiroLiveContextUsage = Schema.decodeUnknownOption( + Schema.Struct({ + kiro: Schema.Struct({ + contextUsage: Schema.Struct({ usagePercentage: KiroContextPercentage }), + }), + }), +); + +function kiroContextUsageSnapshot(input: { + readonly selection: ModelSelection; + readonly session: AcpSessionRuntime.AcpSessionRuntimeStartResult; + readonly usagePercentage: number; +}): ThreadTokenUsageSnapshot | null { + const maxTokens = kiroModelContextWindow(input.selection); + if (maxTokens === undefined) return null; + const metadata = Option.getOrUndefined( + decodeKiroSessionContextUsage(input.session.sessionSetupResult._meta), + ); + const threshold = metadata?.contextUsage.summarizationThreshold; + return { + usedTokens: Math.round((input.usagePercentage / 100) * maxTokens), + maxTokens, + ...(threshold === undefined || threshold <= 0 + ? {} + : { + compactsAutomatically: true, + autoCompactThreshold: Math.round((threshold / 100) * maxTokens), + }), + }; +} + +export function kiroContextUsageFromSession(input: { + readonly selection: ModelSelection; + readonly session: AcpSessionRuntime.AcpSessionRuntimeStartResult; +}): ThreadTokenUsageSnapshot | null | undefined { + const modelOption = findKiroModelOption(input.session.sessionSetupResult.configOptions ?? []); + const loadedModel = ( + modelOption?.type === "select" + ? modelOption.currentValue + : input.session.sessionSetupResult.models?.currentModelId + )?.trim(); + // Kiro's load can name no model, which says nothing against the reading T3 already saved. + if (!loadedModel) return undefined; + const metadata = Option.getOrUndefined( + decodeKiroSessionContextUsage(input.session.sessionSetupResult._meta), + ); + if (metadata === undefined || loadedModel !== input.selection.model.trim()) return null; + return kiroContextUsageSnapshot({ + ...input, + usagePercentage: metadata.contextUsage.usagePercentage, + }); +} + +export function kiroContextUsageFromSessionUpdate(input: { + readonly selection: ModelSelection; + readonly session: AcpSessionRuntime.AcpSessionRuntimeStartResult; + readonly notification: EffectAcpSchema.SessionNotification; +}): ThreadTokenUsageSnapshot | null | undefined { + if (input.notification.update.sessionUpdate !== "session_info_update") return undefined; + const metadata = Option.getOrUndefined( + decodeKiroLiveContextUsage(input.notification.update._meta), + ); + if (metadata === undefined) return undefined; + return kiroContextUsageSnapshot({ + selection: input.selection, + session: input.session, + usagePercentage: metadata.kiro.contextUsage.usagePercentage, + }); +} + const KiroProviderCapabilitiesV2 = { ...AcpProviderCapabilitiesV2, sessions: { @@ -539,6 +657,9 @@ function makeKiroAcpAdapterFlavor(options: KiroAdapterV2Options): AcpAdapterV2Fl driver: KIRO_PROVIDER, runtimeHarness: "Kiro", capabilities: KiroProviderCapabilitiesV2, + getModelContextWindow: kiroModelContextWindow, + contextUsageFromSession: kiroContextUsageFromSession, + contextUsageFromSessionUpdate: kiroContextUsageFromSessionUpdate, // Kiro V3 loads the user's hooks only when the client opts in. `v2` makes // Kiro run them itself; `enabled` alone hands them to the client as // `_kiro/hooks/*` requests, which T3 does not answer. diff --git a/packages/provider-kiro/src/testing.ts b/packages/provider-kiro/src/testing.ts index 3f18a713ad8c3..655d011e4b8e3 100644 --- a/packages/provider-kiro/src/testing.ts +++ b/packages/provider-kiro/src/testing.ts @@ -1,4 +1,11 @@ -export { KIRO_PROVIDER, makeKiroAdapterV2, type KiroAdapterV2Options } from "./server/adapter.ts"; +export { + KIRO_PROVIDER, + kiroContextUsageFromSession, + kiroContextUsageFromSessionUpdate, + kiroModelContextWindow, + makeKiroAdapterV2, + type KiroAdapterV2Options, +} from "./server/adapter.ts"; export { kiroApprovalOptions, kiroAutopilotValue, From ebdf422d115c9a5e3d0f514e40f69bb8b098136a Mon Sep 17 00:00:00 2001 From: TinBane <363069+TinBane@users.noreply.github.com> Date: Wed, 4 Nov 2026 14:16:15 +1100 Subject: [PATCH 2/5] fix(provider-acp): settle context usage under the callback permit Live session updates write the per-session usage maps under the runtime callback permit, but turn start and resume settled them holding only the transition permit, so a live reading could be overwritten by an older load value and tagged with the wrong model. Take the callback permit for the settlement. Co-Authored-By: Claude Opus 5.5 --- packages/provider-acp/src/server/adapter.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 3d731546502a8..b16e36e3374f1 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -8059,9 +8059,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const promptWireSettled = yield* Deferred.make(); const promptDispatched = yield* Deferred.make(); const promptStarted = yield* Deferred.make(); - const settledContextUsage = yield* settleContextUsage( - requestedSessionId, - turnInput.modelSelection, + // Live session updates write the same maps under the callback permit. + const settledContextUsage = yield* runtimeCallbackPermit.withPermit( + settleContextUsage(requestedSessionId, turnInput.modelSelection), ); const rememberedContextUsage = (yield* Ref.get(contextUsageBySessionId)).get( requestedSessionId, @@ -8654,7 +8654,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( yield* Ref.set(activeSelection, nextSelection); yield* Ref.set(activeInteractionMode, nextRuntimePolicy.interactionMode); } - const settledContextUsage = yield* settleContextUsage(sessionId, nextSelection); + const settledContextUsage = yield* runtimeCallbackPermit.withPermit( + settleContextUsage(sessionId, nextSelection), + ); const now = yield* DateTime.now; return { ...threadInput.providerThread, From 25129f38898f4feff0def51067f73200b81296df Mon Sep 17 00:00:00 2001 From: TinBane <363069+TinBane@users.noreply.github.com> Date: Wed, 4 Nov 2026 21:23:27 +1100 Subject: [PATCH 3/5] fix(provider-acp): keep live readings from landing behind turn start A live usage update arriving after settlement but before the turn became active updated the cache and remembered thread, but not the turn, which then overwrote them with the older value and published it. Hold the runtime callback permit from settlement through the first turn and provider-thread publication, so such an update lands on the published turn. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AcpAdapterV2.test.ts | 56 +++++ packages/provider-acp/src/server/adapter.ts | 229 +++++++++--------- 2 files changed, 174 insertions(+), 111 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index fb3da60aee8f3..24355562280bb 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -4189,6 +4189,62 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest), Effect.scoped), ); + it.live("publishes a live reading that arrives while a turn is starting", () => + Effect.gen(function* () { + let settled = false; + let liveReading: Fiber.Fiber | undefined; + const { runtime, threadId, instanceId, runtimePolicy, selection, providerThread, update } = + yield* openContextUsageRuntime({ + name: "turn-start-reading", + initialNativeThreadId: "persisted-session", + contextUsageFromSession: () => { + settled = true; + return { usedTokens: 100, maxTokens: 1_000 }; + }, + contextUsageFromSessionUpdate: ({ notification }) => + notification.update.sessionUpdate === "session_info_update" && + notification.update._meta?.usage === true + ? { usedTokens: 500, maxTokens: 1_000 } + : undefined, + }); + const services = yield* Effect.context(); + const startingThread: OrchestrationV2ProviderThread = { + ...providerThread, + get nativeMetadata() { + if (settled && liveReading === undefined) { + // oxlint-disable-next-line t3code/no-manual-effect-runtime-in-tests -- Turn start reads this between settling usage and publishing, where nothing yields; only a synchronous fork lands there. + liveReading = Effect.runForkWith(services)( + update({ + sessionId: "persisted-session", + update: { sessionUpdate: "session_info_update", _meta: { usage: true } }, + }).pipe(Effect.orDie), + ); + } + return null; + }, + }; + const now = yield* DateTime.now; + yield* runtime.startTurn( + makeTurnInput({ + threadId, + providerThread: startingThread, + instanceId, + runtimePolicy, + now, + ordinal: 1, + modelSelection: selection("small"), + }), + ); + const events = yield* collectUntilTerminal(runtime); + assert.isDefined(liveReading); + yield* Fiber.join(liveReading!); + const usages = events.flatMap((event) => + event.type === "provider_thread.updated" ? [event.providerThread.contextUsage] : [], + ); + assert.deepEqual(usages.at(-1), { usedTokens: 500, maxTokens: 1_000 }); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + it.live("creates a fresh ACP identity when recovering an exhausted target", () => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index b16e36e3374f1..ef03bfd9e77e2 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -8059,119 +8059,126 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const promptWireSettled = yield* Deferred.make(); const promptDispatched = yield* Deferred.make(); const promptStarted = yield* Deferred.make(); - // Live session updates write the same maps under the callback permit. - const settledContextUsage = yield* runtimeCallbackPermit.withPermit( - settleContextUsage(requestedSessionId, turnInput.modelSelection), - ); - const rememberedContextUsage = (yield* Ref.get(contextUsageBySessionId)).get( - requestedSessionId, - ); - const rememberedNativeMetadata = (yield* Ref.get(nativeMetadataBySessionId)).get( - requestedSessionId, - ); - const initialNativeMetadata = - rememberedNativeMetadata === undefined - ? (turnInput.providerThread.nativeMetadata ?? null) - : { - ...turnInput.providerThread.nativeMetadata, - ...rememberedNativeMetadata, + // A live reading must land on the published turn, so it waits for the first publication. + const { context, rehydratedCarryoverSubagents } = + yield* runtimeCallbackPermit.withPermit( + Effect.gen(function* () { + const settledContextUsage = yield* settleContextUsage( + requestedSessionId, + turnInput.modelSelection, + ); + const rememberedContextUsage = (yield* Ref.get(contextUsageBySessionId)).get( + requestedSessionId, + ); + const rememberedNativeMetadata = (yield* Ref.get(nativeMetadataBySessionId)).get( + requestedSessionId, + ); + const initialNativeMetadata = + rememberedNativeMetadata === undefined + ? (turnInput.providerThread.nativeMetadata ?? null) + : { + ...turnInput.providerThread.nativeMetadata, + ...rememberedNativeMetadata, + }; + if (initialNativeMetadata !== null) { + yield* Ref.update(nativeMetadataBySessionId, (current) => + new Map(current).set(requestedSessionId, initialNativeMetadata), + ); + } + const context: ActiveAcpTurn = { + input: turnInput, + providerTurnId, + nativeThreadId: requestedSessionId, + nativeTurnId, + startedAt, + completed, + itemOrdinals: new Map(), + outgoingPrompt: + (yield* Ref.get(activeSessionSetup))?.initializeResult.protocolVersion === 2 + ? promptParts?.prompt + : undefined, + user: { current: null, nextSegment: 0 }, + assistant: { current: null, nextSegment: 0 }, + reasoning: { current: null, nextSegment: 0 }, + contextUsage: + settledContextUsage !== undefined + ? settledContextUsage + : (rememberedContextUsage ?? turnInput.providerThread.contextUsage ?? null), + nativeMetadata: initialNativeMetadata, + tools: new Map(), + toolUpdatesSkipped: new Map(), + toolStartedAt: new Map(), + subagents: new Map(), + subagentsBySessionId: new Map(), + pendingSubagentNotifications: new Map(), + toolCallIdsByBackgroundTaskId: new Map(), + backgroundCommands: new Map(), + persistentBackgroundTaskIds: new Set(), + awaitingBackgroundHydration: new Set(), + pendingInjectedReport: new Set(), + earlyInjectedReportObserved: false, + plans: new Map(), + interrupted: false, + finalized: false, + finalizedStatus: null, + promptSettled: false, + promptSettledStatus: null, + workAccepted: false, + promptWireSettled, + promptDispatched, + promptSentAt: null, + promptStarted, + promptUsage: undefined, + backgroundFinalizeGeneration: 0, + nativePromptId: undefined, }; - if (initialNativeMetadata !== null) { - yield* Ref.update(nativeMetadataBySessionId, (current) => - new Map(current).set(requestedSessionId, initialNativeMetadata), + const carryover = yield* Ref.getAndSet(carryoverSubagents, null); + let rehydratedCarryoverSubagents: ReadonlyArray = []; + if (carryover !== null && carryover.sessionId === requestedSessionId) { + rehydratedCarryoverSubagents = carryover.subagents; + for (const subagent of carryover.subagents) { + const nativeId = subagent.task.nativeTaskRef?.nativeId ?? null; + if (nativeId !== null) { + context.subagents.set(nativeId, subagent); + } + if (subagent.childSessionId !== null) { + context.subagentsBySessionId.set(subagent.childSessionId, subagent); + } + } + } + yield* Ref.set(activeTurn, context); + yield* Ref.set(stoppedRunQuarantine, false); + const runningTurn = providerTurnPayload(context, "running", null); + yield* Ref.update(providerTurns, (current) => { + const updated = new Map(current); + updated.set(String(runningTurn.id), runningTurn); + return updated; + }); + yield* emitProviderEvent({ + type: "provider_turn.updated", + driver, + threadId: turnInput.threadId, + providerTurn: runningTurn, + }); + const activeProviderThread: OrchestrationV2ProviderThread = { + ...turnInput.providerThread, + providerSessionId: input.providerSessionId, + status: "active", + contextUsage: context.contextUsage, + nativeMetadata: context.nativeMetadata, + updatedAt: startedAt, + }; + yield* Ref.update(providerThreadByNativeSessionId, (current) => + new Map(current).set(requestedSessionId, activeProviderThread), + ); + yield* emitProviderEvent({ + type: "provider_thread.updated", + driver, + providerThread: activeProviderThread, + }); + return { context, rehydratedCarryoverSubagents }; + }), ); - } - const context: ActiveAcpTurn = { - input: turnInput, - providerTurnId, - nativeThreadId: requestedSessionId, - nativeTurnId, - startedAt, - completed, - itemOrdinals: new Map(), - outgoingPrompt: - (yield* Ref.get(activeSessionSetup))?.initializeResult.protocolVersion === 2 - ? promptParts?.prompt - : undefined, - user: { current: null, nextSegment: 0 }, - assistant: { current: null, nextSegment: 0 }, - reasoning: { current: null, nextSegment: 0 }, - contextUsage: - settledContextUsage !== undefined - ? settledContextUsage - : (rememberedContextUsage ?? turnInput.providerThread.contextUsage ?? null), - nativeMetadata: initialNativeMetadata, - tools: new Map(), - toolUpdatesSkipped: new Map(), - toolStartedAt: new Map(), - subagents: new Map(), - subagentsBySessionId: new Map(), - pendingSubagentNotifications: new Map(), - toolCallIdsByBackgroundTaskId: new Map(), - backgroundCommands: new Map(), - persistentBackgroundTaskIds: new Set(), - awaitingBackgroundHydration: new Set(), - pendingInjectedReport: new Set(), - earlyInjectedReportObserved: false, - plans: new Map(), - interrupted: false, - finalized: false, - finalizedStatus: null, - promptSettled: false, - promptSettledStatus: null, - workAccepted: false, - promptWireSettled, - promptDispatched, - promptSentAt: null, - promptStarted, - promptUsage: undefined, - backgroundFinalizeGeneration: 0, - nativePromptId: undefined, - }; - const carryover = yield* Ref.getAndSet(carryoverSubagents, null); - let rehydratedCarryoverSubagents: ReadonlyArray = []; - if (carryover !== null && carryover.sessionId === requestedSessionId) { - rehydratedCarryoverSubagents = carryover.subagents; - for (const subagent of carryover.subagents) { - const nativeId = subagent.task.nativeTaskRef?.nativeId ?? null; - if (nativeId !== null) { - context.subagents.set(nativeId, subagent); - } - if (subagent.childSessionId !== null) { - context.subagentsBySessionId.set(subagent.childSessionId, subagent); - } - } - } - yield* Ref.set(activeTurn, context); - yield* Ref.set(stoppedRunQuarantine, false); - const runningTurn = providerTurnPayload(context, "running", null); - yield* Ref.update(providerTurns, (current) => { - const updated = new Map(current); - updated.set(String(runningTurn.id), runningTurn); - return updated; - }); - yield* emitProviderEvent({ - type: "provider_turn.updated", - driver, - threadId: turnInput.threadId, - providerTurn: runningTurn, - }); - const activeProviderThread: OrchestrationV2ProviderThread = { - ...turnInput.providerThread, - providerSessionId: input.providerSessionId, - status: "active", - contextUsage: context.contextUsage, - nativeMetadata: context.nativeMetadata, - updatedAt: startedAt, - }; - yield* Ref.update(providerThreadByNativeSessionId, (current) => - new Map(current).set(requestedSessionId, activeProviderThread), - ); - yield* emitProviderEvent({ - type: "provider_thread.updated", - driver, - providerThread: activeProviderThread, - }); yield* rememberSnapshotMessage({ createdBy: turnInput.message.createdBy, creationSource: turnInput.message.creationSource, From a0a366fdeffc050b8eb959685595aaf6a24e46b2 Mon Sep 17 00:00:00 2001 From: TinBane <363069+TinBane@users.noreply.github.com> Date: Fri, 13 Nov 2026 12:14:43 +1100 Subject: [PATCH 4/5] fix(provider-acp): a live reading before the load settles is not lost A repeated resume with nothing pending returned no reading, so the caller's older reading came back instead of the cached one; it now returns the cached reading for the same model. A live reading that arrived after T3 loaded a session but before settlement lost to the older load result when it was a clear, since the clear only deleted the cache. Live readings, from session_info_update or an attributed usage_update, are now kept on the pending load in arrival order and outrank it. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AcpAdapterV2.test.ts | 58 ++++++++++++++++++- packages/provider-acp/src/server/adapter.ts | 26 +++++++-- 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index f0240016005dc..ecae0fe4c0337 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -3962,7 +3962,7 @@ describe("AcpAdapterV2", () => { modelSelection: selection("small"), runtimePolicy, }); - assert.isNull(resumedAgain.contextUsage); + assert.deepEqual(resumedAgain.contextUsage, { usedTokens: 100, maxTokens: 1_000 }); assert.deepEqual(loads, ["persisted-session"]); yield* runtime.resumeThread({ @@ -4344,6 +4344,62 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest), Effect.scoped), ); + it.live("keeps a live clear that arrives before the load settles", () => + Effect.gen(function* () { + const { runtime, runtimePolicy, selection, providerThread, update } = + yield* openContextUsageRuntime({ + name: "live-clear-before-settle", + initialNativeThreadId: "persisted-session", + contextUsageFromSession: () => ({ usedTokens: 100, maxTokens: 1_000 }), + contextUsageFromSessionUpdate: ({ notification }) => + notification.update.sessionUpdate === "session_info_update" && + notification.update._meta?.clear === true + ? null + : undefined, + }); + yield* update({ + sessionId: "persisted-session", + update: { sessionUpdate: "session_info_update", _meta: { clear: true } }, + }); + const resumed = yield* runtime.resumeThread({ + providerThread: { ...providerThread, contextUsage: { usedTokens: 50, maxTokens: 1_000 } }, + modelSelection: selection("small"), + runtimePolicy, + }); + assert.isNull(resumed.contextUsage); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + + it.live("lets a newer usage update outrank an earlier live reading before the load settles", () => + Effect.gen(function* () { + const { runtime, runtimePolicy, selection, providerThread, update } = + yield* openContextUsageRuntime({ + name: "usage-after-live-before-settle", + initialNativeThreadId: "persisted-session", + contextUsageFromSession: () => ({ usedTokens: 100, maxTokens: 1_000 }), + contextUsageFromSessionUpdate: ({ notification }) => + notification.update.sessionUpdate === "session_info_update" && + notification.update._meta?.clear === true + ? null + : undefined, + }); + yield* update({ + sessionId: "persisted-session", + update: { sessionUpdate: "session_info_update", _meta: { clear: true } }, + }); + yield* update({ + sessionId: "persisted-session", + update: { sessionUpdate: "usage_update", used: 700, size: 1_000 }, + }); + const resumed = yield* runtime.resumeThread({ + providerThread, + modelSelection: selection("small"), + runtimePolicy, + }); + assert.deepEqual(resumed.contextUsage, { usedTokens: 700, maxTokens: 1_000 }); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + it.live("creates a fresh ACP identity when recovering an exhausted target", () => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 5d376420af323..1904a4518b8e4 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -1970,6 +1970,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const pendingLoadedSession = yield* Ref.make<{ readonly setup: AcpSessionRuntime.AcpSessionRuntimeStartResult; readonly savedUsage: ThreadTokenUsageSnapshot | undefined; + /** A live reading that arrived before settlement; it outranks the load, a clear included. */ + readonly liveUsage?: ThreadTokenUsageSnapshot | null; } | null>(null); /** * Makes a session T3 loaded, rather than created, the active one. Its load telemetry waits @@ -1988,7 +1990,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( }); }); /** - * Settles a session's reading for the model about to run; undefined leaves it as it is. + * Settles a session's reading for the model about to run; undefined means there is none. * A window and threshold from another or an unknown model never carry over. A reading newer * than a pending load wins; an older one only outlives a load that reports nothing. */ @@ -2005,16 +2007,18 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const cached = (yield* Ref.get(contextUsageBySessionId)).get(sessionId); const fromOtherModel = cached !== undefined && contextUsageModelBySessionId.get(sessionId) !== selection.model; - if (pending === null && !fromOtherModel) return undefined; + if (pending === null && !fromOtherModel) return cached; const loaded = - pending === null + pending === null || (pending.liveUsage !== undefined && !fromOtherModel) ? undefined : flavor.contextUsageFromSession({ selection, session: pending.setup }); const usage = fromOtherModel ? (loaded ?? null) - : cached !== undefined && cached !== pending?.savedUsage - ? cached - : loaded; + : pending?.liveUsage !== undefined + ? pending.liveUsage + : cached !== undefined && cached !== pending?.savedUsage + ? cached + : loaded; if (usage === undefined) return undefined; yield* Ref.update(contextUsageBySessionId, (current) => { const updated = new Map(current); @@ -5121,6 +5125,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( return updated; }); contextUsageModelBySessionId.set(notification.sessionId, selection.model); + yield* Ref.update(pendingLoadedSession, (current) => + current?.setup.sessionId === notification.sessionId + ? { ...current, liveUsage: nextContextUsage } + : current, + ); if (context?.nativeThreadId === notification.sessionId) { context.contextUsage = contextUsageOverride; } @@ -5135,6 +5144,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const session = yield* Ref.get(activeSessionSetup); if (selection !== null && session?.sessionId === notification.sessionId) { contextUsageModelBySessionId.set(notification.sessionId, selection.model); + yield* Ref.update(pendingLoadedSession, (current) => + current?.setup.sessionId === notification.sessionId + ? { ...current, liveUsage: stateEvent.usage } + : current, + ); } else { contextUsageModelBySessionId.delete(notification.sessionId); } From 891692a247075537e12107923e2461be39177ab2 Mon Sep 17 00:00:00 2001 From: TinBane <363069+TinBane@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:34:54 +1100 Subject: [PATCH 5/5] fix(provider-acp): a cleared context reading stays cleared A clear, from a live update, a settlement or a compaction, deleted the session's cached reading, so a later resume or turn brought back the stale reading the caller supplied. The cache now keeps null as an explicit clear, the fallbacks treat it as the answer, and a load that reports nothing leaves the latest reading in place, a clear included. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AcpAdapterV2.test.ts | 73 ++++++++++++++++--- packages/provider-acp/src/server/adapter.ts | 55 +++++++------- 2 files changed, 90 insertions(+), 38 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 2307241c77104..70ece4538346b 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -4920,27 +4920,82 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest), Effect.scoped), ); - it.live("keeps a live clear that arrives before the load settles", () => + it.live( + "keeps a live clear that arrives before the load settles, across later resumes and turns", + () => + Effect.gen(function* () { + const { runtime, threadId, instanceId, runtimePolicy, selection, providerThread, update } = + yield* openContextUsageRuntime({ + name: "live-clear-before-settle", + initialNativeThreadId: "persisted-session", + contextUsageFromSession: () => ({ usedTokens: 100, maxTokens: 1_000 }), + contextUsageFromSessionUpdate: ({ notification }) => + notification.update.sessionUpdate === "session_info_update" && + notification.update._meta?.clear === true + ? null + : undefined, + }); + yield* update({ + sessionId: "persisted-session", + update: { sessionUpdate: "session_info_update", _meta: { clear: true } }, + }); + const staleThread = { + ...providerThread, + contextUsage: { usedTokens: 50, maxTokens: 1_000 }, + }; + const resume = () => + runtime.resumeThread({ + providerThread: staleThread, + modelSelection: selection("small"), + runtimePolicy, + }); + assert.isNull((yield* resume()).contextUsage); + assert.isNull((yield* resume()).contextUsage); + yield* runtime.startTurn( + makeTurnInput({ + threadId, + providerThread: staleThread, + instanceId, + runtimePolicy, + now: yield* DateTime.now, + ordinal: 1, + modelSelection: selection("small"), + }), + ); + assert.isNull(activeContextUsage(yield* collectUntilTerminal(runtime))); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + + it.live("keeps a cleared reading when a later load of the session reports nothing", () => Effect.gen(function* () { const { runtime, runtimePolicy, selection, providerThread, update } = yield* openContextUsageRuntime({ - name: "live-clear-before-settle", - initialNativeThreadId: "persisted-session", - contextUsageFromSession: () => ({ usedTokens: 100, maxTokens: 1_000 }), + name: "clear-then-silent-load", + contextUsageFromSession: () => undefined, contextUsageFromSessionUpdate: ({ notification }) => notification.update.sessionUpdate === "session_info_update" && notification.update._meta?.clear === true ? null : undefined, }); + const sessionId = "cleared-session"; + const clearedThread = withNativeSession(providerThread, sessionId); + const resume = (thread: OrchestrationV2ProviderThread) => + runtime.resumeThread({ + providerThread: thread, + modelSelection: selection("small"), + runtimePolicy, + }); + + yield* resume(clearedThread); yield* update({ - sessionId: "persisted-session", + sessionId, update: { sessionUpdate: "session_info_update", _meta: { clear: true } }, }); - const resumed = yield* runtime.resumeThread({ - providerThread: { ...providerThread, contextUsage: { usedTokens: 50, maxTokens: 1_000 } }, - modelSelection: selection("small"), - runtimePolicy, + yield* resume(withNativeSession(providerThread, "other-session")); + const resumed = yield* resume({ + ...clearedThread, + contextUsage: { usedTokens: 900, maxTokens: 1_000 }, }); assert.isNull(resumed.contextUsage); }).pipe(Effect.provide(layerTest), Effect.scoped), diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 9b9518f270b87..944fe1827da23 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -2019,8 +2019,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( : flavor.extractSubagentUpdate?.(tool); let turnAttached = yield* Deferred.make(); const activeSessionId = yield* Ref.make(null); + /** Each session's latest reading; null is an explicit clear that outlasts older readings. */ const contextUsageBySessionId = yield* Ref.make( - new Map(), + new Map(), ); const contextUsageModelBySessionId = new Map(); const nativeMetadataBySessionId = yield* Ref.make( @@ -2052,7 +2053,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const activeInteractionMode = yield* Ref.make(null); const pendingLoadedSession = yield* Ref.make<{ readonly setup: AcpSessionRuntime.AcpSessionRuntimeStartResult; - readonly savedUsage: ThreadTokenUsageSnapshot | undefined; + readonly savedUsage: ThreadTokenUsageSnapshot | null | undefined; /** A live reading that arrived before settlement; it outranks the load, a clear included. */ readonly liveUsage?: ThreadTokenUsageSnapshot | null; } | null>(null); @@ -2102,13 +2103,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( : cached !== undefined && cached !== pending?.savedUsage ? cached : loaded; - if (usage === undefined) return undefined; - yield* Ref.update(contextUsageBySessionId, (current) => { - const updated = new Map(current); - if (usage === null) updated.delete(sessionId); - else updated.set(sessionId, usage); - return updated; - }); + // A load that reports nothing leaves the session's latest reading, a clear included. + if (usage === undefined) return cached; + yield* Ref.update(contextUsageBySessionId, (current) => + new Map(current).set(sessionId, usage), + ); contextUsageModelBySessionId.set(sessionId, selection.model); yield* Ref.update(providerThreadByNativeSessionId, (current) => { const known = current.get(sessionId); @@ -4302,11 +4301,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( }; if (status === "completed") { context.contextUsage = null; - yield* Ref.update(contextUsageBySessionId, (current) => { - const updated = new Map(current); - updated.delete(context.nativeThreadId); - return updated; - }); + yield* Ref.update(contextUsageBySessionId, (current) => + new Map(current).set(context.nativeThreadId, null), + ); // A completion projected at turn end must not swallow the agent's later real one. yield* markSessionCompacted( context.nativeThreadId, @@ -5214,12 +5211,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( }); if (contextUsageOverride !== undefined) { const nextContextUsage = contextUsageOverride; - yield* Ref.update(contextUsageBySessionId, (current) => { - const updated = new Map(current); - if (nextContextUsage === null) updated.delete(notification.sessionId); - else updated.set(notification.sessionId, nextContextUsage); - return updated; - }); + yield* Ref.update(contextUsageBySessionId, (current) => + new Map(current).set(notification.sessionId, nextContextUsage), + ); contextUsageModelBySessionId.set(notification.sessionId, selection.model); yield* Ref.update(pendingLoadedSession, (current) => current?.setup.sessionId === notification.sessionId @@ -5268,14 +5262,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( ); if (knownProviderThread !== undefined) { const now = yield* DateTime.now; + const rememberedContextUsage = (yield* Ref.get(contextUsageBySessionId)).get( + notification.sessionId, + ); const providerThread: OrchestrationV2ProviderThread = { ...knownProviderThread, contextUsage: contextUsageOverride !== undefined ? contextUsageOverride - : ((yield* Ref.get(contextUsageBySessionId)).get(notification.sessionId) ?? - knownProviderThread.contextUsage ?? - null), + : rememberedContextUsage !== undefined + ? rememberedContextUsage + : (knownProviderThread.contextUsage ?? null), nativeMetadata: (yield* Ref.get(nativeMetadataBySessionId)).get(notification.sessionId) ?? knownProviderThread.nativeMetadata ?? @@ -7968,11 +7965,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( !context.tools.keys().some((id) => id.startsWith("acp-compaction:")) ) { context.contextUsage = null; - yield* Ref.update(contextUsageBySessionId, (current) => { - const updated = new Map(current); - updated.delete(context.nativeThreadId); - return updated; - }); + yield* Ref.update(contextUsageBySessionId, (current) => + new Map(current).set(context.nativeThreadId, null), + ); const nativeItemId = `${context.nativeTurnId}:compaction`; yield* emitProviderEvent({ type: "turn_item.updated", @@ -8536,7 +8531,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( contextUsage: settledContextUsage !== undefined ? settledContextUsage - : (rememberedContextUsage ?? turnInput.providerThread.contextUsage ?? null), + : rememberedContextUsage !== undefined + ? rememberedContextUsage + : (turnInput.providerThread.contextUsage ?? null), nativeMetadata: initialNativeMetadata, tools: new Map(), toolUpdatesSkipped: new Map(),