chainlesschain 0.143.0 → 0.145.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 (133) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +196 -28
  3. package/src/commands/activitypub.js +157 -27
  4. package/src/commands/agent.js +587 -103
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +152 -27
  7. package/src/commands/bm25.js +111 -27
  8. package/src/commands/browse.js +190 -29
  9. package/src/commands/ccron.js +128 -28
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +414 -34
  12. package/src/commands/compt.js +127 -29
  13. package/src/commands/consol.js +8 -2
  14. package/src/commands/cowork.js +580 -34
  15. package/src/commands/crosschain.js +182 -28
  16. package/src/commands/dao.js +182 -28
  17. package/src/commands/dlp.js +172 -27
  18. package/src/commands/economy.js +205 -50
  19. package/src/commands/evolution.js +203 -50
  20. package/src/commands/evomap.js +182 -27
  21. package/src/commands/fflag.js +74 -22
  22. package/src/commands/git.js +177 -37
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +204 -50
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +170 -34
  27. package/src/commands/itbudget.js +149 -33
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +195 -38
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +179 -27
  32. package/src/commands/mcpscaf.js +145 -33
  33. package/src/commands/meminj.js +145 -33
  34. package/src/commands/nostr.js +178 -27
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +144 -33
  37. package/src/commands/pdfp.js +109 -27
  38. package/src/commands/perf.js +169 -32
  39. package/src/commands/perm.js +144 -33
  40. package/src/commands/pipeline.js +207 -52
  41. package/src/commands/planmode.js +141 -32
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +111 -27
  44. package/src/commands/recommend.js +177 -34
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +145 -33
  48. package/src/commands/seshsearch.js +141 -33
  49. package/src/commands/seshtail.js +144 -33
  50. package/src/commands/seshu.js +152 -33
  51. package/src/commands/sganal.js +123 -29
  52. package/src/commands/siem.js +201 -34
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +146 -33
  55. package/src/commands/social.js +157 -32
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +145 -33
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +171 -33
  60. package/src/commands/topiccls.js +146 -33
  61. package/src/commands/uprof.js +141 -32
  62. package/src/commands/vcheck.js +122 -28
  63. package/src/commands/webfetch.js +141 -32
  64. package/src/commands/zkp.js +184 -28
  65. package/src/index.js +40 -0
  66. package/src/lib/a2a-protocol.js +319 -51
  67. package/src/lib/activitypub-bridge.js +288 -50
  68. package/src/lib/agent-economy.js +304 -51
  69. package/src/lib/app-builder.js +279 -46
  70. package/src/lib/audit-logger.js +321 -0
  71. package/src/lib/autonomous-agent.js +284 -48
  72. package/src/lib/bi-engine.js +283 -49
  73. package/src/lib/bm25-search.js +301 -49
  74. package/src/lib/browser-automation.js +296 -49
  75. package/src/lib/chat-core.js +336 -0
  76. package/src/lib/claude-code-bridge.js +341 -0
  77. package/src/lib/compliance-framework-reporter.js +305 -51
  78. package/src/lib/compliance-manager.js +330 -0
  79. package/src/lib/compression-telemetry.js +301 -49
  80. package/src/lib/content-recommender.js +317 -52
  81. package/src/lib/cowork-cron.js +298 -49
  82. package/src/lib/cowork-learning.js +333 -0
  83. package/src/lib/cowork-task-runner.js +308 -51
  84. package/src/lib/cowork-workflow.js +327 -0
  85. package/src/lib/cross-chain.js +311 -51
  86. package/src/lib/dao-governance.js +280 -46
  87. package/src/lib/dlp-engine.js +287 -49
  88. package/src/lib/evolution-system.js +278 -47
  89. package/src/lib/evomap-manager.js +280 -46
  90. package/src/lib/execution-backend.js +294 -48
  91. package/src/lib/feature-flags.js +294 -49
  92. package/src/lib/git-integration.js +285 -47
  93. package/src/lib/hardening-manager.js +341 -0
  94. package/src/lib/hierarchical-memory.js +284 -48
  95. package/src/lib/inference-network.js +308 -51
  96. package/src/lib/iteration-budget.js +302 -50
  97. package/src/lib/knowledge-graph.js +333 -0
  98. package/src/lib/matrix-bridge.js +281 -47
  99. package/src/lib/mcp-scaffold.js +318 -54
  100. package/src/lib/memory-injection.js +288 -49
  101. package/src/lib/nostr-bridge.js +286 -49
  102. package/src/lib/orchestrator.js +293 -48
  103. package/src/lib/pdf-parser.js +298 -49
  104. package/src/lib/perf-tuning.js +309 -50
  105. package/src/lib/permission-engine.js +287 -49
  106. package/src/lib/pipeline-orchestrator.js +289 -49
  107. package/src/lib/plan-mode.js +298 -51
  108. package/src/lib/privacy-computing.js +335 -0
  109. package/src/lib/reputation-optimizer.js +340 -0
  110. package/src/lib/sandbox-v2.js +327 -0
  111. package/src/lib/service-container.js +313 -52
  112. package/src/lib/session-consolidator.js +296 -49
  113. package/src/lib/session-hooks.js +312 -53
  114. package/src/lib/session-search.js +304 -51
  115. package/src/lib/session-tail.js +288 -49
  116. package/src/lib/session-usage.js +298 -52
  117. package/src/lib/siem-exporter.js +298 -51
  118. package/src/lib/skill-marketplace.js +345 -0
  119. package/src/lib/sla-manager.js +341 -0
  120. package/src/lib/slot-filler.js +303 -51
  121. package/src/lib/social-graph-analytics.js +295 -49
  122. package/src/lib/social-graph.js +272 -49
  123. package/src/lib/stress-tester.js +342 -0
  124. package/src/lib/sub-agent-registry.js +302 -53
  125. package/src/lib/task-model-selector.js +302 -50
  126. package/src/lib/terraform-manager.js +333 -0
  127. package/src/lib/todo-manager.js +281 -47
  128. package/src/lib/token-incentive.js +341 -0
  129. package/src/lib/topic-classifier.js +297 -49
  130. package/src/lib/user-profile.js +294 -50
  131. package/src/lib/version-checker.js +304 -50
  132. package/src/lib/web-fetch.js +292 -51
  133. package/src/lib/zkp-engine.js +286 -49
@@ -370,25 +370,53 @@ export function _resetState() {
370
370
  _didMappings.clear();
371
371
  }
372
372
 
373
-
374
373
  // ===== V2 Surface: Nostr Bridge governance overlay (CLI v0.134.0) =====
375
374
  export const NOSTR_RELAY_MATURITY_V2 = Object.freeze({
376
- PENDING: "pending", ACTIVE: "active", OFFLINE: "offline", RETIRED: "retired",
375
+ PENDING: "pending",
376
+ ACTIVE: "active",
377
+ OFFLINE: "offline",
378
+ RETIRED: "retired",
377
379
  });
378
380
  export const NOSTR_EVENT_LIFECYCLE_V2 = Object.freeze({
379
- QUEUED: "queued", PUBLISHING: "publishing", PUBLISHED: "published", FAILED: "failed", CANCELLED: "cancelled",
381
+ QUEUED: "queued",
382
+ PUBLISHING: "publishing",
383
+ PUBLISHED: "published",
384
+ FAILED: "failed",
385
+ CANCELLED: "cancelled",
380
386
  });
381
387
 
382
388
  const _nsRelayTrans = new Map([
383
- [NOSTR_RELAY_MATURITY_V2.PENDING, new Set([NOSTR_RELAY_MATURITY_V2.ACTIVE, NOSTR_RELAY_MATURITY_V2.RETIRED])],
384
- [NOSTR_RELAY_MATURITY_V2.ACTIVE, new Set([NOSTR_RELAY_MATURITY_V2.OFFLINE, NOSTR_RELAY_MATURITY_V2.RETIRED])],
385
- [NOSTR_RELAY_MATURITY_V2.OFFLINE, new Set([NOSTR_RELAY_MATURITY_V2.ACTIVE, NOSTR_RELAY_MATURITY_V2.RETIRED])],
389
+ [
390
+ NOSTR_RELAY_MATURITY_V2.PENDING,
391
+ new Set([NOSTR_RELAY_MATURITY_V2.ACTIVE, NOSTR_RELAY_MATURITY_V2.RETIRED]),
392
+ ],
393
+ [
394
+ NOSTR_RELAY_MATURITY_V2.ACTIVE,
395
+ new Set([NOSTR_RELAY_MATURITY_V2.OFFLINE, NOSTR_RELAY_MATURITY_V2.RETIRED]),
396
+ ],
397
+ [
398
+ NOSTR_RELAY_MATURITY_V2.OFFLINE,
399
+ new Set([NOSTR_RELAY_MATURITY_V2.ACTIVE, NOSTR_RELAY_MATURITY_V2.RETIRED]),
400
+ ],
386
401
  [NOSTR_RELAY_MATURITY_V2.RETIRED, new Set()],
387
402
  ]);
388
403
  const _nsRelayTerminal = new Set([NOSTR_RELAY_MATURITY_V2.RETIRED]);
389
404
  const _nsEventTrans = new Map([
390
- [NOSTR_EVENT_LIFECYCLE_V2.QUEUED, new Set([NOSTR_EVENT_LIFECYCLE_V2.PUBLISHING, NOSTR_EVENT_LIFECYCLE_V2.CANCELLED])],
391
- [NOSTR_EVENT_LIFECYCLE_V2.PUBLISHING, new Set([NOSTR_EVENT_LIFECYCLE_V2.PUBLISHED, NOSTR_EVENT_LIFECYCLE_V2.FAILED, NOSTR_EVENT_LIFECYCLE_V2.CANCELLED])],
405
+ [
406
+ NOSTR_EVENT_LIFECYCLE_V2.QUEUED,
407
+ new Set([
408
+ NOSTR_EVENT_LIFECYCLE_V2.PUBLISHING,
409
+ NOSTR_EVENT_LIFECYCLE_V2.CANCELLED,
410
+ ]),
411
+ ],
412
+ [
413
+ NOSTR_EVENT_LIFECYCLE_V2.PUBLISHING,
414
+ new Set([
415
+ NOSTR_EVENT_LIFECYCLE_V2.PUBLISHED,
416
+ NOSTR_EVENT_LIFECYCLE_V2.FAILED,
417
+ NOSTR_EVENT_LIFECYCLE_V2.CANCELLED,
418
+ ]),
419
+ ],
392
420
  [NOSTR_EVENT_LIFECYCLE_V2.PUBLISHED, new Set()],
393
421
  [NOSTR_EVENT_LIFECYCLE_V2.FAILED, new Set()],
394
422
  [NOSTR_EVENT_LIFECYCLE_V2.CANCELLED, new Set()],
@@ -401,76 +429,285 @@ let _nsMaxPendingPerRelay = 30;
401
429
  let _nsRelayIdleMs = 60 * 60 * 1000;
402
430
  let _nsEventStuckMs = 2 * 60 * 1000;
403
431
 
404
- function _nsPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
432
+ function _nsPos(n, lbl) {
433
+ const v = Math.floor(Number(n));
434
+ if (!Number.isFinite(v) || v <= 0)
435
+ throw new Error(`${lbl} must be positive integer`);
436
+ return v;
437
+ }
405
438
 
406
- export function setMaxActiveNostrRelaysPerOwnerV2(n) { _nsMaxActivePerOwner = _nsPos(n, "maxActiveNostrRelaysPerOwner"); }
407
- export function getMaxActiveNostrRelaysPerOwnerV2() { return _nsMaxActivePerOwner; }
408
- export function setMaxPendingNostrEventsPerRelayV2(n) { _nsMaxPendingPerRelay = _nsPos(n, "maxPendingNostrEventsPerRelay"); }
409
- export function getMaxPendingNostrEventsPerRelayV2() { return _nsMaxPendingPerRelay; }
410
- export function setNostrRelayIdleMsV2(n) { _nsRelayIdleMs = _nsPos(n, "nostrRelayIdleMs"); }
411
- export function getNostrRelayIdleMsV2() { return _nsRelayIdleMs; }
412
- export function setNostrEventStuckMsV2(n) { _nsEventStuckMs = _nsPos(n, "nostrEventStuckMs"); }
413
- export function getNostrEventStuckMsV2() { return _nsEventStuckMs; }
439
+ export function setMaxActiveNostrRelaysPerOwnerV2(n) {
440
+ _nsMaxActivePerOwner = _nsPos(n, "maxActiveNostrRelaysPerOwner");
441
+ }
442
+ export function getMaxActiveNostrRelaysPerOwnerV2() {
443
+ return _nsMaxActivePerOwner;
444
+ }
445
+ export function setMaxPendingNostrEventsPerRelayV2(n) {
446
+ _nsMaxPendingPerRelay = _nsPos(n, "maxPendingNostrEventsPerRelay");
447
+ }
448
+ export function getMaxPendingNostrEventsPerRelayV2() {
449
+ return _nsMaxPendingPerRelay;
450
+ }
451
+ export function setNostrRelayIdleMsV2(n) {
452
+ _nsRelayIdleMs = _nsPos(n, "nostrRelayIdleMs");
453
+ }
454
+ export function getNostrRelayIdleMsV2() {
455
+ return _nsRelayIdleMs;
456
+ }
457
+ export function setNostrEventStuckMsV2(n) {
458
+ _nsEventStuckMs = _nsPos(n, "nostrEventStuckMs");
459
+ }
460
+ export function getNostrEventStuckMsV2() {
461
+ return _nsEventStuckMs;
462
+ }
414
463
 
415
464
  export function _resetStateNostrBridgeV2() {
416
- _nsRelays.clear(); _nsEvents.clear();
417
- _nsMaxActivePerOwner = 10; _nsMaxPendingPerRelay = 30;
418
- _nsRelayIdleMs = 60 * 60 * 1000; _nsEventStuckMs = 2 * 60 * 1000;
465
+ _nsRelays.clear();
466
+ _nsEvents.clear();
467
+ _nsMaxActivePerOwner = 10;
468
+ _nsMaxPendingPerRelay = 30;
469
+ _nsRelayIdleMs = 60 * 60 * 1000;
470
+ _nsEventStuckMs = 2 * 60 * 1000;
419
471
  }
420
472
 
421
473
  export function registerNostrRelayV2({ id, owner, url, metadata } = {}) {
422
474
  if (!id || typeof id !== "string") throw new Error("id is required");
423
475
  if (!owner || typeof owner !== "string") throw new Error("owner is required");
424
- if (_nsRelays.has(id)) throw new Error(`nostr relay ${id} already registered`);
476
+ if (_nsRelays.has(id))
477
+ throw new Error(`nostr relay ${id} already registered`);
425
478
  const now = Date.now();
426
- const r = { id, owner, url: url || "", status: NOSTR_RELAY_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, retiredAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
479
+ const r = {
480
+ id,
481
+ owner,
482
+ url: url || "",
483
+ status: NOSTR_RELAY_MATURITY_V2.PENDING,
484
+ createdAt: now,
485
+ updatedAt: now,
486
+ activatedAt: null,
487
+ retiredAt: null,
488
+ lastTouchedAt: now,
489
+ metadata: { ...(metadata || {}) },
490
+ };
427
491
  _nsRelays.set(id, r);
428
492
  return { ...r, metadata: { ...r.metadata } };
429
493
  }
430
- function _nsCheckR(from, to) { const a = _nsRelayTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid nostr relay transition ${from} → ${to}`); }
431
- function _nsCountActive(owner) { let n = 0; for (const r of _nsRelays.values()) if (r.owner === owner && r.status === NOSTR_RELAY_MATURITY_V2.ACTIVE) n++; return n; }
494
+ function _nsCheckR(from, to) {
495
+ const a = _nsRelayTrans.get(from);
496
+ if (!a || !a.has(to))
497
+ throw new Error(`invalid nostr relay transition ${from} → ${to}`);
498
+ }
499
+ function _nsCountActive(owner) {
500
+ let n = 0;
501
+ for (const r of _nsRelays.values())
502
+ if (r.owner === owner && r.status === NOSTR_RELAY_MATURITY_V2.ACTIVE) n++;
503
+ return n;
504
+ }
432
505
 
433
506
  export function activateNostrRelayV2(id) {
434
- const r = _nsRelays.get(id); if (!r) throw new Error(`nostr relay ${id} not found`);
507
+ const r = _nsRelays.get(id);
508
+ if (!r) throw new Error(`nostr relay ${id} not found`);
435
509
  _nsCheckR(r.status, NOSTR_RELAY_MATURITY_V2.ACTIVE);
436
510
  const recovery = r.status === NOSTR_RELAY_MATURITY_V2.OFFLINE;
437
- if (!recovery) { const a = _nsCountActive(r.owner); if (a >= _nsMaxActivePerOwner) throw new Error(`max active nostr relays per owner (${_nsMaxActivePerOwner}) reached for ${r.owner}`); }
438
- const now = Date.now(); r.status = NOSTR_RELAY_MATURITY_V2.ACTIVE; r.updatedAt = now; r.lastTouchedAt = now; if (!r.activatedAt) r.activatedAt = now;
511
+ if (!recovery) {
512
+ const a = _nsCountActive(r.owner);
513
+ if (a >= _nsMaxActivePerOwner)
514
+ throw new Error(
515
+ `max active nostr relays per owner (${_nsMaxActivePerOwner}) reached for ${r.owner}`,
516
+ );
517
+ }
518
+ const now = Date.now();
519
+ r.status = NOSTR_RELAY_MATURITY_V2.ACTIVE;
520
+ r.updatedAt = now;
521
+ r.lastTouchedAt = now;
522
+ if (!r.activatedAt) r.activatedAt = now;
523
+ return { ...r, metadata: { ...r.metadata } };
524
+ }
525
+ export function offlineNostrRelayV2(id) {
526
+ const r = _nsRelays.get(id);
527
+ if (!r) throw new Error(`nostr relay ${id} not found`);
528
+ _nsCheckR(r.status, NOSTR_RELAY_MATURITY_V2.OFFLINE);
529
+ r.status = NOSTR_RELAY_MATURITY_V2.OFFLINE;
530
+ r.updatedAt = Date.now();
531
+ return { ...r, metadata: { ...r.metadata } };
532
+ }
533
+ export function retireNostrRelayV2(id) {
534
+ const r = _nsRelays.get(id);
535
+ if (!r) throw new Error(`nostr relay ${id} not found`);
536
+ _nsCheckR(r.status, NOSTR_RELAY_MATURITY_V2.RETIRED);
537
+ const now = Date.now();
538
+ r.status = NOSTR_RELAY_MATURITY_V2.RETIRED;
539
+ r.updatedAt = now;
540
+ if (!r.retiredAt) r.retiredAt = now;
541
+ return { ...r, metadata: { ...r.metadata } };
542
+ }
543
+ export function touchNostrRelayV2(id) {
544
+ const r = _nsRelays.get(id);
545
+ if (!r) throw new Error(`nostr relay ${id} not found`);
546
+ if (_nsRelayTerminal.has(r.status))
547
+ throw new Error(`cannot touch terminal nostr relay ${id}`);
548
+ const now = Date.now();
549
+ r.lastTouchedAt = now;
550
+ r.updatedAt = now;
551
+ return { ...r, metadata: { ...r.metadata } };
552
+ }
553
+ export function getNostrRelayV2(id) {
554
+ const r = _nsRelays.get(id);
555
+ if (!r) return null;
439
556
  return { ...r, metadata: { ...r.metadata } };
440
557
  }
441
- export function offlineNostrRelayV2(id) { const r = _nsRelays.get(id); if (!r) throw new Error(`nostr relay ${id} not found`); _nsCheckR(r.status, NOSTR_RELAY_MATURITY_V2.OFFLINE); r.status = NOSTR_RELAY_MATURITY_V2.OFFLINE; r.updatedAt = Date.now(); return { ...r, metadata: { ...r.metadata } }; }
442
- export function retireNostrRelayV2(id) { const r = _nsRelays.get(id); if (!r) throw new Error(`nostr relay ${id} not found`); _nsCheckR(r.status, NOSTR_RELAY_MATURITY_V2.RETIRED); const now = Date.now(); r.status = NOSTR_RELAY_MATURITY_V2.RETIRED; r.updatedAt = now; if (!r.retiredAt) r.retiredAt = now; return { ...r, metadata: { ...r.metadata } }; }
443
- export function touchNostrRelayV2(id) { const r = _nsRelays.get(id); if (!r) throw new Error(`nostr relay ${id} not found`); if (_nsRelayTerminal.has(r.status)) throw new Error(`cannot touch terminal nostr relay ${id}`); const now = Date.now(); r.lastTouchedAt = now; r.updatedAt = now; return { ...r, metadata: { ...r.metadata } }; }
444
- export function getNostrRelayV2(id) { const r = _nsRelays.get(id); if (!r) return null; return { ...r, metadata: { ...r.metadata } }; }
445
- export function listNostrRelaysV2() { return [..._nsRelays.values()].map((r) => ({ ...r, metadata: { ...r.metadata } })); }
558
+ export function listNostrRelaysV2() {
559
+ return [..._nsRelays.values()].map((r) => ({
560
+ ...r,
561
+ metadata: { ...r.metadata },
562
+ }));
563
+ }
446
564
 
447
- function _nsCountPending(rid) { let n = 0; for (const e of _nsEvents.values()) if (e.relayId === rid && (e.status === NOSTR_EVENT_LIFECYCLE_V2.QUEUED || e.status === NOSTR_EVENT_LIFECYCLE_V2.PUBLISHING)) n++; return n; }
565
+ function _nsCountPending(rid) {
566
+ let n = 0;
567
+ for (const e of _nsEvents.values())
568
+ if (
569
+ e.relayId === rid &&
570
+ (e.status === NOSTR_EVENT_LIFECYCLE_V2.QUEUED ||
571
+ e.status === NOSTR_EVENT_LIFECYCLE_V2.PUBLISHING)
572
+ )
573
+ n++;
574
+ return n;
575
+ }
448
576
 
449
577
  export function createNostrEventV2({ id, relayId, kind, metadata } = {}) {
450
578
  if (!id || typeof id !== "string") throw new Error("id is required");
451
- if (!relayId || typeof relayId !== "string") throw new Error("relayId is required");
579
+ if (!relayId || typeof relayId !== "string")
580
+ throw new Error("relayId is required");
452
581
  if (_nsEvents.has(id)) throw new Error(`nostr event ${id} already exists`);
453
- if (!_nsRelays.has(relayId)) throw new Error(`nostr relay ${relayId} not found`);
582
+ if (!_nsRelays.has(relayId))
583
+ throw new Error(`nostr relay ${relayId} not found`);
454
584
  const pending = _nsCountPending(relayId);
455
- if (pending >= _nsMaxPendingPerRelay) throw new Error(`max pending nostr events per relay (${_nsMaxPendingPerRelay}) reached for ${relayId}`);
585
+ if (pending >= _nsMaxPendingPerRelay)
586
+ throw new Error(
587
+ `max pending nostr events per relay (${_nsMaxPendingPerRelay}) reached for ${relayId}`,
588
+ );
456
589
  const now = Date.now();
457
- const e = { id, relayId, kind: typeof kind === "number" ? kind : 1, status: NOSTR_EVENT_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
590
+ const e = {
591
+ id,
592
+ relayId,
593
+ kind: typeof kind === "number" ? kind : 1,
594
+ status: NOSTR_EVENT_LIFECYCLE_V2.QUEUED,
595
+ createdAt: now,
596
+ updatedAt: now,
597
+ startedAt: null,
598
+ settledAt: null,
599
+ metadata: { ...(metadata || {}) },
600
+ };
458
601
  _nsEvents.set(id, e);
459
602
  return { ...e, metadata: { ...e.metadata } };
460
603
  }
461
- function _nsCheckE(from, to) { const a = _nsEventTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid nostr event transition ${from} → ${to}`); }
462
- export function startNostrEventV2(id) { const e = _nsEvents.get(id); if (!e) throw new Error(`nostr event ${id} not found`); _nsCheckE(e.status, NOSTR_EVENT_LIFECYCLE_V2.PUBLISHING); const now = Date.now(); e.status = NOSTR_EVENT_LIFECYCLE_V2.PUBLISHING; e.updatedAt = now; if (!e.startedAt) e.startedAt = now; return { ...e, metadata: { ...e.metadata } }; }
463
- export function publishNostrEventV2(id) { const e = _nsEvents.get(id); if (!e) throw new Error(`nostr event ${id} not found`); _nsCheckE(e.status, NOSTR_EVENT_LIFECYCLE_V2.PUBLISHED); const now = Date.now(); e.status = NOSTR_EVENT_LIFECYCLE_V2.PUBLISHED; e.updatedAt = now; if (!e.settledAt) e.settledAt = now; return { ...e, metadata: { ...e.metadata } }; }
464
- export function failNostrEventV2(id, reason) { const e = _nsEvents.get(id); if (!e) throw new Error(`nostr event ${id} not found`); _nsCheckE(e.status, NOSTR_EVENT_LIFECYCLE_V2.FAILED); const now = Date.now(); e.status = NOSTR_EVENT_LIFECYCLE_V2.FAILED; e.updatedAt = now; if (!e.settledAt) e.settledAt = now; if (reason) e.metadata.failReason = String(reason); return { ...e, metadata: { ...e.metadata } }; }
465
- export function cancelNostrEventV2(id, reason) { const e = _nsEvents.get(id); if (!e) throw new Error(`nostr event ${id} not found`); _nsCheckE(e.status, NOSTR_EVENT_LIFECYCLE_V2.CANCELLED); const now = Date.now(); e.status = NOSTR_EVENT_LIFECYCLE_V2.CANCELLED; e.updatedAt = now; if (!e.settledAt) e.settledAt = now; if (reason) e.metadata.cancelReason = String(reason); return { ...e, metadata: { ...e.metadata } }; }
466
- export function getNostrEventV2(id) { const e = _nsEvents.get(id); if (!e) return null; return { ...e, metadata: { ...e.metadata } }; }
467
- export function listNostrEventsV2() { return [..._nsEvents.values()].map((e) => ({ ...e, metadata: { ...e.metadata } })); }
604
+ function _nsCheckE(from, to) {
605
+ const a = _nsEventTrans.get(from);
606
+ if (!a || !a.has(to))
607
+ throw new Error(`invalid nostr event transition ${from} ${to}`);
608
+ }
609
+ export function startNostrEventV2(id) {
610
+ const e = _nsEvents.get(id);
611
+ if (!e) throw new Error(`nostr event ${id} not found`);
612
+ _nsCheckE(e.status, NOSTR_EVENT_LIFECYCLE_V2.PUBLISHING);
613
+ const now = Date.now();
614
+ e.status = NOSTR_EVENT_LIFECYCLE_V2.PUBLISHING;
615
+ e.updatedAt = now;
616
+ if (!e.startedAt) e.startedAt = now;
617
+ return { ...e, metadata: { ...e.metadata } };
618
+ }
619
+ export function publishNostrEventV2(id) {
620
+ const e = _nsEvents.get(id);
621
+ if (!e) throw new Error(`nostr event ${id} not found`);
622
+ _nsCheckE(e.status, NOSTR_EVENT_LIFECYCLE_V2.PUBLISHED);
623
+ const now = Date.now();
624
+ e.status = NOSTR_EVENT_LIFECYCLE_V2.PUBLISHED;
625
+ e.updatedAt = now;
626
+ if (!e.settledAt) e.settledAt = now;
627
+ return { ...e, metadata: { ...e.metadata } };
628
+ }
629
+ export function failNostrEventV2(id, reason) {
630
+ const e = _nsEvents.get(id);
631
+ if (!e) throw new Error(`nostr event ${id} not found`);
632
+ _nsCheckE(e.status, NOSTR_EVENT_LIFECYCLE_V2.FAILED);
633
+ const now = Date.now();
634
+ e.status = NOSTR_EVENT_LIFECYCLE_V2.FAILED;
635
+ e.updatedAt = now;
636
+ if (!e.settledAt) e.settledAt = now;
637
+ if (reason) e.metadata.failReason = String(reason);
638
+ return { ...e, metadata: { ...e.metadata } };
639
+ }
640
+ export function cancelNostrEventV2(id, reason) {
641
+ const e = _nsEvents.get(id);
642
+ if (!e) throw new Error(`nostr event ${id} not found`);
643
+ _nsCheckE(e.status, NOSTR_EVENT_LIFECYCLE_V2.CANCELLED);
644
+ const now = Date.now();
645
+ e.status = NOSTR_EVENT_LIFECYCLE_V2.CANCELLED;
646
+ e.updatedAt = now;
647
+ if (!e.settledAt) e.settledAt = now;
648
+ if (reason) e.metadata.cancelReason = String(reason);
649
+ return { ...e, metadata: { ...e.metadata } };
650
+ }
651
+ export function getNostrEventV2(id) {
652
+ const e = _nsEvents.get(id);
653
+ if (!e) return null;
654
+ return { ...e, metadata: { ...e.metadata } };
655
+ }
656
+ export function listNostrEventsV2() {
657
+ return [..._nsEvents.values()].map((e) => ({
658
+ ...e,
659
+ metadata: { ...e.metadata },
660
+ }));
661
+ }
468
662
 
469
- export function autoOfflineIdleNostrRelaysV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const r of _nsRelays.values()) if (r.status === NOSTR_RELAY_MATURITY_V2.ACTIVE && (t - r.lastTouchedAt) >= _nsRelayIdleMs) { r.status = NOSTR_RELAY_MATURITY_V2.OFFLINE; r.updatedAt = t; flipped.push(r.id); } return { flipped, count: flipped.length }; }
470
- export function autoFailStuckNostrEventsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const e of _nsEvents.values()) if (e.status === NOSTR_EVENT_LIFECYCLE_V2.PUBLISHING && e.startedAt != null && (t - e.startedAt) >= _nsEventStuckMs) { e.status = NOSTR_EVENT_LIFECYCLE_V2.FAILED; e.updatedAt = t; if (!e.settledAt) e.settledAt = t; e.metadata.failReason = "auto-fail-stuck"; flipped.push(e.id); } return { flipped, count: flipped.length }; }
663
+ export function autoOfflineIdleNostrRelaysV2({ now } = {}) {
664
+ const t = now ?? Date.now();
665
+ const flipped = [];
666
+ for (const r of _nsRelays.values())
667
+ if (
668
+ r.status === NOSTR_RELAY_MATURITY_V2.ACTIVE &&
669
+ t - r.lastTouchedAt >= _nsRelayIdleMs
670
+ ) {
671
+ r.status = NOSTR_RELAY_MATURITY_V2.OFFLINE;
672
+ r.updatedAt = t;
673
+ flipped.push(r.id);
674
+ }
675
+ return { flipped, count: flipped.length };
676
+ }
677
+ export function autoFailStuckNostrEventsV2({ now } = {}) {
678
+ const t = now ?? Date.now();
679
+ const flipped = [];
680
+ for (const e of _nsEvents.values())
681
+ if (
682
+ e.status === NOSTR_EVENT_LIFECYCLE_V2.PUBLISHING &&
683
+ e.startedAt != null &&
684
+ t - e.startedAt >= _nsEventStuckMs
685
+ ) {
686
+ e.status = NOSTR_EVENT_LIFECYCLE_V2.FAILED;
687
+ e.updatedAt = t;
688
+ if (!e.settledAt) e.settledAt = t;
689
+ e.metadata.failReason = "auto-fail-stuck";
690
+ flipped.push(e.id);
691
+ }
692
+ return { flipped, count: flipped.length };
693
+ }
471
694
 
472
695
  export function getNostrBridgeStatsV2() {
473
- const relaysByStatus = {}; for (const s of Object.values(NOSTR_RELAY_MATURITY_V2)) relaysByStatus[s] = 0; for (const r of _nsRelays.values()) relaysByStatus[r.status]++;
474
- const eventsByStatus = {}; for (const s of Object.values(NOSTR_EVENT_LIFECYCLE_V2)) eventsByStatus[s] = 0; for (const e of _nsEvents.values()) eventsByStatus[e.status]++;
475
- return { totalRelaysV2: _nsRelays.size, totalEventsV2: _nsEvents.size, maxActiveNostrRelaysPerOwner: _nsMaxActivePerOwner, maxPendingNostrEventsPerRelay: _nsMaxPendingPerRelay, nostrRelayIdleMs: _nsRelayIdleMs, nostrEventStuckMs: _nsEventStuckMs, relaysByStatus, eventsByStatus };
696
+ const relaysByStatus = {};
697
+ for (const s of Object.values(NOSTR_RELAY_MATURITY_V2)) relaysByStatus[s] = 0;
698
+ for (const r of _nsRelays.values()) relaysByStatus[r.status]++;
699
+ const eventsByStatus = {};
700
+ for (const s of Object.values(NOSTR_EVENT_LIFECYCLE_V2))
701
+ eventsByStatus[s] = 0;
702
+ for (const e of _nsEvents.values()) eventsByStatus[e.status]++;
703
+ return {
704
+ totalRelaysV2: _nsRelays.size,
705
+ totalEventsV2: _nsEvents.size,
706
+ maxActiveNostrRelaysPerOwner: _nsMaxActivePerOwner,
707
+ maxPendingNostrEventsPerRelay: _nsMaxPendingPerRelay,
708
+ nostrRelayIdleMs: _nsRelayIdleMs,
709
+ nostrEventStuckMs: _nsEventStuckMs,
710
+ relaysByStatus,
711
+ eventsByStatus,
712
+ };
476
713
  }