@hasna/switcher 0.2.12 → 0.2.14
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 +12 -0
- package/dist/claude-desktop-gateway.d.ts +3 -0
- package/dist/claude-desktop.js +423 -62
- package/dist/cli/index.js +1946 -208
- package/dist/desktop-state.d.ts +20 -1
- package/dist/gateway-client.d.ts +84 -0
- package/dist/gateway-service.d.ts +60 -0
- package/dist/gateway-supervisor.d.ts +73 -0
- package/dist/harness-process.d.ts +1 -0
- package/dist/harness-types.d.ts +3 -1
- package/dist/index.js +9 -9
- package/dist/inference-gateway.d.ts +25 -2
- package/dist/launcher.d.ts +11 -1
- package/dist/mcp/index.js +13 -13
- package/dist/process-table.d.ts +28 -0
- package/dist/sdk.js +9 -9
- package/dist/serve/index.js +2 -2
- package/docs/CLAUDE-DESKTOP-GATEWAY.md +22 -7
- package/docs/GATEWAY-SERVICE.md +179 -0
- package/hasna.contract.json +1 -1
- package/openapi.json +1 -1
- package/package.json +5 -4
package/dist/claude-desktop.js
CHANGED
|
@@ -18,9 +18,9 @@ var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
|
18
18
|
// src/claude-desktop-gateway.ts
|
|
19
19
|
import { lstat as lstat4, opendir as opendir2, realpath as realpath2, stat as stat2 } from "node:fs/promises";
|
|
20
20
|
import { lstatSync as lstatSync3 } from "node:fs";
|
|
21
|
-
import { isAbsolute as isAbsolute3, join as join4, relative as relative2, resolve as
|
|
21
|
+
import { isAbsolute as isAbsolute3, join as join4, relative as relative2, resolve as resolve3 } from "node:path";
|
|
22
22
|
|
|
23
|
-
//
|
|
23
|
+
// ../../node_modules/.bun/zod@3.25.76/node_modules/zod/v3/external.js
|
|
24
24
|
var exports_external = {};
|
|
25
25
|
__export(exports_external, {
|
|
26
26
|
void: () => voidType,
|
|
@@ -132,7 +132,7 @@ __export(exports_external, {
|
|
|
132
132
|
BRAND: () => BRAND
|
|
133
133
|
});
|
|
134
134
|
|
|
135
|
-
//
|
|
135
|
+
// ../../node_modules/.bun/zod@3.25.76/node_modules/zod/v3/helpers/util.js
|
|
136
136
|
var util;
|
|
137
137
|
(function(util2) {
|
|
138
138
|
util2.assertEqual = (_) => {};
|
|
@@ -263,7 +263,7 @@ var getParsedType = (data) => {
|
|
|
263
263
|
}
|
|
264
264
|
};
|
|
265
265
|
|
|
266
|
-
//
|
|
266
|
+
// ../../node_modules/.bun/zod@3.25.76/node_modules/zod/v3/ZodError.js
|
|
267
267
|
var ZodIssueCode = util.arrayToEnum([
|
|
268
268
|
"invalid_type",
|
|
269
269
|
"invalid_literal",
|
|
@@ -382,7 +382,7 @@ ZodError.create = (issues) => {
|
|
|
382
382
|
return error;
|
|
383
383
|
};
|
|
384
384
|
|
|
385
|
-
//
|
|
385
|
+
// ../../node_modules/.bun/zod@3.25.76/node_modules/zod/v3/locales/en.js
|
|
386
386
|
var errorMap = (issue, _ctx) => {
|
|
387
387
|
let message;
|
|
388
388
|
switch (issue.code) {
|
|
@@ -485,7 +485,7 @@ var errorMap = (issue, _ctx) => {
|
|
|
485
485
|
};
|
|
486
486
|
var en_default = errorMap;
|
|
487
487
|
|
|
488
|
-
//
|
|
488
|
+
// ../../node_modules/.bun/zod@3.25.76/node_modules/zod/v3/errors.js
|
|
489
489
|
var overrideErrorMap = en_default;
|
|
490
490
|
function setErrorMap(map) {
|
|
491
491
|
overrideErrorMap = map;
|
|
@@ -493,7 +493,7 @@ function setErrorMap(map) {
|
|
|
493
493
|
function getErrorMap() {
|
|
494
494
|
return overrideErrorMap;
|
|
495
495
|
}
|
|
496
|
-
//
|
|
496
|
+
// ../../node_modules/.bun/zod@3.25.76/node_modules/zod/v3/helpers/parseUtil.js
|
|
497
497
|
var makeIssue = (params) => {
|
|
498
498
|
const { data, path, errorMaps, issueData } = params;
|
|
499
499
|
const fullPath = [...path, ...issueData.path || []];
|
|
@@ -599,14 +599,14 @@ var isAborted = (x) => x.status === "aborted";
|
|
|
599
599
|
var isDirty = (x) => x.status === "dirty";
|
|
600
600
|
var isValid = (x) => x.status === "valid";
|
|
601
601
|
var isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
|
|
602
|
-
//
|
|
602
|
+
// ../../node_modules/.bun/zod@3.25.76/node_modules/zod/v3/helpers/errorUtil.js
|
|
603
603
|
var errorUtil;
|
|
604
604
|
(function(errorUtil2) {
|
|
605
605
|
errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
606
606
|
errorUtil2.toString = (message) => typeof message === "string" ? message : message?.message;
|
|
607
607
|
})(errorUtil || (errorUtil = {}));
|
|
608
608
|
|
|
609
|
-
//
|
|
609
|
+
// ../../node_modules/.bun/zod@3.25.76/node_modules/zod/v3/types.js
|
|
610
610
|
class ParseInputLazyPath {
|
|
611
611
|
constructor(parent, value, path, key) {
|
|
612
612
|
this._cachedPath = [];
|
|
@@ -4415,21 +4415,59 @@ async function privateDirectory(path) {
|
|
|
4415
4415
|
|
|
4416
4416
|
// src/desktop-state.ts
|
|
4417
4417
|
import { constants as constants2, closeSync, fstatSync, lstatSync as lstatSync2, openSync, readSync } from "node:fs";
|
|
4418
|
-
import { open as open2, rm, lstat as lstat3, rename, chmod } from "node:fs/promises";
|
|
4418
|
+
import { open as open2, rm, lstat as lstat3, rename, chmod, copyFile, link } from "node:fs/promises";
|
|
4419
|
+
import { spawnSync } from "node:child_process";
|
|
4420
|
+
import { resolve as resolve2 } from "node:path";
|
|
4421
|
+
import { createHash } from "node:crypto";
|
|
4419
4422
|
var desktopReadLimit = 1024 * 1024;
|
|
4420
4423
|
function admitDesktopFile(info2) {
|
|
4421
|
-
if (!info2.isFile() || info2.isSymbolicLink() || info2.nlink !== 1 || info2.uid !== process.getuid?.() || (info2.mode & 18) !== 0 || info2.size > desktopReadLimit)
|
|
4422
|
-
throw new Fault(422, "desktop_state_permissions", "Desktop settings must be regular files owned by this user.");
|
|
4424
|
+
if (!info2.isFile() || info2.isSymbolicLink() || info2.nlink !== 1 || info2.uid !== process.getuid?.() || info2.gid !== process.getgid?.() || (info2.mode & 18) !== 0 || info2.size > desktopReadLimit)
|
|
4425
|
+
throw new Fault(422, "desktop_state_permissions", "Desktop settings must be regular files owned by this user and their primary group.");
|
|
4426
|
+
}
|
|
4427
|
+
function admitDesktopMetadata(path, info2) {
|
|
4428
|
+
const attributes = [];
|
|
4429
|
+
const hash = () => createHash("sha256").update(JSON.stringify({ gid: info2.gid, attributes })).digest("hex");
|
|
4430
|
+
if (process.platform !== "darwin")
|
|
4431
|
+
return hash();
|
|
4432
|
+
const deadline = Date.now() + 2000;
|
|
4433
|
+
const inspect = (executable, args, maxBuffer) => {
|
|
4434
|
+
const remaining = deadline - Date.now();
|
|
4435
|
+
if (remaining <= 0)
|
|
4436
|
+
throw new Fault(422, "desktop_state_permissions", "Desktop metadata inspection timed out.");
|
|
4437
|
+
const result = spawnSync(executable, args, { encoding: "utf8", env: { LC_ALL: "C" }, stdio: ["ignore", "pipe", "ignore"], timeout: remaining, maxBuffer });
|
|
4438
|
+
if (result.error || result.status !== 0)
|
|
4439
|
+
throw new Fault(422, "desktop_state_permissions", `Desktop security metadata could not be verified (${result.error?.code === "ETIMEDOUT" ? "inspection_timeout" : "inspection_exit"}).`);
|
|
4440
|
+
return result.stdout;
|
|
4441
|
+
};
|
|
4442
|
+
const listing = inspect("/bin/ls", ["-ldneO@", resolve2(path)], 64 * 1024);
|
|
4443
|
+
const lines = listing.trimEnd().split(`
|
|
4444
|
+
`), fields = lines[0]?.trim().split(/\s+/);
|
|
4445
|
+
const refused = !fields || !/^-[-rwxStTs]{9}@?$/.test(fields[0] ?? "") ? "mode_or_acl" : fields[1] !== "1" ? "link_count" : fields[2] !== String(info2.uid) ? "owner" : fields[3] !== String(info2.gid) ? "group" : fields[4] !== "-" ? "file_flags" : lines.slice(1).some((line) => !line.startsWith("\t") || /^\s*\d+:/.test(line)) ? "metadata_records" : undefined;
|
|
4446
|
+
if (refused)
|
|
4447
|
+
throw new Fault(422, "desktop_state_permissions", `Desktop settings security metadata cannot be safely restored (${refused}).`);
|
|
4448
|
+
const names = inspect("/usr/bin/xattr", [resolve2(path)], 64 * 1024).trimEnd().split(`
|
|
4449
|
+
`).filter(Boolean).sort();
|
|
4450
|
+
if (names.length > 64 || new Set(names).size !== names.length || names.some((name) => name.length > 255 || /[\x00-\x1f\x7f]/.test(name)))
|
|
4451
|
+
throw new Fault(422, "desktop_state_permissions", "Desktop extended attributes exceed bounded inspection.");
|
|
4452
|
+
let total = 0;
|
|
4453
|
+
for (const name of names) {
|
|
4454
|
+
const hex = inspect("/usr/bin/xattr", ["-px", name, resolve2(path)], 2 * desktopReadLimit + 64 * 1024).replace(/\s/g, "");
|
|
4455
|
+
total += hex.length / 2;
|
|
4456
|
+
if (hex.length % 2 || !/^[0-9a-fA-F]*$/.test(hex) || total > desktopReadLimit)
|
|
4457
|
+
throw new Fault(422, "desktop_state_permissions", "Desktop extended attributes exceed bounded inspection.");
|
|
4458
|
+
attributes.push({ name, digest: createHash("sha256").update(Buffer.from(hex, "hex")).digest("hex") });
|
|
4459
|
+
}
|
|
4460
|
+
return hash();
|
|
4423
4461
|
}
|
|
4424
4462
|
function admitOpenedDesktopFile(info2, opened) {
|
|
4425
|
-
if (opened.dev !== info2.dev || opened.ino !== info2.ino || opened.nlink !== 1 || opened.size > desktopReadLimit || opened.mode !== info2.mode || opened.uid !== info2.uid)
|
|
4463
|
+
if (opened.dev !== info2.dev || opened.ino !== info2.ino || opened.nlink !== 1 || opened.size > desktopReadLimit || opened.mode !== info2.mode || opened.uid !== info2.uid || opened.gid !== info2.gid || opened.ctimeMs !== info2.ctimeMs)
|
|
4426
4464
|
throw new Fault(409, "desktop_state_changed", "Desktop settings changed while being read.");
|
|
4427
4465
|
}
|
|
4428
|
-
function desktopSnapshot(bytes, length, opened, after) {
|
|
4429
|
-
if (length > desktopReadLimit || length !== opened.size || after.size !== opened.size || after.mtimeMs !== opened.mtimeMs || after.ctimeMs !== opened.ctimeMs || after.mode !== opened.mode)
|
|
4466
|
+
function desktopSnapshot(bytes, length, opened, after, metadataHash) {
|
|
4467
|
+
if (length > desktopReadLimit || length !== opened.size || after.size !== opened.size || after.mtimeMs !== opened.mtimeMs || after.ctimeMs !== opened.ctimeMs || after.mode !== opened.mode || after.gid !== opened.gid)
|
|
4430
4468
|
throw new Fault(409, "desktop_state_changed", "Desktop settings changed while being read.");
|
|
4431
4469
|
try {
|
|
4432
|
-
return { text: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes.subarray(0, length)), mode: after.mode & 4095 };
|
|
4470
|
+
return { text: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes.subarray(0, length)), mode: after.mode & 4095, metadataHash };
|
|
4433
4471
|
} catch {
|
|
4434
4472
|
throw new Fault(422, "desktop_state_encoding", "Desktop settings must be valid UTF-8; original bytes were left unchanged.");
|
|
4435
4473
|
}
|
|
@@ -4438,6 +4476,7 @@ async function safeDesktopSnapshot(path) {
|
|
|
4438
4476
|
try {
|
|
4439
4477
|
const info2 = await lstat3(path);
|
|
4440
4478
|
admitDesktopFile(info2);
|
|
4479
|
+
const metadataHash = admitDesktopMetadata(path, info2);
|
|
4441
4480
|
const file = await open2(path, constants2.O_RDONLY | constants2.O_NOFOLLOW);
|
|
4442
4481
|
try {
|
|
4443
4482
|
const opened = await file.stat();
|
|
@@ -4450,7 +4489,9 @@ async function safeDesktopSnapshot(path) {
|
|
|
4450
4489
|
break;
|
|
4451
4490
|
length += read.bytesRead;
|
|
4452
4491
|
}
|
|
4453
|
-
|
|
4492
|
+
const snapshot = desktopSnapshot(bytes, length, opened, await file.stat(), metadataHash);
|
|
4493
|
+
admitOpenedDesktopFile(opened, lstatSync2(path));
|
|
4494
|
+
return snapshot;
|
|
4454
4495
|
} finally {
|
|
4455
4496
|
await file.close();
|
|
4456
4497
|
}
|
|
@@ -4464,6 +4505,7 @@ function safeDesktopSnapshotSync(path) {
|
|
|
4464
4505
|
try {
|
|
4465
4506
|
const info2 = lstatSync2(path);
|
|
4466
4507
|
admitDesktopFile(info2);
|
|
4508
|
+
const metadataHash = admitDesktopMetadata(path, info2);
|
|
4467
4509
|
const file = openSync(path, constants2.O_RDONLY | constants2.O_NOFOLLOW);
|
|
4468
4510
|
try {
|
|
4469
4511
|
const opened = fstatSync(file);
|
|
@@ -4476,7 +4518,7 @@ function safeDesktopSnapshotSync(path) {
|
|
|
4476
4518
|
break;
|
|
4477
4519
|
length += count;
|
|
4478
4520
|
}
|
|
4479
|
-
const snapshot = desktopSnapshot(bytes, length, opened, fstatSync(file));
|
|
4521
|
+
const snapshot = desktopSnapshot(bytes, length, opened, fstatSync(file), metadataHash);
|
|
4480
4522
|
admitOpenedDesktopFile(opened, lstatSync2(path));
|
|
4481
4523
|
return snapshot;
|
|
4482
4524
|
} finally {
|
|
@@ -4491,24 +4533,204 @@ function safeDesktopSnapshotSync(path) {
|
|
|
4491
4533
|
async function safeDesktopRead(path) {
|
|
4492
4534
|
return (await safeDesktopSnapshot(path))?.text;
|
|
4493
4535
|
}
|
|
4494
|
-
|
|
4536
|
+
function sameDesktopSnapshot(a, b) {
|
|
4537
|
+
return a?.text === b?.text && a?.mode === b?.mode && a?.metadataHash === b?.metadataHash;
|
|
4538
|
+
}
|
|
4539
|
+
async function preserveDesktopFile(source, destination, expected) {
|
|
4540
|
+
if (await safeDesktopSnapshot(destination) !== undefined || !sameDesktopSnapshot(await safeDesktopSnapshot(source), expected))
|
|
4541
|
+
throw new Fault(409, "desktop_state_changed", "Desktop preservation target or source changed.");
|
|
4542
|
+
if (process.platform === "darwin") {
|
|
4543
|
+
const copied = spawnSync("/bin/cp", ["-pn", resolve2(source), resolve2(destination)], { env: { LC_ALL: "C" }, stdio: "ignore", timeout: 2000 });
|
|
4544
|
+
if (copied.error || copied.status !== 0)
|
|
4545
|
+
throw new Fault(422, "desktop_state_permissions", "Desktop metadata preservation failed.");
|
|
4546
|
+
} else
|
|
4547
|
+
await copyFile(source, destination, constants2.COPYFILE_EXCL);
|
|
4548
|
+
if (!sameDesktopSnapshot(await safeDesktopSnapshot(source), expected) || !sameDesktopSnapshot(await safeDesktopSnapshot(destination), expected))
|
|
4549
|
+
throw new Fault(409, "desktop_state_changed", "Desktop original preservation readback failed.");
|
|
4550
|
+
}
|
|
4551
|
+
var conflictPath = (path, suffix) => path + "." + crypto.randomUUID() + "." + suffix;
|
|
4552
|
+
async function exists(path) {
|
|
4553
|
+
try {
|
|
4554
|
+
await lstat3(path);
|
|
4555
|
+
return true;
|
|
4556
|
+
} catch (error) {
|
|
4557
|
+
if (error.code === "ENOENT")
|
|
4558
|
+
return false;
|
|
4559
|
+
throw error;
|
|
4560
|
+
}
|
|
4561
|
+
}
|
|
4562
|
+
async function preserveConflict(path) {
|
|
4563
|
+
const target = conflictPath(path, "conflict");
|
|
4564
|
+
await rename(path, target);
|
|
4565
|
+
return target;
|
|
4566
|
+
}
|
|
4567
|
+
async function restoreDisplaced(backup, path) {
|
|
4568
|
+
const snapshot = await safeDesktopSnapshot(backup);
|
|
4569
|
+
if (!snapshot)
|
|
4570
|
+
throw new Fault(409, "desktop_state_changed", "Desktop displaced state is missing; existing state was preserved.");
|
|
4571
|
+
const temporary = conflictPath(path, "restore");
|
|
4572
|
+
try {
|
|
4573
|
+
await preserveDesktopFile(backup, temporary, snapshot);
|
|
4574
|
+
try {
|
|
4575
|
+
await link(temporary, path);
|
|
4576
|
+
} catch (error) {
|
|
4577
|
+
if (error.code === "EEXIST") {
|
|
4578
|
+
const preserved = await preserveConflict(path);
|
|
4579
|
+
throw new Fault(409, "desktop_state_changed", `Desktop settings changed; foreign file preserved at ${preserved}; original retained at ${backup}.`);
|
|
4580
|
+
}
|
|
4581
|
+
throw error;
|
|
4582
|
+
}
|
|
4583
|
+
await rm(temporary, { force: true });
|
|
4584
|
+
if (!sameDesktopSnapshot(await safeDesktopSnapshot(path), snapshot))
|
|
4585
|
+
throw new Fault(409, "desktop_state_changed", `Desktop settings changed after restoration; original retained at ${backup}.`);
|
|
4586
|
+
} finally {
|
|
4587
|
+
if (await safeDesktopSnapshot(temporary).catch(() => {
|
|
4588
|
+
return;
|
|
4589
|
+
}) !== undefined)
|
|
4590
|
+
await rm(temporary, { force: true });
|
|
4591
|
+
}
|
|
4592
|
+
}
|
|
4593
|
+
async function replaceDesktopFile(path, temporary, expectedCurrent, expectedNew) {
|
|
4594
|
+
if (!sameDesktopSnapshot(await safeDesktopSnapshot(temporary), expectedNew))
|
|
4595
|
+
throw new Fault(409, "desktop_state_changed", "Desktop replacement bytes changed before installation.");
|
|
4596
|
+
if (expectedCurrent === undefined) {
|
|
4597
|
+
if (await exists(path))
|
|
4598
|
+
throw new Fault(409, "desktop_state_changed", "Desktop replacement target appeared concurrently; existing state was preserved.");
|
|
4599
|
+
try {
|
|
4600
|
+
await link(temporary, path);
|
|
4601
|
+
} catch (error) {
|
|
4602
|
+
if (error.code === "EEXIST")
|
|
4603
|
+
throw new Fault(409, "desktop_state_changed", "Desktop replacement target appeared concurrently; existing state was preserved.");
|
|
4604
|
+
throw error;
|
|
4605
|
+
}
|
|
4606
|
+
await rm(temporary, { force: true });
|
|
4607
|
+
if (!sameDesktopSnapshot(await safeDesktopSnapshot(path), expectedNew)) {
|
|
4608
|
+
const preserved = await preserveConflict(path);
|
|
4609
|
+
throw new Fault(409, "desktop_state_changed", `Desktop replacement changed after installation; foreign file preserved at ${preserved}.`);
|
|
4610
|
+
}
|
|
4611
|
+
return;
|
|
4612
|
+
}
|
|
4613
|
+
const backup = conflictPath(path, "previous");
|
|
4614
|
+
let displaced = false;
|
|
4615
|
+
try {
|
|
4616
|
+
try {
|
|
4617
|
+
await rename(path, backup);
|
|
4618
|
+
displaced = true;
|
|
4619
|
+
} catch (error) {
|
|
4620
|
+
if (error.code === "ENOENT")
|
|
4621
|
+
throw new Fault(409, "desktop_state_changed", "Desktop replacement target disappeared; existing state was preserved.");
|
|
4622
|
+
throw error;
|
|
4623
|
+
}
|
|
4624
|
+
let current;
|
|
4625
|
+
try {
|
|
4626
|
+
current = await safeDesktopSnapshot(backup);
|
|
4627
|
+
} catch {
|
|
4628
|
+
await restoreDisplaced(backup, path);
|
|
4629
|
+
displaced = false;
|
|
4630
|
+
throw new Fault(409, "desktop_state_changed", "Desktop replacement target changed; existing state was preserved.");
|
|
4631
|
+
}
|
|
4632
|
+
if (!sameDesktopSnapshot(current, expectedCurrent)) {
|
|
4633
|
+
await restoreDisplaced(backup, path);
|
|
4634
|
+
displaced = false;
|
|
4635
|
+
throw new Fault(409, "desktop_state_changed", "Desktop settings changed before replacement; existing state was preserved.");
|
|
4636
|
+
}
|
|
4637
|
+
try {
|
|
4638
|
+
await link(temporary, path);
|
|
4639
|
+
} catch (error) {
|
|
4640
|
+
if (error.code === "EEXIST") {
|
|
4641
|
+
await restoreDisplaced(backup, path);
|
|
4642
|
+
displaced = false;
|
|
4643
|
+
throw new Fault(409, "desktop_state_changed", "Desktop replacement target appeared concurrently; existing state was preserved.");
|
|
4644
|
+
}
|
|
4645
|
+
throw error;
|
|
4646
|
+
}
|
|
4647
|
+
await rm(temporary, { force: true });
|
|
4648
|
+
if (!sameDesktopSnapshot(await safeDesktopSnapshot(path), expectedNew)) {
|
|
4649
|
+
await restoreDisplaced(backup, path);
|
|
4650
|
+
displaced = false;
|
|
4651
|
+
throw new Fault(409, "desktop_state_changed", "Desktop replacement changed after installation; existing state was preserved.");
|
|
4652
|
+
}
|
|
4653
|
+
displaced = false;
|
|
4654
|
+
} finally {
|
|
4655
|
+
if (displaced) {
|
|
4656
|
+
try {
|
|
4657
|
+
await restoreDisplaced(backup, path);
|
|
4658
|
+
} catch {}
|
|
4659
|
+
}
|
|
4660
|
+
}
|
|
4661
|
+
}
|
|
4662
|
+
async function removeDesktopFile(path, expected, guard) {
|
|
4663
|
+
if (expected === undefined) {
|
|
4664
|
+
if (await exists(path))
|
|
4665
|
+
throw new Fault(409, "desktop_state_changed", "Desktop removal target appeared concurrently; existing state was preserved.");
|
|
4666
|
+
return;
|
|
4667
|
+
}
|
|
4668
|
+
const quarantine = conflictPath(path, "remove");
|
|
4669
|
+
try {
|
|
4670
|
+
await rename(path, quarantine);
|
|
4671
|
+
} catch (error) {
|
|
4672
|
+
if (error.code === "ENOENT")
|
|
4673
|
+
throw new Fault(409, "desktop_state_changed", "Desktop removal target disappeared; existing state was preserved.");
|
|
4674
|
+
throw error;
|
|
4675
|
+
}
|
|
4676
|
+
let moved;
|
|
4677
|
+
try {
|
|
4678
|
+
moved = await safeDesktopSnapshot(quarantine);
|
|
4679
|
+
} catch {
|
|
4680
|
+
await restoreDisplaced(quarantine, path);
|
|
4681
|
+
throw new Fault(409, "desktop_state_changed", "Desktop removal target changed; existing state was preserved.");
|
|
4682
|
+
}
|
|
4683
|
+
if (!sameDesktopSnapshot(moved, expected)) {
|
|
4684
|
+
await restoreDisplaced(quarantine, path);
|
|
4685
|
+
throw new Fault(409, "desktop_state_changed", "Desktop removal target changed; existing state was preserved.");
|
|
4686
|
+
}
|
|
4687
|
+
if (guard) {
|
|
4688
|
+
try {
|
|
4689
|
+
await guard();
|
|
4690
|
+
} catch (error) {
|
|
4691
|
+
try {
|
|
4692
|
+
await restoreDisplaced(quarantine, path);
|
|
4693
|
+
} catch {}
|
|
4694
|
+
throw error;
|
|
4695
|
+
}
|
|
4696
|
+
}
|
|
4697
|
+
await rm(quarantine, { force: true });
|
|
4698
|
+
if (await exists(path))
|
|
4699
|
+
throw new Fault(409, "desktop_state_changed", "Desktop removal target appeared concurrently; existing state was preserved.");
|
|
4700
|
+
}
|
|
4701
|
+
async function writeDesktopPrivate(path, text, mode = 384, metadataSource, expectation) {
|
|
4495
4702
|
if (!Number.isInteger(mode) || mode < 0 || mode > 4095 || (mode & 18) !== 0)
|
|
4496
4703
|
throw new Fault(422, "desktop_state_permissions", "Desktop settings restoration requires an admitted original mode.");
|
|
4704
|
+
const observed = await safeDesktopSnapshot(path);
|
|
4705
|
+
if (expectation && !sameDesktopSnapshot(observed, expectation.current))
|
|
4706
|
+
throw new Fault(409, "desktop_state_changed", "Desktop settings changed before restoration; existing state was preserved.");
|
|
4707
|
+
const expectedCurrent = expectation ? expectation.current : observed;
|
|
4708
|
+
const template = metadataSource?.snapshot ?? expectedCurrent;
|
|
4709
|
+
if (metadataSource && !sameDesktopSnapshot(await safeDesktopSnapshot(metadataSource.path), template))
|
|
4710
|
+
throw new Fault(409, "desktop_state_changed", "Desktop restoration original changed or is missing.");
|
|
4497
4711
|
const temporary = path + "." + crypto.randomUUID() + ".tmp";
|
|
4498
4712
|
try {
|
|
4499
|
-
|
|
4713
|
+
if (template) {
|
|
4714
|
+
await preserveDesktopFile(metadataSource?.path ?? path, temporary, template);
|
|
4715
|
+
await chmod(temporary, 384);
|
|
4716
|
+
}
|
|
4717
|
+
const file = await open2(temporary, constants2.O_RDWR | constants2.O_NOFOLLOW | (template ? 0 : constants2.O_CREAT | constants2.O_EXCL), 384);
|
|
4500
4718
|
try {
|
|
4719
|
+
await file.truncate(0);
|
|
4501
4720
|
await file.writeFile(text);
|
|
4502
4721
|
await file.chmod(mode);
|
|
4503
4722
|
} finally {
|
|
4504
4723
|
await file.close();
|
|
4505
4724
|
}
|
|
4506
4725
|
const written = await safeDesktopSnapshot(temporary);
|
|
4507
|
-
if (written?.text !== text || written.mode !== mode)
|
|
4726
|
+
if (written?.text !== text || written.mode !== mode || template && written.metadataHash !== template.metadataHash)
|
|
4508
4727
|
throw new Fault(409, "desktop_state_changed", "Desktop settings restoration readback failed.");
|
|
4509
|
-
await
|
|
4728
|
+
await replaceDesktopFile(path, temporary, expectedCurrent, written);
|
|
4510
4729
|
} finally {
|
|
4511
|
-
await
|
|
4730
|
+
if (await safeDesktopSnapshot(temporary).catch(() => {
|
|
4731
|
+
return;
|
|
4732
|
+
}) !== undefined)
|
|
4733
|
+
await rm(temporary, { force: true });
|
|
4512
4734
|
}
|
|
4513
4735
|
}
|
|
4514
4736
|
async function desktopLease(path) {
|
|
@@ -4576,15 +4798,17 @@ async function assertDesktopLeaseAvailable(path) {
|
|
|
4576
4798
|
}
|
|
4577
4799
|
|
|
4578
4800
|
// src/desktop-selection.ts
|
|
4579
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
4580
|
-
import {
|
|
4801
|
+
import { createHash as createHash2, randomUUID } from "node:crypto";
|
|
4802
|
+
import { writeFile, rename as rename2 } from "node:fs/promises";
|
|
4581
4803
|
import { join as join2 } from "node:path";
|
|
4582
|
-
var hash = (text) =>
|
|
4804
|
+
var hash = (text) => createHash2("sha256").update(text).digest("hex");
|
|
4583
4805
|
var conflict = () => new Fault(409, "desktop_config_changed", "Desktop configuration changed; existing bytes and the recovery receipt were preserved.");
|
|
4584
4806
|
var invalid2 = () => new Fault(409, "desktop_recovery", "Desktop selection needs recovery by its owning supported version before another launch.");
|
|
4585
4807
|
var identifier = (value) => typeof value === "string" && /^[A-Za-z0-9_-]{1,80}$/.test(value);
|
|
4586
4808
|
var admittedMode = (value) => typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= 4095 && (value & 18) === 0;
|
|
4587
|
-
var matches = (actual, text, mode) => (actual?.text ?? null) === text && (actual?.mode ?? null) === mode;
|
|
4809
|
+
var matches = (actual, text, mode, metadataHash) => (actual?.text ?? null) === text && (actual?.mode ?? null) === mode && (actual?.metadataHash ?? null) === metadataHash;
|
|
4810
|
+
var originalPath = (library, id) => join2(library, `.switcher-original-${id}.bak`);
|
|
4811
|
+
var preparedPath = (library, id) => join2(library, `.switcher-selected-${id}.tmp`);
|
|
4588
4812
|
function metadata(text) {
|
|
4589
4813
|
if (text === null)
|
|
4590
4814
|
return { entries: [] };
|
|
@@ -4601,31 +4825,94 @@ function metadata(text) {
|
|
|
4601
4825
|
throw invalid2();
|
|
4602
4826
|
return m;
|
|
4603
4827
|
}
|
|
4828
|
+
function v4Receipt(value) {
|
|
4829
|
+
const r = value;
|
|
4830
|
+
if (r?.version !== 4 || !identifier(r.id) || !/^[0-9a-f-]{36}$/.test(r.id) || !/^[a-f0-9]{64}$/.test(r.configHash) || r.previousMeta !== null && typeof r.previousMeta !== "string" || typeof r.appliedMeta !== "string" || (r.previousMeta === null ? r.previousMetaMode !== null : !admittedMode(r.previousMetaMode)) || (r.previousMeta === null ? r.previousMetadataHash !== null : typeof r.previousMetadataHash !== "string" || !/^[a-f0-9]{64}$/.test(r.previousMetadataHash)) || typeof r.appliedMetadataHash !== "string" || !/^[a-f0-9]{64}$/.test(r.appliedMetadataHash) || r.previousMetadataHash !== null && r.appliedMetadataHash !== r.previousMetadataHash || r.previousSelected !== null && (!r.previousSelected || !identifier(r.previousSelected.id) || r.previousSelected.hash === undefined && r.previousSelected.text === undefined || r.previousSelected.hash !== undefined && r.previousSelected.text !== undefined || r.previousSelected.hash !== undefined && r.previousSelected.hash !== null && (typeof r.previousSelected.hash !== "string" || !/^[a-f0-9]{64}$/.test(r.previousSelected.hash)) || r.previousSelected.text !== undefined && r.previousSelected.text !== null && typeof r.previousSelected.text !== "string"))
|
|
4831
|
+
throw invalid2();
|
|
4832
|
+
const before = metadata(r.previousMeta), after = metadata(r.appliedMeta);
|
|
4833
|
+
if (after.appliedId !== r.id || before.appliedId === r.id || (before.appliedId ?? null) !== (r.previousSelected?.id ?? null))
|
|
4834
|
+
throw invalid2();
|
|
4835
|
+
return r;
|
|
4836
|
+
}
|
|
4604
4837
|
function receipt(text) {
|
|
4605
|
-
let
|
|
4838
|
+
let value;
|
|
4606
4839
|
try {
|
|
4607
|
-
|
|
4840
|
+
value = JSON.parse(text);
|
|
4608
4841
|
} catch {
|
|
4609
4842
|
throw invalid2();
|
|
4610
4843
|
}
|
|
4611
|
-
if (
|
|
4612
|
-
|
|
4613
|
-
const
|
|
4614
|
-
if (
|
|
4844
|
+
if (value?.version === 4)
|
|
4845
|
+
return v4Receipt(value);
|
|
4846
|
+
const r = value;
|
|
4847
|
+
if (!r || typeof r !== "object" || Array.isArray(r) || Object.keys(r).sort().join(",") !== "appliedMeta,configHash,id,previousMeta" || !identifier(r.id) || !/^[0-9a-f-]{36}$/.test(r.id) || typeof r.configHash !== "string" || !/^[a-f0-9]{64}$/.test(r.configHash) || r.previousMeta !== null || typeof r.appliedMeta !== "string")
|
|
4615
4848
|
throw invalid2();
|
|
4616
|
-
return r;
|
|
4849
|
+
return { id: r.id, configHash: r.configHash, previousMeta: null, appliedMeta: r.appliedMeta };
|
|
4850
|
+
}
|
|
4851
|
+
function upgradeReceipt(r, currentMeta) {
|
|
4852
|
+
if ("version" in r)
|
|
4853
|
+
return r;
|
|
4854
|
+
return {
|
|
4855
|
+
version: 4,
|
|
4856
|
+
id: r.id,
|
|
4857
|
+
configHash: r.configHash,
|
|
4858
|
+
previousMeta: null,
|
|
4859
|
+
previousMetaMode: null,
|
|
4860
|
+
previousMetadataHash: null,
|
|
4861
|
+
appliedMeta: r.appliedMeta,
|
|
4862
|
+
appliedMetadataHash: currentMeta?.metadataHash ?? "0".repeat(64),
|
|
4863
|
+
previousSelected: null
|
|
4864
|
+
};
|
|
4617
4865
|
}
|
|
4618
4866
|
async function selectedUnchanged(library, r) {
|
|
4619
|
-
|
|
4867
|
+
const selected = r.previousSelected;
|
|
4868
|
+
if (!selected)
|
|
4869
|
+
return;
|
|
4870
|
+
const current = await safeDesktopRead(join2(library, selected.id + ".json"));
|
|
4871
|
+
if (selected.hash !== undefined) {
|
|
4872
|
+
if (selected.hash === null ? current !== undefined : current === undefined || hash(current) !== selected.hash)
|
|
4873
|
+
throw conflict();
|
|
4874
|
+
} else if ((current ?? null) !== (selected.text ?? null))
|
|
4875
|
+
throw conflict();
|
|
4876
|
+
}
|
|
4877
|
+
function selectedUnchangedSync(library, selected) {
|
|
4878
|
+
if (!selected)
|
|
4879
|
+
return true;
|
|
4880
|
+
const current = safeDesktopSnapshotSync(join2(library, selected.id + ".json"))?.text;
|
|
4881
|
+
if (selected.hash !== undefined)
|
|
4882
|
+
return selected.hash === null ? current === undefined : current !== undefined && hash(current) === selected.hash;
|
|
4883
|
+
return (current ?? null) === (selected.text ?? null);
|
|
4884
|
+
}
|
|
4885
|
+
function retainedReceipt(r) {
|
|
4886
|
+
const selected = r.previousSelected;
|
|
4887
|
+
if (!selected || selected.text === undefined)
|
|
4888
|
+
return JSON.stringify(r) + `
|
|
4889
|
+
`;
|
|
4890
|
+
return JSON.stringify({ ...r, previousSelected: { id: selected.id, hash: selected.hash ?? (selected.text === null ? null : hash(selected.text)) } }) + `
|
|
4891
|
+
`;
|
|
4892
|
+
}
|
|
4893
|
+
async function preservedSelection(library, r, wasApplied) {
|
|
4894
|
+
const backup = await safeDesktopSnapshot(originalPath(library, r.id)), prepared = await safeDesktopSnapshot(preparedPath(library, r.id));
|
|
4895
|
+
if (backup && !matches(backup, r.previousMeta, r.previousMetaMode, r.previousMetadataHash) || wasApplied && r.previousMeta !== null && !backup || prepared && !matches(prepared, r.appliedMeta, 384, r.appliedMetadataHash))
|
|
4620
4896
|
throw conflict();
|
|
4897
|
+
return { backup, prepared };
|
|
4621
4898
|
}
|
|
4622
4899
|
async function recoverDesktopSelection(library) {
|
|
4623
4900
|
const path = join2(library, ".switcher-selection.json"), raw = await safeDesktopRead(path);
|
|
4624
4901
|
if (raw === undefined)
|
|
4625
4902
|
return;
|
|
4626
|
-
const
|
|
4627
|
-
|
|
4628
|
-
const
|
|
4903
|
+
const parsed = receipt(raw), metaPath = join2(library, "_meta.json"), configPath = join2(library, parsed.id + ".json");
|
|
4904
|
+
let currentMeta = await safeDesktopSnapshot(metaPath);
|
|
4905
|
+
const r = upgradeReceipt(parsed, currentMeta);
|
|
4906
|
+
const currentConfig = await safeDesktopRead(configPath);
|
|
4907
|
+
if (currentMeta === undefined && r.previousMeta !== null) {
|
|
4908
|
+
const preserved = await safeDesktopSnapshot(originalPath(library, r.id));
|
|
4909
|
+
if (!matches(preserved, r.previousMeta, r.previousMetaMode, r.previousMetadataHash))
|
|
4910
|
+
throw conflict();
|
|
4911
|
+
await writeDesktopPrivate(metaPath, r.previousMeta, r.previousMetaMode, { path: originalPath(library, r.id), snapshot: { text: r.previousMeta, mode: r.previousMetaMode, metadataHash: r.previousMetadataHash } }, { current: undefined });
|
|
4912
|
+
currentMeta = await safeDesktopSnapshot(metaPath);
|
|
4913
|
+
}
|
|
4914
|
+
const wasOriginal = matches(currentMeta, r.previousMeta, r.previousMetaMode, r.previousMetadataHash), wasApplied = matches(currentMeta, r.appliedMeta, 384, r.appliedMetadataHash);
|
|
4915
|
+
const { backup, prepared } = await preservedSelection(library, r, wasApplied);
|
|
4629
4916
|
await selectedUnchanged(library, r);
|
|
4630
4917
|
if (!wasOriginal && !wasApplied)
|
|
4631
4918
|
throw conflict();
|
|
@@ -4633,24 +4920,78 @@ async function recoverDesktopSelection(library) {
|
|
|
4633
4920
|
throw conflict();
|
|
4634
4921
|
if (await safeDesktopRead(path) !== raw)
|
|
4635
4922
|
throw conflict();
|
|
4636
|
-
if (!matches(await safeDesktopSnapshot(metaPath), currentMeta?.text ?? null, currentMeta?.mode ?? null))
|
|
4923
|
+
if (!matches(await safeDesktopSnapshot(metaPath), currentMeta?.text ?? null, currentMeta?.mode ?? null, currentMeta?.metadataHash ?? null))
|
|
4637
4924
|
throw conflict();
|
|
4925
|
+
const assertMetaRestored = async () => {
|
|
4926
|
+
if (!matches(await safeDesktopSnapshot(metaPath), r.previousMeta, r.previousMetaMode, r.previousMetadataHash))
|
|
4927
|
+
throw conflict();
|
|
4928
|
+
};
|
|
4929
|
+
const assertRestored = async () => {
|
|
4930
|
+
await assertMetaRestored();
|
|
4931
|
+
if (await safeDesktopRead(path) !== raw)
|
|
4932
|
+
throw conflict();
|
|
4933
|
+
};
|
|
4638
4934
|
if (wasApplied) {
|
|
4639
|
-
if (r.previousMeta === null)
|
|
4640
|
-
|
|
4641
|
-
|
|
4642
|
-
await
|
|
4643
|
-
|
|
4935
|
+
if (r.previousMeta === null) {
|
|
4936
|
+
if (!currentMeta)
|
|
4937
|
+
throw conflict();
|
|
4938
|
+
await removeDesktopFile(metaPath, currentMeta);
|
|
4939
|
+
} else
|
|
4940
|
+
await writeDesktopPrivate(metaPath, r.previousMeta, r.previousMetaMode, { path: originalPath(library, r.id), snapshot: { text: r.previousMeta, mode: r.previousMetaMode, metadataHash: r.previousMetadataHash } }, { current: currentMeta });
|
|
4941
|
+
await assertRestored();
|
|
4942
|
+
}
|
|
4943
|
+
if (prepared) {
|
|
4944
|
+
if (!matches(safeDesktopSnapshotSync(preparedPath(library, r.id)), r.appliedMeta, 384, r.appliedMetadataHash))
|
|
4644
4945
|
throw conflict();
|
|
4946
|
+
await removeDesktopFile(preparedPath(library, r.id), prepared, assertRestored);
|
|
4645
4947
|
}
|
|
4948
|
+
if (backup && !matches(safeDesktopSnapshotSync(originalPath(library, r.id)), r.previousMeta, r.previousMetaMode, r.previousMetadataHash))
|
|
4949
|
+
throw conflict();
|
|
4646
4950
|
if (currentConfig !== undefined) {
|
|
4647
|
-
|
|
4951
|
+
const configSnapshot = await safeDesktopSnapshot(configPath);
|
|
4952
|
+
if (!configSnapshot || hash(configSnapshot.text) !== r.configHash)
|
|
4648
4953
|
throw conflict();
|
|
4649
|
-
await
|
|
4954
|
+
await removeDesktopFile(configPath, configSnapshot, assertRestored);
|
|
4650
4955
|
}
|
|
4651
|
-
|
|
4956
|
+
await assertRestored();
|
|
4957
|
+
const receiptSnapshot = await safeDesktopSnapshot(path);
|
|
4958
|
+
if (!receiptSnapshot || receiptSnapshot.text !== raw)
|
|
4652
4959
|
throw conflict();
|
|
4653
|
-
|
|
4960
|
+
const recoveredPath = path + ".recovered-" + randomUUID(), retained = retainedReceipt(r);
|
|
4961
|
+
if (retained !== raw) {
|
|
4962
|
+
await writeFile(recoveredPath, retained, { mode: 384, flag: "wx" });
|
|
4963
|
+
const redacted = await safeDesktopSnapshot(recoveredPath);
|
|
4964
|
+
if (!redacted || redacted.text !== retained || redacted.mode !== 384)
|
|
4965
|
+
throw conflict();
|
|
4966
|
+
if (await safeDesktopRead(path) !== raw)
|
|
4967
|
+
throw conflict();
|
|
4968
|
+
await removeDesktopFile(path, receiptSnapshot, assertMetaRestored);
|
|
4969
|
+
await assertMetaRestored();
|
|
4970
|
+
const finalReceipt2 = await safeDesktopSnapshot(recoveredPath);
|
|
4971
|
+
if (!sameDesktopSnapshot(finalReceipt2, redacted)) {
|
|
4972
|
+
const foreignPath = recoveredPath + ".foreign-" + randomUUID();
|
|
4973
|
+
if (finalReceipt2 !== undefined)
|
|
4974
|
+
await rename2(recoveredPath, foreignPath);
|
|
4975
|
+
await writeDesktopPrivate(recoveredPath, retained, 384, undefined, { current: undefined });
|
|
4976
|
+
throw new Fault(409, "desktop_config_changed", `Desktop recovery receipt changed; redacted receipt preserved at ${recoveredPath}; foreign receipt preserved at ${foreignPath}.`);
|
|
4977
|
+
}
|
|
4978
|
+
return;
|
|
4979
|
+
}
|
|
4980
|
+
await rename2(path, recoveredPath);
|
|
4981
|
+
const preserveRecovered = async (snapshot) => {
|
|
4982
|
+
const foreignPath = recoveredPath + ".foreign-" + randomUUID();
|
|
4983
|
+
if (snapshot !== undefined)
|
|
4984
|
+
await rename2(recoveredPath, foreignPath);
|
|
4985
|
+
await writeDesktopPrivate(recoveredPath, raw, 384, undefined, { current: undefined });
|
|
4986
|
+
throw new Fault(409, "desktop_config_changed", `Desktop recovery receipt changed; original receipt preserved at ${recoveredPath}; foreign receipt preserved at ${foreignPath}.`);
|
|
4987
|
+
};
|
|
4988
|
+
const recoveredSnapshot = await safeDesktopSnapshot(recoveredPath);
|
|
4989
|
+
if (!sameDesktopSnapshot(recoveredSnapshot, receiptSnapshot))
|
|
4990
|
+
await preserveRecovered(recoveredSnapshot);
|
|
4991
|
+
await assertMetaRestored();
|
|
4992
|
+
const finalReceipt = await safeDesktopSnapshot(recoveredPath);
|
|
4993
|
+
if (!sameDesktopSnapshot(finalReceipt, receiptSnapshot))
|
|
4994
|
+
await preserveRecovered(finalReceipt);
|
|
4654
4995
|
}
|
|
4655
4996
|
async function inspectDesktopSelection(library) {
|
|
4656
4997
|
const snapshot = await safeDesktopSnapshot(join2(library, "_meta.json")), raw = snapshot?.text ?? null, m = metadata(raw);
|
|
@@ -4669,24 +5010,37 @@ async function inspectDesktopSelection(library) {
|
|
|
4669
5010
|
}
|
|
4670
5011
|
const recovery = await safeDesktopRead(join2(library, ".switcher-selection.json"));
|
|
4671
5012
|
if (recovery !== undefined) {
|
|
4672
|
-
const r = receipt(recovery), generated = await safeDesktopRead(join2(library, r.id + ".json"));
|
|
5013
|
+
const r = upgradeReceipt(receipt(recovery), snapshot), generated = await safeDesktopRead(join2(library, r.id + ".json"));
|
|
4673
5014
|
await selectedUnchanged(library, r);
|
|
4674
|
-
const wasApplied = matches(snapshot, r.appliedMeta, 384);
|
|
4675
|
-
|
|
5015
|
+
const wasApplied = matches(snapshot, r.appliedMeta, 384, r.appliedMetadataHash);
|
|
5016
|
+
await preservedSelection(library, r, wasApplied);
|
|
5017
|
+
if (!matches(snapshot, r.previousMeta, r.previousMetaMode, r.previousMetadataHash) && !wasApplied || generated !== undefined && hash(generated) !== r.configHash || wasApplied && generated === undefined)
|
|
4676
5018
|
throw conflict();
|
|
4677
5019
|
}
|
|
4678
|
-
return { metadataHash:
|
|
5020
|
+
return { metadataHash: snapshot === undefined ? null : hash(JSON.stringify(snapshot)), metadataMode: snapshot?.mode ?? null, selectedHash: selected === undefined ? null : hash(selected), recoveryHash: recovery === undefined ? null : hash(recovery) };
|
|
4679
5021
|
}
|
|
4680
5022
|
async function applyDesktopSelection(library, config, label2, checkpoint) {
|
|
4681
5023
|
await recoverDesktopSelection(library);
|
|
4682
5024
|
const path = join2(library, ".switcher-selection.json"), metaPath = join2(library, "_meta.json");
|
|
4683
5025
|
const original = await safeDesktopSnapshot(metaPath), previousMeta = original?.text ?? null, previousMetaMode = original?.mode ?? null, previous = metadata(previousMeta);
|
|
4684
|
-
const
|
|
5026
|
+
const previousSelectedText = previous.appliedId ? await safeDesktopRead(join2(library, previous.appliedId + ".json")) : undefined;
|
|
5027
|
+
const previousSelected = previous.appliedId ? { id: previous.appliedId, hash: previousSelectedText === undefined ? null : hash(previousSelectedText) } : null;
|
|
4685
5028
|
const id = randomUUID(), configPath = join2(library, id + ".json"), text = JSON.stringify(config, null, 2) + `
|
|
4686
5029
|
`;
|
|
4687
5030
|
const appliedMeta = JSON.stringify({ ...previous, appliedId: id, entries: [...previous.entries, { id, name: label2 }] }, null, 2) + `
|
|
4688
5031
|
`;
|
|
4689
|
-
const
|
|
5032
|
+
const previousMetadataHash = original?.metadataHash ?? null;
|
|
5033
|
+
const r = { version: 4, id, configHash: hash(text), previousMeta, previousMetaMode, previousMetadataHash, appliedMeta, appliedMetadataHash: "0".repeat(64), previousSelected };
|
|
5034
|
+
if (Buffer.byteLength(JSON.stringify(r) + `
|
|
5035
|
+
`) > 1024 * 1024)
|
|
5036
|
+
throw invalid2();
|
|
5037
|
+
if (original)
|
|
5038
|
+
await preserveDesktopFile(metaPath, originalPath(library, id), original);
|
|
5039
|
+
await writeDesktopPrivate(preparedPath(library, id), appliedMeta, 384, original ? { path: originalPath(library, id), snapshot: original } : undefined, { current: undefined });
|
|
5040
|
+
const prepared = await safeDesktopSnapshot(preparedPath(library, id));
|
|
5041
|
+
if (!prepared || prepared.text !== appliedMeta || prepared.mode !== 384)
|
|
5042
|
+
throw conflict();
|
|
5043
|
+
r.appliedMetadataHash = prepared.metadataHash;
|
|
4690
5044
|
const journal = JSON.stringify(r) + `
|
|
4691
5045
|
`;
|
|
4692
5046
|
if (Buffer.byteLength(journal) > 1024 * 1024)
|
|
@@ -4700,7 +5054,7 @@ async function applyDesktopSelection(library, config, label2, checkpoint) {
|
|
|
4700
5054
|
try {
|
|
4701
5055
|
await unchanged();
|
|
4702
5056
|
await checkpoint?.("preserved");
|
|
4703
|
-
if (!matches(await safeDesktopSnapshot(metaPath), previousMeta, previousMetaMode))
|
|
5057
|
+
if (!matches(await safeDesktopSnapshot(metaPath), previousMeta, previousMetaMode, previousMetadataHash))
|
|
4704
5058
|
throw conflict();
|
|
4705
5059
|
await selectedUnchanged(library, r);
|
|
4706
5060
|
await writeFile(configPath, text, { mode: 384, flag: "wx" });
|
|
@@ -4708,10 +5062,14 @@ async function applyDesktopSelection(library, config, label2, checkpoint) {
|
|
|
4708
5062
|
throw conflict();
|
|
4709
5063
|
await checkpoint?.("config");
|
|
4710
5064
|
await unchanged();
|
|
4711
|
-
|
|
5065
|
+
const preparedCurrent = await safeDesktopSnapshot(preparedPath(library, id));
|
|
5066
|
+
if (!preparedCurrent || !matches(preparedCurrent, appliedMeta, 384, r.appliedMetadataHash))
|
|
5067
|
+
throw conflict();
|
|
5068
|
+
const currentMeta = await safeDesktopSnapshot(metaPath);
|
|
5069
|
+
if (!matches(currentMeta, previousMeta, previousMetaMode, previousMetadataHash))
|
|
4712
5070
|
throw conflict();
|
|
4713
|
-
await
|
|
4714
|
-
if (!matches(await safeDesktopSnapshot(metaPath), appliedMeta, 384))
|
|
5071
|
+
await replaceDesktopFile(metaPath, preparedPath(library, id), currentMeta, preparedCurrent);
|
|
5072
|
+
if (!matches(await safeDesktopSnapshot(metaPath), appliedMeta, 384, r.appliedMetadataHash))
|
|
4715
5073
|
throw conflict();
|
|
4716
5074
|
await checkpoint?.("selected");
|
|
4717
5075
|
} catch (error) {
|
|
@@ -4724,9 +5082,11 @@ async function applyDesktopSelection(library, config, label2, checkpoint) {
|
|
|
4724
5082
|
const assertCurrentSync = () => {
|
|
4725
5083
|
if (safeDesktopSnapshotSync(path)?.text !== journal)
|
|
4726
5084
|
throw conflict();
|
|
4727
|
-
if (
|
|
5085
|
+
if (original && !matches(safeDesktopSnapshotSync(originalPath(library, id)), previousMeta, previousMetaMode, previousMetadataHash))
|
|
5086
|
+
throw conflict();
|
|
5087
|
+
if (!selectedUnchangedSync(library, previousSelected))
|
|
4728
5088
|
throw conflict();
|
|
4729
|
-
if (!matches(safeDesktopSnapshotSync(metaPath), appliedMeta, 384) || safeDesktopSnapshotSync(configPath)?.text !== text)
|
|
5089
|
+
if (!matches(safeDesktopSnapshotSync(metaPath), appliedMeta, 384, r.appliedMetadataHash) || safeDesktopSnapshotSync(configPath)?.text !== text)
|
|
4730
5090
|
throw conflict();
|
|
4731
5091
|
};
|
|
4732
5092
|
return {
|
|
@@ -4845,8 +5205,9 @@ function assertNativeSettings(settings) {
|
|
|
4845
5205
|
if (["model", "modelPicker", "fallbackModel", "apiKeyHelper"].some((key) => (key in data)) || data.env !== undefined && (!data.env || typeof data.env !== "object" || Array.isArray(data.env) || Object.keys(data.env).some((key) => /^(?:ANTHROPIC_|CLAUDE_CODE_USE_|CLAUDE_CODE_OAUTH_TOKEN$)/.test(key))))
|
|
4846
5206
|
throw invalid3("desktop_settings_routing", "Claude native settings contain unresolved endpoint, credential or model routing; resolve native precedence before using this gateway.");
|
|
4847
5207
|
}
|
|
5208
|
+
var NATIVE_LAYOUT_LIMIT = 20000;
|
|
4848
5209
|
async function canonicalRoot(path) {
|
|
4849
|
-
if (typeof path !== "string" || !isAbsolute3(path) ||
|
|
5210
|
+
if (typeof path !== "string" || !isAbsolute3(path) || resolve3(path) !== path)
|
|
4850
5211
|
throw invalid3("desktop_config_root", "Claude Desktop requires an absolute canonical native root.");
|
|
4851
5212
|
await resolveNativeState("claude", { HASNA_CLAUDE_STATE_HOME: path }, { create: false });
|
|
4852
5213
|
await assertNativeStateDirectory(path);
|
|
@@ -4856,7 +5217,7 @@ async function canonicalRoot(path) {
|
|
|
4856
5217
|
if (depth > 64)
|
|
4857
5218
|
throw invalid3("desktop_layout_limit", "Claude Desktop native layout exceeds bounded inspection depth.");
|
|
4858
5219
|
for await (const entry of await opendir2(dir)) {
|
|
4859
|
-
if (++count >
|
|
5220
|
+
if (++count > NATIVE_LAYOUT_LIMIT)
|
|
4860
5221
|
throw invalid3("desktop_layout_limit", "Claude Desktop native layout exceeds bounded inspection size.");
|
|
4861
5222
|
const child = join4(dir, entry.name), info2 = await lstat4(child);
|
|
4862
5223
|
if (info2.isSymbolicLink()) {
|