Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions agents/__tests__/base3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,20 @@ describe('a lean compaction policy', () => {
test('keeps `lean` out of compactContext, whose schema is strict', () => {
expect(lean.compactContext).toEqual(runtimePolicy)
})

test('workspace roots get the budget without the todo tool they lack', () => {
// Desktop/Cloud workspace roots drop write_todos (agents/base3.ts), so
// telling them to write a todo plan leaves the guidance unactionable.
const workspaceLean = createBase3('anthropic/claude-haiku-5.5', {
compaction: LEAN_COMPACTION_POLICY,
workspace: true,
})
expect(workspaceLean.systemPrompt).toContain('# Context budget')
expect(workspaceLean.systemPrompt).not.toContain('write_todos')
expect(workspaceLean.systemPrompt).not.toContain('your todos')
expect(workspaceLean.systemPrompt).toContain('keep a short plan in your reply')
expect(lean.systemPrompt).toContain('write_todos')
})
})

describe('working in batches', () => {
Expand Down
4 changes: 3 additions & 1 deletion agents/base3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ export function createBase3(
options.compaction ?? compactionPolicyForModel(model)
const leanGuidance =
lean && compactContext.maxContextLength
? `\n${leanContextGuidance(compactContext.maxContextLength)}\n`
? `\n${leanContextGuidance(compactContext.maxContextLength, {
workspace: options.workspace,
})}\n`
: ''
const batching = options.batching === true && batchingModel && !lean
return {
Expand Down
31 changes: 27 additions & 4 deletions agents/lean-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,33 @@ import { compactionThreshold } from '@codebuff/common/constants/compaction-polic
* often a run compacted. Telling the model to save context made it do less
* work until the second paragraph said it must not, and the write_todos line
* is what lets it resume after a pass (remaining todos survive in the
* summary).
* summary). Workspace roots have no write_todos, so for them that line is a
* short plan in the transcript instead.
*/
export function leanContextGuidance(maxContextLength: number): string {
export function leanContextGuidance(
maxContextLength: number,
options: { workspace?: boolean } = {},
): string {
const condensedAt = Math.round(compactionThreshold(maxContextLength) / 1000)
// Desktop and Cloud workspace roots have no write_todos tool
// (agents/base3.ts drops it), so their plan has to live in the transcript.
const plan = options.workspace
? {
write: 'keep a short plan in your reply and update it as you go',
survives:
'This workspace has no to-do tool; the plan in the transcript is what you check after condensing.',
check: 'your plan',
}
: {
write:
'write the plan with write_todos before editing and keep it current',
survives:
'Your remaining todos survive condensing, so they are how you pick up where you left off; without them you lose track of what is done.',
check: 'your todos',
}
// One sentence skeleton, two variants: the non-workspace text stays as it was.
const planGuidance = `- For any task with more than a couple of steps, ${plan.write}. ${plan.survives}`
const stayGuidance = `- Stay within the request. After condensing, check ${plan.check} and the request before starting anything new rather than broadening the change.`
return `# Context budget

Your context is small: everything you read or run stays in this conversation. When it grows past ~${condensedAt}k tokens it is condensed automatically: you keep the user's request, a log of your earlier messages and tool calls, the most recent file reads, and an outline with line numbers of the other files you read. Old tool output is gone after that, and files you edited are not carried over; re-read only the window you need, using the outline's line numbers.
Expand All @@ -24,7 +47,7 @@ The budget limits how you read, never how much of the task you do. Do every part
- Don't re-read lines you have already seen unless the file changed since.
- Keep terminal output short: filter long output (grep, head, tail -n 50) while preserving the exit status. Still run the typecheck and the tests that cover your change.
- Edit with str_replace rather than rewriting whole files.
- For any task with more than a couple of steps, write the plan with write_todos before editing and keep it current. Your remaining todos survive condensing, so they are how you pick up where you left off; without them you lose track of what is done.
- Stay within the request. After condensing, check your todos and the request before starting anything new rather than broadening the change.
${planGuidance}
${stayGuidance}
- Before finishing, code_search for remaining references to anything you renamed, removed or changed the signature of, including tests, and update them.`
}
Loading