@codedology/code-forge 0.3.2 → 0.5.0
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 +85 -0
- package/README.md +88 -21
- package/bin/code-forge.mjs +6 -1
- package/docs/README.md +5 -4
- package/docs/reference/blocks.json +236 -1
- package/docs/reference/config.md +13 -1
- package/package.json +6 -2
- package/schema/code-forge.schema.json +26 -4
- package/skill/SKILL.md +8 -7
- package/skill/references/adapters/claude-code.md +5 -1
- package/skill/references/adapters/solo.md +1 -1
- package/skill/references/adapters/subprocess.md +1 -1
- package/skill/references/code.md +4 -3
- package/skill/references/continuity.md +4 -0
- package/skill/references/decisions.md +5 -2
- package/skill/references/ledger.md +10 -2
- package/skill/references/plan.md +12 -3
- package/skill/references/review.md +13 -6
- package/skill/references/security.md +3 -1
- package/skill/scripts/forge +1 -1
- package/skill/templates/coder-brief.md +36 -0
- package/skill/templates/plan.md +2 -0
- package/src/cli/block.mjs +51 -8
- package/src/cli/facts.mjs +16 -1
- package/src/cli/jev.mjs +114 -17
- package/src/cli/ledger.mjs +99 -8
- package/src/cli/logs.mjs +193 -58
- package/src/cli/plan.mjs +27 -11
- package/src/cli/report.mjs +15 -1
- package/src/cli/resolve.mjs +16 -1
- package/src/cli/review-file.mjs +10 -5
- package/src/cli/review.mjs +40 -9
- package/src/cli/run.mjs +109 -8
- package/src/config/closed-book.mjs +75 -0
- package/src/config/validate.mjs +128 -7
- package/src/decide/escalation.mjs +112 -11
- package/src/decide/questions.mjs +27 -4
- package/src/doctor/index.mjs +2 -1
- package/src/doctor/local.mjs +15 -0
- package/src/doctor/probes.mjs +48 -6
- package/src/engines/builders/claude.mjs +4 -1
- package/src/engines/builders/codex.mjs +26 -11
- package/src/engines/builders/grok.mjs +3 -3
- package/src/engines/efforts.mjs +109 -0
- package/src/engines/sentinel.mjs +34 -0
- package/src/keys/onepassword.mjs +3 -0
- package/src/ledger/report.mjs +66 -29
- package/src/ledger/spend.mjs +288 -0
- package/src/review/engine.mjs +57 -16
- package/src/review/fixloop.mjs +29 -6
- package/src/review/session-retry.mjs +69 -0
- package/src/review/triage.mjs +26 -1
- package/src/review/validate-review.mjs +6 -3
- package/src/session/author.mjs +6 -2
- package/src/session/facts.mjs +218 -53
- package/src/session/plan-check.mjs +71 -6
- package/src/session/plan-sections.mjs +98 -0
- package/src/session/spawn.mjs +373 -20
- package/src/state/block.mjs +13 -4
- package/src/state/config-snapshot.mjs +150 -0
- package/src/state/run.mjs +86 -0
- package/src/util/breadcrumbs.mjs +145 -0
- package/src/util/error-hint.mjs +3 -2
- package/src/util/error-kind.mjs +2 -2
- package/src/util/error-log.mjs +109 -18
- package/src/util/forbidden.mjs +3 -1
- package/src/util/known-fixes.json +1 -0
- package/src/util/known-fixes.mjs +153 -0
- package/src/util/locks.mjs +104 -0
- package/src/worker/engine.mjs +122 -40
- package/src/worker/loop.mjs +326 -78
- package/src/worker/queue.mjs +68 -7
package/src/engines/sentinel.mjs
CHANGED
|
@@ -131,3 +131,37 @@ export function verifyAck(output, expectedSha8, expectedLines, opts = {}) {
|
|
|
131
131
|
}
|
|
132
132
|
return { status: 'ok', ack };
|
|
133
133
|
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* A `review-file` ticket id: 24 lowercase hex characters standing alone — not part of a longer hex
|
|
137
|
+
* run (a commit sha) and not inside a path or file name (no word, `.`, `/` or `-` touching it).
|
|
138
|
+
*/
|
|
139
|
+
const TICKET_IN_LINE = /(?<![\w./-])[0-9a-f]{24}(?![\w/-]|\.\w)/;
|
|
140
|
+
|
|
141
|
+
/** @param {string} s */
|
|
142
|
+
const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The coder's final report must name the `review-file` ticket of every changed file (B36): a line
|
|
146
|
+
* that names the path and a ticket id, e.g. `reviewed src/a.mjs ticket 0123456789abcdef01234567`.
|
|
147
|
+
* A line counts for a file only when it names exactly ONE of the changed files (a line listing two
|
|
148
|
+
* paths counts for neither) and carries a ticket id outside that path (the path is blanked before
|
|
149
|
+
* the ticket is looked for). Fail closed: a file with no such line is missing, and the
|
|
150
|
+
* orchestrator treats the report as FAILED whatever its sentinel says
|
|
151
|
+
* (`skill/templates/coder-brief.md`).
|
|
152
|
+
* @param {string} report - the coder's final report (or its whole transcript).
|
|
153
|
+
* @param {string[]} files - the block's changed files, repo-root-relative.
|
|
154
|
+
* @returns {string[]} the files with no ticket id in the report, in the order given.
|
|
155
|
+
*/
|
|
156
|
+
export function missingReviewTickets(report, files) {
|
|
157
|
+
if (typeof report !== 'string') throw new TypeError('missingReviewTickets: report must be a string');
|
|
158
|
+
const named = files.map((file) => ({ file, re: new RegExp(`(?<![\\w./-])${escapeRe(file)}(?![\\w/-]|\\.\\w)`, 'g') }));
|
|
159
|
+
/** @type {Set<string>} */
|
|
160
|
+
const ticketed = new Set();
|
|
161
|
+
for (const line of report.split('\n')) {
|
|
162
|
+
const hits = named.filter((n) => line.search(n.re) >= 0);
|
|
163
|
+
if (hits.length !== 1) continue;
|
|
164
|
+
if (TICKET_IN_LINE.test(line.replace(hits[0].re, ' '))) ticketed.add(hits[0].file);
|
|
165
|
+
}
|
|
166
|
+
return files.filter((file) => !ticketed.has(file));
|
|
167
|
+
}
|
package/src/keys/onepassword.mjs
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import { reportErrorKind } from '../util/error-kind.mjs';
|
|
15
|
+
import { logWarning } from '../util/error-log.mjs';
|
|
15
16
|
import { exec as realExec } from '../util/exec.mjs';
|
|
16
17
|
|
|
17
18
|
// Long enough for a person to see and approve the 1Password prompt (Touch ID or password).
|
|
@@ -172,6 +173,8 @@ async function runOp(argv, exec, timeoutMs) {
|
|
|
172
173
|
attempts += 1;
|
|
173
174
|
res = await exec(argv, { timeoutMs });
|
|
174
175
|
} while (res.result !== 'ok' && res.timedOut && attempts === 1);
|
|
176
|
+
// B37: a call that needed its retry is a recoverable problem worth a warning line
|
|
177
|
+
if (attempts > 1) await logWarning({ warning: 'op_retry', message: '1Password did not answer in time; the call was retried once' }).catch(() => null);
|
|
175
178
|
return { res, attempts };
|
|
176
179
|
}
|
|
177
180
|
|
package/src/ledger/report.mjs
CHANGED
|
@@ -8,14 +8,15 @@
|
|
|
8
8
|
* Row conventions read here: `dispatch` {block,level,lane,lines}; `block.close`
|
|
9
9
|
* {block,status,lines_actual} — ANY `block.close` row marks the block completed (a stop is
|
|
10
10
|
* `block.stop`; B19); `review.plan` {block,tier,depth_unconstrained,depth_chosen,
|
|
11
|
-
* degrade_step}; `session` {block,role,provider,level,tokens_in,tokens_out,
|
|
12
|
-
* coder/reviewer/judge/s2/author/facts spend
|
|
13
|
-
*
|
|
11
|
+
* degrade_step}; `session` {block,role,provider,level,tokens_in,tokens_out,usd,usd_unknown?} — one row per
|
|
12
|
+
* coder/reviewer/judge/s2/author/facts spend (plus `ledger add coder` manual rows and
|
|
13
|
+
* `session.background` rows, whose price is unknown); its dollars come from `./spend.mjs`
|
|
14
|
+
* (`usd`, else `cost_usd`, else ESTIMATED from its tokens × `./prices.mjs`); `review.done` {block,file,context_mode,findings_by_severity,round}; `decision`
|
|
14
15
|
* {block,question,source}; `outcome` {block,missed_after_degrade}; `escalation` {block,trigger};
|
|
15
16
|
* `review.unavailable` {block,reason}; `proof` {block,duration_ms}; `run.stop` {reason}.
|
|
16
17
|
*/
|
|
17
18
|
|
|
18
|
-
import {
|
|
19
|
+
import { isSpendRow, rowUsd, spendByRun, sumSpend } from './spend.mjs';
|
|
19
20
|
|
|
20
21
|
const ROLE_BUCKET = Object.freeze({
|
|
21
22
|
coder: 'coderUsd',
|
|
@@ -38,30 +39,60 @@ function countBy(rows, keyOf) {
|
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
/**
|
|
41
|
-
* A session row's dollars: `
|
|
42
|
-
*
|
|
42
|
+
* A session row's dollars (B33: its `usd`; null when `usd_unknown`; older rows: `cost_usd`, else
|
|
43
|
+
* tokens × the static price table); null when the price is unknown.
|
|
43
44
|
* @param {Record<string, any>} row @returns {number | null}
|
|
44
45
|
*/
|
|
45
46
|
export function sessionCostUsd(row) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
return rowUsd(row);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* B33: spend per run (running totals, open blocks included) and over the whole ledger — kept
|
|
52
|
+
* beside the 13 sections, not as a 14th (`SECTION_NAMES` stays pinned at 13).
|
|
53
|
+
* @param {Array<Record<string, any>>} rows
|
|
54
|
+
* @returns {{runs: Array<{run: string | null, usd: number, unknown: number, sessions: number}>, total: {usd: number, unknown: number, sessions: number}}}
|
|
55
|
+
*/
|
|
56
|
+
export function buildSpend(rows) {
|
|
57
|
+
const acted = rows.filter((r) => r.source !== 'shadow');
|
|
58
|
+
return { runs: spendByRun(acted), total: sumSpend(acted) };
|
|
55
59
|
}
|
|
56
60
|
|
|
57
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* Cost per block — completed AND still open (B33: a running total while the block is open):
|
|
63
|
+
* review $ / coder $ / S1+S2 $ / facts $ / proof ms, `completed` (any `block.close` row) and
|
|
64
|
+
* `unknownUsdSessions` (spend rows whose price is unknown, counted 0 in the totals). Grouped by
|
|
65
|
+
* run + block, so a block id reused in a second run is its own entry; a row without a run joins
|
|
66
|
+
* the entry of its block when that block id appears in exactly one run (else a `run: null` entry); shadow rows are dropped here too, exactly as `buildSpend` does. An entry is listed
|
|
67
|
+
* once it has a spend row or a `block.close` row.
|
|
68
|
+
*/
|
|
58
69
|
function costPerBlock(rows) {
|
|
59
70
|
/** @type {Map<string, any>} */
|
|
60
71
|
const byBlock = new Map();
|
|
72
|
+
/** @type {Set<string>} run+block keys with at least one spend row */
|
|
73
|
+
const spent = new Set();
|
|
74
|
+
// never split one block: a row without a run joins its block's entry when that block id appears
|
|
75
|
+
// in exactly one run; only a block seen in several runs leaves such a row in a `run: null` entry
|
|
76
|
+
/** @type {Map<string, Set<string>>} */
|
|
77
|
+
const runsOfBlock = new Map();
|
|
78
|
+
for (const row of rows) {
|
|
79
|
+
if (!row.block || row.source === 'shadow' || typeof row.run !== 'string') continue;
|
|
80
|
+
if (!runsOfBlock.has(row.block)) runsOfBlock.set(row.block, new Set());
|
|
81
|
+
/** @type {Set<string>} */ (runsOfBlock.get(row.block)).add(row.run);
|
|
82
|
+
}
|
|
83
|
+
/** @param {Record<string, any>} row @returns {string | null} */
|
|
84
|
+
const runOf = (row) => {
|
|
85
|
+
if (typeof row.run === 'string') return row.run;
|
|
86
|
+
const runs = runsOfBlock.get(row.block);
|
|
87
|
+
return runs && runs.size === 1 ? [...runs][0] : null;
|
|
88
|
+
};
|
|
61
89
|
for (const row of rows) {
|
|
62
|
-
if (!row.block) continue;
|
|
63
|
-
|
|
64
|
-
|
|
90
|
+
if (!row.block || row.source === 'shadow') continue;
|
|
91
|
+
const run = runOf(row);
|
|
92
|
+
const key = `${run ?? ''}\u0000${row.block}`;
|
|
93
|
+
if (!byBlock.has(key)) {
|
|
94
|
+
byBlock.set(key, {
|
|
95
|
+
run,
|
|
65
96
|
block: row.block,
|
|
66
97
|
level: null,
|
|
67
98
|
lane: null,
|
|
@@ -73,26 +104,32 @@ function costPerBlock(rows) {
|
|
|
73
104
|
otherUsd: 0,
|
|
74
105
|
proofTimeMs: 0,
|
|
75
106
|
totalUsd: 0,
|
|
107
|
+
unknownUsdSessions: 0,
|
|
76
108
|
});
|
|
77
109
|
}
|
|
78
|
-
const e = byBlock.get(
|
|
110
|
+
const e = byBlock.get(key);
|
|
79
111
|
if (row.event === 'dispatch') {
|
|
80
112
|
e.level = row.level ?? e.level;
|
|
81
113
|
e.lane = row.lane ?? e.lane;
|
|
82
114
|
}
|
|
83
115
|
if (row.event === 'block.close') e.completed = true;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
116
|
+
if (isSpendRow(row)) {
|
|
117
|
+
spent.add(key);
|
|
118
|
+
const cost = sessionCostUsd(row);
|
|
119
|
+
if (cost === null) {
|
|
120
|
+
e.unknownUsdSessions += 1;
|
|
121
|
+
} else {
|
|
122
|
+
// An unrecognized role still spent real money — bucket it as `otherUsd` instead of
|
|
123
|
+
// silently dropping it from every total (fix round 1: an unknown role must not make
|
|
124
|
+
// totalUsd under-report actual spend).
|
|
125
|
+
const bucket = ROLE_BUCKET[row.role] ?? 'otherUsd';
|
|
126
|
+
e[bucket] = (e[bucket] ?? 0) + cost;
|
|
127
|
+
e.totalUsd += cost;
|
|
128
|
+
}
|
|
92
129
|
}
|
|
93
130
|
if (row.event === 'proof' && typeof row.duration_ms === 'number') e.proofTimeMs += row.duration_ms;
|
|
94
131
|
}
|
|
95
|
-
const out = [...byBlock.
|
|
132
|
+
const out = [...byBlock.entries()].filter(([key, e]) => e.completed || spent.has(key)).map(([, e]) => e);
|
|
96
133
|
for (const e of out) {
|
|
97
134
|
for (const k of ['reviewUsd', 'coderUsd', 's1s2Usd', 'factsUsd', 'otherUsd', 'totalUsd']) e[k] = Math.round(e[k] * 10000) / 10000;
|
|
98
135
|
}
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run spend in USD (B33, plan rule R6): the one place that turns ledger rows into dollars for
|
|
3
|
+
* `report`, `run status`, the `budget.usd` stop in `spawnSession`, and `ledger add coder`.
|
|
4
|
+
*
|
|
5
|
+
* Dollars are estimates from the static price table (`./prices.mjs`, provider × level, blended
|
|
6
|
+
* in+out per 1K tokens). A row whose price is unknown carries `usd: null, usd_unknown: true` —
|
|
7
|
+
* never a guess — and counts as 0 in totals while being counted in `unknown`.
|
|
8
|
+
*
|
|
9
|
+
* Spend rows: `event: 'session'` (one per foreground attempt, plus the manual/cloud coder rows
|
|
10
|
+
* `ledger add coder` writes with `manual: true`) and `event: 'session.background'` (a detached
|
|
11
|
+
* coder: its tokens are never seen, so its price is unknown). Shadow rows never count.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { logWarning } from '../util/error-log.mjs';
|
|
15
|
+
import { keyedLock } from '../util/locks.mjs';
|
|
16
|
+
import { writeSafe } from '../util/redact.mjs';
|
|
17
|
+
import { estimateCostUsd } from './prices.mjs';
|
|
18
|
+
|
|
19
|
+
/** The ledger events that carry a session's spend. */
|
|
20
|
+
export const SPEND_EVENTS = Object.freeze(['session', 'session.background']);
|
|
21
|
+
|
|
22
|
+
/** Round to 4 decimals (the price table's precision). @param {number} n */
|
|
23
|
+
export const roundUsd = (n) => Math.round(n * 10000) / 10000;
|
|
24
|
+
|
|
25
|
+
/** A usable amount: a finite number ≥ 0 (tokens or dollars). @param {unknown} v @returns {v is number} */
|
|
26
|
+
export const isAmount = (v) => typeof v === 'number' && Number.isFinite(v) && v >= 0;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Price one session from its token counts. Both counts must be finite numbers ≥ 0 — a null,
|
|
30
|
+
* missing, NaN, negative or string count is unknown (never read as 0) — and the provider/level
|
|
31
|
+
* must be in the price table; anything else ⇒ `{usd: null, usd_unknown: true}`.
|
|
32
|
+
* @param {{provider?: unknown, level?: unknown, tokens_in?: unknown, tokens_out?: unknown}} row
|
|
33
|
+
* @returns {{usd: number, usd_unknown: false} | {usd: null, usd_unknown: true}}
|
|
34
|
+
*/
|
|
35
|
+
export function priceSession({ provider, level, tokens_in, tokens_out }) {
|
|
36
|
+
if (!isAmount(tokens_in) || !isAmount(tokens_out)) return { usd: null, usd_unknown: true };
|
|
37
|
+
try {
|
|
38
|
+
const usd = estimateCostUsd({ provider: /** @type {string} */ (provider), level: /** @type {string} */ (level), tokensIn: tokens_in, tokensOut: tokens_out });
|
|
39
|
+
return isAmount(usd) ? { usd, usd_unknown: false } : { usd: null, usd_unknown: true };
|
|
40
|
+
} catch {
|
|
41
|
+
return { usd: null, usd_unknown: true };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @param {Record<string, any>} row @returns {boolean} */
|
|
46
|
+
export const isSpendRow = (row) => SPEND_EVENTS.includes(row?.event) && row.source !== 'shadow';
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A spend row's dollars, or null when unknown. `usd` when the row carries the key (a non-amount —
|
|
50
|
+
* null, NaN, Infinity, negative, a string — is unknown); `usd_unknown: true` is unknown; older rows
|
|
51
|
+
* (before B33): `cost_usd` when present (a non-amount is unknown), else tokens × the price table.
|
|
52
|
+
* @param {Record<string, any>} row @returns {number | null}
|
|
53
|
+
*/
|
|
54
|
+
export function rowUsd(row) {
|
|
55
|
+
if (row.usd !== undefined) return isAmount(row.usd) ? row.usd : null;
|
|
56
|
+
if (row.usd_unknown === true) return null;
|
|
57
|
+
if (row.cost_usd !== undefined && row.cost_usd !== null) return isAmount(row.cost_usd) ? row.cost_usd : null;
|
|
58
|
+
if (row.event === 'session.background') return null;
|
|
59
|
+
return priceSession(row).usd;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Total spend of the rows: `usd` (unknown rows count 0), `unknown` (rows whose price is unknown),
|
|
64
|
+
* `sessions` (spend rows counted).
|
|
65
|
+
* @param {Array<Record<string, any>>} rows
|
|
66
|
+
* @returns {{usd: number, unknown: number, sessions: number}}
|
|
67
|
+
*/
|
|
68
|
+
export function sumSpend(rows) {
|
|
69
|
+
let usd = 0;
|
|
70
|
+
let unknown = 0;
|
|
71
|
+
let sessions = 0;
|
|
72
|
+
for (const row of rows) {
|
|
73
|
+
if (!isSpendRow(row)) continue;
|
|
74
|
+
sessions += 1;
|
|
75
|
+
const cost = rowUsd(row);
|
|
76
|
+
if (cost === null) unknown += 1;
|
|
77
|
+
else usd += cost;
|
|
78
|
+
}
|
|
79
|
+
return { usd: roundUsd(usd), unknown, sessions };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Spend of one run (rows whose `run` is `runId`).
|
|
84
|
+
* @param {Array<Record<string, any>>} rows @param {string} runId
|
|
85
|
+
*/
|
|
86
|
+
export const runSpend = (rows, runId) => sumSpend(rows.filter((r) => r.run === runId));
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Spend per run, in first-seen order; rows without a run are grouped under `null`.
|
|
90
|
+
* @param {Array<Record<string, any>>} rows
|
|
91
|
+
* @returns {Array<{run: string | null, usd: number, unknown: number, sessions: number}>}
|
|
92
|
+
*/
|
|
93
|
+
export function spendByRun(rows) {
|
|
94
|
+
/** @type {Map<string | null, Array<Record<string, any>>>} */
|
|
95
|
+
const groups = new Map();
|
|
96
|
+
for (const row of rows) {
|
|
97
|
+
if (!isSpendRow(row)) continue;
|
|
98
|
+
const key = typeof row.run === 'string' ? row.run : null;
|
|
99
|
+
if (!groups.has(key)) groups.set(key, []);
|
|
100
|
+
/** @type {Array<Record<string, any>>} */ (groups.get(key)).push(row);
|
|
101
|
+
}
|
|
102
|
+
return [...groups.entries()].map(([run, list]) => ({ run, ...sumSpend(list) }));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* `budget.usd` from a loaded config: a finite number > 0, else null (no budget).
|
|
107
|
+
* @param {Record<string, any> | null | undefined} cfg @returns {number | null}
|
|
108
|
+
*/
|
|
109
|
+
export function budgetUsdOf(cfg) {
|
|
110
|
+
const v = cfg?.budget?.usd;
|
|
111
|
+
return typeof v === 'number' && Number.isFinite(v) && v > 0 ? v : null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The plain refusal text (R6). @param {number} budget @param {number} spent */
|
|
115
|
+
export const budgetReachedMessage = (budget, spent) => `budget.usd ${budget.toFixed(2)} reached (spent ${spent.toFixed(2)}); raise budget.usd or end the run`;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* B41: the refusal text when the ledger spend is under the budget but the sessions still running
|
|
119
|
+
* (reserved) plus this session's estimate would pass it.
|
|
120
|
+
* @param {number} budget @param {number} spent @param {number} reserved @param {number} estimate
|
|
121
|
+
*/
|
|
122
|
+
export const budgetWouldExceedMessage = (budget, spent, reserved, estimate) =>
|
|
123
|
+
reserved > 0
|
|
124
|
+
? `budget.usd ${budget.toFixed(2)} would be exceeded: spent ${spent.toFixed(2)}, reserved by running sessions ${reserved.toFixed(2)}, this session about ${estimate.toFixed(2)}; it starts when running sessions finish or when budget.usd is raised`
|
|
125
|
+
: `budget.usd ${budget.toFixed(2)} would be exceeded: spent ${spent.toFixed(2)}, this session about ${estimate.toFixed(2)}; raise budget.usd or end the run`;
|
|
126
|
+
|
|
127
|
+
/** B41: the refusal text when this session's estimate alone is above the budget. @param {number} budget @param {number} estimate */
|
|
128
|
+
export const budgetEstimateAboveMessage = (budget, estimate) => `this session's estimated cost (about ${estimate.toFixed(2)}) is above budget.usd ${budget.toFixed(2)}; raise budget.usd`;
|
|
129
|
+
|
|
130
|
+
/** The one-time warning text at 80 %. @param {number} budget @param {number} spent */
|
|
131
|
+
export const budgetWarningMessage = (budget, spent) => `budget.usd: spent ${spent.toFixed(2)} of ${budget.toFixed(2)} (≥ 80%); new sessions stop at 100%`;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Runs this process warned about — the FALLBACK dedupe, used only when the warning row could not
|
|
135
|
+
* be written (no writer, or the write failed). The primary record is the `budget.warning` row.
|
|
136
|
+
*/
|
|
137
|
+
const warnedRuns = new Set();
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The budget check run before every session spawn. Reads the run's spend from `rows` (only rows
|
|
141
|
+
* whose `run` is `run` count): spent ≥ budget, or (B41) spent + reserved + estimate > budget ⇒
|
|
142
|
+
* `{refuse: true}` and a `budget.refused` row (with `reserved_usd`/`estimate_usd` when either is > 0);
|
|
143
|
+
* the message is the B33 text when spent ≥ budget, else `budgetEstimateAboveMessage` when the
|
|
144
|
+
* estimate alone is above the budget, else `budgetWouldExceedMessage`;
|
|
145
|
+
* spent ≥ 80 % of budget ⇒ one `budget.warning` row and stderr line per run — skipped when the
|
|
146
|
+
* ledger already holds a `budget.warning` row for the run.
|
|
147
|
+
* @param {{budget: number, run: string, rows: Array<Record<string, any>>, block?: string | null, role?: string,
|
|
148
|
+
* writeRow?: ((row: Record<string, any>) => Promise<unknown>) | null, stderr: {write: (s: string) => unknown},
|
|
149
|
+
* reserved?: number, estimate?: number}} opts - B41: `reserved` (other sessions' holds) and
|
|
150
|
+
* `estimate` (this session) join the refusal test; the 80 % warning still reads `spent` only.
|
|
151
|
+
* @returns {Promise<{refuse: boolean, spent: number, message?: string}>}
|
|
152
|
+
*/
|
|
153
|
+
export async function checkBudget({ budget, run, rows, block = null, role, writeRow = null, stderr, reserved = 0, estimate = 0 }) {
|
|
154
|
+
const { usd: spent, unknown } = runSpend(rows, run);
|
|
155
|
+
const held = isAmount(reserved) ? reserved : 0;
|
|
156
|
+
const next = isAmount(estimate) ? estimate : 0;
|
|
157
|
+
const base = { run, block, ...(role ? { role } : {}), budget_usd: budget, spent_usd: spent, unknown_usd_sessions: unknown };
|
|
158
|
+
// B41: the sessions still running (reserved) and this one (estimate) count too
|
|
159
|
+
if (spent >= budget || roundUsd(spent + held + next) > budget) {
|
|
160
|
+
// spent ≥ budget: the B33 text; else the estimate alone, else the running sessions' holds
|
|
161
|
+
const message = spent >= budget ? budgetReachedMessage(budget, spent) : next > budget ? budgetEstimateAboveMessage(budget, next) : budgetWouldExceedMessage(budget, spent, held, next);
|
|
162
|
+
writeSafe(stderr, `code-forge: ${message}\n`);
|
|
163
|
+
if (writeRow) {
|
|
164
|
+
try {
|
|
165
|
+
await writeRow({ event: 'budget.refused', ...base, ...(held > 0 || next > 0 ? { reserved_usd: held, estimate_usd: next } : {}) });
|
|
166
|
+
} catch {
|
|
167
|
+
// the refusal stands without its row
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return { refuse: true, spent, message };
|
|
171
|
+
}
|
|
172
|
+
// compared at the ledger's 4-decimal precision, so 0.24 of 0.30 is exactly 80 %
|
|
173
|
+
if (spent >= roundUsd(0.8 * budget) && !rows.some((r) => r.event === 'budget.warning' && r.run === run)) {
|
|
174
|
+
let recorded = false;
|
|
175
|
+
if (writeRow) {
|
|
176
|
+
try {
|
|
177
|
+
await writeRow({ event: 'budget.warning', ...base });
|
|
178
|
+
recorded = true;
|
|
179
|
+
} catch {
|
|
180
|
+
recorded = false; // fall back to the in-process dedupe below
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (recorded || !warnedRuns.has(run)) {
|
|
184
|
+
writeSafe(stderr, `code-forge: ${budgetWarningMessage(budget, spent)}\n`);
|
|
185
|
+
warnedRuns.add(run); // after the write attempt, never before
|
|
186
|
+
// B37: a warning line in the local error log — never the amounts (config values)
|
|
187
|
+
await logWarning({ warning: 'budget_warning', message: 'budget.usd: 80% of the run budget is spent; new sessions stop at 100%' }).catch(() => null);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return { refuse: false, spent };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The fail-closed refusal text: a budget is set but the run's spend cannot be read. */
|
|
194
|
+
export const SPEND_UNREADABLE_MESSAGE = "budget.usd is set but the run's spend cannot be read; not starting a session";
|
|
195
|
+
|
|
196
|
+
/*
|
|
197
|
+
* B41 — budget reservations under concurrency.
|
|
198
|
+
*
|
|
199
|
+
* Several sessions of one run can start at once (B40 slots, B42 ticket pool). The ledger only
|
|
200
|
+
* learns a session's cost when its row is written, so a plain "spent < budget" check lets every
|
|
201
|
+
* session that starts before the first row lands go through. Instead the check AND a reservation
|
|
202
|
+
* of the session's estimated cost happen under ONE in-process lock per run (`withBudgetLock`):
|
|
203
|
+
*
|
|
204
|
+
* spent ≥ budget, or spent + reserved + estimate > budget ⇒ refused (the B33 message, unchanged)
|
|
205
|
+
*
|
|
206
|
+
* The reservation is held until the session row is written (or the spawn fails, throws or is
|
|
207
|
+
* refused) and then released. Reservations live in this process only (module level, keyed by run).
|
|
208
|
+
*
|
|
209
|
+
* The estimate (`estimateSessionUsd`): tokens_in = the packet's bytes / 4 (the ledger's own
|
|
210
|
+
* `estimateTokens` rule) and a fixed, deliberately high tokens_out per role and level
|
|
211
|
+
* (`TOKENS_OUT_ESTIMATE`), priced with the static table. A provider/level with no price reserves 0.
|
|
212
|
+
*/
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Conservative tokens_out per role and level, used only for reservations. Coders run an agentic
|
|
216
|
+
* tool loop whose billed tokens dwarf the brief, so they get the largest figures; an author writes a
|
|
217
|
+
* whole plan; reviewers, facts and judges return one structured answer; S2 a short judgement.
|
|
218
|
+
* Each is several times what a typical session of that kind reports.
|
|
219
|
+
*/
|
|
220
|
+
export const TOKENS_OUT_ESTIMATE = Object.freeze({
|
|
221
|
+
coder: Object.freeze({ L0: 8000, L1: 16000, L2: 32000, L3: 64000 }),
|
|
222
|
+
author: Object.freeze({ L0: 8000, L1: 12000, L2: 16000, L3: 24000 }),
|
|
223
|
+
reviewer: Object.freeze({ L0: 4000, L1: 6000, L2: 8000, L3: 12000 }),
|
|
224
|
+
facts: Object.freeze({ L0: 4000, L1: 6000, L2: 8000, L3: 12000 }),
|
|
225
|
+
judge: Object.freeze({ L0: 2000, L1: 3000, L2: 4000, L3: 6000 }),
|
|
226
|
+
s2: Object.freeze({ L0: 1000, L1: 1500, L2: 2000, L3: 3000 }),
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
/** tokens_out when the role or level is not in the table (the coder L3 figure: the largest). */
|
|
230
|
+
export const TOKENS_OUT_FALLBACK = 64000;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* The dollars reserved for one session before it starts. `tokensIn` not an amount ⇒ 0 in.
|
|
234
|
+
* @param {{provider: string, level: string, role: string, tokensIn?: number | null}} args
|
|
235
|
+
* @returns {number} USD at 4 decimals; 0 when the provider/level has no price.
|
|
236
|
+
*/
|
|
237
|
+
export function estimateSessionUsd({ provider, level, role, tokensIn }) {
|
|
238
|
+
const out = /** @type {Record<string, Record<string, number>>} */ (TOKENS_OUT_ESTIMATE)[role]?.[level] ?? TOKENS_OUT_FALLBACK;
|
|
239
|
+
const priced = priceSession({ provider, level, tokens_in: isAmount(tokensIn) ? tokensIn : 0, tokens_out: out });
|
|
240
|
+
return priced.usd ?? 0;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** @type {Map<string, Map<number, number>>} run → (reservation id → USD) */
|
|
244
|
+
const reservations = new Map();
|
|
245
|
+
let nextReservation = 0;
|
|
246
|
+
let lockCalls = 0;
|
|
247
|
+
|
|
248
|
+
/** USD reserved right now for `run` by sessions that have not written their row. @param {string} run */
|
|
249
|
+
export function reservedUsd(run) {
|
|
250
|
+
let sum = 0;
|
|
251
|
+
for (const usd of reservations.get(run)?.values() ?? []) sum += usd;
|
|
252
|
+
return roundUsd(sum);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Reserve `usd` for `run`; call only inside {@link withBudgetLock}. Returns an idempotent release.
|
|
257
|
+
* @param {string} run @param {number} usd @returns {() => void}
|
|
258
|
+
*/
|
|
259
|
+
export function reserveBudget(run, usd) {
|
|
260
|
+
const id = ++nextReservation;
|
|
261
|
+
let map = reservations.get(run);
|
|
262
|
+
if (!map) reservations.set(run, (map = new Map()));
|
|
263
|
+
map.set(id, isAmount(usd) ? usd : 0);
|
|
264
|
+
return () => {
|
|
265
|
+
const m = reservations.get(run);
|
|
266
|
+
if (!m) return;
|
|
267
|
+
m.delete(id);
|
|
268
|
+
if (m.size === 0) reservations.delete(run);
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Run `fn` under the run's budget lock: budget checks of one run never interleave.
|
|
274
|
+
* @template T @param {string} run @param {() => T | Promise<T>} fn @returns {Promise<T>}
|
|
275
|
+
*/
|
|
276
|
+
export function withBudgetLock(run, fn) {
|
|
277
|
+
lockCalls += 1;
|
|
278
|
+
return keyedLock(`budget:${run}`, fn);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Test spy: how many times {@link withBudgetLock} was entered since the last reset. */
|
|
282
|
+
export const budgetLockCalls = () => lockCalls;
|
|
283
|
+
|
|
284
|
+
/** Test helper: drop every reservation and zero the lock counter. */
|
|
285
|
+
export function resetBudgetReservations() {
|
|
286
|
+
reservations.clear();
|
|
287
|
+
lockCalls = 0;
|
|
288
|
+
}
|
package/src/review/engine.mjs
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* 1. plans the sessions (`planSessions`):
|
|
6
6
|
* adaptive (default) — `risk < 1` ⇒ one L2 `quick`; `1 ≤ risk < 2` ⇒ one L2 `full`;
|
|
7
7
|
* `risk ≥ 2` ⇒ two blind L2 sessions (lens `A`, lens `B`) + one L3 `judge`;
|
|
8
|
-
* consensus (`review.multimodel: true
|
|
8
|
+
* consensus (`review.multimodel: true`, risk above `review.single_reviewer_max_risk` (1),
|
|
9
|
+
* and not a docs/contract block unless `review.multimodel_for_docs`; B34) — one L2 `full` per EFFECTIVE provider
|
|
9
10
|
* (`resolve(L2)` and `review.second_levels.L2` / `review.second_provider`) + one L3 judge;
|
|
10
11
|
* the same effective provider twice is refused (`consensus-same-provider`);
|
|
11
12
|
* 2. reads the diff once and assembles one packet per lens (`packet.mjs`);
|
|
@@ -15,7 +16,10 @@
|
|
|
15
16
|
* is `unavailable` with its reason and is NEVER approval; a failed lens means no judge;
|
|
16
17
|
* 5. a valid answer with `needs_file` gets ONE more round with those files attached (git-tracked
|
|
17
18
|
* files only, never secret-like paths — `attachFiles`); a second `needs_file` is ignored; a
|
|
18
|
-
* request with a non-repo-relative path is refused (`needs_file-refused`, never approval)
|
|
19
|
+
* request with a non-repo-relative path is refused (`needs_file-refused`, never approval);
|
|
20
|
+
* 6. a session that ends in `timeout` is spawned ONCE more on the same packet and level
|
|
21
|
+
* (`session-retry.mjs`, B30); a second timeout is `unavailable: timeout`. The summary's
|
|
22
|
+
* `attempts` says how many ran.
|
|
19
23
|
* Approval (until B12b's triage lands) = the final answer (the judge's in dual/consensus mode, else
|
|
20
24
|
* the single reviewer's) passed the guard, says `passed: true`, and carries no `critical` or
|
|
21
25
|
* `warning` finding.
|
|
@@ -25,10 +29,12 @@ import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
|
25
29
|
import { randomBytes } from 'node:crypto';
|
|
26
30
|
import path from 'node:path';
|
|
27
31
|
import { resolveLevel } from '../config/known-ids.mjs';
|
|
32
|
+
import { blockKind, isDocsKind } from '../decide/escalation.mjs';
|
|
28
33
|
import { fallbackRisk } from '../decide/fallback-rules.mjs';
|
|
29
34
|
import { tierFor } from '../proof/tiers.mjs';
|
|
30
35
|
import { assembleJudgePacket, assemblePacket, attachFiles, budgetFor, readFileDiff } from './packet.mjs';
|
|
31
36
|
import { FINDING_SCHEMA, minTokensOut, SMALL_DIFF_ADDED_LINES, validateReview } from './validate-review.mjs';
|
|
37
|
+
import { spawnWithTimeoutRetry } from './session-retry.mjs';
|
|
32
38
|
import { assertRowPath } from '../worker/ticket.mjs';
|
|
33
39
|
|
|
34
40
|
/**
|
|
@@ -60,13 +66,31 @@ export function secondLevel(cfg) {
|
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
/**
|
|
63
|
-
*
|
|
69
|
+
* `review.single_reviewer_max_risk` (B34, default 1): at or below it ONE reviewer runs, no judge.
|
|
70
|
+
* @param {Record<string, any> | undefined} cfg @returns {number}
|
|
71
|
+
*/
|
|
72
|
+
export function singleReviewerMaxRisk(cfg) {
|
|
73
|
+
const v = cfg?.review?.single_reviewer_max_risk;
|
|
74
|
+
// `validate` rejects anything outside 0–3 (schema); this guard only covers an unvalidated config
|
|
75
|
+
return typeof v === 'number' && Number.isFinite(v) && v >= 0 && v <= 3 ? v : 1;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The review topology — the ONE place it is decided (B34):
|
|
80
|
+
* - consensus runs only when `review.multimodel` is true, the block is not a docs/contract block
|
|
81
|
+
* (unless `review.multimodel_for_docs` is true), and `risk > review.single_reviewer_max_risk`;
|
|
82
|
+
* - otherwise adaptive: `risk ≤ single_reviewer_max_risk` or `risk < 2` ⇒ one L2 session
|
|
83
|
+
* (`quick` below risk 1, else `full`); else two blind lenses + an L3 judge.
|
|
84
|
+
* @param {{risk: number, cfg: Record<string, any>, kind?: unknown}} opts - `kind`: the block kind
|
|
85
|
+
* (`blockKind`), default `code`.
|
|
64
86
|
* @returns {SessionPlan}
|
|
65
87
|
*/
|
|
66
|
-
export function planSessions({ risk, cfg }) {
|
|
88
|
+
export function planSessions({ risk, cfg, kind = 'code' }) {
|
|
67
89
|
if (typeof risk !== 'number' || !Number.isFinite(risk) || risk < 0 || risk > 3) throw new TypeError('planSessions: risk must be a number from 0 to 3');
|
|
68
90
|
const judge = /** @type {SessionSpec} */ ({ lens: 'judge', role: 'judge', level: 'L3', slot: null });
|
|
69
|
-
|
|
91
|
+
const single = risk <= singleReviewerMaxRisk(cfg);
|
|
92
|
+
const docsOff = isDocsKind(kind) && cfg?.review?.multimodel_for_docs !== true;
|
|
93
|
+
if (cfg?.review?.multimodel === true && !single && !docsOff) {
|
|
70
94
|
const second = secondLevel(cfg);
|
|
71
95
|
if (!second.ok) return { mode: 'consensus', depth: null, sessions: [], refused: second.reason };
|
|
72
96
|
return {
|
|
@@ -80,7 +104,7 @@ export function planSessions({ risk, cfg }) {
|
|
|
80
104
|
};
|
|
81
105
|
}
|
|
82
106
|
if (risk < 1) return { mode: 'adaptive', depth: 'quick', sessions: [{ lens: 'quick', role: 'reviewer', level: 'L2', slot: null }] };
|
|
83
|
-
if (risk < 2) return { mode: 'adaptive', depth: 'full', sessions: [{ lens: 'full', role: 'reviewer', level: 'L2', slot: null }] };
|
|
107
|
+
if (risk < 2 || single) return { mode: 'adaptive', depth: 'full', sessions: [{ lens: 'full', role: 'reviewer', level: 'L2', slot: null }] };
|
|
84
108
|
return {
|
|
85
109
|
mode: 'adaptive',
|
|
86
110
|
depth: 'dual',
|
|
@@ -117,6 +141,7 @@ export function rulesRisk({ file, plusCount, cfg }) {
|
|
|
117
141
|
* @property {string | null} [base] - the block's base SHA (null ⇒ HEAD).
|
|
118
142
|
* @property {Record<string, any>} cfg
|
|
119
143
|
* @property {number} [risk] - the S1 risk (0–3); absent ⇒ the rules risk.
|
|
144
|
+
* @property {string} [kind] - the block kind (`code`, `docs`, `contract`; B34); absent ⇒ `blockKind` of the reviewed file.
|
|
120
145
|
* @property {string} [rulesDigest] @property {string} [factsExcerpt]
|
|
121
146
|
* @property {string} workDir - where packet files are written (under the run's temp root).
|
|
122
147
|
*/
|
|
@@ -161,7 +186,9 @@ export async function reviewFile(input, deps) {
|
|
|
161
186
|
const { repoRoot, cfg, workDir } = input;
|
|
162
187
|
const diff = await readFileDiff({ repoRoot, file: input.file, base: input.base ?? null });
|
|
163
188
|
const risk = input.risk ?? rulesRisk({ file: diff.file, plusCount: diff.plusCount, cfg });
|
|
164
|
-
|
|
189
|
+
// no recorded kind ⇒ the reviewed file's own kind (a lone `.md` is a docs review), never a blind `code`
|
|
190
|
+
const kind = typeof input.kind === 'string' ? input.kind : blockKind({ owned: [diff.file] });
|
|
191
|
+
const plan = planSessions({ risk, cfg, kind });
|
|
165
192
|
const head = { engine: plan.mode, depth: plan.depth, risk, file: diff.file };
|
|
166
193
|
if (plan.refused) return { status: 'refused', reason: plan.refused, approved: false, ...head, sessions: [] };
|
|
167
194
|
if (diff.diffText.trim().length === 0) return { status: 'no_change', approved: false, ...head, sessions: [] };
|
|
@@ -231,17 +258,30 @@ async function runOne(spec, text, packet, { deps, workDir, min, smallDiff = fals
|
|
|
231
258
|
writeFileSync(promptPath, text, { mode: 0o600 });
|
|
232
259
|
/** @type {Record<string, any> | null} */
|
|
233
260
|
let res = null;
|
|
261
|
+
let attempts = 0;
|
|
234
262
|
try {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
263
|
+
// a `timeout` is spawned once more on the same packet and level (B30); a spawn that throws
|
|
264
|
+
// (bad config, refused argv) is an `exit` failure, never approval
|
|
265
|
+
({ res, attempts } = await spawnWithTimeoutRetry(
|
|
266
|
+
deps.spawn,
|
|
267
|
+
{
|
|
268
|
+
level: spec.level,
|
|
269
|
+
role: spec.role,
|
|
270
|
+
promptPath,
|
|
271
|
+
schema: FINDING_SCHEMA,
|
|
272
|
+
rowExtra: { lens: spec.lens, context_mode: packet.contextMode, ctx_tokens_in: packet.tokensIn },
|
|
273
|
+
...(spec.cfg ? { cfg: spec.cfg } : {}),
|
|
274
|
+
},
|
|
275
|
+
(row) => note(deps, row),
|
|
276
|
+
(n) => {
|
|
277
|
+
attempts = n;
|
|
278
|
+
},
|
|
279
|
+
));
|
|
243
280
|
} catch {
|
|
244
|
-
|
|
281
|
+
// defensive only: the helper never throws. If it ever did, it is still an `exit` failure,
|
|
282
|
+
// never approval, with the attempts it had reported so far (at least 1).
|
|
283
|
+
res = null;
|
|
284
|
+
attempts = Math.max(attempts, 1);
|
|
245
285
|
} finally {
|
|
246
286
|
rmSync(promptPath, { force: true });
|
|
247
287
|
}
|
|
@@ -251,6 +291,7 @@ async function runOne(spec, text, packet, { deps, workDir, min, smallDiff = fals
|
|
|
251
291
|
provider: res?.provider ?? null,
|
|
252
292
|
model: res?.model ?? null,
|
|
253
293
|
fallback_step: res?.fallback_step ?? 0,
|
|
294
|
+
attempts,
|
|
254
295
|
tokens_in: res?.usage?.tokens_in ?? null,
|
|
255
296
|
tokens_out: res?.usage?.tokens_out ?? null,
|
|
256
297
|
};
|