@lilmgenius/win-hooks 1.15.3 → 1.16.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 +2 -1
- package/bin/win-hooks.mjs +23 -5
- package/package.json +2 -2
- package/src/heal.mjs +4 -4
- package/src/hosts.mjs +60 -3
- package/src/rules.mjs +10 -0
- package/src/settings.mjs +43 -5
- package/src/stale.mjs +77 -6
package/README.md
CHANGED
|
@@ -58,6 +58,7 @@ You installed a plugin. It works fine for everyone on a Mac. On your machine it
|
|
|
58
58
|
| `Cannot find module 'C:\Users...'` | Backslashes in a path getting eaten before the hook runs |
|
|
59
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 |
|
|
60
60
|
| win-hooks' own hooks fail in sessions opened before its update, and `patch` says healthy | The update removed the version those sessions run from; win-hooks now remembers it and puts it back |
|
|
61
|
+
| `hook exited with code 1` on every prompt, from a hook you wrote in `~/.codex/hooks.json` that starts with a quoted path | Codex runs it through PowerShell, which cannot start a program from a quoted path; win-hooks rewrites its Windows command, and you trust the file again in `/hooks` |
|
|
61
62
|
|
|
62
63
|
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.
|
|
63
64
|
|
|
@@ -76,7 +77,7 @@ At the start of a session it says so in one line, so you can tell a clean run fr
|
|
|
76
77
|
|
|
77
78
|
## Privacy
|
|
78
79
|
|
|
79
|
-
win-hooks runs entirely on your machine and sends nothing anywhere. No telemetry, no network calls, no account. Outside a plugin's own folder it writes only a short local log of its own runs, so you can confirm it ran.
|
|
80
|
+
win-hooks runs entirely on your machine and sends nothing anywhere. No telemetry, no network calls, no account. Outside a plugin's own folder it writes only the hook commands it repairs in your own hook settings (after a one-time backup beside the file) and a short local log of its own runs, so you can confirm it ran.
|
|
80
81
|
|
|
81
82
|
## License
|
|
82
83
|
|
package/bin/win-hooks.mjs
CHANGED
|
@@ -70,7 +70,10 @@ function summarize({ host, patched, failed, settings, issues, fixes }) {
|
|
|
70
70
|
const lines = [];
|
|
71
71
|
for (const p of patched) lines.push('win-hooks: patched ' + p.plugin.id + ' (' + p.hooks.join(', ') + ')');
|
|
72
72
|
for (const f of failed) lines.push('win-hooks: could not patch ' + f.plugin.id + ' - ' + f.error);
|
|
73
|
-
|
|
73
|
+
const repaired = settings.filter((s) => !s.manual);
|
|
74
|
+
if (repaired.length) lines.push('win-hooks: repaired ' + repaired.length + ' hook command(s) in ' + host.label + ' settings');
|
|
75
|
+
for (const s of settings.filter((s) => s.manual)) lines.push('win-hooks: ' + s.type + ' in ' + s.path + ' - ' + s.to);
|
|
76
|
+
lines.push(...retrustNotes(settings).map((note) => 'win-hooks: ' + note));
|
|
74
77
|
for (const f of fixes) lines.push('win-hooks: ' + f);
|
|
75
78
|
if (patched.length || fixes.length) {
|
|
76
79
|
lines.push('win-hooks: repairs are on disk - run /reload-plugins to apply them to this session.');
|
|
@@ -95,11 +98,20 @@ function patchHost(id) {
|
|
|
95
98
|
|
|
96
99
|
const after = inspect(id);
|
|
97
100
|
return report(before) + '\n\n' + report(after, 'after repair') + heartbeat(after)
|
|
98
|
-
+ '\n repairs are on disk - run /reload-plugins to apply them to this session.'
|
|
101
|
+
+ '\n repairs are on disk - run /reload-plugins to apply them to this session.'
|
|
102
|
+
+ retrustNotes(before.settings).map((note) => '\n ' + note).join('');
|
|
99
103
|
}
|
|
100
104
|
|
|
101
105
|
// Every function below is a declaration, not a const: the dispatch above runs
|
|
102
106
|
// before this point in the module, and only declarations are hoisted.
|
|
107
|
+
|
|
108
|
+
// A repaired user hooks file has a new hash, and Codex skips an untrusted hook
|
|
109
|
+
// without a word (CASE-33), so the person is told where to trust it again.
|
|
110
|
+
function retrustNotes(settings) {
|
|
111
|
+
return [...new Set(settings.filter((s) => s.retrust).map((s) => s.retrust))]
|
|
112
|
+
.map((file) => file + ' changed, so Codex skips its hooks until you trust them again in /hooks');
|
|
113
|
+
}
|
|
114
|
+
|
|
103
115
|
function statusOf(id) {
|
|
104
116
|
const state = inspect(id);
|
|
105
117
|
return report(state) + heartbeat(state);
|
|
@@ -120,6 +132,14 @@ function rootName(r) {
|
|
|
120
132
|
return r.plugin + '/' + r.version + ' (' + r.marketplace + ')';
|
|
121
133
|
}
|
|
122
134
|
|
|
135
|
+
function rootDetail(r) {
|
|
136
|
+
if (r.state === 'shadowing') {
|
|
137
|
+
return 'ranks above the installed ' + r.currentVersion + ', so Codex loads it and finds no plugin there';
|
|
138
|
+
}
|
|
139
|
+
return (r.state === 'missing' ? 'removed' : 'emptied') + ' while a session may still run from it; '
|
|
140
|
+
+ r.currentVersion + ' is installed';
|
|
141
|
+
}
|
|
142
|
+
|
|
123
143
|
function report({ host, plugins, incompatible, issues, settings, roots = [] }, heading) {
|
|
124
144
|
const title = '# ' + host.label + (heading ? ' ' + heading : '')
|
|
125
145
|
+ ' - ' + plugins.length + ' hook file(s) scanned';
|
|
@@ -131,9 +151,7 @@ function report({ host, plugins, incompatible, issues, settings, roots = [] }, h
|
|
|
131
151
|
...incompatible.map((i) => ['incompatible', i.id + ' [' + i.event + ']', i.command]),
|
|
132
152
|
...issues.map((i) => [i.type, i.plugin, i.detail]),
|
|
133
153
|
...settings.map((s) => [s.type, s.path, s.to]),
|
|
134
|
-
...brokenRoots(roots).map((r) => [r.state + ' root', rootName(r),
|
|
135
|
-
(r.state === 'missing' ? 'removed' : 'emptied') + ' while a session may still run from it; '
|
|
136
|
-
+ r.currentVersion + ' is installed']),
|
|
154
|
+
...brokenRoots(roots).map((r) => [r.state + ' root', rootName(r), rootDetail(r)]),
|
|
137
155
|
];
|
|
138
156
|
const info = roots.filter((r) => r.state === 'restored')
|
|
139
157
|
.map((r) => ['restored root', rootName(r), 'links to ' + r.currentVersion + ' for the sessions still running from it']);
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lilmgenius/win-hooks",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.16.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
7
|
"description": "Windows auto-patcher for vibe coders.",
|
|
8
|
-
"longDescription": "win-hooks scans the Claude Code and Codex plugins installed on a Windows machine and repairs the hooks that cannot run there: .sh scripts cmd.exe cannot execute, bare Unix commands and interpreters that do not resolve, UTF-8 BOMs, and
|
|
8
|
+
"longDescription": "win-hooks scans the Claude Code and Codex plugins installed on a Windows machine and repairs the hooks that cannot run there: .sh scripts cmd.exe cannot execute, bare Unix commands and interpreters that do not resolve, UTF-8 BOMs, backslash paths, and Codex user hooks that open with a quoted path PowerShell cannot run. It repairs at every session start and again whenever a plugin update reverts a fix, so plugins installed later are covered too.",
|
|
9
9
|
"scripts": {
|
|
10
10
|
"test": "node test/run.mjs"
|
|
11
11
|
},
|
package/src/heal.mjs
CHANGED
|
@@ -14,7 +14,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
14
14
|
import { newestMtime, readJson, writeJson, writeText } from './env.mjs';
|
|
15
15
|
import { HOSTS } from './hosts.mjs';
|
|
16
16
|
import { findIncompatible, patchAll } from './patch.mjs';
|
|
17
|
-
import { fixSettings } from './settings.mjs';
|
|
17
|
+
import { fixSettings, fixUserHooks } from './settings.mjs';
|
|
18
18
|
import { recordRoot, restoreStaleRoots, staleRoots } from './stale.mjs';
|
|
19
19
|
import { verify } from './verify.mjs';
|
|
20
20
|
|
|
@@ -98,7 +98,7 @@ export function heal(hostId, { changedOnly = false } = {}) {
|
|
|
98
98
|
|
|
99
99
|
const plugins = host.listPlugins();
|
|
100
100
|
const { patched, failed } = patchAll(host, TEMPLATE_CMD, plugins);
|
|
101
|
-
const settings = fixSettings(host.settingsFile, { fix: true });
|
|
101
|
+
const settings = [...fixSettings(host.settingsFile, { fix: true }), ...fixUserHooks(host, { fix: true })];
|
|
102
102
|
const { issues, fixes } = verify(host, { fix: true, templateCmd: TEMPLATE_CMD, plugins });
|
|
103
103
|
fixes.push(...restored);
|
|
104
104
|
|
|
@@ -107,7 +107,7 @@ export function heal(hostId, { changedOnly = false } = {}) {
|
|
|
107
107
|
dur: (Date.now() - started) + 'ms',
|
|
108
108
|
plugins: plugins.length,
|
|
109
109
|
patched: patched.length,
|
|
110
|
-
settings: settings.length,
|
|
110
|
+
settings: settings.filter((s) => !s.manual).length,
|
|
111
111
|
fixed: fixes.length,
|
|
112
112
|
issues: issues.length,
|
|
113
113
|
restored: restored.length,
|
|
@@ -125,7 +125,7 @@ export function inspect(hostId) {
|
|
|
125
125
|
plugins,
|
|
126
126
|
incompatible: findIncompatible(host, plugins),
|
|
127
127
|
issues: verify(host, { templateCmd: TEMPLATE_CMD, plugins }).issues,
|
|
128
|
-
settings: fixSettings(host.settingsFile),
|
|
128
|
+
settings: [...fixSettings(host.settingsFile), ...fixUserHooks(host)],
|
|
129
129
|
roots: staleRoots(host),
|
|
130
130
|
log: readLog(host),
|
|
131
131
|
};
|
package/src/hosts.mjs
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
import { spawnSync } from 'node:child_process';
|
|
10
10
|
import { existsSync } from 'node:fs';
|
|
11
11
|
import { join, resolve } from 'node:path';
|
|
12
|
-
import { HOME, readJson, toPosix } from './env.mjs';
|
|
12
|
+
import { HOME, readJson, readText, toPosix } from './env.mjs';
|
|
13
13
|
import { DISPATCH_PREFIX, opensWithQuotedPath } from './rules.mjs';
|
|
14
14
|
|
|
15
15
|
// Both hosts use the same hooks.json shape:
|
|
@@ -61,10 +61,16 @@ const claude = {
|
|
|
61
61
|
// Files that change when the plugin set does. The per-prompt guard stats
|
|
62
62
|
// these to rule out work without enumerating anything (CASE-26).
|
|
63
63
|
registry: [join(HOME, '.claude/plugins/installed_plugins.json'), join(HOME, '.claude/settings.json')],
|
|
64
|
+
// Claude's own user-level hooks live in settingsFile, dispatched by cmd.exe.
|
|
65
|
+
userHooksFile: null,
|
|
66
|
+
userTomlHooks: () => [],
|
|
64
67
|
// One directory per installed plugin version, and what marks one installed
|
|
65
68
|
// (CASE-36).
|
|
66
69
|
cacheDir: join(HOME, '.claude/plugins/cache'),
|
|
67
70
|
manifestDir: '.claude-plugin',
|
|
71
|
+
// Claude runs the installPath installed_plugins.json names, so which version
|
|
72
|
+
// directories exist beside it never changes what loads.
|
|
73
|
+
loadsHighestVersion: false,
|
|
68
74
|
listPlugins: listClaudePlugins,
|
|
69
75
|
// Claude replaces the command outright, so an already-patched hook is one
|
|
70
76
|
// that already points at our dispatcher - which isIncompatible sees as a .cmd.
|
|
@@ -114,6 +120,46 @@ const isStaleCodexPatch = (hook) =>
|
|
|
114
120
|
String(hook.commandWindows || '').replace(/\\/g, '/').includes('_codex_hooks/run-hook.cmd')
|
|
115
121
|
&& opensWithQuotedPath(hook.commandWindows);
|
|
116
122
|
|
|
123
|
+
// Hooks a person declared inline in config.toml, one { where, command } per
|
|
124
|
+
// `[[hooks.<Event>.hooks]]` table: its Windows command when it sets one, its
|
|
125
|
+
// portable command otherwise (CASE-31). Read line by line, because only those
|
|
126
|
+
// tables and their one-line string values matter here; a multi-line string or
|
|
127
|
+
// an inline array is not seen. Read only, like everything this module does to
|
|
128
|
+
// config.toml, so a finding is reported for a hand edit and never repaired.
|
|
129
|
+
function readTomlHooks(file) {
|
|
130
|
+
let text;
|
|
131
|
+
try {
|
|
132
|
+
text = readText(file);
|
|
133
|
+
} catch {
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
136
|
+
const tables = [];
|
|
137
|
+
let table = null;
|
|
138
|
+
text.split(/\r?\n/).forEach((line, i) => {
|
|
139
|
+
const header = line.match(/^\s*\[\[?\s*([^\]]+?)\s*\]\]?\s*(#.*)?$/);
|
|
140
|
+
if (header) {
|
|
141
|
+
table = /^hooks\.\w+\.hooks$/.test(header[1]) ? { where: 'config.toml line ' + (i + 1) } : null;
|
|
142
|
+
if (table) tables.push(table);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const entry = table && line.match(/^\s*(command|command_windows|commandWindows)\s*=\s*("(?:[^"\\]|\\.)*"|'[^']*')\s*(#.*)?$/);
|
|
146
|
+
if (!entry) return;
|
|
147
|
+
const value = entry[2].startsWith("'") ? entry[2].slice(1, -1) : parseBasicString(entry[2]);
|
|
148
|
+
table[entry[1] === 'command' ? 'portable' : 'windows'] = value;
|
|
149
|
+
});
|
|
150
|
+
return tables.map(({ where, portable, windows }) => ({ where, command: windows ?? portable }))
|
|
151
|
+
.filter((t) => typeof t.command === 'string');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// A TOML basic string is a JSON string for every escape a command uses.
|
|
155
|
+
const parseBasicString = (quoted) => {
|
|
156
|
+
try {
|
|
157
|
+
return JSON.parse(quoted);
|
|
158
|
+
} catch {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
117
163
|
const codex = {
|
|
118
164
|
id: 'codex',
|
|
119
165
|
label: 'Codex',
|
|
@@ -123,12 +169,23 @@ const codex = {
|
|
|
123
169
|
stateDir: join(HOME, '.codex/win-hooks'),
|
|
124
170
|
settingsFile: null,
|
|
125
171
|
// Codex records every install, removal, enable, and marketplace update in
|
|
126
|
-
// config.toml, so one stat covers the whole plugin set
|
|
127
|
-
|
|
172
|
+
// config.toml, so one stat covers the whole plugin set; the user's own hooks
|
|
173
|
+
// file is watched beside it.
|
|
174
|
+
registry: [join(HOME, '.codex/config.toml'), join(HOME, '.codex/hooks.json')],
|
|
175
|
+
// The user layer: hooks a person declared for every session, in hooks.json
|
|
176
|
+
// and inline in config.toml. Codex runs them through the same session shell
|
|
177
|
+
// as a plugin's commandWindows (CASE-31).
|
|
178
|
+
userHooksFile: join(HOME, '.codex/hooks.json'),
|
|
179
|
+
userTomlHooks: () => readTomlHooks(join(HOME, '.codex/config.toml')),
|
|
128
180
|
// Where Codex keeps one directory per installed plugin version, and what
|
|
129
181
|
// marks one installed (CASE-35, CASE-36).
|
|
130
182
|
cacheDir: join(HOME, '.codex/plugins/cache'),
|
|
131
183
|
manifestDir: '.codex-plugin',
|
|
184
|
+
// Codex loads the highest version directory of a plugin by semver, or one
|
|
185
|
+
// named `local` (active_plugin_version in openai/codex
|
|
186
|
+
// codex-rs/core-plugin-common/src/installed.rs), so a restored root above the
|
|
187
|
+
// installed version is the one it loads (CASE-36).
|
|
188
|
+
loadsHighestVersion: true,
|
|
132
189
|
listPlugins: listCodexPlugins,
|
|
133
190
|
// A hook that already carries commandWindows is patched; leave the portable
|
|
134
191
|
// command alone so it still runs natively on macOS and Linux.
|
package/src/rules.mjs
CHANGED
|
@@ -72,6 +72,16 @@ export const opensWithQuotedPath = (cmd) => /^["']/.test(decode(cmd).trim());
|
|
|
72
72
|
export const isDispatchable = (cmd, dispatchers) =>
|
|
73
73
|
!opensWithQuotedPath(cmd) || dispatchers.every((shell) => shell === 'cmd');
|
|
74
74
|
|
|
75
|
+
// CASE-31 in the user layer: a hook command a person wrote, rather than one
|
|
76
|
+
// this tool emits, opens with a quoted path just as often - "C:/Program
|
|
77
|
+
// Files/nodejs/node.exe" "C:/x/hook.mjs" - and cmd.exe, the shell it was
|
|
78
|
+
// written for, runs it while PowerShell rejects it before node starts. The text
|
|
79
|
+
// is the person's, so it is handed to cmd.exe whole. `--%` makes PowerShell pass
|
|
80
|
+
// the rest of the line through unparsed, cmd.exe skips it, and /s strips exactly
|
|
81
|
+
// the outer quotes added here. Measured under cmd.exe, Windows PowerShell 5.1
|
|
82
|
+
// and pwsh 7: the arguments, stdin and the exit code all arrive.
|
|
83
|
+
export const anyShellCommand = (cmd) => 'cmd --% /d /s /c "' + String(cmd).trim() + '"';
|
|
84
|
+
|
|
75
85
|
// The two halves of the dispatcher, copied into a hook directory from the
|
|
76
86
|
// shipped template and never generated per hook.
|
|
77
87
|
export const DISPATCHER_FILES = ['run-hook.cmd', 'run.mjs'];
|
package/src/settings.mjs
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
// Repairs for
|
|
1
|
+
// Repairs for the hook commands a person writes for every session: Claude's
|
|
2
|
+
// ~/.claude/settings.json and Codex's user layer.
|
|
2
3
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// string rather than a
|
|
6
|
-
// pipeline.
|
|
4
|
+
// Claude dispatches these through cmd.exe, whose environment is not Git Bash's,
|
|
5
|
+
// and Codex through its session shell, often PowerShell. What breaks is the
|
|
6
|
+
// command string itself rather than a plugin's files, so the repairs rewrite
|
|
7
|
+
// that string and live apart from the plugin pipeline.
|
|
7
8
|
|
|
8
9
|
import { existsSync } from 'node:fs';
|
|
9
10
|
import { backupOnce, readJson, resolveInterpreter, writeJson } from './env.mjs';
|
|
11
|
+
import { eachHook } from './hosts.mjs';
|
|
12
|
+
import { anyShellCommand, isDispatchable } from './rules.mjs';
|
|
10
13
|
|
|
11
14
|
// CASE-20: a Windows path inside a hook command loses its backslashes to shell
|
|
12
15
|
// unescaping, producing MODULE_NOT_FOUND. Forward slashes survive.
|
|
@@ -59,3 +62,38 @@ export function fixSettings(file, { fix = false } = {}) {
|
|
|
59
62
|
}
|
|
60
63
|
return changes;
|
|
61
64
|
}
|
|
65
|
+
|
|
66
|
+
// CASE-31 in the user layer: a hook command that opens with a quoted path, which
|
|
67
|
+
// a PowerShell session shell rejects before the program starts. In hooks.json
|
|
68
|
+
// the Windows command is replaced by one every shell runs, keeping the portable
|
|
69
|
+
// command for macOS and Linux as the plugin lane does. A hook declared inline in
|
|
70
|
+
// the Codex config file is only reported, since win-hooks never writes that
|
|
71
|
+
// file (CASE-33). Every change to hooks.json changes the hash Codex trusted it under,
|
|
72
|
+
// so a repaired change carries `retrust`: Codex skips the file's hooks until the
|
|
73
|
+
// person trusts them again in /hooks.
|
|
74
|
+
export function fixUserHooks(host, { fix = false } = {}) {
|
|
75
|
+
const changes = [];
|
|
76
|
+
const file = host.userHooksFile;
|
|
77
|
+
const { ok, data } = file && existsSync(file) ? readJson(file) : { ok: false };
|
|
78
|
+
if (ok) {
|
|
79
|
+
for (const { event, hook } of eachHook(data)) {
|
|
80
|
+
const key = ['commandWindows', 'command_windows'].find((k) => typeof hook[k] === 'string') || 'commandWindows';
|
|
81
|
+
const command = hook[key] ?? hook.command;
|
|
82
|
+
if (typeof command !== 'string' || isDispatchable(command, host.dispatchers)) continue;
|
|
83
|
+
const to = anyShellCommand(command);
|
|
84
|
+
changes.push({ path: file + ' ' + event, type: 'quoted_command', from: command, to, retrust: file });
|
|
85
|
+
hook[key] = to;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (changes.length && fix) {
|
|
89
|
+
backupOnce(file, '.winhooks.bak');
|
|
90
|
+
writeJson(file, data);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
for (const { where, command } of host.userTomlHooks()) {
|
|
94
|
+
if (isDispatchable(command, host.dispatchers)) continue;
|
|
95
|
+
changes.push({ path: where, type: 'quoted_command', from: command, manual: true,
|
|
96
|
+
to: 'edit by hand: commandWindows = ' + JSON.stringify(anyShellCommand(command)) });
|
|
97
|
+
}
|
|
98
|
+
return changes;
|
|
99
|
+
}
|
package/src/stale.mjs
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
// the restored root gets no manifest directory, so it never reads as an
|
|
22
22
|
// installed version. Removing a junction does not follow it.
|
|
23
23
|
|
|
24
|
-
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, renameSync, statSync, symlinkSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
24
|
+
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, renameSync, rmdirSync, statSync, symlinkSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
25
25
|
import { dirname, join, relative, resolve } from 'node:path';
|
|
26
26
|
import { toPosix } from './env.mjs';
|
|
27
27
|
|
|
@@ -99,6 +99,55 @@ function newestInstalled(base, manifest) {
|
|
|
99
99
|
const linkable = (current, manifest) =>
|
|
100
100
|
childNames(current).filter((name) => name !== manifest && isRealDir(join(current, name)));
|
|
101
101
|
|
|
102
|
+
// -- Which version a host loads ----------------------------------------
|
|
103
|
+
|
|
104
|
+
// Codex loads the highest version directory of a plugin, comparing names as
|
|
105
|
+
// semver when both parse and as plain strings otherwise, and one named `local`
|
|
106
|
+
// above all (active_plugin_version in openai/codex
|
|
107
|
+
// codex-rs/core-plugin-common/src/installed.rs). A root restored above the
|
|
108
|
+
// installed version is therefore the one it loads, finds no manifest in, and
|
|
109
|
+
// drops the plugin over. That happens when a catalogue moves a plugin back -
|
|
110
|
+
// openai-curated-remote served visualize 1.0.89 after 1.0.109 - so such a root
|
|
111
|
+
// is never restored, and one already restored is taken away (CASE-36).
|
|
112
|
+
const SEMVER = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+([0-9A-Za-z.-]+))?$/;
|
|
113
|
+
|
|
114
|
+
const compareText = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
115
|
+
|
|
116
|
+
// Dot-separated identifiers, as semver orders a pre-release: numbers by value,
|
|
117
|
+
// numbers below words, and a shorter list below a longer one it begins.
|
|
118
|
+
function compareIdentifiers(a, b) {
|
|
119
|
+
const left = a.split('.');
|
|
120
|
+
const right = b.split('.');
|
|
121
|
+
for (let i = 0; i < Math.min(left.length, right.length); i++) {
|
|
122
|
+
const [x, y] = [left[i], right[i]];
|
|
123
|
+
const numeric = [/^\d+$/.test(x), /^\d+$/.test(y)];
|
|
124
|
+
let order;
|
|
125
|
+
if (numeric[0] && numeric[1]) order = Math.sign(Number(x) - Number(y));
|
|
126
|
+
else if (numeric[0] !== numeric[1]) order = numeric[0] ? -1 : 1;
|
|
127
|
+
else order = compareText(x, y);
|
|
128
|
+
if (order) return order;
|
|
129
|
+
}
|
|
130
|
+
return Math.sign(left.length - right.length);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function compareVersions(a, b) {
|
|
134
|
+
const x = a.match(SEMVER);
|
|
135
|
+
const y = b.match(SEMVER);
|
|
136
|
+
if (!x || !y) return compareText(a, b);
|
|
137
|
+
for (const i of [1, 2, 3]) {
|
|
138
|
+
const order = Math.sign(Number(x[i]) - Number(y[i]));
|
|
139
|
+
if (order) return order;
|
|
140
|
+
}
|
|
141
|
+
// A release ranks above its pre-releases; build metadata breaks the last tie.
|
|
142
|
+
if (x[4] !== y[4]) return !x[4] ? 1 : !y[4] ? -1 : compareIdentifiers(x[4], y[4]);
|
|
143
|
+
if (x[5] !== y[5]) return !x[5] ? -1 : !y[5] ? 1 : compareIdentifiers(x[5], y[5]);
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Would a directory named `version` be loaded instead of the installed one?
|
|
148
|
+
const outranks = (host, version, installed) => host.loadsHighestVersion
|
|
149
|
+
&& (version === 'local' || (installed !== 'local' && compareVersions(version, installed) > 0));
|
|
150
|
+
|
|
102
151
|
// Where a root sits in a host's cache: <marketplace>/<plugin>/<version>.
|
|
103
152
|
function placeOf(host, root) {
|
|
104
153
|
if (!host.cacheDir) return null;
|
|
@@ -169,8 +218,10 @@ function installedRoots(host) {
|
|
|
169
218
|
|
|
170
219
|
// Every root a live session may still run from that does not hold a working
|
|
171
220
|
// copy, plus the ones already restored. `state` is `missing` (removed whole,
|
|
172
|
-
// known from a record), `emptied` (left on disk with no working links),
|
|
173
|
-
// `restored
|
|
221
|
+
// known from a record), `emptied` (left on disk with no working links),
|
|
222
|
+
// `restored`, or `shadowing` (empty or links only, and ranked above the
|
|
223
|
+
// installed version, so the host loads it instead). Reads only: `status`
|
|
224
|
+
// reports from this.
|
|
174
225
|
export function staleRoots(host, now = Date.now()) {
|
|
175
226
|
const manifest = host.manifestDir;
|
|
176
227
|
if (!host.cacheDir || !manifest) return [];
|
|
@@ -190,6 +241,10 @@ export function staleRoots(host, now = Date.now()) {
|
|
|
190
241
|
for (const name of childNames(base).filter((n) => !n.startsWith('.'))) {
|
|
191
242
|
const root = join(base, name);
|
|
192
243
|
if (!isEmptied(root, manifest)) continue;
|
|
244
|
+
if (outranks(host, name, current.slice(base.length + 1))) {
|
|
245
|
+
add(root, current, 'shadowing');
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
193
248
|
const whole = linkable(current, manifest).every((n) => linkedTo(join(root, n), join(current, n)));
|
|
194
249
|
add(root, current, whole ? 'restored' : 'emptied');
|
|
195
250
|
}
|
|
@@ -197,21 +252,37 @@ export function staleRoots(host, now = Date.now()) {
|
|
|
197
252
|
}
|
|
198
253
|
|
|
199
254
|
// Only a version of a plugin still installed is restored: an uninstall
|
|
200
|
-
// leaves nothing newer to run.
|
|
255
|
+
// leaves nothing newer to run. A version above the installed one is not
|
|
256
|
+
// restored either, since the host would load it in place of the install.
|
|
201
257
|
for (const root of Object.keys(readRecords(host, now))) {
|
|
202
258
|
const place = placeOf(host, root);
|
|
203
259
|
if (!place || lstatOf(root)) continue;
|
|
204
260
|
const current = newestInstalled(place.base, manifest);
|
|
205
|
-
if (current
|
|
261
|
+
if (current && !outranks(host, place.version, current.slice(place.base.length + 1))) {
|
|
262
|
+
add(resolve(root), current, 'missing');
|
|
263
|
+
}
|
|
206
264
|
}
|
|
207
265
|
return [...found.values()];
|
|
208
266
|
}
|
|
209
267
|
|
|
210
|
-
// Restore every root staleRoots finds broken
|
|
268
|
+
// Restore every root staleRoots finds broken, and take away every one that
|
|
269
|
+
// shadows the installed version; returns one line per root.
|
|
211
270
|
export function restoreStaleRoots(host, now = Date.now()) {
|
|
212
271
|
const lines = [];
|
|
213
272
|
for (const s of staleRoots(host, now)) {
|
|
214
273
|
if (s.state === 'restored') continue;
|
|
274
|
+
if (s.state === 'shadowing') {
|
|
275
|
+
try {
|
|
276
|
+
for (const name of childNames(s.root)) unlinkSync(join(s.root, name));
|
|
277
|
+
rmdirSync(s.root);
|
|
278
|
+
} catch (e) {
|
|
279
|
+
lines.push('could not remove ' + s.plugin + '/' + s.version + ' - ' + e.message);
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
lines.push('removed ' + s.plugin + '/' + s.version + ', which ranked above the installed '
|
|
283
|
+
+ s.currentVersion + ' so the host loaded it instead (' + s.marketplace + ')');
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
215
286
|
try {
|
|
216
287
|
mkdirSync(s.root, { recursive: true });
|
|
217
288
|
for (const name of linkable(s.current, host.manifestDir)) linkTo(join(s.root, name), join(s.current, name));
|