Skip to content

Commit c463930

Browse files
committed
fix(server): bound OpenCode child cleanup requests
1 parent 196548d commit c463930

2 files changed

Lines changed: 108 additions & 18 deletions

File tree

‎apps/server/src/provider/Layers/OpenCodeAdapter.test.ts‎

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ const runtimeMock = {
7171
| null,
7272
sessionChildrenCalls: [] as string[],
7373
sessionChildrenById: new Map<string, Array<{ id: string }>>(),
74+
sessionChildrenImplementation: null as
75+
| ((sessionID: string) => Promise<Array<{ id: string }>>)
76+
| null,
7477
closeCalls: [] as string[],
7578
revertCalls: [] as Array<{ sessionID: string; messageID?: string }>,
7679
messageCalls: [] as Array<{ sessionID: string; messageID: string }>,
@@ -120,6 +123,7 @@ const runtimeMock = {
120123
this.state.abortImplementation = null;
121124
this.state.sessionChildrenCalls.length = 0;
122125
this.state.sessionChildrenById.clear();
126+
this.state.sessionChildrenImplementation = null;
123127
this.state.closeCalls.length = 0;
124128
this.state.revertCalls.length = 0;
125129
this.state.messageCalls.length = 0;
@@ -258,7 +262,11 @@ const OpenCodeRuntimeTestDouble: OpenCodeRuntimeShape = {
258262
},
259263
children: async ({ sessionID }: { sessionID: string }) => {
260264
runtimeMock.state.sessionChildrenCalls.push(sessionID);
261-
return { data: runtimeMock.state.sessionChildrenById.get(sessionID) ?? [] };
265+
return {
266+
data: runtimeMock.state.sessionChildrenImplementation
267+
? await runtimeMock.state.sessionChildrenImplementation(sessionID)
268+
: (runtimeMock.state.sessionChildrenById.get(sessionID) ?? []),
269+
};
262270
},
263271
status: async () => {
264272
runtimeMock.state.sessionStatusCalls += 1;
@@ -3131,6 +3139,79 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
31313139
}),
31323140
);
31333141

3142+
it.effect("limits SDK requests across the full OpenCode child tree", () =>
3143+
Effect.gen(function* () {
3144+
const adapter = yield* OpenCodeAdapter;
3145+
const threadId = asThreadId("thread-interrupt-child-request-limit");
3146+
const rootSessionId = "http://127.0.0.1:9999/session";
3147+
const requestRelease = promiseWithResolvers<void>();
3148+
const limitReached = promiseWithResolvers<void>();
3149+
let inFlight = 0;
3150+
let maxInFlight = 0;
3151+
const holdRequest = async <T>(result: T): Promise<T> => {
3152+
inFlight += 1;
3153+
maxInFlight = Math.max(maxInFlight, inFlight);
3154+
if (inFlight === 8) {
3155+
limitReached.resolve(undefined);
3156+
}
3157+
await requestRelease.promise;
3158+
inFlight -= 1;
3159+
return result;
3160+
};
3161+
3162+
const children = Array.from({ length: 8 }, (_, index) => ({ id: `ses_child_${index}` }));
3163+
runtimeMock.state.sessionChildrenById.set(rootSessionId, children);
3164+
for (const child of children.slice(1)) {
3165+
runtimeMock.state.sessionChildrenById.set(
3166+
child.id,
3167+
Array.from({ length: 8 }, (_, index) => ({ id: `${child.id}_nested_${index}` })),
3168+
);
3169+
}
3170+
runtimeMock.state.abortImplementation = async (sessionID) => {
3171+
if (sessionID.includes("_nested_")) {
3172+
await holdRequest(undefined);
3173+
}
3174+
};
3175+
runtimeMock.state.sessionChildrenImplementation = async (sessionID) => {
3176+
if (sessionID === "ses_child_0") {
3177+
return await holdRequest([]);
3178+
}
3179+
return runtimeMock.state.sessionChildrenById.get(sessionID) ?? [];
3180+
};
3181+
3182+
yield* adapter.startSession({
3183+
provider: ProviderDriverKind.make("opencode"),
3184+
threadId,
3185+
runtimeMode: "full-access",
3186+
});
3187+
const turn = yield* adapter.sendTurn({
3188+
threadId,
3189+
input: "Run a nested child tree",
3190+
modelSelection: createModelSelection(
3191+
ProviderInstanceId.make("opencode"),
3192+
"opencode/kimi-k3",
3193+
),
3194+
});
3195+
3196+
const interruptFiber = yield* adapter
3197+
.interruptTurn(threadId, turn.turnId)
3198+
.pipe(Effect.forkChild);
3199+
yield* Effect.promise(() => limitReached.promise);
3200+
yield* Effect.yieldNow;
3201+
3202+
NodeAssert.equal(inFlight, 8);
3203+
NodeAssert.equal(maxInFlight, 8);
3204+
3205+
requestRelease.resolve(undefined);
3206+
yield* Fiber.join(interruptFiber);
3207+
3208+
runtimeMock.state.abortImplementation = null;
3209+
runtimeMock.state.sessionChildrenImplementation = null;
3210+
runtimeMock.state.sessionChildrenById.clear();
3211+
yield* adapter.stopSession(threadId);
3212+
}),
3213+
);
3214+
31343215
it.effect("attempts every child abort and fails the interrupt when one child abort fails", () =>
31353216
Effect.gen(function* () {
31363217
const adapter = yield* OpenCodeAdapter;

‎apps/server/src/provider/Layers/OpenCodeAdapter.ts‎

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ const abortOpenCodeDescendants = Effect.fn("abortOpenCodeDescendants")(function*
704704
context: OpenCodeSessionContext,
705705
) {
706706
const visited = new Set([context.openCodeSessionId]);
707+
const requestSemaphore = Semaphore.makeUnsafe(8);
707708

708709
const visit = (
709710
sessionId: string,
@@ -712,29 +713,37 @@ const abortOpenCodeDescendants = Effect.fn("abortOpenCodeDescendants")(function*
712713
Effect.gen(function* () {
713714
let firstFailure: OpenCodeRuntimeError | undefined;
714715
if (abortSession) {
715-
const abortResult = yield* runOpenCodeSdk("session.abort", (signal) =>
716-
context.client.session.abort({ sessionID: sessionId }, { signal }),
717-
).pipe(
716+
const abortResult = yield* requestSemaphore
717+
.withPermit(
718+
runOpenCodeSdk("session.abort", (signal) =>
719+
context.client.session.abort({ sessionID: sessionId }, { signal }),
720+
),
721+
)
722+
.pipe(
723+
Effect.catchIf(
724+
(cause) => isOpenCodeNotFound(cause),
725+
() => Effect.void,
726+
),
727+
Effect.result,
728+
);
729+
if (abortResult._tag === "Failure") {
730+
firstFailure = abortResult.failure;
731+
}
732+
}
733+
734+
const childrenResult = yield* requestSemaphore
735+
.withPermit(
736+
runOpenCodeSdk("session.children", (signal) =>
737+
context.client.session.children({ sessionID: sessionId }, { signal }),
738+
),
739+
)
740+
.pipe(
718741
Effect.catchIf(
719742
(cause) => isOpenCodeNotFound(cause),
720743
() => Effect.void,
721744
),
722745
Effect.result,
723746
);
724-
if (abortResult._tag === "Failure") {
725-
firstFailure = abortResult.failure;
726-
}
727-
}
728-
729-
const childrenResult = yield* runOpenCodeSdk("session.children", (signal) =>
730-
context.client.session.children({ sessionID: sessionId }, { signal }),
731-
).pipe(
732-
Effect.catchIf(
733-
(cause) => isOpenCodeNotFound(cause),
734-
() => Effect.void,
735-
),
736-
Effect.result,
737-
);
738747
if (childrenResult._tag === "Failure") {
739748
return firstFailure ?? childrenResult.failure;
740749
}

0 commit comments

Comments
 (0)