Skip to content

Commit d970983

Browse files
committed
fix(opencode): run one session loop at a time across processes
Two opencode processes on one project could each run the loop for the same session, because SessionRunState keeps its runners in a per-process map. Each loop's reload saw the other's unfinished assistant message, the exit check failed, and they took turns answering one user message. A per-session EffectFlock lock now wraps the work of ensureRunning and startShell. A process that can't take it marks the session busy and retries with an interruptible jittered sleep, warning with the holder's pid after 60 s. runLoop checks it still owns the lock before every iteration and stops if another process took it over; a stalled holder's in-flight turn may still finish, so this is best-effort, not fencing. EffectFlock gains tryAcquire (one attempt, release registered atomically), Held.verify and holder. acquire and withLock are unchanged. opencode run: a waiting run whose prompt the other process answered used to print nothing, because /event only carries the local process's events. After the prompt returns, run now renders the reply's messages through the same part renderer as the event loop, skipping parts it already printed, so text and JSON output match a local run. It also prints one error record per rejected prompt instead of reporting the same failure from both the session event and the HTTP rejection. Fixes anomalyco#49367
1 parent 7cb044e commit d970983

8 files changed

Lines changed: 1100 additions & 82 deletions

File tree

‎packages/core/src/util/effect-flock.ts‎

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,17 @@ export namespace EffectFlock {
7474

7575
export interface Interface {
7676
readonly acquire: (key: string, dir?: string) => Effect.Effect<void, LockError, Scope.Scope>
77+
readonly tryAcquire: (key: string, dir?: string) => Effect.Effect<Option.Option<Held>, never, Scope.Scope>
78+
readonly holder: (key: string, dir?: string) => Effect.Effect<Option.Option<Holder>>
7779
readonly withLock: {
7880
(key: string, dir?: string): <A, E, R>(body: Effect.Effect<A, E, R>) => Effect.Effect<A, E | LockError, R>
7981
<A, E, R>(body: Effect.Effect<A, E, R>, key: string, dir?: string): Effect.Effect<A, E | LockError, R>
8082
}
8183
}
8284

85+
export type Holder = { readonly pid: number; readonly hostname: string; readonly createdAt: string }
86+
export type Held = { readonly verify: Effect.Effect<boolean> }
87+
8388
export class Service extends Context.Service<Service, Interface>()("EffectFlock") {}
8489

8590
// ---------------------------------------------------------------------------
@@ -248,18 +253,79 @@ export namespace EffectFlock {
248253
yield* forceRemove(handle.lockDir)
249254
})
250255

251-
// -- build service --
256+
const verify = (handle: Handle) =>
257+
fs.readFileString(handle.metaPath).pipe(
258+
Effect.map((raw) =>
259+
Option.match(Schema.decodeUnknownOption(LockMetaJson)(raw), {
260+
onNone: () => false,
261+
onSome: (meta) => meta.token === handle.token,
262+
}),
263+
),
264+
Effect.orElseSucceed(() => false),
265+
)
252266

253-
const acquire = Effect.fn("EffectFlock.acquire")(function* (key: string, dir?: string) {
267+
const releaseLease = (handle: Handle) =>
268+
Effect.uninterruptible(
269+
Effect.gen(function* () {
270+
const breakerPath = handle.lockDir + ".breaker"
271+
const claimed = yield* fs.makeDirectory(breakerPath, { mode: 0o700 }).pipe(
272+
Effect.as(true),
273+
Effect.catchIf(
274+
(e) => e.reason._tag === "AlreadyExists",
275+
() => Effect.succeed(false),
276+
),
277+
Effect.orDie,
278+
)
279+
if (!claimed) {
280+
yield* Effect.logWarning("EffectFlock lease lost before release; preserving current lock", {
281+
lockDir: handle.lockDir,
282+
})
283+
return
284+
}
285+
yield* Effect.gen(function* () {
286+
if (yield* verify(handle)) return yield* forceRemove(handle.lockDir)
287+
yield* Effect.logWarning("EffectFlock lease lost before release; preserving current lock", {
288+
lockDir: handle.lockDir,
289+
})
290+
}).pipe(Effect.ensuring(forceRemove(breakerPath)))
291+
}),
292+
)
293+
294+
const tryAcquire = Effect.fn("EffectFlock.tryAcquire")(function* (key: string, dir?: string) {
254295
const lockDir = dir ?? lockRoot
255296
yield* ensureDir(lockDir)
297+
// acquireRelease runs the single attempt uninterruptibly and registers the release in
298+
// the same step, so an interrupt can't leave an acquired lock without its release
299+
const acquired = yield* Effect.acquireRelease(
300+
tryAcquireLockDir(path.join(lockDir, Hash.fast(key) + ".lock"), key).pipe(Effect.option),
301+
(result) => (Option.isSome(result) ? releaseLease(result.value) : Effect.void),
302+
)
303+
if (Option.isNone(acquired)) return Option.none()
304+
yield* fs
305+
.utimes(acquired.value.heartbeatPath, new Date(), new Date())
306+
.pipe(Effect.ignore, Effect.repeat(Schedule.spaced(HEARTBEAT_MS)), Effect.forkScoped)
307+
return Option.some({ verify: verify(acquired.value) })
308+
})
256309

257-
const lockfile = path.join(lockDir, Hash.fast(key) + ".lock")
258-
259-
// acquireRelease: acquire is uninterruptible, release is guaranteed
260-
const handle = yield* Effect.acquireRelease(acquireHandle(lockfile, key), (handle) => release(handle))
310+
const holder = Effect.fn("EffectFlock.holder")(function* (key: string, dir?: string) {
311+
const metaPath = path.join(dir ?? lockRoot, Hash.fast(key) + ".lock", "meta.json")
312+
return yield* fs.readFileString(metaPath).pipe(
313+
Effect.map((raw) =>
314+
Schema.decodeUnknownOption(LockMetaJson)(raw).pipe(
315+
Option.map((meta) => ({ pid: meta.pid, hostname: meta.hostname, createdAt: meta.createdAt })),
316+
),
317+
),
318+
Effect.orElseSucceed(() => Option.none<Holder>()),
319+
)
320+
})
261321

262-
// Heartbeat fiber — scoped, so it's interrupted before release runs
322+
const acquire = Effect.fn("EffectFlock.acquire")(function* (key: string, dir?: string) {
323+
const lockDir = dir ?? lockRoot
324+
yield* ensureDir(lockDir)
325+
const handle = yield* Effect.acquireRelease(
326+
acquireHandle(path.join(lockDir, Hash.fast(key) + ".lock"), key),
327+
(handle) => release(handle),
328+
)
263329
yield* fs
264330
.utimes(handle.heartbeatPath, new Date(), new Date())
265331
.pipe(Effect.ignore, Effect.repeat(Schedule.spaced(HEARTBEAT_MS)), Effect.forkScoped)
@@ -276,7 +342,7 @@ export namespace EffectFlock {
276342
),
277343
)
278344

279-
return Service.of({ acquire, withLock })
345+
return Service.of({ acquire, tryAcquire, holder, withLock })
280346
}),
281347
)
282348

‎packages/core/test/util/effect-flock.test.ts‎

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { spawn } from "child_process"
33
import fs from "fs/promises"
44
import path from "path"
55
import os from "os"
6-
import { Cause, Effect, Exit } from "effect"
6+
import { Cause, Deferred, Effect, Exit, Fiber } from "effect"
77
import { testEffect } from "../lib/effect"
88
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
99
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
@@ -119,6 +119,118 @@ const testLayer = AppNodeBuilder.build(EffectFlock.node, [[Global.node, testGlob
119119
describe("util.effect-flock", () => {
120120
const it = testEffect(testLayer)
121121

122+
it.live(
123+
"tryAcquire exposes ownership and preserves a takeover",
124+
Effect.gen(function* () {
125+
const flock = yield* EffectFlock.Service
126+
const tmp = yield* Effect.promise(() => fs.mkdtemp(path.join(os.tmpdir(), "eflock-lease-")))
127+
const dir = path.join(tmp, "locks")
128+
const key = "eflock:lease"
129+
const lockDir = lock(dir, key)
130+
yield* Effect.scoped(
131+
Effect.gen(function* () {
132+
const first = yield* flock.tryAcquire(key, dir)
133+
expect(first._tag).toBe("Some")
134+
expect((yield* flock.tryAcquire(key, dir))._tag).toBe("None")
135+
const holder = yield* flock.holder(key, dir)
136+
expect(holder._tag).toBe("Some")
137+
if (holder._tag === "Some") expect(holder.value.pid).toBe(process.pid)
138+
if (first._tag === "None") return false
139+
expect(yield* first.value.verify).toBe(true)
140+
yield* Effect.promise(async () => {
141+
const old = new Date(Date.now() - 120_000)
142+
await fs.utimes(path.join(lockDir, "heartbeat"), old, old)
143+
await fs.utimes(path.join(lockDir, "meta.json"), old, old)
144+
})
145+
const second = yield* flock.tryAcquire(key, dir)
146+
expect(second._tag).toBe("Some")
147+
expect(yield* first.value.verify).toBe(false)
148+
if (second._tag === "None") return
149+
expect(yield* second.value.verify).toBe(true)
150+
}),
151+
)
152+
expect(yield* Effect.promise(() => exists(lockDir))).toBe(false)
153+
expect((yield* flock.holder(key, dir))._tag).toBe("None")
154+
yield* Effect.promise(() => fs.rm(tmp, { recursive: true, force: true }))
155+
}),
156+
)
157+
158+
it.live(
159+
"releasing a stale holder preserves the new owner's lock",
160+
Effect.gen(function* () {
161+
const flock = yield* EffectFlock.Service
162+
const tmp = yield* Effect.promise(() => fs.mkdtemp(path.join(os.tmpdir(), "eflock-takeover-")))
163+
yield* Effect.addFinalizer(() => Effect.promise(() => fs.rm(tmp, { recursive: true, force: true })))
164+
const dir = path.join(tmp, "locks")
165+
const key = "eflock:takeover-release"
166+
const lockDir = lock(dir, key)
167+
const ready = yield* Deferred.make<EffectFlock.Held>()
168+
const finish = yield* Deferred.make<void>()
169+
const owner = yield* Effect.gen(function* () {
170+
yield* Effect.scoped(
171+
Effect.gen(function* () {
172+
const result = yield* flock.tryAcquire(key, dir)
173+
if (result._tag === "None") return
174+
yield* Deferred.succeed(ready, result.value)
175+
yield* Deferred.await(finish)
176+
}),
177+
)
178+
}).pipe(Effect.forkScoped)
179+
const original = yield* Deferred.await(ready)
180+
yield* Effect.sleep("100 millis")
181+
yield* Effect.promise(async () => {
182+
const old = new Date(Date.now() - 120_000)
183+
await fs.utimes(path.join(lockDir, "heartbeat"), old, old)
184+
await fs.utimes(path.join(lockDir, "meta.json"), old, old)
185+
await fs.utimes(lockDir, old, old)
186+
})
187+
const current = yield* flock.tryAcquire(key, dir)
188+
expect(current._tag).toBe("Some")
189+
expect(yield* original.verify).toBe(false)
190+
if (current._tag === "None") return
191+
yield* Deferred.succeed(finish, undefined)
192+
yield* Fiber.join(owner)
193+
expect(yield* Effect.promise(() => exists(lockDir))).toBe(true)
194+
expect(yield* current.value.verify).toBe(true)
195+
}),
196+
)
197+
198+
it.live(
199+
"an interrupted retry loop leaves the released key available",
200+
Effect.gen(function* () {
201+
const flock = yield* EffectFlock.Service
202+
const tmp = yield* Effect.promise(() => fs.mkdtemp(path.join(os.tmpdir(), "eflock-wait-")))
203+
const dir = path.join(tmp, "locks")
204+
const key = "eflock:wait-interrupt"
205+
const lockDir = lock(dir, key)
206+
yield* Effect.scoped(
207+
Effect.gen(function* () {
208+
const holder = yield* flock.tryAcquire(key, dir)
209+
expect(holder._tag).toBe("Some")
210+
let waiterAcquired = false
211+
const waiter = yield* Effect.gen(function* () {
212+
while (true) {
213+
const result = yield* flock.tryAcquire(key, dir)
214+
if (result._tag === "Some") {
215+
waiterAcquired = true
216+
return
217+
}
218+
yield* Effect.sleep("50 millis")
219+
}
220+
}).pipe(Effect.forkScoped)
221+
yield* Effect.sleep("10 millis")
222+
yield* Fiber.interrupt(waiter)
223+
expect(waiterAcquired).toBe(false)
224+
expect(yield* Effect.promise(() => exists(lockDir))).toBe(true)
225+
}),
226+
)
227+
expect(yield* Effect.promise(() => exists(lockDir))).toBe(false)
228+
const fresh = yield* Effect.scoped(flock.tryAcquire(key, dir))
229+
expect(fresh._tag).toBe("Some")
230+
yield* Effect.promise(() => fs.rm(tmp, { recursive: true, force: true }))
231+
}),
232+
)
233+
122234
it.live(
123235
"acquire and release via scoped Effect",
124236
Effect.gen(function* () {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export function createRunErrorDeduper() {
2+
let sessionErrorEmitted = false
3+
const requestErrors = new Set<string>()
4+
return (error: unknown, source: "session" | "request") => {
5+
const key = errorKey(error)
6+
if (source === "session") {
7+
if (key && requestErrors.has(key)) return true
8+
sessionErrorEmitted = true
9+
return false
10+
}
11+
if (key) requestErrors.add(key)
12+
return sessionErrorEmitted
13+
}
14+
}
15+
16+
function errorKey(error: unknown) {
17+
if (!error || typeof error !== "object" || !("name" in error) || !("data" in error)) return
18+
const data = error.data
19+
if (!data || typeof data !== "object" || !("message" in data)) return
20+
return JSON.stringify([String(error.name), String(data.message)])
21+
}

0 commit comments

Comments
 (0)