diff --git a/README.md b/README.md index a6d0781a4..c08b8556a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # CoreCoder -**The nanoGPT of coding agents. A 1.4k-line engine inside 3,276 readable lines of pure Python: understand how a coding agent actually works, then fork your own.** +**The nanoGPT of coding agents. A 1.4k-line engine inside 3,307 readable lines of pure Python: understand how a coding agent actually works, then fork your own.** *learn from it · fork it · ship something better* @@ -12,7 +12,7 @@ [![Python](https://img.shields.io/badge/python-3.10+-blue)](https://python.org) [![License: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE) [![Tests](https://github.com/he-yufeng/CoreCoder/actions/workflows/ci.yml/badge.svg)](https://github.com/he-yufeng/CoreCoder/actions) -[![engine](https://img.shields.io/badge/engine-1483_LoC-blue)](article/00-index_EN.md) +[![engine](https://img.shields.io/badge/engine-1511_LoC-blue)](article/00-index_EN.md) [![essays](https://img.shields.io/badge/source--reading-8_bilingual-orange)](article/00-index_EN.md) @@ -25,7 +25,7 @@ | | CoreCoder | Claude Code | aider | nanoGPT | |---|---|---|---|---| -| Lines of code | ~1,483 engine / 3,276 total | hundreds of thousands (closed) | tens of thousands of Python | ~600 (two files) | +| Lines of code | ~1,511 engine / 3,307 total | hundreds of thousands (closed) | tens of thousands of Python | ~600 (two files) | | Time to read it all | one afternoon | can't (closed) | a few days of slogging | one afternoon | | Breakpoint, change, rerun? | yes, every line | no | yes, but there's a lot | yes | | What it's for | understand one, then fork your own | production coding assistant | terminal pair-programming | minimal GPT for teaching | @@ -36,9 +36,9 @@ The nanoGPT column is there as a reference point: minimal, readable, but it teac I've always felt coding agents get talked about as if they were arcane. Strip a tool like Claude Code or Cursor all the way down and the core is a `while` loop wrapped around a large model, plus seven or eight tools that let it actually do things. The hard part was never the loop; it's everything the loop has to cope with once it meets the real world. CoreCoder is the minimal version that writes that core out honestly. -The engine (loop, model interface, context, tools, sessions) is 1,483 lines once you drop blank lines and comments. Counting the outer CLI, config and packaging too, the whole package is 26 files: 3,276 physical lines, 2,703 net, every one short enough to read in a single sitting. The growth since the original 1,161-line snapshot went into visible features: plan mode, hooks and checkpoints, each documented below. +The engine (loop, model interface, context, tools, sessions) is 1,511 lines once you drop blank lines and comments. Counting the outer CLI, config and packaging too, the whole package is 26 files: 3,307 physical lines, 2,731 net, every one short enough to read in a single sitting. The growth since the original 1,161-line snapshot went into visible features: plan mode, hooks and checkpoints, each documented below. -And it really runs: reads and writes files, executes shell, spawns sub-agents, compacts context in three tiers, and tells you the tokens and dollars a run burned whenever you ask. Anything that would mutate your disk or run a command stops for your consent first. The suite has 353 offline tests and an opt-in live smoke test. But the point of it running isn't to become your daily driver. It runs so the walkthrough can't lie: a reference that shows how an agent works has to actually work. +And it really runs: reads and writes files, executes shell, spawns sub-agents, compacts context in three tiers, and tells you the tokens and dollars a run burned whenever you ask. Anything that would mutate your disk or run a command stops for your consent first. The suite has 356 offline tests and an opt-in live smoke test. But the point of it running isn't to become your daily driver. It runs so the walkthrough can't lie: a reference that shows how an agent works has to actually work. The code came out of a public teardown: open analyses have already exposed a lot of the load-bearing architecture inside production agents like Claude Code. I took the most essential layer and rewrote it honestly, in as little code as I could. So reading CoreCoder is roughly like reading a runnable, annotated take on how that kind of agent works, except it's only a minimal reimplementation, sitting right there on your machine for you to take apart and change. @@ -145,7 +145,7 @@ Three decisions are worth a closer look, because they're the kind of call you ca **`edit_file` does search-and-replace on a unique match, not line numbers.** Line numbers are a trap: the model only has to miscount by one and it quietly edits the wrong place. Anchor on a unique snippet of the original instead. If there's no match, it hands the start of the file back so the model can re-anchor; if there are several matches, it makes the model bring more surrounding context rather than gamble on one. On a successful edit it returns a diff. Recoverable on failure, verifiable on success: the whole loop stays inside the tool. -**Context isn't cut all at once when it's full; it gives ground in three tiers, cheapest first.** At half full (50%) it trims over-long tool outputs in place, a tier that's purely mechanical and costs no model call. If 70% still isn't enough, it has the model summarize the older turns into a single paragraph while keeping the most recent ones verbatim. Only at 90% does it hit the emergency tier and pull everything, summary and recent turns alike, down to its tightest form. Blunt truncation tends to throw away exactly the early decision a long task leans on most; tiering lets it surrender the least important things first instead of lopping off the oldest decisions wholesale from the start. +**Context isn't cut all at once when it's full; it gives ground in three tiers, cheapest first.** At half full (50%) it trims over-long tool outputs in place, a tier that's purely mechanical and costs no model call. If 70% still isn't enough, it has the model summarize the older turns into a single paragraph while keeping the most recent ones verbatim. Only at 90% does it hit the emergency tier and pull everything, summary and recent turns alike, down to its tightest form. Blunt truncation tends to throw away exactly the early decision a long task leans on most; tiering lets it surrender the least important things first instead of lopping off the oldest decisions wholesale from the start. Each pass also summarizes only the turns that arrived since the last one and carries the earlier summary forward untouched, so a long session does not slowly grind an early fact into nothing through repeated summaries of summaries. **You constrain a sub-agent by withholding the tool, not by writing rules and hoping it obeys.** A spawned sub-agent gets an isolated context and its own separate history, with a toolset exactly one item shorter than the parent's: the `agent` tool itself, so it can't recursively spawn more sub-agents. Handing it one fewer tool is cleaner than legislating a rule after the fact. It also reuses the parent's model connection (its spend folded into the same running total), truncates its output once it runs past 5,000 characters down to just the opening, and runs on a shorter round limit than the parent. The same restraint, end to end. diff --git a/README_CN.md b/README_CN.md index 69829ce6c..29115e506 100644 --- a/README_CN.md +++ b/README_CN.md @@ -2,7 +2,7 @@ # CoreCoder -**编程 agent 里的 nanoGPT。1.4k 行引擎、整包 3276 行纯 Python 全部一口气可读,读懂一个 coding agent 到底怎么运作,再 fork 出你自己的。** +**编程 agent 里的 nanoGPT。1.4k 行引擎、整包 3307 行纯 Python 全部一口气可读,读懂一个 coding agent 到底怎么运作,再 fork 出你自己的。** *learn from it · fork it · ship something better* @@ -12,7 +12,7 @@ [![Python](https://img.shields.io/badge/python-3.10+-blue)](https://python.org) [![License: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE) [![Tests](https://github.com/he-yufeng/CoreCoder/actions/workflows/ci.yml/badge.svg)](https://github.com/he-yufeng/CoreCoder/actions) -[![engine](https://img.shields.io/badge/engine-1483_LoC-blue)](article/) +[![engine](https://img.shields.io/badge/engine-1511_LoC-blue)](article/) [![源码导读](https://img.shields.io/badge/源码导读-8篇双语-orange)](article/) @@ -25,7 +25,7 @@ | | CoreCoder | Claude Code | aider | nanoGPT | |---|---|---|---|---| -| 代码量 | 引擎约 1483 行 / 整包 3276 行 | 几十万行(闭源) | 数万行 Python | 约 600 行(两个文件) | +| 代码量 | 引擎约 1511 行 / 整包 3307 行 | 几十万行(闭源) | 数万行 Python | 约 600 行(两个文件) | | 读完要多久 | 一个下午 | 读不了(闭源) | 得啃几天 | 一个下午 | | 能不能下断点改了再跑 | 能,每一行 | 不能 | 能,但量大 | 能 | | 定位 | 读懂并 fork 出你自己的 agent | 生产级编程助手 | 终端结对编程 | 教学用最小 GPT | @@ -36,9 +36,9 @@ nanoGPT 那一列是拿来对照的:它最小、可读,但教的是训一个 我一直觉得 coding agent 被讲得太玄了。把 Claude Code、Cursor 这类工具扒到底,核心是一个 while 循环套着一个大模型,外加七八个让它能真正动手的工具。难的从来不是这个循环,而是循环跑进真实世界以后要兜的那些底。CoreCoder 就是把这个核心老老实实写出来的最小版本。 -引擎部分(循环、模型接口、上下文、工具、会话)去掉空行和注释是 1483 行。连最外层的 CLI、配置、打包一起算,整个包 26 个文件、物理 3276 行、净 2703 行,每个文件都短到能一口气读完。自 1161 行快照之后的增长都花在了看得见的功能上:plan mode、hooks、checkpoints,下文各有交代。 +引擎部分(循环、模型接口、上下文、工具、会话)去掉空行和注释是 1511 行。连最外层的 CLI、配置、打包一起算,整个包 26 个文件、物理 3307 行、净 2731 行,每个文件都短到能一口气读完。自 1161 行快照之后的增长都花在了看得见的功能上:plan mode、hooks、checkpoints,下文各有交代。 -它真能跑:读写文件、执行 shell、派子 agent、分三层压上下文,还能随时把这趟烧掉的 token 和美元数报给你。任何要动你磁盘、要跑命令的调用,都会先停下来等你点头。现在有 353 个离线测试,另有按需开启的真实 API 冒烟。但能跑不是为了劝你拿去日用,而是为了让这份「注释」不撒谎:一个解释 agent 怎么运作的范例,自己得真能运作。 +它真能跑:读写文件、执行 shell、派子 agent、分三层压上下文,还能随时把这趟烧掉的 token 和美元数报给你。任何要动你磁盘、要跑命令的调用,都会先停下来等你点头。现在有 356 个离线测试,另有按需开启的真实 API 冒烟。但能跑不是为了劝你拿去日用,而是为了让这份「注释」不撒谎:一个解释 agent 怎么运作的范例,自己得真能运作。 代码来自一次公开拆解。公开的源码分析里,Claude Code 这类生产级 agent 暴露出不少关键架构,我挑出最核心的一层,用尽量少的代码诚实地复写了一遍。所以读 CoreCoder,约等于读一份基于公开源码分析的「可运行注释版」:讲的是这类 agent 的核心思路,而它本身只是最小复写,就摆在你机器上,随你拆、随你改。 @@ -145,7 +145,7 @@ def chat(self, user_input): **`edit_file` 用唯一匹配的搜索替换,不靠行号。** 行号这东西,模型只要数偏一行,就会悄悄改错地方;锚定一段唯一的原文:匹配不到,就把文件开头甩回去让模型照着重新锚定;匹配到多处,就让它多带几行上下文再来,而不是赌一个。改成功了,连一段 diff 一起返回。失败能复位、成功能复核,闭环都收在工具自己手里。 -**上下文不是满了才一刀切,而是按代价从轻到重分三档退让。** 先在半满(50%)时把超长的工具输出就地截短,这一档纯机械、不花一次模型调用;到 70% 还压不下去,就把较早的轮次交给模型总结成一段摘要,最近几轮原文原样留着;逼到 90% 才进应急档,连摘要带最近几轮一起收到最紧。粗暴截断往往恰好丢掉一个长任务最依赖的早期决定;分层退让,是让它按重要性从低到高一档档地让,而不是一上来就把最老的决定整段切掉。 +**上下文不是满了才一刀切,而是按代价从轻到重分三档退让。** 先在半满(50%)时把超长的工具输出就地截短,这一档纯机械、不花一次模型调用;到 70% 还压不下去,就把较早的轮次交给模型总结成一段摘要,最近几轮原文原样留着;逼到 90% 才进应急档,连摘要带最近几轮一起收到最紧。粗暴截断往往恰好丢掉一个长任务最依赖的早期决定;分层退让,是让它按重要性从低到高一档档地让,而不是一上来就把最老的决定整段切掉。每一档还只总结上次之后新长出来的轮次,先前那段摘要原样带过去,长会话才不会靠一遍遍「摘要的摘要」把早期的事实慢慢磨没。 **约束子 agent 能干什么,靠的是不给它那把工具,而不是写一堆规则求它听话。** 派出去的子 agent 拿到的是隔离的上下文、自己独立的一份历史,工具集只比父 agent 少一样:`agent` 工具本身,于是它没法再往下递归派子 agent。少给一件工具,比事后立一条规矩干净得多。它还复用父 agent 同一个模型连接(花销一并算进总账),输出一过 5000 字就截短、只留开头一段,轮次上限也压得比父 agent 更短。同一套克制,从头贯到尾。 diff --git a/article/00-index.md b/article/00-index.md index 1987dc02c..84b13814c 100644 --- a/article/00-index.md +++ b/article/00-index.md @@ -8,7 +8,7 @@ 因为 Claude Code 本体太大了。它的代码量在几十万行的量级,光一个负责跑 shell 命令的 BashTool 就上千行,真要逐行读完,多数人会在第三个文件就关掉编辑器。可 agent 的核心其实没那么复杂,复杂的是工程化之后那些为真实世界兜底的边角:模型半路被打断怎么办、上下文塞满了怎么办、十个工具调用同时回来怎么办、provider 偶尔抽风返回 500 又怎么办。这些东西摊在几十万行里,你很难一眼看到骨架。 -CoreCoder 做的事,是把这套骨架压到一千四百行出头的纯 Python。准确说,agent 引擎(循环、模型接口、上下文、工具、会话)去掉空行和注释是 1483 行;连最外层的 CLI 终端、配置和打包都算上,整个包去掉空行和注释 2703 行,物理代码 3276 行。每个文件都短到能一口气读完,跑起来又确实是个能改代码、能跑命令、能自己开子任务、能压缩上下文、能算钱的 agent。它不是玩具。它是把生产级 agent 的每个设计决策,拣出最核心的那版,用最少的代码诚实地写一遍。 +CoreCoder 做的事,是把这套骨架压到一千四百行出头的纯 Python。准确说,agent 引擎(循环、模型接口、上下文、工具、会话)去掉空行和注释是 1511 行;连最外层的 CLI 终端、配置和打包都算上,整个包去掉空行和注释 2731 行,物理代码 3307 行。每个文件都短到能一口气读完,跑起来又确实是个能改代码、能跑命令、能自己开子任务、能压缩上下文、能算钱的 agent。它不是玩具。它是把生产级 agent 的每个设计决策,拣出最核心的那版,用最少的代码诚实地写一遍。 读它,约等于读一份 Claude Code 的「可运行注释版」。区别在于,CoreCoder 的每一行你都能在自己机器上断点、改、跑、看它出什么效果。本系列里我引用的每一个行数、每一段代码,都是从仓库里现读现核的,不是凭印象。这点我后面会反复较真,因为编码 agent 这个领域,太多文章在凭感觉编数字。 @@ -25,7 +25,7 @@ CoreCoder 做的事,是把这套骨架压到一千四百行出头的纯 Python 1. [一个 agent 的本体,是一个 while 循环](01-the-loop.md)。整个 agent 最核心的东西,是一个「问模型、跑工具、把结果喂回去、再问」的循环。我们看 CoreCoder 的 `agent.py`(240 行)怎么把它写明白,以及打断、轮次上限、半截工具调用回填这些真实世界的麻烦各自怎么收场。 2. [工具系统:让模型安全地动手](02-tools.md)。模型本身只会吐字,是工具让它能读文件、写文件、跑命令。这篇讲 CoreCoder 的七个工具,重点是那个看似平平无奇、实则是 Claude Code 关键创新的「唯一性搜索替换」编辑,以及 bash 的安全闸。v0.6.0 之后还补了一节「门之外」:plan 模式、hooks、MCP 这三个进阶件怎么挂在一次调用的前后。末尾教你写第一个自己的工具。 3. [接入任意大模型,顺便把钱算清楚](03-llm-and-cost.md)。`llm.py`(364 行,引擎里最大的文件)怎么用一套 OpenAI 兼容接口接住 DeepSeek、Qwen、Kimi、本地 Ollama,怎么做指数退避重试,怎么在流式输出里顺手把 token 和美元成本统计出来。 -4. [用有限的窗口扛住一个长任务](04-context.md)。上下文窗口是 agent 的硬约束。`context.py`(220 行)实现了三层压缩,从轻到重。这篇还会讲一个特别容易踩、API 一定报错的坑:孤儿 tool 消息。这是我做这个项目时真改过的 bug。 +4. [用有限的窗口扛住一个长任务](04-context.md)。上下文窗口是 agent 的硬约束。`context.py`(251 行)实现了三层压缩,从轻到重。这篇还会讲一个特别容易踩、API 一定报错的坑:孤儿 tool 消息。这是我做这个项目时真改过的 bug。 5. [并行执行与子 agent](05-parallel-and-subagents.md)。模型一次返回多个工具调用时,CoreCoder 用线程池并发跑。这篇老实讲这个简化版相对 Claude Code 的流式执行器差在哪,并发又会引入什么新麻烦,以及子 agent 为什么不准递归。 6. [把它跑成一个真正的命令行工具](06-session-and-cli.md)。会话存盘、断点续聊、斜杠命令、一次性模式。`session.py` 里有个不起眼但很要命的安全细节:怎么防住用恶意会话名做路径穿越。 7. [Fork CoreCoder,搭一个你自己的 coding agent](07-build-your-own.md)。收尾的实操篇。从 clone 到换成你常用的模型,到加一个真正有用的自定义工具,到改系统提示词调教它的风格,到打包发布。读完前六篇你已经懂了原理,这篇让你真有一个东西。 diff --git a/article/00-index_EN.md b/article/00-index_EN.md index a9569d8c6..0313acd8b 100644 --- a/article/00-index_EN.md +++ b/article/00-index_EN.md @@ -8,7 +8,7 @@ First, use CoreCoder, an open source project whose core runs to just over fourte Because Claude Code itself is too big. Its codebase runs into the hundreds of thousands of lines, and the BashTool alone, the part that runs shell commands, is over a thousand. Read it line by line and most people close the editor by the third file. Yet the core of an agent isn't really that complicated. What's complicated is everything engineered around it to survive the real world: what to do when the model gets interrupted mid-stream, when the context window fills up, when ten tool calls come back at once, when a provider hiccups and returns a 500. Spread across hundreds of thousands of lines, that skeleton is hard to see. -What CoreCoder does is squeeze the skeleton down to just over fourteen hundred lines of pure Python. To be precise, the agent engine (the loop, model interface, context, tools, session) is 1,483 lines once you drop blank lines and comments; add the outermost CLI terminal, config, and packaging and the whole package is 2,703 lines without blanks and comments, 3,276 physical. Every file is short enough to read in one sitting, and what runs is genuinely an agent: it edits code, runs commands, spins up its own subtasks, compresses context, tracks cost. It is not a toy. It takes every design decision a production agent makes, picks the most essential version of each, and writes it out honestly in the least code it can. +What CoreCoder does is squeeze the skeleton down to just over fourteen hundred lines of pure Python. To be precise, the agent engine (the loop, model interface, context, tools, session) is 1,511 lines once you drop blank lines and comments; add the outermost CLI terminal, config, and packaging and the whole package is 2,731 lines without blanks and comments, 3,307 physical. Every file is short enough to read in one sitting, and what runs is genuinely an agent: it edits code, runs commands, spins up its own subtasks, compresses context, tracks cost. It is not a toy. It takes every design decision a production agent makes, picks the most essential version of each, and writes it out honestly in the least code it can. Reading it is close to reading a runnable, annotated edition of Claude Code. The difference is that every line of CoreCoder is something you can breakpoint, change, run, and watch on your own machine. Every line count and every snippet I quote in this series is read straight out of the repository, not recalled from memory. I'll keep being pedantic about that, because in the coding-agent space far too many write-ups just make the numbers up. diff --git a/article/04-context.md b/article/04-context.md index 6f2af6ec9..ff9c4f144 100644 --- a/article/04-context.md +++ b/article/04-context.md @@ -4,7 +4,7 @@ agent 有一个绕不过去的物理约束:上下文窗口就那么大。 而编码任务偏偏极其能产 token。模型读一个一千行的文件,那一千行连同行号全进了历史;跑一次测试,几百行输出全进了历史;grep 一下,几十个匹配全进了历史。一个稍微像样的任务转上十几轮,几万 token 就没了。窗口一旦塞满,要么 API 报错,要么你得砍历史,而砍历史砍不好,agent 就会「忘事」,前面读过的文件转头又读一遍,刚做过的决定又推翻重来。 -所以怎么在有限窗口里装下一个长任务,是 agent 工程里最硬核的子问题之一。这一篇看 `corecoder/context.py`(220 行)怎么解。 +所以怎么在有限窗口里装下一个长任务,是 agent 工程里最硬核的子问题之一。这一篇看 `corecoder/context.py`(251 行)怎么解。 ## 分层,从轻到重 diff --git a/corecoder/context.py b/corecoder/context.py index 0dde876ba..0608e9959 100644 --- a/corecoder/context.py +++ b/corecoder/context.py @@ -115,6 +115,29 @@ def _safe_split(messages: list[dict], keep_recent: int) -> int: split -= 1 return split + _SUMMARY_MARKER = "[Context compressed - conversation summary]" + _COLLAPSE_MARKER = "[Hard context reset]" + + @classmethod + def _split_prior_summary(cls, messages: list[dict]) -> tuple[str, list[dict]]: + """Separate a summary this manager wrote from the turns after it. + + Re-summarizing a summary loses detail a little at a time: after a few + compressions an early fact is gone even though every step looked + reasonable. Carry the earlier summary forward verbatim and summarize + only the turns that arrived since. + """ + if len(messages) >= 2 and messages[0].get("role") == "user": + head = messages[0].get("content") or "" + for marker in (cls._SUMMARY_MARKER, cls._COLLAPSE_MARKER): + if head.startswith(marker): + prior = head[len(marker):].lstrip("\n") + rest = messages[1:] + if rest and rest[0].get("role") == "assistant": + rest = rest[1:] + return prior, rest + return "", messages + def _summarize_old(self, messages: list[dict], llm: LLM | None, keep_recent: int = 8) -> bool: """Layer 2: Summarize old conversation, keep recent messages intact.""" @@ -125,12 +148,17 @@ def _summarize_old(self, messages: list[dict], llm: LLM | None, old = messages[:split] tail = messages[split:] - summary = self._get_summary(old, llm) + prior, fresh = self._split_prior_summary(old) + if prior and not fresh: + return False + summary = self._get_summary(fresh, llm) + if prior: + summary = f"{prior}\n{summary}" messages.clear() messages.append({ "role": "user", - "content": f"[Context compressed - conversation summary]\n{summary}", + "content": f"{self._SUMMARY_MARKER}\n{summary}", }) messages.append({ "role": "assistant", @@ -143,12 +171,15 @@ def _hard_collapse(self, messages: list[dict], llm: LLM | None): """Layer 3: Emergency compression. Keep only last 4 messages + summary.""" split = self._safe_split(messages, 4 if len(messages) > 4 else 2) tail = messages[split:] - summary = self._get_summary(messages[:split], llm) + prior, fresh = self._split_prior_summary(messages[:split]) + summary = self._get_summary(fresh, llm) if fresh else "" + if prior: + summary = f"{prior}\n{summary}" if summary else prior messages.clear() messages.append({ "role": "user", - "content": f"[Hard context reset]\n{summary}", + "content": f"{self._COLLAPSE_MARKER}\n{summary}", }) messages.append({ "role": "assistant", diff --git a/tests/test_context_rolling_summary.py b/tests/test_context_rolling_summary.py new file mode 100644 index 000000000..9eb282fdc --- /dev/null +++ b/tests/test_context_rolling_summary.py @@ -0,0 +1,81 @@ +"""Rolling summary: compress the delta, not the summary of a summary.""" + +from corecoder.context import ContextManager + + +class RecordingLLM: + """Summarizer that records its payload and returns a marked summary.""" + + def __init__(self): + self.payloads: list[str] = [] + + def chat(self, messages, **kw): + self.payloads.append(messages[-1]["content"]) + index = len(self.payloads) + + class Response: + content = f"summary#{index}" + + return Response() + + +def _chatty_rounds(messages: list[dict], tag: str, count: int = 4) -> None: + for i in range(count): + messages.append({"role": "user", "content": f"{tag}-{i} " + "c" * 300}) + messages.append({"role": "assistant", "content": f"{tag}-r{i} " + "d" * 300}) + + +def _seed() -> list[dict]: + messages = [{"role": "user", "content": "the deploy key lives in /etc/app/deploy.key"}] + for i in range(12): + messages.append({"role": "user", "content": f"task {i} " + "a" * 300}) + messages.append({"role": "assistant", "content": f"done {i} " + "b" * 300}) + return messages + + +def test_second_compression_summarizes_only_the_new_turns(): + ctx = ContextManager(max_tokens=2000) + llm = RecordingLLM() + messages = _seed() + + ctx.maybe_compress(messages, llm) + _chatty_rounds(messages, "second") + ctx.maybe_compress(messages, llm) + + assert len(llm.payloads) == 2 + assert "task 0" in llm.payloads[0] + # The second pass must not re-summarize the summary the first one wrote. + assert "[Context compressed" not in llm.payloads[1] + assert "summary#1" not in llm.payloads[1] + # It summarizes the turns that aged out of the kept tail since then. + assert "task 8" in llm.payloads[1] + assert "second-3" not in llm.payloads[1] + + +def test_prior_summary_is_carried_forward_verbatim(): + ctx = ContextManager(max_tokens=2000) + llm = RecordingLLM() + messages = _seed() + + ctx.maybe_compress(messages, llm) + _chatty_rounds(messages, "second") + ctx.maybe_compress(messages, llm) + _chatty_rounds(messages, "third") + ctx.maybe_compress(messages, llm) + + head = messages[0]["content"] + assert "summary#1" in head + assert "summary#2" in head + assert "summary#3" in head + + +def test_hard_collapse_also_preserves_the_earlier_summary(): + ctx = ContextManager(max_tokens=2000) + llm = RecordingLLM() + messages = _seed() + ctx.maybe_compress(messages, llm) + _chatty_rounds(messages, "second", count=10) + + ctx._hard_collapse(messages, llm) + + assert "summary#1" in messages[0]["content"]