@@ -12,6 +12,9 @@ import { Truncate } from "@/tool/truncate"
1212import { Tool } from "@/tool/tool"
1313import { Agent } from "../../src/agent/agent"
1414import { SessionID , MessageID } from "../../src/session/schema"
15+ import { PermissionV1 } from "@opencode-ai/core/v1/permission"
16+ import { Permission } from "../../src/permission"
17+ import { InstanceRef } from "../../src/effect/instance-ref"
1518import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
1619import { disposeAllInstances , TestInstance } from "../fixture/fixture"
1720import { testEffect } from "../lib/effect"
@@ -58,6 +61,123 @@ const run = Effect.fn("WriteToolTest.run")(function* (
5861 return yield * tool . execute ( args , next )
5962} )
6063
64+ const askWithRules = ( ruleset : PermissionV1 . Ruleset ) => ( {
65+ ...ctx ,
66+ ask : ( input : Parameters < Tool . Context [ "ask" ] > [ 0 ] ) =>
67+ Effect . sync ( ( ) => {
68+ if ( input . permission === "external_directory" ) return
69+ for ( const pattern of input . patterns ) {
70+ const rule = Permission . evaluate ( input . permission , pattern , ruleset )
71+ if ( rule . action !== "allow" ) throw new Error ( `permission ${ rule . action } : ${ pattern } ` )
72+ }
73+ } ) ,
74+ } )
75+
76+ describe ( "tool.write permission paths" , ( ) => {
77+ it . instance (
78+ "matches an inside-worktree relative rule" ,
79+ ( ) =>
80+ Effect . gen ( function * ( ) {
81+ const test = yield * TestInstance
82+ const filepath = path . join ( test . directory , "src" , "inside.txt" )
83+ yield * run (
84+ { filePath : filepath , content : "inside" } ,
85+ askWithRules ( Permission . fromConfig ( { edit : { "src/**" : "allow" } } ) ) ,
86+ )
87+ expect ( yield * Effect . promise ( ( ) => fs . readFile ( filepath , "utf-8" ) ) ) . toBe ( "inside" )
88+ } ) ,
89+ { git : true } ,
90+ )
91+
92+ it . instance (
93+ "allows an outside-worktree absolute rule" ,
94+ ( ) =>
95+ Effect . gen ( function * ( ) {
96+ const test = yield * TestInstance
97+ const outer = yield * Effect . promise ( ( ) =>
98+ fs . mkdtemp ( path . join ( path . dirname ( test . directory ) , "opencode-outside-" ) ) ,
99+ )
100+ const filepath = path . join ( outer , "allowed.txt" )
101+ yield * run (
102+ { filePath : filepath , content : "allowed" } ,
103+ askWithRules ( Permission . fromConfig ( { edit : { [ `${ outer } /**` ] : "allow" } } ) ) ,
104+ )
105+ expect ( yield * Effect . promise ( ( ) => fs . readFile ( filepath , "utf-8" ) ) ) . toBe ( "allowed" )
106+ yield * Effect . promise ( ( ) => fs . rm ( outer , { recursive : true , force : true } ) )
107+ } ) ,
108+ { git : true } ,
109+ )
110+
111+ it . instance (
112+ "enforces an outside-worktree absolute deny over a wildcard allow" ,
113+ ( ) =>
114+ Effect . gen ( function * ( ) {
115+ const test = yield * TestInstance
116+ const outer = yield * Effect . promise ( ( ) =>
117+ fs . mkdtemp ( path . join ( path . dirname ( test . directory ) , "opencode-outside-" ) ) ,
118+ )
119+ const filepath = path . join ( outer , "denied.txt" )
120+ const rules = Permission . fromConfig ( { edit : { "*" : "allow" , [ `${ outer } /**` ] : "deny" } } )
121+ const exit = yield * run ( { filePath : filepath , content : "denied" } , askWithRules ( rules ) ) . pipe ( Effect . exit )
122+ expect ( exit . _tag ) . toBe ( "Failure" )
123+ expect ( yield * Effect . promise ( ( ) => fs . stat ( filepath ) . catch ( ( ) => undefined ) ) ) . toBeUndefined ( )
124+ yield * Effect . promise ( ( ) => fs . rm ( outer , { recursive : true , force : true } ) )
125+ } ) ,
126+ { git : true } ,
127+ )
128+
129+ it . instance (
130+ "enforces an absolute deny rule in a project with no VCS" ,
131+ ( ) =>
132+ Effect . gen ( function * ( ) {
133+ const test = yield * TestInstance
134+ const instance = yield * InstanceRef
135+ expect ( instance ?. worktree ) . toBe ( "/" )
136+ const filepath = path . join ( test . directory , "denied.txt" )
137+ const rules = Permission . fromConfig ( { edit : { "*" : "allow" , [ `${ test . directory } /**` ] : "deny" } } )
138+ const exit = yield * run ( { filePath : filepath , content : "denied" } , askWithRules ( rules ) ) . pipe ( Effect . exit )
139+ expect ( exit . _tag ) . toBe ( "Failure" )
140+ expect ( yield * Effect . promise ( ( ) => fs . stat ( filepath ) . catch ( ( ) => undefined ) ) ) . toBeUndefined ( )
141+ } ) ,
142+ { git : false } ,
143+ )
144+
145+ it . instance (
146+ "allows an absolute rule in a project with no VCS" ,
147+ ( ) =>
148+ Effect . gen ( function * ( ) {
149+ const test = yield * TestInstance
150+ const instance = yield * InstanceRef
151+ expect ( instance ?. worktree ) . toBe ( "/" )
152+ const filepath = path . join ( test . directory , "allowed.txt" )
153+ yield * run (
154+ { filePath : filepath , content : "allowed" } ,
155+ askWithRules ( Permission . fromConfig ( { edit : { [ `${ test . directory } /**` ] : "allow" } } ) ) ,
156+ )
157+ expect ( yield * Effect . promise ( ( ) => fs . readFile ( filepath , "utf-8" ) ) ) . toBe ( "allowed" )
158+ } ) ,
159+ { git : false } ,
160+ )
161+
162+ it . instance (
163+ "does not match an outside-worktree relative rule" ,
164+ ( ) =>
165+ Effect . gen ( function * ( ) {
166+ const test = yield * TestInstance
167+ const outer = yield * Effect . promise ( ( ) =>
168+ fs . mkdtemp ( path . join ( path . dirname ( test . directory ) , "opencode-outside-" ) ) ,
169+ )
170+ const filepath = path . join ( outer , "not-src.txt" )
171+ const rules = Permission . fromConfig ( { edit : { "src/**" : "allow" } } )
172+ const exit = yield * run ( { filePath : filepath , content : "not allowed" } , askWithRules ( rules ) ) . pipe ( Effect . exit )
173+ expect ( exit . _tag ) . toBe ( "Failure" )
174+ expect ( yield * Effect . promise ( ( ) => fs . stat ( filepath ) . catch ( ( ) => undefined ) ) ) . toBeUndefined ( )
175+ yield * Effect . promise ( ( ) => fs . rm ( outer , { recursive : true , force : true } ) )
176+ } ) ,
177+ { git : true } ,
178+ )
179+ } )
180+
61181describe ( "tool.write" , ( ) => {
62182 describe ( "new file creation" , ( ) => {
63183 it . instance ( "writes content to new file" , ( ) =>
0 commit comments