fix(sdk/policy_cache): remove no-op background refresh (lost writes + NaN)#433
Open
dsmfa10 wants to merge 1 commit into
Open
fix(sdk/policy_cache): remove no-op background refresh (lost writes + NaN)#433dsmfa10 wants to merge 1 commit into
dsmfa10 wants to merge 1 commit into
Conversation
… NaN) The near-expiry "background refresh" spawned a task over `Arc::new(self.clone())`, but `TokenPolicyCache::Clone` deliberately produces a fresh, empty cache (asserted by `cache_clone_has_fresh_cache`). The refreshed policy was therefore written into a throwaway clone and never reached the shared cache — a pure no-op that still performed a network fetch, and computed `ttl_remaining / total_ttl` as NaN when `default_ttl == 0` (which silently never triggered anyway). Remove the dead refresh block; policies refresh lazily on expiry via the miss path. A correct proactive refresh would require sharing the cache behind an `Arc` without breaking clone semantics — a separate change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
TokenPolicyCache::get_policyattempted a proactive "background refresh" when acached entry neared expiry. It spawned a task over
Arc::new(self.clone())andwrote the refreshed policy into that clone:
TokenPolicyCache::Clonedeliberately yields a fresh, empty cache (there isan explicit test,
cache_clone_has_fresh_cache, asserting this). So the spawnedtask refreshes into a clone whose cache is then dropped — the refreshed policy
never reaches the shared cache. Net effects:
network fetch and a task spawn each time.
NaNratio whendefault_ttl == 0(x / 0.0);NaN <= thresholdis false,so it silently never fires anyway.
Fix
Remove the dead refresh block. Policies refresh lazily on expiry via the
existing miss path (which already fetches and inserts into the real cache). This
also removes the wasted fetch/spawn and the NaN computation. (Note:
now/ theTTL fields here are deterministic
dt::tick()counters, not wall-clock, so thisstays within DSM's clockless rule — no protocol timestamps are involved.)
A correct proactive refresh would require sharing the cache behind an
Arcwithout breaking the documented "fresh cache on clone" invariant — that's a
separate change, noted in the deferred dead-code/refactor backlog.
Verification
cargo check -p dsm_sdkclean (no unusedArc/method warnings — those symbolsare used elsewhere). The
cache_clone_has_fresh_cacheinvariant is preserved.CI gates & coverage
Full verification for this PR runs in CI — Rust (
cargo fmt --check,clippy -D warnings, workspace tests), Frontend, Android Unit Tests,Coverage, SPDX headers, CodeQL (see the PR's Checks tab). The local
check noted above is a subset; the broader mandated gates (full workspace test
suite, codegen/scan, Android, Frontend) run in CI, not locally — none is
silently skipped.