@sniper.ai/cli 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -56
- package/dist/index.js +2742 -1260
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
4
|
import { createRequire as createRequire2 } from "module";
|
|
5
|
-
import { defineCommand as
|
|
5
|
+
import { defineCommand as defineCommand14, runMain } from "citty";
|
|
6
6
|
|
|
7
7
|
// src/commands/init.ts
|
|
8
8
|
import { defineCommand } from "citty";
|
|
@@ -13,15 +13,17 @@ import { readFile, writeFile, access } from "fs/promises";
|
|
|
13
13
|
import { join, dirname } from "path";
|
|
14
14
|
import { createRequire } from "module";
|
|
15
15
|
import YAML from "yaml";
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
function isV2Config(data) {
|
|
17
|
+
if (!data || typeof data !== "object") return false;
|
|
18
|
+
const cfg = data;
|
|
19
|
+
return "review_gates" in cfg || "agent_teams" in cfg || "domain_packs" in cfg;
|
|
20
|
+
}
|
|
21
|
+
function isV3Config(data) {
|
|
22
|
+
if (!data || typeof data !== "object") return false;
|
|
23
|
+
const cfg = data;
|
|
24
|
+
return "agents" in cfg && "routing" in cfg && "visibility" in cfg;
|
|
24
25
|
}
|
|
26
|
+
var CONFIG_PATH = ".sniper/config.yaml";
|
|
25
27
|
function assertField(obj, section, field, type) {
|
|
26
28
|
const val = obj[field];
|
|
27
29
|
if (typeof val !== type) {
|
|
@@ -30,18 +32,12 @@ function assertField(obj, section, field, type) {
|
|
|
30
32
|
);
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
|
-
function
|
|
35
|
+
function validateV3Config(data) {
|
|
34
36
|
if (!data || typeof data !== "object") {
|
|
35
37
|
throw new Error("Invalid config.yaml: expected an object");
|
|
36
38
|
}
|
|
37
39
|
const cfg = data;
|
|
38
|
-
for (const key of [
|
|
39
|
-
"project",
|
|
40
|
-
"stack",
|
|
41
|
-
"state",
|
|
42
|
-
"review_gates",
|
|
43
|
-
"agent_teams"
|
|
44
|
-
]) {
|
|
40
|
+
for (const key of ["project", "agents", "routing", "cost", "stack"]) {
|
|
45
41
|
if (!cfg[key] || typeof cfg[key] !== "object") {
|
|
46
42
|
throw new Error(`Invalid config.yaml: missing "${key}" section`);
|
|
47
43
|
}
|
|
@@ -49,37 +45,72 @@ function validateConfig(data) {
|
|
|
49
45
|
const project = cfg.project;
|
|
50
46
|
assertField(project, "project", "name", "string");
|
|
51
47
|
assertField(project, "project", "type", "string");
|
|
48
|
+
assertField(project, "project", "description", "string");
|
|
49
|
+
const agents = cfg.agents;
|
|
50
|
+
assertField(agents, "agents", "max_teammates", "number");
|
|
52
51
|
const stack = cfg.stack;
|
|
53
52
|
assertField(stack, "stack", "language", "string");
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
53
|
+
assertField(stack, "stack", "package_manager", "string");
|
|
54
|
+
if (!cfg.plugins || !Array.isArray(cfg.plugins)) {
|
|
55
|
+
cfg.plugins = [];
|
|
56
|
+
}
|
|
57
|
+
if (!cfg.visibility || typeof cfg.visibility !== "object") {
|
|
58
|
+
cfg.visibility = {
|
|
59
|
+
live_status: true,
|
|
60
|
+
checkpoints: true,
|
|
61
|
+
cost_tracking: true,
|
|
62
|
+
auto_retro: true
|
|
63
|
+
};
|
|
61
64
|
}
|
|
62
|
-
if (!
|
|
63
|
-
cfg.
|
|
65
|
+
if (!cfg.ownership || typeof cfg.ownership !== "object") {
|
|
66
|
+
cfg.ownership = {};
|
|
64
67
|
}
|
|
65
68
|
return data;
|
|
66
69
|
}
|
|
70
|
+
async function sniperConfigExists(cwd) {
|
|
71
|
+
try {
|
|
72
|
+
await access(join(cwd, CONFIG_PATH));
|
|
73
|
+
return true;
|
|
74
|
+
} catch {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
67
78
|
async function readConfig(cwd) {
|
|
68
79
|
const raw = await readFile(join(cwd, CONFIG_PATH), "utf-8");
|
|
69
|
-
|
|
80
|
+
const data = YAML.parse(raw);
|
|
81
|
+
if (isV2Config(data)) {
|
|
82
|
+
throw new Error(
|
|
83
|
+
'This project uses SNIPER v2 config. Run "sniper migrate" to upgrade to v3.'
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
return validateV3Config(data);
|
|
87
|
+
}
|
|
88
|
+
async function readRawConfig(cwd) {
|
|
89
|
+
const raw = await readFile(join(cwd, CONFIG_PATH), "utf-8");
|
|
90
|
+
return YAML.parse(raw);
|
|
70
91
|
}
|
|
71
92
|
async function writeConfig(cwd, config) {
|
|
72
93
|
const content = YAML.stringify(config, { lineWidth: 0 });
|
|
73
94
|
await writeFile(join(cwd, CONFIG_PATH), content, "utf-8");
|
|
74
95
|
}
|
|
96
|
+
var DEFAULT_BUDGETS = Object.freeze({
|
|
97
|
+
full: 2e6,
|
|
98
|
+
feature: 8e5,
|
|
99
|
+
patch: 2e5,
|
|
100
|
+
ingest: 1e6,
|
|
101
|
+
explore: 5e5,
|
|
102
|
+
refactor: 6e5,
|
|
103
|
+
hotfix: 1e5
|
|
104
|
+
});
|
|
75
105
|
function getCorePath() {
|
|
76
106
|
const require3 = createRequire(import.meta.url);
|
|
77
107
|
try {
|
|
78
108
|
const corePkgPath = require3.resolve("@sniper.ai/core/package.json");
|
|
79
|
-
return
|
|
80
|
-
} catch {
|
|
109
|
+
return dirname(corePkgPath);
|
|
110
|
+
} catch (err) {
|
|
81
111
|
throw new Error(
|
|
82
|
-
'@sniper.ai/core is not installed. Run "pnpm add -D @sniper.ai/core" first.'
|
|
112
|
+
'@sniper.ai/core is not installed. Run "pnpm add -D @sniper.ai/core" first.',
|
|
113
|
+
{ cause: err }
|
|
83
114
|
);
|
|
84
115
|
}
|
|
85
116
|
}
|
|
@@ -95,119 +126,239 @@ import {
|
|
|
95
126
|
} from "fs/promises";
|
|
96
127
|
import { join as join2 } from "path";
|
|
97
128
|
import YAML2 from "yaml";
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
];
|
|
129
|
+
function assertSafeName(name, kind) {
|
|
130
|
+
if (!/^[a-z][a-z0-9-]*$/.test(name)) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`Invalid ${kind} name "${name}": must start with a letter and contain only lowercase letters, digits, and hyphens`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
106
136
|
async function ensureDir(dir) {
|
|
107
137
|
await mkdir(dir, { recursive: true });
|
|
108
138
|
}
|
|
109
|
-
async function fileExists(
|
|
139
|
+
async function fileExists(p14) {
|
|
110
140
|
try {
|
|
111
|
-
await access2(
|
|
141
|
+
await access2(p14);
|
|
112
142
|
return true;
|
|
113
143
|
} catch {
|
|
114
144
|
return false;
|
|
115
145
|
}
|
|
116
146
|
}
|
|
147
|
+
async function composeMixin(basePath, mixinPaths) {
|
|
148
|
+
let content = await readFile2(basePath, "utf-8");
|
|
149
|
+
for (const mixinPath of mixinPaths) {
|
|
150
|
+
const mixin = await readFile2(mixinPath, "utf-8");
|
|
151
|
+
content += "\n\n---\n\n" + mixin;
|
|
152
|
+
}
|
|
153
|
+
return content;
|
|
154
|
+
}
|
|
155
|
+
function mergeHooks(base, ...sources) {
|
|
156
|
+
const result = { ...base };
|
|
157
|
+
if (!result.hooks || typeof result.hooks !== "object") {
|
|
158
|
+
result.hooks = {};
|
|
159
|
+
}
|
|
160
|
+
const hooks = result.hooks;
|
|
161
|
+
for (const source of sources) {
|
|
162
|
+
const sourceHooks = source.hooks || {};
|
|
163
|
+
for (const [event, entries] of Object.entries(sourceHooks)) {
|
|
164
|
+
if (!Array.isArray(entries)) continue;
|
|
165
|
+
if (!hooks[event]) hooks[event] = [];
|
|
166
|
+
for (const entry of entries) {
|
|
167
|
+
const desc = entry.description;
|
|
168
|
+
const existing = hooks[event].find(
|
|
169
|
+
(h) => h.description === desc
|
|
170
|
+
);
|
|
171
|
+
if (!existing) {
|
|
172
|
+
hooks[event].push(entry);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
117
179
|
async function scaffoldProject(cwd, config, options = {}) {
|
|
118
180
|
const corePath = getCorePath();
|
|
119
181
|
const sniperDir = join2(cwd, ".sniper");
|
|
120
|
-
const
|
|
182
|
+
const claudeDir = join2(cwd, ".claude");
|
|
183
|
+
const log14 = [];
|
|
121
184
|
const isUpdate = options.update === true;
|
|
122
185
|
await ensureDir(sniperDir);
|
|
123
|
-
for (const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
186
|
+
for (const sub of [
|
|
187
|
+
"checkpoints",
|
|
188
|
+
"gates",
|
|
189
|
+
"retros",
|
|
190
|
+
"self-reviews",
|
|
191
|
+
"protocols",
|
|
192
|
+
"knowledge",
|
|
193
|
+
"memory/signals"
|
|
194
|
+
]) {
|
|
195
|
+
await ensureDir(join2(sniperDir, sub));
|
|
196
|
+
}
|
|
197
|
+
const checklistsSrc = join2(corePath, "checklists");
|
|
198
|
+
const checklistsDest = join2(sniperDir, "checklists");
|
|
199
|
+
await cp(checklistsSrc, checklistsDest, { recursive: true, force: true });
|
|
200
|
+
log14.push("Copied checklists/");
|
|
201
|
+
const manifestTemplate = join2(corePath, "templates", "knowledge-manifest.yaml");
|
|
202
|
+
const manifestDest = join2(sniperDir, "knowledge", "manifest.yaml");
|
|
203
|
+
if (await fileExists(manifestTemplate) && !await fileExists(manifestDest)) {
|
|
204
|
+
await cp(manifestTemplate, manifestDest);
|
|
205
|
+
log14.push("Created .sniper/knowledge/manifest.yaml");
|
|
144
206
|
}
|
|
145
|
-
log9.push("Created memory/ directory");
|
|
146
207
|
if (!isUpdate) {
|
|
147
208
|
const configContent = YAML2.stringify(config, { lineWidth: 0 });
|
|
148
209
|
await writeFile2(join2(sniperDir, "config.yaml"), configContent, "utf-8");
|
|
149
|
-
|
|
210
|
+
log14.push("Created .sniper/config.yaml");
|
|
211
|
+
}
|
|
212
|
+
await ensureDir(claudeDir);
|
|
213
|
+
await ensureDir(join2(claudeDir, "agents"));
|
|
214
|
+
const agentsSrc = join2(corePath, "agents");
|
|
215
|
+
for (const agentName of config.agents.base) {
|
|
216
|
+
assertSafeName(agentName, "agent");
|
|
217
|
+
const srcFile = join2(agentsSrc, `${agentName}.md`);
|
|
218
|
+
if (!await fileExists(srcFile)) continue;
|
|
219
|
+
const mixinNames = config.agents.mixins[agentName] || [];
|
|
220
|
+
if (mixinNames.length > 0) {
|
|
221
|
+
const mixinPaths = mixinNames.map((m) => {
|
|
222
|
+
assertSafeName(m, "mixin");
|
|
223
|
+
return join2(corePath, "personas", "cognitive", `${m}.md`);
|
|
224
|
+
});
|
|
225
|
+
const composed = await composeMixin(srcFile, mixinPaths);
|
|
226
|
+
await writeFile2(join2(claudeDir, "agents", `${agentName}.md`), composed, "utf-8");
|
|
227
|
+
} else {
|
|
228
|
+
await cp(srcFile, join2(claudeDir, "agents", `${agentName}.md`), { force: true });
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
log14.push("Scaffolded .claude/agents/");
|
|
232
|
+
const skillsSrc = join2(corePath, "skills");
|
|
233
|
+
const commandsDest = join2(claudeDir, "commands");
|
|
234
|
+
await ensureDir(commandsDest);
|
|
235
|
+
if (await fileExists(skillsSrc)) {
|
|
236
|
+
const skillDirs = await readdir(skillsSrc);
|
|
237
|
+
for (const skillDir of skillDirs) {
|
|
238
|
+
const skillFile = join2(skillsSrc, skillDir, "SKILL.md");
|
|
239
|
+
if (await fileExists(skillFile)) {
|
|
240
|
+
await cp(skillFile, join2(commandsDest, `${skillDir}.md`), { force: true });
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
log14.push("Copied skills to .claude/commands/");
|
|
245
|
+
const settingsPath = join2(claudeDir, "settings.json");
|
|
246
|
+
let settings = {};
|
|
247
|
+
if (isUpdate && await fileExists(settingsPath)) {
|
|
248
|
+
const raw = await readFile2(settingsPath, "utf-8");
|
|
249
|
+
try {
|
|
250
|
+
settings = JSON.parse(raw);
|
|
251
|
+
} catch {
|
|
252
|
+
log14.push("Warning: .claude/settings.json was invalid JSON; starting with empty settings");
|
|
253
|
+
settings = {};
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
const coreHooksPath = join2(corePath, "hooks", "settings-hooks.json");
|
|
257
|
+
if (await fileExists(coreHooksPath)) {
|
|
258
|
+
const coreHooks = JSON.parse(await readFile2(coreHooksPath, "utf-8"));
|
|
259
|
+
settings = mergeHooks(settings, coreHooks);
|
|
150
260
|
}
|
|
261
|
+
if (!settings.env || typeof settings.env !== "object") {
|
|
262
|
+
settings.env = {};
|
|
263
|
+
}
|
|
264
|
+
settings.env.CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS = "1";
|
|
265
|
+
const settingsExisted = isUpdate && await fileExists(settingsPath);
|
|
266
|
+
await writeFile2(settingsPath, JSON.stringify(settings, null, 2), "utf-8");
|
|
267
|
+
log14.push(settingsExisted ? "Updated .claude/settings.json hooks" : "Created .claude/settings.json");
|
|
151
268
|
if (!isUpdate || !await fileExists(join2(cwd, "CLAUDE.md"))) {
|
|
152
269
|
const claudeTemplate = await readFile2(
|
|
153
270
|
join2(corePath, "claude-md.template"),
|
|
154
271
|
"utf-8"
|
|
155
272
|
);
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
log9.push("Skipped CLAUDE.md (preserved user customizations)");
|
|
160
|
-
}
|
|
161
|
-
const settingsDir = join2(cwd, ".claude");
|
|
162
|
-
await ensureDir(settingsDir);
|
|
163
|
-
if (!isUpdate || !await fileExists(join2(settingsDir, "settings.json"))) {
|
|
164
|
-
const settingsTemplate = await readFile2(
|
|
165
|
-
join2(corePath, "settings.template.json"),
|
|
166
|
-
"utf-8"
|
|
167
|
-
);
|
|
168
|
-
await writeFile2(
|
|
169
|
-
join2(settingsDir, "settings.json"),
|
|
170
|
-
settingsTemplate,
|
|
171
|
-
"utf-8"
|
|
172
|
-
);
|
|
173
|
-
log9.push("Created .claude/settings.json");
|
|
273
|
+
const claudeMd = claudeTemplate.replace("{{PROJECT_NAME}}", config.project.name).replace("{{CUSTOM_INSTRUCTIONS}}", "");
|
|
274
|
+
await writeFile2(join2(cwd, "CLAUDE.md"), claudeMd, "utf-8");
|
|
275
|
+
log14.push("Created CLAUDE.md");
|
|
174
276
|
} else {
|
|
175
|
-
|
|
277
|
+
log14.push("Skipped CLAUDE.md (preserved user customizations)");
|
|
176
278
|
}
|
|
177
|
-
const commandsSrc = join2(corePath, "commands");
|
|
178
|
-
const commandsDest = join2(settingsDir, "commands");
|
|
179
|
-
await cp(commandsSrc, commandsDest, { recursive: true, force: true });
|
|
180
|
-
log9.push("Copied skills to .claude/commands/");
|
|
181
279
|
if (!isUpdate) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
280
|
+
await ensureDir(join2(cwd, "docs"));
|
|
281
|
+
log14.push("Created docs/");
|
|
282
|
+
}
|
|
283
|
+
return log14;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// src/fs-utils.ts
|
|
287
|
+
import { mkdir as mkdir2, access as access3 } from "fs/promises";
|
|
288
|
+
async function ensureDir2(dir) {
|
|
289
|
+
await mkdir2(dir, { recursive: true });
|
|
290
|
+
}
|
|
291
|
+
async function pathExists(path) {
|
|
292
|
+
try {
|
|
293
|
+
await access3(path);
|
|
294
|
+
return true;
|
|
295
|
+
} catch {
|
|
296
|
+
return false;
|
|
195
297
|
}
|
|
196
|
-
return log9;
|
|
197
298
|
}
|
|
198
299
|
|
|
199
300
|
// src/commands/init.ts
|
|
301
|
+
import { join as join3, basename } from "path";
|
|
302
|
+
async function detectLanguage(cwd) {
|
|
303
|
+
const checks = [
|
|
304
|
+
[["tsconfig.json"], "typescript"],
|
|
305
|
+
[["pyproject.toml", "requirements.txt"], "python"],
|
|
306
|
+
[["go.mod"], "go"],
|
|
307
|
+
[["Cargo.toml"], "rust"],
|
|
308
|
+
[["pom.xml", "build.gradle"], "java"],
|
|
309
|
+
[["package.json"], "javascript"]
|
|
310
|
+
];
|
|
311
|
+
for (const [files, lang] of checks) {
|
|
312
|
+
for (const file of files) {
|
|
313
|
+
if (await pathExists(join3(cwd, file))) return lang;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
async function detectPackageManager(cwd) {
|
|
319
|
+
const checks = [
|
|
320
|
+
["pnpm-lock.yaml", "pnpm"],
|
|
321
|
+
["yarn.lock", "yarn"],
|
|
322
|
+
["bun.lockb", "bun"],
|
|
323
|
+
["package-lock.json", "npm"],
|
|
324
|
+
["uv.lock", "uv"],
|
|
325
|
+
["poetry.lock", "poetry"]
|
|
326
|
+
];
|
|
327
|
+
for (const [file, pm] of checks) {
|
|
328
|
+
if (await pathExists(join3(cwd, file))) return pm;
|
|
329
|
+
}
|
|
330
|
+
return "npm";
|
|
331
|
+
}
|
|
332
|
+
async function detectTestRunner(cwd) {
|
|
333
|
+
const checks = [
|
|
334
|
+
[["vitest.config.ts", "vitest.config.js", "vitest.config.mts"], "vitest"],
|
|
335
|
+
[["jest.config.ts", "jest.config.js", "jest.config.mjs"], "jest"],
|
|
336
|
+
[["pytest.ini", "conftest.py", "pyproject.toml"], "pytest"]
|
|
337
|
+
];
|
|
338
|
+
for (const [files, runner] of checks) {
|
|
339
|
+
for (const file of files) {
|
|
340
|
+
if (await pathExists(join3(cwd, file))) return runner;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
200
345
|
var initCommand = defineCommand({
|
|
201
346
|
meta: {
|
|
202
347
|
name: "init",
|
|
203
|
-
description: "Initialize
|
|
348
|
+
description: "Initialize SNIPER v3 in a project"
|
|
204
349
|
},
|
|
205
350
|
run: async () => {
|
|
206
351
|
const cwd = process.cwd();
|
|
207
|
-
p.intro("SNIPER \u2014 Project Initialization");
|
|
352
|
+
p.intro("SNIPER v3 \u2014 Project Initialization");
|
|
208
353
|
if (await sniperConfigExists(cwd)) {
|
|
354
|
+
const raw = await readRawConfig(cwd);
|
|
355
|
+
if (isV2Config(raw)) {
|
|
356
|
+
p.log.warning(
|
|
357
|
+
'Detected SNIPER v2 config. Run "sniper migrate" to upgrade, or reinitialize.'
|
|
358
|
+
);
|
|
359
|
+
}
|
|
209
360
|
const overwrite = await p.confirm({
|
|
210
|
-
message: "SNIPER is already initialized
|
|
361
|
+
message: "SNIPER is already initialized. Reinitialize?",
|
|
211
362
|
initialValue: false
|
|
212
363
|
});
|
|
213
364
|
if (p.isCancel(overwrite) || !overwrite) {
|
|
@@ -215,9 +366,14 @@ var initCommand = defineCommand({
|
|
|
215
366
|
process.exit(0);
|
|
216
367
|
}
|
|
217
368
|
}
|
|
369
|
+
const detectedLang = await detectLanguage(cwd);
|
|
370
|
+
const detectedPM = await detectPackageManager(cwd);
|
|
371
|
+
const detectedTestRunner = await detectTestRunner(cwd);
|
|
372
|
+
const dirName = basename(cwd);
|
|
218
373
|
const projectName = await p.text({
|
|
219
374
|
message: "Project name:",
|
|
220
|
-
placeholder:
|
|
375
|
+
placeholder: dirName,
|
|
376
|
+
initialValue: dirName,
|
|
221
377
|
validate: (v) => v.length === 0 ? "Project name is required" : void 0
|
|
222
378
|
});
|
|
223
379
|
if (p.isCancel(projectName)) {
|
|
@@ -240,17 +396,19 @@ var initCommand = defineCommand({
|
|
|
240
396
|
process.exit(0);
|
|
241
397
|
}
|
|
242
398
|
const description = await p.text({
|
|
243
|
-
message: "One-line
|
|
244
|
-
placeholder: "A brief description
|
|
399
|
+
message: "One-line description:",
|
|
400
|
+
placeholder: "A brief description"
|
|
245
401
|
});
|
|
246
402
|
if (p.isCancel(description)) {
|
|
247
403
|
p.cancel("Aborted.");
|
|
248
404
|
process.exit(0);
|
|
249
405
|
}
|
|
250
406
|
const language = await p.select({
|
|
251
|
-
message:
|
|
407
|
+
message: `Primary language${detectedLang ? ` (detected: ${detectedLang})` : ""}:`,
|
|
408
|
+
initialValue: detectedLang || "typescript",
|
|
252
409
|
options: [
|
|
253
410
|
{ value: "typescript", label: "TypeScript" },
|
|
411
|
+
{ value: "javascript", label: "JavaScript" },
|
|
254
412
|
{ value: "python", label: "Python" },
|
|
255
413
|
{ value: "go", label: "Go" },
|
|
256
414
|
{ value: "rust", label: "Rust" },
|
|
@@ -261,63 +419,6 @@ var initCommand = defineCommand({
|
|
|
261
419
|
p.cancel("Aborted.");
|
|
262
420
|
process.exit(0);
|
|
263
421
|
}
|
|
264
|
-
const frontend = await p.select({
|
|
265
|
-
message: "Frontend framework:",
|
|
266
|
-
options: [
|
|
267
|
-
{ value: "react", label: "React" },
|
|
268
|
-
{ value: "nextjs", label: "Next.js" },
|
|
269
|
-
{ value: "vue", label: "Vue" },
|
|
270
|
-
{ value: "svelte", label: "Svelte" },
|
|
271
|
-
{ value: "none", label: "None" }
|
|
272
|
-
]
|
|
273
|
-
});
|
|
274
|
-
if (p.isCancel(frontend)) {
|
|
275
|
-
p.cancel("Aborted.");
|
|
276
|
-
process.exit(0);
|
|
277
|
-
}
|
|
278
|
-
const backend = await p.select({
|
|
279
|
-
message: "Backend framework:",
|
|
280
|
-
options: [
|
|
281
|
-
{ value: "node-express", label: "Node + Express" },
|
|
282
|
-
{ value: "node-fastify", label: "Node + Fastify" },
|
|
283
|
-
{ value: "django", label: "Django" },
|
|
284
|
-
{ value: "fastapi", label: "FastAPI" },
|
|
285
|
-
{ value: "gin", label: "Go Gin" },
|
|
286
|
-
{ value: "none", label: "None" }
|
|
287
|
-
]
|
|
288
|
-
});
|
|
289
|
-
if (p.isCancel(backend)) {
|
|
290
|
-
p.cancel("Aborted.");
|
|
291
|
-
process.exit(0);
|
|
292
|
-
}
|
|
293
|
-
const database = await p.select({
|
|
294
|
-
message: "Primary database:",
|
|
295
|
-
options: [
|
|
296
|
-
{ value: "postgresql", label: "PostgreSQL" },
|
|
297
|
-
{ value: "mysql", label: "MySQL" },
|
|
298
|
-
{ value: "mongodb", label: "MongoDB" },
|
|
299
|
-
{ value: "sqlite", label: "SQLite" },
|
|
300
|
-
{ value: "none", label: "None" }
|
|
301
|
-
]
|
|
302
|
-
});
|
|
303
|
-
if (p.isCancel(database)) {
|
|
304
|
-
p.cancel("Aborted.");
|
|
305
|
-
process.exit(0);
|
|
306
|
-
}
|
|
307
|
-
const infrastructure = await p.select({
|
|
308
|
-
message: "Cloud infrastructure:",
|
|
309
|
-
options: [
|
|
310
|
-
{ value: "aws", label: "AWS" },
|
|
311
|
-
{ value: "gcp", label: "Google Cloud" },
|
|
312
|
-
{ value: "azure", label: "Azure" },
|
|
313
|
-
{ value: "vercel", label: "Vercel" },
|
|
314
|
-
{ value: "none", label: "None / Self-hosted" }
|
|
315
|
-
]
|
|
316
|
-
});
|
|
317
|
-
if (p.isCancel(infrastructure)) {
|
|
318
|
-
p.cancel("Aborted.");
|
|
319
|
-
process.exit(0);
|
|
320
|
-
}
|
|
321
422
|
const maxTeammates = await p.text({
|
|
322
423
|
message: "Max concurrent agent teammates:",
|
|
323
424
|
placeholder: "5",
|
|
@@ -338,82 +439,83 @@ var initCommand = defineCommand({
|
|
|
338
439
|
type: projectType,
|
|
339
440
|
description: description || ""
|
|
340
441
|
},
|
|
341
|
-
|
|
342
|
-
language,
|
|
343
|
-
frontend: frontend === "none" ? null : frontend,
|
|
344
|
-
backend: backend === "none" ? null : backend,
|
|
345
|
-
database: database === "none" ? null : database,
|
|
346
|
-
cache: null,
|
|
347
|
-
infrastructure: infrastructure === "none" ? null : infrastructure,
|
|
348
|
-
test_runner: null,
|
|
349
|
-
package_manager: "pnpm"
|
|
350
|
-
},
|
|
351
|
-
review_gates: {
|
|
352
|
-
after_discover: "flexible",
|
|
353
|
-
after_plan: "strict",
|
|
354
|
-
after_solve: "flexible",
|
|
355
|
-
after_sprint: "strict"
|
|
356
|
-
},
|
|
357
|
-
agent_teams: {
|
|
442
|
+
agents: {
|
|
358
443
|
max_teammates: parseInt(maxTeammates, 10),
|
|
359
|
-
default_model: "sonnet",
|
|
360
|
-
planning_model: "opus",
|
|
361
|
-
delegate_mode: true,
|
|
362
444
|
plan_approval: true,
|
|
363
|
-
coordination_timeout: 30
|
|
445
|
+
coordination_timeout: 30,
|
|
446
|
+
base: [
|
|
447
|
+
"lead-orchestrator",
|
|
448
|
+
"analyst",
|
|
449
|
+
"architect",
|
|
450
|
+
"product-manager",
|
|
451
|
+
"backend-dev",
|
|
452
|
+
"frontend-dev",
|
|
453
|
+
"qa-engineer",
|
|
454
|
+
"code-reviewer",
|
|
455
|
+
"gate-reviewer",
|
|
456
|
+
"retro-analyst"
|
|
457
|
+
],
|
|
458
|
+
mixins: {}
|
|
459
|
+
},
|
|
460
|
+
routing: {
|
|
461
|
+
auto_detect: {
|
|
462
|
+
patch_max_files: 5,
|
|
463
|
+
feature_max_files: 20
|
|
464
|
+
},
|
|
465
|
+
default: "feature",
|
|
466
|
+
budgets: { ...DEFAULT_BUDGETS }
|
|
467
|
+
},
|
|
468
|
+
cost: {
|
|
469
|
+
warn_threshold: 0.7,
|
|
470
|
+
soft_cap: 0.9,
|
|
471
|
+
hard_cap: 1
|
|
472
|
+
},
|
|
473
|
+
review: {
|
|
474
|
+
multi_model: false,
|
|
475
|
+
models: [],
|
|
476
|
+
require_consensus: true
|
|
364
477
|
},
|
|
365
|
-
domain_packs: [],
|
|
366
478
|
ownership: {
|
|
367
|
-
backend: [
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
"src/services/",
|
|
371
|
-
"src/db/",
|
|
372
|
-
"src/workers/"
|
|
373
|
-
],
|
|
374
|
-
frontend: [
|
|
375
|
-
"src/frontend/",
|
|
376
|
-
"src/components/",
|
|
377
|
-
"src/hooks/",
|
|
378
|
-
"src/styles/",
|
|
379
|
-
"src/pages/"
|
|
380
|
-
],
|
|
381
|
-
infrastructure: [
|
|
382
|
-
"docker/",
|
|
383
|
-
".github/",
|
|
384
|
-
"infra/",
|
|
385
|
-
"terraform/",
|
|
386
|
-
"scripts/"
|
|
387
|
-
],
|
|
479
|
+
backend: ["src/backend/", "src/api/", "src/services/", "src/db/"],
|
|
480
|
+
frontend: ["src/frontend/", "src/components/", "src/hooks/", "src/styles/", "src/pages/"],
|
|
481
|
+
infrastructure: ["docker/", ".github/", "infra/", "scripts/"],
|
|
388
482
|
tests: ["tests/", "__tests__/", "*.test.*", "*.spec.*"],
|
|
389
|
-
ai: ["src/ai/", "src/ml/", "src/pipeline/"],
|
|
390
483
|
docs: ["docs/"]
|
|
391
484
|
},
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
485
|
+
stack: {
|
|
486
|
+
language,
|
|
487
|
+
frontend: null,
|
|
488
|
+
backend: null,
|
|
489
|
+
database: null,
|
|
490
|
+
infrastructure: null,
|
|
491
|
+
test_runner: detectedTestRunner,
|
|
492
|
+
package_manager: detectedPM,
|
|
493
|
+
commands: {
|
|
494
|
+
test: "",
|
|
495
|
+
lint: "",
|
|
496
|
+
typecheck: "",
|
|
497
|
+
build: ""
|
|
404
498
|
}
|
|
499
|
+
},
|
|
500
|
+
plugins: [],
|
|
501
|
+
triggers: [],
|
|
502
|
+
visibility: {
|
|
503
|
+
live_status: true,
|
|
504
|
+
checkpoints: true,
|
|
505
|
+
cost_tracking: true,
|
|
506
|
+
auto_retro: true
|
|
405
507
|
}
|
|
406
508
|
};
|
|
407
509
|
const s = p.spinner();
|
|
408
|
-
s.start("Scaffolding SNIPER project...");
|
|
510
|
+
s.start("Scaffolding SNIPER v3 project...");
|
|
409
511
|
try {
|
|
410
|
-
const
|
|
512
|
+
const log14 = await scaffoldProject(cwd, config);
|
|
411
513
|
s.stop("Done!");
|
|
412
|
-
for (const entry of
|
|
514
|
+
for (const entry of log14) {
|
|
413
515
|
p.log.success(entry);
|
|
414
516
|
}
|
|
415
517
|
p.outro(
|
|
416
|
-
'SNIPER initialized. Run "sniper
|
|
518
|
+
'SNIPER v3 initialized. Run "/sniper-flow" to start your first protocol.'
|
|
417
519
|
);
|
|
418
520
|
} catch (err) {
|
|
419
521
|
s.stop("Failed!");
|
|
@@ -426,1302 +528,2682 @@ var initCommand = defineCommand({
|
|
|
426
528
|
// src/commands/status.ts
|
|
427
529
|
import { defineCommand as defineCommand2 } from "citty";
|
|
428
530
|
import * as p2 from "@clack/prompts";
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
};
|
|
531
|
+
import { readFile as readFile3 } from "fs/promises";
|
|
532
|
+
import { join as join4 } from "path";
|
|
533
|
+
import YAML3 from "yaml";
|
|
433
534
|
var statusCommand = defineCommand2({
|
|
434
535
|
meta: {
|
|
435
536
|
name: "status",
|
|
436
|
-
description: "Show SNIPER
|
|
537
|
+
description: "Show SNIPER v3 status and protocol progress"
|
|
437
538
|
},
|
|
438
539
|
run: async () => {
|
|
439
540
|
const cwd = process.cwd();
|
|
440
541
|
if (!await sniperConfigExists(cwd)) {
|
|
441
542
|
p2.log.error(
|
|
442
|
-
'SNIPER is not initialized
|
|
543
|
+
'SNIPER is not initialized. Run "sniper init" first.'
|
|
443
544
|
);
|
|
444
545
|
process.exit(1);
|
|
445
546
|
}
|
|
446
547
|
const config = await readConfig(cwd);
|
|
447
|
-
p2.intro("SNIPER Status");
|
|
548
|
+
p2.intro("SNIPER v3 Status");
|
|
448
549
|
p2.log.info(
|
|
449
550
|
`Project: ${config.project.name || "(unnamed)"} (${config.project.type})`
|
|
450
551
|
);
|
|
451
|
-
p2.log.info(
|
|
452
|
-
`Phase: ${config.state.current_phase || "not started"}`
|
|
453
|
-
);
|
|
454
|
-
if (config.state.current_sprint > 0) {
|
|
455
|
-
p2.log.info(`Sprint: ${config.state.current_sprint}`);
|
|
456
|
-
}
|
|
457
|
-
p2.log.step("Artifacts:");
|
|
458
|
-
const artifacts = config.state.artifacts;
|
|
459
|
-
for (const [name, status] of Object.entries(artifacts)) {
|
|
460
|
-
const icon = status ? ARTIFACT_ICONS[status] || "?" : "\u25CB";
|
|
461
|
-
const label = status || "\u2014";
|
|
462
|
-
console.log(` ${icon} ${name.padEnd(16)} ${label}`);
|
|
463
|
-
}
|
|
464
|
-
if (config.domain_packs && config.domain_packs.length > 0) {
|
|
465
|
-
const packNames = config.domain_packs.map((pk) => pk.name).join(", ");
|
|
466
|
-
p2.log.info(`
|
|
467
|
-
Packs: ${packNames}`);
|
|
468
|
-
}
|
|
469
|
-
const stack = config.stack;
|
|
470
552
|
const stackParts = [
|
|
471
|
-
stack.language,
|
|
472
|
-
stack.frontend,
|
|
473
|
-
stack.backend,
|
|
474
|
-
stack.database,
|
|
475
|
-
stack.infrastructure
|
|
553
|
+
config.stack.language,
|
|
554
|
+
config.stack.frontend,
|
|
555
|
+
config.stack.backend,
|
|
556
|
+
config.stack.database,
|
|
557
|
+
config.stack.infrastructure
|
|
476
558
|
].filter(Boolean);
|
|
477
559
|
p2.log.info(`Stack: ${stackParts.join(", ")}`);
|
|
560
|
+
p2.log.info(`Agents: ${config.agents.base.length} configured, max ${config.agents.max_teammates} concurrent`);
|
|
561
|
+
if (config.plugins.length > 0) {
|
|
562
|
+
const pluginNames = config.plugins.map((pk) => pk.name).join(", ");
|
|
563
|
+
p2.log.info(`Plugins: ${pluginNames}`);
|
|
564
|
+
}
|
|
565
|
+
const statusPath = join4(cwd, ".sniper", "live-status.yaml");
|
|
566
|
+
if (await pathExists(statusPath)) {
|
|
567
|
+
const raw = await readFile3(statusPath, "utf-8");
|
|
568
|
+
const liveStatus = YAML3.parse(raw);
|
|
569
|
+
if (liveStatus && liveStatus.protocol) {
|
|
570
|
+
p2.log.step("Active Protocol:");
|
|
571
|
+
console.log(` Protocol: ${liveStatus.protocol}`);
|
|
572
|
+
console.log(` Status: ${liveStatus.status}`);
|
|
573
|
+
if (liveStatus.current_phase) {
|
|
574
|
+
console.log(` Phase: ${liveStatus.current_phase}`);
|
|
575
|
+
}
|
|
576
|
+
if (Array.isArray(liveStatus.phases)) {
|
|
577
|
+
for (const phase of liveStatus.phases) {
|
|
578
|
+
const icon = phase.status === "completed" ? "\u2713" : phase.status === "in_progress" ? "\u25B6" : phase.status === "failed" ? "\u2717" : "\u25CB";
|
|
579
|
+
console.log(` ${icon} ${phase.name.padEnd(16)} ${phase.status}`);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
if (liveStatus.cost && typeof liveStatus.cost.percent === "number" && typeof liveStatus.cost.tokens_used === "number" && typeof liveStatus.cost.budget === "number") {
|
|
583
|
+
const pct = Math.max(0, Math.min(100, Math.round(liveStatus.cost.percent * 100)));
|
|
584
|
+
const bar = "=".repeat(Math.floor(pct / 5)) + "-".repeat(20 - Math.floor(pct / 5));
|
|
585
|
+
console.log(`
|
|
586
|
+
Cost: ${(liveStatus.cost.tokens_used / 1e3).toFixed(0)}K / ${(liveStatus.cost.budget / 1e3).toFixed(0)}K tokens (${pct}%)`);
|
|
587
|
+
console.log(` [${bar}] ${pct}%`);
|
|
588
|
+
}
|
|
589
|
+
if (liveStatus.next_action) {
|
|
590
|
+
console.log(`
|
|
591
|
+
Next: ${liveStatus.next_action}`);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
} else {
|
|
595
|
+
p2.log.info("No active protocol. Run /sniper-flow to start.");
|
|
596
|
+
}
|
|
597
|
+
p2.log.step("Protocol Routing:");
|
|
598
|
+
console.log(` Default: ${config.routing.default}`);
|
|
599
|
+
console.log(` Budgets: full=${(config.routing.budgets.full / 1e6).toFixed(1)}M, feature=${(config.routing.budgets.feature / 1e3).toFixed(0)}K, patch=${(config.routing.budgets.patch / 1e3).toFixed(0)}K`);
|
|
600
|
+
const velocityPath = join4(cwd, ".sniper", "memory", "velocity.yaml");
|
|
601
|
+
if (await pathExists(velocityPath)) {
|
|
602
|
+
const velRaw = await readFile3(velocityPath, "utf-8");
|
|
603
|
+
const velocity = YAML3.parse(velRaw);
|
|
604
|
+
if (velocity && velocity.calibrated_budgets && Object.keys(velocity.calibrated_budgets).length > 0) {
|
|
605
|
+
p2.log.step("Velocity (calibrated budgets):");
|
|
606
|
+
for (const [protocol, budget] of Object.entries(velocity.calibrated_budgets)) {
|
|
607
|
+
const configured = config.routing.budgets[protocol];
|
|
608
|
+
const calibrated = budget;
|
|
609
|
+
const avg = velocity.rolling_averages?.[protocol];
|
|
610
|
+
const avgStr = avg ? `${(avg / 1e3).toFixed(0)}K avg` : "";
|
|
611
|
+
const trend = configured && calibrated < configured * 0.9 ? "\u2193" : calibrated > configured * 1.1 ? "\u2191" : "\u2192";
|
|
612
|
+
console.log(` ${protocol}: ${avgStr} (calibrated: ${(calibrated / 1e3).toFixed(0)}K, configured: ${configured ? (configured / 1e3).toFixed(0) + "K" : "N/A"}) ${trend}`);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
478
616
|
p2.outro("");
|
|
479
617
|
}
|
|
480
618
|
});
|
|
481
619
|
|
|
482
|
-
// src/commands/
|
|
620
|
+
// src/commands/migrate.ts
|
|
483
621
|
import { defineCommand as defineCommand3 } from "citty";
|
|
484
622
|
import * as p3 from "@clack/prompts";
|
|
623
|
+
import { writeFile as writeFile3, readFile as readFile4 } from "fs/promises";
|
|
624
|
+
import { join as join5 } from "path";
|
|
625
|
+
function migrateV2ToV3(v2) {
|
|
626
|
+
return {
|
|
627
|
+
project: {
|
|
628
|
+
name: v2.project.name,
|
|
629
|
+
type: v2.project.type,
|
|
630
|
+
description: v2.project.description || ""
|
|
631
|
+
},
|
|
632
|
+
agents: {
|
|
633
|
+
max_teammates: v2.agent_teams?.max_teammates || 5,
|
|
634
|
+
plan_approval: v2.agent_teams?.plan_approval ?? true,
|
|
635
|
+
coordination_timeout: v2.agent_teams?.coordination_timeout || 30,
|
|
636
|
+
base: [
|
|
637
|
+
"lead-orchestrator",
|
|
638
|
+
"analyst",
|
|
639
|
+
"architect",
|
|
640
|
+
"product-manager",
|
|
641
|
+
"backend-dev",
|
|
642
|
+
"frontend-dev",
|
|
643
|
+
"qa-engineer",
|
|
644
|
+
"code-reviewer",
|
|
645
|
+
"gate-reviewer",
|
|
646
|
+
"retro-analyst"
|
|
647
|
+
],
|
|
648
|
+
mixins: {}
|
|
649
|
+
},
|
|
650
|
+
routing: {
|
|
651
|
+
auto_detect: {
|
|
652
|
+
patch_max_files: 5,
|
|
653
|
+
feature_max_files: 20
|
|
654
|
+
},
|
|
655
|
+
default: "feature",
|
|
656
|
+
budgets: { ...DEFAULT_BUDGETS }
|
|
657
|
+
},
|
|
658
|
+
cost: {
|
|
659
|
+
warn_threshold: 0.7,
|
|
660
|
+
soft_cap: 0.9,
|
|
661
|
+
hard_cap: 1
|
|
662
|
+
},
|
|
663
|
+
review: {
|
|
664
|
+
multi_model: false,
|
|
665
|
+
models: [],
|
|
666
|
+
require_consensus: true
|
|
667
|
+
},
|
|
668
|
+
ownership: v2.ownership || {},
|
|
669
|
+
stack: {
|
|
670
|
+
language: v2.stack?.language || "",
|
|
671
|
+
frontend: v2.stack?.frontend || null,
|
|
672
|
+
backend: v2.stack?.backend || null,
|
|
673
|
+
database: v2.stack?.database || null,
|
|
674
|
+
infrastructure: v2.stack?.infrastructure || null,
|
|
675
|
+
test_runner: v2.stack?.test_runner || null,
|
|
676
|
+
package_manager: v2.stack?.package_manager || "npm",
|
|
677
|
+
commands: {
|
|
678
|
+
test: v2.stack?.commands?.test || "",
|
|
679
|
+
lint: v2.stack?.commands?.lint || "",
|
|
680
|
+
typecheck: v2.stack?.commands?.typecheck || "",
|
|
681
|
+
build: v2.stack?.commands?.build || ""
|
|
682
|
+
}
|
|
683
|
+
},
|
|
684
|
+
plugins: [],
|
|
685
|
+
triggers: [],
|
|
686
|
+
visibility: {
|
|
687
|
+
live_status: true,
|
|
688
|
+
checkpoints: true,
|
|
689
|
+
cost_tracking: true,
|
|
690
|
+
auto_retro: true
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
var migrateCommand = defineCommand3({
|
|
695
|
+
meta: {
|
|
696
|
+
name: "migrate",
|
|
697
|
+
description: "Migrate SNIPER v2 config to v3"
|
|
698
|
+
},
|
|
699
|
+
run: async () => {
|
|
700
|
+
const cwd = process.cwd();
|
|
701
|
+
p3.intro("SNIPER v2 \u2192 v3 Migration");
|
|
702
|
+
if (!await sniperConfigExists(cwd)) {
|
|
703
|
+
p3.log.error(
|
|
704
|
+
'No SNIPER config found. Run "sniper init" to initialize a new project.'
|
|
705
|
+
);
|
|
706
|
+
process.exit(1);
|
|
707
|
+
}
|
|
708
|
+
const raw = await readRawConfig(cwd);
|
|
709
|
+
if (isV3Config(raw)) {
|
|
710
|
+
p3.log.info("This project already uses SNIPER v3 config. No migration needed.");
|
|
711
|
+
p3.outro("");
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
if (!isV2Config(raw)) {
|
|
715
|
+
p3.log.error("Unrecognized config format. Cannot migrate.");
|
|
716
|
+
process.exit(1);
|
|
717
|
+
}
|
|
718
|
+
const v2Config = raw;
|
|
719
|
+
p3.log.info(`Migrating project: ${v2Config.project.name}`);
|
|
720
|
+
const backupPath = join5(cwd, ".sniper", "config.v2.yaml");
|
|
721
|
+
const backupContent = await readFile4(join5(cwd, ".sniper", "config.yaml"), "utf-8");
|
|
722
|
+
await writeFile3(backupPath, backupContent, "utf-8");
|
|
723
|
+
p3.log.success("Backed up v2 config to .sniper/config.v2.yaml");
|
|
724
|
+
const v3Config = migrateV2ToV3(v2Config);
|
|
725
|
+
p3.log.step("Migration changes:");
|
|
726
|
+
console.log(" - review_gates \u2192 protocol-based gates");
|
|
727
|
+
console.log(" - agent_teams \u2192 agents (with base roster + mixins)");
|
|
728
|
+
console.log(" - domain_packs \u2192 plugins");
|
|
729
|
+
console.log(" - state tracking \u2192 checkpoint files");
|
|
730
|
+
console.log(" + routing (auto protocol selection)");
|
|
731
|
+
console.log(" + cost enforcement");
|
|
732
|
+
console.log(" + visibility settings");
|
|
733
|
+
const confirm6 = await p3.confirm({
|
|
734
|
+
message: "Apply migration and re-scaffold?",
|
|
735
|
+
initialValue: true
|
|
736
|
+
});
|
|
737
|
+
if (p3.isCancel(confirm6) || !confirm6) {
|
|
738
|
+
p3.cancel("Aborted. v2 config preserved.");
|
|
739
|
+
process.exit(0);
|
|
740
|
+
}
|
|
741
|
+
const s = p3.spinner();
|
|
742
|
+
s.start("Re-scaffolding with v3 structure...");
|
|
743
|
+
try {
|
|
744
|
+
const log14 = await scaffoldProject(cwd, v3Config, { update: true });
|
|
745
|
+
await writeConfig(cwd, v3Config);
|
|
746
|
+
s.stop("Done!");
|
|
747
|
+
p3.log.success("Wrote v3 config");
|
|
748
|
+
for (const entry of log14) {
|
|
749
|
+
p3.log.success(entry);
|
|
750
|
+
}
|
|
751
|
+
p3.log.warning(
|
|
752
|
+
"Review .sniper/config.yaml to configure stack commands (test, lint, build) and agent mixins."
|
|
753
|
+
);
|
|
754
|
+
p3.outro("Migration complete.");
|
|
755
|
+
} catch (err) {
|
|
756
|
+
s.stop("Failed!");
|
|
757
|
+
p3.log.error(`Migration failed: ${err}`);
|
|
758
|
+
p3.log.info("Your v2 config is preserved at .sniper/config.yaml (backup also at .sniper/config.v2.yaml)");
|
|
759
|
+
process.exit(1);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
});
|
|
485
763
|
|
|
486
|
-
// src/
|
|
764
|
+
// src/commands/plugin.ts
|
|
765
|
+
import { defineCommand as defineCommand4 } from "citty";
|
|
766
|
+
import * as p4 from "@clack/prompts";
|
|
767
|
+
|
|
768
|
+
// src/plugin-manager.ts
|
|
487
769
|
import {
|
|
488
770
|
cp as cp2,
|
|
489
|
-
rm,
|
|
490
771
|
readdir as readdir2,
|
|
491
|
-
readFile as
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
mkdir as mkdir2
|
|
772
|
+
readFile as readFile5,
|
|
773
|
+
access as access4,
|
|
774
|
+
mkdir as mkdir3
|
|
495
775
|
} from "fs/promises";
|
|
496
|
-
import { join as
|
|
776
|
+
import { join as join6, resolve as resolve2, sep as sep2 } from "path";
|
|
497
777
|
import { execFileSync } from "child_process";
|
|
498
|
-
import
|
|
778
|
+
import YAML4 from "yaml";
|
|
779
|
+
function getPackageManagerCommand(config) {
|
|
780
|
+
return config?.stack?.package_manager || "pnpm";
|
|
781
|
+
}
|
|
499
782
|
function assertSafePath(base, untrusted) {
|
|
500
|
-
const full =
|
|
501
|
-
const safeBase =
|
|
502
|
-
if (!full.startsWith(safeBase) && full !==
|
|
783
|
+
const full = resolve2(base, untrusted);
|
|
784
|
+
const safeBase = resolve2(base) + sep2;
|
|
785
|
+
if (!full.startsWith(safeBase) && full !== resolve2(base)) {
|
|
503
786
|
throw new Error(
|
|
504
787
|
`Invalid name: path traversal detected in "${untrusted}"`
|
|
505
788
|
);
|
|
506
789
|
}
|
|
507
790
|
return full;
|
|
508
791
|
}
|
|
509
|
-
async function
|
|
792
|
+
async function pathExists2(p14) {
|
|
510
793
|
try {
|
|
511
|
-
await
|
|
794
|
+
await access4(p14);
|
|
512
795
|
return true;
|
|
513
796
|
} catch {
|
|
514
797
|
return false;
|
|
515
798
|
}
|
|
516
799
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
return JSON.parse(raw);
|
|
800
|
+
function getPackageDir(pkgName, cwd) {
|
|
801
|
+
return join6(cwd, "node_modules", ...pkgName.split("/"));
|
|
520
802
|
}
|
|
521
|
-
function
|
|
522
|
-
const
|
|
523
|
-
|
|
803
|
+
async function validatePluginYaml(pluginPath) {
|
|
804
|
+
const raw = await readFile5(pluginPath, "utf-8");
|
|
805
|
+
const manifest = YAML4.parse(raw);
|
|
806
|
+
if (!manifest.name || typeof manifest.name !== "string") {
|
|
807
|
+
throw new Error("Plugin manifest missing required 'name' field");
|
|
808
|
+
}
|
|
809
|
+
if (!manifest.version || typeof manifest.version !== "string") {
|
|
810
|
+
throw new Error("Plugin manifest missing required 'version' field");
|
|
811
|
+
}
|
|
812
|
+
return manifest;
|
|
524
813
|
}
|
|
525
|
-
async function
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
814
|
+
async function installPlugin(packageName, cwd) {
|
|
815
|
+
let projectConfig;
|
|
816
|
+
try {
|
|
817
|
+
projectConfig = await readConfig(cwd);
|
|
818
|
+
} catch {
|
|
819
|
+
}
|
|
820
|
+
const pm = getPackageManagerCommand(projectConfig);
|
|
821
|
+
execFileSync(pm, ["add", "-D", packageName], { cwd, stdio: "pipe" });
|
|
822
|
+
const pkgDir = getPackageDir(packageName, cwd);
|
|
823
|
+
const pkgJsonRaw = await readFile5(join6(pkgDir, "package.json"), "utf-8");
|
|
824
|
+
const pkgJson = JSON.parse(pkgJsonRaw);
|
|
825
|
+
const validTypes = ["plugin", "agent", "mixin", "pack"];
|
|
826
|
+
if (!pkgJson.sniper || !validTypes.includes(pkgJson.sniper.type)) {
|
|
827
|
+
execFileSync(pm, ["remove", packageName], { cwd, stdio: "pipe" });
|
|
531
828
|
throw new Error(
|
|
532
|
-
`${packageName} is not a valid SNIPER
|
|
829
|
+
`${packageName} is not a valid SNIPER package (missing sniper.type: one of ${validTypes.join(", ")})`
|
|
533
830
|
);
|
|
534
831
|
}
|
|
535
|
-
const
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
832
|
+
const sniperType = pkgJson.sniper.type;
|
|
833
|
+
if (sniperType === "agent") {
|
|
834
|
+
const agentsDir = join6(cwd, ".claude", "agents");
|
|
835
|
+
await mkdir3(agentsDir, { recursive: true });
|
|
836
|
+
const files = await readdir2(pkgDir);
|
|
837
|
+
for (const file of files) {
|
|
838
|
+
if (file.endsWith(".md") && file !== "README.md") {
|
|
839
|
+
const src = assertSafePath(pkgDir, file);
|
|
840
|
+
await cp2(src, join6(agentsDir, file), { force: true });
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
const config2 = await readConfig(cwd);
|
|
844
|
+
if (!config2.plugins.some((p14) => p14.name === pkgJson.name)) {
|
|
845
|
+
config2.plugins.push({ name: pkgJson.name, package: packageName });
|
|
846
|
+
}
|
|
847
|
+
await writeConfig(cwd, config2);
|
|
848
|
+
return { name: pkgJson.name, package: packageName, version: pkgJson.version };
|
|
849
|
+
}
|
|
850
|
+
if (sniperType === "mixin") {
|
|
851
|
+
const mixinsDir = join6(cwd, ".claude", "personas", "cognitive");
|
|
852
|
+
await mkdir3(mixinsDir, { recursive: true });
|
|
853
|
+
const files = await readdir2(pkgDir);
|
|
854
|
+
for (const file of files) {
|
|
855
|
+
if (file.endsWith(".md") && file !== "README.md") {
|
|
856
|
+
const src = assertSafePath(pkgDir, file);
|
|
857
|
+
await cp2(src, join6(mixinsDir, file), { force: true });
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
const config2 = await readConfig(cwd);
|
|
861
|
+
if (!config2.plugins.some((p14) => p14.name === pkgJson.name)) {
|
|
862
|
+
config2.plugins.push({ name: pkgJson.name, package: packageName });
|
|
863
|
+
}
|
|
864
|
+
await writeConfig(cwd, config2);
|
|
865
|
+
return { name: pkgJson.name, package: packageName, version: pkgJson.version };
|
|
866
|
+
}
|
|
867
|
+
if (sniperType === "pack") {
|
|
868
|
+
const sniperDir = join6(cwd, ".sniper");
|
|
869
|
+
const claudeDir = join6(cwd, ".claude");
|
|
870
|
+
const contentRoot = pkgJson.sniper?.packDir ? join6(pkgDir, pkgJson.sniper.packDir) : pkgDir;
|
|
871
|
+
if (pkgJson.sniper?.packDir) {
|
|
872
|
+
assertSafePath(pkgDir, pkgJson.sniper.packDir);
|
|
873
|
+
}
|
|
874
|
+
const knowledgeDir = join6(contentRoot, "knowledge");
|
|
875
|
+
if (await pathExists2(knowledgeDir)) {
|
|
876
|
+
const dest = join6(sniperDir, "knowledge");
|
|
877
|
+
await mkdir3(dest, { recursive: true });
|
|
878
|
+
await cp2(knowledgeDir, dest, { recursive: true, force: true });
|
|
879
|
+
}
|
|
880
|
+
const personasDir = join6(contentRoot, "personas");
|
|
881
|
+
if (await pathExists2(personasDir)) {
|
|
882
|
+
const dest = join6(claudeDir, "personas", "cognitive");
|
|
883
|
+
await mkdir3(dest, { recursive: true });
|
|
884
|
+
await cp2(personasDir, dest, { recursive: true, force: true });
|
|
885
|
+
}
|
|
886
|
+
const checklistsDir = join6(contentRoot, "checklists");
|
|
887
|
+
if (await pathExists2(checklistsDir)) {
|
|
888
|
+
const dest = join6(sniperDir, "checklists");
|
|
889
|
+
await mkdir3(dest, { recursive: true });
|
|
890
|
+
await cp2(checklistsDir, dest, { recursive: true, force: true });
|
|
891
|
+
}
|
|
892
|
+
const templatesDir = join6(contentRoot, "templates");
|
|
893
|
+
if (await pathExists2(templatesDir)) {
|
|
894
|
+
const dest = join6(sniperDir, "templates");
|
|
895
|
+
await mkdir3(dest, { recursive: true });
|
|
896
|
+
await cp2(templatesDir, dest, { recursive: true, force: true });
|
|
897
|
+
}
|
|
898
|
+
const config2 = await readConfig(cwd);
|
|
899
|
+
if (!config2.plugins.some((p14) => p14.name === pkgJson.name)) {
|
|
900
|
+
config2.plugins.push({ name: pkgJson.name, package: packageName });
|
|
901
|
+
}
|
|
902
|
+
await writeConfig(cwd, config2);
|
|
903
|
+
return { name: pkgJson.name, package: packageName, version: pkgJson.version };
|
|
904
|
+
}
|
|
905
|
+
const pluginYamlPath = join6(pkgDir, "plugin.yaml");
|
|
906
|
+
if (!await pathExists2(pluginYamlPath)) {
|
|
907
|
+
execFileSync(pm, ["remove", packageName], { cwd, stdio: "pipe" });
|
|
908
|
+
throw new Error(`${packageName} is missing plugin.yaml`);
|
|
909
|
+
}
|
|
910
|
+
const manifest = await validatePluginYaml(pluginYamlPath);
|
|
911
|
+
if (manifest.agent_mixins) {
|
|
912
|
+
const mixinsDir = join6(cwd, ".claude", "personas", "cognitive");
|
|
913
|
+
await mkdir3(mixinsDir, { recursive: true });
|
|
914
|
+
for (const [, mixinPaths] of Object.entries(manifest.agent_mixins)) {
|
|
915
|
+
for (const mixinPath of mixinPaths) {
|
|
916
|
+
const src = assertSafePath(pkgDir, mixinPath);
|
|
917
|
+
const parts = mixinPath.split("/");
|
|
918
|
+
const filename = parts[parts.length - 1];
|
|
919
|
+
const dest = join6(mixinsDir, filename);
|
|
920
|
+
await cp2(src, dest, { force: true });
|
|
921
|
+
}
|
|
922
|
+
}
|
|
546
923
|
}
|
|
547
924
|
const config = await readConfig(cwd);
|
|
548
|
-
if (!config.
|
|
549
|
-
|
|
550
|
-
config.domain_packs.push({ name: shortName, package: packageName });
|
|
925
|
+
if (!config.plugins.some((p14) => p14.name === manifest.name)) {
|
|
926
|
+
config.plugins.push({ name: manifest.name, package: packageName });
|
|
551
927
|
}
|
|
552
928
|
await writeConfig(cwd, config);
|
|
553
929
|
return {
|
|
554
|
-
name:
|
|
930
|
+
name: manifest.name,
|
|
555
931
|
package: packageName,
|
|
556
|
-
version:
|
|
557
|
-
contextCount
|
|
932
|
+
version: manifest.version
|
|
558
933
|
};
|
|
559
934
|
}
|
|
560
|
-
async function
|
|
935
|
+
async function removePlugin(pluginName, cwd) {
|
|
561
936
|
const config = await readConfig(cwd);
|
|
562
|
-
const
|
|
563
|
-
|
|
564
|
-
);
|
|
565
|
-
const packageName = packEntry?.package || `@sniper.ai/pack-${packName}`;
|
|
566
|
-
const domainPacksDir = join3(cwd, ".sniper", "domain-packs");
|
|
567
|
-
const packDir = assertSafePath(domainPacksDir, packName);
|
|
568
|
-
if (await pathExists(packDir)) {
|
|
569
|
-
await rm(packDir, { recursive: true, force: true });
|
|
570
|
-
}
|
|
937
|
+
const entry = config.plugins.find((p14) => p14.name === pluginName);
|
|
938
|
+
const packageName = entry?.package || `@sniper.ai/plugin-${pluginName}`;
|
|
939
|
+
const pm = getPackageManagerCommand(config);
|
|
571
940
|
try {
|
|
572
|
-
execFileSync(
|
|
941
|
+
execFileSync(pm, ["remove", packageName], { cwd, stdio: "pipe" });
|
|
573
942
|
} catch {
|
|
574
943
|
}
|
|
575
|
-
config.
|
|
576
|
-
(p9) => p9.name !== packName
|
|
577
|
-
);
|
|
944
|
+
config.plugins = config.plugins.filter((p14) => p14.name !== pluginName);
|
|
578
945
|
await writeConfig(cwd, config);
|
|
579
946
|
}
|
|
580
|
-
async function
|
|
581
|
-
const
|
|
582
|
-
|
|
583
|
-
const entries = await readdir2(packsDir);
|
|
584
|
-
const packs = [];
|
|
585
|
-
for (const entry of entries) {
|
|
586
|
-
const entryPath = join3(packsDir, entry);
|
|
587
|
-
const s = await stat(entryPath);
|
|
588
|
-
if (!s.isDirectory()) continue;
|
|
589
|
-
const packYaml = join3(entryPath, "pack.yaml");
|
|
590
|
-
if (await pathExists(packYaml)) {
|
|
591
|
-
const raw = await readFile3(packYaml, "utf-8");
|
|
592
|
-
const parsed = YAML3.parse(raw);
|
|
593
|
-
packs.push({ name: entry, version: parsed.version || "unknown" });
|
|
594
|
-
} else {
|
|
595
|
-
packs.push({ name: entry, version: "unknown" });
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
return packs;
|
|
599
|
-
}
|
|
600
|
-
async function searchRegistryPacks() {
|
|
601
|
-
try {
|
|
602
|
-
const result = execFileSync(
|
|
603
|
-
"npm",
|
|
604
|
-
["search", "@sniper.ai/pack-", "--json"],
|
|
605
|
-
{ encoding: "utf-8", timeout: 1e4, stdio: ["pipe", "pipe", "pipe"] }
|
|
606
|
-
).toString();
|
|
607
|
-
const packages = JSON.parse(result);
|
|
608
|
-
return packages.map(
|
|
609
|
-
(pkg) => ({
|
|
610
|
-
name: pkg.name,
|
|
611
|
-
version: pkg.version,
|
|
612
|
-
description: pkg.description || ""
|
|
613
|
-
})
|
|
614
|
-
);
|
|
615
|
-
} catch {
|
|
616
|
-
return [];
|
|
617
|
-
}
|
|
947
|
+
async function listPlugins(cwd) {
|
|
948
|
+
const config = await readConfig(cwd);
|
|
949
|
+
return config.plugins;
|
|
618
950
|
}
|
|
619
951
|
|
|
620
|
-
// src/commands/
|
|
621
|
-
var
|
|
952
|
+
// src/commands/plugin.ts
|
|
953
|
+
var installSubcommand = defineCommand4({
|
|
622
954
|
meta: {
|
|
623
|
-
name: "
|
|
624
|
-
description: "
|
|
955
|
+
name: "install",
|
|
956
|
+
description: "Install a SNIPER plugin"
|
|
625
957
|
},
|
|
626
958
|
args: {
|
|
627
|
-
|
|
959
|
+
package: {
|
|
628
960
|
type: "positional",
|
|
629
|
-
description: "
|
|
961
|
+
description: "Plugin package name (e.g. @sniper.ai/plugin-typescript)",
|
|
630
962
|
required: true
|
|
631
963
|
}
|
|
632
964
|
},
|
|
633
965
|
run: async ({ args }) => {
|
|
634
966
|
const cwd = process.cwd();
|
|
635
967
|
if (!await sniperConfigExists(cwd)) {
|
|
636
|
-
|
|
637
|
-
'SNIPER is not initialized in this directory. Run "sniper init" first.'
|
|
638
|
-
);
|
|
968
|
+
p4.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
639
969
|
process.exit(1);
|
|
640
970
|
}
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
packageName = `@sniper.ai/pack-${packageName}`;
|
|
644
|
-
}
|
|
645
|
-
const s = p3.spinner();
|
|
646
|
-
s.start(`Installing ${packageName}...`);
|
|
971
|
+
const s = p4.spinner();
|
|
972
|
+
s.start(`Installing ${args.package}...`);
|
|
647
973
|
try {
|
|
648
|
-
const result = await
|
|
974
|
+
const result = await installPlugin(args.package, cwd);
|
|
649
975
|
s.stop("Done!");
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
`Copied pack to .sniper/domain-packs/${result.name}/`
|
|
653
|
-
);
|
|
654
|
-
p3.log.success("Updated config.yaml with pack reference");
|
|
655
|
-
p3.log.info(
|
|
656
|
-
`
|
|
657
|
-
Pack "${result.name}" added. ${result.contextCount} context files available.`
|
|
658
|
-
);
|
|
976
|
+
p4.log.success(`Installed plugin: ${result.name} v${result.version}`);
|
|
977
|
+
p4.log.info("Plugin mixins and hooks have been merged into your project.");
|
|
659
978
|
} catch (err) {
|
|
660
979
|
s.stop("Failed!");
|
|
661
|
-
|
|
980
|
+
p4.log.error(`Installation failed: ${err}`);
|
|
662
981
|
process.exit(1);
|
|
663
982
|
}
|
|
664
983
|
}
|
|
665
984
|
});
|
|
666
|
-
|
|
667
|
-
// src/commands/remove-pack.ts
|
|
668
|
-
import { defineCommand as defineCommand4 } from "citty";
|
|
669
|
-
import * as p4 from "@clack/prompts";
|
|
670
|
-
var removePackCommand = defineCommand4({
|
|
985
|
+
var removeSubcommand = defineCommand4({
|
|
671
986
|
meta: {
|
|
672
|
-
name: "remove
|
|
673
|
-
description: "Remove a
|
|
987
|
+
name: "remove",
|
|
988
|
+
description: "Remove a SNIPER plugin"
|
|
674
989
|
},
|
|
675
990
|
args: {
|
|
676
991
|
name: {
|
|
677
992
|
type: "positional",
|
|
678
|
-
description: "
|
|
993
|
+
description: "Plugin name to remove",
|
|
679
994
|
required: true
|
|
680
995
|
}
|
|
681
996
|
},
|
|
682
997
|
run: async ({ args }) => {
|
|
683
998
|
const cwd = process.cwd();
|
|
684
999
|
if (!await sniperConfigExists(cwd)) {
|
|
685
|
-
p4.log.error(
|
|
686
|
-
'SNIPER is not initialized in this directory. Run "sniper init" first.'
|
|
687
|
-
);
|
|
1000
|
+
p4.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
688
1001
|
process.exit(1);
|
|
689
1002
|
}
|
|
690
|
-
const confirm4 = await p4.confirm({
|
|
691
|
-
message: `Remove pack "${args.name}" and all its files?`
|
|
692
|
-
});
|
|
693
|
-
if (p4.isCancel(confirm4) || !confirm4) {
|
|
694
|
-
p4.cancel("Aborted.");
|
|
695
|
-
process.exit(0);
|
|
696
|
-
}
|
|
697
1003
|
const s = p4.spinner();
|
|
698
1004
|
s.start(`Removing ${args.name}...`);
|
|
699
1005
|
try {
|
|
700
|
-
await
|
|
1006
|
+
await removePlugin(args.name, cwd);
|
|
701
1007
|
s.stop("Done!");
|
|
702
|
-
p4.log.success(`Removed
|
|
703
|
-
p4.log.success("Updated config.yaml");
|
|
1008
|
+
p4.log.success(`Removed plugin: ${args.name}`);
|
|
704
1009
|
} catch (err) {
|
|
705
1010
|
s.stop("Failed!");
|
|
706
|
-
p4.log.error(
|
|
1011
|
+
p4.log.error(`Removal failed: ${err}`);
|
|
707
1012
|
process.exit(1);
|
|
708
1013
|
}
|
|
709
1014
|
}
|
|
710
1015
|
});
|
|
711
|
-
|
|
712
|
-
// src/commands/list-packs.ts
|
|
713
|
-
import { defineCommand as defineCommand5 } from "citty";
|
|
714
|
-
import * as p5 from "@clack/prompts";
|
|
715
|
-
var listPacksCommand = defineCommand5({
|
|
1016
|
+
var listSubcommand = defineCommand4({
|
|
716
1017
|
meta: {
|
|
717
|
-
name: "list
|
|
718
|
-
description: "List
|
|
1018
|
+
name: "list",
|
|
1019
|
+
description: "List installed SNIPER plugins"
|
|
719
1020
|
},
|
|
720
1021
|
run: async () => {
|
|
721
1022
|
const cwd = process.cwd();
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
const available = await searchRegistryPacks();
|
|
726
|
-
s.stop(
|
|
727
|
-
available.length > 0 ? `Found ${available.length} pack(s) on npm` : "No packs found on npm registry (packages may not be published yet)"
|
|
728
|
-
);
|
|
729
|
-
if (available.length > 0) {
|
|
730
|
-
p5.log.step("Available packs:");
|
|
731
|
-
for (const pkg of available) {
|
|
732
|
-
console.log(
|
|
733
|
-
` ${pkg.name.padEnd(40)} v${pkg.version.padEnd(10)} ${pkg.description}`
|
|
734
|
-
);
|
|
735
|
-
}
|
|
1023
|
+
if (!await sniperConfigExists(cwd)) {
|
|
1024
|
+
p4.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
1025
|
+
process.exit(1);
|
|
736
1026
|
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
for (const pack of installed) {
|
|
742
|
-
console.log(` ${pack.name.padEnd(20)} v${pack.version}`);
|
|
743
|
-
}
|
|
744
|
-
} else {
|
|
745
|
-
p5.log.info("\nNo packs installed.");
|
|
746
|
-
}
|
|
1027
|
+
const plugins = await listPlugins(cwd);
|
|
1028
|
+
if (plugins.length === 0) {
|
|
1029
|
+
p4.log.info("No plugins installed.");
|
|
1030
|
+
return;
|
|
747
1031
|
}
|
|
748
|
-
|
|
1032
|
+
p4.log.step("Installed plugins:");
|
|
1033
|
+
for (const plugin of plugins) {
|
|
1034
|
+
console.log(` - ${plugin.name} (${plugin.package})`);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
});
|
|
1038
|
+
var pluginCommand = defineCommand4({
|
|
1039
|
+
meta: {
|
|
1040
|
+
name: "plugin",
|
|
1041
|
+
description: "Manage SNIPER plugins"
|
|
1042
|
+
},
|
|
1043
|
+
subCommands: {
|
|
1044
|
+
install: installSubcommand,
|
|
1045
|
+
remove: removeSubcommand,
|
|
1046
|
+
list: listSubcommand
|
|
749
1047
|
}
|
|
750
1048
|
});
|
|
751
1049
|
|
|
752
|
-
// src/commands/
|
|
753
|
-
import { defineCommand as
|
|
754
|
-
import * as
|
|
755
|
-
|
|
1050
|
+
// src/commands/protocol.ts
|
|
1051
|
+
import { defineCommand as defineCommand5 } from "citty";
|
|
1052
|
+
import * as p5 from "@clack/prompts";
|
|
1053
|
+
import { readdir as readdir3, readFile as readFile6, writeFile as writeFile4, mkdir as mkdir4, access as access5 } from "fs/promises";
|
|
1054
|
+
import { join as join7 } from "path";
|
|
1055
|
+
import YAML5 from "yaml";
|
|
1056
|
+
var CUSTOM_PROTOCOLS_DIR = ".sniper/protocols";
|
|
1057
|
+
function validateProtocol(data) {
|
|
1058
|
+
const errors = [];
|
|
1059
|
+
if (!data || typeof data !== "object") {
|
|
1060
|
+
errors.push({ path: "(root)", message: "Expected a YAML object" });
|
|
1061
|
+
return errors;
|
|
1062
|
+
}
|
|
1063
|
+
const proto = data;
|
|
1064
|
+
if (typeof proto.name !== "string" || proto.name.length === 0) {
|
|
1065
|
+
errors.push({ path: "name", message: "Required string field" });
|
|
1066
|
+
}
|
|
1067
|
+
if (typeof proto.description !== "string" || proto.description.length === 0) {
|
|
1068
|
+
errors.push({ path: "description", message: "Required string field" });
|
|
1069
|
+
}
|
|
1070
|
+
if (typeof proto.budget !== "number" || !Number.isInteger(proto.budget) || proto.budget < 1) {
|
|
1071
|
+
errors.push({ path: "budget", message: "Required positive integer" });
|
|
1072
|
+
}
|
|
1073
|
+
if (proto.auto_retro !== void 0 && typeof proto.auto_retro !== "boolean") {
|
|
1074
|
+
errors.push({ path: "auto_retro", message: "Must be a boolean" });
|
|
1075
|
+
}
|
|
1076
|
+
if (!Array.isArray(proto.phases) || proto.phases.length === 0) {
|
|
1077
|
+
errors.push({ path: "phases", message: "Required non-empty array" });
|
|
1078
|
+
return errors;
|
|
1079
|
+
}
|
|
1080
|
+
for (let i = 0; i < proto.phases.length; i++) {
|
|
1081
|
+
const phase = proto.phases[i];
|
|
1082
|
+
const prefix = `phases[${i}]`;
|
|
1083
|
+
if (!phase || typeof phase !== "object") {
|
|
1084
|
+
errors.push({ path: prefix, message: "Must be an object" });
|
|
1085
|
+
continue;
|
|
1086
|
+
}
|
|
1087
|
+
if (typeof phase.name !== "string" || phase.name.length === 0) {
|
|
1088
|
+
errors.push({ path: `${prefix}.name`, message: "Required string field" });
|
|
1089
|
+
}
|
|
1090
|
+
if (typeof phase.description !== "string" || phase.description.length === 0) {
|
|
1091
|
+
errors.push({ path: `${prefix}.description`, message: "Required string field" });
|
|
1092
|
+
}
|
|
1093
|
+
if (!Array.isArray(phase.agents) || phase.agents.length === 0) {
|
|
1094
|
+
errors.push({ path: `${prefix}.agents`, message: "Required non-empty array of strings" });
|
|
1095
|
+
} else {
|
|
1096
|
+
for (let j = 0; j < phase.agents.length; j++) {
|
|
1097
|
+
if (typeof phase.agents[j] !== "string") {
|
|
1098
|
+
errors.push({ path: `${prefix}.agents[${j}]`, message: "Must be a string" });
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
if (phase.spawn_strategy !== "single" && phase.spawn_strategy !== "team") {
|
|
1103
|
+
errors.push({ path: `${prefix}.spawn_strategy`, message: 'Must be "single" or "team"' });
|
|
1104
|
+
}
|
|
1105
|
+
if (phase.gate !== void 0) {
|
|
1106
|
+
if (!phase.gate || typeof phase.gate !== "object") {
|
|
1107
|
+
errors.push({ path: `${prefix}.gate`, message: "Must be an object" });
|
|
1108
|
+
} else {
|
|
1109
|
+
const gate = phase.gate;
|
|
1110
|
+
if (typeof gate.checklist !== "string") {
|
|
1111
|
+
errors.push({ path: `${prefix}.gate.checklist`, message: "Required string field" });
|
|
1112
|
+
}
|
|
1113
|
+
if (typeof gate.human_approval !== "boolean") {
|
|
1114
|
+
errors.push({ path: `${prefix}.gate.human_approval`, message: "Required boolean field" });
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
if (phase.plan_approval !== void 0 && typeof phase.plan_approval !== "boolean") {
|
|
1119
|
+
errors.push({ path: `${prefix}.plan_approval`, message: "Must be a boolean" });
|
|
1120
|
+
}
|
|
1121
|
+
if (phase.outputs !== void 0) {
|
|
1122
|
+
if (!Array.isArray(phase.outputs)) {
|
|
1123
|
+
errors.push({ path: `${prefix}.outputs`, message: "Must be an array of strings" });
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
if (phase.coordination !== void 0) {
|
|
1127
|
+
if (!Array.isArray(phase.coordination)) {
|
|
1128
|
+
errors.push({ path: `${prefix}.coordination`, message: "Must be an array" });
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
return errors;
|
|
1133
|
+
}
|
|
1134
|
+
var createSubcommand = defineCommand5({
|
|
756
1135
|
meta: {
|
|
757
|
-
name: "
|
|
758
|
-
description: "
|
|
1136
|
+
name: "create",
|
|
1137
|
+
description: "Create a new custom protocol"
|
|
759
1138
|
},
|
|
760
|
-
|
|
1139
|
+
args: {
|
|
1140
|
+
name: {
|
|
1141
|
+
type: "positional",
|
|
1142
|
+
description: "Protocol name (e.g. my-workflow)",
|
|
1143
|
+
required: true
|
|
1144
|
+
}
|
|
1145
|
+
},
|
|
1146
|
+
run: async ({ args }) => {
|
|
761
1147
|
const cwd = process.cwd();
|
|
762
1148
|
if (!await sniperConfigExists(cwd)) {
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
1149
|
+
p5.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
1150
|
+
process.exit(1);
|
|
1151
|
+
}
|
|
1152
|
+
const protocolName = args.name;
|
|
1153
|
+
if (!/^[a-z][a-z0-9-]*$/.test(protocolName)) {
|
|
1154
|
+
p5.log.error("Protocol name must start with a letter and contain only lowercase letters, digits, and hyphens.");
|
|
1155
|
+
process.exit(1);
|
|
1156
|
+
}
|
|
1157
|
+
const protocolsDir = join7(cwd, CUSTOM_PROTOCOLS_DIR);
|
|
1158
|
+
const targetPath = join7(protocolsDir, `${protocolName}.yaml`);
|
|
1159
|
+
try {
|
|
1160
|
+
await access5(targetPath);
|
|
1161
|
+
p5.log.error(`Protocol "${args.name}" already exists at ${CUSTOM_PROTOCOLS_DIR}/${args.name}.yaml`);
|
|
766
1162
|
process.exit(1);
|
|
1163
|
+
} catch {
|
|
1164
|
+
}
|
|
1165
|
+
const description = await p5.text({
|
|
1166
|
+
message: "Protocol description:",
|
|
1167
|
+
placeholder: "Describe the goal of your protocol",
|
|
1168
|
+
validate: (val) => {
|
|
1169
|
+
if (!val || val.trim().length === 0) return "Description is required";
|
|
1170
|
+
}
|
|
1171
|
+
});
|
|
1172
|
+
if (p5.isCancel(description)) {
|
|
1173
|
+
p5.cancel("Cancelled.");
|
|
1174
|
+
process.exit(0);
|
|
767
1175
|
}
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
1176
|
+
const budgetStr = await p5.text({
|
|
1177
|
+
message: "Token budget:",
|
|
1178
|
+
placeholder: "500000",
|
|
1179
|
+
initialValue: "500000",
|
|
1180
|
+
validate: (val) => {
|
|
1181
|
+
const n = Number(val);
|
|
1182
|
+
if (!Number.isInteger(n) || n < 1) return "Must be a positive integer";
|
|
1183
|
+
}
|
|
772
1184
|
});
|
|
773
|
-
if (
|
|
774
|
-
|
|
1185
|
+
if (p5.isCancel(budgetStr)) {
|
|
1186
|
+
p5.cancel("Cancelled.");
|
|
775
1187
|
process.exit(0);
|
|
776
1188
|
}
|
|
777
|
-
const
|
|
778
|
-
|
|
1189
|
+
const budget = Number(budgetStr);
|
|
1190
|
+
let templateContent;
|
|
779
1191
|
try {
|
|
780
|
-
const
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
1192
|
+
const corePath = getCorePath();
|
|
1193
|
+
templateContent = await readFile6(
|
|
1194
|
+
join7(corePath, "templates", "custom-protocol.yaml"),
|
|
1195
|
+
"utf-8"
|
|
1196
|
+
);
|
|
1197
|
+
} catch {
|
|
1198
|
+
p5.log.warn("Could not read template from @sniper.ai/core. Using minimal template.");
|
|
1199
|
+
templateContent = YAML5.stringify({
|
|
1200
|
+
name: args.name,
|
|
1201
|
+
description,
|
|
1202
|
+
budget,
|
|
1203
|
+
phases: [
|
|
1204
|
+
{
|
|
1205
|
+
name: "implement",
|
|
1206
|
+
description: "Implementation phase",
|
|
1207
|
+
agents: ["fullstack-dev"],
|
|
1208
|
+
spawn_strategy: "single",
|
|
1209
|
+
gate: { checklist: "implement", human_approval: false },
|
|
1210
|
+
outputs: ["source code changes"]
|
|
1211
|
+
}
|
|
1212
|
+
],
|
|
1213
|
+
auto_retro: true
|
|
1214
|
+
});
|
|
1215
|
+
}
|
|
1216
|
+
let content;
|
|
1217
|
+
try {
|
|
1218
|
+
const parsed = YAML5.parse(templateContent);
|
|
1219
|
+
parsed.name = protocolName;
|
|
1220
|
+
parsed.description = description;
|
|
1221
|
+
parsed.budget = budget;
|
|
1222
|
+
content = YAML5.stringify(parsed, { lineWidth: 0 });
|
|
1223
|
+
} catch {
|
|
1224
|
+
content = templateContent.replace(/^name: .+$/m, `name: ${protocolName}`).replace(/^description: .+$/m, `description: ${YAML5.stringify(description).trim()}`).replace(/^budget: .+$/m, `budget: ${budget}`);
|
|
1225
|
+
}
|
|
1226
|
+
await mkdir4(protocolsDir, { recursive: true });
|
|
1227
|
+
await writeFile4(targetPath, content, "utf-8");
|
|
1228
|
+
p5.log.success(`Created custom protocol: ${CUSTOM_PROTOCOLS_DIR}/${protocolName}.yaml`);
|
|
1229
|
+
p5.log.info("Edit the file to customize phases, agents, and gates.");
|
|
1230
|
+
p5.log.info(`Run "sniper protocol validate ${protocolName}" to check your protocol.`);
|
|
1231
|
+
}
|
|
1232
|
+
});
|
|
1233
|
+
var listSubcommand2 = defineCommand5({
|
|
1234
|
+
meta: {
|
|
1235
|
+
name: "list",
|
|
1236
|
+
description: "List all available protocols (built-in and custom)"
|
|
1237
|
+
},
|
|
1238
|
+
run: async () => {
|
|
1239
|
+
const cwd = process.cwd();
|
|
1240
|
+
if (!await sniperConfigExists(cwd)) {
|
|
1241
|
+
p5.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
1242
|
+
process.exit(1);
|
|
1243
|
+
}
|
|
1244
|
+
let builtInFiles = [];
|
|
1245
|
+
try {
|
|
1246
|
+
const corePath = getCorePath();
|
|
1247
|
+
const protocolsPath = join7(corePath, "protocols");
|
|
1248
|
+
const files = await readdir3(protocolsPath);
|
|
1249
|
+
builtInFiles = files.filter((f) => f.endsWith(".yaml"));
|
|
1250
|
+
} catch {
|
|
1251
|
+
p5.log.warn("Could not read built-in protocols from @sniper.ai/core.");
|
|
1252
|
+
}
|
|
1253
|
+
let customFiles = [];
|
|
1254
|
+
try {
|
|
1255
|
+
const customDir = join7(cwd, CUSTOM_PROTOCOLS_DIR);
|
|
1256
|
+
const files = await readdir3(customDir);
|
|
1257
|
+
customFiles = files.filter((f) => f.endsWith(".yaml"));
|
|
1258
|
+
} catch {
|
|
1259
|
+
}
|
|
1260
|
+
if (builtInFiles.length === 0 && customFiles.length === 0) {
|
|
1261
|
+
p5.log.info("No protocols found.");
|
|
1262
|
+
return;
|
|
1263
|
+
}
|
|
1264
|
+
if (builtInFiles.length > 0) {
|
|
1265
|
+
const corePathForList = getCorePath();
|
|
1266
|
+
p5.log.step("Built-in protocols:");
|
|
1267
|
+
for (const file of builtInFiles) {
|
|
1268
|
+
const name = file.replace(/\.yaml$/, "");
|
|
1269
|
+
try {
|
|
1270
|
+
const raw = await readFile6(join7(corePathForList, "protocols", file), "utf-8");
|
|
1271
|
+
const data = YAML5.parse(raw);
|
|
1272
|
+
console.log(` - ${name}: ${data.description || "(no description)"}`);
|
|
1273
|
+
} catch {
|
|
1274
|
+
console.log(` - ${name}`);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
if (customFiles.length > 0) {
|
|
1279
|
+
p5.log.step("Custom protocols:");
|
|
1280
|
+
for (const file of customFiles) {
|
|
1281
|
+
const name = file.replace(/\.yaml$/, "");
|
|
1282
|
+
try {
|
|
1283
|
+
const raw = await readFile6(join7(cwd, CUSTOM_PROTOCOLS_DIR, file), "utf-8");
|
|
1284
|
+
const data = YAML5.parse(raw);
|
|
1285
|
+
console.log(` - ${name}: ${data.description || "(no description)"}`);
|
|
1286
|
+
} catch {
|
|
1287
|
+
console.log(` - ${name}`);
|
|
1288
|
+
}
|
|
784
1289
|
}
|
|
785
|
-
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
});
|
|
1293
|
+
var validateSubcommand = defineCommand5({
|
|
1294
|
+
meta: {
|
|
1295
|
+
name: "validate",
|
|
1296
|
+
description: "Validate a custom protocol against the schema"
|
|
1297
|
+
},
|
|
1298
|
+
args: {
|
|
1299
|
+
name: {
|
|
1300
|
+
type: "positional",
|
|
1301
|
+
description: "Protocol name to validate",
|
|
1302
|
+
required: true
|
|
1303
|
+
}
|
|
1304
|
+
},
|
|
1305
|
+
run: async ({ args }) => {
|
|
1306
|
+
const cwd = process.cwd();
|
|
1307
|
+
if (!await sniperConfigExists(cwd)) {
|
|
1308
|
+
p5.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
1309
|
+
process.exit(1);
|
|
1310
|
+
}
|
|
1311
|
+
if (!/^[a-z][a-z0-9-]*$/.test(args.name)) {
|
|
1312
|
+
p5.log.error("Protocol name must be lowercase alphanumeric with hyphens");
|
|
1313
|
+
process.exit(1);
|
|
1314
|
+
}
|
|
1315
|
+
const filePath = join7(cwd, CUSTOM_PROTOCOLS_DIR, `${args.name}.yaml`);
|
|
1316
|
+
let raw;
|
|
1317
|
+
try {
|
|
1318
|
+
raw = await readFile6(filePath, "utf-8");
|
|
1319
|
+
} catch {
|
|
1320
|
+
p5.log.error(`Protocol not found: ${CUSTOM_PROTOCOLS_DIR}/${args.name}.yaml`);
|
|
1321
|
+
process.exit(1);
|
|
1322
|
+
}
|
|
1323
|
+
let data;
|
|
1324
|
+
try {
|
|
1325
|
+
data = YAML5.parse(raw);
|
|
786
1326
|
} catch (err) {
|
|
787
|
-
|
|
788
|
-
|
|
1327
|
+
p5.log.error(`Invalid YAML: ${err}`);
|
|
1328
|
+
process.exit(1);
|
|
1329
|
+
}
|
|
1330
|
+
const errors = validateProtocol(data);
|
|
1331
|
+
if (errors.length === 0) {
|
|
1332
|
+
p5.log.success(`Protocol "${args.name}" is valid.`);
|
|
1333
|
+
} else {
|
|
1334
|
+
p5.log.error(`Protocol "${args.name}" has ${errors.length} error(s):`);
|
|
1335
|
+
for (const err of errors) {
|
|
1336
|
+
console.log(` ${err.path}: ${err.message}`);
|
|
1337
|
+
}
|
|
1338
|
+
process.exit(1);
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
});
|
|
1342
|
+
var protocolCommand = defineCommand5({
|
|
1343
|
+
meta: {
|
|
1344
|
+
name: "protocol",
|
|
1345
|
+
description: "Manage SNIPER protocols"
|
|
1346
|
+
},
|
|
1347
|
+
subCommands: {
|
|
1348
|
+
create: createSubcommand,
|
|
1349
|
+
list: listSubcommand2,
|
|
1350
|
+
validate: validateSubcommand
|
|
1351
|
+
}
|
|
1352
|
+
});
|
|
1353
|
+
|
|
1354
|
+
// src/commands/dashboard.ts
|
|
1355
|
+
import { defineCommand as defineCommand6 } from "citty";
|
|
1356
|
+
import * as p6 from "@clack/prompts";
|
|
1357
|
+
import { readFile as readFile7, readdir as readdir4 } from "fs/promises";
|
|
1358
|
+
import { join as join8 } from "path";
|
|
1359
|
+
import YAML6 from "yaml";
|
|
1360
|
+
async function readYamlDir(dirPath) {
|
|
1361
|
+
if (!await pathExists(dirPath)) return [];
|
|
1362
|
+
const files = await readdir4(dirPath);
|
|
1363
|
+
const yamlFiles = files.filter((f) => f.endsWith(".yaml") || f.endsWith(".yml"));
|
|
1364
|
+
const results = [];
|
|
1365
|
+
for (const file of yamlFiles) {
|
|
1366
|
+
try {
|
|
1367
|
+
const raw = await readFile7(join8(dirPath, file), "utf-8");
|
|
1368
|
+
const parsed = YAML6.parse(raw);
|
|
1369
|
+
if (parsed) results.push(parsed);
|
|
1370
|
+
} catch {
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
return results;
|
|
1374
|
+
}
|
|
1375
|
+
function formatTokens(n) {
|
|
1376
|
+
if (n >= 1e6) return `${(n / 1e6).toFixed(1)}M`;
|
|
1377
|
+
if (n >= 1e3) return `${(n / 1e3).toFixed(0)}K`;
|
|
1378
|
+
return String(n);
|
|
1379
|
+
}
|
|
1380
|
+
function aggregateData(checkpoints, gates, velocity, protocolFilter) {
|
|
1381
|
+
const filtered = protocolFilter ? checkpoints.filter((c) => c.protocol === protocolFilter) : checkpoints;
|
|
1382
|
+
const byProtocol = {};
|
|
1383
|
+
for (const cp3 of filtered) {
|
|
1384
|
+
if (!byProtocol[cp3.protocol]) {
|
|
1385
|
+
byProtocol[cp3.protocol] = { phase_tokens: 0, cumulative_tokens: 0, phases: [] };
|
|
1386
|
+
}
|
|
1387
|
+
const entry = byProtocol[cp3.protocol];
|
|
1388
|
+
entry.phase_tokens += cp3.token_usage?.phase_tokens ?? 0;
|
|
1389
|
+
if (cp3.token_usage?.cumulative_tokens && cp3.token_usage.cumulative_tokens > entry.cumulative_tokens) {
|
|
1390
|
+
entry.cumulative_tokens = cp3.token_usage.cumulative_tokens;
|
|
1391
|
+
}
|
|
1392
|
+
if (!entry.phases.includes(cp3.phase)) {
|
|
1393
|
+
entry.phases.push(cp3.phase);
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
const byAgent = {};
|
|
1397
|
+
for (const cp3 of filtered) {
|
|
1398
|
+
const agentCount = cp3.agents?.length ?? 1;
|
|
1399
|
+
const tokensPerAgent = agentCount > 0 ? (cp3.token_usage?.phase_tokens ?? 0) / agentCount : 0;
|
|
1400
|
+
for (const agent of cp3.agents ?? []) {
|
|
1401
|
+
if (!byAgent[agent.name]) {
|
|
1402
|
+
byAgent[agent.name] = { tokens: 0, tasks_completed: 0, tasks_total: 0 };
|
|
1403
|
+
}
|
|
1404
|
+
byAgent[agent.name].tokens += tokensPerAgent;
|
|
1405
|
+
byAgent[agent.name].tasks_completed += agent.tasks_completed;
|
|
1406
|
+
byAgent[agent.name].tasks_total += agent.tasks_total;
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
const filteredGates = protocolFilter ? gates.filter((g) => g.protocol === protocolFilter) : gates;
|
|
1410
|
+
const gateRates = {};
|
|
1411
|
+
for (const g of filteredGates) {
|
|
1412
|
+
const key = g.protocol ? `${g.protocol}/${g.gate}` : g.gate;
|
|
1413
|
+
if (!gateRates[key]) {
|
|
1414
|
+
gateRates[key] = { pass: 0, fail: 0, total_checks: 0 };
|
|
1415
|
+
}
|
|
1416
|
+
if (g.result === "pass") gateRates[key].pass++;
|
|
1417
|
+
else gateRates[key].fail++;
|
|
1418
|
+
gateRates[key].total_checks += g.total_checks;
|
|
1419
|
+
}
|
|
1420
|
+
const agentEfficiency = {};
|
|
1421
|
+
for (const [name, data] of Object.entries(byAgent)) {
|
|
1422
|
+
const totalTasks = data.tasks_completed || 1;
|
|
1423
|
+
agentEfficiency[name] = {
|
|
1424
|
+
tokens_per_task: Math.round(data.tokens / totalTasks),
|
|
1425
|
+
total_tokens: Math.round(data.tokens),
|
|
1426
|
+
total_tasks: data.tasks_completed
|
|
1427
|
+
};
|
|
1428
|
+
}
|
|
1429
|
+
const executions = velocity?.executions ?? [];
|
|
1430
|
+
const filteredExecs = protocolFilter ? executions.filter((e) => e.protocol === protocolFilter) : executions;
|
|
1431
|
+
const timeline = [];
|
|
1432
|
+
for (const cp3 of filtered) {
|
|
1433
|
+
timeline.push({
|
|
1434
|
+
timestamp: cp3.timestamp,
|
|
1435
|
+
type: "checkpoint",
|
|
1436
|
+
protocol: cp3.protocol,
|
|
1437
|
+
phase: cp3.phase,
|
|
1438
|
+
status: cp3.status
|
|
1439
|
+
});
|
|
1440
|
+
}
|
|
1441
|
+
for (const g of filteredGates) {
|
|
1442
|
+
timeline.push({
|
|
1443
|
+
timestamp: g.timestamp,
|
|
1444
|
+
type: "gate",
|
|
1445
|
+
protocol: g.protocol ?? "unknown",
|
|
1446
|
+
phase: g.gate,
|
|
1447
|
+
status: g.result
|
|
1448
|
+
});
|
|
1449
|
+
}
|
|
1450
|
+
timeline.sort((a, b) => b.timestamp.localeCompare(a.timestamp));
|
|
1451
|
+
return {
|
|
1452
|
+
cost_breakdown: { by_protocol: byProtocol, by_agent: byAgent },
|
|
1453
|
+
performance_trends: {
|
|
1454
|
+
executions: filteredExecs,
|
|
1455
|
+
calibrated_budgets: velocity?.calibrated_budgets ?? {},
|
|
1456
|
+
rolling_averages: velocity?.rolling_averages ?? {}
|
|
1457
|
+
},
|
|
1458
|
+
gate_pass_rates: gateRates,
|
|
1459
|
+
agent_efficiency: agentEfficiency,
|
|
1460
|
+
timeline: timeline.slice(0, 20)
|
|
1461
|
+
};
|
|
1462
|
+
}
|
|
1463
|
+
function renderDashboard(data, config) {
|
|
1464
|
+
p6.log.step("Cost Breakdown");
|
|
1465
|
+
const protocols = Object.entries(data.cost_breakdown.by_protocol);
|
|
1466
|
+
if (protocols.length === 0) {
|
|
1467
|
+
console.log(" No checkpoint data found.");
|
|
1468
|
+
} else {
|
|
1469
|
+
for (const [protocol, info] of protocols) {
|
|
1470
|
+
const budget = config.routing.budgets[protocol];
|
|
1471
|
+
const budgetStr = budget ? ` / ${formatTokens(budget)} budget` : "";
|
|
1472
|
+
console.log(` ${protocol}: ${formatTokens(info.cumulative_tokens)} cumulative${budgetStr}`);
|
|
1473
|
+
console.log(` Phase tokens: ${formatTokens(info.phase_tokens)} across ${info.phases.length} phase(s)`);
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
const agents = Object.entries(data.cost_breakdown.by_agent);
|
|
1477
|
+
if (agents.length > 0) {
|
|
1478
|
+
console.log("");
|
|
1479
|
+
console.log(" By Agent:");
|
|
1480
|
+
for (const [name, info] of agents) {
|
|
1481
|
+
console.log(` ${name.padEnd(24)} ${formatTokens(info.tokens).padStart(8)} tokens (${info.tasks_completed}/${info.tasks_total} tasks)`);
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
p6.log.step("Performance Trends");
|
|
1485
|
+
const execs = data.performance_trends.executions;
|
|
1486
|
+
if (execs.length === 0) {
|
|
1487
|
+
console.log(" No execution history found.");
|
|
1488
|
+
} else {
|
|
1489
|
+
const byProto = {};
|
|
1490
|
+
for (const e of execs) {
|
|
1491
|
+
if (!byProto[e.protocol]) byProto[e.protocol] = [];
|
|
1492
|
+
byProto[e.protocol].push(e);
|
|
1493
|
+
}
|
|
1494
|
+
for (const [proto, runs] of Object.entries(byProto)) {
|
|
1495
|
+
const avg = Math.round(runs.reduce((s, r) => s + r.tokens_used, 0) / runs.length);
|
|
1496
|
+
const calibrated = data.performance_trends.calibrated_budgets[proto];
|
|
1497
|
+
const rolling = data.performance_trends.rolling_averages[proto];
|
|
1498
|
+
console.log(` ${proto}: ${runs.length} execution(s), avg ${formatTokens(avg)} tokens`);
|
|
1499
|
+
if (rolling) console.log(` Rolling average: ${formatTokens(rolling)}`);
|
|
1500
|
+
if (calibrated) console.log(` Calibrated budget (p75): ${formatTokens(calibrated)}`);
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
p6.log.step("Gate Pass Rates");
|
|
1504
|
+
const gateEntries = Object.entries(data.gate_pass_rates);
|
|
1505
|
+
if (gateEntries.length === 0) {
|
|
1506
|
+
console.log(" No gate results found.");
|
|
1507
|
+
} else {
|
|
1508
|
+
for (const [key, info] of gateEntries) {
|
|
1509
|
+
const total = info.pass + info.fail;
|
|
1510
|
+
const rate = total > 0 ? Math.round(info.pass / total * 100) : 0;
|
|
1511
|
+
const icon = rate === 100 ? "\u2713" : rate >= 50 ? "~" : "\u2717";
|
|
1512
|
+
console.log(` ${icon} ${key.padEnd(28)} ${rate}% pass (${info.pass}/${total}), ${info.total_checks} checks`);
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
p6.log.step("Agent Efficiency");
|
|
1516
|
+
const effEntries = Object.entries(data.agent_efficiency);
|
|
1517
|
+
if (effEntries.length === 0) {
|
|
1518
|
+
console.log(" No agent data found.");
|
|
1519
|
+
} else {
|
|
1520
|
+
for (const [name, info] of effEntries) {
|
|
1521
|
+
console.log(` ${name.padEnd(24)} ${formatTokens(info.tokens_per_task).padStart(8)} tokens/task (${info.total_tasks} tasks, ${formatTokens(info.total_tokens)} total)`);
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
p6.log.step("Timeline (recent)");
|
|
1525
|
+
if (data.timeline.length === 0) {
|
|
1526
|
+
console.log(" No recent activity.");
|
|
1527
|
+
} else {
|
|
1528
|
+
for (const entry of data.timeline.slice(0, 10)) {
|
|
1529
|
+
const ts = entry.timestamp.replace("T", " ").substring(0, 19);
|
|
1530
|
+
const icon = entry.type === "gate" ? entry.status === "pass" ? "\u2713" : "\u2717" : "\u25B6";
|
|
1531
|
+
const phaseStr = entry.phase ? `/${entry.phase}` : "";
|
|
1532
|
+
console.log(` ${ts} ${icon} ${entry.type.padEnd(12)} ${entry.protocol}${phaseStr} [${entry.status}]`);
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
var dashboardCommand = defineCommand6({
|
|
1537
|
+
meta: {
|
|
1538
|
+
name: "dashboard",
|
|
1539
|
+
description: "Show observability dashboard with cost, performance, gates, and agent metrics"
|
|
1540
|
+
},
|
|
1541
|
+
args: {
|
|
1542
|
+
protocol: {
|
|
1543
|
+
type: "string",
|
|
1544
|
+
description: "Filter by protocol name",
|
|
1545
|
+
required: false
|
|
1546
|
+
},
|
|
1547
|
+
json: {
|
|
1548
|
+
type: "boolean",
|
|
1549
|
+
description: "Output structured JSON instead of formatted text",
|
|
1550
|
+
required: false
|
|
1551
|
+
}
|
|
1552
|
+
},
|
|
1553
|
+
run: async ({ args }) => {
|
|
1554
|
+
const cwd = process.cwd();
|
|
1555
|
+
if (!await sniperConfigExists(cwd)) {
|
|
1556
|
+
p6.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
789
1557
|
process.exit(1);
|
|
790
1558
|
}
|
|
1559
|
+
const config = await readConfig(cwd);
|
|
1560
|
+
const sniperDir = join8(cwd, ".sniper");
|
|
1561
|
+
const checkpoints = await readYamlDir(join8(sniperDir, "checkpoints"));
|
|
1562
|
+
const gates = await readYamlDir(join8(sniperDir, "gates"));
|
|
1563
|
+
let velocity = null;
|
|
1564
|
+
const velocityPath = join8(sniperDir, "memory", "velocity.yaml");
|
|
1565
|
+
if (await pathExists(velocityPath)) {
|
|
1566
|
+
try {
|
|
1567
|
+
const raw = await readFile7(velocityPath, "utf-8");
|
|
1568
|
+
velocity = YAML6.parse(raw);
|
|
1569
|
+
} catch {
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
const protocolFilter = args.protocol || void 0;
|
|
1573
|
+
const data = aggregateData(checkpoints, gates, velocity, protocolFilter);
|
|
1574
|
+
if (args.json) {
|
|
1575
|
+
console.log(JSON.stringify(data, null, 2));
|
|
1576
|
+
return;
|
|
1577
|
+
}
|
|
1578
|
+
const title = protocolFilter ? `SNIPER Dashboard \u2014 ${protocolFilter}` : "SNIPER Dashboard";
|
|
1579
|
+
p6.intro(title);
|
|
1580
|
+
if (checkpoints.length === 0 && gates.length === 0 && !velocity) {
|
|
1581
|
+
p6.log.info("No observability data found yet. Run a protocol to generate metrics.");
|
|
1582
|
+
p6.outro("");
|
|
1583
|
+
return;
|
|
1584
|
+
}
|
|
1585
|
+
renderDashboard(data, config);
|
|
1586
|
+
p6.outro("");
|
|
791
1587
|
}
|
|
792
1588
|
});
|
|
793
1589
|
|
|
794
|
-
// src/commands/
|
|
795
|
-
import { readFile as readFile4, writeFile as writeFile3, readdir as readdir3 } from "fs/promises";
|
|
796
|
-
import { join as join4 } from "path";
|
|
1590
|
+
// src/commands/workspace.ts
|
|
797
1591
|
import { defineCommand as defineCommand7 } from "citty";
|
|
798
1592
|
import * as p7 from "@clack/prompts";
|
|
799
|
-
import YAML4 from "yaml";
|
|
800
1593
|
|
|
801
|
-
// src/
|
|
802
|
-
import {
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
1594
|
+
// src/workspace-manager.ts
|
|
1595
|
+
import { readFile as readFile8, writeFile as writeFile5, access as access6, mkdir as mkdir5 } from "fs/promises";
|
|
1596
|
+
import { join as join9, resolve as resolve3, dirname as dirname2 } from "path";
|
|
1597
|
+
import YAML7 from "yaml";
|
|
1598
|
+
var WORKSPACE_DIR = ".sniper-workspace";
|
|
1599
|
+
var WORKSPACE_CONFIG = "config.yaml";
|
|
1600
|
+
async function pathExists3(p14) {
|
|
807
1601
|
try {
|
|
808
|
-
await
|
|
1602
|
+
await access6(p14);
|
|
809
1603
|
return true;
|
|
810
1604
|
} catch {
|
|
811
1605
|
return false;
|
|
812
1606
|
}
|
|
813
1607
|
}
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
1608
|
+
async function findWorkspaceRoot(cwd) {
|
|
1609
|
+
let dir = resolve3(cwd);
|
|
1610
|
+
while (true) {
|
|
1611
|
+
const configPath = join9(dir, WORKSPACE_DIR, WORKSPACE_CONFIG);
|
|
1612
|
+
if (await pathExists3(configPath)) {
|
|
1613
|
+
return dir;
|
|
1614
|
+
}
|
|
1615
|
+
const parent = dirname2(dir);
|
|
1616
|
+
if (parent === dir) break;
|
|
1617
|
+
dir = parent;
|
|
1618
|
+
}
|
|
1619
|
+
return null;
|
|
1620
|
+
}
|
|
1621
|
+
async function readWorkspaceConfig(workspaceRoot) {
|
|
1622
|
+
const configPath = join9(workspaceRoot, WORKSPACE_DIR, WORKSPACE_CONFIG);
|
|
1623
|
+
const raw = await readFile8(configPath, "utf-8");
|
|
1624
|
+
const data = YAML7.parse(raw);
|
|
1625
|
+
if (!data || typeof data !== "object") {
|
|
1626
|
+
throw new Error("Invalid workspace config: expected an object");
|
|
1627
|
+
}
|
|
1628
|
+
if (!data.name || typeof data.name !== "string") {
|
|
1629
|
+
throw new Error('Invalid workspace config: missing "name"');
|
|
1630
|
+
}
|
|
1631
|
+
if (!Array.isArray(data.projects)) {
|
|
1632
|
+
throw new Error('Invalid workspace config: missing "projects" array');
|
|
824
1633
|
}
|
|
1634
|
+
return data;
|
|
825
1635
|
}
|
|
826
|
-
async function
|
|
827
|
-
|
|
828
|
-
|
|
1636
|
+
async function addProject(workspaceRoot, name, path) {
|
|
1637
|
+
if (path.includes("..")) {
|
|
1638
|
+
throw new Error(`Project path must not contain '..': ${path}`);
|
|
1639
|
+
}
|
|
1640
|
+
const config = await readWorkspaceConfig(workspaceRoot);
|
|
1641
|
+
if (config.projects.some((p14) => p14.name === name)) {
|
|
1642
|
+
throw new Error(`Project "${name}" already exists in workspace`);
|
|
1643
|
+
}
|
|
1644
|
+
config.projects.push({ name, path });
|
|
1645
|
+
const configPath = join9(workspaceRoot, WORKSPACE_DIR, WORKSPACE_CONFIG);
|
|
1646
|
+
await writeFile5(configPath, YAML7.stringify(config, { lineWidth: 0 }), "utf-8");
|
|
829
1647
|
}
|
|
830
|
-
function
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
1648
|
+
async function syncConventions(workspaceRoot) {
|
|
1649
|
+
const config = await readWorkspaceConfig(workspaceRoot);
|
|
1650
|
+
const candidates = [];
|
|
1651
|
+
for (const project of config.projects) {
|
|
1652
|
+
const projectConfigPath = join9(
|
|
1653
|
+
workspaceRoot,
|
|
1654
|
+
project.path,
|
|
1655
|
+
".sniper",
|
|
1656
|
+
"config.yaml"
|
|
1657
|
+
);
|
|
1658
|
+
if (await pathExists3(projectConfigPath)) {
|
|
1659
|
+
candidates.push(project.name);
|
|
837
1660
|
}
|
|
838
1661
|
}
|
|
839
|
-
return
|
|
1662
|
+
return candidates;
|
|
840
1663
|
}
|
|
841
|
-
|
|
1664
|
+
async function initWorkspace(cwd, name) {
|
|
1665
|
+
const wsDir = join9(cwd, WORKSPACE_DIR);
|
|
1666
|
+
const memoryDir = join9(wsDir, "memory");
|
|
1667
|
+
const locksDir2 = join9(wsDir, "locks");
|
|
1668
|
+
await mkdir5(wsDir, { recursive: true });
|
|
1669
|
+
await mkdir5(memoryDir, { recursive: true });
|
|
1670
|
+
await mkdir5(locksDir2, { recursive: true });
|
|
1671
|
+
const config = {
|
|
1672
|
+
name,
|
|
1673
|
+
projects: [],
|
|
1674
|
+
shared: {
|
|
1675
|
+
conventions: [],
|
|
1676
|
+
anti_patterns: [],
|
|
1677
|
+
architectural_decisions: []
|
|
1678
|
+
},
|
|
1679
|
+
memory: {
|
|
1680
|
+
directory: `${WORKSPACE_DIR}/memory`
|
|
1681
|
+
}
|
|
1682
|
+
};
|
|
1683
|
+
const configPath = join9(wsDir, WORKSPACE_CONFIG);
|
|
1684
|
+
await writeFile5(configPath, YAML7.stringify(config, { lineWidth: 0 }), "utf-8");
|
|
1685
|
+
return wsDir;
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
// src/commands/workspace.ts
|
|
1689
|
+
var initSubcommand = defineCommand7({
|
|
1690
|
+
meta: {
|
|
1691
|
+
name: "init",
|
|
1692
|
+
description: "Initialize a SNIPER workspace for multi-project orchestration"
|
|
1693
|
+
},
|
|
1694
|
+
args: {
|
|
1695
|
+
name: {
|
|
1696
|
+
type: "string",
|
|
1697
|
+
description: "Workspace name",
|
|
1698
|
+
required: false
|
|
1699
|
+
}
|
|
1700
|
+
},
|
|
1701
|
+
run: async ({ args }) => {
|
|
1702
|
+
const cwd = process.cwd();
|
|
1703
|
+
p7.intro("SNIPER Workspace \u2014 Initialization");
|
|
1704
|
+
const existing = await findWorkspaceRoot(cwd);
|
|
1705
|
+
if (existing) {
|
|
1706
|
+
p7.log.warning(`Workspace already exists at ${existing}`);
|
|
1707
|
+
const overwrite = await p7.confirm({
|
|
1708
|
+
message: "Reinitialize workspace?",
|
|
1709
|
+
initialValue: false
|
|
1710
|
+
});
|
|
1711
|
+
if (p7.isCancel(overwrite) || !overwrite) {
|
|
1712
|
+
p7.cancel("Aborted.");
|
|
1713
|
+
process.exit(0);
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
let name = args.name;
|
|
1717
|
+
if (!name) {
|
|
1718
|
+
const input = await p7.text({
|
|
1719
|
+
message: "Workspace name:",
|
|
1720
|
+
placeholder: "my-workspace",
|
|
1721
|
+
validate: (v) => v.length === 0 ? "Name is required" : void 0
|
|
1722
|
+
});
|
|
1723
|
+
if (p7.isCancel(input)) {
|
|
1724
|
+
p7.cancel("Aborted.");
|
|
1725
|
+
process.exit(0);
|
|
1726
|
+
}
|
|
1727
|
+
name = input;
|
|
1728
|
+
}
|
|
1729
|
+
const s = p7.spinner();
|
|
1730
|
+
s.start("Creating workspace...");
|
|
1731
|
+
try {
|
|
1732
|
+
const wsDir = await initWorkspace(cwd, name);
|
|
1733
|
+
s.stop("Done!");
|
|
1734
|
+
p7.log.success(`Workspace "${name}" created at ${wsDir}`);
|
|
1735
|
+
p7.log.info("Created: config.yaml, memory/, locks/");
|
|
1736
|
+
p7.outro('Add projects with "sniper workspace add <name> --path <dir>"');
|
|
1737
|
+
} catch (err) {
|
|
1738
|
+
s.stop("Failed!");
|
|
1739
|
+
p7.log.error(`Workspace init failed: ${err}`);
|
|
1740
|
+
process.exit(1);
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
});
|
|
1744
|
+
var addSubcommand = defineCommand7({
|
|
1745
|
+
meta: {
|
|
1746
|
+
name: "add",
|
|
1747
|
+
description: "Add a project to the workspace"
|
|
1748
|
+
},
|
|
1749
|
+
args: {
|
|
1750
|
+
name: {
|
|
1751
|
+
type: "positional",
|
|
1752
|
+
description: "Project name",
|
|
1753
|
+
required: true
|
|
1754
|
+
},
|
|
1755
|
+
path: {
|
|
1756
|
+
type: "string",
|
|
1757
|
+
description: "Relative path to the project directory",
|
|
1758
|
+
required: true
|
|
1759
|
+
}
|
|
1760
|
+
},
|
|
1761
|
+
run: async ({ args }) => {
|
|
1762
|
+
const cwd = process.cwd();
|
|
1763
|
+
const wsRoot = await findWorkspaceRoot(cwd);
|
|
1764
|
+
if (!wsRoot) {
|
|
1765
|
+
p7.log.error('No workspace found. Run "sniper workspace init" first.');
|
|
1766
|
+
process.exit(1);
|
|
1767
|
+
}
|
|
1768
|
+
try {
|
|
1769
|
+
await addProject(wsRoot, args.name, args.path);
|
|
1770
|
+
p7.log.success(`Added project "${args.name}" (${args.path}) to workspace.`);
|
|
1771
|
+
} catch (err) {
|
|
1772
|
+
p7.log.error(`Failed to add project: ${err}`);
|
|
1773
|
+
process.exit(1);
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
});
|
|
1777
|
+
var statusSubcommand = defineCommand7({
|
|
1778
|
+
meta: {
|
|
1779
|
+
name: "status",
|
|
1780
|
+
description: "Show workspace status"
|
|
1781
|
+
},
|
|
1782
|
+
run: async () => {
|
|
1783
|
+
const cwd = process.cwd();
|
|
1784
|
+
const wsRoot = await findWorkspaceRoot(cwd);
|
|
1785
|
+
if (!wsRoot) {
|
|
1786
|
+
p7.log.error('No workspace found. Run "sniper workspace init" first.');
|
|
1787
|
+
process.exit(1);
|
|
1788
|
+
}
|
|
1789
|
+
const config = await readWorkspaceConfig(wsRoot);
|
|
1790
|
+
p7.intro(`Workspace: ${config.name}`);
|
|
1791
|
+
p7.log.step("Projects:");
|
|
1792
|
+
if (config.projects.length === 0) {
|
|
1793
|
+
console.log(" (none)");
|
|
1794
|
+
} else {
|
|
1795
|
+
for (const proj of config.projects) {
|
|
1796
|
+
const typeLabel = proj.type ? ` (${proj.type})` : "";
|
|
1797
|
+
console.log(` - ${proj.name}: ${proj.path}${typeLabel}`);
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
const conventions = config.shared?.conventions ?? [];
|
|
1801
|
+
p7.log.info(`Shared conventions: ${conventions.length}`);
|
|
1802
|
+
const antiPatterns = config.shared?.anti_patterns ?? [];
|
|
1803
|
+
p7.log.info(`Anti-patterns: ${antiPatterns.length}`);
|
|
1804
|
+
const adrs = config.shared?.architectural_decisions ?? [];
|
|
1805
|
+
p7.log.info(`Architectural decisions: ${adrs.length}`);
|
|
1806
|
+
const memDir = config.memory?.directory;
|
|
1807
|
+
p7.log.info(`Memory: ${memDir ? memDir : "not configured"}`);
|
|
1808
|
+
p7.outro("");
|
|
1809
|
+
}
|
|
1810
|
+
});
|
|
1811
|
+
var syncSubcommand = defineCommand7({
|
|
1812
|
+
meta: {
|
|
1813
|
+
name: "sync",
|
|
1814
|
+
description: "Sync shared conventions to workspace projects"
|
|
1815
|
+
},
|
|
1816
|
+
run: async () => {
|
|
1817
|
+
const cwd = process.cwd();
|
|
1818
|
+
const wsRoot = await findWorkspaceRoot(cwd);
|
|
1819
|
+
if (!wsRoot) {
|
|
1820
|
+
p7.log.error('No workspace found. Run "sniper workspace init" first.');
|
|
1821
|
+
process.exit(1);
|
|
1822
|
+
}
|
|
1823
|
+
const s = p7.spinner();
|
|
1824
|
+
s.start("Syncing conventions...");
|
|
1825
|
+
try {
|
|
1826
|
+
const synced = await syncConventions(wsRoot);
|
|
1827
|
+
s.stop("Done!");
|
|
1828
|
+
if (synced.length === 0) {
|
|
1829
|
+
p7.log.info("No projects with .sniper/config.yaml found to sync.");
|
|
1830
|
+
} else {
|
|
1831
|
+
for (const name of synced) {
|
|
1832
|
+
p7.log.success(`Checked: ${name}`);
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
} catch (err) {
|
|
1836
|
+
s.stop("Failed!");
|
|
1837
|
+
p7.log.error(`Sync failed: ${err}`);
|
|
1838
|
+
process.exit(1);
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
});
|
|
1842
|
+
var workspaceCommand = defineCommand7({
|
|
1843
|
+
meta: {
|
|
1844
|
+
name: "workspace",
|
|
1845
|
+
description: "Manage SNIPER workspaces for multi-project orchestration"
|
|
1846
|
+
},
|
|
1847
|
+
subCommands: {
|
|
1848
|
+
init: initSubcommand,
|
|
1849
|
+
add: addSubcommand,
|
|
1850
|
+
status: statusSubcommand,
|
|
1851
|
+
sync: syncSubcommand
|
|
1852
|
+
}
|
|
1853
|
+
});
|
|
1854
|
+
|
|
1855
|
+
// src/commands/revert.ts
|
|
1856
|
+
import { defineCommand as defineCommand8 } from "citty";
|
|
1857
|
+
import * as p8 from "@clack/prompts";
|
|
1858
|
+
import { readFile as readFile9, readdir as readdir5 } from "fs/promises";
|
|
1859
|
+
import { join as join10 } from "path";
|
|
1860
|
+
import { execFileSync as execFileSync2 } from "child_process";
|
|
1861
|
+
import YAML8 from "yaml";
|
|
1862
|
+
function isValidSha(sha) {
|
|
1863
|
+
return /^[0-9a-f]{7,40}$/i.test(sha);
|
|
1864
|
+
}
|
|
1865
|
+
var revertCommand = defineCommand8({
|
|
1866
|
+
meta: {
|
|
1867
|
+
name: "revert",
|
|
1868
|
+
description: "Logically revert a SNIPER protocol, phase, or checkpoint"
|
|
1869
|
+
},
|
|
1870
|
+
args: {
|
|
1871
|
+
protocol: {
|
|
1872
|
+
type: "string",
|
|
1873
|
+
description: "Protocol ID to revert"
|
|
1874
|
+
},
|
|
1875
|
+
phase: {
|
|
1876
|
+
type: "string",
|
|
1877
|
+
description: "Specific phase to revert"
|
|
1878
|
+
},
|
|
1879
|
+
checkpoint: {
|
|
1880
|
+
type: "string",
|
|
1881
|
+
description: "Specific checkpoint file to revert"
|
|
1882
|
+
},
|
|
1883
|
+
"dry-run": {
|
|
1884
|
+
type: "boolean",
|
|
1885
|
+
description: "Show what would be reverted without doing it",
|
|
1886
|
+
default: false
|
|
1887
|
+
},
|
|
1888
|
+
yes: {
|
|
1889
|
+
type: "boolean",
|
|
1890
|
+
description: "Skip confirmation prompt",
|
|
1891
|
+
default: false
|
|
1892
|
+
}
|
|
1893
|
+
},
|
|
1894
|
+
run: async ({ args }) => {
|
|
1895
|
+
const cwd = process.cwd();
|
|
1896
|
+
if (!await sniperConfigExists(cwd)) {
|
|
1897
|
+
p8.log.error(
|
|
1898
|
+
'SNIPER is not initialized. Run "sniper init" first.'
|
|
1899
|
+
);
|
|
1900
|
+
process.exit(1);
|
|
1901
|
+
}
|
|
1902
|
+
p8.intro("SNIPER v3 \u2014 Logical Revert");
|
|
1903
|
+
const checkpointsDir = join10(cwd, ".sniper", "checkpoints");
|
|
1904
|
+
if (!await pathExists(checkpointsDir)) {
|
|
1905
|
+
p8.log.error("No checkpoints found. Nothing to revert.");
|
|
1906
|
+
process.exit(1);
|
|
1907
|
+
}
|
|
1908
|
+
const files = await readdir5(checkpointsDir);
|
|
1909
|
+
const yamlFiles = files.filter(
|
|
1910
|
+
(f) => f.endsWith(".yaml") || f.endsWith(".yml")
|
|
1911
|
+
);
|
|
1912
|
+
if (yamlFiles.length === 0) {
|
|
1913
|
+
p8.log.error("No checkpoint files found. Nothing to revert.");
|
|
1914
|
+
process.exit(1);
|
|
1915
|
+
}
|
|
1916
|
+
const checkpoints = [];
|
|
1917
|
+
for (const file of yamlFiles) {
|
|
1918
|
+
const raw = await readFile9(join10(checkpointsDir, file), "utf-8");
|
|
1919
|
+
const data = YAML8.parse(raw);
|
|
1920
|
+
if (data) {
|
|
1921
|
+
checkpoints.push({ filename: file, data });
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
let filtered = checkpoints;
|
|
1925
|
+
if (args.checkpoint) {
|
|
1926
|
+
filtered = filtered.filter((c) => c.filename === args.checkpoint);
|
|
1927
|
+
}
|
|
1928
|
+
if (args.protocol) {
|
|
1929
|
+
filtered = filtered.filter((c) => c.data.protocol === args.protocol);
|
|
1930
|
+
}
|
|
1931
|
+
if (args.phase) {
|
|
1932
|
+
filtered = filtered.filter((c) => c.data.phase === args.phase);
|
|
1933
|
+
}
|
|
1934
|
+
if (filtered.length === 0) {
|
|
1935
|
+
p8.log.error("No matching checkpoints found for the given filters.");
|
|
1936
|
+
process.exit(1);
|
|
1937
|
+
}
|
|
1938
|
+
const commits = [];
|
|
1939
|
+
for (const cp3 of filtered) {
|
|
1940
|
+
if (Array.isArray(cp3.data.commits)) {
|
|
1941
|
+
for (const commit of cp3.data.commits) {
|
|
1942
|
+
if (!commits.some((c) => c.sha === commit.sha)) {
|
|
1943
|
+
commits.push(commit);
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1948
|
+
if (commits.length === 0) {
|
|
1949
|
+
p8.log.error(
|
|
1950
|
+
"No commits found in matching checkpoints. Nothing to revert."
|
|
1951
|
+
);
|
|
1952
|
+
process.exit(1);
|
|
1953
|
+
}
|
|
1954
|
+
const protocolLabel = args.protocol || filtered[0].data.protocol;
|
|
1955
|
+
const phaseLabel = args.phase || "(all phases)";
|
|
1956
|
+
p8.log.step(
|
|
1957
|
+
`Revert plan: ${commits.length} commit(s) from protocol "${protocolLabel}" ${args.phase ? `phase "${phaseLabel}"` : ""}`
|
|
1958
|
+
);
|
|
1959
|
+
for (const commit of commits) {
|
|
1960
|
+
console.log(` ${commit.sha.substring(0, 8)} ${commit.message} (${commit.agent})`);
|
|
1961
|
+
}
|
|
1962
|
+
if (args["dry-run"]) {
|
|
1963
|
+
p8.log.info("Dry run complete. No changes were made.");
|
|
1964
|
+
p8.outro("");
|
|
1965
|
+
return;
|
|
1966
|
+
}
|
|
1967
|
+
if (!args.yes) {
|
|
1968
|
+
const confirmed = await p8.confirm({
|
|
1969
|
+
message: `Revert ${commits.length} commit(s)? A backup branch will be created.`,
|
|
1970
|
+
initialValue: false
|
|
1971
|
+
});
|
|
1972
|
+
if (p8.isCancel(confirmed) || !confirmed) {
|
|
1973
|
+
p8.cancel("Revert aborted.");
|
|
1974
|
+
process.exit(0);
|
|
1975
|
+
}
|
|
1976
|
+
}
|
|
1977
|
+
const timestamp = Date.now();
|
|
1978
|
+
const backupBranch = `sniper-revert-backup-${timestamp}`;
|
|
1979
|
+
try {
|
|
1980
|
+
execFileSync2("git", ["branch", backupBranch], { cwd });
|
|
1981
|
+
p8.log.success(`Created backup branch: ${backupBranch}`);
|
|
1982
|
+
} catch (err) {
|
|
1983
|
+
p8.log.error(`Failed to create backup branch: ${err}`);
|
|
1984
|
+
process.exit(1);
|
|
1985
|
+
}
|
|
1986
|
+
const s = p8.spinner();
|
|
1987
|
+
s.start("Reverting commits...");
|
|
1988
|
+
try {
|
|
1989
|
+
for (const commit of commits) {
|
|
1990
|
+
if (!isValidSha(commit.sha)) {
|
|
1991
|
+
throw new Error(`Invalid commit SHA: ${commit.sha}`);
|
|
1992
|
+
}
|
|
1993
|
+
execFileSync2("git", ["revert", "--no-commit", commit.sha], { cwd });
|
|
1994
|
+
}
|
|
1995
|
+
const revertMessage = `revert: undo ${commits.length} commit(s) from protocol "${protocolLabel}"${args.phase ? ` phase "${phaseLabel}"` : ""}
|
|
1996
|
+
|
|
1997
|
+
Reverted commits:
|
|
1998
|
+
${commits.map((c) => ` - ${c.sha.substring(0, 8)} ${c.message}`).join("\n")}
|
|
1999
|
+
|
|
2000
|
+
Backup branch: ${backupBranch}`;
|
|
2001
|
+
execFileSync2("git", ["commit", "-m", revertMessage], { cwd });
|
|
2002
|
+
s.stop("Revert complete!");
|
|
2003
|
+
p8.log.success(
|
|
2004
|
+
`Successfully reverted ${commits.length} commit(s). Backup branch: ${backupBranch}`
|
|
2005
|
+
);
|
|
2006
|
+
p8.outro("");
|
|
2007
|
+
} catch (err) {
|
|
2008
|
+
s.stop("Revert failed!");
|
|
2009
|
+
p8.log.error(`Revert failed: ${err}`);
|
|
2010
|
+
p8.log.info(
|
|
2011
|
+
`Your backup branch "${backupBranch}" is intact. You can run "git revert --abort" to undo the partial revert.`
|
|
2012
|
+
);
|
|
2013
|
+
process.exit(1);
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
});
|
|
2017
|
+
|
|
2018
|
+
// src/commands/run.ts
|
|
2019
|
+
import { defineCommand as defineCommand9 } from "citty";
|
|
2020
|
+
import * as p9 from "@clack/prompts";
|
|
2021
|
+
|
|
2022
|
+
// src/headless.ts
|
|
2023
|
+
import { readFile as readFile10 } from "fs/promises";
|
|
2024
|
+
import { join as join11 } from "path";
|
|
2025
|
+
import YAML9 from "yaml";
|
|
2026
|
+
var BUILT_IN_PROTOCOLS = [
|
|
2027
|
+
"full",
|
|
2028
|
+
"feature",
|
|
2029
|
+
"patch",
|
|
2030
|
+
"ingest",
|
|
2031
|
+
"explore",
|
|
2032
|
+
"refactor",
|
|
2033
|
+
"hotfix"
|
|
2034
|
+
];
|
|
2035
|
+
var HeadlessRunner = class {
|
|
2036
|
+
cwd;
|
|
2037
|
+
options;
|
|
2038
|
+
constructor(cwd, options) {
|
|
2039
|
+
this.cwd = cwd;
|
|
2040
|
+
this.options = options;
|
|
2041
|
+
}
|
|
2042
|
+
async run() {
|
|
2043
|
+
const startTime = Date.now();
|
|
2044
|
+
const errors = [];
|
|
2045
|
+
let config;
|
|
2046
|
+
try {
|
|
2047
|
+
config = await readConfig(this.cwd);
|
|
2048
|
+
} catch (err) {
|
|
2049
|
+
return {
|
|
2050
|
+
exitCode: 4 /* ConfigError */,
|
|
2051
|
+
protocol: this.options.protocol,
|
|
2052
|
+
phases: [],
|
|
2053
|
+
totalTokens: 0,
|
|
2054
|
+
duration: Date.now() - startTime,
|
|
2055
|
+
errors: [
|
|
2056
|
+
`Config error: ${err instanceof Error ? err.message : String(err)}`
|
|
2057
|
+
]
|
|
2058
|
+
};
|
|
2059
|
+
}
|
|
2060
|
+
if (!/^[a-z][a-z0-9-]*$/.test(this.options.protocol)) {
|
|
2061
|
+
return {
|
|
2062
|
+
exitCode: 4 /* ConfigError */,
|
|
2063
|
+
protocol: this.options.protocol,
|
|
2064
|
+
phases: [],
|
|
2065
|
+
totalTokens: 0,
|
|
2066
|
+
duration: Date.now() - startTime,
|
|
2067
|
+
errors: [
|
|
2068
|
+
`Invalid protocol name: "${this.options.protocol}". Must be lowercase alphanumeric with hyphens.`
|
|
2069
|
+
]
|
|
2070
|
+
};
|
|
2071
|
+
}
|
|
2072
|
+
const isBuiltIn = BUILT_IN_PROTOCOLS.includes(this.options.protocol);
|
|
2073
|
+
let isCustom = false;
|
|
2074
|
+
if (!isBuiltIn) {
|
|
2075
|
+
try {
|
|
2076
|
+
const customPath = join11(
|
|
2077
|
+
this.cwd,
|
|
2078
|
+
".sniper",
|
|
2079
|
+
"protocols",
|
|
2080
|
+
`${this.options.protocol}.yaml`
|
|
2081
|
+
);
|
|
2082
|
+
await readFile10(customPath, "utf-8");
|
|
2083
|
+
isCustom = true;
|
|
2084
|
+
} catch {
|
|
2085
|
+
}
|
|
2086
|
+
}
|
|
2087
|
+
if (!isBuiltIn && !isCustom) {
|
|
2088
|
+
return {
|
|
2089
|
+
exitCode: 4 /* ConfigError */,
|
|
2090
|
+
protocol: this.options.protocol,
|
|
2091
|
+
phases: [],
|
|
2092
|
+
totalTokens: 0,
|
|
2093
|
+
duration: Date.now() - startTime,
|
|
2094
|
+
errors: [
|
|
2095
|
+
`Unknown protocol: "${this.options.protocol}". Available: ${BUILT_IN_PROTOCOLS.join(", ")} (or define a custom protocol in .sniper/protocols/)`
|
|
2096
|
+
]
|
|
2097
|
+
};
|
|
2098
|
+
}
|
|
2099
|
+
return {
|
|
2100
|
+
exitCode: 4 /* ConfigError */,
|
|
2101
|
+
protocol: this.options.protocol,
|
|
2102
|
+
phases: [],
|
|
2103
|
+
totalTokens: 0,
|
|
2104
|
+
duration: Date.now() - startTime,
|
|
2105
|
+
errors: [
|
|
2106
|
+
"Headless mode is not yet implemented. Protocol validation passed, but no execution occurred. Use /sniper-flow interactively instead."
|
|
2107
|
+
]
|
|
2108
|
+
};
|
|
2109
|
+
}
|
|
2110
|
+
formatOutput(result) {
|
|
2111
|
+
switch (this.options.outputFormat) {
|
|
2112
|
+
case "json":
|
|
2113
|
+
return JSON.stringify(
|
|
2114
|
+
{
|
|
2115
|
+
protocol: result.protocol,
|
|
2116
|
+
status: exitCodeToStatus(result.exitCode),
|
|
2117
|
+
phases: result.phases,
|
|
2118
|
+
total_tokens: result.totalTokens,
|
|
2119
|
+
duration_seconds: Math.round(result.duration / 1e3),
|
|
2120
|
+
errors: result.errors
|
|
2121
|
+
},
|
|
2122
|
+
null,
|
|
2123
|
+
2
|
|
2124
|
+
);
|
|
2125
|
+
case "yaml":
|
|
2126
|
+
return YAML9.stringify({
|
|
2127
|
+
protocol: result.protocol,
|
|
2128
|
+
status: exitCodeToStatus(result.exitCode),
|
|
2129
|
+
phases: result.phases,
|
|
2130
|
+
total_tokens: result.totalTokens,
|
|
2131
|
+
duration_seconds: Math.round(result.duration / 1e3),
|
|
2132
|
+
errors: result.errors
|
|
2133
|
+
});
|
|
2134
|
+
case "text":
|
|
2135
|
+
return formatTextTable(result);
|
|
2136
|
+
default:
|
|
2137
|
+
return JSON.stringify(
|
|
2138
|
+
{
|
|
2139
|
+
protocol: result.protocol,
|
|
2140
|
+
status: exitCodeToStatus(result.exitCode),
|
|
2141
|
+
phases: result.phases,
|
|
2142
|
+
total_tokens: result.totalTokens,
|
|
2143
|
+
duration_seconds: Math.round(result.duration / 1e3),
|
|
2144
|
+
errors: result.errors
|
|
2145
|
+
},
|
|
2146
|
+
null,
|
|
2147
|
+
2
|
|
2148
|
+
);
|
|
2149
|
+
}
|
|
2150
|
+
}
|
|
2151
|
+
};
|
|
2152
|
+
function exitCodeToStatus(code) {
|
|
2153
|
+
switch (code) {
|
|
2154
|
+
case 0 /* Success */:
|
|
2155
|
+
return "success";
|
|
2156
|
+
case 1 /* GateFail */:
|
|
2157
|
+
return "gate_fail";
|
|
2158
|
+
case 2 /* CostExceeded */:
|
|
2159
|
+
return "cost_exceeded";
|
|
2160
|
+
case 3 /* Timeout */:
|
|
2161
|
+
return "timeout";
|
|
2162
|
+
case 4 /* ConfigError */:
|
|
2163
|
+
return "config_error";
|
|
2164
|
+
}
|
|
2165
|
+
}
|
|
2166
|
+
function formatTextTable(result) {
|
|
2167
|
+
const lines = [];
|
|
2168
|
+
const status = exitCodeToStatus(result.exitCode);
|
|
2169
|
+
lines.push(`Protocol: ${result.protocol}`);
|
|
2170
|
+
lines.push(`Status: ${status}`);
|
|
2171
|
+
lines.push(`Duration: ${Math.round(result.duration / 1e3)}s`);
|
|
2172
|
+
lines.push(`Tokens: ${result.totalTokens}`);
|
|
2173
|
+
if (result.phases.length > 0) {
|
|
2174
|
+
lines.push("");
|
|
2175
|
+
lines.push("Phase Status Gate Tokens");
|
|
2176
|
+
lines.push("---------------- ----------- ---------------- ------");
|
|
2177
|
+
for (const phase of result.phases) {
|
|
2178
|
+
const name = phase.name.padEnd(16);
|
|
2179
|
+
const phaseStatus = phase.status.padEnd(11);
|
|
2180
|
+
const gate = (phase.gate_result ?? "-").padEnd(16);
|
|
2181
|
+
lines.push(`${name} ${phaseStatus} ${gate} ${phase.tokens}`);
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
if (result.errors.length > 0) {
|
|
2185
|
+
lines.push("");
|
|
2186
|
+
lines.push("Errors:");
|
|
2187
|
+
for (const err of result.errors) {
|
|
2188
|
+
lines.push(` - ${err}`);
|
|
2189
|
+
}
|
|
2190
|
+
}
|
|
2191
|
+
return lines.join("\n");
|
|
2192
|
+
}
|
|
2193
|
+
|
|
2194
|
+
// src/commands/run.ts
|
|
2195
|
+
var runCommand = defineCommand9({
|
|
2196
|
+
meta: {
|
|
2197
|
+
name: "run",
|
|
2198
|
+
description: "Run a SNIPER protocol in headless mode (for CI/CD)"
|
|
2199
|
+
},
|
|
2200
|
+
args: {
|
|
2201
|
+
protocol: {
|
|
2202
|
+
type: "string",
|
|
2203
|
+
description: "Protocol to run (full, feature, patch, ingest, explore, refactor, hotfix)",
|
|
2204
|
+
required: true
|
|
2205
|
+
},
|
|
2206
|
+
ci: {
|
|
2207
|
+
type: "boolean",
|
|
2208
|
+
description: "CI mode: sets auto-approve, json output, warn-level logging",
|
|
2209
|
+
default: false
|
|
2210
|
+
},
|
|
2211
|
+
"auto-approve": {
|
|
2212
|
+
type: "boolean",
|
|
2213
|
+
description: "Auto-approve all gates",
|
|
2214
|
+
default: false
|
|
2215
|
+
},
|
|
2216
|
+
output: {
|
|
2217
|
+
type: "string",
|
|
2218
|
+
description: "Output format: json, yaml, text",
|
|
2219
|
+
default: "text"
|
|
2220
|
+
},
|
|
2221
|
+
timeout: {
|
|
2222
|
+
type: "string",
|
|
2223
|
+
description: "Timeout in minutes",
|
|
2224
|
+
default: "60"
|
|
2225
|
+
}
|
|
2226
|
+
},
|
|
2227
|
+
run: async ({ args }) => {
|
|
2228
|
+
const cwd = process.cwd();
|
|
2229
|
+
if (!await sniperConfigExists(cwd)) {
|
|
2230
|
+
p9.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
2231
|
+
process.exit(4 /* ConfigError */);
|
|
2232
|
+
}
|
|
2233
|
+
const validFormats = ["json", "yaml", "text"];
|
|
2234
|
+
const outputFormat = args.ci ? "json" : args.output ?? "text";
|
|
2235
|
+
if (!validFormats.includes(outputFormat)) {
|
|
2236
|
+
p9.log.error(
|
|
2237
|
+
`Invalid output format: "${outputFormat}". Use: ${validFormats.join(", ")}`
|
|
2238
|
+
);
|
|
2239
|
+
process.exit(4 /* ConfigError */);
|
|
2240
|
+
}
|
|
2241
|
+
const options = {
|
|
2242
|
+
protocol: args.protocol,
|
|
2243
|
+
autoApproveGates: args.ci || args["auto-approve"] || false,
|
|
2244
|
+
outputFormat,
|
|
2245
|
+
logLevel: args.ci ? "warn" : "info",
|
|
2246
|
+
timeoutMinutes: parseInt(args.timeout, 10) || 60,
|
|
2247
|
+
failOnGateFailure: true
|
|
2248
|
+
};
|
|
2249
|
+
const runner = new HeadlessRunner(cwd, options);
|
|
2250
|
+
const result = await runner.run();
|
|
2251
|
+
const output = runner.formatOutput(result);
|
|
2252
|
+
if (result.exitCode === 0 /* Success */) {
|
|
2253
|
+
process.stdout.write(output + "\n");
|
|
2254
|
+
} else {
|
|
2255
|
+
if (result.errors.length > 0) {
|
|
2256
|
+
for (const err of result.errors) {
|
|
2257
|
+
process.stderr.write(`error: ${err}
|
|
2258
|
+
`);
|
|
2259
|
+
}
|
|
2260
|
+
}
|
|
2261
|
+
process.stdout.write(output + "\n");
|
|
2262
|
+
}
|
|
2263
|
+
process.exit(result.exitCode);
|
|
2264
|
+
}
|
|
2265
|
+
});
|
|
2266
|
+
|
|
2267
|
+
// src/commands/marketplace.ts
|
|
2268
|
+
import { defineCommand as defineCommand10 } from "citty";
|
|
2269
|
+
import * as p10 from "@clack/prompts";
|
|
2270
|
+
|
|
2271
|
+
// src/marketplace-client.ts
|
|
2272
|
+
import { readFile as readFile11 } from "fs/promises";
|
|
2273
|
+
import { join as join12 } from "path";
|
|
2274
|
+
function inferSniperType(sniper) {
|
|
2275
|
+
if (!sniper?.type) return null;
|
|
2276
|
+
const t = sniper.type;
|
|
2277
|
+
if (t === "plugin" || t === "agent" || t === "mixin" || t === "pack") {
|
|
2278
|
+
return t;
|
|
2279
|
+
}
|
|
2280
|
+
return null;
|
|
2281
|
+
}
|
|
2282
|
+
async function searchPackages(query, limit) {
|
|
2283
|
+
const size = limit || 20;
|
|
2284
|
+
const url = `https://registry.npmjs.org/-/v1/search?text=${encodeURIComponent(query)}+keywords:sniper&size=${size}`;
|
|
2285
|
+
const resp = await fetch(url);
|
|
2286
|
+
if (!resp.ok) {
|
|
2287
|
+
throw new Error(`npm registry search failed: ${resp.status}`);
|
|
2288
|
+
}
|
|
2289
|
+
const data = await resp.json();
|
|
2290
|
+
const results = await Promise.all(
|
|
2291
|
+
data.objects.map((obj) => getPackageInfo(obj.package.name))
|
|
2292
|
+
);
|
|
2293
|
+
const packages = results.filter(
|
|
2294
|
+
(info) => info !== null
|
|
2295
|
+
);
|
|
2296
|
+
return { packages, total: packages.length };
|
|
2297
|
+
}
|
|
2298
|
+
async function getPackageInfo(name) {
|
|
2299
|
+
const url = `https://registry.npmjs.org/${encodeURIComponent(name)}`;
|
|
2300
|
+
const resp = await fetch(url);
|
|
2301
|
+
if (!resp.ok) {
|
|
2302
|
+
if (resp.status === 404) return null;
|
|
2303
|
+
throw new Error(`npm registry fetch failed: ${resp.status}`);
|
|
2304
|
+
}
|
|
2305
|
+
const data = await resp.json();
|
|
2306
|
+
const latest = data["dist-tags"]?.latest;
|
|
2307
|
+
if (!latest || !data.versions?.[latest]) return null;
|
|
2308
|
+
const version2 = data.versions[latest];
|
|
2309
|
+
const sniperType = inferSniperType(version2.sniper);
|
|
2310
|
+
if (!sniperType) return null;
|
|
2311
|
+
return {
|
|
2312
|
+
name: data.name,
|
|
2313
|
+
version: latest,
|
|
2314
|
+
description: version2.description || "",
|
|
2315
|
+
sniperType,
|
|
2316
|
+
tags: version2.keywords || [],
|
|
2317
|
+
author: version2.author?.name
|
|
2318
|
+
};
|
|
2319
|
+
}
|
|
2320
|
+
async function validatePublishable(cwd) {
|
|
2321
|
+
const errors = [];
|
|
2322
|
+
let pkgJson;
|
|
2323
|
+
try {
|
|
2324
|
+
const raw = await readFile11(join12(cwd, "package.json"), "utf-8");
|
|
2325
|
+
pkgJson = JSON.parse(raw);
|
|
2326
|
+
} catch {
|
|
2327
|
+
return { valid: false, errors: ["No package.json found in current directory"] };
|
|
2328
|
+
}
|
|
2329
|
+
if (!pkgJson.name) {
|
|
2330
|
+
errors.push("package.json is missing a 'name' field");
|
|
2331
|
+
} else if (!pkgJson.name.startsWith("@sniper.ai/") && !pkgJson.name.startsWith("sniper-")) {
|
|
2332
|
+
errors.push(
|
|
2333
|
+
`Package name "${pkgJson.name}" must start with @sniper.ai/ or sniper-`
|
|
2334
|
+
);
|
|
2335
|
+
}
|
|
2336
|
+
if (!pkgJson.sniper?.type) {
|
|
2337
|
+
errors.push("package.json is missing 'sniper.type' field");
|
|
2338
|
+
}
|
|
2339
|
+
if (pkgJson.sniper?.type === "plugin") {
|
|
2340
|
+
try {
|
|
2341
|
+
await readFile11(join12(cwd, "plugin.yaml"), "utf-8");
|
|
2342
|
+
} catch {
|
|
2343
|
+
errors.push("Plugins require a plugin.yaml file in the package root");
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
return { valid: errors.length === 0, errors };
|
|
2347
|
+
}
|
|
2348
|
+
|
|
2349
|
+
// src/commands/marketplace.ts
|
|
2350
|
+
var searchSubcommand = defineCommand10({
|
|
2351
|
+
meta: {
|
|
2352
|
+
name: "search",
|
|
2353
|
+
description: "Search the SNIPER marketplace for packages"
|
|
2354
|
+
},
|
|
2355
|
+
args: {
|
|
2356
|
+
query: {
|
|
2357
|
+
type: "positional",
|
|
2358
|
+
description: "Search query",
|
|
2359
|
+
required: true
|
|
2360
|
+
}
|
|
2361
|
+
},
|
|
2362
|
+
run: async ({ args }) => {
|
|
2363
|
+
const s = p10.spinner();
|
|
2364
|
+
s.start("Searching marketplace...");
|
|
2365
|
+
try {
|
|
2366
|
+
const result = await searchPackages(args.query);
|
|
2367
|
+
s.stop("Done!");
|
|
2368
|
+
if (result.packages.length === 0) {
|
|
2369
|
+
p10.log.info("No packages found.");
|
|
2370
|
+
return;
|
|
2371
|
+
}
|
|
2372
|
+
p10.log.step(`Found ${result.total} result(s):`);
|
|
2373
|
+
console.log(
|
|
2374
|
+
` ${"Name".padEnd(35)} ${"Version".padEnd(10)} ${"Type".padEnd(8)} Description`
|
|
2375
|
+
);
|
|
2376
|
+
console.log(` ${"\u2500".repeat(35)} ${"\u2500".repeat(10)} ${"\u2500".repeat(8)} ${"\u2500".repeat(30)}`);
|
|
2377
|
+
for (const pkg of result.packages) {
|
|
2378
|
+
const desc = pkg.description.length > 40 ? pkg.description.slice(0, 37) + "..." : pkg.description;
|
|
2379
|
+
console.log(
|
|
2380
|
+
` ${pkg.name.padEnd(35)} ${pkg.version.padEnd(10)} ${pkg.sniperType.padEnd(8)} ${desc}`
|
|
2381
|
+
);
|
|
2382
|
+
}
|
|
2383
|
+
} catch (err) {
|
|
2384
|
+
s.stop("Failed!");
|
|
2385
|
+
p10.log.error(`Search failed: ${err}`);
|
|
2386
|
+
process.exit(1);
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2389
|
+
});
|
|
2390
|
+
var installSubcommand2 = defineCommand10({
|
|
2391
|
+
meta: {
|
|
2392
|
+
name: "install",
|
|
2393
|
+
description: "Install a package from the SNIPER marketplace"
|
|
2394
|
+
},
|
|
2395
|
+
args: {
|
|
2396
|
+
package: {
|
|
2397
|
+
type: "positional",
|
|
2398
|
+
description: "Package name to install",
|
|
2399
|
+
required: true
|
|
2400
|
+
}
|
|
2401
|
+
},
|
|
2402
|
+
run: async ({ args }) => {
|
|
2403
|
+
const cwd = process.cwd();
|
|
2404
|
+
if (!await sniperConfigExists(cwd)) {
|
|
2405
|
+
p10.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
2406
|
+
process.exit(1);
|
|
2407
|
+
}
|
|
2408
|
+
const s = p10.spinner();
|
|
2409
|
+
s.start(`Checking ${args.package}...`);
|
|
2410
|
+
try {
|
|
2411
|
+
const info = await getPackageInfo(args.package);
|
|
2412
|
+
if (!info) {
|
|
2413
|
+
s.stop("Failed!");
|
|
2414
|
+
p10.log.error(
|
|
2415
|
+
`${args.package} is not a valid SNIPER package (not found or missing sniper metadata).`
|
|
2416
|
+
);
|
|
2417
|
+
process.exit(1);
|
|
2418
|
+
}
|
|
2419
|
+
s.message(`Installing ${args.package}...`);
|
|
2420
|
+
const result = await installPlugin(args.package, cwd);
|
|
2421
|
+
s.stop("Done!");
|
|
2422
|
+
p10.log.success(
|
|
2423
|
+
`Installed ${info.sniperType}: ${result.name} v${result.version}`
|
|
2424
|
+
);
|
|
2425
|
+
} catch (err) {
|
|
2426
|
+
s.stop("Failed!");
|
|
2427
|
+
p10.log.error(`Installation failed: ${err}`);
|
|
2428
|
+
process.exit(1);
|
|
2429
|
+
}
|
|
2430
|
+
}
|
|
2431
|
+
});
|
|
2432
|
+
var infoSubcommand = defineCommand10({
|
|
2433
|
+
meta: {
|
|
2434
|
+
name: "info",
|
|
2435
|
+
description: "Show details about a SNIPER marketplace package"
|
|
2436
|
+
},
|
|
2437
|
+
args: {
|
|
2438
|
+
package: {
|
|
2439
|
+
type: "positional",
|
|
2440
|
+
description: "Package name to inspect",
|
|
2441
|
+
required: true
|
|
2442
|
+
}
|
|
2443
|
+
},
|
|
2444
|
+
run: async ({ args }) => {
|
|
2445
|
+
const s = p10.spinner();
|
|
2446
|
+
s.start(`Fetching info for ${args.package}...`);
|
|
2447
|
+
try {
|
|
2448
|
+
const info = await getPackageInfo(args.package);
|
|
2449
|
+
s.stop("Done!");
|
|
2450
|
+
if (!info) {
|
|
2451
|
+
p10.log.error(
|
|
2452
|
+
`${args.package} not found or is not a SNIPER package.`
|
|
2453
|
+
);
|
|
2454
|
+
process.exit(1);
|
|
2455
|
+
}
|
|
2456
|
+
p10.log.step(`Package: ${info.name}`);
|
|
2457
|
+
console.log(` Version: ${info.version}`);
|
|
2458
|
+
console.log(` Type: ${info.sniperType}`);
|
|
2459
|
+
console.log(` Description: ${info.description || "(none)"}`);
|
|
2460
|
+
console.log(` Tags: ${info.tags.length > 0 ? info.tags.join(", ") : "(none)"}`);
|
|
2461
|
+
console.log(` Author: ${info.author || "(unknown)"}`);
|
|
2462
|
+
} catch (err) {
|
|
2463
|
+
s.stop("Failed!");
|
|
2464
|
+
p10.log.error(`Fetch failed: ${err}`);
|
|
2465
|
+
process.exit(1);
|
|
2466
|
+
}
|
|
2467
|
+
}
|
|
2468
|
+
});
|
|
2469
|
+
var publishSubcommand = defineCommand10({
|
|
2470
|
+
meta: {
|
|
2471
|
+
name: "publish",
|
|
2472
|
+
description: "Validate and guide publishing a SNIPER package"
|
|
2473
|
+
},
|
|
2474
|
+
run: async () => {
|
|
2475
|
+
const cwd = process.cwd();
|
|
2476
|
+
const s = p10.spinner();
|
|
2477
|
+
s.start("Validating package...");
|
|
2478
|
+
try {
|
|
2479
|
+
const result = await validatePublishable(cwd);
|
|
2480
|
+
s.stop("Done!");
|
|
2481
|
+
if (!result.valid) {
|
|
2482
|
+
p10.log.error("Package is not publishable:");
|
|
2483
|
+
for (const err of result.errors) {
|
|
2484
|
+
console.log(` - ${err}`);
|
|
2485
|
+
}
|
|
2486
|
+
process.exit(1);
|
|
2487
|
+
}
|
|
2488
|
+
p10.log.success("Package is valid and ready to publish.");
|
|
2489
|
+
p10.log.info('Run "npm publish" to publish your package to the marketplace.');
|
|
2490
|
+
} catch (err) {
|
|
2491
|
+
s.stop("Failed!");
|
|
2492
|
+
p10.log.error(`Validation failed: ${err}`);
|
|
2493
|
+
process.exit(1);
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
});
|
|
2497
|
+
var marketplaceCommand = defineCommand10({
|
|
2498
|
+
meta: {
|
|
2499
|
+
name: "marketplace",
|
|
2500
|
+
description: "Browse and manage SNIPER marketplace packages"
|
|
2501
|
+
},
|
|
2502
|
+
subCommands: {
|
|
2503
|
+
search: searchSubcommand,
|
|
2504
|
+
install: installSubcommand2,
|
|
2505
|
+
info: infoSubcommand,
|
|
2506
|
+
publish: publishSubcommand
|
|
2507
|
+
}
|
|
2508
|
+
});
|
|
2509
|
+
|
|
2510
|
+
// src/commands/signal.ts
|
|
2511
|
+
import { defineCommand as defineCommand11 } from "citty";
|
|
2512
|
+
import * as p11 from "@clack/prompts";
|
|
2513
|
+
|
|
2514
|
+
// src/signal-collector.ts
|
|
2515
|
+
import { readFile as readFile12, writeFile as writeFile6, readdir as readdir6, rm as rm2 } from "fs/promises";
|
|
2516
|
+
import { join as join13 } from "path";
|
|
2517
|
+
import { execFileSync as execFileSync3 } from "child_process";
|
|
2518
|
+
import YAML10 from "yaml";
|
|
2519
|
+
var SIGNAL_DIR = ".sniper/memory/signals";
|
|
2520
|
+
function getSignalDir(cwd) {
|
|
2521
|
+
return join13(cwd, SIGNAL_DIR);
|
|
2522
|
+
}
|
|
2523
|
+
function signalFilename(signal) {
|
|
2524
|
+
const date = new Date(signal.timestamp);
|
|
2525
|
+
const dateStr = date.toISOString().slice(0, 10).replace(/-/g, "");
|
|
2526
|
+
const ts = Math.floor(date.getTime() / 1e3);
|
|
2527
|
+
return `${dateStr}-${signal.type}-${ts}.yaml`;
|
|
2528
|
+
}
|
|
2529
|
+
async function ingestSignal(cwd, signal) {
|
|
2530
|
+
const dir = getSignalDir(cwd);
|
|
2531
|
+
await ensureDir2(dir);
|
|
2532
|
+
const filename = signalFilename(signal);
|
|
2533
|
+
const filepath = join13(dir, filename);
|
|
2534
|
+
const content = YAML10.stringify(signal, { lineWidth: 0 });
|
|
2535
|
+
await writeFile6(filepath, content, "utf-8");
|
|
2536
|
+
return filename;
|
|
2537
|
+
}
|
|
2538
|
+
function assertGhAvailable() {
|
|
2539
|
+
try {
|
|
2540
|
+
execFileSync3("gh", ["--version"], { stdio: "pipe" });
|
|
2541
|
+
} catch {
|
|
2542
|
+
throw new Error(
|
|
2543
|
+
"GitHub CLI (gh) is not installed or not on PATH. Install it from https://cli.github.com/"
|
|
2544
|
+
);
|
|
2545
|
+
}
|
|
2546
|
+
}
|
|
2547
|
+
async function ingestFromPR(cwd, prNumber) {
|
|
2548
|
+
assertGhAvailable();
|
|
2549
|
+
const raw = execFileSync3("gh", [
|
|
2550
|
+
"pr",
|
|
2551
|
+
"view",
|
|
2552
|
+
prNumber.toString(),
|
|
2553
|
+
"--json",
|
|
2554
|
+
"comments,reviews,title"
|
|
2555
|
+
], { cwd, encoding: "utf-8" });
|
|
2556
|
+
const prData = JSON.parse(raw);
|
|
2557
|
+
const signals = [];
|
|
2558
|
+
for (const review of prData.reviews) {
|
|
2559
|
+
if (!review.body) continue;
|
|
2560
|
+
const signal = {
|
|
2561
|
+
type: "pr_review_comment",
|
|
2562
|
+
source: `pr-${prNumber}`,
|
|
2563
|
+
timestamp: review.submittedAt,
|
|
2564
|
+
summary: `PR #${prNumber} review (${review.state}) by ${review.author?.login ?? "unknown"}`,
|
|
2565
|
+
details: review.body,
|
|
2566
|
+
relevance_tags: ["pr-review", review.state.toLowerCase()]
|
|
2567
|
+
};
|
|
2568
|
+
const filename = await ingestSignal(cwd, signal);
|
|
2569
|
+
signals.push(signal);
|
|
2570
|
+
}
|
|
2571
|
+
for (const comment of prData.comments) {
|
|
2572
|
+
const signal = {
|
|
2573
|
+
type: "pr_review_comment",
|
|
2574
|
+
source: `pr-${prNumber}`,
|
|
2575
|
+
timestamp: comment.createdAt,
|
|
2576
|
+
summary: `PR #${prNumber} comment by ${comment.author?.login ?? "unknown"}`,
|
|
2577
|
+
details: comment.body,
|
|
2578
|
+
relevance_tags: ["pr-comment"]
|
|
2579
|
+
};
|
|
2580
|
+
await ingestSignal(cwd, signal);
|
|
2581
|
+
signals.push(signal);
|
|
2582
|
+
}
|
|
2583
|
+
return signals;
|
|
2584
|
+
}
|
|
2585
|
+
async function listSignals(cwd, options) {
|
|
2586
|
+
const dir = getSignalDir(cwd);
|
|
2587
|
+
if (!await pathExists(dir)) {
|
|
2588
|
+
return [];
|
|
2589
|
+
}
|
|
2590
|
+
const files = await readdir6(dir);
|
|
2591
|
+
const yamlFiles = files.filter((f) => f.endsWith(".yaml"));
|
|
2592
|
+
const signals = [];
|
|
2593
|
+
for (const file of yamlFiles) {
|
|
2594
|
+
const raw = await readFile12(join13(dir, file), "utf-8");
|
|
2595
|
+
const signal = YAML10.parse(raw);
|
|
2596
|
+
signals.push(signal);
|
|
2597
|
+
}
|
|
2598
|
+
let filtered = signals;
|
|
2599
|
+
if (options?.type) {
|
|
2600
|
+
filtered = filtered.filter((s) => s.type === options.type);
|
|
2601
|
+
}
|
|
2602
|
+
filtered.sort(
|
|
2603
|
+
(a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
|
|
2604
|
+
);
|
|
2605
|
+
if (options?.limit) {
|
|
2606
|
+
filtered = filtered.slice(0, options.limit);
|
|
2607
|
+
}
|
|
2608
|
+
return filtered;
|
|
2609
|
+
}
|
|
2610
|
+
async function clearSignals(cwd) {
|
|
2611
|
+
const dir = getSignalDir(cwd);
|
|
2612
|
+
if (!await pathExists(dir)) {
|
|
2613
|
+
return 0;
|
|
2614
|
+
}
|
|
2615
|
+
const files = await readdir6(dir);
|
|
2616
|
+
const yamlFiles = files.filter((f) => f.endsWith(".yaml") || f.endsWith(".yml"));
|
|
2617
|
+
for (const file of yamlFiles) {
|
|
2618
|
+
await rm2(join13(dir, file));
|
|
2619
|
+
}
|
|
2620
|
+
return yamlFiles.length;
|
|
2621
|
+
}
|
|
2622
|
+
|
|
2623
|
+
// src/commands/signal.ts
|
|
2624
|
+
var SIGNAL_TYPES = [
|
|
2625
|
+
{ value: "ci_failure", label: "CI Failure" },
|
|
2626
|
+
{ value: "pr_review_comment", label: "PR Review Comment" },
|
|
2627
|
+
{ value: "production_error", label: "Production Error" },
|
|
2628
|
+
{ value: "manual", label: "Manual" }
|
|
2629
|
+
];
|
|
2630
|
+
var ingestSubcommand = defineCommand11({
|
|
2631
|
+
meta: {
|
|
2632
|
+
name: "ingest",
|
|
2633
|
+
description: "Interactively create a new signal record"
|
|
2634
|
+
},
|
|
2635
|
+
run: async () => {
|
|
2636
|
+
const cwd = process.cwd();
|
|
2637
|
+
if (!await sniperConfigExists(cwd)) {
|
|
2638
|
+
p11.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
2639
|
+
process.exit(1);
|
|
2640
|
+
}
|
|
2641
|
+
const type = await p11.select({
|
|
2642
|
+
message: "Signal type:",
|
|
2643
|
+
options: SIGNAL_TYPES.map((t) => ({ value: t.value, label: t.label }))
|
|
2644
|
+
});
|
|
2645
|
+
if (p11.isCancel(type)) {
|
|
2646
|
+
p11.cancel("Cancelled.");
|
|
2647
|
+
process.exit(0);
|
|
2648
|
+
}
|
|
2649
|
+
const source = await p11.text({
|
|
2650
|
+
message: "Source (e.g., github-actions, pr-42, datadog):",
|
|
2651
|
+
validate: (v) => v.length === 0 ? "Source is required" : void 0
|
|
2652
|
+
});
|
|
2653
|
+
if (p11.isCancel(source)) {
|
|
2654
|
+
p11.cancel("Cancelled.");
|
|
2655
|
+
process.exit(0);
|
|
2656
|
+
}
|
|
2657
|
+
const summary = await p11.text({
|
|
2658
|
+
message: "Summary (one-line description):",
|
|
2659
|
+
validate: (v) => v.length === 0 ? "Summary is required" : void 0
|
|
2660
|
+
});
|
|
2661
|
+
if (p11.isCancel(summary)) {
|
|
2662
|
+
p11.cancel("Cancelled.");
|
|
2663
|
+
process.exit(0);
|
|
2664
|
+
}
|
|
2665
|
+
const details = await p11.text({
|
|
2666
|
+
message: "Details (optional \u2014 full error message or context):"
|
|
2667
|
+
});
|
|
2668
|
+
if (p11.isCancel(details)) {
|
|
2669
|
+
p11.cancel("Cancelled.");
|
|
2670
|
+
process.exit(0);
|
|
2671
|
+
}
|
|
2672
|
+
const filesInput = await p11.text({
|
|
2673
|
+
message: "Affected files (optional \u2014 comma-separated paths):"
|
|
2674
|
+
});
|
|
2675
|
+
if (p11.isCancel(filesInput)) {
|
|
2676
|
+
p11.cancel("Cancelled.");
|
|
2677
|
+
process.exit(0);
|
|
2678
|
+
}
|
|
2679
|
+
const signal = {
|
|
2680
|
+
type,
|
|
2681
|
+
source,
|
|
2682
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2683
|
+
summary
|
|
2684
|
+
};
|
|
2685
|
+
if (details && details.length > 0) {
|
|
2686
|
+
signal.details = details;
|
|
2687
|
+
}
|
|
2688
|
+
if (filesInput && filesInput.length > 0) {
|
|
2689
|
+
signal.affected_files = filesInput.split(",").map((f) => f.trim()).filter((f) => f.length > 0);
|
|
2690
|
+
}
|
|
2691
|
+
await ensureDir2(getSignalDir(cwd));
|
|
2692
|
+
const filename = await ingestSignal(cwd, signal);
|
|
2693
|
+
p11.log.success(`Signal captured: ${filename}`);
|
|
2694
|
+
}
|
|
2695
|
+
});
|
|
2696
|
+
var ingestPrSubcommand = defineCommand11({
|
|
2697
|
+
meta: {
|
|
2698
|
+
name: "ingest-pr",
|
|
2699
|
+
description: "Ingest signals from a GitHub PR's reviews and comments"
|
|
2700
|
+
},
|
|
2701
|
+
args: {
|
|
2702
|
+
"pr-number": {
|
|
2703
|
+
type: "positional",
|
|
2704
|
+
description: "Pull request number",
|
|
2705
|
+
required: true
|
|
2706
|
+
}
|
|
2707
|
+
},
|
|
2708
|
+
run: async ({ args }) => {
|
|
2709
|
+
const cwd = process.cwd();
|
|
2710
|
+
if (!await sniperConfigExists(cwd)) {
|
|
2711
|
+
p11.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
2712
|
+
process.exit(1);
|
|
2713
|
+
}
|
|
2714
|
+
const prNumber = parseInt(args["pr-number"], 10);
|
|
2715
|
+
if (isNaN(prNumber)) {
|
|
2716
|
+
p11.log.error("Invalid PR number.");
|
|
2717
|
+
process.exit(1);
|
|
2718
|
+
}
|
|
2719
|
+
const s = p11.spinner();
|
|
2720
|
+
s.start(`Ingesting signals from PR #${prNumber}...`);
|
|
2721
|
+
try {
|
|
2722
|
+
const signals = await ingestFromPR(cwd, prNumber);
|
|
2723
|
+
s.stop("Done!");
|
|
2724
|
+
p11.log.success(`Captured ${signals.length} signal(s) from PR #${prNumber}`);
|
|
2725
|
+
for (const signal of signals) {
|
|
2726
|
+
p11.log.info(` - ${signal.summary}`);
|
|
2727
|
+
}
|
|
2728
|
+
} catch (err) {
|
|
2729
|
+
s.stop("Failed!");
|
|
2730
|
+
p11.log.error(`Failed to ingest PR signals: ${err}`);
|
|
2731
|
+
process.exit(1);
|
|
2732
|
+
}
|
|
2733
|
+
}
|
|
2734
|
+
});
|
|
2735
|
+
var listSubcommand3 = defineCommand11({
|
|
842
2736
|
meta: {
|
|
843
|
-
name: "
|
|
844
|
-
description: "
|
|
2737
|
+
name: "list",
|
|
2738
|
+
description: "List captured signals"
|
|
845
2739
|
},
|
|
846
2740
|
args: {
|
|
847
|
-
action: {
|
|
848
|
-
type: "positional",
|
|
849
|
-
description: "Action to perform: list, add, remove, promote, export, import",
|
|
850
|
-
required: false
|
|
851
|
-
},
|
|
852
2741
|
type: {
|
|
853
|
-
type: "
|
|
854
|
-
description: "
|
|
2742
|
+
type: "string",
|
|
2743
|
+
description: "Filter by signal type",
|
|
855
2744
|
required: false
|
|
856
2745
|
},
|
|
857
|
-
|
|
858
|
-
type: "
|
|
859
|
-
description: "
|
|
860
|
-
required: false
|
|
2746
|
+
limit: {
|
|
2747
|
+
type: "string",
|
|
2748
|
+
description: "Maximum number of signals to display",
|
|
2749
|
+
required: false,
|
|
2750
|
+
default: "20"
|
|
861
2751
|
}
|
|
862
2752
|
},
|
|
863
2753
|
run: async ({ args }) => {
|
|
864
2754
|
const cwd = process.cwd();
|
|
865
2755
|
if (!await sniperConfigExists(cwd)) {
|
|
866
|
-
|
|
867
|
-
'SNIPER is not initialized in this directory. Run "sniper init" first.'
|
|
868
|
-
);
|
|
2756
|
+
p11.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
869
2757
|
process.exit(1);
|
|
870
2758
|
}
|
|
871
|
-
const
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
);
|
|
880
|
-
await writeFile3(
|
|
881
|
-
join4(memoryDir, "anti-patterns.yaml"),
|
|
882
|
-
"anti_patterns: []\n",
|
|
883
|
-
"utf-8"
|
|
884
|
-
);
|
|
885
|
-
await writeFile3(
|
|
886
|
-
join4(memoryDir, "decisions.yaml"),
|
|
887
|
-
"decisions: []\n",
|
|
888
|
-
"utf-8"
|
|
889
|
-
);
|
|
890
|
-
await writeFile3(
|
|
891
|
-
join4(memoryDir, "estimates.yaml"),
|
|
892
|
-
"calibration:\n velocity_factor: 1.0\n common_underestimates: []\n last_updated: null\n sprints_analyzed: 0\n",
|
|
893
|
-
"utf-8"
|
|
894
|
-
);
|
|
895
|
-
p7.log.info("Initialized .sniper/memory/ directory");
|
|
2759
|
+
const limit = parseInt(args.limit, 10) || 20;
|
|
2760
|
+
const signals = await listSignals(cwd, {
|
|
2761
|
+
type: args.type,
|
|
2762
|
+
limit
|
|
2763
|
+
});
|
|
2764
|
+
if (signals.length === 0) {
|
|
2765
|
+
p11.log.info("No signals found.");
|
|
2766
|
+
return;
|
|
896
2767
|
}
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
const antiPatterns = await readYamlArray(
|
|
902
|
-
join4(memoryDir, "anti-patterns.yaml"),
|
|
903
|
-
"anti_patterns"
|
|
2768
|
+
p11.log.step(`Signals (${signals.length}):`);
|
|
2769
|
+
console.log();
|
|
2770
|
+
console.log(
|
|
2771
|
+
" " + "Type".padEnd(22) + "Source".padEnd(20) + "Timestamp".padEnd(22) + "Summary"
|
|
904
2772
|
);
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
"
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
const retrosDir = join4(memoryDir, "retros");
|
|
911
|
-
if (await pathExists2(retrosDir)) {
|
|
912
|
-
const files = await readdir3(retrosDir);
|
|
913
|
-
retroCount = files.filter((f) => f.endsWith(".yaml")).length;
|
|
914
|
-
}
|
|
915
|
-
const action = args.action;
|
|
916
|
-
if (!action || action === "list") {
|
|
917
|
-
p7.intro("SNIPER Memory");
|
|
918
|
-
const confirmedConv = conventions.filter(
|
|
919
|
-
(c) => c.status !== "candidate"
|
|
920
|
-
).length;
|
|
921
|
-
const candidateConv = conventions.filter(
|
|
922
|
-
(c) => c.status === "candidate"
|
|
923
|
-
).length;
|
|
924
|
-
const confirmedAp = antiPatterns.filter(
|
|
925
|
-
(a) => a.status !== "candidate"
|
|
926
|
-
).length;
|
|
927
|
-
const candidateAp = antiPatterns.filter(
|
|
928
|
-
(a) => a.status === "candidate"
|
|
929
|
-
).length;
|
|
930
|
-
const activeDecisions = decisions.filter(
|
|
931
|
-
(d) => d.status === "active" || !d.status
|
|
932
|
-
).length;
|
|
933
|
-
const supersededDecisions = decisions.filter(
|
|
934
|
-
(d) => d.status === "superseded"
|
|
935
|
-
).length;
|
|
936
|
-
p7.log.info(
|
|
937
|
-
`Conventions: ${confirmedConv} confirmed, ${candidateConv} candidates`
|
|
938
|
-
);
|
|
939
|
-
p7.log.info(
|
|
940
|
-
`Anti-Patterns: ${confirmedAp} confirmed, ${candidateAp} candidates`
|
|
2773
|
+
console.log(" " + "-".repeat(90));
|
|
2774
|
+
for (const signal of signals) {
|
|
2775
|
+
const ts = new Date(signal.timestamp).toISOString().slice(0, 19).replace("T", " ");
|
|
2776
|
+
console.log(
|
|
2777
|
+
" " + signal.type.padEnd(22) + signal.source.padEnd(20) + ts.padEnd(22) + signal.summary.slice(0, 50)
|
|
941
2778
|
);
|
|
942
|
-
p7.log.info(
|
|
943
|
-
`Decisions: ${activeDecisions} active, ${supersededDecisions} superseded`
|
|
944
|
-
);
|
|
945
|
-
p7.log.info(`Retrospectives: ${retroCount}`);
|
|
946
|
-
const config = await readConfig(cwd);
|
|
947
|
-
if (config.workspace?.enabled && config.workspace.workspace_path) {
|
|
948
|
-
const wsMemory = join4(
|
|
949
|
-
cwd,
|
|
950
|
-
config.workspace.workspace_path,
|
|
951
|
-
"memory"
|
|
952
|
-
);
|
|
953
|
-
if (await pathExists2(wsMemory)) {
|
|
954
|
-
const wsConv = await readYamlArray(
|
|
955
|
-
join4(wsMemory, "conventions.yaml"),
|
|
956
|
-
"conventions"
|
|
957
|
-
);
|
|
958
|
-
const wsAp = await readYamlArray(
|
|
959
|
-
join4(wsMemory, "anti-patterns.yaml"),
|
|
960
|
-
"anti_patterns"
|
|
961
|
-
);
|
|
962
|
-
const wsDec = await readYamlArray(
|
|
963
|
-
join4(wsMemory, "decisions.yaml"),
|
|
964
|
-
"decisions"
|
|
965
|
-
);
|
|
966
|
-
p7.log.step("Workspace Memory:");
|
|
967
|
-
p7.log.info(` Conventions: ${wsConv.length}`);
|
|
968
|
-
p7.log.info(` Anti-Patterns: ${wsAp.length}`);
|
|
969
|
-
p7.log.info(` Decisions: ${wsDec.length}`);
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
p7.outro("");
|
|
973
|
-
return;
|
|
974
2779
|
}
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
conventions.push({
|
|
988
|
-
id,
|
|
989
|
-
rule: value,
|
|
990
|
-
rationale: "",
|
|
991
|
-
source: { type: "manual", ref: "user-added", date: today },
|
|
992
|
-
applies_to: [],
|
|
993
|
-
enforcement: "both",
|
|
994
|
-
scope: "project",
|
|
995
|
-
status: "confirmed",
|
|
996
|
-
examples: { positive: "", negative: "" }
|
|
997
|
-
});
|
|
998
|
-
await writeYamlArray(
|
|
999
|
-
join4(memoryDir, "conventions.yaml"),
|
|
1000
|
-
"conventions",
|
|
1001
|
-
conventions
|
|
1002
|
-
);
|
|
1003
|
-
p7.log.success(`Added convention ${id}: ${value}`);
|
|
1004
|
-
} else if (type === "anti-pattern") {
|
|
1005
|
-
const id = nextId(antiPatterns, "ap");
|
|
1006
|
-
antiPatterns.push({
|
|
1007
|
-
id,
|
|
1008
|
-
description: value,
|
|
1009
|
-
why_bad: "",
|
|
1010
|
-
fix_pattern: "",
|
|
1011
|
-
source: { type: "manual", ref: "user-added", date: today },
|
|
1012
|
-
detection_hint: "",
|
|
1013
|
-
applies_to: [],
|
|
1014
|
-
severity: "medium",
|
|
1015
|
-
status: "confirmed"
|
|
1016
|
-
});
|
|
1017
|
-
await writeYamlArray(
|
|
1018
|
-
join4(memoryDir, "anti-patterns.yaml"),
|
|
1019
|
-
"anti_patterns",
|
|
1020
|
-
antiPatterns
|
|
1021
|
-
);
|
|
1022
|
-
p7.log.success(`Added anti-pattern ${id}: ${value}`);
|
|
1023
|
-
} else if (type === "decision") {
|
|
1024
|
-
const id = nextId(decisions, "dec");
|
|
1025
|
-
decisions.push({
|
|
1026
|
-
id,
|
|
1027
|
-
title: value,
|
|
1028
|
-
context: "",
|
|
1029
|
-
decision: value,
|
|
1030
|
-
alternatives_considered: [],
|
|
1031
|
-
source: { type: "manual", ref: "user-added", date: today },
|
|
1032
|
-
applies_to: [],
|
|
1033
|
-
status: "active",
|
|
1034
|
-
superseded_by: null
|
|
1035
|
-
});
|
|
1036
|
-
await writeYamlArray(
|
|
1037
|
-
join4(memoryDir, "decisions.yaml"),
|
|
1038
|
-
"decisions",
|
|
1039
|
-
decisions
|
|
1040
|
-
);
|
|
1041
|
-
p7.log.success(`Added decision ${id}: ${value}`);
|
|
1042
|
-
} else {
|
|
1043
|
-
p7.log.error(
|
|
1044
|
-
`Unknown memory type "${type}". Use: convention, anti-pattern, decision`
|
|
1045
|
-
);
|
|
1046
|
-
process.exit(1);
|
|
1047
|
-
}
|
|
1048
|
-
return;
|
|
2780
|
+
}
|
|
2781
|
+
});
|
|
2782
|
+
var clearSubcommand = defineCommand11({
|
|
2783
|
+
meta: {
|
|
2784
|
+
name: "clear",
|
|
2785
|
+
description: "Delete all captured signals"
|
|
2786
|
+
},
|
|
2787
|
+
run: async () => {
|
|
2788
|
+
const cwd = process.cwd();
|
|
2789
|
+
if (!await sniperConfigExists(cwd)) {
|
|
2790
|
+
p11.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
2791
|
+
process.exit(1);
|
|
1049
2792
|
}
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
let found = false;
|
|
1057
|
-
if (id.startsWith("conv-")) {
|
|
1058
|
-
const idx = conventions.findIndex((c) => c.id === id);
|
|
1059
|
-
if (idx >= 0) {
|
|
1060
|
-
conventions.splice(idx, 1);
|
|
1061
|
-
await writeYamlArray(
|
|
1062
|
-
join4(memoryDir, "conventions.yaml"),
|
|
1063
|
-
"conventions",
|
|
1064
|
-
conventions
|
|
1065
|
-
);
|
|
1066
|
-
found = true;
|
|
1067
|
-
}
|
|
1068
|
-
} else if (id.startsWith("ap-")) {
|
|
1069
|
-
const idx = antiPatterns.findIndex((a) => a.id === id);
|
|
1070
|
-
if (idx >= 0) {
|
|
1071
|
-
antiPatterns.splice(idx, 1);
|
|
1072
|
-
await writeYamlArray(
|
|
1073
|
-
join4(memoryDir, "anti-patterns.yaml"),
|
|
1074
|
-
"anti_patterns",
|
|
1075
|
-
antiPatterns
|
|
1076
|
-
);
|
|
1077
|
-
found = true;
|
|
1078
|
-
}
|
|
1079
|
-
} else if (id.startsWith("dec-")) {
|
|
1080
|
-
const idx = decisions.findIndex((d) => d.id === id);
|
|
1081
|
-
if (idx >= 0) {
|
|
1082
|
-
decisions.splice(idx, 1);
|
|
1083
|
-
await writeYamlArray(
|
|
1084
|
-
join4(memoryDir, "decisions.yaml"),
|
|
1085
|
-
"decisions",
|
|
1086
|
-
decisions
|
|
1087
|
-
);
|
|
1088
|
-
found = true;
|
|
1089
|
-
}
|
|
1090
|
-
}
|
|
1091
|
-
if (found) {
|
|
1092
|
-
p7.log.success(`Removed ${id}`);
|
|
1093
|
-
} else {
|
|
1094
|
-
p7.log.error(`Entry ${id} not found in memory.`);
|
|
1095
|
-
process.exit(1);
|
|
1096
|
-
}
|
|
1097
|
-
return;
|
|
2793
|
+
const confirm6 = await p11.confirm({
|
|
2794
|
+
message: "Delete all captured signals? This cannot be undone."
|
|
2795
|
+
});
|
|
2796
|
+
if (p11.isCancel(confirm6) || !confirm6) {
|
|
2797
|
+
p11.cancel("Cancelled.");
|
|
2798
|
+
process.exit(0);
|
|
1098
2799
|
}
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
join4(memoryDir, "decisions.yaml"),
|
|
1134
|
-
"decisions",
|
|
1135
|
-
decisions
|
|
1136
|
-
);
|
|
1137
|
-
found = true;
|
|
1138
|
-
}
|
|
1139
|
-
}
|
|
1140
|
-
if (found) {
|
|
1141
|
-
p7.log.success(`Promoted ${id} to confirmed/active`);
|
|
1142
|
-
} else {
|
|
1143
|
-
p7.log.error(
|
|
1144
|
-
`Entry ${id} not found or is not a candidate.`
|
|
1145
|
-
);
|
|
1146
|
-
process.exit(1);
|
|
1147
|
-
}
|
|
1148
|
-
return;
|
|
2800
|
+
const count = await clearSignals(cwd);
|
|
2801
|
+
p11.log.success(`Cleared ${count} signal(s).`);
|
|
2802
|
+
}
|
|
2803
|
+
});
|
|
2804
|
+
var signalCommand = defineCommand11({
|
|
2805
|
+
meta: {
|
|
2806
|
+
name: "signal",
|
|
2807
|
+
description: "Manage external signal learning"
|
|
2808
|
+
},
|
|
2809
|
+
subCommands: {
|
|
2810
|
+
ingest: ingestSubcommand,
|
|
2811
|
+
"ingest-pr": ingestPrSubcommand,
|
|
2812
|
+
list: listSubcommand3,
|
|
2813
|
+
clear: clearSubcommand
|
|
2814
|
+
}
|
|
2815
|
+
});
|
|
2816
|
+
|
|
2817
|
+
// src/commands/knowledge.ts
|
|
2818
|
+
import { defineCommand as defineCommand12 } from "citty";
|
|
2819
|
+
import * as p12 from "@clack/prompts";
|
|
2820
|
+
import { join as join14 } from "path";
|
|
2821
|
+
import { readFile as readFile13 } from "fs/promises";
|
|
2822
|
+
var KNOWLEDGE_DIR = ".sniper/knowledge";
|
|
2823
|
+
var INDEX_FILENAME = "knowledge-index.json";
|
|
2824
|
+
var indexSubcommand = defineCommand12({
|
|
2825
|
+
meta: {
|
|
2826
|
+
name: "index",
|
|
2827
|
+
description: "Index the SNIPER knowledge base"
|
|
2828
|
+
},
|
|
2829
|
+
run: async () => {
|
|
2830
|
+
const cwd = process.cwd();
|
|
2831
|
+
if (!await sniperConfigExists(cwd)) {
|
|
2832
|
+
p12.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
2833
|
+
process.exit(1);
|
|
1149
2834
|
}
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
conventions: conventions.map(({ id: _id, source: _src, ...rest }) => rest),
|
|
1156
|
-
anti_patterns: antiPatterns.map(
|
|
1157
|
-
({ id: _id, source: _src, ...rest }) => rest
|
|
1158
|
-
),
|
|
1159
|
-
decisions: decisions.map(({ id: _id, source: _src, ...rest }) => rest)
|
|
1160
|
-
};
|
|
1161
|
-
const exportPath = join4(cwd, "sniper-memory-export.yaml");
|
|
1162
|
-
await writeFile3(
|
|
1163
|
-
exportPath,
|
|
1164
|
-
YAML4.stringify(exportData, { lineWidth: 0 }),
|
|
1165
|
-
"utf-8"
|
|
2835
|
+
const knowledgeDir = join14(cwd, KNOWLEDGE_DIR);
|
|
2836
|
+
if (!await pathExists(knowledgeDir)) {
|
|
2837
|
+
p12.log.error(
|
|
2838
|
+
`Knowledge directory not found: ${KNOWLEDGE_DIR}
|
|
2839
|
+
Create it and add .md files to index.`
|
|
1166
2840
|
);
|
|
1167
|
-
|
|
1168
|
-
`Exported ${conventions.length} conventions, ${antiPatterns.length} anti-patterns, ${decisions.length} decisions to sniper-memory-export.yaml`
|
|
1169
|
-
);
|
|
1170
|
-
return;
|
|
2841
|
+
process.exit(1);
|
|
1171
2842
|
}
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
(c) => c.rule === conv.rule
|
|
1188
|
-
);
|
|
1189
|
-
if (exists) {
|
|
1190
|
-
skipped++;
|
|
1191
|
-
continue;
|
|
1192
|
-
}
|
|
1193
|
-
conventions.push({
|
|
1194
|
-
...conv,
|
|
1195
|
-
id: nextId(conventions, "conv"),
|
|
1196
|
-
source: { type: "imported", ref: filePath, date: today },
|
|
1197
|
-
status: "candidate"
|
|
1198
|
-
});
|
|
1199
|
-
addedConv++;
|
|
1200
|
-
}
|
|
1201
|
-
await writeYamlArray(
|
|
1202
|
-
join4(memoryDir, "conventions.yaml"),
|
|
1203
|
-
"conventions",
|
|
1204
|
-
conventions
|
|
1205
|
-
);
|
|
1206
|
-
}
|
|
1207
|
-
if (Array.isArray(imported.anti_patterns)) {
|
|
1208
|
-
for (const ap of imported.anti_patterns) {
|
|
1209
|
-
const exists = antiPatterns.some(
|
|
1210
|
-
(a) => a.description === ap.description
|
|
1211
|
-
);
|
|
1212
|
-
if (exists) {
|
|
1213
|
-
skipped++;
|
|
1214
|
-
continue;
|
|
1215
|
-
}
|
|
1216
|
-
antiPatterns.push({
|
|
1217
|
-
...ap,
|
|
1218
|
-
id: nextId(antiPatterns, "ap"),
|
|
1219
|
-
source: { type: "imported", ref: filePath, date: today },
|
|
1220
|
-
status: "candidate"
|
|
1221
|
-
});
|
|
1222
|
-
addedAp++;
|
|
1223
|
-
}
|
|
1224
|
-
await writeYamlArray(
|
|
1225
|
-
join4(memoryDir, "anti-patterns.yaml"),
|
|
1226
|
-
"anti_patterns",
|
|
1227
|
-
antiPatterns
|
|
1228
|
-
);
|
|
1229
|
-
}
|
|
1230
|
-
p7.log.success(
|
|
1231
|
-
`Imported ${addedConv} conventions, ${addedAp} anti-patterns (${skipped} skipped as duplicates)`
|
|
1232
|
-
);
|
|
1233
|
-
return;
|
|
2843
|
+
const s = p12.spinner();
|
|
2844
|
+
s.start("Indexing knowledge base...");
|
|
2845
|
+
try {
|
|
2846
|
+
const { indexKnowledgeDir, writeIndex } = await import("@sniper.ai/mcp-knowledge/indexer");
|
|
2847
|
+
const index = await indexKnowledgeDir(knowledgeDir);
|
|
2848
|
+
const indexPath = join14(knowledgeDir, INDEX_FILENAME);
|
|
2849
|
+
await writeIndex(indexPath, index);
|
|
2850
|
+
s.stop("Done!");
|
|
2851
|
+
p12.log.success(`Indexed ${index.entries.length} entries`);
|
|
2852
|
+
p12.log.info(`Total tokens: ${index.total_tokens.toLocaleString()}`);
|
|
2853
|
+
p12.log.info(`Index written to: ${KNOWLEDGE_DIR}/${INDEX_FILENAME}`);
|
|
2854
|
+
} catch (err) {
|
|
2855
|
+
s.stop("Failed!");
|
|
2856
|
+
p12.log.error(`Indexing failed: ${err}`);
|
|
2857
|
+
process.exit(1);
|
|
1234
2858
|
}
|
|
1235
|
-
p7.log.error(
|
|
1236
|
-
`Unknown action "${action}". Use: list, add, remove, promote, export, import`
|
|
1237
|
-
);
|
|
1238
|
-
process.exit(1);
|
|
1239
2859
|
}
|
|
1240
2860
|
});
|
|
1241
|
-
|
|
1242
|
-
// src/commands/workspace.ts
|
|
1243
|
-
import {
|
|
1244
|
-
readFile as readFile5,
|
|
1245
|
-
writeFile as writeFile4,
|
|
1246
|
-
readdir as readdir4,
|
|
1247
|
-
stat as stat2,
|
|
1248
|
-
symlink
|
|
1249
|
-
} from "fs/promises";
|
|
1250
|
-
import { join as join5, relative, resolve as resolve2 } from "path";
|
|
1251
|
-
import { defineCommand as defineCommand8 } from "citty";
|
|
1252
|
-
import * as p8 from "@clack/prompts";
|
|
1253
|
-
import YAML5 from "yaml";
|
|
1254
|
-
var initSubCommand = defineCommand8({
|
|
2861
|
+
var statusSubcommand2 = defineCommand12({
|
|
1255
2862
|
meta: {
|
|
1256
|
-
name: "
|
|
1257
|
-
description: "
|
|
2863
|
+
name: "status",
|
|
2864
|
+
description: "Show knowledge base status"
|
|
1258
2865
|
},
|
|
1259
2866
|
run: async () => {
|
|
1260
2867
|
const cwd = process.cwd();
|
|
1261
|
-
if (await
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
p8.log.warn(
|
|
1265
|
-
`A workspace already exists: ${ws.name} (${ws.repositories.length} repos)`
|
|
1266
|
-
);
|
|
1267
|
-
p8.log.info("Use /sniper-workspace status to view details.");
|
|
1268
|
-
process.exit(0);
|
|
1269
|
-
}
|
|
1270
|
-
p8.intro("Initialize SNIPER Workspace");
|
|
1271
|
-
const name = await p8.text({
|
|
1272
|
-
message: "Workspace name:",
|
|
1273
|
-
placeholder: "my-saas-platform"
|
|
1274
|
-
});
|
|
1275
|
-
if (p8.isCancel(name)) {
|
|
1276
|
-
p8.cancel("Aborted.");
|
|
1277
|
-
process.exit(0);
|
|
2868
|
+
if (!await sniperConfigExists(cwd)) {
|
|
2869
|
+
p12.log.error('SNIPER is not initialized. Run "sniper init" first.');
|
|
2870
|
+
process.exit(1);
|
|
1278
2871
|
}
|
|
1279
|
-
const
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
process.exit(0);
|
|
2872
|
+
const indexPath = join14(cwd, KNOWLEDGE_DIR, INDEX_FILENAME);
|
|
2873
|
+
if (!await pathExists(indexPath)) {
|
|
2874
|
+
p12.log.warn(
|
|
2875
|
+
'Knowledge base has not been indexed yet. Run "sniper knowledge index" first.'
|
|
2876
|
+
);
|
|
2877
|
+
return;
|
|
1286
2878
|
}
|
|
1287
|
-
const s = p8.spinner();
|
|
1288
|
-
s.start("Scanning for SNIPER-enabled repositories...");
|
|
1289
|
-
const parentDir = resolve2(cwd, "..");
|
|
1290
|
-
const repos = [];
|
|
1291
2879
|
try {
|
|
1292
|
-
const
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
consumes: []
|
|
1311
|
-
});
|
|
1312
|
-
} catch {
|
|
1313
|
-
}
|
|
2880
|
+
const raw = await readFile13(indexPath, "utf-8");
|
|
2881
|
+
const index = JSON.parse(raw);
|
|
2882
|
+
const topics = [...new Set(index.entries.map((e) => e.topic))];
|
|
2883
|
+
p12.log.step("Knowledge Base Status:");
|
|
2884
|
+
console.log(` Entries: ${index.entries.length}`);
|
|
2885
|
+
console.log(` Topics: ${topics.length}`);
|
|
2886
|
+
console.log(
|
|
2887
|
+
` Total tokens: ${index.total_tokens.toLocaleString()}`
|
|
2888
|
+
);
|
|
2889
|
+
console.log(` Last indexed: ${index.indexed_at}`);
|
|
2890
|
+
if (topics.length > 0) {
|
|
2891
|
+
p12.log.step("Topics:");
|
|
2892
|
+
for (const topic of topics.slice(0, 20)) {
|
|
2893
|
+
const count = index.entries.filter((e) => e.topic === topic).length;
|
|
2894
|
+
console.log(` - ${topic} (${count} entries)`);
|
|
2895
|
+
}
|
|
2896
|
+
if (topics.length > 20) {
|
|
2897
|
+
console.log(` ... and ${topics.length - 20} more`);
|
|
1314
2898
|
}
|
|
1315
2899
|
}
|
|
1316
|
-
} catch {
|
|
2900
|
+
} catch (err) {
|
|
2901
|
+
p12.log.error(`Failed to read index: ${err}`);
|
|
2902
|
+
process.exit(1);
|
|
1317
2903
|
}
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
2904
|
+
}
|
|
2905
|
+
});
|
|
2906
|
+
var knowledgeCommand = defineCommand12({
|
|
2907
|
+
meta: {
|
|
2908
|
+
name: "knowledge",
|
|
2909
|
+
description: "Manage SNIPER knowledge base"
|
|
2910
|
+
},
|
|
2911
|
+
subCommands: {
|
|
2912
|
+
index: indexSubcommand,
|
|
2913
|
+
status: statusSubcommand2
|
|
2914
|
+
}
|
|
2915
|
+
});
|
|
2916
|
+
|
|
2917
|
+
// src/commands/sphere.ts
|
|
2918
|
+
import { defineCommand as defineCommand13 } from "citty";
|
|
2919
|
+
import * as p13 from "@clack/prompts";
|
|
2920
|
+
|
|
2921
|
+
// src/conflict-detector.ts
|
|
2922
|
+
import { readFile as readFile14, writeFile as writeFile7, readdir as readdir7, mkdir as mkdir6, rm as rm3 } from "fs/promises";
|
|
2923
|
+
import { join as join15 } from "path";
|
|
2924
|
+
import YAML11 from "yaml";
|
|
2925
|
+
var WORKSPACE_DIR2 = ".sniper-workspace";
|
|
2926
|
+
var LOCKS_DIR = "locks";
|
|
2927
|
+
function encodeLockFilename(file) {
|
|
2928
|
+
return file.replace(/%/g, "%25").replace(/\//g, "%2F").replace(/\\/g, "%5C") + ".yaml";
|
|
2929
|
+
}
|
|
2930
|
+
function locksDir(workspaceRoot) {
|
|
2931
|
+
return join15(workspaceRoot, WORKSPACE_DIR2, LOCKS_DIR);
|
|
2932
|
+
}
|
|
2933
|
+
async function createLock(workspaceRoot, file, project, agent, protocol, reason) {
|
|
2934
|
+
const dir = locksDir(workspaceRoot);
|
|
2935
|
+
await mkdir6(dir, { recursive: true });
|
|
2936
|
+
const lock = {
|
|
2937
|
+
file,
|
|
2938
|
+
locked_by: { project, agent, protocol },
|
|
2939
|
+
since: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2940
|
+
...reason ? { reason } : {}
|
|
2941
|
+
};
|
|
2942
|
+
const lockPath = join15(dir, encodeLockFilename(file));
|
|
2943
|
+
try {
|
|
2944
|
+
await writeFile7(lockPath, YAML11.stringify(lock, { lineWidth: 0 }), {
|
|
2945
|
+
encoding: "utf-8",
|
|
2946
|
+
flag: "wx"
|
|
2947
|
+
});
|
|
2948
|
+
} catch (err) {
|
|
2949
|
+
if (err && typeof err === "object" && "code" in err && err.code === "EEXIST") {
|
|
2950
|
+
throw new Error(
|
|
2951
|
+
`Lock already exists for "${file}". Another agent may be modifying this file.`
|
|
1325
2952
|
);
|
|
1326
|
-
} else {
|
|
1327
|
-
for (const repo of repos) {
|
|
1328
|
-
p8.log.info(` ${repo.name} (${repo.role}, ${repo.language}) ${repo.path}`);
|
|
1329
|
-
}
|
|
1330
2953
|
}
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
shared_domain_packs: [],
|
|
1345
|
-
memory: {
|
|
1346
|
-
workspace_conventions: true,
|
|
1347
|
-
auto_promote: false
|
|
1348
|
-
}
|
|
1349
|
-
},
|
|
1350
|
-
state: {
|
|
1351
|
-
feature_counter: 1,
|
|
1352
|
-
features: []
|
|
1353
|
-
}
|
|
1354
|
-
};
|
|
1355
|
-
await writeFile4(
|
|
1356
|
-
join5(cwd, "workspace.yaml"),
|
|
1357
|
-
YAML5.stringify(workspace, { lineWidth: 0 }),
|
|
1358
|
-
"utf-8"
|
|
1359
|
-
);
|
|
1360
|
-
await ensureDir2(join5(cwd, "memory"));
|
|
1361
|
-
await writeFile4(
|
|
1362
|
-
join5(cwd, "memory", "conventions.yaml"),
|
|
1363
|
-
"conventions: []\n",
|
|
1364
|
-
"utf-8"
|
|
1365
|
-
);
|
|
1366
|
-
await writeFile4(
|
|
1367
|
-
join5(cwd, "memory", "anti-patterns.yaml"),
|
|
1368
|
-
"anti_patterns: []\n",
|
|
1369
|
-
"utf-8"
|
|
1370
|
-
);
|
|
1371
|
-
await writeFile4(
|
|
1372
|
-
join5(cwd, "memory", "decisions.yaml"),
|
|
1373
|
-
"decisions: []\n",
|
|
1374
|
-
"utf-8"
|
|
1375
|
-
);
|
|
1376
|
-
await ensureDir2(join5(cwd, "contracts"));
|
|
1377
|
-
await writeFile4(join5(cwd, "contracts", ".gitkeep"), "", "utf-8");
|
|
1378
|
-
await ensureDir2(join5(cwd, "features"));
|
|
1379
|
-
await writeFile4(join5(cwd, "features", ".gitkeep"), "", "utf-8");
|
|
1380
|
-
if (repos.length > 0) {
|
|
1381
|
-
await ensureDir2(join5(cwd, "repositories"));
|
|
1382
|
-
for (const repo of repos) {
|
|
1383
|
-
const linkPath = join5(cwd, "repositories", repo.name);
|
|
1384
|
-
const targetPath = resolve2(cwd, repo.path);
|
|
1385
|
-
if (!await pathExists2(linkPath)) {
|
|
1386
|
-
try {
|
|
1387
|
-
await symlink(targetPath, linkPath);
|
|
1388
|
-
} catch {
|
|
1389
|
-
}
|
|
1390
|
-
}
|
|
2954
|
+
throw err;
|
|
2955
|
+
}
|
|
2956
|
+
}
|
|
2957
|
+
async function releaseLock(workspaceRoot, file, owner) {
|
|
2958
|
+
const lockPath = join15(locksDir(workspaceRoot), encodeLockFilename(file));
|
|
2959
|
+
if (await pathExists(lockPath)) {
|
|
2960
|
+
if (owner) {
|
|
2961
|
+
const raw = await readFile14(lockPath, "utf-8");
|
|
2962
|
+
const lock = YAML11.parse(raw);
|
|
2963
|
+
if (lock?.locked_by?.agent !== owner && lock?.locked_by?.project !== owner) {
|
|
2964
|
+
throw new Error(
|
|
2965
|
+
`Cannot release lock for "${file}": owned by agent "${lock?.locked_by?.agent}" / project "${lock?.locked_by?.project}", not "${owner}"`
|
|
2966
|
+
);
|
|
1391
2967
|
}
|
|
1392
2968
|
}
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
2969
|
+
await rm3(lockPath);
|
|
2970
|
+
return true;
|
|
2971
|
+
}
|
|
2972
|
+
return false;
|
|
2973
|
+
}
|
|
2974
|
+
async function readLocks(workspaceRoot) {
|
|
2975
|
+
const dir = locksDir(workspaceRoot);
|
|
2976
|
+
if (!await pathExists(dir)) {
|
|
2977
|
+
return [];
|
|
2978
|
+
}
|
|
2979
|
+
const files = await readdir7(dir);
|
|
2980
|
+
const yamlFiles = files.filter(
|
|
2981
|
+
(f) => f.endsWith(".yaml") || f.endsWith(".yml")
|
|
2982
|
+
);
|
|
2983
|
+
const locks = [];
|
|
2984
|
+
for (const file of yamlFiles) {
|
|
2985
|
+
const raw = await readFile14(join15(dir, file), "utf-8");
|
|
2986
|
+
const data = YAML11.parse(raw);
|
|
2987
|
+
if (data && data.file && data.locked_by) {
|
|
2988
|
+
locks.push(data);
|
|
1406
2989
|
}
|
|
1407
|
-
p8.log.success("Workspace initialized!");
|
|
1408
|
-
p8.log.info(` Location: ${cwd}`);
|
|
1409
|
-
p8.log.info(` Repos: ${repos.length}`);
|
|
1410
|
-
p8.log.info("");
|
|
1411
|
-
p8.log.info("Next steps:");
|
|
1412
|
-
p8.log.info(
|
|
1413
|
-
' /sniper-workspace feature "description" \u2014 Plan a cross-repo feature'
|
|
1414
|
-
);
|
|
1415
|
-
p8.log.info(
|
|
1416
|
-
" /sniper-workspace status \u2014 View workspace status"
|
|
1417
|
-
);
|
|
1418
|
-
p8.outro("");
|
|
1419
2990
|
}
|
|
1420
|
-
|
|
1421
|
-
|
|
2991
|
+
return locks;
|
|
2992
|
+
}
|
|
2993
|
+
async function checkConflicts(workspaceRoot, filesToModify, project) {
|
|
2994
|
+
const locks = await readLocks(workspaceRoot);
|
|
2995
|
+
const conflicts = [];
|
|
2996
|
+
const normalizedFiles = filesToModify.map((f) => f.replace(/\\/g, "/"));
|
|
2997
|
+
for (const lock of locks) {
|
|
2998
|
+
const normalizedLockFile = lock.file.replace(/\\/g, "/");
|
|
2999
|
+
if (normalizedFiles.includes(normalizedLockFile) && lock.locked_by.project !== project) {
|
|
3000
|
+
conflicts.push({
|
|
3001
|
+
file: lock.file,
|
|
3002
|
+
held_by: lock.locked_by,
|
|
3003
|
+
requested_by: {
|
|
3004
|
+
project,
|
|
3005
|
+
agent: "unknown",
|
|
3006
|
+
protocol: "unknown"
|
|
3007
|
+
}
|
|
3008
|
+
});
|
|
3009
|
+
}
|
|
3010
|
+
}
|
|
3011
|
+
return conflicts;
|
|
3012
|
+
}
|
|
3013
|
+
|
|
3014
|
+
// src/commands/sphere.ts
|
|
3015
|
+
import { execFileSync as execFileSync4 } from "child_process";
|
|
3016
|
+
import { basename as basename2 } from "path";
|
|
3017
|
+
var WORKSPACE_ERROR = "No workspace found. Sphere 7 requires a workspace. Run `sniper workspace init` first.";
|
|
3018
|
+
var statusSubcommand3 = defineCommand13({
|
|
1422
3019
|
meta: {
|
|
1423
3020
|
name: "status",
|
|
1424
|
-
description: "Show
|
|
3021
|
+
description: "Show active file locks and dependency graph summary"
|
|
1425
3022
|
},
|
|
1426
3023
|
run: async () => {
|
|
1427
3024
|
const cwd = process.cwd();
|
|
1428
|
-
const
|
|
1429
|
-
if (!
|
|
1430
|
-
|
|
1431
|
-
"No workspace found. Run /sniper-workspace init to create one."
|
|
1432
|
-
);
|
|
3025
|
+
const wsRoot = await findWorkspaceRoot(cwd);
|
|
3026
|
+
if (!wsRoot) {
|
|
3027
|
+
p13.log.error(WORKSPACE_ERROR);
|
|
1433
3028
|
process.exit(1);
|
|
1434
3029
|
}
|
|
1435
|
-
|
|
1436
|
-
const
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
for (const repo of ws.repositories) {
|
|
1441
|
-
const repoPath = resolve2(cwd, repo.path);
|
|
1442
|
-
const accessible = await pathExists2(repoPath);
|
|
1443
|
-
const icon = accessible ? "\u2713" : "\u2717";
|
|
1444
|
-
p8.log.info(
|
|
1445
|
-
` ${icon} ${repo.name.padEnd(20)} ${repo.role.padEnd(12)} ${repo.language}`
|
|
1446
|
-
);
|
|
1447
|
-
}
|
|
1448
|
-
const activeFeatures = ws.state.features.filter(
|
|
1449
|
-
(f) => f.phase !== "complete"
|
|
1450
|
-
);
|
|
1451
|
-
if (activeFeatures.length > 0) {
|
|
1452
|
-
p8.log.step("Active Features:");
|
|
1453
|
-
for (const f of activeFeatures) {
|
|
1454
|
-
p8.log.info(
|
|
1455
|
-
` ${f.id} "${f.title}" Phase: ${f.phase}${f.sprint_wave ? ` Wave: ${f.sprint_wave}` : ""}`
|
|
1456
|
-
);
|
|
1457
|
-
}
|
|
3030
|
+
p13.intro("Sphere 7 \u2014 Workspace Status");
|
|
3031
|
+
const locks = await readLocks(wsRoot);
|
|
3032
|
+
p13.log.step(`Active locks: ${locks.length}`);
|
|
3033
|
+
if (locks.length === 0) {
|
|
3034
|
+
console.log(" (none)");
|
|
1458
3035
|
} else {
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
);
|
|
1466
|
-
if (files.length > 0) {
|
|
1467
|
-
p8.log.step("Contracts:");
|
|
1468
|
-
for (const file of files) {
|
|
1469
|
-
try {
|
|
1470
|
-
const cRaw = await readFile5(join5(contractsDir, file), "utf-8");
|
|
1471
|
-
const contract = YAML5.parse(cRaw);
|
|
1472
|
-
const name = contract.contract?.name || file;
|
|
1473
|
-
const version2 = contract.contract?.version || "?";
|
|
1474
|
-
const between = contract.contract?.between?.join(" \u2194 ") || "?";
|
|
1475
|
-
p8.log.info(` ${name} v${version2} ${between}`);
|
|
1476
|
-
} catch {
|
|
1477
|
-
p8.log.info(` ${file} (parse error)`);
|
|
1478
|
-
}
|
|
1479
|
-
}
|
|
1480
|
-
} else {
|
|
1481
|
-
p8.log.step("No contracts defined.");
|
|
1482
|
-
}
|
|
1483
|
-
}
|
|
1484
|
-
const memDir = join5(cwd, "memory");
|
|
1485
|
-
if (await pathExists2(memDir)) {
|
|
1486
|
-
const convFile = join5(memDir, "conventions.yaml");
|
|
1487
|
-
const apFile = join5(memDir, "anti-patterns.yaml");
|
|
1488
|
-
const decFile = join5(memDir, "decisions.yaml");
|
|
1489
|
-
let convCount = 0;
|
|
1490
|
-
let apCount = 0;
|
|
1491
|
-
let decCount = 0;
|
|
1492
|
-
if (await pathExists2(convFile)) {
|
|
1493
|
-
try {
|
|
1494
|
-
const parsed = YAML5.parse(await readFile5(convFile, "utf-8"));
|
|
1495
|
-
convCount = Array.isArray(parsed?.conventions) ? parsed.conventions.length : 0;
|
|
1496
|
-
} catch {
|
|
1497
|
-
}
|
|
1498
|
-
}
|
|
1499
|
-
if (await pathExists2(apFile)) {
|
|
1500
|
-
try {
|
|
1501
|
-
const parsed = YAML5.parse(await readFile5(apFile, "utf-8"));
|
|
1502
|
-
apCount = Array.isArray(parsed?.anti_patterns) ? parsed.anti_patterns.length : 0;
|
|
1503
|
-
} catch {
|
|
1504
|
-
}
|
|
1505
|
-
}
|
|
1506
|
-
if (await pathExists2(decFile)) {
|
|
1507
|
-
try {
|
|
1508
|
-
const parsed = YAML5.parse(await readFile5(decFile, "utf-8"));
|
|
1509
|
-
decCount = Array.isArray(parsed?.decisions) ? parsed.decisions.length : 0;
|
|
1510
|
-
} catch {
|
|
1511
|
-
}
|
|
3036
|
+
for (const lock of locks) {
|
|
3037
|
+
const age = timeSince(lock.since);
|
|
3038
|
+
const reason = lock.reason ? ` \u2014 ${lock.reason}` : "";
|
|
3039
|
+
console.log(
|
|
3040
|
+
` ${lock.file} locked by ${lock.locked_by.project}/${lock.locked_by.agent} (${age})${reason}`
|
|
3041
|
+
);
|
|
1512
3042
|
}
|
|
1513
|
-
p8.log.step("Workspace Memory:");
|
|
1514
|
-
p8.log.info(` Conventions: ${convCount}`);
|
|
1515
|
-
p8.log.info(` Anti-Patterns: ${apCount}`);
|
|
1516
|
-
p8.log.info(` Decisions: ${decCount}`);
|
|
1517
3043
|
}
|
|
1518
|
-
|
|
3044
|
+
p13.outro("");
|
|
1519
3045
|
}
|
|
1520
3046
|
});
|
|
1521
|
-
var
|
|
3047
|
+
var lockSubcommand = defineCommand13({
|
|
1522
3048
|
meta: {
|
|
1523
|
-
name: "
|
|
1524
|
-
description: "
|
|
3049
|
+
name: "lock",
|
|
3050
|
+
description: "Acquire an advisory lock on a file"
|
|
1525
3051
|
},
|
|
1526
3052
|
args: {
|
|
1527
|
-
|
|
3053
|
+
file: {
|
|
1528
3054
|
type: "positional",
|
|
1529
|
-
description: "
|
|
3055
|
+
description: "File path to lock",
|
|
1530
3056
|
required: true
|
|
3057
|
+
},
|
|
3058
|
+
reason: {
|
|
3059
|
+
type: "string",
|
|
3060
|
+
description: "Reason for acquiring the lock",
|
|
3061
|
+
required: false
|
|
1531
3062
|
}
|
|
1532
3063
|
},
|
|
1533
3064
|
run: async ({ args }) => {
|
|
1534
3065
|
const cwd = process.cwd();
|
|
1535
|
-
const
|
|
1536
|
-
if (!
|
|
1537
|
-
|
|
3066
|
+
const wsRoot = await findWorkspaceRoot(cwd);
|
|
3067
|
+
if (!wsRoot) {
|
|
3068
|
+
p13.log.error(WORKSPACE_ERROR);
|
|
1538
3069
|
process.exit(1);
|
|
1539
3070
|
}
|
|
1540
|
-
const
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
3071
|
+
const project = basename2(cwd);
|
|
3072
|
+
try {
|
|
3073
|
+
await createLock(
|
|
3074
|
+
wsRoot,
|
|
3075
|
+
args.file,
|
|
3076
|
+
project,
|
|
3077
|
+
"cli",
|
|
3078
|
+
"manual",
|
|
3079
|
+
args.reason
|
|
1544
3080
|
);
|
|
3081
|
+
p13.log.success(`Locked: ${args.file}`);
|
|
3082
|
+
} catch (err) {
|
|
3083
|
+
p13.log.error(`Failed to acquire lock: ${err}`);
|
|
1545
3084
|
process.exit(1);
|
|
1546
3085
|
}
|
|
1547
|
-
const repoConfig = await readConfig(repoPath);
|
|
1548
|
-
const raw = await readFile5(wsPath, "utf-8");
|
|
1549
|
-
const ws = YAML5.parse(raw);
|
|
1550
|
-
const repoName = repoConfig.project.name;
|
|
1551
|
-
if (ws.repositories.some((r) => r.name === repoName)) {
|
|
1552
|
-
p8.log.warn(`Repository "${repoName}" is already in the workspace.`);
|
|
1553
|
-
process.exit(0);
|
|
1554
|
-
}
|
|
1555
|
-
ws.repositories.push({
|
|
1556
|
-
name: repoName,
|
|
1557
|
-
path: relative(cwd, repoPath),
|
|
1558
|
-
role: inferRole(repoConfig.project.type),
|
|
1559
|
-
language: repoConfig.stack.language,
|
|
1560
|
-
sniper_enabled: true,
|
|
1561
|
-
exposes: [],
|
|
1562
|
-
consumes: []
|
|
1563
|
-
});
|
|
1564
|
-
ws.dependency_graph[repoName] = [];
|
|
1565
|
-
await writeFile4(wsPath, YAML5.stringify(ws, { lineWidth: 0 }), "utf-8");
|
|
1566
|
-
repoConfig.workspace = {
|
|
1567
|
-
enabled: true,
|
|
1568
|
-
workspace_path: relative(repoPath, cwd),
|
|
1569
|
-
repo_name: repoName
|
|
1570
|
-
};
|
|
1571
|
-
await writeConfig(repoPath, repoConfig);
|
|
1572
|
-
p8.log.success(
|
|
1573
|
-
`Added ${repoName} (${repoConfig.project.type}, ${repoConfig.stack.language})`
|
|
1574
|
-
);
|
|
1575
3086
|
}
|
|
1576
3087
|
});
|
|
1577
|
-
var
|
|
3088
|
+
var unlockSubcommand = defineCommand13({
|
|
1578
3089
|
meta: {
|
|
1579
|
-
name: "
|
|
1580
|
-
description: "
|
|
3090
|
+
name: "unlock",
|
|
3091
|
+
description: "Release an advisory lock on a file"
|
|
1581
3092
|
},
|
|
1582
3093
|
args: {
|
|
1583
|
-
|
|
3094
|
+
file: {
|
|
1584
3095
|
type: "positional",
|
|
1585
|
-
description: "
|
|
3096
|
+
description: "File path to unlock",
|
|
1586
3097
|
required: true
|
|
1587
3098
|
}
|
|
1588
3099
|
},
|
|
1589
3100
|
run: async ({ args }) => {
|
|
1590
3101
|
const cwd = process.cwd();
|
|
1591
|
-
const
|
|
1592
|
-
if (!
|
|
1593
|
-
|
|
1594
|
-
process.exit(1);
|
|
1595
|
-
}
|
|
1596
|
-
const raw = await readFile5(wsPath, "utf-8");
|
|
1597
|
-
const ws = YAML5.parse(raw);
|
|
1598
|
-
const repoName = args.name;
|
|
1599
|
-
const idx = ws.repositories.findIndex((r) => r.name === repoName);
|
|
1600
|
-
if (idx < 0) {
|
|
1601
|
-
p8.log.error(`Repository "${repoName}" not found in workspace.`);
|
|
3102
|
+
const wsRoot = await findWorkspaceRoot(cwd);
|
|
3103
|
+
if (!wsRoot) {
|
|
3104
|
+
p13.log.error(WORKSPACE_ERROR);
|
|
1602
3105
|
process.exit(1);
|
|
1603
3106
|
}
|
|
1604
|
-
const
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
if (depIdx >= 0) deps.splice(depIdx, 1);
|
|
1610
|
-
}
|
|
1611
|
-
await writeFile4(wsPath, YAML5.stringify(ws, { lineWidth: 0 }), "utf-8");
|
|
1612
|
-
const repoPath = resolve2(cwd, repo.path);
|
|
1613
|
-
try {
|
|
1614
|
-
const repoConfig = await readConfig(repoPath);
|
|
1615
|
-
repoConfig.workspace = {
|
|
1616
|
-
enabled: false,
|
|
1617
|
-
workspace_path: null,
|
|
1618
|
-
repo_name: null
|
|
1619
|
-
};
|
|
1620
|
-
await writeConfig(repoPath, repoConfig);
|
|
1621
|
-
} catch {
|
|
3107
|
+
const released = await releaseLock(wsRoot, args.file);
|
|
3108
|
+
if (released) {
|
|
3109
|
+
p13.log.success(`Unlocked: ${args.file}`);
|
|
3110
|
+
} else {
|
|
3111
|
+
p13.log.warning(`No lock found for: ${args.file}`);
|
|
1622
3112
|
}
|
|
1623
|
-
p8.log.success(`Removed ${repoName} from workspace.`);
|
|
1624
3113
|
}
|
|
1625
3114
|
});
|
|
1626
|
-
var
|
|
3115
|
+
var conflictsSubcommand = defineCommand13({
|
|
1627
3116
|
meta: {
|
|
1628
|
-
name: "
|
|
1629
|
-
description: "
|
|
3117
|
+
name: "conflicts",
|
|
3118
|
+
description: "Detect file lock conflicts with current changes"
|
|
1630
3119
|
},
|
|
1631
3120
|
run: async () => {
|
|
1632
3121
|
const cwd = process.cwd();
|
|
1633
|
-
const
|
|
1634
|
-
if (!
|
|
1635
|
-
|
|
3122
|
+
const wsRoot = await findWorkspaceRoot(cwd);
|
|
3123
|
+
if (!wsRoot) {
|
|
3124
|
+
p13.log.error(WORKSPACE_ERROR);
|
|
1636
3125
|
process.exit(1);
|
|
1637
3126
|
}
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
3127
|
+
p13.intro("Sphere 7 \u2014 Conflict Detection");
|
|
3128
|
+
let changedFiles;
|
|
3129
|
+
try {
|
|
3130
|
+
const output = execFileSync4("git", ["diff", "--name-only"], {
|
|
3131
|
+
cwd,
|
|
3132
|
+
encoding: "utf-8"
|
|
3133
|
+
});
|
|
3134
|
+
changedFiles = output.split("\n").map((f) => f.trim()).filter(Boolean);
|
|
3135
|
+
} catch {
|
|
3136
|
+
p13.log.error("Failed to get changed files from git.");
|
|
1641
3137
|
process.exit(1);
|
|
1642
3138
|
}
|
|
1643
|
-
|
|
1644
|
-
(
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
p8.log.info("No contracts found. Create them with /sniper-workspace feature.");
|
|
1648
|
-
process.exit(0);
|
|
3139
|
+
if (changedFiles.length === 0) {
|
|
3140
|
+
p13.log.info("No changed files detected.");
|
|
3141
|
+
p13.outro("");
|
|
3142
|
+
return;
|
|
1649
3143
|
}
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
p8.log.info(
|
|
1661
|
-
`${name} v${version2}: ${endpoints} endpoints, ${types} types, ${events} events`
|
|
1662
|
-
);
|
|
1663
|
-
p8.log.info(
|
|
1664
|
-
" (Structural validation requires running /sniper-workspace validate as a slash command)"
|
|
3144
|
+
p13.log.step(`Changed files: ${changedFiles.length}`);
|
|
3145
|
+
const project = basename2(cwd);
|
|
3146
|
+
const conflicts = await checkConflicts(wsRoot, changedFiles, project);
|
|
3147
|
+
if (conflicts.length === 0) {
|
|
3148
|
+
p13.log.success("No conflicts detected.");
|
|
3149
|
+
} else {
|
|
3150
|
+
p13.log.warning(`${conflicts.length} conflict(s) detected:`);
|
|
3151
|
+
for (const conflict of conflicts) {
|
|
3152
|
+
console.log(
|
|
3153
|
+
` ${conflict.file} held by ${conflict.held_by.project}/${conflict.held_by.agent} (protocol: ${conflict.held_by.protocol})`
|
|
1665
3154
|
);
|
|
1666
|
-
} catch {
|
|
1667
|
-
p8.log.warn(` ${file}: parse error`);
|
|
1668
3155
|
}
|
|
1669
3156
|
}
|
|
1670
|
-
|
|
1671
|
-
"\nFull validation (endpoint/type/event checking) runs via the /sniper-workspace validate slash command."
|
|
1672
|
-
);
|
|
1673
|
-
p8.outro("");
|
|
3157
|
+
p13.outro("");
|
|
1674
3158
|
}
|
|
1675
3159
|
});
|
|
1676
|
-
|
|
1677
|
-
switch (projectType) {
|
|
1678
|
-
case "saas":
|
|
1679
|
-
case "web":
|
|
1680
|
-
case "mobile":
|
|
1681
|
-
return "frontend";
|
|
1682
|
-
case "api":
|
|
1683
|
-
return "backend";
|
|
1684
|
-
case "library":
|
|
1685
|
-
return "library";
|
|
1686
|
-
case "cli":
|
|
1687
|
-
case "monorepo":
|
|
1688
|
-
return "service";
|
|
1689
|
-
default:
|
|
1690
|
-
return "service";
|
|
1691
|
-
}
|
|
1692
|
-
}
|
|
1693
|
-
var workspaceCommand = defineCommand8({
|
|
3160
|
+
var sphereCommand = defineCommand13({
|
|
1694
3161
|
meta: {
|
|
1695
|
-
name: "
|
|
1696
|
-
description: "
|
|
3162
|
+
name: "sphere",
|
|
3163
|
+
description: "Sphere 7 \u2014 Cross-human workspace coordination"
|
|
1697
3164
|
},
|
|
1698
3165
|
subCommands: {
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
validate: validateSubCommand
|
|
3166
|
+
status: statusSubcommand3,
|
|
3167
|
+
lock: lockSubcommand,
|
|
3168
|
+
unlock: unlockSubcommand,
|
|
3169
|
+
conflicts: conflictsSubcommand
|
|
1704
3170
|
}
|
|
1705
3171
|
});
|
|
3172
|
+
function timeSince(isoDate) {
|
|
3173
|
+
const ms = Date.now() - new Date(isoDate).getTime();
|
|
3174
|
+
const seconds = Math.floor(ms / 1e3);
|
|
3175
|
+
if (seconds < 60) return `${seconds}s ago`;
|
|
3176
|
+
const minutes = Math.floor(seconds / 60);
|
|
3177
|
+
if (minutes < 60) return `${minutes}m ago`;
|
|
3178
|
+
const hours = Math.floor(minutes / 60);
|
|
3179
|
+
if (hours < 24) return `${hours}h ago`;
|
|
3180
|
+
const days = Math.floor(hours / 24);
|
|
3181
|
+
return `${days}d ago`;
|
|
3182
|
+
}
|
|
1706
3183
|
|
|
1707
3184
|
// src/index.ts
|
|
1708
3185
|
var require2 = createRequire2(import.meta.url);
|
|
1709
3186
|
var { version } = require2("../package.json");
|
|
1710
|
-
var main =
|
|
3187
|
+
var main = defineCommand14({
|
|
1711
3188
|
meta: {
|
|
1712
3189
|
name: "sniper",
|
|
1713
3190
|
version,
|
|
1714
|
-
description: "SNIPER \u2014
|
|
3191
|
+
description: "SNIPER v3 \u2014 AI-Powered Project Lifecycle Framework"
|
|
1715
3192
|
},
|
|
1716
3193
|
subCommands: {
|
|
1717
3194
|
init: initCommand,
|
|
1718
3195
|
status: statusCommand,
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
3196
|
+
migrate: migrateCommand,
|
|
3197
|
+
plugin: pluginCommand,
|
|
3198
|
+
protocol: protocolCommand,
|
|
3199
|
+
dashboard: dashboardCommand,
|
|
3200
|
+
workspace: workspaceCommand,
|
|
3201
|
+
revert: revertCommand,
|
|
3202
|
+
run: runCommand,
|
|
3203
|
+
marketplace: marketplaceCommand,
|
|
3204
|
+
signal: signalCommand,
|
|
3205
|
+
knowledge: knowledgeCommand,
|
|
3206
|
+
sphere: sphereCommand
|
|
1725
3207
|
}
|
|
1726
3208
|
});
|
|
1727
3209
|
runMain(main);
|