@aragon-agent/cli 0.6.63 → 0.6.66
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 +131 -2
- package/README.md +98 -21
- package/dist/agent/controller.d.ts +119 -14
- package/dist/agent/controller.js +385 -82
- package/dist/agent/headless.d.ts +16 -0
- package/dist/agent/headless.js +68 -6
- package/dist/agent/reconnect-memory.d.ts +68 -0
- package/dist/agent/reconnect-memory.js +82 -0
- package/dist/agent/team-notices.d.ts +57 -7
- package/dist/agent/team-notices.js +93 -10
- package/dist/app/App.js +284 -78
- package/dist/cli.js +4 -1
- package/dist/commands/builtins.js +8 -72
- package/dist/commands/catalog.js +1 -1
- package/dist/commands/retry-command.d.ts +52 -0
- package/dist/commands/retry-command.js +225 -0
- package/dist/config/cli-commands.d.ts +1 -1
- package/dist/config/cli-commands.js +39 -0
- package/dist/exec/events.d.ts +12 -0
- package/dist/exec/index.js +2 -0
- package/dist/exec/runner.js +16 -1
- package/dist/gene/cli-commands.js +30 -1
- package/dist/gene/command-report.js +5 -1
- package/dist/gene/commit-prepare.js +27 -1
- package/dist/gene/curator/budget.d.ts +24 -1
- package/dist/gene/curator/budget.js +67 -14
- package/dist/gene/curator/curator.d.ts +25 -0
- package/dist/gene/curator/curator.js +61 -7
- package/dist/gene/curator/scripted-lane.d.ts +12 -0
- package/dist/gene/curator/scripted-lane.js +101 -0
- package/dist/gene/curator/task-context.d.ts +29 -0
- package/dist/gene/curator/task-context.js +43 -0
- package/dist/gene/curator/task-runner.d.ts +10 -1
- package/dist/gene/curator/task-runner.js +52 -6
- package/dist/gene/curator/tool-support.js +11 -4
- package/dist/gene/curator/toolkit.d.ts +13 -0
- package/dist/gene/curator/toolkit.js +23 -1
- package/dist/gene/curator/tools-verify.js +2 -2
- package/dist/gene/curator/tools-write.js +6 -1
- package/dist/gene/curator/validate/evidence-tokens.js +133 -22
- package/dist/gene/curator/validate/knowledge.d.ts +41 -1
- package/dist/gene/curator/validate/knowledge.js +74 -11
- package/dist/gene/curator/validate/prompts.d.ts +10 -2
- package/dist/gene/curator/validate/prompts.js +2 -2
- package/dist/gene/hub.js +5 -0
- package/dist/gene/import-plan.js +8 -5
- package/dist/gene/learn/draft-guard.d.ts +17 -0
- package/dist/gene/learn/draft-guard.js +113 -0
- package/dist/gene/learn/learn-prompt.d.ts +22 -1
- package/dist/gene/learn/learn-prompt.js +27 -2
- package/dist/gene/learn/learner.js +27 -5
- package/dist/gene/queue.d.ts +24 -0
- package/dist/gene/queue.js +80 -1
- package/dist/governance/store/run-store.d.ts +20 -0
- package/dist/governance/store/run-store.js +54 -8
- package/dist/host/controller-factory.js +6 -0
- package/dist/host/index.d.ts +1 -0
- package/dist/host/index.js +1 -0
- package/dist/host/interaction.d.ts +8 -17
- package/dist/host/interaction.js +50 -72
- package/dist/host/team-wake-driver.d.ts +90 -0
- package/dist/host/team-wake-driver.js +114 -0
- package/dist/logging/install.d.ts +35 -1
- package/dist/logging/install.js +23 -0
- package/dist/session/persist.js +8 -1
- package/dist/session/validate-session.js +15 -5
- package/dist/team/console-tools.d.ts +12 -0
- package/dist/team/console-tools.js +20 -3
- package/dist/team/console-view.js +11 -1
- package/dist/team/notices.d.ts +81 -4
- package/dist/team/notices.js +187 -22
- package/dist/team/prompt.js +1 -1
- package/dist/team/runtime.d.ts +67 -24
- package/dist/team/runtime.js +250 -31
- package/dist/team/stall-watch.d.ts +54 -0
- package/dist/team/stall-watch.js +119 -0
- package/dist/team/task-tool.js +2 -2
- package/dist/team/wake.d.ts +176 -0
- package/dist/team/wake.js +444 -0
- package/package.json +81 -81
|
@@ -1,4 +1,21 @@
|
|
|
1
1
|
import type { GeneConfig } from '@aragon-agent/tui/model/config/schema';
|
|
2
|
+
/** The operator's switch: `1` makes `gene.dailyTokens` and `gene.curatorTaskTokens` ceilings again. */
|
|
3
|
+
export declare const BUDGET_ENFORCED_ENV = "ARAGON_GENE_BUDGET_ENFORCED";
|
|
4
|
+
/**
|
|
5
|
+
* A task's internal token valve while the budget is not enforced. Not a setting. A curator task that
|
|
6
|
+
* is making progress needs 45k-95k tokens (measured), and a loop that is not making progress is
|
|
7
|
+
* ended by the turn and wall-clock limits first, so this only fires when a model has gone wrong in
|
|
8
|
+
* a way those two cannot see (a very large prompt on every turn).
|
|
9
|
+
*/
|
|
10
|
+
export declare const RUNAWAY_TASK_TOKENS = 400000;
|
|
11
|
+
/** Are the token ceilings (`dailyTokens`, `curatorTaskTokens`) enforced? Off unless the env var is `1`. */
|
|
12
|
+
export declare function isBudgetEnforced(env?: Readonly<Record<string, string | undefined>>): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* The one question every daily-budget gate asks (the curator's pause, a task's admission, the
|
|
15
|
+
* scripted lane, the learner's call). `limit <= 0` is "do not spend" in both modes; a positive
|
|
16
|
+
* limit only stops anything while the budget is enforced.
|
|
17
|
+
*/
|
|
18
|
+
export declare function isDailyBudgetExhausted(limit: number, used: number, enforced?: boolean): boolean;
|
|
2
19
|
export interface TaskLimits {
|
|
3
20
|
tokens: number;
|
|
4
21
|
turns: number;
|
|
@@ -22,7 +39,13 @@ export interface BudgetDeps {
|
|
|
22
39
|
*/
|
|
23
40
|
export declare const IMPORT_MIN_REMAINING_SHARE = 0.5;
|
|
24
41
|
/** May a cold-start import proposal be claimed now? Pure over the day's budget state. */
|
|
25
|
-
export declare function importClaimAllowed(daily: Pick<DailyState, 'limit' | 'used'
|
|
42
|
+
export declare function importClaimAllowed(daily: Pick<DailyState, 'limit' | 'used'>, enforced?: boolean): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* How many estimated tokens one cold-start import preview may queue: half of today's remaining
|
|
45
|
+
* allowance while the budget is enforced, otherwise no token cap (the count and queue-room limits
|
|
46
|
+
* still apply). `limit <= 0` stays zero.
|
|
47
|
+
*/
|
|
48
|
+
export declare function importTokenAllowance(limit: number, used: number, enforced?: boolean): number;
|
|
26
49
|
export declare class CuratorBudget {
|
|
27
50
|
private readonly deps;
|
|
28
51
|
constructor(deps: BudgetDeps);
|
|
@@ -3,37 +3,85 @@
|
|
|
3
3
|
*
|
|
4
4
|
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
6
|
+
* TOKENS ARE METERED, NOT RATIONED (the default). Publishing, revising and retiring genes is the
|
|
7
|
+
* point of the library, and the only thing a token ceiling ever did was freeze it: a nine-minute
|
|
8
|
+
* burst of killed attempts once parked the whole pipeline - the reviewer included - for a day, and
|
|
9
|
+
* the experience that happened meanwhile was never reviewed. So `gene.dailyTokens` and
|
|
10
|
+
* `gene.curatorTaskTokens` are no longer enforced; the run log still records every token and
|
|
11
|
+
* `/gene` still shows the 24 h sum. ONE predicate decides, `isBudgetEnforced()`: set
|
|
12
|
+
* `ARAGON_GENE_BUDGET_ENFORCED=1` to bring the old ceilings back (for a comparison or an
|
|
13
|
+
* emergency). Every gate that used to look at the limits asks it, in this file, so there is one
|
|
14
|
+
* place to flip and one place a test has to pin. `dailyTokens <= 0` keeps its older meaning,
|
|
15
|
+
* "do not spend at all", and is honoured in both modes.
|
|
16
|
+
*
|
|
17
|
+
* WHAT STILL STOPS A RUNAWAY, none of it a budget:
|
|
18
|
+
* - per TASK turns (20), wall clock (240 s) and `RUNAWAY_TASK_TOKENS`, an internal valve that is
|
|
19
|
+
* not a setting and sits far above any task that is making progress. A task that
|
|
20
|
+
* crosses one is stopped and retried later (at most twice for an overrun).
|
|
13
21
|
* - per FAILURE STREAK three consecutive infrastructure failures open a breaker for 30 minutes;
|
|
14
22
|
* then ONE trial request is let through (half-open) and its result decides.
|
|
23
|
+
* The deterministic P0 paths (revoke, rollback, recheck) never consult any of it: they spend no tokens.
|
|
15
24
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
25
|
+
* WHEN ENFORCED (`ARAGON_GENE_BUDGET_ENFORCED=1`) the old behaviour applies in full: a rolling 24 h
|
|
26
|
+
* sum of every `tokens` field in the run log (the learner's, the curator's, the validators' and
|
|
27
|
+
* retrieval's share ONE budget) pauses the curator at the limit and the queue keeps everything, and
|
|
28
|
+
* each task is capped at `gene.curatorTaskTokens`. The reset time is then an estimate: a rolling
|
|
29
|
+
* window has no single reset instant, so `daily()` probes it an hour at a time and reports the
|
|
30
|
+
* first instant at which the sum is back under the limit.
|
|
20
31
|
*/
|
|
21
32
|
import { GENE_LIMITS } from '@aragon-agent/tui/model/gene/limits';
|
|
22
33
|
const HOUR_MS = 60 * 60 * 1000;
|
|
23
34
|
const DAY_MS = 24 * HOUR_MS;
|
|
24
35
|
/** When the limit is zero there is no instant at which more is allowed; look again in an hour. */
|
|
25
36
|
const ZERO_LIMIT_RECHECK_MS = HOUR_MS;
|
|
37
|
+
/** The operator's switch: `1` makes `gene.dailyTokens` and `gene.curatorTaskTokens` ceilings again. */
|
|
38
|
+
export const BUDGET_ENFORCED_ENV = 'ARAGON_GENE_BUDGET_ENFORCED';
|
|
39
|
+
/**
|
|
40
|
+
* A task's internal token valve while the budget is not enforced. Not a setting. A curator task that
|
|
41
|
+
* is making progress needs 45k-95k tokens (measured), and a loop that is not making progress is
|
|
42
|
+
* ended by the turn and wall-clock limits first, so this only fires when a model has gone wrong in
|
|
43
|
+
* a way those two cannot see (a very large prompt on every turn).
|
|
44
|
+
*/
|
|
45
|
+
export const RUNAWAY_TASK_TOKENS = 400_000;
|
|
46
|
+
/** Are the token ceilings (`dailyTokens`, `curatorTaskTokens`) enforced? Off unless the env var is `1`. */
|
|
47
|
+
export function isBudgetEnforced(env = process.env) {
|
|
48
|
+
return env[BUDGET_ENFORCED_ENV] === '1';
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The one question every daily-budget gate asks (the curator's pause, a task's admission, the
|
|
52
|
+
* scripted lane, the learner's call). `limit <= 0` is "do not spend" in both modes; a positive
|
|
53
|
+
* limit only stops anything while the budget is enforced.
|
|
54
|
+
*/
|
|
55
|
+
export function isDailyBudgetExhausted(limit, used, enforced = isBudgetEnforced()) {
|
|
56
|
+
if (limit <= 0)
|
|
57
|
+
return true;
|
|
58
|
+
return enforced && used >= limit;
|
|
59
|
+
}
|
|
26
60
|
/**
|
|
27
61
|
* Cold-start imports (`origin.source === 'import'`) are claimed only while at least this share of
|
|
28
62
|
* the day's token allowance is still unspent: they must never starve live learning.
|
|
29
63
|
*/
|
|
30
64
|
export const IMPORT_MIN_REMAINING_SHARE = 0.5;
|
|
31
65
|
/** May a cold-start import proposal be claimed now? Pure over the day's budget state. */
|
|
32
|
-
export function importClaimAllowed(daily) {
|
|
66
|
+
export function importClaimAllowed(daily, enforced = isBudgetEnforced()) {
|
|
33
67
|
if (daily.limit <= 0)
|
|
34
68
|
return false;
|
|
69
|
+
if (!enforced)
|
|
70
|
+
return true;
|
|
35
71
|
return daily.limit - daily.used >= daily.limit * IMPORT_MIN_REMAINING_SHARE;
|
|
36
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* How many estimated tokens one cold-start import preview may queue: half of today's remaining
|
|
75
|
+
* allowance while the budget is enforced, otherwise no token cap (the count and queue-room limits
|
|
76
|
+
* still apply). `limit <= 0` stays zero.
|
|
77
|
+
*/
|
|
78
|
+
export function importTokenAllowance(limit, used, enforced = isBudgetEnforced()) {
|
|
79
|
+
if (limit <= 0)
|
|
80
|
+
return 0;
|
|
81
|
+
if (!enforced)
|
|
82
|
+
return Number.MAX_SAFE_INTEGER;
|
|
83
|
+
return Math.floor(Math.max(0, limit - used) * IMPORT_MIN_REMAINING_SHARE);
|
|
84
|
+
}
|
|
37
85
|
export class CuratorBudget {
|
|
38
86
|
deps;
|
|
39
87
|
constructor(deps) {
|
|
@@ -41,7 +89,7 @@ export class CuratorBudget {
|
|
|
41
89
|
}
|
|
42
90
|
taskLimits(config) {
|
|
43
91
|
return {
|
|
44
|
-
tokens: config.curatorTaskTokens,
|
|
92
|
+
tokens: isBudgetEnforced() ? config.curatorTaskTokens : RUNAWAY_TASK_TOKENS,
|
|
45
93
|
turns: GENE_LIMITS.curatorMaxTurns,
|
|
46
94
|
wallMs: GENE_LIMITS.curatorWallClockMs,
|
|
47
95
|
};
|
|
@@ -49,8 +97,13 @@ export class CuratorBudget {
|
|
|
49
97
|
async daily(config) {
|
|
50
98
|
const now = this.deps.now();
|
|
51
99
|
const limit = config.dailyTokens;
|
|
100
|
+
if (limit > 0 && !isBudgetEnforced()) {
|
|
101
|
+
// Metering only: an unreadable log must not decide anything, so it reads as "nothing spent".
|
|
102
|
+
const used = await this.deps.tokensSince(now - DAY_MS).catch(() => 0);
|
|
103
|
+
return { limit, used, exhausted: false, resetAt: null };
|
|
104
|
+
}
|
|
52
105
|
const used = await this.deps.tokensSince(now - DAY_MS);
|
|
53
|
-
if (limit
|
|
106
|
+
if (!isDailyBudgetExhausted(limit, used, true))
|
|
54
107
|
return { limit, used, exhausted: false, resetAt: null };
|
|
55
108
|
return { limit, used, exhausted: true, resetAt: await this.estimateReset(now, limit) };
|
|
56
109
|
}
|
|
@@ -70,6 +70,13 @@ export interface CuratorDeps {
|
|
|
70
70
|
housekeeping?(): Promise<void>;
|
|
71
71
|
probeDeps?: ProbeDeps;
|
|
72
72
|
timing?: Partial<CuratorTiming>;
|
|
73
|
+
/**
|
|
74
|
+
* The scripted create lane (`scripted-lane.ts`). Absent = on, which is what production wants; a
|
|
75
|
+
* test that scripts the agent loop turns it off so its proposals reach the model, and a test of
|
|
76
|
+
* the lane turns it on. `ARAGON_GENE_SCRIPTED_LANE=0` is the operator's kill switch and applies
|
|
77
|
+
* either way.
|
|
78
|
+
*/
|
|
79
|
+
scriptedLane?(): boolean;
|
|
73
80
|
}
|
|
74
81
|
export interface CuratorStatus {
|
|
75
82
|
started: boolean;
|
|
@@ -143,9 +150,27 @@ export declare class GeneCurator {
|
|
|
143
150
|
private notify;
|
|
144
151
|
private tickOnce;
|
|
145
152
|
private ensureLease;
|
|
153
|
+
/**
|
|
154
|
+
* Wake the proposals an earlier budget pause parked. `release` / a `budget` failure wrote their
|
|
155
|
+
* `notBefore` (up to a day out) into the queue file, and `claimNext` only reads the file, so lifting
|
|
156
|
+
* the budget gate does not wake them: they would sleep until the old window's reset. Done once per
|
|
157
|
+
* lease, never while the budget is enforced (the parking is then still true), and only for the items
|
|
158
|
+
* a budget pause parks: a `lastError` of `released` / `budget_exhausted` and a wait longer than the
|
|
159
|
+
* breaker's 30 minutes, which keeps the short, legitimate waits (no fast model, an open breaker, a
|
|
160
|
+
* network backoff) exactly as they were. Best effort: a failed pass is retried by the next lease.
|
|
161
|
+
*/
|
|
162
|
+
private thawParkedItems;
|
|
146
163
|
private every;
|
|
147
164
|
private housekeeping;
|
|
148
165
|
private recoverStale;
|
|
166
|
+
/**
|
|
167
|
+
* Move the queued reviewer proposals whose draft the curator can never turn into a candidate to
|
|
168
|
+
* `dead/`, once an hour and logged (R-3'). They were queued before the enqueue guard existed (a
|
|
169
|
+
* skill draft alone cost 145k-205k tokens and ended in nothing), and nothing else would ever
|
|
170
|
+
* stop them: this is a sweep with an event per proposal, not a hand edit of `queue/**`, and
|
|
171
|
+
* `retryDead` puts any of them back.
|
|
172
|
+
*/
|
|
173
|
+
private sweepUncreatableDrafts;
|
|
149
174
|
private enqueueMaintenance;
|
|
150
175
|
/** Returns whether to claim at all; sets the model block that only blocks model work. */
|
|
151
176
|
private evaluatePause;
|
|
@@ -26,10 +26,11 @@
|
|
|
26
26
|
* curator at its next checkpoint.
|
|
27
27
|
*
|
|
28
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
|
|
30
|
-
* the deterministic paths run - a revoke or a
|
|
31
|
-
* budget - and put model items back with a
|
|
32
|
-
* is a run record, with the reason as the
|
|
29
|
+
* nothing. Model-related pauses (no fast model, an open breaker, and the daily budget when it is
|
|
30
|
+
* enforced - `budget.ts`, off by default) still let the deterministic paths run - a revoke or a
|
|
31
|
+
* rollback spends no tokens and must not wait for a budget - and put model items back with a
|
|
32
|
+
* `notBefore`, no attempt counted. Every pause and resume is a run record, with the reason as the
|
|
33
|
+
* first word of its detail (the activity hub reads that).
|
|
33
34
|
*
|
|
34
35
|
* LOGGING NEVER CARRIES GENE TEXT (I-19): ids, scopes, enums, counts and stable tokens only.
|
|
35
36
|
*/
|
|
@@ -37,7 +38,7 @@ import { agingPriority } from '@aragon-agent/tui/model/gene/proposal';
|
|
|
37
38
|
import { scopeId } from '@aragon-agent/tui/model/gene/ids';
|
|
38
39
|
import { GENE_LIMITS } from '@aragon-agent/tui/model/gene/limits';
|
|
39
40
|
import { curatorLeaseFile } from '../paths.js';
|
|
40
|
-
import { BREAKER_OPEN_MS, CircuitBreaker, CuratorBudget, importClaimAllowed, } from './budget.js';
|
|
41
|
+
import { BREAKER_OPEN_MS, CircuitBreaker, CuratorBudget, importClaimAllowed, isBudgetEnforced, } from './budget.js';
|
|
41
42
|
import { makeSystemProposal } from './executors.js';
|
|
42
43
|
import { LaneScheduler } from './lanes.js';
|
|
43
44
|
import { CuratorLease } from './lease.js';
|
|
@@ -47,6 +48,7 @@ import { SwitchGuard } from './switches.js';
|
|
|
47
48
|
import { runClaimedTask } from './task-runner.js';
|
|
48
49
|
import { createValidatorPortsFactory } from './validator-ports.js';
|
|
49
50
|
import { realScheduler } from '../hub.js';
|
|
51
|
+
import { checkDraftCreatable, draftRefusalDetail, isDraftGuardEnabled } from '../learn/draft-guard.js';
|
|
50
52
|
const MINUTE_MS = 60_000;
|
|
51
53
|
export const DEFAULT_TIMING = {
|
|
52
54
|
pollMs: 5000,
|
|
@@ -162,6 +164,8 @@ export class GeneCurator {
|
|
|
162
164
|
random: this.random,
|
|
163
165
|
scheduler: this.scheduler,
|
|
164
166
|
modelBlocked: () => this.modelBlockedNow(),
|
|
167
|
+
// Peeks: unlike `modelBlockedNow`, it never takes the half-open trial.
|
|
168
|
+
scriptedLaneAvailable: () => (this.deps.scriptedLane?.() ?? true) && this.modelBlock === null && this.breaker.view().state === 'closed',
|
|
165
169
|
};
|
|
166
170
|
}
|
|
167
171
|
// ---- public surface ---------------------------------------------------------
|
|
@@ -323,6 +327,10 @@ export class GeneCurator {
|
|
|
323
327
|
if (!got.ok)
|
|
324
328
|
return false;
|
|
325
329
|
this.leaseErrors = 0;
|
|
330
|
+
// The pause the log shows belongs to whoever held the lease: the activity hub drops it at
|
|
331
|
+
// `lease_acquired`. A process that lost the lease and took it back would otherwise find its own
|
|
332
|
+
// pause "unchanged" and never say it again, leaving the chip idle while nothing is claimed.
|
|
333
|
+
this.paused = undefined;
|
|
326
334
|
this.lastRun.clear();
|
|
327
335
|
this.lastRun.set('maintenance', now - this.timing.maintenanceMs + this.timing.maintenanceFirstMs);
|
|
328
336
|
if (this.timing.choresFirstRun === 'after_interval') {
|
|
@@ -330,8 +338,32 @@ export class GeneCurator {
|
|
|
330
338
|
this.lastRun.set(chore, now);
|
|
331
339
|
}
|
|
332
340
|
await this.events.emit('lease_acquired', { detail: got.takenOver ? 'takeover' : 'fresh' });
|
|
341
|
+
await this.thawParkedItems();
|
|
333
342
|
return true;
|
|
334
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* Wake the proposals an earlier budget pause parked. `release` / a `budget` failure wrote their
|
|
346
|
+
* `notBefore` (up to a day out) into the queue file, and `claimNext` only reads the file, so lifting
|
|
347
|
+
* the budget gate does not wake them: they would sleep until the old window's reset. Done once per
|
|
348
|
+
* lease, never while the budget is enforced (the parking is then still true), and only for the items
|
|
349
|
+
* a budget pause parks: a `lastError` of `released` / `budget_exhausted` and a wait longer than the
|
|
350
|
+
* breaker's 30 minutes, which keeps the short, legitimate waits (no fast model, an open breaker, a
|
|
351
|
+
* network backoff) exactly as they were. Best effort: a failed pass is retried by the next lease.
|
|
352
|
+
*/
|
|
353
|
+
async thawParkedItems() {
|
|
354
|
+
if (isBudgetEnforced())
|
|
355
|
+
return;
|
|
356
|
+
try {
|
|
357
|
+
const now = this.now();
|
|
358
|
+
const thawed = await this.deps.queue.thawPendingWhere((record) => (record.lastError === 'released' || record.lastError === 'budget_exhausted') &&
|
|
359
|
+
record.notBefore - now > BREAKER_OPEN_MS);
|
|
360
|
+
if (thawed.length > 0)
|
|
361
|
+
await this.events.emit('skipped', { detail: `thawed n=${thawed.length}` });
|
|
362
|
+
}
|
|
363
|
+
catch {
|
|
364
|
+
// The items keep their old wait; nothing is lost.
|
|
365
|
+
}
|
|
366
|
+
}
|
|
335
367
|
async every(name, ms, job) {
|
|
336
368
|
const last = this.lastRun.get(name);
|
|
337
369
|
const now = this.now();
|
|
@@ -350,6 +382,7 @@ export class GeneCurator {
|
|
|
350
382
|
await this.every('recover', t.recoverMs, () => this.recoverStale());
|
|
351
383
|
await this.every('probation', t.probationMs, () => sweepProbation({ service: this.deps.service, library: this.deps.library, now: this.now }));
|
|
352
384
|
await this.every('maintenance', t.maintenanceMs, () => this.enqueueMaintenance());
|
|
385
|
+
await this.every('draft_sweep', t.pruneMs, () => this.sweepUncreatableDrafts());
|
|
353
386
|
await this.every('prune', t.pruneMs, async () => {
|
|
354
387
|
await this.deps.queue.prune();
|
|
355
388
|
await this.deps.runLog.prune();
|
|
@@ -365,6 +398,25 @@ export class GeneCurator {
|
|
|
365
398
|
await this.events.emit(ev, { proposalId: item.proposalId, detail: `claim_expired attempt=${item.attempt}` });
|
|
366
399
|
}
|
|
367
400
|
}
|
|
401
|
+
/**
|
|
402
|
+
* Move the queued reviewer proposals whose draft the curator can never turn into a candidate to
|
|
403
|
+
* `dead/`, once an hour and logged (R-3'). They were queued before the enqueue guard existed (a
|
|
404
|
+
* skill draft alone cost 145k-205k tokens and ended in nothing), and nothing else would ever
|
|
405
|
+
* stop them: this is a sweep with an event per proposal, not a hand edit of `queue/**`, and
|
|
406
|
+
* `retryDead` puts any of them back.
|
|
407
|
+
*/
|
|
408
|
+
async sweepUncreatableDrafts() {
|
|
409
|
+
if (!isDraftGuardEnabled())
|
|
410
|
+
return;
|
|
411
|
+
const secrets = this.deps.knownSecrets?.() ?? [];
|
|
412
|
+
const now = this.now();
|
|
413
|
+
const moved = await this.deps.queue.quarantinePendingWhere((proposal) => {
|
|
414
|
+
const verdict = checkDraftCreatable(proposal, secrets, now);
|
|
415
|
+
return verdict.creatable ? null : draftRefusalDetail(verdict);
|
|
416
|
+
});
|
|
417
|
+
for (const proposalId of moved)
|
|
418
|
+
await this.events.emit('dead', { proposalId, detail: 'draft_not_creatable' });
|
|
419
|
+
}
|
|
368
420
|
async enqueueMaintenance() {
|
|
369
421
|
const proposal = makeSystemProposal({ now: this.now, random: this.random }, {
|
|
370
422
|
kind: 'maintenance',
|
|
@@ -408,6 +460,7 @@ export class GeneCurator {
|
|
|
408
460
|
this.modelBlock = { reason: 'curator_failing', until: view.until ?? this.now() + BREAKER_OPEN_MS };
|
|
409
461
|
return 'curator_failing';
|
|
410
462
|
}
|
|
463
|
+
// Only ever exhausted while the budget is enforced, or when `dailyTokens` is 0 ("do not spend").
|
|
411
464
|
const daily = await this.budget.daily(config);
|
|
412
465
|
if (daily.exhausted) {
|
|
413
466
|
const resetAt = daily.resetAt ?? until;
|
|
@@ -555,10 +608,11 @@ export class GeneCurator {
|
|
|
555
608
|
return;
|
|
556
609
|
}
|
|
557
610
|
const error = outcome.failure.kind === 'transient' ? outcome.failure.error : 'validation';
|
|
611
|
+
const stats = outcome.stats ?? '';
|
|
558
612
|
if (result.state === 'dead')
|
|
559
|
-
await this.events.emit('dead', { proposalId, detail: `${error} attempts=${result.attempt}` });
|
|
613
|
+
await this.events.emit('dead', { proposalId, detail: `${error} attempts=${result.attempt}${stats}` });
|
|
560
614
|
else
|
|
561
|
-
await this.events.emit('retry_scheduled', { proposalId, detail: `${error} attempt=${result.attempt}` });
|
|
615
|
+
await this.events.emit('retry_scheduled', { proposalId, detail: `${error} attempt=${result.attempt}${stats}` });
|
|
562
616
|
}
|
|
563
617
|
async settleRelease(claimed, outcome) {
|
|
564
618
|
const proposalId = claimed.item.proposal.id;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GeneProposal } from '@aragon-agent/tui/model/gene/types';
|
|
2
|
+
import type { CandidateRef } from './task-context.js';
|
|
3
|
+
import { type ToolEnv } from './tool-support.js';
|
|
4
|
+
export declare function isScriptedLaneEnabled(env?: NodeJS.ProcessEnv): boolean;
|
|
5
|
+
/** The only shape the lane takes: a reviewer's knowledge create that names no existing gene. */
|
|
6
|
+
export declare function isScriptedShape(proposal: GeneProposal): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Write the candidate when every deterministic check would pass, and return its reference for
|
|
9
|
+
* `resume_validation`. `null` = decline (nothing was written); `'abandoned'` = a switch or a
|
|
10
|
+
* cancellation stopped the task and `ctx.abandoned` says why.
|
|
11
|
+
*/
|
|
12
|
+
export declare function writeScriptedCandidate(env: ToolEnv): Promise<CandidateRef | 'abandoned' | null>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The scripted create lane (gene-bank-never-publishes-3, F2 / beta').
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* WHAT IT IS. For a reviewer's `create` proposal whose knowledge draft is already COMPLETE and
|
|
7
|
+
* already passes the two checks that need no model (K1 static, K2 source, K3 scope), the CODE writes
|
|
8
|
+
* the candidate - through the same `buildContent` and the same `service.commit` that
|
|
9
|
+
* `gene_create_candidate` uses - and hands it to `resume_validation`, the executor that already
|
|
10
|
+
* validates and publishes a candidate a killed task left behind. No agent is created.
|
|
11
|
+
*
|
|
12
|
+
* WHY. A curator task is a 9-call loop whose cost is the model's behaviour: 26k-70k tokens to reach
|
|
13
|
+
* the validator, against a validator that costs about 1-2k. 12 of the 15 validations ever produced,
|
|
14
|
+
* and the only published gene, came out of `resume_validation` after a cap kill. This lane makes
|
|
15
|
+
* that path the first resort for the proposals that can take it, so the cost no longer depends on
|
|
16
|
+
* whether the model happens to be tidy.
|
|
17
|
+
*
|
|
18
|
+
* WHAT IT DOES NOT TOUCH. Every gate stays where it was: the switches are re-read right before the
|
|
19
|
+
* write (`switchGate`), the write is `service.commit` as the curator actor, validation is
|
|
20
|
+
* `runCandidateValidation` (K4 and K5 still call a model; a conflict still converts or waits for a
|
|
21
|
+
* person; a K6 untrusted-only candidate still waits), and publishing is `publishCandidate`, so
|
|
22
|
+
* probation, the publish gate and the leftover cleanup are the ones the agent path uses.
|
|
23
|
+
*
|
|
24
|
+
* WHEN IT DECLINES. Any doubt returns `null` and the proposal takes the agent loop exactly as it
|
|
25
|
+
* did before: not a knowledge create, not from the reviewer, not complete, K1/K2/K3 would fail, the
|
|
26
|
+
* switch is off, the model is blocked, or the write itself was refused. The decision is made BEFORE
|
|
27
|
+
* the candidate exists, because once it does a failed validation is final.
|
|
28
|
+
*
|
|
29
|
+
* KILL SWITCH. `ARAGON_GENE_SCRIPTED_LANE=0` turns it off and nothing turns it on.
|
|
30
|
+
*/
|
|
31
|
+
import process from 'node:process';
|
|
32
|
+
import { validateGeneContent } from '@aragon-agent/tui/model/gene/validate';
|
|
33
|
+
import { geneLooksLikeSecret } from '../secrets.js';
|
|
34
|
+
import { CURATOR_ACTOR, buildContent, evidenceFor, switchGate, } from './tool-support.js';
|
|
35
|
+
import { passesDeterministicChecks } from './validate/knowledge.js';
|
|
36
|
+
const KILL_SWITCH = 'ARAGON_GENE_SCRIPTED_LANE';
|
|
37
|
+
const REASON = 'Written by the curator from a reviewer proposal after the deterministic checks passed.';
|
|
38
|
+
export function isScriptedLaneEnabled(env = process.env) {
|
|
39
|
+
return env[KILL_SWITCH]?.trim() !== '0';
|
|
40
|
+
}
|
|
41
|
+
/** The only shape the lane takes: a reviewer's knowledge create that names no existing gene. */
|
|
42
|
+
export function isScriptedShape(proposal) {
|
|
43
|
+
return (proposal.kind === 'create' &&
|
|
44
|
+
(proposal.type ?? 'knowledge') === 'knowledge' &&
|
|
45
|
+
proposal.target === undefined &&
|
|
46
|
+
proposal.origin.source === 'reviewer' &&
|
|
47
|
+
proposal.draft !== undefined);
|
|
48
|
+
}
|
|
49
|
+
/** The draft as the fields `gene_create_candidate` would receive from a model. */
|
|
50
|
+
function rawFromDraft(draft) {
|
|
51
|
+
return { ...draft };
|
|
52
|
+
}
|
|
53
|
+
/** The content the lane would write, or null when the draft is not complete and valid as it stands. */
|
|
54
|
+
async function completeContent(env) {
|
|
55
|
+
const { ctx } = env;
|
|
56
|
+
const draft = ctx.proposal.draft;
|
|
57
|
+
if (draft === undefined)
|
|
58
|
+
return null;
|
|
59
|
+
const built = buildContent(env, 'knowledge', rawFromDraft(draft));
|
|
60
|
+
const verdict = validateGeneContent('knowledge', built.content, {
|
|
61
|
+
scope: ctx.scope,
|
|
62
|
+
actor: 'agent',
|
|
63
|
+
secretCheck: (text) => geneLooksLikeSecret(text, env.deps.knownSecrets()),
|
|
64
|
+
});
|
|
65
|
+
return verdict.ok && built.requestedClass === undefined ? built.content : null;
|
|
66
|
+
}
|
|
67
|
+
/** Would K2 and K3 pass on the content as drafted? Uses the validator's own functions. */
|
|
68
|
+
async function passesPrecheck(env, content) {
|
|
69
|
+
const { ctx } = env;
|
|
70
|
+
const ports = await env.deps.validatorPorts(ctx);
|
|
71
|
+
return passesDeterministicChecks({ ports, type: 'knowledge', scope: ctx.scope, content, evidence: ctx.evidence });
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Write the candidate when every deterministic check would pass, and return its reference for
|
|
75
|
+
* `resume_validation`. `null` = decline (nothing was written); `'abandoned'` = a switch or a
|
|
76
|
+
* cancellation stopped the task and `ctx.abandoned` says why.
|
|
77
|
+
*/
|
|
78
|
+
export async function writeScriptedCandidate(env) {
|
|
79
|
+
const { ctx } = env;
|
|
80
|
+
const content = await completeContent(env);
|
|
81
|
+
if (content === null || !(await passesPrecheck(env, content)))
|
|
82
|
+
return null;
|
|
83
|
+
// The same checkpoint every write tool passes: a person may have switched learning off meanwhile.
|
|
84
|
+
if ((await switchGate(env, 'scripted_create')) !== null)
|
|
85
|
+
return 'abandoned';
|
|
86
|
+
const done = await env.deps.service.commit({
|
|
87
|
+
op: 'create',
|
|
88
|
+
type: 'knowledge',
|
|
89
|
+
scope: ctx.scope,
|
|
90
|
+
content,
|
|
91
|
+
reason: REASON,
|
|
92
|
+
class: 'standard',
|
|
93
|
+
proposalId: ctx.proposal.id,
|
|
94
|
+
evidence: evidenceFor(ctx, null),
|
|
95
|
+
}, CURATOR_ACTOR);
|
|
96
|
+
if (!done.ok)
|
|
97
|
+
return null;
|
|
98
|
+
ctx.wrote = true;
|
|
99
|
+
ctx.evidenceAttached.add(done.value.id);
|
|
100
|
+
return { id: done.value.id, scope: ctx.scope, type: 'knowledge', version: done.value.version ?? 1 };
|
|
101
|
+
}
|
|
@@ -49,6 +49,19 @@ export interface TaskUsage {
|
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
51
|
export declare const CURATOR_CONTEXT_BRAND: unique symbol;
|
|
52
|
+
/**
|
|
53
|
+
* What one task did, as numbers and our own names (gene-bank-never-publishes-3, R-6'). The run log
|
|
54
|
+
* had a token total per attempt and nothing else, so "80k went where?" was unanswerable: a skill
|
|
55
|
+
* that was refused five times and a knowledge task that spent its turns searching looked the same.
|
|
56
|
+
* Only tool NAMES (a source constant) and refusal CODES (`GENE_*`) are counted - never an argument,
|
|
57
|
+
* a draft or a receipt (I-19).
|
|
58
|
+
*/
|
|
59
|
+
export interface TaskStats {
|
|
60
|
+
/** Model turns, written by the runner when the agent loop ends. */
|
|
61
|
+
turns: number;
|
|
62
|
+
tools: Map<string, number>;
|
|
63
|
+
refusals: Map<string, number>;
|
|
64
|
+
}
|
|
52
65
|
export interface CuratorTaskContext {
|
|
53
66
|
readonly brand: typeof CURATOR_CONTEXT_BRAND;
|
|
54
67
|
/** False once the task has ended: every tool then refuses. */
|
|
@@ -78,6 +91,7 @@ export interface CuratorTaskContext {
|
|
|
78
91
|
needsHuman: GeneNeedsHuman['reason'] | null;
|
|
79
92
|
/** Whether a gene-writing call already took place (a retry needs to know). */
|
|
80
93
|
wrote: boolean;
|
|
94
|
+
stats: TaskStats;
|
|
81
95
|
}
|
|
82
96
|
export interface ContextSeed {
|
|
83
97
|
proposal: GeneProposal;
|
|
@@ -88,6 +102,21 @@ export interface ContextSeed {
|
|
|
88
102
|
rebased: boolean;
|
|
89
103
|
}
|
|
90
104
|
export declare function createTaskContext(seed: ContextSeed): CuratorTaskContext;
|
|
105
|
+
/**
|
|
106
|
+
* ` turns=9 tools=gene_create_candidate:5,gene_toolkit:3 refusals=GENE_INVALID_CONTENT:4` for a run-log
|
|
107
|
+
* `detail`; empty when the task took no turn. Bounded: the log clips a detail at 240 characters.
|
|
108
|
+
*/
|
|
109
|
+
export declare function describeTaskStats(stats: TaskStats): string;
|
|
110
|
+
/** Turns after which a task that has called no tool at all still counts as having worked. */
|
|
111
|
+
export declare const PROGRESS_MIN_TURNS = 3;
|
|
112
|
+
/**
|
|
113
|
+
* Did the task get anywhere before something ended it: one tool call, or `PROGRESS_MIN_TURNS`
|
|
114
|
+
* model turns? A deadline that finds a task with neither is a stalled provider; one that finds
|
|
115
|
+
* either is a task too big for the clock (`task-runner.ts::modelOutcome`).
|
|
116
|
+
*/
|
|
117
|
+
export declare function madeProgress(stats: TaskStats): boolean;
|
|
118
|
+
/** Count one finished tool call, and its refusal code when it was refused. Never throws. */
|
|
119
|
+
export declare function noteToolCall(ctx: CuratorTaskContext, tool: string, result: ToolResult): void;
|
|
91
120
|
/** A tool refusal, or null when the context is a live curator task. Never throws. */
|
|
92
121
|
export declare function assertCuratorContext(ctx: CuratorTaskContext | undefined): ToolResult | null;
|
|
93
122
|
export declare function validationKey(id: string, version: number): string;
|
|
@@ -46,8 +46,51 @@ export function createTaskContext(seed) {
|
|
|
46
46
|
merged: [],
|
|
47
47
|
needsHuman: null,
|
|
48
48
|
wrote: false,
|
|
49
|
+
stats: { turns: 0, tools: new Map(), refusals: new Map() },
|
|
49
50
|
};
|
|
50
51
|
}
|
|
52
|
+
const STATS_MAX_TOOLS = 6;
|
|
53
|
+
const STATS_MAX_REFUSALS = 4;
|
|
54
|
+
function listed(counts, max) {
|
|
55
|
+
return [...counts.entries()]
|
|
56
|
+
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
|
|
57
|
+
.slice(0, max)
|
|
58
|
+
.map(([name, n]) => `${name}:${n}`)
|
|
59
|
+
.join(',');
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* ` turns=9 tools=gene_create_candidate:5,gene_toolkit:3 refusals=GENE_INVALID_CONTENT:4` for a run-log
|
|
63
|
+
* `detail`; empty when the task took no turn. Bounded: the log clips a detail at 240 characters.
|
|
64
|
+
*/
|
|
65
|
+
export function describeTaskStats(stats) {
|
|
66
|
+
if (stats.turns === 0 && stats.tools.size === 0)
|
|
67
|
+
return '';
|
|
68
|
+
const tools = listed(stats.tools, STATS_MAX_TOOLS);
|
|
69
|
+
const refusals = listed(stats.refusals, STATS_MAX_REFUSALS);
|
|
70
|
+
return ` turns=${stats.turns}${tools === '' ? '' : ` tools=${tools}`}${refusals === '' ? '' : ` refusals=${refusals}`}`;
|
|
71
|
+
}
|
|
72
|
+
/** Turns after which a task that has called no tool at all still counts as having worked. */
|
|
73
|
+
export const PROGRESS_MIN_TURNS = 3;
|
|
74
|
+
/**
|
|
75
|
+
* Did the task get anywhere before something ended it: one tool call, or `PROGRESS_MIN_TURNS`
|
|
76
|
+
* model turns? A deadline that finds a task with neither is a stalled provider; one that finds
|
|
77
|
+
* either is a task too big for the clock (`task-runner.ts::modelOutcome`).
|
|
78
|
+
*/
|
|
79
|
+
export function madeProgress(stats) {
|
|
80
|
+
return stats.tools.size > 0 || stats.turns >= PROGRESS_MIN_TURNS;
|
|
81
|
+
}
|
|
82
|
+
/** Count one finished tool call, and its refusal code when it was refused. Never throws. */
|
|
83
|
+
export function noteToolCall(ctx, tool, result) {
|
|
84
|
+
const { stats } = ctx;
|
|
85
|
+
stats.tools.set(tool, (stats.tools.get(tool) ?? 0) + 1);
|
|
86
|
+
if (result.isError !== true)
|
|
87
|
+
return;
|
|
88
|
+
const first = result.content.find((block) => block.type === 'text');
|
|
89
|
+
// `errorResult` prefixes `Error: `; the refusal itself is `<tool>: <CODE>[:<detail>] - <message>`.
|
|
90
|
+
const code = /^(?:Error: )?[a-z_]+: ([A-Z][A-Z_]*)/.exec(first?.text ?? '')?.[1];
|
|
91
|
+
if (code !== undefined)
|
|
92
|
+
stats.refusals.set(code, (stats.refusals.get(code) ?? 0) + 1);
|
|
93
|
+
}
|
|
51
94
|
/** A tool refusal, or null when the context is a live curator task. Never throws. */
|
|
52
95
|
export function assertCuratorContext(ctx) {
|
|
53
96
|
if (ctx === undefined || ctx.brand !== CURATOR_CONTEXT_BRAND) {
|
|
@@ -10,10 +10,13 @@ export type TaskOutcome = {
|
|
|
10
10
|
kind: 'complete';
|
|
11
11
|
result: QueueResult;
|
|
12
12
|
detail: string;
|
|
13
|
-
}
|
|
13
|
+
}
|
|
14
|
+
/** `stats`: ` turns=9 tools=... refusals=...` for the run-log record of the failed attempt (R-6'). */
|
|
15
|
+
| {
|
|
14
16
|
kind: 'fail';
|
|
15
17
|
failure: QueueFailure;
|
|
16
18
|
infra: boolean;
|
|
19
|
+
stats?: string;
|
|
17
20
|
} | {
|
|
18
21
|
kind: 'release';
|
|
19
22
|
reason: string;
|
|
@@ -38,6 +41,12 @@ export interface TaskRunnerDeps {
|
|
|
38
41
|
scheduler?: Pick<HubScheduler, 'after'>;
|
|
39
42
|
/** Why model work cannot start right now, or null. Deterministic work is never blocked. */
|
|
40
43
|
modelBlocked(): ModelBlock | null;
|
|
44
|
+
/**
|
|
45
|
+
* May the scripted create lane run? It needs a model for K4/K5 and must NOT consume the breaker's
|
|
46
|
+
* single half-open trial, which `modelBlocked()` does: ask that question twice and the agent task
|
|
47
|
+
* the trial was meant for is released as `curator_failing`. Absent = yes.
|
|
48
|
+
*/
|
|
49
|
+
scriptedLaneAvailable?(): boolean;
|
|
41
50
|
}
|
|
42
51
|
/**
|
|
43
52
|
* The proposal's evidence with refs minted ONCE: the validators' corpus (redacted exactly as the
|