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
60 changes: 53 additions & 7 deletions bin/fm-ensure-agents-md.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
# 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.
# 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.
# 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 @@ -128,6 +132,36 @@ 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 Expand Up @@ -209,11 +243,19 @@ if [ -e "$AGENTS" ]; then
fi
if [ ! -e "$CLAUDE" ]; then
ensure_maintenance_section
install_claude_pointer
if [ "$MAINT_INJECTED" -eq 1 ]; then
echo "updated: added ## Maintaining this file to AGENTS.md and wrote CLAUDE.md @AGENTS.md pointer in $DIR"
if claude_supports_native_agents_md; then
if [ "$MAINT_INJECTED" -eq 1 ]; then
echo "updated: added ## Maintaining this file to AGENTS.md in $DIR"
else
echo "unchanged: AGENTS.md in $DIR"
fi
else
echo "wrote: CLAUDE.md @AGENTS.md pointer in $DIR"
install_claude_pointer
if [ "$MAINT_INJECTED" -eq 1 ]; then
echo "updated: added ## Maintaining this file to AGENTS.md and wrote CLAUDE.md @AGENTS.md pointer in $DIR"
else
echo "wrote: CLAUDE.md @AGENTS.md pointer in $DIR"
fi
fi
exit 0
fi
Expand Down Expand Up @@ -263,5 +305,9 @@ if [ -e "$CLAUDE" ]; then
fi

write_skeleton
install_claude_pointer
echo "created: AGENTS.md and CLAUDE.md @AGENTS.md pointer in $DIR"
if claude_supports_native_agents_md; then
echo "created: AGENTS.md in $DIR"
else
install_claude_pointer
echo "created: AGENTS.md and CLAUDE.md @AGENTS.md pointer in $DIR"
fi
147 changes: 138 additions & 9 deletions tests/fm-ensure-agents-md.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,40 @@ 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"
"$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 || fail "fm-ensure-agents-md.sh failed for empty project"
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"
agents="$repo/AGENTS.md"
assert_present "$agents" "AGENTS.md was not created"
assert_claude_pointer "$repo/CLAUDE.md"
Expand All @@ -50,7 +79,7 @@ test_fresh_setup_writes_real_claude_pointer() {
local repo out
repo="$TMP_ROOT/fresh-pointer-project"
mkdir -p "$repo"
out=$("$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
out=$(with_mock_claude "2.0.0" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
|| fail "fm-ensure-agents-md.sh failed creating a fresh pointer"
assert_contains "$out" "created:" "fresh setup did not report created"
assert_claude_pointer "$repo/CLAUDE.md"
Expand Down Expand Up @@ -159,7 +188,7 @@ test_existing_agents_md_without_claude_gains_section_and_pointer() {
mkdir -p "$repo"
printf '# Existing agent memory\n\nDeploy with kubectl.\n' > "$repo/AGENTS.md"
agents="$repo/AGENTS.md"
out=$("$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
out=$(with_mock_claude "2.0.0" 0 "$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_claude_pointer "$repo/CLAUDE.md"
Expand All @@ -174,13 +203,13 @@ test_existing_agents_md_with_section_reports_unchanged() {
repo="$TMP_ROOT/fully-formed-project"
mkdir -p "$repo"
# Build a fully-formed project (AGENTS.md with the section + canonical pointer).
"$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 \
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 building the fully-formed fixture"
agents="$repo/AGENTS.md"
assert_claude_pointer "$repo/CLAUDE.md"
cp "$agents" "$repo/.before"
cp "$repo/CLAUDE.md" "$repo/.claude-before"
out=$("$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
out=$(with_mock_claude "2.0.0" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
|| fail "fm-ensure-agents-md.sh failed on already-formed project"
assert_contains "$out" "unchanged:" "already-formed project was not reported unchanged"
diff "$repo/.before" "$agents" >/dev/null \
Expand All @@ -206,12 +235,12 @@ test_marked_project_guidance_stays_unchanged() {
symlink) ln -s AGENTS.md "$repo/CLAUDE.md" ;;
promotion) mv "$repo/AGENTS.md" "$repo/CLAUDE.md" ;;
esac
"$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 \
with_mock_claude "2.0.0" 0 "$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)"
assert_claude_pointer "$repo/CLAUDE.md"
out=$("$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
out=$(with_mock_claude "2.0.0" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
|| fail "ensure failed on marked project re-run ($route)"
assert_contains "$out" "unchanged:" "marked project re-run did not report unchanged"
cmp -s "$repo/.before" "$repo/AGENTS.md" \
Expand All @@ -235,15 +264,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"
"$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 \
with_mock_claude "2.0.0" 0 "$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_claude_pointer "$repo/CLAUDE.md"
cp "$repo/AGENTS.md" "$repo/.after-first"
"$ROOT/bin/fm-ensure-agents-md.sh" "$repo" >/dev/null 2>&1 \
with_mock_claude "2.0.0" 0 "$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 Expand Up @@ -415,8 +444,107 @@ test_lowercase_agents_md_refuses_case_fragile_pointer() {
pass "fm-ensure-agents-md.sh: refuses a case-variant lowercase agents.md (issue #389)"
}

test_fresh_setup_version_below_cutoff_writes_pointer() {
local repo out
repo="$TMP_ROOT/fresh-below-cutoff"
mkdir -p "$repo"
out=$(with_mock_claude "2.0.0" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
|| fail "fm-ensure-agents-md.sh failed for version below cutoff"
assert_contains "$out" "created: AGENTS.md and CLAUDE.md @AGENTS.md pointer" "did not report created pointer"
assert_claude_pointer "$repo/CLAUDE.md"
pass "fm-ensure-agents-md.sh: version below cutoff writes pointer"
}

test_fresh_setup_version_at_cutoff_skips_pointer() {
local repo out
repo="$TMP_ROOT/fresh-at-cutoff"
mkdir -p "$repo"
out=$(with_mock_claude "2.1.277" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
|| fail "fm-ensure-agents-md.sh failed for version at cutoff"
assert_contains "$out" "created: AGENTS.md in" "did not report created AGENTS.md without pointer"
assert_absent "$repo/CLAUDE.md" "CLAUDE.md pointer was created at version cutoff"
pass "fm-ensure-agents-md.sh: version at cutoff skips pointer"
}

test_fresh_setup_version_above_cutoff_skips_pointer() {
local repo out
repo="$TMP_ROOT/fresh-above-cutoff"
mkdir -p "$repo"
out=$(with_mock_claude "2.2.0 (Claude Code)" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
|| fail "fm-ensure-agents-md.sh failed for version above cutoff"
assert_contains "$out" "created: AGENTS.md in" "did not report created AGENTS.md without pointer"
assert_absent "$repo/CLAUDE.md" "CLAUDE.md pointer was created above version cutoff"
pass "fm-ensure-agents-md.sh: version above cutoff skips pointer"
}

test_fresh_setup_claude_missing_writes_pointer_fallback() {
local repo out
repo="$TMP_ROOT/fresh-no-claude"
mkdir -p "$repo"
out=$(with_no_claude "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
|| fail "fm-ensure-agents-md.sh failed when claude is missing"
assert_contains "$out" "created: AGENTS.md and CLAUDE.md @AGENTS.md pointer" "did not write pointer as fallback"
assert_claude_pointer "$repo/CLAUDE.md"
pass "fm-ensure-agents-md.sh: missing claude falls back to writing pointer"
}

test_fresh_setup_version_unparseable_writes_pointer_fallback() {
local repo out
repo="$TMP_ROOT/fresh-unparseable"
mkdir -p "$repo"
out=$(with_mock_claude "invalid-version-string" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
|| fail "fm-ensure-agents-md.sh failed when version is unparseable"
assert_contains "$out" "created: AGENTS.md and CLAUDE.md @AGENTS.md pointer" "did not write pointer on unparseable version"
assert_claude_pointer "$repo/CLAUDE.md"
pass "fm-ensure-agents-md.sh: unparseable version falls back to writing pointer"
}

test_fresh_setup_claude_error_writes_pointer_fallback() {
local repo out
repo="$TMP_ROOT/fresh-claude-error"
mkdir -p "$repo"
out=$(with_mock_claude "" 1 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
|| fail "fm-ensure-agents-md.sh failed when claude errors"
assert_contains "$out" "created: AGENTS.md and CLAUDE.md @AGENTS.md pointer" "did not write pointer when claude errors"
assert_claude_pointer "$repo/CLAUDE.md"
pass "fm-ensure-agents-md.sh: claude command error falls back to writing pointer"
}

test_existing_pointer_untouched_at_or_above_cutoff() {
local repo out
repo="$TMP_ROOT/existing-pointer-at-cutoff"
mkdir -p "$repo"
printf '# Project agent memory\n\n## Maintaining this file\n' > "$repo/AGENTS.md"
write_fixture_claude_pointer "$repo"
out=$(with_mock_claude "2.1.277" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
|| fail "fm-ensure-agents-md.sh failed for existing pointer at cutoff"
assert_contains "$out" "unchanged:" "did not report unchanged"
assert_claude_pointer "$repo/CLAUDE.md"
pass "fm-ensure-agents-md.sh: existing pointer is untouched at cutoff"
}

test_existing_agents_md_without_claude_at_or_above_cutoff() {
local repo out
repo="$TMP_ROOT/existing-bare-at-cutoff"
mkdir -p "$repo"
printf '# Existing agent memory\n\nDeploy with kubectl.\n' > "$repo/AGENTS.md"
out=$(with_mock_claude "2.1.277" 0 "$ROOT/bin/fm-ensure-agents-md.sh" "$repo" 2>&1) \
|| fail "fm-ensure-agents-md.sh failed for bare AGENTS.md at cutoff"
assert_contains "$out" "updated: added ## Maintaining this file to AGENTS.md in" "did not report updated AGENTS.md"
assert_absent "$repo/CLAUDE.md" "CLAUDE.md pointer was created for bare AGENTS.md at cutoff"
pass "fm-ensure-agents-md.sh: bare AGENTS.md at cutoff gains section and skips pointer"
}

test_created_agents_md_includes_self_governance
test_fresh_setup_writes_real_claude_pointer
test_fresh_setup_version_below_cutoff_writes_pointer
test_fresh_setup_version_at_cutoff_skips_pointer
test_fresh_setup_version_above_cutoff_skips_pointer
test_fresh_setup_claude_missing_writes_pointer_fallback
test_fresh_setup_version_unparseable_writes_pointer_fallback
test_fresh_setup_claude_error_writes_pointer_fallback
test_existing_pointer_untouched_at_or_above_cutoff
test_existing_agents_md_without_claude_at_or_above_cutoff
test_promoted_claude_md_includes_self_governance
test_promoted_claude_md_without_trailing_newline_keeps_blank_separator
test_existing_agents_md_with_symlink_gains_self_governance
Expand All @@ -433,3 +561,4 @@ test_agents_md_symlink_is_refused
test_wrong_target_symlink_is_refused
test_non_regular_claude_md_is_refused
test_lowercase_agents_md_refuses_case_fragile_pointer