@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,448 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `GeneLearner`: turns a finished run into at most a few queued proposals - and, far more
|
|
3
|
+
* often, into nothing at all (gene-bank spec 10.1, G-08, I-8, I-9, I-12, I-18).
|
|
4
|
+
*
|
|
5
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
6
|
+
*
|
|
7
|
+
* THE SHAPE OF THE DECISION. Events flow into an in-memory `EvidenceLedger`; at the end of a run
|
|
8
|
+
* (or, for a user correction or a gene flag, at the next turn end) the learner reduces the ledger
|
|
9
|
+
* to COUNTS, asks the pure gate whether any signal justifies a model call, and only then pays
|
|
10
|
+
* for one. The order inside `evaluate` is the invariant:
|
|
11
|
+
*
|
|
12
|
+
* 1. signals from counts -> none? return. (I-8: no file read, no write, no call)
|
|
13
|
+
* 2. cheap switches + pauses -> any? return.
|
|
14
|
+
* 3. the daily budget -> the ONLY file read, and only now that a call is about to be paid for
|
|
15
|
+
* 4. switches AGAIN -> the user may have turned learning off during step 3 (I-18)
|
|
16
|
+
* 5. the call, in the background; the foreground has long since returned (G-10)
|
|
17
|
+
*
|
|
18
|
+
* SINGLE FLIGHT. One learning call at a time. A trigger that arrives while one is running sets a
|
|
19
|
+
* flag and is judged again when it ends; the watermark taken at fire time means the signals the
|
|
20
|
+
* running call already covers cannot fire a second one.
|
|
21
|
+
*
|
|
22
|
+
* NOTHING THE MODEL SAYS WRITES THE LIBRARY. The reply is normalised, its evidence checked
|
|
23
|
+
* against the ledger, its attributions limited by I-9, its proposals run through the oversight
|
|
24
|
+
* fence (I-12) and only then enqueued; the curator validates again before anything is published.
|
|
25
|
+
*
|
|
26
|
+
* FAILURE POLICY. A transient provider error (rate limit, overload) costs nothing and is retried
|
|
27
|
+
* at the next trigger. A real failure or a timeout is a strike; three in a row disable learning
|
|
28
|
+
* for the session (`learner_failing`) and the status chip says so. A cancelled call (`/clear`,
|
|
29
|
+
* exit) is dropped without a trace: no strike, no enqueue. A reply that stopped at the output
|
|
30
|
+
* ceiling and did not parse is a truncation, not a model failure: it costs no strike and rolls the
|
|
31
|
+
* watermark back, so the next trigger pays for that run again instead of losing the experience.
|
|
32
|
+
*/
|
|
33
|
+
import { collectSignals, shouldLearn, } from '@aragon-agent/tui/model/gene/signals';
|
|
34
|
+
import { normalizeLearningOutput } from '@aragon-agent/tui/model/gene/proposal';
|
|
35
|
+
import { EvidenceLedger, } from './ledger.js';
|
|
36
|
+
import { factsFromLedger } from './signals-port.js';
|
|
37
|
+
import { buildLearningInput, LEARN_SYSTEM_PROMPT } from './learn-prompt.js';
|
|
38
|
+
import { isTruncated, runLearnCall, } from './learn-call.js';
|
|
39
|
+
import { guardProposal } from './proposal-guard.js';
|
|
40
|
+
import { adjustAttributions, buildProposal, outcomeEvents, } from './proposal-build.js';
|
|
41
|
+
export const LEARNER_STRIKES_MAX = 3;
|
|
42
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
43
|
+
/** Only the error's class name goes in a diagnostic: its message may quote the run (I-19). */
|
|
44
|
+
function nameOf(error) {
|
|
45
|
+
return error instanceof Error ? error.name : 'unknown';
|
|
46
|
+
}
|
|
47
|
+
export class GeneLearner {
|
|
48
|
+
deps;
|
|
49
|
+
ledger;
|
|
50
|
+
inFlight = null;
|
|
51
|
+
rerun = false;
|
|
52
|
+
reviewing = false;
|
|
53
|
+
abort = null;
|
|
54
|
+
sessionCount = 0;
|
|
55
|
+
lastLearnAt;
|
|
56
|
+
strikes = 0;
|
|
57
|
+
selfDisabled = false;
|
|
58
|
+
budgetExhausted = false;
|
|
59
|
+
watermark;
|
|
60
|
+
disposed = false;
|
|
61
|
+
/** The call's spend, attached to the FIRST record written after it (the budget reads it back). */
|
|
62
|
+
pendingTokens;
|
|
63
|
+
constructor(deps) {
|
|
64
|
+
this.deps = deps;
|
|
65
|
+
this.ledger = new EvidenceLedger({ knownSecrets: deps.knownSecrets });
|
|
66
|
+
}
|
|
67
|
+
// ---- the feed (all synchronous, none may throw into the agent loop) ----
|
|
68
|
+
onUserMessage(text) {
|
|
69
|
+
this.guarded(() => this.ledger.addUser(text));
|
|
70
|
+
}
|
|
71
|
+
onToolStart(event) {
|
|
72
|
+
this.guarded(() => this.ledger.startCall(event.id, event.tool, event.args));
|
|
73
|
+
}
|
|
74
|
+
onToolEnd(event) {
|
|
75
|
+
this.guarded(() => this.ledger.endCall(event));
|
|
76
|
+
}
|
|
77
|
+
onTurnEnd(event) {
|
|
78
|
+
this.guarded(() => {
|
|
79
|
+
this.ledger.endTurn(event.tail);
|
|
80
|
+
this.evaluate('turn_end');
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
onAgentEnd(event) {
|
|
84
|
+
this.guarded(() => {
|
|
85
|
+
this.ledger.finishRun(event.outcome, event.todoAllDone === true);
|
|
86
|
+
this.evaluate('agent_end');
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
onGeneEvent(event) {
|
|
90
|
+
this.guarded(() => this.ledger.addGeneEvent(event));
|
|
91
|
+
}
|
|
92
|
+
onChildReport(summary) {
|
|
93
|
+
this.guarded(() => this.ledger.addChild(summary));
|
|
94
|
+
}
|
|
95
|
+
onRetry() {
|
|
96
|
+
this.guarded(() => this.ledger.noteRetry());
|
|
97
|
+
}
|
|
98
|
+
/** The learner observes; a bug in it must never become a bug in the user's turn (I-24). */
|
|
99
|
+
guarded(action) {
|
|
100
|
+
if (this.disposed)
|
|
101
|
+
return;
|
|
102
|
+
try {
|
|
103
|
+
action();
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
this.deps.log?.warn('gene_learner_event_failed', { error: nameOf(error) });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// ---- lifecycle -----------------------------------------------------------
|
|
110
|
+
/** `/clear` and `/resume`: forget the ledger and abandon a call in flight WITHOUT enqueueing. */
|
|
111
|
+
resetSession() {
|
|
112
|
+
this.abort?.abort();
|
|
113
|
+
this.ledger.resetSession();
|
|
114
|
+
this.watermark = undefined;
|
|
115
|
+
this.rerun = false;
|
|
116
|
+
this.deps.notify?.();
|
|
117
|
+
}
|
|
118
|
+
/** Exit: nothing more is observed, nothing in flight is enqueued. */
|
|
119
|
+
dispose() {
|
|
120
|
+
this.disposed = true;
|
|
121
|
+
this.abort?.abort();
|
|
122
|
+
}
|
|
123
|
+
/** Re-arm after `learner_failing` (a model change, or the user turning learning back on). */
|
|
124
|
+
resume() {
|
|
125
|
+
this.strikes = 0;
|
|
126
|
+
this.selfDisabled = false;
|
|
127
|
+
this.deps.notify?.();
|
|
128
|
+
}
|
|
129
|
+
/** Resolves when no learning call is in flight (tests, orderly shutdown). */
|
|
130
|
+
async idle() {
|
|
131
|
+
while (this.inFlight !== null)
|
|
132
|
+
await this.inFlight;
|
|
133
|
+
}
|
|
134
|
+
// ---- the view (hub facts) --------------------------------------------------
|
|
135
|
+
/** A non-budget reason learning cannot run right now. Pure and synchronous. */
|
|
136
|
+
hardPause() {
|
|
137
|
+
const mode = this.deps.mode();
|
|
138
|
+
if (mode.printMode && !mode.learnFlag)
|
|
139
|
+
return 'print_mode';
|
|
140
|
+
if (mode.redTeamArmed)
|
|
141
|
+
return 'redteam';
|
|
142
|
+
if (this.selfDisabled)
|
|
143
|
+
return 'learner_failing';
|
|
144
|
+
if (this.deps.project?.last()?.learn === false)
|
|
145
|
+
return 'project_excluded';
|
|
146
|
+
if (this.deps.complete === undefined || !this.deps.tier().ok)
|
|
147
|
+
return 'no_fast_tier';
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
/** Where `learn` stands, for the status chip and the hub. */
|
|
151
|
+
view() {
|
|
152
|
+
const cfg = this.deps.config();
|
|
153
|
+
const base = { sessionCount: this.sessionCount };
|
|
154
|
+
if (!cfg.enabled)
|
|
155
|
+
return { state: 'off', ...base };
|
|
156
|
+
if (!cfg.autoLearn)
|
|
157
|
+
return { state: 'paused', pausedReason: 'learn_off', ...base };
|
|
158
|
+
const reason = this.hardPause() ?? (this.budgetExhausted ? 'daily_budget' : undefined);
|
|
159
|
+
if (reason !== undefined)
|
|
160
|
+
return { state: 'paused', pausedReason: reason, ...base };
|
|
161
|
+
return { state: this.reviewing ? 'reviewing' : 'idle', ...base };
|
|
162
|
+
}
|
|
163
|
+
/** Re-read the project switch so `view()` is current (hub poll). Never throws. */
|
|
164
|
+
async refreshProject() {
|
|
165
|
+
const before = this.deps.project?.last();
|
|
166
|
+
if (!(await this.projectMayLearn()) || before?.learn === false)
|
|
167
|
+
this.deps.notify?.();
|
|
168
|
+
}
|
|
169
|
+
/** Re-read the shared daily spend. Called by the hub's poll and, once, before a paid call. */
|
|
170
|
+
async refreshBudget() {
|
|
171
|
+
try {
|
|
172
|
+
const cfg = this.deps.config();
|
|
173
|
+
if (cfg.dailyTokens <= 0) {
|
|
174
|
+
this.budgetExhausted = true;
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const spent = await this.deps.runLog.tokensSince(this.deps.now() - DAY_MS);
|
|
178
|
+
this.budgetExhausted = spent >= cfg.dailyTokens;
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// Keep the last known answer: an unreadable log must not flip learning on or off.
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// ---- the gate ------------------------------------------------------------------
|
|
185
|
+
evaluate(trigger) {
|
|
186
|
+
const snapshot = this.ledger.snapshot();
|
|
187
|
+
const signals = collectSignals(factsFromLedger(snapshot, this.watermark));
|
|
188
|
+
// I-8: with nothing to learn from, NOTHING below this line runs - no read, no write, no call.
|
|
189
|
+
if (signals.length === 0)
|
|
190
|
+
return;
|
|
191
|
+
if (trigger === 'turn_end' && !signals.some((s) => s.id === 'L1' || s.id === 'L3'))
|
|
192
|
+
return;
|
|
193
|
+
if (this.inFlight !== null) {
|
|
194
|
+
this.rerun = true;
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
this.inFlight = this.decide(signals).finally(() => this.afterFlight());
|
|
198
|
+
}
|
|
199
|
+
afterFlight() {
|
|
200
|
+
this.inFlight = null;
|
|
201
|
+
if (!this.rerun)
|
|
202
|
+
return;
|
|
203
|
+
this.rerun = false;
|
|
204
|
+
if (!this.disposed)
|
|
205
|
+
this.evaluate(this.ledger.isActive() ? 'turn_end' : 'agent_end');
|
|
206
|
+
}
|
|
207
|
+
async decide(signals) {
|
|
208
|
+
try {
|
|
209
|
+
// The privacy switch is judged FIRST and from disk: nothing below may run, and above all
|
|
210
|
+
// no transport call may be made, for a project that is switched off.
|
|
211
|
+
if (!(await this.projectMayLearn()))
|
|
212
|
+
return;
|
|
213
|
+
const cfg = this.deps.config();
|
|
214
|
+
const now = this.deps.now();
|
|
215
|
+
const gate = shouldLearn({
|
|
216
|
+
signals,
|
|
217
|
+
autoLearnOn: cfg.enabled && cfg.autoLearn,
|
|
218
|
+
paused: this.hardPause() !== undefined,
|
|
219
|
+
msSinceLastLearn: this.lastLearnAt === undefined ? Number.POSITIVE_INFINITY : now - this.lastLearnAt,
|
|
220
|
+
minIntervalMs: cfg.learnMinIntervalSec * 1000,
|
|
221
|
+
sessionCount: this.sessionCount,
|
|
222
|
+
sessionMax: cfg.learnMaxPerSession,
|
|
223
|
+
});
|
|
224
|
+
if (!gate.learn)
|
|
225
|
+
return;
|
|
226
|
+
await this.refreshBudget();
|
|
227
|
+
// The user may have switched learning off while the budget was being read (I-18).
|
|
228
|
+
const live = this.deps.config();
|
|
229
|
+
if (this.disposed || this.budgetExhausted || !(live.enabled && live.autoLearn))
|
|
230
|
+
return;
|
|
231
|
+
// And once more, immediately before the call: the user may have switched the project off.
|
|
232
|
+
if (!(await this.projectMayLearn()))
|
|
233
|
+
return;
|
|
234
|
+
await this.learn({ signals, reasons: gate.reasons }, live);
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
this.deps.log?.warn('gene_learner_failed', { error: nameOf(error) });
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
/** A failed read of the policy is already folded into the verdict (unreadable = excluded). */
|
|
241
|
+
async projectMayLearn() {
|
|
242
|
+
const project = this.deps.project;
|
|
243
|
+
if (project === undefined)
|
|
244
|
+
return true;
|
|
245
|
+
try {
|
|
246
|
+
return (await project.refresh()).learn;
|
|
247
|
+
}
|
|
248
|
+
catch {
|
|
249
|
+
return false; // an unreadable policy is "excluded", never "allowed" (I3-5 fail direction)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
// ---- the call ----------------------------------------------------------------------
|
|
253
|
+
async learn(job, cfg) {
|
|
254
|
+
const complete = this.deps.complete;
|
|
255
|
+
if (complete === undefined)
|
|
256
|
+
return;
|
|
257
|
+
const snapshot = this.ledger.snapshot();
|
|
258
|
+
const before = { watermark: this.watermark, lastLearnAt: this.lastLearnAt };
|
|
259
|
+
// Taken BEFORE the call: signals this call covers must not fire the next one.
|
|
260
|
+
this.watermark = this.ledger.watermark();
|
|
261
|
+
this.lastLearnAt = this.deps.now();
|
|
262
|
+
this.sessionCount += 1;
|
|
263
|
+
this.setReviewing(true);
|
|
264
|
+
const controller = new AbortController();
|
|
265
|
+
this.abort = controller;
|
|
266
|
+
await this.record({ ev: 'reviewing', detail: job.reasons.join(',') });
|
|
267
|
+
const prepared = await this.prepare(job, snapshot, cfg);
|
|
268
|
+
const result = await runLearnCall({
|
|
269
|
+
complete,
|
|
270
|
+
system: LEARN_SYSTEM_PROMPT,
|
|
271
|
+
user: prepared.input.text,
|
|
272
|
+
signal: controller.signal,
|
|
273
|
+
now: this.deps.now,
|
|
274
|
+
...(this.deps.timers ? { timers: this.deps.timers } : {}),
|
|
275
|
+
...(this.deps.sleep ? { sleep: this.deps.sleep } : {}),
|
|
276
|
+
...(this.deps.retries !== undefined ? { retries: this.deps.retries } : {}),
|
|
277
|
+
});
|
|
278
|
+
this.abort = null;
|
|
279
|
+
this.setReviewing(false);
|
|
280
|
+
if (this.disposed || (!result.ok && result.kind === 'cancelled')) {
|
|
281
|
+
this.undoFire(before);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
this.deps.onUsage?.(result.usage);
|
|
285
|
+
if (result.ok) {
|
|
286
|
+
await this.settleReply(result.text, { job, prepared, usage: result.usage, truncated: isTruncated(result), replyChars: result.replyChars }, before);
|
|
287
|
+
}
|
|
288
|
+
else
|
|
289
|
+
await this.settleFailure(result, before);
|
|
290
|
+
this.deps.notify?.();
|
|
291
|
+
}
|
|
292
|
+
async prepare(job, snapshot, cfg) {
|
|
293
|
+
let existing = [];
|
|
294
|
+
try {
|
|
295
|
+
existing = await this.deps.existingLines({
|
|
296
|
+
maxItems: cfg.reviewerMaxItems,
|
|
297
|
+
maxTokens: cfg.reviewerMaxTokens,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
// Reads fail open: a library that cannot be listed just means no dedupe context this time.
|
|
302
|
+
}
|
|
303
|
+
const used = [...snapshot.reads.keys()].map((id) => ({ id, line: this.deps.describeGene(id) ?? '' }));
|
|
304
|
+
return { input: buildLearningInput({ snapshot, signals: job.signals, used, existing }), snapshot };
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Un-spend one attempt: the run's signals become learnable again and the session count goes
|
|
308
|
+
* back. The cooldown (`lastLearnAt`) is NOT part of it - a busy provider, a truncation and a
|
|
309
|
+
* cancellation all keep it, and only a cancellation gives it back (`undoFire`).
|
|
310
|
+
*/
|
|
311
|
+
giveBack(before) {
|
|
312
|
+
this.watermark = before.watermark;
|
|
313
|
+
this.sessionCount = Math.max(0, this.sessionCount - 1);
|
|
314
|
+
}
|
|
315
|
+
undoFire(before) {
|
|
316
|
+
this.giveBack(before);
|
|
317
|
+
this.lastLearnAt = before.lastLearnAt;
|
|
318
|
+
this.deps.notify?.();
|
|
319
|
+
}
|
|
320
|
+
setReviewing(value) {
|
|
321
|
+
this.reviewing = value;
|
|
322
|
+
this.deps.notify?.();
|
|
323
|
+
}
|
|
324
|
+
async settleFailure(result, before) {
|
|
325
|
+
// The spend of a FAILED call is still spend: without this the run log said `tokens: null` for
|
|
326
|
+
// every truncation and the budget could never see the wasted calls (RV-14).
|
|
327
|
+
this.pendingTokens = { in: result.usage.inTok, out: result.usage.outTok, component: 'reviewer' };
|
|
328
|
+
if (result.kind === 'transient') {
|
|
329
|
+
// Busy provider: not our fault and not a strike. Keep the cooldown, forget the attempt.
|
|
330
|
+
this.giveBack(before);
|
|
331
|
+
await this.record({ ev: 'skipped', detail: `transient:${result.errorType}` });
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
this.noteStrike();
|
|
335
|
+
await this.record({ ev: 'error', detail: `${result.kind}:${result.errorType}` });
|
|
336
|
+
}
|
|
337
|
+
noteStrike() {
|
|
338
|
+
this.strikes += 1;
|
|
339
|
+
if (this.strikes >= LEARNER_STRIKES_MAX)
|
|
340
|
+
this.selfDisabled = true;
|
|
341
|
+
}
|
|
342
|
+
// ---- the reply -------------------------------------------------------------------------
|
|
343
|
+
async settleReply(text, ctx, before) {
|
|
344
|
+
const { snapshot, input } = ctx.prepared;
|
|
345
|
+
const normalized = normalizeLearningOutput(text, {
|
|
346
|
+
readGeneIds: input.usedIds,
|
|
347
|
+
scopeOf: this.deps.scopeOf,
|
|
348
|
+
});
|
|
349
|
+
this.pendingTokens = { in: ctx.usage.inTok, out: ctx.usage.outTok, component: 'reviewer' };
|
|
350
|
+
if (normalized.parseFailed === true) {
|
|
351
|
+
// PARSE FIRST, THEN BLAME (RV-18). A reply cut at OUR ceiling is not the tier's fault and not
|
|
352
|
+
// the run's fault: no strike, the watermark rolls back so the next trigger tries again, and
|
|
353
|
+
// the cooldown stays. Without this, three truncations in a row disabled learning for the
|
|
354
|
+
// whole session and the signals they covered were dropped forever (RC-1).
|
|
355
|
+
if (ctx.truncated) {
|
|
356
|
+
this.giveBack(before);
|
|
357
|
+
await this.record({ ev: 'skipped', detail: `truncated:reply=${ctx.replyChars}` });
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
this.noteStrike();
|
|
361
|
+
await this.record({ ev: 'error', detail: 'unparseable_reply' });
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
this.strikes = 0;
|
|
365
|
+
const adjusted = adjustAttributions(normalized.attributions, snapshot);
|
|
366
|
+
this.emitOutcomes(adjusted.kept, snapshot);
|
|
367
|
+
const made = await this.enqueueProposals(normalized.proposals, { ctx, adjusted: adjusted.kept });
|
|
368
|
+
if (made > 0)
|
|
369
|
+
return;
|
|
370
|
+
const dropped = normalized.dropped + adjusted.downgraded;
|
|
371
|
+
let detail = 'nothing_new';
|
|
372
|
+
if (normalized.proposals.length > 0)
|
|
373
|
+
detail = 'none_accepted';
|
|
374
|
+
else if (dropped > 0)
|
|
375
|
+
detail = `dropped=${dropped}`;
|
|
376
|
+
await this.record({ ev: 'reviewed_none', detail });
|
|
377
|
+
}
|
|
378
|
+
emitOutcomes(kept, snapshot) {
|
|
379
|
+
if (!this.deps.recordUsage)
|
|
380
|
+
return;
|
|
381
|
+
const events = outcomeEvents(kept, {
|
|
382
|
+
now: this.deps.now(),
|
|
383
|
+
runId: this.deps.session().runId,
|
|
384
|
+
snapshot,
|
|
385
|
+
scopeOf: this.deps.scopeOf,
|
|
386
|
+
...(this.deps.versionOf ? { versionOf: this.deps.versionOf } : {}),
|
|
387
|
+
});
|
|
388
|
+
for (const event of events)
|
|
389
|
+
this.deps.recordUsage(event);
|
|
390
|
+
}
|
|
391
|
+
/** Build, fence and enqueue each draft. Returns how many reached the queue. */
|
|
392
|
+
async enqueueProposals(drafts, args) {
|
|
393
|
+
let made = 0;
|
|
394
|
+
for (const draft of drafts) {
|
|
395
|
+
const proposal = buildProposal(draft, {
|
|
396
|
+
now: this.deps.now(),
|
|
397
|
+
random: this.deps.random,
|
|
398
|
+
session: this.deps.session(),
|
|
399
|
+
snapshot: args.ctx.prepared.snapshot,
|
|
400
|
+
signals: args.ctx.job.signals,
|
|
401
|
+
attributions: args.adjusted,
|
|
402
|
+
basisOf: this.deps.basisOf,
|
|
403
|
+
knownSecrets: this.deps.knownSecrets?.() ?? [],
|
|
404
|
+
});
|
|
405
|
+
if (proposal === null) {
|
|
406
|
+
await this.record({ ev: 'skipped', detail: 'unanchored' });
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (await this.admit(proposal))
|
|
410
|
+
made += 1;
|
|
411
|
+
}
|
|
412
|
+
return made;
|
|
413
|
+
}
|
|
414
|
+
async admit(proposal) {
|
|
415
|
+
const verdict = guardProposal(proposal, this.deps.lookupGene);
|
|
416
|
+
const type = proposal.type ? { type: proposal.type } : {};
|
|
417
|
+
const entry = { proposalId: proposal.id, scope: proposal.scope, ...type };
|
|
418
|
+
if (!verdict.ok) {
|
|
419
|
+
await this.record({ ev: 'skipped', ...entry, detail: `guard:${verdict.code}` });
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
const queued = await this.deps.queue.enqueue(proposal);
|
|
423
|
+
if (!queued.ok) {
|
|
424
|
+
await this.record({ ev: 'skipped', ...entry, detail: `queue:${queued.reason}` });
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
await this.record({ ev: 'proposed', ...entry, detail: `kind=${proposal.kind}` });
|
|
428
|
+
if (queued.merged) {
|
|
429
|
+
await this.record({ ev: 'merged_into_pending', ...entry, proposalId: queued.mergedInto, detail: 'folded' });
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
await this.record({ ev: 'queued', ...entry, detail: `p=${proposal.priority}` });
|
|
433
|
+
}
|
|
434
|
+
return true;
|
|
435
|
+
}
|
|
436
|
+
async record(entry) {
|
|
437
|
+
if (this.disposed)
|
|
438
|
+
return;
|
|
439
|
+
const tokens = this.pendingTokens;
|
|
440
|
+
this.pendingTokens = undefined;
|
|
441
|
+
try {
|
|
442
|
+
await this.deps.runLog.append(tokens && entry.tokens === undefined ? { ...entry, tokens } : entry);
|
|
443
|
+
}
|
|
444
|
+
catch {
|
|
445
|
+
// The run log never throws (I-24); a custom sink might, and the learner must not care.
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
export declare const LEDGER_LIMITS: {
|
|
2
|
+
readonly maxBytes: number;
|
|
3
|
+
readonly userText: 600;
|
|
4
|
+
readonly usersMax: 24;
|
|
5
|
+
readonly goalMax: 500;
|
|
6
|
+
readonly callsMax: 80;
|
|
7
|
+
readonly argText: 160;
|
|
8
|
+
readonly errText: 300;
|
|
9
|
+
readonly arcsMax: 8;
|
|
10
|
+
/** A repair counts only if the same tool succeeds within this many calls of the failure. */
|
|
11
|
+
readonly arcWindow: 6;
|
|
12
|
+
readonly genesMax: 60;
|
|
13
|
+
readonly turnsMax: 12;
|
|
14
|
+
readonly turnTail: 300;
|
|
15
|
+
readonly filesMax: 30;
|
|
16
|
+
readonly fileText: 200;
|
|
17
|
+
readonly childText: 400;
|
|
18
|
+
readonly childrenMax: 8;
|
|
19
|
+
readonly priorTurns: 2;
|
|
20
|
+
readonly priorGoal: 200;
|
|
21
|
+
};
|
|
22
|
+
export type GeneFeedbackVerdict = 'helped' | 'misleading' | 'not_applicable' | 'outdated';
|
|
23
|
+
/** What the gene subsystem tells the learner (the controller forwards these). */
|
|
24
|
+
export type GeneLedgerEvent = {
|
|
25
|
+
kind: 'shown';
|
|
26
|
+
geneIds: readonly string[];
|
|
27
|
+
} | {
|
|
28
|
+
kind: 'search';
|
|
29
|
+
query: string;
|
|
30
|
+
} | {
|
|
31
|
+
kind: 'read';
|
|
32
|
+
geneId: string;
|
|
33
|
+
tier: 2 | 3;
|
|
34
|
+
version?: number;
|
|
35
|
+
} | {
|
|
36
|
+
kind: 'feedback';
|
|
37
|
+
geneId: string;
|
|
38
|
+
verdict: GeneFeedbackVerdict;
|
|
39
|
+
}
|
|
40
|
+
/** A `gene_run` result: a deterministic outcome for that gene, no model needed. */
|
|
41
|
+
| {
|
|
42
|
+
kind: 'run_result';
|
|
43
|
+
geneId: string;
|
|
44
|
+
ok: boolean;
|
|
45
|
+
} | {
|
|
46
|
+
kind: 'unfamiliar_recovery';
|
|
47
|
+
};
|
|
48
|
+
export type RunOutcome = 'completed' | 'error' | 'aborted';
|
|
49
|
+
interface Sized {
|
|
50
|
+
seq: number;
|
|
51
|
+
size: number;
|
|
52
|
+
signalled: boolean;
|
|
53
|
+
}
|
|
54
|
+
export interface LedgerUser extends Sized {
|
|
55
|
+
text: string;
|
|
56
|
+
correction: boolean;
|
|
57
|
+
statedFact: boolean;
|
|
58
|
+
afterAction: boolean;
|
|
59
|
+
}
|
|
60
|
+
export interface LedgerCall extends Sized {
|
|
61
|
+
id: string;
|
|
62
|
+
tool: string;
|
|
63
|
+
arg?: string;
|
|
64
|
+
ok: boolean;
|
|
65
|
+
err?: string;
|
|
66
|
+
ms: number;
|
|
67
|
+
sig?: string;
|
|
68
|
+
turn: number;
|
|
69
|
+
}
|
|
70
|
+
export interface LedgerArc extends Sized {
|
|
71
|
+
sig: string;
|
|
72
|
+
tool: string;
|
|
73
|
+
/** Calls between the failure and the success. */
|
|
74
|
+
gap: number;
|
|
75
|
+
/** Some other tool ran in between: an actual repair, not a plain retry. */
|
|
76
|
+
viaFix: boolean;
|
|
77
|
+
}
|
|
78
|
+
export interface LedgerGene extends Sized {
|
|
79
|
+
kind: 'search' | 'read' | 'feedback' | 'run_result';
|
|
80
|
+
geneId?: string;
|
|
81
|
+
query?: string;
|
|
82
|
+
version?: number;
|
|
83
|
+
tier?: 2 | 3;
|
|
84
|
+
verdict?: GeneFeedbackVerdict;
|
|
85
|
+
ok?: boolean;
|
|
86
|
+
}
|
|
87
|
+
export interface LedgerTurn extends Sized {
|
|
88
|
+
index: number;
|
|
89
|
+
tail: string;
|
|
90
|
+
}
|
|
91
|
+
export interface LedgerRead {
|
|
92
|
+
seq: number;
|
|
93
|
+
tier: 2 | 3;
|
|
94
|
+
version?: number;
|
|
95
|
+
}
|
|
96
|
+
export interface LedgerCounters {
|
|
97
|
+
userCorrections: number;
|
|
98
|
+
correctionAfterAction: number;
|
|
99
|
+
statedFacts: number;
|
|
100
|
+
toolCalls: number;
|
|
101
|
+
toolErrors: number;
|
|
102
|
+
retries: number;
|
|
103
|
+
arcs: number;
|
|
104
|
+
flaggedFeedback: number;
|
|
105
|
+
unfamiliarRecoveries: number;
|
|
106
|
+
turns: number;
|
|
107
|
+
}
|
|
108
|
+
export interface LedgerSnapshot {
|
|
109
|
+
/** Which run this describes. A watermark from another run says nothing about this one. */
|
|
110
|
+
run: number;
|
|
111
|
+
active: boolean;
|
|
112
|
+
goal: string;
|
|
113
|
+
prior?: {
|
|
114
|
+
goal: string;
|
|
115
|
+
tails: readonly string[];
|
|
116
|
+
};
|
|
117
|
+
users: readonly LedgerUser[];
|
|
118
|
+
calls: readonly LedgerCall[];
|
|
119
|
+
arcs: readonly LedgerArc[];
|
|
120
|
+
genes: readonly LedgerGene[];
|
|
121
|
+
turns: readonly LedgerTurn[];
|
|
122
|
+
files: readonly string[];
|
|
123
|
+
children: readonly string[];
|
|
124
|
+
counters: LedgerCounters;
|
|
125
|
+
/** Failed-call count per error signature, whole run. */
|
|
126
|
+
sigCounts: ReadonlyMap<string, number>;
|
|
127
|
+
reads: ReadonlyMap<string, LedgerRead>;
|
|
128
|
+
feedback: ReadonlyMap<string, GeneFeedbackVerdict>;
|
|
129
|
+
runResults: ReadonlyMap<string, boolean>;
|
|
130
|
+
shown: ReadonlySet<string>;
|
|
131
|
+
outcome: RunOutcome | 'running';
|
|
132
|
+
todoAllDone: boolean;
|
|
133
|
+
lastVerifyOk: boolean;
|
|
134
|
+
bytes: number;
|
|
135
|
+
}
|
|
136
|
+
/** A frozen copy of the countable facts, used to ask "what is NEW since the last learning call". */
|
|
137
|
+
export interface LedgerWatermark {
|
|
138
|
+
/** The run it was taken in; `factsFromLedger` ignores it once a new run has begun. */
|
|
139
|
+
run: number;
|
|
140
|
+
counters: LedgerCounters;
|
|
141
|
+
sigCounts: ReadonlyMap<string, number>;
|
|
142
|
+
/** Genes already read when the mark was taken: the learning call has attributed those. */
|
|
143
|
+
readIds: ReadonlySet<string>;
|
|
144
|
+
}
|
|
145
|
+
export interface LedgerDeps {
|
|
146
|
+
knownSecrets?: () => readonly string[];
|
|
147
|
+
maxBytes?: number;
|
|
148
|
+
}
|
|
149
|
+
/** The one argument a tool call is worth naming (same keys as the fast reviewer's digest). */
|
|
150
|
+
export declare function pickArg(args: unknown): string | undefined;
|
|
151
|
+
/** A call whose success means "the work was checked": a test / build / lint style command. */
|
|
152
|
+
export declare function isVerifyCall(tool: string, arg: string | undefined): boolean;
|
|
153
|
+
export declare function isFileTool(tool: string): boolean;
|
|
154
|
+
export declare class EvidenceLedger {
|
|
155
|
+
private readonly deps;
|
|
156
|
+
private readonly maxBytes;
|
|
157
|
+
private seq;
|
|
158
|
+
private runSerial;
|
|
159
|
+
private active;
|
|
160
|
+
private sessionActed;
|
|
161
|
+
private goal;
|
|
162
|
+
private prior;
|
|
163
|
+
private users;
|
|
164
|
+
private calls;
|
|
165
|
+
private arcs;
|
|
166
|
+
private genes;
|
|
167
|
+
private turns;
|
|
168
|
+
private files;
|
|
169
|
+
private children;
|
|
170
|
+
private counters;
|
|
171
|
+
private sigCounts;
|
|
172
|
+
private reads;
|
|
173
|
+
private feedback;
|
|
174
|
+
private runResults;
|
|
175
|
+
private shown;
|
|
176
|
+
private pending;
|
|
177
|
+
private openFailures;
|
|
178
|
+
private outcome;
|
|
179
|
+
private todoAllDone;
|
|
180
|
+
private lastVerifyOk;
|
|
181
|
+
private turnIndex;
|
|
182
|
+
constructor(deps?: LedgerDeps);
|
|
183
|
+
/** Start a new run. A little of the previous run is kept so a correction has something to refer to. */
|
|
184
|
+
beginRun(): void;
|
|
185
|
+
/** `/clear`, `/resume`, exit: forget everything, including that the agent ever acted. */
|
|
186
|
+
resetSession(): void;
|
|
187
|
+
isActive(): boolean;
|
|
188
|
+
/** Mark the run over. The content stays readable until the next `beginRun`. */
|
|
189
|
+
finishRun(outcome: RunOutcome, todoAllDone: boolean): void;
|
|
190
|
+
private carryOver;
|
|
191
|
+
private clearRunState;
|
|
192
|
+
private ensureRun;
|
|
193
|
+
/** Hint-stripped, secret-scrubbed, clipped. The only way text enters the ledger. */
|
|
194
|
+
private clean;
|
|
195
|
+
addUser(text: string): void;
|
|
196
|
+
startCall(id: string, tool: string, args: unknown): void;
|
|
197
|
+
endCall(args: {
|
|
198
|
+
id: string;
|
|
199
|
+
tool: string;
|
|
200
|
+
isError: boolean;
|
|
201
|
+
text: string;
|
|
202
|
+
ms: number;
|
|
203
|
+
}): void;
|
|
204
|
+
/** A success closes the open failures of the same tool that are still inside the window. */
|
|
205
|
+
private recordRecovery;
|
|
206
|
+
endTurn(tail: string): void;
|
|
207
|
+
addGeneEvent(event: GeneLedgerEvent): void;
|
|
208
|
+
private noteFeedback;
|
|
209
|
+
private pushGene;
|
|
210
|
+
addChild(summary: string): void;
|
|
211
|
+
noteRetry(): void;
|
|
212
|
+
private push;
|
|
213
|
+
private trimCount;
|
|
214
|
+
private totalBytes;
|
|
215
|
+
/** Over 24 KB: unsignalled turns, then calls, users, genes go first; then the oldest signalled. */
|
|
216
|
+
private enforceTotal;
|
|
217
|
+
snapshot(): LedgerSnapshot;
|
|
218
|
+
watermark(): LedgerWatermark;
|
|
219
|
+
}
|
|
220
|
+
export {};
|