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
@@ -242,3 +242,330 @@ export function revertToVersion(db, noteId, version) {
242
242
  title: targetVersion.title,
243
243
  };
244
244
  }
245
+
246
+ /* ═══════════════════════════════════════════════════════════════
247
+ * V2 Surface — Note governance layer.
248
+ * Tracks per-author note maturity + per-note revision lifecycle
249
+ * independent of legacy SQLite note_versions table.
250
+ * ═══════════════════════════════════════════════════════════════ */
251
+
252
+ export const NOTE_MATURITY_V2 = Object.freeze({
253
+ DRAFT: "draft",
254
+ ACTIVE: "active",
255
+ LOCKED: "locked",
256
+ ARCHIVED: "archived",
257
+ });
258
+
259
+ export const REVISION_LIFECYCLE_V2 = Object.freeze({
260
+ PROPOSED: "proposed",
261
+ REVIEWED: "reviewed",
262
+ APPLIED: "applied",
263
+ SUPERSEDED: "superseded",
264
+ DISCARDED: "discarded",
265
+ });
266
+
267
+ const NOTE_TRANSITIONS_V2 = new Map([
268
+ ["draft", new Set(["active", "archived"])],
269
+ ["active", new Set(["locked", "archived"])],
270
+ ["locked", new Set(["active", "archived"])],
271
+ ["archived", new Set()],
272
+ ]);
273
+ const NOTE_TERMINALS_V2 = new Set(["archived"]);
274
+
275
+ const REV_TRANSITIONS_V2 = new Map([
276
+ ["proposed", new Set(["reviewed", "discarded"])],
277
+ ["reviewed", new Set(["applied", "discarded", "superseded"])],
278
+ ["applied", new Set(["superseded"])],
279
+ ["superseded", new Set()],
280
+ ["discarded", new Set()],
281
+ ]);
282
+ const REV_TERMINALS_V2 = new Set(["superseded", "discarded"]);
283
+
284
+ export const NOTE_DEFAULT_MAX_ACTIVE_NOTES_PER_AUTHOR = 100;
285
+ export const NOTE_DEFAULT_MAX_OPEN_REVS_PER_NOTE = 10;
286
+ export const NOTE_DEFAULT_NOTE_IDLE_MS = 1000 * 60 * 60 * 24 * 30; // 30 days
287
+ export const NOTE_DEFAULT_REV_STUCK_MS = 1000 * 60 * 60 * 24 * 7; // 7 days
288
+
289
+ const _notesV2 = new Map();
290
+ const _revsV2 = new Map();
291
+ let _maxActiveNotesPerAuthorV2 = NOTE_DEFAULT_MAX_ACTIVE_NOTES_PER_AUTHOR;
292
+ let _maxOpenRevsPerNoteV2 = NOTE_DEFAULT_MAX_OPEN_REVS_PER_NOTE;
293
+ let _noteIdleMsV2 = NOTE_DEFAULT_NOTE_IDLE_MS;
294
+ let _revStuckMsV2 = NOTE_DEFAULT_REV_STUCK_MS;
295
+
296
+ function _posIntNoteV2(n, label) {
297
+ const v = Math.floor(Number(n));
298
+ if (!Number.isFinite(v) || v <= 0)
299
+ throw new Error(`${label} must be a positive integer`);
300
+ return v;
301
+ }
302
+
303
+ export function getMaxActiveNotesPerAuthorV2() {
304
+ return _maxActiveNotesPerAuthorV2;
305
+ }
306
+ export function setMaxActiveNotesPerAuthorV2(n) {
307
+ _maxActiveNotesPerAuthorV2 = _posIntNoteV2(n, "maxActiveNotesPerAuthor");
308
+ }
309
+ export function getMaxOpenRevsPerNoteV2() {
310
+ return _maxOpenRevsPerNoteV2;
311
+ }
312
+ export function setMaxOpenRevsPerNoteV2(n) {
313
+ _maxOpenRevsPerNoteV2 = _posIntNoteV2(n, "maxOpenRevsPerNote");
314
+ }
315
+ export function getNoteIdleMsV2() {
316
+ return _noteIdleMsV2;
317
+ }
318
+ export function setNoteIdleMsV2(n) {
319
+ _noteIdleMsV2 = _posIntNoteV2(n, "noteIdleMs");
320
+ }
321
+ export function getRevStuckMsV2() {
322
+ return _revStuckMsV2;
323
+ }
324
+ export function setRevStuckMsV2(n) {
325
+ _revStuckMsV2 = _posIntNoteV2(n, "revStuckMs");
326
+ }
327
+
328
+ export function getActiveNoteCountV2(authorId) {
329
+ let n = 0;
330
+ for (const note of _notesV2.values()) {
331
+ if (note.authorId === authorId && note.status === "active") n += 1;
332
+ }
333
+ return n;
334
+ }
335
+
336
+ export function getOpenRevCountV2(noteId) {
337
+ let n = 0;
338
+ for (const r of _revsV2.values()) {
339
+ if (
340
+ r.noteId === noteId &&
341
+ (r.status === "proposed" || r.status === "reviewed")
342
+ )
343
+ n += 1;
344
+ }
345
+ return n;
346
+ }
347
+
348
+ function _copyNoteV2(n) {
349
+ return { ...n, metadata: { ...n.metadata } };
350
+ }
351
+ function _copyRevV2(r) {
352
+ return { ...r, metadata: { ...r.metadata } };
353
+ }
354
+
355
+ export function registerNoteV2(
356
+ id,
357
+ { authorId, title, metadata = {}, now = Date.now() } = {},
358
+ ) {
359
+ if (!id || typeof id !== "string") throw new Error("id must be a string");
360
+ if (!authorId || typeof authorId !== "string")
361
+ throw new Error("authorId must be a string");
362
+ if (!title || typeof title !== "string")
363
+ throw new Error("title must be a string");
364
+ if (_notesV2.has(id)) throw new Error(`note ${id} already exists`);
365
+ const note = {
366
+ id,
367
+ authorId,
368
+ title,
369
+ status: "draft",
370
+ createdAt: now,
371
+ lastSeenAt: now,
372
+ activatedAt: null,
373
+ archivedAt: null,
374
+ metadata: { ...metadata },
375
+ };
376
+ _notesV2.set(id, note);
377
+ return _copyNoteV2(note);
378
+ }
379
+
380
+ export function getNoteV2(id) {
381
+ const note = _notesV2.get(id);
382
+ return note ? _copyNoteV2(note) : null;
383
+ }
384
+
385
+ export function listNotesV2({ authorId, status } = {}) {
386
+ const out = [];
387
+ for (const note of _notesV2.values()) {
388
+ if (authorId && note.authorId !== authorId) continue;
389
+ if (status && note.status !== status) continue;
390
+ out.push(_copyNoteV2(note));
391
+ }
392
+ return out;
393
+ }
394
+
395
+ export function setNoteStatusV2(id, next, { now = Date.now() } = {}) {
396
+ const note = _notesV2.get(id);
397
+ if (!note) throw new Error(`note ${id} not found`);
398
+ if (!NOTE_TRANSITIONS_V2.has(next))
399
+ throw new Error(`unknown note status: ${next}`);
400
+ if (NOTE_TERMINALS_V2.has(note.status))
401
+ throw new Error(`note ${id} is in terminal state ${note.status}`);
402
+ const allowed = NOTE_TRANSITIONS_V2.get(note.status);
403
+ if (!allowed.has(next))
404
+ throw new Error(`cannot transition note from ${note.status} to ${next}`);
405
+ if (next === "active") {
406
+ if (note.status === "draft") {
407
+ const count = getActiveNoteCountV2(note.authorId);
408
+ if (count >= _maxActiveNotesPerAuthorV2)
409
+ throw new Error(
410
+ `author ${note.authorId} already at active-note cap (${_maxActiveNotesPerAuthorV2})`,
411
+ );
412
+ }
413
+ if (!note.activatedAt) note.activatedAt = now;
414
+ }
415
+ if (next === "archived" && !note.archivedAt) note.archivedAt = now;
416
+ note.status = next;
417
+ note.lastSeenAt = now;
418
+ return _copyNoteV2(note);
419
+ }
420
+
421
+ export function activateNoteV2(id, opts) {
422
+ return setNoteStatusV2(id, "active", opts);
423
+ }
424
+ export function lockNoteV2(id, opts) {
425
+ return setNoteStatusV2(id, "locked", opts);
426
+ }
427
+ export function archiveNoteV2(id, opts) {
428
+ return setNoteStatusV2(id, "archived", opts);
429
+ }
430
+
431
+ export function touchNoteV2(id, { now = Date.now() } = {}) {
432
+ const note = _notesV2.get(id);
433
+ if (!note) throw new Error(`note ${id} not found`);
434
+ note.lastSeenAt = now;
435
+ return _copyNoteV2(note);
436
+ }
437
+
438
+ export function createRevisionV2(
439
+ id,
440
+ { noteId, summary, metadata = {}, now = Date.now() } = {},
441
+ ) {
442
+ if (!id || typeof id !== "string") throw new Error("id must be a string");
443
+ if (!noteId || typeof noteId !== "string")
444
+ throw new Error("noteId must be a string");
445
+ if (!summary || typeof summary !== "string")
446
+ throw new Error("summary must be a string");
447
+ if (_revsV2.has(id)) throw new Error(`revision ${id} already exists`);
448
+ const count = getOpenRevCountV2(noteId);
449
+ if (count >= _maxOpenRevsPerNoteV2)
450
+ throw new Error(
451
+ `note ${noteId} already at open-revision cap (${_maxOpenRevsPerNoteV2})`,
452
+ );
453
+ const r = {
454
+ id,
455
+ noteId,
456
+ summary,
457
+ status: "proposed",
458
+ createdAt: now,
459
+ lastSeenAt: now,
460
+ reviewedAt: null,
461
+ appliedAt: null,
462
+ settledAt: null,
463
+ metadata: { ...metadata },
464
+ };
465
+ _revsV2.set(id, r);
466
+ return _copyRevV2(r);
467
+ }
468
+
469
+ export function getRevisionV2(id) {
470
+ const r = _revsV2.get(id);
471
+ return r ? _copyRevV2(r) : null;
472
+ }
473
+
474
+ export function listRevisionsV2({ noteId, status } = {}) {
475
+ const out = [];
476
+ for (const r of _revsV2.values()) {
477
+ if (noteId && r.noteId !== noteId) continue;
478
+ if (status && r.status !== status) continue;
479
+ out.push(_copyRevV2(r));
480
+ }
481
+ return out;
482
+ }
483
+
484
+ export function setRevisionStatusV2(id, next, { now = Date.now() } = {}) {
485
+ const r = _revsV2.get(id);
486
+ if (!r) throw new Error(`revision ${id} not found`);
487
+ if (!REV_TRANSITIONS_V2.has(next))
488
+ throw new Error(`unknown revision status: ${next}`);
489
+ if (REV_TERMINALS_V2.has(r.status))
490
+ throw new Error(`revision ${id} is in terminal state ${r.status}`);
491
+ const allowed = REV_TRANSITIONS_V2.get(r.status);
492
+ if (!allowed.has(next))
493
+ throw new Error(`cannot transition revision from ${r.status} to ${next}`);
494
+ if (next === "reviewed" && !r.reviewedAt) r.reviewedAt = now;
495
+ if (next === "applied" && !r.appliedAt) r.appliedAt = now;
496
+ if (REV_TERMINALS_V2.has(next) && !r.settledAt) r.settledAt = now;
497
+ r.status = next;
498
+ r.lastSeenAt = now;
499
+ return _copyRevV2(r);
500
+ }
501
+
502
+ export function reviewRevisionV2(id, opts) {
503
+ return setRevisionStatusV2(id, "reviewed", opts);
504
+ }
505
+ export function applyRevisionV2(id, opts) {
506
+ return setRevisionStatusV2(id, "applied", opts);
507
+ }
508
+ export function supersedeRevisionV2(id, opts) {
509
+ return setRevisionStatusV2(id, "superseded", opts);
510
+ }
511
+ export function discardRevisionV2(id, opts) {
512
+ return setRevisionStatusV2(id, "discarded", opts);
513
+ }
514
+
515
+ export function autoLockIdleNotesV2({ now = Date.now() } = {}) {
516
+ const flipped = [];
517
+ for (const note of _notesV2.values()) {
518
+ if (note.status !== "active") continue;
519
+ if (now - note.lastSeenAt > _noteIdleMsV2) {
520
+ note.status = "locked";
521
+ note.lastSeenAt = now;
522
+ flipped.push(_copyNoteV2(note));
523
+ }
524
+ }
525
+ return flipped;
526
+ }
527
+
528
+ export function autoDiscardStaleRevisionsV2({ now = Date.now() } = {}) {
529
+ const flipped = [];
530
+ for (const r of _revsV2.values()) {
531
+ if (r.status !== "proposed" && r.status !== "reviewed") continue;
532
+ if (now - r.lastSeenAt > _revStuckMsV2) {
533
+ r.status = "discarded";
534
+ r.lastSeenAt = now;
535
+ if (!r.settledAt) r.settledAt = now;
536
+ flipped.push(_copyRevV2(r));
537
+ }
538
+ }
539
+ return flipped;
540
+ }
541
+
542
+ export function getNoteVersioningStatsV2() {
543
+ const notesByStatus = {};
544
+ for (const v of Object.values(NOTE_MATURITY_V2)) notesByStatus[v] = 0;
545
+ for (const note of _notesV2.values()) notesByStatus[note.status] += 1;
546
+
547
+ const revisionsByStatus = {};
548
+ for (const v of Object.values(REVISION_LIFECYCLE_V2))
549
+ revisionsByStatus[v] = 0;
550
+ for (const r of _revsV2.values()) revisionsByStatus[r.status] += 1;
551
+
552
+ return {
553
+ totalNotesV2: _notesV2.size,
554
+ totalRevisionsV2: _revsV2.size,
555
+ maxActiveNotesPerAuthor: _maxActiveNotesPerAuthorV2,
556
+ maxOpenRevsPerNote: _maxOpenRevsPerNoteV2,
557
+ noteIdleMs: _noteIdleMsV2,
558
+ revStuckMs: _revStuckMsV2,
559
+ notesByStatus,
560
+ revisionsByStatus,
561
+ };
562
+ }
563
+
564
+ export function _resetStateNoteVersioningV2() {
565
+ _notesV2.clear();
566
+ _revsV2.clear();
567
+ _maxActiveNotesPerAuthorV2 = NOTE_DEFAULT_MAX_ACTIVE_NOTES_PER_AUTHOR;
568
+ _maxOpenRevsPerNoteV2 = NOTE_DEFAULT_MAX_OPEN_REVS_PER_NOTE;
569
+ _noteIdleMsV2 = NOTE_DEFAULT_NOTE_IDLE_MS;
570
+ _revStuckMsV2 = NOTE_DEFAULT_REV_STUCK_MS;
571
+ }
@@ -436,3 +436,108 @@ function _extractChangedFiles(output) {
436
436
  const matches = output.match(/(?:modified|created|deleted):\s+(\S+)/g) || [];
437
437
  return matches.map((m) => m.split(/:\s+/)[1]).filter(Boolean);
438
438
  }
439
+
440
+
441
+ // ===== V2 Surface: Orchestrator governance overlay (CLI v0.140.0) =====
442
+ export const ORCH_PROFILE_MATURITY_V2 = Object.freeze({
443
+ PENDING: "pending", ACTIVE: "active", PAUSED: "paused", RETIRED: "retired",
444
+ });
445
+ export const ORCH_TASK_LIFECYCLE_V2 = Object.freeze({
446
+ QUEUED: "queued", DISPATCHING: "dispatching", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
447
+ });
448
+
449
+ const _opTrans = new Map([
450
+ [ORCH_PROFILE_MATURITY_V2.PENDING, new Set([ORCH_PROFILE_MATURITY_V2.ACTIVE, ORCH_PROFILE_MATURITY_V2.RETIRED])],
451
+ [ORCH_PROFILE_MATURITY_V2.ACTIVE, new Set([ORCH_PROFILE_MATURITY_V2.PAUSED, ORCH_PROFILE_MATURITY_V2.RETIRED])],
452
+ [ORCH_PROFILE_MATURITY_V2.PAUSED, new Set([ORCH_PROFILE_MATURITY_V2.ACTIVE, ORCH_PROFILE_MATURITY_V2.RETIRED])],
453
+ [ORCH_PROFILE_MATURITY_V2.RETIRED, new Set()],
454
+ ]);
455
+ const _opTerminal = new Set([ORCH_PROFILE_MATURITY_V2.RETIRED]);
456
+ const _otTrans = new Map([
457
+ [ORCH_TASK_LIFECYCLE_V2.QUEUED, new Set([ORCH_TASK_LIFECYCLE_V2.DISPATCHING, ORCH_TASK_LIFECYCLE_V2.CANCELLED])],
458
+ [ORCH_TASK_LIFECYCLE_V2.DISPATCHING, new Set([ORCH_TASK_LIFECYCLE_V2.COMPLETED, ORCH_TASK_LIFECYCLE_V2.FAILED, ORCH_TASK_LIFECYCLE_V2.CANCELLED])],
459
+ [ORCH_TASK_LIFECYCLE_V2.COMPLETED, new Set()],
460
+ [ORCH_TASK_LIFECYCLE_V2.FAILED, new Set()],
461
+ [ORCH_TASK_LIFECYCLE_V2.CANCELLED, new Set()],
462
+ ]);
463
+
464
+ const _opsV2 = new Map();
465
+ const _otsV2 = new Map();
466
+ let _opMaxActivePerOwner = 6;
467
+ let _opMaxPendingTasksPerProfile = 12;
468
+ let _opIdleMs = 14 * 24 * 60 * 60 * 1000;
469
+ let _otStuckMs = 15 * 60 * 1000;
470
+
471
+ function _opPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
472
+
473
+ export function setMaxActiveOrchProfilesPerOwnerV2(n) { _opMaxActivePerOwner = _opPos(n, "maxActiveOrchProfilesPerOwner"); }
474
+ export function getMaxActiveOrchProfilesPerOwnerV2() { return _opMaxActivePerOwner; }
475
+ export function setMaxPendingOrchTasksPerProfileV2(n) { _opMaxPendingTasksPerProfile = _opPos(n, "maxPendingOrchTasksPerProfile"); }
476
+ export function getMaxPendingOrchTasksPerProfileV2() { return _opMaxPendingTasksPerProfile; }
477
+ export function setOrchProfileIdleMsV2(n) { _opIdleMs = _opPos(n, "orchProfileIdleMs"); }
478
+ export function getOrchProfileIdleMsV2() { return _opIdleMs; }
479
+ export function setOrchTaskStuckMsV2(n) { _otStuckMs = _opPos(n, "orchTaskStuckMs"); }
480
+ export function getOrchTaskStuckMsV2() { return _otStuckMs; }
481
+
482
+ export function _resetStateOrchestratorV2() {
483
+ _opsV2.clear(); _otsV2.clear();
484
+ _opMaxActivePerOwner = 6; _opMaxPendingTasksPerProfile = 12;
485
+ _opIdleMs = 14 * 24 * 60 * 60 * 1000; _otStuckMs = 15 * 60 * 1000;
486
+ }
487
+
488
+ export function registerOrchProfileV2({ id, owner, source, metadata } = {}) {
489
+ if (!id || typeof id !== "string") throw new Error("id is required");
490
+ if (!owner || typeof owner !== "string") throw new Error("owner is required");
491
+ if (_opsV2.has(id)) throw new Error(`orch profile ${id} already registered`);
492
+ const now = Date.now();
493
+ const p = { id, owner, source: source || "cli", status: ORCH_PROFILE_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, retiredAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
494
+ _opsV2.set(id, p);
495
+ return { ...p, metadata: { ...p.metadata } };
496
+ }
497
+ function _opCheckP(from, to) { const a = _opTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid orch profile transition ${from} → ${to}`); }
498
+ function _opCountActive(owner) { let n = 0; for (const p of _opsV2.values()) if (p.owner === owner && p.status === ORCH_PROFILE_MATURITY_V2.ACTIVE) n++; return n; }
499
+
500
+ export function activateOrchProfileV2(id) {
501
+ const p = _opsV2.get(id); if (!p) throw new Error(`orch profile ${id} not found`);
502
+ _opCheckP(p.status, ORCH_PROFILE_MATURITY_V2.ACTIVE);
503
+ const recovery = p.status === ORCH_PROFILE_MATURITY_V2.PAUSED;
504
+ if (!recovery) { const c = _opCountActive(p.owner); if (c >= _opMaxActivePerOwner) throw new Error(`max active orch profiles per owner (${_opMaxActivePerOwner}) reached for ${p.owner}`); }
505
+ const now = Date.now(); p.status = ORCH_PROFILE_MATURITY_V2.ACTIVE; p.updatedAt = now; p.lastTouchedAt = now; if (!p.activatedAt) p.activatedAt = now;
506
+ return { ...p, metadata: { ...p.metadata } };
507
+ }
508
+ export function pauseOrchProfileV2(id) { const p = _opsV2.get(id); if (!p) throw new Error(`orch profile ${id} not found`); _opCheckP(p.status, ORCH_PROFILE_MATURITY_V2.PAUSED); p.status = ORCH_PROFILE_MATURITY_V2.PAUSED; p.updatedAt = Date.now(); return { ...p, metadata: { ...p.metadata } }; }
509
+ export function retireOrchProfileV2(id) { const p = _opsV2.get(id); if (!p) throw new Error(`orch profile ${id} not found`); _opCheckP(p.status, ORCH_PROFILE_MATURITY_V2.RETIRED); const now = Date.now(); p.status = ORCH_PROFILE_MATURITY_V2.RETIRED; p.updatedAt = now; if (!p.retiredAt) p.retiredAt = now; return { ...p, metadata: { ...p.metadata } }; }
510
+ export function touchOrchProfileV2(id) { const p = _opsV2.get(id); if (!p) throw new Error(`orch profile ${id} not found`); if (_opTerminal.has(p.status)) throw new Error(`cannot touch terminal orch profile ${id}`); const now = Date.now(); p.lastTouchedAt = now; p.updatedAt = now; return { ...p, metadata: { ...p.metadata } }; }
511
+ export function getOrchProfileV2(id) { const p = _opsV2.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
512
+ export function listOrchProfilesV2() { return [..._opsV2.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
513
+
514
+ function _otCountPending(profileId) { let n = 0; for (const t of _otsV2.values()) if (t.profileId === profileId && (t.status === ORCH_TASK_LIFECYCLE_V2.QUEUED || t.status === ORCH_TASK_LIFECYCLE_V2.DISPATCHING)) n++; return n; }
515
+
516
+ export function createOrchTaskV2({ id, profileId, prompt, metadata } = {}) {
517
+ if (!id || typeof id !== "string") throw new Error("id is required");
518
+ if (!profileId || typeof profileId !== "string") throw new Error("profileId is required");
519
+ if (_otsV2.has(id)) throw new Error(`orch task ${id} already exists`);
520
+ if (!_opsV2.has(profileId)) throw new Error(`orch profile ${profileId} not found`);
521
+ const pending = _otCountPending(profileId);
522
+ if (pending >= _opMaxPendingTasksPerProfile) throw new Error(`max pending orch tasks per profile (${_opMaxPendingTasksPerProfile}) reached for ${profileId}`);
523
+ const now = Date.now();
524
+ const t = { id, profileId, prompt: prompt || "", status: ORCH_TASK_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
525
+ _otsV2.set(id, t);
526
+ return { ...t, metadata: { ...t.metadata } };
527
+ }
528
+ function _otCheckT(from, to) { const a = _otTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid orch task transition ${from} → ${to}`); }
529
+ export function dispatchOrchTaskV2(id) { const t = _otsV2.get(id); if (!t) throw new Error(`orch task ${id} not found`); _otCheckT(t.status, ORCH_TASK_LIFECYCLE_V2.DISPATCHING); const now = Date.now(); t.status = ORCH_TASK_LIFECYCLE_V2.DISPATCHING; t.updatedAt = now; if (!t.startedAt) t.startedAt = now; return { ...t, metadata: { ...t.metadata } }; }
530
+ export function completeOrchTaskV2(id) { const t = _otsV2.get(id); if (!t) throw new Error(`orch task ${id} not found`); _otCheckT(t.status, ORCH_TASK_LIFECYCLE_V2.COMPLETED); const now = Date.now(); t.status = ORCH_TASK_LIFECYCLE_V2.COMPLETED; t.updatedAt = now; if (!t.settledAt) t.settledAt = now; return { ...t, metadata: { ...t.metadata } }; }
531
+ export function failOrchTaskV2(id, reason) { const t = _otsV2.get(id); if (!t) throw new Error(`orch task ${id} not found`); _otCheckT(t.status, ORCH_TASK_LIFECYCLE_V2.FAILED); const now = Date.now(); t.status = ORCH_TASK_LIFECYCLE_V2.FAILED; t.updatedAt = now; if (!t.settledAt) t.settledAt = now; if (reason) t.metadata.failReason = String(reason); return { ...t, metadata: { ...t.metadata } }; }
532
+ export function cancelOrchTaskV2(id, reason) { const t = _otsV2.get(id); if (!t) throw new Error(`orch task ${id} not found`); _otCheckT(t.status, ORCH_TASK_LIFECYCLE_V2.CANCELLED); const now = Date.now(); t.status = ORCH_TASK_LIFECYCLE_V2.CANCELLED; t.updatedAt = now; if (!t.settledAt) t.settledAt = now; if (reason) t.metadata.cancelReason = String(reason); return { ...t, metadata: { ...t.metadata } }; }
533
+ export function getOrchTaskV2(id) { const t = _otsV2.get(id); if (!t) return null; return { ...t, metadata: { ...t.metadata } }; }
534
+ export function listOrchTasksV2() { return [..._otsV2.values()].map((t) => ({ ...t, metadata: { ...t.metadata } })); }
535
+
536
+ export function autoPauseIdleOrchProfilesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _opsV2.values()) if (p.status === ORCH_PROFILE_MATURITY_V2.ACTIVE && (t - p.lastTouchedAt) >= _opIdleMs) { p.status = ORCH_PROFILE_MATURITY_V2.PAUSED; p.updatedAt = t; flipped.push(p.id); } return { flipped, count: flipped.length }; }
537
+ export function autoFailStuckOrchTasksV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const x of _otsV2.values()) if (x.status === ORCH_TASK_LIFECYCLE_V2.DISPATCHING && x.startedAt != null && (t - x.startedAt) >= _otStuckMs) { x.status = ORCH_TASK_LIFECYCLE_V2.FAILED; x.updatedAt = t; if (!x.settledAt) x.settledAt = t; x.metadata.failReason = "auto-fail-stuck"; flipped.push(x.id); } return { flipped, count: flipped.length }; }
538
+
539
+ export function getOrchestratorGovStatsV2() {
540
+ const profilesByStatus = {}; for (const s of Object.values(ORCH_PROFILE_MATURITY_V2)) profilesByStatus[s] = 0; for (const p of _opsV2.values()) profilesByStatus[p.status]++;
541
+ const tasksByStatus = {}; for (const s of Object.values(ORCH_TASK_LIFECYCLE_V2)) tasksByStatus[s] = 0; for (const t of _otsV2.values()) tasksByStatus[t.status]++;
542
+ return { totalOrchProfilesV2: _opsV2.size, totalOrchTasksV2: _otsV2.size, maxActiveOrchProfilesPerOwner: _opMaxActivePerOwner, maxPendingOrchTasksPerProfile: _opMaxPendingTasksPerProfile, orchProfileIdleMs: _opIdleMs, orchTaskStuckMs: _otStuckMs, profilesByStatus, tasksByStatus };
543
+ }