Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Close an OpenCode 2 harness: back up its conversation from the v2 tables (#1317)

lingrd748269145cb7
diff --git a/cli/src/lib/sessionCheckpoint.spec.ts b/cli/src/lib/sessionCheckpoint.spec.ts
index 010de6545..bf34e08f7 100644
--- a/cli/src/lib/sessionCheckpoint.spec.ts
+++ b/cli/src/lib/sessionCheckpoint.spec.ts
@@ -245,6 +245,12 @@ it.each(['opencode', 'kilo', 'hermes', 'devin'] as const)('exports %s through re
await store.save(row)
const saved = await manifest()
expect(await readFile(join(directory, saved.file), 'utf8')).toContain('Stored in WAL')
+ // OpenCode first looks for its v2 tables (holding this session, here); every read is a SELECT of this conversation.
+ const probes = engine === 'opencode' ? sql.mock.calls.splice(0, 2) : []
+ expect(probes.map(([, query, params]) => [query, params ?? []])).toEqual(engine === 'opencode' ? [
+ ["SELECT name FROM sqlite_master WHERE type='table' AND name='session_v2';", []],
+ ['SELECT id FROM session_v2 WHERE id = ?;', ['conversation']],
+ ] : [])
for (const [, query, params] of sql.mock.calls) {
expect(query).toMatch(/^SELECT \* FROM /)
expect(params).toEqual(['conversation'])
@@ -255,6 +261,13 @@ it('treats a locked or missing database as a save failure, never an empty succes
vi.mocked(sqliteReadAll).mockResolvedValue({ ok: false, reason: 'transient' })
await expect(store.save(row)).rejects.toThrow('Could not read')
expect(await readdir(directory)).toEqual([])
+ // OpenCode's look for its v2 tables fails the same way, whether at the table or at this session.
+ row.engine = 'opencode'
+ await expect(store.save(row)).rejects.toThrow('Could not read')
+ vi.mocked(sqliteReadAll).mockReset().mockResolvedValueOnce({ ok: true, via: 'builtin', rows: [{ name: 'session_v2' }] })
+ .mockResolvedValueOnce({ ok: false, reason: 'transient' })
+ await expect(store.save(row)).rejects.toThrow('Could not read')
+ expect(await readdir(directory)).toEqual([])
})
it('rebuilds a missing backup and refuses a missing terminal snapshot', async () => {
diff --git a/cli/src/lib/sessionCheckpoint.sqlite.spec.ts b/cli/src/lib/sessionCheckpoint.sqlite.spec.ts
index 3ef2c9656..9b9e42597 100644
--- a/cli/src/lib/sessionCheckpoint.sqlite.spec.ts
+++ b/cli/src/lib/sessionCheckpoint.sqlite.spec.ts
@@ -46,3 +46,37 @@ it.each(['opencode', 'kilo', 'hermes', 'devin'] as const)('backs up only the sel
expect(backup).not.toContain('unrelated private text')
expect(database.prepare(`SELECT count(*) AS n FROM ${engine === 'opencode' || engine === 'kilo' ? 'session' : 'sessions'}`).get().n).toBe(2)
})
+
+it('backs up an OpenCode 2 conversation from its v2 tables, and a v1 one from the same file as before', async () => {
+ // OpenCode 2.0.24 writes `session_v2` / `session_message` and leaves the v1 tables holding only older chats. Read
+ // there, a v2 chat was "not found" and its harness could never be closed (2026-10-14).
+ const { DatabaseSync } = process.getBuiltinModule('node:sqlite') as any
+ const database = new DatabaseSync(join(root, 'opencode.db'))
+ databases.push(database)
+ database.exec(`PRAGMA journal_mode=WAL;
+ CREATE TABLE session (id TEXT PRIMARY KEY); CREATE TABLE message (id TEXT PRIMARY KEY, session_id TEXT, body TEXT);
+ CREATE TABLE part (id TEXT PRIMARY KEY, message_id TEXT, body TEXT);
+ CREATE TABLE session_v2 (id TEXT PRIMARY KEY); CREATE TABLE session_message (id TEXT PRIMARY KEY, session_id TEXT, data TEXT);
+ INSERT INTO session VALUES ('older'); INSERT INTO message VALUES ('m0', 'older', 'a v1 chat'); INSERT INTO part VALUES ('p0', 'm0', 'v1 part');
+ INSERT INTO session_v2 VALUES ('wanted'), ('other');
+ INSERT INTO session_message VALUES ('s1', 'wanted', 'history in v2'), ('s2', 'other', 'unrelated private text');`)
+ const { SessionCheckpointStore, SessionCheckpointError } = await import('./sessionCheckpoint.js')
+ const directory = join(root, 'backups')
+ mkdirSync(directory, { mode: 0o700 })
+ const store = new SessionCheckpointStore(directory)
+ const backup = async (sessionId: string) => {
+ await store.save({ agentId: sessionId, sessionId, engine: 'opencode' } as RegisteredSession)
+ const manifest = readdirSync(directory).filter(f => f.endsWith('.json'))
+ .map(f => JSON.parse(readFileSync(join(directory, f), 'utf8'))).find(m => m.sessionId === sessionId)
+ return JSON.parse(readFileSync(join(directory, manifest.file), 'utf8'))
+ }
+ const v2 = await backup('wanted')
+ expect(Object.keys(v2.tables)).toEqual(['session_v2', 'session_message'])
+ expect(JSON.stringify(v2)).toContain('history in v2')
+ expect(JSON.stringify(v2)).not.toContain('unrelated private text')
+ const v1 = await backup('older')
+ expect(Object.keys(v1.tables)).toEqual(['session', 'message', 'part'])
+ expect(JSON.stringify(v1)).toContain('v1 part')
+ // In neither: nothing to keep, so the session stays open.
+ await expect(store.save({ agentId: 'gone', sessionId: 'gone', engine: 'opencode' } as RegisteredSession)).rejects.toBeInstanceOf(SessionCheckpointError)
+})
diff --git a/cli/src/lib/sessionCheckpoint.ts b/cli/src/lib/sessionCheckpoint.ts
index 83975d7b7..f7fbfbc81 100644
--- a/cli/src/lib/sessionCheckpoint.ts
+++ b/cli/src/lib/sessionCheckpoint.ts
@@ -38,6 +38,15 @@ async function databaseHistory(s: RegisteredSession): Promise<Record<string, unk
break
default: throw new SessionCheckpointError('This engine has no supported history checkpoint yet. The session is still open.')
}
+ // OpenCode 2 keeps a conversation in `session_v2` and `session_message`; the v1 tables stay in the same file with
+ // only older conversations, so a v2 one read there was "not found" and could never be closed (2026-10-14). Read the
+ // v2 tables when they hold this session, as purgeAgentService does.
+ if (s.engine === 'opencode') {
+ const v2 = await sqliteReadAll(database, "SELECT name FROM sqlite_master WHERE type='table' AND name='session_v2';")
+ const held = v2.ok && v2.rows.length ? await sqliteReadAll(database, 'SELECT id FROM session_v2 WHERE id = ?;', [s.sessionId]) : null
+ if (!v2.ok || (held && !held.ok)) throw new SessionCheckpointError('Could not read the saved conversation. The session has not been closed.')
+ if (held?.rows.length) selections = [['session_v2', 'id = ?'], ['session_message', 'session_id = ?']]
+ }
const tables: Record<string, unknown[]> = {}
for (const [table, where] of selections) {
const result = await sqliteReadAll(database, `SELECT * FROM ${table} WHERE ${where};`, [s.sessionId])