@hop-top/kit 0.4.0-alpha.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.
Files changed (174) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/LICENSE +25 -0
  3. package/README.md +189 -0
  4. package/dist/aim.d.ts +116 -0
  5. package/dist/aim.js +417 -0
  6. package/dist/alias.d.ts +73 -0
  7. package/dist/alias.js +207 -0
  8. package/dist/api.d.ts +62 -0
  9. package/dist/api.js +165 -0
  10. package/dist/auth.d.ts +26 -0
  11. package/dist/auth.js +41 -0
  12. package/dist/cli.d.ts +309 -0
  13. package/dist/cli.js +885 -0
  14. package/dist/config.d.ts +97 -0
  15. package/dist/config.js +79 -0
  16. package/dist/errcorrect.d.ts +37 -0
  17. package/dist/errcorrect.js +69 -0
  18. package/dist/llm.d.ts +188 -0
  19. package/dist/llm.js +356 -0
  20. package/dist/output.d.ts +213 -0
  21. package/dist/output.js +447 -0
  22. package/dist/progress.d.ts +30 -0
  23. package/dist/progress.js +56 -0
  24. package/dist/provenance.d.ts +24 -0
  25. package/dist/provenance.js +32 -0
  26. package/dist/routellm.d.ts +64 -0
  27. package/dist/routellm.js +258 -0
  28. package/dist/rpc.d.ts +25 -0
  29. package/dist/rpc.js +101 -0
  30. package/dist/safety.d.ts +24 -0
  31. package/dist/safety.js +44 -0
  32. package/dist/scope.d.ts +121 -0
  33. package/dist/scope.js +380 -0
  34. package/dist/sqlstore.d.ts +84 -0
  35. package/dist/sqlstore.js +85 -0
  36. package/dist/stream.d.ts +35 -0
  37. package/dist/stream.js +52 -0
  38. package/dist/tui/index.d.ts +300 -0
  39. package/dist/tui/index.js +332 -0
  40. package/dist/upgrade.d.ts +95 -0
  41. package/dist/upgrade.js +115 -0
  42. package/dist/xdg.d.ts +169 -0
  43. package/dist/xdg.js +129 -0
  44. package/eslint.config.mjs +17 -0
  45. package/package.json +133 -0
  46. package/src/README.md +3 -0
  47. package/src/aim.test.ts +331 -0
  48. package/src/aim.ts +388 -0
  49. package/src/alias.test.ts +423 -0
  50. package/src/alias.ts +256 -0
  51. package/src/api.test.ts +190 -0
  52. package/src/api.ts +214 -0
  53. package/src/auth.test.ts +24 -0
  54. package/src/auth.ts +37 -0
  55. package/src/bus.test.ts +178 -0
  56. package/src/bus.ts +114 -0
  57. package/src/cli.test.ts +690 -0
  58. package/src/cli.ts +741 -0
  59. package/src/completion-values.test.ts +202 -0
  60. package/src/completion-values.ts +173 -0
  61. package/src/completion.test.ts +212 -0
  62. package/src/completion.ts +158 -0
  63. package/src/compliance.test.ts +134 -0
  64. package/src/compliance.ts +669 -0
  65. package/src/config.test.ts +110 -0
  66. package/src/config.ts +164 -0
  67. package/src/errcorrect.test.ts +134 -0
  68. package/src/errcorrect.ts +84 -0
  69. package/src/flagregister.test.ts +103 -0
  70. package/src/flagregister.ts +78 -0
  71. package/src/gen/crud_connect.ts +76 -0
  72. package/src/gen/crud_pb.ts +269 -0
  73. package/src/hint.test.ts +315 -0
  74. package/src/hint.ts +178 -0
  75. package/src/llm.test.ts +612 -0
  76. package/src/llm.ts +577 -0
  77. package/src/log.test.ts +145 -0
  78. package/src/log.ts +155 -0
  79. package/src/output/builtins.ts +34 -0
  80. package/src/output/dispatch.ts +195 -0
  81. package/src/output/flags.ts +151 -0
  82. package/src/output/format_help.ts +80 -0
  83. package/src/output/formatter.ts +167 -0
  84. package/src/output/formatters/csv.ts +72 -0
  85. package/src/output/formatters/json.ts +44 -0
  86. package/src/output/formatters/table.ts +44 -0
  87. package/src/output/formatters/text.ts +109 -0
  88. package/src/output/formatters/yaml.ts +47 -0
  89. package/src/output/projection.ts +126 -0
  90. package/src/output/registry.ts +88 -0
  91. package/src/output/template.ts +63 -0
  92. package/src/output.test.ts +117 -0
  93. package/src/output.ts +90 -0
  94. package/src/progress.test.ts +93 -0
  95. package/src/progress.ts +58 -0
  96. package/src/provenance.test.ts +59 -0
  97. package/src/provenance.ts +26 -0
  98. package/src/routellm.test.ts +179 -0
  99. package/src/routellm.ts +365 -0
  100. package/src/router/bert.test.ts +180 -0
  101. package/src/router/bert.ts +145 -0
  102. package/src/router/controller.test.ts +183 -0
  103. package/src/router/controller.ts +165 -0
  104. package/src/router/integration.test.ts +309 -0
  105. package/src/router/intent.test.ts +187 -0
  106. package/src/router/intent.ts +208 -0
  107. package/src/router/mf.test.ts +66 -0
  108. package/src/router/mf.ts +52 -0
  109. package/src/router/random.test.ts +23 -0
  110. package/src/router/random.ts +13 -0
  111. package/src/router/router.test.ts +31 -0
  112. package/src/router/router.ts +62 -0
  113. package/src/router/server.test.ts +193 -0
  114. package/src/router/server.ts +134 -0
  115. package/src/router/sw.test.ts +199 -0
  116. package/src/router/sw.ts +232 -0
  117. package/src/rpc.test.ts +47 -0
  118. package/src/rpc.ts +91 -0
  119. package/src/safety.test.ts +53 -0
  120. package/src/safety.ts +42 -0
  121. package/src/scope-defaults.json +72 -0
  122. package/src/scope.test.ts +177 -0
  123. package/src/scope.ts +382 -0
  124. package/src/setflag.test.ts +110 -0
  125. package/src/setflag.ts +102 -0
  126. package/src/sqlstore.test.ts +110 -0
  127. package/src/sqlstore.ts +144 -0
  128. package/src/stream.test.ts +69 -0
  129. package/src/stream.ts +47 -0
  130. package/src/textflag.test.ts +105 -0
  131. package/src/textflag.ts +68 -0
  132. package/src/toolspec.test.ts +187 -0
  133. package/src/toolspec.ts +279 -0
  134. package/src/triton/client.test.ts +205 -0
  135. package/src/triton/client.ts +261 -0
  136. package/src/tui/anim.test.ts +146 -0
  137. package/src/tui/anim.ts +134 -0
  138. package/src/tui/badge.test.ts +41 -0
  139. package/src/tui/badge.ts +55 -0
  140. package/src/tui/confirm.test.ts +68 -0
  141. package/src/tui/confirm.ts +49 -0
  142. package/src/tui/index.ts +29 -0
  143. package/src/tui/list.test.ts +84 -0
  144. package/src/tui/list.ts +72 -0
  145. package/src/tui/parity.ts +41 -0
  146. package/src/tui/pills.test.ts +40 -0
  147. package/src/tui/pills.ts +43 -0
  148. package/src/tui/progress.test.ts +164 -0
  149. package/src/tui/progress.ts +120 -0
  150. package/src/tui/spinner.test.ts +111 -0
  151. package/src/tui/spinner.ts +96 -0
  152. package/src/tui/status.test.ts +68 -0
  153. package/src/tui/status.ts +66 -0
  154. package/src/upgrade.test.ts +298 -0
  155. package/src/upgrade.ts +205 -0
  156. package/src/uri.ts +199 -0
  157. package/src/wizard.test.ts +441 -0
  158. package/src/wizard.ts +662 -0
  159. package/src/xdg.test.ts +202 -0
  160. package/src/xdg.ts +280 -0
  161. package/test/output/cols.test.ts +148 -0
  162. package/test/output/csv.test.ts +129 -0
  163. package/test/output/dispatch.test.ts +194 -0
  164. package/test/output/format_help.test.ts +131 -0
  165. package/test/output/formatter.test.ts +137 -0
  166. package/test/output/output_flag.test.ts +124 -0
  167. package/test/output/registry.test.ts +127 -0
  168. package/test/output/smoke-capture.ts +83 -0
  169. package/test/output/smoke.test.ts +142 -0
  170. package/test/output/template.test.ts +95 -0
  171. package/test/output/text.test.ts +75 -0
  172. package/test/testdata/api-fixture.json +115 -0
  173. package/test/testdata/query-vectors.json +187 -0
  174. package/tsconfig.json +12 -0
@@ -0,0 +1,669 @@
1
+ /**
2
+ * compliance.ts — 12-factor AI CLI compliance checker.
3
+ *
4
+ * Static checks analyse a toolspec YAML. Runtime checks execute
5
+ * the binary. Port of Go hop.top/kit/compliance.
6
+ */
7
+
8
+ import * as fs from "fs";
9
+ import * as yaml from "js-yaml";
10
+ import { execFileSync } from "child_process";
11
+
12
+ // --- Types ---
13
+
14
+ export enum Factor {
15
+ SelfDescribing = 1,
16
+ StructuredIO = 2,
17
+ StreamDiscipline = 3,
18
+ ContractsErrors = 4,
19
+ Preview = 5,
20
+ Idempotency = 6,
21
+ StateTransparency = 7,
22
+ SafeDelegation = 8,
23
+ ObservableOps = 9,
24
+ Provenance = 10,
25
+ Evolution = 11,
26
+ AuthLifecycle = 12,
27
+ }
28
+
29
+ const factorNames: Record<number, string> = {
30
+ [Factor.SelfDescribing]: "Self-Describing",
31
+ [Factor.StructuredIO]: "Structured I/O",
32
+ [Factor.StreamDiscipline]: "Stream Discipline",
33
+ [Factor.ContractsErrors]: "Contracts & Errors",
34
+ [Factor.Preview]: "Preview",
35
+ [Factor.Idempotency]: "Idempotency",
36
+ [Factor.StateTransparency]: "State Transparency",
37
+ [Factor.SafeDelegation]: "Safe Delegation",
38
+ [Factor.ObservableOps]: "Observable Ops",
39
+ [Factor.Provenance]: "Provenance",
40
+ [Factor.Evolution]: "Evolution",
41
+ [Factor.AuthLifecycle]: "Auth Lifecycle",
42
+ };
43
+
44
+ export function factorName(f: Factor): string {
45
+ return factorNames[f] ?? `Factor(${f})`;
46
+ }
47
+
48
+ export interface CheckResult {
49
+ factor: Factor;
50
+ name: string;
51
+ status: "pass" | "fail" | "skip" | "warn";
52
+ details?: string;
53
+ suggestion?: string;
54
+ }
55
+
56
+ export interface Report {
57
+ binary: string;
58
+ toolspec: string;
59
+ results: CheckResult[];
60
+ score: number;
61
+ total: number;
62
+ }
63
+
64
+ // --- Internal YAML types ---
65
+
66
+ interface SpecYAML {
67
+ name?: string;
68
+ schema_version?: string;
69
+ commands?: CmdYAML[];
70
+ state_introspection?: {
71
+ config_commands?: string[];
72
+ auth_commands?: string[];
73
+ };
74
+ }
75
+
76
+ interface CmdYAML {
77
+ name?: string;
78
+ children?: CmdYAML[];
79
+ contract?: {
80
+ idempotent?: boolean;
81
+ side_effects?: string[];
82
+ };
83
+ safety?: {
84
+ level?: string;
85
+ requires_confirmation?: boolean;
86
+ };
87
+ preview_modes?: string[];
88
+ output_schema?: { format?: string };
89
+ }
90
+
91
+ // --- Helpers ---
92
+
93
+ function loadSpec(path: string): SpecYAML {
94
+ const raw = fs.readFileSync(path, "utf-8");
95
+ return yaml.load(raw) as SpecYAML;
96
+ }
97
+
98
+ function allCommands(cmds: CmdYAML[]): CmdYAML[] {
99
+ const out: CmdYAML[] = [];
100
+ for (const c of cmds) {
101
+ out.push(c);
102
+ if (c.children) out.push(...allCommands(c.children));
103
+ }
104
+ return out;
105
+ }
106
+
107
+ function mutatingCommands(cmds: CmdYAML[]): CmdYAML[] {
108
+ return allCommands(cmds).filter(
109
+ (c) =>
110
+ c.contract?.side_effects &&
111
+ c.contract.side_effects.length > 0,
112
+ );
113
+ }
114
+
115
+ function dangerousCommands(cmds: CmdYAML[]): CmdYAML[] {
116
+ return allCommands(cmds).filter(
117
+ (c) => c.safety?.level === "dangerous",
118
+ );
119
+ }
120
+
121
+ function pass(
122
+ f: Factor,
123
+ details: string,
124
+ ): CheckResult {
125
+ return {
126
+ factor: f,
127
+ name: factorName(f),
128
+ status: "pass",
129
+ details,
130
+ };
131
+ }
132
+
133
+ function fail(
134
+ f: Factor,
135
+ details: string,
136
+ suggestion: string,
137
+ ): CheckResult {
138
+ return {
139
+ factor: f,
140
+ name: factorName(f),
141
+ status: "fail",
142
+ details,
143
+ suggestion,
144
+ };
145
+ }
146
+
147
+ function skip(
148
+ f: Factor,
149
+ details: string,
150
+ ): CheckResult {
151
+ return {
152
+ factor: f,
153
+ name: factorName(f),
154
+ status: "skip",
155
+ details,
156
+ };
157
+ }
158
+
159
+ // --- Static Checks ---
160
+
161
+ function checkSelfDescribing(spec: SpecYAML): CheckResult {
162
+ const f = Factor.SelfDescribing;
163
+ if (!spec.commands || spec.commands.length === 0) {
164
+ return fail(f, "no commands defined",
165
+ "Add a commands array with at least one named command");
166
+ }
167
+ for (const c of spec.commands) {
168
+ if (!c.name) {
169
+ return fail(f, "command missing name",
170
+ "Every command must have a name field");
171
+ }
172
+ }
173
+ return pass(f, "commands array non-empty, all named");
174
+ }
175
+
176
+ function checkStructuredIO(spec: SpecYAML): CheckResult {
177
+ const f = Factor.StructuredIO;
178
+ for (const c of allCommands(spec.commands ?? [])) {
179
+ if (c.output_schema) {
180
+ return pass(f, `output_schema found on ${c.name}`);
181
+ }
182
+ }
183
+ return fail(f, "no command has output_schema",
184
+ "Add output_schema to at least one command");
185
+ }
186
+
187
+ function checkContractsErrors(spec: SpecYAML): CheckResult {
188
+ const f = Factor.ContractsErrors;
189
+ const mut = mutatingCommands(spec.commands ?? []);
190
+ if (mut.length === 0) {
191
+ for (const c of allCommands(spec.commands ?? [])) {
192
+ if (c.contract) return pass(f, "contracts found");
193
+ }
194
+ return fail(f, "no contracts declared",
195
+ "Add contract fields to commands");
196
+ }
197
+ for (const c of mut) {
198
+ if (!c.contract) {
199
+ return fail(f, `${c.name} has side_effects but no contract`,
200
+ "Add contract fields to mutating commands");
201
+ }
202
+ }
203
+ return pass(f, "all mutating commands have contracts");
204
+ }
205
+
206
+ function checkPreview(spec: SpecYAML): CheckResult {
207
+ const f = Factor.Preview;
208
+ const mut = mutatingCommands(spec.commands ?? []);
209
+ if (mut.length === 0) {
210
+ return pass(f, "no mutating commands to preview");
211
+ }
212
+ let withPreview = 0;
213
+ for (const c of mut) {
214
+ if (c.preview_modes && c.preview_modes.length > 0) {
215
+ withPreview++;
216
+ }
217
+ }
218
+ if (withPreview === 0) {
219
+ return fail(f, "no mutating command has preview_modes",
220
+ "Add preview_modes (e.g. --dry-run) to mutating commands");
221
+ }
222
+ return pass(f,
223
+ `${withPreview}/${mut.length} mutating commands have preview_modes`);
224
+ }
225
+
226
+ function checkIdempotency(spec: SpecYAML): CheckResult {
227
+ const f = Factor.Idempotency;
228
+ const all = allCommands(spec.commands ?? []);
229
+ if (all.length === 0) {
230
+ return fail(f, "no commands", "Add commands");
231
+ }
232
+ let declared = 0;
233
+ for (const c of all) {
234
+ if (c.contract && c.contract.idempotent !== undefined) {
235
+ declared++;
236
+ }
237
+ }
238
+ if (declared === 0) {
239
+ return fail(f, "no command declares idempotent",
240
+ "Add contract.idempotent to each command");
241
+ }
242
+ return pass(f, "idempotency declared on commands");
243
+ }
244
+
245
+ function checkStateTransparency(spec: SpecYAML): CheckResult {
246
+ const f = Factor.StateTransparency;
247
+ const si = spec.state_introspection;
248
+ if (!si?.config_commands || si.config_commands.length === 0) {
249
+ return fail(f, "no config_commands in state_introspection",
250
+ "Add state_introspection.config_commands");
251
+ }
252
+ return pass(f, "config_commands present");
253
+ }
254
+
255
+ function checkSafeDelegation(spec: SpecYAML): CheckResult {
256
+ const f = Factor.SafeDelegation;
257
+ const dangerous = dangerousCommands(spec.commands ?? []);
258
+ if (dangerous.length === 0) {
259
+ return pass(f, "no dangerous commands");
260
+ }
261
+ for (const c of dangerous) {
262
+ if (!c.safety) {
263
+ return fail(f,
264
+ `${c.name} is dangerous but has no safety block`,
265
+ "Add safety with requires_confirmation");
266
+ }
267
+ }
268
+ return pass(f, "all dangerous commands have safety metadata");
269
+ }
270
+
271
+ function checkEvolution(spec: SpecYAML): CheckResult {
272
+ const f = Factor.Evolution;
273
+ if (!spec.schema_version) {
274
+ return fail(f, "schema_version not set",
275
+ "Set schema_version in the toolspec");
276
+ }
277
+ return pass(f, `schema_version: ${spec.schema_version}`);
278
+ }
279
+
280
+ function checkAuthLifecycle(spec: SpecYAML): CheckResult {
281
+ const f = Factor.AuthLifecycle;
282
+ const si = spec.state_introspection;
283
+ if (!si?.auth_commands || si.auth_commands.length === 0) {
284
+ return skip(f,
285
+ "no auth_commands — skipped (tool may not need auth)");
286
+ }
287
+ return pass(f, "auth_commands present");
288
+ }
289
+
290
+ function runStaticChecks(spec: SpecYAML): CheckResult[] {
291
+ return [
292
+ checkSelfDescribing(spec),
293
+ checkStructuredIO(spec),
294
+ skip(Factor.StreamDiscipline, "runtime check only"),
295
+ checkContractsErrors(spec),
296
+ checkPreview(spec),
297
+ checkIdempotency(spec),
298
+ checkStateTransparency(spec),
299
+ checkSafeDelegation(spec),
300
+ skip(Factor.ObservableOps, "runtime check only"),
301
+ skip(Factor.Provenance, "runtime check only"),
302
+ checkEvolution(spec),
303
+ checkAuthLifecycle(spec),
304
+ ];
305
+ }
306
+
307
+ // --- Runtime Checks ---
308
+
309
+ function execBin(
310
+ bin: string,
311
+ args: string[],
312
+ ): { stdout: string; stderr: string; code: number } {
313
+ try {
314
+ const stdout = execFileSync(bin, args, {
315
+ timeout: 10000,
316
+ stdio: ["pipe", "pipe", "pipe"],
317
+ }).toString();
318
+ return { stdout, stderr: "", code: 0 };
319
+ } catch (e: any) {
320
+ return {
321
+ stdout: e.stdout?.toString() ?? "",
322
+ stderr: e.stderr?.toString() ?? "",
323
+ code: e.status ?? 1,
324
+ };
325
+ }
326
+ }
327
+
328
+ function findReadCommand(spec: SpecYAML): string | undefined {
329
+ for (const c of allCommands(spec.commands ?? [])) {
330
+ if (c.output_schema && c.contract?.idempotent === true) {
331
+ return c.name;
332
+ }
333
+ }
334
+ return undefined;
335
+ }
336
+
337
+ function isValidJSON(s: string): boolean {
338
+ try {
339
+ JSON.parse(s.trim());
340
+ return true;
341
+ } catch {
342
+ return false;
343
+ }
344
+ }
345
+
346
+ function runRuntimeChecks(
347
+ bin: string,
348
+ spec: SpecYAML,
349
+ ): CheckResult[] {
350
+ const results: CheckResult[] = [];
351
+
352
+ // F1: --help exits 0
353
+ {
354
+ const f = Factor.SelfDescribing;
355
+ const r = execBin(bin, ["--help"]);
356
+ if (r.code !== 0) {
357
+ results.push(fail(f, `--help exited ${r.code}`,
358
+ "Ensure --help exits 0"));
359
+ } else {
360
+ const upper = r.stdout.toUpperCase();
361
+ if (!upper.includes("COMMANDS") && !upper.includes("USAGE")) {
362
+ results.push(fail(f, "--help lacks COMMANDS/USAGE",
363
+ "Help should list available commands"));
364
+ } else {
365
+ results.push(pass(f,
366
+ "--help exits 0, contains command listing"));
367
+ }
368
+ }
369
+ }
370
+
371
+ // F2: read cmd --format json
372
+ {
373
+ const f = Factor.StructuredIO;
374
+ const readCmd = findReadCommand(spec);
375
+ if (!readCmd) {
376
+ results.push(skip(f, "no read command found"));
377
+ } else {
378
+ const r = execBin(bin, [readCmd, "--format", "json"]);
379
+ if (r.code !== 0) {
380
+ results.push(
381
+ fail(f, `${readCmd} --format json exited ${r.code}`,
382
+ "Read commands should support --format json"));
383
+ } else if (!isValidJSON(r.stdout)) {
384
+ results.push(fail(f, "output is not valid JSON",
385
+ "--format json should produce valid JSON"));
386
+ } else {
387
+ results.push(
388
+ pass(f, `${readCmd} --format json returns valid JSON`));
389
+ }
390
+ }
391
+ }
392
+
393
+ // F3: stream discipline
394
+ {
395
+ const f = Factor.StreamDiscipline;
396
+ const readCmd = findReadCommand(spec);
397
+ if (!readCmd) {
398
+ results.push(skip(f, "no read command found"));
399
+ } else {
400
+ const r = execBin(bin, [readCmd, "--format", "json"]);
401
+ if (!r.stdout.trim()) {
402
+ results.push(fail(f, "stdout is empty",
403
+ "Data should go to stdout"));
404
+ } else if (
405
+ isValidJSON(r.stderr) && r.stderr.trim().length > 2
406
+ ) {
407
+ results.push(fail(f,
408
+ "stderr contains JSON",
409
+ "Keep structured data on stdout, logs on stderr"));
410
+ } else {
411
+ results.push(pass(f, "stdout has data, stderr clean"));
412
+ }
413
+ }
414
+ }
415
+
416
+ // F4: bogus arg
417
+ {
418
+ const f = Factor.ContractsErrors;
419
+ const r = execBin(bin, ["--bogus-arg-xyzzy"]);
420
+ if (r.code === 0) {
421
+ results.push(fail(f, "bogus arg didn't cause error exit",
422
+ "Unknown flags should cause non-zero exit"));
423
+ } else {
424
+ results.push({
425
+ factor: f,
426
+ name: factorName(f),
427
+ status: "warn",
428
+ details: "error output is not structured JSON",
429
+ suggestion:
430
+ "Return JSON errors with a 'code' field on stderr",
431
+ });
432
+ }
433
+ }
434
+
435
+ // F5: preview
436
+ {
437
+ const f = Factor.Preview;
438
+ const mut = mutatingCommands(spec.commands ?? []);
439
+ if (mut.length === 0) {
440
+ results.push(skip(f, "no mutating commands"));
441
+ } else {
442
+ let found = false;
443
+ for (const c of mut) {
444
+ for (const mode of c.preview_modes ?? []) {
445
+ const r = execBin(bin, [c.name!, mode]);
446
+ if (r.code === 0) {
447
+ results.push(
448
+ pass(f, `${c.name} ${mode} exits 0`));
449
+ found = true;
450
+ break;
451
+ }
452
+ }
453
+ if (found) break;
454
+ }
455
+ if (!found) {
456
+ results.push(fail(f,
457
+ "no mutating command succeeds with preview mode",
458
+ "Ensure --dry-run exits 0"));
459
+ }
460
+ }
461
+ }
462
+
463
+ // F7: config command
464
+ {
465
+ const f = Factor.StateTransparency;
466
+ let r = execBin(bin, ["config", "show"]);
467
+ if (r.code === 0) {
468
+ results.push(pass(f, "config show exits 0"));
469
+ } else {
470
+ r = execBin(bin, ["config"]);
471
+ if (r.code === 0) {
472
+ results.push(pass(f, "config exits 0"));
473
+ } else {
474
+ results.push(fail(f, "config command failed",
475
+ "Add a config/config show command"));
476
+ }
477
+ }
478
+ }
479
+
480
+ // F8: safe delegation
481
+ {
482
+ const f = Factor.SafeDelegation;
483
+ const dangerous = dangerousCommands(spec.commands ?? []);
484
+ if (dangerous.length === 0) {
485
+ results.push(skip(f, "no dangerous commands"));
486
+ } else {
487
+ results.push(pass(f,
488
+ "dangerous commands have safety metadata"));
489
+ }
490
+ }
491
+
492
+ // F10: provenance
493
+ {
494
+ const f = Factor.Provenance;
495
+ const readCmd = findReadCommand(spec);
496
+ if (!readCmd) {
497
+ results.push(skip(f, "no read command found"));
498
+ } else {
499
+ const r = execBin(bin, [readCmd, "--format", "json"]);
500
+ if (r.code !== 0) {
501
+ results.push(skip(f, `${readCmd} failed`));
502
+ } else {
503
+ try {
504
+ const obj = JSON.parse(r.stdout.trim());
505
+ if (obj._meta) {
506
+ results.push(pass(f, "_meta field present"));
507
+ } else {
508
+ results.push(fail(f, "no _meta field in JSON output",
509
+ "Add _meta with provenance info"));
510
+ }
511
+ } catch {
512
+ results.push(skip(f, "output not JSON object"));
513
+ }
514
+ }
515
+ }
516
+ }
517
+
518
+ // F11: --version
519
+ {
520
+ const f = Factor.Evolution;
521
+ const r = execBin(bin, ["--version"]);
522
+ if (r.code !== 0) {
523
+ results.push(
524
+ fail(f, `--version exited ${r.code}`,
525
+ "Ensure --version exits 0"));
526
+ } else {
527
+ results.push(pass(f, "--version exits 0"));
528
+ }
529
+ }
530
+
531
+ // F12: auth
532
+ {
533
+ const f = Factor.AuthLifecycle;
534
+ const si = spec.state_introspection;
535
+ if (!si?.auth_commands || si.auth_commands.length === 0) {
536
+ results.push(skip(f, "no auth_commands declared"));
537
+ } else {
538
+ const r = execBin(bin, ["auth", "status"]);
539
+ if (r.code === 0) {
540
+ results.push(pass(f, "auth status exits 0"));
541
+ } else {
542
+ const r2 = execBin(bin, ["auth"]);
543
+ if (r2.code === 0) {
544
+ results.push(pass(f, "auth exits 0"));
545
+ } else {
546
+ results.push(fail(f, "auth command failed",
547
+ "Implement auth status/auth commands"));
548
+ }
549
+ }
550
+ }
551
+ }
552
+
553
+ return results;
554
+ }
555
+
556
+ // --- Public API ---
557
+
558
+ /** Run static checks against a toolspec YAML file. */
559
+ export function runStatic(toolspecPath: string): CheckResult[] {
560
+ const spec = loadSpec(toolspecPath);
561
+ return runStaticChecks(spec);
562
+ }
563
+
564
+ /** Run runtime checks against a binary + toolspec. */
565
+ export function runRuntime(
566
+ binaryPath: string,
567
+ toolspecPath: string,
568
+ ): CheckResult[] {
569
+ const spec = loadSpec(toolspecPath);
570
+ return runRuntimeChecks(binaryPath, spec);
571
+ }
572
+
573
+ /** Run both static + runtime checks. If binaryPath is empty,
574
+ * only static checks run. */
575
+ export function run(
576
+ binaryPath: string,
577
+ toolspecPath: string,
578
+ ): Report {
579
+ const spec = loadSpec(toolspecPath);
580
+ let results = runStaticChecks(spec);
581
+
582
+ if (binaryPath) {
583
+ const rtResults = runRuntimeChecks(binaryPath, spec);
584
+ results = mergeResults(results, rtResults);
585
+ }
586
+
587
+ const score = results.filter((r) => r.status === "pass").length;
588
+ return {
589
+ binary: binaryPath,
590
+ toolspec: toolspecPath,
591
+ results,
592
+ score,
593
+ total: 12,
594
+ };
595
+ }
596
+
597
+ function mergeResults(
598
+ staticR: CheckResult[],
599
+ runtime: CheckResult[],
600
+ ): CheckResult[] {
601
+ const byFactor = new Map<number, CheckResult>();
602
+ for (const r of staticR) byFactor.set(r.factor, r);
603
+ for (const r of runtime) {
604
+ const existing = byFactor.get(r.factor);
605
+ if (!existing || existing.status === "skip") {
606
+ byFactor.set(r.factor, r);
607
+ }
608
+ }
609
+
610
+ const out: CheckResult[] = [];
611
+ for (
612
+ let f = Factor.SelfDescribing;
613
+ f <= Factor.AuthLifecycle;
614
+ f++
615
+ ) {
616
+ const r = byFactor.get(f);
617
+ if (r) out.push(r);
618
+ }
619
+ return out;
620
+ }
621
+
622
+ /** Render report as text or JSON string. */
623
+ export function formatReport(
624
+ r: Report,
625
+ format: string = "text",
626
+ ): string {
627
+ if (format === "json") {
628
+ return JSON.stringify(r, null, 2) + "\n";
629
+ }
630
+ return formatText(r);
631
+ }
632
+
633
+ function statusIcon(s: string): string {
634
+ switch (s) {
635
+ case "pass": return "PASS";
636
+ case "fail": return "FAIL";
637
+ case "warn": return "WARN";
638
+ case "skip": return "SKIP";
639
+ default: return "????";
640
+ }
641
+ }
642
+
643
+ function formatText(r: Report): string {
644
+ const lines: string[] = [
645
+ "",
646
+ " 12-Factor AI CLI Compliance Report",
647
+ " ══════════════════════════════════",
648
+ ];
649
+ if (r.binary) lines.push(` Binary : ${r.binary}`);
650
+ if (r.toolspec) lines.push(` Toolspec : ${r.toolspec}`);
651
+ lines.push("");
652
+
653
+ for (const cr of r.results) {
654
+ const icon = statusIcon(cr.status);
655
+ const fNum = String(cr.factor).padStart(2, " ");
656
+ lines.push(
657
+ ` ${icon} F${fNum} ${cr.name.padEnd(20)} ` +
658
+ `${cr.details ?? ""}`,
659
+ );
660
+ if (cr.suggestion) {
661
+ lines.push(` └─ ${cr.suggestion}`);
662
+ }
663
+ }
664
+
665
+ lines.push("");
666
+ lines.push(` Score: ${r.score}/${r.total} factors passing`);
667
+ lines.push("");
668
+ return lines.join("\n");
669
+ }