State and backends
Where the CLI keeps state, how locking works, and how to recover from drift.
For what state contains and why it is safe to keep, see Security: State. This page covers operating it.
Location
The CLI stores state in the project directory:
| Path | Content |
|---|---|
.amicu/state/state.json | The applied resource graph, the started endpoints, the pending deployments and the stop records (below) |
.amicu/state/state.json.bak | The previous version of state.json, copied on save only when the current file is valid |
.amicu/state/state.lock | The lock file |
.amicu/state/state.cas | Short lock held only while a save checks the serial and swaps the file |
.amicu/config.yaml | Project config. init writes state.backend: local. |
local is the only state backend the CLI implements. There is no remote state backend for the CLI yet.
Treat state.json as sensitive operational data. Do not edit it by hand. amicu init adds .amicu/ to .gitignore. Do not share state through version control: the lock is local to one machine, so two people applying from a committed state.json can overwrite each other and orphan running resources. A state serial with compare-and-swap saves is planned.
Locking
apply, destroy and amicu modal stop take an advisory lock on .amicu/state/state.lock for their whole read, refresh, plan, apply, and save sequence. The lock belongs to the operating system process: when the process dies, the kernel releases it, so there is no stale lock to clean up.
If another amicu process holds the lock, the command fails with a message naming the holder (pid, command, start time). To wait instead, pass a timeout:
amicu apply --lock-timeout 60State version
Current state files are version v4. amicu reads v1 to v4 files, and migrates older files forward on load, one version at a time (v1 to v2, v2 to v3, then v3 to v4). The file on disk becomes v4 the next time amicu saves it, for example on apply or destroy. Besides the graph and endpoints, v4 holds these fields:
-
serial: a counter that goes up by one on every save. A save only goes through when the file on disk still has the serial that amicu read, so a second concurrent amicu run on the same machine that got past the lock makes the save fail with a "changed since it was read" error instead of overwriting the other run. Nothing is merged or discarded; re-run the command. The check is not a distributed lock: state shared through version control or a synced folder is unsupported. Older files read as serial 0. -
project_id: an 8-digit hex id, minted when state is first written. It scopes the host service labels (see Host service labels). -
pending: deployments whose start was begun but not confirmed. Amicu writes them before the provider starts anything.plantreats them as absent, soapplyrecreates them, anddestroytries to confirm their stop. -
stops: one stop record per instance that may be running. Each record carries what is needed to stop it from state alone: the backend (launchd,systemdormodal), the identity (launchd label, systemd unit or Modal app name), and the provider's id once known. Host records also keep the uid, for launchd the domain (gui/<uid>oruser/<uid>), the path of the service file, and the log files. Modal records keep the workspace, the environment, and the names of the cache volumes the app mounts. A record is removed only after its stop is confirmed. -
fingerprints: SHA-256 digests of the secret pins, container image and binary path each deployment was last confirmed started with. See Plan, apply and state. Deleting this map only resets the baseline.
Amicu refuses to act on a record when the current machine or account does not match it. Each refusal names the mismatch and says how to fix it: switch MODAL_TOKEN_ID and MODAL_TOKEN_SECRET to the recorded Modal workspace, or re-run as the user that started the host instance. Nothing is changed in that case.
State written by v1 amicu has no pending or stops. The stop records are derived from the graph on load, and a v1 Modal app is stamped with a workspace only after amicu proves the app is in the active one. State written by v2 has no project_id, and state before v4 has no serial.
amicu refuses a state file written by a newer version, and says to upgrade amicu rather than overwrite the file. Unknown top-level fields and unknown fields in stop records are kept on save. An unknown field in an endpoint is an error with the same advice, because endpoints cannot keep unknown fields. A build that only knows older versions refuses a v4 file, so do not run one on a project after destroy or apply has saved v4.
Host service labels
A local deployment runs as a launchd job or systemd user unit labelled io.amicu.llama.<project_id>.<deployment>. The project_id is stored in .amicu/state/state.json, so two projects never share a label.
- To stop a service by hand:
launchctl bootout gui/$(id -u)/<label>on macOS, orsystemctl --user disable --now <label>on Linux, orschtasks /End /TN "amicu\<label>"thenschtasks /Delete /TN "amicu\<label>" /Fon Windows. Then runamicu destroyso that state and the service file are cleaned up too. - Deployments created before v3 keep the unscoped label
io.amicu.llama.<deployment>until their nextapply. Thatapplystops the old job and confirms the stop before it starts the new one. - Do not copy
.amicu/to another checkout. Both copies would have the sameproject_id, and each could stop the other's instances. To work from another checkout, runamicu destroyin the first one, then apply in the second. - Do not delete state while instances are running. Their stop identity exists only in state, so deleting it orphans them. Run
amicu destroyfirst.
Partial apply and destroy
If an apply or destroy fails after starting or stopping some deployments, those changes are recorded in state and the error lists them. Fix the cause and re-run the same command: deployments that already succeeded show no change and the command finishes the rest. See Security: State and Plan, apply and state.
Modal app names
A Modal app is named amicu-<project>-<deployment>-<digest>: a short form of the project id, the deployment id lowercased, and a digest of the exact project and deployment ids. Deployment ids that differ only in case or separator, and the same id in two projects, get different apps. The name is recorded in the stop record, so stop, refresh and amicu modal stop always use the recorded name.
One-time migration. State written by an earlier version records an app named amicu- plus the deployment id. The next amicu apply that launches such a deployment stops the old app, waits for the stop to be confirmed, then starts the new one. amicu plan shows this as a destroy old step. Expect the endpoint URL to change (clients must update), a short downtime, and no rollback to the previous version if the new deploy fails. The old app is never left running: if its stop is not confirmed, apply aborts and keeps its record.
Drift
State can fall out of date if something changes a deployment outside amicu.
- Remote providers (Modal and RunPod). Before each
plan,applyanddestroy, amicu asks the provider which recorded apps, endpoints or pods still exist. A deployment whose app is not found by name is kept in state aspending, and adrift:line is printed (to stderr with--output json).planthen shows acreate,applyrecreates the app, anddestroytries to confirm the stop by app id or, if none is recorded, by name. If the lookup fails, amicu prints awarning: could not check Modal for live appsand uses state as is. - Local services. Before
plan,applyanddestroy, amicu asks launchd or systemd whether each recorded service is running. A service that is confirmed gone (after a reboot, a crash, or a manual removal) is kept in state aspending, and adrift:line is printed.planthen shows acreate,applyrestarts the service, anddestroystops it and confirms the stop. If the check cannot be completed, amicu printswarning: could not check host servicesand uses state as is. See This machine.
Failed updates
If a host update fails, the deployment stays pending in state with its stop record. The next plan shows a create, and amicu destroy --target <id> stops the instance and confirms the stop.
Starting over
If state.json is corrupt or missing but state.json.bak is valid, amicu refuses to continue and names the backup. Inspect it, and if it is correct restore it with cp .amicu/state/state.json.bak .amicu/state/state.json.
If state is lost or corrupt with no usable backup, plan shows create for everything, and amicu no longer knows how to stop what it started. Stop each instance by hand first: launchctl bootout gui/$(id -u)/<label> on macOS, systemctl --user disable --now <label> on Linux, schtasks /End /TN "amicu\<label>" then schtasks /Delete /TN "amicu\<label>" /F on Windows, or amicu modal stop <app> for Modal. The labels are io.amicu.llama.<project_id>.<deployment> (or io.amicu.llama.<deployment> for older deployments). Then delete state.json and state.json.bak and apply. Do not delete state while instances are running: the instances are then orphaned, and the stop identity that only state held is gone. Only do this with explicit intent.
Last updated