Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pm-stack/aggregator/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pm-stack/aggregator/test/health-priming.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 3 additions & 3 deletions pm-stack/aggregator/test/orchestration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions pm-stack/aggregator/test/registry-hygiene-2376.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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"]);
Expand Down
2 changes: 1 addition & 1 deletion pm-stack/aggregator/test/registry-hygiene-2408.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
133 changes: 133 additions & 0 deletions pm-stack/aggregator/test/registry-hygiene-3525.test.ts
Original file line number Diff line number Diff line change
@@ -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<SessionRecord> & { 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<string, unknown>): Record<string, unknown> {
const copy = { ...base };
delete copy[key];
return copy;
}

const validRaw: Record<string, unknown> = {
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"]);
});
});
2 changes: 1 addition & 1 deletion pm-stack/aggregator/test/registry-isodate-5078.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down