@lilmgenius/win-hooks 1.14.0 → 1.15.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
@@ -56,9 +56,12 @@ You installed a plugin. It works fine for everyone on a Mac. On your machine it
56
56
  | `Python was not found; ... Microsoft Store` | Windows' fake `python3` placeholder shadowing your real one |
57
57
  | `JSON Parse error: Unrecognized token` | An invisible byte (BOM) at the top of a config file |
58
58
  | `Cannot find module 'C:\Users...'` | Backslashes in a path getting eaten before the hook runs |
59
+ | `hook exited with code 1` on every hook, in a Codex session that was fine an hour ago | Another Codex window updated the plugin and deleted the version this session was running |
59
60
 
60
61
  None of that is your fault. Almost every plugin is written and tested on macOS or Linux, so its hooks assume Unix tools that Windows does not have.
61
62
 
63
+ It also repairs the damage no error message shows you: CRLF line endings, a `hooks.json` no parser will read, and the patched hook entries themselves - one that went missing, one that calls an interpreter on itself, one aimed at a target that is gone, and files an older win-hooks left behind.
64
+
62
65
  ## How it stays fixed
63
66
 
64
67
  win-hooks repairs your plugins at every session start, including ones you install later, ones that break again after an update, and ones an older win-hooks left behind. Repairs are written next to the original file, never on top of it, and anything already working is left alone.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lilmgenius/win-hooks",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/heal.mjs CHANGED
@@ -15,6 +15,7 @@ import { newestMtime, readJson, writeJson, writeText } from './env.mjs';
15
15
  import { HOSTS } from './hosts.mjs';
16
16
  import { findIncompatible, patchAll } from './patch.mjs';
17
17
  import { fixSettings } from './settings.mjs';
18
+ import { restoreStaleRoots } from './stale.mjs';
18
19
  import { verify } from './verify.mjs';
19
20
 
20
21
  const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
@@ -83,12 +84,20 @@ const commit = (host, plugins) => {
83
84
  export function heal(hostId, { changedOnly = false } = {}) {
84
85
  const host = HOSTS[hostId];
85
86
  const started = Date.now();
86
- if (changedOnly && unchanged(host)) return null;
87
+ // Before the guard: an upgrade run by another process moves nothing the
88
+ // guard watches, and the sessions it broke are waiting on this prompt.
89
+ const restored = host.cacheDir ? restoreStaleRoots(host.cacheDir) : [];
90
+ if (changedOnly && unchanged(host)) {
91
+ if (!restored.length) return null;
92
+ record(host, { dur: (Date.now() - started) + 'ms', restored: restored.length });
93
+ return { host, scanned: 0, patched: [], failed: [], settings: [], issues: [], fixes: restored };
94
+ }
87
95
 
88
96
  const plugins = host.listPlugins();
89
97
  const { patched, failed } = patchAll(host, TEMPLATE_CMD, plugins);
90
98
  const settings = fixSettings(host.settingsFile, { fix: true });
91
99
  const { issues, fixes } = verify(host, { fix: true, templateCmd: TEMPLATE_CMD, plugins });
100
+ fixes.push(...restored);
92
101
 
93
102
  commit(host, plugins);
94
103
  record(host, {
@@ -98,6 +107,7 @@ export function heal(hostId, { changedOnly = false } = {}) {
98
107
  settings: settings.length,
99
108
  fixed: fixes.length,
100
109
  issues: issues.length,
110
+ restored: restored.length,
101
111
  });
102
112
 
103
113
  return { host, scanned: plugins.length, patched, failed, settings, issues, fixes };
package/src/hosts.mjs CHANGED
@@ -121,6 +121,8 @@ const codex = {
121
121
  // Codex records every install, removal, enable, and marketplace update in
122
122
  // config.toml, so one stat covers the whole plugin set.
123
123
  registry: [join(HOME, '.codex/config.toml')],
124
+ // Where Codex keeps one directory per installed plugin version (CASE-35).
125
+ cacheDir: join(HOME, '.codex/plugins/cache'),
124
126
  listPlugins: listCodexPlugins,
125
127
  // A hook that already carries commandWindows is patched; leave the portable
126
128
  // command alone so it still runs natively on macOS and Linux.
package/src/stale.mjs ADDED
@@ -0,0 +1,99 @@
1
+ // Version directories a live Codex session still runs from (CASE-35).
2
+ //
3
+ // Codex resolves ${PLUGIN_ROOT} into a versioned cache path once, when a
4
+ // session loads its hooks, and never again. A marketplace upgrade run by any
5
+ // other Codex process installs the next version beside it and deletes the old
6
+ // one, so every hook of every session already open fails from then on: the
7
+ // session's shell reports `hook exited with code 1` and Codex shows nothing
8
+ // else (openai/codex#31383). On Windows the deletion usually stops short of
9
+ // the directory itself, because a process the old session started still holds
10
+ // it, and that leftover empty directory is the one place those sessions look.
11
+ //
12
+ // The repair links each directory of the installed version into the emptied
13
+ // one with a junction, so the old sessions run the new code until they end.
14
+ // Nothing real is touched: a directory holding any real file or directory is
15
+ // either a whole older version or an install in progress, and the restored
16
+ // root gets no `.codex-plugin`, so it never reads as an installed version.
17
+ // Codex's own cleanup removes a junction without following it.
18
+
19
+ import { existsSync, lstatSync, mkdirSync, readdirSync, readlinkSync, statSync, symlinkSync, unlinkSync } from 'node:fs';
20
+ import { join, resolve } from 'node:path';
21
+
22
+ const MANIFEST_DIR = '.codex-plugin';
23
+
24
+ const lstatOf = (path) => {
25
+ try {
26
+ return lstatSync(path);
27
+ } catch {
28
+ return null;
29
+ }
30
+ };
31
+
32
+ const childNames = (dir) => {
33
+ try {
34
+ return readdirSync(dir);
35
+ } catch {
36
+ return [];
37
+ }
38
+ };
39
+
40
+ const isRealDir = (path) => {
41
+ const st = lstatOf(path);
42
+ return Boolean(st && st.isDirectory() && !st.isSymbolicLink());
43
+ };
44
+
45
+ // An installed version: its manifest directory is real, not one of our links.
46
+ const isInstalled = (root) => isRealDir(join(root, MANIFEST_DIR)) && existsSync(join(root, MANIFEST_DIR, 'plugin.json'));
47
+
48
+ // Empty, or holding nothing but links: what an upgrade left of a version.
49
+ const isEmptied = (root) => isRealDir(root) && !isInstalled(root)
50
+ && childNames(root).every((name) => lstatOf(join(root, name))?.isSymbolicLink());
51
+
52
+ const samePath = (a, b) => {
53
+ const norm = (p) => resolve(String(p).replace(/^\\\\\?\\/, '')).replace(/[\\/]+$/, '').toLowerCase();
54
+ return norm(a) === norm(b);
55
+ };
56
+
57
+ // Point `link` at `target`; returns whether anything changed.
58
+ function linkTo(link, target) {
59
+ const st = lstatOf(link);
60
+ if (st && !st.isSymbolicLink()) return false;
61
+ if (st) {
62
+ if (samePath(readlinkSync(link), target)) return false;
63
+ unlinkSync(link);
64
+ }
65
+ symlinkSync(target, link, 'junction');
66
+ return true;
67
+ }
68
+
69
+ // The version Codex installed last. Codex copies files with their source
70
+ // times, so the directory's own creation time is the install time.
71
+ const newest = (roots) => roots
72
+ .map((root) => ({ root, born: statSync(root).birthtimeMs }))
73
+ .sort((a, b) => b.born - a.born)[0]?.root;
74
+
75
+ // Repair every emptied version under a Codex plugin cache; returns one line
76
+ // per version it relinked.
77
+ export function restoreStaleRoots(cacheDir) {
78
+ const restored = [];
79
+ for (const marketplace of childNames(cacheDir)) {
80
+ for (const plugin of childNames(join(cacheDir, marketplace))) {
81
+ const base = join(cacheDir, marketplace, plugin);
82
+ const versions = childNames(base).filter((name) => !name.startsWith('.')).map((name) => join(base, name));
83
+ const current = newest(versions.filter(isInstalled));
84
+ if (!current) continue;
85
+
86
+ for (const root of versions.filter(isEmptied)) {
87
+ const names = childNames(current).filter((name) => name !== MANIFEST_DIR && isRealDir(join(current, name)));
88
+ mkdirSync(root, { recursive: true });
89
+ const changed = names.filter((name) => linkTo(join(root, name), join(current, name)));
90
+ if (changed.length) {
91
+ restored.push('relinked ' + plugin + '/' + root.slice(base.length + 1) + ' to ' + current.slice(base.length + 1)
92
+ + ' for the sessions still running from it (' + marketplace + ')');
93
+ }
94
+ }
95
+ }
96
+ }
97
+ return restored;
98
+ }
99
+