Skip to content

Commit b8bf11b

Browse files
fix(tui,opencode,core,sdk): audit-v8 wave D — tombstone, lease & bound hardening
Closes the runtime/UI findings from core-review/opencode-audit-v8-{opus,deepseek}.md (fix wave D ownership set): - tui NEW-02 / opus NEW-05: live handlers (todo.updated, session.diff, message.updated, message.part.*, session.status, message.diff.updated) drop events for tombstoned deleted sessions; tombstone set bounded (1024, oldest evicted) via rememberDeleted(). - deepseek NEW-03 / wave-A D-P2-15 residual: boundQueueMaps() actually caps both the per-session queue map and the cached-share map, skipping in-flight entries; sweep runs after getCached/create/flush so it can progress. - deepseek NEW-04 / wave-A D-P1-04 residual: pending remote unshare retry moved before the share early-return in sync() so any later sync re-attempts it; removals map bounded (256). Restart-durability residual documented. - deepseek NEW-05: explicit dispose/disposeDirectory/disposeAll/reload now drain an active provide lease (bounded 2s, then log) before teardown; provide leases the exact entry loadEntry() returned, not a reload-replaced entry. - deepseek NEW-06: ws-pool refreshes busyAt as the consumer reads the response so prune only reclaims genuinely stalled lanes; busyTimeout option for tests. - deepseek NEW-07: event remove() tombstones the aggregate so durable subscribers created afterwards complete immediately; cleared on re-commit. - deepseek NEW-08 / opus P3-19: snowflake role-hold window raised 512B -> 64KiB (documented bound) so >512B whitespace gaps are not flushed un-rewritten. - deepseek NEW-09: concurrent stop() coalesced through a WeakMap in both opencode util/process.ts and sdk/js process.ts. - opus NEW-06: frame cache scoped per instance directory; dispose clears only the disposed instance's frames. Tests: 20 new/updated across tui/opencode/core/sdk/js. No push.
1 parent e459adc commit b8bf11b

20 files changed

Lines changed: 682 additions & 47 deletions

File tree

‎packages/core/src/event.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ export const layerWith = (options?: LayerOptions) =>
300300
typed: new Map<string, PubSub.PubSub<Payload>>(),
301301
}
302302
const projectors = new Map<string, Subscriber[]>()
303+
// Aggregates removed by `remove()`: a durable stream created afterwards must end
304+
// immediately instead of parking on a wake that will never receive a null. Cleared
305+
// when the aggregate is re-created by a later commit (v8 NEW-07).
306+
const removedAggregates = new Set<string>()
303307
// TODO: Bind durable projectors to exact type+version before supporting incompatible historical payloads.
304308
const listeners = new Array<Subscriber>()
305309
const { db } = yield* Database.Service
@@ -478,6 +482,7 @@ export const layerWith = (options?: LayerOptions) =>
478482
)
479483
.pipe(retryDurableWrite("durableEvent.commit"), Effect.orDie)
480484
if (committed) {
485+
removedAggregates.delete(committed.aggregateID)
481486
const wakes = pubsub.durable.get(committed.aggregateID)
482487
if (wakes) {
483488
const wakePayload = {
@@ -686,6 +691,7 @@ export const layerWith = (options?: LayerOptions) =>
686691
.pipe(retryDurableWrite("event.remove"), Effect.orDie)
687692
// Terminal null marker ends durable streams against a deleted log; the Deferred
688693
// guarantees termination even when the sliding(1) wake evicts the null.
694+
removedAggregates.add(aggregateID)
689695
const wakes = pubsub.durable.get(aggregateID)
690696
if (wakes)
691697
yield* Effect.forEach(
@@ -771,6 +777,9 @@ export const layerWith = (options?: LayerOptions) =>
771777
if (wakes?.size === 0) pubsub.durable.delete(aggregateID)
772778
}).pipe(Effect.andThen(PubSub.shutdown(wake))),
773779
)
780+
// A subscriber created after the aggregate was removed would otherwise never
781+
// receive the terminal signal; complete its own deferred up front.
782+
if (removedAggregates.has(aggregateID)) yield* Deferred.succeed(removed, undefined)
774783
return { subscription, removed }
775784
})
776785

‎packages/core/test/event.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,42 @@ describe("EventV2", () => {
11601160
}),
11611161
)
11621162

1163+
it.effect("ends a durable stream created after the aggregate was removed", () =>
1164+
Effect.gen(function* () {
1165+
const events = yield* EventV2.Service
1166+
const aggregateID = Session.ID.create()
1167+
yield* events.publish(DurableMessage, durableData(aggregateID, "zero"))
1168+
yield* events.remove(aggregateID)
1169+
1170+
const result = yield* events
1171+
.durable({ aggregateID })
1172+
.pipe(Stream.runCollect, Effect.timeoutOption("1 second"))
1173+
1174+
expect(Option.isSome(result)).toBe(true)
1175+
if (Option.isSome(result)) expect(Array.from(result.value)).toEqual([])
1176+
}),
1177+
)
1178+
1179+
it.effect("allows durable streaming after the aggregate is re-created", () =>
1180+
Effect.gen(function* () {
1181+
const events = yield* EventV2.Service
1182+
const aggregateID = Session.ID.create()
1183+
yield* events.publish(DurableMessage, durableData(aggregateID, "zero"))
1184+
yield* events.remove(aggregateID)
1185+
yield* events.publish(DurableMessage, durableData(aggregateID, "reborn"))
1186+
1187+
const result = yield* events
1188+
.durable({ aggregateID })
1189+
.pipe(Stream.take(1), Stream.runCollect, Effect.timeoutOption("1 second"))
1190+
1191+
expect(Option.isSome(result)).toBe(true)
1192+
if (!Option.isSome(result)) return
1193+
const received = Array.from(result.value)
1194+
expect(received).toHaveLength(1)
1195+
expect(received[0]?.data).toEqual(durableData(aggregateID, "reborn"))
1196+
}),
1197+
)
1198+
11631199
it.effect("runs every live-only listener and the pubsub fan-out before re-raising the defect", () =>
11641200
Effect.gen(function* () {
11651201
const events = yield* EventV2.Service

‎packages/opencode/src/plugin/openai/ws-pool.ts‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface CreateWebSocketFetchOptions {
1414
fallbackTimeout?: number
1515
maxConnectionAge?: number
1616
streamRetries?: number
17+
busyTimeout?: number
1718
}
1819

1920
interface PoolEntry {
@@ -46,6 +47,7 @@ export function createWebSocketFetch(options?: CreateWebSocketFetchOptions) {
4647
const fallbackTimeout = options?.fallbackTimeout ?? DEFAULT_FALLBACK_TIMEOUT
4748
const maxConnectionAge = options?.maxConnectionAge ?? DEFAULT_MAX_CONNECTION_AGE
4849
const streamRetries = options?.streamRetries ?? 5
50+
const busyTimeout = options?.busyTimeout ?? Math.max(idleTimeout, 60_000)
4951
const pruneTimer = setInterval(() => prune(), Math.min(idleTimeout, 60_000))
5052
if (typeof pruneTimer === "object" && "unref" in pruneTimer && typeof pruneTimer.unref === "function") {
5153
pruneTimer.unref()
@@ -162,7 +164,7 @@ export function createWebSocketFetch(options?: CreateWebSocketFetchOptions) {
162164
const first = await firstEvent
163165
if (first !== false) {
164166
resetBackoff(entry)
165-
if (first === true || first.status < 200 || first.status > 599) return response
167+
if (first === true || first.status < 200 || first.status > 599) return withActivity(entry, response)
166168
return new Response(first.body, {
167169
status: first.status,
168170
headers: { "content-type": "application/json", ...first.headers },
@@ -219,7 +221,6 @@ export function createWebSocketFetch(options?: CreateWebSocketFetchOptions) {
219221

220222
function prune() {
221223
const now = Date.now()
222-
const busyTimeout = Math.max(idleTimeout, 60_000)
223224
for (const [key, entry] of pool) {
224225
// A caller that drops the response without reading or cancelling never fires
225226
// `onTerminal`/`onAbort`, so reclaim a slot that has been busy far longer than
@@ -252,7 +253,7 @@ export function createWebSocketFetch(options?: CreateWebSocketFetchOptions) {
252253
}
253254
}
254255

255-
return Object.assign(websocketFetch, { close, remove })
256+
return Object.assign(websocketFetch, { close, remove, prune })
256257
}
257258

258259
function waitForBackoff(entry: PoolEntry, signal?: AbortSignal | null) {
@@ -336,6 +337,31 @@ function invalidate(entry: PoolEntry) {
336337
entry.connectedAt = undefined
337338
}
338339

340+
// Refresh the prune clock as the consumer reads data, so prune only reclaims a lane that
341+
// has genuinely stalled (the caller stopped reading). Without this a legitimate stream
342+
// longer than `busyTimeout` is terminated mid-flight (v8 NEW-06).
343+
function withActivity(entry: PoolEntry, response: Response): Response {
344+
if (!response.body) return response
345+
const reader = response.body.getReader()
346+
return new Response(
347+
new ReadableStream<Uint8Array>({
348+
async pull(controller) {
349+
const { done, value } = await reader.read()
350+
if (done) {
351+
controller.close()
352+
return
353+
}
354+
entry.busyAt = Date.now()
355+
controller.enqueue(value)
356+
},
357+
cancel(reason) {
358+
return reader.cancel(reason)
359+
},
360+
}),
361+
{ status: response.status, statusText: response.statusText, headers: response.headers },
362+
)
363+
}
364+
339365
export function withoutInternalHeaders<T extends { headers?: HeadersInit }>(init: T | undefined): T | undefined {
340366
if (!init?.headers) return init
341367
if (init.headers instanceof Headers) {

‎packages/opencode/src/project/instance-store.ts‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ const layer: Layer.Layer<Service, never, Project.Service | InstanceBootstrap.Ser
6767
return ctx
6868
}).pipe(Effect.withSpan("InstanceStore.boot"))
6969

70+
// Explicit lifecycle ops must not tear down resources an active `provide` lease is
71+
// using. Wait (bounded) for the lease to drain; after the timeout an operator-explicit
72+
// teardown proceeds and logs, so a stuck lease can never deadlock disposal (v8 NEW-05).
73+
const LEASE_DRAIN_TIMEOUT_MS = 2_000
74+
const awaitLease = (entry: Entry) =>
75+
Effect.gen(function* () {
76+
const deadline = Date.now() + LEASE_DRAIN_TIMEOUT_MS
77+
while (entry.uses > 0 && Date.now() < deadline) yield* Effect.sleep("10 millis")
78+
if (entry.uses > 0) yield* Effect.logWarning("instance lease did not drain before disposal", { uses: entry.uses })
79+
})
80+
7081
const removeEntry = (directory: string, entry: Entry) =>
7182
Effect.sync(() => {
7283
if (cache.get(directory) !== entry) return false
@@ -103,6 +114,8 @@ const layer: Layer.Layer<Service, never, Project.Service | InstanceBootstrap.Ser
103114
})
104115

105116
const disposeEntry = Effect.fnUntraced(function* (directory: string, entry: Entry, ctx: InstanceContext) {
117+
if (cache.get(directory) !== entry) return false
118+
yield* awaitLease(entry)
106119
if (cache.get(directory) !== entry) return false
107120
yield* disposeContext(ctx)
108121
if (cache.get(directory) !== entry) return false
@@ -123,15 +136,15 @@ const layer: Layer.Layer<Service, never, Project.Service | InstanceBootstrap.Ser
123136
}
124137
})
125138

126-
const load = (input: LoadInput): Effect.Effect<InstanceContext> => {
139+
const loadEntry = (input: LoadInput): Effect.Effect<{ ctx: InstanceContext; entry: Entry }> => {
127140
const directory = FSUtil.resolve(input.directory)
128141
return Effect.uninterruptibleMask((restore) =>
129142
Effect.gen(function* () {
130143
const existing = cache.get(directory)
131144
if (existing) {
132145
cache.delete(directory)
133146
cache.set(directory, existing)
134-
return yield* restore(Deferred.await(existing.deferred))
147+
return { ctx: yield* restore(Deferred.await(existing.deferred)), entry: existing }
135148
}
136149

137150
yield* evictStale()
@@ -141,11 +154,17 @@ const layer: Layer.Layer<Service, never, Project.Service | InstanceBootstrap.Ser
141154
yield* Effect.logInfo("creating instance", { directory: directory })
142155
yield* completeLoad(directory, input, entry)
143156
}).pipe(Effect.forkIn(scope, { startImmediately: true }))
144-
return yield* restore(Deferred.await(entry.deferred))
157+
return { ctx: yield* restore(Deferred.await(entry.deferred)), entry }
145158
}),
146-
).pipe(Effect.withSpan("InstanceStore.load"))
159+
)
147160
}
148161

162+
const load = (input: LoadInput): Effect.Effect<InstanceContext> =>
163+
loadEntry(input).pipe(
164+
Effect.map((loaded) => loaded.ctx),
165+
Effect.withSpan("InstanceStore.load"),
166+
)
167+
149168
const reload = (input: LoadInput): Effect.Effect<InstanceContext> => {
150169
const directory = FSUtil.resolve(input.directory)
151170
return Effect.uninterruptibleMask((restore) =>
@@ -160,6 +179,7 @@ const layer: Layer.Layer<Service, never, Project.Service | InstanceBootstrap.Ser
160179
yield* Effect.logInfo("reloading instance", { directory: directory })
161180
if (previous) {
162181
yield* Deferred.await(previous.deferred).pipe(Effect.ignore)
182+
yield* awaitLease(previous)
163183
yield* Effect.promise(() => runDisposers(directory))
164184
yield* emitDisposed({ directory, project: input.project?.id })
165185
}
@@ -214,16 +234,15 @@ const layer: Layer.Layer<Service, never, Project.Service | InstanceBootstrap.Ser
214234

215235
const provide = <A, E, R>(input: LoadInput, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
216236
Effect.gen(function* () {
217-
const ctx = yield* load(input)
218-
// Hold a lease for the effect's lifetime so eviction cannot dispose resources
219-
// the effect is still using; released on success, failure, or interruption.
220-
const entry = cache.get(ctx.directory)
221-
if (entry) entry.uses += 1
237+
const { ctx, entry } = yield* loadEntry(input)
238+
// Hold the lease on the exact entry the effect is using, not whatever the cache
239+
// holds now, so a concurrent reload cannot make the lease land on the wrong entry.
240+
entry.uses += 1
222241
return yield* effect.pipe(
223242
Effect.provideService(InstanceRef, ctx),
224243
Effect.ensuring(
225244
Effect.sync(() => {
226-
if (entry) entry.uses = Math.max(0, entry.uses - 1)
245+
entry.uses = Math.max(0, entry.uses - 1)
227246
}),
228247
),
229248
)

‎packages/opencode/src/provider/snowflake.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ const ROLE_ASSISTANT = /"role"\s*:\s*""/g
33
// A `"role":""` match can straddle a chunk boundary, so a per-chunk replace misses
44
// it. Hold back the longest suffix that is a prefix of `"role"\s*:\s*""` and only
55
// rewrite complete matches; the held tail is prepended to the next chunk and flushed
6-
// on close.
7-
const ROLE_HOLD_MAX = 512
6+
// on close. The hold window is 64 KiB: a whitespace gap larger than that is flushed
7+
// un-rewritten rather than retaining an attacker-sized run (documented bound).
8+
const ROLE_HOLD_MAX = 64 * 1024
89
const PARTIAL_ROLE = /^"role"\s*:?\s*"?$|^"(?:r(?:o(?:l(?:e"?)?)?)?)?$/
910

1011
function partialTail(text: string): number {

‎packages/opencode/src/server/routes/instance/httpapi/handlers/event.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function eventResponse(events: EventV2.Interface) {
6565
}) => {
6666
if (event.directory !== instance.directory || event.payload.type !== "server.instance.disposed") return
6767
// The frames retained for this instance are dead weight once it is gone.
68-
clearFrameCache()
68+
clearFrameCache(instance.directory)
6969
Queue.offerUnsafe(queue, {
7070
id: event.payload.id ?? eventID(),
7171
type: "server.instance.disposed",
@@ -90,7 +90,7 @@ function eventResponse(events: EventV2.Interface) {
9090
return HttpServerResponse.stream(
9191
Stream.make({ id: eventID(), type: "server.connected", properties: {} }).pipe(
9292
Stream.concat(output.pipe(Stream.merge(heartbeat, { haltStrategy: "left" }))),
93-
Stream.map((event) => frame(event.id, event.id, event, !isTransientEvent(event.type))),
93+
Stream.map((event) => frame(event.id, event.id, event, !isTransientEvent(event.type), instance.directory)),
9494
Stream.mapArray((batch) => (batch.length <= 1 ? batch : [join(batch)])),
9595
Stream.ensuring(Effect.logInfo("event disconnected")),
9696
),

‎packages/opencode/src/server/routes/instance/httpapi/handlers/sse-frame.ts‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,30 @@ export function isTransientEvent(type: string) {
2222
return TRANSIENT_EVENT_TYPES.has(type)
2323
}
2424

25-
/** Drops retained frames so serialized payloads cannot outlive the instance that produced them. */
26-
export function clearFrameCache() {
27-
frames.clear()
28-
framesBytes = 0
25+
/**
26+
* Drops retained frames. With a `scope`, only that instance's frames go; without one the
27+
* whole cache is cleared. Scoping keeps disposing one instance from dropping frames another
28+
* live instance still serves (v8 NEW-06), while the O-P3-12 hook still clears the disposed
29+
* instance's frames.
30+
*/
31+
export function clearFrameCache(scope?: string) {
32+
if (scope === undefined) {
33+
frames.clear()
34+
framesBytes = 0
35+
return
36+
}
37+
const prefix = `${scope}\u0000`
38+
for (const key of [...frames.keys()]) {
39+
if (!key.startsWith(prefix)) continue
40+
framesBytes -= frames.get(key)?.byteLength ?? 0
41+
frames.delete(key)
42+
}
2943
}
3044

31-
export function frame(key: string, id: string | undefined, payload: unknown, cache = true): Uint8Array {
45+
export function frame(key: string, id: string | undefined, payload: unknown, cache = true, scope?: string): Uint8Array {
46+
const cacheKey = scope === undefined ? key : `${scope}\u0000${key}`
3247
if (cache) {
33-
const cached = frames.get(key)
48+
const cached = frames.get(cacheKey)
3449
if (cached) return cached
3550
}
3651
const bytes = encoder.encode(
@@ -43,7 +58,7 @@ export function frame(key: string, id: string | undefined, payload: unknown, cac
4358
framesBytes -= frames.get(oldest.value)?.byteLength ?? 0
4459
frames.delete(oldest.value)
4560
}
46-
frames.set(key, bytes)
61+
frames.set(cacheKey, bytes)
4762
framesBytes += bytes.byteLength
4863
}
4964
return bytes

0 commit comments

Comments
 (0)