Skip to content

[v2] Fresh databases skip legacy auth.json credential import #51342

Closed

Description

@bjorntechCarl

Description

Actual: With a valid legacy auth.json already present, fresh V2 database bootstrap does not import its OpenAI OAuth credential. A native regression test reads undefined for the credential row. Restarting does not import it either.

Expected: The credential is imported during first initialization and appears among saved accounts. The synthetic test credentials are not expected to authenticate.

Cause: Fresh bootstrap runs schema.up(tx) and records all migration IDs without calling their up functions. Thus the existing legacy credential importer is skipped and then marked complete. A minimal fix is to run that importer's up(tx) immediately after schema.up(tx) in the fresh-bootstrap transaction, before recording migration IDs. The attached candidate patch additionally makes malformed, non-object legacy files non-fatal and includes regression coverage.

On native source commit c1c9a139934a707636e59278e18ef3ec4ac763b9, the tests-only patch reproduced the failure (missing row, exit 1); the full candidate passed all 20 migration tests and bun typecheck in packages/core. The fix was also checked against pinned 2.0.6 source (b084acc55ea2cdb50e9c2ec49a8d9ab3608d43ad): 21 focused migration tests and packages/core typecheck passed. These are local database tests, not a verified live OAuth login, provider request, or token refresh.

This fix helps only databases initialized after the legacy file is in place. Already-initialized databases need a separate safe recovery path; resetting an existing database risks user data. Related discussion: #40920 (closed, unmerged).

Plugins

No plugins

OpenCode version

2.0.16

Steps to reproduce

  1. Use a clean upstream source checkout with Bun and the candidate patch attached to this issue. Set PATCH to the downloaded patch's absolute path (if GitHub will not accept .patch, attach a .txt copy; its contents remain the same). These commands create a disposable worktree; they do not reset an existing database. No Docker, sandbox, account, or provider access is needed.

  2. Run the regression test with only the test file applied. It creates a temporary legacy auth.json containing synthetic OpenAI OAuth fields, bootstraps an in-memory database, and queries its credential row. The core test runner isolates HOME/XDG paths.

    PATCH=/absolute/path/to/opencode-v2-sbx-auth.patch
    tmp=$(mktemp -d)
    git worktree add --detach "$tmp/opencode" c1c9a139934a707636e59278e18ef3ec4ac763b9
    cd "$tmp/opencode"
    bun install --frozen-lockfile
    git apply --include='packages/core/test/database-migration.test.ts' "$PATCH"
    cd packages/core
    bun run test test/database-migration.test.ts --test-name-pattern 'imports legacy credentials during fresh schema bootstrap'
    # Expected RED: query returns undefined instead of the OpenAI row (exit 1).
  3. Apply the production fix and remaining test changes to the same disposable worktree; rerun the migration tests and typecheck:

    cd ../..
    git apply --exclude='packages/core/test/database-migration.test.ts' "$PATCH"
    cd packages/core
    bun run test test/database-migration.test.ts
    bun typecheck
    # Expected GREEN: 20 migration tests and typecheck pass at this source commit.

The adjusted pre-existing importer test creates its fixture after bootstrap to preserve its original migration-only assertion. The new regression also checks repeated bootstrap and malformed legacy input. Do not use real OAuth credentials in the fixture.

opencode-v2-sbx-auth.patch
test-sbx-auth.sh

Screenshot and/or share link

No screenshot or share link: this is a headless native database regression. The attached patch contains the reproducible test

Operating System

macOS 26.5.1 (Apple silicon, arm64)

Terminal

Not terminal-specific but using Warp

Activity

  1. Starefossen commented on Oct 8, 2026

    @Starefossen

    Also on 2.0.24 (macOS arm64). If you delete the 20260805200742_import_legacy_credentials row from migration and restart, the credentials import, so only the fresh-bootstrap path (migration.ts:31-47) skips it. Workaround: opencode auth import with the auth export format.

  2. opencodejr commented on Oct 28, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report. I reproduced this on 2.0.16, and it still happens on the latest dev (9adc42b).

    What I did, with isolated HOME/XDG directories:

    1. Put a fake v1-format auth.json (an OpenAI OAuth entry and an Anthropic API key) in $XDG_DATA_HOME/opencode/ before any database existed.
    2. Ran opencode auth list --standalone twice.

    Both runs printed No authenticated integrations. The new database has no credential rows, but 20260805200742_import_legacy_credentials is already recorded as completed in the migration table, so later starts never retry the import. The cause is the one you describe: on an empty database, apply() in packages/core/src/database/migration.ts creates the schema and records every migration ID without running any migration's up.

    The script below reproduces it against the installed opencode, or against a source checkout via OC_DEV. Related: #40920 (closed, unmerged), which notes the same fresh-bootstrap behaviour for a different credential import.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Reproduces #51342: a legacy auth.json present before the first V2 run is
    # not imported into a freshly bootstrapped database.
    # Usage: ./repro.sh                 (uses `opencode` from PATH)
    #        OC_DEV=/path/to/checkout ./repro.sh   (runs from source)
    set -u
    root=$(mktemp -d /tmp/opencode/repro-XXXX)
    export HOME="$root/home" XDG_DATA_HOME="$root/data" XDG_CONFIG_HOME="$root/config" \
      XDG_STATE_HOME="$root/state" XDG_CACHE_HOME="$root/cache"
    mkdir -p "$HOME" "$XDG_DATA_HOME/opencode" "$XDG_CONFIG_HOME" "$XDG_STATE_HOME" "$XDG_CACHE_HOME"
    mkdir -p "$root/project" && cd "$root/project"
    
    # Synthetic (fake) legacy OAuth credential, v1 auth.json format.
    cat > "$XDG_DATA_HOME/opencode/auth.json" <<'EOF'
    {
      "openai": {
        "type": "oauth",
        "refresh": "fake-refresh",
        "access": "fake-access",
        "expires": 4102444800000,
        "accountId": "acct_fake"
      },
      "anthropic": { "type": "api", "key": "sk-fake-key" }
    }
    EOF
    
    oc() {
      if [ -n "${OC_DEV:-}" ]; then
        BUN_OPTIONS="--conditions=browser --preload=$OC_DEV/packages/tui/node_modules/@opentui/solid/scripts/preload.js" timeout 120 bun --conditions=browser --preload="$OC_DEV/packages/tui/node_modules/@opentui/solid/scripts/preload.js" \
          "$OC_DEV/packages/cli/src/index.ts" "$@"
      else
        timeout 90 opencode "$@"
      fi
    }
    
    echo "== version: $(oc --version)"
    for run in 1 2; do
      echo "== run $run: opencode auth list --standalone"
      oc auth list --standalone </dev/null 2>&1 | tail -20
    done
    
    db=$(ls "$XDG_DATA_HOME"/opencode/*.db 2>/dev/null | head -1)
    echo "== database: $db"
    bun -e '
    const { Database } = require("bun:sqlite")
    const db = new Database(process.argv.at(-1), { readonly: true })
    console.log("credential rows:", JSON.stringify(db.query("SELECT integration_id FROM credential").all()))
    console.log("import migration recorded:", JSON.stringify(db.query("SELECT id FROM migration WHERE id LIKE ?").all("%import_legacy_credentials")))
    ' "$db"
    # leave service running? stop any background service started for this HOME
    oc service stop >/dev/null 2>&1 || true

    Run it: bash repro.sh

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions