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
18 changes: 11 additions & 7 deletions pm-stack/aggregator/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
);
}

Expand Down
2 changes: 1 addition & 1 deletion pm-stack/aggregator/test/feed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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" }],
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));
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 @@ -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);
Expand All @@ -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);
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 @@ -17,6 +17,10 @@ function rec(partial: Partial<SessionRecord> & { 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;
}
Expand All @@ -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);
});

Expand All @@ -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"]);
Expand Down
9 changes: 5 additions & 4 deletions pm-stack/aggregator/test/registry-hygiene-2408.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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]);
Expand Down
112 changes: 112 additions & 0 deletions pm-stack/aggregator/test/registry-hygiene-4284.test.ts
Original file line number Diff line number Diff line change
@@ -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<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 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([]);
});
});
4 changes: 2 additions & 2 deletions pm-stack/aggregator/test/registry-taskrefs-2485.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ function rec(partial: Partial<SessionRecord> & { 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);
Expand Down
2 changes: 1 addition & 1 deletion pm-stack/aggregator/test/watchdog-priming.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading