Only a daemon that actually runs agents writes extended progression (#621) - #625
Merged
Merged
Conversation
…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
This was referenced Aug 5, 2026
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.
Closes #621.
The problem
Trackeris per-database and shared. EveryJasperFxAsyncDaemonsubscribed anExtendedProgressionWriterto 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 sameUPDATE ... FROM unnest(...)against the same rows.That includes daemons built purely to read.
BuildProjectionDaemonAsyncisnew ProjectionDaemon(...)per call with no caching, and nothing in its signature suggests the returned object owns a background write loop against the database. CritterWatch'sEventProgressionPollerdid 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 inpg_stat_activitywere both the heartbeatUPDATE, 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":
armExtendedProgressionWriter()builds and subscribes the writer, called fromStartHighWaterDetectionAsync,tryStartAgentAsync(the funnel every continuous start path goes through),rebuildAgentandPrepareForRebuildsAsync. Idempotent — repeated starts can't stack a second subscription on the shared tracker.StopAllAsyncunsubscribes and drains exactly as before, but no longer resubscribes at the end; the next start re-arms a fresh writer. Daemon shutdown doesn't drain extended-progression writer or barrier the batch-write throttle → late telemetry clobbers rows + ObjectDisposedException on SemaphoreSlim #557's "resume after a rebuild keeps persisting extended progression" guarantee is preserved, just relocated.Dispose()handles the never-armed case.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-countingIEventDatabase: 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. FullEventTestssuite passes (686).Not addressed here
The two related items in the issue belong to the cost model in #622 and are handled there:
HeartbeatWriteIntervalhaving no configuration path, and_lastFlushstarting atMinValueso 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