Welcome to the final chapter of our Coordinator tutorial!
In the previous chapter, Dynamic Context Injection, we learned how to build the "Manager's Handbook" (the context) at the exact moment the application starts. We learned how to inject tools based on the environment.
But there is one final, critical challenge: Time Travel.
When you close the application and open it again later to resume a conversation, the system needs to remember not just what was said, but who the AI was acting as.
In this chapter, we will explore Session State Synchronization, the logic that ensures the AI wakes up wearing the right uniform.
Imagine this scenario:
claude resume to finish the job.The Danger: If the application starts up in "Default Mode" (Solo Developer) but loads a conversation history full of "Manager" talk, the AI will be confused.
AgentTool.AgentTool.To fix this, we need a mechanism that runs before the AI says a single word.
We call this matchSessionMode. It compares two things:
If they don't match, the system forces the runtime to change, ensuring consistency.
You are debugging a complex issue. The Coordinator has 3 workers running. You have to leave for a meeting. When you come back, you want the Coordinator to pick up exactly where it left off, still acting as the Manager.
Let's visualize the boot process when you resume a session.
matchSessionMode
The logic for this lives in coordinatorMode.ts. It acts as a gatekeeper during the startup phase.
First, the function accepts the sessionMode retrieved from the database or file system.
// From coordinatorMode.ts
export function matchSessionMode(
sessionMode: 'coordinator' | 'normal' | undefined,
): string | undefined {
// If this is an old session with no mode data, do nothing.
if (!sessionMode) {
return undefined
}
Explanation: If the session is ancient (created before this feature existed), we skip the check to avoid breaking things.
Next, we check if the current environment matches the saved session.
const currentIsCoordinator = isCoordinatorMode()
const sessionIsCoordinator = sessionMode === 'coordinator'
// If they already match, we are good to go!
if (currentIsCoordinator === sessionIsCoordinator) {
return undefined
}
Explanation: isCoordinatorMode() checks the current environment variable. If current == saved, no action is needed.
If there is a mismatch, we modify the global environment variable process.env immediately.
// Flip the env var directly
if (sessionIsCoordinator) {
process.env.CLAUDE_CODE_COORDINATOR_MODE = '1'
} else {
delete process.env.CLAUDE_CODE_COORDINATOR_MODE
}
Explanation: This is the critical line. By setting process.env, any future calls to isCoordinatorMode() (like those used in Dynamic Context Injection) will now return true. We have successfully put the "Manager Uniform" back on.
Finally, the function returns a message. This is often logged so the developer knows an automatic switch happened.
return sessionIsCoordinator
? 'Entered coordinator mode to match resumed session.'
: 'Exited coordinator mode to match resumed session.'
}
Without this synchronization, the user would have to manually remember to add flags like --coordinator every time they resumed a specific session.
claude resumeclaude resumeCongratulations! You have completed the Coordinator project tutorial. Let's recap what we've built:
AgentTool) to Spawn, Continue, and Stop workers.<task-notification>) for workers to report back without confusing the AI.You now understand the architecture of an agentic system that can manage parallel work, maintain context hygiene, and persist across sessions.
End of Tutorial.
Generated by Code IQ