@lilmgenius/win-hooks 1.10.0 → 1.12.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/README.md +36 -130
- package/bin/win-hooks.mjs +94 -60
- package/hooks/codex-hooks.json +7 -7
- package/hooks/hooks.json +4 -4
- package/hooks/run-hook.cmd +43 -30
- package/hooks/win-hooks +10 -0
- package/package.json +8 -13
- package/src/env.mjs +122 -0
- package/src/heal.mjs +125 -0
- package/src/hosts.mjs +122 -0
- package/src/patch.mjs +93 -0
- package/src/rules.mjs +129 -0
- package/src/settings.mjs +61 -0
- package/src/verify.mjs +196 -0
- package/hooks/codex-patch-all +0 -75
- package/hooks/patch-all +0 -169
- package/hooks/reheal +0 -71
- package/scripts/apply-patches +0 -250
- package/scripts/codex-apply-patches +0 -108
- package/scripts/codex-find-incompatible +0 -94
- package/scripts/codex-verify +0 -393
- package/scripts/find-incompatible +0 -82
- package/scripts/fix-backslash-paths +0 -94
- package/scripts/fix-bare-commands +0 -147
- package/scripts/lib/plugins.sh +0 -132
- package/scripts/sync-version +0 -42
- package/scripts/verify +0 -334
package/src/verify.mjs
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// Health check for already-patched plugins, with optional auto-repair.
|
|
2
|
+
//
|
|
3
|
+
// The issue types below are a closed vocabulary, shared verbatim with
|
|
4
|
+
// skills/patch/SKILL.md. Adding one means updating both.
|
|
5
|
+
|
|
6
|
+
import { copyFileSync, existsSync, mkdirSync, readdirSync, statSync } from 'node:fs';
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import { hasBom, hasCrlf, readJson, readText, resolvePython, sanitize, writeText } from './env.mjs';
|
|
9
|
+
import { eachHook } from './hosts.mjs';
|
|
10
|
+
import { brokenWrapperTarget, disabledBody, passthroughBody, relPath, wrapperBody, wrapperName } from './rules.mjs';
|
|
11
|
+
|
|
12
|
+
const listFiles = (dir) => {
|
|
13
|
+
try {
|
|
14
|
+
return readdirSync(dir).map((n) => join(dir, n)).filter((f) => statSync(f).isFile());
|
|
15
|
+
} catch {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const baseName = (file) => file.split(/[\\/]/).pop();
|
|
21
|
+
|
|
22
|
+
const readOrNull = (file) => {
|
|
23
|
+
try { return readText(file); } catch { return null; }
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Recover the pre-patch command for a wrapper by replaying the naming rule over
|
|
27
|
+
// the backups. That is what lets a deleted wrapper be rebuilt exactly (CASE-16).
|
|
28
|
+
function originalCommandFor(host, hooksFiles, wrapper) {
|
|
29
|
+
for (const file of hooksFiles) {
|
|
30
|
+
const bak = readJson(file + host.bakSuffix);
|
|
31
|
+
if (!bak.ok) continue;
|
|
32
|
+
for (const { hook } of eachHook(bak.data)) {
|
|
33
|
+
const command = hook.command;
|
|
34
|
+
if (command && wrapperName(command, host.rootVar) === wrapper) return command;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Every file in one install whose encoding matters: the hook directories, plus
|
|
41
|
+
// anything a hooks.json points at, so a wrapper parked in a nonstandard subdir
|
|
42
|
+
// (scripts/) is not missed.
|
|
43
|
+
function encodingCandidates(host, install, dirFiles) {
|
|
44
|
+
const files = new Set([...install.hooksFiles, ...dirFiles]);
|
|
45
|
+
const re = new RegExp('\\$\\{?' + host.rootVar + '\\}?/([A-Za-z0-9_./-]+)', 'g');
|
|
46
|
+
for (const raw of install.hooksFiles.map(readOrNull)) {
|
|
47
|
+
for (const m of (raw || '').matchAll(re)) files.add(join(install.path, m[1]));
|
|
48
|
+
}
|
|
49
|
+
return [...files].filter((f) => !/\.bak$|\.tmp$/.test(f) && existsSync(f));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Directory-level checks, run once per install tree. A Codex plugin declares
|
|
53
|
+
// one hooks.json per event, so this is where re-scanning the same directory
|
|
54
|
+
// dozens of times would otherwise come from.
|
|
55
|
+
function checkTree(host, install, report, repair) {
|
|
56
|
+
const wrapperDir = join(install.path, host.wrapperDir);
|
|
57
|
+
const rel = (file) => file.replace(install.path, '').replace(/^[\\/]/, '').replace(/\\/g, '/');
|
|
58
|
+
const dirFiles = [...listFiles(join(install.path, 'hooks')), ...listFiles(wrapperDir)];
|
|
59
|
+
|
|
60
|
+
// ── Encoding (CASE-01/02/03) ──────────────────────────────────────
|
|
61
|
+
for (const file of encodingCandidates(host, install, dirFiles)) {
|
|
62
|
+
if (hasBom(file)) {
|
|
63
|
+
report('bom', rel(file));
|
|
64
|
+
repair(() => { sanitize(file); return 'stripped BOM from ' + rel(file); });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (const file of install.hooksFiles) {
|
|
68
|
+
if (!hasCrlf(file)) continue;
|
|
69
|
+
report('json_crlf', 'CRLF line endings');
|
|
70
|
+
repair(() => { sanitize(file); return 'normalized CRLF in ' + rel(file); });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ── Wrapper bodies ────────────────────────────────────────────────
|
|
74
|
+
for (const file of dirFiles) {
|
|
75
|
+
const name = baseName(file);
|
|
76
|
+
if (name === 'run-hook.cmd') continue;
|
|
77
|
+
const body = readOrNull(file);
|
|
78
|
+
if (body === null || !/^#!\/(bin\/bash|usr\/bin\/env bash)/.test(body)) continue;
|
|
79
|
+
|
|
80
|
+
// CASE-22: a bash wrapper that invokes an interpreter on its own filename
|
|
81
|
+
// recurses until the hook times out.
|
|
82
|
+
const self = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
83
|
+
if (new RegExp('(python3?|node)\\s+\\S*' + self).test(body)) {
|
|
84
|
+
report('recursive_wrapper', name + ' calls an interpreter on itself');
|
|
85
|
+
repair(() => {
|
|
86
|
+
writeText(file, disabledBody('bash wrapper called an interpreter on itself'));
|
|
87
|
+
return 'disabled recursive wrapper ' + name;
|
|
88
|
+
});
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// CASE-24: the wrapper execs a target that cannot exist.
|
|
93
|
+
const broken = brokenWrapperTarget(body, install.path, existsSync);
|
|
94
|
+
if (!broken) continue;
|
|
95
|
+
report('wrapper_broken', rel(file) + ' execs an invalid target: ' + broken.rel);
|
|
96
|
+
repair(() => {
|
|
97
|
+
const original = originalCommandFor(host, install.hooksFiles, name);
|
|
98
|
+
writeText(file, original ? wrapperBody(original, host.rootVar) : passthroughBody());
|
|
99
|
+
return 'repaired wrapper ' + rel(file);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Per-hooks.json checks: is it parseable, and does every wrapper it names exist?
|
|
105
|
+
function checkHooksFile(host, install, plugin, report, repair) {
|
|
106
|
+
const wrapperDir = join(install.path, host.wrapperDir);
|
|
107
|
+
const { ok, data, error } = readJson(plugin.hooksFile);
|
|
108
|
+
if (!ok) return report('json_invalid', error);
|
|
109
|
+
|
|
110
|
+
let needsRunHook = false;
|
|
111
|
+
for (const { hook } of eachHook(data)) {
|
|
112
|
+
// A python hook still running bare on a machine with no usable interpreter
|
|
113
|
+
// can never succeed; say so rather than failing silently (CASE-09/21).
|
|
114
|
+
if (/^python3?\s/.test(String(host.sourceCommand(hook) || '').trim()) && !resolvePython()) {
|
|
115
|
+
report('python3_stub', 'hook uses python but no working interpreter is installed');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const patched = String(host.patchedCommand(hook) || '').replace(/\\/g, '/');
|
|
119
|
+
if (!patched.includes(host.wrapperDir + '/run-hook.cmd')) continue;
|
|
120
|
+
needsRunHook = true;
|
|
121
|
+
|
|
122
|
+
const rest = patched.replace(/^.*run-hook\.cmd"?\s*/i, '');
|
|
123
|
+
const wrapper = (rest.match(/^"?([^"\s]+)/) || [])[1];
|
|
124
|
+
if (!wrapper) { report('wrapper_missing', 'wrapper name is not parseable'); continue; }
|
|
125
|
+
if (existsSync(join(wrapperDir, wrapper))) continue;
|
|
126
|
+
|
|
127
|
+
report('wrapper_missing', host.wrapperDir + '/' + wrapper + ' not found');
|
|
128
|
+
repair(() => {
|
|
129
|
+
// An older patch form forwarded the real target as a trailing argument;
|
|
130
|
+
// run-hook.cmd passes it through, so a passthrough body is correct there.
|
|
131
|
+
const forwarded = relPath(rest, host.rootVar);
|
|
132
|
+
const original = forwarded ? null : originalCommandFor(host, install.hooksFiles, wrapper);
|
|
133
|
+
if (!forwarded && !original) return 'could not rebuild ' + wrapper + ' (no usable backup)';
|
|
134
|
+
mkdirSync(wrapperDir, { recursive: true });
|
|
135
|
+
writeText(join(wrapperDir, wrapper), forwarded ? passthroughBody() : wrapperBody(original, host.rootVar));
|
|
136
|
+
return 'recreated missing wrapper ' + host.wrapperDir + '/' + wrapper;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (needsRunHook && !existsSync(join(wrapperDir, 'run-hook.cmd'))) {
|
|
141
|
+
report('cmd_missing', host.wrapperDir + '/run-hook.cmd not found');
|
|
142
|
+
repair((templateCmd) => {
|
|
143
|
+
if (!templateCmd) return null;
|
|
144
|
+
mkdirSync(wrapperDir, { recursive: true });
|
|
145
|
+
copyFileSync(templateCmd, join(wrapperDir, 'run-hook.cmd'));
|
|
146
|
+
return 'restored ' + host.wrapperDir + '/run-hook.cmd';
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function verify(host, { fix = false, templateCmd = null, plugins = host.listPlugins() } = {}) {
|
|
152
|
+
const issues = [];
|
|
153
|
+
const fixes = [];
|
|
154
|
+
|
|
155
|
+
for (const install of byInstall(plugins)) {
|
|
156
|
+
const record = (plugin) => ({
|
|
157
|
+
report: (type, detail) => issues.push({ plugin: plugin.id, path: install.path, type, detail }),
|
|
158
|
+
repair: (action) => {
|
|
159
|
+
if (!fix) return;
|
|
160
|
+
let done;
|
|
161
|
+
try { done = action(templateCmd); } catch (e) { done = 'repair failed: ' + e.message; }
|
|
162
|
+
if (done) fixes.push(done);
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
const guard = (plugin, run) => {
|
|
167
|
+
const { report, repair } = record(plugin);
|
|
168
|
+
try {
|
|
169
|
+
run(report, repair);
|
|
170
|
+
} catch (e) {
|
|
171
|
+
report('json_invalid', 'could not inspect plugin: ' + e.message);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
guard(install.plugins[0], (report, repair) => checkTree(host, install, report, repair));
|
|
176
|
+
for (const plugin of install.plugins) {
|
|
177
|
+
guard(plugin, (report, repair) => checkHooksFile(host, install, plugin, report, repair));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return { issues, fixes };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Codex declares one hooks.json per event, so many "plugins" share one install
|
|
185
|
+
// tree. Group them, or every directory-level check runs once per hooks file.
|
|
186
|
+
function byInstall(plugins) {
|
|
187
|
+
const trees = new Map();
|
|
188
|
+
for (const plugin of plugins) {
|
|
189
|
+
const tree = trees.get(plugin.installPath)
|
|
190
|
+
|| { path: plugin.installPath, plugins: [], hooksFiles: [] };
|
|
191
|
+
tree.plugins.push(plugin);
|
|
192
|
+
tree.hooksFiles.push(plugin.hooksFile);
|
|
193
|
+
trees.set(plugin.installPath, tree);
|
|
194
|
+
}
|
|
195
|
+
return [...trees.values()];
|
|
196
|
+
}
|
package/hooks/codex-patch-all
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
#!/bin/bash
|
|
2
|
-
# win-hooks: codex-patch-all
|
|
3
|
-
# Scans installed Codex plugins and adds Windows-specific commandWindows
|
|
4
|
-
# wrappers for hook commands that assume Unix dispatch.
|
|
5
|
-
|
|
6
|
-
set -euo pipefail
|
|
7
|
-
|
|
8
|
-
case "$(uname -s)" in
|
|
9
|
-
MINGW*|MSYS*|CYGWIN*|Windows_NT) ;;
|
|
10
|
-
*) exit 0 ;;
|
|
11
|
-
esac
|
|
12
|
-
|
|
13
|
-
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
14
|
-
PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
|
15
|
-
TEMPLATE_CMD="${PLUGIN_ROOT}/hooks/run-hook.cmd"
|
|
16
|
-
SCRIPTS_DIR="${PLUGIN_ROOT}/scripts"
|
|
17
|
-
HOME_DIR="${HOME:-$USERPROFILE}"
|
|
18
|
-
STATE_DIR="${HOME_DIR}/.codex/win-hooks"
|
|
19
|
-
LOG_FILE="${STATE_DIR}/last-run.log"
|
|
20
|
-
STAMP="${STATE_DIR}/codex.stamp"
|
|
21
|
-
PHASE="start"
|
|
22
|
-
PATCHED=0
|
|
23
|
-
VERIFY="skipped"
|
|
24
|
-
T0=$(date +%s)
|
|
25
|
-
|
|
26
|
-
mkdir -p "$STATE_DIR" 2>/dev/null || true
|
|
27
|
-
|
|
28
|
-
beat() {
|
|
29
|
-
local code=$? now
|
|
30
|
-
now=$(date +%s)
|
|
31
|
-
{
|
|
32
|
-
printf '%s\tphase=%s\texit=%d\tdur=%ss\tpatched=%s\tverify=%s\n' \
|
|
33
|
-
"$(date '+%Y-%m-%dT%H:%M:%S')" "$PHASE" "$code" "$(( now - ${T0:-now} ))" \
|
|
34
|
-
"${PATCHED:-0}" "${VERIFY:-?}" >> "$LOG_FILE"
|
|
35
|
-
tail -n 50 "$LOG_FILE" > "${LOG_FILE}.tmp" 2>/dev/null && mv "${LOG_FILE}.tmp" "$LOG_FILE"
|
|
36
|
-
} 2>/dev/null || true
|
|
37
|
-
}
|
|
38
|
-
trap beat EXIT
|
|
39
|
-
|
|
40
|
-
printf '%s\tphase=start\texit=-\tdur=-\tpatched=-\tverify=-\n' \
|
|
41
|
-
"$(date '+%Y-%m-%dT%H:%M:%S')" >> "$LOG_FILE" 2>/dev/null || true
|
|
42
|
-
|
|
43
|
-
if [[ "${1:-}" == "--changed-only" && -f "$STAMP" ]]; then
|
|
44
|
-
if [[ -z "$(bash "${SCRIPTS_DIR}/codex-find-incompatible" --newer "$STAMP" 2>/dev/null)" ]]; then
|
|
45
|
-
PHASE="done"
|
|
46
|
-
: > "$STAMP" 2>/dev/null || true
|
|
47
|
-
exit 0
|
|
48
|
-
fi
|
|
49
|
-
fi
|
|
50
|
-
|
|
51
|
-
PHASE="pipeline"
|
|
52
|
-
RESULT=$(bash "${SCRIPTS_DIR}/codex-find-incompatible" \
|
|
53
|
-
| bash "${SCRIPTS_DIR}/codex-apply-patches" "$TEMPLATE_CMD" 2>&1) || {
|
|
54
|
-
echo "win-hooks: Codex patch pipeline failed" >&2
|
|
55
|
-
echo "$RESULT" >&2
|
|
56
|
-
exit 1
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if echo "$RESULT" | grep -q "^PATCHED:"; then
|
|
60
|
-
PATCHED=$(echo "$RESULT" | grep -c "^PATCHED:")
|
|
61
|
-
echo "win-hooks: patched $PATCHED Codex plugin(s)" >&2
|
|
62
|
-
fi
|
|
63
|
-
|
|
64
|
-
PHASE="verify"
|
|
65
|
-
VERIFY_RC=0
|
|
66
|
-
VERIFY_OUT=$(bash "${SCRIPTS_DIR}/codex-verify" --fix 2>&1) || VERIFY_RC=$?
|
|
67
|
-
VERIFY=$(printf '%s\n' "$VERIFY_OUT" | grep "^win-hooks codex verify:" | tail -n1 | sed 's/^win-hooks codex verify: //') || true
|
|
68
|
-
VERIFY="${VERIFY:-rc=$VERIFY_RC}"
|
|
69
|
-
if [[ $VERIFY_RC -ne 0 ]]; then
|
|
70
|
-
echo "win-hooks: Codex verify found issues (auto-fixed where possible)" >&2
|
|
71
|
-
printf '%s\n' "$VERIFY_OUT" | grep -v "^win-hooks codex verify:" >&2
|
|
72
|
-
fi
|
|
73
|
-
|
|
74
|
-
: > "$STAMP" 2>/dev/null || true
|
|
75
|
-
PHASE="done"
|
package/hooks/patch-all
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
#!/bin/bash
|
|
2
|
-
# win-hooks: patch-all
|
|
3
|
-
# Scans all installed Claude Code plugins and patches incompatible hooks
|
|
4
|
-
# for Windows compatibility. Creates run-hook.cmd polyglot wrappers and
|
|
5
|
-
# extensionless bash wrapper scripts. Idempotent - safe to run repeatedly.
|
|
6
|
-
#
|
|
7
|
-
# Runs at SessionStart via the win-hooks plugin hook.
|
|
8
|
-
# Also callable manually: bash patch-all
|
|
9
|
-
|
|
10
|
-
set -euo pipefail
|
|
11
|
-
|
|
12
|
-
# ── Platform check ──────────────────────────────────────────────────
|
|
13
|
-
# Only patch on Windows. On Unix/macOS, hooks work natively.
|
|
14
|
-
case "$(uname -s)" in
|
|
15
|
-
MINGW*|MSYS*|CYGWIN*|Windows_NT)
|
|
16
|
-
;; # Windows - continue
|
|
17
|
-
*)
|
|
18
|
-
exit 0 ;; # Not Windows - skip
|
|
19
|
-
esac
|
|
20
|
-
|
|
21
|
-
# ── Heartbeat ───────────────────────────────────────────────────────
|
|
22
|
-
# Proof-of-run on disk so "did SessionStart actually heal this session?"
|
|
23
|
-
# is answerable — the happy path is otherwise completely silent (no stdout,
|
|
24
|
-
# no context, no marker), making a healthy run indistinguishable from one
|
|
25
|
-
# that never fired or was killed by the SessionStart timeout.
|
|
26
|
-
# Written ONLY to disk (never stdout) so it adds zero session-context noise,
|
|
27
|
-
# and rotated to the last 50 lines so it never grows unbounded (lean).
|
|
28
|
-
# • no log line at all ⇒ hook never dispatched
|
|
29
|
-
# • lone `phase=start` line ⇒ killed mid-run (e.g. timeout / hang)
|
|
30
|
-
# • a terminal `phase=...` ⇒ ran to that stage; `phase=done` = full success
|
|
31
|
-
WH_LOG="${HOME:-$USERPROFILE}/.claude/win-hooks/last-run.log"
|
|
32
|
-
WH_PHASE="start"
|
|
33
|
-
WH_T0=$(date +%s)
|
|
34
|
-
PATCHED=0
|
|
35
|
-
mkdir -p "$(dirname "$WH_LOG")" 2>/dev/null || true
|
|
36
|
-
_wh_beat() {
|
|
37
|
-
local code=$? now; now=$(date +%s)
|
|
38
|
-
{ printf '%s\tphase=%s\texit=%d\tdur=%ss\tplugins=%s\tnext_timeout=%s\tpatched=%s\tverify=%s\n' \
|
|
39
|
-
"$(date '+%Y-%m-%dT%H:%M:%S')" "$WH_PHASE" "$code" "$(( now - ${WH_T0:-now} ))" \
|
|
40
|
-
"${N:-?}" "${WH_TIMEOUT:-?}" "${PATCHED:-0}" "${WH_VERIFY:-?}" \
|
|
41
|
-
>> "$WH_LOG"
|
|
42
|
-
tail -n 50 "$WH_LOG" > "${WH_LOG}.tmp" 2>/dev/null && mv "${WH_LOG}.tmp" "$WH_LOG"
|
|
43
|
-
} 2>/dev/null || true
|
|
44
|
-
}
|
|
45
|
-
trap _wh_beat EXIT
|
|
46
|
-
# Early start marker — survives a hard timeout-kill that pre-empts the EXIT
|
|
47
|
-
# trap, so an aborted run stays distinguishable from one that never fired.
|
|
48
|
-
printf '%s\tphase=start\texit=-\tdur=-\tplugins=-\tnext_timeout=-\tpatched=-\tverify=-\n' \
|
|
49
|
-
"$(date '+%Y-%m-%dT%H:%M:%S')" >> "$WH_LOG" 2>/dev/null || true
|
|
50
|
-
|
|
51
|
-
# ── Paths ───────────────────────────────────────────────────────────
|
|
52
|
-
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
53
|
-
PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
|
54
|
-
TEMPLATE_CMD="${PLUGIN_ROOT}/hooks/run-hook.cmd"
|
|
55
|
-
SCRIPTS_DIR="${PLUGIN_ROOT}/scripts"
|
|
56
|
-
|
|
57
|
-
if [[ ! -f "$TEMPLATE_CMD" ]]; then
|
|
58
|
-
echo "win-hooks: template run-hook.cmd not found at ${TEMPLATE_CMD}" >&2
|
|
59
|
-
exit 1
|
|
60
|
-
fi
|
|
61
|
-
|
|
62
|
-
# ── Adaptive timeout self-tuning ────────────────────────────────────
|
|
63
|
-
# The SessionStart timeout lives in THIS plugin's hooks.json, which Claude
|
|
64
|
-
# Code reads BEFORE dispatching patch-all — so patch-all cannot set its own
|
|
65
|
-
# runtime limit, but it CAN size the NEXT session's limit to the local
|
|
66
|
-
# machine. Run cost is ~linear in installed-plugin count (per plugin: one
|
|
67
|
-
# `node` JSON-validate plus ~10 coreutils forks; on Windows, process spawn —
|
|
68
|
-
# inflated by Defender — dominates). So size the limit by count:
|
|
69
|
-
# timeout(ms) = clamp(OVERHEAD + PER_PLUGIN * N, FLOOR, CAP)
|
|
70
|
-
# Constants are conservative (cover a slow HDD+Defender laptop); over-
|
|
71
|
-
# provisioning a fast machine is harmless — the value is only a backstop.
|
|
72
|
-
# Written EARLY (before the pipeline) so even a timed-out run has already
|
|
73
|
-
# right-sized the next session; the self-edit is JSON-validated and applied
|
|
74
|
-
# only on a real change. Effect is next-session / after `/reload-plugins`.
|
|
75
|
-
# Full rationale (cost model, examples, cap-saturation): CLAUDE.md CASE-25.
|
|
76
|
-
OWN_HOOKS="${PLUGIN_ROOT}/hooks/hooks.json"
|
|
77
|
-
INSTALLED="${HOME:-$USERPROFILE}/.claude/plugins/installed_plugins.json"
|
|
78
|
-
if [[ -f "$OWN_HOOKS" && -f "$INSTALLED" ]] && command -v node >/dev/null 2>&1; then
|
|
79
|
-
N=$(grep -c '"installPath"' "$INSTALLED" 2>/dev/null || echo 0)
|
|
80
|
-
OVERHEAD=20000; PER_PLUGIN=4000; FLOOR=60000; CAP=600000
|
|
81
|
-
WANT=$(( OVERHEAD + PER_PLUGIN * N ))
|
|
82
|
-
(( WANT < FLOOR )) && WANT=$FLOOR
|
|
83
|
-
(( WANT > CAP )) && WANT=$CAP
|
|
84
|
-
CUR=$(grep -oE '"timeout":[[:space:]]*[0-9]+' "$OWN_HOOKS" | grep -oE '[0-9]+' | head -1) || CUR=""
|
|
85
|
-
if [[ -n "$CUR" && "$CUR" != "$WANT" ]]; then
|
|
86
|
-
TMP="${OWN_HOOKS}.tmp.$$"
|
|
87
|
-
if sed -E 's/("timeout":[[:space:]]*)[0-9]+/\1'"$WANT"'/' "$OWN_HOOKS" > "$TMP" 2>/dev/null \
|
|
88
|
-
&& node -e "JSON.parse(require('fs').readFileSync(process.argv[1],'utf8'))" "$TMP" 2>/dev/null; then
|
|
89
|
-
mv "$TMP" "$OWN_HOOKS" && WH_TIMEOUT="${CUR}->${WANT}"
|
|
90
|
-
else
|
|
91
|
-
rm -f "$TMP" 2>/dev/null || true
|
|
92
|
-
WH_TIMEOUT="keep:${CUR}"
|
|
93
|
-
fi
|
|
94
|
-
else
|
|
95
|
-
WH_TIMEOUT="${CUR:-?}"
|
|
96
|
-
fi
|
|
97
|
-
fi
|
|
98
|
-
|
|
99
|
-
# ── Best-effort: make `python3` resolve globally ────────────────────
|
|
100
|
-
# Primary handling for `python3 ${CLAUDE_PLUGIN_ROOT}/x.py` hooks is the
|
|
101
|
-
# wrapper apply-patches generates (resolves a real Python at patch time).
|
|
102
|
-
# This copy is a secondary nicety for bare `python3` that slips past the
|
|
103
|
-
# scanner: when `python3` can't actually run (missing or a dead Microsoft
|
|
104
|
-
# Store stub) but a real `python` can, copy it to python3.exe alongside.
|
|
105
|
-
# Detection is a functional probe (`-c ""`), not a path heuristic, so a
|
|
106
|
-
# legitimately Store-installed Python is not mistaken for the dead stub.
|
|
107
|
-
# Best-effort only: a system Python dir (C:\ProgramData, C:\Program Files)
|
|
108
|
-
# isn't writable without admin, so the copy may fail — the wrapper covers it.
|
|
109
|
-
if ! { command -v python3 >/dev/null 2>&1 && python3 -c "" >/dev/null 2>&1; } \
|
|
110
|
-
&& command -v python >/dev/null 2>&1 && python -c "" >/dev/null 2>&1; then
|
|
111
|
-
python_src="$(command -v python)"
|
|
112
|
-
[[ ! -f "$python_src" && -f "${python_src}.exe" ]] && python_src="${python_src}.exe"
|
|
113
|
-
python_dir="$(dirname "$python_src")"
|
|
114
|
-
if [[ -f "$python_src" ]]; then
|
|
115
|
-
[[ ! -f "${python_dir}/python3.exe" ]] && cp "$python_src" "${python_dir}/python3.exe" 2>/dev/null || true
|
|
116
|
-
[[ ! -f "${python_dir}/python3" ]] && cp "$python_src" "${python_dir}/python3" 2>/dev/null || true
|
|
117
|
-
fi
|
|
118
|
-
fi
|
|
119
|
-
|
|
120
|
-
# ── Run pipeline: find-incompatible | apply-patches ─────────────────
|
|
121
|
-
WH_PHASE="pipeline"
|
|
122
|
-
RESULT=$(bash "${SCRIPTS_DIR}/find-incompatible" \
|
|
123
|
-
| bash "${SCRIPTS_DIR}/apply-patches" "$TEMPLATE_CMD" 2>&1) || {
|
|
124
|
-
echo "win-hooks: patch pipeline failed" >&2
|
|
125
|
-
echo "$RESULT" >&2
|
|
126
|
-
exit 1
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
# Show summary to stderr (visible in hook output)
|
|
130
|
-
if echo "$RESULT" | grep -q "^PATCHED:"; then
|
|
131
|
-
PATCHED=$(echo "$RESULT" | grep -c "^PATCHED:")
|
|
132
|
-
echo "win-hooks: patched $PATCHED plugin(s)" >&2
|
|
133
|
-
fi
|
|
134
|
-
|
|
135
|
-
# ── Fix backslash paths in settings.json ────────────────────────────
|
|
136
|
-
# Convert C:\... to C:/... in hook commands to prevent path mangling
|
|
137
|
-
WH_PHASE="settings-fixers"
|
|
138
|
-
if [[ -f "${SCRIPTS_DIR}/fix-backslash-paths" ]]; then
|
|
139
|
-
bash "${SCRIPTS_DIR}/fix-backslash-paths" 2>&1 | grep -v "^hooks\." || true
|
|
140
|
-
fi
|
|
141
|
-
|
|
142
|
-
# ── Fix bare interpreter commands in settings.json ──────────────────
|
|
143
|
-
# Rewrite `node/python/python3/npx/npm <args>` to quoted absolute paths
|
|
144
|
-
# so hooks don't fail when cmd.exe's PATH lacks the interpreter at runtime.
|
|
145
|
-
if [[ -f "${SCRIPTS_DIR}/fix-bare-commands" ]]; then
|
|
146
|
-
bash "${SCRIPTS_DIR}/fix-bare-commands" 2>&1 | grep -v "^hooks\." || true
|
|
147
|
-
fi
|
|
148
|
-
|
|
149
|
-
# ── Post-patch verification ─────────────────────────────────────────
|
|
150
|
-
# Run verify --fix to detect and auto-repair any remaining issues
|
|
151
|
-
# (BOM, CRLF, broken JSON, missing wrappers). Captured unconditionally so
|
|
152
|
-
# the healthy summary lands in the heartbeat too — verify exits 0 when
|
|
153
|
-
# healthy, so the old `|| {...}` form silently dropped that line.
|
|
154
|
-
WH_VERIFY="skipped"
|
|
155
|
-
if [[ -x "${SCRIPTS_DIR}/verify" ]] || [[ -f "${SCRIPTS_DIR}/verify" ]]; then
|
|
156
|
-
WH_PHASE="verify"
|
|
157
|
-
# `|| VERIFY_RC=$?` keeps `set -e` from aborting here when verify exits
|
|
158
|
-
# non-zero (its exit code is the issue count), and captures that code.
|
|
159
|
-
VERIFY_RC=0
|
|
160
|
-
VERIFY_OUT=$(bash "${SCRIPTS_DIR}/verify" --fix 2>&1) || VERIFY_RC=$?
|
|
161
|
-
WH_VERIFY=$(printf '%s\n' "$VERIFY_OUT" | grep "^win-hooks verify:" | tail -n1 | sed 's/^win-hooks verify: //') || true
|
|
162
|
-
WH_VERIFY="${WH_VERIFY:-rc=$VERIFY_RC}"
|
|
163
|
-
if [[ $VERIFY_RC -ne 0 ]]; then
|
|
164
|
-
echo "win-hooks: verify found issues (auto-fixed where possible)" >&2
|
|
165
|
-
printf '%s\n' "$VERIFY_OUT" | grep -v "^win-hooks verify:" >&2
|
|
166
|
-
fi
|
|
167
|
-
fi
|
|
168
|
-
|
|
169
|
-
WH_PHASE="done"
|
package/hooks/reheal
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
#!/bin/bash
|
|
2
|
-
# win-hooks: reheal
|
|
3
|
-
# Mid-session re-heal guard (CASE-26). Runs at UserPromptSubmit.
|
|
4
|
-
#
|
|
5
|
-
# SessionStart's patch-all heals once per session, but a mid-session plugin
|
|
6
|
-
# update (a /plugin bump) reinstalls a plugin's hooks.json in its un-patched
|
|
7
|
-
# form, overwriting win-hooks' patch (CASE-13). SessionStart has already fired,
|
|
8
|
-
# so nothing re-heals until the next session or a manual /win-hooks:fix. This
|
|
9
|
-
# guard closes that gap: each prompt it does a near-zero mtime check, and re-runs
|
|
10
|
-
# patch-all only when a plugin's hooks changed since the last check AND an
|
|
11
|
-
# incompatible command is actually present.
|
|
12
|
-
#
|
|
13
|
-
# State: a single stamp file marks "checked up to here". Comparing plugin
|
|
14
|
-
# hooks.json mtimes against it keeps the hot path to a handful of stat calls
|
|
15
|
-
# (no node, no per-plugin forks), so a normal prompt pays almost nothing.
|
|
16
|
-
#
|
|
17
|
-
# The re-patch lands on DISK; the running session already loaded the old config,
|
|
18
|
-
# so /reload-plugins (or a new session) applies it (same caveat as CASE-13).
|
|
19
|
-
#
|
|
20
|
-
# Always exits 0 and never writes to stdout (a UserPromptSubmit hook's stdout is
|
|
21
|
-
# injected into the model's context); notices go to stderr, like patch-all.
|
|
22
|
-
|
|
23
|
-
set -uo pipefail
|
|
24
|
-
|
|
25
|
-
# Windows only; hooks run natively on Unix/macOS (mirrors patch-all).
|
|
26
|
-
case "$(uname -s)" in
|
|
27
|
-
MINGW*|MSYS*|CYGWIN*|Windows_NT) ;;
|
|
28
|
-
*) exit 0 ;;
|
|
29
|
-
esac
|
|
30
|
-
|
|
31
|
-
HOME_DIR="${HOME:-$USERPROFILE}"
|
|
32
|
-
STATE_DIR="${HOME_DIR}/.claude/win-hooks"
|
|
33
|
-
STAMP="${STATE_DIR}/reheal.stamp"
|
|
34
|
-
INSTALLED="${HOME_DIR}/.claude/plugins/installed_plugins.json"
|
|
35
|
-
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
36
|
-
|
|
37
|
-
# Fail-safe to no-op (hot UserPromptSubmit path — never break a prompt over a
|
|
38
|
-
# missing/corrupt shared lib): unlike the occasionally-invoked scripts, this
|
|
39
|
-
# is not a "loud" failure worth surfacing.
|
|
40
|
-
LIB="${SCRIPT_DIR}/../scripts/lib/plugins.sh"
|
|
41
|
-
[[ -f "$LIB" ]] || exit 0
|
|
42
|
-
source "$LIB" || exit 0
|
|
43
|
-
|
|
44
|
-
[[ -f "$INSTALLED" ]] || exit 0
|
|
45
|
-
|
|
46
|
-
# Installed plugins' hooks.json paths (win-hooks itself is skipped inside
|
|
47
|
-
# wh_each_plugin_hooksjson, because patch-all self-edits its own hooks.json
|
|
48
|
-
# timeout — CASE-25 — which would otherwise trip the check on every prompt).
|
|
49
|
-
mapfile -t HOOKS < <(wh_each_plugin_hooksjson "$INSTALLED" | cut -f3)
|
|
50
|
-
[[ ${#HOOKS[@]} -eq 0 ]] && exit 0
|
|
51
|
-
|
|
52
|
-
# Hot path: stamp exists and no plugin's hooks.json is newer than it, so nothing
|
|
53
|
-
# changed since the last check; bail in a few stat calls.
|
|
54
|
-
if [[ -f "$STAMP" ]] && [[ -z "$(find "${HOOKS[@]}" -newer "$STAMP" -print -quit 2>/dev/null)" ]]; then
|
|
55
|
-
exit 0
|
|
56
|
-
fi
|
|
57
|
-
|
|
58
|
-
# A hooks.json changed since the last check (or this is the first run). Re-patch
|
|
59
|
-
# only if an incompatible command is actually present; a compatible update needs
|
|
60
|
-
# no work.
|
|
61
|
-
if [[ -n "$(bash "${SCRIPT_DIR}/../scripts/find-incompatible" 2>/dev/null)" ]]; then
|
|
62
|
-
bash "${SCRIPT_DIR}/patch-all" >&2 || true
|
|
63
|
-
echo "win-hooks: a plugin update overwrote a patch, so it was re-patched. Run /reload-plugins to apply it to this session." >&2
|
|
64
|
-
fi
|
|
65
|
-
|
|
66
|
-
# Mark "checked up to now" so unchanged prompts stay on the hot path. Written
|
|
67
|
-
# LAST so it post-dates any hooks.json that patch-all just wrote.
|
|
68
|
-
mkdir -p "$STATE_DIR" 2>/dev/null || true
|
|
69
|
-
: > "$STAMP" 2>/dev/null || true
|
|
70
|
-
|
|
71
|
-
exit 0
|