@lilmgenius/win-hooks 1.12.0 → 1.13.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 CHANGED
@@ -63,7 +63,7 @@ None of that is your fault. Almost every plugin is written and tested on macOS o
63
63
 
64
64
  win-hooks repairs your plugins at every session start, including ones you install later and ones that break again after an update. Repairs are written next to the original file, never on top of it, and anything already working is left alone.
65
65
 
66
- A healthy run is silent. To look anyway:
66
+ At the start of a session it says so in one line, so you can tell a clean run from a hook that never fired. To look closer:
67
67
 
68
68
  | Command | What it does |
69
69
  |---|---|
package/bin/win-hooks.mjs CHANGED
@@ -2,23 +2,23 @@
2
2
  // win-hooks CLI - the single entry point. The plugin's own SessionStart and
3
3
  // UserPromptSubmit hooks land here too, via hooks/run-hook.cmd -> hooks/win-hooks.
4
4
  //
5
- // win-hooks repair Claude Code and Codex plugin hooks
6
- // win-hooks heal [host] repair one host (claude | codex)
7
- // win-hooks status [host] report without changing anything
8
- // win-hooks patch [host] report, repair what is not healthy, prove the result
5
+ // heal [host] silent repair; --changed-only skips when no plugin's hooks
6
+ // changed since the last run (the near-free per-prompt guard);
7
+ // --announce speaks the result into the session (CASE-32)
8
+ // status [host] report, change nothing
9
+ // patch [host] report, repair what is not healthy, prove the result
9
10
  //
10
- // --changed-only skip when no plugin's hooks changed since the last run
11
- // (the per-prompt guard; near-free on the hot path)
12
- //
13
- // Progress goes to stderr, never stdout: these hooks run under Claude Code,
14
- // whose UserPromptSubmit stdout is injected into the model's context. `status`
15
- // and `patch` are the exception - a person asked for their output.
11
+ // Progress goes to stderr, never stdout: a UserPromptSubmit hook's stdout is
12
+ // injected into the model's context. `status`, `patch`, and an announced heal
13
+ // are the exceptions - a person asked for the first two, and the third is a
14
+ // session-start hook, whose stdout both hosts inject into the session as it
15
+ // stands (CASE-32).
16
16
 
17
17
  import { isWindows } from '../src/env.mjs';
18
18
  import { HOSTS } from '../src/hosts.mjs';
19
19
  import { heal, inspect } from '../src/heal.mjs';
20
20
 
21
- const USAGE = 'usage: win-hooks [heal|status|patch] [claude|codex] [--changed-only]';
21
+ const USAGE = 'usage: win-hooks [heal|status|patch] [claude|codex] [--changed-only] [--announce]';
22
22
 
23
23
  const args = process.argv.slice(2);
24
24
  const flags = new Set(args.filter((a) => a.startsWith('--')));
@@ -42,16 +42,30 @@ if (command !== 'heal') {
42
42
  process.exit(0);
43
43
  }
44
44
 
45
+ const announce = flags.has('--announce');
46
+
45
47
  for (const id of hostIds) {
46
48
  try {
47
49
  const result = heal(id, { changedOnly: flags.has('--changed-only') });
48
- if (result) for (const line of summarize(result)) console.error(line);
50
+ if (!result) continue;
51
+ const lines = summarize(result);
52
+ if (announce) lines.unshift(announcement(result));
53
+ for (const line of lines) (announce ? console.log : console.error)(line);
49
54
  } catch (e) {
50
55
  // One broken host must not take the session down with it.
51
56
  console.error('win-hooks: ' + HOSTS[id].label + ' repair failed - ' + e.message);
52
57
  }
53
58
  }
54
59
 
60
+ // One line a person can read at session start. A silent happy path is
61
+ // indistinguishable from a hook the host never dispatched, and the run log
62
+ // cannot separate them either, since a manual repair writes the same line
63
+ // (CASE-32). Said once per session, never per prompt.
64
+ function announcement({ host, scanned, patched, issues, fixes }) {
65
+ return 'win-hooks: ' + host.label + ' - ' + scanned + ' hook file(s) checked, '
66
+ + patched.length + ' repaired, ' + Math.max(issues.length - fixes.length, 0) + ' issue(s) open';
67
+ }
68
+
55
69
  function summarize({ host, patched, failed, settings, issues, fixes }) {
56
70
  const lines = [];
57
71
  for (const p of patched) lines.push('win-hooks: patched ' + p.plugin.id + ' (' + p.wrappers.join(', ') + ')');
@@ -66,10 +80,9 @@ function summarize({ host, patched, failed, settings, issues, fixes }) {
66
80
  return lines;
67
81
  }
68
82
 
69
- // Report, repair what is not healthy, then prove the result - one verb,
70
- // because nobody inspects hooks for their own sake. A healthy host leaves
71
- // nothing to decide, and a broken one was always going to be repaired, so
72
- // splitting the two only asks the user which one they needed.
83
+ // One verb, because nobody inspects hooks for their own sake: a healthy host
84
+ // leaves nothing to decide and a broken one was always going to be repaired,
85
+ // so splitting the two only asks the user which one they needed (CASE-30).
73
86
  function patchHost(id) {
74
87
  const before = inspect(id);
75
88
  if (healthy(before)) return report(before) + heartbeat(before);
@@ -6,8 +6,8 @@
6
6
  "hooks": [
7
7
  {
8
8
  "type": "command",
9
- "command": "bash \"${PLUGIN_ROOT}/hooks/win-hooks\" heal codex",
10
- "commandWindows": "\"${PLUGIN_ROOT}\\hooks\\run-hook.cmd\" win-hooks heal codex",
9
+ "command": "bash \"${PLUGIN_ROOT}/hooks/win-hooks\" heal codex --announce",
10
+ "commandWindows": "cmd /c \"${PLUGIN_ROOT}\\hooks\\run-hook.cmd\" win-hooks heal codex --announce",
11
11
  "timeout": 60,
12
12
  "statusMessage": "win-hooks: checking Codex plugin hooks"
13
13
  }
@@ -20,7 +20,7 @@
20
20
  {
21
21
  "type": "command",
22
22
  "command": "bash \"${PLUGIN_ROOT}/hooks/win-hooks\" heal codex --changed-only",
23
- "commandWindows": "\"${PLUGIN_ROOT}\\hooks\\run-hook.cmd\" win-hooks heal codex --changed-only",
23
+ "commandWindows": "cmd /c \"${PLUGIN_ROOT}\\hooks\\run-hook.cmd\" win-hooks heal codex --changed-only",
24
24
  "timeout": 20,
25
25
  "statusMessage": "win-hooks: checking changed Codex plugin hooks"
26
26
  }
package/hooks/hooks.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "hooks": [
7
7
  {
8
8
  "type": "command",
9
- "command": "\"${CLAUDE_PLUGIN_ROOT}/hooks/run-hook.cmd\" win-hooks heal",
9
+ "command": "\"${CLAUDE_PLUGIN_ROOT}/hooks/run-hook.cmd\" win-hooks heal --announce",
10
10
  "timeout": 60
11
11
  }
12
12
  ]
@@ -1,13 +1,11 @@
1
1
  : << 'CMDBLOCK'
2
2
  @echo off
3
- REM The entry point every patched hook is dispatched through - a single file
4
- REM that is valid batch AND valid bash at once. cmd.exe runs the batch block
5
- REM below; bash treats it as a quoted here-doc and falls through to the tail.
6
- REM
7
- REM Why a polyglot: Claude Code and Codex each dispatch hooks differently, and
8
- REM the wrapper has to be reachable from both without shipping two files.
9
- REM Wrapper names are extensionless because Claude Code prepends "bash" to any
10
- REM command containing .sh on Windows, which would undo the patch (CASE-07).
3
+ REM The entry point every patched hook is dispatched through: one file that is
4
+ REM valid batch AND valid bash at once, so Claude Code and Codex can each
5
+ REM dispatch it their own way. cmd.exe runs the batch block below; bash treats
6
+ REM it as a quoted here-doc and falls through to the tail. Wrapper names are
7
+ REM extensionless because Claude Code prepends "bash" to any command containing
8
+ REM .sh, which would undo the patch (CASE-07).
11
9
  REM
12
10
  REM Usage: run-hook.cmd <script-name> [args...]
13
11
 
@@ -19,8 +17,7 @@ if "%~1"=="" (
19
17
  set "HOOK_DIR=%~dp0"
20
18
  set "WH_BASH="
21
19
 
22
- REM Explicit override first: set WH_BASH_EXE when bash lives somewhere this
23
- REM does not already look, e.g. a portable, scoop, or winget Git install.
20
+ REM Explicit override first, for a portable, scoop, or winget Git install.
24
21
  if defined WH_BASH_EXE if exist "%WH_BASH_EXE%" set "WH_BASH=%WH_BASH_EXE%"
25
22
 
26
23
  REM Then Git for Windows, which is where it almost always is.
@@ -29,19 +26,17 @@ if not defined WH_BASH if exist "C:\Program Files (x86)\Git\bin\bash.exe" set "W
29
26
 
30
27
  if defined WH_BASH goto :run
31
28
 
32
- REM Finally PATH (MSYS2, Cygwin, a nonstandard install). Resolved by the FOR
33
- REM path-search modifier below rather than by `where`, which costs a subprocess.
34
- REM That modifier must not be named in a REM: cmd.exe expands it there too, and
35
- REM a comment mentioning it is a syntax error.
29
+ REM Finally PATH (MSYS2, Cygwin, a nonstandard install), resolved by the FOR
30
+ REM path-search modifier below - `where` costs a subprocess. Never name that
31
+ REM modifier in a REM: cmd.exe expands it there too and the comment breaks.
36
32
  for %%I in (bash.exe) do set "WH_BASH=%%~$PATH:I"
37
33
  if not defined WH_BASH goto :nobash
38
34
 
39
- REM PATH bash is often C:\Windows\System32\bash.exe, the WSL launcher, which
40
- REM cannot open Windows paths and still exits 0 - it would swallow every hook
41
- REM forever. So the candidate has to prove it can see the script it is about to
42
- REM run (CASE-29). Only PATH is probed; the paths above are known-good. The
43
- REM path goes through the environment because bash would receive it as $0 and
44
- REM WSL's launcher does not forward $0 the way a real bash does.
35
+ REM PATH bash is often C:\Windows\System32\bash.exe, the WSL launcher: it
36
+ REM cannot open Windows paths yet still exits 0, so it would swallow every hook
37
+ REM forever. A PATH candidate must prove it can see the script it is about to
38
+ REM run (CASE-29); the paths above are known-good. The probe travels through
39
+ REM the environment because WSL does not forward $0 the way a real bash does.
45
40
  set "WH_PROBE=%HOOK_DIR%%~1"
46
41
  "%WH_BASH%" -c "test -f \"$WH_PROBE\"" >nul 2>&1
47
42
  if errorlevel 1 goto :nobash
package/hooks/win-hooks CHANGED
@@ -1,9 +1,7 @@
1
1
  #!/bin/bash
2
- # The bridge from a hook dispatch to the engine.
3
- #
4
- # run-hook.cmd already knows how to find bash on Windows, so win-hooks reaches
5
- # its own engine exactly the way a patched plugin reaches its wrapper - if this
6
- # path works here, it works there.
2
+ # The bridge from a hook dispatch to the engine. win-hooks reaches its own
3
+ # engine exactly the way a patched plugin reaches its wrapper, so if this path
4
+ # works here it works there.
7
5
  #
8
6
  # Fail-safe: a missing runtime means no repair, never a broken session.
9
7
  command -v node >/dev/null 2>&1 || exit 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lilmgenius/win-hooks",
3
- "version": "1.12.0",
3
+ "version": "1.13.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/heal.mjs CHANGED
@@ -1,11 +1,12 @@
1
1
  // Orchestration: the one code path every surface goes through - the plugin's
2
2
  // own hooks, the CLI, and the slash commands.
3
3
  //
4
- // Two rules govern this file. Nothing is ever written to stdout from a hook: a
5
- // UserPromptSubmit hook's stdout is injected into the model's context, so
6
- // progress chatter would silently poison every prompt. And an environmental
7
- // failure degrades to a no-op rather than breaking the session it exists to
8
- // protect.
4
+ // Two rules govern this file. It prints nothing itself: a UserPromptSubmit
5
+ // hook's stdout is injected into the model's context, so progress chatter
6
+ // would silently poison every prompt. The one place that is a feature rather
7
+ // than a hazard is session start, and only the CLI decides that, from
8
+ // --announce (CASE-32). And an environmental failure degrades to a no-op
9
+ // rather than breaking the session it exists to protect.
9
10
 
10
11
  import { appendFileSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
11
12
  import { join, dirname } from 'node:path';
@@ -99,7 +100,7 @@ export function heal(hostId, { changedOnly = false } = {}) {
99
100
  issues: issues.length,
100
101
  });
101
102
 
102
- return { host, patched, failed, settings, issues, fixes };
103
+ return { host, scanned: plugins.length, patched, failed, settings, issues, fixes };
103
104
  }
104
105
 
105
106
  // Report without touching anything.
package/src/hosts.mjs CHANGED
@@ -10,6 +10,7 @@ import { spawnSync } from 'node:child_process';
10
10
  import { existsSync } from 'node:fs';
11
11
  import { join, resolve } from 'node:path';
12
12
  import { HOME, readJson, toPosix } from './env.mjs';
13
+ import { DISPATCH_PREFIX, opensWithQuotedPath } from './rules.mjs';
13
14
 
14
15
  // Both hosts use the same hooks.json shape:
15
16
  // { hooks: { <Event>: [ { hooks: [ { type: "command", command } ] } ] } }
@@ -66,6 +67,11 @@ const claude = {
66
67
  sourceCommand: (hook) => hook.command,
67
68
  patchedCommand: (hook) => hook.command,
68
69
  applyPatch: (hook, ref) => { hook.command = ref; },
70
+ // The shells that may dispatch a command this host emits. Claude Code runs a
71
+ // Windows hook command through cmd.exe, the one shell that also accepts a
72
+ // quoted path in command position, so the reference below needs no prefix.
73
+ // The CASE-31 gate executes wrapperRef under every shell named here.
74
+ dispatchers: ['cmd'],
69
75
  wrapperRef: (wrapper, args) =>
70
76
  '"${CLAUDE_PLUGIN_ROOT}/_hooks/run-hook.cmd" ' + wrapper + (args ? ' ' + args : ''),
71
77
  };
@@ -97,6 +103,13 @@ function listCodexPlugins() {
97
103
  });
98
104
  }
99
105
 
106
+ // A commandWindows this tool wrote before CASE-31 opens with a quoted path,
107
+ // which a PowerShell dispatcher cannot parse. It is ours to re-derive, so it
108
+ // counts as unpatched; a commandWindows a plugin author wrote is left alone.
109
+ const isStaleCodexPatch = (hook) =>
110
+ String(hook.commandWindows || '').replace(/\\/g, '/').includes('_codex_hooks/run-hook.cmd')
111
+ && opensWithQuotedPath(hook.commandWindows);
112
+
100
113
  const codex = {
101
114
  id: 'codex',
102
115
  label: 'Codex',
@@ -111,12 +124,17 @@ const codex = {
111
124
  listPlugins: listCodexPlugins,
112
125
  // A hook that already carries commandWindows is patched; leave the portable
113
126
  // command alone so it still runs natively on macOS and Linux.
114
- sourceCommand: (hook) => (hook.commandWindows ? null : hook.command),
127
+ sourceCommand: (hook) => (hook.commandWindows && !isStaleCodexPatch(hook) ? null : hook.command),
115
128
  patchedCommand: (hook) => hook.commandWindows,
116
129
  applyPatch: (hook, ref) => { hook.commandWindows = ref; },
117
- // Codex dispatches commandWindows through cmd.exe, which wants backslashes.
130
+ // Codex hands commandWindows to the session shell, which is cmd.exe or
131
+ // PowerShell depending on how the session was started - so the reference has
132
+ // to carry the CASE-31 prefix. The backslashes are for the cmd.exe that ends
133
+ // up running the wrapper.
134
+ dispatchers: ['cmd', 'powershell'],
118
135
  wrapperRef: (wrapper, args) =>
119
- '"${PLUGIN_ROOT}\\_codex_hooks\\run-hook.cmd" ' + wrapper + (args ? ' ' + args : ''),
136
+ DISPATCH_PREFIX + '"${PLUGIN_ROOT}\\_codex_hooks\\run-hook.cmd" ' + wrapper
137
+ + (args ? ' ' + args : ''),
120
138
  };
121
139
 
122
140
  export const HOSTS = { claude, codex };
package/src/rules.mjs CHANGED
@@ -56,6 +56,21 @@ export function isIncompatible(cmd, { rootVar, isInstalled = () => true } = {})
56
56
  return false;
57
57
  }
58
58
 
59
+ // CASE-31: the shell that dispatches a hook command is the host's choice, not
60
+ // ours. Every Windows shell runs a bare command name in command position, but
61
+ // only cmd.exe also accepts a quoted path there - PowerShell reads the leading
62
+ // quote as a string expression and rejects every argument after it. A command
63
+ // that has to survive an unknown dispatcher therefore opens with cmd /c, which
64
+ // is a command name under both and hands the quoted path to the cmd.exe that
65
+ // was wanted all along.
66
+ export const DISPATCH_PREFIX = 'cmd /c ';
67
+
68
+ export const opensWithQuotedPath = (cmd) => /^["']/.test(decode(cmd).trim());
69
+
70
+ // Would this command survive every shell the host might dispatch it through?
71
+ export const isDispatchable = (cmd, dispatchers) =>
72
+ !opensWithQuotedPath(cmd) || dispatchers.every((shell) => shell === 'cmd');
73
+
59
74
  // Wrapper filenames are extensionless, so Claude Code's Windows auto-detection
60
75
  // does not prepend "bash" to anything containing ".sh" (CASE-07).
61
76
  export function wrapperName(cmd, rootVar) {