|
| 1 | +export * as ConfigReload from "./reload" |
| 2 | + |
| 3 | +import { EventV2 } from "@opencode-ai/core/event" |
| 4 | +import { LayerNode } from "@opencode-ai/core/effect/layer-node" |
| 5 | +import { Context, Effect, Layer, Schema } from "effect" |
| 6 | +import { EventV2Bridge } from "@/event-v2-bridge" |
| 7 | +import { InstanceState } from "@/effect/instance-state" |
| 8 | +import { InstanceStore } from "@/project/instance-store" |
| 9 | +import { SessionID } from "@/session/schema" |
| 10 | +import { SessionPrompt } from "@/session/prompt" |
| 11 | + |
| 12 | +export const Event = { |
| 13 | + Pending: EventV2.define({ |
| 14 | + type: "config.reload.pending", |
| 15 | + schema: { |
| 16 | + pending: Schema.Boolean, |
| 17 | + }, |
| 18 | + }), |
| 19 | + Executing: EventV2.define({ |
| 20 | + type: "config.reload.executing", |
| 21 | + schema: { |
| 22 | + executing: Schema.Boolean, |
| 23 | + bootstrapCycle: Schema.optional(Schema.Int.check(Schema.isGreaterThanOrEqualTo(1))), |
| 24 | + }, |
| 25 | + }), |
| 26 | + Done: EventV2.define({ |
| 27 | + type: "config.reload.done", |
| 28 | + schema: { |
| 29 | + sessionID: Schema.optional(Schema.String), |
| 30 | + }, |
| 31 | + }), |
| 32 | +} |
| 33 | + |
| 34 | +export type RequestResult = { |
| 35 | + immediate: boolean |
| 36 | + input: InstanceStore.LoadInput |
| 37 | + bootstrapCycle?: number |
| 38 | + resumeSessionID?: string |
| 39 | +} |
| 40 | + |
| 41 | +export type Status = { |
| 42 | + pending: boolean |
| 43 | + executing: boolean |
| 44 | + bootstrapCycle?: number |
| 45 | +} |
| 46 | + |
| 47 | +export type LocationInput = { |
| 48 | + directory: string |
| 49 | + workspaceID?: string |
| 50 | +} |
| 51 | + |
| 52 | +export type ResumeFn = (sessionID: string) => Effect.Effect<void> |
| 53 | + |
| 54 | +export interface Interface { |
| 55 | + readonly isPending: () => Effect.Effect<boolean> |
| 56 | + readonly start: (sessionID: string) => Effect.Effect<void> |
| 57 | + readonly finish: (sessionID: string) => Effect.Effect<void> |
| 58 | + readonly getBootstrapCycle: () => Effect.Effect<number> |
| 59 | + readonly status: () => Effect.Effect<Status> |
| 60 | + readonly statusForLocation: (input: LocationInput) => Effect.Effect<Status> |
| 61 | + readonly releaseBlocker: (blockerID: string) => Effect.Effect<void> |
| 62 | + readonly completeBootstrap: (cycle: number) => Effect.Effect<boolean> |
| 63 | + readonly completeBootstrapForLocation: (input: LocationInput & { cycle: number }) => Effect.Effect<boolean> |
| 64 | + readonly request: (sessionID?: string) => Effect.Effect<RequestResult> |
| 65 | + readonly check: () => Effect.Effect<void> |
| 66 | +} |
| 67 | + |
| 68 | +export class Service extends Context.Service<Service, Interface>()("@opencode/ConfigReload") {} |
| 69 | + |
| 70 | +type State = { |
| 71 | + pending: boolean |
| 72 | + reloadInFlight: boolean |
| 73 | + reloadInput?: InstanceStore.LoadInput |
| 74 | + resumeSessionID?: string |
| 75 | + bootstrapCycle: number |
| 76 | + active: Set<string> |
| 77 | + blockers: Set<string> |
| 78 | +} |
| 79 | + |
| 80 | +export const layer = Layer.effect( |
| 81 | + Service, |
| 82 | + Effect.gen(function* () { |
| 83 | + const prompt = yield* SessionPrompt.Service |
| 84 | + const events = yield* EventV2Bridge.Service |
| 85 | + const store = yield* InstanceStore.Service |
| 86 | + const states = new Map<string, State>() |
| 87 | + |
| 88 | + const resume: ResumeFn = (sessionID) => |
| 89 | + prompt.loop({ sessionID: SessionID.make(sessionID) }).pipe( |
| 90 | + Effect.catchCause((cause) => |
| 91 | + Effect.logWarning("config reload auto-resume failed", { sessionID, cause: String(cause) }), |
| 92 | + ), |
| 93 | + Effect.asVoid, |
| 94 | + ) |
| 95 | + |
| 96 | + const isPending = Effect.fn("ConfigReload.isPending")(function* () { |
| 97 | + return [...states.values()].some((state) => state.pending) |
| 98 | + }) |
| 99 | + |
| 100 | + const start = Effect.fn("ConfigReload.start")(function* (sessionID: string) { |
| 101 | + const state = yield* currentState(states) |
| 102 | + state.active.add(sessionID) |
| 103 | + yield* Effect.logDebug("config reload waiting for all sessions to finish", { sessionID }) |
| 104 | + }) |
| 105 | + |
| 106 | + const finish = Effect.fn("ConfigReload.finish")(function* (sessionID: string) { |
| 107 | + const state = yield* currentState(states) |
| 108 | + state.active.delete(sessionID) |
| 109 | + yield* Effect.logDebug("config reload session finished current turn", { sessionID }) |
| 110 | + yield* continueOrDone(state, store, events, resume) |
| 111 | + }) |
| 112 | + |
| 113 | + const getBootstrapCycle = Effect.fn("ConfigReload.getBootstrapCycle")(function* () { |
| 114 | + return (yield* currentState(states)).bootstrapCycle |
| 115 | + }) |
| 116 | + |
| 117 | + const status = Effect.fn("ConfigReload.status")(function* () { |
| 118 | + return stateStatus(yield* currentState(states)) |
| 119 | + }) |
| 120 | + |
| 121 | + const statusForLocation = Effect.fn("ConfigReload.statusForLocation")(function* (input: LocationInput) { |
| 122 | + const state = findStateForLocation(states, input) |
| 123 | + if (!state) return { pending: false, executing: false } satisfies Status |
| 124 | + return stateStatus(state) |
| 125 | + }) |
| 126 | + |
| 127 | + const releaseBlocker = Effect.fn("ConfigReload.releaseBlocker")(function* (blockerID: string) { |
| 128 | + const state = yield* currentState(states) |
| 129 | + state.blockers.delete(blockerID) |
| 130 | + yield* Effect.logDebug("config reload blocker released", { blockerID }) |
| 131 | + yield* continueOrDone(state, store, events, resume) |
| 132 | + }) |
| 133 | + |
| 134 | + const completeBootstrap = Effect.fn("ConfigReload.completeBootstrap")(function* (cycle: number) { |
| 135 | + const current = yield* currentInput() |
| 136 | + const state = findStateForCycle(states, current, cycle) |
| 137 | + if (!state) return false |
| 138 | + state.blockers.delete("tui-bootstrap") |
| 139 | + yield* Effect.logDebug("config reload bootstrap completed", { cycle }) |
| 140 | + yield* continueOrDone(state, store, events, resume) |
| 141 | + return true |
| 142 | + }) |
| 143 | + |
| 144 | + const completeBootstrapForLocation = Effect.fn("ConfigReload.completeBootstrapForLocation")(function* ( |
| 145 | + input: LocationInput & { cycle: number }, |
| 146 | + ) { |
| 147 | + const state = findStateForLocation(states, input, input.cycle) |
| 148 | + if (!state) return false |
| 149 | + state.blockers.delete("tui-bootstrap") |
| 150 | + yield* Effect.logDebug("config reload bootstrap completed", { cycle: input.cycle }) |
| 151 | + yield* continueOrDone(state, store, events, resume) |
| 152 | + return true |
| 153 | + }) |
| 154 | + |
| 155 | + const request = Effect.fn("ConfigReload.request")(function* (sessionID?: string) { |
| 156 | + const current = yield* currentInput() |
| 157 | + const state = getState(states, current.key) |
| 158 | + state.reloadInput = current.input |
| 159 | + if (sessionID) state.resumeSessionID = sessionID |
| 160 | + if (isBlocked(state)) { |
| 161 | + state.pending = true |
| 162 | + yield* Effect.logInfo("config reload queued", { sessionID }) |
| 163 | + yield* publish(events, Event.Pending, { pending: true }) |
| 164 | + return { immediate: false, input: current.input, resumeSessionID: sessionID } satisfies RequestResult |
| 165 | + } |
| 166 | + yield* Effect.logInfo("config reload executing immediately", { sessionID }) |
| 167 | + const execution = yield* prepareExecution(state, current.input, events) |
| 168 | + return { |
| 169 | + immediate: true, |
| 170 | + input: current.input, |
| 171 | + bootstrapCycle: execution.bootstrapCycle, |
| 172 | + resumeSessionID: sessionID, |
| 173 | + } satisfies RequestResult |
| 174 | + }) |
| 175 | + |
| 176 | + const check = Effect.fn("ConfigReload.check")(function* () { |
| 177 | + const current = yield* currentInput() |
| 178 | + const state = getState(states, current.key) |
| 179 | + if (!state.pending) return |
| 180 | + if (isBlocked(state)) { |
| 181 | + yield* Effect.logDebug("config reload waiting for sessions and blockers to finish", { |
| 182 | + active: [...state.active], |
| 183 | + blockers: [...state.blockers], |
| 184 | + }) |
| 185 | + return |
| 186 | + } |
| 187 | + yield* Effect.logInfo("config reload executing deferred request") |
| 188 | + yield* executePending(state, current.input, store, events, resume) |
| 189 | + }) |
| 190 | + |
| 191 | + return Service.of({ |
| 192 | + isPending, |
| 193 | + start, |
| 194 | + finish, |
| 195 | + getBootstrapCycle, |
| 196 | + status, |
| 197 | + statusForLocation, |
| 198 | + releaseBlocker, |
| 199 | + completeBootstrap, |
| 200 | + completeBootstrapForLocation, |
| 201 | + request, |
| 202 | + check, |
| 203 | + }) |
| 204 | + }), |
| 205 | +) |
| 206 | + |
| 207 | +function isBlocked(state: State) { |
| 208 | + return state.active.size > 0 || state.blockers.size > 0 |
| 209 | +} |
| 210 | + |
| 211 | +function currentInput() { |
| 212 | + return Effect.gen(function* () { |
| 213 | + const ctx = yield* InstanceState.context |
| 214 | + const workspaceID = yield* InstanceState.workspaceID |
| 215 | + return { |
| 216 | + key: `${ctx.directory}\0${ctx.worktree}\0${workspaceID ?? ""}`, |
| 217 | + input: { |
| 218 | + directory: ctx.directory, |
| 219 | + worktree: ctx.worktree, |
| 220 | + project: ctx.project, |
| 221 | + }, |
| 222 | + } |
| 223 | + }) |
| 224 | +} |
| 225 | + |
| 226 | +function currentState(states: Map<string, State>) { |
| 227 | + return Effect.map(currentInput(), (current) => getState(states, current.key)) |
| 228 | +} |
| 229 | + |
| 230 | +function getState(states: Map<string, State>, key: string) { |
| 231 | + const existing = states.get(key) |
| 232 | + if (existing) return existing |
| 233 | + const state: State = { |
| 234 | + pending: false, |
| 235 | + reloadInFlight: false, |
| 236 | + bootstrapCycle: 0, |
| 237 | + active: new Set(), |
| 238 | + blockers: new Set(), |
| 239 | + } |
| 240 | + states.set(key, state) |
| 241 | + return state |
| 242 | +} |
| 243 | + |
| 244 | +function findStateForCycle(states: Map<string, State>, current: { key: string; input: InstanceStore.LoadInput }, cycle: number) { |
| 245 | + const exact = states.get(current.key) |
| 246 | + if (exact?.bootstrapCycle === cycle) return exact |
| 247 | + const locationPrefix = `${current.input.directory}\0${current.input.worktree}\0` |
| 248 | + return [...states.entries()].find(([key, state]) => key.startsWith(locationPrefix) && state.bootstrapCycle === cycle)?.[1] |
| 249 | +} |
| 250 | + |
| 251 | +function findStateForLocation(states: Map<string, State>, input: LocationInput, cycle?: number) { |
| 252 | + const locationPrefix = `${input.directory}\0` |
| 253 | + const workspaceSuffix = input.workspaceID ? `\0${input.workspaceID}` : undefined |
| 254 | + return [...states.entries()].find(([key, state]) => { |
| 255 | + if (!key.startsWith(locationPrefix)) return false |
| 256 | + if (workspaceSuffix && !key.endsWith(workspaceSuffix)) return false |
| 257 | + if (cycle !== undefined) return state.bootstrapCycle === cycle |
| 258 | + return true |
| 259 | + })?.[1] |
| 260 | +} |
| 261 | + |
| 262 | +function stateStatus(state: State) { |
| 263 | + return { |
| 264 | + pending: state.pending, |
| 265 | + executing: state.reloadInFlight, |
| 266 | + bootstrapCycle: state.bootstrapCycle > 0 ? state.bootstrapCycle : undefined, |
| 267 | + } satisfies Status |
| 268 | +} |
| 269 | + |
| 270 | +function startBlocker(state: State, blockerID: string) { |
| 271 | + state.blockers.add(blockerID) |
| 272 | + if (blockerID === "tui-bootstrap") state.bootstrapCycle++ |
| 273 | +} |
| 274 | + |
| 275 | +function prepareExecution(state: State, input: InstanceStore.LoadInput, events: EventV2.Interface) { |
| 276 | + return Effect.gen(function* () { |
| 277 | + state.pending = false |
| 278 | + state.active.clear() |
| 279 | + state.blockers.clear() |
| 280 | + startBlocker(state, "tui-bootstrap") |
| 281 | + state.reloadInFlight = true |
| 282 | + state.reloadInput = input |
| 283 | + yield* publish(events, Event.Pending, { pending: false }) |
| 284 | + yield* publish(events, Event.Executing, { executing: true, bootstrapCycle: state.bootstrapCycle }) |
| 285 | + return { input, bootstrapCycle: state.bootstrapCycle } |
| 286 | + }) |
| 287 | +} |
| 288 | + |
| 289 | +function executePending( |
| 290 | + state: State, |
| 291 | + fallbackInput: InstanceStore.LoadInput, |
| 292 | + store: InstanceStore.Interface, |
| 293 | + events: EventV2.Interface, |
| 294 | + _resume: ResumeFn, |
| 295 | +) { |
| 296 | + return Effect.gen(function* () { |
| 297 | + const execution = yield* prepareExecution(state, state.reloadInput ?? fallbackInput, events) |
| 298 | + yield* store.reload(execution.input).pipe( |
| 299 | + Effect.catchCause((cause) => |
| 300 | + Effect.logError("config reload store.reload failed", { cause: String(cause) }), |
| 301 | + ), |
| 302 | + ) |
| 303 | + }) |
| 304 | +} |
| 305 | + |
| 306 | +function continueOrDone( |
| 307 | + state: State, |
| 308 | + store: InstanceStore.Interface, |
| 309 | + events: EventV2.Interface, |
| 310 | + resume: ResumeFn, |
| 311 | +) { |
| 312 | + return Effect.gen(function* () { |
| 313 | + if (isBlocked(state)) return |
| 314 | + if (state.pending) { |
| 315 | + if (!state.reloadInput) return |
| 316 | + yield* Effect.logInfo("config reload executing deferred request") |
| 317 | + yield* executePending(state, state.reloadInput, store, events, resume) |
| 318 | + return |
| 319 | + } |
| 320 | + if (!state.reloadInFlight) return |
| 321 | + state.reloadInFlight = false |
| 322 | + yield* Effect.logInfo("config reload completed") |
| 323 | + yield* publish(events, Event.Done, { sessionID: state.resumeSessionID }) |
| 324 | + if (state.resumeSessionID) { |
| 325 | + yield* Effect.logInfo("config reload auto-resuming session", { sessionID: state.resumeSessionID }) |
| 326 | + yield* resume(state.resumeSessionID) |
| 327 | + } |
| 328 | + }) |
| 329 | +} |
| 330 | + |
| 331 | +function publish<Definition extends EventV2.Definition>( |
| 332 | + events: EventV2.Interface, |
| 333 | + definition: Definition, |
| 334 | + data: EventV2.Data<Definition>, |
| 335 | +) { |
| 336 | + return events.publish(definition, data).pipe(Effect.asVoid) |
| 337 | +} |
| 338 | + |
| 339 | +export const node = LayerNode.make({ |
| 340 | + service: Service, |
| 341 | + layer, |
| 342 | + deps: [EventV2Bridge.node, InstanceStore.node, SessionPrompt.node], |
| 343 | +}) |
0 commit comments