@aragon-agent/cli 0.6.55 → 0.6.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2912 -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 +103 -0
- package/dist/gene/curator/run-events.d.ts +63 -0
- package/dist/gene/curator/run-events.js +55 -0
- package/dist/gene/curator/similarity.d.ts +13 -0
- package/dist/gene/curator/similarity.js +46 -0
- package/dist/gene/curator/switches.d.ts +78 -0
- package/dist/gene/curator/switches.js +146 -0
- package/dist/gene/curator/task-context.d.ts +93 -0
- package/dist/gene/curator/task-context.js +63 -0
- package/dist/gene/curator/task-runner.d.ts +56 -0
- package/dist/gene/curator/task-runner.js +257 -0
- package/dist/gene/curator/tool-support.d.ts +112 -0
- package/dist/gene/curator/tool-support.js +197 -0
- package/dist/gene/curator/toolkit.d.ts +49 -0
- package/dist/gene/curator/toolkit.js +104 -0
- package/dist/gene/curator/tools-read.d.ts +4 -0
- package/dist/gene/curator/tools-read.js +177 -0
- package/dist/gene/curator/tools-release.d.ts +73 -0
- package/dist/gene/curator/tools-release.js +287 -0
- package/dist/gene/curator/tools-verify.d.ts +47 -0
- package/dist/gene/curator/tools-verify.js +202 -0
- package/dist/gene/curator/tools-write.d.ts +26 -0
- package/dist/gene/curator/tools-write.js +394 -0
- package/dist/gene/curator/tools.d.ts +23 -0
- package/dist/gene/curator/tools.js +21 -0
- package/dist/gene/curator/validate/common.d.ts +87 -0
- package/dist/gene/curator/validate/common.js +132 -0
- package/dist/gene/curator/validate/evidence-tokens.d.ts +36 -0
- package/dist/gene/curator/validate/evidence-tokens.js +139 -0
- package/dist/gene/curator/validate/index.d.ts +23 -0
- package/dist/gene/curator/validate/index.js +62 -0
- package/dist/gene/curator/validate/knowledge.d.ts +25 -0
- package/dist/gene/curator/validate/knowledge.js +216 -0
- package/dist/gene/curator/validate/live-check.d.ts +14 -0
- package/dist/gene/curator/validate/live-check.js +144 -0
- package/dist/gene/curator/validate/ports.d.ts +67 -0
- package/dist/gene/curator/validate/ports.js +1 -0
- package/dist/gene/curator/validate/prompts.d.ts +41 -0
- package/dist/gene/curator/validate/prompts.js +161 -0
- package/dist/gene/curator/validate/skill.d.ts +15 -0
- package/dist/gene/curator/validate/skill.js +251 -0
- package/dist/gene/curator/validate/tool.d.ts +6 -0
- package/dist/gene/curator/validate/tool.js +217 -0
- package/dist/gene/curator/validate/types.d.ts +90 -0
- package/dist/gene/curator/validate/types.js +1 -0
- package/dist/gene/curator/validator-ports.d.ts +38 -0
- package/dist/gene/curator/validator-ports.js +29 -0
- package/dist/gene/diag.d.ts +209 -0
- package/dist/gene/diag.js +549 -0
- package/dist/gene/env.d.ts +10 -0
- package/dist/gene/env.js +214 -0
- package/dist/gene/eval-recorder.d.ts +84 -0
- package/dist/gene/eval-recorder.js +222 -0
- package/dist/gene/explain-transport.d.ts +3 -0
- package/dist/gene/explain-transport.js +39 -0
- package/dist/gene/explain.d.ts +96 -0
- package/dist/gene/explain.js +293 -0
- package/dist/gene/fs.d.ts +39 -0
- package/dist/gene/fs.js +139 -0
- package/dist/gene/fsck.d.ts +68 -0
- package/dist/gene/fsck.js +134 -0
- package/dist/gene/gc-library-sweeps.d.ts +9 -0
- package/dist/gene/gc-library-sweeps.js +220 -0
- package/dist/gene/gc-rejected-set.d.ts +15 -0
- package/dist/gene/gc-rejected-set.js +63 -0
- package/dist/gene/gc-safe.d.ts +12 -0
- package/dist/gene/gc-safe.js +74 -0
- package/dist/gene/gc-scope-sweeps.d.ts +18 -0
- package/dist/gene/gc-scope-sweeps.js +297 -0
- package/dist/gene/gc-types.d.ts +55 -0
- package/dist/gene/gc-types.js +26 -0
- package/dist/gene/gc.d.ts +42 -0
- package/dist/gene/gc.js +129 -0
- package/dist/gene/gene-errors.d.ts +23 -0
- package/dist/gene/gene-errors.js +29 -0
- package/dist/gene/hints.d.ts +91 -0
- package/dist/gene/hints.js +394 -0
- package/dist/gene/hub.d.ts +149 -0
- package/dist/gene/hub.js +371 -0
- package/dist/gene/import-plan.d.ts +80 -0
- package/dist/gene/import-plan.js +294 -0
- package/dist/gene/import-proposal.d.ts +23 -0
- package/dist/gene/import-proposal.js +101 -0
- package/dist/gene/import-skill-port.d.ts +6 -0
- package/dist/gene/import-skill-port.js +53 -0
- package/dist/gene/import-sources.d.ts +48 -0
- package/dist/gene/import-sources.js +140 -0
- package/dist/gene/index-writer.d.ts +36 -0
- package/dist/gene/index-writer.js +101 -0
- package/dist/gene/learn/learn-call.d.ts +63 -0
- package/dist/gene/learn/learn-call.js +157 -0
- package/dist/gene/learn/learn-prompt.d.ts +51 -0
- package/dist/gene/learn/learn-prompt.js +245 -0
- package/dist/gene/learn/learner.d.ts +181 -0
- package/dist/gene/learn/learner.js +426 -0
- package/dist/gene/learn/ledger.d.ts +220 -0
- package/dist/gene/learn/ledger.js +394 -0
- package/dist/gene/learn/proposal-build.d.ts +83 -0
- package/dist/gene/learn/proposal-build.js +211 -0
- package/dist/gene/learn/proposal-guard.d.ts +43 -0
- package/dist/gene/learn/proposal-guard.js +37 -0
- package/dist/gene/learn/signals-port.d.ts +47 -0
- package/dist/gene/learn/signals-port.js +86 -0
- package/dist/gene/maintenance-paths.d.ts +7 -0
- package/dist/gene/maintenance-paths.js +20 -0
- package/dist/gene/maintenance-probation.d.ts +33 -0
- package/dist/gene/maintenance-probation.js +127 -0
- package/dist/gene/maintenance-proposals.d.ts +24 -0
- package/dist/gene/maintenance-proposals.js +68 -0
- package/dist/gene/maintenance-runner.d.ts +92 -0
- package/dist/gene/maintenance-runner.js +262 -0
- package/dist/gene/maintenance-tasks.d.ts +37 -0
- package/dist/gene/maintenance-tasks.js +95 -0
- package/dist/gene/manager-actions.d.ts +12 -0
- package/dist/gene/manager-actions.js +279 -0
- package/dist/gene/manager-approve.d.ts +29 -0
- package/dist/gene/manager-approve.js +84 -0
- package/dist/gene/manager-context.d.ts +90 -0
- package/dist/gene/manager-context.js +201 -0
- package/dist/gene/manager-detail.d.ts +17 -0
- package/dist/gene/manager-detail.js +313 -0
- package/dist/gene/manager-gc.d.ts +13 -0
- package/dist/gene/manager-gc.js +81 -0
- package/dist/gene/manager-library.d.ts +39 -0
- package/dist/gene/manager-library.js +90 -0
- package/dist/gene/manager-list.d.ts +27 -0
- package/dist/gene/manager-list.js +170 -0
- package/dist/gene/manager-queue.d.ts +16 -0
- package/dist/gene/manager-queue.js +124 -0
- package/dist/gene/manager-stats.d.ts +28 -0
- package/dist/gene/manager-stats.js +53 -0
- package/dist/gene/manager-view.d.ts +27 -0
- package/dist/gene/manager-view.js +124 -0
- package/dist/gene/manager.d.ts +56 -0
- package/dist/gene/manager.js +179 -0
- package/dist/gene/orphan-scopes.d.ts +37 -0
- package/dist/gene/orphan-scopes.js +150 -0
- package/dist/gene/paths.d.ts +73 -0
- package/dist/gene/paths.js +239 -0
- package/dist/gene/plan-content.d.ts +73 -0
- package/dist/gene/plan-content.js +522 -0
- package/dist/gene/plan-lifecycle.d.ts +29 -0
- package/dist/gene/plan-lifecycle.js +310 -0
- package/dist/gene/policy.d.ts +71 -0
- package/dist/gene/policy.js +313 -0
- package/dist/gene/project-learning.d.ts +25 -0
- package/dist/gene/project-learning.js +54 -0
- package/dist/gene/propagation.d.ts +48 -0
- package/dist/gene/propagation.js +45 -0
- package/dist/gene/queue-lock.d.ts +22 -0
- package/dist/gene/queue-lock.js +99 -0
- package/dist/gene/queue-record.d.ts +57 -0
- package/dist/gene/queue-record.js +145 -0
- package/dist/gene/queue.d.ts +235 -0
- package/dist/gene/queue.js +764 -0
- package/dist/gene/read.d.ts +61 -0
- package/dist/gene/read.js +252 -0
- package/dist/gene/runlog.d.ts +101 -0
- package/dist/gene/runlog.js +404 -0
- package/dist/gene/schema-lite.d.ts +24 -0
- package/dist/gene/schema-lite.js +159 -0
- package/dist/gene/scope-guard.d.ts +23 -0
- package/dist/gene/scope-guard.js +9 -0
- package/dist/gene/scope-lock.d.ts +48 -0
- package/dist/gene/scope-lock.js +222 -0
- package/dist/gene/scope-verify.d.ts +63 -0
- package/dist/gene/scope-verify.js +123 -0
- package/dist/gene/secrets.d.ts +43 -0
- package/dist/gene/secrets.js +161 -0
- package/dist/gene/service-context.d.ts +43 -0
- package/dist/gene/service-context.js +79 -0
- package/dist/gene/service-prepare.d.ts +25 -0
- package/dist/gene/service-prepare.js +219 -0
- package/dist/gene/service-types.d.ts +196 -0
- package/dist/gene/service-types.js +1 -0
- package/dist/gene/service-view.d.ts +41 -0
- package/dist/gene/service-view.js +69 -0
- package/dist/gene/service.d.ts +114 -0
- package/dist/gene/service.js +761 -0
- package/dist/gene/session-start.d.ts +36 -0
- package/dist/gene/session-start.js +212 -0
- package/dist/gene/store-port.d.ts +88 -0
- package/dist/gene/store-port.js +10 -0
- package/dist/gene/store-rows.d.ts +36 -0
- package/dist/gene/store-rows.js +72 -0
- package/dist/gene/store.d.ts +125 -0
- package/dist/gene/store.js +622 -0
- package/dist/gene/tombstones.d.ts +47 -0
- package/dist/gene/tombstones.js +61 -0
- package/dist/gene/tool-runner.d.ts +47 -0
- package/dist/gene/tool-runner.js +154 -0
- package/dist/gene/tool-screen.d.ts +41 -0
- package/dist/gene/tool-screen.js +227 -0
- package/dist/gene/tools.d.ts +112 -0
- package/dist/gene/tools.js +539 -0
- package/dist/gene/transfer.d.ts +85 -0
- package/dist/gene/transfer.js +433 -0
- package/dist/gene/ui-hints.d.ts +15 -0
- package/dist/gene/ui-hints.js +67 -0
- package/dist/gene/usage-fold-cursor.d.ts +24 -0
- package/dist/gene/usage-fold-cursor.js +115 -0
- package/dist/gene/usage-fold.d.ts +49 -0
- package/dist/gene/usage-fold.js +397 -0
- package/dist/gene/usage.d.ts +67 -0
- package/dist/gene/usage.js +231 -0
- package/dist/gene/version-service.d.ts +118 -0
- package/dist/gene/version-service.js +453 -0
- package/dist/gene/wiring-events.d.ts +43 -0
- package/dist/gene/wiring-events.js +113 -0
- package/dist/gene/wiring-models.d.ts +46 -0
- package/dist/gene/wiring-models.js +81 -0
- package/dist/gene/wiring.d.ts +200 -0
- package/dist/gene/wiring.js +764 -0
- package/dist/governance/prompts/orchestrator-block.js +3 -2
- package/dist/host/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,617 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `GeneCurator`: the independent background host of the library's memory agent
|
|
3
|
+
* (gene-bank spec 10.3 - 10.6, 14.2; I-8, I-18, I-19, I-21, I-22, I-24).
|
|
4
|
+
*
|
|
5
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
6
|
+
*
|
|
7
|
+
* WHAT IT IS. A loop that, while this process holds the library's curator lease, claims proposals
|
|
8
|
+
* from the queue, hands each to `runClaimedTask`, settles the queue file from the verdict, and keeps
|
|
9
|
+
* the library tidy (stale claims, probation, maintenance, retention). It is NOT part of the
|
|
10
|
+
* foreground: nothing in a user turn calls it except `pause`, `resume` and `status`, and it never
|
|
11
|
+
* takes a foreground lock (I-22).
|
|
12
|
+
*
|
|
13
|
+
* ONE CURATOR PER LIBRARY (I-21). Only an interactive process starts one; the lease
|
|
14
|
+
* (`lease.ts`) decides which. A process that does not hold it keeps retrying every 30 s and does
|
|
15
|
+
* nothing else; the others only enqueue. When the holder dies the next retry takes over within
|
|
16
|
+
* about 45 s, and every task is idempotent (`deterministic.ts`), so "killed half way" costs a
|
|
17
|
+
* redo, never a duplicate.
|
|
18
|
+
*
|
|
19
|
+
* LANES. A lane is a proposal's scope: serial inside, fair between. The queue can only hand out its
|
|
20
|
+
* globally best item, so the host claims a small look-ahead window into `LaneScheduler` and runs
|
|
21
|
+
* the lanes' heads, at most `curatorConcurrency` at a time.
|
|
22
|
+
*
|
|
23
|
+
* THE USER'S SWITCHES ARE CHECKED THREE TIMES per task - after the claim, before validation, and
|
|
24
|
+
* immediately before the publishing commit - each against the STRICTER of the live config and the
|
|
25
|
+
* on-disk config (`switches.ts`), so turning learning off in the desktop settings stops a terminal's
|
|
26
|
+
* curator at its next checkpoint.
|
|
27
|
+
*
|
|
28
|
+
* PAUSING NEVER DESTROYS WORK. Forced or switch-driven pauses (red team, learning off) claim
|
|
29
|
+
* nothing. Model-related pauses (no fast model, an open breaker, the daily budget) still let
|
|
30
|
+
* the deterministic paths run - a revoke or a rollback spends no tokens and must not wait for a
|
|
31
|
+
* budget - and put model items back with a `notBefore`, no attempt counted. Every pause and resume
|
|
32
|
+
* is a run record, with the reason as the first word of its detail (the activity hub reads that).
|
|
33
|
+
*
|
|
34
|
+
* LOGGING NEVER CARRIES GENE TEXT (I-19): ids, scopes, enums, counts and stable tokens only.
|
|
35
|
+
*/
|
|
36
|
+
import { agingPriority } from '@aragon-agent/tui/model/gene/proposal';
|
|
37
|
+
import { scopeId } from '@aragon-agent/tui/model/gene/ids';
|
|
38
|
+
import { GENE_LIMITS } from '@aragon-agent/tui/model/gene/limits';
|
|
39
|
+
import { curatorLeaseFile } from '../paths.js';
|
|
40
|
+
import { BREAKER_OPEN_MS, CircuitBreaker, CuratorBudget, importClaimAllowed, } from './budget.js';
|
|
41
|
+
import { makeSystemProposal } from './executors.js';
|
|
42
|
+
import { LaneScheduler } from './lanes.js';
|
|
43
|
+
import { CuratorLease } from './lease.js';
|
|
44
|
+
import { sweepProbation } from './probation.js';
|
|
45
|
+
import { CuratorEvents, TokenLedger } from './run-events.js';
|
|
46
|
+
import { SwitchGuard } from './switches.js';
|
|
47
|
+
import { runClaimedTask } from './task-runner.js';
|
|
48
|
+
import { createValidatorPortsFactory } from './validator-ports.js';
|
|
49
|
+
import { realScheduler } from '../hub.js';
|
|
50
|
+
const MINUTE_MS = 60_000;
|
|
51
|
+
export const DEFAULT_TIMING = {
|
|
52
|
+
pollMs: 5000,
|
|
53
|
+
leaseRetryMs: 30_000,
|
|
54
|
+
leaseHeartbeatMs: GENE_LIMITS.leaseHeartbeatMs,
|
|
55
|
+
claimHeartbeatMs: GENE_LIMITS.claimHeartbeatMs,
|
|
56
|
+
recoverMs: 30_000,
|
|
57
|
+
probationMs: 5 * MINUTE_MS,
|
|
58
|
+
maintenanceFirstMs: 10 * MINUTE_MS,
|
|
59
|
+
maintenanceMs: 6 * 60 * MINUTE_MS,
|
|
60
|
+
pruneMs: 60 * MINUTE_MS,
|
|
61
|
+
housekeepingMs: MINUTE_MS,
|
|
62
|
+
lookahead: 4,
|
|
63
|
+
blockedRetryMs: 5 * MINUTE_MS,
|
|
64
|
+
choresFirstRun: 'immediate',
|
|
65
|
+
};
|
|
66
|
+
const WORKER_ALPHABET = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
67
|
+
function mintWorkerId(pid, random) {
|
|
68
|
+
let tail = '';
|
|
69
|
+
for (let i = 0; i < 4; i += 1)
|
|
70
|
+
tail += WORKER_ALPHABET[random(WORKER_ALPHABET.length)] ?? '0';
|
|
71
|
+
return `curator-${pid}-${tail}`;
|
|
72
|
+
}
|
|
73
|
+
export class GeneCurator {
|
|
74
|
+
deps;
|
|
75
|
+
now;
|
|
76
|
+
random;
|
|
77
|
+
timing;
|
|
78
|
+
scheduler;
|
|
79
|
+
workerId;
|
|
80
|
+
lease;
|
|
81
|
+
lanes = new LaneScheduler();
|
|
82
|
+
inflight = new Map();
|
|
83
|
+
ledger = new TokenLedger();
|
|
84
|
+
events;
|
|
85
|
+
budget;
|
|
86
|
+
breaker;
|
|
87
|
+
switches;
|
|
88
|
+
toolDeps;
|
|
89
|
+
listeners = new Set();
|
|
90
|
+
lastRun = new Map();
|
|
91
|
+
cancels = [];
|
|
92
|
+
ticking = null;
|
|
93
|
+
started = false;
|
|
94
|
+
disposed = false;
|
|
95
|
+
forced = null;
|
|
96
|
+
paused;
|
|
97
|
+
modelBlock = null;
|
|
98
|
+
budgetResetAt;
|
|
99
|
+
lastLeaseTry = Number.NEGATIVE_INFINITY;
|
|
100
|
+
leaseErrors = 0;
|
|
101
|
+
working = false;
|
|
102
|
+
constructor(deps) {
|
|
103
|
+
this.deps = deps;
|
|
104
|
+
this.now = deps.now ?? Date.now;
|
|
105
|
+
this.random = deps.random ?? ((n) => Math.floor(Math.random() * n));
|
|
106
|
+
this.timing = { ...DEFAULT_TIMING, ...deps.timing };
|
|
107
|
+
this.scheduler = deps.scheduler ?? realScheduler;
|
|
108
|
+
this.workerId = mintWorkerId(deps.pid, this.random);
|
|
109
|
+
this.lease = new CuratorLease({
|
|
110
|
+
path: curatorLeaseFile(deps.ctx),
|
|
111
|
+
now: this.now,
|
|
112
|
+
pid: deps.pid,
|
|
113
|
+
workerId: this.workerId,
|
|
114
|
+
kind: deps.kind,
|
|
115
|
+
...(deps.isPidAlive === undefined ? {} : { isPidAlive: deps.isPidAlive }),
|
|
116
|
+
});
|
|
117
|
+
this.switches = new SwitchGuard({
|
|
118
|
+
live: deps.config,
|
|
119
|
+
...(deps.readPersistedGeneConfig === undefined ? {} : { persisted: deps.readPersistedGeneConfig }),
|
|
120
|
+
...(deps.redteam === undefined ? {} : { redteam: deps.redteam }),
|
|
121
|
+
...(deps.projects === undefined ? {} : { projects: deps.projects }),
|
|
122
|
+
});
|
|
123
|
+
this.events = new CuratorEvents({ log: deps.runLog, ledger: this.ledger, holdsLease: () => this.lease.holds() });
|
|
124
|
+
this.budget = new CuratorBudget({ now: this.now, tokensSince: (since) => deps.runLog.tokensSince(since) });
|
|
125
|
+
this.breaker = new CircuitBreaker({ now: this.now });
|
|
126
|
+
this.toolDeps = this.buildToolDeps();
|
|
127
|
+
}
|
|
128
|
+
buildToolDeps() {
|
|
129
|
+
const d = this.deps;
|
|
130
|
+
const knownSecrets = d.knownSecrets ?? (() => []);
|
|
131
|
+
const validatorPorts = d.validatorPorts ??
|
|
132
|
+
createValidatorPortsFactory({
|
|
133
|
+
library: d.library,
|
|
134
|
+
model: d.model,
|
|
135
|
+
switches: this.switches,
|
|
136
|
+
env: d.env,
|
|
137
|
+
now: this.now,
|
|
138
|
+
knownSecrets,
|
|
139
|
+
...d.validatorExtras,
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
service: d.service,
|
|
143
|
+
library: d.library,
|
|
144
|
+
switches: this.switches,
|
|
145
|
+
events: this.events,
|
|
146
|
+
ledger: this.ledger,
|
|
147
|
+
validatorPorts,
|
|
148
|
+
isForgotten: d.isForgotten,
|
|
149
|
+
isCancelled: (claimed) => d.queue.isCancelled(claimed),
|
|
150
|
+
knownSecrets,
|
|
151
|
+
now: this.now,
|
|
152
|
+
...(d.probeDeps === undefined ? {} : { probeDeps: d.probeDeps }),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
runnerDeps() {
|
|
156
|
+
return {
|
|
157
|
+
queue: this.deps.queue,
|
|
158
|
+
toolDeps: this.toolDeps,
|
|
159
|
+
model: this.deps.model,
|
|
160
|
+
budget: this.budget,
|
|
161
|
+
breaker: this.breaker,
|
|
162
|
+
random: this.random,
|
|
163
|
+
scheduler: this.scheduler,
|
|
164
|
+
modelBlocked: () => this.modelBlockedNow(),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
// ---- public surface ---------------------------------------------------------
|
|
168
|
+
/** Begin trying for the lease and working. Idempotent. */
|
|
169
|
+
start() {
|
|
170
|
+
if (this.started || this.disposed)
|
|
171
|
+
return;
|
|
172
|
+
this.started = true;
|
|
173
|
+
const t = this.timing;
|
|
174
|
+
this.cancels = [
|
|
175
|
+
this.scheduler.every(t.pollMs, () => void this.tick()),
|
|
176
|
+
this.scheduler.every(t.leaseHeartbeatMs, () => void this.leaseBeat()),
|
|
177
|
+
this.scheduler.every(t.claimHeartbeatMs, () => void this.claimBeat()),
|
|
178
|
+
];
|
|
179
|
+
this.kick();
|
|
180
|
+
this.notify();
|
|
181
|
+
}
|
|
182
|
+
/** Stop taking work; let the running tasks finish, hand back what has not started, free the lease. */
|
|
183
|
+
async stop() {
|
|
184
|
+
if (!this.started)
|
|
185
|
+
return;
|
|
186
|
+
this.started = false;
|
|
187
|
+
this.cancelTimers();
|
|
188
|
+
await this.idle();
|
|
189
|
+
await this.releaseWaiting();
|
|
190
|
+
await this.giveUpLease('stopped');
|
|
191
|
+
this.notify();
|
|
192
|
+
}
|
|
193
|
+
/** Like stop, but abort the running tasks instead of waiting for them. */
|
|
194
|
+
async dispose() {
|
|
195
|
+
if (this.disposed)
|
|
196
|
+
return;
|
|
197
|
+
this.disposed = true;
|
|
198
|
+
this.started = false;
|
|
199
|
+
this.cancelTimers();
|
|
200
|
+
for (const task of this.inflight.values())
|
|
201
|
+
task.abort.abort();
|
|
202
|
+
await this.idle();
|
|
203
|
+
await this.releaseWaiting();
|
|
204
|
+
await this.giveUpLease('released');
|
|
205
|
+
this.listeners.clear();
|
|
206
|
+
}
|
|
207
|
+
/** Pause for an outside reason (red team armed, learning off). Claims nothing while paused. */
|
|
208
|
+
pause(reason) {
|
|
209
|
+
this.forced = reason;
|
|
210
|
+
this.notify();
|
|
211
|
+
}
|
|
212
|
+
resume() {
|
|
213
|
+
this.forced = null;
|
|
214
|
+
this.notify();
|
|
215
|
+
this.kick();
|
|
216
|
+
}
|
|
217
|
+
pausedReason() {
|
|
218
|
+
return this.forced ?? this.paused;
|
|
219
|
+
}
|
|
220
|
+
holdsLease() {
|
|
221
|
+
return this.lease.holds();
|
|
222
|
+
}
|
|
223
|
+
status() {
|
|
224
|
+
const reason = this.pausedReason();
|
|
225
|
+
const state = !this.started
|
|
226
|
+
? 'off'
|
|
227
|
+
: reason !== undefined
|
|
228
|
+
? 'paused'
|
|
229
|
+
: this.working || this.inflight.size > 0
|
|
230
|
+
? 'working'
|
|
231
|
+
: 'idle';
|
|
232
|
+
return {
|
|
233
|
+
started: this.started,
|
|
234
|
+
holdsLease: this.lease.holds(),
|
|
235
|
+
state,
|
|
236
|
+
...(reason === undefined ? {} : { pausedReason: reason }),
|
|
237
|
+
running: this.inflight.size,
|
|
238
|
+
waiting: this.lanes.size(),
|
|
239
|
+
breaker: this.breaker.view(),
|
|
240
|
+
...(this.budgetResetAt === undefined ? {} : { budgetResetAt: this.budgetResetAt }),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
onChange(listener) {
|
|
244
|
+
this.listeners.add(listener);
|
|
245
|
+
return () => this.listeners.delete(listener);
|
|
246
|
+
}
|
|
247
|
+
/** One scheduling pass. Never throws; concurrent calls share one pass. */
|
|
248
|
+
tick() {
|
|
249
|
+
if (this.ticking !== null)
|
|
250
|
+
return this.ticking;
|
|
251
|
+
this.ticking = this.tickOnce().finally(() => {
|
|
252
|
+
this.ticking = null;
|
|
253
|
+
});
|
|
254
|
+
return this.ticking;
|
|
255
|
+
}
|
|
256
|
+
/** Resolves when no task is running. */
|
|
257
|
+
async idle() {
|
|
258
|
+
while (this.inflight.size > 0)
|
|
259
|
+
await Promise.allSettled([...this.inflight.values()].map((t) => t.promise));
|
|
260
|
+
}
|
|
261
|
+
/** Test and shutdown helper: schedule, wait, and repeat until a pass finds nothing to do. */
|
|
262
|
+
async runUntilIdle(maxRounds = 50) {
|
|
263
|
+
for (let round = 0; round < maxRounds; round += 1) {
|
|
264
|
+
await this.tick();
|
|
265
|
+
const busy = this.inflight.size + this.lanes.size();
|
|
266
|
+
await this.idle();
|
|
267
|
+
if (busy === 0)
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
// ---- the pass -----------------------------------------------------------------
|
|
272
|
+
kick() {
|
|
273
|
+
this.scheduler.after(0, () => void this.tick());
|
|
274
|
+
}
|
|
275
|
+
notify() {
|
|
276
|
+
for (const listener of [...this.listeners]) {
|
|
277
|
+
try {
|
|
278
|
+
listener();
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
// A broken listener must not stop the loop.
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
async tickOnce() {
|
|
286
|
+
if (!this.started || this.disposed)
|
|
287
|
+
return;
|
|
288
|
+
try {
|
|
289
|
+
if (!(await this.ensureLease()))
|
|
290
|
+
return;
|
|
291
|
+
await this.housekeeping();
|
|
292
|
+
const claim = await this.evaluatePause();
|
|
293
|
+
if (claim) {
|
|
294
|
+
await this.refill();
|
|
295
|
+
this.launch();
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
// Paused: work that was claimed into a lane but never started goes back untouched.
|
|
299
|
+
await this.releaseWaiting();
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
// The loop must survive any single failed pass (I-24).
|
|
304
|
+
}
|
|
305
|
+
this.notify();
|
|
306
|
+
}
|
|
307
|
+
async ensureLease() {
|
|
308
|
+
if (this.lease.holds())
|
|
309
|
+
return true;
|
|
310
|
+
const now = this.now();
|
|
311
|
+
if (now - this.lastLeaseTry < this.timing.leaseRetryMs)
|
|
312
|
+
return false;
|
|
313
|
+
this.lastLeaseTry = now;
|
|
314
|
+
const got = await this.lease.acquire();
|
|
315
|
+
if (!got.ok)
|
|
316
|
+
return false;
|
|
317
|
+
this.leaseErrors = 0;
|
|
318
|
+
this.lastRun.clear();
|
|
319
|
+
this.lastRun.set('maintenance', now - this.timing.maintenanceMs + this.timing.maintenanceFirstMs);
|
|
320
|
+
if (this.timing.choresFirstRun === 'after_interval') {
|
|
321
|
+
for (const chore of ['recover', 'probation', 'prune', 'hook'])
|
|
322
|
+
this.lastRun.set(chore, now);
|
|
323
|
+
}
|
|
324
|
+
await this.events.emit('lease_acquired', { detail: got.takenOver ? 'takeover' : 'fresh' });
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
async every(name, ms, job) {
|
|
328
|
+
const last = this.lastRun.get(name);
|
|
329
|
+
const now = this.now();
|
|
330
|
+
if (last !== undefined && now - last < ms)
|
|
331
|
+
return;
|
|
332
|
+
this.lastRun.set(name, now);
|
|
333
|
+
try {
|
|
334
|
+
await job();
|
|
335
|
+
}
|
|
336
|
+
catch {
|
|
337
|
+
// Chores are best effort; the next interval tries again.
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
async housekeeping() {
|
|
341
|
+
const t = this.timing;
|
|
342
|
+
await this.every('recover', t.recoverMs, () => this.recoverStale());
|
|
343
|
+
await this.every('probation', t.probationMs, () => sweepProbation({ service: this.deps.service, library: this.deps.library, now: this.now }));
|
|
344
|
+
await this.every('maintenance', t.maintenanceMs, () => this.enqueueMaintenance());
|
|
345
|
+
await this.every('prune', t.pruneMs, async () => {
|
|
346
|
+
await this.deps.queue.prune();
|
|
347
|
+
await this.deps.runLog.prune();
|
|
348
|
+
});
|
|
349
|
+
if (this.deps.housekeeping !== undefined) {
|
|
350
|
+
const chore = this.deps.housekeeping;
|
|
351
|
+
await this.every('hook', t.housekeepingMs, () => chore());
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
async recoverStale() {
|
|
355
|
+
for (const item of await this.deps.queue.recoverStale()) {
|
|
356
|
+
const ev = item.to === 'dead' ? 'dead' : 'retry_scheduled';
|
|
357
|
+
await this.events.emit(ev, { proposalId: item.proposalId, detail: `claim_expired attempt=${item.attempt}` });
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
async enqueueMaintenance() {
|
|
361
|
+
const proposal = makeSystemProposal({ now: this.now, random: this.random }, {
|
|
362
|
+
kind: 'maintenance',
|
|
363
|
+
scope: { kind: 'user' },
|
|
364
|
+
why: 'Periodic library maintenance.',
|
|
365
|
+
expectedBenefit: 'Duplicates, idle genes and capacity are reviewed.',
|
|
366
|
+
});
|
|
367
|
+
await this.deps.queue.enqueue(proposal);
|
|
368
|
+
}
|
|
369
|
+
// ---- pausing ----------------------------------------------------------------
|
|
370
|
+
/** Returns whether to claim at all; sets the model block that only blocks model work. */
|
|
371
|
+
async evaluatePause() {
|
|
372
|
+
const verdict = await this.switches.check();
|
|
373
|
+
this.modelBlock = null;
|
|
374
|
+
this.budgetResetAt = undefined;
|
|
375
|
+
let reason;
|
|
376
|
+
let claim = true;
|
|
377
|
+
if (this.forced !== null) {
|
|
378
|
+
reason = this.forced;
|
|
379
|
+
claim = false;
|
|
380
|
+
}
|
|
381
|
+
else if (!verdict.on) {
|
|
382
|
+
reason = verdict.reason === 'redteam' ? 'redteam' : verdict.reason === 'learning_off' ? 'learn_off' : undefined;
|
|
383
|
+
claim = false;
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
reason = await this.computeModelBlock(verdict.config);
|
|
387
|
+
}
|
|
388
|
+
await this.transition(reason);
|
|
389
|
+
return claim;
|
|
390
|
+
}
|
|
391
|
+
async computeModelBlock(config) {
|
|
392
|
+
const until = this.now() + this.timing.blockedRetryMs;
|
|
393
|
+
const available = this.deps.model.available?.() ?? { ok: true };
|
|
394
|
+
if (!available.ok) {
|
|
395
|
+
this.modelBlock = { reason: 'no_fast_tier', until };
|
|
396
|
+
return 'no_fast_tier';
|
|
397
|
+
}
|
|
398
|
+
const view = this.breaker.view();
|
|
399
|
+
if (view.state === 'open') {
|
|
400
|
+
this.modelBlock = { reason: 'curator_failing', until: view.until ?? this.now() + BREAKER_OPEN_MS };
|
|
401
|
+
return 'curator_failing';
|
|
402
|
+
}
|
|
403
|
+
const daily = await this.budget.daily(config);
|
|
404
|
+
if (daily.exhausted) {
|
|
405
|
+
const resetAt = daily.resetAt ?? until;
|
|
406
|
+
this.budgetResetAt = resetAt;
|
|
407
|
+
this.modelBlock = { reason: 'daily_budget', until: resetAt };
|
|
408
|
+
return 'daily_budget';
|
|
409
|
+
}
|
|
410
|
+
return undefined;
|
|
411
|
+
}
|
|
412
|
+
/** A trial request in half-open state: the first model task gets it, the rest wait. */
|
|
413
|
+
modelBlockedNow() {
|
|
414
|
+
if (this.modelBlock !== null)
|
|
415
|
+
return this.modelBlock;
|
|
416
|
+
if (this.breaker.allow())
|
|
417
|
+
return null;
|
|
418
|
+
return { reason: 'curator_failing', until: this.now() + this.timing.blockedRetryMs };
|
|
419
|
+
}
|
|
420
|
+
async transition(next) {
|
|
421
|
+
if (next === this.paused)
|
|
422
|
+
return;
|
|
423
|
+
this.paused = next;
|
|
424
|
+
if (next === undefined) {
|
|
425
|
+
await this.events.emit('resumed');
|
|
426
|
+
}
|
|
427
|
+
else if (next === 'daily_budget') {
|
|
428
|
+
await this.events.emit('budget_exhausted', { detail: `daily_budget reset_at=${this.budgetResetAt ?? 0}` });
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
await this.events.emit('paused', { detail: next });
|
|
432
|
+
}
|
|
433
|
+
this.notify();
|
|
434
|
+
}
|
|
435
|
+
// ---- claiming and running -----------------------------------------------------
|
|
436
|
+
async refill() {
|
|
437
|
+
const config = await this.switches.effective();
|
|
438
|
+
const capacity = Math.max(1, config.curatorConcurrency);
|
|
439
|
+
const window = Math.max(this.timing.lookahead, capacity);
|
|
440
|
+
let importAllowed;
|
|
441
|
+
const skipImports = async (proposal) => {
|
|
442
|
+
if (proposal.origin.source !== 'import')
|
|
443
|
+
return false;
|
|
444
|
+
importAllowed ??= importClaimAllowed(await this.budget.daily(config));
|
|
445
|
+
return !importAllowed;
|
|
446
|
+
};
|
|
447
|
+
while (this.lanes.size() + this.inflight.size < window) {
|
|
448
|
+
const claimed = await this.deps.queue.claimNext(this.workerId, skipImports);
|
|
449
|
+
if (claimed === null)
|
|
450
|
+
return;
|
|
451
|
+
const proposal = claimed.item.proposal;
|
|
452
|
+
if (await this.dropIfExcluded(claimed))
|
|
453
|
+
continue;
|
|
454
|
+
this.lanes.add({
|
|
455
|
+
laneKey: scopeId(proposal.scope),
|
|
456
|
+
priority: agingPriority(proposal.priority, proposal.createdAt, this.now()),
|
|
457
|
+
createdAt: proposal.createdAt,
|
|
458
|
+
payload: claimed,
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Checkpoint one of two for the per-project switch (the publish gate is the other): an item
|
|
464
|
+
* whose source project is switched off is settled with its text stripped, never worked on.
|
|
465
|
+
* Does NOT touch the curator's own pause state - one project's opt-out is not a global pause.
|
|
466
|
+
*/
|
|
467
|
+
async dropIfExcluded(claimed) {
|
|
468
|
+
const origin = claimed.item.proposal.origin;
|
|
469
|
+
const verdict = await this.switches.checkFor({ projectKey: origin.projectKey ?? null, source: origin.source });
|
|
470
|
+
if (verdict.on)
|
|
471
|
+
return false;
|
|
472
|
+
const proposalId = claimed.item.proposal.id;
|
|
473
|
+
const ok = await this.deps.queue.dropExcluded(claimed);
|
|
474
|
+
await this.events.emit(ok ? 'skipped' : 'error', { proposalId, detail: ok ? 'project_excluded' : 'claim_lost' });
|
|
475
|
+
return true;
|
|
476
|
+
}
|
|
477
|
+
launch() {
|
|
478
|
+
const capacity = Math.max(1, this.deps.config().curatorConcurrency);
|
|
479
|
+
while (this.inflight.size < capacity) {
|
|
480
|
+
const entry = this.lanes.next();
|
|
481
|
+
if (entry === null)
|
|
482
|
+
return;
|
|
483
|
+
this.start1(entry.payload, entry.laneKey);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
start1(claimed, laneKey) {
|
|
487
|
+
const abort = new AbortController();
|
|
488
|
+
const proposalId = claimed.item.proposal.id;
|
|
489
|
+
const promise = this.processOne(claimed, abort.signal).finally(() => {
|
|
490
|
+
this.lanes.finish(laneKey);
|
|
491
|
+
this.inflight.delete(proposalId);
|
|
492
|
+
this.working = this.inflight.size > 0;
|
|
493
|
+
this.notify();
|
|
494
|
+
if (!this.disposed && this.started)
|
|
495
|
+
this.kick();
|
|
496
|
+
});
|
|
497
|
+
this.inflight.set(proposalId, { claimed, laneKey, abort, promise });
|
|
498
|
+
this.working = true;
|
|
499
|
+
this.notify();
|
|
500
|
+
}
|
|
501
|
+
async processOne(claimed, signal) {
|
|
502
|
+
let outcome;
|
|
503
|
+
try {
|
|
504
|
+
outcome = await runClaimedTask(this.runnerDeps(), claimed, signal);
|
|
505
|
+
}
|
|
506
|
+
catch {
|
|
507
|
+
this.breaker.recordInfraFailure('internal');
|
|
508
|
+
outcome = { kind: 'fail', failure: { kind: 'transient', error: 'internal' }, infra: true };
|
|
509
|
+
}
|
|
510
|
+
await this.settle(claimed, outcome);
|
|
511
|
+
}
|
|
512
|
+
async settle(claimed, outcome) {
|
|
513
|
+
const proposalId = claimed.item.proposal.id;
|
|
514
|
+
try {
|
|
515
|
+
if (outcome.kind === 'complete') {
|
|
516
|
+
const ok = await this.deps.queue.complete(claimed, { result: outcome.result, detail: outcome.detail });
|
|
517
|
+
if (!ok)
|
|
518
|
+
await this.events.emit('error', { proposalId, detail: 'claim_lost' });
|
|
519
|
+
}
|
|
520
|
+
else if (outcome.kind === 'fail') {
|
|
521
|
+
await this.settleFailure(claimed, outcome);
|
|
522
|
+
}
|
|
523
|
+
else if (outcome.kind === 'drop_excluded') {
|
|
524
|
+
const ok = await this.deps.queue.dropExcluded(claimed);
|
|
525
|
+
if (!ok)
|
|
526
|
+
await this.events.emit('error', { proposalId, detail: 'claim_lost' });
|
|
527
|
+
}
|
|
528
|
+
else {
|
|
529
|
+
await this.settleRelease(claimed, outcome);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
catch {
|
|
533
|
+
await this.events.emit('error', { proposalId, detail: 'settle_failed' });
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
async settleFailure(claimed, outcome) {
|
|
537
|
+
const proposalId = claimed.item.proposal.id;
|
|
538
|
+
const result = await this.deps.queue.fail(claimed, outcome.failure);
|
|
539
|
+
if (result === null) {
|
|
540
|
+
await this.events.emit('error', { proposalId, detail: 'claim_lost' });
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
if (outcome.failure.kind === 'budget') {
|
|
544
|
+
this.budgetResetAt = outcome.failure.resetAt;
|
|
545
|
+
this.modelBlock = { reason: 'daily_budget', until: outcome.failure.resetAt };
|
|
546
|
+
await this.transition('daily_budget');
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
const error = outcome.failure.kind === 'transient' ? outcome.failure.error : 'validation';
|
|
550
|
+
if (result.state === 'dead')
|
|
551
|
+
await this.events.emit('dead', { proposalId, detail: `${error} attempts=${result.attempt}` });
|
|
552
|
+
else
|
|
553
|
+
await this.events.emit('retry_scheduled', { proposalId, detail: `${error} attempt=${result.attempt}` });
|
|
554
|
+
}
|
|
555
|
+
async settleRelease(claimed, outcome) {
|
|
556
|
+
const proposalId = claimed.item.proposal.id;
|
|
557
|
+
const released = { ...claimed, item: { ...claimed.item, lastError: 'released' } };
|
|
558
|
+
await this.deps.queue.release(released, outcome.notBefore ?? 0);
|
|
559
|
+
await this.events.emit('skipped', { proposalId, detail: `released ${outcome.reason}` });
|
|
560
|
+
}
|
|
561
|
+
// ---- heartbeats and the lease ----------------------------------------------------
|
|
562
|
+
async claimBeat() {
|
|
563
|
+
try {
|
|
564
|
+
for (const task of this.inflight.values()) {
|
|
565
|
+
if (!(await this.deps.queue.heartbeat(task.claimed)))
|
|
566
|
+
task.abort.abort();
|
|
567
|
+
}
|
|
568
|
+
for (const entry of this.lanes.queuedEntries())
|
|
569
|
+
await this.deps.queue.heartbeat(entry.payload);
|
|
570
|
+
}
|
|
571
|
+
catch {
|
|
572
|
+
// A missed beat is retried in 15 s; the claim only expires after 3 minutes.
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
async leaseBeat() {
|
|
576
|
+
if (!this.lease.believesHeld())
|
|
577
|
+
return;
|
|
578
|
+
const outcome = await this.lease.refresh();
|
|
579
|
+
if (outcome === 'ok') {
|
|
580
|
+
this.leaseErrors = 0;
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
if (outcome === 'error') {
|
|
584
|
+
this.leaseErrors += 1;
|
|
585
|
+
if (this.leaseErrors < 3)
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
await this.onLeaseLost();
|
|
589
|
+
}
|
|
590
|
+
/** Someone else is (or may be) the curator: stop everything and hand the work back. */
|
|
591
|
+
async onLeaseLost() {
|
|
592
|
+
await this.events.emit('lease_lost', { detail: 'taken_over' });
|
|
593
|
+
for (const task of this.inflight.values())
|
|
594
|
+
task.abort.abort();
|
|
595
|
+
await this.releaseWaiting();
|
|
596
|
+
this.notify();
|
|
597
|
+
}
|
|
598
|
+
async releaseWaiting() {
|
|
599
|
+
for (const entry of this.lanes.drain()) {
|
|
600
|
+
const claimed = entry.payload;
|
|
601
|
+
const released = { ...claimed, item: { ...claimed.item, lastError: 'released' } };
|
|
602
|
+
await this.deps.queue.release(released, 0).catch(() => false);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
async giveUpLease(detail) {
|
|
606
|
+
if (!this.lease.believesHeld())
|
|
607
|
+
return;
|
|
608
|
+
await this.events.emit('lease_lost', { detail });
|
|
609
|
+
await this.events.drain();
|
|
610
|
+
await this.lease.release();
|
|
611
|
+
}
|
|
612
|
+
cancelTimers() {
|
|
613
|
+
for (const cancel of this.cancels)
|
|
614
|
+
cancel();
|
|
615
|
+
this.cancels = [];
|
|
616
|
+
}
|
|
617
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { GeneDep, GeneScope, GeneStatus } from '@aragon-agent/tui/model/gene/types';
|
|
2
|
+
export interface DependencyRow {
|
|
3
|
+
id: string;
|
|
4
|
+
scope: GeneScope;
|
|
5
|
+
status: GeneStatus;
|
|
6
|
+
deps: readonly GeneDep[];
|
|
7
|
+
pinned?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface DependencyCause {
|
|
10
|
+
id: string;
|
|
11
|
+
version: number;
|
|
12
|
+
kind: 'archived' | 'deleted' | 'revoked' | 'broken' | 'interface';
|
|
13
|
+
}
|
|
14
|
+
export interface RecheckFlag {
|
|
15
|
+
id: string;
|
|
16
|
+
scope: GeneScope;
|
|
17
|
+
/** `dependency:<causeId>@<version>` - what `healthReason` is set to. */
|
|
18
|
+
reason: string;
|
|
19
|
+
pinned: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface DependencyImpact {
|
|
22
|
+
flags: RecheckFlag[];
|
|
23
|
+
/** Dependents beyond the fan-out cap, folded into one maintenance request. */
|
|
24
|
+
overflow: {
|
|
25
|
+
count: number;
|
|
26
|
+
ids: string[];
|
|
27
|
+
} | null;
|
|
28
|
+
}
|
|
29
|
+
export interface ImpactOptions {
|
|
30
|
+
cap?: number;
|
|
31
|
+
transitive?: boolean;
|
|
32
|
+
}
|
|
33
|
+
export declare function dependencyReason(cause: Pick<DependencyCause, 'id' | 'version'>): string;
|
|
34
|
+
export declare function planDependencyImpact(rows: readonly DependencyRow[], cause: DependencyCause, options?: ImpactOptions): DependencyImpact;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependency impact: which dependents a change must flag for `recheck`
|
|
3
|
+
* (gene-bank spec 10.9; RV-6; I-7, I-22).
|
|
4
|
+
*
|
|
5
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`. PURE graph planning over
|
|
6
|
+
* index rows: nothing is read or written here.
|
|
7
|
+
*
|
|
8
|
+
* WHEN A GENE STOPS BEING WHAT ITS DEPENDENTS WERE WRITTEN AGAINST (archived, deleted, revoked,
|
|
9
|
+
* broken, or a tool whose `interfaceVersion` moved), every PUBLISHED gene that `requires` it
|
|
10
|
+
* gets `health: 'recheck'` with the reason `dependency:<id>@<version>`, and a `recheck` proposal.
|
|
11
|
+
* Candidates, rejected, archived and revoked dependents are skipped: they are not served, and a
|
|
12
|
+
* candidate meets S2 when it is validated.
|
|
13
|
+
*
|
|
14
|
+
* FAN-OUT IS BOUNDED. At most `dependentFanoutMax` (50) dependents are flagged one by one; the
|
|
15
|
+
* rest are folded into ONE `maintenance` request that carries their ids, so a library-wide
|
|
16
|
+
* dependency on one popular tool does not flood the queue with thousands of rechecks.
|
|
17
|
+
*
|
|
18
|
+
* PINNED DEPENDENTS ARE FLAGGED TOO (I-7): flagging is a health mark, which is the one thing that
|
|
19
|
+
* is allowed to touch a pinned gene. The flag carries `pinned` so the consumer can avoid ever
|
|
20
|
+
* pairing the mark with a revoke or archive.
|
|
21
|
+
*
|
|
22
|
+
* DIRECT BY DEFAULT. A gene is broken by what it names, not by what its dependencies name; a
|
|
23
|
+
* dependent that turns out broken produces its own notice and the cascade continues from there.
|
|
24
|
+
* `transitive: true` is available for a planner that wants the whole blast radius at once.
|
|
25
|
+
*/
|
|
26
|
+
import { GENE_LIMITS } from '@aragon-agent/tui/model/gene/limits';
|
|
27
|
+
import { buildReverseIndex, transitiveDependents } from '@aragon-agent/tui/model/gene/deps';
|
|
28
|
+
/** The overflow list is itself bounded; a runaway library must not grow a proposal without end. */
|
|
29
|
+
const OVERFLOW_IDS_MAX = 1000;
|
|
30
|
+
export function dependencyReason(cause) {
|
|
31
|
+
return `dependency:${cause.id}@${cause.version}`;
|
|
32
|
+
}
|
|
33
|
+
export function planDependencyImpact(rows, cause, options = {}) {
|
|
34
|
+
const cap = Math.max(0, options.cap ?? GENE_LIMITS.dependentFanoutMax);
|
|
35
|
+
const published = rows.filter((r) => r.status === 'published' && r.id !== cause.id);
|
|
36
|
+
const reverse = buildReverseIndex(published);
|
|
37
|
+
const all = options.transitive
|
|
38
|
+
? transitiveDependents(reverse, cause.id, Number.MAX_SAFE_INTEGER).ids
|
|
39
|
+
: [...(reverse.get(cause.id) ?? [])];
|
|
40
|
+
const byId = new Map(published.map((r) => [r.id, r]));
|
|
41
|
+
const ordered = all.filter((id) => byId.has(id));
|
|
42
|
+
const flags = ordered.slice(0, cap).map((id) => {
|
|
43
|
+
const row = byId.get(id);
|
|
44
|
+
return { id, scope: row.scope, reason: dependencyReason(cause), pinned: row.pinned === true };
|
|
45
|
+
});
|
|
46
|
+
const rest = ordered.slice(cap);
|
|
47
|
+
return { flags, overflow: rest.length === 0 ? null : { count: rest.length, ids: rest.slice(0, OVERFLOW_IDS_MAX) } };
|
|
48
|
+
}
|