@hop-top/kit 0.5.0-alpha.10 → 0.5.0-alpha.12

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 (68) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +4 -4
  3. package/dist/alias.js +3 -3
  4. package/dist/{chunk-T46YPHKN.js → chunk-4FMALWYB.js} +45 -45
  5. package/dist/{chunk-JZ22ZUFA.js → chunk-65VPWMPV.js} +4 -1
  6. package/dist/{chunk-YGYQUFGY.js → chunk-BXJ2J27B.js} +10 -10
  7. package/dist/chunk-CXBZTONY.js +65 -0
  8. package/dist/{chunk-K7VCKJ27.js → chunk-EE4M2UOF.js} +8 -4
  9. package/dist/{chunk-72XZWQX7.js → chunk-GFRAYYRD.js} +475 -162
  10. package/dist/{chunk-2JV3A7OP.js → chunk-IVQHLI56.js} +6 -10
  11. package/dist/{chunk-NLMS7HGP.js → chunk-IZ6Q4SE3.js} +3 -3
  12. package/dist/chunk-MFVUENZR.js +1549 -0
  13. package/dist/{chunk-BLS2OBIQ.js → chunk-ML7D5DWY.js} +2 -2
  14. package/dist/chunk-P3Y46PYF.js +138 -0
  15. package/dist/{chunk-GGWJRWWA.js → chunk-RYKLVOVK.js} +7 -7
  16. package/dist/{chunk-QPUCXQXT.js → chunk-T6OZMPPV.js} +2 -1
  17. package/dist/{chunk-MUIQVU6E.js → chunk-VGOA5Y7S.js} +11 -11
  18. package/dist/{chunk-ABQSE7AI.js → chunk-VWGP4NVJ.js} +38 -38
  19. package/dist/chunk-X4E5QGEB.js +171 -0
  20. package/dist/cli-RSRk1M36.d.ts +707 -0
  21. package/dist/cli.d.ts +1 -1
  22. package/dist/cli.js +60 -5
  23. package/dist/completion.js +2 -2
  24. package/dist/id/index.d.ts +1 -1
  25. package/dist/id/index.js +4 -2
  26. package/dist/{index-DCe1boBz.d.ts → index-CGE5yLBj.d.ts} +1 -1
  27. package/dist/{index-VLDcI5NF.d.ts → index-D9zpUrpE.d.ts} +18 -6
  28. package/dist/index.d.ts +4 -4
  29. package/dist/index.js +23 -23
  30. package/dist/llm-anthropic.js +5 -5
  31. package/dist/llm-openai.js +5 -5
  32. package/dist/llm.js +3 -3
  33. package/dist/log.js +3 -3
  34. package/dist/mcp/index.js +3 -2
  35. package/dist/output.js +2 -4
  36. package/dist/routellm.js +6 -6
  37. package/dist/toolspec.js +4 -4
  38. package/dist/tui/index.d.ts +2 -2
  39. package/dist/tui/index.js +3 -3
  40. package/dist/{upgrade-p_zh3pvV.d.ts → upgrade-BJ2z6Oqf.d.ts} +58 -4
  41. package/dist/upgrade.d.ts +1 -1
  42. package/dist/upgrade.js +4 -2
  43. package/package.json +9 -9
  44. package/src/cli.root.conformance.test.ts +166 -0
  45. package/src/cli.ts +39 -0
  46. package/src/completion.conformance.test.ts +3 -2
  47. package/src/completion.test.ts +12 -0
  48. package/src/completion.ts +8 -3
  49. package/src/delegation.test.ts +299 -0
  50. package/src/delegation.ts +807 -0
  51. package/src/id/README.md +2 -2
  52. package/src/id/contract.test.ts +11 -8
  53. package/src/id/differential.test.ts +54 -0
  54. package/src/id/index.ts +123 -17
  55. package/src/mcp/bridge.test.ts +28 -0
  56. package/src/mcp/bridge.ts +5 -10
  57. package/src/plan.ts +89 -0
  58. package/src/policy.ts +472 -0
  59. package/src/sideeffect.ts +105 -0
  60. package/src/toolspec-manifest.ts +7 -2
  61. package/src/upgrade.test.ts +393 -1
  62. package/src/upgrade.ts +183 -25
  63. package/dist/chunk-GHPJJV5L.js +0 -613
  64. package/dist/chunk-IXVQ62NZ.js +0 -334
  65. package/dist/chunk-L23O2FFE.js +0 -51
  66. package/dist/chunk-WWUKGJB5.js +0 -90
  67. package/dist/cli-C5B5tnpA.d.ts +0 -400
  68. package/dist/{chunk-PSFYJGDG.js → chunk-K7E34Q3A.js} +2 -2
@@ -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
+ });