|
| 1 | +# Custom Executor Guide |
| 2 | + |
| 3 | +Herald's executor architecture is pluggable. While the default executor is `claude-code` (Claude Code CLI), you can implement adapters for any CLI tool — Codex, Gemini CLI, Aider, or your own wrapper. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +``` |
| 8 | +executor/ |
| 9 | +├── executor.go # Interface + Capabilities + Registry |
| 10 | +├── registry.go # Register/Get/Available |
| 11 | +├── kill.go # GracefulKill (shared POSIX utility) |
| 12 | +└── claude/ |
| 13 | + ├── claude.go # Claude Code implementation (init() auto-registers as "claude-code") |
| 14 | + └── stream.go # stream-json output parsing |
| 15 | +``` |
| 16 | + |
| 17 | +The registry pattern uses Go `init()` functions for zero-config registration. Each executor lives in its own sub-package under `internal/executor/`. |
| 18 | + |
| 19 | +## Implementing an Executor |
| 20 | + |
| 21 | +### 1. Create a sub-package |
| 22 | + |
| 23 | +``` |
| 24 | +internal/executor/mybackend/ |
| 25 | +└── mybackend.go |
| 26 | +``` |
| 27 | + |
| 28 | +### 2. Implement the interface |
| 29 | + |
| 30 | +```go |
| 31 | +package mybackend |
| 32 | + |
| 33 | +import ( |
| 34 | + "context" |
| 35 | + |
| 36 | + "github.com/btouchard/herald/internal/executor" |
| 37 | +) |
| 38 | + |
| 39 | +const name = "my-backend" |
| 40 | + |
| 41 | +func init() { |
| 42 | + executor.Register(name, factory) |
| 43 | +} |
| 44 | + |
| 45 | +func factory(cfg map[string]any) (executor.Executor, error) { |
| 46 | + binaryPath, _ := cfg["binary_path"].(string) |
| 47 | + if binaryPath == "" { |
| 48 | + binaryPath = "my-cli" // default binary name |
| 49 | + } |
| 50 | + return &Executor{binaryPath: binaryPath}, nil |
| 51 | +} |
| 52 | + |
| 53 | +type Executor struct { |
| 54 | + binaryPath string |
| 55 | +} |
| 56 | + |
| 57 | +func (e *Executor) Capabilities() executor.Capabilities { |
| 58 | + return executor.Capabilities{ |
| 59 | + Name: name, |
| 60 | + Version: "0.1.0", |
| 61 | + SupportsSession: false, // set true if your CLI supports session resumption |
| 62 | + SupportsModel: false, // set true if your CLI supports model selection |
| 63 | + SupportsToolList: false, // set true if your CLI supports tool restrictions |
| 64 | + SupportsDryRun: false, // set true if your CLI supports dry-run/plan mode |
| 65 | + SupportsStreaming: true, // set true if you stream progress events |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (e *Executor) Execute(ctx context.Context, req executor.Request, onProgress executor.ProgressFunc) (*executor.Result, error) { |
| 70 | + // Build and run your CLI command here. |
| 71 | + // Use req.Prompt, req.ProjectPath, req.TimeoutMinutes, etc. |
| 72 | + // Call onProgress("progress", "message") to report status. |
| 73 | + // Return an executor.Result with output, cost, turns, etc. |
| 74 | + |
| 75 | + return &executor.Result{ |
| 76 | + Output: "task completed", |
| 77 | + ExitCode: 0, |
| 78 | + }, nil |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### 3. Register via blank import |
| 83 | + |
| 84 | +In `cmd/herald/main.go`, add a blank import so the `init()` function runs: |
| 85 | + |
| 86 | +```go |
| 87 | +import ( |
| 88 | + _ "github.com/btouchard/herald/internal/executor/claude" |
| 89 | + _ "github.com/btouchard/herald/internal/executor/mybackend" |
| 90 | +) |
| 91 | +``` |
| 92 | + |
| 93 | +### 4. Select via config |
| 94 | + |
| 95 | +In `herald.yaml`: |
| 96 | + |
| 97 | +```yaml |
| 98 | +execution: |
| 99 | + executor: "my-backend" |
| 100 | +``` |
| 101 | +
|
| 102 | +If omitted, defaults to `"claude-code"`. |
| 103 | +
|
| 104 | +## Capabilities |
| 105 | +
|
| 106 | +The `Capabilities` struct tells MCP handlers what your executor supports. When a user requests a feature that your executor doesn't support (e.g., session resumption), Herald shows a warning in the response — not an error. |
| 107 | + |
| 108 | +| Field | Description | |
| 109 | +|---|---| |
| 110 | +| `SupportsSession` | Can resume previous conversations via `session_id` | |
| 111 | +| `SupportsModel` | Can override the model per task | |
| 112 | +| `SupportsToolList` | Can restrict which tools the CLI uses | |
| 113 | +| `SupportsDryRun` | Can run in plan-only mode without making changes | |
| 114 | +| `SupportsStreaming` | Emits progress events during execution | |
| 115 | +| `Name` | Display name shown in MCP responses | |
| 116 | +| `Version` | Executor version string | |
| 117 | + |
| 118 | +## Request and Result |
| 119 | + |
| 120 | +### Request fields |
| 121 | + |
| 122 | +| Field | Description | Capability required | |
| 123 | +|---|---|---| |
| 124 | +| `TaskID` | Unique task identifier | Always available | |
| 125 | +| `Prompt` | User prompt text | Always available | |
| 126 | +| `ProjectPath` | Absolute path to project directory | Always available | |
| 127 | +| `TimeoutMinutes` | Maximum execution time | Always available | |
| 128 | +| `Env` | Environment variables to set | Always available | |
| 129 | +| `SessionID` | Session to resume | `SupportsSession` | |
| 130 | +| `Model` | Model override | `SupportsModel` | |
| 131 | +| `AllowedTools` | Tool restrictions | `SupportsToolList` | |
| 132 | +| `DryRun` | Plan-only mode | `SupportsDryRun` | |
| 133 | + |
| 134 | +Fields that require capabilities your executor doesn't support are silently ignored. Herald warns the user in the MCP response. |
| 135 | + |
| 136 | +### Result fields |
| 137 | + |
| 138 | +| Field | Description | |
| 139 | +|---|---| |
| 140 | +| `SessionID` | Session ID for future resumption (if supported) | |
| 141 | +| `Output` | Full text output from the CLI | |
| 142 | +| `CostUSD` | Estimated cost in USD (0 if not available) | |
| 143 | +| `Turns` | Number of conversation turns | |
| 144 | +| `Duration` | Execution duration | |
| 145 | +| `ExitCode` | Process exit code (0 = success) | |
| 146 | + |
| 147 | +## Progress reporting |
| 148 | + |
| 149 | +Call `onProgress` during execution to update task status in real-time: |
| 150 | + |
| 151 | +```go |
| 152 | +onProgress("progress", "Reading project files...") |
| 153 | +onProgress("progress", "Generating code changes...") |
| 154 | +onProgress("result", "Task completed successfully") |
| 155 | +``` |
| 156 | + |
| 157 | +Progress messages appear in `check_task` responses and MCP push notifications. |
| 158 | + |
| 159 | +## Factory configuration |
| 160 | + |
| 161 | +The factory receives a `map[string]any` built from Herald's config. The Claude Code executor uses these keys: |
| 162 | + |
| 163 | +- `claude_path` — path to the CLI binary |
| 164 | +- `work_dir` — working directory for task files |
| 165 | +- `env` — environment variables map |
| 166 | + |
| 167 | +You can define your own keys. They are passed through from the execution config. |
| 168 | + |
| 169 | +## Testing |
| 170 | + |
| 171 | +Test your executor with mock commands. See `internal/executor/claude/claude_test.go` for patterns: |
| 172 | + |
| 173 | +```go |
| 174 | +func TestMyExecutor_Execute_Success(t *testing.T) { |
| 175 | + exec := &Executor{binaryPath: "/usr/bin/echo"} |
| 176 | + result, err := exec.Execute(context.Background(), executor.Request{ |
| 177 | + TaskID: "herald-test01", |
| 178 | + Prompt: "hello", |
| 179 | + ProjectPath: t.TempDir(), |
| 180 | + }, func(eventType, message string) {}) |
| 181 | +
|
| 182 | + require.NoError(t, err) |
| 183 | + assert.Equal(t, 0, result.ExitCode) |
| 184 | +} |
| 185 | +``` |
0 commit comments