bareloop 0.2.0 → 0.4.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/src/interpret.js CHANGED
@@ -12,25 +12,164 @@
12
12
 
13
13
  import { createRequire } from 'node:module';
14
14
  import { writeFileSync, readFileSync } from 'node:fs';
15
+ import { createHash } from 'node:crypto';
15
16
  import { join, resolve, sep } from 'node:path';
17
+ import { homedir } from 'node:os';
16
18
  import { Gate, redact } from 'bareguard';
17
19
  import { LiteCtx, compress } from 'litectx';
18
20
  import { validateConfig, diffPaths, globToPrefix, SECRET_PATTERNS } from './validate.js';
19
21
  import { ralph } from './ralph.js';
22
+ import { createRoot } from './root.js';
20
23
 
21
- /** @typedef {Error & {category?: string}} CategorizedError the failure map's carrier: ralph relays by `category` */
24
+ /** @typedef {Error & {category?: string, lib?: string}} CategorizedError the failure map's carrier: ralph relays by `category`, and by `lib` when the thrower knows which library owns the failure (the ledger prefers it over prose) */
22
25
 
23
26
  // consecutive close reds that count as a stall; one revision per run
24
27
  export const STALL_REDS = 2;
25
28
 
26
29
  const require = createRequire(import.meta.url);
27
30
  const { Loop, wireGate, HaltError } = require('bare-agent');
31
+ const { createShellTools } = require('bare-agent/tools');
28
32
 
29
- import { stripFences } from './text.js';
33
+ import { extractArtifact, priceOf } from './text.js';
30
34
 
31
35
  /** @typedef {{body?: string|null, text?: string|null}} RecallHit litectx recall hit — body present only with `{body: true}` */
32
36
 
33
37
  const PERSONA = 'You are a senior engineer. Reply with ONLY the complete contents of the requested JavaScript file — no markdown fences, no commentary. ESM.';
38
+ // The tool persona states the LOOP CONTRACT (F16): the worker is one attempt inside
39
+ // `while close-red and under-cap`, not a one-shot. Without knowing that, a model does
40
+ // the rational one-shot thing — read everything, be certain, then act — and the real
41
+ // run spent its ENTIRE budget on 12 rounds of reading without one write, never once
42
+ // reaching the close. Every round re-pays for every earlier tool result, so reads
43
+ // compound: that run's context grew 2k → 121k tokens and its last round cost $0.25.
44
+ // Telling the worker it will be re-run with the close's verdict makes an early,
45
+ // cheap, wrong attempt the rational move — which is exactly what the loop wants.
46
+ const PERSONA_TOOLS = 'You are a senior engineer working in a repository through file tools. '
47
+ + 'ALWAYS use absolute paths — relative paths resolve against the process, not the repository, and will be denied. '
48
+ + 'You are ONE attempt inside an automated loop: when you finish, a test suite runs and, if it still fails, you are called again with its output. '
49
+ + 'So do not try to be certain before acting. Read only what you need to form your best hypothesis, make the change with the write tool, and stop. '
50
+ + 'A wrong cheap attempt is corrected by the next round; exhaustive reading is not — every file you read is re-sent on every later round and the run has a hard budget it can exhaust before you ever write. '
51
+ + 'Make the required changes with the write tool, then reply with a short summary of what you changed. Never put file contents in your reply.';
52
+
53
+ // ---- tool mode (2b): the spec-side grant menu mapped to the underlying tools ----
54
+ // read/grep/write are bare-agent's shell tools; recall/get are litectx's retrieval
55
+ // verbs (F19), composed from the SAME LiteCtx the memory hooks already use.
56
+ const TOOL_BY_VERB = Object.freeze({ read: 'shell_read', grep: 'shell_grep', write: 'shell_write', edit: 'shell_edit', recall: 'ctx_recall', get: 'ctx_get' });
57
+ const CTX_TOOLS = Object.freeze(['ctx_recall', 'ctx_get']);
58
+
59
+ /**
60
+ * The retrieval pair (F19). `shell_read` cannot seek — it starts at byte zero — so a
61
+ * pointer at a symbol was inert and the worker paged whole files to reach one function.
62
+ * `ctx_recall` hands back a POINTER (no body: a search index returns pointers, and bodies
63
+ * on every hit rebuild the bloat — measured, a 5-hit recall dumps ~15k tokens unbidden);
64
+ * `ctx_get` trades that pointer for ONE chunk. A chunk starts at its doc-comment, so the
65
+ * body arrives WITH the docstring that says what it was supposed to do.
66
+ * @param {any} lc the run's LiteCtx (rooted at workdir)
67
+ * @param {string} workdir
68
+ * @param {(type: string, data: object) => void} emit the spine — a retrieval verb whose
69
+ * RESULT is invisible cannot be judged: a `ctx_get` that silently reds (stale pointer,
70
+ * bad range) looks exactly like one that worked, and the worker's fallback to a
71
+ * whole-file read looks like a free choice instead of a forced one.
72
+ */
73
+ function createCtxTools(lc, workdir, emit) {
74
+ return [
75
+ {
76
+ name: 'ctx_recall',
77
+ description: 'Search the repository index for a symbol or phrase. Returns POINTERS (path, symbol, line range) — not code. '
78
+ + 'Pass a pointer to ctx_get to read that one function. Search finds what you can NAME: it will not find a bug from a failing '
79
+ + "test's output (the symptom and the cause live in different files) — read the failing test first, then recall the function it calls.",
80
+ parameters: {
81
+ type: 'object',
82
+ properties: {
83
+ query: { type: 'string', description: 'Symbol name or phrase, e.g. "keywords" or "stopword filter".' },
84
+ n: { type: 'integer', description: 'Max pointers to return (default 5, max 20).' },
85
+ },
86
+ required: ['query'],
87
+ },
88
+ execute: async (/** @type {{query: string, n?: number}} */ { query, n }) => {
89
+ const hits = await lc.recall(String(query), { kind: 'code', n: Math.min(Math.max(Number(n) || 5, 1), 20) });
90
+ const out = hits.length
91
+ ? hits.map((/** @type {any} */ h) => (h.chunk
92
+ ? `${h.path}\t${h.chunk.symbol ?? '(anonymous)'}\t${h.chunk.nodeType}\tlines ${h.chunk.startLine}-${h.chunk.endLine}`
93
+ : `${h.path}\t(whole file — no chunk)`)).join('\n')
94
+ : 'no hits';
95
+ emit('ctx-tool', { tool: 'ctx_recall', query: String(query), hits: hits.length, paths: hits.map((/** @type {any} */ h) => h.path), bytes: Buffer.byteLength(out) });
96
+ return out;
97
+ },
98
+ },
99
+ {
100
+ name: 'ctx_get',
101
+ description: 'Read ONE function by the line range ctx_recall gave you — code plus its doc-comment, without the rest of the file. '
102
+ + 'The line range is a HANDLE you copy from ctx_recall, never one you compute: a range that is not a chunk boundary is refused, '
103
+ + 'and a file edited since it was indexed is refused (re-run ctx_recall for a fresh pointer).',
104
+ parameters: {
105
+ type: 'object',
106
+ properties: {
107
+ path: { type: 'string', description: 'Repository-relative path, exactly as ctx_recall printed it.' },
108
+ startLine: { type: 'integer', description: 'From ctx_recall.' },
109
+ endLine: { type: 'integer', description: 'From ctx_recall.' },
110
+ },
111
+ required: ['path', 'startLine', 'endLine'],
112
+ },
113
+ execute: async (/** @type {{path: string, startLine: number, endLine: number}} */ { path: p, startLine, endLine }) => {
114
+ // The path is spelled as recall printed it (repo-relative); the gate judges the
115
+ // resolved absolute path, exactly as toolAction resolves it. Boundary-aware
116
+ // prefix (workdir + sep, the line-247 pattern): bare startsWith would garble a
117
+ // sibling like `${workdir}-backup/x` into `ackup/x` — the gate independently
118
+ // denies such a path before execute() runs, so this is defense in depth only.
119
+ const rel = String(p).startsWith(workdir + sep) ? String(p).slice(workdir.length + 1) : String(p);
120
+ try {
121
+ const item = lc.get(rel, { startLine: Number(startLine), endLine: Number(endLine) });
122
+ if (!item?.text) {
123
+ emit('ctx-tool', { tool: 'ctx_get', path: rel, startLine, endLine, outcome: 'no-chunk', bytes: 0 });
124
+ return `no chunk at ${startLine}-${endLine} in "${rel}" — copy a line range from ctx_recall, do not compute one`;
125
+ }
126
+ emit('ctx-tool', { tool: 'ctx_get', path: rel, startLine, endLine, outcome: 'ok', bytes: Buffer.byteLength(item.text) });
127
+ return item.text;
128
+ } catch (e) {
129
+ // StalePointerError: the file changed after indexing, so these lines now describe
130
+ // DIFFERENT code. Its message IS the recovery instruction — hand it to the worker.
131
+ const detail = String(/** @type {Error} */ (e)?.message || e);
132
+ emit('ctx-tool', { tool: 'ctx_get', path: rel, startLine, endLine, outcome: 'stale', bytes: 0, detail });
133
+ return `stale pointer: ${detail}`;
134
+ }
135
+ },
136
+ },
137
+ ];
138
+ }
139
+
140
+ // The load-bearing containment line (2b POC, scenario B): bare-agent's built-in
141
+ // tools are deliberately ungated and their action type is their own NAME, which
142
+ // never trips bareguard's fs primitives — this translator maps them onto
143
+ // write/read actions so the SAME fence text mode enforces manually governs
144
+ // every tool call. Paths resolve exactly as the tools resolve them
145
+ // (path.resolve(expandHome(p)), POC scenario F) so the gate judges the same
146
+ // file the tool would touch; a relative spelling resolves against the process
147
+ // cwd and reds at the fence, and the deny reason teaches the retry.
148
+ /** @param {string} p */
149
+ const expandHome = (p) => (p === '~' || p.startsWith('~/')) ? join(homedir(), p.slice(1)) : p;
150
+ /** @param {string} name @param {any} args @param {string} [workdir] */
151
+ const toolAction = (name, args, workdir) => {
152
+ if (name === 'shell_write') return { type: 'write', path: resolve(expandHome(String(args?.path ?? ''))), args: { bytes: String(args?.content ?? '').length } };
153
+ // shell_edit (BA-13) is judged as bareguard's own 'edit' action type — the SAME
154
+ // fs.writeScope fence as write, decided upstream (bareguard FS_TYPES). `bytes` is
155
+ // the replacement's size so the audit can read the edit economy (a 40-byte splice
156
+ // vs an 8KB rewrite is the variable BA-13 exists to move — F18 blindness rule).
157
+ if (name === 'shell_edit') return { type: 'edit', path: resolve(expandHome(String(args?.path ?? ''))), args: { bytes: String(args?.newText ?? '').length } };
158
+ // `tool` rides the action so the AUDIT can tell the read tools apart. Without it every
159
+ // read tool collapses to {type:'read', path} and a whole-file shell_read is
160
+ // indistinguishable from a bounded ctx_get chunk — the ledger cannot see the one
161
+ // variable the retrieval arm exists to test (the F18 blindness, repeated).
162
+ if (name === 'shell_read' || name === 'shell_grep') return { type: 'read', path: resolve(expandHome(String(args?.path ?? ''))), args: { tool: name } };
163
+ // The retrieval verbs are READS and are judged as reads by the SAME fence (F14/F19):
164
+ // ctx_get names a file, so the gate judges that file — the deny list (gate audit,
165
+ // primitive smoke, the litectx store itself) applies to a chunk read exactly as it
166
+ // applies to shell_read, or the worker would read the arbiter's books through the
167
+ // other door. ctx_recall names no file: it is a read OF THE INDEX, judged against the
168
+ // run directory, so it is contained by readScope and cannot reach outside the repo.
169
+ if (name === 'ctx_get') return { type: 'read', path: resolve(String(workdir ?? ''), expandHome(String(args?.path ?? ''))), args: { tool: name } };
170
+ if (name === 'ctx_recall') return { type: 'read', path: resolve(String(workdir ?? '')), args: { tool: name } };
171
+ return { type: name, args };
172
+ };
34
173
 
35
174
  /**
36
175
  * Execute a workflow config against one task under the dumb shell.
@@ -38,13 +177,26 @@ const PERSONA = 'You are a senior engineer. Reply with ONLY the complete content
38
177
  * @param {object|string} configRaw schema v1 config (object or raw JSON text)
39
178
  * @param {object} opts
40
179
  * @param {string} opts.task implement instruction shown to the worker
41
- * @param {string} opts.target absolute path the artifact is written to
180
+ * @param {string} [opts.target] absolute path the artifact is written to —
181
+ * required in text mode; unused in tool mode (the worker writes through
182
+ * the gated tools, wherever the fence allows)
42
183
  * @param {string[]} opts.close argv whose exit code is truth (shell-owned)
184
+ * @param {number} [opts.closeExpect] the exit code the SIGNED spec calls success
185
+ * @param {{pattern: string, min: number}} [opts.closeJudged] the signed spec's
186
+ * judgment-rendered signal — proof the close actually judged something before
187
+ * its exit code is believed in EITHER direction (PRD v1.11; the drafted config
188
+ * cannot express it, and must not: it is the arbiter's own honesty check)
189
+ * @param {string} [opts.closeGapKeep] the signed spec's kept-failures pattern (F28):
190
+ * close-output lines matching it survive the gap bound, so a large suite's
191
+ * `not ok` names reach the worker instead of being elided into "N fail" — also
192
+ * arbiter territory, inexpressible in the drafted config
43
193
  * @param {string} opts.workdir run directory (litectx root, gate audit, scope base)
44
194
  * @param {number} opts.capRuns shell iteration budget; the config may tighten via loop.maxIterations, never exceed
45
195
  * @param {(type: string, data?: object) => object} opts.emit spine emitter
46
196
  * @param {object} opts.provider a bareagent provider — SHELL-owned binding (adaptlearn F8: an unsealed binding is a gate bypass)
47
- * @param {number} [opts.shellCapUsd=2] the shell's USD cap; config budgetUsd is clamped by validation
197
+ * @param {number} [opts.shellCapUsd=2] the shell's USD cap; a config budgetUsd above it REDS at validation (bounds — no silent clamping)
198
+ * @param {number} [opts.closeTimeoutMs] close wall-clock cap, threaded to the shell
199
+ * (shell/operator territory — the workflow config cannot express it)
48
200
  * @param {string[]} [opts.jobWriteScope] the job spec's outer write fence (operator law,
49
201
  * job-v1) — enforced HERE, the one choke point where a config becomes a Gate:
50
202
  * every workflow scope must fit inside it (scope-escape config-red otherwise),
@@ -56,9 +208,32 @@ const PERSONA = 'You are a senior engineer. Reply with ONLY the complete content
56
208
  * (arbiter-touch / cap-touch revision-reds otherwise; the run continues on the old
57
209
  * config). Revisor spend rides the run's own gate handlers — same budget axis as
58
210
  * the worker.
211
+ * @param {string} [opts.closeState] the close's CURRENT output on the tree as it stands
212
+ * (the shell's pre-token close check, F13) — shown to the first attempt only, and
213
+ * never framed as an attempt: the worker cannot run the close itself (`run` is a
214
+ * locked verb), so without this it is asked to fix a failure it cannot see
215
+ * @param {'text'|'tools'} [opts.mode] middle mode (2b): 'text' (default) writes the ONE
216
+ * target from the response artifact; 'tools' gives the worker Gate-governed file
217
+ * tools — SPEC-side territory (the step declares it; the config cannot express it)
218
+ * @param {boolean} [opts.layerRoot=false] Layer R (the within-run ratchet, src/root.js):
219
+ * shell-assembled fixation detection over the run's own books, injecting an
220
+ * escalating summary→verbatim note when consecutive attempts rewrite the same
221
+ * files without moving the reds. Inert when the worker is not stuck (RSI §3.3).
222
+ * Defaults OFF (decided 2026-07-21): fixation is extinct on every current job
223
+ * (F41), so ON is unproven — the field read defers to Layer 2. `true` is the
224
+ * ON/experimental arm; never a worker-visible knob
225
+ * @param {string[]} [opts.tools] the spec's tool grant (subset of read|grep|write,
226
+ * job-v1 validated); defaults to the full menu in tool mode
59
227
  * @returns {Promise<'green'|'escalated'|'config-red'>}
60
228
  */
61
- export async function interpret(configRaw, { task, target, close, workdir, capRuns, emit, provider, shellCapUsd = 2, jobWriteScope, revisor }) {
229
+ export async function interpret(configRaw, { task, target, close, workdir, capRuns, emit, provider, shellCapUsd = 2, jobWriteScope, revisor, closeTimeoutMs, mode = 'text', tools, closeState, closeExpect, closeJudged, closeGapKeep, layerRoot = false }) {
230
+ // Reds-before-tokens: text mode writes ONE artifact — a missing target is a
231
+ // caller bug that must be loud NOW, not a TypeError after a paid worker call
232
+ // that ralph would misfile as interpreter-red (the gate skips an absent path,
233
+ // so nothing downstream catches it before writeFileSync(undefined)).
234
+ if (mode === 'text' && (typeof target !== 'string' || !target)) {
235
+ throw new TypeError('interpret: text mode requires target (the absolute artifact path)');
236
+ }
62
237
  // Normalize ONCE: a trailing slash or a relative spelling must mean the same
63
238
  // directory everywhere below — the enforcement belt compares string prefixes,
64
239
  // and "/run/" vs "/run" would false-red every legal scope (release review).
@@ -87,41 +262,397 @@ export async function interpret(configRaw, { task, target, close, workdir, capRu
87
262
  emit('run-end', { outcome: 'config-red', iterations: 0 });
88
263
  return 'config-red';
89
264
  }
265
+ // The attempt's round bound — ONE number for the Gate's run-wide maxTurns and the
266
+ // per-attempt cutoff below (two literals here once drifted apart in kind: the
267
+ // advertised/enforced class). 24→40 on hamr's word 2026-07-16 (TESTGEN amendment
268
+ // 2026-07-16e): the curve measured that prompting cannot buy readability at 24 —
269
+ // the read-first prelude eats the window before the first write.
270
+ const TURNS_PER_ATTEMPT = mode === 'tools' ? 40 : 8;
90
271
  const gate = new Gate({
91
272
  // bareguard fs.writeScope is prefix-containment, not glob (adaptlearn F4); globToPrefix
92
273
  // is the ONE transform shared with the validator's legality rule — mid-path wildcards
93
274
  // and workdir-escaping scopes were already rejected up front (adaptlearn F9, law #1).
94
- fs: { writeScope: resolvedScopes },
275
+ // Tool mode adds readScope: the worker's reads stay inside the run directory —
276
+ // the stray-read secrets channel (~/.ssh, /etc) closes with one field (2b POC D).
277
+ // Tool mode adds readScope (the stray-read secrets channel, 2b POC D) AND
278
+ // deny (F14): readScope is the whole workdir, which CONTAINS the run's own
279
+ // machinery — the gate's audit ledger, the primitive-smoke store, the litectx
280
+ // memory store. The real run's worker read its own gate audit and spine. The
281
+ // emergent middle does not author the arbiter, and it does not get to read the
282
+ // arbiter's books either: that is an invitation to fit-to-pass and it fills the
283
+ // context with the run's own bookkeeping instead of the repository's code.
284
+ fs: {
285
+ writeScope: resolvedScopes,
286
+ ...(mode === 'tools'
287
+ ? { readScope: [workdir], deny: [join(workdir, 'gate-audit.jsonl'), join(workdir, '.smoke'), join(workdir, '.litectx')] }
288
+ : {}),
289
+ },
95
290
  budget: { maxCostUsd: config.gate.budgetUsd },
96
- limits: { maxTurns: 8 * (capRuns + 1) },
291
+ // text mode is ~1-2 rounds per attempt; tool mode is N rounds (read→write→…).
292
+ // maxTurns ticks on every gate.RECORD (bareguard limits.js:88), and this
293
+ // number means "LLM rounds" ONLY because the sole record path is the LLM one
294
+ // (onLlmResult → gate.record{type:'llm'}); tool calls take gate.CHECK, which
295
+ // does not tick. That invariant is load-bearing: wiring wireGate's own
296
+ // onToolResult into gate.record would silently start ticking maxTurns on
297
+ // tools and halve the LLM budget (the F37 lower-silent-ceiling class). It is
298
+ // pinned by the "every record is type:llm" guard test — do not wire tool
299
+ // records without switching this bound (bareguard offers no llm-only counter;
300
+ // it has maxTurns=all and maxToolRounds=tools-only, neither of which is
301
+ // llm-only once tools also record). Cap semantics are arbiter territory.
302
+ limits: { maxTurns: TURNS_PER_ATTEMPT * (capRuns + 1) },
97
303
  audit: { path: join(workdir, 'gate-audit.jsonl') },
98
304
  humanChannel: async () => ({ decision: 'terminate' }), // no human mid-run: a tripped cap terminates → decision-ready escalation
99
305
  });
100
306
  await gate.init();
101
- const { policy, onLlmResult } = wireGate(gate);
102
- const loop = new Loop({ provider, system: PERSONA, policy, onLlmResult });
307
+ // The crash-attribution instrument (F32): which files has the worker written this
308
+ // run? Answered from the arbiter's OWN books — the gate audit's allow-decision
309
+ // write lines, run_id-scoped because the audit file appends across steps and runs.
310
+ // Both middles land here: tool-mode writes are policy-checked, text-mode writes go
311
+ // through gate.check. Read on demand (only ever at a crashed verdict) and
312
+ // fail-closed: an unreadable audit attributes nothing, so the crash escalates
313
+ // exactly as it did before F32 — attribution can only ADD a recovery path, never
314
+ // swallow an instrument stop.
315
+ const auditPath = join(workdir, 'gate-audit.jsonl');
316
+ // NOTE (Finding 4, deferred): this re-reads and re-parses the whole audit each
317
+ // call. That is O(1) reads when Layer R is OFF (called only at the rare crashed
318
+ // verdict), but O(attempts) when Layer R is ON, and the audit can accumulate
319
+ // across steps/runs. An incremental byte-cursor reader would make it O(total)
320
+ // — deferred as cleanup-tier (only bites the OFF-by-default arm) until it can
321
+ // carry its own multi-path-accumulation tests; the simple full-parse is kept
322
+ // because it is obviously correct.
323
+ const workerWrites = () => {
324
+ try {
325
+ const paths = new Set();
326
+ for (const line of readFileSync(auditPath, 'utf8').split('\n')) {
327
+ if (!line) continue;
328
+ let rec;
329
+ try { rec = JSON.parse(line); } catch { continue; }
330
+ if (rec.run_id === gate.runId && rec.phase === 'gate' && rec.decision === 'allow'
331
+ && (rec.action?.type === 'write' || rec.action?.type === 'edit')
332
+ && typeof rec.action.path === 'string') paths.add(rec.action.path);
333
+ }
334
+ return [...paths];
335
+ } catch { return []; }
336
+ };
337
+ // Layer R — the root (within-run ratchet, design record 2026-07-19). Shell-
338
+ // assembled from the arbiter's own books: per-attempt write-sets come from
339
+ // the SAME workerWrites audit read (allow-decision only, so a denied write is
340
+ // never counted), red-sets from the close's kept-failure lines. The verbatim
341
+ // stage needs the content the audit deliberately does not keep (bytes-only),
342
+ // so it is teed below at the one seam that sees it — memory-only, scrubbed,
343
+ // never the spine.
344
+ const root = layerRoot ? createRoot({ gapKeep: closeGapKeep, redact: (/** @type {string} */ s) => redact(s, { patterns: SECRET_PATTERNS }), writesInformative: mode === 'tools' }) : null;
345
+ // The tee rides the translator and resolves paths exactly as toolAction does,
346
+ // so tee keys match audit paths byte-for-byte. The translator fires BEFORE the
347
+ // gate decides (wireGate calls translate() inside its policy, then awaits
348
+ // gate.check), so it can only STAGE: the audit-path filter downstream is not
349
+ // enough on its own, because the tee is last-write-wins PER PATH and a path the
350
+ // audit legitimately lists as allowed can end the attempt holding the content
351
+ // of a LATER write the gate rejected — the verbatim note would then swear that
352
+ // code landed when it is not in the file (Finding 6, 2026-07-20). Settlement
353
+ // is split across the two seams that actually know (Finding 7): the gate
354
+ // verdict can only DISCARD (a deny means the tool never runs), and the
355
+ // post-execution seam supplies the outcome an allow cannot — see `policy` and
356
+ // `onToolOutcome` below.
357
+ /** @param {string} n @param {any} a */
358
+ const teeingTranslator = (n, a) => {
359
+ // Classify ONCE and decide by the action TYPE, not a hardcoded name list — a
360
+ // third enumeration of the write verbs (workerWrites already filters on
361
+ // action.type write|edit) would let a future granted write-class verb enter
362
+ // the gate audit but silently bypass the tee (the recurring blind-instrument
363
+ // class). Reuse act.path so the tee key matches the audit path byte-for-byte
364
+ // (both from this same resolution). Content is read off the RAW args and never
365
+ // put on the action object — the gate audit stays bytes-only (the secrets law:
366
+ // what an append-only log captures, it captures forever).
367
+ const act = toolAction(n, a, workdir);
368
+ if (root && (act.type === 'write' || act.type === 'edit')) {
369
+ // act.path is always present on the write/edit branch (cast: toolAction's
370
+ // object literals widen `type` to string, so tsc cannot narrow the union)
371
+ const path = /** @type {string} */ (act.path);
372
+ const content = String((act.type === 'edit' ? a?.newText : a?.content) ?? '');
373
+ root.stageWrite(path, content);
374
+ // Finding 7 — the OUTCOME probe. The gate's allow is an INTENT record
375
+ // written before the tool runs; shell_edit returns an anchor miss as a
376
+ // refusal RESULT (bare-agent tools/shell.js:190) and a byte-cap overflow
377
+ // as a throw (:204), both leaving the file untouched with the allow
378
+ // already on the audit. Snapshot the file hash HERE (pre-execution) so the
379
+ // post-execution seam can settle the note against ground truth. Carry the
380
+ // action TYPE: for the file-unchanged case, a write and an edit answer
381
+ // "did it land?" differently (Finding 2). Raw content, never the scrubbed
382
+ // copy — the comparison is against real bytes; transient, never on a
383
+ // prompt, the spine, or the audit.
384
+ pendingProbe = { path, content, type: act.type, before: fileHash(path) };
385
+ }
386
+ return act;
387
+ };
388
+ /**
389
+ * Content hash of a file, or null when it does not exist / cannot be read —
390
+ * null is the honest unknown here, and a missing file BEFORE a write is the
391
+ * normal new-file case (it compares unequal to the hash after, which is
392
+ * exactly right: the write landed).
393
+ * @param {string} p
394
+ */
395
+ const fileHash = (p) => {
396
+ try { return createHash('sha256').update(readFileSync(p)).digest('hex'); } catch { return null; }
397
+ };
398
+ /** @type {{path: string, content: string, type: string, before: string|null}|null} the write-class action awaiting its OUTCOME */
399
+ let pendingProbe = null;
400
+ const { policy: gatePolicy, onLlmResult } = wireGate(gate, mode === 'tools' ? { actionTranslator: teeingTranslator } : {});
401
+ // Settle the stage on the verdict. wireGate's policy returns true on allow,
402
+ // a string on deny, and THROWS HaltError on halt — and bare-agent processes
403
+ // tool calls strictly sequentially (`for (const tc of result.toolCalls) { …
404
+ // await this.policy(…) }`, loop.js), so at most one stage is ever in flight.
405
+ // The catch discards and RE-THROWS: cap-halt routing reads that throw, and a
406
+ // swallowed halt would launder a tripped budget into a plain deny. A non-write
407
+ // action stages nothing, so both settlements are no-ops for it.
408
+ const policy = root && mode === 'tools'
409
+ ? async (/** @type {string} */ n, /** @type {any} */ a, /** @type {any} */ c) => {
410
+ try {
411
+ const verdict = await gatePolicy(n, a, c);
412
+ // A DENY is settled here and only here: the tool never executes, so the
413
+ // outcome seam below never fires for it. An ALLOW is deliberately NOT
414
+ // settled — the bytes have not been written yet, and settling on the
415
+ // verdict is precisely the Finding 7 defect (validated 2026-07-20: three
416
+ // allowed edits, zero bytes changed, and the note swore they landed).
417
+ if (verdict !== true) { root.discardWrite(); pendingProbe = null; }
418
+ return verdict;
419
+ } catch (e) {
420
+ root.discardWrite();
421
+ pendingProbe = null;
422
+ throw e;
423
+ }
424
+ }
425
+ : gatePolicy;
426
+ /**
427
+ * Finding 7 — settle the staged write on what the tool ACTUALLY did. Fires
428
+ * after every `tool.execute` (bare-agent loop.js:986), success or error, and
429
+ * bare-agent processes tool calls strictly sequentially, so exactly one probe
430
+ * is ever in flight. A probe that never reaches here (the BA-12 spin-guard
431
+ * early return, or a HaltError out of a tool body) leaves the stage pending
432
+ * and unsettled — correct, because in both cases nothing landed, and the
433
+ * attempt boundary drops it.
434
+ *
435
+ * NOT wired: `wireGate`'s own `onToolResult`, which would start feeding
436
+ * `gate.record` per-tool records. That changes what `limits.maxTurns` /
437
+ * `maxToolRounds` count and therefore when a run halts (the guarded
438
+ * llm-only-turns invariant at the Gate config above) — cap semantics are
439
+ * arbiter territory, so it is deliberately not wired here.
440
+ * @param {{result?: any}} [info] the tool's return value (bare-agent loop.js:988)
441
+ */
442
+ const onToolOutcome = async (info) => {
443
+ if (!root || !pendingProbe) return;
444
+ const { path: p, content, type, before } = pendingProbe;
445
+ pendingProbe = null;
446
+ // ONE read serves both the after-hash and the content check (Finding 5: the
447
+ // old path read the file twice). null = unreadable/absent.
448
+ let after = null;
449
+ try { after = readFileSync(p, 'utf8'); } catch { /* absent → after stays null */ }
450
+ let landed = after !== null && createHash('sha256').update(after, 'utf8').digest('hex') !== before;
451
+ if (!landed && after !== null) {
452
+ // The bytes did not change — but the action may still have left `content`
453
+ // in the file, and write vs edit answer that differently (Finding 2):
454
+ // - write: a whole-file write always writes; identical bytes means the
455
+ // content genuinely IS the file (exact equality, never a substring —
456
+ // substring false-positived when newText appeared elsewhere).
457
+ // - edit: a no-byte-change edit is EITHER an idempotent apply (newText===
458
+ // oldText, "edited …: 1 replacement") OR a missed anchor ("shell_edit:
459
+ // … no change made") — indistinguishable on disk, so the tool RESULT is
460
+ // the only honest signal. An anchor miss is NOT landed.
461
+ if (type === 'write') landed = after === content;
462
+ else landed = typeof info?.result === 'string' && info.result.startsWith('edited ');
463
+ }
464
+ root.settleWrite(landed);
465
+ };
466
+ // Money is metered as it is SPENT — per ROUND, not per attempt (F12). A
467
+ // multi-round attempt that halts (or throws) never returns, so its rounds
468
+ // never reach `worker-result`: the real run bought $1.4375 of tokens inside a
469
+ // halted attempt and the job ledger reported $0.0048. `onLlmResult` is the one
470
+ // seam that sees every round, and it fires BEFORE the gate records the round —
471
+ // so even the round that trips the cap lands on the spine. Emitted first, then
472
+ // forwarded verbatim: the gate's own accounting is never altered. costUsd/
473
+ // pricing ride AS-IS (a null is the honest unknown, never $0 — F6).
474
+ /** @type {number|undefined} the attempt a round belongs to (display only) */
475
+ let roundIteration;
476
+ // F20 — THE ATTEMPT HAD NO BOUND, so ralph never ralphed. `limits.maxTurns` is bareguard's,
477
+ // and the Gate is constructed ONCE for the whole RUN: it is a run-wide HALT, not a
478
+ // per-attempt cap. Nothing else ended an attempt — a tool-mode attempt ran until the model
479
+ // CHOSE to stop calling tools. A worker that has never been told it is wrong does not stop:
480
+ // measured, attempt #1 ran 55 rounds, spent the entire $1.50 budget, and the close NEVER RAN
481
+ // ONCE (zero verdicts, zero gaps, zero writes). The loop's whole premise — a cheap wrong
482
+ // attempt corrected by the next round — was unreachable, and the persona's promise ("a test
483
+ // suite runs and you are called again") was a lie the shell never kept.
484
+ // `loop.stop()` breaks at the round boundary and — since bare-agent 0.27.0 (BA-3/BA-5) —
485
+ // returns the transcript with `error: null` and the produced text preserved, so the attempt
486
+ // ends cleanly, its summary stands, and the close renders the verdict that feeds the next
487
+ // attempt. (Before 0.27.0 stop() fell through to a HARD_ROUND_LIMIT return carrying a bogus
488
+ // error, which bareloop had to un-lie behind a `stoppedByBound` flag — the upgrade removed
489
+ // both the lie and the shim; see ask().) Per-attempt, because each attempt is a FRESH
490
+ // conversation (the context resets), which is what keeps four bounded attempts inside a
491
+ // budget one unbounded attempt exhausts.
492
+ /** rounds spent inside the CURRENT attempt (reset per attempt, unlike the gate's run-wide tick) */
493
+ let roundsThisAttempt = 0;
494
+ /** @type {number|undefined} the attempt that was cut short by the bound (spine + gap note) */
495
+ let attemptBounded;
496
+ /** true while the REVISOR holds the metered handler: its rounds are real money on
497
+ * the run's axis (F12) but they are not the WORKER's rounds, and charging them to
498
+ * the per-attempt bound silently handed the worker 40−R turns while the prompt
499
+ * still advertised 40 (advertised == enforced, PRD; a revisor that burned the whole
500
+ * bound stopped the worker loop before its first tool call) */
501
+ let inRevisor = false;
502
+ // `tokens` alone cannot answer the question the ledger exists to answer.
503
+ // bare-agent's `inputTokens` is the UNCACHED prompt remainder — re-sent context
504
+ // is billed as a cache READ and never appears in it. So a round that re-pays for
505
+ // half the repo and a round that reads it fresh can carry the same `tokens`, at
506
+ // very different cost. Record the four tiers the provider actually prices
507
+ // separately, and record the `kind` (a summarizer fold is an LLM call too, and
508
+ // must never hide inside the worker's own numbers).
509
+ /** @param {{costUsd?: number|null, pricing?: string|null, usage?: any, kind?: string}} arg */
510
+ const meteredOnLlmResult = async (arg) => {
511
+ const u = arg?.usage ?? {};
512
+ emit('worker-round', {
513
+ iteration: roundIteration,
514
+ kind: arg?.kind ?? 'turn',
515
+ costUsd: arg?.costUsd ?? null,
516
+ pricing: arg?.pricing ?? null,
517
+ tokens: (u.inputTokens ?? 0) + (u.outputTokens ?? 0),
518
+ usage: {
519
+ inputTokens: u.inputTokens ?? 0,
520
+ outputTokens: u.outputTokens ?? 0,
521
+ cacheReadTokens: u.cacheReadTokens ?? 0,
522
+ cacheCreationTokens: u.cacheCreationTokens ?? 0,
523
+ },
524
+ });
525
+ // The attempt's own bound (F20). A summarizer fold is an LLM call but not a worker
526
+ // ROUND — counting it would let a fold shorten the attempt that paid for it. The
527
+ // revisor is the same case wearing `kind: 'turn'`: metered above, never charged here.
528
+ if ((arg?.kind ?? 'turn') === 'turn' && mode === 'tools' && !inRevisor) {
529
+ roundsThisAttempt += 1;
530
+ if (roundsThisAttempt >= TURNS_PER_ATTEMPT) {
531
+ // Clean stop at the round boundary: since 0.27.0 (BA-3/BA-5) run() returns the
532
+ // transcript with error=null and the text preserved, so this is an attempt that
533
+ // ENDED, not one that FAILED. The close now runs and its verdict becomes the next
534
+ // attempt's gap — which is the entire point.
535
+ attemptBounded = roundIteration;
536
+ emit('attempt-bounded', { iteration: roundIteration, rounds: roundsThisAttempt, cap: TURNS_PER_ATTEMPT });
537
+ loop.stop();
538
+ }
539
+ }
540
+ return onLlmResult(arg);
541
+ };
542
+ // The offered tools ARE the grant (2b decision #2): an ungranted tool is never
543
+ // in the menu the model sees — a call to it is "unknown tool", not a deny.
544
+ const toolDefs = mode === 'tools'
545
+ ? await (async () => {
546
+ const granted = new Set((tools ?? Object.keys(TOOL_BY_VERB)).map((v) => /** @type {Record<string, string>} */ (TOOL_BY_VERB)[v]));
547
+ const shell = createShellTools().tools.filter((/** @type {{name: string}} */ t) => granted.has(t.name));
548
+ const ctx = CTX_TOOLS.some((t) => granted.has(t))
549
+ ? createCtxTools(lc, workdir, emit).filter((t) => granted.has(t.name))
550
+ : [];
551
+ // A retrieval verb with no index is a tool that always answers "no hits". Index is
552
+ // incremental (unchanged files skipped) and BM25-only: measured on the real repo, the
553
+ // semantic tier costs 26s of embedding and does NOT find a symbol the lexical tier
554
+ // misses (for code it only RE-RANKS a BM25-gated pool — it cannot nominate), and it
555
+ // demoted the exact-symbol hit from rank 1 to rank 4. Lexical is the better instrument here.
556
+ if (ctx.length) await lc.index();
557
+ return [...shell, ...ctx];
558
+ })()
559
+ : [];
560
+
561
+ // The retrieval verbs only pay if the worker reaches for them INSTEAD of paging a file.
562
+ // F18 measured the failure they exist to fix: the worker read one 117 KB file NINE times
563
+ // and dragged 1.37 MB of source through context to reach an 8-line function. The tool
564
+ // descriptions carry the mechanics; the persona has to carry the STRATEGY, or a model
565
+ // with a familiar `read` and an unfamiliar `recall` will simply keep reading.
566
+ const RETRIEVAL_STRATEGY = '\nYou also have a repository index. To read a function, do NOT read its whole file: '
567
+ + 'call ctx_recall with the function name to get a pointer, then ctx_get with that pointer to read that function alone '
568
+ + '(it comes with its doc-comment, which states what the function is SUPPOSED to do — compare it against what the code does). '
569
+ + 'Reserve shell_read for whole files that are genuinely small, and for files you cannot name a symbol in yet. '
570
+ + 'Search only finds what you can NAME: a failing test names the symptom, not the cause, so read the failing test first, '
571
+ + 'see which function it calls, and recall THAT.';
572
+ // BA-13: like retrieval, the edit verb only pays if the worker reaches for it INSTEAD
573
+ // of a whole-file rewrite. F31 measured the default: 4 of 5 big-file whole-writes
574
+ // broke the tree, and every one was a rewrite to change ~one line. The tool
575
+ // description carries the mechanics; the persona carries the strategy.
576
+ const EDIT_STRATEGY = '\nPrefer the edit tool over whole-file writes: quote the EXACT text to change (it must match exactly once) and its replacement. '
577
+ + 'Rewriting a whole file to change one line is how trees get broken and budgets get burned — reserve the write tool for genuinely new files.';
578
+ const usesCtx = toolDefs.some((/** @type {{name: string}} */ t) => CTX_TOOLS.includes(t.name));
579
+ const usesEdit = toolDefs.some((/** @type {{name: string}} */ t) => t.name === 'shell_edit');
580
+ const loop = new Loop({
581
+ provider,
582
+ system: mode === 'tools' ? PERSONA_TOOLS + (usesEdit ? EDIT_STRATEGY : '') + (usesCtx ? RETRIEVAL_STRATEGY : '') : PERSONA,
583
+ policy,
584
+ onLlmResult: meteredOnLlmResult,
585
+ onToolResult: onToolOutcome,
586
+ });
103
587
 
104
588
  /** @param {string} slot */
105
589
  const slotOps = (slot) => config.hooks?.[slot] ?? [];
106
590
  /** @param {{kinds?: string[]}} op */
107
591
  const recallKinds = (op) => op.kinds ?? config.memory.recall?.kinds ?? ['fact'];
108
592
 
109
- /** @param {string} prompt */
110
- async function ask(prompt) {
593
+ /**
594
+ * @param {string} prompt
595
+ * @param {typeof toolDefs} [defs] the tool menu for THIS call — defaults to the
596
+ * step's grant; the plan-only call passes [] (its own prompt says "no code",
597
+ * and the menu is the grant: a tool it isn't offered cannot be called)
598
+ */
599
+ async function ask(prompt, defs = toolDefs) {
111
600
  let r;
112
601
  try {
113
- r = await loop.run([{ role: 'user', content: prompt }]);
602
+ // cacheMessages (BA-1, bare-agent 0.27.0): roll an Anthropic cache breakpoint onto the
603
+ // transcript so a tool loop stops re-buying its whole growing history at full price every
604
+ // round — F18 measured 754k full-price tokens, $1.55, and the job died at the cap without
605
+ // one write. Opt-in and provider-routed (loop.run forwards options to generate): a
606
+ // non-Anthropic binding ignores it, and it is safe because bareloop wires no trim/compaction
607
+ // fold — the one interaction (a fold that rewrites the cached prefix) that would defeat it.
608
+ // maxTokens (F30): the provider's 4096 default cannot hold a whole-file
609
+ // shell_write of a real source file — the API cuts the round, BA-6 surfaces
610
+ // `truncated:max_tokens`, and doctrine reads it provider-red: run over.
611
+ // Battery pass 1 lost 3/3 rows to exactly this. Output budget is shell
612
+ // territory (never the config's); 32k fits any single-file write the
613
+ // writeScope admits, and output tokens are only paid when generated.
614
+ r = await loop.run([{ role: 'user', content: prompt }], defs, { cacheMessages: true, maxTokens: 32000 });
114
615
  } catch (e) {
115
- if (e instanceof HaltError) /** @type {CategorizedError} */ (e).category = 'cap-halt'; // belt: bare-agent's contract could change
116
- throw e;
616
+ const err = /** @type {CategorizedError} */ (e);
617
+ // A throw OUT OF loop.run() is provider/loop territory by definition — the
618
+ // interpreter's own code is not on that stack. The real run died `read
619
+ // ENETUNREACH` mid-call and was filed interpreter-red ("fix the middle"),
620
+ // when the honest decision was "the network failed — retry" (F11). A
621
+ // governance halt keeps its own category; everything else is provider-red,
622
+ // the same class the drafting path already names.
623
+ err.category = e instanceof HaltError ? 'cap-halt' : (err.category ?? 'provider-red');
624
+ throw err;
117
625
  }
118
626
  // bare-agent NEVER throws HaltError out of run() — a governance halt comes
119
627
  // back as an error RETURN ({text: '', error: 'halt:<rule>'}; loop.js: "no
120
628
  // throw even when throwOnError: true"). Read it, or the failure map goes
121
629
  // blind and a cap story masquerades as a worker result (design law #8).
630
+ // A denial streak ('denied:<tool>', BA-11) is a governance deny, not a
631
+ // broken interpreter — gate-red, same category as text mode's fence deny.
632
+ // A TRUNCATED round ('truncated:max_tokens', BA-6, bare-agent 0.27.0) is the
633
+ // API cutting the generation off at the output cap — not a middle fault. It is
634
+ // provider-red (retry), the same class as an ENETUNREACH transport failure
635
+ // (F11): no verdict exists and the failed round's spend is only partly known.
636
+ // Before 0.27.0 this round was laundered into a clean finish with error:null
637
+ // (F25) — every prior sonnet arm ran on that bug; the fix surfaces it as its
638
+ // own error, so the middle can escalate instead of scoring an empty attempt.
639
+ // OUR OWN stop is not a fault (F20), and since 0.27.0 (BA-3/BA-5) `loop.stop()`
640
+ // returns `error: null` with the run's text preserved — so a bounded attempt
641
+ // lands on the clean-return path below with no special-casing (the 0.26.2
642
+ // `stoppedByBound` shim that un-lied the HARD_ROUND_LIMIT return is gone).
122
643
  if (r.error) {
123
644
  const err = /** @type {CategorizedError} */ (new Error(`worker loop: ${r.error}`));
124
- err.category = r.error.startsWith('halt:') ? 'cap-halt' : 'interpreter-red';
645
+ err.category = r.error.startsWith('halt:') ? 'cap-halt'
646
+ : r.error.startsWith('denied:') ? 'gate-red'
647
+ : r.error.startsWith('truncated:') ? 'provider-red'
648
+ : 'interpreter-red';
649
+ // Attribute at the THROW site, where the owner is known, instead of
650
+ // leaving the ledger to sniff the prose downstream: this error came out
651
+ // of bare-agent's loop, whatever words it happens to carry. A litectx
652
+ // hook throw (runOps) reaches the same escalation with no lib field and
653
+ // keeps its verb→lib mapping. Review 2026-07-18: prose-sniffing billed a
654
+ // transport failure to litectx whenever the text said "recall".
655
+ err.lib = 'bare-agent';
125
656
  throw err;
126
657
  }
127
658
  return r;
@@ -141,7 +672,10 @@ export async function interpret(configRaw, { task, target, close, workdir, capRu
141
672
  }
142
673
  context.text = hits.map((h) => h.body ?? h.text ?? '').filter(Boolean).join('\n');
143
674
  context.level = null;
144
- emit('hook-op', { slot, op: 'recall', hits: hits.length, iteration });
675
+ // paths ride on the event: a recall whose CONTENT is invisible blinds every
676
+ // downstream instrument to what the worker was handed (F33 — P5's green looked
677
+ // like an impossible sight-unseen edit until recall_log forensics explained it)
678
+ emit('hook-op', { slot, op: 'recall', hits: hits.length, paths: hits.map((h) => /** @type {any} */ (h).path).filter(Boolean), iteration });
145
679
  } else if (op.op === 'compress') {
146
680
  const level = op.level ?? config.memory.compressLevel ?? 'verbatim';
147
681
  if (context.text) context.text = await compress({ text: context.text, format: 'js' }, { level });
@@ -150,7 +684,11 @@ export async function interpret(configRaw, { task, target, close, workdir, capRu
150
684
  if (gap) lc.stash(`gap-${iteration}`, gap);
151
685
  emit('hook-op', { slot, op: 'stash', iteration });
152
686
  } else if (op.op === 'remember') {
153
- await lc.remember(`green-${iteration ?? 'final'}-${target.split('/').at(-1)}`, readFileSync(target, 'utf8'), { kind: op.kind ?? 'fact' });
687
+ // tool mode has no single target to read back — the green's retained
688
+ // form is the worker's own change summary (its final loop text), which
689
+ // is what a future recall can actually use
690
+ const content = mode === 'tools' ? (lastText ?? '') : readFileSync(/** @type {string} */ (target), 'utf8');
691
+ await lc.remember(`green-${iteration ?? 'final'}-${mode === 'tools' ? 'tools' : /** @type {string} */ (target).split('/').at(-1)}`, content, { kind: op.kind ?? 'fact' });
154
692
  emit('hook-op', { slot, op: 'remember' });
155
693
  }
156
694
  }
@@ -182,11 +720,22 @@ export async function interpret(configRaw, { task, target, close, workdir, capRu
182
720
  /** @type {string[]} */
183
721
  const gaps = [];
184
722
  let revised = false;
723
+ /** @type {string|undefined} set on artifact-red, consumed by the next attempt's prompt */
724
+ let artifactNote;
725
+ /** @type {string|undefined} tool mode: the last attempt's summary text (retention source) */
726
+ let lastText;
185
727
  /**
186
728
  * @param {number} iteration
187
729
  * @param {string} [gap]
188
730
  */
189
731
  const middle = async (iteration, gap) => {
732
+ roundIteration = iteration; // stamps every round of this attempt (F12)
733
+ roundsThisAttempt = 0; // the bound is PER ATTEMPT — a fresh conversation, a fresh budget (F20)
734
+ // Layer R: finalize the previous attempt from the books and ask the ratchet.
735
+ // Inert (null) unless consecutive attempts rewrote the same files without
736
+ // moving the reds; the event carries stage/paths/counts, NEVER content.
737
+ const rootInj = root ? root.observe({ iteration, gap, writes: workerWrites() }) : null;
738
+ if (rootInj) emit('root-injected', rootInj.event);
190
739
  if (gap) gaps.push(gap);
191
740
  if (revisor && !revised && gaps.length >= STALL_REDS) {
192
741
  emit('stall-detected', { iteration, consecutiveReds: gaps.length });
@@ -196,41 +745,108 @@ export async function interpret(configRaw, { task, target, close, workdir, capRu
196
745
  // the run's own gate handlers ride along: revisor spend hits the same
197
746
  // budget axis as the worker; a budget halt mid-revision is a cap story,
198
747
  // not a revision bug
199
- rv = await revisor({ config, gaps: [...gaps], policy, onLlmResult });
748
+ // the METERED handler: revisor rounds are real money on the same axis (F12)
749
+ inRevisor = true;
750
+ rv = await revisor({ config, gaps: [...gaps], policy, onLlmResult: meteredOnLlmResult });
200
751
  } catch (e) {
201
752
  if (e instanceof HaltError) /** @type {CategorizedError} */ (e).category = 'cap-halt';
202
753
  throw e;
754
+ } finally {
755
+ // `finally`, not a trailing assignment: a revisor that throws (cap-halt is
756
+ // the expected one) must not leave the worker's rounds uncounted for the
757
+ // rest of the run — that would turn this fix into an unbounded attempt.
758
+ inRevisor = false;
203
759
  }
204
760
  const rr = acceptRevision(rv.candidate);
205
761
  if (rr.red) {
206
- emit('revision-red', { iteration, ...rr.red, detail: rv.parseError ?? undefined, costUsd: rv.costUsd ?? 0 });
762
+ // `?? null`, never `?? 0`: an unpriced revisor's spend is UNKNOWN, and
763
+ // the spine is the permanent record (F6 — the cap is governed honestly
764
+ // by the metered worker-round path, but $0 on the record is a lie).
765
+ emit('revision-red', { iteration, ...rr.red, detail: rv.parseError ?? undefined, costUsd: rv.costUsd ?? null });
207
766
  } else {
208
- emit('revision-accepted', { iteration, changedPaths: diffPaths(config, rr.config), costUsd: rv.costUsd ?? 0 });
767
+ emit('revision-accepted', { iteration, changedPaths: diffPaths(config, rr.config), costUsd: rv.costUsd ?? null });
209
768
  config = rr.config;
210
769
  }
211
770
  }
212
771
  if (gap) await runOps('after-red', { iteration, gap, context: {} });
213
772
  const context = {};
214
773
  await runOps('before-attempt', { iteration, context });
215
- const parts = [task, context.text && `Possibly relevant notes:\n${context.text}`, gap && `Previous attempt failed the test suite:\n${gap}`];
774
+ // F10 (first real-model run): a tool-mode worker must be TOLD where the
775
+ // repository is. The persona demands absolute paths, but bare-agent's shell
776
+ // tools resolve relative paths against the PROCESS cwd — not the workdir —
777
+ // so a worker with no root is working blind: the real run groped for the repo
778
+ // (/home/hamr, the runner's own directory, then /) and the fence denied every
779
+ // guess until the denial streak stopped it. Containment held; the task was
780
+ // impossible. Text mode is told nothing: it has no tools to point anywhere,
781
+ // and the shell alone chooses its one target.
782
+ // (parts[0] stays the task: the plan shape re-orders around it)
783
+ const parts = [
784
+ task,
785
+ mode === 'tools' && `Repository root (absolute): ${workdir}\nEvery path you pass to a tool MUST be absolute and inside this root — a relative path resolves against a different directory and will be denied by the gate.`,
786
+ // F13: what the close says about the tree RIGHT NOW — the state a human
787
+ // maintainer reads first. Given only until an attempt of our own exists
788
+ // (then `gap` is the truer, attributable evidence). The real run withheld
789
+ // this on the grounds that "no attempt has happened", and the worker — which
790
+ // cannot run the suite itself, the `run` verb being locked — groped through
791
+ // the repository and burned the entire cap without one write. Attributing an
792
+ // attempt and describing the tree are different claims; this is the second.
793
+ !gap && closeState && `The close is currently failing. This is its output on the tree as it stands (not an attempt of yours):\n${closeState}`,
794
+ context.text && `Possibly relevant notes:\n${context.text}`, gap && `Previous attempt failed the test suite:\n${gap}`,
795
+ // Layer R: the ratchet note rides directly after the gap it qualifies —
796
+ // same channel discipline as the gap itself (bounded, trims announced)
797
+ rootInj && rootInj.note,
798
+ artifactNote && `Previous attempt never reached the close:\n${artifactNote}`,
799
+ // F20: a bounded attempt must SAY it was bounded. Otherwise the worker reads its own
800
+ // truncated transcript as a finished one, learns nothing from being cut off, and spends
801
+ // the next attempt investigating exactly as far and stopping exactly as short.
802
+ attemptBounded !== undefined && attemptBounded === iteration - 1
803
+ && `Your previous attempt was CUT OFF after ${TURNS_PER_ATTEMPT} tool rounds without making a change. Reading is bounded; writing is not. `
804
+ + `Form a hypothesis EARLY and make the edit — a wrong cheap edit is corrected by the next round, but an attempt that never writes teaches this loop nothing.`];
216
805
 
217
806
  if (config.loop.shape === 'plan') {
218
807
  // plan-then-execute: one call to decompose, one to implement following the plan
219
- const p = await ask([`Produce a SHORT numbered implementation plan (2-4 steps) for this task. Plan only, no code.`, ...parts.slice(1), parts[0]].filter(Boolean).join('\n\n'));
220
- emit('worker-plan', { iteration, costUsd: p.metrics?.costUsd ?? p.cost ?? null }); // metrics.costUsd carries the honest null when unpriced; cost sums priced rounds only
808
+ // Plan only, no code — so no tools either: the menu IS the grant (2b), and a
809
+ // plan call offered shell_write can mutate the tree before the implement
810
+ // round exists (release review 2026-07-19; unexercised combination until then)
811
+ const p = await ask([`Produce a SHORT numbered implementation plan (2-4 steps) for this task. Plan only, no code.`, ...parts.slice(1), parts[0]].filter(Boolean).join('\n\n'), []);
812
+ emit('worker-plan', { iteration, ...priceOf(p) }); // priceOf: the ONE honest-null cost read (F6)
221
813
  parts.push(`Follow this plan:\n${p.text}`);
222
814
  }
223
815
  const r = await ask(parts.filter(Boolean).join('\n\n'));
224
- emit('worker-result', { iteration, costUsd: r.metrics?.costUsd ?? r.cost ?? null, tokens: r.usage?.outputTokens ?? null });
816
+ emit('worker-result', { iteration, ...priceOf(r), toolCalls: r.metrics?.toolCalls ?? 0, tokens: r.usage?.outputTokens ?? null });
817
+
818
+ // Tool mode: the worker already wrote through the gated tools (every call
819
+ // policy-checked against the fence); its final text is a change summary,
820
+ // not an artifact — there is nothing to extract and artifact-red genuinely
821
+ // does not exist here (2b decision #3). The close judges the tree as-is.
822
+ if (mode === 'tools') {
823
+ lastText = r.text ?? '';
824
+ return;
825
+ }
225
826
 
226
- const decision = await gate.check({ type: 'write', path: target, args: { bytes: r.text.length } });
827
+ // Fence-robust extraction BEFORE the gate: what gets written is the
828
+ // artifact, not the response (F2 #2). A response with no artifact reds on
829
+ // its OWN axis — artifact-red — and writes nothing: the close will red
830
+ // against the stale/missing target, but the spine names the true cause,
831
+ // so the contrast evidence stays clean (F21's instrument caveat).
832
+ const ex = extractArtifact(r.text);
833
+ if (ex.red) {
834
+ emit('artifact-red', { iteration, category: 'artifact-red', reason: ex.red });
835
+ artifactNote = `your response was rejected as a non-artifact (${ex.red}) — emit ONLY the code artifact`;
836
+ return; // retryable: the next attempt carries the note; ralph's cap still governs
837
+ }
838
+ artifactNote = undefined;
839
+ const code = /** @type {string} */ (ex.code); // non-null: ex.red was checked above
840
+ const t = /** @type {string} */ (target); // text mode contract: target is required
841
+ const decision = await gate.check({ type: 'write', path: t, args: { bytes: code.length } });
227
842
  if (decision.outcome !== 'allow') {
228
- const err = /** @type {CategorizedError} */ (new Error(`gate ${decision.outcome} write to ${target} (${decision.rule ?? 'no rule'})`));
843
+ const err = /** @type {CategorizedError} */ (new Error(`gate ${decision.outcome} write to ${t} (${decision.rule ?? 'no rule'})`));
229
844
  err.category = decision.severity === 'halt' ? 'cap-halt' : 'gate-red';
230
845
  throw err;
231
846
  }
232
- writeFileSync(target, stripFences(r.text));
233
- emit('artifact-written', { iteration, path: target });
847
+ writeFileSync(t, code);
848
+ root?.noteWrite(t, code); // Layer R tee, text mode: same seam role as the translator's
849
+ emit('artifact-written', { iteration, path: t });
234
850
  };
235
851
 
236
852
  // the config may tighten the shell's iteration budget, never exceed it (mirrors budgetUsd)
@@ -241,7 +857,14 @@ export async function interpret(configRaw, { task, target, close, workdir, capRu
241
857
  // pass a shape detection reds (a git close echoing a ghp_ token was the gap).
242
858
  // Injected here (the layer that owns bareguard) so ralph stays stdlib-only and
243
859
  // the scrub is a fixed shell primitive, not an emergent component (V4 holds).
244
- const outcome = await ralph({ middle, close, capRuns: effectiveCap, emit, redact: (/** @type {string} */ s) => redact(s, { patterns: SECRET_PATTERNS }) });
860
+ // cwd: workdir — the close judges the tree the work happened in (F8). A close
861
+ // is a repository command (`npm test`); run from anywhere else it silently
862
+ // judges another repo and exit-code-is-truth stops being true.
863
+ // expect/judged/gapKeep are the SIGNED spec's, threaded verbatim — the drafted
864
+ // config cannot express any of them (they are arbiter territory, PRD v1.11; F28
865
+ // for gapKeep, which keeps a large suite's failure NAMES in the gap the worker
866
+ // sees instead of letting the bound elide them into a bare "N fail").
867
+ const outcome = await ralph({ middle, close, capRuns: effectiveCap, emit, closeTimeoutMs, cwd: workdir, expect: closeExpect, judged: closeJudged, gapKeep: closeGapKeep, workerWrites, redact: (/** @type {string} */ s) => redact(s, { patterns: SECRET_PATTERNS }) });
245
868
  if (outcome === 'green') {
246
869
  // The close already passed — a retention hiccup must not un-green a real
247
870
  // green (it would corrupt the learning curve). It degrades loudly: