| 37 | } |
| 38 | |
| 39 | class GitWorktreeChildExecutor implements SubagentWorktreeExecutor { |
| 40 | private readonly inFlight = new Map<string, Promise<SubagentWorkspaceBinding>>(); |
| 41 | private readonly repositoryTails = new Map<string, Promise<void>>(); |
| 42 | |
| 43 | constructor(private readonly worktreeRoot: string) {} |
| 44 | |
| 45 | async isAvailable( |
| 46 | input: Pick<ProvisionSubagentWorktreeInput, 'sourceCwd' | 'sourceProjectId'>, |
| 47 | ): Promise<boolean> { |
| 48 | try { |
| 49 | const source = await resolveProjectLocation({ path: input.sourceCwd }); |
| 50 | return source.kind === 'git' && source.git !== undefined; |
| 51 | } catch { |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | async provision(input: ProvisionSubagentWorktreeInput): Promise<SubagentWorkspaceBinding> { |
| 57 | const suffix = leaseSuffix(input.leaseId); |
| 58 | const existing = this.inFlight.get(input.leaseId); |
| 59 | if (existing) return existing; |
| 60 | const task = this.provisionOnce(input, suffix).finally(() => { |
| 61 | if (this.inFlight.get(input.leaseId) === task) this.inFlight.delete(input.leaseId); |
| 62 | }); |
| 63 | this.inFlight.set(input.leaseId, task); |
| 64 | return task; |
| 65 | } |
| 66 | |
| 67 | async ensure(binding: SubagentWorkspaceBinding): Promise<void> { |
| 68 | if (!isSubagentWorkspaceBinding(binding)) { |
| 69 | throw new Error('Invalid subagent worktree binding'); |
| 70 | } |
| 71 | await this.assertOwnedBindingLocation(binding); |
| 72 | const inspected = await this.inspectOwnedWorktree(binding.worktreePath); |
| 73 | if (!inspected) { |
| 74 | throw new Error(`Subagent worktree is unavailable: ${binding.worktreePath}`); |
| 75 | } |
| 76 | if (inspected.gitCommonDir !== normalize(binding.gitCommonDir)) { |
| 77 | throw new Error(`Subagent worktree binding changed: ${binding.worktreePath}`); |
| 78 | } |
| 79 | const [lease, baseCommit] = await Promise.all([ |
| 80 | gitConfigGet(inspected.worktreePath, branchLeaseConfigKey(binding.branch)), |
| 81 | gitConfigGet(inspected.worktreePath, branchBaseConfigKey(binding.branch)), |
| 82 | ]); |
| 83 | if (lease !== binding.leaseId || baseCommit !== binding.baseCommit) { |
| 84 | throw new Error(`Subagent worktree lease changed: ${binding.worktreePath}`); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | async capturePatch(binding: SubagentWorkspaceBinding): Promise<Uint8Array> { |
| 89 | await this.ensure(binding); |
| 90 | return this.withRepositoryAllocation(binding.gitCommonDir, async () => { |
| 91 | const temporary = await mkdtemp(join(tmpdir(), 'maka-subagent-patch-')); |
| 92 | const indexPath = join(temporary, 'index'); |
| 93 | try { |
| 94 | const currentIndex = ( |
| 95 | await runGit(binding.worktreePath, ['rev-parse', '--git-path', 'index']) |
| 96 | ).trim(); |
nothing calls this directly
no outgoing calls
no test coverage detected