chainlesschain 0.143.0 → 0.152.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 (227) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +196 -28
  3. package/src/commands/activitypub.js +364 -27
  4. package/src/commands/agent-network.js +217 -0
  5. package/src/commands/agent.js +587 -103
  6. package/src/commands/audit.js +206 -0
  7. package/src/commands/automation.js +201 -0
  8. package/src/commands/bi.js +355 -27
  9. package/src/commands/bm25.js +111 -27
  10. package/src/commands/browse.js +403 -29
  11. package/src/commands/ccron.js +128 -28
  12. package/src/commands/chat.js +207 -0
  13. package/src/commands/codegen.js +207 -0
  14. package/src/commands/collab.js +211 -0
  15. package/src/commands/compliance.js +824 -34
  16. package/src/commands/compt.js +127 -29
  17. package/src/commands/consol.js +8 -2
  18. package/src/commands/cowork.js +783 -34
  19. package/src/commands/crosschain.js +182 -28
  20. package/src/commands/dao.js +182 -28
  21. package/src/commands/dbevo.js +227 -0
  22. package/src/commands/dev.js +207 -0
  23. package/src/commands/did-v2.js +217 -0
  24. package/src/commands/did.js +221 -0
  25. package/src/commands/dlp.js +385 -27
  26. package/src/commands/economy.js +205 -50
  27. package/src/commands/evolution.js +203 -50
  28. package/src/commands/evomap.js +387 -27
  29. package/src/commands/export.js +213 -0
  30. package/src/commands/federation.js +209 -0
  31. package/src/commands/fflag.js +74 -22
  32. package/src/commands/fusion.js +205 -0
  33. package/src/commands/git.js +177 -37
  34. package/src/commands/governance.js +209 -0
  35. package/src/commands/hardening.js +209 -0
  36. package/src/commands/hmemory.js +204 -50
  37. package/src/commands/hook.js +209 -0
  38. package/src/commands/import.js +209 -0
  39. package/src/commands/incentive.js +209 -0
  40. package/src/commands/inference.js +170 -34
  41. package/src/commands/infra.js +203 -0
  42. package/src/commands/instinct.js +209 -0
  43. package/src/commands/ipfs.js +207 -0
  44. package/src/commands/itbudget.js +149 -33
  45. package/src/commands/kg.js +206 -0
  46. package/src/commands/llm.js +217 -0
  47. package/src/commands/lowcode.js +195 -38
  48. package/src/commands/marketplace.js +206 -0
  49. package/src/commands/matrix.js +386 -27
  50. package/src/commands/mcp.js +217 -0
  51. package/src/commands/mcpscaf.js +145 -33
  52. package/src/commands/meminj.js +145 -33
  53. package/src/commands/memory.js +209 -0
  54. package/src/commands/multimodal.js +203 -0
  55. package/src/commands/nlprog.js +225 -0
  56. package/src/commands/nostr.js +387 -27
  57. package/src/commands/note.js +205 -0
  58. package/src/commands/ops.js +219 -0
  59. package/src/commands/orchestrate.js +217 -0
  60. package/src/commands/orchgov.js +144 -33
  61. package/src/commands/org.js +209 -0
  62. package/src/commands/p2p.js +209 -0
  63. package/src/commands/pdfp.js +109 -27
  64. package/src/commands/perception.js +209 -0
  65. package/src/commands/perf.js +169 -32
  66. package/src/commands/perm.js +144 -33
  67. package/src/commands/permmem.js +203 -0
  68. package/src/commands/pipeline.js +207 -52
  69. package/src/commands/planmode.js +141 -32
  70. package/src/commands/plugin-ecosystem.js +209 -0
  71. package/src/commands/pqc.js +213 -0
  72. package/src/commands/privacy.js +203 -0
  73. package/src/commands/promcomp.js +111 -27
  74. package/src/commands/quantization.js +207 -0
  75. package/src/commands/rcache.js +205 -0
  76. package/src/commands/recommend.js +410 -34
  77. package/src/commands/reputation.js +208 -0
  78. package/src/commands/runtime.js +205 -0
  79. package/src/commands/sandbox.js +206 -0
  80. package/src/commands/scim.js +209 -0
  81. package/src/commands/seshhook.js +145 -33
  82. package/src/commands/seshsearch.js +141 -33
  83. package/src/commands/seshtail.js +144 -33
  84. package/src/commands/seshu.js +152 -33
  85. package/src/commands/session.js +209 -0
  86. package/src/commands/sganal.js +123 -29
  87. package/src/commands/siem.js +201 -34
  88. package/src/commands/skill.js +207 -0
  89. package/src/commands/sla.js +212 -0
  90. package/src/commands/slotfill.js +146 -33
  91. package/src/commands/social.js +358 -32
  92. package/src/commands/sso.js +209 -0
  93. package/src/commands/stress.js +206 -0
  94. package/src/commands/svccont.js +145 -33
  95. package/src/commands/sync.js +209 -0
  96. package/src/commands/tech.js +209 -0
  97. package/src/commands/tenant.js +217 -0
  98. package/src/commands/terraform.js +206 -0
  99. package/src/commands/tms.js +171 -33
  100. package/src/commands/tokens.js +209 -0
  101. package/src/commands/topiccls.js +146 -33
  102. package/src/commands/trust.js +217 -0
  103. package/src/commands/uprof.js +141 -32
  104. package/src/commands/vcheck.js +122 -28
  105. package/src/commands/wallet.js +209 -0
  106. package/src/commands/webfetch.js +141 -32
  107. package/src/commands/workflow.js +203 -0
  108. package/src/commands/zkp.js +184 -28
  109. package/src/index.js +180 -0
  110. package/src/lib/a2a-protocol.js +319 -51
  111. package/src/lib/activitypub-bridge.js +622 -50
  112. package/src/lib/agent-economy.js +304 -51
  113. package/src/lib/agent-network.js +341 -0
  114. package/src/lib/aiops.js +346 -0
  115. package/src/lib/app-builder.js +279 -46
  116. package/src/lib/audit-logger.js +321 -0
  117. package/src/lib/automation-engine.js +335 -0
  118. package/src/lib/autonomous-agent.js +284 -48
  119. package/src/lib/autonomous-developer.js +332 -0
  120. package/src/lib/bi-engine.js +616 -49
  121. package/src/lib/bm25-search.js +301 -49
  122. package/src/lib/browser-automation.js +630 -49
  123. package/src/lib/chat-core.js +336 -0
  124. package/src/lib/claude-code-bridge.js +341 -0
  125. package/src/lib/code-agent.js +339 -0
  126. package/src/lib/collaboration-governance.js +334 -0
  127. package/src/lib/community-governance.js +346 -0
  128. package/src/lib/compliance-framework-reporter.js +305 -51
  129. package/src/lib/compliance-manager.js +330 -0
  130. package/src/lib/compression-telemetry.js +301 -49
  131. package/src/lib/content-recommendation.js +351 -0
  132. package/src/lib/content-recommender.js +317 -52
  133. package/src/lib/cowork-cron.js +298 -49
  134. package/src/lib/cowork-learning.js +333 -0
  135. package/src/lib/cowork-share.js +338 -0
  136. package/src/lib/cowork-task-runner.js +308 -51
  137. package/src/lib/cowork-workflow.js +327 -0
  138. package/src/lib/cross-chain.js +311 -51
  139. package/src/lib/dao-governance.js +280 -46
  140. package/src/lib/dbevo.js +351 -0
  141. package/src/lib/decentral-infra.js +330 -0
  142. package/src/lib/did-manager.js +341 -0
  143. package/src/lib/did-v2-manager.js +341 -0
  144. package/src/lib/dlp-engine.js +626 -49
  145. package/src/lib/evolution-system.js +278 -47
  146. package/src/lib/evomap-governance.js +334 -0
  147. package/src/lib/evomap-manager.js +280 -46
  148. package/src/lib/execution-backend.js +294 -48
  149. package/src/lib/feature-flags.js +294 -49
  150. package/src/lib/federation-hardening.js +340 -0
  151. package/src/lib/git-integration.js +285 -47
  152. package/src/lib/hardening-manager.js +341 -0
  153. package/src/lib/hierarchical-memory.js +284 -48
  154. package/src/lib/hook-manager.js +341 -0
  155. package/src/lib/inference-network.js +308 -51
  156. package/src/lib/instinct-manager.js +346 -0
  157. package/src/lib/ipfs-storage.js +334 -0
  158. package/src/lib/iteration-budget.js +302 -50
  159. package/src/lib/knowledge-exporter.js +341 -0
  160. package/src/lib/knowledge-graph.js +333 -0
  161. package/src/lib/knowledge-importer.js +341 -0
  162. package/src/lib/llm-providers.js +346 -0
  163. package/src/lib/matrix-bridge.js +620 -47
  164. package/src/lib/mcp-registry.js +346 -0
  165. package/src/lib/mcp-scaffold.js +318 -54
  166. package/src/lib/memory-injection.js +288 -49
  167. package/src/lib/memory-manager.js +336 -0
  168. package/src/lib/multimodal.js +330 -0
  169. package/src/lib/nl-programming.js +341 -0
  170. package/src/lib/nostr-bridge.js +622 -49
  171. package/src/lib/note-versioning.js +339 -0
  172. package/src/lib/orchestrator.js +293 -48
  173. package/src/lib/org-manager.js +336 -0
  174. package/src/lib/p2p-manager.js +341 -0
  175. package/src/lib/pdf-parser.js +298 -49
  176. package/src/lib/perception.js +346 -0
  177. package/src/lib/perf-tuning.js +309 -50
  178. package/src/lib/permanent-memory.js +327 -0
  179. package/src/lib/permission-engine.js +287 -49
  180. package/src/lib/pipeline-orchestrator.js +289 -49
  181. package/src/lib/plan-mode.js +298 -51
  182. package/src/lib/plugin-ecosystem.js +346 -0
  183. package/src/lib/pqc-manager.js +346 -0
  184. package/src/lib/privacy-computing.js +335 -0
  185. package/src/lib/protocol-fusion.js +338 -0
  186. package/src/lib/quantization.js +337 -0
  187. package/src/lib/reputation-optimizer.js +340 -0
  188. package/src/lib/response-cache.js +333 -0
  189. package/src/lib/sandbox-v2.js +327 -0
  190. package/src/lib/scim-manager.js +346 -0
  191. package/src/lib/service-container.js +313 -52
  192. package/src/lib/session-consolidator.js +296 -49
  193. package/src/lib/session-hooks.js +312 -53
  194. package/src/lib/session-manager.js +334 -0
  195. package/src/lib/session-search.js +304 -51
  196. package/src/lib/session-tail.js +288 -49
  197. package/src/lib/session-usage.js +298 -52
  198. package/src/lib/siem-exporter.js +298 -51
  199. package/src/lib/skill-loader.js +334 -0
  200. package/src/lib/skill-marketplace.js +345 -0
  201. package/src/lib/sla-manager.js +341 -0
  202. package/src/lib/slot-filler.js +303 -51
  203. package/src/lib/social-graph-analytics.js +295 -49
  204. package/src/lib/social-graph.js +272 -49
  205. package/src/lib/social-manager.js +330 -0
  206. package/src/lib/sso-manager.js +340 -0
  207. package/src/lib/stress-tester.js +342 -0
  208. package/src/lib/sub-agent-registry.js +302 -53
  209. package/src/lib/sync-manager.js +336 -0
  210. package/src/lib/task-model-selector.js +302 -50
  211. package/src/lib/tech-learning-engine.js +341 -0
  212. package/src/lib/tenant-saas.js +341 -0
  213. package/src/lib/terraform-manager.js +333 -0
  214. package/src/lib/threat-intel.js +330 -0
  215. package/src/lib/todo-manager.js +281 -47
  216. package/src/lib/token-incentive.js +341 -0
  217. package/src/lib/token-tracker.js +336 -0
  218. package/src/lib/topic-classifier.js +297 -49
  219. package/src/lib/trust-security.js +343 -0
  220. package/src/lib/ueba.js +340 -0
  221. package/src/lib/universal-runtime.js +330 -0
  222. package/src/lib/user-profile.js +294 -50
  223. package/src/lib/version-checker.js +304 -50
  224. package/src/lib/wallet-manager.js +336 -0
  225. package/src/lib/web-fetch.js +292 -51
  226. package/src/lib/workflow-engine.js +330 -0
  227. package/src/lib/zkp-engine.js +286 -49
@@ -352,3 +352,339 @@ export async function chatWithStreaming(messages, options, onEvent) {
352
352
  }
353
353
  return fullContent;
354
354
  }
355
+
356
+ // =====================================================================
357
+ // chat-core V2 governance overlay (iter17)
358
+ // =====================================================================
359
+ export const CHATGOV_PROFILE_MATURITY_V2 = Object.freeze({
360
+ PENDING: "pending",
361
+ ACTIVE: "active",
362
+ STALE: "stale",
363
+ ARCHIVED: "archived",
364
+ });
365
+ export const CHATGOV_MESSAGE_LIFECYCLE_V2 = Object.freeze({
366
+ QUEUED: "queued",
367
+ SENDING: "sending",
368
+ SENT: "sent",
369
+ FAILED: "failed",
370
+ CANCELLED: "cancelled",
371
+ });
372
+ const _chatgovPTrans = new Map([
373
+ [
374
+ CHATGOV_PROFILE_MATURITY_V2.PENDING,
375
+ new Set([
376
+ CHATGOV_PROFILE_MATURITY_V2.ACTIVE,
377
+ CHATGOV_PROFILE_MATURITY_V2.ARCHIVED,
378
+ ]),
379
+ ],
380
+ [
381
+ CHATGOV_PROFILE_MATURITY_V2.ACTIVE,
382
+ new Set([
383
+ CHATGOV_PROFILE_MATURITY_V2.STALE,
384
+ CHATGOV_PROFILE_MATURITY_V2.ARCHIVED,
385
+ ]),
386
+ ],
387
+ [
388
+ CHATGOV_PROFILE_MATURITY_V2.STALE,
389
+ new Set([
390
+ CHATGOV_PROFILE_MATURITY_V2.ACTIVE,
391
+ CHATGOV_PROFILE_MATURITY_V2.ARCHIVED,
392
+ ]),
393
+ ],
394
+ [CHATGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
395
+ ]);
396
+ const _chatgovPTerminal = new Set([CHATGOV_PROFILE_MATURITY_V2.ARCHIVED]);
397
+ const _chatgovJTrans = new Map([
398
+ [
399
+ CHATGOV_MESSAGE_LIFECYCLE_V2.QUEUED,
400
+ new Set([
401
+ CHATGOV_MESSAGE_LIFECYCLE_V2.SENDING,
402
+ CHATGOV_MESSAGE_LIFECYCLE_V2.CANCELLED,
403
+ ]),
404
+ ],
405
+ [
406
+ CHATGOV_MESSAGE_LIFECYCLE_V2.SENDING,
407
+ new Set([
408
+ CHATGOV_MESSAGE_LIFECYCLE_V2.SENT,
409
+ CHATGOV_MESSAGE_LIFECYCLE_V2.FAILED,
410
+ CHATGOV_MESSAGE_LIFECYCLE_V2.CANCELLED,
411
+ ]),
412
+ ],
413
+ [CHATGOV_MESSAGE_LIFECYCLE_V2.SENT, new Set()],
414
+ [CHATGOV_MESSAGE_LIFECYCLE_V2.FAILED, new Set()],
415
+ [CHATGOV_MESSAGE_LIFECYCLE_V2.CANCELLED, new Set()],
416
+ ]);
417
+ const _chatgovPsV2 = new Map();
418
+ const _chatgovJsV2 = new Map();
419
+ let _chatgovMaxActive = 8,
420
+ _chatgovMaxPending = 30,
421
+ _chatgovIdleMs = 30 * 24 * 60 * 60 * 1000,
422
+ _chatgovStuckMs = 60 * 1000;
423
+ function _chatgovPos(n, label) {
424
+ const v = Math.floor(Number(n));
425
+ if (!Number.isFinite(v) || v <= 0)
426
+ throw new Error(`${label} must be positive integer`);
427
+ return v;
428
+ }
429
+ function _chatgovCheckP(from, to) {
430
+ const a = _chatgovPTrans.get(from);
431
+ if (!a || !a.has(to))
432
+ throw new Error(`invalid chatgov profile transition ${from} → ${to}`);
433
+ }
434
+ function _chatgovCheckJ(from, to) {
435
+ const a = _chatgovJTrans.get(from);
436
+ if (!a || !a.has(to))
437
+ throw new Error(`invalid chatgov message transition ${from} → ${to}`);
438
+ }
439
+ function _chatgovCountActive(owner) {
440
+ let c = 0;
441
+ for (const p of _chatgovPsV2.values())
442
+ if (p.owner === owner && p.status === CHATGOV_PROFILE_MATURITY_V2.ACTIVE)
443
+ c++;
444
+ return c;
445
+ }
446
+ function _chatgovCountPending(profileId) {
447
+ let c = 0;
448
+ for (const j of _chatgovJsV2.values())
449
+ if (
450
+ j.profileId === profileId &&
451
+ (j.status === CHATGOV_MESSAGE_LIFECYCLE_V2.QUEUED ||
452
+ j.status === CHATGOV_MESSAGE_LIFECYCLE_V2.SENDING)
453
+ )
454
+ c++;
455
+ return c;
456
+ }
457
+ export function setMaxActiveChatgovProfilesPerOwnerV2(n) {
458
+ _chatgovMaxActive = _chatgovPos(n, "maxActiveChatgovProfilesPerOwner");
459
+ }
460
+ export function getMaxActiveChatgovProfilesPerOwnerV2() {
461
+ return _chatgovMaxActive;
462
+ }
463
+ export function setMaxPendingChatgovMessagesPerProfileV2(n) {
464
+ _chatgovMaxPending = _chatgovPos(n, "maxPendingChatgovMessagesPerProfile");
465
+ }
466
+ export function getMaxPendingChatgovMessagesPerProfileV2() {
467
+ return _chatgovMaxPending;
468
+ }
469
+ export function setChatgovProfileIdleMsV2(n) {
470
+ _chatgovIdleMs = _chatgovPos(n, "chatgovProfileIdleMs");
471
+ }
472
+ export function getChatgovProfileIdleMsV2() {
473
+ return _chatgovIdleMs;
474
+ }
475
+ export function setChatgovMessageStuckMsV2(n) {
476
+ _chatgovStuckMs = _chatgovPos(n, "chatgovMessageStuckMs");
477
+ }
478
+ export function getChatgovMessageStuckMsV2() {
479
+ return _chatgovStuckMs;
480
+ }
481
+ export function _resetStateChatCoreV2() {
482
+ _chatgovPsV2.clear();
483
+ _chatgovJsV2.clear();
484
+ _chatgovMaxActive = 8;
485
+ _chatgovMaxPending = 30;
486
+ _chatgovIdleMs = 30 * 24 * 60 * 60 * 1000;
487
+ _chatgovStuckMs = 60 * 1000;
488
+ }
489
+ export function registerChatgovProfileV2({ id, owner, mode, metadata } = {}) {
490
+ if (!id || !owner) throw new Error("id and owner required");
491
+ if (_chatgovPsV2.has(id))
492
+ throw new Error(`chatgov profile ${id} already exists`);
493
+ const now = Date.now();
494
+ const p = {
495
+ id,
496
+ owner,
497
+ mode: mode || "interactive",
498
+ status: CHATGOV_PROFILE_MATURITY_V2.PENDING,
499
+ createdAt: now,
500
+ updatedAt: now,
501
+ lastTouchedAt: now,
502
+ activatedAt: null,
503
+ archivedAt: null,
504
+ metadata: { ...(metadata || {}) },
505
+ };
506
+ _chatgovPsV2.set(id, p);
507
+ return { ...p, metadata: { ...p.metadata } };
508
+ }
509
+ export function activateChatgovProfileV2(id) {
510
+ const p = _chatgovPsV2.get(id);
511
+ if (!p) throw new Error(`chatgov profile ${id} not found`);
512
+ const isInitial = p.status === CHATGOV_PROFILE_MATURITY_V2.PENDING;
513
+ _chatgovCheckP(p.status, CHATGOV_PROFILE_MATURITY_V2.ACTIVE);
514
+ if (isInitial && _chatgovCountActive(p.owner) >= _chatgovMaxActive)
515
+ throw new Error(`max active chatgov profiles for owner ${p.owner} reached`);
516
+ const now = Date.now();
517
+ p.status = CHATGOV_PROFILE_MATURITY_V2.ACTIVE;
518
+ p.updatedAt = now;
519
+ p.lastTouchedAt = now;
520
+ if (!p.activatedAt) p.activatedAt = now;
521
+ return { ...p, metadata: { ...p.metadata } };
522
+ }
523
+ export function staleChatgovProfileV2(id) {
524
+ const p = _chatgovPsV2.get(id);
525
+ if (!p) throw new Error(`chatgov profile ${id} not found`);
526
+ _chatgovCheckP(p.status, CHATGOV_PROFILE_MATURITY_V2.STALE);
527
+ p.status = CHATGOV_PROFILE_MATURITY_V2.STALE;
528
+ p.updatedAt = Date.now();
529
+ return { ...p, metadata: { ...p.metadata } };
530
+ }
531
+ export function archiveChatgovProfileV2(id) {
532
+ const p = _chatgovPsV2.get(id);
533
+ if (!p) throw new Error(`chatgov profile ${id} not found`);
534
+ _chatgovCheckP(p.status, CHATGOV_PROFILE_MATURITY_V2.ARCHIVED);
535
+ const now = Date.now();
536
+ p.status = CHATGOV_PROFILE_MATURITY_V2.ARCHIVED;
537
+ p.updatedAt = now;
538
+ if (!p.archivedAt) p.archivedAt = now;
539
+ return { ...p, metadata: { ...p.metadata } };
540
+ }
541
+ export function touchChatgovProfileV2(id) {
542
+ const p = _chatgovPsV2.get(id);
543
+ if (!p) throw new Error(`chatgov profile ${id} not found`);
544
+ if (_chatgovPTerminal.has(p.status))
545
+ throw new Error(`cannot touch terminal chatgov profile ${id}`);
546
+ const now = Date.now();
547
+ p.lastTouchedAt = now;
548
+ p.updatedAt = now;
549
+ return { ...p, metadata: { ...p.metadata } };
550
+ }
551
+ export function getChatgovProfileV2(id) {
552
+ const p = _chatgovPsV2.get(id);
553
+ if (!p) return null;
554
+ return { ...p, metadata: { ...p.metadata } };
555
+ }
556
+ export function listChatgovProfilesV2() {
557
+ return [..._chatgovPsV2.values()].map((p) => ({
558
+ ...p,
559
+ metadata: { ...p.metadata },
560
+ }));
561
+ }
562
+ export function createChatgovMessageV2({ id, profileId, role, metadata } = {}) {
563
+ if (!id || !profileId) throw new Error("id and profileId required");
564
+ if (_chatgovJsV2.has(id))
565
+ throw new Error(`chatgov message ${id} already exists`);
566
+ if (!_chatgovPsV2.has(profileId))
567
+ throw new Error(`chatgov profile ${profileId} not found`);
568
+ if (_chatgovCountPending(profileId) >= _chatgovMaxPending)
569
+ throw new Error(
570
+ `max pending chatgov messages for profile ${profileId} reached`,
571
+ );
572
+ const now = Date.now();
573
+ const j = {
574
+ id,
575
+ profileId,
576
+ role: role || "",
577
+ status: CHATGOV_MESSAGE_LIFECYCLE_V2.QUEUED,
578
+ createdAt: now,
579
+ updatedAt: now,
580
+ startedAt: null,
581
+ settledAt: null,
582
+ metadata: { ...(metadata || {}) },
583
+ };
584
+ _chatgovJsV2.set(id, j);
585
+ return { ...j, metadata: { ...j.metadata } };
586
+ }
587
+ export function sendingChatgovMessageV2(id) {
588
+ const j = _chatgovJsV2.get(id);
589
+ if (!j) throw new Error(`chatgov message ${id} not found`);
590
+ _chatgovCheckJ(j.status, CHATGOV_MESSAGE_LIFECYCLE_V2.SENDING);
591
+ const now = Date.now();
592
+ j.status = CHATGOV_MESSAGE_LIFECYCLE_V2.SENDING;
593
+ j.updatedAt = now;
594
+ if (!j.startedAt) j.startedAt = now;
595
+ return { ...j, metadata: { ...j.metadata } };
596
+ }
597
+ export function completeMessageChatgovV2(id) {
598
+ const j = _chatgovJsV2.get(id);
599
+ if (!j) throw new Error(`chatgov message ${id} not found`);
600
+ _chatgovCheckJ(j.status, CHATGOV_MESSAGE_LIFECYCLE_V2.SENT);
601
+ const now = Date.now();
602
+ j.status = CHATGOV_MESSAGE_LIFECYCLE_V2.SENT;
603
+ j.updatedAt = now;
604
+ if (!j.settledAt) j.settledAt = now;
605
+ return { ...j, metadata: { ...j.metadata } };
606
+ }
607
+ export function failChatgovMessageV2(id, reason) {
608
+ const j = _chatgovJsV2.get(id);
609
+ if (!j) throw new Error(`chatgov message ${id} not found`);
610
+ _chatgovCheckJ(j.status, CHATGOV_MESSAGE_LIFECYCLE_V2.FAILED);
611
+ const now = Date.now();
612
+ j.status = CHATGOV_MESSAGE_LIFECYCLE_V2.FAILED;
613
+ j.updatedAt = now;
614
+ if (!j.settledAt) j.settledAt = now;
615
+ if (reason) j.metadata.failReason = String(reason);
616
+ return { ...j, metadata: { ...j.metadata } };
617
+ }
618
+ export function cancelChatgovMessageV2(id, reason) {
619
+ const j = _chatgovJsV2.get(id);
620
+ if (!j) throw new Error(`chatgov message ${id} not found`);
621
+ _chatgovCheckJ(j.status, CHATGOV_MESSAGE_LIFECYCLE_V2.CANCELLED);
622
+ const now = Date.now();
623
+ j.status = CHATGOV_MESSAGE_LIFECYCLE_V2.CANCELLED;
624
+ j.updatedAt = now;
625
+ if (!j.settledAt) j.settledAt = now;
626
+ if (reason) j.metadata.cancelReason = String(reason);
627
+ return { ...j, metadata: { ...j.metadata } };
628
+ }
629
+ export function getChatgovMessageV2(id) {
630
+ const j = _chatgovJsV2.get(id);
631
+ if (!j) return null;
632
+ return { ...j, metadata: { ...j.metadata } };
633
+ }
634
+ export function listChatgovMessagesV2() {
635
+ return [..._chatgovJsV2.values()].map((j) => ({
636
+ ...j,
637
+ metadata: { ...j.metadata },
638
+ }));
639
+ }
640
+ export function autoStaleIdleChatgovProfilesV2({ now } = {}) {
641
+ const t = now ?? Date.now();
642
+ const flipped = [];
643
+ for (const p of _chatgovPsV2.values())
644
+ if (
645
+ p.status === CHATGOV_PROFILE_MATURITY_V2.ACTIVE &&
646
+ t - p.lastTouchedAt >= _chatgovIdleMs
647
+ ) {
648
+ p.status = CHATGOV_PROFILE_MATURITY_V2.STALE;
649
+ p.updatedAt = t;
650
+ flipped.push(p.id);
651
+ }
652
+ return { flipped, count: flipped.length };
653
+ }
654
+ export function autoFailStuckChatgovMessagesV2({ now } = {}) {
655
+ const t = now ?? Date.now();
656
+ const flipped = [];
657
+ for (const j of _chatgovJsV2.values())
658
+ if (
659
+ j.status === CHATGOV_MESSAGE_LIFECYCLE_V2.SENDING &&
660
+ j.startedAt != null &&
661
+ t - j.startedAt >= _chatgovStuckMs
662
+ ) {
663
+ j.status = CHATGOV_MESSAGE_LIFECYCLE_V2.FAILED;
664
+ j.updatedAt = t;
665
+ if (!j.settledAt) j.settledAt = t;
666
+ j.metadata.failReason = "auto-fail-stuck";
667
+ flipped.push(j.id);
668
+ }
669
+ return { flipped, count: flipped.length };
670
+ }
671
+ export function getChatCoreGovStatsV2() {
672
+ const profilesByStatus = {};
673
+ for (const v of Object.values(CHATGOV_PROFILE_MATURITY_V2))
674
+ profilesByStatus[v] = 0;
675
+ for (const p of _chatgovPsV2.values()) profilesByStatus[p.status]++;
676
+ const messagesByStatus = {};
677
+ for (const v of Object.values(CHATGOV_MESSAGE_LIFECYCLE_V2))
678
+ messagesByStatus[v] = 0;
679
+ for (const j of _chatgovJsV2.values()) messagesByStatus[j.status]++;
680
+ return {
681
+ totalChatgovProfilesV2: _chatgovPsV2.size,
682
+ totalChatgovMessagesV2: _chatgovJsV2.size,
683
+ maxActiveChatgovProfilesPerOwner: _chatgovMaxActive,
684
+ maxPendingChatgovMessagesPerProfile: _chatgovMaxPending,
685
+ chatgovProfileIdleMs: _chatgovIdleMs,
686
+ chatgovMessageStuckMs: _chatgovStuckMs,
687
+ profilesByStatus,
688
+ messagesByStatus,
689
+ };
690
+ }
@@ -351,3 +351,344 @@ function _parseStreamJson(raw) {
351
351
 
352
352
  return lastText;
353
353
  }
354
+
355
+ // =====================================================================
356
+ // claude-code-bridge V2 governance overlay (iter17)
357
+ // =====================================================================
358
+ export const CCBGOV_PROFILE_MATURITY_V2 = Object.freeze({
359
+ PENDING: "pending",
360
+ ACTIVE: "active",
361
+ DEGRADED: "degraded",
362
+ ARCHIVED: "archived",
363
+ });
364
+ export const CCBGOV_INVOCATION_LIFECYCLE_V2 = Object.freeze({
365
+ QUEUED: "queued",
366
+ RUNNING: "running",
367
+ COMPLETED: "completed",
368
+ FAILED: "failed",
369
+ CANCELLED: "cancelled",
370
+ });
371
+ const _ccbgovPTrans = new Map([
372
+ [
373
+ CCBGOV_PROFILE_MATURITY_V2.PENDING,
374
+ new Set([
375
+ CCBGOV_PROFILE_MATURITY_V2.ACTIVE,
376
+ CCBGOV_PROFILE_MATURITY_V2.ARCHIVED,
377
+ ]),
378
+ ],
379
+ [
380
+ CCBGOV_PROFILE_MATURITY_V2.ACTIVE,
381
+ new Set([
382
+ CCBGOV_PROFILE_MATURITY_V2.DEGRADED,
383
+ CCBGOV_PROFILE_MATURITY_V2.ARCHIVED,
384
+ ]),
385
+ ],
386
+ [
387
+ CCBGOV_PROFILE_MATURITY_V2.DEGRADED,
388
+ new Set([
389
+ CCBGOV_PROFILE_MATURITY_V2.ACTIVE,
390
+ CCBGOV_PROFILE_MATURITY_V2.ARCHIVED,
391
+ ]),
392
+ ],
393
+ [CCBGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
394
+ ]);
395
+ const _ccbgovPTerminal = new Set([CCBGOV_PROFILE_MATURITY_V2.ARCHIVED]);
396
+ const _ccbgovJTrans = new Map([
397
+ [
398
+ CCBGOV_INVOCATION_LIFECYCLE_V2.QUEUED,
399
+ new Set([
400
+ CCBGOV_INVOCATION_LIFECYCLE_V2.RUNNING,
401
+ CCBGOV_INVOCATION_LIFECYCLE_V2.CANCELLED,
402
+ ]),
403
+ ],
404
+ [
405
+ CCBGOV_INVOCATION_LIFECYCLE_V2.RUNNING,
406
+ new Set([
407
+ CCBGOV_INVOCATION_LIFECYCLE_V2.COMPLETED,
408
+ CCBGOV_INVOCATION_LIFECYCLE_V2.FAILED,
409
+ CCBGOV_INVOCATION_LIFECYCLE_V2.CANCELLED,
410
+ ]),
411
+ ],
412
+ [CCBGOV_INVOCATION_LIFECYCLE_V2.COMPLETED, new Set()],
413
+ [CCBGOV_INVOCATION_LIFECYCLE_V2.FAILED, new Set()],
414
+ [CCBGOV_INVOCATION_LIFECYCLE_V2.CANCELLED, new Set()],
415
+ ]);
416
+ const _ccbgovPsV2 = new Map();
417
+ const _ccbgovJsV2 = new Map();
418
+ let _ccbgovMaxActive = 6,
419
+ _ccbgovMaxPending = 15,
420
+ _ccbgovIdleMs = 30 * 24 * 60 * 60 * 1000,
421
+ _ccbgovStuckMs = 60 * 1000;
422
+ function _ccbgovPos(n, label) {
423
+ const v = Math.floor(Number(n));
424
+ if (!Number.isFinite(v) || v <= 0)
425
+ throw new Error(`${label} must be positive integer`);
426
+ return v;
427
+ }
428
+ function _ccbgovCheckP(from, to) {
429
+ const a = _ccbgovPTrans.get(from);
430
+ if (!a || !a.has(to))
431
+ throw new Error(`invalid ccbgov profile transition ${from} → ${to}`);
432
+ }
433
+ function _ccbgovCheckJ(from, to) {
434
+ const a = _ccbgovJTrans.get(from);
435
+ if (!a || !a.has(to))
436
+ throw new Error(`invalid ccbgov invocation transition ${from} → ${to}`);
437
+ }
438
+ function _ccbgovCountActive(owner) {
439
+ let c = 0;
440
+ for (const p of _ccbgovPsV2.values())
441
+ if (p.owner === owner && p.status === CCBGOV_PROFILE_MATURITY_V2.ACTIVE)
442
+ c++;
443
+ return c;
444
+ }
445
+ function _ccbgovCountPending(profileId) {
446
+ let c = 0;
447
+ for (const j of _ccbgovJsV2.values())
448
+ if (
449
+ j.profileId === profileId &&
450
+ (j.status === CCBGOV_INVOCATION_LIFECYCLE_V2.QUEUED ||
451
+ j.status === CCBGOV_INVOCATION_LIFECYCLE_V2.RUNNING)
452
+ )
453
+ c++;
454
+ return c;
455
+ }
456
+ export function setMaxActiveCcbgovProfilesPerOwnerV2(n) {
457
+ _ccbgovMaxActive = _ccbgovPos(n, "maxActiveCcbgovProfilesPerOwner");
458
+ }
459
+ export function getMaxActiveCcbgovProfilesPerOwnerV2() {
460
+ return _ccbgovMaxActive;
461
+ }
462
+ export function setMaxPendingCcbgovInvocationsPerProfileV2(n) {
463
+ _ccbgovMaxPending = _ccbgovPos(n, "maxPendingCcbgovInvocationsPerProfile");
464
+ }
465
+ export function getMaxPendingCcbgovInvocationsPerProfileV2() {
466
+ return _ccbgovMaxPending;
467
+ }
468
+ export function setCcbgovProfileIdleMsV2(n) {
469
+ _ccbgovIdleMs = _ccbgovPos(n, "ccbgovProfileIdleMs");
470
+ }
471
+ export function getCcbgovProfileIdleMsV2() {
472
+ return _ccbgovIdleMs;
473
+ }
474
+ export function setCcbgovInvocationStuckMsV2(n) {
475
+ _ccbgovStuckMs = _ccbgovPos(n, "ccbgovInvocationStuckMs");
476
+ }
477
+ export function getCcbgovInvocationStuckMsV2() {
478
+ return _ccbgovStuckMs;
479
+ }
480
+ export function _resetStateClaudeCodeBridgeV2() {
481
+ _ccbgovPsV2.clear();
482
+ _ccbgovJsV2.clear();
483
+ _ccbgovMaxActive = 6;
484
+ _ccbgovMaxPending = 15;
485
+ _ccbgovIdleMs = 30 * 24 * 60 * 60 * 1000;
486
+ _ccbgovStuckMs = 60 * 1000;
487
+ }
488
+ export function registerCcbgovProfileV2({ id, owner, channel, metadata } = {}) {
489
+ if (!id || !owner) throw new Error("id and owner required");
490
+ if (_ccbgovPsV2.has(id))
491
+ throw new Error(`ccbgov profile ${id} already exists`);
492
+ const now = Date.now();
493
+ const p = {
494
+ id,
495
+ owner,
496
+ channel: channel || "stdio",
497
+ status: CCBGOV_PROFILE_MATURITY_V2.PENDING,
498
+ createdAt: now,
499
+ updatedAt: now,
500
+ lastTouchedAt: now,
501
+ activatedAt: null,
502
+ archivedAt: null,
503
+ metadata: { ...(metadata || {}) },
504
+ };
505
+ _ccbgovPsV2.set(id, p);
506
+ return { ...p, metadata: { ...p.metadata } };
507
+ }
508
+ export function activateCcbgovProfileV2(id) {
509
+ const p = _ccbgovPsV2.get(id);
510
+ if (!p) throw new Error(`ccbgov profile ${id} not found`);
511
+ const isInitial = p.status === CCBGOV_PROFILE_MATURITY_V2.PENDING;
512
+ _ccbgovCheckP(p.status, CCBGOV_PROFILE_MATURITY_V2.ACTIVE);
513
+ if (isInitial && _ccbgovCountActive(p.owner) >= _ccbgovMaxActive)
514
+ throw new Error(`max active ccbgov profiles for owner ${p.owner} reached`);
515
+ const now = Date.now();
516
+ p.status = CCBGOV_PROFILE_MATURITY_V2.ACTIVE;
517
+ p.updatedAt = now;
518
+ p.lastTouchedAt = now;
519
+ if (!p.activatedAt) p.activatedAt = now;
520
+ return { ...p, metadata: { ...p.metadata } };
521
+ }
522
+ export function degradeCcbgovProfileV2(id) {
523
+ const p = _ccbgovPsV2.get(id);
524
+ if (!p) throw new Error(`ccbgov profile ${id} not found`);
525
+ _ccbgovCheckP(p.status, CCBGOV_PROFILE_MATURITY_V2.DEGRADED);
526
+ p.status = CCBGOV_PROFILE_MATURITY_V2.DEGRADED;
527
+ p.updatedAt = Date.now();
528
+ return { ...p, metadata: { ...p.metadata } };
529
+ }
530
+ export function archiveCcbgovProfileV2(id) {
531
+ const p = _ccbgovPsV2.get(id);
532
+ if (!p) throw new Error(`ccbgov profile ${id} not found`);
533
+ _ccbgovCheckP(p.status, CCBGOV_PROFILE_MATURITY_V2.ARCHIVED);
534
+ const now = Date.now();
535
+ p.status = CCBGOV_PROFILE_MATURITY_V2.ARCHIVED;
536
+ p.updatedAt = now;
537
+ if (!p.archivedAt) p.archivedAt = now;
538
+ return { ...p, metadata: { ...p.metadata } };
539
+ }
540
+ export function touchCcbgovProfileV2(id) {
541
+ const p = _ccbgovPsV2.get(id);
542
+ if (!p) throw new Error(`ccbgov profile ${id} not found`);
543
+ if (_ccbgovPTerminal.has(p.status))
544
+ throw new Error(`cannot touch terminal ccbgov profile ${id}`);
545
+ const now = Date.now();
546
+ p.lastTouchedAt = now;
547
+ p.updatedAt = now;
548
+ return { ...p, metadata: { ...p.metadata } };
549
+ }
550
+ export function getCcbgovProfileV2(id) {
551
+ const p = _ccbgovPsV2.get(id);
552
+ if (!p) return null;
553
+ return { ...p, metadata: { ...p.metadata } };
554
+ }
555
+ export function listCcbgovProfilesV2() {
556
+ return [..._ccbgovPsV2.values()].map((p) => ({
557
+ ...p,
558
+ metadata: { ...p.metadata },
559
+ }));
560
+ }
561
+ export function createCcbgovInvocationV2({
562
+ id,
563
+ profileId,
564
+ command,
565
+ metadata,
566
+ } = {}) {
567
+ if (!id || !profileId) throw new Error("id and profileId required");
568
+ if (_ccbgovJsV2.has(id))
569
+ throw new Error(`ccbgov invocation ${id} already exists`);
570
+ if (!_ccbgovPsV2.has(profileId))
571
+ throw new Error(`ccbgov profile ${profileId} not found`);
572
+ if (_ccbgovCountPending(profileId) >= _ccbgovMaxPending)
573
+ throw new Error(
574
+ `max pending ccbgov invocations for profile ${profileId} reached`,
575
+ );
576
+ const now = Date.now();
577
+ const j = {
578
+ id,
579
+ profileId,
580
+ command: command || "",
581
+ status: CCBGOV_INVOCATION_LIFECYCLE_V2.QUEUED,
582
+ createdAt: now,
583
+ updatedAt: now,
584
+ startedAt: null,
585
+ settledAt: null,
586
+ metadata: { ...(metadata || {}) },
587
+ };
588
+ _ccbgovJsV2.set(id, j);
589
+ return { ...j, metadata: { ...j.metadata } };
590
+ }
591
+ export function runningCcbgovInvocationV2(id) {
592
+ const j = _ccbgovJsV2.get(id);
593
+ if (!j) throw new Error(`ccbgov invocation ${id} not found`);
594
+ _ccbgovCheckJ(j.status, CCBGOV_INVOCATION_LIFECYCLE_V2.RUNNING);
595
+ const now = Date.now();
596
+ j.status = CCBGOV_INVOCATION_LIFECYCLE_V2.RUNNING;
597
+ j.updatedAt = now;
598
+ if (!j.startedAt) j.startedAt = now;
599
+ return { ...j, metadata: { ...j.metadata } };
600
+ }
601
+ export function completeInvocationCcbgovV2(id) {
602
+ const j = _ccbgovJsV2.get(id);
603
+ if (!j) throw new Error(`ccbgov invocation ${id} not found`);
604
+ _ccbgovCheckJ(j.status, CCBGOV_INVOCATION_LIFECYCLE_V2.COMPLETED);
605
+ const now = Date.now();
606
+ j.status = CCBGOV_INVOCATION_LIFECYCLE_V2.COMPLETED;
607
+ j.updatedAt = now;
608
+ if (!j.settledAt) j.settledAt = now;
609
+ return { ...j, metadata: { ...j.metadata } };
610
+ }
611
+ export function failCcbgovInvocationV2(id, reason) {
612
+ const j = _ccbgovJsV2.get(id);
613
+ if (!j) throw new Error(`ccbgov invocation ${id} not found`);
614
+ _ccbgovCheckJ(j.status, CCBGOV_INVOCATION_LIFECYCLE_V2.FAILED);
615
+ const now = Date.now();
616
+ j.status = CCBGOV_INVOCATION_LIFECYCLE_V2.FAILED;
617
+ j.updatedAt = now;
618
+ if (!j.settledAt) j.settledAt = now;
619
+ if (reason) j.metadata.failReason = String(reason);
620
+ return { ...j, metadata: { ...j.metadata } };
621
+ }
622
+ export function cancelCcbgovInvocationV2(id, reason) {
623
+ const j = _ccbgovJsV2.get(id);
624
+ if (!j) throw new Error(`ccbgov invocation ${id} not found`);
625
+ _ccbgovCheckJ(j.status, CCBGOV_INVOCATION_LIFECYCLE_V2.CANCELLED);
626
+ const now = Date.now();
627
+ j.status = CCBGOV_INVOCATION_LIFECYCLE_V2.CANCELLED;
628
+ j.updatedAt = now;
629
+ if (!j.settledAt) j.settledAt = now;
630
+ if (reason) j.metadata.cancelReason = String(reason);
631
+ return { ...j, metadata: { ...j.metadata } };
632
+ }
633
+ export function getCcbgovInvocationV2(id) {
634
+ const j = _ccbgovJsV2.get(id);
635
+ if (!j) return null;
636
+ return { ...j, metadata: { ...j.metadata } };
637
+ }
638
+ export function listCcbgovInvocationsV2() {
639
+ return [..._ccbgovJsV2.values()].map((j) => ({
640
+ ...j,
641
+ metadata: { ...j.metadata },
642
+ }));
643
+ }
644
+ export function autoDegradeIdleCcbgovProfilesV2({ now } = {}) {
645
+ const t = now ?? Date.now();
646
+ const flipped = [];
647
+ for (const p of _ccbgovPsV2.values())
648
+ if (
649
+ p.status === CCBGOV_PROFILE_MATURITY_V2.ACTIVE &&
650
+ t - p.lastTouchedAt >= _ccbgovIdleMs
651
+ ) {
652
+ p.status = CCBGOV_PROFILE_MATURITY_V2.DEGRADED;
653
+ p.updatedAt = t;
654
+ flipped.push(p.id);
655
+ }
656
+ return { flipped, count: flipped.length };
657
+ }
658
+ export function autoFailStuckCcbgovInvocationsV2({ now } = {}) {
659
+ const t = now ?? Date.now();
660
+ const flipped = [];
661
+ for (const j of _ccbgovJsV2.values())
662
+ if (
663
+ j.status === CCBGOV_INVOCATION_LIFECYCLE_V2.RUNNING &&
664
+ j.startedAt != null &&
665
+ t - j.startedAt >= _ccbgovStuckMs
666
+ ) {
667
+ j.status = CCBGOV_INVOCATION_LIFECYCLE_V2.FAILED;
668
+ j.updatedAt = t;
669
+ if (!j.settledAt) j.settledAt = t;
670
+ j.metadata.failReason = "auto-fail-stuck";
671
+ flipped.push(j.id);
672
+ }
673
+ return { flipped, count: flipped.length };
674
+ }
675
+ export function getClaudeCodeBridgeGovStatsV2() {
676
+ const profilesByStatus = {};
677
+ for (const v of Object.values(CCBGOV_PROFILE_MATURITY_V2))
678
+ profilesByStatus[v] = 0;
679
+ for (const p of _ccbgovPsV2.values()) profilesByStatus[p.status]++;
680
+ const invocationsByStatus = {};
681
+ for (const v of Object.values(CCBGOV_INVOCATION_LIFECYCLE_V2))
682
+ invocationsByStatus[v] = 0;
683
+ for (const j of _ccbgovJsV2.values()) invocationsByStatus[j.status]++;
684
+ return {
685
+ totalCcbgovProfilesV2: _ccbgovPsV2.size,
686
+ totalCcbgovInvocationsV2: _ccbgovJsV2.size,
687
+ maxActiveCcbgovProfilesPerOwner: _ccbgovMaxActive,
688
+ maxPendingCcbgovInvocationsPerProfile: _ccbgovMaxPending,
689
+ ccbgovProfileIdleMs: _ccbgovIdleMs,
690
+ ccbgovInvocationStuckMs: _ccbgovStuckMs,
691
+ profilesByStatus,
692
+ invocationsByStatus,
693
+ };
694
+ }