Expand description
Pure config-building logic for agent definitions.
Builds an agent launch’s config files (the startup file, settings, skills, .mcp.json) and expands
the {{...}} template variables. agent.open and the window’s Launch both render them here.
Most functions here are pure — no I/O, no async. The exception is the
managed skill files manifest section near the end: real filesystem
I/O shared by the two independent RPC handlers that materialize config
files to disk (agent.open’s write_agent_config_files in
server/app_api/agent_open.rs, and writeagentconfig in
server/editor_handlers.rs — the latter is the actual “click Launch”
path used on every normal agent launch). Lives here rather than in
either handler file so the two callers can’t drift out of sync on the
manifest format or the path-traversal defense (reagent P1, PR #2322 —
writeagentconfig initially had no cleanup at all).
Structs§
- Agent
Config File - A single file to be written to the agent working directory.
Constants§
- AGENTMUX_
MEMORY_ FILENAME - AgentMux-owned side file carrying the full Soul+AgentMD+Bundle+Skills
composition when the real
CLAUDE.mdis foreign — always safe to regenerate in place every launch, unlikeCLAUDE.mditself in that case. - BUNDLE_
SECTION_ HEADING - Heading of the bundle-content section in a generated
CLAUDE.md. - CLAUDE_
MD_ MANAGED_ MARKER - First line AgentMux writes on any
CLAUDE.mdit fully owns (freshly created, or regenerated on every launch since). Its ABSENCE on an EXISTING file is what marks that file as foreign — predates AgentMux touching this working directory, or a human replaced AgentMux’s file with their own — and is never overwritten again. Seedocs/specs/SPEC_CLAUDE_MD_OWNERSHIP_PROTECTION_2026_08_22.md. - CLAUDE_
MD_ PRE_ ADOPT_ BACKUP - Where an adopted legacy
CLAUDE.mdis kept (is_legacy_agentmux_claude_md). - MANAGED_
MCP_ SERVERS_ MANIFEST - Names of the
mcpServersentries AgentMux wrote into a working directory’s.mcp.jsonon the last materialization — seewrite_mcp_json_respecting_user_servers. Lives besideMANAGED_SKILL_FILES_MANIFESTfor the same reason: it lets the next launch tell entries it owns from entries the user put there. - MANAGED_
SKILL_ FILES_ MANIFEST - Hidden manifest (relative to the agent working directory) tracking which
skill-derived paths (
.claude/commands/*.md,.claude/skills/*/SKILL.md) AgentMux itself wrote on the last materialization, so a subsequent one can delete ones that are no longer current without touching any user-authored.claude/commands/.claude/skillscontent. - MCP_
JSON_ WRITE_ LOCK - Held (exclusively) for the whole read-merge-write in
write_mcp_json_respecting_user_servers. See its doc comment. - SKILL_
TYPE_ AGENT_ SKILL skill_typevalue that materializes a skill as an Agent Skills-format.claude/skills/<slug>/SKILL.mdinstead of a.claude/commands/<trigger>.mdslash command. Any other value (in practice, always"prompt"today) keeps the pre-existing slash-command behavior. Seedocs/specs/REPORT_ARMORY_BUNDLE_STANDARD_RESEARCH_2026_07_16.mdPhase 0 —skill_typealready existed end-to-end but was never branched on before this, making it the lowest-friction place to hang the format discriminator rather than adding a new column.
Functions§
- build_
config_ files - Build the list of config files to write to the agent working directory.
- build_
mcp_ config - Build
.mcp.jsoncontent with the auto-injected AgentMux MCP server entry. - build_
mcp_ config_ from_ refs - Build
.mcp.jsonfrom standalone McpServer ref rows (v1 composable model). - build_
settings_ with_ hooks - Build
.claude/settings.jsoncontent with the auto-injected PreToolUse Bash hook and PreCompact hooks (under the"hooks"key). PreToolUse redirects Bash invocations into the streaming wrapper (agentmux-bashwrap exec); PreCompact (two entries, matcher"manual"/"auto") pings the sidecar the instant compaction starts (agentmux-bashwrap precompact) — seedocs/specs/SPEC_COMPACTION_DETECTION_AND_HANDLING_2026_07_31.md§4.2. User-supplied settings.json (from the agent’scontent_map["settings"]) is parsed and merged at the top level; user-supplied legacy hooks content (fromcontent_map["hooks"]) is merged intosettings.hooks. - cleanup_
stale_ managed_ skill_ files - Delete skill-derived files a PREVIOUS materialization wrote (per the
on-disk manifest) but that are no longer part of
new_managed_paths– e.g. a skill’s format switched between"prompt"/"agent-skill", or it was renamed/removed. Without this, the stale file stays on disk and Claude keeps treating it as active alongside the newly selected format (reagent P1 + Codex P1/P2, PR #2322). - expand_
template - Replace
{{VARIABLE}}placeholders incontentwith values fromvars. - inject_
jekt_ signing_ keys_ into_ mcp_ json - Best-effort: make sure
agent_slughas its jekt/LAN signing keys (SPEC_JEKT_TRUST_LAYER_COMPLETION_2026_08_13.md§2.2,SPEC_JEKT_LAN_TIER_SIGNING_2026_08_15.md§2.1), and write the rest of what a.mcp.jsonfile’smcpServers.agentmux.envblock carries for them. The file holds references, not values: no signing value is written, and one an older version wrote is removed. The agent’s MCP server asks srv for its keys with its own token (/agentmux/agents/self/keys). - is_
agentmux_ managed_ instructions - Does this file’s content say AgentMux wrote it?
- is_
legacy_ agentmux_ claude_ md - Whether
contentis exactly what an older AgentMux wrote asCLAUDE.md, in its order, ignoring blank lines: the# Available Skillsheading, its usage line, at least one skill row (- **…; AgentMux wrote the section only when there were skills), then optionally the managed import (its comment and line, or the line alone). Anything else means a person wrote there, and it stays theirs. - managed_
skill_ file_ paths - Compute the subset of
filenamesthat are skill-derived managed paths (the ones tracked inMANAGED_SKILL_FILES_MANIFEST) — everything else (CLAUDE.md,.mcp.json,.claude/settings.json, …) is always fully regenerated at a fixed path every launch, so it has no staleness problem to track. - render_
skill_ md - Render an Agent Skills-format
SKILL.md: YAML frontmatter with the two required fields (name,description— per the spec’s six-field frontmatter; AgentMux doesn’t populate the four optional ones today:license,compatibility,metadata,allowed-tools), followed by the skill’s content as the Markdown body. See https://agentskills.io/specification. - skills_
index_ text - The startup file’s skills index, each skill by its
/<trigger>; empty when it lists none. Claude Code lists the skills it has a file for (.claude/commands, .claude/skills) by itself, so for it the index carries only those without one (LC3, §4.4). By the resolved provider, so an alias (claude-code) counts as Claude. Shared withmemory:launchcost. - skills_
to_ agent_ skills - Convert standalone Skill records into the AgentSkill shape expected by
build_config_files. Used when the v1 ref tables are non-empty. - write_
claude_ md_ respecting_ ownership - Shared by
agent.open(server/app_api/agent_open.rs) and theWriteAgentConfig“click Launch” path (server/editor_handlers.rs) — per this module’s own doc comment, the two config-materializing call sites that must not drift out of sync. Seedocs/specs/SPEC_CLAUDE_MD_OWNERSHIP_PROTECTION_2026_08_22.md. - write_
managed_ skill_ file_ manifest - Record
new_managed_pathsas the manifest for the NEXT call tocleanup_stale_managed_skill_files. Best-effort: a write failure is logged, not propagated — losing this write only means the next materialization’s stale-file cleanup is skipped once, not a correctness issue for the current one. - write_
mcp_ json_ respecting_ user_ servers - Write
.mcp.jsonintobase_pathwithout destroying a user’s own MCP server entries (issue #3680). Shared byagent.openand theWriteAgentConfig“click Launch” path — the two config-materializing call sites this module keeps in sync. - write_
startup_ instructions_ respecting_ existing - Write a NON-
CLAUDE.mdstartup-instructions file (AGENTS.md,GEMINI.md,QWEN.md,.pi/APPEND_SYSTEM.md, …) WITHOUT ever overwriting a pre-existing, non-AgentMux-authored file at that path.