@dharma-ai-labs/agent-fabric-relay-client 0.2.29 → 0.2.30
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 +14 -0
- package/dist/hostOperationScope.d.ts +16 -0
- package/dist/hostOperationScope.d.ts.map +1 -0
- package/dist/hostOperationScope.js +58 -0
- package/dist/hostOperationScope.js.map +1 -0
- package/dist/hostProtectedHelpers.test.d.ts +2 -0
- package/dist/hostProtectedHelpers.test.d.ts.map +1 -0
- package/dist/hostProtectedHelpers.test.js +121 -0
- package/dist/hostProtectedHelpers.test.js.map +1 -0
- package/dist/hostScope.test.d.ts +2 -0
- package/dist/hostScope.test.d.ts.map +1 -0
- package/dist/hostScope.test.js +372 -0
- package/dist/hostScope.test.js.map +1 -0
- package/dist/index.d.ts +26 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +280 -61
- package/dist/index.js.map +1 -1
- package/dist/setupClaim.d.ts +12 -0
- package/dist/setupClaim.d.ts.map +1 -1
- package/dist/setupClaim.js +66 -10
- package/dist/setupClaim.js.map +1 -1
- package/dist/setupClaim.test.js +263 -0
- package/dist/setupClaim.test.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,20 @@ ciphertext, private key or private terminal input. HTTPS redirects, changed
|
|
|
19
19
|
approval context, expired responses and unavailable protected storage fail
|
|
20
20
|
closed. Source tests do not establish live enrollment or startup readiness.
|
|
21
21
|
|
|
22
|
+
The optional local `onFailureDiagnostic` observer receives only a frozen
|
|
23
|
+
`dharma.setup-claim-failure/v1` record with the fixed `setup_claim_failed` code
|
|
24
|
+
and an allowlisted phase. The CLI emits this record to stderr; stdout and the
|
|
25
|
+
existing sanitized exception remain unchanged. Observer failures are ignored.
|
|
26
|
+
No caught exception, server response, credential, approval URL or device data
|
|
27
|
+
is included. A phase describes the last local boundary attempted, not the
|
|
28
|
+
remote root cause, proof that a preceding phase succeeded atomically, or
|
|
29
|
+
authority to retry. In particular, a `credential_commit` failure may retain
|
|
30
|
+
protected partial writes for the existing same-key recovery path.
|
|
31
|
+
|
|
32
|
+
This diagnostic does not provide a trusted pre-enrollment host callback, relax
|
|
33
|
+
the coding sandbox, or make an unavailable OS-backed store usable. Qualify
|
|
34
|
+
those runtime boundaries independently before actual onboarding.
|
|
35
|
+
|
|
22
36
|
- [Bidirectional protocol](https://github.com/dharma-ai-labs/dharma-agent-fabric/blob/main/docs/05-bidirectional-protocol.md)
|
|
23
37
|
- [Security boundary](https://github.com/dharma-ai-labs/dharma-agent-fabric/blob/main/docs/10-security-privacy-and-threat-model.md)
|
|
24
38
|
- [Source](https://github.com/dharma-ai-labs/dharma-agent-fabric/tree/main/packages/relay-client)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SecureSecretStore } from '@dharma-ai-labs/agent-fabric-secure-store';
|
|
2
|
+
/** Supplied by the owning host, never by a task, model argument or saved approval. */
|
|
3
|
+
export interface HostOperationScope {
|
|
4
|
+
signal: AbortSignal;
|
|
5
|
+
current(): Promise<boolean>;
|
|
6
|
+
}
|
|
7
|
+
export declare class HostOperationFence {
|
|
8
|
+
#private;
|
|
9
|
+
readonly signal: AbortSignal;
|
|
10
|
+
constructor(scope: HostOperationScope);
|
|
11
|
+
withdraw(): void;
|
|
12
|
+
assert(): Promise<void>;
|
|
13
|
+
step<T>(operation: () => Promise<T>): Promise<T>;
|
|
14
|
+
store(raw: SecureSecretStore): SecureSecretStore;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=hostOperationScope.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hostOperationScope.d.ts","sourceRoot":"","sources":["../src/hostOperationScope.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,2CAA2C,CAAC;AAEjF,sFAAsF;AACtF,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,WAAW,CAAC;IACpB,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC7B;AAED,qBAAa,kBAAkB;;IAC7B,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;gBAIjB,KAAK,EAAE,kBAAkB;IASrC,QAAQ;IACF,MAAM;IAKN,IAAI,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAKtD,KAAK,CAAC,GAAG,EAAE,iBAAiB,GAAG,iBAAiB;CAUjD"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export class HostOperationFence {
|
|
2
|
+
signal;
|
|
3
|
+
#withdrawn = new AbortController();
|
|
4
|
+
#current;
|
|
5
|
+
#stores = new WeakMap();
|
|
6
|
+
constructor(scope) {
|
|
7
|
+
try {
|
|
8
|
+
if (!scope || typeof scope !== 'object')
|
|
9
|
+
throw new Error();
|
|
10
|
+
const signal = scope.signal, current = scope.current;
|
|
11
|
+
if (!(signal instanceof AbortSignal) || typeof current !== 'function')
|
|
12
|
+
throw new Error();
|
|
13
|
+
this.signal = AbortSignal.any([signal, this.#withdrawn.signal]);
|
|
14
|
+
this.#current = () => Reflect.apply(current, scope, []);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
throw new Error('relay_host_scope_unavailable');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
withdraw() { this.#withdrawn.abort(); }
|
|
21
|
+
async assert() {
|
|
22
|
+
let allowed = false;
|
|
23
|
+
try {
|
|
24
|
+
allowed = !this.signal.aborted && await this.#current() === true && !this.signal.aborted;
|
|
25
|
+
}
|
|
26
|
+
catch { }
|
|
27
|
+
if (!allowed) {
|
|
28
|
+
this.withdraw();
|
|
29
|
+
throw new Error('relay_host_scope_unavailable');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async step(operation) {
|
|
33
|
+
await this.assert();
|
|
34
|
+
try {
|
|
35
|
+
const value = await operation();
|
|
36
|
+
await this.assert();
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
await this.assert();
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
store(raw) {
|
|
45
|
+
const existing = this.#stores.get(raw);
|
|
46
|
+
if (existing)
|
|
47
|
+
return existing;
|
|
48
|
+
const guarded = { backend: raw.backend,
|
|
49
|
+
get: account => this.step(() => raw.get(account)),
|
|
50
|
+
getFresh: account => this.step(() => (raw.getFresh ?? raw.get).call(raw, account)),
|
|
51
|
+
put: (account, value) => this.step(() => raw.put(account, value)),
|
|
52
|
+
delete: account => this.step(() => raw.delete(account)) };
|
|
53
|
+
this.#stores.set(raw, guarded);
|
|
54
|
+
this.#stores.set(guarded, guarded);
|
|
55
|
+
return guarded;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=hostOperationScope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hostOperationScope.js","sourceRoot":"","sources":["../src/hostOperationScope.ts"],"names":[],"mappings":"AAQA,MAAM,OAAO,kBAAkB;IACpB,MAAM,CAAc;IACpB,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;IACnC,QAAQ,CAAyB;IACjC,OAAO,GAAG,IAAI,OAAO,EAAwC,CAAC;IACvE,YAAY,KAAyB;QACnC,IAAI,CAAC;YACH,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;gBAAE,MAAM,IAAI,KAAK,EAAE,CAAC;YAC3D,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,EAAE,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;YACrD,IAAI,CAAC,CAAC,MAAM,YAAY,WAAW,CAAC,IAAI,OAAO,OAAO,KAAK,UAAU;gBAAE,MAAM,IAAI,KAAK,EAAE,CAAC;YACzF,IAAI,CAAC,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,IAAI,CAA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AAQ,CAAC;QACtE,MAAM,OAAO,GAAsB,EAAC,OAAO,EAAE,GAAG,CAAC,OAAO;YACtD,GAAG,EAAE,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YACjD,QAAQ,EAAE,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YAClF,GAAG,EAAE,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YACjE,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,EAAC,CAAC;QAC3D,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACnE,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hostProtectedHelpers.test.d.ts","sourceRoot":"","sources":["../src/hostProtectedHelpers.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
import * as relay from './index.js';
|
|
4
|
+
const config = { schema: 'dharma.device-config/v1', organizationId: 'org_demo',
|
|
5
|
+
hqUrl: 'https://hq.example', relayUrl: 'wss://relay.example', deviceId: '11111111-1111-4111-8111-111111111111',
|
|
6
|
+
deviceName: 'Synthetic', platform: 'linux', publicKeyEd25519: 'a'.repeat(43), serverPublicKeyEd25519: 'b'.repeat(43),
|
|
7
|
+
enrolledAt: '2026-10-05T22:00:00.000Z' };
|
|
8
|
+
const skill = { config, workspaceId: '22222222-2222-4222-8222-222222222222',
|
|
9
|
+
organizationAgentId: '33333333-3333-4333-8333-333333333333', provider: 'codex',
|
|
10
|
+
bundleId: '44444444-4444-4444-8444-444444444444', receiptHash: `sha256:${'c'.repeat(64)}`,
|
|
11
|
+
activatedAt: config.enrolledAt, expiresAt: null };
|
|
12
|
+
function fixture() {
|
|
13
|
+
const values = new Map(), calls = [];
|
|
14
|
+
let allowed = true, after = () => { };
|
|
15
|
+
const store = { backend: 'linux-secret-service',
|
|
16
|
+
async get(account) { calls.push('get'); const value = values.get(account) ?? null; after(); return value; },
|
|
17
|
+
async getFresh(account) { calls.push('getFresh'); const value = values.get(account) ?? null; after(); return value; },
|
|
18
|
+
async put(account, value) { calls.push('put'); values.set(account, value); after(); },
|
|
19
|
+
async delete(account) { calls.push('delete'); values.delete(account); after(); } };
|
|
20
|
+
const scope = { signal: new AbortController().signal, current: async () => allowed };
|
|
21
|
+
return { values, calls, store, scope, allow(value) { allowed = value; }, after(fn) { after = fn; } };
|
|
22
|
+
}
|
|
23
|
+
const api = relay;
|
|
24
|
+
const quota = { schema: 'dharma.evidence-quota-anchor/v1', day: '2026-10-05', totalBytes: 0,
|
|
25
|
+
ledgerHash: 'd'.repeat(64), updatedAt: config.enrolledAt };
|
|
26
|
+
const operations = [
|
|
27
|
+
['loadOrganizationApiToken', { hqUrl: config.hqUrl, organizationId: config.organizationId }],
|
|
28
|
+
['saveOrganizationApiToken', { hqUrl: config.hqUrl, organizationId: config.organizationId, token: `dharma_org_${'e'.repeat(40)}` }],
|
|
29
|
+
['loadDeviceEnrollmentAnchor', { config }], ['saveDeviceEnrollmentAnchor', { config }],
|
|
30
|
+
['loadActiveSkillAuthorizationAnchor', skill], ['saveActiveSkillAuthorizationAnchor', skill],
|
|
31
|
+
['deleteActiveSkillAuthorizationAnchor', skill], ['loadEvidenceQuotaAnchor', { config }],
|
|
32
|
+
['saveEvidenceQuotaAnchor', { config, anchor: quota }],
|
|
33
|
+
];
|
|
34
|
+
test('every protected helper denies a withdrawn scope before its first store effect', async () => {
|
|
35
|
+
for (const [name, input] of operations) {
|
|
36
|
+
const f = fixture();
|
|
37
|
+
f.allow(false);
|
|
38
|
+
await assert.rejects(api[name]({ ...input, store: f.store, hostScope: f.scope }), { message: 'relay_host_scope_unavailable' }, name);
|
|
39
|
+
assert.deepEqual(f.calls, [], name);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
test('protected helper writes retain a partial effect but do not read it back after withdrawal', async () => {
|
|
43
|
+
for (const [name, input] of operations.filter(([name]) => name.startsWith('save'))) {
|
|
44
|
+
const f = fixture();
|
|
45
|
+
f.after(() => f.allow(false));
|
|
46
|
+
await assert.rejects(api[name]({ ...input, store: f.store, hostScope: f.scope }), { message: 'relay_host_scope_unavailable' }, name);
|
|
47
|
+
assert.deepEqual(f.calls, ['put'], name);
|
|
48
|
+
assert.equal(f.values.size, 1, name);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
test('enrollment fallback cannot migrate after losing authority during its first read', async () => {
|
|
52
|
+
const f = fixture();
|
|
53
|
+
f.after(() => f.allow(false));
|
|
54
|
+
await assert.rejects(api.loadDeviceEnrollmentAnchor({ config, store: f.store, hostScope: f.scope }), { message: 'relay_host_scope_unavailable' });
|
|
55
|
+
assert.deepEqual(f.calls, ['get']);
|
|
56
|
+
});
|
|
57
|
+
test('fresh skill reads neither expose a result nor fallback to another store effect after withdrawal', async () => {
|
|
58
|
+
const f = fixture();
|
|
59
|
+
await relay.saveActiveSkillAuthorizationAnchor({ ...skill, store: f.store });
|
|
60
|
+
f.calls.length = 0;
|
|
61
|
+
f.after(() => f.allow(false));
|
|
62
|
+
await assert.rejects(api.loadActiveSkillAuthorizationAnchor({ ...skill, fresh: true, store: f.store, hostScope: f.scope }), { message: 'relay_host_scope_unavailable' });
|
|
63
|
+
assert.deepEqual(f.calls, ['getFresh']);
|
|
64
|
+
});
|
|
65
|
+
test('post-enrollment identity access refuses a missing key without replacement writes', async () => {
|
|
66
|
+
const f = fixture();
|
|
67
|
+
assert.equal(typeof api.loadDeviceIdentity, 'function');
|
|
68
|
+
await assert.rejects(api.loadDeviceIdentity({ config, store: f.store, hostScope: f.scope }), { message: 'relay_host_device_identity_unavailable' });
|
|
69
|
+
assert.deepEqual(f.calls, ['get']);
|
|
70
|
+
assert.equal(f.values.size, 0);
|
|
71
|
+
});
|
|
72
|
+
test('current scoped helpers keep compatible protected read/write behavior', async () => {
|
|
73
|
+
const f = fixture(), common = { store: f.store, hostScope: f.scope };
|
|
74
|
+
const token = `dharma_org_${'e'.repeat(40)}`;
|
|
75
|
+
await api.saveOrganizationApiToken({ hqUrl: config.hqUrl, organizationId: config.organizationId, token, ...common });
|
|
76
|
+
assert.equal(await api.loadOrganizationApiToken({ hqUrl: config.hqUrl, organizationId: config.organizationId, ...common }), token);
|
|
77
|
+
const anchor = await api.saveDeviceEnrollmentAnchor({ config, ...common });
|
|
78
|
+
assert.deepEqual(await api.loadDeviceEnrollmentAnchor({ config, ...common }), anchor);
|
|
79
|
+
const active = await api.saveActiveSkillAuthorizationAnchor({ ...skill, ...common });
|
|
80
|
+
assert.deepEqual(await api.loadActiveSkillAuthorizationAnchor({ ...skill, fresh: true, ...common }), active);
|
|
81
|
+
await api.deleteActiveSkillAuthorizationAnchor({ ...skill, ...common });
|
|
82
|
+
assert.equal(await api.loadActiveSkillAuthorizationAnchor({ ...skill, ...common }), null);
|
|
83
|
+
await api.saveEvidenceQuotaAnchor({ config, anchor: quota, ...common });
|
|
84
|
+
assert.deepEqual(await api.loadEvidenceQuotaAnchor({ config, ...common }), quota);
|
|
85
|
+
});
|
|
86
|
+
test('scoped malformed anchors do not expose protected serialized content in parser errors', async () => {
|
|
87
|
+
for (const [save, load, input] of [
|
|
88
|
+
['saveDeviceEnrollmentAnchor', 'loadDeviceEnrollmentAnchor', { config }],
|
|
89
|
+
['saveActiveSkillAuthorizationAnchor', 'loadActiveSkillAuthorizationAnchor', skill],
|
|
90
|
+
['saveEvidenceQuotaAnchor', 'loadEvidenceQuotaAnchor', { config, anchor: quota }],
|
|
91
|
+
]) {
|
|
92
|
+
const f = fixture();
|
|
93
|
+
await api[save]({ ...input, store: f.store });
|
|
94
|
+
for (const account of f.values.keys())
|
|
95
|
+
f.values.set(account, 'private-malformed-anchor-canary');
|
|
96
|
+
await assert.rejects(api[load]({ ...input, store: f.store, hostScope: f.scope }), error => {
|
|
97
|
+
assert.ok(error instanceof Error);
|
|
98
|
+
assert.doesNotMatch(error.message, /private-ma/);
|
|
99
|
+
return true;
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
test('anchor reads retain their original scope when caller input changes during the store read', async () => {
|
|
104
|
+
for (const [save, load, fields] of [
|
|
105
|
+
['saveDeviceEnrollmentAnchor', 'loadDeviceEnrollmentAnchor', { config }],
|
|
106
|
+
['saveEvidenceQuotaAnchor', 'loadEvidenceQuotaAnchor', { config, anchor: quota }],
|
|
107
|
+
]) {
|
|
108
|
+
const f = fixture();
|
|
109
|
+
await api[save]({ ...fields, store: f.store });
|
|
110
|
+
for (const account of f.values.keys())
|
|
111
|
+
f.values.set(account, 'private-malformed-anchor-canary');
|
|
112
|
+
const input = { ...fields, store: f.store, hostScope: f.scope };
|
|
113
|
+
f.after(() => { input.hostScope = undefined; });
|
|
114
|
+
await assert.rejects(api[load](input), error => {
|
|
115
|
+
assert.ok(error instanceof Error);
|
|
116
|
+
assert.doesNotMatch(error.message, /private-ma/);
|
|
117
|
+
return true;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
//# sourceMappingURL=hostProtectedHelpers.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hostProtect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K,CAAC,EAAE,KAAK,CAAC,EAAE;YAC9C,MAAM,CAAC,EAAE,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC;YAAC,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;YAAC,OAAO,IAAI,CAAC;QACnG,CAAC,CAAC,CAAC;IACL,CAAC;AACH,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hostScope.test.d.ts","sourceRoot":"","sources":["../src/hostScope.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { mkdtemp, readFile, rm } from 'node:fs/promises';
|
|
3
|
+
import { readFileSync, readdirSync } from 'node:fs';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { resolve } from 'node:path';
|
|
6
|
+
import test from 'node:test';
|
|
7
|
+
import { createServer } from 'node:http';
|
|
8
|
+
import { createHash, createPublicKey, verify } from 'node:crypto';
|
|
9
|
+
import { AgentFabricClient, loadOrCreateDeviceIdentity, saveDeviceConfig, saveDeviceEnrollmentAnchor, loadOrganizationApiToken } from './index.js';
|
|
10
|
+
import { HostOperationFence } from './hostOperationScope.js';
|
|
11
|
+
const unavailable = { message: 'relay_host_scope_unavailable' };
|
|
12
|
+
test('malformed supplied protected-store scopes never select legacy behavior', async () => {
|
|
13
|
+
const calls = [];
|
|
14
|
+
const store = { backend: 'linux-secret-service',
|
|
15
|
+
async get() { calls.push('get'); return null; }, async put() { calls.push('put'); }, async delete() { calls.push('delete'); } };
|
|
16
|
+
const throwing = (field) => Object.defineProperty({}, field, { get() { throw new Error('private-accessor-canary'); } });
|
|
17
|
+
for (const scope of [null, false, 0, '', {}, throwing('signal'),
|
|
18
|
+
Object.defineProperty({ signal: new AbortController().signal }, 'current', { get() { throw new Error('private-accessor-canary'); } })]) {
|
|
19
|
+
await assert.rejects(loadOrganizationApiToken({ hqUrl: 'https://hq.example', organizationId: 'org_demo', store,
|
|
20
|
+
hostScope: scope }), unavailable);
|
|
21
|
+
assert.deepEqual(calls, []);
|
|
22
|
+
}
|
|
23
|
+
await loadOrganizationApiToken({ hqUrl: 'https://hq.example', organizationId: 'org_demo', store });
|
|
24
|
+
assert.deepEqual(calls, ['get']);
|
|
25
|
+
});
|
|
26
|
+
test('host fence captures qualification fields once without using a supplied bind method', async () => {
|
|
27
|
+
let signals = 0, callbacks = 0, checks = 0;
|
|
28
|
+
const signal = new AbortController().signal;
|
|
29
|
+
const current = async () => { checks++; return true; };
|
|
30
|
+
Object.defineProperty(current, 'bind', { get() { throw new Error('private-bind-canary'); } });
|
|
31
|
+
const scope = { get signal() { signals++; return signal; }, get current() { callbacks++; return current; } };
|
|
32
|
+
const fence = new HostOperationFence(scope);
|
|
33
|
+
await fence.assert();
|
|
34
|
+
await fence.assert();
|
|
35
|
+
assert.equal(signals, 1);
|
|
36
|
+
assert.equal(callbacks, 1);
|
|
37
|
+
assert.equal(checks, 2);
|
|
38
|
+
});
|
|
39
|
+
async function fixture(t) {
|
|
40
|
+
const root = await mkdtemp(resolve(tmpdir(), 'fabric-host-scope-'));
|
|
41
|
+
t.after(() => rm(root, { recursive: true, force: true }));
|
|
42
|
+
const values = new Map(), storeCalls = [], requests = [];
|
|
43
|
+
let onRead = () => { };
|
|
44
|
+
const store = { backend: 'linux-secret-service',
|
|
45
|
+
async get(account) { storeCalls.push('get'); onRead(); return values.get(account) ?? null; },
|
|
46
|
+
async put(account, value) { storeCalls.push('put'); values.set(account, value); },
|
|
47
|
+
async delete(account) { storeCalls.push('delete'); values.delete(account); } };
|
|
48
|
+
const identity = await loadOrCreateDeviceIdentity({ hqUrl: 'https://hq.example', organizationId: 'org_demo', store });
|
|
49
|
+
const config = { schema: 'dharma.device-config/v1', hqUrl: 'https://hq.example',
|
|
50
|
+
organizationId: 'org_demo', deviceId: '11111111-1111-4111-8111-111111111111', deviceName: 'Synthetic',
|
|
51
|
+
platform: 'linux', publicKeyEd25519: identity.publicKeyEd25519, serverPublicKeyEd25519: identity.publicKeyEd25519,
|
|
52
|
+
relayUrl: 'wss://relay.example', enrolledAt: new Date().toISOString() };
|
|
53
|
+
const configPath = resolve(root, 'device.json'), statePath = resolve(root, 'state.json');
|
|
54
|
+
await saveDeviceConfig(configPath, config);
|
|
55
|
+
await saveDeviceEnrollmentAnchor({ config, store });
|
|
56
|
+
storeCalls.length = 0;
|
|
57
|
+
const controller = new AbortController();
|
|
58
|
+
let allowed = true;
|
|
59
|
+
const hostScope = { signal: controller.signal, current: async () => allowed };
|
|
60
|
+
const fetcher = async (url, options) => {
|
|
61
|
+
requests.push(new URL(String(url)).pathname);
|
|
62
|
+
return new Response(JSON.stringify({ ok: true }), { status: 201 });
|
|
63
|
+
};
|
|
64
|
+
const input = { configPath, statePath, store, fetcher, hostScope };
|
|
65
|
+
return { input, requests, storeCalls, controller, configPath, statePath, root, config,
|
|
66
|
+
isAllowed() { return allowed; },
|
|
67
|
+
dropIdentity() { values.delete(identity.account); },
|
|
68
|
+
setAllowed(value) { allowed = value; }, setRead(fn) { onRead = fn; } };
|
|
69
|
+
}
|
|
70
|
+
test('scoped client refuses absent authority before protected-store access', async (t) => {
|
|
71
|
+
const f = await fixture(t);
|
|
72
|
+
f.setAllowed(false);
|
|
73
|
+
await assert.rejects(AgentFabricClient.open(f.input), unavailable);
|
|
74
|
+
assert.deepEqual(f.storeCalls, []);
|
|
75
|
+
assert.deepEqual(f.requests, []);
|
|
76
|
+
});
|
|
77
|
+
test('scoped client refuses cancellation and private qualification exceptions', async (t) => {
|
|
78
|
+
const f = await fixture(t);
|
|
79
|
+
f.controller.abort('private-abort-canary');
|
|
80
|
+
await assert.rejects(AgentFabricClient.open(f.input), unavailable);
|
|
81
|
+
const input = { ...f.input, hostScope: { signal: new AbortController().signal,
|
|
82
|
+
current: async () => { throw new Error('private-qualification-canary'); } } };
|
|
83
|
+
await assert.rejects(AgentFabricClient.open(input), unavailable);
|
|
84
|
+
assert.deepEqual(f.storeCalls, []);
|
|
85
|
+
});
|
|
86
|
+
test('loss during a store read blocks subsequent identity reads and mutations', async (t) => {
|
|
87
|
+
const f = await fixture(t);
|
|
88
|
+
f.setRead(() => f.setAllowed(false));
|
|
89
|
+
await assert.rejects(AgentFabricClient.open(f.input), unavailable);
|
|
90
|
+
assert.deepEqual(f.storeCalls, ['get']);
|
|
91
|
+
assert.deepEqual(f.requests, []);
|
|
92
|
+
});
|
|
93
|
+
test('withdrawn clients neither mutate protocol state nor regain authority for another turn', async (t) => {
|
|
94
|
+
const f = await fixture(t);
|
|
95
|
+
const client = await AgentFabricClient.open(f.input);
|
|
96
|
+
await client.openSession();
|
|
97
|
+
const before = await readFile(f.statePath, 'utf8');
|
|
98
|
+
const count = f.requests.length;
|
|
99
|
+
f.setAllowed(false);
|
|
100
|
+
await assert.rejects(client.registerWorkspace({ workspaceId: 'synthetic' }), unavailable);
|
|
101
|
+
f.setAllowed(true);
|
|
102
|
+
await assert.rejects(client.registerWorkspace({ workspaceId: 'synthetic' }), unavailable);
|
|
103
|
+
assert.equal(await readFile(f.statePath, 'utf8'), before);
|
|
104
|
+
assert.equal(f.requests.length, count);
|
|
105
|
+
});
|
|
106
|
+
test('cancelled response remains unacknowledged and preserves the pending request', async (t) => {
|
|
107
|
+
const f = await fixture(t);
|
|
108
|
+
let requestSignal;
|
|
109
|
+
f.input.fetcher = async (url, options) => {
|
|
110
|
+
const path = new URL(String(url)).pathname;
|
|
111
|
+
f.requests.push(path);
|
|
112
|
+
if (path.endsWith('/workspaces')) {
|
|
113
|
+
requestSignal = options?.signal;
|
|
114
|
+
f.controller.abort();
|
|
115
|
+
}
|
|
116
|
+
return new Response(JSON.stringify({ ok: true, private: 'private-response-canary' }), { status: 201 });
|
|
117
|
+
};
|
|
118
|
+
const client = await AgentFabricClient.open(f.input);
|
|
119
|
+
await client.openSession();
|
|
120
|
+
await assert.rejects(client.registerWorkspace({ workspaceId: 'synthetic' }), unavailable);
|
|
121
|
+
assert.equal(requestSignal?.aborted, true);
|
|
122
|
+
const persisted = await readFile(f.statePath, 'utf8');
|
|
123
|
+
assert.match(JSON.parse(persisted).pending.pathname, /\/workspaces$/);
|
|
124
|
+
assert.doesNotMatch(persisted, /private-response-canary/);
|
|
125
|
+
});
|
|
126
|
+
test('queued requests cannot dispatch after the active request loses authority', async (t) => {
|
|
127
|
+
const f = await fixture(t);
|
|
128
|
+
let release, entered;
|
|
129
|
+
const blocked = new Promise(done => { release = done; }), observed = new Promise(done => { entered = done; });
|
|
130
|
+
f.input.fetcher = async (url) => {
|
|
131
|
+
const path = new URL(String(url)).pathname;
|
|
132
|
+
f.requests.push(path);
|
|
133
|
+
if (path.endsWith('/first')) {
|
|
134
|
+
entered();
|
|
135
|
+
await blocked;
|
|
136
|
+
}
|
|
137
|
+
return new Response(JSON.stringify({ ok: true }), { status: 201 });
|
|
138
|
+
};
|
|
139
|
+
const client = await AgentFabricClient.open(f.input);
|
|
140
|
+
await client.openSession();
|
|
141
|
+
const first = client.signedPost('/agent-fabric/first', {});
|
|
142
|
+
const second = client.signedPost('/agent-fabric/second', {});
|
|
143
|
+
const settled = Promise.allSettled([first, second]);
|
|
144
|
+
await observed;
|
|
145
|
+
f.controller.abort();
|
|
146
|
+
release();
|
|
147
|
+
const outcomes = await settled;
|
|
148
|
+
assert.deepEqual(outcomes.map(result => result.status), ['rejected', 'rejected']);
|
|
149
|
+
for (const result of outcomes)
|
|
150
|
+
if (result.status === 'rejected')
|
|
151
|
+
assert.equal(result.reason.message, unavailable.message);
|
|
152
|
+
assert.equal(f.requests.filter(path => path.endsWith('/second')).length, 0);
|
|
153
|
+
});
|
|
154
|
+
test('scoped client refuses a changed enrolled identity before signing or dispatch', async (t) => {
|
|
155
|
+
const f = await fixture(t);
|
|
156
|
+
const client = await AgentFabricClient.open(f.input);
|
|
157
|
+
await client.openSession();
|
|
158
|
+
const before = await readFile(f.statePath, 'utf8');
|
|
159
|
+
const count = f.requests.length;
|
|
160
|
+
client.config.organizationId = 'org_foreign';
|
|
161
|
+
await assert.rejects(client.registerWorkspace({ workspaceId: 'synthetic' }), unavailable);
|
|
162
|
+
assert.equal(f.requests.length, count);
|
|
163
|
+
assert.equal(await readFile(f.statePath, 'utf8'), before);
|
|
164
|
+
});
|
|
165
|
+
test('valid scoped client retains the ordinary signed protocol contract', async (t) => {
|
|
166
|
+
const f = await fixture(t);
|
|
167
|
+
let wire;
|
|
168
|
+
f.input.fetcher = async (url, options) => {
|
|
169
|
+
f.requests.push(new URL(String(url)).pathname);
|
|
170
|
+
wire = options;
|
|
171
|
+
return new Response(JSON.stringify({ ok: true }), { status: 201 });
|
|
172
|
+
};
|
|
173
|
+
const client = await AgentFabricClient.open(f.input);
|
|
174
|
+
await client.openSession();
|
|
175
|
+
assert.equal((await client.registerWorkspace({ workspaceId: 'synthetic' })).ok, true);
|
|
176
|
+
assert.equal(wire?.redirect, 'error');
|
|
177
|
+
assert.equal(wire?.signal?.aborted, false);
|
|
178
|
+
const state = JSON.parse(await readFile(f.statePath, 'utf8'));
|
|
179
|
+
assert.equal(state.pending, null);
|
|
180
|
+
assert.equal(state.nextSequence, 3);
|
|
181
|
+
});
|
|
182
|
+
test('withdrawal during a temporary state write prevents publication and network admission', async (t) => {
|
|
183
|
+
const f = await fixture(t);
|
|
184
|
+
f.input.hostScope.current = async () => {
|
|
185
|
+
for (const name of readdirSync(f.root).filter(name => name.endsWith('.tmp'))) {
|
|
186
|
+
if (readFileSync(resolve(f.root, name), 'utf8').includes('during-scoped-persist'))
|
|
187
|
+
f.setAllowed(false);
|
|
188
|
+
}
|
|
189
|
+
return f.isAllowed();
|
|
190
|
+
};
|
|
191
|
+
const client = await AgentFabricClient.open(f.input);
|
|
192
|
+
await client.openSession();
|
|
193
|
+
const before = await readFile(f.statePath, 'utf8');
|
|
194
|
+
const count = f.requests.length;
|
|
195
|
+
await assert.rejects(client.signedPost('/agent-fabric/guard-state', { operation: 'during-scoped-persist' }), unavailable);
|
|
196
|
+
assert.equal(await readFile(f.statePath, 'utf8'), before);
|
|
197
|
+
assert.equal(f.requests.length, count);
|
|
198
|
+
assert.equal(readdirSync(f.root).filter(name => name.endsWith('.tmp')).length, 0);
|
|
199
|
+
});
|
|
200
|
+
test('public device-config commit can carry the same current host authority', async (t) => {
|
|
201
|
+
const f = await fixture(t);
|
|
202
|
+
const before = await readFile(f.configPath, 'utf8');
|
|
203
|
+
f.setAllowed(false);
|
|
204
|
+
const save = saveDeviceConfig;
|
|
205
|
+
await assert.rejects(save(f.configPath, { ...f.config, deviceName: 'Unapproved' }, f.input.hostScope), unavailable);
|
|
206
|
+
assert.equal(await readFile(f.configPath, 'utf8'), before);
|
|
207
|
+
});
|
|
208
|
+
test('a cancelled fetch error cannot disclose its private transport diagnostics', async (t) => {
|
|
209
|
+
const f = await fixture(t);
|
|
210
|
+
f.input.fetcher = async (url) => {
|
|
211
|
+
if (new URL(String(url)).pathname.endsWith('/workspaces')) {
|
|
212
|
+
f.controller.abort();
|
|
213
|
+
throw new Error('private-transport-canary');
|
|
214
|
+
}
|
|
215
|
+
return new Response(JSON.stringify({ ok: true }), { status: 201 });
|
|
216
|
+
};
|
|
217
|
+
const client = await AgentFabricClient.open(f.input);
|
|
218
|
+
await client.openSession();
|
|
219
|
+
await assert.rejects(client.registerWorkspace({ workspaceId: 'synthetic' }), unavailable);
|
|
220
|
+
});
|
|
221
|
+
test('foreign direct responses are rejected before consuming their body', async (t) => {
|
|
222
|
+
const f = await fixture(t);
|
|
223
|
+
let parsed = 0;
|
|
224
|
+
f.input.fetcher = async (url) => {
|
|
225
|
+
const response = new Response(JSON.stringify({ ok: true }), { status: 201 });
|
|
226
|
+
if (new URL(String(url)).pathname.endsWith('/workspaces')) {
|
|
227
|
+
Object.defineProperty(response, 'redirected', { value: true });
|
|
228
|
+
Object.defineProperty(response, 'json', { value: async () => { parsed++; throw new Error('private-foreign-body-canary'); } });
|
|
229
|
+
}
|
|
230
|
+
return response;
|
|
231
|
+
};
|
|
232
|
+
const client = await AgentFabricClient.open(f.input);
|
|
233
|
+
await client.openSession();
|
|
234
|
+
await assert.rejects(client.registerWorkspace({ workspaceId: 'synthetic' }), { message: 'relay_host_transport_response_invalid' });
|
|
235
|
+
assert.equal(parsed, 0);
|
|
236
|
+
});
|
|
237
|
+
for (const phase of ['before_open', 'after_send'])
|
|
238
|
+
test(`WebSocket withdrawal ${phase} stops the owned connection`, async (t) => {
|
|
239
|
+
const f = await fixture(t);
|
|
240
|
+
delete f.input.fetcher;
|
|
241
|
+
const original = Object.getOwnPropertyDescriptor(globalThis, 'WebSocket');
|
|
242
|
+
let sent = 0, closed = 0;
|
|
243
|
+
class Socket extends EventTarget {
|
|
244
|
+
static CLOSING = 2;
|
|
245
|
+
static CLOSED = 3;
|
|
246
|
+
readyState = 0;
|
|
247
|
+
constructor(_url) {
|
|
248
|
+
super();
|
|
249
|
+
queueMicrotask(() => {
|
|
250
|
+
if (phase === 'before_open')
|
|
251
|
+
f.setAllowed(false);
|
|
252
|
+
this.readyState = 1;
|
|
253
|
+
this.dispatchEvent(new Event('open'));
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
send(frame) {
|
|
257
|
+
sent++;
|
|
258
|
+
f.controller.abort();
|
|
259
|
+
const request = JSON.parse(frame);
|
|
260
|
+
queueMicrotask(() => this.dispatchEvent(new MessageEvent('message', { data: JSON.stringify({
|
|
261
|
+
requestId: request.requestId, status: 201, body: JSON.stringify({ ok: true }),
|
|
262
|
+
}) })));
|
|
263
|
+
}
|
|
264
|
+
close() { closed++; this.readyState = Socket.CLOSED; this.dispatchEvent(new Event('close')); }
|
|
265
|
+
}
|
|
266
|
+
Object.defineProperty(globalThis, 'WebSocket', { ...original, value: Socket });
|
|
267
|
+
t.after(async () => { Object.defineProperty(globalThis, 'WebSocket', original); });
|
|
268
|
+
const client = await AgentFabricClient.open(f.input);
|
|
269
|
+
await assert.rejects(client.openSession(), unavailable);
|
|
270
|
+
assert.equal(sent, phase === 'before_open' ? 0 : 1);
|
|
271
|
+
assert.equal(closed, 1);
|
|
272
|
+
});
|
|
273
|
+
test('withdrawal while reading a direct response cancels its owned stream', async (t) => {
|
|
274
|
+
const f = await fixture(t);
|
|
275
|
+
let cancelled = false, pulled = 0, closing = false;
|
|
276
|
+
let fallback;
|
|
277
|
+
t.after(async () => { if (fallback)
|
|
278
|
+
clearTimeout(fallback); });
|
|
279
|
+
f.input.fetcher = async (url) => {
|
|
280
|
+
if (!new URL(String(url)).pathname.endsWith('/workspaces'))
|
|
281
|
+
return new Response(JSON.stringify({ ok: true }), { status: 201 });
|
|
282
|
+
const stream = new ReadableStream({ pull(controller) {
|
|
283
|
+
if (++pulled === 1)
|
|
284
|
+
controller.enqueue(Buffer.from('{"ok":'));
|
|
285
|
+
else if (!closing) {
|
|
286
|
+
closing = true;
|
|
287
|
+
fallback = setTimeout(() => {
|
|
288
|
+
if (!cancelled) {
|
|
289
|
+
controller.enqueue(Buffer.from('true}'));
|
|
290
|
+
controller.close();
|
|
291
|
+
}
|
|
292
|
+
}, 25);
|
|
293
|
+
f.controller.abort();
|
|
294
|
+
}
|
|
295
|
+
}, cancel() { cancelled = true; if (fallback)
|
|
296
|
+
clearTimeout(fallback); } });
|
|
297
|
+
return new Response(stream, { status: 201 });
|
|
298
|
+
};
|
|
299
|
+
const client = await AgentFabricClient.open(f.input);
|
|
300
|
+
await client.openSession();
|
|
301
|
+
await assert.rejects(client.registerWorkspace({ workspaceId: 'synthetic' }), unavailable);
|
|
302
|
+
assert.equal(cancelled, true);
|
|
303
|
+
});
|
|
304
|
+
test('oversized scoped direct responses are rejected before parsing', async (t) => {
|
|
305
|
+
const f = await fixture(t);
|
|
306
|
+
let parsed = 0;
|
|
307
|
+
f.input.fetcher = async (url) => {
|
|
308
|
+
const response = new Response(JSON.stringify({ ok: true }), { status: 201,
|
|
309
|
+
headers: new URL(String(url)).pathname.endsWith('/workspaces') ? { 'content-length': '5000001' } : {} });
|
|
310
|
+
if (new URL(String(url)).pathname.endsWith('/workspaces'))
|
|
311
|
+
Object.defineProperty(response, 'json', {
|
|
312
|
+
value: async () => { parsed++; return { ok: true }; }
|
|
313
|
+
});
|
|
314
|
+
return response;
|
|
315
|
+
};
|
|
316
|
+
const client = await AgentFabricClient.open(f.input);
|
|
317
|
+
await client.openSession();
|
|
318
|
+
await assert.rejects(client.registerWorkspace({ workspaceId: 'synthetic' }), { message: 'relay_host_transport_response_invalid' });
|
|
319
|
+
assert.equal(parsed, 0);
|
|
320
|
+
});
|
|
321
|
+
test('post-enrollment scoped opening never generates a replacement for a missing accepted key', async (t) => {
|
|
322
|
+
const f = await fixture(t);
|
|
323
|
+
f.dropIdentity();
|
|
324
|
+
await assert.rejects(AgentFabricClient.open(f.input), { message: 'relay_host_device_identity_unavailable' });
|
|
325
|
+
assert.equal(f.storeCalls.includes('put'), false);
|
|
326
|
+
assert.deepEqual(f.requests, []);
|
|
327
|
+
});
|
|
328
|
+
test('real HTTP delivery with withdrawn authority stays pending rather than inventing an acknowledgement', async (t) => {
|
|
329
|
+
const f = await fixture(t);
|
|
330
|
+
const deliveries = [];
|
|
331
|
+
let publicKey;
|
|
332
|
+
const server = createServer(async (request, response) => {
|
|
333
|
+
const chunks = [];
|
|
334
|
+
for await (const chunk of request)
|
|
335
|
+
chunks.push(Buffer.from(chunk));
|
|
336
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
337
|
+
const payload = Buffer.from(JSON.stringify({ bodyHash: `sha256:${createHash('sha256').update(text).digest('hex')}`,
|
|
338
|
+
deviceId: request.headers['x-dharma-device-id'], messageId: request.headers['x-dharma-message-id'],
|
|
339
|
+
method: request.method, nonce: request.headers['x-dharma-nonce'], organizationId: 'org_demo', pathname: request.url,
|
|
340
|
+
sequence: Number(request.headers['x-dharma-sequence']), sessionId: request.headers['x-dharma-session-id'],
|
|
341
|
+
timestamp: request.headers['x-dharma-timestamp'] }));
|
|
342
|
+
const checked = verify(null, payload, publicKey, Buffer.from(String(request.headers['x-dharma-signature']), 'base64url'));
|
|
343
|
+
deliveries.push({ path: request.url, signatureVerified: checked });
|
|
344
|
+
if (request.url.endsWith('/workspaces'))
|
|
345
|
+
f.setAllowed(false);
|
|
346
|
+
response.writeHead(201, { 'content-type': 'application/json' });
|
|
347
|
+
response.end(JSON.stringify({ ok: true }));
|
|
348
|
+
});
|
|
349
|
+
await new Promise(done => server.listen(0, '127.0.0.1', done));
|
|
350
|
+
t.after(() => new Promise((done, reject) => {
|
|
351
|
+
server.close(error => error ? reject(error) : done());
|
|
352
|
+
server.closeAllConnections();
|
|
353
|
+
}));
|
|
354
|
+
const address = server.address();
|
|
355
|
+
assert.ok(address && typeof address === 'object');
|
|
356
|
+
const origin = `http://127.0.0.1:${address.port}`;
|
|
357
|
+
const identity = await loadOrCreateDeviceIdentity({ hqUrl: origin, organizationId: 'org_demo', store: f.input.store });
|
|
358
|
+
publicKey = createPublicKey({ key: { kty: 'OKP', crv: 'Ed25519', x: identity.publicKeyEd25519 }, format: 'jwk' });
|
|
359
|
+
const config = { ...f.config, hqUrl: origin, publicKeyEd25519: identity.publicKeyEd25519,
|
|
360
|
+
serverPublicKeyEd25519: identity.publicKeyEd25519 };
|
|
361
|
+
await saveDeviceConfig(f.configPath, config);
|
|
362
|
+
await saveDeviceEnrollmentAnchor({ config, store: f.input.store });
|
|
363
|
+
const client = await AgentFabricClient.open({ ...f.input, fetcher: fetch });
|
|
364
|
+
await client.openSession();
|
|
365
|
+
await assert.rejects(client.registerWorkspace({ workspaceId: 'synthetic' }), unavailable);
|
|
366
|
+
assert.equal(deliveries.length, 2);
|
|
367
|
+
assert.equal(deliveries.every(value => value.signatureVerified), true);
|
|
368
|
+
assert.match(JSON.parse(await readFile(f.statePath, 'utf8')).pending.pathname, /\/workspaces$/);
|
|
369
|
+
await assert.rejects(client.registerWorkspace({ workspaceId: 'not-dispatched' }), unavailable);
|
|
370
|
+
assert.equal(deliveries.length, 2);
|
|
371
|
+
});
|
|
372
|
+
//# sourceMappingURL=hostScope.test.js.map
|