Skip to content

feat(capture): let a failed session leave a lesson, but only an attested one - #384

Merged
abogartz merged 2 commits into
mainfrom
learn-from-failure-s1
Nov 12, 2026
Merged

abogartz merged 2 commits into
mainfrom
learn-from-failure-s1

Conversation

@abogartz

Copy link
Copy Markdown
Owner

Problem

A session that fails stores nothing about why it failed.

Measured on the bench on 2026-11-10 (benchmark-tests/ocsi/results-learn-t3-diag, plugin f8f42fd v0.22.2): a session on a bug it could not fix ran 574 s and 33,287 tokens, failed two tests, and auto-capture wrote one row:

[preference/docs] "In this repository, src module invariants are authoritative over docs/ARCHITECTURE.md; documentation may contain stale behavior claims."

That is a paraphrase of a sentence in the task brief. Zero lesson-shaped rows out of one. The most expensive knowledge the plugin ever sees — 33k tokens of discovering what does not work — has a half-life of one session.

The cause is not the model declining. src/capture/prompt.ts listed "blockers hit" as one word in a list of seven, and grepping the whole prompt file for fail, dead end, wrong, mistake or ruled out returns nothing. The model was never asked.

This is the mechanism the plugin is actually for. Every result the bench has produced so far needs a human to state the fact in pass 1; the plugin persists what a person said and discovers nothing.

What changes

  1. attestedGateFailures(s, sinceTurn) (src/capture/policy.ts) reads the tool ledger for gate commands that FAILED with no later PASS of the same family. First-failure order, deduped by family, capped at eight. A recovered failure is excluded: FAIL then PASS means the agent fixed it, and "do not do what I just did" is only a lesson while the thing is still broken. captureSignalSince reads the same ledger for the opposite purpose — a recovery is the best reason to capture, an unrecovered failure is the best thing to capture.

  2. The capture prompt gets an ATTESTED FAILURES block and asks for one blocker row per failure worth a lesson, in the form when <condition>, avoid <action>, naming a file or symbol so the rule can later be checked against the code. When nothing failed the prompt says so explicitly rather than going quiet.

  3. A lesson must cite its failure, and the citation is checked against the ledger, not against the model's retelling. A candidate carrying "failure" is dropped before the write gate unless that string appears in the ledger's unrecovered failures. failure is a write-time check, not a stored field, so no row format changes.

  4. gateCmd had no test family. bun test, npm test and pytest registered as no gate at all, so a session could go red-green-red and the ledger never saw it. The matcher is anchored on the runner rather than the word, because the word appears everywhere in a repo that has a test directory — ls src/test and cat test/fixtures/a.json are not test runs.

Why attestation is the load-bearing part

ReMe (Findings ACL 2026) found selective addition beats full addition, because a single failed trajectory rarely carries enough context for a correct failure analysis and produces misguided experiences. So this feature does not fail by missing right lessons — it fails by storing wrong ones. A rule about a failure that never happened rides every later session as an instruction. The attestation requirement is not bureaucracy; it is the difference between a lesson and a rumour.

Containment for this row class is already merged: the write gate, quarantine, provenance stripping (#342), the code-checked contradiction surface, and undo.

No recall loss

The gate is monotone. Only a row carrying failure makes the claim "this is a lesson", so an ordinary uncited blocker note — a type this path has always written — is untouched. Refusing every uncited blocker would delete rows the store has always kept, and a provenance fix does not buy a recall loss.

Cost

No new model calls. The block is a handful of prompt lines bounded at eight failures. captureOnSignal is unaffected in the common case: it returns edited first for any session that wrote a file, so the added test family only changes verdicts for read-only sessions that ran a test and recovered.

Out of scope, deliberately

  • captureSignalSince still requires success. With captureOnSignal on, a session that investigates, hits a dead end and stops is not a signal, so the cost lever suppresses exactly the class this ticket is about. Adding gateUnrecovered is one branch, and it is filed separately.
  • A lesson can still never be graded or pruned. Record what happened after a memory rode: a row-to-outcome edge, so usefulness is measurable #332 is unmerged, so nothing can compute prevented/matched and nothing can retire a rule that stopped helping.
  • The bench cannot yet show a lesson on the existing fixture. The failing session above never ran a gate command at all — makeRepo deletes test/, so there is nothing to gate against. The prompt effect is measured directly against the capture model instead.

Test plan

  • bun run typecheck — clean
  • bun test — 1242 pass / 19 skip / 0 fail
  • bun run lint — unchanged at 542 warnings, 147 infos
  • New test/capture-failure.test.ts, 22 tests: ledger attestation (recovery, per-family recovery, window, cap, non-gate commands), gateCmd test families and the ls src/test non-match, prompt contract (block renders, rule form, citation-only-from-the-list, explicit no-failure statement, byte-identical when the argument is omitted), parseCandidates citation shapes, and four end-to-end capture runs: attested lesson stored, unattested refused with a receipt, uncited blocker unaffected, recovered failure refused.
  • Diff is additions only.

Refs the bench plan benchmark-tests/ocsi/PLAN-learn-from-failure-2026-11-10.md.

Closes #381

Follow-ups filed from the same measurement, deliberately not in this diff: #382 (auto-capture turns task-brief boilerplate into a persistent repo "fact") and #383 (captureSignalSince still requires success, so captureOnSignal can refuse the very pass that would capture a lesson).

…ted one

Measured on the bench (results-learn-t3-diag, 2026-11-10): a session that failed
after 574 s and 33,287 tokens wrote ONE captured row, and it was a paraphrase of
a sentence in the task brief. Zero lesson-shaped rows. The capture prompt listed
"blockers hit" as one word in a list of seven and never mentioned failure, dead
ends or ruled-out hypotheses — the model was not declining to store lessons, it
was never asked. This is the mechanism the plugin is for: every result the bench
has so far needed a human to state the fact in pass 1.

The prompt now gets an ATTESTED FAILURES block and asks for one `blocker` row per
failure worth a lesson, in the form `when <condition>, avoid <action>`, naming a
file or symbol so the rule can later be checked against the code.

The block is not the model's recollection. attestedGateFailures reads the tool
ledger for gate commands that FAILED with no later PASS of the same family, and
a candidate carrying "failure" is dropped before the write gate unless that
citation appears in the ledger. ReMe (Findings ACL 2026) found selective
addition BEATS full addition, because one failed trajectory rarely carries
enough context for a correct analysis and yields misguided experiences: the way
this feature fails is by storing a wrong rule, and a rule about a failure that
never happened rides every later session as an instruction. A lesson that cites
a failure the agent already fixed is refused for the same reason — captureSignalSince
reads the same ledger for the opposite purpose, since a recovery is the best
REASON to capture and an unrecovered failure is the best thing to capture.

The gate is monotone. Only a row carrying `failure` makes the claim, so an
ordinary uncited `blocker` note — a type this path has always written — is
untouched. Refusing every uncited blocker would delete rows the store has always
kept, and a provenance fix does not buy a recall loss here. `failure` is a
write-time check, not a stored field, so no row format changes.

gateCmd had no `test` family: `bun test`, `npm test` and `pytest` registered as
no gate at all, so a session could go red-green-red and the ledger never saw it.
That made attestation useless for the commonest failure and meant `gateRecovered`
could not fire for it either. The matcher is anchored on the runner, not the
word, because the word appears everywhere in a repo with a test directory —
`ls src/test` and `cat test/fixtures/a.json` are not test runs.

Cost: no new model calls, and the block is a handful of lines bounded at eight
failures. captureOnSignal is unaffected — it returns `edited` first for any
session that wrote a file, which is the common case, so the added `test` family
only changes verdicts for read-only sessions that ran a test and recovered.

Typecheck clean. 1242 pass / 19 skip / 0 fail. Lint unchanged at 542 warnings,
147 infos. Diff is additions only.
@abogartz

Copy link
Copy Markdown
Owner Author

Prompt A/B against the real capture model — and a correction to the premise above

A full bench session cannot test this change, and the reason is worth recording: the failed session in benchmark-tests/ocsi/results-learn-t3-diag ran zero gate commands. It explored, wrote a scratch bun run tmp/probe.ts, and deleted it — because makeRepo deletes test/, so bun test reports "0 test files matching" and there is nothing to gate against. No gate run means no attested failure, which means this change has nothing to bite on in that run.

So the prompt was measured directly: benchmark-tests/ocsi/probe-capture-prompt.ts. Same transcript, same model, both prompts built by importing this repo's own buildCapturePrompt. The failing-test text inside the transcript is real output — the hidden suite against base2 rebuilt with t3, 24 pass / 2 fail — not invented. 5 reps per arm.

old prompt new prompt
rows emitted 10 10
blocker rows 4 5
cited an attested failure 0/10 5/10
rule form when … avoid … 0/10 5/10
lesson-marked rows 7/10 8/10
docs platitude emitted 5/5 5/5
mean completion tokens 3,094 7,268

Representative old-arm row:

[blocker] "Hidden suite still fails in src/pricing.ts after setting TAX_RATE to 0.0825; reordering rounding so tax is applied before shipping caused additional failures, so the original rounding order was restored."

Representative new-arm row:

[blocker] cites "test — bun test" — "When src/pricing.ts totals fail, avoid treating a change of TAX_RATE to 0.0825 as a complete fix; bun test still failed after that change."

What this corrects

The ticket says the old prompt stores nothing from a failure. That is what results-learn-t3-diag showed, but the probe shows why: given failure evidence in the transcript, the old prompt already writes negative knowledge, in narrative form, 4 blockers in 5 reps. The one-word "blockers hit" was not inert.

So the honest split of the cause is:

  1. The dominant blocker is upstream of this PR. The session never runs a gate, so there is no failure evidence to mine. That is captureSignalSince requires success, so a pure failure is never captured #383 and the fixture, not the prompt.
  2. What this PR actually adds is verifiability, not the ability. 0/10 → 5/10 citations and 0/10 → 5/10 rule form. The old narrative rows are unfalsifiable prose about one session; the new rows name a command the ledger can check and a rule a later session can act on or reject. That is the difference the ReMe selective-addition result says matters.

On the cost

Completion tokens go 3,094 → 7,268, and one rep hit 13,088 with a 272 s call. That is real and it is not disqualifying on its own: the goal is better future runs, not cheaper capture. Whether the extra deliberation buys anything is a separate measurement, and it is running now — arm N (benchmark-tests/ocsi/chain-learn.ts --nudge) puts the five real lesson rows from this probe in front of fresh sessions on the same bug, against an empty-store baseline and against a platitude arm that measures what a memory which helps nothing costs. Its verdict rule was written before the seat was spent: HELPS / NEUTRAL / HURTS.

Results go on #381.

@WhollyMindless WhollyMindless left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two independently reproduced P2 findings on 458b8ce:

  1. Redact the new attested-command prompt input. src/capture/runtime.ts:494–508 builds attested from raw ledger arg, and src/capture/prompt.ts:120 interpolates those strings without redactSecrets. Through the real tool-result/capture hooks, a failing bun test --token=<synthetic sk-token> puts that recognized token verbatim in generate.text's ATTESTED FAILURES block, even with a token-free transcript and an empty model response. The scrubber redacts the identical token in the control. The existing transcript/user/known-row redaction does not cover this new input. Please redact before rendering, and compare citations against the same safe representation shown to the model.

  2. Match an attested command, not a substring of the joined list. At src/capture/runtime.ts:625–626, lessonHay.includes(cite) accepts a lesson citing tsc after the only failed command was bun test --test-name-pattern tsc. The row persists and auto_capture_lessons records kept=1, although no typecheck ran. Changing that test-name argument to cart correctly refuses the same candidate. Membership in a normalized set of the permitted complete citations would preserve the claimed attestation boundary; substring presence cannot establish that the cited command ran.

Validation: 128 focused capture/failure/policy/disposal tests pass; three additional synthetic hook-level probes pass with six assertions establishing the two behaviors and the refusal control; typecheck passes. Windows, Bun 1.4.4, fake generation and isolated stores only—no live credentials, model calls or production store. I read the author's prompt A/B correction and the deliberately deferred #382/#383 scope; these findings concern the new attestation path itself.

…ield

Review findings on #384 (WhollyMindless), both reproduced as stated.

1. The ATTESTED FAILURES block interpolated RAW ledger args. A failing
   `bun test --token=<sk-...>` put the credential verbatim into
   generate.text's prompt even with a token-free transcript -- the #132
   transcript scrub covers the transcript, not this newer input. Entries are
   now redacted at construction, so the prompt renders the safe string and the
   citation check compares against the same representation the model was shown:
   an honest copy of what was displayed still passes, and the raw token cannot
   ride the row either.

2. `lessonHay.includes(cite)` accepted a lesson citing `tsc` when the only
   failure was `bun test --test-name-pattern tsc` -- substring presence cannot
   establish that the cited command ran. Membership is now tested against
   COMPLETE displayed fields: each whole entry, its cmd, and its arg. The
   review's `tsc` case is none of those and is refused (kept=0, refused=1,
   receipt pinned); a cmd-only citation -- the shape the prompt's own example
   uses -- still passes, pinned as a no-recall-loss control.

- src/capture/runtime.ts: redactSecrets at entry construction; lessonHay ->
  lessonSet of whole fields
- test/capture-failure.test.ts: three regressions (substring refused, exact
  cmd passes, credential never reaches the prompt)

Co-authored-by: OpenCode <[email protected]>
@abogartz

Copy link
Copy Markdown
Owner Author

@WhollyMindless both findings confirmed and fixed in 15e764e.

1 — attested args redacted at construction. attested entries now pass through redactSecrets where they are built, so the ATTESTED FAILURES block renders the safe string and the citation check compares against the same representation the model was shown — your "compare citations against the same safe representation" direction, taken literally. Regression: a failing bun test --token=sk-… — the prompt contains ATTESTED FAILURES and [redacted], never the token; an honest copy of the displayed (redacted) entry still stores.

2 — whole-field citation membership. One refinement over your suggested set: the list renders cmd — arg, and the prompt's own accepted shape includes cmd-only citations, so membership is tested against each complete displayed field — the whole entry, its cmd, and its arg — never a fragment. Your tsc case is none of those against bun test --test-name-pattern tsc and is refused (kept=0, refused=1, receipt pinned); the exact cmd and the exact full entry pass (no-recall-loss control pinned).

Three regressions in test/capture-failure.test.ts; bun run gate green: 1234 unit, 30 e2e, typecheck, lint.

@WhollyMindless WhollyMindless left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independently rechecked 15e764edef56cf7b2108246c954ec0259072bb02: both previously reported findings are resolved. Replayed the failing-command synthetic credential case: the actual capture prompt contains [redacted] and no token. Replayed the unrun tsc fragment against bun test --test-name-pattern tsc: no cart row is stored, and the lesson receipt reports kept=0. The unmatched-citation control still refuses.

131 focused failure/capture/policy/disposal tests, three separate hook probes (7 assertions), and typecheck pass on Windows/Bun 1.4.4. Approval covers this head and the independently verified boundary fixes; no live-model or benchmark certification.

@abogartz
abogartz merged commit a494ee5 into main Nov 12, 2026
4 checks passed
abogartz-opencode-self-improvement Bot pushed a commit that referenced this pull request Nov 12, 2026
# [0.23.0](v0.22.2...v0.23.0) (2026-11-12)

### Bug Fixes

* **capture:** redact attested args, and match a citation as a whole field ([15e764e](15e764e)), closes [#384](#384) [#132](#132)
* **capture:** the prose extractor keeps the sign before granting code-checked ([838ac7e](838ac7e)), closes [#399](#399)
* **gate:** different environments are two facts, never a correction ([64184c5](64184c5)), closes [#400](#400) [pre-#390](https://github.com/pre-/issues/390) [#399](#399)
* **gate:** digits are context; only the corrected value may drop out ([d24744b](d24744b)), closes [#400](#400)
* **gate:** excise the asserted value by position, not by literal ([8114cec](8114cec)), closes [#400](#400)
* **gate:** same-context is established by the words, not looked up ([0d0afbe](0d0afbe)), closes [#400](#400) [pre-#390](https://github.com/pre-/issues/390)

### Features

* **capture:** let a failed session leave a lesson, but only an attested one ([458b8ce](458b8ce))
* **gate:** a correction retires the row it corrects in the data ([bcba2f4](bcba2f4)), closes [#390](#390) [#393](#393) [#208](#208) [#389](#389) [#393](#393) [#393](#393)
* **provenance:** an unverified row says so where the model reads it ([8bcb6ac](8bcb6ac)), closes [#386](#386)
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.23.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto-capture stores nothing from a failed session

2 participants