-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(agent-core-v2): remind the model of its context budget and point compaction notes at the wire journal #3423
New issue
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2392bac
feat(agent-core-v2): remind the model of its context budget and point…
RealKai42 5bf4843
Merge main into feat/compaction-context-budget
RealKai42 442cf63
Merge origin/main into feat/compaction-context-budget
RealKai42 107b27b
feat(agent-core-v2): ship context budget reminders and the recovery p…
RealKai42 77c7e4a
fix(agent-core-v2): cap event log reads and refuse empty-history comp…
RealKai42 c0054f5
fix(agent-core-v2): drop the redundant ninety bucket and soften ahead…
RealKai42 200374c
chore: merge the compaction changesets into one
RealKai42 a0e0bc0
Merge remote-tracking branch 'origin/main' into feat/compaction-conte…
RealKai42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": minor | ||
| --- | ||
|
|
||
| Remind the model of its context budget before automatic compaction, and after compaction point it at the session's event log for exact details. |
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
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
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
15 changes: 15 additions & 0 deletions
15
packages/agent-core-v2/src/agent/fullCompaction/compactionInstruction.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { renderPrompt } from '#/_base/utils/render-prompt'; | ||
|
|
||
| import compactionInstructionTemplate from './compaction-instruction.md?raw'; | ||
|
|
||
| export interface CompactionInstructionInput { | ||
| readonly customInstruction?: string; | ||
| } | ||
|
|
||
| export function renderCompactionInstruction(input: CompactionInstructionInput): string { | ||
| const customInstruction = input.customInstruction?.trim() ?? ''; | ||
| return renderPrompt(compactionInstructionTemplate, { | ||
| custom_instruction_block: | ||
| customInstruction.length > 0 ? `\nOptional user instruction:\n${customInstruction}\n` : '', | ||
| }).trimEnd(); | ||
| } |
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
10 changes: 10 additions & 0 deletions
10
packages/agent-core-v2/src/agent/fullCompaction/context-recovery-footer.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| ## Context Recovery | ||
| Everything before this note is still on disk in this agent's event log (read-only, append-only): | ||
| ${wire_path} | ||
| ${window_lines} | ||
| If you need exact command output, file contents, error text, or the wording of an earlier request, look it up there instead of guessing. How to read it: | ||
| - Layout: one file per agent. agents/main/ is the main agent; each subagent has its own agents/<agentId>/wire.jsonl. A parent's log holds only the Agent tool call and the subagent's returned result — the subagent's own steps are in its own file. | ||
| - Format: one JSON record per line, append-only; `type` says what it is. The conversation is in `context.append_message` (user prompts) and `context.append_loop_event` (event.type: step.begin | content.part [text|think] | tool.call | tool.result | step.end). Every other type (llm.request, usage.record, token_counting.measured, metadata, profile.bind, …) is bookkeeping — skip it. | ||
| - Boundaries: `context.apply_compaction` marks a compaction (older lines stay in the file; grep for it to find exact boundaries). `context.undo` count=N retracts the previous N messages — treat retracted content as never having happened. `context.clear` resets the conversation. | ||
| - Externalized content: tool results over 50k chars are stored truncated, with an `output_path` to a tool-results/*.txt file holding the full text. Media parts are blob references, not inline. | ||
| - Reading: lines are long JSON (often 10k+ chars). Grep the file for a keyword to get line numbers, then Read exactly that line (line_offset=N, n_lines=1) — Read returns wire.jsonl lines whole up to ~150k chars. To pull one field with real newlines: sed -n 'Np' wire.jsonl | jq -r '.event.result.output'. Never Read large ranges — a handful of records can exceed the per-call byte cap. |
28 changes: 28 additions & 0 deletions
28
packages/agent-core-v2/src/agent/fullCompaction/contextRecovery.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { renderPrompt } from '#/_base/utils/render-prompt'; | ||
| import type { WireLineRange } from '#/wire/record'; | ||
|
|
||
| import contextRecoveryTemplate from './context-recovery-footer.md?raw'; | ||
|
|
||
| export const CONTEXT_RECOVERY_HEADING = '## Context Recovery'; | ||
|
|
||
| export interface ContextRecoveryPointer { | ||
| readonly journalPath: string; | ||
| readonly windows: readonly WireLineRange[]; | ||
| } | ||
|
|
||
| export function renderContextRecoveryPointer(pointer: ContextRecoveryPointer): string { | ||
| const windows = pointer.windows; | ||
| const summarized = windows.length - 1; | ||
| const lines = windows.map((range, index) => { | ||
| const label = `window ${String(index + 1)}: lines ${String(range.start)}–${String(range.end)}`; | ||
| return index === summarized ? `${label} ← the conversation this note summarizes` : label; | ||
| }); | ||
| const nextStart = windows[summarized]!.end + 1; | ||
| lines.push( | ||
| `window ${String(windows.length + 1)} (the one you are in now) starts at line ${String(nextStart)} with the \`context.apply_compaction\` record that carries this note — it is already in your context; no need to read it.`, | ||
| ); | ||
| return renderPrompt(contextRecoveryTemplate, { | ||
| wire_path: pointer.journalPath, | ||
| window_lines: lines.map((line) => ` ${line}`).join('\n'), | ||
| }).trimEnd(); | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.