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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): clamp a future-dated proposal timestamp so a skewed record cannot report a negative wait
[._.]/ Adam Eivybe9fa4d7241d
diff --git a/server/services/layeredIntelligence/approvalFunnel.js b/server/services/layeredIntelligence/approvalFunnel.jsindex 73e1718b2f..dce8c1f774 100644--- a/server/services/layeredIntelligence/approvalFunnel.js+++ b/server/services/layeredIntelligence/approvalFunnel.js@@ -49,6 +49,19 @@ const parsedMs = (value) => {return Number.isFinite(ms) ? ms : null;};+/**+ * Clamp a timestamp at `now`. Nothing can have elapsed INTO the future, but a record's+ * timestamps are not guaranteed to be in the past: cross-peer clock skew, a repaired+ * record, or a tracker reporting in a different timezone offset can all leave a+ * `filedAt`/`outcomeAt` ahead of the local clock. Unclamped that yields a NEGATIVE+ * pending age (which `formatDuration` renders as a nonsensical "-1m") and an inflated+ * filing-to-decision latency that drags the median. Clamping reads a future-dated record+ * as "this just happened", which is the closest true statement available — and it is+ * applied only to ELAPSED-TIME math, never to the window membership test: a decision is+ * still a decision whenever it is dated.+ */+const elapsedSince = (ms, now) => Math.max(0, now - ms);+/*** Compute the approval-funnel metrics for one app's outcome records.*@@ -91,11 +104,13 @@ export function computeApprovalFunnel(outcomes = [], { now = Date.now(), windowMconst approvedInWindow = decidedInWindow.filter(({ record }) => record.outcome === 'merged');// Filing-to-decision latency. Only measurable when BOTH timestamps parse and the// decision does not precede the filing (a clock-skewed or hand-edited record);- // summarizeDurations drops the NaN/negative values for us.+ // summarizeDurations drops the NaN/negative values for us. Both ends are clamped at+ // `now` so a future-dated record cannot contribute a latency longer than the proposal+ // has existed and drag the median.const latency = (entries) => summarizeDurations(entries.map(({ record, decidedMs }) => {const filedMs = parsedMs(record.filedAt);- return filedMs === null ? NaN : decidedMs - filedMs;+ return filedMs === null ? NaN : Math.min(decidedMs, now) - Math.min(filedMs, now);}));@@ -110,7 +125,10 @@ export function computeApprovalFunnel(outcomes = [], { now = Date.now(), windowMfor (const r of pending) {const filedMs = parsedMs(r.filedAt);if (filedMs === null) { undatedPending += 1; continue; }- const ageMs = now - filedMs;+ // Clamped at zero: a future-dated filing has waited no time at all, not a negative+ // amount. Without this the oldest-wait line renders as "-1m" and a skewed record+ // could sort itself to the top of the backlog.+ const ageMs = elapsedSince(filedMs, now);if (oldestAgeMs === null || ageMs > oldestAgeMs) oldestAgeMs = ageMs;// CUMULATIVE buckets: a 9-day-old proposal counts in 1d, 3d AND 7d, so the three// numbers read as a decay curve. `>=` so an age exactly at the threshold counts —diff --git a/server/services/layeredIntelligence/approvalFunnel.test.js b/server/services/layeredIntelligence/approvalFunnel.test.jsindex c4c4991639..64713701a5 100644--- a/server/services/layeredIntelligence/approvalFunnel.test.js+++ b/server/services/layeredIntelligence/approvalFunnel.test.js@@ -136,6 +136,29 @@ describe('computeApprovalFunnel (#3120)', () => {expect(funnel.window.timeToDecision).toMatchObject({ count: 0, medianMs: null });});+ it('clamps a FUTURE-dated pending filing to a zero age, never a negative one', () => {+ // Cross-peer clock skew / a repaired record can leave filedAt ahead of the local+ // clock. An unclamped age renders as a nonsensical "-1m" wait and would sort itself+ // to the top of the backlog.+ const outcomes = [pendingRecord('from-the-future', -2 * DAY), pendingRecord('real', 3 * DAY)];+ const { pending } = computeApprovalFunnel(outcomes, { now: NOW });+ expect(pending.count).toBe(2);+ // The real 3d record still owns the oldest-wait slot; the skewed one reads as 0.+ expect(pending.oldestAgeMs).toBe(3 * DAY);+ expect(pending.byAge[delayBucketKey(1)]).toBe(1);+ expect(pending.byAge[delayBucketKey(3)]).toBe(1);+ });++ it('clamps a FUTURE-dated decision so it cannot inflate the latency median', () => {+ // A decision dated 2 days ahead of a filing 1 day ago must not report a 3-day+ // latency for a proposal that has only existed for one day.+ const outcomes = [decidedRecord('skewed-forward', 'merged', DAY, -2 * DAY)];+ const funnel = computeApprovalFunnel(outcomes, { now: NOW });+ expect(funnel.window).toMatchObject({ decided: 1, approved: 1, approvalRate: 100 });+ expect(funnel.window.timeToDecision.medianMs).toBe(DAY);+ expect(funnel.window.timeToDecision.medianMs).toBeLessThanOrEqual(DAY);+ });+it('honors an injected window span', () => {const outcomes = [decidedRecord('mid', 'merged', 6 * DAY, 5 * DAY)];expect(computeApprovalFunnel(outcomes, { now: NOW, windowMs: 3 * DAY }).window.approvalRate).toBeNull();