diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 02d6f64c0be66..3abe3aadda30c 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -465,6 +465,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; @@ -4262,15 +4389,29 @@ describe("AcpAdapterV2", () => { const mockAgentPath = yield* bundledMockAgentPath; 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, @@ -4301,25 +4442,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({ @@ -4330,6 +4510,527 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + 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.deepEqual(resumedAgain.contextUsage, { usedTokens: 100, maxTokens: 1_000 }); + 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("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( + "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: "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, + update: { sessionUpdate: "session_info_update", _meta: { clear: true } }, + }); + 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), + ); + + 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/apps/server/src/orchestration-v2/Adapters/KiroAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/KiroAdapterV2.test.ts index fb2f20bddd2eb..8fd2dfc387e43 100644 --- a/apps/server/src/orchestration-v2/Adapters/KiroAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/KiroAdapterV2.test.ts @@ -60,6 +60,9 @@ import { KiroSettings } from "@t3tools/provider-kiro/settings"; import { KIRO_PROVIDER, kiroApprovalOptions, + kiroContextUsageFromSession, + kiroContextUsageFromSessionUpdate, + kiroModelContextWindow, kiroAutopilotValue, kiroPermissionDisposition, makeKiroAdapterV2, @@ -78,6 +81,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/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 24cde9fd967ba..912b1cea640c2 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -430,6 +430,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; @@ -2019,9 +2029,11 @@ 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( new Map(), ); @@ -2049,6 +2061,72 @@ 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 | null | 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 + * 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 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. + */ + 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 cached; + const loaded = + pending === null || (pending.liveUsage !== undefined && !fromOtherModel) + ? undefined + : flavor.contextUsageFromSession({ selection, session: pending.setup }); + const usage = fromOtherModel + ? (loaded ?? null) + : pending?.liveUsage !== undefined + ? pending.liveUsage + : cached !== undefined && cached !== pending?.savedUsage + ? cached + : loaded; + // 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); + return known === undefined + ? current + : new Map(current).set(sessionId, { ...known, contextUsage: usage }); + }); + return usage; + }); const promptInstructionStates = yield* Ref.make(new Map()); const promptRuntimeInstructions = yield* Ref.make(new Map()); // Unique markers keep delayed prompt completions from clearing later compactions. @@ -4233,11 +4311,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, @@ -5129,10 +5205,53 @@ 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) => + new Map(current).set(notification.sessionId, nextContextUsage), + ); + 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; + } + } + } + } 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); + yield* Ref.update(pendingLoadedSession, (current) => + current?.setup.sessionId === notification.sessionId + ? { ...current, liveUsage: stateEvent.usage } + : current, + ); + } else { + contextUsageModelBySessionId.delete(notification.sessionId); + } if (context?.nativeThreadId === notification.sessionId) { context.contextUsage = stateEvent.usage; } @@ -5153,12 +5272,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: - (yield* Ref.get(contextUsageBySessionId)).get(notification.sessionId) ?? - knownProviderThread.contextUsage ?? - null, + contextUsageOverride !== undefined + ? contextUsageOverride + : rememberedContextUsage !== undefined + ? rememberedContextUsage + : (knownProviderThread.contextUsage ?? null), nativeMetadata: (yield* Ref.get(nativeMetadataBySessionId)).get(notification.sessionId) ?? knownProviderThread.nativeMetadata ?? @@ -7344,8 +7468,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)); yield* runtime.drainEvents; const capabilities = negotiatedCapabilities( @@ -7868,11 +7996,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", @@ -8305,8 +8431,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); @@ -8393,115 +8518,131 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const promptWireSettled = yield* Deferred.make(); const promptDispatched = yield* Deferred.make(); const promptStarted = yield* Deferred.make(); - 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, closedNamedSegments: new Map() }, + assistant: { current: null, nextSegment: 0, closedNamedSegments: new Map() }, + reasoning: { current: null, nextSegment: 0, closedNamedSegments: new Map() }, + contextUsage: + settledContextUsage !== undefined + ? settledContextUsage + : rememberedContextUsage !== undefined + ? 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, + foregroundIdle: yield* Deferred.make(), + promptSentAt: null, + promptStarted, + promptUsage: undefined, + backgroundFinalizeGeneration: 0, + nativePromptId: undefined, + lastProgressAt: 0, + inactivityWatchdog: 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, }; - if (initialNativeMetadata !== null) { - yield* Ref.update(nativeMetadataBySessionId, (current) => - new Map(current).set(requestedSessionId, initialNativeMetadata), + 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, closedNamedSegments: new Map() }, - assistant: { current: null, nextSegment: 0, closedNamedSegments: new Map() }, - reasoning: { current: null, nextSegment: 0, closedNamedSegments: new Map() }, - contextUsage: 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, - foregroundIdle: yield* Deferred.make(), - promptSentAt: null, - promptStarted, - promptUsage: undefined, - backgroundFinalizeGeneration: 0, - nativePromptId: undefined, - lastProgressAt: 0, - inactivityWatchdog: 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, @@ -8996,24 +9137,29 @@ 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, emptySnapshotState()); const activated = yield* activateSession( 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* runtimeCallbackPermit.withPermit( + settleContextUsage(sessionId, nextSelection), + ); const now = yield* DateTime.now; return { ...threadInput.providerThread, providerSessionId: input.providerSessionId, + ...(settledContextUsage === undefined + ? {} + : { contextUsage: settledContextUsage }), status: "idle" as const, updatedAt: now, }; @@ -9511,9 +9657,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 67e837a35b354..ca80771112711 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: { @@ -551,6 +669,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,