@@ -74,6 +74,7 @@ import {
7474import type { ProviderContinuationRequest } from "../ProviderContinuationRequests.ts";
7575import {
7676 AcpProviderCapabilitiesV2,
77+ acpPromptResponseTurnTokenUsage,
7778 acpProviderItemNativeId,
7879 acpScopedNativeId,
7980 acpCarryoverTerminalShouldClearContinuation,
@@ -108,6 +109,60 @@ const testLayer = Layer.mergeAll(NodeServices.layer, IdAllocator.layer, serverCo
108109const ACP_TEST_DRIVER = ProviderDriverKind.make("acp-test");
109110const decodeUnknownJson = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
110111
112+ describe("acpPromptResponseTurnTokenUsage", () => {
113+ it("projects ACP prompt usage into complete main-agent turn usage", () => {
114+ assert.deepEqual(
115+ acpPromptResponseTurnTokenUsage(
116+ {
117+ inputTokens: 60,
118+ outputTokens: 15,
119+ totalTokens: 75,
120+ cachedReadTokens: 40,
121+ cachedWriteTokens: 1,
122+ thoughtTokens: 4,
123+ },
124+ true,
125+ "completed",
126+ ),
127+ {
128+ usageScope: "main_agent",
129+ usageStatus: "complete",
130+ hasSubagents: true,
131+ inputTokens: 60,
132+ outputTokens: 15,
133+ cachedInputTokens: 40,
134+ cacheCreationTokens: 1,
135+ reasoningTokens: 4,
136+ },
137+ );
138+ });
139+
140+ it("returns unavailable usage when ACP omits prompt usage", () => {
141+ assert.deepEqual(acpPromptResponseTurnTokenUsage(undefined, false, "failed"), {
142+ usageScope: "main_agent",
143+ usageStatus: "unavailable",
144+ hasSubagents: false,
145+ });
146+ });
147+
148+ it("marks observed usage partial when the turn does not complete", () => {
149+ assert.deepEqual(
150+ acpPromptResponseTurnTokenUsage(
151+ { inputTokens: 60, outputTokens: 15, totalTokens: 75 },
152+ false,
153+ "interrupted",
154+ ),
155+ {
156+ usageScope: "main_agent",
157+ usageStatus: "partial",
158+ hasSubagents: false,
159+ inputTokens: 60,
160+ outputTokens: 15,
161+ },
162+ );
163+ });
164+ });
165+
111166describe("acpProjectedCommandExitCode", () => {
112167 const successOutput = { type: "Bash", exit_code: 0 };
113168 const failedOutput = { type: "Bash", exit_code: 1 };
@@ -604,6 +659,96 @@ function makeTurnInput(input: {
604659}
605660
606661describe("AcpAdapterV2", () => {
662+ it.live(
663+ "records prompt response usage on the terminal turn without carrying it into the next turn",
664+ () =>
665+ Effect.gen(function* () {
666+ const path = yield* Path.Path;
667+ const instanceId = ProviderInstanceId.make("acp-test-prompt-usage");
668+ const threadId = ThreadId.make("thread-acp-prompt-usage");
669+ let promptOrdinal = 0;
670+ const adapter = makeAcpAdapterV2({
671+ crypto: yield* Crypto.Crypto,
672+ instanceId,
673+ fileSystem: yield* FileSystem.FileSystem,
674+ idAllocator: yield* IdAllocator.IdAllocatorV2,
675+ serverConfig: yield* ServerConfig.ServerConfig,
676+ selfInvocation: yield* resolveSelfInvocation(),
677+ flavor: {
678+ driver: ACP_TEST_DRIVER,
679+ capabilities: AcpProviderCapabilitiesV2,
680+ makeRuntime: makeMockRuntime({
681+ childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
682+ mockAgentPath: yield* path.fromFileUrl(
683+ new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
684+ ),
685+ wrapRuntime: (runtime) => ({
686+ ...runtime,
687+ prompt: () => {
688+ promptOrdinal += 1;
689+ return Effect.succeed(
690+ promptOrdinal === 1
691+ ? {
692+ stopReason: "end_turn" as const,
693+ usage: { inputTokens: 60, outputTokens: 15, totalTokens: 75 },
694+ }
695+ : { stopReason: "end_turn" as const },
696+ );
697+ },
698+ }),
699+ }),
700+ },
701+ });
702+ const runtimePolicy = ProviderAdapterV2RuntimePolicy.make({
703+ runtimeMode: "full-access",
704+ interactionMode: "default",
705+ cwd: process.cwd(),
706+ });
707+ const modelSelection = { instanceId, model: "default" } as const;
708+ const runtime = yield* adapter.openSession({
709+ threadId,
710+ providerSessionId: ProviderSessionId.make("provider-session-acp-prompt-usage"),
711+ modelSelection,
712+ runtimePolicy,
713+ });
714+ const providerThread = yield* runtime.ensureThread({
715+ threadId,
716+ modelSelection,
717+ runtimePolicy,
718+ });
719+ const now = yield* DateTime.now;
720+ for (const ordinal of [1, 2]) {
721+ yield* runtime.startTurn(
722+ makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal }),
723+ );
724+ const events = Array.from(
725+ yield* runtime.events.pipe(
726+ Stream.takeUntil((event) => event.type === "turn.terminal"),
727+ Stream.runCollect,
728+ ),
729+ );
730+ const completed = events.find(
731+ (event) =>
732+ event.type === "provider_turn.updated" && event.providerTurn.status === "completed",
733+ );
734+ assert.deepEqual(
735+ completed?.type === "provider_turn.updated"
736+ ? completed.providerTurn.turnTokenUsage
737+ : null,
738+ ordinal === 1
739+ ? {
740+ usageScope: "main_agent",
741+ usageStatus: "complete",
742+ hasSubagents: false,
743+ inputTokens: 60,
744+ outputTokens: 15,
745+ }
746+ : { usageScope: "main_agent", usageStatus: "unavailable", hasSubagents: false },
747+ );
748+ }
749+ }).pipe(Effect.provide(testLayer), Effect.scoped),
750+ );
751+
607752 it.live.each(["failed", "recovered", "completed", "cancelled"] as const)(
608753 "projects Mistral retry notices and their %s outcome",
609754 (outcome) =>
0 commit comments