Skip to content

project: GET /api/project returns 500 when a project row is persisted with commands = NULL #63076

Open

Description

@motusakapeppo

Summary

GET /api/project returns 500 with Expected Project.Commands | undefined whenever any row in the project table is persisted with an invalid commands value (NULL, or the legacy [] array shape). The failure is total: the client cannot list or switch projects, and opening a new folder in the desktop app fails with UnexpectedStatus: 500. Creating a session in a directory that has no project row yet persists commands as SQL NULL, so the database poisons itself over time.

Environment

  • opencode version: 2.2.5 (desktop app, bundled cli\2.2.5-x64\opencode-cli.exe; /api/info reports 2.2.5). Note: a second, older CLI build (1.19.5) is also installed on this machine and shares the same database.
  • OS: Windows 10.0.26300 (win32 x64)
  • Terminal: desktop app (Electron renderer); reproduction done over HTTP against the app's own local server
  • Shell: cmd.exe / PowerShell (C:\Windows\system32\cmd.exe)
  • Install/channel: desktop app release build, version 2.2.5
  • Active plugins: [email protected], ./ext/voce

Reproduction

  1. Pick a directory that has no row in project yet (here: an empty temp directory).
  2. Create a session there:
    POST /api/session with {"location":{"directory":"<fresh-dir>"},"title":"probe"} → 200
  3. Inspect the database (C:\Users\<user>\.local\share\opencode\opencode.db):
    select id, worktree, commands, typeof(commands) from project where worktree like '%<fresh-dir>%';
    -- id | <fresh-dir> | (empty) | null      <-- persisted as SQL NULL
    Pre-existing rows created by older builds hold '[]' instead.
  4. GET /api/project → 500
    {"_tag":"UnknownError","message":"Server failed to encode its response. Check server logs for details.","ref":"err_…"}
    Server log:
    level=ERROR message="failed to encode response" kind=Body endpoint=server.project.t
    route="GET /api/project" reason="Expected Project.Commands | undefined\n  at [0][\"commands\"]"
    http.url=/api/project http.status=500
    
  5. Repair the data:
    UPDATE project SET commands='{}' WHERE commands IS NULL OR commands NOT LIKE '{%';
    → GET /api/project returns 200 with a valid array; GET /api/session also stops returning 500.

The index in the error message moves with the repair, which isolates the field: setting commands='{}' on a single project row moved the failure from at [0]["commands"] to at [1]["commands"].

Expected Behavior

POST /api/session (or any internal "ensure project for directory" path) should persist commands in a shape its own API can read back, i.e. {}, or the reader should treat NULL as "no commands" instead of rejecting the whole response. A single unreadable row should not take down the entire project list endpoint.

Actual Behavior

  • New project rows are written with commands = NULL.
  • The in-memory project object built during creation omits the key, so the instance that created it keeps working — the 500 only appears once the row is read back from disk (app restart, or a list rebuilt from the database). This makes it look intermittent and very hard to attribute.
  • Legacy rows written by older builds contain [] (array), which is also invalid for the current schema, so an upgrade without a data migration breaks the endpoint for every existing project.

Additional Context

  • The response schema (/openapi.json) declares Project.Commands as an object with a single optional property start, and commands is not in Project's required list — so the encoder accepts an object or an absent value, but rejects null and rejects an array. The column is nullable text with no default (ALTER TABLE project ADD COLUMN commands text), which is why inserts that do not set it produce NULL.
  • Two different opencode builds share one database on this machine (desktop 2.2.5 and CLI 1.19.5), so the mixed []/NULL contents also point at missing compatibility between the shapes written by different versions.
  • Impact seen by the user: after this state was reached, the desktop app showed Richiesta non riuscita / UnexpectedStatus: 500 every time another folder was selected; existing sessions kept working, which made it look like a per-folder permission problem rather than a global project-list failure.
  • Local workaround applied: UPDATE project SET commands='{}' WHERE commands IS NULL OR commands NOT LIKE '{%' on 78 rows. No other data was touched.

Activity

  1. opencodejr commented on Nov 10, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report. I reproduced the 500 on 2.2.5, and it still happens on the latest dev (57719d7). The cause turned out a little different from what the report suggests:

    • Creating a session in a fresh directory does store commands as SQL NULL, but that's handled: GET /api/project kept returning 200, even after restarting the server.
    • What breaks it is a row whose commands is the legacy array '[]'. With one such row, GET /api/project returns 500 with Expected Project.Commands | undefined at [i]["commands"], so that single row takes down the whole list. The v1 data import copies commands across unchecked, which fits your setup with a 1.19.5 CLI on the same database.
    • GET /api/session stayed 200 in my runs, so I couldn't confirm that part.

    The script below reproduces it against opencode serve with an isolated data directory. Your UPDATE ... SET commands='{}' workaround is a reasonable stopgap until this is addressed.

    Related: #61025 is the same failure pattern with an unparseable worktree value, and #61028 is a PR that skips projects with an invalid stored worktree when listing.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Reproduce #63076: GET /api/project 500s when a project row has a non-object `commands`.
    # Usage: ./repro.sh            (uses `opencode` from PATH)
    #        OC="bun ... /workspace/repo/packages/cli/src/index.ts" ./repro.sh
    set -u
    OC=${OC:-opencode}
    PORT=${PORT:-4999}
    ROOT=$(mktemp -d /tmp/opencode/oc63076.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" "$ROOT/fresh1" "$ROOT/fresh2"
    URL=http://127.0.0.1:$PORT
    export OPENCODE_PASSWORD=repro-pass
    sql() { bun -e 'const {Database}=require("bun:sqlite");const d=new Database(process.argv[1]);const q=process.argv[2];console.log(/^\s*select/i.test(q)?d.query(q).all():d.run(q))' "$1" "$2"; }
    
    start() { (cd "$ROOT" && $OC serve --port $PORT --hostname 127.0.0.1 >>"$ROOT/server.log" 2>&1 &) ; for i in $(seq 60); do curl -u opencode:repro-pass -sf $URL/api/project >/dev/null 2>&1 && return; curl -u opencode:repro-pass -s $URL/api/project -o /dev/null -w '%{http_code}' 2>/dev/null | grep -q 500 && return; sleep 1; done; }
    stop() { pkill -f "serve --port $PORT" ; sleep 2; }
    req() { echo "--- $1 $2"; curl -u opencode:repro-pass -s -X "$1" "$URL$2" -H 'content-type: application/json' ${3:+-d "$3"} -w '\nHTTP %{http_code}\n'; }
    
    start
    req POST /api/session "{\"location\":{\"directory\":\"$ROOT/fresh1\"},\"title\":\"probe\"}" | tail -1
    req POST /api/session "{\"location\":{\"directory\":\"$ROOT/fresh2\"},\"title\":\"probe\"}" | tail -1
    req GET /api/project | tail -1
    stop
    DB=$(find "$XDG_DATA_HOME" -name '*.db' | head -1)
    echo "DB: $DB"
    sql "$DB" "select id, worktree, quote(commands), typeof(commands) from project;"
    
    echo "=== after restart, NULL rows as written by the server"
    start; req GET /api/project | tail -1; stop
    
    for v in "'[]'" "'null'"; do
      echo "=== set commands=$v on one row"
      sql "$DB" "update project set commands=$v where worktree like '%fresh1%';"
      start; req GET /api/project | tail -2; req GET /api/session | tail -1; stop
    done
    grep -h "failed to encode" -r "$ROOT" 2>/dev/null | tail -2

    Run it: bash repro.sh

  2. evan188199-tech commented on Nov 10, 2026

    @evan188199-tech

    I'd like to take a look at this one if no one else is on it. Found with AI assistance.

  3. DZH166 commented on Nov 14, 2026

    @DZH166

    Working on this. Reproduced the failure mode locally on dev (1fa50dd): a project row whose stored commands is a JSON array (the legacy shape the V1 import copies across unchecked) flows through Project.list/fromRow and the response encode fails with Expected Project.Commands | undefined. Plan: validate the stored value against Project.Commands at the read boundary in fromRow and treat non-object values as absent (like NULL rows already are), with a regression test seeding a raw-SQL row. A PR will follow shortly.

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