Skip to content

feat(pm-stack): freshness reconciliation + push-health surfaces (#3976) - #9193

Merged
dezren39 merged 3 commits into
mainfrom
queue/8755-3976-freshness-health
Nov 13, 2026
Merged

dezren39 merged 3 commits into
mainfrom
queue/8755-3976-freshness-health

Conversation

@dezren39

@dezren39 dezren39 commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Closes the implementation scope of #3976 (task 8755): anti-entropy + honest push-health for event-driven freshness, on top of the merged #3977 journal (PR #5720) and #3978 SSE delivery (PR #8241).

What lands

5-minute reconciliation with synthetic invalidation — src/freshness-reconcile.ts

  • FreshnessReconciler runs on the 5-minute design cadence (boot pass + interval in server.ts, live mode).
  • Each pass reads every wired canonical source's currently-accepted revision — the exact eventId a source-event notice for that revision carries — and compares it with the journal. A revision with no matching notice gets a synthetic invalidation published under the revision's own eventId with reason: "reconcile", so the late real notice dedupes against the repair instead of double-invalidating.
  • Wired reader: the fleet feed — the collector's notice identity recomputed from the file on disk (fleet-feed:<generatedAt>:<sha256>, mirroring scripts/collect-fleet-feed.ts). Further sources plug in through CanonicalRevisionReader without changing the loop.
  • A synthetic notice the journal only accepts as pending leaves the gap open and the reconcile unsuccessful; a source whose canonical read fails is unavailable, never treated as unchanged; a journal cursor moving backwards is counted as a reset, never silently re-baselined.

Push health on /health, /api/health, and the health page's freshness chips — src/health.ts, src/server.ts, src/render.ts

  • push: state up | degraded | down, detail, last event, last successful reconcile, last attempt, journal cursor, per-source rows (state, last event, age, detection lag, notice count, missing-notice flag), and gap counts (open / repaired / resets + last reset).
  • Push connectivity and source freshness stay separate, per the issue: a quiet source renders silent, not an outage; an unreadable journal is down (and degrades the health view), never a healthy guess; no assembled view in fixtures mode renders as null, never invented. The existing 15-minute feed stale threshold (render chip) is untouched, as is the 5-minute collector fallback.

SSE-down browser fallback — src/assets/freshness-client.js, policy in src/freshness-fallback.ts

  • The client rides /freshness/stream; while the stream is down it polls /api/freshness-events.json on the collector's 5-minute cadence and refreshes through the existing dashrefresh fragment reread — never a page reload, so view state survives — and stops polling the moment the stream reopens. The cadence/resume policy is a pure module with unit tests; the asset implements the same constants.

Tests

New: test/freshness-reconcile-3976.test.ts, test/freshness-fallback-3976.test.ts — the issue's five scenarios (dropped event repaired, producer outage/recovery, journal loss/reset, quiet-but-healthy, stale stays visibly stale) plus pending-gap, journal-down, reader identity, health-surface exposure/render, and fallback cadence/resume.

Verified locally (laptop worktree):

  • New + adjacent suites: freshness, freshness-delivery, freshness-ipc, health (25/25 with PM_AGG_FRESHNESS_PORT on a free port), render, conflicts, nav, boot, overview, ui/dashboard overhaul, views, watchdog, events — all pass.
  • tsc --noEmit: no errors in any touched file (pre-existing errors in unrelated test files unchanged).
  • Live boot end-to-end: empty journal + a feed file whose notice never arrived → boot reconcile published the synthetic notice (journal cursor 1, reason: "reconcile") and /api/health reported push up, fleet-feed live with measured lag, other sources silent.

Known pre-existing failure, unchanged by this PR: test/health.test.ts's spawned fixtures server binds the default freshness port 4871, which the live laptop aggregator already holds (EADDRINUSE at the #3977 IPC listener) — it fails identically on pristine origin/main on this machine and passes when the port is free.

🤖 Queue task 8755 · lane code-c


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Anti-entropy for the #3977 journal: a FreshnessReconciler runs on the 5-minute design cadence, reads each canonical source accepted revision (fleet-feed wired: the collector notice identity, generatedAt + sha256 of the feed file), and publishes a synthetic invalidation under the revision own eventId (reason reconcile) when no notice matches — a dropped event is repaired and the late real notice dedupes against the repair.

Push health is exposed on /health and /api/health from the same assembly: push up/degraded/down, last event, last successful reconcile, journal cursor, per-source state/age/lag/notices, and open/repaired/reset gap counts. Push connectivity and source freshness stay separate: a quiet source is silent, never an outage; an unreadable journal is down, never a healthy guess; a non-durable synthetic notice leaves its gap open. The existing 15-minute feed stale chip threshold is untouched.

Browser degraded mode: freshness-client.js rides the #3978 EventSource stream and, while it is down, polls /api/freshness-events.json on the collectors 5-minute cadence (policy in freshness-fallback.ts, unit-tested), refreshing through dashrefresh fragment rereads — never a reload — and stops polling the moment the stream resumes.

Tests (freshness-reconcile-3976, freshness-fallback-3976): dropped event repaired (real FreshnessStore), producer outage/recovery, journal loss/reset counted and repaired, quiet-but-healthy silent, stale data visibly stale, pending notice leaves gap open, journal down, fleet-feed reader identity, health-surface exposure + render, fallback cadence/resume. Live boot verified end-to-end: an empty journal + noticeless feed file was repaired at boot and /api/health reported push up with the synthetic notice as last event.
@dezren39
dezren39 requested a review from a team as a code owner November 13, 2026 03:15
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 22 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8525c6c9-c5ae-428d-9471-3074c1a40b40
Commits

Reviewing files that changed from the base of the PR and between 878f082 and ee6827a.

Files selected for processing: 11

  • pm-stack/aggregator/src/assets/freshness-client.js
  • pm-stack/aggregator/src/freshness-delivery.ts
  • pm-stack/aggregator/src/freshness-fallback.ts
  • pm-stack/aggregator/src/freshness-reconcile.ts
  • pm-stack/aggregator/src/freshness.ts
  • pm-stack/aggregator/src/health.ts
  • pm-stack/aggregator/src/render.ts
  • pm-stack/aggregator/src/server.ts
  • pm-stack/aggregator/test/freshness-fallback-3976.test.ts
  • pm-stack/aggregator/test/freshness-reconcile-3976.test.ts
  • pm-stack/aggregator/test/freshness.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T06:07:11.358448Z ee6827a New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ce9a5625eb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

this.lastAttemptAtMs = nowMs;
let page: { cursor: number; resetRequired: boolean; events: FreshnessNotice[] };
try {
page = await this.journal.listAfter(0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Verify the durable journal instead of only its cached listing

If the journal file is deleted, replaced, or becomes unreadable after startup, this call cannot detect it: FreshnessStore.load() returns immediately once loaded is true, and listAfter() serves the in-memory events and cursor. Consequently, reconciliation can continue reporting push up, advancing the successful-reconcile timestamp, and reporting zero resets even though the durable journal has disappeared. The reset test exercises a fake journal whose cursor changes, but the production store never observes that filesystem change. Add a durable-state verification seam so reconciliation can detect journal loss or unreadability.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0966a06: FreshnessStore.verifyDurable() re-reads the journal file inside the operation chain and throws when it is missing while the in-memory journal is non-empty, unreadable, corrupt, or diverged in cursor, length, or last notice. FreshnessReconciler calls the seam on every pass and reports push down on failure instead of trusting the cached listing. Covered by store-level tests plus reconciler tests, including a real-store test that deletes the journal file after startup.

Comment on lines +51 to +53
var moved = envelope.cursor !== cursor || envelope.resetRequired === true;
cursor = envelope.cursor;
if (moved) refresh();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retry canonical refreshes independently of the notice cursor

While SSE is down, a successful notice poll advances cursor before the asynchronous fragment reread has succeeded. If that reread fails transiently, later polls with the same cursor never dispatch another dashrefresh, so the dashboard remains stale even after the canonical endpoint recovers, until another notice arrives or the user navigates manually. Dispatch canonical refreshes on every fallback interval, as described in freshness-fallback.ts, or track successful canonical reads before suppressing retries.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0966a06: the SSE-down fallback now dispatches the canonical refresh on every successful poll, per the freshness-fallback.ts cadence policy, instead of only when the notice cursor moved. A fragment reread that fails transiently after a notice was consumed is retried on the next cadence tick. Asset test added in freshness-fallback-3976.test.ts.

Comment on lines +185 to +186
this.unavailable.set(reader.source, err instanceof Error ? err.message : String(err));
continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep failed source reads from advancing reconcile success

When a canonical reader throws, this branch records the source as unavailable but leaves durable true. With the production configuration's single fleet-feed reader, every pass during a missing or invalid feed therefore advances lastSuccessfulReconcileAt, even though no canonical revision could be checked. The overall state is degraded, but the health page and API misleadingly report a recent successful reconciliation throughout the outage. Mark the pass unsuccessful on reader failure and preserve the previous successful timestamp.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0966a06: a throwing canonical reader now marks the pass not durable (durable = false in the reader catch), so lastSuccessfulReconcileAt no longer advances while a source cannot be checked; the previous successful timestamp is preserved and the source still reports unavailable. Regression test in freshness-reconcile-3976.test.ts: a failed read after a healthy pass leaves lastSuccessfulReconcileAt unchanged.

Comment on lines +73 to +77
stream.addEventListener("freshness", function (ev) {
if (ev && typeof ev.lastEventId === "string" && /^\d+$/.test(ev.lastEventId)) {
cursor = Number(ev.lastEventId);
}
refresh();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Invalidate server-side caches before refreshing on a notice

If a source notice arrives within ten seconds of an overview or today assembly, this refresh successfully fetches the old data: /frag/overview and /frag/today use overviewContextCache and todayContextCache in server.ts, whose TodayCache.get() returns the cached assembly until its TTL expires. Neither freshness publication nor delivery clears those caches. The client consumes the notice once, and cache expiry does not trigger another browser request, so the accepted revision can remain invisible until another notice or manual refresh despite a healthy stream. Invalidate the affected assembly caches when a notice is accepted, or provide a freshness-aware fragment read that cannot reuse an assembly predating the notice.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0966a06: FreshnessStore.onNotice fires at the moment a notice becomes durable (direct publish persist or outbox flush; never for duplicates or still-pending notices), and server.ts registers a listener that clears overviewContextCache and todayContextCache. Every acceptance path (in-process recordFreshness, the IPC handler, and reconciler synthetic invalidations) publishes through the same store, so none can bypass the invalidation. Store tests cover the fire-once semantics, including the outbox-flush moment.

…3976)

1. FreshnessStore.verifyDurable() re-reads the journal file and throws on loss/replacement/corruption; FreshnessReconciler calls the seam per pass and reports push down instead of serving health from the cached listing. 2. A failed canonical read no longer advances lastSuccessfulReconcileAt (durable=false on reader failure). 3. The SSE-down client refreshes canonical data on every successful fallback poll (the freshness-fallback.ts cadence policy), not only when the notice cursor moved. 4. FreshnessStore.onNotice fires when a notice becomes durable (publish persist or outbox flush, never duplicates); server.ts clears the overview/today assembly caches on notice so a pre-notice assembly cannot be served after acceptance. Tests: 58 pass across freshness suites (new verifyDurable/onNotice store tests, failed-read and durable-loss reconciler tests, fallback asset test); tsc output identical to baseline.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0966a065b7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +60 to +61
refresh();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Refresh canonical fragments even when journal polling fails

When SSE is down and /api/freshness-events.json returns an error or rejects while the canonical fragment endpoints remain available, the fallback never refreshes the dashboard. refresh() is inside the successful-response branch, while failed polls merely advance lastRefreshAt and schedule another journal poll. Simulating three fallback intervals with HTTP 500 responses produces three polls but zero canonical refreshes. Run the canonical refresh independently of journal-poll success so the five-minute fallback described in freshness-fallback.ts still works during a freshness-transport outage.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ee6827a: the fallback poll now dispatches the canonical refresh after the try/catch, on every poll attempt, instead of only inside the successful-response branch. A journal poll that returns an error or rejects no longer blocks the fragment reread; the cursor update still happens only on a successful journal response. Regression test in freshness-fallback-3976.test.ts asserts the refresh dispatch sits after the catch in poll() and that the asset documents refreshing even when the journal poll fails.


function refresh() {
lastRefreshAt = Date.now();
window.dispatchEvent(new CustomEvent("dashrefresh"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Wire refresh dispatches to a registered Datastar listener

Even when a live freshness notice arrives successfully, this dispatch cannot trigger a canonical reread: the fragment containers in server.ts use data-on-dashrefresh__window, but the shipped assets/datastar.js requires data-on:dashrefresh__window. Its attribute parser splits the plugin and event on :, so the existing attributes resolve to an unregistered on-dashrefresh plugin rather than the registered on plugin. Repository-wide search found no alternate JavaScript listener or compatibility bridge; render.ts also documents the colon-syntax requirement in themeToggle(). Wire the new client to working listeners or provide a bridge, otherwise its live-refresh path remains inert.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ee6827a: every dashrefresh listener now uses the colon form the vendored Datastar v1 binds — all 10 fragment containers across render.ts, server.ts, and freshness-delivery.ts converted from data-on-dashrefresh__window to data-on:dashrefresh__window, so the freshness client's window dashrefresh dispatch (and the dashboard's own dispatches in render.ts) reach a registered on plugin. Verified against render.ts themeToggle(), which documents the colon requirement from live testing of the vendored bundle. Regression test in freshness-fallback-3976.test.ts asserts the colon form is present and the dash form absent in all three sources.

P1: dashrefresh listeners used data-on-dashrefresh__window, which the vendored Datastar v1 does not bind (it splits plugin/event on the colon); every dashrefresh dispatch was inert. Convert all 10 fragment containers in render.ts, server.ts and freshness-delivery.ts to data-on:dashrefresh__window.

P2: the SSE-down fallback only refreshed inside the journal poll success branch, so a freshness-journal outage froze the dashboard even though the fragment endpoints were healthy. Dispatch the canonical refresh on every fallback poll, after the try/catch.

Tests: freshness-fallback-3976 gains a colon-form oracle over the three sources and a poll-structure oracle for refresh-on-failed-poll. Targeted freshness suites 58 pass; full aggregator suite matches the clean-tree baseline (17 pre-existing live-source/boot failures, unchanged).
@dezren39
dezren39 merged commit 6a601dd into main Nov 13, 2026
7 checks passed
@dezren39
dezren39 deleted the queue/8755-3976-freshness-health branch November 13, 2026 06:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant