@hop-top/kit 0.5.0-alpha.1 → 0.5.0-alpha.11

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 (293) hide show
  1. package/CHANGELOG.md +275 -0
  2. package/LICENSE +1 -1
  3. package/LICENSE-cobra.txt +174 -0
  4. package/NOTICE +15 -0
  5. package/README.md +55 -259
  6. package/dist/aim-DZHwwiIc.d.ts +139 -0
  7. package/dist/aim.d.ts +1 -116
  8. package/dist/aim.js +13 -414
  9. package/dist/alias-D9_qFlyP.d.ts +174 -0
  10. package/dist/alias.d.ts +2 -73
  11. package/dist/alias.js +27 -207
  12. package/dist/api-DAexsu2H.d.ts +77 -0
  13. package/dist/api.d.ts +1 -62
  14. package/dist/api.js +10 -164
  15. package/dist/auth-2-jfgiHn.d.ts +34 -0
  16. package/dist/auth.d.ts +1 -26
  17. package/dist/auth.js +6 -40
  18. package/dist/chunk-3FIKGFKI.js +54 -0
  19. package/dist/chunk-4FMALWYB.js +669 -0
  20. package/dist/chunk-4GPCWBBA.js +143 -0
  21. package/dist/chunk-4UE5TCMQ.js +104 -0
  22. package/dist/chunk-65VPWMPV.js +1134 -0
  23. package/dist/chunk-6HFAEZ52.js +29 -0
  24. package/dist/chunk-6SVOHNIC.js +17 -0
  25. package/dist/chunk-BDNNPRX6.js +41 -0
  26. package/dist/chunk-BRNEKPWX.js +37 -0
  27. package/dist/chunk-BXJ2J27B.js +219 -0
  28. package/dist/chunk-CSJWB6D3.js +151 -0
  29. package/dist/chunk-CXBZTONY.js +65 -0
  30. package/dist/chunk-EE4M2UOF.js +818 -0
  31. package/dist/chunk-GFRAYYRD.js +817 -0
  32. package/dist/chunk-IVQHLI56.js +1890 -0
  33. package/dist/chunk-IZ6Q4SE3.js +178 -0
  34. package/dist/chunk-JKMQSAJP.js +174 -0
  35. package/dist/chunk-K7E34Q3A.js +1445 -0
  36. package/dist/chunk-KVG7YCTS.js +115 -0
  37. package/dist/chunk-L23O2FFE.js +51 -0
  38. package/dist/chunk-MFVUENZR.js +1549 -0
  39. package/dist/chunk-ML7D5DWY.js +263 -0
  40. package/dist/chunk-MQXYDYRD.js +968 -0
  41. package/dist/chunk-POR5L2DG.js +247 -0
  42. package/dist/chunk-QJVSKKRQ.js +369 -0
  43. package/dist/chunk-QQQYCWSC.js +240 -0
  44. package/dist/chunk-R5U7XKVJ.js +16 -0
  45. package/dist/chunk-RYKLVOVK.js +309 -0
  46. package/dist/chunk-SNVGDYX7.js +187 -0
  47. package/dist/chunk-T6OZMPPV.js +68 -0
  48. package/dist/chunk-UKZIAQA7.js +26 -0
  49. package/dist/chunk-VGOA5Y7S.js +251 -0
  50. package/dist/chunk-VWGP4NVJ.js +513 -0
  51. package/dist/chunk-WBVO43HU.js +355 -0
  52. package/dist/chunk-WUOAQVOA.js +86 -0
  53. package/dist/chunk-XJTRY453.js +868 -0
  54. package/dist/chunk-YOBAUCVN.js +583 -0
  55. package/dist/cli-RSRk1M36.d.ts +707 -0
  56. package/dist/cli.d.ts +7 -309
  57. package/dist/cli.js +125 -945
  58. package/dist/completion-csVun6SP.d.ts +112 -0
  59. package/dist/completion-values-By-mnjNt.d.ts +142 -0
  60. package/dist/completion-values.d.ts +1 -0
  61. package/dist/completion-values.js +29 -0
  62. package/dist/completion.d.ts +3 -0
  63. package/dist/completion.js +34 -0
  64. package/dist/compliance-AxFf-uMy.d.ts +60 -0
  65. package/dist/compliance.d.ts +1 -0
  66. package/dist/compliance.js +17 -0
  67. package/dist/config-CaK60ByQ.d.ts +241 -0
  68. package/dist/config.d.ts +1 -97
  69. package/dist/config.js +8 -78
  70. package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
  71. package/dist/errcorrect.d.ts +1 -37
  72. package/dist/errcorrect.js +8 -68
  73. package/dist/error-Do8Qji43.d.ts +231 -0
  74. package/dist/id/index.d.ts +2 -83
  75. package/dist/id/index.js +14 -65
  76. package/dist/index-BKJZClJx.d.ts +1486 -0
  77. package/dist/index-C3Zw4wDJ.d.ts +359 -0
  78. package/dist/index-CGE5yLBj.d.ts +328 -0
  79. package/dist/index-VLDcI5NF.d.ts +95 -0
  80. package/dist/index.d.ts +37 -0
  81. package/dist/index.js +132 -0
  82. package/dist/llm-DYuR7_uX.d.ts +1069 -0
  83. package/dist/llm-anthropic-Cd_I2xXB.d.ts +67 -0
  84. package/dist/llm-anthropic.d.ts +2 -0
  85. package/dist/llm-anthropic.js +13 -0
  86. package/dist/llm-openai-DyFrucmV.d.ts +87 -0
  87. package/dist/llm-openai.d.ts +2 -0
  88. package/dist/llm-openai.js +19 -0
  89. package/dist/llm.d.ts +1 -188
  90. package/dist/llm.js +101 -356
  91. package/dist/log-CkaxMX3U.d.ts +115 -0
  92. package/dist/log.d.ts +2 -0
  93. package/dist/log.js +14 -0
  94. package/dist/mcp/index.d.ts +4 -0
  95. package/dist/mcp/index.js +185 -0
  96. package/dist/netpolicy-Brx5XZUB.d.ts +151 -0
  97. package/dist/netpolicy.d.ts +1 -0
  98. package/dist/netpolicy.js +25 -0
  99. package/dist/output-CjZWmL8s.d.ts +420 -0
  100. package/dist/output.d.ts +3 -213
  101. package/dist/output.js +135 -447
  102. package/dist/parity-CvMuYcVI.d.ts +61 -0
  103. package/dist/progress-CAzyxeWl.d.ts +38 -0
  104. package/dist/progress.d.ts +1 -30
  105. package/dist/progress.js +6 -55
  106. package/dist/provenance-SMFHm5gx.d.ts +30 -0
  107. package/dist/provenance.d.ts +1 -24
  108. package/dist/provenance.js +6 -31
  109. package/dist/routellm-B0nv59mH.d.ts +111 -0
  110. package/dist/routellm.d.ts +2 -64
  111. package/dist/routellm.js +13 -255
  112. package/dist/rpc-D31Lg0Kc.d.ts +32 -0
  113. package/dist/rpc.d.ts +2 -25
  114. package/dist/rpc.js +4 -98
  115. package/dist/safety-CUaYEjZc.d.ts +30 -0
  116. package/dist/safety.d.ts +1 -24
  117. package/dist/safety.js +6 -43
  118. package/dist/scope-BrhYB4ij.d.ts +141 -0
  119. package/dist/scope.d.ts +1 -121
  120. package/dist/scope.js +20 -377
  121. package/dist/serve-D8yWMrTc.d.ts +646 -0
  122. package/dist/serve.d.ts +3 -0
  123. package/dist/serve.js +75 -0
  124. package/dist/sqlstore.js +6 -39
  125. package/dist/stream-CrjeCp0M.d.ts +44 -0
  126. package/dist/stream.d.ts +1 -35
  127. package/dist/stream.js +8 -51
  128. package/dist/telemetry/index.d.ts +1 -315
  129. package/dist/telemetry/index.js +26 -599
  130. package/dist/toolspec-DcPwwoAZ.d.ts +368 -0
  131. package/dist/toolspec.d.ts +2 -0
  132. package/dist/toolspec.js +37 -0
  133. package/dist/tui/index.d.ts +6 -298
  134. package/dist/tui/index.js +17 -381
  135. package/dist/upgrade-C3RWGwRi.d.ts +125 -0
  136. package/dist/upgrade.d.ts +1 -95
  137. package/dist/upgrade.js +6 -114
  138. package/dist/xdg-DB7Shoib.d.ts +179 -0
  139. package/dist/xdg.d.ts +1 -169
  140. package/dist/xdg.js +14 -128
  141. package/package.json +192 -24
  142. package/src/README.md +15 -0
  143. package/src/alias.conformance.test.ts +250 -0
  144. package/src/alias.test.ts +206 -2
  145. package/src/alias.ts +196 -21
  146. package/src/cli.help-command.test.ts +105 -0
  147. package/src/cli.offline.test.ts +93 -0
  148. package/src/cli.parity.test.ts +192 -0
  149. package/src/cli.root.conformance.test.ts +166 -0
  150. package/src/cli.test.ts +95 -1
  151. package/src/cli.ts +234 -25
  152. package/src/completion-scripts.ts +16 -0
  153. package/src/completion-values.test.ts +76 -16
  154. package/src/completion-values.ts +170 -66
  155. package/src/completion.conformance.test.ts +298 -0
  156. package/src/completion.test.ts +268 -145
  157. package/src/completion.ts +772 -121
  158. package/src/compliance.test.ts +196 -2
  159. package/src/compliance.ts +440 -20
  160. package/src/config-merge.test.ts +425 -0
  161. package/src/config.test.ts +228 -1
  162. package/src/config.ts +429 -16
  163. package/src/delegation.test.ts +299 -0
  164. package/src/delegation.ts +807 -0
  165. package/src/gen/crud_pb.ts +7 -7
  166. package/src/id/README.md +38 -0
  167. package/src/id/contract.test.ts +1 -1
  168. package/src/id/index.test.ts +1 -1
  169. package/src/id/index.ts +3 -2
  170. package/src/index.test.ts +71 -0
  171. package/src/index.ts +46 -0
  172. package/src/llm-anthropic.conformance.test.ts +1135 -0
  173. package/src/llm-anthropic.test.ts +152 -0
  174. package/src/llm-anthropic.ts +940 -0
  175. package/src/llm-choice-hermetic.test.ts +335 -0
  176. package/src/llm-config.test.ts +232 -0
  177. package/src/llm-config.ts +106 -0
  178. package/src/llm-endpoint.ts +254 -0
  179. package/src/llm-error.ts +17 -0
  180. package/src/llm-errors.ts +152 -0
  181. package/src/llm-file.ts +126 -0
  182. package/src/llm-http.ts +317 -0
  183. package/src/llm-keys.ts +167 -0
  184. package/src/llm-media.conformance.test.ts +87 -0
  185. package/src/llm-media.test.ts +279 -0
  186. package/src/llm-media.ts +322 -0
  187. package/src/llm-openai.conformance.test.ts +407 -0
  188. package/src/llm-openai.test.ts +510 -0
  189. package/src/llm-openai.ts +708 -0
  190. package/src/llm-payload.ts +155 -0
  191. package/src/llm-quote.ts +46 -0
  192. package/src/llm-routellm.conformance.test.ts +44 -0
  193. package/src/llm-toolnames.conformance.test.ts +112 -0
  194. package/src/llm-toolnames.test.ts +132 -0
  195. package/src/llm-toolnames.ts +298 -0
  196. package/src/llm-uri.ts +99 -0
  197. package/src/llm.test.ts +43 -15
  198. package/src/llm.toolcall.test.ts +175 -0
  199. package/src/llm.ts +602 -190
  200. package/src/log.parity.test.ts +105 -0
  201. package/src/log.test.ts +270 -1
  202. package/src/log.ts +228 -49
  203. package/src/mcp/README.md +63 -0
  204. package/src/mcp/args-separator.test.ts +164 -0
  205. package/src/mcp/args.test.ts +223 -0
  206. package/src/mcp/args.ts +160 -0
  207. package/src/mcp/audit.test.ts +520 -0
  208. package/src/mcp/audit.ts +70 -0
  209. package/src/mcp/bridge.test.ts +250 -0
  210. package/src/mcp/bridge.ts +286 -0
  211. package/src/mcp/conformance.test.ts +357 -0
  212. package/src/mcp/dispatch.ts +359 -0
  213. package/src/mcp/identity.test.ts +166 -0
  214. package/src/mcp/identity.ts +72 -0
  215. package/src/mcp/index.ts +176 -0
  216. package/src/mcp/legacy-notify.test.ts +59 -0
  217. package/src/mcp/legacy.ts +367 -0
  218. package/src/mcp/mcp.test.ts +707 -0
  219. package/src/mcp/modern-confirm.test.ts +520 -0
  220. package/src/mcp/modern-confirm.ts +484 -0
  221. package/src/mcp/modern.ts +788 -0
  222. package/src/mcp/stdio.test.ts +313 -0
  223. package/src/mcp/stdio.ts +254 -0
  224. package/src/mcp/tasks.ts +82 -0
  225. package/src/mcp/testtree.ts +250 -0
  226. package/src/mcp/toolname.test.ts +147 -0
  227. package/src/mcp/types.ts +689 -0
  228. package/src/netpolicy.test.ts +280 -0
  229. package/src/netpolicy.ts +256 -0
  230. package/src/output/README.md +46 -0
  231. package/src/output/builtins.ts +5 -5
  232. package/src/output/dispatch.ts +10 -2
  233. package/src/output/error.ts +459 -0
  234. package/src/output/formatter.ts +29 -5
  235. package/src/output/formatters/README.md +50 -0
  236. package/src/output/formatters/csv.ts +48 -12
  237. package/src/output/formatters/json.ts +4 -25
  238. package/src/output/formatters/project.ts +34 -0
  239. package/src/output/formatters/table.ts +8 -6
  240. package/src/output/formatters/text.ts +7 -3
  241. package/src/output/formatters/yaml.ts +5 -22
  242. package/src/output/projection.test.ts +298 -0
  243. package/src/output/projection.ts +33 -59
  244. package/src/output/template.ts +11 -21
  245. package/src/output.surface.test.ts +41 -0
  246. package/src/output.ts +64 -3
  247. package/src/plan.ts +89 -0
  248. package/src/policy.ts +472 -0
  249. package/src/published-types.test.ts +133 -0
  250. package/src/routellm.test.ts +209 -28
  251. package/src/routellm.toolcall.test.ts +521 -0
  252. package/src/routellm.ts +213 -177
  253. package/src/router/README.md +44 -0
  254. package/src/router/server.test.ts +41 -0
  255. package/src/router/server.ts +11 -0
  256. package/src/safety.test.ts +19 -9
  257. package/src/scope-defaults.json +29 -29
  258. package/src/serve.test.ts +1257 -0
  259. package/src/serve.ts +1665 -0
  260. package/src/sideeffect.ts +105 -0
  261. package/src/telemetry/README.md +48 -0
  262. package/src/telemetry/client.test.ts +31 -0
  263. package/src/telemetry/client.ts +12 -6
  264. package/src/telemetry/offline.test.ts +64 -0
  265. package/src/toolspec-internal.ts +279 -0
  266. package/src/toolspec-manifest.test.ts +167 -0
  267. package/src/toolspec-manifest.ts +415 -0
  268. package/src/toolspec-spec-schemas.ts +12 -0
  269. package/src/toolspec-spec.test.ts +273 -0
  270. package/src/toolspec-spec.ts +458 -0
  271. package/src/toolspec.from-command.test.ts +350 -0
  272. package/src/toolspec.test.ts +2 -2
  273. package/src/toolspec.ts +222 -0
  274. package/src/triton/README.md +34 -0
  275. package/src/tui/README.md +42 -0
  276. package/src/tui/parity.test.ts +120 -31
  277. package/src/tui/parity.ts +58 -7
  278. package/src/upgrade.test.ts +291 -0
  279. package/src/upgrade.ts +116 -7
  280. package/src/wizard.test.ts +7 -7
  281. package/test/llm/conformance-helpers.ts +100 -0
  282. package/test/output/csv.test.ts +164 -0
  283. package/test/output/dispatch.test.ts +1 -2
  284. package/test/output/error.test.ts +277 -0
  285. package/test/output/smoke-capture.ts +2 -2
  286. package/test/output/smoke.test.ts +2 -2
  287. package/test/output/taxonomy-contract.test.ts +148 -0
  288. package/test/setup-env.ts +16 -0
  289. package/test/toolspec/fixture.ts +79 -0
  290. package/tsconfig.json +3 -1
  291. package/tsconfig.test.json +10 -0
  292. package/vitest.config.ts +9 -0
  293. package/src/tui/parity.json +0 -74
@@ -0,0 +1,299 @@
1
+ import { mkdtempSync, mkdirSync, writeFileSync } from 'node:fs';
2
+ import { tmpdir } from 'node:os';
3
+ import { join } from 'node:path';
4
+ import { Command, CommanderError } from 'commander';
5
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
6
+
7
+ import { createCLI } from './cli';
8
+ import {
9
+ destructiveToken, dryRunApplied, isDryRun, noPrompt, resolveConfirmMode,
10
+ resolveDryRunPolicy, scriptedPrompt, type PromptSource,
11
+ } from './delegation';
12
+ import { defaultPolicyLoader, Engine, kitDefault, pathMatch, resolvePolicyPath } from './policy';
13
+ import { destructiveNames, gateSideEffect, inferredDestructive } from './sideeffect';
14
+
15
+ interface Leaf {
16
+ name: string;
17
+ se?: string;
18
+ ann?: Record<string, string>;
19
+ opts?: string[];
20
+ run?: (cmd: Command) => void;
21
+ }
22
+
23
+ interface Res { code: number; out: string; err: string; ran: string[] }
24
+
25
+ function build(leaves: Leaf[], cfg: { prompt?: PromptSource; policy?: boolean } = {}) {
26
+ const ran: string[] = [];
27
+ const { program } = createCLI({
28
+ name: 'kitapp', version: '1.0.0', description: 'd',
29
+ promptSource: cfg.prompt ?? noPrompt,
30
+ policy: cfg.policy ? defaultPolicyLoader('kitapp') : undefined,
31
+ });
32
+ let out = '';
33
+ let err = '';
34
+ program.exitOverride();
35
+ program.configureOutput({
36
+ writeOut: (s) => { out += s; },
37
+ writeErr: (s) => { err += s; },
38
+ });
39
+ for (const l of leaves) {
40
+ const c = program.command(l.name).description(l.name);
41
+ c.exitOverride();
42
+ for (const o of l.opts ?? []) c.option(o);
43
+ const ann = { ...(l.ann ?? {}) };
44
+ if (l.se !== undefined) ann['kit/side-effect'] = l.se;
45
+ (c as unknown as { annotations: Record<string, string> }).annotations = ann;
46
+ c.action(() => { ran.push(l.name); l.run?.(c); });
47
+ }
48
+ const run = (args: string[]): Res => {
49
+ let code = 0;
50
+ try {
51
+ program.parse(['node', 'kitapp', ...args]);
52
+ } catch (e) {
53
+ if (!(e instanceof CommanderError)) throw e;
54
+ code = e.exitCode;
55
+ }
56
+ return { code, out, err, ran };
57
+ };
58
+ return { program, run };
59
+ }
60
+
61
+ let xdg: string;
62
+ beforeEach(() => {
63
+ xdg = mkdtempSync(join(tmpdir(), 'ts-gate-'));
64
+ process.env['XDG_CONFIG_HOME'] = xdg;
65
+ });
66
+ afterEach(() => { delete process.env['XDG_CONFIG_HOME']; });
67
+
68
+ function policyFile(name: string, body: string): void {
69
+ mkdirSync(join(xdg, 'kitapp', 'policies'), { recursive: true });
70
+ writeFileSync(join(xdg, 'kitapp', 'policies', `${name}.yaml`), body);
71
+ }
72
+
73
+ describe('side-effect inference', () => {
74
+ it('infers only for an absent or empty annotation', () => {
75
+ expect(inferredDestructive('delete', {})).toBe(true);
76
+ expect(inferredDestructive('delete', { 'kit/side-effect': '' })).toBe(true);
77
+ expect(inferredDestructive('delete', { 'kit/side-effect': 'read' })).toBe(false);
78
+ expect(inferredDestructive('delete', { 'kit/side-effect': 'bogus' })).toBe(false);
79
+ expect(inferredDestructive('delete-all', {})).toBe(false);
80
+ expect(gateSideEffect('rm', undefined)).toBe('destructive-shared');
81
+ expect(destructiveNames()).toEqual(['delete', 'destroy', 'drop', 'purge', 'remove', 'rm']);
82
+ });
83
+ });
84
+
85
+ describe('confirm gate', () => {
86
+ const l: Leaf[] = [{ name: 'rm', se: 'destructive' }, { name: 'add', se: 'write' }];
87
+
88
+ it('refuses a destructive without a terminal', () => {
89
+ const r = build(l).run(['rm']);
90
+ expect(r.code).toBe(5);
91
+ expect(r.err).toContain('refused: --confirm=no (or non-TTY default)');
92
+ expect(r.ran).toEqual([]);
93
+ });
94
+ it.each(['yes', 'auto'])('--confirm %s lets it run', (m) => {
95
+ const r = build(l).run(['--confirm', m, 'rm']);
96
+ expect(r.code).toBe(0);
97
+ expect(r.ran).toEqual(['rm']);
98
+ });
99
+ it('prompt: y accepts, n aborts', () => {
100
+ expect(build(l, { prompt: scriptedPrompt(['y']) }).run(['--confirm', 'prompt', 'rm']).code).toBe(0);
101
+ const r = build(l, { prompt: scriptedPrompt(['n']) }).run(['--confirm', 'prompt', 'rm']);
102
+ expect(r.code).toBe(5);
103
+ expect(r.err).toContain('aborted by user at confirm prompt for kitapp rm');
104
+ });
105
+ it('unset confirm prompts only when a terminal exists', () => {
106
+ expect(resolveConfirmMode(noPrompt, '')).toBe('no');
107
+ expect(resolveConfirmMode(scriptedPrompt([]), '')).toBe('prompt');
108
+ expect(build(l, { prompt: scriptedPrompt(['yes']) }).run(['rm']).code).toBe(0);
109
+ });
110
+ it('a write needs no confirmation', () => {
111
+ expect(build(l).run(['add']).code).toBe(0);
112
+ });
113
+ it('machine format gets exactly one json envelope', () => {
114
+ const r = build(l).run(['--format', 'json', 'rm']);
115
+ expect(r.code).toBe(5);
116
+ expect(JSON.parse(r.err).code).toBe('UNAUTHORIZED');
117
+ });
118
+ it('a leaf-local --confirm is the leaf mode, as in Go', () => {
119
+ const r = build([{ name: 'rm', se: 'destructive', opts: ['--confirm <m>'] }]).run(['rm', '--confirm', 'yes']);
120
+ expect(r.code).toBe(0);
121
+ });
122
+ });
123
+
124
+ describe('typed token', () => {
125
+ const l: Leaf[] = [{ name: 'purge', se: 'destructive', ann: { 'kit/destructive-token': 'required' } }];
126
+ it('token is first 6 bytes of sha256 of the path', () => {
127
+ expect(destructiveToken('kitapp purge')).toBe('9bcc9064589a');
128
+ });
129
+ it('required, accepted, mismatched', () => {
130
+ const miss = build(l).run(['--confirm', 'yes', 'purge']);
131
+ expect(miss.code).toBe(5);
132
+ expect(miss.err).toContain('requires --confirm-token=9bcc9064589a');
133
+ expect(build(l).run(['purge', '--confirm-token', '9bcc9064589a']).code).toBe(0);
134
+ const bad = build(l).run(['purge', '--confirm-token', 'deadbeef0000']);
135
+ expect(bad.code).toBe(5);
136
+ expect(bad.err).toContain('--confirm-token mismatch for kitapp purge; expected 9bcc9064589a');
137
+ });
138
+ it('machine format carries the fix in the envelope, no prose preview', () => {
139
+ const r = build(l).run(['--format', 'json', 'purge']);
140
+ expect(JSON.parse(r.err).suggested_fix).toContain('--confirm-token=9bcc9064589a');
141
+ });
142
+ });
143
+
144
+ describe('policy', () => {
145
+ const l: Leaf[] = [
146
+ { name: 'add', se: 'write' }, { name: 'ls', se: 'read' }, { name: 'rm', se: 'destructive' },
147
+ { name: 'import', se: 'write', opts: ['--policy <file>'], run: () => {} },
148
+ ];
149
+ it('refuses, and --confirm yes does not lift it', () => {
150
+ policyFile('readonly', 'allow:\n write: []\n destructive: []\n');
151
+ expect(build(l, { policy: true }).run(['--policy', 'readonly', 'add']).err).toContain('policy: write not allowed for add');
152
+ const r = build(l, { policy: true }).run(['--policy', 'readonly', '--confirm', 'yes', 'rm']);
153
+ expect(r.code).toBe(5);
154
+ expect(build(l, { policy: true }).run(['--policy', 'readonly', 'ls']).code).toBe(0);
155
+ });
156
+ it('require_confirm lifts a write into the gate', () => {
157
+ policyFile('confirm', 'require_confirm:\n - add\n');
158
+ expect(build(l, { policy: true }).run(['--policy', 'confirm', 'add']).err).toContain('refused: --confirm=no');
159
+ expect(build(l, { policy: true }).run(['--policy', 'confirm', '--confirm', 'yes', 'add']).code).toBe(0);
160
+ });
161
+ it('unknown and unparsable policies are usage errors', () => {
162
+ policyFile('broken', 'allow: [\n');
163
+ for (const n of ['ghost', 'broken']) {
164
+ const r = build(l, { policy: true }).run(['--policy', n, 'add']);
165
+ expect(r.code).toBe(2);
166
+ expect(r.err).toContain(`policy "${n}"`);
167
+ }
168
+ });
169
+ it('no loader wired: only kit-default resolves', () => {
170
+ expect(build(l).run(['--policy', 'x', 'add']).code).toBe(2);
171
+ const r = build(l).run(['--policy', 'kit-default', 'add']);
172
+ expect(r.code).toBe(5);
173
+ expect(r.err).toContain('policy kit-default:');
174
+ });
175
+ it('validates the name before any filesystem access', () => {
176
+ for (const n of ['../x', join(xdg, 'kitapp', 'policies', 'readonly'), 'a b', 'x'.repeat(65)]) {
177
+ const r = build(l, { policy: true }).run(['--policy', n, 'add']);
178
+ expect(r.code).toBe(2);
179
+ expect(r.err).toContain('invalid policy name');
180
+ }
181
+ expect(() => resolvePolicyPath('t', '..')).toThrow(/invalid policy name/);
182
+ });
183
+ it('a leaf-local --policy is not the delegation policy', () => {
184
+ const r = build(l, { policy: true }).run(['import', '--policy', '/tmp/some.yaml']);
185
+ expect(r.code).toBe(0);
186
+ expect(r.ran).toEqual(['import']);
187
+ });
188
+ it('a root --policy before a shadowing leaf still gates', () => {
189
+ policyFile('readonly', 'allow:\n write: []\n');
190
+ const r = build(l, { policy: true }).run(['--policy', 'readonly', 'import', '--policy', 'f']);
191
+ expect(r.code).toBe(5);
192
+ });
193
+ it('engine globs, tiers and budget', () => {
194
+ expect(pathMatch('a*', 'abc')).toBe(true);
195
+ expect(pathMatch('a*', 'a/b')).toBe(false);
196
+ const e = new Engine(kitDefault(), 1);
197
+ expect(e.authorize({ verb: 'x', sideEffect: 'destructive-shared' }).allowed).toBe(false);
198
+ e.recordOp();
199
+ expect(() => e.recordOp()).toThrow(/max-ops/);
200
+ });
201
+ });
202
+
203
+ describe('max-ops', () => {
204
+ it('within budget runs; a non-integer is a usage error', () => {
205
+ const l: Leaf[] = [{ name: 'add', se: 'write' }];
206
+ expect(build(l).run(['--max-ops', '1', 'add']).code).toBe(0);
207
+ expect(build(l).run(['--max-ops', 'x', 'add']).code).toBe(2);
208
+ });
209
+ });
210
+
211
+ describe('dry-run', () => {
212
+ const l: Leaf[] = [
213
+ { name: 'ls', se: 'read' }, { name: 'add', se: 'write' }, { name: 'rm', se: 'destructive' },
214
+ { name: 'shell', se: 'interactive' }, { name: 'sync', se: 'write', ann: { 'kit/dry-run': 'opted-out' } },
215
+ ];
216
+ const seen: Record<string, boolean> = {};
217
+ const probe = (name: string): Leaf => ({ name, se: name === 'rm' ? 'destructive' : name === 'ls' ? 'read' : 'write',
218
+ run: (c) => { seen[name] = dryRunApplied(c); seen[`${name}:is`] = isDryRun(c); } });
219
+
220
+ it('applies to a write, skips the destructive confirm, no-ops a read', () => {
221
+ const b = build(['add', 'rm', 'ls'].map(probe));
222
+ expect(b.run(['--dry-run', 'add']).code).toBe(0);
223
+ expect(seen['add']).toBe(true);
224
+ expect(b.run(['--dry-run', 'rm']).code).toBe(0);
225
+ expect(seen['rm']).toBe(true);
226
+ expect(b.run(['--dry-run', 'ls']).code).toBe(0);
227
+ expect(seen['ls']).toBe(false);
228
+ expect(seen['ls:is']).toBe(true);
229
+ });
230
+ it('refuses interactive and opted-out leaves', () => {
231
+ const r = build(l).run(['--dry-run', 'shell']);
232
+ expect(r.code).toBe(2);
233
+ expect(r.err).toContain('"kitapp shell" does not accept --dry-run: interactive');
234
+ expect(build(l).run(['--dry-run', 'sync']).err).toContain('explicitly opted out');
235
+ expect(JSON.parse(build(l).run(['--format', 'json', '--dry-run', 'shell']).err).code).toBe('USAGE');
236
+ });
237
+ it('dry-run table', () => {
238
+ expect(resolveDryRunPolicy('delete', {})).toBe('allow');
239
+ expect(resolveDryRunPolicy('list', {})).toBe('reject-untagged');
240
+ expect(resolveDryRunPolicy('delete', { 'kit/side-effect': 'bogus' })).toBe('reject-malformed');
241
+ expect(resolveDryRunPolicy('x', { 'kit/side-effect': 'read' })).toBe('noop');
242
+ });
243
+ it('inferred destructive: kit previews, handler never runs, nothing mutates', () => {
244
+ let mutated = false;
245
+ const b = build([{ name: 'delete', run: () => { mutated = true; } }]);
246
+ const r = b.run(['--dry-run', '--format', 'json', 'delete']);
247
+ expect(r.code).toBe(0);
248
+ expect(mutated).toBe(false);
249
+ expect(r.ran).toEqual([]);
250
+ expect(JSON.parse(r.out).command).toBe('kitapp delete');
251
+ });
252
+ it('inferred destructive under a policy refusal is still refused in dry-run', () => {
253
+ policyFile('nodel', 'allow:\n destructive: []\n');
254
+ const b = build([{ name: 'delete' }], { policy: true });
255
+ expect(b.run(['--policy', 'nodel', '--dry-run', 'delete']).code).toBe(5);
256
+ });
257
+ });
258
+
259
+ describe('inferred destructive gate', () => {
260
+ const l: Leaf[] = [
261
+ { name: 'delete' }, { name: 'list' }, { name: 'drop', se: 'read' }, { name: 'delete-all' },
262
+ ];
263
+ it('gates the unannotated destructive name, nothing else', () => {
264
+ expect(build(l).run(['delete']).code).toBe(5);
265
+ expect(build(l).run(['--confirm', 'yes', 'delete']).code).toBe(0);
266
+ for (const n of ['list', 'drop', 'delete-all']) expect(build(l).run([n]).code).toBe(0);
267
+ });
268
+ it('the destructive name is the leaf name below a group', () => {
269
+ const { program } = createCLI({ name: 'kitapp', version: '1', description: 'd', promptSource: noPrompt });
270
+ program.exitOverride();
271
+ program.configureOutput({ writeErr: () => {} });
272
+ const g = program.command('widget').exitOverride();
273
+ g.command('remove').exitOverride().action(() => {});
274
+ g.command('list').exitOverride().action(() => {});
275
+ expect(() => program.parse(['node', 'k', 'widget', 'remove'])).toThrow();
276
+ expect(() => program.parse(['node', 'k', 'widget', 'list'])).not.toThrow();
277
+ });
278
+ });
279
+
280
+ describe('malformed side-effect tier', () => {
281
+ it('is refused as usage before the handler runs, unconfirmed or not', () => {
282
+ let mutated = false;
283
+ const mk = () => build([{ name: 'wipe', se: 'destructve', run: () => { mutated = true; } }]);
284
+ for (const args of [['wipe'], ['--confirm', 'yes', 'wipe'], ['--dry-run', 'wipe']]) {
285
+ const r = mk().run(args);
286
+ expect(r.code).toBe(2);
287
+ expect(r.err).toContain('malformed kit/side-effect tag "destructve"');
288
+ }
289
+ expect(mutated).toBe(false);
290
+ });
291
+ it('an explicit malformed tier beats the destructive name too', () => {
292
+ expect(build([{ name: 'purge', se: 'bogus' }]).run(['--confirm', 'yes', 'purge']).code).toBe(2);
293
+ });
294
+ it('the engine fails closed on an unknown non-empty tier', () => {
295
+ const e = new Engine({ ...kitDefault(), allow: undefined, unannotated: undefined }, 0);
296
+ expect(e.authorize({ verb: 'x', sideEffect: 'destructve' }).allowed).toBe(false);
297
+ expect(e.authorize({ verb: 'x', sideEffect: '' }).allowed).toBe(true);
298
+ });
299
+ });