claude_cli
claude_cli
¶
Claude CLI backend using subprocess.
Wraps the claude CLI command for running prompts.
Based on patterns from run-sheet-review.sh.
Security Note: This module uses asyncio.create_subprocess_exec() which is the safe subprocess method in Python - it does NOT use shell=True, so there is no shell injection risk. Arguments are passed as a list, not interpolated into a shell command string.
Progress Tracking: When a progress_callback is provided, this backend streams output in real-time and reports bytes/lines received periodically. This enables the CLI to show "Still running... 5.2KB received, 3m elapsed" during long executions.
Attributes¶
Classes¶
ClaudeCliBackend
¶
ClaudeCliBackend(skip_permissions=True, disable_mcp=True, output_format='text', cli_model=None, allowed_tools=None, system_prompt_file=None, working_directory=None, timeout_seconds=1800.0, progress_callback=None, progress_interval_seconds=5.0, cli_extra_args=None)
Bases: Backend
Run prompts via the Claude CLI.
Uses asyncio.create_subprocess_exec to invoke claude -p <prompt>.
This is shell-injection safe as arguments are passed as a list.
Initialize CLI backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skip_permissions
|
bool
|
Pass --dangerously-skip-permissions |
True
|
disable_mcp
|
bool
|
Disable MCP servers for faster execution (--strict-mcp-config) |
True
|
output_format
|
str
|
Output format (json, text, stream-json) |
'text'
|
cli_model
|
str | None
|
Model to use (--model flag), None uses default |
None
|
allowed_tools
|
list[str] | None
|
Restrict to specific tools (--allowedTools) |
None
|
system_prompt_file
|
Path | None
|
Custom system prompt file (--system-prompt) |
None
|
working_directory
|
Path | None
|
Working directory for running commands |
None
|
timeout_seconds
|
float
|
Maximum time allowed per prompt |
1800.0
|
progress_callback
|
ProgressCallback | None
|
Optional callback for progress updates during execution. Called with dict containing: bytes_received, lines_received, elapsed_seconds, phase. |
None
|
progress_interval_seconds
|
float
|
How often to call progress callback (default 5s). |
5.0
|
cli_extra_args
|
list[str] | None
|
Extra arguments to pass to claude CLI (escape hatch). |
None
|
Source code in src/marianne/backends/claude_cli.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
Functions¶
from_config
classmethod
¶
Create backend from configuration.
Source code in src/marianne/backends/claude_cli.py
apply_overrides
¶
Apply per-sheet overrides for the next execution.
Source code in src/marianne/backends/claude_cli.py
clear_overrides
¶
Restore original backend parameters after per-sheet execution.
Source code in src/marianne/backends/claude_cli.py
set_output_log_path
¶
Set base path for real-time output logging.
Called per-sheet by runner to enable streaming output to log files. This provides visibility into Claude's output during long executions.
Uses industry-standard separate files for stdout and stderr: - {path}.stdout.log - standard output - {path}.stderr.log - standard error
This enables clean tail -f monitoring without stream interleaving.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | None
|
Base path for log files (without extension), or None to disable. Example: workspace/logs/sheet-01 creates sheet-01.stdout.log |
required |
Source code in src/marianne/backends/claude_cli.py
set_preamble
¶
Set the dynamic preamble for the next execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preamble
|
str | None
|
Preamble text to prepend, or None to clear. |
required |
set_prompt_extensions
¶
Set prompt extensions for the next execution.
Extensions are additional directive blocks injected after the preamble. Called per-sheet by the runner to apply score-level and sheet-level extensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extensions
|
list[str]
|
List of extension text blocks. Empty strings are ignored. |
required |
Source code in src/marianne/backends/claude_cli.py
execute
async
¶
Execute a prompt (Backend protocol implementation).
Source code in src/marianne/backends/claude_cli.py
health_check
async
¶
Check if claude CLI is available and responsive.
Source code in src/marianne/backends/claude_cli.py
availability_check
async
¶
Check if the claude CLI binary exists and is executable.
Unlike health_check(), this does NOT send a prompt or consume API quota. Used after quota exhaustion waits.