@aragon-agent/cli 0.6.54 → 0.6.56
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 +2912 -2838
- package/README.md +4226 -4042
- package/dist/agent/controller.d.ts +119 -0
- package/dist/agent/controller.js +365 -2
- package/dist/agent/governance-wiring.js +8 -1
- package/dist/agent/redteam-prompt.d.ts +47 -0
- package/dist/agent/redteam-prompt.js +161 -0
- package/dist/agent/redteam-wiring.d.ts +159 -0
- package/dist/agent/redteam-wiring.js +349 -0
- package/dist/agent/system-prompt.d.ts +39 -0
- package/dist/agent/system-prompt.js +18 -0
- package/dist/app/App.d.ts +10 -1
- package/dist/app/App.js +233 -24
- package/dist/app/use-redteam-mode.d.ts +56 -0
- package/dist/app/use-redteam-mode.js +121 -0
- package/dist/cli.js +82 -4
- package/dist/commands/builtins.js +43 -1
- package/dist/commands/catalog.js +5 -0
- package/dist/commands/index-build.js +2 -1
- package/dist/commands/redteam-command.d.ts +22 -0
- package/dist/commands/redteam-command.js +141 -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 +3 -1
- package/dist/config/env.js +23 -1
- package/dist/config/load.d.ts +22 -0
- package/dist/config/load.js +22 -2
- package/dist/config/redteam-launch.d.ts +77 -0
- package/dist/config/redteam-launch.js +58 -0
- package/dist/config/store.js +21 -1
- package/dist/config/ui-state.d.ts +44 -0
- package/dist/config/ui-state.js +38 -0
- 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 +103 -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 +197 -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 +202 -0
- package/dist/gene/curator/tools-write.d.ts +26 -0
- package/dist/gene/curator/tools-write.js +394 -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 +36 -0
- package/dist/gene/curator/validate/evidence-tokens.js +139 -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 +25 -0
- package/dist/gene/curator/validate/knowledge.js +216 -0
- package/dist/gene/curator/validate/live-check.d.ts +14 -0
- package/dist/gene/curator/validate/live-check.js +144 -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 +161 -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 +63 -0
- package/dist/gene/learn/learn-call.js +157 -0
- package/dist/gene/learn/learn-prompt.d.ts +51 -0
- package/dist/gene/learn/learn-prompt.js +245 -0
- package/dist/gene/learn/learner.d.ts +181 -0
- package/dist/gene/learn/learner.js +426 -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 +211 -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 +81 -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/controller-factory.d.ts +8 -0
- package/dist/host/controller-factory.js +7 -0
- 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/session/checkpoint.d.ts +15 -0
- package/dist/session/checkpoint.js +18 -0
- package/dist/session/persist.d.ts +21 -0
- package/dist/session/persist.js +10 -0
- package/dist/session/startup-session.d.ts +11 -2
- package/dist/session/startup-session.js +12 -2
- package/dist/session/validate-session.js +31 -0
- package/dist/team/runtime.d.ts +16 -0
- package/dist/team/runtime.js +29 -0
- package/dist/team/subagent.d.ts +55 -0
- package/dist/team/subagent.js +46 -2
- package/dist/tools/index.d.ts +60 -2
- package/dist/tools/index.js +68 -10
- package/package.json +3 -3
- package/skills/project-indexer/SKILL.md +275 -147
- package/skills/project-indexer/templates/artifact-hygiene-template.md +14 -0
- package/skills/project-indexer/templates/config-template.md +12 -0
- package/skills/project-indexer/templates/index-template.md +4 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `GeneUsageRecorder` - the only thing the read path ever writes (gene-bank spec 7.5, I-4, I-19).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* WHAT IT DOES. Collects `GeneUsageEvent`s in memory and appends them, as JSON lines, to
|
|
7
|
+
* `usage/<sessionId>.jsonl` every `usageFlushMs` or `usageFlushEvents`, whichever comes first.
|
|
8
|
+
* Folding those lines into the statistics shards is the lease holder's job (a later work package);
|
|
9
|
+
* this file only produces the log.
|
|
10
|
+
*
|
|
11
|
+
* WHY IT IS ALLOWED TO WRITE. Every event is a counter increment ("this gene was shown / read /
|
|
12
|
+
* adopted"), never gene content. The library directory a read path may touch is exactly `usage/`
|
|
13
|
+
* (I-4); `read-path-no-write.test.ts` holds that line.
|
|
14
|
+
*
|
|
15
|
+
* DEDUPE (7.5). `shown` counts once per `(runId, geneId)` however many times the context zone is
|
|
16
|
+
* rebuilt in a run. The other kinds count every call: a gene returned by two searches was
|
|
17
|
+
* retrieved twice.
|
|
18
|
+
*
|
|
19
|
+
* NEVER BLOCKS, NEVER THROWS INTO THE CALLER. `record()` is a synchronous push. A failed append
|
|
20
|
+
* drops the batch and counts it; a user turn does not wait for, or learn about, a full disk.
|
|
21
|
+
*
|
|
22
|
+
* NO TEXT (I-19). Events are rebuilt field by field from a whitelist, so nothing a caller attaches
|
|
23
|
+
* by mistake (a note, a name, a body) can reach the log.
|
|
24
|
+
*/
|
|
25
|
+
import { promises as nodeFs } from 'node:fs';
|
|
26
|
+
import { GENE_LIMITS } from '@aragon-agent/tui/model/gene/limits';
|
|
27
|
+
import { ensureDir } from './fs.js';
|
|
28
|
+
import { usageDir, usageFile } from './paths.js';
|
|
29
|
+
/** `setTimeout`, unref'd: a pending flush must never hold the process open (dispose flushes). */
|
|
30
|
+
export const SYSTEM_TIMER = {
|
|
31
|
+
set(callback, ms) {
|
|
32
|
+
const handle = setTimeout(callback, ms);
|
|
33
|
+
handle.unref?.();
|
|
34
|
+
return handle;
|
|
35
|
+
},
|
|
36
|
+
clear(handle) {
|
|
37
|
+
clearTimeout(handle);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
const DEDUPE_CAP = 20_000;
|
|
41
|
+
/** Batches waiting on a slow disk; past this, new batches are dropped rather than queued. */
|
|
42
|
+
const MAX_PENDING_WRITES = 8;
|
|
43
|
+
const EXPOSURE_KINDS = new Set(['shown', 'retrieved', 'read', 'readL3']);
|
|
44
|
+
const OUTCOMES = new Set(['helped', 'harmed', 'neutral']);
|
|
45
|
+
const ADOPTERS = new Set(['agent', 'reviewer', 'tool']);
|
|
46
|
+
const SOURCES = new Set(['context', 'hint', 'search', 'read']);
|
|
47
|
+
function isText(value) {
|
|
48
|
+
return typeof value === 'string' && value.length > 0 && value.length <= 200;
|
|
49
|
+
}
|
|
50
|
+
function isNumber(value) {
|
|
51
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
52
|
+
}
|
|
53
|
+
function oneOf(allowed, value) {
|
|
54
|
+
return typeof value === 'string' && allowed.has(value) ? value : null;
|
|
55
|
+
}
|
|
56
|
+
function withAgent(base, agent) {
|
|
57
|
+
return isText(agent) ? { ...base, agent } : base;
|
|
58
|
+
}
|
|
59
|
+
function headOf(e) {
|
|
60
|
+
const { sc, g, run, t, v } = e;
|
|
61
|
+
if (!isText(sc) || !isText(g) || !isText(run) || !isNumber(t) || !isNumber(v))
|
|
62
|
+
return null;
|
|
63
|
+
return { sc, t, g, v, run };
|
|
64
|
+
}
|
|
65
|
+
function exposureEvent(head, e) {
|
|
66
|
+
const src = oneOf(SOURCES, e.src);
|
|
67
|
+
if (src === null)
|
|
68
|
+
return null;
|
|
69
|
+
return withAgent({ ...head, k: e.k, src }, e.agent);
|
|
70
|
+
}
|
|
71
|
+
function adoptedEvent(head, e) {
|
|
72
|
+
const by = oneOf(ADOPTERS, e.by);
|
|
73
|
+
if (by === null)
|
|
74
|
+
return null;
|
|
75
|
+
return withAgent({ ...head, k: 'adopted', by }, e.agent);
|
|
76
|
+
}
|
|
77
|
+
function outcomeEvent(head, e) {
|
|
78
|
+
const effect = oneOf(OUTCOMES, e.e);
|
|
79
|
+
const by = oneOf(ADOPTERS, e.by);
|
|
80
|
+
if (effect === null || by === null)
|
|
81
|
+
return null;
|
|
82
|
+
const w = isNumber(e.w) ? e.w : 0;
|
|
83
|
+
return { ...head, k: 'outcome', e: effect, w, by };
|
|
84
|
+
}
|
|
85
|
+
const BUILDERS = new Map([
|
|
86
|
+
...[...EXPOSURE_KINDS].map((kind) => [kind, exposureEvent]),
|
|
87
|
+
['adopted', adoptedEvent],
|
|
88
|
+
['outcome', outcomeEvent],
|
|
89
|
+
]);
|
|
90
|
+
/**
|
|
91
|
+
* Rebuild an event from known fields only. Returns null for anything malformed - a bad event is
|
|
92
|
+
* dropped and counted, never written, because the folder cannot tell a corrupt line from a real
|
|
93
|
+
* one.
|
|
94
|
+
*/
|
|
95
|
+
export function sanitizeUsageEvent(raw) {
|
|
96
|
+
if (raw === null || typeof raw !== 'object')
|
|
97
|
+
return null;
|
|
98
|
+
const e = raw;
|
|
99
|
+
const head = headOf(e);
|
|
100
|
+
const build = typeof e.k === 'string' ? BUILDERS.get(e.k) : undefined;
|
|
101
|
+
return head === null || build === undefined ? null : build(head, e);
|
|
102
|
+
}
|
|
103
|
+
async function defaultAppend(path, text) {
|
|
104
|
+
await nodeFs.appendFile(path, text, 'utf8');
|
|
105
|
+
}
|
|
106
|
+
export class GeneUsageRecorder {
|
|
107
|
+
buffer = [];
|
|
108
|
+
seenShown = new Set();
|
|
109
|
+
handle = null;
|
|
110
|
+
chain = Promise.resolve();
|
|
111
|
+
pendingWrites = 0;
|
|
112
|
+
disposed = false;
|
|
113
|
+
counters = {
|
|
114
|
+
recorded: 0,
|
|
115
|
+
written: 0,
|
|
116
|
+
deduped: 0,
|
|
117
|
+
dropped: 0,
|
|
118
|
+
flushes: 0,
|
|
119
|
+
};
|
|
120
|
+
path;
|
|
121
|
+
dir;
|
|
122
|
+
flushMs;
|
|
123
|
+
flushEvents;
|
|
124
|
+
timer;
|
|
125
|
+
append;
|
|
126
|
+
onError;
|
|
127
|
+
constructor(options) {
|
|
128
|
+
this.path = usageFile(options.sessionId, options.ctx);
|
|
129
|
+
this.dir = usageDir(options.ctx);
|
|
130
|
+
this.flushMs = options.flushMs ?? GENE_LIMITS.usageFlushMs;
|
|
131
|
+
this.flushEvents = options.flushEvents ?? GENE_LIMITS.usageFlushEvents;
|
|
132
|
+
this.timer = options.timer ?? SYSTEM_TIMER;
|
|
133
|
+
this.append = options.append ?? defaultAppend;
|
|
134
|
+
this.onError = options.onError ?? (() => undefined);
|
|
135
|
+
}
|
|
136
|
+
stats() {
|
|
137
|
+
return { ...this.counters };
|
|
138
|
+
}
|
|
139
|
+
/** Synchronous; swallows everything. */
|
|
140
|
+
record(event) {
|
|
141
|
+
try {
|
|
142
|
+
this.accept(event);
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
this.counters.dropped += 1;
|
|
146
|
+
this.onError(error);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
accept(event) {
|
|
150
|
+
if (this.disposed)
|
|
151
|
+
return;
|
|
152
|
+
const clean = sanitizeUsageEvent(event);
|
|
153
|
+
if (clean === null || this.path === null) {
|
|
154
|
+
this.counters.dropped += 1;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (clean.k === 'shown' && this.alreadyShown(clean)) {
|
|
158
|
+
this.counters.deduped += 1;
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
this.counters.recorded += 1;
|
|
162
|
+
this.buffer.push(JSON.stringify(clean));
|
|
163
|
+
if (this.buffer.length >= this.flushEvents)
|
|
164
|
+
void this.flush();
|
|
165
|
+
else
|
|
166
|
+
this.arm();
|
|
167
|
+
}
|
|
168
|
+
alreadyShown(event) {
|
|
169
|
+
const key = `${event.run}\u0000${event.g}`;
|
|
170
|
+
if (this.seenShown.has(key))
|
|
171
|
+
return true;
|
|
172
|
+
// A bounded set: past the cap an old pair may count twice, which only over-counts exposure
|
|
173
|
+
// (never effectiveness), and an unbounded set would be a leak in a month-long session.
|
|
174
|
+
if (this.seenShown.size >= DEDUPE_CAP)
|
|
175
|
+
this.seenShown.clear();
|
|
176
|
+
this.seenShown.add(key);
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
arm() {
|
|
180
|
+
if (this.handle !== null)
|
|
181
|
+
return;
|
|
182
|
+
this.handle = this.timer.set(() => {
|
|
183
|
+
this.handle = null;
|
|
184
|
+
void this.flush();
|
|
185
|
+
}, this.flushMs);
|
|
186
|
+
}
|
|
187
|
+
disarm() {
|
|
188
|
+
if (this.handle === null)
|
|
189
|
+
return;
|
|
190
|
+
this.timer.clear(this.handle);
|
|
191
|
+
this.handle = null;
|
|
192
|
+
}
|
|
193
|
+
/** Append what is buffered. Resolves when the write settled; never rejects. */
|
|
194
|
+
flush() {
|
|
195
|
+
this.disarm();
|
|
196
|
+
const lines = this.buffer;
|
|
197
|
+
this.buffer = [];
|
|
198
|
+
if (lines.length === 0 || this.path === null)
|
|
199
|
+
return this.chain;
|
|
200
|
+
if (this.pendingWrites >= MAX_PENDING_WRITES) {
|
|
201
|
+
this.counters.dropped += lines.length;
|
|
202
|
+
return this.chain;
|
|
203
|
+
}
|
|
204
|
+
this.pendingWrites += 1;
|
|
205
|
+
this.chain = this.chain.then(() => this.write(this.path, lines));
|
|
206
|
+
return this.chain;
|
|
207
|
+
}
|
|
208
|
+
async write(path, lines) {
|
|
209
|
+
try {
|
|
210
|
+
await ensureDir(this.dir);
|
|
211
|
+
await this.append(path, `${lines.join('\n')}\n`);
|
|
212
|
+
this.counters.written += lines.length;
|
|
213
|
+
this.counters.flushes += 1;
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
this.counters.dropped += lines.length;
|
|
217
|
+
this.onError(error);
|
|
218
|
+
}
|
|
219
|
+
finally {
|
|
220
|
+
this.pendingWrites -= 1;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/** Flush the tail and stop accepting events. Never rejects. */
|
|
224
|
+
async dispose() {
|
|
225
|
+
if (this.disposed)
|
|
226
|
+
return;
|
|
227
|
+
await this.flush();
|
|
228
|
+
this.disposed = true;
|
|
229
|
+
this.disarm();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `GeneVersionService`: THE ONLY WRITER of the Gene Bank (gene-bank spec 7.1, 7.6, 11.3;
|
|
3
|
+
* I-4, I-10, I-12, I-16, I-17, I-22, I-24).
|
|
4
|
+
*
|
|
5
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
6
|
+
*
|
|
7
|
+
* EVERY WRITE TO A GENE, FROM ANYONE, IS `commit(change, actor, expect?)`. The manager's editor,
|
|
8
|
+
* the background curator, the importer and the migration all go through it, which is what lets
|
|
9
|
+
* one set of tests prove "nothing writes a gene any other way". The 10 steps of the protocol:
|
|
10
|
+
*
|
|
11
|
+
* 1 validate ids, scope and content (no disk touched) -> service-prepare.ts
|
|
12
|
+
* - make room if the commit publishes into a full library -> capacity.ts (its OWN commit)
|
|
13
|
+
* 2 take the scope lock -> scope-lock.ts
|
|
14
|
+
* 3 read the head, check the basis (CAS), the fence, budgets -> plan-*.ts
|
|
15
|
+
* 4 tombstone check (create / import) -> plan-content.ts
|
|
16
|
+
* 5 capacity re-check for this scope -> commit-plan.ts
|
|
17
|
+
* 6 write new version files 7 write the head (COMMIT POINT) -> commit-exec.ts
|
|
18
|
+
* 8 journal line + counts for THIS scope
|
|
19
|
+
* 9 release the lock
|
|
20
|
+
* 10 OUTSIDE the lock: dependency propagation, stale-proposal marking, the run record
|
|
21
|
+
*
|
|
22
|
+
* NEVER THROWS. The public methods return a `GeneResult`; an unexpected exception becomes
|
|
23
|
+
* `GENE_UNAVAILABLE` with a short errno token (never a path, never gene text, I-19). Reads fail
|
|
24
|
+
* open and writes fail closed (I-24): a failed commit leaves the library as it was or as a
|
|
25
|
+
* recoverable crash-table state.
|
|
26
|
+
*
|
|
27
|
+
* AT MOST ONE SCOPE LOCK AT A TIME (I-22). A commit holds one lock, only for steps 3-8, never
|
|
28
|
+
* across a model call. A commit that needs another scope - eviction, propagation, the second
|
|
29
|
+
* half of a merge - does it as a SEPARATE commit after the first has released.
|
|
30
|
+
*
|
|
31
|
+
* `confirmPreference` and `rollback` are rewritten here into an internal edit that publishes
|
|
32
|
+
* directly; `merge` and `promote` are sequences of commits (`commit-compound.ts`). None of them
|
|
33
|
+
* has a flag a caller can use to skip the candidate stage.
|
|
34
|
+
*/
|
|
35
|
+
import type { GeneIndexRow, GeneResult, GeneScope, GeneType } from '@aragon-agent/tui/model/gene/types';
|
|
36
|
+
import { type LibraryTotals } from './capacity.js';
|
|
37
|
+
import type { CommitOutcome, DependentsResolver, GeneActor, GeneChange, GeneExpect, GeneRunLog, GeneServiceLimits, InvalidateHook, ProposalFence } from './commit-types.js';
|
|
38
|
+
import { type RepairSummary, type ScopeReport } from './scope-verify.js';
|
|
39
|
+
import { ScopeLockManager, type ScopeLockOptions } from './scope-lock.js';
|
|
40
|
+
import type { ExtendedCounts, GeneStore } from './store.js';
|
|
41
|
+
import { TombstoneStore } from './tombstones.js';
|
|
42
|
+
export interface GeneVersionServiceOptions {
|
|
43
|
+
store: GeneStore;
|
|
44
|
+
locks?: ScopeLockManager;
|
|
45
|
+
lockOptions?: ScopeLockOptions;
|
|
46
|
+
/** Read at COMMIT time, so a settings change applies to the next write. */
|
|
47
|
+
limits?: () => Partial<GeneServiceLimits>;
|
|
48
|
+
now?: () => number;
|
|
49
|
+
random?: (n: number) => number;
|
|
50
|
+
/** The user's configured provider keys: the strongest secret signal there is (RV-18). */
|
|
51
|
+
knownSecrets?: () => readonly string[];
|
|
52
|
+
tombstones?: TombstoneStore;
|
|
53
|
+
dependents?: DependentsResolver;
|
|
54
|
+
proposals?: ProposalFence;
|
|
55
|
+
runLog?: GeneRunLog;
|
|
56
|
+
onInvalidate?: InvalidateHook;
|
|
57
|
+
}
|
|
58
|
+
export declare class GeneVersionService {
|
|
59
|
+
readonly store: GeneStore;
|
|
60
|
+
private readonly locks;
|
|
61
|
+
private readonly rows;
|
|
62
|
+
private readonly counter;
|
|
63
|
+
private readonly tombstones;
|
|
64
|
+
private readonly dependents;
|
|
65
|
+
private readonly proposals;
|
|
66
|
+
private readonly runLog;
|
|
67
|
+
private readonly onInvalidate;
|
|
68
|
+
private readonly now;
|
|
69
|
+
private readonly random;
|
|
70
|
+
private readonly knownSecrets;
|
|
71
|
+
private readonly limitsFn;
|
|
72
|
+
private schemaReady;
|
|
73
|
+
constructor(options: GeneVersionServiceOptions);
|
|
74
|
+
limits(): GeneServiceLimits;
|
|
75
|
+
private capacityEnv;
|
|
76
|
+
private prepareEnv;
|
|
77
|
+
private planEnv;
|
|
78
|
+
private execEnv;
|
|
79
|
+
private compound;
|
|
80
|
+
/** Run an action and turn anything it throws into a failure result. */
|
|
81
|
+
private guarded;
|
|
82
|
+
commit(change: GeneChange, actor: GeneActor, expect?: GeneExpect): Promise<GeneResult<CommitOutcome>>;
|
|
83
|
+
private ensureLibrary;
|
|
84
|
+
private expand;
|
|
85
|
+
/** Metadata lives on the head and may be newer than the version file's copy; keep it. */
|
|
86
|
+
private overlayMetadata;
|
|
87
|
+
private expandConfirm;
|
|
88
|
+
private expandRollback;
|
|
89
|
+
private checkTarget;
|
|
90
|
+
private apply;
|
|
91
|
+
/** Steps 3-8, inside the lock. */
|
|
92
|
+
private locked;
|
|
93
|
+
private plan;
|
|
94
|
+
private planSimple;
|
|
95
|
+
private outcomeOf;
|
|
96
|
+
/** Archive one gene so a publish fits; `GENE_CAP_PINNED_FULL` when nothing may go (7.2). */
|
|
97
|
+
private makeRoom;
|
|
98
|
+
/** The gene that could not be published is flagged for a person (`cap_pinned_full`). */
|
|
99
|
+
private flagLibraryFull;
|
|
100
|
+
private archiveVictim;
|
|
101
|
+
/** Maintenance: archive the least valuable evictable gene of `type`, even below the cap. */
|
|
102
|
+
evictOne(type: GeneType, preferScope: GeneScope): Promise<GeneResult<string[]>>;
|
|
103
|
+
/** Library totals from the per-scope counts (cached a few seconds). */
|
|
104
|
+
totals(): Promise<LibraryTotals>;
|
|
105
|
+
/** Who would be victim? Exposed for the UI's "what would be archived" hint and for tests. */
|
|
106
|
+
peekVictim(type: GeneType, scope: GeneScope): Promise<GeneIndexRow | null>;
|
|
107
|
+
/** Run record, proposal fencing, propagation. NEVER throws; returns proposals superseded. */
|
|
108
|
+
private afterCommit;
|
|
109
|
+
private propagationEnv;
|
|
110
|
+
private record;
|
|
111
|
+
private fenceProposals;
|
|
112
|
+
/** Read-only comparison of a scope with its head files (the truth). */
|
|
113
|
+
verifyScope(scope: GeneScope): Promise<GeneResult<ScopeReport>>;
|
|
114
|
+
/** Recount a scope from its heads and rewrite `counts.json` under the scope lock. */
|
|
115
|
+
rebuildScopeCounts(scope: GeneScope): Promise<GeneResult<ExtendedCounts>>;
|
|
116
|
+
/** Bring the derived parts of a scope back in line with its heads (index, counts, moves). */
|
|
117
|
+
reconcileScope(scope: GeneScope): Promise<GeneResult<RepairSummary>>;
|
|
118
|
+
}
|