opencode icon indicating copy to clipboard operation
opencode copied to clipboard

session import: large sessions fail with "SQLite query expected N values, received M"

Open Novice-PG opened this issue 1 month ago • 4 comments

Summary

opencode session import fails for large sessions with Internal Server Error. The server log shows SQLite query expected N values, received M, which indicates the importer builds a statement with more bind parameters than SQLite allows (or batches values incorrectly). Sessions with more than roughly 10k messages cannot be imported, which blocks migrating large sessions to a new machine.

Environment

  • opencode version: 2.1.1
  • OS: Linux radxa-dragon-q8b 7.0.11-7-qcom aarch64
  • Terminal: xterm-256color (COLORTERM=truecolor)
  • Shell: /bin/bash
  • Install/channel: latest (v2.1.1)
  • Active plugins: none configured

Reproduction

  1. Export a session with many messages: opencode session export ses_xxx > session.json
  2. Import it into a fresh database: OPENCODE_DB=/tmp/fresh.db opencode session import session.json --directory /tmp/dir --standalone
  3. Small/medium sessions import fine; sessions with many messages fail.

Observed threshold (message count of the top-level session):

messages result
2474 OK
11448 FAIL
20779 FAIL
24611 FAIL

Expected Behavior

The session imports successfully regardless of size.

Actual Behavior

CLI output:

Error: Failed to import session: Internal Server Error

Server log (~/.local/share/opencode/log/opencode.log, role=server, http.url=/api/experimental/session/import, http.status=500):

effect/sql/SqlError: Failed to execute statement (cause: effect/sql/SqlError/UnknownError: Failed to execute statement (cause: Error: SQLite query expected 14593 values, received 80129)))

Stack trace points at SessionTransfer.import.

Additional Context

  • Tested 29 top-level sessions: 26 import OK, 3 fail (the three largest).
  • Exported JSON sizes of the failing sessions: ~191 MB, ~230 MB, ~261 MB.
  • The exported JSON itself is valid and complete (contains info + messages).
  • This blocks full session migration to a new machine via export/import; the only workaround is copying the entire data directory.
  • Reproducible consistently.

Novice-PG avatar Oct 21 '26 14:10 Novice-PG

Thanks for the detailed report. I can reproduce this on v2.1.1, and it still happens on the latest dev (0c6be86).

What I did: generated a valid session export with N simple messages, then ran opencode session import session.json --directory <dir> --standalone against a fresh OPENCODE_DB.

  • 2,000 messages and 9,362 messages: import OK.
  • 9,363 messages: fails (SQLite query expected 5 values, received 65541).
  • 11,448 messages: fails with Error: Failed to import session: Internal Server Error, and the server log shows SQLite query expected 14600 values, received 80136, the same kind of error you saw.

The importer appears to write every message in one SQL insert, with 7 values per message. Above 65,535 values (just over 9,362 messages), that statement fails. This fits your results: 2,474 messages worked and 11,448 failed.

The script below reproduces it. Until this is fixed, copying the data directory, as you mentioned, is the only workaround I know of.

Reproduction script (repro.sh)
#!/usr/bin/env bash
# Reproduce anomalyco/opencode#57806: `opencode session import` fails for sessions
# with many messages (single INSERT exceeds SQLite's bind parameter limit).
# Usage: ./repro.sh [message-count]   (OPENCODE_CMD overrides the opencode command)
set -u
N=${1:-11448}
OC=${OPENCODE_CMD:-opencode}
WORK=$(mktemp -d /tmp/opencode/repro-XXXX)
mkdir -p "$WORK/dir"
export OPENCODE_DB="$WORK/fresh.db"
export XDG_DATA_HOME="$WORK/data" XDG_STATE_HOME="$WORK/state" XDG_CONFIG_HOME="$WORK/config" XDG_CACHE_HOME="$WORK/cache"

N=$N DIR="$WORK/dir" bun -e '
const n = Number(process.env.N), now = Date.now()
const messages = Array.from({ length: n }, (_, i) => ({
  id: "msg_" + String(i).padStart(26, "0"),
  type: "synthetic",
  text: "message " + i,
  time: { created: now + i },
}))
const data = {
  info: {
    id: "ses_repro57806000000000000000",
    projectID: "global",
    cost: 0,
    tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
    time: { created: now, updated: now + n },
    title: "repro " + n,
    location: { directory: process.env.DIR },
  },
  messages,
}
await Bun.write(process.env.DIR + "/../session.json", JSON.stringify(data))
'
echo "== importing session with $N messages using: $OC"
cd "$WORK/dir"
$OC session import "$WORK/session.json" --directory "$WORK/dir" --standalone --print-logs > "$WORK/out.log" 2>&1
code=$?
grep -oE "SQLite query expected [0-9]+ values, received [0-9]+|Error: Failed to import session.*" "$WORK/out.log" | sort -u
grep -v "^\(INFO\|DEBUG\|WARN\|ERROR\) " "$WORK/out.log" | grep -v "Error" | cut -c1-200 | head -3
echo "exit: $code"

Run it: bash repro.sh

opencode-agent[bot] avatar Oct 21 '26 14:10 opencode-agent[bot]

I'll take this: the importer builds one bulk insert and exceeds SQLite's bind-variable limit on large sessions. I'll chunk the message batch (and any other bulk insert in the transfer) by a row count that stays under the limit, with a regression test above the reported threshold.

reynold-hu avatar Oct 21 '26 14:10 reynold-hu

The fix is ready on branch session-import-batching (commit ad57e984): messages are inserted in 1,000-row batches inside the same event commit, and a new test imports 10,000 messages over HTTP (fails with 500 on the parent). GitHub's open pull request limit for this repository is currently reached, so I will open the pull request as soon as a slot frees.

reynold-hu avatar Oct 21 '26 14:10 reynold-hu

Fixed on dev by PR #58916 (commit 6000b7c51b), which batches the imported message insert under SQLite's bound-parameter limit instead of building one statement for the whole session. Thanks for the clear threshold data.

neriousy avatar Dec 03 '26 12:12 neriousy