abc_mine.sandbox¶
WARNING
This documentation is almost entirely "vibe coded"; while a helpful resource for humans and agents to understand this codebase, it should not be considered a source of ground truth in the way documentation normally is, and the user is responsible for verifying that all instructions given here are safe.
abc_mine.sandbox
¶
Sandbox wiring: staging assets and running one agent in the container.
This module owns the single most failure-prone piece of mechanics in the tool: getting a host directory, a source tree, and a set of staged files presented to an agent inside a Charliecloud container, consistently, across all three stages.
The single-mount rule
Everything the agent sees lives under one host directory, bind-mounted at one in-container path. Nothing is nested.
This is not a stylistic preference. With ch-run --write-fake a bind-mount
target cannot be created inside an already-bound directory: binding
workspace/ -> /workspace and then src/ -> /workspace/src fails with
can't mkdir /workspace/src under existing bind-mount /workspace whenever the
target does not already exist in the image. The original pipeline hit exactly this
and, notably, its own handoff notes record that the nested pattern should have
failed in two stages that nonetheless produced real output — a discrepancy never
explained.
Rather than depend on behaviour nobody could account for, this module only ever uses the pattern that was verified to work: materialize everything inside one host directory (the source tree as a git worktree, the staged prompt, the outputs) and carry it in with a single mount. There is no ambiguity about whether it works, and the mechanism is identical in every stage.
A consequence worth stating: because we never inject a tools directory into the
container, the container must already contain whatever the toolchain commands
invoke. That is why those commands are declared in config and proved by
preflight — the verifiers that ship inside emitted tasks have to call something
that still exists at grading time, when even fewer host paths are available.
AgentRun
dataclass
¶
Result of one agent invocation.
Attributes:
| Name | Type | Description |
|---|---|---|
execution |
ExecutionResult
|
automaton's raw result (success flag, return code, log path, failure detail). |
work_dir |
Path
|
Host directory that was mounted at :data: |
run_dir |
Path
|
Host directory holding staged assets and harness output. |
Source code in abc_mine/sandbox.py
build_automaton_config
¶
Build an :class:AutomatonConfig from the resolved model settings.
The provider/model/agent/harness graph is generated in memory rather than read
from a file, so there is exactly one way to configure the model — the
environment — and no chance of a stale YAML silently overriding it. The API key
is referenced by variable name (api_key_env), never by value, so it is
never written to disk.
Source code in abc_mine/sandbox.py
make_agent
¶
make_harness
¶
Return the packaged mining :class:Harness.
Source code in abc_mine/sandbox.py
make_sandbox
¶
make_sandbox(cfg: MineConfig, *, dry_run: bool = False) -> Sandbox
Return a :class:Sandbox with this tool's in-container layout.
Source code in abc_mine/sandbox.py
scratch_dir
¶
scratch_dir(cfg: MineConfig, prefix: str) -> Iterator[Path]
Yield a node-local scratch directory, removed on exit.
Heavy, throwaway state (worktrees, build trees, agent transcripts) belongs
here rather than in the run's durable output tree: a sweep over a large history
would otherwise accumulate many gigabytes of material nobody reads, on shared
storage. Removal is unconditional, including on failure — callers that need to
preserve evidence must copy it out first (see keep_logs).
Source code in abc_mine/sandbox.py
run_agent
¶
run_agent(cfg: MineConfig, *, sandbox: Sandbox, harness: Harness, agent: Agent, work_dir: Path, run_dir: Path, prompt_text: str, build_env_text: str | None = None, extra_env: Mapping[str, str] | None = None, opencode_runtime_dir: Path | None = None, session_id: str | None = None, resume: bool = False, resume_message: str | None = None, timeout_seconds: int | None = None, budget_exhausted_callback: Any | None = None, stop_check: Any | None = None) -> AgentRun
Stage assets and run one agent against work_dir.
work_dir must already contain everything the agent should see — the source
worktree, the staged inputs, and an out/ directory for its deliverables.
It is mounted, whole, at :data:CONTAINER_WORK_ROOT; see the module docstring
for why nothing is nested.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
work_dir
|
Path
|
Host directory mounted at the container work root. |
required |
run_dir
|
Path
|
Host directory for staged prompt/harness assets and harness output. |
required |
prompt_text
|
str
|
The fully-composed prompt. |
required |
build_env_text
|
str
|
Contents of the pre-agent setup script. A sensible no-op is used when omitted. |
None
|
extra_env
|
mapping
|
Additional environment variables for the agent process. |
None
|
Source code in abc_mine/sandbox.py
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 | |
run_command
¶
run_command(cfg: MineConfig, *, sandbox: Sandbox, command: str, work_dir: Path, log_file: Path, mount_at: Path | None = None, env: Mapping[str, str] | None = None, check: bool = False) -> int | None
Run one shell command in the container against work_dir.
Used by preflight (to prove the toolchain works) and by the build stage's
independent polarity re-check. bash -lc is used so the command string may
use ordinary shell syntax — pipes, quoting, && — exactly as written in the
config.
Returns the exit status, or None under a dry run.
Source code in abc_mine/sandbox.py
read_usage
¶
Summarize token usage from a harness's usage.jsonl.
Recorded per item so a sweep's cost is attributable after the fact, rather
than being a single opaque bill. See :func:abc_mine.telemetry.summarize
for the fields; input_tokens totals all three input categories.
Source code in abc_mine/sandbox.py
preserve_logs
¶
Copy agent transcripts out of scratch, per the retention policy.
Scratch is deleted unconditionally, so anything worth keeping must be copied
first. The default policy (failed) keeps transcripts for failures only:
enough to diagnose what went wrong, without retaining gigabytes of logs for
thousands of successful items.
Returns the destination directory, or None if nothing was kept.