Skip to content

Commit 4582d01

Browse files
Fluffy-Bunny-23opencode/minimax-m3.1-flash-free
andcommitted
fix(opencode): keep a native child session's own rules on resume
CodeRabbit review of the previous commit: the in-memory subagent marker is gone after a server restart, so a resumed native child session looked like a root session and received the root runtime-policy rules, replacing the child agent's own restrictions. The native session's parent id is the durable signal, so a resumed session with a parent keeps the rules it carries. Co-Authored-By: opencode/minimax-m3.1-flash-free <[email protected]>
1 parent 43de869 commit 4582d01

2 files changed

Lines changed: 109 additions & 56 deletions

File tree

‎packages/provider-opencode/src/server/adapter.test.ts‎

Lines changed: 98 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,69 @@ const makeOpenCodeRuntimeHarness = Effect.fn("makeOpenCodeRuntimeHarness")(funct
240240
};
241241
});
242242

243+
const makePermissionRuleHarness = Effect.fn("makePermissionRuleHarness")(function* (
244+
suffix: string,
245+
nativeSession: { readonly id: string; readonly parentID?: string },
246+
) {
247+
const nativeEvents = asyncEventStream();
248+
const createdPermissions: Array<unknown> = [];
249+
const updatedPermissions: Array<unknown> = [];
250+
const instanceId = ProviderInstanceId.make(`opencode-${suffix}`);
251+
const threadId = ThreadId.make(`thread-${suffix}`);
252+
const modelSelection = {
253+
instanceId,
254+
model: "anthropic/claude-sonnet",
255+
options: [],
256+
};
257+
const nativeSessionData = { ...nativeSession, time: { created: 1, updated: 1 } };
258+
const adapter = yield* makeOpenCodeAdapterV2({
259+
instanceId,
260+
settings: OPEN_CODE_TEST_SETTINGS,
261+
environment: {},
262+
}).pipe(
263+
Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, {
264+
connectToOpenCodeServer: () => Effect.succeed({ url: "http://test.invalid", external: true }),
265+
createOpenCodeSdkClient: () => ({
266+
event: { subscribe: async () => ({ stream: nativeEvents.stream }) },
267+
session: {
268+
create: async (input: { permission?: unknown }) => {
269+
createdPermissions.push(input.permission);
270+
return { data: nativeSessionData };
271+
},
272+
update: async (input: { permission?: unknown }) => {
273+
updatedPermissions.push(input.permission);
274+
return { data: true };
275+
},
276+
get: async () => ({ data: nativeSessionData }),
277+
},
278+
}),
279+
} as unknown as OpenCodeRuntime.OpenCodeRuntime["Service"]),
280+
Effect.provideService(ProviderHost.ProviderHost, {
281+
paths: { cwd: "/workspace" },
282+
resolveAttachmentPath: () => null,
283+
} as unknown as ProviderHost.ProviderHost["Service"]),
284+
);
285+
const runtime = yield* adapter.openSession({
286+
threadId,
287+
providerSessionId: ProviderSessionId.make(`session-${suffix}`),
288+
modelSelection,
289+
runtimePolicy: runtimePolicy("approval-required", { cwd: "/workspace" }),
290+
});
291+
const providerThread = yield* runtime.ensureThread({
292+
threadId,
293+
modelSelection,
294+
runtimePolicy: runtimePolicy("approval-required", { cwd: "/workspace" }),
295+
});
296+
return {
297+
createdPermissions,
298+
updatedPermissions,
299+
modelSelection,
300+
providerThread,
301+
runtime,
302+
threadId,
303+
};
304+
});
305+
243306
describe("OpenCodeAdapterV2", () => {
244307
it.effect.each(["completed", "failed", "unresolved", "unavailable", "reconnect"] as const)(
245308
"normalizes OpenCode step usage for %s turns",
@@ -2758,66 +2821,48 @@ describe("OpenCodeAdapterV2", () => {
27582821

27592822
it.effect("reapplies the runtime mode permission rules when a session is resumed", () =>
27602823
Effect.gen(function* () {
2761-
const nativeEvents = asyncEventStream();
2762-
const createdPermissions: Array<unknown> = [];
2763-
const updatedPermissions: Array<unknown> = [];
2764-
const instanceId = ProviderInstanceId.make("opencode-resume-policy");
2765-
const threadId = ThreadId.make("thread-opencode-resume-policy");
2766-
const modelSelection = {
2767-
instanceId,
2768-
model: "anthropic/claude-sonnet",
2769-
options: [],
2770-
};
2771-
const adapter = yield* makeOpenCodeAdapterV2({
2772-
instanceId,
2773-
settings: OPEN_CODE_TEST_SETTINGS,
2774-
environment: {},
2775-
}).pipe(
2776-
Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, {
2777-
connectToOpenCodeServer: () =>
2778-
Effect.succeed({ url: "http://test.invalid", external: true }),
2779-
createOpenCodeSdkClient: () => ({
2780-
event: { subscribe: async () => ({ stream: nativeEvents.stream }) },
2781-
session: {
2782-
create: async (input: { permission?: unknown }) => {
2783-
createdPermissions.push(input.permission);
2784-
return { data: { id: "root", time: { created: 1, updated: 1 } } };
2785-
},
2786-
update: async (input: { permission?: unknown }) => {
2787-
updatedPermissions.push(input.permission);
2788-
return { data: true };
2789-
},
2790-
get: async () => ({ data: { id: "root", time: { created: 1, updated: 1 } } }),
2791-
},
2792-
}),
2793-
} as unknown as OpenCodeRuntime.OpenCodeRuntime["Service"]),
2794-
Effect.provideService(ProviderHost.ProviderHost, {
2795-
paths: { cwd: "/workspace" },
2796-
resolveAttachmentPath: () => null,
2797-
} as unknown as ProviderHost.ProviderHost["Service"]),
2824+
const harness = yield* makePermissionRuleHarness("resume-policy", { id: "root" });
2825+
assert.equal(
2826+
permissionAction(harness.createdPermissions[0] as PermissionRuleset, "bash"),
2827+
"ask",
27982828
);
2799-
const runtime = yield* adapter.openSession({
2800-
threadId,
2801-
providerSessionId: ProviderSessionId.make("session-opencode-resume-policy"),
2802-
modelSelection,
2803-
runtimePolicy: runtimePolicy("approval-required", { cwd: "/workspace" }),
2829+
2830+
yield* harness.runtime.resumeThread({
2831+
providerThread: harness.providerThread,
2832+
threadId: harness.threadId,
2833+
modelSelection: harness.modelSelection,
2834+
runtimePolicy: runtimePolicy("full-access", { cwd: "/workspace" }),
28042835
});
2805-
const providerThread = yield* runtime.ensureThread({
2806-
threadId,
2807-
modelSelection,
2808-
runtimePolicy: runtimePolicy("approval-required", { cwd: "/workspace" }),
2836+
2837+
assert.equal(harness.updatedPermissions.length, 1);
2838+
assert.equal(
2839+
permissionAction(harness.updatedPermissions[0] as PermissionRuleset, "bash"),
2840+
"allow",
2841+
);
2842+
}).pipe(
2843+
Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer)),
2844+
Effect.scoped,
2845+
),
2846+
);
2847+
2848+
it.effect("keeps the rules of a resumed native child session", () =>
2849+
Effect.gen(function* () {
2850+
// A server restart drops the in-memory subagent marker, so the native
2851+
// parent id is the only signal that this session carries the child
2852+
// agent's own restrictions.
2853+
const harness = yield* makePermissionRuleHarness("resume-child-policy", {
2854+
id: "child",
2855+
parentID: "root",
28092856
});
2810-
assert.equal(permissionAction(createdPermissions[0] as PermissionRuleset, "bash"), "ask");
28112857

2812-
yield* runtime.resumeThread({
2813-
providerThread,
2814-
threadId,
2815-
modelSelection,
2858+
yield* harness.runtime.resumeThread({
2859+
providerThread: harness.providerThread,
2860+
threadId: harness.threadId,
2861+
modelSelection: harness.modelSelection,
28162862
runtimePolicy: runtimePolicy("full-access", { cwd: "/workspace" }),
28172863
});
28182864

2819-
assert.equal(updatedPermissions.length, 1);
2820-
assert.equal(permissionAction(updatedPermissions[0] as PermissionRuleset, "bash"), "allow");
2865+
assert.deepEqual(harness.updatedPermissions, []);
28212866
}).pipe(
28222867
Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer)),
28232868
Effect.scoped,

‎packages/provider-opencode/src/server/adapter.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3013,12 +3013,20 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
30133013
const applyPermissionRules = (
30143014
state: OpenCodeThreadState,
30153015
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy | undefined,
3016+
nativeParentId: string | undefined,
30163017
) =>
30173018
Effect.gen(function* () {
30183019
// A native subagent session carries the parent's rules plus the
30193020
// restrictions of its own agent, so the parent policy must not
3020-
// overwrite them.
3021-
if (runtimePolicy === undefined || state.parentSubagent !== null) return;
3021+
// overwrite them. The in-memory marker is gone after a server
3022+
// restart, so the native parent id is the durable signal.
3023+
if (
3024+
runtimePolicy === undefined ||
3025+
state.parentSubagent !== null ||
3026+
nativeParentId !== undefined
3027+
) {
3028+
return;
3029+
}
30223030
const rules = openCodePermissionRules(runtimePolicy);
30233031
if (permissionRulesEqual(state.appliedPermissionRules, rules)) return;
30243032
yield* sdkCall("session.update", { sessionID: state.nativeSessionId }, () =>
@@ -3108,7 +3116,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
31083116
updatedAt: dateTimeFromEpoch(nativeSession.time.updated, resumedAt),
31093117
};
31103118
const state = registerThread(nativeSession, providerThread, null);
3111-
yield* applyPermissionRules(state, threadInput.runtimePolicy);
3119+
yield* applyPermissionRules(state, threadInput.runtimePolicy, nativeSession.parentID);
31123120
return providerThread;
31133121
}).pipe(
31143122
Effect.mapError(

0 commit comments

Comments
 (0)