Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(delta): reject invalid calendar dates in since

masashiono0611b6c5672f9f0d
diff --git a/docs/architecture/key-files/graph-and-facts.md b/docs/architecture/key-files/graph-and-facts.md
index d6cea2fee8..a18f7ec694 100644
--- a/docs/architecture/key-files/graph-and-facts.md
+++ b/docs/architecture/key-files/graph-and-facts.md
@@ -75,7 +75,7 @@ rejection of serve-only flags before a brain opens.
- `src/core/extract-timeline-from-meetings.ts` — normalizes canonical incoming and pack-owned outgoing `attended` edges into meeting/person roles before timeline fan-out. SQL requires a live person and live meeting (including legacy meeting notes); existing private-meeting filtering, source opt-in and source-qualified deduplication still apply. Dual-engine tests in `test/extract-timeline-attendance.test.ts` use an empty gazetteer so a body-mention fallback cannot conceal a broken attendance consumer.
-- `src/core/ops/facts.ts` — shared fact/memory operations, including `recall`. Recall keeps source/visibility and fact filters before per-arm candidate limits. Its default/explicit `facts_first` packing preserves the frozen memory-verb contract, including the positive sub-one budget quirk. Optional `query_first` packs the ranked page prefix first only with a nonblank query and positive finite budget; floor-zero and exhausted remainders explicitly return empty arms rather than call the unbounded zero-budget packer. Neither arm skips oversized prefix items or truncates. No-query and inactive-budget paths keep legacy behavior. Only policy-supplied calls add `budget_packing` effective-policy/reason and candidate/kept/dropped/estimated-used accounting. Counter sums match the frozen fields. Do not change the global packer or fact relevance to implement this policy. `test/recall-budget-policy.test.ts` pins numeric boundaries, compatibility, candidate ordering, filters, source/private/safe-projection behavior and shared transport validation.
+- `src/core/ops/facts.ts` — shared fact/memory operations, including `recall` and the `delta` cursor. Recall keeps source/visibility and fact filters before per-arm candidate limits. Its default/explicit `facts_first` packing preserves the frozen memory-verb contract, including the positive sub-one budget quirk. Optional `query_first` packs the ranked page prefix first only with a nonblank query and positive finite budget; floor-zero and exhausted remainders explicitly return empty arms rather than call the unbounded zero-budget packer. Neither arm skips oversized prefix items or truncates. No-query and inactive-budget paths keep legacy behavior. Only policy-supplied calls add `budget_packing` effective-policy/reason and candidate/kept/dropped/estimated-used accounting. Counter sums match the frozen fields. Do not change the global packer or fact relevance to implement this policy. Delta rejects calendar-invalid ISO dates before normalization across fractional-second forms, while preserving valid six-digit microsecond cursors. `test/recall-budget-policy.test.ts` pins recall behavior; `test/ambient-recall.test.ts` pins delta cursor validation.
- `src/core/markdown-code.ts` — `stripCodeBlocks` masks fenced and inline code while preserving every CR/LF and UTF-16 code-unit offset. Citation paragraphs and later link references retain their original boundaries, including after an unclosed fence. `parseTimelineEntries` in `src/core/link-extraction.ts` groups the optional dash with its trailing whitespace so leading whitespace does not trigger overlapping repetition. Existing date and delimiter rules remain unchanged. Standalone backtick and tilde fences require valid closing markers; historical inline triple-backtick spans remain masked. Pinned through real FS/DB link and timeline consumers by `test/timeline-parser-regression.test.ts`; its adversarial whitespace workload runs in a timeout-bounded child process.
diff --git a/src/core/ops/facts.ts b/src/core/ops/facts.ts
index 5ad05bd2a4..a812512d11 100644
--- a/src/core/ops/facts.ts
+++ b/src/core/ops/facts.ts
@@ -754,14 +754,14 @@ const delta: Operation = {
// canonical microsecond shape `listPages` projects (`next_cursor.since`
// passed back) is kept verbatim: rounding it through a JS Date would
// re-select every same-millisecond row on the resumed wake.
- // The verbatim passthrough must round-trip: a calendar-invalid but
- // Date.parse-able value (2026-02-31T…) would otherwise reach the
- // ::timestamptz cast raw and surface as an engine error, not invalid_params.
- if (
- rawSince !== null &&
- /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z$/.test(rawSince) &&
- new Date(rawSince).toISOString().slice(0, 19) !== rawSince.slice(0, 19)
- ) {
+ // Date.parse normalizes dates such as Feb 31 into March. Check the ISO
+ // calendar date before normalization, including cursors without the
+ // microsecond passthrough shape.
+ const isoDate = rawSince?.match(/^(\d{4})-(\d{2})-(\d{2})T/);
+ const [year, month, day] = isoDate?.slice(1).map(Number) ?? [];
+ const leapYear = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
+ const daysInMonth = [31, leapYear ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
+ if (rawSince !== null && isoDate && (month < 1 || month > 12 || day < 1 || day > daysInMonth[month - 1])) {
throw verbError(
'invalid_params',
`delta: since is not a valid ISO 8601 calendar timestamp: "${rawSince.slice(0, 60)}"`,
diff --git a/test/ambient-recall.test.ts b/test/ambient-recall.test.ts
index bc0323e4ea..6c4798fe6d 100644
--- a/test/ambient-recall.test.ts
+++ b/test/ambient-recall.test.ts
@@ -225,7 +225,13 @@ describe('delta cursor lifecycle', () => {
// Wave review: a calendar-INVALID value in the microsecond passthrough shape
// is Date.parse-able (JS rolls Feb 31 to Mar 3) but would reach the
// ::timestamptz cast raw → engine error. It is invalid_params instead.
- await expect(call(del, ctxFor({ remote: false }), { since: '2026-02-31T00:00:00.000000Z' })).rejects.toThrow(/calendar|ISO/i);
+ for (const since of [
+ '2026-02-31T00:00:00Z',
+ '2026-02-31T00:00:00.000Z',
+ '2026-02-31T00:00:00.000000Z',
+ ]) {
+ await expect(call(del, ctxFor({ remote: false }), { since })).rejects.toThrow(/calendar|ISO/i);
+ }
// ...while a valid microsecond cursor still passes through verbatim.
const r3 = await call(del, ctxFor({ remote: false }), { since: '2026-02-28T00:00:00.000100Z' });
expect(r3.since).toBe('2026-02-28T00:00:00.000100Z');