counterparts 0.3.13 → 0.3.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +160 -0
- package/package.json +1 -1
- package/src/adapters/claude-code/bin/hook.mjs +4 -2
- package/src/adapters/claude-code/bin/hook.ts +11 -4
- package/src/adapters/claude-code/bin/nightly.ts +5 -1
- package/src/adapters/claude-code/bin/runner.ts +11 -3
- package/src/adapters/claude-code/doctor.ts +69 -5
- package/src/adapters/claude-code/embed-client.ts +3 -0
- package/src/adapters/cli/bin/counterparts.mjs +4 -2
- package/src/adapters/cli/bin/counterparts.ts +11 -3
- package/src/adapters/cli/commands.ts +65 -23
- package/src/adapters/cli/index.ts +1 -0
- package/src/adapters/cli/install.ts +22 -9
- package/src/adapters/cli/settle.ts +39 -2
- package/src/adapters/cli/wire.ts +4 -0
- package/src/adapters/config.ts +29 -1
- package/src/adapters/dashboard/NOTES.md +33 -0
- package/src/adapters/dashboard/bin/dashboard.mjs +4 -2
- package/src/adapters/dashboard/bin/dashboard.ts +11 -3
- package/src/adapters/dashboard/index.ts +14 -0
- package/src/adapters/dashboard/source.ts +0 -8
- package/src/adapters/dashboard/web/fired.ts +8 -0
- package/src/adapters/dashboard/web/pages/flow/sections/fired.js +1 -1
- package/src/adapters/dashboard/web/pages/health/sections/cycle.js +7 -10
- package/src/adapters/dashboard/web/pages/memories/memories.css +3 -0
- package/src/adapters/dashboard/web/pages/memories/row.js +5 -2
- package/src/adapters/dashboard/web/pages/memories/sections/hold.js +2 -1
- package/src/adapters/dashboard/web/pages/memories/sections/search.js +19 -3
- package/src/adapters/dashboard/web/server.ts +16 -4
- package/src/adapters/dashboard/web/shared/dates.js +6 -16
- package/src/adapters/dashboard/web/shared/memory-modal.js +21 -8
- package/src/adapters/dashboard/web/views/health.ts +11 -0
- package/src/adapters/dashboard/web/views/mechanism-panel.ts +8 -5
- package/src/adapters/dashboard/web/views/memories.ts +26 -7
- package/src/adapters/dashboard/web/views/memory-words.ts +16 -0
- package/src/adapters/dashboard/web/views/memory.ts +51 -2
- package/src/adapters/dashboard/web/views/mind.ts +31 -6
- package/src/adapters/empty-bunfig.toml +5 -0
- package/src/adapters/fired.ts +6 -2
- package/src/adapters/host-wiring.ts +4 -3
- package/src/adapters/log/index.ts +2 -2
- package/src/adapters/mcp/CONTRACT.md +16 -2
- package/src/adapters/mcp/INTERFACE-GAPS.md +15 -1
- package/src/adapters/mcp/NOTES.md +243 -0
- package/src/adapters/mcp/bin/serve.mjs +4 -2
- package/src/adapters/mcp/bin/serve.ts +11 -4
- package/src/adapters/mcp/facts.ts +72 -4
- package/src/adapters/mcp/meaning.ts +218 -15
- package/src/adapters/mcp/server.ts +1 -1
- package/src/adapters/mcp/tools.ts +81 -57
- package/src/adapters/plugin-run.sh +405 -33
- package/src/adapters/plugin.ts +14 -2
- package/src/adapters/runtime.ts +192 -17
- package/src/adapters/sessions.ts +2 -2
- package/src/core/associate/NOTES.md +10 -0
- package/src/core/bridge.ts +1 -1
- package/src/core/briefing.ts +20 -3
- package/src/core/contradictions.ts +56 -20
- package/src/core/counterpart.ts +39 -17
- package/src/core/dream/CONTRACT.md +4 -12
- package/src/core/dream/INTERFACE-GAPS.md +3 -2
- package/src/core/dream/NOTES.md +10 -0
- package/src/core/dream/index.ts +10 -52
- package/src/core/embed/static.ts +5 -1
- package/src/core/encode/INTERFACE-GAPS.md +2 -1
- package/src/core/encode/NOTES.md +12 -0
- package/src/core/encode/chunk.ts +6 -30
- package/src/core/encode/index.ts +0 -1
- package/src/core/encode/types.ts +1 -13
- package/src/core/handoff/last-here.ts +18 -2
- package/src/core/physics/CONTRACT.md +6 -0
- package/src/core/physics/NOTES.md +33 -2
- package/src/core/physics/index.ts +22 -7
- package/src/core/prospective/CONTRACT.md +3 -1
- package/src/core/prospective/NOTES.md +15 -0
- package/src/core/recall/time-ask.ts +154 -19
- package/src/core/self/CONTRACT.md +23 -3
- package/src/core/self/NOTES.md +64 -0
- package/src/core/self/behind.ts +6 -3
- package/src/core/self/briefing.ts +272 -11
- package/src/core/self/index.ts +50 -4
- package/src/core/sleep/NOTES.md +32 -0
- package/src/core/store/NOTES.md +16 -0
- package/src/core/time.ts +15 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/bin/sh
|
|
2
|
-
# The Claude Code plugin's launcher
|
|
2
|
+
# The Claude Code plugin's launcher.
|
|
3
3
|
#
|
|
4
4
|
# sh plugin-run.sh hook - the five hooks (hooks/hooks.json)
|
|
5
5
|
# sh plugin-run.sh mcp - the memory server (.claude-plugin/plugin.json)
|
|
@@ -14,35 +14,85 @@
|
|
|
14
14
|
# plugin's command is fixed in a file we ship, so the search happens here, at
|
|
15
15
|
# every launch, in this order:
|
|
16
16
|
#
|
|
17
|
-
# 1. COUNTERPARTS_RUNTIME, when set: that executable and
|
|
17
|
+
# 1. COUNTERPARTS_RUNTIME, when set: that executable and no other runtime
|
|
18
|
+
# (Bun, Node, or a Counterparts single binary); one that is not there or
|
|
19
|
+
# too old goes on to 4, not to 2 or 3.
|
|
18
20
|
# 2. Bun 1.3+: on PATH, then $BUN_INSTALL/bin, ~/.bun/bin, Homebrew.
|
|
19
21
|
# 3. Node 22.15+: on PATH, then Homebrew, /usr/local, Volta, nvm, fnm, asdf,
|
|
20
22
|
# mise. A Node older than 22.15 is skipped, not run (it cannot load the
|
|
21
23
|
# TypeScript sources: adapters/runtime.ts#NODE_FLOOR).
|
|
24
|
+
# 4. THE SINGLE BINARY (docs/single-binary.md): one prebuilt Counterparts
|
|
25
|
+
# program for this platform and this exact version, kept in the plugin's
|
|
26
|
+
# data directory, $CLAUDE_PLUGIN_DATA/bin/<version>/counterparts.
|
|
22
27
|
#
|
|
23
28
|
# Bun first, because that is what the npm bins do (cli/bin/counterparts.mjs).
|
|
24
29
|
# The entry it runs is the same one those bins run (hook.mjs, serve.mjs,
|
|
25
30
|
# counterparts.mjs), so the runtime choice is the only difference.
|
|
26
31
|
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
+
# Bun runs with --no-env-file and --config=<empty-bunfig.toml>: Claude Code
|
|
33
|
+
# starts the hooks and the server in the person's project, and Bun would
|
|
34
|
+
# otherwise load that project's .env into them (nothing pins the plugin
|
|
35
|
+
# server's COUNTERPARTS_DATA_DIR, so a project .env could name another store)
|
|
36
|
+
# and run its bunfig.toml `preload` inside them (adapters/runtime.ts). Node
|
|
37
|
+
# reads neither unless told to; the single binary is built with both off
|
|
38
|
+
# (--no-compile-autoload-dotenv, --no-compile-autoload-bunfig).
|
|
32
39
|
#
|
|
33
|
-
#
|
|
34
|
-
#
|
|
40
|
+
# WITH NONE OF THE FOUR, THIS FETCHES THE BINARY — once, in the background,
|
|
41
|
+
# under a lock, never blocking a hook or the server's start-up:
|
|
42
|
+
#
|
|
43
|
+
# - The platform: macOS arm64 (`sysctl hw.optional.arm64`, which a shell
|
|
44
|
+
# started under Rosetta still answers truthfully, unlike `uname -m`), macOS
|
|
45
|
+
# x64, Linux x64/arm64 (glibc), Windows x64 — though no release offers
|
|
46
|
+
# Windows one yet (tools/single-binary/build.ts#RELEASED), so there the
|
|
47
|
+
# message still names a runtime to install.
|
|
48
|
+
# - The program comes from this version's GitHub release: the URL is built
|
|
49
|
+
# here from plugin.json's version and the platform, nothing else. Its
|
|
50
|
+
# sha256s come from .claude-plugin/binaries.json: the checksums SHIP WITH
|
|
51
|
+
# THE PLUGIN — a sum fetched from the same place as the file would prove
|
|
52
|
+
# the transfer and nothing about where it came from. curl takes HTTPS only,
|
|
53
|
+
# redirects included (GitHub sends release assets on to its own CDN, whose
|
|
54
|
+
# host curl cannot pin and has changed before; the pinned sha256 is what
|
|
55
|
+
# makes any host's bytes safe), and no more than the size binaries.json
|
|
56
|
+
# names. The download is checked compressed and again unpacked, in a
|
|
57
|
+
# directory only this user can enter, before it is made executable; only
|
|
58
|
+
# then is it moved into place, in one rename. A file that fails either
|
|
59
|
+
# check is deleted and never run.
|
|
60
|
+
# - A KEPT PROGRAM IS NOT TRUSTED FOREVER. Every full check writes a stamp
|
|
61
|
+
# beside it: the sha256 it matched, and the file's inode, size and mtime.
|
|
62
|
+
# Each launch compares the stamp with binaries.json and the file (one
|
|
63
|
+
# `stat`); the server's start (once a session), a stamp a day old, or any
|
|
64
|
+
# difference re-hashes the whole file (50–80 ms for 100 MiB with openssl,
|
|
65
|
+
# measured on an M3 Pro; a hook's own check is about 1 ms). A program
|
|
66
|
+
# that no longer matches is deleted and downloaded again.
|
|
67
|
+
# - Meanwhile SessionStart says, once, that Counterparts is getting ready
|
|
68
|
+
# (the size, where it comes from, and that memory starts next session);
|
|
69
|
+
# every other hook exits 0 in silence; the server answers the protocol
|
|
70
|
+
# with no tools and says the same in its instructions. A failure gets one
|
|
71
|
+
# line and is retried at a later start (not sooner than ten minutes).
|
|
72
|
+
# - COUNTERPARTS_BINARY_DOWNLOAD=off forbids the download (the message then
|
|
73
|
+
# names a runtime to install, as it did before the binary existed).
|
|
74
|
+
# COUNTERPARTS_BINARY_URL replaces the release URL — for tests; the
|
|
75
|
+
# checksums still come from the plugin.
|
|
76
|
+
#
|
|
77
|
+
# Claude Code's own native binary does not double as a JavaScript runtime
|
|
78
|
+
# (BUN_BE_BUN=1 is ignored; measured on 2.1.295).
|
|
79
|
+
#
|
|
80
|
+
# Shell built-ins and parameter expansion for everything up to the message,
|
|
81
|
+
# plus the runtimes' own --version; the download alone needs curl, gunzip and
|
|
82
|
+
# a sha256 tool, and says so by name when one is missing.
|
|
35
83
|
|
|
36
84
|
mode="${1:-}"
|
|
37
85
|
[ $# -gt 0 ] && shift
|
|
38
86
|
|
|
39
87
|
here="${0%/*}"
|
|
40
88
|
[ "$here" = "$0" ] && here="."
|
|
89
|
+
root="$here/../.."
|
|
41
90
|
|
|
42
91
|
case "$mode" in
|
|
43
92
|
hook) entry="$here/claude-code/bin/hook.mjs" ;;
|
|
44
93
|
mcp) entry="$here/mcp/bin/serve.mjs" ;;
|
|
45
94
|
cli) entry="$here/cli/bin/counterparts.mjs" ;;
|
|
95
|
+
__fetch) entry="" ;;
|
|
46
96
|
*)
|
|
47
97
|
echo "counterparts plugin-run: usage: sh plugin-run.sh hook|mcp|cli [args]" >&2
|
|
48
98
|
exit 2
|
|
@@ -50,6 +100,7 @@ case "$mode" in
|
|
|
50
100
|
esac
|
|
51
101
|
|
|
52
102
|
runtime=""
|
|
103
|
+
kind=""
|
|
53
104
|
|
|
54
105
|
# version_at_least <version> <major> <minor> (a leading "v" is ignored)
|
|
55
106
|
version_at_least() {
|
|
@@ -70,6 +121,7 @@ try_bun() {
|
|
|
70
121
|
v="$("$1" --version 2>/dev/null)" || return 1
|
|
71
122
|
version_at_least "$v" 1 3 || return 1
|
|
72
123
|
runtime="$1"
|
|
124
|
+
kind="bun"
|
|
73
125
|
}
|
|
74
126
|
|
|
75
127
|
try_node() {
|
|
@@ -77,6 +129,7 @@ try_node() {
|
|
|
77
129
|
v="$("$1" --version 2>/dev/null)" || return 1
|
|
78
130
|
version_at_least "$v" 22 15 || return 1
|
|
79
131
|
runtime="$1"
|
|
132
|
+
kind="node"
|
|
80
133
|
}
|
|
81
134
|
|
|
82
135
|
try_node_glob() { # a glob of node binaries: the first that is new enough
|
|
@@ -86,34 +139,305 @@ try_node_glob() { # a glob of node binaries: the first that is new enough
|
|
|
86
139
|
return 1
|
|
87
140
|
}
|
|
88
141
|
|
|
89
|
-
if [
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
142
|
+
if [ "$mode" != "__fetch" ]; then
|
|
143
|
+
if [ -n "${COUNTERPARTS_RUNTIME:-}" ]; then
|
|
144
|
+
case "${COUNTERPARTS_RUNTIME##*/}" in
|
|
145
|
+
node*) try_node "$COUNTERPARTS_RUNTIME" ;;
|
|
146
|
+
counterparts | counterparts.exe | counterparts-*)
|
|
147
|
+
[ -x "$COUNTERPARTS_RUNTIME" ] && exec "$COUNTERPARTS_RUNTIME" "$mode" "$@"
|
|
148
|
+
;;
|
|
149
|
+
*) try_bun "$COUNTERPARTS_RUNTIME" ;;
|
|
150
|
+
esac
|
|
151
|
+
else
|
|
152
|
+
try_bun "$(command -v bun 2>/dev/null)" ||
|
|
153
|
+
try_bun "${BUN_INSTALL:-$HOME/.bun}/bin/bun" ||
|
|
154
|
+
try_bun "$HOME/.bun/bin/bun" ||
|
|
155
|
+
try_bun /opt/homebrew/bin/bun ||
|
|
156
|
+
try_bun /usr/local/bin/bun ||
|
|
157
|
+
try_node "$(command -v node 2>/dev/null)" ||
|
|
158
|
+
try_node /opt/homebrew/bin/node ||
|
|
159
|
+
try_node /usr/local/bin/node ||
|
|
160
|
+
try_node /usr/bin/node ||
|
|
161
|
+
try_node "$HOME/.volta/bin/node" ||
|
|
162
|
+
try_node_glob "$HOME"/.nvm/versions/node/v*/bin/node ||
|
|
163
|
+
try_node_glob "$HOME"/.local/share/fnm/node-versions/*/installation/bin/node ||
|
|
164
|
+
try_node_glob "$HOME/Library/Application Support/fnm/node-versions"/*/installation/bin/node ||
|
|
165
|
+
try_node_glob "$HOME"/.asdf/installs/nodejs/*/bin/node ||
|
|
166
|
+
try_node_glob "$HOME"/.local/share/mise/installs/node/*/bin/node
|
|
167
|
+
fi
|
|
168
|
+
|
|
169
|
+
if [ "$kind" = "bun" ]; then
|
|
170
|
+
exec "$runtime" --no-env-file "--config=$here/empty-bunfig.toml" "$entry" "$@"
|
|
171
|
+
fi
|
|
172
|
+
if [ -n "$runtime" ]; then
|
|
173
|
+
exec "$runtime" "$entry" "$@"
|
|
174
|
+
fi
|
|
175
|
+
fi
|
|
176
|
+
|
|
177
|
+
# ── the single binary ───────────────────────────────────────────────────────
|
|
178
|
+
|
|
179
|
+
# "key": value out of one line of JSON, by parameter expansion.
|
|
180
|
+
json_field() { # json_field <line> <key>
|
|
181
|
+
case "$1" in *"\"$2\": "*) ;; *) return 1 ;; esac
|
|
182
|
+
jv="${1#*\"$2\": }"
|
|
183
|
+
jv="${jv#\"}"
|
|
184
|
+
jv="${jv%%[\",\}]*}"
|
|
185
|
+
printf '%s' "$jv"
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
platform=""
|
|
189
|
+
exe=""
|
|
190
|
+
why="" # set when there is no binary to be had, in words
|
|
191
|
+
os="$(uname -s 2>/dev/null)"
|
|
192
|
+
case "$os" in
|
|
193
|
+
Darwin)
|
|
194
|
+
# By its full path: a host's PATH need not carry /usr/sbin, and an Apple
|
|
195
|
+
# silicon Mac that could not ask would be sent the Intel program.
|
|
196
|
+
arm="$(/usr/sbin/sysctl -n hw.optional.arm64 2>/dev/null || sysctl -n hw.optional.arm64 2>/dev/null)"
|
|
197
|
+
[ -z "$arm" ] && [ "$(uname -m 2>/dev/null)" = "arm64" ] && arm="1"
|
|
198
|
+
if [ "$arm" = "1" ]; then platform="darwin-arm64"; else platform="darwin-x64"; fi
|
|
199
|
+
;;
|
|
200
|
+
Linux)
|
|
201
|
+
if ls /lib/ld-musl-* >/dev/null 2>&1; then
|
|
202
|
+
why="no prebuilt program for musl Linux (Alpine)"
|
|
203
|
+
else
|
|
204
|
+
case "$(uname -m 2>/dev/null)" in
|
|
205
|
+
x86_64 | amd64) platform="linux-x64" ;;
|
|
206
|
+
aarch64 | arm64) platform="linux-arm64" ;;
|
|
207
|
+
*) why="no prebuilt program for this processor" ;;
|
|
208
|
+
esac
|
|
209
|
+
fi
|
|
210
|
+
;;
|
|
211
|
+
MINGW* | MSYS* | CYGWIN*) platform="windows-x64" exe=".exe" ;;
|
|
212
|
+
*) why="no prebuilt program for this system" ;;
|
|
213
|
+
esac
|
|
214
|
+
|
|
215
|
+
data="${CLAUDE_PLUGIN_DATA:-}"
|
|
216
|
+
# `/counterparts:doctor` runs this through Claude Code's Bash tool, whose
|
|
217
|
+
# environment need not carry CLAUDE_PLUGIN_DATA: then the directory Claude
|
|
218
|
+
# Code gives this plugin (`<config dir>/plugins/data/<name>-<marketplace>`),
|
|
219
|
+
# if it is there.
|
|
220
|
+
if [ -z "$data" ] && [ -d "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/plugins/data/counterparts-counterparts" ]; then
|
|
221
|
+
data="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/plugins/data/counterparts-counterparts"
|
|
222
|
+
fi
|
|
223
|
+
[ -z "$data" ] && [ -z "$why" ] && why="the plugin has no data directory to keep it in"
|
|
224
|
+
case "${COUNTERPARTS_BINARY_DOWNLOAD:-}" in off | 0 | no | false) [ -z "$why" ] && why="COUNTERPARTS_BINARY_DOWNLOAD is off" ;; esac
|
|
225
|
+
|
|
226
|
+
version=""
|
|
227
|
+
manifest="$root/.claude-plugin/binaries.json"
|
|
228
|
+
if [ -r "$root/.claude-plugin/plugin.json" ]; then
|
|
229
|
+
while IFS= read -r line || [ -n "$line" ]; do
|
|
230
|
+
v="$(json_field "$line" version)" && { version="$v"; break; }
|
|
231
|
+
done <"$root/.claude-plugin/plugin.json"
|
|
232
|
+
fi
|
|
233
|
+
|
|
234
|
+
case "$version" in "" | *[!0-9A-Za-z.+-]*) [ -z "$why" ] && why="the plugin names no usable version" ;; esac
|
|
235
|
+
|
|
236
|
+
sha=""
|
|
237
|
+
gzsha=""
|
|
238
|
+
gzbytes=""
|
|
239
|
+
if [ -z "$why" ]; then
|
|
240
|
+
if [ ! -r "$manifest" ]; then
|
|
241
|
+
why="this version of the plugin carries no prebuilt program"
|
|
242
|
+
else
|
|
243
|
+
mversion=""
|
|
244
|
+
while IFS= read -r line || [ -n "$line" ]; do
|
|
245
|
+
case "$line" in
|
|
246
|
+
*'"version": '*) mversion="$(json_field "$line" version)" ;;
|
|
247
|
+
*"\"$platform\": "*)
|
|
248
|
+
sha="$(json_field "$line" sha256)"
|
|
249
|
+
gzsha="$(json_field "$line" gzSha256)"
|
|
250
|
+
gzbytes="$(json_field "$line" gzBytes)"
|
|
251
|
+
;;
|
|
252
|
+
esac
|
|
253
|
+
done <"$manifest"
|
|
254
|
+
if [ "$mversion" != "$version" ]; then
|
|
255
|
+
why="no prebuilt program was published for version $version"
|
|
256
|
+
elif [ -z "$sha" ] || [ -z "$gzsha" ]; then
|
|
257
|
+
case "$platform" in
|
|
258
|
+
windows-*) why="no prebuilt program for Windows yet" ;;
|
|
259
|
+
*) why="no prebuilt program for $platform" ;;
|
|
260
|
+
esac
|
|
261
|
+
fi
|
|
262
|
+
fi
|
|
263
|
+
fi
|
|
264
|
+
# The release's URL and the asset's name, from the version and platform alone
|
|
265
|
+
# (binaries.json's own "url" and "file" say the same, for verify-release.ts).
|
|
266
|
+
# COUNTERPARTS_BINARY_URL is for tests; the checksums still come from the plugin.
|
|
267
|
+
url="https://github.com/mlapeter/counterparts/releases/download/v$version"
|
|
268
|
+
asset="counterparts-$version-$platform$exe.gz"
|
|
269
|
+
[ -n "${COUNTERPARTS_BINARY_URL:-}" ] && url="${COUNTERPARTS_BINARY_URL%/}"
|
|
270
|
+
|
|
271
|
+
bindir="$data/bin"
|
|
272
|
+
bin="$bindir/$version/counterparts$exe"
|
|
273
|
+
verified="$bindir/$version/verified" # "<sha256> <inode> <size> <mtime>", written by a full check
|
|
274
|
+
lock="$bindir/download-$version.lock"
|
|
275
|
+
failed="$bindir/download-$version.failed"
|
|
276
|
+
|
|
277
|
+
sha256_of() { # the hex digest of a file, or nothing (fastest tool first)
|
|
278
|
+
# The file on stdin, so no tool prints (or escapes) its name: GNU sha256sum
|
|
279
|
+
# puts a backslash before the digest of a path that has one in it.
|
|
280
|
+
if [ ! -r "$1" ]; then
|
|
281
|
+
d=""
|
|
282
|
+
elif command -v sha256sum >/dev/null 2>&1; then
|
|
283
|
+
d="$(sha256sum <"$1" 2>/dev/null)"
|
|
284
|
+
elif command -v openssl >/dev/null 2>&1; then
|
|
285
|
+
d="$(openssl dgst -sha256 -r <"$1" 2>/dev/null)"
|
|
286
|
+
elif command -v shasum >/dev/null 2>&1; then
|
|
287
|
+
d="$(shasum -a 256 <"$1" 2>/dev/null)"
|
|
288
|
+
else
|
|
289
|
+
d=""
|
|
290
|
+
fi
|
|
291
|
+
printf '%s' "${d%% *}"
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
stat_of() { # "<inode> <size> <mtime>" of a file, or nothing
|
|
295
|
+
# macOS's own stat, by its full path: GNU coreutils' (Homebrew's gnubin,
|
|
296
|
+
# first on PATH) reads -f as --file-system, and no stamp would ever match.
|
|
297
|
+
case "$os" in
|
|
298
|
+
Darwin) /usr/bin/stat -f '%i %z %m' "$1" 2>/dev/null ;;
|
|
299
|
+
*) stat -c '%i %s %Y' "$1" 2>/dev/null ;;
|
|
93
300
|
esac
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
# Stamp the program as checked against $sha, in one rename.
|
|
304
|
+
stamp_verified() {
|
|
305
|
+
s="$(stat_of "$bin")"
|
|
306
|
+
[ -n "$s" ] || return 0
|
|
307
|
+
printf '%s %s\n' "$sha" "$s" >"$verified.$$" 2>/dev/null && mv -f "$verified.$$" "$verified" 2>/dev/null
|
|
308
|
+
rm -f "$verified.$$" 2>/dev/null
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
# ── a program already here: run it, checked ─────────────────────────────────
|
|
312
|
+
if [ -z "$why" ] && [ "$mode" != "__fetch" ] && [ -f "$bin" ]; then
|
|
313
|
+
stamp=""
|
|
314
|
+
[ -r "$verified" ] && IFS= read -r stamp <"$verified"
|
|
315
|
+
now="$(stat_of "$bin")"
|
|
316
|
+
# Fast path, every hook: the stamp names THIS plugin's checksum and this very
|
|
317
|
+
# file (same inode, size, mtime), and is under a day old.
|
|
318
|
+
if [ "$mode" != "mcp" ] && [ -n "$now" ] && [ "$stamp" = "$sha $now" ] && [ -x "$bin" ] &&
|
|
319
|
+
[ -z "$(find "$verified" -mmin +1440 2>/dev/null)" ]; then
|
|
320
|
+
exec "$bin" "$mode" "$@"
|
|
321
|
+
fi
|
|
322
|
+
# The server's start, an old or missing stamp, or a changed file: hash it all.
|
|
323
|
+
if [ "$(sha256_of "$bin")" = "$sha" ]; then
|
|
324
|
+
chmod 700 "$bindir" "$bindir/$version" "$bin" 2>/dev/null
|
|
325
|
+
stamp_verified
|
|
326
|
+
exec "$bin" "$mode" "$@"
|
|
327
|
+
fi
|
|
328
|
+
# It no longer matches what the plugin carries: never run it; fetch it again.
|
|
329
|
+
rm -f "$bin" "$verified"
|
|
110
330
|
fi
|
|
111
331
|
|
|
112
|
-
|
|
113
|
-
|
|
332
|
+
# ── the download itself: `sh plugin-run.sh __fetch`, detached, lock held ──
|
|
333
|
+
if [ "$mode" = "__fetch" ]; then
|
|
334
|
+
[ -z "$why" ] || exit 0
|
|
335
|
+
# Only this user may enter where the program is unpacked and kept.
|
|
336
|
+
umask 077
|
|
337
|
+
tmp="$bindir/.partial-$version-$$"
|
|
338
|
+
fail() {
|
|
339
|
+
printf '%s\n' "$1" >"$failed"
|
|
340
|
+
rm -rf "$tmp"
|
|
341
|
+
rmdir "$lock" 2>/dev/null
|
|
342
|
+
exit 0
|
|
343
|
+
}
|
|
344
|
+
# A partial download whose process is gone died mid-transfer (its bytes
|
|
345
|
+
# were never made executable): swept, whatever its version. One whose
|
|
346
|
+
# process lives is left alone — a download that outlived its lock (a laptop
|
|
347
|
+
# asleep mid-transfer) and the one that took the lock over each finish, or
|
|
348
|
+
# fail, in their own directory, and the rename is atomic either way.
|
|
349
|
+
for p in "$bindir"/.partial-*; do
|
|
350
|
+
[ -d "$p" ] || continue
|
|
351
|
+
kill -0 "${p##*-}" 2>/dev/null || rm -rf "$p"
|
|
352
|
+
done
|
|
353
|
+
mkdir -p "$tmp" "$bindir/$version" || fail "it could not write to the plugin's data directory"
|
|
354
|
+
chmod 700 "$bindir" "$bindir/$version" || fail "it could not make the plugin's data directory private"
|
|
355
|
+
proto="=https"
|
|
356
|
+
case "$url" in http://127.0.0.1[:/]* | http://localhost[:/]*) proto="=http,https" ;; esac # a test's local server
|
|
357
|
+
limit=""
|
|
358
|
+
case "$gzbytes" in "" | *[!0-9]*) ;; *) limit="$((gzbytes + 1048576))" ;; esac
|
|
359
|
+
# Done or given up within 25 minutes, retries included (--max-time bounds
|
|
360
|
+
# each attempt; a retry starts only in the first five), so the half-hour
|
|
361
|
+
# stale lock below is never taken from a download that is merely slow.
|
|
362
|
+
curl -fsSL --proto "$proto" --proto-redir =https --max-redirs 5 ${limit:+--max-filesize "$limit"} \
|
|
363
|
+
--retry 2 --retry-max-time 300 --connect-timeout 20 --max-time 1200 -o "$tmp/download.gz" "$url/$asset" 2>/dev/null ||
|
|
364
|
+
fail "the download from GitHub failed"
|
|
365
|
+
[ "$(sha256_of "$tmp/download.gz")" = "$gzsha" ] || fail "the download did not match the checksum the plugin carries"
|
|
366
|
+
gunzip -c "$tmp/download.gz" >"$tmp/counterparts$exe" 2>/dev/null || fail "the download would not unpack"
|
|
367
|
+
[ "$(sha256_of "$tmp/counterparts$exe")" = "$sha" ] || fail "the program did not match the checksum the plugin carries"
|
|
368
|
+
chmod 700 "$tmp/counterparts$exe" || fail "it could not mark the program runnable"
|
|
369
|
+
mv -f "$tmp/counterparts$exe" "$bin" || fail "it could not move the program into the plugin's data directory"
|
|
370
|
+
stamp_verified
|
|
371
|
+
rm -rf "$tmp"
|
|
372
|
+
rm -f "$failed"
|
|
373
|
+
# Another version's program not started for a week (its stamp is rewritten
|
|
374
|
+
# at every server start) is one no installed plugin runs any more: ~100 MB.
|
|
375
|
+
for old in "$bindir"/*/; do
|
|
376
|
+
old="${old%/}"
|
|
377
|
+
[ "$old" = "$bindir/$version" ] && continue
|
|
378
|
+
[ -f "$old/verified" ] && [ -n "$(find "$old/verified" -mmin +10080 2>/dev/null)" ] && rm -rf "$old"
|
|
379
|
+
done
|
|
380
|
+
rmdir "$lock" 2>/dev/null
|
|
381
|
+
exit 0
|
|
382
|
+
fi
|
|
383
|
+
|
|
384
|
+
# ── no binary yet: start the download (once), and say so ───────────────────
|
|
385
|
+
state="unavailable"
|
|
386
|
+
if [ -z "$why" ]; then
|
|
387
|
+
for tool in curl gunzip; do
|
|
388
|
+
command -v "$tool" >/dev/null 2>&1 || why="$tool is not on PATH"
|
|
389
|
+
done
|
|
390
|
+
if [ -z "$why" ] && ! command -v shasum >/dev/null 2>&1 && ! command -v sha256sum >/dev/null 2>&1 &&
|
|
391
|
+
! command -v openssl >/dev/null 2>&1; then
|
|
392
|
+
why="no sha256 tool (shasum, sha256sum or openssl) is on PATH"
|
|
393
|
+
fi
|
|
394
|
+
fi
|
|
395
|
+
if [ -z "$why" ]; then
|
|
396
|
+
(umask 077 && mkdir -p "$bindir") 2>/dev/null
|
|
397
|
+
# A lock older than half an hour is a download that died holding it (curl
|
|
398
|
+
# gives up within 25 minutes).
|
|
399
|
+
[ -d "$lock" ] && [ -n "$(find "$lock" -maxdepth 0 -mmin +30 2>/dev/null)" ] && rmdir "$lock" 2>/dev/null
|
|
400
|
+
if [ -d "$lock" ]; then
|
|
401
|
+
state="downloading"
|
|
402
|
+
elif [ -f "$failed" ] && [ -z "$(find "$failed" -mmin +10 2>/dev/null)" ]; then
|
|
403
|
+
state="failed"
|
|
404
|
+
elif mkdir "$lock" 2>/dev/null; then
|
|
405
|
+
state="downloading"
|
|
406
|
+
# DETACHED, so neither the hook's exit nor the host ending its process
|
|
407
|
+
# group takes the download with it: its own session where `setsid`
|
|
408
|
+
# exists (Linux), its own process group through job control otherwise
|
|
409
|
+
# (macOS's /bin/sh).
|
|
410
|
+
if command -v setsid >/dev/null 2>&1; then
|
|
411
|
+
setsid sh "$0" __fetch </dev/null >/dev/null 2>&1 &
|
|
412
|
+
else
|
|
413
|
+
(
|
|
414
|
+
set -m 2>/dev/null
|
|
415
|
+
sh "$0" __fetch </dev/null >/dev/null 2>&1 &
|
|
416
|
+
)
|
|
417
|
+
fi
|
|
418
|
+
else
|
|
419
|
+
state="downloading" # another process took the lock a moment ago
|
|
420
|
+
fi
|
|
114
421
|
fi
|
|
115
422
|
|
|
116
|
-
|
|
423
|
+
mb=""
|
|
424
|
+
[ -n "$gzbytes" ] && mb="about $((gzbytes / 1048576)) MB, "
|
|
425
|
+
case "$state" in
|
|
426
|
+
downloading)
|
|
427
|
+
said="Counterparts is getting ready: this computer has no Bun or Node.js, so it is downloading its own program (${mb}from github.com/mlapeter/counterparts releases, checked against the checksum the plugin carries). Memory starts in your next session."
|
|
428
|
+
told="Counterparts memory is not running yet in this session: it is downloading its program (${mb}from github.com/mlapeter/counterparts releases) and starts in the next session. If the person asks about memory, tell them that."
|
|
429
|
+
;;
|
|
430
|
+
failed)
|
|
431
|
+
reason=""
|
|
432
|
+
IFS= read -r reason <"$failed" 2>/dev/null
|
|
433
|
+
said="Counterparts could not get its program ready: ${reason:-the download failed}. Nothing unchecked was run. It will try again when a later session starts, or install Bun (https://bun.sh) or Node.js 22.15+ (https://nodejs.org) and restart Claude Code."
|
|
434
|
+
told="Counterparts memory is not running in this session: downloading its program failed (${reason:-unknown reason}); it retries at a later session start. If the person asks about memory, tell them that."
|
|
435
|
+
;;
|
|
436
|
+
*)
|
|
437
|
+
said="Counterparts (memory) is not running: it needs Bun 1.3+ or Node.js 22.15+ and found neither on PATH or in the usual places${why:+ ($why)}. Install one (https://bun.sh or https://nodejs.org), then restart Claude Code."
|
|
438
|
+
told="Counterparts memory is not running in this session (no Bun 1.3+ or Node.js 22.15+ found). If the person asks about memory, tell them that."
|
|
439
|
+
;;
|
|
440
|
+
esac
|
|
117
441
|
|
|
118
442
|
if [ "$mode" = "hook" ]; then
|
|
119
443
|
payload=""
|
|
@@ -123,11 +447,59 @@ if [ "$mode" = "hook" ]; then
|
|
|
123
447
|
case "$payload" in
|
|
124
448
|
*'"hook_event_name":"SessionStart"'* | *'"hook_event_name": "SessionStart"'*)
|
|
125
449
|
printf '{"systemMessage":"%s","hookSpecificOutput":{"hookEventName":"SessionStart","additionalContext":"%s"}}\n' \
|
|
126
|
-
"$said" "
|
|
450
|
+
"$said" "$told"
|
|
127
451
|
;;
|
|
128
452
|
esac
|
|
129
453
|
exit 0
|
|
130
454
|
fi
|
|
131
455
|
|
|
456
|
+
if [ "$mode" = "mcp" ] && [ "$state" != "unavailable" ]; then
|
|
457
|
+
# A SERVER WITH NO TOOLS that says why, so /mcp shows it connected and the
|
|
458
|
+
# model can answer "is memory on?" — newline-delimited JSON-RPC, read with
|
|
459
|
+
# parameter expansion. Requests get an answer; notifications get none.
|
|
460
|
+
while IFS= read -r line || [ -n "$line" ]; do
|
|
461
|
+
case "$line" in *'"id"'*) ;; *) continue ;; esac
|
|
462
|
+
# The request's own id, not one inside its params: next to "jsonrpc"
|
|
463
|
+
# (the MCP SDK writes `…,"jsonrpc":"2.0","id":N}`, most clients
|
|
464
|
+
# `{"jsonrpc":"2.0","id":N,…`), else first, else the last one.
|
|
465
|
+
case "$line" in
|
|
466
|
+
*'"jsonrpc":"2.0","id":'*) id="${line#*\"jsonrpc\":\"2.0\",\"id\":}" ;;
|
|
467
|
+
'{"id":'*) id="${line#\{\"id\":}" ;;
|
|
468
|
+
*)
|
|
469
|
+
id="${line##*\"id\"}"
|
|
470
|
+
id="${id#*:}"
|
|
471
|
+
;;
|
|
472
|
+
esac
|
|
473
|
+
id="${id# }"
|
|
474
|
+
case "$id" in
|
|
475
|
+
\"*)
|
|
476
|
+
id="${id#\"}"
|
|
477
|
+
id="\"${id%%\"*}\""
|
|
478
|
+
;;
|
|
479
|
+
*) id="${id%%[,\} ]*}" ;;
|
|
480
|
+
esac
|
|
481
|
+
method="${line#*\"method\"}"
|
|
482
|
+
method="${method#*\"}"
|
|
483
|
+
method="${method%%\"*}"
|
|
484
|
+
case "$method" in
|
|
485
|
+
initialize)
|
|
486
|
+
pv="2025-06-18"
|
|
487
|
+
case "$line" in *'"protocolVersion"'*)
|
|
488
|
+
pv="${line#*\"protocolVersion\"}"
|
|
489
|
+
pv="${pv#*\"}"
|
|
490
|
+
pv="${pv%%\"*}"
|
|
491
|
+
;;
|
|
492
|
+
esac
|
|
493
|
+
printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":"%s","capabilities":{"tools":{}},"serverInfo":{"name":"counterparts","version":"%s"},"instructions":"%s"}}\n' \
|
|
494
|
+
"$id" "$pv" "${version:-0}" "$told"
|
|
495
|
+
;;
|
|
496
|
+
tools/list) printf '{"jsonrpc":"2.0","id":%s,"result":{"tools":[]}}\n' "$id" ;;
|
|
497
|
+
ping) printf '{"jsonrpc":"2.0","id":%s,"result":{}}\n' "$id" ;;
|
|
498
|
+
*) printf '{"jsonrpc":"2.0","id":%s,"error":{"code":-32601,"message":"%s"}}\n' "$id" "$told" ;;
|
|
499
|
+
esac
|
|
500
|
+
done
|
|
501
|
+
exit 0
|
|
502
|
+
fi
|
|
503
|
+
|
|
132
504
|
echo "$said" >&2
|
|
133
505
|
exit 127
|
package/src/adapters/plugin.ts
CHANGED
|
@@ -40,13 +40,14 @@ import { spawnSync } from "node:child_process";
|
|
|
40
40
|
import { createHash } from "node:crypto";
|
|
41
41
|
import { existsSync, mkdirSync, readdirSync, realpathSync, rmdirSync, statSync } from "node:fs";
|
|
42
42
|
import { tmpdir } from "node:os";
|
|
43
|
-
import { basename, dirname, join, resolve } from "node:path";
|
|
43
|
+
import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
44
44
|
import { fileURLToPath } from "node:url";
|
|
45
45
|
|
|
46
46
|
import { implicitConfigRefusal } from "./config-path.js";
|
|
47
47
|
import type { ConfigChoice } from "./config-path.js";
|
|
48
48
|
import type { NpmWiring } from "./host-wiring.js";
|
|
49
|
-
import { CLI_SCRIPT, scriptArgs } from "./runtime.js";
|
|
49
|
+
import { BINARY, CLI_SCRIPT, scriptArgs } from "./runtime.js";
|
|
50
|
+
import type { Binary } from "./runtime.js";
|
|
50
51
|
|
|
51
52
|
/** Set by Claude Code on a plugin's hook, MCP and LSP processes. */
|
|
52
53
|
export const PLUGIN_ROOT_ENV = "CLAUDE_PLUGIN_ROOT";
|
|
@@ -81,9 +82,20 @@ function realOrResolved(path: string): string {
|
|
|
81
82
|
export function runningAsPlugin(
|
|
82
83
|
env: Record<string, string | undefined>,
|
|
83
84
|
packageRoot: string = PACKAGE_ROOT,
|
|
85
|
+
binary: Binary | null = BINARY,
|
|
84
86
|
): boolean {
|
|
85
87
|
const named = (env[PLUGIN_ROOT_ENV] ?? "").trim();
|
|
86
88
|
if (named.length === 0) return false;
|
|
89
|
+
// THE SINGLE BINARY: it has no package root on disk — `plugin-run.sh`
|
|
90
|
+
// downloads it into the plugin's own data directory — so "this very copy"
|
|
91
|
+
// is a binary sitting under CLAUDE_PLUGIN_DATA, which Claude Code sets only
|
|
92
|
+
// on the plugin's processes and their children.
|
|
93
|
+
if (binary !== null) {
|
|
94
|
+
const data = (env[PLUGIN_DATA_ENV] ?? "").trim();
|
|
95
|
+
if (data.length === 0) return false;
|
|
96
|
+
const rel = relative(realOrResolved(data), realOrResolved(binary.self));
|
|
97
|
+
return rel.length > 0 && !rel.startsWith("..") && !isAbsolute(rel);
|
|
98
|
+
}
|
|
87
99
|
return realOrResolved(named) === realOrResolved(packageRoot);
|
|
88
100
|
}
|
|
89
101
|
|