codenib.sandbox
¶
Provider-neutral isolation for repository execution.
Sandboxing is opt-in. Importing this package never contacts a container
runtime; construct :class:DockerSandboxProvider explicitly on a dedicated
worker when untrusted code execution is required.
Modules:
| Name | Description |
|---|---|
docker |
Rootless-Docker sandbox backend for untrusted repository commands. |
protocol |
Backend-neutral sandbox provider and session protocols. |
types |
Stable value types for isolated repository execution. |
Classes:
| Name | Description |
|---|---|
DockerSandboxProvider |
Create digest-pinned, rootless-Docker sandbox sessions. |
DockerSandboxSession |
A copied repository backed by private baseline/workspace volumes. |
SandboxClosedError |
Raised when an operation targets a closed session. |
SandboxError |
Base error for sandbox infrastructure or policy failures. |
SandboxPolicyError |
Raised when a request would weaken an enforced policy. |
SandboxProvider |
Factory for backend-specific sandbox sessions. |
SandboxSession |
One isolated, copied repository workspace. |
SandboxUnavailableError |
Raised when the configured runtime or image is unavailable. |
ArtifactBundle |
Controller-owned ZIP export of selected workspace files. |
ArtifactMember |
One regular file included in an exported artifact bundle. |
DiffResult |
Canonical Git patch produced from the immutable source snapshot. |
ExecRequest |
One argv-based command request. |
ExecResult |
Bounded model-facing output plus audit hashes for one command. |
NetworkMode |
Container egress policy. |
SandboxCapabilities |
Auditable guarantees a provider can truthfully claim. |
SandboxLimits |
Hard resource and output bounds applied to every command. |
SandboxMetadata |
Non-secret identity recorded in agent traces and job artifacts. |
SandboxPolicy |
Security policy for one sandbox session. |
SandboxSpec |
Immutable request for a repository sandbox. |
DockerSandboxProvider
¶
DockerSandboxProvider(
*,
allowed_images: Collection[str],
docker_binary: str = "docker",
docker_host: str | None = None,
git_binary: str = "git",
work_root: Path | None = None,
retain_audit_logs: bool = False,
run_cli: RunCLI = run,
popen_factory: PopenFactory = Popen,
clock: Callable[[], float] = monotonic,
id_factory: Callable[[], UUID] = uuid4
)
Create digest-pinned, rootless-Docker sandbox sessions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
allowed_images
|
Collection[str]
|
Exact digest-pinned image references approved by the service control plane. The model never selects an arbitrary image. |
required |
docker_binary
|
str
|
Docker-compatible client binary. This backend validates Docker semantics and must not be pointed at Podman. |
'docker'
|
work_root
|
Path | None
|
Controller-only directory for bounded audit logs. |
None
|
retain_audit_logs
|
bool
|
Keep audit logs after session close. Production bot workers should set this and apply their own retention policy. |
False
|
Methods:
| Name | Description |
|---|---|
create |
Preflight runtime/image policy, copy the source, and return a session. |
Source code in codenib/sandbox/docker.py
create
¶
create(spec: SandboxSpec) -> 'DockerSandboxSession'
Preflight runtime/image policy, copy the source, and return a session.
Source code in codenib/sandbox/docker.py
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 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | |
DockerSandboxSession
¶
DockerSandboxSession(
*,
provider: DockerSandboxProvider,
spec: SandboxSpec,
metadata: SandboxMetadata,
baseline_volume: str,
workspace_volume: str,
audit_dir: Path
)
A copied repository backed by private baseline/workspace volumes.
Attributes:
| Name | Type | Description |
|---|---|---|
audit_dir |
Path
|
Controller-only audit directory; never mounted into the sandbox. |
Source code in codenib/sandbox/docker.py
audit_dir
property
¶
Controller-only audit directory; never mounted into the sandbox.
SandboxClosedError
¶
Bases: SandboxError
Raised when an operation targets a closed session.
SandboxError
¶
Bases: RuntimeError
Base error for sandbox infrastructure or policy failures.
SandboxPolicyError
¶
Bases: SandboxError
Raised when a request would weaken an enforced policy.
SandboxProvider
¶
Bases: Protocol
Factory for backend-specific sandbox sessions.
SandboxSession
¶
Bases: Protocol
One isolated, copied repository workspace.
SandboxUnavailableError
¶
Bases: SandboxError
Raised when the configured runtime or image is unavailable.
ArtifactBundle
dataclass
¶
ArtifactBundle(path: Path, size: int, sha256: str, members: tuple[ArtifactMember, ...])
Controller-owned ZIP export of selected workspace files.
ArtifactMember
dataclass
¶
One regular file included in an exported artifact bundle.
DiffResult
dataclass
¶
Canonical Git patch produced from the immutable source snapshot.
ExecRequest
dataclass
¶
ExecRequest(
argv: Tuple[str, ...] | Sequence[str],
cwd: PurePosixPath | str = PurePosixPath("."),
stdin: bytes | None = None,
environment: Mapping[str, str] = dict(),
timeout_seconds: float | None = None,
)
One argv-based command request.
No host shell is involved. Callers that deliberately need shell syntax can
request ('/bin/sh', '-lc', command); those three argv elements are
still passed after the pinned container image.
ExecResult
dataclass
¶
ExecResult(
command_id: str,
argv: tuple[str, ...],
exit_code: int | None,
stdout: str,
stderr: str,
duration_ms: float,
timed_out: bool = False,
output_limited: bool = False,
stdout_truncated: bool = False,
stderr_truncated: bool = False,
stdout_sha256: str = "",
stderr_sha256: str = "",
stdout_bytes: int = 0,
stderr_bytes: int = 0,
)
Bounded model-facing output plus audit hashes for one command.
NetworkMode
¶
Bases: str, Enum
Container egress policy.
BRIDGE is intentionally explicit. Docker cannot implement a reliable
hostname allowlist by itself, so public issue-bot jobs should remain on
NONE and use a separate, policy-enforcing bootstrap service when they
need dependencies.
SandboxCapabilities
dataclass
¶
SandboxCapabilities(
provider: str,
isolation: str,
non_root_user: bool,
network_isolation: bool,
read_only_rootfs: bool,
resource_limits: bool,
process_tree_cleanup: bool,
disk_quota: bool,
rootless_runtime: bool | None = None,
)
Auditable guarantees a provider can truthfully claim.
SandboxLimits
dataclass
¶
SandboxLimits(
cpus: float = 2.0,
memory_bytes: int = 2 * 1024**3,
pids: int = 256,
command_timeout_seconds: float = 300.0,
output_bytes: int = 64 * 1024,
audit_log_bytes: int = 8 * 1024**2,
stdin_bytes: int = 1024**2,
tmpfs_bytes: int = 256 * 1024**2,
artifact_bytes: int = 64 * 1024**2,
)
Hard resource and output bounds applied to every command.
SandboxMetadata
dataclass
¶
SandboxMetadata(
sandbox_id: str,
task_id: str,
provider: str,
image: str,
image_id: str,
platform: str,
source_revision: str | None,
network: str,
rootless_runtime: bool | None,
source_fingerprint: str = "",
policy: Mapping[str, object] = dict(),
)
Non-secret identity recorded in agent traces and job artifacts.
SandboxPolicy
dataclass
¶
SandboxPolicy(
network: NetworkMode = NONE,
read_only_rootfs: bool = True,
require_rootless_runtime: bool = True,
require_source_revision: bool = True,
allow_unpinned_image: bool = False,
seccomp_profile: str | None = None,
runtime: str | None = None,
limits: SandboxLimits = SandboxLimits(),
)
Security policy for one sandbox session.
The safe defaults are fail-closed: no network, a read-only container root,
no Linux capabilities, no privilege escalation, and a rootless daemon
requirement. require_rootless_runtime=False is intended only for
explicitly trusted repositories or a separately isolated worker VM.
SandboxSpec
dataclass
¶
SandboxSpec(
source_dir: Path,
image: str,
platform: str,
source_revision: str | None = None,
task_id: str | None = None,
policy: SandboxPolicy = SandboxPolicy(),
source_selection: RepositorySourceSelection = RepositorySourceSelection(),
)
Immutable request for a repository sandbox.
source_revision is optional at the library boundary for generated or
non-Git fixtures. GitHub automation should always provide the exact
40-character commit and let the provider verify it before copying files.