launchpolicy

package
v0.199.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: BSD-2-Clause Imports: 7 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package launchpolicy materialises, exactly once per daemon process, the set of launch-shape inputs that are intrinsic to "this is a dark-factory container": image, project identity, mounts, base environment, netrc/gitconfig paths, hide-git, and the canonical Linux capability set (NET_ADMIN, NET_RAW).

Two call sites consume it: the executor's prompt-run path (pkg/executor.dockerExecutor.buildDockerCommand) and the healthcheck probes' container-launch path (pkg/cmd/healthcheck.runContainerProbe). Both derive their executor.ContainerLaunchOpts from the same Policy value via Policy.BuildOpts; per-invocation differences (container name, entrypoint, command, env overlay, label overlay) flow through the Extras argument.

A reader who wants to add a new launch-shape concern (new cap, new mount, new base env var) adds it here once. Both the executor and the probes pick it up with no further changes. The canonical container-startup-site inventory is maintained in the comment block on Policy itself; the inventory's enumerated set of file:line pairs must equal the UNION of:

grep -rnE 'exec\.Command(Context)?\(.*"docker"' pkg/ | grep -v _test.go
grep -rnE 'RunWithWarnAndTimeout\([^,]+,[^,]+,[^,]+"docker"' pkg/ | grep -v _test.go

(Dark-factory invokes `docker` two ways: directly via `exec.Command*` and indirectly via `subproc.Runner.RunWithWarnAndTimeout`. The inventory must cover both. Any drift between the inventory and the union of the two grep outputs is an unresolved architectural divergence and must be fixed in the same change that introduced it.)

Index

Constants

View Source
const EnvManagedMarker = "DARK_FACTORY_MANAGED"

EnvManagedMarker is the environment variable dark-factory sets on every container it launches. Its presence signals to processes inside the container that the dark-factory daemon manages this container's lifecycle — for example, the generate-prompts-for-spec command skips its host-only `spec mark-prompted` step because the host-side generator finalizes the spec after the container exits (and the dark-factory CLI is not present inside the container).

Variables

View Source
var CanonicalCaps = []string{"NET_ADMIN", "NET_RAW"}

CanonicalCaps is the Linux capability set every dark-factory container requires. NET_ADMIN + NET_RAW are needed by the claude-yolo entrypoint's init-firewall.sh (iptables rules) to run on container backends that reject those syscalls without explicit grants (e.g. OrbStack).

This is the SINGLE production reference to the capability literals in the repository. The architectural invariant

grep -rn "NET_ADMIN" pkg/ | grep -v _test.go | wc -l

MUST return exactly 1 (this file). Adding the literals anywhere else re-introduces spec-098's divergence-by-construction.

Functions

func DropSecretEnvKeys added in v0.199.2

func DropSecretEnvKeys(env map[string]string) (map[string]string, []string)

DropSecretEnvKeys returns a copy of env with credential-shaped keys removed, plus the names of the keys it dropped (sorted). A copy is always returned when env is non-empty, so mutating the result never mutates the input. This is the fail-closed guard on the legacy `-e` path: a call site that forgets PrepareEnvFile loses the variable (loudly, via the returned names) instead of publishing it to `ps`.

func FormatEnvFile added in v0.199.2

func FormatEnvFile(env map[string]string) string

FormatEnvFile renders env as `--env-file` lines, sorted by key so the output is byte-stable for tests. docker does not interpolate or quote values in an env-file, so each value is written verbatim; a value containing a newline cannot be represented in this format and would produce two lines.

func IsSecretEnvKey added in v0.199.2

func IsSecretEnvKey(key string) bool

IsSecretEnvKey reports whether an env key looks like a credential. The test is deliberately over-inclusive: a false positive costs one env var moved to the env-file, a false negative puts a credential in the process table.

func PrepareEnvFile added in v0.199.2

func PrepareEnvFile(ctx context.Context, opts *ContainerLaunchOpts) (func(), error)

PrepareEnvFile materialises opts.Env into a 0600 file inside a fresh 0700 temp directory, points opts.EnvFile at it, and clears opts.Env so that no env value — credential or otherwise — can reach the docker argv.

The container env reaches docker via `--env-file`, which the docker CLI reads on the HOST as the invoking user. The container never opens the file, so the file needs no container-visible path and 0600 is sufficient for docker to read it.

0600 is NOT an isolation boundary against processes running as the same uid. Another agent session on this machine can still read the file, and its path is in the argv, so the exposure is one `ps` plus one `cat` rather than a bare `ps`. What this change removes is the credential being printed by the process listing itself. Two residuals remain and are out of scope here: same-uid reads of the file, and the value transiting the docker API into the container config (readable with `docker inspect` for the container's lifetime).

The returned cleanup removes the temp directory. Callers must defer it. A SIGKILL of the dark-factory process skips the cleanup and leaves a 0600 file in the user's own temp directory.

func WithManagedMarker added in v0.199.2

func WithManagedMarker(env map[string]string) map[string]string

WithManagedMarker returns a copy of env with EnvManagedMarker set to "true". The marker is written after copying so it takes precedence over any inbound value for the same key. A new map is returned so the caller's map is never mutated, and a non-nil map is always returned so the marker is emitted even when env is nil.

Types

type ContainerLaunchOpts

type ContainerLaunchOpts struct {
	// ContainerName is the value passed to `--name`.
	ContainerName string
	// ContainerImage is the image reference (positional, last before Command).
	ContainerImage string
	// ProjectName is the value of the `dark-factory.project=` label.
	ProjectName string
	// ProjectRoot is the host path mounted at /workspace and the base used by
	// HideGit + ExtraMounts path resolution.
	ProjectRoot string
	// ClaudeDir is the host path mounted at /home/node/.claude (auth credentials).
	ClaudeDir string
	// Home is the host's HOME, used for ~/ expansion in NetrcFile/GitconfigFile/ExtraMounts.
	Home string
	// Env is the container environment. It reaches docker as `-e KEY=VALUE`
	// flags only when EnvFile is empty, and credential-shaped keys are dropped
	// from that path (see DropSecretEnvKeys).
	//
	// Env values are visible to any process that can read the host's process
	// table, so callers must route the environment through PrepareEnvFile
	// instead: it writes Env into a 0600 file, sets EnvFile, and clears Env.
	Env map[string]string
	// EnvFile, when non-empty, is passed as `--env-file <path>` and Env is
	// ignored — no `-e` flag is emitted at all. This is how the container env
	// (including credentials) reaches docker without any value appearing in
	// argv.
	//
	// It must be produced by PrepareEnvFile: that is what bakes in
	// EnvManagedMarker, which the in-container spec generator relies on to skip
	// its host-only `spec mark-prompted` step. A hand-written env-file looks
	// valid and silently loses the marker.
	EnvFile string
	// ExtraMounts is appended as -v <src>:<dst>[:ro] flags; missing src is skipped + logged.
	ExtraMounts []config.ExtraMount
	// NetrcFile, when non-empty, is mounted at /home/node/.netrc:ro.
	NetrcFile string
	// GitconfigFile, when non-empty, is mounted at /home/node/.gitconfig-extra:ro.
	GitconfigFile string
	// HideGit, when true, masks ProjectRoot/.git inside the container.
	HideGit bool
	// ExtraLabels is appended as additional --label KEY=VALUE flags after the project label.
	ExtraLabels map[string]string
	// CapAdd is appended as --cap-add=<value> flags.
	CapAdd []string
	// Entrypoint, when non-empty, is passed as --entrypoint <value>.
	Entrypoint string
	// Command is appended after the image (positional args to the container).
	Command []string

	// RunAsUser, when non-empty, is passed as --user <value>. Empty means
	// container runs as the image's default user (typically root).
	RunAsUser string
	// MemoryLimit, when non-empty, is passed as --memory <value> (e.g. "8g").
	MemoryLimit string
	// CPULimit, when non-empty, is passed as --cpus <value> (e.g. "4").
	CPULimit string
	// PIDsLimit, when > 0, is passed as --pids-limit <N>. Values <= 0 are
	// treated as "unset" — zero is the field's zero value used as the
	// "unset" sentinel; negative values are silently dropped (docker would
	// reject them anyway and the sentinel semantics are simpler than
	// returning an error from BuildDockerRunArgs).
	PIDsLimit int
	// ClaudeDirReadOnly, when true, mounts ClaudeDir with :ro suffix. Default
	// false preserves rw mount needed for OAuth token refresh.
	ClaudeDirReadOnly bool
}

ContainerLaunchOpts carries the inputs needed to assemble a `docker run --rm ...` argv for any dark-factory container — prompt execution, spec generation, or healthcheck probes. Centralising the argv build here keeps the production launch path and the healthcheck probes on the same mount/env/hideGit/extraMounts wiring; if production stops launching containers correctly, the healthcheck probes notice immediately.

type Extras

type Extras struct {
	// ContainerName is the value passed to --name. Required.
	ContainerName string
	// Entrypoint is passed as --entrypoint <value> when non-empty.
	Entrypoint string
	// Command is appended after the image (positional container args).
	Command []string
	// EnvOverlay is merged into the Policy's base env. Keys in EnvOverlay
	// win on collision with baseEnv — the executor's prompt-specific values
	// (YOLO_PROMPT_FILE, YOLO_OUTPUT, ANTHROPIC_MODEL) are passed this way.
	EnvOverlay map[string]string
	// ExtraLabels is appended as --label KEY=VALUE flags after the project
	// label. Used e.g. by the executor's "dark-factory.prompt=<basename>"
	// label. Empty / nil leaves no extra labels.
	ExtraLabels map[string]string
}

Extras carries the per-invocation inputs that differ between the executor's prompt-run and the healthcheck probes' one-shot containers. Everything in Extras is layered ON TOP of the Policy's base launch shape.

type Policy

type Policy struct {
	// contains filtered or unexported fields
}

Policy carries the launch-shape inputs intrinsic to every dark-factory container. Constructed once per daemon process (or per command invocation) from config + environment; consumed by both the executor's prompt-run path and the healthcheck probes.

All fields are unexported. Construct via NewPolicy and consume via BuildOpts.

CONTAINER-STARTUP-SITE INVENTORY (spec 098 AC "Container-startup-site inventory is complete"). Every production-code site that invokes `exec.Command(ctx, "docker", ...)` OR `subproc.Runner.RunWithWarnAndTimeout(ctx, op, "docker", ...)` in pkg/ is classified below. The enumerated set MUST equal the UNION of

grep -rnE 'exec\.Command(Context)?\(.*"docker"' pkg/ | grep -v _test.go
grep -rnE 'RunWithWarnAndTimeout\([^,]+,[^,]+,[^,]+"docker"' pkg/ | grep -v _test.go

at HEAD. CI may grep this comment to detect drift.

Routed through Policy.BuildOpts (docker run / claude-yolo containers):

pkg/executor/executor.go:517   exec.CommandContext(ctx, "docker", args...)
                               -- prompt-run path (buildDockerCommand)
                               -- also serves spec generation via the
                                  shared executor.NewDockerExecutor
pkg/cmd/healthcheck/probes.go:319 a.runner.RunWithWarnAndTimeout(
                                    ctx, a.op, "docker",
                                    executor.BuildDockerRunArgs(opts)...)
                               -- boot / mount / claude probes

Explicitly out of scope (do NOT invoke claude-yolo; carry no caps, no mounts, no /workspace bind):

pkg/cmd/healthcheck/probes.go:115  "docker version"
pkg/cmd/healthcheck/probes.go:150  "docker image inspect --format=..."
pkg/executor/checker.go:72         "docker inspect --format ..."
pkg/executor/checker.go:105        "docker ps ..." (NewDockerContainerChecker)
pkg/executor/executor.go:244       "docker logs --follow <name>"
pkg/executor/executor.go:298       "docker stop <name>"
pkg/executor/executor.go:303       "docker kill <name>"
pkg/executor/executor.go:352       "docker stop <name>"
pkg/executor/executor.go:600       "docker stop <name>"
pkg/executor/executor.go:611       "docker rm -f <name>"
pkg/executor/stopper.go:32         "docker stop <name>"
pkg/status/status.go:546           "docker ps --filter ..."
pkg/status/status.go:569           "docker ps --filter ..."

Out-of-scope rationale: these sites do not start a claude-yolo container; they query/inspect/stop/kill/log existing containers. They have no mount, env, capability, or hide-git surface.

func NewPolicy

func NewPolicy(
	containerImage string,
	projectName string,
	projectRoot string,
	claudeDir string,
	home string,
	baseEnv map[string]string,
	extraMounts []config.ExtraMount,
	netrcFile string,
	gitconfigFile string,
	hideGit bool,
) Policy

NewPolicy returns a Policy capturing the launch-shape inputs from cfg + the resolved process environment (home, projectRoot). capAdd is initialised to CanonicalCaps; callers cannot override (see spec 098 Non-goal "Do NOT make capabilities configurable").

projectName is the value of the dark-factory.project label. projectRoot is the host path mounted at /workspace. home is the host HOME, used for ~/ expansion in mount paths. baseEnv is the operator-configured env map (cfg.Env) plus daemon-injected values such as ANTHROPIC_MODEL. Prompt-specific keys (YOLO_PROMPT_FILE, YOLO_OUTPUT) are NOT part of the base — they are passed in via Extras so unrelated invocations stay clean.

func (Policy) BaseEnv

func (p Policy) BaseEnv() map[string]string

BaseEnv returns a shallow copy of the base environment map. Callers that only read the map may use the returned value directly; callers that mutate it should copy first.

func (Policy) BuildOpts

func (p Policy) BuildOpts(extras Extras) ContainerLaunchOpts

BuildOpts returns a ContainerLaunchOpts ready for executor.BuildDockerRunArgs. The returned value carries the policy's base launch shape plus the per-invocation extras.

THIS IS THE ONLY production-code site that composes ContainerLaunchOpts{...} after spec 098 lands. The architectural invariant

grep -rn "ContainerLaunchOpts{" pkg/ | grep -v _test.go | wc -l

MUST return exactly 1 (this method).

func (Policy) ClaudeDir

func (p Policy) ClaudeDir() string

ClaudeDir returns the resolved claude config directory (host path).

func (Policy) ContainerImage

func (p Policy) ContainerImage() string

ContainerImage returns the image reference (consumed by callers needing it outside BuildOpts, e.g. the executor's insertPromptFileMount).

func (Policy) ProjectName

func (p Policy) ProjectName() string

ProjectName returns the value of the dark-factory.project label.

func (Policy) WithCapAddForTest

func (p Policy) WithCapAddForTest(caps []string) Policy

WithCapAddForTest returns a copy of p with capAdd replaced by caps. Test-only override; production callers cannot vary the cap set (spec 098 Non-goal). The method name carries "ForTest" so reviewers and tooling can flag any non-test caller as a violation.

func (Policy) WithSecurity added in v0.184.0

func (p Policy) WithSecurity(opts SecurityOpts) Policy

WithSecurity returns a copy of p with the security fields REPLACED by opts. Policy is a value type and this method uses a value receiver: assignments to p inside this body mutate a local copy, not the caller's Policy. Mirrors WithCapAddForTest's immutable-builder pattern.

Replace (not merge) semantics: a zero-value SecurityOpts{} explicitly resets every security field on the returned policy.

Phase 2 (separate PR) will call this from the factory with non-zero defaults after dev validation confirms ro claudeDir works for Claude SDK auth refresh.

type SecurityOpts added in v0.184.0

type SecurityOpts struct {
	RunAsUser         string
	MemoryLimit       string
	CPULimit          string
	PIDsLimit         int
	ClaudeDirReadOnly bool
}

SecurityOpts groups the security hardening fields applied via WithSecurity. Defaults (zero values) preserve current production behavior (root, rw credentials, no resource limits). See ADR-0001 for the rollout plan.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL