feat: harden workflow gates and expose token usage

This commit is contained in:
2026-07-21 12:14:26 +02:00
parent 8aa1676811
commit 2ff63d371f
20 changed files with 604 additions and 53 deletions
+37
View File
@@ -42,6 +42,43 @@ test("real Pi thinking_delta becomes a dedicated activity_delta to the FE", () =
expect(seen).toEqual([{ type: "activity_delta", text: "Valuto le ambiguità" }]);
});
test("assistant message_end exposes sanitized token usage with the configured context window", () => {
const { rpc, fire } = fakeRpc();
const bridge = new SessionBridge(rpc);
const seen: any[] = [];
bridge.onClientEvent((event) => seen.push(event));
bridge.setContextWindow(200_000);
fire({
type: "message_end",
message: {
role: "assistant",
stopReason: "stop",
usage: {
input: 110_348,
cacheRead: 633_344,
cacheWrite: 12,
output: 33_652,
totalTokens: 777_356,
cost: { total: 99 },
},
content: "DO_NOT_FORWARD",
},
});
expect(seen).toEqual([{
type: "usage",
usage: {
input: 110_348,
cacheRead: 633_344,
output: 33_652,
totalTokens: 777_356,
contextWindow: 200_000,
},
}]);
expect(JSON.stringify(seen)).not.toMatch(/DO_NOT_FORWARD|cost|cacheWrite/);
});
test("assistant provider errors are sanitized and leave the turn failed", () => {
const { rpc, fire } = fakeRpc();
const bridge = new SessionBridge(rpc);