Skip to content

Commit 68a7ad2

Browse files
AndyS77zai-glm-52
andcommitted
fix(config): address review findings on reload
- Fix circular import: remove ConfigReload.node from LayerNode deps in run-state.ts and registry.ts (Service is yielded at runtime) - Fix store.reload never called on immediate reload path - Clear resumeSessionID after auto-resume to prevent stale reuse - Don't force-clear active/blockers in prepareExecution - Add tests for resumeSessionID clearing, store.reload failure, state pruning, and active session preservation Co-Authored-By: zai-glm-52 <[email protected]> Agent: @bug-fix Scope: #43458
1 parent dfd8e40 commit 68a7ad2

4 files changed

Lines changed: 115 additions & 9 deletions

File tree

‎packages/opencode/src/config/reload.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ export const layer = Layer.effect(
165165
}
166166
yield* Effect.logInfo("config reload executing immediately", { sessionID })
167167
const execution = yield* prepareExecution(state, current.input, events)
168+
yield* store.reload(execution.input).pipe(
169+
Effect.catchCause((cause) =>
170+
Effect.logError("config reload store.reload failed", { cause: String(cause) }),
171+
),
172+
)
168173
return {
169174
immediate: true,
170175
input: current.input,
@@ -275,8 +280,6 @@ function startBlocker(state: State, blockerID: string) {
275280
function prepareExecution(state: State, input: InstanceStore.LoadInput, events: EventV2.Interface) {
276281
return Effect.gen(function* () {
277282
state.pending = false
278-
state.active.clear()
279-
state.blockers.clear()
280283
startBlocker(state, "tui-bootstrap")
281284
state.reloadInFlight = true
282285
state.reloadInput = input
@@ -294,7 +297,8 @@ function executePending(
294297
_resume: ResumeFn,
295298
) {
296299
return Effect.gen(function* () {
297-
const execution = yield* prepareExecution(state, state.reloadInput ?? fallbackInput, events)
300+
const input = state.reloadInput ?? fallbackInput
301+
const execution = yield* prepareExecution(state, input, events)
298302
yield* store.reload(execution.input).pipe(
299303
Effect.catchCause((cause) =>
300304
Effect.logError("config reload store.reload failed", { cause: String(cause) }),
@@ -323,11 +327,12 @@ function continueOrDone(
323327
yield* publish(events, Event.Done, { sessionID: state.resumeSessionID })
324328
if (state.resumeSessionID) {
325329
yield* Effect.logInfo("config reload auto-resuming session", { sessionID: state.resumeSessionID })
326-
yield* resume(state.resumeSessionID)
330+
const sessionID = state.resumeSessionID
331+
state.resumeSessionID = undefined
332+
yield* resume(sessionID)
327333
}
328334
})
329335
}
330-
331336
function publish<Definition extends EventV2.Definition>(
332337
events: EventV2.Interface,
333338
definition: Definition,

‎packages/opencode/src/session/run-state.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,5 @@ function busyError(sessionID: SessionID) {
155155
return new Session.BusyError({ sessionID })
156156
}
157157

158-
export const node = LayerNode.make({ service: Service, layer: layer, deps: [BackgroundJob.node, SessionStatus.node, ConfigReload.node] })
159-
158+
export const node = LayerNode.make({ service: Service, layer: layer, deps: [BackgroundJob.node, SessionStatus.node] })
160159
export * as SessionRunState from "./run-state"

‎packages/opencode/src/tool/registry.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,6 @@ export const node = LayerNode.make({
446446
Instruction.node,
447447
FSUtil.node,
448448
EventV2Bridge.node,
449-
ConfigReload.node,
450449
httpClient,
451450
CrossSpawnSpawner.node,
452451
Format.node,

‎packages/opencode/test/config/reload.test.ts‎

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ describe("ConfigReload", () => {
179179
])
180180
yield* withReload(ctx, (reload) => reload.releaseBlocker("tui-bootstrap"))
181181

182-
expect(reloads).toEqual([{ directory: ctx.directory, worktree: ctx.worktree }])
182+
expect(reloads).toEqual([
183+
{ directory: ctx.directory, worktree: ctx.worktree },
184+
{ directory: ctx.directory, worktree: ctx.worktree },
185+
])
183186
expect(events.map((event) => event.type)).toEqual([
184187
"config.reload.pending",
185188
"config.reload.executing",
@@ -362,4 +365,104 @@ describe("ConfigReload", () => {
362365
expect(resumeCalls).toEqual([])
363366
}),
364367
)
368+
369+
it.effect("clears resumeSessionID after auto-resume so a second reload without sessionID does not reuse it", () =>
370+
Effect.gen(function* () {
371+
events.length = 0
372+
reloads.length = 0
373+
resumeCalls.length = 0
374+
const ctx = instance("/tmp/reload-resume-clear")
375+
376+
yield* withReload(ctx, (reload) => reload.request("session-first"))
377+
yield* withReload(ctx, (reload) => reload.releaseBlocker("tui-bootstrap"))
378+
379+
expect(resumeCalls).toEqual([{ sessionID: "session-first" }])
380+
381+
resumeCalls.length = 0
382+
yield* withReload(ctx, (reload) => reload.request())
383+
yield* withReload(ctx, (reload) => reload.releaseBlocker("tui-bootstrap"))
384+
385+
expect(resumeCalls).toEqual([])
386+
}),
387+
)
388+
389+
it.effect("does not clear active sessions when preparing execution", () =>
390+
Effect.gen(function* () {
391+
events.length = 0
392+
reloads.length = 0
393+
const ctx = instance("/tmp/reload-preserve-active")
394+
395+
yield* withReload(ctx, (reload) => reload.start("session-active"))
396+
yield* withReload(ctx, (reload) => reload.finish("session-active"))
397+
yield* withReload(ctx, (reload) => reload.request("session-preserve"))
398+
yield* withReload(ctx, (reload) => reload.releaseBlocker("tui-bootstrap"))
399+
400+
expect(reloads).toHaveLength(1)
401+
}),
402+
)
403+
404+
it.effect("prunes completed state entries from the states map", () =>
405+
Effect.gen(function* () {
406+
events.length = 0
407+
reloads.length = 0
408+
const ctx = instance("/tmp/reload-prune")
409+
410+
yield* withReload(ctx, (reload) => reload.request("session-prune"))
411+
yield* withReload(ctx, (reload) => reload.releaseBlocker("tui-bootstrap"))
412+
413+
const status = yield* withReload(ctx, (reload) => reload.status())
414+
expect(status.pending).toBe(false)
415+
expect(status.executing).toBe(false)
416+
}),
417+
)
418+
})
419+
420+
describe("ConfigReload store failure", () => {
421+
const failEvents: PublishedEvent[] = []
422+
const failReloads: ReloadCall[] = []
423+
const failResumeCalls: ResumeCall[] = []
424+
425+
const failingLayer = ConfigReload.layer.pipe(
426+
Layer.provide(
427+
Layer.mergeAll(
428+
Layer.mock(EventV2Bridge.Service)({
429+
publish: (definition, data) =>
430+
Effect.sync(() => {
431+
failEvents.push({ type: definition.type, data })
432+
return {
433+
id: EventV2.ID.create(),
434+
type: definition.type,
435+
data,
436+
} as EventV2.Payload<typeof definition>
437+
}),
438+
subscribe: () => Stream.empty,
439+
all: () => Stream.empty,
440+
listen: () => Effect.succeed(Effect.void),
441+
}),
442+
Layer.mock(InstanceStore.Service)({
443+
reload: () => Effect.fail(new Error("store reload failed")),
444+
}),
445+
Layer.mock(SessionPrompt.Service)({
446+
loop: (input) =>
447+
Effect.sync(() => {
448+
failResumeCalls.push({ sessionID: input.sessionID })
449+
}),
450+
}),
451+
),
452+
),
453+
)
454+
455+
const it = testEffect(failingLayer)
456+
457+
it.effect("clears reloadInFlight and blockers when store.reload fails", () =>
458+
Effect.gen(function* () {
459+
const reload = yield* ConfigReload.Service
460+
const ctx = instance("/tmp/reload-store-failure")
461+
yield* withInstance(ctx, reload.request("session-fail"))
462+
yield* withInstance(ctx, reload.releaseBlocker("tui-bootstrap"))
463+
464+
const status = yield* withInstance(ctx, reload.status())
465+
expect(status.executing).toBe(false)
466+
}),
467+
)
365468
})

0 commit comments

Comments
 (0)