Context Compaction μ»¨ν…μŠ€νŠΈ μ»΄νŒ©μ…˜

Phase-aware summarization and microcompaction for long sessions κΈ΄ μ„Έμ…˜μ„ μœ„ν•œ 단계 인식 μš”μ•½ 및 λ§ˆμ΄ν¬λ‘œμ»΄νŒ©μ…˜

Overview κ°œμš”

Long coding sessions generate large conversation histories that eventually exceed the context window. ROACH PI uses a two-layer approach to context management:

κΈ΄ μ½”λ”© μ„Έμ…˜μ€ κ²°κ΅­ μ»¨ν…μŠ€νŠΈ μœˆλ„μš°λ₯Ό μ΄ˆκ³Όν•˜λŠ” λŒ€ν™” 기둝을 μƒμ„±ν•©λ‹ˆλ‹€. ROACH PIλŠ” μ»¨ν…μŠ€νŠΈ 관리λ₯Ό μœ„ν•΄ 두 계측 μ ‘κ·Ό 방식을 μ‚¬μš©ν•©λ‹ˆλ‹€:

  • Full compaction전체 μ»΄νŒ©μ…˜ β€” Summarizes the entire old conversation into a structured summary, freeing context for new work.전체 이전 λŒ€ν™”λ₯Ό κ΅¬μ‘°ν™”λœ μš”μ•½μœΌλ‘œ μ••μΆ•ν•˜μ—¬ μƒˆ μž‘μ—…μ„ μœ„ν•œ μ»¨ν…μŠ€νŠΈλ₯Ό ν™•λ³΄ν•©λ‹ˆλ‹€.
  • Microcompactionλ§ˆμ΄ν¬λ‘œμ»΄νŒ©μ…˜ β€” Truncates old tool results to short placeholders before full compaction is needed, extending session lifetime.전체 μ»΄νŒ©μ…˜μ΄ ν•„μš”ν•˜κΈ° 전에 였래된 도ꡬ κ²°κ³Όλ₯Ό 짧은 ν”Œλ ˆμ΄μŠ€ν™€λ”λ‘œ 자λ₯΄κ³ , μ„Έμ…˜ 수λͺ…을 μ—°μž₯ν•©λ‹ˆλ‹€.

How Compaction Works μ»΄νŒ©μ…˜ μž‘λ™ 방식

Phase-aware custom compaction takes over during active workflows. When the context window fills up, instead of using the default summarizer, ROACH PI injects a specialized compaction prompt that is tailored to the current workflow phase.

단계 인식 μ»€μŠ€ν…€ μ»΄νŒ©μ…˜μ΄ ν™œμ„± μ›Œν¬ν”Œλ‘œ 쀑에 μž‘λ™ν•©λ‹ˆλ‹€. μ»¨ν…μŠ€νŠΈ μœˆλ„μš°κ°€ 꽉 μ°¨λ©΄ κΈ°λ³Έ μš”μ•½κΈ°λ₯Ό μ‚¬μš©ν•˜λŠ” λŒ€μ‹ , ROACH PIλŠ” ν˜„μž¬ μ›Œν¬ν”Œλ‘œ 단계에 λ§žμΆ€ν™”λœ 특수 μ»΄νŒ©μ…˜ ν”„λ‘¬ν”„νŠΈλ₯Ό μ£Όμž…ν•©λ‹ˆλ‹€.

The prompt starts with a critical directive that prevents the model from making tool calls during compaction:

ν”„λ‘¬ν”„νŠΈλŠ” μ»΄νŒ©μ…˜ 쀑 λͺ¨λΈμ΄ 도ꡬλ₯Ό ν˜ΈμΆœν•˜μ§€ λͺ»ν•˜κ²Œ ν•˜λŠ” μ€‘μš” μ§€μ‹œλ¬ΈμœΌλ‘œ μ‹œμž‘ν•©λ‹ˆλ‹€:

CRITICAL: Respond with TEXT ONLY. Do NOT call any tools. - Do NOT use any tool. Tool calls will be REJECTED and will waste your only turn. - Your entire response must be plain text: an <analysis> block followed by a <summary> block.

The model is required to produce two XML blocks in its response:

λͺ¨λΈμ€ 응닡에 두 개의 XML 블둝을 생성해야 ν•©λ‹ˆλ‹€:

  • <analysis> β€” Chronological thought process analyzing the conversationλŒ€ν™”λ₯Ό λΆ„μ„ν•˜λŠ” 순차적 사고 κ³Όμ •
  • <summary> β€” The structured summary in the prescribed 9-section formatκ·œμ •λœ 9개 μ„Ήμ…˜ ν˜•μ‹μ˜ κ΅¬μ‘°ν™”λœ μš”μ•½

After compaction, the <analysis> block is stripped and only the formatted summary is retained in context.

μ»΄νŒ©μ…˜ ν›„ <analysis> 블둝은 제거되고 ν˜•μ‹ν™”λœ μš”μ•½λ§Œ μ»¨ν…μŠ€νŠΈμ— μœ μ§€λ©λ‹ˆλ‹€.

Summary Format μš”μ•½ ν˜•μ‹

Every compaction summary follows this nine-section structure. The model is given an explicit example template to follow.

λͺ¨λ“  μ»΄νŒ©μ…˜ μš”μ•½μ€ 이 9개 μ„Ήμ…˜ ꡬ쑰λ₯Ό λ”°λ¦…λ‹ˆλ‹€. λͺ¨λΈμ€ λͺ…μ‹œμ μΈ 예제 ν…œν”Œλ¦Ώμ„ μ œκ³΅λ°›μŠ΅λ‹ˆλ‹€.

1. Primary Request and Intent Capture all of the user's explicit requests and intents in detail. 2. Key Technical Concepts - [Concept 1] - [Concept 2] 3. Files and Code Sections - [File Name 1] - [Summary of why this file is important] - [Important Code Snippet] 4. Errors and Fixes - [Error description]: - [How you fixed it] 5. Problem Solving [Description] 6. All User Messages - [Detailed non tool use user message] 7. Pending Tasks - [Task 1] 8. Current Work [Precise description of current work] 9. Optional Next Step [Optional next step to take]

Workflow Phases μ›Œν¬ν”Œλ‘œ 단계

Compaction is phase-aware. When an active workflow is running, the compaction prompt is augmented with phase-specific instructions that tell the model what to preserve.

μ»΄νŒ©μ…˜μ€ 단계λ₯Ό μΈμ‹ν•©λ‹ˆλ‹€. ν™œμ„± μ›Œν¬ν”Œλ‘œκ°€ μ‹€ν–‰ 쀑일 λ•Œ, μ»΄νŒ©μ…˜ ν”„λ‘¬ν”„νŠΈλŠ” λͺ¨λΈμ΄ 보쑴해야 ν•  λ‚΄μš©μ„ μ•Œλ €μ£ΌλŠ” 단계별 μ§€μ‹œλ¬ΈμœΌλ‘œ λ³΄κ°•λ©λ‹ˆλ‹€.

Phase단계 Whenμ‹œμ  Survives Compactionμ»΄νŒ©μ…˜ ν›„ μœ μ§€
idle DefaultκΈ°λ³Έ Yes (no special handling)예 (νŠΉλ³„ν•œ 처리 μ—†μŒ)
clarifying During /clarify/clarify 쀑 Yes (phase preserved)예 (단계 μœ μ§€)
planning During /plan/plan 쀑 Yes (phase + goal preserved)예 (단계 + λͺ©ν‘œ μœ μ§€)
milestoneplanning During /plan --milestones/plan --milestones 쀑 Yes (phase preserved)예 (단계 μœ μ§€)
reviewing During /review/review 쀑 Yes (phase preserved)예 (단계 μœ μ§€)
ultrareviewing During /ultrareview/ultrareview 쀑 Yes (phase preserved)예 (단계 μœ μ§€)

Each non-idle phase injects additional instructions into the compaction prompt, emphasizing what the model must preserve. For example, during planning, the prompt requires the summary to capture task progress, implementation decisions, and the current task state.

각 λΉ„μœ νœ΄(non-idle) λ‹¨κ³„λŠ” μ»΄νŒ©μ…˜ ν”„λ‘¬ν”„νŠΈμ— μΆ”κ°€ μ§€μ‹œλ¬Έμ„ μ£Όμž…ν•˜μ—¬ λͺ¨λΈμ΄ 보쑴해야 ν•  λ‚΄μš©μ„ κ°•μ‘°ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, planning μ€‘μ—λŠ” ν”„λ‘¬ν”„νŠΈκ°€ μš”μ•½μ— μž‘μ—… μ§„ν–‰ 상황, κ΅¬ν˜„ κ²°μ •, ν˜„μž¬ μž‘μ—… μƒνƒœλ₯Ό ν¬ν•¨ν•˜λ„λ‘ μš”κ΅¬ν•©λ‹ˆλ‹€.

Microcompaction λ§ˆμ΄ν¬λ‘œμ»΄νŒ©μ…˜

Microcompaction is a lightweight pre-compaction pass that replaces old tool results with short placeholders. This extends session lifetime by reducing token usage before a full compaction is triggered.

λ§ˆμ΄ν¬λ‘œμ»΄νŒ©μ…˜μ€ 였래된 도ꡬ κ²°κ³Όλ₯Ό 짧은 ν”Œλ ˆμ΄μŠ€ν™€λ”λ‘œ κ΅μ²΄ν•˜λŠ” κ²½λŸ‰ 사전 μ»΄νŒ©μ…˜ νŒ¨μŠ€μž…λ‹ˆλ‹€. 전체 μ»΄νŒ©μ…˜μ΄ 트리거되기 전에 토큰 μ‚¬μš©λŸ‰μ„ 쀄여 μ„Έμ…˜ 수λͺ…을 μ—°μž₯ν•©λ‹ˆλ‹€.

A message is eligible for microcompaction when all of these conditions are met:

λ©”μ‹œμ§€λŠ” λ‹€μŒ λͺ¨λ“  쑰건이 좩쑱될 λ•Œ λ§ˆμ΄ν¬λ‘œμ»΄νŒ©μ…˜ λŒ€μƒμ΄ λ©λ‹ˆλ‹€:

  • role === "toolResult"
  • isError !== true (error results are never compacted)(였λ₯˜ κ²°κ³ΌλŠ” μ ˆλŒ€ μ»΄νŒ©μ…˜λ˜μ§€ μ•ŠμŒ)
  • toolName is in이 PI_TOOL_NAME_SET (known pi tools only)(μ•Œλ €μ§„ pi λ„κ΅¬λ§Œ)
  • Message ageλ©”μ‹œμ§€ λ‚˜μ΄ >= MICROCOMPACT_AGE_MS (1 hour)(1μ‹œκ°„)

When compacted, the tool result content is replaced with:

μ»΄νŒ©μ…˜λ˜λ©΄ 도ꡬ κ²°κ³Ό μ½˜ν…μΈ κ°€ λ‹€μŒμœΌλ‘œ κ΅μ²΄λ©λ‹ˆλ‹€:

[Compacted] <toolName> result

Microcompaction is opt-in. It must be explicitly enabled via environment variable.

λ§ˆμ΄ν¬λ‘œμ»΄νŒ©μ…˜μ€ μ˜΅νŠΈμΈμž…λ‹ˆλ‹€. ν™˜κ²½ λ³€μˆ˜λ₯Ό 톡해 λͺ…μ‹œμ μœΌλ‘œ ν™œμ„±ν™”ν•΄μ•Ό ν•©λ‹ˆλ‹€.

Configurationμ„€μ •

Env Varν™˜κ²½ λ³€μˆ˜ DefaultκΈ°λ³Έκ°’ Descriptionμ„€λͺ…
PI_AGENTIC_MICROCOMPACTION (unset)(λ―Έμ„€μ •) Set to 1 to enable microcompaction1둜 μ„€μ • μ‹œ λ§ˆμ΄ν¬λ‘œμ»΄νŒ©μ…˜ ν™œμ„±ν™”

Plan Execution Interactionκ³„νš μ‹€ν–‰κ³Όμ˜ μƒν˜Έμž‘μš©

During plan execution (plan-compliance, plan-worker, plan-validator), the workflow phase and goal document are critical for continuity. If a compaction occurs mid-execution:

κ³„νš μ‹€ν–‰ 쀑(plan-compliance, plan-worker, plan-validator), μ›Œν¬ν”Œλ‘œ 단계와 λͺ©ν‘œ λ¬Έμ„œλŠ” 연속성에 μ€‘μš”ν•©λ‹ˆλ‹€. μ‹€ν–‰ 쀑 μ»΄νŒ©μ…˜μ΄ λ°œμƒν•˜λ©΄:

  • The current workflow phase is stored in compaction metadata and restored after the summary is generated. ν˜„μž¬ μ›Œν¬ν”Œλ‘œ 단계가 μ»΄νŒ©μ…˜ 메타데이터에 μ €μž₯되고, μš”μ•½ 생성 ν›„ λ³΅μ›λ©λ‹ˆλ‹€.
  • The goal document (if present) is referenced in the compaction prompt so the model anchors the user's intent. λͺ©ν‘œ λ¬Έμ„œ(μžˆλŠ” 경우)κ°€ μ»΄νŒ©μ…˜ ν”„λ‘¬ν”„νŠΈμ— μ°Έμ‘°λ˜μ–΄ λͺ¨λΈμ΄ μ‚¬μš©μžμ˜ μ˜λ„λ₯Ό κ³ μ •ν•©λ‹ˆλ‹€.
  • Phase-specific instructions ensure that task progress, implementation decisions, and current task state survive the compaction. 단계별 μ§€μ‹œλ¬Έμ΄ μž‘μ—… μ§„ν–‰ 상황, κ΅¬ν˜„ κ²°μ •, ν˜„μž¬ μž‘μ—… μƒνƒœκ°€ μ»΄νŒ©μ…˜ 후에도 μœ μ§€λ˜λ„λ‘ 보μž₯ν•©λ‹ˆλ‹€.

This means a long-running plan execution will not lose track of its goal or phase even if multiple compactions occur.

즉, κΈ΄ κ³„νš μ‹€ν–‰ 쀑 μ—¬λŸ¬ μ»΄νŒ©μ…˜μ΄ λ°œμƒν•΄λ„ λͺ©ν‘œλ‚˜ 단계λ₯Ό μžƒμ§€ μ•ŠμŠ΅λ‹ˆλ‹€.