Skip to content

Commit a35f682

Browse files
committed
Fix for context pruner: actually pass system prompt and tools through
1 parent 37914b8 commit a35f682

File tree

9 files changed

+73
-26
lines changed

9 files changed

+73
-26
lines changed

.agents/types/agent-definition.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,14 @@ export interface AgentState {
283283
/** The last value set by the set_output tool. This is a plain object or undefined if not set. */
284284
output: Record<string, any> | undefined
285285

286-
/** The system prompt for this agent. Used by context-pruner to account for system prompt size when calculating effective message budget. */
287-
systemPrompt?: string
286+
/** The system prompt for this agent. */
287+
systemPrompt: string
288288

289-
/** The tool definitions for this agent. Used by context-pruner to account for tool definition tokens when calculating effective message budget. */
290-
toolDefinitions?: Record<string, { name: string; description: string; parameters: JsonObjectSchema }>[]
289+
/** The tool definitions for this agent. */
290+
toolDefinitions: Record<
291+
string,
292+
{ description: string | undefined; inputSchema: {} }
293+
>
291294
}
292295

293296
/**

common/src/templates/initial-agents-dir/types/agent-definition.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,15 @@ export interface AgentState {
282282

283283
/** The last value set by the set_output tool. This is a plain object or undefined if not set. */
284284
output: Record<string, any> | undefined
285+
286+
/** The system prompt for this agent. */
287+
systemPrompt: string
288+
289+
/** The tool definitions for this agent. */
290+
toolDefinitions: Record<
291+
string,
292+
{ description: string | undefined; inputSchema: {} }
293+
>
285294
}
286295

287296
/**

common/src/types/session-state.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ export type AgentState = {
3939
directCreditsUsed: number
4040
output?: Record<string, any>
4141
parentId?: string
42-
systemPrompt?: string
43-
toolDefinitions?: Record<string, { description: string | undefined; inputSchema: {} }>
42+
systemPrompt: string
43+
toolDefinitions: Record<
44+
string,
45+
{ description: string | undefined; inputSchema: {} }
46+
>
4447
}
4548

4649
export const AgentOutputSchema = z.discriminatedUnion('type', [
@@ -122,6 +125,8 @@ export function getInitialAgentState(): AgentState {
122125
directCreditsUsed: 0,
123126
output: undefined,
124127
parentId: undefined,
128+
systemPrompt: '',
129+
toolDefinitions: {},
125130
}
126131
}
127132
export function getInitialSessionState(

packages/agent-runtime/src/run-agent-step.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ export async function loopAgentSteps(
493493
startAgentRun: StartAgentRunFn
494494
userId: string | undefined
495495
userInputId: string
496+
agentTemplate?: AgentTemplate
496497
} & ParamsExcluding<typeof additionalToolDefinitions, 'agentTemplate'> &
497498
ParamsExcluding<
498499
typeof runProgrammaticStep,
@@ -569,10 +570,12 @@ export async function loopAgentSteps(
569570
userInputId,
570571
} = params
571572

572-
const agentTemplate = await getAgentTemplate({
573-
...params,
574-
agentId: agentType,
575-
})
573+
const agentTemplate =
574+
params.agentTemplate ??
575+
(await getAgentTemplate({
576+
...params,
577+
agentId: agentType,
578+
}))
576579
if (!agentTemplate) {
577580
throw new Error(`Agent template not found for type: ${agentType}`)
578581
}
@@ -767,6 +770,7 @@ export async function loopAgentSteps(
767770
stepNumber: totalSteps,
768771
stepsComplete: shouldEndTurn,
769772
system,
773+
tools,
770774
template: agentTemplate,
771775
toolCallParams: currentParams,
772776
})

packages/agent-runtime/src/run-programmatic-step.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,23 @@ export async function runProgrammaticStep(
384384
export const getPublicAgentState = (
385385
agentState: AgentState & Required<Pick<AgentState, 'runId'>>,
386386
): PublicAgentState => {
387-
const { agentId, runId, parentId, messageHistory, output } = agentState
387+
const {
388+
agentId,
389+
runId,
390+
parentId,
391+
messageHistory,
392+
output,
393+
systemPrompt,
394+
toolDefinitions,
395+
} = agentState
388396
return {
389397
agentId,
390398
runId,
391399
parentId,
392400
messageHistory: messageHistory as any as PublicAgentState['messageHistory'],
393401
output,
402+
systemPrompt,
403+
toolDefinitions,
394404
}
395405
}
396406

packages/agent-runtime/src/tools/handlers/handler-function-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export type CodebuffToolHandlerFunction<T extends ToolName = ToolName> = (
5050
sendSubagentChunk: SendSubagentChunkFn
5151
signal: AbortSignal
5252
system: string
53-
tools?: ToolSet
53+
tools: ToolSet
5454
trackEvent: TrackEventFn
5555
userId: string | undefined
5656
userInputId: string

packages/agent-runtime/src/tools/handlers/tool/spawn-agent-inline.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type { PrintModeEvent } from '@codebuff/common/types/print-mode'
1818
import type { AgentState } from '@codebuff/common/types/session-state'
1919
import type { ProjectFileContext } from '@codebuff/common/util/file'
2020
import type { ToolSet } from 'ai'
21+
import { mapValues } from 'lodash'
2122

2223
type ToolName = 'spawn_agent_inline'
2324
export const handleSpawnAgentInline = (async (
@@ -33,7 +34,7 @@ export const handleSpawnAgentInline = (async (
3334
localAgentTemplates: Record<string, AgentTemplate>
3435
logger: Logger
3536
system: string
36-
tools?: ToolSet
37+
tools: ToolSet
3738
userId: string | undefined
3839
userInputId: string
3940
writeToClient: (chunk: string | PrintModeEvent) => void
@@ -60,9 +61,10 @@ export const handleSpawnAgentInline = (async (
6061
agentTemplate: parentAgentTemplate,
6162
fingerprintId,
6263
system,
63-
tools: parentTools = {},
64+
tools: parentTools,
6465
userInputId,
6566
writeToClient,
67+
logger,
6668
} = params
6769
const {
6870
agent_type: agentTypeStr,
@@ -80,17 +82,31 @@ export const handleSpawnAgentInline = (async (
8082

8183
validateAgentInput(agentTemplate, agentType, prompt, spawnParams)
8284

85+
// Override template for inline agent to share system prompt & message history with parent
86+
const inlineTemplate = {
87+
...agentTemplate,
88+
includeMessageHistory: true,
89+
inheritParentSystemPrompt: true,
90+
}
91+
8392
// Create child agent state that shares message history with parent
84-
const childAgentState: AgentState = createAgentState(
85-
agentType,
86-
agentTemplate,
87-
parentAgentState,
88-
parentAgentState.agentContext,
89-
)
93+
const childAgentState: AgentState = {
94+
...createAgentState(
95+
agentType,
96+
inlineTemplate,
97+
parentAgentState,
98+
parentAgentState.agentContext,
99+
),
100+
systemPrompt: system,
101+
toolDefinitions: mapValues(parentTools, (tool) => ({
102+
description: tool.description,
103+
inputSchema: tool.inputSchema as {},
104+
})),
105+
}
90106

91107
logAgentSpawn({
92108
...params,
93-
agentTemplate,
109+
agentTemplate: inlineTemplate,
94110
agentType,
95111
agentId: childAgentState.agentId,
96112
parentId: childAgentState.parentId,
@@ -104,14 +120,12 @@ export const handleSpawnAgentInline = (async (
104120
userInputId: `${userInputId}-inline-${agentType}${childAgentState.agentId}`,
105121
prompt: prompt || '',
106122
spawnParams,
107-
agentTemplate,
123+
agentTemplate: inlineTemplate,
108124
parentAgentState,
109125
agentState: childAgentState,
110126
fingerprintId,
111127
parentSystemPrompt: system,
112-
parentTools: agentTemplate.inheritParentSystemPrompt
113-
? parentTools
114-
: undefined,
128+
parentTools,
115129
onResponseChunk: (chunk) => {
116130
// Inherits parent's onResponseChunk, except for context-pruner (TODO: add an option for it to be silent?)
117131
if (agentType !== 'context-pruner') {

packages/agent-runtime/src/tools/handlers/tool/spawn-agent-utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ export function createAgentState(
186186
directCreditsUsed: 0,
187187
output: undefined,
188188
parentId: parentAgentState.agentId,
189+
systemPrompt: '',
190+
toolDefinitions: {},
189191
}
190192
}
191193

packages/agent-runtime/src/tools/tool-executor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export type ExecuteToolCallParams<T extends string = ToolName> = {
143143
runId: string
144144
signal: AbortSignal
145145
system: string
146-
tools?: ToolSet
146+
tools: ToolSet
147147
toolCallId: string | undefined
148148
toolCalls: (CodebuffToolCall | CustomToolCall)[]
149149
toolResults: ToolMessage[]

0 commit comments

Comments
 (0)