-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathpaths.ts
More file actions
215 lines (192 loc) · 6.83 KB
/
paths.ts
File metadata and controls
215 lines (192 loc) · 6.83 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* Path utilities for hash-based directory structure.
*
* Architecture:
* - Config location determines hash: sha256(dirname(configPath)).slice(0, 12)
* - Each project gets directory: ~/.agent-orchestrator/{hash}-{projectId}/
* - Sessions inside: sessions/{sessionName} (no hash prefix, already namespaced)
* - Tmux names include hash for global uniqueness: {hash}-{prefix}-{num}
*/
import { createHash } from "node:crypto";
import { dirname, basename, join } from "node:path";
import { homedir } from "node:os";
import { realpathSync, existsSync, writeFileSync, readFileSync, mkdirSync } from "node:fs";
/**
* Generate a 12-character hash from a config directory path.
* Always resolves symlinks before hashing to ensure consistency.
*/
export function generateConfigHash(configPath: string): string {
const resolved = realpathSync(configPath);
const configDir = dirname(resolved);
const hash = createHash("sha256").update(configDir).digest("hex");
return hash.slice(0, 12);
}
/**
* Generate project ID from project path (basename of the path).
* Example: ~/repos/integrator → "integrator"
*/
export function generateProjectId(projectPath: string): string {
return basename(projectPath);
}
/**
* Generate instance ID combining hash and project ID.
* Format: {hash}-{projectId}
* Example: "a3b4c5d6e7f8-integrator"
*/
export function generateInstanceId(configPath: string, projectPath: string): string {
const hash = generateConfigHash(configPath);
const projectId = generateProjectId(projectPath);
return `${hash}-${projectId}`;
}
/**
* Generate session prefix from project ID using clean heuristics.
*
* Rules:
* 1. ≤4 chars: use as-is (lowercase)
* 2. CamelCase: extract uppercase letters (PyTorch → pt)
* 3. kebab/snake case: use initials (agent-orchestrator → ao)
* 4. Single word: first 3 chars (integrator → int)
*/
export function generateSessionPrefix(projectId: string): string {
if (projectId.length <= 4) {
return projectId.toLowerCase();
}
// CamelCase: extract uppercase letters
const uppercase = projectId.match(/[A-Z]/g);
if (uppercase && uppercase.length > 1) {
return uppercase.join("").toLowerCase();
}
// kebab-case or snake_case: use initials
if (projectId.includes("-") || projectId.includes("_")) {
const separator = projectId.includes("-") ? "-" : "_";
return projectId
.split(separator)
.map((word) => word[0])
.join("")
.toLowerCase();
}
// Single word: first 3 characters
return projectId.slice(0, 3).toLowerCase();
}
/**
* Get the project base directory for a given config and project.
* Format: ~/.agent-orchestrator/{hash}-{projectId}
*/
export function getProjectBaseDir(configPath: string, projectPath: string): string {
const instanceId = generateInstanceId(configPath, projectPath);
return join(expandHome("~/.agent-orchestrator"), instanceId);
}
/**
* Get the shared observability base directory for a config.
* Format: ~/.agent-orchestrator/{hash}-observability
*/
export function getObservabilityBaseDir(configPath: string): string {
const hash = generateConfigHash(configPath);
return join(expandHome("~/.agent-orchestrator"), `${hash}-observability`);
}
/**
* Get the sessions directory for a project.
* Format: ~/.agent-orchestrator/{hash}-{projectId}/sessions
*/
export function getSessionsDir(configPath: string, projectPath: string): string {
return join(getProjectBaseDir(configPath, projectPath), "sessions");
}
/**
* Get the worktrees directory for a project.
* Format: ~/.agent-orchestrator/{hash}-{projectId}/worktrees
*/
export function getWorktreesDir(configPath: string, projectPath: string): string {
return join(getProjectBaseDir(configPath, projectPath), "worktrees");
}
/**
* Get the feedback reports directory for a project.
* Format: ~/.agent-orchestrator/{hash}-{projectId}/feedback-reports
*/
export function getFeedbackReportsDir(configPath: string, projectPath: string): string {
return join(getProjectBaseDir(configPath, projectPath), "feedback-reports");
}
export function getReviewIntegrityDir(configPath: string, projectPath: string): string {
return join(getProjectBaseDir(configPath, projectPath), "review-integrity");
}
/**
* Get the archive directory for a project.
* Format: ~/.agent-orchestrator/{hash}-{projectId}/archive
*/
export function getArchiveDir(configPath: string, projectPath: string): string {
return join(getSessionsDir(configPath, projectPath), "archive");
}
/**
* Get the .origin file path for a project.
* This file stores the config path for collision detection.
*/
export function getOriginFilePath(configPath: string, projectPath: string): string {
return join(getProjectBaseDir(configPath, projectPath), ".origin");
}
/**
* Generate user-facing session name.
* Format: {prefix}-{num}
* Example: "int-1", "ao-42"
*/
export function generateSessionName(prefix: string, num: number): string {
return `${prefix}-${num}`;
}
/**
* Generate tmux session name (globally unique).
* Format: {hash}-{prefix}-{num}
* Example: "a3b4c5d6e7f8-int-1"
*/
export function generateTmuxName(configPath: string, prefix: string, num: number): string {
const hash = generateConfigHash(configPath);
return `${hash}-${prefix}-${num}`;
}
/**
* Parse a tmux session name to extract components.
* Returns null if the name doesn't match the expected format.
*/
export function parseTmuxName(tmuxName: string): {
hash: string;
prefix: string;
num: number;
} | null {
const match = tmuxName.match(/^([a-f0-9]{12})-([a-zA-Z0-9_-]+)-(\d+)$/);
if (!match) return null;
return {
hash: match[1],
prefix: match[2],
num: parseInt(match[3], 10),
};
}
/**
* Expand ~ to home directory.
*/
export function expandHome(filepath: string): string {
if (filepath.startsWith("~/")) {
return join(homedir(), filepath.slice(2));
}
return filepath;
}
/**
* Validate and store the .origin file for a project.
* Throws if a hash collision is detected (different config, same hash).
*/
export function validateAndStoreOrigin(configPath: string, projectPath: string): void {
const originPath = getOriginFilePath(configPath, projectPath);
const resolvedConfigPath = realpathSync(configPath);
if (existsSync(originPath)) {
const stored = readFileSync(originPath, "utf-8").trim();
if (stored !== resolvedConfigPath) {
throw new Error(
`Hash collision detected!\n` +
`Directory: ${getProjectBaseDir(configPath, projectPath)}\n` +
`Expected config: ${resolvedConfigPath}\n` +
`Actual config: ${stored}\n` +
`This is a rare hash collision. Please move one of the configs to a different directory.`,
);
}
} else {
// Create project base directory and .origin file
const baseDir = getProjectBaseDir(configPath, projectPath);
mkdirSync(baseDir, { recursive: true });
writeFileSync(originPath, resolvedConfigPath, "utf-8");
}
}