Skip to content
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion apps/server/src/provider/Layers/OpenCodeAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down Expand Up @@ -668,6 +670,48 @@ export function makeOpenCodeAdapter(

const emit = (event: ProviderRuntimeEvent) =>
Queue.offer(runtimeEvents, event).pipe(Effect.asVoid);

Comment thread
cursor[bot] marked this conversation as resolved.
const settleOpenCodePendingAsCancelled = (context: OpenCodeSessionContext) =>
Effect.gen(function* () {
yield* Ref.set(context.acceptingRequests, false);
const permissions = [...context.pendingPermissions.entries()];
Comment thread
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 },
);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes backend behavior (pending permissions/questions are now settled as cancel / empty answers on stop, resume-restart, unexpected exit, and stopAll, and late permission.asked / question.asked events are auto-resolved once acceptingRequests is false), but no tests accompany it. Consider adding focused cases to OpenCodeAdapter.test.ts — the existing it.layer(OpenCodeAdapterTestLayer) harness can push a permission.asked event and then assert the emitted request.resolved on stop/stopAll — so the new settle path is pinned.

Posted via Macroscope — Effect Service Conventions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added focused OpenCodeAdapter tests in cead9c1: a permission.asked event is opened, then stopSession / stopAll emit request.resolved with decision: "cancel".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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: {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
context.pendingPermissions.set(event.properties.id, event.properties);
yield* emit({
...(yield* buildEventBase({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -1626,6 +1702,7 @@ export function makeOpenCodeAdapter(
threadId,
});
}
yield* settleOpenCodePendingAsCancelled(context);
Comment thread
cursor[bot] marked this conversation as resolved.
const stopped = yield* stopOpenCodeContext(context);
sessions.delete(threadId);
if (!stopped) {
Expand Down Expand Up @@ -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 },
);
});
Expand Down
Loading