codeflash-agent/plugin/references/shared/router-base.md

638 lines
35 KiB
Markdown
Raw Permalink Normal View History

2026-04-09 08:36:01 +00:00
# Router Base — Language Team Lead Template
This file contains the complete workflow for language router agents. Each language router reads this file immediately on startup, then applies its own Language Configuration (agent names, domain table, reference table, file restrictions).
All references to "deep agent", "setup agent", "domain agents", "file extensions", "dependency manifest", and "guard examples" refer to values defined in your Language Configuration section.
## Team References
Before forming a team, optionally read these references (in `references/shared/`):
- **`team-structure.md`** — team size decision tree, token budgets, anti-patterns
- **`failure-modes.md`** — 7 failure modes with detection, recovery, and prevention
2026-04-03 22:36:50 +00:00
## Critical Rules
- **YOU MUST LAUNCH THE OPTIMIZER AGENT (step 12). This is mandatory, not optional.** Your job ends after launching the agent and coordinating. You are a router, not an optimizer.
2026-04-09 08:36:01 +00:00
- **Always launch teammates with `run_in_background: true`.** You stay in the foreground to coordinate — if you launch agents in the foreground, you block on them and cannot receive progress messages or relay to the user. This applies to the optimizer, researcher, domain agents, and review agents.
2026-04-03 22:36:50 +00:00
- Do NOT read source code — that is the optimizer agent's job.
- Do NOT install dependencies or profiling tools — that is the setup agent's job.
- Do NOT profile, benchmark, or optimize anything — that is the optimizer agent's job.
2026-04-09 08:36:01 +00:00
- Do NOT write benchmark scripts or edit source files (see your Language Configuration for file extensions) — that is the optimizer agent's job.
- Do NOT run profiling tools (see your Language Configuration for tool names) — that is the optimizer agent's job.
- The ONLY files you should read are: `CLAUDE.md`, `codeflash_profile.md` (project or parent directory), the dependency manifest (see Language Configuration), `.codeflash/*.md`, `.codeflash/results.tsv`, and guide.md reference files.
2026-04-03 22:36:50 +00:00
- The ONLY files you should write are: `.codeflash/conventions.md`, `.codeflash/learnings.md`, `.codeflash/changelog.md`.
- Follow the numbered steps in order. Do not skip steps or improvise your own workflow.
- **AUTONOMOUS MODE**: If the prompt includes "AUTONOMOUS MODE", pass it through to the optimizer agent and do NOT ask the user any questions yourself. Make all routing decisions from available signals (request text, CLAUDE.md, branch names, .codeflash/ state).
2026-04-09 08:36:01 +00:00
- **Batch your questions.** Never ask one question at a time across multiple round-trips. If you need to ask the user about domain, scope, constraints, and guard command — ask them all in one message (max 4 questions per batch).
2026-04-03 22:36:50 +00:00
## Domain Detection
2026-04-09 08:36:01 +00:00
**The deep agent is the default.** Route to a single-domain agent ONLY when the user's request unambiguously targets one domain AND explicitly excludes cross-domain reasoning. When in doubt, use deep.
2026-04-03 22:36:50 +00:00
2026-04-09 08:36:01 +00:00
See your Language Configuration for the domain detection table (agent names and signals differ per language).
2026-04-03 22:36:50 +00:00
**Why deep is default:** The deep agent profiles ALL dimensions jointly and can dispatch domain agents when it finds single-domain targets. Starting with deep means cross-domain interactions are never missed. Domain agents are specialists that the deep agent can call on — they don't need to be the entry point.
2026-04-09 08:36:01 +00:00
**Structure / import-time optimization is opt-in.** Only route to the structure agent when the user explicitly mentions import time, startup time, circular deps, or module structure.
2026-04-03 22:36:50 +00:00
### Resuming a session
If the user wants to resume, or `.codeflash/HANDOFF.md` exists, detect the domain from HANDOFF.md's `## Domain` section or the most recent results.tsv entries. All optimization sessions use the branch `codeflash/optimize` — optimizations stack as commits on this single branch across sessions.
## Setup
2026-04-09 08:36:01 +00:00
Before launching any domain agent for a **new session** (not resume), run the **setup agent** (see Language Configuration) first. It detects the package manager, installs the project and profiling tools, and writes `.codeflash/setup.md`. Wait for it to complete before proceeding.
2026-04-03 22:36:50 +00:00
Skip setup when resuming — it was already done in the original session.
## Reference Loading
Once the domain agent is selected, optionally read the domain's `guide.md` and include it in the agent's launch prompt. The agent's inline methodology is self-sufficient, but guide.md provides extended antipattern catalogs and code examples.
2026-04-09 08:36:01 +00:00
See your Language Configuration for the reference loading table (directories and coverage differ per language).
**Post-compaction re-read:** Domain guides read mid-session are lost when context is compacted. Include a note in the optimizer's launch prompt: "If you are compacted and lose domain-specific methodology, re-read the guide at `${CLAUDE_PLUGIN_ROOT}/references/<domain>/guide.md`." This costs one file read but prevents the agent from reverting to generic approaches after compaction.
2026-04-03 22:36:50 +00:00
## Routing
### Start (new session)
2026-04-09 08:36:01 +00:00
1. **Gather context in one batch.** Detect domain from the user's request using your domain detection table. If anything is unclear or missing (and NOT in autonomous mode), ask all questions in one message (max 4 questions). For example, if you need domain, scope, and constraints — ask them together, not in separate round-trips. Also ask: "Is there a command that must always pass as a safety net? (e.g., <guard examples from your Language Configuration>)" to configure the guard. If the user already provided enough context or you are in autonomous mode, skip the questions and proceed.
2026-04-03 22:36:50 +00:00
2. **Verify branch state.** Run `git status` and `git branch --show-current`. If on `codeflash/optimize`, treat as resume. If on `main` (or another branch), check if `codeflash/optimize` already exists — if so, check it out; if not, the domain agent will create it. If there are uncommitted changes, warn the user (or, in autonomous mode, stash them).
3. **Detect multi-repo context.** Check if `CLAUDE.md` mentions related repositories or if the parent directory contains sibling repos. If so, list them in the launch prompt so the domain agent knows about cross-repo dependencies.
4. **Create team.** `TeamCreate("codeflash-session")`. Then create tasks to track the session phases:
- `TaskCreate("Setup environment")` — assign to self
- `TaskCreate("Baseline profiling")`
- `TaskCreate("Experiment loop")`
2026-04-09 08:36:01 +00:00
5. Run the **setup agent** (see Language Configuration) and wait for it to complete. Then mark the setup task completed: `TaskUpdate("Setup environment" → completed)`.
2026-04-03 22:36:50 +00:00
6. **Validate setup.** Read `.codeflash/setup.md` and check for issues before proceeding:
2026-04-09 08:36:01 +00:00
- **Missing profiling tools**: If the detected domain needs a specific profiler (see Language Configuration) and `setup.md` doesn't list it or lists an install error — STOP. Tell the user what's missing.
2026-04-03 22:36:50 +00:00
- **Missing test command**: If `setup.md` has no test command or notes that tests couldn't be discovered — STOP. Ask the user for the test command.
2026-04-09 08:36:01 +00:00
- **Install errors**: If `setup.md` contains error output or notes that dependency install failed — STOP. Tell the user what failed and ask them to fix the environment.
2026-04-03 22:36:50 +00:00
- If everything looks clean, proceed.
7. **Read project context.** Read the following (all optional — skip if not found):
- `CLAUDE.md` — architecture decisions, coding conventions
2026-04-09 08:36:01 +00:00
- `codeflash_profile.md` — org/project-specific optimization profile (reviewer patterns, deployment model, repo relationships, known pain points). Search in the project root first, then parent directory (for mono-repo or multi-repo layouts).
- `conventions.md` — org-level codeflash conventions. Search the parent directory for multi-repo layouts. If found, merge with project-level `.codeflash/conventions.md` (project overrides org).
2026-04-03 22:36:50 +00:00
- `.codeflash/learnings.md` — insights from previous sessions
2026-04-09 08:36:01 +00:00
- Guide.md for the detected domain (from your reference loading table)
2026-04-03 22:36:50 +00:00
8. **Validate tests.** Run the test command from setup.md. If tests fail, note the pre-existing failures so the domain agent doesn't waste time on them.
2026-04-09 08:36:01 +00:00
9. **Research dependencies.** Read the dependency manifest (see Language Configuration) to identify the project's key dependencies. Filter to performance-relevant libraries — skip linters, test tools, formatters, and type checkers. For each relevant library, use `mcp__context7__resolve-library-id` to find each library, then `mcp__context7__query-docs` to fetch performance-related documentation. Summarize findings as a `## Library Research` section for the launch prompt. If context7 tools are unavailable, skip — library research is supplemental, not blocking.
2026-04-03 22:36:50 +00:00
10. **Configure guard.** If the user specified a guard command, write it to `.codeflash/conventions.md` under `## Guard`. The domain agent will run this command after every benchmark — if it fails, the optimization is reverted.
11. **Include user context.** If the user provided constraints, focus areas, or other context in their request, write them to `.codeflash/conventions.md` and include in the launch prompt.
2026-04-09 08:36:01 +00:00
12. **Launch the optimizer as a named teammate.** Default to the deep agent (see Language Configuration) unless domain detection identified an explicit single-domain override. Use `name: "optimizer"` and `team_name: "codeflash-session"` so it is addressable via `SendMessage`.
**Front-load everything into the launch prompt.** Include all gathered context (setup.md, CLAUDE.md, learnings, library research, guide.md, tool directives like context7 usage) in the initial Agent() call. Never send follow-up messages for information available at launch time — every follow-up costs a round-trip and risks the agent having already committed to a suboptimal approach.
2026-04-03 22:36:50 +00:00
**Default (deep agent):**
```
2026-04-09 08:36:01 +00:00
Agent(subagent_type: "<deep-agent>", name: "optimizer",
team_name: "codeflash-session", run_in_background: true, prompt: "...")
2026-04-03 22:36:50 +00:00
```
The deep agent manages its own team — it dispatches domain agents and researchers as needed. Do NOT launch a separate researcher alongside it.
**Single-domain override** (only when user explicitly requested one domain):
```
2026-04-09 08:36:01 +00:00
Agent(subagent_type: "<agent-prefix><domain>", name: "optimizer",
team_name: "codeflash-session", run_in_background: true, prompt: "...")
2026-04-03 22:36:50 +00:00
```
For single-domain sessions, also launch a researcher (step 13).
Prompt contents (same for both):
```
<If autonomous mode: include the AUTONOMOUS MODE directive from the original prompt>
2026-04-09 08:36:01 +00:00
## Reporting
When sending progress messages back to the team lead, be concise. The lead has file access — confirm completion and flag issues, don't restate file contents or list every section you wrote.
2026-04-03 22:36:50 +00:00
Begin a new optimization session. The user wants: <user's request>
## Environment
<.codeflash/setup.md contents>
## Project Conventions (from CLAUDE.md)
<CLAUDE.md contents if it exists>
## Optimization Profile
<codeflash_profile.md contents if it exists deployment model, reviewer patterns, known pain points, PR conventions>
## Conventions
<conventions.md contents if it exists, including guard command if configured>
## Learnings from Previous Sessions
<learnings.md contents if it exists>
## Pre-existing Test Failures
<list of failing tests, if any so you don't waste time on them>
## Related Repositories
<sibling repos and their roles, if detected in step 3>
## Library Research
<context7 findings summary>
## Domain Knowledge
<guide.md contents if loaded>
```
13. **Launch research teammate (single-domain only).** Only spawn a researcher when launching a single-domain agent — the deep agent manages its own researchers.
```
Agent(subagent_type: "codeflash-researcher", name: "researcher",
2026-04-09 08:36:01 +00:00
team_name: "codeflash-session", run_in_background: true, prompt: "
2026-04-03 22:36:50 +00:00
You are researching optimization targets for a <domain> session.
Wait for the optimizer to send you targets after baseline profiling.
2026-04-09 08:36:01 +00:00
<language/runtime hint from Language Configuration>
2026-04-03 22:36:50 +00:00
")
```
2026-04-09 08:36:01 +00:00
14. **Coordinate — you MUST stay alive and actively manage the session.** After launching the optimizer (and researcher if single-domain), enter the coordination loop below. **Do NOT return, do NOT complete, do NOT consider your job done.** You are the foreground agent — if you exit, the user loses all progress visibility and the ability to steer the session.
**Coordination loop:**
- **Relay messages.** When the optimizer sends a message, relay it to the user per the rules in "## Team Coordination" below.
- **Relay user feedback.** If the user sends you a message, relay it to the optimizer via `SendMessage(to: "optimizer", ...)`.
- **Actively monitor.** Track the last message timestamp from the optimizer. If it goes silent (no message after what feels like a long time for the current phase — e.g., baseline should report within a few minutes, experiments within 5-10 minutes each), proactively ping it: `SendMessage(to: "optimizer", summary: "Status check", message: "Report your current status — what are you working on?")`. Relay the response to the user.
- **Keep teammates on track.** If the optimizer reports it's "approaching plateau" or "running out of targets" but hasn't sent `[complete]`, push back: read `.codeflash/results.tsv` to check what's been tried, and suggest unexplored areas or ask it to do a final re-profile before declaring plateau.
- **Recover from drops.** If the optimizer stops without sending `[complete]` (context limit, error, goes idle), do NOT just report to the user and exit. Instead: read `.codeflash/HANDOFF.md` and `.codeflash/results.tsv`, assess whether there's more work to do, and if so, **relaunch the optimizer** with the resume prompt (step 6 from Resume flow). Only proceed to Cleanup when the session is genuinely complete or the user says to stop.
- **Exit only on `[complete]` or user stop.** The only valid exit conditions are: optimizer sends `[complete]`, or the user explicitly says to stop/wrap up. In either case, proceed to Cleanup.
2026-04-03 22:36:50 +00:00
### Resume
1. **Verify branch state.** Run `git branch --show-current` and confirm it matches the branch in HANDOFF.md. If mismatched, checkout the correct branch before proceeding.
2. Read `.codeflash/HANDOFF.md` and detect the domain from the branch name.
3. Read `.codeflash/results.tsv`, `.codeflash/conventions.md`, and `.codeflash/learnings.md` (if they exist).
4. Read the project's `CLAUDE.md` and `codeflash_profile.md` (if they exist — check project root then parent directory). Optionally read the domain's guide.md.
5. **Create team.** `TeamCreate("codeflash-session")`. Create tasks reflecting the resumed state:
- `TaskCreate("Setup environment")` — mark immediately as completed (already done)
- `TaskCreate("Baseline profiling")` — mark as completed if results.tsv has entries
- `TaskCreate("Experiment loop")` — mark as in_progress
6. **Launch the domain agent as a named teammate** with `name: "optimizer"` and `team_name: "codeflash-session"`:
```
Resume the optimization session.
## Session State
<HANDOFF.md contents>
## Experiment History
<results.tsv contents>
## Project Conventions (from CLAUDE.md)
<CLAUDE.md contents if it exists>
## Optimization Profile
<codeflash_profile.md contents if it exists>
## Conventions
<conventions.md contents if it exists>
## Learnings from Previous Sessions
<learnings.md contents if it exists>
## Domain Knowledge
<guide.md contents if loaded>
```
2026-04-09 08:36:01 +00:00
7. **Coordinate.** Enter the same coordination loop from step 14 of Start — stay alive and actively manage.
2026-04-03 22:36:50 +00:00
### Status
**If a team is active** (optimizer is running as a named teammate): Use `SendMessage(to: "optimizer", summary: "Status request", message: "Report your current status: experiments run, keeps/discards, current target, cumulative improvement.")` and relay the response.
**Otherwise** (no active team, or between sessions): Read `.codeflash/results.tsv` and `.codeflash/HANDOFF.md` and show:
- Total experiments run (keeps vs discards)
- Current branch and tag
- Best improvement achieved vs baseline
- What was planned next
Also check `TaskList` if a team exists — the task statuses show which phase the session is in.
### Scan
Quick cross-domain diagnosis before committing to a full optimization session. Profiles CPU, memory, import time, and async patterns in one pass.
1. **Verify branch state.** Run `git status` and `git branch --show-current`.
2026-04-09 08:36:01 +00:00
2. Run the **setup agent** (see Language Configuration) if `.codeflash/setup.md` doesn't exist.
3. Launch the **scan agent** (see Language Configuration) and **wait for it to complete**:
2026-04-03 22:36:50 +00:00
```
2026-04-09 08:36:01 +00:00
Agent(subagent_type: "<scan-agent>", prompt: "
2026-04-03 22:36:50 +00:00
Scan this project for performance issues.
## Environment
<.codeflash/setup.md contents>
## Scope
<user's target test or 'discover automatically' if not specified>
")
```
4. Read `.codeflash/scan-report.md`.
5. Present the ranked findings to the user with the domain recommendations. Ask: "Which domain(s) do you want to optimize? I can start a full session on any of these."
2026-04-09 08:36:01 +00:00
6. If the user picks domain(s), proceed to **Start** step 4 (create team) with the selected domain(s). Include the scan report in the domain agent's launch prompt under `## Scan Results`.
2026-04-03 22:36:50 +00:00
### Review
Independent deep-review of optimization changes. Can be triggered standalone (user asks to review a branch/PR) or as a post-session gate before cleanup.
#### Standalone review
When the user asks to review changes, a PR, or a branch — and no optimization session is active:
1. Launch **codeflash-review** and wait for it to complete:
```
Agent(subagent_type: "codeflash-review", prompt: "
Review the following: <user's request branch name, PR number, or 'current changes'>
## Environment
<.codeflash/setup.md contents if it exists>
## Project Conventions (from CLAUDE.md)
<CLAUDE.md contents if it exists>
## Optimization Profile
<codeflash_profile.md contents if it exists>
## Session Context
<.codeflash/results.tsv contents if it exists>
<.codeflash/HANDOFF.md contents if it exists>
")
```
2. When the review completes, relay the verdict and key findings to the user.
3. If verdict is BLOCK or REQUEST CHANGES, list the findings by severity.
4. Tell the user: "Full report at `.codeflash/review-report.md`."
#### Post-session review gate
When the domain agent sends `[complete]` and the user wants a review before merging (or if `conventions.md` specifies `## Review: required`):
1. Launch **codeflash-review** as a named teammate:
```
Agent(subagent_type: "codeflash-review", name: "reviewer",
2026-04-09 08:36:01 +00:00
team_name: "codeflash-session", run_in_background: true, prompt: "
2026-04-03 22:36:50 +00:00
Review the completed optimization session.
## Branch
<current branch> vs <base branch>
## Environment
<.codeflash/setup.md contents>
## Results
<.codeflash/results.tsv contents>
## Session State
<.codeflash/HANDOFF.md contents>
## Conventions
<.codeflash/conventions.md contents if it exists>
## Project Conventions (from CLAUDE.md)
<CLAUDE.md contents if it exists>
## Optimization Profile
<codeflash_profile.md contents if it exists>
")
```
2. Wait for the review to complete. Handle `[review]` messages (see Team Coordination).
3. Relay the verdict to the user:
- **APPROVE**: Proceed to Cleanup.
- **REQUEST CHANGES**: Show findings, ask the user if they want to fix issues (re-launch optimizer with the findings) or proceed anyway.
- **BLOCK**: Show blocking findings. Do NOT proceed to cleanup until resolved.
### Cleanup
When the user says "done", "clean up", or "finish session", or when the domain agent sends a `[complete]` message:
2026-04-09 08:36:01 +00:00
1. **Generate changelog.** Before cleaning up, generate `.codeflash/changelog.md` (see "## Changelog Generation" below). For multi-domain sessions, do this after the merge step.
2026-04-03 22:36:50 +00:00
2. **Shut down teammates.** Send `SendMessage(to: "optimizer", message: {type: "shutdown_request"})` and `SendMessage(to: "researcher", message: {type: "shutdown_request"})`. Wait for confirmation. If multiple domain agents are running, shut down each one.
3. **Delete team.** `TeamDelete` to clean up team config and task list.
4. **Preserve** `.codeflash/learnings.md`, `.codeflash/results.tsv`, and `.codeflash/changelog.md` (useful for future sessions and PR creation).
2026-04-09 08:36:01 +00:00
5. **Delete transient files**: `HANDOFF.md`, `setup.md`, `conventions.md`, and any benchmark scripts in `.codeflash/`.
2026-04-03 22:36:50 +00:00
6. If `.codeflash/` is now empty (no learnings, results, or changelog), remove the directory entirely.
7. Delete `.claude/agent-memory/` if it exists in the project directory (agent memory is per-session, not meant to persist).
## Maintainer Feedback
When the user shares maintainer feedback, PR review comments, or project-specific conventions (e.g. from Slack, GitHub reviews, or conversation), write them to `.codeflash/conventions.md` — NOT to auto-memory. The agents read `conventions.md` at startup and follow it as binding constraints.
Append to the file if it already exists. Use clear headings per topic (e.g. `## Pylint Policy`, `## Profiling`, `## Code Style`).
## Cross-Session Learnings
2026-04-09 08:36:01 +00:00
When domain agents discover non-obvious technical facts about the codebase, they record them in HANDOFF.md's "Key Discoveries" section. After a session ends or plateau is reached, distill the most important discoveries into `.codeflash/learnings.md` so future sessions across ALL domains can benefit.
2026-04-03 22:36:50 +00:00
Learnings.md is NOT a session log — it's a curated set of facts that prevent future sessions from repeating dead ends. Each entry should be:
```
## <Short title>
<Specific technical detail with evidence. Include what was tried and why it didn't work.>
```
Read learnings.md at every session start and include it in the domain agent's launch prompt.
## Team Coordination
As team lead, you manage the optimization session through the team infrastructure. After launching the domain agent as a named teammate, you stay alive to coordinate.
### Receiving and relaying progress messages
Domain agents send structured progress messages at key milestones. **Actively relay progress to the user** — don't just track silently. The user should see what's happening without having to ask.
**Always relay to the user immediately:**
- **`[baseline]`**: Print the ranked targets summary. The session is underway.
- **`[progress]`**: **Print directly to the user.** This is the periodic summary (sent every 3 experiments) — it's designed to be user-facing. Print it as-is.
- **`[milestone]`**: Print the cumulative results to the user.
- **`[complete]`**: Print the final summary to the user. If this is a multi-domain session and other agents are still running, wait for all to complete before proceeding to "Merging results". If this is the last (or only) agent, proceed to Cleanup.
- **`[stuck]`**: Print to the user and check if you can suggest a different approach, or ask the user for guidance.
- **`[strategy]`** (deep agent only): Print strategy pivots — these are informative and show the agent's reasoning.
- **`[review]`** (review agent): Print the verdict and findings summary. If BLOCKING findings are reported mid-review, relay immediately — don't wait for the full review to complete.
**Track silently (do not relay unless the user asks):**
- **`[experiment N]`**: Track keeps vs discards internally. Only flag to the user if the pattern suggests the agent is off-track (e.g., 5+ consecutive discards).
- **`[cross-domain]`**: Log the signal. See "### Cross-Domain Handoff" below.
- **`[modified]`**: Internal — for researcher coordination only.
### Relaying user feedback
If you receive a message from the user (or from the skill via SendMessage), relay it to the running optimizer:
```
SendMessage(to: "optimizer", summary: "User feedback",
message: "<user's instruction e.g., skip function X, focus on file Y, stop after next experiment>")
```
### Cross-domain handoff
When a domain agent discovers something outside its scope, it sends a `[cross-domain]` message:
```
[cross-domain] domain: memory | signal: load_model() allocates 1.2 GiB in a function that's also a CPU hotspot
```
When you receive this:
1. **Log it.** Note the cross-domain signal for the session record.
2. **Decide whether to act.** If the current optimizer is still making progress, queue the signal — don't interrupt a productive session. If the optimizer is near plateau or stuck, this is a good pivot point.
3. **Spawn a secondary agent.** When ready, launch the appropriate domain agent in a worktree:
```
2026-04-09 08:36:01 +00:00
Agent(subagent_type: "<agent-prefix><target-domain>", name: "<domain>-optimizer",
team_name: "codeflash-session", run_in_background: true, isolation: "worktree", prompt: "
2026-04-03 22:36:50 +00:00
Cross-domain handoff from <source-domain> optimizer.
Signal: <the cross-domain message>
Focus on: <the specific target identified>
...")
```
Also spawn a researcher for the new agent if the workload warrants it.
4. **Track both.** Create tasks for the secondary domain and coordinate both agents' progress.
### Task tracking
Check `TaskList` to see the current phase. Update tasks when you observe phase transitions from the optimizer's messages.
## Multi-Domain Coordination
**With the deep agent as default, multi-domain coordination is usually handled by the deep agent itself** — it profiles all dimensions, identifies which targets are cross-domain vs single-domain, and dispatches domain agents as needed. The router does NOT need to detect multi-domain signals or launch parallel agents for the common case.
The sections below only apply when a user explicitly requested single-domain agents and you need to coordinate them, or when resuming a legacy multi-domain session.
2026-04-09 08:36:01 +00:00
### Launching parallel domain agents
2026-04-03 22:36:50 +00:00
Each domain agent gets its own worktree via `isolation: "worktree"`:
```
2026-04-09 08:36:01 +00:00
Agent(subagent_type: "<agent-prefix>cpu", name: "cpu-optimizer",
team_name: "codeflash-session", run_in_background: true, isolation: "worktree", prompt: "...")
Agent(subagent_type: "<agent-prefix>memory", name: "mem-optimizer",
team_name: "codeflash-session", run_in_background: true, isolation: "worktree", prompt: "...")
2026-04-03 22:36:50 +00:00
```
Each agent:
- Works in an isolated copy of the repo (separate worktree directory)
- Creates its own branch (`codeflash/optimize`)
- Has its own `.codeflash/` state — no conflicts
- Commits independently to its own branch
- Sends progress to you (the router) via `SendMessage`
### Merging results
When all domain agents send `[complete]`, coordinate the merge of their branches back to the base branch.
#### 1. Collect branch info
For each completed domain agent, gather:
```bash
git branch --list 'codeflash/*'
git log <base>..<branch> --oneline
git diff <base>..<branch> --name-only
```
Build a summary table:
```
| Branch | Domain | Keeps | Files changed | Improvement |
```
#### 2. Conflict analysis
Check whether domain branches touch overlapping files:
```bash
git diff <base>...<branch-A> --name-only > /tmp/files-a.txt
git diff <base>...<branch-B> --name-only > /tmp/files-b.txt
comm -12 <(sort /tmp/files-a.txt) <(sort /tmp/files-b.txt)
```
Classify the result:
- **No overlap**: Branches are independent — safe to merge in any order.
2026-04-09 08:36:01 +00:00
- **Overlap in different functions**: Likely clean merge. Flag as "likely clean" but verify.
- **Overlap in same functions**: Conflicting changes. Requires manual resolution or cherry-picking.
2026-04-03 22:36:50 +00:00
#### 3. Suggest merge order
Present the analysis to the user with a recommended strategy:
**If no file overlap:**
```
All <N> domain branches are independent (no shared files). Safe to merge in any order.
Recommended: merge in order of impact (highest improvement first).
Proceed with this merge order?
```
**If overlap exists but in different hunks:**
```
Branches share <N> files but changes are in different functions. Git should auto-merge cleanly.
Recommended: merge highest-impact branch first, then rebase the other before merging.
Proceed?
```
**If conflicting overlap:**
```
Branches conflict in <files>:
2026-04-09 08:36:01 +00:00
- <branch-A> and <branch-B> both modify function_name() in file.py
2026-04-03 22:36:50 +00:00
Options:
2026-04-09 08:36:01 +00:00
a) Merge <branch-A> first, then manually resolve <branch-B> conflicts
b) Cherry-pick non-conflicting commits from <branch-B>, skip conflicting ones
2026-04-03 22:36:50 +00:00
c) Keep both branches for separate PRs
Which approach?
```
#### 4. Execute merge
Only after user confirms. For each branch in the agreed order:
```bash
git checkout <base-branch>
git merge <domain-branch> --no-ff -m "perf(<domain>): merge <N> optimizations from codeflash session"
```
2026-04-09 08:36:01 +00:00
If merge conflicts occur, stop and show the conflicts to the user. Do NOT resolve conflicts automatically.
2026-04-03 22:36:50 +00:00
After all merges complete, run the full test suite to verify nothing regressed.
## Default Routing: Deep Agent
The deep agent is the **primary optimizer** for all new sessions. It profiles across CPU, memory, and async dimensions jointly, identifies cross-domain interactions, and dispatches domain-specialist agents when it finds single-domain targets. This means the router doesn't need to guess the domain — the deep agent figures it out from profiling evidence.
### When to override with a domain agent
Only route directly to a domain agent when all of these are true:
1. The user **explicitly** names a single domain (e.g., "just fix the memory leak", "only optimize async")
2. The request **excludes** cross-domain reasoning (no ambiguity about other dimensions)
3. The user has a specific, narrow target in mind
If any of these are not met, use deep.
| Situation | Route to |
|-----------|----------|
| General "make it faster", "optimize this" | **Deep** (default) |
| User explicitly asks for one domain only | Domain agent |
| Independent problems in different domains | **Deep** (it dispatches domain agents itself) |
| Domain agent plateaued and user wants more | **Deep** (escalation) |
| Scan report with findings in any combination of domains | **Deep** (default) |
### Launching deep mode
For a **new session**, follow the standard setup steps (1-9 from Start), then:
1. **Launch the deep agent as a named teammate:**
```
2026-04-09 08:36:01 +00:00
Agent(subagent_type: "<deep-agent>", name: "optimizer",
team_name: "codeflash-session", run_in_background: true, prompt: "
2026-04-03 22:36:50 +00:00
<If autonomous mode: include the AUTONOMOUS MODE directive>
Begin a deep optimization session. The user wants: <user's request>
## Environment
<.codeflash/setup.md contents>
## Project Conventions (from CLAUDE.md)
<CLAUDE.md contents if it exists>
## Optimization Profile
<codeflash_profile.md contents if it exists>
## Conventions
<conventions.md contents if it exists>
## Learnings from Previous Sessions
<learnings.md contents if it exists>
## Pre-existing Test Failures
<list of failing tests, if any>
## Library Research
<context7 findings summary>
")
```
2026-04-09 08:36:01 +00:00
2. **Do NOT launch a researcher or domain agents alongside it.** The deep agent manages its own team — it creates its own team, dispatches domain agents with cross-domain context, and spawns researchers as needed.
3. **Coordinate as normal** — receive progress messages, relay user feedback, actively manage per the coordination loop.
2026-04-03 22:36:50 +00:00
### Escalation from single-domain override plateau
2026-04-09 08:36:01 +00:00
This only applies when you launched a single-domain agent (because the user explicitly requested one domain) and it plateaued.
2026-04-03 22:36:50 +00:00
When a single-domain agent sends `[complete]` and the user wants to continue:
1. Read the domain agent's `.codeflash/results.tsv` and `.codeflash/HANDOFF.md`.
2. Tell the user: "The <domain> optimizer plateaued after <N> experiments (<keeps> kept). I'll switch to the deep optimizer to look for cross-domain interactions the <domain> agent couldn't see."
2026-04-09 08:36:01 +00:00
3. Launch the deep agent with the domain agent's context:
2026-04-03 22:36:50 +00:00
```
2026-04-09 08:36:01 +00:00
Agent(subagent_type: "<deep-agent>", name: "optimizer",
team_name: "codeflash-session", run_in_background: true, prompt: "
2026-04-03 22:36:50 +00:00
Escalating from <domain> single-domain session.
## Previous Session Results
<results.tsv contents>
## Domain Agent's Plateau Analysis
<HANDOFF.md contents especially plateau documentation and key discoveries>
## What Was Tried
<Summary of approaches that worked and failed>
## Conventions
<conventions.md contents if it exists>
## Learnings
<learnings.md contents if it exists>
The <domain> agent was launched as a single-domain override and has
plateaued. Profile all dimensions jointly to find cross-domain
interactions and approaches the single-domain agent couldn't reach.
")
```
2026-04-09 08:36:01 +00:00
## Changelog Generation
2026-04-03 22:36:50 +00:00
After a session completes (single-domain or post-merge for multi-domain), generate `.codeflash/changelog.md` from the experiment history. This file can be used directly as a PR description body.
2026-04-09 08:36:01 +00:00
### Input sources
2026-04-03 22:36:50 +00:00
1. **`.codeflash/results.tsv`** from each domain branch (or merged branch) — lists every experiment with status, metrics, and pattern.
2. **`git log <base>..<branch> --oneline`** — commit messages for kept optimizations.
3. **`.codeflash/HANDOFF.md`** — key discoveries and session context.
2026-04-09 08:36:01 +00:00
### Generation steps
2026-04-03 22:36:50 +00:00
2026-04-09 08:36:01 +00:00
1. **Parse results.tsv.** Read the file and filter to `status=keep` rows only. Group by domain.
2026-04-03 22:36:50 +00:00
2. **Build the changelog.** Write `.codeflash/changelog.md` with this structure:
```markdown
## Summary
<1-3 sentences: what was optimized and why, derived from the original user request>
## Optimizations
### <Domain name> (`<branch-name>`)
| # | Target | Pattern | Before | After | Improvement |
|---|--------|---------|--------|-------|-------------|
| 1 | function_name | antipattern-name | 2.3s | 0.8s | 65% faster |
| 2 | function_name | antipattern-name | 450 MiB | 280 MiB | 38% less memory |
**Commits:**
- `abc1234` — Replace list.pop(0) with deque in score_records
- `def5678` — Use __slots__ on SensorReading dataclass
<Repeat for each domain if multi-domain>
## Key Discoveries
2026-04-09 08:36:01 +00:00
<Non-obvious findings from HANDOFF.md that reviewers should know>
2026-04-03 22:36:50 +00:00
## Test Plan
- [x] All existing tests pass after each optimization
- [x] No performance regressions in non-targeted benchmarks
<Add guard command results if a guard was configured>
## Session Stats
- **Experiments**: <total> (<keeps> kept, <discards> discarded)
- **Session duration**: <from first to last commit timestamp>
- **Domains**: <list of domains optimized>
```
3. **Metric formatting.** Use the appropriate units per domain:
- CPU: seconds, speedup % (e.g., "2.3s → 0.8s, 65% faster")
- Memory: MiB, reduction % (e.g., "450 MiB → 280 MiB, 38% less")
- Async: latency ms + throughput req/s (e.g., "p50: 120ms → 45ms, 62% faster")
- Structure: import time seconds, dep count (e.g., "1.2s → 0.4s, 67% faster import")
4. **Print the result.** After writing the file, print:
```
[changelog] Written to .codeflash/changelog.md — <N> optimizations across <M> domain(s)
```
5. **Usage.** Tell the user: "The changelog is ready at `.codeflash/changelog.md`. You can use it as a PR description body when you're ready to open a PR."