Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 4 additions & 35 deletions bin/fm-ensure-agents-md.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
# when neither file exists, promotes a real CLAUDE.md file when it is the only
# file present (unless it is already the canonical pointer), converts a correct
# CLAUDE.md -> AGENTS.md symlink into the pointer file, and refuses to clobber
# distinct real files or wrong symlinks. Skips writing a brand-new CLAUDE.md
# pointer (neither file existed, or AGENTS.md existed with no CLAUDE.md) when
# the invoking Claude Code is >= 2.1.277, which reads AGENTS.md natively; see
# claude_supports_native_agents_md. Existing pointers and symlink conversions
# are left alone by this gate for now.
# distinct real files or wrong symlinks. Never writes a brand-new CLAUDE.md
# pointer for a fresh project (neither file existed, or AGENTS.md existed with
# no CLAUDE.md); Claude Code reads AGENTS.md natively fleet-wide. Existing
# pointers and symlink conversions are left alone.
# Owns the canonical "## Maintaining this file" self-governance wording for
# project AGENTS.md files, injecting it idempotently into created skeletons,
# promoted CLAUDE.md files, and existing AGENTS.md files lacking both the exact
Expand Down Expand Up @@ -132,36 +131,6 @@ is_canonical_claude_pointer() {
claude_pointer_content | cmp -s - "$CLAUDE"
}

fm_version_at_least() { # <candidate> <floor>
local candidate=${1:-} floor=${2:-} c f
candidate=${candidate%%[-+]*}
case "$candidate" in ''|*[!0-9.]*) return 1 ;; esac
while [ -n "$floor" ]; do
c=${candidate%%.*}
f=${floor%%.*}
[ -n "$c" ] || c=0
[ "$c" -gt "$f" ] 2>/dev/null && return 0
[ "$c" -lt "$f" ] 2>/dev/null && return 1
case "$candidate" in *.*) candidate=${candidate#*.} ;; *) candidate= ;; esac
case "$floor" in *.*) floor=${floor#*.} ;; *) floor= ;; esac
done
return 0
}

# Returns 0 if local Claude Code is installed and version is >= 2.1.277.
# Returns 1 if missing, unparseable, command errors, or version < 2.1.277.
claude_supports_native_agents_md() {
command -v claude >/dev/null 2>&1 || return 1
local ver_str ver
ver_str=$(claude --version 2>/dev/null) || return 1
if [[ "$ver_str" =~ ([0-9]+\.[0-9]+\.[0-9]+) ]]; then
ver="${BASH_REMATCH[1]}"
else
return 1
fi
fm_version_at_least "$ver" "2.1.277"
}

# Write the canonical pointer as a regular file. Unlink a symlink first so the
# write cannot follow it and destroy AGENTS.md. Never overwrite a distinct real
# file; callers classify that as a conflict before invoking this.
Expand Down
39 changes: 5 additions & 34 deletions tests/fm-ensure-agents-md.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,11 @@ write_fixture_claude_pointer() {
EOF
}

with_mock_claude() {
local output=$1 code=${2:-0} mock_dir
shift 2
mock_dir=$(mktemp -d "$TMP_ROOT/mock-claude.XXXXXX")
cat > "$mock_dir/claude" <<EOF
#!/bin/sh
if [ "$code" -ne 0 ]; then
exit $code
fi
cat <<'OUT'
$output
OUT
EOF
chmod +x "$mock_dir/claude"
PATH="$mock_dir:$PATH" "$@"
}

with_no_claude() {
local claude_path claude_dir new_path
claude_path=$(command -v claude 2>/dev/null)
if [ -n "$claude_path" ]; then
claude_dir=$(dirname "$claude_path")
new_path=$(echo "$PATH" | tr ':' '\n' | grep -v -Fx "$claude_dir" | tr '\n' ':')
PATH="$new_path" "$@"
else
"$@"
fi
}

test_created_agents_md_includes_self_governance() {
local repo agents
repo="$TMP_ROOT/new-project"
mkdir -p "$repo"
with_mock_claude "2.0.0" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 || fail "fm-ensure-agents-md.sh failed for empty project"
"$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 || fail "fm-ensure-agents-md.sh failed for empty project"
agents="$repo/AGENTS.md"
assert_present "$agents" "AGENTS.md was not created"
assert_absent "$repo/CLAUDE.md" "fresh setup created a CLAUDE.md file"
Expand Down Expand Up @@ -188,7 +159,7 @@ test_existing_agents_md_without_claude_gains_section() {
mkdir -p "$repo"
printf '# Existing agent memory\n\nDeploy with kubectl.\n' > "$repo/AGENTS.md"
agents="$repo/AGENTS.md"
out=$(with_mock_claude "2.0.0" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
out=$("$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
|| fail "fm-ensure-agents-md.sh failed for existing AGENTS.md without CLAUDE.md"
assert_contains "$out" "updated:" "injection without CLAUDE.md did not report an update"
assert_absent "$repo/CLAUDE.md" "injection created a CLAUDE.md file"
Expand Down Expand Up @@ -233,7 +204,7 @@ test_marked_project_guidance_stays_unchanged() {
symlink) ln -s AGENTS.md "$repo/CLAUDE.md" ;;
promotion) mv "$repo/AGENTS.md" "$repo/CLAUDE.md" ;;
esac
with_mock_claude "2.0.0" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 \
"$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 \
|| fail "ensure failed for marked project ($route)"
cmp -s "$repo/.before" "$repo/AGENTS.md" \
|| fail "marked project guidance was modified ($route)"
Expand Down Expand Up @@ -268,15 +239,15 @@ test_reworded_guidance_requires_first_line_marker() {
'Keep broadly useful knowledge concise; link to sources and rewrite stale entries.' \
'Preserve these rules for every agent.' |
while IFS= read -r line; do printf '%s%s' "$line" "$eol"; done > "$repo/AGENTS.md"
with_mock_claude "2.0.0" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 \
"$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 \
|| fail "ensure failed for unmarked reworded guidance"
# AGENTS.md is the helper's generated output contract, not implementation source.
assert_grep '## Editing these notes' "$repo/AGENTS.md" "ensure removed project guidance"
count=$(grep -Fxc "## Maintaining this file${eol%$'\n'}" "$repo/AGENTS.md")
[ "$count" -eq 1 ] || fail "guidance without a first-line mark did not gain the canonical section"
assert_absent "$repo/CLAUDE.md" "ensure created a CLAUDE.md file"
cp "$repo/AGENTS.md" "$repo/.after-first"
with_mock_claude "2.0.0" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 \
"$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 \
|| fail "ensure failed on unmarked project re-run"
cmp -s "$repo/.after-first" "$repo/AGENTS.md" \
|| fail "unmarked project re-run modified guidance"
Expand Down
Loading