-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWorktreeClassifier.js
More file actions
44 lines (40 loc) · 1.73 KB
/
Copy pathWorktreeClassifier.js
File metadata and controls
44 lines (40 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* WorktreeClassifier - detect agent sessions that ran in isolated git
* worktree checkouts.
*
* Harnesses that give an agent its own worktree (the Agent tool's
* isolation mode, Claude Code's EnterWorktree, the Cyrus Linear runner)
* start the session with a cwd inside a well-known worktree directory.
* The transcript itself carries no parent linkage (isSidechain is false,
* parentUuid is null), so the cwd convention is the only reliable signal.
* Without it, every worktree shows up as its own fake top-level project.
*/
const path = require('path');
const PATTERNS = [
// <repo>/.worktrees/<name>[/...]: Agent tool / workflow worktree isolation
{ re: /^(.*)\/\.worktrees\/[^/]+(?:\/|$)/, owner: (m) => path.basename(m[1]) },
// <repo>/.claude/worktrees/<name>[/...]: Claude Code EnterWorktree
{ re: /^(.*)\/\.claude\/worktrees\/[^/]+(?:\/|$)/, owner: (m) => path.basename(m[1]) },
// ~/.cyrus/worktrees/<issue>[/...]: Cyrus checkouts carry no repo name in
// the path, so they group under a single "cyrus" project.
{ re: /^.*\/\.cyrus\/worktrees\/[^/]+(?:\/|$)/, owner: () => 'cyrus' },
];
/**
* Classify a session cwd.
* @param {?string} cwd - The session's working directory
* @returns {?{owningProject: ?string}} null when the cwd is not a recognized
* agent-worktree location; otherwise the project the session belongs to
* (null owningProject means "recognized worktree, owner unknown").
*/
function classifyAgentWorktree(cwd) {
if (typeof cwd !== 'string' || !cwd) return null;
for (const p of PATTERNS) {
const m = cwd.match(p.re);
if (m) {
const owningProject = p.owner(m);
return { owningProject: owningProject || null };
}
}
return null;
}
module.exports = { classifyAgentWorktree };