The System Executor¶
If the Orchestrator is the state machine, the SystemExecutor is the engine that physically mutates the host. It is responsible for translating the declarative EnvironmentManifest into imperative disk operations and shell commands.
By strictly confining all physical mutations to this single class, Protostar ensures that partial failures (such as a missing dependency or a syntax error in an existing file) do not leave the workspace in a fragmented, irrecoverable state.
-
Abstract Syntax Tree (AST) Preservation
Configuration files (like
pyproject.toml) are not blindly overwritten or manipulated via fragile regex. They are parsed into ASTs, deeply merged, and serialized back to disk, preserving all user comments and structural formatting. -
Pre-Execution Validation
Before a single directory is created, the Executor validates the syntax of all target files. If a user has a malformed TOML file, execution halts immediately rather than failing halfway through the sequence.
-
Subprocess Isolation
All shell executions (e.g.,
uv add,git init) are routed through a sandboxed wrapper. Standard output and error streams are captured, preventing silent failures and ensuring critical telemetry is preserved for debugging.
The Execution Topology¶
The SystemExecutor processes the manifest sequentially. This exact chronological ordering is critical to prevent race conditions (e.g., attempting to append configurations to a pyproject.toml before uv init has generated it).
flowchart TD
classDef core fill:#1e293b,stroke:#00e5ff,stroke-width:2px,color:#fff;
classDef io_file fill:#334155,stroke:#475569,stroke-width:1px,color:#e2e8f0;
classDef io_shell fill:#0f172a,stroke:#3b82f6,stroke-width:1px,color:#e2e8f0;
Start([Execute Manifest]) --> Prep
subgraph Prep [Initialization & Base Scaffolding]
direction LR
V[1. Validate AST Targets]:::io_file --> D[2. Scaffold Directories]:::io_file
D --> I[3. Write Injected Files]:::io_file
I --> PC[4. Write pre-commit Config]:::io_file
end
Prep --> ST[5. Execute System Tasks]:::io_shell
ST --> Synthesis
subgraph Synthesis [Late-Binding Configurations]
direction LR
AF[6. AST Merge & File Appends]:::io_file --> IG[7. Deduplicate Ignores]:::io_file
IG --> DOCK[8. Write Docker Artifacts]:::io_file
DOCK --> IDE[9. Write IDE Settings]:::io_file
end
Synthesis --> Runtime
subgraph Runtime [Dependency Resolution]
direction LR
DEP[10. Resolve Dependencies]:::io_shell --> PT[11. Execute Post-Install Tasks]:::io_shell
end
Runtime --> End([Execution Complete]):::core
AST Deep Merging¶
When merging arrays or configuration tables into existing TOML files, Protostar utilizes tomlkit to manipulate the Abstract Syntax Tree. This is handled by the recursive _deep_merge_tomlkit() method.
The merge behavior is governed by the orchestrator's resolved CollisionStrategy:
-
Merge (Default): The executor walks the AST, appending missing keys and extending
Array of Tables(AoT). Existing scalar values or sibling tables that are not explicitly targeted by the payload are safely ignored and preserved. -
Overwrite: The executor aggressively prunes the target. If the payload defines a specific table (e.g.,
[tool.ruff]), any existing scalar keys within that table on the host that do not exist in the payload are purged, forcing strict parity with Protostar's baseline.
Dynamic Python Version Resolution
During the file append phase, the executor dynamically resolves the target environment's Python version (scanning pyproject.toml, .venv/pyvenv.cfg, or the configuration fallback). Any {{PYTHON_VERSION}} tokens within the injected payloads are interpolated before the AST is evaluated.
Subprocess Telemetry¶
Directly calling subprocess.run in a CLI tool often leads to silent failures or messy interleaved terminal output. Protostar routes all system tasks and dependency resolutions through protostar.system.execute_subprocess.
This wrapper executes the command silently while capturing both stdout and stderr, and enforces granular task-level timeouts to prevent the orchestrator from blocking indefinitely on stalled network requests. If the process returns a non-zero exit code or exceeds its execution timeout, the streams are concatenated and raised within a RuntimeError. This ensures the Orchestrator can catch the failure and present the raw diagnostics to the user without dropping context.
Simulated Subprocess Telemetry Output
When a shell execution fails, the captured streams are formatted to pinpoint the exact failure mechanism:
API Reference¶
Core Interface: SystemExecutor
protostar.executor.SystemExecutor ¶
Executes the materialized environment manifest by mutating the local disk and shell.
Source code in src/protostar/executor.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | |
__init__ ¶
Initializes the executor with the target manifest state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest
|
EnvironmentManifest
|
The centralized state object containing all execution directives. |
required |
config
|
ProtostarConfig
|
The active Protostar configuration instance. |
required |
docker
|
bool
|
If True, scaffolds a .dockerignore from the manifest ignores. |
False
|
Source code in src/protostar/executor.py
execute ¶
Executes the materialized manifest in a deterministic sequence.
Source code in src/protostar/executor.py
Core Interface: execute_subprocess
protostar.system.execute_subprocess ¶
Executes a subprocess silently and captures telemetry on failure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
list[str]
|
The command and its arguments as a list of strings. |
required |
timeout
|
int | None
|
The maximum execution time in seconds. Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the subprocess returns a non-zero exit code or times out. |