@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,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The evidence ledger: a bounded, in-memory record of what happened in a run (gene-bank spec
|
|
3
|
+
* 10.1.1, I-16, RV-17).
|
|
4
|
+
*
|
|
5
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
6
|
+
*
|
|
7
|
+
* NEVER PERSISTED. The ledger lives in this process and is dropped on `/clear`, `/resume` and
|
|
8
|
+
* exit. The only thing that ever leaves it is a PROJECTION built for one learning call
|
|
9
|
+
* (`learn-prompt.ts`), and only after the local gate (`signals-port.ts`) found a reason to pay
|
|
10
|
+
* for that call.
|
|
11
|
+
*
|
|
12
|
+
* EVERY TEXT IS CLEANED ON THE WAY IN, nowhere else: first `stripGeneHints` (the learner sees
|
|
13
|
+
* tool results AFTER the hint engine decorated them - without stripping, the library's own
|
|
14
|
+
* advice would be distilled back into the library, RV-17), then `redactSecrets`, then clipped.
|
|
15
|
+
* Downstream code - the projection, the evidence check, the signatures - can therefore treat
|
|
16
|
+
* ledger text as already safe. The one exception is the error SIGNATURE, hashed from the
|
|
17
|
+
* stripped-but-not-redacted text so it matches `KnowledgePayload.resolves` (both come from
|
|
18
|
+
* `errorSignature`); a hash cannot leak what it hashes.
|
|
19
|
+
*
|
|
20
|
+
* BOUNDED TWO WAYS: per-array entry counts, and a 24 KB total. Over the total, UNSIGNALLED
|
|
21
|
+
* frames go first (an ordinary successful call tells the reviewer nothing), oldest first, and
|
|
22
|
+
* signalled frames last - a user's correction is the last thing to be forgotten.
|
|
23
|
+
*/
|
|
24
|
+
import { detectStatedFact, detectUserCorrection, errorSignature, stripGeneHints, } from '@aragon-agent/tui/model/gene/signals';
|
|
25
|
+
import { redactSecrets } from '../secrets.js';
|
|
26
|
+
export const LEDGER_LIMITS = {
|
|
27
|
+
maxBytes: 24 * 1024,
|
|
28
|
+
userText: 600,
|
|
29
|
+
usersMax: 24,
|
|
30
|
+
goalMax: 500,
|
|
31
|
+
callsMax: 80,
|
|
32
|
+
argText: 160,
|
|
33
|
+
errText: 300,
|
|
34
|
+
arcsMax: 8,
|
|
35
|
+
/** A repair counts only if the same tool succeeds within this many calls of the failure. */
|
|
36
|
+
arcWindow: 6,
|
|
37
|
+
genesMax: 60,
|
|
38
|
+
turnsMax: 12,
|
|
39
|
+
turnTail: 300,
|
|
40
|
+
filesMax: 30,
|
|
41
|
+
fileText: 200,
|
|
42
|
+
childText: 400,
|
|
43
|
+
childrenMax: 8,
|
|
44
|
+
priorTurns: 2,
|
|
45
|
+
priorGoal: 200,
|
|
46
|
+
};
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
// Pure helpers
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
function emptyCounters() {
|
|
51
|
+
return {
|
|
52
|
+
userCorrections: 0,
|
|
53
|
+
correctionAfterAction: 0,
|
|
54
|
+
statedFacts: 0,
|
|
55
|
+
toolCalls: 0,
|
|
56
|
+
toolErrors: 0,
|
|
57
|
+
retries: 0,
|
|
58
|
+
arcs: 0,
|
|
59
|
+
flaggedFeedback: 0,
|
|
60
|
+
unfamiliarRecoveries: 0,
|
|
61
|
+
turns: 0,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function clipPoints(text, max) {
|
|
65
|
+
const points = Array.from(text);
|
|
66
|
+
return points.length <= max ? text : `${points.slice(0, max - 3).join('')}...`;
|
|
67
|
+
}
|
|
68
|
+
/** The one argument a tool call is worth naming (same keys as the fast reviewer's digest). */
|
|
69
|
+
export function pickArg(args) {
|
|
70
|
+
const a = (args ?? {});
|
|
71
|
+
for (const key of ['path', 'file_path', 'pattern', 'command', 'query', 'label']) {
|
|
72
|
+
const value = a[key];
|
|
73
|
+
if (typeof value === 'string' && value.length > 0)
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
const SHELL_TOOL_RE = /^(?:bash|shell|powershell|pwsh|cmd|exec|run_command|execute_command)$/i;
|
|
79
|
+
const VERIFY_RE = /\b(?:test|tests|vitest|jest|pytest|mocha|tsc|typecheck|lint|build|verify|cargo\s+(?:test|check|build)|go\s+(?:test|build|vet)|dotnet\s+(?:test|build))\b/i;
|
|
80
|
+
const FILE_TOOL_RE = /^(?:write_file|edit_file|write|edit|apply_patch|multi_edit|create_file)$/i;
|
|
81
|
+
/** A call whose success means "the work was checked": a test / build / lint style command. */
|
|
82
|
+
export function isVerifyCall(tool, arg) {
|
|
83
|
+
return SHELL_TOOL_RE.test(tool) && arg !== undefined && VERIFY_RE.test(arg);
|
|
84
|
+
}
|
|
85
|
+
export function isFileTool(tool) {
|
|
86
|
+
return FILE_TOOL_RE.test(tool);
|
|
87
|
+
}
|
|
88
|
+
/** The optional version, spread-ready: absent stays absent. */
|
|
89
|
+
function versionOf(event) {
|
|
90
|
+
return event.version ? { version: event.version } : {};
|
|
91
|
+
}
|
|
92
|
+
function approxSize(entry) {
|
|
93
|
+
return JSON.stringify(entry).length;
|
|
94
|
+
}
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
// The ledger
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
export class EvidenceLedger {
|
|
99
|
+
deps;
|
|
100
|
+
maxBytes;
|
|
101
|
+
seq = 0;
|
|
102
|
+
runSerial = 0;
|
|
103
|
+
active = false;
|
|
104
|
+
sessionActed = false;
|
|
105
|
+
goal = '';
|
|
106
|
+
prior;
|
|
107
|
+
users = [];
|
|
108
|
+
calls = [];
|
|
109
|
+
arcs = [];
|
|
110
|
+
genes = [];
|
|
111
|
+
turns = [];
|
|
112
|
+
files = [];
|
|
113
|
+
children = [];
|
|
114
|
+
counters = emptyCounters();
|
|
115
|
+
sigCounts = new Map();
|
|
116
|
+
reads = new Map();
|
|
117
|
+
feedback = new Map();
|
|
118
|
+
runResults = new Map();
|
|
119
|
+
shown = new Set();
|
|
120
|
+
pending = new Map();
|
|
121
|
+
openFailures = [];
|
|
122
|
+
outcome = 'running';
|
|
123
|
+
todoAllDone = false;
|
|
124
|
+
lastVerifyOk = false;
|
|
125
|
+
turnIndex = 0;
|
|
126
|
+
constructor(deps = {}) {
|
|
127
|
+
this.deps = deps;
|
|
128
|
+
this.maxBytes = deps.maxBytes ?? LEDGER_LIMITS.maxBytes;
|
|
129
|
+
}
|
|
130
|
+
// ---- lifecycle ----------------------------------------------------------
|
|
131
|
+
/** Start a new run. A little of the previous run is kept so a correction has something to refer to. */
|
|
132
|
+
beginRun() {
|
|
133
|
+
const carried = this.carryOver();
|
|
134
|
+
this.clearRunState();
|
|
135
|
+
this.prior = carried;
|
|
136
|
+
this.runSerial += 1;
|
|
137
|
+
this.active = true;
|
|
138
|
+
}
|
|
139
|
+
/** `/clear`, `/resume`, exit: forget everything, including that the agent ever acted. */
|
|
140
|
+
resetSession() {
|
|
141
|
+
this.clearRunState();
|
|
142
|
+
this.prior = undefined;
|
|
143
|
+
this.sessionActed = false;
|
|
144
|
+
this.runSerial += 1;
|
|
145
|
+
this.active = false;
|
|
146
|
+
}
|
|
147
|
+
isActive() {
|
|
148
|
+
return this.active;
|
|
149
|
+
}
|
|
150
|
+
/** Mark the run over. The content stays readable until the next `beginRun`. */
|
|
151
|
+
finishRun(outcome, todoAllDone) {
|
|
152
|
+
this.outcome = outcome;
|
|
153
|
+
this.todoAllDone = todoAllDone;
|
|
154
|
+
this.active = false;
|
|
155
|
+
}
|
|
156
|
+
carryOver() {
|
|
157
|
+
if (this.goal === '' && this.turns.length === 0)
|
|
158
|
+
return undefined;
|
|
159
|
+
const tails = this.turns.slice(-LEDGER_LIMITS.priorTurns).map((t) => t.tail);
|
|
160
|
+
return { goal: clipPoints(this.goal, LEDGER_LIMITS.priorGoal), tails };
|
|
161
|
+
}
|
|
162
|
+
clearRunState() {
|
|
163
|
+
this.goal = '';
|
|
164
|
+
this.users = [];
|
|
165
|
+
this.calls = [];
|
|
166
|
+
this.arcs = [];
|
|
167
|
+
this.genes = [];
|
|
168
|
+
this.turns = [];
|
|
169
|
+
this.files = [];
|
|
170
|
+
this.children = [];
|
|
171
|
+
this.counters = emptyCounters();
|
|
172
|
+
this.sigCounts = new Map();
|
|
173
|
+
this.reads = new Map();
|
|
174
|
+
this.feedback = new Map();
|
|
175
|
+
this.runResults = new Map();
|
|
176
|
+
this.shown = new Set();
|
|
177
|
+
this.pending = new Map();
|
|
178
|
+
this.openFailures = [];
|
|
179
|
+
this.outcome = 'running';
|
|
180
|
+
this.todoAllDone = false;
|
|
181
|
+
this.lastVerifyOk = false;
|
|
182
|
+
this.turnIndex = 0;
|
|
183
|
+
}
|
|
184
|
+
ensureRun() {
|
|
185
|
+
if (!this.active)
|
|
186
|
+
this.beginRun();
|
|
187
|
+
}
|
|
188
|
+
// ---- cleaning ------------------------------------------------------------
|
|
189
|
+
/** Hint-stripped, secret-scrubbed, clipped. The only way text enters the ledger. */
|
|
190
|
+
clean(text, max) {
|
|
191
|
+
const stripped = stripGeneHints(typeof text === 'string' ? text : '');
|
|
192
|
+
const redacted = redactSecrets(stripped, this.deps.knownSecrets?.() ?? []).text;
|
|
193
|
+
return clipPoints(redacted.replace(/\s+$/g, ''), max);
|
|
194
|
+
}
|
|
195
|
+
// ---- inputs ------------------------------------------------------------------
|
|
196
|
+
addUser(text) {
|
|
197
|
+
this.ensureRun();
|
|
198
|
+
const clean = this.clean(text, LEDGER_LIMITS.userText);
|
|
199
|
+
if (clean.length === 0)
|
|
200
|
+
return;
|
|
201
|
+
const correction = detectUserCorrection(clean);
|
|
202
|
+
const statedFact = detectStatedFact(clean);
|
|
203
|
+
const afterAction = this.sessionActed;
|
|
204
|
+
if (this.goal === '')
|
|
205
|
+
this.goal = clipPoints(clean, LEDGER_LIMITS.goalMax);
|
|
206
|
+
this.counters.userCorrections += correction ? 1 : 0;
|
|
207
|
+
this.counters.correctionAfterAction += correction && afterAction ? 1 : 0;
|
|
208
|
+
this.counters.statedFacts += statedFact ? 1 : 0;
|
|
209
|
+
this.push(this.users, { text: clean, correction, statedFact, afterAction }, correction || statedFact);
|
|
210
|
+
this.trimCount(this.users, LEDGER_LIMITS.usersMax);
|
|
211
|
+
}
|
|
212
|
+
startCall(id, tool, args) {
|
|
213
|
+
this.ensureRun();
|
|
214
|
+
this.sessionActed = true;
|
|
215
|
+
const picked = pickArg(args);
|
|
216
|
+
const arg = picked === undefined ? undefined : this.clean(picked, LEDGER_LIMITS.argText);
|
|
217
|
+
this.pending.set(id, { tool, ...(arg !== undefined ? { arg } : {}), turn: this.turnIndex + 1 });
|
|
218
|
+
if (isFileTool(tool) && arg !== undefined && !this.files.includes(arg)) {
|
|
219
|
+
this.files.push(clipPoints(arg, LEDGER_LIMITS.fileText));
|
|
220
|
+
if (this.files.length > LEDGER_LIMITS.filesMax)
|
|
221
|
+
this.files.shift();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
endCall(args) {
|
|
225
|
+
this.ensureRun();
|
|
226
|
+
const started = this.pending.get(args.id);
|
|
227
|
+
this.pending.delete(args.id);
|
|
228
|
+
const ok = !args.isError;
|
|
229
|
+
const arg = started?.arg;
|
|
230
|
+
this.counters.toolCalls += 1;
|
|
231
|
+
this.counters.toolErrors += ok ? 0 : 1;
|
|
232
|
+
if (isVerifyCall(args.tool, arg))
|
|
233
|
+
this.lastVerifyOk = ok;
|
|
234
|
+
const entry = {
|
|
235
|
+
id: args.id,
|
|
236
|
+
tool: args.tool,
|
|
237
|
+
...(arg !== undefined ? { arg } : {}),
|
|
238
|
+
ok,
|
|
239
|
+
ms: Math.max(0, Math.trunc(args.ms) || 0),
|
|
240
|
+
turn: started?.turn ?? this.turnIndex + 1,
|
|
241
|
+
};
|
|
242
|
+
if (ok) {
|
|
243
|
+
// A success that closed a recovery arc is itself evidence; keep it when the ledger is full.
|
|
244
|
+
const closed = this.recordRecovery(args.tool);
|
|
245
|
+
this.push(this.calls, entry, closed > 0);
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
const sig = errorSignature(args.tool, args.text);
|
|
249
|
+
this.sigCounts.set(sig, (this.sigCounts.get(sig) ?? 0) + 1);
|
|
250
|
+
const err = this.clean(args.text, LEDGER_LIMITS.errText);
|
|
251
|
+
const call = this.push(this.calls, { ...entry, sig, ...(err ? { err } : {}) }, true);
|
|
252
|
+
this.openFailures.push({ seq: call.seq, tool: args.tool, sig, callIndex: this.counters.toolCalls });
|
|
253
|
+
}
|
|
254
|
+
this.trimCount(this.calls, LEDGER_LIMITS.callsMax);
|
|
255
|
+
}
|
|
256
|
+
/** A success closes the open failures of the same tool that are still inside the window. */
|
|
257
|
+
recordRecovery(tool) {
|
|
258
|
+
const now = this.counters.toolCalls;
|
|
259
|
+
const live = this.openFailures.filter((f) => now - f.callIndex <= LEDGER_LIMITS.arcWindow);
|
|
260
|
+
const closing = live.filter((f) => f.tool === tool);
|
|
261
|
+
this.openFailures = live.filter((f) => f.tool !== tool);
|
|
262
|
+
for (const failure of closing) {
|
|
263
|
+
const between = this.calls.filter((c) => c.seq > failure.seq);
|
|
264
|
+
const arc = { sig: failure.sig, tool, gap: between.length, viaFix: between.some((c) => c.tool !== tool) };
|
|
265
|
+
this.push(this.arcs, arc, true);
|
|
266
|
+
this.counters.arcs += 1;
|
|
267
|
+
}
|
|
268
|
+
while (this.arcs.length > LEDGER_LIMITS.arcsMax)
|
|
269
|
+
this.arcs.shift();
|
|
270
|
+
return closing.length;
|
|
271
|
+
}
|
|
272
|
+
endTurn(tail) {
|
|
273
|
+
this.ensureRun();
|
|
274
|
+
this.turnIndex += 1;
|
|
275
|
+
this.counters.turns += 1;
|
|
276
|
+
const text = this.clean(tail, LEDGER_LIMITS.turnTail);
|
|
277
|
+
this.push(this.turns, { index: this.turnIndex, tail: text }, false);
|
|
278
|
+
this.trimCount(this.turns, LEDGER_LIMITS.turnsMax);
|
|
279
|
+
}
|
|
280
|
+
addGeneEvent(event) {
|
|
281
|
+
this.ensureRun();
|
|
282
|
+
switch (event.kind) {
|
|
283
|
+
case 'shown':
|
|
284
|
+
for (const id of event.geneIds)
|
|
285
|
+
this.shown.add(id);
|
|
286
|
+
return;
|
|
287
|
+
case 'unfamiliar_recovery':
|
|
288
|
+
this.counters.unfamiliarRecoveries += 1;
|
|
289
|
+
return;
|
|
290
|
+
case 'search':
|
|
291
|
+
this.pushGene({ kind: 'search', query: this.clean(event.query, 120) }, false);
|
|
292
|
+
return;
|
|
293
|
+
case 'read':
|
|
294
|
+
if (!this.reads.has(event.geneId)) {
|
|
295
|
+
this.reads.set(event.geneId, { seq: this.seq + 1, tier: event.tier, ...versionOf(event) });
|
|
296
|
+
}
|
|
297
|
+
this.pushGene({ kind: 'read', geneId: event.geneId, tier: event.tier, ...versionOf(event) }, true);
|
|
298
|
+
return;
|
|
299
|
+
case 'feedback':
|
|
300
|
+
this.noteFeedback(event);
|
|
301
|
+
return;
|
|
302
|
+
case 'run_result':
|
|
303
|
+
this.runResults.set(event.geneId, event.ok);
|
|
304
|
+
this.pushGene({ kind: 'run_result', geneId: event.geneId, ok: event.ok }, true);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
noteFeedback(event) {
|
|
308
|
+
this.feedback.set(event.geneId, event.verdict);
|
|
309
|
+
const flagged = event.verdict === 'misleading' || event.verdict === 'outdated';
|
|
310
|
+
this.counters.flaggedFeedback += flagged ? 1 : 0;
|
|
311
|
+
this.pushGene({ kind: 'feedback', geneId: event.geneId, verdict: event.verdict }, true);
|
|
312
|
+
}
|
|
313
|
+
pushGene(entry, signalled) {
|
|
314
|
+
this.push(this.genes, entry, signalled);
|
|
315
|
+
this.trimCount(this.genes, LEDGER_LIMITS.genesMax);
|
|
316
|
+
}
|
|
317
|
+
addChild(summary) {
|
|
318
|
+
this.ensureRun();
|
|
319
|
+
this.children.push(this.clean(summary, LEDGER_LIMITS.childText));
|
|
320
|
+
if (this.children.length > LEDGER_LIMITS.childrenMax)
|
|
321
|
+
this.children.shift();
|
|
322
|
+
}
|
|
323
|
+
noteRetry() {
|
|
324
|
+
this.ensureRun();
|
|
325
|
+
this.counters.retries += 1;
|
|
326
|
+
}
|
|
327
|
+
// ---- bounds ------------------------------------------------------------------
|
|
328
|
+
push(list, body, signalled) {
|
|
329
|
+
this.seq += 1;
|
|
330
|
+
const entry = { ...body, seq: this.seq, size: 0, signalled };
|
|
331
|
+
entry.size = approxSize(entry);
|
|
332
|
+
list.push(entry);
|
|
333
|
+
this.enforceTotal();
|
|
334
|
+
return entry;
|
|
335
|
+
}
|
|
336
|
+
trimCount(list, max) {
|
|
337
|
+
while (list.length > max)
|
|
338
|
+
list.shift();
|
|
339
|
+
}
|
|
340
|
+
totalBytes() {
|
|
341
|
+
const all = [this.users, this.calls, this.arcs, this.genes, this.turns];
|
|
342
|
+
return all.reduce((sum, list) => sum + list.reduce((s, e) => s + e.size, 0), 0);
|
|
343
|
+
}
|
|
344
|
+
/** Over 24 KB: unsignalled turns, then calls, users, genes go first; then the oldest signalled. */
|
|
345
|
+
enforceTotal() {
|
|
346
|
+
const order = [this.turns, this.calls, this.users, this.genes];
|
|
347
|
+
while (this.totalBytes() > this.maxBytes) {
|
|
348
|
+
const unsignalled = order.find((list) => list.some((e) => !e.signalled));
|
|
349
|
+
if (unsignalled) {
|
|
350
|
+
unsignalled.splice(unsignalled.findIndex((e) => !e.signalled), 1);
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
const lists = [...order, this.arcs].filter((l) => l.length > 0);
|
|
354
|
+
if (lists.length === 0)
|
|
355
|
+
return;
|
|
356
|
+
const oldest = lists.reduce((a, b) => (a[0].seq <= b[0].seq ? a : b));
|
|
357
|
+
oldest.shift();
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
// ---- reading -----------------------------------------------------------------
|
|
361
|
+
snapshot() {
|
|
362
|
+
return {
|
|
363
|
+
run: this.runSerial,
|
|
364
|
+
active: this.active,
|
|
365
|
+
goal: this.goal,
|
|
366
|
+
...(this.prior ? { prior: this.prior } : {}),
|
|
367
|
+
users: this.users,
|
|
368
|
+
calls: this.calls,
|
|
369
|
+
arcs: this.arcs,
|
|
370
|
+
genes: this.genes,
|
|
371
|
+
turns: this.turns,
|
|
372
|
+
files: this.files,
|
|
373
|
+
children: this.children,
|
|
374
|
+
counters: { ...this.counters },
|
|
375
|
+
sigCounts: new Map(this.sigCounts),
|
|
376
|
+
reads: new Map(this.reads),
|
|
377
|
+
feedback: new Map(this.feedback),
|
|
378
|
+
runResults: new Map(this.runResults),
|
|
379
|
+
shown: new Set(this.shown),
|
|
380
|
+
outcome: this.outcome,
|
|
381
|
+
todoAllDone: this.todoAllDone,
|
|
382
|
+
lastVerifyOk: this.lastVerifyOk,
|
|
383
|
+
bytes: this.totalBytes(),
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
watermark() {
|
|
387
|
+
return {
|
|
388
|
+
run: this.runSerial,
|
|
389
|
+
counters: { ...this.counters },
|
|
390
|
+
sigCounts: new Map(this.sigCounts),
|
|
391
|
+
readIds: new Set(this.reads.keys()),
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* From the reviewer's normalised answer to queue-ready proposals and usage events (gene-bank
|
|
3
|
+
* spec 10.1.4, I-9, I-16).
|
|
4
|
+
*
|
|
5
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
6
|
+
*
|
|
7
|
+
* `normalizeLearningOutput` (pure, in the model) decides what a reply MEANS; this file decides
|
|
8
|
+
* what the ENGINE does with it, using facts only the engine has: the ledger (did the user really
|
|
9
|
+
* say that? did anything fail after the gene was read?), the session (which scope key?) and the
|
|
10
|
+
* library (what version is the target at?). Everything here is a pure function of its inputs so
|
|
11
|
+
* the learner can stay a thin state machine.
|
|
12
|
+
*
|
|
13
|
+
* TWO DISCIPLINES LIVE HERE.
|
|
14
|
+
*
|
|
15
|
+
* I-9, "a run's failure is not every gene's fault". The model is told to judge a gene only by
|
|
16
|
+
* what followed its read, but a prompt is a request, not a guarantee. `adjustAttributions` makes
|
|
17
|
+
* it structural: a `harmed` verdict survives only if something actually went wrong AFTER the
|
|
18
|
+
* gene was read (a failed call, or the user correcting the agent). A gene whose window was all
|
|
19
|
+
* successes cannot be marked harmful because the task failed somewhere else. The reverse
|
|
20
|
+
* double-count is closed too: a gene the agent already rated through `gene_feedback`, or whose
|
|
21
|
+
* `gene_run` result is known, is attributed deterministically at that moment and is not scored a
|
|
22
|
+
* second time by a model.
|
|
23
|
+
*
|
|
24
|
+
* Evidence trust. The model quotes the user; the engine checks the quote. Only a quote found in
|
|
25
|
+
* the ledger's user messages becomes `trust: 'user'` (and is replaced by the ledger's own text,
|
|
26
|
+
* so what is stored is what was said, not what the model remembers). A quote that cannot be
|
|
27
|
+
* found is `untrusted` and cannot, on its own, make a gene trustworthy (11.6).
|
|
28
|
+
*/
|
|
29
|
+
import type { GeneEvidenceInput, GeneProposal, GeneProposalAttribution, GeneScope, GeneScopeKind, GeneUsageEvent } from '@aragon-agent/tui/model/gene/types';
|
|
30
|
+
import { type NormalizedProposalDraft } from '@aragon-agent/tui/model/gene/proposal';
|
|
31
|
+
import type { LearningSignal } from '@aragon-agent/tui/model/gene/signals';
|
|
32
|
+
import type { LedgerSnapshot } from './ledger.js';
|
|
33
|
+
export interface SessionFacts {
|
|
34
|
+
sessionId: string;
|
|
35
|
+
runId: string;
|
|
36
|
+
projectKey?: string;
|
|
37
|
+
agent?: string;
|
|
38
|
+
taskLabel?: string;
|
|
39
|
+
}
|
|
40
|
+
export interface GeneBasis {
|
|
41
|
+
version: number | null;
|
|
42
|
+
hash?: string;
|
|
43
|
+
}
|
|
44
|
+
export interface ProposalContext {
|
|
45
|
+
now: number;
|
|
46
|
+
random: (n: number) => number;
|
|
47
|
+
session: SessionFacts;
|
|
48
|
+
snapshot: LedgerSnapshot;
|
|
49
|
+
signals: readonly LearningSignal[];
|
|
50
|
+
attributions: readonly GeneProposalAttribution[];
|
|
51
|
+
basisOf: (geneId: string) => GeneBasis | undefined;
|
|
52
|
+
knownSecrets: readonly string[];
|
|
53
|
+
}
|
|
54
|
+
/** The concrete scope for a scope KIND, or null when the session cannot name one. */
|
|
55
|
+
export declare function resolveScope(kind: GeneScopeKind, session: SessionFacts): GeneScope | null;
|
|
56
|
+
/** Check quotes against the ledger, scrub secrets, clip. */
|
|
57
|
+
export declare function verifyEvidence(list: readonly GeneEvidenceInput[], snapshot: LedgerSnapshot, knownSecrets: readonly string[]): GeneEvidenceInput[];
|
|
58
|
+
/** Did anything go wrong AFTER the gene was read? Only then can the gene be blamed. */
|
|
59
|
+
export declare function troubleAfterRead(snapshot: LedgerSnapshot, geneId: string): boolean;
|
|
60
|
+
export interface AdjustedAttributions {
|
|
61
|
+
kept: GeneProposalAttribution[];
|
|
62
|
+
/** `harmed` verdicts with no trouble in the gene's window, reduced to `unknown`. */
|
|
63
|
+
downgraded: number;
|
|
64
|
+
/** Verdicts dropped because the agent already rated the gene itself. */
|
|
65
|
+
alreadyAttributed: number;
|
|
66
|
+
}
|
|
67
|
+
export declare function adjustAttributions(attributions: readonly GeneProposalAttribution[], snapshot: LedgerSnapshot): AdjustedAttributions;
|
|
68
|
+
export interface OutcomeContext {
|
|
69
|
+
now: number;
|
|
70
|
+
runId: string;
|
|
71
|
+
snapshot: LedgerSnapshot;
|
|
72
|
+
scopeOf: (geneId: string) => GeneScope | undefined;
|
|
73
|
+
versionOf?: (geneId: string) => number | undefined;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* `outcome` usage events for the attributions that count (10.1.4): adopted, and `helped` /
|
|
77
|
+
* `harmed` / `neutral`. `unknown` is not an event at all (I-9). A gene whose scope or version
|
|
78
|
+
* cannot be determined is skipped: an event the fold cannot route would be dropped and counted
|
|
79
|
+
* as a defect there.
|
|
80
|
+
*/
|
|
81
|
+
export declare function outcomeEvents(attributions: readonly GeneProposalAttribution[], ctx: OutcomeContext): GeneUsageEvent[];
|
|
82
|
+
/** A queue-ready proposal, or null when the draft cannot be anchored (no scope, target vanished). */
|
|
83
|
+
export declare function buildProposal(draft: NormalizedProposalDraft, ctx: ProposalContext): GeneProposal | null;
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { escapeGeneText, unescapeGeneEntities } from '@aragon-agent/tui/model/gene/escape';
|
|
2
|
+
import { isValidScope, mintProposalId, scopeId } from '@aragon-agent/tui/model/gene/ids';
|
|
3
|
+
import { GENE_LIMITS, GENE_SCORE } from '@aragon-agent/tui/model/gene/limits';
|
|
4
|
+
import { dedupeKeyOf, priorityOf } from '@aragon-agent/tui/model/gene/proposal';
|
|
5
|
+
import { fingerprintOf } from '@aragon-agent/tui/model/gene/validate-base';
|
|
6
|
+
import { redactSecrets } from '../secrets.js';
|
|
7
|
+
function clipPoints(text, max) {
|
|
8
|
+
const points = Array.from(text);
|
|
9
|
+
return points.length <= max ? text : points.slice(0, max).join('');
|
|
10
|
+
}
|
|
11
|
+
/** The concrete scope for a scope KIND, or null when the session cannot name one. */
|
|
12
|
+
export function resolveScope(kind, session) {
|
|
13
|
+
let scope = null;
|
|
14
|
+
if (kind === 'user')
|
|
15
|
+
scope = { kind: 'user' };
|
|
16
|
+
else if (kind === 'project')
|
|
17
|
+
scope = session.projectKey ? { kind: 'project', key: session.projectKey } : null;
|
|
18
|
+
else if (kind === 'task')
|
|
19
|
+
scope = { kind: 'task', key: session.sessionId };
|
|
20
|
+
return scope !== null && isValidScope(scope) ? scope : null;
|
|
21
|
+
}
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Evidence
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
/** Same normalisation on both sides, so the model's escaped view of a message still matches. */
|
|
26
|
+
function quoteKey(text) {
|
|
27
|
+
return escapeGeneText(text, 'line');
|
|
28
|
+
}
|
|
29
|
+
/** Check quotes against the ledger, scrub secrets, clip. */
|
|
30
|
+
export function verifyEvidence(list, snapshot, knownSecrets) {
|
|
31
|
+
const users = snapshot.users.map((u) => ({ key: quoteKey(u.text), raw: u.text }));
|
|
32
|
+
return list.slice(0, GENE_LIMITS.proposalEvidenceMax).map((entry) => {
|
|
33
|
+
let text = entry.text;
|
|
34
|
+
let trust = entry.trust;
|
|
35
|
+
if (entry.kind === 'user_statement') {
|
|
36
|
+
const keys = [quoteKey(entry.text), quoteKey(unescapeGeneEntities(entry.text))].filter((k) => k.length > 0);
|
|
37
|
+
const hit = users.find((u) => keys.some((key) => u.key.includes(key)));
|
|
38
|
+
text = hit ? hit.raw : text;
|
|
39
|
+
trust = hit ? 'user' : 'untrusted';
|
|
40
|
+
}
|
|
41
|
+
else if (trust === undefined) {
|
|
42
|
+
trust = 'agent';
|
|
43
|
+
}
|
|
44
|
+
const scrubbed = redactSecrets(text, knownSecrets).text;
|
|
45
|
+
return { ...entry, text: clipPoints(scrubbed, GENE_LIMITS.proposalEvidenceTextMax), trust };
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
// Attributions (I-9)
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
/** Did anything go wrong AFTER the gene was read? Only then can the gene be blamed. */
|
|
52
|
+
export function troubleAfterRead(snapshot, geneId) {
|
|
53
|
+
const read = snapshot.reads.get(geneId);
|
|
54
|
+
if (!read)
|
|
55
|
+
return false;
|
|
56
|
+
const failed = snapshot.calls.some((c) => c.seq > read.seq && !c.ok);
|
|
57
|
+
const corrected = snapshot.users.some((u) => u.seq > read.seq && u.correction);
|
|
58
|
+
return failed || corrected;
|
|
59
|
+
}
|
|
60
|
+
export function adjustAttributions(attributions, snapshot) {
|
|
61
|
+
const out = { kept: [], downgraded: 0, alreadyAttributed: 0 };
|
|
62
|
+
for (const a of attributions) {
|
|
63
|
+
if (snapshot.feedback.has(a.geneId) || snapshot.runResults.has(a.geneId)) {
|
|
64
|
+
out.alreadyAttributed += 1;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (a.effect === 'harmed' && !troubleAfterRead(snapshot, a.geneId)) {
|
|
68
|
+
out.downgraded += 1;
|
|
69
|
+
out.kept.push({ ...a, effect: 'unknown' });
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
out.kept.push(a);
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* `outcome` usage events for the attributions that count (10.1.4): adopted, and `helped` /
|
|
78
|
+
* `harmed` / `neutral`. `unknown` is not an event at all (I-9). A gene whose scope or version
|
|
79
|
+
* cannot be determined is skipped: an event the fold cannot route would be dropped and counted
|
|
80
|
+
* as a defect there.
|
|
81
|
+
*/
|
|
82
|
+
export function outcomeEvents(attributions, ctx) {
|
|
83
|
+
const events = [];
|
|
84
|
+
for (const a of attributions) {
|
|
85
|
+
if (a.effect === 'unknown' || !a.adopted)
|
|
86
|
+
continue;
|
|
87
|
+
const scope = ctx.scopeOf(a.geneId);
|
|
88
|
+
const version = ctx.snapshot.reads.get(a.geneId)?.version ?? ctx.versionOf?.(a.geneId);
|
|
89
|
+
if (scope === undefined || version === undefined)
|
|
90
|
+
continue;
|
|
91
|
+
events.push({
|
|
92
|
+
sc: scopeId(scope),
|
|
93
|
+
t: ctx.now,
|
|
94
|
+
k: 'outcome',
|
|
95
|
+
g: a.geneId,
|
|
96
|
+
v: version,
|
|
97
|
+
run: ctx.runId,
|
|
98
|
+
e: a.effect,
|
|
99
|
+
w: GENE_SCORE.adoptionWeights.reviewer,
|
|
100
|
+
by: 'reviewer',
|
|
101
|
+
...(a.ev ? { ev: a.ev } : {}),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return events;
|
|
105
|
+
}
|
|
106
|
+
// ---------------------------------------------------------------------------
|
|
107
|
+
// Proposals
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
function harmFlagged(draft, ctx) {
|
|
110
|
+
if (draft.target === undefined)
|
|
111
|
+
return false;
|
|
112
|
+
const verdict = ctx.snapshot.feedback.get(draft.target);
|
|
113
|
+
const flagged = verdict === 'misleading' || verdict === 'outdated';
|
|
114
|
+
return flagged || ctx.attributions.some((a) => a.geneId === draft.target && a.effect === 'harmed');
|
|
115
|
+
}
|
|
116
|
+
function statesPreference(draft, evidence) {
|
|
117
|
+
const payload = draft.draft?.payload;
|
|
118
|
+
const fromUser = evidence.some((e) => e.kind === 'user_statement' && e.trust === 'user');
|
|
119
|
+
return payload?.kind === 'preference' && fromUser;
|
|
120
|
+
}
|
|
121
|
+
function fingerprintOfDraft(draft, scopeKind) {
|
|
122
|
+
if (draft.kind !== 'create' || draft.type === undefined)
|
|
123
|
+
return undefined;
|
|
124
|
+
return fingerprintOf(draft.type, scopeKind, {
|
|
125
|
+
name: draft.title,
|
|
126
|
+
purpose: draft.draft?.purpose ?? '',
|
|
127
|
+
trigger: draft.draft?.trigger ?? '',
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/** Queue class (10.5.2): a user correction, a harmed target and a revoke each move it up. */
|
|
131
|
+
function priorityFor(draft, ctx, evidence) {
|
|
132
|
+
return priorityOf({
|
|
133
|
+
kind: draft.kind,
|
|
134
|
+
fromUserCorrection: ctx.signals.some((s) => s.id === 'L1'),
|
|
135
|
+
harmedFlag: harmFlagged(draft, ctx),
|
|
136
|
+
userStatedPreference: statesPreference(draft, evidence),
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/** Two proposals with the same key are the same request and fold into one (10.5.3). */
|
|
140
|
+
function dedupeKeyFor(draft, scope, basis) {
|
|
141
|
+
const fingerprint = fingerprintOfDraft(draft, draft.scope);
|
|
142
|
+
return dedupeKeyOf({
|
|
143
|
+
kind: draft.kind,
|
|
144
|
+
scope,
|
|
145
|
+
...(draft.target ? { targetId: draft.target } : {}),
|
|
146
|
+
...(fingerprint ? { fingerprint } : {}),
|
|
147
|
+
...(basis ? { basisVersion: basis.version } : {}),
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/** What the proposal is aimed at: the target with the basis it was based on (I-10), or a merge. */
|
|
151
|
+
function targetFields(draft, basis) {
|
|
152
|
+
if (!draft.target || !basis)
|
|
153
|
+
return {};
|
|
154
|
+
const hash = basis.hash ? { basisHash: basis.hash } : {};
|
|
155
|
+
const target = { id: draft.target, basisVersion: basis.version, ...hash };
|
|
156
|
+
if (draft.kind !== 'merge')
|
|
157
|
+
return { target };
|
|
158
|
+
return { target, mergeInto: draft.target, mergeFrom: draft.mergeFrom ?? [] };
|
|
159
|
+
}
|
|
160
|
+
function originOf(ctx) {
|
|
161
|
+
const goal = ctx.snapshot.goal === '' ? '(unknown)' : ctx.snapshot.goal;
|
|
162
|
+
return {
|
|
163
|
+
source: 'reviewer',
|
|
164
|
+
sessionId: ctx.session.sessionId,
|
|
165
|
+
runId: ctx.session.runId,
|
|
166
|
+
...(ctx.session.projectKey ? { projectKey: ctx.session.projectKey } : {}),
|
|
167
|
+
...(ctx.session.agent ? { agent: ctx.session.agent } : {}),
|
|
168
|
+
...(ctx.session.taskLabel ? { taskLabel: ctx.session.taskLabel } : {}),
|
|
169
|
+
goal: clipPoints(goal, GENE_LIMITS.proposalGoalMax),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/** A create names its gene; the model's own draft fields ride along. */
|
|
173
|
+
function draftFields(draft) {
|
|
174
|
+
if (draft.kind === 'create')
|
|
175
|
+
return { draft: { ...draft.draft, name: draft.title } };
|
|
176
|
+
return draft.draft ? { draft: draft.draft } : {};
|
|
177
|
+
}
|
|
178
|
+
/** A queue-ready proposal, or null when the draft cannot be anchored (no scope, target vanished). */
|
|
179
|
+
export function buildProposal(draft, ctx) {
|
|
180
|
+
const scope = resolveScope(draft.scope, ctx.session);
|
|
181
|
+
if (scope === null)
|
|
182
|
+
return null;
|
|
183
|
+
const basis = draft.target === undefined ? undefined : ctx.basisOf(draft.target);
|
|
184
|
+
if (draft.target !== undefined && basis === undefined)
|
|
185
|
+
return null;
|
|
186
|
+
const evidence = verifyEvidence(draft.evidence, ctx.snapshot, ctx.knownSecrets);
|
|
187
|
+
const informative = ctx.attributions.filter((a) => a.effect !== 'unknown');
|
|
188
|
+
return {
|
|
189
|
+
schema: 1,
|
|
190
|
+
id: mintProposalId(ctx.random),
|
|
191
|
+
createdAt: ctx.now,
|
|
192
|
+
kind: draft.kind,
|
|
193
|
+
...(draft.type ? { type: draft.type } : {}),
|
|
194
|
+
scope,
|
|
195
|
+
...targetFields(draft, basis),
|
|
196
|
+
origin: originOf(ctx),
|
|
197
|
+
why: draft.why,
|
|
198
|
+
expectedBenefit: draft.expectedBenefit,
|
|
199
|
+
...(draft.applicability ? { applicability: draft.applicability } : {}),
|
|
200
|
+
...draftFields(draft),
|
|
201
|
+
evidence,
|
|
202
|
+
...(informative.length > 0 ? { attributions: informative } : {}),
|
|
203
|
+
priority: priorityFor(draft, ctx, evidence),
|
|
204
|
+
dedupeKey: dedupeKeyFor(draft, scope, basis),
|
|
205
|
+
merged: 0,
|
|
206
|
+
};
|
|
207
|
+
}
|