跳到正文Skip to content
Agent J Beta
菜单Menu
中文EN
登录Sign in

搜索文档和页面Search docs and pages

文档Docs

压缩与交接:上下文满了也不失忆Compaction and handoff — a full context without forgetting

压缩前 Agent 先把原话、进度、决定和路径写进一份交接文件;压缩或清空之后,不用你再发消息,Agent J 会提醒它先读交接再接着干。Before a compaction the Agent writes your words, progress, decisions and paths to a handoff file; afterwards Agent J reminds it to read that file and carry on — you don't have to send anything.

和 Agent 聊得久了,上下文会满。压缩(/compact)把前面的对话变成一段摘要,腾出空间;摘要会丢细节:确切的数字、文件路径、你否掉的方案、试过不行的做法。所以 Agent J 的做法是:压缩前先写交接文件,压缩后先读它。 磁盘上的文件不会被压缩。

交接文件在哪

路径固定,名字是人能看懂的,在手机上可以直接复制:

情况文件
共享模式(你自己电脑上的 Claude Code)压缩前~/.local/state/agentj/handoff/compact.md
共享模式 /clear 前~/.local/state/agentj/handoff/clear.md(只用一次,交回给 Agent 后改名为 clear-used.md)
Agent J 自己的主对话<工作目录>/.agentj/handover/compact.md

路径里不会出现会话 ID 之类的随机串:手机会把像密钥的长串打码,你就复制不到了。

切换 AI 编程工具(/switch-harness)也用同一套交接:离开的那个先写 switch.md(独立会话在 <工作目录>/.agentj/handover/,共享会话在 ~/.local/state/agentj/handoff/),接上的那个先读再接着干,见 换 AI 编程工具。

压缩之前

  • 你说要压缩、点 /compact,或者在菜单里选压缩:交接文件不存在、或者比这一轮对话旧,Agent J 先让 Agent 按 agentj-handoff 技能写好交接,再压缩。
  • 你告诉 Agent 上下文过半了、先写个交接:它自己写,写完把路径单独放进代码块,方便你复制。
  • 交接里写:你的原话、已完成 / 进行中 / 下一步、定下的决定、绝对路径、踩过的坑。不写任何密钥、token、密码。
  • 在电脑上直接敲 /compact,或者 Claude Code 自己自动压缩时,Agent J 来不及先写;它会记下来,压缩后告诉 Agent「本次没有交接文件」。
  • 手机上的 /compact、/clear 会等电脑上这一轮说完、确认框处理完再执行,但不等后台任务:电脑上挂着后台命令或后台子代理时,手机会告诉你电脑上有几个后台任务在跑、前台已经空闲,几秒后照常压缩,后台任务继续跑。真等了 30 分钟还没轮到,手机会说明是哪一轮没结束、还是哪个确认框没处理,命令不会发出去。

压缩或清空之后

以前,压缩完要等你再发一条消息,Agent 才想起去读交接;你不发,它就停在那里,看起来像失忆了。

现在不用等:压缩完 1–2 秒内如果没有新消息,Agent J 自己给会话发一条〔Agent J 系统提示〕:「压缩完成。先读交接文件 …,再继续刚才的任务。」手机上只显示一行「压缩好了,我在翻交接笔记,马上接上思路」,Agent 读完交接后的回复照常发到手机。这条提示由电脑上的 Agent J 发出,不算你的消息,也不给 Agent 任何新权限。

/clear 之后同理:如果有 clear.md,Agent J 提醒 Agent 先读它;没有就什么都不发,新对话从零开始。

如果 Claude Code 是在一个回合中途自动压缩的,压缩完它会自己接着干,Agent J 不插话。

复制给主 Agent:

上下文过半或我说要压缩时,先按 agentj-handoff 技能写好交接文件(我的原话、进度、决定、绝对路径、坑、下一步,不写凭据),把路径单独放进代码块发我;压缩之后第一件事读它,再接着干。

定时任务正在运行时,手机上的 /compact 或 /clear 会显示任务名,并说明命令尚未发送。发起命令的遥控器可以点「取消」;取消后,任务完成也不会再执行这条命令。命令开始执行后,取消入口会收起。共享 Claude 会话若还在等桌面空闲,仍可取消尚未发送的控制。

Long conversations fill the context. Compaction (/compact) replaces the earlier conversation with a summary to make room, and a summary loses detail: exact numbers, file paths, ideas you rejected, attempts that failed. Agent J's rule: write a handoff file before compacting, read it first afterwards. A file on disk is never compacted.

Where the handoff lives

Fixed, human-readable paths you can copy straight from the phone:

SituationFile
Shared mode (your own Claude Code) before a compaction~/.local/state/agentj/handoff/compact.md
Shared mode before /clear~/.local/state/agentj/handoff/clear.md (used once; renamed to clear-used.md when handed back)
Agent J's own main conversation<work folder>/.agentj/handover/compact.md

No session id or other random string appears in these paths: the phone masks key-like strings, and you could not copy the path any more.

Switching the AI coding tool (/switch-harness) uses the same handoff: the tool leaving writes switch.md first (<work folder>/.agentj/handover/ for an independent session, ~/.local/state/agentj/handoff/ for a shared one), and the one taking over reads it, then carries on — see Switch AI coding tool.

Before compacting

  • You say "compact", tap /compact, or pick it in the menu: if the handoff is missing or older than the current turn, Agent J first has the Agent write it with the agentj-handoff skill, then compacts.
  • You tell the Agent the context is past half and ask for a handoff: it writes it and puts the path alone in a code block so you can copy it.
  • The handoff holds your own words, done / in progress / next, decisions, absolute paths and pitfalls. Never a key, token or password.
  • When you type /compact on the computer, or Claude Code compacts by itself, there is no time to write it first; Agent J records that and tells the Agent afterwards that there was no handoff this time.
  • /compact and /clear from the phone wait for the computer's current turn to end and for any dialog to be answered, but not for background work: with a background command or a background sub-agent running, the phone tells you how many background tasks are running and that the foreground is idle, and the command goes in a few seconds while the background work carries on. If it still could not go after 30 minutes, the phone says whether a turn never ended or a dialog was never answered, and the command is not sent.

After a compaction or /clear

Before, the Agent only remembered its handoff when your next message arrived; if you sent nothing, it just sat there and looked like it had forgotten everything.

Now nothing waits on you: if no new message comes within a second or two of the compaction, Agent J sends the session one [Agent J system note]: "Compaction finished. Read the handoff … first, then carry on with the task you were doing." The phone shows one line — "Compacted. I'm reading my handover notes to pick up the thread." — and Agent J's reply after reading the handoff reaches the phone as usual. The note comes from Agent J on your computer; it is not one of your messages and grants the Agent no new permission.

After /clear the same: with a clear.md, Agent J reminds the Agent to read it; without one nothing is sent and the new conversation starts from zero.

If Claude Code compacted by itself in the middle of a turn, it carries on by itself and Agent J stays out of the way.

Copy to your main Agent:

When the context is past half or I say compact, first write the handoff with the agentj-handoff skill (my own words, progress, decisions, absolute paths, pitfalls, next step; no credentials) and send me the path alone in a code block; after the compaction read it first, then carry on.

While a scheduled task is running, /compact and /clear show its name and say that the command has not been sent. The remote that requested it can cancel the wait; the command then stays cancelled after the task finishes. Once command execution begins, withdrawal closes. Shared Claude keeps its existing withdrawal option while waiting for desktop idle, until native control is sent.

这一页的 Markdown 原文This page as Markdown: zh.md · en.md