From 0ee1acce887082aa7d6ed64354c9cf8ed3f995f2 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 14:03:00 -0600 Subject: [PATCH] fix(aggregator): isSessionRecord validates status/agentId/agentLabel/tokensTotal (#3525) --- pm-stack/aggregator/src/registry.ts | 14 ++ .../aggregator/test/health-priming.test.ts | 2 +- .../aggregator/test/orchestration.test.ts | 6 +- .../test/registry-hygiene-2376.test.ts | 8 +- .../test/registry-hygiene-2408.test.ts | 2 +- .../test/registry-hygiene-3525.test.ts | 133 ++++++++++++++++++ .../test/registry-isodate-5078.test.ts | 2 +- 7 files changed, 159 insertions(+), 8 deletions(-) create mode 100644 pm-stack/aggregator/test/registry-hygiene-3525.test.ts diff --git a/pm-stack/aggregator/src/registry.ts b/pm-stack/aggregator/src/registry.ts index 6de0132d1a..29c852ee47 100644 --- a/pm-stack/aggregator/src/registry.ts +++ b/pm-stack/aggregator/src/registry.ts @@ -181,6 +181,20 @@ export function isSessionRecord(v: unknown): v is SessionRecord { typeof r.id === "string" && typeof r.source === "string" && typeof r.label === "string" && + // #3525: render.ts dereferences status (sessionRecordIsLive calls + // r.status.toLowerCase()), agentId/agentLabel (identitiesFromRecords + // interpolates agentLabel) and tokensTotal (renderSessions sums it, + // sessionRecordLine formats it with toLocaleString()) unconditionally. + // SessionRecord requires string status/agentId/agentLabel, so a record + // missing any of them is poisoned and rejected, not coerced (the + // #2347/#2376 precedent). tokensTotal is optional on SessionRecord, so + // absent is fine; when present it must be a finite number >= 0 — a + // string/Infinity/negative/object value poisons the page token total. + typeof r.status === "string" && + typeof r.agentId === "string" && + typeof r.agentLabel === "string" && + (r.tokensTotal === undefined || + (typeof r.tokensTotal === "number" && Number.isFinite(r.tokensTotal) && r.tokensTotal >= 0)) && (r.lastSeenAt === undefined || isIsoDate(r.lastSeenAt)) && (r.startedAt === undefined || isIsoDate(r.startedAt)) && // #2408/#2485/#4284: mergeRecords spreads taskRefs as an array, and diff --git a/pm-stack/aggregator/test/health-priming.test.ts b/pm-stack/aggregator/test/health-priming.test.ts index 6e70e4d2dc..006035db69 100644 --- a/pm-stack/aggregator/test/health-priming.test.ts +++ b/pm-stack/aggregator/test/health-priming.test.ts @@ -52,7 +52,7 @@ beforeAll(async () => { goals: [], actions: [], }, - registry: [{ id: "ses-health-1", source: "opencode-sandbox", label: "Health probe session", taskRefs: [] }], + registry: [{ id: "ses-health-1", source: "opencode-sandbox", label: "Health probe session", status: "idle", agentId: "opencode:main", agentLabel: "OpenCode · main (sandbox)", taskRefs: [] }], }; const feedFile = join(dir, "fleet-feed.json"); await writeFile(feedFile, JSON.stringify(feed)); diff --git a/pm-stack/aggregator/test/orchestration.test.ts b/pm-stack/aggregator/test/orchestration.test.ts index 954468092e..7f595baaa7 100644 --- a/pm-stack/aggregator/test/orchestration.test.ts +++ b/pm-stack/aggregator/test/orchestration.test.ts @@ -633,7 +633,7 @@ describe("live source diagnostics + provenance detail (#1139/#1138)", () => { test("registry facet counts the MERGED total, with its noun (#1138)", async () => { const src = await sourceWithFeed({ orchestration: liveSection(), - registry: [{ id: "ses-diag-1", source: "opencode-sandbox", label: "Diag session", taskRefs: [] }], + registry: [{ id: "ses-diag-1", source: "opencode-sandbox", label: "Diag session", status: "idle", agentId: "opencode:main", agentLabel: "OpenCode · main (sandbox)", taskRefs: [] }], }); const records = await src.registry(); expect(records.length).toBe(1); @@ -659,8 +659,8 @@ describe("live source diagnostics + provenance detail (#1139/#1138)", () => { const clean = await sourceWithFeed({ registry: [ - { id: "ses-muse-1", source: "claude-code", label: "Muse launch", taskRefs: [] }, - { id: "ses-opencode-1", source: "opencode-sandbox", label: "OpenCode session", taskRefs: [] }, + { id: "ses-muse-1", source: "claude-code", label: "Muse launch", status: "launched", agentId: "claude-code", agentLabel: "Muse", taskRefs: [] }, + { id: "ses-opencode-1", source: "opencode-sandbox", label: "OpenCode session", status: "idle", agentId: "opencode:main", agentLabel: "OpenCode · main (sandbox)", taskRefs: [] }, ], }); expect(await clean.registry()).toHaveLength(2); diff --git a/pm-stack/aggregator/test/registry-hygiene-2376.test.ts b/pm-stack/aggregator/test/registry-hygiene-2376.test.ts index 301e9ced0c..97e76bef17 100644 --- a/pm-stack/aggregator/test/registry-hygiene-2376.test.ts +++ b/pm-stack/aggregator/test/registry-hygiene-2376.test.ts @@ -41,7 +41,11 @@ const older = rec({ describe("isSessionRecord timestamp hygiene (#2376)", () => { test("accepts string or absent timestamps", () => { expect(isSessionRecord(valid)).toBe(true); - expect(isSessionRecord({ id: "a", source: "s", label: "l", taskRefs: [] })).toBe(true); + // #3525: the minimal fixture now carries the required status/agentId/ + // agentLabel fields — a status-less record is rejected by the gate. + expect( + isSessionRecord({ id: "a", source: "s", label: "l", status: "succeeded", agentId: "claude-code", agentLabel: "Muse", taskRefs: [] }), + ).toBe(true); expect(isSessionRecord(older)).toBe(true); }); @@ -67,7 +71,7 @@ describe("poisoned records cannot reach the mergeRecords sort (#2376)", () => { { id: "ses_poison_num", source: "claude-code", label: "Poison", lastSeenAt: 1730000000 }, { id: "ses_poison_bool", source: "claude-code", label: "Poison", lastSeenAt: true }, older, - { id: "ses_no_ts", source: "claude-code", label: "No timestamps", taskRefs: [] }, + { id: "ses_no_ts", source: "claude-code", label: "No timestamps", status: "succeeded", agentId: "claude-code", agentLabel: "Muse", taskRefs: [] }, ]; const admitted = raw.filter(isSessionRecord); expect(admitted.map((r) => r.id)).toEqual(["ses_valid", "ses_older", "ses_no_ts"]); diff --git a/pm-stack/aggregator/test/registry-hygiene-2408.test.ts b/pm-stack/aggregator/test/registry-hygiene-2408.test.ts index 016695efe0..090be7e0f6 100644 --- a/pm-stack/aggregator/test/registry-hygiene-2408.test.ts +++ b/pm-stack/aggregator/test/registry-hygiene-2408.test.ts @@ -32,7 +32,7 @@ const valid = rec({ describe("isSessionRecord taskRefs hygiene (#2408, #4284)", () => { test("accepts empty and string-array taskRefs, rejects absent taskRefs", () => { expect(isSessionRecord({ id: "a", source: "s", label: "l" })).toBe(false); - expect(isSessionRecord({ id: "a", source: "s", label: "l", taskRefs: [] })).toBe(true); + expect(isSessionRecord({ id: "a", source: "s", label: "l", status: "succeeded", agentId: "claude-code", agentLabel: "Muse", taskRefs: [] })).toBe(true); expect(isSessionRecord({ ...valid, taskRefs: [] })).toBe(true); expect(isSessionRecord(valid)).toBe(true); expect(isSessionRecord({ ...valid, taskRefs: ["developing-today/code#123", "developing-today/code#2408"] })).toBe( diff --git a/pm-stack/aggregator/test/registry-hygiene-3525.test.ts b/pm-stack/aggregator/test/registry-hygiene-3525.test.ts new file mode 100644 index 0000000000..52d657e749 --- /dev/null +++ b/pm-stack/aggregator/test/registry-hygiene-3525.test.ts @@ -0,0 +1,133 @@ +import { describe, expect, test } from "bun:test"; +import { sanitizeRegistry } from "../src/feed.ts"; +import type { SessionRecord } from "../src/model.ts"; +import { identitiesFromRecords, isSessionRecord, mergeRecords, parseLaunchRecords, recordFromLaunch, recordFromSessionEntry } from "../src/registry.ts"; +import { sessionRecordIsLive } from "../src/render.ts"; + +// #3525: isSessionRecord admitted records missing status/agentId/agentLabel +// and records whose tokensTotal was a string, Infinity, negative or an +// object. Consumers dereference all of them unconditionally: +// sessionRecordIsLive calls r.status.toLowerCase(), identitiesFromRecords +// interpolates r.agentLabel, renderSessions sums r.tokensTotal and +// sessionRecordLine formats it with toLocaleString(). One poisoned record +// in the durable store, the feed registry section or a hand-edited launch +// JSONL line crashed the sessions page or poisoned its token total. The +// shared gate now owns the rule (the #2347/#2376 precedent): status, +// agentId and agentLabel must be strings; tokensTotal is optional but, +// when present, must be a finite number >= 0. Reject, never coerce. + +function rec(partial: Partial & { id: string }): SessionRecord { + return { + source: "claude-code", + label: partial.id, + agentId: "claude-code", + agentLabel: "Muse", + parentId: null, + status: "succeeded", + startedAt: "2026-11-01T00:00:00Z", + lastSeenAt: "2026-11-02T00:00:00Z", + taskRefs: [], + ...partial, + } as SessionRecord; +} + +const valid = rec({ id: "ses_valid", tokensTotal: 1200 }); + +function without(key: string, base: Record): Record { + const copy = { ...base }; + delete copy[key]; + return copy; +} + +const validRaw: Record = { + id: "ses_raw", + source: "claude-code", + label: "Raw", + agentId: "claude-code", + agentLabel: "Muse", + parentId: null, + status: "succeeded", + taskRefs: [], +}; + +describe("status / agent identity are required strings at the gate (#3525)", () => { + test("gate accepts a well-formed record, with or without tokensTotal", () => { + expect(isSessionRecord(valid)).toBe(true); + expect(isSessionRecord(rec({ id: "ses_no_tokens" }))).toBe(true); + expect(isSessionRecord(rec({ id: "ses_zero_tokens", tokensTotal: 0 }))).toBe(true); + }); + + test("gate rejects missing or non-string status", () => { + expect(isSessionRecord(without("status", validRaw))).toBe(false); + expect(isSessionRecord({ ...validRaw, status: 42 })).toBe(false); + expect(isSessionRecord({ ...validRaw, status: null })).toBe(false); + }); + + test("gate rejects missing or non-string agentId / agentLabel", () => { + expect(isSessionRecord(without("agentId", validRaw))).toBe(false); + expect(isSessionRecord(without("agentLabel", validRaw))).toBe(false); + expect(isSessionRecord({ ...validRaw, agentId: 7 })).toBe(false); + expect(isSessionRecord({ ...validRaw, agentLabel: { name: "x" } })).toBe(false); + }); +}); + +describe("tokensTotal, when present, must be a finite number >= 0 (#3525)", () => { + test("gate rejects string, Infinity, NaN, negative and object tokensTotal", () => { + expect(isSessionRecord({ ...validRaw, tokensTotal: "5000" })).toBe(false); + const inf = JSON.parse('{"tokensTotal":1e999}') as { tokensTotal: number }; + expect(inf.tokensTotal).toBe(Infinity); + expect(isSessionRecord({ ...validRaw, tokensTotal: inf.tokensTotal })).toBe(false); + expect(isSessionRecord({ ...validRaw, tokensTotal: NaN })).toBe(false); + expect(isSessionRecord({ ...validRaw, tokensTotal: -500 })).toBe(false); + expect(isSessionRecord({ ...validRaw, tokensTotal: { x: 1 } })).toBe(false); + expect(isSessionRecord({ ...validRaw, tokensTotal: null })).toBe(false); + }); +}); + +describe("real producers still pass the gate unchanged (#3525)", () => { + test("recordFromSessionEntry output passes, with and without tokens", () => { + const entry = { sessionId: "ses_feed", source: "claude-code" as const, lastActivityAt: "2026-11-02T00:00:00Z", title: "Feed session" }; + expect(isSessionRecord(recordFromSessionEntry(entry, 42_000))).toBe(true); + expect(isSessionRecord(recordFromSessionEntry(entry))).toBe(true); + }); + + test("recordFromLaunch output passes (status defaults to launched)", () => { + expect(isSessionRecord(recordFromLaunch({ id: "launch_ok" }))).toBe(true); + expect(isSessionRecord(recordFromLaunch({ id: "launch_tok", tokensTotal: 9000 }))).toBe(true); + }); +}); + +describe("a poisoned record drops individually on every admission path (#3525)", () => { + test("store path: filter drops the poisoned records, survivors render", () => { + const raw: unknown[] = [ + valid, + without("status", validRaw), + { ...validRaw, id: "ses_str_tokens", tokensTotal: "5000" }, + rec({ id: "ses_good_2", tokensTotal: 300 }), + ]; + const admitted = raw.filter(isSessionRecord) as SessionRecord[]; + expect(admitted.map((r) => r.id)).toEqual(["ses_valid", "ses_good_2"]); + const merged = mergeRecords([], admitted); + for (const r of merged) expect(() => sessionRecordIsLive(r)).not.toThrow(); + const total = merged.reduce((sum, r) => sum + (r.tokensTotal ?? 0), 0); + expect(total).toBe(1500); + expect(identitiesFromRecords(merged).some((i) => i.label.includes("undefined"))).toBe(false); + }); + + test("feed path: sanitizeRegistry drops poisoned entries, keeps well-formed ones", () => { + const sanitized = sanitizeRegistry([valid, { ...validRaw, id: "ses_inf", tokensTotal: Infinity }, rec({ id: "ses_feed_good" })]); + expect(sanitized.rows?.map((r) => r.id)).toEqual(["ses_valid", "ses_feed_good"]); + expect(sanitized.dropped).toBe(1); + }); + + test("launch path: parseLaunchRecords drops non-string status and string tokensTotal lines", () => { + const jsonl = [ + JSON.stringify({ id: "launch_ok", label: "OK", tokensTotal: 100 }), + JSON.stringify({ id: "launch_bad_status", label: "Bad", status: 42 }), + JSON.stringify({ id: "launch_bad_tokens", label: "Bad", tokensTotal: "9000" }), + '{"id":"launch_inf","label":"Inf","tokensTotal":1e999}', + ].join("\n"); + const records = parseLaunchRecords(jsonl); + expect(records.map((r) => r.id)).toEqual(["launch_ok"]); + }); +}); diff --git a/pm-stack/aggregator/test/registry-isodate-5078.test.ts b/pm-stack/aggregator/test/registry-isodate-5078.test.ts index b3e80f0481..b8b87b6621 100644 --- a/pm-stack/aggregator/test/registry-isodate-5078.test.ts +++ b/pm-stack/aggregator/test/registry-isodate-5078.test.ts @@ -52,7 +52,7 @@ describe("isSessionRecord timestamp validity (#5078)", () => { }); test("accepts absent timestamps", () => { - expect(isSessionRecord({ id: "a", source: "s", label: "l", taskRefs: [] })).toBe(true); + expect(isSessionRecord({ id: "a", source: "s", label: "l", status: "succeeded", agentId: "claude-code", agentLabel: "Muse", taskRefs: [] })).toBe(true); const { lastSeenAt: _ls, startedAt: _st, ...absent } = valid; expect(isSessionRecord(absent)).toBe(true); });