Documentation
¶
Overview ¶
Package worker is the worker side of worker protocol v1 (docs/contracts/worker-protocol-v1.md): enrolment, the session loop, the host policy, offers, running jobs through internal/exec with worker-derived paths, the spool, blobs and controls. cmd/dd-worker is its command line. It imports no serve-side package (boundary_test.go).
Index ¶
- Constants
- Variables
- func CanonicalRepoURL(raw string) (string, error)
- func CheckCredentialDir(p Paths) error
- func EffectiveSandbox(spec, floor string) string
- func Enroll(ctx context.Context, o EnrollOptions) (config.Config, error)
- func Inside(base, p string) bool
- func MigrateCredential(p Paths, logf func(string, ...any)) error
- func Under(base, rel string) (string, error)
- func ValidEnvName(s string) bool
- type AdapterConfig
- type EnrollOptions
- type Options
- type Paths
- func (p Paths) AgentState(id string) string
- func (p Paths) Blobs(id string) string
- func (p Paths) ControlsDir() string
- func (p Paths) CredentialDir() string
- func (p Paths) Env() string
- func (p Paths) EnvPrev() string
- func (p Paths) Evidence(id string) string
- func (p Paths) Handoff(id string) string
- func (p Paths) JobControls(id string) string
- func (p Paths) JobDir(id string) string
- func (p Paths) JobMeta(id string) string
- func (p Paths) JobsDir() string
- func (p Paths) LegacyEnv() string
- func (p Paths) LegacyEnvPrev() string
- func (p Paths) Policy() string
- func (p Paths) ReadinessDir() string
- func (p Paths) RepoClone(canonicalURL string) string
- func (p Paths) ReposDir() string
- func (p Paths) Secrets() string
- func (p Paths) SpoolRoot(id string) string
- func (p Paths) StatusFile() string
- func (p Paths) Worktree(id, repo string) (string, error)
- func (p Paths) WorktreeRoot(id string) string
- type Policy
- type Secrets
- type Snapshot
- type Timing
- type Worker
Constants ¶
const ( SandboxReadOnly = "read-only" SandboxStrict = "strict" SandboxWorkspace = "workspace" )
Sandbox levels, strictest first (protocol v1 §6.5).
const ( DefaultMaxTurns = 80 DefaultMaxWallSec = 2700 DefaultMaxJobs = 1 MaxMaxJobs = 16 MaxMaxTurns = 1000 // MaxAllowedDomains bounds agent_allowed_domains; MaxMaxCostUSD bounds // max_cost_usd, the same ceiling the server puts on its own cap. MaxAllowedDomains = 16 MaxMaxCostUSD = 100 // MaxDenySockets bounds agent_deny_sockets. MaxDenySockets = 32 // DefaultAgentMaxCostUSD is the per-job cost cap when neither the server // nor this policy sets one (spec D5). DefaultAgentMaxCostUSD = 5 )
Policy defaults. A key absent from policy.toml takes these.
Variables ¶
var ( ErrRevoked = errors.New("credential revoked: re-enrol this host (dd-worker init)") ErrReplaced = errors.New("socket replaced 3 times in 10 minutes: another host uses this credential; re-enrol one of them") ErrProtocol = errors.New("server does not speak worker protocol 1") ErrOrgMismatch = errors.New("policy.toml orgs does not list this worker's org") ErrNotEnrolled = errors.New("not enrolled: run dd-worker init") )
Exit reasons of Run. cmd/dd-worker maps each to a non-zero exit.
var KnownAdapters = []string{"claude", "grok", "codex", "antigravity"}
KnownAdapters are the adapter names a policy may list (§5.1, Q4).
var Version = "0.1.0"
Version is the dd-worker version; cmd/dd-worker sets it from ldflags.
Functions ¶
func CanonicalRepoURL ¶
CanonicalRepoURL returns the canonical form the worker clones and hashes.
func CheckCredentialDir ¶
CheckCredentialDir refuses a {state}/credential that is a symlink or not a directory: the credential is never written or read through a link. A missing dir is fine (it is created at mode 0700), and so is an empty one install.sh pre-created at 0700; one open to group or others is refused.
func EffectiveSandbox ¶
EffectiveSandbox is the stricter of the spec's sandbox ("" is strict, as exec treats it) and the host's floor.
func Enroll ¶
Enroll is §3: dial with the join token, send enroll, store the credential atomically at mode 0600. A refused token gets an error and writes nothing.
func MigrateCredential ¶
MigrateCredential is the one-time move of a pre-W3 credential ({config}/env, env.prev) to {state}/credential/ (docs/worker-w3.md §3.2). It does nothing when there is no old env, or when the new env already exists. The move is a rename; when it fails (a root-owned /etc, or another filesystem) it logs the exact commands for the host owner and returns an error, so dd-worker run exits 1 instead of running from /etc.
func Under ¶
Under joins base and a protocol relative path (§2) and checks the result stays under base. Every path the worker builds from a spec or frame goes through it.
func ValidEnvName ¶
ValidEnvName reports whether s is a usable environment variable name.
Types ¶
type AdapterConfig ¶
type AdapterConfig struct {
ClaudeBin string
ClaudeConfigDir string // subscription mode: the logged-in config dir
GrokBin string
GrokHome string
AgyBin string
AgyHome string
}
AdapterConfig is how the host's coding-agent CLIs are found. Empty bins are looked up on PATH; empty homes take the CLI's default.
type EnrollOptions ¶
type EnrollOptions struct {
Paths Paths
Server string // https origin
JoinToken string // dd_wjn_…, read from stdin
InsecureDev bool
// MachineIDFile is /etc/machine-id by default. When it is missing, a
// random id is kept in {state}/machine-id instead.
MachineIDFile string
Hostname string
}
EnrollOptions are dd-worker init's inputs.
type Options ¶
type Options struct {
Paths Paths
Adapters AdapterConfig
Timing Timing
// SpoolMax is the worker-wide spool cap (§6.1, 200 MiB).
SpoolMax int64
// Log receives the worker's log lines, already redacted. Nil is stderr.
Log io.Writer
// OwnerUID overrides the owner check of policy.toml and secrets.env
// (tests: a temp file cannot be chowned to root).
OwnerUID func(os.FileInfo) (uint32, bool)
}
Options configure a Worker.
type Paths ¶
Paths is the worker's on-disk layout (docs/worker-w3.md §3.2). ConfigDir holds the host owner's files (policy.toml, secrets.env); it is root-owned and the worker never writes it. StateDir holds what the worker writes: the rotating credential (credential/env, env.prev), repos, jobs, controls.
func (Paths) AgentState ¶
AgentState is exec's per-job adapter state dir under DataRoot (the state dir): exec.Host.StateDir.
func (Paths) ControlsDir ¶
func (Paths) CredentialDir ¶
CredentialDir is {state}/credential (mode 0700): env and env.prev.
func (Paths) JobControls ¶
JobControls holds a job's stored control results, outside the job dir so a discard (which removes the job dir) can still answer a redelivery.
func (Paths) JobDir ¶
JobDir is {state}/jobs/<job_id>. id must already be a valid protocol id; JobDir panics otherwise, because a path built from an unchecked id is a traversal bug, not an input error.
func (Paths) LegacyEnv ¶
LegacyEnv and LegacyEnvPrev are where a worker before W3 kept the credential; MigrateCredential moves them once.
func (Paths) LegacyEnvPrev ¶
func (Paths) ReadinessDir ¶
func (Paths) StatusFile ¶
StatusFile is {state}/status.json (dd-worker status); ReadinessDir is {state}/readiness (the adapters' readiness checks). Both are the worker's own files that an agent must not write (spec §4.1.11).
func (Paths) WorktreeRoot ¶
type Policy ¶
type Policy struct {
Exists bool
Valid bool
Reason string
Hash string
Enabled bool
Orgs []string
Adapters []string
Repos []string
MinSandbox string
MaxTurns int
MaxWallSec int
MaxJobs int
AllowVerifyCommands bool
AllowShip bool
// AllowedDomains are the exact hosts an agent's sandbox may reach
// (empty: no network). MaxCostUSD lowers the server's per-job cost cap
// (0: unset). Whole dollars: the policy file has no decimals.
AllowedDomains []string
MaxCostUSD int
// AgentBrowser is the host's headless browser ("": no browser jobs).
// Setting it also gives a browser job's sandbox unix sockets, with every
// host socket the worker could reach masked (B1, B2). DenySockets are
// extra paths that sandbox hides.
AgentBrowser string
DenySockets []string
// AllowedModels are the models a job may run on, matched exactly (an
// alias does not cover a full id). Nil: any model; the file may not
// set an empty list.
AllowedModels []string
// contains filtered or unexported fields
}
Policy is /etc/dd-worker/policy.toml as the worker enforces it. When Valid is false every offer is declined policy_denied and Reason says why.
func LoadPolicy ¶
LoadPolicy reads path with bot-agent's file checks (root owner, not group- or world-writable, size) unless skipOwner, then applies DD's keys and defaults. uid overrides the owner for tests.
func (Policy) AdapterAllowed ¶
AdapterAllowed reports whether name is in policy.adapters.
func (Policy) ModelAllowed ¶
ModelAllowed reports whether a job of adapter may run on model under allowed_models (workerproto.ModelAllowed).
func (Policy) RepoAllowed ¶
RepoAllowed canonicalises raw and matches it against policy.repos: the host exactly, the path segment by segment, "*" matching one segment.
func (Policy) Wire ¶
func (p Policy) Wire() workerproto.Policy
Wire is the policy as hello and status report it (§4.1). masked_sockets is empty here; the worker adds its last scan (Worker.wirePolicy).
type Secrets ¶
type Secrets struct {
// contains filtered or unexported fields
}
Secrets is /etc/dd-worker/secrets.env (D5): test-account passwords and adapter API keys the host owner keeps on this host. Values never leave the worker except to the one child that needs them.
func LoadSecrets ¶
LoadSecrets reads path. A missing file is an empty set. The file must be a regular file (not a symlink) with no group or world bits, owned by root or by the worker's own user (the unit runs unprivileged and must read it). uid overrides the owner for tests.
func (Secrets) DenySet ¶
DenySet is the env names stripped from every child of a job (§6.6, with Main Dev's 2026-11-15 clarification): every name in secrets.env except the chosen adapter's own key, plus the spec's StripEnv. The job's own SecretEnv is stripped too: exec reads that one value in-process and the child gets it only as DD_TEST_ACCOUNT_PASSWORD. Invalid names are ignored.
type Snapshot ¶
type Snapshot struct {
Connected bool `json:"connected"`
Since string `json:"since"`
Server string `json:"server"`
WorkerID string `json:"workerId"`
OrgID string `json:"orgId"`
Version string `json:"version"`
Adapters []workerproto.AdapterStatus `json:"adapters"`
Policy workerproto.Policy `json:"policy"`
PolicyWhy string `json:"policyReason,omitempty"`
MaxJobs int `json:"maxJobs"`
Busy []string `json:"busyJobIds"`
// Tools is the last tools readiness (git, and browser with
// agent_browser), as hello and status send it.
Tools []workerproto.ToolStatus `json:"tools,omitempty"`
}
Snapshot is {state}/status.json, written by run for dd-worker status. It holds no credential, token or secret value.
func ReadSnapshot ¶
ReadSnapshot reads the last snapshot run wrote.
type Timing ¶
type Timing struct {
AcceptGrace time.Duration // §5.2: 5 s with no lease_lost before starting
CancelGrace time.Duration // §4.4, §6.3: SIGINT then SIGKILL after 3 s
BatchInterval time.Duration // §6.1: 1 s
Readiness time.Duration // §4.2: 5 min
ReplacedBackoff time.Duration // §4.5: at least 60 s plus jitter
ReplacedWindow time.Duration // §4.5: 10 min
LeaseWait time.Duration // replay a busy job with no lease frame after this
HelloTimeout time.Duration
BackoffMax time.Duration // reconnect backoff cap (conn.Backoff caps at 30 s)
}
Timing are the protocol's waits, overridable so tests run in seconds. Zero fields take the protocol values.