@hop-top/kit 0.5.0-alpha.0 → 0.5.0-alpha.2
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 +98 -0
- package/README.md +237 -9
- package/dist/aim-CTp-kJW2.d.ts +139 -0
- package/dist/aim.d.ts +1 -116
- package/dist/alias-dhhr08Ei.d.ts +88 -0
- package/dist/alias.d.ts +2 -73
- package/dist/api-miiso81x.d.ts +77 -0
- package/dist/api.d.ts +1 -62
- package/dist/auth-2-jfgiHn.d.ts +34 -0
- package/dist/auth.d.ts +1 -26
- package/dist/cli-DgOEOsPB.d.ts +450 -0
- package/dist/cli.d.ts +5 -309
- package/dist/cli.js +250 -62
- package/dist/config-BUY7_W2n.d.ts +103 -0
- package/dist/config.d.ts +1 -97
- package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
- package/dist/errcorrect.d.ts +1 -37
- package/dist/id/index.d.ts +2 -83
- package/dist/index--C7Mtsmm.d.ts +328 -0
- package/dist/index-C3Zw4wDJ.d.ts +359 -0
- package/dist/index-MLrRDvEG.d.ts +1073 -0
- package/dist/index-QSffbLnj.d.ts +94 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +5863 -0
- package/dist/llm-B5XUWH8T.d.ts +231 -0
- package/dist/llm.d.ts +1 -188
- package/dist/mcp/index.d.ts +2 -0
- package/dist/mcp/index.js +1453 -0
- package/dist/netpolicy-asvCfwi8.d.ts +138 -0
- package/dist/netpolicy.d.ts +1 -0
- package/dist/netpolicy.js +118 -0
- package/dist/output-VFFLqor9.d.ts +521 -0
- package/dist/output.d.ts +2 -213
- package/dist/output.js +554 -43
- package/dist/progress-CAzyxeWl.d.ts +38 -0
- package/dist/progress.d.ts +1 -30
- package/dist/provenance-SMFHm5gx.d.ts +30 -0
- package/dist/provenance.d.ts +1 -24
- package/dist/routellm-PGSt2D1y.d.ts +75 -0
- package/dist/routellm.d.ts +2 -64
- package/dist/rpc-D5xLEf8V.d.ts +32 -0
- package/dist/rpc.d.ts +2 -25
- package/dist/safety-CUaYEjZc.d.ts +30 -0
- package/dist/safety.d.ts +1 -24
- package/dist/scope-BrhYB4ij.d.ts +141 -0
- package/dist/scope.d.ts +1 -121
- package/dist/stream-CrjeCp0M.d.ts +44 -0
- package/dist/stream.d.ts +1 -35
- package/dist/telemetry/index.d.ts +1 -315
- package/dist/telemetry/index.js +12 -1
- package/dist/tui/index.d.ts +4 -298
- package/dist/tui/index.js +61 -9
- package/dist/upgrade-p_zh3pvV.d.ts +102 -0
- package/dist/upgrade.d.ts +1 -95
- package/dist/xdg-DB7Shoib.d.ts +179 -0
- package/dist/xdg.d.ts +1 -169
- package/package.json +65 -25
- package/src/cli.offline.test.ts +93 -0
- package/src/cli.parity.test.ts +192 -0
- package/src/cli.ts +112 -12
- package/src/index.test.ts +58 -0
- package/src/index.ts +36 -0
- package/src/log.parity.test.ts +105 -0
- package/src/log.ts +64 -13
- package/src/mcp/bridge.test.ts +150 -0
- package/src/mcp/bridge.ts +246 -0
- package/src/mcp/conformance.test.ts +336 -0
- package/src/mcp/dispatch.ts +313 -0
- package/src/mcp/index.ts +138 -0
- package/src/mcp/legacy.ts +314 -0
- package/src/mcp/mcp.test.ts +697 -0
- package/src/mcp/modern-confirm.test.ts +520 -0
- package/src/mcp/modern-confirm.ts +452 -0
- package/src/mcp/modern.ts +723 -0
- package/src/mcp/tasks.ts +82 -0
- package/src/mcp/testtree.ts +250 -0
- package/src/mcp/types.ts +504 -0
- package/src/netpolicy.test.ts +202 -0
- package/src/netpolicy.ts +231 -0
- package/src/output/dispatch.ts +10 -2
- package/src/output/error.ts +293 -0
- package/src/output/formatter.ts +29 -5
- package/src/output/formatters/csv.ts +48 -12
- package/src/output/formatters/json.ts +4 -25
- package/src/output/formatters/project.ts +34 -0
- package/src/output/formatters/table.ts +8 -6
- package/src/output/formatters/text.ts +7 -3
- package/src/output/formatters/yaml.ts +5 -22
- package/src/output/projection.test.ts +298 -0
- package/src/output/projection.ts +33 -59
- package/src/output/template.ts +11 -21
- package/src/output.surface.test.ts +41 -0
- package/src/output.ts +39 -3
- package/src/telemetry/client.test.ts +31 -0
- package/src/telemetry/client.ts +7 -1
- package/src/telemetry/offline.test.ts +64 -0
- package/src/tui/parity.test.ts +142 -0
- package/src/tui/parity.ts +59 -13
- package/test/output/csv.test.ts +164 -0
- package/test/output/error.test.ts +241 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load-bearing coverage for the verbosity/streams wiring in `cli.ts`.
|
|
3
|
+
*
|
|
4
|
+
* These feed a *constructed* ParityData (never the shared parity.json) whose
|
|
5
|
+
* values differ from the shipped contract, and assert the flag names, label
|
|
6
|
+
* and destination follow it. A port that re-hardcodes `-V`, `--stream`,
|
|
7
|
+
* `[name] ` or `process.stderr` fails here.
|
|
8
|
+
*
|
|
9
|
+
* Mirrors `go/console/cli/verbose_parity_wiring_test.go` and
|
|
10
|
+
* `go/console/cli/streams_parity_wiring_test.go`.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { Command } from 'commander';
|
|
14
|
+
import { describe, expect, it } from 'vitest';
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
channel,
|
|
18
|
+
createCLI,
|
|
19
|
+
registerStream,
|
|
20
|
+
streamFlagName,
|
|
21
|
+
streamLabel,
|
|
22
|
+
streamOutput,
|
|
23
|
+
verbosityFlagSpec,
|
|
24
|
+
verbosityShorthand,
|
|
25
|
+
} from './cli';
|
|
26
|
+
import { parity, type ParityData } from './tui/parity';
|
|
27
|
+
|
|
28
|
+
/** A ParityData carrying only the given streams block. */
|
|
29
|
+
function streamsData(flag: string, labelFormat: string, output: string): ParityData {
|
|
30
|
+
return { streams: { flag, label_format: labelFormat, output } } as unknown as ParityData;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A ParityData carrying only the given verbosity flag. */
|
|
34
|
+
function verbosityData(flag: string): ParityData {
|
|
35
|
+
return { verbosity: { flag, levels: {}, quiet_override: 'warn' } } as unknown as ParityData;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Name a resolved destination. `process.stdout` and `process.stderr` render
|
|
40
|
+
* identically in vitest diffs, so assert on the name instead — a failure
|
|
41
|
+
* then reads "expected 'stderr' to be 'stdout'".
|
|
42
|
+
*/
|
|
43
|
+
function destName(w: NodeJS.WritableStream): string {
|
|
44
|
+
if (w === process.stdout) return 'stdout';
|
|
45
|
+
if (w === process.stderr) return 'stderr';
|
|
46
|
+
return 'other';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Capture what a body writes to stdout and stderr. */
|
|
50
|
+
function captureStreams(fn: () => void): { out: string; err: string } {
|
|
51
|
+
const outChunks: string[] = [];
|
|
52
|
+
const errChunks: string[] = [];
|
|
53
|
+
const oo = process.stdout.write;
|
|
54
|
+
const oe = process.stderr.write;
|
|
55
|
+
process.stdout.write = ((c: string) => { outChunks.push(c); return true; }) as never;
|
|
56
|
+
process.stderr.write = ((c: string) => { errChunks.push(c); return true; }) as never;
|
|
57
|
+
try {
|
|
58
|
+
fn();
|
|
59
|
+
} finally {
|
|
60
|
+
process.stdout.write = oo;
|
|
61
|
+
process.stderr.write = oe;
|
|
62
|
+
}
|
|
63
|
+
return { out: outChunks.join(''), err: errChunks.join('') };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ─── Load-bearing: streams ───────────────────────────────────────────────────
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Swaps the label template, destination and flag name away from the shipped
|
|
70
|
+
* `[{name}]` / stderr / `--stream`. Hardcoded literals fail here.
|
|
71
|
+
*/
|
|
72
|
+
describe('streams follow contract not literals', () => {
|
|
73
|
+
const d = streamsData('--channel', '<<{name}>>', 'stdout');
|
|
74
|
+
|
|
75
|
+
it('renders the contract label_format', () => {
|
|
76
|
+
expect(streamLabel(d, 'trace'), 'label must render label_format, not a hardcoded [name]')
|
|
77
|
+
.toBe('<<trace>> ');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('resolves the contract output destination', () => {
|
|
81
|
+
expect(destName(streamOutput(d)),
|
|
82
|
+
'destination must follow streams.output, not a hardcoded stderr').toBe('stdout');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('resolves the contract flag name', () => {
|
|
86
|
+
expect(streamFlagName(d), 'flag name must follow streams.flag, not a hardcoded "stream"')
|
|
87
|
+
.toBe('channel');
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// ─── Load-bearing: verbosity flag ────────────────────────────────────────────
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Swaps the shorthand away from the shipped `-V`. A hardcoded `'-V, --verbose'`
|
|
95
|
+
* option spec fails here.
|
|
96
|
+
*/
|
|
97
|
+
describe('verbosity flag follows contract not literals', () => {
|
|
98
|
+
it('takes the shorthand from the contract', () => {
|
|
99
|
+
expect(verbosityShorthand(verbosityData('-d')),
|
|
100
|
+
'shorthand must follow verbosity.flag, not a hardcoded V').toBe('d');
|
|
101
|
+
expect(verbosityFlagSpec(verbosityData('-d')),
|
|
102
|
+
'option spec must be built from the contract shorthand').toBe('-d, --verbose');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('registers a diverging shorthand on a real commander program', () => {
|
|
106
|
+
// Commander takes the option spec as a runtime string, so the shorthand
|
|
107
|
+
// need not be a compile-time literal. This proves the wiring would work
|
|
108
|
+
// if the contract changed.
|
|
109
|
+
const p = new Command('t');
|
|
110
|
+
p.option(verbosityFlagSpec(verbosityData('-d')), 'Increase verbosity',
|
|
111
|
+
(_: string, prev: number) => prev + 1, 0);
|
|
112
|
+
p.command('run').action(() => {});
|
|
113
|
+
p.parse(['node', 't', 'run', '-dd']);
|
|
114
|
+
expect(p.opts().verbose).toBe(2);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// ─── Requirement pins: observable behavior unchanged ─────────────────────────
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Pins the wiring to the values actually declared in parity.json. NOT
|
|
122
|
+
* load-bearing: it keeps passing if a literal is restored, by design.
|
|
123
|
+
*/
|
|
124
|
+
describe('streams match shipped contract', () => {
|
|
125
|
+
it('pins the contract values', () => {
|
|
126
|
+
expect(parity.streams.label_format).toBe('[{name}]');
|
|
127
|
+
expect(parity.streams.output).toBe('stderr');
|
|
128
|
+
expect(parity.streams.flag).toBe('--stream');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('renders the historical [name] prefix to stderr', () => {
|
|
132
|
+
expect(streamLabel(parity, 'trace')).toBe('[trace] ');
|
|
133
|
+
expect(destName(streamOutput(parity))).toBe('stderr');
|
|
134
|
+
expect(streamFlagName(parity)).toBe('stream');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('registers --stream and prefixes enabled channels end-to-end', () => {
|
|
138
|
+
const { program } = createCLI({ name: 't', version: '1.0.0', description: 'd' });
|
|
139
|
+
const sub = program.command('run');
|
|
140
|
+
registerStream(sub, 'sql', 'SQL queries');
|
|
141
|
+
expect(sub.options.map((o) => o.flags)).toContain('--stream <names>');
|
|
142
|
+
|
|
143
|
+
program.parse(['node', 't', 'run', '--stream', 'sql']);
|
|
144
|
+
const { out, err } = captureStreams(() => {
|
|
145
|
+
channel(sub, 'sql').write('SELECT 1\n');
|
|
146
|
+
channel(sub, 'off').write('hidden\n');
|
|
147
|
+
});
|
|
148
|
+
expect(err).toBe('[sql] SELECT 1\n');
|
|
149
|
+
expect(out).toBe('');
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('verbosity flag matches shipped contract', () => {
|
|
154
|
+
it('pins the contract flag', () => {
|
|
155
|
+
expect(parity.verbosity.flag).toBe('-V');
|
|
156
|
+
expect(verbosityShorthand(parity)).toBe('V');
|
|
157
|
+
expect(verbosityFlagSpec(parity)).toBe('-V, --verbose');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('registers -V as a stacking count on the real CLI', () => {
|
|
161
|
+
const { program } = createCLI({ name: 't', version: '1.0.0', description: 'd' });
|
|
162
|
+
program.command('run').action(() => {});
|
|
163
|
+
program.parse(['node', 't', 'run', '-VVV']);
|
|
164
|
+
expect(program.opts().verbose).toBe(3);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// ─── Degradation ─────────────────────────────────────────────────────────────
|
|
169
|
+
|
|
170
|
+
describe('streams degrade safely', () => {
|
|
171
|
+
it('falls back on an empty contract', () => {
|
|
172
|
+
const empty = streamsData('', '', '');
|
|
173
|
+
expect(streamLabel(empty, 'trace'), 'absent label_format falls back to [name]')
|
|
174
|
+
.toBe('[trace] ');
|
|
175
|
+
expect(destName(streamOutput(empty)), 'absent output falls back to stderr').toBe('stderr');
|
|
176
|
+
expect(streamFlagName(empty), 'absent flag falls back to "stream"').toBe('stream');
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('renders a label_format without {name} verbatim', () => {
|
|
180
|
+
const noPlaceholder = streamsData('--s', 'LOG:', 'stderr');
|
|
181
|
+
expect(streamLabel(noPlaceholder, 'trace')).toBe('LOG: ');
|
|
182
|
+
expect(streamFlagName(noPlaceholder)).toBe('s');
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe('verbosity flag degrades safely', () => {
|
|
187
|
+
it('falls back when the contract flag is absent or not a shorthand', () => {
|
|
188
|
+
expect(verbosityShorthand(verbosityData('')), 'absent flag falls back to V').toBe('V');
|
|
189
|
+
expect(verbosityShorthand(verbosityData('--verbose')),
|
|
190
|
+
'a multi-character flag is not a commander shorthand; fall back to V').toBe('V');
|
|
191
|
+
});
|
|
192
|
+
});
|
package/src/cli.ts
CHANGED
|
@@ -26,9 +26,12 @@
|
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
28
|
import { Command, Help } from 'commander';
|
|
29
|
-
import { parity } from './tui/parity.js';
|
|
29
|
+
import { parity, type ParityData } from './tui/parity.js';
|
|
30
30
|
import { registerOutputFlags } from './output/flags.js';
|
|
31
|
+
import { install as installNetPolicy, setProcessOffline } from './netpolicy.js';
|
|
31
32
|
import './output/builtins.js';
|
|
33
|
+
export { ErrOffline, OfflineError, guardFetch, isOffline, isOfflineError,
|
|
34
|
+
withOffline } from './netpolicy.js';
|
|
32
35
|
export { HintSet, hintsEnabled, renderHints, active,
|
|
33
36
|
registerUpgradeHints, registerVersionHints } from './hint.js';
|
|
34
37
|
export type { Hint, HintOptions } from './hint.js';
|
|
@@ -39,6 +42,29 @@ export type { Hint, HintOptions } from './hint.js';
|
|
|
39
42
|
|
|
40
43
|
const verboseCountMap = new WeakMap<Command, number>();
|
|
41
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Return the single-character shorthand declared by the parity contract's
|
|
47
|
+
* `verbosity.flag`, with leading dashes stripped. A contract value that is
|
|
48
|
+
* not a single character after stripping falls back to `V`.
|
|
49
|
+
*
|
|
50
|
+
* Taking the contract as a parameter (rather than reading the module-level
|
|
51
|
+
* `parity`) keeps the mapping testable against a constructed `ParityData`
|
|
52
|
+
* without mutating the shared `parity.json`.
|
|
53
|
+
*/
|
|
54
|
+
export function verbosityShorthand(d: ParityData): string {
|
|
55
|
+
const s = (d.verbosity?.flag ?? '').replace(/^-+/, '');
|
|
56
|
+
return s.length === 1 ? s : 'V';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Render the `-V` option spec commander registers, e.g. `-V, --verbose`.
|
|
61
|
+
* The shorthand comes from the parity contract; the long name is
|
|
62
|
+
* kit-local (the contract declares only the shorthand).
|
|
63
|
+
*/
|
|
64
|
+
export function verbosityFlagSpec(d: ParityData): string {
|
|
65
|
+
return `-${verbosityShorthand(d)}, --verbose`;
|
|
66
|
+
}
|
|
67
|
+
|
|
42
68
|
/** Read the accumulated -V count for a parsed command tree. */
|
|
43
69
|
export function verboseCount(cmd: Command): number {
|
|
44
70
|
// Walk up to root to find the stored count.
|
|
@@ -62,9 +88,43 @@ interface StreamDef {
|
|
|
62
88
|
|
|
63
89
|
const streamDefsMap = new WeakMap<Command, StreamDef[]>();
|
|
64
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Return the long flag name declared by the parity contract's
|
|
93
|
+
* `streams.flag`, with leading dashes stripped (commander's parsed option
|
|
94
|
+
* key is the undashed camelCase name). Falls back to `stream`.
|
|
95
|
+
*
|
|
96
|
+
* Taking the contract as a parameter (rather than reading the module-level
|
|
97
|
+
* `parity`) keeps the mapping testable against a constructed `ParityData`
|
|
98
|
+
* without mutating the shared `parity.json`.
|
|
99
|
+
*/
|
|
100
|
+
export function streamFlagName(d: ParityData): string {
|
|
101
|
+
const name = (d.streams?.flag ?? '').replace(/^-+/, '');
|
|
102
|
+
return name === '' ? 'stream' : name;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Render a stream's prefix from the parity contract's
|
|
107
|
+
* `streams.label_format` template. The template's `{name}` placeholder is
|
|
108
|
+
* substituted with the stream name; a trailing space separates the label
|
|
109
|
+
* from the payload.
|
|
110
|
+
*/
|
|
111
|
+
export function streamLabel(d: ParityData, name: string): string {
|
|
112
|
+
const format = d.streams?.label_format || '[{name}]';
|
|
113
|
+
return format.split('{name}').join(name) + ' ';
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Resolve the parity contract's `streams.output` destination to a stream.
|
|
118
|
+
* Anything other than `stdout` resolves to stderr, which is the contract's
|
|
119
|
+
* declared destination.
|
|
120
|
+
*/
|
|
121
|
+
export function streamOutput(d: ParityData): NodeJS.WritableStream {
|
|
122
|
+
return d.streams?.output === 'stdout' ? process.stdout : process.stderr;
|
|
123
|
+
}
|
|
124
|
+
|
|
65
125
|
/**
|
|
66
126
|
* Register a named diagnostic stream on a command.
|
|
67
|
-
* Adds
|
|
127
|
+
* Adds the parity contract's stream option (comma-separated) on first call.
|
|
68
128
|
*
|
|
69
129
|
* Parity note: Go uses StringSlice (repeatable --stream); TS and Python
|
|
70
130
|
* use a single comma-separated string value.
|
|
@@ -76,9 +136,9 @@ export function registerStream(
|
|
|
76
136
|
if (!defs) {
|
|
77
137
|
defs = [];
|
|
78
138
|
streamDefsMap.set(cmd, defs);
|
|
79
|
-
// Add
|
|
139
|
+
// Add the contract's stream option on first registration.
|
|
80
140
|
cmd.option(
|
|
81
|
-
|
|
141
|
+
`--${streamFlagName(parity)} <names>`,
|
|
82
142
|
'Enable diagnostic streams (comma-separated)',
|
|
83
143
|
);
|
|
84
144
|
}
|
|
@@ -93,24 +153,37 @@ export function getStreamDefs(cmd: Command): StreamDef[] {
|
|
|
93
153
|
const noopWriter = { write(_s: string): boolean { return true; } };
|
|
94
154
|
|
|
95
155
|
/**
|
|
96
|
-
*
|
|
97
|
-
* `--
|
|
98
|
-
*
|
|
156
|
+
* Commander stores a parsed option under the camelCased form of its long
|
|
157
|
+
* name (`--log-level` → `opts.logLevel`), so the contract's flag name must
|
|
158
|
+
* be camelCased before it is read back off `cmd.opts()`.
|
|
159
|
+
*/
|
|
160
|
+
function optionKey(flagName: string): string {
|
|
161
|
+
return flagName.replace(/-([a-z])/g, (_, c: string) => c.toUpperCase());
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Return a writer for a named stream. When the stream is enabled via the
|
|
166
|
+
* parity contract's stream flag, writes go to the contract's
|
|
167
|
+
* `streams.output` destination, prefixed with its `streams.label_format`
|
|
168
|
+
* label. Otherwise writes are discarded.
|
|
99
169
|
*/
|
|
100
170
|
export function channel(
|
|
101
171
|
cmd: Command, name: string,
|
|
102
172
|
): { write(s: string): boolean } {
|
|
103
173
|
const opts = cmd.opts();
|
|
104
|
-
const
|
|
105
|
-
|
|
174
|
+
const raw = opts[optionKey(streamFlagName(parity))];
|
|
175
|
+
const enabled = typeof raw === 'string'
|
|
176
|
+
? raw.split(',').map((s: string) => s.trim()).includes(name)
|
|
106
177
|
: false;
|
|
107
178
|
if (!enabled) return noopWriter;
|
|
179
|
+
const prefix = streamLabel(parity, name);
|
|
180
|
+
const w = streamOutput(parity);
|
|
108
181
|
return {
|
|
109
182
|
write(s: string): boolean {
|
|
110
183
|
// Split on newlines and prefix each line (parity with Go/Python).
|
|
111
184
|
const lines = s.split('\n');
|
|
112
185
|
for (const line of lines) {
|
|
113
|
-
if (line)
|
|
186
|
+
if (line) w.write(`${prefix}${line}\n`);
|
|
114
187
|
}
|
|
115
188
|
return true;
|
|
116
189
|
},
|
|
@@ -238,6 +311,7 @@ export interface CLIConfig {
|
|
|
238
311
|
quiet?: boolean;
|
|
239
312
|
noColor?: boolean;
|
|
240
313
|
hints?: boolean;
|
|
314
|
+
offline?: boolean;
|
|
241
315
|
};
|
|
242
316
|
|
|
243
317
|
/** Command groups for partitioned help (e.g. COMMANDS vs MANAGEMENT). */
|
|
@@ -663,9 +737,35 @@ export function createCLI(cfg: CLIConfig): CLIResult {
|
|
|
663
737
|
program.option('--no-hints', 'Suppress next-step hints after command output', false);
|
|
664
738
|
}
|
|
665
739
|
|
|
666
|
-
//
|
|
740
|
+
// --offline (cli-parity-guide, "Global Flags") disables all network
|
|
741
|
+
// access. It is the highest-precedence override: per-command network
|
|
742
|
+
// opt-ins (peer discovery, sync replication, repo creation, initial
|
|
743
|
+
// push, upgrade checks) must behave as if their corresponding opt-out
|
|
744
|
+
// flag had been passed. The override only forces opt-outs ON — it never
|
|
745
|
+
// un-sets an explicitly passed --no-* flag, which is why it stamps a
|
|
746
|
+
// marker rather than rewriting parsed options.
|
|
747
|
+
//
|
|
748
|
+
// Registering the flag alone would be advisory: a leaf that never
|
|
749
|
+
// consults the marker would still reach the wire. installNetPolicy
|
|
750
|
+
// guards globalThis.fetch so the refusal happens beneath naive callers.
|
|
751
|
+
if (!cfg.disable?.offline) {
|
|
752
|
+
program.option('--offline', 'Disable network access', false);
|
|
753
|
+
installNetPolicy();
|
|
754
|
+
// preSubcommand fires before a subcommand's action handler, and the
|
|
755
|
+
// root's own action for a leaf-less program; hook both so the marker
|
|
756
|
+
// is set whichever shape the adopter uses.
|
|
757
|
+
const stamp = (thisCmd: Command) => {
|
|
758
|
+
setProcessOffline(thisCmd.opts()['offline'] === true);
|
|
759
|
+
};
|
|
760
|
+
program.hook('preSubcommand', (thisCmd) => stamp(thisCmd));
|
|
761
|
+
program.hook('preAction', (thisCmd) => stamp(thisCmd));
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// Stackable verbosity count flag (-VV = 2). The shorthand comes from the
|
|
765
|
+
// parity contract's verbosity block; commander takes the option spec as a
|
|
766
|
+
// runtime string, so it need not be a literal.
|
|
667
767
|
program.option(
|
|
668
|
-
|
|
768
|
+
verbosityFlagSpec(parity), 'Increase verbosity',
|
|
669
769
|
(_: string, prev: number) => prev + 1, 0,
|
|
670
770
|
);
|
|
671
771
|
// Store count after parse; quiet overrides verbose.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
|
|
5
|
+
import * as kit from './index.js';
|
|
6
|
+
|
|
7
|
+
const pkgDir = join(__dirname, '..');
|
|
8
|
+
const pkg = JSON.parse(readFileSync(join(pkgDir, 'package.json'), 'utf8')) as {
|
|
9
|
+
exports: Record<string, Record<string, string>>;
|
|
10
|
+
scripts: Record<string, string>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** Map a dist target like './dist/foo/index.js' to its source entry 'src/foo/index.ts'. */
|
|
14
|
+
function srcEntryFor(distTarget: string): string {
|
|
15
|
+
return distTarget.replace(/^\.\/dist\//, 'src/').replace(/\.js$/, '.ts');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('exports map contract', () => {
|
|
19
|
+
const subpaths = Object.entries(pkg.exports);
|
|
20
|
+
|
|
21
|
+
it('declares at least the root and one subpath', () => {
|
|
22
|
+
expect(subpaths.length).toBeGreaterThan(1);
|
|
23
|
+
expect(pkg.exports['.']).toBeDefined();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it.each(subpaths)('%s: every condition target is emitted by the build', (subpath, conditions) => {
|
|
27
|
+
const buildScript = pkg.scripts.build;
|
|
28
|
+
for (const [condition, target] of Object.entries(conditions)) {
|
|
29
|
+
// All runtime conditions must point into dist/.
|
|
30
|
+
expect(target, `${subpath} ${condition}`).toMatch(/^\.\/dist\//);
|
|
31
|
+
const entry = srcEntryFor(condition === 'types' ? target.replace(/\.d\.ts$/, '.js') : target);
|
|
32
|
+
// The source entry must exist on disk...
|
|
33
|
+
expect(existsSync(join(pkgDir, entry)), `${subpath} ${condition}: missing ${entry}`).toBe(true);
|
|
34
|
+
// ...and be listed as a tsup build entry, or the dist target dangles.
|
|
35
|
+
expect(buildScript, `${subpath} ${condition}: ${entry} not in build entries`).toContain(entry);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('root "." exposes the same condition set as the other entries', () => {
|
|
40
|
+
expect(Object.keys(pkg.exports['.']).sort()).toEqual(['default', 'require', 'types']);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('root barrel', () => {
|
|
45
|
+
it('re-exports every public subpath except sqlstore as a namespace', () => {
|
|
46
|
+
const expected = Object.keys(pkg.exports)
|
|
47
|
+
.filter((p) => p !== '.' && p !== './sqlstore')
|
|
48
|
+
.map((p) => p.replace(/^\.\//, ''));
|
|
49
|
+
for (const name of expected) {
|
|
50
|
+
expect(kit, name).toHaveProperty(name);
|
|
51
|
+
expect(typeof (kit as Record<string, unknown>)[name], name).toBe('object');
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('does not eagerly expose sqlstore (native deps stay behind the subpath)', () => {
|
|
56
|
+
expect(kit).not.toHaveProperty('sqlstore');
|
|
57
|
+
});
|
|
58
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module index
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Root barrel: namespace re-exports of every public subpath, so
|
|
6
|
+
* `require('@hop-top/kit')` / `import ... from '@hop-top/kit'` resolve.
|
|
7
|
+
* Namespaces mirror the package exports map one-to-one.
|
|
8
|
+
*
|
|
9
|
+
* `sqlstore` is intentionally NOT re-exported here: an eager re-export
|
|
10
|
+
* would load better-sqlite3 native bindings on every bare import of the
|
|
11
|
+
* package. Use the dedicated subpath instead: `@hop-top/kit/sqlstore`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export * as aim from './aim.js';
|
|
15
|
+
export * as alias from './alias.js';
|
|
16
|
+
export * as api from './api.js';
|
|
17
|
+
export * as auth from './auth.js';
|
|
18
|
+
export * as cli from './cli.js';
|
|
19
|
+
export * as config from './config.js';
|
|
20
|
+
export * as errcorrect from './errcorrect.js';
|
|
21
|
+
export * as id from './id/index.js';
|
|
22
|
+
export * as llm from './llm.js';
|
|
23
|
+
export * as mcp from './mcp/index.js';
|
|
24
|
+
export * as netpolicy from './netpolicy.js';
|
|
25
|
+
export * as output from './output.js';
|
|
26
|
+
export * as progress from './progress.js';
|
|
27
|
+
export * as provenance from './provenance.js';
|
|
28
|
+
export * as routellm from './routellm.js';
|
|
29
|
+
export * as rpc from './rpc.js';
|
|
30
|
+
export * as safety from './safety.js';
|
|
31
|
+
export * as scope from './scope.js';
|
|
32
|
+
export * as stream from './stream.js';
|
|
33
|
+
export * as telemetry from './telemetry/index.js';
|
|
34
|
+
export * as tui from './tui/index.js';
|
|
35
|
+
export * as upgrade from './upgrade.js';
|
|
36
|
+
export * as xdg from './xdg.js';
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load-bearing coverage for the verbosity wiring in `log.ts`.
|
|
3
|
+
*
|
|
4
|
+
* These feed a *constructed* ParityData (never the shared parity.json) whose
|
|
5
|
+
* values differ from the shipped contract, and assert the level mapping
|
|
6
|
+
* follows it. A port that re-hardcodes info/debug/trace/warn fails here.
|
|
7
|
+
*
|
|
8
|
+
* Mirrors `go/console/log/parity_wiring_test.go`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, expect, it } from 'vitest';
|
|
12
|
+
|
|
13
|
+
import { quietLevel, verbosityLevel } from './log';
|
|
14
|
+
import { parity, type ParityData } from './tui/parity';
|
|
15
|
+
|
|
16
|
+
/** A ParityData carrying only the given verbosity block. */
|
|
17
|
+
function contractData(
|
|
18
|
+
levels: Record<string, string>,
|
|
19
|
+
quiet: string,
|
|
20
|
+
): ParityData {
|
|
21
|
+
return { verbosity: { flag: '-V', levels, quiet_override: quiet } } as unknown as ParityData;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// ─── Load-bearing: behavior must follow the contract, not literals ───────────
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Swaps the level names away from the shipped info/debug/trace. If
|
|
28
|
+
* `withVerbose`'s mapping is hardcoded, the resolved levels stay at the
|
|
29
|
+
* shipped values and this fails.
|
|
30
|
+
*/
|
|
31
|
+
describe('verbosityLevel follows contract not literals', () => {
|
|
32
|
+
const d = contractData({ '0': 'error', '1': 'warn', '2': 'info' }, 'fatal');
|
|
33
|
+
|
|
34
|
+
it('resolves each count through the contract levels table', () => {
|
|
35
|
+
expect(verbosityLevel(d, 0), 'count 0 must resolve via levels["0"], not a hardcoded info')
|
|
36
|
+
.toBe('error');
|
|
37
|
+
expect(verbosityLevel(d, 1), 'count 1 must resolve via levels["1"], not a hardcoded debug')
|
|
38
|
+
.toBe('warn');
|
|
39
|
+
expect(verbosityLevel(d, 2), 'count 2 must resolve via levels["2"], not a hardcoded trace')
|
|
40
|
+
.toBe('info');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('clamps counts above the highest declared key', () => {
|
|
44
|
+
expect(verbosityLevel(d, 7)).toBe('info');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('resolves quiet through the contract override', () => {
|
|
48
|
+
expect(quietLevel(d), 'quiet must resolve via quiet_override, not a hardcoded warn')
|
|
49
|
+
.toBe('fatal');
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Proves the mapping is table-driven rather than a three-branch if/else: a
|
|
55
|
+
* contract declaring a 4th level must be honored.
|
|
56
|
+
*/
|
|
57
|
+
describe('verbosityLevel honors a contract beyond the shipped 0/1/2', () => {
|
|
58
|
+
const d = contractData({ '0': 'warn', '3': 'trace' }, 'error');
|
|
59
|
+
|
|
60
|
+
it('falls back to the nearest lower declared key', () => {
|
|
61
|
+
expect(verbosityLevel(d, 0)).toBe('warn');
|
|
62
|
+
expect(verbosityLevel(d, 2)).toBe('warn');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('honors a key the shipped contract does not declare', () => {
|
|
66
|
+
expect(verbosityLevel(d, 3)).toBe('trace');
|
|
67
|
+
expect(quietLevel(d)).toBe('error');
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// ─── Requirement pin: observable behavior unchanged ──────────────────────────
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Pins the wiring to the values actually declared in parity.json, so the
|
|
75
|
+
* refactor cannot silently change observable behavior. NOT load-bearing:
|
|
76
|
+
* it keeps passing if a literal is restored, by design.
|
|
77
|
+
*/
|
|
78
|
+
describe('verbosityLevel matches shipped contract', () => {
|
|
79
|
+
it('resolves the historical -V level ladder', () => {
|
|
80
|
+
expect(parity.verbosity.levels).toEqual({ '0': 'info', '1': 'debug', '2': 'trace' });
|
|
81
|
+
expect(parity.verbosity.quiet_override).toBe('warn');
|
|
82
|
+
|
|
83
|
+
expect(verbosityLevel(parity, 0)).toBe('info');
|
|
84
|
+
expect(verbosityLevel(parity, 1)).toBe('debug');
|
|
85
|
+
expect(verbosityLevel(parity, 2)).toBe('trace');
|
|
86
|
+
expect(verbosityLevel(parity, 3)).toBe('trace');
|
|
87
|
+
expect(quietLevel(parity)).toBe('warn');
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// ─── Degradation ─────────────────────────────────────────────────────────────
|
|
92
|
+
|
|
93
|
+
describe('verbosityLevel degrades safely', () => {
|
|
94
|
+
it('falls back on an empty contract', () => {
|
|
95
|
+
const empty = contractData({}, '');
|
|
96
|
+
expect(verbosityLevel(empty, 0)).toBe('info');
|
|
97
|
+
expect(quietLevel(empty), 'absent quiet_override falls back to warn').toBe('warn');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('falls back on unrecognized level names', () => {
|
|
101
|
+
const bogus = contractData({ '0': 'nonsense', x: 'debug' }, 'nonsense');
|
|
102
|
+
expect(verbosityLevel(bogus, 0), 'unrecognized level name falls back to info').toBe('info');
|
|
103
|
+
expect(quietLevel(bogus)).toBe('warn');
|
|
104
|
+
});
|
|
105
|
+
});
|