Index
isolation
¶
Isolation module for parallel job execution.
This module provides worktree-based isolation and process sandboxing for Marianne jobs, enabling multiple jobs to execute in parallel without interfering with each other's file modifications or processes.
Key components: - GitWorktreeManager: Manages git worktree lifecycle - BwrapSandbox: Bubblewrap process-level isolation - ResourceLimits: Optional resource caps for sandboxed processes - WorktreeInfo: Information about a created worktree - WorktreeResult: Result of worktree operations - Exception classes for error handling
Classes¶
BwrapSandbox
¶
Wraps subprocess execution in a bubblewrap namespace.
Given a workspace path, shared directories, MCP sockets, and optional resource limits, produces the bwrap command line that sets up isolation boundaries. The conductor uses this to wrap agent subprocess execution.
Usage::
sandbox = BwrapSandbox(
workspace=Path("/tmp/agent-ws"),
shared_dirs=[Path("/tmp/shared/specs")],
mcp_sockets=[Path("/tmp/mzt/mcp/github.sock")],
resource_limits=ResourceLimits(memory_limit_mb=512),
)
cmd = sandbox.wrap_command(["python", "agent_script.py"])
# cmd is ["bwrap", "--bind", "/tmp/agent-ws", ...]
Source code in src/marianne/isolation/sandbox.py
Functions¶
wrap_command
¶
Prepend bwrap args to a command.
Produces a complete bwrap invocation that isolates the inner command in a namespace with the configured bind mounts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
list[str]
|
The command to execute inside the sandbox. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Full bwrap command line as a list of strings. |
Source code in src/marianne/isolation/sandbox.py
is_available
async
staticmethod
¶
Check if bwrap is installed and runnable.
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in src/marianne/isolation/sandbox.py
ResourceLimits
dataclass
¶
Optional resource caps for sandbox processes.
These are NOT enforced by bwrap itself — they are metadata consumed by the conductor's resource governance layer (systemd-run, prlimit). BwrapSandbox stores them for the caller to apply separately.
Attributes¶
memory_limit_mb
class-attribute
instance-attribute
¶
Maximum memory in MB. None means no cap.
cpu_quota_percent
class-attribute
instance-attribute
¶
CPU quota as a percentage (e.g. 50 = 50%%). None means no cap.
pid_limit
class-attribute
instance-attribute
¶
Maximum number of PIDs. None means no cap.
BranchExistsError
¶
Bases: WorktreeError
Raised when target branch already exists.
GitWorktreeManager
¶
Manages git worktree lifecycle for isolated execution.
This class handles all git worktree operations asynchronously, providing isolation for parallel job execution. Each job gets its own worktree with a dedicated branch, preventing race conditions when multiple AI agents modify code simultaneously.
Usage
manager = GitWorktreeManager(Path("/path/to/repo")) result = await manager.create_worktree( job_id="review-abc123", source_branch="main", ) if result.success: # Execute job in result.worktree.path ... await manager.remove_worktree(result.worktree.path)
Initialize the worktree manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_path
|
Path
|
Path to the git repository root. |
required |
Source code in src/marianne/isolation/worktree.py
Functions¶
is_git_repository
¶
Check if the manager's base path is a git repository.
Returns:
| Type | Description |
|---|---|
bool
|
True if base path is inside a git repository. |
Source code in src/marianne/isolation/worktree.py
create_worktree_detached
async
¶
Create isolated worktree in detached HEAD mode.
Unlike create_worktree(), this creates a worktree without a branch, allowing multiple worktrees to start from the same commit without branch locking conflicts. This is the preferred method for parallel job execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_id
|
str
|
Unique job identifier for worktree naming. |
required |
source_ref
|
str | None
|
Commit/branch to base worktree on (default: HEAD). |
None
|
worktree_base
|
Path | None
|
Directory for worktrees (default: repo/.worktrees). |
None
|
lock
|
bool
|
Whether to lock worktree after creation (default: True). |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
WorktreeResult
|
WorktreeResult with worktree info on success, error on failure. |
|
Note |
WorktreeResult
|
WorktreeInfo.branch will be "(detached)" for detached HEAD. |
Raises:
| Type | Description |
|---|---|
NotGitRepositoryError
|
If not in a git repository. |
WorktreeCreationError
|
If worktree cannot be created. |
Source code in src/marianne/isolation/worktree.py
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 | |
create_worktree
async
¶
create_worktree(job_id, source_branch=None, branch_prefix='marianne', worktree_base=None, lock=True)
Create isolated worktree for job execution.
Creates a new git worktree with a dedicated branch for the job. The worktree provides an isolated working directory, index, and HEAD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_id
|
str
|
Unique job identifier for worktree and branch naming. |
required |
source_branch
|
str | None
|
Branch to base worktree on (default: HEAD). |
None
|
branch_prefix
|
str
|
Prefix for branch name (default: "marianne"). |
'marianne'
|
worktree_base
|
Path | None
|
Directory for worktrees (default: repo/.worktrees). |
None
|
lock
|
bool
|
Whether to lock worktree after creation (default: True). |
True
|
Returns:
| Type | Description |
|---|---|
WorktreeResult
|
WorktreeResult with worktree info on success, error on failure. |
Source code in src/marianne/isolation/worktree.py
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 | |
remove_worktree
async
¶
Remove worktree and optionally its branch.
Removes the worktree directory and cleans up git metadata. The associated branch is preserved by default for review/merge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worktree_path
|
Path
|
Path to the worktree to remove. |
required |
force
|
bool
|
Force removal even if worktree is dirty (default: True). |
True
|
delete_branch
|
bool
|
Also delete the associated branch (default: False). |
False
|
Returns:
| Type | Description |
|---|---|
WorktreeResult
|
WorktreeResult indicating success or failure. |
Source code in src/marianne/isolation/worktree.py
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 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
lock_worktree
async
¶
Lock worktree to prevent accidental removal.
Locking prevents 'git worktree remove' and 'git worktree prune' from affecting this worktree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worktree_path
|
Path
|
Path to the worktree to lock. |
required |
reason
|
str | None
|
Human-readable reason for the lock. |
None
|
Returns:
| Type | Description |
|---|---|
WorktreeResult
|
WorktreeResult indicating success or failure. |
Source code in src/marianne/isolation/worktree.py
unlock_worktree
async
¶
Unlock a previously locked worktree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worktree_path
|
Path
|
Path to the worktree to unlock. |
required |
Returns:
| Type | Description |
|---|---|
WorktreeResult
|
WorktreeResult indicating success or failure. |
Note
Returns success if worktree is already unlocked (idempotent).
Source code in src/marianne/isolation/worktree.py
list_worktrees
async
¶
List all worktrees, optionally filtered by branch prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix_filter
|
str | None
|
Only return worktrees with branches matching prefix. For example, "marianne" returns all marianne/* branches. |
None
|
Returns:
| Type | Description |
|---|---|
list[WorktreeInfo]
|
List of WorktreeInfo for matching worktrees. |
Source code in src/marianne/isolation/worktree.py
prune_orphaned
async
¶
Clean up orphaned worktree metadata.
Removes metadata for worktrees whose directories no longer exist. Only affects worktrees with branches matching the prefix filter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix_filter
|
str
|
Only prune worktrees with matching branch prefix. |
'marianne'
|
dry_run
|
bool
|
If True, return what would be pruned without pruning. |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of pruned worktree names (or would-be-pruned if dry_run). |
Source code in src/marianne/isolation/worktree.py
get_worktree_info
async
¶
Get information about a specific worktree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worktree_path
|
Path
|
Path to the worktree. |
required |
Returns:
| Type | Description |
|---|---|
WorktreeInfo | None
|
WorktreeInfo if worktree exists, None otherwise. |
Source code in src/marianne/isolation/worktree.py
NotGitRepositoryError
¶
Bases: WorktreeError
Raised when operation attempted outside git repository.
WorktreeCreationError
¶
Bases: WorktreeError
Raised when worktree cannot be created.
WorktreeError
¶
Bases: Exception
Base exception for worktree operations.
WorktreeLockError
¶
Bases: WorktreeError
Raised when worktree lock/unlock fails.
WorktreeRemovalError
¶
Bases: WorktreeError
Raised when worktree cannot be removed.