Skip to main content

Module registry

Module registry 

Source
Expand description

File-based cross-instance agent registry.

Each AgentMux instance writes agent registrations to {data_dir}/agents/{agent_id}.json. When a local inject fails with “agent not found”, the inject handler looks up this registry and HTTP-forwards the request to the owning instance.

Lifecycle:

  • Register: write file (on HTTP register endpoint + shell auto-register)
  • Unregister: delete file (on HTTP unregister endpoint + process exit)
  • Cleanup: TTL-based removal of stale files at startup

Structs§

AgentEntry
One entry per registered agent in the shared data dir.

Constants§

FORWARD_FAILURE_GRACE_MS
Grace period during which a forward failure against a live-process entry is presumed transient rather than proof the specific agent is gone — see should_evict_on_forward_failure. pub(crate) so muxspect_handlers’s verify-sender route can reuse the same real, already-tested staleness judgment for shared-registry entries instead of inventing its own threshold (see that module’s own doc comment for why).

Functions§

cleanup_stale
Remove stale entries at startup.
cleanup_stale_shared
Startup sweep over the host-global registry: remove any entry whose updated_at is past max_age_ms OR whose pid is no longer alive on this host (§4.4) — the dead-Agent3 case from the spec’s problem statement, where per-channel TTL cleanup never reached an entry belonging to a channel that hadn’t restarted.
init_jekt_public_key_resolver
Install the process’s agent-public-key resolver. Idempotent — first call wins, same contract as init_local_auth_key.
init_local_auth_key
Initialise the process’s local auth key. Idempotent — first call wins.
init_uid_key_resolver
Install the process’s block → (UID, UID public key) resolver. First call wins. Until it is called, entries are written without a UID.
list_all_shared
List every entry currently in the host-global shared registry, across every agent name and channel. Used by the /agentmux/discovery endpoint to populate host.cross_channel[] (§4.5 of the cross-channel delivery spec) — unlike lookup_all_shared, which targets one known agent name, this enumerates the whole directory.
local_channel_id
This process’s own channel id — AGENTMUX_CHANNEL, default "stable".
local_host_label
This machine’s host label — the hostname half of an agent’s WAN instance identity, paired with local_channel_id to identify which AgentMux instance under an account owns a given agent’s WAN keypair.
lookup
Look up an agent entry. Returns None if not found or file is malformed.
lookup_all_shared
Return every live candidate for agent_id across all channels, freshest-first (§4.3: Tier 2b prefers the freshest candidate). Does NOT filter by staleness/pid-liveness — callers needing that should use cleanup_stale_shared (startup sweep) plus the existing evict-on-forward-failure pattern already used by Tier 2a/3 in server/reactive.rs, matching how this codebase already handles registry staleness elsewhere rather than re-validating on every read.
lookup_shared_by_uid
Every channel’s shared entry that published a key for uid (identity M4d-6). Scans the whole registry: entries are filed by name, and one UID live in several channels has an entry, and a key, in each.
pid_alive
True if a process with this PID is currently running on this host. Meaningful here specifically because the shared registry is always same-host by construction (a local file only readable by local processes) — unlike a LAN/cloud registry, PID-liveness is an authoritative staleness signal, not just a heuristic.
remove
Remove an agent entry from the shared registry.
remove_for_block
Remove every per-channel entry registered for block_id, under any name: the agent’s slug, a former slug, or its display name’s route (an entry written during a turn is filed under the block’s current name, not the one its spawn registered). A removal keyed on one name leaves the others behind, and a stopped agent then reads as running. Returns how many files it removed.
remove_if_nonce
Remove an agent entry only if it was written by the spawn with expected_nonce — compare-and-remove for persistent-controller exit-handlers (issue #2363; nonce is process-wide unique, so the guard holds across controller replacement too — codex P1 on PR #2500). An entry with no recorded nonce (0) is never removed by this variant: leaving a stale file to the TTL sweep (cleanup_stale) is strictly safer than deleting a live registration. Atomic w.r.t. this process’s own writes via [REGISTRY_OP_LOCK].
remove_shared
Remove this channel’s entry for agent_id from the shared registry. Best-effort removes the now-possibly-empty agent directory too (a concurrent writer recreating it a moment later is harmless — the next write just re-creates the directory via create_dir_all).
remove_shared_for_block
remove_for_block for this channel’s entries in the host-global shared registry, which files them <shared_dir>/<name>/<channel>.json.
remove_shared_for_block_from_env
Convenience wrapper over remove_shared_for_block — see write_shared_from_env.
remove_shared_from_env
Convenience wrapper over remove_shared — see write_shared_from_env.
remove_shared_from_env_if_nonce
Convenience wrapper over remove_shared_if_nonce — see write_shared_from_env.
remove_shared_if_nonce
remove_shared only if this channel’s entry was written by the spawn with expected_nonce — see remove_if_nonce (issue #2363). Cross-channel note: only this channel’s own file is read and removed, matching write_shared’s single-writer contract, so the process-local [REGISTRY_OP_LOCK] still suffices.
should_evict_on_forward_failure
Should server/reactive.rs’s evict-on-forward-failure sites remove this entry after a failed forward (success:false or a connection error)?
write
Write (create or update) an agent entry in the shared registry.
write_shared
Write (create or update) this channel’s entry for agent_id in the host-global shared registry. Writes only this channel’s own file (shared_channel_path) — never touches another channel’s file, so concurrent writers for different channels of the same agent name cannot race or clobber each other.
write_shared_from_env
Convenience wrapper over write_shared: resolves the shared dir and this process’s channel itself, no-op if the shared root can’t be resolved. Exists so call sites outside server/reactive.rs’s explicit register/unregister handlers (PTY shell auto-register, persistent stream-json controller auto-register — both call the per-channel write/remove directly, not through the HTTP handler) don’t each repeat the same resolve-dir-then-read-env dance.
write_shared_from_env_with_nonce
Convenience wrapper over write_shared_with_nonce — see write_shared_from_env.
write_shared_with_nonce
write_shared, recording the registering persistent-controller spawn’s registration nonce — see AgentEntry::registration_nonce / issue #2363.
write_with_nonce
[write], recording the registering persistent-controller spawn’s registration nonce — see AgentEntry::registration_nonce / issue #2363.