Task Agent
The ephemeral agent that executes coding tasks — searching code, reading files, generating changes with Claude, parsing structured output, creating branches, and opening pull requests.
Task Agent
The Task Agent is the coding workhorse of CodeSpar. It is an ephemeral agent spawned whenever a user requests a code change — from fixing a bug to adding a feature. It reads your codebase, generates changes using Claude, creates a branch, commits the changes, and opens a pull request.
Characteristics
| Property | Value |
|---|---|
| Lifecycle | Ephemeral — spawned per task, terminates on completion |
| Spawned by | Project Agent, on fix, implement, refactor commands |
| AI Model | Claude Sonnet (configurable via TASK_MODEL) |
| Default Model | claude-sonnet-4-20250514 |
| Color | Agent Green (#10B981) |
ClaudeBridge
The Task Agent communicates with Claude through a ClaudeBridge — a wrapper around the Anthropic Messages API that manages context, system prompts, and response parsing:
Two Execution Modes
Generic Mode: execute()
For tasks that do not require repository access — answering questions, generating snippets, explaining concepts:
This mode is used when no repository is linked or the task does not involve code changes.
Repository Mode: executeWithRepo()
For tasks that modify code in a linked GitHub repository. This is the primary mode and follows a structured, six-step flow. The rest of this page documents this flow in detail.
The executeWithRepo Flow — Step by Step
When a user sends a command like @codespar fix the auth timeout in login.ts, the Task Agent executes the following pipeline:
Step 1: Search for Relevant Code
The agent searches the repository for files relevant to the task using keyword extraction and the GitHub Code Search API.
Keyword Extraction
Keywords are extracted from the task instruction by removing common stop words and keeping significant terms:
These keywords are joined and passed to GitHub's code search:
File Selection Rules
- Maximum files: 5 files are selected from search results
- Maximum size per file: 20KB each (files larger than 20KB are truncated or skipped)
- Priority: Files that match more keywords rank higher
Fallback: File Tree Search
If the GitHub Code Search API returns no results (e.g., the keywords are too generic, or the repository is small/new), the agent falls back to using the repository file tree:
The fallback looks for .ts, .js, .tsx, .jsx, .py, .go, .rs, .java, .rb, and .php files, prioritizing files in the src/ directory.
Step 2: Read File Contents
Once relevant files are identified, the agent fetches their full content via the GitHub API:
Each file is capped at 20KB. For files near or over this limit, the content is truncated with a comment indicating the truncation.
Step 3: Generate Changes with Claude
The agent constructs a system prompt and sends it to Claude along with the task description and file contents.
System Prompt Format
The system prompt tells Claude how to format its response using the ===FILE:=== block convention:
User Prompt Format
The user prompt includes the task and all file contents:
Claude's Response
Claude returns one or more ===FILE:=== blocks with the complete modified file contents:
Step 4: Parse ===FILE:=== Blocks
The agent parses Claude's response to extract file paths and contents using a regex:
The regex /===FILE:\s*(.+?)===\n([\s\S]*?)===END===/g captures:
- Group 1: The file path (e.g.,
src/config/auth.config.ts) - Group 2: The complete file content between the
===FILE:===and===END===markers
If Claude's response contains no ===FILE:=== blocks (e.g., it responded with an explanation instead of code), the agent reports that no code changes were generated.
Step 5: Create Branch and Commit
The agent creates a new branch from the repository's default branch:
Branch Naming Convention
Branches are named codespar/<taskId>, where <taskId> is the unique identifier assigned to the task when it is created. Examples:
This naming convention makes it easy to identify CodeSpar-generated branches and clean them up after merge.
Step 6: Open Pull Request
The agent creates a pull request with a detailed description:
The PR description includes:
- The original task instruction (as a quote block)
- A summary of changes (generated by Claude)
- A list of all modified files
- Attribution to CodeSpar
Complete Flow Diagram
Example Interaction
Simulation Mode
When no Anthropic API key is configured (ANTHROPIC_API_KEY is not set), the Task Agent falls back to simulation mode. It goes through the same flow — searching code, reading files, creating branches, opening PRs — but returns simulated code changes instead of calling Claude:
Simulation mode is useful for:
- Testing the full flow (branch creation, PR opening) without incurring API costs
- Development and debugging of the Task Agent itself
- CI/CD pipelines that need to validate the integration without real API calls
In simulation mode, branches and PRs are still created on GitHub (if GITHUB_TOKEN is set), but the PR body will indicate that changes are simulated.
Environment Variables
| Variable | Required | Default | Description |
|---|---|---|---|
ANTHROPIC_API_KEY | No | — | Anthropic API key. Without it, runs in simulation mode. |
TASK_MODEL | No | claude-sonnet-4-20250514 | Claude model to use for code generation. |
GITHUB_TOKEN | Yes | — | GitHub token with repo scope for branch/PR operations. |
Configuration Tips
Model Selection
The default model (claude-sonnet-4-20250514) balances speed and quality for most coding tasks. For complex refactors or architecture-level changes, you can switch to a more capable model:
Context Window Management
The Task Agent is selective about which files it sends to Claude to stay within context limits. It prioritizes:
- Files directly mentioned in the task description (e.g., "fix login.ts")
- Files found via code search matching task keywords
- Maximum 5 files, each capped at 20KB
- Total context stays well under the model's context window
If your task requires changes across many files, consider breaking it into smaller, focused tasks (e.g., "fix the auth timeout" then "update the auth tests").
Next Steps
- Project Agent — The persistent agent that spawns Task Agents
- Review Agent — Automated PR review
- Command Reference —
fix,instruct, and other commands that trigger Task Agent