Repository navigation
Expand file tree
/
Copy pathupdate-agentvmapp.sh
More file actions
executable file
·193 lines (180 loc) · 10.3 KB
/
Copy pathupdate-agentvmapp.sh
File metadata and controls
executable file
·193 lines (180 loc) · 10.3 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
# update-agentvmapp.sh - make AgentVM.app runnable and signed from this working tree.
#
# The repository holds the applet's own files only. The OMC engine (Contents/Frameworks and the
# executable in Contents/MacOS) is not committed: AppletBuilder installs it. This script:
# 1. sets AGENTVM_MIN_VERSION in lib.agentvm.sh to the version of the agent-vm working tree
# beside this repository: the app requires the agent-vm it is built and tested with;
# 1b. when the test fixtures come from another agent-vm than that version, captures them again
# from the installed agent-vm (which must be that version), with
# Tests/helpers/refresh_agentvm_fixtures.sh: the parts version, packs and recipe-check, and
# follow, which makes the hand-made fixtures name the new version;
# 2. removes tool residue from the bundle (a .claude folder left by a coding agent whose
# working directory was inside it makes codesign refuse the whole bundle);
# 3. runs `appletbuilder build`, which validates the bundle, installs or refreshes the engine,
# thins every binary to arm64, runs the test suite (unless --skip-tests), and signs;
# 4. verifies the result: a valid signature, arm64 only, and macOS 27 as the minimum.
#
# AgentVM.app carries no agent-vm. It runs the agent-vm installed in ~/.local/bin (or a
# developer override), so nothing of agent-vm is built or copied here, and the app needs no
# virtualization entitlement: agent-vm, a separate program with its own signature, is what runs
# virtual machines.
#
# Usage: ./update-agentvmapp.sh [--identity <codesign identity>] [--skip-tests] [--keep-min-version]
# --identity passed to codesign (default "-", ad hoc: runs on this Mac only)
# --skip-tests build and sign without running Tests/ (for a quick rebuild mid-change)
# --keep-min-version leave AGENTVM_MIN_VERSION as committed (no agent-vm working tree here)
#
# Environment: APPLETBUILDER, the appletbuilder command line tool, when it is not in
# ../OMC/Distribution/AppletBuilder.app or /Applications/AppletBuilder.app; AGENTVM_REPO, the
# agent-vm working tree, when it is not ../agent-vm.
#
# When a coding agent runs this script, the tests need its sandbox off (see Tests/README.md):
# the contract test runs the real agent-vm, whose doctor asks the virtualization framework.
script_dir="$(cd "$(/usr/bin/dirname "$0")" && pwd -P)"
app="$script_dir/AgentVM.app"
identity="-"
run_tests="yes"
set_min_version="yes"
fail() {
printf 'update-agentvmapp.sh: %s\n' "$*" >&2
exit 1
}
while [ "$#" -gt 0 ]; do
case "$1" in
--identity)
[ "$#" -ge 2 ] || fail "--identity needs a value"
identity="$2"
shift 2 ;;
--skip-tests)
run_tests="no"
shift ;;
--keep-min-version)
set_min_version="no"
shift ;;
-h|--help)
/usr/bin/sed -n '2,/^$/p' "$0"
exit 0 ;;
*)
fail "unknown argument: $1 (see --help)" ;;
esac
done
[ -d "$app" ] || fail "no AgentVM.app beside this script, in $script_dir"
# -- The appletbuilder tool ------------------------------------------------------------------
appletbuilder=""
for candidate in "${APPLETBUILDER:-}" \
"$script_dir/../OMC/Distribution/AppletBuilder.app/Contents/Resources/Agents/appletbuilder" \
"/Applications/AppletBuilder.app/Contents/Resources/Agents/appletbuilder"; do
if [ -n "$candidate" ] && [ -f "$candidate" ] && [ -x "$candidate" ]; then
appletbuilder="$candidate"
break
fi
done
[ -n "$appletbuilder" ] || fail "cannot find appletbuilder: set APPLETBUILDER to its path, or put AppletBuilder.app in /Applications or ../OMC/Distribution"
# -- 1. The agent-vm version the app requires -------------------------------------------------
# One development stream: the app is tested against the newest agent-vm only, so it requires
# exactly that version or newer. The number lives on one line of the library, and the tests
# compare it with the fixtures' agent-vm, so step 1b refreshes the fixtures after an update.
printf '==== agent-vm version\n'
library="$app/Contents/Resources/Scripts/lib.agentvm.sh"
current="$(/usr/bin/sed -n 's/^AGENTVM_MIN_VERSION="\(.*\)"$/\1/p' "$library")"
[ -n "$current" ] || fail "no AGENTVM_MIN_VERSION line in $library"
if [ "$set_min_version" = "yes" ]; then
agentvm_repo="${AGENTVM_REPO:-$script_dir/../agent-vm}"
# agent-vm 0.7.0 moved the file that holds its version into the guest daemon's target; a
# working tree from before that has it in the old place.
version_file="$agentvm_repo/Sources/AgentVMGuestKit/AgentVM.swift"
[ -f "$version_file" ] || version_file="$agentvm_repo/Sources/AgentVMKit/AgentVM.swift"
[ -f "$version_file" ] || fail "no agent-vm working tree at $agentvm_repo (no Sources/AgentVMGuestKit/AgentVM.swift, nor Sources/AgentVMKit/AgentVM.swift): set AGENTVM_REPO, or pass --keep-min-version"
wanted="$(/usr/bin/sed -n 's/^ *public static let version = "\([0123456789.]*\)"$/\1/p' "$version_file")"
[ -n "$wanted" ] || fail "cannot read agent-vm's version from $version_file"
if [ "$current" != "$wanted" ]; then
/usr/bin/sed -i '' "s/^AGENTVM_MIN_VERSION=\".*\"\$/AGENTVM_MIN_VERSION=\"$wanted\"/" "$library"
status=$?
[ "$status" -eq 0 ] || fail "cannot rewrite AGENTVM_MIN_VERSION in $library"
printf ' AGENTVM_MIN_VERSION changed from %s to %s, the version in %s\n' "$current" "$wanted" "$agentvm_repo"
current="$wanted"
else
printf ' AGENTVM_MIN_VERSION is %s, the version in %s\n' "$current" "$agentvm_repo"
fi
else
printf ' AGENTVM_MIN_VERSION kept at %s\n' "$current"
fi
# -- 1b. The test fixtures ---------------------------------------------------------------------
# The fixtures are captured from the installed agent-vm, not from a build in the agent-vm working
# tree: a build writes its build paths, and a guest daemon requirement that is a code hash instead
# of the Developer ID one, into version.json. Only the parts an agent-vm update changes are
# captured; doctor.json is kept (a test names its free space) and follow updates its digest. When
# the installed agent-vm is not the version the app requires, the tests cannot pass, so the
# script stops before the build; with --skip-tests it only warns.
printf '==== Test fixtures\n'
fixtures="$script_dir/Tests/fixtures/agentvm"
refresh="$script_dir/Tests/helpers/refresh_agentvm_fixtures.sh"
fixture_version="$(/usr/bin/jq -r '.version // empty' "$fixtures/version.json")"
[ -n "$fixture_version" ] || fail "cannot read the agent-vm version of $fixtures/version.json"
if [ "$fixture_version" = "$current" ]; then
printf ' Tests/fixtures/agentvm come from agent-vm %s\n' "$fixture_version"
else
installed="$HOME/.local/bin/agent-vm"
installed_version=""
if [ -x "$installed" ]; then
installed_version="$("$installed" --version 2>/dev/null)"
fi
if [ "$installed_version" = "$current" ]; then
printf ' Tests/fixtures/agentvm come from agent-vm %s; capturing them from the installed %s\n' "$fixture_version" "$installed_version"
"$refresh" "$installed" version packs recipe-check follow
status=$?
[ "$status" -eq 0 ] || fail "refreshing the fixtures failed (status $status); its output above says why"
printf ' add a section on the move to agent-vm %s to Tests/fixtures/agentvm/README.md\n' "$current"
elif [ "$run_tests" = "yes" ]; then
fail "the test fixtures come from agent-vm $fixture_version and the app requires $current, but the installed agent-vm (~/.local/bin/agent-vm) is ${installed_version:-missing}: install agent-vm $current and run this again, or pass --skip-tests"
else
printf ' warning: Tests/fixtures/agentvm come from agent-vm %s, not %s (installed: %s); the tests will fail until they are refreshed\n' "$fixture_version" "$current" "${installed_version:-none}"
fi
fi
# -- 2. Tool residue ---------------------------------------------------------------------------
# Only names that no part of this app ever creates. Anything else starting with a dot is named,
# not deleted: it may be residue of a tool this list does not know yet.
printf '==== Residue\n'
residue="$(/usr/bin/find "$app" -type d -name '.claude' -prune -print)"
if [ -n "$residue" ]; then
printf ' removing tool residue before signing:\n%s\n' "$residue"
/usr/bin/find "$app" -type d -name '.claude' -prune -exec /bin/rm -rf {} +
status=$?
[ "$status" -eq 0 ] || fail "cannot remove the .claude folders listed above"
fi
unknown="$(/usr/bin/find "$app" -path "$app/Contents/Frameworks" -prune -o -name '.*' -not -name '.DS_Store' -print)"
if [ -n "$unknown" ]; then
printf ' warning: unexpected dot-files in the bundle, which codesign may refuse:\n%s\n' "$unknown"
fi
/usr/bin/find "$app" -name '.DS_Store' -delete
# -- 3. Build ----------------------------------------------------------------------------------
printf '==== appletbuilder build\n'
# The arguments were parsed above, so the positional parameters are free to hold the command.
# appletbuilder signs ad hoc by default and reads "-" as an option, so the identity is passed
# only when it is a real one.
set -- build "$app" --thin arm64
if [ "$run_tests" = "yes" ]; then
set -- "$@" --test
fi
if [ "$identity" != "-" ]; then
set -- "$@" --identity "$identity"
fi
"$appletbuilder" "$@"
status=$?
[ "$status" -eq 0 ] || fail "appletbuilder build failed (status $status); its output above says why"
# -- 4. Verify ---------------------------------------------------------------------------------
printf '==== Verify\n'
/usr/bin/codesign --verify --deep --strict "$app"
status=$?
[ "$status" -eq 0 ] || fail "the signature of $app does not verify (status $status)"
archs="$(/usr/bin/lipo -archs "$app/Contents/MacOS/AgentVM")"
status=$?
[ "$status" -eq 0 ] || fail "cannot read the architectures of $app/Contents/MacOS/AgentVM"
[ "$archs" = "arm64" ] || fail "the executable is built for \"$archs\", not arm64 only"
minimum="$(/usr/bin/plutil -extract LSMinimumSystemVersion raw "$app/Contents/Info.plist")"
status=$?
[ "$status" -eq 0 ] || fail "Info.plist has no LSMinimumSystemVersion"
[ "$minimum" = "27.0" ] || fail "Info.plist says LSMinimumSystemVersion $minimum, not 27.0"
printf ' signed (%s), arm64 only, macOS %s or later\n' "$identity" "$minimum"
printf 'AgentVM.app is ready: %s\n' "$app"