Skip to content

fix(devkitd): discard stale sweep commits (#493) - #505

Open
codeaholicguy wants to merge 2 commits into
mainfrom
cursor/fix-sweep-race-493-6d0b
Open

codeaholicguy wants to merge 2 commits into
mainfrom
cursor/fix-sweep-race-493-6d0b

Conversation

@codeaholicguy

@codeaholicguy codeaholicguy commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Closes #493

Root cause

The 2s timer and RPC handlers all call apply_sweep() with no ordering on the commit step. A timer sweep can run discovery before a new agent exists, spend time on enrichment without holding any sweep-wide lock, then commit after agent.start has already applied a fresh snapshot. That stale commit tombstones the live row in daemon.db, replaces the enriched cache, and emits a phantom agent.disappeared event. TLC counterexample on branch feature-tla-analysis (Inv_NoPhantomDisappeared).

Fix

Each apply_sweep takes a monotonic sequence number at entry. The commit phase (enriched cache, apply_agent_snapshot, lifecycle events) runs under sweep_mutex and is skipped when my_seq < sweep_applied, so an older sweep cannot land after a newer one. The 2s timer uses try_apply_sweep() and bails out when sweep_in_flight is already non-zero, so timer sweeps do not queue up behind slow RPC-triggered passes. Enrichment still happens outside the enriched RwLock; start_mutex is untouched. No changes to daemon startup or autostart (#495).

Regression test

stale_sweep_commit_does_not_tombstone_live_registration uses a SweepCommitPause stored on the target Daemon only (not a process-global flag). A stale timer-shaped apply_sweep pauses at commit; the test registers the agent and runs a full second apply_sweep() whose seq comes from sweep_seq, using discover_override to fake ps seeing the pid (no codex fixture in this crate). When overrides are used, the sweep skips durable reconcile and session file resolution and builds minimal enriched rows so the test stays fast.

Guard check: Removing the stale discard block in commit_sweep makes this test fail (tombstone assertion). The same test also failed on the first commit before the production fix landed.

Test plan

  • cargo test -p devkitd stale_sweep
  • cargo test --workspace
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo fmt -- --check

Risks

Stale commit drops are quiet aside from a trace log. A skipped timer tick defers discovery by up to one 2s interval when another sweep is still running. Expect a small merge conflict with #503 if both touch serve() / test_daemon() in server.rs; this branch does not rebase onto #503.

Behavior changes

For Hoang: the periodic discovery tick may skip one 2s cycle when a sweep is already running. No CLI, RPC, or event schema changes.

Overlap with open PRs

No overlap with #489 (durable.rs only). #481 edits listed_agent() and the agent.lookup / agent.unregister dispatch arm; this PR only touches apply_sweep, the timer spawn, and adjacent tests.

AI DevKit skills used

dev-lifecycle, tdd, humanizer

Tooling friction

Long-running or stuck devkitd test binaries from earlier harness iterations can hold locks; pkill the test binary if a sweep test hangs. Pre-commit failed once on flaky stop_and_restart_adoption_do_not_double_launch on the first commit; retry passed.

Open in Web Open in Cursor 

cursoragent and others added 2 commits November 27, 2026 16:07
Serialize sweep commits with a monotonic id so a timer sweep that
started before agent.start cannot overwrite a fresher post-start snapshot.
Timer ticks use try_apply_sweep to skip while another sweep is in flight.

Closes #493
Use per-daemon commit pause hooks, sync_channel(1), and release
sweep_commit_pause before blocking. Drive the fresh sweep through real
apply_sweep with discover_override for deterministic ps.

Co-authored-by: Hoang Nguyen <codeaholicguy@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

fix(devkitd): stale timer sweep races RPC sweeps — tombstones live agents, wipes enriched cache, phantom disappeared events (Inv_NoPhantomDisappeared)

2 participants