@nekzus/liop 2.0.0-alpha.1 → 2.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/dist/bin/agent.d.ts +0 -1
  2. package/dist/bin/agent.js +5 -306
  3. package/dist/bin/agent.js.map +1 -0
  4. package/dist/{bridge/stream.d.ts → bridge.d.ts} +44 -3
  5. package/dist/bridge.js +2 -0
  6. package/dist/bridge.js.map +1 -0
  7. package/dist/chunk-4ABAFG44.js +33 -0
  8. package/dist/chunk-4ABAFG44.js.map +1 -0
  9. package/dist/chunk-ANFXJGMP.js +2 -0
  10. package/dist/chunk-ANFXJGMP.js.map +1 -0
  11. package/dist/chunk-DBXGYHKY.js +2 -0
  12. package/dist/chunk-DBXGYHKY.js.map +1 -0
  13. package/dist/chunk-HM77MWB6.js +2 -0
  14. package/dist/chunk-HM77MWB6.js.map +1 -0
  15. package/dist/chunk-HNDVAKEK.js +24 -0
  16. package/dist/chunk-HNDVAKEK.js.map +1 -0
  17. package/dist/chunk-HQZHZM6U.js +2 -0
  18. package/dist/chunk-HQZHZM6U.js.map +1 -0
  19. package/dist/chunk-P52IE4L6.js +2 -0
  20. package/dist/chunk-P52IE4L6.js.map +1 -0
  21. package/dist/chunk-PIBCW4BD.js +13 -0
  22. package/dist/chunk-PIBCW4BD.js.map +1 -0
  23. package/dist/chunk-PPCOS2NU.js +2 -0
  24. package/dist/chunk-PPCOS2NU.js.map +1 -0
  25. package/dist/chunk-RWRRBYG4.js +2 -0
  26. package/dist/chunk-RWRRBYG4.js.map +1 -0
  27. package/dist/chunk-S6RJHZV2.js +2 -0
  28. package/dist/chunk-S6RJHZV2.js.map +1 -0
  29. package/dist/chunk-UVTEJYHN.js +2 -0
  30. package/dist/chunk-UVTEJYHN.js.map +1 -0
  31. package/dist/chunk-X6FJATUE.js +29 -0
  32. package/dist/chunk-X6FJATUE.js.map +1 -0
  33. package/dist/chunk-XLVRRGOX.js +3 -0
  34. package/dist/chunk-XLVRRGOX.js.map +1 -0
  35. package/dist/client.d.ts +5 -0
  36. package/dist/client.js +2 -0
  37. package/dist/client.js.map +1 -0
  38. package/dist/{gateway/router.d.ts → gateway.d.ts} +30 -5
  39. package/dist/gateway.js +2 -0
  40. package/dist/gateway.js.map +1 -0
  41. package/dist/{client/index.d.ts → index-CyxNLlz7.d.ts} +24 -5
  42. package/dist/index.d.ts +313 -12
  43. package/dist/index.js +31 -12
  44. package/dist/index.js.map +1 -0
  45. package/dist/kyber-2WDOTUQX.js +2 -0
  46. package/dist/kyber-2WDOTUQX.js.map +1 -0
  47. package/dist/{mesh/node.d.ts → mesh.d.ts} +5 -3
  48. package/dist/mesh.js +2 -0
  49. package/dist/mesh.js.map +1 -0
  50. package/dist/{server/index.d.ts → server.d.ts} +125 -12
  51. package/dist/server.js +2 -0
  52. package/dist/server.js.map +1 -0
  53. package/dist/types.d.ts +17 -14
  54. package/dist/types.js +2 -26
  55. package/dist/types.js.map +1 -0
  56. package/dist/{crypto/verifier.d.ts → verifier-DTCD9imJ.d.ts} +3 -1
  57. package/dist/verifier-RQRYXA4C.js +2 -0
  58. package/dist/verifier-RQRYXA4C.js.map +1 -0
  59. package/dist/workers/logic-execution.d.ts +4 -2
  60. package/dist/workers/logic-execution.js +2 -123
  61. package/dist/workers/logic-execution.js.map +1 -0
  62. package/dist/workers/zk-verifier.d.ts +4 -2
  63. package/dist/workers/zk-verifier.js +2 -98
  64. package/dist/workers/zk-verifier.js.map +1 -0
  65. package/package.json +31 -18
  66. package/dist/bridge/index.d.ts +0 -37
  67. package/dist/bridge/index.js +0 -249
  68. package/dist/bridge/stream.js +0 -210
  69. package/dist/client/index.js +0 -275
  70. package/dist/crypto/logic-image-id.d.ts +0 -3
  71. package/dist/crypto/logic-image-id.js +0 -27
  72. package/dist/crypto/verifier.js +0 -97
  73. package/dist/economy/estimator.d.ts +0 -53
  74. package/dist/economy/estimator.js +0 -69
  75. package/dist/economy/index.d.ts +0 -5
  76. package/dist/economy/index.js +0 -3
  77. package/dist/economy/otel.d.ts +0 -38
  78. package/dist/economy/otel.js +0 -100
  79. package/dist/economy/telemetry.d.ts +0 -77
  80. package/dist/economy/telemetry.js +0 -224
  81. package/dist/errors.d.ts +0 -14
  82. package/dist/errors.js +0 -19
  83. package/dist/gateway/hybrid.d.ts +0 -23
  84. package/dist/gateway/hybrid.js +0 -199
  85. package/dist/gateway/router.js +0 -1054
  86. package/dist/mesh/index.d.ts +0 -1
  87. package/dist/mesh/index.js +0 -1
  88. package/dist/mesh/node.js +0 -853
  89. package/dist/prompts/adapters.d.ts +0 -16
  90. package/dist/prompts/adapters.js +0 -55
  91. package/dist/rpc/client.d.ts +0 -22
  92. package/dist/rpc/client.js +0 -40
  93. package/dist/rpc/codec/lpm.d.ts +0 -20
  94. package/dist/rpc/codec/lpm.js +0 -36
  95. package/dist/rpc/crypto/aes.d.ts +0 -22
  96. package/dist/rpc/crypto/aes.js +0 -47
  97. package/dist/rpc/crypto/kyber.d.ts +0 -27
  98. package/dist/rpc/crypto/kyber.js +0 -70
  99. package/dist/rpc/proto.d.ts +0 -2
  100. package/dist/rpc/proto.js +0 -33
  101. package/dist/rpc/server.d.ts +0 -13
  102. package/dist/rpc/server.js +0 -50
  103. package/dist/rpc/tls.d.ts +0 -26
  104. package/dist/rpc/tls.js +0 -54
  105. package/dist/rpc/types.d.ts +0 -28
  106. package/dist/rpc/types.js +0 -5
  107. package/dist/sandbox/guardian.d.ts +0 -18
  108. package/dist/sandbox/guardian.js +0 -58
  109. package/dist/sandbox/wasi.d.ts +0 -36
  110. package/dist/sandbox/wasi.js +0 -233
  111. package/dist/security/guardian.d.ts +0 -22
  112. package/dist/security/guardian.js +0 -52
  113. package/dist/security/zk.d.ts +0 -37
  114. package/dist/security/zk.js +0 -76
  115. package/dist/server/index.js +0 -1047
  116. package/dist/server/ner-scanner.d.ts +0 -29
  117. package/dist/server/ner-scanner.js +0 -141
  118. package/dist/server/pii.d.ts +0 -66
  119. package/dist/server/pii.js +0 -428
  120. package/dist/utils/logger.d.ts +0 -21
  121. package/dist/utils/logger.js +0 -70
  122. package/dist/utils/mcpCompact.d.ts +0 -11
  123. package/dist/utils/mcpCompact.js +0 -29
package/dist/mesh/node.js DELETED
@@ -1,853 +0,0 @@
1
- import * as fs from "node:fs/promises";
2
- import * as path from "node:path";
3
- import { noise } from "@chainsafe/libp2p-noise";
4
- import { yamux } from "@chainsafe/libp2p-yamux";
5
- import { bootstrap } from "@libp2p/bootstrap";
6
- import { identify } from "@libp2p/identify";
7
- import { kadDHT } from "@libp2p/kad-dht";
8
- import { ping } from "@libp2p/ping";
9
- import { tcp } from "@libp2p/tcp";
10
- import { webSockets } from "@libp2p/websockets";
11
- import { multiaddr } from "@multiformats/multiaddr";
12
- import { pipe } from "it-pipe";
13
- import { createLibp2p } from "libp2p";
14
- import { CID } from "multiformats/cid";
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
- ];
23
- const LIOP_MANIFEST_PROTOCOL = "/liop/manifest/1.0.0";
24
- const LIOP_MANIFEST_CAPABILITY = "liop:manifest";
25
- /**
26
- * P2P Mesh Node backed by libp2p + Kademlia DHT.
27
- *
28
- * Provides capability advertisement via CID-based content routing
29
- * and decentralized peer discovery.
30
- */
31
- export class MeshNode {
32
- node = null;
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;
38
- /**
39
- * Buffer of capability hashes that have been announced.
40
- * Used to re-announce capabilities when new peers connect
41
- * (critical for small / isolated clusters where the initial
42
- * provide() finds zero peers in the routing table).
43
- */
44
- announcedCapabilities = new Set();
45
- /** Guards against concurrent re-announcement storms. */
46
- reannouncing = false;
47
- /** Callback that returns the local node's manifest on request. */
48
- manifestProvider = null;
49
- /** Flag to ensure the manifest protocol is only registered once. */
50
- manifestProtocolRegistered = false;
51
- /** Local Ed25519 Private Key for protocol signatures */
52
- // biome-ignore lint/suspicious/noExplicitAny: libp2p keys type
53
- localPrivateKey = null;
54
- constructor(config = {}) {
55
- this.config = {
56
- listenAddresses: config.listenAddresses || [
57
- "/ip4/0.0.0.0/tcp/0/ws",
58
- "/ip4/0.0.0.0/tcp/0",
59
- ],
60
- bootstrapNodes: config.bootstrapNodes || [],
61
- identityPath: config.identityPath,
62
- enableWAN: config.enableWAN ?? false,
63
- dhtStoragePath: config.dhtStoragePath,
64
- addressMapper: config.addressMapper,
65
- };
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
- }
95
- /**
96
- * Loads a persistent identity from disk or generates a new Ed25519 keypair.
97
- * Uses privateKeyToProtobuf/privateKeyFromProtobuf (libp2p v3.x official API).
98
- */
99
- async loadOrCreateIdentity() {
100
- try {
101
- const { generateKeyPair, privateKeyFromProtobuf } = (await import("@libp2p/crypto/keys"
102
- // biome-ignore lint/suspicious/noExplicitAny: <libp2p type workaround>
103
- ));
104
- // @ts-expect-error: libp2p ESM dynamic import type conflict
105
- // biome-ignore lint/suspicious/noExplicitAny: <libp2p type workaround>
106
- const uint8arrays = (await import("uint8arrays"));
107
- if (this.config.identityPath) {
108
- const absolutePath = path.resolve(this.config.identityPath);
109
- try {
110
- const data = await fs.readFile(absolutePath, "utf-8");
111
- const json = JSON.parse(data);
112
- const protobufBytes = uint8arrays.fromString(json.privKey, "base64");
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
- }
124
- }
125
- catch (error) {
126
- const e = error;
127
- if (e.code !== "ENOENT") {
128
- log.error(`[LIOP-Mesh] Error loading identity: ${e.message}`);
129
- }
130
- }
131
- }
132
- const privateKey = await generateKeyPair("Ed25519");
133
- return { privateKey, isNew: true };
134
- }
135
- catch (error) {
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
- }
149
- }
150
- }
151
- /**
152
- * Persists the private key to disk using protobuf serialization (libp2p v3.x).
153
- */
154
- // biome-ignore lint/suspicious/noExplicitAny: Libp2p private key type is complex for Alpha
155
- async saveIdentity(privateKey) {
156
- if (!this.config.identityPath || !this.node)
157
- return;
158
- try {
159
- const absolutePath = path.resolve(this.config.identityPath);
160
- const { privateKeyToProtobuf } = (await import("@libp2p/crypto/keys"
161
- // biome-ignore lint/suspicious/noExplicitAny: <libp2p type workaround>
162
- ));
163
- // @ts-expect-error: libp2p ESM dynamic import type conflict
164
- const uint8arrays = await import("uint8arrays");
165
- const protobufBytes = privateKeyToProtobuf(privateKey);
166
- const privKeyEncoded = (uint8arrays.toString || uint8arrays.default.toString)(protobufBytes, "base64");
167
- const json = {
168
- id: this.node.peerId.toString(),
169
- privKey: privKeyEncoded,
170
- };
171
- await fs.mkdir(path.dirname(absolutePath), { recursive: true });
172
- await fs.writeFile(absolutePath, JSON.stringify(json, null, 2));
173
- log.info(`[LIOP-Mesh] Identity persisted to ${absolutePath}`);
174
- }
175
- catch (error) {
176
- log.error(`[LIOP-Mesh] FAILED to persist identity: ${error}`);
177
- }
178
- }
179
- /**
180
- * Creates a CID v1 (raw codec 0x55) from a SHA-256 hash of the capability string.
181
- * Required by @libp2p/kad-dht v16+ for provide/findProviders.
182
- */
183
- async capabilityToCID(capability) {
184
- const hash = await sha256.digest(new TextEncoder().encode(capability));
185
- return CID.create(1, 0x55, hash);
186
- }
187
- /**
188
- * Re-announces all buffered capabilities after a new peer connects.
189
- * Uses a small delay to allow the DHT protocol handshake to complete.
190
- */
191
- async reannounceAll() {
192
- if (this.reannouncing ||
193
- !this.node ||
194
- this.announcedCapabilities.size === 0)
195
- return;
196
- this.reannouncing = true;
197
- try {
198
- // Wait for the DHT protocol handshake to settle
199
- await new Promise((resolve) => setTimeout(resolve, 500));
200
- if (!this.node)
201
- return;
202
- log.info(`[LIOP-Mesh] Re-announcing ${this.announcedCapabilities.size} capabilities to updated routing table...`);
203
- for (const hash of this.announcedCapabilities) {
204
- try {
205
- const cid = await this.capabilityToCID(hash);
206
- await this.node.contentRouting.provide(cid);
207
- log.info(`[LIOP-Mesh] Re-announced: ${hash}`);
208
- }
209
- catch (_e) {
210
- log.info(`[LIOP-Mesh] Re-announce failed for ${hash}: ${_e}`);
211
- }
212
- }
213
- }
214
- finally {
215
- this.reannouncing = false;
216
- }
217
- }
218
- async start() {
219
- if (this.node)
220
- return;
221
- const result = await this.loadOrCreateIdentity();
222
- if (!result)
223
- throw new Error("Could not initialize P2P Identity");
224
- const { privateKey, isNew } = result;
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
231
- ? [
232
- bootstrap({
233
- list: bootNodes,
234
- }),
235
- ]
236
- : undefined;
237
- const dhtProtocol = this.config.enableWAN
238
- ? "/ipfs/kad/1.0.0"
239
- : "/ipfs/lan/kad/1.0.0";
240
- this.node = await createLibp2p({
241
- privateKey,
242
- addresses: {
243
- listen: this.config.listenAddresses,
244
- },
245
- transports: [tcp(), webSockets()],
246
- connectionEncrypters: [noise()],
247
- streamMuxers: [yamux()],
248
- services: {
249
- identify: identify(),
250
- ping: ping(),
251
- dht: kadDHT({
252
- protocol: dhtProtocol,
253
- clientMode: false,
254
- // Allow local/private IPs in the DHT routing table for development/testing
255
- allowQueryWithZeroPeers: true,
256
- // By default kadDHT drops local IP addresses. Override the mapper to keep them.
257
- peerInfoMapper: (peer) => peer,
258
- }),
259
- },
260
- // biome-ignore lint/suspicious/noExplicitAny: libp2p discovery type mismatch
261
- peerDiscovery: discovery,
262
- });
263
- // Monitor Connectivity Events
264
- this.node.addEventListener("peer:discovery", (evt) => {
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
- }
286
- });
287
- this.node.addEventListener("peer:connect", (evt) => {
288
- const peerId = evt.detail;
289
- log.info(`[LIOP-Mesh] Connected to peer: ${peerId.toString()}`);
290
- if (!this.node)
291
- return;
292
- // biome-ignore lint/suspicious/noExplicitAny: access internal services
293
- const dht = this.node.services.dht;
294
- if (dht?.routingTable) {
295
- log.info(`[LIOP-Mesh] Adding ${peerId.toString()} to DHT Routing Table`);
296
- dht.routingTable.add(peerId).catch((err) => {
297
- log.info(`[LIOP-Mesh] Failed to add peer to routing table: ${err instanceof Error ? err.message : String(err)}`);
298
- });
299
- }
300
- // Trigger reactive re-announcement of all capabilities
301
- // so that ADD_PROVIDER messages reach the new peer
302
- this.reannounceAll().catch((err) => {
303
- log.info(`[LIOP-Mesh] Re-announce error: ${err instanceof Error ? err.message : String(err)}`);
304
- });
305
- });
306
- await this.node.start();
307
- // Load persisted DHT routing table to enable rapid cold-start reconnections
308
- await this.loadRoutingTable();
309
- // [LIOP-ALPHA] Protocols and services setup
310
- this.applyHandlers();
311
- if (isNew && this.config.identityPath) {
312
- await this.saveIdentity(privateKey);
313
- }
314
- log.info(`[LIOP-Mesh] Node started with id: ${this.node.peerId.toString()}`);
315
- this.node.getMultiaddrs().forEach((addr) => {
316
- log.info(`[LIOP-Mesh] Listening on: ${addr.toString()}`);
317
- });
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
- }
342
- }
343
- }
344
- }
345
- }
346
- async stop() {
347
- if (this.node) {
348
- await this.saveRoutingTable();
349
- await this.node.stop();
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}`);
411
- }
412
- }
413
- /**
414
- * Internal logic to register protocol handlers against the libp2p node.
415
- * Can be called multiple times; handles idempotent registration.
416
- */
417
- applyHandlers() {
418
- if (!this.node || this.manifestProtocolRegistered)
419
- return;
420
- if (!this.manifestProvider)
421
- return;
422
- this.manifestProtocolRegistered = true;
423
- // Announce manifest capability to the Mesh DHT for discovery
424
- this.announceCapability(LIOP_MANIFEST_CAPABILITY).catch((err) => {
425
- log.info(`[LIOP-Mesh] Initial manifest announcement failed: ${err}`);
426
- });
427
- // libp2p v3.x: handler receives (stream, connection) as separate args
428
- this.node.handle(LIOP_MANIFEST_PROTOCOL,
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}.`);
436
- try {
437
- const manifest = this.manifestProvider?.();
438
- if (!manifest || !stream) {
439
- log.info(`[LIOP-Mesh] Skipping manifest request (no provider or stream)`);
440
- try {
441
- if (typeof stream?.close === "function")
442
- await stream.close();
443
- }
444
- catch (_e) { }
445
- return;
446
- }
447
- const manifestStr = JSON.stringify(manifest);
448
- const payload = new TextEncoder().encode(manifestStr);
449
- // Write length-prefixed payload (Big Endian 4 bytes)
450
- const lengthBuf = Buffer.alloc(4);
451
- lengthBuf.writeUInt32BE(payload.length, 0);
452
- const fullPacket = Buffer.concat([lengthBuf, Buffer.from(payload)]);
453
- log.info(`[LIOP-Mesh] Serving manifest (${fullPacket.length} bytes) to ${remotePeer} [Tools: ${manifest.tools.map((t) => t.name).join(", ")}]`);
454
- try {
455
- // libp2p v3.x: stream.send() for writing
456
- if (typeof stream.send === "function") {
457
- const accepted = stream.send(fullPacket);
458
- if (!accepted && typeof stream.onDrain === "function") {
459
- try {
460
- await stream.onDrain({ signal: AbortSignal.timeout(5000) });
461
- }
462
- catch (drainErr) {
463
- log.info(`[LIOP-Mesh] WARN: Drain timeout for ${remotePeer}: ${drainErr instanceof Error ? drainErr.message : String(drainErr)}`);
464
- }
465
- }
466
- }
467
- else {
468
- // Fallback for environments where stream.send is not available
469
- await pipe([fullPacket], stream);
470
- }
471
- log.info(`[LIOP-Mesh] Manifest sent successfully to ${remotePeer}`);
472
- }
473
- catch (writeErr) {
474
- log.info(`[LIOP-Mesh] Write error serving manifest to ${remotePeer}: ${writeErr instanceof Error ? writeErr.message : String(writeErr)}`);
475
- }
476
- finally {
477
- try {
478
- if (typeof stream.close === "function")
479
- await stream.close();
480
- }
481
- catch (_e) {
482
- // Ignore close errors
483
- }
484
- }
485
- return;
486
- }
487
- catch (err) {
488
- log.info(`[LIOP-Mesh] Error serving manifest to ${remotePeer}: ${err instanceof Error ? err.message : String(err)}`);
489
- }
490
- });
491
- log.info(`[LIOP-Mesh] Manifest Protocol registered: ${LIOP_MANIFEST_PROTOCOL}`);
492
- }
493
- /**
494
- * Registers a callback as the manifest provider.
495
- * Will be applied immediately if the node is already initialized.
496
- */
497
- registerManifestHandler(provider) {
498
- this.manifestProvider = provider;
499
- if (this.node) {
500
- this.applyHandlers();
501
- }
502
- }
503
- /**
504
- * Queries a remote peer's manifest by opening a /liop/manifest/1.0.0 stream.
505
- * Returns null if the peer doesn't support the protocol or is unreachable.
506
- */
507
- async queryManifest(peerIdStr) {
508
- if (!this.node)
509
- throw new Error("Mesh Node is not running");
510
- // [ALPHA-OPTIMIZATION] Local Loopback Bypass
511
- // If we are querying our own manifest, return it directly from the provider.
512
- if (peerIdStr === this.node.peerId.toString()) {
513
- log.info(`[LIOP-Mesh] Loopback: Returning local manifest directly for ${peerIdStr}`);
514
- return this.manifestProvider?.() || null;
515
- }
516
- if (this.shouldSkipManifestDial(peerIdStr)) {
517
- return null;
518
- }
519
- const MAX_ATTEMPTS = 3;
520
- for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
521
- try {
522
- // biome-ignore lint/suspicious/noExplicitAny: targetPeer can be from connections or from string
523
- let targetPeer = null;
524
- const connections = this.node.getConnections();
525
- const activeConn = connections.find((c) => c.remotePeer.toString() === peerIdStr);
526
- if (activeConn) {
527
- targetPeer = activeConn.remotePeer;
528
- }
529
- else {
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
- }
576
- }
577
- // Open a protocol stream using high-level dialProtocol for automated it-stream wrapping
578
- // biome-ignore lint/suspicious/noExplicitAny: stream type varies by transport
579
- let stream;
580
- try {
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
- });
591
- stream = result.stream || result;
592
- }
593
- catch (dialErr) {
594
- if (attempt === MAX_ATTEMPTS) {
595
- log.info(`[LIOP-Mesh] Dial error for ${peerIdStr} after ${MAX_ATTEMPTS} attempts: ${dialErr}`);
596
- return null;
597
- }
598
- const delay = 500 * 2 ** attempt;
599
- log.info(`[LIOP-Mesh] Dial error for ${peerIdStr} (Attempt ${attempt}). Retrying in ${delay}ms...`);
600
- await new Promise((r) => setTimeout(r, delay));
601
- continue;
602
- }
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 ??
606
- (typeof stream[Symbol.asyncIterator] === "function" ? stream : null);
607
- if (!source) {
608
- throw new Error("Target stream has no AsyncIterable source");
609
- }
610
- const chunks = [];
611
- let totalReceived = 0;
612
- let expectedPayloadLength = -1;
613
- // Read length-prefixed manifest: first 4 bytes = payload length (BE)
614
- let manifestTimeoutId;
615
- const timeoutPromise = new Promise((_, reject) => {
616
- manifestTimeoutId = setTimeout(() => reject(new Error("Manifest read timeout (5.0s)")), 5000);
617
- });
618
- try {
619
- await Promise.race([
620
- (async () => {
621
- for await (const chunk of source) {
622
- if (!chunk)
623
- continue;
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);
635
- if (bytes.length > 0) {
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
- }
648
- }
649
- }
650
- })(),
651
- timeoutPromise,
652
- ]);
653
- }
654
- catch (itErr) {
655
- if (chunks.length === 0)
656
- throw 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);
662
- }
663
- const raw = Buffer.concat(chunks);
664
- if (raw.length < 4) {
665
- throw new Error("Received empty/invalid manifest (too short)");
666
- }
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");
670
- const manifest = JSON.parse(jsonStr);
671
- log.info(`[LIOP-Mesh] Received manifest from ${peerIdStr}: ${manifest.tools.length} tools`);
672
- this.clearManifestDialFailure(peerIdStr);
673
- return manifest;
674
- }
675
- catch (err) {
676
- if (attempt === MAX_ATTEMPTS) {
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)}`);
679
- return null;
680
- }
681
- const delay = 500 * 2 ** attempt;
682
- log.info(`[LIOP-Mesh] Query error for ${peerIdStr} (Attempt ${attempt}): ${err instanceof Error ? err.message : String(err)}. Retrying in ${delay}ms...`);
683
- await new Promise((r) => setTimeout(r, delay));
684
- }
685
- }
686
- return null;
687
- }
688
- /**
689
- * Discovers all peers in the DHT that have announced "liop:manifest".
690
- * Returns their PeerIDs for subsequent manifest queries.
691
- */
692
- async discoverManifestProviders() {
693
- return this.findProviders(LIOP_MANIFEST_CAPABILITY);
694
- }
695
- /**
696
- * Announces this node as a manifest provider in the DHT.
697
- * Should be called after tools/resources have been registered.
698
- */
699
- async announceManifest() {
700
- await this.announceCapability(LIOP_MANIFEST_CAPABILITY);
701
- }
702
- /**
703
- * Returns the current size of the routing table for diagnostic purposes.
704
- */
705
- getRoutingTableSize() {
706
- if (!this.node)
707
- return 0;
708
- // @ts-expect-error: Accessing internal routing table size for diagnostics
709
- return this.node.services.dht?.routingTable?.size || 0;
710
- }
711
- getPeerId() {
712
- if (!this.node)
713
- throw new Error("Mesh Node is not running");
714
- return this.node.peerId.toString();
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
- }
723
- getMultiaddrs() {
724
- if (!this.node)
725
- throw new Error("Mesh Node is not running");
726
- return this.node.getMultiaddrs().map((a) => a.toString());
727
- }
728
- async announceCapability(hash) {
729
- if (!this.node)
730
- throw new Error("Mesh Node is not running");
731
- // Buffer the capability for reactive re-announcement
732
- this.announcedCapabilities.add(hash);
733
- try {
734
- const cid = await this.capabilityToCID(hash);
735
- log.info(`[LIOP-Mesh] Announcing capability: ${hash} (CID: ${cid.toString()})`);
736
- // In libp2p v1.x, contentRouting.provide returns Promise<void>
737
- await this.node.contentRouting.provide(cid);
738
- log.info(`[LIOP-Mesh] Successfully announced capability: ${hash}`);
739
- // [DEV-ONLY] Self-verification
740
- const selfId = this.node.peerId.toString();
741
- for await (const peer of this.node.contentRouting.findProviders(cid)) {
742
- if (peer.id.toString() === selfId) {
743
- log.info(`[LIOP-Mesh] Self-verification success: Node is providing ${hash}`);
744
- break;
745
- }
746
- }
747
- }
748
- catch (error) {
749
- log.error(`[LIOP-Mesh] Failed to announce capability: ${error}`);
750
- }
751
- }
752
- async findProviders(hash) {
753
- if (!this.node)
754
- throw new Error("Mesh Node is not running");
755
- const providers = [];
756
- try {
757
- const cid = await this.capabilityToCID(hash);
758
- log.info(`[LIOP-Mesh] Querying DHT for ${hash} (CID: ${cid.toString()})...`);
759
- let foundAny = false;
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;
800
- }
801
- }
802
- if (!foundAny) {
803
- const services = this.node.services;
804
- const dhtSize = services.dht?.routingTable?.size || 0;
805
- log.info(`[LIOP-Mesh] DHT search for ${hash} returned zero results (routing table size: ${dhtSize})`);
806
- }
807
- // [DEVELOPER-EXPERIENCE] Local Loopback Discovery
808
- // If we are providing this capability, ensure we find ourselves even if DHT findProviders doesn't return us.
809
- if (this.announcedCapabilities.has(hash)) {
810
- const selfId = this.node.peerId.toString();
811
- if (!providers.includes(selfId)) {
812
- log.info(`[LIOP-Mesh] Including local node (${selfId}) in results for ${hash}`);
813
- providers.push(selfId);
814
- }
815
- }
816
- }
817
- catch (error) {
818
- log.info(`[LIOP-Mesh] Error finding providers for ${hash}: ${error instanceof Error ? error.message : String(error)}`);
819
- }
820
- log.info(`[LIOP-Mesh] DHT search for ${hash} finished. Found ${providers.length} providers.`);
821
- return providers;
822
- }
823
- async resolvePeer(peerIdStr) {
824
- if (!this.node)
825
- throw new Error("Mesh Node is not running");
826
- try {
827
- // Strategy 1: Check active connections for the peer's multiaddrs
828
- const connections = this.node.getConnections();
829
- for (const conn of connections) {
830
- if (conn.remotePeer.toString() === peerIdStr) {
831
- const remoteAddr = conn.remoteAddr.toString();
832
- log.info(`[LIOP-Mesh] Resolved peer ${peerIdStr} via active connection: ${remoteAddr}`);
833
- return [remoteAddr];
834
- }
835
- }
836
- // Strategy 2: Try peerStore (iterate all peers to avoid toMultihash conflict)
837
- const allPeers = await this.node.peerStore.all();
838
- for (const peer of allPeers) {
839
- if (peer.id.toString() === peerIdStr && peer.addresses.length > 0) {
840
- // biome-ignore lint/suspicious/noExplicitAny: Internal libp2p addr type
841
- const addrs = peer.addresses.map((a) => a.multiaddr.toString());
842
- log.info(`[LIOP-Mesh] Resolved peer ${peerIdStr} via peerStore: ${addrs[0]}`);
843
- return addrs;
844
- }
845
- }
846
- log.info(`[LIOP-Mesh] Peer ${peerIdStr} not found in connections or peerStore`);
847
- }
848
- catch (error) {
849
- log.info(`[LIOP-Mesh] Failed to resolve peer ${peerIdStr}: ${error}`);
850
- }
851
- return [];
852
- }
853
- }