Skip to content

fix(drive): atomically hand off detached start ownership#44

Merged
kitlangton merged 2 commits into
mainfrom
fix/concurrent-start-ownership
Jul 20, 2026
Merged

fix(drive): atomically hand off detached start ownership#44
kitlangton merged 2 commits into
mainfrom
fix/concurrent-start-ownership

Conversation

@kitlangton

@kitlangton kitlangton commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

What

Prevent concurrent detached start commands from spawning competing daemon processes for the same named instance, covering both automatic initialization and init-prepared artifacts.

Before / After

Before: Two launchers could reuse the same initialized manifest before either daemon registered. Both spawned detached owners; the loser could remain in the 60-second startup loop while the integration test timed out after 30 seconds with exit code 130. Prepared manifests had the same unclaimed race, and overlapping artifact cleanup could then fail.

After: Exactly one launcher atomically claims the initialized manifest and may transfer that claim to its daemon. Competing launchers fail immediately with already starting. Matching daemon adoption survives the launcher exiting first, and dead claims on prepared manifests are released without replacing their artifacts.

How

  • src/instance/registry.ts records a launcher PID under the registry lock, adopts a matching daemon before stale-owner pruning, persists dead prepared-claim release, and rechecks ownership during registration.
  • src/cli/start.ts passes the claim through a one-shot internal environment variable, removes it before runtime children are created, and relies on the registry claim rather than a redundant pre-spawn scan.
  • test/cli/integration.test.ts synchronizes on the ready manifest and launcher exits, covers automatic and prepared starts independently, force-cleans an unready detached owner, and removes each tracked temporary root once.
  • test/fixtures/fake-opencode.ts replaces a 500 ms recording-test lifetime with an event-driven exit after LLM attachment.
  • Adds a patch changeset for opencode-drive.
sequenceDiagram
  participant A as Launcher A
  participant B as Launcher B
  participant R as Registry
  participant D as Daemon

  par concurrent start
    A->>R: claim name with launcher PID
  and
    B->>R: claim name with launcher PID
  end
  R-->>A: claim granted
  R--xB: already starting
  A->>D: spawn with initializer PID
  D->>R: adopt matching claim
  R-->>D: transfer ownership
  D->>R: register ready instance
Loading

Scope

Limited to detached launcher/daemon ownership, dead prepared-claim recovery, and integration-harness determinism. No CLI command or simulation protocol shapes change.

Testing

  • Reproduced the old concurrent-start failure in 1 of 10 stress runs.
  • Automatic and prepared ownership stress passed 10 rounds each before simplification; the final simplified harness passed another 10/10 combined stress runs.
  • bun run release:validate: 202 Effect tests, 57 CLI tests, lint, typecheck, and 95-file package dry-run passed.
  • Final exact-tree bun run test:cli: 57/57 passed after the persisted-release fix.
  • Final exact-tree bun run check and git diff --check passed.

@kitlangton
kitlangton merged commit 6a8d52b into main Jul 20, 2026
1 check passed
@kitlangton
kitlangton deleted the fix/concurrent-start-ownership branch July 20, 2026 18:13
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