@@ -30,6 +30,7 @@ import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
3030import * as ServerConfig from "../config.ts" ;
3131import { mcpToolSession , type McpToolSession } from "../mcp/AcpMcpStdioBridge.ts" ;
3232import { expandHomePath } from "../os-jank.ts" ;
33+ import * as ProcessRunner from "../processRunner.ts" ;
3334import { readPersistedServerRuntimeState } from "../serverRuntimeState.ts" ;
3435import { projectLocationFlags , resolveCliAuthConfig } from "./config.ts" ;
3536
@@ -273,8 +274,34 @@ const listAllProjects = Effect.fn("cli.thread.listAllProjects")(function* (tools
273274 return projects ;
274275} ) ;
275276
276- /** A project id, or a path inside a project's folder (deepest project wins). */
277- const resolveProject = Effect . fn ( "cli.thread.resolveProject" ) ( function * (
277+ /** The checkouts of the git repository at `directory`, or none outside a repository. */
278+ const listGitCheckouts = Effect . fn ( "cli.thread.listGitCheckouts" ) ( function * ( directory : string ) {
279+ const processRunner = yield * ProcessRunner . ProcessRunner ;
280+ const list = ( args : ReadonlyArray < string > ) =>
281+ processRunner
282+ . run ( { command : "git" , args : [ "worktree" , "list" , "--porcelain" , ...args ] , cwd : directory } )
283+ . pipe (
284+ Effect . option ,
285+ Effect . map ( ( result ) =>
286+ Option . isSome ( result ) && result . value . code === 0 ? result . value . stdout : undefined ,
287+ ) ,
288+ ) ;
289+ // Pre-2.36 Git rejects worktree list -z; normalize its line output to NUL records.
290+ const stdout = ( yield * list ( [ "-z" ] ) ) ?? ( yield * list ( [ ] ) ) ?. replaceAll ( "\n" , "\0" ) ?? "" ;
291+ return stdout . split ( "\0\0" ) . flatMap ( ( record ) => {
292+ const [ first = "" , ...attributes ] = record . split ( "\0" ) ;
293+ return first . startsWith ( "worktree " ) && ! attributes . includes ( "bare" )
294+ ? [ first . slice ( "worktree " . length ) ]
295+ : [ ] ;
296+ } ) ;
297+ } ) ;
298+
299+ /**
300+ * A project id, or a path inside a project's folder (deepest project wins).
301+ * A path inside a git worktree resolves as the same path in the repository's
302+ * other checkouts.
303+ */
304+ export const resolveProject = Effect . fn ( "cli.thread.resolveProject" ) ( function * (
278305 tools : ThreadTools ,
279306 identifier : string ,
280307) {
@@ -294,19 +321,51 @@ const resolveProject = Effect.fn("cli.thread.resolveProject")(function* (
294321 const roots = yield * Effect . forEach ( projects , ( project ) =>
295322 Effect . map ( realPath ( project . workspaceRoot ) , ( root ) => ( { project, root } ) ) ,
296323 ) ;
297- const containing = roots
298- . filter ( ( { root } ) => {
299- const relative = path . relative ( root , target ) ;
300- return (
301- relative === "" ||
302- ( relative !== ".." && ! relative . startsWith ( `..${ path . sep } ` ) && ! path . isAbsolute ( relative ) )
303- ) ;
304- } )
305- . toSorted ( ( left , right ) => right . root . length - left . root . length ) [ 0 ] ;
306- if ( containing === undefined ) {
324+ const contains = ( root : string , candidate : string ) => {
325+ const relative = path . relative ( root , candidate ) ;
326+ return (
327+ relative === "" ||
328+ ( relative !== ".." && ! relative . startsWith ( `..${ path . sep } ` ) && ! path . isAbsolute ( relative ) )
329+ ) ;
330+ } ;
331+ const deepestContaining = ( candidate : string ) =>
332+ roots
333+ . filter ( ( { root } ) => contains ( root , candidate ) )
334+ . toSorted ( ( left , right ) => right . root . length - left . root . length ) [ 0 ] ?. project ;
335+
336+ const containing = deepestContaining ( target ) ;
337+ if ( containing !== undefined ) return containing ;
338+ const targetInfo = yield * fileSystem . stat ( target ) . pipe ( Effect . option ) ;
339+ const gitDirectory =
340+ Option . isSome ( targetInfo ) && targetInfo . value . type === "File" ? path . dirname ( target ) : target ;
341+ const listed = yield * listGitCheckouts ( gitDirectory ) . pipe (
342+ Effect . flatMap ( ( paths ) => Effect . forEach ( paths , realPath ) ) ,
343+ Effect . provide ( ProcessRunner . layer ) ,
344+ ) ;
345+ const ownCheckout = listed
346+ . filter ( ( checkout ) => contains ( checkout , target ) )
347+ . toSorted ( ( left , right ) => right . length - left . length ) [ 0 ] ;
348+ if ( ownCheckout === undefined ) {
349+ return yield * new ThreadProjectNotFoundError ( { identifier } ) ;
350+ }
351+ const relative = path . relative ( ownCheckout , target ) ;
352+ const matches = new Map < ProjectId , ( typeof projects ) [ number ] > ( ) ;
353+ for ( const checkout of listed ) {
354+ if ( checkout === ownCheckout ) continue ;
355+ const project = deepestContaining ( path . join ( checkout , relative ) ) ;
356+ if ( project !== undefined ) matches . set ( project . id , project ) ;
357+ }
358+ const [ match , ...others ] = matches . values ( ) ;
359+ if ( match === undefined ) {
307360 return yield * new ThreadProjectNotFoundError ( { identifier } ) ;
308361 }
309- return containing . project ;
362+ if ( others . length > 0 ) {
363+ const names = [ match , ...others ] . map ( ( project ) => `${ project . title } (${ project . id } )` ) ;
364+ return yield * new ThreadUsageError ( {
365+ detail : `'${ identifier } ' is in a git worktree shared by several projects: ${ names . join ( ", " ) } . Pass a project id instead.` ,
366+ } ) ;
367+ }
368+ return match ;
310369} ) ;
311370
312371/** Every item on a timeline view, oldest first. */
0 commit comments