@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,764 @@
|
|
|
1
|
+
import { clampGeneConfig, } from '@aragon-agent/tui/model/config/schema';
|
|
2
|
+
import { emptyGeneSessionView } from '@aragon-agent/tui/model/gene/activity';
|
|
3
|
+
import { planGeneContext } from '@aragon-agent/tui/model/gene/context';
|
|
4
|
+
import { geneBriefLine } from '@aragon-agent/tui/model/gene/brief';
|
|
5
|
+
import { isValidScopeKey } from '@aragon-agent/tui/model/gene/ids';
|
|
6
|
+
import { getConfigPath } from '../config/app-paths.js';
|
|
7
|
+
import { getLogger } from '../logging/logger.js';
|
|
8
|
+
import { resolveProjectKey } from '../memory/paths.js';
|
|
9
|
+
import { describeShell } from '../agent/system-prompt.js';
|
|
10
|
+
import { createCuratorModelPort } from './curator/agent.js';
|
|
11
|
+
import { GeneCurator } from './curator/curator.js';
|
|
12
|
+
import { createCuratorLibrary } from './curator/library.js';
|
|
13
|
+
import { createPersistedGeneConfigReader } from './curator/switches.js';
|
|
14
|
+
import { captureEnvFingerprint } from './env.js';
|
|
15
|
+
import { CostMeter } from './eval-recorder.js';
|
|
16
|
+
import { GeneGc } from './gc.js';
|
|
17
|
+
import { GeneHintEngine } from './hints.js';
|
|
18
|
+
import { GeneActivityHub } from './hub.js';
|
|
19
|
+
import { GeneUiHints } from './ui-hints.js';
|
|
20
|
+
import { StoreIndexWriter } from './index-writer.js';
|
|
21
|
+
import { GeneLearner } from './learn/learner.js';
|
|
22
|
+
import { evalFactsFromLedger } from './learn/signals-port.js';
|
|
23
|
+
import { GeneMaintenanceRunner } from './maintenance-runner.js';
|
|
24
|
+
import { LEARN_ALLOWED, ProjectLearningPolicy } from './policy.js';
|
|
25
|
+
import { projectMetaFile } from './paths.js';
|
|
26
|
+
import { readJson } from './fs.js';
|
|
27
|
+
import { createGeneManager } from './manager.js';
|
|
28
|
+
import { getGeneLibrary, limitsFromConfig } from './manager-library.js';
|
|
29
|
+
import { GeneService } from './service.js';
|
|
30
|
+
import { isHoldoutRun, queryTermsOf } from './service-context.js';
|
|
31
|
+
import { runSessionStartChecks } from './session-start.js';
|
|
32
|
+
import { TombstoneStore } from './tombstones.js';
|
|
33
|
+
import { createGeneTools, shellKindOfDescribed } from './tools.js';
|
|
34
|
+
import { GeneUsageRecorder } from './usage.js';
|
|
35
|
+
import { GeneEventFeed } from './wiring-events.js';
|
|
36
|
+
import { createFailFastRegistry, createLearnComplete, resolveGeneModel, } from './wiring-models.js';
|
|
37
|
+
/** How long a turn waits for the context zone to be planned before it goes on without one. */
|
|
38
|
+
const PLAN_WAIT_MS = 150;
|
|
39
|
+
/** How long the evaluation row waits for the run's learning call so its cost can be included. */
|
|
40
|
+
const EVAL_SETTLE_MS = 30_000;
|
|
41
|
+
const ENV_TTL_MS = 60_000;
|
|
42
|
+
const SUBAGENT_TASK_CHARS = 1500;
|
|
43
|
+
function sleepUnref(ms) {
|
|
44
|
+
let handle;
|
|
45
|
+
const promise = new Promise((resolve) => {
|
|
46
|
+
handle = setTimeout(resolve, ms);
|
|
47
|
+
handle.unref?.();
|
|
48
|
+
});
|
|
49
|
+
return { promise, cancel: () => clearTimeout(handle) };
|
|
50
|
+
}
|
|
51
|
+
/** Resolve when `job` settles or after `ms`, whichever is first. Never rejects. */
|
|
52
|
+
async function settleWithin(job, ms) {
|
|
53
|
+
const timer = sleepUnref(ms);
|
|
54
|
+
try {
|
|
55
|
+
await Promise.race([job.catch(() => undefined), timer.promise]);
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
timer.cancel();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const EXPLAIN_CARRY_MAX = 200;
|
|
62
|
+
export class GeneWiring {
|
|
63
|
+
deps;
|
|
64
|
+
service;
|
|
65
|
+
hints;
|
|
66
|
+
hub;
|
|
67
|
+
lib;
|
|
68
|
+
/** The per-project "do not learn here" switch (I3-5); the files on disk are the truth. */
|
|
69
|
+
policy;
|
|
70
|
+
/** The one recorder of this process: the service, the learner and the hint engine share it. */
|
|
71
|
+
usage;
|
|
72
|
+
learner;
|
|
73
|
+
feed;
|
|
74
|
+
curator;
|
|
75
|
+
runner;
|
|
76
|
+
registry = createFailFastRegistry();
|
|
77
|
+
managerHandle = null;
|
|
78
|
+
cache = null;
|
|
79
|
+
projectMemo = null;
|
|
80
|
+
envMemo = null;
|
|
81
|
+
useOverride = null;
|
|
82
|
+
learnOverride = null;
|
|
83
|
+
runCounter = 0;
|
|
84
|
+
runId;
|
|
85
|
+
runStartedAt = Date.now();
|
|
86
|
+
meter = new CostMeter();
|
|
87
|
+
/** Explanation spend since the last run began; folded into the next run's cost row. */
|
|
88
|
+
explainCarry = [];
|
|
89
|
+
lastTask = '';
|
|
90
|
+
lastLearnerState = '';
|
|
91
|
+
started = false;
|
|
92
|
+
disposed = false;
|
|
93
|
+
constructor(deps) {
|
|
94
|
+
this.deps = deps;
|
|
95
|
+
this.runId = `${deps.sessionId}-r0`;
|
|
96
|
+
this.lib = getGeneLibrary({
|
|
97
|
+
...(deps.root === undefined ? {} : { root: deps.root }),
|
|
98
|
+
teamPath: this.effective().teamPath,
|
|
99
|
+
kind: deps.kind,
|
|
100
|
+
});
|
|
101
|
+
this.policy = new ProjectLearningPolicy({ ctx: this.lib.ctx });
|
|
102
|
+
this.lib.tuning.limits = () => limitsFromConfig(this.effective());
|
|
103
|
+
this.lib.tuning.knownSecrets = deps.knownSecrets;
|
|
104
|
+
this.usage = this.usageSink();
|
|
105
|
+
this.service = this.buildService();
|
|
106
|
+
this.hints = new GeneHintEngine({
|
|
107
|
+
service: this.service,
|
|
108
|
+
actor: () => this.actor(),
|
|
109
|
+
active: () => {
|
|
110
|
+
const cfg = this.effective();
|
|
111
|
+
return cfg.enabled && cfg.use && cfg.hints;
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
this.learner = this.buildLearner();
|
|
115
|
+
this.feed = new GeneEventFeed({
|
|
116
|
+
learner: this.learner,
|
|
117
|
+
meter: () => this.meter,
|
|
118
|
+
onRunEnd: (outcome) => this.onRunEnd(outcome),
|
|
119
|
+
});
|
|
120
|
+
this.curator = this.isBackground() ? this.buildCurator() : undefined;
|
|
121
|
+
this.runner = this.curator === undefined ? undefined : this.buildRunner(this.curator);
|
|
122
|
+
this.hub = this.buildHub();
|
|
123
|
+
this.wireNotifications();
|
|
124
|
+
}
|
|
125
|
+
// ===========================================================================
|
|
126
|
+
// Switches
|
|
127
|
+
// ===========================================================================
|
|
128
|
+
/** The live config with the session overrides laid over `use` / `autoLearn`. */
|
|
129
|
+
effective() {
|
|
130
|
+
const raw = this.deps.getConfig().gene;
|
|
131
|
+
const cached = this.cache;
|
|
132
|
+
if (cached !== null &&
|
|
133
|
+
cached.raw === raw &&
|
|
134
|
+
cached.use === this.useOverride &&
|
|
135
|
+
cached.learn === this.learnOverride) {
|
|
136
|
+
return cached.value;
|
|
137
|
+
}
|
|
138
|
+
const base = clampGeneConfig(raw);
|
|
139
|
+
const value = {
|
|
140
|
+
...base,
|
|
141
|
+
...(this.useOverride === null ? {} : { use: this.useOverride }),
|
|
142
|
+
...(this.learnOverride === null ? {} : { autoLearn: this.learnOverride }),
|
|
143
|
+
};
|
|
144
|
+
this.cache = { raw, use: this.useOverride, learn: this.learnOverride, value };
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
/** Curator and maintenance run only where a person is at the keyboard (9.8). */
|
|
148
|
+
isBackground() {
|
|
149
|
+
return !this.deps.headless && !this.deps.printMode && this.deps.kind !== 'exec';
|
|
150
|
+
}
|
|
151
|
+
setUseOverride(value) {
|
|
152
|
+
this.useOverride = value;
|
|
153
|
+
this.settingsTouched();
|
|
154
|
+
}
|
|
155
|
+
setAutoLearnOverride(value) {
|
|
156
|
+
this.learnOverride = value;
|
|
157
|
+
if (value === true)
|
|
158
|
+
this.learner.resume();
|
|
159
|
+
this.settingsTouched();
|
|
160
|
+
}
|
|
161
|
+
/** A `gene` config edit landed (Settings > Genes, `/gene on|off`). */
|
|
162
|
+
onConfigChanged() {
|
|
163
|
+
if (this.effective().autoLearn)
|
|
164
|
+
this.learner.resume();
|
|
165
|
+
this.settingsTouched();
|
|
166
|
+
}
|
|
167
|
+
settingsTouched() {
|
|
168
|
+
this.service.invalidateContext();
|
|
169
|
+
this.service.emitIfChanged();
|
|
170
|
+
this.hub.notify();
|
|
171
|
+
}
|
|
172
|
+
/** Pause or resume THIS machine's consumption of the queue; queueing is unaffected. */
|
|
173
|
+
curatorControl(action) {
|
|
174
|
+
if (this.curator === undefined)
|
|
175
|
+
return;
|
|
176
|
+
if (action === 'pause')
|
|
177
|
+
this.curator.pause('learn_off');
|
|
178
|
+
else
|
|
179
|
+
this.curator.resume();
|
|
180
|
+
}
|
|
181
|
+
// ===========================================================================
|
|
182
|
+
// Builders
|
|
183
|
+
// ===========================================================================
|
|
184
|
+
actor() {
|
|
185
|
+
return { runId: this.runId, role: 'main' };
|
|
186
|
+
}
|
|
187
|
+
project() {
|
|
188
|
+
const cwd = this.deps.getCwd();
|
|
189
|
+
if (this.projectMemo?.cwd === cwd)
|
|
190
|
+
return this.projectMemo.info;
|
|
191
|
+
let info = null;
|
|
192
|
+
try {
|
|
193
|
+
const identity = resolveProjectKey(cwd);
|
|
194
|
+
info = isValidScopeKey('project', identity.key) ? identity : null;
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
info = null;
|
|
198
|
+
}
|
|
199
|
+
this.projectMemo = { cwd, info };
|
|
200
|
+
return info;
|
|
201
|
+
}
|
|
202
|
+
/** The authority for this session's project: both layers, re-read if their stamp moved. */
|
|
203
|
+
async refreshProjectVerdict() {
|
|
204
|
+
const project = this.project();
|
|
205
|
+
if (project === null)
|
|
206
|
+
return LEARN_ALLOWED;
|
|
207
|
+
return this.policy.refresh(project.key, project.root);
|
|
208
|
+
}
|
|
209
|
+
/** The root `session-start` recorded for a project scope; null when unreadable (I3-5). */
|
|
210
|
+
async recordedProjectRoot(projectKey) {
|
|
211
|
+
const path = projectMetaFile({ kind: 'project', key: projectKey }, this.lib.ctx);
|
|
212
|
+
if (path === null)
|
|
213
|
+
return null;
|
|
214
|
+
const meta = await readJson(path);
|
|
215
|
+
return meta !== null && typeof meta.root === 'string' && meta.root.length > 0 ? meta.root : null;
|
|
216
|
+
}
|
|
217
|
+
env() {
|
|
218
|
+
const cwd = this.deps.getCwd();
|
|
219
|
+
const now = Date.now();
|
|
220
|
+
const held = this.envMemo;
|
|
221
|
+
if (held !== null && held.cwd === cwd && now >= held.at && now - held.at < ENV_TTL_MS) {
|
|
222
|
+
return held.value;
|
|
223
|
+
}
|
|
224
|
+
const value = captureEnvFingerprint(cwd);
|
|
225
|
+
this.envMemo = { at: now, cwd, value };
|
|
226
|
+
return value;
|
|
227
|
+
}
|
|
228
|
+
usageSink() {
|
|
229
|
+
const recorder = new GeneUsageRecorder({ sessionId: this.deps.sessionId, ctx: this.lib.ctx });
|
|
230
|
+
return {
|
|
231
|
+
record: (event) => {
|
|
232
|
+
recorder.record(event);
|
|
233
|
+
if (event.k === 'shown') {
|
|
234
|
+
this.learner.onGeneEvent({ kind: 'shown', geneIds: [event.g] });
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
flush: () => recorder.flush(),
|
|
238
|
+
dispose: () => recorder.dispose(),
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
buildService() {
|
|
242
|
+
return new GeneService({
|
|
243
|
+
reader: this.lib.store,
|
|
244
|
+
getConfig: () => this.effective(),
|
|
245
|
+
getCwd: () => this.deps.getCwd(),
|
|
246
|
+
sessionId: this.deps.sessionId,
|
|
247
|
+
now: Date.now,
|
|
248
|
+
env: () => this.env(),
|
|
249
|
+
registeredTools: () => this.deps.registeredToolNames(),
|
|
250
|
+
windowTokens: () => this.deps.windowTokens(),
|
|
251
|
+
knownSecrets: () => this.deps.knownSecrets(),
|
|
252
|
+
role: 'main',
|
|
253
|
+
usage: this.usage,
|
|
254
|
+
onFeedback: (signal) => this.learner.onGeneEvent({ kind: 'feedback', geneId: signal.geneId, verdict: signal.verdict }),
|
|
255
|
+
log: { warn: (message, data) => getLogger().warn('tool', message, data) },
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
learnerConfig() {
|
|
259
|
+
const cfg = this.effective();
|
|
260
|
+
return {
|
|
261
|
+
enabled: cfg.enabled,
|
|
262
|
+
autoLearn: cfg.autoLearn,
|
|
263
|
+
learnMinIntervalSec: cfg.learnMinIntervalSec,
|
|
264
|
+
learnMaxPerSession: cfg.learnMaxPerSession,
|
|
265
|
+
dailyTokens: cfg.dailyTokens,
|
|
266
|
+
reviewerMaxItems: cfg.reviewerMaxItems,
|
|
267
|
+
reviewerMaxTokens: cfg.reviewerMaxTokens,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
buildLearner() {
|
|
271
|
+
const catalog = this.service.catalog;
|
|
272
|
+
return new GeneLearner({
|
|
273
|
+
now: Date.now,
|
|
274
|
+
random: (n) => Math.floor(Math.random() * n),
|
|
275
|
+
config: () => this.learnerConfig(),
|
|
276
|
+
mode: () => ({
|
|
277
|
+
printMode: this.deps.printMode,
|
|
278
|
+
learnFlag: this.deps.learnFlag,
|
|
279
|
+
redTeamArmed: this.deps.redTeamArmed(),
|
|
280
|
+
}),
|
|
281
|
+
tier: () => ({ ok: resolveGeneModel(this.effective().reviewerModel, this.deps).ok }),
|
|
282
|
+
complete: createLearnComplete({
|
|
283
|
+
slot: () => this.effective().reviewerModel,
|
|
284
|
+
ports: this.deps,
|
|
285
|
+
registry: this.registry,
|
|
286
|
+
onSpend: (spend) => this.meter.add('reviewer', spend),
|
|
287
|
+
}),
|
|
288
|
+
queue: this.lib.queue,
|
|
289
|
+
runLog: this.lib.runLog,
|
|
290
|
+
project: {
|
|
291
|
+
refresh: () => this.refreshProjectVerdict(),
|
|
292
|
+
last: () => this.policy.lastVerdict(this.project()?.key ?? null),
|
|
293
|
+
},
|
|
294
|
+
notify: () => this.hub?.notify(),
|
|
295
|
+
session: () => {
|
|
296
|
+
const project = this.project();
|
|
297
|
+
return {
|
|
298
|
+
sessionId: this.deps.sessionId,
|
|
299
|
+
runId: this.runId,
|
|
300
|
+
agent: 'main',
|
|
301
|
+
...(project === null ? {} : { projectKey: project.key }),
|
|
302
|
+
};
|
|
303
|
+
},
|
|
304
|
+
existingLines: (budget) => this.reviewerLines(budget),
|
|
305
|
+
describeGene: (id) => {
|
|
306
|
+
const row = catalog.get(id);
|
|
307
|
+
return row === undefined ? undefined : geneBriefLine(row);
|
|
308
|
+
},
|
|
309
|
+
scopeOf: (id) => catalog.get(id)?.scope,
|
|
310
|
+
versionOf: (id) => catalog.get(id)?.v,
|
|
311
|
+
basisOf: (id) => {
|
|
312
|
+
const row = catalog.get(id);
|
|
313
|
+
return row === undefined ? undefined : { version: row.v };
|
|
314
|
+
},
|
|
315
|
+
lookupGene: (id) => catalog.get(id),
|
|
316
|
+
recordUsage: (event) => this.recordUsage(event),
|
|
317
|
+
knownSecrets: () => this.deps.knownSecrets(),
|
|
318
|
+
log: { warn: (event, fields) => getLogger().warn('tool', event, fields) },
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
recordUsage(event) {
|
|
322
|
+
try {
|
|
323
|
+
this.usage.record(event);
|
|
324
|
+
}
|
|
325
|
+
catch {
|
|
326
|
+
// Bookkeeping never fails a turn.
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* The reviewer-budget tier-1 list the learning call sees so it can tell "already known" from
|
|
331
|
+
* "new". Planned straight from the pure planner: going through `service.planContext` would
|
|
332
|
+
* count every line as `shown`, and the reviewer showing itself genes is not exposure.
|
|
333
|
+
*/
|
|
334
|
+
reviewerLines(budget) {
|
|
335
|
+
const { service } = this;
|
|
336
|
+
const goal = this.learner.ledger.snapshot().goal;
|
|
337
|
+
const plan = planGeneContext({
|
|
338
|
+
role: 'reviewer',
|
|
339
|
+
queryTerms: queryTermsOf(goal),
|
|
340
|
+
caller: service.buildCaller('reviewer'),
|
|
341
|
+
budgets: budget,
|
|
342
|
+
now: Date.now(),
|
|
343
|
+
}, service.catalog.index, service.catalog.statsOf);
|
|
344
|
+
return plan.lines.map((line) => line.line);
|
|
345
|
+
}
|
|
346
|
+
buildCurator() {
|
|
347
|
+
const { lib } = this;
|
|
348
|
+
const tombstones = new TombstoneStore(lib.store, Date.now);
|
|
349
|
+
const kind = this.deps.kind === 'desktop' ? 'desktop' : 'tui';
|
|
350
|
+
return new GeneCurator({
|
|
351
|
+
ctx: lib.ctx,
|
|
352
|
+
queue: lib.queue,
|
|
353
|
+
service: lib.versions,
|
|
354
|
+
library: createCuratorLibrary(lib.store),
|
|
355
|
+
runLog: lib.runLog,
|
|
356
|
+
model: createCuratorModelPort({
|
|
357
|
+
resolve: () => resolveGeneModel(this.effective().curatorModel, this.deps),
|
|
358
|
+
registry: this.registry,
|
|
359
|
+
}),
|
|
360
|
+
config: () => this.effective(),
|
|
361
|
+
readPersistedGeneConfig: createPersistedGeneConfigReader({ path: getConfigPath() }),
|
|
362
|
+
redteam: () => this.deps.redTeamArmed(),
|
|
363
|
+
projects: { policy: this.policy, rootOf: (key) => this.recordedProjectRoot(key) },
|
|
364
|
+
env: () => this.env(),
|
|
365
|
+
isForgotten: (query) => tombstones.isForgotten(query),
|
|
366
|
+
knownSecrets: () => this.deps.knownSecrets(),
|
|
367
|
+
pid: process.pid,
|
|
368
|
+
kind,
|
|
369
|
+
validatorExtras: {
|
|
370
|
+
hostToolNames: () => this.deps.registeredToolNames(),
|
|
371
|
+
projectRoot: () => this.project()?.root,
|
|
372
|
+
},
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
buildRunner(curator) {
|
|
376
|
+
const { lib } = this;
|
|
377
|
+
const isLeaseHolder = () => curator.holdsLease();
|
|
378
|
+
return new GeneMaintenanceRunner({
|
|
379
|
+
store: lib.store,
|
|
380
|
+
locks: lib.locks,
|
|
381
|
+
service: lib.versions,
|
|
382
|
+
catalog: this.service.catalog,
|
|
383
|
+
queue: lib.queue,
|
|
384
|
+
gc: new GeneGc({
|
|
385
|
+
store: lib.store,
|
|
386
|
+
locks: lib.locks,
|
|
387
|
+
service: lib.versions,
|
|
388
|
+
isLeaseHolder,
|
|
389
|
+
queue: lib.queue,
|
|
390
|
+
runLog: lib.runLog,
|
|
391
|
+
evalRecorder: lib.evals,
|
|
392
|
+
}),
|
|
393
|
+
compactor: new StoreIndexWriter({ store: lib.store, locks: lib.locks, now: Date.now }),
|
|
394
|
+
isLeaseHolder,
|
|
395
|
+
sessionId: this.deps.sessionId,
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
buildHub() {
|
|
399
|
+
return new GeneActivityHub({
|
|
400
|
+
ctx: this.lib.ctx,
|
|
401
|
+
facts: {
|
|
402
|
+
available: () => this.effective().enabled,
|
|
403
|
+
use: () => this.effective().use,
|
|
404
|
+
autoLearn: () => this.effective().autoLearn,
|
|
405
|
+
library: () => this.service.getView().library,
|
|
406
|
+
context: () => this.service.getView().context,
|
|
407
|
+
retrieving: () => this.service.getView().retrieving,
|
|
408
|
+
learner: () => this.learner.view(),
|
|
409
|
+
curatorPausedReason: () => this.curator?.pausedReason(),
|
|
410
|
+
refresh: async () => {
|
|
411
|
+
await this.learner.refreshBudget();
|
|
412
|
+
await this.learner.refreshProject();
|
|
413
|
+
},
|
|
414
|
+
},
|
|
415
|
+
queue: this.lib.queue,
|
|
416
|
+
runLog: this.lib.runLog,
|
|
417
|
+
now: Date.now,
|
|
418
|
+
pid: process.pid,
|
|
419
|
+
hints: new GeneUiHints(this.lib.ctx),
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
/** Every part that can change what the status chip says tells the hub. */
|
|
423
|
+
wireNotifications() {
|
|
424
|
+
this.service.subscribe(() => this.hub.notify());
|
|
425
|
+
this.curator?.onChange(() => this.hub.notify());
|
|
426
|
+
this.hub.subscribe(() => this.warnWhenLearnerFails());
|
|
427
|
+
}
|
|
428
|
+
/** One transcript notice when learning switches itself off, not one per status refresh. */
|
|
429
|
+
warnWhenLearnerFails() {
|
|
430
|
+
const state = this.learner.view();
|
|
431
|
+
const key = `${state.state}:${state.pausedReason ?? ''}`;
|
|
432
|
+
if (key === this.lastLearnerState)
|
|
433
|
+
return;
|
|
434
|
+
this.lastLearnerState = key;
|
|
435
|
+
if (state.pausedReason === 'learner_failing') {
|
|
436
|
+
this.deps.notify('warn', 'Gene Bank learning paused after repeated failures. Change the model or /gene learn on to retry.');
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
// ===========================================================================
|
|
440
|
+
// Lifecycle
|
|
441
|
+
// ===========================================================================
|
|
442
|
+
/** Open the library and start the background parts. Never rejects. */
|
|
443
|
+
async start() {
|
|
444
|
+
if (this.started || this.disposed)
|
|
445
|
+
return;
|
|
446
|
+
this.started = true;
|
|
447
|
+
try {
|
|
448
|
+
await this.service.start();
|
|
449
|
+
this.curator?.start();
|
|
450
|
+
this.runner?.start();
|
|
451
|
+
this.hub.notify();
|
|
452
|
+
void this.hub.refresh();
|
|
453
|
+
this.runStartChecks();
|
|
454
|
+
}
|
|
455
|
+
catch (error) {
|
|
456
|
+
getLogger().warn('tool', 'gene start failed', { error: errorName(error) });
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
runStartChecks() {
|
|
460
|
+
if (this.curator === undefined)
|
|
461
|
+
return;
|
|
462
|
+
void runSessionStartChecks({
|
|
463
|
+
store: this.lib.store,
|
|
464
|
+
queue: this.lib.queue,
|
|
465
|
+
catalog: this.service.catalog,
|
|
466
|
+
autoLearn: () => this.effective().autoLearn,
|
|
467
|
+
project: this.project(),
|
|
468
|
+
env: this.env(),
|
|
469
|
+
sessionId: this.deps.sessionId,
|
|
470
|
+
}).catch((error) => {
|
|
471
|
+
getLogger().warn('tool', 'gene session-start checks failed', { error: errorName(error) });
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
/** Stop every part and flush usage. Never rejects. */
|
|
475
|
+
async dispose() {
|
|
476
|
+
if (this.disposed)
|
|
477
|
+
return;
|
|
478
|
+
this.disposed = true;
|
|
479
|
+
this.learner.dispose();
|
|
480
|
+
this.hub.dispose();
|
|
481
|
+
this.managerHandle?.value.dispose();
|
|
482
|
+
await Promise.allSettled([
|
|
483
|
+
this.curator?.dispose(),
|
|
484
|
+
this.runner?.stop(),
|
|
485
|
+
this.service.dispose(),
|
|
486
|
+
]);
|
|
487
|
+
}
|
|
488
|
+
// ===========================================================================
|
|
489
|
+
// The agent loop's entry points
|
|
490
|
+
// ===========================================================================
|
|
491
|
+
/** A new user-initiated run begins (not a steering message). */
|
|
492
|
+
beginRun() {
|
|
493
|
+
this.runCounter += 1;
|
|
494
|
+
this.runId = `${this.deps.sessionId}-r${this.runCounter}`;
|
|
495
|
+
this.runStartedAt = Date.now();
|
|
496
|
+
this.meter = new CostMeter();
|
|
497
|
+
for (const spend of this.explainCarry.splice(0))
|
|
498
|
+
this.meter.add('explain', spend);
|
|
499
|
+
this.learner.ledger.beginRun();
|
|
500
|
+
}
|
|
501
|
+
onUserMessage(text, options) {
|
|
502
|
+
try {
|
|
503
|
+
if (!options.steering)
|
|
504
|
+
this.lastTask = text;
|
|
505
|
+
this.learner.onUserMessage(text);
|
|
506
|
+
}
|
|
507
|
+
catch {
|
|
508
|
+
// Observers never fail a turn (I-24).
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
onAgentEvent(event, aborted = false) {
|
|
512
|
+
if (!this.disposed)
|
|
513
|
+
this.feed.handle(event, aborted);
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Plan the context zone for the turn that is about to start. Bounded: the zone is an
|
|
517
|
+
* optimisation, so a slow or cold index costs the turn at most `PLAN_WAIT_MS`.
|
|
518
|
+
*/
|
|
519
|
+
async planForTurn(text, reason) {
|
|
520
|
+
try {
|
|
521
|
+
const job = this.service.planContext({
|
|
522
|
+
runId: this.runId,
|
|
523
|
+
role: 'main',
|
|
524
|
+
taskText: text,
|
|
525
|
+
reason,
|
|
526
|
+
});
|
|
527
|
+
await settleWithin(job, PLAN_WAIT_MS);
|
|
528
|
+
}
|
|
529
|
+
catch {
|
|
530
|
+
// The block stays as it was; the prompt falls back to the previous zone or none.
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
/** `/cwd`, `/clear`, `/resume`: forget what depended on the old context and plan again. */
|
|
534
|
+
resetSession(reason) {
|
|
535
|
+
this.feed.reset();
|
|
536
|
+
this.hints.reset();
|
|
537
|
+
this.service.invalidateContext();
|
|
538
|
+
if (reason !== 'cwd') {
|
|
539
|
+
this.learner.resetSession();
|
|
540
|
+
this.lastTask = '';
|
|
541
|
+
}
|
|
542
|
+
void this.replan(reason);
|
|
543
|
+
}
|
|
544
|
+
async replan(reason) {
|
|
545
|
+
await this.planForTurn(this.lastTask, reason);
|
|
546
|
+
if (!this.disposed)
|
|
547
|
+
this.deps.rebuildPrompt();
|
|
548
|
+
}
|
|
549
|
+
/** The stable `<gene_library>` rules block, or '' when the caller cannot use the library. */
|
|
550
|
+
rulesBlock(toolNames, _config) {
|
|
551
|
+
try {
|
|
552
|
+
return this.service.renderRulesBlock(toolNames, this.runId);
|
|
553
|
+
}
|
|
554
|
+
catch {
|
|
555
|
+
return '';
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
/** The last planned `<gene_context>` block, or ''. */
|
|
559
|
+
contextBlock() {
|
|
560
|
+
try {
|
|
561
|
+
return this.service.renderContextBlock('main');
|
|
562
|
+
}
|
|
563
|
+
catch {
|
|
564
|
+
return '';
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
// ===========================================================================
|
|
568
|
+
// Tools, sub-agents, UI surface
|
|
569
|
+
// ===========================================================================
|
|
570
|
+
/** The four gene tools for the main agent. `shell` is read when `gene_run` quotes arguments. */
|
|
571
|
+
tools(access, includeRun, shell) {
|
|
572
|
+
return createGeneTools({
|
|
573
|
+
service: this.service,
|
|
574
|
+
actor: () => this.actor(),
|
|
575
|
+
hasTool: access.hasTool,
|
|
576
|
+
shellKind: () => shellKindOfDescribed(shell()),
|
|
577
|
+
runCommand: access.runCommand,
|
|
578
|
+
includeRun,
|
|
579
|
+
warn: (message, data) => getLogger().warn('tool', message, data),
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* What `TeamRuntime` needs to give each child the same library (9.7): the child's own tools
|
|
584
|
+
* (tagged with its label), a hint engine per child, the rules block and a context zone planned
|
|
585
|
+
* ONCE per dispatch from its brief. Children share the index; none gets a copy.
|
|
586
|
+
*/
|
|
587
|
+
subagentTools() {
|
|
588
|
+
const zones = new Map();
|
|
589
|
+
const engines = new Map();
|
|
590
|
+
return {
|
|
591
|
+
prepare: (specs) => this.prepareZones(specs, zones),
|
|
592
|
+
toolsFor: (spec, access) => createGeneTools({
|
|
593
|
+
service: this.service,
|
|
594
|
+
actor: () => ({ runId: this.runId, role: 'subagent', agent: spec.label }),
|
|
595
|
+
hasTool: access.hasTool,
|
|
596
|
+
shellKind: () => shellKindOfDescribed(describeShell().shell),
|
|
597
|
+
runCommand: access.runCommand,
|
|
598
|
+
includeRun: true,
|
|
599
|
+
warn: (message, data) => getLogger().warn('tool', message, data),
|
|
600
|
+
}),
|
|
601
|
+
hintsFor: (spec) => this.hintEngineFor(spec, engines),
|
|
602
|
+
rulesBlock: (toolNames) => this.rulesBlock(toolNames),
|
|
603
|
+
contextFor: (label) => zones.get(label) ?? '',
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
hintEngineFor(spec, engines) {
|
|
607
|
+
let engine = engines.get(spec.label);
|
|
608
|
+
if (engine === undefined) {
|
|
609
|
+
engine = new GeneHintEngine({
|
|
610
|
+
service: this.service,
|
|
611
|
+
actor: () => ({ runId: this.runId, role: 'subagent', agent: spec.label }),
|
|
612
|
+
active: () => {
|
|
613
|
+
const cfg = this.effective();
|
|
614
|
+
return cfg.enabled && cfg.use && cfg.hints;
|
|
615
|
+
},
|
|
616
|
+
});
|
|
617
|
+
engines.set(spec.label, engine);
|
|
618
|
+
}
|
|
619
|
+
return engine;
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Plan each child's zone one after another (they share the service's per-role hysteresis
|
|
623
|
+
* memory, so interleaving them would let one child's plan reuse another's text), then restore
|
|
624
|
+
* the main agent's own zone so the status chip describes the main agent again.
|
|
625
|
+
*/
|
|
626
|
+
async prepareZones(specs, zones) {
|
|
627
|
+
zones.clear();
|
|
628
|
+
for (const spec of specs) {
|
|
629
|
+
try {
|
|
630
|
+
const brief = `${spec.description}\n${spec.prompt}`.slice(0, SUBAGENT_TASK_CHARS);
|
|
631
|
+
// Any reason but `user_turn` drops the hysteresis state, so each child gets its own plan.
|
|
632
|
+
const out = await this.service.planContext({
|
|
633
|
+
runId: this.runId,
|
|
634
|
+
role: 'subagent',
|
|
635
|
+
taskText: brief,
|
|
636
|
+
reason: 'settings',
|
|
637
|
+
});
|
|
638
|
+
zones.set(spec.label, out.text);
|
|
639
|
+
}
|
|
640
|
+
catch {
|
|
641
|
+
zones.set(spec.label, '');
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
await this.planForTurn(this.lastTask, 'user_turn');
|
|
645
|
+
}
|
|
646
|
+
/** The manager both user interfaces talk to; rebuilt when the working directory moves. */
|
|
647
|
+
get manager() {
|
|
648
|
+
const cwd = this.deps.getCwd();
|
|
649
|
+
if (this.managerHandle === null || this.managerHandle.cwd !== cwd) {
|
|
650
|
+
this.managerHandle?.value.dispose();
|
|
651
|
+
const cfg = this.effective();
|
|
652
|
+
this.managerHandle = {
|
|
653
|
+
cwd,
|
|
654
|
+
value: createGeneManager({
|
|
655
|
+
cwd,
|
|
656
|
+
sessionId: this.deps.sessionId,
|
|
657
|
+
teamPath: cfg.teamPath,
|
|
658
|
+
...(this.deps.root === undefined ? {} : { root: this.deps.root }),
|
|
659
|
+
config: () => this.effective(),
|
|
660
|
+
knownSecrets: () => this.deps.knownSecrets(),
|
|
661
|
+
kind: this.deps.kind,
|
|
662
|
+
explainTransport: this.explainTransport(),
|
|
663
|
+
...(this.deps.skills === undefined ? {} : { skillSources: () => this.deps.skills?.() ?? null }),
|
|
664
|
+
}),
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
return this.managerHandle.value;
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* The model a Chinese explanation uses: the fast tier, through this session's own fail-fast
|
|
671
|
+
* registry. Its spend is carried into the NEXT run's evaluation row as the `explain` component
|
|
672
|
+
* (a click is outside any run, so it cannot belong to a run that is not happening).
|
|
673
|
+
*/
|
|
674
|
+
explainTransport() {
|
|
675
|
+
const complete = createLearnComplete({
|
|
676
|
+
slot: () => 'fast',
|
|
677
|
+
ports: this.deps,
|
|
678
|
+
registry: this.registry,
|
|
679
|
+
onSpend: (spend) => {
|
|
680
|
+
// Bounded: clicks with no run in between must not grow this without limit.
|
|
681
|
+
if (this.explainCarry.length < EXPLAIN_CARRY_MAX)
|
|
682
|
+
this.explainCarry.push(spend);
|
|
683
|
+
},
|
|
684
|
+
});
|
|
685
|
+
return {
|
|
686
|
+
provider: () => {
|
|
687
|
+
const resolved = resolveGeneModel('fast', this.deps);
|
|
688
|
+
return resolved.ok ? resolved.choice.ref.providerId : null;
|
|
689
|
+
},
|
|
690
|
+
complete,
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
/** The unified status (one source for both interfaces). */
|
|
694
|
+
getView() {
|
|
695
|
+
try {
|
|
696
|
+
return this.hub.view();
|
|
697
|
+
}
|
|
698
|
+
catch {
|
|
699
|
+
return emptyGeneSessionView();
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
subscribe(listener) {
|
|
703
|
+
return this.hub.subscribe(listener);
|
|
704
|
+
}
|
|
705
|
+
// ===========================================================================
|
|
706
|
+
// Evaluation (13)
|
|
707
|
+
// ===========================================================================
|
|
708
|
+
/** The run ended: write its evaluation row once the run's learning call has reported its cost. */
|
|
709
|
+
onRunEnd(outcome) {
|
|
710
|
+
const facts = evalFactsFromLedger(this.learner.ledger.snapshot());
|
|
711
|
+
if (facts.turns === 0)
|
|
712
|
+
return;
|
|
713
|
+
const cfg = this.effective();
|
|
714
|
+
const project = this.project();
|
|
715
|
+
const row = {
|
|
716
|
+
run: this.runId,
|
|
717
|
+
session: this.deps.sessionId,
|
|
718
|
+
...(project === null ? {} : { projectKey: project.key }),
|
|
719
|
+
mode: this.deps.mode(),
|
|
720
|
+
outcome,
|
|
721
|
+
durationMs: Math.max(0, Date.now() - this.runStartedAt),
|
|
722
|
+
...facts,
|
|
723
|
+
flags: {
|
|
724
|
+
geneUse: cfg.enabled && cfg.use,
|
|
725
|
+
learn: cfg.autoLearn,
|
|
726
|
+
holdout: isHoldoutRun(this.runId, cfg.holdoutPercent),
|
|
727
|
+
},
|
|
728
|
+
};
|
|
729
|
+
void this.settleEval(row, this.meter);
|
|
730
|
+
}
|
|
731
|
+
async settleEval(row, meter) {
|
|
732
|
+
await settleWithin(this.learner.idle(), EVAL_SETTLE_MS);
|
|
733
|
+
try {
|
|
734
|
+
await this.lib.evals.record({ ...row, cost: meter.snapshot() });
|
|
735
|
+
}
|
|
736
|
+
catch {
|
|
737
|
+
// An evaluation row is a statistic, never a reason to disturb the session.
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
function errorName(error) {
|
|
742
|
+
return error instanceof Error ? error.name : 'Error';
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* The composition root, or `undefined` when the feature is off (byte-identical sessions, I-20).
|
|
746
|
+
*
|
|
747
|
+
* A config with NO `gene` section is OFF, not default-on: the section is optional on `CliConfig`
|
|
748
|
+
* exactly like `memory` and `redteam`, `App` reads `gene?.enabled !== true`, and every config
|
|
749
|
+
* that went through `load.ts` carries the section. Reading absence as "on" would hand a fixture,
|
|
750
|
+
* or any embedder that builds a `CliConfig` by hand, four tools and a prompt block it never asked for.
|
|
751
|
+
*/
|
|
752
|
+
export function createGeneWiring(deps) {
|
|
753
|
+
if (deps.getConfig().gene?.enabled !== true)
|
|
754
|
+
return undefined;
|
|
755
|
+
try {
|
|
756
|
+
return new GeneWiring(deps);
|
|
757
|
+
}
|
|
758
|
+
catch (error) {
|
|
759
|
+
getLogger().warn('tool', 'gene wiring failed; the library is off for this session', {
|
|
760
|
+
error: errorName(error),
|
|
761
|
+
});
|
|
762
|
+
return undefined;
|
|
763
|
+
}
|
|
764
|
+
}
|