Skip to content

fix: register detached runs in DB at launch (#2)#84

Merged
mattleaverton merged 1 commit into
danshapiro:mainfrom
mattleaverton:fix/2-register-detached-runs-in-db
Apr 24, 2026
Merged

fix: register detached runs in DB at launch (#2)#84
mattleaverton merged 1 commit into
danshapiro:mainfrom
mattleaverton:fix/2-register-detached-runs-in-db

Conversation

@mattleaverton
Copy link
Copy Markdown
Collaborator

Summary

From docs/plans/2026-04-24-kilroy-fixes-from-feedback.md item #2.

Bug: kilroy attractor run --detach forked a child and exited before any DB row was written. Until the child reached engine.RunWithConfigRecordRunStart, every query (runs list, runs show <id>, runs wait --latest --label …) returned "not found." Callers had to capture logs_root= from stdout and poll final.json by hand.

Fix: Added registerDetachedRunInDB() (new file cmd/kilroy/run_detach_db.go), called in the if detach { branch of attractorRun() just before launchDetached(). Writes status="running" with labels, inputs, invocation, repoPath before the parent exits. The child's existing RecordRunStart() uses INSERT OR REPLACE so full metadata (worktreeDir, runBranch) lands once the engine starts. CompleteRun() updates to the terminal status as before.

Test plan

  • TestRegisterDetachedRunInDB_AppearsBefore_TerminalState — covers the GetRun and ListRuns(Labels: ...) paths (exactly what runs wait --latest --label KEY=VALUE does).
  • TestRegisterDetachedRunInDB_FallsBackToFilename — falls back gracefully when the graph file is missing.
  • go test ./cmd/kilroy/... passes.

Risks / follow-ups

  • Timestamp drift: Parent writes started_at = time.Now(); child's INSERT OR REPLACE resets it to the child's start time (ms later). Could use INSERT OR IGNORE in the engine path to preserve the parent's timestamp. Minor.
  • Silent DB failure: registerDetachedRunInDB logs a warning on stderr but doesn't fail the launch, consistent with the existing best-effort DB design. If the DB is corrupt the launch succeeds but the run stays invisible.

Context

Produced by a dogfood quick-launch run against this repo — the engine's own worktree isolation (PR #83) was validated end-to-end by this process.

🤖 Generated with Claude Code

Before this change, running with --detach forked a child process and
exited immediately. The run row was only written to the run database when
the child's engine.RunWithConfig path reached RecordRunStart — which
could be seconds or minutes after the parent exited.

Consequences: `runs list`, `runs show <id>`, and
`runs wait --latest --label ...` all failed to find the run until it
reached a terminal state, because the DB had no row for it yet. Users
had to capture logs_root= from launch stdout and poll final.json by hand.

The fix adds registerDetachedRunInDB() (cmd/kilroy/run_detach_db.go),
which is called in the detach branch of attractorRun() — after paths are
resolved, before launchDetached() forks the child. It reads the graph
file to extract graphName/goal/dotSource (best-effort; falls back to
the filename stem if parsing fails), then inserts a row with
status=running and all available metadata (labels, inputs, invocation,
logs_root) via rundb.InsertRun.

The child's engine path later calls InsertRun (INSERT OR REPLACE), which
overwrites the parent's row with full metadata (worktreeDir, runBranch,
etc.); status remains running until CompleteRun sets the terminal value.

Added tests in cmd/kilroy/run_detach_db_test.go:
  - TestRegisterDetachedRunInDB_AppearsBefore_TerminalState: writes the
    pre-registration row, then asserts GetRun and ListRuns with a label
    filter both find the run with status=running — exactly the path used
    by `runs wait --latest --label KEY=VALUE`.
  - TestRegisterDetachedRunInDB_FallsBackToFilename: verifies that when
    the graph file is missing the row is still written using the
    filename stem as graph_name.
@mattleaverton mattleaverton merged commit a019bce into danshapiro:main Apr 24, 2026
1 check failed
@mattleaverton mattleaverton deleted the fix/2-register-detached-runs-in-db branch April 27, 2026 17:57
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