@jini-ai/desktop-host 0.7.3-next.1 → 0.7.3-next.3
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/CHANGELOG.md +6 -0
- package/README.md +217 -1
- package/dist/electron/electron-shell.js +5 -7
- package/dist/electron/handoff/index.d.ts +54 -0
- package/dist/electron/handoff/index.js +32 -0
- package/dist/electron/memory-sampler.d.ts +28 -0
- package/dist/electron/memory-sampler.js +41 -0
- package/dist/electron/partitions/cache-maintenance.d.ts +280 -0
- package/dist/electron/partitions/cache-maintenance.js +239 -0
- package/dist/electron/partitions/claim-sources.d.ts +54 -0
- package/dist/electron/partitions/claim-sources.js +36 -0
- package/dist/electron/partitions/holder-registry.d.ts +116 -0
- package/dist/electron/partitions/holder-registry.js +130 -0
- package/dist/electron/partitions/index.d.ts +10 -0
- package/dist/electron/partitions/index.js +10 -0
- package/dist/electron/partitions/maintenance-marker.d.ts +61 -0
- package/dist/electron/partitions/maintenance-marker.js +85 -0
- package/dist/electron/partitions/partition-inventory.d.ts +23 -0
- package/dist/electron/partitions/partition-inventory.js +19 -0
- package/dist/electron/partitions/partition-pass.d.ts +114 -0
- package/dist/electron/partitions/partition-pass.js +414 -0
- package/dist/electron/partitions/partition-root.d.ts +42 -0
- package/dist/electron/partitions/partition-root.js +52 -0
- package/dist/electron/partitions/profile-interlock.d.ts +141 -0
- package/dist/electron/partitions/profile-interlock.js +273 -0
- package/dist/electron/partitions/retirement-ledger.d.ts +116 -0
- package/dist/electron/partitions/retirement-ledger.js +149 -0
- package/dist/electron/preferences/index.d.ts +5 -0
- package/dist/electron/preferences/index.js +5 -0
- package/dist/electron/preferences/ipc.d.ts +35 -0
- package/dist/electron/preferences/ipc.js +21 -0
- package/dist/electron/preferences/menu.d.ts +103 -0
- package/dist/electron/preferences/menu.js +67 -0
- package/dist/electron/preferences/preference-file.d.ts +63 -0
- package/dist/electron/preferences/preference-file.js +51 -0
- package/dist/electron/preferences/values.d.ts +25 -0
- package/dist/electron/preferences/values.js +18 -0
- package/dist/electron/preload/index.d.ts +3 -0
- package/dist/electron/preload/index.js +2 -0
- package/dist/electron/preload/ipc-subscribe.d.ts +34 -0
- package/dist/electron/preload/ipc-subscribe.js +18 -0
- package/dist/electron/selftest/index.d.ts +3 -0
- package/dist/electron/selftest/index.js +3 -0
- package/dist/electron/selftest/selftest-tracker.d.ts +82 -0
- package/dist/electron/selftest/selftest-tracker.js +67 -0
- package/dist/electron/session-bootstrap/exchange.d.ts +50 -0
- package/dist/electron/session-bootstrap/exchange.js +41 -0
- package/dist/electron/session-bootstrap/header-arming.d.ts +18 -0
- package/dist/electron/session-bootstrap/header-arming.js +13 -0
- package/dist/electron/session-bootstrap/index.d.ts +11 -0
- package/dist/electron/session-bootstrap/index.js +10 -0
- package/dist/electron/session-bootstrap/partition.d.ts +17 -0
- package/dist/electron/session-bootstrap/partition.js +24 -0
- package/dist/electron/session-bootstrap/probe.d.ts +40 -0
- package/dist/electron/session-bootstrap/probe.js +29 -0
- package/dist/electron/session-bootstrap/types.d.ts +62 -0
- package/dist/electron/session-bootstrap/types.js +7 -0
- package/dist/electron/updates/desktop-updater.d.ts +90 -0
- package/dist/electron/updates/desktop-updater.js +124 -0
- package/dist/electron/updates/index.d.ts +4 -0
- package/dist/electron/updates/index.js +4 -0
- package/dist/electron/updates/instance-presence-strict.d.ts +27 -0
- package/dist/electron/updates/instance-presence-strict.js +46 -0
- package/dist/electron/updates/native-notices.d.ts +89 -0
- package/dist/electron/updates/native-notices.js +41 -0
- package/dist/electron/updates/update-check-now.d.ts +117 -0
- package/dist/electron/updates/update-check-now.js +88 -0
- package/dist/electron/usability/find-in-page-ipc.d.ts +3 -6
- package/dist/electron/usability/window-bounds-store.js +14 -12
- package/dist/electron/usability/zoom-menu.d.ts +1 -1
- package/dist/electron/webview/index.d.ts +2 -0
- package/dist/electron/webview/index.js +2 -0
- package/dist/electron/webview/webview-types.d.ts +66 -0
- package/dist/electron/webview/webview-types.js +12 -0
- package/dist/electron/windows/app-window.d.ts +83 -0
- package/dist/electron/windows/app-window.js +62 -0
- package/dist/electron/windows/dock-icon.d.ts +34 -0
- package/dist/electron/windows/dock-icon.js +32 -0
- package/dist/electron/windows/index.d.ts +3 -0
- package/dist/electron/windows/index.js +3 -0
- package/dist/logging.js +3 -1
- package/dist/node-toolchain/boot-setup.d.ts +63 -0
- package/dist/node-toolchain/boot-setup.js +59 -0
- package/dist/node-toolchain/index.d.ts +2 -0
- package/dist/node-toolchain/index.js +1 -0
- package/dist/shell/auto-update-controller.d.ts +34 -0
- package/dist/shell/auto-update-controller.js +27 -0
- package/dist/shell/copied-site-databases.d.ts +71 -0
- package/dist/shell/copied-site-databases.js +60 -0
- package/dist/shell/find-in-page-verify.d.ts +33 -0
- package/dist/shell/find-in-page-verify.js +30 -0
- package/dist/shell/find-menu.d.ts +33 -0
- package/dist/shell/find-menu.js +36 -0
- package/dist/shell/guest-navigation.d.ts +47 -0
- package/dist/shell/guest-navigation.js +58 -0
- package/dist/shell/instance-presence.d.ts +20 -0
- package/dist/shell/instance-presence.js +26 -0
- package/dist/shell/js-backslide-guard.d.ts +42 -0
- package/dist/shell/js-backslide-guard.js +44 -0
- package/dist/shell/node-toolchain.d.ts +34 -0
- package/dist/shell/node-toolchain.js +50 -0
- package/dist/shell/pty-helpers.d.ts +25 -0
- package/dist/shell/pty-helpers.js +50 -0
- package/dist/shell/site-presence.d.ts +32 -0
- package/dist/shell/site-presence.js +42 -0
- package/dist/shell/speech/mac-on-device-transcriber.d.ts +24 -0
- package/dist/shell/speech/mac-on-device-transcriber.js +81 -0
- package/dist/shell/speech/speech-ipc.d.ts +52 -0
- package/dist/shell/speech/speech-ipc.js +40 -0
- package/dist/shell/spellcheck-menu.d.ts +84 -0
- package/dist/shell/spellcheck-menu.js +63 -0
- package/dist/shell/supervised-site-url.d.ts +20 -0
- package/dist/shell/supervised-site-url.js +22 -0
- package/dist/speech/macos/mac-on-device-transcriber.d.ts +2 -1
- package/dist/speech/speech-bridge.d.ts +1 -8
- package/dist/speech/transcription-port.d.ts +1 -9
- package/dist/tauri/tauri-shell.js +8 -10
- package/package.json +154 -7
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { listCandidates } from './partition-root.js';
|
|
3
|
+
import { isAmbiguousPartitionEligible } from './partition-inventory.js';
|
|
4
|
+
import { partitionDirName } from './profile-interlock.js';
|
|
5
|
+
export const DEFAULT_ASIDE_DIR_NAME = '.removing';
|
|
6
|
+
const CACHE_DIRS = ['Cache', 'Code Cache'];
|
|
7
|
+
const CACHE_ASIDE = /^(.+)\.(?:Cache|Code Cache)\.[0-9a-f]+$/;
|
|
8
|
+
const WHOLE_ASIDE = /^(.+)\.[0-9a-f]+$/;
|
|
9
|
+
const HOLD_PREFIX = 'hold-';
|
|
10
|
+
const code = (error) => error?.code;
|
|
11
|
+
const describe = (error) => String(code(error) ?? (error instanceof Error ? error.message : error));
|
|
12
|
+
const emptyReport = () => ({ result: 'done', removed: [], cacheCleared: [], restored: [], kept: [], skipped: [] });
|
|
13
|
+
async function lstatOr(fs, target) {
|
|
14
|
+
try {
|
|
15
|
+
return await fs.lstat({ path: target });
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
return code(error) === 'ENOENT' ? 'absent' : 'error';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** Absence counts only when the recorded parent is present on the recorded device and the child is ENOENT. */
|
|
22
|
+
async function absentForSure(fs, rec) {
|
|
23
|
+
const parent = await lstatOr(fs, rec.parentDir);
|
|
24
|
+
if (typeof parent === 'string' || !parent.isDirectory() || parent.dev !== rec.parentDev)
|
|
25
|
+
return 'uncertain';
|
|
26
|
+
const subject = await lstatOr(fs, rec.subjectDir);
|
|
27
|
+
if (subject === 'absent')
|
|
28
|
+
return 'absent';
|
|
29
|
+
return subject === 'error' ? 'uncertain' : 'exists';
|
|
30
|
+
}
|
|
31
|
+
function absenceVerdict(absence) {
|
|
32
|
+
if (absence === 'absent')
|
|
33
|
+
return null;
|
|
34
|
+
return absence === 'exists' ? { to: 'invalidated', reason: 'site-exists' } : { to: 'pending', reason: 'absence-uncertain' };
|
|
35
|
+
}
|
|
36
|
+
function safeTransition(S, input) {
|
|
37
|
+
try {
|
|
38
|
+
return S.ledger.transition(input, {}).state === 'ok';
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function settle(S, rec, verdict) {
|
|
45
|
+
safeTransition(S, { id: rec.id, from: 'pending', to: verdict.to, reason: verdict.reason });
|
|
46
|
+
S.report.kept.push({ partition: rec.partition, reason: verdict.reason });
|
|
47
|
+
}
|
|
48
|
+
async function heldNow(S, partition) {
|
|
49
|
+
const holders = await S.interlock.readHoldersStrict();
|
|
50
|
+
return holders.state === 'unknown' || holders.value.some(h => h.intents.includes(partition) || h.held.includes(partition));
|
|
51
|
+
}
|
|
52
|
+
/** A fresh strict read of every claim source; anything other than "no claim at all" keeps the partition. */
|
|
53
|
+
async function freshClaimVerdict(S, partition) {
|
|
54
|
+
const claims = await S.ctx.readClaims();
|
|
55
|
+
if (claims.state === 'unknown')
|
|
56
|
+
return { to: 'pending', reason: 'unknown-claims' };
|
|
57
|
+
if (claims.otherInstances.length > 0)
|
|
58
|
+
return { to: 'pending', reason: 'other-instance' };
|
|
59
|
+
if (!claims.claimed.includes(partition))
|
|
60
|
+
return null;
|
|
61
|
+
return { to: 'pending', reason: (await heldNow(S, partition)) ? 'held' : 'tracked' };
|
|
62
|
+
}
|
|
63
|
+
async function renameAside(S, from, to) {
|
|
64
|
+
try {
|
|
65
|
+
await S.ctx.assertHeld();
|
|
66
|
+
await S.fs.mkdir({ path: S.asideRoot });
|
|
67
|
+
await S.fs.rename({ oldPath: from, newPath: to });
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** Puts an uncommitted aside back. If something now occupies the name, the aside becomes a protected hold. */
|
|
75
|
+
async function restoreAside(S, partition, asidePath) {
|
|
76
|
+
try {
|
|
77
|
+
await S.ctx.setTarget({ target: partition });
|
|
78
|
+
const occupied = await lstatOr(S.fs, path.join(S.root, partition));
|
|
79
|
+
const dest = occupied === 'absent' ? path.join(S.root, partition) : path.join(S.asideRoot, `${HOLD_PREFIX}${path.basename(asidePath)}`);
|
|
80
|
+
await S.fs.rename({ oldPath: asidePath, newPath: dest });
|
|
81
|
+
if (occupied === 'absent')
|
|
82
|
+
S.report.restored.push(partition);
|
|
83
|
+
else
|
|
84
|
+
S.report.skipped.push({ name: path.basename(dest), reason: 'hold' });
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
S.report.skipped.push({ name: path.basename(asidePath), reason: `restore-failed (${describe(error)})` });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Shared by automatic and owner removal: record the aside name, rename, recheck, then commit by CAS on
|
|
92
|
+
* the record ID and exact aside name. The CAS is the only commit point; anything else restores.
|
|
93
|
+
*/
|
|
94
|
+
async function asideAndCommit(S, rec, recheck) {
|
|
95
|
+
const aside = `${rec.partition}.${S.newToken()}`, asidePath = path.join(S.asideRoot, aside);
|
|
96
|
+
if (!safeTransition(S, { id: rec.id, from: 'pending', to: 'pending', reason: 'renaming', asideIdentity: aside })) {
|
|
97
|
+
S.report.kept.push({ partition: rec.partition, reason: 'ledger-unavailable' });
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (!(await renameAside(S, path.join(S.root, rec.partition), asidePath))) {
|
|
101
|
+
settle(S, rec, { to: 'pending', reason: 'busy' });
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
let committed = false;
|
|
105
|
+
try {
|
|
106
|
+
const verdict = await recheck();
|
|
107
|
+
if (verdict)
|
|
108
|
+
settle(S, rec, verdict);
|
|
109
|
+
else
|
|
110
|
+
committed = safeTransition(S, { id: rec.id, from: 'pending', to: 'removed', asideIdentity: aside });
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
S.report.skipped.push({ name: rec.partition, reason: `recheck-failed (${describe(error)})` });
|
|
114
|
+
}
|
|
115
|
+
if (!committed) {
|
|
116
|
+
await restoreAside(S, rec.partition, asidePath);
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
S.deleteQueue.push(asidePath);
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
async function precheck(S, rec) {
|
|
123
|
+
if (rec.basis !== 'erased')
|
|
124
|
+
return { to: 'invalidated', reason: 'owner-interrupted' };
|
|
125
|
+
if (rec.profile !== S.ledger.profile || rec.sessionRoot !== S.root)
|
|
126
|
+
return { to: 'invalidated', reason: 'profile-mismatch' };
|
|
127
|
+
if (S.others.length > 0)
|
|
128
|
+
return { to: 'pending', reason: 'other-instance' };
|
|
129
|
+
if (S.claimed.has(rec.partition))
|
|
130
|
+
return { to: 'pending', reason: S.held.has(rec.partition) ? 'held' : 'tracked' };
|
|
131
|
+
return absenceVerdict(await absentForSure(S.fs, rec));
|
|
132
|
+
}
|
|
133
|
+
async function recheckRetired(S, rec) {
|
|
134
|
+
return (await freshClaimVerdict(S, rec.partition)) ?? absenceVerdict(await absentForSure(S.fs, rec));
|
|
135
|
+
}
|
|
136
|
+
async function retireOne(S, rec) {
|
|
137
|
+
const verdict = await precheck(S, rec);
|
|
138
|
+
if (verdict || rec.basis !== 'erased') {
|
|
139
|
+
settle(S, rec, verdict ?? { to: 'invalidated', reason: 'owner-interrupted' });
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
await S.ctx.setTarget({ target: rec.partition });
|
|
143
|
+
// Dekker read: the target is on disk before claims are re-read, and an acquirer publishes its intent
|
|
144
|
+
// before it reads the target, so at least one side sees the other.
|
|
145
|
+
const fresh = await freshClaimVerdict(S, rec.partition);
|
|
146
|
+
if (fresh)
|
|
147
|
+
settle(S, rec, fresh);
|
|
148
|
+
else if (await asideAndCommit(S, rec, () => recheckRetired(S, rec)))
|
|
149
|
+
S.report.removed.push(rec.partition);
|
|
150
|
+
await S.ctx.setTarget({ target: '*' });
|
|
151
|
+
}
|
|
152
|
+
/** Ambiguous partitions lose only measured cache folders; sign-in stores are never touched. */
|
|
153
|
+
async function cleanAmbiguous(S, partition) {
|
|
154
|
+
for (const sub of CACHE_DIRS) {
|
|
155
|
+
const from = path.join(S.root, partition, sub);
|
|
156
|
+
const stat = await lstatOr(S.fs, from);
|
|
157
|
+
if (typeof stat === 'string' || stat.isSymbolicLink() || !stat.isDirectory())
|
|
158
|
+
continue;
|
|
159
|
+
await S.ctx.setTarget({ target: partition });
|
|
160
|
+
if (await freshClaimVerdict(S, partition)) {
|
|
161
|
+
S.report.kept.push({ partition, reason: 'held' });
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const to = path.join(S.asideRoot, `${partition}.${sub}.${S.newToken()}`);
|
|
165
|
+
if (!(await renameAside(S, from, to))) {
|
|
166
|
+
S.report.skipped.push({ name: `${partition}/${sub}`, reason: 'busy' });
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
S.deleteQueue.push(to);
|
|
170
|
+
if (!S.report.cacheCleared.includes(partition))
|
|
171
|
+
S.report.cacheCleared.push(partition);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
async function runMaintenance(S, partition, classification, maintenance) {
|
|
175
|
+
if (S.others.length > 0)
|
|
176
|
+
return;
|
|
177
|
+
await S.ctx.setTarget({ target: partition });
|
|
178
|
+
if (await heldNow(S, partition))
|
|
179
|
+
return;
|
|
180
|
+
try {
|
|
181
|
+
await maintenance({ partition, path: path.join(S.root, partition), classification, writer: S.ctx });
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
S.report.skipped.push({ name: partition, reason: `maintenance-failed (${describe(error)})` });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
async function classifyOne(S, candidate, maintenance) {
|
|
188
|
+
const p = candidate.name;
|
|
189
|
+
const rec = S.records.find(r => r.state === 'pending' && r.partition === p);
|
|
190
|
+
if (S.claimed.has(p)) {
|
|
191
|
+
const reason = S.held.has(p) ? 'held' : 'tracked';
|
|
192
|
+
if (rec)
|
|
193
|
+
settle(S, rec, { to: 'pending', reason });
|
|
194
|
+
else
|
|
195
|
+
S.report.kept.push({ partition: p, reason });
|
|
196
|
+
if (reason === 'tracked')
|
|
197
|
+
await runMaintenance(S, p, 'tracked', maintenance);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (rec) {
|
|
201
|
+
await retireOne(S, rec);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (S.others.length > 0) {
|
|
205
|
+
S.report.kept.push({ partition: p, reason: 'other-instance' });
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
await cleanAmbiguous(S, p);
|
|
209
|
+
await runMaintenance(S, p, 'ambiguous', maintenance);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* An aside folder is not deletion authority: its name says which partition it came from, not that the
|
|
213
|
+
* removal was proven. Only a `removed` record naming this exact folder, or a cache-only aside name,
|
|
214
|
+
* lets it be deleted; every other whole aside is restored, and unrecognised entries are left alone.
|
|
215
|
+
*/
|
|
216
|
+
async function sweepOne(S, name) {
|
|
217
|
+
const full = path.join(S.asideRoot, name);
|
|
218
|
+
if (name.startsWith(HOLD_PREFIX)) {
|
|
219
|
+
S.report.skipped.push({ name, reason: 'hold' });
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const cache = CACHE_ASIDE.exec(name);
|
|
223
|
+
if (cache && S.isManaged(cache[1])) {
|
|
224
|
+
S.deleteQueue.push(full);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const whole = WHOLE_ASIDE.exec(name);
|
|
228
|
+
if (!whole || !S.isManaged(whole[1])) {
|
|
229
|
+
S.report.skipped.push({ name, reason: 'unknown-aside' });
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const committed = S.records.some(r => r.state === 'removed' && r.partition === whole[1] && r.asideIdentity === name);
|
|
233
|
+
if (committed)
|
|
234
|
+
S.deleteQueue.push(full);
|
|
235
|
+
else
|
|
236
|
+
await restoreAside(S, whole[1], full);
|
|
237
|
+
}
|
|
238
|
+
async function sweepAsides(S) {
|
|
239
|
+
let names;
|
|
240
|
+
try {
|
|
241
|
+
names = await S.fs.readdir({ path: S.asideRoot });
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
if (code(error) !== 'ENOENT')
|
|
245
|
+
S.report.skipped.push({ name: S.asideDirName, reason: 'unreadable' });
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
for (const name of names)
|
|
249
|
+
await sweepOne(S, name);
|
|
250
|
+
}
|
|
251
|
+
async function loadClaims(S) {
|
|
252
|
+
const claims = await S.ctx.readClaims();
|
|
253
|
+
const holders = await S.interlock.readHoldersStrict();
|
|
254
|
+
if (claims.state === 'unknown' || holders.state === 'unknown')
|
|
255
|
+
return false;
|
|
256
|
+
claims.claimed.forEach(p => S.claimed.add(p));
|
|
257
|
+
S.others.push(...claims.otherInstances);
|
|
258
|
+
for (const h of holders.value)
|
|
259
|
+
[...h.intents, ...h.held].forEach(p => S.held.add(p));
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
/** A damaged ledger leaves the retired class empty: no proof, so nothing is removed whole. */
|
|
263
|
+
function readRecords(shared, report) {
|
|
264
|
+
const read = shared.ledger.readStrict({}, {});
|
|
265
|
+
if (read.state === 'ok')
|
|
266
|
+
return read.records;
|
|
267
|
+
report.skipped.push({ name: 'ledger', reason: read.reason });
|
|
268
|
+
return [];
|
|
269
|
+
}
|
|
270
|
+
function openPass(shared, ctx, root, report, deleteQueue) {
|
|
271
|
+
return {
|
|
272
|
+
...shared, ctx, root, asideRoot: path.join(root, shared.asideDirName), records: readRecords(shared, report),
|
|
273
|
+
others: [], claimed: new Set(), held: new Set(), report, deleteQueue,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
const skip = (report, reason) => Object.assign(report, { result: 'skipped', reason });
|
|
277
|
+
async function passUnderWriter(shared, ctx, report, deleteQueue, maintenance) {
|
|
278
|
+
const rootRead = await ctx.resolveRoot();
|
|
279
|
+
if (rootRead.state === 'absent')
|
|
280
|
+
return;
|
|
281
|
+
if (rootRead.state === 'unknown') {
|
|
282
|
+
skip(report, 'unknown-root');
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const S = openPass(shared, ctx, rootRead.root, report, deleteQueue);
|
|
286
|
+
// Restoring uncommitted asides first is always safe and must not wait for claims to become readable.
|
|
287
|
+
await sweepAsides(S);
|
|
288
|
+
if (!(await loadClaims(S))) {
|
|
289
|
+
skip(report, 'unknown-claims');
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
const list = await listCandidates({ root: S.root, fs: S.fs, isManaged: S.isManaged });
|
|
293
|
+
if (list.state === 'unknown') {
|
|
294
|
+
skip(report, 'unknown-root');
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
report.skipped.push(...list.skipped);
|
|
298
|
+
for (const candidate of list.candidates)
|
|
299
|
+
await classifyOne(S, candidate, maintenance);
|
|
300
|
+
}
|
|
301
|
+
/** Deletion happens outside the writer lock, and only for committed or cache-only asides. */
|
|
302
|
+
async function drainDeleteQueue(fs, queue, report) {
|
|
303
|
+
for (const target of queue) {
|
|
304
|
+
try {
|
|
305
|
+
await fs.rm({ path: target });
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
report.skipped.push({ name: path.basename(target), reason: `delete-failed (${describe(error)})` });
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
const sharedFrom = (r, asideDirName) => ({ interlock: r.interlock, ledger: r.ledger, fs: r.fs, isManaged: name => r.root.isManaged(name), newToken: r.newToken, asideDirName });
|
|
313
|
+
async function executePass(required, asideDirName) {
|
|
314
|
+
const report = emptyReport(), deleteQueue = [];
|
|
315
|
+
const shared = sharedFrom(required, asideDirName);
|
|
316
|
+
try {
|
|
317
|
+
const outcome = await required.interlock.withPartitionWriter({ run: ctx => passUnderWriter(shared, ctx, report, deleteQueue, required.maintenance) });
|
|
318
|
+
if (outcome.state === 'skipped')
|
|
319
|
+
skip(report, outcome.reason);
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
Object.assign(report, { result: 'failed', reason: describe(error) });
|
|
323
|
+
}
|
|
324
|
+
await drainDeleteQueue(required.fs, deleteQueue, report);
|
|
325
|
+
required.report({ report });
|
|
326
|
+
return report;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Next-start partition pass: restore uncommitted asides, then remove proven-retired partitions whole and
|
|
330
|
+
* clear cache folders of ambiguous ones. Resolves at the budget at the latest; the pass keeps running.
|
|
331
|
+
* @complexity O(candidates + asides) fs operations.
|
|
332
|
+
*/
|
|
333
|
+
export async function runPartitionPass(required, { budgetMs = 3000, asideDirName = DEFAULT_ASIDE_DIR_NAME } = {}) {
|
|
334
|
+
const done = executePass(required, asideDirName);
|
|
335
|
+
const budget = required.scheduler.sleep({ ms: budgetMs }).then(() => ({ state: 'continuing', done }));
|
|
336
|
+
return Promise.race([done.then(report => ({ state: 'finished', report })), budget]);
|
|
337
|
+
}
|
|
338
|
+
function isConfirmed(confirmation, name) {
|
|
339
|
+
const c = confirmation;
|
|
340
|
+
return c?.confirmed === true && c.evidence === 'owner-dialog' && typeof c.partition === 'string' && partitionDirName({ partition: c.partition }) === name;
|
|
341
|
+
}
|
|
342
|
+
const keptBecause = (reason) => {
|
|
343
|
+
if (reason === 'busy')
|
|
344
|
+
return { state: 'kept', reason: 'busy' };
|
|
345
|
+
if (reason === 'ledger-unavailable' || reason === 'unknown-claims')
|
|
346
|
+
return { state: 'kept', reason: 'unknown' };
|
|
347
|
+
return { state: 'kept', reason: 'protected' };
|
|
348
|
+
};
|
|
349
|
+
/** Pre-write checks for owner-confirmed removal, in order: claims readable, no other holders, not claimed, a real directory. `null` means proceed. */
|
|
350
|
+
async function manualPreflight(S, partition) {
|
|
351
|
+
if (!(await loadClaims(S)))
|
|
352
|
+
return { state: 'kept', reason: 'unknown' };
|
|
353
|
+
const ledger = S.ledger.readStrict({}, {});
|
|
354
|
+
if (ledger.state === 'unknown')
|
|
355
|
+
return { state: 'kept', reason: 'unknown' };
|
|
356
|
+
if (S.others.length > 0)
|
|
357
|
+
return { state: 'kept', reason: 'busy' };
|
|
358
|
+
const claims = { state: 'ok', claimed: [...S.claimed, ...S.held], otherInstances: S.others };
|
|
359
|
+
if (!isAmbiguousPartitionEligible({ partition, claims, ledger }))
|
|
360
|
+
return { state: 'kept', reason: 'protected' };
|
|
361
|
+
const list = await listCandidates({ root: S.root, fs: S.fs, isManaged: S.isManaged });
|
|
362
|
+
if (list.state === 'unknown')
|
|
363
|
+
return { state: 'kept', reason: 'unknown' };
|
|
364
|
+
if (!list.candidates.some(candidate => candidate.name === partition))
|
|
365
|
+
return { state: 'kept', reason: 'missing' };
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
async function manualUnderWriter(shared, ctx, partition, deleteQueue) {
|
|
369
|
+
const rootRead = await ctx.resolveRoot();
|
|
370
|
+
if (rootRead.state !== 'ok')
|
|
371
|
+
return { state: 'kept', reason: rootRead.state === 'absent' ? 'missing' : 'unknown' };
|
|
372
|
+
const S = openPass(shared, ctx, rootRead.root, emptyReport(), deleteQueue);
|
|
373
|
+
const notRemovable = await manualPreflight(S, partition);
|
|
374
|
+
if (notRemovable)
|
|
375
|
+
return notRemovable;
|
|
376
|
+
await ctx.setTarget({ target: partition });
|
|
377
|
+
const fresh = await freshClaimVerdict(S, partition);
|
|
378
|
+
if (fresh)
|
|
379
|
+
return keptBecause(fresh.reason);
|
|
380
|
+
const rec = S.ledger.append({ candidate: { basis: 'owner-dialog', profile: S.ledger.profile, sessionRoot: S.root, partition } }, {});
|
|
381
|
+
const recheck = async () => {
|
|
382
|
+
const verdict = await freshClaimVerdict(S, partition);
|
|
383
|
+
return verdict && { to: 'invalidated', reason: verdict.reason };
|
|
384
|
+
};
|
|
385
|
+
if (await asideAndCommit(S, rec, recheck))
|
|
386
|
+
return { state: 'removed' };
|
|
387
|
+
return keptBecause(S.report.kept.at(-1)?.reason);
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Owner-confirmed removal of one ambiguous partition (its sign-ins), with the same fence, rename,
|
|
391
|
+
* post-rename recheck and CAS commit as automatic removal. A missing or mismatched confirmation is refused.
|
|
392
|
+
* @complexity O(1) partitions; O(claims) per recheck.
|
|
393
|
+
*/
|
|
394
|
+
export async function removeAmbiguousPartition(required, { asideDirName = DEFAULT_ASIDE_DIR_NAME } = {}) {
|
|
395
|
+
const name = partitionDirName({ partition: required.partition });
|
|
396
|
+
if (!isConfirmed(required.confirmation, name))
|
|
397
|
+
return { state: 'refused', reason: 'unconfirmed' };
|
|
398
|
+
if (!required.root.isManaged(name))
|
|
399
|
+
return { state: 'refused', reason: 'not-managed' };
|
|
400
|
+
const deleteQueue = [];
|
|
401
|
+
let result = { state: 'kept', reason: 'busy' };
|
|
402
|
+
try {
|
|
403
|
+
const shared = sharedFrom(required, asideDirName);
|
|
404
|
+
const outcome = await required.interlock.withPartitionWriter({ run: async (ctx) => { result = await manualUnderWriter(shared, ctx, name, deleteQueue); } });
|
|
405
|
+
if (outcome.state === 'skipped')
|
|
406
|
+
result = { state: 'kept', reason: 'busy' };
|
|
407
|
+
}
|
|
408
|
+
catch {
|
|
409
|
+
result = deleteQueue.length > 0 ? { state: 'removed' } : { state: 'kept', reason: 'unknown' };
|
|
410
|
+
}
|
|
411
|
+
await drainDeleteQueue(required.fs, deleteQueue, emptyReport());
|
|
412
|
+
return result;
|
|
413
|
+
}
|
|
414
|
+
//# sourceMappingURL=partition-pass.js.map
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { PartitionFsPort } from './holder-registry.js';
|
|
2
|
+
export type PartitionRootRead = {
|
|
3
|
+
state: 'ok';
|
|
4
|
+
root: string;
|
|
5
|
+
} | {
|
|
6
|
+
state: 'absent';
|
|
7
|
+
} | {
|
|
8
|
+
state: 'unknown';
|
|
9
|
+
reason: string;
|
|
10
|
+
};
|
|
11
|
+
export interface PartitionCandidate {
|
|
12
|
+
name: string;
|
|
13
|
+
path: string;
|
|
14
|
+
}
|
|
15
|
+
export interface SkippedCandidate {
|
|
16
|
+
name: string;
|
|
17
|
+
reason: 'link' | 'not-directory' | 'escape' | 'unreadable';
|
|
18
|
+
}
|
|
19
|
+
export type CandidateList = {
|
|
20
|
+
state: 'ok';
|
|
21
|
+
candidates: PartitionCandidate[];
|
|
22
|
+
skipped: SkippedCandidate[];
|
|
23
|
+
} | {
|
|
24
|
+
state: 'unknown';
|
|
25
|
+
reason: string;
|
|
26
|
+
};
|
|
27
|
+
/** Canonical partitions root: `realpath(<sessionData>/Partitions)`. ENOENT means nothing to do. @complexity O(1) fs calls. */
|
|
28
|
+
export declare function resolvePartitionRoot({ sessionDataDir, fs }: {
|
|
29
|
+
sessionDataDir: string;
|
|
30
|
+
fs: Pick<PartitionFsPort, 'realpath'>;
|
|
31
|
+
}, _optional?: Record<string, never>): Promise<PartitionRootRead>;
|
|
32
|
+
/**
|
|
33
|
+
* Direct, non-link, contained children of `root` whose names the host manages. Skipped entries are
|
|
34
|
+
* reported so a caller can show them, and are never handed to anything that mutates.
|
|
35
|
+
* @complexity O(n) children, two fs calls each.
|
|
36
|
+
*/
|
|
37
|
+
export declare function listCandidates({ root, fs, isManaged }: {
|
|
38
|
+
root: string;
|
|
39
|
+
fs: Pick<PartitionFsPort, 'readdir' | 'lstat' | 'realpath'>;
|
|
40
|
+
isManaged: (name: string) => boolean;
|
|
41
|
+
}, _optional?: Record<string, never>): Promise<CandidateList>;
|
|
42
|
+
//# sourceMappingURL=partition-root.d.ts.map
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
const code = (error) => error?.code;
|
|
3
|
+
/** Canonical partitions root: `realpath(<sessionData>/Partitions)`. ENOENT means nothing to do. @complexity O(1) fs calls. */
|
|
4
|
+
export async function resolvePartitionRoot({ sessionDataDir, fs }, _optional = {}) {
|
|
5
|
+
try {
|
|
6
|
+
return { state: 'ok', root: await fs.realpath({ path: path.join(sessionDataDir, 'Partitions') }) };
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
if (code(error) === 'ENOENT')
|
|
10
|
+
return { state: 'absent' };
|
|
11
|
+
return { state: 'unknown', reason: `partitions root unreadable (${code(error) ?? 'error'})` };
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
async function classify(root, name, fs) {
|
|
15
|
+
const candidate = path.join(root, name);
|
|
16
|
+
try {
|
|
17
|
+
const stat = await fs.lstat({ path: candidate });
|
|
18
|
+
if (stat.isSymbolicLink())
|
|
19
|
+
return 'link';
|
|
20
|
+
if (!stat.isDirectory())
|
|
21
|
+
return 'not-directory';
|
|
22
|
+
// A Windows junction can pass lstat as a plain directory; containment is decided by realpath.
|
|
23
|
+
return path.dirname(await fs.realpath({ path: candidate })) === root ? null : 'escape';
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return 'unreadable';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Direct, non-link, contained children of `root` whose names the host manages. Skipped entries are
|
|
31
|
+
* reported so a caller can show them, and are never handed to anything that mutates.
|
|
32
|
+
* @complexity O(n) children, two fs calls each.
|
|
33
|
+
*/
|
|
34
|
+
export async function listCandidates({ root, fs, isManaged }, _optional = {}) {
|
|
35
|
+
let names;
|
|
36
|
+
try {
|
|
37
|
+
names = await fs.readdir({ path: root });
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
return { state: 'unknown', reason: `partitions root unreadable (${code(error) ?? 'error'})` };
|
|
41
|
+
}
|
|
42
|
+
const candidates = [], skipped = [];
|
|
43
|
+
for (const name of names.filter(entry => isManaged(entry))) {
|
|
44
|
+
const reason = await classify(root, name, fs);
|
|
45
|
+
if (reason === null)
|
|
46
|
+
candidates.push({ name, path: path.join(root, name) });
|
|
47
|
+
else
|
|
48
|
+
skipped.push({ name, reason });
|
|
49
|
+
}
|
|
50
|
+
return { state: 'ok', candidates, skipped };
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=partition-root.js.map
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { ClaimsRead, PartitionClaimSources, StrictRead } from './claim-sources.js';
|
|
2
|
+
import type { HolderRecord, PartitionFsPort, PartitionProcessPort, PartitionSchedulerPort } from './holder-registry.js';
|
|
3
|
+
import type { PartitionRootRead } from './partition-root.js';
|
|
4
|
+
/** The async writer lock, held across every await inside `run` and released in `finally`. */
|
|
5
|
+
export interface HeldWriterLock {
|
|
6
|
+
token: string;
|
|
7
|
+
assertHeld(): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
export interface WriterLockOptions {
|
|
10
|
+
staleMs: number;
|
|
11
|
+
timeoutMs: number;
|
|
12
|
+
createParent: boolean;
|
|
13
|
+
}
|
|
14
|
+
export type WriterLockOwner = {
|
|
15
|
+
state: 'free';
|
|
16
|
+
} | {
|
|
17
|
+
state: 'held';
|
|
18
|
+
token: string;
|
|
19
|
+
owner: 'live' | 'dead' | 'unknown';
|
|
20
|
+
} | {
|
|
21
|
+
state: 'unknown';
|
|
22
|
+
reason: string;
|
|
23
|
+
};
|
|
24
|
+
export interface PartitionWriterLockPort {
|
|
25
|
+
withFileLock<T>(required: {
|
|
26
|
+
lockPath: string;
|
|
27
|
+
run: (held: HeldWriterLock) => Promise<T>;
|
|
28
|
+
}, optional: WriterLockOptions): Promise<T>;
|
|
29
|
+
readToken(required: {
|
|
30
|
+
lockPath: string;
|
|
31
|
+
}): Promise<WriterLockOwner>;
|
|
32
|
+
}
|
|
33
|
+
export interface PartitionInterlockMessages {
|
|
34
|
+
busy: string;
|
|
35
|
+
unregistered: (required: {
|
|
36
|
+
directory: string;
|
|
37
|
+
}) => string;
|
|
38
|
+
refused: string;
|
|
39
|
+
}
|
|
40
|
+
export type PartitionInterlockLogEvent = {
|
|
41
|
+
event: string;
|
|
42
|
+
partition?: string;
|
|
43
|
+
detail?: string;
|
|
44
|
+
};
|
|
45
|
+
export type FenceTarget = string | '*' | null;
|
|
46
|
+
export interface PartitionWriterContext {
|
|
47
|
+
assertHeld(): Promise<void>;
|
|
48
|
+
setTarget(required: {
|
|
49
|
+
target: FenceTarget;
|
|
50
|
+
}): Promise<void>;
|
|
51
|
+
readClaims(): Promise<ClaimsRead>;
|
|
52
|
+
resolveRoot(): Promise<PartitionRootRead>;
|
|
53
|
+
}
|
|
54
|
+
export type OpenMaintenance = 'ran' | 'already-held' | 'skipped-other-holder' | 'skipped-unknown-holders' | 'failed';
|
|
55
|
+
export interface PartitionInterlockRequired<Session> {
|
|
56
|
+
paths: {
|
|
57
|
+
interlockDir: string;
|
|
58
|
+
sessionDataDir: string;
|
|
59
|
+
};
|
|
60
|
+
fs: PartitionFsPort;
|
|
61
|
+
lock: PartitionWriterLockPort;
|
|
62
|
+
process: PartitionProcessPort;
|
|
63
|
+
clock: {
|
|
64
|
+
mono(): number;
|
|
65
|
+
now(): number;
|
|
66
|
+
};
|
|
67
|
+
scheduler: PartitionSchedulerPort;
|
|
68
|
+
fromPartition: (required: {
|
|
69
|
+
partition: string;
|
|
70
|
+
}) => Session;
|
|
71
|
+
isManaged: (name: string) => boolean;
|
|
72
|
+
claimSources: PartitionClaimSources;
|
|
73
|
+
invalidateRetirement: (required: {
|
|
74
|
+
partition: string;
|
|
75
|
+
subjectDir: string;
|
|
76
|
+
reason: 'reacquired';
|
|
77
|
+
}) => Promise<void>;
|
|
78
|
+
openMaintenance: (required: {
|
|
79
|
+
partition: string;
|
|
80
|
+
subjectDir: string;
|
|
81
|
+
}) => Promise<void>;
|
|
82
|
+
messages: PartitionInterlockMessages;
|
|
83
|
+
log: (event: PartitionInterlockLogEvent) => void;
|
|
84
|
+
}
|
|
85
|
+
export interface PartitionInterlockOptional {
|
|
86
|
+
heartbeatMs?: number;
|
|
87
|
+
waitCapMs?: number;
|
|
88
|
+
abandonMs?: number;
|
|
89
|
+
}
|
|
90
|
+
export declare class PartitionBusyError extends Error {
|
|
91
|
+
name: string;
|
|
92
|
+
}
|
|
93
|
+
export declare class PartitionOpenRefusedError extends Error {
|
|
94
|
+
name: string;
|
|
95
|
+
}
|
|
96
|
+
export declare class PartitionNotHeldError extends Error {
|
|
97
|
+
readonly partition: string;
|
|
98
|
+
name: string;
|
|
99
|
+
constructor(partition: string);
|
|
100
|
+
}
|
|
101
|
+
/** Electron names a persistent partition `persist:<dir>`; claims and files use the directory name. */
|
|
102
|
+
export declare const partitionDirName: ({ partition }: {
|
|
103
|
+
partition: string;
|
|
104
|
+
}, _optional?: Record<string, never>) => string;
|
|
105
|
+
export interface SiteSessionAcquired {
|
|
106
|
+
state: 'held';
|
|
107
|
+
partition: string;
|
|
108
|
+
maintenance: OpenMaintenance;
|
|
109
|
+
}
|
|
110
|
+
/** @complexity O(1) to construct; see each method. */
|
|
111
|
+
export declare function createPartitionInterlock<Session>(required: PartitionInterlockRequired<Session>, { heartbeatMs, waitCapMs, abandonMs }?: PartitionInterlockOptional): {
|
|
112
|
+
registerHolder: () => {
|
|
113
|
+
state: "failed";
|
|
114
|
+
reason: string;
|
|
115
|
+
} | {
|
|
116
|
+
state: "registered";
|
|
117
|
+
};
|
|
118
|
+
unregisterHolder: () => void;
|
|
119
|
+
acquireSiteSession: ({ partition, subjectDir }: {
|
|
120
|
+
partition: string;
|
|
121
|
+
subjectDir: string;
|
|
122
|
+
}, _optional?: Record<string, never>) => Promise<SiteSessionAcquired>;
|
|
123
|
+
assertHeld: ({ partition }: {
|
|
124
|
+
partition: string;
|
|
125
|
+
}, _optional?: Record<string, never>) => void;
|
|
126
|
+
siteSession: ({ partition }: {
|
|
127
|
+
partition: string;
|
|
128
|
+
}, _optional?: Record<string, never>) => Session;
|
|
129
|
+
withPartitionWriter: <T>({ run }: {
|
|
130
|
+
run: (context: PartitionWriterContext) => Promise<T>;
|
|
131
|
+
}, _optional?: Record<string, never>) => Promise<{
|
|
132
|
+
state: "skipped";
|
|
133
|
+
reason: "writer-busy";
|
|
134
|
+
} | {
|
|
135
|
+
state: "ran";
|
|
136
|
+
value: T;
|
|
137
|
+
}>;
|
|
138
|
+
readHoldersStrict: () => Promise<StrictRead<HolderRecord[]>>;
|
|
139
|
+
};
|
|
140
|
+
export type PartitionInterlock = ReturnType<typeof createPartitionInterlock>;
|
|
141
|
+
//# sourceMappingURL=profile-interlock.d.ts.map
|