Skip to content

feat(core): resource cache phase 3 — cacheTime single-runtime eviction#127

Merged
mahyarmlk merged 3 commits into
mainfrom
feat/core-resource-cachetime-phase3
Jun 27, 2026
Merged

feat(core): resource cache phase 3 — cacheTime single-runtime eviction#127
mahyarmlk merged 3 commits into
mainfrom
feat/core-resource-cachetime-phase3

Conversation

@mahyarmlk

@mahyarmlk mahyarmlk commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator

Summary

Implements Phase 3 of the resource cache proposal: single-runtime cacheTime for @intent-framework/core. After an entry becomes stale, the cached value is retained for cacheTime milliseconds before eviction, giving consumers a grace window to use stale data.

API Added

  • cache.cacheTime?: number on ResourceCacheOptions — eviction delay in ms after staleness

Behavior

  • cacheTime timer starts when an entry becomes stale (from invalidate(), action invalidation, or staleTime expiry)
  • Active entry (has subscribers): eviction resets entry to idle state, clears value/error/stale, notifies subscribers
  • Inactive keyed entry: entries.delete(key) silently — no subscriber notification
  • executeLoad clears the cacheTime timer at start (even before deduplicate check) and on success
  • Failed load/reload does not start cacheTime
  • cacheTime: 0 schedules eviction on next event loop tick via setTimeout(fn, 0)

Follow-Up Runtime Fixes (35642f1)

  • Detached in-flight promise ownership checks — prevents stale promise interference across key switches
  • Active-key sync before dedupe return — ensures deduplicated callers see the correct active key entry

Scope Limitations

  • Per-key, per-ResourceNode, within a single ScreenRuntime
  • No cross-navigation survival
  • No SWR (stale-while-revalidate) pattern
  • No InMemoryResourceCache or dependency-tracked keys

Files Changed

File Change
packages/core/src/resource.ts Added cacheTime type, cacheTimer on Entry, helpers, invalidation/load/dispose
packages/core/src/core.test.ts 26 new cacheTime tests + 3 runtime fix regression tests (232 total, all pass)
docs/Resources.md cacheTime docs with scope notes
docs/proposals/Resource-Cache-And-Stale-Semantics.md Phase 3 marked implemented
.changeset/nine-snails-walk.md Patch changeset

Validation

  • pnpm test — all 232 core tests pass
  • pnpm typecheck
  • pnpm build
  • pnpm lint
  • pnpm pack:check
  • pnpm changeset status

Fix 1 — in-flight promise after inactive eviction must not mutate
detached entries. Add ownership check (entries.get(key) !== entry)
before applying success/failure results, calling staleNotify(), or
starting staleTime/cacheTime timers.

Fix 2 — switching active key to an already in-flight entry must sync
public state (syncFromEntry) before dedupe early return. Previously
the dedupe path returned the in-flight promise without updating
currentStatus/currentValue/currentError, leaving public getters
showing the previous key's state.

Add tests:
- detached promise resolution does not notify subscribers
- detached promise resolution does not start timers
- switching to in-flight key syncs state before dedupe return
@mahyarmlk mahyarmlk merged commit 95b68b4 into main Jun 27, 2026
1 check passed
@mahyarmlk mahyarmlk deleted the feat/core-resource-cachetime-phase3 branch June 27, 2026 12:55
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