Fills in the data model added by the previous PR. Every adapter now reports what its subagents are actually doing: role provenance, token usage, recent activity, and per-activation records. Usage needs two merge strategies because providers report it differently. Codex sends cumulative snapshots per thread, so those merge by maximum — duplicate and late out-of-order frames then leave the total unchanged instead of inflating it. Claude reports per activation, so lifetime usage accumulates only the delta since the previous snapshot. A subagent identity is reusable, so completing an activation leaves it at "idle" rather than "completed"; the timeline row for that activation is still recorded as completed via the status mapping. A Codex subAgentActivity frame carries no model of its own, so agents registered from one were recorded with none at all — the common case for Codex-native subagents, which then showed a role and a token count but no model. They are recorded against the turn's model selection, which is what they actually run on. Three activation-lifecycle fixes in the Claude adapter, found in review: - A resume that raced past settle was pre-opened by the wake buffer, which erased the terminal status the reopen check reads. The resume then reused the finished activation instead of starting a new one, so its ordinal stopped matching reality and its usage was measured against the previous activation's totals — clamping the delta to zero and silently dropping the resumed run's tokens. The pre-open now records that it happened, and the reopen check honours it. - A replayed "waiting" frame could resurrect a settled subagent; only pending/running were guarded before. - A duplicate terminal frame re-stamped completedAt, drifting the recorded completion time. Codex already preserved it; Claude now matches. Reuse across runs is deliberately not addressed here — a subagent row still carries the run it was spawned under. That is the next PR.
133 lines
3.8 KiB
TypeScript
133 lines
3.8 KiB
TypeScript
import { describe, expect, it } from "vite-plus/test";
|
|
|
|
import {
|
|
accumulateCumulativeSubagentUsage,
|
|
appendSubagentActivity,
|
|
defaultSubagentRole,
|
|
mergeCumulativeSubagentUsage,
|
|
providerSubagentRole,
|
|
} from "./SubagentObservability.ts";
|
|
import * as DateTime from "effect/DateTime";
|
|
|
|
describe("subagent observability", () => {
|
|
it("preserves role provenance instead of using titles as roles", () => {
|
|
expect(providerSubagentRole(" reviewer ")).toEqual({
|
|
name: "reviewer",
|
|
source: "provider",
|
|
});
|
|
expect(providerSubagentRole(undefined)).toEqual(defaultSubagentRole());
|
|
});
|
|
|
|
it("accumulates activation deltas into stable lifetime usage", () => {
|
|
const first = accumulateCumulativeSubagentUsage(null, null, {
|
|
totalTokens: 100,
|
|
inputTokens: 70,
|
|
outputTokens: 30,
|
|
toolUses: 2,
|
|
});
|
|
const firstAdvanced = accumulateCumulativeSubagentUsage(
|
|
first,
|
|
{
|
|
totalTokens: 100,
|
|
inputTokens: 70,
|
|
outputTokens: 30,
|
|
toolUses: 2,
|
|
},
|
|
{
|
|
totalTokens: 140,
|
|
inputTokens: 95,
|
|
outputTokens: 45,
|
|
toolUses: 3,
|
|
},
|
|
);
|
|
const resumed = accumulateCumulativeSubagentUsage(firstAdvanced, null, {
|
|
totalTokens: 80,
|
|
inputTokens: 50,
|
|
outputTokens: 30,
|
|
toolUses: 1,
|
|
});
|
|
|
|
expect(firstAdvanced).toEqual({
|
|
totalTokens: 140,
|
|
inputTokens: 95,
|
|
outputTokens: 45,
|
|
toolUses: 3,
|
|
});
|
|
expect(resumed).toEqual({
|
|
totalTokens: 220,
|
|
inputTokens: 145,
|
|
outputTokens: 75,
|
|
toolUses: 4,
|
|
});
|
|
});
|
|
|
|
it("bounds recent activity and deduplicates adjacent updates", () => {
|
|
const now = DateTime.makeUnsafe("2026-07-26T00:00:00.000Z");
|
|
const activity = Array.from({ length: 8 }, (_, index) => `step ${index}`).reduce(
|
|
(current, summary) => appendSubagentActivity(current, summary, now),
|
|
appendSubagentActivity([], "step 0", now),
|
|
);
|
|
|
|
expect(activity).toHaveLength(6);
|
|
expect(activity.map((entry) => entry.summary)).toEqual([
|
|
"step 2",
|
|
"step 3",
|
|
"step 4",
|
|
"step 5",
|
|
"step 6",
|
|
"step 7",
|
|
]);
|
|
expect(appendSubagentActivity([], " ", now)).toEqual([]);
|
|
});
|
|
|
|
it("merges cumulative provider counters without double-counting", () => {
|
|
expect(
|
|
mergeCumulativeSubagentUsage(
|
|
{ totalTokens: 100, outputTokens: 25 },
|
|
{ totalTokens: 140, outputTokens: 20 },
|
|
),
|
|
).toEqual({ totalTokens: 140, outputTokens: 25 });
|
|
});
|
|
|
|
it("is idempotent for duplicate and late cumulative snapshots", () => {
|
|
const latest = {
|
|
totalTokens: 140,
|
|
inputTokens: 95,
|
|
outputTokens: 45,
|
|
toolUses: 3,
|
|
};
|
|
const duplicate = mergeCumulativeSubagentUsage(latest, latest);
|
|
const late = mergeCumulativeSubagentUsage(duplicate, {
|
|
totalTokens: 100,
|
|
inputTokens: 70,
|
|
outputTokens: 30,
|
|
toolUses: 2,
|
|
});
|
|
|
|
expect(duplicate).toEqual(latest);
|
|
expect(late).toEqual(latest);
|
|
expect(accumulateCumulativeSubagentUsage(latest, latest, duplicate)).toEqual(latest);
|
|
});
|
|
|
|
it("preserves omitted activation counters when they reappear", () => {
|
|
const firstActivation = { totalTokens: 100, inputTokens: 70 };
|
|
const withoutInput = mergeCumulativeSubagentUsage(firstActivation, { totalTokens: 140 });
|
|
const lifetimeWithoutInput = accumulateCumulativeSubagentUsage(
|
|
firstActivation,
|
|
firstActivation,
|
|
{
|
|
totalTokens: 140,
|
|
},
|
|
);
|
|
const restoredInput = mergeCumulativeSubagentUsage(withoutInput, {
|
|
totalTokens: 180,
|
|
inputTokens: 100,
|
|
});
|
|
|
|
expect(withoutInput).toEqual({ totalTokens: 140, inputTokens: 70 });
|
|
expect(
|
|
accumulateCumulativeSubagentUsage(lifetimeWithoutInput, withoutInput, restoredInput),
|
|
).toEqual({ totalTokens: 180, inputTokens: 100 });
|
|
});
|
|
});
|