chainlesschain 0.143.0 → 0.152.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 (227) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +196 -28
  3. package/src/commands/activitypub.js +364 -27
  4. package/src/commands/agent-network.js +217 -0
  5. package/src/commands/agent.js +587 -103
  6. package/src/commands/audit.js +206 -0
  7. package/src/commands/automation.js +201 -0
  8. package/src/commands/bi.js +355 -27
  9. package/src/commands/bm25.js +111 -27
  10. package/src/commands/browse.js +403 -29
  11. package/src/commands/ccron.js +128 -28
  12. package/src/commands/chat.js +207 -0
  13. package/src/commands/codegen.js +207 -0
  14. package/src/commands/collab.js +211 -0
  15. package/src/commands/compliance.js +824 -34
  16. package/src/commands/compt.js +127 -29
  17. package/src/commands/consol.js +8 -2
  18. package/src/commands/cowork.js +783 -34
  19. package/src/commands/crosschain.js +182 -28
  20. package/src/commands/dao.js +182 -28
  21. package/src/commands/dbevo.js +227 -0
  22. package/src/commands/dev.js +207 -0
  23. package/src/commands/did-v2.js +217 -0
  24. package/src/commands/did.js +221 -0
  25. package/src/commands/dlp.js +385 -27
  26. package/src/commands/economy.js +205 -50
  27. package/src/commands/evolution.js +203 -50
  28. package/src/commands/evomap.js +387 -27
  29. package/src/commands/export.js +213 -0
  30. package/src/commands/federation.js +209 -0
  31. package/src/commands/fflag.js +74 -22
  32. package/src/commands/fusion.js +205 -0
  33. package/src/commands/git.js +177 -37
  34. package/src/commands/governance.js +209 -0
  35. package/src/commands/hardening.js +209 -0
  36. package/src/commands/hmemory.js +204 -50
  37. package/src/commands/hook.js +209 -0
  38. package/src/commands/import.js +209 -0
  39. package/src/commands/incentive.js +209 -0
  40. package/src/commands/inference.js +170 -34
  41. package/src/commands/infra.js +203 -0
  42. package/src/commands/instinct.js +209 -0
  43. package/src/commands/ipfs.js +207 -0
  44. package/src/commands/itbudget.js +149 -33
  45. package/src/commands/kg.js +206 -0
  46. package/src/commands/llm.js +217 -0
  47. package/src/commands/lowcode.js +195 -38
  48. package/src/commands/marketplace.js +206 -0
  49. package/src/commands/matrix.js +386 -27
  50. package/src/commands/mcp.js +217 -0
  51. package/src/commands/mcpscaf.js +145 -33
  52. package/src/commands/meminj.js +145 -33
  53. package/src/commands/memory.js +209 -0
  54. package/src/commands/multimodal.js +203 -0
  55. package/src/commands/nlprog.js +225 -0
  56. package/src/commands/nostr.js +387 -27
  57. package/src/commands/note.js +205 -0
  58. package/src/commands/ops.js +219 -0
  59. package/src/commands/orchestrate.js +217 -0
  60. package/src/commands/orchgov.js +144 -33
  61. package/src/commands/org.js +209 -0
  62. package/src/commands/p2p.js +209 -0
  63. package/src/commands/pdfp.js +109 -27
  64. package/src/commands/perception.js +209 -0
  65. package/src/commands/perf.js +169 -32
  66. package/src/commands/perm.js +144 -33
  67. package/src/commands/permmem.js +203 -0
  68. package/src/commands/pipeline.js +207 -52
  69. package/src/commands/planmode.js +141 -32
  70. package/src/commands/plugin-ecosystem.js +209 -0
  71. package/src/commands/pqc.js +213 -0
  72. package/src/commands/privacy.js +203 -0
  73. package/src/commands/promcomp.js +111 -27
  74. package/src/commands/quantization.js +207 -0
  75. package/src/commands/rcache.js +205 -0
  76. package/src/commands/recommend.js +410 -34
  77. package/src/commands/reputation.js +208 -0
  78. package/src/commands/runtime.js +205 -0
  79. package/src/commands/sandbox.js +206 -0
  80. package/src/commands/scim.js +209 -0
  81. package/src/commands/seshhook.js +145 -33
  82. package/src/commands/seshsearch.js +141 -33
  83. package/src/commands/seshtail.js +144 -33
  84. package/src/commands/seshu.js +152 -33
  85. package/src/commands/session.js +209 -0
  86. package/src/commands/sganal.js +123 -29
  87. package/src/commands/siem.js +201 -34
  88. package/src/commands/skill.js +207 -0
  89. package/src/commands/sla.js +212 -0
  90. package/src/commands/slotfill.js +146 -33
  91. package/src/commands/social.js +358 -32
  92. package/src/commands/sso.js +209 -0
  93. package/src/commands/stress.js +206 -0
  94. package/src/commands/svccont.js +145 -33
  95. package/src/commands/sync.js +209 -0
  96. package/src/commands/tech.js +209 -0
  97. package/src/commands/tenant.js +217 -0
  98. package/src/commands/terraform.js +206 -0
  99. package/src/commands/tms.js +171 -33
  100. package/src/commands/tokens.js +209 -0
  101. package/src/commands/topiccls.js +146 -33
  102. package/src/commands/trust.js +217 -0
  103. package/src/commands/uprof.js +141 -32
  104. package/src/commands/vcheck.js +122 -28
  105. package/src/commands/wallet.js +209 -0
  106. package/src/commands/webfetch.js +141 -32
  107. package/src/commands/workflow.js +203 -0
  108. package/src/commands/zkp.js +184 -28
  109. package/src/index.js +180 -0
  110. package/src/lib/a2a-protocol.js +319 -51
  111. package/src/lib/activitypub-bridge.js +622 -50
  112. package/src/lib/agent-economy.js +304 -51
  113. package/src/lib/agent-network.js +341 -0
  114. package/src/lib/aiops.js +346 -0
  115. package/src/lib/app-builder.js +279 -46
  116. package/src/lib/audit-logger.js +321 -0
  117. package/src/lib/automation-engine.js +335 -0
  118. package/src/lib/autonomous-agent.js +284 -48
  119. package/src/lib/autonomous-developer.js +332 -0
  120. package/src/lib/bi-engine.js +616 -49
  121. package/src/lib/bm25-search.js +301 -49
  122. package/src/lib/browser-automation.js +630 -49
  123. package/src/lib/chat-core.js +336 -0
  124. package/src/lib/claude-code-bridge.js +341 -0
  125. package/src/lib/code-agent.js +339 -0
  126. package/src/lib/collaboration-governance.js +334 -0
  127. package/src/lib/community-governance.js +346 -0
  128. package/src/lib/compliance-framework-reporter.js +305 -51
  129. package/src/lib/compliance-manager.js +330 -0
  130. package/src/lib/compression-telemetry.js +301 -49
  131. package/src/lib/content-recommendation.js +351 -0
  132. package/src/lib/content-recommender.js +317 -52
  133. package/src/lib/cowork-cron.js +298 -49
  134. package/src/lib/cowork-learning.js +333 -0
  135. package/src/lib/cowork-share.js +338 -0
  136. package/src/lib/cowork-task-runner.js +308 -51
  137. package/src/lib/cowork-workflow.js +327 -0
  138. package/src/lib/cross-chain.js +311 -51
  139. package/src/lib/dao-governance.js +280 -46
  140. package/src/lib/dbevo.js +351 -0
  141. package/src/lib/decentral-infra.js +330 -0
  142. package/src/lib/did-manager.js +341 -0
  143. package/src/lib/did-v2-manager.js +341 -0
  144. package/src/lib/dlp-engine.js +626 -49
  145. package/src/lib/evolution-system.js +278 -47
  146. package/src/lib/evomap-governance.js +334 -0
  147. package/src/lib/evomap-manager.js +280 -46
  148. package/src/lib/execution-backend.js +294 -48
  149. package/src/lib/feature-flags.js +294 -49
  150. package/src/lib/federation-hardening.js +340 -0
  151. package/src/lib/git-integration.js +285 -47
  152. package/src/lib/hardening-manager.js +341 -0
  153. package/src/lib/hierarchical-memory.js +284 -48
  154. package/src/lib/hook-manager.js +341 -0
  155. package/src/lib/inference-network.js +308 -51
  156. package/src/lib/instinct-manager.js +346 -0
  157. package/src/lib/ipfs-storage.js +334 -0
  158. package/src/lib/iteration-budget.js +302 -50
  159. package/src/lib/knowledge-exporter.js +341 -0
  160. package/src/lib/knowledge-graph.js +333 -0
  161. package/src/lib/knowledge-importer.js +341 -0
  162. package/src/lib/llm-providers.js +346 -0
  163. package/src/lib/matrix-bridge.js +620 -47
  164. package/src/lib/mcp-registry.js +346 -0
  165. package/src/lib/mcp-scaffold.js +318 -54
  166. package/src/lib/memory-injection.js +288 -49
  167. package/src/lib/memory-manager.js +336 -0
  168. package/src/lib/multimodal.js +330 -0
  169. package/src/lib/nl-programming.js +341 -0
  170. package/src/lib/nostr-bridge.js +622 -49
  171. package/src/lib/note-versioning.js +339 -0
  172. package/src/lib/orchestrator.js +293 -48
  173. package/src/lib/org-manager.js +336 -0
  174. package/src/lib/p2p-manager.js +341 -0
  175. package/src/lib/pdf-parser.js +298 -49
  176. package/src/lib/perception.js +346 -0
  177. package/src/lib/perf-tuning.js +309 -50
  178. package/src/lib/permanent-memory.js +327 -0
  179. package/src/lib/permission-engine.js +287 -49
  180. package/src/lib/pipeline-orchestrator.js +289 -49
  181. package/src/lib/plan-mode.js +298 -51
  182. package/src/lib/plugin-ecosystem.js +346 -0
  183. package/src/lib/pqc-manager.js +346 -0
  184. package/src/lib/privacy-computing.js +335 -0
  185. package/src/lib/protocol-fusion.js +338 -0
  186. package/src/lib/quantization.js +337 -0
  187. package/src/lib/reputation-optimizer.js +340 -0
  188. package/src/lib/response-cache.js +333 -0
  189. package/src/lib/sandbox-v2.js +327 -0
  190. package/src/lib/scim-manager.js +346 -0
  191. package/src/lib/service-container.js +313 -52
  192. package/src/lib/session-consolidator.js +296 -49
  193. package/src/lib/session-hooks.js +312 -53
  194. package/src/lib/session-manager.js +334 -0
  195. package/src/lib/session-search.js +304 -51
  196. package/src/lib/session-tail.js +288 -49
  197. package/src/lib/session-usage.js +298 -52
  198. package/src/lib/siem-exporter.js +298 -51
  199. package/src/lib/skill-loader.js +334 -0
  200. package/src/lib/skill-marketplace.js +345 -0
  201. package/src/lib/sla-manager.js +341 -0
  202. package/src/lib/slot-filler.js +303 -51
  203. package/src/lib/social-graph-analytics.js +295 -49
  204. package/src/lib/social-graph.js +272 -49
  205. package/src/lib/social-manager.js +330 -0
  206. package/src/lib/sso-manager.js +340 -0
  207. package/src/lib/stress-tester.js +342 -0
  208. package/src/lib/sub-agent-registry.js +302 -53
  209. package/src/lib/sync-manager.js +336 -0
  210. package/src/lib/task-model-selector.js +302 -50
  211. package/src/lib/tech-learning-engine.js +341 -0
  212. package/src/lib/tenant-saas.js +341 -0
  213. package/src/lib/terraform-manager.js +333 -0
  214. package/src/lib/threat-intel.js +330 -0
  215. package/src/lib/todo-manager.js +281 -47
  216. package/src/lib/token-incentive.js +341 -0
  217. package/src/lib/token-tracker.js +336 -0
  218. package/src/lib/topic-classifier.js +297 -49
  219. package/src/lib/trust-security.js +343 -0
  220. package/src/lib/ueba.js +340 -0
  221. package/src/lib/universal-runtime.js +330 -0
  222. package/src/lib/user-profile.js +294 -50
  223. package/src/lib/version-checker.js +304 -50
  224. package/src/lib/wallet-manager.js +336 -0
  225. package/src/lib/web-fetch.js +292 -51
  226. package/src/lib/workflow-engine.js +330 -0
  227. package/src/lib/zkp-engine.js +286 -49
@@ -321,83 +321,335 @@ export class BM25Search {
321
321
  }
322
322
  }
323
323
 
324
-
325
324
  // =====================================================================
326
325
  // BM25 Search V2 governance overlay
327
326
  // =====================================================================
328
- export const BM25_PROFILE_MATURITY_V2 = Object.freeze({ PENDING: "pending", ACTIVE: "active", STALE: "stale", ARCHIVED: "archived" });
329
- export const BM25_QUERY_LIFECYCLE_V2 = Object.freeze({ QUEUED: "queued", SEARCHING: "searching", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled" });
327
+ export const BM25_PROFILE_MATURITY_V2 = Object.freeze({
328
+ PENDING: "pending",
329
+ ACTIVE: "active",
330
+ STALE: "stale",
331
+ ARCHIVED: "archived",
332
+ });
333
+ export const BM25_QUERY_LIFECYCLE_V2 = Object.freeze({
334
+ QUEUED: "queued",
335
+ SEARCHING: "searching",
336
+ COMPLETED: "completed",
337
+ FAILED: "failed",
338
+ CANCELLED: "cancelled",
339
+ });
330
340
  const _bm25PTrans = new Map([
331
- [BM25_PROFILE_MATURITY_V2.PENDING, new Set([BM25_PROFILE_MATURITY_V2.ACTIVE, BM25_PROFILE_MATURITY_V2.ARCHIVED])],
332
- [BM25_PROFILE_MATURITY_V2.ACTIVE, new Set([BM25_PROFILE_MATURITY_V2.STALE, BM25_PROFILE_MATURITY_V2.ARCHIVED])],
333
- [BM25_PROFILE_MATURITY_V2.STALE, new Set([BM25_PROFILE_MATURITY_V2.ACTIVE, BM25_PROFILE_MATURITY_V2.ARCHIVED])],
341
+ [
342
+ BM25_PROFILE_MATURITY_V2.PENDING,
343
+ new Set([
344
+ BM25_PROFILE_MATURITY_V2.ACTIVE,
345
+ BM25_PROFILE_MATURITY_V2.ARCHIVED,
346
+ ]),
347
+ ],
348
+ [
349
+ BM25_PROFILE_MATURITY_V2.ACTIVE,
350
+ new Set([
351
+ BM25_PROFILE_MATURITY_V2.STALE,
352
+ BM25_PROFILE_MATURITY_V2.ARCHIVED,
353
+ ]),
354
+ ],
355
+ [
356
+ BM25_PROFILE_MATURITY_V2.STALE,
357
+ new Set([
358
+ BM25_PROFILE_MATURITY_V2.ACTIVE,
359
+ BM25_PROFILE_MATURITY_V2.ARCHIVED,
360
+ ]),
361
+ ],
334
362
  [BM25_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
335
363
  ]);
336
364
  const _bm25PTerminal = new Set([BM25_PROFILE_MATURITY_V2.ARCHIVED]);
337
365
  const _bm25JTrans = new Map([
338
- [BM25_QUERY_LIFECYCLE_V2.QUEUED, new Set([BM25_QUERY_LIFECYCLE_V2.SEARCHING, BM25_QUERY_LIFECYCLE_V2.CANCELLED])],
339
- [BM25_QUERY_LIFECYCLE_V2.SEARCHING, new Set([BM25_QUERY_LIFECYCLE_V2.COMPLETED, BM25_QUERY_LIFECYCLE_V2.FAILED, BM25_QUERY_LIFECYCLE_V2.CANCELLED])],
366
+ [
367
+ BM25_QUERY_LIFECYCLE_V2.QUEUED,
368
+ new Set([
369
+ BM25_QUERY_LIFECYCLE_V2.SEARCHING,
370
+ BM25_QUERY_LIFECYCLE_V2.CANCELLED,
371
+ ]),
372
+ ],
373
+ [
374
+ BM25_QUERY_LIFECYCLE_V2.SEARCHING,
375
+ new Set([
376
+ BM25_QUERY_LIFECYCLE_V2.COMPLETED,
377
+ BM25_QUERY_LIFECYCLE_V2.FAILED,
378
+ BM25_QUERY_LIFECYCLE_V2.CANCELLED,
379
+ ]),
380
+ ],
340
381
  [BM25_QUERY_LIFECYCLE_V2.COMPLETED, new Set()],
341
382
  [BM25_QUERY_LIFECYCLE_V2.FAILED, new Set()],
342
383
  [BM25_QUERY_LIFECYCLE_V2.CANCELLED, new Set()],
343
384
  ]);
344
385
  const _bm25PsV2 = new Map();
345
386
  const _bm25JsV2 = new Map();
346
- let _bm25MaxActive = 8, _bm25MaxPending = 20, _bm25IdleMs = 30 * 24 * 60 * 60 * 1000, _bm25StuckMs = 30 * 1000;
347
- function _bm25Pos(n, label) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${label} must be positive integer`); return v; }
348
- function _bm25CheckP(from, to) { const a = _bm25PTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid bm25 profile transition ${from} → ${to}`); }
349
- function _bm25CheckJ(from, to) { const a = _bm25JTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid bm25 query transition ${from} → ${to}`); }
350
- function _bm25CountActive(owner) { let c = 0; for (const p of _bm25PsV2.values()) if (p.owner === owner && p.status === BM25_PROFILE_MATURITY_V2.ACTIVE) c++; return c; }
351
- function _bm25CountPending(profileId) { let c = 0; for (const j of _bm25JsV2.values()) if (j.profileId === profileId && (j.status === BM25_QUERY_LIFECYCLE_V2.QUEUED || j.status === BM25_QUERY_LIFECYCLE_V2.SEARCHING)) c++; return c; }
352
- export function setMaxActiveBm25ProfilesPerOwnerV2(n) { _bm25MaxActive = _bm25Pos(n, "maxActiveBm25ProfilesPerOwner"); }
353
- export function getMaxActiveBm25ProfilesPerOwnerV2() { return _bm25MaxActive; }
354
- export function setMaxPendingBm25QueriesPerProfileV2(n) { _bm25MaxPending = _bm25Pos(n, "maxPendingBm25QueriesPerProfile"); }
355
- export function getMaxPendingBm25QueriesPerProfileV2() { return _bm25MaxPending; }
356
- export function setBm25ProfileIdleMsV2(n) { _bm25IdleMs = _bm25Pos(n, "bm25ProfileIdleMs"); }
357
- export function getBm25ProfileIdleMsV2() { return _bm25IdleMs; }
358
- export function setBm25QueryStuckMsV2(n) { _bm25StuckMs = _bm25Pos(n, "bm25QueryStuckMs"); }
359
- export function getBm25QueryStuckMsV2() { return _bm25StuckMs; }
360
- export function _resetStateBm25SearchV2() { _bm25PsV2.clear(); _bm25JsV2.clear(); _bm25MaxActive = 8; _bm25MaxPending = 20; _bm25IdleMs = 30 * 24 * 60 * 60 * 1000; _bm25StuckMs = 30 * 1000; }
387
+ let _bm25MaxActive = 8,
388
+ _bm25MaxPending = 20,
389
+ _bm25IdleMs = 30 * 24 * 60 * 60 * 1000,
390
+ _bm25StuckMs = 30 * 1000;
391
+ function _bm25Pos(n, label) {
392
+ const v = Math.floor(Number(n));
393
+ if (!Number.isFinite(v) || v <= 0)
394
+ throw new Error(`${label} must be positive integer`);
395
+ return v;
396
+ }
397
+ function _bm25CheckP(from, to) {
398
+ const a = _bm25PTrans.get(from);
399
+ if (!a || !a.has(to))
400
+ throw new Error(`invalid bm25 profile transition ${from} ${to}`);
401
+ }
402
+ function _bm25CheckJ(from, to) {
403
+ const a = _bm25JTrans.get(from);
404
+ if (!a || !a.has(to))
405
+ throw new Error(`invalid bm25 query transition ${from} → ${to}`);
406
+ }
407
+ function _bm25CountActive(owner) {
408
+ let c = 0;
409
+ for (const p of _bm25PsV2.values())
410
+ if (p.owner === owner && p.status === BM25_PROFILE_MATURITY_V2.ACTIVE) c++;
411
+ return c;
412
+ }
413
+ function _bm25CountPending(profileId) {
414
+ let c = 0;
415
+ for (const j of _bm25JsV2.values())
416
+ if (
417
+ j.profileId === profileId &&
418
+ (j.status === BM25_QUERY_LIFECYCLE_V2.QUEUED ||
419
+ j.status === BM25_QUERY_LIFECYCLE_V2.SEARCHING)
420
+ )
421
+ c++;
422
+ return c;
423
+ }
424
+ export function setMaxActiveBm25ProfilesPerOwnerV2(n) {
425
+ _bm25MaxActive = _bm25Pos(n, "maxActiveBm25ProfilesPerOwner");
426
+ }
427
+ export function getMaxActiveBm25ProfilesPerOwnerV2() {
428
+ return _bm25MaxActive;
429
+ }
430
+ export function setMaxPendingBm25QueriesPerProfileV2(n) {
431
+ _bm25MaxPending = _bm25Pos(n, "maxPendingBm25QueriesPerProfile");
432
+ }
433
+ export function getMaxPendingBm25QueriesPerProfileV2() {
434
+ return _bm25MaxPending;
435
+ }
436
+ export function setBm25ProfileIdleMsV2(n) {
437
+ _bm25IdleMs = _bm25Pos(n, "bm25ProfileIdleMs");
438
+ }
439
+ export function getBm25ProfileIdleMsV2() {
440
+ return _bm25IdleMs;
441
+ }
442
+ export function setBm25QueryStuckMsV2(n) {
443
+ _bm25StuckMs = _bm25Pos(n, "bm25QueryStuckMs");
444
+ }
445
+ export function getBm25QueryStuckMsV2() {
446
+ return _bm25StuckMs;
447
+ }
448
+ export function _resetStateBm25SearchV2() {
449
+ _bm25PsV2.clear();
450
+ _bm25JsV2.clear();
451
+ _bm25MaxActive = 8;
452
+ _bm25MaxPending = 20;
453
+ _bm25IdleMs = 30 * 24 * 60 * 60 * 1000;
454
+ _bm25StuckMs = 30 * 1000;
455
+ }
361
456
  export function registerBm25ProfileV2({ id, owner, field, metadata } = {}) {
362
457
  if (!id || !owner) throw new Error("id and owner required");
363
458
  if (_bm25PsV2.has(id)) throw new Error(`bm25 profile ${id} already exists`);
364
459
  const now = Date.now();
365
- const p = { id, owner, field: field || "content", status: BM25_PROFILE_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, lastTouchedAt: now, activatedAt: null, archivedAt: null, metadata: { ...(metadata || {}) } };
366
- _bm25PsV2.set(id, p); return { ...p, metadata: { ...p.metadata } };
460
+ const p = {
461
+ id,
462
+ owner,
463
+ field: field || "content",
464
+ status: BM25_PROFILE_MATURITY_V2.PENDING,
465
+ createdAt: now,
466
+ updatedAt: now,
467
+ lastTouchedAt: now,
468
+ activatedAt: null,
469
+ archivedAt: null,
470
+ metadata: { ...(metadata || {}) },
471
+ };
472
+ _bm25PsV2.set(id, p);
473
+ return { ...p, metadata: { ...p.metadata } };
367
474
  }
368
475
  export function activateBm25ProfileV2(id) {
369
- const p = _bm25PsV2.get(id); if (!p) throw new Error(`bm25 profile ${id} not found`);
476
+ const p = _bm25PsV2.get(id);
477
+ if (!p) throw new Error(`bm25 profile ${id} not found`);
370
478
  const isInitial = p.status === BM25_PROFILE_MATURITY_V2.PENDING;
371
479
  _bm25CheckP(p.status, BM25_PROFILE_MATURITY_V2.ACTIVE);
372
- if (isInitial && _bm25CountActive(p.owner) >= _bm25MaxActive) throw new Error(`max active bm25 profiles for owner ${p.owner} reached`);
373
- const now = Date.now(); p.status = BM25_PROFILE_MATURITY_V2.ACTIVE; p.updatedAt = now; p.lastTouchedAt = now;
480
+ if (isInitial && _bm25CountActive(p.owner) >= _bm25MaxActive)
481
+ throw new Error(`max active bm25 profiles for owner ${p.owner} reached`);
482
+ const now = Date.now();
483
+ p.status = BM25_PROFILE_MATURITY_V2.ACTIVE;
484
+ p.updatedAt = now;
485
+ p.lastTouchedAt = now;
374
486
  if (!p.activatedAt) p.activatedAt = now;
375
487
  return { ...p, metadata: { ...p.metadata } };
376
488
  }
377
- export function staleBm25ProfileV2(id) { const p = _bm25PsV2.get(id); if (!p) throw new Error(`bm25 profile ${id} not found`); _bm25CheckP(p.status, BM25_PROFILE_MATURITY_V2.STALE); p.status = BM25_PROFILE_MATURITY_V2.STALE; p.updatedAt = Date.now(); return { ...p, metadata: { ...p.metadata } }; }
378
- export function archiveBm25ProfileV2(id) { const p = _bm25PsV2.get(id); if (!p) throw new Error(`bm25 profile ${id} not found`); _bm25CheckP(p.status, BM25_PROFILE_MATURITY_V2.ARCHIVED); const now = Date.now(); p.status = BM25_PROFILE_MATURITY_V2.ARCHIVED; p.updatedAt = now; if (!p.archivedAt) p.archivedAt = now; return { ...p, metadata: { ...p.metadata } }; }
379
- export function touchBm25ProfileV2(id) { const p = _bm25PsV2.get(id); if (!p) throw new Error(`bm25 profile ${id} not found`); if (_bm25PTerminal.has(p.status)) throw new Error(`cannot touch terminal bm25 profile ${id}`); const now = Date.now(); p.lastTouchedAt = now; p.updatedAt = now; return { ...p, metadata: { ...p.metadata } }; }
380
- export function getBm25ProfileV2(id) { const p = _bm25PsV2.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
381
- export function listBm25ProfilesV2() { return [..._bm25PsV2.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
489
+ export function staleBm25ProfileV2(id) {
490
+ const p = _bm25PsV2.get(id);
491
+ if (!p) throw new Error(`bm25 profile ${id} not found`);
492
+ _bm25CheckP(p.status, BM25_PROFILE_MATURITY_V2.STALE);
493
+ p.status = BM25_PROFILE_MATURITY_V2.STALE;
494
+ p.updatedAt = Date.now();
495
+ return { ...p, metadata: { ...p.metadata } };
496
+ }
497
+ export function archiveBm25ProfileV2(id) {
498
+ const p = _bm25PsV2.get(id);
499
+ if (!p) throw new Error(`bm25 profile ${id} not found`);
500
+ _bm25CheckP(p.status, BM25_PROFILE_MATURITY_V2.ARCHIVED);
501
+ const now = Date.now();
502
+ p.status = BM25_PROFILE_MATURITY_V2.ARCHIVED;
503
+ p.updatedAt = now;
504
+ if (!p.archivedAt) p.archivedAt = now;
505
+ return { ...p, metadata: { ...p.metadata } };
506
+ }
507
+ export function touchBm25ProfileV2(id) {
508
+ const p = _bm25PsV2.get(id);
509
+ if (!p) throw new Error(`bm25 profile ${id} not found`);
510
+ if (_bm25PTerminal.has(p.status))
511
+ throw new Error(`cannot touch terminal bm25 profile ${id}`);
512
+ const now = Date.now();
513
+ p.lastTouchedAt = now;
514
+ p.updatedAt = now;
515
+ return { ...p, metadata: { ...p.metadata } };
516
+ }
517
+ export function getBm25ProfileV2(id) {
518
+ const p = _bm25PsV2.get(id);
519
+ if (!p) return null;
520
+ return { ...p, metadata: { ...p.metadata } };
521
+ }
522
+ export function listBm25ProfilesV2() {
523
+ return [..._bm25PsV2.values()].map((p) => ({
524
+ ...p,
525
+ metadata: { ...p.metadata },
526
+ }));
527
+ }
382
528
  export function createBm25QueryV2({ id, profileId, q, metadata } = {}) {
383
529
  if (!id || !profileId) throw new Error("id and profileId required");
384
530
  if (_bm25JsV2.has(id)) throw new Error(`bm25 query ${id} already exists`);
385
- if (!_bm25PsV2.has(profileId)) throw new Error(`bm25 profile ${profileId} not found`);
386
- if (_bm25CountPending(profileId) >= _bm25MaxPending) throw new Error(`max pending bm25 queries for profile ${profileId} reached`);
531
+ if (!_bm25PsV2.has(profileId))
532
+ throw new Error(`bm25 profile ${profileId} not found`);
533
+ if (_bm25CountPending(profileId) >= _bm25MaxPending)
534
+ throw new Error(
535
+ `max pending bm25 queries for profile ${profileId} reached`,
536
+ );
387
537
  const now = Date.now();
388
- const j = { id, profileId, q: q || "", status: BM25_QUERY_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
389
- _bm25JsV2.set(id, j); return { ...j, metadata: { ...j.metadata } };
390
- }
391
- export function searchingBm25QueryV2(id) { const j = _bm25JsV2.get(id); if (!j) throw new Error(`bm25 query ${id} not found`); _bm25CheckJ(j.status, BM25_QUERY_LIFECYCLE_V2.SEARCHING); const now = Date.now(); j.status = BM25_QUERY_LIFECYCLE_V2.SEARCHING; j.updatedAt = now; if (!j.startedAt) j.startedAt = now; return { ...j, metadata: { ...j.metadata } }; }
392
- export function completeBm25QueryV2(id) { const j = _bm25JsV2.get(id); if (!j) throw new Error(`bm25 query ${id} not found`); _bm25CheckJ(j.status, BM25_QUERY_LIFECYCLE_V2.COMPLETED); const now = Date.now(); j.status = BM25_QUERY_LIFECYCLE_V2.COMPLETED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; return { ...j, metadata: { ...j.metadata } }; }
393
- export function failBm25QueryV2(id, reason) { const j = _bm25JsV2.get(id); if (!j) throw new Error(`bm25 query ${id} not found`); _bm25CheckJ(j.status, BM25_QUERY_LIFECYCLE_V2.FAILED); const now = Date.now(); j.status = BM25_QUERY_LIFECYCLE_V2.FAILED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; if (reason) j.metadata.failReason = String(reason); return { ...j, metadata: { ...j.metadata } }; }
394
- export function cancelBm25QueryV2(id, reason) { const j = _bm25JsV2.get(id); if (!j) throw new Error(`bm25 query ${id} not found`); _bm25CheckJ(j.status, BM25_QUERY_LIFECYCLE_V2.CANCELLED); const now = Date.now(); j.status = BM25_QUERY_LIFECYCLE_V2.CANCELLED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; if (reason) j.metadata.cancelReason = String(reason); return { ...j, metadata: { ...j.metadata } }; }
395
- export function getBm25QueryV2(id) { const j = _bm25JsV2.get(id); if (!j) return null; return { ...j, metadata: { ...j.metadata } }; }
396
- export function listBm25QueriesV2() { return [..._bm25JsV2.values()].map((j) => ({ ...j, metadata: { ...j.metadata } })); }
397
- export function autoStaleIdleBm25ProfilesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _bm25PsV2.values()) if (p.status === BM25_PROFILE_MATURITY_V2.ACTIVE && (t - p.lastTouchedAt) >= _bm25IdleMs) { p.status = BM25_PROFILE_MATURITY_V2.STALE; p.updatedAt = t; flipped.push(p.id); } return { flipped, count: flipped.length }; }
398
- export function autoFailStuckBm25QueriesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const j of _bm25JsV2.values()) if (j.status === BM25_QUERY_LIFECYCLE_V2.SEARCHING && j.startedAt != null && (t - j.startedAt) >= _bm25StuckMs) { j.status = BM25_QUERY_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 }; }
538
+ const j = {
539
+ id,
540
+ profileId,
541
+ q: q || "",
542
+ status: BM25_QUERY_LIFECYCLE_V2.QUEUED,
543
+ createdAt: now,
544
+ updatedAt: now,
545
+ startedAt: null,
546
+ settledAt: null,
547
+ metadata: { ...(metadata || {}) },
548
+ };
549
+ _bm25JsV2.set(id, j);
550
+ return { ...j, metadata: { ...j.metadata } };
551
+ }
552
+ export function searchingBm25QueryV2(id) {
553
+ const j = _bm25JsV2.get(id);
554
+ if (!j) throw new Error(`bm25 query ${id} not found`);
555
+ _bm25CheckJ(j.status, BM25_QUERY_LIFECYCLE_V2.SEARCHING);
556
+ const now = Date.now();
557
+ j.status = BM25_QUERY_LIFECYCLE_V2.SEARCHING;
558
+ j.updatedAt = now;
559
+ if (!j.startedAt) j.startedAt = now;
560
+ return { ...j, metadata: { ...j.metadata } };
561
+ }
562
+ export function completeBm25QueryV2(id) {
563
+ const j = _bm25JsV2.get(id);
564
+ if (!j) throw new Error(`bm25 query ${id} not found`);
565
+ _bm25CheckJ(j.status, BM25_QUERY_LIFECYCLE_V2.COMPLETED);
566
+ const now = Date.now();
567
+ j.status = BM25_QUERY_LIFECYCLE_V2.COMPLETED;
568
+ j.updatedAt = now;
569
+ if (!j.settledAt) j.settledAt = now;
570
+ return { ...j, metadata: { ...j.metadata } };
571
+ }
572
+ export function failBm25QueryV2(id, reason) {
573
+ const j = _bm25JsV2.get(id);
574
+ if (!j) throw new Error(`bm25 query ${id} not found`);
575
+ _bm25CheckJ(j.status, BM25_QUERY_LIFECYCLE_V2.FAILED);
576
+ const now = Date.now();
577
+ j.status = BM25_QUERY_LIFECYCLE_V2.FAILED;
578
+ j.updatedAt = now;
579
+ if (!j.settledAt) j.settledAt = now;
580
+ if (reason) j.metadata.failReason = String(reason);
581
+ return { ...j, metadata: { ...j.metadata } };
582
+ }
583
+ export function cancelBm25QueryV2(id, reason) {
584
+ const j = _bm25JsV2.get(id);
585
+ if (!j) throw new Error(`bm25 query ${id} not found`);
586
+ _bm25CheckJ(j.status, BM25_QUERY_LIFECYCLE_V2.CANCELLED);
587
+ const now = Date.now();
588
+ j.status = BM25_QUERY_LIFECYCLE_V2.CANCELLED;
589
+ j.updatedAt = now;
590
+ if (!j.settledAt) j.settledAt = now;
591
+ if (reason) j.metadata.cancelReason = String(reason);
592
+ return { ...j, metadata: { ...j.metadata } };
593
+ }
594
+ export function getBm25QueryV2(id) {
595
+ const j = _bm25JsV2.get(id);
596
+ if (!j) return null;
597
+ return { ...j, metadata: { ...j.metadata } };
598
+ }
599
+ export function listBm25QueriesV2() {
600
+ return [..._bm25JsV2.values()].map((j) => ({
601
+ ...j,
602
+ metadata: { ...j.metadata },
603
+ }));
604
+ }
605
+ export function autoStaleIdleBm25ProfilesV2({ now } = {}) {
606
+ const t = now ?? Date.now();
607
+ const flipped = [];
608
+ for (const p of _bm25PsV2.values())
609
+ if (
610
+ p.status === BM25_PROFILE_MATURITY_V2.ACTIVE &&
611
+ t - p.lastTouchedAt >= _bm25IdleMs
612
+ ) {
613
+ p.status = BM25_PROFILE_MATURITY_V2.STALE;
614
+ p.updatedAt = t;
615
+ flipped.push(p.id);
616
+ }
617
+ return { flipped, count: flipped.length };
618
+ }
619
+ export function autoFailStuckBm25QueriesV2({ now } = {}) {
620
+ const t = now ?? Date.now();
621
+ const flipped = [];
622
+ for (const j of _bm25JsV2.values())
623
+ if (
624
+ j.status === BM25_QUERY_LIFECYCLE_V2.SEARCHING &&
625
+ j.startedAt != null &&
626
+ t - j.startedAt >= _bm25StuckMs
627
+ ) {
628
+ j.status = BM25_QUERY_LIFECYCLE_V2.FAILED;
629
+ j.updatedAt = t;
630
+ if (!j.settledAt) j.settledAt = t;
631
+ j.metadata.failReason = "auto-fail-stuck";
632
+ flipped.push(j.id);
633
+ }
634
+ return { flipped, count: flipped.length };
635
+ }
399
636
  export function getBm25SearchGovStatsV2() {
400
- const profilesByStatus = {}; for (const v of Object.values(BM25_PROFILE_MATURITY_V2)) profilesByStatus[v] = 0; for (const p of _bm25PsV2.values()) profilesByStatus[p.status]++;
401
- const queriesByStatus = {}; for (const v of Object.values(BM25_QUERY_LIFECYCLE_V2)) queriesByStatus[v] = 0; for (const j of _bm25JsV2.values()) queriesByStatus[j.status]++;
402
- return { totalBm25ProfilesV2: _bm25PsV2.size, totalBm25QueriesV2: _bm25JsV2.size, maxActiveBm25ProfilesPerOwner: _bm25MaxActive, maxPendingBm25QueriesPerProfile: _bm25MaxPending, bm25ProfileIdleMs: _bm25IdleMs, bm25QueryStuckMs: _bm25StuckMs, profilesByStatus, queriesByStatus };
637
+ const profilesByStatus = {};
638
+ for (const v of Object.values(BM25_PROFILE_MATURITY_V2))
639
+ profilesByStatus[v] = 0;
640
+ for (const p of _bm25PsV2.values()) profilesByStatus[p.status]++;
641
+ const queriesByStatus = {};
642
+ for (const v of Object.values(BM25_QUERY_LIFECYCLE_V2))
643
+ queriesByStatus[v] = 0;
644
+ for (const j of _bm25JsV2.values()) queriesByStatus[j.status]++;
645
+ return {
646
+ totalBm25ProfilesV2: _bm25PsV2.size,
647
+ totalBm25QueriesV2: _bm25JsV2.size,
648
+ maxActiveBm25ProfilesPerOwner: _bm25MaxActive,
649
+ maxPendingBm25QueriesPerProfile: _bm25MaxPending,
650
+ bm25ProfileIdleMs: _bm25IdleMs,
651
+ bm25QueryStuckMs: _bm25StuckMs,
652
+ profilesByStatus,
653
+ queriesByStatus,
654
+ };
403
655
  }