Repository navigation
Cache OpenAI-compat model discovery with a 30s TTL - #1860
Conversation
## Completed in This Session - [x] Added a 30s TTL cache over discover_openai_compat_models in local_agent_service.rs - [x] Added ListModelsRequest.force_refresh to bypass the cache (proto + daemon + Tauri + frontend) - [x] Wired the Settings "Refresh remote models" button to force_refresh=true - [x] Updated CLI and secondary Tauri list_local_models call sites for the new proto field - [x] Added 4 daemon unit tests covering fresh-hit, stale-miss, force_refresh-bypass, and cache-population - [x] cargo test -p nodespace-daemon passes (all suites); bun run test passes 171/171 files, 4113/4113 tests ## Remaining Work - [ ] Run bun run test:all end to end and confirm no regressions - [ ] bun run quality:fix and commit any formatting changes - [ ] Push and open PR against #1807 - [ ] /pragmatic-code-review, address feedback loop ## Current State - Files modified: packages/proto/proto/local_agent_service.proto, packages/daemon/src/services/local_agent_service.rs, packages/desktop-app/src-tauri/src/commands/chat_models.rs, packages/desktop-app/src-tauri/src/commands/local_agent.rs, packages/desktop-app/src/lib/services/tauri-commands.ts, packages/desktop-app/src/lib/components/settings/model-manager.svelte, packages/dev-tools/src/dev-proxy.ts, packages/cli/src/commands/model.rs - Tests status: Passing (daemon unit tests + frontend bun run test baseline) - Known issues: none currently open - Dependencies: none ## Context for Next Session TTL-only cache (no cross-service invalidation wiring) was chosen over an invalidate-on-config-write design because SettingsServiceImpl (process-global) and LocalAgentServiceImpl (per-database, potentially multiple instances) are constructed in different scopes in assembly.rs, making shared invalidation state architecturally awkward for the benefit gained. The 30s TTL plus an explicit force_refresh bypass (used by the Settings "Refresh remote models" button) satisfies all three acceptance criteria on issue #1807 without that complexity. ## Acceptance Criteria Status From issue #1807: - [x] Repeated ListModels calls within the TTL do not re-query endpoints - [x] A newly available model still appears without an app restart — TTL expiry (30s) or the explicit "Refresh remote models" button (force_refresh) - [x] A slow or dead endpoint does not stall the selector on every mount — cache hit avoids DISCOVERY_TIMEOUT on repeated calls Co-Authored-By: Claude <[email protected]>
…rce_refresh param Co-Authored-By: Claude <[email protected]>
Pragmatic Code Review — PR #1860 (issue #1807)Review Type: Initial Review Reviewed the full diff against Requirements Check
Code Review Findings🟡 Important1.
The issue's framing is explicit user action bypasses the cache. Mounting the Settings pane is not an explicit refresh action — it's a mount, structurally identical to the three mount-time call sites the issue names as the problem. As written, opening Settings unconditionally pays up to The principle is least astonishment / intent-preserving parameterization: a function whose behavior is "bypass the cache" should not be reachable from a code path whose intent is "render initial state." Suggested fix — make the bypass a parameter of the call, not a property of the function: async function refreshRemoteModels(forceRefresh = false) { ... await chatModelList(forceRefresh); ... }
// onMount:
await refreshRemoteModels(); // cache-friendly
// button:
onclick={() => refreshRemoteModels(true)} // explicit bypassNote the button's 2.
I want to be explicit that I agree with the author's decision to skip daemon-side invalidate-on-config-write. That reasoning is sound: But the same user-visible outcome is available for ~2 lines at the layer that already knows a config just changed — the frontend. Adding 3. The lock is correctly released before the network call (good — no deadlock, no holding a The docstring at line 1105 says the cache exists "so repeated This is not a correctness bug (the fan-out is already bounded and per-endpoint-degrading, and the last writer simply wins), so I'm not blocking on it. But the cost/benefit favors fixing it: the first chat open is the worst-case latency the issue was filed about, and the cheapest correct form is small. Either use a For contrast, 🟢 Suggestions4. The other three tests are genuinely well-constructed — I want to call that out, because the "seed a cache entry containing a fake model that discovery could never produce (zero endpoints configured)" trick makes cache-hit-vs-miss observable without HTTP mocking, and the comments explain exactly why the empty- This fourth one is weaker. It asserts the cache is let first_fetched_at = { ...lock, unwrap, .0 };
let _ = svc.discover_openai_compat_models(false).await;
let second_fetched_at = { ...lock, unwrap, .0 };
assert_eq!(first_fetched_at, second_fetched_at, "second call must be served from cache, not re-discovered");5.
Nit — Verified Clean
RecommendationREQUEST CHANGES The core daemon-side implementation is solid — correct lock discipline, monotonic staleness, complete end-to-end plumbing, genuinely good documentation, and three of four tests are thoughtfully constructed. The architectural judgment call on skipping cross-service invalidation is the right one and well-reasoned. The blocker is narrow and frontend-side: |
malibio
left a comment
There was a problem hiding this comment.
REQUEST CHANGES (posted as a comment — GitHub blocks formal change-requests on one's own PR). See the detailed review comment above. Blocker is narrow and frontend-side: refreshRemoteModels() now force-refreshes on mount (model-manager.svelte:121), re-creating the per-mount stall acceptance criterion #3 exists to eliminate. Daemon-side cache implementation is solid.
Pragmatic Code Review — PR #1860 (issue #1807)Review Type: Initial Review Requirements Check
All three acceptance criteria are met. Code Review Findings🟡 Important: Cache invalidation option was dismissed without the cheapest version being considered📁 The PR's own reference pattern, The author's stated reason for skipping config-write invalidation is that Engineering principle: when a comparison pattern is explicitly named as the design reference, the tradeoff analysis should engage with what that pattern actually does, not a more expensive version of it. 🟡 Important: Thundering herd on concurrent cache miss📁
Engineering principle: an accepted tradeoff that isn't called out in a comment reads as an oversight to the next reader; a short note prevents redundant re-litigation. 🟢 Suggestion:
|
malibio
left a comment
There was a problem hiding this comment.
Review recommendation: APPROVE (GitHub disallows self-approval on this PR's author account, so recording the verdict here instead of via --approve). See the full review comment above for the complete report. All 3 acceptance criteria from issue #1807 are met and independently verified; cache logic is correct (no lock held across the network await, staleness check correct, force_refresh plumbing complete end-to-end across proto/daemon/Tauri/frontend/dev-proxy); tests are meaningful, not tautological. Two Important (non-blocking) notes: (1) the cache-invalidation tradeoff writeup overstates the cost of the InboundRelationshipCache-style event-driven invalidation it cites as precedent — worth a comment noting it was deferred as YAGNI rather than 'too costly to wire'; (2) no single-flight guard on concurrent cache-miss (thundering herd), same gap as the precedent pattern, narrow blast radius, worth a one-line comment marking it as accepted rather than an oversight.
Correction to my review above (Approve → Request Changes)I need to retract my "APPROVE" recommendation. On re-checking after seeing another review pass on this PR flag the same thing, I verified directly and confirmed a real bug I missed the first time. 🔴 Critical:
|
malibio
left a comment
There was a problem hiding this comment.
REVISED review recommendation: REQUEST CHANGES (posted as a comment -- GitHub blocks formal review states on one's own PR). Retracting my earlier APPROVE; see the correction comment above. Verified a real bug: model-manager.svelte's onMount calls refreshRemoteModels(), which unconditionally passes force_refresh:true, so opening Settings bypasses the cache and re-queries every endpoint live on every mount -- reproducing the exact stall issue #1807 exists to fix, for this one surface (fails acceptance criterion #3 for this call site). Fix is small: parameterize refreshRemoteModels(forceRefresh = false), pass true only from the button's onclick at line 323. Everything else in the PR (daemon cache logic, proto/Tauri/frontend plumbing, tests) remains sound per my first comment.
…, strengthen cache test - Fixed model-manager.svelte's refreshRemoteModels() unconditionally passing force_refresh: true — it was called from onMount, reintroducing the per-mount stall this issue exists to fix for the Settings pane. Now parameterized (forceRefresh = false), with onMount passing false and the explicit "Refresh remote models" button passing true. - saveConfig()/deleteConfig() now call refreshRemoteModels(true) after writing, so editing/removing an endpoint config is reflected immediately instead of waiting out the 30s TTL. - Strengthened discovery_result_is_cached_for_subsequent_calls to assert the cache's fetch timestamp is unchanged across a second call, so it actually verifies "subsequent calls are served from cache" rather than only that the cache gets populated once. - Documented two accepted tradeoffs raised in review as deferred-not-missed: no event-driven cache invalidation (Settings' force_refresh(true) after a config write already covers it without cross-service wiring), and no single-flight de-dup on a concurrent cache miss (same accepted gap as InboundRelationshipCache's refresh_cache, narrow blast radius). Addresses reviewer recommendations from the pragmatic-code-review on PR #1860 (🔴 critical mount-time bug, 🟡 config-write refresh, 🟢 test tautology). Skipped: gRPC-handler-level test for force_refresh extraction (marginal value, single field read with no branching) and a named struct for the cache tuple (documented, used in exactly two places) — both style/coverage polish beyond this PR's scope. Co-Authored-By: Claude <[email protected]>
Address Review — commit 6ae387aAddressed the findings from both review passes (both converged on the same critical bug independently). ✅ Addressed🔴 Critical — mount-time force-refresh in 🟡 Important — config writes don't refresh the discovered list. 🟢 Suggestion — tautological test. ⏭️ Skipped (with reasoning)🟡 Important — cache-invalidation design tradeoff writeup. Both reviews agreed the underlying decision (TTL-only, no 🟡 Important — no single-flight de-dup on concurrent cache miss (thundering herd). Both reviews explicitly called this a narrow, precedented gap (same property as 🟢 Suggestion — gRPC-handler-level test for 🟢 Suggestion — named struct instead of the cache tuple. Verification
Re-Review DecisionDecision: RE-REVIEW NEEDED Rationale: The critical finding was a real functional bug in a code path both reviews independently flagged, and the fix changes call-site behavior (mount vs. explicit-refresh semantics) — worth a second pass to confirm the fix is complete and didn't introduce a new gap (e.g. double-checking the button's arrow-wrapper doesn't regress to passing the event object). |
Re-Review — commit 6ae387aReview Type: Re-Review (delta since Previous Review Summary
Verification of the Critical fix📁 Confirmed by reading the full current file, not just the diff:
This is a complete, correct fix. All three call sites now have the semantics the issue requires: passive mount (cache-friendly), explicit user refresh (bypass), and config write (bypass, since the source data changed). Verification of the strengthened test📁
Verified the cache field really is Ran the full module: Also ran Judgment on the two new code comments📁 Both comments make specific, checkable claims about
Both comments are accurate, not overclaiming. They correctly scope the deferral: "revisit if a second config writer appears that can't reach for the same frontend hook" is an honest, falsifiable condition rather than a vague "future work" wave-off. New issues introduced by this commitNone found. The two call sites added ( Recommendation: APPROVEThe critical bug is fixed correctly and completely, verified by direct code reading (not just trusting the diff) against exactly the failure mode both prior reviews and my own instructions called out (arrow-wrapper vs. bare function reference). The test strengthening is genuine — it would now catch a cache-bypass regression. The two deferred Important items are honestly and accurately documented as accepted tradeoffs with a concrete revisit condition, not swept under the rug. No new issues introduced by the fix commit. |
malibio
left a comment
There was a problem hiding this comment.
Review recommendation: APPROVE (GitHub disallows self-approval on this PR's author account, so recording the verdict here instead of via --approve). See the detailed re-review comment above for full verification: critical bug fixed and verified by direct code reading against the exact failure mode flagged (arrow-wrapper vs bare function ref), strengthened cache test genuinely distinguishes cache-hit from cache-populate (35/35 daemon tests pass), deferred Important items honestly documented as accepted tradeoffs with concrete revisit conditions, no new issues introduced by commit 6ae387a.
Summary
discover_openai_compat_modelsin the daemon'sLocalAgentService, so repeatedListModelsRPCs within the window are served from cache instead of re-querying every configured OpenAI-compatible endpoint.ListModelsRequest.force_refresh(proto → daemon → Taurichat_model_list→ frontendchatModelList(forceRefresh)) so the explicit "Refresh remote models" action in Settings can bypass a fresh cache hit.list_local_modelsTauri command for the new proto field.Design notes
A TTL-only cache (no invalidate-on-config-write wiring) was chosen because
SettingsServiceImpl(process-global, one instance) andLocalAgentServiceImpl(per-database, potentially multiple instances under ADR-053) are constructed in different scopes inassembly.rs— sharing invalidation state between them would require threading a new field throughSharedContextand touching 4 platform-gated entrypoints inmain.rs, for marginal benefit over a short TTL plus an explicit refresh escape hatch.Acceptance criteria (issue #1807)
ListModelscalls within the TTL do not re-query endpointsforce_refresh)DISCOVERY_TIMEOUTon repeated callsTest plan
cargo test -p nodespace-daemon— added 4 unit tests covering fresh-hit, stale-miss, force_refresh-bypass, and cache-populationcargo test --workspace— full workspace, no failuresbun run test— 171/171 files, 4113/4113 tests (frontend baseline unchanged)bun run test:all— cleanbun run quality:fix— clean (ESLint, tsc, cargo fmt, cargo clippy -D warnings)Closes #1807
🤖 Generated with Claude Code