Skip to content

Lifecycle

What a command does from start to exit, and which package does each step. For what a run isolates, see Security model.

Every command

flowchart LR
    exec["cli.Execute"] --> pre["root pre-run<br/>config, context, migrate, checks, upgrade"]
    pre --> parse["command<br/>parse flags and args"]
    parse --> resolve["usecase<br/>Resolve"]
    resolve --> apply["usecase<br/>DryRun or Apply"]
    apply --> exit["cli.Execute<br/>prints a returned error once"]

The root pre-run (internal/cli/root.go) runs before every subcommand. Commands like completion skip the steps they don't need.

Step Why
Config Loads .agenticrc.toml once for every later step.
Docker context Picks the context; one from a config file needs approval first.
Migrate Updates the $AGENTIC_HOME layout (usecase/migrate).
Docker Fails early when the daemon is unreachable.
git Only for run, when the tool syncs marketplaces.
Upgrade Offers a due upgrade, then reruns on the new binary (usecase/upgradecheck).

Action commands (build, update, clean, run) then call their usecase in two steps: Resolve checks, asks for approvals and returns a plan without changing anything; DryRun prints the plan, or Apply carries it out.

agentic run

internal/cli/run.go parses the invocation into a request and calls run.Service in internal/usecase/run. Each runtime has a runner there; only runners talk to Docker or msb.

Overview

flowchart LR
    start["root pre-run"] --> parse["parse<br/>tool, image, args, request"]
    parse --> checks["checks<br/>runtime, credentials, image"]
    checks --> approve["prompts<br/>trust, settings, tool update"]
    approve --> resolve["resolve<br/>config + image inspect"]
    resolve --> startRun["Apply: start<br/>RunID, watchdog, temp dir"]
    startRun --> spec["RunSpec<br/>+ instructions snapshot"]
    spec --> runner{runtime}
    runner -->|docker| docker["docker runner"]
    runner -->|microsandbox| msb["microsandbox runner"]
    docker --> endRun["end<br/>save snapshot, remove temp dir"]
    msb --> endRun
  • Resolve covers everything up to the resolved config, the same for every runtime. It picks the runner, and nothing is started yet.
  • Checks before prompts: a run the runtime can't do, a bad credential or a missing image fails before the user approves anything.
  • Apply starts the run, builds its RunSpec, runs it on the runner and ends it. DryRun does the same without a watchdog, printing the docker or msb commands instead.

Docker run over time

sequenceDiagram
    participant cli
    participant svc as run.Service
    participant watch as agentic __watch
    participant dr as docker runner
    participant dc as docker.Client
    cli->>svc: Resolve
    svc-->>cli: Plan
    cli->>svc: Apply(Plan)
    svc->>watch: spawn
    watch-->>svc: ready
    svc->>dr: Run
    dr->>dc: start sidecars (sweep, run guard, network, proxy, dind)
    dr->>dc: run tool
    dr->>dc: stop sidecars (release run guard)
    svc->>svc: end run
    Note over watch: agentic exits
    watch->>svc: Teardown after 5 s
  • The watchdog is a detached process started before anything is created. The run only goes on once it's ready, so its exit is always watched.
  • Sidecars: starting them installs a run guard, released once the tool exits.
  • After exit: the watchdog finds nothing left after a normal run; it matters after a kill. See Teardown.

Steps

Phase Step Where Why
Parse Project dir usecase/run Refuses a working dir Docker can't bind-mount, before its config is read.
Resolve Runtime runtime.go Picks the runner and refuses a run it can't do safely.
Resolve Credentials network/credentials Refuses a bad [[run.proxy.credentials]] entry before any prompt.
Resolve Image image.go Fails early when the tool image isn't built.
Resolve Trust, settings trust.go Asks to trust the dir and approve changed settings; the approved config is the only one a run reads.
Resolve Tool resolve.go Offers a due tool update and creates the tool's host files.
Resolve Config resolve.go Resolves flags against the approved config and inspects the image once.
Apply Start runtime.go Creates the RunID, watchdog and run temp dir before anything the run can leave behind.
Apply Instructions instructions.go Writes the environment instructions snapshot.
Apply RunSpec runspec.go, mounts.go Syncs marketplaces, checks mounts, resolves env and builds the sandbox.RunSpec.
Apply Spec check capabilities.go Refuses a RunSpec needing a feature the runtime lacks.
Apply Run log runlog.go Opens the run's log dir and commands.jsonl, which logs every docker and msb command.
Apply Runner docker.go, microsandbox.go See below.
End Cleanup runtime.go Saves the instructions snapshot back and removes the temp dir, also when the runner failed.

The docker runner creates named volumes and missing sidecar images, then runtime/docker sweeps orphans, creates the run network, starts the proxy and dind sidecars, runs the tool attached to the terminal, and stops the sidecars. It prints the proxy summary from the log last.

The microsandbox runner builds the dockerd image with --dind, loads the image into msb unless it's there, starts agentic-proxy on host loopback with backend = "agentic", and runs the tool VM (runtime/microsandbox).

Each runner checks the mounts again right before a process that mounts them starts (Docker before the sidecars and the tool, msb before the VM), as image builds and sidecar starts give a path time to turn into a symlink.

Teardown

flowchart TD
    exit{How did agentic stop?}
    exit -->|normal, Ctrl-C, SIGTERM/HUP| guard["run guard release + end run"]
    exit -->|terminal closed during docker run| attach["tool attach removes the tool"]
    exit -->|SIGKILL or crash| watchdog["watchdog: wait 5 s, Teardown"]
    watchdog -->|watchdog killed too| sweep["next run's orphan sweep after 5 min"]

Every per-run resource derives from one sandbox.RunID. Containers and networks carry agentic.run, agentic.owner and agentic.started labels. Host files go in $TMPDIR/agentic-run-<id>/, except the dind run dir and the run's log dir under $AGENTIC_HOME.

Each runtime's Teardown(runID) removes one run, best-effort and idempotent; every exit path, the sweep and clean/prune use it. run.Service.Teardown also saves a leftover instructions snapshot and removes the temp dir.

Layer Covers Where
Run guard Normal exit, Ctrl-C, SIGTERM, SIGHUP runtime/docker/guard.go
Tool attach A closed terminal detaching docker run runtime/docker/run.go
Watchdog SIGKILL or a crash usecase/watch, cli/watch.go
Orphan sweep The watchdog killed too, after 5 minutes; Docker only runtime/docker/sweep.go
Stale run dir sweep A run that left only its dind run dir, after 10 minutes runtime/docker/sweep.go
  • The run guard tears the run down before it stops catching signals, so a second Ctrl-C can't cut the teardown short.
  • The watchdog is a hidden agentic __watch. It waits 5 seconds after agentic exits, as a docker call in flight at the kill can still create a container.