@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,36 @@
|
|
|
1
|
+
import type { GeneScope } from '@aragon-agent/tui/model/gene/types';
|
|
2
|
+
import type { CompactReport, GeneCatalog, IndexCompactRequest, IndexWriter } from './catalog.js';
|
|
3
|
+
import type { ScopeLockManager } from './scope-lock.js';
|
|
4
|
+
import type { GeneStore } from './store.js';
|
|
5
|
+
export interface IndexWriterHooks {
|
|
6
|
+
/** Test seam: runs between the snapshot write and the journal truncation. */
|
|
7
|
+
beforeTruncate?: () => void | Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
export interface IndexWriterDeps {
|
|
10
|
+
store: GeneStore;
|
|
11
|
+
locks: ScopeLockManager;
|
|
12
|
+
now?: () => number;
|
|
13
|
+
hooks?: IndexWriterHooks;
|
|
14
|
+
}
|
|
15
|
+
export interface CompactIfNeededOptions {
|
|
16
|
+
/** Compact this scope only; the catalog's other scopes are declined. */
|
|
17
|
+
scope?: GeneScope;
|
|
18
|
+
/** Compact whether or not the thresholds (2 MB / 2000 lines) are reached. */
|
|
19
|
+
force?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare class StoreIndexWriter implements IndexWriter {
|
|
22
|
+
private readonly deps;
|
|
23
|
+
private readonly now;
|
|
24
|
+
constructor(deps: IndexWriterDeps);
|
|
25
|
+
/** Returns the generation written, or null when the journal moved under the catalog. */
|
|
26
|
+
compactScope(request: IndexCompactRequest): Promise<number | null>;
|
|
27
|
+
private compactLocked;
|
|
28
|
+
/** The journal holds exactly what the catalog applied, plus at most a torn tail. */
|
|
29
|
+
private journalIsSettled;
|
|
30
|
+
/**
|
|
31
|
+
* Compact the catalog's scopes whose journal passed `journalCompactBytes` / `journalCompactLines`
|
|
32
|
+
* (the catalog tracks both). The catalog catches up first, so its rows include every journal
|
|
33
|
+
* line up to the offset it passes back.
|
|
34
|
+
*/
|
|
35
|
+
compactIfNeeded(catalog: Pick<GeneCatalog, 'compact'>, options?: CompactIfNeededOptions): Promise<CompactReport>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The real `IndexWriter`: fold a scope's journal into a fresh snapshot (gene-bank spec 7.7, D-39).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* WHO CALLS. Only the lease holder, through `GeneCatalog.compact(writer)`. The catalog hands over
|
|
7
|
+
* the rows it has applied and the journal byte offset those rows include (`includedOffset`); this
|
|
8
|
+
* writer turns that into `index/snapshot.json` (generation + 1, `journalOffset: 0`) and then
|
|
9
|
+
* truncates `index/journal.jsonl`. It writes derived files only - never a head, a version, a
|
|
10
|
+
* stats shard - so everything it produces can be deleted and rebuilt from the heads.
|
|
11
|
+
*
|
|
12
|
+
* WHY A LOCK. A commit appends a journal line inside its scope lock. If the snapshot were written
|
|
13
|
+
* while a line landed, that line would be truncated away and the row it describes would vanish
|
|
14
|
+
* from the index until the next reconcile. Under the lock the writer compares the journal's real
|
|
15
|
+
* size with `includedOffset`:
|
|
16
|
+
*
|
|
17
|
+
* equal nothing arrived since the catalog looked: compact
|
|
18
|
+
* larger lines arrived. If the extra bytes hold NO complete line (the tail a crash tore
|
|
19
|
+
* mid-append, which no live writer can be producing while we hold the lock) they are
|
|
20
|
+
* garbage and are discarded; otherwise decline (`null`) and let the catalog retry
|
|
21
|
+
* after it has caught up
|
|
22
|
+
* smaller somebody else already compacted and our rows are older than their snapshot: decline
|
|
23
|
+
*
|
|
24
|
+
* ORDER. Snapshot first, journal truncation second. A crash between them leaves a snapshot with
|
|
25
|
+
* `journalOffset: 0` next to a journal whose lines it already contains; a reader replays them in
|
|
26
|
+
* order and arrives at the same rows (`put` is last-write-wins, `del` is idempotent), and the next
|
|
27
|
+
* compaction removes the redundancy. The opposite order would lose the journal's content.
|
|
28
|
+
*/
|
|
29
|
+
import { promises as nodeFs } from 'node:fs';
|
|
30
|
+
import { sameScope } from '@aragon-agent/tui/model/gene/ids';
|
|
31
|
+
import { readJsonlFrom, retryOnWindowsContention, statOrNull } from './fs.js';
|
|
32
|
+
import { indexFile } from './paths.js';
|
|
33
|
+
/** One row per id, only this scope's, in a stable order. */
|
|
34
|
+
function usableRows(request) {
|
|
35
|
+
const byId = new Map();
|
|
36
|
+
for (const row of request.rows) {
|
|
37
|
+
if (sameScope(row.scope, request.scope))
|
|
38
|
+
byId.set(row.id, row);
|
|
39
|
+
}
|
|
40
|
+
return [...byId.values()].sort((a, b) => (a.id < b.id ? -1 : 1));
|
|
41
|
+
}
|
|
42
|
+
export class StoreIndexWriter {
|
|
43
|
+
deps;
|
|
44
|
+
now;
|
|
45
|
+
constructor(deps) {
|
|
46
|
+
this.deps = deps;
|
|
47
|
+
this.now = deps.now ?? Date.now;
|
|
48
|
+
}
|
|
49
|
+
/** Returns the generation written, or null when the journal moved under the catalog. */
|
|
50
|
+
async compactScope(request) {
|
|
51
|
+
return this.deps.locks.withLock(request.scope, () => this.compactLocked(request));
|
|
52
|
+
}
|
|
53
|
+
async compactLocked(request) {
|
|
54
|
+
const { store } = this.deps;
|
|
55
|
+
const journal = indexFile(request.scope, 'journal.jsonl', store.ctx);
|
|
56
|
+
if (journal === null)
|
|
57
|
+
return null;
|
|
58
|
+
const size = (await statOrNull(journal))?.size ?? 0;
|
|
59
|
+
if (!(await this.journalIsSettled(journal, size, request.includedOffset)))
|
|
60
|
+
return null;
|
|
61
|
+
const existing = await store.readSnapshot(request.scope);
|
|
62
|
+
const gen = Math.max(request.gen, (existing?.gen ?? 0) + 1);
|
|
63
|
+
const rows = usableRows(request);
|
|
64
|
+
await store.writeSnapshotAtomic(request.scope, {
|
|
65
|
+
schema: 1,
|
|
66
|
+
gen,
|
|
67
|
+
builtAt: this.now(),
|
|
68
|
+
vecAlgo: request.vecAlgo,
|
|
69
|
+
count: rows.length,
|
|
70
|
+
journalOffset: 0,
|
|
71
|
+
rows,
|
|
72
|
+
});
|
|
73
|
+
await this.deps.hooks?.beforeTruncate?.();
|
|
74
|
+
if (size > 0)
|
|
75
|
+
await retryOnWindowsContention(() => nodeFs.truncate(journal, 0));
|
|
76
|
+
return gen;
|
|
77
|
+
}
|
|
78
|
+
/** The journal holds exactly what the catalog applied, plus at most a torn tail. */
|
|
79
|
+
async journalIsSettled(path, size, included) {
|
|
80
|
+
if (size === included)
|
|
81
|
+
return true;
|
|
82
|
+
if (size < included)
|
|
83
|
+
return false;
|
|
84
|
+
const rest = await readJsonlFrom(path, included);
|
|
85
|
+
return rest.rows.length === 0 && rest.corrupt === 0 && rest.nextOffset === included;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Compact the catalog's scopes whose journal passed `journalCompactBytes` / `journalCompactLines`
|
|
89
|
+
* (the catalog tracks both). The catalog catches up first, so its rows include every journal
|
|
90
|
+
* line up to the offset it passes back.
|
|
91
|
+
*/
|
|
92
|
+
compactIfNeeded(catalog, options = {}) {
|
|
93
|
+
const only = options.scope;
|
|
94
|
+
const writer = only === undefined
|
|
95
|
+
? this
|
|
96
|
+
: {
|
|
97
|
+
compactScope: (request) => sameScope(request.scope, only) ? this.compactScope(request) : Promise.resolve(null),
|
|
98
|
+
};
|
|
99
|
+
return catalog.compact(writer, { force: options.force === true });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The learning model call (gene-bank spec 10.1.3, 10.6).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* ONE BOUNDED CALL, NO TOOLS. `maxTokens 4096`, `temperature 0`, 60 s for the whole thing
|
|
7
|
+
* INCLUDING the retry. The transport is an injected `complete` function: the controller wires it
|
|
8
|
+
* to `FastWiring.completeViaFastRegistry` (or to a main-model, fail-fast registry when
|
|
9
|
+
* `gene.reviewerModel` is `main`, RV-22). It must be a FAIL-FAST transport - a background
|
|
10
|
+
* learning call that inherits the foreground's long retry backoff would sit on a rate limit and
|
|
11
|
+
* compete with the user's turn.
|
|
12
|
+
*
|
|
13
|
+
* FOUR OUTCOMES, because the caller reacts to each differently:
|
|
14
|
+
* - `transient` the provider is busy (`rate_limit` / `overloaded`). Not the tier's fault, so it
|
|
15
|
+
* never counts as a strike (same rule as `classifyReviewFailure`, D-24).
|
|
16
|
+
* - `failure` anything else that went wrong. A strike; three in a row disable learning for
|
|
17
|
+
* the session so a misconfigured tier cannot burn tokens forever.
|
|
18
|
+
* - `timeout` the deadline passed. A strike.
|
|
19
|
+
* - `cancelled` the caller aborted (`/clear`, exit). Never a strike and never enqueued.
|
|
20
|
+
*
|
|
21
|
+
* THE TIMEOUT AND THE CANCEL ARE TOLD APART HERE, by a variable set BEFORE the abort. After
|
|
22
|
+
* the retry layer both arrive as the same bare abort error, so they cannot be told apart at the
|
|
23
|
+
* catch (the lesson of `fast/review-call.ts`, where the same mistake once made a Esc press count
|
|
24
|
+
* as a model failure). The deadline is also enforced by a race, not only by the signal, so a
|
|
25
|
+
* transport that ignores its signal still cannot hold the single-flight slot.
|
|
26
|
+
*
|
|
27
|
+
* WHY 4096 TOKENS AND 60 s (RC-1). The reply is one JSON object holding up to three proposals with
|
|
28
|
+
* a body of up to 6000 characters each, so a compliant answer does not fit in 1500 tokens: every
|
|
29
|
+
* paid call on the machine this fix was written for stopped at exactly 1500 and 7 of 14 were
|
|
30
|
+
* unparseable. The two numbers move TOGETHER: at the measured ~125 tokens/second, 4096 tokens
|
|
31
|
+
* needs ~33 s, so a 1500 -> 4096 change alone would turn every truncation into a timeout, and a
|
|
32
|
+
* timeout is a strike. `minRetryWindowMs` still leaves room for one retry inside the deadline.
|
|
33
|
+
*
|
|
34
|
+
* A REPLY THAT HIT THE CEILING IS NOT A FAILURE OF THE TIER. `stopReason` comes back from the
|
|
35
|
+
* provider through the transport; `isTruncated` below is the only place that reads it. Whether a
|
|
36
|
+
* truncated reply is unusable depends on whether it PARSES, which is the learner's call - a reply
|
|
37
|
+
* cut after its final `}` is still a good answer (RV-18).
|
|
38
|
+
*/
|
|
39
|
+
import type { StopReason } from '@aragon-agent/core';
|
|
40
|
+
export declare const LEARN_CALL: {
|
|
41
|
+
readonly maxTokens: 4096;
|
|
42
|
+
readonly timeoutMs: 60000;
|
|
43
|
+
/** One extra attempt, and only for a transient failure with time left. */
|
|
44
|
+
readonly retries: 1;
|
|
45
|
+
readonly retryDelayMs: 500;
|
|
46
|
+
/** Not worth a second try with less time than this left. */
|
|
47
|
+
readonly minRetryWindowMs: 4000;
|
|
48
|
+
};
|
|
49
|
+
export interface LearnRequest {
|
|
50
|
+
system: string;
|
|
51
|
+
user: string;
|
|
52
|
+
maxTokens: number;
|
|
53
|
+
temperature: 0;
|
|
54
|
+
signal: AbortSignal;
|
|
55
|
+
}
|
|
56
|
+
export interface LearnCompletion {
|
|
57
|
+
text: string;
|
|
58
|
+
usage?: {
|
|
59
|
+
inTok: number;
|
|
60
|
+
outTok: number;
|
|
61
|
+
};
|
|
62
|
+
/** The provider's stop reason (`max_tokens` means "cut off at the ceiling"), when it has one. */
|
|
63
|
+
stopReason?: StopReason;
|
|
64
|
+
}
|
|
65
|
+
/** The transport port. Throws on failure; the thrown value may carry `errorType`. */
|
|
66
|
+
export type LearnComplete = (request: LearnRequest) => Promise<LearnCompletion>;
|
|
67
|
+
export interface LearnCallTimers {
|
|
68
|
+
after(ms: number, fn: () => void): () => void;
|
|
69
|
+
}
|
|
70
|
+
export declare const realLearnTimers: LearnCallTimers;
|
|
71
|
+
export interface LearnCallOptions {
|
|
72
|
+
complete: LearnComplete;
|
|
73
|
+
system: string;
|
|
74
|
+
user: string;
|
|
75
|
+
/** External cancellation (`/clear`, exit). */
|
|
76
|
+
signal?: AbortSignal;
|
|
77
|
+
now: () => number;
|
|
78
|
+
timers?: LearnCallTimers;
|
|
79
|
+
/** Resolves after `ms` or when the signal aborts; injectable so tests do not wait. */
|
|
80
|
+
sleep?: (ms: number, signal: AbortSignal) => Promise<void>;
|
|
81
|
+
timeoutMs?: number;
|
|
82
|
+
retries?: number;
|
|
83
|
+
}
|
|
84
|
+
export interface LearnUsage {
|
|
85
|
+
inTok: number;
|
|
86
|
+
outTok: number;
|
|
87
|
+
}
|
|
88
|
+
export type LearnCallResult = LearnReply | LearnFailure;
|
|
89
|
+
/** A usable reply. `replyChars` is the only size the run log may record (I-19: never the text). */
|
|
90
|
+
export interface LearnReply {
|
|
91
|
+
ok: true;
|
|
92
|
+
text: string;
|
|
93
|
+
usage: LearnUsage;
|
|
94
|
+
attempts: number;
|
|
95
|
+
/** The provider's stop reason, when it reported one. */
|
|
96
|
+
stopReason?: StopReason;
|
|
97
|
+
replyChars: number;
|
|
98
|
+
}
|
|
99
|
+
export interface LearnFailure {
|
|
100
|
+
ok: false;
|
|
101
|
+
kind: 'transient' | 'failure' | 'timeout' | 'cancelled';
|
|
102
|
+
errorType: string;
|
|
103
|
+
message: string;
|
|
104
|
+
usage: LearnUsage;
|
|
105
|
+
attempts: number;
|
|
106
|
+
replyChars?: number;
|
|
107
|
+
}
|
|
108
|
+
/** True for the kinds that count toward self-disabling. */
|
|
109
|
+
export declare function countsAsStrike(result: LearnCallResult): boolean;
|
|
110
|
+
/**
|
|
111
|
+
* Did the provider cut the reply off at the output ceiling? The learner pairs this with "and it
|
|
112
|
+
* did not parse" before treating the call as a wasted (transient) one (RV-18).
|
|
113
|
+
*/
|
|
114
|
+
export declare function isTruncated(result: LearnCallResult): boolean;
|
|
115
|
+
/** Run the call with a deadline and at most one retry of a transient failure. Never throws. */
|
|
116
|
+
export declare function runLearnCall(options: LearnCallOptions): Promise<LearnCallResult>;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { classifyReviewFailure, errorText } from '../../fast/review-call.js';
|
|
2
|
+
export const LEARN_CALL = {
|
|
3
|
+
maxTokens: 4096,
|
|
4
|
+
timeoutMs: 60_000,
|
|
5
|
+
/** One extra attempt, and only for a transient failure with time left. */
|
|
6
|
+
retries: 1,
|
|
7
|
+
retryDelayMs: 500,
|
|
8
|
+
/** Not worth a second try with less time than this left. */
|
|
9
|
+
minRetryWindowMs: 4_000,
|
|
10
|
+
};
|
|
11
|
+
export const realLearnTimers = {
|
|
12
|
+
after(ms, fn) {
|
|
13
|
+
const handle = setTimeout(fn, ms);
|
|
14
|
+
handle.unref?.();
|
|
15
|
+
return () => clearTimeout(handle);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
/** True for the kinds that count toward self-disabling. */
|
|
19
|
+
export function countsAsStrike(result) {
|
|
20
|
+
return !result.ok && (result.kind === 'failure' || result.kind === 'timeout');
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Did the provider cut the reply off at the output ceiling? The learner pairs this with "and it
|
|
24
|
+
* did not parse" before treating the call as a wasted (transient) one (RV-18).
|
|
25
|
+
*/
|
|
26
|
+
export function isTruncated(result) {
|
|
27
|
+
return result.ok && result.stopReason === 'max_tokens';
|
|
28
|
+
}
|
|
29
|
+
function realSleep(ms, signal) {
|
|
30
|
+
return new Promise((resolve) => {
|
|
31
|
+
const timer = setTimeout(resolve, ms);
|
|
32
|
+
signal.addEventListener('abort', () => {
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
resolve();
|
|
35
|
+
}, { once: true });
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
class Aborted extends Error {
|
|
39
|
+
constructor() {
|
|
40
|
+
super('aborted');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Run the call with a deadline and at most one retry of a transient failure. Never throws. */
|
|
44
|
+
export async function runLearnCall(options) {
|
|
45
|
+
const controller = new AbortController();
|
|
46
|
+
let reason = null;
|
|
47
|
+
const abortFor = (why) => {
|
|
48
|
+
reason ??= why;
|
|
49
|
+
controller.abort();
|
|
50
|
+
};
|
|
51
|
+
const onExternal = () => abortFor('cancelled');
|
|
52
|
+
if (options.signal?.aborted)
|
|
53
|
+
abortFor('cancelled');
|
|
54
|
+
else
|
|
55
|
+
options.signal?.addEventListener('abort', onExternal, { once: true });
|
|
56
|
+
const timers = options.timers ?? realLearnTimers;
|
|
57
|
+
const timeoutMs = options.timeoutMs ?? LEARN_CALL.timeoutMs;
|
|
58
|
+
const startedAt = options.now();
|
|
59
|
+
const cancelTimer = timers.after(timeoutMs, () => abortFor('timeout'));
|
|
60
|
+
// An abort promise that nobody awaits must not become an unhandled rejection.
|
|
61
|
+
const aborted = new Promise((_, reject) => {
|
|
62
|
+
// An abort that already happened fires no event: reject at once or the race below never ends.
|
|
63
|
+
if (controller.signal.aborted)
|
|
64
|
+
reject(new Aborted());
|
|
65
|
+
else
|
|
66
|
+
controller.signal.addEventListener('abort', () => reject(new Aborted()), { once: true });
|
|
67
|
+
});
|
|
68
|
+
aborted.catch(() => undefined);
|
|
69
|
+
try {
|
|
70
|
+
return await attempts({ options, controller, aborted, startedAt, timeoutMs, reasonOf: () => reason });
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
cancelTimer();
|
|
74
|
+
options.signal?.removeEventListener('abort', onExternal);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async function attempts(ctx) {
|
|
78
|
+
const maxAttempts = 1 + Math.max(0, ctx.options.retries ?? LEARN_CALL.retries);
|
|
79
|
+
const sleep = ctx.options.sleep ?? realSleep;
|
|
80
|
+
const usage = { inTok: 0, outTok: 0 };
|
|
81
|
+
for (let attempt = 1;; attempt += 1) {
|
|
82
|
+
const stopped = ctx.reasonOf();
|
|
83
|
+
if (stopped !== null)
|
|
84
|
+
return abortedResult(stopped, usage, attempt - 1);
|
|
85
|
+
const step = await tryOnce({ ctx, usage, attempt, maxAttempts });
|
|
86
|
+
if ('done' in step)
|
|
87
|
+
return step.done;
|
|
88
|
+
await sleep(LEARN_CALL.retryDelayMs, ctx.controller.signal);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async function tryOnce(t) {
|
|
92
|
+
try {
|
|
93
|
+
const completion = await Promise.race([oneAttempt(t.ctx), t.ctx.aborted]);
|
|
94
|
+
return { done: settle(completion, t) };
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
return afterError(error, t);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function settle(completion, t) {
|
|
101
|
+
t.usage.inTok += completion.usage?.inTok ?? 0;
|
|
102
|
+
t.usage.outTok += completion.usage?.outTok ?? 0;
|
|
103
|
+
const text = typeof completion.text === 'string' ? completion.text.trim() : '';
|
|
104
|
+
const stop = completion.stopReason;
|
|
105
|
+
if (text.length === 0) {
|
|
106
|
+
// An EMPTY reply that stopped at the ceiling is a truncation, not a model that had nothing to
|
|
107
|
+
// say: charging it as a strike would disable learning for the session (RC-1 / RV-14).
|
|
108
|
+
if (stop === 'max_tokens') {
|
|
109
|
+
return failure({
|
|
110
|
+
kind: 'transient',
|
|
111
|
+
errorType: 'truncated',
|
|
112
|
+
message: 'truncated to nothing',
|
|
113
|
+
usage: t.usage,
|
|
114
|
+
attempts: t.attempt,
|
|
115
|
+
replyChars: 0,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return failure({
|
|
119
|
+
kind: 'failure',
|
|
120
|
+
errorType: 'empty_response',
|
|
121
|
+
message: 'empty reply',
|
|
122
|
+
usage: t.usage,
|
|
123
|
+
attempts: t.attempt,
|
|
124
|
+
replyChars: 0,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
ok: true,
|
|
129
|
+
text,
|
|
130
|
+
usage: t.usage,
|
|
131
|
+
attempts: t.attempt,
|
|
132
|
+
...(stop === undefined ? {} : { stopReason: stop }),
|
|
133
|
+
replyChars: text.length,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/** A thrown error: an abort wins, a busy provider may be retried, anything else ends the call. */
|
|
137
|
+
function afterError(error, t) {
|
|
138
|
+
const why = t.ctx.reasonOf();
|
|
139
|
+
if (why !== null)
|
|
140
|
+
return { done: abortedResult(why, t.usage, t.attempt) };
|
|
141
|
+
const verdict = classifyReviewFailure(error);
|
|
142
|
+
const left = t.ctx.timeoutMs - (t.ctx.options.now() - t.ctx.startedAt);
|
|
143
|
+
const mayRetry = verdict.transient && t.attempt < t.maxAttempts && left >= LEARN_CALL.minRetryWindowMs;
|
|
144
|
+
if (mayRetry)
|
|
145
|
+
return { retry: true };
|
|
146
|
+
const kind = verdict.transient ? 'transient' : 'failure';
|
|
147
|
+
const { errorType } = verdict;
|
|
148
|
+
return { done: failure({ kind, errorType, message: errorText(error), usage: t.usage, attempts: t.attempt }) };
|
|
149
|
+
}
|
|
150
|
+
async function oneAttempt(ctx) {
|
|
151
|
+
return ctx.options.complete({
|
|
152
|
+
system: ctx.options.system,
|
|
153
|
+
user: ctx.options.user,
|
|
154
|
+
maxTokens: LEARN_CALL.maxTokens,
|
|
155
|
+
temperature: 0,
|
|
156
|
+
signal: ctx.controller.signal,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
function abortedResult(why, usage, attempts) {
|
|
160
|
+
const message = why === 'timeout' ? 'learning call timed out' : 'cancelled';
|
|
161
|
+
return failure({ kind: why, errorType: why, message, usage, attempts });
|
|
162
|
+
}
|
|
163
|
+
function failure(r) {
|
|
164
|
+
return { ok: false, ...r };
|
|
165
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { LearningSignal } from '@aragon-agent/tui/model/gene/signals';
|
|
2
|
+
import type { LedgerSnapshot } from './ledger.js';
|
|
3
|
+
export declare const GENE_LEARN_PROMPT_VERSION = "v1-2026-11";
|
|
4
|
+
export declare const LEARN_INPUT_LIMITS: {
|
|
5
|
+
readonly sectionsMaxBytes: number;
|
|
6
|
+
readonly totalMaxBytes: number;
|
|
7
|
+
readonly goalMax: 500;
|
|
8
|
+
readonly signalsMaxBytes: 600;
|
|
9
|
+
readonly usedMax: 12;
|
|
10
|
+
readonly usedWindowMax: 600;
|
|
11
|
+
readonly usedSectionMaxBytes: number;
|
|
12
|
+
readonly lineMax: 700;
|
|
13
|
+
};
|
|
14
|
+
/** The system prompt. Source constant: nothing a run or a gene says can reach it. */
|
|
15
|
+
export declare const LEARN_SYSTEM_PROMPT: string;
|
|
16
|
+
/**
|
|
17
|
+
* One line, fence-tag defended, clipped: the only form untrusted text takes in the input.
|
|
18
|
+
*
|
|
19
|
+
* NO ENTITY SUBSTITUTION (RV-19, RC-3). The reviewer is told to quote what it was shown, and that
|
|
20
|
+
* quote becomes an evidence snapshot compared verbatim against the draft. Escaping `&&` to
|
|
21
|
+
* `&&` here made every shell-command quote unmatchable. `line-noent` keeps what the fence
|
|
22
|
+
* needs (one line, no controls, no open tag) and leaves the characters alone.
|
|
23
|
+
*/
|
|
24
|
+
export declare function oneLine(text: string, max?: number): string;
|
|
25
|
+
/**
|
|
26
|
+
* The LEDGER section within `maxBytes`: unsignalled and oldest frames are dropped first, the
|
|
27
|
+
* survivors stay in chronological order, and a note says how many went.
|
|
28
|
+
*/
|
|
29
|
+
export declare function projectLedger(snap: LedgerSnapshot, maxBytes: number): string;
|
|
30
|
+
/**
|
|
31
|
+
* What followed a gene's first read: later user messages and tool outcomes, oldest first,
|
|
32
|
+
* as one compact line of at most `max` characters. This is the ONLY evidence the reviewer
|
|
33
|
+
* has for judging the gene (I-9), so it is built from frames AFTER the read, never from the run
|
|
34
|
+
* as a whole.
|
|
35
|
+
*/
|
|
36
|
+
export declare function windowAfterRead(snap: LedgerSnapshot, geneId: string, max: number): string;
|
|
37
|
+
export interface UsedGene {
|
|
38
|
+
id: string;
|
|
39
|
+
/** The tier-1 line, or '' when the gene is no longer in the library. */
|
|
40
|
+
line: string;
|
|
41
|
+
}
|
|
42
|
+
export interface LearningInputParts {
|
|
43
|
+
snapshot: LedgerSnapshot;
|
|
44
|
+
signals: readonly LearningSignal[];
|
|
45
|
+
used: readonly UsedGene[];
|
|
46
|
+
/** Tier-1 lines for EXISTING, most relevant first (the reviewer's context budget). */
|
|
47
|
+
existing: readonly string[];
|
|
48
|
+
}
|
|
49
|
+
export interface LearningInput {
|
|
50
|
+
text: string;
|
|
51
|
+
bytes: number;
|
|
52
|
+
/** Ids listed in GENES_USED: the only ones an attribution may name. */
|
|
53
|
+
usedIds: string[];
|
|
54
|
+
existingIncluded: number;
|
|
55
|
+
existingOmitted: number;
|
|
56
|
+
}
|
|
57
|
+
/** Assemble the five sections (10.1.3). Deterministic for a given ledger. */
|
|
58
|
+
export declare function buildLearningInput(parts: LearningInputParts): LearningInput;
|