GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(core): record session outcomes after storage outages · anomalyco/opencode@e7d4987

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/core
    • src/session
      • execution.ts
      • to-session-error.ts
    • test
      • session-execution.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/core/src/session/execution.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/session/execution.ts
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * as SessionExecution from "./execution.js"
33
import { Cause, Context, Effect, Exit, Layer } from "effect"
44
import { Bus } from "../bus.js"
55
import { Database } from "../database/database.js"
6+
import { StorageRetry } from "../database/storage-retry.js"
67
import { Job } from "../job.js"
78
import { Instance } from "../instance/service.js"
89
import { makeGlobalNode } from "@opencode/util/effect/app-node"
@@ -62,8 +63,15 @@ export const layer = Layer.effect(
6263
const bus = yield* Bus.Service
6364
const jobs = yield* Job.Service
6465
const db = (yield* Database.Service).db
66+
// Lifecycle events are the only durable record of a turn's outcome and the claim release, so keep
67+
// retrying through a storage outage: a dropped terminal leaves the Session looking busy forever.
68+
// The retries wait interruptibly, so shutdown is not held up; the claim then survives for restart.
6569
const reportLifecycle = <A>(sessionID: SessionSchema.ID, effect: Effect.Effect<A>) =>
6670
effect.pipe(
71+
Effect.catchCauseIf(
72+
(cause) => StorageRetry.isTransient(cause),
73+
() => Effect.interruptible(effect.pipe(StorageRetry.retry(StorageRetry.settlement))),
74+
),
6775
Effect.tapCause((cause) =>
6876
Cause.hasInterruptsOnly(cause)
6977
? Effect.void
Collapse file

‎packages/core/src/session/to-session-error.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/session/to-session-error.ts
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AIError, ToolFailure, type FinishReasonDetails } from "@opencode/ai"
2+
import { StorageRetry } from "../database/storage-retry.js"
23
import { Tool } from "@opencode/schema/tool"
34
import { SessionError } from "@opencode/schema/session-error"
45
import { Permission } from "../permission.js"
@@ -82,6 +83,11 @@ export function toSessionError(cause: unknown): SessionError.Error {
8283
)
8384
return { type: "provider.no-route", message: cause.message }
8485
if (cause instanceof Integration.AuthorizationError) return { type: "provider.auth", message: cause.message }
86+
if (StorageRetry.isTransientError(cause))
87+
return {
88+
type: "storage",
89+
message: `Couldn’t save the session: ${StorageRetry.message(cause) ?? "storage unavailable"}`,
90+
}
8591
return { type: "unknown", message: cause instanceof Error ? cause.message : String(cause) }
8692
}
8793

Collapse file

‎packages/core/test/session-execution.test.ts‎

Copy file name to clipboardExpand all lines: packages/core/test/session-execution.test.ts
+91-1Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import { SessionMessage } from "@opencode/core/session/message"
2222
import { SessionRunner } from "@opencode/core/session/runner/index"
2323
import { SessionInboxTable, SessionTable } from "@opencode/core/session/sql"
2424
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"
2626
import { eq } from "drizzle-orm"
2727
import { testEffect } from "./lib/effect"
28+
import { StorageFault } from "./lib/storage-fault"
2829

2930
const it = testEffect(
3031
AppNodeBuilder.build(
@@ -1233,6 +1234,95 @@ describe("SessionExecution interrupt continuation", () => {
12331234
)
12341235
})
12351236

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+
12361326
function seedBackground(
12371327
jobs: Job.Interface,
12381328
sessionID: Session.ID,

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.