@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.
- package/CHANGELOG.md +42 -0
- package/README.md +4 -4
- package/dist/alias.js +3 -3
- package/dist/{chunk-T46YPHKN.js → chunk-4FMALWYB.js} +45 -45
- package/dist/{chunk-JZ22ZUFA.js → chunk-65VPWMPV.js} +4 -1
- package/dist/{chunk-YGYQUFGY.js → chunk-BXJ2J27B.js} +10 -10
- package/dist/chunk-CXBZTONY.js +65 -0
- package/dist/{chunk-K7VCKJ27.js → chunk-EE4M2UOF.js} +8 -4
- package/dist/{chunk-72XZWQX7.js → chunk-GFRAYYRD.js} +475 -162
- package/dist/{chunk-2JV3A7OP.js → chunk-IVQHLI56.js} +6 -10
- package/dist/{chunk-NLMS7HGP.js → chunk-IZ6Q4SE3.js} +3 -3
- package/dist/chunk-MFVUENZR.js +1549 -0
- package/dist/{chunk-BLS2OBIQ.js → chunk-ML7D5DWY.js} +2 -2
- package/dist/chunk-P3Y46PYF.js +138 -0
- package/dist/{chunk-GGWJRWWA.js → chunk-RYKLVOVK.js} +7 -7
- package/dist/{chunk-QPUCXQXT.js → chunk-T6OZMPPV.js} +2 -1
- package/dist/{chunk-MUIQVU6E.js → chunk-VGOA5Y7S.js} +11 -11
- package/dist/{chunk-ABQSE7AI.js → chunk-VWGP4NVJ.js} +38 -38
- package/dist/chunk-X4E5QGEB.js +171 -0
- package/dist/cli-RSRk1M36.d.ts +707 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +60 -5
- package/dist/completion.js +2 -2
- package/dist/id/index.d.ts +1 -1
- package/dist/id/index.js +4 -2
- package/dist/{index-DCe1boBz.d.ts → index-CGE5yLBj.d.ts} +1 -1
- package/dist/{index-VLDcI5NF.d.ts → index-D9zpUrpE.d.ts} +18 -6
- package/dist/index.d.ts +4 -4
- package/dist/index.js +23 -23
- package/dist/llm-anthropic.js +5 -5
- package/dist/llm-openai.js +5 -5
- package/dist/llm.js +3 -3
- package/dist/log.js +3 -3
- package/dist/mcp/index.js +3 -2
- package/dist/output.js +2 -4
- package/dist/routellm.js +6 -6
- package/dist/toolspec.js +4 -4
- package/dist/tui/index.d.ts +2 -2
- package/dist/tui/index.js +3 -3
- package/dist/{upgrade-p_zh3pvV.d.ts → upgrade-BJ2z6Oqf.d.ts} +58 -4
- package/dist/upgrade.d.ts +1 -1
- package/dist/upgrade.js +4 -2
- package/package.json +9 -9
- package/src/cli.root.conformance.test.ts +166 -0
- package/src/cli.ts +39 -0
- package/src/completion.conformance.test.ts +3 -2
- package/src/completion.test.ts +12 -0
- package/src/completion.ts +8 -3
- package/src/delegation.test.ts +299 -0
- package/src/delegation.ts +807 -0
- package/src/id/README.md +2 -2
- package/src/id/contract.test.ts +11 -8
- package/src/id/differential.test.ts +54 -0
- package/src/id/index.ts +123 -17
- package/src/mcp/bridge.test.ts +28 -0
- package/src/mcp/bridge.ts +5 -10
- package/src/plan.ts +89 -0
- package/src/policy.ts +472 -0
- package/src/sideeffect.ts +105 -0
- package/src/toolspec-manifest.ts +7 -2
- package/src/upgrade.test.ts +393 -1
- package/src/upgrade.ts +183 -25
- package/dist/chunk-GHPJJV5L.js +0 -613
- package/dist/chunk-IXVQ62NZ.js +0 -334
- package/dist/chunk-L23O2FFE.js +0 -51
- package/dist/chunk-WWUKGJB5.js +0 -90
- package/dist/cli-C5B5tnpA.d.ts +0 -400
- package/dist/{chunk-PSFYJGDG.js → chunk-K7E34Q3A.js} +2 -2
package/src/id/README.md
CHANGED
|
@@ -21,10 +21,10 @@ console.log(id, parse(id).uuid);
|
|
|
21
21
|
|
|
22
22
|
## Contract
|
|
23
23
|
|
|
24
|
-
-
|
|
24
|
+
- Self-contained TypeID (spec v0.3, UUIDv7), no runtime dependency; the string returned by `newId` is the JSON wire form and round-trips losslessly through `parse`. `fromUUID` encodes an existing UUID.
|
|
25
25
|
- Prefix grammar and length limit follow the typeid spec pinned in the fixture; a divergence is a wire-format break.
|
|
26
26
|
- No transitive dependency on the URI registry, by design.
|
|
27
|
-
- Parity: [`contracts/typeid-v1/fixtures.json`](../../../../contracts/typeid-v1/fixtures.json), replayed by [`contract.test.ts`](contract.test.ts).
|
|
27
|
+
- Parity: [`contracts/typeid-v1/fixtures.json`](../../../../contracts/typeid-v1/fixtures.json), replayed by [`contract.test.ts`](contract.test.ts); [`differential.test.ts`](differential.test.ts) checks against `typeid-js`, the reference implementation, as a dev dependency.
|
|
28
28
|
|
|
29
29
|
## Neighbours
|
|
30
30
|
|
package/src/id/contract.test.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
// Cross-language parity contract test.
|
|
2
2
|
//
|
|
3
3
|
// Loads contracts/typeid-v1/fixtures.json from the repo root and
|
|
4
|
-
// asserts that this TS SDK's encode (
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
4
|
+
// asserts that this TS SDK's encode (fromUUID) and decode (parse)
|
|
5
|
+
// agree with the canonical wire form shared by go/rs/py/php, and that
|
|
6
|
+
// typeid-js, the reference implementation, still agrees too. A
|
|
7
|
+
// divergence means the SDK drifted, typeid-js drifted, or the contract
|
|
8
|
+
// was edited without updating all five SDKs.
|
|
8
9
|
|
|
9
10
|
import { existsSync, readFileSync } from 'node:fs';
|
|
10
11
|
import { dirname, resolve } from 'node:path';
|
|
@@ -13,7 +14,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
13
14
|
import { describe, it, expect } from 'vitest';
|
|
14
15
|
import { TypeID } from 'typeid-js';
|
|
15
16
|
|
|
16
|
-
import { parse } from './index';
|
|
17
|
+
import { fromUUID, parse } from './index';
|
|
17
18
|
|
|
18
19
|
interface ContractVector {
|
|
19
20
|
readonly name: string;
|
|
@@ -65,9 +66,11 @@ describe('typeid-v1 contract generation (TS)', () => {
|
|
|
65
66
|
for (const v of CONTRACT.vectors) {
|
|
66
67
|
const skipped = v.skip_in?.includes('ts') ?? false;
|
|
67
68
|
const fn = skipped ? it.skip : it;
|
|
68
|
-
fn(`vector ${v.name}:
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
fn(`vector ${v.name}: fromUUID produces the pinned canonical`, () => {
|
|
70
|
+
expect(fromUUID(v.prefix, v.uuid)).toBe(v.typeid);
|
|
71
|
+
});
|
|
72
|
+
fn(`vector ${v.name}: typeid-js (reference) agrees`, () => {
|
|
73
|
+
expect(TypeID.fromUUID(v.prefix, v.uuid).toString()).toBe(v.typeid);
|
|
71
74
|
});
|
|
72
75
|
}
|
|
73
76
|
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Differential test: the SDK's self-contained TypeID against typeid-js,
|
|
2
|
+
// the reference implementation (a dev dependency only). Random UUIDs
|
|
3
|
+
// and prefixes for the happy path; a fixed list of malformed inputs
|
|
4
|
+
// both must reject.
|
|
5
|
+
|
|
6
|
+
import { randomUUID } from 'node:crypto';
|
|
7
|
+
|
|
8
|
+
import { describe, expect, it } from 'vitest';
|
|
9
|
+
import { TypeID } from 'typeid-js';
|
|
10
|
+
|
|
11
|
+
import { fromUUID, newId, parse } from './index';
|
|
12
|
+
|
|
13
|
+
const PREFIXES = ['', 'a', 'task', 'my_app', 'a__b', 'a_b_c', 'z'.repeat(63)];
|
|
14
|
+
|
|
15
|
+
describe('differential vs typeid-js', () => {
|
|
16
|
+
it('encodes and decodes like typeid-js for random UUIDs', () => {
|
|
17
|
+
for (let i = 0; i < 500; i++) {
|
|
18
|
+
const uuid = randomUUID();
|
|
19
|
+
const prefix = PREFIXES[i % PREFIXES.length]!;
|
|
20
|
+
const ours = fromUUID(prefix, uuid);
|
|
21
|
+
expect(ours).toBe(TypeID.fromUUID(prefix, uuid).toString());
|
|
22
|
+
const ref = TypeID.fromString(ours);
|
|
23
|
+
expect(parse(ours)).toEqual({ prefix: ref.getType(), uuid: ref.toUUID() });
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('mints UUIDv7 ids typeid-js accepts', () => {
|
|
28
|
+
for (const prefix of PREFIXES) {
|
|
29
|
+
const id = newId(prefix);
|
|
30
|
+
const { uuid } = parse(id);
|
|
31
|
+
expect(uuid[14]).toBe('7');
|
|
32
|
+
expect('89ab').toContain(uuid[19]);
|
|
33
|
+
expect(TypeID.fromString(id).toString()).toBe(id);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it.each([
|
|
38
|
+
'not-a-typeid',
|
|
39
|
+
'task_too_short',
|
|
40
|
+
'_01jg000000e008000000000000',
|
|
41
|
+
'Task_01jg000000e008000000000000',
|
|
42
|
+
'task__01jg000000e008000000000000',
|
|
43
|
+
'_task_01jg000000e008000000000000',
|
|
44
|
+
'task_81jg000000e008000000000000',
|
|
45
|
+
'task_01JG000000E008000000000000',
|
|
46
|
+
'task_01jg000000e00800000000000u',
|
|
47
|
+
'task_01jg000000e0080000000000000',
|
|
48
|
+
`${'a'.repeat(64)}_01jg000000e008000000000000`,
|
|
49
|
+
'task1_01jg000000e008000000000000',
|
|
50
|
+
])('rejects %s like typeid-js', (s) => {
|
|
51
|
+
expect(() => TypeID.fromString(s)).toThrow();
|
|
52
|
+
expect(() => parse(s)).toThrow();
|
|
53
|
+
});
|
|
54
|
+
});
|
package/src/id/index.ts
CHANGED
|
@@ -2,14 +2,18 @@
|
|
|
2
2
|
* @module id
|
|
3
3
|
* @package @hop-top/kit
|
|
4
4
|
*
|
|
5
|
-
* TypeID primitive
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* TypeID primitive implementing the cross-language kit API SHAPE per
|
|
6
|
+
* docs/announcements/2026-05-typeid-primitive.md, on the jetify TypeID
|
|
7
|
+
* spec v0.3. Self-contained: no runtime dependency, so no transitive
|
|
8
|
+
* uuid release reaches adopters (typeid-js 1.2.0 pins uuid ^10, under
|
|
9
|
+
* an open advisory). contracts/typeid-v1/fixtures.json and a
|
|
10
|
+
* differential test against typeid-js (dev only) keep it on the wire
|
|
11
|
+
* format.
|
|
8
12
|
*
|
|
9
13
|
* Canonical wire form: `<prefix>_<26-char-base32>` where the suffix is
|
|
10
14
|
* a Crockford-base32-encoded UUIDv7. The TS surface adds a
|
|
11
15
|
* template-literal `Typed<P>` for compile-time prefix safety on top of
|
|
12
|
-
* the runtime
|
|
16
|
+
* the runtime checks in {@link parse}.
|
|
13
17
|
*
|
|
14
18
|
* URI composition (e.g. `tlc://task/task_…`) is NOT in this module —
|
|
15
19
|
* callers should use the `@hop-top/cite` package directly. This
|
|
@@ -17,9 +21,107 @@
|
|
|
17
21
|
* registry.
|
|
18
22
|
*/
|
|
19
23
|
|
|
20
|
-
import { typeid, TypeID } from 'typeid-js';
|
|
21
24
|
import { z } from 'zod';
|
|
22
25
|
|
|
26
|
+
// Crockford base32, lowercase, as the TypeID spec defines it.
|
|
27
|
+
const ALPHABET = '0123456789abcdefghjkmnpqrstvwxyz';
|
|
28
|
+
const DECODE: Record<string, number> = Object.fromEntries(
|
|
29
|
+
[...ALPHABET].map((c, i) => [c, i]),
|
|
30
|
+
);
|
|
31
|
+
// Spec v0.3: up to 63 chars of [a-z_], starting and ending with [a-z].
|
|
32
|
+
const PREFIX_RE = /^(?:[a-z](?:[a-z_]{0,61}[a-z])?)?$/;
|
|
33
|
+
|
|
34
|
+
function checkPrefix(prefix: string): void {
|
|
35
|
+
if (!PREFIX_RE.test(prefix)) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`invalid typeid prefix '${prefix}': want up to 63 chars of [a-z_], starting and ending with a letter`,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 16 bytes -> 26 chars: 130 bits, the top two always zero.
|
|
43
|
+
function encode(bytes: Uint8Array): string {
|
|
44
|
+
let n = 0n;
|
|
45
|
+
for (const b of bytes) n = (n << 8n) | BigInt(b);
|
|
46
|
+
let out = '';
|
|
47
|
+
for (let i = 0; i < 26; i++) {
|
|
48
|
+
out = ALPHABET[Number(n & 31n)] + out;
|
|
49
|
+
n >>= 5n;
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function decode(suffix: string): Uint8Array {
|
|
55
|
+
if (suffix.length !== 26) {
|
|
56
|
+
throw new Error(`invalid typeid suffix '${suffix}': want 26 chars`);
|
|
57
|
+
}
|
|
58
|
+
if (suffix[0]! > '7') {
|
|
59
|
+
throw new Error(`invalid typeid suffix '${suffix}': first char must be 0-7`);
|
|
60
|
+
}
|
|
61
|
+
let n = 0n;
|
|
62
|
+
for (const c of suffix) {
|
|
63
|
+
const v = DECODE[c];
|
|
64
|
+
if (v === undefined) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`invalid typeid suffix '${suffix}': '${c}' is not lowercase Crockford base32`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
n = (n << 5n) | BigInt(v);
|
|
70
|
+
}
|
|
71
|
+
const bytes = new Uint8Array(16);
|
|
72
|
+
for (let i = 15; i >= 0; i--) {
|
|
73
|
+
bytes[i] = Number(n & 0xffn);
|
|
74
|
+
n >>= 8n;
|
|
75
|
+
}
|
|
76
|
+
return bytes;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function uuidString(bytes: Uint8Array): string {
|
|
80
|
+
const hex = [...bytes].map((b) => b.toString(16).padStart(2, '0')).join('');
|
|
81
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function uuidBytes(uuid: string): Uint8Array {
|
|
85
|
+
const hex = uuid.replace(/-/g, '');
|
|
86
|
+
if (!/^[0-9a-fA-F]{32}$/.test(hex)) {
|
|
87
|
+
throw new Error(`invalid uuid '${uuid}'`);
|
|
88
|
+
}
|
|
89
|
+
const bytes = new Uint8Array(16);
|
|
90
|
+
for (let i = 0; i < 16; i++) bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
91
|
+
return bytes;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// UUIDv7 (RFC 9562): 48-bit Unix-ms timestamp, version 7, variant 10,
|
|
95
|
+
// the remaining 74 bits random.
|
|
96
|
+
function uuidv7(): Uint8Array {
|
|
97
|
+
const bytes = new Uint8Array(16);
|
|
98
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
99
|
+
let ms = BigInt(Date.now());
|
|
100
|
+
for (let i = 5; i >= 0; i--) {
|
|
101
|
+
bytes[i] = Number(ms & 0xffn);
|
|
102
|
+
ms >>= 8n;
|
|
103
|
+
}
|
|
104
|
+
bytes[6] = (bytes[6]! & 0x0f) | 0x70;
|
|
105
|
+
bytes[8] = (bytes[8]! & 0x3f) | 0x80;
|
|
106
|
+
return bytes;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function format(prefix: string, bytes: Uint8Array): string {
|
|
110
|
+
const suffix = encode(bytes);
|
|
111
|
+
return prefix === '' ? suffix : `${prefix}_${suffix}`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Canonical TypeID string for `prefix` and an existing UUID (any
|
|
116
|
+
* version, hyphenated or not).
|
|
117
|
+
*
|
|
118
|
+
* @throws {Error} on an invalid prefix or UUID.
|
|
119
|
+
*/
|
|
120
|
+
export function fromUUID(prefix: string, uuid: string): string {
|
|
121
|
+
checkPrefix(prefix);
|
|
122
|
+
return format(prefix, uuidBytes(uuid));
|
|
123
|
+
}
|
|
124
|
+
|
|
23
125
|
/**
|
|
24
126
|
* Compile-time-typed canonical TypeID string for prefix `P`.
|
|
25
127
|
*
|
|
@@ -47,7 +149,8 @@ export interface Parsed {
|
|
|
47
149
|
* newId('task') // => 'task_01h2e8kqv8000000000000000'
|
|
48
150
|
*/
|
|
49
151
|
export function newId(prefix: string): string {
|
|
50
|
-
|
|
152
|
+
checkPrefix(prefix);
|
|
153
|
+
return format(prefix, uuidv7());
|
|
51
154
|
}
|
|
52
155
|
|
|
53
156
|
/**
|
|
@@ -57,11 +160,14 @@ export function newId(prefix: string): string {
|
|
|
57
160
|
* (e.g. malformed suffix, illegal prefix characters).
|
|
58
161
|
*/
|
|
59
162
|
export function parse(s: string): Parsed {
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
163
|
+
const sep = s.lastIndexOf('_');
|
|
164
|
+
const prefix = sep === -1 ? '' : s.slice(0, sep);
|
|
165
|
+
const suffix = sep === -1 ? s : s.slice(sep + 1);
|
|
166
|
+
if (sep !== -1 && prefix === '') {
|
|
167
|
+
throw new Error(`invalid typeid '${s}': empty prefix with a separator`);
|
|
168
|
+
}
|
|
169
|
+
checkPrefix(prefix);
|
|
170
|
+
return { prefix, uuid: uuidString(decode(suffix)) };
|
|
65
171
|
}
|
|
66
172
|
|
|
67
173
|
/**
|
|
@@ -70,7 +176,7 @@ export function parse(s: string): Parsed {
|
|
|
70
176
|
* between e.g. `TaskId` and `InvoiceId`.
|
|
71
177
|
*/
|
|
72
178
|
export function newTyped<P extends string>(prefix: P): Typed<P> {
|
|
73
|
-
return
|
|
179
|
+
return newId(prefix) as Typed<P>;
|
|
74
180
|
}
|
|
75
181
|
|
|
76
182
|
/**
|
|
@@ -82,11 +188,11 @@ export function newTyped<P extends string>(prefix: P): Typed<P> {
|
|
|
82
188
|
* when the input is not a syntactically valid TypeID.
|
|
83
189
|
*/
|
|
84
190
|
export function parseTyped<P extends string>(prefix: P, s: string): Typed<P> {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return
|
|
191
|
+
const parsed = parse(s);
|
|
192
|
+
if (parsed.prefix !== prefix) {
|
|
193
|
+
throw new Error(`typeid '${s}' has prefix '${parsed.prefix}', want '${prefix}'`);
|
|
194
|
+
}
|
|
195
|
+
return s as Typed<P>;
|
|
90
196
|
}
|
|
91
197
|
|
|
92
198
|
// Crockford base32 alphabet used by the TypeID spec (lowercase),
|
package/src/mcp/bridge.test.ts
CHANGED
|
@@ -220,3 +220,31 @@ describe('commanderBridge', () => {
|
|
|
220
220
|
);
|
|
221
221
|
});
|
|
222
222
|
});
|
|
223
|
+
|
|
224
|
+
describe('classify: destructive-name inference', () => {
|
|
225
|
+
const leafOf = (name: string, ann?: Record<string, string>) => {
|
|
226
|
+
const root = new Command('root');
|
|
227
|
+
const c = new Command(name).description(name);
|
|
228
|
+
if (ann) annotate(c, ann);
|
|
229
|
+
root.addCommand(c);
|
|
230
|
+
return commanderBridge(root, { run }).leaves()[0]!;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
it('treats an unannotated destructive-named leaf as destructive and blocks it', async () => {
|
|
234
|
+
const root = new Command('root');
|
|
235
|
+
root.addCommand(new Command('delete').description('d'));
|
|
236
|
+
const bridge = commanderBridge(root, { run });
|
|
237
|
+
expect(bridge.leaves()[0]!.class?.destructive).toBe(true);
|
|
238
|
+
await expect(
|
|
239
|
+
bridge.invoke({ path: ['delete'], args: [], flags: {}, meta: { surface: SURFACE_MCP } } as never),
|
|
240
|
+
).rejects.toThrow(/destructive command blocked on this surface/);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('an explicit annotation wins, valid or malformed; empty infers; other names do not', () => {
|
|
244
|
+
expect(leafOf('delete', { 'kit/side-effect': 'read' }).class?.destructive).toBeUndefined();
|
|
245
|
+
expect(leafOf('delete', { 'kit/side-effect': 'bogus' }).class?.destructive).toBeUndefined();
|
|
246
|
+
expect(leafOf('delete', { 'kit/side-effect': '' }).class?.destructive).toBe(true);
|
|
247
|
+
expect(leafOf('delete-all').class?.destructive).toBeUndefined();
|
|
248
|
+
expect(leafOf('rm').class?.destructive).toBe(true);
|
|
249
|
+
});
|
|
250
|
+
});
|
package/src/mcp/bridge.ts
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
|
|
19
19
|
import type { Command, Option } from 'commander';
|
|
20
20
|
import { declaredArgs, takesNoValue } from '../toolspec-internal.js';
|
|
21
|
+
import { gateSideEffect, isDestructiveLike } from '../sideeffect.js';
|
|
21
22
|
import {
|
|
22
23
|
DestructiveBlockedError,
|
|
23
24
|
policyAllowed,
|
|
@@ -36,7 +37,6 @@ import {
|
|
|
36
37
|
} from './types.js';
|
|
37
38
|
|
|
38
39
|
/** Annotation keys read off a command, matching the kit/ vocabulary. */
|
|
39
|
-
const ANN_SIDE_EFFECT = 'kit/side-effect';
|
|
40
40
|
const ANN_AUTH_REQUIRED = 'kit/auth-required';
|
|
41
41
|
const ANN_REQUIRES_CONFIRM = 'kit/requires-confirmation';
|
|
42
42
|
const ANN_PERMISSIONS = 'kit/permissions';
|
|
@@ -134,15 +134,10 @@ export function commanderBridge(
|
|
|
134
134
|
export function classify(cmd: AnnotatedCommand): SafetyClass {
|
|
135
135
|
const ann = cmd.annotations ?? {};
|
|
136
136
|
const cls: SafetyClass = {};
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
cls.destructive = true;
|
|
142
|
-
break;
|
|
143
|
-
default:
|
|
144
|
-
break;
|
|
145
|
-
}
|
|
137
|
+
// A declared tier wins; an unannotated destructive-named leaf is the
|
|
138
|
+
// inferred destructive tier, as the CLI gate and Go's cmdreflect read it.
|
|
139
|
+
const tier = gateSideEffect(cmd.name(), ann);
|
|
140
|
+
if (tier !== undefined && isDestructiveLike(tier)) cls.destructive = true;
|
|
146
141
|
if (ann[ANN_AUTH_REQUIRED] === 'true') cls.authRequired = true;
|
|
147
142
|
if (ann[ANN_REQUIRES_CONFIRM] === 'true') cls.requiresConfirmation = true;
|
|
148
143
|
const perms = splitCSV(ann[ANN_PERMISSIONS]);
|
package/src/plan.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module plan
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* The structured dry-run output of a write or destructive command. Ports
|
|
6
|
+
* Go `cli.Plan` / `cli.RenderPlan`: a handler that sees `isDryRun(cmd)`
|
|
7
|
+
* builds a {@link Plan} of what it would change and renders it instead of
|
|
8
|
+
* acting.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import * as yaml from 'js-yaml';
|
|
12
|
+
|
|
13
|
+
import { render, TABLE_FORMAT } from './output.js';
|
|
14
|
+
|
|
15
|
+
/** One declared state change a {@link Plan} would apply. */
|
|
16
|
+
export interface Effect {
|
|
17
|
+
/** A short verb: `create`, `update`, `delete`. */
|
|
18
|
+
kind: string;
|
|
19
|
+
/** The addressable resource (`alias:foo`, `/etc/hosts`). */
|
|
20
|
+
target: string;
|
|
21
|
+
/** Whether the inverse command restores prior state losslessly. */
|
|
22
|
+
reversible: boolean;
|
|
23
|
+
/** A free-form one-line note. */
|
|
24
|
+
detail?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** What a command would do if re-issued without `--dry-run`. */
|
|
28
|
+
export interface Plan {
|
|
29
|
+
/** The full command path; filled in by {@link renderPlan} when empty. */
|
|
30
|
+
command?: string;
|
|
31
|
+
/** The resolved arguments. */
|
|
32
|
+
args?: Record<string, unknown>;
|
|
33
|
+
/** The ordered changes. */
|
|
34
|
+
effects?: Effect[];
|
|
35
|
+
/** Pre-flight checks that passed while planning. */
|
|
36
|
+
prerequisites_checked?: string[];
|
|
37
|
+
/** Non-fatal advisories for the operator. */
|
|
38
|
+
warnings?: string[];
|
|
39
|
+
/** RFC 3339 UTC time of assembly; stamped when empty. */
|
|
40
|
+
generated_at?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** `YYYY-MM-DDTHH:MM:SSZ` for now. */
|
|
44
|
+
function rfc3339Now(): string {
|
|
45
|
+
return new Date().toISOString().replace(/\.\d{3}Z$/, 'Z');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The plan as its wire object: Go struct key order, empty optionals off. */
|
|
49
|
+
function toWire(p: Plan, commandPath: string): Record<string, unknown> {
|
|
50
|
+
const w: Record<string, unknown> = { command: p.command || commandPath };
|
|
51
|
+
if (p.args && Object.keys(p.args).length > 0) w['args'] = p.args;
|
|
52
|
+
w['effects'] = (p.effects ?? []).map((e) => {
|
|
53
|
+
const o: Record<string, unknown> = { kind: e.kind, target: e.target, reversible: e.reversible };
|
|
54
|
+
if (e.detail) o['detail'] = e.detail;
|
|
55
|
+
return o;
|
|
56
|
+
});
|
|
57
|
+
if (p.prerequisites_checked && p.prerequisites_checked.length > 0) {
|
|
58
|
+
w['prerequisites_checked'] = p.prerequisites_checked;
|
|
59
|
+
}
|
|
60
|
+
if (p.warnings && p.warnings.length > 0) w['warnings'] = p.warnings;
|
|
61
|
+
w['generated_at'] = p.generated_at || rfc3339Now();
|
|
62
|
+
return w;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Writes `plan` as the command's dry-run output in `format`: `json` and
|
|
67
|
+
* `yaml` serialize it, anything else prints the plan table. An empty
|
|
68
|
+
* `command` defaults to `commandPath` and an empty `generated_at` to now.
|
|
69
|
+
*/
|
|
70
|
+
export function renderPlan(
|
|
71
|
+
w: { write(chunk: string): unknown },
|
|
72
|
+
format: string,
|
|
73
|
+
plan: Plan,
|
|
74
|
+
commandPath = '',
|
|
75
|
+
): void {
|
|
76
|
+
const wire = toWire(plan, commandPath);
|
|
77
|
+
if (format === 'json') {
|
|
78
|
+
w.write(`${JSON.stringify(wire, null, 2)}\n`);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (format === 'yaml') {
|
|
82
|
+
w.write(yaml.dump(wire));
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
w.write(`COMMAND ${wire['command'] as string}\n`);
|
|
86
|
+
w.write(`GENERATED ${wire['generated_at'] as string}\n`);
|
|
87
|
+
for (const warn of plan.warnings ?? []) w.write(`WARNING ${warn}\n`);
|
|
88
|
+
render(w as NodeJS.WritableStream, TABLE_FORMAT, wire['effects']);
|
|
89
|
+
}
|