Skip to content

Commit 261f4a8

Browse files
committed
fix(server): keep usage snapshots status-neutral
1 parent 38d79b9 commit 261f4a8

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.activity.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe("runtimeEventToActivities task progress", () => {
4949
]);
5050
const usagePayload = usageActivities[0]?.payload as Record<string, unknown> | undefined;
5151
expect(usagePayload?.typedUsage).toEqual({ totalTokens: 73_700_000 });
52+
expect(usagePayload?.usageSnapshot).toBe(true);
5253
});
5354

5455
it("splits combined progress and usage into their independent snapshots", () => {
@@ -77,6 +78,7 @@ describe("runtimeEventToActivities task progress", () => {
7778
expect(progressPayload.status).toBe("running");
7879
expect(progressPayload).not.toHaveProperty("typedUsage");
7980
expect(usagePayload.typedUsage).toEqual({ totalTokens: 4_200, toolUses: 7 });
81+
expect(usagePayload.usageSnapshot).toBe(true);
8082
expect(usagePayload).not.toHaveProperty("status");
8183
});
8284
});

‎apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ export function runtimeEventToActivities(
629629
taskId: event.payload.taskId,
630630
...title,
631631
...identityLinkage,
632+
usageSnapshot: true,
632633
typedUsage: event.payload.typedUsage,
633634
},
634635
turnId: toTurnId(event.turnId) ?? null,

‎packages/client-runtime/src/state/subagentRuntime.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,34 @@ describe("foldSubagentActivities", () => {
186186
expect(agents[0]!.usage).toEqual({ totalTokens: 900, inputTokens: 700 });
187187
});
188188

189+
it("usage snapshots enrich an existing agent without changing its status", () => {
190+
const [agent] = fold([
191+
activity("task.started", { taskId: "usage-waiting", taskType: "local_agent" }),
192+
activity("task.progress", { taskId: "usage-waiting", status: "waiting" }),
193+
activity("task.progress", {
194+
taskId: "usage-waiting",
195+
usageSnapshot: true,
196+
typedUsage: { totalTokens: 1_200 },
197+
}),
198+
]);
199+
200+
expect(agent?.status).toBe("waiting");
201+
expect(agent?.usage?.totalTokens).toBe(1_200);
202+
});
203+
204+
it("a retained usage snapshot can still reconstruct a running agent", () => {
205+
const [agent] = fold([
206+
activity("task.progress", {
207+
taskId: "usage-only",
208+
usageSnapshot: true,
209+
typedUsage: { totalTokens: 800 },
210+
}),
211+
]);
212+
213+
expect(agent?.status).toBe("running");
214+
expect(agent?.usage?.totalTokens).toBe(800);
215+
});
216+
189217
it("partial terminal usage preserves known breakdown fields", () => {
190218
const agents = fold([
191219
activity("task.started", { taskId: "task-6", taskType: "local_agent" }),

‎packages/client-runtime/src/state/subagentRuntime.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,14 +504,19 @@ export function foldSubagentActivities(
504504
// Membership is sticky per taskId: rows after the first (terminal
505505
// rows often carry only taskId+status, no marker fields) inherit the
506506
// first row's classification instead of being re-judged.
507-
if (!agents.has(taskId) && isBackgroundTaskActivity(payload)) break;
507+
const existed = agents.has(taskId);
508+
if (!existed && isBackgroundTaskActivity(payload)) break;
508509
const agent = getOrCreate(agents, taskId, payload, at);
509510
fillMetadata(agent, payload);
510511
if (agent.activationCount === 0) agent.activationCount = 1;
511512
const explicitStatus = asRuntimeStatus(payload.status);
512513
if (explicitStatus) {
513514
applyStatus(agent, explicitStatus, at);
514-
} else if (!isTerminalSubagentStatus(agent.status) && agent.status !== "idle") {
515+
} else if (
516+
(payload.usageSnapshot !== true || !existed) &&
517+
!isTerminalSubagentStatus(agent.status) &&
518+
agent.status !== "idle"
519+
) {
515520
applyStatus(agent, "running", at);
516521
}
517522
const summary = asString(payload.summary);

0 commit comments

Comments
 (0)