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 2 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>; | ||
|
|
@@ -668,6 +670,48 @@ export function makeOpenCodeAdapter( | |
|
|
||
| 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. |
||
| const writeNativeEvent = ( | ||
| threadId: ThreadId, | ||
| event: { | ||
|
|
@@ -696,6 +740,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 +1001,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 +1057,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 +1284,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 +1469,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 +1702,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 +1787,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.