What survives context compaction in a long agent session: Claude Code, Codex and Cursor, and a checkpoint that outlives the summary
A long session gets compacted into a summary, sometimes while you are away from the keyboard. What Claude Code, Codex and Cursor document about what comes back, the hooks each one gives you around compaction, a 15-minute drill with five planted facts, and where to keep the state a summary is allowed to lose.
Three hours into a session, the agent proposes the approach you turned down in the first twenty minutes. Nothing failed and no error was printed. The conversation was compacted on the way, the summary kept the request and the code, and the reason you said no was not in it.
Every coding harness does this, and each one says in its own documentation that the summary loses things. This guide puts side by side what Claude Code, Codex and Cursor document, gives you a drill to see what your own setup keeps, and shows where to put the state that has to outlive the summary.
What compaction replaces
When the context window fills, the harness replaces the conversation so far with a summary and carries on. The vendors are direct about the trade:
- Claude Code: "The summary keeps your requests and key code snippets, but detailed instructions from early in the conversation may be lost."
- Cursor: "the agent's knowledge can degrade after summarization since it's a lossy compression of the context. The agent might have forgotten crucial details about its task."
- Codex:
/compact"replaces earlier turns with a concise summary, freeing context while keeping critical details."
It does not only happen when you ask for it. All three compact automatically near the limit. Claude Code also compacts a conversation you stepped away from: idle compaction is on by default in interactive sessions, once the context has reached 200,000 tokens and about an hour has passed since the last request (the settings reference lists the remaining conditions). The terminal shows a line starting with Compacted while idle, before the prompt cache expired, and that line is easy to miss when you come back from lunch.
What each harness documents
| Claude Code | Codex | Cursor | |
|---|---|---|---|
| Manual | /compact, optionally with a focus: /compact focus on the API changes | /compact | The user can summarize manually |
| Automatic | Near the limit, about 967K tokens on models with a native 1M window; change it with /autocompact 500k or autoCompactWindow. Idle compaction as above | model_auto_compact_token_limit; unset uses the model's default | When the context window fills up |
| Reloaded from disk afterwards | Project-root CLAUDE.md and unscoped rules, auto memory, the plan written in plan mode, up to five files read or edited (most recently modified first; a file over 5,000 tokens comes back as a path only) | Not stated in the pages we read | The agent gets a reference to the chat history file and can search it for details missing from the summary |
| Summarized with everything else | Rules with paths: frontmatter and nested CLAUDE.md files (they reload on demand), and context that hooks added earlier | Earlier turns | The conversation |
| Steering the summary | A "Compact Instructions" section in CLAUDE.md; the stdout of a PreCompact hook is used as instructions for the summary, up to 10,000 characters per hook | compact_prompt in config.toml, described as an inline override for the compaction prompt | Not documented |
| Hook before | PreCompact, matchers manual and auto; it can block the compaction | PreCompact, same matchers; continue: false stops it | preCompact, described as observational: it cannot block or modify compaction |
| Hook after | PostCompact receives the summary in compact_summary. SessionStart hooks that match compact run and their output is added to the new context | PostCompact. SessionStart hooks that match source: "compact" run before the next model request, including when compaction happens mid-turn | None documented. sessionStart runs when a new conversation is created |
One row in the Claude Code column deserves a second read: context that a hook injected earlier in the session is summarized with the rest. A hook that loaded your team's conventions at startup did its job once. After compaction, those conventions are whatever the summary made of them, unless the hook runs again.
The four things that live only in the conversation
Files are on disk, and the instruction file is reloaded. The state at risk is the part that was only ever said in the chat. From the vendors' own wording, and from what a summary of "requests and key code snippets" leaves out, we sort it into four kinds:
- Rejected options, with the reason. "Not the ORM migration, it locks the table for minutes." The summary tends to record what was done, and an option that was dropped left no code behind.
- Verified results, with how they were verified. "The three failures in payments were already there before our change, checked on
main." Without the second half, the next turn either trusts too much or checks again. - Open questions. "We still do not know whether staging has the new column." A summary reads as settled prose, and an unknown that is not restated turns into an assumption.
- Values said in passing. A rate limit, a date, a directory nobody may touch.
This list is our classification, not a measurement. The drill below is how you find out what your harness, your model and your instruction file actually keep.
A 15-minute drill
You do not need to fill the context window. Claude Code documents that the automatic pass works the same way as /compact. For the other two, a manual compaction is the closest thing you can trigger on demand.
- Open a fresh session in a scratch repository and give the agent a small real task, such as adding a function and its test.
- In the first turns, plant five facts, one of each kind, in ordinary sentences:
- a rejected approach and why ("do not use a regex for this, the last one let nested brackets through");
- a number ("that API allows 40 requests per minute");
- an early instruction ("never edit anything under
legacy/"); - a result the agent verified itself (have it run the tests and state what failed before it changed anything);
- an open question ("we do not know yet whether the config file exists in production, do not assume it").
- Do three or four turns of real work so the facts are not the last thing said.
- Compact:
/compactin Claude Code or Codex, a manual summarization in Cursor. - In the same session, ask one question per fact. For the rejected approach, do not ask "what did we reject?". Ask for a proposal on the same problem and see whether the rejected option comes back.
- Score each fact: kept exactly, kept vaguely, lost, or contradicted.
In Claude Code you can read what the summarizer wrote instead of inferring it. A PostCompact hook receives the summary on stdin:
{
"hooks": {
"PostCompact": [
{
"hooks": [
{ "type": "command", "command": "jq -r .compact_summary >> ~/compact-summaries.log" }
]
}
]
}
}Then run the drill again with one protection at a time from the next section, and compare the scores.
Three places to keep what has to survive
1. The instruction file, for rules. CLAUDE.md at the project root comes back from disk after compaction in Claude Code, so a standing rule belongs there and not in the chat. The same file can steer the summary:
## Compact Instructions
When compacting, keep: every approach we rejected and the reason, every
number or limit the user stated, every open question, and the single next step.This covers rules, and it does not cover state. The instruction file should not change every twenty minutes, and it is shared by everyone who opens the repository. Where values that change should live is in CLAUDE.md, AGENTS.md and .cursor/rules in one repo.
2. A hook that runs after compaction. Claude Code and Codex both run SessionStart hooks again with the compact source. Claude Code's own example re-injects a reminder:
{
"hooks": {
"SessionStart": [
{
"matcher": "compact",
"hooks": [
{ "type": "command", "command": "echo 'Reminder: use Bun, not npm. Run bun test before committing. Current sprint: auth refactor.'" }
]
}
]
}
}In Codex the matcher goes in config.toml:
[[hooks.SessionStart]]
matcher = "^compact$"
[[hooks.SessionStart.hooks]]
type = "command"
command = '/usr/bin/python3 "$(git rev-parse --show-toplevel)/.codex/hooks/session_start.py"'A hook can only print something that already exists somewhere. A fixed echo brings back a rule. To bring back the state of the work, the hook has to read it from a place that was written before the compaction.
3. A checkpoint outside the session. This is the piece that covers the four kinds above, and it has to be written during the work, because nothing tells the agent that the next turn will start from a summary. A checkpoint that does its job has:
- the outcome, what verified it, and how far that verification reaches;
- each open question next to the cheapest test that would settle it;
- one next action;
- the options that were rejected, with the reason.
A file in the repository works for one person on one machine. It stops working when a second agent, a second harness or a teammate has to pick the work up, which is the case covered in Handing work from one coding agent to the next.
The same checkpoint with ValorBrain
ValorBrain keeps this state in task_state, a ledger on the server. The agent writes to it through MCP while it works:
{ "action": "ledger_open",
"description": "Guide on what survives context compaction. Done = PR open with green CI, reviewed, page returns 200.",
"next": "write both .mdx files and open the PR" }
{ "action": "ledger_checkpoint",
"what": "Research closed: what the three vendors document about compaction and its hooks.",
"verified_by": "vendor docs downloaded on Nov 28, 2026, and the public connect manifest",
"coverage": "documentation and published code only; no real compaction was run" }
{ "action": "ledger_open_question",
"question": "After a compaction, does the session-start hook bring the ledger back into context?",
"settled_by": "run /compact in a real session with connect installed and read what the hook injects" }The server refuses a checkpoint that does not say what verified it ("A checkpoint without a record is not a checkpoint") and a question with no settled_by ("an open question with nothing that would settle it cannot be closed"). We sent both while writing this page and both came back as errors. A checkpoint whose verification states no reach is refused as well: without a coverage field, verified_by has to carry a status code, a measurement or a counted set ("verified without stated coverage is a mood, not a result"). The form asks for the two halves a summary drops. This is what task_state with action: "read" returned for the ledger we kept while writing this page (our entries are in Portuguese; translated, shortened and wrapped here):
# Task State Ledger
## Goal
Guide on what survives context compaction. Done = PR open with green CI, reviewed, page returns 200.
## Verified
- ✓01 Research closed: what the three vendors document about compaction and its hooks.
— verified by: vendor docs downloaded on Nov 28, 2026, and the public connect manifest
(coverage: documentation and published code only; no real compaction was run)
## Open
- ?01 After a compaction, does the session-start hook bring the ledger back into context?
— settled by: run /compact in a real session with connect installed and read what the hook injects
## Next
write both .mdx files and open the PRReading it back is one call. memory_prepare returns the task state together with identity, what changed and the relevant memory, so the rule "call memory_prepare when you are unsure where the work stands" brings the ledger back in any harness that has the MCP server connected, compacted or not. Decisions that outlive the task ("we rejected the ORM migration because it locks the table") go in memory_store with type: "decision", where the next session and the next teammate find them too.
Not depending on the model to remember. @valorbrain/connect installs two hooks that matter here. We read them in version 0.13.0, the one the public manifest served on November 28, 2026.
- The session-start hook is installed with an empty matcher in Claude Code and Codex. Both harnesses document that an empty matcher matches every source, and
compactis one of the sources, so the hook also runs after a compaction and sendssourcein its request. The server answerscompactthe same way it answers a new session, and that answer includes the caller's own opentask_stateledger. In Cursor the hook runs when a conversation is created, and Cursor documents no hook that adds context after a compaction. - The stop hook asks the agent for a short checkpoint every few turns: by default after at least 3 turns, or 2 turns adding up to 8 minutes of work, at least 10 minutes apart and at most 6 times per session. If the agent already wrote memory since the last checkpoint, the server skips the request and the count starts over. The text handed to the model asks for one call per durable item: a decision, root cause or lesson to
memory_storeorrecord_lesson, progress totask_state, work for someone else toteam_handoff. Turn it off with--checkpoint offorVALORBRAIN_CHECKPOINT=off.
What those hooks send is listed in What leaves your machine.
What ValorBrain does not do
- It does not stop, delay or rewrite compaction. The summary is the harness's. The hosted connect installs no
PreCompacthook, so nothing is extracted at the moment of compaction. What exists afterwards is what was written before. - It does not write the checkpoint for the agent. The stop hook hands a request to the harness's own model, which decides what to record. If the model records nothing, nothing is saved, and after six checkpoints in a session the hook stops asking.
- It does not restore the conversation. The ledger holds outcomes, questions and a next step. The exact wording of turn 12 stays in the harness's own transcript. Cursor is the one that documents handing the agent a reference to it after a summary.
- In Cursor, connect installs only the session-start and stop hooks, and Cursor documents no hook after compaction. There, the read after a summary depends on the rules file telling the agent to call
memory_prepare. - We did not run a compaction for this page. Everything about harness behavior is what each vendor's documentation said on the date below. The server half of the open question in our ledger above is settled in the code: the answer to a session start with
source: compactcarries one ledger, the one named after the harness session if it exists, else the credential's default ledger, which is where atask_statecall withoutsessionwrites. The recall has a 3-second budget, and a session start that misses it is answered with no context. Whether the harness puts that answer in front of the model after its summary is the vendor's half, and the drill is how you check it on your setup.
Checklist
- You know your harness's automatic threshold, and whether it compacts while you are away.
- You ran the drill once with no protection and wrote down which of the five facts came back.
- Standing rules are in the root instruction file, not in the chat or in a path-scoped rule.
- If your harness supports it, a hook runs after compaction and reads the state from somewhere outside the session.
- Rejected options, verified results, open questions and the next step are written during the work, with what verified each result.
- You ran the drill again and at least the rejected approach no longer comes back.
Sources, read on November 28, 2026: Claude Code, Explore the context window, How Claude Code works, Hooks reference, hooks guide, model configuration and settings reference, idleCompaction; Codex, commands, hooks and configuration reference; Cursor, Hooks and Dynamic context discovery (January 6, 2026). The connect hooks are from the public manifest, GET https://valorbrain-api.valor.digital/setup/artifacts?agent=claude-code&hooks=2, and from the @valorbrain/connect 0.13.0 package on npm.
Next: Handing work from one coding agent to the next for the note that crosses sessions and harnesses, CLAUDE.md, AGENTS.md and .cursor/rules in one repo for the rules that reload from disk, and What a memory MCP server costs before the first question for what the memory server itself adds to the window.
What a memory MCP server costs before the first question: measure tools/list, then decide what loads
A memory server is connected in every session of every agent, so its tool definitions are a fixed cost on all of them. An 89-line script that measures it on any MCP server, what Claude Code, Codex and Cursor document about loading tools, the catch that deferral creates for a server the agent has to call first, and ValorBrain's own numbers.
Subagents and worktrees: what parallel coding agents share, and where a decision has to live so every worker sees it
A subagent starts with a clean context and hands back a summary. A worktree isolates files and nothing else. What Claude Code, Codex and Cursor document about what a parallel worker starts with, what comes back and which hooks run when one starts, a 20-minute drill with four planted facts, and where to keep the decisions that have to reach every worker.