@nekzus/liop 1.2.0-alpha.9 → 1.2.0

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.
Files changed (50) hide show
  1. package/README.md +12 -3
  2. package/dist/bin/agent.js +222 -51
  3. package/dist/bridge/index.js +7 -6
  4. package/dist/bridge/stream.js +11 -11
  5. package/dist/client/index.js +46 -35
  6. package/dist/crypto/logic-image-id.d.ts +3 -0
  7. package/dist/crypto/logic-image-id.js +27 -0
  8. package/dist/crypto/verifier.js +7 -19
  9. package/dist/economy/estimator.d.ts +53 -0
  10. package/dist/economy/estimator.js +69 -0
  11. package/dist/economy/index.d.ts +5 -0
  12. package/dist/economy/index.js +3 -0
  13. package/dist/economy/otel.d.ts +38 -0
  14. package/dist/economy/otel.js +100 -0
  15. package/dist/economy/telemetry.d.ts +77 -0
  16. package/dist/economy/telemetry.js +224 -0
  17. package/dist/errors.d.ts +14 -0
  18. package/dist/errors.js +19 -0
  19. package/dist/gateway/hybrid.d.ts +3 -1
  20. package/dist/gateway/hybrid.js +38 -13
  21. package/dist/gateway/router.d.ts +25 -9
  22. package/dist/gateway/router.js +484 -133
  23. package/dist/index.d.ts +3 -0
  24. package/dist/index.js +3 -0
  25. package/dist/mesh/node.d.ts +16 -0
  26. package/dist/mesh/node.js +394 -113
  27. package/dist/prompts/adapters.d.ts +16 -0
  28. package/dist/prompts/adapters.js +55 -0
  29. package/dist/rpc/proto.js +2 -1
  30. package/dist/rpc/server.d.ts +1 -1
  31. package/dist/rpc/server.js +4 -3
  32. package/dist/rpc/tls.js +3 -2
  33. package/dist/sandbox/wasi.d.ts +1 -1
  34. package/dist/sandbox/wasi.js +43 -3
  35. package/dist/security/guardian.js +3 -2
  36. package/dist/security/zk.d.ts +2 -3
  37. package/dist/security/zk.js +22 -9
  38. package/dist/server/index.d.ts +53 -4
  39. package/dist/server/index.js +362 -49
  40. package/dist/server/pii.d.ts +12 -0
  41. package/dist/server/pii.js +90 -0
  42. package/dist/types.d.ts +16 -0
  43. package/dist/utils/logger.d.ts +21 -0
  44. package/dist/utils/logger.js +70 -0
  45. package/dist/utils/mcpCompact.d.ts +11 -0
  46. package/dist/utils/mcpCompact.js +29 -0
  47. package/dist/workers/logic-execution.d.ts +1 -1
  48. package/dist/workers/logic-execution.js +38 -20
  49. package/dist/workers/zk-verifier.js +37 -33
  50. package/package.json +14 -2
package/dist/mesh/node.js CHANGED
@@ -13,6 +13,13 @@ import { pipe } from "it-pipe";
13
13
  import { createLibp2p } from "libp2p";
14
14
  import { CID } from "multiformats/cid";
15
15
  import { sha256 } from "multiformats/hashes/sha2";
16
+ import { log } from "../utils/logger.js";
17
+ const DEFAULT_BOOTSTRAP_NODES = [
18
+ "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDuVkcruPhcoXdia1vAHm1qrCEYWvmqVkMBjeEbFR",
19
+ "/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
20
+ "/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
21
+ "/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjWZcYW7dwt",
22
+ ];
16
23
  const LIOP_MANIFEST_PROTOCOL = "/liop/manifest/1.0.0";
17
24
  const LIOP_MANIFEST_CAPABILITY = "liop:manifest";
18
25
  /**
@@ -24,6 +31,10 @@ const LIOP_MANIFEST_CAPABILITY = "liop:manifest";
24
31
  export class MeshNode {
25
32
  node = null;
26
33
  config;
34
+ manifestDialFailureState = new Map();
35
+ static MANIFEST_DIAL_BASE_COOLDOWN_MS = 10_000;
36
+ static MANIFEST_DIAL_MAX_COOLDOWN_MS = 2 * 60_000;
37
+ static MANIFEST_DIAL_SKIP_LOG_THROTTLE_MS = 30_000;
27
38
  /**
28
39
  * Buffer of capability hashes that have been announced.
29
40
  * Used to re-announce capabilities when new peers connect
@@ -37,6 +48,9 @@ export class MeshNode {
37
48
  manifestProvider = null;
38
49
  /** Flag to ensure the manifest protocol is only registered once. */
39
50
  manifestProtocolRegistered = false;
51
+ /** Local Ed25519 Private Key for protocol signatures */
52
+ // biome-ignore lint/suspicious/noExplicitAny: libp2p keys type
53
+ localPrivateKey = null;
40
54
  constructor(config = {}) {
41
55
  this.config = {
42
56
  listenAddresses: config.listenAddresses || [
@@ -45,8 +59,39 @@ export class MeshNode {
45
59
  ],
46
60
  bootstrapNodes: config.bootstrapNodes || [],
47
61
  identityPath: config.identityPath,
62
+ enableWAN: config.enableWAN ?? false,
63
+ dhtStoragePath: config.dhtStoragePath,
64
+ addressMapper: config.addressMapper,
48
65
  };
49
66
  }
67
+ shouldSkipManifestDial(peerIdStr) {
68
+ const state = this.manifestDialFailureState.get(peerIdStr);
69
+ if (!state)
70
+ return false;
71
+ const now = Date.now();
72
+ if (now >= state.cooldownUntil)
73
+ return false;
74
+ if (now - state.lastSkipLogAt >
75
+ MeshNode.MANIFEST_DIAL_SKIP_LOG_THROTTLE_MS) {
76
+ log.info(`[LIOP-Mesh] Skipping manifest dial for ${peerIdStr} during cooldown (${Math.ceil((state.cooldownUntil - now) / 1000)}s remaining)`);
77
+ state.lastSkipLogAt = now;
78
+ }
79
+ return true;
80
+ }
81
+ recordManifestDialFailure(peerIdStr) {
82
+ const now = Date.now();
83
+ const prev = this.manifestDialFailureState.get(peerIdStr);
84
+ const failures = (prev?.failures || 0) + 1;
85
+ const backoff = Math.min(MeshNode.MANIFEST_DIAL_BASE_COOLDOWN_MS * 2 ** Math.max(0, failures - 1), MeshNode.MANIFEST_DIAL_MAX_COOLDOWN_MS);
86
+ this.manifestDialFailureState.set(peerIdStr, {
87
+ failures,
88
+ cooldownUntil: now + backoff,
89
+ lastSkipLogAt: 0,
90
+ });
91
+ }
92
+ clearManifestDialFailure(peerIdStr) {
93
+ this.manifestDialFailureState.delete(peerIdStr);
94
+ }
50
95
  /**
51
96
  * Loads a persistent identity from disk or generates a new Ed25519 keypair.
52
97
  * Uses privateKeyToProtobuf/privateKeyFromProtobuf (libp2p v3.x official API).
@@ -65,14 +110,22 @@ export class MeshNode {
65
110
  const data = await fs.readFile(absolutePath, "utf-8");
66
111
  const json = JSON.parse(data);
67
112
  const protobufBytes = uint8arrays.fromString(json.privKey, "base64");
68
- const privateKey = privateKeyFromProtobuf(protobufBytes);
69
- console.error(`[LIOP-Mesh] Loaded persistent identity from ${absolutePath}`);
70
- return { privateKey, isNew: false };
113
+ try {
114
+ const privateKey = privateKeyFromProtobuf(protobufBytes);
115
+ log.info(`[LIOP-Mesh] Loaded persistent identity from ${absolutePath}`);
116
+ return { privateKey, isNew: false };
117
+ }
118
+ catch (parseError) {
119
+ log.error(`[LIOP-Mesh] Persistent identity at ${absolutePath} is invalid or corrupt. Generating new one. Error: ${parseError instanceof Error
120
+ ? parseError.message
121
+ : String(parseError)}`);
122
+ // Fall through to generate new key
123
+ }
71
124
  }
72
125
  catch (error) {
73
126
  const e = error;
74
127
  if (e.code !== "ENOENT") {
75
- console.error(`[LIOP-Mesh] Error loading identity: ${e.message}`);
128
+ log.error(`[LIOP-Mesh] Error loading identity: ${e.message}`);
76
129
  }
77
130
  }
78
131
  }
@@ -80,8 +133,19 @@ export class MeshNode {
80
133
  return { privateKey, isNew: true };
81
134
  }
82
135
  catch (error) {
83
- console.error(`[LIOP-Mesh] Critical error in identity management: ${error}`);
84
- return undefined;
136
+ log.error(`[LIOP-Mesh] Critical error in identity management: ${error}. Falling back to ephemeral identity.`);
137
+ // EPOCH FALLBACK: In extreme cases (corrupt env), use a volatile in-memory identity
138
+ // to allow the node to start and serve traffic.
139
+ try {
140
+ const { generateKeyPair } = (await import("@libp2p/crypto/keys"
141
+ // biome-ignore lint/suspicious/noExplicitAny: libp2p ESM dynamic import type workaround
142
+ ));
143
+ const ephemeralKey = await generateKeyPair("Ed25519");
144
+ return { privateKey: ephemeralKey, isNew: true };
145
+ }
146
+ catch (fallbackError) {
147
+ throw new Error(`Identity system failure: ${fallbackError}`);
148
+ }
85
149
  }
86
150
  }
87
151
  /**
@@ -106,10 +170,10 @@ export class MeshNode {
106
170
  };
107
171
  await fs.mkdir(path.dirname(absolutePath), { recursive: true });
108
172
  await fs.writeFile(absolutePath, JSON.stringify(json, null, 2));
109
- console.error(`[LIOP-Mesh] Identity persisted to ${absolutePath}`);
173
+ log.info(`[LIOP-Mesh] Identity persisted to ${absolutePath}`);
110
174
  }
111
175
  catch (error) {
112
- console.error(`[LIOP-Mesh] FAILED to persist identity: ${error}`);
176
+ log.error(`[LIOP-Mesh] FAILED to persist identity: ${error}`);
113
177
  }
114
178
  }
115
179
  /**
@@ -135,15 +199,15 @@ export class MeshNode {
135
199
  await new Promise((resolve) => setTimeout(resolve, 500));
136
200
  if (!this.node)
137
201
  return;
138
- console.error(`[LIOP-Mesh] Re-announcing ${this.announcedCapabilities.size} capabilities to updated routing table...`);
202
+ log.info(`[LIOP-Mesh] Re-announcing ${this.announcedCapabilities.size} capabilities to updated routing table...`);
139
203
  for (const hash of this.announcedCapabilities) {
140
204
  try {
141
205
  const cid = await this.capabilityToCID(hash);
142
206
  await this.node.contentRouting.provide(cid);
143
- console.error(`[LIOP-Mesh] Re-announced: ${hash}`);
207
+ log.info(`[LIOP-Mesh] Re-announced: ${hash}`);
144
208
  }
145
- catch (e) {
146
- console.error(`[LIOP-Mesh] Re-announce failed for ${hash}: ${e}`);
209
+ catch (_e) {
210
+ log.info(`[LIOP-Mesh] Re-announce failed for ${hash}: ${_e}`);
147
211
  }
148
212
  }
149
213
  }
@@ -152,17 +216,27 @@ export class MeshNode {
152
216
  }
153
217
  }
154
218
  async start() {
219
+ if (this.node)
220
+ return;
155
221
  const result = await this.loadOrCreateIdentity();
156
222
  if (!result)
157
223
  throw new Error("Could not initialize P2P Identity");
158
224
  const { privateKey, isNew } = result;
159
- const discovery = this.config.bootstrapNodes && this.config.bootstrapNodes.length > 0
225
+ this.localPrivateKey = privateKey;
226
+ let bootNodes = this.config.bootstrapNodes || [];
227
+ if (bootNodes.length === 0 && this.config.enableWAN) {
228
+ bootNodes = DEFAULT_BOOTSTRAP_NODES;
229
+ }
230
+ const discovery = bootNodes.length > 0
160
231
  ? [
161
232
  bootstrap({
162
- list: this.config.bootstrapNodes,
233
+ list: bootNodes,
163
234
  }),
164
235
  ]
165
236
  : undefined;
237
+ const dhtProtocol = this.config.enableWAN
238
+ ? "/ipfs/kad/1.0.0"
239
+ : "/ipfs/lan/kad/1.0.0";
166
240
  this.node = await createLibp2p({
167
241
  privateKey,
168
242
  addresses: {
@@ -175,7 +249,7 @@ export class MeshNode {
175
249
  identify: identify(),
176
250
  ping: ping(),
177
251
  dht: kadDHT({
178
- protocol: "/ipfs/lan/kad/1.0.0", // SHIFT TO LAN PROTOCOL!
252
+ protocol: dhtProtocol,
179
253
  clientMode: false,
180
254
  // Allow local/private IPs in the DHT routing table for development/testing
181
255
  allowQueryWithZeroPeers: true,
@@ -188,55 +262,152 @@ export class MeshNode {
188
262
  });
189
263
  // Monitor Connectivity Events
190
264
  this.node.addEventListener("peer:discovery", (evt) => {
191
- console.error(`[LIOP-Mesh] Discovered peer: ${evt.detail.id.toString()}`);
265
+ const peerId = evt.detail.id;
266
+ log.info(`[LIOP-Mesh] Discovered peer: ${peerId.toString()}`);
267
+ // [Phase 104] Auto-dial discovered peers to bypass DHT propagation latency
268
+ if (this.node) {
269
+ // biome-ignore lint/suspicious/noExplicitAny: target polymorphic type
270
+ let dialTarget = peerId;
271
+ // Apply port translation if necessary (Docker -> Windows Host)
272
+ if (this.config.addressMapper && evt.detail.multiaddrs.length > 0) {
273
+ const translated = evt.detail.multiaddrs
274
+ .map((ma) => {
275
+ // biome-ignore lint/style/noNonNullAssertion: mapped conditionally
276
+ const mapped = this.config.addressMapper(ma.toString());
277
+ return mapped ? multiaddr(mapped) : null;
278
+ })
279
+ .filter((t) => t !== null);
280
+ const directTCP = translated.find((ma) => ma.toString().includes("/tcp/") && !ma.toString().includes("/ws"));
281
+ if (directTCP)
282
+ dialTarget = directTCP;
283
+ }
284
+ this.node.dial(dialTarget).catch(() => { });
285
+ }
192
286
  });
193
287
  this.node.addEventListener("peer:connect", (evt) => {
194
288
  const peerId = evt.detail;
195
- console.error(`[LIOP-Mesh] Connected to peer: ${peerId.toString()}`);
289
+ log.info(`[LIOP-Mesh] Connected to peer: ${peerId.toString()}`);
196
290
  if (!this.node)
197
291
  return;
198
292
  // biome-ignore lint/suspicious/noExplicitAny: access internal services
199
293
  const dht = this.node.services.dht;
200
294
  if (dht?.routingTable) {
201
- console.error(`[LIOP-Mesh] Adding ${peerId.toString()} to DHT Routing Table`);
295
+ log.info(`[LIOP-Mesh] Adding ${peerId.toString()} to DHT Routing Table`);
202
296
  dht.routingTable.add(peerId).catch((err) => {
203
- console.error(`[LIOP-Mesh] Failed to add peer to routing table: ${err instanceof Error ? err.message : String(err)}`);
297
+ log.info(`[LIOP-Mesh] Failed to add peer to routing table: ${err instanceof Error ? err.message : String(err)}`);
204
298
  });
205
299
  }
206
300
  // Trigger reactive re-announcement of all capabilities
207
301
  // so that ADD_PROVIDER messages reach the new peer
208
302
  this.reannounceAll().catch((err) => {
209
- console.error(`[LIOP-Mesh] Re-announce error: ${err instanceof Error ? err.message : String(err)}`);
303
+ log.info(`[LIOP-Mesh] Re-announce error: ${err instanceof Error ? err.message : String(err)}`);
210
304
  });
211
305
  });
212
306
  await this.node.start();
307
+ // Load persisted DHT routing table to enable rapid cold-start reconnections
308
+ await this.loadRoutingTable();
213
309
  // [LIOP-ALPHA] Protocols and services setup
214
310
  this.applyHandlers();
215
311
  if (isNew && this.config.identityPath) {
216
312
  await this.saveIdentity(privateKey);
217
313
  }
218
- console.error(`[LIOP-Mesh] Node started with id: ${this.node.peerId.toString()}`);
314
+ log.info(`[LIOP-Mesh] Node started with id: ${this.node.peerId.toString()}`);
219
315
  this.node.getMultiaddrs().forEach((addr) => {
220
- console.error(`[LIOP-Mesh] Listening on: ${addr.toString()}`);
316
+ log.info(`[LIOP-Mesh] Listening on: ${addr.toString()}`);
221
317
  });
222
- // Force explicit dialing of Bootstrap nodes to guarantee topology
223
- if (this.config.bootstrapNodes && this.config.bootstrapNodes.length > 0) {
224
- console.error(`[LIOP-Mesh] Forcing direct P2P dial to ${this.config.bootstrapNodes.length} bootstrap nodes...`);
225
- for (const addr of this.config.bootstrapNodes) {
226
- try {
227
- await this.node.dial(multiaddr(addr));
228
- console.error(`[LIOP-Mesh] Successfully dialed ${addr}`);
229
- }
230
- catch (e) {
231
- console.error(`[LIOP-Mesh] Failed to explicitly dial ${addr}`, e);
318
+ // Force explicit dialing of Bootstrap nodes with bounded backoff
319
+ if (bootNodes.length > 0) {
320
+ log.info(`[LIOP-Mesh] Forcing direct P2P dial to ${bootNodes.length} bootstrap nodes...`);
321
+ const maxRetries = 5;
322
+ for (const addr of bootNodes) {
323
+ let success = false;
324
+ let attempt = 1;
325
+ while (attempt <= maxRetries && !success) {
326
+ try {
327
+ await this.node.dial(multiaddr(addr));
328
+ log.info(`[LIOP-Mesh] ✅ Successfully dialed ${addr}`);
329
+ success = true;
330
+ }
331
+ catch (_e) {
332
+ const delay = Math.min(1000 * 2 ** (attempt - 1), 3000);
333
+ log.warn(`[LIOP-Mesh] ⚠️ Dial attempt ${attempt}/${maxRetries} to ${addr} failed. Retrying in ${delay / 1000}s...`);
334
+ if (attempt < maxRetries) {
335
+ await new Promise((resolve) => setTimeout(resolve, delay));
336
+ }
337
+ else {
338
+ log.error(`[LIOP-Mesh] ❌ Could not connect to bootstrap ${addr} after ${maxRetries} attempts. Continuing...`);
339
+ }
340
+ attempt++;
341
+ }
232
342
  }
233
343
  }
234
344
  }
235
345
  }
236
346
  async stop() {
237
347
  if (this.node) {
348
+ await this.saveRoutingTable();
238
349
  await this.node.stop();
239
- console.error("[LIOP-Mesh] Node stopped");
350
+ log.info("[LIOP-Mesh] Node stopped");
351
+ }
352
+ }
353
+ async loadRoutingTable() {
354
+ if (!this.config.dhtStoragePath || !this.node)
355
+ return;
356
+ try {
357
+ const absolutePath = path.resolve(this.config.dhtStoragePath);
358
+ const data = await fs.readFile(absolutePath, "utf-8");
359
+ const peers = JSON.parse(data);
360
+ const { peerIdFromString } = await import("@libp2p/peer-id");
361
+ let loadedCount = 0;
362
+ for (const peer of peers) {
363
+ if (!peer.id || !peer.addresses)
364
+ continue;
365
+ try {
366
+ const peerId = peerIdFromString(peer.id);
367
+ const addrs = peer.addresses.map((a) => multiaddr(a));
368
+ // @ts-expect-error: libp2p version drift workaround
369
+ await this.node.peerStore.save(peerId, { multiaddrs: addrs });
370
+ // Pre-seed DHT routing table
371
+ // biome-ignore lint/suspicious/noExplicitAny: Internal service access
372
+ const dht = this.node.services.dht;
373
+ if (dht?.routingTable) {
374
+ dht.routingTable.add(peerId).catch(() => { });
375
+ }
376
+ loadedCount++;
377
+ }
378
+ catch (_e) { }
379
+ }
380
+ log.info(`[LIOP-Mesh] Loaded ${loadedCount} peers from DHT storage`);
381
+ }
382
+ catch (error) {
383
+ const e = error;
384
+ if (e.code !== "ENOENT") {
385
+ log.error(`[LIOP-Mesh] Failed to load DHT table: ${e.message}`);
386
+ }
387
+ }
388
+ }
389
+ async saveRoutingTable() {
390
+ if (!this.config.dhtStoragePath || !this.node)
391
+ return;
392
+ try {
393
+ const absolutePath = path.resolve(this.config.dhtStoragePath);
394
+ const allPeers = await this.node.peerStore.all();
395
+ const peersToSave = [];
396
+ for (const peer of allPeers) {
397
+ if (peer.addresses.length > 0) {
398
+ peersToSave.push({
399
+ id: peer.id.toString(),
400
+ // biome-ignore lint/suspicious/noExplicitAny: internal libp2p addr
401
+ addresses: peer.addresses.map((a) => a.multiaddr.toString()),
402
+ });
403
+ }
404
+ }
405
+ await fs.mkdir(path.dirname(absolutePath), { recursive: true });
406
+ await fs.writeFile(absolutePath, JSON.stringify(peersToSave, null, 2));
407
+ log.info(`[LIOP-Mesh] Saved ${peersToSave.length} peers to DHT storage`);
408
+ }
409
+ catch (error) {
410
+ log.error(`[LIOP-Mesh] FAILED to save DHT routing table: ${error}`);
240
411
  }
241
412
  }
242
413
  /**
@@ -251,21 +422,24 @@ export class MeshNode {
251
422
  this.manifestProtocolRegistered = true;
252
423
  // Announce manifest capability to the Mesh DHT for discovery
253
424
  this.announceCapability(LIOP_MANIFEST_CAPABILITY).catch((err) => {
254
- console.error(`[LIOP-Mesh] Initial manifest announcement failed: ${err}`);
425
+ log.info(`[LIOP-Mesh] Initial manifest announcement failed: ${err}`);
255
426
  });
256
- // libp2p v1.x/v3.x handle API uses { stream, connection }
427
+ // libp2p v3.x: handler receives (stream, connection) as separate args
257
428
  this.node.handle(LIOP_MANIFEST_PROTOCOL,
258
- // biome-ignore lint/suspicious/noExplicitAny: libp2p v1.x/v3.x polymorphic handler
259
- async (arg, connection) => {
260
- const stream = arg.stream || arg; // Robust extraction
261
- const remotePeer = (arg.connection || connection)?.remotePeer?.toString() || "unknown";
262
- console.error(`[LIOP-Mesh] Incoming manifest request from ${remotePeer}.`);
429
+ // biome-ignore lint/suspicious/noExplicitAny: libp2p v3.x stream/connection types
430
+ async (streamArg, connectionArg) => {
431
+ // v3.x passes (stream, connection); v1.x passed ({ stream, connection })
432
+ const stream = streamArg?.stream ?? streamArg;
433
+ const conn = streamArg?.connection ?? connectionArg;
434
+ const remotePeer = conn?.remotePeer?.toString() || "unknown";
435
+ log.info(`[LIOP-Mesh] Incoming manifest request from ${remotePeer}.`);
263
436
  try {
264
437
  const manifest = this.manifestProvider?.();
265
438
  if (!manifest || !stream) {
266
- console.error(`[LIOP-Mesh] Skipping manifest request (no provider or stream)`);
439
+ log.info(`[LIOP-Mesh] Skipping manifest request (no provider or stream)`);
267
440
  try {
268
- await (stream.close || stream.abort)?.();
441
+ if (typeof stream?.close === "function")
442
+ await stream.close();
269
443
  }
270
444
  catch (_e) { }
271
445
  return;
@@ -276,37 +450,33 @@ export class MeshNode {
276
450
  const lengthBuf = Buffer.alloc(4);
277
451
  lengthBuf.writeUInt32BE(payload.length, 0);
278
452
  const fullPacket = Buffer.concat([lengthBuf, Buffer.from(payload)]);
279
- console.error(`[LIOP-Mesh] Serving manifest (${fullPacket.length} bytes) to ${remotePeer} [Tools: ${manifest.tools.map((t) => t.name).join(", ")}]`);
453
+ log.info(`[LIOP-Mesh] Serving manifest (${fullPacket.length} bytes) to ${remotePeer} [Tools: ${manifest.tools.map((t) => t.name).join(", ")}]`);
280
454
  try {
281
- // Modern libp2p (v1.x/v3.0+) uses stream.send() for writing
455
+ // libp2p v3.x: stream.send() for writing
282
456
  if (typeof stream.send === "function") {
283
- if (!stream.send(fullPacket)) {
284
- // Handle backpressure
285
- const { pEvent } = await import("p-event");
457
+ const accepted = stream.send(fullPacket);
458
+ if (!accepted && typeof stream.onDrain === "function") {
286
459
  try {
287
- await pEvent(stream, "drain", { timeout: 5000 });
460
+ await stream.onDrain({ signal: AbortSignal.timeout(5000) });
288
461
  }
289
- catch (e) {
290
- console.error(`[LIOP-Mesh] WARN: Drain timeout or error for ${remotePeer}: ${e instanceof Error ? e.message : String(e)}`);
462
+ catch (drainErr) {
463
+ log.info(`[LIOP-Mesh] WARN: Drain timeout for ${remotePeer}: ${drainErr instanceof Error ? drainErr.message : String(drainErr)}`);
291
464
  }
292
465
  }
293
466
  }
294
467
  else {
295
- // Legacy fallback for older libp2p or custom wrappers
468
+ // Fallback for environments where stream.send is not available
296
469
  await pipe([fullPacket], stream);
297
470
  }
298
- console.error(`[LIOP-Mesh] Manifest sent successfully to ${remotePeer}`);
471
+ log.info(`[LIOP-Mesh] Manifest sent successfully to ${remotePeer}`);
299
472
  }
300
473
  catch (writeErr) {
301
- console.error(`[LIOP-Mesh] Write error serving manifest to ${remotePeer}: ${writeErr instanceof Error ? writeErr.message : String(writeErr)}`);
474
+ log.info(`[LIOP-Mesh] Write error serving manifest to ${remotePeer}: ${writeErr instanceof Error ? writeErr.message : String(writeErr)}`);
302
475
  }
303
476
  finally {
304
- // Ensure the stream is closed after serving the manifest
305
477
  try {
306
478
  if (typeof stream.close === "function")
307
479
  await stream.close();
308
- else if (typeof stream.abort === "function")
309
- await stream.abort();
310
480
  }
311
481
  catch (_e) {
312
482
  // Ignore close errors
@@ -315,10 +485,10 @@ export class MeshNode {
315
485
  return;
316
486
  }
317
487
  catch (err) {
318
- console.error(`[LIOP-Mesh] Error serving manifest to ${remotePeer}: ${err instanceof Error ? err.message : String(err)}`);
488
+ log.info(`[LIOP-Mesh] Error serving manifest to ${remotePeer}: ${err instanceof Error ? err.message : String(err)}`);
319
489
  }
320
490
  });
321
- console.error(`[LIOP-Mesh] Manifest Protocol registered: ${LIOP_MANIFEST_PROTOCOL}`);
491
+ log.info(`[LIOP-Mesh] Manifest Protocol registered: ${LIOP_MANIFEST_PROTOCOL}`);
322
492
  }
323
493
  /**
324
494
  * Registers a callback as the manifest provider.
@@ -340,9 +510,12 @@ export class MeshNode {
340
510
  // [ALPHA-OPTIMIZATION] Local Loopback Bypass
341
511
  // If we are querying our own manifest, return it directly from the provider.
342
512
  if (peerIdStr === this.node.peerId.toString()) {
343
- console.error(`[LIOP-Mesh] Loopback: Returning local manifest directly for ${peerIdStr}`);
513
+ log.info(`[LIOP-Mesh] Loopback: Returning local manifest directly for ${peerIdStr}`);
344
514
  return this.manifestProvider?.() || null;
345
515
  }
516
+ if (this.shouldSkipManifestDial(peerIdStr)) {
517
+ return null;
518
+ }
346
519
  const MAX_ATTEMPTS = 3;
347
520
  for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
348
521
  try {
@@ -354,44 +527,93 @@ export class MeshNode {
354
527
  targetPeer = activeConn.remotePeer;
355
528
  }
356
529
  else {
357
- // Fallback to string parsing if not connected yet
358
- const { peerIdFromString } = await import("@libp2p/peer-id");
359
- targetPeer = peerIdFromString(peerIdStr);
530
+ // Fallback: search peerStore to find a valid PeerId object that libp2p understands natively
531
+ const allPeers = await this.node.peerStore.all();
532
+ const stored = allPeers.find((p) => p.id.toString() === peerIdStr);
533
+ if (stored) {
534
+ targetPeer = stored.id;
535
+ }
536
+ else {
537
+ // Final fallback parsing.
538
+ // [LIOP-CAUTION] This is where the toMultihash error usually triggers if libp2p version drift exists.
539
+ const { peerIdFromString } = await import("@libp2p/peer-id");
540
+ targetPeer = peerIdFromString(peerIdStr);
541
+ }
542
+ }
543
+ // [LIOP-PORT-TRANSLATION] If an address mapper is configured (e.g. in the Host Agent),
544
+ // ensure the targetPeer's addresses are translated before libp2p attempts to dial.
545
+ const dialTargetFromPeer = targetPeer;
546
+ let dialTarget = dialTargetFromPeer;
547
+ if (this.config.addressMapper && this.node) {
548
+ const mapper = this.config.addressMapper;
549
+ const peer = await this.node.peerStore.get(targetPeer);
550
+ if (peer && peer.addresses.length > 0) {
551
+ const translated = peer.addresses
552
+ .map((oa) => {
553
+ const original = oa.multiaddr.toString();
554
+ const mapped = mapper(original);
555
+ if (!mapped)
556
+ return null;
557
+ return {
558
+ isCertified: oa.isCertified,
559
+ multiaddr: multiaddr(mapped),
560
+ };
561
+ })
562
+ .filter((t) => t !== null);
563
+ // Strategy: Force direct dial to the first translated TCP address to bypass DHT routing delays
564
+ const directTCP = translated.find((t) => t.multiaddr.toString().includes("/tcp/") &&
565
+ !t.multiaddr.toString().includes("/ws"));
566
+ if (directTCP) {
567
+ dialTarget = directTCP.multiaddr;
568
+ log.info(`[LIOP-Mesh] ⚡ Direct dial to translated addr: ${dialTarget.toString()}`);
569
+ }
570
+ // Update the peerStore so subsequent dials also use the right path
571
+ // biome-ignore lint/suspicious/noExplicitAny: access internal peerStore
572
+ await this.node.peerStore.save(targetPeer, {
573
+ multiaddrs: translated.map((t) => t.multiaddr),
574
+ });
575
+ }
360
576
  }
361
577
  // Open a protocol stream using high-level dialProtocol for automated it-stream wrapping
362
578
  // biome-ignore lint/suspicious/noExplicitAny: stream type varies by transport
363
579
  let stream;
364
580
  try {
365
- // biome-ignore lint/suspicious/noExplicitAny: complex libp2p dial types
366
- const result = await this.node.dialProtocol(
367
- // biome-ignore lint/suspicious/noExplicitAny: PeerId type mismatch
368
- targetPeer, LIOP_MANIFEST_PROTOCOL);
581
+ // biome-ignore lint/suspicious/noExplicitAny: libp2p returns polymorphic dialProtocol result
582
+ const result = await this.node
583
+ .dialProtocol(dialTarget, LIOP_MANIFEST_PROTOCOL)
584
+ .catch((e) => {
585
+ // Catch specific TypeError that breaks the loop
586
+ if (String(e).includes("toMultihash")) {
587
+ throw new Error("INCOMPATIBLE_PEER_ID_INTERFACE");
588
+ }
589
+ throw e;
590
+ });
369
591
  stream = result.stream || result;
370
592
  }
371
593
  catch (dialErr) {
372
594
  if (attempt === MAX_ATTEMPTS) {
373
- console.error(`[LIOP-Mesh] Dial error for ${peerIdStr} after ${MAX_ATTEMPTS} attempts: ${dialErr}`);
595
+ log.info(`[LIOP-Mesh] Dial error for ${peerIdStr} after ${MAX_ATTEMPTS} attempts: ${dialErr}`);
374
596
  return null;
375
597
  }
376
598
  const delay = 500 * 2 ** attempt;
377
- console.error(`[LIOP-Mesh] Dial error for ${peerIdStr} (Attempt ${attempt}). Retrying in ${delay}ms...`);
599
+ log.info(`[LIOP-Mesh] Dial error for ${peerIdStr} (Attempt ${attempt}). Retrying in ${delay}ms...`);
378
600
  await new Promise((r) => setTimeout(r, delay));
379
601
  continue;
380
602
  }
381
- // Strategy: Robust Async Reader
382
- let source = stream.source ||
603
+ // libp2p v3.x: stream IS the AsyncIterable<Uint8Array>
604
+ // v1.x had stream.source; v3.x has the stream itself as iterable
605
+ const source = stream.source ??
383
606
  (typeof stream[Symbol.asyncIterator] === "function" ? stream : null);
384
- // Final attempt: check if it's already an iterable
385
- if (!source && typeof stream[Symbol.asyncIterator] === "function") {
386
- source = stream;
387
- }
388
607
  if (!source) {
389
- throw new Error("Target stream has no source (AsyncIterable)");
608
+ throw new Error("Target stream has no AsyncIterable source");
390
609
  }
391
610
  const chunks = [];
392
- // Read segments until timeout or closure
611
+ let totalReceived = 0;
612
+ let expectedPayloadLength = -1;
613
+ // Read length-prefixed manifest: first 4 bytes = payload length (BE)
614
+ let manifestTimeoutId;
393
615
  const timeoutPromise = new Promise((_, reject) => {
394
- setTimeout(() => reject(new Error("Manifest read timeout (1.5s)")), 1500);
616
+ manifestTimeoutId = setTimeout(() => reject(new Error("Manifest read timeout (5.0s)")), 5000);
395
617
  });
396
618
  try {
397
619
  await Promise.race([
@@ -399,18 +621,30 @@ export class MeshNode {
399
621
  for await (const chunk of source) {
400
622
  if (!chunk)
401
623
  continue;
402
- // Telemetry: inspect chunk structure
403
- const bytes = chunk instanceof Uint8Array
404
- ? chunk
405
- : // biome-ignore lint/suspicious/noExplicitAny: chunks can be Buffer/Uint8Array hybrids
406
- chunk.subarray
407
- ? // biome-ignore lint/suspicious/noExplicitAny: chunks can be Buffer/Uint8Array hybrids
408
- chunk.subarray()
409
- : // biome-ignore lint/suspicious/noExplicitAny: chunks can be Buffer/Uint8Array hybrids
410
- Buffer.from(chunk);
624
+ // libp2p streams yield Uint8ArrayList (from uint8arraylist package)
625
+ // which reports .length correctly but Buffer.from() produces zeros.
626
+ // .subarray() returns a flat contiguous Uint8Array with actual data.
627
+ const raw =
628
+ // biome-ignore lint/suspicious/noExplicitAny: Uint8ArrayList type guard
629
+ typeof chunk.subarray === "function"
630
+ ? chunk.subarray()
631
+ : chunk instanceof Uint8Array
632
+ ? chunk
633
+ : new Uint8Array(0);
634
+ const bytes = Buffer.from(raw.buffer, raw.byteOffset, raw.byteLength);
411
635
  if (bytes.length > 0) {
412
- console.error(`[LIOP-Mesh] Received chunk (${bytes.length} bytes) from ${peerIdStr}`);
413
636
  chunks.push(bytes);
637
+ totalReceived += bytes.length;
638
+ // Extract expected length from the first 4 bytes once available
639
+ if (expectedPayloadLength < 0 && totalReceived >= 4) {
640
+ const header = Buffer.concat(chunks);
641
+ expectedPayloadLength = header.readUInt32BE(0);
642
+ }
643
+ // Stop reading once we have the full payload (4 prefix + N payload)
644
+ if (expectedPayloadLength >= 0 &&
645
+ totalReceived >= 4 + expectedPayloadLength) {
646
+ break;
647
+ }
414
648
  }
415
649
  }
416
650
  })(),
@@ -420,25 +654,32 @@ export class MeshNode {
420
654
  catch (itErr) {
421
655
  if (chunks.length === 0)
422
656
  throw itErr;
423
- console.error(`[LIOP-Mesh] Partial manifest read from ${peerIdStr}: ${itErr instanceof Error ? itErr.message : String(itErr)}`);
657
+ log.info(`[LIOP-Mesh] Partial manifest read from ${peerIdStr}: ${itErr instanceof Error ? itErr.message : String(itErr)}`);
658
+ }
659
+ finally {
660
+ if (manifestTimeoutId)
661
+ clearTimeout(manifestTimeoutId);
424
662
  }
425
663
  const raw = Buffer.concat(chunks);
426
664
  if (raw.length < 4) {
427
665
  throw new Error("Received empty/invalid manifest (too short)");
428
666
  }
429
- // Skip length prefix (4 bytes)
430
- const jsonStr = raw.subarray(4).toString("utf-8");
667
+ // Use the length prefix to extract exactly the expected JSON
668
+ const declaredLen = raw.readUInt32BE(0);
669
+ const jsonStr = raw.subarray(4, 4 + declaredLen).toString("utf-8");
431
670
  const manifest = JSON.parse(jsonStr);
432
- console.error(`[LIOP-Mesh] Received manifest from ${peerIdStr}: ${manifest.tools.length} tools`);
671
+ log.info(`[LIOP-Mesh] Received manifest from ${peerIdStr}: ${manifest.tools.length} tools`);
672
+ this.clearManifestDialFailure(peerIdStr);
433
673
  return manifest;
434
674
  }
435
675
  catch (err) {
436
676
  if (attempt === MAX_ATTEMPTS) {
437
- console.error(`[LIOP-Mesh] Failed to query manifest from ${peerIdStr} after ${MAX_ATTEMPTS} attempts: ${err instanceof Error ? err.message : String(err)}`);
677
+ this.recordManifestDialFailure(peerIdStr);
678
+ log.info(`[LIOP-Mesh] Failed to query manifest from ${peerIdStr} after ${MAX_ATTEMPTS} attempts: ${err instanceof Error ? err.message : String(err)}`);
438
679
  return null;
439
680
  }
440
681
  const delay = 500 * 2 ** attempt;
441
- console.error(`[LIOP-Mesh] Query error for ${peerIdStr} (Attempt ${attempt}): ${err instanceof Error ? err.message : String(err)}. Retrying in ${delay}ms...`);
682
+ log.info(`[LIOP-Mesh] Query error for ${peerIdStr} (Attempt ${attempt}): ${err instanceof Error ? err.message : String(err)}. Retrying in ${delay}ms...`);
442
683
  await new Promise((r) => setTimeout(r, delay));
443
684
  }
444
685
  }
@@ -472,6 +713,13 @@ export class MeshNode {
472
713
  throw new Error("Mesh Node is not running");
473
714
  return this.node.peerId.toString();
474
715
  }
716
+ async sign(data) {
717
+ if (!this.localPrivateKey) {
718
+ throw new Error("Local identity not loaded or initialized");
719
+ }
720
+ // libp2p private key implementations typically return a Promise<Uint8Array> or Uint8Array
721
+ return Buffer.from(await this.localPrivateKey.sign(data));
722
+ }
475
723
  getMultiaddrs() {
476
724
  if (!this.node)
477
725
  throw new Error("Mesh Node is not running");
@@ -484,21 +732,21 @@ export class MeshNode {
484
732
  this.announcedCapabilities.add(hash);
485
733
  try {
486
734
  const cid = await this.capabilityToCID(hash);
487
- console.error(`[LIOP-Mesh] Announcing capability: ${hash} (CID: ${cid.toString()})`);
735
+ log.info(`[LIOP-Mesh] Announcing capability: ${hash} (CID: ${cid.toString()})`);
488
736
  // In libp2p v1.x, contentRouting.provide returns Promise<void>
489
737
  await this.node.contentRouting.provide(cid);
490
- console.error(`[LIOP-Mesh] Successfully announced capability: ${hash}`);
738
+ log.info(`[LIOP-Mesh] Successfully announced capability: ${hash}`);
491
739
  // [DEV-ONLY] Self-verification
492
740
  const selfId = this.node.peerId.toString();
493
741
  for await (const peer of this.node.contentRouting.findProviders(cid)) {
494
742
  if (peer.id.toString() === selfId) {
495
- console.error(`[LIOP-Mesh] Self-verification success: Node is providing ${hash}`);
743
+ log.info(`[LIOP-Mesh] Self-verification success: Node is providing ${hash}`);
496
744
  break;
497
745
  }
498
746
  }
499
747
  }
500
748
  catch (error) {
501
- console.error(`[LIOP-Mesh] Failed to announce capability: ${error}`);
749
+ log.error(`[LIOP-Mesh] Failed to announce capability: ${error}`);
502
750
  }
503
751
  }
504
752
  async findProviders(hash) {
@@ -507,36 +755,69 @@ export class MeshNode {
507
755
  const providers = [];
508
756
  try {
509
757
  const cid = await this.capabilityToCID(hash);
510
- console.error(`[LIOP-Mesh] Querying DHT for ${hash} (CID: ${cid.toString()})...`);
511
- // In libp2p v1.x, contentRouting.findProviders returns AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>
758
+ log.info(`[LIOP-Mesh] Querying DHT for ${hash} (CID: ${cid.toString()})...`);
512
759
  let foundAny = false;
513
- for await (const peer of this.node.contentRouting.findProviders(cid)) {
514
- foundAny = true;
515
- const peerId = peer.id.toString();
516
- console.error(`[LIOP-Mesh] Found provider: ${peerId}`);
517
- if (!providers.includes(peerId)) {
518
- providers.push(peerId);
760
+ // Phase 103: Adaptive Tail-Wait Polling for DHT Discovery
761
+ const connections = this.node.getConnections?.()?.length || 0;
762
+ const idleTimeoutMs = connections > 1 ? 1500 : 3000;
763
+ log.info(`[LIOP-Mesh] Starting DHT search with intelligent idle-timeout of ${idleTimeoutMs}ms (Active connections: ${connections})`);
764
+ // We manually iterate the AsyncIterable to abort it via Promise.race
765
+ const iterator = this.node.contentRouting
766
+ .findProviders(cid)[Symbol.asyncIterator]();
767
+ let isDone = false;
768
+ while (!isDone) {
769
+ const nextPromise = iterator.next();
770
+ const timeoutPromise = new Promise((resolve) => setTimeout(() => resolve({ timeout: true }), idleTimeoutMs));
771
+ try {
772
+ const result = await Promise.race([nextPromise, timeoutPromise]);
773
+ if (result && typeof result === "object" && "timeout" in result) {
774
+ log.info(`[LIOP-Mesh] DHT discovery idle-timeout reached. Stopping search early.`);
775
+ if (typeof iterator.return === "function") {
776
+ // Fire-and-forget: Kademlia iterators can block for 30s on return()
777
+ iterator.return().catch(() => { });
778
+ }
779
+ isDone = true;
780
+ break;
781
+ }
782
+ // biome-ignore lint/suspicious/noExplicitAny: polymorphic Kademlia peer result
783
+ const itResult = result;
784
+ if (itResult.done) {
785
+ isDone = true;
786
+ break;
787
+ }
788
+ foundAny = true;
789
+ const peer = itResult.value;
790
+ const peerId = peer.id.toString();
791
+ log.info(`[LIOP-Mesh] Found provider: ${peerId}`);
792
+ if (!providers.includes(peerId)) {
793
+ providers.push(peerId);
794
+ }
795
+ }
796
+ catch (e) {
797
+ log.warn(`[LIOP-Mesh] DHT iteration error: ${e instanceof Error ? e.message : String(e)}`);
798
+ isDone = true;
799
+ break;
519
800
  }
520
801
  }
521
802
  if (!foundAny) {
522
803
  const services = this.node.services;
523
804
  const dhtSize = services.dht?.routingTable?.size || 0;
524
- console.error(`[LIOP-Mesh] DHT search for ${hash} returned zero results (routing table size: ${dhtSize})`);
805
+ log.info(`[LIOP-Mesh] DHT search for ${hash} returned zero results (routing table size: ${dhtSize})`);
525
806
  }
526
807
  // [DEVELOPER-EXPERIENCE] Local Loopback Discovery
527
808
  // If we are providing this capability, ensure we find ourselves even if DHT findProviders doesn't return us.
528
809
  if (this.announcedCapabilities.has(hash)) {
529
810
  const selfId = this.node.peerId.toString();
530
811
  if (!providers.includes(selfId)) {
531
- console.error(`[LIOP-Mesh] Including local node (${selfId}) in results for ${hash}`);
812
+ log.info(`[LIOP-Mesh] Including local node (${selfId}) in results for ${hash}`);
532
813
  providers.push(selfId);
533
814
  }
534
815
  }
535
816
  }
536
817
  catch (error) {
537
- console.error(`[LIOP-Mesh] Error finding providers for ${hash}: ${error instanceof Error ? error.message : String(error)}`);
818
+ log.info(`[LIOP-Mesh] Error finding providers for ${hash}: ${error instanceof Error ? error.message : String(error)}`);
538
819
  }
539
- console.error(`[LIOP-Mesh] DHT search for ${hash} finished. Found ${providers.length} providers.`);
820
+ log.info(`[LIOP-Mesh] DHT search for ${hash} finished. Found ${providers.length} providers.`);
540
821
  return providers;
541
822
  }
542
823
  async resolvePeer(peerIdStr) {
@@ -548,7 +829,7 @@ export class MeshNode {
548
829
  for (const conn of connections) {
549
830
  if (conn.remotePeer.toString() === peerIdStr) {
550
831
  const remoteAddr = conn.remoteAddr.toString();
551
- console.error(`[LIOP-Mesh] Resolved peer ${peerIdStr} via active connection: ${remoteAddr}`);
832
+ log.info(`[LIOP-Mesh] Resolved peer ${peerIdStr} via active connection: ${remoteAddr}`);
552
833
  return [remoteAddr];
553
834
  }
554
835
  }
@@ -558,14 +839,14 @@ export class MeshNode {
558
839
  if (peer.id.toString() === peerIdStr && peer.addresses.length > 0) {
559
840
  // biome-ignore lint/suspicious/noExplicitAny: Internal libp2p addr type
560
841
  const addrs = peer.addresses.map((a) => a.multiaddr.toString());
561
- console.error(`[LIOP-Mesh] Resolved peer ${peerIdStr} via peerStore: ${addrs[0]}`);
842
+ log.info(`[LIOP-Mesh] Resolved peer ${peerIdStr} via peerStore: ${addrs[0]}`);
562
843
  return addrs;
563
844
  }
564
845
  }
565
- console.error(`[LIOP-Mesh] Peer ${peerIdStr} not found in connections or peerStore`);
846
+ log.info(`[LIOP-Mesh] Peer ${peerIdStr} not found in connections or peerStore`);
566
847
  }
567
848
  catch (error) {
568
- console.error(`[LIOP-Mesh] Failed to resolve peer ${peerIdStr}: ${error}`);
849
+ log.info(`[LIOP-Mesh] Failed to resolve peer ${peerIdStr}: ${error}`);
569
850
  }
570
851
  return [];
571
852
  }