@aragon-agent/cli 0.6.55 → 0.6.57
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/CHANGELOG.md +2935 -2864
- package/README.md +160 -45
- package/dist/agent/controller.d.ts +47 -0
- package/dist/agent/controller.js +153 -2
- package/dist/agent/governance-wiring.js +8 -1
- package/dist/agent/system-prompt.d.ts +26 -0
- package/dist/agent/system-prompt.js +11 -0
- package/dist/app/App.js +101 -3
- package/dist/cli.js +29 -1
- package/dist/commands/builtins.js +4 -0
- package/dist/commands/catalog.js +1 -0
- package/dist/commands/registry.d.ts +8 -0
- package/dist/config/app-paths.d.ts +10 -0
- package/dist/config/app-paths.js +12 -0
- package/dist/config/cli-commands.d.ts +29 -0
- package/dist/config/cli-commands.js +76 -1
- package/dist/config/env.d.ts +2 -1
- package/dist/config/env.js +12 -1
- package/dist/config/load.d.ts +10 -0
- package/dist/config/load.js +13 -2
- package/dist/config/store.js +11 -1
- package/dist/exec/bridge/server.d.ts +7 -0
- package/dist/exec/bridge/server.js +6 -2
- package/dist/exec/cli-commands.js +4 -0
- package/dist/exec/index.js +2 -0
- package/dist/exec/options.d.ts +11 -0
- package/dist/exec/options.js +21 -1
- package/dist/gene/block.d.ts +33 -0
- package/dist/gene/block.js +51 -0
- package/dist/gene/capacity.d.ts +74 -0
- package/dist/gene/capacity.js +169 -0
- package/dist/gene/catalog-reconcile.d.ts +13 -0
- package/dist/gene/catalog-reconcile.js +105 -0
- package/dist/gene/catalog-stats.d.ts +27 -0
- package/dist/gene/catalog-stats.js +89 -0
- package/dist/gene/catalog-sync.d.ts +82 -0
- package/dist/gene/catalog-sync.js +324 -0
- package/dist/gene/catalog.d.ts +160 -0
- package/dist/gene/catalog.js +433 -0
- package/dist/gene/cli-commands.d.ts +50 -0
- package/dist/gene/cli-commands.js +500 -0
- package/dist/gene/command-report.d.ts +40 -0
- package/dist/gene/command-report.js +246 -0
- package/dist/gene/command.d.ts +29 -0
- package/dist/gene/command.js +561 -0
- package/dist/gene/commit-apply-types.d.ts +13 -0
- package/dist/gene/commit-apply-types.js +1 -0
- package/dist/gene/commit-compound.d.ts +34 -0
- package/dist/gene/commit-compound.js +212 -0
- package/dist/gene/commit-counts.d.ts +41 -0
- package/dist/gene/commit-counts.js +70 -0
- package/dist/gene/commit-exec.d.ts +42 -0
- package/dist/gene/commit-exec.js +134 -0
- package/dist/gene/commit-head.d.ts +52 -0
- package/dist/gene/commit-head.js +97 -0
- package/dist/gene/commit-plan.d.ts +95 -0
- package/dist/gene/commit-plan.js +68 -0
- package/dist/gene/commit-prepare.d.ts +63 -0
- package/dist/gene/commit-prepare.js +159 -0
- package/dist/gene/commit-types.d.ts +234 -0
- package/dist/gene/commit-types.js +3 -0
- package/dist/gene/curator/agent.d.ts +137 -0
- package/dist/gene/curator/agent.js +203 -0
- package/dist/gene/curator/budget.d.ts +69 -0
- package/dist/gene/curator/budget.js +131 -0
- package/dist/gene/curator/conflicts.d.ts +54 -0
- package/dist/gene/curator/conflicts.js +143 -0
- package/dist/gene/curator/curator.d.ts +170 -0
- package/dist/gene/curator/curator.js +617 -0
- package/dist/gene/curator/deps.d.ts +34 -0
- package/dist/gene/curator/deps.js +48 -0
- package/dist/gene/curator/deterministic.d.ts +62 -0
- package/dist/gene/curator/deterministic.js +287 -0
- package/dist/gene/curator/executors.d.ts +36 -0
- package/dist/gene/curator/executors.js +379 -0
- package/dist/gene/curator/lanes.d.ts +52 -0
- package/dist/gene/curator/lanes.js +99 -0
- package/dist/gene/curator/lease.d.ts +76 -0
- package/dist/gene/curator/lease.js +171 -0
- package/dist/gene/curator/library.d.ts +36 -0
- package/dist/gene/curator/library.js +169 -0
- package/dist/gene/curator/lifecycle.d.ts +52 -0
- package/dist/gene/curator/lifecycle.js +109 -0
- package/dist/gene/curator/maintenance.d.ts +71 -0
- package/dist/gene/curator/maintenance.js +213 -0
- package/dist/gene/curator/probation.d.ts +51 -0
- package/dist/gene/curator/probation.js +71 -0
- package/dist/gene/curator/probe.d.ts +131 -0
- package/dist/gene/curator/probe.js +534 -0
- package/dist/gene/curator/prompt.d.ts +18 -0
- package/dist/gene/curator/prompt.js +128 -0
- package/dist/gene/curator/publish.d.ts +64 -0
- package/dist/gene/curator/publish.js +113 -0
- package/dist/gene/curator/recheck.d.ts +55 -0
- package/dist/gene/curator/recheck.js +104 -0
- package/dist/gene/curator/run-events.d.ts +63 -0
- package/dist/gene/curator/run-events.js +55 -0
- package/dist/gene/curator/similarity.d.ts +13 -0
- package/dist/gene/curator/similarity.js +46 -0
- package/dist/gene/curator/switches.d.ts +78 -0
- package/dist/gene/curator/switches.js +146 -0
- package/dist/gene/curator/task-context.d.ts +93 -0
- package/dist/gene/curator/task-context.js +63 -0
- package/dist/gene/curator/task-runner.d.ts +56 -0
- package/dist/gene/curator/task-runner.js +257 -0
- package/dist/gene/curator/tool-support.d.ts +112 -0
- package/dist/gene/curator/tool-support.js +206 -0
- package/dist/gene/curator/toolkit.d.ts +49 -0
- package/dist/gene/curator/toolkit.js +104 -0
- package/dist/gene/curator/tools-read.d.ts +4 -0
- package/dist/gene/curator/tools-read.js +177 -0
- package/dist/gene/curator/tools-release.d.ts +73 -0
- package/dist/gene/curator/tools-release.js +287 -0
- package/dist/gene/curator/tools-verify.d.ts +47 -0
- package/dist/gene/curator/tools-verify.js +243 -0
- package/dist/gene/curator/tools-write.d.ts +26 -0
- package/dist/gene/curator/tools-write.js +434 -0
- package/dist/gene/curator/tools.d.ts +23 -0
- package/dist/gene/curator/tools.js +21 -0
- package/dist/gene/curator/validate/common.d.ts +87 -0
- package/dist/gene/curator/validate/common.js +132 -0
- package/dist/gene/curator/validate/evidence-tokens.d.ts +24 -0
- package/dist/gene/curator/validate/evidence-tokens.js +217 -0
- package/dist/gene/curator/validate/index.d.ts +23 -0
- package/dist/gene/curator/validate/index.js +62 -0
- package/dist/gene/curator/validate/knowledge.d.ts +41 -0
- package/dist/gene/curator/validate/knowledge.js +255 -0
- package/dist/gene/curator/validate/live-check.d.ts +14 -0
- package/dist/gene/curator/validate/live-check.js +150 -0
- package/dist/gene/curator/validate/ports.d.ts +67 -0
- package/dist/gene/curator/validate/ports.js +1 -0
- package/dist/gene/curator/validate/prompts.d.ts +41 -0
- package/dist/gene/curator/validate/prompts.js +165 -0
- package/dist/gene/curator/validate/skill.d.ts +15 -0
- package/dist/gene/curator/validate/skill.js +251 -0
- package/dist/gene/curator/validate/tool.d.ts +6 -0
- package/dist/gene/curator/validate/tool.js +217 -0
- package/dist/gene/curator/validate/types.d.ts +90 -0
- package/dist/gene/curator/validate/types.js +1 -0
- package/dist/gene/curator/validator-ports.d.ts +38 -0
- package/dist/gene/curator/validator-ports.js +29 -0
- package/dist/gene/diag.d.ts +209 -0
- package/dist/gene/diag.js +549 -0
- package/dist/gene/env.d.ts +10 -0
- package/dist/gene/env.js +214 -0
- package/dist/gene/eval-recorder.d.ts +84 -0
- package/dist/gene/eval-recorder.js +222 -0
- package/dist/gene/explain-transport.d.ts +3 -0
- package/dist/gene/explain-transport.js +39 -0
- package/dist/gene/explain.d.ts +96 -0
- package/dist/gene/explain.js +293 -0
- package/dist/gene/fs.d.ts +39 -0
- package/dist/gene/fs.js +139 -0
- package/dist/gene/fsck.d.ts +68 -0
- package/dist/gene/fsck.js +134 -0
- package/dist/gene/gc-library-sweeps.d.ts +9 -0
- package/dist/gene/gc-library-sweeps.js +220 -0
- package/dist/gene/gc-rejected-set.d.ts +15 -0
- package/dist/gene/gc-rejected-set.js +63 -0
- package/dist/gene/gc-safe.d.ts +12 -0
- package/dist/gene/gc-safe.js +74 -0
- package/dist/gene/gc-scope-sweeps.d.ts +18 -0
- package/dist/gene/gc-scope-sweeps.js +297 -0
- package/dist/gene/gc-types.d.ts +55 -0
- package/dist/gene/gc-types.js +26 -0
- package/dist/gene/gc.d.ts +42 -0
- package/dist/gene/gc.js +129 -0
- package/dist/gene/gene-errors.d.ts +23 -0
- package/dist/gene/gene-errors.js +29 -0
- package/dist/gene/hints.d.ts +91 -0
- package/dist/gene/hints.js +394 -0
- package/dist/gene/hub.d.ts +149 -0
- package/dist/gene/hub.js +371 -0
- package/dist/gene/import-plan.d.ts +80 -0
- package/dist/gene/import-plan.js +294 -0
- package/dist/gene/import-proposal.d.ts +23 -0
- package/dist/gene/import-proposal.js +101 -0
- package/dist/gene/import-skill-port.d.ts +6 -0
- package/dist/gene/import-skill-port.js +53 -0
- package/dist/gene/import-sources.d.ts +48 -0
- package/dist/gene/import-sources.js +140 -0
- package/dist/gene/index-writer.d.ts +36 -0
- package/dist/gene/index-writer.js +101 -0
- package/dist/gene/learn/learn-call.d.ts +116 -0
- package/dist/gene/learn/learn-call.js +165 -0
- package/dist/gene/learn/learn-prompt.d.ts +58 -0
- package/dist/gene/learn/learn-prompt.js +254 -0
- package/dist/gene/learn/learner.d.ts +187 -0
- package/dist/gene/learn/learner.js +448 -0
- package/dist/gene/learn/ledger.d.ts +220 -0
- package/dist/gene/learn/ledger.js +394 -0
- package/dist/gene/learn/proposal-build.d.ts +83 -0
- package/dist/gene/learn/proposal-build.js +207 -0
- package/dist/gene/learn/proposal-guard.d.ts +43 -0
- package/dist/gene/learn/proposal-guard.js +37 -0
- package/dist/gene/learn/signals-port.d.ts +47 -0
- package/dist/gene/learn/signals-port.js +86 -0
- package/dist/gene/maintenance-paths.d.ts +7 -0
- package/dist/gene/maintenance-paths.js +20 -0
- package/dist/gene/maintenance-probation.d.ts +33 -0
- package/dist/gene/maintenance-probation.js +127 -0
- package/dist/gene/maintenance-proposals.d.ts +24 -0
- package/dist/gene/maintenance-proposals.js +68 -0
- package/dist/gene/maintenance-runner.d.ts +92 -0
- package/dist/gene/maintenance-runner.js +262 -0
- package/dist/gene/maintenance-tasks.d.ts +37 -0
- package/dist/gene/maintenance-tasks.js +95 -0
- package/dist/gene/manager-actions.d.ts +12 -0
- package/dist/gene/manager-actions.js +279 -0
- package/dist/gene/manager-approve.d.ts +29 -0
- package/dist/gene/manager-approve.js +84 -0
- package/dist/gene/manager-context.d.ts +90 -0
- package/dist/gene/manager-context.js +201 -0
- package/dist/gene/manager-detail.d.ts +17 -0
- package/dist/gene/manager-detail.js +313 -0
- package/dist/gene/manager-gc.d.ts +13 -0
- package/dist/gene/manager-gc.js +81 -0
- package/dist/gene/manager-library.d.ts +39 -0
- package/dist/gene/manager-library.js +90 -0
- package/dist/gene/manager-list.d.ts +27 -0
- package/dist/gene/manager-list.js +170 -0
- package/dist/gene/manager-queue.d.ts +16 -0
- package/dist/gene/manager-queue.js +124 -0
- package/dist/gene/manager-stats.d.ts +28 -0
- package/dist/gene/manager-stats.js +53 -0
- package/dist/gene/manager-view.d.ts +27 -0
- package/dist/gene/manager-view.js +124 -0
- package/dist/gene/manager.d.ts +56 -0
- package/dist/gene/manager.js +179 -0
- package/dist/gene/orphan-scopes.d.ts +37 -0
- package/dist/gene/orphan-scopes.js +150 -0
- package/dist/gene/paths.d.ts +73 -0
- package/dist/gene/paths.js +239 -0
- package/dist/gene/plan-content.d.ts +73 -0
- package/dist/gene/plan-content.js +522 -0
- package/dist/gene/plan-lifecycle.d.ts +29 -0
- package/dist/gene/plan-lifecycle.js +310 -0
- package/dist/gene/policy.d.ts +71 -0
- package/dist/gene/policy.js +313 -0
- package/dist/gene/project-learning.d.ts +25 -0
- package/dist/gene/project-learning.js +54 -0
- package/dist/gene/propagation.d.ts +48 -0
- package/dist/gene/propagation.js +45 -0
- package/dist/gene/queue-lock.d.ts +22 -0
- package/dist/gene/queue-lock.js +99 -0
- package/dist/gene/queue-record.d.ts +57 -0
- package/dist/gene/queue-record.js +145 -0
- package/dist/gene/queue.d.ts +235 -0
- package/dist/gene/queue.js +764 -0
- package/dist/gene/read.d.ts +61 -0
- package/dist/gene/read.js +252 -0
- package/dist/gene/runlog.d.ts +101 -0
- package/dist/gene/runlog.js +404 -0
- package/dist/gene/schema-lite.d.ts +24 -0
- package/dist/gene/schema-lite.js +159 -0
- package/dist/gene/scope-guard.d.ts +23 -0
- package/dist/gene/scope-guard.js +9 -0
- package/dist/gene/scope-lock.d.ts +48 -0
- package/dist/gene/scope-lock.js +222 -0
- package/dist/gene/scope-verify.d.ts +63 -0
- package/dist/gene/scope-verify.js +123 -0
- package/dist/gene/secrets.d.ts +43 -0
- package/dist/gene/secrets.js +161 -0
- package/dist/gene/service-context.d.ts +43 -0
- package/dist/gene/service-context.js +79 -0
- package/dist/gene/service-prepare.d.ts +25 -0
- package/dist/gene/service-prepare.js +219 -0
- package/dist/gene/service-types.d.ts +196 -0
- package/dist/gene/service-types.js +1 -0
- package/dist/gene/service-view.d.ts +41 -0
- package/dist/gene/service-view.js +69 -0
- package/dist/gene/service.d.ts +114 -0
- package/dist/gene/service.js +761 -0
- package/dist/gene/session-start.d.ts +36 -0
- package/dist/gene/session-start.js +212 -0
- package/dist/gene/store-port.d.ts +88 -0
- package/dist/gene/store-port.js +10 -0
- package/dist/gene/store-rows.d.ts +36 -0
- package/dist/gene/store-rows.js +72 -0
- package/dist/gene/store.d.ts +125 -0
- package/dist/gene/store.js +622 -0
- package/dist/gene/tombstones.d.ts +47 -0
- package/dist/gene/tombstones.js +61 -0
- package/dist/gene/tool-runner.d.ts +47 -0
- package/dist/gene/tool-runner.js +154 -0
- package/dist/gene/tool-screen.d.ts +41 -0
- package/dist/gene/tool-screen.js +227 -0
- package/dist/gene/tools.d.ts +112 -0
- package/dist/gene/tools.js +539 -0
- package/dist/gene/transfer.d.ts +85 -0
- package/dist/gene/transfer.js +433 -0
- package/dist/gene/ui-hints.d.ts +15 -0
- package/dist/gene/ui-hints.js +67 -0
- package/dist/gene/usage-fold-cursor.d.ts +24 -0
- package/dist/gene/usage-fold-cursor.js +115 -0
- package/dist/gene/usage-fold.d.ts +49 -0
- package/dist/gene/usage-fold.js +397 -0
- package/dist/gene/usage.d.ts +67 -0
- package/dist/gene/usage.js +231 -0
- package/dist/gene/version-service.d.ts +118 -0
- package/dist/gene/version-service.js +453 -0
- package/dist/gene/wiring-events.d.ts +43 -0
- package/dist/gene/wiring-events.js +113 -0
- package/dist/gene/wiring-models.d.ts +46 -0
- package/dist/gene/wiring-models.js +84 -0
- package/dist/gene/wiring.d.ts +200 -0
- package/dist/gene/wiring.js +764 -0
- package/dist/governance/prompts/orchestrator-block.js +3 -2
- package/dist/host/gene.d.ts +26 -0
- package/dist/host/gene.js +24 -0
- package/dist/host/index.d.ts +1 -0
- package/dist/host/index.js +1 -0
- package/dist/team/runtime.d.ts +5 -0
- package/dist/team/runtime.js +23 -0
- package/dist/team/subagent.d.ts +34 -0
- package/dist/team/subagent.js +39 -2
- package/dist/tools/index.d.ts +36 -2
- package/dist/tools/index.js +48 -7
- package/package.json +3 -3
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The catalog's read-only view of the folded statistics shards (gene-bank spec 6.8, 7.5).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* Statistics are EVENTUALLY CONSISTENT by design: the lease holder folds the usage logs into
|
|
7
|
+
* `stats/<aa>.json` once a minute. A reader therefore never needs a fresh value, it needs an
|
|
8
|
+
* answer that costs nothing during ranking. Ranking (`GeneSearchIndex.search`) wants a
|
|
9
|
+
* SYNCHRONOUS `(id) => GeneStats | undefined`, so this cache is filled ahead of time by an
|
|
10
|
+
* async `warm()` and read synchronously afterwards; a gene whose shard is not cached yet simply
|
|
11
|
+
* ranks as a gene with no history.
|
|
12
|
+
*
|
|
13
|
+
* `warm()` lists the `stats/` directory once and stats each shard file: only a shard whose
|
|
14
|
+
* (size, mtime) moved is re-read, so a refresh on an idle library is one `readdir` plus N cheap
|
|
15
|
+
* `stat`s, and a library with no statistics at all is a single `readdir` that finds nothing.
|
|
16
|
+
*/
|
|
17
|
+
import { geneShard } from '@aragon-agent/tui/model/gene/ids';
|
|
18
|
+
import { statsDir, statsFile } from './paths.js';
|
|
19
|
+
const SHARD_FILE = /^([0-9a-z_]{2})\.json$/;
|
|
20
|
+
const CONCURRENCY = 8;
|
|
21
|
+
export class StatsCache {
|
|
22
|
+
options;
|
|
23
|
+
shards = new Map();
|
|
24
|
+
warmedAt = Number.NEGATIVE_INFINITY;
|
|
25
|
+
inflight = null;
|
|
26
|
+
constructor(options) {
|
|
27
|
+
this.options = options;
|
|
28
|
+
}
|
|
29
|
+
/** Synchronous; `undefined` for a gene whose shard was never loaded or has no entry. */
|
|
30
|
+
get(sid, id) {
|
|
31
|
+
return this.shards.get(`${sid}/${geneShard(id)}`)?.data[id];
|
|
32
|
+
}
|
|
33
|
+
isStale() {
|
|
34
|
+
return this.options.now() - this.warmedAt > this.options.ttlMs;
|
|
35
|
+
}
|
|
36
|
+
hasWarmed() {
|
|
37
|
+
return Number.isFinite(this.warmedAt);
|
|
38
|
+
}
|
|
39
|
+
/** Drop everything cached for a scope that left the visible set. */
|
|
40
|
+
forget(sid) {
|
|
41
|
+
for (const key of [...this.shards.keys()]) {
|
|
42
|
+
if (key.startsWith(`${sid}/`))
|
|
43
|
+
this.shards.delete(key);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Single-flight; never rejects. */
|
|
47
|
+
warm(scopes, sidOf) {
|
|
48
|
+
if (this.inflight !== null)
|
|
49
|
+
return this.inflight;
|
|
50
|
+
this.inflight = this.warmAll(scopes, sidOf)
|
|
51
|
+
.catch(() => undefined)
|
|
52
|
+
.finally(() => {
|
|
53
|
+
this.warmedAt = this.options.now();
|
|
54
|
+
this.inflight = null;
|
|
55
|
+
});
|
|
56
|
+
return this.inflight;
|
|
57
|
+
}
|
|
58
|
+
async warmAll(scopes, sidOf) {
|
|
59
|
+
for (const scope of scopes) {
|
|
60
|
+
const dir = statsDir(scope, this.options.reader.ctx);
|
|
61
|
+
if (dir === null)
|
|
62
|
+
continue;
|
|
63
|
+
const names = await this.options.io.listDir(dir);
|
|
64
|
+
const shards = names
|
|
65
|
+
.map((name) => SHARD_FILE.exec(name)?.[1])
|
|
66
|
+
.filter((s) => s !== undefined);
|
|
67
|
+
for (let i = 0; i < shards.length; i += CONCURRENCY) {
|
|
68
|
+
const batch = shards.slice(i, i + CONCURRENCY);
|
|
69
|
+
await Promise.all(batch.map((shard) => this.refreshShard(scope, sidOf(scope), shard)));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async refreshShard(scope, sid, shard) {
|
|
74
|
+
const path = statsFile(scope, shard, this.options.reader.ctx);
|
|
75
|
+
if (path === null)
|
|
76
|
+
return;
|
|
77
|
+
const stamp = await this.options.io.stat(path);
|
|
78
|
+
const key = `${sid}/${shard}`;
|
|
79
|
+
const known = this.shards.get(key);
|
|
80
|
+
const unchanged = known !== undefined &&
|
|
81
|
+
stamp !== null &&
|
|
82
|
+
known.stamp?.size === stamp.size &&
|
|
83
|
+
known.stamp.mtimeMs === stamp.mtimeMs;
|
|
84
|
+
if (unchanged)
|
|
85
|
+
return;
|
|
86
|
+
const data = stamp === null ? {} : await this.options.reader.readStatsShard(scope, shard);
|
|
87
|
+
this.shards.set(key, { stamp, data: data ?? {} });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { GeneSearchIndex } from '@aragon-agent/tui/model/gene/search';
|
|
2
|
+
import type { GeneIndexRow, GeneScope } from '@aragon-agent/tui/model/gene/types';
|
|
3
|
+
import { type FileStamp, type JsonlRead } from './fs.js';
|
|
4
|
+
import type { GeneStoreReader } from './store-port.js';
|
|
5
|
+
/** The four file operations the catalog performs; injectable so a test can count them. */
|
|
6
|
+
export interface CatalogIo {
|
|
7
|
+
stat(path: string): Promise<FileStamp | null>;
|
|
8
|
+
readJson<T>(path: string): Promise<T | null>;
|
|
9
|
+
readJsonlFrom<T>(path: string, offset: number): Promise<JsonlRead<T>>;
|
|
10
|
+
listDir(path: string): Promise<string[]>;
|
|
11
|
+
}
|
|
12
|
+
export declare const FS_IO: CatalogIo;
|
|
13
|
+
export interface ScopeState {
|
|
14
|
+
scope: GeneScope;
|
|
15
|
+
sid: string;
|
|
16
|
+
/** The first sync of this scope has completed (successfully or as an empty scope). */
|
|
17
|
+
loaded: boolean;
|
|
18
|
+
/** The last sync threw; the scope contributes nothing until a later pass succeeds. */
|
|
19
|
+
failed: boolean;
|
|
20
|
+
gen: number;
|
|
21
|
+
/** Bytes of the journal already applied (always on a line boundary). */
|
|
22
|
+
journalOffset: number;
|
|
23
|
+
/** Journal size seen by the last successful sync; the O(1) unchanged test compares it. */
|
|
24
|
+
journalSize: number;
|
|
25
|
+
/** Entries applied since the snapshot was loaded; drives the compaction trigger. */
|
|
26
|
+
journalLines: number;
|
|
27
|
+
snapStamp: FileStamp | null;
|
|
28
|
+
rowIds: Set<string>;
|
|
29
|
+
}
|
|
30
|
+
export declare function newScopeState(scope: GeneScope): ScopeState;
|
|
31
|
+
export interface SyncEnv {
|
|
32
|
+
reader: GeneStoreReader;
|
|
33
|
+
io: CatalogIo;
|
|
34
|
+
index: GeneSearchIndex;
|
|
35
|
+
vecAlgo: number;
|
|
36
|
+
/** Heads read between two yields to the event loop (spec 7.7: 200). */
|
|
37
|
+
yieldEvery: number;
|
|
38
|
+
yieldTo(): Promise<void>;
|
|
39
|
+
/** Readers see `index_rebuilding` while a scope is being rescanned. */
|
|
40
|
+
setRebuilding(sid: string, on: boolean): void;
|
|
41
|
+
/** Called after every mutation of the shared index: lets cached views notice. */
|
|
42
|
+
touched(): void;
|
|
43
|
+
}
|
|
44
|
+
export type SyncMode = 'auto' | 'reload' | 'rebuild';
|
|
45
|
+
export type SyncKind = 'unchanged' | 'tailed' | 'reloaded' | 'rebuilt';
|
|
46
|
+
export interface SyncOutcome {
|
|
47
|
+
kind: SyncKind;
|
|
48
|
+
applied: number;
|
|
49
|
+
/** A published row appeared, disappeared or changed. */
|
|
50
|
+
visibleChanged: boolean;
|
|
51
|
+
/** The very first sync of this scope: never reported as a change to the user. */
|
|
52
|
+
initial: boolean;
|
|
53
|
+
}
|
|
54
|
+
interface IndexPaths {
|
|
55
|
+
snapshot: string;
|
|
56
|
+
journal: string;
|
|
57
|
+
}
|
|
58
|
+
/** Insert or replace one row. Returns true when a PUBLISHED row visibly changed. */
|
|
59
|
+
export declare function putRow(env: SyncEnv, state: ScopeState, row: GeneIndexRow): boolean;
|
|
60
|
+
/** Remove one row. Returns true when a PUBLISHED row disappeared. */
|
|
61
|
+
export declare function dropRow(env: SyncEnv, state: ScopeState, id: string): boolean;
|
|
62
|
+
export declare function replaceRows(env: SyncEnv, state: ScopeState, rows: readonly GeneIndexRow[]): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Rows put between two yields during a REBUILD. Reading heads is I/O and yields every
|
|
65
|
+
* `yieldEvery` files, but tokenising and embedding a row is CPU (about 0.2 ms each): putting a
|
|
66
|
+
* whole scope in one synchronous loop blocked the event loop for hundreds of milliseconds, which
|
|
67
|
+
* a user turn feels as a stall (bench-gene `rebuild.eventLoopDelayP99Ms`, spec 17.4 / I3-2).
|
|
68
|
+
*/
|
|
69
|
+
export declare const REBUILD_PUT_SLICE = 50;
|
|
70
|
+
declare function indexPaths(env: SyncEnv, scope: GeneScope): IndexPaths | null;
|
|
71
|
+
/**
|
|
72
|
+
* Rescan the heads. The journal boundary is read BEFORE the scan so that entries appended while
|
|
73
|
+
* it runs are replayed afterwards (a `put` is idempotent; a missed one would be a lost update).
|
|
74
|
+
*/
|
|
75
|
+
export declare function rebuildScope(env: SyncEnv, state: ScopeState, paths: IndexPaths): Promise<SyncOutcome>;
|
|
76
|
+
/**
|
|
77
|
+
* Bring one scope up to date. Never mutates anything but the in-memory index and `state`.
|
|
78
|
+
* `mode: 'auto'` is the cheap path; the others are forced by the caller (rebuild-on-demand, tests).
|
|
79
|
+
*/
|
|
80
|
+
export declare function syncScope(env: SyncEnv, state: ScopeState, mode?: SyncMode): Promise<SyncOutcome>;
|
|
81
|
+
export { indexPaths };
|
|
82
|
+
export type { IndexPaths };
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-scope index synchronisation behind `GeneCatalog` (gene-bank spec 7.7, D-39, RV-8).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* ONE SCOPE = ONE SNAPSHOT + ONE JOURNAL. The version service appends a `put` / `del` line to
|
|
7
|
+
* `index/journal.jsonl` at every commit point and the lease holder periodically folds the journal
|
|
8
|
+
* into `index/snapshot.json` (gen + 1) and truncates it. A READER therefore only ever does one of
|
|
9
|
+
* three things, cheapest first:
|
|
10
|
+
*
|
|
11
|
+
* unchanged two `stat`s say neither file moved: nothing is read (the O(1) path every search and
|
|
12
|
+
* every context plan takes)
|
|
13
|
+
* tailed the journal grew: read ONLY the new complete lines, apply them
|
|
14
|
+
* reloaded the snapshot was replaced (compaction): re-read it, then tail from its offset
|
|
15
|
+
* rebuilt the index cannot be trusted (no snapshot but heads exist, schema / vecAlgo moved
|
|
16
|
+
* on,
|
|
17
|
+
* a line that does not parse): rescan the head files, which are the truth
|
|
18
|
+
*
|
|
19
|
+
* THE HEADS ARE THE TRUTH (7.7). Everything here is a cache of them, so every failure mode ends
|
|
20
|
+
* in "rebuild from heads", never in an exception that reaches a user turn.
|
|
21
|
+
*
|
|
22
|
+
* NOTHING IN THIS FILE WRITES. The catalog is a reader (I-4); compaction goes through the injected
|
|
23
|
+
* `IndexWriter` of the lease holder.
|
|
24
|
+
*/
|
|
25
|
+
import { sameScope, scopeId } from '@aragon-agent/tui/model/gene/ids';
|
|
26
|
+
import { GENE_LIMITS } from '@aragon-agent/tui/model/gene/limits';
|
|
27
|
+
import { rowFromHead } from '@aragon-agent/tui/model/gene/row';
|
|
28
|
+
import { isUsableRow } from '@aragon-agent/tui/model/gene/search-support';
|
|
29
|
+
import { listDir, readJson, readJsonlFrom, statOrNull, } from './fs.js';
|
|
30
|
+
import { indexFile } from './paths.js';
|
|
31
|
+
export const FS_IO = {
|
|
32
|
+
stat: statOrNull,
|
|
33
|
+
readJson,
|
|
34
|
+
readJsonlFrom: (path, offset) => readJsonlFrom(path, offset),
|
|
35
|
+
listDir,
|
|
36
|
+
};
|
|
37
|
+
export function newScopeState(scope) {
|
|
38
|
+
return {
|
|
39
|
+
scope,
|
|
40
|
+
sid: scopeId(scope),
|
|
41
|
+
loaded: false,
|
|
42
|
+
failed: false,
|
|
43
|
+
gen: -1,
|
|
44
|
+
journalOffset: 0,
|
|
45
|
+
journalSize: 0,
|
|
46
|
+
journalLines: 0,
|
|
47
|
+
snapStamp: null,
|
|
48
|
+
rowIds: new Set(),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
// Row operations (every mutation of the shared index goes through these two)
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
function isVisibleChange(old, next) {
|
|
55
|
+
const was = old?.status === 'published';
|
|
56
|
+
const is = next.status === 'published';
|
|
57
|
+
if (was !== is)
|
|
58
|
+
return true;
|
|
59
|
+
return was && old?.rev !== next.rev;
|
|
60
|
+
}
|
|
61
|
+
/** Insert or replace one row. Returns true when a PUBLISHED row visibly changed. */
|
|
62
|
+
export function putRow(env, state, row) {
|
|
63
|
+
const old = env.index.get(row.id);
|
|
64
|
+
env.index.put(row);
|
|
65
|
+
state.rowIds.add(row.id);
|
|
66
|
+
env.touched();
|
|
67
|
+
return isVisibleChange(old, row);
|
|
68
|
+
}
|
|
69
|
+
/** Remove one row. Returns true when a PUBLISHED row disappeared. */
|
|
70
|
+
export function dropRow(env, state, id) {
|
|
71
|
+
const old = env.index.get(id);
|
|
72
|
+
env.index.remove(id);
|
|
73
|
+
state.rowIds.delete(id);
|
|
74
|
+
env.touched();
|
|
75
|
+
return old?.status === 'published';
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Make the scope's rows exactly `rows`, touching only what differs. A row with an unchanged
|
|
79
|
+
* `rev` is skipped, so reloading a snapshot that another process just compacted costs a map
|
|
80
|
+
* build and a loop, not a re-tokenisation of the library.
|
|
81
|
+
*/
|
|
82
|
+
function dropMissing(env, state, keep) {
|
|
83
|
+
let changed = false;
|
|
84
|
+
for (const id of [...state.rowIds]) {
|
|
85
|
+
if (!keep.has(id))
|
|
86
|
+
changed = dropRow(env, state, id) || changed;
|
|
87
|
+
}
|
|
88
|
+
return changed;
|
|
89
|
+
}
|
|
90
|
+
function isCurrent(env, state, row) {
|
|
91
|
+
const old = env.index.get(row.id);
|
|
92
|
+
return old !== undefined && old.rev === row.rev && state.rowIds.has(row.id);
|
|
93
|
+
}
|
|
94
|
+
function usableById(state, rows) {
|
|
95
|
+
const next = new Map();
|
|
96
|
+
for (const row of rows) {
|
|
97
|
+
if (isUsableRow(row) && sameScope(row.scope, state.scope))
|
|
98
|
+
next.set(row.id, row);
|
|
99
|
+
}
|
|
100
|
+
return next;
|
|
101
|
+
}
|
|
102
|
+
export function replaceRows(env, state, rows) {
|
|
103
|
+
const next = usableById(state, rows);
|
|
104
|
+
let changed = dropMissing(env, state, next);
|
|
105
|
+
for (const row of next.values()) {
|
|
106
|
+
if (!isCurrent(env, state, row))
|
|
107
|
+
changed = putRow(env, state, row) || changed;
|
|
108
|
+
}
|
|
109
|
+
return changed;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Rows put between two yields during a REBUILD. Reading heads is I/O and yields every
|
|
113
|
+
* `yieldEvery` files, but tokenising and embedding a row is CPU (about 0.2 ms each): putting a
|
|
114
|
+
* whole scope in one synchronous loop blocked the event loop for hundreds of milliseconds, which
|
|
115
|
+
* a user turn feels as a stall (bench-gene `rebuild.eventLoopDelayP99Ms`, spec 17.4 / I3-2).
|
|
116
|
+
*/
|
|
117
|
+
export const REBUILD_PUT_SLICE = 50;
|
|
118
|
+
/**
|
|
119
|
+
* `replaceRows` that lets the event loop breathe. ONLY the rebuild may use it: `setRebuilding`
|
|
120
|
+
* hides the half-filled index from readers (they see `index_rebuilding`); a plain reload has no
|
|
121
|
+
* such guard and must stay atomic.
|
|
122
|
+
*/
|
|
123
|
+
async function replaceRowsInSlices(env, state, rows) {
|
|
124
|
+
const next = usableById(state, rows);
|
|
125
|
+
let changed = dropMissing(env, state, next);
|
|
126
|
+
let sinceYield = 0;
|
|
127
|
+
for (const row of next.values()) {
|
|
128
|
+
if (isCurrent(env, state, row))
|
|
129
|
+
continue;
|
|
130
|
+
changed = putRow(env, state, row) || changed;
|
|
131
|
+
sinceYield += 1;
|
|
132
|
+
if (sinceYield >= REBUILD_PUT_SLICE) {
|
|
133
|
+
sinceYield = 0;
|
|
134
|
+
await env.yieldTo();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return changed;
|
|
138
|
+
}
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
// Snapshot / journal parsing
|
|
141
|
+
// ---------------------------------------------------------------------------
|
|
142
|
+
function usableSnapshot(snapshot, vecAlgo) {
|
|
143
|
+
return (snapshot !== null &&
|
|
144
|
+
snapshot.schema === 1 &&
|
|
145
|
+
snapshot.vecAlgo === vecAlgo &&
|
|
146
|
+
Array.isArray(snapshot.rows) &&
|
|
147
|
+
Number.isInteger(snapshot.gen) &&
|
|
148
|
+
Number.isInteger(snapshot.journalOffset) &&
|
|
149
|
+
snapshot.journalOffset >= 0);
|
|
150
|
+
}
|
|
151
|
+
/** A line of another scope is ignored (it must not poison this scope); junk is `null`. */
|
|
152
|
+
function parseEntry(raw, scope) {
|
|
153
|
+
if (raw === null || typeof raw !== 'object')
|
|
154
|
+
return null;
|
|
155
|
+
const entry = raw;
|
|
156
|
+
if (entry.op === 'del')
|
|
157
|
+
return typeof entry.id === 'string' ? { op: 'del', id: entry.id } : null;
|
|
158
|
+
if (entry.op !== 'put' || !isUsableRow(entry.row))
|
|
159
|
+
return null;
|
|
160
|
+
return sameScope(entry.row.scope, scope) ? { op: 'put', row: entry.row } : 'foreign';
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Apply the complete journal lines past the stored offset. A torn trailing line (the writer is
|
|
164
|
+
* mid-append) is left unconsumed by `readJsonlFrom` and picked up whole next time. `tolerant`
|
|
165
|
+
* is for the pass right after a rebuild, where a bad line is already covered by the head scan.
|
|
166
|
+
*/
|
|
167
|
+
async function tailJournal(env, state, journalPath, tolerant = false) {
|
|
168
|
+
const read = await env.io.readJsonlFrom(journalPath, state.journalOffset);
|
|
169
|
+
if (read.corrupt < 0) {
|
|
170
|
+
if (!tolerant)
|
|
171
|
+
return { status: 'truncated', applied: 0, visibleChanged: false };
|
|
172
|
+
state.journalOffset = 0;
|
|
173
|
+
return { status: 'ok', applied: 0, visibleChanged: false };
|
|
174
|
+
}
|
|
175
|
+
let bad = read.corrupt;
|
|
176
|
+
let applied = 0;
|
|
177
|
+
let changed = false;
|
|
178
|
+
for (const raw of read.rows) {
|
|
179
|
+
const entry = parseEntry(raw, state.scope);
|
|
180
|
+
if (entry === 'foreign')
|
|
181
|
+
continue;
|
|
182
|
+
if (entry === null) {
|
|
183
|
+
bad += 1;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const moved = entry.op === 'put' ? putRow(env, state, entry.row) : dropRow(env, state, entry.id);
|
|
187
|
+
changed = moved || changed;
|
|
188
|
+
applied += 1;
|
|
189
|
+
}
|
|
190
|
+
if (bad > 0 && !tolerant)
|
|
191
|
+
return { status: 'corrupt', applied, visibleChanged: changed };
|
|
192
|
+
state.journalOffset = read.nextOffset;
|
|
193
|
+
state.journalLines += applied;
|
|
194
|
+
return { status: 'ok', applied, visibleChanged: changed };
|
|
195
|
+
}
|
|
196
|
+
// ---------------------------------------------------------------------------
|
|
197
|
+
// The four ways a sync can end
|
|
198
|
+
// ---------------------------------------------------------------------------
|
|
199
|
+
function sameStamp(a, b) {
|
|
200
|
+
if (a === null || b === null)
|
|
201
|
+
return a === b;
|
|
202
|
+
return a.size === b.size && a.mtimeMs === b.mtimeMs;
|
|
203
|
+
}
|
|
204
|
+
function indexPaths(env, scope) {
|
|
205
|
+
const snapshot = indexFile(scope, 'snapshot.json', env.reader.ctx);
|
|
206
|
+
const journal = indexFile(scope, 'journal.jsonl', env.reader.ctx);
|
|
207
|
+
return snapshot !== null && journal !== null ? { snapshot, journal } : null;
|
|
208
|
+
}
|
|
209
|
+
async function restat(env, paths) {
|
|
210
|
+
const [snap, journal] = await Promise.all([
|
|
211
|
+
env.io.stat(paths.snapshot),
|
|
212
|
+
env.io.stat(paths.journal),
|
|
213
|
+
]);
|
|
214
|
+
return { snap, journalSize: journal?.size ?? 0 };
|
|
215
|
+
}
|
|
216
|
+
function commit(state, stamps) {
|
|
217
|
+
state.snapStamp = stamps.snap;
|
|
218
|
+
state.journalSize = stamps.journalSize;
|
|
219
|
+
}
|
|
220
|
+
function done(kind, applied, visibleChanged) {
|
|
221
|
+
return { kind, applied, visibleChanged, initial: false };
|
|
222
|
+
}
|
|
223
|
+
/** Heads of one scope -> rows, in slices so the event loop breathes (7.7: every 200 files). */
|
|
224
|
+
async function scanHeads(env, scope) {
|
|
225
|
+
const ids = (await env.reader.listHeadIds(scope)).slice(0, GENE_LIMITS.maxScanEntries);
|
|
226
|
+
const rows = [];
|
|
227
|
+
for (let start = 0; start < ids.length; start += env.yieldEvery) {
|
|
228
|
+
const slice = ids.slice(start, start + env.yieldEvery);
|
|
229
|
+
const reads = await Promise.all(slice.map((id) => env.reader.readHead(scope, id)));
|
|
230
|
+
for (const read of reads) {
|
|
231
|
+
if (read === null)
|
|
232
|
+
continue;
|
|
233
|
+
rows.push(rowFromHead(read.head, { rev: read.rev, mtimeMs: read.mtimeMs, size: read.size }));
|
|
234
|
+
}
|
|
235
|
+
await env.yieldTo();
|
|
236
|
+
}
|
|
237
|
+
return rows;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Rescan the heads. The journal boundary is read BEFORE the scan so that entries appended while
|
|
241
|
+
* it runs are replayed afterwards (a `put` is idempotent; a missed one would be a lost update).
|
|
242
|
+
*/
|
|
243
|
+
export async function rebuildScope(env, state, paths) {
|
|
244
|
+
env.setRebuilding(state.sid, true);
|
|
245
|
+
try {
|
|
246
|
+
const boundary = await env.io.readJsonlFrom(paths.journal, 0);
|
|
247
|
+
const rows = await scanHeads(env, state.scope);
|
|
248
|
+
const changed = await replaceRowsInSlices(env, state, rows);
|
|
249
|
+
state.gen = -1;
|
|
250
|
+
state.journalOffset = Math.max(0, boundary.nextOffset);
|
|
251
|
+
state.journalLines = 0;
|
|
252
|
+
const after = await restat(env, paths);
|
|
253
|
+
const tail = await tailJournal(env, state, paths.journal, true);
|
|
254
|
+
commit(state, after);
|
|
255
|
+
return done('rebuilt', rows.length + tail.applied, changed || tail.visibleChanged);
|
|
256
|
+
}
|
|
257
|
+
finally {
|
|
258
|
+
env.setRebuilding(state.sid, false);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
async function reloadScope(env, state, paths, first) {
|
|
262
|
+
let stamps = first;
|
|
263
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
264
|
+
const snapshot = stamps.snap === null ? null : await env.io.readJson(paths.snapshot);
|
|
265
|
+
if (!usableSnapshot(snapshot, env.vecAlgo))
|
|
266
|
+
break;
|
|
267
|
+
const changed = replaceRows(env, state, snapshot.rows);
|
|
268
|
+
state.gen = snapshot.gen;
|
|
269
|
+
state.journalOffset = snapshot.journalOffset;
|
|
270
|
+
state.journalLines = 0;
|
|
271
|
+
const tail = await tailJournal(env, state, paths.journal);
|
|
272
|
+
if (tail.status === 'ok') {
|
|
273
|
+
commit(state, stamps);
|
|
274
|
+
return done('reloaded', snapshot.rows.length + tail.applied, changed || tail.visibleChanged);
|
|
275
|
+
}
|
|
276
|
+
if (tail.status === 'corrupt')
|
|
277
|
+
break;
|
|
278
|
+
// `truncated`: a compaction replaced the snapshot while we were reading. Look again, once.
|
|
279
|
+
stamps = await restat(env, paths);
|
|
280
|
+
}
|
|
281
|
+
return rebuildScope(env, state, paths);
|
|
282
|
+
}
|
|
283
|
+
async function tailScope(env, state, paths, stamps) {
|
|
284
|
+
const tail = await tailJournal(env, state, paths.journal);
|
|
285
|
+
if (tail.status !== 'ok')
|
|
286
|
+
return rebuildScope(env, state, paths);
|
|
287
|
+
commit(state, stamps);
|
|
288
|
+
return done('tailed', tail.applied, tail.visibleChanged);
|
|
289
|
+
}
|
|
290
|
+
/** A scope with no addressable directory (team without a path) is simply empty. */
|
|
291
|
+
function adoptEmpty(env, state) {
|
|
292
|
+
const changed = replaceRows(env, state, []);
|
|
293
|
+
return done('reloaded', 0, changed);
|
|
294
|
+
}
|
|
295
|
+
function needsReload(state, stamps, mode) {
|
|
296
|
+
if (mode === 'reload' || !state.loaded)
|
|
297
|
+
return true;
|
|
298
|
+
return !sameStamp(stamps.snap, state.snapStamp) || stamps.journalSize < state.journalOffset;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Bring one scope up to date. Never mutates anything but the in-memory index and `state`.
|
|
302
|
+
* `mode: 'auto'` is the cheap path; the others are forced by the caller (rebuild-on-demand, tests).
|
|
303
|
+
*/
|
|
304
|
+
export async function syncScope(env, state, mode = 'auto') {
|
|
305
|
+
const initial = !state.loaded;
|
|
306
|
+
const paths = indexPaths(env, state.scope);
|
|
307
|
+
const outcome = paths === null ? adoptEmpty(env, state) : await syncAt(env, state, paths, mode);
|
|
308
|
+
state.loaded = true;
|
|
309
|
+
state.failed = false;
|
|
310
|
+
return { ...outcome, initial };
|
|
311
|
+
}
|
|
312
|
+
async function syncAt(env, state, paths, mode) {
|
|
313
|
+
const stamps = await restat(env, paths);
|
|
314
|
+
const settled = state.loaded && !state.failed && sameStamp(stamps.snap, state.snapStamp);
|
|
315
|
+
if (mode === 'auto' && settled && stamps.journalSize === state.journalSize) {
|
|
316
|
+
return done('unchanged', 0, false);
|
|
317
|
+
}
|
|
318
|
+
if (mode === 'rebuild')
|
|
319
|
+
return rebuildScope(env, state, paths);
|
|
320
|
+
if (needsReload(state, stamps, mode))
|
|
321
|
+
return reloadScope(env, state, paths, stamps);
|
|
322
|
+
return tailScope(env, state, paths, stamps);
|
|
323
|
+
}
|
|
324
|
+
export { indexPaths };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { GeneSearchIndex, type GeneSearchResultEx } from '@aragon-agent/tui/model/gene/search';
|
|
2
|
+
import type { GeneCaller, GeneIndexRow, GeneScope, GeneScoreBreakdown, GeneSearchOptions, GeneStats, GeneType } from '@aragon-agent/tui/model/gene/types';
|
|
3
|
+
import { type CatalogIo } from './catalog-sync.js';
|
|
4
|
+
import { type GeneStoreReader, type ScopeCounts } from './store-port.js';
|
|
5
|
+
export type { CatalogIo };
|
|
6
|
+
export type CatalogState = 'loading' | 'rebuilding' | 'ready';
|
|
7
|
+
export type CatalogDegraded = 'index_rebuilding' | 'index_unavailable';
|
|
8
|
+
export interface GeneCatalogOptions {
|
|
9
|
+
reader: GeneStoreReader;
|
|
10
|
+
/** File operations; the default is the real filesystem. Tests wrap it to count calls. */
|
|
11
|
+
io?: CatalogIo;
|
|
12
|
+
now?: () => number;
|
|
13
|
+
/** Monotonic milliseconds for the search budget (default `performance.now`). */
|
|
14
|
+
clock?: () => number;
|
|
15
|
+
/** Expected `vecAlgo`; a snapshot written with another one forces a rebuild. */
|
|
16
|
+
vecAlgo?: number;
|
|
17
|
+
statsTtlMs?: number;
|
|
18
|
+
/** Heads per slice between two yields to the event loop. */
|
|
19
|
+
yieldEvery?: number;
|
|
20
|
+
yieldTo?: () => Promise<void>;
|
|
21
|
+
/** Failure hook. Receives an operation name and the error; must not throw. */
|
|
22
|
+
onError?: (op: string, error: unknown) => void;
|
|
23
|
+
}
|
|
24
|
+
export interface CatchUpReport {
|
|
25
|
+
/** The visible gene set changed (never true for a scope's very first load). */
|
|
26
|
+
changed: boolean;
|
|
27
|
+
tailed: string[];
|
|
28
|
+
reloaded: string[];
|
|
29
|
+
rebuilt: string[];
|
|
30
|
+
failed: string[];
|
|
31
|
+
applied: number;
|
|
32
|
+
}
|
|
33
|
+
export interface ReconcileReport {
|
|
34
|
+
scopes: number;
|
|
35
|
+
added: number;
|
|
36
|
+
removed: number;
|
|
37
|
+
repaired: number;
|
|
38
|
+
statChecked: number;
|
|
39
|
+
truncated: boolean;
|
|
40
|
+
changed: boolean;
|
|
41
|
+
}
|
|
42
|
+
/** What the lease holder must do to compact one scope's index (7.7). */
|
|
43
|
+
export interface IndexCompactRequest {
|
|
44
|
+
scope: GeneScope;
|
|
45
|
+
/** Suggested generation; the writer may choose a higher one and returns what it wrote. */
|
|
46
|
+
gen: number;
|
|
47
|
+
vecAlgo: number;
|
|
48
|
+
rows: GeneIndexRow[];
|
|
49
|
+
/**
|
|
50
|
+
* The journal byte offset these rows already include. The writer, UNDER THE SCOPE LOCK, must
|
|
51
|
+
* refuse (return null) when the journal has grown past it - it would lose the extra entries.
|
|
52
|
+
*/
|
|
53
|
+
includedOffset: number;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Injected by the lease holder: write `snapshot.tmp` -> rename over `snapshot.json` with
|
|
57
|
+
* `journalOffset: 0`, then truncate the journal, all under the scope lock. Returns the generation
|
|
58
|
+
* written, or null when it declined.
|
|
59
|
+
*/
|
|
60
|
+
export interface IndexWriter {
|
|
61
|
+
compactScope(request: IndexCompactRequest): Promise<number | null>;
|
|
62
|
+
}
|
|
63
|
+
export interface CompactReport {
|
|
64
|
+
compacted: string[];
|
|
65
|
+
skipped: string[];
|
|
66
|
+
}
|
|
67
|
+
export declare class GeneCatalog {
|
|
68
|
+
/** Exposed for `planGeneContext`, which needs the index itself, not a search call. */
|
|
69
|
+
readonly index: GeneSearchIndex;
|
|
70
|
+
private readonly reader;
|
|
71
|
+
private readonly io;
|
|
72
|
+
private readonly vecAlgo;
|
|
73
|
+
private readonly onError;
|
|
74
|
+
private readonly stats;
|
|
75
|
+
private readonly env;
|
|
76
|
+
private readonly states;
|
|
77
|
+
private readonly rebuilding;
|
|
78
|
+
private readonly listeners;
|
|
79
|
+
private scopeList;
|
|
80
|
+
private pending;
|
|
81
|
+
private queue;
|
|
82
|
+
private clockScale;
|
|
83
|
+
private mutations;
|
|
84
|
+
private disposed;
|
|
85
|
+
constructor(options: GeneCatalogOptions);
|
|
86
|
+
/**
|
|
87
|
+
* Replace the list of scopes this catalog serves. Rows of a scope that left the list are
|
|
88
|
+
* dropped at once (so a `/cwd` change cannot leave the old project's genes searchable); a new
|
|
89
|
+
* scope is loaded by the next `catchUp()` and meanwhile reads as `index_rebuilding`.
|
|
90
|
+
*/
|
|
91
|
+
setScopes(scopes: readonly GeneScope[]): void;
|
|
92
|
+
scopes(): readonly GeneScope[];
|
|
93
|
+
/** Incremented by every change to the index or the scope list; cheap cache key for views. */
|
|
94
|
+
version(): number;
|
|
95
|
+
/** Every wanted scope finished its first sync (a scope that failed has SETTLED, as empty). */
|
|
96
|
+
isLoaded(): boolean;
|
|
97
|
+
state(): CatalogState;
|
|
98
|
+
private failureFlag;
|
|
99
|
+
/**
|
|
100
|
+
* Bring every scope up to date. O(1) (two `stat`s per scope) when nothing changed. Concurrent
|
|
101
|
+
* callers share one pass; a scope added while a pass was running gets its own pass afterwards.
|
|
102
|
+
* Never rejects.
|
|
103
|
+
*/
|
|
104
|
+
catchUp(): Promise<CatchUpReport>;
|
|
105
|
+
/** Forget the derived files and rescan the heads of every scope (or one). Never rejects. */
|
|
106
|
+
rebuild(scope?: GeneScope): Promise<CatchUpReport>;
|
|
107
|
+
private exclusive;
|
|
108
|
+
private pass;
|
|
109
|
+
private tally;
|
|
110
|
+
private syncOne;
|
|
111
|
+
/** The first pass awaits the statistics (so the first ranking has them); later ones refresh. */
|
|
112
|
+
private refreshStats;
|
|
113
|
+
/** Resolves when the statistics of the loaded scopes are cached. Never rejects. */
|
|
114
|
+
warmStats(): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* Synchronous, never throws. `budgetMs` shrinks the CPU budget for latency-critical callers
|
|
117
|
+
* (the hint engine passes 8). While the catalog cannot answer honestly the result is empty and
|
|
118
|
+
* flagged, which every consumer renders as a polite one-liner.
|
|
119
|
+
*/
|
|
120
|
+
search(opts: GeneSearchOptions, caller: GeneCaller, budgetMs?: number): GeneSearchResultEx;
|
|
121
|
+
/** Score breakdown for the `explain` views: numbers and field names only. */
|
|
122
|
+
explain(row: GeneIndexRow, opts: GeneSearchOptions, caller: GeneCaller): GeneScoreBreakdown;
|
|
123
|
+
/** The `StatsFn` the planner and the searcher take; reads the cache, never the disk. */
|
|
124
|
+
readonly statsOf: (id: string) => GeneStats | undefined;
|
|
125
|
+
get(id: string): GeneIndexRow | undefined;
|
|
126
|
+
rows(): IterableIterator<GeneIndexRow>;
|
|
127
|
+
size(): number;
|
|
128
|
+
/** Recomputed from the in-memory rows of one scope (`index/counts.json` is the writer's). */
|
|
129
|
+
countsFor(scope: GeneScope): ScopeCounts;
|
|
130
|
+
/** `index/counts.json` when present and well-formed, else the recomputed view. */
|
|
131
|
+
readCounts(scope: GeneScope): Promise<ScopeCounts>;
|
|
132
|
+
/** Published rows per type, candidates and pinned, over every loaded scope. */
|
|
133
|
+
libraryCounts(): Record<GeneType, number> & {
|
|
134
|
+
candidates: number;
|
|
135
|
+
pinned: number;
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Compare head files with the index and repair differences from the heads. `deep` adds the
|
|
139
|
+
* stat pass (RV-8) and belongs to the lease holder; a session start does the names only.
|
|
140
|
+
*/
|
|
141
|
+
reconcile(opts?: {
|
|
142
|
+
deep?: boolean;
|
|
143
|
+
}): Promise<ReconcileReport>;
|
|
144
|
+
private reconcileAll;
|
|
145
|
+
/**
|
|
146
|
+
* Fold the journal into a fresh snapshot through `writer`. Only scopes over the size / line
|
|
147
|
+
* thresholds are touched unless `force`. The catalog first catches up, so the rows it hands over
|
|
148
|
+
* include every journal line up to `includedOffset`.
|
|
149
|
+
*/
|
|
150
|
+
compact(writer: IndexWriter, opts?: {
|
|
151
|
+
force?: boolean;
|
|
152
|
+
}): Promise<CompactReport>;
|
|
153
|
+
private compactAll;
|
|
154
|
+
private compactOne;
|
|
155
|
+
/** Test aid: the snapshot generation currently loaded for a scope (-1 = rebuilt / none). */
|
|
156
|
+
generationOf(scope: GeneScope): number;
|
|
157
|
+
onChange(listener: () => void): () => void;
|
|
158
|
+
private fire;
|
|
159
|
+
dispose(): void;
|
|
160
|
+
}
|