This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SUMMARY_TEMPLATE = `Output exactly the Markdown structure shown inside <template> and keep the section order unchanged. Do not include the <template> tags in your response.
<template>
## Objective
- [one or two brief sentences describing what the user is trying to accomplish]
## Important Details
- [constraints/preferences, decisions and why, important facts/assumptions, exact context needed to continue, or "(none)"]
## Work State
### Completed
- [finished work, verified facts, or changes made; otherwise "(none)"]
### Active
- [current work, partial changes, or investigation state; otherwise "(none)"]
### Blocked
- [blockers, failing commands, or unknowns; otherwise "(none)"]
## Next Move
1. [immediate concrete action, or "(none)"]
2. [next action if known, or "(none)"]
## Relevant Files
- [file or directory path: why it matters, or "(none)"]
</template>
Rules:
- Keep every section, even when empty.
- Use terse bullets, not prose paragraphs.
- Preserve exact file paths, symbols, commands, error strings, URLs, and identifiers when known.
- Do not mention the summary process or that context was compacted.`
const SUMMARY_UPDATE_INSTRUCTIONS = `The <prior-summary> summarizes everything that happened before the <conversation>. Construct a new summary that combines both. The <prior-summary> is discarded after this: anything you do not carry into the new summary is lost.
const SUMMARY_TEMPLATE = `You are creating a comprehensive context restoration document. This document will serve as the foundation for continued work - it must preserve critical knowledge that would otherwise be lost.
Create a detailed summary with these sections:
1. Current Task State - what is being worked on, next steps, blockers
- Be specific with paths, line numbers, function names
- Capture the "why" behind decisions
- User directives are sacred - never omit them`
const SUMMARY_UPDATE_INSTRUCTIONS = `The <prior-summary> summarizes everything that happened before the <extracted_context>. Merge all information from the <prior-summary> into your new document. Do not lose any historical context.
When combining:
- Carry forward objectives, constraints, user directives, decisions, and parallel workstreams from the <prior-summary> even when the <conversation> does not mention them. Drop only what is finished and no longer needed.
- The <conversation> is more recent than the <prior-summary>. Where they conflict, the conversation wins: state the corrected fact and drop the old claim.
- Add new progress, decisions, constraints, and context from the conversation.
- Move completed work from "Active" to "Completed".
- If a blocker has been resolved, update the summary to reflect that while keeping any details still needed to continue the work.
- Update "Objective" and "Next Move" to reflect the current work state.`
- Carry forward objectives, constraints, user directives, decisions, and parallel workstreams from the <prior-summary> even when the <extracted_context> does not mention them. Drop only what is finished and no longer needed.
- The <extracted_context> is more recent than the <prior-summary>. Where they conflict, the newer content wins: state the corrected fact and drop the old claim.
- Add new progress, decisions, constraints, and context from the <extracted_context>.`
const RECENT_GUIDANCE = `The <recent_context> below is the most recent activity in the session. It is NOT part of what you are summarizing - it stays in the conversation verbatim. Use it only to judge relevance: weight the summary toward what matters for where the session is currently headed, and drop detail from the <extracted_context> that is no longer relevant to the current direction.`
const conversation = `Here is the conversation so far:\n\n<conversation>\n${input.context.join("\n\n")}\n</conversation>`
if (!input.previousSummary)
return [
conversation,
"Create a new anchored summary from the conversation history in the <conversation> tags above so another coding agent can continue the work.",
SUMMARY_TEMPLATE,
].join("\n\n")
export const buildPrompt = (input: {
readonly previousSummary?: string
readonly context: readonly string[]
readonly recent?: string
}) => {
const extracted = `The following conversation content needs to be distilled into the summary:\n\n<extracted_context>\n${input.context.join("\n\n")}\n</extracted_context>`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Tokens of the most recent messages to include in the compaction prompt as a relevance signal, so the summary is weighted toward the session's current direction. These messages are also kept verbatim (they are within the preserved tail). 0 disables the signal.",
}),
trigger_ratio: Schema.optional(Ratio).annotate({
description:
"Fraction (0-1) of the model's context window at which automatic compaction triggers. When set, compaction fires once usage reaches trigger_ratio x context (a proactive percentage), overriding the default fixed-headroom (reserved) mechanism. Example: 0.85 compacts at 85% of the context window.",
}),
extract_ratio: Schema.optional(Ratio).annotate({
description:
"Fraction (0-1) of the current scoped conversation to summarize (the oldest portion). The rest is kept verbatim as the tail. When set, this overrides preserve_recent_tokens and scales with session size, so a small session is never fully summarized. Example: 0.40 summarizes the oldest 40% and keeps the newest 60% verbatim.",
}),
recent_ratio: Schema.optional(Ratio).annotate({
description:
"Fraction (0-1) of the current scoped conversation, taken from the newest end, to include in the compaction prompt as the relevance signal. When set, overrides recent_tokens and scales with session size. Example: 0.15 feeds the newest 15% to the summarizer as the lens.",
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat(core): proportional compaction with context-restoration prompt #5
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Are you sure you want to change the base?
feat(core): proportional compaction with context-restoration prompt #5
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
Uh oh!
There was an error while loading. Please reload this page.