chainlesschain 0.152.0 → 0.156.2

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 (80) hide show
  1. package/README.md +52 -3
  2. package/package.json +1 -1
  3. package/src/commands/a2a.js +201 -0
  4. package/src/commands/agent.js +1250 -0
  5. package/src/commands/chat.js +605 -0
  6. package/src/commands/cli-anything.js +426 -0
  7. package/src/commands/compliance.js +412 -0
  8. package/src/commands/config.js +213 -0
  9. package/src/commands/cowork.js +1463 -0
  10. package/src/commands/crosschain.js +203 -0
  11. package/src/commands/dao.js +203 -0
  12. package/src/commands/economy.js +199 -0
  13. package/src/commands/encrypt.js +201 -0
  14. package/src/commands/evolution.js +199 -0
  15. package/src/commands/evomap.js +625 -0
  16. package/src/commands/hmemory.js +203 -0
  17. package/src/commands/inference.js +207 -0
  18. package/src/commands/kg.js +195 -0
  19. package/src/commands/llm.js +209 -0
  20. package/src/commands/memory.js +203 -0
  21. package/src/commands/orchestrate.js +406 -0
  22. package/src/commands/pipeline.js +199 -0
  23. package/src/commands/planmode.js +426 -0
  24. package/src/commands/plugin.js +209 -0
  25. package/src/commands/services.js +207 -0
  26. package/src/commands/setup.js +205 -0
  27. package/src/commands/skill.js +207 -0
  28. package/src/commands/start.js +209 -0
  29. package/src/commands/stream.js +213 -0
  30. package/src/commands/ui.js +225 -0
  31. package/src/commands/workflow.js +209 -0
  32. package/src/index.js +112 -0
  33. package/src/lib/a2a-protocol.js +332 -0
  34. package/src/lib/agent-coordinator.js +334 -0
  35. package/src/lib/agent-economy.js +334 -0
  36. package/src/lib/agent-router.js +333 -0
  37. package/src/lib/autonomous-agent.js +332 -0
  38. package/src/lib/chat-core.js +335 -0
  39. package/src/lib/cli-anything-bridge.js +341 -0
  40. package/src/lib/cli-context-engineering.js +351 -0
  41. package/src/lib/compliance-manager.js +334 -0
  42. package/src/lib/cowork-adapter.js +336 -0
  43. package/src/lib/cowork-evomap-adapter.js +341 -0
  44. package/src/lib/cowork-mcp-tools.js +341 -0
  45. package/src/lib/cowork-observe-html.js +341 -0
  46. package/src/lib/cowork-observe.js +341 -0
  47. package/src/lib/cowork-task-templates.js +342 -1
  48. package/src/lib/cowork-template-marketplace.js +340 -0
  49. package/src/lib/cross-chain.js +339 -0
  50. package/src/lib/crypto-manager.js +334 -0
  51. package/src/lib/dao-governance.js +339 -0
  52. package/src/lib/downloader.js +334 -0
  53. package/src/lib/evolution-system.js +334 -0
  54. package/src/lib/evomap-client.js +342 -0
  55. package/src/lib/evomap-federation.js +338 -0
  56. package/src/lib/evomap-manager.js +330 -0
  57. package/src/lib/execution-backend.js +330 -0
  58. package/src/lib/hashline.js +338 -0
  59. package/src/lib/hierarchical-memory.js +334 -0
  60. package/src/lib/inference-network.js +341 -0
  61. package/src/lib/interaction-adapter.js +330 -0
  62. package/src/lib/interactive-planner.js +354 -0
  63. package/src/lib/knowledge-graph.js +331 -0
  64. package/src/lib/pipeline-orchestrator.js +332 -0
  65. package/src/lib/plan-mode.js +336 -0
  66. package/src/lib/plugin-autodiscovery.js +334 -0
  67. package/src/lib/process-manager.js +336 -0
  68. package/src/lib/provider-options.js +346 -0
  69. package/src/lib/provider-stream.js +348 -0
  70. package/src/lib/service-manager.js +337 -0
  71. package/src/lib/session-core-singletons.js +341 -0
  72. package/src/lib/skill-mcp.js +336 -0
  73. package/src/lib/stix-parser.js +346 -0
  74. package/src/lib/sub-agent-context.js +343 -0
  75. package/src/lib/sub-agent-profiles.js +335 -0
  76. package/src/lib/sub-agent-registry.js +336 -0
  77. package/src/lib/todo-manager.js +336 -0
  78. package/src/lib/web-ui-server.js +348 -0
  79. package/src/lib/workflow-expr.js +346 -0
  80. package/src/lib/ws-chat-handler.js +337 -0
@@ -121,3 +121,340 @@ function runCompose(cmd, args) {
121
121
  throw err;
122
122
  }
123
123
  }
124
+
125
+ // =====================================================================
126
+ // service-manager V2 governance overlay (iter27)
127
+ // =====================================================================
128
+ export const SMGRGOV_PROFILE_MATURITY_V2 = Object.freeze({
129
+ PENDING: "pending",
130
+ ACTIVE: "active",
131
+ DEGRADED: "degraded",
132
+ ARCHIVED: "archived",
133
+ });
134
+ export const SMGRGOV_OP_LIFECYCLE_V2 = Object.freeze({
135
+ QUEUED: "queued",
136
+ OPERATING: "operating",
137
+ OPERATED: "operated",
138
+ FAILED: "failed",
139
+ CANCELLED: "cancelled",
140
+ });
141
+ const _smgrgovPTrans = new Map([
142
+ [
143
+ SMGRGOV_PROFILE_MATURITY_V2.PENDING,
144
+ new Set([
145
+ SMGRGOV_PROFILE_MATURITY_V2.ACTIVE,
146
+ SMGRGOV_PROFILE_MATURITY_V2.ARCHIVED,
147
+ ]),
148
+ ],
149
+ [
150
+ SMGRGOV_PROFILE_MATURITY_V2.ACTIVE,
151
+ new Set([
152
+ SMGRGOV_PROFILE_MATURITY_V2.DEGRADED,
153
+ SMGRGOV_PROFILE_MATURITY_V2.ARCHIVED,
154
+ ]),
155
+ ],
156
+ [
157
+ SMGRGOV_PROFILE_MATURITY_V2.DEGRADED,
158
+ new Set([
159
+ SMGRGOV_PROFILE_MATURITY_V2.ACTIVE,
160
+ SMGRGOV_PROFILE_MATURITY_V2.ARCHIVED,
161
+ ]),
162
+ ],
163
+ [SMGRGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
164
+ ]);
165
+ const _smgrgovPTerminal = new Set([SMGRGOV_PROFILE_MATURITY_V2.ARCHIVED]);
166
+ const _smgrgovJTrans = new Map([
167
+ [
168
+ SMGRGOV_OP_LIFECYCLE_V2.QUEUED,
169
+ new Set([
170
+ SMGRGOV_OP_LIFECYCLE_V2.OPERATING,
171
+ SMGRGOV_OP_LIFECYCLE_V2.CANCELLED,
172
+ ]),
173
+ ],
174
+ [
175
+ SMGRGOV_OP_LIFECYCLE_V2.OPERATING,
176
+ new Set([
177
+ SMGRGOV_OP_LIFECYCLE_V2.OPERATED,
178
+ SMGRGOV_OP_LIFECYCLE_V2.FAILED,
179
+ SMGRGOV_OP_LIFECYCLE_V2.CANCELLED,
180
+ ]),
181
+ ],
182
+ [SMGRGOV_OP_LIFECYCLE_V2.OPERATED, new Set()],
183
+ [SMGRGOV_OP_LIFECYCLE_V2.FAILED, new Set()],
184
+ [SMGRGOV_OP_LIFECYCLE_V2.CANCELLED, new Set()],
185
+ ]);
186
+ const _smgrgovPsV2 = new Map();
187
+ const _smgrgovJsV2 = new Map();
188
+ let _smgrgovMaxActive = 8,
189
+ _smgrgovMaxPending = 20,
190
+ _smgrgovIdleMs = 30 * 24 * 60 * 60 * 1000,
191
+ _smgrgovStuckMs = 60 * 1000;
192
+ function _smgrgovPos(n, label) {
193
+ const v = Math.floor(Number(n));
194
+ if (!Number.isFinite(v) || v <= 0)
195
+ throw new Error(`${label} must be positive integer`);
196
+ return v;
197
+ }
198
+ function _smgrgovCheckP(from, to) {
199
+ const a = _smgrgovPTrans.get(from);
200
+ if (!a || !a.has(to))
201
+ throw new Error(`invalid smgrgov profile transition ${from} → ${to}`);
202
+ }
203
+ function _smgrgovCheckJ(from, to) {
204
+ const a = _smgrgovJTrans.get(from);
205
+ if (!a || !a.has(to))
206
+ throw new Error(`invalid smgrgov op transition ${from} → ${to}`);
207
+ }
208
+ function _smgrgovCountActive(owner) {
209
+ let c = 0;
210
+ for (const p of _smgrgovPsV2.values())
211
+ if (p.owner === owner && p.status === SMGRGOV_PROFILE_MATURITY_V2.ACTIVE)
212
+ c++;
213
+ return c;
214
+ }
215
+ function _smgrgovCountPending(profileId) {
216
+ let c = 0;
217
+ for (const j of _smgrgovJsV2.values())
218
+ if (
219
+ j.profileId === profileId &&
220
+ (j.status === SMGRGOV_OP_LIFECYCLE_V2.QUEUED ||
221
+ j.status === SMGRGOV_OP_LIFECYCLE_V2.OPERATING)
222
+ )
223
+ c++;
224
+ return c;
225
+ }
226
+ export function setMaxActiveSmgrgovProfilesPerOwnerV2(n) {
227
+ _smgrgovMaxActive = _smgrgovPos(n, "maxActiveSmgrgovProfilesPerOwner");
228
+ }
229
+ export function getMaxActiveSmgrgovProfilesPerOwnerV2() {
230
+ return _smgrgovMaxActive;
231
+ }
232
+ export function setMaxPendingSmgrgovOpsPerProfileV2(n) {
233
+ _smgrgovMaxPending = _smgrgovPos(n, "maxPendingSmgrgovOpsPerProfile");
234
+ }
235
+ export function getMaxPendingSmgrgovOpsPerProfileV2() {
236
+ return _smgrgovMaxPending;
237
+ }
238
+ export function setSmgrgovProfileIdleMsV2(n) {
239
+ _smgrgovIdleMs = _smgrgovPos(n, "smgrgovProfileIdleMs");
240
+ }
241
+ export function getSmgrgovProfileIdleMsV2() {
242
+ return _smgrgovIdleMs;
243
+ }
244
+ export function setSmgrgovOpStuckMsV2(n) {
245
+ _smgrgovStuckMs = _smgrgovPos(n, "smgrgovOpStuckMs");
246
+ }
247
+ export function getSmgrgovOpStuckMsV2() {
248
+ return _smgrgovStuckMs;
249
+ }
250
+ export function _resetStateServiceManagerGovV2() {
251
+ _smgrgovPsV2.clear();
252
+ _smgrgovJsV2.clear();
253
+ _smgrgovMaxActive = 8;
254
+ _smgrgovMaxPending = 20;
255
+ _smgrgovIdleMs = 30 * 24 * 60 * 60 * 1000;
256
+ _smgrgovStuckMs = 60 * 1000;
257
+ }
258
+ export function registerSmgrgovProfileV2({
259
+ id,
260
+ owner,
261
+ service,
262
+ metadata,
263
+ } = {}) {
264
+ if (!id || !owner) throw new Error("id and owner required");
265
+ if (_smgrgovPsV2.has(id))
266
+ throw new Error(`smgrgov profile ${id} already exists`);
267
+ const now = Date.now();
268
+ const p = {
269
+ id,
270
+ owner,
271
+ service: service || "default",
272
+ status: SMGRGOV_PROFILE_MATURITY_V2.PENDING,
273
+ createdAt: now,
274
+ updatedAt: now,
275
+ lastTouchedAt: now,
276
+ activatedAt: null,
277
+ archivedAt: null,
278
+ metadata: { ...(metadata || {}) },
279
+ };
280
+ _smgrgovPsV2.set(id, p);
281
+ return { ...p, metadata: { ...p.metadata } };
282
+ }
283
+ export function activateSmgrgovProfileV2(id) {
284
+ const p = _smgrgovPsV2.get(id);
285
+ if (!p) throw new Error(`smgrgov profile ${id} not found`);
286
+ const isInitial = p.status === SMGRGOV_PROFILE_MATURITY_V2.PENDING;
287
+ _smgrgovCheckP(p.status, SMGRGOV_PROFILE_MATURITY_V2.ACTIVE);
288
+ if (isInitial && _smgrgovCountActive(p.owner) >= _smgrgovMaxActive)
289
+ throw new Error(`max active smgrgov profiles for owner ${p.owner} reached`);
290
+ const now = Date.now();
291
+ p.status = SMGRGOV_PROFILE_MATURITY_V2.ACTIVE;
292
+ p.updatedAt = now;
293
+ p.lastTouchedAt = now;
294
+ if (!p.activatedAt) p.activatedAt = now;
295
+ return { ...p, metadata: { ...p.metadata } };
296
+ }
297
+ export function degradeSmgrgovProfileV2(id) {
298
+ const p = _smgrgovPsV2.get(id);
299
+ if (!p) throw new Error(`smgrgov profile ${id} not found`);
300
+ _smgrgovCheckP(p.status, SMGRGOV_PROFILE_MATURITY_V2.DEGRADED);
301
+ p.status = SMGRGOV_PROFILE_MATURITY_V2.DEGRADED;
302
+ p.updatedAt = Date.now();
303
+ return { ...p, metadata: { ...p.metadata } };
304
+ }
305
+ export function archiveSmgrgovProfileV2(id) {
306
+ const p = _smgrgovPsV2.get(id);
307
+ if (!p) throw new Error(`smgrgov profile ${id} not found`);
308
+ _smgrgovCheckP(p.status, SMGRGOV_PROFILE_MATURITY_V2.ARCHIVED);
309
+ const now = Date.now();
310
+ p.status = SMGRGOV_PROFILE_MATURITY_V2.ARCHIVED;
311
+ p.updatedAt = now;
312
+ if (!p.archivedAt) p.archivedAt = now;
313
+ return { ...p, metadata: { ...p.metadata } };
314
+ }
315
+ export function touchSmgrgovProfileV2(id) {
316
+ const p = _smgrgovPsV2.get(id);
317
+ if (!p) throw new Error(`smgrgov profile ${id} not found`);
318
+ if (_smgrgovPTerminal.has(p.status))
319
+ throw new Error(`cannot touch terminal smgrgov profile ${id}`);
320
+ const now = Date.now();
321
+ p.lastTouchedAt = now;
322
+ p.updatedAt = now;
323
+ return { ...p, metadata: { ...p.metadata } };
324
+ }
325
+ export function getSmgrgovProfileV2(id) {
326
+ const p = _smgrgovPsV2.get(id);
327
+ if (!p) return null;
328
+ return { ...p, metadata: { ...p.metadata } };
329
+ }
330
+ export function listSmgrgovProfilesV2() {
331
+ return [..._smgrgovPsV2.values()].map((p) => ({
332
+ ...p,
333
+ metadata: { ...p.metadata },
334
+ }));
335
+ }
336
+ export function createSmgrgovOpV2({ id, profileId, action, metadata } = {}) {
337
+ if (!id || !profileId) throw new Error("id and profileId required");
338
+ if (_smgrgovJsV2.has(id)) throw new Error(`smgrgov op ${id} already exists`);
339
+ if (!_smgrgovPsV2.has(profileId))
340
+ throw new Error(`smgrgov profile ${profileId} not found`);
341
+ if (_smgrgovCountPending(profileId) >= _smgrgovMaxPending)
342
+ throw new Error(`max pending smgrgov ops for profile ${profileId} reached`);
343
+ const now = Date.now();
344
+ const j = {
345
+ id,
346
+ profileId,
347
+ action: action || "",
348
+ status: SMGRGOV_OP_LIFECYCLE_V2.QUEUED,
349
+ createdAt: now,
350
+ updatedAt: now,
351
+ startedAt: null,
352
+ settledAt: null,
353
+ metadata: { ...(metadata || {}) },
354
+ };
355
+ _smgrgovJsV2.set(id, j);
356
+ return { ...j, metadata: { ...j.metadata } };
357
+ }
358
+ export function operatingSmgrgovOpV2(id) {
359
+ const j = _smgrgovJsV2.get(id);
360
+ if (!j) throw new Error(`smgrgov op ${id} not found`);
361
+ _smgrgovCheckJ(j.status, SMGRGOV_OP_LIFECYCLE_V2.OPERATING);
362
+ const now = Date.now();
363
+ j.status = SMGRGOV_OP_LIFECYCLE_V2.OPERATING;
364
+ j.updatedAt = now;
365
+ if (!j.startedAt) j.startedAt = now;
366
+ return { ...j, metadata: { ...j.metadata } };
367
+ }
368
+ export function completeOpSmgrgovV2(id) {
369
+ const j = _smgrgovJsV2.get(id);
370
+ if (!j) throw new Error(`smgrgov op ${id} not found`);
371
+ _smgrgovCheckJ(j.status, SMGRGOV_OP_LIFECYCLE_V2.OPERATED);
372
+ const now = Date.now();
373
+ j.status = SMGRGOV_OP_LIFECYCLE_V2.OPERATED;
374
+ j.updatedAt = now;
375
+ if (!j.settledAt) j.settledAt = now;
376
+ return { ...j, metadata: { ...j.metadata } };
377
+ }
378
+ export function failSmgrgovOpV2(id, reason) {
379
+ const j = _smgrgovJsV2.get(id);
380
+ if (!j) throw new Error(`smgrgov op ${id} not found`);
381
+ _smgrgovCheckJ(j.status, SMGRGOV_OP_LIFECYCLE_V2.FAILED);
382
+ const now = Date.now();
383
+ j.status = SMGRGOV_OP_LIFECYCLE_V2.FAILED;
384
+ j.updatedAt = now;
385
+ if (!j.settledAt) j.settledAt = now;
386
+ if (reason) j.metadata.failReason = String(reason);
387
+ return { ...j, metadata: { ...j.metadata } };
388
+ }
389
+ export function cancelSmgrgovOpV2(id, reason) {
390
+ const j = _smgrgovJsV2.get(id);
391
+ if (!j) throw new Error(`smgrgov op ${id} not found`);
392
+ _smgrgovCheckJ(j.status, SMGRGOV_OP_LIFECYCLE_V2.CANCELLED);
393
+ const now = Date.now();
394
+ j.status = SMGRGOV_OP_LIFECYCLE_V2.CANCELLED;
395
+ j.updatedAt = now;
396
+ if (!j.settledAt) j.settledAt = now;
397
+ if (reason) j.metadata.cancelReason = String(reason);
398
+ return { ...j, metadata: { ...j.metadata } };
399
+ }
400
+ export function getSmgrgovOpV2(id) {
401
+ const j = _smgrgovJsV2.get(id);
402
+ if (!j) return null;
403
+ return { ...j, metadata: { ...j.metadata } };
404
+ }
405
+ export function listSmgrgovOpsV2() {
406
+ return [..._smgrgovJsV2.values()].map((j) => ({
407
+ ...j,
408
+ metadata: { ...j.metadata },
409
+ }));
410
+ }
411
+ export function autoDegradeIdleSmgrgovProfilesV2({ now } = {}) {
412
+ const t = now ?? Date.now();
413
+ const flipped = [];
414
+ for (const p of _smgrgovPsV2.values())
415
+ if (
416
+ p.status === SMGRGOV_PROFILE_MATURITY_V2.ACTIVE &&
417
+ t - p.lastTouchedAt >= _smgrgovIdleMs
418
+ ) {
419
+ p.status = SMGRGOV_PROFILE_MATURITY_V2.DEGRADED;
420
+ p.updatedAt = t;
421
+ flipped.push(p.id);
422
+ }
423
+ return { flipped, count: flipped.length };
424
+ }
425
+ export function autoFailStuckSmgrgovOpsV2({ now } = {}) {
426
+ const t = now ?? Date.now();
427
+ const flipped = [];
428
+ for (const j of _smgrgovJsV2.values())
429
+ if (
430
+ j.status === SMGRGOV_OP_LIFECYCLE_V2.OPERATING &&
431
+ j.startedAt != null &&
432
+ t - j.startedAt >= _smgrgovStuckMs
433
+ ) {
434
+ j.status = SMGRGOV_OP_LIFECYCLE_V2.FAILED;
435
+ j.updatedAt = t;
436
+ if (!j.settledAt) j.settledAt = t;
437
+ j.metadata.failReason = "auto-fail-stuck";
438
+ flipped.push(j.id);
439
+ }
440
+ return { flipped, count: flipped.length };
441
+ }
442
+ export function getServiceManagerGovStatsV2() {
443
+ const profilesByStatus = {};
444
+ for (const v of Object.values(SMGRGOV_PROFILE_MATURITY_V2))
445
+ profilesByStatus[v] = 0;
446
+ for (const p of _smgrgovPsV2.values()) profilesByStatus[p.status]++;
447
+ const opsByStatus = {};
448
+ for (const v of Object.values(SMGRGOV_OP_LIFECYCLE_V2)) opsByStatus[v] = 0;
449
+ for (const j of _smgrgovJsV2.values()) opsByStatus[j.status]++;
450
+ return {
451
+ totalSmgrgovProfilesV2: _smgrgovPsV2.size,
452
+ totalSmgrgovOpsV2: _smgrgovJsV2.size,
453
+ maxActiveSmgrgovProfilesPerOwner: _smgrgovMaxActive,
454
+ maxPendingSmgrgovOpsPerProfile: _smgrgovMaxPending,
455
+ smgrgovProfileIdleMs: _smgrgovIdleMs,
456
+ smgrgovOpStuckMs: _smgrgovStuckMs,
457
+ profilesByStatus,
458
+ opsByStatus,
459
+ };
460
+ }
@@ -124,3 +124,344 @@ export function resetSessionCoreSingletonsForTests() {
124
124
  _approvalGate = null;
125
125
  _sessionManager = null;
126
126
  }
127
+
128
+ // =====================================================================
129
+ // session-core-singletons V2 governance overlay (iter27)
130
+ // =====================================================================
131
+ export const SCSGOV_PROFILE_MATURITY_V2 = Object.freeze({
132
+ PENDING: "pending",
133
+ ACTIVE: "active",
134
+ STALE: "stale",
135
+ ARCHIVED: "archived",
136
+ });
137
+ export const SCSGOV_ACCESS_LIFECYCLE_V2 = Object.freeze({
138
+ QUEUED: "queued",
139
+ RESOLVING: "resolving",
140
+ RESOLVED: "resolved",
141
+ FAILED: "failed",
142
+ CANCELLED: "cancelled",
143
+ });
144
+ const _scsgovPTrans = new Map([
145
+ [
146
+ SCSGOV_PROFILE_MATURITY_V2.PENDING,
147
+ new Set([
148
+ SCSGOV_PROFILE_MATURITY_V2.ACTIVE,
149
+ SCSGOV_PROFILE_MATURITY_V2.ARCHIVED,
150
+ ]),
151
+ ],
152
+ [
153
+ SCSGOV_PROFILE_MATURITY_V2.ACTIVE,
154
+ new Set([
155
+ SCSGOV_PROFILE_MATURITY_V2.STALE,
156
+ SCSGOV_PROFILE_MATURITY_V2.ARCHIVED,
157
+ ]),
158
+ ],
159
+ [
160
+ SCSGOV_PROFILE_MATURITY_V2.STALE,
161
+ new Set([
162
+ SCSGOV_PROFILE_MATURITY_V2.ACTIVE,
163
+ SCSGOV_PROFILE_MATURITY_V2.ARCHIVED,
164
+ ]),
165
+ ],
166
+ [SCSGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
167
+ ]);
168
+ const _scsgovPTerminal = new Set([SCSGOV_PROFILE_MATURITY_V2.ARCHIVED]);
169
+ const _scsgovJTrans = new Map([
170
+ [
171
+ SCSGOV_ACCESS_LIFECYCLE_V2.QUEUED,
172
+ new Set([
173
+ SCSGOV_ACCESS_LIFECYCLE_V2.RESOLVING,
174
+ SCSGOV_ACCESS_LIFECYCLE_V2.CANCELLED,
175
+ ]),
176
+ ],
177
+ [
178
+ SCSGOV_ACCESS_LIFECYCLE_V2.RESOLVING,
179
+ new Set([
180
+ SCSGOV_ACCESS_LIFECYCLE_V2.RESOLVED,
181
+ SCSGOV_ACCESS_LIFECYCLE_V2.FAILED,
182
+ SCSGOV_ACCESS_LIFECYCLE_V2.CANCELLED,
183
+ ]),
184
+ ],
185
+ [SCSGOV_ACCESS_LIFECYCLE_V2.RESOLVED, new Set()],
186
+ [SCSGOV_ACCESS_LIFECYCLE_V2.FAILED, new Set()],
187
+ [SCSGOV_ACCESS_LIFECYCLE_V2.CANCELLED, new Set()],
188
+ ]);
189
+ const _scsgovPsV2 = new Map();
190
+ const _scsgovJsV2 = new Map();
191
+ let _scsgovMaxActive = 8,
192
+ _scsgovMaxPending = 20,
193
+ _scsgovIdleMs = 30 * 24 * 60 * 60 * 1000,
194
+ _scsgovStuckMs = 60 * 1000;
195
+ function _scsgovPos(n, label) {
196
+ const v = Math.floor(Number(n));
197
+ if (!Number.isFinite(v) || v <= 0)
198
+ throw new Error(`${label} must be positive integer`);
199
+ return v;
200
+ }
201
+ function _scsgovCheckP(from, to) {
202
+ const a = _scsgovPTrans.get(from);
203
+ if (!a || !a.has(to))
204
+ throw new Error(`invalid scsgov profile transition ${from} → ${to}`);
205
+ }
206
+ function _scsgovCheckJ(from, to) {
207
+ const a = _scsgovJTrans.get(from);
208
+ if (!a || !a.has(to))
209
+ throw new Error(`invalid scsgov access transition ${from} → ${to}`);
210
+ }
211
+ function _scsgovCountActive(owner) {
212
+ let c = 0;
213
+ for (const p of _scsgovPsV2.values())
214
+ if (p.owner === owner && p.status === SCSGOV_PROFILE_MATURITY_V2.ACTIVE)
215
+ c++;
216
+ return c;
217
+ }
218
+ function _scsgovCountPending(profileId) {
219
+ let c = 0;
220
+ for (const j of _scsgovJsV2.values())
221
+ if (
222
+ j.profileId === profileId &&
223
+ (j.status === SCSGOV_ACCESS_LIFECYCLE_V2.QUEUED ||
224
+ j.status === SCSGOV_ACCESS_LIFECYCLE_V2.RESOLVING)
225
+ )
226
+ c++;
227
+ return c;
228
+ }
229
+ export function setMaxActiveScsgovProfilesPerOwnerV2(n) {
230
+ _scsgovMaxActive = _scsgovPos(n, "maxActiveScsgovProfilesPerOwner");
231
+ }
232
+ export function getMaxActiveScsgovProfilesPerOwnerV2() {
233
+ return _scsgovMaxActive;
234
+ }
235
+ export function setMaxPendingScsgovAccesssPerProfileV2(n) {
236
+ _scsgovMaxPending = _scsgovPos(n, "maxPendingScsgovAccesssPerProfile");
237
+ }
238
+ export function getMaxPendingScsgovAccesssPerProfileV2() {
239
+ return _scsgovMaxPending;
240
+ }
241
+ export function setScsgovProfileIdleMsV2(n) {
242
+ _scsgovIdleMs = _scsgovPos(n, "scsgovProfileIdleMs");
243
+ }
244
+ export function getScsgovProfileIdleMsV2() {
245
+ return _scsgovIdleMs;
246
+ }
247
+ export function setScsgovAccessStuckMsV2(n) {
248
+ _scsgovStuckMs = _scsgovPos(n, "scsgovAccessStuckMs");
249
+ }
250
+ export function getScsgovAccessStuckMsV2() {
251
+ return _scsgovStuckMs;
252
+ }
253
+ export function _resetStateSessionCoreSingletonsGovV2() {
254
+ _scsgovPsV2.clear();
255
+ _scsgovJsV2.clear();
256
+ _scsgovMaxActive = 8;
257
+ _scsgovMaxPending = 20;
258
+ _scsgovIdleMs = 30 * 24 * 60 * 60 * 1000;
259
+ _scsgovStuckMs = 60 * 1000;
260
+ }
261
+ export function registerScsgovProfileV2({
262
+ id,
263
+ owner,
264
+ component,
265
+ metadata,
266
+ } = {}) {
267
+ if (!id || !owner) throw new Error("id and owner required");
268
+ if (_scsgovPsV2.has(id))
269
+ throw new Error(`scsgov profile ${id} already exists`);
270
+ const now = Date.now();
271
+ const p = {
272
+ id,
273
+ owner,
274
+ component: component || "default",
275
+ status: SCSGOV_PROFILE_MATURITY_V2.PENDING,
276
+ createdAt: now,
277
+ updatedAt: now,
278
+ lastTouchedAt: now,
279
+ activatedAt: null,
280
+ archivedAt: null,
281
+ metadata: { ...(metadata || {}) },
282
+ };
283
+ _scsgovPsV2.set(id, p);
284
+ return { ...p, metadata: { ...p.metadata } };
285
+ }
286
+ export function activateScsgovProfileV2(id) {
287
+ const p = _scsgovPsV2.get(id);
288
+ if (!p) throw new Error(`scsgov profile ${id} not found`);
289
+ const isInitial = p.status === SCSGOV_PROFILE_MATURITY_V2.PENDING;
290
+ _scsgovCheckP(p.status, SCSGOV_PROFILE_MATURITY_V2.ACTIVE);
291
+ if (isInitial && _scsgovCountActive(p.owner) >= _scsgovMaxActive)
292
+ throw new Error(`max active scsgov profiles for owner ${p.owner} reached`);
293
+ const now = Date.now();
294
+ p.status = SCSGOV_PROFILE_MATURITY_V2.ACTIVE;
295
+ p.updatedAt = now;
296
+ p.lastTouchedAt = now;
297
+ if (!p.activatedAt) p.activatedAt = now;
298
+ return { ...p, metadata: { ...p.metadata } };
299
+ }
300
+ export function staleScsgovProfileV2(id) {
301
+ const p = _scsgovPsV2.get(id);
302
+ if (!p) throw new Error(`scsgov profile ${id} not found`);
303
+ _scsgovCheckP(p.status, SCSGOV_PROFILE_MATURITY_V2.STALE);
304
+ p.status = SCSGOV_PROFILE_MATURITY_V2.STALE;
305
+ p.updatedAt = Date.now();
306
+ return { ...p, metadata: { ...p.metadata } };
307
+ }
308
+ export function archiveScsgovProfileV2(id) {
309
+ const p = _scsgovPsV2.get(id);
310
+ if (!p) throw new Error(`scsgov profile ${id} not found`);
311
+ _scsgovCheckP(p.status, SCSGOV_PROFILE_MATURITY_V2.ARCHIVED);
312
+ const now = Date.now();
313
+ p.status = SCSGOV_PROFILE_MATURITY_V2.ARCHIVED;
314
+ p.updatedAt = now;
315
+ if (!p.archivedAt) p.archivedAt = now;
316
+ return { ...p, metadata: { ...p.metadata } };
317
+ }
318
+ export function touchScsgovProfileV2(id) {
319
+ const p = _scsgovPsV2.get(id);
320
+ if (!p) throw new Error(`scsgov profile ${id} not found`);
321
+ if (_scsgovPTerminal.has(p.status))
322
+ throw new Error(`cannot touch terminal scsgov profile ${id}`);
323
+ const now = Date.now();
324
+ p.lastTouchedAt = now;
325
+ p.updatedAt = now;
326
+ return { ...p, metadata: { ...p.metadata } };
327
+ }
328
+ export function getScsgovProfileV2(id) {
329
+ const p = _scsgovPsV2.get(id);
330
+ if (!p) return null;
331
+ return { ...p, metadata: { ...p.metadata } };
332
+ }
333
+ export function listScsgovProfilesV2() {
334
+ return [..._scsgovPsV2.values()].map((p) => ({
335
+ ...p,
336
+ metadata: { ...p.metadata },
337
+ }));
338
+ }
339
+ export function createScsgovAccessV2({ id, profileId, caller, metadata } = {}) {
340
+ if (!id || !profileId) throw new Error("id and profileId required");
341
+ if (_scsgovJsV2.has(id))
342
+ throw new Error(`scsgov access ${id} already exists`);
343
+ if (!_scsgovPsV2.has(profileId))
344
+ throw new Error(`scsgov profile ${profileId} not found`);
345
+ if (_scsgovCountPending(profileId) >= _scsgovMaxPending)
346
+ throw new Error(
347
+ `max pending scsgov accesss for profile ${profileId} reached`,
348
+ );
349
+ const now = Date.now();
350
+ const j = {
351
+ id,
352
+ profileId,
353
+ caller: caller || "",
354
+ status: SCSGOV_ACCESS_LIFECYCLE_V2.QUEUED,
355
+ createdAt: now,
356
+ updatedAt: now,
357
+ startedAt: null,
358
+ settledAt: null,
359
+ metadata: { ...(metadata || {}) },
360
+ };
361
+ _scsgovJsV2.set(id, j);
362
+ return { ...j, metadata: { ...j.metadata } };
363
+ }
364
+ export function resolvingScsgovAccessV2(id) {
365
+ const j = _scsgovJsV2.get(id);
366
+ if (!j) throw new Error(`scsgov access ${id} not found`);
367
+ _scsgovCheckJ(j.status, SCSGOV_ACCESS_LIFECYCLE_V2.RESOLVING);
368
+ const now = Date.now();
369
+ j.status = SCSGOV_ACCESS_LIFECYCLE_V2.RESOLVING;
370
+ j.updatedAt = now;
371
+ if (!j.startedAt) j.startedAt = now;
372
+ return { ...j, metadata: { ...j.metadata } };
373
+ }
374
+ export function completeAccessScsgovV2(id) {
375
+ const j = _scsgovJsV2.get(id);
376
+ if (!j) throw new Error(`scsgov access ${id} not found`);
377
+ _scsgovCheckJ(j.status, SCSGOV_ACCESS_LIFECYCLE_V2.RESOLVED);
378
+ const now = Date.now();
379
+ j.status = SCSGOV_ACCESS_LIFECYCLE_V2.RESOLVED;
380
+ j.updatedAt = now;
381
+ if (!j.settledAt) j.settledAt = now;
382
+ return { ...j, metadata: { ...j.metadata } };
383
+ }
384
+ export function failScsgovAccessV2(id, reason) {
385
+ const j = _scsgovJsV2.get(id);
386
+ if (!j) throw new Error(`scsgov access ${id} not found`);
387
+ _scsgovCheckJ(j.status, SCSGOV_ACCESS_LIFECYCLE_V2.FAILED);
388
+ const now = Date.now();
389
+ j.status = SCSGOV_ACCESS_LIFECYCLE_V2.FAILED;
390
+ j.updatedAt = now;
391
+ if (!j.settledAt) j.settledAt = now;
392
+ if (reason) j.metadata.failReason = String(reason);
393
+ return { ...j, metadata: { ...j.metadata } };
394
+ }
395
+ export function cancelScsgovAccessV2(id, reason) {
396
+ const j = _scsgovJsV2.get(id);
397
+ if (!j) throw new Error(`scsgov access ${id} not found`);
398
+ _scsgovCheckJ(j.status, SCSGOV_ACCESS_LIFECYCLE_V2.CANCELLED);
399
+ const now = Date.now();
400
+ j.status = SCSGOV_ACCESS_LIFECYCLE_V2.CANCELLED;
401
+ j.updatedAt = now;
402
+ if (!j.settledAt) j.settledAt = now;
403
+ if (reason) j.metadata.cancelReason = String(reason);
404
+ return { ...j, metadata: { ...j.metadata } };
405
+ }
406
+ export function getScsgovAccessV2(id) {
407
+ const j = _scsgovJsV2.get(id);
408
+ if (!j) return null;
409
+ return { ...j, metadata: { ...j.metadata } };
410
+ }
411
+ export function listScsgovAccesssV2() {
412
+ return [..._scsgovJsV2.values()].map((j) => ({
413
+ ...j,
414
+ metadata: { ...j.metadata },
415
+ }));
416
+ }
417
+ export function autoStaleIdleScsgovProfilesV2({ now } = {}) {
418
+ const t = now ?? Date.now();
419
+ const flipped = [];
420
+ for (const p of _scsgovPsV2.values())
421
+ if (
422
+ p.status === SCSGOV_PROFILE_MATURITY_V2.ACTIVE &&
423
+ t - p.lastTouchedAt >= _scsgovIdleMs
424
+ ) {
425
+ p.status = SCSGOV_PROFILE_MATURITY_V2.STALE;
426
+ p.updatedAt = t;
427
+ flipped.push(p.id);
428
+ }
429
+ return { flipped, count: flipped.length };
430
+ }
431
+ export function autoFailStuckScsgovAccesssV2({ now } = {}) {
432
+ const t = now ?? Date.now();
433
+ const flipped = [];
434
+ for (const j of _scsgovJsV2.values())
435
+ if (
436
+ j.status === SCSGOV_ACCESS_LIFECYCLE_V2.RESOLVING &&
437
+ j.startedAt != null &&
438
+ t - j.startedAt >= _scsgovStuckMs
439
+ ) {
440
+ j.status = SCSGOV_ACCESS_LIFECYCLE_V2.FAILED;
441
+ j.updatedAt = t;
442
+ if (!j.settledAt) j.settledAt = t;
443
+ j.metadata.failReason = "auto-fail-stuck";
444
+ flipped.push(j.id);
445
+ }
446
+ return { flipped, count: flipped.length };
447
+ }
448
+ export function getSessionCoreSingletonsGovStatsV2() {
449
+ const profilesByStatus = {};
450
+ for (const v of Object.values(SCSGOV_PROFILE_MATURITY_V2))
451
+ profilesByStatus[v] = 0;
452
+ for (const p of _scsgovPsV2.values()) profilesByStatus[p.status]++;
453
+ const accesssByStatus = {};
454
+ for (const v of Object.values(SCSGOV_ACCESS_LIFECYCLE_V2))
455
+ accesssByStatus[v] = 0;
456
+ for (const j of _scsgovJsV2.values()) accesssByStatus[j.status]++;
457
+ return {
458
+ totalScsgovProfilesV2: _scsgovPsV2.size,
459
+ totalScsgovAccesssV2: _scsgovJsV2.size,
460
+ maxActiveScsgovProfilesPerOwner: _scsgovMaxActive,
461
+ maxPendingScsgovAccesssPerProfile: _scsgovMaxPending,
462
+ scsgovProfileIdleMs: _scsgovIdleMs,
463
+ scsgovAccessStuckMs: _scsgovStuckMs,
464
+ profilesByStatus,
465
+ accesssByStatus,
466
+ };
467
+ }