@kontourai/flow-agents 0.1.1 → 0.2.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.
Files changed (97) hide show
  1. package/.github/dependabot.yml +23 -0
  2. package/.github/workflows/publish-npm.yml +1 -1
  3. package/.github/workflows/release-please.yml +31 -0
  4. package/.github/workflows/runtime-compat.yml +118 -0
  5. package/CHANGELOG.md +38 -0
  6. package/CONTRIBUTING.md +4 -0
  7. package/README.md +58 -19
  8. package/build/src/cli/init.js +215 -5
  9. package/build/src/cli/utterance-check.js +236 -0
  10. package/build/src/cli.js +3 -0
  11. package/build/src/tools/build-universal-bundles.js +268 -0
  12. package/build/src/tools/filter-installed-packs.js +3 -0
  13. package/build/src/tools/validate-source-tree.js +6 -1
  14. package/context/scripts/telemetry/lib/config.sh +5 -1
  15. package/context/settings/flow-agents-settings.json +7 -0
  16. package/docs/agent-system-guidebook.md +4 -5
  17. package/docs/context-map.md +1 -0
  18. package/docs/index.md +46 -6
  19. package/docs/integrations/conformance.md +246 -0
  20. package/docs/integrations/framework-adapter.md +275 -0
  21. package/docs/integrations/harness-install.md +213 -0
  22. package/docs/integrations/index.md +54 -0
  23. package/docs/north-star.md +3 -3
  24. package/docs/repository-structure.md +1 -1
  25. package/docs/skills-map.md +10 -4
  26. package/docs/spec/runtime-hook-surface.md +472 -0
  27. package/docs/survey-utterance-check.md +308 -0
  28. package/docs/vision.md +45 -0
  29. package/docs/workflow-usage-guide.md +1 -1
  30. package/evals/acceptance/run.sh +4 -2
  31. package/evals/acceptance/test_opencode_harness.sh +121 -0
  32. package/evals/acceptance/test_pi_harness.sh +98 -0
  33. package/evals/integration/test_bundle_install.sh +226 -1
  34. package/evals/integration/test_bundle_lifecycle.sh +641 -0
  35. package/evals/integration/test_utterance_check.sh +518 -0
  36. package/evals/run.sh +2 -0
  37. package/evals/static/test_universal_bundles.sh +137 -2
  38. package/integrations/strands/README.md +256 -0
  39. package/integrations/strands/example.py +74 -0
  40. package/integrations/strands/flow_agents_strands/__init__.py +27 -0
  41. package/integrations/strands/flow_agents_strands/hooks.py +194 -0
  42. package/integrations/strands/flow_agents_strands/policy.py +348 -0
  43. package/integrations/strands/flow_agents_strands/steering.py +172 -0
  44. package/integrations/strands/flow_agents_strands/telemetry.py +238 -0
  45. package/integrations/strands/pyproject.toml +38 -0
  46. package/integrations/strands/tests/__init__.py +0 -0
  47. package/integrations/strands/tests/test_hooks.py +304 -0
  48. package/integrations/strands/tests/test_policy.py +315 -0
  49. package/integrations/strands/tests/test_telemetry.py +184 -0
  50. package/integrations/strands-ts/README.md +224 -0
  51. package/integrations/strands-ts/bin/conformance-shim.mjs +257 -0
  52. package/integrations/strands-ts/package.json +53 -0
  53. package/integrations/strands-ts/src/hooks.ts +208 -0
  54. package/integrations/strands-ts/src/index.ts +22 -0
  55. package/integrations/strands-ts/src/policy.ts +345 -0
  56. package/integrations/strands-ts/src/telemetry.ts +251 -0
  57. package/integrations/strands-ts/test/test-policy.ts +322 -0
  58. package/integrations/strands-ts/test/test-telemetry.ts +226 -0
  59. package/integrations/strands-ts/tsconfig.json +20 -0
  60. package/package.json +7 -2
  61. package/packaging/conformance/README.md +142 -0
  62. package/packaging/conformance/fixtures/config-protection--allow-no-path.json +18 -0
  63. package/packaging/conformance/fixtures/config-protection--allow-safe-file.json +20 -0
  64. package/packaging/conformance/fixtures/config-protection--block-biome.json +20 -0
  65. package/packaging/conformance/fixtures/config-protection--block-eslintrc.json +20 -0
  66. package/packaging/conformance/fixtures/quality-gate--allow-no-path.json +17 -0
  67. package/packaging/conformance/fixtures/quality-gate--allow-nonexistent-file.json +19 -0
  68. package/packaging/conformance/fixtures/stop-goal-fit--allow-clean-cwd.json +17 -0
  69. package/packaging/conformance/fixtures/stop-goal-fit--block-strict-mode.json +23 -0
  70. package/packaging/conformance/fixtures/stop-goal-fit--warn-active-delivery.json +21 -0
  71. package/packaging/conformance/fixtures/workflow-steering--allow-no-state.json +16 -0
  72. package/packaging/conformance/fixtures/workflow-steering--inject-active-state.json +29 -0
  73. package/packaging/conformance/fixtures/workflow-steering--inject-subagent-steering.json +25 -0
  74. package/packaging/conformance/package.json +4 -0
  75. package/packaging/conformance/run-conformance.js +322 -0
  76. package/packaging/manifest.json +59 -0
  77. package/schemas/flow-agents-settings.schema.json +48 -0
  78. package/scripts/README.md +5 -0
  79. package/scripts/dogfood.js +16 -0
  80. package/scripts/hooks/opencode-hook-adapter.js +123 -0
  81. package/scripts/hooks/opencode-telemetry-hook.js +101 -0
  82. package/scripts/hooks/pi-hook-adapter.js +123 -0
  83. package/scripts/hooks/pi-telemetry-hook.js +105 -0
  84. package/scripts/hooks/run-hook.js +8 -0
  85. package/scripts/hooks/utterance-check.js +327 -0
  86. package/scripts/telemetry/lib/config.sh +5 -1
  87. package/skills/idea-to-backlog/SKILL.md +1 -1
  88. package/src/cli/init.ts +219 -6
  89. package/src/cli/utterance-check.ts +324 -0
  90. package/src/cli.ts +3 -0
  91. package/src/tools/build-universal-bundles.ts +266 -0
  92. package/src/tools/filter-installed-packs.ts +3 -0
  93. package/src/tools/validate-source-tree.ts +6 -1
  94. package/build/src/cli/docs-preview.js +0 -39
  95. package/build/src/cli/export-bookmarks.js +0 -38
  96. package/build/src/cli/import-bookmarks.js +0 -50
  97. package/build/src/cli/instinct-cli.js +0 -93
@@ -102,7 +102,7 @@ Gate: the opportunity is worth shaping, or it is parked/rejected.
102
102
 
103
103
  ### 4. Explore Options
104
104
 
105
- Use `knowledge-search`, `search-first`, `explore`, or `crowdsource` when context is missing.
105
+ Use `search-first` or `explore` when context is missing.
106
106
 
107
107
  Decide the path:
108
108
 
package/src/cli/init.ts CHANGED
@@ -9,7 +9,7 @@ import { activateCodexLocal } from "../runtime-adapters.js";
9
9
  import { main as buildBundles } from "../tools/build-universal-bundles.js";
10
10
  import { root } from "../tools/common.js";
11
11
 
12
- type Runtime = "base" | "codex" | "claude-code" | "kiro";
12
+ type Runtime = "base" | "codex" | "claude-code" | "kiro" | "opencode" | "pi";
13
13
  type TelemetrySink = "local-files" | "local-kontour-console" | "kontour-hosted-console" | "user-hosted-console" | "kontour-cloud" | "hosted-kontour-console";
14
14
 
15
15
  type InitOptions = {
@@ -30,13 +30,69 @@ const runtimeBundles: Record<Runtime, string> = {
30
30
  codex: "codex",
31
31
  "claude-code": "claude-code",
32
32
  kiro: "kiro",
33
+ opencode: "opencode",
34
+ pi: "pi",
33
35
  };
34
36
 
37
+ // Stable marker present in every Flow Agents claude-code hook command.
38
+ // Used by scope-collision detection to identify an existing flow-agents install.
39
+ // Marker must be distinctive to Flow Agents generated settings. Sibling
40
+ // products from the same lineage ship identically named hook scripts such
41
+ // as claude-hook-adapter.js, so script filenames are NOT a safe marker.
42
+ export const COLLISION_MARKER = "Recording Flow Agents telemetry";
43
+
44
+ /**
45
+ * Check whether a user-level Claude Code settings file already contains
46
+ * Flow Agents hook commands. If it does, print a WARNING explaining that
47
+ * Claude Code merges user-level and project-level settings and runs ALL
48
+ * matching hooks, so having flow-agents in both places causes duplicate
49
+ * hook execution (double telemetry, double policy enforcement).
50
+ *
51
+ * The check does NOT block the install; it is advisory only.
52
+ *
53
+ * @param userSettingsFile Path to inspect (defaults to $HOME/.claude/settings.json;
54
+ * overridable via FLOW_AGENTS_USER_CLAUDE_SETTINGS env var for testability).
55
+ * @returns true if a collision was detected, false otherwise.
56
+ */
57
+ export function checkScopeCollision(userSettingsFile?: string): boolean {
58
+ const filePath = userSettingsFile
59
+ ?? process.env["FLOW_AGENTS_USER_CLAUDE_SETTINGS"]
60
+ ?? path.join(os.homedir(), ".claude", "settings.json");
61
+ if (!fs.existsSync(filePath)) return false;
62
+ let text: string;
63
+ try {
64
+ text = fs.readFileSync(filePath, "utf8");
65
+ } catch {
66
+ return false;
67
+ }
68
+ if (!text.includes(COLLISION_MARKER)) return false;
69
+ console.warn(
70
+ `\nWARNING: Flow Agents scope collision detected.\n` +
71
+ ` ${filePath}\n` +
72
+ `already contains Flow Agents hook commands (marker: ${COLLISION_MARKER}).\n` +
73
+ `\n` +
74
+ `Claude Code merges user-level (~/.claude/settings.json) and project-level\n` +
75
+ `(.claude/settings.json) settings, then runs ALL matching hooks from both files.\n` +
76
+ `Installing Flow Agents at the project level while it is also present at the\n` +
77
+ `user level will cause duplicate hook execution: telemetry events are recorded\n` +
78
+ `twice and policy hooks (workflow-steering, config-protection, quality-gate,\n` +
79
+ `stop-goal-fit) run twice per event.\n` +
80
+ `\n` +
81
+ `To resolve:\n` +
82
+ ` - Remove the hooks section from ${filePath} and rely solely on the\n` +
83
+ ` project-level .claude/settings.json installed by flow-agents init, OR\n` +
84
+ ` - Remove the project-level install and keep only the user-level one.\n` +
85
+ `\n` +
86
+ `The install will continue; resolve the collision before running Claude Code.\n`
87
+ );
88
+ return true;
89
+ }
90
+
35
91
  function usage(): void {
36
92
  console.error(`usage: flow-agents init [options]
37
93
 
38
94
  Options:
39
- --runtime base|codex|claude-code|kiro
95
+ --runtime base|codex|claude-code|kiro|opencode|pi
40
96
  --dest PATH
41
97
  --telemetry-sink local-files|local-kontour-console|kontour-hosted-console|user-hosted-console
42
98
  --console-url URL
@@ -52,8 +108,8 @@ Options:
52
108
  function normalizeRuntime(value: string | undefined): Runtime | undefined {
53
109
  if (!value) return undefined;
54
110
  if (value === "claude") return "claude-code";
55
- if (value === "base" || value === "codex" || value === "claude-code" || value === "kiro") return value;
56
- throw new Error(`unknown runtime '${value}'; expected base, codex, claude-code, or kiro`);
111
+ if (value === "base" || value === "codex" || value === "claude-code" || value === "kiro" || value === "opencode" || value === "pi") return value;
112
+ throw new Error(`unknown runtime '${value}'; expected base, codex, claude-code, kiro, opencode, or pi`);
57
113
  }
58
114
 
59
115
  function normalizeTelemetrySink(value: string): TelemetrySink {
@@ -86,7 +142,7 @@ async function questionHidden(prompt: string): Promise<string> {
86
142
  let value = "";
87
143
  const onData = (buffer: Buffer) => {
88
144
  const text = buffer.toString("utf8");
89
- if (text === "\u0003") {
145
+ if (text === "") {
90
146
  stdout.write("\n");
91
147
  process.exit(130);
92
148
  }
@@ -97,7 +153,7 @@ async function questionHidden(prompt: string): Promise<string> {
97
153
  resolve(value);
98
154
  return;
99
155
  }
100
- if (text === "\u007f") {
156
+ if (text === "") {
101
157
  value = value.slice(0, -1);
102
158
  return;
103
159
  }
@@ -235,6 +291,18 @@ export async function main(argv = process.argv.slice(2)): Promise<number> {
235
291
  const headless = argv.includes("--yes") || argv.includes("--headless") || !process.stdin.isTTY;
236
292
  try {
237
293
  const options = headless ? headlessOptions(argv) : await interactiveOptions(argv);
294
+ // Scope-collision check for claude-code: Claude Code merges user-level
295
+ // (~/.claude/settings.json) and project-level (.claude/settings.json) settings
296
+ // and runs ALL matching hooks from both files. If a user-level settings file
297
+ // already contains flow-agents hooks, installing at the project level will
298
+ // cause duplicate hook execution. We warn but do not block.
299
+ //
300
+ // Codex note: Codex hooks live in .codex/hooks.json (project-level only).
301
+ // There is no well-known user-level codex hooks file in our install paths,
302
+ // so no collision check is needed for codex.
303
+ if (options.runtime === "claude-code") {
304
+ checkScopeCollision();
305
+ }
238
306
  const bundle = ensureBundle(options.runtime);
239
307
  const installed = installBundle(bundle, options);
240
308
  if (installed !== 0) return installed;
@@ -245,4 +313,149 @@ export async function main(argv = process.argv.slice(2)): Promise<number> {
245
313
  }
246
314
  }
247
315
 
316
+ // ---------------------------------------------------------------------------
317
+ // Dogfood subcommand
318
+ //
319
+ // `flow-agents dogfood --runtime claude-code [--dest PATH]`
320
+ //
321
+ // Writes only the hook-wiring artifacts for the specified runtime into the
322
+ // target directory (default: cwd). Unlike a full install, dogfood:
323
+ // - Does NOT rsync the full bundle (no agents/skills duplication).
324
+ // - Reads the generated settings/config from dist/<runtime>/ so the output
325
+ // cannot drift from what the bundle generates (DRY guarantee).
326
+ // - For claude-code: OMITS permissions.defaultMode and
327
+ // skipDangerousModePermissionPrompt (permissive defaults are for installed
328
+ // workspaces, not source repos).
329
+ // - Runs the same scope-collision warning as init.
330
+ // ---------------------------------------------------------------------------
331
+
332
+ function dogfoodUsage(): void {
333
+ console.error(`usage: flow-agents dogfood [options]
334
+
335
+ Options:
336
+ --runtime claude-code|codex|opencode|pi (required)
337
+ --dest PATH (default: cwd)
338
+ --yes, --headless
339
+ `);
340
+ }
341
+
342
+ type DogfoodRuntime = "claude-code" | "codex" | "opencode" | "pi";
343
+
344
+ function normalizeDogfoodRuntime(value: string | undefined): DogfoodRuntime | undefined {
345
+ if (!value) return undefined;
346
+ if (value === "claude" || value === "claude-code") return "claude-code";
347
+ if (value === "codex" || value === "opencode" || value === "pi") return value;
348
+ throw new Error(`dogfood: unsupported runtime '${value}'; expected claude-code, codex, opencode, or pi`);
349
+ }
350
+
351
+ /**
352
+ * Write the claude-code hook-wiring artifacts into dest.
353
+ * Reads dist/claude-code/.claude/settings.json (generated by build-bundles),
354
+ * strips the permissive-mode permission keys (defaultMode, skipDangerousModePermissionPrompt),
355
+ * and writes .claude/settings.json to dest.
356
+ */
357
+ function dogfoodClaudeCode(bundleRoot: string, dest: string): void {
358
+ const sourcePath = path.join(bundleRoot, ".claude", "settings.json");
359
+ if (!fs.existsSync(sourcePath)) throw new Error(`dogfood: bundle settings missing: ${sourcePath}`);
360
+ const settings = JSON.parse(fs.readFileSync(sourcePath, "utf8")) as Record<string, unknown>;
361
+ // Remove permissive defaults that are only appropriate for installed workspaces.
362
+ // These keys must not be present in the source repo's .claude/settings.json.
363
+ delete settings["permissions"];
364
+ delete settings["skipDangerousModePermissionPrompt"];
365
+ const outDir = path.join(dest, ".claude");
366
+ fs.mkdirSync(outDir, { recursive: true });
367
+ fs.writeFileSync(path.join(outDir, "settings.json"), `${JSON.stringify(settings, null, 2)}\n`, "utf8");
368
+ }
369
+
370
+ /**
371
+ * Write the codex hook-wiring artifacts into dest.
372
+ * Reads dist/codex/.codex/hooks.json and writes .codex/hooks.json to dest.
373
+ * The monolithic .codex/config.toml is not written here because it contains
374
+ * workspace settings (approvals_reviewer, features) that would override the
375
+ * developer's existing codex configuration. Only the hooks file is written.
376
+ */
377
+ function dogfoodCodex(bundleRoot: string, dest: string): void {
378
+ const sourcePath = path.join(bundleRoot, ".codex", "hooks.json");
379
+ if (!fs.existsSync(sourcePath)) throw new Error(`dogfood: bundle hooks.json missing: ${sourcePath}`);
380
+ const hooks = fs.readFileSync(sourcePath, "utf8");
381
+ const outDir = path.join(dest, ".codex");
382
+ fs.mkdirSync(outDir, { recursive: true });
383
+ fs.writeFileSync(path.join(outDir, "hooks.json"), hooks, "utf8");
384
+ }
385
+
386
+ /**
387
+ * Write the opencode hook-wiring artifacts into dest.
388
+ * Reads dist/opencode/.opencode/plugins/flow-agents.js and opencode.json,
389
+ * and writes them into dest. These are the minimal hook-wiring files; the
390
+ * full skill/agent tree is not copied.
391
+ */
392
+ function dogfoodOpencode(bundleRoot: string, dest: string): void {
393
+ const pluginSource = path.join(bundleRoot, ".opencode", "plugins", "flow-agents.js");
394
+ const configSource = path.join(bundleRoot, "opencode.json");
395
+ if (!fs.existsSync(pluginSource)) throw new Error(`dogfood: bundle plugin missing: ${pluginSource}`);
396
+ const pluginDir = path.join(dest, ".opencode", "plugins");
397
+ fs.mkdirSync(pluginDir, { recursive: true });
398
+ fs.copyFileSync(pluginSource, path.join(pluginDir, "flow-agents.js"));
399
+ // Write opencode.json only if it does not already exist to avoid clobbering
400
+ // any workspace-specific opencode configuration.
401
+ const destConfig = path.join(dest, "opencode.json");
402
+ if (!fs.existsSync(destConfig) && fs.existsSync(configSource)) {
403
+ fs.copyFileSync(configSource, destConfig);
404
+ }
405
+ }
406
+
407
+ /**
408
+ * Write the pi hook-wiring artifacts into dest.
409
+ * Reads dist/pi/.pi/extensions/flow-agents.ts and writes it to dest.
410
+ * The extension is the only hook-wiring file needed for pi.
411
+ */
412
+ function dogfoodPi(bundleRoot: string, dest: string): void {
413
+ const extSource = path.join(bundleRoot, ".pi", "extensions", "flow-agents.ts");
414
+ if (!fs.existsSync(extSource)) throw new Error(`dogfood: bundle extension missing: ${extSource}`);
415
+ const extDir = path.join(dest, ".pi", "extensions");
416
+ fs.mkdirSync(extDir, { recursive: true });
417
+ fs.copyFileSync(extSource, path.join(extDir, "flow-agents.ts"));
418
+ }
419
+
420
+ export async function mainDogfood(argv = process.argv.slice(2)): Promise<number> {
421
+ if (argv.includes("--help") || argv.includes("-h")) {
422
+ dogfoodUsage();
423
+ return 0;
424
+ }
425
+ const args = parseArgs(argv);
426
+ try {
427
+ const runtimeRaw = flagString(args.flags, "runtime");
428
+ const runtime = normalizeDogfoodRuntime(runtimeRaw);
429
+ if (!runtime) {
430
+ console.error("dogfood: --runtime is required (claude-code, codex, opencode, or pi)");
431
+ dogfoodUsage();
432
+ return 2;
433
+ }
434
+ const dest = path.resolve(flagString(args.flags, "dest") ?? process.cwd());
435
+
436
+ // Ensure the bundle for the requested runtime is built.
437
+ const bundleRuntime: Runtime = runtime as Runtime;
438
+ const bundleRoot = ensureBundle(bundleRuntime);
439
+
440
+ // Scope-collision check: warn if user-level claude settings already has flow-agents.
441
+ // Codex: no user-level hooks file in our install paths — skip with note above.
442
+ if (runtime === "claude-code") {
443
+ checkScopeCollision();
444
+ }
445
+
446
+ // Write only the hook-wiring artifacts, not the full bundle.
447
+ fs.mkdirSync(dest, { recursive: true });
448
+ if (runtime === "claude-code") dogfoodClaudeCode(bundleRoot, dest);
449
+ else if (runtime === "codex") dogfoodCodex(bundleRoot, dest);
450
+ else if (runtime === "opencode") dogfoodOpencode(bundleRoot, dest);
451
+ else if (runtime === "pi") dogfoodPi(bundleRoot, dest);
452
+
453
+ console.log(`Flow Agents dogfood hooks wired for ${runtime} in ${dest}`);
454
+ return 0;
455
+ } catch (error) {
456
+ console.error(`flow-agents dogfood: ${(error as Error).message}`);
457
+ return 2;
458
+ }
459
+ }
460
+
248
461
  if (import.meta.url === `file://${process.argv[1]}`) process.exit(await main());
@@ -0,0 +1,324 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import { flagBool, flagString, parseArgs } from "../lib/args.js";
4
+
5
+ // ---------------------------------------------------------------------------
6
+ // Output types
7
+ // ---------------------------------------------------------------------------
8
+
9
+ interface StatementResult {
10
+ excerpt: string;
11
+ badge: string;
12
+ target: { subjectType: string; subjectId: string; fieldOrBehavior: string };
13
+ span?: { start: number; end: number };
14
+ }
15
+
16
+ interface UtteranceReport {
17
+ status: "ok" | "not_configured" | "error";
18
+ agent_id: string;
19
+ utterance_excerpt: string;
20
+ statements: StatementResult[];
21
+ summary: string;
22
+ }
23
+
24
+ // ---------------------------------------------------------------------------
25
+ // Survey module interface (mirrors @kontourai/survey exported shapes)
26
+ // ---------------------------------------------------------------------------
27
+
28
+ interface SurveyExtractedItem {
29
+ target: { subjectType: string; subjectId: string; fieldOrBehavior: string };
30
+ value?: unknown;
31
+ excerpt: string;
32
+ span?: { start: number; end: number };
33
+ confidence: number;
34
+ }
35
+
36
+ interface SurveyExtractor {
37
+ name: string;
38
+ extract(utterance: string): SurveyExtractedItem[] | Promise<SurveyExtractedItem[]>;
39
+ }
40
+
41
+ interface SurveyStatementItem {
42
+ excerpt: string;
43
+ badge: string;
44
+ target: { subjectType: string; subjectId: string; fieldOrBehavior: string };
45
+ span?: { start: number; end: number };
46
+ inquiryRecord: Record<string, unknown>;
47
+ }
48
+
49
+ interface SurveyTrustReport {
50
+ source: Record<string, unknown>;
51
+ statements: SurveyStatementItem[];
52
+ }
53
+
54
+ interface SurveyMod {
55
+ surveyAgentUtterance: (
56
+ utterance: string,
57
+ extractor: SurveyExtractor,
58
+ context: { bundle: Record<string, unknown>; agentId: string; now?: Date }
59
+ ) => Promise<SurveyTrustReport>;
60
+ referenceUtteranceExtractor: SurveyExtractor;
61
+ }
62
+
63
+ interface AnthropicSurveyMod {
64
+ createAnthropicUtteranceExtractor: (options?: { model?: string; apiKey?: string }) => SurveyExtractor;
65
+ }
66
+
67
+ // ---------------------------------------------------------------------------
68
+ // Helpers
69
+ // ---------------------------------------------------------------------------
70
+
71
+ function usage(): void {
72
+ console.error(
73
+ [
74
+ "usage: flow-agents utterance-check check [options]",
75
+ "",
76
+ "Check an agent utterance for evidence coverage using @kontourai/survey.",
77
+ "Requires @kontourai/survey to be installed in the target workspace.",
78
+ "",
79
+ "Options:",
80
+ " --utterance TEXT Utterance text to check (required unless --not-configured).",
81
+ " --bundle-path FILE Trust bundle JSON file. Omit for an empty bundle (all unsupported).",
82
+ " --agent-id ID Agent identifier for provenance (default: flow-agents-utterance-check).",
83
+ " --extractor NAME Extractor to use: 'reference' (default, pattern-based) or 'anthropic'",
84
+ " (model-backed, requires ANTHROPIC_API_KEY and @kontourai/survey/anthropic).",
85
+ " --model MODEL Model for the anthropic extractor (e.g. claude-haiku-4-5).",
86
+ " --not-configured Skip survey call; output not_configured without error.",
87
+ " --strict Exit non-zero when any badge is disputed, rejected, or unsupported.",
88
+ " --help Show this help.",
89
+ ].join("\n")
90
+ );
91
+ }
92
+
93
+ function excerptText(text: string, maxLen = 200): string {
94
+ const trimmed = text.trim().replace(/\s+/g, " ");
95
+ return trimmed.length > maxLen ? `${trimmed.slice(0, maxLen - 3)}...` : trimmed;
96
+ }
97
+
98
+ function badgeSummary(statements: StatementResult[]): string {
99
+ if (statements.length === 0) return "no factual statements extracted";
100
+ const counts: Record<string, number> = {};
101
+ for (const s of statements) {
102
+ counts[s.badge] = (counts[s.badge] ?? 0) + 1;
103
+ }
104
+ return Object.entries(counts)
105
+ .sort((a, b) => b[1] - a[1])
106
+ .map(([badge, n]) => `${badge}:${n}`)
107
+ .join(", ");
108
+ }
109
+
110
+ function hasConcerningBadge(badge: string): boolean {
111
+ return badge === "disputed" || badge === "rejected" || badge === "unsupported";
112
+ }
113
+
114
+ async function loadSurvey(): Promise<SurveyMod | undefined> {
115
+ try {
116
+ const pkg = "@kontourai/survey";
117
+ // Dynamic import avoids a static dependency on @kontourai/survey —
118
+ // the same pattern survey/src/anthropic.ts uses for @anthropic-ai/sdk.
119
+ const mod = await (Function("m", "return import(m)")(pkg) as Promise<unknown>);
120
+ return mod as SurveyMod;
121
+ } catch {
122
+ return undefined;
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Dynamically import @kontourai/survey/anthropic and create the Anthropic extractor.
128
+ * Fails open with a clear not_configured message when the key or peer dep is missing.
129
+ */
130
+ async function loadAnthropicExtractor(model?: string): Promise<SurveyExtractor | { notConfigured: true; reason: string }> {
131
+ const apiKey = process.env.ANTHROPIC_API_KEY;
132
+ if (!apiKey) {
133
+ return {
134
+ notConfigured: true,
135
+ reason:
136
+ "anthropic extractor requires ANTHROPIC_API_KEY to be set. " +
137
+ "Set the environment variable or switch extractor to 'reference'.",
138
+ };
139
+ }
140
+ try {
141
+ const pkg = "@kontourai/survey/anthropic";
142
+ const mod = await (Function("m", "return import(m)")(pkg) as Promise<unknown>) as AnthropicSurveyMod;
143
+ if (typeof mod.createAnthropicUtteranceExtractor !== "function") {
144
+ return {
145
+ notConfigured: true,
146
+ reason:
147
+ "@kontourai/survey/anthropic does not export createAnthropicUtteranceExtractor. " +
148
+ "Update @kontourai/survey to a version that supports the anthropic extractor.",
149
+ };
150
+ }
151
+ const opts: { model?: string; apiKey?: string } = { apiKey };
152
+ if (model) opts.model = model;
153
+ return mod.createAnthropicUtteranceExtractor(opts);
154
+ } catch (err) {
155
+ const msg = err instanceof Error ? err.message : String(err);
156
+ return {
157
+ notConfigured: true,
158
+ reason:
159
+ `@kontourai/survey/anthropic is not available: ${msg}. ` +
160
+ "Install @kontourai/survey with the anthropic subpath export, or switch extractor to 'reference'.",
161
+ };
162
+ }
163
+ }
164
+
165
+ // ---------------------------------------------------------------------------
166
+ // Core check logic
167
+ // ---------------------------------------------------------------------------
168
+
169
+ async function runCheck(argv: string[]): Promise<number> {
170
+ const { flags } = parseArgs(argv);
171
+
172
+ if (flagBool(flags, "help")) {
173
+ usage();
174
+ return 0;
175
+ }
176
+
177
+ const agentId = flagString(flags, "agent-id") ?? "flow-agents-utterance-check";
178
+ const notConfigured = flagBool(flags, "not-configured");
179
+ const strict = flagBool(flags, "strict");
180
+ const extractorName = flagString(flags, "extractor") ?? "reference";
181
+ const model = flagString(flags, "model");
182
+
183
+ if (notConfigured) {
184
+ const report: UtteranceReport = {
185
+ status: "not_configured",
186
+ agent_id: agentId,
187
+ utterance_excerpt: "",
188
+ statements: [],
189
+ summary: "@kontourai/survey is not configured for this workspace.",
190
+ };
191
+ process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
192
+ return 0;
193
+ }
194
+
195
+ const utterance = flagString(flags, "utterance");
196
+ if (!utterance) {
197
+ usage();
198
+ return 3;
199
+ }
200
+
201
+ const bundlePath = flagString(flags, "bundle-path");
202
+ let bundle: Record<string, unknown> = { claims: [] };
203
+ if (bundlePath) {
204
+ const resolved = path.resolve(bundlePath);
205
+ try {
206
+ const raw = fs.readFileSync(resolved, "utf8");
207
+ bundle = JSON.parse(raw) as Record<string, unknown>;
208
+ } catch (err) {
209
+ const msg = err instanceof Error ? err.message : String(err);
210
+ process.stderr.write(`[UtteranceCheck] could not read bundle from ${resolved}: ${msg}\n`);
211
+ }
212
+ }
213
+
214
+ const survey = await loadSurvey();
215
+ if (!survey) {
216
+ const report: UtteranceReport = {
217
+ status: "not_configured",
218
+ agent_id: agentId,
219
+ utterance_excerpt: excerptText(utterance),
220
+ statements: [],
221
+ summary: "@kontourai/survey is not installed. Install it or run with --not-configured.",
222
+ };
223
+ process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
224
+ process.stderr.write(
225
+ "[UtteranceCheck] not_configured: @kontourai/survey is not installed in this workspace.\n"
226
+ );
227
+ return 1;
228
+ }
229
+
230
+ const { surveyAgentUtterance, referenceUtteranceExtractor } = survey;
231
+
232
+ // Resolve which extractor to use.
233
+ let extractor: SurveyExtractor;
234
+ if (extractorName === "anthropic") {
235
+ const anthropicResult = await loadAnthropicExtractor(model);
236
+ if ("notConfigured" in anthropicResult) {
237
+ // Fail open: emit not_configured with a clear reason rather than erroring.
238
+ const report: UtteranceReport = {
239
+ status: "not_configured",
240
+ agent_id: agentId,
241
+ utterance_excerpt: excerptText(utterance),
242
+ statements: [],
243
+ summary: anthropicResult.reason,
244
+ };
245
+ process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
246
+ process.stderr.write(`[UtteranceCheck] not_configured: ${anthropicResult.reason}\n`);
247
+ return 0;
248
+ }
249
+ extractor = anthropicResult;
250
+ } else {
251
+ extractor = referenceUtteranceExtractor;
252
+ }
253
+
254
+ let trustReport: SurveyTrustReport;
255
+ try {
256
+ trustReport = await surveyAgentUtterance(utterance, extractor, {
257
+ bundle,
258
+ agentId,
259
+ });
260
+ } catch (err) {
261
+ const msg = err instanceof Error ? err.message : String(err);
262
+ const report: UtteranceReport = {
263
+ status: "error",
264
+ agent_id: agentId,
265
+ utterance_excerpt: excerptText(utterance),
266
+ statements: [],
267
+ summary: `Survey call failed: ${msg}`,
268
+ };
269
+ process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
270
+ process.stderr.write(`[UtteranceCheck] survey call failed: ${msg}\n`);
271
+ return 1;
272
+ }
273
+
274
+ const statements: StatementResult[] = trustReport.statements.map((s) => ({
275
+ excerpt: s.excerpt,
276
+ badge: s.badge,
277
+ target: s.target,
278
+ span: s.span,
279
+ }));
280
+
281
+ const summary = badgeSummary(statements);
282
+ const report: UtteranceReport = {
283
+ status: "ok",
284
+ agent_id: agentId,
285
+ utterance_excerpt: excerptText(utterance),
286
+ statements,
287
+ summary,
288
+ };
289
+
290
+ process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
291
+
292
+ const concerning = statements.filter((s) => hasConcerningBadge(s.badge));
293
+ if (concerning.length > 0) {
294
+ process.stderr.write(
295
+ `[UtteranceCheck] ${concerning.length} statement(s) lack evidence coverage: ${summary}\n`
296
+ );
297
+ for (const s of concerning.slice(0, 4)) {
298
+ process.stderr.write(` - [${s.badge}] "${excerptText(s.excerpt, 100)}"\n`);
299
+ }
300
+ }
301
+
302
+ if (strict && concerning.length > 0) return 2;
303
+ return 0;
304
+ }
305
+
306
+ // ---------------------------------------------------------------------------
307
+ // Entry point
308
+ // ---------------------------------------------------------------------------
309
+
310
+ export async function main(argv = process.argv.slice(2)): Promise<number> {
311
+ const [subcommand, ...rest] = argv;
312
+ if (!subcommand || subcommand === "--help" || subcommand === "-h") {
313
+ usage();
314
+ return 0;
315
+ }
316
+ if (subcommand !== "check") {
317
+ console.error(`Unknown utterance-check subcommand: ${subcommand}`);
318
+ usage();
319
+ return 3;
320
+ }
321
+ return runCheck(rest);
322
+ }
323
+
324
+ if (import.meta.url === `file://${process.argv[1]}`) process.exit(await main());
package/src/cli.ts CHANGED
@@ -19,6 +19,7 @@ import { main as validateSource } from "./tools/validate-source-tree.js";
19
19
  import { main as validatePackage } from "./tools/validate-package.js";
20
20
  import { main as validateHookInfluence } from "./cli/validate-hook-influence.js";
21
21
  import { main as runtimeAdapter } from "./cli/runtime-adapter.js";
22
+ import { main as utteranceCheck } from "./cli/utterance-check.js";
22
23
 
23
24
  const availableCommands = new Map<string, (argv: string[]) => number | Promise<number>>([
24
25
  ["build-bundles", () => buildBundles()],
@@ -33,6 +34,7 @@ const availableCommands = new Map<string, (argv: string[]) => number | Promise<n
33
34
  ["publish-change", publishChange],
34
35
  ["pull-work-provider", pullWorkProvider],
35
36
  ["runtime-adapter", runtimeAdapter],
37
+ ["utterance-check", utteranceCheck],
36
38
  ["telemetry-doctor", telemetryDoctor],
37
39
  ["usage-feedback", usageFeedback],
38
40
  ["veritas-governance", veritasGovernance],
@@ -58,6 +60,7 @@ const aliases = new Map<string, string>([
58
60
  ["flow-agents-usage-feedback", "usage-feedback"],
59
61
  ["flow-agents-veritas-governance", "veritas-governance"],
60
62
  ["flow-agents-validate-hook-influence", "validate-hook-influence"],
63
+ ["flow-agents-utterance-check", "utterance-check"],
61
64
  ["flow-agents-validate-source", "validate-source"],
62
65
  ["flow-agents-workflow-artifact-cleanup-audit", "workflow-artifact-cleanup-audit"],
63
66
  ]);