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
10 changes: 9 additions & 1 deletion pm-stack/aggregator/src/tier-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* shape and stays untouched.
*/
import type { DashAction, NewAction } from "./actions.ts";
import { parseIsoDate } from "./validate.ts";
import type {
DelegationMode,
OrchestrationSection,
Expand Down Expand Up @@ -66,7 +67,14 @@ function optionalBounded(v: unknown, max: number): string | undefined {
}

function isIso(v: unknown): v is string {
return typeof v === "string" && v.includes("T") && !Number.isNaN(Date.parse(v));
// #3396: Date.parse alone normalizes impossible calendar days
// (2026-02-30 -> Mar 2) instead of refusing them, so a delegation
// timestamp could name a day that never existed — and proposalExpired
// then clocked the 48h TTL from that phantom day. parseIsoDate (the
// strict in-package primitive validate.ts already enforces at ingest)
// re-checks the calendar day; the existing shape gates stay, so
// accepted formats are unchanged apart from the impossible-date rule.
return typeof v === "string" && v.includes("T") && !Number.isNaN(Date.parse(v)) && parseIsoDate(v) !== null;
}

function sanitizeOptions(raw: unknown): TierItemOption[] | undefined {
Expand Down
99 changes: 99 additions & 0 deletions pm-stack/aggregator/test/tier-items-is-iso-3396.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// #3396: tier-items isIso must refuse impossible calendar dates.
// Date.parse normalizes 2026-02-30 to Mar 2 instead of failing, so
// sanitizeDelegation used to keep delegation.updatedAt, proposal.at
// and unable.at carrying days that never existed — and proposalExpired
// clocked the 48h proposal TTL from the phantom day. The sanitizer now
// re-checks the calendar day with validate.ts parseIsoDate strictness.
// Fail-soft contract is unchanged: a refused timestamp drops the whole
// delegation, never the item.
import { describe, expect, test } from "bun:test";
import type { TierItem } from "../src/model.ts";
import { proposalExpired, sanitizeTierItemExtensions } from "../src/tier-items.ts";

const BASE: TierItem = {
id: "developing-today/code#3396",
kind: "decision",
title: "Impossible-date delegation timestamps",
source: "backlog",
reason: "A scope decision only the user can make.",
since: "2026-10-26T10:00:00.000Z",
lastSeenAt: "2026-10-27T03:00:00.000Z",
ref: "developing-today/code#3396",
};

const VALID = "2026-10-27T02:00:00.000Z";
const IMPOSSIBLE = ["2026-02-30T02:00:00.000Z", "2026-02-29T02:00:00.000Z", "2026-04-31T02:00:00.000Z"];

function rawWith(delegation: Record<string, unknown>): TierItem {
return { ...BASE, whyUser: "decision", delegation } as unknown as TierItem;
}

function proposalDelegation(at: string, updatedAt: string = VALID): Record<string, unknown> {
return {
mode: "first-pass",
status: "proposal-ready",
updatedAt,
runId: "orch-2026-10-27T02:00:00Z",
proposal: { summary: "Recommend CSV", at },
};
}

function unableDelegation(at: string, updatedAt: string = VALID): Record<string, unknown> {
return {
mode: "user",
status: "unable",
updatedAt,
unable: { reason: "No access", whatItTried: ["retried"], whatItNeeds: "a token", at },
};
}

describe("isIso impossible calendar dates (#3396)", () => {
test("impossible updatedAt drops the delegation, never the item", () => {
for (const at of IMPOSSIBLE) {
const out = sanitizeTierItemExtensions(rawWith({ mode: "user", status: "idle", updatedAt: at }));
expect(out.delegation).toBeUndefined();
expect(out.id).toBe(BASE.id);
expect(out.title).toBe(BASE.title);
}
});

test("impossible proposal.at drops the delegation", () => {
for (const at of IMPOSSIBLE) {
const out = sanitizeTierItemExtensions(rawWith(proposalDelegation(at)));
expect(out.delegation).toBeUndefined();
expect(out.id).toBe(BASE.id);
}
});

test("impossible unable.at drops the delegation", () => {
for (const at of IMPOSSIBLE) {
const out = sanitizeTierItemExtensions(rawWith(unableDelegation(at)));
expect(out.delegation).toBeUndefined();
expect(out.id).toBe(BASE.id);
}
});

test("valid timestamps still pass: leap day, offset-bearing, fractional-second", () => {
for (const at of ["2024-02-29T02:00:00.000Z", "2026-03-01T12:00:00+02:00", "2026-03-01T12:00:00.123Z"]) {
const out = sanitizeTierItemExtensions(rawWith({ mode: "user", status: "idle", updatedAt: at }));
expect(out.delegation?.updatedAt).toBe(at);
}
const proposal = sanitizeTierItemExtensions(rawWith(proposalDelegation("2026-03-01T12:00:00+02:00")));
expect(proposal.delegation?.proposal?.at).toBe("2026-03-01T12:00:00+02:00");
});

test("proposalExpired TTL consequence: an impossible proposal.at no longer clocks the TTL", () => {
// Before the fix this delegation survived sanitization and
// proposalExpired read the phantom Feb-30 (parsed as Mar 2) as
// unexpired well past a Feb-28 control. Now the delegation drops
// at sanitize time, so there is no live proposal to expire.
const out = sanitizeTierItemExtensions(rawWith(proposalDelegation("2026-02-30T00:00:00.000Z", "2026-02-27T00:00:00.000Z")));
expect(out.delegation).toBeUndefined();
expect(proposalExpired(out.delegation, Date.parse("2026-03-10T00:00:00.000Z"))).toBe(false);
// A valid old proposal still expires on schedule.
const live = sanitizeTierItemExtensions(rawWith(proposalDelegation("2026-02-27T00:00:00.000Z", "2026-02-27T00:00:00.000Z")));
expect(live.delegation).toBeDefined();
expect(proposalExpired(live.delegation, Date.parse("2026-03-10T00:00:00.000Z"))).toBe(true);
expect(proposalExpired(live.delegation, Date.parse("2026-02-27T12:00:00.000Z"))).toBe(false);
});
});
Loading