Skip to content

feat(fetcher): introduce RealtimeFetcher trait for clock-like widgets - #37

Merged
unhappychoice merged 1 commit into
mainfrom
feat/realtime-fetcher
Apr 21, 2026
Merged

unhappychoice merged 1 commit into
mainfrom
feat/realtime-fetcher

Conversation

@unhappychoice

@unhappychoice unhappychoice commented Apr 21, 2026 •

Copy link
Copy Markdown
Owner

Closes #36

Summary

  • Add sync `RealtimeFetcher` trait — contract is <1ms, infallible, no I/O. Bypasses the disk cache entirely and recomputes every frame.
  • Teach `Registry` to hold both `Fetcher` (cached) and `RealtimeFetcher` under one name-keyed lookup. `RegisteredFetcher` enum exposes unified `name()` / `safety()` so the trust gate needs no branching.
  • Migrate `ClockFetcher` and `TodayStub` to the new trait. Both are pure `Local::now()` calls — caching them was always wrong (cache-warm splash would show a minute-old clock).
  • `runtime.rs` splits trust-passed widgets by kind: cached path goes through the existing daemon + disk cache, realtime path computes each frame and is overlaid into the payload map in `refresh_payloads`.
  • Daemon's `fetch_and_persist` skips realtime widgets. First-run wait promotion only triggers when there are cached widgets expecting data, so a clock-only config doesn't force the full wait window.

Why not `CachePolicy` on the existing trait?

A realtime fetcher that accidentally does `reqwest` inside `async fetch` would silently block the frame loop. Separate sync trait makes the "no I/O, fast" contract type-enforced.

Test plan

  • `cargo test` (148 passing, +1 new for realtime-fetcher partition gating)
  • `cargo clippy --all-targets -- -D warnings` clean
  • manual: cd into a repo, confirm clock shows current time (not cached-from-previous-invocation)
  • manual: `--wait` mode, confirm clock ticks during the animation window if format includes `%S`

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Separated real-time and cached data sources within the system architecture.
    • Clock and date information now update dynamically on each frame instead of relying on cached values, improving data freshness and accuracy.

Fetchers whose value is intrinsically "right now" (wall clock, instantaneous
counters, countdowns) shouldn't go through the TTL-based disk cache — caching
a timestamp for 60s meant a cache-warm splash showed a minute-old clock.

Adds a sync `RealtimeFetcher` trait (contract: <1ms, infallible, no I/O) and
teaches the Registry to hold both kinds under one name-keyed lookup. Runtime
splits trust-passed widgets by kind: cached widgets go through the existing
daemon + disk cache path unchanged, realtime widgets recompute on every frame
and never touch disk.

Migrates `ClockFetcher` and `TodayStub` to the new trait. Daemon's
`fetch_and_persist` skips realtime widgets (nothing to persist). First-run
wait promotion now only triggers when there are cached widgets expecting data,
so a clock-only config no longer forces the full wait window.

Closes #36

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Apr 21, 2026 •

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

The PR introduces a RealtimeFetcher trait for widgets with fast, synchronous, infallible computations (e.g., clock, today) that bypass disk cache. A new RegisteredFetcher enum holds either cached or realtime fetchers. The registry, runtime, and related modules are updated to distinguish and handle both kinds: realtime fetchers compute fresh values each frame; cached fetchers use the existing disk cache mechanism.

Changes

Cohort / File(s) Summary
New Registry Infrastructure
src/fetcher/mod.rs
Introduced RealtimeFetcher trait (sync compute method, no Result), RegisteredFetcher enum to unify cached/realtime variants, and updated Registry with register_realtime, get_cached, get_realtime methods. Registry now stores and retrieves both fetcher kinds.
Fetcher Migrations
src/fetcher/clock.rs, src/fetcher/stub.rs
Migrated ClockFetcher and TodayStub from async Fetcher to sync RealtimeFetcher. Removed async_trait and FetchError dependencies; converted tests from #[tokio::test] to #[test]. Added realtime_stubs() function parallel to stubs().
Runtime Integration
src/runtime.rs
Split widget handling into cached and realtime paths. Realtime widgets compute fresh values on each frame via refresh_payloads; cached widgets use disk cache as before. Updated cache lookup and fetch logic to distinguish widget kinds.
Trust Gating Tests
src/trust.rs
Added FakeRealtime test struct and new test partition_treats_realtime_safe_fetcher_as_fetchable_when_untrusted to verify realtime-safe fetchers bypass gating and are returned as fetchable.

Sequence Diagram

sequenceDiagram
    actor User
    participant Runtime as Runtime
    participant Registry
    participant CachedFetcher as Cached Fetcher
    participant RealtimeFetcher as Realtime Fetcher
    participant Cache as Disk Cache

    User->>Runtime: Request payload (first frame)
    Runtime->>Registry: get_cached(name) / get_realtime(name)
    
    alt Cached Widget
        Registry-->>Runtime: Some(Cached fetcher)
        Runtime->>Cache: Load cached entry
        alt Cache hit & fresh
            Cache-->>Runtime: Payload
        else Cache miss or stale
            Runtime->>CachedFetcher: fetch() async
            CachedFetcher-->>Runtime: Result<Payload>
            Runtime->>Cache: Persist payload
        end
    else Realtime Widget
        Registry-->>Runtime: Some(Realtime fetcher)
        Runtime->>RealtimeFetcher: compute()
        RealtimeFetcher-->>Runtime: Payload (no disk I/O)
    end
    
    Runtime-->>User: Rendered output

    User->>Runtime: Refresh (next frame)
    Runtime->>Registry: get_cached(name) / get_realtime(name)
    
    alt Cached Widget
        Runtime->>Cache: Check TTL, refresh if expired
    else Realtime Widget
        Runtime->>RealtimeFetcher: compute() again
        RealtimeFetcher-->>Runtime: Fresh payload
    end
    
    Runtime-->>User: Updated output
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • PR #28: Introduced the async Fetcher trait and Registry infrastructure that this PR extends with RealtimeFetcher and split handling logic.
  • PR #30: Added trust gating via partition_by_trust; this PR's new test verifies realtime-safe fetchers are correctly handled by trust partitioning.

Poem

🐰 A flutter of whiskers, a bound through the code,
Realtime fetchers dance down the fresh-data road!
No cache shall delay them—each frame, they tick true,
Clock strikes with precision: one sync call, one view! ⏰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 59.65% 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat(fetcher): introduce RealtimeFetcher trait for clock-like widgets' accurately summarizes the primary change—introducing a new RealtimeFetcher trait for synchronous, cache-bypassing fetchers like clock.
Linked Issues check ✅ Passed All objectives from issue #36 are met: RealtimeFetcher trait defined (sync, infallible, name/safety/compute), realtime fetchers bypass cache and recompute each frame, Safety semantics unchanged, Registry updated for both kinds, runtime routes properly, daemon skips realtime, and ClockFetcher/TodayStub migrated.
Out of Scope Changes check ✅ Passed All changes are directly aligned with issue #36 objectives: trait definition, Registry enhancements, fetcher migrations, runtime integration, and supporting test updates; no unrelated refactoring or scope creep detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/realtime-fetcher

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

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

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.

core: RealtimeFetcher trait for clock-like widgets (bypass disk cache)

1 participant