createAgentWorktree method
Future<({String? gitRoot, String? headCommit, bool hookBased, String? worktreeBranch, String worktreePath})>
createAgentWorktree(
- String slug
Create a lightweight worktree for a subagent.
Implementation
Future<
({
String worktreePath,
String? worktreeBranch,
String? headCommit,
String? gitRoot,
bool hookBased,
})
>
createAgentWorktree(String slug) async {
validateWorktreeSlug(slug);
if (_hasWorktreeCreateHook() && onExecuteWorktreeCreateHook != null) {
final hookResult = await onExecuteWorktreeCreateHook!(slug);
_logForDebugging(
'Created hook-based agent worktree at: ${hookResult.worktreePath}',
);
return (
worktreePath: hookResult.worktreePath,
worktreeBranch: null,
headCommit: null,
gitRoot: null,
hookBased: true,
);
}
final gitRoot = _findCanonicalGitRoot(_getCwd());
if (gitRoot == null) {
throw StateError(
'Cannot create agent worktree: not in a git repository and no '
'WorktreeCreate hooks are configured.',
);
}
final result = await _getOrCreateWorktree(gitRoot, slug);
if (result is WorktreeCreated) {
_logForDebugging(
'Created agent worktree at: ${result.worktreePath} on branch: ${result.worktreeBranch}',
);
await _performPostCreationSetup(gitRoot, result.worktreePath);
} else {
// Bump mtime so periodic cleanup doesn't consider this stale
final now = DateTime.now();
try {
await Process.run('touch', [result.worktreePath]);
} catch (_) {}
_logForDebugging(
'Resuming existing agent worktree at: ${result.worktreePath}',
);
}
return (
worktreePath: result.worktreePath,
worktreeBranch: result.worktreeBranch,
headCommit: result.headCommit,
gitRoot: gitRoot,
hookBased: false,
);
}