Skip to content

Commit a6a6c10

Browse files
committed
fix(subagents): roll announce token stats over to m
1 parent e10c8e1 commit a6a6c10

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/agents/subagent-announce-output.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ import { afterEach, describe, expect, it, vi } from "vitest";
22
import {
33
testing,
44
applySubagentWaitOutcome,
5+
buildCompactAnnounceStatsLine,
56
buildChildCompletionFindings,
67
readSubagentOutput,
78
} from "./subagent-announce-output.js";
89

910
type CallGateway = typeof import("../gateway/call.js").callGateway;
11+
type GetRuntimeConfig = typeof import("./subagent-announce.runtime.js").getRuntimeConfig;
12+
type ReadSessionEntry = typeof import("./subagent-announce.runtime.js").readSessionEntry;
1013
type ReadSessionMessagesAsync =
1114
typeof import("./subagent-announce.runtime.js").readSessionMessagesAsync;
15+
type ResolveAgentIdFromSessionKey =
16+
typeof import("./subagent-announce.runtime.js").resolveAgentIdFromSessionKey;
17+
type ResolveStorePath = typeof import("./subagent-announce.runtime.js").resolveStorePath;
1218

1319
function installOutputDeps(params: {
1420
messages: Array<unknown>;
@@ -53,6 +59,31 @@ function sessionsYieldTurn(message = "Waiting for subagent completion.") {
5359
];
5460
}
5561

62+
describe("buildCompactAnnounceStatsLine", () => {
63+
afterEach(() => {
64+
testing.setDepsForTest();
65+
});
66+
67+
it("rolls one-decimal thousand token stats over to the million unit", async () => {
68+
testing.setDepsForTest({
69+
getRuntimeConfig: (() => ({ session: { store: "memory" } })) as GetRuntimeConfig,
70+
readSessionEntry: (() => ({
71+
inputTokens: 999_999,
72+
outputTokens: 0,
73+
totalTokens: 999_999,
74+
})) as ReadSessionEntry,
75+
resolveAgentIdFromSessionKey: (() => "main") as ResolveAgentIdFromSessionKey,
76+
resolveStorePath: (() => "/tmp/openclaw-session-store") as ResolveStorePath,
77+
});
78+
79+
await expect(
80+
buildCompactAnnounceStatsLine({
81+
sessionKey: "agent:main:subagent:child",
82+
}),
83+
).resolves.toBe("Stats: runtime n/a • tokens 1.0m (in 1.0m / out 0)");
84+
});
85+
});
86+
5687
describe("readSubagentOutput", () => {
5788
afterEach(() => {
5889
testing.setDepsForTest();

src/agents/subagent-announce-output.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,13 @@ function formatTokenCount(value?: number) {
511511
return `${(value / 1_000_000).toFixed(1)}m`;
512512
}
513513
if (value >= 1_000) {
514-
return `${(value / 1_000).toFixed(1)}k`;
514+
const formattedThousands = (value / 1_000).toFixed(1);
515+
// Keep the compact stats unit scheme stable when one-decimal rounding
516+
// reaches the next unit, e.g. 999_999 -> 1000.0k.
517+
if (Number(formattedThousands) >= 1_000) {
518+
return `${(value / 1_000_000).toFixed(1)}m`;
519+
}
520+
return `${formattedThousands}k`;
515521
}
516522
return String(Math.round(value));
517523
}

0 commit comments

Comments
 (0)