Skip to content

Commit 02cbd55

Browse files
author
simonvanlaak
committed
fix(server): record ACP prompt response turn usage
1 parent 0080e80 commit 02cbd55

2 files changed

Lines changed: 174 additions & 0 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import {
7474
import type { ProviderContinuationRequest } from "../ProviderContinuationRequests.ts";
7575
import {
7676
AcpProviderCapabilitiesV2,
77+
acpPromptResponseTurnTokenUsage,
7778
acpProviderItemNativeId,
7879
acpScopedNativeId,
7980
acpCarryoverTerminalShouldClearContinuation,
@@ -108,6 +109,60 @@ const testLayer = Layer.mergeAll(NodeServices.layer, IdAllocator.layer, serverCo
108109
const ACP_TEST_DRIVER = ProviderDriverKind.make("acp-test");
109110
const 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+
111166
describe("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

606661
describe("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) =>

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
type RuntimeRequestId,
3131
type ThreadTokenUsageSnapshot,
3232
type ThreadId,
33+
type TurnTokenUsage,
3334
} from "@t3tools/contracts";
3435
import { modelSelectionsEqual } from "@t3tools/shared/model";
3536
import { type SelfInvocation, selfInvocationArgs } from "@t3tools/shared/nodeRuntime";
@@ -210,6 +211,26 @@ export interface AcpAdapterV2ExtensionContext {
210211
readonly lastProposedPlanMarkdown: Effect.Effect<string | undefined>;
211212
}
212213

214+
export function acpPromptResponseTurnTokenUsage(
215+
usage: EffectAcpSchema.Usage | null | undefined,
216+
hasSubagents: boolean,
217+
terminalStatus: OrchestrationV2ProviderTurn["status"],
218+
): TurnTokenUsage {
219+
if (usage == null) {
220+
return { usageScope: "main_agent", usageStatus: "unavailable", hasSubagents };
221+
}
222+
return {
223+
usageScope: "main_agent",
224+
usageStatus: terminalStatus === "completed" ? "complete" : "partial",
225+
hasSubagents,
226+
inputTokens: usage.inputTokens,
227+
outputTokens: usage.outputTokens,
228+
...(usage.cachedReadTokens == null ? {} : { cachedInputTokens: usage.cachedReadTokens }),
229+
...(usage.cachedWriteTokens == null ? {} : { cacheCreationTokens: usage.cachedWriteTokens }),
230+
...(usage.thoughtTokens == null ? {} : { reasoningTokens: usage.thoughtTokens }),
231+
};
232+
}
233+
213234
export interface AcpAdapterV2Flavor {
214235
/** Interprets provider-specific prompt errors before they cross into orchestration. */
215236
readonly promptFailure?: (cause: unknown) => OrchestrationV2ProviderFailure;
@@ -1167,6 +1188,7 @@ interface ActiveAcpTurn {
11671188
* complete this; settled-soft classification ORs it with `promptSettled`.
11681189
*/
11691190
readonly promptWireSettled: Deferred.Deferred<void, never>;
1191+
promptUsage: EffectAcpSchema.Usage | undefined;
11701192
backgroundFinalizeGeneration: number;
11711193
}
11721194

@@ -6344,6 +6366,11 @@ export function makeAcpAdapterV2(
63446366
status,
63456367
startedAt: context.startedAt,
63466368
completedAt,
6369+
turnTokenUsage: acpPromptResponseTurnTokenUsage(
6370+
context.promptUsage,
6371+
context.subagents.size > 0,
6372+
status,
6373+
),
63476374
});
63486375

63496376
const terminalizeOpenRunOwnedItems = Effect.fnUntraced(function* (
@@ -6857,6 +6884,7 @@ export function makeAcpAdapterV2(
68576884
promptSettled: false,
68586885
promptSettledStatus: null,
68596886
promptWireSettled,
6887+
promptUsage: undefined,
68606888
backgroundFinalizeGeneration: 0,
68616889
};
68626890
const carryover = yield* Ref.getAndSet(carryoverSubagents, null);
@@ -7007,6 +7035,7 @@ export function makeAcpAdapterV2(
70077035
promptGeneration,
70087036
Effect.gen(function* () {
70097037
if (context.finalized) return;
7038+
context.promptUsage = result.usage ?? undefined;
70107039
const status =
70117040
result.stopReason === "cancelled"
70127041
? context.interrupted

0 commit comments

Comments
 (0)