@agentbridge1/cli 0.0.7 → 0.0.8

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.
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MCP_SERVER_NAME = void 0;
4
+ exports.buildLocalMcpServerEntry = buildLocalMcpServerEntry;
5
+ exports.buildServerMcpServerEntry = buildServerMcpServerEntry;
6
+ exports.mergeAgentbridgeIntoCursorMcp = mergeAgentbridgeIntoCursorMcp;
7
+ exports.writeLocalMcpConfig = writeLocalMcpConfig;
8
+ exports.writeServerMcpConfig = writeServerMcpConfig;
9
+ const node_fs_1 = require("node:fs");
10
+ const node_path_1 = require("node:path");
11
+ const node_process_1 = require("node:process");
12
+ const mcp_runtime_1 = require("./mcp-runtime");
13
+ exports.MCP_SERVER_NAME = "agentbridge";
14
+ function buildLocalMcpServerEntry(cliDistDir) {
15
+ const runtime = (0, mcp_runtime_1.resolveMcpRuntime)(cliDistDir);
16
+ const repoRoot = (0, node_path_1.resolve)((0, node_process_1.cwd)());
17
+ const entry = runtime
18
+ ? { command: runtime.command, args: runtime.args }
19
+ : { command: "agentbridge", args: ["mcp"] };
20
+ entry.env = { AGENTBRIDGE_REPO_ROOT: repoRoot };
21
+ return entry;
22
+ }
23
+ function buildServerMcpServerEntry(projectId, apiKey, apiBaseUrl) {
24
+ return {
25
+ command: "agentbridge",
26
+ args: ["mcp"],
27
+ env: {
28
+ AGENTBRIDGE_PROJECT_ID: projectId,
29
+ AGENTBRIDGE_API_KEY: apiKey,
30
+ AGENTBRIDGE_BASE_URL: apiBaseUrl,
31
+ AGENTBRIDGE_REPO_ROOT: (0, node_path_1.resolve)((0, node_process_1.cwd)()),
32
+ },
33
+ };
34
+ }
35
+ /** Merge AgentBridge into project-level `.cursor/mcp.json` (preserves other servers). */
36
+ function mergeAgentbridgeIntoCursorMcp(entry) {
37
+ const cursorDir = (0, node_path_1.join)((0, node_process_1.cwd)(), ".cursor");
38
+ const mcpPath = (0, node_path_1.join)(cursorDir, "mcp.json");
39
+ let existing = {};
40
+ if ((0, node_fs_1.existsSync)(mcpPath)) {
41
+ try {
42
+ existing = JSON.parse((0, node_fs_1.readFileSync)(mcpPath, "utf8"));
43
+ }
44
+ catch {
45
+ // Malformed file — start fresh
46
+ }
47
+ }
48
+ const mcpServers = existing.mcpServers && typeof existing.mcpServers === "object"
49
+ ? existing.mcpServers
50
+ : {};
51
+ mcpServers[exports.MCP_SERVER_NAME] = entry;
52
+ const updated = { ...existing, mcpServers };
53
+ if (!(0, node_fs_1.existsSync)(cursorDir)) {
54
+ (0, node_fs_1.mkdirSync)(cursorDir, { recursive: true });
55
+ }
56
+ (0, node_fs_1.writeFileSync)(mcpPath, JSON.stringify(updated, null, 2) + "\n", "utf8");
57
+ return mcpPath;
58
+ }
59
+ function writeLocalMcpConfig(cliDistDir) {
60
+ return mergeAgentbridgeIntoCursorMcp(buildLocalMcpServerEntry(cliDistDir));
61
+ }
62
+ function writeServerMcpConfig(projectId, apiKey, apiBaseUrl) {
63
+ return mergeAgentbridgeIntoCursorMcp(buildServerMcpServerEntry(projectId, apiKey, apiBaseUrl));
64
+ }
@@ -1,5 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.STALE_MCP_ACTIVITY_MINUTES = void 0;
4
+ exports.resolveWatchStartupSupervision = resolveWatchStartupSupervision;
5
+ exports.computeMcpActivityStaleMinutes = computeMcpActivityStaleMinutes;
6
+ exports.effectiveWatchSupervisionStatus = effectiveWatchSupervisionStatus;
7
+ exports.buildWatchSupervisionRenderContext = buildWatchSupervisionRenderContext;
3
8
  exports.inferSupervisionWorkType = inferSupervisionWorkType;
4
9
  exports.requiredProofHints = requiredProofHints;
5
10
  exports.computeScopeDriftAlerts = computeScopeDriftAlerts;
@@ -8,10 +13,113 @@ exports.fallbackSupervisionSnapshot = fallbackSupervisionSnapshot;
8
13
  exports.enrichSupervisionWithLocalState = enrichSupervisionWithLocalState;
9
14
  exports.supervisionSignature = supervisionSignature;
10
15
  exports.renderSupervisionSummary = renderSupervisionSummary;
16
+ const node_fs_1 = require("node:fs");
17
+ const node_path_1 = require("node:path");
11
18
  const claimed_paths_1 = require("./claimed-paths");
12
19
  const domain_resolution_1 = require("./domain-resolution");
13
20
  const preflight_changed_files_1 = require("./preflight-changed-files");
14
21
  const memory_context_render_1 = require("./memory-context-render");
22
+ /** Minutes without MCP activity before watch downgrades READY → DEGRADED. */
23
+ exports.STALE_MCP_ACTIVITY_MINUTES = 15;
24
+ function readWatchMcpConfig(workspaceRoot) {
25
+ const mcpConfigPath = (0, node_path_1.resolve)(workspaceRoot, ".cursor", "mcp.json");
26
+ if (!(0, node_fs_1.existsSync)(mcpConfigPath)) {
27
+ return { configured: false, projectId: null };
28
+ }
29
+ try {
30
+ const parsed = JSON.parse((0, node_fs_1.readFileSync)(mcpConfigPath, "utf8"));
31
+ const entry = parsed?.mcpServers?.["agentbridge"] ?? parsed?.mcpServers?.["agentbridge-mcp"];
32
+ return {
33
+ configured: Boolean(entry),
34
+ projectId: entry?.env?.AGENTBRIDGE_PROJECT_ID ?? null,
35
+ };
36
+ }
37
+ catch {
38
+ return { configured: false, projectId: null };
39
+ }
40
+ }
41
+ function watchRulesInstalled(workspaceRoot) {
42
+ return ((0, node_fs_1.existsSync)((0, node_path_1.resolve)(workspaceRoot, "AGENTBRIDGE.md")) ||
43
+ (0, node_fs_1.existsSync)((0, node_path_1.resolve)(workspaceRoot, ".cursor", "rules", "agentbridge.mdc")));
44
+ }
45
+ /** Startup decision tree for watch's four-state supervision model. */
46
+ function resolveWatchStartupSupervision(input) {
47
+ const mcp = readWatchMcpConfig(input.workspaceRoot);
48
+ const rulesInstalled = watchRulesInstalled(input.workspaceRoot);
49
+ const credentialMismatch = mcp.configured && mcp.projectId !== null && input.cliProjectId !== undefined
50
+ ? mcp.projectId !== input.cliProjectId
51
+ : false;
52
+ const base = {
53
+ mcpConfigured: mcp.configured,
54
+ rulesInstalled,
55
+ mcpProjectId: mcp.projectId,
56
+ credentialMismatch,
57
+ };
58
+ if (credentialMismatch) {
59
+ return {
60
+ ...base,
61
+ supervisionStatus: "broken",
62
+ warningBanner: "\n⚠ SUPERVISION BROKEN: CLI and MCP are using different project identities.\n" +
63
+ ` CLI project id: ${input.cliProjectId ?? "?"}\n` +
64
+ ` MCP project id: ${mcp.projectId ?? "?"}\n` +
65
+ " watch cannot reliably correlate CLI sessions with MCP evidence.\n" +
66
+ " Fix: run `agentbridge connect` to re-write the MCP config with the correct credentials.\n",
67
+ };
68
+ }
69
+ if (!mcp.configured && !rulesInstalled) {
70
+ return {
71
+ ...base,
72
+ supervisionStatus: "blind",
73
+ warningBanner: "\n⚠ SUPERVISION BLIND: MCP is not configured and no AgentBridge rules are installed.\n" +
74
+ " watch can observe filesystem changes only — agent actions inside Cursor are invisible.\n" +
75
+ " Fix: run `agentbridge connect` to configure MCP and install rules automatically.\n",
76
+ };
77
+ }
78
+ if (!mcp.configured) {
79
+ return {
80
+ ...base,
81
+ supervisionStatus: "degraded",
82
+ warningBanner: "\n⚠ SUPERVISION DEGRADED: MCP is not configured.\n" +
83
+ " watch cannot see MCP-recorded evidence, implementation packets, or agent tool usage.\n" +
84
+ " Fix: run `agentbridge connect` or `agentbridge setup-mcp` then restart Cursor.\n",
85
+ };
86
+ }
87
+ if (!rulesInstalled) {
88
+ return {
89
+ ...base,
90
+ supervisionStatus: "degraded",
91
+ warningBanner: "\n⚠ SUPERVISION DEGRADED: AgentBridge rules are not installed in this project.\n" +
92
+ " The agent is not operating under AgentBridge protocol — no task discipline enforced.\n" +
93
+ " Fix: run `agentbridge install-rules` to add rules to this project.\n",
94
+ };
95
+ }
96
+ return { ...base, supervisionStatus: "ready" };
97
+ }
98
+ function computeMcpActivityStaleMinutes(updatedAt, nowMs = Date.now()) {
99
+ if (updatedAt == null)
100
+ return undefined;
101
+ const updatedMs = new Date(updatedAt).getTime();
102
+ if (Number.isNaN(updatedMs))
103
+ return undefined;
104
+ return Math.floor((nowMs - updatedMs) / 60_000);
105
+ }
106
+ function effectiveWatchSupervisionStatus(baseStatus, mcpConfigured, mcpActivityStaleMinutes) {
107
+ if (baseStatus === "ready" &&
108
+ mcpConfigured &&
109
+ mcpActivityStaleMinutes != null &&
110
+ mcpActivityStaleMinutes > exports.STALE_MCP_ACTIVITY_MINUTES) {
111
+ return "degraded";
112
+ }
113
+ return baseStatus;
114
+ }
115
+ function buildWatchSupervisionRenderContext(input) {
116
+ const mcpActivityStaleMinutes = computeMcpActivityStaleMinutes(input.agentDeclaredUpdatedAt, input.nowMs);
117
+ return {
118
+ supervisionStatus: effectiveWatchSupervisionStatus(input.baseStatus, input.mcpConfigured, mcpActivityStaleMinutes),
119
+ mcpConfigured: input.mcpConfigured,
120
+ mcpActivityStaleMinutes,
121
+ };
122
+ }
15
123
  const CLI_GIT_REQUIRED_PROOF = [
16
124
  "tracked modified file handled",
17
125
  "untracked new file handled",
@@ -28,6 +136,20 @@ function inferSupervisionWorkType(changedFiles) {
28
136
  if (changedFiles.length === 0)
29
137
  return "unknown";
30
138
  const lower = changedFiles.map((file) => file.toLowerCase());
139
+ const isUiSurfaceFile = (file) => {
140
+ if (file.endsWith(".html") ||
141
+ file.endsWith(".css") ||
142
+ file.endsWith(".scss") ||
143
+ file.endsWith(".sass") ||
144
+ file.endsWith(".less") ||
145
+ file.endsWith(".tsx") ||
146
+ file.endsWith(".jsx")) {
147
+ return true;
148
+ }
149
+ // Keep this narrow: only treat plain .ts/.js as UI when clearly in public assets.
150
+ return ((file.endsWith(".ts") || file.endsWith(".js")) &&
151
+ (file.startsWith("src/public/") || file.includes("/public/")));
152
+ };
31
153
  const cliSignal = lower.some((file) => file.includes("cli/src/") &&
32
154
  (file.includes("watch") || file.includes("precommit") || file.includes("revert-crossing")));
33
155
  if (cliSignal)
@@ -35,6 +157,9 @@ function inferSupervisionWorkType(changedFiles) {
35
157
  const docsOnly = lower.every((file) => file.startsWith("docs/") || file.endsWith(".md") || file.endsWith(".mdx") || file.includes("/docs/"));
36
158
  if (docsOnly)
37
159
  return "documentation";
160
+ const uiSurfaceOnly = lower.every(isUiSurfaceFile);
161
+ if (uiSurfaceOnly)
162
+ return "ui_copy";
38
163
  return "general";
39
164
  }
40
165
  function requiredProofHints(workType) {
@@ -42,6 +167,12 @@ function requiredProofHints(workType) {
42
167
  return [...CLI_GIT_REQUIRED_PROOF];
43
168
  if (workType === "documentation")
44
169
  return ["explicit verification evidence for changed docs/files"];
170
+ if (workType === "ui_copy") {
171
+ return [
172
+ "visual confirmation/screenshot of updated UI copy",
173
+ "optional quick smoke command: agentbridge verify -- npm run build",
174
+ ];
175
+ }
45
176
  return [
46
177
  "explicit verification evidence for changed files",
47
178
  "run agentbridge verify, then rerun agentbridge watch",
@@ -210,7 +341,7 @@ function enrichSupervisionWithLocalState(state, supervision) {
210
341
  : supervision.scopeStatus,
211
342
  };
212
343
  }
213
- function supervisionSignature(snapshot) {
344
+ function supervisionSignature(snapshot, renderContext) {
214
345
  return JSON.stringify({
215
346
  workSessionId: snapshot.workSessionId,
216
347
  changedFiles: snapshot.changedFiles,
@@ -225,52 +356,81 @@ function supervisionSignature(snapshot) {
225
356
  ? `${snapshot.agentDeclared.intent ?? ""}:${snapshot.agentDeclared.lastEvent ?? ""}`
226
357
  : "",
227
358
  declaredDrift: snapshot.declaredDriftFiles?.join(",") ?? "",
359
+ supervisionStatus: renderContext?.supervisionStatus ?? "",
360
+ mcpConfigured: renderContext?.mcpConfigured ?? false,
361
+ mcpActivityStale: renderContext?.mcpActivityStaleMinutes != null
362
+ ? renderContext.mcpActivityStaleMinutes > exports.STALE_MCP_ACTIVITY_MINUTES
363
+ : false,
228
364
  });
229
365
  }
366
+ const SUPERVISION_STATUS_LABEL = {
367
+ ready: "✓ READY",
368
+ degraded: "⚠ DEGRADED",
369
+ blind: "⚠ BLIND",
370
+ broken: "✗ BROKEN",
371
+ };
230
372
  function renderSupervisionSummary(snapshot, options) {
231
373
  const lines = [];
232
- lines.push(options?.compact ? "AgentBridge watch:" : "AgentBridge supervision:");
374
+ lines.push("AgentBridge watch:");
375
+ lines.push("");
376
+ lines.push("1) Actions performed");
377
+ lines.push(`- Reviewed ${snapshot.changedFiles.length} changed file(s)`);
378
+ lines.push(`- Work type detected: ${snapshot.workType}`);
379
+ lines.push(`- Scope status: ${snapshot.scopeStatus}`);
380
+ lines.push(`- Boundary status: ${snapshot.boundaryStatus}`);
381
+ lines.push(`- Files (disk): ${snapshot.diskObserved?.postBaselineFiles.length ?? snapshot.changedFiles.length} changed since session start`);
233
382
  if (!options?.compact) {
234
- lines.push(`Current run: ${snapshot.workSessionId}${snapshot.changeRequestId ? ` / task ${snapshot.changeRequestId}` : ""}`);
383
+ lines.push(`- Active run: ${snapshot.workSessionId}${snapshot.changeRequestId ? ` (task ${snapshot.changeRequestId})` : ""}`);
384
+ }
385
+ if (options?.supervisionStatus) {
386
+ lines.push(`- Supervision: ${SUPERVISION_STATUS_LABEL[options.supervisionStatus] ?? options.supervisionStatus}`);
235
387
  }
236
388
  const declared = snapshot.agentDeclared;
237
389
  if (declared) {
238
390
  const intentLabel = declared.intent?.trim() || declared.summary?.trim() || "(no intent text)";
239
- const eventSuffix = declared.lastEvent && declared.updatedAt
240
- ? ` [${declared.lastEvent} @ ${formatMcpEventTime(declared.updatedAt)}]`
241
- : declared.lastEvent
242
- ? ` [${declared.lastEvent}]`
243
- : "";
244
- lines.push(`Agent (MCP): ${intentLabel}${eventSuffix}`);
391
+ lines.push(`- Agent (MCP): ${intentLabel}`);
245
392
  if (declared.claimedPaths.length > 0) {
246
393
  const scopePreview = declared.claimedPaths.length > 6
247
394
  ? `${declared.claimedPaths.slice(0, 6).join(", ")} (+${declared.claimedPaths.length - 6} more)`
248
395
  : declared.claimedPaths.join(", ");
249
- lines.push(`Declared scope: ${scopePreview}`);
250
- }
251
- else {
252
- lines.push("Declared scope: (none yet)");
396
+ lines.push(`- Declared scope: ${scopePreview}`);
253
397
  }
254
398
  }
255
- const diskCount = snapshot.diskObserved?.postBaselineFiles.length ?? snapshot.changedFiles.length;
256
- lines.push(`Files (disk): ${diskCount} changed since session start`);
257
- if (snapshot.declaredVsObservedDrift === "warn" && snapshot.declaredDriftFiles?.length) {
258
- const preview = snapshot.declaredDriftFiles.length > 4
259
- ? `${snapshot.declaredDriftFiles.slice(0, 4).join(", ")} (+${snapshot.declaredDriftFiles.length - 4} more)`
260
- : snapshot.declaredDriftFiles.join(", ");
261
- lines.push(`Note: ${snapshot.declaredDriftFiles.length} file(s) outside declared scope (${preview}) — warn only, not blocked`);
399
+ else if (options?.mcpConfigured) {
400
+ lines.push("- Agent (MCP): not yet active");
401
+ }
402
+ lines.push(`- Changed files: ${snapshot.changedFiles.length}`);
403
+ lines.push("");
404
+ lines.push("2) Proof present / missing");
405
+ if (snapshot.decision === "accepted") {
406
+ lines.push("- Present: proof accepted for current changes");
407
+ }
408
+ else if (snapshot.serverAcceptanceUnavailable) {
409
+ lines.push(`- Unknown: proof check unavailable (${snapshot.serverAcceptanceUnavailable})`);
410
+ }
411
+ else {
412
+ lines.push(`- Missing/blocked: watch result is ${snapshot.decision}`);
262
413
  }
263
- lines.push(`Changed files: ${snapshot.changedFiles.length}`);
264
- lines.push(`Detected work type: ${snapshot.workType}`);
265
- lines.push(`Scope: ${snapshot.scopeStatus}`);
266
- lines.push(`Boundary: ${snapshot.boundaryStatus}`);
267
- lines.push(`Watch result: ${snapshot.decision}`);
268
414
  if (snapshot.requiredProof.length > 0) {
269
- lines.push("");
270
- lines.push("Likely required proof:");
271
- for (const proof of snapshot.requiredProof) {
272
- lines.push(`- ${proof}`);
273
- }
415
+ lines.push(`- Still needed: ${snapshot.requiredProof.slice(0, 3).join("; ")}`);
416
+ }
417
+ if (snapshot.declaredVsObservedDrift === "warn" && snapshot.declaredDriftFiles?.length) {
418
+ lines.push(`- Scope warning: ${snapshot.declaredDriftFiles.length} file(s) appear outside declared scope`);
419
+ lines.push("- Note: warn only, not blocked");
420
+ }
421
+ if (snapshot.serverAcceptanceUnavailable) {
422
+ lines.push(`- Watch result unavailable: ${snapshot.serverAcceptanceUnavailable}`);
423
+ }
424
+ lines.push("");
425
+ lines.push("3) Next move");
426
+ lines.push(`- ${snapshot.nextAction}`);
427
+ if (snapshot.driftAlerts.length > 0) {
428
+ lines.push("- Resolve out-of-scope files or explicitly update scope before continuing");
429
+ }
430
+ if (options?.mcpActivityStaleMinutes != null &&
431
+ options.mcpActivityStaleMinutes > exports.STALE_MCP_ACTIVITY_MINUTES) {
432
+ lines.push("- Reconnect or wake MCP activity so supervision returns to ready state");
433
+ lines.push(`- MCP activity stale — last update ${options.mcpActivityStaleMinutes}m ago`);
274
434
  }
275
435
  if (snapshot.knownTraps.length > 0) {
276
436
  lines.push("");
@@ -282,25 +442,8 @@ function renderSupervisionSummary(snapshot, options) {
282
442
  const watchMemoryLines = (0, memory_context_render_1.renderWatchMemoryContext)(snapshot.memoryContext);
283
443
  if (watchMemoryLines.length > 0) {
284
444
  lines.push("");
445
+ lines.push("Context:");
285
446
  lines.push(...watchMemoryLines);
286
447
  }
287
- if (snapshot.driftAlerts.length > 0) {
288
- for (const alert of snapshot.driftAlerts) {
289
- lines.push("");
290
- lines.push("Files outside scope detected:");
291
- lines.push(`- file: ${alert.file}`);
292
- lines.push(`- claimed paths: [${alert.claimedPaths.join(", ")}]`);
293
- lines.push(`- likely domain: ${alert.likelyDomain}`);
294
- lines.push(`- severity: ${alert.severity}`);
295
- lines.push("- suggested action: update scope or confirm this belongs to the current work");
296
- }
297
- }
298
- if (snapshot.serverAcceptanceUnavailable) {
299
- lines.push("");
300
- lines.push(`Watch result unavailable: ${snapshot.serverAcceptanceUnavailable}`);
301
- }
302
- lines.push("");
303
- lines.push("Before saying done:");
304
- lines.push(`- ${snapshot.nextAction}`);
305
448
  return lines.join("\n");
306
449
  }
@@ -1,44 +1,152 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.detectTestCommand = detectTestCommand;
4
+ exports.buildScopedCommand = buildScopedCommand;
5
+ exports.extractFailedTestNames = extractFailedTestNames;
4
6
  exports.runDetectedTests = runDetectedTests;
7
+ exports.extractIntentKeywords = extractIntentKeywords;
8
+ exports.tokenizePath = tokenizePath;
9
+ exports.analyzeCoherence = analyzeCoherence;
5
10
  const node_fs_1 = require("node:fs");
6
11
  const node_path_1 = require("node:path");
7
12
  const node_child_process_1 = require("node:child_process");
13
+ /**
14
+ * Detect the appropriate test command for the project rooted at `cwd`.
15
+ * Checks config override first, then language-specific conventions.
16
+ */
8
17
  function detectTestCommand(cwd = process.cwd()) {
18
+ // 1. Check .agentbridge.json for an explicit verify.command override
19
+ const abConfigPath = (0, node_path_1.resolve)(cwd, ".agentbridge.json");
20
+ if ((0, node_fs_1.existsSync)(abConfigPath)) {
21
+ try {
22
+ const cfg = JSON.parse((0, node_fs_1.readFileSync)(abConfigPath, "utf8"));
23
+ if (cfg.verify?.command)
24
+ return cfg.verify.command;
25
+ }
26
+ catch {
27
+ // malformed config — fall through
28
+ }
29
+ }
30
+ // 2. Node / npm — prefer test:unit for speed
9
31
  const packageJsonPath = (0, node_path_1.resolve)(cwd, "package.json");
10
- if (!(0, node_fs_1.existsSync)(packageJsonPath))
11
- return null;
12
- const pkg = JSON.parse((0, node_fs_1.readFileSync)(packageJsonPath, "utf8"));
13
- if (!pkg.scripts)
14
- return null;
15
- if (pkg.scripts["test:unit"])
16
- return "npm run test:unit";
17
- if (pkg.scripts.test)
18
- return "npm run test";
32
+ if ((0, node_fs_1.existsSync)(packageJsonPath)) {
33
+ try {
34
+ const pkg = JSON.parse((0, node_fs_1.readFileSync)(packageJsonPath, "utf8"));
35
+ if (pkg.scripts?.["test:unit"])
36
+ return "npm run test:unit";
37
+ if (pkg.scripts?.["test"])
38
+ return "npm run test";
39
+ }
40
+ catch {
41
+ // malformed package.json — fall through
42
+ }
43
+ }
44
+ // 3. Python
45
+ if ((0, node_fs_1.existsSync)((0, node_path_1.resolve)(cwd, "pytest.ini")) || (0, node_fs_1.existsSync)((0, node_path_1.resolve)(cwd, "pyproject.toml"))) {
46
+ return "pytest";
47
+ }
48
+ // 4. Rust
49
+ if ((0, node_fs_1.existsSync)((0, node_path_1.resolve)(cwd, "Cargo.toml"))) {
50
+ return "cargo test";
51
+ }
52
+ // 5. Go
53
+ if ((0, node_fs_1.existsSync)((0, node_path_1.resolve)(cwd, "go.mod"))) {
54
+ return "go test ./...";
55
+ }
56
+ // 6. Makefile with a `test` target
57
+ const makefilePath = (0, node_path_1.resolve)(cwd, "Makefile");
58
+ if ((0, node_fs_1.existsSync)(makefilePath)) {
59
+ try {
60
+ const contents = (0, node_fs_1.readFileSync)(makefilePath, "utf8");
61
+ if (/^test:/m.test(contents))
62
+ return "make test";
63
+ }
64
+ catch {
65
+ // unreadable Makefile — fall through
66
+ }
67
+ }
19
68
  return null;
20
69
  }
21
- async function runDetectedTests(timeoutMs = 5 * 60 * 1000) {
22
- const command = detectTestCommand();
23
- if (!command)
70
+ /**
71
+ * Build a scoped version of the base command that only runs tests related to
72
+ * the provided files. Falls back to the full command if scoping is not
73
+ * supported for the detected runner.
74
+ */
75
+ function buildScopedCommand(base, relatedFiles) {
76
+ if (!relatedFiles.length)
77
+ return base;
78
+ // vitest / jest both support --testPathPattern (regex)
79
+ if (/vitest|jest/.test(base)) {
80
+ // Build a pattern that matches the test files corresponding to the changed source files.
81
+ // e.g. "src/foo.ts" → look for "src/foo" anywhere in test paths.
82
+ const stems = relatedFiles.map((f) => f.replace(/\.[^/.]+$/, "").replace(/\//g, "\\/"));
83
+ const pattern = stems.join("|");
84
+ return `${base} --testPathPattern="${pattern}"`;
85
+ }
86
+ // pytest supports positional file/dir args (best effort)
87
+ if (base.startsWith("pytest")) {
88
+ const pyFiles = relatedFiles
89
+ .map((f) => f.replace(/\.ts$/, ".py"))
90
+ .filter((f) => (0, node_fs_1.existsSync)(f));
91
+ if (pyFiles.length)
92
+ return `${base} ${pyFiles.join(" ")}`;
93
+ }
94
+ // Everything else: full run
95
+ return base;
96
+ }
97
+ /**
98
+ * Extract failed test names from stdout/stderr output of vitest, jest, or pytest.
99
+ */
100
+ function extractFailedTestNames(output) {
101
+ const lines = [];
102
+ for (const line of output.split("\n")) {
103
+ const trimmed = line.trim();
104
+ // vitest / jest: lines starting with × or ✗ or containing "● " (jest describe block)
105
+ if (/^[×✗]/.test(trimmed) || /^\s*●\s+/.test(line)) {
106
+ lines.push(trimmed.replace(/^[×✗●\s]+/, "").trim());
107
+ continue;
108
+ }
109
+ // pytest: lines like "FAILED src/foo_test.py::test_name"
110
+ if (/^FAILED\s+/.test(trimmed)) {
111
+ lines.push(trimmed.replace(/^FAILED\s+/, "").trim());
112
+ }
113
+ }
114
+ return [...new Set(lines)].filter(Boolean).slice(0, 10);
115
+ }
116
+ /**
117
+ * Run the detected (or configured) test suite and return the result.
118
+ * Returns null if no test command could be detected.
119
+ */
120
+ async function runDetectedTests(optionsOrTimeout = {}) {
121
+ // Accept the legacy `timeoutMs` number signature for backwards compatibility.
122
+ const options = typeof optionsOrTimeout === "number" ? { timeoutMs: optionsOrTimeout } : optionsOrTimeout;
123
+ const cwd = options.cwd ?? process.cwd();
124
+ const timeoutMs = options.timeoutMs ?? 5 * 60 * 1000;
125
+ const baseCommand = detectTestCommand(cwd);
126
+ if (!baseCommand)
24
127
  return null;
128
+ const command = options.relatedFiles?.length
129
+ ? buildScopedCommand(baseCommand, options.relatedFiles)
130
+ : baseCommand;
25
131
  const started = Date.now();
26
132
  return new Promise((resolveResult) => {
27
133
  const child = (0, node_child_process_1.spawn)(command, {
28
134
  shell: true,
29
- cwd: process.cwd(),
135
+ cwd,
30
136
  env: process.env,
31
137
  stdio: ["ignore", "pipe", "pipe"],
32
138
  });
33
139
  let stdout = "";
34
140
  let stderr = "";
141
+ let didTimeout = false;
35
142
  child.stdout?.on("data", (chunk) => {
36
143
  stdout += chunk.toString();
37
144
  });
38
145
  child.stderr?.on("data", (chunk) => {
39
146
  stderr += chunk.toString();
40
147
  });
41
- const timeout = setTimeout(() => {
148
+ const timer = setTimeout(() => {
149
+ didTimeout = true;
42
150
  child.kill("SIGTERM");
43
151
  resolveResult({
44
152
  command,
@@ -46,17 +154,95 @@ async function runDetectedTests(timeoutMs = 5 * 60 * 1000) {
46
154
  stdout,
47
155
  stderr: `${stderr}\nTimed out after ${timeoutMs}ms`,
48
156
  durationMs: Date.now() - started,
157
+ timedOut: true,
49
158
  });
50
159
  }, timeoutMs);
51
160
  child.on("close", (code) => {
52
- clearTimeout(timeout);
161
+ clearTimeout(timer);
162
+ if (didTimeout)
163
+ return; // already resolved
53
164
  resolveResult({
54
165
  command,
55
166
  passed: code === 0,
56
167
  stdout,
57
168
  stderr,
58
169
  durationMs: Date.now() - started,
170
+ timedOut: false,
59
171
  });
60
172
  });
61
173
  });
62
174
  }
175
+ /** Common English stop words to strip from intent before keyword extraction. */
176
+ const STOP_WORDS = new Set([
177
+ "a", "an", "the", "and", "or", "of", "to", "in", "for", "on", "with",
178
+ "at", "by", "from", "as", "is", "was", "are", "were", "be", "been",
179
+ "it", "this", "that", "these", "those", "we", "i", "you", "they",
180
+ "add", "fix", "update", "change", "make", "get", "set", "use",
181
+ "all", "some", "any", "new", "old", "via", "into", "also", "should",
182
+ ]);
183
+ /**
184
+ * Extract meaningful tokens from an intent string.
185
+ * Splits on non-alphanumeric characters and removes stop words.
186
+ */
187
+ function extractIntentKeywords(intent) {
188
+ return intent
189
+ .toLowerCase()
190
+ .split(/[^a-z0-9]+/)
191
+ .filter((t) => t.length >= 3 && !STOP_WORDS.has(t));
192
+ }
193
+ /**
194
+ * Extract tokens from a file path (useful for overlap scoring).
195
+ * e.g. "src/middleware/userAuth.ts" → ["src","middleware","userauth","ts"]
196
+ */
197
+ function tokenizePath(filePath) {
198
+ return filePath
199
+ .toLowerCase()
200
+ .split(/[/\\._\-]+/)
201
+ .filter((t) => t.length >= 2);
202
+ }
203
+ /**
204
+ * Analyse whether the changed files are coherent with the declared intent.
205
+ * Returns a CoherenceResult with a verdict and list of suspicious files.
206
+ *
207
+ * Does NOT require an LLM — purely keyword/token overlap heuristics.
208
+ */
209
+ function analyzeCoherence(ctx) {
210
+ const { intent, changedFiles } = ctx;
211
+ if (!intent || intent.trim().length === 0) {
212
+ return {
213
+ score: 0,
214
+ suspiciousFiles: [],
215
+ intentKeywords: [],
216
+ verdict: "unknown",
217
+ };
218
+ }
219
+ const intentKeywords = extractIntentKeywords(intent);
220
+ if (intentKeywords.length === 0) {
221
+ return {
222
+ score: 0,
223
+ suspiciousFiles: [],
224
+ intentKeywords: [],
225
+ verdict: "unknown",
226
+ };
227
+ }
228
+ if (changedFiles.length === 0) {
229
+ return {
230
+ score: 1,
231
+ suspiciousFiles: [],
232
+ intentKeywords,
233
+ verdict: "coherent",
234
+ };
235
+ }
236
+ const suspiciousFiles = [];
237
+ for (const file of changedFiles) {
238
+ const pathTokens = tokenizePath(file);
239
+ const hasOverlap = intentKeywords.some((kw) => pathTokens.some((pt) => pt.includes(kw) || kw.includes(pt)));
240
+ if (!hasOverlap) {
241
+ suspiciousFiles.push(file);
242
+ }
243
+ }
244
+ const score = (changedFiles.length - suspiciousFiles.length) / changedFiles.length;
245
+ const driftThreshold = 0.4;
246
+ const verdict = suspiciousFiles.length / changedFiles.length > driftThreshold ? "drift" : "coherent";
247
+ return { score, suspiciousFiles, intentKeywords, verdict };
248
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbridge1/cli",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "CLI for AgentBridge — structured AI agent work sessions with domain authority and approval gates",
5
5
  "type": "commonjs",
6
6
  "bin": {