@lilmgenius/win-hooks 1.10.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/LICENSE +21 -0
- package/README.md +173 -0
- package/bin/win-hooks.mjs +82 -0
- package/hooks/codex-hooks.json +31 -0
- package/hooks/codex-patch-all +75 -0
- package/hooks/hooks.json +26 -0
- package/hooks/patch-all +169 -0
- package/hooks/reheal +71 -0
- package/hooks/run-hook.cmd +53 -0
- package/package.json +48 -0
- package/scripts/apply-patches +250 -0
- package/scripts/codex-apply-patches +108 -0
- package/scripts/codex-find-incompatible +94 -0
- package/scripts/codex-verify +393 -0
- package/scripts/find-incompatible +82 -0
- package/scripts/fix-backslash-paths +94 -0
- package/scripts/fix-bare-commands +147 -0
- package/scripts/lib/plugins.sh +132 -0
- package/scripts/sync-version +42 -0
- package/scripts/verify +334 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LilMGenius
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# win-hooks
|
|
4
|
+
|
|
5
|
+
### *"Linux? Nah. WinUX!"*
|
|
6
|
+
|
|
7
|
+
**Don't let Windows kill your vibe coding flow.**
|
|
8
|
+
|
|
9
|
+
Every Claude Code / Codex plugin assumes you're on macOS or Linux.<br>
|
|
10
|
+
On Windows, your sessions start with a wall of red hook errors.<br>
|
|
11
|
+
**win-hooks fixes that. Automatically. Every session.**
|
|
12
|
+
|
|
13
|
+
[](LICENSE)
|
|
14
|
+
[](https://www.microsoft.com/windows)
|
|
15
|
+
[](https://docs.anthropic.com/en/docs/claude-code)
|
|
16
|
+
[](https://developers.openai.com/codex)
|
|
17
|
+
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## The Problem
|
|
23
|
+
|
|
24
|
+
You install a Claude Code / Codex plugin, start a session, and see errors like:
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
SessionStart hook error: /bin/bash: command not found
|
|
28
|
+
PreToolUse hook error: scripts/check.sh: No such file or directory
|
|
29
|
+
PostToolUse hook error: semgrep: command not found
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The plugin may be fine on macOS or Linux, but its hooks assume Unix tools, `.sh` scripts, or shell paths that Windows does not run directly.
|
|
33
|
+
|
|
34
|
+
## Quick Start
|
|
35
|
+
|
|
36
|
+
### Claude Code
|
|
37
|
+
|
|
38
|
+
Paste once:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
claude plugin marketplace add LilMGenius/win-hooks && claude plugin install win-hooks
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
That is the setup. No config, no flags, no manual patching.
|
|
45
|
+
|
|
46
|
+
### Codex
|
|
47
|
+
|
|
48
|
+
Paste once:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
codex plugin marketplace add LilMGenius/win-hooks && codex plugin add win-hooks@win-hooks
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Codex uses its native `commandWindows` field, so the original cross-platform `command` stays intact.
|
|
55
|
+
|
|
56
|
+
### CLI (npm)
|
|
57
|
+
|
|
58
|
+
Want a one-shot fix, or to run it in CI, without installing the plugin? Run it directly:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
npx @lilmgenius/win-hooks # repair installed Claude Code AND Codex plugin hooks
|
|
62
|
+
npx @lilmgenius/win-hooks status # show the last repair result
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Windows only. It uses the same runtimes as the plugin — Git for Windows (Bash) and Node.js. Installed as a plugin, win-hooks also runs automatically every session; the CLI is for when you'd rather trigger it yourself.
|
|
66
|
+
|
|
67
|
+
## What win-hooks Fixes
|
|
68
|
+
|
|
69
|
+
win-hooks scans your installed Claude Code / Codex plugins and repairs Windows-incompatible hook commands before they keep breaking your session.
|
|
70
|
+
|
|
71
|
+
It handles the common failure modes:
|
|
72
|
+
|
|
73
|
+
- `.sh` scripts called directly from Windows
|
|
74
|
+
- missing Unix-only commands such as `semgrep` or `shellcheck`
|
|
75
|
+
- bare `node`, `python`, `python3`, `npx`, or `npm` commands that work in Git Bash but fail through Windows hook dispatch
|
|
76
|
+
- Windows backslash paths inside hook commands
|
|
77
|
+
- UTF-8 BOM, CRLF, invalid JSON, missing wrappers, and broken wrapper files
|
|
78
|
+
- `python3` hooks blocked by the Microsoft Store Python alias
|
|
79
|
+
|
|
80
|
+
Original plugin files are backed up where hooks are patched, and already-compatible plugins are skipped.
|
|
81
|
+
|
|
82
|
+
## How It Stays Fixed
|
|
83
|
+
|
|
84
|
+
win-hooks runs automatically at session start.
|
|
85
|
+
|
|
86
|
+
Claude Code pipeline:
|
|
87
|
+
|
|
88
|
+
```mermaid
|
|
89
|
+
flowchart LR
|
|
90
|
+
A[scan plugins] --> B[patch hooks.json] --> C[normalize settings.json] --> D["verify & auto-repair"]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Codex pipeline:
|
|
94
|
+
|
|
95
|
+
```mermaid
|
|
96
|
+
flowchart LR
|
|
97
|
+
A[scan plugins] --> B["add commandWindows wrappers"] --> C["verify & auto-repair"]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Plugin updates are covered too. If an update replaces a repaired hook with a fresh broken one, win-hooks re-patches it at the next session start.
|
|
101
|
+
|
|
102
|
+
It also checks again on your next prompt after a plugin update. Use `/reload-plugins` when you want the repaired hook config loaded without starting a new session.
|
|
103
|
+
|
|
104
|
+
## Check Status
|
|
105
|
+
|
|
106
|
+
The normal path is silent, so win-hooks writes a small heartbeat log:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
tail -n 5 ~/.claude/win-hooks/last-run.log
|
|
110
|
+
tail -n 5 ~/.codex/win-hooks/last-run.log
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
- `phase=done` means the self-heal completed.
|
|
114
|
+
- a lone `phase=start` means the run was cut off mid-way and should retry next session.
|
|
115
|
+
- no file means it has not dispatched yet.
|
|
116
|
+
|
|
117
|
+
You can also run:
|
|
118
|
+
|
|
119
|
+
```text
|
|
120
|
+
/win-hooks:status
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Commands
|
|
124
|
+
|
|
125
|
+
| Command | Description |
|
|
126
|
+
|---|---|
|
|
127
|
+
| `/win-hooks:status` | Show the current compatibility status of installed plugin hooks. |
|
|
128
|
+
| `/win-hooks:fix` | Manually run the repair pipeline. Normally you should not need this. |
|
|
129
|
+
|
|
130
|
+
## Requirements
|
|
131
|
+
|
|
132
|
+
- Windows 10/11
|
|
133
|
+
- Claude Code / Codex
|
|
134
|
+
- Git for Windows
|
|
135
|
+
- Node.js available on PATH
|
|
136
|
+
|
|
137
|
+
Claude Code already provides the Node.js runtime used by the Claude-side patch/verify flow. The Codex flow also uses `node` in `scripts/codex-find-incompatible` and `scripts/codex-verify`. Git for Windows provides the Bash runtime used to execute repaired hooks.
|
|
138
|
+
|
|
139
|
+
## Technical Notes
|
|
140
|
+
|
|
141
|
+
For Claude Code, win-hooks creates a dedicated `_hooks/` directory inside each patched plugin. The original hook target stays untouched, and `hooks.json` points at a Windows-safe wrapper.
|
|
142
|
+
|
|
143
|
+
```
|
|
144
|
+
plugin/
|
|
145
|
+
├── hooks/
|
|
146
|
+
│ ├── hooks.json
|
|
147
|
+
│ └── hooks.json.bak
|
|
148
|
+
├── _hooks/
|
|
149
|
+
│ ├── run-hook.cmd
|
|
150
|
+
│ └── <wrapper>
|
|
151
|
+
└── scripts/
|
|
152
|
+
└── setup.sh
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The wrapper entry point is a polyglot `.cmd` file: Windows runs the batch portion, while Bash can run the shell portion. That keeps one repaired hook path usable across both Windows dispatch and Bash execution.
|
|
156
|
+
|
|
157
|
+
For Codex, win-hooks creates `_codex_hooks/` and adds `commandWindows` to incompatible hook entries:
|
|
158
|
+
|
|
159
|
+
```
|
|
160
|
+
plugin/
|
|
161
|
+
├── .codex-plugin/
|
|
162
|
+
│ └── plugin.json
|
|
163
|
+
├── hooks/
|
|
164
|
+
│ ├── hooks.json
|
|
165
|
+
│ └── hooks.json.codex-win-hooks.bak
|
|
166
|
+
└── _codex_hooks/
|
|
167
|
+
├── run-hook.cmd
|
|
168
|
+
└── <wrapper>
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Codex keeps using the original `command` on macOS/Linux. On Windows it uses the generated `commandWindows` wrapper.
|
|
172
|
+
|
|
173
|
+
Contributors can read [`CLAUDE.md`](CLAUDE.md) for the internal scanner, patcher, verifier, and case notes.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// win-hooks CLI — a thin shim over the same bash pipeline the plugin runs.
|
|
3
|
+
//
|
|
4
|
+
// `npx win-hooks` scan + repair installed Claude Code AND Codex plugin hooks
|
|
5
|
+
// `npx win-hooks claude` Claude Code plugins only (hooks/patch-all)
|
|
6
|
+
// `npx win-hooks codex` Codex plugins only (hooks/codex-patch-all)
|
|
7
|
+
// `npx win-hooks status` tail the heartbeat logs
|
|
8
|
+
//
|
|
9
|
+
// win-hooks patches files on disk; if you install it as a plugin it also runs
|
|
10
|
+
// automatically at session start. This CLI is for a one-shot fix, CI, or when
|
|
11
|
+
// you'd rather not run the SessionStart hook.
|
|
12
|
+
|
|
13
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
14
|
+
import { execFileSync, spawnSync } from 'node:child_process';
|
|
15
|
+
import { fileURLToPath } from 'node:url';
|
|
16
|
+
import { dirname, join } from 'node:path';
|
|
17
|
+
import { homedir } from 'node:os';
|
|
18
|
+
|
|
19
|
+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
20
|
+
|
|
21
|
+
if (process.platform !== 'win32') {
|
|
22
|
+
// Hooks run natively on macOS/Linux — nothing to repair. Not an error.
|
|
23
|
+
console.error('win-hooks only applies on Windows; your plugin hooks run natively here. Nothing to do.');
|
|
24
|
+
process.exit(0);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Resolve a real bash.exe, mirroring hooks/run-hook.cmd's search order.
|
|
28
|
+
function resolveBash() {
|
|
29
|
+
const candidates = [];
|
|
30
|
+
if (process.env.WH_BASH_EXE) candidates.push(process.env.WH_BASH_EXE);
|
|
31
|
+
candidates.push(
|
|
32
|
+
'C:\\Program Files\\Git\\bin\\bash.exe',
|
|
33
|
+
'C:\\Program Files (x86)\\Git\\bin\\bash.exe',
|
|
34
|
+
);
|
|
35
|
+
for (const c of candidates) if (existsSync(c)) return c;
|
|
36
|
+
try {
|
|
37
|
+
const out = execFileSync('where', ['bash'], { encoding: 'utf8' });
|
|
38
|
+
const first = out.split(/\r?\n/).map((l) => l.trim()).find(Boolean);
|
|
39
|
+
if (first && existsSync(first)) return first;
|
|
40
|
+
} catch { /* fall through */ }
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function runBash(bash, scriptRel) {
|
|
45
|
+
const res = spawnSync(bash, [join(ROOT, scriptRel)], { stdio: 'inherit', env: process.env });
|
|
46
|
+
if (res.error) {
|
|
47
|
+
console.error(`win-hooks: failed to run ${scriptRel}: ${res.error.message}`);
|
|
48
|
+
return 1;
|
|
49
|
+
}
|
|
50
|
+
return res.status ?? 0;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function status() {
|
|
54
|
+
const home = process.env.USERPROFILE || homedir();
|
|
55
|
+
let shown = false;
|
|
56
|
+
for (const log of [join(home, '.claude', 'win-hooks', 'last-run.log'), join(home, '.codex', 'win-hooks', 'last-run.log')]) {
|
|
57
|
+
if (!existsSync(log)) continue;
|
|
58
|
+
shown = true;
|
|
59
|
+
console.log(`\n# ${log}`);
|
|
60
|
+
console.log(readFileSync(log, 'utf8').trim().split(/\r?\n/).slice(-5).join('\n'));
|
|
61
|
+
}
|
|
62
|
+
if (!shown) console.log('win-hooks: no heartbeat yet — run `npx win-hooks` or start a session.');
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const cmd = (process.argv[2] || 'fix').toLowerCase();
|
|
67
|
+
if (cmd === 'status') process.exit(status());
|
|
68
|
+
|
|
69
|
+
const bash = resolveBash();
|
|
70
|
+
if (!bash) {
|
|
71
|
+
console.error('win-hooks: could not find bash.exe. Install Git for Windows, or set WH_BASH_EXE to your bash path.');
|
|
72
|
+
process.exit(1);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let rc = 0;
|
|
76
|
+
if (cmd === 'fix' || cmd === 'claude') rc = runBash(bash, 'hooks/patch-all') || rc;
|
|
77
|
+
if (cmd === 'fix' || cmd === 'codex') rc = runBash(bash, 'hooks/codex-patch-all') || rc;
|
|
78
|
+
if (!['fix', 'claude', 'codex'].includes(cmd)) {
|
|
79
|
+
console.error(`win-hooks: unknown command "${cmd}". Use: (none) | claude | codex | status`);
|
|
80
|
+
process.exit(2);
|
|
81
|
+
}
|
|
82
|
+
process.exit(rc);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"description": "win-hooks: Auto-patches Codex plugin hooks for Windows compatibility",
|
|
3
|
+
"hooks": {
|
|
4
|
+
"SessionStart": [
|
|
5
|
+
{
|
|
6
|
+
"hooks": [
|
|
7
|
+
{
|
|
8
|
+
"type": "command",
|
|
9
|
+
"command": "bash \"${PLUGIN_ROOT}/hooks/codex-patch-all\"",
|
|
10
|
+
"commandWindows": "\"${PLUGIN_ROOT}\\hooks\\run-hook.cmd\" codex-patch-all",
|
|
11
|
+
"timeout": 60,
|
|
12
|
+
"statusMessage": "win-hooks: Checking Codex plugin hooks"
|
|
13
|
+
}
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
],
|
|
17
|
+
"UserPromptSubmit": [
|
|
18
|
+
{
|
|
19
|
+
"hooks": [
|
|
20
|
+
{
|
|
21
|
+
"type": "command",
|
|
22
|
+
"command": "bash \"${PLUGIN_ROOT}/hooks/codex-patch-all\" --changed-only",
|
|
23
|
+
"commandWindows": "\"${PLUGIN_ROOT}\\hooks\\run-hook.cmd\" codex-patch-all --changed-only",
|
|
24
|
+
"timeout": 20,
|
|
25
|
+
"statusMessage": "win-hooks: Checking changed Codex plugin hooks"
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
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/hooks.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"description": "win-hooks: Auto-patches Claude Code plugin hooks for Windows compatibility",
|
|
3
|
+
"hooks": {
|
|
4
|
+
"SessionStart": [
|
|
5
|
+
{
|
|
6
|
+
"hooks": [
|
|
7
|
+
{
|
|
8
|
+
"type": "command",
|
|
9
|
+
"command": "\"${CLAUDE_PLUGIN_ROOT}/hooks/run-hook.cmd\" patch-all",
|
|
10
|
+
"timeout": 60000
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
}
|
|
14
|
+
],
|
|
15
|
+
"UserPromptSubmit": [
|
|
16
|
+
{
|
|
17
|
+
"hooks": [
|
|
18
|
+
{
|
|
19
|
+
"type": "command",
|
|
20
|
+
"command": "\"${CLAUDE_PLUGIN_ROOT}/hooks/run-hook.cmd\" reheal"
|
|
21
|
+
}
|
|
22
|
+
]
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
}
|
package/hooks/patch-all
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
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
|