@@ -6,6 +6,7 @@ import type { SessionHooks } from "@opencode/plugin/effect/session"
66import { Model } from "@opencode/schema/model"
77import { AbsolutePath } from "@opencode/schema/schema"
88import { Session } from "@opencode/schema/session"
9+ import { FSUtil } from "@opencode/util/fs-util"
910import { Effect , Schema } from "effect"
1011import { toolFailure } from "../runtime.js"
1112import { FileAccess } from "../../file-access.js"
@@ -145,14 +146,26 @@ export const Plugin = {
145146 execute : ( input , context ) =>
146147 Effect . gen ( function * ( ) {
147148 const sessionID = input . sessionID ?? context . sessionID
149+ const session = yield * ctx . session . get ( { sessionID } )
150+ const directory = AbsolutePath . make (
151+ FileAccess . resolvePath ( session . location . directory , input . directory . trim ( ) ) ,
152+ )
153+ // The destination becomes the session's internal directory, so leaving the project
154+ // needs the same approval as reaching outside it. The project's own worktrees don't.
155+ const target = yield * access . resolve ( { path : directory , kind : "directory" } )
156+ const worktrees = target . externalDirectory
157+ ? yield * ctx . worktree . list ( { projectID : ctx . location . project . id } )
158+ : [ ]
159+ if ( ! worktrees . some ( ( worktree ) => FSUtil . contains ( worktree . directory , directory ) ) )
160+ yield * access . authorizeExternal ( [ target ] , context )
148161 yield * ctx . session . move ( {
149162 sessionID,
150- directory : input . directory ,
163+ directory,
151164 delivery : "steer" ,
152165 } )
153166 return {
154- output : { sessionID, directory : input . directory } ,
155- content : `Moved session ${ sessionID } to ${ input . directory } .` ,
167+ output : { sessionID, directory } ,
168+ content : `Moved session ${ sessionID } to ${ directory } .` ,
156169 }
157170 } ) . pipe (
158171 Effect . mapError (
0 commit comments