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
@@ -124,25 +124,62 @@ export async function consolidateJsonlSession(sessionId, options = {}) {
124
124
  );
125
125
  }
126
126
 
127
-
128
127
  // ===== V2 Surface: Session Consolidator governance overlay (CLI v0.134.0) =====
129
128
  export const CONSOL_PROFILE_MATURITY_V2 = Object.freeze({
130
- PENDING: "pending", ACTIVE: "active", PAUSED: "paused", ARCHIVED: "archived",
129
+ PENDING: "pending",
130
+ ACTIVE: "active",
131
+ PAUSED: "paused",
132
+ ARCHIVED: "archived",
131
133
  });
132
134
  export const CONSOL_JOB_LIFECYCLE_V2 = Object.freeze({
133
- QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
135
+ QUEUED: "queued",
136
+ RUNNING: "running",
137
+ COMPLETED: "completed",
138
+ FAILED: "failed",
139
+ CANCELLED: "cancelled",
134
140
  });
135
141
 
136
142
  const _scProfTrans = new Map([
137
- [CONSOL_PROFILE_MATURITY_V2.PENDING, new Set([CONSOL_PROFILE_MATURITY_V2.ACTIVE, CONSOL_PROFILE_MATURITY_V2.ARCHIVED])],
138
- [CONSOL_PROFILE_MATURITY_V2.ACTIVE, new Set([CONSOL_PROFILE_MATURITY_V2.PAUSED, CONSOL_PROFILE_MATURITY_V2.ARCHIVED])],
139
- [CONSOL_PROFILE_MATURITY_V2.PAUSED, new Set([CONSOL_PROFILE_MATURITY_V2.ACTIVE, CONSOL_PROFILE_MATURITY_V2.ARCHIVED])],
143
+ [
144
+ CONSOL_PROFILE_MATURITY_V2.PENDING,
145
+ new Set([
146
+ CONSOL_PROFILE_MATURITY_V2.ACTIVE,
147
+ CONSOL_PROFILE_MATURITY_V2.ARCHIVED,
148
+ ]),
149
+ ],
150
+ [
151
+ CONSOL_PROFILE_MATURITY_V2.ACTIVE,
152
+ new Set([
153
+ CONSOL_PROFILE_MATURITY_V2.PAUSED,
154
+ CONSOL_PROFILE_MATURITY_V2.ARCHIVED,
155
+ ]),
156
+ ],
157
+ [
158
+ CONSOL_PROFILE_MATURITY_V2.PAUSED,
159
+ new Set([
160
+ CONSOL_PROFILE_MATURITY_V2.ACTIVE,
161
+ CONSOL_PROFILE_MATURITY_V2.ARCHIVED,
162
+ ]),
163
+ ],
140
164
  [CONSOL_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
141
165
  ]);
142
166
  const _scProfTerminal = new Set([CONSOL_PROFILE_MATURITY_V2.ARCHIVED]);
143
167
  const _scJobTrans = new Map([
144
- [CONSOL_JOB_LIFECYCLE_V2.QUEUED, new Set([CONSOL_JOB_LIFECYCLE_V2.RUNNING, CONSOL_JOB_LIFECYCLE_V2.CANCELLED])],
145
- [CONSOL_JOB_LIFECYCLE_V2.RUNNING, new Set([CONSOL_JOB_LIFECYCLE_V2.COMPLETED, CONSOL_JOB_LIFECYCLE_V2.FAILED, CONSOL_JOB_LIFECYCLE_V2.CANCELLED])],
168
+ [
169
+ CONSOL_JOB_LIFECYCLE_V2.QUEUED,
170
+ new Set([
171
+ CONSOL_JOB_LIFECYCLE_V2.RUNNING,
172
+ CONSOL_JOB_LIFECYCLE_V2.CANCELLED,
173
+ ]),
174
+ ],
175
+ [
176
+ CONSOL_JOB_LIFECYCLE_V2.RUNNING,
177
+ new Set([
178
+ CONSOL_JOB_LIFECYCLE_V2.COMPLETED,
179
+ CONSOL_JOB_LIFECYCLE_V2.FAILED,
180
+ CONSOL_JOB_LIFECYCLE_V2.CANCELLED,
181
+ ]),
182
+ ],
146
183
  [CONSOL_JOB_LIFECYCLE_V2.COMPLETED, new Set()],
147
184
  [CONSOL_JOB_LIFECYCLE_V2.FAILED, new Set()],
148
185
  [CONSOL_JOB_LIFECYCLE_V2.CANCELLED, new Set()],
@@ -155,76 +192,286 @@ let _scMaxPendingPerProfile = 12;
155
192
  let _scProfileIdleMs = 7 * 24 * 60 * 60 * 1000;
156
193
  let _scJobStuckMs = 10 * 60 * 1000;
157
194
 
158
- function _scPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
195
+ function _scPos(n, lbl) {
196
+ const v = Math.floor(Number(n));
197
+ if (!Number.isFinite(v) || v <= 0)
198
+ throw new Error(`${lbl} must be positive integer`);
199
+ return v;
200
+ }
159
201
 
160
- export function setMaxActiveConsolProfilesPerOwnerV2(n) { _scMaxActivePerOwner = _scPos(n, "maxActiveConsolProfilesPerOwner"); }
161
- export function getMaxActiveConsolProfilesPerOwnerV2() { return _scMaxActivePerOwner; }
162
- export function setMaxPendingConsolJobsPerProfileV2(n) { _scMaxPendingPerProfile = _scPos(n, "maxPendingConsolJobsPerProfile"); }
163
- export function getMaxPendingConsolJobsPerProfileV2() { return _scMaxPendingPerProfile; }
164
- export function setConsolProfileIdleMsV2(n) { _scProfileIdleMs = _scPos(n, "consolProfileIdleMs"); }
165
- export function getConsolProfileIdleMsV2() { return _scProfileIdleMs; }
166
- export function setConsolJobStuckMsV2(n) { _scJobStuckMs = _scPos(n, "consolJobStuckMs"); }
167
- export function getConsolJobStuckMsV2() { return _scJobStuckMs; }
202
+ export function setMaxActiveConsolProfilesPerOwnerV2(n) {
203
+ _scMaxActivePerOwner = _scPos(n, "maxActiveConsolProfilesPerOwner");
204
+ }
205
+ export function getMaxActiveConsolProfilesPerOwnerV2() {
206
+ return _scMaxActivePerOwner;
207
+ }
208
+ export function setMaxPendingConsolJobsPerProfileV2(n) {
209
+ _scMaxPendingPerProfile = _scPos(n, "maxPendingConsolJobsPerProfile");
210
+ }
211
+ export function getMaxPendingConsolJobsPerProfileV2() {
212
+ return _scMaxPendingPerProfile;
213
+ }
214
+ export function setConsolProfileIdleMsV2(n) {
215
+ _scProfileIdleMs = _scPos(n, "consolProfileIdleMs");
216
+ }
217
+ export function getConsolProfileIdleMsV2() {
218
+ return _scProfileIdleMs;
219
+ }
220
+ export function setConsolJobStuckMsV2(n) {
221
+ _scJobStuckMs = _scPos(n, "consolJobStuckMs");
222
+ }
223
+ export function getConsolJobStuckMsV2() {
224
+ return _scJobStuckMs;
225
+ }
168
226
 
169
227
  export function _resetStateSessionConsolidatorV2() {
170
- _scProfiles.clear(); _scJobs.clear();
171
- _scMaxActivePerOwner = 8; _scMaxPendingPerProfile = 12;
172
- _scProfileIdleMs = 7 * 24 * 60 * 60 * 1000; _scJobStuckMs = 10 * 60 * 1000;
228
+ _scProfiles.clear();
229
+ _scJobs.clear();
230
+ _scMaxActivePerOwner = 8;
231
+ _scMaxPendingPerProfile = 12;
232
+ _scProfileIdleMs = 7 * 24 * 60 * 60 * 1000;
233
+ _scJobStuckMs = 10 * 60 * 1000;
173
234
  }
174
235
 
175
236
  export function registerConsolProfileV2({ id, owner, scope, metadata } = {}) {
176
237
  if (!id || typeof id !== "string") throw new Error("id is required");
177
238
  if (!owner || typeof owner !== "string") throw new Error("owner is required");
178
- if (_scProfiles.has(id)) throw new Error(`consol profile ${id} already registered`);
239
+ if (_scProfiles.has(id))
240
+ throw new Error(`consol profile ${id} already registered`);
179
241
  const now = Date.now();
180
- const p = { id, owner, scope: scope || "agent", status: CONSOL_PROFILE_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
242
+ const p = {
243
+ id,
244
+ owner,
245
+ scope: scope || "agent",
246
+ status: CONSOL_PROFILE_MATURITY_V2.PENDING,
247
+ createdAt: now,
248
+ updatedAt: now,
249
+ activatedAt: null,
250
+ archivedAt: null,
251
+ lastTouchedAt: now,
252
+ metadata: { ...(metadata || {}) },
253
+ };
181
254
  _scProfiles.set(id, p);
182
255
  return { ...p, metadata: { ...p.metadata } };
183
256
  }
184
- function _scCheckP(from, to) { const a = _scProfTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid consol profile transition ${from} → ${to}`); }
185
- function _scCountActive(owner) { let n = 0; for (const p of _scProfiles.values()) if (p.owner === owner && p.status === CONSOL_PROFILE_MATURITY_V2.ACTIVE) n++; return n; }
257
+ function _scCheckP(from, to) {
258
+ const a = _scProfTrans.get(from);
259
+ if (!a || !a.has(to))
260
+ throw new Error(`invalid consol profile transition ${from} → ${to}`);
261
+ }
262
+ function _scCountActive(owner) {
263
+ let n = 0;
264
+ for (const p of _scProfiles.values())
265
+ if (p.owner === owner && p.status === CONSOL_PROFILE_MATURITY_V2.ACTIVE)
266
+ n++;
267
+ return n;
268
+ }
186
269
 
187
270
  export function activateConsolProfileV2(id) {
188
- const p = _scProfiles.get(id); if (!p) throw new Error(`consol profile ${id} not found`);
271
+ const p = _scProfiles.get(id);
272
+ if (!p) throw new Error(`consol profile ${id} not found`);
189
273
  _scCheckP(p.status, CONSOL_PROFILE_MATURITY_V2.ACTIVE);
190
274
  const recovery = p.status === CONSOL_PROFILE_MATURITY_V2.PAUSED;
191
- if (!recovery) { const a = _scCountActive(p.owner); if (a >= _scMaxActivePerOwner) throw new Error(`max active consol profiles per owner (${_scMaxActivePerOwner}) reached for ${p.owner}`); }
192
- const now = Date.now(); p.status = CONSOL_PROFILE_MATURITY_V2.ACTIVE; p.updatedAt = now; p.lastTouchedAt = now; if (!p.activatedAt) p.activatedAt = now;
275
+ if (!recovery) {
276
+ const a = _scCountActive(p.owner);
277
+ if (a >= _scMaxActivePerOwner)
278
+ throw new Error(
279
+ `max active consol profiles per owner (${_scMaxActivePerOwner}) reached for ${p.owner}`,
280
+ );
281
+ }
282
+ const now = Date.now();
283
+ p.status = CONSOL_PROFILE_MATURITY_V2.ACTIVE;
284
+ p.updatedAt = now;
285
+ p.lastTouchedAt = now;
286
+ if (!p.activatedAt) p.activatedAt = now;
287
+ return { ...p, metadata: { ...p.metadata } };
288
+ }
289
+ export function pauseConsolProfileV2(id) {
290
+ const p = _scProfiles.get(id);
291
+ if (!p) throw new Error(`consol profile ${id} not found`);
292
+ _scCheckP(p.status, CONSOL_PROFILE_MATURITY_V2.PAUSED);
293
+ p.status = CONSOL_PROFILE_MATURITY_V2.PAUSED;
294
+ p.updatedAt = Date.now();
295
+ return { ...p, metadata: { ...p.metadata } };
296
+ }
297
+ export function archiveConsolProfileV2(id) {
298
+ const p = _scProfiles.get(id);
299
+ if (!p) throw new Error(`consol profile ${id} not found`);
300
+ _scCheckP(p.status, CONSOL_PROFILE_MATURITY_V2.ARCHIVED);
301
+ const now = Date.now();
302
+ p.status = CONSOL_PROFILE_MATURITY_V2.ARCHIVED;
303
+ p.updatedAt = now;
304
+ if (!p.archivedAt) p.archivedAt = now;
305
+ return { ...p, metadata: { ...p.metadata } };
306
+ }
307
+ export function touchConsolProfileV2(id) {
308
+ const p = _scProfiles.get(id);
309
+ if (!p) throw new Error(`consol profile ${id} not found`);
310
+ if (_scProfTerminal.has(p.status))
311
+ throw new Error(`cannot touch terminal consol profile ${id}`);
312
+ const now = Date.now();
313
+ p.lastTouchedAt = now;
314
+ p.updatedAt = now;
315
+ return { ...p, metadata: { ...p.metadata } };
316
+ }
317
+ export function getConsolProfileV2(id) {
318
+ const p = _scProfiles.get(id);
319
+ if (!p) return null;
193
320
  return { ...p, metadata: { ...p.metadata } };
194
321
  }
195
- export function pauseConsolProfileV2(id) { const p = _scProfiles.get(id); if (!p) throw new Error(`consol profile ${id} not found`); _scCheckP(p.status, CONSOL_PROFILE_MATURITY_V2.PAUSED); p.status = CONSOL_PROFILE_MATURITY_V2.PAUSED; p.updatedAt = Date.now(); return { ...p, metadata: { ...p.metadata } }; }
196
- export function archiveConsolProfileV2(id) { const p = _scProfiles.get(id); if (!p) throw new Error(`consol profile ${id} not found`); _scCheckP(p.status, CONSOL_PROFILE_MATURITY_V2.ARCHIVED); const now = Date.now(); p.status = CONSOL_PROFILE_MATURITY_V2.ARCHIVED; p.updatedAt = now; if (!p.archivedAt) p.archivedAt = now; return { ...p, metadata: { ...p.metadata } }; }
197
- export function touchConsolProfileV2(id) { const p = _scProfiles.get(id); if (!p) throw new Error(`consol profile ${id} not found`); if (_scProfTerminal.has(p.status)) throw new Error(`cannot touch terminal consol profile ${id}`); const now = Date.now(); p.lastTouchedAt = now; p.updatedAt = now; return { ...p, metadata: { ...p.metadata } }; }
198
- export function getConsolProfileV2(id) { const p = _scProfiles.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
199
- export function listConsolProfilesV2() { return [..._scProfiles.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
322
+ export function listConsolProfilesV2() {
323
+ return [..._scProfiles.values()].map((p) => ({
324
+ ...p,
325
+ metadata: { ...p.metadata },
326
+ }));
327
+ }
200
328
 
201
- function _scCountPending(pid) { let n = 0; for (const j of _scJobs.values()) if (j.profileId === pid && (j.status === CONSOL_JOB_LIFECYCLE_V2.QUEUED || j.status === CONSOL_JOB_LIFECYCLE_V2.RUNNING)) n++; return n; }
329
+ function _scCountPending(pid) {
330
+ let n = 0;
331
+ for (const j of _scJobs.values())
332
+ if (
333
+ j.profileId === pid &&
334
+ (j.status === CONSOL_JOB_LIFECYCLE_V2.QUEUED ||
335
+ j.status === CONSOL_JOB_LIFECYCLE_V2.RUNNING)
336
+ )
337
+ n++;
338
+ return n;
339
+ }
202
340
 
203
341
  export function createConsolJobV2({ id, profileId, sessionId, metadata } = {}) {
204
342
  if (!id || typeof id !== "string") throw new Error("id is required");
205
- if (!profileId || typeof profileId !== "string") throw new Error("profileId is required");
343
+ if (!profileId || typeof profileId !== "string")
344
+ throw new Error("profileId is required");
206
345
  if (_scJobs.has(id)) throw new Error(`consol job ${id} already exists`);
207
- if (!_scProfiles.has(profileId)) throw new Error(`consol profile ${profileId} not found`);
346
+ if (!_scProfiles.has(profileId))
347
+ throw new Error(`consol profile ${profileId} not found`);
208
348
  const pending = _scCountPending(profileId);
209
- if (pending >= _scMaxPendingPerProfile) throw new Error(`max pending consol jobs per profile (${_scMaxPendingPerProfile}) reached for ${profileId}`);
349
+ if (pending >= _scMaxPendingPerProfile)
350
+ throw new Error(
351
+ `max pending consol jobs per profile (${_scMaxPendingPerProfile}) reached for ${profileId}`,
352
+ );
210
353
  const now = Date.now();
211
- const j = { id, profileId, sessionId: sessionId || null, status: CONSOL_JOB_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
354
+ const j = {
355
+ id,
356
+ profileId,
357
+ sessionId: sessionId || null,
358
+ status: CONSOL_JOB_LIFECYCLE_V2.QUEUED,
359
+ createdAt: now,
360
+ updatedAt: now,
361
+ startedAt: null,
362
+ settledAt: null,
363
+ metadata: { ...(metadata || {}) },
364
+ };
212
365
  _scJobs.set(id, j);
213
366
  return { ...j, metadata: { ...j.metadata } };
214
367
  }
215
- function _scCheckJ(from, to) { const a = _scJobTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid consol job transition ${from} → ${to}`); }
216
- export function startConsolJobV2(id) { const j = _scJobs.get(id); if (!j) throw new Error(`consol job ${id} not found`); _scCheckJ(j.status, CONSOL_JOB_LIFECYCLE_V2.RUNNING); const now = Date.now(); j.status = CONSOL_JOB_LIFECYCLE_V2.RUNNING; j.updatedAt = now; if (!j.startedAt) j.startedAt = now; return { ...j, metadata: { ...j.metadata } }; }
217
- export function completeConsolJobV2(id) { const j = _scJobs.get(id); if (!j) throw new Error(`consol job ${id} not found`); _scCheckJ(j.status, CONSOL_JOB_LIFECYCLE_V2.COMPLETED); const now = Date.now(); j.status = CONSOL_JOB_LIFECYCLE_V2.COMPLETED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; return { ...j, metadata: { ...j.metadata } }; }
218
- export function failConsolJobV2(id, reason) { const j = _scJobs.get(id); if (!j) throw new Error(`consol job ${id} not found`); _scCheckJ(j.status, CONSOL_JOB_LIFECYCLE_V2.FAILED); const now = Date.now(); j.status = CONSOL_JOB_LIFECYCLE_V2.FAILED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; if (reason) j.metadata.failReason = String(reason); return { ...j, metadata: { ...j.metadata } }; }
219
- export function cancelConsolJobV2(id, reason) { const j = _scJobs.get(id); if (!j) throw new Error(`consol job ${id} not found`); _scCheckJ(j.status, CONSOL_JOB_LIFECYCLE_V2.CANCELLED); const now = Date.now(); j.status = CONSOL_JOB_LIFECYCLE_V2.CANCELLED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; if (reason) j.metadata.cancelReason = String(reason); return { ...j, metadata: { ...j.metadata } }; }
220
- export function getConsolJobV2(id) { const j = _scJobs.get(id); if (!j) return null; return { ...j, metadata: { ...j.metadata } }; }
221
- export function listConsolJobsV2() { return [..._scJobs.values()].map((j) => ({ ...j, metadata: { ...j.metadata } })); }
368
+ function _scCheckJ(from, to) {
369
+ const a = _scJobTrans.get(from);
370
+ if (!a || !a.has(to))
371
+ throw new Error(`invalid consol job transition ${from} ${to}`);
372
+ }
373
+ export function startConsolJobV2(id) {
374
+ const j = _scJobs.get(id);
375
+ if (!j) throw new Error(`consol job ${id} not found`);
376
+ _scCheckJ(j.status, CONSOL_JOB_LIFECYCLE_V2.RUNNING);
377
+ const now = Date.now();
378
+ j.status = CONSOL_JOB_LIFECYCLE_V2.RUNNING;
379
+ j.updatedAt = now;
380
+ if (!j.startedAt) j.startedAt = now;
381
+ return { ...j, metadata: { ...j.metadata } };
382
+ }
383
+ export function completeConsolJobV2(id) {
384
+ const j = _scJobs.get(id);
385
+ if (!j) throw new Error(`consol job ${id} not found`);
386
+ _scCheckJ(j.status, CONSOL_JOB_LIFECYCLE_V2.COMPLETED);
387
+ const now = Date.now();
388
+ j.status = CONSOL_JOB_LIFECYCLE_V2.COMPLETED;
389
+ j.updatedAt = now;
390
+ if (!j.settledAt) j.settledAt = now;
391
+ return { ...j, metadata: { ...j.metadata } };
392
+ }
393
+ export function failConsolJobV2(id, reason) {
394
+ const j = _scJobs.get(id);
395
+ if (!j) throw new Error(`consol job ${id} not found`);
396
+ _scCheckJ(j.status, CONSOL_JOB_LIFECYCLE_V2.FAILED);
397
+ const now = Date.now();
398
+ j.status = CONSOL_JOB_LIFECYCLE_V2.FAILED;
399
+ j.updatedAt = now;
400
+ if (!j.settledAt) j.settledAt = now;
401
+ if (reason) j.metadata.failReason = String(reason);
402
+ return { ...j, metadata: { ...j.metadata } };
403
+ }
404
+ export function cancelConsolJobV2(id, reason) {
405
+ const j = _scJobs.get(id);
406
+ if (!j) throw new Error(`consol job ${id} not found`);
407
+ _scCheckJ(j.status, CONSOL_JOB_LIFECYCLE_V2.CANCELLED);
408
+ const now = Date.now();
409
+ j.status = CONSOL_JOB_LIFECYCLE_V2.CANCELLED;
410
+ j.updatedAt = now;
411
+ if (!j.settledAt) j.settledAt = now;
412
+ if (reason) j.metadata.cancelReason = String(reason);
413
+ return { ...j, metadata: { ...j.metadata } };
414
+ }
415
+ export function getConsolJobV2(id) {
416
+ const j = _scJobs.get(id);
417
+ if (!j) return null;
418
+ return { ...j, metadata: { ...j.metadata } };
419
+ }
420
+ export function listConsolJobsV2() {
421
+ return [..._scJobs.values()].map((j) => ({
422
+ ...j,
423
+ metadata: { ...j.metadata },
424
+ }));
425
+ }
222
426
 
223
- export function autoPauseIdleConsolProfilesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _scProfiles.values()) if (p.status === CONSOL_PROFILE_MATURITY_V2.ACTIVE && (t - p.lastTouchedAt) >= _scProfileIdleMs) { p.status = CONSOL_PROFILE_MATURITY_V2.PAUSED; p.updatedAt = t; flipped.push(p.id); } return { flipped, count: flipped.length }; }
224
- export function autoFailStuckConsolJobsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const j of _scJobs.values()) if (j.status === CONSOL_JOB_LIFECYCLE_V2.RUNNING && j.startedAt != null && (t - j.startedAt) >= _scJobStuckMs) { j.status = CONSOL_JOB_LIFECYCLE_V2.FAILED; j.updatedAt = t; if (!j.settledAt) j.settledAt = t; j.metadata.failReason = "auto-fail-stuck"; flipped.push(j.id); } return { flipped, count: flipped.length }; }
427
+ export function autoPauseIdleConsolProfilesV2({ now } = {}) {
428
+ const t = now ?? Date.now();
429
+ const flipped = [];
430
+ for (const p of _scProfiles.values())
431
+ if (
432
+ p.status === CONSOL_PROFILE_MATURITY_V2.ACTIVE &&
433
+ t - p.lastTouchedAt >= _scProfileIdleMs
434
+ ) {
435
+ p.status = CONSOL_PROFILE_MATURITY_V2.PAUSED;
436
+ p.updatedAt = t;
437
+ flipped.push(p.id);
438
+ }
439
+ return { flipped, count: flipped.length };
440
+ }
441
+ export function autoFailStuckConsolJobsV2({ now } = {}) {
442
+ const t = now ?? Date.now();
443
+ const flipped = [];
444
+ for (const j of _scJobs.values())
445
+ if (
446
+ j.status === CONSOL_JOB_LIFECYCLE_V2.RUNNING &&
447
+ j.startedAt != null &&
448
+ t - j.startedAt >= _scJobStuckMs
449
+ ) {
450
+ j.status = CONSOL_JOB_LIFECYCLE_V2.FAILED;
451
+ j.updatedAt = t;
452
+ if (!j.settledAt) j.settledAt = t;
453
+ j.metadata.failReason = "auto-fail-stuck";
454
+ flipped.push(j.id);
455
+ }
456
+ return { flipped, count: flipped.length };
457
+ }
225
458
 
226
459
  export function getSessionConsolidatorStatsV2() {
227
- const profilesByStatus = {}; for (const s of Object.values(CONSOL_PROFILE_MATURITY_V2)) profilesByStatus[s] = 0; for (const p of _scProfiles.values()) profilesByStatus[p.status]++;
228
- const jobsByStatus = {}; for (const s of Object.values(CONSOL_JOB_LIFECYCLE_V2)) jobsByStatus[s] = 0; for (const j of _scJobs.values()) jobsByStatus[j.status]++;
229
- return { totalProfilesV2: _scProfiles.size, totalJobsV2: _scJobs.size, maxActiveConsolProfilesPerOwner: _scMaxActivePerOwner, maxPendingConsolJobsPerProfile: _scMaxPendingPerProfile, consolProfileIdleMs: _scProfileIdleMs, consolJobStuckMs: _scJobStuckMs, profilesByStatus, jobsByStatus };
460
+ const profilesByStatus = {};
461
+ for (const s of Object.values(CONSOL_PROFILE_MATURITY_V2))
462
+ profilesByStatus[s] = 0;
463
+ for (const p of _scProfiles.values()) profilesByStatus[p.status]++;
464
+ const jobsByStatus = {};
465
+ for (const s of Object.values(CONSOL_JOB_LIFECYCLE_V2)) jobsByStatus[s] = 0;
466
+ for (const j of _scJobs.values()) jobsByStatus[j.status]++;
467
+ return {
468
+ totalProfilesV2: _scProfiles.size,
469
+ totalJobsV2: _scJobs.size,
470
+ maxActiveConsolProfilesPerOwner: _scMaxActivePerOwner,
471
+ maxPendingConsolJobsPerProfile: _scMaxPendingPerProfile,
472
+ consolProfileIdleMs: _scProfileIdleMs,
473
+ consolJobStuckMs: _scJobStuckMs,
474
+ profilesByStatus,
475
+ jobsByStatus,
476
+ };
230
477
  }