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
|
@@ -291,3 +291,382 @@ export function importNotionDir(db, dir) {
|
|
|
291
291
|
|
|
292
292
|
return imported;
|
|
293
293
|
}
|
|
294
|
+
|
|
295
|
+
// ─── V2 Governance Layer ────────────────────────────────────────────
|
|
296
|
+
//
|
|
297
|
+
// In-memory governance for source manifests + import jobs, independent
|
|
298
|
+
// of the SQLite notes table populated by the legacy import* helpers.
|
|
299
|
+
// V2 tracks maturity transitions, per-owner active-source caps, per-source
|
|
300
|
+
// pending-job caps, stamp-once timestamps, and bulk auto-flip routines.
|
|
301
|
+
|
|
302
|
+
export const SOURCE_MATURITY_V2 = Object.freeze({
|
|
303
|
+
PENDING: "pending",
|
|
304
|
+
ACTIVE: "active",
|
|
305
|
+
PAUSED: "paused",
|
|
306
|
+
ARCHIVED: "archived",
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
export const IMPORT_JOB_LIFECYCLE_V2 = Object.freeze({
|
|
310
|
+
QUEUED: "queued",
|
|
311
|
+
RUNNING: "running",
|
|
312
|
+
COMPLETED: "completed",
|
|
313
|
+
FAILED: "failed",
|
|
314
|
+
CANCELLED: "cancelled",
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
const _SOURCE_TRANSITIONS_V2 = new Map([
|
|
318
|
+
[
|
|
319
|
+
SOURCE_MATURITY_V2.PENDING,
|
|
320
|
+
new Set([SOURCE_MATURITY_V2.ACTIVE, SOURCE_MATURITY_V2.ARCHIVED]),
|
|
321
|
+
],
|
|
322
|
+
[
|
|
323
|
+
SOURCE_MATURITY_V2.ACTIVE,
|
|
324
|
+
new Set([SOURCE_MATURITY_V2.PAUSED, SOURCE_MATURITY_V2.ARCHIVED]),
|
|
325
|
+
],
|
|
326
|
+
[
|
|
327
|
+
SOURCE_MATURITY_V2.PAUSED,
|
|
328
|
+
new Set([SOURCE_MATURITY_V2.ACTIVE, SOURCE_MATURITY_V2.ARCHIVED]),
|
|
329
|
+
],
|
|
330
|
+
[SOURCE_MATURITY_V2.ARCHIVED, new Set()],
|
|
331
|
+
]);
|
|
332
|
+
|
|
333
|
+
const _SOURCE_TERMINALS_V2 = new Set([SOURCE_MATURITY_V2.ARCHIVED]);
|
|
334
|
+
|
|
335
|
+
const _JOB_TRANSITIONS_V2 = new Map([
|
|
336
|
+
[
|
|
337
|
+
IMPORT_JOB_LIFECYCLE_V2.QUEUED,
|
|
338
|
+
new Set([
|
|
339
|
+
IMPORT_JOB_LIFECYCLE_V2.RUNNING,
|
|
340
|
+
IMPORT_JOB_LIFECYCLE_V2.CANCELLED,
|
|
341
|
+
]),
|
|
342
|
+
],
|
|
343
|
+
[
|
|
344
|
+
IMPORT_JOB_LIFECYCLE_V2.RUNNING,
|
|
345
|
+
new Set([
|
|
346
|
+
IMPORT_JOB_LIFECYCLE_V2.COMPLETED,
|
|
347
|
+
IMPORT_JOB_LIFECYCLE_V2.FAILED,
|
|
348
|
+
IMPORT_JOB_LIFECYCLE_V2.CANCELLED,
|
|
349
|
+
]),
|
|
350
|
+
],
|
|
351
|
+
[IMPORT_JOB_LIFECYCLE_V2.COMPLETED, new Set()],
|
|
352
|
+
[IMPORT_JOB_LIFECYCLE_V2.FAILED, new Set()],
|
|
353
|
+
[IMPORT_JOB_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
354
|
+
]);
|
|
355
|
+
|
|
356
|
+
const _JOB_TERMINALS_V2 = new Set([
|
|
357
|
+
IMPORT_JOB_LIFECYCLE_V2.COMPLETED,
|
|
358
|
+
IMPORT_JOB_LIFECYCLE_V2.FAILED,
|
|
359
|
+
IMPORT_JOB_LIFECYCLE_V2.CANCELLED,
|
|
360
|
+
]);
|
|
361
|
+
|
|
362
|
+
export const IMPORTER_DEFAULT_MAX_ACTIVE_SOURCES_PER_OWNER = 15;
|
|
363
|
+
export const IMPORTER_DEFAULT_MAX_PENDING_JOBS_PER_SOURCE = 3;
|
|
364
|
+
export const IMPORTER_DEFAULT_SOURCE_IDLE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
|
|
365
|
+
export const IMPORTER_DEFAULT_JOB_STUCK_MS = 20 * 60 * 1000; // 20 min
|
|
366
|
+
|
|
367
|
+
const _stateV2 = {
|
|
368
|
+
sources: new Map(),
|
|
369
|
+
jobs: new Map(),
|
|
370
|
+
maxActiveSourcesPerOwner: IMPORTER_DEFAULT_MAX_ACTIVE_SOURCES_PER_OWNER,
|
|
371
|
+
maxPendingJobsPerSource: IMPORTER_DEFAULT_MAX_PENDING_JOBS_PER_SOURCE,
|
|
372
|
+
sourceIdleMs: IMPORTER_DEFAULT_SOURCE_IDLE_MS,
|
|
373
|
+
jobStuckMs: IMPORTER_DEFAULT_JOB_STUCK_MS,
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
function _posIntImporterV2(n, label) {
|
|
377
|
+
const v = Math.floor(Number(n));
|
|
378
|
+
if (!Number.isFinite(v) || v <= 0) {
|
|
379
|
+
throw new Error(`${label} must be a positive integer, got ${n}`);
|
|
380
|
+
}
|
|
381
|
+
return v;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export function getMaxActiveSourcesPerOwnerV2() {
|
|
385
|
+
return _stateV2.maxActiveSourcesPerOwner;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export function setMaxActiveSourcesPerOwnerV2(n) {
|
|
389
|
+
_stateV2.maxActiveSourcesPerOwner = _posIntImporterV2(
|
|
390
|
+
n,
|
|
391
|
+
"maxActiveSourcesPerOwner",
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export function getMaxPendingJobsPerSourceV2() {
|
|
396
|
+
return _stateV2.maxPendingJobsPerSource;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
export function setMaxPendingJobsPerSourceV2(n) {
|
|
400
|
+
_stateV2.maxPendingJobsPerSource = _posIntImporterV2(
|
|
401
|
+
n,
|
|
402
|
+
"maxPendingJobsPerSource",
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
export function getSourceIdleMsV2() {
|
|
407
|
+
return _stateV2.sourceIdleMs;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export function setSourceIdleMsV2(ms) {
|
|
411
|
+
_stateV2.sourceIdleMs = _posIntImporterV2(ms, "sourceIdleMs");
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export function getJobStuckMsV2() {
|
|
415
|
+
return _stateV2.jobStuckMs;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
export function setJobStuckMsV2(ms) {
|
|
419
|
+
_stateV2.jobStuckMs = _posIntImporterV2(ms, "jobStuckMs");
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function _copySourceV2(s) {
|
|
423
|
+
return { ...s, metadata: { ...s.metadata } };
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function _copyJobV2(j) {
|
|
427
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export function getActiveSourceCountV2(ownerId) {
|
|
431
|
+
let count = 0;
|
|
432
|
+
for (const s of _stateV2.sources.values()) {
|
|
433
|
+
if (s.ownerId === ownerId && s.status === SOURCE_MATURITY_V2.ACTIVE)
|
|
434
|
+
count++;
|
|
435
|
+
}
|
|
436
|
+
return count;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
export function getPendingJobCountV2(sourceId) {
|
|
440
|
+
let count = 0;
|
|
441
|
+
for (const j of _stateV2.jobs.values()) {
|
|
442
|
+
if (
|
|
443
|
+
j.sourceId === sourceId &&
|
|
444
|
+
(j.status === IMPORT_JOB_LIFECYCLE_V2.QUEUED ||
|
|
445
|
+
j.status === IMPORT_JOB_LIFECYCLE_V2.RUNNING)
|
|
446
|
+
) {
|
|
447
|
+
count++;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return count;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
export function registerSourceV2(id, { ownerId, label, kind, metadata } = {}) {
|
|
454
|
+
if (!id) throw new Error("source id is required");
|
|
455
|
+
if (!ownerId) throw new Error("ownerId is required");
|
|
456
|
+
if (!label) throw new Error("label is required");
|
|
457
|
+
if (_stateV2.sources.has(id)) throw new Error(`source ${id} already exists`);
|
|
458
|
+
const now = Date.now();
|
|
459
|
+
const source = {
|
|
460
|
+
id,
|
|
461
|
+
ownerId,
|
|
462
|
+
label,
|
|
463
|
+
kind: kind || "markdown",
|
|
464
|
+
status: SOURCE_MATURITY_V2.PENDING,
|
|
465
|
+
createdAt: now,
|
|
466
|
+
lastSeenAt: now,
|
|
467
|
+
activatedAt: null,
|
|
468
|
+
archivedAt: null,
|
|
469
|
+
metadata: metadata ? { ...metadata } : {},
|
|
470
|
+
};
|
|
471
|
+
_stateV2.sources.set(id, source);
|
|
472
|
+
return _copySourceV2(source);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
export function getSourceV2(id) {
|
|
476
|
+
const s = _stateV2.sources.get(id);
|
|
477
|
+
return s ? _copySourceV2(s) : null;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
export function listSourcesV2({ ownerId, status } = {}) {
|
|
481
|
+
const out = [];
|
|
482
|
+
for (const s of _stateV2.sources.values()) {
|
|
483
|
+
if (ownerId && s.ownerId !== ownerId) continue;
|
|
484
|
+
if (status && s.status !== status) continue;
|
|
485
|
+
out.push(_copySourceV2(s));
|
|
486
|
+
}
|
|
487
|
+
return out;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
export function setSourceStatusV2(id, next) {
|
|
491
|
+
const s = _stateV2.sources.get(id);
|
|
492
|
+
if (!s) throw new Error(`source ${id} not found`);
|
|
493
|
+
const allowed = _SOURCE_TRANSITIONS_V2.get(s.status);
|
|
494
|
+
if (!allowed || !allowed.has(next)) {
|
|
495
|
+
throw new Error(`invalid source transition: ${s.status} → ${next}`);
|
|
496
|
+
}
|
|
497
|
+
// Cap enforcement: pending → active only (recovery from paused exempt)
|
|
498
|
+
if (
|
|
499
|
+
s.status === SOURCE_MATURITY_V2.PENDING &&
|
|
500
|
+
next === SOURCE_MATURITY_V2.ACTIVE
|
|
501
|
+
) {
|
|
502
|
+
const count = getActiveSourceCountV2(s.ownerId);
|
|
503
|
+
if (count >= _stateV2.maxActiveSourcesPerOwner) {
|
|
504
|
+
throw new Error(
|
|
505
|
+
`owner ${s.ownerId} active-source cap reached (${count}/${_stateV2.maxActiveSourcesPerOwner})`,
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
const now = Date.now();
|
|
510
|
+
s.status = next;
|
|
511
|
+
s.lastSeenAt = now;
|
|
512
|
+
if (next === SOURCE_MATURITY_V2.ACTIVE && !s.activatedAt) s.activatedAt = now;
|
|
513
|
+
if (_SOURCE_TERMINALS_V2.has(next) && !s.archivedAt) s.archivedAt = now;
|
|
514
|
+
return _copySourceV2(s);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
export function activateSourceV2(id) {
|
|
518
|
+
return setSourceStatusV2(id, SOURCE_MATURITY_V2.ACTIVE);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
export function pauseSourceV2(id) {
|
|
522
|
+
return setSourceStatusV2(id, SOURCE_MATURITY_V2.PAUSED);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
export function archiveSourceV2(id) {
|
|
526
|
+
return setSourceStatusV2(id, SOURCE_MATURITY_V2.ARCHIVED);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
export function touchSourceV2(id) {
|
|
530
|
+
const s = _stateV2.sources.get(id);
|
|
531
|
+
if (!s) throw new Error(`source ${id} not found`);
|
|
532
|
+
s.lastSeenAt = Date.now();
|
|
533
|
+
return _copySourceV2(s);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
export function createImportJobV2(id, { sourceId, kind, metadata } = {}) {
|
|
537
|
+
if (!id) throw new Error("job id is required");
|
|
538
|
+
if (!sourceId) throw new Error("sourceId is required");
|
|
539
|
+
if (_stateV2.jobs.has(id)) throw new Error(`job ${id} already exists`);
|
|
540
|
+
const source = _stateV2.sources.get(sourceId);
|
|
541
|
+
if (!source) throw new Error(`source ${sourceId} not found`);
|
|
542
|
+
const pending = getPendingJobCountV2(sourceId);
|
|
543
|
+
if (pending >= _stateV2.maxPendingJobsPerSource) {
|
|
544
|
+
throw new Error(
|
|
545
|
+
`source ${sourceId} pending-job cap reached (${pending}/${_stateV2.maxPendingJobsPerSource})`,
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
const now = Date.now();
|
|
549
|
+
const job = {
|
|
550
|
+
id,
|
|
551
|
+
sourceId,
|
|
552
|
+
kind: kind || "scan",
|
|
553
|
+
status: IMPORT_JOB_LIFECYCLE_V2.QUEUED,
|
|
554
|
+
createdAt: now,
|
|
555
|
+
lastSeenAt: now,
|
|
556
|
+
startedAt: null,
|
|
557
|
+
settledAt: null,
|
|
558
|
+
metadata: metadata ? { ...metadata } : {},
|
|
559
|
+
};
|
|
560
|
+
_stateV2.jobs.set(id, job);
|
|
561
|
+
return _copyJobV2(job);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
export function getImportJobV2(id) {
|
|
565
|
+
const j = _stateV2.jobs.get(id);
|
|
566
|
+
return j ? _copyJobV2(j) : null;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
export function listImportJobsV2({ sourceId, status } = {}) {
|
|
570
|
+
const out = [];
|
|
571
|
+
for (const j of _stateV2.jobs.values()) {
|
|
572
|
+
if (sourceId && j.sourceId !== sourceId) continue;
|
|
573
|
+
if (status && j.status !== status) continue;
|
|
574
|
+
out.push(_copyJobV2(j));
|
|
575
|
+
}
|
|
576
|
+
return out;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
export function setImportJobStatusV2(id, next) {
|
|
580
|
+
const j = _stateV2.jobs.get(id);
|
|
581
|
+
if (!j) throw new Error(`job ${id} not found`);
|
|
582
|
+
const allowed = _JOB_TRANSITIONS_V2.get(j.status);
|
|
583
|
+
if (!allowed || !allowed.has(next)) {
|
|
584
|
+
throw new Error(`invalid job transition: ${j.status} → ${next}`);
|
|
585
|
+
}
|
|
586
|
+
const now = Date.now();
|
|
587
|
+
j.status = next;
|
|
588
|
+
j.lastSeenAt = now;
|
|
589
|
+
if (next === IMPORT_JOB_LIFECYCLE_V2.RUNNING && !j.startedAt)
|
|
590
|
+
j.startedAt = now;
|
|
591
|
+
if (_JOB_TERMINALS_V2.has(next) && !j.settledAt) j.settledAt = now;
|
|
592
|
+
return _copyJobV2(j);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
export function startImportJobV2(id) {
|
|
596
|
+
return setImportJobStatusV2(id, IMPORT_JOB_LIFECYCLE_V2.RUNNING);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
export function completeImportJobV2(id) {
|
|
600
|
+
return setImportJobStatusV2(id, IMPORT_JOB_LIFECYCLE_V2.COMPLETED);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
export function failImportJobV2(id) {
|
|
604
|
+
return setImportJobStatusV2(id, IMPORT_JOB_LIFECYCLE_V2.FAILED);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
export function cancelImportJobV2(id) {
|
|
608
|
+
return setImportJobStatusV2(id, IMPORT_JOB_LIFECYCLE_V2.CANCELLED);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
export function autoPauseIdleSourcesV2({ now = Date.now() } = {}) {
|
|
612
|
+
const flipped = [];
|
|
613
|
+
for (const s of _stateV2.sources.values()) {
|
|
614
|
+
if (
|
|
615
|
+
s.status === SOURCE_MATURITY_V2.ACTIVE &&
|
|
616
|
+
now - s.lastSeenAt > _stateV2.sourceIdleMs
|
|
617
|
+
) {
|
|
618
|
+
s.status = SOURCE_MATURITY_V2.PAUSED;
|
|
619
|
+
s.lastSeenAt = now;
|
|
620
|
+
flipped.push(_copySourceV2(s));
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
return flipped;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
export function autoFailStuckImportJobsV2({ now = Date.now() } = {}) {
|
|
627
|
+
const flipped = [];
|
|
628
|
+
for (const j of _stateV2.jobs.values()) {
|
|
629
|
+
if (
|
|
630
|
+
j.status === IMPORT_JOB_LIFECYCLE_V2.RUNNING &&
|
|
631
|
+
now - j.lastSeenAt > _stateV2.jobStuckMs
|
|
632
|
+
) {
|
|
633
|
+
j.status = IMPORT_JOB_LIFECYCLE_V2.FAILED;
|
|
634
|
+
j.lastSeenAt = now;
|
|
635
|
+
if (!j.settledAt) j.settledAt = now;
|
|
636
|
+
flipped.push(_copyJobV2(j));
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
return flipped;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
export function getKnowledgeImporterStatsV2() {
|
|
643
|
+
const sourcesByStatus = {};
|
|
644
|
+
for (const v of Object.values(SOURCE_MATURITY_V2)) sourcesByStatus[v] = 0;
|
|
645
|
+
for (const s of _stateV2.sources.values()) sourcesByStatus[s.status]++;
|
|
646
|
+
|
|
647
|
+
const jobsByStatus = {};
|
|
648
|
+
for (const v of Object.values(IMPORT_JOB_LIFECYCLE_V2)) jobsByStatus[v] = 0;
|
|
649
|
+
for (const j of _stateV2.jobs.values()) jobsByStatus[j.status]++;
|
|
650
|
+
|
|
651
|
+
return {
|
|
652
|
+
totalSourcesV2: _stateV2.sources.size,
|
|
653
|
+
totalImportJobsV2: _stateV2.jobs.size,
|
|
654
|
+
maxActiveSourcesPerOwner: _stateV2.maxActiveSourcesPerOwner,
|
|
655
|
+
maxPendingJobsPerSource: _stateV2.maxPendingJobsPerSource,
|
|
656
|
+
sourceIdleMs: _stateV2.sourceIdleMs,
|
|
657
|
+
jobStuckMs: _stateV2.jobStuckMs,
|
|
658
|
+
sourcesByStatus,
|
|
659
|
+
jobsByStatus,
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
export function _resetStateKnowledgeImporterV2() {
|
|
664
|
+
_stateV2.sources.clear();
|
|
665
|
+
_stateV2.jobs.clear();
|
|
666
|
+
_stateV2.maxActiveSourcesPerOwner =
|
|
667
|
+
IMPORTER_DEFAULT_MAX_ACTIVE_SOURCES_PER_OWNER;
|
|
668
|
+
_stateV2.maxPendingJobsPerSource =
|
|
669
|
+
IMPORTER_DEFAULT_MAX_PENDING_JOBS_PER_SOURCE;
|
|
670
|
+
_stateV2.sourceIdleMs = IMPORTER_DEFAULT_SOURCE_IDLE_MS;
|
|
671
|
+
_stateV2.jobStuckMs = IMPORTER_DEFAULT_JOB_STUCK_MS;
|
|
672
|
+
}
|