Skip to content

Commit de53726

Browse files
committed
fix(opencode): preserve child denies and current resume selection
1 parent d1b45c3 commit de53726

2 files changed

Lines changed: 46 additions & 27 deletions

File tree

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

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -402,32 +402,39 @@ describe("OpenCodeAdapterV2", () => {
402402
[child, [taskDeny]],
403403
]);
404404
let failNextChildUpdate = false;
405-
const harness = yield* makeOpenCodeRuntimeHarness("child-policy-change", root, {
406-
event: { subscribe: async () => ({ stream: nativeEvents.stream }) },
407-
session: {
408-
create: async () => ({ data: { id: root, time: { created: 1, updated: 1 } } }),
409-
get: async ({ sessionID }: { sessionID: string }) => ({
410-
data: {
411-
id: sessionID,
412-
...(sessionID === child ? { parentID: root } : {}),
413-
permission: nativePermissions.get(sessionID),
414-
time: { created: 1, updated: 1 },
405+
const harness = yield* makeOpenCodeRuntimeHarness(
406+
"child-policy-change",
407+
root,
408+
{
409+
event: { subscribe: async () => ({ stream: nativeEvents.stream }) },
410+
app: { agents: async () => ({ data: [{ name: "review", permission: [taskDeny] }] }) },
411+
session: {
412+
create: async () => ({ data: { id: root, time: { created: 1, updated: 1 } } }),
413+
get: async ({ sessionID }: { sessionID: string }) => ({
414+
data: {
415+
id: sessionID,
416+
...(sessionID === child ? { parentID: root } : {}),
417+
permission: nativePermissions.get(sessionID),
418+
time: { created: 1, updated: 1 },
419+
},
420+
}),
421+
update: async (input: (typeof updates)[number]) => {
422+
if (input.sessionID === child && failNextChildUpdate) {
423+
failNextChildUpdate = false;
424+
throw new Error("child update failed");
425+
}
426+
updates.push(input);
427+
nativePermissions.set(input.sessionID, input.permission);
428+
return { data: { id: input.sessionID, time: { created: 1, updated: 2 } } };
415429
},
416-
}),
417-
update: async (input: (typeof updates)[number]) => {
418-
if (input.sessionID === child && failNextChildUpdate) {
419-
failNextChildUpdate = false;
420-
throw new Error("child update failed");
421-
}
422-
updates.push(input);
423-
nativePermissions.set(input.sessionID, input.permission);
424-
return { data: { id: input.sessionID, time: { created: 1, updated: 2 } } };
430+
promptAsync: async () => ({ data: true }),
431+
abort: async () => ({ data: true }),
432+
children: async () => ({ data: [] }),
425433
},
426-
promptAsync: async () => ({ data: true }),
427-
abort: async () => ({ data: true }),
428-
children: async () => ({ data: [] }),
429434
},
430-
});
435+
{},
436+
[{ id: "agent", value: "review" }],
437+
);
431438

432439
yield* harness.startTurn("hello", harness.providerThread, fullAccess);
433440
yield* push({
@@ -2737,6 +2744,12 @@ describe("OpenCodeAdapterV2", () => {
27372744
);
27382745
assert(Exit.isFailure(result));
27392746
assert.equal(prompts.length, beforeMissing);
2747+
yield* missing.runtime.resumeThread({
2748+
providerThread: missing.providerThread,
2749+
runtimePolicy: harness.policy,
2750+
modelSelection: { ...harness.modelSelection, options: [] },
2751+
});
2752+
assert.equal(permissionAction(sessions.get("native-review")!.permission!, "bash"), "allow");
27402753
}).pipe(
27412754
Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer)),
27422755
Effect.scoped,

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,6 +2822,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
28222822
modelSelection: ModelSelection,
28232823
) {
28242824
const parentRules = openCodePermissionRules(policy);
2825+
let rootRules = parentRules;
28252826
const agent =
28262827
getModelSelectionStringOptionValue(modelSelection, "agent") ??
28272828
(policy.interactionMode === "plan" ? "plan" : undefined);
@@ -2841,18 +2842,19 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
28412842
return yield* protocolError(`OpenCode agent '${agent}' is not available`);
28422843
}
28432844
// OpenCode takes the last matching rule; full access must not shadow agent denies.
2844-
parentRules.push(
2845+
rootRules = [
2846+
...parentRules,
28452847
...selected.permission.map((rule) =>
28462848
rule.action === "ask" ? { ...rule, action: "allow" as const } : rule,
28472849
),
2848-
);
2850+
];
28492851
}
28502852
const pending = [state];
28512853
for (let target = pending.pop(); target !== undefined; target = pending.pop()) {
28522854
const previous = target.permission;
28532855
const permission =
28542856
target.parentSubagent === null
2855-
? parentRules
2857+
? rootRules
28562858
: openCodeChildPermissionRules(policy, previous, target.inheritedPermission);
28572859
if (
28582860
permission.length > previous.length ||
@@ -3125,7 +3127,11 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
31253127
};
31263128
const state = registerThread(nativeSession, providerThread, null);
31273129
if (threadInput.runtimePolicy !== undefined) {
3128-
yield* applyPermissionRules(state, threadInput.runtimePolicy, input.modelSelection);
3130+
yield* applyPermissionRules(
3131+
state,
3132+
threadInput.runtimePolicy,
3133+
threadInput.modelSelection ?? input.modelSelection,
3134+
);
31293135
}
31303136
return providerThread;
31313137
},

0 commit comments

Comments
 (0)