claude-instance-manager 1.0.0 → 1.0.1
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 +9 -0
- package/cli.js +35 -0
- package/instances.js +48 -15
- package/package.json +1 -1
- package/test/instances.test.js +19 -0
- package/test/shortcut.test.js +1 -1
- package/test/wipe.test.js +173 -0
- package/wipe.js +128 -0
package/README.md
CHANGED
|
@@ -94,6 +94,7 @@ cim open Work
|
|
|
94
94
|
cim open "My Account"
|
|
95
95
|
cim list
|
|
96
96
|
cim delete Work
|
|
97
|
+
cim wipe-instances
|
|
97
98
|
cim --help
|
|
98
99
|
```
|
|
99
100
|
|
|
@@ -101,6 +102,14 @@ cim --help
|
|
|
101
102
|
instance directories. **`delete` permanently removes the named instance
|
|
102
103
|
directory and its data.**
|
|
103
104
|
|
|
105
|
+
**`wipe-instances` permanently deletes all stopped managed instance directories**,
|
|
106
|
+
including hidden ones, after you type `DELETE` in an interactive terminal. It
|
|
107
|
+
skips running instances and protects the Default instance, including Default
|
|
108
|
+
data linked into a managed folder. It stops if running status cannot be checked;
|
|
109
|
+
some folders may already have been deleted if an error occurs. Close instances
|
|
110
|
+
before wiping to avoid a concurrent launch. Launcher-saved names may remain
|
|
111
|
+
visible after their data is deleted.
|
|
112
|
+
|
|
104
113
|
## Instance storage
|
|
105
114
|
|
|
106
115
|
Instance data is stored locally under `%APPDATA%\Claude-Profile-<name>`.
|
package/cli.js
CHANGED
|
@@ -6,6 +6,9 @@ const path = require('path');
|
|
|
6
6
|
const fs = require('fs');
|
|
7
7
|
const yargs = require('yargs/yargs');
|
|
8
8
|
const { hideBin } = require('yargs/helpers');
|
|
9
|
+
const readline = require('readline/promises');
|
|
10
|
+
const { getAllRunningManagedInstances } = require('./instances');
|
|
11
|
+
const { wipeInstances } = require('./wipe');
|
|
9
12
|
|
|
10
13
|
const appData = process.env.APPDATA;
|
|
11
14
|
|
|
@@ -98,6 +101,13 @@ yargs(hideBin(process.argv))
|
|
|
98
101
|
}
|
|
99
102
|
})
|
|
100
103
|
|
|
104
|
+
.command('wipe-instances', 'Permanently delete stopped managed instance data', {}, () => {
|
|
105
|
+
return runWipe().catch(error => {
|
|
106
|
+
console.error(`Error: Could not wipe instances: ${error.message}`);
|
|
107
|
+
process.exitCode = 1;
|
|
108
|
+
});
|
|
109
|
+
})
|
|
110
|
+
|
|
101
111
|
.command('create-shortcut', 'Create a desktop shortcut for the manager', {}, () => {
|
|
102
112
|
manageShortcut('create');
|
|
103
113
|
})
|
|
@@ -126,6 +136,31 @@ yargs(hideBin(process.argv))
|
|
|
126
136
|
.alias('h', 'help')
|
|
127
137
|
.parse();
|
|
128
138
|
|
|
139
|
+
async function runWipe() {
|
|
140
|
+
if (process.platform !== 'win32') throw new Error('Wiping instances is only supported on Windows.');
|
|
141
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
142
|
+
throw new Error('An interactive terminal is required to confirm the wipe.');
|
|
143
|
+
}
|
|
144
|
+
if (!process.env.LOCALAPPDATA || !path.isAbsolute(process.env.LOCALAPPDATA)) {
|
|
145
|
+
throw new Error('The Windows local application data directory is unavailable.');
|
|
146
|
+
}
|
|
147
|
+
await wipeInstances(appData, {
|
|
148
|
+
localAppData: process.env.LOCALAPPDATA,
|
|
149
|
+
checkRunning: () => getAllRunningManagedInstances(appData),
|
|
150
|
+
log: message => console.log(message),
|
|
151
|
+
confirm: async instances => {
|
|
152
|
+
console.log(`These instance directories will be permanently deleted: ${instances.join(', ')}`);
|
|
153
|
+
console.log('The Default instance will not be touched. Running instances are skipped.');
|
|
154
|
+
const prompt = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
155
|
+
try {
|
|
156
|
+
return (await prompt.question('Type DELETE to confirm: ')) === 'DELETE';
|
|
157
|
+
} finally {
|
|
158
|
+
prompt.close();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
129
164
|
function manageShortcut(action) {
|
|
130
165
|
if (process.platform !== 'win32') {
|
|
131
166
|
console.error('Error: Desktop shortcuts are only supported on Windows.');
|
package/instances.js
CHANGED
|
@@ -6,16 +6,21 @@ const { promisify } = require('util');
|
|
|
6
6
|
const execFileAsync = promisify(execFile);
|
|
7
7
|
const instancePrefix = 'Claude-Profile-';
|
|
8
8
|
|
|
9
|
-
function
|
|
9
|
+
function listInstanceEntries(appData) {
|
|
10
10
|
return fs.readdirSync(appData, { withFileTypes: true })
|
|
11
|
-
.filter(entry => entry.
|
|
11
|
+
.filter(entry => entry.name.toLowerCase().startsWith(instancePrefix.toLowerCase()));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function listInstances(appData) {
|
|
15
|
+
return listInstanceEntries(appData)
|
|
16
|
+
.filter(entry => entry.isDirectory())
|
|
12
17
|
.map(entry => {
|
|
13
18
|
const id = entry.name.slice(instancePrefix.length);
|
|
14
19
|
return { id, name: id.replace(/_/g, ' ') };
|
|
15
20
|
});
|
|
16
21
|
}
|
|
17
22
|
|
|
18
|
-
function runningInstances(processes, claudePath, appData) {
|
|
23
|
+
function runningInstances(processes, claudePath, appData, options = {}) {
|
|
19
24
|
const normalize = value => path.win32.normalize(value).toLowerCase();
|
|
20
25
|
const running = new Set();
|
|
21
26
|
let defaultRunning = false;
|
|
@@ -24,7 +29,7 @@ function runningInstances(processes, claudePath, appData) {
|
|
|
24
29
|
if (!process.ExecutablePath || !process.CommandLine) {
|
|
25
30
|
throw new Error('Some Claude process details are unavailable.');
|
|
26
31
|
}
|
|
27
|
-
if (normalize(process.ExecutablePath) !== normalize(claudePath)) continue;
|
|
32
|
+
if (!options.allExecutables && normalize(process.ExecutablePath) !== normalize(claudePath)) continue;
|
|
28
33
|
if (/(?:^|\s)"?--type(?:=|\s)/i.test(process.CommandLine)) continue;
|
|
29
34
|
|
|
30
35
|
const match = process.CommandLine.match(/(?:^|\s)(?:"--user-data-dir=([^"]+)"|--user-data-dir(?:=|\s+)(?:"([^"]+)"|(\S+)))/i);
|
|
@@ -36,25 +41,40 @@ function runningInstances(processes, claudePath, appData) {
|
|
|
36
41
|
continue;
|
|
37
42
|
}
|
|
38
43
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
let instancePath = match[1] || match[2] || match[3];
|
|
45
|
+
if (options.allExecutables && !path.win32.isAbsolute(instancePath)) {
|
|
46
|
+
throw new Error('A Claude instance uses a relative data directory; its running status cannot be determined safely.');
|
|
47
|
+
}
|
|
48
|
+
const paths = options.resolveInstancePath ? [instancePath, options.resolveInstancePath(instancePath)] : [instancePath];
|
|
49
|
+
const roots = options.originalAppData ? [appData, options.originalAppData] : [appData];
|
|
50
|
+
for (const root of roots) {
|
|
51
|
+
for (const dataPath of paths) {
|
|
52
|
+
const relative = path.win32.relative(normalize(root), normalize(dataPath));
|
|
53
|
+
const [name] = relative.split(path.win32.sep);
|
|
54
|
+
if (name && name !== '..' && !path.win32.isAbsolute(relative) &&
|
|
55
|
+
name.startsWith(instancePrefix.toLowerCase()) &&
|
|
56
|
+
(options.allExecutables || !relative.includes(path.win32.sep))) {
|
|
57
|
+
running.add(name.slice(instancePrefix.length));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
44
60
|
}
|
|
45
61
|
}
|
|
46
62
|
return { defaultRunning, instances: [...running] };
|
|
47
63
|
}
|
|
48
64
|
|
|
49
|
-
async function
|
|
65
|
+
async function getClaudeProcesses(allUsers) {
|
|
66
|
+
const ownerFilter = allUsers ? '' : `
|
|
67
|
+
$owner = Invoke-CimMethod -InputObject $process -MethodName GetOwnerSid -ErrorAction Stop
|
|
68
|
+
if ($owner.ReturnValue -ne 0) { throw 'Could not determine Claude process owner.' }
|
|
69
|
+
if ($owner.Sid -ne $sid) { continue }
|
|
70
|
+
`;
|
|
50
71
|
const command = `
|
|
51
72
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
52
73
|
$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
|
|
53
74
|
$items = @(foreach ($process in @(Get-CimInstance Win32_Process -Filter "Name = 'claude.exe'" -ErrorAction Stop)) {
|
|
54
75
|
if ($process.CommandLine -match '(?:^|\\s)"?--type(?:=|\\s)') { continue }
|
|
55
|
-
$
|
|
56
|
-
|
|
57
|
-
if ($owner.Sid -eq $sid) { $process | Select-Object ExecutablePath, CommandLine }
|
|
76
|
+
${ownerFilter}
|
|
77
|
+
$process | Select-Object ExecutablePath, CommandLine
|
|
58
78
|
})
|
|
59
79
|
ConvertTo-Json -InputObject $items -Compress
|
|
60
80
|
`;
|
|
@@ -63,7 +83,20 @@ async function getRunningInstances(claudePath, appData) {
|
|
|
63
83
|
timeout: 10000,
|
|
64
84
|
windowsHide: true
|
|
65
85
|
});
|
|
66
|
-
return
|
|
86
|
+
return JSON.parse(stdout);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function getRunningInstances(claudePath, appData) {
|
|
90
|
+
return runningInstances(await getClaudeProcesses(false), claudePath, appData);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function getAllRunningManagedInstances(appData) {
|
|
94
|
+
const root = await fs.promises.realpath(appData);
|
|
95
|
+
return runningInstances(await getClaudeProcesses(true), null, root, {
|
|
96
|
+
allExecutables: true,
|
|
97
|
+
originalAppData: appData,
|
|
98
|
+
resolveInstancePath: directory => fs.realpathSync(directory)
|
|
99
|
+
});
|
|
67
100
|
}
|
|
68
101
|
|
|
69
|
-
module.exports = { listInstances, runningInstances, getRunningInstances };
|
|
102
|
+
module.exports = { instancePrefix, listInstanceEntries, listInstances, runningInstances, getRunningInstances, getAllRunningManagedInstances };
|
package/package.json
CHANGED
package/test/instances.test.js
CHANGED
|
@@ -44,6 +44,25 @@ test('reports unavailable process details rather than claiming instances are sto
|
|
|
44
44
|
assert.throws(() => runningInstances([{ ExecutablePath: 'C:\\claude.exe', CommandLine: 'claude.exe --user-data-dir' }], 'C:\\claude.exe', 'C:\\data'), /could not be determined/);
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
+
test('wipe status checks all Claude executable paths and rejects relative data directories', () => {
|
|
48
|
+
const root = 'C:\\Users\\Test\\AppData\\Roaming';
|
|
49
|
+
const processes = [{
|
|
50
|
+
ExecutablePath: 'C:\\Other\\Claude.exe',
|
|
51
|
+
CommandLine: `Claude.exe --user-data-dir="${root}\\Claude-Profile-Work"`
|
|
52
|
+
}];
|
|
53
|
+
assert.deepEqual(runningInstances(processes, null, root, { allExecutables: true }).instances, ['work']);
|
|
54
|
+
processes[0].CommandLine = `Claude.exe --user-data-dir="${root}\\Claude-Profile-Work\\nested"`;
|
|
55
|
+
assert.deepEqual(runningInstances(processes, null, root, { allExecutables: true }).instances, ['work']);
|
|
56
|
+
assert.deepEqual(runningInstances(processes, null, root, {
|
|
57
|
+
allExecutables: true,
|
|
58
|
+
resolveInstancePath: () => 'C:\\outside'
|
|
59
|
+
}).instances, ['work']);
|
|
60
|
+
assert.throws(() => runningInstances([{
|
|
61
|
+
ExecutablePath: 'C:\\Other\\Claude.exe',
|
|
62
|
+
CommandLine: 'Claude.exe --user-data-dir=relative'
|
|
63
|
+
}], null, root, { allExecutables: true }), /relative data directory/);
|
|
64
|
+
});
|
|
65
|
+
|
|
47
66
|
test('launches the existing default without a custom data directory', () => {
|
|
48
67
|
const handlers = {};
|
|
49
68
|
const launches = [];
|
package/test/shortcut.test.js
CHANGED
|
@@ -21,7 +21,7 @@ function runShortcut({ platform = 'win32', electronExists = true, failure, actio
|
|
|
21
21
|
};
|
|
22
22
|
const sandbox = {
|
|
23
23
|
require: id => id === 'electron' ? electron : id === 'fs' ? { existsSync: () => electronExists } :
|
|
24
|
-
id === 'child_process' ? childProcess : require(id),
|
|
24
|
+
id === 'child_process' ? childProcess : id === './instances' || id === './wipe' ? {} : require(id),
|
|
25
25
|
process,
|
|
26
26
|
console: { log: message => logs.push(message), error: message => errors.push(message) },
|
|
27
27
|
__dirname: path.resolve(__dirname, '..')
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
const test = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const { spawnSync } = require('child_process');
|
|
7
|
+
const { wipeInstances } = require('../wipe');
|
|
8
|
+
|
|
9
|
+
function setup() {
|
|
10
|
+
const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'cim-wipe-test-'));
|
|
11
|
+
const root = path.join(temp, 'appdata');
|
|
12
|
+
fs.mkdirSync(root);
|
|
13
|
+
return { temp, root };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function makeDirectory(root, name) {
|
|
17
|
+
const dir = path.join(root, name);
|
|
18
|
+
fs.mkdirSync(dir);
|
|
19
|
+
fs.writeFileSync(path.join(dir, 'data.txt'), 'keep or delete');
|
|
20
|
+
return dir;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
test('wipes confirmed stopped instances while protecting default, running and linked directories', async () => {
|
|
24
|
+
const { temp, root } = setup();
|
|
25
|
+
const work = makeDirectory(root, 'Claude-Profile-Work');
|
|
26
|
+
const hidden = makeDirectory(root, 'CLAUDE-PROFILE-Hidden');
|
|
27
|
+
const running = makeDirectory(root, 'Claude-Profile-Running');
|
|
28
|
+
const reserved = makeDirectory(root, 'Claude-Profile-Default');
|
|
29
|
+
const defaultData = makeDirectory(root, 'Claude');
|
|
30
|
+
const outside = makeDirectory(temp, 'outside');
|
|
31
|
+
const link = path.join(root, 'Claude-Profile-Link');
|
|
32
|
+
fs.symlinkSync(outside, link, 'junction');
|
|
33
|
+
const logs = [];
|
|
34
|
+
let confirmed = false;
|
|
35
|
+
try {
|
|
36
|
+
await wipeInstances(root, {
|
|
37
|
+
checkRunning: async () => ({ instances: confirmed ? ['running', 'hidden'] : ['running'] }),
|
|
38
|
+
confirm: async ids => {
|
|
39
|
+
assert.deepEqual(new Set(ids), new Set(['Work', 'Hidden']));
|
|
40
|
+
confirmed = true;
|
|
41
|
+
return true;
|
|
42
|
+
},
|
|
43
|
+
log: message => logs.push(message)
|
|
44
|
+
});
|
|
45
|
+
assert.equal(fs.existsSync(work), false);
|
|
46
|
+
for (const dir of [hidden, running, reserved, defaultData, outside, link]) {
|
|
47
|
+
assert.equal(fs.existsSync(path.join(dir, 'data.txt')), true);
|
|
48
|
+
}
|
|
49
|
+
assert.ok(logs.some(message => /Skipped running instance: Running/.test(message)));
|
|
50
|
+
assert.ok(logs.some(message => /Skipped running instance: Hidden/.test(message)));
|
|
51
|
+
assert.ok(logs.some(message => /Claude-Profile-Default/.test(message)));
|
|
52
|
+
} finally {
|
|
53
|
+
fs.rmSync(temp, { recursive: true, force: true });
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('cancellation and unavailable running status never delete data', async () => {
|
|
58
|
+
const { temp, root } = setup();
|
|
59
|
+
const work = makeDirectory(root, 'Claude-Profile-Work');
|
|
60
|
+
try {
|
|
61
|
+
const logs = [];
|
|
62
|
+
await wipeInstances(root, {
|
|
63
|
+
checkRunning: async () => ({ instances: [] }),
|
|
64
|
+
confirm: async () => false,
|
|
65
|
+
log: message => logs.push(message)
|
|
66
|
+
});
|
|
67
|
+
assert.ok(fs.existsSync(work));
|
|
68
|
+
assert.ok(logs.some(message => /cancelled/i.test(message)));
|
|
69
|
+
await assert.rejects(wipeInstances(root, {
|
|
70
|
+
checkRunning: async () => { throw new Error('status unavailable'); },
|
|
71
|
+
confirm: async () => { throw new Error('should not prompt'); },
|
|
72
|
+
log: () => {}
|
|
73
|
+
}), /status unavailable/);
|
|
74
|
+
await assert.rejects(wipeInstances(root, {
|
|
75
|
+
checkRunning: async () => ({ instances: null }),
|
|
76
|
+
confirm: async () => { throw new Error('should not prompt'); },
|
|
77
|
+
log: () => {}
|
|
78
|
+
}), /status is unavailable/);
|
|
79
|
+
assert.ok(fs.existsSync(work));
|
|
80
|
+
} finally {
|
|
81
|
+
fs.rmSync(temp, { recursive: true, force: true });
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('protects Default data linked into managed directories, including links added after confirmation', async () => {
|
|
86
|
+
const { temp, root } = setup();
|
|
87
|
+
const local = path.join(temp, 'local');
|
|
88
|
+
const packaged = path.join(local, 'Claude-Data');
|
|
89
|
+
fs.mkdirSync(packaged, { recursive: true });
|
|
90
|
+
const legacyTarget = makeDirectory(root, 'Claude-Profile-LegacyDefault');
|
|
91
|
+
const packagedTarget = makeDirectory(root, 'Claude-Profile-PackagedDefault');
|
|
92
|
+
const lateTarget = makeDirectory(root, 'Claude-Profile-LateDefault');
|
|
93
|
+
fs.symlinkSync(legacyTarget, path.join(root, 'Claude'), 'junction');
|
|
94
|
+
fs.symlinkSync(packagedTarget, path.join(packaged, 'family'), 'junction');
|
|
95
|
+
const logs = [];
|
|
96
|
+
try {
|
|
97
|
+
await wipeInstances(root, {
|
|
98
|
+
localAppData: local,
|
|
99
|
+
checkRunning: async () => ({ instances: [] }),
|
|
100
|
+
confirm: async ids => {
|
|
101
|
+
assert.deepEqual(ids, ['LateDefault']);
|
|
102
|
+
fs.symlinkSync(lateTarget, path.join(packaged, 'new-family'), 'junction');
|
|
103
|
+
return true;
|
|
104
|
+
},
|
|
105
|
+
log: message => logs.push(message)
|
|
106
|
+
});
|
|
107
|
+
for (const dir of [legacyTarget, packagedTarget, lateTarget]) {
|
|
108
|
+
assert.ok(fs.existsSync(path.join(dir, 'data.txt')));
|
|
109
|
+
}
|
|
110
|
+
assert.equal(logs.filter(message => /Skipped instance containing Default data/.test(message)).length, 3);
|
|
111
|
+
} finally {
|
|
112
|
+
fs.rmSync(temp, { recursive: true, force: true });
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('stops and reports an error if status becomes unavailable after a partial wipe', async () => {
|
|
117
|
+
const { temp, root } = setup();
|
|
118
|
+
makeDirectory(root, 'Claude-Profile-One');
|
|
119
|
+
makeDirectory(root, 'Claude-Profile-Two');
|
|
120
|
+
let checks = 0;
|
|
121
|
+
const logs = [];
|
|
122
|
+
try {
|
|
123
|
+
await assert.rejects(wipeInstances(root, {
|
|
124
|
+
checkRunning: async () => {
|
|
125
|
+
if (++checks === 3) throw new Error('status unavailable');
|
|
126
|
+
return { instances: [] };
|
|
127
|
+
},
|
|
128
|
+
confirm: async () => true,
|
|
129
|
+
log: message => logs.push(message)
|
|
130
|
+
}), /status unavailable/);
|
|
131
|
+
assert.equal(logs.filter(message => message.startsWith('Deleted instance data:')).length, 1);
|
|
132
|
+
assert.equal(fs.readdirSync(root).length, 1);
|
|
133
|
+
} finally {
|
|
134
|
+
fs.rmSync(temp, { recursive: true, force: true });
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('refuses to delete a directory replaced after confirmation', async () => {
|
|
139
|
+
const { temp, root } = setup();
|
|
140
|
+
const work = makeDirectory(root, 'Claude-Profile-Work');
|
|
141
|
+
const original = path.join(root, 'backup');
|
|
142
|
+
try {
|
|
143
|
+
await assert.rejects(wipeInstances(root, {
|
|
144
|
+
checkRunning: async () => ({ instances: [] }),
|
|
145
|
+
confirm: async () => {
|
|
146
|
+
fs.renameSync(work, original);
|
|
147
|
+
makeDirectory(root, 'Claude-Profile-Work');
|
|
148
|
+
return true;
|
|
149
|
+
},
|
|
150
|
+
log: () => {}
|
|
151
|
+
}), /changed after confirmation/);
|
|
152
|
+
assert.ok(fs.existsSync(path.join(work, 'data.txt')));
|
|
153
|
+
assert.ok(fs.existsSync(path.join(original, 'data.txt')));
|
|
154
|
+
} finally {
|
|
155
|
+
fs.rmSync(temp, { recursive: true, force: true });
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test('CLI refuses a wipe without an interactive terminal', { skip: process.platform !== 'win32' }, () => {
|
|
160
|
+
const { temp, root } = setup();
|
|
161
|
+
const work = makeDirectory(root, 'Claude-Profile-Work');
|
|
162
|
+
try {
|
|
163
|
+
const result = spawnSync(process.execPath, [path.join(__dirname, '..', 'cli.js'), 'wipe-instances'], {
|
|
164
|
+
encoding: 'utf8',
|
|
165
|
+
env: { ...process.env, APPDATA: root }
|
|
166
|
+
});
|
|
167
|
+
assert.equal(result.status, 1);
|
|
168
|
+
assert.match(result.stderr, /interactive terminal/);
|
|
169
|
+
assert.ok(fs.existsSync(work));
|
|
170
|
+
} finally {
|
|
171
|
+
fs.rmSync(temp, { recursive: true, force: true });
|
|
172
|
+
}
|
|
173
|
+
});
|
package/wipe.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { instancePrefix, listInstanceEntries } = require('./instances');
|
|
4
|
+
|
|
5
|
+
async function directoryIdentity(directory, root) {
|
|
6
|
+
const stat = await fs.promises.lstat(directory, { bigint: true });
|
|
7
|
+
if (!stat.isDirectory() || stat.isSymbolicLink() || stat.ino === 0n || stat.dev === 0n) {
|
|
8
|
+
throw new Error('An instance directory cannot be verified safely.');
|
|
9
|
+
}
|
|
10
|
+
const resolved = await fs.promises.realpath(directory);
|
|
11
|
+
if (path.normalize(path.dirname(resolved)).toLowerCase() !== path.normalize(root).toLowerCase()) {
|
|
12
|
+
throw new Error('An instance directory is outside the managed data directory.');
|
|
13
|
+
}
|
|
14
|
+
return { dev: stat.dev, ino: stat.ino };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function defaultDataPaths(appData, localAppData) {
|
|
18
|
+
const paths = [path.join(appData, 'Claude')];
|
|
19
|
+
if (localAppData) {
|
|
20
|
+
const packaged = path.join(localAppData, 'Claude-Data');
|
|
21
|
+
let stat;
|
|
22
|
+
try {
|
|
23
|
+
stat = await fs.promises.lstat(packaged);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (error.code !== 'ENOENT') throw error;
|
|
26
|
+
}
|
|
27
|
+
if (stat) {
|
|
28
|
+
paths.push(packaged);
|
|
29
|
+
for (const name of await fs.promises.readdir(packaged)) paths.push(path.join(packaged, name));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const resolved = [];
|
|
33
|
+
for (const directory of paths) {
|
|
34
|
+
try {
|
|
35
|
+
await fs.promises.lstat(directory);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (error.code === 'ENOENT') continue;
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
resolved.push(await fs.promises.realpath(directory));
|
|
41
|
+
}
|
|
42
|
+
return resolved;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function containsPath(directory, target) {
|
|
46
|
+
const relative = path.relative(directory.toLowerCase(), target.toLowerCase());
|
|
47
|
+
return relative === '' || (relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function wipeInstances(appData, { checkRunning, confirm, log, localAppData }) {
|
|
51
|
+
if (typeof appData !== 'string' || !path.isAbsolute(appData)) {
|
|
52
|
+
throw new Error('The Windows application data directory is unavailable.');
|
|
53
|
+
}
|
|
54
|
+
if (localAppData && !path.isAbsolute(localAppData)) {
|
|
55
|
+
throw new Error('The Windows local application data directory is unavailable.');
|
|
56
|
+
}
|
|
57
|
+
const root = await fs.promises.realpath(appData);
|
|
58
|
+
const defaultPaths = await defaultDataPaths(appData, localAppData);
|
|
59
|
+
const entries = listInstanceEntries(appData);
|
|
60
|
+
const candidates = [];
|
|
61
|
+
for (const entry of entries) {
|
|
62
|
+
const id = entry.name.slice(instancePrefix.length);
|
|
63
|
+
if (!entry.isDirectory() || !id || id.toLowerCase() === 'default') {
|
|
64
|
+
log(`Skipped protected or non-directory entry: ${entry.name}`);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const directory = path.join(appData, entry.name);
|
|
68
|
+
const identity = await directoryIdentity(directory, root);
|
|
69
|
+
if (defaultPaths.some(target => containsPath(path.join(root, entry.name), target))) {
|
|
70
|
+
log(`Skipped instance containing Default data: ${id}`);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
candidates.push({ id, directory, identity });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!candidates.length) {
|
|
77
|
+
log('No managed instance directories to wipe. Default was not touched.');
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function runningIds() {
|
|
82
|
+
const state = await checkRunning();
|
|
83
|
+
if (!state || !Array.isArray(state.instances) || !state.instances.every(id => typeof id === 'string')) {
|
|
84
|
+
throw new Error('Running instance status is unavailable.');
|
|
85
|
+
}
|
|
86
|
+
return new Set(state.instances.map(id => id.toLowerCase()));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const running = await runningIds();
|
|
90
|
+
const stopped = candidates.filter(candidate => {
|
|
91
|
+
if (!running.has(candidate.id.toLowerCase())) return true;
|
|
92
|
+
log(`Skipped running instance: ${candidate.id}`);
|
|
93
|
+
return false;
|
|
94
|
+
});
|
|
95
|
+
if (!stopped.length) {
|
|
96
|
+
log('No stopped managed instances to wipe. Default was not touched.');
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (await confirm(stopped.map(candidate => candidate.id)) !== true) {
|
|
101
|
+
log('Wipe cancelled. No instance data was deleted.');
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let deleted = 0;
|
|
106
|
+
for (const candidate of stopped) {
|
|
107
|
+
const currentRunning = await runningIds();
|
|
108
|
+
if (currentRunning.has(candidate.id.toLowerCase())) {
|
|
109
|
+
log(`Skipped running instance: ${candidate.id}`);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const identity = await directoryIdentity(candidate.directory, root);
|
|
113
|
+
if (identity.dev !== candidate.identity.dev || identity.ino !== candidate.identity.ino) {
|
|
114
|
+
throw new Error(`Instance "${candidate.id}" changed after confirmation; stopped without deleting it.`);
|
|
115
|
+
}
|
|
116
|
+
const currentDefaultPaths = await defaultDataPaths(appData, localAppData);
|
|
117
|
+
if (currentDefaultPaths.some(target => containsPath(path.join(root, path.basename(candidate.directory)), target))) {
|
|
118
|
+
log(`Skipped instance containing Default data: ${candidate.id}`);
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
await fs.promises.rm(candidate.directory, { recursive: true });
|
|
122
|
+
deleted++;
|
|
123
|
+
log(`Deleted instance data: ${candidate.id}`);
|
|
124
|
+
}
|
|
125
|
+
log(`Wiped ${deleted} managed instance(s). Default was not touched.`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports = { wipeInstances };
|