Skip to main content

Module agent_config

Module agent_config 

Source
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§

AgentConfigFile
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.md is foreign — always safe to regenerate in place every launch, unlike CLAUDE.md itself 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.md it 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. See docs/specs/SPEC_CLAUDE_MD_OWNERSHIP_PROTECTION_2026_08_22.md.
CLAUDE_MD_PRE_ADOPT_BACKUP
Where an adopted legacy CLAUDE.md is kept (is_legacy_agentmux_claude_md).
MANAGED_MCP_SERVERS_MANIFEST
Names of the mcpServers entries AgentMux wrote into a working directory’s .mcp.json on the last materialization — see write_mcp_json_respecting_user_servers. Lives beside MANAGED_SKILL_FILES_MANIFEST for 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/skills content.
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_type value that materializes a skill as an Agent Skills-format .claude/skills/<slug>/SKILL.md instead of a .claude/commands/<trigger>.md slash command. Any other value (in practice, always "prompt" today) keeps the pre-existing slash-command behavior. See docs/specs/REPORT_ARMORY_BUNDLE_STANDARD_RESEARCH_2026_07_16.md Phase 0 — skill_type already 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.json content with the auto-injected AgentMux MCP server entry.
build_mcp_config_from_refs
Build .mcp.json from standalone McpServer ref rows (v1 composable model).
build_settings_with_hooks
Build .claude/settings.json content 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) — see docs/specs/SPEC_COMPACTION_DETECTION_AND_HANDLING_2026_07_31.md §4.2. User-supplied settings.json (from the agent’s content_map["settings"]) is parsed and merged at the top level; user-supplied legacy hooks content (from content_map["hooks"]) is merged into settings.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 in content with values from vars.
inject_jekt_signing_keys_into_mcp_json
Best-effort: make sure agent_slug has 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.json file’s mcpServers.agentmux.env block 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 content is exactly what an older AgentMux wrote as CLAUDE.md, in its order, ignoring blank lines: the # Available Skills heading, 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 filenames that are skill-derived managed paths (the ones tracked in MANAGED_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 with memory: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 the WriteAgentConfig “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. See docs/specs/SPEC_CLAUDE_MD_OWNERSHIP_PROTECTION_2026_08_22.md.
write_managed_skill_file_manifest
Record new_managed_paths as the manifest for the NEXT call to cleanup_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.json into base_path without destroying a user’s own MCP server entries (issue #3680). Shared by agent.open and the WriteAgentConfig “click Launch” path — the two config-materializing call sites this module keeps in sync.
write_startup_instructions_respecting_existing
Write a NON-CLAUDE.md startup-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.