Skip to content

refactor(sync): make /admin/sync stop claiming more than it checked - #120

Merged
guarzo merged 4 commits into
mainfrom
worktree-sync-deferred-findings
Aug 5, 2026
Merged

refactor(sync): make /admin/sync stop claiming more than it checked#120
guarzo merged 4 commits into
mainfrom
worktree-sync-deferred-findings

Conversation

@guarzo

@guarzo guarzo commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Eight code-review findings deliberately deferred out of #118. Items 1–3 and 8
were mechanical; items 4–7 changed what the page is permitted to claim and were
decided before implementing. All eight re-verified as still reproducing at
d679af9 — none were skipped.

What the page is now allowed to say

The queued marker carries an age (4). undispatchedPayloads became
undispatchedSummary: grouped by payload with min(createdAt), so the status
read no longer scales with how long the worker has been down — the one condition
under which an admin loads this page. getSyncStatus folds those timestamps
through jobsFor to a per-job-type queuedSince, and the dot now reads
", queued 3d ago" and turns amber past 15 minutes. startDispatcher swallows
dispatch failures into console.error and retries forever while pg-boss cron
keeps worker.fresh true, so a green worker line beside a silent queued dot was
the exact shape of a stuck outbox. It is no longer silent.

The "few seconds" promise is gone (5, 6). It was gated on a 90-minute
staleness window, which cannot support a claim about seconds. And "no runs
recorded" was being rendered as "the worker is not running" — an assertion from
absence of evidence. queuedNotice now states the worker's actual age, or says
plainly that there is nothing to date it by. Its workerAge parameter is
required, so a forgetful caller is a compile error rather than a default to the
reassuring string.

Collapsing keeps the time axis (7). The finding's own first remedy — add
duration to sameOutcome — would stop anything from ever collapsing, since
durations are near-never equal. Instead the group carries a durationMs
min/max span and renders it in the "Took" column, so a 47-minute run during an
ESI slowdown no longer reads identically to a 2-minute one. That also resolves
(2): the run count was squatting in that column under a header promising a
duration, announced to screen readers as "Took: 4 runs". The count moved beside
the time range in cell 1, and both ends of the range now reach the
accessibility tree at every width.

Mechanical

  • (1) Unbounded SELECT over undispatched rows → grouped, folded into (4).
  • (3) {entry.status ?? "running"} in the group branch — unreachable; removed.
    The single-run branch keeps its fallback, where null really is in flight.
  • (8a) CollapsedRun.to: Date | nullDate, matching its own comment.
  • (8b) errorSummary?: string | null → required; normalizeErrorSummary gone.
  • (8c) RERUNNABLE's comment, stale since isJobType became the gate.
  • (8d) The anonymous three-field shape → exported SyncStatusGroup.

Verification

npm run typecheck     tsc --noEmit — clean
npm run format:check  All matched files use Prettier code style!
npm run lint          1 problem (0 errors, 1 warning) — pre-existing no-img-element in ui.tsx
npm test              Test Files 76 passed (76) / Tests 1051 passed (1051)
npm run test:e2e      190 passed (3.1m)

No schema change, no migration, no change to token handling or the OAuth state
flow. The one new query is read-only and takes no lock, deliberately: a
FOR UPDATE read here would contend with the dispatcher's own claim.

Summary by CodeRabbit

  • New Features

    • Added age-aware queued indicators on the sync administration page, including accessible elapsed-time labels and a warning state for stalled queues.
    • Added clearer queued status messaging based on worker activity and available run history.
    • Grouped sync runs now show counts, start/end times, duration ranges, and status more consistently across screen sizes.
  • Bug Fixes

    • Improved handling of missing timestamps and queued entries.
    • Corrected grouped-run duration and layout behavior on narrow screens.

guarzo added 2 commits August 5, 2026 14:02
Eight review findings deferred out of #118.

The page made three assertions it could not support. The queued marker was
a bare boolean, so a row enqueued 2s ago and one wedged 3 days read
identically -- reachable, since startDispatcher swallows dispatch failures
into console.error and retries forever while the cron scheduler keeps the
worker looking fresh. The "picks it up within a few seconds" promise was
gated on a 90-minute freshness window. And "no runs recorded" rendered as
"the worker is not running", asserting from absence of evidence about a
worker that may have booted seconds ago.

All three now state the age instead of a verdict. queuedNotice's worker
argument is required rather than defaulted, so a forgetful caller is a
compile error instead of silently borrowing the reassuring string.

Collapsing consecutive same-outcome runs also dropped the entire time axis:
five hourly runs, one of which took 47 minutes because ESI was degraded,
collapsed to a row that said nothing about the 47 minutes. sameOutcome
deliberately still ignores duration -- comparing it would stop anything
from ever collapsing -- so the span is carried forward and rendered in
Took, which is also where "N runs" had been sitting under a header
promising a duration.

- outbox: group by payload + min(createdAt); the old unbounded SELECT grew
  without bound exactly while the worker was down
- run-summary: group `to` is Date, not Date | null, as its own comment
  already claimed; errorSummary required, normalizeErrorSummary removed
- page: group status drops an unreachable `?? "running"` that would have
  claimed a finished group was running
- dispatcher: RERUNNABLE's comment now says isJobType is the real gate
…ield

Two independent nullables that are always both-null or both-set spell
four representable states for the two that exist, and force every
consumer to test both to rule out the two that cannot happen -- the same
defect class this branch's type-design findings exist to remove.

Also drops the non-null assertion in toGroup's duration map by filtering
on both ends rather than asserting past finishedAt.
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 28 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 48bd3d0f-d316-4c70-b2f8-c91a45fd03ac

📥 Commits

Reviewing files that changed from the base of the PR and between d8ba841 and 5cbcbec.

📒 Files selected for processing (7)
  • src/app/admin/sync/page.tsx
  • src/app/admin/sync/view.ts
  • src/core/run-summary.ts
  • src/services/outbox.ts
  • src/services/sync-status.ts
  • tests/run-summary.test.ts
  • tests/sync-view.test.ts
📝 Walkthrough

Walkthrough

The sync status pipeline now records the oldest queued timestamp per job. The admin sync page uses this data for age-aware queued markers. Grouped runs now expose duration ranges, counts, and precise time endpoints.

Changes

Sync status and queued age

Layer / File(s) Summary
Queued age data pipeline
src/services/outbox.ts, src/services/sync-status.ts, src/worker/dispatcher.ts, tests/outbox.test.ts, tests/sync-status.test.ts
Undispatched payloads are grouped by payload with their oldest creation time. Sync status maps these timestamps to job types and exposes queuedSince. Dispatcher documentation clarifies the fixture role of RERUNNABLE.
Age-aware queued markers
src/app/admin/sync/view.ts, src/app/admin/sync/page.tsx, src/app/globals.css, tests/sync-view.test.ts, e2e/sync.spec.ts
Queued notices now use worker age. Queue markers show elapsed age and use a stuck state at the configured threshold. Tests cover accessible text and five- and twenty-minute queue ages.

Grouped run display

Layer / File(s) Summary
Grouped run aggregation
src/core/run-summary.ts, tests/run-summary.test.ts
Grouped runs require a finish time and retain minimum and maximum complete-run durations. Error summaries remain explicitly nullable.
Grouped run rendering
src/app/admin/sync/page.tsx, src/app/globals.css, e2e/sync.spec.ts
Grouped rows show counts with the Started range, expose both time endpoints on narrow layouts, and display fixed or ranged durations. End-to-end tests verify the updated layout.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AdminSyncPage
  participant getSyncStatus
  participant undispatchedSummary
  participant OutboxDatabase
  AdminSyncPage->>getSyncStatus: request sync status
  getSyncStatus->>undispatchedSummary: read queued payload summaries
  undispatchedSummary->>OutboxDatabase: group undispatched rows by payload
  OutboxDatabase-->>undispatchedSummary: oldest queued timestamps
  undispatchedSummary-->>getSyncStatus: queued and queuedSince values
  getSyncStatus-->>AdminSyncPage: render age-aware queue markers
Loading

Possibly related PRs

  • guarzo/authGD#2: Modifies the shared dispatcher and RERUNNABLE fixture behavior.
  • guarzo/authGD#4: Modifies the admin sync UI and sync-status plumbing extended by this change.

Poem

I’m a rabbit watching queues grow old,
With warning markers bright and bold.
Runs show their starts, their ends, their span,
Counts sit neatly where they can.
Hop, hop—the sync page tells the plan!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: making sync status claims reflect only verified state and timing.
Description check ✅ Passed The description explains the changes, rationale, verification results, and deployment impact, although it does not use every template heading explicitly.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch worktree-sync-deferred-findings
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch worktree-sync-deferred-findings

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/app/admin/sync/page.tsx`:
- Line 122: Update the lede construction around queuedNotice in the admin sync
page to remove claims based on worker.fresh about pickup timing or whether the
worker is running. Replace those branches with neutral queue guidance or the
known workerAge, while preserving the existing queued and notice behavior.

In `@src/core/run-summary.ts`:
- Around line 256-277: Update the duration calculation in the grouped-run
summary around the durations map to exclude intervals unless both dates produce
finite timestamps and finishedAt is not earlier than startedAt, matching the
validation used by formatDuration. Preserve valid non-negative durations and
return null when none remain, then add coverage for a grouped run whose
finishedAt precedes startedAt.

In `@tests/sync-view.test.ts`:
- Around line 302-315: Extend the “stays a bare marker under the notable
threshold” test around queuedMarkerText to assert that at(-2 * MIN) returns the
age-bearing ", queued 2m ago" text. Keep the existing below-threshold assertions
unchanged so the exact QUEUED_AGE_NOTABLE_MS boundary is covered.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 21b5e51e-bb78-4997-b9d1-8a06689b403f

📥 Commits

Reviewing files that changed from the base of the PR and between 3fbab8b and d8ba841.

📒 Files selected for processing (12)
  • e2e/sync.spec.ts
  • src/app/admin/sync/page.tsx
  • src/app/admin/sync/view.ts
  • src/app/globals.css
  • src/core/run-summary.ts
  • src/services/outbox.ts
  • src/services/sync-status.ts
  • src/worker/dispatcher.ts
  • tests/outbox.test.ts
  • tests/run-summary.test.ts
  • tests/sync-status.test.ts
  • tests/sync-view.test.ts

Comment thread src/app/admin/sync/page.tsx
Comment thread src/core/run-summary.ts
Comment thread tests/sync-view.test.ts
guarzo added 2 commits August 5, 2026 14:40
undispatchedSummary dropped a row whose min(createdAt) came back null, and
getSyncStatus derived `queued` from the presence of that row -- so an
impossible null would not merely have cost the "3d ago" suffix, it would
have made a job with work waiting in the outbox render as idle. That is
the exact under-reporting the queued marker exists to prevent.

The row is now carried through with a nullable age, presence and age are
tracked separately in the fold, and both marker helpers degrade on their
own: no age means a bare ", queued" and never an escalation, since
escalation is a claim about elapsed time.
toGroup subtracted the pair unconditionally, unlike formatDuration, which
has always guarded Number.isFinite and ms >= 0 on the same computation. A
clock adjustment between the two writes inverts the pair, and an Invalid
Date subtracts to NaN -- either would let Math.min/max report a span the
group cannot have taken, and one bad member poisons a row standing in for
several good ones.

Also covers the exact QUEUED_AGE_NOTABLE_MS boundary, which sat between
the existing under- and over-threshold assertions.
@guarzo
guarzo merged commit e5d76df into main Aug 5, 2026
7 checks passed
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