Skip to content

feat(integrations): two-pass /distill + Claude Code /nt-learn, /nt-distill - #159

Merged
navbytes merged 6 commits into
mainfrom
claude/compact-learnings-notes-zbuz5s
Jul 28, 2026
Merged

navbytes merged 6 commits into
mainfrom
claude/compact-learnings-notes-zbuz5s

Conversation

@navbytes

Copy link
Copy Markdown
Owner

What

Extends /distill with a second pass over the always-loaded block, and ports both /distill and /learn to Claude Code — the runtime that had neither.

Pass 2 — rules/ + memory/

Lessons and notes are free until retrieved. rules/ and memory/ are not: they're injected into (Pi, OpenCode) or compiled into (Claude Code, via nt export --tag rule) every request, forever. That's where consolidation actually buys something, and nothing looked at it before.

Pass 2 reviews that block for four things:

Finding Fix
Subsumption — a general rule already covers a specific one keep the general, retire the specific
Contradiction — two rules point opposite ways surface and ask; never auto-resolve
Dead trigger — names a file/command/dep that's gone verify by grepping the repo first, then archive
Not actually always-relevant demote to a lesson, don't delete

The last one is the common case and the reason this is worth doing: a demoted rule still fires through nt_recall when its situation comes up — it just stops being billed on every unrelated request. nt_mv to lessons/ + nt_tag remove:["rule"] add:["lesson"]; the body is untouched.

Both passes land in one approval list. Nothing is merged, demoted, or retired without approval — unchanged from before, and consistent with the roadmap's rejection of silent LLM-driven consolidation.

Why one prompt, not a second command

The two passes share their write primitives (nt_note_edit / nt_archive superseded_by / nt_tag) and their approval gate, so splitting them would duplicate the back half of the file — in a repo whose subject is deduplication. More practically, two skills whose descriptions both amount to "tidy the nt store" are a mis-trigger waiting to happen. /distill rules runs pass 2 alone for its different cadence.

Extending /distill rather than minting a new name keeps the north star intact: one prompt, three runtimes, no muscle memory broken.

Claude Code

It shipped only the nt skill and the MCP server — no /learn, no /distill, and SKILL.md never mentioned nt distill existed at all. Both are now bundled as skills, picked up by the plugin's existing .claude/skills glob (no plugin.json change needed).

Named nt-learn / nt-distill rather than matching Pi and OpenCode exactly, because docs/claude-integration.md tells users to copy skills into ~/.claude/skills/ — a global namespace shared with every other tool, where bare learn and distill are too generic to claim. The prompt bodies are the same file.

Pass 2 earns the most here, which is the argument for the port: Pi and OpenCode cap the block at NT_INJECT_MAX and emit an agent-visible over-budget nudge. Claude Code has no injection layer and therefore no size signal at all — a prompt is the only one available.

Changed

  • integrations/pi/prompts/distill.md, integrations/opencode/commands/distill.md — two-pass rewrite
  • integrations/{pi,opencode}/…/learn.md — cross-link to the reverse pass from the ⚠ always-in-context flag
  • .claude/skills/nt-distill/SKILL.md, .claude/skills/nt-learn/SKILL.md — new
  • docs/claude-integration.md §2b, both integration READMEs

Verification

go build, go vet ./..., go test ./... all clean. Both new skills parse and load (confirmed registered by the harness). No Go code changed — the embed directives already cover whole directories, so the edited prompts need no wiring change.

Currently being field-tested: five agents working real tasks across the repo's separate modules against a shared store, after which the two-pass flow runs against whatever duplication and rule bloat they actually produced. Findings to follow in a comment.


Generated by Claude Code

…still

`/distill` gained a second pass over the always-loaded block. Pass 1 is
unchanged — `nt_distill`'s near-duplicate note pairs, reviewed to approval.
Pass 2 reviews `rules/` + `memory/`, the only notes billed on every request
(lessons and notes are free until retrieved), for four things: subsumption,
contradiction, dead triggers, and rules that turned out not to be
always-relevant. The fix for the last is a demotion to a lesson — still found
by recall, no longer injected — not a deletion. Both passes land in one
approval list; nothing is written without approval, same as before.

Kept as one prompt rather than a second command: the two passes share their
write primitives and approval gate, and two skills whose descriptions both
mean "tidy the nt store" are a mis-trigger waiting to happen. `/distill rules`
runs pass 2 alone for the different cadence.

Claude Code had neither prompt — it shipped only the `nt` skill and the MCP
server, and `SKILL.md` never mentioned `nt distill` existed. Both are now
bundled as skills, picked up by the plugin's existing `.claude/skills` glob.
Named `nt-learn`/`nt-distill` rather than matching Pi and OpenCode exactly:
`~/.claude/skills/` is a global namespace and bare `learn`/`distill` are too
generic to claim there. The prompt bodies are the same file.

Pass 2 earns the most on Claude Code, which is the runtime with no injection
layer: Pi and OpenCode cap the block at NT_INJECT_MAX and nudge when it's
exceeded, while Claude gets it via `nt export --tag rule` into CLAUDE.md with
no size signal at all.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgqQf7Lvou5F5as9aNmagu

Copy link
Copy Markdown
Owner Author

CI: govulncheck is red, but it fails on main too and predates this branch.

Vulnerability #1: GO-2026-5970
    Infinite loop on invalid input in golang.org/x/text
    Found in: golang.org/x/text@v0.37.0
    Fixed in: golang.org/x/text@v0.39.0
    #1: internal/cli/cli.go:132:15: cli.Run calls fmt.Fprintln,
        which eventually calls norm.Form.Properties

The scheduled Vulncheck run on main @ 94e0ab1b — this branch's base — failed on 2026-07-27. The same SHA passed on 2026-07-20, so GO-2026-5970 was disclosed between those two dates. This branch changes only prompt/skill markdown and docs; no Go code and no go.mod.

Not bundling the x/text 0.37.0 → 0.39.0 bump into this PR — it's an unrelated security change that deserves its own reviewable commit, and Dependabot covers root go.mod. Happy to push it here if you'd rather unblock this PR directly.

One related thing worth flagging, found during the field test below: desktop/go.mod pins the same vulnerable x/text@v0.37.0, and nothing watches it. .github/dependabot.yml has a single gomod entry at directory: "/", and Dependabot v2 doesn't auto-discover nested modules; govulncheck ./..., go vet, golangci-lint and CodeQL all run from the root, where go list ./... returns zero desktop packages. That module has needed manual catch-up bumps before (3966536, 98d1a9d), and golang.org/x/crypto lives there and nowhere else — so it currently has no automated watcher at all.


Generated by Claude Code

Field test, 5 concurrent agents against one shared store, 44 notes: three of
them independently hit the same footgun and two wrote it down as separate
lessons. `nt distill` reported "no near-duplicate notes found".

Not a bug in the detector — a structural limit. FindSimilar requires a shared
NON-structural tag, and `lesson`/`rule`/`memory-core` are excluded on purpose
(internal/note/note.go:690) so that every lesson isn't treated as related to
every other one. But `--kind lesson` applies only that one tag, so two lessons
with no topical tag can never pair on anything but an exact slug match. The
class of note the store accumulates fastest is the class dedup can't see.

Pass 1 now says so and adds a stub-level sweep of `lessons/` (nt_index returns
descriptions without bodies, so it stays cheap) for the semantic overlap the
title tokens miss. §1.1 no longer claims an empty result means a clean corpus.

Also: pass 2 now exits cleanly on a small or empty rules block instead of
hunting for something to prune. Same field test — all five agents correctly
declined to create any rule or memory-core note, so pass 2's target set was
empty, which is a healthy store rather than a finding.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgqQf7Lvou5F5as9aNmagu

Copy link
Copy Markdown
Owner Author

Field test results

Five agents, real tasks across the repo's separate modules (recall internals, the two TS ports, docs accuracy, CLI destructive-path coverage, the desktop/ module), all writing to one shared store. 44 notes. Then the two-pass flow ran against whatever they actually produced.

Both passes came back empty. Both for informative reasons.

Pass 1 reported "no near-duplicate notes found" — and it was wrong.

Three agents independently hit the same footgun; two wrote it up as separate lessons:

01KYM575…T3XHR1  lessons/  a note title beginning with a dir name plus slash
                           silently overrides --kind and loses that word
01KYM585…HXTA7M  lessons/  A slash in a note title silently overrides --kind
                           and truncates the title

Same lesson. Not a detector bug — a structural limit. FindSimilar requires a shared non-structural tag, and lesson/rule/memory-core are excluded deliberately (internal/note/note.go:690) so every lesson isn't treated as related to every other one. But --kind lesson applies only that tag. Both notes are tags: [lesson], so tagset is empty, sharedTag is always false, and the pair can only match on an exact slug — which it doesn't. The class of note the store accumulates fastest is the class dedup structurally cannot see.

The comment at note.go:684-690 says the exclusion exists because not excluding it "silently dropped parallel agents' captures." So this is a real trade-off with evidence on both sides now, not something to just flip. 4c2d4cd handles it in the prompt instead: pass 1 no longer claims an empty result means a clean corpus, and adds a stub-level sweep of lessons/ (nt_index returns descriptions without bodies, so it stays cheap) for the overlap titles miss.

Pass 2 had an empty target set — because the agents were right.

All five declined to create a single rule or memory-core note, each independently reasoning that nothing they'd learned was durable and always-relevant enough to justify permanent context cost. nt export --tag rule = 82 bytes, header only. That's learn.md's "be stingy" instruction working exactly as intended, and it means pass 2's value accrues over time rather than showing up on a fresh store. Pass 2 now says so and stops, instead of hunting for something to prune.

Honest read on the design

The strongest argument for pass 2 — that the always-loaded block is where compaction has real token payoff — held up. The weaker part is cadence: on a young store there's nothing there, so /distill rules will no-op for a while. That's an argument for the combined command, not against it. Had these shipped as two separate skills, a user would have installed a second command that does nothing for weeks.

Unrelated bugs the test surfaced

Not in scope for this PR, but found while agents were using nt in anger. Filing separately unless you want any of them here:

Severity Finding
Data loss mutate.TrashNote (internal/mutate/rename.go:161) uses os.Rename into .trash/, which clobbers. Delete → recreate → delete destroys the first version. .trash/ is the only recovery path (not journaled), and nt gc --yes calls it in a loop. Reproduced.
Silent partial write archiveNotes (internal/cli/maintenance.go:64-85) saves inside the loop, then bails on the first bad handle — notes end up archived while the command reports failure. cmdRm explicitly guards against exactly this (commands.go:1186-1189); archive never got the same treatment. Reproduced.
Doc says false thing SKILL.md: "[[<id>]] … backlinks are found automatically." Id-form wikilinks resolve forward but produce no backlink; only slug-form does. The id form is what nt prints you everywhere.
No repair path A note's title can't be changed from the non-interactive CLI. nt mv pins the old title into frontmatter, where it then wins over the body H1 — one agent had to hand-edit frontmatter in Python. nt mv also doesn't slugify its destination.
Integration OpenCode's NT_WRITE_TOOL regex omits nt_note_edit, so cache invalidation and write-detection miss it — breaking exactly the /learn and /distill flows, which drive most writes through nt_note_edit. Pi's regex has it.
Injection --description has no --description-file; backticks in it get shell-expanded, description silently truncates, exit 0. SKILL.md's escaping warning is scoped to bodies only.
Docs drift MCP tool count is 19, not 18 (nt_mindmap missing from both README and SPEC). nt search --full and nt show --section are documented but don't exist — and cmdShow silently ignores unknown flags, so --section exits 0 having done nothing. NT_ICONS/NT_GIT appear in SPEC and nowhere in the code. nt.log is documented as rotated at 10MB; LogFile() has no callers.

The path-style-title footgun (three agents, independently) is worth a note: 94e0ab1 was a fix for this, but its guard only rejects prefixes containing whitespace, so a bare directory token like internal/ or README/ still reads as filing syntax and gets eaten.


Generated by Claude Code

…ong field

The fifth field-test agent benchmarked the ranker directly and found that
`Title + tags + Description(240)` is the strong bag, so a demoted note's
description is the highest-weighted text it has. Measured: an ordinary
preamble line ahead of the substance costs a lesson 43% of its score and its
#1 rank; padding a description promotes an unrelated note over the right one
(score 0 -> 2218 from filler alone).

Pass 2 told the agent to rewrite a demoted rule's description into trigger-form
"when X, do Y — not Z" — which spends that field's limited budget on the
remedy, while a future session searches with the symptom. Now it says lead with
the symptom and keep it tight, which is what the measurements support.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgqQf7Lvou5F5as9aNmagu
@navbytes
navbytes marked this pull request as ready for review July 28, 2026 11:34
navbytes added a commit that referenced this pull request Jul 28, 2026
…lidates (#162)

Two data-integrity bugs found and reproduced during the multi-agent field
test in #159. Both silently shrink the store while reporting nothing wrong.

**TrashNote overwrote existing .trash/ entries.** It renamed straight onto
.trash/<rel with / → _>, and os.Rename overwrites its destination. Two
collisions are reachable in ordinary use: trashing the same rel twice (create
→ delete → recreate → delete), and two distinct notes that flatten to one name
(a/b.md and a_b.md). .trash/ is the ONLY recovery path — TrashNote is
deliberately not a journaled undo txn — so the overwritten copy was gone for
good, and `nt gc --yes` trashes in a loop while printing "collected N note(s)
→ .trash/ (recoverable)". The new regression test reproduces it: under the old
code 1 of 3 notes survived.

Destination names are now claimed with O_CREATE|O_EXCL before the rename, so
the rename can only land on a name nothing else holds — that also closes the
stat-then-rename race rather than narrowing it. Collisions get -2, -3, … .

**archiveNotes wrote before validating.** It resolved and saved inside one
loop, returning on the first bad handle, so `nt archive good1 good2 typo`
archived good1 and good2 — dropping them out of index, search and recall —
while the only output was an error and a non-zero exit. Nobody re-checks a
command that reported failure. cmdRm already resolves every handle up front
for exactly this reason and says so in a comment (commands.go:1186); archive
never got the same treatment. It does now.

Both regression tests were confirmed to fail against the unfixed code before
being committed.


Claude-Session: https://claude.ai/code/session_01FgqQf7Lvou5F5as9aNmagu

Co-authored-by: Claude <noreply@anthropic.com>
@navbytes
navbytes enabled auto-merge (squash) July 28, 2026 14:19
@navbytes
navbytes merged commit 4ae29ab into main Jul 28, 2026
9 checks passed
@navbytes
navbytes deleted the claude/compact-learnings-notes-zbuz5s branch July 28, 2026 14:20
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