From 8d4e5d5fa7570947ca6e04f20c7ac06b9836c945 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 16:47:21 -0600 Subject: [PATCH] fix(aggregator): reject session records without taskRefs (#4284) --- pm-stack/aggregator/src/registry.ts | 18 +-- pm-stack/aggregator/test/feed.test.ts | 2 +- .../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 | 9 +- .../test/registry-hygiene-4284.test.ts | 112 ++++++++++++++++++ .../test/registry-taskrefs-2485.test.ts | 4 +- .../aggregator/test/watchdog-priming.test.ts | 2 +- 9 files changed, 142 insertions(+), 21 deletions(-) create mode 100644 pm-stack/aggregator/test/registry-hygiene-4284.test.ts diff --git a/pm-stack/aggregator/src/registry.ts b/pm-stack/aggregator/src/registry.ts index 9cab929f53..43e03aebe6 100644 --- a/pm-stack/aggregator/src/registry.ts +++ b/pm-stack/aggregator/src/registry.ts @@ -7,7 +7,7 @@ // // Loose v1 by design: identity is (source, session id); task linkage is // whatever refs the session's title/PR/branch or launch record names. -import { parseRef, type SessionFeedEntry, type SessionRecord, refsInText } from "./model.ts"; +import { parseRef, refsInText, type SessionFeedEntry, type SessionRecord } from "./model.ts"; function isValidTaskRef(value: unknown): value is string { if (typeof value !== "string") return false; @@ -176,12 +176,16 @@ export function isSessionRecord(v: unknown): v is SessionRecord { typeof r.label === "string" && (r.lastSeenAt === undefined || typeof r.lastSeenAt === "string") && (r.startedAt === undefined || typeof r.startedAt === "string") && - // #2408/#2485: mergeRecords spreads taskRefs as an array, and rendering - // parses each ref as owner/repo#number. The validator owns the rule, per - // the #2347/#2376 precedent above: taskRefs must be absent or an array - // of parseable refs with safe-integer issue numbers; malformed records - // are rejected, not coerced. - (r.taskRefs === undefined || (Array.isArray(r.taskRefs) && r.taskRefs.every(isValidTaskRef))) + // #2408/#2485/#4284: mergeRecords spreads taskRefs as an array, and + // querying/rendering dereferences it unconditionally (spread in + // matchesText, .some in the ?task= filter and the item-page linked- + // sessions filter, .flatMap in sessionRecordLine). The validator owns + // the rule, per the #2347/#2376 precedent above: SessionRecord.taskRefs + // is required, so taskRefs must be a present array of parseable refs + // with safe-integer issue numbers; absent or malformed records are + // rejected, not coerced. + Array.isArray(r.taskRefs) && + r.taskRefs.every(isValidTaskRef) ); } diff --git a/pm-stack/aggregator/test/feed.test.ts b/pm-stack/aggregator/test/feed.test.ts index 26943293fb..68ef344829 100644 --- a/pm-stack/aggregator/test/feed.test.ts +++ b/pm-stack/aggregator/test/feed.test.ts @@ -551,7 +551,7 @@ describe("feed subscriptions sanitization (#2446)", () => { }); describe("feed registry sanitization (#2539)", () => { - const RECORD = { id: "ses-1", source: "claude-code", label: "Muse run" }; + const RECORD = { id: "ses-1", source: "claude-code", label: "Muse run", taskRefs: [] }; const feedWithRegistry = (registry: unknown) => ({ version: 1, generatedAt: "2026-10-01T00:00:00Z", diff --git a/pm-stack/aggregator/test/health-priming.test.ts b/pm-stack/aggregator/test/health-priming.test.ts index 94b1d5d707..6e70e4d2dc 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" }], + registry: [{ id: "ses-health-1", source: "opencode-sandbox", label: "Health probe session", 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 201964fb70..155e3975b3 100644 --- a/pm-stack/aggregator/test/orchestration.test.ts +++ b/pm-stack/aggregator/test/orchestration.test.ts @@ -495,7 +495,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" }], + registry: [{ id: "ses-diag-1", source: "opencode-sandbox", label: "Diag session", taskRefs: [] }], }); const records = await src.registry(); expect(records.length).toBe(1); @@ -521,8 +521,8 @@ describe("live source diagnostics + provenance detail (#1139/#1138)", () => { const clean = await sourceWithFeed({ registry: [ - { id: "ses-muse-1", source: "claude-code", label: "Muse launch" }, - { id: "ses-opencode-1", source: "opencode-sandbox", label: "OpenCode session" }, + { id: "ses-muse-1", source: "claude-code", label: "Muse launch", taskRefs: [] }, + { id: "ses-opencode-1", source: "opencode-sandbox", label: "OpenCode session", 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 56f0a5746b..301e9ced0c 100644 --- a/pm-stack/aggregator/test/registry-hygiene-2376.test.ts +++ b/pm-stack/aggregator/test/registry-hygiene-2376.test.ts @@ -17,6 +17,10 @@ function rec(partial: Partial & { id: string }): SessionRecord { agentLabel: "Muse", parentId: null, status: "succeeded", + // #4284: SessionRecord.taskRefs is required, so the shared minimal + // fixture carries it (coordinated with the #3525 validator work, which + // owns the remaining required fields). + taskRefs: [], ...partial, } as SessionRecord; } @@ -37,7 +41,7 @@ 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" })).toBe(true); + expect(isSessionRecord({ id: "a", source: "s", label: "l", taskRefs: [] })).toBe(true); expect(isSessionRecord(older)).toBe(true); }); @@ -63,7 +67,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" }, + { id: "ses_no_ts", source: "claude-code", label: "No timestamps", 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 d41876f382..016695efe0 100644 --- a/pm-stack/aggregator/test/registry-hygiene-2408.test.ts +++ b/pm-stack/aggregator/test/registry-hygiene-2408.test.ts @@ -29,9 +29,10 @@ const valid = rec({ lastSeenAt: "2026-11-01T00:00:00Z", }); -describe("isSessionRecord taskRefs hygiene (#2408)", () => { - test("accepts absent, empty, and string-array taskRefs", () => { - expect(isSessionRecord({ id: "a", source: "s", label: "l" })).toBe(true); +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({ ...valid, taskRefs: [] })).toBe(true); expect(isSessionRecord(valid)).toBe(true); expect(isSessionRecord({ ...valid, taskRefs: ["developing-today/code#123", "developing-today/code#2408"] })).toBe( @@ -80,7 +81,7 @@ describe("poisoned taskRefs cannot reach the mergeRecords spread (#2408)", () => ]); }); - test("missing taskRefs stays valid and coalesces to the other side", () => { + test("mergeRecords still coalesces when one side lacks taskRefs (the #4284 gate now rejects such records before they reach the merge)", () => { const prev = rec({ id: "ses_b", taskRefs: ["developing-today/code#7"] }); const incoming = rec({ id: "ses_b" }); const merged = mergeRecords([prev], [incoming]); diff --git a/pm-stack/aggregator/test/registry-hygiene-4284.test.ts b/pm-stack/aggregator/test/registry-hygiene-4284.test.ts new file mode 100644 index 0000000000..db0f995305 --- /dev/null +++ b/pm-stack/aggregator/test/registry-hygiene-4284.test.ts @@ -0,0 +1,112 @@ +import { describe, expect, test } from "bun:test"; +import { sanitizeRegistry } from "../src/feed.ts"; +import type { SessionRecord } from "../src/model.ts"; +import { isSessionRecord, mergeRecords, recordFromLaunch, recordFromSessionEntry } from "../src/registry.ts"; +import { parseRegistryQuery, queryRegistry } from "../src/registry-view.ts"; +import { sessionRecordLine } from "../src/render.ts"; + +// #4284: SessionRecord.taskRefs is required, but isSessionRecord admitted a +// record with taskRefs absent. mergeRecords preserved undefined on the +// new-key path, and the admitted record then threw in queryRegistry for +// ?q= (spread of undefined) and ?task= (.some on undefined) as well as in +// sessionRecordLine (.flatMap on undefined) and the item-page linked- +// sessions filter (.some on undefined). The shared admission gate now +// rejects absent taskRefs, so the poisoned shape can no longer reach the +// query/render path through either untrusted admission path (the durable +// store file and the fleet feed registry section, both filtered with +// isSessionRecord in sources/live.ts). + +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 noRefs = { + id: "ses_no_refs", + source: "claude-code", + label: "No taskRefs", + agentId: "claude-code", + agentLabel: "Muse", + parentId: null, + status: "succeeded", + startedAt: "2026-11-01T00:00:00Z", + lastSeenAt: "2026-11-02T00:00:00Z", +}; + +describe("absent taskRefs is rejected at the shared gate (#4284)", () => { + test("gate rejects an otherwise-well-formed record without taskRefs", () => { + expect(isSessionRecord(noRefs)).toBe(false); + }); + + test("gate still accepts [] and valid refs", () => { + expect(isSessionRecord(rec({ id: "empty" }))).toBe(true); + expect(isSessionRecord(rec({ id: "linked", taskRefs: ["developing-today/code#4284"] }))).toBe(true); + }); +}); + +describe("a taskRefs-less record drops individually through admission (#4284)", () => { + test("store path: the bad record is filtered while well-formed neighbors survive", () => { + const raw: unknown[] = [ + rec({ id: "ses_good" }), + noRefs, + rec({ id: "ses_linked", taskRefs: ["developing-today/code#4284"] }), + ]; + // Same filter the durable store load applies (sources/live.ts). + const admitted = raw.filter(isSessionRecord); + expect(admitted.map((r) => (r as SessionRecord).id)).toEqual(["ses_good", "ses_linked"]); + const merged = mergeRecords([], admitted as SessionRecord[]); + expect(merged.map((r) => r.id)).toEqual(["ses_good", "ses_linked"]); + for (const r of merged) expect(Array.isArray(r.taskRefs)).toBe(true); + }); + + test("feed path: the bad registry entry is skipped while well-formed entries merge", () => { + // Feed sanitization uses the same gate the live registry later applies. + const sanitized = sanitizeRegistry([rec({ id: "ses_feed_good" }), noRefs]); + expect(sanitized.rows?.map((r) => r.id)).toEqual(["ses_feed_good"]); + expect(sanitized.dropped).toBe(1); + const merged = mergeRecords([], sanitized.rows ?? []); + expect(merged).toHaveLength(1); + expect(merged[0].taskRefs).toEqual([]); + }); +}); + +describe("admitted records no longer crash query/render (#4284)", () => { + test("?q=, ?task=, sessionRecordLine, and the item-page session filter do not throw", () => { + const raw: unknown[] = [rec({ id: "ses_good", label: "probe session" }), noRefs]; + const admitted = raw.filter(isSessionRecord) as SessionRecord[]; + expect(() => { + queryRegistry(admitted, parseRegistryQuery(new URLSearchParams("q=probe&window=all"))); + queryRegistry(admitted, parseRegistryQuery(new URLSearchParams("task=code&window=all"))); + for (const r of admitted) sessionRecordLine(r); + // Same filter the item page applies to linked sessions (server.ts). + admitted.filter((r) => r.taskRefs.some((ref) => ["developing-today/code#4284"].includes(ref))); + }).not.toThrow(); + expect(queryRegistry(admitted, parseRegistryQuery(new URLSearchParams("q=probe&window=all"))).matched).toBe(1); + }); +}); + +describe("both producers still emit gate-accepted records (#4284)", () => { + test("recordFromSessionEntry and recordFromLaunch pass the gate", () => { + const fromEntry = recordFromSessionEntry({ + sessionId: "ses_live", + source: "opencode-sandbox", + title: "Working developing-today/code#4284", + lastActivityAt: "2026-11-02T00:00:00Z", + }); + expect(isSessionRecord(fromEntry)).toBe(true); + expect(Array.isArray(fromEntry.taskRefs)).toBe(true); + const fromLaunch = recordFromLaunch({ id: "launch-1", label: "Launch" }); + expect(isSessionRecord(fromLaunch)).toBe(true); + expect(fromLaunch.taskRefs).toEqual([]); + }); +}); diff --git a/pm-stack/aggregator/test/registry-taskrefs-2485.test.ts b/pm-stack/aggregator/test/registry-taskrefs-2485.test.ts index 9407a97834..85929bef49 100644 --- a/pm-stack/aggregator/test/registry-taskrefs-2485.test.ts +++ b/pm-stack/aggregator/test/registry-taskrefs-2485.test.ts @@ -21,8 +21,8 @@ function rec(partial: Partial & { id: string }): SessionRecord { } describe("registry task-ref validation (#2485)", () => { - test("isSessionRecord accepts absent and well-formed refs, rejects malformed elements", () => { - expect(isSessionRecord({ id: "sparse", source: "claude-code", label: "Sparse" })).toBe(true); + test("isSessionRecord rejects absent refs, accepts well-formed refs, rejects malformed elements", () => { + expect(isSessionRecord({ id: "sparse", source: "claude-code", label: "Sparse" })).toBe(false); expect(isSessionRecord(rec({ id: "good", taskRefs: ["developing-today/code#2485"] }))).toBe(true); expect(isSessionRecord(rec({ id: "short", taskRefs: ["code#2480"] }))).toBe(false); expect(isSessionRecord(rec({ id: "number", taskRefs: [42] as unknown as string[] }))).toBe(false); diff --git a/pm-stack/aggregator/test/watchdog-priming.test.ts b/pm-stack/aggregator/test/watchdog-priming.test.ts index fb1ae61950..c04e5b2963 100644 --- a/pm-stack/aggregator/test/watchdog-priming.test.ts +++ b/pm-stack/aggregator/test/watchdog-priming.test.ts @@ -37,7 +37,7 @@ beforeAll(async () => { goals: [], actions: [], }, - registry: [{ id: "ses-watchdog-1", source: "opencode-sandbox", label: "Watchdog probe session" }], + registry: [{ id: "ses-watchdog-1", source: "opencode-sandbox", label: "Watchdog probe session", taskRefs: [] }], }; const feedFile = join(dir, "fleet-feed.json"); await writeFile(feedFile, JSON.stringify(feed));