Skip to content

learn: right tool per result, misuse notes count, nightly SCARS refresh - #628

Merged
FMXExpress merged 3 commits into
mainfrom
claude/pasclaw-learn-scars
Nov 5, 2026
Merged

FMXExpress merged 3 commits into
mainfrom
claude/pasclaw-learn-scars

Conversation

@FMXExpress

@FMXExpress FMXExpress commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

Three fixes to pasclaw learn, the failure mining that fills SCARS.md. They came out of reading what it made of the live security run. Two more commits fix what two rounds of code review found; see Review fixes and Second review below.

1. Each result goes to the call that made it

A tool result was matched to the first call in the session with its id. Some servers number calls per turn (c1, c2, ...): the relay provider, and several local OpenAI-compatible servers. So every later c1 went to turn one's tool.

On the live security run, the eval refusal from shell_exec was reported as a grep_files failure.

The fix: the latest call with the id wins. A result always follows its own call.

2. Misuse notes count

Some tool results say the call was the wrong one and what to do instead:

  • grep_files given a regex: (no matches) -- note: grep_files matches a LITERAL substring ...
  • a definition hunted by guessing its keyword: -- to find where a name is defined ... code_outline symbol="X"

Nothing failed, so nothing counted them. They are patterns now, of kind misuse, shown in the report and in SCARS.md (**Kind:** misuse).

  • They cluster by what was done wrong, not by their text. The definition note names the symbol searched for, so two hunts for two different names are one pattern (§GREP-FILES-DEFINITION-HUNT-KEYWORD), not two one-offs.
  • A test runs grep_files itself, so a reworded note fails that test instead of going quietly uncounted.

3. The nightly pass runs it

The mining moves out of the command into src/pkg/memory/PasClaw.Learn.pas. pasclaw maintain, and so the @maintain cron entry, runs it over the last 30 days:

  • The report gets a Recurring section. Each pattern seen in 2+ sessions is listed with its count, tool, sample and the §ANCHOR it has in SCARS.md. The optional review model sees this section too.
  • SCARS.md gets an anchor for each recurring pattern it lacks. This is what learn --write-scars does by hand.
    • Existing anchors, and what the operator wrote under them, are left alone.
    • --dry-run lists the patterns and how many anchors a real run would add, and leaves the file untouched.
  • Each session is read once, and its messages walked once, for all three windows (stats, skills, learn), and each window is checked on its own.
  • The cron summary line adds N recurring (M new in SCARS.md).

Review fixes

The nightly pass writes SCARS.md with nobody looking, so what it mines and what it keeps had to tighten.

  • Quoted text is no longer mined.

    • Readers count only by their first line. For a tool whose result is text it read, fetched or keeps, only line 1 counts, and only as the tool's own ERROR: or misuse note.
    • SCARS.md and report lines are skipped everywhere, wherever they show up, including a cat in a shell.
    • Why: without this, SCARS.md read back in two sessions came back as new patterns, and the next night wrote anchors for its own **Symptom:** lines.
  • Misuse notes are one table in the new PasClaw.Tools.Notes.

    • grep_files builds its notes from it, and its output is byte-identical (make bench-codenav is unchanged).
    • Learn reads the same table, and a line counts only when it starts with a note, so a file that quotes a note is not a misuse.
  • Credentials are masked as <redacted> before a line is kept, in both the sample and the signature. This covers:

    • URL userinfo;
    • tokens with a known prefix (ghp_, sk-, xoxb-, AKIA, ...);
    • the value after password= / token: / Bearer;
    • long mixed-case alphanumeric runs.

    The reason: pasclaw export copies SCARS.md into committed rule files. Hashes and plain lowercase words stay as they are.

  • Anchors.

    • Renames are followed. A pattern is found again by its signature marker, so an anchor the operator renamed keeps being cited under its new name.
    • Collisions get a suffix. Two patterns that derive the same five-word anchor become -2, -3, .... Before, the second was dropped as "already there".
    • Hand-written blocks. A block with no signature marker is matched to the pattern whose anchor it carries, at most once.
    • What the report cites. The report cites the anchor each pattern actually has in the file.
  • A failed SCARS write reports nothing added.

  • One failure definition. Maintain's session stats use Learn's classifier, so the Sessions count and the Recurring list agree, and misuses are counted (..., 1 tool failure, 1 misuse).

  • The stats window is checked on its own, so a --since longer than unused_skill_days counts every session in it.

  • Efficiency. One signature index for the whole pass, line splitting in a single pass, a stable merge sort, samples clipped by character, and the pattern kind as an enum.

Second review

  • Board and subagent tools quote too. task and rule carry the checks' output, and agent, spawn_wait and spawn_status carry another agent's answer.
    • Why: one failing check read by every agent on a team had recurred across their sessions and earned a SCARS anchor. In the live security run, the rule result carries a check that failed (exit 1), with its output; that failure is the point of the proof.
    • Where the list lives: it is QuotingTools in PasClaw.Tools.Notes, beside the tools.
    • A test enforces it: it registers the tools and fails on a read-only tool that is in neither that list nor its own-outcome list (plan_write). It also fails on a listed name that is not a real tool.
  • Bare status lines. A line that says a result failed but not how is bare: a nonzero exit=N, a traceback's header, build FAILED (...).
    • A bare line counts the result as failed, and is never a pattern. exit=1, the first line of every failing shell_exec, had been a content-free pattern shared by every failing command; the traceback header was one shared by every Python error.
    • Every nonzero exit counts now, not only 1 and 2: pytest's 4 and 5, curl's 6 and 7.
    • Maintain's failure sample is the line that says how the command failed.
  • Undated sessions age. A session without timestamps ages by its CreatedAt, else by its file's modification time. One of unknown age is outside a window, so undated sessions no longer recur in "the last 30 days" forever.
  • Lines are cleaned before they are kept.
    • Colour codes are removed, so a coloured and a plain run of one failure are one pattern.
    • Lines are redacted over their first 4,000 columns, then cut to 400. So a minified bundle on one line is not a megabyte in SCARS.md, and no secret is cut in half before it is seen whole.
  • Redaction also covers a flag's value after a space (--password x) and whole base64 keys (+, or = padding). Paths, and KEY=value with its key, stay readable.
  • More recorded lines. The Patterns bullets --write puts in MEMORY.md, and recurring lines without an anchor, are recorded lines too.
  • SCARS.md signatures compare by case, as the miner keeps them, and the file is read once per write.

On the live run's sessions

before after
pasclaw learn --min 1 1 pattern: the eval refusal, tool grep_files 2 patterns: the refusal, tool shell_exec; the regex misuse, tool grep_files
pasclaw maintain (no recurring step) "nothing went wrong the same way in 2 or more sessions"

The second row is correct: each problem happened once, in one session.

To exercise the recurring path through the real binary, I added a copy of the security session under a second id, as if it had happened twice. This is a simulation, not real data:

  • The first maintain listed both patterns and wrote two anchors.
  • The second reported 0 new anchors added, 2 already there.
  • --dry-run said 0 new anchors would be added.
  • The rule result's check output was not mined.

The § mark

The anchor mark is a SectionSign constant.

  • A literal § came out as the Latin-1 byte in a test binary, because FPC transcodes literals through the runtime codepage.
  • The #$C2#$A7 byte escapes used before are two characters under Delphi.
  • The constant is the UTF-8 bytes under FPC and #$00A7 under Delphi.

Tests

  • learn_tests cover:
    • attribution: repeated ids, a turn split over two messages, an id containing =;
    • misuse: clustering across names; grep_files run for real, both for its notes and for a file that quotes a note;
    • quoting: reads, memory searches, grep hits, board and subagent results, and SCARS/report/MEMORY lines quoted back are all left out;
    • bare lines and every nonzero exit;
    • cleaning: colour codes, the length cap, and a secret across the cut;
    • redaction: 12 secret shapes masked and 6 look-alikes left alone;
    • anchors: collision, rename, hand-written block, dry run, an unwritable path, and case-distinct signatures;
    • the window rule and SessionStamp;
    • the tool classification over the real registries;
    • the sort is stable.
  • maintain_tests use sessions at 2h, 10d, 25d, 33d and 40d, plus two undated ones aged 5 and 40 days by their files. They cover:
    • the learn window: 4x across 4 sessions;
    • quoted SCARS lines, both in the learn window and in the stats;
    • one misuse in the stats;
    • a 35-day stats window;
    • the renamed anchor cited by its new name;
    • the dry-run line;
    • exit=1 results sampled by the line that says how, as valid UTF-8.
  • Revert-checked: 15 checks on the first commit, 45 on the second and 33 on the third. Each undoes one behaviour, and every one fails a test.

Verification

  • make test passes on FPC 3.2.2.
  • make bench-codenav: the definition-hunt bytes (8,499) are unchanged.
  • Not compiled with Delphi (dcc64 is not available in this environment). PasClaw.Learn.pas and PasClaw.Tools.Notes.pas are added to PasClaw.dproj.

Notes

🤖 Generated with Claude Code

https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U

…ightly

Three changes to failure mining, the record behind SCARS.md.

Attribution. A tool result went to the first call in the session with
its id. Some servers number calls per turn (c1, c2 ...: the relay
provider, several local OpenAI-compatible servers), so a later turn's
result was blamed on turn one's tool. On the live security run, a
shell_exec refusal was reported as grep_files'. The latest call with the
id wins now.

Misuse. When a tool's result says it was used wrongly (grep_files given
a regex, a definition hunted by guessing its keyword), nothing failed,
so nothing was counted. Those lines are patterns now, of kind "misuse".
They cluster by what was done wrong rather than by their text, because
the definition note names the symbol searched for and every name would
otherwise be a one-off. A test runs grep_files itself, so a reworded
note fails there instead of going quietly uncounted.

Nightly. The mining moves from the learn command into PasClaw.Learn,
and pasclaw maintain (and so the @Maintain cron entry) runs it over the
last 30 days: a Recurring section in the report, and an anchor in
SCARS.md for each recurring pattern the file lacks (not on --dry-run).
Existing anchors and the operator's text under them are left alone.
Each session is still read once for all three windows.

The anchor mark is a SectionSign constant: a literal glyph was
transcoded to the Latin-1 byte in a test binary, and the UTF-8 byte
escapes are two characters under Delphi.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-05T18:44:46.863942Z 4ce9e95 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4ce9e951dd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/cmd/PasClaw.Cmd.Learn.pas Outdated
Comment on lines +248 to +251
if not WriteScars(Path, Patterns, MinSessions, Fresh, Skipped) then
begin
PrintLn(Ansi.Red + 'could not write ' + Path + Ansi.Reset);
Exit;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Propagate SCARS write failures to the command exit status

When learn --write-scars cannot read or write SCARS.md (for example, because the memory directory is read-only or the disk is full), WriteScars now catches the exception and returns false, but this branch only prints a message and exits the procedure. Cmd_Learn_Run consequently returns its initialized status of 0, so scripts treat a failed persistence operation as successful. Previously, the I/O exception propagated instead. Return the failure to Cmd_Learn_Run and set a nonzero exit status.

Useful? React with 👍 / 👎.

The nightly pass writes SCARS.md unattended, so what it mines and what it
keeps had to tighten.

- Quoted text is not mined. A tool that fetches or stores text (read_file,
  grep_files, memory_search, web_fetch ...) counts only by its first line,
  and only as its own ERROR: or misuse note; lines of SCARS.md and of a
  maintenance report are skipped wherever they turn up. Before, SCARS.md
  read back in two sessions came back as new patterns, and the next night
  wrote anchors for its own Symptom lines.
- Misuse notes are one table in the new PasClaw.Tools.Notes, which
  grep_files builds its notes from (output byte-identical) and Learn reads;
  a line counts only when it starts with a note, so a file or a memory
  note that quotes one is not a misuse.
- Credentials are masked before a line is kept, in sample and signature:
  URL userinfo, prefixed tokens, password= / token: / Bearer values, long
  mixed-case runs. SCARS.md goes into committed rule files via export.
- WriteScars hands back counts only after the write, follows a renamed
  anchor through its signature, gives a colliding anchor a -2 suffix
  instead of dropping the pattern as "already there", claims a
  hand-written block once, and has a dry-run mode. It returns each
  pattern's anchor as the file has it, which the report now cites.
- Maintain loads each session for the widest of its three windows and
  checks each on its own, so --since past unused_skill_days counts every
  session in it. Its session stats use Learn's classifier, so the
  failure count and the Recurring list agree, and misuses are counted.
  Its own failure heuristic is gone.
- One window rule (InWindow) for both callers: a session with no
  timestamp is inside it, for maintain as for learn. ScanSession lost its
  own window check.
- A miner keeps one signature index for the whole pass; lines are split
  in one pass; the sort is a stable merge sort. Pattern kind is an enum.
  Samples are clipped by character, not byte.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
- Tools that quote stored text are a table beside the tools
  (QuotingTools in PasClaw.Tools.Notes), and it now covers the board and
  subagents: task and rule carry checks' output, agent and spawn_wait a
  subagent's answer. Before, one failing check read by every agent on a
  team recurred across their sessions and got a SCARS anchor. A test
  registers the tools and fails on a read-only one in neither the table
  nor its own-outcome list.
- A line that says a result failed but not how -- a nonzero exit=N, a
  traceback's header, "build FAILED" -- is Bare: it counts the result as
  failed and is never a pattern. "exit=1" had been a content-free
  pattern shared by every failing command, and the traceback header one
  shared by every Python error. Every nonzero exit counts now, not only
  1 and 2; maintain's sample is the line that says how it failed.
- A session without timestamps ages by its CreatedAt, else its file's
  mtime, and one of unknown age is outside a window: undated sessions
  no longer recur in "the last 30 days" forever.
- Lines lose their colour codes, are redacted over their first 4000
  columns and then cut to 400, so a minified bundle on one line is not
  a megabyte in SCARS.md and no secret is cut before it is seen whole.
- Redaction also takes a flag's value after a space (--password x) and
  whole base64 keys ('+' or '=' padding).
- MEMORY.md's Patterns bullets and recurring lines without an anchor are
  recorded lines too.
- SCARS.md signatures compare by case, as the miner keeps them; the
  file is read once per write; maintain walks each session's messages
  once and classifies each tool result once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
@FMXExpress
FMXExpress merged commit 324d28b into main Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants