gstack icon indicating copy to clipboard operation
gstack copied to clipboard

Lines removed by /learn prune come back after a sync merge: gstack-jsonl-merge keeps every base line

Open exGeni opened this issue 3 weeks ago • 3 comments

What happened

/learn prune tells the agent to remove an entry by deleting its lines from learnings.jsonl (learn/SKILL.md.tmpl:49). With artifacts sync on, those lines can come back with no warning when a later merge or stash pop invokes the JSONL merge driver and its base or other side still holds them. A clone that only fast-forwards and has no local edits takes the removal correctly, because the driver does not run.

The cause is the merge driver. bin/gstack-jsonl-merge:13 says "Concatenate base + ours + theirs", and the loop at lines 57-85 reads all three files into one set. A line that one side deleted is still in the base, so it is always in the result. Git reports a clean merge (exit 0).

Two sync paths reach the driver, both described in docs/gbrain-sync.md:194-195 and implemented in bin/gstack-brain-sync (lines 609, 616, 622):

  1. A push is rejected and the sync merges origin/<branch>. The local clone's commits are built on a base that still has the line.
  2. A file the merge would change has local edits. The sync stashes it, merges, and pops the stash. The pop goes through the driver too, with a base that still has the line. The line is back in the working tree, and the next drain commits it as an ordinary one-parent commit that only adds lines. The same pop also undoes a prune the machine made itself and has not committed yet, if another machine first pushed a change to the same file: the stash then holds the removal, and the driver adds the line back from the base.

Expected: a line removed on one side and unchanged on the other stays removed, as in any three-way merge. Appends from both sides still survive.

This is already known: TODOS.md:5525 lists it as "P2 — brain-sync resurrects removed learnings". I found no issue or pull request for it, so this gives it a tracker entry with a reproduction and the two sync paths. The header of the driver calls these files append-only, and that holds for most of them. /learn prune is a documented writer that removes lines; its update is not affected because the latest entry wins at read time. docs/gbrain-sync.md at this commit covers removal of whole files ("Removed and moved files", line 120) and does not cover removal of lines inside a synced JSONL file. test/jsonl-merge.test.ts has no case for a line removed on one side.

We saw this on our own artifacts repo, synced between machines. A prune removed 206 lines from one project's learnings.jsonl. Nine days later a one-parent sync commit added 191 lines and removed none; 158 of them matched removed lines byte for byte. That is the shape of path 2. We did not trace it on the machine that made the commit, so treat it as consistent with this bug and not as proof of it. The reproduction below is the proof.

Steps to reproduce

Run this in an empty directory. It uses only git and the driver.

DRIVER=~/.claude/skills/gstack/bin/gstack-jsonl-merge
git init -q --bare -b main origin.git
git clone -q origin.git a 2>/dev/null
setup() { git -C "$1" config user.email t@t; git -C "$1" config user.name t
          git -C "$1" config merge.jsonl-append.driver "$DRIVER %O %A %B"; }
setup a
echo '*.jsonl merge=jsonl-append' > a/.gitattributes
printf '%s\n' '{"ts":"2026-01-01T00:00:00Z","key":"kept"}' '{"ts":"2026-01-02T00:00:00Z","key":"pruned"}' > a/learnings.jsonl
git -C a add .gitattributes learnings.jsonl; git -C a commit -qm base; git -C a push -q origin main
git clone -q origin.git b; setup b
git clone -q origin.git c; setup c

# Machine a prunes and pushes.
sed -i '/"pruned"/d' a/learnings.jsonl; git -C a commit -qam prune; git -C a push -q origin main

# Path 1: machine b committed an append before it saw the prune.
echo '{"ts":"2026-01-03T00:00:00Z","key":"b-new"}' >> b/learnings.jsonl; git -C b commit -qam b-append
git -C b fetch -q origin; git -C b merge --no-edit origin/main; echo "merge exit $?"; cat b/learnings.jsonl

# Path 2: machine c has an uncommitted append when the sync merges.
echo '{"ts":"2026-01-04T00:00:00Z","key":"c-new"}' >> c/learnings.jsonl
git -C c stash push -q -- learnings.jsonl; git -C c fetch -q origin; git -C c merge --no-edit origin/main
git -C c stash pop -q; echo "pop exit $?"; cat c/learnings.jsonl

sed -i is the GNU form. On macOS use sed -i ''.

Result, with the driver at the commit below:

merge exit 0
{"ts":"2026-01-01T00:00:00Z","key":"kept"}
{"ts":"2026-01-02T00:00:00Z","key":"pruned"}
{"ts":"2026-01-03T00:00:00Z","key":"b-new"}
pop exit 0
{"ts":"2026-01-01T00:00:00Z","key":"kept"}
{"ts":"2026-01-02T00:00:00Z","key":"pruned"}
{"ts":"2026-01-04T00:00:00Z","key":"c-new"}

Both results contain pruned, and each has its own new line, which is correct.

Possible fix

TODOS.md:5525 proposes a removal marker that the driver and every raw reader honour. A smaller option is worth weighing first: keep a base line only when it is still in both ours and theirs. Lines new on either side are kept as they are today, so appends from two machines still survive, and sorting and dedup do not change. The rule is symmetric in ours and theirs, so both machines still converge, and readers need no change because a removed line is simply gone.

The limit: the driver is registered for every *.jsonl, so any other writer that rewrites or rotates a synced JSONL file would start propagating its removals too. I did not audit those writers. A marker also covers one case this does not: a clone that re-adds the same line later.

The workaround today is to prune again after every machine has pulled, and to repeat it whenever the lines return. Nothing warns you that they did.

Readiness report

gstack doctor 1.98.0.0
ok              install root    ~/.claude/skills/gstack: claude global current (1.98.0.0), agy global current (1.98.0.0), codex global current (1.98.0.0)
ok              rescue refs     none in ~/.claude/skills/gstack
ok              state root      ~/.gstack durable=yes (selected by default)
ok              bun             1.4.4 at /usr/local/bin/bun (floor 1.3.3, tested 1.4.2)
ok              hooks           15 hook(s) parse
ok              codex           codex-cli 0.160.1; self-locate ok; model gpt-6.1-sol (source: GSTACK_CODEX_MODEL)
ok              codex probe     cached MODEL_OK, 11h ago (not re-run; --live probes now)
ok              artifacts sync  status ok (mode full; queue 17)
ok              browse bundle   browse/dist built
ok              binaries        find-browse, design, pdf, gstack-global-discover launch (4 of 4)
ok              cso             native helper 1.98.0.0 (910093087dec)
ok              claude code     2.1.309 (Claude Code) at /usr/local/bin/claude
ok              session journal no session journals for this project
ok              guard log       75 decisions (5 denied)
ok              CLAUDE.md       no gstack section lists skills (~/.claude/CLAUDE.md)
15 ok, 0 warn, 0 not configured, 0 fail

Environment

  • Host (Claude Code, Codex, Cursor, ...) and its version: Claude Code 2.1.309
  • OS: Debian GNU/Linux 12, Linux 7.0.0-43-generic; git 2.39.5, Python 3.11.2
  • gstack: running 1.98.0.0; code and docs cited at main ba2289e1cd9f2387d069b3b6c51b404d56c4a1e9. bin/gstack-jsonl-merge has the same blob at both (4a7074186955).
  • Related, different problems in the same sync: #4174 (a derived snapshot conflicts in the retry merge), #3704 (the retry merge itself), #3659 (driver encoding on Windows). A tracker search for "gstack-jsonl-merge", "learn prune", "pruned learnings" and "merge driver deleted lines" found no issue on this. The neighbouring TODOS.md entry "learnings.jsonl has no writer lock" is about a different race in the same prune step.

exGeni avatar Nov 09 '26 12:11 exGeni

Update on our own case from the issue text: we traced it. The one-parent commit came from a machine that repaired a stuck sync by hand with git pull --ff-only --autostash in ~/.gstack. Its learnings.jsonl held queued local lines on a base from before the prune, so the autostash re-apply ran through the driver with that base, and the lines came back. The next drain committed them.

This is the mechanism of path 2, reached through a manual pull instead of the retry merge. With the setup from the issue, machine b appends without committing and then runs git pull --ff-only --autostash: exit 0, and pruned is back in the working tree.

The trace rests on that machine's repair log, which names the command and the same commit size (133 files). We did not replay it on that machine.

exGeni avatar Nov 09 '26 13:11 exGeni

Thanks, and thanks for tracing your own case. Confirmed by reading main at 119ed92: gstack-jsonl-merge takes the union of base, ours and theirs, so a line removed on one side comes back from the base, and test/jsonl-merge.test.ts has no removal case. It's the same as the P2 "brain-sync resurrects removed learnings" in TODOS.md, so this issue is now its tracker. It's on our list for a crew fix.

capy-ai[bot] avatar Nov 09 '26 16:11 capy-ai[bot]

Thanks, and thanks for tracing your own case back to the manual git pull --autostash repair. The merge driver does keep a line one side removed, and that's noted in TODOS.md, but fixing it means a real three-way merge for JSONL, which is more than the prune path justifies right now. Closing; if a normal sync, with no hand repair, brings removed lines back for you, please open a new issue with that sequence.

capy-ai[bot] avatar Nov 16 '26 03:11 capy-ai[bot]