@@ -22,9 +22,10 @@ import { SessionMessage } from "@opencode/core/session/message" |
22 | 22 | import { SessionRunner } from "@opencode/core/session/runner/index" |
23 | 23 | import { SessionInboxTable, SessionTable } from "@opencode/core/session/sql" |
24 | 24 | import { SessionStore } from "@opencode/core/session/store" |
25 | | -import { Cause, Context, Deferred, Effect, Exit, Fiber, Layer, LayerMap, Scope } from "effect" |
| 25 | +import { Cause, Context, Deferred, Effect, Exit, Fiber, Layer, LayerMap, Option, Scope, Stream } from "effect" |
26 | 26 | import { eq } from "drizzle-orm" |
27 | 27 | import { testEffect } from "./lib/effect" |
| 28 | +import { StorageFault } from "./lib/storage-fault" |
28 | 29 |
|
29 | 30 | const it = testEffect( |
30 | 31 | AppNodeBuilder.build( |
@@ -1233,6 +1234,95 @@ describe("SessionExecution interrupt continuation", () => { |
1233 | 1234 | ) |
1234 | 1235 | }) |
1235 | 1236 |
|
| 1237 | +const disk = StorageFault.make() |
| 1238 | +const itFullDisk = testEffect( |
| 1239 | + AppNodeBuilder.build( |
| 1240 | + LayerNode.group([Database.node, Bus.node, SessionStore.node, SessionInbox.node, Job.node, KV.node, Session.node]), |
| 1241 | + [Database.node.replace(disk.node)], |
| 1242 | + ), |
| 1243 | +) |
| 1244 | + |
| 1245 | +describe("SessionExecution on a full disk", () => { |
| 1246 | + itFullDisk.effect("a turn that dies on a full disk records its failure once storage recovers", () => |
| 1247 | + Effect.gen(function* () { |
| 1248 | + const database = yield* Database.Service |
| 1249 | + const bus = yield* Bus.Service |
| 1250 | + const store = yield* SessionStore.Service |
| 1251 | + const sessionID = Session.ID.make("ses_disk_full_turn") |
| 1252 | + yield* seedSessions(database, [sessionID]) |
| 1253 | + const failed = yield* bus |
| 1254 | + .subscribe(SessionEvent.Execution.Failed) |
| 1255 | + .pipe(Stream.runHead, Effect.forkScoped({ startImmediately: true })) |
| 1256 | + |
| 1257 | + const scope = yield* Scope.make() |
| 1258 | + yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void)) |
| 1259 | + const context = yield* buildExecution(scope, () => |
| 1260 | + Effect.gen(function* () { |
| 1261 | + yield* disk.fill |
| 1262 | + // Any write inside the turn now fails, as the runner's step publications did. |
| 1263 | + yield* database.db |
| 1264 | + .update(SessionTable) |
| 1265 | + .set({ title: "unsaved" }) |
| 1266 | + .where(eq(SessionTable.id, sessionID)) |
| 1267 | + .run() |
| 1268 | + .pipe(Effect.orDie) |
| 1269 | + }), |
| 1270 | + ) |
| 1271 | + const execution = Context.get(context, SessionExecution.Service) |
| 1272 | + const turn = yield* execution.resume(sessionID).pipe(Effect.exit, Effect.forkScoped) |
| 1273 | + |
| 1274 | + // Longer than one write's retry window. The outcome cannot be recorded yet, so the |
| 1275 | + // Session stays busy instead of silently dropping its terminal and its claim. |
| 1276 | + yield* StorageFault.elapse(120_000) |
| 1277 | + expect(yield* execution.isActive(sessionID)).toBe(true) |
| 1278 | + expect((yield* claims(database))[sessionID]).toBe(true) |
| 1279 | + |
| 1280 | + yield* disk.free |
| 1281 | + yield* StorageFault.elapse(15_000) |
| 1282 | + expect(Exit.isFailure(yield* Fiber.join(turn))).toBe(true) |
| 1283 | + yield* execution.awaitIdle(sessionID) |
| 1284 | + expect((yield* claims(database))[sessionID]).toBe(false) |
| 1285 | + expect((yield* store.get(sessionID))?.outcome).toBe("failed") |
| 1286 | + const event = yield* Fiber.join(failed) |
| 1287 | + expect(Option.getOrUndefined(event)?.data.error).toEqual({ |
| 1288 | + type: "storage", |
| 1289 | + message: "Couldn’t save the session: database or disk is full", |
| 1290 | + }) |
| 1291 | + }), |
| 1292 | + ) |
| 1293 | + |
| 1294 | + itFullDisk.effect("shutdown during a full disk stops waiting and keeps the claim for restart", () => |
| 1295 | + Effect.gen(function* () { |
| 1296 | + const database = yield* Database.Service |
| 1297 | + const sessionID = Session.ID.make("ses_disk_full_shutdown") |
| 1298 | + yield* seedSessions(database, [sessionID]) |
| 1299 | + const scope = yield* Scope.make() |
| 1300 | + const context = yield* buildExecution(scope, () => |
| 1301 | + disk.fill.pipe( |
| 1302 | + Effect.andThen( |
| 1303 | + database.db |
| 1304 | + .update(SessionTable) |
| 1305 | + .set({ title: "unsaved" }) |
| 1306 | + .where(eq(SessionTable.id, sessionID)) |
| 1307 | + .run() |
| 1308 | + .pipe(Effect.orDie), |
| 1309 | + ), |
| 1310 | + ), |
| 1311 | + ) |
| 1312 | + const execution = Context.get(context, SessionExecution.Service) |
| 1313 | + yield* execution.resume(sessionID).pipe(Effect.exit, Effect.forkScoped) |
| 1314 | + yield* StorageFault.elapse(60_000) |
| 1315 | + expect(yield* execution.isActive(sessionID)).toBe(true) |
| 1316 | + |
| 1317 | + const closing = yield* Scope.close(scope, Exit.void).pipe(Effect.forkScoped) |
| 1318 | + yield* StorageFault.elapse(1_000) |
| 1319 | + expect(closing.pollUnsafe()).toBeDefined() |
| 1320 | + yield* disk.free |
| 1321 | + expect((yield* claims(database))[sessionID]).toBe(true) |
| 1322 | + }), |
| 1323 | + ) |
| 1324 | +}) |
| 1325 | + |
1236 | 1326 | function seedBackground( |
1237 | 1327 | jobs: Job.Interface, |
1238 | 1328 | sessionID: Session.ID, |
|