Skip to content

fix: let the reload policy decide whether subscribe refreshes the cache - #7

Merged
sujal-maheshwari2004 merged 1 commit into
mainfrom
fix/3-hold-keeps-cache
Oct 18, 2026
Merged

sujal-maheshwari2004 merged 1 commit into
mainfrom
fix/3-hold-keeps-cache

Conversation

@sujal-maheshwari2004

Copy link
Copy Markdown
Contributor

What was wrong

The subscribe handler dropped and refetched the cached prompt on every event, before on_change ran. So when an agent's reload policy decided to hold a change, client.get() returned the new version anyway (with cache_ttl set).

Fix

subscribe(uri, on_change, reload=None) takes the reload policy for the client cache: a set of verdicts (e.g. {"minor edit", "localized tweak", "new"}) or a function (version, classification) -> bool.

  • An accepted change is fetched into the cache before on_change runs, so get() inside the callback returns it.
  • Any other change is held: the cache keeps the old version until cache_ttl expires.
  • Default None holds every change (fail closed, like the docs' policy guidance).

The event parsing was also untangled so an exception raised inside on_change no longer triggers a second on_change call with the raw body. README example updated to pass reload=AUTO_RELOAD.

Tests

New tests use a fake nats module to deliver events without a broker: held change keeps the cached version, default holds everything, accepted change is in the cache before on_change, and a function policy works.

$ pytest -q            # before the fix (tests only)
FAILED tests/test_client.py::test_held_change_keeps_cached_version - TypeError...
FAILED tests/test_client.py::test_default_policy_holds_every_change - Asserti...
FAILED tests/test_client.py::test_accepted_change_refreshes_cache_before_on_change
FAILED tests/test_client.py::test_reload_policy_can_be_a_function - TypeError...
4 failed, 14 passed

$ pytest -q            # after
18 passed in 0.39s

Real run against priompt v0.7.2 (serve with -nats-token), cache_ttl=30, an agent that holds every change, then a structural publish:

Before:

held 62cfb53f5f95 [structural]
served before: 133a4b14cff9
get() after hold: 62cfb53f5f95
BUG: held change was served

After:

held 62cfb53f5f95 [structural]
served before: 133a4b14cff9
get() after hold: 133a4b14cff9
HELD OK

With reload={"structural"} the same publish is reloaded: get() inside on_change returns 62cfb53f5f95.

Fixes #3

🤖 Generated with Claude Code

@sujal-maheshwari2004

Copy link
Copy Markdown
Contributor Author

Rebased onto main after #8 (fix for #4) merged. The only conflict was the subscribe() docstring, and I kept both paragraphs. On the rebased branch: pytest -q gives 19 passed, the #3 real-run repro still prints HELD OK, and the #4 repro still raises ConnectionError ... 'Authorization Violation'.

The subscribe handler dropped and refetched the cached prompt on every
event, before on_change ran, so after the agent decided to hold a change,
client.get() returned the new version anyway.

subscribe() now takes reload=, a set of verdicts or a function
(version, classification) -> bool. Only an accepted change is fetched into
the cache (before on_change, so get() inside the callback sees it). Any
other change is held: the cache keeps the old version until cache_ttl
expires. The default holds every change, matching the fail-closed policy
the docs recommend.

Fixes #3

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sujal-maheshwari2004
sujal-maheshwari2004 merged commit 390679c into main Oct 18, 2026
1 check passed
@sujal-maheshwari2004
sujal-maheshwari2004 deleted the fix/3-hold-keeps-cache branch October 18, 2026 17:50
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.

Holding a change doesn't hold it: subscribe refreshes the cache on every event

1 participant