diff --git a/pm-stack/aggregator/src/feed.ts b/pm-stack/aggregator/src/feed.ts index 6415674370..fefebc28be 100644 --- a/pm-stack/aggregator/src/feed.ts +++ b/pm-stack/aggregator/src/feed.ts @@ -191,10 +191,63 @@ function isGoalMeasurement(v: unknown): boolean { Array.isArray(g.linedUp) && Array.isArray(g.stalled) && Array.isArray(g.gaps) && + // #2417: todayFromSection dereferences every lined-up / stalled + // element, so a non-record element must fail here, at the gate — + // presence of the arrays alone let [null] through to a TypeError. + g.linedUp.every(isRecord) && + g.stalled.every(isRecord) && (typeof g.progressedSinceLastRun === "string" || Array.isArray(g.progressedSinceLastRun)) ); } +/** + * Element gates for the section's remaining arrays (#2417). The + * section gate used to check only that these arrays exist; poisoned + * elements (null, primitives, shapeless objects) then reached + * consumers that dereference them — buildAttention on tier items, + * the projects view on dependencies, the today view on actions — + * and threw TypeErrors instead of taking the designed invalid-section + * fallback. The rules stay at the depth consumers need: + * + * - Tier items: a plain record with a string `id`. Deeper fields are + * the per-item sanitizer's job (src/tier-items.ts, #1290) — a + * malformed extension drops there, never the item, so the gate + * must not reject on them. + * - Dependencies: the declared consumed shape — required string + * `task`, `status` in the DependencyRef union, and the optional + * ref/worker fields string-typed when present. + * - Actions: the four rendered string fields (at, kind, target, + * summary). + */ +function isTierElement(v: unknown): boolean { + return isRecord(v) && typeof v.id === "string"; +} + +const DEPENDENCY_STATUSES: readonly string[] = ["in-progress", "queued", "stalled", "done", "unknown"]; + +function isDependencyElement(v: unknown): boolean { + if (!isRecord(v)) return false; + return ( + typeof v.task === "string" && + typeof v.status === "string" && + DEPENDENCY_STATUSES.includes(v.status) && + (v.ref === undefined || typeof v.ref === "string") && + (v.taskRef === undefined || typeof v.taskRef === "string") && + (v.workerSessionId === undefined || typeof v.workerSessionId === "string") && + (v.workerLabel === undefined || typeof v.workerLabel === "string") + ); +} + +function isActionElement(v: unknown): boolean { + return ( + isRecord(v) && + typeof v.at === "string" && + typeof v.kind === "string" && + typeof v.target === "string" && + typeof v.summary === "string" + ); +} + /** * The section's provenance (#1110). The contract is the publisher's * object form — `{ sources: string[], runId?: string }` — which every @@ -231,11 +284,15 @@ export function orchestrationSectionProblem(v: unknown): string | null { if (!isProvenance(s.provenance)) return "provenance"; if (typeof s.tiers !== "object" || s.tiers === null) return "tiers"; if (!Array.isArray(s.tiers.needsDrew)) return "tiers.needsDrew"; + if (!s.tiers.needsDrew.every(isTierElement)) return "tiers.needsDrew (per-item shape)"; if (!Array.isArray(s.tiers.needsOrchestrator)) return "tiers.needsOrchestrator"; + if (!s.tiers.needsOrchestrator.every(isTierElement)) return "tiers.needsOrchestrator (per-item shape)"; if (!Array.isArray(s.dependencies)) return "dependencies"; + if (!s.dependencies.every(isDependencyElement)) return "dependencies (per-dependency shape)"; if (!Array.isArray(s.goals)) return "goals"; if (!s.goals.every(isGoalMeasurement)) return "goals (per-goal shape)"; if (!Array.isArray(s.actions)) return "actions"; + if (!s.actions.every(isActionElement)) return "actions (per-action shape)"; return null; } diff --git a/pm-stack/aggregator/test/orchestration.test.ts b/pm-stack/aggregator/test/orchestration.test.ts index 9e114e1995..8ed6f3d1cf 100644 --- a/pm-stack/aggregator/test/orchestration.test.ts +++ b/pm-stack/aggregator/test/orchestration.test.ts @@ -124,6 +124,80 @@ describe("orchestration section validation", () => { expect(() => todayFromSection(minimal)).not.toThrow(); }); + // #2417: the gate checked array presence only, so poisoned elements + // (null / primitives / shapeless objects) passed into consumers and + // threw TypeErrors. Every array's elements are now validated at this + // boundary; a poisoned section must reject, normalize to null, and + // diagnose as invalid with a stable failed-check name. + test("rejects poisoned tier elements in both tier arrays", () => { + for (const bad of [null, 42, {}]) { + const drew = { ...SECTION, tiers: { ...SECTION.tiers, needsDrew: [bad] } }; + expect(isOrchestrationSection(drew)).toBe(false); + expect(orchestrationSectionProblem(drew)).toBe("tiers.needsDrew (per-item shape)"); + expect(normalizeOrchestrationSection(drew)).toBeNull(); + const orch = { ...SECTION, tiers: { ...SECTION.tiers, needsOrchestrator: [bad] } }; + expect(isOrchestrationSection(orch)).toBe(false); + expect(orchestrationSectionProblem(orch)).toBe("tiers.needsOrchestrator (per-item shape)"); + } + }); + + test("rejects non-record elements in goal linedUp and stalled arrays", () => { + const goal = SECTION.goals[0]!; + for (const field of ["linedUp", "stalled"] as const) { + const malformed = { ...SECTION, goals: [{ ...goal, [field]: [null] }] }; + expect(isOrchestrationSection(malformed)).toBe(false); + expect(orchestrationSectionProblem(malformed)).toBe("goals (per-goal shape)"); + expect(normalizeOrchestrationSection(malformed)).toBeNull(); + } + const nullGoal = { ...SECTION, goals: [null] }; + expect(isOrchestrationSection(nullGoal)).toBe(false); + }); + + test("rejects poisoned dependency elements and bad dependency shapes", () => { + const nullDep = { ...SECTION, dependencies: [null] }; + expect(isOrchestrationSection(nullDep)).toBe(false); + expect(orchestrationSectionProblem(nullDep)).toBe("dependencies (per-dependency shape)"); + const noStatus = { ...SECTION, dependencies: [{ task: "Write the thing" }] }; + expect(isOrchestrationSection(noStatus)).toBe(false); + const badStatus = { ...SECTION, dependencies: [{ task: "Write the thing", status: "bogus" }] }; + expect(isOrchestrationSection(badStatus)).toBe(false); + const badRef = { + ...SECTION, + dependencies: [{ task: "Write the thing", status: "queued", ref: 42 }], + }; + expect(isOrchestrationSection(badRef)).toBe(false); + const good = { + ...SECTION, + dependencies: [{ task: "Write the thing", status: "queued", ref: "developing-today/code#1" }], + }; + expect(isOrchestrationSection(good)).toBe(true); + }); + + test("rejects poisoned action elements and actions missing rendered fields", () => { + const nullAction = { ...SECTION, actions: [null] }; + expect(isOrchestrationSection(nullAction)).toBe(false); + expect(orchestrationSectionProblem(nullAction)).toBe("actions (per-action shape)"); + const noSummary = { + ...SECTION, + actions: [{ at: "2026-10-25T17:20:00.000Z", kind: "nudge", target: "task 1" }], + }; + expect(isOrchestrationSection(noSummary)).toBe(false); + const good = { + ...SECTION, + actions: [ + { at: "2026-10-25T17:20:00.000Z", kind: "nudge", target: "task 1", summary: "Nudged it." }, + ], + }; + expect(isOrchestrationSection(good)).toBe(true); + }); + + test("a poisoned section diagnoses as invalid, never absent or live", () => { + const poisoned = { ...SECTION, tiers: { ...SECTION.tiers, needsDrew: [null] } }; + const diagnosis = diagnoseOrchestrationSection(poisoned); + expect(diagnosis.state).toBe("invalid"); + expect(diagnosis.failedChecks).toEqual(["tiers.needsDrew (per-item shape)"]); + }); + test("a fleet feed carrying the section still validates as a feed", () => { const feed = { version: 1,