chainlesschain 0.81.0 → 0.143.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 (209) hide show
  1. package/bin/chainlesschain.js +0 -0
  2. package/package.json +1 -1
  3. package/src/commands/a2a.js +62 -0
  4. package/src/commands/activitypub.js +61 -0
  5. package/src/commands/agent-network.js +254 -1
  6. package/src/commands/agent.js +117 -0
  7. package/src/commands/audit.js +302 -0
  8. package/src/commands/automation.js +271 -1
  9. package/src/commands/bi.js +61 -0
  10. package/src/commands/bm25.js +78 -0
  11. package/src/commands/browse.js +64 -0
  12. package/src/commands/ccron.js +78 -0
  13. package/src/commands/codegen.js +224 -0
  14. package/src/commands/collab.js +341 -0
  15. package/src/commands/compliance.js +1075 -0
  16. package/src/commands/compt.js +78 -0
  17. package/src/commands/consol.js +231 -0
  18. package/src/commands/cowork.js +263 -0
  19. package/src/commands/crosschain.js +62 -0
  20. package/src/commands/dao.js +62 -0
  21. package/src/commands/dbevo.js +284 -0
  22. package/src/commands/dev.js +252 -0
  23. package/src/commands/did.js +358 -0
  24. package/src/commands/dlp.js +61 -0
  25. package/src/commands/economy.js +56 -0
  26. package/src/commands/encrypt.js +341 -0
  27. package/src/commands/evolution.js +56 -0
  28. package/src/commands/evomap.js +61 -0
  29. package/src/commands/export.js +256 -1
  30. package/src/commands/fflag.js +178 -0
  31. package/src/commands/fusion.js +258 -0
  32. package/src/commands/git.js +45 -0
  33. package/src/commands/governance.js +325 -0
  34. package/src/commands/hardening.js +411 -0
  35. package/src/commands/hmemory.js +56 -0
  36. package/src/commands/hook.js +148 -0
  37. package/src/commands/import.js +252 -0
  38. package/src/commands/incentive.js +322 -0
  39. package/src/commands/inference.js +42 -0
  40. package/src/commands/infra.js +244 -0
  41. package/src/commands/instinct.js +260 -0
  42. package/src/commands/ipfs.js +318 -0
  43. package/src/commands/itbudget.js +45 -0
  44. package/src/commands/kg.js +387 -0
  45. package/src/commands/llm.js +263 -0
  46. package/src/commands/lowcode.js +44 -0
  47. package/src/commands/matrix.js +62 -0
  48. package/src/commands/mcp.js +221 -0
  49. package/src/commands/mcpscaf.js +41 -0
  50. package/src/commands/meminj.js +41 -0
  51. package/src/commands/memory.js +248 -0
  52. package/src/commands/multimodal.js +296 -0
  53. package/src/commands/nlprog.js +356 -0
  54. package/src/commands/nostr.js +62 -0
  55. package/src/commands/note.js +244 -0
  56. package/src/commands/ops.js +354 -0
  57. package/src/commands/orchestrate.js +166 -0
  58. package/src/commands/orchgov.js +45 -0
  59. package/src/commands/org.js +277 -0
  60. package/src/commands/p2p.js +390 -0
  61. package/src/commands/pdfp.js +78 -0
  62. package/src/commands/perception.js +290 -0
  63. package/src/commands/perf.js +39 -0
  64. package/src/commands/perm.js +45 -0
  65. package/src/commands/permmem.js +251 -0
  66. package/src/commands/pipeline.js +57 -1
  67. package/src/commands/planmode.js +45 -0
  68. package/src/commands/plugin-ecosystem.js +273 -0
  69. package/src/commands/pqc.js +393 -0
  70. package/src/commands/promcomp.js +82 -0
  71. package/src/commands/quantization.js +351 -0
  72. package/src/commands/rcache.js +271 -0
  73. package/src/commands/recommend.js +382 -0
  74. package/src/commands/runtime.js +307 -0
  75. package/src/commands/scim.js +262 -0
  76. package/src/commands/seshhook.js +41 -0
  77. package/src/commands/seshsearch.js +41 -0
  78. package/src/commands/seshtail.js +41 -0
  79. package/src/commands/seshu.js +41 -0
  80. package/src/commands/session.js +258 -0
  81. package/src/commands/sganal.js +78 -0
  82. package/src/commands/siem.js +40 -0
  83. package/src/commands/skill.js +267 -1
  84. package/src/commands/slotfill.js +41 -0
  85. package/src/commands/social.js +290 -0
  86. package/src/commands/sso.js +186 -1
  87. package/src/commands/svccont.js +45 -0
  88. package/src/commands/sync.js +256 -0
  89. package/src/commands/tech.js +338 -0
  90. package/src/commands/tenant.js +351 -0
  91. package/src/commands/tms.js +45 -0
  92. package/src/commands/tokens.js +269 -0
  93. package/src/commands/topiccls.js +45 -0
  94. package/src/commands/trust.js +249 -0
  95. package/src/commands/uprof.js +45 -0
  96. package/src/commands/vcheck.js +78 -0
  97. package/src/commands/wallet.js +277 -0
  98. package/src/commands/webfetch.js +41 -0
  99. package/src/commands/workflow.js +171 -0
  100. package/src/commands/zkp.js +62 -0
  101. package/src/harness/prompt-compressor.js +331 -0
  102. package/src/index.js +65 -1
  103. package/src/lib/a2a-protocol.js +105 -0
  104. package/src/lib/activitypub-bridge.js +105 -0
  105. package/src/lib/agent-coordinator.js +325 -0
  106. package/src/lib/agent-economy.js +105 -0
  107. package/src/lib/agent-network.js +387 -0
  108. package/src/lib/agent-router.js +395 -0
  109. package/src/lib/aiops.js +478 -0
  110. package/src/lib/app-builder.js +105 -0
  111. package/src/lib/audit-logger.js +379 -0
  112. package/src/lib/automation-engine.js +330 -0
  113. package/src/lib/autonomous-agent.js +105 -0
  114. package/src/lib/autonomous-developer.js +350 -0
  115. package/src/lib/bi-engine.js +105 -0
  116. package/src/lib/bm25-search.js +81 -0
  117. package/src/lib/browser-automation.js +105 -0
  118. package/src/lib/code-agent.js +323 -0
  119. package/src/lib/collaboration-governance.js +364 -0
  120. package/src/lib/community-governance.js +436 -0
  121. package/src/lib/compliance-framework-reporter.js +105 -0
  122. package/src/lib/compliance-manager.js +434 -0
  123. package/src/lib/compression-telemetry.js +81 -0
  124. package/src/lib/content-recommendation.js +469 -0
  125. package/src/lib/content-recommender.js +105 -0
  126. package/src/lib/cowork-cron.js +81 -0
  127. package/src/lib/cowork-task-runner.js +105 -0
  128. package/src/lib/cross-chain.js +105 -0
  129. package/src/lib/crypto-manager.js +350 -0
  130. package/src/lib/dao-governance.js +105 -0
  131. package/src/lib/dbevo.js +338 -0
  132. package/src/lib/decentral-infra.js +340 -0
  133. package/src/lib/did-manager.js +367 -0
  134. package/src/lib/dlp-engine.js +105 -0
  135. package/src/lib/evolution-system.js +105 -0
  136. package/src/lib/evomap-manager.js +105 -0
  137. package/src/lib/execution-backend.js +105 -0
  138. package/src/lib/feature-flags.js +85 -0
  139. package/src/lib/git-integration.js +105 -0
  140. package/src/lib/hardening-manager.js +348 -0
  141. package/src/lib/hierarchical-memory.js +105 -0
  142. package/src/lib/hook-manager.js +380 -0
  143. package/src/lib/inference-network.js +105 -0
  144. package/src/lib/instinct-manager.js +332 -0
  145. package/src/lib/ipfs-storage.js +334 -0
  146. package/src/lib/iteration-budget.js +105 -0
  147. package/src/lib/knowledge-exporter.js +381 -0
  148. package/src/lib/knowledge-graph.js +432 -0
  149. package/src/lib/knowledge-importer.js +379 -0
  150. package/src/lib/llm-providers.js +391 -0
  151. package/src/lib/matrix-bridge.js +105 -0
  152. package/src/lib/mcp-registry.js +333 -0
  153. package/src/lib/mcp-scaffold.js +81 -0
  154. package/src/lib/memory-injection.js +81 -0
  155. package/src/lib/memory-manager.js +330 -0
  156. package/src/lib/multimodal.js +346 -0
  157. package/src/lib/nl-programming.js +343 -0
  158. package/src/lib/nostr-bridge.js +105 -0
  159. package/src/lib/note-versioning.js +327 -0
  160. package/src/lib/orchestrator.js +105 -0
  161. package/src/lib/org-manager.js +323 -0
  162. package/src/lib/p2p-manager.js +387 -0
  163. package/src/lib/pdf-parser.js +81 -0
  164. package/src/lib/perception.js +346 -0
  165. package/src/lib/perf-tuning.js +109 -1
  166. package/src/lib/permanent-memory.js +320 -0
  167. package/src/lib/permission-engine.js +81 -0
  168. package/src/lib/pipeline-orchestrator.js +105 -0
  169. package/src/lib/plan-mode.js +81 -0
  170. package/src/lib/plugin-ecosystem.js +377 -0
  171. package/src/lib/pqc-manager.js +368 -0
  172. package/src/lib/prompt-compressor.js +1 -10
  173. package/src/lib/protocol-fusion.js +417 -0
  174. package/src/lib/quantization.js +325 -0
  175. package/src/lib/response-cache.js +327 -0
  176. package/src/lib/scim-manager.js +329 -0
  177. package/src/lib/service-container.js +81 -0
  178. package/src/lib/session-consolidator.js +105 -0
  179. package/src/lib/session-hooks.js +81 -0
  180. package/src/lib/session-manager.js +329 -0
  181. package/src/lib/session-search.js +81 -0
  182. package/src/lib/session-tail.js +81 -0
  183. package/src/lib/session-usage.js +83 -0
  184. package/src/lib/siem-exporter.js +105 -0
  185. package/src/lib/skill-loader.js +377 -0
  186. package/src/lib/slot-filler.js +81 -0
  187. package/src/lib/social-graph-analytics.js +81 -0
  188. package/src/lib/social-graph.js +81 -0
  189. package/src/lib/social-manager.js +326 -0
  190. package/src/lib/sso-manager.js +332 -0
  191. package/src/lib/sub-agent-registry.js +110 -0
  192. package/src/lib/sync-manager.js +326 -0
  193. package/src/lib/task-model-selector.js +81 -0
  194. package/src/lib/tech-learning-engine.js +369 -0
  195. package/src/lib/tenant-saas.js +460 -0
  196. package/src/lib/threat-intel.js +335 -0
  197. package/src/lib/todo-manager.js +105 -0
  198. package/src/lib/token-incentive.js +293 -0
  199. package/src/lib/token-tracker.js +329 -0
  200. package/src/lib/topic-classifier.js +105 -0
  201. package/src/lib/trust-security.js +390 -0
  202. package/src/lib/ueba.js +389 -0
  203. package/src/lib/universal-runtime.js +325 -0
  204. package/src/lib/user-profile.js +81 -0
  205. package/src/lib/version-checker.js +81 -0
  206. package/src/lib/wallet-manager.js +326 -0
  207. package/src/lib/web-fetch.js +81 -0
  208. package/src/lib/workflow-engine.js +322 -0
  209. package/src/lib/zkp-engine.js +105 -0
@@ -398,3 +398,108 @@ export function classifyTopic(text, opts = {}) {
398
398
  export function _resetState() {
399
399
  _customLexicons.clear();
400
400
  }
401
+
402
+
403
+ // ===== V2 Surface: Topic Classifier governance overlay (CLI v0.140.0) =====
404
+ export const TOPIC_CLS_PROFILE_MATURITY_V2 = Object.freeze({
405
+ PENDING: "pending", ACTIVE: "active", STALE: "stale", ARCHIVED: "archived",
406
+ });
407
+ export const TOPIC_CLS_JOB_LIFECYCLE_V2 = Object.freeze({
408
+ QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
409
+ });
410
+
411
+ const _tcpTrans = new Map([
412
+ [TOPIC_CLS_PROFILE_MATURITY_V2.PENDING, new Set([TOPIC_CLS_PROFILE_MATURITY_V2.ACTIVE, TOPIC_CLS_PROFILE_MATURITY_V2.ARCHIVED])],
413
+ [TOPIC_CLS_PROFILE_MATURITY_V2.ACTIVE, new Set([TOPIC_CLS_PROFILE_MATURITY_V2.STALE, TOPIC_CLS_PROFILE_MATURITY_V2.ARCHIVED])],
414
+ [TOPIC_CLS_PROFILE_MATURITY_V2.STALE, new Set([TOPIC_CLS_PROFILE_MATURITY_V2.ACTIVE, TOPIC_CLS_PROFILE_MATURITY_V2.ARCHIVED])],
415
+ [TOPIC_CLS_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
416
+ ]);
417
+ const _tcpTerminal = new Set([TOPIC_CLS_PROFILE_MATURITY_V2.ARCHIVED]);
418
+ const _tcjTrans = new Map([
419
+ [TOPIC_CLS_JOB_LIFECYCLE_V2.QUEUED, new Set([TOPIC_CLS_JOB_LIFECYCLE_V2.RUNNING, TOPIC_CLS_JOB_LIFECYCLE_V2.CANCELLED])],
420
+ [TOPIC_CLS_JOB_LIFECYCLE_V2.RUNNING, new Set([TOPIC_CLS_JOB_LIFECYCLE_V2.COMPLETED, TOPIC_CLS_JOB_LIFECYCLE_V2.FAILED, TOPIC_CLS_JOB_LIFECYCLE_V2.CANCELLED])],
421
+ [TOPIC_CLS_JOB_LIFECYCLE_V2.COMPLETED, new Set()],
422
+ [TOPIC_CLS_JOB_LIFECYCLE_V2.FAILED, new Set()],
423
+ [TOPIC_CLS_JOB_LIFECYCLE_V2.CANCELLED, new Set()],
424
+ ]);
425
+
426
+ const _tcpsV2 = new Map();
427
+ const _tcjsV2 = new Map();
428
+ let _tcpMaxActivePerOwner = 8;
429
+ let _tcpMaxPendingJobsPerProfile = 20;
430
+ let _tcpIdleMs = 14 * 24 * 60 * 60 * 1000;
431
+ let _tcjStuckMs = 5 * 60 * 1000;
432
+
433
+ function _tcpPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
434
+
435
+ export function setMaxActiveTopicClsProfilesPerOwnerV2(n) { _tcpMaxActivePerOwner = _tcpPos(n, "maxActiveTopicClsProfilesPerOwner"); }
436
+ export function getMaxActiveTopicClsProfilesPerOwnerV2() { return _tcpMaxActivePerOwner; }
437
+ export function setMaxPendingTopicClsJobsPerProfileV2(n) { _tcpMaxPendingJobsPerProfile = _tcpPos(n, "maxPendingTopicClsJobsPerProfile"); }
438
+ export function getMaxPendingTopicClsJobsPerProfileV2() { return _tcpMaxPendingJobsPerProfile; }
439
+ export function setTopicClsProfileIdleMsV2(n) { _tcpIdleMs = _tcpPos(n, "topicClsProfileIdleMs"); }
440
+ export function getTopicClsProfileIdleMsV2() { return _tcpIdleMs; }
441
+ export function setTopicClsJobStuckMsV2(n) { _tcjStuckMs = _tcpPos(n, "topicClsJobStuckMs"); }
442
+ export function getTopicClsJobStuckMsV2() { return _tcjStuckMs; }
443
+
444
+ export function _resetStateTopicClsV2() {
445
+ _tcpsV2.clear(); _tcjsV2.clear();
446
+ _tcpMaxActivePerOwner = 8; _tcpMaxPendingJobsPerProfile = 20;
447
+ _tcpIdleMs = 14 * 24 * 60 * 60 * 1000; _tcjStuckMs = 5 * 60 * 1000;
448
+ }
449
+
450
+ export function registerTopicClsProfileV2({ id, owner, model, metadata } = {}) {
451
+ if (!id || typeof id !== "string") throw new Error("id is required");
452
+ if (!owner || typeof owner !== "string") throw new Error("owner is required");
453
+ if (_tcpsV2.has(id)) throw new Error(`topic cls profile ${id} already registered`);
454
+ const now = Date.now();
455
+ const p = { id, owner, model: model || "default", status: TOPIC_CLS_PROFILE_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
456
+ _tcpsV2.set(id, p);
457
+ return { ...p, metadata: { ...p.metadata } };
458
+ }
459
+ function _tcpCheckP(from, to) { const a = _tcpTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid topic cls profile transition ${from} → ${to}`); }
460
+ function _tcpCountActive(owner) { let n = 0; for (const p of _tcpsV2.values()) if (p.owner === owner && p.status === TOPIC_CLS_PROFILE_MATURITY_V2.ACTIVE) n++; return n; }
461
+
462
+ export function activateTopicClsProfileV2(id) {
463
+ const p = _tcpsV2.get(id); if (!p) throw new Error(`topic cls profile ${id} not found`);
464
+ _tcpCheckP(p.status, TOPIC_CLS_PROFILE_MATURITY_V2.ACTIVE);
465
+ const recovery = p.status === TOPIC_CLS_PROFILE_MATURITY_V2.STALE;
466
+ if (!recovery) { const c = _tcpCountActive(p.owner); if (c >= _tcpMaxActivePerOwner) throw new Error(`max active topic cls profiles per owner (${_tcpMaxActivePerOwner}) reached for ${p.owner}`); }
467
+ const now = Date.now(); p.status = TOPIC_CLS_PROFILE_MATURITY_V2.ACTIVE; p.updatedAt = now; p.lastTouchedAt = now; if (!p.activatedAt) p.activatedAt = now;
468
+ return { ...p, metadata: { ...p.metadata } };
469
+ }
470
+ export function staleTopicClsProfileV2(id) { const p = _tcpsV2.get(id); if (!p) throw new Error(`topic cls profile ${id} not found`); _tcpCheckP(p.status, TOPIC_CLS_PROFILE_MATURITY_V2.STALE); p.status = TOPIC_CLS_PROFILE_MATURITY_V2.STALE; p.updatedAt = Date.now(); return { ...p, metadata: { ...p.metadata } }; }
471
+ export function archiveTopicClsProfileV2(id) { const p = _tcpsV2.get(id); if (!p) throw new Error(`topic cls profile ${id} not found`); _tcpCheckP(p.status, TOPIC_CLS_PROFILE_MATURITY_V2.ARCHIVED); const now = Date.now(); p.status = TOPIC_CLS_PROFILE_MATURITY_V2.ARCHIVED; p.updatedAt = now; if (!p.archivedAt) p.archivedAt = now; return { ...p, metadata: { ...p.metadata } }; }
472
+ export function touchTopicClsProfileV2(id) { const p = _tcpsV2.get(id); if (!p) throw new Error(`topic cls profile ${id} not found`); if (_tcpTerminal.has(p.status)) throw new Error(`cannot touch terminal topic cls profile ${id}`); const now = Date.now(); p.lastTouchedAt = now; p.updatedAt = now; return { ...p, metadata: { ...p.metadata } }; }
473
+ export function getTopicClsProfileV2(id) { const p = _tcpsV2.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
474
+ export function listTopicClsProfilesV2() { return [..._tcpsV2.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
475
+
476
+ function _tcjCountPending(profileId) { let n = 0; for (const j of _tcjsV2.values()) if (j.profileId === profileId && (j.status === TOPIC_CLS_JOB_LIFECYCLE_V2.QUEUED || j.status === TOPIC_CLS_JOB_LIFECYCLE_V2.RUNNING)) n++; return n; }
477
+
478
+ export function createTopicClsJobV2({ id, profileId, text, metadata } = {}) {
479
+ if (!id || typeof id !== "string") throw new Error("id is required");
480
+ if (!profileId || typeof profileId !== "string") throw new Error("profileId is required");
481
+ if (_tcjsV2.has(id)) throw new Error(`topic cls job ${id} already exists`);
482
+ if (!_tcpsV2.has(profileId)) throw new Error(`topic cls profile ${profileId} not found`);
483
+ const pending = _tcjCountPending(profileId);
484
+ if (pending >= _tcpMaxPendingJobsPerProfile) throw new Error(`max pending topic cls jobs per profile (${_tcpMaxPendingJobsPerProfile}) reached for ${profileId}`);
485
+ const now = Date.now();
486
+ const j = { id, profileId, text: text || "", status: TOPIC_CLS_JOB_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
487
+ _tcjsV2.set(id, j);
488
+ return { ...j, metadata: { ...j.metadata } };
489
+ }
490
+ function _tcjCheckJ(from, to) { const a = _tcjTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid topic cls job transition ${from} → ${to}`); }
491
+ export function startTopicClsJobV2(id) { const j = _tcjsV2.get(id); if (!j) throw new Error(`topic cls job ${id} not found`); _tcjCheckJ(j.status, TOPIC_CLS_JOB_LIFECYCLE_V2.RUNNING); const now = Date.now(); j.status = TOPIC_CLS_JOB_LIFECYCLE_V2.RUNNING; j.updatedAt = now; if (!j.startedAt) j.startedAt = now; return { ...j, metadata: { ...j.metadata } }; }
492
+ export function completeTopicClsJobV2(id) { const j = _tcjsV2.get(id); if (!j) throw new Error(`topic cls job ${id} not found`); _tcjCheckJ(j.status, TOPIC_CLS_JOB_LIFECYCLE_V2.COMPLETED); const now = Date.now(); j.status = TOPIC_CLS_JOB_LIFECYCLE_V2.COMPLETED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; return { ...j, metadata: { ...j.metadata } }; }
493
+ export function failTopicClsJobV2(id, reason) { const j = _tcjsV2.get(id); if (!j) throw new Error(`topic cls job ${id} not found`); _tcjCheckJ(j.status, TOPIC_CLS_JOB_LIFECYCLE_V2.FAILED); const now = Date.now(); j.status = TOPIC_CLS_JOB_LIFECYCLE_V2.FAILED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; if (reason) j.metadata.failReason = String(reason); return { ...j, metadata: { ...j.metadata } }; }
494
+ export function cancelTopicClsJobV2(id, reason) { const j = _tcjsV2.get(id); if (!j) throw new Error(`topic cls job ${id} not found`); _tcjCheckJ(j.status, TOPIC_CLS_JOB_LIFECYCLE_V2.CANCELLED); const now = Date.now(); j.status = TOPIC_CLS_JOB_LIFECYCLE_V2.CANCELLED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; if (reason) j.metadata.cancelReason = String(reason); return { ...j, metadata: { ...j.metadata } }; }
495
+ export function getTopicClsJobV2(id) { const j = _tcjsV2.get(id); if (!j) return null; return { ...j, metadata: { ...j.metadata } }; }
496
+ export function listTopicClsJobsV2() { return [..._tcjsV2.values()].map((j) => ({ ...j, metadata: { ...j.metadata } })); }
497
+
498
+ export function autoStaleIdleTopicClsProfilesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _tcpsV2.values()) if (p.status === TOPIC_CLS_PROFILE_MATURITY_V2.ACTIVE && (t - p.lastTouchedAt) >= _tcpIdleMs) { p.status = TOPIC_CLS_PROFILE_MATURITY_V2.STALE; p.updatedAt = t; flipped.push(p.id); } return { flipped, count: flipped.length }; }
499
+ export function autoFailStuckTopicClsJobsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const j of _tcjsV2.values()) if (j.status === TOPIC_CLS_JOB_LIFECYCLE_V2.RUNNING && j.startedAt != null && (t - j.startedAt) >= _tcjStuckMs) { j.status = TOPIC_CLS_JOB_LIFECYCLE_V2.FAILED; j.updatedAt = t; if (!j.settledAt) j.settledAt = t; j.metadata.failReason = "auto-fail-stuck"; flipped.push(j.id); } return { flipped, count: flipped.length }; }
500
+
501
+ export function getTopicClassifierGovStatsV2() {
502
+ const profilesByStatus = {}; for (const s of Object.values(TOPIC_CLS_PROFILE_MATURITY_V2)) profilesByStatus[s] = 0; for (const p of _tcpsV2.values()) profilesByStatus[p.status]++;
503
+ const jobsByStatus = {}; for (const s of Object.values(TOPIC_CLS_JOB_LIFECYCLE_V2)) jobsByStatus[s] = 0; for (const j of _tcjsV2.values()) jobsByStatus[j.status]++;
504
+ return { totalTopicClsProfilesV2: _tcpsV2.size, totalTopicClsJobsV2: _tcjsV2.size, maxActiveTopicClsProfilesPerOwner: _tcpMaxActivePerOwner, maxPendingTopicClsJobsPerProfile: _tcpMaxPendingJobsPerProfile, topicClsProfileIdleMs: _tcpIdleMs, topicClsJobStuckMs: _tcjStuckMs, profilesByStatus, jobsByStatus };
505
+ }
@@ -471,3 +471,393 @@ export function _resetState() {
471
471
  _satMessages.clear();
472
472
  _hsmDevices.clear();
473
473
  }
474
+
475
+ /* ── V2 Surface (Phase 68-71) ─────────────────────────────
476
+ * Strictly additive. Two parallel state machines:
477
+ * - HSM device maturity (4 states, retired terminal)
478
+ * - Satellite transmission lifecycle (5 states, 3 terminals)
479
+ * Per-operator active-device cap + per-device pending-transmission cap.
480
+ * Auto-flip: stale devices → retired; stuck sending transmissions → failed.
481
+ */
482
+
483
+ export const HSM_MATURITY_V2 = Object.freeze({
484
+ PROVISIONAL: "provisional",
485
+ ACTIVE: "active",
486
+ DEGRADED: "degraded",
487
+ RETIRED: "retired",
488
+ });
489
+
490
+ export const TRANSMISSION_V2 = Object.freeze({
491
+ QUEUED: "queued",
492
+ SENDING: "sending",
493
+ CONFIRMED: "confirmed",
494
+ FAILED: "failed",
495
+ CANCELED: "canceled",
496
+ });
497
+
498
+ const _HSM_TRANSITIONS_V2 = new Map([
499
+ [
500
+ HSM_MATURITY_V2.PROVISIONAL,
501
+ new Set([HSM_MATURITY_V2.ACTIVE, HSM_MATURITY_V2.RETIRED]),
502
+ ],
503
+ [
504
+ HSM_MATURITY_V2.ACTIVE,
505
+ new Set([HSM_MATURITY_V2.DEGRADED, HSM_MATURITY_V2.RETIRED]),
506
+ ],
507
+ [
508
+ HSM_MATURITY_V2.DEGRADED,
509
+ new Set([HSM_MATURITY_V2.ACTIVE, HSM_MATURITY_V2.RETIRED]),
510
+ ],
511
+ ]);
512
+ const _HSM_TERMINAL_V2 = new Set([HSM_MATURITY_V2.RETIRED]);
513
+
514
+ const _TRANSMISSION_TRANSITIONS_V2 = new Map([
515
+ [
516
+ TRANSMISSION_V2.QUEUED,
517
+ new Set([
518
+ TRANSMISSION_V2.SENDING,
519
+ TRANSMISSION_V2.CANCELED,
520
+ TRANSMISSION_V2.FAILED,
521
+ ]),
522
+ ],
523
+ [
524
+ TRANSMISSION_V2.SENDING,
525
+ new Set([
526
+ TRANSMISSION_V2.CONFIRMED,
527
+ TRANSMISSION_V2.FAILED,
528
+ TRANSMISSION_V2.CANCELED,
529
+ ]),
530
+ ],
531
+ ]);
532
+ const _TRANSMISSION_TERMINAL_V2 = new Set([
533
+ TRANSMISSION_V2.CONFIRMED,
534
+ TRANSMISSION_V2.FAILED,
535
+ TRANSMISSION_V2.CANCELED,
536
+ ]);
537
+
538
+ export const TS_DEFAULT_MAX_ACTIVE_DEVICES_PER_OPERATOR = 8;
539
+ export const TS_DEFAULT_MAX_PENDING_TRANSMISSIONS_PER_DEVICE = 20;
540
+ export const TS_DEFAULT_DEVICE_IDLE_MS = 30 * 86400000;
541
+ export const TS_DEFAULT_TRANSMISSION_STUCK_MS = 2 * 60000;
542
+
543
+ let _tsMaxActiveDevicesPerOperator = TS_DEFAULT_MAX_ACTIVE_DEVICES_PER_OPERATOR;
544
+ let _tsMaxPendingTransmissionsPerDevice =
545
+ TS_DEFAULT_MAX_PENDING_TRANSMISSIONS_PER_DEVICE;
546
+ let _tsDeviceIdleMs = TS_DEFAULT_DEVICE_IDLE_MS;
547
+ let _tsTransmissionStuckMs = TS_DEFAULT_TRANSMISSION_STUCK_MS;
548
+
549
+ const _devicesV2 = new Map();
550
+ const _transmissionsV2 = new Map();
551
+
552
+ function _positiveIntV2(n, label) {
553
+ const f = Math.floor(n);
554
+ if (!Number.isFinite(f) || f <= 0)
555
+ throw new Error(`${label} must be a positive integer`);
556
+ return f;
557
+ }
558
+
559
+ export function getMaxActiveDevicesPerOperator() {
560
+ return _tsMaxActiveDevicesPerOperator;
561
+ }
562
+ export function setMaxActiveDevicesPerOperator(n) {
563
+ _tsMaxActiveDevicesPerOperator = _positiveIntV2(
564
+ n,
565
+ "maxActiveDevicesPerOperator",
566
+ );
567
+ return _tsMaxActiveDevicesPerOperator;
568
+ }
569
+ export function getMaxPendingTransmissionsPerDevice() {
570
+ return _tsMaxPendingTransmissionsPerDevice;
571
+ }
572
+ export function setMaxPendingTransmissionsPerDevice(n) {
573
+ _tsMaxPendingTransmissionsPerDevice = _positiveIntV2(
574
+ n,
575
+ "maxPendingTransmissionsPerDevice",
576
+ );
577
+ return _tsMaxPendingTransmissionsPerDevice;
578
+ }
579
+ export function getDeviceIdleMs() {
580
+ return _tsDeviceIdleMs;
581
+ }
582
+ export function setDeviceIdleMs(n) {
583
+ _tsDeviceIdleMs = _positiveIntV2(n, "deviceIdleMs");
584
+ return _tsDeviceIdleMs;
585
+ }
586
+ export function getTransmissionStuckMs() {
587
+ return _tsTransmissionStuckMs;
588
+ }
589
+ export function setTransmissionStuckMs(n) {
590
+ _tsTransmissionStuckMs = _positiveIntV2(n, "transmissionStuckMs");
591
+ return _tsTransmissionStuckMs;
592
+ }
593
+
594
+ export function getActiveDeviceCount(operator) {
595
+ let c = 0;
596
+ for (const d of _devicesV2.values()) {
597
+ if (d.status === HSM_MATURITY_V2.RETIRED) continue;
598
+ if (d.status === HSM_MATURITY_V2.PROVISIONAL) continue;
599
+ if (operator !== undefined && d.operator !== operator) continue;
600
+ c++;
601
+ }
602
+ return c;
603
+ }
604
+
605
+ export function getPendingTransmissionCount(deviceId) {
606
+ let c = 0;
607
+ for (const t of _transmissionsV2.values()) {
608
+ if (_TRANSMISSION_TERMINAL_V2.has(t.status)) continue;
609
+ if (deviceId !== undefined && t.deviceId !== deviceId) continue;
610
+ c++;
611
+ }
612
+ return c;
613
+ }
614
+
615
+ const _VALID_VENDORS = new Set(Object.values(HSM_VENDOR));
616
+
617
+ export function registerDeviceV2({
618
+ id,
619
+ operator,
620
+ vendor,
621
+ initialStatus,
622
+ metadata,
623
+ } = {}) {
624
+ if (!id) throw new Error("id required");
625
+ if (!operator) throw new Error("operator required");
626
+ if (!vendor || !_VALID_VENDORS.has(vendor)) throw new Error("invalid vendor");
627
+ if (_devicesV2.has(id)) throw new Error(`device ${id} already exists`);
628
+ const status = initialStatus ?? HSM_MATURITY_V2.PROVISIONAL;
629
+ if (!Object.values(HSM_MATURITY_V2).includes(status))
630
+ throw new Error(`invalid initial status ${status}`);
631
+ const countsActive =
632
+ status !== HSM_MATURITY_V2.RETIRED &&
633
+ status !== HSM_MATURITY_V2.PROVISIONAL;
634
+ if (
635
+ countsActive &&
636
+ getActiveDeviceCount(operator) >= _tsMaxActiveDevicesPerOperator
637
+ )
638
+ throw new Error(`operator ${operator} active device cap reached`);
639
+ const now = _now();
640
+ const d = {
641
+ id,
642
+ operator,
643
+ vendor,
644
+ status,
645
+ metadata: metadata ? { ...metadata } : {},
646
+ createdAt: now,
647
+ updatedAt: now,
648
+ activatedAt: status === HSM_MATURITY_V2.ACTIVE ? now : null,
649
+ lastUsedAt: now,
650
+ };
651
+ _devicesV2.set(id, d);
652
+ return { ...d, metadata: { ...d.metadata } };
653
+ }
654
+
655
+ export function getDeviceV2(id) {
656
+ const d = _devicesV2.get(id);
657
+ return d ? { ...d, metadata: { ...d.metadata } } : null;
658
+ }
659
+
660
+ export function listDevicesV2({ operator, status } = {}) {
661
+ const out = [];
662
+ for (const d of _devicesV2.values()) {
663
+ if (operator !== undefined && d.operator !== operator) continue;
664
+ if (status !== undefined && d.status !== status) continue;
665
+ out.push({ ...d, metadata: { ...d.metadata } });
666
+ }
667
+ return out;
668
+ }
669
+
670
+ export function setDeviceMaturityV2(id, nextStatus, { reason, metadata } = {}) {
671
+ const d = _devicesV2.get(id);
672
+ if (!d) throw new Error(`device ${id} not found`);
673
+ if (_HSM_TERMINAL_V2.has(d.status))
674
+ throw new Error(`device ${id} is terminal (${d.status})`);
675
+ const allowed = _HSM_TRANSITIONS_V2.get(d.status);
676
+ if (!allowed || !allowed.has(nextStatus))
677
+ throw new Error(`illegal transition ${d.status} → ${nextStatus}`);
678
+ const wasActive =
679
+ d.status !== HSM_MATURITY_V2.PROVISIONAL &&
680
+ d.status !== HSM_MATURITY_V2.RETIRED;
681
+ const willBeActive =
682
+ nextStatus !== HSM_MATURITY_V2.PROVISIONAL &&
683
+ nextStatus !== HSM_MATURITY_V2.RETIRED;
684
+ if (!wasActive && willBeActive) {
685
+ if (getActiveDeviceCount(d.operator) >= _tsMaxActiveDevicesPerOperator)
686
+ throw new Error(`operator ${d.operator} active device cap reached`);
687
+ }
688
+ d.status = nextStatus;
689
+ d.updatedAt = _now();
690
+ if (reason !== undefined) d.reason = reason;
691
+ if (metadata) d.metadata = { ...d.metadata, ...metadata };
692
+ if (nextStatus === HSM_MATURITY_V2.ACTIVE && !d.activatedAt)
693
+ d.activatedAt = d.updatedAt;
694
+ return { ...d, metadata: { ...d.metadata } };
695
+ }
696
+
697
+ export function activateDevice(id, opts) {
698
+ return setDeviceMaturityV2(id, HSM_MATURITY_V2.ACTIVE, opts);
699
+ }
700
+ export function degradeDevice(id, opts) {
701
+ return setDeviceMaturityV2(id, HSM_MATURITY_V2.DEGRADED, opts);
702
+ }
703
+ export function retireDevice(id, opts) {
704
+ return setDeviceMaturityV2(id, HSM_MATURITY_V2.RETIRED, opts);
705
+ }
706
+
707
+ export function touchDeviceUsage(id) {
708
+ const d = _devicesV2.get(id);
709
+ if (!d) throw new Error(`device ${id} not found`);
710
+ d.lastUsedAt = _now();
711
+ return { ...d, metadata: { ...d.metadata } };
712
+ }
713
+
714
+ export function enqueueTransmissionV2({
715
+ id,
716
+ deviceId,
717
+ provider,
718
+ payload,
719
+ metadata,
720
+ } = {}) {
721
+ if (!id) throw new Error("id required");
722
+ if (!deviceId) throw new Error("deviceId required");
723
+ if (!provider) throw new Error("provider required");
724
+ if (!payload) throw new Error("payload required");
725
+ const d = _devicesV2.get(deviceId);
726
+ if (!d) throw new Error(`device ${deviceId} not found`);
727
+ if (_transmissionsV2.has(id))
728
+ throw new Error(`transmission ${id} already exists`);
729
+ if (
730
+ getPendingTransmissionCount(deviceId) >= _tsMaxPendingTransmissionsPerDevice
731
+ )
732
+ throw new Error(`device ${deviceId} pending transmission cap reached`);
733
+ const now = _now();
734
+ const t = {
735
+ id,
736
+ deviceId,
737
+ provider,
738
+ payload,
739
+ status: TRANSMISSION_V2.QUEUED,
740
+ metadata: metadata ? { ...metadata } : {},
741
+ createdAt: now,
742
+ updatedAt: now,
743
+ startedAt: null,
744
+ };
745
+ _transmissionsV2.set(id, t);
746
+ return { ...t, metadata: { ...t.metadata } };
747
+ }
748
+
749
+ export function getTransmissionV2(id) {
750
+ const t = _transmissionsV2.get(id);
751
+ return t ? { ...t, metadata: { ...t.metadata } } : null;
752
+ }
753
+
754
+ export function listTransmissionsV2({ deviceId, status } = {}) {
755
+ const out = [];
756
+ for (const t of _transmissionsV2.values()) {
757
+ if (deviceId !== undefined && t.deviceId !== deviceId) continue;
758
+ if (status !== undefined && t.status !== status) continue;
759
+ out.push({ ...t, metadata: { ...t.metadata } });
760
+ }
761
+ return out;
762
+ }
763
+
764
+ export function setTransmissionStatusV2(
765
+ id,
766
+ nextStatus,
767
+ { reason, metadata } = {},
768
+ ) {
769
+ const t = _transmissionsV2.get(id);
770
+ if (!t) throw new Error(`transmission ${id} not found`);
771
+ if (_TRANSMISSION_TERMINAL_V2.has(t.status))
772
+ throw new Error(`transmission ${id} is terminal (${t.status})`);
773
+ const allowed = _TRANSMISSION_TRANSITIONS_V2.get(t.status);
774
+ if (!allowed || !allowed.has(nextStatus))
775
+ throw new Error(`illegal transition ${t.status} → ${nextStatus}`);
776
+ t.status = nextStatus;
777
+ t.updatedAt = _now();
778
+ if (reason !== undefined) t.reason = reason;
779
+ if (metadata) t.metadata = { ...t.metadata, ...metadata };
780
+ if (nextStatus === TRANSMISSION_V2.SENDING && !t.startedAt)
781
+ t.startedAt = t.updatedAt;
782
+ return { ...t, metadata: { ...t.metadata } };
783
+ }
784
+
785
+ export function startTransmission(id, opts) {
786
+ return setTransmissionStatusV2(id, TRANSMISSION_V2.SENDING, opts);
787
+ }
788
+ export function confirmTransmission(id, opts) {
789
+ return setTransmissionStatusV2(id, TRANSMISSION_V2.CONFIRMED, opts);
790
+ }
791
+ export function failTransmission(id, opts) {
792
+ return setTransmissionStatusV2(id, TRANSMISSION_V2.FAILED, opts);
793
+ }
794
+ export function cancelTransmission(id, opts) {
795
+ return setTransmissionStatusV2(id, TRANSMISSION_V2.CANCELED, opts);
796
+ }
797
+
798
+ export function autoRetireIdleDevices({ now } = {}) {
799
+ const cutoff = (now ?? _now()) - _tsDeviceIdleMs;
800
+ const flipped = [];
801
+ for (const d of _devicesV2.values()) {
802
+ if (
803
+ d.status !== HSM_MATURITY_V2.ACTIVE &&
804
+ d.status !== HSM_MATURITY_V2.DEGRADED
805
+ )
806
+ continue;
807
+ if ((d.lastUsedAt ?? d.createdAt) > cutoff) continue;
808
+ d.status = HSM_MATURITY_V2.RETIRED;
809
+ d.updatedAt = now ?? _now();
810
+ d.reason = "auto_retire_idle";
811
+ flipped.push(d.id);
812
+ }
813
+ return flipped;
814
+ }
815
+
816
+ export function autoFailStuckTransmissions({ now } = {}) {
817
+ const cutoff = (now ?? _now()) - _tsTransmissionStuckMs;
818
+ const flipped = [];
819
+ for (const t of _transmissionsV2.values()) {
820
+ if (t.status !== TRANSMISSION_V2.SENDING) continue;
821
+ if (!t.startedAt || t.startedAt > cutoff) continue;
822
+ t.status = TRANSMISSION_V2.FAILED;
823
+ t.updatedAt = now ?? _now();
824
+ t.reason = "auto_fail_stuck";
825
+ flipped.push(t.id);
826
+ }
827
+ return flipped;
828
+ }
829
+
830
+ function _zeroByEnum(enumObj) {
831
+ const out = {};
832
+ for (const v of Object.values(enumObj)) out[v] = 0;
833
+ return out;
834
+ }
835
+
836
+ export function getTrustSecurityStatsV2() {
837
+ const devices = [..._devicesV2.values()];
838
+ const transmissions = [..._transmissionsV2.values()];
839
+ const devicesByStatus = _zeroByEnum(HSM_MATURITY_V2);
840
+ for (const d of devices) devicesByStatus[d.status]++;
841
+ const transmissionsByStatus = _zeroByEnum(TRANSMISSION_V2);
842
+ for (const t of transmissions) transmissionsByStatus[t.status]++;
843
+ return {
844
+ totalDevicesV2: devices.length,
845
+ totalTransmissionsV2: transmissions.length,
846
+ maxActiveDevicesPerOperator: _tsMaxActiveDevicesPerOperator,
847
+ maxPendingTransmissionsPerDevice: _tsMaxPendingTransmissionsPerDevice,
848
+ deviceIdleMs: _tsDeviceIdleMs,
849
+ transmissionStuckMs: _tsTransmissionStuckMs,
850
+ devicesByStatus,
851
+ transmissionsByStatus,
852
+ };
853
+ }
854
+
855
+ export function _resetStateV2() {
856
+ _devicesV2.clear();
857
+ _transmissionsV2.clear();
858
+ _tsMaxActiveDevicesPerOperator = TS_DEFAULT_MAX_ACTIVE_DEVICES_PER_OPERATOR;
859
+ _tsMaxPendingTransmissionsPerDevice =
860
+ TS_DEFAULT_MAX_PENDING_TRANSMISSIONS_PER_DEVICE;
861
+ _tsDeviceIdleMs = TS_DEFAULT_DEVICE_IDLE_MS;
862
+ _tsTransmissionStuckMs = TS_DEFAULT_TRANSMISSION_STUCK_MS;
863
+ }