Skip to content

Only a daemon that actually runs agents writes extended progression (#621) - #625

Merged
jeremydmiller merged 1 commit into
mainfrom
gh/621-extended-progression-lifecycle
Aug 4, 2026
Merged

Only a daemon that actually runs agents writes extended progression (#621)#625
jeremydmiller merged 1 commit into
mainfrom
gh/621-extended-progression-lifecycle

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

Closes #621.

The problem

Tracker is per-database and shared. Every JasperFxAsyncDaemon subscribed an ExtendedProgressionWriter to it in the constructor, unconditionally — so N daemons for one database meant N writers on one publication stream, each renting its own connection and issuing the same UPDATE ... FROM unnest(...) against the same rows.

That includes daemons built purely to read. BuildProjectionDaemonAsync is new ProjectionDaemon(...) per call with no caching, and nothing in its signature suggests the returned object owns a background write loop against the database. CritterWatch's EventProgressionPoller did exactly this, once per node-owned database every 15 seconds, without disposing — the likely mechanism behind marten#5167, where the blocker and the blocked statement in pg_stat_activity were both the heartbeat UPDATE, same database, same node, zero agent churn.

The fix

Option 2 from the issue — move the subscription off the constructor — funnelled at the points where the daemon actually takes ownership of a shard rather than at one public method, so it also covers "a caller deliberately starts a second daemon":

An unstarted daemon now creates no subscription, no Block, and no connection — the writer object isn't even built.

Tests

src/EventTests/Daemon/ExtendedProgressionSubscriptionLifecycleTests.cs — 8 tests over a shared tracker and a write-counting IEventDatabase: an unstarted daemon writes nothing, five unstarted daemons still write nothing, a started daemon does write, one owner + three ad-hoc readers produce exactly one write per publication (not four), starting twice doesn't double-write, a stopped daemon stops, a restarted daemon resumes, and disposing a never-started daemon is clean. Full EventTests suite passes (686).

Not addressed here

The two related items in the issue belong to the cost model in #622 and are handled there: HeartbeatWriteInterval having no configuration path, and _lastFlush starting at MinValue so a fresh writer flushes on its first publication. The latter is reasonable now that a writer only exists on a genuine daemon start.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Fuk1GybEEmohFmboJuM4Po

…ion (#621)

Every JasperFxAsyncDaemon subscribed an ExtendedProgressionWriter to its
database's SHARED ShardStateTracker in the constructor -- including daemons
built ad hoc purely to read state, and daemons that are never started. N
daemons for one database meant N writers on one publication stream, each
renting its own connection and issuing the same UPDATE against the same
rows. Building a daemon is a documented way to inspect projection state
(BuildProjectionDaemonAsync is a fresh instance per call, no caching), so a
caller wanting CurrentAgents() got a background write loop it never asked
for and, without disposing, kept.

Option 2 from the issue, funnelled at the points where the daemon actually
takes ownership of a shard rather than at a single public method:

- The subscription (and the writer itself) is armed by
  armExtendedProgressionWriter() from StartHighWaterDetectionAsync,
  tryStartAgentAsync, rebuildAgent and PrepareForRebuildsAsync. Idempotent,
  so repeated starts can't stack a second subscription on the shared tracker.
- StopAllAsync unsubscribes and drains as before, but no longer resubscribes;
  the next start re-arms a fresh writer. #557's "resume after a
  rebuild keeps persisting" guarantee is preserved, just moved.
- Dispose() handles the never-armed case.

An unstarted daemon now writes nothing at all -- no subscription, no Block,
no connection.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Fuk1GybEEmohFmboJuM4Po
@jeremydmiller
jeremydmiller merged commit 5c3844b into main Aug 4, 2026
1 check passed
@jeremydmiller
jeremydmiller deleted the gh/621-extended-progression-lifecycle branch August 4, 2026 12:11
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.

Every JasperFxAsyncDaemon subscribes an ExtendedProgressionWriter to the shared per-database tracker, including read-only ad-hoc daemons

1 participant