@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,379 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The deterministic executors: work the curator does WITHOUT a model (gene-bank spec 10.9, 10.10,
|
|
3
|
+
* 10.7.3, 10.5.5; I-7, I-8, I-11, I-25).
|
|
4
|
+
*
|
|
5
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
6
|
+
*
|
|
7
|
+
* `executeDirect` carries out a `DirectAction` that `planTask` chose:
|
|
8
|
+
*
|
|
9
|
+
* revoke withdraw the gene (the harmful flag from the attributions becomes a
|
|
10
|
+
* tombstone so the same idea is not learned again).
|
|
11
|
+
* archive move a low-value gene to the archive.
|
|
12
|
+
* invalidate a feedback report says the gene is outdated or misleading: if it is on
|
|
13
|
+
* probation and has now hurt twice, roll it back (or revoke it); otherwise
|
|
14
|
+
* mark it `recheck` so retrieval trusts it less until someone looks.
|
|
15
|
+
* recheck re-run the deterministic checks of the PUBLISHED version and set the
|
|
16
|
+
* health mark; never revokes or archives, and for a pinned gene the mark is
|
|
17
|
+
* all there is (I-7).
|
|
18
|
+
* promote_suggest a project gene earned a wider scope. Only a person may widen (I-11): this
|
|
19
|
+
* records the suggestion as `needs_human` and nothing more.
|
|
20
|
+
* maintenance plan the library pass and ENQUEUE its findings as ordinary proposals
|
|
21
|
+
* (merges, archives, promotions); the pass itself changes nothing.
|
|
22
|
+
* resume_validation a candidate from this proposal exists (an earlier run died): validate it
|
|
23
|
+
* and carry on to publish or reject - never write a second candidate.
|
|
24
|
+
* resume_merge the survivor of this merge is written; finish archiving the members.
|
|
25
|
+
*
|
|
26
|
+
* AN EXECUTOR RETURNS A VERDICT, NOT AN EXCEPTION. Every commit failure is mapped to a queue
|
|
27
|
+
* outcome by `outcomeOfFailure`: a moved basis is superseded, a missing gene is `target_gone`, a
|
|
28
|
+
* pinned gene is `needs_human`, and only an infrastructure failure (a lock timeout, an
|
|
29
|
+
* unavailable library) comes back as a retryable `failure`, which is what the circuit breaker
|
|
30
|
+
* counts.
|
|
31
|
+
*/
|
|
32
|
+
import { dedupeKeyOf, priorityOf } from '@aragon-agent/tui/model/gene/proposal';
|
|
33
|
+
import { mintProposalId } from '@aragon-agent/tui/model/gene/ids';
|
|
34
|
+
import { planMaintenance } from './maintenance.js';
|
|
35
|
+
import { applyRollback, decideProbation } from './probation.js';
|
|
36
|
+
import { recheckGene } from './recheck.js';
|
|
37
|
+
import { publishCandidate, releaseEnvOf, revokeGene, settleLeftovers } from './tools-release.js';
|
|
38
|
+
import { flagNeedsHuman, runCandidateValidation } from './tools-verify.js';
|
|
39
|
+
import { CURATOR_ACTOR } from './tool-support.js';
|
|
40
|
+
const INFRA_CODES = new Set([
|
|
41
|
+
'GENE_LOCK_TIMEOUT',
|
|
42
|
+
'GENE_UNAVAILABLE',
|
|
43
|
+
'GENE_INDEX_REBUILDING',
|
|
44
|
+
]);
|
|
45
|
+
const MAINTENANCE_PROPOSAL_CAP = 10;
|
|
46
|
+
/** Is this commit failure the infrastructure's fault (worth a retry) rather than a verdict? */
|
|
47
|
+
export function isInfraFailure(code) {
|
|
48
|
+
return INFRA_CODES.has(code);
|
|
49
|
+
}
|
|
50
|
+
/** A commit failure as a queue outcome. */
|
|
51
|
+
export function outcomeOfFailure(failure) {
|
|
52
|
+
switch (failure.code) {
|
|
53
|
+
case 'GENE_STALE_BASIS':
|
|
54
|
+
return { result: 'superseded', detail: 'stale_basis', event: 'superseded' };
|
|
55
|
+
case 'GENE_NOT_FOUND':
|
|
56
|
+
return { result: 'target_gone', detail: 'missing', event: 'skipped' };
|
|
57
|
+
case 'GENE_PINNED':
|
|
58
|
+
return { result: 'needs_human', detail: 'pinned', event: 'needs_human' };
|
|
59
|
+
case 'GENE_CAP_PINNED_FULL':
|
|
60
|
+
return { result: 'needs_human', detail: 'cap_pinned_full', event: 'needs_human' };
|
|
61
|
+
case 'GENE_OVERSIGHT_HUMAN_ONLY':
|
|
62
|
+
return { result: 'rejected', detail: 'oversight_human_only', event: 'rejected' };
|
|
63
|
+
case 'GENE_FORGOTTEN':
|
|
64
|
+
return { result: 'forgotten', detail: 'forgotten', event: 'skipped' };
|
|
65
|
+
default:
|
|
66
|
+
if (isInfraFailure(failure.code)) {
|
|
67
|
+
return { result: 'done', detail: failure.code, failure: { kind: 'transient', error: failure.code } };
|
|
68
|
+
}
|
|
69
|
+
return { result: 'rejected', detail: `commit_${failure.code.toLowerCase()}`, event: 'rejected' };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** A commit that parked itself for a person (`needsHuman`) wrote nothing: the verdict is theirs. */
|
|
73
|
+
function fromCommit(done, okResult) {
|
|
74
|
+
if (!done.ok)
|
|
75
|
+
return outcomeOfFailure(done);
|
|
76
|
+
if (done.value.needsHuman === true) {
|
|
77
|
+
return { result: 'needs_human', detail: 'conflict_with_human', ...(okResult.geneId ? { geneId: okResult.geneId } : {}) };
|
|
78
|
+
}
|
|
79
|
+
return okResult;
|
|
80
|
+
}
|
|
81
|
+
// ---------------------------------------------------------------------------
|
|
82
|
+
// revoke / archive / invalidate / recheck
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
async function runRevoke(deps, id) {
|
|
85
|
+
const { proposal } = deps.env.ctx;
|
|
86
|
+
const harmful = (proposal.attributions ?? []).some((a) => a.geneId === id && a.effect === 'harmed');
|
|
87
|
+
const done = await revokeGene(releaseEnvOf(deps.env), {
|
|
88
|
+
id,
|
|
89
|
+
reason: harmful ? 'Revoked after it harmed a run.' : 'Revoked on request.',
|
|
90
|
+
harmful,
|
|
91
|
+
});
|
|
92
|
+
if (done.ok)
|
|
93
|
+
deps.env.ctx.wrote = true;
|
|
94
|
+
return fromCommit(done, { result: 'revoked', detail: 'revoked', geneId: id });
|
|
95
|
+
}
|
|
96
|
+
async function runArchive(deps, id) {
|
|
97
|
+
const { ctx } = deps.env;
|
|
98
|
+
const read = await deps.env.deps.library.readHead(ctx.scope, id);
|
|
99
|
+
if (read === null)
|
|
100
|
+
return { result: 'target_gone', detail: 'missing', event: 'skipped', geneId: id };
|
|
101
|
+
const done = await deps.env.deps.service.commit({
|
|
102
|
+
op: 'archive',
|
|
103
|
+
scope: ctx.scope,
|
|
104
|
+
id,
|
|
105
|
+
reason: 'Archived by the curator after a maintenance suggestion.',
|
|
106
|
+
proposalId: ctx.proposal.id,
|
|
107
|
+
}, CURATOR_ACTOR, { version: ctx.basis.get(id) ?? read.head.latestVersion });
|
|
108
|
+
if (done.ok)
|
|
109
|
+
ctx.wrote = true;
|
|
110
|
+
return fromCommit(done, { result: 'archived', detail: 'archived', geneId: id });
|
|
111
|
+
}
|
|
112
|
+
async function runInvalidate(deps, id) {
|
|
113
|
+
const { env } = deps;
|
|
114
|
+
const read = await env.deps.library.readHead(env.ctx.scope, id);
|
|
115
|
+
if (read === null)
|
|
116
|
+
return { result: 'target_gone', detail: 'missing', event: 'skipped', geneId: id };
|
|
117
|
+
const decision = await decideProbation({ library: env.deps.library }, read.head, env.deps.now());
|
|
118
|
+
if (decision.action === 'rollback' && !read.head.pinned) {
|
|
119
|
+
const done = await applyRollback(releaseEnvOf(env), read.head, decision);
|
|
120
|
+
if (done.ok)
|
|
121
|
+
env.ctx.wrote = true;
|
|
122
|
+
return fromCommit(done, { result: 'done', detail: `probation_${decision.to}`, geneId: id });
|
|
123
|
+
}
|
|
124
|
+
if (read.head.health === 'recheck')
|
|
125
|
+
return { result: 'no_change', detail: 'already_flagged', event: 'skipped', geneId: id };
|
|
126
|
+
const done = await env.deps.service.commit({ op: 'setHealth', scope: env.ctx.scope, id, health: 'recheck', healthReason: 'invalidated' }, CURATOR_ACTOR);
|
|
127
|
+
if (done.ok)
|
|
128
|
+
env.ctx.wrote = true;
|
|
129
|
+
return fromCommit(done, { result: 'done', detail: 'flagged_recheck', geneId: id });
|
|
130
|
+
}
|
|
131
|
+
async function scriptTextFor(env, id, version) {
|
|
132
|
+
const exec = version.content.payload.exec;
|
|
133
|
+
if (exec === undefined || exec.kind !== 'script')
|
|
134
|
+
return undefined;
|
|
135
|
+
return (await env.deps.library.readEvidence(env.ctx.scope, id, exec.evidence))?.text;
|
|
136
|
+
}
|
|
137
|
+
/** The published head and its served version, or null when there is nothing to recheck. */
|
|
138
|
+
async function publishedPair(env, id) {
|
|
139
|
+
const read = await env.deps.library.readHead(env.ctx.scope, id);
|
|
140
|
+
const n = read?.head.currentVersion ?? null;
|
|
141
|
+
const served = n === null ? null : await env.deps.library.readVersion(env.ctx.scope, id, n);
|
|
142
|
+
if (read === null || served === null || read.head.status !== 'published')
|
|
143
|
+
return null;
|
|
144
|
+
return { head: read.head, served };
|
|
145
|
+
}
|
|
146
|
+
async function runRecheck(deps, id) {
|
|
147
|
+
const { env } = deps;
|
|
148
|
+
const pair = await publishedPair(env, id);
|
|
149
|
+
if (pair === null) {
|
|
150
|
+
return { result: 'no_change', detail: 'not_published', event: 'skipped', geneId: id };
|
|
151
|
+
}
|
|
152
|
+
const { head, served } = pair;
|
|
153
|
+
const scriptText = await scriptTextFor(env, id, served);
|
|
154
|
+
const outcome = await recheckGene({
|
|
155
|
+
ports: await env.deps.validatorPorts(env.ctx),
|
|
156
|
+
head,
|
|
157
|
+
version: served,
|
|
158
|
+
...(scriptText === undefined ? {} : { scriptText }),
|
|
159
|
+
...(env.deps.probeDeps === undefined ? {} : { probeDeps: env.deps.probeDeps }),
|
|
160
|
+
});
|
|
161
|
+
const detail = `recheck_${outcome.health}`;
|
|
162
|
+
if (outcome.health === head.health && outcome.healthReason === head.healthReason) {
|
|
163
|
+
return { result: 'done', detail, event: 'skipped', geneId: id };
|
|
164
|
+
}
|
|
165
|
+
const done = await env.deps.service.commit({
|
|
166
|
+
op: 'setHealth',
|
|
167
|
+
scope: env.ctx.scope,
|
|
168
|
+
id,
|
|
169
|
+
health: outcome.health,
|
|
170
|
+
...(outcome.healthReason === undefined ? {} : { healthReason: outcome.healthReason }),
|
|
171
|
+
}, CURATOR_ACTOR);
|
|
172
|
+
if (done.ok)
|
|
173
|
+
env.ctx.wrote = true;
|
|
174
|
+
// A mark back to ok is not logged by the commit (only a flag is), so that record is ours.
|
|
175
|
+
const own = outcome.health === 'ok' ? { event: 'skipped' } : {};
|
|
176
|
+
return fromCommit(done, { result: 'done', detail, geneId: id, ...own });
|
|
177
|
+
}
|
|
178
|
+
/** A proposal the curator makes for itself (maintenance findings, the periodic pass). */
|
|
179
|
+
export function makeSystemProposal(clock, parts) {
|
|
180
|
+
const base = {
|
|
181
|
+
schema: 1,
|
|
182
|
+
id: mintProposalId(clock.random),
|
|
183
|
+
createdAt: clock.now(),
|
|
184
|
+
origin: { source: 'system', sessionId: 'curator', runId: 'maintenance', goal: 'Library maintenance.' },
|
|
185
|
+
evidence: [],
|
|
186
|
+
priority: priorityOf({ kind: parts.kind }),
|
|
187
|
+
dedupeKey: '',
|
|
188
|
+
merged: 0,
|
|
189
|
+
...parts,
|
|
190
|
+
};
|
|
191
|
+
const targetId = base.target?.id ?? base.mergeInto;
|
|
192
|
+
base.dedupeKey = dedupeKeyOf({
|
|
193
|
+
kind: base.kind,
|
|
194
|
+
scope: base.scope,
|
|
195
|
+
...(targetId === undefined ? {} : { targetId }),
|
|
196
|
+
basisVersion: base.target?.basisVersion ?? null,
|
|
197
|
+
});
|
|
198
|
+
return base;
|
|
199
|
+
}
|
|
200
|
+
function systemProposal(deps, parts) {
|
|
201
|
+
return makeSystemProposal({ now: deps.env.deps.now, random: deps.random }, parts);
|
|
202
|
+
}
|
|
203
|
+
async function headVersion(deps, scope, id) {
|
|
204
|
+
return (await deps.env.deps.library.readHead(scope, id).catch(() => null))?.head.latestVersion ?? null;
|
|
205
|
+
}
|
|
206
|
+
async function enqueueFindings(deps) {
|
|
207
|
+
const { library } = deps.env.deps;
|
|
208
|
+
const snapshot = await library.maintenanceSnapshot();
|
|
209
|
+
const plan = planMaintenance({
|
|
210
|
+
rows: snapshot.rows,
|
|
211
|
+
stats: snapshot.stats,
|
|
212
|
+
now: deps.env.deps.now(),
|
|
213
|
+
capPerType: deps.capPerType,
|
|
214
|
+
});
|
|
215
|
+
let merges = 0;
|
|
216
|
+
for (const m of plan.merges.slice(0, MAINTENANCE_PROPOSAL_CAP)) {
|
|
217
|
+
const basis = await headVersion(deps, m.scope, m.survivor);
|
|
218
|
+
const proposal = systemProposal(deps, {
|
|
219
|
+
kind: 'merge',
|
|
220
|
+
type: m.type,
|
|
221
|
+
scope: m.scope,
|
|
222
|
+
mergeInto: m.survivor,
|
|
223
|
+
mergeFrom: m.from,
|
|
224
|
+
...(basis === null ? {} : { target: { id: m.survivor, basisVersion: basis } }),
|
|
225
|
+
why: 'Near-duplicate genes were found by the maintenance pass.',
|
|
226
|
+
expectedBenefit: 'A smaller library with one clear gene instead of several.',
|
|
227
|
+
});
|
|
228
|
+
if ((await deps.queue.enqueue(proposal)).ok)
|
|
229
|
+
merges += 1;
|
|
230
|
+
}
|
|
231
|
+
let archives = 0;
|
|
232
|
+
for (const a of plan.archives.slice(0, MAINTENANCE_PROPOSAL_CAP)) {
|
|
233
|
+
const row = snapshot.rows.find((r) => r.id === a.id);
|
|
234
|
+
if (row === undefined)
|
|
235
|
+
continue;
|
|
236
|
+
const proposal = systemProposal(deps, {
|
|
237
|
+
kind: 'archive',
|
|
238
|
+
type: row.type,
|
|
239
|
+
scope: row.scope,
|
|
240
|
+
target: { id: a.id, basisVersion: row.v },
|
|
241
|
+
why: 'The gene has been idle and has a low retention value.',
|
|
242
|
+
expectedBenefit: 'Room for genes that are used.',
|
|
243
|
+
});
|
|
244
|
+
if ((await deps.queue.enqueue(proposal)).ok)
|
|
245
|
+
archives += 1;
|
|
246
|
+
}
|
|
247
|
+
let promotions = 0;
|
|
248
|
+
for (const p of plan.promotions.slice(0, MAINTENANCE_PROPOSAL_CAP)) {
|
|
249
|
+
const proposal = systemProposal(deps, {
|
|
250
|
+
kind: 'promote_suggest',
|
|
251
|
+
scope: p.from,
|
|
252
|
+
target: { id: p.id, basisVersion: null },
|
|
253
|
+
why: 'The gene was adopted effectively in several sessions.',
|
|
254
|
+
expectedBenefit: 'Other projects could use it too.',
|
|
255
|
+
});
|
|
256
|
+
if ((await deps.queue.enqueue(proposal)).ok)
|
|
257
|
+
promotions += 1;
|
|
258
|
+
}
|
|
259
|
+
return { merges, archives, promotions };
|
|
260
|
+
}
|
|
261
|
+
async function runMaintenance(deps) {
|
|
262
|
+
const found = await enqueueFindings(deps);
|
|
263
|
+
return {
|
|
264
|
+
result: 'done',
|
|
265
|
+
detail: `maintenance merges=${found.merges} archives=${found.archives} promotions=${found.promotions}`,
|
|
266
|
+
event: 'skipped',
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
// ---------------------------------------------------------------------------
|
|
270
|
+
// resume
|
|
271
|
+
// ---------------------------------------------------------------------------
|
|
272
|
+
async function resumeValidation(deps, action) {
|
|
273
|
+
const { env } = deps;
|
|
274
|
+
const ref = action.candidate;
|
|
275
|
+
const read = await env.deps.library.readHead(ref.scope, ref.id);
|
|
276
|
+
const version = read?.head.candidateVersion == null ? null : await env.deps.library.readVersion(ref.scope, ref.id, read.head.candidateVersion);
|
|
277
|
+
if (read === null || version === null)
|
|
278
|
+
return { result: 'target_gone', detail: 'candidate_missing', event: 'skipped', geneId: ref.id };
|
|
279
|
+
env.ctx.candidates.set(ref.id, { ...ref, version: version.n });
|
|
280
|
+
const current = read.head.currentVersion === null ? null : await env.deps.library.readVersion(ref.scope, ref.id, read.head.currentVersion);
|
|
281
|
+
const stored = await runCandidateValidation(env, {
|
|
282
|
+
type: read.head.type,
|
|
283
|
+
scope: ref.scope,
|
|
284
|
+
geneId: ref.id,
|
|
285
|
+
version: version.n,
|
|
286
|
+
contentHash: version.contentHash,
|
|
287
|
+
content: version.content,
|
|
288
|
+
head: read.head,
|
|
289
|
+
...(current === null ? {} : { headContent: current.content }),
|
|
290
|
+
});
|
|
291
|
+
await env.deps.events.emit('validating', { proposalId: env.ctx.proposal.id, geneId: ref.id, version: version.n, scope: ref.scope, type: ref.type, detail: 'resumed' });
|
|
292
|
+
return afterResumedValidation(deps, ref.id, stored.outcome.verdict, stored.outcome.needsHuman);
|
|
293
|
+
}
|
|
294
|
+
async function afterResumedValidation(deps, id, verdict, needsHuman) {
|
|
295
|
+
const { env } = deps;
|
|
296
|
+
if (verdict === 'pass') {
|
|
297
|
+
const published = await publishCandidate(env, { id });
|
|
298
|
+
if (published.kind === 'published')
|
|
299
|
+
return { result: 'published', detail: 'resumed', geneId: id };
|
|
300
|
+
if (published.kind === 'needs_human')
|
|
301
|
+
return { result: 'needs_human', detail: published.code.toLowerCase(), geneId: id };
|
|
302
|
+
return { result: 'rejected', detail: published.code.toLowerCase(), event: 'rejected', geneId: id };
|
|
303
|
+
}
|
|
304
|
+
if (verdict === 'needs_human' && needsHuman !== undefined) {
|
|
305
|
+
const head = (await env.deps.library.readHead(env.ctx.scope, id))?.head;
|
|
306
|
+
if (head !== undefined)
|
|
307
|
+
await flagNeedsHuman(env, head, needsHuman);
|
|
308
|
+
return { result: 'needs_human', detail: needsHuman, geneId: id };
|
|
309
|
+
}
|
|
310
|
+
await settleLeftovers(env, verdict === 'convert' ? 'reject_all' : 'reject_failed');
|
|
311
|
+
return { result: 'rejected', detail: verdict === 'convert' ? 'conflict_convert' : 'validation_failed', geneId: id };
|
|
312
|
+
}
|
|
313
|
+
async function resumeMerge(deps, action) {
|
|
314
|
+
const { env } = deps;
|
|
315
|
+
const { ctx } = env;
|
|
316
|
+
const into = action.into.head;
|
|
317
|
+
const survivor = await env.deps.library.readVersion(ctx.scope, into.id, into.latestVersion);
|
|
318
|
+
if (survivor === null)
|
|
319
|
+
return { result: 'target_gone', detail: 'merge_target', event: 'skipped', geneId: into.id };
|
|
320
|
+
const from = ctx.proposal.mergeFrom ?? action.remaining;
|
|
321
|
+
const fromBasis = {};
|
|
322
|
+
for (const id of action.remaining)
|
|
323
|
+
fromBasis[id] = ctx.basis.get(id) ?? 0;
|
|
324
|
+
const done = await env.deps.service.commit({
|
|
325
|
+
op: 'merge',
|
|
326
|
+
scope: ctx.scope,
|
|
327
|
+
into: into.id,
|
|
328
|
+
from,
|
|
329
|
+
content: survivor.content,
|
|
330
|
+
reason: 'Resumed an interrupted merge.',
|
|
331
|
+
proposalId: ctx.proposal.id,
|
|
332
|
+
...(Object.keys(fromBasis).length > 0 && !Object.values(fromBasis).includes(0) ? { fromBasis } : {}),
|
|
333
|
+
}, CURATOR_ACTOR, { version: into.latestVersion });
|
|
334
|
+
if (done.ok)
|
|
335
|
+
ctx.wrote = true;
|
|
336
|
+
return fromCommit(done, { result: 'merged', detail: 'resumed', geneId: into.id });
|
|
337
|
+
}
|
|
338
|
+
// ---------------------------------------------------------------------------
|
|
339
|
+
// entry point
|
|
340
|
+
// ---------------------------------------------------------------------------
|
|
341
|
+
const ON_TARGET = {
|
|
342
|
+
revoke: runRevoke,
|
|
343
|
+
archive: runArchive,
|
|
344
|
+
invalidate: runInvalidate,
|
|
345
|
+
recheck: runRecheck,
|
|
346
|
+
};
|
|
347
|
+
function suggestion(deps) {
|
|
348
|
+
const target = deps.env.ctx.proposal.target;
|
|
349
|
+
return {
|
|
350
|
+
result: 'needs_human',
|
|
351
|
+
detail: 'promote_suggest',
|
|
352
|
+
event: 'needs_human',
|
|
353
|
+
...(target === undefined ? {} : { geneId: target.id }),
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
function runAction(deps, action) {
|
|
357
|
+
switch (action.kind) {
|
|
358
|
+
case 'promote_suggest':
|
|
359
|
+
return Promise.resolve(suggestion(deps));
|
|
360
|
+
case 'maintenance':
|
|
361
|
+
return runMaintenance(deps);
|
|
362
|
+
case 'resume_validation':
|
|
363
|
+
return resumeValidation(deps, action);
|
|
364
|
+
case 'resume_merge':
|
|
365
|
+
return resumeMerge(deps, action);
|
|
366
|
+
default:
|
|
367
|
+
return ON_TARGET[action.kind](deps, action.target.head.id);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
/** Run a deterministic action. Never throws: an unexpected error is an infrastructure failure. */
|
|
371
|
+
export async function executeDirect(deps, action) {
|
|
372
|
+
try {
|
|
373
|
+
return await runAction(deps, action);
|
|
374
|
+
}
|
|
375
|
+
catch {
|
|
376
|
+
const failure = { kind: 'transient', error: 'executor_error' };
|
|
377
|
+
return { result: 'done', detail: 'executor_error', failure };
|
|
378
|
+
}
|
|
379
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lanes: serial inside a scope, fair between scopes (gene-bank spec 10.3, 10.4, I-21).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* A LANE IS A PROPOSAL'S SCOPE (`user` or `project:<key>`). Two tasks in one lane would race on
|
|
7
|
+
* the same genes (a create and a merge of its duplicate, two revisions of one gene), so a lane
|
|
8
|
+
* runs one task at a time. Between lanes the order ROTATES: after a lane has been served the
|
|
9
|
+
* cursor moves past it, so a project with five hundred queued items cannot starve a project with
|
|
10
|
+
* one. Inside a lane the order is the queue's own (aging priority, then age).
|
|
11
|
+
*
|
|
12
|
+
* PURE DATA STRUCTURE. No timers, no disk: the host claims items from the queue into the lanes
|
|
13
|
+
* (a bounded look-ahead, because the queue can only hand out its globally best item) and asks
|
|
14
|
+
* `next()` for the one to run.
|
|
15
|
+
*/
|
|
16
|
+
export interface LaneEntry<T> {
|
|
17
|
+
laneKey: string;
|
|
18
|
+
/** Smaller runs first INSIDE the lane (the queue's aged priority). */
|
|
19
|
+
priority: number;
|
|
20
|
+
createdAt: number;
|
|
21
|
+
payload: T;
|
|
22
|
+
}
|
|
23
|
+
export declare class LaneScheduler<T> {
|
|
24
|
+
private readonly queued;
|
|
25
|
+
private readonly running;
|
|
26
|
+
/** Lane keys in first-seen order; the rotation walks this list. */
|
|
27
|
+
private order;
|
|
28
|
+
private cursor;
|
|
29
|
+
private seq;
|
|
30
|
+
add(entry: LaneEntry<T>): void;
|
|
31
|
+
/**
|
|
32
|
+
* The next entry to run: the head of the first idle lane at or after the cursor that has
|
|
33
|
+
* work. Marks that lane running until `finish`. Null when every non-empty lane is busy.
|
|
34
|
+
*/
|
|
35
|
+
next(): LaneEntry<T> | null;
|
|
36
|
+
/** The lane's task is over; the lane may serve again. Empty idle lanes are forgotten. */
|
|
37
|
+
finish(laneKey: string): void;
|
|
38
|
+
private forgetIfIdle;
|
|
39
|
+
/** Entries waiting (not running). */
|
|
40
|
+
size(): number;
|
|
41
|
+
runningCount(): number;
|
|
42
|
+
isRunning(laneKey: string): boolean;
|
|
43
|
+
/** Every waiting entry (the host heartbeats their claims while they wait). */
|
|
44
|
+
queuedEntries(): LaneEntry<T>[];
|
|
45
|
+
/** Waiting entries per lane, in rotation order. For status and tests. */
|
|
46
|
+
waiting(): Array<{
|
|
47
|
+
laneKey: string;
|
|
48
|
+
count: number;
|
|
49
|
+
}>;
|
|
50
|
+
/** Remove and return everything that has not started (dispose hands it back to the queue). */
|
|
51
|
+
drain(): LaneEntry<T>[];
|
|
52
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lanes: serial inside a scope, fair between scopes (gene-bank spec 10.3, 10.4, I-21).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* A LANE IS A PROPOSAL'S SCOPE (`user` or `project:<key>`). Two tasks in one lane would race on
|
|
7
|
+
* the same genes (a create and a merge of its duplicate, two revisions of one gene), so a lane
|
|
8
|
+
* runs one task at a time. Between lanes the order ROTATES: after a lane has been served the
|
|
9
|
+
* cursor moves past it, so a project with five hundred queued items cannot starve a project with
|
|
10
|
+
* one. Inside a lane the order is the queue's own (aging priority, then age).
|
|
11
|
+
*
|
|
12
|
+
* PURE DATA STRUCTURE. No timers, no disk: the host claims items from the queue into the lanes
|
|
13
|
+
* (a bounded look-ahead, because the queue can only hand out its globally best item) and asks
|
|
14
|
+
* `next()` for the one to run.
|
|
15
|
+
*/
|
|
16
|
+
export class LaneScheduler {
|
|
17
|
+
queued = new Map();
|
|
18
|
+
running = new Set();
|
|
19
|
+
/** Lane keys in first-seen order; the rotation walks this list. */
|
|
20
|
+
order = [];
|
|
21
|
+
cursor = 0;
|
|
22
|
+
seq = 0;
|
|
23
|
+
add(entry) {
|
|
24
|
+
const lane = this.queued.get(entry.laneKey) ?? [];
|
|
25
|
+
if (!this.order.includes(entry.laneKey))
|
|
26
|
+
this.order.push(entry.laneKey);
|
|
27
|
+
lane.push({ ...entry, seq: (this.seq += 1) });
|
|
28
|
+
lane.sort((a, b) => a.priority - b.priority || a.createdAt - b.createdAt || a.seq - b.seq);
|
|
29
|
+
this.queued.set(entry.laneKey, lane);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The next entry to run: the head of the first idle lane at or after the cursor that has
|
|
33
|
+
* work. Marks that lane running until `finish`. Null when every non-empty lane is busy.
|
|
34
|
+
*/
|
|
35
|
+
next() {
|
|
36
|
+
const count = this.order.length;
|
|
37
|
+
for (let step = 0; step < count; step += 1) {
|
|
38
|
+
const index = (this.cursor + step) % count;
|
|
39
|
+
const key = this.order[index];
|
|
40
|
+
const lane = this.queued.get(key);
|
|
41
|
+
if (this.running.has(key) || lane === undefined || lane.length === 0)
|
|
42
|
+
continue;
|
|
43
|
+
const slot = lane.shift();
|
|
44
|
+
this.running.add(key);
|
|
45
|
+
this.cursor = (index + 1) % count;
|
|
46
|
+
return slot;
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
/** The lane's task is over; the lane may serve again. Empty idle lanes are forgotten. */
|
|
51
|
+
finish(laneKey) {
|
|
52
|
+
this.running.delete(laneKey);
|
|
53
|
+
this.forgetIfIdle(laneKey);
|
|
54
|
+
}
|
|
55
|
+
forgetIfIdle(laneKey) {
|
|
56
|
+
if (this.running.has(laneKey) || (this.queued.get(laneKey)?.length ?? 0) > 0)
|
|
57
|
+
return;
|
|
58
|
+
const index = this.order.indexOf(laneKey);
|
|
59
|
+
if (index < 0)
|
|
60
|
+
return;
|
|
61
|
+
this.order.splice(index, 1);
|
|
62
|
+
this.queued.delete(laneKey);
|
|
63
|
+
if (index < this.cursor)
|
|
64
|
+
this.cursor -= 1;
|
|
65
|
+
if (this.cursor >= this.order.length)
|
|
66
|
+
this.cursor = 0;
|
|
67
|
+
}
|
|
68
|
+
/** Entries waiting (not running). */
|
|
69
|
+
size() {
|
|
70
|
+
let total = 0;
|
|
71
|
+
for (const lane of this.queued.values())
|
|
72
|
+
total += lane.length;
|
|
73
|
+
return total;
|
|
74
|
+
}
|
|
75
|
+
runningCount() {
|
|
76
|
+
return this.running.size;
|
|
77
|
+
}
|
|
78
|
+
isRunning(laneKey) {
|
|
79
|
+
return this.running.has(laneKey);
|
|
80
|
+
}
|
|
81
|
+
/** Every waiting entry (the host heartbeats their claims while they wait). */
|
|
82
|
+
queuedEntries() {
|
|
83
|
+
return [...this.queued.values()].flat();
|
|
84
|
+
}
|
|
85
|
+
/** Waiting entries per lane, in rotation order. For status and tests. */
|
|
86
|
+
waiting() {
|
|
87
|
+
return this.order.map((laneKey) => ({ laneKey, count: this.queued.get(laneKey)?.length ?? 0 }));
|
|
88
|
+
}
|
|
89
|
+
/** Remove and return everything that has not started (dispose hands it back to the queue). */
|
|
90
|
+
drain() {
|
|
91
|
+
const all = [];
|
|
92
|
+
for (const key of [...this.order]) {
|
|
93
|
+
all.push(...(this.queued.get(key) ?? []));
|
|
94
|
+
this.queued.set(key, []);
|
|
95
|
+
this.forgetIfIdle(key);
|
|
96
|
+
}
|
|
97
|
+
return all;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { LockInfo } from '../../governance/store/run-lock.js';
|
|
2
|
+
export interface LeaseInfo extends LockInfo {
|
|
3
|
+
workerId?: string;
|
|
4
|
+
kind?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface LeaseDeps {
|
|
7
|
+
/** `lock/curator.lease` (see `curatorLeaseFile`). */
|
|
8
|
+
path: string;
|
|
9
|
+
now(): number;
|
|
10
|
+
pid: number;
|
|
11
|
+
/** Unique per host instance; two hosts in one process must differ. */
|
|
12
|
+
workerId: string;
|
|
13
|
+
kind: 'tui' | 'desktop';
|
|
14
|
+
isPidAlive?(pid: number): boolean;
|
|
15
|
+
staleMs?: number;
|
|
16
|
+
/**
|
|
17
|
+
* How long to wait before the second read-back of a fresh acquisition. Two processes that start
|
|
18
|
+
* together can both pass the pre-check and both write; the last writer wins, and this delay is
|
|
19
|
+
* what lets the earlier writer see that it lost instead of finding out at its first heartbeat.
|
|
20
|
+
*/
|
|
21
|
+
settleMs?: number;
|
|
22
|
+
sleep?(ms: number): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export type AcquireOutcome = {
|
|
25
|
+
ok: true;
|
|
26
|
+
takenOver: boolean;
|
|
27
|
+
} | {
|
|
28
|
+
ok: false;
|
|
29
|
+
reason: 'held' | 'lost_race' | 'io';
|
|
30
|
+
holder: LeaseInfo | null;
|
|
31
|
+
};
|
|
32
|
+
export type RefreshOutcome = 'ok' | 'lost' | 'error';
|
|
33
|
+
export declare class CuratorLease {
|
|
34
|
+
private readonly deps;
|
|
35
|
+
private held;
|
|
36
|
+
private startedAt;
|
|
37
|
+
/** When this host last SAW the file name it (acquire, adopt or a good heartbeat). */
|
|
38
|
+
private confirmedAt;
|
|
39
|
+
constructor(deps: LeaseDeps);
|
|
40
|
+
/**
|
|
41
|
+
* May this host act as the curator right now? A lease nobody has re-confirmed for a whole stale
|
|
42
|
+
* period is treated as lost: a process that slept through its heartbeats must not claim work in
|
|
43
|
+
* the seconds before its next beat discovers that someone else took over.
|
|
44
|
+
*/
|
|
45
|
+
holds(): boolean;
|
|
46
|
+
/** Does this host still believe it owns the file (so a heartbeat is worth trying)? */
|
|
47
|
+
believesHeld(): boolean;
|
|
48
|
+
private staleMs;
|
|
49
|
+
private isAlive;
|
|
50
|
+
private lockDeps;
|
|
51
|
+
private isMine;
|
|
52
|
+
/**
|
|
53
|
+
* Must this lease be honoured? Spec 10.4: it may be taken over only when the heartbeat has
|
|
54
|
+
* expired AND the holder is gone, so a live process that merely stalled (a laptop that slept)
|
|
55
|
+
* keeps it. The governance primitive's own rule is "expired OR gone", which is why the pre-check
|
|
56
|
+
* lives here and the primitive is only called once this says "take over". One safety valve: a
|
|
57
|
+
* heartbeat silent for ten stale periods is abandoned whatever its pid says - pids are recycled,
|
|
58
|
+
* and a lease that can never be released by a wedged or impersonated holder would end learning.
|
|
59
|
+
*/
|
|
60
|
+
private isLiveForeign;
|
|
61
|
+
peek(): Promise<LeaseInfo | null>;
|
|
62
|
+
private textFor;
|
|
63
|
+
/** Try to become the one curator. Never throws. */
|
|
64
|
+
acquire(): Promise<AcquireOutcome>;
|
|
65
|
+
/**
|
|
66
|
+
* Write the enriched file, then read it back twice, the second time after a short settle: the
|
|
67
|
+
* last writer's workerId must be ours both times. Re-adopting a lease the file still names
|
|
68
|
+
* after a pause goes through the same check, because a competitor may be taking it over at that
|
|
69
|
+
* very moment.
|
|
70
|
+
*/
|
|
71
|
+
private confirm;
|
|
72
|
+
/** Heartbeat. `lost` only when the file definitely names someone else (or is gone). */
|
|
73
|
+
refresh(): Promise<RefreshOutcome>;
|
|
74
|
+
/** Give the lease up if it is ours; a foreign lease is left alone. Never throws. */
|
|
75
|
+
release(): Promise<void>;
|
|
76
|
+
}
|