|
| 1 | +import { describe, expect } from "bun:test" |
| 2 | +import { Effect } from "effect" |
| 3 | +import { Session as SessionNs } from "@/session/session" |
| 4 | +import { MessageV2 } from "@/session/message-v2" |
| 5 | +import { Database } from "@/storage/db" |
| 6 | +import { MessageTable, PartTable } from "@/session/session.sql" |
| 7 | +import { MessageID, PartID, type SessionID } from "@/session/schema" |
| 8 | +import * as Log from "@opencode-ai/core/util/log" |
| 9 | +import { testEffect } from "../lib/effect" |
| 10 | + |
| 11 | +void Log.init({ print: false }) |
| 12 | + |
| 13 | +const it = testEffect(SessionNs.defaultLayer) |
| 14 | + |
| 15 | +// Stale enough that the orphan-recovery threshold fires (60s). |
| 16 | +const ANCIENT_MS = Date.now() - 5 * 60_000 |
| 17 | + |
| 18 | +// Insert a raw orphan tool row directly into the part table, simulating the |
| 19 | +// state on disk after a process was killed mid-tool-call (hivemind #254 |
| 20 | +// repro). Bypasses session.updatePart on purpose — that path would refuse a |
| 21 | +// freshly-created running tool because the message hasn't been authored yet, |
| 22 | +// and we want exactly the kill-9 shape (orphan row, message present, no |
| 23 | +// follow-up part). |
| 24 | +const seedOrphan = Effect.fn("Test.seedOrphan")(function* ( |
| 25 | + sessionID: SessionID, |
| 26 | + tool: string, |
| 27 | + status: "running" | "pending", |
| 28 | +) { |
| 29 | + const messageID = MessageID.ascending() |
| 30 | + const partID = PartID.ascending() |
| 31 | + yield* Effect.sync(() => |
| 32 | + Database.use((db) => { |
| 33 | + db.insert(MessageTable) |
| 34 | + .values({ |
| 35 | + id: messageID, |
| 36 | + session_id: sessionID, |
| 37 | + time_created: ANCIENT_MS, |
| 38 | + time_updated: ANCIENT_MS, |
| 39 | + data: { |
| 40 | + role: "assistant", |
| 41 | + time: { created: ANCIENT_MS }, |
| 42 | + agent: "test", |
| 43 | + cost: 0, |
| 44 | + tokens: { |
| 45 | + input: 0, |
| 46 | + output: 0, |
| 47 | + reasoning: 0, |
| 48 | + cache: { read: 0, write: 0 }, |
| 49 | + }, |
| 50 | + mode: "", |
| 51 | + model: { providerID: "test", modelID: "test" }, |
| 52 | + path: { cwd: "/", root: "/" }, |
| 53 | + system: [], |
| 54 | + tools: {}, |
| 55 | + } as never, |
| 56 | + }) |
| 57 | + .run() |
| 58 | + db.insert(PartTable) |
| 59 | + .values({ |
| 60 | + id: partID, |
| 61 | + message_id: messageID, |
| 62 | + session_id: sessionID, |
| 63 | + time_created: ANCIENT_MS, |
| 64 | + time_updated: ANCIENT_MS, |
| 65 | + data: |
| 66 | + status === "running" |
| 67 | + ? ({ |
| 68 | + type: "tool", |
| 69 | + callID: `call_${tool}`, |
| 70 | + tool, |
| 71 | + state: { |
| 72 | + status: "running", |
| 73 | + input: { foo: "bar" }, |
| 74 | + title: tool, |
| 75 | + time: { start: ANCIENT_MS }, |
| 76 | + }, |
| 77 | + } as never) |
| 78 | + : ({ |
| 79 | + type: "tool", |
| 80 | + callID: `call_${tool}`, |
| 81 | + tool, |
| 82 | + state: { |
| 83 | + status: "pending", |
| 84 | + input: { foo: "bar" }, |
| 85 | + raw: "", |
| 86 | + }, |
| 87 | + } as never), |
| 88 | + }) |
| 89 | + .run() |
| 90 | + }), |
| 91 | + ) |
| 92 | + return { messageID, partID } |
| 93 | +}) |
| 94 | + |
| 95 | +const withSession = <A, E, R>( |
| 96 | + fn: (input: { session: SessionNs.Interface; sessionID: SessionID }) => Effect.Effect<A, E, R>, |
| 97 | +) => |
| 98 | + Effect.acquireUseRelease( |
| 99 | + Effect.gen(function* () { |
| 100 | + const session = yield* SessionNs.Service |
| 101 | + const created = yield* session.create({}) |
| 102 | + return { session, sessionID: created.id } |
| 103 | + }), |
| 104 | + fn, |
| 105 | + (input) => input.session.remove(input.sessionID).pipe(Effect.ignore), |
| 106 | + ) |
| 107 | + |
| 108 | +describe("orphan tool-part recovery on session resume (#254)", () => { |
| 109 | + it.instance("transforms an orphaned running `question` tool-part into a synthetic error", () => |
| 110 | + withSession(({ session, sessionID }) => |
| 111 | + Effect.gen(function* () { |
| 112 | + yield* seedOrphan(sessionID, "question", "running") |
| 113 | + |
| 114 | + const messages = yield* session.messages({ sessionID }) |
| 115 | + const orphan = messages |
| 116 | + .flatMap((m) => m.parts) |
| 117 | + .find((p) => p.type === "tool") |
| 118 | + expect(orphan).toBeDefined() |
| 119 | + if (orphan?.type !== "tool") throw new Error("expected tool part") |
| 120 | + expect(orphan.state.status).toBe("error") |
| 121 | + if (orphan.state.status !== "error") throw new Error("narrowing failed") |
| 122 | + expect(orphan.state.error).toContain("interrupted") |
| 123 | + expect(orphan.state.metadata?.interrupted).toBe(true) |
| 124 | + // The synthetic timeline must satisfy ToolStateError's schema invariant |
| 125 | + // (end >= start) — otherwise downstream consumers panic. |
| 126 | + expect(orphan.state.time.end).toBeGreaterThanOrEqual(orphan.state.time.start) |
| 127 | + }), |
| 128 | + ), |
| 129 | + ) |
| 130 | + |
| 131 | + it.instance("transforms an orphaned pending bash tool-part into a synthetic error", () => |
| 132 | + withSession(({ session, sessionID }) => |
| 133 | + Effect.gen(function* () { |
| 134 | + yield* seedOrphan(sessionID, "bash", "pending") |
| 135 | + |
| 136 | + const messages = yield* session.messages({ sessionID }) |
| 137 | + const orphan = messages.flatMap((m) => m.parts).find((p) => p.type === "tool") |
| 138 | + if (orphan?.type !== "tool") throw new Error("expected tool part") |
| 139 | + expect(orphan.state.status).toBe("error") |
| 140 | + }), |
| 141 | + ), |
| 142 | + ) |
| 143 | + |
| 144 | + it.instance("does not transform a tool-part whose state is already completed", () => |
| 145 | + withSession(({ session, sessionID }) => |
| 146 | + Effect.gen(function* () { |
| 147 | + const messageID = MessageID.ascending() |
| 148 | + const partID = PartID.ascending() |
| 149 | + yield* Effect.sync(() => |
| 150 | + Database.use((db) => { |
| 151 | + db.insert(MessageTable) |
| 152 | + .values({ |
| 153 | + id: messageID, |
| 154 | + session_id: sessionID, |
| 155 | + time_created: ANCIENT_MS, |
| 156 | + time_updated: ANCIENT_MS, |
| 157 | + data: { |
| 158 | + role: "assistant", |
| 159 | + time: { created: ANCIENT_MS }, |
| 160 | + agent: "test", |
| 161 | + cost: 0, |
| 162 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 163 | + mode: "", |
| 164 | + model: { providerID: "test", modelID: "test" }, |
| 165 | + path: { cwd: "/", root: "/" }, |
| 166 | + system: [], |
| 167 | + tools: {}, |
| 168 | + } as never, |
| 169 | + }) |
| 170 | + .run() |
| 171 | + db.insert(PartTable) |
| 172 | + .values({ |
| 173 | + id: partID, |
| 174 | + message_id: messageID, |
| 175 | + session_id: sessionID, |
| 176 | + time_created: ANCIENT_MS, |
| 177 | + time_updated: ANCIENT_MS, |
| 178 | + data: { |
| 179 | + type: "tool", |
| 180 | + callID: "call_done", |
| 181 | + tool: "read", |
| 182 | + state: { |
| 183 | + status: "completed", |
| 184 | + input: { path: "/etc/hostname" }, |
| 185 | + output: "ok", |
| 186 | + title: "read", |
| 187 | + metadata: { exit: 0 }, |
| 188 | + time: { start: ANCIENT_MS - 1, end: ANCIENT_MS }, |
| 189 | + }, |
| 190 | + } as never, |
| 191 | + }) |
| 192 | + .run() |
| 193 | + }), |
| 194 | + ) |
| 195 | + |
| 196 | + const messages = yield* session.messages({ sessionID }) |
| 197 | + const completed = messages.flatMap((m) => m.parts).find((p) => p.type === "tool") |
| 198 | + if (completed?.type !== "tool") throw new Error("expected tool part") |
| 199 | + expect(completed.state.status).toBe("completed") |
| 200 | + }), |
| 201 | + ), |
| 202 | + ) |
| 203 | + |
| 204 | + it.instance("does not transform a running tool-part that's still within the 60s window", () => |
| 205 | + withSession(({ session, sessionID }) => |
| 206 | + Effect.gen(function* () { |
| 207 | + const messageID = MessageID.ascending() |
| 208 | + const partID = PartID.ascending() |
| 209 | + // start = NOW. Recovery threshold is 60s; this should be untouched. |
| 210 | + const now = Date.now() |
| 211 | + yield* Effect.sync(() => |
| 212 | + Database.use((db) => { |
| 213 | + db.insert(MessageTable) |
| 214 | + .values({ |
| 215 | + id: messageID, |
| 216 | + session_id: sessionID, |
| 217 | + time_created: now, |
| 218 | + time_updated: now, |
| 219 | + data: { |
| 220 | + role: "assistant", |
| 221 | + time: { created: now }, |
| 222 | + agent: "test", |
| 223 | + cost: 0, |
| 224 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 225 | + mode: "", |
| 226 | + model: { providerID: "test", modelID: "test" }, |
| 227 | + path: { cwd: "/", root: "/" }, |
| 228 | + system: [], |
| 229 | + tools: {}, |
| 230 | + } as never, |
| 231 | + }) |
| 232 | + .run() |
| 233 | + db.insert(PartTable) |
| 234 | + .values({ |
| 235 | + id: partID, |
| 236 | + message_id: messageID, |
| 237 | + session_id: sessionID, |
| 238 | + time_created: now, |
| 239 | + time_updated: now, |
| 240 | + data: { |
| 241 | + type: "tool", |
| 242 | + callID: "call_live", |
| 243 | + tool: "bash", |
| 244 | + state: { |
| 245 | + status: "running", |
| 246 | + input: { cmd: "sleep 1" }, |
| 247 | + title: "bash", |
| 248 | + time: { start: now }, |
| 249 | + }, |
| 250 | + } as never, |
| 251 | + }) |
| 252 | + .run() |
| 253 | + }), |
| 254 | + ) |
| 255 | + |
| 256 | + const messages = yield* session.messages({ sessionID }) |
| 257 | + const live = messages.flatMap((m) => m.parts).find((p) => p.type === "tool") |
| 258 | + if (live?.type !== "tool") throw new Error("expected tool part") |
| 259 | + // Fresh running tool must stay running — would be wrong to clobber a |
| 260 | + // tool call that's actually in flight in some other process. |
| 261 | + expect(live.state.status).toBe("running") |
| 262 | + }), |
| 263 | + ), |
| 264 | + ) |
| 265 | + |
| 266 | + it.instance("covers multiple orphan tool types simultaneously (bash, read, edit, question)", () => |
| 267 | + withSession(({ session, sessionID }) => |
| 268 | + Effect.gen(function* () { |
| 269 | + for (const tool of ["bash", "read", "edit", "question"]) { |
| 270 | + yield* seedOrphan(sessionID, tool, "running") |
| 271 | + } |
| 272 | + |
| 273 | + const messages = yield* session.messages({ sessionID }) |
| 274 | + const tools = messages.flatMap((m) => m.parts).filter((p) => p.type === "tool") |
| 275 | + expect(tools).toHaveLength(4) |
| 276 | + for (const t of tools) { |
| 277 | + if (t.type !== "tool") throw new Error("expected tool part") |
| 278 | + expect(t.state.status).toBe("error") |
| 279 | + } |
| 280 | + }), |
| 281 | + ), |
| 282 | + ) |
| 283 | +}) |
0 commit comments