Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): reject normalized year 0000 in mirrorTimestamp

Adam Eivyf1e216d312d4
diff --git a/server/services/creativeDirector/projectsLogic.js b/server/services/creativeDirector/projectsLogic.js
index 69e764d4b7..4ffcb4b77e 100644
--- a/server/services/creativeDirector/projectsLogic.js
+++ b/server/services/creativeDirector/projectsLogic.js
@@ -76,18 +76,20 @@ export function mirrorStatus(status) {
* JSONB `data` (callers stringify the full record separately), so this only
* affects the queryable mirror column.
*
- * Extended-year guard: `toISOString()` emits a plain 4-digit year (`YYYY-…`)
- * only for years 0000–9999, and a SIGNED expanded form (`±YYYYYY-…`) otherwise.
- * A normalized year outside 4-digit range (e.g. `-100000-01-01`, which
- * `Date.parse` accepts) is outside Postgres TIMESTAMPTZ range and would still
- * throw on bind — so we require the leading 4-digit form and fall back otherwise.
+ * Year-range guard: `toISOString()` emits a plain 4-digit year (`YYYY-…`) only
+ * for years 0000–9999, and a SIGNED expanded form (`±YYYYYY-…`) otherwise. We
+ * accept ONLY a 4-digit year 0001–9999 — those are all well inside Postgres
+ * TIMESTAMPTZ range. Rejected (→ fallback): the signed expanded forms
+ * (`-100000-…`, `+275760-…`, which `Date.parse` accepts but PG can't bind) AND
+ * year `0000` (Postgres has no Gregorian year zero — AD 1–99 are `0001`–`0099`,
+ * so a normalized `0000-…` would still be bind-rejected).
*/
export function mirrorTimestamp(value, fallback) {
if (typeof value === 'string') {
const ms = Date.parse(value);
if (!Number.isNaN(ms)) {
const iso = new Date(ms).toISOString();
- if (/^\d{4}-/.test(iso)) return iso;
+ if (/^\d{4}-/.test(iso) && !iso.startsWith('0000-')) return iso;
}
}
return fallback;
diff --git a/server/services/creativeDirector/projectsLogic.test.js b/server/services/creativeDirector/projectsLogic.test.js
index 50f0049505..361a73cfeb 100644
--- a/server/services/creativeDirector/projectsLogic.test.js
+++ b/server/services/creativeDirector/projectsLogic.test.js
@@ -135,6 +135,11 @@ describe('mirrorStatus / mirrorTimestamp (typed-column safety)', () => {
// TIMESTAMPTZ range → must fall back, not bind a ±YYYYYY string that throws.
expect(mirrorTimestamp('-100000-01-01T00:00:00.000Z', 'fb')).toBe('fb');
expect(mirrorTimestamp('+275760-09-13T00:00:00.000Z', 'fb')).toBe('fb');
+ // Year 0000: Date.parse accepts it and toISOString emits a 4-digit
+ // '0000-…', but Postgres has no Gregorian year zero → must fall back.
+ expect(mirrorTimestamp('0000-01-01T00:00:00.000Z', 'fb')).toBe('fb');
+ // A normal in-range year still passes.
+ expect(mirrorTimestamp('0001-01-01T00:00:00.000Z', 'fb')).toBe('0001-01-01T00:00:00.000Z');
});
});