feat(agents-vault): derive vault slugs from normalized remote URLs
Keys a project by its remote rather than its path so the key survives a machine change or a directory rename. Falls back to a path-derived local-* key when no remote exists. Adds a hermetic test harness that builds throwaway repos under mktemp.
This commit is contained in:
@@ -0,0 +1,69 @@
|
|||||||
|
# Copyright (C) 2026 Rootiest
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
# SYNOPSIS
|
||||||
|
# _agents_repo_slug <dir>
|
||||||
|
#
|
||||||
|
# DESCRIPTION
|
||||||
|
# Derives the vault slug for a project directory. Prefers the normalized
|
||||||
|
# git remote URL so the same project keys identically from any clone on
|
||||||
|
# any machine; falls back to a path-derived key when no remote exists.
|
||||||
|
#
|
||||||
|
# Normalization strips the scheme, userinfo, and a numeric port, rewrites
|
||||||
|
# scp-form host:path to host/path, drops a trailing .git, lowercases, and
|
||||||
|
# maps every character outside [a-z0-9._-] to a dash. These all yield
|
||||||
|
# git.rootiest.dev-rootiest-fish-config:
|
||||||
|
#
|
||||||
|
# https://git.rootiest.dev/rootiest/fish-config.git
|
||||||
|
# git@git.rootiest.dev:rootiest/fish-config.git
|
||||||
|
# ssh://git@git.rootiest.dev:22/rootiest/fish-config.git
|
||||||
|
#
|
||||||
|
# With no remote the slug is local-<basename>-<8 hex of sha256(realpath)>.
|
||||||
|
# That key is machine-dependent by construction and is best-effort only;
|
||||||
|
# agents-vault --adopt rebinds such an entry by hand.
|
||||||
|
#
|
||||||
|
# ARGUMENTS
|
||||||
|
# dir Absolute path to the project directory
|
||||||
|
#
|
||||||
|
# EXIT STATUS
|
||||||
|
# 0 Slug printed
|
||||||
|
# 1 No directory argument given
|
||||||
|
#
|
||||||
|
# RETURNS
|
||||||
|
# The slug, one line on stdout.
|
||||||
|
#
|
||||||
|
# EXAMPLE
|
||||||
|
# set -l slug (_agents_repo_slug /home/user/myproject)
|
||||||
|
function _agents_repo_slug --argument-names dir
|
||||||
|
test -n "$dir"; or return 1
|
||||||
|
|
||||||
|
set -l url (git -C "$dir" remote get-url origin 2>/dev/null)
|
||||||
|
if test -z "$url"
|
||||||
|
set -l remotes (git -C "$dir" remote 2>/dev/null)
|
||||||
|
if test (count $remotes) -gt 0
|
||||||
|
set url (git -C "$dir" remote get-url $remotes[1] 2>/dev/null)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if test -n "$url"
|
||||||
|
set -l s $url
|
||||||
|
# Order matters: the port must go before the scp-form rewrite, or
|
||||||
|
# ssh://host:22/a/b becomes host/22/a/b and diverges from the
|
||||||
|
# https slug for the same repository.
|
||||||
|
set s (string replace -r '^[A-Za-z][A-Za-z0-9+.-]*://' '' -- $s)
|
||||||
|
set s (string replace -r '^[^@/]+@' '' -- $s)
|
||||||
|
set s (string replace -r '^([^/:]+):[0-9]+/' '$1/' -- $s)
|
||||||
|
set s (string replace -r '^([^/:]+):' '$1/' -- $s)
|
||||||
|
set s (string replace -r '\.git$' '' -- $s)
|
||||||
|
set s (string replace -r '/+$' '' -- $s)
|
||||||
|
set s (string lower -- $s)
|
||||||
|
set s (string replace -ra '[^a-z0-9._-]' '-' -- $s)
|
||||||
|
printf '%s\n' $s
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
set -l rp (path resolve "$dir")
|
||||||
|
set -l base (string lower -- (path basename "$rp"))
|
||||||
|
set -l digest (printf '%s' "$rp" | sha256sum | string split -f1 ' ')
|
||||||
|
printf 'local-%s-%s\n' "$base" (string sub -l 8 -- "$digest")
|
||||||
|
end
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
#!/usr/bin/env fish
|
||||||
|
# Copyright (C) 2026 Rootiest
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
#
|
||||||
|
# Hermetic tests for the agent memory vault helpers. Every test builds its
|
||||||
|
# own throwaway git repos and directories under mktemp; nothing touches the
|
||||||
|
# live vault, ~/.claude, or this checkout.
|
||||||
|
#
|
||||||
|
# Usage: fish tests/test-agents-vault.fish
|
||||||
|
|
||||||
|
set -l here (realpath (dirname (status filename)))
|
||||||
|
set -g repo_root (realpath $here/..)
|
||||||
|
set -p fish_function_path $repo_root/functions
|
||||||
|
|
||||||
|
set -g TESTS_RUN 0
|
||||||
|
set -g TESTS_FAILED 0
|
||||||
|
set -g TMPDIRS
|
||||||
|
|
||||||
|
function check --argument-names label want got
|
||||||
|
set -g TESTS_RUN (math $TESTS_RUN + 1)
|
||||||
|
if test "$want" = "$got"
|
||||||
|
echo " PASS $label"
|
||||||
|
else
|
||||||
|
echo " FAIL $label"
|
||||||
|
echo " want: $want"
|
||||||
|
echo " got: $got"
|
||||||
|
set -g TESTS_FAILED (math $TESTS_FAILED + 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Create a throwaway git repo, optionally with an origin remote.
|
||||||
|
function new_repo --argument-names url
|
||||||
|
set -l d (mktemp -d)
|
||||||
|
set -ga TMPDIRS $d
|
||||||
|
git -C $d init -q
|
||||||
|
git -C $d config user.email t@t
|
||||||
|
git -C $d config user.name t
|
||||||
|
git -C $d config commit.gpgsign false
|
||||||
|
git -C $d config core.hooksPath /dev/null
|
||||||
|
test -n "$url"; and git -C $d remote add origin $url
|
||||||
|
printf '%s\n' $d
|
||||||
|
end
|
||||||
|
|
||||||
|
function cleanup
|
||||||
|
for d in $TMPDIRS
|
||||||
|
test -n "$d"; and rm -rf $d
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# ─────────────────────────── slug derivation ───────────────────────────
|
||||||
|
echo "== _agents_repo_slug =="
|
||||||
|
|
||||||
|
set -l want git.rootiest.dev-rootiest-fish-config
|
||||||
|
|
||||||
|
set -l r (new_repo https://git.rootiest.dev/rootiest/fish-config.git)
|
||||||
|
check "https form" $want (_agents_repo_slug $r)
|
||||||
|
|
||||||
|
set r (new_repo git@git.rootiest.dev:rootiest/fish-config.git)
|
||||||
|
check "scp form" $want (_agents_repo_slug $r)
|
||||||
|
|
||||||
|
set r (new_repo ssh://git@git.rootiest.dev:22/rootiest/fish-config.git)
|
||||||
|
check "ssh form with port" $want (_agents_repo_slug $r)
|
||||||
|
|
||||||
|
set r (new_repo https://git.rootiest.dev/rootiest/fish-config)
|
||||||
|
check "no .git suffix" $want (_agents_repo_slug $r)
|
||||||
|
|
||||||
|
set r (new_repo HTTPS://Git.Rootiest.DEV/Rootiest/Fish-Config.git)
|
||||||
|
check "case folded" $want (_agents_repo_slug $r)
|
||||||
|
|
||||||
|
# Remote-less: local- prefix, stable across runs, distinct per path.
|
||||||
|
set -l n1 (new_repo)
|
||||||
|
set -l s1 (_agents_repo_slug $n1)
|
||||||
|
set -l s2 (_agents_repo_slug $n1)
|
||||||
|
check "local slug is stable" $s1 $s2
|
||||||
|
check "local slug is prefixed" true (string match -q 'local-*' -- $s1; and echo true; or echo false)
|
||||||
|
|
||||||
|
set -l n2 (new_repo)
|
||||||
|
check "local slugs differ by path" false (test "$s1" = (_agents_repo_slug $n2); and echo true; or echo false)
|
||||||
|
|
||||||
|
# A non-origin remote is used when origin is absent.
|
||||||
|
set -l r3 (new_repo)
|
||||||
|
git -C $r3 remote add upstream https://git.rootiest.dev/rootiest/fish-config.git
|
||||||
|
check "falls back to first remote" $want (_agents_repo_slug $r3)
|
||||||
|
|
||||||
|
cleanup
|
||||||
|
echo ""
|
||||||
|
echo (math $TESTS_RUN - $TESTS_FAILED)"/$TESTS_RUN passed"
|
||||||
|
exit $TESTS_FAILED
|
||||||
Reference in New Issue
Block a user