launch
launch
¶
Per-container wiring for user-owned containers.
prepare/run/cleanup compose podman flags that wire a caller-owned
container into the sandbox's services (vault token broker, vault SSH
signer, git gate, shield egress firewall) and persist enough per-container
state for cleanup to be a no-arg reverse of prepare.
Container lifecycle stays with the user; sandbox owns only the services and the per-container ancillary state (tokens, shield rules, meta JSON).
CONTAINER_BRIDGES_DIR = '/usr/local/share/terok-sandbox/bridges'
module-attribute
¶
LOOPBACK_VAULT_PORT = 9419
module-attribute
¶
SANDBOX_MANAGED_FLAGS = frozenset({'--name', '--network', '--hooks-dir', '--annotation', '--cap-add', '--cap-drop', '--userns'})
module-attribute
¶
WiringPlan(scope, shield, gate, broker, ssh)
dataclass
¶
Subsystems activated for a single prepare/run invocation.
Persisted to meta.json so cleanup reverses exactly what was
activated, without re-running the flag-defaults dance.
scope
instance-attribute
¶
shield
instance-attribute
¶
gate
instance-attribute
¶
broker
instance-attribute
¶
ssh
instance-attribute
¶
to_dict()
¶
Return a JSON-serialisable representation.
from_dict(data)
classmethod
¶
Construct from a previously-persisted to_dict payload.
Source code in src/terok_sandbox/launch.py
PerContainerResources(container_runtime_dir, token_broker_port, ssh_signer_port, gate_port)
dataclass
¶
Per-container socket dir + (for TCP mode) ports.
Allocated once per launch so the same values reach mount flags, env vars, and the sidecar JSON the supervisor reads. Keeps concurrent containers from colliding on host-global filenames or ports.
container_runtime_dir
instance-attribute
¶
Host-side directory that becomes /run/terok/ inside the
container. Contains the supervisor-bound vault.sock /
ssh-agent.sock. Created (mode 0700) before the bind mount.
token_broker_port
instance-attribute
¶
Per-container TCP port for the vault proxy in TCP mode; None
in socket mode.
ssh_signer_port
instance-attribute
¶
Per-container TCP port for the SSH signer in TCP mode; None
in socket mode.
gate_port
instance-attribute
¶
Per-container TCP port for the git gate in TCP mode; None
in socket mode.
allocate_per_container_resources(cfg, container)
¶
Compute per-container paths + (for TCP mode) ports.
Both transport modes get a per-container directory under
cfg.runtime_dir/run/<container> (mode 0700) that the caller
bind-mounts at /run/terok/ inside the container. In TCP mode,
two free ports are claimed via bind(0) + getsockname +
close so each container gets its own pair instead of fighting
over the singleton from cfg.
The narrow window between bind(0)'s close and the supervisor's
re-bind on the same port is an EADDRINUSE-loud failure mode, not
silent breakage.
Source code in src/terok_sandbox/launch.py
bridges_resource_dir()
¶
run_state_dir(cfg, container)
¶
compose(container, *, cfg, shield, gate, broker, scope, profiles=None)
¶
Compose podman args for one prepare/run invocation.
Mints any tokens needed for the active subsystems (broker/gate/ssh),
creates the per-container state directory, persists meta.json,
and returns the assembled podman flag list plus the resolved plan.
Subsystems that require scope are silently disabled (with a
stderr note) when scope is None — sandbox only enforces the
fail-closed property; nudging the caller toward a useful invocation
is the job of the CLI layer.
Raises SystemExit if shield setup is required (propagated from
ShieldManager.pre_start).
Source code in src/terok_sandbox/launch.py
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 | |
write_sidecar(container_name, *, cfg, per_container, scope_id='', project_id='', task_id='', dossier_path=None, gate_base_path=None, gate_token=None, gate_port=None, allow_debugger=False)
¶
Persist the per-container sidecar config the supervisor reads.
The canonical writer for the whole package chain — compose calls
it for standalone sandbox runs and terok-executor's
AgentRunner.launch_prepared calls it for terok tasks — so the
schema load_sidecar
parses has exactly one producer.
Path: <cfg.state_dir>/sidecar/<container-name>.json. Returns
the absolute path on success — the caller emits it as the
terok.sandbox.sidecar OCI annotation that triggers the hook
and tells it where to find this file. No XDG guessing, no
name-vs-id rename: one anchor, one path. The file survives
container stop/start cycles by design (its ports and tokens must
keep matching the container's immutable env, and the createRuntime
hook re-reads it on every podman start); it is removed at real
teardown by
remove_container_state.
Socket paths are NOT carried in the sidecar — the supervisor
derives them from the container name + runtime dir via
SupervisorPaths.for_container.
TCP ports ARE carried because the launch path allocates them
fresh per container via bind(0). Gate config travels only
when the gate is wired — the supervisor composes the gate iff
both gate_base_path and gate_token are present.
allow_debugger records debug mode: the supervisor children then
leave themselves ptrace-able instead of clearing the dumpable flag.
Best-effort: a write failure logs to stderr and returns None;
callers pick their own policy (compose rolls back and aborts the
launch, the executor raises its BuildError).
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/terok_sandbox/launch.py
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 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |
remove_container_state(container, *, cfg)
¶
Remove the per-container sidecar + host-side runtime directory.
The inverse of
allocate_per_container_resources
+ write_sidecar: deletes the
state keyed by container name that deliberately outlives a stop
(the sidecar survives poststop so restarts come back supervised).
Call at real teardown — after the container is removed — never at
mere stop. Idempotent; missing state is a no-op.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/terok_sandbox/launch.py
make_stray_sidecar_check(cfg=None)
¶
Sweep per-container state left behind by out-of-band removal.
The sidecar deliberately survives podman stop — a stopped
container must come back supervised, and the preserved file is the
only wiring that still matches the container's immutable env (see
write_sidecar). The real
teardown paths (cleanup, terok's
task delete) remove it; a container removed outside those paths
(bare podman rm) strands its sidecar. Strays are inert —
launches overwrite by name and the hook only fires via a live
container's annotation — so this reconciliation is hygiene, not
correctness.
The check acts on what it finds: a sidecar whose container podman
no longer knows, and which is past the prepare→run grace window, is
swept on the spot via
remove_container_state,
and the verdict reports what was done. When podman is unreachable,
live and stray are indistinguishable and the sweep is skipped.
Host-level like the recovery-key check: intentionally NOT bundled
into
sandbox_doctor_checks
(that list renders per container); top-level callers append it so
it runs exactly once.
Source code in src/terok_sandbox/launch.py
exec_podman(sandbox_args, podman_args)
¶
Replace this process with podman run.
Validates that podman_args (everything the user typed after --)
doesn't collide with sandbox-owned flags or volume targets, then
os.execvs into podman. Caller doesn't return.
Source code in src/terok_sandbox/launch.py
reject_managed_flags(podman_args)
¶
Reject user-supplied flags that sandbox owns.
Mirrors terok-shield's _reject_shield_managed_flags and adds
sandbox-specific entries (e.g. --userns).
Source code in src/terok_sandbox/launch.py
reject_managed_volumes(podman_args)
¶
Reject -v host:target whose target overlaps a sandbox mount.
Source code in src/terok_sandbox/launch.py
cleanup(container, *, cfg)
¶
Reverse a prior prepare/run for container.
Returns True when state was found and torn down, False when
there was nothing to clean up. Idempotent — safe to call repeatedly.
Source code in src/terok_sandbox/launch.py
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 | |
format_args(args, *, output_json)
¶
Return the printable form of an args list.