/ src / services / compact / prompt.ts
prompt.ts
  1  import { feature } from 'bun:bundle'
  2  import type { PartialCompactDirection } from '../../types/message.js'
  3  
  4  // Dead code elimination: conditional import for proactive mode
  5  /* eslint-disable @typescript-eslint/no-require-imports */
  6  const proactiveModule =
  7    feature('PROACTIVE') || feature('KAIROS')
  8      ? (require('../../proactive/index.js') as typeof import('../../proactive/index.js'))
  9      : null
 10  /* eslint-enable @typescript-eslint/no-require-imports */
 11  
 12  // Aggressive no-tools preamble. The cache-sharing fork path inherits the
 13  // parent's full tool set (required for cache-key match), and on Sonnet 4.6+
 14  // adaptive-thinking models the model sometimes attempts a tool call despite
 15  // the weaker trailer instruction. With maxTurns: 1, a denied tool call means
 16  // no text output → falls through to the streaming fallback (2.79% on 4.6 vs
 17  // 0.01% on 4.5). Putting this FIRST and making it explicit about rejection
 18  // consequences prevents the wasted turn.
 19  const NO_TOOLS_PREAMBLE = `CRITICAL: Respond with TEXT ONLY. Do NOT call any tools.
 20  
 21  - Do NOT use Read, Bash, Grep, Glob, Edit, Write, or ANY other tool.
 22  - You already have all the context you need in the conversation above.
 23  - Tool calls will be REJECTED and will waste your only turn — you will fail the task.
 24  - Your entire response must be plain text: an <analysis> block followed by a <summary> block.
 25  
 26  `
 27  
 28  // Two variants: BASE scopes to "the conversation", PARTIAL scopes to "the
 29  // recent messages". The <analysis> block is a drafting scratchpad that
 30  // formatCompactSummary() strips before the summary reaches context.
 31  const DETAILED_ANALYSIS_INSTRUCTION_BASE = `Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
 32  
 33  1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
 34     - The user's explicit requests and intents
 35     - Your approach to addressing the user's requests
 36     - Key decisions, technical concepts and code patterns
 37     - Specific details like:
 38       - file names
 39       - full code snippets
 40       - function signatures
 41       - file edits
 42     - Errors that you ran into and how you fixed them
 43     - Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
 44  2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.`
 45  
 46  const DETAILED_ANALYSIS_INSTRUCTION_PARTIAL = `Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
 47  
 48  1. Analyze the recent messages chronologically. For each section thoroughly identify:
 49     - The user's explicit requests and intents
 50     - Your approach to addressing the user's requests
 51     - Key decisions, technical concepts and code patterns
 52     - Specific details like:
 53       - file names
 54       - full code snippets
 55       - function signatures
 56       - file edits
 57     - Errors that you ran into and how you fixed them
 58     - Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
 59  2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.`
 60  
 61  const BASE_COMPACT_PROMPT = `Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
 62  This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.
 63  
 64  ${DETAILED_ANALYSIS_INSTRUCTION_BASE}
 65  
 66  Your summary should include the following sections:
 67  
 68  1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
 69  2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
 70  3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important.
 71  4. Errors and fixes: List all errors that you ran into, and how you fixed them. Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
 72  5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
 73  6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent.
 74  7. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
 75  8. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.
 76  9. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's most recent explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests or really old requests that were already completed without confirming with the user first.
 77                         If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.
 78  
 79  Here's an example of how your output should be structured:
 80  
 81  <example>
 82  <analysis>
 83  [Your thought process, ensuring all points are covered thoroughly and accurately]
 84  </analysis>
 85  
 86  <summary>
 87  1. Primary Request and Intent:
 88     [Detailed description]
 89  
 90  2. Key Technical Concepts:
 91     - [Concept 1]
 92     - [Concept 2]
 93     - [...]
 94  
 95  3. Files and Code Sections:
 96     - [File Name 1]
 97        - [Summary of why this file is important]
 98        - [Summary of the changes made to this file, if any]
 99        - [Important Code Snippet]
100     - [File Name 2]
101        - [Important Code Snippet]
102     - [...]
103  
104  4. Errors and fixes:
105      - [Detailed description of error 1]:
106        - [How you fixed the error]
107        - [User feedback on the error if any]
108      - [...]
109  
110  5. Problem Solving:
111     [Description of solved problems and ongoing troubleshooting]
112  
113  6. All user messages: 
114      - [Detailed non tool use user message]
115      - [...]
116  
117  7. Pending Tasks:
118     - [Task 1]
119     - [Task 2]
120     - [...]
121  
122  8. Current Work:
123     [Precise description of current work]
124  
125  9. Optional Next Step:
126     [Optional Next step to take]
127  
128  </summary>
129  </example>
130  
131  Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response. 
132  
133  There may be additional summarization instructions provided in the included context. If so, remember to follow these instructions when creating the above summary. Examples of instructions include:
134  <example>
135  ## Compact Instructions
136  When summarizing the conversation focus on typescript code changes and also remember the mistakes you made and how you fixed them.
137  </example>
138  
139  <example>
140  # Summary instructions
141  When you are using compact - please focus on test output and code changes. Include file reads verbatim.
142  </example>
143  `
144  
145  const PARTIAL_COMPACT_PROMPT = `Your task is to create a detailed summary of the RECENT portion of the conversation — the messages that follow earlier retained context. The earlier messages are being kept intact and do NOT need to be summarized. Focus your summary on what was discussed, learned, and accomplished in the recent messages only.
146  
147  ${DETAILED_ANALYSIS_INSTRUCTION_PARTIAL}
148  
149  Your summary should include the following sections:
150  
151  1. Primary Request and Intent: Capture the user's explicit requests and intents from the recent messages
152  2. Key Technical Concepts: List important technical concepts, technologies, and frameworks discussed recently.
153  3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Include full code snippets where applicable and include a summary of why this file read or edit is important.
154  4. Errors and fixes: List errors encountered and how they were fixed.
155  5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
156  6. All user messages: List ALL user messages from the recent portion that are not tool results.
157  7. Pending Tasks: Outline any pending tasks from the recent messages.
158  8. Current Work: Describe precisely what was being worked on immediately before this summary request.
159  9. Optional Next Step: List the next step related to the most recent work. Include direct quotes from the most recent conversation.
160  
161  Here's an example of how your output should be structured:
162  
163  <example>
164  <analysis>
165  [Your thought process, ensuring all points are covered thoroughly and accurately]
166  </analysis>
167  
168  <summary>
169  1. Primary Request and Intent:
170     [Detailed description]
171  
172  2. Key Technical Concepts:
173     - [Concept 1]
174     - [Concept 2]
175  
176  3. Files and Code Sections:
177     - [File Name 1]
178        - [Summary of why this file is important]
179        - [Important Code Snippet]
180  
181  4. Errors and fixes:
182      - [Error description]:
183        - [How you fixed it]
184  
185  5. Problem Solving:
186     [Description]
187  
188  6. All user messages:
189      - [Detailed non tool use user message]
190  
191  7. Pending Tasks:
192     - [Task 1]
193  
194  8. Current Work:
195     [Precise description of current work]
196  
197  9. Optional Next Step:
198     [Optional Next step to take]
199  
200  </summary>
201  </example>
202  
203  Please provide your summary based on the RECENT messages only (after the retained earlier context), following this structure and ensuring precision and thoroughness in your response.
204  `
205  
206  // 'up_to': model sees only the summarized prefix (cache hit). Summary will
207  // precede kept recent messages, hence "Context for Continuing Work" section.
208  const PARTIAL_COMPACT_UP_TO_PROMPT = `Your task is to create a detailed summary of this conversation. This summary will be placed at the start of a continuing session; newer messages that build on this context will follow after your summary (you do not see them here). Summarize thoroughly so that someone reading only your summary and then the newer messages can fully understand what happened and continue the work.
209  
210  ${DETAILED_ANALYSIS_INSTRUCTION_BASE}
211  
212  Your summary should include the following sections:
213  
214  1. Primary Request and Intent: Capture the user's explicit requests and intents in detail
215  2. Key Technical Concepts: List important technical concepts, technologies, and frameworks discussed.
216  3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Include full code snippets where applicable and include a summary of why this file read or edit is important.
217  4. Errors and fixes: List errors encountered and how they were fixed.
218  5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
219  6. All user messages: List ALL user messages that are not tool results.
220  7. Pending Tasks: Outline any pending tasks.
221  8. Work Completed: Describe what was accomplished by the end of this portion.
222  9. Context for Continuing Work: Summarize any context, decisions, or state that would be needed to understand and continue the work in subsequent messages.
223  
224  Here's an example of how your output should be structured:
225  
226  <example>
227  <analysis>
228  [Your thought process, ensuring all points are covered thoroughly and accurately]
229  </analysis>
230  
231  <summary>
232  1. Primary Request and Intent:
233     [Detailed description]
234  
235  2. Key Technical Concepts:
236     - [Concept 1]
237     - [Concept 2]
238  
239  3. Files and Code Sections:
240     - [File Name 1]
241        - [Summary of why this file is important]
242        - [Important Code Snippet]
243  
244  4. Errors and fixes:
245      - [Error description]:
246        - [How you fixed it]
247  
248  5. Problem Solving:
249     [Description]
250  
251  6. All user messages:
252      - [Detailed non tool use user message]
253  
254  7. Pending Tasks:
255     - [Task 1]
256  
257  8. Work Completed:
258     [Description of what was accomplished]
259  
260  9. Context for Continuing Work:
261     [Key context, decisions, or state needed to continue the work]
262  
263  </summary>
264  </example>
265  
266  Please provide your summary following this structure, ensuring precision and thoroughness in your response.
267  `
268  
269  const NO_TOOLS_TRAILER =
270    '\n\nREMINDER: Do NOT call any tools. Respond with plain text only — ' +
271    'an <analysis> block followed by a <summary> block. ' +
272    'Tool calls will be rejected and you will fail the task.'
273  
274  export function getPartialCompactPrompt(
275    customInstructions?: string,
276    direction: PartialCompactDirection = 'from',
277  ): string {
278    const template =
279      direction === 'up_to'
280        ? PARTIAL_COMPACT_UP_TO_PROMPT
281        : PARTIAL_COMPACT_PROMPT
282    let prompt = NO_TOOLS_PREAMBLE + template
283  
284    if (customInstructions && customInstructions.trim() !== '') {
285      prompt += `\n\nAdditional Instructions:\n${customInstructions}`
286    }
287  
288    prompt += NO_TOOLS_TRAILER
289  
290    return prompt
291  }
292  
293  export function getCompactPrompt(customInstructions?: string): string {
294    let prompt = NO_TOOLS_PREAMBLE + BASE_COMPACT_PROMPT
295  
296    if (customInstructions && customInstructions.trim() !== '') {
297      prompt += `\n\nAdditional Instructions:\n${customInstructions}`
298    }
299  
300    prompt += NO_TOOLS_TRAILER
301  
302    return prompt
303  }
304  
305  /**
306   * Formats the compact summary by stripping the <analysis> drafting scratchpad
307   * and replacing <summary> XML tags with readable section headers.
308   * @param summary The raw summary string potentially containing <analysis> and <summary> XML tags
309   * @returns The formatted summary with analysis stripped and summary tags replaced by headers
310   */
311  export function formatCompactSummary(summary: string): string {
312    let formattedSummary = summary
313  
314    // Strip analysis section — it's a drafting scratchpad that improves summary
315    // quality but has no informational value once the summary is written.
316    formattedSummary = formattedSummary.replace(
317      /<analysis>[\s\S]*?<\/analysis>/,
318      '',
319    )
320  
321    // Extract and format summary section
322    const summaryMatch = formattedSummary.match(/<summary>([\s\S]*?)<\/summary>/)
323    if (summaryMatch) {
324      const content = summaryMatch[1] || ''
325      formattedSummary = formattedSummary.replace(
326        /<summary>[\s\S]*?<\/summary>/,
327        `Summary:\n${content.trim()}`,
328      )
329    }
330  
331    // Clean up extra whitespace between sections
332    formattedSummary = formattedSummary.replace(/\n\n+/g, '\n\n')
333  
334    return formattedSummary.trim()
335  }
336  
337  export function getCompactUserSummaryMessage(
338    summary: string,
339    suppressFollowUpQuestions?: boolean,
340    transcriptPath?: string,
341    recentMessagesPreserved?: boolean,
342  ): string {
343    const formattedSummary = formatCompactSummary(summary)
344  
345    let baseSummary = `This session is being continued from a previous conversation that ran out of context. The summary below covers the earlier portion of the conversation.
346  
347  ${formattedSummary}`
348  
349    if (transcriptPath) {
350      baseSummary += `\n\nIf you need specific details from before compaction (like exact code snippets, error messages, or content you generated), read the full transcript at: ${transcriptPath}`
351    }
352  
353    if (recentMessagesPreserved) {
354      baseSummary += `\n\nRecent messages are preserved verbatim.`
355    }
356  
357    if (suppressFollowUpQuestions) {
358      let continuation = `${baseSummary}
359  Continue the conversation from where it left off without asking the user any further questions. Resume directly — do not acknowledge the summary, do not recap what was happening, do not preface with "I'll continue" or similar. Pick up the last task as if the break never happened.`
360  
361      if (
362        (feature('PROACTIVE') || feature('KAIROS')) &&
363        proactiveModule?.isProactiveActive()
364      ) {
365        continuation += `
366  
367  You are running in autonomous/proactive mode. This is NOT a first wake-up — you were already working autonomously before compaction. Continue your work loop: pick up where you left off based on the summary above. Do not greet the user or ask what to work on.`
368      }
369  
370      return continuation
371    }
372  
373    return baseSummary
374  }