@@ -3,7 +3,7 @@ import { Effect, Schema } from "effect"
33import * as Tool from "./tool"
44import { Bus } from "../bus"
55import { FileWatcher } from "../file/watcher"
6- import { Instance } from "../project /instance"
6+ import { InstanceState } from "@/effect /instance-state "
77import { Patch } from "../patch"
88import { createTwoFilesPatch , diffLines } from "diff"
99import { assertExternalDirectoryEffect } from "./external-directory"
@@ -52,6 +52,8 @@ export const ApplyPatchTool = Tool.define(
5252 return yield * Effect . fail ( new Error ( "apply_patch verification failed: no hunks found" ) )
5353 }
5454
55+ const instance = yield * InstanceState . context
56+
5557 // Validate file paths and check permissions
5658 const fileChanges : Array < {
5759 filePath : string
@@ -68,7 +70,7 @@ export const ApplyPatchTool = Tool.define(
6870 let totalDiff = ""
6971
7072 for ( const hunk of hunks ) {
71- const filePath = path . resolve ( Instance . directory , hunk . path )
73+ const filePath = path . resolve ( instance . directory , hunk . path )
7274 yield * assertExternalDirectoryEffect ( ctx , filePath )
7375
7476 switch ( hunk . type ) {
@@ -133,7 +135,7 @@ export const ApplyPatchTool = Tool.define(
133135 if ( change . removed ) deletions += change . count || 0
134136 }
135137
136- const movePath = hunk . move_path ? path . resolve ( Instance . directory , hunk . move_path ) : undefined
138+ const movePath = hunk . move_path ? path . resolve ( instance . directory , hunk . move_path ) : undefined
137139 yield * assertExternalDirectoryEffect ( ctx , movePath )
138140
139141 fileChanges . push ( {
@@ -187,7 +189,7 @@ export const ApplyPatchTool = Tool.define(
187189 // Build per-file metadata for UI rendering (used for both permission and result)
188190 const files = fileChanges . map ( ( change ) => ( {
189191 filePath : change . filePath ,
190- relativePath : path . relative ( Instance . worktree , change . movePath ?? change . filePath ) . replaceAll ( "\\" , "/" ) ,
192+ relativePath : path . relative ( instance . worktree , change . movePath ?? change . filePath ) . replaceAll ( "\\" , "/" ) ,
191193 type : change . type ,
192194 patch : change . diff ,
193195 additions : change . additions ,
@@ -196,7 +198,7 @@ export const ApplyPatchTool = Tool.define(
196198 } ) )
197199
198200 // Check permissions if needed
199- const relativePaths = fileChanges . map ( ( c ) => path . relative ( Instance . worktree , c . filePath ) . replaceAll ( "\\" , "/" ) )
201+ const relativePaths = fileChanges . map ( ( c ) => path . relative ( instance . worktree , c . filePath ) . replaceAll ( "\\" , "/" ) )
200202 yield * ctx . ask ( {
201203 permission : "edit" ,
202204 patterns : relativePaths ,
@@ -267,13 +269,13 @@ export const ApplyPatchTool = Tool.define(
267269 // Generate output summary
268270 const summaryLines = fileChanges . map ( ( change ) => {
269271 if ( change . type === "add" ) {
270- return `A ${ path . relative ( Instance . worktree , change . filePath ) . replaceAll ( "\\" , "/" ) } `
272+ return `A ${ path . relative ( instance . worktree , change . filePath ) . replaceAll ( "\\" , "/" ) } `
271273 }
272274 if ( change . type === "delete" ) {
273- return `D ${ path . relative ( Instance . worktree , change . filePath ) . replaceAll ( "\\" , "/" ) } `
275+ return `D ${ path . relative ( instance . worktree , change . filePath ) . replaceAll ( "\\" , "/" ) } `
274276 }
275277 const target = change . movePath ?? change . filePath
276- return `M ${ path . relative ( Instance . worktree , target ) . replaceAll ( "\\" , "/" ) } `
278+ return `M ${ path . relative ( instance . worktree , target ) . replaceAll ( "\\" , "/" ) } `
277279 } )
278280 let output = `Success. Updated the following files:\n${ summaryLines . join ( "\n" ) } `
279281
@@ -282,7 +284,7 @@ export const ApplyPatchTool = Tool.define(
282284 const target = change . movePath ?? change . filePath
283285 const block = LSP . Diagnostic . report ( target , diagnostics [ AppFileSystem . normalizePath ( target ) ] ?? [ ] )
284286 if ( ! block ) continue
285- const rel = path . relative ( Instance . worktree , target ) . replaceAll ( "\\" , "/" )
287+ const rel = path . relative ( instance . worktree , target ) . replaceAll ( "\\" , "/" )
286288 output += `\n\nLSP errors detected in ${ rel } , please fix:\n${ block } `
287289 }
288290
0 commit comments