GeezerVison is showing github.com — fix(opencode): apply runtime mode changes to existing OpenCode sessio… · pingdotgg/t3code@b5af460
File treeExpand file treeCollapse file tree Open diff view settings packages/provider-opencode/src/server
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/provider-opencode/src/server/adapter.test.ts +58-1Lines changed: 58 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -184,6 +184,7 @@ const makeOpenCodeRuntimeHarness = Effect.fn("makeOpenCodeRuntimeHarness")(funct | 184 | 184 | const startTurn = ( | 185 | 185 | text = "hello", | 186 | 186 | startProviderThread: OrchestrationV2ProviderThread = providerThread, | | 187 | + turnPolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy = policy, | 187 | 188 | ) => | 188 | 189 | runtime.startTurn({ | 189 | 190 | appThread: { | @@ -224,7 +225,7 @@ const makeOpenCodeRuntimeHarness = Effect.fn("makeOpenCodeRuntimeHarness")(funct | 224 | 225 | attachments: [], | 225 | 226 | }, | 226 | 227 | modelSelection, | 227 | | - runtimePolicy: policy, | | 228 | + runtimePolicy: turnPolicy, | 228 | 229 | }); | 229 | 230 | return { | 230 | 231 | nativeSessionId, | @@ -239,6 +240,62 @@ const makeOpenCodeRuntimeHarness = Effect.fn("makeOpenCodeRuntimeHarness")(funct | 239 | 240 | }); | 240 | 241 |
| 241 | 242 | describe("OpenCodeAdapterV2", () => { | | 243 | + it.effect("applies a changed runtime policy to the session on resume and turn start", () => | | 244 | + Effect.gen(function* () { | | 245 | + const updates: Array<{ sessionID: string; permission: unknown }> = []; | | 246 | + const fullAccess = runtimePolicy("full-access", { cwd: "/workspace" }); | | 247 | + const supervised = runtimePolicy("approval-required", { cwd: "/workspace" }); | | 248 | + const harness = yield* makeOpenCodeRuntimeHarness("policy-change", "root", { | | 249 | + event: { subscribe: async () => ({ stream: asyncEventStream().stream }) }, | | 250 | + session: { | | 251 | + create: async (input: { permission: unknown }) => ({ | | 252 | + data: { | | 253 | + id: "root", | | 254 | + permission: input.permission, | | 255 | + time: { created: 1, updated: 1 }, | | 256 | + }, | | 257 | + }), | | 258 | + get: async () => ({ | | 259 | + data: { | | 260 | + id: "root", | | 261 | + permission: openCodePermissionRules(fullAccess), | | 262 | + time: { created: 1, updated: 1 }, | | 263 | + }, | | 264 | + }), | | 265 | + update: async (input: { sessionID: string; permission: unknown }) => { | | 266 | + updates.push(input); | | 267 | + return { data: { id: input.sessionID, time: { created: 1, updated: 2 } } }; | | 268 | + }, | | 269 | + promptAsync: async () => ({ data: true }), | | 270 | + abort: async () => ({ data: true }), | | 271 | + children: async () => ({ data: [] }), | | 272 | + }, | | 273 | + }); | | 274 | + | | 275 | + yield* harness.runtime.resumeThread({ | | 276 | + providerThread: harness.providerThread, | | 277 | + runtimePolicy: fullAccess, | | 278 | + }); | | 279 | + assert.deepStrictEqual(updates, []); | | 280 | + | | 281 | + yield* harness.runtime.resumeThread({ | | 282 | + providerThread: harness.providerThread, | | 283 | + runtimePolicy: supervised, | | 284 | + }); | | 285 | + assert.deepStrictEqual(updates, [ | | 286 | + { sessionID: "root", permission: openCodePermissionRules(supervised) }, | | 287 | + ]); | | 288 | + | | 289 | + yield* harness.startTurn("hello", harness.providerThread, fullAccess); | | 290 | + assert.deepStrictEqual(updates.slice(1), [ | | 291 | + { sessionID: "root", permission: openCodePermissionRules(fullAccess) }, | | 292 | + ]); | | 293 | + }).pipe( | | 294 | + Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer)), | | 295 | + Effect.scoped, | | 296 | + ), | | 297 | + ); | | 298 | + | 242 | 299 | it.effect.each(["completed", "failed", "unavailable"] as const)( | 243 | 300 | "normalizes OpenCode step usage for %s turns", | 244 | 301 | (ending) => | |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-opencode/src/server/adapter.ts +31-9Lines changed: 31 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -379,6 +379,7 @@ interface OpenCodeThreadState { | 379 | 379 | parentSubagent: OpenCodeSubagentContext | null; | 380 | 380 | nextChildTurnOrdinal: number; | 381 | 381 | nextAdmissionGeneration: number; | | 382 | + permission: PermissionRuleset; | 382 | 383 | } | 383 | 384 |
| 384 | 385 | interface OpenCodeRequestOwner { | @@ -1395,6 +1396,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 1395 | 1396 | parentSubagent: context, | 1396 | 1397 | nextChildTurnOrdinal: 1, | 1397 | 1398 | nextAdmissionGeneration: 1, | | 1399 | + permission: childPermission, | 1398 | 1400 | }); | 1399 | 1401 | yield* emitProviderEvent({ | 1400 | 1402 | type: "app_thread.created", | @@ -2775,6 +2777,8 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 2775 | 2777 | if (existing !== undefined) { | 2776 | 2778 | existing.providerThread = providerThread; | 2777 | 2779 | if (appThread !== null) existing.appThread = appThread; | | 2780 | + if (nativeSession.permission !== undefined) | | 2781 | + existing.permission = nativeSession.permission; | 2778 | 2782 | return existing; | 2779 | 2783 | } | 2780 | 2784 | const state: OpenCodeThreadState = { | @@ -2789,11 +2793,29 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 2789 | 2793 | parentSubagent: subagentsByChildSessionId.get(nativeSession.id) ?? null, | 2790 | 2794 | nextChildTurnOrdinal: 1, | 2791 | 2795 | nextAdmissionGeneration: 1, | | 2796 | + permission: nativeSession.permission ?? [], | 2792 | 2797 | }; | 2793 | 2798 | threads.set(nativeSession.id, state); | 2794 | 2799 | return state; | 2795 | 2800 | }; | 2796 | 2801 |
| | 2802 | + const applyPermissionRules = Effect.fnUntraced(function* ( | | 2803 | + state: OpenCodeThreadState, | | 2804 | + policy: ProviderAdapter.ProviderAdapterV2RuntimePolicy, | | 2805 | + ) { | | 2806 | + const permission = openCodePermissionRules(policy); | | 2807 | + if ( | | 2808 | + permission.length === state.permission.length && | | 2809 | + permission.every((rule, index) => permissionRuleEquals(rule, state.permission[index]!)) | | 2810 | + ) { | | 2811 | + return; | | 2812 | + } | | 2813 | + yield* sdkCall("session.update", { sessionID: state.nativeSessionId, permission }, () => | | 2814 | + client.session.update({ sessionID: state.nativeSessionId, permission }), | | 2815 | + ); | | 2816 | + state.permission = permission; | | 2817 | + }); | | 2818 | + | 2797 | 2819 | const resolvePromptParts = Effect.fnUntraced(function* ( | 2798 | 2820 | turnInput: ProviderAdapter.ProviderAdapterV2TurnInput, | 2799 | 2821 | ) { | @@ -2988,14 +3010,10 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 2988 | 3010 | providerThread: threadInput.existingProviderThread, | 2989 | 3011 | }); | 2990 | 3012 | } | | 3013 | + const permission = openCodePermissionRules(threadInput.runtimePolicy); | 2991 | 3014 | // OpenCode generates a title only when session.create leaves it unset. | 2992 | | - const response = yield* sdkCall( | 2993 | | - "session.create", | 2994 | | - { permission: openCodePermissionRules(threadInput.runtimePolicy) }, | 2995 | | - () => | 2996 | | - client.session.create({ | 2997 | | - permission: openCodePermissionRules(threadInput.runtimePolicy), | 2998 | | - }), | | 3015 | + const response = yield* sdkCall("session.create", { permission }, () => | | 3016 | + client.session.create({ permission }), | 2999 | 3017 | ); | 3000 | 3018 | const nativeSession = unwrapData("session.create", response); | 3001 | 3019 | const createdAt = yield* DateTime.now; | @@ -3019,7 +3037,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 3019 | 3037 | status: created.status, | 3020 | 3038 | updatedAt: created.updatedAt, | 3021 | 3039 | }; | 3022 | | - registerThread(nativeSession, providerThread, null); | | 3040 | + registerThread(nativeSession, providerThread, null).permission = permission; | 3023 | 3041 | return providerThread; | 3024 | 3042 | }, | 3025 | 3043 | (effect, threadInput) => | @@ -3048,7 +3066,10 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 3048 | 3066 | status: "idle" as const, | 3049 | 3067 | updatedAt: dateTimeFromEpoch(nativeSession.time.updated, resumedAt), | 3050 | 3068 | }; | 3051 | | - registerThread(nativeSession, providerThread, null); | | 3069 | + const state = registerThread(nativeSession, providerThread, null); | | 3070 | + if (threadInput.runtimePolicy !== undefined) { | | 3071 | + yield* applyPermissionRules(state, threadInput.runtimePolicy); | | 3072 | + } | 3052 | 3073 | return providerThread; | 3053 | 3074 | }, | 3054 | 3075 | (effect, threadInput) => | @@ -3096,6 +3117,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function | 3096 | 3117 | `OpenCode model '${turnInput.modelSelection.model}' must use provider/model format`, | 3097 | 3118 | ); | 3098 | 3119 | } | | 3120 | + yield* applyPermissionRules(state, turnInput.runtimePolicy); | 3099 | 3121 | const isCompaction = | 3100 | 3122 | turnInput.message.text.trim() === "/compact" && | 3101 | 3123 | turnInput.message.attachments.length === 0; | |
|