In the previous chapter, Communication Channels, we gave our agents the ability to talk to users and teammates. They can now ask for help and report status.
But communicating isn't the same as thinking.
If you ask an AI to "Rebuild the entire payment system," you don't want it to immediately start deleting files and guessing code. You want it to pause, study the existing code, draft a plan, and ask for your approval before it touches anything sensitive.
This chapter introduces the Planning Workflow: a safety mechanism that forces agents to "Measure twice, cut once."
Imagine you are a contractor building a house.
The Planning Workflow enforces the "Architect" approach.
A user asks: "Migrate our database from SQLite to PostgreSQL."
PLAN.md.There are three main components to this workflow:
FileEdit or RunCommand) are disabled.
The workflow consists of two specific tools: EnterPlanMode and ExitPlanMode.
When the agent realizes a task is complex, it calls EnterPlanMode.
// The agent decides to stop and think
{
name: "EnterPlanMode",
input: {}
}
Result: The system locks the "Write" permissions. The agent can now only Read files and Think.
The agent explores the codebase. It creates a plan file (usually CURRENT_PLAN.md). It writes down:
When the agent is ready, it submits the plan for approval using ExitPlanMode.
// The agent submits the plan
{
name: "ExitPlanMode",
input: {
"plan": "1. Update schema.prisma\n2. Run migration..."
}
}
Result: The user sees a prompt: "Do you approve this plan?" If yes, the agent unlocks FileEdit and starts coding.
How does the system enforce this safety latch?
Let's look at the code that manages this state machine.
EnterPlanModeTool.tsWhen this tool is called, we update the global state of the application.
// From EnterPlanModeTool.ts (Simplified)
export const EnterPlanModeTool = buildTool({
name: "EnterPlanMode",
// This tool changes the state of the agent
async call(_input, context) {
// 1. Get current application state
const appState = context.getAppState();
// 2. Switch mode to 'plan'
// This flag is checked by other tools to block write access
context.setAppState(prev => ({
...prev,
toolPermissionContext: {
...prev.toolPermissionContext,
mode: 'plan'
}
}));
return { data: { message: "Entered plan mode." } };
}
});
Explanation: This is the switch. By setting mode: 'plan', the system knows to restrict behavior.
planAgent.ts
How do we actually stop the agent from writing code? We define a specialized "Plan Agent" that has a list of disallowedTools.
// From AgentTool/built-in/planAgent.ts (Simplified)
export const PLAN_AGENT = {
agentType: 'Plan',
// The list of forbidden actions
disallowedTools: [
'Agent', // Cannot spawn sub-agents
'ExitPlanMode', // Cannot exit without the specific tool logic
'FileEdit', // CANNOT edit files
'FileWrite', // CANNOT write new files
],
// The instructions given to the AI
getSystemPrompt: () => `
You are a software architect.
CRITICAL: READ-ONLY MODE.
You are STRICTLY PROHIBITED from modifying files.
Your role is to explore and design.
`
};
Explanation: Even if the AI wanted to write code, the FileEdit tool is physically removed from its toolbox while in this mode.
ExitPlanModeV2Tool.tsThis is the most critical part: getting back to work. This tool requires user interaction.
// From ExitPlanModeTool/ExitPlanModeV2Tool.ts (Simplified)
export const ExitPlanModeV2Tool = buildTool({
name: "ExitPlanMode",
// 1. Force the system to pause and ask the human
requiresUserInteraction() {
// If it's a sub-agent, the Team Lead approves.
// If it's the main agent, the Human approves.
return !isTeammate();
},
async call(input, context) {
const plan = input.plan;
// 2. Save the plan to disk so we don't lose it
await writeFile('current_plan.md', plan);
// 3. Switch mode back to normal
context.setAppState(prev => ({
...prev,
toolPermissionContext: { mode: 'default' }
}));
return { data: { message: "Plan approved. Start coding." } };
}
});
Explanation:
requiresUserInteraction() tells the runtime "Don't run this automatically."default, restoring the FileEdit tool.The Planning Workflow turns an impulsive AI into a thoughtful engineer.
Now that we have a plan and permission to edit files, how do our agents actually touch the hard drive?
Next Chapter: File System Manipulation
Generated by Code IQ