Skip to main content

Module agent_lifecycle

Module agent_lifecycle 

Source
Expand description

What keeps agents that outlive their pane from piling up (docs/specs/SPEC_ALWAYS_AVAILABLE_AGENTS_2026_10_27.md Β§7, D5 and D6; P4a). With agents:always_available on, closing a pane no longer stops its agent, so two limits stand in for the pane:

  • Idle stop (D5). An agent with no view (its block is parked) that has been idle for the window (agents:idle_stop_minutes, default 60, 0 = never; agent:idle_stop_minutes on its block overrides it) is stopped by a sweep once a minute. It stays addressable: a message starts it and resumes its session (jekt_start). Never stopped: an agent in a turn, one waiting on the user, one that holds a claimed Kanban task or has a held message, or one that holds the build slot.
  • Cap (D6). A start (agent_attach::start_agent: agent.start, a message, a Job) is admitted while fewer than agents:max_running (default 24, 0 = no cap) agent processes run and, on Windows, commit use is under agents:commit_limit_pct (default 85, 0 = off). Over either, the agent idle longest with no view is stopped to make room; when none can be, the start is refused with capacity: and a message stays held.

An agent that is shown in a pane is never stopped by either: it has a view. Off, none of this runs.

StructsΒ§

Live πŸ”’
A running agent process and what the policy needs to know about it.
Policy πŸ”’
The limits, from settings. None: that limit is off.
Wanted πŸ”’
An agent a batch would start: the key its room is held under (its id, or a stand-in while it does not exist yet) and the name a refusal gives it.

EnumsΒ§

Over πŸ”’
Which limit a start is over, and why.

ConstantsΒ§

COMMIT_FREED_BY_ONE_STOP_GB πŸ”’
What stopping one idle agent frees, in GB of commit: a claude.exe holds about 1 GB of private commit (agents::runner::DEFAULT_AGENT_COMMIT_RESERVE_GB has the measurement).
DEFAULT_COMMIT_LIMIT_PCT πŸ”’
DEFAULT_IDLE_STOP_MINUTES πŸ”’
DEFAULT_MAX_RUNNING πŸ”’
META_IDLE_STOP_MINUTES
Block meta: this agent’s own idle window in minutes (0 = never stop it).
PENDING_FOR πŸ”’
How long an admitted start counts as running before its process shows as such.
SETTING_COMMIT_LIMIT_PCT
SETTING_IDLE_STOP_MINUTES
SETTING_MAX_RUNNING
SWEEP_EVERY πŸ”’
How often the idle sweep runs.

FunctionsΒ§

admission πŸ”’
One admission at a time: the count, the stop that makes room and the claim of the room are one step.
admit πŸ”’
admit_batch πŸ”’
May all of wanted start? A batch never stops an agent to make room (a start may stop one idle agent with no view; five of them stopping five would be a mass stop nobody asked for), and it is admitted whole or refused whole. Those already running need no room. With reserve the room is claimed under the same lock as the check, so a start that comes between this and the batch’s own cannot take it; release_batch gives it back when the batch is done or failed.
admit_batch_with πŸ”’
admit_start πŸ”’
May agent_id be started? Yes when it already runs, when nothing is over a limit, or when an idle agent with no view can be stopped to make room; otherwise Err("capacity: ...").
admit_start_with πŸ”’
admit_start_without_eviction πŸ”’
admit_start for a start that must not cost another agent its process (restart resume): over a limit it is refused with Err("capacity: ...") where admit_start would stop an idle agent to make room. The check and the claim of the room are one step under the admission lock.
batch_held πŸ”’
The reservations a batch made (admit_batch): only these let their agent’s start through without counting the room again, and only once. An ordinary reservation (a start that was admitted) does not, or an agent started, stopped and started again within PENDING_FOR would skip the cap.
build_slot_holder πŸ”’
The agent that holds the host’s build slot (~/.agentmux/shared/locks/build-slot.sh), if any.
commit_over πŸ”’
The commit use and limit (percent) when use is over the limit, whichever limit over_limit names first (it names the cap before commit): a stop for the cap is as useless as one for commit when commit use is past what one stop frees.
commit_total_gb πŸ”’
The commit (GB) the host can hold in all, where it reports one (Windows: GlobalMemoryStatusEx).
commit_used_pct πŸ”’
Commit use as a percentage of the commit limit, where the host reports it (Windows: GlobalMemoryStatusEx). None elsewhere: Linux overcommits and macOS has no commit limit, so no figure means no refusal.
exempt πŸ”’
Why this agent is not stopped even when idle: it holds a claimed Kanban task, has a message held for it, or holds the build slot. None: nothing keeps it.
exempt_agent πŸ”’
exempt for the agent uid, whose slug (else its id) is agent_id: what a Kanban claim and the build slot name.
holder_in πŸ”’
The agent= line of a build-slot owner file.
idle_from_view_lost πŸ”’
Move each parked agent’s since_ms up to when it was first seen parked, and forget blocks that are shown again or gone. A sweep sees a pane’s closing within a minute, so the window can only run long.
idle_override_of πŸ”’
The block’s own idle window: Some(None) never stop it, Some(Some(ms)) its own window.
idle_window_of πŸ”’
The idle window that applies to block: its own, else the default (None: no window).
install πŸ”’
Run the idle sweep once a minute.
is_reserved πŸ”’
Whether agent_id holds a reservation that has not run out.
live_agents πŸ”’
The agent processes running now, one per agent block that has a live controller.
names_in_words πŸ”’
β€œA”, β€œA and B”, β€œA, B and C”.
one_stop_brings_commit_down πŸ”’
Whether stopping one idle agent can bring commit use down to the limit. Err says how much has to go and that nothing was stopped, so a start that stops an agent for nothing is never made. With no total to size the shortfall against (non-Windows, or a failed read) it cannot tell, and the stop goes ahead.
over_limit πŸ”’
Whether the host is at its cap or over its commit limit, given the agents running.
pending πŸ”’
pending_key πŸ”’
The key of an admitted start: per store, so two instances in one process (tests) do not count each other.
pending_not_running πŸ”’
The admitted starts of this instance that are not yet running and not yet PENDING_FOR old. One seen among live is dropped for good: it counts as itself from then on, and if it is stopped to make room it must not count a second time.
policy πŸ”’
rekey_reservation πŸ”’
The room admit_batch claimed under the stand-in key from is now the agent to’s, in one step: a batch that makes its agents as it goes (spawn) keeps its room under stand-ins until each exists, and the agent’s own start must find its room reserved, not count it against itself.
release_batch πŸ”’
Give back the room admit_batch claimed for wanted: the batch is done (its agents run, and count as themselves) or failed.
release_start πŸ”’
A start that was admitted did not happen (the agent is unknown, its provider has no headless mode, its CLI is missing): its room is free again.
running_agent_ids πŸ”’
The ids of the agents running in this process: the only ones whose memory folders it looks at (native_drift::OwnedAgents). Live::uid is the db_agents row id, the same id as the agent definition and the memory roster (AgentInstance::id == definition_id, row.rs).
start_blocked πŸ”’
Why agent_id’s start would be refused now: admit_start without its side effects (it stops no one and claims no room), so a sender is not told β€œstarting” about a start that will not happen. None: it would go ahead, or no limit applies.
start_blocked_with πŸ”’
stop πŸ”’
Stop live the way an explicit stop does (its process; the block, the transcript and the session stay).
stop_note πŸ”’
The words of a note to the lead of an agent that was stopped to make room for another’s start: who, why, what it was working on (its goal, cut short), and how to get it back.
sweep_once πŸ”’
One idle sweep: stop each agent with no view that has been idle past its window and that nothing keeps running. Returns how many were stopped.
take_batch_claim πŸ”’
Whether agent_id’s start is the one a batch reserved room for, taking that claim: the next start of the agent is counted as any other.
tell_leader_of_stop πŸ”’
Tell the stopped agent’s lead (see worker_notes::tell_leader) that admission stopped it: losing an agent in silence is what this exists to prevent.
view_lost_at πŸ”’
When each parked agent block was first seen without a view (ms). The agent-state tracker times how long a state has lasted, shown in a pane or not; the idle window is for the time with no view.

Type AliasesΒ§

Reservation πŸ”’
Starts admitted but not yet seen running, so a burst of starts counts each other. Each entry holds a Weak to its instance’s store: that keeps the store’s address from being reused while the entry exists, and an entry whose store is gone (a test’s) is dropped.