specrails-hub 1.1.0 → 1.1.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # specrails hub
2
2
 
3
- A local dashboard and CLI for managing all your [specrails](https://github.com/fjpulidop/specrails) projects from a single interface. Visualizes the AI pipeline phases (Architect, Developer, Reviewer, Ship), streams logs in real-time, and lets you launch commands from the browser or terminal.
3
+ A local dashboard and CLI for managing all your [specrails-core](https://github.com/fjpulidop/specrails-core) projects from a single interface. Visualizes the AI pipeline phases (Architect, Developer, Reviewer, Ship), streams logs in real-time, and lets you launch commands from the browser or terminal.
4
4
 
5
5
  ## Features
6
6
 
@@ -16,12 +16,12 @@ A local dashboard and CLI for managing all your [specrails](https://github.com/f
16
16
 
17
17
  - Node.js 18+
18
18
  - `claude` CLI on your PATH ([Claude Code](https://claude.com/claude-code))
19
- - At least one project with specrails installed (`npx specrails`)
19
+ - At least one project with specrails-core installed (`npx specrails-core`)
20
20
 
21
21
  ## Installation
22
22
 
23
23
  ```bash
24
- npm install -g @specrails/hub
24
+ npm install -g specrails-hub
25
25
  ```
26
26
 
27
27
  ## Quick Start
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "specrails-hub",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "bin": {
5
- "specrails-hub": "./cli/dist/specrails-hub.js"
5
+ "specrails-hub": "cli/dist/specrails-hub.js"
6
6
  },
7
7
  "files": [
8
8
  "cli/dist",
package/cli/dist/hub.js DELETED
@@ -1,910 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- /**
4
- * specrails-hub — specrails CLI bridge
5
- *
6
- * Routes commands to the manager when running, or falls back to invoking
7
- * claude directly when the manager is not reachable.
8
- *
9
- * Usage:
10
- * specrails-hub implement #42 → /sr:implement #42 (via manager or direct)
11
- * specrails-hub "any raw prompt" → raw prompt (no /sr: prefix)
12
- * specrails-hub --status → print manager state
13
- * specrails-hub --jobs → print job history table
14
- * specrails-hub --port 5000 <command> → use port 5000 instead of 4200
15
- * specrails-hub --help → print usage and exit 0
16
- */
17
- var __importDefault = (this && this.__importDefault) || function (mod) {
18
- return (mod && mod.__esModule) ? mod : { "default": mod };
19
- };
20
- Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.parseArgs = parseArgs;
22
- exports.detectWebManager = detectWebManager;
23
- exports.formatDuration = formatDuration;
24
- exports.formatTokens = formatTokens;
25
- exports.printSummary = printSummary;
26
- const http_1 = __importDefault(require("http"));
27
- const child_process_1 = require("child_process");
28
- const child_process_2 = require("child_process");
29
- const readline_1 = require("readline");
30
- const ws_1 = __importDefault(require("ws"));
31
- const path_1 = __importDefault(require("path"));
32
- const os_1 = __importDefault(require("os"));
33
- const fs_1 = __importDefault(require("fs"));
34
- // ---------------------------------------------------------------------------
35
- // Constants
36
- // ---------------------------------------------------------------------------
37
- const DEFAULT_PORT = 4200;
38
- const DETECTION_TIMEOUT_MS = 500;
39
- const KNOWN_VERBS = new Set([
40
- 'implement',
41
- 'batch-implement',
42
- 'why',
43
- 'product-backlog',
44
- 'update-product-driven-backlog',
45
- 'refactor-recommender',
46
- 'health-check',
47
- 'compat-check',
48
- ]);
49
- const EXIT_PATTERN = /\[process exited with code (\d+)/;
50
- // ---------------------------------------------------------------------------
51
- // ANSI helpers
52
- // ---------------------------------------------------------------------------
53
- const isTTY = process.stdout.isTTY === true;
54
- function ansi(code, text) {
55
- if (!isTTY)
56
- return text;
57
- return `\x1b[${code}m${text}\x1b[0m`;
58
- }
59
- const dim = (t) => ansi('2', t);
60
- const red = (t) => ansi('31', t);
61
- const bold = (t) => ansi('1', t);
62
- const dimCyan = (t) => ansi('2;36', t);
63
- function hubPrefix() {
64
- return dim('[specrails-hub]');
65
- }
66
- function hubLog(msg) {
67
- process.stdout.write(`${hubPrefix()} ${msg}\n`);
68
- }
69
- function hubError(msg) {
70
- process.stderr.write(`${hubPrefix()} ${red(`error: ${msg}`)}\n`);
71
- }
72
- function hubWarn(msg) {
73
- process.stderr.write(`${hubPrefix()} ${dim(`warning: ${msg}`)}\n`);
74
- }
75
- const HUB_SUBCOMMANDS = new Set(['start', 'stop', 'status', 'add', 'remove', 'list']);
76
- function parseArgs(argv) {
77
- // argv is process.argv.slice(2)
78
- let port = DEFAULT_PORT;
79
- const args = [...argv];
80
- // Extract --port <n> from any position
81
- for (let i = 0; i < args.length; i++) {
82
- if (args[i] === '--port' && i + 1 < args.length) {
83
- const parsed = parseInt(args[i + 1], 10);
84
- if (!isNaN(parsed)) {
85
- port = parsed;
86
- }
87
- args.splice(i, 2);
88
- i--;
89
- }
90
- }
91
- if (args.length === 0 || args[0] === '--help' || args[0] === '-h') {
92
- return { mode: 'help' };
93
- }
94
- if (args[0] === '--status') {
95
- return { mode: 'status', port };
96
- }
97
- if (args[0] === '--jobs') {
98
- return { mode: 'jobs', port };
99
- }
100
- // Hub subcommands — top-level (no `hub` prefix needed)
101
- // backward compat: `specrails-hub hub start` still works
102
- if (args[0] === 'hub') {
103
- return { mode: 'hub', subArgs: args.slice(1), port };
104
- }
105
- if (HUB_SUBCOMMANDS.has(args[0])) {
106
- return { mode: 'hub', subArgs: args, port };
107
- }
108
- const first = args[0];
109
- // Slash-prefixed command: pass through unchanged
110
- if (first.startsWith('/')) {
111
- const resolved = args.join(' ');
112
- return { mode: 'raw', resolved, port };
113
- }
114
- // Known verb: inject /sr: prefix
115
- if (KNOWN_VERBS.has(first)) {
116
- const resolved = `/sr:${args.join(' ')}`;
117
- return { mode: 'command', resolved, port };
118
- }
119
- // Unknown first token: treat as raw prompt
120
- const resolved = args.join(' ');
121
- return { mode: 'raw', resolved, port };
122
- }
123
- function printHelp() {
124
- process.stdout.write(`
125
- ${bold('specrails-hub')} — specrails CLI bridge
126
-
127
- ${bold('Usage:')}
128
- specrails-hub start Start the hub server
129
- specrails-hub stop Stop the hub server
130
- specrails-hub status Show hub status and registered projects
131
- specrails-hub add <path> Register a project
132
- specrails-hub remove <id> Unregister a project
133
- specrails-hub list List all registered projects
134
- specrails-hub implement #42 Run a known specrails verb (prepends /sr:)
135
- specrails-hub batch-implement #40 #41 Known verbs: ${[...KNOWN_VERBS].join(', ')}
136
- specrails-hub "any raw prompt" Pass a raw prompt directly to claude
137
- specrails-hub --jobs Print recent job history and exit
138
- specrails-hub --port <n> Override default port (${DEFAULT_PORT})
139
- specrails-hub --help Show this help text
140
-
141
- ${bold('Execution paths:')}
142
- Manager running → POST /api/spawn + stream logs via WebSocket
143
- Manager not running → spawn claude directly with stream-json output
144
- `.trimStart());
145
- }
146
- function detectWebManager(port) {
147
- const baseUrl = `http://127.0.0.1:${port}`;
148
- return new Promise((resolve) => {
149
- const timer = setTimeout(() => {
150
- req.destroy();
151
- resolve({ running: false, baseUrl });
152
- }, DETECTION_TIMEOUT_MS);
153
- const req = http_1.default.get(`${baseUrl}/api/state`, { timeout: DETECTION_TIMEOUT_MS }, (res) => {
154
- clearTimeout(timer);
155
- res.resume(); // drain the response
156
- if (res.statusCode !== undefined && res.statusCode >= 200 && res.statusCode < 300) {
157
- resolve({ running: true, baseUrl });
158
- }
159
- else {
160
- resolve({ running: false, baseUrl });
161
- }
162
- });
163
- req.on('error', () => {
164
- clearTimeout(timer);
165
- resolve({ running: false, baseUrl });
166
- });
167
- req.on('timeout', () => {
168
- req.destroy();
169
- clearTimeout(timer);
170
- resolve({ running: false, baseUrl });
171
- });
172
- });
173
- }
174
- // ---------------------------------------------------------------------------
175
- // HTTP helpers
176
- // ---------------------------------------------------------------------------
177
- function httpGet(url) {
178
- return new Promise((resolve, reject) => {
179
- const req = http_1.default.get(url, (res) => {
180
- let body = '';
181
- res.on('data', (chunk) => { body += chunk; });
182
- res.on('end', () => resolve({ status: res.statusCode ?? 0, body }));
183
- });
184
- req.on('error', reject);
185
- });
186
- }
187
- function httpPost(url, payload) {
188
- return new Promise((resolve, reject) => {
189
- const data = JSON.stringify(payload);
190
- const urlObj = new URL(url);
191
- const options = {
192
- hostname: urlObj.hostname,
193
- port: urlObj.port,
194
- path: urlObj.pathname,
195
- method: 'POST',
196
- headers: {
197
- 'Content-Type': 'application/json',
198
- 'Content-Length': Buffer.byteLength(data),
199
- },
200
- };
201
- const req = http_1.default.request(options, (res) => {
202
- let body = '';
203
- res.on('data', (chunk) => { body += chunk; });
204
- res.on('end', () => resolve({ status: res.statusCode ?? 0, body }));
205
- });
206
- req.on('error', reject);
207
- req.write(data);
208
- req.end();
209
- });
210
- }
211
- // ---------------------------------------------------------------------------
212
- // Duration formatting
213
- // ---------------------------------------------------------------------------
214
- function formatDuration(ms) {
215
- const totalSeconds = Math.floor(ms / 1000);
216
- if (totalSeconds < 60) {
217
- return `${totalSeconds}s`;
218
- }
219
- const minutes = Math.floor(totalSeconds / 60);
220
- const seconds = totalSeconds % 60;
221
- return `${minutes}m ${seconds}s`;
222
- }
223
- // ---------------------------------------------------------------------------
224
- // Token formatting
225
- // ---------------------------------------------------------------------------
226
- function formatTokens(n) {
227
- return new Intl.NumberFormat('en-US', { useGrouping: true })
228
- .format(n)
229
- .replace(/,/g, ' ');
230
- }
231
- function printSummary(data) {
232
- const doneLabel = isTTY ? bold('[specrails-hub] done') : '[specrails-hub] done';
233
- const durationPart = `duration: ${formatDuration(data.durationMs)}`;
234
- const costPart = data.costUsd != null ? ` cost: $${data.costUsd.toFixed(2)}` : '';
235
- const tokenPart = data.totalTokens != null ? ` tokens: ${formatTokens(data.totalTokens)}` : '';
236
- const exitPart = ` exit: ${data.exitCode}`;
237
- process.stdout.write(`${doneLabel} ${durationPart}${costPart}${tokenPart}${exitPart}\n`);
238
- }
239
- async function resolveProjectFromCwd(baseUrl) {
240
- try {
241
- const cwd = process.cwd();
242
- const res = await httpGet(`${baseUrl}/api/hub/resolve?path=${encodeURIComponent(cwd)}`);
243
- if (res.status === 200) {
244
- const data = JSON.parse(res.body);
245
- return data.project ?? null;
246
- }
247
- }
248
- catch {
249
- // Hub endpoint not available — not in hub mode
250
- }
251
- return null;
252
- }
253
- async function runViaWebManager(command, baseUrl) {
254
- // Detect hub mode: check if /api/hub/state is reachable
255
- let spawnUrl = `${baseUrl}/api/spawn`;
256
- let jobApiBase = `${baseUrl}/api`;
257
- try {
258
- const hubCheck = await httpGet(`${baseUrl}/api/hub/state`);
259
- if (hubCheck.status === 200) {
260
- // Hub mode: resolve project from CWD
261
- const project = await resolveProjectFromCwd(baseUrl);
262
- if (!project) {
263
- hubError('hub is running but no project registered for the current directory.\n' +
264
- ` Run: specrails-hub hub add ${process.cwd()}`);
265
- return 1;
266
- }
267
- spawnUrl = `${baseUrl}/api/projects/${project.id}/spawn`;
268
- jobApiBase = `${baseUrl}/api/projects/${project.id}`;
269
- hubLog(`project: ${project.name}`);
270
- }
271
- }
272
- catch {
273
- // Single-project mode — use default paths
274
- }
275
- // Spawn the job
276
- let spawnRes;
277
- try {
278
- spawnRes = await httpPost(spawnUrl, { command });
279
- }
280
- catch (err) {
281
- hubError('failed to connect to manager');
282
- return 1;
283
- }
284
- if (spawnRes.status === 409) {
285
- hubError('manager is busy (another job is running)');
286
- return 1;
287
- }
288
- if (spawnRes.status >= 400) {
289
- let errMsg = `spawn failed with HTTP ${spawnRes.status}`;
290
- try {
291
- const parsed = JSON.parse(spawnRes.body);
292
- if (parsed.error)
293
- errMsg = parsed.error;
294
- }
295
- catch { /* use default */ }
296
- hubError(errMsg);
297
- return 1;
298
- }
299
- let processId;
300
- try {
301
- const parsed = JSON.parse(spawnRes.body);
302
- // Server returns jobId; processId is the legacy field name used in LogMessage
303
- processId = (parsed.jobId ?? parsed.processId) ?? '';
304
- if (!processId)
305
- throw new Error('missing jobId');
306
- }
307
- catch {
308
- hubError('invalid response from /api/spawn');
309
- return 1;
310
- }
311
- const startTime = Date.now();
312
- // Connect WebSocket and stream logs
313
- const wsUrl = baseUrl.replace(/^http/, 'ws');
314
- let exitCode = 1;
315
- let resolved = false;
316
- await new Promise((resolve) => {
317
- const ws = new ws_1.default(wsUrl);
318
- ws.on('message', (data) => {
319
- let msg;
320
- try {
321
- msg = JSON.parse(data.toString());
322
- }
323
- catch {
324
- return;
325
- }
326
- if (msg.type === 'init') {
327
- // Replay only log lines from our processId
328
- const initMsg = msg;
329
- for (const logLine of initMsg.logBuffer) {
330
- if (logLine.processId === processId) {
331
- handleLogLine(logLine);
332
- }
333
- }
334
- return;
335
- }
336
- if (msg.type === 'log') {
337
- const logMsg = msg;
338
- if (logMsg.processId !== processId)
339
- return;
340
- handleLogLine(logMsg);
341
- return;
342
- }
343
- if (msg.type === 'phase') {
344
- const phaseMsg = msg;
345
- process.stdout.write(` ${dimCyan(`→ [${phaseMsg.phase}] ${phaseMsg.state}`)}\n`);
346
- return;
347
- }
348
- });
349
- function handleLogLine(logMsg) {
350
- if (resolved)
351
- return;
352
- // Check for exit signal
353
- const match = EXIT_PATTERN.exec(logMsg.line);
354
- if (match) {
355
- exitCode = parseInt(match[1], 10);
356
- resolved = true;
357
- ws.close();
358
- resolve();
359
- return;
360
- }
361
- // Print to appropriate stream, preserving ANSI
362
- if (logMsg.source === 'stderr') {
363
- process.stderr.write(`${logMsg.line}\n`);
364
- }
365
- else {
366
- process.stdout.write(`${logMsg.line}\n`);
367
- }
368
- }
369
- ws.on('close', () => {
370
- if (!resolved) {
371
- hubWarn('lost connection to manager');
372
- resolved = true;
373
- resolve();
374
- }
375
- });
376
- ws.on('error', (err) => {
377
- if (!resolved) {
378
- hubWarn(`WebSocket error: ${err.message}`);
379
- resolved = true;
380
- resolve();
381
- }
382
- });
383
- });
384
- const durationMs = Date.now() - startTime;
385
- // Fetch job metadata for cost/tokens
386
- let costUsd;
387
- let totalTokens;
388
- try {
389
- const jobRes = await httpGet(`${jobApiBase}/jobs/${processId}`);
390
- if (jobRes.status === 200) {
391
- const parsed = JSON.parse(jobRes.body);
392
- if (parsed.job) {
393
- if (parsed.job.total_cost_usd != null)
394
- costUsd = parsed.job.total_cost_usd;
395
- const tokensIn = parsed.job.tokens_in ?? 0;
396
- const tokensOut = parsed.job.tokens_out ?? 0;
397
- if (parsed.job.tokens_in != null || parsed.job.tokens_out != null) {
398
- totalTokens = tokensIn + tokensOut;
399
- }
400
- // Prefer server-side duration when available
401
- if (parsed.job.duration_ms != null) {
402
- printSummary({ durationMs: parsed.job.duration_ms, costUsd, totalTokens, exitCode });
403
- return exitCode;
404
- }
405
- }
406
- }
407
- }
408
- catch { /* fall through to duration-only summary */ }
409
- printSummary({ durationMs, costUsd, totalTokens, exitCode });
410
- return exitCode;
411
- }
412
- async function runDirect(command) {
413
- const startTime = Date.now();
414
- const args = [
415
- '--dangerously-skip-permissions',
416
- '-p',
417
- ...command.trim().split(/\s+/),
418
- '--output-format', 'stream-json',
419
- '--verbose',
420
- ];
421
- let child;
422
- try {
423
- child = (0, child_process_1.spawn)('claude', args, {
424
- env: process.env,
425
- shell: false,
426
- });
427
- }
428
- catch (err) {
429
- const code = err.code;
430
- if (code === 'ENOENT') {
431
- hubError('claude binary not found');
432
- }
433
- else {
434
- hubError(`failed to spawn claude: ${err.message}`);
435
- }
436
- return 1;
437
- }
438
- let resultData;
439
- // Stderr: pass through unchanged
440
- child.stderr?.pipe(process.stderr);
441
- // Stdout: parse NDJSON line by line
442
- const rl = (0, readline_1.createInterface)({ input: child.stdout, crlfDelay: Infinity });
443
- rl.on('line', (line) => {
444
- if (!line.trim())
445
- return;
446
- let parsed = null;
447
- try {
448
- parsed = JSON.parse(line);
449
- }
450
- catch {
451
- // Non-JSON line: print as-is
452
- process.stdout.write(`${line}\n`);
453
- return;
454
- }
455
- if (parsed.type === 'text') {
456
- const content = parsed.content ?? '';
457
- if (content)
458
- process.stdout.write(`${content}\n`);
459
- }
460
- else if (parsed.type === 'result') {
461
- resultData = parsed;
462
- }
463
- // All other types: silently ignore
464
- });
465
- const exitCode = await new Promise((resolve) => {
466
- child.on('close', (code) => {
467
- resolve(code ?? 1);
468
- });
469
- child.on('error', (err) => {
470
- if (err.code === 'ENOENT') {
471
- hubError('claude binary not found');
472
- }
473
- else {
474
- hubError(`claude process error: ${err.message}`);
475
- }
476
- resolve(1);
477
- });
478
- });
479
- const durationMs = Date.now() - startTime;
480
- let costUsd;
481
- let totalTokens;
482
- if (resultData) {
483
- if (resultData.cost_usd != null)
484
- costUsd = resultData.cost_usd;
485
- const tokensIn = resultData.input_tokens ?? 0;
486
- const tokensOut = resultData.output_tokens ?? 0;
487
- if (resultData.input_tokens != null || resultData.output_tokens != null) {
488
- totalTokens = tokensIn + tokensOut;
489
- }
490
- }
491
- printSummary({ durationMs, costUsd, totalTokens, exitCode });
492
- return exitCode;
493
- }
494
- // ---------------------------------------------------------------------------
495
- // --status handler
496
- // ---------------------------------------------------------------------------
497
- async function handleStatus(port) {
498
- const baseUrl = `http://127.0.0.1:${port}`;
499
- const detection = await detectWebManager(port);
500
- if (!detection.running) {
501
- process.stdout.write(`manager: not running (${baseUrl})\n`);
502
- return 1;
503
- }
504
- try {
505
- const res = await httpGet(`${baseUrl}/api/state`);
506
- if (res.status !== 200) {
507
- process.stdout.write(`manager: not running (${baseUrl})\n`);
508
- return 1;
509
- }
510
- const state = JSON.parse(res.body);
511
- const version = state.version ? ` (v${state.version})` : '';
512
- process.stdout.write(`manager: running${version}\n`);
513
- process.stdout.write(`project: ${state.projectName ?? 'unknown'}\n`);
514
- process.stdout.write(`busy: ${state.busy ? 'true' : 'false'}\n`);
515
- if (state.phases) {
516
- const phaseStr = Object.entries(state.phases)
517
- .map(([phase, st]) => `${phase}=${st}`)
518
- .join(' ');
519
- process.stdout.write(`phases: ${phaseStr}\n`);
520
- }
521
- return 0;
522
- }
523
- catch {
524
- process.stdout.write(`manager: not running (${baseUrl})\n`);
525
- return 1;
526
- }
527
- }
528
- function formatJobDuration(ms) {
529
- if (ms == null)
530
- return '-';
531
- return formatDuration(ms);
532
- }
533
- function formatJobStarted(isoStr) {
534
- try {
535
- const d = new Date(isoStr);
536
- const year = d.getFullYear();
537
- const month = String(d.getMonth() + 1).padStart(2, '0');
538
- const day = String(d.getDate()).padStart(2, '0');
539
- const hour = String(d.getHours()).padStart(2, '0');
540
- const min = String(d.getMinutes()).padStart(2, '0');
541
- return `${year}-${month}-${day} ${hour}:${min}`;
542
- }
543
- catch {
544
- return isoStr.slice(0, 16);
545
- }
546
- }
547
- async function handleJobs(port) {
548
- const baseUrl = `http://127.0.0.1:${port}`;
549
- const detection = await detectWebManager(port);
550
- if (!detection.running) {
551
- hubError(`manager is not running (${baseUrl})`);
552
- return 1;
553
- }
554
- let res;
555
- try {
556
- res = await httpGet(`${baseUrl}/api/jobs`);
557
- }
558
- catch {
559
- hubError('failed to fetch job list');
560
- return 1;
561
- }
562
- if (res.status === 501 || res.status === 404) {
563
- hubLog('jobs history requires manager with SQLite persistence (#57)');
564
- return 1;
565
- }
566
- if (res.status !== 200) {
567
- hubError(`unexpected response from /api/jobs: HTTP ${res.status}`);
568
- return 1;
569
- }
570
- let data;
571
- try {
572
- data = JSON.parse(res.body);
573
- }
574
- catch {
575
- hubError('invalid response from /api/jobs');
576
- return 1;
577
- }
578
- if (!data.jobs || data.jobs.length === 0) {
579
- hubLog('no jobs recorded yet');
580
- return 0;
581
- }
582
- // Column widths
583
- const idW = 8;
584
- const cmdW = 30;
585
- const startW = 18;
586
- const durW = 8;
587
- const exitW = 4;
588
- const header = [
589
- 'ID'.padEnd(idW),
590
- 'COMMAND'.padEnd(cmdW),
591
- 'STARTED'.padEnd(startW),
592
- 'DURATION'.padEnd(durW),
593
- 'EXIT'.padEnd(exitW),
594
- ].join(' ');
595
- process.stdout.write(`${bold(header)}\n`);
596
- for (const job of data.jobs) {
597
- const idCell = job.id.slice(0, idW).padEnd(idW);
598
- const cmdCell = job.command.slice(0, cmdW).padEnd(cmdW);
599
- const startCell = formatJobStarted(job.started_at).padEnd(startW);
600
- const durCell = formatJobDuration(job.duration_ms).padEnd(durW);
601
- const exitCell = (job.exit_code ?? '-').toString().padEnd(exitW);
602
- process.stdout.write(`${idCell} ${cmdCell} ${startCell} ${durCell} ${exitCell}\n`);
603
- }
604
- return 0;
605
- }
606
- // ---------------------------------------------------------------------------
607
- // Hub subcommand group
608
- // ---------------------------------------------------------------------------
609
- const HUB_PID_FILE = path_1.default.join(os_1.default.homedir(), '.specrails', 'manager.pid');
610
- function readPid() {
611
- try {
612
- const raw = fs_1.default.readFileSync(HUB_PID_FILE, 'utf-8').trim();
613
- const pid = parseInt(raw, 10);
614
- return isNaN(pid) ? null : pid;
615
- }
616
- catch {
617
- return null;
618
- }
619
- }
620
- function isProcessRunning(pid) {
621
- try {
622
- process.kill(pid, 0);
623
- return true;
624
- }
625
- catch {
626
- return false;
627
- }
628
- }
629
- function hubServerPath() {
630
- // cli/dist/hub.js → __dirname is cli/dist/ → go up two levels to package root
631
- const base = path_1.default.resolve(__dirname, '..', '..');
632
- // npm install: compiled server at dist/server/index.js
633
- const compiled = path_1.default.join(base, 'dist', 'server', 'index.js');
634
- // dev fallback: run TypeScript source directly via tsx
635
- const devTs = path_1.default.join(base, 'server', 'index.ts');
636
- if (fs_1.default.existsSync(compiled))
637
- return compiled;
638
- if (fs_1.default.existsSync(devTs))
639
- return devTs;
640
- return compiled;
641
- }
642
- async function hubStart(port) {
643
- const pid = readPid();
644
- if (pid !== null && isProcessRunning(pid)) {
645
- hubLog(`hub already running (pid ${pid}) on port ${port}`);
646
- return 0;
647
- }
648
- const serverPath = hubServerPath();
649
- const isTs = serverPath.endsWith('.ts');
650
- const args = isTs
651
- ? ['tsx', serverPath, '--port', String(port)]
652
- : ['node', serverPath, '--port', String(port)];
653
- const child = (0, child_process_2.spawn)(args[0], args.slice(1), {
654
- detached: true,
655
- stdio: 'ignore',
656
- env: { ...process.env },
657
- });
658
- child.unref();
659
- // Poll until the hub is ready (up to 15 seconds)
660
- const deadline = Date.now() + 15_000;
661
- const pollInterval = 500;
662
- await new Promise((resolve) => setTimeout(resolve, 500));
663
- while (Date.now() < deadline) {
664
- const detection = await detectWebManager(port);
665
- if (detection.running) {
666
- hubLog(`hub started on http://127.0.0.1:${port}`);
667
- return 0;
668
- }
669
- await new Promise((resolve) => setTimeout(resolve, pollInterval));
670
- }
671
- hubError('hub failed to start (check logs)');
672
- return 1;
673
- }
674
- async function hubStop() {
675
- const pid = readPid();
676
- if (pid === null) {
677
- hubLog('hub is not running (no pid file)');
678
- return 0;
679
- }
680
- if (!isProcessRunning(pid)) {
681
- hubLog('hub is not running (stale pid file)');
682
- try {
683
- fs_1.default.unlinkSync(HUB_PID_FILE);
684
- }
685
- catch { /* ignore */ }
686
- return 0;
687
- }
688
- try {
689
- process.kill(pid, 'SIGTERM');
690
- hubLog(`hub stopped (pid ${pid})`);
691
- return 0;
692
- }
693
- catch (err) {
694
- hubError(`failed to stop hub: ${err.message}`);
695
- return 1;
696
- }
697
- }
698
- async function hubStatus(port) {
699
- const pid = readPid();
700
- const detection = await detectWebManager(port);
701
- if (!detection.running) {
702
- process.stdout.write(`hub: not running\n`);
703
- return 1;
704
- }
705
- try {
706
- const res = await httpGet(`${detection.baseUrl}/api/hub/state`);
707
- const state = JSON.parse(res.body);
708
- process.stdout.write(`hub: running (pid ${pid ?? '?'}) on ${detection.baseUrl}\n`);
709
- process.stdout.write(`projects: ${state.projectCount ?? 0}\n`);
710
- if (state.projects) {
711
- for (const p of state.projects) {
712
- process.stdout.write(` - ${p.name}\n`);
713
- }
714
- }
715
- return 0;
716
- }
717
- catch {
718
- process.stdout.write(`hub: running on ${detection.baseUrl}\n`);
719
- return 0;
720
- }
721
- }
722
- async function hubAdd(projectPath, port) {
723
- const detection = await detectWebManager(port);
724
- if (!detection.running) {
725
- hubError('hub is not running. Start it first with: specrails-hub start');
726
- return 1;
727
- }
728
- try {
729
- const res = await httpPost(`${detection.baseUrl}/api/hub/projects`, {
730
- path: path_1.default.resolve(projectPath),
731
- });
732
- if (res.status === 201) {
733
- const data = JSON.parse(res.body);
734
- hubLog(`added project: ${data.project?.name ?? projectPath}`);
735
- return 0;
736
- }
737
- else if (res.status === 409) {
738
- hubLog('project already registered');
739
- return 0;
740
- }
741
- else {
742
- let errMsg = `HTTP ${res.status}`;
743
- try {
744
- errMsg = JSON.parse(res.body).error ?? errMsg;
745
- }
746
- catch { /* use default */ }
747
- hubError(`failed to add project: ${errMsg}`);
748
- return 1;
749
- }
750
- }
751
- catch (err) {
752
- hubError(`failed to connect to hub: ${err.message}`);
753
- return 1;
754
- }
755
- }
756
- async function hubRemove(projectId, port) {
757
- const detection = await detectWebManager(port);
758
- if (!detection.running) {
759
- hubError('hub is not running');
760
- return 1;
761
- }
762
- try {
763
- const deleteRes = await new Promise((resolve, reject) => {
764
- const urlObj = new URL(`${detection.baseUrl}/api/hub/projects/${projectId}`);
765
- const options = {
766
- hostname: urlObj.hostname,
767
- port: urlObj.port,
768
- path: urlObj.pathname,
769
- method: 'DELETE',
770
- };
771
- const req = http_1.default.request(options, (res) => {
772
- let body = '';
773
- res.on('data', (chunk) => { body += chunk; });
774
- res.on('end', () => resolve({ status: res.statusCode ?? 0, body }));
775
- });
776
- req.on('error', reject);
777
- req.end();
778
- });
779
- if (deleteRes.status === 200) {
780
- hubLog(`project removed`);
781
- return 0;
782
- }
783
- else {
784
- hubError(`failed to remove project: HTTP ${deleteRes.status}`);
785
- return 1;
786
- }
787
- }
788
- catch (err) {
789
- hubError(`failed to connect to hub: ${err.message}`);
790
- return 1;
791
- }
792
- }
793
- async function hubList(port) {
794
- const detection = await detectWebManager(port);
795
- if (!detection.running) {
796
- hubError('hub is not running');
797
- return 1;
798
- }
799
- try {
800
- const res = await httpGet(`${detection.baseUrl}/api/hub/projects`);
801
- const data = JSON.parse(res.body);
802
- if (!data.projects || data.projects.length === 0) {
803
- hubLog('no projects registered');
804
- return 0;
805
- }
806
- const idW = 36;
807
- const nameW = 24;
808
- process.stdout.write(`${bold('ID'.padEnd(idW))} ${bold('NAME'.padEnd(nameW))} ${bold('PATH')}\n`);
809
- for (const p of data.projects) {
810
- process.stdout.write(`${p.id.padEnd(idW)} ${p.name.padEnd(nameW)} ${p.path}\n`);
811
- }
812
- return 0;
813
- }
814
- catch (err) {
815
- hubError(`failed to fetch projects: ${err.message}`);
816
- return 1;
817
- }
818
- }
819
- async function handleHub(subArgs, port) {
820
- const sub = subArgs[0];
821
- if (!sub || sub === 'help' || sub === '--help' || sub === '-h') {
822
- process.stdout.write(`
823
- ${bold('specrails-hub')} — manage the specrails hub (multi-project mode)
824
-
825
- ${bold('Usage:')}
826
- specrails-hub start Start the hub server
827
- specrails-hub stop Stop the hub server
828
- specrails-hub status Show hub status and registered projects
829
- specrails-hub add <path> Register a project by path
830
- specrails-hub remove <id> Unregister a project by ID
831
- specrails-hub list List all registered projects
832
- `.trimStart());
833
- return 0;
834
- }
835
- if (sub === 'start') {
836
- return hubStart(port);
837
- }
838
- if (sub === 'stop') {
839
- return hubStop();
840
- }
841
- if (sub === 'status') {
842
- return hubStatus(port);
843
- }
844
- if (sub === 'add') {
845
- const projectPath = subArgs[1];
846
- if (!projectPath) {
847
- hubError('usage: specrails-hub add <path>');
848
- return 1;
849
- }
850
- return hubAdd(projectPath, port);
851
- }
852
- if (sub === 'remove') {
853
- const projectId = subArgs[1];
854
- if (!projectId) {
855
- hubError('usage: specrails-hub remove <id>');
856
- return 1;
857
- }
858
- return hubRemove(projectId, port);
859
- }
860
- if (sub === 'list') {
861
- return hubList(port);
862
- }
863
- hubError(`unknown hub subcommand: ${sub}`);
864
- return 1;
865
- }
866
- // ---------------------------------------------------------------------------
867
- // Main entry point
868
- // ---------------------------------------------------------------------------
869
- async function main() {
870
- const argv = process.argv.slice(2);
871
- const parsed = parseArgs(argv);
872
- if (parsed.mode === 'help') {
873
- printHelp();
874
- process.exit(0);
875
- }
876
- if (parsed.mode === 'status') {
877
- const code = await handleStatus(parsed.port);
878
- process.exit(code);
879
- }
880
- if (parsed.mode === 'jobs') {
881
- const code = await handleJobs(parsed.port);
882
- process.exit(code);
883
- }
884
- if (parsed.mode === 'hub') {
885
- const code = await handleHub(parsed.subArgs, parsed.port);
886
- process.exit(code);
887
- }
888
- // Command or raw: resolve command string
889
- const command = parsed.resolved;
890
- const port = parsed.port;
891
- hubLog(`running: ${command}`);
892
- const detection = await detectWebManager(port);
893
- let exitCode;
894
- if (detection.running) {
895
- hubLog(`routing via manager at ${detection.baseUrl}`);
896
- exitCode = await runViaWebManager(command, detection.baseUrl);
897
- }
898
- else {
899
- hubLog('manager not running — invoking claude directly');
900
- exitCode = await runDirect(command);
901
- }
902
- process.exit(exitCode);
903
- }
904
- // Only run main() when this file is executed directly (not when imported in tests)
905
- if (require.main === module) {
906
- main().catch((err) => {
907
- hubError(err.message ?? String(err));
908
- process.exit(1);
909
- });
910
- }