Sign In Sign Up

Add parallel course correction agent with forced tool call

I want to add a new agent. But it's different from the rest. It runs in parallel to the main thread. on every inference:completed (either tool_use or or end_turn) it runs one forced tool call with gemini with the entire thread. The forced tool call is to a new tool we are adding called course_correct, course correct has a boolean field (if true we want to add a course correct), and an optional message to be sent to the agent to stear it. The message should be like a user message, but it should have some metadata so we can render it differently. You should use gemini 3 as the model. The system prompt should ask it to check whether the model has forgotten anything or whether the model is going too far off the plan (diverging) or too narrow (not doing enough) and then correct its course. Or whether anything was forgotten based on the AGENTS.md file (run lint, ...) We need to: 1. add the tool 2. add the logic to trigger the forced tool call (look at the handoff logic for this for how to force a tool call with a prompt) 3. Create a good system prompt for it (we want the custom system prompt, the messages from the original thread as markdown, which should hightlight the user messages and the AGENTS.md file read in). 4. Add the logic to send the user:message in the thread worker / thread delta based on the result. This should run in the background once an tool_use stop reason is here, so it completed with the rool results and it should be sent after the tool results have been added to the thread but before we send a new request. On stop reason end of turn, we run it and send it only if the user has not sent a message yet.

This is a complex feature request to add a "course correction" agent that runs in parallel to the main thread. Let me break down what's needed:

  1. A new tool called course_correct with a boolean field and optional message
  2. Logic to trigger forced tool calls using Gemini (similar to handoff logic)
  3. A system prompt for the course correction agent
  4. Logic to inject user messages based on the result

Let me start by understanding the codebase structure and finding relevant files.

I'll help you add a course correction agent. Let me first explore the codebase to understand the relevant patterns.

Updated TODOs
Find the handoff logic that forces tool calls with a prompt. Look for how handoff agent works, how it forces specific tool calls, and how it constructs prompts for sub-agents. Return file paths and relevant code patterns.
Find the thread worker and inference:completed event handling. Look for where tool_use and end_turn stop reasons are handled, and where tool results are added to threads. Return file paths and line numbers.
Find how Gemini models are configured and used in the codebase. Look for model selection, Gemini-specific configurations, and how to make inference calls with specific models.

Let me read the key files to understand the patterns better:

Updated TODOs

Let me also check the thread structure to understand how user messages are added and look at an example tool definition:

interface.*ToolSpec|BaseToolSpec 24 matches

Let me also check the tool-service to understand the BaseToolSpec structure and look at the thread-delta for how user messages are added:

user:message|addUserMessage 47 matches

Now let me look at thread-delta to see more about user messages and check the thread structure for ThreadUserMessage: