@@ -388,6 +388,12 @@ interface OpenCodeThreadState {
388388 parentSubagent : OpenCodeSubagentContext | null ;
389389 nextChildTurnOrdinal : number ;
390390 nextAdmissionGeneration : number ;
391+ /**
392+ * The permission rules the native session currently carries. OpenCode keeps
393+ * them on the session, so a runtime mode change only lands when they are
394+ * written back.
395+ */
396+ appliedPermissionRules : PermissionRuleset | null ;
391397}
392398
393399interface OpenCodeRequestOwner {
@@ -706,6 +712,17 @@ function permissionRuleEquals(
706712 ) ;
707713}
708714
715+ function permissionRulesEqual ( left : PermissionRuleset | null , right : PermissionRuleset ) : boolean {
716+ return (
717+ left !== null &&
718+ left . length === right . length &&
719+ left . every ( ( rule , index ) => {
720+ const other = right [ index ] ;
721+ return other !== undefined && permissionRuleEquals ( rule , other ) ;
722+ } )
723+ ) ;
724+ }
725+
709726/**
710727 * OpenCode task sessions inherit only the parent's deny/external-directory
711728 * rules and then add agent-specific restrictions such as disabling nested
@@ -1417,6 +1434,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
14171434 parentSubagent : context ,
14181435 nextChildTurnOrdinal : 1 ,
14191436 nextAdmissionGeneration : 1 ,
1437+ appliedPermissionRules : childPermission ,
14201438 } ) ;
14211439 yield * emitProviderEvent ( {
14221440 type : "app_thread.created" ,
@@ -2806,6 +2824,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
28062824 parentSubagent : subagentsByChildSessionId . get ( nativeSession . id ) ?? null ,
28072825 nextChildTurnOrdinal : 1 ,
28082826 nextAdmissionGeneration : 1 ,
2827+ appliedPermissionRules : null ,
28092828 } ;
28102829 threads . set ( nativeSession . id , state ) ;
28112830 return state ;
@@ -2991,6 +3010,26 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
29913010 ) ;
29923011 } ) ;
29933012
3013+ const applyPermissionRules = (
3014+ state : OpenCodeThreadState ,
3015+ runtimePolicy : ProviderAdapter . ProviderAdapterV2RuntimePolicy | undefined ,
3016+ ) =>
3017+ Effect . gen ( function * ( ) {
3018+ // A native subagent session carries the parent's rules plus the
3019+ // restrictions of its own agent, so the parent policy must not
3020+ // overwrite them.
3021+ if ( runtimePolicy === undefined || state . parentSubagent !== null ) return ;
3022+ const rules = openCodePermissionRules ( runtimePolicy ) ;
3023+ if ( permissionRulesEqual ( state . appliedPermissionRules , rules ) ) return ;
3024+ yield * sdkCall ( "session.update" , { sessionID : state . nativeSessionId } , ( ) =>
3025+ client . session . update ( {
3026+ sessionID : state . nativeSessionId ,
3027+ permission : rules ,
3028+ } ) ,
3029+ ) ;
3030+ state . appliedPermissionRules = rules ;
3031+ } ) ;
3032+
29943033 const runtimeSession : ProviderAdapter . ProviderAdapterV2SessionRuntime = {
29953034 instanceId : options . instanceId ,
29963035 driver : OPENCODE_PROVIDER ,
@@ -3003,16 +3042,20 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
30033042 if ( threadInput . existingProviderThread ?. nativeThreadRef != null ) {
30043043 return yield * runtimeSession . resumeThread ( {
30053044 providerThread : threadInput . existingProviderThread ,
3045+ threadId : threadInput . threadId ,
3046+ modelSelection : threadInput . modelSelection ,
3047+ runtimePolicy : threadInput . runtimePolicy ,
30063048 } ) ;
30073049 }
30083050 // No title: OpenCode generates one from the first prompt only when
30093051 // session.create leaves it unset (SessionPrompt.ensureTitle).
3052+ const permissionRules = openCodePermissionRules ( threadInput . runtimePolicy ) ;
30103053 const response = yield * sdkCall (
30113054 "session.create" ,
3012- { permission : openCodePermissionRules ( threadInput . runtimePolicy ) } ,
3055+ { permission : permissionRules } ,
30133056 ( ) =>
30143057 client . session . create ( {
3015- permission : openCodePermissionRules ( threadInput . runtimePolicy ) ,
3058+ permission : permissionRules ,
30163059 } ) ,
30173060 ) ;
30183061 const nativeSession = unwrapData ( "session.create" , response ) ;
@@ -3037,7 +3080,8 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
30373080 status : created . status ,
30383081 updatedAt : created . updatedAt ,
30393082 } ;
3040- registerThread ( nativeSession , providerThread , null ) ;
3083+ const state = registerThread ( nativeSession , providerThread , null ) ;
3084+ state . appliedPermissionRules = permissionRules ;
30413085 return providerThread ;
30423086 } ) . pipe (
30433087 Effect . mapError (
@@ -3063,7 +3107,8 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
30633107 status : "idle" as const ,
30643108 updatedAt : dateTimeFromEpoch ( nativeSession . time . updated , resumedAt ) ,
30653109 } ;
3066- registerThread ( nativeSession , providerThread , null ) ;
3110+ const state = registerThread ( nativeSession , providerThread , null ) ;
3111+ yield * applyPermissionRules ( state , threadInput . runtimePolicy ) ;
30673112 return providerThread ;
30683113 } ) . pipe (
30693114 Effect . mapError (
0 commit comments