Skip to content

fix(agent): route agent start through the daemon with round-trip verification - #403

Closed
codeaholicguy wants to merge 1 commit into
mainfrom
fix/opencode-spawned-session-names
Closed

codeaholicguy wants to merge 1 commit into
mainfrom
fix/opencode-spawned-session-names

Conversation

@codeaholicguy

@codeaholicguy codeaholicguy commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

Closes #402.

Confirmed root cause

Split-brain identity stores: agent start writes agents.db under the starting process's $HOME while devkitd (socket + data dir) resolves under its own HOME. Under a nested/sandboxed start (HOME=/tmp/fakehome, overlay envs), the write lands where no client looks — the sweep synthesizes <cwd>-<pid> (slug-lib-75800), send --id can't resolve the name, and start reports a false success. Harness-agnostic; plausibly shares a root with #400.

Fix (scoped per review)

  • Core — agent.start RPC in devkitd (crates/devkitd/src/agent_start.rs): the daemon validates (name regex, name-in-use against agents.db + kill(pid,0) liveness, cwd exists), creates the tmux session, polls the pane tree for the deepest harness pid (ported findAgentPid/pollForPid semantics), writes the registry row under its own store (schema-v1 columns so TS migrations take a fresh db forward), and returns the entry. Because the write happens inside the daemon's sane view, both HOME-divergence and write-overlay flavors are covered.
  • CLI agent start is a thin client on the tmux provider path: calls agent.start when the socket answers; falls back to local startAgent when the daemon is absent or predates the method. Daemon-side errors surface as command failures.
  • Round-trip verification + loud failure (both paths): the local fallback now refuses loudly with AgentHomeDivergenceError before creating any session when $HOME diverges and the account .ai-devkit store exists, warns when divergent-but-self-consistent, and re-reads the registry write before reporting success — a failed round-trip is an error, not a false OK. The daemon path's registry write is authoritative by construction; its errors propagate.
  • Autostart guard: ensureDaemon never auto-spawns a second daemon from a divergent HOME ($HOME vs passwd home) — it refuses with a loud diagnostic instead of spawning an invisible twin.
  • unregistered:<folder>-<pid>: synthesized identities in the listAgents merge are marked self-explaining instead of masquerading as assigned names.
  • Defenses kept: unique type:sessionId → type:cwd re-home on pid drift (generic, all harnesses); dropDescendantProcesses folds spawned child procs into the parent session (TS + devkitd); opencode session.title read tolerantly, surfaced as summary/firstUserMessage metadata only.

Deferred to phase 2 per review: HOME-independent socket path chain ($XDG_RUNTIME_DIR → /tmp → $HOME). Socket stays $HOME/.ai-devkit/daemon.sock as today.

Evidence

  • agent_start.rs: name-validation parity with AGENT_NAME_REGEX.
  • ManagedAgentRuntime.test.ts: refuses under divergent HOME when the account store exists (no session created), warns+proceeds when self-consistent, fails when the registry write doesn't round-trip — the fake-HOME repro shape, at unit level: via-daemon succeeds with the name or fails loudly, never folder-PID fallback.
  • AgentManager.test.ts: registry name survives detected-pid drift, sessionId re-home, ambiguity/no-steal guards, unregistered: expectations, resolveAgent resolves the adopted name.
  • fixtures/harness/opencode/spawned.json parity bundle (TS + rust): spawned child folds into parent; slug-lib-75800 title stays metadata.

Test plan

  • Scoped: agent-manager 140, daemon-client 14, cargo test -p devkitd 7 — green
  • npm run lint, npm test (8 projects), npm run test:e2e (42) — green on hook runs (unrelated ClaudePrintAgentService cold-import flake noted)

Generated with Devin

@codeaholicguy
codeaholicguy force-pushed the fix/opencode-spawned-session-names branch 2 times, most recently from ba933d9 to 84972f2 Compare October 17, 2026 06:15
@codeaholicguy codeaholicguy changed the title fix(agent): opencode agents must keep their assigned name fix(agent): route agent start through the daemon with HOME-independent socket resolution Oct 17, 2026
Closes #402.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@codeaholicguy
codeaholicguy force-pushed the fix/opencode-spawned-session-names branch from 84972f2 to 5be480d Compare October 17, 2026 06:59
@codeaholicguy codeaholicguy changed the title fix(agent): route agent start through the daemon with HOME-independent socket resolution fix(agent): route agent start through the daemon with round-trip verification Oct 17, 2026
@codeaholicguy
codeaholicguy deleted the fix/opencode-spawned-session-names branch October 17, 2026 07:24
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(agent): nested agent start loses assigned name — list/send fall back to folder-PID identity

1 participant