#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompactionKind {
Regular,
MidLoop,
Emergency,
PostTool,
Manual,
}
pub fn build_continuation(kind: CompactionKind, silent: bool, auto_approve: bool) -> String {
let mut text = if silent {
silent_body(kind).to_string()
} else {
fun_body(kind).to_string()
};
text.push_str(
"\n\nSESSION RECOVERY: Call `plan` with operation=\"start\" (no args) to \
re-surface the in-progress task and update the TUI plan widget. If \
this session has an active plan, this will show exactly where you \
left off (task details, progress count). Continue executing from \
there. If the task involves coding, load CODE.md for coding standards \
before editing files.",
);
if !auto_approve {
text.push_str(
"\n\nCRITICAL: Tool approval is REQUIRED. You MUST wait for user \
approval before EVERY tool execution. Do NOT batch tool calls \
without approval.",
);
}
text
}
fn fun_body(kind: CompactionKind) -> &'static str {
match kind {
CompactionKind::Regular => {
"[SYSTEM: Context was auto-compacted. The summary above includes a snapshot \
of recent messages before compaction.\n\
POST-COMPACTION PROTOCOL (follow in order):\n\
1. Read the compaction summary and the recent message snapshot to understand \
the current task, tools in use, and what you were doing.\n\
2. If the summary references older context you don't have in the snapshot, use \
`session_search` with specific keywords to find those messages. Example: if the \
summary mentions \"vision fallback investigation\", run session_search with that \
query to recover the details.\n\
3. If you need specific brain context, selectively load ONLY the relevant \
brain file (e.g. TOOLS.md, SOUL.md, USER.md). NEVER use name=\"all\".\n\
4. IMMEDIATELY continue the task described in the \"IMMEDIATE TASK\" section \
of the compaction summary. This is NOT optional — you MUST pick up exactly \
where you left off. Do NOT start a new topic. Do NOT ask what to do next. \
Do NOT deviate to unrelated work. If the IMMEDIATE TASK section says \
\"CONTINUE: fixing X\", then fix X.]"
}
CompactionKind::MidLoop => {
"[SYSTEM: Context was auto-compacted mid-loop. The summary above includes \
a snapshot of recent messages. POST-COMPACTION PROTOCOL:\n\
1. Review the summary and snapshot to understand current task state.\n\
2. Use `session_search` with keywords from the summary if you need older \
context not in the snapshot.\n\
3. IMMEDIATELY continue the task described in the \"IMMEDIATE TASK\" section \
of the compaction summary. This is NOT optional — you MUST pick up exactly \
where you left off. Do NOT start a new topic. Do NOT ask what to do next. \
Do NOT deviate to unrelated work.]"
}
CompactionKind::Emergency => {
"[SYSTEM: Emergency compaction — provider rejected the prompt as \
too large. Context has been compacted. POST-COMPACTION PROTOCOL:\n\
1. Review the summary to understand where you left off.\n\
2. Use `session_search` with keywords if you need older context.\n\
3. Briefly acknowledge the compaction with a fun/cheeky remark, \
then resume the task. Do NOT repeat completed work.]"
}
CompactionKind::PostTool => {
"[SYSTEM: Mid-loop context compaction complete. The summary above has \
full context of everything done so far. POST-COMPACTION PROTOCOL:\n\
1. Review the summary to understand current task state.\n\
2. Use `session_search` with keywords if you need older context.\n\
Briefly acknowledge the compaction to the user with a fun/cheeky remark (be \
creative, surprise them — cursing allowed), then IMMEDIATELY continue the task \
described in the \"IMMEDIATE TASK\" section of the compaction summary. \
Do NOT start a new topic. Do NOT deviate to unrelated work. \
Do NOT re-do completed work.]"
}
CompactionKind::Manual => {
"[SYSTEM: Context was manually compacted. The summary above is your memory of \
everything before this point. Resume the IMMEDIATE TASK from the summary. \
Do NOT acknowledge the compaction to the user — they already know.]"
}
}
}
fn silent_body(kind: CompactionKind) -> &'static str {
match kind {
CompactionKind::Regular => {
"[SYSTEM: Context was auto-compacted. The summary above includes a snapshot \
of recent messages before compaction.\n\n\
Silently continue the IMMEDIATE TASK from the summary as if nothing happened. \
Do NOT announce, narrate, apologise for, or acknowledge the compaction. The \
user does not know it happened and does not need to. Any file you were about \
to read or edit can simply be re-read on the next tool call — that's normal, \
not worth narrating.\n\n\
Use `session_search` with specific keywords if the snapshot lacks older context. \
Selectively load brain files (TOOLS.md / SOUL.md / USER.md) only if the task \
needs them — never name=\"all\".\n\n\
FALLBACK: only if the summary genuinely doesn't make the next step clear, send \
ONE short cheeky line (\"brain refresh — what was I working on?\") and ask. \
Default behaviour is silent continuation.]"
}
CompactionKind::MidLoop => {
"[SYSTEM: Context was auto-compacted mid-loop. The summary above includes \
a snapshot of recent messages.\n\n\
Silently continue the IMMEDIATE TASK exactly where you left off. Do NOT \
announce or narrate the compaction. Any file you were mid-edit on can be \
silently re-read on the next tool call — that's normal, not worth \
mentioning. Do NOT restart, ask what to do, or deviate.\n\n\
Use `session_search` with keywords from the summary if older context is \
missing from the snapshot.]"
}
CompactionKind::Emergency => {
"[SYSTEM: Emergency compaction — the prompt was too large and got \
compacted. The summary above contains the recovered state.\n\n\
Silently resume from the IMMEDIATE TASK in the summary. Do NOT \
acknowledge or apologise for the compaction — the user does not \
know it happened. Re-read any file you were mid-edit on without \
narrating it. Do NOT repeat completed work.\n\n\
Use `session_search` with specific keywords if older context is \
missing.\n\n\
FALLBACK: only if the next step is genuinely unclear from the \
summary, send ONE short cheeky line (\"brain refresh — what was \
I doing?\") and ask. Default behaviour is silent continuation.]"
}
CompactionKind::PostTool => {
"[SYSTEM: Mid-loop context compaction complete. The summary above has \
full context of everything done so far.\n\n\
Silently continue the IMMEDIATE TASK from the summary. Do NOT announce \
or narrate the compaction. Do NOT start a new topic. Do NOT deviate to \
unrelated work. Do NOT re-do completed work.\n\n\
Use `session_search` with keywords if you need older context.]"
}
CompactionKind::Manual => {
"[SYSTEM: Context was manually compacted. Silently resume the IMMEDIATE \
TASK from the summary. Do not acknowledge the compaction — the user \
triggered it intentionally and already sees the confirmation.]"
}
}
}