Commit Graph

9 Commits

  • refactor(coding-agent): fix compaction for branched sessions, consolidate hook context types
    Compaction API:
    - prepareCompaction() now takes (pathEntries, settings) only
    - CompactionPreparation restructured: removed cutPoint/messagesToKeep/boundaryStart, added turnPrefixMessages/isSplitTurn/previousSummary/fileOps/settings
    - compact() now takes (preparation, model, apiKey, customInstructions?, signal?)
    - Fixed token overflow by using getPath() instead of getEntries()
    
    Hook types:
    - HookEventContext renamed to HookContext
    - HookCommandContext removed, RegisteredCommand.handler takes (args, ctx)
    - HookContext now includes model field
    - SessionBeforeCompactEvent: removed previousCompactions/model, added branchEntries
    - SessionBeforeTreeEvent: removed model (use ctx.model)
    - HookRunner.initialize() added for modes to set up callbacks
  • Extract shared compaction/branch-summarization utils
    - New utils.ts with shared functions:
      - FileOperations type and createFileOps()
      - extractFileOpsFromMessage()
      - computeFileLists()
      - formatFileOperations()
      - serializeConversation()
      - SUMMARIZATION_SYSTEM_PROMPT
    
    - branch-summarization.ts now uses:
      - Serialization approach (conversation as text, not LLM messages)
      - completeSimple with system prompt
      - Shared utility functions
  • Use convertToLlm before serializing, include thinking, remove truncation
    - serializeConversation now takes Message[] (after convertToLlm)
    - Handles all custom message types via convertToLlm
    - Includes thinking blocks as [Assistant thinking]
    - Removes truncation of tool args and results (already token-budgeted)
  • Serialize conversation to text for summarization
    Instead of passing conversation as LLM messages (which makes the model
    try to continue it), serialize to text wrapped in <conversation> tags.
    
    - serializeConversation() formats messages as [User]/[Assistant]/[Tool result]
    - Tool calls shown as function(args) format
    - Tool results truncated to prevent bloat
    - Conversation wrapped in <conversation> tags in the prompt
  • Add system prompt to compaction summarization
    - SUMMARIZATION_SYSTEM_PROMPT explains the task clearly
    - Tells model to NOT continue conversation, ONLY output summary
    - Updated prompts to reference 'messages above' for clarity
    - Pass systemPrompt to completeSimple
  • Add file tracking and iterative summary merging to compaction
    - CompactionDetails type with readFiles/modifiedFiles
    - extractFileOperations collects from tool calls and previous compaction details
    - UPDATE_SUMMARIZATION_PROMPT for merging with previous summary
    - generateSummary now accepts previousSummary for iterative updates
    - compact() extracts files, passes previousSummary, returns details
    - Only merges from !fromHook compaction entries (backward compatible)
  • Use structured output format for compaction and branch summarization
    Both now use consistent sections:
    - Goal
    - Constraints & Preferences
    - Progress (Done/In Progress/Blocked)
    - Key Decisions
    - Next Steps
    - Critical Context (compaction only)
    
    Prompts instruct LLM to use EXACT format and preserve file paths/function names.
  • Refactor: move compaction code to src/core/compaction/
    - Move compaction.ts to src/core/compaction/compaction.ts
    - Extract branch summarization to src/core/compaction/branch-summarization.ts
    - Add index.ts to re-export all compaction utilities
    - Update all imports across the codebase