@ours.network/tg-connector 0.1.8-nightly.4 → 0.1.8-nightly.5

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/dist/connector.js CHANGED
@@ -26677,12 +26677,14 @@ async function restoreConnection(name) {
26677
26677
  log(`[${name}] pre-migration retention failed (continuing):`, String(err));
26678
26678
  }
26679
26679
  }
26680
+ let imported = false;
26680
26681
  try {
26681
26682
  const buf = fs3.readFileSync(dataPath(dir));
26682
26683
  await withScopeAsync(async (lt) => {
26683
26684
  const adaptData = pkt.pw.packet.ParseValue(new Uint8Array(buf)).Attach(lt);
26684
26685
  await pkt.mutatingTx("::actor::import_state", adaptData, lt);
26685
26686
  });
26687
+ imported = true;
26686
26688
  } catch (err) {
26687
26689
  log(`[${name}] FAILED TO IMPORT SAVED STATE \u2014 continuing with the reseeded identity; surviving contacts (if the blob was partially migrated) self-heal via contact restore:`, String(err));
26688
26690
  try {
@@ -26691,6 +26693,26 @@ async function restoreConnection(name) {
26691
26693
  } catch {
26692
26694
  }
26693
26695
  }
26696
+ if (imported) {
26697
+ try {
26698
+ const st = await withScopeAsync(async (lt) => {
26699
+ const r = await pkt.mutatingTx("::a2a_messaging::commit_e2e_restore", {}, lt);
26700
+ const status = r.Reduce("status").Visualize();
26701
+ const sessions = r.Reduce("sessions").IsNil() ? "0" : r.Reduce("sessions").Visualize();
26702
+ return { status: String(status), sessions: String(sessions) };
26703
+ });
26704
+ log(`[${name}] e2e restore commit: status=${st.status} sessions=${st.sessions}`);
26705
+ } catch (err) {
26706
+ log(`[${name}] E2E RESTORE REJECTED \u2014 staged session blob failed pickle validation; discarding it, fresh account + self-heal fallback take over: ${String(err).slice(0, 260)}`);
26707
+ try {
26708
+ await withScopeAsync(async (lt) => {
26709
+ await pkt.mutatingTx("::a2a_messaging::reject_e2e_restore", {}, lt);
26710
+ });
26711
+ } catch (err2) {
26712
+ log(`[${name}] reject_e2e_restore failed (staging is transient; continuing):`, String(err2));
26713
+ }
26714
+ }
26715
+ }
26694
26716
  }
26695
26717
  const key = isCatchAll(cfg.chatId) ? "any chat" : chatKey(cfg.chatId, cfg.threadId || void 0);
26696
26718
  log(`[${name}] restored (bot ${botLabel(bot)}, ${key}${cfg.peerCid ? `, peer ${cfg.peerCid}` : ", awaiting proxy"})`);
@@ -1402,34 +1402,35 @@ application actor loads libraries
1402
1402
  // The packet-level snapshot is NOT used for upgrades: it is bound to the unit
1403
1403
  // code hash, so a new .muflo cannot load an old snapshot. This data blob is.
1404
1404
  //
1405
- // The blob stays FLAT with the historical field names — the core fields come
1406
- // from a2a_messaging::export_core_state / import_core_state, the app fields
1407
- // are composed in here — so a PRE-migration export imports unchanged.
1405
+ // Core (contact / profile / hierarchy / DR-session) state is carried WHOLE under
1406
+ // $core: export_core_state owns its OWN field set, so any field the shared core
1407
+ // adds — most importantly $e2e_sessions (the DR ratchet pickles that a manual
1408
+ // per-field projection silently DROPPED, losing messages across a reload) — rides
1409
+ // forward automatically, and the core's monitoring keys never collide with the
1410
+ // APP-level monitoring keys we still own below (they share both names AND shapes).
1411
+ // The blob stays code-independent; import_state migrates the pre-namespacing FLAT
1412
+ // shape forward (see there). Mirrors ours-mcp's actor exactly.
1408
1413
 
1409
1414
  trn readonly export_state _
1410
1415
  {
1411
- core_state = a2a_messaging::export_core_state NIL.
1412
1416
  return (
1413
1417
  $app_format_version -> 1,
1414
- $my_name -> core_state $my_name,
1415
- $contacts -> core_state $contacts,
1416
- $pending_invites -> core_state $pending_invites,
1418
+ $core -> a2a_messaging::export_core_state NIL,
1419
+ // ---- app-owned state (route inbox + files + local book + monitoring) ----
1417
1420
  $inbox -> inbox,
1418
1421
  $next_msg_seq -> next_msg_seq,
1419
1422
  $file_inbox -> file_inbox,
1420
1423
  $next_file_seq -> next_file_seq,
1421
- $peer_ads -> core_state $peer_ads,
1422
1424
  $registrar_ad -> registrar_ad,
1423
1425
  $local_auto_accept -> local_auto_accept,
1424
1426
  // Nonces are exported so a restart does not reopen the replay window
1425
1427
  // for still-fresh credentials; stale ones are purged lazily anyway.
1426
1428
  $seen_nonces -> seen_nonces,
1427
1429
  $pending_introductions -> pending_introductions,
1428
- $my_bio -> core_state $my_bio,
1429
- $delegation_cert -> core_state $delegation_cert,
1430
- $root_ad -> core_state $root_ad,
1431
- $root_profile -> core_state $root_profile,
1432
- $contact_roots -> core_state $contact_roots,
1430
+ // App-level (role->root) monitoring — a DISTINCT mechanism from core's
1431
+ // forced monitoring. Stays app-owned and top-level; it must NEVER be fed
1432
+ // into import_core_state (that would arm core's hidden monitoring_proxy,
1433
+ // which the app cannot disarm). See import_state's legacy branch.
1433
1434
  $monitoring_enabled -> monitoring_enabled,
1434
1435
  $monitoring_inbox -> monitoring_inbox,
1435
1436
  $proxy_pending -> proxy_pending,
@@ -1449,11 +1450,36 @@ application actor loads libraries
1449
1450
  if (data $app_format_version) != NIL { app_fmt -> (data $app_format_version) safe int. }
1450
1451
  abort "App state blob format_version " + (_str app_fmt) + " is newer than this code (supports up to 1) — upgrade ours-tg-connector before importing." when app_fmt > 1.
1451
1452
 
1452
- // Core fields (contacts/profile/hierarchy) — validated and restored by
1453
- // the shared library, which also replays every peer's address document
1454
- // through process_address_document so encrypted channels keep working
1455
- // after the upgrade with no re-handshake.
1456
- a2a_messaging::import_core_state data.
1453
+ // Core fields (contacts/profile/hierarchy/DR-sessions) — validated and restored
1454
+ // by the shared library, which also replays every peer's address document
1455
+ // through process_address_document so encrypted channels keep working after the
1456
+ // upgrade with no re-handshake, and parks $e2e_sessions for the staged commit
1457
+ // below. The current export namespaces core under $core, so the core owns its
1458
+ // own field set and its monitoring keys never collide with the app's. A
1459
+ // PRE-namespacing FLAT blob carries the core fields at the top level — migrate it
1460
+ // by feeding import_core_state ONLY the genuine shared fields. We deliberately
1461
+ // OMIT proxy_pending / monitoring_proxy on the legacy path: in a flat blob those
1462
+ // keys hold the APP-level monitoring state (same names AND shapes as core's), and
1463
+ // feeding them to import_core_state would arm core's hidden forced-monitoring
1464
+ // proxy — which the app can NEVER disarm. Mirrors ours-mcp's actor exactly.
1465
+ if (data $core) != NIL
1466
+ {
1467
+ a2a_messaging::import_core_state (data $core).
1468
+ }
1469
+ else
1470
+ {
1471
+ a2a_messaging::import_core_state (
1472
+ $my_name -> data $my_name,
1473
+ $contacts -> data $contacts,
1474
+ $pending_invites -> data $pending_invites,
1475
+ $peer_ads -> data $peer_ads,
1476
+ $my_bio -> data $my_bio,
1477
+ $delegation_cert -> data $delegation_cert,
1478
+ $root_ad -> data $root_ad,
1479
+ $root_profile -> data $root_profile,
1480
+ $contact_roots -> data $contact_roots
1481
+ ).
1482
+ }
1457
1483
 
1458
1484
  // The inbox + next_msg_seq are the only parts the message-lifecycle changes
1459
1485
  // touched. A pre-lifecycle blob has no $next_msg_seq and inbox entries with
@@ -427,6 +427,14 @@ library a2a_messaging loads libraries
427
427
  // can order/dedup. No opt-in flag — push is gated only on monitoring_proxy.
428
428
  roster_push_seq is int = 0.
429
429
 
430
+ // core 0.12 (2b auto-advertise): the wire_version + a fingerprint of my self cap-id
431
+ // set captured at the LAST reconcile_advertise. Persisted so a post-upgrade boot
432
+ // NOTICES a version/cap change and re-advertises to legacy peers exactly once — the
433
+ // core owns the trigger, so the app stops orchestrating readvertise_on_upgrade.
434
+ // 0/"" = never reconciled (⇒ the first reconcile after any boot advertises).
435
+ advertised_pv is int = 0.
436
+ advertised_caps is str = "".
437
+
430
438
  // core 3.0: per-outstanding-invite ephemeral PRIVATE keys, keyed by invite id.
431
439
  // hidden ⇒ only this library mutates it AND it is structurally invisible to the
432
440
  // export record builder, but `hidden` governs VISIBILITY, not persistence — it
@@ -4198,6 +4206,70 @@ library a2a_messaging loads libraries
4198
4206
  return transaction::success actions.
4199
4207
  }
4200
4208
 
4209
+ // core 0.12 (2b): comma-joined fingerprint of my advertised cap-id set. self_cap_ids is
4210
+ // captured at init in a stable order (supported ∪ advertise, then any runtime
4211
+ // add_self_cap APPENDS), so this string changes iff my advertised caps change — the cheap
4212
+ // change-detector reconcile_advertise gates the legacy upgrade push on.
4213
+ fn caps_fingerprint (_) -> str
4214
+ {
4215
+ s is str = "".
4216
+ sc (a2a_capabilities::self_cap_ids NIL) -- ( -> c) { s -> (s + c) + ",". }
4217
+ return s.
4218
+ }
4219
+
4220
+ // core 0.12 (2b): the SINGLE core-owned re-advertise entrypoint. The app declares its
4221
+ // capabilities ONCE at init (a2a_capabilities) and calls THIS once per boot, replacing the
4222
+ // app-orchestrated readvertise_on_upgrade + readvertise_e2e_recovery pair — the core now
4223
+ // owns WHAT gets advertised. It ALWAYS runs the e2e self-heal recovery push (LOAD-BEARING:
4224
+ // a re-minted account after a lost/rejected restore leaves every peer holding my stale
4225
+ // bundle) and, when it NOTICES a wire-version or cap-set change since the last reconcile
4226
+ // (persisted advertised_pv/advertised_caps), ALSO runs the legacy upgrade push. Idempotent:
4227
+ // an unchanged pv+caps skips the legacy push (no redundant legacy traffic). Persists the
4228
+ // advertised record (_save_state). A contact that becomes reachable later relearns my AD
4229
+ // through handle_readvertise_ad once these pushes round-trip. STATELESS re-advertise
4230
+ // semantics (readvertise_ad_tx) are unchanged, so a pre-0.10 peer ignores the tx as before.
4231
+ trn reconcile_advertise _
4232
+ {
4233
+ current_transaction_info::validate_origin_or_abort (transaction::envelope::origin::user,).
4234
+ b = my_identity_bundle_fields_fresh NIL.
4235
+ cur_pv = a2a_versions::wire_version.
4236
+ cur_fp = caps_fingerprint NIL.
4237
+ changed is bool = (advertised_pv != cur_pv) || (cur_fp != advertised_caps).
4238
+ actions is transaction::action::type[] = [].
4239
+ e2e_n is int = 0.
4240
+ legacy_n is int = 0.
4241
+ // ALWAYS: the e2e self-heal recovery push (the load-bearing fallback re-arm).
4242
+ sc contacts -- (cid -> ) ?? ((contact_e2e_epoch cid) != NIL || (contact_born_dr cid) == TRUE || (e2e_pinned cid) || (contact_migration cid) != NIL)
4243
+ {
4244
+ actions (_count actions|) -> encrypted_channel::send_encrypted_tx cid (
4245
+ $name -> readvertise_ad_tx,
4246
+ $targ -> ( $ad -> (b $ad), $cert -> (b $cert), $root_profile -> (b $root_profile),
4247
+ $cp_binding -> (b $cp_binding),
4248
+ $pv -> a2a_versions::wire_version,
4249
+ $caps -> (a2a_capabilities::self_cap_ids NIL) ) ).
4250
+ e2e_n -> e2e_n + 1.
4251
+ }
4252
+ // ON CHANGE ONLY: the legacy upgrade push to pre-existing legacy contacts.
4253
+ if changed
4254
+ {
4255
+ sc contacts -- (cid -> ) ?? ((contact_e2e_epoch cid) == NIL && (contact_born_dr cid) != TRUE && (contact_migration cid) == NIL)
4256
+ {
4257
+ actions (_count actions|) -> encrypted_channel::send_encrypted_tx cid (
4258
+ $name -> readvertise_ad_tx,
4259
+ $targ -> ( $ad -> (b $ad), $cert -> (b $cert), $root_profile -> (b $root_profile),
4260
+ $cp_binding -> (b $cp_binding),
4261
+ $pv -> a2a_versions::wire_version,
4262
+ $caps -> (a2a_capabilities::self_cap_ids NIL) ) ).
4263
+ legacy_n -> legacy_n + 1.
4264
+ }
4265
+ }
4266
+ advertised_pv -> cur_pv.
4267
+ advertised_caps -> cur_fp.
4268
+ actions (_count actions|) -> _return_data ($changed -> changed, $e2e_readvertised -> e2e_n, $legacy_readvertised -> legacy_n).
4269
+ actions (_count actions|) -> _save_state NIL.
4270
+ return transaction::success actions.
4271
+ }
4272
+
4201
4273
  // core 0.11 (session self-heal, retry leg): re-send unacked e2e messages stuck without
4202
4274
  // a delivered receipt for redrive_min_age_seconds+ (see the const above). Host calls
4203
4275
  // this on the boot/GC cadence next to readvertise_e2e_recovery. Mutates session state
@@ -4427,7 +4499,11 @@ library a2a_messaging loads libraries
4427
4499
  $delivered_wire -> delivered_wire,
4428
4500
  // Ship-review major-4: the sweep resume cursor survives restarts, else a
4429
4501
  // boot-sweep-only lifecycle re-scans the same prefix forever (tail starves).
4430
- $redrive_cursor -> redrive_sweep_cursor
4502
+ $redrive_cursor -> redrive_sweep_cursor,
4503
+ // core 0.12 (2b): the pv + cap-set fingerprint of my last self-advertise
4504
+ // reconcile, so a post-upgrade boot detects the change and re-advertises once.
4505
+ $advertised_pv -> advertised_pv,
4506
+ $advertised_caps -> advertised_caps
4431
4507
  ).
4432
4508
  }
4433
4509
 
@@ -4628,6 +4704,11 @@ library a2a_messaging loads libraries
4628
4704
  // Ship-review major-4: restore the sweep resume cursor (absent in older blobs).
4629
4705
  if (data $redrive_cursor) != NIL
4630
4706
  { redrive_sweep_cursor -> (data $redrive_cursor) safe global_id. }
4707
+ // core 0.12 (2b): restore the last-advertised pv + cap fingerprint (absent in older
4708
+ // blobs → stays 0/"" → the first reconcile_advertise after upgrade sees a change and
4709
+ // re-advertises, which is exactly the desired post-upgrade behavior).
4710
+ if (data $advertised_pv) != NIL { advertised_pv -> (data $advertised_pv) safe int. }
4711
+ if (data $advertised_caps) != NIL { advertised_caps -> (data $advertised_caps) safe str. }
4631
4712
 
4632
4713
  // Re-register every peer's keys so encrypted channels keep working after
4633
4714
  // the upgrade — no handshake needed (my own keys are unchanged, and the
@@ -0,0 +1,82 @@
1
+ # tests/compat — cross-version back-compat matrix (M-legs) + upgrade-UX legs (U-legs)
2
+
3
+ The DR-rollout ship gate (DR-ROLLOUT-PLAN.md §5/§5.1): prove that an OLD-core peer and a
4
+ NEW-core peer interoperate (DoD D1) and that upgrades migrate seamlessly (D2/D3) — with the
5
+ two peers running in **separate processes, each under its own SDK runtime and its own
6
+ compiled unit**. That two-runtime split is what the rest of `tests/` deliberately avoids
7
+ (single node_modules, both peers in-process) and what production actually is mid-rollout.
8
+
9
+ ## Peers
10
+
11
+ - **OLD** — core `v0.2.0` (`faa2b52`, the OSP: the deployed ours-mcp/tg-connector repo pin;
12
+ no `$pv`, no DR, legacy `encrypted_channel`), compiled by and run under an 0.9.x-era
13
+ toolkit (`OLD_ADAPT_TOOLKIT` / `OLD_SDK_NM`).
14
+ - **NEW** — THIS working tree's core, compiled by and run under the current pinned toolkit
15
+ (`ADAPT_TOOLKIT` / `OURS_SDK_NODE_MODULES`, same knobs as `run.sh`).
16
+
17
+ Each peer's build dir gets its own `core/` sources, its own compiled `.muflo`, and its own
18
+ `node_modules` symlink — a packet is only ever loaded by the runtime whose compiler built
19
+ it (the eval_unit.h hash-check contract).
20
+
21
+ ## Env (run_compat.sh)
22
+
23
+ | Var | Meaning | Default |
24
+ |---|---|---|
25
+ | `OLD_CORE_REF` | git ref for the OLD peer's core sources | `v0.2.0` |
26
+ | `OLD_ADAPT_TOOLKIT` | toolkit checkout whose `mufl-compile` + stdlib build the OLD unit | required |
27
+ | `OLD_SDK_NM` | node_modules with the OLD-era `@adapt-toolkit` sdk | required |
28
+ | `ADAPT_TOOLKIT` / `OURS_SDK_NODE_MODULES` / `DEV_BROKER` / `PORT` | as in `run.sh` | required |
29
+
30
+ ## Leg inventory (status is explicit — an unimplemented leg FAILS, never skips silently)
31
+
32
+ | Leg | Gate | DoD | Status |
33
+ |---|---|---|---|
34
+ | M1 invite gen→redeem, both directions | MP | D1 | implemented |
35
+ | M2 first-contact message after redeem | MP | D1 | implemented |
36
+ | M3 steady-state send/receive both ways | MP | D1 | implemented |
37
+ | M4 file transfer both ways | MP | D1 | implemented |
38
+ | M5 OLD restarts: v0-blob export→reimport, channel resumes | MP | D1,D2 | implemented |
39
+ | M6 NEW restarts: stamped blob + DR sessions restore | MP | D1,D2 | implemented |
40
+ | M7 NEW↔NEW DR handshake + ratchet + dual restart | MP | D1 | not-implemented (needs 2× NEW peers — trivial config once M1–M6 green) |
41
+ | M8 stale-snapshot restore → self-heal | NTH | D2 | not-implemented |
42
+ | M9 corrupt blob import reject-to-empty | MP(NEW) | D2 | not-implemented |
43
+ | M10 version_error_t as data | NTH | D1 | not-implemented (assertion pattern exists in tests/test.mjs V-series) |
44
+ | M11 pv re-learning on peer downgrade | NTH | D1 | not-implemented |
45
+ | M12 cross-app tg-connector↔mcp | MP | D1,D4 | lives in the consumer repos (drives their packets, not this actor) |
46
+ | U1/U2/U3 npm-bump upgrade legs | MP | D2,D3 | live in the consumer repos' CI (install old → state → bump → verify) |
47
+
48
+ Related prior art this seeds from: control-plane `tests/blob-import.mjs` (old-unit→new-unit
49
+ same-seed gate) and `tests/mixed-unit.mjs` (4-cell pairing matrix) — single-runtime versions
50
+ of M5/M1; this harness generalizes them across runtimes.
51
+
52
+ ## Run
53
+
54
+ ```
55
+ OLD_ADAPT_TOOLKIT=... OLD_SDK_NM=... ADAPT_TOOLKIT=... OURS_SDK_NODE_MODULES=... \
56
+ DEV_BROKER=... bash tests/compat/run_compat.sh
57
+ ```
58
+
59
+ ## M12 — control-plane NEW ↔ mcp OLD (owner's named leg) — GREEN 2026-07-21
60
+
61
+ Reuses this orchestrator with per-side flavors (`NEW_PEER_FLAVOR=messenger` switches the
62
+ READ verbs to `::actor::get_conversation`; the whole `::a2a_messaging::` write surface is
63
+ shared). Peers: NEW = the control-plane DR unit (dev5/dr-e2e-integration, sdk 0.10.10,
64
+ core v0.8.0) · OLD = the REAL mcp actor (`packages/core/mufl_code/actor.mu`) at its
65
+ deployed pins (core `faa2b52` = v0.2.0/OSP) compiled+run under published sdk/mufl 0.9.1.
66
+ Result: M1–M6 all green (invites both ways, first-contact, steady both ways, files both
67
+ ways, both restart-restore legs).
68
+
69
+ ```
70
+ # build dirs: old = mcp actor + 0.9.1 stage; new = cp unit + its node_modules
71
+ OLD_BUILD_DIR=<mcp-old> NEW_BUILD_DIR=<cp-new> NEW_PEER_FLAVOR=messenger \
72
+ LEG_FILTER=M1,M2,M3,M4,M5,M6 node tests/compat/compat.mjs
73
+ ```
74
+
75
+ ## U3 — control-plane upgrade path — GREEN 2026-07-21
76
+
77
+ Under the fresh-address ruling (unpublished app, zero clients) the npm-bump leg's
78
+ meaningful nucleus is the control-plane's own blob-import gate: main-era unit sources
79
+ (messenger + core 0.7.1) compiled with the CURRENT toolchain → real state → import under
80
+ the DR unit → same seed ⇒ same cid, history/contacts preserved, zero rejects, re-export
81
+ round-trips. Run via the cp repo's tests/run-blob-import.sh with OLD_UNIT pointed at the
82
+ main-era build.
@@ -0,0 +1,245 @@
1
+ #!/usr/bin/env node
2
+ // Cross-version matrix orchestrator: spawns OLD-runtime and NEW-runtime peers
3
+ // (compat_peer.mjs each, separate processes, own node_modules + own unit — see README),
4
+ // then drives the M-legs against the pair. Scorecard semantics: an MP leg failing (or not
5
+ // implemented) exits non-zero; NTH legs report but never gate. No leg ever silently skips.
6
+ import { spawn } from 'node:child_process';
7
+ import { openSync as fsOpenSync, appendFileSync as fsAppendFileSync } from 'node:fs';
8
+ import * as readline from 'node:readline';
9
+ import { resolve } from 'node:path';
10
+
11
+ const OLD_DIR = process.env.OLD_BUILD_DIR;
12
+ const NEW_DIR = process.env.NEW_BUILD_DIR;
13
+ const BROKER_URL = process.env.BROKER_URL || 'ws://127.0.0.1:9797';
14
+ if (!OLD_DIR || !NEW_DIR) { console.error('OLD_BUILD_DIR / NEW_BUILD_DIR required'); process.exit(2); }
15
+
16
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
17
+ let nextId = 1;
18
+
19
+ function spawnPeer(dir, name, extraEnv = {}) {
20
+ // stderr goes to a per-peer log — handshake rejects surface there, and
21
+ // discarding it turned real compat failures into blind timeouts.
22
+ const errLog = fsOpenSync(resolve(dir, `${name}.stderr.log`), 'a');
23
+ const child = spawn(process.execPath, [resolve(dir, 'compat_peer.mjs')], {
24
+ cwd: dir, stdio: ['pipe', 'pipe', errLog],
25
+ env: { ...process.env, BROKER_URL, PEER_NAME: name, PEER_SEED: `compat ${name} seed`,
26
+ PEER_FLAVOR: dir === NEW_DIR ? (process.env.NEW_PEER_FLAVOR || 'actor') : (process.env.OLD_PEER_FLAVOR || 'actor'),
27
+ ...extraEnv },
28
+ });
29
+ const peer = { name, dir, child, cid: '', events: [], waiters: new Map(), readyP: null, exited: null };
30
+ const rl = readline.createInterface({ input: child.stdout });
31
+ let readyResolve; peer.readyP = new Promise((r) => { readyResolve = r; });
32
+ child.on('exit', (code, signal) => {
33
+ peer.exited = { code, signal };
34
+ console.log(` [peer ${name}] exited code=${code} signal=${signal} (${peer.waiters.size} call(s) in flight)`);
35
+ for (const [, w] of peer.waiters) w.reject(new Error(`peer ${name} exited (code=${code} signal=${signal})`));
36
+ peer.waiters.clear();
37
+ readyResolve(); // a dead peer resolves readyP; callers check peer.cid
38
+ });
39
+ rl.on('line', (line) => {
40
+ let msg; try { msg = JSON.parse(line); } catch {
41
+ // Non-JSON stdout (e.g. the sdk leak-detector dump on exit) must not
42
+ // vanish — append it to the peer's log so leak totals are auditable.
43
+ try { fsAppendFileSync(resolve(dir, `${name}.stderr.log`), `[stdout] ${line}\n`); } catch {}
44
+ return;
45
+ }
46
+ if (msg.ready) { peer.cid = msg.cid; return readyResolve(); }
47
+ if (msg.event) return void peer.events.push(msg.event);
48
+ if (msg.fatal) { console.error(`[${name}] FATAL ${msg.fatal}`); return readyResolve(); }
49
+ const w = peer.waiters.get(msg.id);
50
+ if (w) { peer.waiters.delete(msg.id); msg.ok ? w.resolve(msg.value) : w.reject(new Error(msg.error)); }
51
+ });
52
+ return peer;
53
+ }
54
+
55
+ function call(peer, op, fields = {}, timeoutMs = 90000) {
56
+ const id = nextId++;
57
+ return new Promise((resolveP, rejectP) => {
58
+ if (peer.exited && op !== 'exit') return rejectP(new Error(`peer ${peer.name} already exited`));
59
+ const timer = setTimeout(() => {
60
+ peer.waiters.delete(id);
61
+ rejectP(new Error(`call ${op} on ${peer.name} timed out after ${timeoutMs}ms`));
62
+ }, timeoutMs);
63
+ peer.waiters.set(id, {
64
+ resolve: (v) => { clearTimeout(timer); resolveP(v); },
65
+ reject: (e) => { clearTimeout(timer); rejectP(e); },
66
+ });
67
+ peer.child.stdin.write(JSON.stringify({ id, op, ...fields }) + '\n');
68
+ });
69
+ }
70
+
71
+ // ---- shared drive helpers --------------------------------------------------
72
+ async function contactsInclude(peer, cid, timeoutMs = 30000) {
73
+ const deadline = Date.now() + timeoutMs;
74
+ for (;;) {
75
+ const contacts = String(await call(peer, 'contacts').catch(() => ''));
76
+ if (contacts.includes(cid)) return true;
77
+ if (Date.now() > deadline) return false;
78
+ await sleep(1500);
79
+ }
80
+ }
81
+ async function pair(inviter, redeemer) {
82
+ const inviteB64 = await call(inviter, 'invite', { name: redeemer.name });
83
+ await call(redeemer, 'redeem', { invite_b64: inviteB64, name: inviter.name });
84
+ // Handshake is multi-leg and cross-runtime — poll until BOTH sides list the
85
+ // peer instead of guessing a sleep. A timeout here is a real M1 verdict.
86
+ const a = await contactsInclude(inviter, redeemer.cid);
87
+ const b = await contactsInclude(redeemer, inviter.cid);
88
+ if (!a || !b) throw new Error(`pairing incomplete after 30s (inviter-sees=${a} redeemer-sees=${b})`);
89
+ }
90
+ async function roundTrip(from, to, text, expectRoute) {
91
+ const sent = await call(from, 'send', { cid: to.cid, text });
92
+ if (expectRoute && sent?.route !== expectRoute) {
93
+ throw new Error(`route mismatch: expected ${expectRoute}, got ${sent?.route} for "${text}"`);
94
+ }
95
+ await sleep(2500);
96
+ const inbox = await call(to, 'inbox', { cid: from.cid });
97
+ if (!String(inbox).includes(text)) throw new Error(`message not delivered: "${text}"`);
98
+ return sent;
99
+ }
100
+
101
+ // ---- legs ------------------------------------------------------------------
102
+ const LEGS = [
103
+ { id: 'M1', gate: 'MP', dod: 'D1', name: 'invite gen→redeem, both directions',
104
+ run: async (ctx) => {
105
+ await pair(ctx.oldPeer, ctx.newPeer); // OLD invites, NEW redeems (primary pair, reused by later legs)
106
+ // Reverse direction on a fresh throwaway pair: NEW invites, OLD redeems.
107
+ const old2 = spawnPeer(OLD_DIR, 'old2', { PEER_SEED: 'compat old2 seed' });
108
+ const new2 = spawnPeer(NEW_DIR, 'new2', { PEER_SEED: 'compat new2 seed' });
109
+ try {
110
+ await old2.readyP; await new2.readyP;
111
+ if (!old2.cid || !new2.cid) throw new Error('reverse-pair peers failed to boot');
112
+ await pair(new2, old2);
113
+ await roundTrip(new2, old2, 'reverse-pair first-contact new2→old2');
114
+ } finally {
115
+ await call(old2, 'exit').catch(() => {}); await call(new2, 'exit').catch(() => {});
116
+ } } },
117
+ { id: 'M2', gate: 'MP', dod: 'D1', name: 'first-contact message after redeem (new→old)',
118
+ run: async (ctx) => { await roundTrip(ctx.newPeer, ctx.oldPeer, 'first-contact new->old'); } },
119
+ { id: 'M3', gate: 'MP', dod: 'D1', name: 'steady-state send/receive both ways',
120
+ run: async (ctx) => {
121
+ await roundTrip(ctx.oldPeer, ctx.newPeer, 'steady old->new');
122
+ await roundTrip(ctx.newPeer, ctx.oldPeer, 'steady new->old'); } },
123
+ { id: 'M4', gate: 'MP', dod: 'D1', name: 'file transfer both ways',
124
+ run: async (ctx) => {
125
+ const data_b64 = Buffer.from('compat file payload').toString('base64');
126
+ await call(ctx.oldPeer, 'send_file', { cid: ctx.newPeer.cid, filename: 'from-old.txt', data_b64 });
127
+ await call(ctx.newPeer, 'send_file', { cid: ctx.oldPeer.cid, filename: 'from-new.txt', data_b64 });
128
+ await sleep(3000);
129
+ const fNew = String(await call(ctx.newPeer, 'files', { cid: ctx.oldPeer.cid }));
130
+ const fOld = String(await call(ctx.oldPeer, 'files', { cid: ctx.newPeer.cid }));
131
+ if (!fNew.includes('from-old.txt')) throw new Error('old→new file not received');
132
+ if (!fOld.includes('from-new.txt')) throw new Error('new→old file not received'); } },
133
+ { id: 'M5', gate: 'MP', dod: 'D1,D2', name: 'OLD restart: v0-blob export→reimport, channel resumes',
134
+ run: async (ctx) => {
135
+ await call(ctx.oldPeer, 'export_state', { file: resolve(OLD_DIR, 'state.bin') });
136
+ await ctx.respawn('oldPeer', true);
137
+ await roundTrip(ctx.newPeer, ctx.oldPeer, 'post-old-restart new->old'); } },
138
+ { id: 'M6', gate: 'MP', dod: 'D1,D2', name: 'NEW restart: stamped blob + DR-session restore',
139
+ run: async (ctx) => {
140
+ await call(ctx.newPeer, 'export_state', { file: resolve(NEW_DIR, 'state.bin') });
141
+ await ctx.respawn('newPeer', true);
142
+ await roundTrip(ctx.oldPeer, ctx.newPeer, 'post-new-restart old->new'); } },
143
+ { id: 'M7', gate: 'MP', dod: 'D1', name: 'NEW↔NEW DR handshake + ratchet + dual restart',
144
+ run: async () => {
145
+ const A = spawnPeer(NEW_DIR, 'newA', { PEER_SEED: 'compat newA seed' });
146
+ const B = spawnPeer(NEW_DIR, 'newB', { PEER_SEED: 'compat newB seed' });
147
+ try {
148
+ await A.readyP; await B.readyP;
149
+ if (!A.cid || !B.cid) throw new Error('M7 peers failed to boot');
150
+ const CAPS = { advertise: ['core.e2e', 'core.e2e.migrate'] };
151
+ await call(A, 'tx', { verb: '::actor::qa_init_caps', targ: CAPS });
152
+ await call(B, 'tx', { verb: '::actor::qa_init_caps', targ: CAPS });
153
+ await pair(A, B);
154
+ // Drive migration sweeps until the route flips to the double ratchet.
155
+ let route = '';
156
+ const deadline = Date.now() + 45000;
157
+ for (let i = 1; Date.now() < deadline && route !== 'e2e'; i++) {
158
+ await call(A, 'tx', { verb: '::a2a_messaging::sweep_e2e_migrations', targ: {} }).catch(() => {});
159
+ await call(B, 'tx', { verb: '::a2a_messaging::sweep_e2e_migrations', targ: {} }).catch(() => {});
160
+ await sleep(2000);
161
+ route = (await roundTrip(A, B, `dr-probe-${i}`))?.route ?? '';
162
+ }
163
+ if (route !== 'e2e') throw new Error(`route never reached e2e (last: ${route || 'legacy'})`);
164
+ // Ratchet advance both directions on the live session.
165
+ await roundTrip(A, B, 'dr-ratchet-a1', 'e2e');
166
+ await roundTrip(B, A, 'dr-ratchet-b1', 'e2e');
167
+ await roundTrip(A, B, 'dr-ratchet-a2', 'e2e');
168
+ // Dual restart with state — DR sessions must survive on BOTH ends.
169
+ await call(A, 'export_state', { file: resolve(NEW_DIR, 'stateA.bin') });
170
+ await call(B, 'export_state', { file: resolve(NEW_DIR, 'stateB.bin') });
171
+ await call(A, 'exit').catch(() => {}); await call(B, 'exit').catch(() => {});
172
+ await sleep(1000);
173
+ const A2 = spawnPeer(NEW_DIR, 'newA', { PEER_SEED: 'compat newA seed', PEER_STATE_FILE: resolve(NEW_DIR, 'stateA.bin') });
174
+ const B2 = spawnPeer(NEW_DIR, 'newB', { PEER_SEED: 'compat newB seed', PEER_STATE_FILE: resolve(NEW_DIR, 'stateB.bin') });
175
+ try {
176
+ await A2.readyP; await B2.readyP;
177
+ if (!A2.cid || !B2.cid) throw new Error('M7 respawn failed');
178
+ await sleep(2000);
179
+ await roundTrip(A2, B2, 'dr-post-restart-a');
180
+ await roundTrip(B2, A2, 'dr-post-restart-b');
181
+ } finally {
182
+ await call(A2, 'exit').catch(() => {}); await call(B2, 'exit').catch(() => {});
183
+ }
184
+ } finally {
185
+ await call(A, 'exit').catch(() => {}); await call(B, 'exit').catch(() => {});
186
+ } } },
187
+ { id: 'M8', gate: 'NTH', dod: 'D2', name: 'stale-snapshot restore → self-heal',
188
+ run: async () => { throw new Error('NOT IMPLEMENTED'); } },
189
+ { id: 'M9', gate: 'MP', dod: 'D2', name: 'corrupt blob import reject-to-functional (NEW)',
190
+ run: async (ctx) => {
191
+ await call(ctx.newPeer, 'export_state', { file: resolve(NEW_DIR, 'state.bin') });
192
+ const { readFileSync, writeFileSync } = await import('node:fs');
193
+ const good = readFileSync(resolve(NEW_DIR, 'state.bin'));
194
+ writeFileSync(resolve(NEW_DIR, 'state.bin'), good.subarray(0, Math.floor(good.length / 2)));
195
+ await ctx.respawn('newPeer', true);
196
+ const failed = ctx.newPeer.events.some((e) => String(e).startsWith('import_failed'));
197
+ if (!failed) throw new Error('corrupt blob did not surface import_failed');
198
+ // Reject-to-FUNCTIONAL: the fresh identity must still pair + message.
199
+ const inviteB64 = await call(ctx.oldPeer, 'invite', { name: 'renew' });
200
+ await call(ctx.newPeer, 'redeem', { invite_b64: inviteB64, name: 'oldpeer-again' });
201
+ const seen = await contactsInclude(ctx.newPeer, ctx.oldPeer.cid);
202
+ if (!seen) throw new Error('post-corruption re-pairing failed');
203
+ // Restore the good blob so later runs/legs are unaffected.
204
+ writeFileSync(resolve(NEW_DIR, 'state.bin'), good);
205
+ } },
206
+ { id: 'M10', gate: 'NTH', dod: 'D1', name: 'version_error_t as data',
207
+ run: async () => { throw new Error('NOT IMPLEMENTED — assertion pattern in tests/test.mjs V-series'); } },
208
+ { id: 'M11', gate: 'NTH', dod: 'D1', name: 'pv re-learning on peer downgrade',
209
+ run: async () => { throw new Error('NOT IMPLEMENTED'); } },
210
+ ];
211
+
212
+ // ---- main ------------------------------------------------------------------
213
+ const ctx = {
214
+ oldPeer: spawnPeer(OLD_DIR, 'oldpeer'),
215
+ newPeer: spawnPeer(NEW_DIR, 'newpeer'),
216
+ async respawn(ref, withState) {
217
+ const prev = ctx[ref];
218
+ await call(prev, 'exit').catch(() => {});
219
+ await sleep(1000);
220
+ const dir = ref === 'oldPeer' ? OLD_DIR : NEW_DIR;
221
+ ctx[ref] = spawnPeer(dir, prev.name,
222
+ withState ? { PEER_STATE_FILE: resolve(dir, 'state.bin') } : {});
223
+ await ctx[ref].readyP;
224
+ if (!ctx[ref].cid) throw new Error(`${prev.name} failed to respawn`);
225
+ await sleep(2000);
226
+ },
227
+ };
228
+
229
+ const results = [];
230
+ await ctx.oldPeer.readyP; await ctx.newPeer.readyP;
231
+ if (!ctx.oldPeer.cid || !ctx.newPeer.cid) { console.error('peer boot failed'); process.exit(1); }
232
+ console.log(`peers up: old=${ctx.oldPeer.cid.slice(0, 10)} new=${ctx.newPeer.cid.slice(0, 10)}`);
233
+ // LEG_FILTER=M7,M9 runs a subset (unlisted legs report as skipped-by-filter, never silently)
234
+ const filter = (process.env.LEG_FILTER || '').split(',').filter(Boolean);
235
+ for (const leg of LEGS) {
236
+ if (filter.length && !filter.includes(leg.id)) {
237
+ console.log(` - ${leg.id} skipped by LEG_FILTER (not a verdict)`); continue;
238
+ }
239
+ try { await leg.run(ctx); results.push({ leg, pass: true }); console.log(` ✓ ${leg.id} [${leg.gate}/${leg.dod}] ${leg.name}`); }
240
+ catch (err) { results.push({ leg, pass: false, err }); console.log(` ✗ ${leg.id} [${leg.gate}/${leg.dod}] ${leg.name}: ${err.message}`); }
241
+ }
242
+ await call(ctx.oldPeer, 'exit').catch(() => {}); await call(ctx.newPeer, 'exit').catch(() => {});
243
+ const mpFails = results.filter((r) => !r.pass && r.leg.gate === 'MP');
244
+ console.log(`\n${results.filter((r) => r.pass).length}/${LEGS.length} legs passed; MP failures: ${mpFails.length} (${mpFails.map((r) => r.leg.id).join(', ') || 'none'})`);
245
+ process.exit(mpFails.length ? 1 : 0);
@@ -0,0 +1,146 @@
1
+ #!/usr/bin/env node
2
+ // Per-process peer driver for the compat matrix. One process = one runtime = one packet.
3
+ //
4
+ // Loaded from a build dir whose node_modules symlink points at THIS peer's sdk era and
5
+ // whose cwd holds THIS peer's compiled unit — the orchestrator (compat.mjs) never mixes
6
+ // runtimes in-process. Commands arrive as JSON lines on stdin; every command produces
7
+ // exactly one JSON line on stdout: {id, ok, value?, error?}. Async inbound traffic is
8
+ // surfaced via unsolicited {event} lines. Boot/mutate/ro shapes mirror tests/test_common.mjs
9
+ // (the era-stable driver conventions: add_client_message + on_return_data pending queue).
10
+ import { resolve } from 'node:path';
11
+ import * as fs from 'node:fs';
12
+ import * as readline from 'node:readline';
13
+ import { adapt_wrapper } from '@adapt-toolkit/sdk/executables';
14
+ import { PacketWrapperConfigurator } from '@adapt-toolkit/sdk/wrappers';
15
+ import { object_to_adapt_value } from '@adapt-toolkit/sdk/wrapper';
16
+
17
+ const BROKER_URL = process.env.BROKER_URL || 'ws://127.0.0.1:9797';
18
+ const SEED = process.env.PEER_SEED || `compat seed ${process.env.PEER_NAME || 'peer'}`;
19
+ const STATE_FILE = process.env.PEER_STATE_FILE || ''; // set ⇒ import on boot if present
20
+ // PEER_FLAVOR=messenger switches the READ verbs to the control-plane messenger
21
+ // surface (history keyed per contact via ::actor::get_conversation). The whole
22
+ // ::a2a_messaging:: WRITE surface (invite/redeem/send/send_file) is shared.
23
+ const FLAVOR = process.env.PEER_FLAVOR || 'actor';
24
+ const UNIT_DIR = resolve('.');
25
+ const unitHash = fs.readdirSync(UNIT_DIR).find((f) => f.endsWith('.muflo')).slice(0, -'.muflo'.length);
26
+ const UNIT = new Uint8Array(fs.readFileSync(resolve(UNIT_DIR, `${unitHash}.muflo`)));
27
+
28
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
29
+ const out = (obj) => process.stdout.write(JSON.stringify(obj) + '\n');
30
+ const pending = [];
31
+ let pw = null;
32
+
33
+ function wire() {
34
+ pw.on_return_data = (d) => {
35
+ const kind = d.Reduce('kind').Visualize();
36
+ if (kind === 'notify_agent') {
37
+ try { out({ event: d.Reduce('payload').Reduce('event').Visualize() }); } catch { out({ event: 'unparsed' }); }
38
+ return;
39
+ }
40
+ if (kind === 'save_state') return;
41
+ const w = pending.shift(); if (!w) return;
42
+ clearTimeout(w.timer); w.resolve(d.Reduce('payload'));
43
+ };
44
+ pw.on_transaction_failure = (msg) => {
45
+ const w = pending.shift();
46
+ if (w) { clearTimeout(w.timer); w.reject(new Error(msg)); }
47
+ else out({ event: `inbound-reject: ${String(msg).split('\n')[0]}` });
48
+ };
49
+ }
50
+
51
+ function mutate(name, targ) {
52
+ return new Promise((res, rej) => {
53
+ const timer = setTimeout(() => { rej(new Error(`timeout: ${name}`)); }, 20000);
54
+ pending.push({ resolve: res, reject: rej, timer });
55
+ pw.add_client_message(object_to_adapt_value({ name, targ }));
56
+ });
57
+ }
58
+ const ro = (name, targ) => pw.packet.ExecuteTransaction(object_to_adapt_value({ name, targ }));
59
+ const binv = (buf) => pw.packet.NewBinaryFromBuffer(Buffer.from(buf));
60
+
61
+ async function boot() {
62
+ const wrapper = await adapt_wrapper.start(['--broker_address', BROKER_URL, '--test_mode',
63
+ '--logger_config', '--level', 'WARNING', '--stdout', 'stderr', '--logger_config_end']);
64
+ wrapper.start();
65
+ await sleep(1500);
66
+ const cfg = new PacketWrapperConfigurator();
67
+ cfg.process_arguments(['--unit_hash', unitHash, '--seed_phrase', SEED, '--unit_dir_path', UNIT_DIR]);
68
+ await new Promise((res, rej) => {
69
+ const t = setTimeout(() => rej(new Error('create timeout')), 30000);
70
+ wrapper.packet_manager.create_packet(cfg, (p) => { clearTimeout(t); pw = p; wire(); res(); }, UNIT);
71
+ });
72
+ if (STATE_FILE && fs.existsSync(STATE_FILE)) {
73
+ // Import failure must not kill the peer — mirror the consumer posture
74
+ // (tg-connector: continue with the fresh identity, blob preserved): the
75
+ // M9 leg asserts exactly this reject-to-functional behavior.
76
+ try {
77
+ const bytes = fs.readFileSync(STATE_FILE);
78
+ await mutate('::actor::import_state', pw.packet.ParseValue(new Uint8Array(bytes)));
79
+ out({ event: 'state_imported' });
80
+ } catch (err) {
81
+ out({ event: `import_failed: ${String(err).split('\n')[0]}` });
82
+ }
83
+ }
84
+ out({ ready: true, cid: pw.packet.GetContainerID().Visualize() });
85
+ }
86
+
87
+ const rl = readline.createInterface({ input: process.stdin });
88
+ rl.on('line', async (line) => {
89
+ let cmd;
90
+ try { cmd = JSON.parse(line); } catch { return out({ id: null, ok: false, error: 'bad json' }); }
91
+ const { id, op } = cmd;
92
+ try {
93
+ switch (op) {
94
+ case 'invite': {
95
+ const m = await mutate('::a2a_messaging::generate_invite', { name: cmd.name });
96
+ return out({ id, ok: true, value: Buffer.from(m.Reduce('invite').GetBinary()).toString('base64') });
97
+ }
98
+ case 'redeem': {
99
+ await mutate('::a2a_messaging::add_contact',
100
+ { invite: binv(Buffer.from(cmd.invite_b64, 'base64')), name: cmd.name });
101
+ return out({ id, ok: true });
102
+ }
103
+ case 'send': {
104
+ const m = await mutate('::a2a_messaging::send_message', { contact: cmd.cid, text: cmd.text });
105
+ // route: 'e2e' (double ratchet) | 'migrating' | absent/NIL = legacy box
106
+ return out({ id, ok: true, value: { wire_id: m.Reduce('wire_id').Visualize(), route: m.Reduce('route').Visualize() } });
107
+ }
108
+ case 'tx': {
109
+ const m = await mutate(cmd.verb, cmd.targ);
110
+ return out({ id, ok: true, value: m ? m.Visualize() : null });
111
+ }
112
+ case 'send_file': {
113
+ const m = await mutate('::a2a_messaging::send_file', {
114
+ contact: cmd.cid, filename: cmd.filename, mime: cmd.mime || 'application/octet-stream',
115
+ data: binv(Buffer.from(cmd.data_b64, 'base64')),
116
+ });
117
+ return out({ id, ok: true, value: m.Reduce('wire_id').Visualize() });
118
+ }
119
+ case 'contacts': return out({ id, ok: true, value: ro('::a2a_messaging::list_contacts', undefined).Visualize() });
120
+ case 'inbox': {
121
+ if (FLAVOR === 'messenger') {
122
+ return out({ id, ok: true, value: ro('::actor::get_conversation', { contact: cmd.cid }).Visualize() });
123
+ }
124
+ return out({ id, ok: true, value: ro('::actor::list_incoming_messages', undefined).Visualize() });
125
+ }
126
+ case 'files': {
127
+ if (FLAVOR === 'messenger') {
128
+ // messenger file receipts land as history entries carrying $filename
129
+ return out({ id, ok: true, value: ro('::actor::get_conversation', { contact: cmd.cid }).Visualize() });
130
+ }
131
+ return out({ id, ok: true, value: ro('::actor::list_incoming_files', undefined).Visualize() });
132
+ }
133
+ case 'export_state': {
134
+ const exported = ro('::actor::export_state', undefined);
135
+ fs.writeFileSync(cmd.file, Buffer.from(exported.Serialize()));
136
+ return out({ id, ok: true });
137
+ }
138
+ case 'exit': out({ id, ok: true }); return process.exit(0);
139
+ default: return out({ id, ok: false, error: `unknown op ${op}` });
140
+ }
141
+ } catch (err) {
142
+ return out({ id, ok: false, error: String(err) });
143
+ }
144
+ });
145
+
146
+ boot().catch((e) => { out({ fatal: String(e) }); process.exit(1); });
@@ -0,0 +1,96 @@
1
+ #!/usr/bin/env bash
2
+ # Cross-version compat matrix: build TWO units — OLD core (git ref, OLD-era toolchain) and
3
+ # NEW core (this working tree, current toolchain) — each in its own build dir with its own
4
+ # node_modules symlink, boot ONE broker, run tests/compat/compat.mjs against the pair.
5
+ # See tests/compat/README.md for the env contract and leg inventory.
6
+ #
7
+ # Toolchain dirs auto-detect flavor (owner ruling: the OLD side should be the PUBLISHED
8
+ # @adapt-toolkit packages production actually pinned, not a from-scratch build):
9
+ # - npm @adapt-toolkit/mufl package root → prebuilds/linux-x64/mufl-compile
10
+ # - adapt toolkit checkout → build.<platform>.release/mufl-compile
11
+ set -u
12
+ HERE=$(cd "$(dirname "$0")" && pwd)
13
+ CORE_DIR=$(cd "$HERE/../.." && pwd)
14
+
15
+ OLD_CORE_REF=${OLD_CORE_REF:-v0.2.0}
16
+ OLD_TOOLCHAIN=${OLD_TOOLCHAIN:?set OLD_TOOLCHAIN (npm @adapt-toolkit/mufl pkg root of the OLD era, e.g. <stage>/node_modules/@adapt-toolkit/mufl — or an old toolkit checkout)}
17
+ OLD_SDK_NM=${OLD_SDK_NM:?set OLD_SDK_NM (node_modules with the OLD-era @adapt-toolkit sdk, e.g. sdk@0.9.1)}
18
+ NEW_TOOLCHAIN=${NEW_TOOLCHAIN:-${ADAPT_TOOLKIT:-}}
19
+ [ -n "$NEW_TOOLCHAIN" ] || { echo "set NEW_TOOLCHAIN (current @adapt-toolkit/mufl pkg root or toolkit checkout)"; exit 2; }
20
+ SDK_NM=${OURS_SDK_NODE_MODULES:?set OURS_SDK_NODE_MODULES (current sdk node_modules)}
21
+ DEV_BROKER=${DEV_BROKER:?set DEV_BROKER (dev-broker.mjs launcher)}
22
+ PORT=${PORT:-9797}
23
+
24
+ # resolve_toolchain <dir> → sets RESOLVED_COMPILE / RESOLVED_ROOT
25
+ resolve_toolchain() {
26
+ local dir=$1 platform
27
+ platform="$(uname | tr '[:upper:]' '[:lower:]')"
28
+ for cand in "$dir/prebuilds/linux-x64/mufl-compile" \
29
+ "$dir/build/mufl-compile" \
30
+ "$dir/build.$platform.release/mufl-compile"; do
31
+ if [ -x "$cand" ]; then RESOLVED_COMPILE="$cand"; RESOLVED_ROOT="$dir"; return 0; fi
32
+ done
33
+ echo "MISSING: no mufl-compile under $dir"; exit 2
34
+ }
35
+
36
+ for p in "$OLD_SDK_NM/@adapt-toolkit" "$SDK_NM/@adapt-toolkit" "$DEV_BROKER"; do
37
+ [ -e "$p" ] || { echo "MISSING: $p"; exit 2; }
38
+ done
39
+
40
+ WORK=$(mktemp -d); echo "WORK=$WORK (kept for post-mortem when KEEP_WORK=1)"; if [ "${KEEP_WORK:-0}" = "1" ]; then trap '[ -n "${BROKER_PID:-}" ] && kill "$BROKER_PID" 2>/dev/null' EXIT; else trap 'rm -rf "$WORK"; [ -n "${BROKER_PID:-}" ] && kill "$BROKER_PID" 2>/dev/null' EXIT; fi
41
+
42
+ # build_unit <outdir> <core_src_dir> <toolchain_dir> <sdk_nm>
43
+ # Uses the era's OWN tests/test_actor.mu + protocol_container.mm (the actor surface
44
+ # matches its core), same compile shape as tests/run.sh / consumer compile-mufl.sh.
45
+ build_unit() {
46
+ local out=$1 src=$2 tk=$3 nm=$4
47
+ resolve_toolchain "$tk"
48
+ mkdir -p "$out/core"
49
+ cp "$src"/*.mm "$out/core/"; cp "$src/config.mufl" "$out/core/" 2>/dev/null || true
50
+ cp "$src/tests/test_actor.mu" "$out/actor.mu"
51
+ # Cross-process peers must REGISTER with the broker (single-wrapper suites
52
+ # deliver locally and never need this): inject registration_proof, which the
53
+ # real consumer actors (messenger.mu, tg actor.mu) load explicitly and the
54
+ # core test actors deliberately omit. Without it, registration-message
55
+ # creation fails client-side and no traffic ever crosses the broker.
56
+ sed -i '0,/loads libraries/s//loads libraries\n registration_proof,/' "$out/actor.mu"
57
+ if [ -f "$src/tests/protocol_container.mm" ]; then cp "$src/tests/protocol_container.mm" "$out/"
58
+ else cp "$CORE_DIR/tests/protocol_container.mm" "$out/"; fi
59
+ ln -sfn "$nm" "$out/node_modules"
60
+ cp "$HERE/compat_peer.mjs" "$out/"
61
+ cat > "$out/config.mufl" <<'CFG'
62
+ config script
63
+ {
64
+ stdlib_config = (config_load #$MUFL_STDLIB_PATH).
65
+ core_config = (config_load #"core").
66
+ (
67
+ $imports ->
68
+ (
69
+ $libraries -> (stdlib_config $exports $libraries)'(core_config $exports $libraries)'($protocol_container -> #"protocol_container.mm"),
70
+ ),
71
+ $exports -> ( $libraries -> (,), $applications -> (,) )
72
+ ).
73
+ }
74
+ CFG
75
+ ( cd "$out" && MUFL_STDLIB_PATH="$RESOLVED_ROOT/mufl_stdlib" "$RESOLVED_COMPILE" \
76
+ -mp "$RESOLVED_ROOT/meta" -mp "$RESOLVED_ROOT/transactions" -d-c actor.mu >/dev/null ) \
77
+ || { echo "COMPILE FAILED in $out"; exit 3; }
78
+ }
79
+
80
+ echo "== building OLD unit ($OLD_CORE_REF, $(basename "$OLD_TOOLCHAIN")) =="
81
+ OLD_SRC="$WORK/old-src"; mkdir -p "$OLD_SRC"
82
+ git -C "$CORE_DIR" archive "$OLD_CORE_REF" | tar -x -C "$OLD_SRC"
83
+ build_unit "$WORK/old" "$OLD_SRC" "$OLD_TOOLCHAIN" "$OLD_SDK_NM"
84
+
85
+ echo "== building NEW unit (working tree, $(basename "$NEW_TOOLCHAIN")) =="
86
+ build_unit "$WORK/new" "$CORE_DIR" "$NEW_TOOLCHAIN" "$SDK_NM"
87
+
88
+ echo "== broker on :$PORT =="
89
+ # dev-broker contract: --host/--port REQUIRED, --test_mode skips attestation. Runs under
90
+ # the NEW sdk (cwd with the current node_modules) — the broker relays opaque frames, so
91
+ # one broker serves both eras.
92
+ ( cd "$WORK/new" && node "$DEV_BROKER" --host 127.0.0.1 --port "$PORT" --test_mode ) & BROKER_PID=$!
93
+ sleep 2
94
+
95
+ BROKER_URL="ws://127.0.0.1:$PORT" OLD_BUILD_DIR="$WORK/old" NEW_BUILD_DIR="$WORK/new" \
96
+ node "$HERE/compat.mjs"
@@ -670,6 +670,95 @@ async function main() {
670
670
  const lSt2 = ro(lo, '::actor::qa_mig_state', { cid: hi.cid });
671
671
  ok(sweep2.Reduce('initiated').Visualize() === '0' && lSt2.Reduce('phase').Visualize() === 'active' && hex(getBin(lSt2, 'epoch')) === epoch0, '★ C-path IDEMPOTENCY: a second sweep does NOT re-offer the ACTIVE contact (phase + epoch unchanged; the never-cleared contact_migration gate)'); }
672
672
 
673
+ // ═══ 2b AUTO-ADVERTISE — core-owned reconcile_advertise + version/cap change detection ═══
674
+ // The app declares its caps once at init and calls reconcile_advertise once per boot; the
675
+ // core owns the re-advertise (replacing app-orchestrated readvertise_on_upgrade/e2e_recovery)
676
+ // and NOTICES a persisted wire-version/cap-set change to gate the legacy upgrade push.
677
+ console.log('=== mig: 2b reconcile_advertise — core-owned re-advertise + change detection ===');
678
+ {
679
+ const CA = await mkNode('mig-adv-A', 'CAA');
680
+ const CB = await mkNode('mig-adv-B', 'CBB'); await sleep(1000);
681
+ const cinv = await mutate(CA, '::a2a_messaging::generate_invite', { name: 'CBB' });
682
+ await mutate(CB, '::a2a_messaging::add_contact', { invite: binv(CB, Buffer.from(cinv.Reduce('invite').GetBinary())), name: 'CAA' });
683
+ await sleep(6000); // invite redeem round-trips (CA gains a contact)
684
+
685
+ // First reconcile: the advertised record is empty (pv=0) → CHANGED → advertises.
686
+ const r1 = await mutate(CA, '::a2a_messaging::reconcile_advertise', {});
687
+ ok(T(r1.Reduce('changed').Visualize()), '2b: first reconcile_advertise reports changed (fresh/never-advertised record)');
688
+ ok(+r1.Reduce('legacy_readvertised').Visualize() + +r1.Reduce('e2e_readvertised').Visualize() >= 1, '2b: first reconcile re-advertised to the contact');
689
+
690
+ // Second reconcile: no pv/cap change → NOT changed → legacy upgrade push SKIPPED (idempotent).
691
+ const r2 = await mutate(CA, '::a2a_messaging::reconcile_advertise', {});
692
+ ok(!T(r2.Reduce('changed').Visualize()), '2b: second reconcile is idempotent (unchanged pv+caps → not changed)');
693
+ ok(+r2.Reduce('legacy_readvertised').Visualize() === 0, '2b: idempotent reconcile skips the legacy upgrade push');
694
+
695
+ // Capability change: add cap_e2e_migrate at runtime → reconcile NOTICES the new cap.
696
+ await mutate(CA, '::a2a_messaging::advertise_migrate', {});
697
+ const r3 = await mutate(CA, '::a2a_messaging::reconcile_advertise', {});
698
+ ok(T(r3.Reduce('changed').Visualize()), '2b: reconcile_advertise NOTICES a new capability (advertise_migrate added cap_e2e_migrate → cap fingerprint changed)');
699
+ }
700
+
701
+ // ═══ RELOAD PORT (design §3 acceptance) — page-reload DR message-loss, now GREEN ═══
702
+ // Ports ours-control-plane test/dr-reload-session-loss (commit 86c0c93) onto the core:
703
+ // two born-DR peers hold a LIVE e2e session; A is RELOADED — export the whole $core →
704
+ // remove the packet → recreate from the SAME seed → import_state → commit_e2e_restore —
705
+ // then B, still on the pre-reload session, sends in the GAP (before A sends anything).
706
+ // Pre-b608 the session was NOT in the export, so the recreated packet started a fresh
707
+ // ratchet and B's in-gap message was LOST (RED). Persist-primary restores the SAME
708
+ // session, so it decrypts (GREEN). Non-vacuous: we assert the freshly-recreated packet
709
+ // has NO session, and that commit restores the SAME session id (not a fresh ratchet).
710
+ console.log('\n=== mig: RELOAD port — DR session survives packet recreate, in-gap message decrypts ===');
711
+ {
712
+ const RA = await mkNode('mig-reload-A', 'RLA');
713
+ const RB = await mkNode('mig-reload-B', 'RLB'); await sleep(1000);
714
+ const rinv = await mutate(RA, '::a2a_messaging::generate_invite', { name: 'RLB' });
715
+ await mutate(RB, '::a2a_messaging::add_contact', { invite: binv(RB, Buffer.from(rinv.Reduce('invite').GetBinary())), name: 'RLA' });
716
+ await sleep(6000); // invite redeem round-trips (contacts + peer_ads incl. e2e bundle)
717
+ await mutate(RA, '::actor::qa_learn_peer', { cid: RB.cid, pv: 9, caps: ['core.e2e'] });
718
+ await mutate(RB, '::actor::qa_learn_peer', { cid: RA.cid, pv: 9, caps: ['core.e2e'] });
719
+ ok(ro(RA, '::actor::qa_e2e_route', { cid: RB.cid }).Reduce('route').Visualize() === 'e2e', 'reload: the pair is on the e2e route (double ratchet)');
720
+
721
+ // Establish + advance the live session BOTH ways.
722
+ await mutate(RB, '::actor::qa_recv_reset', {});
723
+ await mutate(RA, '::a2a_messaging::send_message', { contact: 'RLB', text: 'reload-a1' });
724
+ await sleep(3000);
725
+ ok(ro(RB, '::actor::qa_recv_last', {}).Reduce('text').Visualize() === 'reload-a1', 'reload: pre-reload A→B delivered (live e2e session established)');
726
+ await mutate(RA, '::actor::qa_recv_reset', {});
727
+ await mutate(RB, '::a2a_messaging::send_message', { contact: 'RLA', text: 'reload-b1' });
728
+ await sleep(3000);
729
+ ok(ro(RA, '::actor::qa_recv_last', {}).Reduce('text').Visualize() === 'reload-b1', 'reload: pre-reload B→A delivered (ratchet advanced both ways)');
730
+
731
+ const sidBefore = hex(getBin(ro(RA, '::actor::qa_e2e_active', { cid: RB.cid }), 'sid'));
732
+ ok(sidBefore.length > 0, 'reload: A holds a live e2e session id before the reload');
733
+
734
+ // Persist the WHOLE $core (includes $e2e_sessions), exactly as a host does.
735
+ const savedCore = Buffer.from(ro(RA, '::actor::export_state', {}).Serialize());
736
+
737
+ // PAGE RELOAD: the old packet (+ its in-memory session) is gone; recreate from the SAME seed.
738
+ wrapper.packet_manager.remove_packet(RA.cid);
739
+ await sleep(800);
740
+ const RA2 = await mkNode('mig-reload-A', 'RLA'); await sleep(500);
741
+ ok(RA2.cid === RA.cid, 'reload: recreated packet keeps the same identity (same container id, so the broker routes here)');
742
+
743
+ // Non-vacuity: the freshly recreated packet has NO session — the reload really lost it.
744
+ const sidFresh = hex(getBin(ro(RA2, '::actor::qa_e2e_active', { cid: RB.cid }), 'sid'));
745
+ ok(sidFresh.length === 0, 'reload: the freshly recreated packet has NO e2e session (confirms the reload discards it)');
746
+
747
+ // Restore: import_state stages the persisted sessions, commit_e2e_restore installs them.
748
+ await mutate(RA2, '::actor::import_state', RA2.pw.packet.ParseValue(new Uint8Array(savedCore)));
749
+ const cr = await mutate(RA2, '::a2a_messaging::commit_e2e_restore', {});
750
+ ok(cr.Reduce('status').Visualize() === 'ok', 'reload: commit_e2e_restore installed the persisted sessions');
751
+ const sidAfter = hex(getBin(ro(RA2, '::actor::qa_e2e_active', { cid: RB.cid }), 'sid'));
752
+ ok(sidAfter === sidBefore, 'reload: the SAME e2e session id is restored (not a fresh ratchet)');
753
+
754
+ // THE GAP: B — still on the pre-reload session — sends BEFORE A2 sends anything.
755
+ await mutate(RA2, '::actor::qa_recv_reset', {});
756
+ await mutate(RB, '::a2a_messaging::send_message', { contact: 'RLA', text: 'reload-in-gap' });
757
+ await sleep(4000);
758
+ ok(ro(RA2, '::actor::qa_recv_last', {}).Reduce('text').Visualize() === 'reload-in-gap',
759
+ 'reload: the reloaded packet DECRYPTS B\'s in-gap message on the restored session (page-reload message-loss FIXED)');
760
+ }
761
+
673
762
  console.log('\n================ MIG ================');
674
763
  if (scorecard.length === 0) console.log('MIG: ALL GREEN');
675
764
  else { console.log(`${scorecard.length} FAILURE(S):`); scorecard.forEach((s) => console.log(' ' + s)); }
@@ -216,6 +216,17 @@ application actor loads libraries
216
216
  trn qa_recv_reset _ { qa_recv_text -> "". qa_recv_wire -> "". qa_recv_file -> "". qa_recv_flen -> 0. return transaction::success [ _return_data ($ok -> TRUE) ]. }
217
217
  trn qa_recv_set_abort _:($abort -> ab: bool) { qa_recv_abort -> ab. return transaction::success [ _return_data ($abort -> qa_recv_abort) ]. }
218
218
 
219
+ // ── Full-state persistence (reload port). The host stores the WHOLE export_core_state
220
+ // as ONE opaque field $core (design §3) and replays it on boot; commit_e2e_restore then
221
+ // installs the restored $e2e_sessions. Same shape as test_actor.mu and the mcp host.
222
+ trn readonly export_state _ { return ($core -> (a2a_messaging::export_core_state NIL)). }
223
+ trn import_state data: any
224
+ {
225
+ current_transaction_info::validate_origin_or_abort (transaction::envelope::origin::user,).
226
+ a2a_messaging::import_core_state (data $core).
227
+ return transaction::success [ _return_data ($imported -> TRUE) ].
228
+ }
229
+
219
230
  // Force a LEGACY plaintext box (receive_message_tx) to a contact, BYPASSING e2e_route — lets the
220
231
  // driver deliver a legacy inbound at a receiver regardless of the sender's route, to prove the
221
232
  // §5.7 receive-side downgrade gate: refused at an EPOCH-pinned receiver, delivered at a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ours.network/tg-connector",
3
- "version": "0.1.8-nightly.4",
3
+ "version": "0.1.8-nightly.5",
4
4
  "description": "Bridge Telegram bots to ours proxy nodes — one ADAPT wrapper hosting N messenger packets, each connecting one Telegram bot to one ours node. Run `ours-tg-connector add_new_connection`.",
5
5
  "type": "module",
6
6
  "license": "FSL-1.1-Apache-2.0",