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
@@ -232,81 +232,340 @@ function withTimeout(promise, ms, label) {
232
232
 
233
233
  // ===== V2 Surface: Session Hooks governance overlay (CLI v0.142.0) =====
234
234
  export const SHOK_PROFILE_MATURITY_V2 = Object.freeze({
235
- PENDING: "pending", ACTIVE: "active", DISABLED: "disabled", RETIRED: "retired",
235
+ PENDING: "pending",
236
+ ACTIVE: "active",
237
+ DISABLED: "disabled",
238
+ RETIRED: "retired",
236
239
  });
237
240
  export const SHOK_INVOCATION_LIFECYCLE_V2 = Object.freeze({
238
- QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
241
+ QUEUED: "queued",
242
+ RUNNING: "running",
243
+ COMPLETED: "completed",
244
+ FAILED: "failed",
245
+ CANCELLED: "cancelled",
239
246
  });
240
247
  const _shokPTrans = new Map([
241
- [SHOK_PROFILE_MATURITY_V2.PENDING, new Set([SHOK_PROFILE_MATURITY_V2.ACTIVE, SHOK_PROFILE_MATURITY_V2.RETIRED])],
242
- [SHOK_PROFILE_MATURITY_V2.ACTIVE, new Set([SHOK_PROFILE_MATURITY_V2.DISABLED, SHOK_PROFILE_MATURITY_V2.RETIRED])],
243
- [SHOK_PROFILE_MATURITY_V2.DISABLED, new Set([SHOK_PROFILE_MATURITY_V2.ACTIVE, SHOK_PROFILE_MATURITY_V2.RETIRED])],
248
+ [
249
+ SHOK_PROFILE_MATURITY_V2.PENDING,
250
+ new Set([
251
+ SHOK_PROFILE_MATURITY_V2.ACTIVE,
252
+ SHOK_PROFILE_MATURITY_V2.RETIRED,
253
+ ]),
254
+ ],
255
+ [
256
+ SHOK_PROFILE_MATURITY_V2.ACTIVE,
257
+ new Set([
258
+ SHOK_PROFILE_MATURITY_V2.DISABLED,
259
+ SHOK_PROFILE_MATURITY_V2.RETIRED,
260
+ ]),
261
+ ],
262
+ [
263
+ SHOK_PROFILE_MATURITY_V2.DISABLED,
264
+ new Set([
265
+ SHOK_PROFILE_MATURITY_V2.ACTIVE,
266
+ SHOK_PROFILE_MATURITY_V2.RETIRED,
267
+ ]),
268
+ ],
244
269
  [SHOK_PROFILE_MATURITY_V2.RETIRED, new Set()],
245
270
  ]);
246
271
  const _shokPTerminal = new Set([SHOK_PROFILE_MATURITY_V2.RETIRED]);
247
272
  const _shokITrans = new Map([
248
- [SHOK_INVOCATION_LIFECYCLE_V2.QUEUED, new Set([SHOK_INVOCATION_LIFECYCLE_V2.RUNNING, SHOK_INVOCATION_LIFECYCLE_V2.CANCELLED])],
249
- [SHOK_INVOCATION_LIFECYCLE_V2.RUNNING, new Set([SHOK_INVOCATION_LIFECYCLE_V2.COMPLETED, SHOK_INVOCATION_LIFECYCLE_V2.FAILED, SHOK_INVOCATION_LIFECYCLE_V2.CANCELLED])],
273
+ [
274
+ SHOK_INVOCATION_LIFECYCLE_V2.QUEUED,
275
+ new Set([
276
+ SHOK_INVOCATION_LIFECYCLE_V2.RUNNING,
277
+ SHOK_INVOCATION_LIFECYCLE_V2.CANCELLED,
278
+ ]),
279
+ ],
280
+ [
281
+ SHOK_INVOCATION_LIFECYCLE_V2.RUNNING,
282
+ new Set([
283
+ SHOK_INVOCATION_LIFECYCLE_V2.COMPLETED,
284
+ SHOK_INVOCATION_LIFECYCLE_V2.FAILED,
285
+ SHOK_INVOCATION_LIFECYCLE_V2.CANCELLED,
286
+ ]),
287
+ ],
250
288
  [SHOK_INVOCATION_LIFECYCLE_V2.COMPLETED, new Set()],
251
289
  [SHOK_INVOCATION_LIFECYCLE_V2.FAILED, new Set()],
252
290
  [SHOK_INVOCATION_LIFECYCLE_V2.CANCELLED, new Set()],
253
291
  ]);
254
292
  const _shokPsV2 = new Map();
255
293
  const _shokIsV2 = new Map();
256
- let _shokMaxActive = 12, _shokMaxPending = 25, _shokIdleMs = 30 * 24 * 60 * 60 * 1000, _shokStuckMs = 30 * 1000;
257
- function _shokPos(n, label) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${label} must be positive integer`); return v; }
258
- function _shokCheckP(from, to) { const a = _shokPTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid shok profile transition ${from} → ${to}`); }
259
- function _shokCheckI(from, to) { const a = _shokITrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid shok invocation transition ${from} → ${to}`); }
260
- export function setMaxActiveShokProfilesPerOwnerV2(n) { _shokMaxActive = _shokPos(n, "maxActiveShokProfilesPerOwner"); }
261
- export function getMaxActiveShokProfilesPerOwnerV2() { return _shokMaxActive; }
262
- export function setMaxPendingShokInvocationsPerProfileV2(n) { _shokMaxPending = _shokPos(n, "maxPendingShokInvocationsPerProfile"); }
263
- export function getMaxPendingShokInvocationsPerProfileV2() { return _shokMaxPending; }
264
- export function setShokProfileIdleMsV2(n) { _shokIdleMs = _shokPos(n, "shokProfileIdleMs"); }
265
- export function getShokProfileIdleMsV2() { return _shokIdleMs; }
266
- export function setShokInvocationStuckMsV2(n) { _shokStuckMs = _shokPos(n, "shokInvocationStuckMs"); }
267
- export function getShokInvocationStuckMsV2() { return _shokStuckMs; }
268
- export function _resetStateSessionHooksV2() { _shokPsV2.clear(); _shokIsV2.clear(); _shokMaxActive = 12; _shokMaxPending = 25; _shokIdleMs = 30 * 24 * 60 * 60 * 1000; _shokStuckMs = 30 * 1000; }
294
+ let _shokMaxActive = 12,
295
+ _shokMaxPending = 25,
296
+ _shokIdleMs = 30 * 24 * 60 * 60 * 1000,
297
+ _shokStuckMs = 30 * 1000;
298
+ function _shokPos(n, label) {
299
+ const v = Math.floor(Number(n));
300
+ if (!Number.isFinite(v) || v <= 0)
301
+ throw new Error(`${label} must be positive integer`);
302
+ return v;
303
+ }
304
+ function _shokCheckP(from, to) {
305
+ const a = _shokPTrans.get(from);
306
+ if (!a || !a.has(to))
307
+ throw new Error(`invalid shok profile transition ${from} → ${to}`);
308
+ }
309
+ function _shokCheckI(from, to) {
310
+ const a = _shokITrans.get(from);
311
+ if (!a || !a.has(to))
312
+ throw new Error(`invalid shok invocation transition ${from} → ${to}`);
313
+ }
314
+ export function setMaxActiveShokProfilesPerOwnerV2(n) {
315
+ _shokMaxActive = _shokPos(n, "maxActiveShokProfilesPerOwner");
316
+ }
317
+ export function getMaxActiveShokProfilesPerOwnerV2() {
318
+ return _shokMaxActive;
319
+ }
320
+ export function setMaxPendingShokInvocationsPerProfileV2(n) {
321
+ _shokMaxPending = _shokPos(n, "maxPendingShokInvocationsPerProfile");
322
+ }
323
+ export function getMaxPendingShokInvocationsPerProfileV2() {
324
+ return _shokMaxPending;
325
+ }
326
+ export function setShokProfileIdleMsV2(n) {
327
+ _shokIdleMs = _shokPos(n, "shokProfileIdleMs");
328
+ }
329
+ export function getShokProfileIdleMsV2() {
330
+ return _shokIdleMs;
331
+ }
332
+ export function setShokInvocationStuckMsV2(n) {
333
+ _shokStuckMs = _shokPos(n, "shokInvocationStuckMs");
334
+ }
335
+ export function getShokInvocationStuckMsV2() {
336
+ return _shokStuckMs;
337
+ }
338
+ export function _resetStateSessionHooksV2() {
339
+ _shokPsV2.clear();
340
+ _shokIsV2.clear();
341
+ _shokMaxActive = 12;
342
+ _shokMaxPending = 25;
343
+ _shokIdleMs = 30 * 24 * 60 * 60 * 1000;
344
+ _shokStuckMs = 30 * 1000;
345
+ }
269
346
  export function registerShokProfileV2({ id, owner, event, metadata } = {}) {
270
- if (!id) throw new Error("shok profile id required"); if (!owner) throw new Error("shok profile owner required");
271
- if (_shokPsV2.has(id)) throw new Error(`shok profile ${id} already registered`);
347
+ if (!id) throw new Error("shok profile id required");
348
+ if (!owner) throw new Error("shok profile owner required");
349
+ if (_shokPsV2.has(id))
350
+ throw new Error(`shok profile ${id} already registered`);
272
351
  const now = Date.now();
273
- const p = { id, owner, event: event || "preTurn", status: SHOK_PROFILE_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, retiredAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
274
- _shokPsV2.set(id, p); return { ...p, metadata: { ...p.metadata } };
352
+ const p = {
353
+ id,
354
+ owner,
355
+ event: event || "preTurn",
356
+ status: SHOK_PROFILE_MATURITY_V2.PENDING,
357
+ createdAt: now,
358
+ updatedAt: now,
359
+ activatedAt: null,
360
+ retiredAt: null,
361
+ lastTouchedAt: now,
362
+ metadata: { ...(metadata || {}) },
363
+ };
364
+ _shokPsV2.set(id, p);
365
+ return { ...p, metadata: { ...p.metadata } };
366
+ }
367
+ function _shokCountActive(owner) {
368
+ let n = 0;
369
+ for (const p of _shokPsV2.values())
370
+ if (p.owner === owner && p.status === SHOK_PROFILE_MATURITY_V2.ACTIVE) n++;
371
+ return n;
275
372
  }
276
- function _shokCountActive(owner) { let n = 0; for (const p of _shokPsV2.values()) if (p.owner === owner && p.status === SHOK_PROFILE_MATURITY_V2.ACTIVE) n++; return n; }
277
373
  export function activateShokProfileV2(id) {
278
- const p = _shokPsV2.get(id); if (!p) throw new Error(`shok profile ${id} not found`);
374
+ const p = _shokPsV2.get(id);
375
+ if (!p) throw new Error(`shok profile ${id} not found`);
279
376
  _shokCheckP(p.status, SHOK_PROFILE_MATURITY_V2.ACTIVE);
280
377
  const recovery = p.status === SHOK_PROFILE_MATURITY_V2.DISABLED;
281
- if (!recovery && _shokCountActive(p.owner) >= _shokMaxActive) throw new Error(`max active shok profiles for owner ${p.owner} reached`);
282
- const now = Date.now(); p.status = SHOK_PROFILE_MATURITY_V2.ACTIVE; p.updatedAt = now; p.lastTouchedAt = now; if (!p.activatedAt) p.activatedAt = now;
378
+ if (!recovery && _shokCountActive(p.owner) >= _shokMaxActive)
379
+ throw new Error(`max active shok profiles for owner ${p.owner} reached`);
380
+ const now = Date.now();
381
+ p.status = SHOK_PROFILE_MATURITY_V2.ACTIVE;
382
+ p.updatedAt = now;
383
+ p.lastTouchedAt = now;
384
+ if (!p.activatedAt) p.activatedAt = now;
385
+ return { ...p, metadata: { ...p.metadata } };
386
+ }
387
+ export function disableShokProfileV2(id) {
388
+ const p = _shokPsV2.get(id);
389
+ if (!p) throw new Error(`shok profile ${id} not found`);
390
+ _shokCheckP(p.status, SHOK_PROFILE_MATURITY_V2.DISABLED);
391
+ p.status = SHOK_PROFILE_MATURITY_V2.DISABLED;
392
+ p.updatedAt = Date.now();
393
+ return { ...p, metadata: { ...p.metadata } };
394
+ }
395
+ export function retireShokProfileV2(id) {
396
+ const p = _shokPsV2.get(id);
397
+ if (!p) throw new Error(`shok profile ${id} not found`);
398
+ _shokCheckP(p.status, SHOK_PROFILE_MATURITY_V2.RETIRED);
399
+ const now = Date.now();
400
+ p.status = SHOK_PROFILE_MATURITY_V2.RETIRED;
401
+ p.updatedAt = now;
402
+ if (!p.retiredAt) p.retiredAt = now;
283
403
  return { ...p, metadata: { ...p.metadata } };
284
404
  }
285
- export function disableShokProfileV2(id) { const p = _shokPsV2.get(id); if (!p) throw new Error(`shok profile ${id} not found`); _shokCheckP(p.status, SHOK_PROFILE_MATURITY_V2.DISABLED); p.status = SHOK_PROFILE_MATURITY_V2.DISABLED; p.updatedAt = Date.now(); return { ...p, metadata: { ...p.metadata } }; }
286
- export function retireShokProfileV2(id) { const p = _shokPsV2.get(id); if (!p) throw new Error(`shok profile ${id} not found`); _shokCheckP(p.status, SHOK_PROFILE_MATURITY_V2.RETIRED); const now = Date.now(); p.status = SHOK_PROFILE_MATURITY_V2.RETIRED; p.updatedAt = now; if (!p.retiredAt) p.retiredAt = now; return { ...p, metadata: { ...p.metadata } }; }
287
- export function touchShokProfileV2(id) { const p = _shokPsV2.get(id); if (!p) throw new Error(`shok profile ${id} not found`); if (_shokPTerminal.has(p.status)) throw new Error(`cannot touch terminal shok profile ${id}`); const now = Date.now(); p.lastTouchedAt = now; p.updatedAt = now; return { ...p, metadata: { ...p.metadata } }; }
288
- export function getShokProfileV2(id) { const p = _shokPsV2.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
289
- export function listShokProfilesV2() { return [..._shokPsV2.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
290
- function _shokCountPending(profileId) { let n = 0; for (const i of _shokIsV2.values()) if (i.profileId === profileId && (i.status === SHOK_INVOCATION_LIFECYCLE_V2.QUEUED || i.status === SHOK_INVOCATION_LIFECYCLE_V2.RUNNING)) n++; return n; }
291
- export function createShokInvocationV2({ id, profileId, payload, metadata } = {}) {
292
- if (!id) throw new Error("shok invocation id required"); if (!profileId) throw new Error("shok invocation profileId required");
293
- if (_shokIsV2.has(id)) throw new Error(`shok invocation ${id} already exists`);
294
- if (!_shokPsV2.has(profileId)) throw new Error(`shok profile ${profileId} not found`);
295
- if (_shokCountPending(profileId) >= _shokMaxPending) throw new Error(`max pending shok invocations for profile ${profileId} reached`);
405
+ export function touchShokProfileV2(id) {
406
+ const p = _shokPsV2.get(id);
407
+ if (!p) throw new Error(`shok profile ${id} not found`);
408
+ if (_shokPTerminal.has(p.status))
409
+ throw new Error(`cannot touch terminal shok profile ${id}`);
296
410
  const now = Date.now();
297
- const i = { id, profileId, payload: payload || "", status: SHOK_INVOCATION_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
298
- _shokIsV2.set(id, i); return { ...i, metadata: { ...i.metadata } };
299
- }
300
- export function runningShokInvocationV2(id) { const i = _shokIsV2.get(id); if (!i) throw new Error(`shok invocation ${id} not found`); _shokCheckI(i.status, SHOK_INVOCATION_LIFECYCLE_V2.RUNNING); const now = Date.now(); i.status = SHOK_INVOCATION_LIFECYCLE_V2.RUNNING; i.updatedAt = now; if (!i.startedAt) i.startedAt = now; return { ...i, metadata: { ...i.metadata } }; }
301
- export function completeShokInvocationV2(id) { const i = _shokIsV2.get(id); if (!i) throw new Error(`shok invocation ${id} not found`); _shokCheckI(i.status, SHOK_INVOCATION_LIFECYCLE_V2.COMPLETED); const now = Date.now(); i.status = SHOK_INVOCATION_LIFECYCLE_V2.COMPLETED; i.updatedAt = now; if (!i.settledAt) i.settledAt = now; return { ...i, metadata: { ...i.metadata } }; }
302
- export function failShokInvocationV2(id, reason) { const i = _shokIsV2.get(id); if (!i) throw new Error(`shok invocation ${id} not found`); _shokCheckI(i.status, SHOK_INVOCATION_LIFECYCLE_V2.FAILED); const now = Date.now(); i.status = SHOK_INVOCATION_LIFECYCLE_V2.FAILED; i.updatedAt = now; if (!i.settledAt) i.settledAt = now; if (reason) i.metadata.failReason = String(reason); return { ...i, metadata: { ...i.metadata } }; }
303
- export function cancelShokInvocationV2(id, reason) { const i = _shokIsV2.get(id); if (!i) throw new Error(`shok invocation ${id} not found`); _shokCheckI(i.status, SHOK_INVOCATION_LIFECYCLE_V2.CANCELLED); const now = Date.now(); i.status = SHOK_INVOCATION_LIFECYCLE_V2.CANCELLED; i.updatedAt = now; if (!i.settledAt) i.settledAt = now; if (reason) i.metadata.cancelReason = String(reason); return { ...i, metadata: { ...i.metadata } }; }
304
- export function getShokInvocationV2(id) { const i = _shokIsV2.get(id); if (!i) return null; return { ...i, metadata: { ...i.metadata } }; }
305
- export function listShokInvocationsV2() { return [..._shokIsV2.values()].map((i) => ({ ...i, metadata: { ...i.metadata } })); }
306
- export function autoDisableIdleShokProfilesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _shokPsV2.values()) if (p.status === SHOK_PROFILE_MATURITY_V2.ACTIVE && (t - p.lastTouchedAt) >= _shokIdleMs) { p.status = SHOK_PROFILE_MATURITY_V2.DISABLED; p.updatedAt = t; flipped.push(p.id); } return { flipped, count: flipped.length }; }
307
- export function autoFailStuckShokInvocationsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const i of _shokIsV2.values()) if (i.status === SHOK_INVOCATION_LIFECYCLE_V2.RUNNING && i.startedAt != null && (t - i.startedAt) >= _shokStuckMs) { i.status = SHOK_INVOCATION_LIFECYCLE_V2.FAILED; i.updatedAt = t; if (!i.settledAt) i.settledAt = t; i.metadata.failReason = "auto-fail-stuck"; flipped.push(i.id); } return { flipped, count: flipped.length }; }
411
+ p.lastTouchedAt = now;
412
+ p.updatedAt = now;
413
+ return { ...p, metadata: { ...p.metadata } };
414
+ }
415
+ export function getShokProfileV2(id) {
416
+ const p = _shokPsV2.get(id);
417
+ if (!p) return null;
418
+ return { ...p, metadata: { ...p.metadata } };
419
+ }
420
+ export function listShokProfilesV2() {
421
+ return [..._shokPsV2.values()].map((p) => ({
422
+ ...p,
423
+ metadata: { ...p.metadata },
424
+ }));
425
+ }
426
+ function _shokCountPending(profileId) {
427
+ let n = 0;
428
+ for (const i of _shokIsV2.values())
429
+ if (
430
+ i.profileId === profileId &&
431
+ (i.status === SHOK_INVOCATION_LIFECYCLE_V2.QUEUED ||
432
+ i.status === SHOK_INVOCATION_LIFECYCLE_V2.RUNNING)
433
+ )
434
+ n++;
435
+ return n;
436
+ }
437
+ export function createShokInvocationV2({
438
+ id,
439
+ profileId,
440
+ payload,
441
+ metadata,
442
+ } = {}) {
443
+ if (!id) throw new Error("shok invocation id required");
444
+ if (!profileId) throw new Error("shok invocation profileId required");
445
+ if (_shokIsV2.has(id))
446
+ throw new Error(`shok invocation ${id} already exists`);
447
+ if (!_shokPsV2.has(profileId))
448
+ throw new Error(`shok profile ${profileId} not found`);
449
+ if (_shokCountPending(profileId) >= _shokMaxPending)
450
+ throw new Error(
451
+ `max pending shok invocations for profile ${profileId} reached`,
452
+ );
453
+ const now = Date.now();
454
+ const i = {
455
+ id,
456
+ profileId,
457
+ payload: payload || "",
458
+ status: SHOK_INVOCATION_LIFECYCLE_V2.QUEUED,
459
+ createdAt: now,
460
+ updatedAt: now,
461
+ startedAt: null,
462
+ settledAt: null,
463
+ metadata: { ...(metadata || {}) },
464
+ };
465
+ _shokIsV2.set(id, i);
466
+ return { ...i, metadata: { ...i.metadata } };
467
+ }
468
+ export function runningShokInvocationV2(id) {
469
+ const i = _shokIsV2.get(id);
470
+ if (!i) throw new Error(`shok invocation ${id} not found`);
471
+ _shokCheckI(i.status, SHOK_INVOCATION_LIFECYCLE_V2.RUNNING);
472
+ const now = Date.now();
473
+ i.status = SHOK_INVOCATION_LIFECYCLE_V2.RUNNING;
474
+ i.updatedAt = now;
475
+ if (!i.startedAt) i.startedAt = now;
476
+ return { ...i, metadata: { ...i.metadata } };
477
+ }
478
+ export function completeShokInvocationV2(id) {
479
+ const i = _shokIsV2.get(id);
480
+ if (!i) throw new Error(`shok invocation ${id} not found`);
481
+ _shokCheckI(i.status, SHOK_INVOCATION_LIFECYCLE_V2.COMPLETED);
482
+ const now = Date.now();
483
+ i.status = SHOK_INVOCATION_LIFECYCLE_V2.COMPLETED;
484
+ i.updatedAt = now;
485
+ if (!i.settledAt) i.settledAt = now;
486
+ return { ...i, metadata: { ...i.metadata } };
487
+ }
488
+ export function failShokInvocationV2(id, reason) {
489
+ const i = _shokIsV2.get(id);
490
+ if (!i) throw new Error(`shok invocation ${id} not found`);
491
+ _shokCheckI(i.status, SHOK_INVOCATION_LIFECYCLE_V2.FAILED);
492
+ const now = Date.now();
493
+ i.status = SHOK_INVOCATION_LIFECYCLE_V2.FAILED;
494
+ i.updatedAt = now;
495
+ if (!i.settledAt) i.settledAt = now;
496
+ if (reason) i.metadata.failReason = String(reason);
497
+ return { ...i, metadata: { ...i.metadata } };
498
+ }
499
+ export function cancelShokInvocationV2(id, reason) {
500
+ const i = _shokIsV2.get(id);
501
+ if (!i) throw new Error(`shok invocation ${id} not found`);
502
+ _shokCheckI(i.status, SHOK_INVOCATION_LIFECYCLE_V2.CANCELLED);
503
+ const now = Date.now();
504
+ i.status = SHOK_INVOCATION_LIFECYCLE_V2.CANCELLED;
505
+ i.updatedAt = now;
506
+ if (!i.settledAt) i.settledAt = now;
507
+ if (reason) i.metadata.cancelReason = String(reason);
508
+ return { ...i, metadata: { ...i.metadata } };
509
+ }
510
+ export function getShokInvocationV2(id) {
511
+ const i = _shokIsV2.get(id);
512
+ if (!i) return null;
513
+ return { ...i, metadata: { ...i.metadata } };
514
+ }
515
+ export function listShokInvocationsV2() {
516
+ return [..._shokIsV2.values()].map((i) => ({
517
+ ...i,
518
+ metadata: { ...i.metadata },
519
+ }));
520
+ }
521
+ export function autoDisableIdleShokProfilesV2({ now } = {}) {
522
+ const t = now ?? Date.now();
523
+ const flipped = [];
524
+ for (const p of _shokPsV2.values())
525
+ if (
526
+ p.status === SHOK_PROFILE_MATURITY_V2.ACTIVE &&
527
+ t - p.lastTouchedAt >= _shokIdleMs
528
+ ) {
529
+ p.status = SHOK_PROFILE_MATURITY_V2.DISABLED;
530
+ p.updatedAt = t;
531
+ flipped.push(p.id);
532
+ }
533
+ return { flipped, count: flipped.length };
534
+ }
535
+ export function autoFailStuckShokInvocationsV2({ now } = {}) {
536
+ const t = now ?? Date.now();
537
+ const flipped = [];
538
+ for (const i of _shokIsV2.values())
539
+ if (
540
+ i.status === SHOK_INVOCATION_LIFECYCLE_V2.RUNNING &&
541
+ i.startedAt != null &&
542
+ t - i.startedAt >= _shokStuckMs
543
+ ) {
544
+ i.status = SHOK_INVOCATION_LIFECYCLE_V2.FAILED;
545
+ i.updatedAt = t;
546
+ if (!i.settledAt) i.settledAt = t;
547
+ i.metadata.failReason = "auto-fail-stuck";
548
+ flipped.push(i.id);
549
+ }
550
+ return { flipped, count: flipped.length };
551
+ }
308
552
  export function getSessionHooksGovStatsV2() {
309
- const profilesByStatus = {}; for (const v of Object.values(SHOK_PROFILE_MATURITY_V2)) profilesByStatus[v] = 0; for (const p of _shokPsV2.values()) profilesByStatus[p.status]++;
310
- const invocationsByStatus = {}; for (const v of Object.values(SHOK_INVOCATION_LIFECYCLE_V2)) invocationsByStatus[v] = 0; for (const i of _shokIsV2.values()) invocationsByStatus[i.status]++;
311
- return { totalShokProfilesV2: _shokPsV2.size, totalShokInvocationsV2: _shokIsV2.size, maxActiveShokProfilesPerOwner: _shokMaxActive, maxPendingShokInvocationsPerProfile: _shokMaxPending, shokProfileIdleMs: _shokIdleMs, shokInvocationStuckMs: _shokStuckMs, profilesByStatus, invocationsByStatus };
553
+ const profilesByStatus = {};
554
+ for (const v of Object.values(SHOK_PROFILE_MATURITY_V2))
555
+ profilesByStatus[v] = 0;
556
+ for (const p of _shokPsV2.values()) profilesByStatus[p.status]++;
557
+ const invocationsByStatus = {};
558
+ for (const v of Object.values(SHOK_INVOCATION_LIFECYCLE_V2))
559
+ invocationsByStatus[v] = 0;
560
+ for (const i of _shokIsV2.values()) invocationsByStatus[i.status]++;
561
+ return {
562
+ totalShokProfilesV2: _shokPsV2.size,
563
+ totalShokInvocationsV2: _shokIsV2.size,
564
+ maxActiveShokProfilesPerOwner: _shokMaxActive,
565
+ maxPendingShokInvocationsPerProfile: _shokMaxPending,
566
+ shokProfileIdleMs: _shokIdleMs,
567
+ shokInvocationStuckMs: _shokStuckMs,
568
+ profilesByStatus,
569
+ invocationsByStatus,
570
+ };
312
571
  }