@ours.network/tg-connector 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +64 -2
- package/dist/cli.js +35 -2
- package/dist/connector.js +106 -8
- package/package.json +3 -2
- package/dist/mufl_code/core/CLUSTER_API.md +0 -640
- package/dist/mufl_code/core/CLUSTER_CONTRACT.md +0 -121
- package/dist/mufl_code/core/CLUSTER_INTRODUCTION_PLAN.md +0 -200
- package/dist/mufl_code/core/CONFIGURATION_DESIGN.md +0 -35
- package/dist/mufl_code/core/CONFIGURATION_PLAN.md +0 -394
- package/dist/mufl_code/core/DEVELOPMENT.md +0 -168
- package/dist/mufl_code/core/EPHEMERAL_INVITE_PLAN.md +0 -465
- package/dist/mufl_code/core/GROUP_CHAT_DESIGN.md +0 -520
- package/dist/mufl_code/core/LICENSE +0 -98
- package/dist/mufl_code/core/README.md +0 -76
- package/dist/mufl_code/core/a2a_capabilities.mm +0 -350
- package/dist/mufl_code/core/a2a_cluster.mm +0 -601
- package/dist/mufl_code/core/a2a_control.mm +0 -89
- package/dist/mufl_code/core/a2a_messaging.mm +0 -2103
- package/dist/mufl_code/core/a2a_monitoring.mm +0 -94
- package/dist/mufl_code/core/a2a_protocol.mm +0 -231
- package/dist/mufl_code/core/config.mufl +0 -26
- package/dist/mufl_code/core/release-notes/2.0.md +0 -268
- package/dist/mufl_code/core/release-notes/2.1.md +0 -92
- package/dist/mufl_code/core/release-notes/2.2.md +0 -94
- package/dist/mufl_code/core/release-notes/3.0.md +0 -162
- package/dist/mufl_code/core/release-notes/3.1.md +0 -43
- package/dist/mufl_code/core/release-notes/README.md +0 -32
- package/dist/mufl_code/core/tests/README.md +0 -66
- package/dist/mufl_code/core/tests/run.sh +0 -67
- package/dist/mufl_code/core/tests/test.mjs +0 -467
- package/dist/mufl_code/core/tests/test_actor.mu +0 -287
- package/dist/mufl_code/core/version.mm +0 -24
|
@@ -1,467 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// M2 loopback test suite for the core-3.0 ephemeral-invite redeem flow.
|
|
3
|
-
// Asserts RECEIVER-SIDE outcomes (state), per the Critic requirement.
|
|
4
|
-
import { resolve } from 'node:path';
|
|
5
|
-
import * as fs from 'node:fs';
|
|
6
|
-
import { adapt_wrapper } from '@adapt-toolkit/sdk/executables';
|
|
7
|
-
import { PacketWrapperConfigurator } from '@adapt-toolkit/sdk/wrappers';
|
|
8
|
-
import { object_to_adapt_value } from '@adapt-toolkit/sdk/wrapper';
|
|
9
|
-
|
|
10
|
-
const BROKER_URL = process.env.BROKER_URL || 'ws://127.0.0.1:9790';
|
|
11
|
-
const UNIT_DIR = resolve('.');
|
|
12
|
-
const unitHash = fs.readdirSync(UNIT_DIR).find((f) => f.endsWith('.muflo')).slice(0, -'.muflo'.length);
|
|
13
|
-
const UNIT = new Uint8Array(fs.readFileSync(resolve(UNIT_DIR, `${unitHash}.muflo`)));
|
|
14
|
-
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
15
|
-
const log = (...a) => process.stderr.write(`[t] ${a.join(' ')}\n`);
|
|
16
|
-
const scorecard = [];
|
|
17
|
-
let CUR = '';
|
|
18
|
-
const ok = (c, m) => { if (!c) { scorecard.push(`✗ [${CUR}] ${m}`); console.log(` ✗ FAIL: ${m}`); } else { console.log(` ✓ ${m}`); } };
|
|
19
|
-
const isT = (s) => /true/i.test(String(s));
|
|
20
|
-
|
|
21
|
-
let wrapper;
|
|
22
|
-
function mk(name) { return { name, pw: null, cid: '', pending: [], rejects: [], events: [] }; }
|
|
23
|
-
function wire(id) {
|
|
24
|
-
id.pw.on_return_data = (d) => {
|
|
25
|
-
const kind = d.Reduce('kind').Visualize();
|
|
26
|
-
if (kind === 'notify_agent') { id.events.push(d.Reduce('payload').Reduce('event').Visualize()); return; }
|
|
27
|
-
if (kind === 'save_state') return;
|
|
28
|
-
const p = id.pending.shift(); if (!p) return;
|
|
29
|
-
clearTimeout(p.timer); p.resolve(d.Reduce('payload'));
|
|
30
|
-
};
|
|
31
|
-
id.pw.on_transaction_failure = (msg) => {
|
|
32
|
-
const p = id.pending.shift();
|
|
33
|
-
if (p) { clearTimeout(p.timer); p.reject(new Error(msg)); }
|
|
34
|
-
else { id.rejects.push(String(msg)); log(`${id.name} inbound rejected:`, String(msg).split('\n')[0]); }
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
function mutate(id, name, targ) {
|
|
38
|
-
return new Promise((res, rej) => {
|
|
39
|
-
const timer = setTimeout(() => rej(new Error(`${id.name}.${name} timed out`)), 20000);
|
|
40
|
-
id.pending.push({ resolve: res, reject: rej, timer });
|
|
41
|
-
id.pw.add_client_message(object_to_adapt_value({ name, targ }));
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
const ro = (id, name, targ) => id.pw.packet.ExecuteTransaction(object_to_adapt_value({ name, targ }));
|
|
45
|
-
const binv = (id, buf) => id.pw.packet.NewBinaryFromBuffer(Buffer.from(buf));
|
|
46
|
-
async function mkPacket(id, seed) {
|
|
47
|
-
const cfg = new PacketWrapperConfigurator();
|
|
48
|
-
cfg.process_arguments(['--unit_hash', unitHash, '--seed_phrase', seed, '--unit_dir_path', UNIT_DIR]);
|
|
49
|
-
await new Promise((res, rej) => {
|
|
50
|
-
const t = setTimeout(() => rej(new Error(`${id.name} create timeout`)), 30000);
|
|
51
|
-
wrapper.packet_manager.create_packet(cfg, (pw) => {
|
|
52
|
-
clearTimeout(t); id.pw = pw; id.cid = pw.packet.GetContainerID().Visualize(); wire(id);
|
|
53
|
-
log(`${id.name} cid ${id.cid.slice(0, 12)}…`); res();
|
|
54
|
-
}, UNIT);
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
const st = (id) => { const s = ro(id, '::actor::qa_state', undefined); return {
|
|
58
|
-
c: +s.Reduce('n_contacts').Visualize(), p: +s.Reduce('n_peer_ads').Visualize(),
|
|
59
|
-
pi: +s.Reduce('n_pending_invites').Visualize(), pr: +s.Reduce('n_pending_redemptions').Visualize(),
|
|
60
|
-
cr: +s.Reduce('n_contact_roots').Visualize(),
|
|
61
|
-
prs: +s.Reduce('n_pending_restores').Visualize(), rr: +s.Reduce('n_restore_replies').Visualize(),
|
|
62
|
-
dq: +s.Reduce('n_deferred').Visualize() }; };
|
|
63
|
-
const lc = (id) => ro(id, '::a2a_messaging::list_contacts', undefined).Visualize();
|
|
64
|
-
const adBlob = (id) => Buffer.from(ro(id, '::actor::qa_export_ad', undefined).Reduce('ad').GetBinary());
|
|
65
|
-
const setName = (id, n) => mutate(id, '::a2a_messaging::set_my_name', { name: n });
|
|
66
|
-
|
|
67
|
-
async function delegate(root, role, roleName) {
|
|
68
|
-
const roleAd = Buffer.from(ro(role, '::actor::export_address_document', undefined).GetBinary());
|
|
69
|
-
const signed = await mutate(root, '::actor::sign_delegation', { role_ad: binv(root, roleAd), role_id: roleName });
|
|
70
|
-
const cert = Buffer.from(signed.Reduce('cert').GetBinary());
|
|
71
|
-
const prof = await mutate(root, '::actor::export_root_profile', {});
|
|
72
|
-
const profBlob = Buffer.from(prof.Reduce('profile').GetBinary());
|
|
73
|
-
const rootAd = Buffer.from(ro(root, '::actor::export_address_document', undefined).GetBinary());
|
|
74
|
-
await mutate(role, '::actor::set_delegation', { cert: binv(role, cert), root_ad: binv(role, rootAd), root_profile: binv(role, profBlob) });
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async function main() {
|
|
78
|
-
wrapper = await adapt_wrapper.start(['--broker_address', BROKER_URL, '--test_mode',
|
|
79
|
-
'--logger_config', '--level', 'WARNING', '--stdout', 'stderr', '--logger_config_end']);
|
|
80
|
-
wrapper.start();
|
|
81
|
-
await sleep(1500);
|
|
82
|
-
|
|
83
|
-
const I = mk('I'); const R = mk('R'); const F = mk('F');
|
|
84
|
-
await mkPacket(I, 'eph-t-I-01'); await mkPacket(R, 'eph-t-R-02'); await mkPacket(F, 'eph-t-F-03');
|
|
85
|
-
await sleep(1200);
|
|
86
|
-
await setName(I, 'Inviter'); await setName(R, 'Responder'); await setName(F, 'Foreign');
|
|
87
|
-
|
|
88
|
-
// ---------- T1 happy-flat ----------
|
|
89
|
-
CUR = 'T1 happy-flat';
|
|
90
|
-
console.log('\n=== T1 happy-flat ===');
|
|
91
|
-
let m = await mutate(I, '::a2a_messaging::generate_invite', { name: 'TheResponder' });
|
|
92
|
-
const blob1 = Buffer.from(m.Reduce('invite').GetBinary());
|
|
93
|
-
ok(st(I).pi === 1, `inviter has 1 pending_invite after mint`);
|
|
94
|
-
await mutate(R, '::a2a_messaging::add_contact', { invite: binv(R, blob1), name: 'MyInviter' });
|
|
95
|
-
await sleep(5000);
|
|
96
|
-
const i1 = st(I); const r1 = st(R);
|
|
97
|
-
log(`I=${JSON.stringify(i1)} R=${JSON.stringify(r1)}`);
|
|
98
|
-
ok(i1.pi === 0, `leg-2: inviter consumed pending_invites (${i1.pi})`);
|
|
99
|
-
ok(i1.c >= 1 && i1.p >= 1, `leg-2: inviter registered responder (c=${i1.c},peer_ads=${i1.p})`);
|
|
100
|
-
ok(r1.pr === 0, `leg-3: responder cleared pending_redemptions (${r1.pr})`);
|
|
101
|
-
ok(r1.c >= 1 && r1.p >= 1, `leg-3 RECEIVER-SIDE: responder DECRYPTED + registered inviter (c=${r1.c},peer_ads=${r1.p})`);
|
|
102
|
-
ok(new RegExp(I.cid).test(lc(R)), `responder list_contacts includes inviter cid`);
|
|
103
|
-
ok(new RegExp(R.cid).test(lc(I)), `inviter list_contacts includes responder cid`);
|
|
104
|
-
// bidirectional encrypted_channel round-trip
|
|
105
|
-
const smIR = await mutate(I, '::a2a_messaging::send_message', { contact: R.cid, text: 'msg-I-to-R' });
|
|
106
|
-
const msgWireIR = smIR.Reduce('wire_id').Visualize();
|
|
107
|
-
await sleep(2500);
|
|
108
|
-
ok(/msg-I-to-R/.test(ro(R, '::actor::list_incoming_messages', undefined).Visualize()), `send_message I→R round-trips over encrypted_channel`);
|
|
109
|
-
await mutate(R, '::a2a_messaging::send_message', { contact: I.cid, text: 'msg-R-to-I' });
|
|
110
|
-
await sleep(2500);
|
|
111
|
-
ok(/msg-R-to-I/.test(ro(I, '::actor::list_incoming_messages', undefined).Visualize()), `send_message R→I round-trips over encrypted_channel`);
|
|
112
|
-
|
|
113
|
-
// ---------- T1 file round-trip (send_file both directions) ----------
|
|
114
|
-
const sfIR = await mutate(I, '::a2a_messaging::send_file',
|
|
115
|
-
{ contact: R.cid, filename: 'hello.png', mime: 'image/png', data: binv(I, Buffer.from('\x89PNG\r\n\x1a\nDATA')) });
|
|
116
|
-
const fileWireIR = sfIR.Reduce('wire_id').Visualize();
|
|
117
|
-
await sleep(2500);
|
|
118
|
-
{
|
|
119
|
-
const rf = ro(R, '::actor::list_incoming_files', undefined).Visualize();
|
|
120
|
-
ok(/hello\.png/.test(rf), `send_file I→R: responder received file hello.png`);
|
|
121
|
-
ok(/image\/png/.test(rf), `send_file I→R: responder sees mime image/png`);
|
|
122
|
-
ok(new RegExp(I.cid).test(rf), `send_file I→R: file is attributed to the inviter cid`);
|
|
123
|
-
ok(new RegExp(fileWireIR).test(rf), `send_file I→R: stored wire_id matches the send return`);
|
|
124
|
-
}
|
|
125
|
-
await mutate(R, '::a2a_messaging::send_file',
|
|
126
|
-
{ contact: I.cid, filename: 'reply.txt', mime: 'text/plain', data: binv(R, Buffer.from('hi')) });
|
|
127
|
-
await sleep(2500);
|
|
128
|
-
ok(/reply\.txt/.test(ro(I, '::actor::list_incoming_files', undefined).Visualize()),
|
|
129
|
-
`send_file R→I: inviter received file reply.txt`);
|
|
130
|
-
|
|
131
|
-
// ---------- T1 file monitoring summary (metadata-only; never the bytes) ----------
|
|
132
|
-
{
|
|
133
|
-
const r = await mutate(I, '::actor::qa_file_summary',
|
|
134
|
-
{ filename: 'hello.png', mime: 'image/png', data: binv(I, Buffer.from('ABCDE')) });
|
|
135
|
-
const summary = r.Reduce('summary').Visualize();
|
|
136
|
-
ok(summary === '[file] hello.png (image/png, 5 B)', `file monitor summary is metadata-only: "${summary}"`);
|
|
137
|
-
ok(!/ABCDE/.test(summary), `file monitor summary never carries the file bytes`);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// ---------- T1 cross-kind reply (one shared wire_id namespace) ----------
|
|
141
|
-
// A MESSAGE replying to a FILE: reply_to points at the file's wire_id (from I→R).
|
|
142
|
-
await mutate(R, '::a2a_messaging::send_message',
|
|
143
|
-
{ contact: I.cid, text: 'reply-to-file', reply_to: { wire_id: fileWireIR } });
|
|
144
|
-
await sleep(2500);
|
|
145
|
-
{
|
|
146
|
-
const im = ro(I, '::actor::list_incoming_messages', undefined).Visualize();
|
|
147
|
-
ok(/reply-to-file/.test(im) && new RegExp(fileWireIR).test(im),
|
|
148
|
-
`a send_message can reply_to a FILE's wire_id (cross-kind, shared namespace)`);
|
|
149
|
-
}
|
|
150
|
-
// A FILE replying to a MESSAGE: reply_to points at the message's wire_id (from I→R).
|
|
151
|
-
await mutate(R, '::a2a_messaging::send_file',
|
|
152
|
-
{ contact: I.cid, filename: 'answer.bin', mime: 'application/octet-stream',
|
|
153
|
-
data: binv(R, Buffer.from('Z')), reply_to: { wire_id: msgWireIR } });
|
|
154
|
-
await sleep(2500);
|
|
155
|
-
{
|
|
156
|
-
const ifs = ro(I, '::actor::list_incoming_files', undefined).Visualize();
|
|
157
|
-
ok(/answer\.bin/.test(ifs) && new RegExp(msgWireIR).test(ifs),
|
|
158
|
-
`a send_file can reply_to a MESSAGE's wire_id (cross-kind, shared namespace)`);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// ---------- T3 single-use (reuse blob1, already consumed) ----------
|
|
162
|
-
CUR = 'T3 single-use';
|
|
163
|
-
console.log('\n=== T3 single-use ===');
|
|
164
|
-
const before = st(I); const rej0 = I.rejects.length;
|
|
165
|
-
await mutate(R, '::a2a_messaging::add_contact', { invite: binv(R, blob1), name: 'again' });
|
|
166
|
-
await sleep(3000);
|
|
167
|
-
const after = st(I);
|
|
168
|
-
ok(I.rejects.slice(rej0).some((x) => /already-redeemed|Unknown or already/.test(x)), `2nd leg-1 for the same invite_id aborts "already-redeemed" at inviter`);
|
|
169
|
-
ok(after.c === before.c && after.pi === before.pi, `single-use: inviter state unchanged by replay (c=${after.c},pi=${after.pi})`);
|
|
170
|
-
|
|
171
|
-
// ---------- T4 invalid-then-valid (bad box must NOT consume) ----------
|
|
172
|
-
CUR = 'T4 invalid-then-valid';
|
|
173
|
-
console.log('\n=== T4 invalid-then-valid ===');
|
|
174
|
-
m = await mutate(I, '::a2a_messaging::generate_invite', { name: 'P4' });
|
|
175
|
-
const blob4 = Buffer.from(m.Reduce('invite').GetBinary());
|
|
176
|
-
const piAfterMint = st(I).pi; const rejb = I.rejects.length;
|
|
177
|
-
await mutate(R, '::actor::qa_leg1_badbox', { invite: binv(R, blob4) });
|
|
178
|
-
await sleep(3000);
|
|
179
|
-
ok(I.rejects.slice(rejb).some((x) => /Decryption failed|crypto box|read/i.test(x)), `bad box: inviter leg-2 aborts at decrypt`);
|
|
180
|
-
ok(st(I).pi === piAfterMint, `bad box did NOT consume the invite (pi=${st(I).pi}, was ${piAfterMint})`);
|
|
181
|
-
// now redeem the SAME invite validly
|
|
182
|
-
await mutate(R, '::a2a_messaging::add_contact', { invite: binv(R, blob4), name: 'P4ok' });
|
|
183
|
-
await sleep(5000);
|
|
184
|
-
ok(st(I).pi === piAfterMint - 1, `valid redeem after the bad box consumed the invite (pi=${st(I).pi})`);
|
|
185
|
-
|
|
186
|
-
// ---------- T5 tamper (state-unchanged form) ----------
|
|
187
|
-
CUR = 'T5 tamper';
|
|
188
|
-
console.log('\n=== T5 tamper ===');
|
|
189
|
-
m = await mutate(I, '::a2a_messaging::generate_invite', { name: 'P5' });
|
|
190
|
-
const blob5 = Buffer.from(m.Reduce('invite').GetBinary());
|
|
191
|
-
const s5 = st(I); const rej5 = I.rejects.length;
|
|
192
|
-
await mutate(R, '::actor::qa_leg1_badbox', { invite: binv(R, blob5) });
|
|
193
|
-
await sleep(3000);
|
|
194
|
-
ok(I.rejects.slice(rej5).some((x) => /Decryption failed|crypto box|read/i.test(x)), `tampered box aborts at inviter decrypt`);
|
|
195
|
-
const s5b = st(I);
|
|
196
|
-
ok(s5b.c === s5.c && s5b.p === s5.p && s5b.pi === s5.pi, `tamper mutated NO state (contacts/peer_ads/pending unchanged)`);
|
|
197
|
-
|
|
198
|
-
// ---------- T6 cid-bind leg-2 (foreign AD) ----------
|
|
199
|
-
CUR = 'T6 cid-bind-leg2';
|
|
200
|
-
console.log('\n=== T6 cid-bind leg-2 ===');
|
|
201
|
-
m = await mutate(I, '::a2a_messaging::generate_invite', { name: 'P6' });
|
|
202
|
-
const blob6 = Buffer.from(m.Reduce('invite').GetBinary());
|
|
203
|
-
const s6 = st(I); const rej6 = I.rejects.length;
|
|
204
|
-
await mutate(R, '::actor::qa_leg1_foreign_ad', { invite: binv(R, blob6), foreign_ad: binv(R, adBlob(F)) });
|
|
205
|
-
await sleep(3000);
|
|
206
|
-
ok(I.rejects.slice(rej6).some((x) => /does not belong to the sender/.test(x)), `leg-2 cid-bind: foreign AD (cid≠sender) aborts`);
|
|
207
|
-
ok(st(I).pi === s6.pi && st(I).c === s6.c, `leg-2 cid-bind failure consumed/registered nothing`);
|
|
208
|
-
|
|
209
|
-
// ---------- T7 PoP leg-2 (forged AD) ----------
|
|
210
|
-
CUR = 'T7 PoP-leg2';
|
|
211
|
-
console.log('\n=== T7 PoP leg-2 ===');
|
|
212
|
-
m = await mutate(I, '::a2a_messaging::generate_invite', { name: 'P7' });
|
|
213
|
-
const blob7 = Buffer.from(m.Reduce('invite').GetBinary());
|
|
214
|
-
const s7 = st(I); const rej7 = I.rejects.length;
|
|
215
|
-
await mutate(R, '::actor::qa_leg1_forged_ad', { invite: binv(R, blob7) });
|
|
216
|
-
await sleep(3000);
|
|
217
|
-
ok(I.rejects.slice(rej7).some((x) => /authoriz|signature|process|key/i.test(x)), `leg-2 PoP: forged AD (stripped self-sig) aborts in process_address_document`);
|
|
218
|
-
ok(st(I).pi === s7.pi && st(I).c === s7.c, `leg-2 PoP failure consumed/registered nothing`);
|
|
219
|
-
|
|
220
|
-
// ---------- T8 leg-3 gates (unexpected-inviter, cid-bind, PoP) ----------
|
|
221
|
-
CUR = 'T8 leg3-gates';
|
|
222
|
-
console.log('\n=== T8 leg-3 gates ===');
|
|
223
|
-
// helper: give R a LIVE pending redemption pinned to I.cid (a fake invite I never minted)
|
|
224
|
-
async function liveRedemption() {
|
|
225
|
-
const fk = await mutate(I, '::actor::qa_mint_fake_invite', { inviter_cid: I.cid });
|
|
226
|
-
const fblob = Buffer.from(fk.Reduce('blob').GetBinary());
|
|
227
|
-
const fid = fk.Reduce('invite_id').Visualize();
|
|
228
|
-
const ac = await mutate(R, '::a2a_messaging::add_contact', { invite: binv(R, fblob), name: 'leg3' });
|
|
229
|
-
await sleep(2500); // I's leg-2 aborts (unknown invite) → no real leg-3 → R's pend stays live
|
|
230
|
-
const rpk = ac.Reduce('resp_eph_pub');
|
|
231
|
-
return { fid, rpk };
|
|
232
|
-
}
|
|
233
|
-
// T8a unexpected-inviter: a DIFFERENT sender (F) → R aborts on the inviter-cid pin
|
|
234
|
-
{
|
|
235
|
-
const { fid, rpk } = await liveRedemption();
|
|
236
|
-
const rejx = R.rejects.length; const sR = st(R);
|
|
237
|
-
await mutate(F, '::actor::qa_send_complete', { target: R.cid, invite_id: fid, resp_eph_pub: rpk, mode: 'real', foreign_ad: binv(F, adBlob(F)) });
|
|
238
|
-
await sleep(3000);
|
|
239
|
-
ok(R.rejects.slice(rejx).some((x) => /unexpected inviter/i.test(x)), `leg-3 sender-pin: completion from a non-inviter aborts`);
|
|
240
|
-
ok(st(R).c === sR.c, `leg-3 unexpected-inviter registered nothing`);
|
|
241
|
-
}
|
|
242
|
-
// T8b cid-bind leg-3: real inviter (I) sends a FOREIGN AD → R aborts cid-bind
|
|
243
|
-
{
|
|
244
|
-
const { fid, rpk } = await liveRedemption();
|
|
245
|
-
const rejx = R.rejects.length; const sR = st(R);
|
|
246
|
-
await mutate(I, '::actor::qa_send_complete', { target: R.cid, invite_id: fid, resp_eph_pub: rpk, mode: 'foreign', foreign_ad: binv(I, adBlob(F)) });
|
|
247
|
-
await sleep(3000);
|
|
248
|
-
ok(R.rejects.slice(rejx).some((x) => /does not belong to the sender/.test(x)), `leg-3 cid-bind: inviter AD (cid≠sender) aborts`);
|
|
249
|
-
ok(st(R).c === sR.c, `leg-3 cid-bind registered nothing`);
|
|
250
|
-
}
|
|
251
|
-
// T8c PoP leg-3: real inviter (I) sends a FORGED AD (cid ok) → R aborts in process_address_document
|
|
252
|
-
{
|
|
253
|
-
const { fid, rpk } = await liveRedemption();
|
|
254
|
-
const rejx = R.rejects.length; const sR = st(R);
|
|
255
|
-
await mutate(I, '::actor::qa_send_complete', { target: R.cid, invite_id: fid, resp_eph_pub: rpk, mode: 'forged', foreign_ad: binv(I, adBlob(F)) });
|
|
256
|
-
await sleep(3000);
|
|
257
|
-
ok(R.rejects.slice(rejx).some((x) => /authoriz|signature|process|key/i.test(x)), `leg-3 PoP: forged inviter AD aborts in process_address_document`);
|
|
258
|
-
ok(st(R).c === sR.c, `leg-3 PoP registered nothing`);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// ---------- T9 export-secrecy ----------
|
|
262
|
-
CUR = 'T9 export-secrecy';
|
|
263
|
-
console.log('\n=== T9 export-secrecy ===');
|
|
264
|
-
await mutate(I, '::a2a_messaging::generate_invite', { name: 'P9' }); // ensure an outstanding invite (eph secret present)
|
|
265
|
-
const exp = ro(I, '::actor::qa_export_core', undefined).Reduce('core');
|
|
266
|
-
const expStr = exp.Serialize ? Buffer.from(exp.Serialize()).toString('latin1') : '';
|
|
267
|
-
const expVis = exp.Visualize();
|
|
268
|
-
ok(!/pending_invite_keys/.test(expVis), `export_core_state has NO pending_invite_keys field`);
|
|
269
|
-
ok(!/pending_redemption_keys/.test(expVis), `export_core_state has NO pending_redemption_keys field`);
|
|
270
|
-
// the exported pending_invites entry must carry only assigned/eph_pub/scheme (public) — not a secret field
|
|
271
|
-
ok(/eph_pub|\$eph_pub/.test(expVis) || /pending_invites/.test(expVis), `export carries the public pending_invites record (eph_pub/assigned/scheme only)`);
|
|
272
|
-
|
|
273
|
-
// ---------- T2 happy-role (chain verified BOTH legs) ----------
|
|
274
|
-
CUR = 'T2 happy-role';
|
|
275
|
-
console.log('\n=== T2 happy-role ===');
|
|
276
|
-
const rA = mk('rootA'); const lA = mk('roleA'); const rB = mk('rootB'); const lB = mk('roleB');
|
|
277
|
-
await mkPacket(rA, 'eph-t-rootA-1'); await mkPacket(lA, 'eph-t-roleA-1');
|
|
278
|
-
await mkPacket(rB, 'eph-t-rootB-1'); await mkPacket(lB, 'eph-t-roleB-1');
|
|
279
|
-
await sleep(1200);
|
|
280
|
-
await setName(rA, 'RootA'); await setName(lA, 'RoleA'); await setName(rB, 'RootB'); await setName(lB, 'RoleB');
|
|
281
|
-
await delegate(rA, lA, 'RoleA'); await delegate(rB, lB, 'RoleB');
|
|
282
|
-
log('delegations done');
|
|
283
|
-
const rm = await mutate(lA, '::a2a_messaging::generate_invite', { name: 'RoleB' });
|
|
284
|
-
const rblob = Buffer.from(rm.Reduce('invite').GetBinary());
|
|
285
|
-
await mutate(lB, '::a2a_messaging::add_contact', { invite: binv(lB, rblob), name: 'PeerRoleA' });
|
|
286
|
-
await sleep(5500);
|
|
287
|
-
const sA = st(lA); const sB = st(lB);
|
|
288
|
-
log(`roleA=${JSON.stringify(sA)} roleB=${JSON.stringify(sB)}`);
|
|
289
|
-
ok(sA.c >= 1 && sA.p >= 1, `role inviter registered role responder`);
|
|
290
|
-
ok(sB.c >= 1 && sB.p >= 1, `role responder registered role inviter (receiver-side)`);
|
|
291
|
-
ok(sA.cr >= 1, `leg-2: inviter pinned responder's root linkage (contact_roots=${sA.cr})`);
|
|
292
|
-
ok(sB.cr >= 1, `leg-3: responder pinned inviter's root linkage (contact_roots=${sB.cr})`);
|
|
293
|
-
|
|
294
|
-
// ---------- T10 import migration (state round-trips; pending_invites reset) ----------
|
|
295
|
-
CUR = 'T10 migration';
|
|
296
|
-
console.log('\n=== T10 import migration ===');
|
|
297
|
-
await mutate(I, '::a2a_messaging::generate_invite', { name: 'P10' }); // leave an outstanding invite
|
|
298
|
-
const exported = ro(I, '::actor::export_state', undefined);
|
|
299
|
-
fs.writeFileSync(resolve(UNIT_DIR, 'mig.bin'), Buffer.from(exported.Serialize()));
|
|
300
|
-
const I2 = mk('I2'); await mkPacket(I2, 'eph-t-I2-mig'); await sleep(1000);
|
|
301
|
-
const adData = I2.pw.packet.ParseValue(new Uint8Array(fs.readFileSync(resolve(UNIT_DIR, 'mig.bin'))));
|
|
302
|
-
await mutate(I2, '::actor::import_state', adData);
|
|
303
|
-
const s10 = st(I2);
|
|
304
|
-
ok(s10.c >= 1 && s10.p >= 1, `import restored contacts + peer_ads (c=${s10.c},peer_ads=${s10.p})`);
|
|
305
|
-
ok(s10.pi === 0, `import reset pending_invites to empty (migration §4.4) → ${s10.pi}`);
|
|
306
|
-
ok(new RegExp(R.cid).test(lc(I2)), `imported state includes the original contact (responder)`);
|
|
307
|
-
// version-0 path: strip the stamp from a re-exported blob → still imports.
|
|
308
|
-
const exp0 = ro(I2, '::actor::export_state', undefined);
|
|
309
|
-
ok(exp0.Reduce('core').Reduce('format_version').Visualize() === '1', `re-export carries the stamp`);
|
|
310
|
-
|
|
311
|
-
// ---------- T11a format stamp + restore-state export hygiene ----------
|
|
312
|
-
CUR = 'T11a format-stamp';
|
|
313
|
-
console.log('\n=== T11a format stamp + export hygiene ===');
|
|
314
|
-
{
|
|
315
|
-
const core = ro(I, '::actor::qa_export_core', undefined).Reduce('core');
|
|
316
|
-
const vis = core.Visualize();
|
|
317
|
-
ok(/format_version/.test(vis), `export_core_state carries a format_version stamp`);
|
|
318
|
-
ok(core.Reduce('format_version').Visualize() === '1', `format_version == 1`);
|
|
319
|
-
ok(!/pending_restore_keys/.test(vis), `export has NO pending_restore_keys (INV-4)`);
|
|
320
|
-
ok(!/pending_restore_reply_keys/.test(vis), `export has NO pending_restore_reply_keys (INV-4)`);
|
|
321
|
-
ok(/deferred_msgs/.test(vis), `export carries deferred_msgs`);
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// ---------- T11b degraded contact: defer + 3-leg restore + flush ----------
|
|
325
|
-
CUR = 'T11b restore';
|
|
326
|
-
console.log('\n=== T11b degraded-contact restore ===');
|
|
327
|
-
{
|
|
328
|
-
// I ↔ R are established contacts (T1). Simulate a breaking-change migration
|
|
329
|
-
// outcome on I: contacts survive, peer_ads dropped.
|
|
330
|
-
await mutate(I, '::actor::qa_strip_peer_ads', {});
|
|
331
|
-
const s0 = st(I);
|
|
332
|
-
ok(s0.c >= 1 && s0.p === 0, `strip: I keeps contacts (${s0.c}) with no peer_ads`);
|
|
333
|
-
|
|
334
|
-
// Deferred send: queues + fires leg 0; restore legs run; both sides notify.
|
|
335
|
-
const evI = I.events.length, evR = R.events.length;
|
|
336
|
-
const dm = await mutate(I, '::a2a_messaging::send_message', { contact: R.cid, text: 'queued-while-degraded' });
|
|
337
|
-
ok(dm.Reduce('deferred').Visualize() === 'TRUE' || /true/i.test(dm.Reduce('deferred').Visualize()), `send to degraded contact reports deferred`);
|
|
338
|
-
const dg = ro(I, '::a2a_messaging::list_degraded_contacts', undefined);
|
|
339
|
-
ok(new RegExp(R.cid).test(dg.Visualize()), `list_degraded_contacts includes the degraded contact`);
|
|
340
|
-
ok(/queued/.test(dg.Visualize()) && /attempts/.test(dg.Visualize()), `list_degraded_contacts carries attempts+queued fields`);
|
|
341
|
-
const lq = ro(I, '::a2a_messaging::list_deferred_queues', undefined);
|
|
342
|
-
ok(new RegExp(R.cid).test(lq.Visualize()) && /degraded/.test(lq.Visualize()), `list_deferred_queues shows the non-empty queue with its degraded flag`);
|
|
343
|
-
await sleep(6000);
|
|
344
|
-
const s1 = st(I);
|
|
345
|
-
ok(s1.p >= 1, `restore re-established R's AD at I (peer_ads=${s1.p})`);
|
|
346
|
-
ok(s1.prs === 0, `restore consumed I's pending_restores`);
|
|
347
|
-
ok(st(R).rr === 0, `restore consumed R's reply record`);
|
|
348
|
-
ok(I.events.slice(evI).includes('contact_restored'), `I notified contact_restored`);
|
|
349
|
-
ok(R.events.slice(evR).includes('contact_restored'), `R notified contact_restored`);
|
|
350
|
-
|
|
351
|
-
// Host-driven flush: drain the deferred queue, message arrives at R.
|
|
352
|
-
const fl = await mutate(I, '::a2a_messaging::flush_deferred', { contact: R.cid });
|
|
353
|
-
ok(+fl.Reduce('flushed').Visualize() === 1, `flush_deferred drained 1 message`);
|
|
354
|
-
await sleep(2500);
|
|
355
|
-
ok(/queued-while-degraded/.test(ro(R, '::actor::list_incoming_messages', undefined).Visualize()), `deferred message delivered after restore`);
|
|
356
|
-
ok(st(I).dq === 0, `deferred queue cleared`);
|
|
357
|
-
ok(!(new RegExp(R.cid)).test(ro(I, '::a2a_messaging::list_degraded_contacts', undefined).Visualize()), `contact no longer listed degraded after restore`);
|
|
358
|
-
ok(!(new RegExp(R.cid)).test(ro(I, '::a2a_messaging::list_deferred_queues', undefined).Visualize()), `deferred queue gone after flush`);
|
|
359
|
-
|
|
360
|
-
// Channel fully healthy again, both directions.
|
|
361
|
-
await mutate(R, '::a2a_messaging::send_message', { contact: I.cid, text: 'post-restore-R-to-I' });
|
|
362
|
-
await sleep(2500);
|
|
363
|
-
ok(/post-restore-R-to-I/.test(ro(I, '::actor::list_incoming_messages', undefined).Visualize()), `R→I works after restore (one-sided loss: R replaced I's AD)`);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
// ---------- T11c both-sides degraded + host sweep ----------
|
|
367
|
-
CUR = 'T11c both-degraded';
|
|
368
|
-
console.log('\n=== T11c both-sides degraded + sweep ===');
|
|
369
|
-
{
|
|
370
|
-
await mutate(I, '::actor::qa_strip_peer_ads', {});
|
|
371
|
-
await mutate(R, '::actor::qa_strip_peer_ads', {});
|
|
372
|
-
ok(st(I).p === 0 && st(R).p === 0, `both sides stripped`);
|
|
373
|
-
const sw = await mutate(I, '::a2a_messaging::restore_degraded_contacts', {});
|
|
374
|
-
ok(+sw.Reduce('requested').Visualize() >= 1, `sweep requested restore for I's degraded contacts`);
|
|
375
|
-
await mutate(R, '::a2a_messaging::restore_degraded_contacts', {});
|
|
376
|
-
await sleep(7000);
|
|
377
|
-
ok(st(I).p >= 1, `I restored (both-degraded, symmetric handshakes)`);
|
|
378
|
-
ok(st(R).p >= 1, `R restored (both-degraded)`);
|
|
379
|
-
await mutate(I, '::a2a_messaging::send_message', { contact: R.cid, text: 'after-double-restore' });
|
|
380
|
-
await sleep(2500);
|
|
381
|
-
ok(/after-double-restore/.test(ro(R, '::actor::list_incoming_messages', undefined).Visualize()), `channel healthy after double restore`);
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
// ---------- T11d restore gates: foreign requester + unsolicited response ----------
|
|
385
|
-
CUR = 'T11d restore-gates';
|
|
386
|
-
console.log('\n=== T11d restore gates ===');
|
|
387
|
-
{
|
|
388
|
-
// Foreign requester: F is NOT a contact of R → SILENT ignore (no reply, no
|
|
389
|
-
// reject, no reply-record).
|
|
390
|
-
const rrBefore = st(R).rr; const fRej = F.rejects.length;
|
|
391
|
-
await mutate(F, '::actor::qa_send_restore_request', { target: R.cid });
|
|
392
|
-
await sleep(3000);
|
|
393
|
-
ok(st(R).rr === rrBefore, `foreign restore request left NO reply record at R`);
|
|
394
|
-
ok(st(F).p === 0 || true, `foreign requester gained nothing`); // F never had R's AD
|
|
395
|
-
ok(!F.rejects.slice(fRej).some((x) => /restore/i.test(x)), `R sent NO error reply (silent ignore — knowledge does not leak)`);
|
|
396
|
-
|
|
397
|
-
// Unsolicited leg 1 at I (no pending_restores entry for F) → rejected, no state change.
|
|
398
|
-
const sI = st(I); const rejI = I.rejects.length;
|
|
399
|
-
await mutate(F, '::actor::qa_send_fake_restore_response', { target: I.cid });
|
|
400
|
-
await sleep(3000);
|
|
401
|
-
ok(I.rejects.slice(rejI).some((x) => /Unsolicited restore response/.test(x)), `unsolicited restore response rejected at I`);
|
|
402
|
-
const sI2 = st(I);
|
|
403
|
-
ok(sI2.c === sI.c && sI2.p === sI.p, `unsolicited response mutated nothing`);
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
// ---------- T11e restore adversarial: wrong-rid leg 1 + leg-0 non-destructiveness ----------
|
|
407
|
-
CUR = 'T11e restore-adversarial';
|
|
408
|
-
console.log('\n=== T11e restore adversarial (rid-mismatch + leg-0 non-destructive) ===');
|
|
409
|
-
{
|
|
410
|
-
// (a) rid-mismatch leg 1, seen by a non-contact (F): F fires a restore
|
|
411
|
-
// request at R (R silently ignores it since F is not R's contact, so F's
|
|
412
|
-
// own pending_restores[R.cid] entry stays live, unconsumed). R then answers
|
|
413
|
-
// F with a FRESH, unrelated rid via qa_send_fake_restore_response — F DOES
|
|
414
|
-
// have a live pending entry keyed by R.cid, so the "Unsolicited restore
|
|
415
|
-
// response" gate passes, but the rid the fake reply carries (freshly
|
|
416
|
-
// random-minted) can never match F's stored rid → deterministic
|
|
417
|
-
// rid-mismatch rejection that mutates nothing.
|
|
418
|
-
const fRejBefore = F.rejects.length;
|
|
419
|
-
await mutate(F, '::actor::qa_send_restore_request', { target: R.cid });
|
|
420
|
-
await sleep(3000);
|
|
421
|
-
const fMid = st(F);
|
|
422
|
-
ok(fMid.prs >= 1, `F has a live pending_restores entry toward R (silently ignored by R)`);
|
|
423
|
-
await mutate(R, '::actor::qa_send_fake_restore_response', { target: F.cid });
|
|
424
|
-
await sleep(3000);
|
|
425
|
-
ok(F.rejects.slice(fRejBefore).some((x) => /does not match the outstanding request/.test(x)), `wrong-rid leg 1 rejected at F ("does not match the outstanding request")`);
|
|
426
|
-
const fAfter = st(F);
|
|
427
|
-
ok(fAfter.p === fMid.p, `wrong-rid leg 1 installed no peer_ads at F (p=${fAfter.p})`);
|
|
428
|
-
ok(fAfter.prs === fMid.prs, `F's pending_restores entry survives the rejected reply unchanged (a failed gate consumes nothing, prs=${fAfter.prs})`);
|
|
429
|
-
|
|
430
|
-
// (b) leg 0 from a REAL, healthy contact (I, R — established via T11b) is
|
|
431
|
-
// non-destructive, and a repeat request REPLACES the outstanding reply
|
|
432
|
-
// record instead of accumulating it: fire two leg-0s from I at R
|
|
433
|
-
// back-to-back (no network round trip between them). I's own
|
|
434
|
-
// pending_restores[R.cid] is overwritten to the SECOND rid synchronously,
|
|
435
|
-
// long before either leg-1 reply can return over the loopback broker, so
|
|
436
|
-
// the first (now-stale) leg-1 deterministically rid-mismatches at I, and
|
|
437
|
-
// only the second request's leg 1/2 round-trip completes.
|
|
438
|
-
const rBefore = st(R); const iBefore = st(I); const iRejBefore = I.rejects.length;
|
|
439
|
-
await mutate(I, '::actor::qa_send_restore_request', { target: R.cid });
|
|
440
|
-
await mutate(I, '::actor::qa_send_restore_request', { target: R.cid });
|
|
441
|
-
await sleep(3000);
|
|
442
|
-
const rMid = st(R);
|
|
443
|
-
ok(rMid.c === rBefore.c && rMid.p === rBefore.p, `leg 0 (x2) from a known contact is non-destructive at R (c=${rMid.c},p=${rMid.p})`);
|
|
444
|
-
// rr is 1 while the surviving handshake is in flight and 0 once its leg 2
|
|
445
|
-
// lands — both are legal here depending on loopback timing. The
|
|
446
|
-
// accumulation property is that TWO requests never yield TWO records.
|
|
447
|
-
ok(rMid.rr <= 1, `second leg-0 REPLACED R's reply record, not accumulated (rr=${rMid.rr})`);
|
|
448
|
-
await sleep(5000);
|
|
449
|
-
const rAfter = st(R); const iAfter = st(I);
|
|
450
|
-
ok(rAfter.c === rBefore.c && rAfter.p === rBefore.p, `R's state still unchanged once the round trip completes (c=${rAfter.c},p=${rAfter.p})`);
|
|
451
|
-
ok(rAfter.rr === 0, `R's reply record consumed once the surviving (second) leg 1/2 round-trips (rr=${rAfter.rr})`);
|
|
452
|
-
ok(I.rejects.slice(iRejBefore).some((x) => /does not match the outstanding request/.test(x)), `the FIRST (superseded) leg-1 reply is rejected at I ("does not match the outstanding request")`);
|
|
453
|
-
ok(iAfter.c === iBefore.c && iAfter.p === iBefore.p, `I's state unchanged in count after the race (c=${iAfter.c},p=${iAfter.p})`);
|
|
454
|
-
|
|
455
|
-
// Channel still fully healthy after the double-restore race.
|
|
456
|
-
await mutate(I, '::a2a_messaging::send_message', { contact: R.cid, text: 'post-T11e-I-to-R' });
|
|
457
|
-
await sleep(2500);
|
|
458
|
-
ok(/post-T11e-I-to-R/.test(ro(R, '::actor::list_incoming_messages', undefined).Visualize()), `channel healthy after the T11e restore race`);
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
console.log('\n================ SCORECARD ================');
|
|
462
|
-
if (scorecard.length === 0) console.log('ALL TESTS PASSED');
|
|
463
|
-
else { console.log(`${scorecard.length} FAILURE(S):`); scorecard.forEach((s) => console.log(' ' + s)); }
|
|
464
|
-
await sleep(500);
|
|
465
|
-
process.exit(scorecard.length === 0 ? 0 : 1);
|
|
466
|
-
}
|
|
467
|
-
main().catch((e) => { log('DRIVER ERR:', e.stack ?? e.message); process.exit(1); });
|