Skip to content

Commit d2b4f2b

Browse files
author
engineer
committed
fix(session): recover orphaned assistant messages on startup
When the server crashes or restarts while a session is actively executing tool calls, in-memory session state is lost but the database retains stale incomplete messages. This leaves sessions permanently stuck in a 'Thinking' state with no way to recover. Add Session.recover() that runs during InstanceBootstrap to: - find assistant messages with no time.completed - force running/pending tool parts to error status - mark orphaned messages as completed - emit bus events so connected frontends update Closes anomalyco#19023
1 parent 6fe40f1 commit d2b4f2b

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎packages/opencode/src/project/bootstrap.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import { Command } from "../command"
1111
import { Instance } from "./instance"
1212
import { Log } from "@/util/log"
1313
import { ShareNext } from "@/share/share-next"
14+
import { Session } from "@/session"
1415

1516
export async function InstanceBootstrap() {
1617
Log.Default.info("bootstrapping", { directory: Instance.directory })
18+
await Session.recover()
1719
await Plugin.init()
1820
ShareNext.init()
1921
Format.init()

‎packages/opencode/src/session/index.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Config } from "../config/config"
99
import { Flag } from "../flag/flag"
1010
import { Installation } from "../installation"
1111

12-
import { Database, NotFoundError, eq, and, or, gte, isNull, desc, like, inArray, lt } from "../storage/db"
12+
import { Database, NotFoundError, eq, and, or, gte, isNull, desc, like, inArray, lt, sql } from "../storage/db"
1313
import type { SQL } from "../storage/db"
1414
import { SessionTable, MessageTable, PartTable } from "./session.sql"
1515
import { ProjectTable } from "../project/project.sql"
@@ -867,6 +867,57 @@ export namespace Session {
867867
},
868868
)
869869

870+
/**
871+
* Recover orphaned assistant messages left incomplete after a server crash or restart.
872+
* Finds assistant messages with no `time.completed`, forces their non-terminal tool
873+
* parts to error status, marks the messages as completed, and emits bus events so
874+
* connected frontends update.
875+
*
876+
* @see https://github.com/anomalyco/opencode/issues/19023
877+
*/
878+
export async function recover() {
879+
const rows = Database.use((db) =>
880+
db
881+
.select()
882+
.from(MessageTable)
883+
.where(
884+
and(
885+
sql`json_extract(${MessageTable.data}, '$.role') = 'assistant'`,
886+
sql`json_extract(${MessageTable.data}, '$.time.completed') is null`,
887+
),
888+
)
889+
.all(),
890+
)
891+
if (rows.length === 0) return
892+
log.info("recovering orphaned assistant messages", { count: rows.length })
893+
const now = Date.now()
894+
for (const row of rows) {
895+
const msg = { ...row.data, id: row.id, sessionID: row.session_id } as MessageV2.Assistant
896+
// Fix non-terminal tool parts
897+
const parts = await MessageV2.parts(row.id)
898+
for (const part of parts) {
899+
if (part.type !== "tool") continue
900+
if (part.state.status === "completed" || part.state.status === "error") continue
901+
await updatePart({
902+
...part,
903+
state: {
904+
...part.state,
905+
status: "error",
906+
error: "Tool execution was interrupted by server restart",
907+
time: {
908+
start: part.state.status === "running" ? part.state.time.start : now,
909+
end: now,
910+
},
911+
},
912+
})
913+
}
914+
// Mark message completed
915+
msg.time.completed = now
916+
await updateMessage(msg)
917+
log.info("recovered orphaned message", { messageID: row.id, sessionID: row.session_id })
918+
}
919+
}
920+
870921
export class BusyError extends Error {
871922
constructor(public readonly sessionID: string) {
872923
super(`Session ${sessionID} is busy`)

0 commit comments

Comments
 (0)