Skip to content

Migration 20260812213948_worktree aborts boot on orphaned project_directory rows (FK constraint failed) #55583

Open

Description

@danielbiller

Description

Version

opencode-ai 1.19.0 (also affects any build containing migration 20260812213948_worktree, incl. v2 line)

Symptom

Every launch fails immediately with only this client-side error:
Error: Standalone server exited before reporting readiness
The real cause is visible solely in the server log
(~/.local/share/opencode/log/opencode.log).

Server log

timestamp=... level=ERROR message="database migration failed"
migration=20260812213948_worktree ... error="... SQLiteError:
FOREIGN KEY constraint failed" role=server
(followed by the CLI-side "cli process failed ... Standalone server
exited before reporting readiness")

Root cause

The migration bulk-inserts project_directory rows into the new
worktree table with no orphan guard. Any project_directory row whose
project_id has no parent project row (mine: 2 stale rows under
/tmp/opencode/difftest-*, project long deleted, 0 sessions and
0 messages referencing them) violates worktree.project_id's foreign
key, aborting the entire migration — and with it every server boot,
deterministically. Fresh databases never hit this, which is why it
survives CI.

Repro

  1. On a working install, insert one orphan row:
    INSERT INTO project_directory(project_id, directory, time_created)
    VALUES ('deadbeef','/tmp/gone', <now_ms>);
    (Backup opencode.db + wal/shm first.)
  2. Start opencode → immediate readiness error; server log shows the
    migration FK failure above.

Suggested fix

Guard the migration INSERT with a parent check, e.g.
WHERE EXISTS (SELECT 1 FROM project WHERE project.id = ...),
and/or consider surfacing migration failures distinctly instead of
the generic "exited before reporting readiness", which sent me
chasing container/network causes for a database-content problem.

Workaround (verified)

Back up opencode.db + -wal + -shm, then:
DELETE FROM project_directory WHERE project_id NOT IN (SELECT id FROM project);
Server boots and the migration completes.

Plugins

No response

OpenCode version

2.0.29

Steps to reproduce

  1. On a working install, insert one orphan row:
    INSERT INTO project_directory(project_id, directory, time_created)
    VALUES ('deadbeef','/tmp/gone', <now_ms>);
    (Backup opencode.db + wal/shm first.)
  2. Start opencode → immediate readiness error; server log shows the
    migration FK failure above.

Screenshot and/or share link

No response

Operating System

Windows 11 WSL Ubuntu

Terminal

Windows Terminal

Activity

  1. opencode-agent commented on Oct 15, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed report and the workaround. I reproduced this on 2.0.29, and it still happens on the latest dev (328f74c).

    Steps:

    1. Let OpenCode create a fresh database.
    2. Roll it back to just before migration 20260812213948_worktree and insert one project_directory row whose project_id has no project row ('deadbeef', /tmp/gone).
    3. Start OpenCode again.

    The CLI only prints Error: Standalone server exited before reporting readiness. The server log shows database migration failed migration=20260812213948_worktree ... SQLiteError: FOREIGN KEY constraint failed. The migration copies every project_directory row into worktree and doesn't check that the project still exists. The script below reproduces it in separate data directories.

    Until this is fixed, your workaround is the way around it: back up the database first, then run DELETE FROM project_directory WHERE project_id NOT IN (SELECT id FROM project);.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Reproduces #55583: migration 20260812213948_worktree aborts on an orphaned
    # project_directory row (FOREIGN KEY constraint failed), so the server never boots.
    # Usage: ./repro.sh            -> uses `opencode` from PATH
    #        OC_DEV=/path/to/repo ./repro.sh  -> runs the CLI from a checkout
    set -u
    ROOT=$(mktemp -d /tmp/opencode/repro55583.XXXX)
    export XDG_DATA_HOME=$ROOT/data XDG_CONFIG_HOME=$ROOT/cfg XDG_STATE_HOME=$ROOT/state XDG_CACHE_HOME=$ROOT/cache
    if [ -n "${OC_DEV:-}" ]; then
      # BUN_OPTIONS so the spawned standalone server gets the same flags
      export BUN_OPTIONS="--conditions=browser --preload=$OC_DEV/packages/tui/node_modules/@opentui/solid/scripts/preload.js"
      OC=(bun "$OC_DEV/packages/cli/src/index.ts")
    else
      OC=(opencode)
    fi
    echo "== version: $("${OC[@]}" --version)"
    
    echo "== 1. first boot creates a fresh database"
    timeout 90 "${OC[@]}" models --standalone >/dev/null 2>&1 || true
    DB=$(ls $XDG_DATA_HOME/opencode/opencode*.db 2>/dev/null | head -1)  # opencode-local.db when run from source
    [ -n "$DB" ] && [ -f "$DB" ] || { echo "no database created"; exit 2; }
    
    echo "== 2. roll the DB back to just before the worktree migration and add an orphan project_directory row"
    python3 - "$DB" <<'EOF'
    import sqlite3, sys, time
    c = sqlite3.connect(sys.argv[1])
    c.execute("PRAGMA foreign_keys = OFF")  # how such orphans can exist in older DBs
    c.execute("DROP TABLE worktree")
    c.execute("DELETE FROM migration WHERE id = '20260812213948_worktree'")
    c.execute("INSERT INTO project_directory(project_id, directory, time_created) VALUES ('deadbeef', '/tmp/gone', ?)", (int(time.time()*1000),))
    c.commit()
    print("orphans:", c.execute("SELECT count(*) FROM project_directory WHERE project_id NOT IN (SELECT id FROM project)").fetchone()[0])
    EOF
    
    echo "== 3. boot again"
    timeout 90 "${OC[@]}" models --standalone --print-logs >"$ROOT/out.txt" 2>&1
    echo "exit code: $?"
    grep -E "Standalone server exited|database migration failed|FOREIGN KEY" "$ROOT/out.txt" | cut -c1-400
    grep -rhE "database migration failed|FOREIGN KEY" "$XDG_DATA_HOME/opencode/log" 2>/dev/null | cut -c1-400 | head -3

    Run it: bash repro.sh

  2. fariello commented on Nov 12, 2026

    @fariello

    Just happened to me on two of three different servers. Still not fixed in the latest update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions