pluginfinity 0.2.0 → 0.3.0
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/bats/pluginfinity.bash +497 -12
- package/package.json +5 -5
- package/version.js +1 -1
package/bats/pluginfinity.bash
CHANGED
|
@@ -6,18 +6,160 @@
|
|
|
6
6
|
# load "$BATS_TEST_DIRNAME/../node_modules/pluginfinity/bats/pluginfinity.bash"
|
|
7
7
|
#
|
|
8
8
|
# Tests run against builds/<target>/, so run `pluginfinity build` first.
|
|
9
|
+
#
|
|
10
|
+
# Every run gets XDG_STATE_HOME=$BATS_TEST_TMPDIR/state unless the call passes
|
|
11
|
+
# its own (a trailing XDG_STATE_HOME=... on run_hook or run_monitor, or
|
|
12
|
+
# --env XDG_STATE_HOME=... on run_script; the last one wins, as env applies
|
|
13
|
+
# them). The session env the helper seeds (--session-env, the project pointer
|
|
14
|
+
# and run_hook's SessionStart done marker) goes under that same state dir, so
|
|
15
|
+
# the script reads what was seeded.
|
|
9
16
|
|
|
10
17
|
bats_require_minimum_version 1.5.0
|
|
11
18
|
|
|
12
19
|
: "${PLUGIN_DIR:=$(cd "$BATS_TEST_DIRNAME/.." && pwd)}"
|
|
13
20
|
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
21
|
+
# _pf_project_dir: print the test's project directory, $BATS_TEST_TMPDIR/project,
|
|
22
|
+
# creating it on first use. It is the default for hook_fixture's cwd,
|
|
23
|
+
# HOOK_PROJECT_DIR (so CLAUDE_PROJECT_DIR), a skill script's cwd and a
|
|
24
|
+
# monitor's cwd, so one test sees one project everywhere.
|
|
25
|
+
_pf_project_dir() {
|
|
26
|
+
mkdir -p "$BATS_TEST_TMPDIR/project"
|
|
27
|
+
printf '%s\n' "$BATS_TEST_TMPDIR/project"
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
# _pf_state_dir [VAR=value...]: print the state dir a run with these
|
|
31
|
+
# assignments gets: the value of the last XDG_STATE_HOME=..., else
|
|
32
|
+
# $BATS_TEST_TMPDIR/state.
|
|
33
|
+
_pf_state_dir() {
|
|
34
|
+
local dir="$BATS_TEST_TMPDIR/state" arg
|
|
35
|
+
for arg in "$@"; do
|
|
36
|
+
case "$arg" in
|
|
37
|
+
XDG_STATE_HOME=*) dir=${arg#XDG_STATE_HOME=} ;;
|
|
38
|
+
esac
|
|
39
|
+
done
|
|
40
|
+
printf '%s\n' "$dir"
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
# _pf_seed_session_env <target> <env-file> <session id> [<project dir>]: write
|
|
44
|
+
# the session env library's own state under the test's state dir, so a reader
|
|
45
|
+
# sees the values without SessionStart having run. The file's NAME=value and
|
|
46
|
+
# `export NAME=value` lines are parsed, not sourced. It writes
|
|
47
|
+
# session/<id>/env and, with a project dir, the project pointer a script with no
|
|
48
|
+
# session id uses (the dir walked up to the nearest .git, as the library does).
|
|
49
|
+
# The plugin's name comes from the build's host.sh. The state dir is
|
|
50
|
+
# $_pf_state when set (see _pf_state_dir), else $BATS_TEST_TMPDIR/state.
|
|
51
|
+
_pf_seed_session_env() {
|
|
52
|
+
local target=$1 file=$2 sid=$3 proj=${4:-} root="$PLUGIN_DIR/builds/$1" plugin base dir pair p
|
|
53
|
+
local filevars=()
|
|
54
|
+
if [ ! -f "$file" ]; then
|
|
55
|
+
echo "--session-env $file not found" >&2
|
|
56
|
+
return 1
|
|
57
|
+
fi
|
|
58
|
+
plugin=$(sed -n "s/^PLUGINFINITY_PLUGIN='\\(.*\\)'$/\\1/p" "$root/lib/pluginfinity/host.sh" 2>/dev/null | head -n 1)
|
|
59
|
+
if [ -z "$plugin" ]; then
|
|
60
|
+
echo "--session-env: $root/lib/pluginfinity/host.sh not found; run pluginfinity build" >&2
|
|
61
|
+
return 1
|
|
62
|
+
fi
|
|
63
|
+
base="${_pf_state:-$BATS_TEST_TMPDIR/state}/pluginfinity/$plugin"
|
|
64
|
+
dir="$base/session/$sid"
|
|
65
|
+
mkdir -p "$dir"
|
|
66
|
+
_pf_read_env_file "$file"
|
|
67
|
+
: >"$dir/env"
|
|
68
|
+
for pair in ${filevars[@]+"${filevars[@]}"}; do
|
|
69
|
+
printf '%s\n' "$pair" >>"$dir/env"
|
|
70
|
+
done
|
|
71
|
+
if [ -n "$proj" ]; then
|
|
72
|
+
mkdir -p "$proj"
|
|
73
|
+
p=$(cd "$proj" && pwd -P)
|
|
74
|
+
local q=$p
|
|
75
|
+
while [ -n "$q" ] && [ "$q" != / ] && [ "$q" != . ]; do
|
|
76
|
+
if [ -e "$q/.git" ]; then
|
|
77
|
+
p=$q
|
|
78
|
+
break
|
|
79
|
+
fi
|
|
80
|
+
q=$(dirname "$q")
|
|
81
|
+
done
|
|
82
|
+
mkdir -p "$base/project"
|
|
83
|
+
printf '%s\n%s\n' "$sid" "$p" >"$base/project/$(printf '%s' "$p" | cksum | awk '{ print $1 "-" $2 }')"
|
|
84
|
+
fi
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
# _pf_valid_session_id <id>: 0 when the session env library would accept <id>:
|
|
88
|
+
# not empty, not ., and no /, .., backslash or control character.
|
|
89
|
+
_pf_valid_session_id() {
|
|
90
|
+
case "${1:-}" in
|
|
91
|
+
'' | . | */* | *..* | *\\*) return 1 ;;
|
|
92
|
+
esac
|
|
93
|
+
case "$1" in
|
|
94
|
+
*[[:cntrl:]]*) return 1 ;;
|
|
95
|
+
esac
|
|
96
|
+
return 0
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
# _pf_mark_env_done <target> <session id>: write the env runner's done marker
|
|
100
|
+
# for <session id> under the run's state dir ($_pf_state, else the test's), so a SessionStart reader with no
|
|
101
|
+
# values file resolves live at once instead of waiting up to 3 s for a runner
|
|
102
|
+
# the test never starts. Nothing happens for an invalid id or a build with no
|
|
103
|
+
# env.sh.
|
|
104
|
+
_pf_mark_env_done() {
|
|
105
|
+
local root="$PLUGIN_DIR/builds/$1" plugin
|
|
106
|
+
[ -f "$root/lib/pluginfinity/env.sh" ] || return 0
|
|
107
|
+
_pf_valid_session_id "$2" || return 0
|
|
108
|
+
plugin=$(sed -n "s/^PLUGINFINITY_PLUGIN='\\(.*\\)'$/\\1/p" "$root/lib/pluginfinity/host.sh" 2>/dev/null | head -n 1)
|
|
109
|
+
[ -n "$plugin" ] || return 0
|
|
110
|
+
local dir="${_pf_state:-$BATS_TEST_TMPDIR/state}/pluginfinity/$plugin/session/$2"
|
|
111
|
+
mkdir -p "$dir"
|
|
112
|
+
: >"$dir/done"
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
# run_hook <target> <script> <fixture> [--matcher <m>] [--session-env <file>] [--env-wait] [VAR=value...]
|
|
116
|
+
# The three options may stand anywhere in the trailing list, mixed with VAR=value
|
|
117
|
+
# arguments; only the exact words --matcher, --session-env and --env-wait are
|
|
118
|
+
# options, so a VAR=value whose value starts with -- is still a VAR=value.
|
|
119
|
+
# Runs builds/<target>/<script> under env -i the way the host runs the built
|
|
120
|
+
# hook entry: the entry in the target's hooks file that runs <script>, under the
|
|
121
|
+
# fixture's hook_event_name (else the event of the first such entry), supplies
|
|
122
|
+
# the environment (Claude: the K=V args before `bash`; Copilot: the `env`
|
|
123
|
+
# object). --matcher picks between entries for one script. Explicit VAR=value
|
|
124
|
+
# arguments override the entry's environment. The fixture is on stdin.
|
|
125
|
+
# --session-env <file> seeds the session values a reader hook sees, as if
|
|
126
|
+
# SessionStart had run: the file's NAME=value lines are written to the session env
|
|
127
|
+
# library's values file for the fixture's session_id, under the test state dir.
|
|
128
|
+
# A SessionStart hook other than the env runner, run with no --session-env,
|
|
129
|
+
# finds the runner's done marker already written for the fixture's session_id,
|
|
130
|
+
# so it resolves the session env live at once; --env-wait leaves the marker out
|
|
131
|
+
# and keeps the library's real wait of up to 3 s, for a test of that wait.
|
|
132
|
+
# Sets $status, $output and $stderr.
|
|
17
133
|
run_hook() {
|
|
18
|
-
local target=$1 script=$2 fixture=$3
|
|
134
|
+
local target=$1 script=$2 fixture=$3 matcher="" has_matcher=0 session_env="" env_wait=0
|
|
135
|
+
local rest=()
|
|
19
136
|
shift 3
|
|
20
|
-
|
|
137
|
+
# The options may stand anywhere among the VAR=value arguments. Only the exact
|
|
138
|
+
# words are options, so a VAR=value whose value starts with -- stays a VAR=value.
|
|
139
|
+
while [ $# -gt 0 ]; do
|
|
140
|
+
case "$1" in
|
|
141
|
+
--matcher)
|
|
142
|
+
matcher=${2:-}
|
|
143
|
+
has_matcher=1
|
|
144
|
+
shift $(($# > 1 ? 2 : 1))
|
|
145
|
+
;;
|
|
146
|
+
--session-env)
|
|
147
|
+
session_env=${2:-}
|
|
148
|
+
shift $(($# > 1 ? 2 : 1))
|
|
149
|
+
;;
|
|
150
|
+
--env-wait)
|
|
151
|
+
env_wait=1
|
|
152
|
+
shift
|
|
153
|
+
;;
|
|
154
|
+
*)
|
|
155
|
+
rest+=("$1")
|
|
156
|
+
shift
|
|
157
|
+
;;
|
|
158
|
+
esac
|
|
159
|
+
done
|
|
160
|
+
set -- ${rest[@]+"${rest[@]}"}
|
|
161
|
+
local root="$PLUGIN_DIR/builds/$target" project
|
|
162
|
+
project=$(_pf_project_dir)
|
|
21
163
|
if [ ! -f "$root/$script" ]; then
|
|
22
164
|
echo "run_hook: $root/$script not found; run pluginfinity build" >&2
|
|
23
165
|
return 1
|
|
@@ -26,28 +168,371 @@ run_hook() {
|
|
|
26
168
|
/*) ;;
|
|
27
169
|
*) fixture="$PLUGIN_DIR/__test__/fixtures/$fixture" ;;
|
|
28
170
|
esac
|
|
29
|
-
local
|
|
171
|
+
local hooks_file entries
|
|
172
|
+
case "$target" in
|
|
173
|
+
claude)
|
|
174
|
+
hooks_file="$root/hooks/hooks.json"
|
|
175
|
+
# An exec-form entry (`env K=V ... bash <script>`) or a command-string entry
|
|
176
|
+
# (`export K='V'; ... <command>`); the env is the leading assignments of each.
|
|
177
|
+
entries='[(.hooks // {}) | to_entries[] | .key as $ev | .value[] | (.matcher // "") as $m | (.hooks // [])[]
|
|
178
|
+
| (if ((.args // []) | map(type == "string" and endswith("/" + $s)) | any) then
|
|
179
|
+
{event: $ev, matcher: $m, env: (.args | (map(test("^[A-Za-z_][A-Za-z0-9_]*=")) | index(false) // length) as $i | .[:$i]
|
|
180
|
+
| map({key: sub("=.*$"; ""; "s"), value: sub("^[^=]*="; ""; "s")}) | from_entries)}
|
|
181
|
+
elif ((.command // "") | (contains("/" + $s + "\"") or contains("/" + $s + " ") or endswith("/" + $s))) then
|
|
182
|
+
{event: $ev, matcher: $m, env: ((.command | capture("^(?<p>(export [A-Za-z_][A-Za-z0-9_]*=\u0027([^\u0027]|\u0027\\\\\u0027\u0027)*\u0027; )*)").p)
|
|
183
|
+
| [match("export ([A-Za-z_][A-Za-z0-9_]*)=\u0027((?:[^\u0027]|\u0027\\\\\u0027\u0027)*)\u0027; "; "g") | {key: .captures[0].string, value: (.captures[1].string | gsub("\u0027\\\\\u0027\u0027"; "\u0027"))}] | from_entries)}
|
|
184
|
+
else empty end)]'
|
|
185
|
+
;;
|
|
186
|
+
copilot)
|
|
187
|
+
hooks_file="$root/com.github.copilot/hooks/hooks.json"
|
|
188
|
+
entries='[(.hooks // {}) | to_entries[] | .key as $ev | .value[]
|
|
189
|
+
| select((.bash // "") | (contains("/" + $s + "\"") or contains("/" + $s + " ") or endswith("/" + $s)))
|
|
190
|
+
| {event: $ev, matcher: (.matcher // .env.PLUGINFINITY_MATCHER // ""), env: (.env // {})}]'
|
|
191
|
+
;;
|
|
192
|
+
*)
|
|
193
|
+
echo "run_hook: unknown target $target" >&2
|
|
194
|
+
return 1
|
|
195
|
+
;;
|
|
196
|
+
esac
|
|
197
|
+
if [ ! -f "$hooks_file" ]; then
|
|
198
|
+
echo "run_hook: $hooks_file not found; run pluginfinity build" >&2
|
|
199
|
+
return 1
|
|
200
|
+
fi
|
|
201
|
+
local event all picked count
|
|
30
202
|
event=$(jq -r '.hook_event_name // empty' "$fixture")
|
|
203
|
+
all=$(jq -c --arg s "$script" "$entries" "$hooks_file") || {
|
|
204
|
+
echo "run_hook: cannot read $hooks_file" >&2
|
|
205
|
+
return 1
|
|
206
|
+
}
|
|
207
|
+
picked=$(jq -c --arg e "$event" --arg m "$matcher" --argjson hm "$has_matcher" '
|
|
208
|
+
(if $e == "" then . else (map(select((.event | ascii_downcase) == ($e | ascii_downcase))) as $x | if ($x | length) > 0 then $x else . end) end)
|
|
209
|
+
| (if $hm == 1 then map(select(.matcher == $m)) else . end)' <<<"$all")
|
|
210
|
+
if [ -n "$event" ] && [ "$(jq 'length' <<<"$all")" -gt 0 ] &&
|
|
211
|
+
[ "$(jq -r --arg e "$event" 'map(select((.event | ascii_downcase) == ($e | ascii_downcase))) | length' <<<"$all")" -eq 0 ]; then
|
|
212
|
+
echo "run_hook: no $script entry for event $event; using entries for any event" >&2
|
|
213
|
+
fi
|
|
214
|
+
count=$(jq 'length' <<<"$picked")
|
|
215
|
+
if [ "$count" -eq 0 ]; then
|
|
216
|
+
if [ "$has_matcher" -eq 1 ]; then
|
|
217
|
+
echo "run_hook: no entry in $hooks_file runs $script with matcher '$matcher'" >&2
|
|
218
|
+
else
|
|
219
|
+
echo "run_hook: no entry in $hooks_file runs $script" >&2
|
|
220
|
+
fi
|
|
221
|
+
return 1
|
|
222
|
+
fi
|
|
223
|
+
if [ "$count" -gt 1 ] && [ "$has_matcher" -eq 0 ]; then
|
|
224
|
+
echo "run_hook: $count entries run $script; using the first (pass --matcher)" >&2
|
|
225
|
+
fi
|
|
226
|
+
local entry_env=() line
|
|
227
|
+
while IFS= read -r line; do
|
|
228
|
+
entry_env+=("$line")
|
|
229
|
+
done < <(jq -r '.[0].env | to_entries[] | "\(.key)=\(.value)"' <<<"$picked")
|
|
230
|
+
local sid _pf_state
|
|
231
|
+
_pf_state=$(_pf_state_dir ${entry_env[@]+"${entry_env[@]}"} "$@")
|
|
232
|
+
sid=$(jq -r '.session_id // .sessionId // empty' "$fixture")
|
|
233
|
+
if [ -n "$session_env" ]; then
|
|
234
|
+
_pf_seed_session_env "$target" "$session_env" "${sid:-test-session}" || return 1
|
|
235
|
+
elif [ "$env_wait" -eq 0 ] && [ "$script" != lib/pluginfinity/env-run.sh ] &&
|
|
236
|
+
[ "$(jq -r '.[0].event | ascii_downcase' <<<"$picked")" = sessionstart ]; then
|
|
237
|
+
_pf_mark_env_done "$target" "$sid"
|
|
238
|
+
fi
|
|
31
239
|
case "$target" in
|
|
32
240
|
claude)
|
|
33
241
|
run --separate-stderr env -i PATH="$PATH" HOME="$BATS_TEST_TMPDIR/home" \
|
|
34
242
|
XDG_STATE_HOME="$BATS_TEST_TMPDIR/state" CLAUDE_PLUGIN_ROOT="$root" \
|
|
35
|
-
CLAUDE_PROJECT_DIR="${HOOK_PROJECT_DIR:-$
|
|
243
|
+
CLAUDE_PROJECT_DIR="${HOOK_PROJECT_DIR:-$project}" \
|
|
244
|
+
${entry_env[@]+"${entry_env[@]}"} "$@" bash "$root/$script" <"$fixture"
|
|
36
245
|
;;
|
|
37
246
|
copilot)
|
|
38
|
-
# Copilot runs hooks from the plugin root
|
|
39
|
-
# PLUGINFINITY_EVENT on every entry.
|
|
247
|
+
# Copilot runs hooks from the plugin root.
|
|
40
248
|
run --separate-stderr env -i PATH="$PATH" HOME="$BATS_TEST_TMPDIR/home" \
|
|
41
249
|
XDG_STATE_HOME="$BATS_TEST_TMPDIR/state" PLUGIN_ROOT="$root" \
|
|
42
|
-
|
|
250
|
+
${entry_env[@]+"${entry_env[@]}"} "$@" bash -c 'cd "$1" && shift && exec bash "$@"' _ "$root" "$root/$script" <"$fixture"
|
|
251
|
+
;;
|
|
252
|
+
esac
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
# _pf_host_env <target> <root> [skill]: set the _pf_env array to the env -i
|
|
256
|
+
# arguments a plugin process gets (hook-only variables are added by run_hook).
|
|
257
|
+
# With "skill" it is the environment of a script a skill runs through the agent's
|
|
258
|
+
# Bash tool, which gets none of the plugin variables: on Claude Code, as
|
|
259
|
+
# measured, only CLAUDE_CODE_SESSION_ID (and what SessionStart wrote to
|
|
260
|
+
# CLAUDE_ENV_FILE); on Copilot, not measured and assumed minimal, nothing beyond
|
|
261
|
+
# the base. A launcher gets the plugin variables.
|
|
262
|
+
_pf_host_env() {
|
|
263
|
+
_pf_env=(PATH="$PATH" HOME="$BATS_TEST_TMPDIR/home" XDG_STATE_HOME="$BATS_TEST_TMPDIR/state")
|
|
264
|
+
case "$1" in
|
|
265
|
+
claude)
|
|
266
|
+
if [ "${3:-}" = skill ]; then
|
|
267
|
+
_pf_env+=(CLAUDE_CODE_SESSION_ID=test-session)
|
|
268
|
+
else
|
|
269
|
+
_pf_env+=(CLAUDE_PLUGIN_ROOT="$2" CLAUDE_PROJECT_DIR="${HOOK_PROJECT_DIR:-$(_pf_project_dir)}")
|
|
270
|
+
fi
|
|
43
271
|
;;
|
|
272
|
+
copilot) [ "${3:-}" = skill ] || _pf_env+=(PLUGIN_ROOT="$2") ;;
|
|
44
273
|
*)
|
|
45
|
-
echo "
|
|
274
|
+
echo "unknown target $1" >&2
|
|
46
275
|
return 1
|
|
47
276
|
;;
|
|
48
277
|
esac
|
|
49
278
|
}
|
|
50
279
|
|
|
280
|
+
# _pf_read_env_file <file>: append the file's assignments to the filevars array.
|
|
281
|
+
# The file is parsed, never sourced: blank lines and # comments are skipped, an
|
|
282
|
+
# `export ` prefix is dropped, one pair of surrounding quotes is stripped and
|
|
283
|
+
# nothing is expanded.
|
|
284
|
+
_pf_read_env_file() {
|
|
285
|
+
local line name value
|
|
286
|
+
while IFS= read -r line || [ -n "$line" ]; do
|
|
287
|
+
line=${line#"${line%%[![:space:]]*}"}
|
|
288
|
+
case "$line" in '' | '#'*) continue ;; esac
|
|
289
|
+
case "$line" in
|
|
290
|
+
"export "*) line=${line#export }; line=${line#"${line%%[![:space:]]*}"} ;;
|
|
291
|
+
esac
|
|
292
|
+
case "$line" in
|
|
293
|
+
[A-Za-z_]*=*) ;;
|
|
294
|
+
*) continue ;;
|
|
295
|
+
esac
|
|
296
|
+
name=${line%%=*}
|
|
297
|
+
value=${line#*=}
|
|
298
|
+
case "$name" in *[!A-Za-z0-9_]*) continue ;; esac
|
|
299
|
+
case "$value" in
|
|
300
|
+
\"*\") value=${value#\"}; value=${value%\"} ;;
|
|
301
|
+
\'*\') value=${value#\'}; value=${value%\'} ;;
|
|
302
|
+
esac
|
|
303
|
+
filevars+=("$name=$value")
|
|
304
|
+
done <"$1"
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
# run_script <target> <path> [--stdin <file>] [--cwd <dir>] [--env VAR=value]... [--env-file <file>] [--session-env <file>] [--interpreter <cmd>] [args...]
|
|
308
|
+
# Runs `<interpreter> builds/<target>/<path> args...` under env -i. The
|
|
309
|
+
# interpreter is `node` for a .mjs, .cjs or .js script and `bash` otherwise;
|
|
310
|
+
# --interpreter <cmd> overrides it (the command may carry arguments, e.g. 'bash -x'). A script under skills/
|
|
311
|
+
# gets the environment a skill script gets when the agent runs it through its
|
|
312
|
+
# Bash tool: PATH, HOME, XDG_STATE_HOME and, on Claude Code,
|
|
313
|
+
# CLAUDE_CODE_SESSION_ID=test-session, with none of CLAUDE_PLUGIN_ROOT,
|
|
314
|
+
# CLAUDE_PLUGIN_DATA, CLAUDE_PROJECT_DIR, CLAUDE_SKILL_DIR or CLAUDE_ENV_FILE. It
|
|
315
|
+
# runs from --cwd (default $BATS_TEST_TMPDIR/project, created) on both hosts.
|
|
316
|
+
# --env-file adds the NAME=value and `export NAME=value` lines of a file, parsed
|
|
317
|
+
# not sourced, to model what a SessionStart hook wrote to CLAUDE_ENV_FILE.
|
|
318
|
+
# --session-env <file> seeds the session env library's own state (see run_hook)
|
|
319
|
+
# for session id test-session and for the project the script runs in, so a
|
|
320
|
+
# script that sources env.sh finds the values through the project pointer. On
|
|
321
|
+
# Claude a script outside skills/ is seeded for the project its CLAUDE_PROJECT_DIR
|
|
322
|
+
# names (HOOK_PROJECT_DIR, else $BATS_TEST_TMPDIR/project), which env.sh prefers.
|
|
323
|
+
# Any other path, a server launcher, gets the host's plugin variables and keeps
|
|
324
|
+
# the host's cwd: the plugin root on Copilot, the caller's on Claude. Each --env
|
|
325
|
+
# adds VAR=value (after --env-file, so it wins); everything after the options, a
|
|
326
|
+
# bare `--` included, is passed to the script as arguments. Sets $status, $output
|
|
327
|
+
# and $stderr.
|
|
328
|
+
run_script() {
|
|
329
|
+
local target=$1 script=$2 stdin=/dev/null cwd="" interp="" vars=() filevars=() session_env=""
|
|
330
|
+
shift 2
|
|
331
|
+
while [ $# -gt 0 ]; do
|
|
332
|
+
case "$1" in
|
|
333
|
+
--stdin)
|
|
334
|
+
stdin=$2
|
|
335
|
+
shift 2
|
|
336
|
+
;;
|
|
337
|
+
--cwd)
|
|
338
|
+
cwd=$2
|
|
339
|
+
shift 2
|
|
340
|
+
;;
|
|
341
|
+
--interpreter)
|
|
342
|
+
if [ -z "${2:-}" ]; then
|
|
343
|
+
echo "run_script: --interpreter needs a command" >&2
|
|
344
|
+
return 1
|
|
345
|
+
fi
|
|
346
|
+
interp=$2
|
|
347
|
+
shift 2
|
|
348
|
+
;;
|
|
349
|
+
--env-file)
|
|
350
|
+
if [ ! -f "${2:-}" ]; then
|
|
351
|
+
echo "run_script: --env-file ${2:-} not found" >&2
|
|
352
|
+
return 1
|
|
353
|
+
fi
|
|
354
|
+
_pf_read_env_file "$2"
|
|
355
|
+
shift 2
|
|
356
|
+
;;
|
|
357
|
+
--session-env)
|
|
358
|
+
session_env=${2:-}
|
|
359
|
+
shift 2
|
|
360
|
+
;;
|
|
361
|
+
--env)
|
|
362
|
+
case "${2:-}" in
|
|
363
|
+
[A-Za-z_]*=*) ;;
|
|
364
|
+
*)
|
|
365
|
+
echo "run_script: --env needs VAR=value, got '${2:-}'" >&2
|
|
366
|
+
return 1
|
|
367
|
+
;;
|
|
368
|
+
esac
|
|
369
|
+
case "${2%%=*}" in
|
|
370
|
+
*[!A-Za-z0-9_]*)
|
|
371
|
+
echo "run_script: --env needs VAR=value, got '$2'" >&2
|
|
372
|
+
return 1
|
|
373
|
+
;;
|
|
374
|
+
esac
|
|
375
|
+
vars+=("$2")
|
|
376
|
+
shift 2
|
|
377
|
+
;;
|
|
378
|
+
*) break ;;
|
|
379
|
+
esac
|
|
380
|
+
done
|
|
381
|
+
local args=("$@")
|
|
382
|
+
local root="$PLUGIN_DIR/builds/$target" kind=""
|
|
383
|
+
if [ ! -f "$root/$script" ]; then
|
|
384
|
+
echo "run_script: $root/$script not found; run pluginfinity build" >&2
|
|
385
|
+
return 1
|
|
386
|
+
fi
|
|
387
|
+
case "$script" in skills/*) kind=skill ;; esac
|
|
388
|
+
if [ -z "$interp" ]; then
|
|
389
|
+
case "$script" in
|
|
390
|
+
*.mjs | *.cjs | *.js) interp=node ;;
|
|
391
|
+
*) interp=bash ;;
|
|
392
|
+
esac
|
|
393
|
+
fi
|
|
394
|
+
# The command is word-split on purpose, so --interpreter 'bash -x' works.
|
|
395
|
+
local cmd
|
|
396
|
+
read -r -a cmd <<<"$interp"
|
|
397
|
+
_pf_host_env "$target" "$root" "$kind" || {
|
|
398
|
+
echo "run_script: unknown target $target" >&2
|
|
399
|
+
return 1
|
|
400
|
+
}
|
|
401
|
+
if [ -n "$kind" ]; then
|
|
402
|
+
[ -n "$cwd" ] || cwd=$(_pf_project_dir)
|
|
403
|
+
mkdir -p "$cwd"
|
|
404
|
+
else
|
|
405
|
+
[ -n "$cwd" ] || { [ "$target" = copilot ] && cwd=$root; }
|
|
406
|
+
fi
|
|
407
|
+
if [ -n "$session_env" ]; then
|
|
408
|
+
local seed_proj=${cwd:-$PWD}
|
|
409
|
+
if [ "$target" = claude ] && [ -z "$kind" ]; then
|
|
410
|
+
seed_proj=${HOOK_PROJECT_DIR:-$(_pf_project_dir)}
|
|
411
|
+
fi
|
|
412
|
+
local _pf_state
|
|
413
|
+
_pf_state=$(_pf_state_dir ${filevars[@]+"${filevars[@]}"} ${vars[@]+"${vars[@]}"})
|
|
414
|
+
_pf_seed_session_env "$target" "$session_env" test-session "$seed_proj" || return 1
|
|
415
|
+
fi
|
|
416
|
+
if [ -n "$cwd" ]; then
|
|
417
|
+
run --separate-stderr env -i "${_pf_env[@]}" ${filevars[@]+"${filevars[@]}"} ${vars[@]+"${vars[@]}"} bash -c 'cd "$1" && shift && exec "$@"' _ "$cwd" "${cmd[@]}" "$root/$script" ${args[@]+"${args[@]}"} <"$stdin"
|
|
418
|
+
else
|
|
419
|
+
run --separate-stderr env -i "${_pf_env[@]}" ${filevars[@]+"${filevars[@]}"} ${vars[@]+"${vars[@]}"} "${cmd[@]}" "$root/$script" ${args[@]+"${args[@]}"} <"$stdin"
|
|
420
|
+
fi
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
# _pf_run_bounded <seconds> <name> <command...>: run <command...> in its own
|
|
424
|
+
# process group and kill the group when <seconds> pass, then exit 124 with a
|
|
425
|
+
# message on stderr. bash 3.2 has no setsid or timeout, so `set -m` gives the
|
|
426
|
+
# background job its own group. Leaves no process behind.
|
|
427
|
+
_pf_run_bounded() {
|
|
428
|
+
local secs=$1 name=$2 job dog rc flag
|
|
429
|
+
shift 2
|
|
430
|
+
flag="$BATS_TEST_TMPDIR/.timeout-$$-$RANDOM"
|
|
431
|
+
set -m
|
|
432
|
+
"$@" &
|
|
433
|
+
job=$!
|
|
434
|
+
(
|
|
435
|
+
sleep "$secs"
|
|
436
|
+
: >"$flag"
|
|
437
|
+
kill -TERM -- "-$job" 2>/dev/null
|
|
438
|
+
sleep 1
|
|
439
|
+
kill -KILL -- "-$job" 2>/dev/null
|
|
440
|
+
) &
|
|
441
|
+
dog=$!
|
|
442
|
+
rc=0
|
|
443
|
+
wait "$job" || rc=$?
|
|
444
|
+
kill -KILL -- "-$dog" 2>/dev/null
|
|
445
|
+
wait "$dog" 2>/dev/null || :
|
|
446
|
+
# A job that ignored TERM may have been killed by the watchdog's KILL.
|
|
447
|
+
kill -KILL -- "-$job" 2>/dev/null || :
|
|
448
|
+
set +m
|
|
449
|
+
if [ -e "$flag" ]; then
|
|
450
|
+
rm -f "$flag"
|
|
451
|
+
echo "run_monitor: $name timed out after ${secs}s" >&2
|
|
452
|
+
return 124
|
|
453
|
+
fi
|
|
454
|
+
return "$rc"
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
# run_monitor <target> <name> [--ticks <n>] [--timeout <seconds>] [--cwd <dir>] [--session-env <file>] [VAR=value...]
|
|
458
|
+
# Runs the monitor's command from builds/claude/monitors/monitors.json the way
|
|
459
|
+
# Claude does: from --cwd (default $BATS_TEST_TMPDIR/project, created, standing
|
|
460
|
+
# in for the session's project dir), with ${CLAUDE_PLUGIN_ROOT} substituted into
|
|
461
|
+
# the command text and none of CLAUDE_PROJECT_DIR, CLAUDE_PLUGIN_ROOT,
|
|
462
|
+
# CLAUDE_PLUGIN_DATA or CLAUDE_SESSION_ID in the environment (a monitor does get
|
|
463
|
+
# CLAUDE_CODE_SESSION_ID, set to "test-session"). Stdin is /dev/null.
|
|
464
|
+
# PLUGINFINITY_MONITOR_MAX_TICKS is set to <n> (default 1): every monitor must
|
|
465
|
+
# honour it, stopping after that many checks, however triggered, a node
|
|
466
|
+
# `command` monitor included. --timeout (default 30, at least 1) is a wall-clock bound: the
|
|
467
|
+
# monitor's process group is killed after that many seconds and $status is 124,
|
|
468
|
+
# with `run_monitor: <name> timed out after <s>s` on stderr, so a monitor that
|
|
469
|
+
# never reaches its tick count fails the test instead of hanging bats. A target
|
|
470
|
+
# other than claude fails with status 1 and a message on stderr. Sets $status,
|
|
471
|
+
# $output and $stderr.
|
|
472
|
+
run_monitor() {
|
|
473
|
+
local target=$1 name=$2 ticks=1 cwd="" timeout=30 session_env=""
|
|
474
|
+
shift 2
|
|
475
|
+
if [ "$target" != claude ]; then
|
|
476
|
+
run --separate-stderr bash -c 'echo "run_monitor: $1 has no monitors" >&2; exit 1' _ "$target"
|
|
477
|
+
return 0
|
|
478
|
+
fi
|
|
479
|
+
while [ $# -gt 0 ]; do
|
|
480
|
+
case "$1" in
|
|
481
|
+
--ticks)
|
|
482
|
+
ticks=$2
|
|
483
|
+
shift 2
|
|
484
|
+
;;
|
|
485
|
+
--timeout)
|
|
486
|
+
case "${2:-}" in
|
|
487
|
+
*[!0]* | '') ;;
|
|
488
|
+
*)
|
|
489
|
+
# All zeros: a 0 s bound would kill every monitor before its first tick.
|
|
490
|
+
run --separate-stderr bash -c 'echo "run_monitor: --timeout needs a number of seconds above 0, got $1" >&2; exit 1' _ "'${2:-}'"
|
|
491
|
+
return 0
|
|
492
|
+
;;
|
|
493
|
+
esac
|
|
494
|
+
case "${2:-}" in
|
|
495
|
+
'' | *[!0-9]*)
|
|
496
|
+
run --separate-stderr bash -c 'echo "run_monitor: --timeout needs a number of seconds, got $1" >&2; exit 1' _ "'${2:-}'"
|
|
497
|
+
return 0
|
|
498
|
+
;;
|
|
499
|
+
esac
|
|
500
|
+
timeout=$2
|
|
501
|
+
shift 2
|
|
502
|
+
;;
|
|
503
|
+
--cwd)
|
|
504
|
+
cwd=$2
|
|
505
|
+
shift 2
|
|
506
|
+
;;
|
|
507
|
+
--session-env)
|
|
508
|
+
session_env=${2:-}
|
|
509
|
+
shift 2
|
|
510
|
+
;;
|
|
511
|
+
*) break ;;
|
|
512
|
+
esac
|
|
513
|
+
done
|
|
514
|
+
local root="$PLUGIN_DIR/builds/claude" command
|
|
515
|
+
command=$(jq -r --arg n "$name" '[.[] | select(.name == $n)][0].command // empty' "$root/monitors/monitors.json" 2>/dev/null)
|
|
516
|
+
if [ -z "$command" ]; then
|
|
517
|
+
run --separate-stderr bash -c 'echo "run_monitor: no monitor $1" >&2; exit 1' _ "$name"
|
|
518
|
+
return 0
|
|
519
|
+
fi
|
|
520
|
+
local token='${CLAUDE_PLUGIN_ROOT}'
|
|
521
|
+
command=${command//"$token"/"$root"}
|
|
522
|
+
[ -n "$cwd" ] || cwd=$(_pf_project_dir)
|
|
523
|
+
mkdir -p "$cwd"
|
|
524
|
+
if [ -n "$session_env" ]; then
|
|
525
|
+
local _pf_state
|
|
526
|
+
_pf_state=$(_pf_state_dir "$@")
|
|
527
|
+
_pf_seed_session_env claude "$session_env" test-session "$cwd" || return 1
|
|
528
|
+
fi
|
|
529
|
+
run --separate-stderr _pf_run_bounded "$timeout" "$name" \
|
|
530
|
+
env -i PATH="$PATH" HOME="$BATS_TEST_TMPDIR/home" \
|
|
531
|
+
XDG_STATE_HOME="$BATS_TEST_TMPDIR/state" CLAUDE_CODE_SESSION_ID=test-session \
|
|
532
|
+
PLUGINFINITY_MONITOR_MAX_TICKS="$ticks" "$@" \
|
|
533
|
+
bash -c 'cd "$1" && exec bash -c "$2"' _ "$cwd" "$command" </dev/null
|
|
534
|
+
}
|
|
535
|
+
|
|
51
536
|
# assert_hook_exit <n>
|
|
52
537
|
assert_hook_exit() {
|
|
53
538
|
[ "$status" -eq "$1" ] || {
|
|
@@ -82,7 +567,7 @@ hook_fixture() {
|
|
|
82
567
|
local overrides=${2:-}
|
|
83
568
|
[ -n "$overrides" ] || overrides='{}'
|
|
84
569
|
local file="$BATS_TEST_TMPDIR/fixture-$1-$RANDOM.json"
|
|
85
|
-
jq -n --arg e "$1" --arg cwd "$
|
|
570
|
+
jq -n --arg e "$1" --arg cwd "$(_pf_project_dir)" --argjson o "$overrides" \
|
|
86
571
|
'{session_id: "test-session", transcript_path: "/dev/null", cwd: $cwd, hook_event_name: $e} + $o' >"$file"
|
|
87
572
|
printf '%s\n' "$file"
|
|
88
573
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pluginfinity",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Build one agent-plugin source into every host's format: Claude Code, GitHub Copilot and more, from a single config.",
|
|
6
6
|
"keywords": [
|
|
@@ -48,10 +48,10 @@
|
|
|
48
48
|
"@effected/glob": "^0.10.0",
|
|
49
49
|
"@effected/semver": "^0.11.0",
|
|
50
50
|
"@effected/walker": "^0.15.0",
|
|
51
|
-
"@pluginfinity/cli": "0.
|
|
52
|
-
"@pluginfinity/core": "0.
|
|
53
|
-
"@pluginfinity/engine": "0.
|
|
54
|
-
"@pluginfinity/targets": "0.
|
|
51
|
+
"@pluginfinity/cli": "0.3.0",
|
|
52
|
+
"@pluginfinity/core": "0.3.0",
|
|
53
|
+
"@pluginfinity/engine": "0.3.0",
|
|
54
|
+
"@pluginfinity/targets": "0.3.0",
|
|
55
55
|
"effect": "^4.0.1",
|
|
56
56
|
"jiti": "^2.6.1"
|
|
57
57
|
},
|
package/version.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* with this package's own version, so this is not a runtime environment read.
|
|
9
9
|
* `"0.0.0"` is the unbuilt-source fallback.
|
|
10
10
|
*/
|
|
11
|
-
const PLUGINFINITY_VERSION = "0.
|
|
11
|
+
const PLUGINFINITY_VERSION = "0.3.0";
|
|
12
12
|
|
|
13
13
|
//#endregion
|
|
14
14
|
export { PLUGINFINITY_VERSION };
|