Skip to content

Commit 6f5852b

Browse files
author
Ben Smith
committed
refactor(server): address review round 2 on the narrowed usage path
Move the registry wiring note next to the spread it documents, drop the stale baseline/delta vocabulary from tests and comments, and collapse the prompt tap to one guarded sync per review.
1 parent 511f15f commit 6f5852b

3 files changed

Lines changed: 79 additions & 91 deletions

File tree

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

Lines changed: 71 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const ACP_TEST_DRIVER = ProviderDriverKind.make("acp-test");
121121
const decodeUnknownJson = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
122122

123123
describe("acpPerTurnPromptUsage", () => {
124-
it("maps a per-turn sample straight through without any baseline", () => {
124+
it("maps a per-turn sample straight onto TurnTokenUsage", () => {
125125
assert.deepEqual(
126126
acpPerTurnPromptUsage(
127127
{
@@ -190,7 +190,7 @@ describe("acpPerTurnPromptUsage", () => {
190190
});
191191

192192
it("caps cache writes at the input left over after cache reads", () => {
193-
// PR review case: each counter fits under input alone, but together they exceed it.
193+
// Each counter fits under input alone, but together they exceed it.
194194
assert.deepEqual(
195195
acpPerTurnPromptUsage(
196196
{
@@ -1073,8 +1073,7 @@ describe("AcpAdapterV2", () => {
10731073
(event) =>
10741074
event.type === "provider_turn.updated" && event.providerTurn.status === "completed",
10751075
);
1076-
// Per-turn semantics map each response's own sample onto its turn:
1077-
// turn 2 reports 2453 input tokens, not the 21-token delta.
1076+
// Per-turn: each response's own sample maps straight onto its turn.
10781077
assert.deepEqual(
10791078
completed?.type === "provider_turn.updated"
10801079
? completed.providerTurn.turnTokenUsage
@@ -1251,77 +1250,75 @@ describe("AcpAdapterV2", () => {
12511250
}).pipe(Effect.provide(layerTest), Effect.scoped),
12521251
);
12531252

1254-
it.live(
1255-
"keeps usage unavailable for undeclared semantics even when the agent reports usage",
1256-
() =>
1257-
Effect.gen(function* () {
1258-
const path = yield* Path.Path;
1259-
const instanceId = ProviderInstanceId.make("acp-test-usage-undeclared");
1260-
const threadId = ThreadId.make("thread-acp-usage-undeclared");
1261-
const adapter = yield* makeAcpAdapterV2({
1262-
instanceId,
1263-
selfInvocation: yield* resolveSelfInvocation(),
1264-
flavor: {
1265-
driver: ACP_TEST_DRIVER,
1266-
capabilities: AcpProviderCapabilitiesV2,
1267-
makeRuntime: makeMockRuntime({
1268-
childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
1269-
mockAgentPath: yield* path.fromFileUrl(
1270-
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
1271-
),
1272-
wrapRuntime: (runtime) => ({
1273-
...runtime,
1274-
prompt: () =>
1275-
Effect.succeed({
1276-
stopReason: "end_turn" as const,
1277-
usage: { inputTokens: 60, outputTokens: 15, totalTokens: 75 },
1278-
}),
1279-
}),
1280-
}),
1281-
},
1282-
});
1283-
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1284-
runtimeMode: "full-access",
1285-
interactionMode: "default",
1286-
cwd: process.cwd(),
1287-
});
1288-
const modelSelection = { instanceId, model: "default" } as const;
1289-
const runtime = yield* adapter.openSession({
1290-
threadId,
1291-
providerSessionId: ProviderSessionId.make("provider-session-acp-usage-undeclared"),
1292-
modelSelection,
1293-
runtimePolicy,
1294-
});
1295-
const providerThread = yield* runtime.ensureThread({
1296-
threadId,
1297-
modelSelection,
1298-
runtimePolicy,
1299-
});
1300-
const now = yield* DateTime.now;
1301-
for (const ordinal of [1, 2]) {
1302-
yield* runtime.startTurn(
1303-
makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal }),
1304-
);
1305-
const events = Array.from(
1306-
yield* runtime.events.pipe(
1307-
Stream.takeUntil((event) => event.type === "turn.terminal"),
1308-
Stream.runCollect,
1253+
it.live("keeps usage unavailable for undeclared agents even when they report usage", () =>
1254+
Effect.gen(function* () {
1255+
const path = yield* Path.Path;
1256+
const instanceId = ProviderInstanceId.make("acp-test-usage-undeclared");
1257+
const threadId = ThreadId.make("thread-acp-usage-undeclared");
1258+
const adapter = yield* makeAcpAdapterV2({
1259+
instanceId,
1260+
selfInvocation: yield* resolveSelfInvocation(),
1261+
flavor: {
1262+
driver: ACP_TEST_DRIVER,
1263+
capabilities: AcpProviderCapabilitiesV2,
1264+
makeRuntime: makeMockRuntime({
1265+
childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
1266+
mockAgentPath: yield* path.fromFileUrl(
1267+
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
13091268
),
1310-
);
1311-
const completed = events.find(
1312-
(event) =>
1313-
event.type === "provider_turn.updated" && event.providerTurn.status === "completed",
1314-
);
1315-
// Usage semantics are unverified for an undeclared agent, so its
1316-
// counters are never trusted, on any turn.
1317-
assert.deepEqual(
1318-
completed?.type === "provider_turn.updated"
1319-
? completed.providerTurn.turnTokenUsage
1320-
: null,
1321-
{ usageScope: "main_agent", usageStatus: "unavailable", hasSubagents: false },
1322-
);
1323-
}
1324-
}).pipe(Effect.provide(layerTest), Effect.scoped),
1269+
wrapRuntime: (runtime) => ({
1270+
...runtime,
1271+
prompt: () =>
1272+
Effect.succeed({
1273+
stopReason: "end_turn" as const,
1274+
usage: { inputTokens: 60, outputTokens: 15, totalTokens: 75 },
1275+
}),
1276+
}),
1277+
}),
1278+
},
1279+
});
1280+
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1281+
runtimeMode: "full-access",
1282+
interactionMode: "default",
1283+
cwd: process.cwd(),
1284+
});
1285+
const modelSelection = { instanceId, model: "default" } as const;
1286+
const runtime = yield* adapter.openSession({
1287+
threadId,
1288+
providerSessionId: ProviderSessionId.make("provider-session-acp-usage-undeclared"),
1289+
modelSelection,
1290+
runtimePolicy,
1291+
});
1292+
const providerThread = yield* runtime.ensureThread({
1293+
threadId,
1294+
modelSelection,
1295+
runtimePolicy,
1296+
});
1297+
const now = yield* DateTime.now;
1298+
for (const ordinal of [1, 2]) {
1299+
yield* runtime.startTurn(
1300+
makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal }),
1301+
);
1302+
const events = Array.from(
1303+
yield* runtime.events.pipe(
1304+
Stream.takeUntil((event) => event.type === "turn.terminal"),
1305+
Stream.runCollect,
1306+
),
1307+
);
1308+
const completed = events.find(
1309+
(event) =>
1310+
event.type === "provider_turn.updated" && event.providerTurn.status === "completed",
1311+
);
1312+
// Usage semantics are unverified for an undeclared agent, so its
1313+
// counters are never trusted, on any turn.
1314+
assert.deepEqual(
1315+
completed?.type === "provider_turn.updated"
1316+
? completed.providerTurn.turnTokenUsage
1317+
: null,
1318+
{ usageScope: "main_agent", usageStatus: "unavailable", hasSubagents: false },
1319+
);
1320+
}
1321+
}).pipe(Effect.provide(layerTest), Effect.scoped),
13251322
);
13261323

13271324
// Factory Droid 0.243.0 sends an optional text companion for every AskUser choice.

‎packages/provider-acp-registry/src/server/adapter.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,6 @@ export function acpRegistryPromptFailure(agentId: string, cause: unknown) {
129129
});
130130
}
131131

132-
/**
133-
* PromptResponse.usage reads per-turn for glm-acp-agent (Z.ai GLM), the one
134-
* registry agent a live probe has verified; every other agent stays
135-
* undeclared, so turn usage stays unavailable until probed.
136-
*/
137132
function makeAcpRegistryRuntime(
138133
options: AcpRegistryAdapterV2Options,
139134
catalog: AcpRegistrySupport.AcpRegistryCatalog["Service"],
@@ -221,6 +216,7 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu
221216
...(registryAgentId === "factory-droid"
222217
? { extractElicitationForm: extractDroidElicitationForm }
223218
: {}),
219+
// glm-acp-agent is the one agent whose per-turn usage a live probe has verified.
224220
...(registryAgentId === "glm-acp-agent" ? { reportsPerTurnPromptUsage: true } : {}),
225221
...(registryAgentId === "mistral-vibe"
226222
? { registerExtensions: registerMistralVibeAcpExtensions }

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7611,19 +7611,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
76117611
// Wire settlement precedes the completion callback's permit request so
76127612
// settled-soft classification can observe the native return even when
76137613
// the completion fiber has not yet set promptSettled under the permit.
7614+
// The sample is captured only before finalize starts: a response
7615+
// landing inside finalize's window must not flip a settled turn.
76147616
Effect.tap((result) =>
7615-
(flavor.reportsPerTurnPromptUsage === true
7616-
? // Per-turn: the sample already covers exactly this prompt,
7617-
// so it maps straight onto the turn unless it settled late.
7618-
Effect.sync(() => {
7619-
if (!context.finalized) {
7620-
context.promptUsage = result.usage ?? undefined;
7621-
}
7622-
})
7623-
: // Undeclared: usage reading is unverified, so the
7624-
// response's counters are not captured.
7625-
Effect.void
7626-
).pipe(
7617+
Effect.sync(() => {
7618+
if (flavor.reportsPerTurnPromptUsage === true && !context.finalized) {
7619+
context.promptUsage = result.usage ?? undefined;
7620+
}
7621+
}).pipe(
76277622
Effect.andThen(
76287623
Deferred.succeed(context.promptWireSettled, undefined).pipe(Effect.asVoid),
76297624
),

0 commit comments

Comments
 (0)