Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·177 lines (157 loc) · 7.06 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·177 lines (157 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
# setup.sh — set up the workbench on this host.
#
# ./setup.sh <profile> build the image and set up a run profile from profiles/<profile>/
# ./setup.sh repo <name> make, or bring up to date, the workers' clean clone ~/workbench/<name>
# ./setup.sh links link the working agreement as ~/AGENTS.md and ~/.claude/CLAUDE.md
# ./setup.sh tools link bin/ into ~/.local/bin, and skills/ into ~/.agents/skills and ~/.claude/skills
# ./setup.sh github-sync <name>
# keep GitHub's copy of <name> up to date every two minutes with cloud/github-sync,
# which deletes nothing there (on one host only: the iMac)
#
# A profile is described by profiles/<name>/: intro.md opens its SOUL.md and
# profile.json holds its description, skills and config. With "agreement": true
# the SOUL.md is the intro followed by working-agreement.md. Running setup again
# brings a profile back to exactly what this repository says.
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
forge=${WORKBENCH_FORGE:-git@git.imhermes.dev:hermes}
usage() {
echo "usage: $0 <profile> | repo <name> | links | tools | github-sync <name>" >&2
echo "profiles: $(ls "$here/profiles" | tr '\n' ' ')" >&2
exit 2
}
# One value out of a profile.json: field <file> <python expression over d>
field() {
python3 -c 'import json, sys; d = json.load(open(sys.argv[1])); print(eval(sys.argv[2]))' "$1" "$2"
}
link_rules() {
mkdir -p "$HOME/.claude"
ln -sfn "$here/working-agreement.md" "$HOME/AGENTS.md"
ln -sfn "$here/working-agreement.md" "$HOME/.claude/CLAUDE.md"
echo "linked ~/AGENTS.md and ~/.claude/CLAUDE.md to $here/working-agreement.md"
}
# The commands and skills for work in any repository, for Hermes and for the
# agents on this host. They are links, so pulling this repository updates them.
# A link left behind by a tool or skill that was deleted here is removed. A
# file that is not a link belongs to someone else and stops the run.
link_into() {
local dest="$1/$(basename "$2")"
if [ -e "$dest" ] && [ ! -L "$dest" ]; then
echo "setup: $dest exists and is not a link; move it away and run again" >&2
exit 1
fi
ln -sfn "$2" "$dest"
}
link_tools() {
local bin="$HOME/.local/bin" dir src
for dir in "$bin" "$HOME/.agents/skills" "$HOME/.claude/skills"; do
mkdir -p "$dir"
find "$dir" -maxdepth 1 -type l -lname "$here/*" ! -exec test -e {} \; -delete
done
for src in "$here"/bin/*; do
if [ -f "$src" ]; then link_into "$bin" "$src"; fi
done
for src in "$here"/skills/*/; do
link_into "$HOME/.agents/skills" "${src%/}"
link_into "$HOME/.claude/skills" "${src%/}"
done
echo "linked $(ls "$here/skills" | tr '\n' ' ')into ~/.agents/skills and ~/.claude/skills"
echo "linked $(find "$here/bin" -maxdepth 1 -type f -printf '%f\n' | sort | tr '\n' ' ')into ~/.local/bin"
}
# Workers never see Hermes's own working copies. They get a clean clone from
# the Git server, and kanban puts each task's worktree under its .worktrees/.
workbench_repo() {
local name=$1 dir="$HOME/workbench/$1"
if [ ! -d "$dir/.git" ]; then
mkdir -p "$HOME/workbench"
git clone -q "$forge/$name.git" "$dir"
echo ".worktrees/" >> "$dir/.git/info/exclude"
fi
git -C "$dir" pull -q --ff-only
echo "$dir is at $(git -C "$dir" log -1 --format='%h %s')"
}
# GitHub's copy of a repository, kept up to date from the Git server by a user
# timer. The units call cloud/github-sync in this checkout, so a pull updates it.
github_sync() {
local name=$1 units="$HOME/.config/systemd/user"
mkdir -p "$units"
cat > "$units/github-sync@.service" <<EOF
[Unit]
Description=Bring GitHub's copy of %i up to date with the Git server
[Service]
Type=oneshot
ExecStart=$here/cloud/github-sync %i
EOF
cat > "$units/github-sync@.timer" <<EOF
[Unit]
Description=Bring GitHub's copy of %i up to date every two minutes
[Timer]
OnBootSec=1min
OnUnitActiveSec=2min
[Install]
WantedBy=timers.target
EOF
systemctl --user daemon-reload
systemctl --user enable --now "github-sync@$name.timer"
echo "github-sync@$name.timer is on. Turn off the Git server's push mirror of $name: it deletes the branches only GitHub has."
}
build_image() {
podman build -q -t localhost/workbench:latest -f "$here/workbench/Containerfile" "$here/workbench" > /dev/null
echo "built localhost/workbench:latest ($(podman images localhost/workbench:latest --format '{{.Size}}'))"
}
setup_profile() {
local name=$1 spec="$here/profiles/$1/profile.json" intro="$here/profiles/$1/intro.md"
[ -f "$spec" ] || usage
command -v hermes > /dev/null || { echo "setup: hermes is not installed on this host" >&2; exit 1; }
mkdir -p "$HOME/.cache/workbench" "$HOME/workbench"
build_image
local home="$HOME/.hermes/profiles/$name"
if [ ! -d "$home" ]; then
hermes profile create "$name" --no-alias --no-skills --description "$(field "$spec" 'd["description"]')" > /dev/null
echo "created profile $name"
fi
# hermes config set keeps string settings verbatim; anything else goes as JSON.
python3 - "$spec" <<'PY' | while IFS=$'\t' read -r key value; do
import json, sys
for key, value in json.load(open(sys.argv[1]))["config"].items():
print(f"{key}\t{value if isinstance(value, str) else json.dumps(value)}")
PY
hermes -p "$name" config set "$key" "$value" > /dev/null
done
echo "applied $(field "$spec" 'len(d["config"])') settings"
# A setting the last setup applied and profile.json no longer names is taken out again.
local applied="$home/.setup-settings" key
field "$spec" '"\n".join(sorted(d["config"]))' > "$applied.now"
if [ -f "$applied" ]; then
for key in $(LC_ALL=C comm -23 "$applied" "$applied.now"); do
hermes -p "$name" config unset "$key" > /dev/null
echo "removed setting $key"
done
fi
mv "$applied.now" "$applied"
# Hermes takes the docker CLI before podman when both are installed. The workbench runs on podman.
hermes -p "$name" config set HERMES_DOCKER_BINARY "$(command -v podman)" > /dev/null
if [ "$(field "$spec" 'd.get("agreement", False)')" = True ]; then
{ cat "$intro"; echo; cat "$here/working-agreement.md"; } > "$home/SOUL.md"
else
cp "$intro" "$home/SOUL.md"
fi
echo "wrote $home/SOUL.md"
local bundled="$HOME/.hermes/hermes-agent/skills" skill src
rm -rf "$home/skills" && mkdir -p "$home/skills"
for skill in $(field "$spec" '" ".join(d.get("skills", []))'); do
src=$(find "$bundled" -mindepth 2 -maxdepth 2 -type d -name "$skill" | head -n 1)
[ -n "$src" ] || { echo "setup: no bundled skill named $skill" >&2; exit 1; }
cp -r "$src" "$home/skills/$skill"
done
echo "skills: $(ls "$home/skills" | tr '\n' ' ')"
}
case "${1:-}" in
"" | -h | --help) usage ;;
links) link_rules ;;
tools) link_tools ;;
repo) [ -n "${2:-}" ] || usage; workbench_repo "$2" ;;
github-sync) [ -n "${2:-}" ] || usage; github_sync "$2" ;;
*) setup_profile "$1" ;;
esac