chainlesschain 0.143.0 → 0.145.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +196 -28
  3. package/src/commands/activitypub.js +157 -27
  4. package/src/commands/agent.js +587 -103
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +152 -27
  7. package/src/commands/bm25.js +111 -27
  8. package/src/commands/browse.js +190 -29
  9. package/src/commands/ccron.js +128 -28
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +414 -34
  12. package/src/commands/compt.js +127 -29
  13. package/src/commands/consol.js +8 -2
  14. package/src/commands/cowork.js +580 -34
  15. package/src/commands/crosschain.js +182 -28
  16. package/src/commands/dao.js +182 -28
  17. package/src/commands/dlp.js +172 -27
  18. package/src/commands/economy.js +205 -50
  19. package/src/commands/evolution.js +203 -50
  20. package/src/commands/evomap.js +182 -27
  21. package/src/commands/fflag.js +74 -22
  22. package/src/commands/git.js +177 -37
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +204 -50
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +170 -34
  27. package/src/commands/itbudget.js +149 -33
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +195 -38
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +179 -27
  32. package/src/commands/mcpscaf.js +145 -33
  33. package/src/commands/meminj.js +145 -33
  34. package/src/commands/nostr.js +178 -27
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +144 -33
  37. package/src/commands/pdfp.js +109 -27
  38. package/src/commands/perf.js +169 -32
  39. package/src/commands/perm.js +144 -33
  40. package/src/commands/pipeline.js +207 -52
  41. package/src/commands/planmode.js +141 -32
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +111 -27
  44. package/src/commands/recommend.js +177 -34
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +145 -33
  48. package/src/commands/seshsearch.js +141 -33
  49. package/src/commands/seshtail.js +144 -33
  50. package/src/commands/seshu.js +152 -33
  51. package/src/commands/sganal.js +123 -29
  52. package/src/commands/siem.js +201 -34
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +146 -33
  55. package/src/commands/social.js +157 -32
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +145 -33
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +171 -33
  60. package/src/commands/topiccls.js +146 -33
  61. package/src/commands/uprof.js +141 -32
  62. package/src/commands/vcheck.js +122 -28
  63. package/src/commands/webfetch.js +141 -32
  64. package/src/commands/zkp.js +184 -28
  65. package/src/index.js +40 -0
  66. package/src/lib/a2a-protocol.js +319 -51
  67. package/src/lib/activitypub-bridge.js +288 -50
  68. package/src/lib/agent-economy.js +304 -51
  69. package/src/lib/app-builder.js +279 -46
  70. package/src/lib/audit-logger.js +321 -0
  71. package/src/lib/autonomous-agent.js +284 -48
  72. package/src/lib/bi-engine.js +283 -49
  73. package/src/lib/bm25-search.js +301 -49
  74. package/src/lib/browser-automation.js +296 -49
  75. package/src/lib/chat-core.js +336 -0
  76. package/src/lib/claude-code-bridge.js +341 -0
  77. package/src/lib/compliance-framework-reporter.js +305 -51
  78. package/src/lib/compliance-manager.js +330 -0
  79. package/src/lib/compression-telemetry.js +301 -49
  80. package/src/lib/content-recommender.js +317 -52
  81. package/src/lib/cowork-cron.js +298 -49
  82. package/src/lib/cowork-learning.js +333 -0
  83. package/src/lib/cowork-task-runner.js +308 -51
  84. package/src/lib/cowork-workflow.js +327 -0
  85. package/src/lib/cross-chain.js +311 -51
  86. package/src/lib/dao-governance.js +280 -46
  87. package/src/lib/dlp-engine.js +287 -49
  88. package/src/lib/evolution-system.js +278 -47
  89. package/src/lib/evomap-manager.js +280 -46
  90. package/src/lib/execution-backend.js +294 -48
  91. package/src/lib/feature-flags.js +294 -49
  92. package/src/lib/git-integration.js +285 -47
  93. package/src/lib/hardening-manager.js +341 -0
  94. package/src/lib/hierarchical-memory.js +284 -48
  95. package/src/lib/inference-network.js +308 -51
  96. package/src/lib/iteration-budget.js +302 -50
  97. package/src/lib/knowledge-graph.js +333 -0
  98. package/src/lib/matrix-bridge.js +281 -47
  99. package/src/lib/mcp-scaffold.js +318 -54
  100. package/src/lib/memory-injection.js +288 -49
  101. package/src/lib/nostr-bridge.js +286 -49
  102. package/src/lib/orchestrator.js +293 -48
  103. package/src/lib/pdf-parser.js +298 -49
  104. package/src/lib/perf-tuning.js +309 -50
  105. package/src/lib/permission-engine.js +287 -49
  106. package/src/lib/pipeline-orchestrator.js +289 -49
  107. package/src/lib/plan-mode.js +298 -51
  108. package/src/lib/privacy-computing.js +335 -0
  109. package/src/lib/reputation-optimizer.js +340 -0
  110. package/src/lib/sandbox-v2.js +327 -0
  111. package/src/lib/service-container.js +313 -52
  112. package/src/lib/session-consolidator.js +296 -49
  113. package/src/lib/session-hooks.js +312 -53
  114. package/src/lib/session-search.js +304 -51
  115. package/src/lib/session-tail.js +288 -49
  116. package/src/lib/session-usage.js +298 -52
  117. package/src/lib/siem-exporter.js +298 -51
  118. package/src/lib/skill-marketplace.js +345 -0
  119. package/src/lib/sla-manager.js +341 -0
  120. package/src/lib/slot-filler.js +303 -51
  121. package/src/lib/social-graph-analytics.js +295 -49
  122. package/src/lib/social-graph.js +272 -49
  123. package/src/lib/stress-tester.js +342 -0
  124. package/src/lib/sub-agent-registry.js +302 -53
  125. package/src/lib/task-model-selector.js +302 -50
  126. package/src/lib/terraform-manager.js +333 -0
  127. package/src/lib/todo-manager.js +281 -47
  128. package/src/lib/token-incentive.js +341 -0
  129. package/src/lib/topic-classifier.js +297 -49
  130. package/src/lib/user-profile.js +294 -50
  131. package/src/lib/version-checker.js +304 -50
  132. package/src/lib/web-fetch.js +292 -51
  133. package/src/lib/zkp-engine.js +286 -49
@@ -245,81 +245,333 @@ export function getTaskTypes() {
245
245
 
246
246
  // ===== V2 Surface: Task Model Selector governance overlay (CLI v0.141.0) =====
247
247
  export const TMS_PROFILE_MATURITY_V2 = Object.freeze({
248
- PENDING: "pending", ACTIVE: "active", STALE: "stale", DECOMMISSIONED: "decommissioned",
248
+ PENDING: "pending",
249
+ ACTIVE: "active",
250
+ STALE: "stale",
251
+ DECOMMISSIONED: "decommissioned",
249
252
  });
250
253
  export const TMS_SELECTION_LIFECYCLE_V2 = Object.freeze({
251
- QUEUED: "queued", SCORING: "scoring", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
254
+ QUEUED: "queued",
255
+ SCORING: "scoring",
256
+ COMPLETED: "completed",
257
+ FAILED: "failed",
258
+ CANCELLED: "cancelled",
252
259
  });
253
260
  const _tmsPTrans = new Map([
254
- [TMS_PROFILE_MATURITY_V2.PENDING, new Set([TMS_PROFILE_MATURITY_V2.ACTIVE, TMS_PROFILE_MATURITY_V2.DECOMMISSIONED])],
255
- [TMS_PROFILE_MATURITY_V2.ACTIVE, new Set([TMS_PROFILE_MATURITY_V2.STALE, TMS_PROFILE_MATURITY_V2.DECOMMISSIONED])],
256
- [TMS_PROFILE_MATURITY_V2.STALE, new Set([TMS_PROFILE_MATURITY_V2.ACTIVE, TMS_PROFILE_MATURITY_V2.DECOMMISSIONED])],
261
+ [
262
+ TMS_PROFILE_MATURITY_V2.PENDING,
263
+ new Set([
264
+ TMS_PROFILE_MATURITY_V2.ACTIVE,
265
+ TMS_PROFILE_MATURITY_V2.DECOMMISSIONED,
266
+ ]),
267
+ ],
268
+ [
269
+ TMS_PROFILE_MATURITY_V2.ACTIVE,
270
+ new Set([
271
+ TMS_PROFILE_MATURITY_V2.STALE,
272
+ TMS_PROFILE_MATURITY_V2.DECOMMISSIONED,
273
+ ]),
274
+ ],
275
+ [
276
+ TMS_PROFILE_MATURITY_V2.STALE,
277
+ new Set([
278
+ TMS_PROFILE_MATURITY_V2.ACTIVE,
279
+ TMS_PROFILE_MATURITY_V2.DECOMMISSIONED,
280
+ ]),
281
+ ],
257
282
  [TMS_PROFILE_MATURITY_V2.DECOMMISSIONED, new Set()],
258
283
  ]);
259
284
  const _tmsPTerminal = new Set([TMS_PROFILE_MATURITY_V2.DECOMMISSIONED]);
260
285
  const _tmsSTrans = new Map([
261
- [TMS_SELECTION_LIFECYCLE_V2.QUEUED, new Set([TMS_SELECTION_LIFECYCLE_V2.SCORING, TMS_SELECTION_LIFECYCLE_V2.CANCELLED])],
262
- [TMS_SELECTION_LIFECYCLE_V2.SCORING, new Set([TMS_SELECTION_LIFECYCLE_V2.COMPLETED, TMS_SELECTION_LIFECYCLE_V2.FAILED, TMS_SELECTION_LIFECYCLE_V2.CANCELLED])],
286
+ [
287
+ TMS_SELECTION_LIFECYCLE_V2.QUEUED,
288
+ new Set([
289
+ TMS_SELECTION_LIFECYCLE_V2.SCORING,
290
+ TMS_SELECTION_LIFECYCLE_V2.CANCELLED,
291
+ ]),
292
+ ],
293
+ [
294
+ TMS_SELECTION_LIFECYCLE_V2.SCORING,
295
+ new Set([
296
+ TMS_SELECTION_LIFECYCLE_V2.COMPLETED,
297
+ TMS_SELECTION_LIFECYCLE_V2.FAILED,
298
+ TMS_SELECTION_LIFECYCLE_V2.CANCELLED,
299
+ ]),
300
+ ],
263
301
  [TMS_SELECTION_LIFECYCLE_V2.COMPLETED, new Set()],
264
302
  [TMS_SELECTION_LIFECYCLE_V2.FAILED, new Set()],
265
303
  [TMS_SELECTION_LIFECYCLE_V2.CANCELLED, new Set()],
266
304
  ]);
267
305
  const _tmsPsV2 = new Map();
268
306
  const _tmsSsV2 = new Map();
269
- let _tmsMaxActivePerOwner = 8, _tmsMaxPendingSelPerProfile = 16, _tmsIdleMs = 14 * 24 * 60 * 60 * 1000, _tmsStuckMs = 2 * 60 * 1000;
270
- function _tmsPos(n, label) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${label} must be positive integer`); return v; }
271
- function _tmsCheckP(from, to) { const a = _tmsPTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid tms profile transition ${from} → ${to}`); }
272
- function _tmsCheckS(from, to) { const a = _tmsSTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid tms selection transition ${from} → ${to}`); }
273
- export function setMaxActiveTmsProfilesPerOwnerV2(n) { _tmsMaxActivePerOwner = _tmsPos(n, "maxActiveTmsProfilesPerOwner"); }
274
- export function getMaxActiveTmsProfilesPerOwnerV2() { return _tmsMaxActivePerOwner; }
275
- export function setMaxPendingTmsSelectionsPerProfileV2(n) { _tmsMaxPendingSelPerProfile = _tmsPos(n, "maxPendingTmsSelectionsPerProfile"); }
276
- export function getMaxPendingTmsSelectionsPerProfileV2() { return _tmsMaxPendingSelPerProfile; }
277
- export function setTmsProfileIdleMsV2(n) { _tmsIdleMs = _tmsPos(n, "tmsProfileIdleMs"); }
278
- export function getTmsProfileIdleMsV2() { return _tmsIdleMs; }
279
- export function setTmsSelectionStuckMsV2(n) { _tmsStuckMs = _tmsPos(n, "tmsSelectionStuckMs"); }
280
- export function getTmsSelectionStuckMsV2() { return _tmsStuckMs; }
281
- export function _resetStateTaskModelSelectorV2() { _tmsPsV2.clear(); _tmsSsV2.clear(); _tmsMaxActivePerOwner = 8; _tmsMaxPendingSelPerProfile = 16; _tmsIdleMs = 14 * 24 * 60 * 60 * 1000; _tmsStuckMs = 2 * 60 * 1000; }
307
+ let _tmsMaxActivePerOwner = 8,
308
+ _tmsMaxPendingSelPerProfile = 16,
309
+ _tmsIdleMs = 14 * 24 * 60 * 60 * 1000,
310
+ _tmsStuckMs = 2 * 60 * 1000;
311
+ function _tmsPos(n, label) {
312
+ const v = Math.floor(Number(n));
313
+ if (!Number.isFinite(v) || v <= 0)
314
+ throw new Error(`${label} must be positive integer`);
315
+ return v;
316
+ }
317
+ function _tmsCheckP(from, to) {
318
+ const a = _tmsPTrans.get(from);
319
+ if (!a || !a.has(to))
320
+ throw new Error(`invalid tms profile transition ${from} → ${to}`);
321
+ }
322
+ function _tmsCheckS(from, to) {
323
+ const a = _tmsSTrans.get(from);
324
+ if (!a || !a.has(to))
325
+ throw new Error(`invalid tms selection transition ${from} → ${to}`);
326
+ }
327
+ export function setMaxActiveTmsProfilesPerOwnerV2(n) {
328
+ _tmsMaxActivePerOwner = _tmsPos(n, "maxActiveTmsProfilesPerOwner");
329
+ }
330
+ export function getMaxActiveTmsProfilesPerOwnerV2() {
331
+ return _tmsMaxActivePerOwner;
332
+ }
333
+ export function setMaxPendingTmsSelectionsPerProfileV2(n) {
334
+ _tmsMaxPendingSelPerProfile = _tmsPos(n, "maxPendingTmsSelectionsPerProfile");
335
+ }
336
+ export function getMaxPendingTmsSelectionsPerProfileV2() {
337
+ return _tmsMaxPendingSelPerProfile;
338
+ }
339
+ export function setTmsProfileIdleMsV2(n) {
340
+ _tmsIdleMs = _tmsPos(n, "tmsProfileIdleMs");
341
+ }
342
+ export function getTmsProfileIdleMsV2() {
343
+ return _tmsIdleMs;
344
+ }
345
+ export function setTmsSelectionStuckMsV2(n) {
346
+ _tmsStuckMs = _tmsPos(n, "tmsSelectionStuckMs");
347
+ }
348
+ export function getTmsSelectionStuckMsV2() {
349
+ return _tmsStuckMs;
350
+ }
351
+ export function _resetStateTaskModelSelectorV2() {
352
+ _tmsPsV2.clear();
353
+ _tmsSsV2.clear();
354
+ _tmsMaxActivePerOwner = 8;
355
+ _tmsMaxPendingSelPerProfile = 16;
356
+ _tmsIdleMs = 14 * 24 * 60 * 60 * 1000;
357
+ _tmsStuckMs = 2 * 60 * 1000;
358
+ }
282
359
  export function registerTmsProfileV2({ id, owner, strategy, metadata } = {}) {
283
- if (!id) throw new Error("tms profile id required"); if (!owner) throw new Error("tms profile owner required");
360
+ if (!id) throw new Error("tms profile id required");
361
+ if (!owner) throw new Error("tms profile owner required");
284
362
  if (_tmsPsV2.has(id)) throw new Error(`tms profile ${id} already registered`);
285
363
  const now = Date.now();
286
- const p = { id, owner, strategy: strategy || "default", status: TMS_PROFILE_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, decommissionedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
287
- _tmsPsV2.set(id, p); return { ...p, metadata: { ...p.metadata } };
364
+ const p = {
365
+ id,
366
+ owner,
367
+ strategy: strategy || "default",
368
+ status: TMS_PROFILE_MATURITY_V2.PENDING,
369
+ createdAt: now,
370
+ updatedAt: now,
371
+ activatedAt: null,
372
+ decommissionedAt: null,
373
+ lastTouchedAt: now,
374
+ metadata: { ...(metadata || {}) },
375
+ };
376
+ _tmsPsV2.set(id, p);
377
+ return { ...p, metadata: { ...p.metadata } };
378
+ }
379
+ function _tmsCountActive(owner) {
380
+ let n = 0;
381
+ for (const p of _tmsPsV2.values())
382
+ if (p.owner === owner && p.status === TMS_PROFILE_MATURITY_V2.ACTIVE) n++;
383
+ return n;
288
384
  }
289
- function _tmsCountActive(owner) { let n = 0; for (const p of _tmsPsV2.values()) if (p.owner === owner && p.status === TMS_PROFILE_MATURITY_V2.ACTIVE) n++; return n; }
290
385
  export function activateTmsProfileV2(id) {
291
- const p = _tmsPsV2.get(id); if (!p) throw new Error(`tms profile ${id} not found`);
386
+ const p = _tmsPsV2.get(id);
387
+ if (!p) throw new Error(`tms profile ${id} not found`);
292
388
  _tmsCheckP(p.status, TMS_PROFILE_MATURITY_V2.ACTIVE);
293
389
  const recovery = p.status === TMS_PROFILE_MATURITY_V2.STALE;
294
- if (!recovery && _tmsCountActive(p.owner) >= _tmsMaxActivePerOwner) throw new Error(`max active tms profiles for owner ${p.owner} reached`);
295
- const now = Date.now(); p.status = TMS_PROFILE_MATURITY_V2.ACTIVE; p.updatedAt = now; p.lastTouchedAt = now; if (!p.activatedAt) p.activatedAt = now;
390
+ if (!recovery && _tmsCountActive(p.owner) >= _tmsMaxActivePerOwner)
391
+ throw new Error(`max active tms profiles for owner ${p.owner} reached`);
392
+ const now = Date.now();
393
+ p.status = TMS_PROFILE_MATURITY_V2.ACTIVE;
394
+ p.updatedAt = now;
395
+ p.lastTouchedAt = now;
396
+ if (!p.activatedAt) p.activatedAt = now;
397
+ return { ...p, metadata: { ...p.metadata } };
398
+ }
399
+ export function staleTmsProfileV2(id) {
400
+ const p = _tmsPsV2.get(id);
401
+ if (!p) throw new Error(`tms profile ${id} not found`);
402
+ _tmsCheckP(p.status, TMS_PROFILE_MATURITY_V2.STALE);
403
+ p.status = TMS_PROFILE_MATURITY_V2.STALE;
404
+ p.updatedAt = Date.now();
296
405
  return { ...p, metadata: { ...p.metadata } };
297
406
  }
298
- export function staleTmsProfileV2(id) { const p = _tmsPsV2.get(id); if (!p) throw new Error(`tms profile ${id} not found`); _tmsCheckP(p.status, TMS_PROFILE_MATURITY_V2.STALE); p.status = TMS_PROFILE_MATURITY_V2.STALE; p.updatedAt = Date.now(); return { ...p, metadata: { ...p.metadata } }; }
299
- export function decommissionTmsProfileV2(id) { const p = _tmsPsV2.get(id); if (!p) throw new Error(`tms profile ${id} not found`); _tmsCheckP(p.status, TMS_PROFILE_MATURITY_V2.DECOMMISSIONED); const now = Date.now(); p.status = TMS_PROFILE_MATURITY_V2.DECOMMISSIONED; p.updatedAt = now; if (!p.decommissionedAt) p.decommissionedAt = now; return { ...p, metadata: { ...p.metadata } }; }
300
- export function touchTmsProfileV2(id) { const p = _tmsPsV2.get(id); if (!p) throw new Error(`tms profile ${id} not found`); if (_tmsPTerminal.has(p.status)) throw new Error(`cannot touch terminal tms profile ${id}`); const now = Date.now(); p.lastTouchedAt = now; p.updatedAt = now; return { ...p, metadata: { ...p.metadata } }; }
301
- export function getTmsProfileV2(id) { const p = _tmsPsV2.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
302
- export function listTmsProfilesV2() { return [..._tmsPsV2.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
303
- function _tmsCountPending(profileId) { let n = 0; for (const s of _tmsSsV2.values()) if (s.profileId === profileId && (s.status === TMS_SELECTION_LIFECYCLE_V2.QUEUED || s.status === TMS_SELECTION_LIFECYCLE_V2.SCORING)) n++; return n; }
407
+ export function decommissionTmsProfileV2(id) {
408
+ const p = _tmsPsV2.get(id);
409
+ if (!p) throw new Error(`tms profile ${id} not found`);
410
+ _tmsCheckP(p.status, TMS_PROFILE_MATURITY_V2.DECOMMISSIONED);
411
+ const now = Date.now();
412
+ p.status = TMS_PROFILE_MATURITY_V2.DECOMMISSIONED;
413
+ p.updatedAt = now;
414
+ if (!p.decommissionedAt) p.decommissionedAt = now;
415
+ return { ...p, metadata: { ...p.metadata } };
416
+ }
417
+ export function touchTmsProfileV2(id) {
418
+ const p = _tmsPsV2.get(id);
419
+ if (!p) throw new Error(`tms profile ${id} not found`);
420
+ if (_tmsPTerminal.has(p.status))
421
+ throw new Error(`cannot touch terminal tms profile ${id}`);
422
+ const now = Date.now();
423
+ p.lastTouchedAt = now;
424
+ p.updatedAt = now;
425
+ return { ...p, metadata: { ...p.metadata } };
426
+ }
427
+ export function getTmsProfileV2(id) {
428
+ const p = _tmsPsV2.get(id);
429
+ if (!p) return null;
430
+ return { ...p, metadata: { ...p.metadata } };
431
+ }
432
+ export function listTmsProfilesV2() {
433
+ return [..._tmsPsV2.values()].map((p) => ({
434
+ ...p,
435
+ metadata: { ...p.metadata },
436
+ }));
437
+ }
438
+ function _tmsCountPending(profileId) {
439
+ let n = 0;
440
+ for (const s of _tmsSsV2.values())
441
+ if (
442
+ s.profileId === profileId &&
443
+ (s.status === TMS_SELECTION_LIFECYCLE_V2.QUEUED ||
444
+ s.status === TMS_SELECTION_LIFECYCLE_V2.SCORING)
445
+ )
446
+ n++;
447
+ return n;
448
+ }
304
449
  export function createTmsSelectionV2({ id, profileId, task, metadata } = {}) {
305
- if (!id) throw new Error("tms selection id required"); if (!profileId) throw new Error("tms selection profileId required");
450
+ if (!id) throw new Error("tms selection id required");
451
+ if (!profileId) throw new Error("tms selection profileId required");
306
452
  if (_tmsSsV2.has(id)) throw new Error(`tms selection ${id} already exists`);
307
- if (!_tmsPsV2.has(profileId)) throw new Error(`tms profile ${profileId} not found`);
308
- if (_tmsCountPending(profileId) >= _tmsMaxPendingSelPerProfile) throw new Error(`max pending tms selections for profile ${profileId} reached`);
453
+ if (!_tmsPsV2.has(profileId))
454
+ throw new Error(`tms profile ${profileId} not found`);
455
+ if (_tmsCountPending(profileId) >= _tmsMaxPendingSelPerProfile)
456
+ throw new Error(
457
+ `max pending tms selections for profile ${profileId} reached`,
458
+ );
459
+ const now = Date.now();
460
+ const s = {
461
+ id,
462
+ profileId,
463
+ task: task || "",
464
+ status: TMS_SELECTION_LIFECYCLE_V2.QUEUED,
465
+ createdAt: now,
466
+ updatedAt: now,
467
+ startedAt: null,
468
+ settledAt: null,
469
+ metadata: { ...(metadata || {}) },
470
+ };
471
+ _tmsSsV2.set(id, s);
472
+ return { ...s, metadata: { ...s.metadata } };
473
+ }
474
+ export function scoreTmsSelectionV2(id) {
475
+ const s = _tmsSsV2.get(id);
476
+ if (!s) throw new Error(`tms selection ${id} not found`);
477
+ _tmsCheckS(s.status, TMS_SELECTION_LIFECYCLE_V2.SCORING);
478
+ const now = Date.now();
479
+ s.status = TMS_SELECTION_LIFECYCLE_V2.SCORING;
480
+ s.updatedAt = now;
481
+ if (!s.startedAt) s.startedAt = now;
482
+ return { ...s, metadata: { ...s.metadata } };
483
+ }
484
+ export function completeTmsSelectionV2(id) {
485
+ const s = _tmsSsV2.get(id);
486
+ if (!s) throw new Error(`tms selection ${id} not found`);
487
+ _tmsCheckS(s.status, TMS_SELECTION_LIFECYCLE_V2.COMPLETED);
488
+ const now = Date.now();
489
+ s.status = TMS_SELECTION_LIFECYCLE_V2.COMPLETED;
490
+ s.updatedAt = now;
491
+ if (!s.settledAt) s.settledAt = now;
492
+ return { ...s, metadata: { ...s.metadata } };
493
+ }
494
+ export function failTmsSelectionV2(id, reason) {
495
+ const s = _tmsSsV2.get(id);
496
+ if (!s) throw new Error(`tms selection ${id} not found`);
497
+ _tmsCheckS(s.status, TMS_SELECTION_LIFECYCLE_V2.FAILED);
309
498
  const now = Date.now();
310
- const s = { id, profileId, task: task || "", status: TMS_SELECTION_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
311
- _tmsSsV2.set(id, s); return { ...s, metadata: { ...s.metadata } };
312
- }
313
- export function scoreTmsSelectionV2(id) { const s = _tmsSsV2.get(id); if (!s) throw new Error(`tms selection ${id} not found`); _tmsCheckS(s.status, TMS_SELECTION_LIFECYCLE_V2.SCORING); const now = Date.now(); s.status = TMS_SELECTION_LIFECYCLE_V2.SCORING; s.updatedAt = now; if (!s.startedAt) s.startedAt = now; return { ...s, metadata: { ...s.metadata } }; }
314
- export function completeTmsSelectionV2(id) { const s = _tmsSsV2.get(id); if (!s) throw new Error(`tms selection ${id} not found`); _tmsCheckS(s.status, TMS_SELECTION_LIFECYCLE_V2.COMPLETED); const now = Date.now(); s.status = TMS_SELECTION_LIFECYCLE_V2.COMPLETED; s.updatedAt = now; if (!s.settledAt) s.settledAt = now; return { ...s, metadata: { ...s.metadata } }; }
315
- export function failTmsSelectionV2(id, reason) { const s = _tmsSsV2.get(id); if (!s) throw new Error(`tms selection ${id} not found`); _tmsCheckS(s.status, TMS_SELECTION_LIFECYCLE_V2.FAILED); const now = Date.now(); s.status = TMS_SELECTION_LIFECYCLE_V2.FAILED; s.updatedAt = now; if (!s.settledAt) s.settledAt = now; if (reason) s.metadata.failReason = String(reason); return { ...s, metadata: { ...s.metadata } }; }
316
- export function cancelTmsSelectionV2(id, reason) { const s = _tmsSsV2.get(id); if (!s) throw new Error(`tms selection ${id} not found`); _tmsCheckS(s.status, TMS_SELECTION_LIFECYCLE_V2.CANCELLED); const now = Date.now(); s.status = TMS_SELECTION_LIFECYCLE_V2.CANCELLED; s.updatedAt = now; if (!s.settledAt) s.settledAt = now; if (reason) s.metadata.cancelReason = String(reason); return { ...s, metadata: { ...s.metadata } }; }
317
- export function getTmsSelectionV2(id) { const s = _tmsSsV2.get(id); if (!s) return null; return { ...s, metadata: { ...s.metadata } }; }
318
- export function listTmsSelectionsV2() { return [..._tmsSsV2.values()].map((s) => ({ ...s, metadata: { ...s.metadata } })); }
319
- export function autoStaleIdleTmsProfilesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _tmsPsV2.values()) if (p.status === TMS_PROFILE_MATURITY_V2.ACTIVE && (t - p.lastTouchedAt) >= _tmsIdleMs) { p.status = TMS_PROFILE_MATURITY_V2.STALE; p.updatedAt = t; flipped.push(p.id); } return { flipped, count: flipped.length }; }
320
- export function autoFailStuckTmsSelectionsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const s of _tmsSsV2.values()) if (s.status === TMS_SELECTION_LIFECYCLE_V2.SCORING && s.startedAt != null && (t - s.startedAt) >= _tmsStuckMs) { s.status = TMS_SELECTION_LIFECYCLE_V2.FAILED; s.updatedAt = t; if (!s.settledAt) s.settledAt = t; s.metadata.failReason = "auto-fail-stuck"; flipped.push(s.id); } return { flipped, count: flipped.length }; }
499
+ s.status = TMS_SELECTION_LIFECYCLE_V2.FAILED;
500
+ s.updatedAt = now;
501
+ if (!s.settledAt) s.settledAt = now;
502
+ if (reason) s.metadata.failReason = String(reason);
503
+ return { ...s, metadata: { ...s.metadata } };
504
+ }
505
+ export function cancelTmsSelectionV2(id, reason) {
506
+ const s = _tmsSsV2.get(id);
507
+ if (!s) throw new Error(`tms selection ${id} not found`);
508
+ _tmsCheckS(s.status, TMS_SELECTION_LIFECYCLE_V2.CANCELLED);
509
+ const now = Date.now();
510
+ s.status = TMS_SELECTION_LIFECYCLE_V2.CANCELLED;
511
+ s.updatedAt = now;
512
+ if (!s.settledAt) s.settledAt = now;
513
+ if (reason) s.metadata.cancelReason = String(reason);
514
+ return { ...s, metadata: { ...s.metadata } };
515
+ }
516
+ export function getTmsSelectionV2(id) {
517
+ const s = _tmsSsV2.get(id);
518
+ if (!s) return null;
519
+ return { ...s, metadata: { ...s.metadata } };
520
+ }
521
+ export function listTmsSelectionsV2() {
522
+ return [..._tmsSsV2.values()].map((s) => ({
523
+ ...s,
524
+ metadata: { ...s.metadata },
525
+ }));
526
+ }
527
+ export function autoStaleIdleTmsProfilesV2({ now } = {}) {
528
+ const t = now ?? Date.now();
529
+ const flipped = [];
530
+ for (const p of _tmsPsV2.values())
531
+ if (
532
+ p.status === TMS_PROFILE_MATURITY_V2.ACTIVE &&
533
+ t - p.lastTouchedAt >= _tmsIdleMs
534
+ ) {
535
+ p.status = TMS_PROFILE_MATURITY_V2.STALE;
536
+ p.updatedAt = t;
537
+ flipped.push(p.id);
538
+ }
539
+ return { flipped, count: flipped.length };
540
+ }
541
+ export function autoFailStuckTmsSelectionsV2({ now } = {}) {
542
+ const t = now ?? Date.now();
543
+ const flipped = [];
544
+ for (const s of _tmsSsV2.values())
545
+ if (
546
+ s.status === TMS_SELECTION_LIFECYCLE_V2.SCORING &&
547
+ s.startedAt != null &&
548
+ t - s.startedAt >= _tmsStuckMs
549
+ ) {
550
+ s.status = TMS_SELECTION_LIFECYCLE_V2.FAILED;
551
+ s.updatedAt = t;
552
+ if (!s.settledAt) s.settledAt = t;
553
+ s.metadata.failReason = "auto-fail-stuck";
554
+ flipped.push(s.id);
555
+ }
556
+ return { flipped, count: flipped.length };
557
+ }
321
558
  export function getTaskModelSelectorGovStatsV2() {
322
- const profilesByStatus = {}; for (const v of Object.values(TMS_PROFILE_MATURITY_V2)) profilesByStatus[v] = 0; for (const p of _tmsPsV2.values()) profilesByStatus[p.status]++;
323
- const selectionsByStatus = {}; for (const v of Object.values(TMS_SELECTION_LIFECYCLE_V2)) selectionsByStatus[v] = 0; for (const s of _tmsSsV2.values()) selectionsByStatus[s.status]++;
324
- return { totalTmsProfilesV2: _tmsPsV2.size, totalTmsSelectionsV2: _tmsSsV2.size, maxActiveTmsProfilesPerOwner: _tmsMaxActivePerOwner, maxPendingTmsSelectionsPerProfile: _tmsMaxPendingSelPerProfile, tmsProfileIdleMs: _tmsIdleMs, tmsSelectionStuckMs: _tmsStuckMs, profilesByStatus, selectionsByStatus };
559
+ const profilesByStatus = {};
560
+ for (const v of Object.values(TMS_PROFILE_MATURITY_V2))
561
+ profilesByStatus[v] = 0;
562
+ for (const p of _tmsPsV2.values()) profilesByStatus[p.status]++;
563
+ const selectionsByStatus = {};
564
+ for (const v of Object.values(TMS_SELECTION_LIFECYCLE_V2))
565
+ selectionsByStatus[v] = 0;
566
+ for (const s of _tmsSsV2.values()) selectionsByStatus[s.status]++;
567
+ return {
568
+ totalTmsProfilesV2: _tmsPsV2.size,
569
+ totalTmsSelectionsV2: _tmsSsV2.size,
570
+ maxActiveTmsProfilesPerOwner: _tmsMaxActivePerOwner,
571
+ maxPendingTmsSelectionsPerProfile: _tmsMaxPendingSelPerProfile,
572
+ tmsProfileIdleMs: _tmsIdleMs,
573
+ tmsSelectionStuckMs: _tmsStuckMs,
574
+ profilesByStatus,
575
+ selectionsByStatus,
576
+ };
325
577
  }