-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.mjs
More file actions
179 lines (167 loc) · 6.63 KB
/
Copy pathuninstall.mjs
File metadata and controls
179 lines (167 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
// Maestro uninstaller — removes only bytes recorded in the ownership manifest and
// exact marked hook commands. Divergent files are preserved.
import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
const HOME = os.homedir();
const CLAUDE = path.join(HOME, '.claude');
const HOOKS = path.join(CLAUDE, 'hooks');
const SETTINGS = path.join(CLAUDE, 'settings.json');
const MAESTRO_DIR = path.join(HOME, '.maestro');
const MANIFEST = path.join(MAESTRO_DIR, 'install-manifest.json');
const AUTHORIZATION_DIR = path.join(MAESTRO_DIR, 'direct-edit');
const AUTHORIZATION_PATTERN = /^maestro-direct-[A-Za-z0-9_-]{1,64}\.flag$/;
const HOOK_FILES = [
'orchestrator-inject.mjs', 'orchestrator-gate.mjs', 'maestro-policy.mjs', 'session-start.mjs',
'implementer-watchdog.sh', 'implementer-loop.sh', 'discussion-loop.sh',
'lib-companion.sh', 'codex-model-select.sh', 'codex-mcp-check.sh',
];
const log = (...args) => console.log('[maestro]', ...args);
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
const hashFile = (file) => crypto.createHash('sha256').update(fs.readFileSync(file)).digest('hex');
function atomicWrite(file, bytes, mode) {
const tmp = `${file}.maestro-tmp-${process.pid}`;
fs.mkdirSync(path.dirname(file), { recursive: true });
try {
fs.writeFileSync(tmp, bytes, { mode });
if (mode !== undefined) fs.chmodSync(tmp, mode);
fs.renameSync(tmp, file);
} catch (error) {
try { fs.rmSync(tmp); } catch {}
throw error;
}
}
function refreshBackup(file, suffix) {
if (!fs.existsSync(file)) return;
atomicWrite(`${file}${suffix}`, fs.readFileSync(file), fs.statSync(file).mode);
}
function managedPath(key) {
if (path.isAbsolute(key) || key.split('/').includes('..') ||
!/^(hooks|rules|skills)\//.test(key)) return null;
const destination = path.resolve(CLAUDE, key);
const prefix = `${path.resolve(CLAUDE)}${path.sep}`;
return destination.startsWith(prefix) ? destination : null;
}
function readManifest() {
if (!fs.existsSync(MANIFEST) || fs.lstatSync(MANIFEST).isSymbolicLink()) return null;
try {
const value = JSON.parse(fs.readFileSync(MANIFEST, 'utf8'));
if (value?.version !== 1 || !isObject(value.files)) return null;
return value;
} catch {
return null;
}
}
function managedCommand(script) {
return `node "${path.join(HOOKS, script)}" # maestro-managed:${script}`;
}
function legacyCommand(script) {
return `node "${path.join(HOOKS, script)}"`;
}
function readSettingsForUninstall() {
if (!fs.existsSync(SETTINGS)) return null;
if (fs.lstatSync(SETTINGS).isSymbolicLink()) {
log('settings.json is a symlink; refusing a partial uninstall.');
process.exit(1);
}
let settings;
try { settings = JSON.parse(fs.readFileSync(SETTINGS, 'utf8')); }
catch {
log('settings.json is invalid JSON; refusing a partial uninstall.');
process.exit(1);
}
if (!isObject(settings) || (settings.hooks !== undefined && !isObject(settings.hooks))) {
log('settings.json schema is invalid; refusing a partial uninstall.');
process.exit(1);
}
for (const blocks of Object.values(settings.hooks ?? {})) {
if (!Array.isArray(blocks) || blocks.some((block) => !isObject(block) ||
(block.hooks !== undefined && !Array.isArray(block.hooks)))) {
log('settings.json hook schema is invalid; refusing a partial uninstall.');
process.exit(1);
}
}
return settings;
}
const settings = readSettingsForUninstall();
const manifest = readManifest();
if (!manifest) {
log('ownership manifest missing or invalid — kept all installed files; no filename-only deletion is safe');
} else {
for (const [key, recordedHash] of Object.entries(manifest.files)) {
const installed = managedPath(key);
if (!installed || !/^[a-f0-9]{64}$/.test(recordedHash)) {
log(`kept untrusted manifest entry ${key}`);
continue;
}
if (!fs.existsSync(installed)) {
delete manifest.files[key];
continue;
}
const stat = fs.lstatSync(installed);
if (!stat.isFile() || stat.isSymbolicLink()) {
log(`kept ${key} — it is not a regular owned file`);
continue;
}
if (hashFile(installed) !== recordedHash) {
log(`kept ${key} — its bytes changed after installation`);
continue;
}
fs.rmSync(installed);
delete manifest.files[key];
log(`removed ${key}`);
let parent = path.dirname(installed);
while (parent !== CLAUDE && parent.startsWith(`${CLAUDE}${path.sep}`)) {
try { fs.rmdirSync(parent); } catch { break; }
parent = path.dirname(parent);
}
}
if (Object.keys(manifest.files).length === 0) {
fs.rmSync(MANIFEST);
} else {
atomicWrite(MANIFEST, `${JSON.stringify(manifest, null, 2)}\n`, 0o600);
}
}
if (settings) {
const commands = new Set(HOOK_FILES.flatMap((script) => [managedCommand(script), legacyCommand(script)]));
let changed = false;
for (const [event, blocks] of Object.entries(settings.hooks ?? {})) {
settings.hooks[event] = blocks
.map((block) => {
if (!Array.isArray(block.hooks)) return block;
const hooks = block.hooks.filter((hook) => {
const remove = isObject(hook) && hook.type === 'command' && commands.has(hook.command);
changed ||= remove;
return !remove;
});
return { ...block, hooks };
})
.filter((block) => !Array.isArray(block.hooks) || block.hooks.length > 0);
if (settings.hooks[event].length === 0) delete settings.hooks[event];
}
if (changed) {
refreshBackup(SETTINGS, '.maestro-uninstall.bak');
atomicWrite(SETTINGS, `${JSON.stringify(settings, null, 2)}\n`, fs.statSync(SETTINGS).mode);
log('stripped exact Maestro hook entries from settings.json (other hooks kept)');
}
}
const askFlag = path.join(MAESTRO_DIR, 'ask-on-start');
if (fs.existsSync(askFlag) && !fs.lstatSync(askFlag).isSymbolicLink()) {
fs.rmSync(askFlag);
log('removed ~/.maestro/ask-on-start');
}
if (fs.existsSync(AUTHORIZATION_DIR) && !fs.lstatSync(AUTHORIZATION_DIR).isSymbolicLink() &&
fs.lstatSync(AUTHORIZATION_DIR).isDirectory()) {
for (const name of fs.readdirSync(AUTHORIZATION_DIR)) {
if (!AUTHORIZATION_PATTERN.test(name)) continue;
const candidate = path.join(AUTHORIZATION_DIR, name);
const stat = fs.lstatSync(candidate);
if (stat.isFile() || stat.isSymbolicLink()) fs.rmSync(candidate, { force: true });
}
try { fs.rmdirSync(AUTHORIZATION_DIR); } catch {}
log('removed private direct-edit authorization markers');
}
try { fs.rmdirSync(MAESTRO_DIR); } catch {}
log('done. web_search="disabled" and model pins in ~/.codex/config.toml were left as-is.');