Repository navigation
feat: install shared agent instructions for Claude and Codex #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| usage() { | ||
| printf '%s\n' \ | ||
| 'Usage: install-agents-md.sh [--agent claude|codex|all] [--scope global|project]' \ | ||
| 'Defaults: --agent all --scope global. --agent also accepts claude,codex.' \ | ||
| 'Global: CLAUDE_CONFIG_DIR/CLAUDE.md and CODEX_HOME/AGENTS.md.' \ | ||
| 'Unset or empty overrides default to ~/.claude and ~/.codex, respectively.' \ | ||
| 'Project: CLAUDE.md and AGENTS.md in the current directory.' | ||
| } | ||
|
|
||
| fail() { | ||
| printf 'Error: %s\n' "$*" >&2 | ||
| exit 1 | ||
| } | ||
|
|
||
| agent=all | ||
| scope=global | ||
| while (($#)); do | ||
| case "$1" in | ||
| --agent|--scope) | ||
| (($# >= 2)) || fail "Missing value for $1" | ||
| case "$1" in | ||
| --agent) agent=$2 ;; | ||
| --scope) scope=$2 ;; | ||
| esac | ||
| shift 2 | ||
| ;; | ||
| --help|-h) usage; exit 0 ;; | ||
| *) fail "Unknown argument: $1" ;; | ||
| esac | ||
| done | ||
|
|
||
| case "$agent" in | ||
| claude|codex|all|claude,codex|codex,claude) ;; | ||
| *) fail "Invalid agent: $agent (expected claude, codex, or all)" ;; | ||
| esac | ||
| case "$scope" in | ||
| global|project) ;; | ||
| *) fail "Invalid scope: $scope (expected global or project)" ;; | ||
| esac | ||
|
|
||
| targets=() | ||
| if [[ "$scope" == global ]]; then | ||
| claude_target="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/CLAUDE.md" | ||
| codex_target="${CODEX_HOME:-$HOME/.codex}/AGENTS.md" | ||
| else | ||
| claude_target="$PWD/CLAUDE.md" | ||
| codex_target="$PWD/AGENTS.md" | ||
| fi | ||
| [[ "$agent" == codex ]] || targets+=("$claude_target") | ||
| [[ "$agent" == claude ]] || targets+=("$codex_target") | ||
|
|
||
| begin='<!-- BEGIN KILLALL AGENTS.md -->' | ||
| end='<!-- END KILLALL AGENTS.md -->' | ||
| script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) | ||
| template="$script_dir/../templates/AGENTS.md" | ||
|
|
||
| validate_markers() { | ||
| awk -v begin="$begin" -v end="$end" -v required="$2" ' | ||
| $0 == begin { count++; if (inside || count > 1) invalid = 1; inside = 1 } | ||
| $0 == end { if (!inside) invalid = 1; inside = 0 } | ||
| END { exit (invalid || inside || (required && count != 1)) } | ||
| ' "$1" || fail "Missing, duplicate, or unbalanced markers in $1" | ||
| } | ||
|
|
||
| validate_markers "$template" 1 | ||
| # Check every destination before changing any of them. | ||
| for target in "${targets[@]}"; do | ||
| [[ ! -e "$target" ]] || validate_markers "$target" 0 | ||
| done | ||
|
|
||
| work=$(mktemp -d) | ||
| trap 'rm -rf -- "$work"' EXIT | ||
| awk -v begin="$begin" -v end="$end" ' | ||
| $0 == begin { inside = 1 } | ||
| inside { print } | ||
| $0 == end { inside = 0 } | ||
| ' "$template" > "$work/block" | ||
|
|
||
| for target in "${targets[@]}"; do | ||
| source=$target | ||
| [[ -e "$source" ]] || source=/dev/null | ||
| awk -v begin="$begin" -v end="$end" -v block="$work/block" ' | ||
| BEGIN { | ||
| while ((getline line < block) > 0) content = content line ORS | ||
| close(block) | ||
| } | ||
| $0 == begin { printf "%s", content; inside = 1; found = 1; next } | ||
| $0 == end { inside = 0; next } | ||
| !inside { print } | ||
| END { | ||
| if (!found) { | ||
| if (NR) print "" | ||
| printf "%s", content | ||
| } | ||
| } | ||
| ' "$source" > "$work/output" | ||
| mkdir -p -- "$(dirname -- "$target")" | ||
| cat "$work/output" > "$target" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A failed copy can truncate a dotfile. The The trigger needs a filesystem with too little free space for the file's net growth. The impact is limited to that case. Do not replace the destination with a temporary file moved over it. Plain Use this sequence instead:
|
||
| printf 'Installed %s\n' "$target" | ||
| done | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <!-- BEGIN KILLALL AGENTS.md --> | ||
| # Working agreement | ||
|
|
||
| - Be concise, constructive, and willing to push back. Clarify terminology and ask when ambiguity changes scope, behavior, or safety. | ||
| - Inspect the working tree before editing and preserve unrelated user changes. Do not commit or push without user authorization. | ||
|
|
||
| ## Code | ||
|
|
||
| - Write code that reads like prose: clear names, cohesive modules, and a main flow a reader can follow in order. | ||
| - Build the smallest design that solves the task. Avoid speculative features and abstractions; generalize when real repeated cases justify it. | ||
| - Prefer pure functions and composition, with IO at the edges and dependencies passed as arguments. Group related values in immutable parameter objects when useful. | ||
| - Keep mutable state inside its owning module. Global mutable state requires the user's approval and a stated reason. | ||
| - Give functions one responsibility and modules a small interface with substantial behavior behind it. Extract helpers to clarify intent, not merely shorten functions. | ||
| - Introduce interfaces where a second implementation exists; a test double counts. Name modules for their domain, not `utils`. | ||
| - Before deleting code and its obsolete tests, check public APIs and external consumers; local non-use is insufficient. Ask before deleting when external use remains uncertain. | ||
| - Comments explain constraints and non-obvious reasoning, not what the code already says. Remove stale or misleading documentation; keep unrelated fixes separate. | ||
| - Prefer the latest compatible stable release for new dependencies. Upgrade existing dependencies only when required by the task or explicitly approved. | ||
| - Trade readability for speed only when measurements justify it. | ||
|
|
||
| ## Verification | ||
|
|
||
| - Before editing, state the check that proves the work done. For behavior changes, demonstrate a test failing for the intended reason, then make it pass. For documentation changes, check accuracy, consistency, and the diff. | ||
| - Test one behavior at a time and name tests for their assertions. Assert on behavior and concrete error types; exact text only when text is the contract. Keep unit and integration tests separately runnable. | ||
| - Treat all agent output, including your own, as an unreviewed draft. Verify consequential claims against source evidence, run applicable checks, and read the diff. | ||
| - Fix failures caused by the change. Investigate latent failures it exposes; resolve them or report them as blockers. Report unrelated findings separately rather than silently expanding scope. | ||
| - At handoff, report changes, validation, and remaining work. Distinguish passed, failed, and skipped checks; label unverified explanations as hypotheses. | ||
|
|
||
| ## Project-specific workflows | ||
|
|
||
| - Keep language tooling, infrastructure safeguards, task tracking, and release procedures in the projects that need them. | ||
| - Follow the project's checks, review, changelog, and release requirements. Draft PRs must disclose incomplete work; merge-ready work must satisfy required gates. Hotfix exceptions require authorized maintainer approval and documented waived checks, risks, and follow-up. | ||
| - Before pushing, fetch and check divergence from the actual PR target branch. Resolve conflicts without overwriting others' work and follow the project's branch-freshness policy. | ||
| - For CI or review activity, use an available waiting skill before writing a polling loop. | ||
| <!-- END KILLALL AGENTS.md --> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import os | ||
| from pathlib import Path | ||
| import subprocess | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| ROOT = Path(__file__).resolve().parents[2] | ||
| SCRIPT = ROOT / "scripts" / "install-agents-md.sh" | ||
| BEGIN = "<!-- BEGIN KILLALL AGENTS.md -->" | ||
| END = "<!-- END KILLALL AGENTS.md -->" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def workspace(tmp_path, monkeypatch): | ||
| for variable in ("CLAUDE_CONFIG_DIR", "CODEX_HOME"): | ||
| monkeypatch.delenv(variable, raising=False) | ||
| home = tmp_path / "home" | ||
| project = tmp_path / "project" | ||
| home.mkdir() | ||
| project.mkdir() | ||
| return home, project | ||
|
|
||
|
|
||
| def install(workspace, *args): | ||
| home, project = workspace | ||
| return subprocess.run( | ||
| ["bash", str(SCRIPT), *args], | ||
| cwd=project, | ||
| env={**os.environ, "HOME": str(home)}, | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
|
|
||
|
|
||
| def template(): | ||
| return (ROOT / "templates" / "AGENTS.md").read_text() | ||
|
|
||
|
|
||
| def test_defaults_install_both_global_files_without_touching_project(workspace): | ||
| home, project = workspace | ||
| result = install(workspace) | ||
| assert result.returncode == 0, result.stderr | ||
| assert (home / ".claude" / "CLAUDE.md").read_text() == template() | ||
| assert (home / ".codex" / "AGENTS.md").read_text() == template() | ||
| assert list(project.iterdir()) == [] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("agent", "variable", "filename"), | ||
| [("claude", "CLAUDE_CONFIG_DIR", "CLAUDE.md"), | ||
| ("codex", "CODEX_HOME", "AGENTS.md")], | ||
| ) | ||
| def test_global_scope_respects_config_directory_override( | ||
| workspace, monkeypatch, tmp_path, agent, variable, filename | ||
| ): | ||
| home, project = workspace | ||
| config = tmp_path / "custom config" / agent | ||
| monkeypatch.setenv(variable, str(config)) | ||
| result = install(workspace, "--agent", agent) | ||
| assert result.returncode == 0, result.stderr | ||
| assert (config / filename).read_text() == template() | ||
| assert list(home.iterdir()) == [] | ||
| assert list(project.iterdir()) == [] | ||
|
|
||
|
|
||
| def test_empty_config_overrides_use_default_global_directories(workspace, monkeypatch): | ||
| home, _ = workspace | ||
| monkeypatch.setenv("CLAUDE_CONFIG_DIR", "") | ||
| monkeypatch.setenv("CODEX_HOME", "") | ||
| result = install(workspace) | ||
| assert result.returncode == 0, result.stderr | ||
| assert (home / ".claude" / "CLAUDE.md").read_text() == template() | ||
| assert (home / ".codex" / "AGENTS.md").read_text() == template() | ||
|
|
||
|
|
||
| def test_project_scope_ignores_global_config_overrides(workspace, monkeypatch, tmp_path): | ||
| home, project = workspace | ||
| config = tmp_path / "global config" | ||
| monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(config / "claude")) | ||
| monkeypatch.setenv("CODEX_HOME", str(config / "codex")) | ||
| result = install(workspace, "--scope", "project") | ||
| assert result.returncode == 0, result.stderr | ||
| assert (project / "CLAUDE.md").read_text() == template() | ||
| assert (project / "AGENTS.md").read_text() == template() | ||
| assert not config.exists() | ||
| assert list(home.iterdir()) == [] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("agent", "files"), | ||
| [("claude", ["CLAUDE.md"]), ("codex", ["AGENTS.md"]), | ||
| ("all", ["CLAUDE.md", "AGENTS.md"]), | ||
| ("claude,codex", ["CLAUDE.md", "AGENTS.md"])], | ||
| ) | ||
| def test_project_scope_installs_only_selected_agents(workspace, agent, files): | ||
| home, project = workspace | ||
| result = install(workspace, "--agent", agent, "--scope", "project") | ||
| assert result.returncode == 0, result.stderr | ||
| assert sorted(path.name for path in project.iterdir()) == sorted(files) | ||
| assert all((project / name).read_text() == template() for name in files) | ||
| assert list(home.iterdir()) == [] | ||
|
|
||
|
|
||
| def test_reinstall_is_idempotent_and_preserves_existing_instructions(workspace): | ||
| home, _ = workspace | ||
| target = home / ".codex" / "AGENTS.md" | ||
| target.parent.mkdir() | ||
| original = "# Project rules\n\n<!-- BEGIN BEADS CODEX SETUP -->\nKeep this.\n" | ||
| target.write_text(original) | ||
| assert install(workspace, "--agent", "codex").returncode == 0 | ||
| expected = original + "\n" + template() | ||
| assert target.read_text() == expected | ||
| assert install(workspace, "--agent", "codex").returncode == 0 | ||
| assert target.read_text() == expected | ||
|
|
||
|
|
||
| def test_update_replaces_only_the_marked_block(workspace): | ||
| _, project = workspace | ||
| target = project / "AGENTS.md" | ||
| target.write_text(f"Before\n{BEGIN}\nOld rules\n{END}\nAfter\n") | ||
| result = install(workspace, "--agent", "codex", "--scope", "project") | ||
| assert result.returncode == 0, result.stderr | ||
| assert target.read_text() == "Before\n" + template() + "After\n" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("args", [ | ||
| ["--agent", "other"], ["--scope", "other"], ["--unknown"], | ||
| ["--agent"], ["--scope"], ["--agent", "claude,"], | ||
| ]) | ||
| def test_invalid_arguments_leave_all_destinations_untouched(workspace, args): | ||
| home, project = workspace | ||
| result = install(workspace, *args) | ||
| assert result.returncode != 0 | ||
| assert list(home.iterdir()) == [] | ||
| assert list(project.iterdir()) == [] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("content", [ | ||
| f"{BEGIN}\nUnclosed\n", f"{END}\n", | ||
| f"{END}\n{BEGIN}\n", f"{BEGIN}\n{BEGIN}\n{END}\n", | ||
| f"{BEGIN}\n{END}\n{BEGIN}\n{END}\n", | ||
| ]) | ||
| def test_malformed_markers_prevent_changes_to_either_target(workspace, content): | ||
| _, project = workspace | ||
| claude = project / "CLAUDE.md" | ||
| codex = project / "AGENTS.md" | ||
| claude.write_text("Keep Claude unchanged\n") | ||
| codex.write_text(content) | ||
| result = install(workspace, "--scope", "project") | ||
| assert result.returncode != 0 | ||
| assert claude.read_text() == "Keep Claude unchanged\n" | ||
| assert codex.read_text() == content | ||
|
|
||
|
|
||
| def test_help_does_not_install_anything(workspace): | ||
| home, project = workspace | ||
| result = install(workspace, "--help") | ||
| assert result.returncode == 0 | ||
| assert "--agent" in result.stdout | ||
| assert list(home.iterdir()) == [] | ||
| assert list(project.iterdir()) == [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recognize CRLF marker lines before appending.
If an editor converts an installed file to CRLF, neither marker matches because each record retains
\r. Validation accepts the file as marker-free. The replacement pass then appends a second block and leaves the old instructions active.Ignore a terminal
\rwhen comparing markers in validation and replacement. Preserve surrounding content. Check that reinstalling both LF and CRLF files leaves exactly one managed block.