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.
- package/bin/chainlesschain.js +0 -0
- package/package.json +1 -1
- package/src/commands/a2a.js +62 -0
- package/src/commands/activitypub.js +61 -0
- package/src/commands/agent-network.js +254 -1
- package/src/commands/agent.js +117 -0
- package/src/commands/audit.js +302 -0
- package/src/commands/automation.js +271 -1
- package/src/commands/bi.js +61 -0
- package/src/commands/bm25.js +78 -0
- package/src/commands/browse.js +64 -0
- package/src/commands/ccron.js +78 -0
- package/src/commands/codegen.js +224 -0
- package/src/commands/collab.js +341 -0
- package/src/commands/compliance.js +1075 -0
- package/src/commands/compt.js +78 -0
- package/src/commands/consol.js +231 -0
- package/src/commands/cowork.js +263 -0
- package/src/commands/crosschain.js +62 -0
- package/src/commands/dao.js +62 -0
- package/src/commands/dbevo.js +284 -0
- package/src/commands/dev.js +252 -0
- package/src/commands/did.js +358 -0
- package/src/commands/dlp.js +61 -0
- package/src/commands/economy.js +56 -0
- package/src/commands/encrypt.js +341 -0
- package/src/commands/evolution.js +56 -0
- package/src/commands/evomap.js +61 -0
- package/src/commands/export.js +256 -1
- package/src/commands/fflag.js +178 -0
- package/src/commands/fusion.js +258 -0
- package/src/commands/git.js +45 -0
- package/src/commands/governance.js +325 -0
- package/src/commands/hardening.js +411 -0
- package/src/commands/hmemory.js +56 -0
- package/src/commands/hook.js +148 -0
- package/src/commands/import.js +252 -0
- package/src/commands/incentive.js +322 -0
- package/src/commands/inference.js +42 -0
- package/src/commands/infra.js +244 -0
- package/src/commands/instinct.js +260 -0
- package/src/commands/ipfs.js +318 -0
- package/src/commands/itbudget.js +45 -0
- package/src/commands/kg.js +387 -0
- package/src/commands/llm.js +263 -0
- package/src/commands/lowcode.js +44 -0
- package/src/commands/matrix.js +62 -0
- package/src/commands/mcp.js +221 -0
- package/src/commands/mcpscaf.js +41 -0
- package/src/commands/meminj.js +41 -0
- package/src/commands/memory.js +248 -0
- package/src/commands/multimodal.js +296 -0
- package/src/commands/nlprog.js +356 -0
- package/src/commands/nostr.js +62 -0
- package/src/commands/note.js +244 -0
- package/src/commands/ops.js +354 -0
- package/src/commands/orchestrate.js +166 -0
- package/src/commands/orchgov.js +45 -0
- package/src/commands/org.js +277 -0
- package/src/commands/p2p.js +390 -0
- package/src/commands/pdfp.js +78 -0
- package/src/commands/perception.js +290 -0
- package/src/commands/perf.js +39 -0
- package/src/commands/perm.js +45 -0
- package/src/commands/permmem.js +251 -0
- package/src/commands/pipeline.js +57 -1
- package/src/commands/planmode.js +45 -0
- package/src/commands/plugin-ecosystem.js +273 -0
- package/src/commands/pqc.js +393 -0
- package/src/commands/promcomp.js +82 -0
- package/src/commands/quantization.js +351 -0
- package/src/commands/rcache.js +271 -0
- package/src/commands/recommend.js +382 -0
- package/src/commands/runtime.js +307 -0
- package/src/commands/scim.js +262 -0
- package/src/commands/seshhook.js +41 -0
- package/src/commands/seshsearch.js +41 -0
- package/src/commands/seshtail.js +41 -0
- package/src/commands/seshu.js +41 -0
- package/src/commands/session.js +258 -0
- package/src/commands/sganal.js +78 -0
- package/src/commands/siem.js +40 -0
- package/src/commands/skill.js +267 -1
- package/src/commands/slotfill.js +41 -0
- package/src/commands/social.js +290 -0
- package/src/commands/sso.js +186 -1
- package/src/commands/svccont.js +45 -0
- package/src/commands/sync.js +256 -0
- package/src/commands/tech.js +338 -0
- package/src/commands/tenant.js +351 -0
- package/src/commands/tms.js +45 -0
- package/src/commands/tokens.js +269 -0
- package/src/commands/topiccls.js +45 -0
- package/src/commands/trust.js +249 -0
- package/src/commands/uprof.js +45 -0
- package/src/commands/vcheck.js +78 -0
- package/src/commands/wallet.js +277 -0
- package/src/commands/webfetch.js +41 -0
- package/src/commands/workflow.js +171 -0
- package/src/commands/zkp.js +62 -0
- package/src/harness/prompt-compressor.js +331 -0
- package/src/index.js +65 -1
- package/src/lib/a2a-protocol.js +105 -0
- package/src/lib/activitypub-bridge.js +105 -0
- package/src/lib/agent-coordinator.js +325 -0
- package/src/lib/agent-economy.js +105 -0
- package/src/lib/agent-network.js +387 -0
- package/src/lib/agent-router.js +395 -0
- package/src/lib/aiops.js +478 -0
- package/src/lib/app-builder.js +105 -0
- package/src/lib/audit-logger.js +379 -0
- package/src/lib/automation-engine.js +330 -0
- package/src/lib/autonomous-agent.js +105 -0
- package/src/lib/autonomous-developer.js +350 -0
- package/src/lib/bi-engine.js +105 -0
- package/src/lib/bm25-search.js +81 -0
- package/src/lib/browser-automation.js +105 -0
- package/src/lib/code-agent.js +323 -0
- package/src/lib/collaboration-governance.js +364 -0
- package/src/lib/community-governance.js +436 -0
- package/src/lib/compliance-framework-reporter.js +105 -0
- package/src/lib/compliance-manager.js +434 -0
- package/src/lib/compression-telemetry.js +81 -0
- package/src/lib/content-recommendation.js +469 -0
- package/src/lib/content-recommender.js +105 -0
- package/src/lib/cowork-cron.js +81 -0
- package/src/lib/cowork-task-runner.js +105 -0
- package/src/lib/cross-chain.js +105 -0
- package/src/lib/crypto-manager.js +350 -0
- package/src/lib/dao-governance.js +105 -0
- package/src/lib/dbevo.js +338 -0
- package/src/lib/decentral-infra.js +340 -0
- package/src/lib/did-manager.js +367 -0
- package/src/lib/dlp-engine.js +105 -0
- package/src/lib/evolution-system.js +105 -0
- package/src/lib/evomap-manager.js +105 -0
- package/src/lib/execution-backend.js +105 -0
- package/src/lib/feature-flags.js +85 -0
- package/src/lib/git-integration.js +105 -0
- package/src/lib/hardening-manager.js +348 -0
- package/src/lib/hierarchical-memory.js +105 -0
- package/src/lib/hook-manager.js +380 -0
- package/src/lib/inference-network.js +105 -0
- package/src/lib/instinct-manager.js +332 -0
- package/src/lib/ipfs-storage.js +334 -0
- package/src/lib/iteration-budget.js +105 -0
- package/src/lib/knowledge-exporter.js +381 -0
- package/src/lib/knowledge-graph.js +432 -0
- package/src/lib/knowledge-importer.js +379 -0
- package/src/lib/llm-providers.js +391 -0
- package/src/lib/matrix-bridge.js +105 -0
- package/src/lib/mcp-registry.js +333 -0
- package/src/lib/mcp-scaffold.js +81 -0
- package/src/lib/memory-injection.js +81 -0
- package/src/lib/memory-manager.js +330 -0
- package/src/lib/multimodal.js +346 -0
- package/src/lib/nl-programming.js +343 -0
- package/src/lib/nostr-bridge.js +105 -0
- package/src/lib/note-versioning.js +327 -0
- package/src/lib/orchestrator.js +105 -0
- package/src/lib/org-manager.js +323 -0
- package/src/lib/p2p-manager.js +387 -0
- package/src/lib/pdf-parser.js +81 -0
- package/src/lib/perception.js +346 -0
- package/src/lib/perf-tuning.js +109 -1
- package/src/lib/permanent-memory.js +320 -0
- package/src/lib/permission-engine.js +81 -0
- package/src/lib/pipeline-orchestrator.js +105 -0
- package/src/lib/plan-mode.js +81 -0
- package/src/lib/plugin-ecosystem.js +377 -0
- package/src/lib/pqc-manager.js +368 -0
- package/src/lib/prompt-compressor.js +1 -10
- package/src/lib/protocol-fusion.js +417 -0
- package/src/lib/quantization.js +325 -0
- package/src/lib/response-cache.js +327 -0
- package/src/lib/scim-manager.js +329 -0
- package/src/lib/service-container.js +81 -0
- package/src/lib/session-consolidator.js +105 -0
- package/src/lib/session-hooks.js +81 -0
- package/src/lib/session-manager.js +329 -0
- package/src/lib/session-search.js +81 -0
- package/src/lib/session-tail.js +81 -0
- package/src/lib/session-usage.js +83 -0
- package/src/lib/siem-exporter.js +105 -0
- package/src/lib/skill-loader.js +377 -0
- package/src/lib/slot-filler.js +81 -0
- package/src/lib/social-graph-analytics.js +81 -0
- package/src/lib/social-graph.js +81 -0
- package/src/lib/social-manager.js +326 -0
- package/src/lib/sso-manager.js +332 -0
- package/src/lib/sub-agent-registry.js +110 -0
- package/src/lib/sync-manager.js +326 -0
- package/src/lib/task-model-selector.js +81 -0
- package/src/lib/tech-learning-engine.js +369 -0
- package/src/lib/tenant-saas.js +460 -0
- package/src/lib/threat-intel.js +335 -0
- package/src/lib/todo-manager.js +105 -0
- package/src/lib/token-incentive.js +293 -0
- package/src/lib/token-tracker.js +329 -0
- package/src/lib/topic-classifier.js +105 -0
- package/src/lib/trust-security.js +390 -0
- package/src/lib/ueba.js +389 -0
- package/src/lib/universal-runtime.js +325 -0
- package/src/lib/user-profile.js +81 -0
- package/src/lib/version-checker.js +81 -0
- package/src/lib/wallet-manager.js +326 -0
- package/src/lib/web-fetch.js +81 -0
- package/src/lib/workflow-engine.js +322 -0
- package/src/lib/zkp-engine.js +105 -0
|
@@ -492,3 +492,108 @@ Reply with a JSON object: { "action": "retry|add_step|skip", "newParams": {...},
|
|
|
492
492
|
return null;
|
|
493
493
|
}
|
|
494
494
|
}
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
// ===== V2 Surface: Autonomous Agent governance overlay (CLI v0.138.0) =====
|
|
498
|
+
export const AUTOAGENT_MATURITY_V2 = Object.freeze({
|
|
499
|
+
PENDING: "pending", ACTIVE: "active", PAUSED: "paused", ARCHIVED: "archived",
|
|
500
|
+
});
|
|
501
|
+
export const AUTOAGENT_RUN_LIFECYCLE_V2 = Object.freeze({
|
|
502
|
+
QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
const _aaTrans = new Map([
|
|
506
|
+
[AUTOAGENT_MATURITY_V2.PENDING, new Set([AUTOAGENT_MATURITY_V2.ACTIVE, AUTOAGENT_MATURITY_V2.ARCHIVED])],
|
|
507
|
+
[AUTOAGENT_MATURITY_V2.ACTIVE, new Set([AUTOAGENT_MATURITY_V2.PAUSED, AUTOAGENT_MATURITY_V2.ARCHIVED])],
|
|
508
|
+
[AUTOAGENT_MATURITY_V2.PAUSED, new Set([AUTOAGENT_MATURITY_V2.ACTIVE, AUTOAGENT_MATURITY_V2.ARCHIVED])],
|
|
509
|
+
[AUTOAGENT_MATURITY_V2.ARCHIVED, new Set()],
|
|
510
|
+
]);
|
|
511
|
+
const _aaTerminal = new Set([AUTOAGENT_MATURITY_V2.ARCHIVED]);
|
|
512
|
+
const _aaRunTrans = new Map([
|
|
513
|
+
[AUTOAGENT_RUN_LIFECYCLE_V2.QUEUED, new Set([AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING, AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED])],
|
|
514
|
+
[AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING, new Set([AUTOAGENT_RUN_LIFECYCLE_V2.COMPLETED, AUTOAGENT_RUN_LIFECYCLE_V2.FAILED, AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED])],
|
|
515
|
+
[AUTOAGENT_RUN_LIFECYCLE_V2.COMPLETED, new Set()],
|
|
516
|
+
[AUTOAGENT_RUN_LIFECYCLE_V2.FAILED, new Set()],
|
|
517
|
+
[AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
518
|
+
]);
|
|
519
|
+
|
|
520
|
+
const _aaAgents = new Map();
|
|
521
|
+
const _aaRuns = new Map();
|
|
522
|
+
let _aaMaxActivePerOwner = 5;
|
|
523
|
+
let _aaMaxPendingRunsPerAgent = 10;
|
|
524
|
+
let _aaAgentIdleMs = 7 * 24 * 60 * 60 * 1000;
|
|
525
|
+
let _aaRunStuckMs = 30 * 60 * 1000;
|
|
526
|
+
|
|
527
|
+
function _aaPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
|
|
528
|
+
|
|
529
|
+
export function setMaxActiveAutoAgentsPerOwnerV2(n) { _aaMaxActivePerOwner = _aaPos(n, "maxActiveAutoAgentsPerOwner"); }
|
|
530
|
+
export function getMaxActiveAutoAgentsPerOwnerV2() { return _aaMaxActivePerOwner; }
|
|
531
|
+
export function setMaxPendingAutoAgentRunsPerAgentV2(n) { _aaMaxPendingRunsPerAgent = _aaPos(n, "maxPendingAutoAgentRunsPerAgent"); }
|
|
532
|
+
export function getMaxPendingAutoAgentRunsPerAgentV2() { return _aaMaxPendingRunsPerAgent; }
|
|
533
|
+
export function setAutoAgentIdleMsV2(n) { _aaAgentIdleMs = _aaPos(n, "autoAgentIdleMs"); }
|
|
534
|
+
export function getAutoAgentIdleMsV2() { return _aaAgentIdleMs; }
|
|
535
|
+
export function setAutoAgentRunStuckMsV2(n) { _aaRunStuckMs = _aaPos(n, "autoAgentRunStuckMs"); }
|
|
536
|
+
export function getAutoAgentRunStuckMsV2() { return _aaRunStuckMs; }
|
|
537
|
+
|
|
538
|
+
export function _resetStateAutonomousAgentV2() {
|
|
539
|
+
_aaAgents.clear(); _aaRuns.clear();
|
|
540
|
+
_aaMaxActivePerOwner = 5; _aaMaxPendingRunsPerAgent = 10;
|
|
541
|
+
_aaAgentIdleMs = 7 * 24 * 60 * 60 * 1000; _aaRunStuckMs = 30 * 60 * 1000;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
export function registerAutoAgentV2({ id, owner, goal, metadata } = {}) {
|
|
545
|
+
if (!id || typeof id !== "string") throw new Error("id is required");
|
|
546
|
+
if (!owner || typeof owner !== "string") throw new Error("owner is required");
|
|
547
|
+
if (_aaAgents.has(id)) throw new Error(`auto-agent ${id} already registered`);
|
|
548
|
+
const now = Date.now();
|
|
549
|
+
const a = { id, owner, goal: goal || "", status: AUTOAGENT_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
|
|
550
|
+
_aaAgents.set(id, a);
|
|
551
|
+
return { ...a, metadata: { ...a.metadata } };
|
|
552
|
+
}
|
|
553
|
+
function _aaCheckA(from, to) { const a = _aaTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid auto-agent transition ${from} → ${to}`); }
|
|
554
|
+
function _aaCountActive(owner) { let n = 0; for (const a of _aaAgents.values()) if (a.owner === owner && a.status === AUTOAGENT_MATURITY_V2.ACTIVE) n++; return n; }
|
|
555
|
+
|
|
556
|
+
export function activateAutoAgentV2(id) {
|
|
557
|
+
const a = _aaAgents.get(id); if (!a) throw new Error(`auto-agent ${id} not found`);
|
|
558
|
+
_aaCheckA(a.status, AUTOAGENT_MATURITY_V2.ACTIVE);
|
|
559
|
+
const recovery = a.status === AUTOAGENT_MATURITY_V2.PAUSED;
|
|
560
|
+
if (!recovery) { const c = _aaCountActive(a.owner); if (c >= _aaMaxActivePerOwner) throw new Error(`max active auto-agents per owner (${_aaMaxActivePerOwner}) reached for ${a.owner}`); }
|
|
561
|
+
const now = Date.now(); a.status = AUTOAGENT_MATURITY_V2.ACTIVE; a.updatedAt = now; a.lastTouchedAt = now; if (!a.activatedAt) a.activatedAt = now;
|
|
562
|
+
return { ...a, metadata: { ...a.metadata } };
|
|
563
|
+
}
|
|
564
|
+
export function pauseAutoAgentV2(id) { const a = _aaAgents.get(id); if (!a) throw new Error(`auto-agent ${id} not found`); _aaCheckA(a.status, AUTOAGENT_MATURITY_V2.PAUSED); a.status = AUTOAGENT_MATURITY_V2.PAUSED; a.updatedAt = Date.now(); return { ...a, metadata: { ...a.metadata } }; }
|
|
565
|
+
export function archiveAutoAgentV2(id) { const a = _aaAgents.get(id); if (!a) throw new Error(`auto-agent ${id} not found`); _aaCheckA(a.status, AUTOAGENT_MATURITY_V2.ARCHIVED); const now = Date.now(); a.status = AUTOAGENT_MATURITY_V2.ARCHIVED; a.updatedAt = now; if (!a.archivedAt) a.archivedAt = now; return { ...a, metadata: { ...a.metadata } }; }
|
|
566
|
+
export function touchAutoAgentV2(id) { const a = _aaAgents.get(id); if (!a) throw new Error(`auto-agent ${id} not found`); if (_aaTerminal.has(a.status)) throw new Error(`cannot touch terminal auto-agent ${id}`); const now = Date.now(); a.lastTouchedAt = now; a.updatedAt = now; return { ...a, metadata: { ...a.metadata } }; }
|
|
567
|
+
export function getAutoAgentV2(id) { const a = _aaAgents.get(id); if (!a) return null; return { ...a, metadata: { ...a.metadata } }; }
|
|
568
|
+
export function listAutoAgentsV2() { return [..._aaAgents.values()].map((a) => ({ ...a, metadata: { ...a.metadata } })); }
|
|
569
|
+
|
|
570
|
+
function _aaCountPendingRuns(aid) { let n = 0; for (const r of _aaRuns.values()) if (r.agentId === aid && (r.status === AUTOAGENT_RUN_LIFECYCLE_V2.QUEUED || r.status === AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING)) n++; return n; }
|
|
571
|
+
|
|
572
|
+
export function createAutoAgentRunV2({ id, agentId, prompt, metadata } = {}) {
|
|
573
|
+
if (!id || typeof id !== "string") throw new Error("id is required");
|
|
574
|
+
if (!agentId || typeof agentId !== "string") throw new Error("agentId is required");
|
|
575
|
+
if (_aaRuns.has(id)) throw new Error(`auto-agent run ${id} already exists`);
|
|
576
|
+
if (!_aaAgents.has(agentId)) throw new Error(`auto-agent ${agentId} not found`);
|
|
577
|
+
const pending = _aaCountPendingRuns(agentId);
|
|
578
|
+
if (pending >= _aaMaxPendingRunsPerAgent) throw new Error(`max pending auto-agent runs per agent (${_aaMaxPendingRunsPerAgent}) reached for ${agentId}`);
|
|
579
|
+
const now = Date.now();
|
|
580
|
+
const r = { id, agentId, prompt: prompt || "", status: AUTOAGENT_RUN_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
|
|
581
|
+
_aaRuns.set(id, r);
|
|
582
|
+
return { ...r, metadata: { ...r.metadata } };
|
|
583
|
+
}
|
|
584
|
+
function _aaCheckR(from, to) { const a = _aaRunTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid auto-agent run transition ${from} → ${to}`); }
|
|
585
|
+
export function startAutoAgentRunV2(id) { const r = _aaRuns.get(id); if (!r) throw new Error(`auto-agent run ${id} not found`); _aaCheckR(r.status, AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING); const now = Date.now(); r.status = AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING; r.updatedAt = now; if (!r.startedAt) r.startedAt = now; return { ...r, metadata: { ...r.metadata } }; }
|
|
586
|
+
export function completeAutoAgentRunV2(id) { const r = _aaRuns.get(id); if (!r) throw new Error(`auto-agent run ${id} not found`); _aaCheckR(r.status, AUTOAGENT_RUN_LIFECYCLE_V2.COMPLETED); const now = Date.now(); r.status = AUTOAGENT_RUN_LIFECYCLE_V2.COMPLETED; r.updatedAt = now; if (!r.settledAt) r.settledAt = now; return { ...r, metadata: { ...r.metadata } }; }
|
|
587
|
+
export function failAutoAgentRunV2(id, reason) { const r = _aaRuns.get(id); if (!r) throw new Error(`auto-agent run ${id} not found`); _aaCheckR(r.status, AUTOAGENT_RUN_LIFECYCLE_V2.FAILED); const now = Date.now(); r.status = AUTOAGENT_RUN_LIFECYCLE_V2.FAILED; r.updatedAt = now; if (!r.settledAt) r.settledAt = now; if (reason) r.metadata.failReason = String(reason); return { ...r, metadata: { ...r.metadata } }; }
|
|
588
|
+
export function cancelAutoAgentRunV2(id, reason) { const r = _aaRuns.get(id); if (!r) throw new Error(`auto-agent run ${id} not found`); _aaCheckR(r.status, AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED); const now = Date.now(); r.status = AUTOAGENT_RUN_LIFECYCLE_V2.CANCELLED; r.updatedAt = now; if (!r.settledAt) r.settledAt = now; if (reason) r.metadata.cancelReason = String(reason); return { ...r, metadata: { ...r.metadata } }; }
|
|
589
|
+
export function getAutoAgentRunV2(id) { const r = _aaRuns.get(id); if (!r) return null; return { ...r, metadata: { ...r.metadata } }; }
|
|
590
|
+
export function listAutoAgentRunsV2() { return [..._aaRuns.values()].map((r) => ({ ...r, metadata: { ...r.metadata } })); }
|
|
591
|
+
|
|
592
|
+
export function autoPauseIdleAutoAgentsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const a of _aaAgents.values()) if (a.status === AUTOAGENT_MATURITY_V2.ACTIVE && (t - a.lastTouchedAt) >= _aaAgentIdleMs) { a.status = AUTOAGENT_MATURITY_V2.PAUSED; a.updatedAt = t; flipped.push(a.id); } return { flipped, count: flipped.length }; }
|
|
593
|
+
export function autoFailStuckAutoAgentRunsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const r of _aaRuns.values()) if (r.status === AUTOAGENT_RUN_LIFECYCLE_V2.RUNNING && r.startedAt != null && (t - r.startedAt) >= _aaRunStuckMs) { r.status = AUTOAGENT_RUN_LIFECYCLE_V2.FAILED; r.updatedAt = t; if (!r.settledAt) r.settledAt = t; r.metadata.failReason = "auto-fail-stuck"; flipped.push(r.id); } return { flipped, count: flipped.length }; }
|
|
594
|
+
|
|
595
|
+
export function getAutonomousAgentGovStatsV2() {
|
|
596
|
+
const agentsByStatus = {}; for (const s of Object.values(AUTOAGENT_MATURITY_V2)) agentsByStatus[s] = 0; for (const a of _aaAgents.values()) agentsByStatus[a.status]++;
|
|
597
|
+
const runsByStatus = {}; for (const s of Object.values(AUTOAGENT_RUN_LIFECYCLE_V2)) runsByStatus[s] = 0; for (const r of _aaRuns.values()) runsByStatus[r.status]++;
|
|
598
|
+
return { totalAutoAgentsV2: _aaAgents.size, totalAutoAgentRunsV2: _aaRuns.size, maxActiveAutoAgentsPerOwner: _aaMaxActivePerOwner, maxPendingAutoAgentRunsPerAgent: _aaMaxPendingRunsPerAgent, autoAgentIdleMs: _aaAgentIdleMs, autoAgentRunStuckMs: _aaRunStuckMs, agentsByStatus, runsByStatus };
|
|
599
|
+
}
|
|
@@ -522,3 +522,353 @@ export function _resetState() {
|
|
|
522
522
|
_adrs.clear();
|
|
523
523
|
_seq = 0;
|
|
524
524
|
}
|
|
525
|
+
|
|
526
|
+
/* ── V2 Surface (Autonomous Developer) ─────────────────────
|
|
527
|
+
* Strictly additive. Two parallel state machines:
|
|
528
|
+
* - ADR maturity (4 states, superseded terminal)
|
|
529
|
+
* - Dev session V2 lifecycle (5 states, 3 terminals)
|
|
530
|
+
* Per-author active-ADR cap + per-developer running-session cap.
|
|
531
|
+
* Auto-flip: stale draft ADRs → superseded; stuck running sessions → failed.
|
|
532
|
+
*/
|
|
533
|
+
|
|
534
|
+
export const ADR_MATURITY_V2 = Object.freeze({
|
|
535
|
+
DRAFT: "draft",
|
|
536
|
+
ACCEPTED: "accepted",
|
|
537
|
+
DEPRECATED: "deprecated",
|
|
538
|
+
SUPERSEDED: "superseded",
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
export const DEV_SESSION_V2 = Object.freeze({
|
|
542
|
+
QUEUED: "queued",
|
|
543
|
+
RUNNING: "running",
|
|
544
|
+
COMPLETED: "completed",
|
|
545
|
+
FAILED: "failed",
|
|
546
|
+
CANCELED: "canceled",
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
const _ADR_TRANSITIONS_V2 = new Map([
|
|
550
|
+
[
|
|
551
|
+
ADR_MATURITY_V2.DRAFT,
|
|
552
|
+
new Set([ADR_MATURITY_V2.ACCEPTED, ADR_MATURITY_V2.SUPERSEDED]),
|
|
553
|
+
],
|
|
554
|
+
[
|
|
555
|
+
ADR_MATURITY_V2.ACCEPTED,
|
|
556
|
+
new Set([ADR_MATURITY_V2.DEPRECATED, ADR_MATURITY_V2.SUPERSEDED]),
|
|
557
|
+
],
|
|
558
|
+
[
|
|
559
|
+
ADR_MATURITY_V2.DEPRECATED,
|
|
560
|
+
new Set([ADR_MATURITY_V2.ACCEPTED, ADR_MATURITY_V2.SUPERSEDED]),
|
|
561
|
+
],
|
|
562
|
+
]);
|
|
563
|
+
const _ADR_TERMINAL_V2 = new Set([ADR_MATURITY_V2.SUPERSEDED]);
|
|
564
|
+
|
|
565
|
+
const _SESSION_TRANSITIONS_V2 = new Map([
|
|
566
|
+
[
|
|
567
|
+
DEV_SESSION_V2.QUEUED,
|
|
568
|
+
new Set([
|
|
569
|
+
DEV_SESSION_V2.RUNNING,
|
|
570
|
+
DEV_SESSION_V2.CANCELED,
|
|
571
|
+
DEV_SESSION_V2.FAILED,
|
|
572
|
+
]),
|
|
573
|
+
],
|
|
574
|
+
[
|
|
575
|
+
DEV_SESSION_V2.RUNNING,
|
|
576
|
+
new Set([
|
|
577
|
+
DEV_SESSION_V2.COMPLETED,
|
|
578
|
+
DEV_SESSION_V2.FAILED,
|
|
579
|
+
DEV_SESSION_V2.CANCELED,
|
|
580
|
+
]),
|
|
581
|
+
],
|
|
582
|
+
]);
|
|
583
|
+
const _SESSION_TERMINAL_V2 = new Set([
|
|
584
|
+
DEV_SESSION_V2.COMPLETED,
|
|
585
|
+
DEV_SESSION_V2.FAILED,
|
|
586
|
+
DEV_SESSION_V2.CANCELED,
|
|
587
|
+
]);
|
|
588
|
+
|
|
589
|
+
export const AD_DEFAULT_MAX_ACTIVE_ADRS_PER_AUTHOR = 20;
|
|
590
|
+
export const AD_DEFAULT_MAX_RUNNING_SESSIONS_PER_DEVELOPER = 3;
|
|
591
|
+
export const AD_DEFAULT_ADR_STALE_MS = 90 * 86400000;
|
|
592
|
+
export const AD_DEFAULT_SESSION_STUCK_MS = 2 * 3600000;
|
|
593
|
+
|
|
594
|
+
let _adMaxActiveAdrsPerAuthor = AD_DEFAULT_MAX_ACTIVE_ADRS_PER_AUTHOR;
|
|
595
|
+
let _adMaxRunningSessionsPerDeveloper =
|
|
596
|
+
AD_DEFAULT_MAX_RUNNING_SESSIONS_PER_DEVELOPER;
|
|
597
|
+
let _adAdrStaleMs = AD_DEFAULT_ADR_STALE_MS;
|
|
598
|
+
let _adSessionStuckMs = AD_DEFAULT_SESSION_STUCK_MS;
|
|
599
|
+
|
|
600
|
+
const _adrsV2 = new Map();
|
|
601
|
+
const _sessionsV2 = new Map();
|
|
602
|
+
|
|
603
|
+
function _positiveIntV2(n, label) {
|
|
604
|
+
const f = Math.floor(n);
|
|
605
|
+
if (!Number.isFinite(f) || f <= 0)
|
|
606
|
+
throw new Error(`${label} must be a positive integer`);
|
|
607
|
+
return f;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
function _now() {
|
|
611
|
+
return Date.now();
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
export function getMaxActiveAdrsPerAuthor() {
|
|
615
|
+
return _adMaxActiveAdrsPerAuthor;
|
|
616
|
+
}
|
|
617
|
+
export function setMaxActiveAdrsPerAuthor(n) {
|
|
618
|
+
_adMaxActiveAdrsPerAuthor = _positiveIntV2(n, "maxActiveAdrsPerAuthor");
|
|
619
|
+
return _adMaxActiveAdrsPerAuthor;
|
|
620
|
+
}
|
|
621
|
+
export function getMaxRunningSessionsPerDeveloper() {
|
|
622
|
+
return _adMaxRunningSessionsPerDeveloper;
|
|
623
|
+
}
|
|
624
|
+
export function setMaxRunningSessionsPerDeveloper(n) {
|
|
625
|
+
_adMaxRunningSessionsPerDeveloper = _positiveIntV2(
|
|
626
|
+
n,
|
|
627
|
+
"maxRunningSessionsPerDeveloper",
|
|
628
|
+
);
|
|
629
|
+
return _adMaxRunningSessionsPerDeveloper;
|
|
630
|
+
}
|
|
631
|
+
export function getAdrStaleMs() {
|
|
632
|
+
return _adAdrStaleMs;
|
|
633
|
+
}
|
|
634
|
+
export function setAdrStaleMs(n) {
|
|
635
|
+
_adAdrStaleMs = _positiveIntV2(n, "adrStaleMs");
|
|
636
|
+
return _adAdrStaleMs;
|
|
637
|
+
}
|
|
638
|
+
export function getSessionStuckMs() {
|
|
639
|
+
return _adSessionStuckMs;
|
|
640
|
+
}
|
|
641
|
+
export function setSessionStuckMs(n) {
|
|
642
|
+
_adSessionStuckMs = _positiveIntV2(n, "sessionStuckMs");
|
|
643
|
+
return _adSessionStuckMs;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
export function getActiveAdrCount(author) {
|
|
647
|
+
let c = 0;
|
|
648
|
+
for (const a of _adrsV2.values()) {
|
|
649
|
+
if (_ADR_TERMINAL_V2.has(a.status)) continue;
|
|
650
|
+
if (author !== undefined && a.author !== author) continue;
|
|
651
|
+
c++;
|
|
652
|
+
}
|
|
653
|
+
return c;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
export function getRunningSessionCount(developer) {
|
|
657
|
+
let c = 0;
|
|
658
|
+
for (const s of _sessionsV2.values()) {
|
|
659
|
+
if (s.status !== DEV_SESSION_V2.RUNNING) continue;
|
|
660
|
+
if (developer !== undefined && s.developer !== developer) continue;
|
|
661
|
+
c++;
|
|
662
|
+
}
|
|
663
|
+
return c;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
export function createAdrV2({
|
|
667
|
+
id,
|
|
668
|
+
author,
|
|
669
|
+
title,
|
|
670
|
+
initialStatus,
|
|
671
|
+
metadata,
|
|
672
|
+
} = {}) {
|
|
673
|
+
if (!id) throw new Error("id required");
|
|
674
|
+
if (!author) throw new Error("author required");
|
|
675
|
+
if (!title) throw new Error("title required");
|
|
676
|
+
if (_adrsV2.has(id)) throw new Error(`ADR ${id} already exists`);
|
|
677
|
+
const status = initialStatus ?? ADR_MATURITY_V2.DRAFT;
|
|
678
|
+
if (!Object.values(ADR_MATURITY_V2).includes(status))
|
|
679
|
+
throw new Error(`invalid initial status ${status}`);
|
|
680
|
+
if (
|
|
681
|
+
!_ADR_TERMINAL_V2.has(status) &&
|
|
682
|
+
getActiveAdrCount(author) >= _adMaxActiveAdrsPerAuthor
|
|
683
|
+
)
|
|
684
|
+
throw new Error(`author ${author} active ADR cap reached`);
|
|
685
|
+
const now = _now();
|
|
686
|
+
const a = {
|
|
687
|
+
id,
|
|
688
|
+
author,
|
|
689
|
+
title,
|
|
690
|
+
status,
|
|
691
|
+
metadata: metadata ? { ...metadata } : {},
|
|
692
|
+
createdAt: now,
|
|
693
|
+
updatedAt: now,
|
|
694
|
+
acceptedAt: status === ADR_MATURITY_V2.ACCEPTED ? now : null,
|
|
695
|
+
};
|
|
696
|
+
_adrsV2.set(id, a);
|
|
697
|
+
return { ...a, metadata: { ...a.metadata } };
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
export function getAdrV2(id) {
|
|
701
|
+
const a = _adrsV2.get(id);
|
|
702
|
+
return a ? { ...a, metadata: { ...a.metadata } } : null;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
export function listAdrsV2({ author, status } = {}) {
|
|
706
|
+
const out = [];
|
|
707
|
+
for (const a of _adrsV2.values()) {
|
|
708
|
+
if (author !== undefined && a.author !== author) continue;
|
|
709
|
+
if (status !== undefined && a.status !== status) continue;
|
|
710
|
+
out.push({ ...a, metadata: { ...a.metadata } });
|
|
711
|
+
}
|
|
712
|
+
return out;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
export function setAdrMaturityV2(id, nextStatus, { reason, metadata } = {}) {
|
|
716
|
+
const a = _adrsV2.get(id);
|
|
717
|
+
if (!a) throw new Error(`ADR ${id} not found`);
|
|
718
|
+
if (_ADR_TERMINAL_V2.has(a.status))
|
|
719
|
+
throw new Error(`ADR ${id} is terminal (${a.status})`);
|
|
720
|
+
const allowed = _ADR_TRANSITIONS_V2.get(a.status);
|
|
721
|
+
if (!allowed || !allowed.has(nextStatus))
|
|
722
|
+
throw new Error(`illegal transition ${a.status} → ${nextStatus}`);
|
|
723
|
+
a.status = nextStatus;
|
|
724
|
+
a.updatedAt = _now();
|
|
725
|
+
if (reason !== undefined) a.reason = reason;
|
|
726
|
+
if (metadata) a.metadata = { ...a.metadata, ...metadata };
|
|
727
|
+
if (nextStatus === ADR_MATURITY_V2.ACCEPTED && !a.acceptedAt)
|
|
728
|
+
a.acceptedAt = a.updatedAt;
|
|
729
|
+
return { ...a, metadata: { ...a.metadata } };
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
export function acceptAdr(id, opts) {
|
|
733
|
+
return setAdrMaturityV2(id, ADR_MATURITY_V2.ACCEPTED, opts);
|
|
734
|
+
}
|
|
735
|
+
export function deprecateAdr(id, opts) {
|
|
736
|
+
return setAdrMaturityV2(id, ADR_MATURITY_V2.DEPRECATED, opts);
|
|
737
|
+
}
|
|
738
|
+
export function supersedeAdr(id, opts) {
|
|
739
|
+
return setAdrMaturityV2(id, ADR_MATURITY_V2.SUPERSEDED, opts);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
export function enqueueSessionV2({ id, developer, goal, metadata } = {}) {
|
|
743
|
+
if (!id) throw new Error("id required");
|
|
744
|
+
if (!developer) throw new Error("developer required");
|
|
745
|
+
if (!goal) throw new Error("goal required");
|
|
746
|
+
if (_sessionsV2.has(id)) throw new Error(`session ${id} already exists`);
|
|
747
|
+
const now = _now();
|
|
748
|
+
const s = {
|
|
749
|
+
id,
|
|
750
|
+
developer,
|
|
751
|
+
goal,
|
|
752
|
+
status: DEV_SESSION_V2.QUEUED,
|
|
753
|
+
metadata: metadata ? { ...metadata } : {},
|
|
754
|
+
createdAt: now,
|
|
755
|
+
updatedAt: now,
|
|
756
|
+
startedAt: null,
|
|
757
|
+
};
|
|
758
|
+
_sessionsV2.set(id, s);
|
|
759
|
+
return { ...s, metadata: { ...s.metadata } };
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
export function getSessionV2(id) {
|
|
763
|
+
const s = _sessionsV2.get(id);
|
|
764
|
+
return s ? { ...s, metadata: { ...s.metadata } } : null;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
export function listSessionsV2({ developer, status } = {}) {
|
|
768
|
+
const out = [];
|
|
769
|
+
for (const s of _sessionsV2.values()) {
|
|
770
|
+
if (developer !== undefined && s.developer !== developer) continue;
|
|
771
|
+
if (status !== undefined && s.status !== status) continue;
|
|
772
|
+
out.push({ ...s, metadata: { ...s.metadata } });
|
|
773
|
+
}
|
|
774
|
+
return out;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
export function setSessionStatusV2(id, nextStatus, { reason, metadata } = {}) {
|
|
778
|
+
const s = _sessionsV2.get(id);
|
|
779
|
+
if (!s) throw new Error(`session ${id} not found`);
|
|
780
|
+
if (_SESSION_TERMINAL_V2.has(s.status))
|
|
781
|
+
throw new Error(`session ${id} is terminal (${s.status})`);
|
|
782
|
+
const allowed = _SESSION_TRANSITIONS_V2.get(s.status);
|
|
783
|
+
if (!allowed || !allowed.has(nextStatus))
|
|
784
|
+
throw new Error(`illegal transition ${s.status} → ${nextStatus}`);
|
|
785
|
+
if (nextStatus === DEV_SESSION_V2.RUNNING) {
|
|
786
|
+
if (
|
|
787
|
+
getRunningSessionCount(s.developer) >= _adMaxRunningSessionsPerDeveloper
|
|
788
|
+
)
|
|
789
|
+
throw new Error(`developer ${s.developer} running session cap reached`);
|
|
790
|
+
}
|
|
791
|
+
s.status = nextStatus;
|
|
792
|
+
s.updatedAt = _now();
|
|
793
|
+
if (reason !== undefined) s.reason = reason;
|
|
794
|
+
if (metadata) s.metadata = { ...s.metadata, ...metadata };
|
|
795
|
+
if (nextStatus === DEV_SESSION_V2.RUNNING && !s.startedAt)
|
|
796
|
+
s.startedAt = s.updatedAt;
|
|
797
|
+
return { ...s, metadata: { ...s.metadata } };
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
export function startSessionV2(id, opts) {
|
|
801
|
+
return setSessionStatusV2(id, DEV_SESSION_V2.RUNNING, opts);
|
|
802
|
+
}
|
|
803
|
+
export function completeSessionV2(id, opts) {
|
|
804
|
+
return setSessionStatusV2(id, DEV_SESSION_V2.COMPLETED, opts);
|
|
805
|
+
}
|
|
806
|
+
export function failSessionV2(id, opts) {
|
|
807
|
+
return setSessionStatusV2(id, DEV_SESSION_V2.FAILED, opts);
|
|
808
|
+
}
|
|
809
|
+
export function cancelSessionV2(id, opts) {
|
|
810
|
+
return setSessionStatusV2(id, DEV_SESSION_V2.CANCELED, opts);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
export function autoSupersedeStaleDrafts({ now } = {}) {
|
|
814
|
+
const cutoff = (now ?? _now()) - _adAdrStaleMs;
|
|
815
|
+
const flipped = [];
|
|
816
|
+
for (const a of _adrsV2.values()) {
|
|
817
|
+
if (a.status !== ADR_MATURITY_V2.DRAFT) continue;
|
|
818
|
+
if ((a.updatedAt ?? a.createdAt) > cutoff) continue;
|
|
819
|
+
a.status = ADR_MATURITY_V2.SUPERSEDED;
|
|
820
|
+
a.updatedAt = now ?? _now();
|
|
821
|
+
a.reason = "auto_supersede_stale_draft";
|
|
822
|
+
flipped.push(a.id);
|
|
823
|
+
}
|
|
824
|
+
return flipped;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
export function autoFailStuckSessions({ now } = {}) {
|
|
828
|
+
const cutoff = (now ?? _now()) - _adSessionStuckMs;
|
|
829
|
+
const flipped = [];
|
|
830
|
+
for (const s of _sessionsV2.values()) {
|
|
831
|
+
if (s.status !== DEV_SESSION_V2.RUNNING) continue;
|
|
832
|
+
if (!s.startedAt || s.startedAt > cutoff) continue;
|
|
833
|
+
s.status = DEV_SESSION_V2.FAILED;
|
|
834
|
+
s.updatedAt = now ?? _now();
|
|
835
|
+
s.reason = "auto_fail_stuck";
|
|
836
|
+
flipped.push(s.id);
|
|
837
|
+
}
|
|
838
|
+
return flipped;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
function _zeroByEnum(enumObj) {
|
|
842
|
+
const out = {};
|
|
843
|
+
for (const v of Object.values(enumObj)) out[v] = 0;
|
|
844
|
+
return out;
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
export function getAutonomousDeveloperStatsV2() {
|
|
848
|
+
const adrs = [..._adrsV2.values()];
|
|
849
|
+
const sessions = [..._sessionsV2.values()];
|
|
850
|
+
const adrsByStatus = _zeroByEnum(ADR_MATURITY_V2);
|
|
851
|
+
for (const a of adrs) adrsByStatus[a.status]++;
|
|
852
|
+
const sessionsByStatus = _zeroByEnum(DEV_SESSION_V2);
|
|
853
|
+
for (const s of sessions) sessionsByStatus[s.status]++;
|
|
854
|
+
return {
|
|
855
|
+
totalAdrsV2: adrs.length,
|
|
856
|
+
totalSessionsV2: sessions.length,
|
|
857
|
+
maxActiveAdrsPerAuthor: _adMaxActiveAdrsPerAuthor,
|
|
858
|
+
maxRunningSessionsPerDeveloper: _adMaxRunningSessionsPerDeveloper,
|
|
859
|
+
adrStaleMs: _adAdrStaleMs,
|
|
860
|
+
sessionStuckMs: _adSessionStuckMs,
|
|
861
|
+
adrsByStatus,
|
|
862
|
+
sessionsByStatus,
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
export function _resetStateV2() {
|
|
867
|
+
_adrsV2.clear();
|
|
868
|
+
_sessionsV2.clear();
|
|
869
|
+
_adMaxActiveAdrsPerAuthor = AD_DEFAULT_MAX_ACTIVE_ADRS_PER_AUTHOR;
|
|
870
|
+
_adMaxRunningSessionsPerDeveloper =
|
|
871
|
+
AD_DEFAULT_MAX_RUNNING_SESSIONS_PER_DEVELOPER;
|
|
872
|
+
_adAdrStaleMs = AD_DEFAULT_ADR_STALE_MS;
|
|
873
|
+
_adSessionStuckMs = AD_DEFAULT_SESSION_STUCK_MS;
|
|
874
|
+
}
|
package/src/lib/bi-engine.js
CHANGED
|
@@ -635,3 +635,108 @@ export function _resetV2State() {
|
|
|
635
635
|
_reportStatusV2.clear();
|
|
636
636
|
_statusHistoryV2.clear();
|
|
637
637
|
}
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
// ===== V2 Surface: BI Engine governance overlay (CLI v0.135.0) =====
|
|
641
|
+
export const BI_DATASET_MATURITY_V2 = Object.freeze({
|
|
642
|
+
PENDING: "pending", ACTIVE: "active", STALE: "stale", ARCHIVED: "archived",
|
|
643
|
+
});
|
|
644
|
+
export const BI_QUERY_LIFECYCLE_V2 = Object.freeze({
|
|
645
|
+
QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
const _biDsTrans = new Map([
|
|
649
|
+
[BI_DATASET_MATURITY_V2.PENDING, new Set([BI_DATASET_MATURITY_V2.ACTIVE, BI_DATASET_MATURITY_V2.ARCHIVED])],
|
|
650
|
+
[BI_DATASET_MATURITY_V2.ACTIVE, new Set([BI_DATASET_MATURITY_V2.STALE, BI_DATASET_MATURITY_V2.ARCHIVED])],
|
|
651
|
+
[BI_DATASET_MATURITY_V2.STALE, new Set([BI_DATASET_MATURITY_V2.ACTIVE, BI_DATASET_MATURITY_V2.ARCHIVED])],
|
|
652
|
+
[BI_DATASET_MATURITY_V2.ARCHIVED, new Set()],
|
|
653
|
+
]);
|
|
654
|
+
const _biDsTerminal = new Set([BI_DATASET_MATURITY_V2.ARCHIVED]);
|
|
655
|
+
const _biQTrans = new Map([
|
|
656
|
+
[BI_QUERY_LIFECYCLE_V2.QUEUED, new Set([BI_QUERY_LIFECYCLE_V2.RUNNING, BI_QUERY_LIFECYCLE_V2.CANCELLED])],
|
|
657
|
+
[BI_QUERY_LIFECYCLE_V2.RUNNING, new Set([BI_QUERY_LIFECYCLE_V2.COMPLETED, BI_QUERY_LIFECYCLE_V2.FAILED, BI_QUERY_LIFECYCLE_V2.CANCELLED])],
|
|
658
|
+
[BI_QUERY_LIFECYCLE_V2.COMPLETED, new Set()],
|
|
659
|
+
[BI_QUERY_LIFECYCLE_V2.FAILED, new Set()],
|
|
660
|
+
[BI_QUERY_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
661
|
+
]);
|
|
662
|
+
|
|
663
|
+
const _biDatasets = new Map();
|
|
664
|
+
const _biQueries = new Map();
|
|
665
|
+
let _biMaxActivePerOwner = 8;
|
|
666
|
+
let _biMaxPendingPerDs = 10;
|
|
667
|
+
let _biDsIdleMs = 7 * 24 * 60 * 60 * 1000;
|
|
668
|
+
let _biQStuckMs = 5 * 60 * 1000;
|
|
669
|
+
|
|
670
|
+
function _biPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
|
|
671
|
+
|
|
672
|
+
export function setMaxActiveBiDatasetsPerOwnerV2(n) { _biMaxActivePerOwner = _biPos(n, "maxActiveBiDatasetsPerOwner"); }
|
|
673
|
+
export function getMaxActiveBiDatasetsPerOwnerV2() { return _biMaxActivePerOwner; }
|
|
674
|
+
export function setMaxPendingBiQueriesPerDatasetV2(n) { _biMaxPendingPerDs = _biPos(n, "maxPendingBiQueriesPerDataset"); }
|
|
675
|
+
export function getMaxPendingBiQueriesPerDatasetV2() { return _biMaxPendingPerDs; }
|
|
676
|
+
export function setBiDatasetIdleMsV2(n) { _biDsIdleMs = _biPos(n, "biDatasetIdleMs"); }
|
|
677
|
+
export function getBiDatasetIdleMsV2() { return _biDsIdleMs; }
|
|
678
|
+
export function setBiQueryStuckMsV2(n) { _biQStuckMs = _biPos(n, "biQueryStuckMs"); }
|
|
679
|
+
export function getBiQueryStuckMsV2() { return _biQStuckMs; }
|
|
680
|
+
|
|
681
|
+
export function _resetStateBiEngineV2() {
|
|
682
|
+
_biDatasets.clear(); _biQueries.clear();
|
|
683
|
+
_biMaxActivePerOwner = 8; _biMaxPendingPerDs = 10;
|
|
684
|
+
_biDsIdleMs = 7 * 24 * 60 * 60 * 1000; _biQStuckMs = 5 * 60 * 1000;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
export function registerBiDatasetV2({ id, owner, source, metadata } = {}) {
|
|
688
|
+
if (!id || typeof id !== "string") throw new Error("id is required");
|
|
689
|
+
if (!owner || typeof owner !== "string") throw new Error("owner is required");
|
|
690
|
+
if (_biDatasets.has(id)) throw new Error(`bi dataset ${id} already registered`);
|
|
691
|
+
const now = Date.now();
|
|
692
|
+
const d = { id, owner, source: source || "", status: BI_DATASET_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
|
|
693
|
+
_biDatasets.set(id, d);
|
|
694
|
+
return { ...d, metadata: { ...d.metadata } };
|
|
695
|
+
}
|
|
696
|
+
function _biCheckD(from, to) { const a = _biDsTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid bi dataset transition ${from} → ${to}`); }
|
|
697
|
+
function _biCountActive(owner) { let n = 0; for (const d of _biDatasets.values()) if (d.owner === owner && d.status === BI_DATASET_MATURITY_V2.ACTIVE) n++; return n; }
|
|
698
|
+
|
|
699
|
+
export function activateBiDatasetV2(id) {
|
|
700
|
+
const d = _biDatasets.get(id); if (!d) throw new Error(`bi dataset ${id} not found`);
|
|
701
|
+
_biCheckD(d.status, BI_DATASET_MATURITY_V2.ACTIVE);
|
|
702
|
+
const recovery = d.status === BI_DATASET_MATURITY_V2.STALE;
|
|
703
|
+
if (!recovery) { const a = _biCountActive(d.owner); if (a >= _biMaxActivePerOwner) throw new Error(`max active bi datasets per owner (${_biMaxActivePerOwner}) reached for ${d.owner}`); }
|
|
704
|
+
const now = Date.now(); d.status = BI_DATASET_MATURITY_V2.ACTIVE; d.updatedAt = now; d.lastTouchedAt = now; if (!d.activatedAt) d.activatedAt = now;
|
|
705
|
+
return { ...d, metadata: { ...d.metadata } };
|
|
706
|
+
}
|
|
707
|
+
export function staleBiDatasetV2(id) { const d = _biDatasets.get(id); if (!d) throw new Error(`bi dataset ${id} not found`); _biCheckD(d.status, BI_DATASET_MATURITY_V2.STALE); d.status = BI_DATASET_MATURITY_V2.STALE; d.updatedAt = Date.now(); return { ...d, metadata: { ...d.metadata } }; }
|
|
708
|
+
export function archiveBiDatasetV2(id) { const d = _biDatasets.get(id); if (!d) throw new Error(`bi dataset ${id} not found`); _biCheckD(d.status, BI_DATASET_MATURITY_V2.ARCHIVED); const now = Date.now(); d.status = BI_DATASET_MATURITY_V2.ARCHIVED; d.updatedAt = now; if (!d.archivedAt) d.archivedAt = now; return { ...d, metadata: { ...d.metadata } }; }
|
|
709
|
+
export function touchBiDatasetV2(id) { const d = _biDatasets.get(id); if (!d) throw new Error(`bi dataset ${id} not found`); if (_biDsTerminal.has(d.status)) throw new Error(`cannot touch terminal bi dataset ${id}`); const now = Date.now(); d.lastTouchedAt = now; d.updatedAt = now; return { ...d, metadata: { ...d.metadata } }; }
|
|
710
|
+
export function getBiDatasetV2(id) { const d = _biDatasets.get(id); if (!d) return null; return { ...d, metadata: { ...d.metadata } }; }
|
|
711
|
+
export function listBiDatasetsV2() { return [..._biDatasets.values()].map((d) => ({ ...d, metadata: { ...d.metadata } })); }
|
|
712
|
+
|
|
713
|
+
function _biCountPending(did) { let n = 0; for (const q of _biQueries.values()) if (q.datasetId === did && (q.status === BI_QUERY_LIFECYCLE_V2.QUEUED || q.status === BI_QUERY_LIFECYCLE_V2.RUNNING)) n++; return n; }
|
|
714
|
+
|
|
715
|
+
export function createBiQueryV2({ id, datasetId, sql, metadata } = {}) {
|
|
716
|
+
if (!id || typeof id !== "string") throw new Error("id is required");
|
|
717
|
+
if (!datasetId || typeof datasetId !== "string") throw new Error("datasetId is required");
|
|
718
|
+
if (_biQueries.has(id)) throw new Error(`bi query ${id} already exists`);
|
|
719
|
+
if (!_biDatasets.has(datasetId)) throw new Error(`bi dataset ${datasetId} not found`);
|
|
720
|
+
const pending = _biCountPending(datasetId);
|
|
721
|
+
if (pending >= _biMaxPendingPerDs) throw new Error(`max pending bi queries per dataset (${_biMaxPendingPerDs}) reached for ${datasetId}`);
|
|
722
|
+
const now = Date.now();
|
|
723
|
+
const q = { id, datasetId, sql: sql || "", status: BI_QUERY_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
|
|
724
|
+
_biQueries.set(id, q);
|
|
725
|
+
return { ...q, metadata: { ...q.metadata } };
|
|
726
|
+
}
|
|
727
|
+
function _biCheckQ(from, to) { const a = _biQTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid bi query transition ${from} → ${to}`); }
|
|
728
|
+
export function startBiQueryV2(id) { const q = _biQueries.get(id); if (!q) throw new Error(`bi query ${id} not found`); _biCheckQ(q.status, BI_QUERY_LIFECYCLE_V2.RUNNING); const now = Date.now(); q.status = BI_QUERY_LIFECYCLE_V2.RUNNING; q.updatedAt = now; if (!q.startedAt) q.startedAt = now; return { ...q, metadata: { ...q.metadata } }; }
|
|
729
|
+
export function completeBiQueryV2(id) { const q = _biQueries.get(id); if (!q) throw new Error(`bi query ${id} not found`); _biCheckQ(q.status, BI_QUERY_LIFECYCLE_V2.COMPLETED); const now = Date.now(); q.status = BI_QUERY_LIFECYCLE_V2.COMPLETED; q.updatedAt = now; if (!q.settledAt) q.settledAt = now; return { ...q, metadata: { ...q.metadata } }; }
|
|
730
|
+
export function failBiQueryV2(id, reason) { const q = _biQueries.get(id); if (!q) throw new Error(`bi query ${id} not found`); _biCheckQ(q.status, BI_QUERY_LIFECYCLE_V2.FAILED); const now = Date.now(); q.status = BI_QUERY_LIFECYCLE_V2.FAILED; q.updatedAt = now; if (!q.settledAt) q.settledAt = now; if (reason) q.metadata.failReason = String(reason); return { ...q, metadata: { ...q.metadata } }; }
|
|
731
|
+
export function cancelBiQueryV2(id, reason) { const q = _biQueries.get(id); if (!q) throw new Error(`bi query ${id} not found`); _biCheckQ(q.status, BI_QUERY_LIFECYCLE_V2.CANCELLED); const now = Date.now(); q.status = BI_QUERY_LIFECYCLE_V2.CANCELLED; q.updatedAt = now; if (!q.settledAt) q.settledAt = now; if (reason) q.metadata.cancelReason = String(reason); return { ...q, metadata: { ...q.metadata } }; }
|
|
732
|
+
export function getBiQueryV2(id) { const q = _biQueries.get(id); if (!q) return null; return { ...q, metadata: { ...q.metadata } }; }
|
|
733
|
+
export function listBiQueriesV2() { return [..._biQueries.values()].map((q) => ({ ...q, metadata: { ...q.metadata } })); }
|
|
734
|
+
|
|
735
|
+
export function autoStaleIdleBiDatasetsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const d of _biDatasets.values()) if (d.status === BI_DATASET_MATURITY_V2.ACTIVE && (t - d.lastTouchedAt) >= _biDsIdleMs) { d.status = BI_DATASET_MATURITY_V2.STALE; d.updatedAt = t; flipped.push(d.id); } return { flipped, count: flipped.length }; }
|
|
736
|
+
export function autoFailStuckBiQueriesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const q of _biQueries.values()) if (q.status === BI_QUERY_LIFECYCLE_V2.RUNNING && q.startedAt != null && (t - q.startedAt) >= _biQStuckMs) { q.status = BI_QUERY_LIFECYCLE_V2.FAILED; q.updatedAt = t; if (!q.settledAt) q.settledAt = t; q.metadata.failReason = "auto-fail-stuck"; flipped.push(q.id); } return { flipped, count: flipped.length }; }
|
|
737
|
+
|
|
738
|
+
export function getBiEngineStatsV2() {
|
|
739
|
+
const datasetsByStatus = {}; for (const s of Object.values(BI_DATASET_MATURITY_V2)) datasetsByStatus[s] = 0; for (const d of _biDatasets.values()) datasetsByStatus[d.status]++;
|
|
740
|
+
const queriesByStatus = {}; for (const s of Object.values(BI_QUERY_LIFECYCLE_V2)) queriesByStatus[s] = 0; for (const q of _biQueries.values()) queriesByStatus[q.status]++;
|
|
741
|
+
return { totalDatasetsV2: _biDatasets.size, totalQueriesV2: _biQueries.size, maxActiveBiDatasetsPerOwner: _biMaxActivePerOwner, maxPendingBiQueriesPerDataset: _biMaxPendingPerDs, biDatasetIdleMs: _biDsIdleMs, biQueryStuckMs: _biQStuckMs, datasetsByStatus, queriesByStatus };
|
|
742
|
+
}
|
package/src/lib/bm25-search.js
CHANGED
|
@@ -320,3 +320,84 @@ export class BM25Search {
|
|
|
320
320
|
};
|
|
321
321
|
}
|
|
322
322
|
}
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
// =====================================================================
|
|
326
|
+
// BM25 Search V2 governance overlay
|
|
327
|
+
// =====================================================================
|
|
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" });
|
|
330
|
+
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])],
|
|
334
|
+
[BM25_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
335
|
+
]);
|
|
336
|
+
const _bm25PTerminal = new Set([BM25_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
337
|
+
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])],
|
|
340
|
+
[BM25_QUERY_LIFECYCLE_V2.COMPLETED, new Set()],
|
|
341
|
+
[BM25_QUERY_LIFECYCLE_V2.FAILED, new Set()],
|
|
342
|
+
[BM25_QUERY_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
343
|
+
]);
|
|
344
|
+
const _bm25PsV2 = new Map();
|
|
345
|
+
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; }
|
|
361
|
+
export function registerBm25ProfileV2({ id, owner, field, metadata } = {}) {
|
|
362
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
363
|
+
if (_bm25PsV2.has(id)) throw new Error(`bm25 profile ${id} already exists`);
|
|
364
|
+
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 } };
|
|
367
|
+
}
|
|
368
|
+
export function activateBm25ProfileV2(id) {
|
|
369
|
+
const p = _bm25PsV2.get(id); if (!p) throw new Error(`bm25 profile ${id} not found`);
|
|
370
|
+
const isInitial = p.status === BM25_PROFILE_MATURITY_V2.PENDING;
|
|
371
|
+
_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;
|
|
374
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
375
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
376
|
+
}
|
|
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 } })); }
|
|
382
|
+
export function createBm25QueryV2({ id, profileId, q, metadata } = {}) {
|
|
383
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
384
|
+
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`);
|
|
387
|
+
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 }; }
|
|
399
|
+
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 };
|
|
403
|
+
}
|