chainlesschain 0.81.0 → 0.143.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/bin/chainlesschain.js +0 -0
  2. package/package.json +1 -1
  3. package/src/commands/a2a.js +62 -0
  4. package/src/commands/activitypub.js +61 -0
  5. package/src/commands/agent-network.js +254 -1
  6. package/src/commands/agent.js +117 -0
  7. package/src/commands/audit.js +302 -0
  8. package/src/commands/automation.js +271 -1
  9. package/src/commands/bi.js +61 -0
  10. package/src/commands/bm25.js +78 -0
  11. package/src/commands/browse.js +64 -0
  12. package/src/commands/ccron.js +78 -0
  13. package/src/commands/codegen.js +224 -0
  14. package/src/commands/collab.js +341 -0
  15. package/src/commands/compliance.js +1075 -0
  16. package/src/commands/compt.js +78 -0
  17. package/src/commands/consol.js +231 -0
  18. package/src/commands/cowork.js +263 -0
  19. package/src/commands/crosschain.js +62 -0
  20. package/src/commands/dao.js +62 -0
  21. package/src/commands/dbevo.js +284 -0
  22. package/src/commands/dev.js +252 -0
  23. package/src/commands/did.js +358 -0
  24. package/src/commands/dlp.js +61 -0
  25. package/src/commands/economy.js +56 -0
  26. package/src/commands/encrypt.js +341 -0
  27. package/src/commands/evolution.js +56 -0
  28. package/src/commands/evomap.js +61 -0
  29. package/src/commands/export.js +256 -1
  30. package/src/commands/fflag.js +178 -0
  31. package/src/commands/fusion.js +258 -0
  32. package/src/commands/git.js +45 -0
  33. package/src/commands/governance.js +325 -0
  34. package/src/commands/hardening.js +411 -0
  35. package/src/commands/hmemory.js +56 -0
  36. package/src/commands/hook.js +148 -0
  37. package/src/commands/import.js +252 -0
  38. package/src/commands/incentive.js +322 -0
  39. package/src/commands/inference.js +42 -0
  40. package/src/commands/infra.js +244 -0
  41. package/src/commands/instinct.js +260 -0
  42. package/src/commands/ipfs.js +318 -0
  43. package/src/commands/itbudget.js +45 -0
  44. package/src/commands/kg.js +387 -0
  45. package/src/commands/llm.js +263 -0
  46. package/src/commands/lowcode.js +44 -0
  47. package/src/commands/matrix.js +62 -0
  48. package/src/commands/mcp.js +221 -0
  49. package/src/commands/mcpscaf.js +41 -0
  50. package/src/commands/meminj.js +41 -0
  51. package/src/commands/memory.js +248 -0
  52. package/src/commands/multimodal.js +296 -0
  53. package/src/commands/nlprog.js +356 -0
  54. package/src/commands/nostr.js +62 -0
  55. package/src/commands/note.js +244 -0
  56. package/src/commands/ops.js +354 -0
  57. package/src/commands/orchestrate.js +166 -0
  58. package/src/commands/orchgov.js +45 -0
  59. package/src/commands/org.js +277 -0
  60. package/src/commands/p2p.js +390 -0
  61. package/src/commands/pdfp.js +78 -0
  62. package/src/commands/perception.js +290 -0
  63. package/src/commands/perf.js +39 -0
  64. package/src/commands/perm.js +45 -0
  65. package/src/commands/permmem.js +251 -0
  66. package/src/commands/pipeline.js +57 -1
  67. package/src/commands/planmode.js +45 -0
  68. package/src/commands/plugin-ecosystem.js +273 -0
  69. package/src/commands/pqc.js +393 -0
  70. package/src/commands/promcomp.js +82 -0
  71. package/src/commands/quantization.js +351 -0
  72. package/src/commands/rcache.js +271 -0
  73. package/src/commands/recommend.js +382 -0
  74. package/src/commands/runtime.js +307 -0
  75. package/src/commands/scim.js +262 -0
  76. package/src/commands/seshhook.js +41 -0
  77. package/src/commands/seshsearch.js +41 -0
  78. package/src/commands/seshtail.js +41 -0
  79. package/src/commands/seshu.js +41 -0
  80. package/src/commands/session.js +258 -0
  81. package/src/commands/sganal.js +78 -0
  82. package/src/commands/siem.js +40 -0
  83. package/src/commands/skill.js +267 -1
  84. package/src/commands/slotfill.js +41 -0
  85. package/src/commands/social.js +290 -0
  86. package/src/commands/sso.js +186 -1
  87. package/src/commands/svccont.js +45 -0
  88. package/src/commands/sync.js +256 -0
  89. package/src/commands/tech.js +338 -0
  90. package/src/commands/tenant.js +351 -0
  91. package/src/commands/tms.js +45 -0
  92. package/src/commands/tokens.js +269 -0
  93. package/src/commands/topiccls.js +45 -0
  94. package/src/commands/trust.js +249 -0
  95. package/src/commands/uprof.js +45 -0
  96. package/src/commands/vcheck.js +78 -0
  97. package/src/commands/wallet.js +277 -0
  98. package/src/commands/webfetch.js +41 -0
  99. package/src/commands/workflow.js +171 -0
  100. package/src/commands/zkp.js +62 -0
  101. package/src/harness/prompt-compressor.js +331 -0
  102. package/src/index.js +65 -1
  103. package/src/lib/a2a-protocol.js +105 -0
  104. package/src/lib/activitypub-bridge.js +105 -0
  105. package/src/lib/agent-coordinator.js +325 -0
  106. package/src/lib/agent-economy.js +105 -0
  107. package/src/lib/agent-network.js +387 -0
  108. package/src/lib/agent-router.js +395 -0
  109. package/src/lib/aiops.js +478 -0
  110. package/src/lib/app-builder.js +105 -0
  111. package/src/lib/audit-logger.js +379 -0
  112. package/src/lib/automation-engine.js +330 -0
  113. package/src/lib/autonomous-agent.js +105 -0
  114. package/src/lib/autonomous-developer.js +350 -0
  115. package/src/lib/bi-engine.js +105 -0
  116. package/src/lib/bm25-search.js +81 -0
  117. package/src/lib/browser-automation.js +105 -0
  118. package/src/lib/code-agent.js +323 -0
  119. package/src/lib/collaboration-governance.js +364 -0
  120. package/src/lib/community-governance.js +436 -0
  121. package/src/lib/compliance-framework-reporter.js +105 -0
  122. package/src/lib/compliance-manager.js +434 -0
  123. package/src/lib/compression-telemetry.js +81 -0
  124. package/src/lib/content-recommendation.js +469 -0
  125. package/src/lib/content-recommender.js +105 -0
  126. package/src/lib/cowork-cron.js +81 -0
  127. package/src/lib/cowork-task-runner.js +105 -0
  128. package/src/lib/cross-chain.js +105 -0
  129. package/src/lib/crypto-manager.js +350 -0
  130. package/src/lib/dao-governance.js +105 -0
  131. package/src/lib/dbevo.js +338 -0
  132. package/src/lib/decentral-infra.js +340 -0
  133. package/src/lib/did-manager.js +367 -0
  134. package/src/lib/dlp-engine.js +105 -0
  135. package/src/lib/evolution-system.js +105 -0
  136. package/src/lib/evomap-manager.js +105 -0
  137. package/src/lib/execution-backend.js +105 -0
  138. package/src/lib/feature-flags.js +85 -0
  139. package/src/lib/git-integration.js +105 -0
  140. package/src/lib/hardening-manager.js +348 -0
  141. package/src/lib/hierarchical-memory.js +105 -0
  142. package/src/lib/hook-manager.js +380 -0
  143. package/src/lib/inference-network.js +105 -0
  144. package/src/lib/instinct-manager.js +332 -0
  145. package/src/lib/ipfs-storage.js +334 -0
  146. package/src/lib/iteration-budget.js +105 -0
  147. package/src/lib/knowledge-exporter.js +381 -0
  148. package/src/lib/knowledge-graph.js +432 -0
  149. package/src/lib/knowledge-importer.js +379 -0
  150. package/src/lib/llm-providers.js +391 -0
  151. package/src/lib/matrix-bridge.js +105 -0
  152. package/src/lib/mcp-registry.js +333 -0
  153. package/src/lib/mcp-scaffold.js +81 -0
  154. package/src/lib/memory-injection.js +81 -0
  155. package/src/lib/memory-manager.js +330 -0
  156. package/src/lib/multimodal.js +346 -0
  157. package/src/lib/nl-programming.js +343 -0
  158. package/src/lib/nostr-bridge.js +105 -0
  159. package/src/lib/note-versioning.js +327 -0
  160. package/src/lib/orchestrator.js +105 -0
  161. package/src/lib/org-manager.js +323 -0
  162. package/src/lib/p2p-manager.js +387 -0
  163. package/src/lib/pdf-parser.js +81 -0
  164. package/src/lib/perception.js +346 -0
  165. package/src/lib/perf-tuning.js +109 -1
  166. package/src/lib/permanent-memory.js +320 -0
  167. package/src/lib/permission-engine.js +81 -0
  168. package/src/lib/pipeline-orchestrator.js +105 -0
  169. package/src/lib/plan-mode.js +81 -0
  170. package/src/lib/plugin-ecosystem.js +377 -0
  171. package/src/lib/pqc-manager.js +368 -0
  172. package/src/lib/prompt-compressor.js +1 -10
  173. package/src/lib/protocol-fusion.js +417 -0
  174. package/src/lib/quantization.js +325 -0
  175. package/src/lib/response-cache.js +327 -0
  176. package/src/lib/scim-manager.js +329 -0
  177. package/src/lib/service-container.js +81 -0
  178. package/src/lib/session-consolidator.js +105 -0
  179. package/src/lib/session-hooks.js +81 -0
  180. package/src/lib/session-manager.js +329 -0
  181. package/src/lib/session-search.js +81 -0
  182. package/src/lib/session-tail.js +81 -0
  183. package/src/lib/session-usage.js +83 -0
  184. package/src/lib/siem-exporter.js +105 -0
  185. package/src/lib/skill-loader.js +377 -0
  186. package/src/lib/slot-filler.js +81 -0
  187. package/src/lib/social-graph-analytics.js +81 -0
  188. package/src/lib/social-graph.js +81 -0
  189. package/src/lib/social-manager.js +326 -0
  190. package/src/lib/sso-manager.js +332 -0
  191. package/src/lib/sub-agent-registry.js +110 -0
  192. package/src/lib/sync-manager.js +326 -0
  193. package/src/lib/task-model-selector.js +81 -0
  194. package/src/lib/tech-learning-engine.js +369 -0
  195. package/src/lib/tenant-saas.js +460 -0
  196. package/src/lib/threat-intel.js +335 -0
  197. package/src/lib/todo-manager.js +105 -0
  198. package/src/lib/token-incentive.js +293 -0
  199. package/src/lib/token-tracker.js +329 -0
  200. package/src/lib/topic-classifier.js +105 -0
  201. package/src/lib/trust-security.js +390 -0
  202. package/src/lib/ueba.js +389 -0
  203. package/src/lib/universal-runtime.js +325 -0
  204. package/src/lib/user-profile.js +81 -0
  205. package/src/lib/version-checker.js +81 -0
  206. package/src/lib/wallet-manager.js +326 -0
  207. package/src/lib/web-fetch.js +81 -0
  208. package/src/lib/workflow-engine.js +322 -0
  209. package/src/lib/zkp-engine.js +105 -0
@@ -214,3 +214,108 @@ export async function takeScreenshot(url, outputPath, options = {}) {
214
214
  throw err;
215
215
  }
216
216
  }
217
+
218
+
219
+ // ===== V2 Surface: Browser Automation governance overlay (CLI v0.134.0) =====
220
+ export const BROWSE_TARGET_MATURITY_V2 = Object.freeze({
221
+ PENDING: "pending", ACTIVE: "active", DEGRADED: "degraded", RETIRED: "retired",
222
+ });
223
+ export const BROWSE_ACTION_LIFECYCLE_V2 = Object.freeze({
224
+ QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
225
+ });
226
+
227
+ const _brTargTrans = new Map([
228
+ [BROWSE_TARGET_MATURITY_V2.PENDING, new Set([BROWSE_TARGET_MATURITY_V2.ACTIVE, BROWSE_TARGET_MATURITY_V2.RETIRED])],
229
+ [BROWSE_TARGET_MATURITY_V2.ACTIVE, new Set([BROWSE_TARGET_MATURITY_V2.DEGRADED, BROWSE_TARGET_MATURITY_V2.RETIRED])],
230
+ [BROWSE_TARGET_MATURITY_V2.DEGRADED, new Set([BROWSE_TARGET_MATURITY_V2.ACTIVE, BROWSE_TARGET_MATURITY_V2.RETIRED])],
231
+ [BROWSE_TARGET_MATURITY_V2.RETIRED, new Set()],
232
+ ]);
233
+ const _brTargTerminal = new Set([BROWSE_TARGET_MATURITY_V2.RETIRED]);
234
+ const _brActTrans = new Map([
235
+ [BROWSE_ACTION_LIFECYCLE_V2.QUEUED, new Set([BROWSE_ACTION_LIFECYCLE_V2.RUNNING, BROWSE_ACTION_LIFECYCLE_V2.CANCELLED])],
236
+ [BROWSE_ACTION_LIFECYCLE_V2.RUNNING, new Set([BROWSE_ACTION_LIFECYCLE_V2.COMPLETED, BROWSE_ACTION_LIFECYCLE_V2.FAILED, BROWSE_ACTION_LIFECYCLE_V2.CANCELLED])],
237
+ [BROWSE_ACTION_LIFECYCLE_V2.COMPLETED, new Set()],
238
+ [BROWSE_ACTION_LIFECYCLE_V2.FAILED, new Set()],
239
+ [BROWSE_ACTION_LIFECYCLE_V2.CANCELLED, new Set()],
240
+ ]);
241
+
242
+ const _brTargets = new Map();
243
+ const _brActions = new Map();
244
+ let _brMaxActivePerOwner = 8;
245
+ let _brMaxPendingPerTarget = 20;
246
+ let _brTargetIdleMs = 12 * 60 * 60 * 1000;
247
+ let _brActionStuckMs = 3 * 60 * 1000;
248
+
249
+ function _brPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
250
+
251
+ export function setMaxActiveBrowseTargetsPerOwnerV2(n) { _brMaxActivePerOwner = _brPos(n, "maxActiveBrowseTargetsPerOwner"); }
252
+ export function getMaxActiveBrowseTargetsPerOwnerV2() { return _brMaxActivePerOwner; }
253
+ export function setMaxPendingBrowseActionsPerTargetV2(n) { _brMaxPendingPerTarget = _brPos(n, "maxPendingBrowseActionsPerTarget"); }
254
+ export function getMaxPendingBrowseActionsPerTargetV2() { return _brMaxPendingPerTarget; }
255
+ export function setBrowseTargetIdleMsV2(n) { _brTargetIdleMs = _brPos(n, "browseTargetIdleMs"); }
256
+ export function getBrowseTargetIdleMsV2() { return _brTargetIdleMs; }
257
+ export function setBrowseActionStuckMsV2(n) { _brActionStuckMs = _brPos(n, "browseActionStuckMs"); }
258
+ export function getBrowseActionStuckMsV2() { return _brActionStuckMs; }
259
+
260
+ export function _resetStateBrowserAutomationV2() {
261
+ _brTargets.clear(); _brActions.clear();
262
+ _brMaxActivePerOwner = 8; _brMaxPendingPerTarget = 20;
263
+ _brTargetIdleMs = 12 * 60 * 60 * 1000; _brActionStuckMs = 3 * 60 * 1000;
264
+ }
265
+
266
+ export function registerBrowseTargetV2({ id, owner, url, metadata } = {}) {
267
+ if (!id || typeof id !== "string") throw new Error("id is required");
268
+ if (!owner || typeof owner !== "string") throw new Error("owner is required");
269
+ if (_brTargets.has(id)) throw new Error(`browse target ${id} already registered`);
270
+ const now = Date.now();
271
+ const t = { id, owner, url: url || "", status: BROWSE_TARGET_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, retiredAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
272
+ _brTargets.set(id, t);
273
+ return { ...t, metadata: { ...t.metadata } };
274
+ }
275
+ function _brCheckT(from, to) { const a = _brTargTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid browse target transition ${from} → ${to}`); }
276
+ function _brCountActive(owner) { let n = 0; for (const t of _brTargets.values()) if (t.owner === owner && t.status === BROWSE_TARGET_MATURITY_V2.ACTIVE) n++; return n; }
277
+
278
+ export function activateBrowseTargetV2(id) {
279
+ const t = _brTargets.get(id); if (!t) throw new Error(`browse target ${id} not found`);
280
+ _brCheckT(t.status, BROWSE_TARGET_MATURITY_V2.ACTIVE);
281
+ const recovery = t.status === BROWSE_TARGET_MATURITY_V2.DEGRADED;
282
+ if (!recovery) { const a = _brCountActive(t.owner); if (a >= _brMaxActivePerOwner) throw new Error(`max active browse targets per owner (${_brMaxActivePerOwner}) reached for ${t.owner}`); }
283
+ const now = Date.now(); t.status = BROWSE_TARGET_MATURITY_V2.ACTIVE; t.updatedAt = now; t.lastTouchedAt = now; if (!t.activatedAt) t.activatedAt = now;
284
+ return { ...t, metadata: { ...t.metadata } };
285
+ }
286
+ export function degradeBrowseTargetV2(id) { const t = _brTargets.get(id); if (!t) throw new Error(`browse target ${id} not found`); _brCheckT(t.status, BROWSE_TARGET_MATURITY_V2.DEGRADED); t.status = BROWSE_TARGET_MATURITY_V2.DEGRADED; t.updatedAt = Date.now(); return { ...t, metadata: { ...t.metadata } }; }
287
+ export function retireBrowseTargetV2(id) { const t = _brTargets.get(id); if (!t) throw new Error(`browse target ${id} not found`); _brCheckT(t.status, BROWSE_TARGET_MATURITY_V2.RETIRED); const now = Date.now(); t.status = BROWSE_TARGET_MATURITY_V2.RETIRED; t.updatedAt = now; if (!t.retiredAt) t.retiredAt = now; return { ...t, metadata: { ...t.metadata } }; }
288
+ export function touchBrowseTargetV2(id) { const t = _brTargets.get(id); if (!t) throw new Error(`browse target ${id} not found`); if (_brTargTerminal.has(t.status)) throw new Error(`cannot touch terminal browse target ${id}`); const now = Date.now(); t.lastTouchedAt = now; t.updatedAt = now; return { ...t, metadata: { ...t.metadata } }; }
289
+ export function getBrowseTargetV2(id) { const t = _brTargets.get(id); if (!t) return null; return { ...t, metadata: { ...t.metadata } }; }
290
+ export function listBrowseTargetsV2() { return [..._brTargets.values()].map((t) => ({ ...t, metadata: { ...t.metadata } })); }
291
+
292
+ function _brCountPending(tid) { let n = 0; for (const a of _brActions.values()) if (a.targetId === tid && (a.status === BROWSE_ACTION_LIFECYCLE_V2.QUEUED || a.status === BROWSE_ACTION_LIFECYCLE_V2.RUNNING)) n++; return n; }
293
+
294
+ export function createBrowseActionV2({ id, targetId, kind, metadata } = {}) {
295
+ if (!id || typeof id !== "string") throw new Error("id is required");
296
+ if (!targetId || typeof targetId !== "string") throw new Error("targetId is required");
297
+ if (_brActions.has(id)) throw new Error(`browse action ${id} already exists`);
298
+ if (!_brTargets.has(targetId)) throw new Error(`browse target ${targetId} not found`);
299
+ const pending = _brCountPending(targetId);
300
+ if (pending >= _brMaxPendingPerTarget) throw new Error(`max pending browse actions per target (${_brMaxPendingPerTarget}) reached for ${targetId}`);
301
+ const now = Date.now();
302
+ const a = { id, targetId, kind: kind || "fetch", status: BROWSE_ACTION_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
303
+ _brActions.set(id, a);
304
+ return { ...a, metadata: { ...a.metadata } };
305
+ }
306
+ function _brCheckA(from, to) { const al = _brActTrans.get(from); if (!al || !al.has(to)) throw new Error(`invalid browse action transition ${from} → ${to}`); }
307
+ export function startBrowseActionV2(id) { const a = _brActions.get(id); if (!a) throw new Error(`browse action ${id} not found`); _brCheckA(a.status, BROWSE_ACTION_LIFECYCLE_V2.RUNNING); const now = Date.now(); a.status = BROWSE_ACTION_LIFECYCLE_V2.RUNNING; a.updatedAt = now; if (!a.startedAt) a.startedAt = now; return { ...a, metadata: { ...a.metadata } }; }
308
+ export function completeBrowseActionV2(id) { const a = _brActions.get(id); if (!a) throw new Error(`browse action ${id} not found`); _brCheckA(a.status, BROWSE_ACTION_LIFECYCLE_V2.COMPLETED); const now = Date.now(); a.status = BROWSE_ACTION_LIFECYCLE_V2.COMPLETED; a.updatedAt = now; if (!a.settledAt) a.settledAt = now; return { ...a, metadata: { ...a.metadata } }; }
309
+ export function failBrowseActionV2(id, reason) { const a = _brActions.get(id); if (!a) throw new Error(`browse action ${id} not found`); _brCheckA(a.status, BROWSE_ACTION_LIFECYCLE_V2.FAILED); const now = Date.now(); a.status = BROWSE_ACTION_LIFECYCLE_V2.FAILED; a.updatedAt = now; if (!a.settledAt) a.settledAt = now; if (reason) a.metadata.failReason = String(reason); return { ...a, metadata: { ...a.metadata } }; }
310
+ export function cancelBrowseActionV2(id, reason) { const a = _brActions.get(id); if (!a) throw new Error(`browse action ${id} not found`); _brCheckA(a.status, BROWSE_ACTION_LIFECYCLE_V2.CANCELLED); const now = Date.now(); a.status = BROWSE_ACTION_LIFECYCLE_V2.CANCELLED; a.updatedAt = now; if (!a.settledAt) a.settledAt = now; if (reason) a.metadata.cancelReason = String(reason); return { ...a, metadata: { ...a.metadata } }; }
311
+ export function getBrowseActionV2(id) { const a = _brActions.get(id); if (!a) return null; return { ...a, metadata: { ...a.metadata } }; }
312
+ export function listBrowseActionsV2() { return [..._brActions.values()].map((a) => ({ ...a, metadata: { ...a.metadata } })); }
313
+
314
+ export function autoDegradeIdleBrowseTargetsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const tg of _brTargets.values()) if (tg.status === BROWSE_TARGET_MATURITY_V2.ACTIVE && (t - tg.lastTouchedAt) >= _brTargetIdleMs) { tg.status = BROWSE_TARGET_MATURITY_V2.DEGRADED; tg.updatedAt = t; flipped.push(tg.id); } return { flipped, count: flipped.length }; }
315
+ export function autoFailStuckBrowseActionsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const a of _brActions.values()) if (a.status === BROWSE_ACTION_LIFECYCLE_V2.RUNNING && a.startedAt != null && (t - a.startedAt) >= _brActionStuckMs) { a.status = BROWSE_ACTION_LIFECYCLE_V2.FAILED; a.updatedAt = t; if (!a.settledAt) a.settledAt = t; a.metadata.failReason = "auto-fail-stuck"; flipped.push(a.id); } return { flipped, count: flipped.length }; }
316
+
317
+ export function getBrowserAutomationStatsV2() {
318
+ const targetsByStatus = {}; for (const s of Object.values(BROWSE_TARGET_MATURITY_V2)) targetsByStatus[s] = 0; for (const t of _brTargets.values()) targetsByStatus[t.status]++;
319
+ const actionsByStatus = {}; for (const s of Object.values(BROWSE_ACTION_LIFECYCLE_V2)) actionsByStatus[s] = 0; for (const a of _brActions.values()) actionsByStatus[a.status]++;
320
+ return { totalTargetsV2: _brTargets.size, totalActionsV2: _brActions.size, maxActiveBrowseTargetsPerOwner: _brMaxActivePerOwner, maxPendingBrowseActionsPerTarget: _brMaxPendingPerTarget, browseTargetIdleMs: _brTargetIdleMs, browseActionStuckMs: _brActionStuckMs, targetsByStatus, actionsByStatus };
321
+ }
@@ -440,3 +440,326 @@ export function _resetState() {
440
440
  _reviews.clear();
441
441
  _scaffolds.clear();
442
442
  }
443
+
444
+ /* ═════════════════════════════════════════════════════════ *
445
+ * Phase 86 V2 — Code Agent Maturity + Generation Job
446
+ * ═════════════════════════════════════════════════════════ */
447
+
448
+ export const AGENT_MATURITY_V2 = Object.freeze({
449
+ DRAFT: "draft",
450
+ ACTIVE: "active",
451
+ DEPRECATED: "deprecated",
452
+ RETIRED: "retired",
453
+ });
454
+
455
+ export const GEN_JOB_V2 = Object.freeze({
456
+ QUEUED: "queued",
457
+ RUNNING: "running",
458
+ SUCCEEDED: "succeeded",
459
+ FAILED: "failed",
460
+ CANCELED: "canceled",
461
+ });
462
+
463
+ const AGENT_TRANSITIONS_V2 = new Map([
464
+ ["draft", new Set(["active", "retired"])],
465
+ ["active", new Set(["deprecated", "retired"])],
466
+ ["deprecated", new Set(["active", "retired"])],
467
+ ]);
468
+ const AGENT_TERMINALS_V2 = new Set(["retired"]);
469
+
470
+ const JOB_TRANSITIONS_V2 = new Map([
471
+ ["queued", new Set(["running", "canceled", "failed"])],
472
+ ["running", new Set(["succeeded", "failed", "canceled"])],
473
+ ]);
474
+ const JOB_TERMINALS_V2 = new Set(["succeeded", "failed", "canceled"]);
475
+
476
+ export const CGA_DEFAULT_MAX_ACTIVE_AGENTS_PER_OWNER = 15;
477
+ export const CGA_DEFAULT_MAX_RUNNING_JOBS_PER_OWNER = 3;
478
+ export const CGA_DEFAULT_AGENT_IDLE_MS = 60 * 86400000; // 60 days
479
+ export const CGA_DEFAULT_JOB_STUCK_MS = 15 * 60000; // 15 minutes
480
+
481
+ let _maxActiveAgentsPerOwnerV2 = CGA_DEFAULT_MAX_ACTIVE_AGENTS_PER_OWNER;
482
+ let _maxRunningJobsPerOwnerV2 = CGA_DEFAULT_MAX_RUNNING_JOBS_PER_OWNER;
483
+ let _agentIdleMsV2 = CGA_DEFAULT_AGENT_IDLE_MS;
484
+ let _jobStuckMsV2 = CGA_DEFAULT_JOB_STUCK_MS;
485
+
486
+ function _positiveIntV2(n, label) {
487
+ const v = Math.floor(Number(n));
488
+ if (!Number.isFinite(v) || v <= 0)
489
+ throw new Error(`${label} must be a positive integer`);
490
+ return v;
491
+ }
492
+
493
+ export function getDefaultMaxActiveAgentsPerOwnerV2() {
494
+ return CGA_DEFAULT_MAX_ACTIVE_AGENTS_PER_OWNER;
495
+ }
496
+ export function getMaxActiveAgentsPerOwnerV2() {
497
+ return _maxActiveAgentsPerOwnerV2;
498
+ }
499
+ export function setMaxActiveAgentsPerOwnerV2(n) {
500
+ return (_maxActiveAgentsPerOwnerV2 = _positiveIntV2(
501
+ n,
502
+ "maxActiveAgentsPerOwner",
503
+ ));
504
+ }
505
+ export function getDefaultMaxRunningJobsPerOwnerV2() {
506
+ return CGA_DEFAULT_MAX_RUNNING_JOBS_PER_OWNER;
507
+ }
508
+ export function getMaxRunningJobsPerOwnerV2() {
509
+ return _maxRunningJobsPerOwnerV2;
510
+ }
511
+ export function setMaxRunningJobsPerOwnerV2(n) {
512
+ return (_maxRunningJobsPerOwnerV2 = _positiveIntV2(
513
+ n,
514
+ "maxRunningJobsPerOwner",
515
+ ));
516
+ }
517
+ export function getDefaultAgentIdleMsV2() {
518
+ return CGA_DEFAULT_AGENT_IDLE_MS;
519
+ }
520
+ export function getAgentIdleMsV2() {
521
+ return _agentIdleMsV2;
522
+ }
523
+ export function setAgentIdleMsV2(ms) {
524
+ return (_agentIdleMsV2 = _positiveIntV2(ms, "agentIdleMs"));
525
+ }
526
+ export function getDefaultJobStuckMsV2() {
527
+ return CGA_DEFAULT_JOB_STUCK_MS;
528
+ }
529
+ export function getJobStuckMsV2() {
530
+ return _jobStuckMsV2;
531
+ }
532
+ export function setJobStuckMsV2(ms) {
533
+ return (_jobStuckMsV2 = _positiveIntV2(ms, "jobStuckMs"));
534
+ }
535
+
536
+ const _agentsV2 = new Map();
537
+ const _jobsV2 = new Map();
538
+
539
+ export function registerAgentV2(
540
+ _db,
541
+ { agentId, ownerId, name, initialStatus, metadata } = {},
542
+ ) {
543
+ if (!agentId) throw new Error("agentId is required");
544
+ if (!ownerId) throw new Error("ownerId is required");
545
+ if (_agentsV2.has(agentId))
546
+ throw new Error(`Agent ${agentId} already exists`);
547
+ const status = initialStatus || AGENT_MATURITY_V2.DRAFT;
548
+ if (!Object.values(AGENT_MATURITY_V2).includes(status))
549
+ throw new Error(`Invalid initial status: ${status}`);
550
+ if (AGENT_TERMINALS_V2.has(status))
551
+ throw new Error(`Cannot register in terminal status: ${status}`);
552
+ if (status === AGENT_MATURITY_V2.ACTIVE) {
553
+ if (getActiveAgentCount(ownerId) >= _maxActiveAgentsPerOwnerV2)
554
+ throw new Error(
555
+ `Owner ${ownerId} reached active-agent cap (${_maxActiveAgentsPerOwnerV2})`,
556
+ );
557
+ }
558
+ const now = Date.now();
559
+ const record = {
560
+ agentId,
561
+ ownerId,
562
+ name: name || agentId,
563
+ status,
564
+ metadata: metadata || {},
565
+ createdAt: now,
566
+ updatedAt: now,
567
+ lastInvokedAt: now,
568
+ };
569
+ _agentsV2.set(agentId, record);
570
+ return { ...record, metadata: { ...record.metadata } };
571
+ }
572
+
573
+ export function getAgentV2(agentId) {
574
+ const r = _agentsV2.get(agentId);
575
+ return r ? { ...r, metadata: { ...r.metadata } } : null;
576
+ }
577
+
578
+ export function setAgentMaturityV2(_db, agentId, newStatus, patch = {}) {
579
+ const record = _agentsV2.get(agentId);
580
+ if (!record) throw new Error(`Unknown agent: ${agentId}`);
581
+ if (!Object.values(AGENT_MATURITY_V2).includes(newStatus))
582
+ throw new Error(`Invalid status: ${newStatus}`);
583
+ const allowed = AGENT_TRANSITIONS_V2.get(record.status) || new Set();
584
+ if (!allowed.has(newStatus))
585
+ throw new Error(`Invalid transition: ${record.status} -> ${newStatus}`);
586
+ if (newStatus === AGENT_MATURITY_V2.ACTIVE) {
587
+ if (getActiveAgentCount(record.ownerId) >= _maxActiveAgentsPerOwnerV2)
588
+ throw new Error(
589
+ `Owner ${record.ownerId} reached active-agent cap (${_maxActiveAgentsPerOwnerV2})`,
590
+ );
591
+ }
592
+ record.status = newStatus;
593
+ record.updatedAt = Date.now();
594
+ if (patch.reason !== undefined) record.lastReason = patch.reason;
595
+ if (patch.metadata)
596
+ record.metadata = { ...record.metadata, ...patch.metadata };
597
+ return { ...record, metadata: { ...record.metadata } };
598
+ }
599
+
600
+ export function activateAgent(db, id, reason) {
601
+ return setAgentMaturityV2(db, id, AGENT_MATURITY_V2.ACTIVE, { reason });
602
+ }
603
+ export function deprecateAgent(db, id, reason) {
604
+ return setAgentMaturityV2(db, id, AGENT_MATURITY_V2.DEPRECATED, { reason });
605
+ }
606
+ export function retireAgent(db, id, reason) {
607
+ return setAgentMaturityV2(db, id, AGENT_MATURITY_V2.RETIRED, { reason });
608
+ }
609
+
610
+ export function touchAgentInvocation(agentId) {
611
+ const record = _agentsV2.get(agentId);
612
+ if (!record) throw new Error(`Unknown agent: ${agentId}`);
613
+ record.lastInvokedAt = Date.now();
614
+ record.updatedAt = record.lastInvokedAt;
615
+ return { ...record, metadata: { ...record.metadata } };
616
+ }
617
+
618
+ export function enqueueGenJobV2(
619
+ _db,
620
+ { jobId, ownerId, agentId, prompt, metadata } = {},
621
+ ) {
622
+ if (!jobId) throw new Error("jobId is required");
623
+ if (!ownerId) throw new Error("ownerId is required");
624
+ if (!agentId) throw new Error("agentId is required");
625
+ if (!prompt) throw new Error("prompt is required");
626
+ if (_jobsV2.has(jobId)) throw new Error(`Job ${jobId} already exists`);
627
+ const now = Date.now();
628
+ const record = {
629
+ jobId,
630
+ ownerId,
631
+ agentId,
632
+ prompt,
633
+ status: GEN_JOB_V2.QUEUED,
634
+ metadata: metadata || {},
635
+ createdAt: now,
636
+ updatedAt: now,
637
+ };
638
+ _jobsV2.set(jobId, record);
639
+ return { ...record, metadata: { ...record.metadata } };
640
+ }
641
+
642
+ export function getGenJobV2(jobId) {
643
+ const r = _jobsV2.get(jobId);
644
+ return r ? { ...r, metadata: { ...r.metadata } } : null;
645
+ }
646
+
647
+ export function setGenJobStatusV2(_db, jobId, newStatus, patch = {}) {
648
+ const record = _jobsV2.get(jobId);
649
+ if (!record) throw new Error(`Unknown job: ${jobId}`);
650
+ if (!Object.values(GEN_JOB_V2).includes(newStatus))
651
+ throw new Error(`Invalid status: ${newStatus}`);
652
+ const allowed = JOB_TRANSITIONS_V2.get(record.status) || new Set();
653
+ if (!allowed.has(newStatus))
654
+ throw new Error(`Invalid transition: ${record.status} -> ${newStatus}`);
655
+ if (newStatus === GEN_JOB_V2.RUNNING) {
656
+ if (getRunningJobCount(record.ownerId) >= _maxRunningJobsPerOwnerV2)
657
+ throw new Error(
658
+ `Owner ${record.ownerId} reached running-job cap (${_maxRunningJobsPerOwnerV2})`,
659
+ );
660
+ if (!record.startedAt) record.startedAt = Date.now();
661
+ }
662
+ record.status = newStatus;
663
+ record.updatedAt = Date.now();
664
+ if (patch.reason !== undefined) record.lastReason = patch.reason;
665
+ if (patch.metadata)
666
+ record.metadata = { ...record.metadata, ...patch.metadata };
667
+ return { ...record, metadata: { ...record.metadata } };
668
+ }
669
+
670
+ export function startGenJob(db, id, reason) {
671
+ return setGenJobStatusV2(db, id, GEN_JOB_V2.RUNNING, { reason });
672
+ }
673
+ export function succeedGenJob(db, id, reason) {
674
+ return setGenJobStatusV2(db, id, GEN_JOB_V2.SUCCEEDED, { reason });
675
+ }
676
+ export function failGenJob(db, id, reason) {
677
+ return setGenJobStatusV2(db, id, GEN_JOB_V2.FAILED, { reason });
678
+ }
679
+ export function cancelGenJob(db, id, reason) {
680
+ return setGenJobStatusV2(db, id, GEN_JOB_V2.CANCELED, { reason });
681
+ }
682
+
683
+ export function getActiveAgentCount(ownerId) {
684
+ let n = 0;
685
+ for (const r of _agentsV2.values()) {
686
+ if (r.status !== AGENT_MATURITY_V2.ACTIVE) continue;
687
+ if (ownerId && r.ownerId !== ownerId) continue;
688
+ n++;
689
+ }
690
+ return n;
691
+ }
692
+
693
+ export function getRunningJobCount(ownerId) {
694
+ let n = 0;
695
+ for (const r of _jobsV2.values()) {
696
+ if (r.status !== GEN_JOB_V2.RUNNING) continue;
697
+ if (ownerId && r.ownerId !== ownerId) continue;
698
+ n++;
699
+ }
700
+ return n;
701
+ }
702
+
703
+ export function autoRetireIdleAgents(_db, nowMs) {
704
+ const now = nowMs ?? Date.now();
705
+ const flipped = [];
706
+ for (const r of _agentsV2.values()) {
707
+ if (
708
+ r.status === AGENT_MATURITY_V2.ACTIVE ||
709
+ r.status === AGENT_MATURITY_V2.DEPRECATED
710
+ ) {
711
+ if (now - r.lastInvokedAt > _agentIdleMsV2) {
712
+ r.status = AGENT_MATURITY_V2.RETIRED;
713
+ r.updatedAt = now;
714
+ r.lastReason = "idle_timeout";
715
+ flipped.push(r.agentId);
716
+ }
717
+ }
718
+ }
719
+ return { flipped, count: flipped.length };
720
+ }
721
+
722
+ export function autoFailStuckGenJobs(_db, nowMs) {
723
+ const now = nowMs ?? Date.now();
724
+ const flipped = [];
725
+ for (const r of _jobsV2.values()) {
726
+ if (r.status === GEN_JOB_V2.RUNNING) {
727
+ const anchor = r.startedAt || r.createdAt;
728
+ if (now - anchor > _jobStuckMsV2) {
729
+ r.status = GEN_JOB_V2.FAILED;
730
+ r.updatedAt = now;
731
+ r.lastReason = "job_timeout";
732
+ flipped.push(r.jobId);
733
+ }
734
+ }
735
+ }
736
+ return { flipped, count: flipped.length };
737
+ }
738
+
739
+ export function getCodeAgentStatsV2() {
740
+ const agentsByStatus = {};
741
+ for (const s of Object.values(AGENT_MATURITY_V2)) agentsByStatus[s] = 0;
742
+ const jobsByStatus = {};
743
+ for (const s of Object.values(GEN_JOB_V2)) jobsByStatus[s] = 0;
744
+ for (const r of _agentsV2.values()) agentsByStatus[r.status]++;
745
+ for (const r of _jobsV2.values()) jobsByStatus[r.status]++;
746
+ return {
747
+ totalAgentsV2: _agentsV2.size,
748
+ totalJobsV2: _jobsV2.size,
749
+ maxActiveAgentsPerOwner: _maxActiveAgentsPerOwnerV2,
750
+ maxRunningJobsPerOwner: _maxRunningJobsPerOwnerV2,
751
+ agentIdleMs: _agentIdleMsV2,
752
+ jobStuckMs: _jobStuckMsV2,
753
+ agentsByStatus,
754
+ jobsByStatus,
755
+ };
756
+ }
757
+
758
+ export function _resetStateV2() {
759
+ _maxActiveAgentsPerOwnerV2 = CGA_DEFAULT_MAX_ACTIVE_AGENTS_PER_OWNER;
760
+ _maxRunningJobsPerOwnerV2 = CGA_DEFAULT_MAX_RUNNING_JOBS_PER_OWNER;
761
+ _agentIdleMsV2 = CGA_DEFAULT_AGENT_IDLE_MS;
762
+ _jobStuckMsV2 = CGA_DEFAULT_JOB_STUCK_MS;
763
+ _agentsV2.clear();
764
+ _jobsV2.clear();
765
+ }