Repository navigation
fix(opencode): settle pending approvals on session teardown #7173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
4d2a396
33aef26
cead9c1
323107a
debfa76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -229,6 +229,8 @@ interface OpenCodeSessionContext { | |
| readonly openCodeSessionId: string; | ||
| readonly pendingPermissions: Map<string, PermissionRequest>; | ||
| readonly pendingQuestions: Map<string, QuestionRequest>; | ||
| /** False once teardown starts so the event pump cannot open new requests. */ | ||
| readonly acceptingRequests: Ref.Ref<boolean>; | ||
| readonly messageRoleById: Map<string, "user" | "assistant">; | ||
| readonly partById: Map<string, Part>; | ||
| readonly emittedTextByPartId: Map<string, string>; | ||
|
|
@@ -638,12 +640,57 @@ export function makeOpenCodeAdapter( | |
| })), | ||
| ); | ||
|
|
||
| // Layer-level finalizer: when the adapter layer shuts down, stop every | ||
| // session. Each session's `Scope.close` tears down its spawned OpenCode | ||
| // server (via the `ChildProcessSpawner` finalizer installed in | ||
| // `startOpenCodeServerProcess`) and interrupts the forked event/exit | ||
| // fibers. Consumers that can't reason about Effect scopes therefore | ||
| // cannot leak OpenCode child processes by forgetting to call `stopAll`. | ||
| const emit = (event: ProviderRuntimeEvent) => | ||
| Queue.offer(runtimeEvents, event).pipe(Effect.asVoid); | ||
|
|
||
| const settleOpenCodePendingAsCancelled = (context: OpenCodeSessionContext) => | ||
| Effect.gen(function* () { | ||
| yield* Ref.set(context.acceptingRequests, false); | ||
| const permissions = [...context.pendingPermissions.entries()]; | ||
|
macroscopeapp[bot] marked this conversation as resolved.
Outdated
|
||
| yield* Effect.forEach( | ||
| permissions, | ||
| ([requestId, request]) => | ||
| Effect.gen(function* () { | ||
| yield* emit({ | ||
| ...(yield* buildEventBase({ | ||
| threadId: context.session.threadId, | ||
| requestId, | ||
| })), | ||
| type: "request.resolved", | ||
| payload: { | ||
| requestType: mapPermissionToRequestType(request.permission), | ||
| decision: "cancel", | ||
| }, | ||
| }); | ||
| context.pendingPermissions.delete(requestId); | ||
| }), | ||
| { discard: true }, | ||
| ); | ||
| const questions = [...context.pendingQuestions.keys()]; | ||
| yield* Effect.forEach( | ||
| questions, | ||
| (requestId) => | ||
| Effect.gen(function* () { | ||
| yield* emit({ | ||
| ...(yield* buildEventBase({ | ||
| threadId: context.session.threadId, | ||
| requestId, | ||
| })), | ||
| type: "user-input.resolved", | ||
| payload: { answers: {} }, | ||
| }); | ||
| context.pendingQuestions.delete(requestId); | ||
| }), | ||
| { discard: true }, | ||
| ); | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes backend behavior (pending permissions/questions are now settled as Posted via Macroscope — Effect Service Conventions
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added focused
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I'm unable to act on this request because you do not have permissions within this repository. |
||
|
|
||
| // Layer-level finalizer: when the adapter layer shuts down, settle every | ||
| // pending request then stop the session. Each session's `Scope.close` | ||
| // tears down its spawned OpenCode server (via the `ChildProcessSpawner` | ||
| // finalizer installed in `startOpenCodeServerProcess`) and interrupts | ||
| // the forked event/exit fibers. Provider instance remove/replace closes | ||
| // this scope without calling `stopAll`, so settlement has to live here. | ||
| yield* Effect.addFinalizer(() => | ||
| Effect.gen(function* () { | ||
| const contexts = [...sessions.values()]; | ||
|
|
@@ -653,7 +700,12 @@ export function makeOpenCodeAdapter( | |
| // the remaining cleanups. | ||
| yield* Effect.forEach( | ||
| contexts, | ||
| (context) => Effect.ignoreCause(stopOpenCodeContext(context)), | ||
| (context) => | ||
| Effect.ignoreCause( | ||
| settleOpenCodePendingAsCancelled(context).pipe( | ||
| Effect.andThen(stopOpenCodeContext(context)), | ||
| ), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Settle failure skips session teardownMedium Severity
Additional Locations (2)Reviewed by Cursor Bugbot for commit debfa76. Configure here. |
||
| ), | ||
| { concurrency: "unbounded", discard: true }, | ||
| ); | ||
| // Close the logger AFTER session teardown so any final lifecycle | ||
|
|
@@ -665,9 +717,6 @@ export function makeOpenCodeAdapter( | |
| } | ||
| }).pipe(Effect.ensuring(Queue.shutdown(runtimeEvents))), | ||
| ); | ||
|
|
||
| const emit = (event: ProviderRuntimeEvent) => | ||
| Queue.offer(runtimeEvents, event).pipe(Effect.asVoid); | ||
| const writeNativeEvent = ( | ||
| threadId: ThreadId, | ||
| event: { | ||
|
|
@@ -696,6 +745,7 @@ export function makeOpenCodeAdapter( | |
| } | ||
| const turnId = context.activeTurnId; | ||
| sessions.delete(context.session.threadId); | ||
| yield* settleOpenCodePendingAsCancelled(context).pipe(Effect.ignore); | ||
| // Emit lifecycle events BEFORE tearing down the scope. Both call sites | ||
| // run this inside a fiber forked via `Effect.forkIn(context.sessionScope)`; | ||
| // closing that scope triggers the fiber-interrupt finalizer, so any | ||
|
|
@@ -956,6 +1006,22 @@ export function makeOpenCodeAdapter( | |
| } | ||
|
|
||
| case "permission.asked": { | ||
| if (!(yield* Ref.get(context.acceptingRequests))) { | ||
| yield* emit({ | ||
| ...(yield* buildEventBase({ | ||
| threadId: context.session.threadId, | ||
| turnId, | ||
| requestId: event.properties.id, | ||
| raw: event, | ||
| })), | ||
| type: "request.resolved", | ||
| payload: { | ||
| requestType: mapPermissionToRequestType(event.properties.permission), | ||
| decision: "cancel", | ||
| }, | ||
| }); | ||
| break; | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| context.pendingPermissions.set(event.properties.id, event.properties); | ||
| yield* emit({ | ||
| ...(yield* buildEventBase({ | ||
|
|
@@ -996,6 +1062,19 @@ export function makeOpenCodeAdapter( | |
| } | ||
|
|
||
| case "question.asked": { | ||
| if (!(yield* Ref.get(context.acceptingRequests))) { | ||
| yield* emit({ | ||
| ...(yield* buildEventBase({ | ||
| threadId: context.session.threadId, | ||
| turnId, | ||
| requestId: event.properties.id, | ||
| raw: event, | ||
| })), | ||
| type: "user-input.resolved", | ||
| payload: { answers: {} }, | ||
| }); | ||
| break; | ||
| } | ||
| context.pendingQuestions.set(event.properties.id, event.properties); | ||
| yield* emit({ | ||
| ...(yield* buildEventBase({ | ||
|
|
@@ -1210,6 +1289,7 @@ export function makeOpenCodeAdapter( | |
| const resumeSessionId = parseOpenCodeResume(input.resumeCursor)?.sessionId; | ||
| const existing = sessions.get(input.threadId); | ||
| if (existing) { | ||
| yield* settleOpenCodePendingAsCancelled(existing); | ||
| yield* stopOpenCodeContext(existing); | ||
| sessions.delete(input.threadId); | ||
| } | ||
|
|
@@ -1394,6 +1474,7 @@ export function makeOpenCodeAdapter( | |
| openCodeSessionId: started.openCodeSession.id, | ||
| pendingPermissions: new Map(), | ||
| pendingQuestions: new Map(), | ||
| acceptingRequests: yield* Ref.make(true), | ||
| partById: new Map(), | ||
| emittedTextByPartId: new Map(), | ||
| messageRoleById: new Map(), | ||
|
|
@@ -1626,6 +1707,7 @@ export function makeOpenCodeAdapter( | |
| threadId, | ||
| }); | ||
| } | ||
| yield* settleOpenCodePendingAsCancelled(context); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| const stopped = yield* stopOpenCodeContext(context); | ||
| sessions.delete(threadId); | ||
| if (!stopped) { | ||
|
|
@@ -1710,7 +1792,12 @@ export function makeOpenCodeAdapter( | |
| // interrupt the sibling fibers. Same pattern as the layer finalizer. | ||
| yield* Effect.forEach( | ||
| contexts, | ||
| (context) => Effect.ignoreCause(stopOpenCodeContext(context)), | ||
| (context) => | ||
| Effect.ignoreCause( | ||
| settleOpenCodePendingAsCancelled(context).pipe( | ||
| Effect.andThen(stopOpenCodeContext(context)), | ||
| ), | ||
| ), | ||
| { concurrency: "unbounded", discard: true }, | ||
| ); | ||
| }); | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.