@knolo/cli 6.2.6 → 6.2.8
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/README.md +7 -4
- package/bin/exchange.mjs +22 -5
- package/bin/knolo.mjs +10 -7
- package/bin/registry/commands.mjs +234 -33
- package/bin/research.mjs +275 -7
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -19,9 +19,9 @@ reader.
|
|
|
19
19
|
|
|
20
20
|
`knolo compose`, `knolo attest`, and `knolo verify-attestation` cite local
|
|
21
21
|
Knowledge Images and write an attestation sidecar. This tree's `@knolo/cli`
|
|
22
|
-
package file is `6.2.
|
|
23
|
-
`@knolo/exchange` `^0.1.0`. npm `latest` is `6.2.
|
|
24
|
-
package
|
|
22
|
+
package file is `6.2.7` and depends on `@knolo/core` `^6.2.3` and
|
|
23
|
+
`@knolo/exchange` `^0.1.0`. npm `latest` is `6.2.7`. `knolo kite` is in this
|
|
24
|
+
package and in that release. It is not in published `@knolo/cli@6.2.5`. `knolo exchange` hands
|
|
25
25
|
a verified V5 image to another agent. Shipped configs keep `adopt: deny`.
|
|
26
26
|
Discover reads a public advertisement and does not download a pack. They do
|
|
27
27
|
not fetch Hub for that handoff. Verification
|
|
@@ -55,6 +55,9 @@ the host-supplied caller. An optional Cortex trace is verified against
|
|
|
55
55
|
record and does not copy the cited image. Pass `--graph` when that trace
|
|
56
56
|
records KAR evaluations. The checked graph is stored in the image. The
|
|
57
57
|
command does not call a model and does not download an image.
|
|
58
|
+
`knolo research verify <image>` re-checks those stored records.
|
|
59
|
+
`--cited` supplies the cited bytes. Without them, digest mode says the
|
|
60
|
+
bytes were not supplied. `--require-material` fails when they are missing.
|
|
58
61
|
`examples/skills/research-and-cite` and `examples/skills/share-findings`
|
|
59
62
|
install with `knolo skill install`. See
|
|
60
63
|
[Research packs](../../docs/guides/research-packs.md).
|
|
@@ -387,7 +390,7 @@ This ensures:
|
|
|
387
390
|
## 🗺 Roadmap
|
|
388
391
|
|
|
389
392
|
The active roadmap is maintained in [`../../docs/ROADMAP.md`](../../docs/ROADMAP.md).
|
|
390
|
-
The published npm release is `6.2.
|
|
393
|
+
The published npm release is `6.2.7`. This tree's package file is `6.2.7` and adds `knolo kite`. `@knolo/cli@6.2.5` does not include that command. The published release preserves the V4 CLI, keeps V5 verification,
|
|
391
394
|
migration, diagnostics, and read-only Studio management, and adds offline
|
|
392
395
|
composition, attestation, and `knolo cortex`. Cortex verification does not
|
|
393
396
|
mean an answer is true.
|
package/bin/exchange.mjs
CHANGED
|
@@ -4,17 +4,32 @@ import {
|
|
|
4
4
|
importExchangeDirectory,
|
|
5
5
|
inspectExchangeReceipt,
|
|
6
6
|
loadExchangeConfig,
|
|
7
|
+
loadRelayConfig,
|
|
7
8
|
publishExchangeOffer,
|
|
8
9
|
pullExchangeOffer,
|
|
10
|
+
startExchangeRelay,
|
|
9
11
|
startExchangeServer,
|
|
10
12
|
} from '@knolo/exchange';
|
|
11
13
|
import { registryApiUrl, resolveRegistryUrl } from './registry/config.mjs';
|
|
12
14
|
|
|
13
15
|
export async function runExchangeCli(argv) {
|
|
14
16
|
const { command, flags, positional } = parse(argv);
|
|
17
|
+
if (command === 'relay') {
|
|
18
|
+
if (!flags.config)
|
|
19
|
+
throw new Error('Usage: knolo exchange relay --config <relay.json>');
|
|
20
|
+
const relayConfig = loadRelayConfig(flags.config);
|
|
21
|
+
const server = await startExchangeRelay(relayConfig);
|
|
22
|
+
console.log(server.url);
|
|
23
|
+
await new Promise((resolve) => {
|
|
24
|
+
process.once('SIGINT', resolve);
|
|
25
|
+
process.once('SIGTERM', resolve);
|
|
26
|
+
});
|
|
27
|
+
await server.close();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
15
30
|
if (!flags.config && command !== 'inspect' && command !== 'discover')
|
|
16
31
|
throw new Error(
|
|
17
|
-
'Usage: knolo exchange <serve|offer|pull|export|import|adopt|discover|inspect> --config <exchange.json>'
|
|
32
|
+
'Usage: knolo exchange <serve|offer|pull|export|import|adopt|discover|inspect|relay> --config <exchange.json>'
|
|
18
33
|
);
|
|
19
34
|
const config = flags.config ? loadExchangeConfig(flags.config) : undefined;
|
|
20
35
|
const now = flags.now === undefined ? Date.now() : Number(flags.now);
|
|
@@ -39,13 +54,14 @@ export async function runExchangeCli(argv) {
|
|
|
39
54
|
const imagePath = positional[0];
|
|
40
55
|
if (!imagePath || !flags.to)
|
|
41
56
|
throw new Error(
|
|
42
|
-
'Usage: knolo exchange offer <image.knolo> --to <peer> --config <exchange.json>'
|
|
57
|
+
'Usage: knolo exchange offer <image.knolo> --to <peer> [--via <https-url>] --config <exchange.json>'
|
|
43
58
|
);
|
|
44
59
|
const published = await publishExchangeOffer(config, {
|
|
45
60
|
imagePath,
|
|
46
61
|
to: flags.to,
|
|
47
62
|
now,
|
|
48
63
|
...(flags.ttl === undefined ? {} : { ttlMs: Number(flags.ttl) }),
|
|
64
|
+
...(flags.via === undefined ? {} : { via: flags.via }),
|
|
49
65
|
});
|
|
50
66
|
console.log(published.offerId);
|
|
51
67
|
return;
|
|
@@ -54,7 +70,7 @@ export async function runExchangeCli(argv) {
|
|
|
54
70
|
const offerId = positional[0];
|
|
55
71
|
if (!offerId || !flags.from)
|
|
56
72
|
throw new Error(
|
|
57
|
-
'Usage: knolo exchange pull <offer-id> --from <peer> --config <exchange.json>'
|
|
73
|
+
'Usage: knolo exchange pull <offer-id> --from <peer-or-https-url> --config <exchange.json>'
|
|
58
74
|
);
|
|
59
75
|
const pulled = await pullExchangeOffer(config, {
|
|
60
76
|
offerId,
|
|
@@ -168,7 +184,7 @@ export async function runExchangeCli(argv) {
|
|
|
168
184
|
return;
|
|
169
185
|
}
|
|
170
186
|
throw new Error(
|
|
171
|
-
'Usage: knolo exchange <serve|offer|pull|export|import|adopt|discover|inspect>'
|
|
187
|
+
'Usage: knolo exchange <serve|offer|pull|export|import|adopt|discover|inspect|relay>'
|
|
172
188
|
);
|
|
173
189
|
}
|
|
174
190
|
|
|
@@ -195,7 +211,8 @@ function parse(argv) {
|
|
|
195
211
|
arg === '--now' ||
|
|
196
212
|
arg === '--out' ||
|
|
197
213
|
arg === '--store' ||
|
|
198
|
-
arg === '--registry'
|
|
214
|
+
arg === '--registry' ||
|
|
215
|
+
arg === '--via'
|
|
199
216
|
) {
|
|
200
217
|
flags[arg.slice(2)] = argv[++i];
|
|
201
218
|
} else if (arg.startsWith('--')) {
|
package/bin/knolo.mjs
CHANGED
|
@@ -186,7 +186,7 @@ Commands:
|
|
|
186
186
|
skill Create and share a Knolo skill pack
|
|
187
187
|
research Package local research files into a V5 Knowledge Image
|
|
188
188
|
setup Prepare a local index and print the hub next steps
|
|
189
|
-
exchange Offer or pull a verified Knowledge Image from a peer
|
|
189
|
+
exchange Offer or pull a verified Knowledge Image from a peer or relay
|
|
190
190
|
icp Scaffold and operate an ICP-native knowledge canister
|
|
191
191
|
|
|
192
192
|
Global options:
|
|
@@ -209,7 +209,7 @@ function printCommandHelp(command) {
|
|
|
209
209
|
whoami: 'Usage: knolo whoami',
|
|
210
210
|
logout: 'Usage: knolo logout',
|
|
211
211
|
publish:
|
|
212
|
-
'Usage: knolo publish <pack.knolo> --slug <slug> --version <version> --license <SPDX> [--publisher <handle>] [--readme <text>] [--sources <text>] [--intended-use <text>] [--json] [--registry <url>]\n\nUses the stored dashboard token as Authorization: Bearer kno_…. Hub issues a public Blob PUT grant; the CLI never needs PACKS_READ_WRITE_TOKEN.\nUploads go to the grant URL without the kno_ token. Complete sends the public Blob URL, then Hub verifies before release.',
|
|
212
|
+
'Usage: knolo publish <pack.knolo> --slug <slug> --version <version> --license <SPDX> [--publisher <handle>] [--readme <text>] [--sources <text>] [--intended-use <text>] [--private] [--json] [--registry <url>]\n\nUses the stored dashboard token as Authorization: Bearer kno_…. Hub issues a public Blob PUT grant; the CLI never needs PACKS_READ_WRITE_TOKEN.\nUploads go to the grant URL without the kno_ token. Complete sends the public Blob URL, then Hub verifies before release.\n--private releases an owner-only pack on a Business or Enterprise plan. The success text omits the Blob URL.',
|
|
213
213
|
yank: 'Usage: knolo yank <publisher>/<slug>@<version> [--json] [--registry <url>]\n\nUses the stored token as Authorization: Bearer kno_…. Yank is owner-only and leaves the public Blob in place.',
|
|
214
214
|
build: 'Usage: knolo build',
|
|
215
215
|
query:
|
|
@@ -228,7 +228,7 @@ function printCommandHelp(command) {
|
|
|
228
228
|
cortex: 'Usage: knolo cortex <find|ask|explain|verify> [options]\n\nBounded evidence retrieval. The CLI does not call a model.\n\n knolo cortex find <image> --question "..." [--top-k 100] --out candidates.json\n knolo cortex ask <image> --question "..." [--graph file] [--plan plan.json] [--mode find_relevant] --out answer.cortex.json\n knolo cortex explain answer.cortex.json\n knolo cortex verify --image <image> --answer answer.cortex.json [--graph file]\n\nVerification recomputes the trace roots. It does not mean the answer is true. A KAR status of SATISFIED means evidence requirements were met. It does not mean a claim is proven.',
|
|
229
229
|
kite: 'Usage: knolo kite <evaluate|verify> [options]\n\n knolo kite evaluate --image <image.knolo> --contract <contract.json> --rules <rules.json> [--completeness <file>] [--changes <file>] [--bounds <file>] [--graph <file>] [--as-of YYYY-MM-DD] [--out <result.json>] [--json]\n knolo kite verify --image <image.knolo> --contract <contract.json> --rules <rules.json> --certificate <certificate.json> [--completeness <file>] [--changes <file>] [--bounds <file>] [--graph <file>] [--as-of YYYY-MM-DD]\n\nEvaluation names unmet obligations and derives a verdict inside the declared changes. Verification recomputes that derivation. A certificate is an integrity check. It does not mean the claim is true. BOUND_EXCEEDED means the search did not finish and is not a certificate. This command ships in this package. It loads @knolo/core/kite.',
|
|
230
230
|
research:
|
|
231
|
-
'Usage: knolo research package --question <text> --agent <id> --out <image.knolo> --source <file> [--source <file>] [--cite <image.knolo> --trace <answer.cortex.json> [--graph <file>]] [--json]\n\nPacks local files into one V5 Knowledge Image. --agent is the host-supplied caller. --cite and --trace are both required or both omitted. --graph is required when the trace contains KAR evaluations, and it is the same file knolo cortex verify would use. A trace is verified against --cite before any write. A failed check writes nothing. When --cite is set, the image stores a digest-bound composition record for that image and does not copy its bytes. When --graph is set, the checked graph is stored. The command does not call a model and does not download an image. A packed trace is an integrity check. It does not mean a claim is proven. SATISFIED means the trace evidence requirements were met.',
|
|
231
|
+
'Usage: knolo research package --question <text> --agent <id> --out <image.knolo> --source <file> [--source <file>] [--cite <image.knolo> --trace <answer.cortex.json> [--graph <file>]] [--json]\n\nPacks local files into one V5 Knowledge Image. --agent is the host-supplied caller. --cite and --trace are both required or both omitted. --graph is required when the trace contains KAR evaluations, and it is the same file knolo cortex verify would use. A trace is verified against --cite before any write. A failed check writes nothing. When --cite is set, the image stores a digest-bound composition record for that image and does not copy its bytes. When --graph is set, the checked graph is stored. The command does not call a model and does not download an image. A packed trace is an integrity check. It does not mean a claim is proven. SATISFIED means the trace evidence requirements were met.\n\nUsage: knolo research verify <image.knolo> [--cited <file>]... [--require-material] [--json]\n\nVerifies the image roots, then re-checks a stored composition record, Cortex trace, and KAR graph with the existing verifiers. --cited supplies member bytes in member order. Without those bytes, digest mode succeeds and says the cited bytes were not supplied. --require-material fails when a member has no bytes. The command does not call a model and does not download an image. A verified trace is an integrity check. It does not mean a claim is proven. SATISFIED means evidence requirements were met.',
|
|
232
232
|
flight: 'Usage: knolo flight <ingest|verify-log> [options]\n\n knolo flight ingest --events <events.json> --log <file> --out <image.knolo> [--json]\n knolo flight verify-log --image <image.knolo> --log <file> [--json]\n\ningest packs a flight-events-1 document into a V5 Knowledge Image. It hashes --log and writes nothing when the hash or byte length differs. verify-log hashes --log again and compares it with the flight-log object. It writes nothing. A missing flight-log object fails closed. Neither command decodes the log. log.format is ulog, dataflash, or rosbag2. A later KAR status of SATISFIED means evidence requirements were met. It does not mean a flight was safe, legal, or compliant.',
|
|
233
233
|
setup: `Usage: knolo setup [--registry <url>] [--root <dir>] [--agent <id>] [--auto-rebuild] [--reset] [--skills <path>] [--managed-skills <path>] [--memory <path>] [--transcripts <path>]
|
|
234
234
|
|
|
@@ -244,23 +244,26 @@ Pass --agent to set the caller name. Pass --auto-rebuild to rebuild on the next
|
|
|
244
244
|
Does not prompt. Does not install a pack or call the hub.
|
|
245
245
|
A missing knolo-box is printed, and setup still exits 0.
|
|
246
246
|
When more than one skill folder matches, pass --skills or --managed-skills.`,
|
|
247
|
-
exchange: `Usage: knolo exchange <serve|offer|pull|export|import|adopt|discover|inspect> --config <exchange.json>
|
|
247
|
+
exchange: `Usage: knolo exchange <serve|offer|pull|export|import|adopt|discover|inspect|relay> --config <exchange.json>
|
|
248
248
|
|
|
249
249
|
knolo exchange serve --config exchange.json
|
|
250
|
-
knolo exchange offer <image.knolo> --to <peer> --config exchange.json
|
|
251
|
-
knolo exchange pull <offer-id> --from <peer> --config exchange.json
|
|
250
|
+
knolo exchange offer <image.knolo> --to <peer> [--via <https-url>] --config exchange.json
|
|
251
|
+
knolo exchange pull <offer-id> --from <peer-or-https-url> --config exchange.json
|
|
252
252
|
knolo exchange export <image.knolo> --to <peer> --out <dir> --config exchange.json
|
|
253
253
|
knolo exchange import <dir> --config exchange.json
|
|
254
254
|
knolo exchange adopt <receipt-id> --store <file> --home <dir> --config exchange.json
|
|
255
255
|
knolo exchange discover <handle> [--registry <url>]
|
|
256
256
|
knolo exchange inspect <receipt-id> --home <dir> [--config exchange.json]
|
|
257
|
+
knolo exchange relay --config relay.json
|
|
257
258
|
|
|
258
259
|
Pull, export, and import do not call Knolo Hub and do not write knolo.lock.json.
|
|
259
260
|
export writes offer.cbor and the image into a directory. import reads that
|
|
260
261
|
directory. Receiving an image verifies SHA-256 and the V5 state root. It does
|
|
261
262
|
not mount the image into a store or activate a skill. adopt opens a local
|
|
262
263
|
store only when policy.adopt is allow. discover reads a public publisher
|
|
263
|
-
advertisement and does not download pack bytes
|
|
264
|
+
advertisement and does not download pack bytes. relay stores the offer and
|
|
265
|
+
the image so the sender can go offline. It does not verify, mount, or adopt,
|
|
266
|
+
and it is not Knolo Hub. offer --via uploads to that relay instead of serve.`,
|
|
264
267
|
skill: `Usage: knolo skill <init|check|pack|publish|install|search|info|list>
|
|
265
268
|
|
|
266
269
|
knolo skill init <name> [--dir <parent>]
|
|
@@ -75,13 +75,19 @@ export async function runHubSearch(
|
|
|
75
75
|
|
|
76
76
|
export async function runHubInfo(
|
|
77
77
|
args,
|
|
78
|
-
{
|
|
78
|
+
{
|
|
79
|
+
env = process.env,
|
|
80
|
+
fetchImpl = globalThis.fetch,
|
|
81
|
+
homeDir,
|
|
82
|
+
print = console.log,
|
|
83
|
+
} = {}
|
|
79
84
|
) {
|
|
80
85
|
const { positional, flags } = parseInfoArgs(args);
|
|
81
86
|
const pack = parsePackName(positional[0]);
|
|
82
87
|
const registry = resolveRegistryUrl({ value: flags.registry, env });
|
|
83
88
|
const url = registryApiUrl(registry, ['packs', pack.publisher, pack.slug]);
|
|
84
|
-
const
|
|
89
|
+
const headers = await optionalPublishHeaders(registry, { env, homeDir });
|
|
90
|
+
const body = await getHubJson(url, { fetchImpl, headers });
|
|
85
91
|
validatePackInfoResponse(body);
|
|
86
92
|
|
|
87
93
|
if (flags.json) print(JSON.stringify(body, null, 2));
|
|
@@ -115,12 +121,17 @@ export async function runHubAdd(
|
|
|
115
121
|
|
|
116
122
|
let manifest;
|
|
117
123
|
let yanked = false;
|
|
124
|
+
let rawBody;
|
|
125
|
+
const manifestHeaders = await optionalPublishHeaders(registry, {
|
|
126
|
+
env,
|
|
127
|
+
homeDir,
|
|
128
|
+
});
|
|
118
129
|
try {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
requestedVersion: spec.version,
|
|
130
|
+
rawBody = await getHubJson(manifestUrl, {
|
|
131
|
+
fetchImpl,
|
|
132
|
+
headers: manifestHeaders,
|
|
123
133
|
});
|
|
134
|
+
manifest = packManifestFromBody(rawBody, spec);
|
|
124
135
|
} catch (error) {
|
|
125
136
|
if (
|
|
126
137
|
error instanceof RegistryError &&
|
|
@@ -132,10 +143,8 @@ export async function runHubAdd(
|
|
|
132
143
|
error instanceof RegistryError &&
|
|
133
144
|
(error.status === 410 || error.code === 'yanked')
|
|
134
145
|
) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
requestedVersion: spec.version,
|
|
138
|
-
});
|
|
146
|
+
rawBody = error.body;
|
|
147
|
+
manifest = packManifestFromBody(error.body, spec);
|
|
139
148
|
yanked = true;
|
|
140
149
|
} else {
|
|
141
150
|
throw error;
|
|
@@ -149,6 +158,26 @@ export async function runHubAdd(
|
|
|
149
158
|
warn(
|
|
150
159
|
`warning: downloading yanked version ${manifest.name}@${manifest.version}`
|
|
151
160
|
);
|
|
161
|
+
if (manifest.visibility === 'private') {
|
|
162
|
+
const credentials = await requirePrivateCredentials(registry, {
|
|
163
|
+
env,
|
|
164
|
+
homeDir,
|
|
165
|
+
});
|
|
166
|
+
const signed = await getHubJson(
|
|
167
|
+
registryApiUrl(registry, [
|
|
168
|
+
'packs',
|
|
169
|
+
spec.publisher,
|
|
170
|
+
spec.slug,
|
|
171
|
+
spec.version,
|
|
172
|
+
'download',
|
|
173
|
+
]),
|
|
174
|
+
{ fetchImpl, headers: authorizationHeaders(credentials.token) }
|
|
175
|
+
);
|
|
176
|
+
manifest.url = assertSignedDownloadUrl(
|
|
177
|
+
signed?.url,
|
|
178
|
+
typeof rawBody?.url === 'string' ? rawBody.url : ''
|
|
179
|
+
);
|
|
180
|
+
}
|
|
152
181
|
if (!manifest.url) throw new Error('artifact bytes are not stored yet');
|
|
153
182
|
if (!PACK_SHA256_PATTERN.test(manifest.sha256))
|
|
154
183
|
throw new Error('manifest sha256 is invalid');
|
|
@@ -218,8 +247,14 @@ export async function runHubAdd(
|
|
|
218
247
|
await commitStagedFiles(stagedFiles);
|
|
219
248
|
const lock = { path: stagedLock.path, entry: stagedLock.entry };
|
|
220
249
|
const result = { ...manifest, path: outputPath, lockfile: lock.path };
|
|
250
|
+
if (result.visibility === 'private') delete result.url;
|
|
221
251
|
if (flags.json) print(JSON.stringify(result, null, 2));
|
|
222
|
-
else {
|
|
252
|
+
else if (manifest.visibility === 'private') {
|
|
253
|
+
print(`added ${manifest.name}@${manifest.version}`);
|
|
254
|
+
print(`sha256 ${manifest.sha256}`);
|
|
255
|
+
print('visibility private');
|
|
256
|
+
print(`path ${outputPath}`);
|
|
257
|
+
} else {
|
|
223
258
|
print(`added ${manifest.name}@${manifest.version}`);
|
|
224
259
|
print(`sha256 ${manifest.sha256}`);
|
|
225
260
|
print(`url ${manifest.url}`);
|
|
@@ -328,17 +363,26 @@ export async function runHubPublish(
|
|
|
328
363
|
const sha256 = createSha256(bytes);
|
|
329
364
|
const pathname = `sha256/${sha256}.knolo`;
|
|
330
365
|
|
|
331
|
-
const grant = await
|
|
366
|
+
const grant = await postHubJson(
|
|
332
367
|
hubApiUrl(registry, ['upload', 'token']),
|
|
333
|
-
{
|
|
368
|
+
{
|
|
369
|
+
sha256,
|
|
370
|
+
contentLength: bytes.length,
|
|
371
|
+
...(flags.private ? { visibility: 'private' } : {}),
|
|
372
|
+
},
|
|
334
373
|
{ fetchImpl, headers: authHeaders }
|
|
335
374
|
);
|
|
336
|
-
const upload = resolveUploadGrant(grant, sha256);
|
|
375
|
+
const upload = resolveUploadGrant(grant, sha256, { privatePack: Boolean(flags.private) });
|
|
337
376
|
let blobUrl;
|
|
338
|
-
if (upload.reused) {
|
|
339
|
-
blobUrl =
|
|
377
|
+
if (upload.reused && !upload.url && flags.private) {
|
|
378
|
+
blobUrl = undefined;
|
|
379
|
+
} else if (upload.reused) {
|
|
380
|
+
blobUrl = (flags.private ? assertPrivatePackBlobUrl : assertPublicPackBlobUrl)(
|
|
381
|
+
upload.url,
|
|
382
|
+
'reused artifact'
|
|
383
|
+
).toString();
|
|
340
384
|
} else {
|
|
341
|
-
assertGrantPutUrl(upload.putUrl, 'upload grant');
|
|
385
|
+
assertGrantPutUrl(upload.putUrl, 'upload grant', { allowPrivate: Boolean(flags.private) });
|
|
342
386
|
const uploaded = await putBytes(upload.putUrl, bytes, {
|
|
343
387
|
fetchImpl,
|
|
344
388
|
headers: {
|
|
@@ -349,27 +393,28 @@ export async function runHubPublish(
|
|
|
349
393
|
});
|
|
350
394
|
if (uploaded.pathname !== undefined && uploaded.pathname !== pathname) {
|
|
351
395
|
throw new Error(
|
|
352
|
-
|
|
396
|
+
`${flags.private ? 'Private' : 'Public'} Blob upload pathname is not locked to ${pathname}.`
|
|
353
397
|
);
|
|
354
398
|
}
|
|
355
|
-
blobUrl = assertPublicPackBlobUrl(
|
|
399
|
+
blobUrl = (flags.private ? assertPrivatePackBlobUrl : assertPublicPackBlobUrl)(
|
|
356
400
|
uploaded.url,
|
|
357
401
|
'uploaded artifact'
|
|
358
402
|
).toString();
|
|
359
403
|
}
|
|
360
404
|
|
|
361
|
-
await
|
|
405
|
+
await postHubJson(
|
|
362
406
|
hubApiUrl(registry, ['upload', 'complete']),
|
|
363
407
|
{
|
|
364
408
|
sha256,
|
|
365
|
-
url: blobUrl,
|
|
409
|
+
...(blobUrl ? { url: blobUrl } : {}),
|
|
366
410
|
sizeBytes: bytes.length,
|
|
367
411
|
pathname,
|
|
412
|
+
...(flags.private ? { visibility: 'private' } : {}),
|
|
368
413
|
},
|
|
369
414
|
{ fetchImpl, headers: authHeaders }
|
|
370
415
|
);
|
|
371
416
|
|
|
372
|
-
const verifyResponse = await
|
|
417
|
+
const verifyResponse = await postHubJson(
|
|
373
418
|
registryApiUrl(registry, ['publish', 'verify']),
|
|
374
419
|
{ sha256 },
|
|
375
420
|
{
|
|
@@ -394,13 +439,14 @@ export async function runHubPublish(
|
|
|
394
439
|
version: flags.version,
|
|
395
440
|
license: flags.license,
|
|
396
441
|
attested: true,
|
|
442
|
+
...(flags.private ? { visibility: 'private' } : {}),
|
|
397
443
|
...(flags.readme !== undefined ? { readme: flags.readme } : {}),
|
|
398
444
|
...(flags.sources !== undefined ? { sources: flags.sources } : {}),
|
|
399
445
|
...(flags.intendedUse !== undefined
|
|
400
446
|
? { intendedUse: flags.intendedUse }
|
|
401
447
|
: {}),
|
|
402
448
|
};
|
|
403
|
-
const draftResponse = await
|
|
449
|
+
const draftResponse = await postHubJson(
|
|
404
450
|
registryApiUrl(registry, ['publish', 'drafts']),
|
|
405
451
|
draftPayload,
|
|
406
452
|
{
|
|
@@ -412,7 +458,7 @@ export async function runHubPublish(
|
|
|
412
458
|
if (!draftId)
|
|
413
459
|
throw new Error('Hub draft response did not include a draft id.');
|
|
414
460
|
|
|
415
|
-
const releaseResponse = await
|
|
461
|
+
const releaseResponse = await postHubJson(
|
|
416
462
|
registryApiUrl(registry, ['publish', 'drafts', draftId, 'release']),
|
|
417
463
|
undefined,
|
|
418
464
|
{
|
|
@@ -430,7 +476,7 @@ export async function runHubPublish(
|
|
|
430
476
|
sha256,
|
|
431
477
|
sizeBytes: bytes.length,
|
|
432
478
|
pathname,
|
|
433
|
-
url: blobUrl,
|
|
479
|
+
...(flags.private ? { visibility: 'private' } : { url: blobUrl }),
|
|
434
480
|
jobId: String(jobId),
|
|
435
481
|
draftId: String(draftId),
|
|
436
482
|
verification: job,
|
|
@@ -438,7 +484,12 @@ export async function runHubPublish(
|
|
|
438
484
|
};
|
|
439
485
|
|
|
440
486
|
if (flags.json) print(JSON.stringify(result, null, 2));
|
|
441
|
-
else {
|
|
487
|
+
else if (flags.private) {
|
|
488
|
+
print(`published ${name}@${flags.version}`);
|
|
489
|
+
print(`sha256 ${sha256}`);
|
|
490
|
+
print('visibility private');
|
|
491
|
+
print(`job ${jobId}`);
|
|
492
|
+
} else {
|
|
442
493
|
print(`published ${name}@${flags.version}`);
|
|
443
494
|
print(`sha256 ${sha256}`);
|
|
444
495
|
print(`url ${blobUrl}`);
|
|
@@ -506,6 +557,10 @@ export function parsePublishArgs(args) {
|
|
|
506
557
|
flags.json = true;
|
|
507
558
|
continue;
|
|
508
559
|
}
|
|
560
|
+
if (arg === '--private') {
|
|
561
|
+
flags.private = true;
|
|
562
|
+
continue;
|
|
563
|
+
}
|
|
509
564
|
if (valueFlags.has(arg)) {
|
|
510
565
|
const value = args[++i];
|
|
511
566
|
if (value === undefined || value.startsWith('--'))
|
|
@@ -525,7 +580,7 @@ export function parsePublishArgs(args) {
|
|
|
525
580
|
!flags.license
|
|
526
581
|
) {
|
|
527
582
|
throw new Error(
|
|
528
|
-
'Usage: knolo publish <pack.knolo> --slug <slug> --version <version> --license <SPDX> [--publisher <handle>] [--readme <text>] [--sources <text>] [--intended-use <text>] [--json] [--registry <url>]'
|
|
583
|
+
'Usage: knolo publish <pack.knolo> --slug <slug> --version <version> --license <SPDX> [--publisher <handle>] [--readme <text>] [--sources <text>] [--intended-use <text>] [--private] [--json] [--registry <url>]'
|
|
529
584
|
);
|
|
530
585
|
}
|
|
531
586
|
if (!/^[a-z0-9-]+$/.test(flags.slug))
|
|
@@ -577,6 +632,87 @@ function authorizationHeaders(token) {
|
|
|
577
632
|
return { Authorization: `Bearer ${token}` };
|
|
578
633
|
}
|
|
579
634
|
|
|
635
|
+
function planLimitError(error) {
|
|
636
|
+
if (
|
|
637
|
+
!(error instanceof RegistryError) ||
|
|
638
|
+
error.status !== 402 ||
|
|
639
|
+
error.code !== 'plan_limit'
|
|
640
|
+
) {
|
|
641
|
+
return error;
|
|
642
|
+
}
|
|
643
|
+
const message =
|
|
644
|
+
typeof error.body?.error === 'string' && error.body.error.trim()
|
|
645
|
+
? error.body.error.trim()
|
|
646
|
+
: error.message;
|
|
647
|
+
const upgrade =
|
|
648
|
+
typeof error.body?.upgradeUrl === 'string'
|
|
649
|
+
? error.body.upgradeUrl.trim()
|
|
650
|
+
: '';
|
|
651
|
+
return new Error(upgrade ? `${message}\n${upgrade}` : message);
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
async function postHubJson(url, body, options) {
|
|
655
|
+
try {
|
|
656
|
+
return await postJson(url, body, options);
|
|
657
|
+
} catch (error) {
|
|
658
|
+
throw planLimitError(error);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
async function getHubJson(url, options) {
|
|
663
|
+
try {
|
|
664
|
+
return await getJson(url, options);
|
|
665
|
+
} catch (error) {
|
|
666
|
+
throw planLimitError(error);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
async function optionalPublishHeaders(registry, { env, homeDir } = {}) {
|
|
671
|
+
const credentials = await readMatchingCredentials(registry, { env, homeDir });
|
|
672
|
+
return credentials ? authorizationHeaders(credentials.token) : {};
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
async function readMatchingCredentials(registry, { env, homeDir } = {}) {
|
|
676
|
+
try {
|
|
677
|
+
const credentials = await loadCredentials({ env, homeDir });
|
|
678
|
+
if (
|
|
679
|
+
normalizeRegistryUrl(credentials.registry) !==
|
|
680
|
+
normalizeRegistryUrl(registry)
|
|
681
|
+
) {
|
|
682
|
+
return undefined;
|
|
683
|
+
}
|
|
684
|
+
return credentials;
|
|
685
|
+
} catch (error) {
|
|
686
|
+
if (error instanceof Error && error.message.startsWith('Not logged in')) {
|
|
687
|
+
return undefined;
|
|
688
|
+
}
|
|
689
|
+
throw error;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
async function requirePrivateCredentials(registry, { env, homeDir } = {}) {
|
|
694
|
+
let credentials;
|
|
695
|
+
try {
|
|
696
|
+
credentials = await loadCredentials({ env, homeDir });
|
|
697
|
+
} catch (error) {
|
|
698
|
+
if (error instanceof Error && error.message.startsWith('Not logged in')) {
|
|
699
|
+
throw new Error('Private packs require a Hub token. Run knolo login.');
|
|
700
|
+
}
|
|
701
|
+
throw error;
|
|
702
|
+
}
|
|
703
|
+
resolveCredentialRegistry(registry, credentials.registry);
|
|
704
|
+
return credentials;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
function packManifestFromBody(body, spec) {
|
|
708
|
+
const manifest = validatePackManifest(body, {
|
|
709
|
+
expectedName: spec.name,
|
|
710
|
+
requestedVersion: spec.version,
|
|
711
|
+
});
|
|
712
|
+
if (body?.visibility === 'private') manifest.visibility = 'private';
|
|
713
|
+
return manifest;
|
|
714
|
+
}
|
|
715
|
+
|
|
580
716
|
function resolveCredentialRegistry(requestedRegistry, storedRegistry) {
|
|
581
717
|
const stored = normalizeRegistryUrl(storedRegistry);
|
|
582
718
|
if (!requestedRegistry) return stored;
|
|
@@ -599,7 +735,7 @@ function createSha256(bytes) {
|
|
|
599
735
|
return createHash('sha256').update(bytes).digest('hex');
|
|
600
736
|
}
|
|
601
737
|
|
|
602
|
-
function resolveUploadGrant(body, sha256) {
|
|
738
|
+
function resolveUploadGrant(body, sha256, { privatePack = false } = {}) {
|
|
603
739
|
const pathname = body?.pathname || body?.upload?.pathname;
|
|
604
740
|
const expectedPathname = `sha256/${sha256}.knolo`;
|
|
605
741
|
if (typeof pathname !== 'string' || pathname !== expectedPathname) {
|
|
@@ -612,6 +748,9 @@ function resolveUploadGrant(body, sha256) {
|
|
|
612
748
|
if (typeof reusedUrl === 'string' && reusedUrl) {
|
|
613
749
|
return { reused: true, pathname, url: reusedUrl };
|
|
614
750
|
}
|
|
751
|
+
if (body?.reused === true && privatePack) {
|
|
752
|
+
return { reused: true, pathname };
|
|
753
|
+
}
|
|
615
754
|
|
|
616
755
|
const upload = body?.upload && typeof body.upload === 'object' ? body.upload : body;
|
|
617
756
|
if (!upload || typeof upload.url !== 'string' || !upload.url) {
|
|
@@ -631,7 +770,7 @@ function resolveUploadGrant(body, sha256) {
|
|
|
631
770
|
};
|
|
632
771
|
}
|
|
633
772
|
|
|
634
|
-
function assertGrantPutUrl(value, label) {
|
|
773
|
+
function assertGrantPutUrl(value, label, { allowPrivate = false } = {}) {
|
|
635
774
|
let url;
|
|
636
775
|
try {
|
|
637
776
|
url = new URL(value);
|
|
@@ -642,8 +781,8 @@ function assertGrantPutUrl(value, label) {
|
|
|
642
781
|
throw new Error(`Hub ${label} URL must use HTTPS.`);
|
|
643
782
|
const hostname = url.hostname.toLowerCase();
|
|
644
783
|
if (
|
|
645
|
-
|
|
646
|
-
|
|
784
|
+
(!allowPrivate &&
|
|
785
|
+
(hostname.includes('.private.') || hostname.startsWith('private.'))) ||
|
|
647
786
|
isPrivateOrLocalIp(hostname) ||
|
|
648
787
|
isLocalOnlyHostname(hostname)
|
|
649
788
|
) {
|
|
@@ -654,6 +793,34 @@ function assertGrantPutUrl(value, label) {
|
|
|
654
793
|
return url;
|
|
655
794
|
}
|
|
656
795
|
|
|
796
|
+
function assertSignedDownloadUrl(value, storedUrl) {
|
|
797
|
+
if (typeof value !== 'string' || !value) {
|
|
798
|
+
throw new Error('Hub did not return a signed download URL.');
|
|
799
|
+
}
|
|
800
|
+
let url;
|
|
801
|
+
try {
|
|
802
|
+
url = new URL(value);
|
|
803
|
+
} catch {
|
|
804
|
+
throw new Error('Hub signed download URL is invalid.');
|
|
805
|
+
}
|
|
806
|
+
if (url.protocol !== 'https:')
|
|
807
|
+
throw new Error('Hub signed download URL must use HTTPS.');
|
|
808
|
+
const hostname = url.hostname.toLowerCase();
|
|
809
|
+
if (isPrivateOrLocalIp(hostname) || isLocalOnlyHostname(hostname)) {
|
|
810
|
+
throw new Error('Hub signed download URL must not be local.');
|
|
811
|
+
}
|
|
812
|
+
if (storedUrl && value === storedUrl) {
|
|
813
|
+
throw new Error('Refusing the stored private Blob URL.');
|
|
814
|
+
}
|
|
815
|
+
if (
|
|
816
|
+
(hostname.includes('.private.') || hostname.startsWith('private.')) &&
|
|
817
|
+
!url.search
|
|
818
|
+
) {
|
|
819
|
+
throw new Error('Refusing the stored private Blob URL.');
|
|
820
|
+
}
|
|
821
|
+
return url.toString();
|
|
822
|
+
}
|
|
823
|
+
|
|
657
824
|
function extractPublisherHandle(account) {
|
|
658
825
|
const candidates = [
|
|
659
826
|
account?.publisherHandle,
|
|
@@ -720,6 +887,32 @@ function assertPublicPackBlobUrl(value, label) {
|
|
|
720
887
|
return url;
|
|
721
888
|
}
|
|
722
889
|
|
|
890
|
+
function assertPrivatePackBlobUrl(value, label) {
|
|
891
|
+
let url;
|
|
892
|
+
try {
|
|
893
|
+
url = new URL(value);
|
|
894
|
+
} catch {
|
|
895
|
+
throw new Error(`Hub ${label} URL is invalid.`);
|
|
896
|
+
}
|
|
897
|
+
if (url.protocol !== 'https:')
|
|
898
|
+
throw new Error(`Hub ${label} URL must use HTTPS.`);
|
|
899
|
+
const hostname = url.hostname.toLowerCase();
|
|
900
|
+
if (isPrivateOrLocalIp(hostname) || isLocalOnlyHostname(hostname)) {
|
|
901
|
+
throw new Error(
|
|
902
|
+
`Refusing local-only Blob URL for ${label}; a private pack uses a private Blob host.`
|
|
903
|
+
);
|
|
904
|
+
}
|
|
905
|
+
const blobHost =
|
|
906
|
+
hostname === 'blob.vercel-storage.com' ||
|
|
907
|
+
hostname.endsWith('.blob.vercel-storage.com');
|
|
908
|
+
const privateHost =
|
|
909
|
+
hostname.includes('.private.') || hostname.startsWith('private.');
|
|
910
|
+
if (!blobHost || !privateHost) {
|
|
911
|
+
throw new Error(`Hub ${label} URL must be a private Vercel Blob pack URL.`);
|
|
912
|
+
}
|
|
913
|
+
return url;
|
|
914
|
+
}
|
|
915
|
+
|
|
723
916
|
function isLocalOnlyHostname(hostname) {
|
|
724
917
|
const normalized = hostname.replace(/^\[|\]$/g, '').replace(/\.$/, '');
|
|
725
918
|
return (
|
|
@@ -852,7 +1045,7 @@ async function waitForPublishJob(
|
|
|
852
1045
|
|
|
853
1046
|
for (let attempt = 0; attempt < maxPollAttempts; attempt++) {
|
|
854
1047
|
if (attempt > 0 && pollIntervalMs > 0) await sleepImpl(pollIntervalMs);
|
|
855
|
-
const body = await
|
|
1048
|
+
const body = await getHubJson(
|
|
856
1049
|
registryApiUrl(registry, ['publish', 'jobs', jobId]),
|
|
857
1050
|
{ fetchImpl, headers }
|
|
858
1051
|
);
|
|
@@ -1204,8 +1397,16 @@ export function renderPackInfo(body) {
|
|
|
1204
1397
|
`topics ${Array.isArray(pack.topics) ? pack.topics.join(', ') : ''}`,
|
|
1205
1398
|
`latest ${latest.version || ''}`,
|
|
1206
1399
|
`sha256 ${latest.sha256 || pack.sha256 || ''}`,
|
|
1207
|
-
`url ${latest.url || pack.blobUrl || ''}`,
|
|
1208
1400
|
];
|
|
1401
|
+
const privatePack =
|
|
1402
|
+
body.visibility === 'private' ||
|
|
1403
|
+
pack.visibility === 'private' ||
|
|
1404
|
+
latest.visibility === 'private';
|
|
1405
|
+
lines.push(
|
|
1406
|
+
privatePack
|
|
1407
|
+
? 'visibility private'
|
|
1408
|
+
: `url ${latest.url || pack.blobUrl || ''}`
|
|
1409
|
+
);
|
|
1209
1410
|
return lines.join('\n');
|
|
1210
1411
|
}
|
|
1211
1412
|
|
package/bin/research.mjs
CHANGED
|
@@ -19,7 +19,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
19
19
|
const ACTOR = 'knolo-research';
|
|
20
20
|
const AGENT = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
21
21
|
|
|
22
|
-
const
|
|
22
|
+
const PACKAGE_USAGE = `Usage: knolo research package --question <text> --agent <id> --out <image.knolo> --source <file> [--source <file>] [--cite <image.knolo> --trace <answer.cortex.json> [--graph <file>]] [--json]
|
|
23
23
|
|
|
24
24
|
Packs local files into one V5 Knowledge Image. --agent is the host-supplied caller.
|
|
25
25
|
--cite and --trace are both required or both omitted. --graph is required when the
|
|
@@ -31,6 +31,17 @@ The command does not call a model and does not download an image.
|
|
|
31
31
|
A packed trace is an integrity check. It does not mean a claim is proven.
|
|
32
32
|
SATISFIED means the trace evidence requirements were met.`;
|
|
33
33
|
|
|
34
|
+
const VERIFY_USAGE = `Usage: knolo research verify <image.knolo> [--cited <file>]... [--require-material] [--json]
|
|
35
|
+
|
|
36
|
+
Verifies the image roots, then re-checks a stored composition record, Cortex trace,
|
|
37
|
+
and KAR graph with the existing verifiers. --cited supplies member bytes in member
|
|
38
|
+
order. Without those bytes, digest mode succeeds and says the cited bytes were not
|
|
39
|
+
supplied. --require-material fails when a member has no bytes. The command does not
|
|
40
|
+
call a model and does not download an image. A verified trace is an integrity check.
|
|
41
|
+
It does not mean a claim is proven. SATISFIED means evidence requirements were met.`;
|
|
42
|
+
|
|
43
|
+
const USAGE = `${PACKAGE_USAGE}\n\n${VERIFY_USAGE}`;
|
|
44
|
+
|
|
34
45
|
export async function runResearchCli(argv) {
|
|
35
46
|
if (
|
|
36
47
|
argv.length === 0 ||
|
|
@@ -41,15 +52,25 @@ export async function runResearchCli(argv) {
|
|
|
41
52
|
console.log(USAGE);
|
|
42
53
|
return;
|
|
43
54
|
}
|
|
44
|
-
if (argv[0]
|
|
45
|
-
|
|
55
|
+
if (argv[0] === 'package') {
|
|
56
|
+
const flags = parseFlags(argv.slice(1));
|
|
57
|
+
if (flags.help) {
|
|
58
|
+
console.log(PACKAGE_USAGE);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
await packageResearch(flags);
|
|
62
|
+
return;
|
|
46
63
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
64
|
+
if (argv[0] === 'verify') {
|
|
65
|
+
const flags = parseVerifyFlags(argv.slice(1));
|
|
66
|
+
if (flags.help) {
|
|
67
|
+
console.log(VERIFY_USAGE);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
await verifyResearch(flags);
|
|
50
71
|
return;
|
|
51
72
|
}
|
|
52
|
-
|
|
73
|
+
throw new Error(`Unknown research command "${argv[0]}".\n${USAGE}`);
|
|
53
74
|
}
|
|
54
75
|
|
|
55
76
|
async function packageResearch(flags) {
|
|
@@ -202,6 +223,211 @@ async function packageResearch(flags) {
|
|
|
202
223
|
);
|
|
203
224
|
}
|
|
204
225
|
|
|
226
|
+
async function verifyResearch(flags) {
|
|
227
|
+
if (!flags.image) {
|
|
228
|
+
throw new Error(`RESEARCH_PACK_INVALID: pass the research image.\n${VERIFY_USAGE}`);
|
|
229
|
+
}
|
|
230
|
+
const bytes = readBounded(flags.image, KAR_CLI_MAX_IMAGE_BYTES);
|
|
231
|
+
const apis = await loadApis();
|
|
232
|
+
let verification;
|
|
233
|
+
try {
|
|
234
|
+
verification = apis.core.verifyKnowledgeImageV5(bytes);
|
|
235
|
+
} catch (error) {
|
|
236
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
237
|
+
throw new Error(`KNOWLEDGE_IMAGE_INVALID: ${message}`);
|
|
238
|
+
}
|
|
239
|
+
if (!verification.valid) {
|
|
240
|
+
throw new Error('KNOWLEDGE_IMAGE_INVALID: Image roots do not verify.');
|
|
241
|
+
}
|
|
242
|
+
const image = apis.core.mountKnowledgeImageV5(bytes);
|
|
243
|
+
const packs = byRole(image, 'research-pack');
|
|
244
|
+
const traces = byRole(image, 'cortex-trace');
|
|
245
|
+
const compositions = byRole(image, 'composition');
|
|
246
|
+
const graphs = byRole(image, 'kar-graph');
|
|
247
|
+
if (packs.length !== 1) {
|
|
248
|
+
throw new Error('RESEARCH_PACK_INVALID: the image needs one research-pack object.');
|
|
249
|
+
}
|
|
250
|
+
const agent = packs[0].meta.agent;
|
|
251
|
+
if (typeof agent !== 'string' || !AGENT.test(agent)) {
|
|
252
|
+
throw new Error('RESEARCH_PACK_INVALID: the research pack has no caller.');
|
|
253
|
+
}
|
|
254
|
+
if (traces.length > 1 || compositions.length > 1 || graphs.length > 1) {
|
|
255
|
+
throw new Error('RESEARCH_PACK_INVALID: a research role is repeated.');
|
|
256
|
+
}
|
|
257
|
+
if (Boolean(traces.length) !== Boolean(compositions.length) || (graphs.length && !traces.length)) {
|
|
258
|
+
throw new Error('RESEARCH_PACK_INVALID: the citation, trace, and graph do not belong together.');
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
let members = [];
|
|
262
|
+
let citedBytesSupplied = false;
|
|
263
|
+
let compositionRoot = null;
|
|
264
|
+
const supplied = new Map();
|
|
265
|
+
if (compositions.length === 1) {
|
|
266
|
+
const record = parseJson(compositions[0].bytes, 'COMPOSITION_INVALID');
|
|
267
|
+
let parsed;
|
|
268
|
+
try {
|
|
269
|
+
parsed = apis.composition.parseCompositionRecord(record);
|
|
270
|
+
} catch (error) {
|
|
271
|
+
throw coded(error, 'COMPOSITION_INVALID');
|
|
272
|
+
}
|
|
273
|
+
if (compositions[0].meta.compositionRoot !== parsed.compositionRoot) {
|
|
274
|
+
throw new Error('COMPOSITION_ROOT_MISMATCH: Stored compositionRoot does not match the record.');
|
|
275
|
+
}
|
|
276
|
+
if (flags.cited.length > parsed.members.length) {
|
|
277
|
+
throw new Error('RESEARCH_PACK_INVALID: more --cited files than composition members.');
|
|
278
|
+
}
|
|
279
|
+
const material = {};
|
|
280
|
+
for (let index = 0; index < flags.cited.length; index += 1) {
|
|
281
|
+
const member = parsed.members[index];
|
|
282
|
+
const citedBytes = new Uint8Array(readBounded(flags.cited[index], KAR_CLI_MAX_IMAGE_BYTES));
|
|
283
|
+
material[member.alias] = { bytes: citedBytes };
|
|
284
|
+
supplied.set(member.alias, citedBytes);
|
|
285
|
+
}
|
|
286
|
+
citedBytesSupplied = flags.cited.length > 0;
|
|
287
|
+
const verdict = apis.composition.verifyCompositionRecord(record, {
|
|
288
|
+
members: material,
|
|
289
|
+
requireMaterial: flags.requireMaterial,
|
|
290
|
+
});
|
|
291
|
+
if (!verdict.ok) {
|
|
292
|
+
throw new Error(`${verdict.code}: composition check failed.`);
|
|
293
|
+
}
|
|
294
|
+
compositionRoot = verdict.compositionRoot;
|
|
295
|
+
members = verdict.members.map((status) => {
|
|
296
|
+
const member = parsed.members.find((item) => item.alias === status.alias);
|
|
297
|
+
return {
|
|
298
|
+
alias: status.alias,
|
|
299
|
+
status: status.status,
|
|
300
|
+
stateRoot: member && member.stateRoot ? member.stateRoot : null,
|
|
301
|
+
sha256: member && member.sha256 ? member.sha256 : null,
|
|
302
|
+
};
|
|
303
|
+
});
|
|
304
|
+
const cite = members.find((member) => member.alias === 'cite');
|
|
305
|
+
if (cite && compositions[0].meta.citedStateRoot !== cite.stateRoot) {
|
|
306
|
+
throw new Error('COMPOSITION_DIGEST_MISMATCH: Stored citedStateRoot does not match the record.');
|
|
307
|
+
}
|
|
308
|
+
} else if (flags.requireMaterial && flags.cited.length > 0) {
|
|
309
|
+
throw new Error('RESEARCH_PACK_INVALID: --cited was passed and the image has no composition record.');
|
|
310
|
+
} else if (flags.cited.length > 0) {
|
|
311
|
+
throw new Error('RESEARCH_PACK_INVALID: --cited was passed and the image has no composition record.');
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
let traceRoot = null;
|
|
315
|
+
let traceChecked = false;
|
|
316
|
+
let traceStatuses = [];
|
|
317
|
+
const citeBytes = supplied.get('cite');
|
|
318
|
+
if (traces.length === 1) {
|
|
319
|
+
const trace = parseJson(traces[0].bytes, 'CORTEX_TRACE_INVALID');
|
|
320
|
+
traceStatuses = Array.isArray(trace.karEvaluations)
|
|
321
|
+
? trace.karEvaluations.map((item) => item && item.status).filter((status) => typeof status === 'string')
|
|
322
|
+
: [];
|
|
323
|
+
if (citeBytes) {
|
|
324
|
+
const graph = graphs.length === 1 ? parseJson(graphs[0].bytes, 'KAR_SIDECAR_INVALID') : undefined;
|
|
325
|
+
if (graph !== undefined) {
|
|
326
|
+
const kar = await loadKar();
|
|
327
|
+
try {
|
|
328
|
+
kar.createKarSession({ image: citeBytes, graph });
|
|
329
|
+
} catch (error) {
|
|
330
|
+
throw coded(error, 'KAR_GRAPH_INVALID');
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const verified = await apis.cortex.verifyCortexAnswer({
|
|
334
|
+
image: citeBytes,
|
|
335
|
+
question: typeof trace.question === 'string' ? trace.question : '',
|
|
336
|
+
result: trace,
|
|
337
|
+
...(graph === undefined ? {} : { graph }),
|
|
338
|
+
});
|
|
339
|
+
if (!verified.ok) throw new Error(`${verified.code}: ${verified.message}`);
|
|
340
|
+
if (traces[0].meta.traceRoot !== verified.traceRoot) {
|
|
341
|
+
throw new Error('CORTEX_ROOT_MISMATCH: Stored traceRoot does not match the trace.');
|
|
342
|
+
}
|
|
343
|
+
traceRoot = verified.traceRoot;
|
|
344
|
+
traceChecked = true;
|
|
345
|
+
} else {
|
|
346
|
+
traceRoot = typeof traces[0].meta.traceRoot === 'string' ? traces[0].meta.traceRoot : null;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
let graphChecked = false;
|
|
351
|
+
if (graphs.length === 1) {
|
|
352
|
+
const graph = parseJson(graphs[0].bytes, 'KAR_SIDECAR_INVALID');
|
|
353
|
+
const kar = await loadKar();
|
|
354
|
+
if (citeBytes) {
|
|
355
|
+
if (!traceChecked) {
|
|
356
|
+
try {
|
|
357
|
+
kar.createKarSession({ image: citeBytes, graph });
|
|
358
|
+
} catch (error) {
|
|
359
|
+
throw coded(error, 'KAR_GRAPH_INVALID');
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
graphChecked = true;
|
|
363
|
+
} else {
|
|
364
|
+
const sidecar = kar.validateKarSidecar(graph);
|
|
365
|
+
if (!sidecar.ok) {
|
|
366
|
+
const issue = sidecar.errors[0];
|
|
367
|
+
throw new Error(`${issue.code}: ${issue.message}`);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const summary = {
|
|
373
|
+
ok: true,
|
|
374
|
+
stateRoot: verification.stateRoot,
|
|
375
|
+
sha256: digest(bytes),
|
|
376
|
+
agent,
|
|
377
|
+
compositionRoot,
|
|
378
|
+
citedBytesSupplied,
|
|
379
|
+
members,
|
|
380
|
+
traceRoot,
|
|
381
|
+
traceChecked,
|
|
382
|
+
traceStatuses,
|
|
383
|
+
graphChecked,
|
|
384
|
+
graphPresent: graphs.length === 1,
|
|
385
|
+
};
|
|
386
|
+
if (flags.json) {
|
|
387
|
+
console.log(JSON.stringify(summary, null, 2));
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
const citedLines = members.length
|
|
391
|
+
? members
|
|
392
|
+
.map((member) => `${member.alias} ${member.status} ${member.sha256} ${member.stateRoot}`)
|
|
393
|
+
.join('\n ')
|
|
394
|
+
: 'none';
|
|
395
|
+
const traceLine = !traces.length
|
|
396
|
+
? 'none'
|
|
397
|
+
: traceChecked
|
|
398
|
+
? `${traceRoot}${traceStatuses.length ? ` ${traceStatuses.join(',')}` : ''}`
|
|
399
|
+
: `${traceRoot ?? 'stored'} not re-checked`;
|
|
400
|
+
const graphLine = !graphs.length ? 'none' : graphChecked ? 'checked' : 'present, cited bytes were not supplied';
|
|
401
|
+
const digestNote =
|
|
402
|
+
compositions.length === 1 && !citedBytesSupplied
|
|
403
|
+
? '\nCited bytes were not supplied.\n'
|
|
404
|
+
: '\n';
|
|
405
|
+
console.log(
|
|
406
|
+
`RESEARCH VERIFY\nstate root ${summary.stateRoot}\nfile sha256 ${summary.sha256}\nagent ${agent}\ncited ${citedLines}\ntrace ${traceLine}\ngraph ${graphLine}\n${digestNote}A verified trace is an integrity check. It does not mean a claim is proven.\nSATISFIED means evidence requirements were met.`
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function byRole(image, role) {
|
|
411
|
+
return image.objects.filter((object) => object.meta && object.meta.role === role);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function parseJson(bytes, code) {
|
|
415
|
+
try {
|
|
416
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
417
|
+
} catch {
|
|
418
|
+
throw new Error(`${code}: stored JSON is not valid.`);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function coded(error, fallback) {
|
|
423
|
+
if (error && typeof error.code === 'string' && error.code.length > 0) {
|
|
424
|
+
return new Error(`${error.code}: ${error.message}`);
|
|
425
|
+
}
|
|
426
|
+
if (error instanceof Error && /^[A-Z0-9_]+:/.test(error.message)) return error;
|
|
427
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
428
|
+
return new Error(`${fallback}: ${message}`);
|
|
429
|
+
}
|
|
430
|
+
|
|
205
431
|
function writeNewFile(resolved, display, bytes) {
|
|
206
432
|
try {
|
|
207
433
|
writeFileSync(resolved, bytes, { flag: 'wx' });
|
|
@@ -260,6 +486,14 @@ function required(value, flag) {
|
|
|
260
486
|
return value;
|
|
261
487
|
}
|
|
262
488
|
|
|
489
|
+
async function loadKar() {
|
|
490
|
+
const dist = path.resolve(__dirname, '../../core/dist');
|
|
491
|
+
return importFirst(
|
|
492
|
+
[path.join(dist, 'experimental/kar/index.js'), '@knolo/core/experimental/kar'],
|
|
493
|
+
(loaded) => loaded.createKarSession && loaded.validateKarSidecar
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
|
|
263
497
|
async function loadApis() {
|
|
264
498
|
const dist = path.resolve(__dirname, '../../core/dist');
|
|
265
499
|
const core = await importFirst(
|
|
@@ -292,6 +526,40 @@ async function importFirst(candidates, check) {
|
|
|
292
526
|
throw new Error('Could not load @knolo/core. Build packages/core first.');
|
|
293
527
|
}
|
|
294
528
|
|
|
529
|
+
function parseVerifyFlags(argv) {
|
|
530
|
+
const flags = { cited: [], help: false, json: false, requireMaterial: false, image: '' };
|
|
531
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
532
|
+
const arg = argv[index];
|
|
533
|
+
if (arg === '--help' || arg === '-h') {
|
|
534
|
+
flags.help = true;
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
if (arg === '--json') {
|
|
538
|
+
flags.json = true;
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
if (arg === '--require-material') {
|
|
542
|
+
flags.requireMaterial = true;
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
if (arg === '--cited') {
|
|
546
|
+
const value = argv[index + 1];
|
|
547
|
+
if (value === undefined || value.startsWith('--')) {
|
|
548
|
+
throw new Error('Missing value for --cited');
|
|
549
|
+
}
|
|
550
|
+
flags.cited.push(value);
|
|
551
|
+
index += 1;
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
if (arg.startsWith('--')) {
|
|
555
|
+
throw new Error(`Unknown option ${arg}.\n${VERIFY_USAGE}`);
|
|
556
|
+
}
|
|
557
|
+
if (flags.image) throw new Error(`Unexpected argument "${arg}".\n${VERIFY_USAGE}`);
|
|
558
|
+
flags.image = arg;
|
|
559
|
+
}
|
|
560
|
+
return flags;
|
|
561
|
+
}
|
|
562
|
+
|
|
295
563
|
function parseFlags(argv) {
|
|
296
564
|
const flags = { sources: [], help: false, json: false };
|
|
297
565
|
for (let index = 0; index < argv.length; index += 1) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knolo/cli",
|
|
3
|
-
"version": "6.2.
|
|
3
|
+
"version": "6.2.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": {
|
|
6
6
|
"knolo": "bin/knolo.mjs"
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@knolo/core": "^6.2.3",
|
|
36
|
-
"@knolo/exchange": "^0.1.
|
|
36
|
+
"@knolo/exchange": "^0.1.1"
|
|
37
37
|
},
|
|
38
38
|
"private": false
|
|
39
39
|
}
|