Skip to content
Merged
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
87 changes: 87 additions & 0 deletions functions/agentsify.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
function _agentsify_usage
echo "Unify AI agent context files into AGENTS.md.
Renames CLAUDE.md/GEMINI.md to AGENTS.md and links CLAUDE.md -> AGENTS.md.
Syntax: agentsify [dir]"
end

function agentsify --description 'Unify AI agent context files into AGENTS.md with a CLAUDE.md symlink'
argparse h/help -- $argv
or return 1
if set -q _flag_help
_agentsify_usage
return 0
end
Comment thread
dclong marked this conversation as resolved.

if test (count $argv) -gt 1
echo (set_color $fish_color_error)"Error: agentsify accepts at most 1 argument."(set_color normal) >&2
_agentsify_usage >&2
return 1
end

set -l dir "."
if test (count $argv) -eq 1
set dir "$argv[1]"
end
if not test -d "$dir"
echo (set_color $fish_color_error)"Error: $dir is not a directory!"(set_color normal) >&2
return 1
end

set -l agents "$dir/AGENTS.md"
if test -L "$agents"
echo (set_color $fish_color_error)"Error: $agents is a symlink; resolve it into a regular file first."(set_color normal) >&2
return 1
end

# Ensure AGENTS.md exists, renaming the first real source file into it.
if not test -e "$agents"
set -l source ""
for name in CLAUDE.md GEMINI.md
set -l file "$dir/$name"
if test -f "$file"; and not test -L "$file"
set source "$file"
break
end
end
if test -z "$source"
echo (set_color $fish_color_error)"Error: no AGENTS.md, CLAUDE.md or GEMINI.md found in $dir!"(set_color normal) >&2
return 1
end
mv -- "$source" "$agents"
or return 1
echo "Renamed "(path basename -- "$source")" -> AGENTS.md"
Comment thread
dclong marked this conversation as resolved.
end

# Fold any remaining real CLAUDE.md/GEMINI.md into AGENTS.md.
set -l conflict 0
for name in CLAUDE.md GEMINI.md
set -l file "$dir/$name"
if test -L "$file"; or not test -e "$file"
continue
end
if cmp -s -- "$file" "$agents"
rm -- "$file"
else
echo (set_color $fish_color_error)"Error: $file differs from AGENTS.md; merge it manually."(set_color normal) >&2
set conflict 1
end
end

# Link CLAUDE.md -> AGENTS.md, since the claude cli only reads CLAUDE.md.
set -l claude "$dir/CLAUDE.md"
if test -L "$claude"
set -l target (readlink "$claude")
if test "$target" != AGENTS.md
echo "Replacing CLAUDE.md symlink that pointed to $target"
end
rm -- "$claude"
end
if test -e "$claude"
echo (set_color $fish_color_error)"Error: a real CLAUDE.md remains; cannot create the symlink."(set_color normal) >&2
return 1
end
ln -s AGENTS.md "$claude"
echo "Linked CLAUDE.md -> AGENTS.md"
Comment thread
dclong marked this conversation as resolved.
Comment thread
dclong marked this conversation as resolved.
Comment on lines +70 to +84

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation unconditionally removes and recreates the CLAUDE.md symlink even if it already correctly points to AGENTS.md. This causes redundant disk writes and prints unnecessary output on subsequent runs. Additionally, if rm or ln -s fails, the function continues and prints a misleading success message.

We can make this function idempotent and robust by checking if the symlink is already correct, and by handling command failures using Fish's or return 1 pattern.

    # Link CLAUDE.md -> AGENTS.md, since the claude cli only reads CLAUDE.md.
    set -l claude "$dir/CLAUDE.md"
    set -l needs_link 1
    if test -L "$claude"
        set -l target (readlink "$claude")
        if test "$target" = AGENTS.md
            set needs_link 0
        else
            echo "Replacing CLAUDE.md symlink that pointed to $target"
            rm -- "$claude"
            or return 1
        end
    end
    if test "$needs_link" = 1
        if test -e "$claude"
            echo (set_color $fish_color_error)"Error: a real CLAUDE.md remains; cannot create the symlink."(set_color normal) >&2
            return 1
        end
        ln -s AGENTS.md "$claude"
        or return 1
        echo "Linked CLAUDE.md -> AGENTS.md"
    end


return $conflict
end