claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

[BUG] Second `/compact` in one process re-appends the history the first one summarized away, transcript grows quadratically

Open thomasbachem opened this issue 2 months ago • 15 comments

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report
  • [x] I am using the latest version of Claude Code

What's Wrong?

The first /compact in a Claude Code process writes its compact_boundary and summary rows and nothing else. A later /compact in the same process first re-appends, to the session .jsonl, a copy of every message the process has held from where its history began (the transcript start, or the boundary it resumed from) up to the row before the previous boundary's preservedSegment.headUuid. The copies keep their original uuid, parentUuid and timestamp.

The finished turn's stop_hook_summary row is then written with parentUuid pointing at the last copied row instead of the assistant message it follows, and the new boundary's logicalParentUuid and preservedSegment.tailUuid point at that hook row while preservedSegment.headUuid points at the real last assistant message, thousands of rows earlier in a long file.

Two effects:

  • The transcript grows quadratically with the number of compactions, since each replay repeats everything before the previous boundary. One desktop session of mine is 166 MB and 17,234 rows, of which 7,394 rows (104 MB) are these copies
  • Following parentUuid from the newest row leads through the replayed copy and around every message written between the previous compaction and this one. Readers that honor preservedMessages re-stitch the chain, readers that don't see that stretch as a dead branch

In the 166 MB case the Claude desktop app logged transcript is 165736911 bytes; tail-loading last 52428800 bytes and its conversation view ended at the last message before the compaction, with a day of later turns missing, until I relaunched the app. I assume that is a downstream effect of the two points above.

Not the same as #78592, which was about per-edit file content. The duplicate rows here are whole message rows tied to compaction.

What Should Happen?

A /compact appends the boundary and the summary. Rows that are already in the file are not written again, and the stop_hook_summary row's parentUuid is the message it follows.

Error Messages/Logs

Transcript written by the script below, uuids shortened, bookkeeping rows omitted

line  3  user                       uuid=1318e849 parentUuid=-
line  9  assistant                  uuid=a5ef5047 parentUuid=0b91f309
line 10  assistant                  uuid=2e4578cf parentUuid=a5ef5047
line 13  system stop_hook_summary   uuid=0d7a39b1 parentUuid=2e4578cf
line 14  user                       uuid=9f124e88 parentUuid=0d7a39b1
line 15  attachment                 uuid=bea57c1f parentUuid=9f124e88
line 16  assistant                  uuid=adafd410 parentUuid=bea57c1f   <- head of the first boundary
line 17  assistant                  uuid=3056d6fb parentUuid=adafd410
line 22  system stop_hook_summary   uuid=410bf7fa parentUuid=3056d6fb
line 23  system compact_boundary    uuid=456f5f65 logicalParentUuid=410bf7fa preservedSegment{head=adafd410 tail=410bf7fa}
line 24  user (isCompactSummary)    uuid=5b264742 parentUuid=456f5f65
...      two more turns
line 41  assistant                  uuid=2122c90c parentUuid=4d78410b   <- last message before the second /compact
line 42  assistant                  uuid=2503f4e8 parentUuid=2122c90c
line 47  user                       uuid=1318e849 parentUuid=-          <- copy of line 3
line 48  attachment                 uuid=e68f0a35 parentUuid=1318e849   <- copy of line 4
...      copies of lines 5 to 14
line 56  attachment                 uuid=bea57c1f parentUuid=9f124e88   <- copy of line 15, the row before the first boundary's head
line 57  system stop_hook_summary   uuid=cdf60317 parentUuid=bea57c1f   <- new row, parent is the copy above, not 2503f4e8
line 58  system compact_boundary    uuid=334cf755 logicalParentUuid=cdf60317 preservedSegment{head=2122c90c tail=cdf60317}
line 59  user (isCompactSummary)    uuid=ed9a72bc parentUuid=334cf755

Steps to Reproduce

  1. Save the script below as repro.py and run it in an empty directory with claude on PATH. I used the binary the desktop app ships, ~/Library/Application Support/Claude/claude-code/2.1.260/claude.app/Contents/MacOS/claude
  2. It drives one claude -p process through two short turns, /compact, two more turns and a second /compact, then prints every row of the session transcript whose uuid already appeared earlier in the file
  3. Expected output is nothing. Actual output is the ten rows before the first boundary's preservedSegment.headUuid, listed a second time
import json, os, subprocess, time, uuid

session = str(uuid.uuid4())
proc = subprocess.Popen(
    ['claude', '-p', '--input-format', 'stream-json', '--output-format', 'stream-json', '--verbose', '--model', 'haiku', '--session-id', session],
    stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True, bufsize=1)

def send(text, until):
    proc.stdin.write(json.dumps({'type': 'user', 'message': {'role': 'user', 'content': text}}) + '\n')
    proc.stdin.flush()
    for line in proc.stdout:
        message = json.loads(line)
        if message.get('type') in until or message.get('subtype') in until:
            return

for step in ['Reply with one word: alpha', 'Reply with one word: beta', '/compact', 'Reply with one word: gamma', 'Reply with one word: delta', '/compact']:
    send(step, ('result', 'compact_boundary'))
    time.sleep(2)
proc.stdin.close()
proc.wait()

path = os.path.expanduser('~/.claude/projects/' + os.getcwd().replace('/', '-') + '/' + session + '.jsonl')
rows = [json.loads(line) for line in open(path)]
seen = {}
for index, row in enumerate(rows):
    if row.get('uuid') in seen:
        print('line %d duplicates line %d: %s uuid=%s' % (index + 1, seen[row['uuid']] + 1, row['type'], row['uuid'][:8]))
    elif row.get('uuid'):
        seen[row['uuid']] = index
line 47 duplicates line 3: user uuid=1318e849
line 48 duplicates line 4: attachment uuid=e68f0a35
line 49 duplicates line 5: attachment uuid=f405d3dc
line 50 duplicates line 6: attachment uuid=7e8e32e4
line 51 duplicates line 7: attachment uuid=0b91f309
line 52 duplicates line 9: assistant uuid=a5ef5047
line 53 duplicates line 10: assistant uuid=2e4578cf
line 54 duplicates line 13: system uuid=0d7a39b1
line 55 duplicates line 14: user uuid=9f124e88
line 56 duplicates line 15: attachment uuid=bea57c1f

The same happens in a process started with --resume on an existing session, the copy then starts at the boundary the process resumed from. My desktop app transcripts agree: all 21 compactions that were the first in their process are clean, and 13 of the 15 later ones replayed. I could not explain the 2 clean ones.

Claude Model

Not sure / Multiple models. The script uses haiku, my desktop transcripts show it on claude-opus-5, claude-fable-5 and claude-fable-5-1.

Is this a regression?

I don't know

Last Working Version

Unknown, the oldest replay I have is from 2026-08-24 on 2.1.237

Claude Code Version

2.1.260, also seen with 2.1.255, 2.1.247, 2.1.246 and 2.1.237

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

Other. I only use the Claude desktop app (1.46388.2), which drives its bundled CLI headless in stream-json mode, and the script runs that binary the same way. I have never used the interactive terminal.

Additional Information

Every compaction in my transcripts was a manual /compact, I have no auto-compaction data. The copied rows are exactly the process's in-memory history: rows on a rewound branch are not copied, and the copies come in memory order, a compaction's summary before its preserved messages, not file order.

thomasbachem avatar Sep 04 '26 13:09 thomasbachem

Two things from a desktop session hit by this on 2026-09-08, its two compactions two hours and twenty minutes apart.

Rewind stops working session-wide, not just the view. The app resolves a rewind target's resume point against the same walk, so it lands inside the replayed copy and the CLI refuses the turn:

2026-09-08 14:00:23 [info] LocalSessions.rewindV2: sessionId=local_4136cebc… target=3792774e…
2026-09-08 14:00:24 [info] [Rewind] resumeSessionAt=71ab79d3… + forkSession for session local_4136cebc…
2026-09-08 14:00:25 [warn] [CCD CycleHealth] local_4136cebc… cli_execution_error (error_during_execution): No message found with message.uuid of: 71ab79d3…
2026-09-08 14:00:26 [info] [CCD] Clearing stale pendingRewindTo 71ab79d3… — rewind target not on CLI's active chain

The target was a message from 14:00 and 71ab79d3… a row from 10:05:10. Every target in the session fails this way, surfacing as "Claude couldn't process that message".

The truncated view is not only the 166 MB tail-load case. This transcript is 5 MB, well under the 50 MiB threshold, and the scrollback still jumps from before the first compaction straight to the newest messages, because the chain itself skips that stretch: 413 rows off the walk, 278 of them user and assistant turns. I have not tried a relaunch on this one, but the rows are absent from the chain on disk rather than merely unread, so re-reading the file should not help.

Nothing is lost on disk either way. Pointing the second compact_boundary at the last message before it, and the row after it at that boundary's summary, put all 413 rows back on the walk when I tried it on a copy.

I opened #92849 for this before finding this thread, now closed as a duplicate.

thomasbachem avatar Sep 08 '26 13:09 thomasbachem

Correcting the repair I gave in my last comment – it is one field on one row, not two edits.

The boundary the second compaction writes already carries the answer in compactMetadata.preservedSegment: tailUuid is the stop_hook_summary row that got the wrong parent, headUuid the row it should point at. Setting that one parentUuid to headUuid put all 413 rows of the affected session back on the walk, applied to the file this time rather than to a copy. My two-edit version rested on the assumption that a healthy walk ends on a compact_boundary, which is wrong – boundaries sit off the chain as parentless branch roots, and a healthy walk reaches the transcript root without touching one.

That may point at the fix as well – the same turn writes the wrong parent onto the hook row and, moments later, the correct one into the boundary.

thomasbachem avatar Sep 08 '26 16:09 thomasbachem

Re-ran the script from the report on 2.1.271, the binary bundled with the desktop app today. The replay is unchanged. After the second /compact I get 21 re-appended rows, a copy of everything before the first boundary's preservedSegment.headUuid.

The broken chain is gone though. The finished turn's stop_hook_summary is now written before the copies, with the assistant message it follows as its parent, and the new boundary's logicalParentUuid and preservedSegment.tailUuid point at that row:

line 77  assistant                  uuid=631e700d
line 78  system stop_hook_summary   uuid=6b3bed32 parentUuid=631e700d
line 83  user                       uuid=7571fbff parentUuid=-          <- copy of line 3
...      copies of lines 4 to 32
line 104 system compact_boundary    uuid=a1b41ffb logicalParentUuid=6b3bed32 preservedSegment{head=9e3cfa0b tail=6b3bed32}

I see the same in a desktop session on 2.1.270. There I count 216 and 476 re-appended rows from the second and third compaction in one process, and every prompt is still reachable from the newest row. So on current versions only the growth is left. I haven't re-tested Rewind, only checked that the chain is intact.

thomasbachem avatar Sep 18 '26 06:09 thomasbachem

Another trigger for this replay, seen on 2.1.271 and 2.1.275 (Windows 11, Claude desktop app): a subagent's auto-compaction. A second /compact in the main thread is not needed.

The sequence in one session:

  1. The main thread compacts (here with a manual /compact, whose boundary has a preservedSegment).
  2. Later, a background subagent (Agent tool) in the same process reaches its own auto-compaction.
  3. The next message that the main thread records (a user message, a peer message or a task notification) triggers the replay. A copy of the history, from the boundary the process resumed from up to the row just before the main compaction's preservedSegment.headUuid, is appended to the session .jsonl. The new message's parentUuid points at the last copied row.
Main compaction (UTC) Subagent compaction Next main message Rows re-appended
09-16 14:30 /compact 09-16 16:12 16:18 2,292
same 09-16 18:41 18:47 2,292
same 09-16 20:30 21:57 2,292
same 09-17 07:05 07:07 2,292
09-22 04:41 /compact 09-22 09:57 10:04 1,781

Each of the 5 subagent compactions in that session was followed by a replay at the next main message. None of the other 42 messages delivered in the same stretches caused one. The main thread's own auto-compaction on 09-17, the second compaction in that process, added a sixth block, as in the original report.

On this path the broken parent link is still present on 2.1.275, although the stop_hook_summary case was reported fixed on 2.1.271 above. Walking parentUuid back from the newest row leads through the copy into the previous cycle, and skips everything written between the main compaction and the subagent's. The model's context was correct: the usage right after the 09-22 replay matched the post-compaction context. Tools that rebuild the current cycle from the chain get the previous cycle. In this session, 5 of the 10 cycle digests that a plugin hook wrote covered the wrong cycle.

The subagent's compaction hooks themselves carry the parent's identity, which is #91910.

raichominev avatar Sep 22 '26 12:09 raichominev

Summary

On Claude Code 2.1.258, 2.1.280 and 2.1.284, a compaction sometimes appends records to the session JSONL that are already in the file. The copies keep the same uuid and the same original timestamp, and they include earlier compact_boundary records.

  • What gets copied: a block running from an earlier compact_boundary (or the root of the current process's chain) up to the previous compaction.
  • How it grows: within one process, each re-appended block is larger than the one before, so the file grows quadratically with the number of compactions.
  • What resets it: resuming in a new process brings the block back to a small size.
  • Scale: in one long session the transcript reached 1,201.5 MB, of which 637.6 MB (53%) is duplicate records.

Adding data to this issue from Linux/WSL2. This looks like the same defect family (this issue: macOS, 2.1.260 and earlier, "second /compact"). This comment adds:

  • Linux/WSL2;
  • versions 2.1.280 and 2.1.284;
  • occurrences on the very first compaction of a fresh process;
  • auto as well as manual compaction;
  • a per-version rate;
  • a second transcript-bloat source: prompt_snapshot records (section below).

Environment

  • Claude Code: 2.1.258, 2.1.280, 2.1.283 and 2.1.284, read from each record's version field. The 2.1.284 native binary was inspected.
  • OS: Linux on WSL2 (kernel 6.18, microsoft-standard-WSL2).
  • Client: VS Code extension, for the affected session.
  • Model: 1M-token context window.
    • Auto-compaction normally ran at about 967k–986k tokens.
    • From 2026-09-24 13:55Z to 2026-09-29, autoCompactWindow: 500000 was set; compactions then ran at about 466k–571k tokens.
  • Custom hooks are configured.

Observations

One top-level session, 2026-09-19 to 2026-10-02:

  • 373,334 JSONL lines and 126,774 distinct uuids.
  • 134 distinct compactions but 279 compact_boundary records; 145 of the records are re-copies.
  1. The re-appended runs. There are 38 runs of 50 or more consecutive lines whose uuids already appear earlier in the file.
    • Each run ends 1–4 lines before a new compact_boundary.
    • The record right after a run is a stop_hook_summary, a user record, or the compact_boundary itself.
  2. Copies keep their original timestamps. The compact_boundary at line 13,233 is stamped 2026-09-19T21:06:22Z, the same as the original at line 3,779.
  3. Where a run starts and ends. A run starts at an earlier compact_boundary or at the root of the process's chain, not at the session's first record (start lines 45,456, 88,233, 266,190 and 316,659 for successive processes). It ends at the previous compaction.
  4. Size grows steadily within a process (MB per run):
    • 2.1.280, 12 consecutive compactions, 2026-09-22 21:33Z to 2026-09-24 01:18Z: 5.1, 7.7, 9.6, 16.1, 20.2, 26.6, 31.0, 34.4, 38.3, 45.6, 49.1, 54.5. The last run was 14,323 lines.
    • 2.1.284, 2026-10-01 13:49Z to 2026-10-02 02:30Z: 3.9, 9.4, 16.1, 20.3, 24.5, 29.5, 34.2.
    • A new process resets it: 54.5 MB at 2026-09-24 01:18Z, then 5.2 MB at 05:22Z.
  5. It also fires on a fresh process. The session's first compaction (2.1.258, manual, 2026-09-19T21:06:22Z) re-appended 439 lines (0.4 MB).
  6. Both triggers are affected: manual and auto compactions.
  7. Rate by version:
Version Distinct compactions With a re-append run Note
2.1.258 13 12 includes the session's first compaction
2.1.280 26 14 13 of 17 at the ~1M window; 1 of 9 at the 500k window
2.1.283 69 (+23 in a second session) 0 every 2.1.283 compaction ran at the 500k window
2.1.284 26 12 intermittent
Total 134 38

We cannot say 2.1.283 is fixed. Every 2.1.283 compaction ran at the 500k window, and in that same regime 2.1.280 re-appended only 1 time in 9.

Other transcripts:

  • A second 2.1.258 session has 15.7 MB of duplicates.
  • A second 2.1.283 session (23 compactions) has 0.
  • Subagent transcripts have 0 duplicates.

Measured impact

  • File size: 1,201.5 MB in total, of which 637.6 MB (53%) is uuid duplicates.
  • Largest single run: 54.5 MB.
  • One process: the 12 consecutive 2.1.280 runs wrote 338.2 MB of duplicates in about 28 h.
  • prompt_snapshot copies: 21.4 MB of re-copied snapshots, already counted in the 637.6 MB.
  • Not measured:
    • whether re-appended records are loaded into model context on resume;
    • whether they affect billing (asked in #82186, which was closed);
    • whether they slow resume or transcript loading.

Related: prompt_snapshot records (2.1.280 and later)

From 2.1.280 on (2026-09-22 in this session), every compaction writes exactly two prompt_snapshot records: one of about 10.6–10.7 KB and one of about 170–179 KB.

  • In this session: 246 distinct snapshot records totalling 23.4 MB.
    • 122 are under 50 KB and 124 are 170–179 KB.
    • Sizes: min 10,574 B, median 170,647 B, mean 94,925 B, max 178,930 B.
  • Re-copies: a further 228 snapshot records (21.4 MB) are copies made by the re-append defect above.
  • Share of the file: snapshots plus their copies are about 3.7% of the file.
  • Suggestion: write the full snapshot once per process, or as a diff against the last one persisted, rather than about 171 KB on every compaction.

Expected vs actual

  • Expected: a compaction appends only new records (the boundary, the summary, and what follows). Each uuid appears once in the file, and file size grows linearly with session activity.
  • Actual: a compaction sometimes re-appends a block of already-persisted records, keeping their original uuids and timestamps. Within a process the block grows with each compaction.

Suggested fix direction

  1. Writer side: never append a record whose uuid is already persisted in that file. Keep either a per-file set of persisted uuids, or a persisted high-water mark, and write only records after it.
  2. On compaction: append only the new boundary, the summary and new records. Never append the preserved segment again.
  3. prompt_snapshot: write once per process, or as a diff.
  4. Readers (resume, transcript viewer): deduplicate by uuid as a defensive measure (see #66947).

Workarounds

  • Resuming in a new process (in VS Code, reloading the window) resets the block size. In practice: start a new process after a few compactions.
  • Tools that read transcripts should deduplicate by uuid and keep the first occurrence.
  • Detection: at each new compact_boundary, add up the bytes of the records just before it whose timestamp is earlier than the previous distinct boundary's timestamp.
Per-run table (38 runs; no conversation content)
Lines Count MB Copied block starts at line Record after run compact_boundary timestamp Version Trigger preTokens
3339-3777 439 0.4 27 system/stop_hook_summary 2026-09-19T21:06:22.176Z 2.1.258 manual 668,673
5492-8139 2648 4.7 4 user/None 2026-09-19T22:28:33.559Z 2.1.258 manual 583,564
10579-14523 3945 7.9 4 system/stop_hook_summary 2026-09-20T02:12:06.081Z 2.1.258 manual 660,025
16564-22417 5854 11.8 4 system/stop_hook_summary 2026-09-20T13:14:24.539Z 2.1.258 manual 600,169
25845-33272 7428 15.2 4 system/compact_boundary 2026-09-20T20:48:26.665Z 2.1.258 manual 771,931
35223-45454 10232 20.0 4 system/stop_hook_summary 2026-09-20T23:57:58.275Z 2.1.258 manual 562,651
49537-51271 1735 3.7 45456 system/compact_boundary 2026-09-21T16:20:04.412Z 2.1.258 manual 467,005
53106-56033 2928 6.1 45456 system/stop_hook_summary 2026-09-21T19:11:43.827Z 2.1.258 manual 479,621
58810-63122 4313 8.8 45456 system/stop_hook_summary 2026-09-22T09:31:51.664Z 2.1.258 manual 565,756
64737-71199 6463 12.4 45456 user/None 2026-09-22T13:57:12.695Z 2.1.258 manual 533,167
71872-79513 7642 15.0 45456 user/None 2026-09-22T15:02:30.957Z 2.1.258 manual 372,048
80086-88231 8146 16.7 45456 system/stop_hook_summary 2026-09-22T16:36:51.965Z 2.1.258 manual 431,272
90746-92041 1296 5.1 88233 system/compact_boundary 2026-09-22T21:33:52.389Z 2.1.280 manual 432,859
92362-94147 1786 7.7 88233 user/None 2026-09-22T21:48:03.459Z 2.1.280 manual 386,423
96313-98294 1982 9.6 88233 system/compact_boundary 2026-09-22T23:19:36.700Z 2.1.280 manual 930,276
100232-103771 3540 16.1 88233 system/compact_boundary 2026-09-23T00:20:02.053Z 2.1.280 manual 511,192
107616-112576 4961 20.2 88233 system/compact_boundary 2026-09-23T11:21:57.381Z 2.1.280 manual 719,509
114918-122716 7799 26.6 88233 system/compact_boundary 2026-09-23T15:03:20.958Z 2.1.280 manual 518,491
124142-133692 9551 31.0 88233 user/None 2026-09-23T17:10:56.447Z 2.1.280 manual 488,760
134779-145342 10564 34.4 88233 system/compact_boundary 2026-09-23T18:41:46.370Z 2.1.280 manual 605,896
147046-158331 11286 38.3 88233 system/compact_boundary 2026-09-23T21:17:58.505Z 2.1.280 manual 927,824
159132-171679 12548 45.6 88233 system/compact_boundary 2026-09-23T22:21:54.983Z 2.1.280 manual 560,371
173306-186435 13130 49.1 88233 system/compact_boundary 2026-09-23T23:49:38.671Z 2.1.280 manual 731,672
188777-203099 14323 54.5 88233 system/compact_boundary 2026-09-24T01:18:56.972Z 2.1.280 auto 971,573
209969-211278 1310 5.2 205194 system/compact_boundary 2026-09-24T05:22:19.968Z 2.1.280 auto 968,276
219127-220389 1263 3.4 216244 system/compact_boundary 2026-09-24T17:38:44.144Z 2.1.280 auto 467,688
268642-269652 1011 4.4 266190 system/compact_boundary 2026-09-29T21:02:28.431Z 2.1.284 manual 635,244
271848-273561 1714 8.3 266190 system/compact_boundary 2026-09-29T22:43:17.161Z 2.1.284 auto 972,275
276655-277306 652 3.9 273562 system/compact_boundary 2026-09-30T01:01:55.696Z 2.1.284 auto 986,283
293453-294890 1438 4.9 288360 system/compact_boundary 2026-09-30T14:01:14.541Z 2.1.284 auto 967,213
314059-316658 2600 8.9 306902 system/compact_boundary 2026-10-01T05:19:47.801Z 2.1.284 auto 967,230
320271-321193 923 3.9 316659 system/compact_boundary 2026-10-01T13:49:53.807Z 2.1.284 manual 750,583
324158-326843 2686 9.4 316659 system/compact_boundary 2026-10-01T15:50:07.976Z 2.1.284 manual 874,628
328294-333147 4854 16.1 316659 system/compact_boundary 2026-10-01T17:13:15.191Z 2.1.284 manual 606,117
334401-340324 5924 20.3 316659 system/compact_boundary 2026-10-01T19:07:25.836Z 2.1.284 manual 632,134
342021-348861 6841 24.5 316659 system/compact_boundary 2026-10-01T20:47:34.260Z 2.1.284 manual 736,345
350895-358952 8058 29.5 316659 system/compact_boundary 2026-10-01T22:48:00.956Z 2.1.284 manual 596,237
361558-371178 9621 34.2 316659 system/compact_boundary 2026-10-02T02:30:53.731Z 2.1.284 auto 966,907

lumen-maximus avatar Oct 02 '26 11:10 lumen-maximus

Same bug on Windows with the VS Code extension and auto-compaction (no manual /compact), at a much larger scale.

One session, Aug 5 – Oct 2, 2026, 79 auto-compactions, extension 2.1.220 – 2.1.286 (entrypoint claude-vscode):

  • The transcript grew to 2.26 GB (399,147 lines). 243,211 lines (1.3 GB) repeat a uuid already written earlier in the same file; without them the file would be ~0.95 GB.
  • The duplicates come in 58 bursts. 57 of them start by re-writing an earlier compact_boundary row and end right before a new compact_boundary. Bursts grow as the session goes on: on Oct 2 three consecutive bursts were 39, 50 and 60 MB, each replaying the previous 1–2 days.
  • The duplicates look re-serialized from memory rather than copied: message, parentUuid and timestamp match the original in 99.6% of them, but cwd, version and promptId are those of the writing process, and toolUseResult is shortened (e.g. 5,142 → 159 chars). cwd often differs from the original only by drive-letter case (D:\… vs d:\…).

Consequence: one of these bursts pushed the file past 2 GiB, and since then opening the session in VS Code crash-loops the extension host (reported separately).

Count of repeated uuids in a transcript:

import json
seen, dup = set(), 0
for line in open(path, 'rb'):
    u = json.loads(line).get('uuid')
    if u in seen:
        dup += 1
    elif u:
        seen.add(u)
print(dup)

Lotrein avatar Oct 02 '26 22:10 Lotrein

A correction to my comment from 09-18, where I wrote that only the growth is left on current versions. On 2.1.286 the replay also hides turns in the desktop app's conversation view, with the chain intact.

Claude desktop app 2.19675.0 on macOS, one Claude Code process that resumed the session from the previous day's compaction at 11:34, then ran a manual /compact at 12:49 and another at 22:52. The second one re-appended 535 rows before its boundary, the block the report describes, from the boundary the process resumed from up to the 12:49 compaction. The stop_hook_summary sits before the copies with the right parent, as on 2.1.271.

Afterwards the view shows the first turn after the previous day's compaction cut off after its first few tool calls, followed directly by the 22:52 compaction. The rest of that turn, three more turns, the 12:49 compaction and the two turns after it are missing, and scrolling up doesn't bring them back. Every row is still in the file, and the session itself carries on normally.

As far as I can tell, the app reads the newest page of the view from the end of the file and orders it by timestamp without dropping repeated uuids, so the copies land in that page as old messages. I haven't worked out how that produces the cut at that exact point.

thomasbachem avatar Oct 05 '26 21:10 thomasbachem

Still reproducible on 2.1.296 (Windows 11, VS Code extension — it runs the CLI in SDK mode). Adding a root-cause pointer from the bundled JS and a condition analysis over our transcripts with control groups, in case it helps triage.

Mechanism (from the JS bundled in the extension's resources/native-binary/claude.exe, 2.1.296):

  1. recordTranscript appends each message it is given whose uuid is not in the "already written" cache (sessionMessages).
  2. After each compaction the cleanup step (labelled post_compact_cleanup in the bundle) clears that cache with clearSessionMessagesCache, unless the session is the interactive fullscreen TUI. The cache is rebuilt by reading the file, and for files over 5 MiB (5242880) the loader skips everything before the last boundary except its preserved segment. CLAUDE_CODE_DISABLE_PRECOMPACT_SKIP turns that skip off; we have not tested whether it avoids the copy.
  3. In SDK mode an auto-compaction boundary trims the in-memory conversation (applyCompactBoundary), but a manual /compact boundary is only appended — the previous epoch stays in memory.
  4. At the next compaction the whole in-memory conversation goes through recordTranscript again (for an automatic one via flushBeforeCompactBoundary; a manual /compact re-writes it as well). Everything before the last on-disk boundary is unknown to the rebuilt cache, so it is written a second time, right before the new boundary.

Condition, checked against 124 compaction boundaries in our transcripts (2.1.207–2.1.296): a copy appears when the previous compaction was a manual /compact, the same process continued (no resume, no version change) and the file was over 5 MiB — 14 of 15 such pairs. Controls: previous compaction automatic — 0 of 5; a new process in between (resume or version change) — 0 of 81. The rule fits 122 of 124 boundaries; both exceptions are on older versions (2.1.207, and 2.1.235 with a 4.4 MiB file — the size threshold may have differed then). Further manual compactions in the same process make the copy grow (1,504 → 2,751 → 4,266 records), consistent with the quadratic growth reported above.

Comments above report copies with automatic compaction only, on 2.1.220–2.1.286. In our data the 5 pairs where the earlier compaction was automatic (4 auto → auto, 1 auto → manual; one session, 2.1.289–2.1.291) had none, and in the 2.1.296 code an automatic boundary trims the in-memory history while a manual one does not — so the automatic path may have changed after 2.1.286 (the 2.1.296 bundle is the only one we read). The subagent trigger described above is a separate path we did not check.

Scale here: 15 runs of 20+ duplicated records in 5 sessions — 21,407 lines, 7.5% of all transcript lines; in one session about a third of the file.

Effect on hook and tooling authors: anything that reads the transcript without de-duplicating by uuid double-counts. In our own hooks this rolled "last edit" times back by hours (last record wins), inflated per-session counters and added phantom compaction boundaries to replay simulations. Reader-side fix we use: skip a uuid already seen (the first occurrence is the original) and take timestamps as the maximum.

Fix directions, agreeing with the suggestions above: never append a uuid already persisted in the file, or trim the in-memory history at a manual /compact boundary in SDK mode the same way automatic compaction does.

HowardCJT avatar Oct 11 '26 07:10 HowardCJT

Still reproducible on 2.1.309 (Windows 11, VS Code extension, entrypoint claude-vscode). On this version it also breaks resume in the extension without any visible error.

Session B: 525 MB, one process, one day (Oct 24–25)

  • 38,107 rows (0.344 GB, 65.5 % of the file) repeat a uuid that was already written earlier in the file.
  • The file has 77 compact_boundary rows, but only 16 unique ones. All 16 were written by 2.1.309: 15 trigger=manual, 1 auto.
  • Every burst of copies ends right before a new boundary, and the bursts grow. Rows re-appended before each boundary, in order: 0, 813, 1,401, 2,106, 2,831, 0, 551, 1,012, 1,132, 1,857, 3,049, 3,570, 4,434, 4,464, 5,294, 5,593.
  • The copies differ from their originals only by the added promptId and slug fields (in one burst, also the drive-letter case of cwd).

Session A: 11.7 GB after 14 days (Oct 11–25)

  • 1,449,651 rows, 2,428 compact_boundary rows.
  • The rows written after the last boundary come to only 8.2 MB.

Impact: resume from the extension silently starts a fresh session. Opening session A via vscode://anthropic.claude-code/open?session=<id> failed three times in a row:

Spawning Claude with SDK query function - ... version: 2.1.310, ... resume: <id>
Error spawning Claude (on channel ...): Error: Subprocess initialization did not complete within 60000ms. Claude Code did not answer the initialize request and reported no SessionStart or Setup hook running. ...

The webview sends launch_claude with freshStartIfUnresumable: true. After the timeout the user gets an empty conversation and no error message, so the context looks lost.

Workaround that worked

  1. Copy the file starting at the last non-sidechain compact_boundary row. That kept 326 rows, 8.2 MB.
  2. Before swapping the file in, check that every parentUuid in the kept part resolves inside it, and that the isCompactSummary row follows the boundary.

After that, resume on 2.1.310 worked again and has written no duplicate rows so far. It has not compacted since, so I can't yet say whether 2.1.310 still re-appends.

edgar965 avatar Oct 25 '26 11:10 edgar965

Also on Linux (aarch64, Sailfish OS) and macOS 27, CLI 2.1.301 and 2.1.309, headless stream-json mode (--input-format stream-json --output-format stream-json, the SDK's mode) with --resume, manual /compact every 30–90 min. Same bug as #102559.

Three of our long-lived sessions on two machines:

Transcript Size Repeated uuid rows
Linux 3.4 GB, 841k lines, 71,877 distinct uuids 2.76 GB
macOS 4.1 GB 93%
macOS 1.2 GB 89%

Each copy runs from the boundary the process resumed from up to the previous compaction and is written right before the new compact_boundary (21 copies of 44–66 MB in the Linux file). Copies differ from the originals only in promptId, toolUseResult or cwd.

Downstream effect we hit: a reader that pages backwards from the tail and looks for its anchor uuid in the last N MB lands in a copy block (a single copy is larger than the window) and falls back to the resume point, so the conversation view showed only day-old messages.

Workaround that leaves the file alone: restart the CLI process (--resume, same session) once it is idle after each compaction. The first compaction in a process writes no copy, so no process reaches a second one. --fork-session does not help; the fork copies the same way.

Hyp3rSon1X avatar Oct 25 '26 21:10 Hyp3rSon1X

Same bug on 2.1.309, Windows 11, Claude desktop app (Code tab, Claude 4.4212.0.0). Here a full PC restart did not bring the missing turns back.

One session, 313 MB, about 23,400 rows, 5,245 of them repeats in three bursts (1,513, 1,237 and 2,495 rows). Each burst ends right before a new compact_boundary.

The last four manual compactions, UTC:

Boundary Copies written before it
10-31 13:19 none
10-31 20:36 none
10-31 23:54 the 13:19 boundary through to the 20:36 compaction
11-01 02:09 the 13:19 boundary through to the end of the turn before the 23:54 compaction

So the 02:09 burst stops short of the 23:54 compaction and leaves out everything after it.

I shut the PC down 34 seconds after the 02:09 compact. After booting and reopening the session, the app logged:

transcript is 313232068 bytes; tail-loading last 52428800 bytes

The view now runs up to the end of the copied block, then straight into the 02:09 compact. Everything from the 23:54 compact to the 02:09 compact is missing, about two hours: 7 of my prompts and 49 replies. Scrolling doesn't reach it.

The rows themselves are fine. They're all in the file, walking parentUuid back from the 02:09 boundary's logicalParentUuid finds them (760 rows), and the agent's context was correct because its compaction summary covered them. Only the view is wrong.

For anyone who doesn't know to check the file, this looks like the app deleted their conversation.

bloodfl0w avatar Nov 01 '26 10:11 bloodfl0w

Hey I'm with the Claude Code team. Compaction shouldn't write duplicate rows to your transcript anymore, as of 2.1.310. Could you update, start a new session, and let me know if the file still balloons?

franklee-ant avatar Nov 24 '26 00:11 franklee-ant

Thanks Frank. On 2.1.330 (Windows desktop app) it looks fixed. Same long-running session as the report, four compactions since updating, including several in one process: the transcript grew only 4 to 13 MB between them, no re-appended history. The file is still 527 MB from the older versions, but it isn't ballooning any more. I'll confirm again on a fresh session.

bloodfl0w avatar Nov 24 '26 17:11 bloodfl0w

A clean data point, Windows 11, Claude desktop app, Claude Code 2.1.331.

One session, about 26 hours, nine compactions in the order auto, auto, manual, auto, manual, auto, manual, auto, manual. Background subagents ran in it throughout. 1M window.

The first seven ran with CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=50, at 240k to 497k tokens before each. Since an earlier comment here saw re-appends mostly when compaction fires near a full window, I then raised the override to 90 and let the context grow:

  • an automatic compaction at 714,026 tokens
  • a typed /compact at 702,755 tokens
  • an app restart 29 seconds after that typed one

The transcript is now 146.2 MB and 12,573 rows; 3,767 rows carry a uuid and none is repeated. After the restart the chat view kept its turns. Compact, then restart, is the sequence that left a gap in my view on 2.1.309.

So on 2.1.331 I could not reproduce either half (re-appended rows, or chat missing after a restart), under 500k or above 700k. One machine and one session, so a data point, not a proof.

bloodfl0w avatar Nov 27 '26 03:11 bloodfl0w

This was fixed in Claude Code 2.1.310, so we're closing this issue. If you disagree, let us know here and we can re-open it. Thanks for reporting!

bpasero-ant avatar Nov 29 '26 17:11 bpasero-ant