@hasna/hooks 0.12.23 → 0.13.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 +2 -1
- package/bin/hooks-mcp.js +1198 -228
- package/bin/index.js +1881 -695
- package/bin/native-safety-worker.cjs +1 -1
- package/bin/serve.js +4 -0
- package/dist/index.js +1332 -294
- package/dist/lib/installer.d.ts +44 -5
- package/dist/lib/manifest.d.ts +21 -0
- package/dist/lib/native-target.d.ts +11 -2
- package/dist/lib/registry.d.ts +12 -0
- package/dist/lib/target-adapters.d.ts +151 -0
- package/dist/lib/targets.d.ts +99 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4794,6 +4794,8 @@ var init_manifest = __esm(() => {
|
|
|
4794
4794
|
script_kind: exports_external.enum(["inline", "file"]).optional(),
|
|
4795
4795
|
args: exports_external.array(exports_external.string()).optional(),
|
|
4796
4796
|
nativeTargetDispatch: exports_external.boolean().optional(),
|
|
4797
|
+
requires_final_message: exports_external.boolean().optional(),
|
|
4798
|
+
requires_block: exports_external.boolean().optional(),
|
|
4797
4799
|
timeout_ms: exports_external.number().int().positive().optional(),
|
|
4798
4800
|
env: exports_external.record(exports_external.string(), exports_external.string()).optional()
|
|
4799
4801
|
});
|
|
@@ -4874,7 +4876,7 @@ __export(exports_store, {
|
|
|
4874
4876
|
});
|
|
4875
4877
|
import { createHash as createHash4 } from "crypto";
|
|
4876
4878
|
import { existsSync as existsSync8, mkdirSync as mkdirSync3, readFileSync as readFileSync5, writeFileSync as writeFileSync2, renameSync, lstatSync as lstatSync3 } from "fs";
|
|
4877
|
-
import { join as
|
|
4879
|
+
import { join as join10 } from "path";
|
|
4878
4880
|
import { readFile } from "fs/promises";
|
|
4879
4881
|
import { randomBytes } from "crypto";
|
|
4880
4882
|
function sha256Of(input) {
|
|
@@ -4978,13 +4980,13 @@ function getPinnedHook(name) {
|
|
|
4978
4980
|
return readLock().hooks[name];
|
|
4979
4981
|
}
|
|
4980
4982
|
function registryManagedMarker(name) {
|
|
4981
|
-
return
|
|
4983
|
+
return join10(getHooksDataDir(), "registry-managed", sha256Of(name) + ".json");
|
|
4982
4984
|
}
|
|
4983
4985
|
function markRegistryManaged(name) {
|
|
4984
4986
|
const path = registryManagedMarker(name);
|
|
4985
4987
|
const content = JSON.stringify({ schema: 1, name }) + `
|
|
4986
4988
|
`;
|
|
4987
|
-
mkdirSync3(
|
|
4989
|
+
mkdirSync3(join10(getHooksDataDir(), "registry-managed"), { recursive: true, mode: 448 });
|
|
4988
4990
|
try {
|
|
4989
4991
|
writeFileSync2(path, content, { flag: "wx", mode: 384 });
|
|
4990
4992
|
} catch (error) {
|
|
@@ -5140,21 +5142,21 @@ __export(exports_profiles, {
|
|
|
5140
5142
|
deleteProfile: () => deleteProfile,
|
|
5141
5143
|
createProfile: () => createProfile
|
|
5142
5144
|
});
|
|
5143
|
-
import { existsSync as
|
|
5145
|
+
import { existsSync as existsSync12, mkdirSync as mkdirSync6, readFileSync as readFileSync7, writeFileSync as writeFileSync7, readdirSync as readdirSync4, rmSync as rmSync3, cpSync as cpSync2 } from "fs";
|
|
5144
5146
|
import { join as join20 } from "path";
|
|
5145
5147
|
function getProfilesDir() {
|
|
5146
5148
|
const newDir = join20(getEffectiveDataRoot(), "profiles");
|
|
5147
5149
|
const oldDir = join20(getHomeDir(), ".hooks", "profiles");
|
|
5148
|
-
if (!
|
|
5149
|
-
|
|
5150
|
+
if (!existsSync12(newDir) && existsSync12(oldDir)) {
|
|
5151
|
+
mkdirSync6(newDir, { recursive: true });
|
|
5150
5152
|
cpSync2(oldDir, newDir, { recursive: true });
|
|
5151
5153
|
}
|
|
5152
5154
|
return newDir;
|
|
5153
5155
|
}
|
|
5154
5156
|
function ensureProfilesDir() {
|
|
5155
5157
|
const dir = getProfilesDir();
|
|
5156
|
-
if (!
|
|
5157
|
-
|
|
5158
|
+
if (!existsSync12(dir)) {
|
|
5159
|
+
mkdirSync6(dir, { recursive: true });
|
|
5158
5160
|
}
|
|
5159
5161
|
}
|
|
5160
5162
|
function profilePath(id) {
|
|
@@ -5177,30 +5179,30 @@ function createProfile(input2) {
|
|
|
5177
5179
|
if (input2.name) {
|
|
5178
5180
|
profile.name = input2.name;
|
|
5179
5181
|
}
|
|
5180
|
-
|
|
5182
|
+
writeFileSync7(profilePath(id), JSON.stringify(profile, null, 2) + `
|
|
5181
5183
|
`);
|
|
5182
5184
|
return profile;
|
|
5183
5185
|
}
|
|
5184
5186
|
function getProfile(id) {
|
|
5185
5187
|
const path = profilePath(id);
|
|
5186
5188
|
try {
|
|
5187
|
-
if (!
|
|
5189
|
+
if (!existsSync12(path))
|
|
5188
5190
|
return null;
|
|
5189
|
-
return JSON.parse(
|
|
5191
|
+
return JSON.parse(readFileSync7(path, "utf-8"));
|
|
5190
5192
|
} catch {
|
|
5191
5193
|
return null;
|
|
5192
5194
|
}
|
|
5193
5195
|
}
|
|
5194
5196
|
function listProfiles() {
|
|
5195
5197
|
const dir = getProfilesDir();
|
|
5196
|
-
if (!
|
|
5198
|
+
if (!existsSync12(dir))
|
|
5197
5199
|
return [];
|
|
5198
5200
|
try {
|
|
5199
|
-
const files =
|
|
5201
|
+
const files = readdirSync4(dir).filter((f) => f.endsWith(".json"));
|
|
5200
5202
|
const profiles = [];
|
|
5201
5203
|
for (const file2 of files) {
|
|
5202
5204
|
try {
|
|
5203
|
-
const content =
|
|
5205
|
+
const content = readFileSync7(join20(dir, file2), "utf-8");
|
|
5204
5206
|
profiles.push(JSON.parse(content));
|
|
5205
5207
|
} catch {}
|
|
5206
5208
|
}
|
|
@@ -5217,13 +5219,13 @@ function updateProfile(id, data) {
|
|
|
5217
5219
|
profile.name = data.name;
|
|
5218
5220
|
if (data.preferences !== undefined)
|
|
5219
5221
|
profile.preferences = data.preferences;
|
|
5220
|
-
|
|
5222
|
+
writeFileSync7(profilePath(id), JSON.stringify(profile, null, 2) + `
|
|
5221
5223
|
`);
|
|
5222
5224
|
return profile;
|
|
5223
5225
|
}
|
|
5224
5226
|
function deleteProfile(id) {
|
|
5225
5227
|
const path = profilePath(id);
|
|
5226
|
-
if (!
|
|
5228
|
+
if (!existsSync12(path))
|
|
5227
5229
|
return false;
|
|
5228
5230
|
try {
|
|
5229
5231
|
rmSync3(path);
|
|
@@ -5237,7 +5239,7 @@ function touchProfile(id) {
|
|
|
5237
5239
|
if (!profile)
|
|
5238
5240
|
return;
|
|
5239
5241
|
profile.last_seen_at = new Date().toISOString();
|
|
5240
|
-
|
|
5242
|
+
writeFileSync7(profilePath(id), JSON.stringify(profile, null, 2) + `
|
|
5241
5243
|
`);
|
|
5242
5244
|
}
|
|
5243
5245
|
function exportProfiles() {
|
|
@@ -5253,11 +5255,11 @@ function importProfiles(profiles) {
|
|
|
5253
5255
|
continue;
|
|
5254
5256
|
}
|
|
5255
5257
|
const path = profilePath(profile.agent_id);
|
|
5256
|
-
if (
|
|
5258
|
+
if (existsSync12(path)) {
|
|
5257
5259
|
skipped++;
|
|
5258
5260
|
continue;
|
|
5259
5261
|
}
|
|
5260
|
-
|
|
5262
|
+
writeFileSync7(path, JSON.stringify(profile, null, 2) + `
|
|
5261
5263
|
`);
|
|
5262
5264
|
imported++;
|
|
5263
5265
|
}
|
|
@@ -5269,16 +5271,16 @@ var init_profiles = __esm(() => {
|
|
|
5269
5271
|
|
|
5270
5272
|
// ../contracts/dist/client/transport.js
|
|
5271
5273
|
import { isIP } from "net";
|
|
5272
|
-
import { spawnSync } from "child_process";
|
|
5274
|
+
import { spawnSync as spawnSync4 } from "child_process";
|
|
5273
5275
|
import { closeSync as closeSync23, fstatSync as fstatSync23, openSync as openSync23, readFileSync as readFileSync22 } from "fs";
|
|
5274
5276
|
import { O_NOFOLLOW as O_NOFOLLOW2, O_NONBLOCK as O_NONBLOCK2, O_RDONLY as O_RDONLY2 } from "constants";
|
|
5275
5277
|
import { hostname as osHostname } from "os";
|
|
5276
|
-
import { isAbsolute as isAbsolute9, join as
|
|
5278
|
+
import { isAbsolute as isAbsolute9, join as join210 } from "path";
|
|
5277
5279
|
import { createRequire as createRequire2 } from "module";
|
|
5278
5280
|
import { fileURLToPath as fileURLToPath22 } from "url";
|
|
5279
|
-
import { closeSync as closeSync5, fstatSync as fstatSync5, lstatSync as lstatSync16, openSync as openSync5, readFileSync as
|
|
5281
|
+
import { closeSync as closeSync5, fstatSync as fstatSync5, lstatSync as lstatSync16, openSync as openSync5, readFileSync as readFileSync18, realpathSync as realpathSync7, statSync as statSync2 } from "fs";
|
|
5280
5282
|
import { O_NOFOLLOW, O_NONBLOCK, O_RDONLY } from "constants";
|
|
5281
|
-
import { basename as
|
|
5283
|
+
import { basename as basename16, dirname as dirname23, join as join32, relative, sep as sep10 } from "path";
|
|
5282
5284
|
import { fileURLToPath as fileURLToPath4, pathToFileURL } from "url";
|
|
5283
5285
|
function envToken2(name) {
|
|
5284
5286
|
return name.toUpperCase().replace(/-/g, "_");
|
|
@@ -5353,13 +5355,13 @@ function o(e2, r2, t2) {
|
|
|
5353
5355
|
}
|
|
5354
5356
|
function entryPoint(directory3) {
|
|
5355
5357
|
const root = realpathSync7(directory3);
|
|
5356
|
-
const fd = openSync5(
|
|
5358
|
+
const fd = openSync5(join32(root, "package.json"), O_RDONLY | O_NOFOLLOW | O_NONBLOCK);
|
|
5357
5359
|
let pkg;
|
|
5358
5360
|
try {
|
|
5359
5361
|
const stat2 = fstatSync5(fd);
|
|
5360
5362
|
if (!stat2.isFile() || stat2.size > MAX_PACKAGE_BYTES2)
|
|
5361
5363
|
throw new Error("Invalid Secrets package metadata");
|
|
5362
|
-
pkg = JSON.parse(
|
|
5364
|
+
pkg = JSON.parse(readFileSync18(fd, "utf8"));
|
|
5363
5365
|
} finally {
|
|
5364
5366
|
closeSync5(fd);
|
|
5365
5367
|
}
|
|
@@ -5373,18 +5375,18 @@ function entryPoint(directory3) {
|
|
|
5373
5375
|
if (/[\\%?#\0]/.test(target) || parts.some((part) => !part || part === "." || part === ".." || part === "node_modules")) {
|
|
5374
5376
|
throw new Error("Invalid Secrets import export");
|
|
5375
5377
|
}
|
|
5376
|
-
const file2 = realpathSync7(
|
|
5378
|
+
const file2 = realpathSync7(join32(root, ...parts));
|
|
5377
5379
|
const within = relative(root, file2);
|
|
5378
|
-
if (!within || within === ".." || within.startsWith(`..${sep10}`) || !
|
|
5380
|
+
if (!within || within === ".." || within.startsWith(`..${sep10}`) || !statSync2(file2).isFile()) {
|
|
5379
5381
|
throw new Error("Secrets import export escapes its package or is not a file");
|
|
5380
5382
|
}
|
|
5381
5383
|
return pathToFileURL(file2).href;
|
|
5382
5384
|
}
|
|
5383
5385
|
function resolveInstalledSecrets(parentUrl) {
|
|
5384
|
-
let directory3 =
|
|
5386
|
+
let directory3 = dirname23(fileURLToPath4(parentUrl));
|
|
5385
5387
|
for (;; ) {
|
|
5386
|
-
if (
|
|
5387
|
-
const candidate =
|
|
5388
|
+
if (basename16(directory3) !== "node_modules") {
|
|
5389
|
+
const candidate = join32(directory3, "node_modules", PACKAGE);
|
|
5388
5390
|
let absent = false;
|
|
5389
5391
|
try {
|
|
5390
5392
|
lstatSync16(candidate);
|
|
@@ -5396,7 +5398,7 @@ function resolveInstalledSecrets(parentUrl) {
|
|
|
5396
5398
|
if (!absent)
|
|
5397
5399
|
return entryPoint(candidate);
|
|
5398
5400
|
}
|
|
5399
|
-
const parent =
|
|
5401
|
+
const parent = dirname23(directory3);
|
|
5400
5402
|
if (parent === directory3)
|
|
5401
5403
|
throw new Error("Secrets SDK is not installed for this consumer");
|
|
5402
5404
|
directory3 = parent;
|
|
@@ -5415,14 +5417,14 @@ function hasnaHomeDir(env) {
|
|
|
5415
5417
|
if (override)
|
|
5416
5418
|
return override;
|
|
5417
5419
|
const home = homeDir(env);
|
|
5418
|
-
return home ?
|
|
5420
|
+
return home ? join210(home, HASNA_HOME_DIR) : null;
|
|
5419
5421
|
}
|
|
5420
5422
|
function appConfigDir(name, env) {
|
|
5421
5423
|
const configRoot = absoluteOverride(env, HASNA_CONFIG_HOME_ENV_KEY);
|
|
5422
5424
|
if (configRoot)
|
|
5423
|
-
return
|
|
5425
|
+
return join210(configRoot, name);
|
|
5424
5426
|
const root = hasnaHomeDir(env);
|
|
5425
|
-
return root ?
|
|
5427
|
+
return root ? join210(root, name, CONFIG_SUBDIR) : null;
|
|
5426
5428
|
}
|
|
5427
5429
|
function credentialDiskSourceList(name, env, profile = null) {
|
|
5428
5430
|
if (!SAFE_APP_SLUG.test(name))
|
|
@@ -5431,7 +5433,7 @@ function credentialDiskSourceList(name, env, profile = null) {
|
|
|
5431
5433
|
if (!directory3)
|
|
5432
5434
|
return [];
|
|
5433
5435
|
const file2 = profile ? `${CREDENTIALS_FILE}-${profile}` : CREDENTIALS_FILE;
|
|
5434
|
-
return [{ path:
|
|
5436
|
+
return [{ path: join210(directory3, file2), tier: "disk" }];
|
|
5435
5437
|
}
|
|
5436
5438
|
function credentialDiskSources(name, env) {
|
|
5437
5439
|
return credentialDiskSourceList(name, env, null).map((s) => s.path);
|
|
@@ -5639,7 +5641,7 @@ function isAmbientEnvironment(env) {
|
|
|
5639
5641
|
return env === process.env || env[AMBIENT_ENVIRONMENT2] === true;
|
|
5640
5642
|
}
|
|
5641
5643
|
function defaultKeychainRunner(argv) {
|
|
5642
|
-
const result =
|
|
5644
|
+
const result = spawnSync4(KEYCHAIN_SECURITY_BIN, [...argv], {
|
|
5643
5645
|
encoding: "utf8",
|
|
5644
5646
|
stdio: ["ignore", "pipe", "pipe"],
|
|
5645
5647
|
timeout: KEYCHAIN_SPAWN_TIMEOUT_MS
|
|
@@ -6481,9 +6483,9 @@ var init_stop_review_context = __esm(() => {
|
|
|
6481
6483
|
});
|
|
6482
6484
|
|
|
6483
6485
|
// src/lib/hook-env.ts
|
|
6484
|
-
import { dirname as
|
|
6485
|
-
import { existsSync as
|
|
6486
|
-
import { tmpdir as
|
|
6486
|
+
import { dirname as dirname37, isAbsolute as isAbsolute35 } from "path";
|
|
6487
|
+
import { existsSync as existsSync18, realpathSync as realpathSync19, statSync as statSync3 } from "fs";
|
|
6488
|
+
import { tmpdir as tmpdir3 } from "os";
|
|
6487
6489
|
function isDeniedEnvName(name) {
|
|
6488
6490
|
const upper = name.toUpperCase();
|
|
6489
6491
|
if (DENY_PREFIXES.some((prefix3) => upper.startsWith(prefix3)))
|
|
@@ -6517,22 +6519,22 @@ function isUnsafePathEntry(entry, home) {
|
|
|
6517
6519
|
}
|
|
6518
6520
|
};
|
|
6519
6521
|
const resolved = canonical10(entry);
|
|
6520
|
-
for (const root of [home,
|
|
6522
|
+
for (const root of [home, tmpdir3(), "/tmp", "/var/tmp"].filter((value) => Boolean(value))) {
|
|
6521
6523
|
const blocked = canonical10(root);
|
|
6522
6524
|
if (resolved === blocked || resolved.startsWith(blocked + "/"))
|
|
6523
6525
|
return true;
|
|
6524
6526
|
}
|
|
6525
6527
|
try {
|
|
6526
|
-
if ((
|
|
6528
|
+
if ((statSync3(entry).mode & 2) !== 0)
|
|
6527
6529
|
return true;
|
|
6528
6530
|
} catch {}
|
|
6529
6531
|
return false;
|
|
6530
6532
|
}
|
|
6531
6533
|
function sanitizeHookPath(pathValue, home) {
|
|
6532
6534
|
const baseline = PATH_BASELINE.slice();
|
|
6533
|
-
if (process.platform === "darwin" &&
|
|
6535
|
+
if (process.platform === "darwin" && existsSync18("/opt/homebrew/bin"))
|
|
6534
6536
|
baseline.push("/opt/homebrew/bin");
|
|
6535
|
-
const bunDir =
|
|
6537
|
+
const bunDir = dirname37(process.execPath);
|
|
6536
6538
|
const seen = new Set;
|
|
6537
6539
|
const out = [];
|
|
6538
6540
|
for (const dir of baseline) {
|
|
@@ -6665,10 +6667,10 @@ __export(exports_run, {
|
|
|
6665
6667
|
BUN_ISOLATION_FLAGS: () => BUN_ISOLATION_FLAGS
|
|
6666
6668
|
});
|
|
6667
6669
|
import { randomBytes as randomBytes3 } from "crypto";
|
|
6668
|
-
import { rmSync as rmSync6, writeFileSync as
|
|
6669
|
-
import { basename as
|
|
6670
|
+
import { rmSync as rmSync6, writeFileSync as writeFileSync15 } from "fs";
|
|
6671
|
+
import { basename as basename19, dirname as dirname38, isAbsolute as isAbsolute36, join as join45 } from "path";
|
|
6670
6672
|
function extensionOf(scriptPath) {
|
|
6671
|
-
const base =
|
|
6673
|
+
const base = basename19(scriptPath);
|
|
6672
6674
|
const dot = base.lastIndexOf(".");
|
|
6673
6675
|
return dot > 0 ? base.slice(dot + 1) : "ts";
|
|
6674
6676
|
}
|
|
@@ -6680,9 +6682,9 @@ function shebangInterpreter(firstLine) {
|
|
|
6680
6682
|
return null;
|
|
6681
6683
|
const parts = rest.split(/\s+/);
|
|
6682
6684
|
if (parts[0] === "env" || parts[0] === "/usr/bin/env") {
|
|
6683
|
-
return parts[1] && parts[1] !== "-S" ?
|
|
6685
|
+
return parts[1] && parts[1] !== "-S" ? basename19(parts[1]) : parts[2] ? basename19(parts[2]) : null;
|
|
6684
6686
|
}
|
|
6685
|
-
return
|
|
6687
|
+
return basename19(parts[0]);
|
|
6686
6688
|
}
|
|
6687
6689
|
function interpreterFor(name, scriptPath, content) {
|
|
6688
6690
|
const ext = extensionOf(scriptPath);
|
|
@@ -6735,9 +6737,9 @@ async function readPipeWithDeadline(stream, deadlineMs) {
|
|
|
6735
6737
|
return chunks.join("");
|
|
6736
6738
|
}
|
|
6737
6739
|
async function executeVerifiedScript(options) {
|
|
6738
|
-
const scriptDir =
|
|
6740
|
+
const scriptDir = dirname38(options.scriptPath);
|
|
6739
6741
|
const interpreter = interpreterFor(options.name, options.scriptPath, options.content);
|
|
6740
|
-
const tempPath =
|
|
6742
|
+
const tempPath = join45(scriptDir, `.hooks-verified-${randomBytes3(12).toString("hex")}.${interpreter.tempExt}`);
|
|
6741
6743
|
let proc = null;
|
|
6742
6744
|
let timedOut = false;
|
|
6743
6745
|
const timeoutTimer = options.timeout ? setTimeout(() => {
|
|
@@ -6745,7 +6747,7 @@ async function executeVerifiedScript(options) {
|
|
|
6745
6747
|
killGroup(proc, { leaderAlive: proc !== null && proc.exitCode === null });
|
|
6746
6748
|
}, options.timeout) : null;
|
|
6747
6749
|
try {
|
|
6748
|
-
|
|
6750
|
+
writeFileSync15(tempPath, options.content, { flag: "wx", mode: 384 });
|
|
6749
6751
|
proc = Bun.spawn([...interpreter.command, isAbsolute36(tempPath) ? tempPath : `./${tempPath}`, ...options.args ?? []], {
|
|
6750
6752
|
stdin: new Response(options.stdin),
|
|
6751
6753
|
stdout: "pipe",
|
|
@@ -7921,9 +7923,9 @@ function getHook(name) {
|
|
|
7921
7923
|
return HOOKS.find((h) => h.name === name);
|
|
7922
7924
|
}
|
|
7923
7925
|
// src/lib/installer.ts
|
|
7924
|
-
import { existsSync as
|
|
7925
|
-
import { join as
|
|
7926
|
-
import { homedir as
|
|
7926
|
+
import { existsSync as existsSync14, mkdirSync as mkdirSync12, readFileSync as readFileSync13, readdirSync as readdirSync12, realpathSync as realpathSync4, rmdirSync as rmdirSync4, unlinkSync as unlinkSync6, writeFileSync as writeFileSync10 } from "fs";
|
|
7927
|
+
import { join as join24, dirname as dirname16, basename as basename12, resolve as resolve28 } from "path";
|
|
7928
|
+
import { homedir as homedir8 } from "os";
|
|
7927
7929
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
7928
7930
|
|
|
7929
7931
|
// src/lib/readiness-process.ts
|
|
@@ -8048,6 +8050,8 @@ function resolveHookMeta(name) {
|
|
|
8048
8050
|
matcher: "",
|
|
8049
8051
|
tags: ["custom"],
|
|
8050
8052
|
nativeTargetDispatch: manifest.nativeTargetDispatch,
|
|
8053
|
+
requiresFinalMessage: manifest.requires_final_message,
|
|
8054
|
+
requiresBlock: manifest.requires_block,
|
|
8051
8055
|
timeoutSeconds: manifest.timeout_ms === undefined ? undefined : Math.ceil(manifest.timeout_ms / 1000)
|
|
8052
8056
|
};
|
|
8053
8057
|
}
|
|
@@ -8530,8 +8534,243 @@ function retainedMementosRegistration(commands) {
|
|
|
8530
8534
|
return values[0];
|
|
8531
8535
|
}
|
|
8532
8536
|
|
|
8537
|
+
// src/lib/targets.ts
|
|
8538
|
+
import { homedir as homedir5 } from "os";
|
|
8539
|
+
import { dirname as dirname5, join as join9, resolve as resolve3 } from "path";
|
|
8540
|
+
var TARGET_NAMES = [
|
|
8541
|
+
"claude",
|
|
8542
|
+
"codex",
|
|
8543
|
+
"gemini",
|
|
8544
|
+
"codewith",
|
|
8545
|
+
"qwen",
|
|
8546
|
+
"muse",
|
|
8547
|
+
"kimi",
|
|
8548
|
+
"pi",
|
|
8549
|
+
"cursor",
|
|
8550
|
+
"opencode",
|
|
8551
|
+
"sumi"
|
|
8552
|
+
];
|
|
8553
|
+
var INSTALL_TARGET_NAMES = [
|
|
8554
|
+
"claude",
|
|
8555
|
+
"codex",
|
|
8556
|
+
"gemini",
|
|
8557
|
+
"codewith",
|
|
8558
|
+
"qwen",
|
|
8559
|
+
"muse",
|
|
8560
|
+
"kimi",
|
|
8561
|
+
"pi",
|
|
8562
|
+
"cursor"
|
|
8563
|
+
];
|
|
8564
|
+
function isTargetName(value) {
|
|
8565
|
+
return typeof value === "string" && TARGET_NAMES.includes(value);
|
|
8566
|
+
}
|
|
8567
|
+
var settingsJson = (dir) => (o) => o.scope === "project" ? join9(resolve3(o.cwd), dir, "settings.json") : join9(o.home, dir, "settings.json");
|
|
8568
|
+
var DEFINITIONS = {
|
|
8569
|
+
claude: {
|
|
8570
|
+
install: true,
|
|
8571
|
+
run: true,
|
|
8572
|
+
stopEvent: "Stop",
|
|
8573
|
+
canBlock: true,
|
|
8574
|
+
deliversFinalMessage: true,
|
|
8575
|
+
finalMessageField: "last_assistant_message",
|
|
8576
|
+
path: settingsJson(".claude")
|
|
8577
|
+
},
|
|
8578
|
+
codex: {
|
|
8579
|
+
install: true,
|
|
8580
|
+
run: true,
|
|
8581
|
+
stopEvent: "Stop",
|
|
8582
|
+
canBlock: true,
|
|
8583
|
+
deliversFinalMessage: true,
|
|
8584
|
+
finalMessageField: "last_assistant_message",
|
|
8585
|
+
path: (o) => o.scope === "project" ? join9(resolve3(o.cwd), ".codex", "hooks.json") : join9(o.home, ".codex", "hooks.json")
|
|
8586
|
+
},
|
|
8587
|
+
gemini: {
|
|
8588
|
+
install: true,
|
|
8589
|
+
run: true,
|
|
8590
|
+
stopEvent: "AfterAgent",
|
|
8591
|
+
canBlock: true,
|
|
8592
|
+
deliversFinalMessage: true,
|
|
8593
|
+
finalMessageField: "prompt_response",
|
|
8594
|
+
path: settingsJson(".gemini")
|
|
8595
|
+
},
|
|
8596
|
+
codewith: {
|
|
8597
|
+
install: true,
|
|
8598
|
+
run: true,
|
|
8599
|
+
stopEvent: "Stop",
|
|
8600
|
+
canBlock: true,
|
|
8601
|
+
deliversFinalMessage: true,
|
|
8602
|
+
finalMessageField: "last_assistant_message",
|
|
8603
|
+
path: (o) => o.scope === "project" ? join9(resolve3(o.cwd), ".codewith", "config.toml") : join9(o.home, ".codewith", "config.toml")
|
|
8604
|
+
},
|
|
8605
|
+
qwen: {
|
|
8606
|
+
install: true,
|
|
8607
|
+
run: true,
|
|
8608
|
+
stopEvent: "Stop",
|
|
8609
|
+
canBlock: true,
|
|
8610
|
+
deliversFinalMessage: true,
|
|
8611
|
+
finalMessageField: "last_assistant_message",
|
|
8612
|
+
path: settingsJson(".qwen")
|
|
8613
|
+
},
|
|
8614
|
+
muse: {
|
|
8615
|
+
install: true,
|
|
8616
|
+
run: true,
|
|
8617
|
+
stopEvent: "Stop",
|
|
8618
|
+
canBlock: true,
|
|
8619
|
+
deliversFinalMessage: true,
|
|
8620
|
+
finalMessageField: "last_assistant_message",
|
|
8621
|
+
installNote: "Muse receives the final message as a quoted JSON string; `hooks run --target muse` parses it once before the hook sees it.",
|
|
8622
|
+
path: (o) => join9(o.home, ".hasna", "hooks", "muse-plugins", o.hook ?? "")
|
|
8623
|
+
},
|
|
8624
|
+
kimi: {
|
|
8625
|
+
install: true,
|
|
8626
|
+
run: true,
|
|
8627
|
+
stopEvent: "Stop",
|
|
8628
|
+
canBlock: true,
|
|
8629
|
+
deliversFinalMessage: false,
|
|
8630
|
+
finalMessageNote: "Kimi's Stop payload carries only hook_event_name, session_id, cwd, client_type, session_title and stop_hook_active",
|
|
8631
|
+
path: (o) => join9(o.home, ".kimi-code", "config.toml")
|
|
8632
|
+
},
|
|
8633
|
+
pi: {
|
|
8634
|
+
install: true,
|
|
8635
|
+
run: true,
|
|
8636
|
+
stopEvent: "turn_end",
|
|
8637
|
+
canBlock: true,
|
|
8638
|
+
deliversFinalMessage: false,
|
|
8639
|
+
finalMessageNote: "the pi-hooks bridge sends only hook_event_name, session_id, cwd and stop_hook_active (transcript_path is declared but never populated)",
|
|
8640
|
+
installNote: "Pi runs hooks through the hasna-pi-hooks bridge: the config points at a Hooks profile, so registration is not by itself proof that a turn was held.",
|
|
8641
|
+
path: (o) => join9(o.home, ".pi", "agent", "state", "hasna-pi-hooks.config.json")
|
|
8642
|
+
},
|
|
8643
|
+
cursor: {
|
|
8644
|
+
install: true,
|
|
8645
|
+
run: true,
|
|
8646
|
+
stopEvent: "stop",
|
|
8647
|
+
canBlock: false,
|
|
8648
|
+
deliversFinalMessage: false,
|
|
8649
|
+
finalMessageNote: "Cursor's stop hook carries no assistant text",
|
|
8650
|
+
blockNote: "a block verdict is ignored, and only followup_message continues a turn (loop_limit 5)",
|
|
8651
|
+
installNote: "Cursor's stop hook cannot block the turn: a block verdict is ignored, and only followup_message continues a turn (loop_limit 5). The registration alone does not hold anything.",
|
|
8652
|
+
path: (o) => o.scope === "project" ? join9(resolve3(o.cwd), ".cursor", "hooks.json") : join9(o.home, ".cursor", "hooks.json")
|
|
8653
|
+
},
|
|
8654
|
+
opencode: {
|
|
8655
|
+
install: false,
|
|
8656
|
+
run: true,
|
|
8657
|
+
stopEvent: null,
|
|
8658
|
+
canBlock: false,
|
|
8659
|
+
deliversFinalMessage: false,
|
|
8660
|
+
blockNote: "opencode has no turn-end event to hold: session.prompt schedules a new agent-loop run instead of continuing the current turn",
|
|
8661
|
+
stopEventSubstitute: "opencode has only the session.idle bus event, which is observe-only and cannot hold the turn; `hooks install stop-review --target opencode` registers the managed observe-and-re-prompt plugin",
|
|
8662
|
+
path: (o) => {
|
|
8663
|
+
const xdg = o.env.XDG_CONFIG_HOME;
|
|
8664
|
+
const base = xdg && xdg.startsWith("/") ? xdg : join9(o.home, ".config");
|
|
8665
|
+
return join9(resolve3(base), "opencode", "opencode.json");
|
|
8666
|
+
}
|
|
8667
|
+
},
|
|
8668
|
+
sumi: {
|
|
8669
|
+
install: false,
|
|
8670
|
+
run: true,
|
|
8671
|
+
stopEvent: "Stop",
|
|
8672
|
+
canBlock: true,
|
|
8673
|
+
deliversFinalMessage: true,
|
|
8674
|
+
finalMessageField: "last_assistant_message",
|
|
8675
|
+
stopEventSubstitute: "Sumi stop review is registered through the managed path: `hooks install stop-review --target sumi`",
|
|
8676
|
+
path: (o) => join9(o.home, ".sumi", "plugins")
|
|
8677
|
+
}
|
|
8678
|
+
};
|
|
8679
|
+
function getTargetCapabilities(target, options = {}) {
|
|
8680
|
+
if (!isTargetName(target))
|
|
8681
|
+
return;
|
|
8682
|
+
const definition = DEFINITIONS[target];
|
|
8683
|
+
const resolved = {
|
|
8684
|
+
scope: options.scope ?? "global",
|
|
8685
|
+
home: options.home ?? homedir5(),
|
|
8686
|
+
cwd: options.cwd ?? process.cwd(),
|
|
8687
|
+
env: options.env ?? process.env,
|
|
8688
|
+
hook: options.hook
|
|
8689
|
+
};
|
|
8690
|
+
return {
|
|
8691
|
+
install: definition.install,
|
|
8692
|
+
run: definition.run,
|
|
8693
|
+
stopEvent: definition.stopEvent,
|
|
8694
|
+
canBlock: definition.canBlock,
|
|
8695
|
+
deliversFinalMessage: definition.deliversFinalMessage,
|
|
8696
|
+
configPath: definition.path(resolved)
|
|
8697
|
+
};
|
|
8698
|
+
}
|
|
8699
|
+
function musePluginRoot(options = {}) {
|
|
8700
|
+
return dirname5(getTargetCapabilities("muse", { ...options, hook: "bundle" }).configPath);
|
|
8701
|
+
}
|
|
8702
|
+
function targetInstallNote(target) {
|
|
8703
|
+
return isTargetName(target) ? DEFINITIONS[target].installNote : undefined;
|
|
8704
|
+
}
|
|
8705
|
+
function runTargetHelp() {
|
|
8706
|
+
return TARGET_NAMES.join(", ");
|
|
8707
|
+
}
|
|
8708
|
+
function blockingInstallTargets() {
|
|
8709
|
+
return INSTALL_TARGET_NAMES.filter((name) => DEFINITIONS[name].canBlock);
|
|
8710
|
+
}
|
|
8711
|
+
function installCapabilityRefusal(hook, target, requirement, options = {}) {
|
|
8712
|
+
const definition = isTargetName(target) ? DEFINITIONS[target] : undefined;
|
|
8713
|
+
if (!definition) {
|
|
8714
|
+
return `Refused: Unknown target '${target}'. Known targets: ${runTargetHelp()}.`;
|
|
8715
|
+
}
|
|
8716
|
+
const capabilities = getTargetCapabilities(target, options);
|
|
8717
|
+
if (requirement.requiresBlock && !capabilities.canBlock) {
|
|
8718
|
+
const others = [];
|
|
8719
|
+
if (!capabilities.install)
|
|
8720
|
+
others.push("no generic install path (install: false)");
|
|
8721
|
+
if (requirement.requiresFinalMessage && !capabilities.deliversFinalMessage)
|
|
8722
|
+
others.push("no final assistant message (deliversFinalMessage: false)");
|
|
8723
|
+
if (requirement.events.includes("Stop") && capabilities.stopEvent === null)
|
|
8724
|
+
others.push("no turn-end event (stopEvent: null)");
|
|
8725
|
+
const also = others.length === 0 ? "" : ` It also has ${others.join(" and ")}.`;
|
|
8726
|
+
return `Refused: '${hook}' declares requires_block, and target '${target}' cannot hold the turn (canBlock: false) \u2014 ${definition.blockNote ?? "a block verdict is ignored there"}.${also} ` + `Unblock it by installing on a target that can block (${blockingInstallTargets().join(", ")}), or by clearing requires_block in the hook's manifest.`;
|
|
8727
|
+
}
|
|
8728
|
+
if (requirement.requiresFinalMessage && !capabilities.deliversFinalMessage) {
|
|
8729
|
+
const others = [];
|
|
8730
|
+
if (!capabilities.install)
|
|
8731
|
+
others.push("no generic install path (install: false)");
|
|
8732
|
+
if (requirement.requiresBlock && !capabilities.canBlock)
|
|
8733
|
+
others.push("no turn-holding verdict (canBlock: false)");
|
|
8734
|
+
if (requirement.events.includes("Stop") && capabilities.stopEvent === null)
|
|
8735
|
+
others.push("no turn-end event (stopEvent: null)");
|
|
8736
|
+
const also = others.length === 0 ? "" : ` It also has ${others.join(" and ")}.`;
|
|
8737
|
+
return `Refused: '${hook}' declares requires_final_message, and target '${target}' does not deliver the assistant's final message (deliversFinalMessage: false) \u2014 ${definition.finalMessageNote ?? "its payload carries no assistant text"}.${also} ` + `Unblock it by installing on a target that delivers the message (${INSTALL_TARGET_NAMES.filter((name) => DEFINITIONS[name].deliversFinalMessage).join(", ")}), or by clearing requires_final_message in the hook's manifest.`;
|
|
8738
|
+
}
|
|
8739
|
+
if (!capabilities.install) {
|
|
8740
|
+
return `Refused: Hooks has no generic install path for target '${target}' (install: false). ${definition.stopEventSubstitute ?? `Supported install targets: ${INSTALL_TARGET_NAMES.join(", ")}.`}`;
|
|
8741
|
+
}
|
|
8742
|
+
if (requirement.events.includes("Stop") && capabilities.stopEvent === null) {
|
|
8743
|
+
return `Refused: '${hook}' runs on the Stop event, and target '${target}' has no turn-end event (stopEvent: null), so nothing would ever run it. ${definition.stopEventSubstitute ?? ""}`.trim();
|
|
8744
|
+
}
|
|
8745
|
+
return;
|
|
8746
|
+
}
|
|
8747
|
+
|
|
8748
|
+
// src/lib/native-target.ts
|
|
8749
|
+
var HOOK_RUN_TARGETS = TARGET_NAMES;
|
|
8750
|
+
function isHookRunTarget(value) {
|
|
8751
|
+
return typeof value === "string" && HOOK_RUN_TARGETS.includes(value);
|
|
8752
|
+
}
|
|
8753
|
+
function nativeTargetArgs(dispatch, target, args = []) {
|
|
8754
|
+
if (target !== undefined && !isHookRunTarget(target))
|
|
8755
|
+
throw new Error(`Invalid --target; expected one of ${HOOK_RUN_TARGETS.join(", ")}`);
|
|
8756
|
+
if (!dispatch)
|
|
8757
|
+
return [...args];
|
|
8758
|
+
if (!isHookRunTarget(target))
|
|
8759
|
+
throw new Error("This hook requires --target from its native registration");
|
|
8760
|
+
if (args.some((arg) => arg === "--harness" || arg.startsWith("--harness=")))
|
|
8761
|
+
throw new Error("nativeTargetDispatch owns the --harness argument; remove it from manifest args");
|
|
8762
|
+
return [...args, "--harness", target];
|
|
8763
|
+
}
|
|
8764
|
+
function nativeTargetCommandSuffix(dispatch, target) {
|
|
8765
|
+
if (!dispatch)
|
|
8766
|
+
return "";
|
|
8767
|
+
if (!isHookRunTarget(target))
|
|
8768
|
+
throw new Error("Native registration requires a supported target");
|
|
8769
|
+
return ` --target ${target}`;
|
|
8770
|
+
}
|
|
8771
|
+
|
|
8533
8772
|
// src/lib/registration.ts
|
|
8534
|
-
var REGISTRATION_COMMAND_RE =
|
|
8773
|
+
var REGISTRATION_COMMAND_RE = new RegExp(`^(?:hooks run ([\\w-]+)(?:\\s+--profile\\s+[\\w-]+)?(?:\\s+--target\\s+(?:${HOOK_RUN_TARGETS.join("|")}))?|hook-([\\w-]+))$`);
|
|
8535
8774
|
function registrationName(command) {
|
|
8536
8775
|
if (typeof command !== "string")
|
|
8537
8776
|
return;
|
|
@@ -8560,7 +8799,7 @@ init_store();
|
|
|
8560
8799
|
|
|
8561
8800
|
// src/lib/codex-settings.ts
|
|
8562
8801
|
import { closeSync as closeSync3, existsSync as existsSync9, lstatSync as lstatSync4, mkdirSync as mkdirSync4, openSync as openSync3, readFileSync as readFileSync6, renameSync as renameSync2, unlinkSync, writeFileSync as writeFileSync3 } from "fs";
|
|
8563
|
-
import { dirname as
|
|
8802
|
+
import { dirname as dirname6 } from "path";
|
|
8564
8803
|
import { createHash as createHash5, randomUUID } from "crypto";
|
|
8565
8804
|
var snapshots = new WeakMap;
|
|
8566
8805
|
function current(path) {
|
|
@@ -8619,7 +8858,7 @@ function writeCodexSettings(path, settings, expectedSha256, beforeWrite) {
|
|
|
8619
8858
|
` });
|
|
8620
8859
|
}
|
|
8621
8860
|
function writeHookSettingsPreimage(path, settings, before, beforeWrite) {
|
|
8622
|
-
mkdirSync4(
|
|
8861
|
+
mkdirSync4(dirname6(path), { recursive: true, mode: 448 });
|
|
8623
8862
|
const lock = `${path}.hooks-write.lock`;
|
|
8624
8863
|
let fd;
|
|
8625
8864
|
try {
|
|
@@ -8649,13 +8888,13 @@ function writeHookSettingsPreimage(path, settings, before, beforeWrite) {
|
|
|
8649
8888
|
// src/lib/claude-skills-coordination.ts
|
|
8650
8889
|
import { createHash as createHash8 } from "crypto";
|
|
8651
8890
|
import { existsSync as existsSync11, lstatSync as lstatSync9 } from "fs";
|
|
8652
|
-
import { homedir as
|
|
8653
|
-
import { join as
|
|
8891
|
+
import { homedir as homedir7 } from "os";
|
|
8892
|
+
import { join as join17, resolve as resolve9 } from "path";
|
|
8654
8893
|
|
|
8655
8894
|
// ../../node_modules/.bun/@hasna+skills@0.10.78+a00d3064559565bb/node_modules/@hasna/skills/dist/index.js
|
|
8656
8895
|
import { existsSync as existsSync10 } from "fs";
|
|
8657
|
-
import { homedir as
|
|
8658
|
-
import { join as
|
|
8896
|
+
import { homedir as homedir6 } from "os";
|
|
8897
|
+
import { join as join11, resolve as resolve4 } from "path";
|
|
8659
8898
|
import { homedir as pathsResolverHomedir2 } from "os";
|
|
8660
8899
|
import { join as pathsResolverJoin2 } from "path";
|
|
8661
8900
|
import { createHash as createHash6 } from "crypto";
|
|
@@ -8670,12 +8909,12 @@ import { createHash as createHash42 } from "crypto";
|
|
|
8670
8909
|
import { basename as basename3, dirname as dirname42, isAbsolute as isAbsolute32, resolve as resolve32 } from "path";
|
|
8671
8910
|
import { createHash as createHash52 } from "crypto";
|
|
8672
8911
|
import { closeSync as closeSync32, constants as constants3, fstatSync as fstatSync3, lstatSync as lstatSync5, openSync as openSync32, readSync as readSync2 } from "fs";
|
|
8673
|
-
import { basename as basename4, dirname as dirname52, isAbsolute as isAbsolute4, resolve as
|
|
8912
|
+
import { basename as basename4, dirname as dirname52, isAbsolute as isAbsolute4, resolve as resolve42, sep as sep22 } from "path";
|
|
8674
8913
|
import { createHash as createHash30 } from "crypto";
|
|
8675
8914
|
import { lstatSync as lstatSync32, readFileSync as readFileSync26, readdirSync as readdirSync13, realpathSync as realpathSync13 } from "fs";
|
|
8676
8915
|
import { basename as basename15, dirname as dirname26, isAbsolute as isAbsolute28, join as join38, relative as relative10, resolve as resolve27, sep as sep15 } from "path";
|
|
8677
8916
|
import { isDeepStrictEqual as isDeepStrictEqual9 } from "util";
|
|
8678
|
-
import { dirname as
|
|
8917
|
+
import { dirname as dirname62, isAbsolute as isAbsolute6, join as join82, resolve as resolve6 } from "path";
|
|
8679
8918
|
import { lstatSync as lstatSync6, readFileSync as readFileSync62, opendirSync } from "fs";
|
|
8680
8919
|
import { createHash as createHash62 } from "crypto";
|
|
8681
8920
|
import { lstatSync as lstatSync7, opendirSync as opendirSync2 } from "fs";
|
|
@@ -15604,10 +15843,10 @@ var SKILLS_HOME_ENV = "SKILLS_HOME";
|
|
|
15604
15843
|
var DEFAULT_SQLITE_FILENAME = "server.db";
|
|
15605
15844
|
var GLOBAL_CONFIG_FILENAME = "config.json";
|
|
15606
15845
|
function effectiveHome() {
|
|
15607
|
-
return process.env["HOME"] || process.env["USERPROFILE"] ||
|
|
15846
|
+
return process.env["HOME"] || process.env["USERPROFILE"] || homedir6() || "/tmp";
|
|
15608
15847
|
}
|
|
15609
15848
|
function legacyDataRoot() {
|
|
15610
|
-
return
|
|
15849
|
+
return join11(effectiveHome(), ".hasna", "skills");
|
|
15611
15850
|
}
|
|
15612
15851
|
function resolverDataRoot(home = effectiveHome(), env) {
|
|
15613
15852
|
return dataDir2({ app: "skills", home, env });
|
|
@@ -15616,13 +15855,13 @@ function adoptResolverDataRoot2(resolved, env = process.env) {
|
|
|
15616
15855
|
const dataOverride = env.HASNA_DATA_HOME;
|
|
15617
15856
|
if (typeof dataOverride === "string" && dataOverride.trim().length > 0)
|
|
15618
15857
|
return true;
|
|
15619
|
-
return existsSync10(
|
|
15858
|
+
return existsSync10(join11(resolved, DEFAULT_SQLITE_FILENAME)) || existsSync10(join11(resolved, GLOBAL_CONFIG_FILENAME));
|
|
15620
15859
|
}
|
|
15621
15860
|
function exactDataRoot() {
|
|
15622
15861
|
for (const key of [DATA_DIR_ENV, HASNA_SKILLS_HOME_ENV, SKILLS_HOME_ENV]) {
|
|
15623
15862
|
const dir = process.env[key]?.trim();
|
|
15624
15863
|
if (dir)
|
|
15625
|
-
return
|
|
15864
|
+
return resolve4(dir);
|
|
15626
15865
|
}
|
|
15627
15866
|
return;
|
|
15628
15867
|
}
|
|
@@ -15631,7 +15870,7 @@ function getDataRoot() {
|
|
|
15631
15870
|
if (exact)
|
|
15632
15871
|
return exact;
|
|
15633
15872
|
const resolved = resolverDataRoot();
|
|
15634
|
-
return adoptResolverDataRoot2(resolved) ?
|
|
15873
|
+
return adoptResolverDataRoot2(resolved) ? resolve4(resolved) : resolve4(legacyDataRoot());
|
|
15635
15874
|
}
|
|
15636
15875
|
function getDataDirReadOnly() {
|
|
15637
15876
|
return getDataRoot();
|
|
@@ -16727,7 +16966,7 @@ function captureClaudeMarketplaceEntry(path, marketplace, plugin, budget2 = { re
|
|
|
16727
16966
|
var CLAUDE_ORPHAN_MARKER_MODE = "claude-plugin-orphaned-at-v1";
|
|
16728
16967
|
var CACHE = `${sep22}.claude${sep22}plugins${sep22}cache${sep22}`;
|
|
16729
16968
|
function isClaudePluginCacheOrphanMarker(path) {
|
|
16730
|
-
if (typeof path !== "string" || basename4(path) !== ".orphaned_at" || !isAbsolute4(path) ||
|
|
16969
|
+
if (typeof path !== "string" || basename4(path) !== ".orphaned_at" || !isAbsolute4(path) || resolve42(path) !== path || path.includes("\x00"))
|
|
16731
16970
|
return false;
|
|
16732
16971
|
const at = path.indexOf(CACHE);
|
|
16733
16972
|
if (at < 0 || path.indexOf(CACHE, at + 1) >= 0)
|
|
@@ -17181,10 +17420,10 @@ function assertHermesEnvironment(home) {
|
|
|
17181
17420
|
throw new Error("NATIVE_SKILL_DRIFT: Hermes project plugins require a dedicated discovery review");
|
|
17182
17421
|
}
|
|
17183
17422
|
function read(path) {
|
|
17184
|
-
for (let current2 = path;; current2 =
|
|
17423
|
+
for (let current2 = path;; current2 = dirname62(current2)) {
|
|
17185
17424
|
if (lstatSync6(current2, { throwIfNoEntry: false })?.isSymbolicLink())
|
|
17186
17425
|
throw new Error("NATIVE_SKILL_DRIFT: symlink Hermes protection input");
|
|
17187
|
-
if (
|
|
17426
|
+
if (dirname62(current2) === current2)
|
|
17188
17427
|
break;
|
|
17189
17428
|
}
|
|
17190
17429
|
const stat = lstatSync6(path, { throwIfNoEntry: false });
|
|
@@ -17224,10 +17463,10 @@ function assertHermesProtection(home, config, command, profile, supervisor2) {
|
|
|
17224
17463
|
}
|
|
17225
17464
|
function assertNoHermesLegacyShadow(root) {
|
|
17226
17465
|
let entries = 0;
|
|
17227
|
-
for (let cursor = resolve6(root);; cursor =
|
|
17466
|
+
for (let cursor = resolve6(root);; cursor = dirname62(cursor)) {
|
|
17228
17467
|
if (lstatSync6(cursor, { throwIfNoEntry: false })?.isSymbolicLink())
|
|
17229
17468
|
throw new Error("NATIVE_SKILL_DRIFT: symlink Hermes legacy discovery root");
|
|
17230
|
-
if (
|
|
17469
|
+
if (dirname62(cursor) === cursor)
|
|
17231
17470
|
break;
|
|
17232
17471
|
}
|
|
17233
17472
|
function scan(path, depth) {
|
|
@@ -46554,7 +46793,7 @@ function claudeSkillsUsage(before, home) {
|
|
|
46554
46793
|
}
|
|
46555
46794
|
if (events3.length)
|
|
46556
46795
|
evidence.push(`Skills-managed hook entries in ${events3.join(", ")}`);
|
|
46557
|
-
const bridge =
|
|
46796
|
+
const bridge = join17(home, ".claude", "skills", "skills-cli", "SKILL.md");
|
|
46558
46797
|
let bridgePresent;
|
|
46559
46798
|
try {
|
|
46560
46799
|
bridgePresent = lstatSync9(bridge, { throwIfNoEntry: false }) !== undefined;
|
|
@@ -46568,8 +46807,8 @@ function claudeSkillsUsage(before, home) {
|
|
|
46568
46807
|
function skillsPolicyLocations(dataDir3) {
|
|
46569
46808
|
const state = (path) => existsSync11(path) ? "present" : "absent";
|
|
46570
46809
|
if (dataDir3 !== undefined)
|
|
46571
|
-
return [`${
|
|
46572
|
-
const env = process.env, home = env.HOME || env.USERPROFILE ||
|
|
46810
|
+
return [`${join17(resolve9(dataDir3), "agent-policy.json")} (explicit data directory, absent)`];
|
|
46811
|
+
const env = process.env, home = env.HOME || env.USERPROFILE || homedir7() || "/tmp", searched = [];
|
|
46573
46812
|
let selected = false;
|
|
46574
46813
|
for (const key2 of ["HASNA_SKILLS_DIR", "HASNA_SKILLS_HOME", "SKILLS_HOME"]) {
|
|
46575
46814
|
const value = env[key2]?.trim();
|
|
@@ -46579,13 +46818,13 @@ function skillsPolicyLocations(dataDir3) {
|
|
|
46579
46818
|
searched.push(`$${key2} (unset)`);
|
|
46580
46819
|
else {
|
|
46581
46820
|
selected = true;
|
|
46582
|
-
searched.push(`$${key2} -> ${
|
|
46821
|
+
searched.push(`$${key2} -> ${join17(resolve9(value), "agent-policy.json")} (selected, absent)`);
|
|
46583
46822
|
}
|
|
46584
46823
|
}
|
|
46585
46824
|
const override = env.HASNA_DATA_HOME;
|
|
46586
|
-
const base = typeof override === "string" && override.length > 0 ? override : process.platform === "darwin" ?
|
|
46587
|
-
const xdg =
|
|
46588
|
-
const adopted = typeof override === "string" && override.trim().length > 0 || existsSync11(
|
|
46825
|
+
const base = typeof override === "string" && override.length > 0 ? override : process.platform === "darwin" ? join17(home, "Library", "Application Support", "Hasna") : join17(home, ".local", "share", "hasna");
|
|
46826
|
+
const xdg = join17(base, "skills"), xdgPolicy = join17(xdg, "agent-policy.json");
|
|
46827
|
+
const adopted = typeof override === "string" && override.trim().length > 0 || existsSync11(join17(xdg, "server.db")) || existsSync11(join17(xdg, "config.json"));
|
|
46589
46828
|
if (selected)
|
|
46590
46829
|
searched.push(`${xdgPolicy} (not consulted, ${state(xdgPolicy)})`);
|
|
46591
46830
|
else if (adopted) {
|
|
@@ -46593,7 +46832,7 @@ function skillsPolicyLocations(dataDir3) {
|
|
|
46593
46832
|
searched.push(`${xdgPolicy} (selected, absent)`);
|
|
46594
46833
|
} else
|
|
46595
46834
|
searched.push(`${xdgPolicy} (not selected: the directory holds no server.db or config.json and HASNA_DATA_HOME is unset; ${state(xdgPolicy)})`);
|
|
46596
|
-
const legacy =
|
|
46835
|
+
const legacy = join17(home, ".hasna", "skills", "agent-policy.json");
|
|
46597
46836
|
searched.push(selected ? `${legacy} (not consulted, ${state(legacy)})` : `${legacy} (selected, absent)`);
|
|
46598
46837
|
return searched;
|
|
46599
46838
|
}
|
|
@@ -46611,7 +46850,7 @@ function planCoordinatedClaudeSettings(before, replacement, options) {
|
|
|
46611
46850
|
if (!plan) {
|
|
46612
46851
|
const usage = claudeSkillsUsage(before, options.home);
|
|
46613
46852
|
if (usage.length)
|
|
46614
|
-
throw new ClaudeSettingsRefusal("skills_policy_unresolved", `Skills manages ${
|
|
46853
|
+
throw new ClaudeSettingsRefusal("skills_policy_unresolved", `Skills manages ${join17(options.home, ".claude", "settings.json")} (${usage.join("; ")}), but no Skills agent policy resolved, ` + "so Hooks cannot coordinate this change and wrote nothing. Locations searched, in Skills resolution order: " + skillsPolicyLocations(options.dataDir).map((location, index) => `${index + 1}) ${location}`).join("; ") + ". Run hooks with the environment that selects the Skills data directory (for example HASNA_SKILLS_DIR), " + "or repair the integration with `skills hook install`.");
|
|
46615
46854
|
}
|
|
46616
46855
|
return plan;
|
|
46617
46856
|
}
|
|
@@ -46624,33 +46863,689 @@ function coordinateClaudeSettings(before, replacement, options, beforeWrite) {
|
|
|
46624
46863
|
return true;
|
|
46625
46864
|
}
|
|
46626
46865
|
|
|
46627
|
-
// src/lib/
|
|
46628
|
-
|
|
46629
|
-
|
|
46630
|
-
|
|
46866
|
+
// src/lib/target-adapters.ts
|
|
46867
|
+
init_profiles();
|
|
46868
|
+
import { spawnSync } from "child_process";
|
|
46869
|
+
import { chmodSync as chmodSync2, existsSync as existsSync13, mkdirSync as mkdirSync10, readFileSync as readFileSync8, readdirSync as readdirSync8, rmdirSync as rmdirSync2, statSync, unlinkSync as unlinkSync3, writeFileSync as writeFileSync9, renameSync as renameSync5 } from "fs";
|
|
46870
|
+
import { tmpdir } from "os";
|
|
46871
|
+
import { basename as basename9, dirname as dirname9, join as join22 } from "path";
|
|
46872
|
+
import { randomUUID as randomUUID5 } from "crypto";
|
|
46873
|
+
var ADAPTER_TARGETS = ["muse", "kimi", "pi", "cursor"];
|
|
46874
|
+
function isAdapterTarget(value) {
|
|
46875
|
+
return typeof value === "string" && ADAPTER_TARGETS.includes(value);
|
|
46876
|
+
}
|
|
46877
|
+
var defaultRunner = (command, args) => {
|
|
46878
|
+
const result = spawnSync(command, args, { encoding: "utf8", maxBuffer: 4 * 1024 * 1024 });
|
|
46879
|
+
return {
|
|
46880
|
+
status: result.status ?? 1,
|
|
46881
|
+
stdout: result.stdout ?? "",
|
|
46882
|
+
stderr: result.stderr ?? "",
|
|
46883
|
+
notFound: result.error?.code === "ENOENT"
|
|
46884
|
+
};
|
|
46885
|
+
};
|
|
46886
|
+
var HOOK_COMMAND_RE = /^hooks run ([\w-]+)(?:\s|$)/;
|
|
46887
|
+
function hookFromCommand(command) {
|
|
46888
|
+
return command.trim().match(HOOK_COMMAND_RE)?.[1];
|
|
46631
46889
|
}
|
|
46632
|
-
function
|
|
46633
|
-
|
|
46634
|
-
throw new Error("Invalid --target; expected claude, codex, gemini, codewith, sumi, or opencode");
|
|
46635
|
-
if (!dispatch)
|
|
46636
|
-
return [...args];
|
|
46637
|
-
if (!isHookRunTarget(target))
|
|
46638
|
-
throw new Error("This hook requires --target from its native registration");
|
|
46639
|
-
if (args.some((arg) => arg === "--harness" || arg.startsWith("--harness=")))
|
|
46640
|
-
throw new Error("nativeTargetDispatch owns the --harness argument; remove it from manifest args");
|
|
46641
|
-
return [...args, "--harness", target];
|
|
46890
|
+
function homeOf(options) {
|
|
46891
|
+
return options.home ?? process.env.HOME ?? process.env.USERPROFILE ?? "";
|
|
46642
46892
|
}
|
|
46643
|
-
function
|
|
46644
|
-
|
|
46645
|
-
|
|
46646
|
-
|
|
46647
|
-
|
|
46648
|
-
|
|
46893
|
+
function adapterStorePath(target, options, hook) {
|
|
46894
|
+
return adapterPath(target, options, hook);
|
|
46895
|
+
}
|
|
46896
|
+
function adapterPath(target, options, hook) {
|
|
46897
|
+
const capabilities = getTargetCapabilities(target, {
|
|
46898
|
+
scope: options.scope,
|
|
46899
|
+
home: homeOf(options),
|
|
46900
|
+
cwd: options.cwd ?? process.cwd(),
|
|
46901
|
+
env: options.env ?? process.env,
|
|
46902
|
+
...hook ? { hook } : {}
|
|
46903
|
+
});
|
|
46904
|
+
if (!capabilities)
|
|
46905
|
+
throw new Error(`No capability record for target '${target}'`);
|
|
46906
|
+
return capabilities.configPath;
|
|
46907
|
+
}
|
|
46908
|
+
function redactedDiagnostic(text3) {
|
|
46909
|
+
const line = (text3.split(`
|
|
46910
|
+
`).find((entry) => entry.trim()) ?? "").trim();
|
|
46911
|
+
return line.slice(0, 300).replace(/[A-Za-z0-9_\-]{24,}/g, "<redacted>");
|
|
46912
|
+
}
|
|
46913
|
+
function redactedCode(value) {
|
|
46914
|
+
return typeof value === "string" && value.length <= 64 && /^[a-z][a-z0-9]*(?:-[a-z0-9]+)+$/.test(value) ? value : "<redacted>";
|
|
46915
|
+
}
|
|
46916
|
+
function messageOf(error61) {
|
|
46917
|
+
return error61 instanceof Error ? error61.message : String(error61);
|
|
46918
|
+
}
|
|
46919
|
+
function readJsonObject(path) {
|
|
46920
|
+
let text3;
|
|
46921
|
+
try {
|
|
46922
|
+
text3 = readFileSync8(path, "utf-8");
|
|
46923
|
+
} catch (error61) {
|
|
46924
|
+
if (error61?.code === "ENOENT")
|
|
46925
|
+
return { status: "absent" };
|
|
46926
|
+
return { status: "unreadable", kind: "file", reason: `cannot read ${path} (${redactedDiagnostic(messageOf(error61))})` };
|
|
46927
|
+
}
|
|
46928
|
+
try {
|
|
46929
|
+
const value = JSON.parse(text3);
|
|
46930
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
46931
|
+
return { status: "unreadable", kind: "content", reason: `${path} is not a JSON object` };
|
|
46932
|
+
}
|
|
46933
|
+
return { status: "verified", value };
|
|
46934
|
+
} catch (error61) {
|
|
46935
|
+
return { status: "unreadable", kind: "content", reason: `${path} is not valid JSON (${redactedDiagnostic(messageOf(error61))})` };
|
|
46936
|
+
}
|
|
46937
|
+
}
|
|
46938
|
+
function writeText(path, content) {
|
|
46939
|
+
const dir = dirname9(path);
|
|
46940
|
+
mkdirSync10(dir, { recursive: true });
|
|
46941
|
+
const temp = join22(dir, `.${basename9(path)}.${randomUUID5()}.tmp`);
|
|
46942
|
+
try {
|
|
46943
|
+
writeFileSync9(temp, content);
|
|
46944
|
+
if (existsSync13(path))
|
|
46945
|
+
chmodSync2(temp, statSync(path).mode & 4095);
|
|
46946
|
+
renameSync5(temp, path);
|
|
46947
|
+
} catch (error61) {
|
|
46948
|
+
try {
|
|
46949
|
+
if (existsSync13(temp))
|
|
46950
|
+
unlinkSync3(temp);
|
|
46951
|
+
} catch {}
|
|
46952
|
+
throw error61;
|
|
46953
|
+
}
|
|
46954
|
+
}
|
|
46955
|
+
function tomlString(value) {
|
|
46956
|
+
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
46957
|
+
}
|
|
46958
|
+
function tomlStringValue(raw) {
|
|
46959
|
+
return raw.replace(/\\"/g, '"').replace(/\\\\/g, "\\");
|
|
46960
|
+
}
|
|
46961
|
+
var TOML_HEADER_RE = /^\s*\[\[?[^\]]/;
|
|
46962
|
+
var KIMI_HOOK_HEADER_RE = /^\s*\[\[hooks\]\]\s*$/;
|
|
46963
|
+
function splitToml(text3) {
|
|
46964
|
+
const preamble = [];
|
|
46965
|
+
const chunks = [];
|
|
46966
|
+
let current2 = null;
|
|
46967
|
+
for (const line of text3.split(`
|
|
46968
|
+
`)) {
|
|
46969
|
+
if (TOML_HEADER_RE.test(line)) {
|
|
46970
|
+
if (current2)
|
|
46971
|
+
chunks.push(current2);
|
|
46972
|
+
current2 = { header: line.trim(), lines: [line] };
|
|
46973
|
+
} else if (current2) {
|
|
46974
|
+
current2.lines.push(line);
|
|
46975
|
+
} else {
|
|
46976
|
+
preamble.push(line);
|
|
46977
|
+
}
|
|
46978
|
+
}
|
|
46979
|
+
if (current2)
|
|
46980
|
+
chunks.push(current2);
|
|
46981
|
+
return { preamble, chunks };
|
|
46982
|
+
}
|
|
46983
|
+
function chunkCommand(chunk) {
|
|
46984
|
+
const text3 = chunk.lines.join(`
|
|
46985
|
+
`);
|
|
46986
|
+
const basic = text3.match(/^\s*command\s*=\s*"((?:[^"\\]|\\.)*)"/m);
|
|
46987
|
+
if (basic)
|
|
46988
|
+
return tomlStringValue(basic[1]);
|
|
46989
|
+
const literal2 = text3.match(/^\s*command\s*=\s*'([^']*)'/m);
|
|
46990
|
+
return literal2 ? literal2[1] : undefined;
|
|
46991
|
+
}
|
|
46992
|
+
function isKimiHookChunk(chunk) {
|
|
46993
|
+
return KIMI_HOOK_HEADER_RE.test(chunk.header);
|
|
46994
|
+
}
|
|
46995
|
+
function kimiRegistrations(text3) {
|
|
46996
|
+
const out = [];
|
|
46997
|
+
for (const chunk of splitToml(text3).chunks) {
|
|
46998
|
+
if (!isKimiHookChunk(chunk))
|
|
46999
|
+
continue;
|
|
47000
|
+
const command = chunkCommand(chunk);
|
|
47001
|
+
const hook = command ? hookFromCommand(command) : undefined;
|
|
47002
|
+
if (command && hook)
|
|
47003
|
+
out.push({ hook, command });
|
|
47004
|
+
}
|
|
47005
|
+
return out;
|
|
47006
|
+
}
|
|
47007
|
+
function kimiBlock(command, timeoutSeconds) {
|
|
47008
|
+
return [
|
|
47009
|
+
"[[hooks]]",
|
|
47010
|
+
'event = "Stop"',
|
|
47011
|
+
`command = ${tomlString(command)}`,
|
|
47012
|
+
`timeout = ${Math.max(1, Math.min(600, timeoutSeconds))}`
|
|
47013
|
+
].join(`
|
|
47014
|
+
`);
|
|
47015
|
+
}
|
|
47016
|
+
function removeKimiChunk(text3, hook) {
|
|
47017
|
+
const { preamble, chunks } = splitToml(text3);
|
|
47018
|
+
const kept = [...preamble];
|
|
47019
|
+
let removed = false;
|
|
47020
|
+
for (const chunk of chunks) {
|
|
47021
|
+
const command = isKimiHookChunk(chunk) ? chunkCommand(chunk) : undefined;
|
|
47022
|
+
if (command && hookFromCommand(command) === hook) {
|
|
47023
|
+
removed = true;
|
|
47024
|
+
while (kept.length > 0 && kept[kept.length - 1].trim() === "")
|
|
47025
|
+
kept.pop();
|
|
47026
|
+
continue;
|
|
47027
|
+
}
|
|
47028
|
+
if (TOML_HEADER_RE.test(chunk.header) && kept.length > 0 && kept[kept.length - 1].trim() !== "") {
|
|
47029
|
+
kept.push("");
|
|
47030
|
+
}
|
|
47031
|
+
kept.push(...chunk.lines);
|
|
47032
|
+
}
|
|
47033
|
+
return { text: kept.join(`
|
|
47034
|
+
`), removed };
|
|
47035
|
+
}
|
|
47036
|
+
function installKimi(hook, command, options, timeoutSeconds) {
|
|
47037
|
+
const path = adapterPath("kimi", options);
|
|
47038
|
+
const read3 = readTextState(path);
|
|
47039
|
+
if (read3.status === "unreadable") {
|
|
47040
|
+
return { ok: false, configPath: path, error: `Refused: ${read3.reason}, so Hooks wrote nothing. Repair the file or move it aside, then retry.` };
|
|
47041
|
+
}
|
|
47042
|
+
const existing = read3.status === "verified" ? read3.text : "";
|
|
47043
|
+
if (kimiRegistrations(existing).some((entry) => entry.hook === hook)) {
|
|
47044
|
+
return { ok: true, configPath: path, note: "already registered; nothing was written" };
|
|
47045
|
+
}
|
|
47046
|
+
const next = existing.trim() === "" ? `${kimiBlock(command, timeoutSeconds)}
|
|
47047
|
+
` : `${existing.trimEnd()}
|
|
47048
|
+
|
|
47049
|
+
${kimiBlock(command, timeoutSeconds)}
|
|
47050
|
+
`;
|
|
47051
|
+
const candidate = join22(tmpdir(), `hooks-kimi-config-${randomUUID5()}.toml`);
|
|
47052
|
+
writeFileSync9(candidate, next);
|
|
47053
|
+
let validationNote;
|
|
47054
|
+
try {
|
|
47055
|
+
const result = (options.run ?? defaultRunner)("kimi", ["doctor", "config", candidate]);
|
|
47056
|
+
if (result.notFound) {
|
|
47057
|
+
validationNote = "kimi is not installed here, so `kimi doctor config` did not validate the entry";
|
|
47058
|
+
} else if (result.status !== 0) {
|
|
47059
|
+
const detail = redactedDiagnostic(result.stderr) || redactedDiagnostic(result.stdout) || `exit ${result.status}`;
|
|
47060
|
+
return { ok: false, configPath: path, error: `Refused: \`kimi doctor config\` rejected the candidate config (${detail}). Nothing was written to ${path}.` };
|
|
47061
|
+
} else {
|
|
47062
|
+
validationNote = "validated with `kimi doctor config`";
|
|
47063
|
+
}
|
|
47064
|
+
} finally {
|
|
47065
|
+
try {
|
|
47066
|
+
if (existsSync13(candidate))
|
|
47067
|
+
unlinkSync3(candidate);
|
|
47068
|
+
} catch {}
|
|
47069
|
+
}
|
|
47070
|
+
writeText(path, next);
|
|
47071
|
+
return {
|
|
47072
|
+
ok: true,
|
|
47073
|
+
configPath: path,
|
|
47074
|
+
note: `Kimi blocks with exit 2 + stderr once per turn; ${validationNote}.`
|
|
47075
|
+
};
|
|
47076
|
+
}
|
|
47077
|
+
var PI_PREFERENCE_KEY = "hasna-pi-hooks";
|
|
47078
|
+
function piPointerPath(options) {
|
|
47079
|
+
return adapterPath("pi", options);
|
|
47080
|
+
}
|
|
47081
|
+
function readPiPointer(options) {
|
|
47082
|
+
return readJsonObject(piPointerPath(options));
|
|
47083
|
+
}
|
|
47084
|
+
function piProfile(options, pointer) {
|
|
47085
|
+
const state = pointer === undefined ? readPiPointer(options) : undefined;
|
|
47086
|
+
const config2 = pointer ?? (state?.status === "verified" ? state.value : undefined);
|
|
47087
|
+
if (typeof config2?.profile === "string") {
|
|
47088
|
+
const pointed = getProfile(config2.profile);
|
|
47089
|
+
if (pointed)
|
|
47090
|
+
return pointed;
|
|
47091
|
+
}
|
|
47092
|
+
return listProfiles().find((profile) => profile.agent_type === "custom" && profile.name === "pi");
|
|
47093
|
+
}
|
|
47094
|
+
function piCommands(profile) {
|
|
47095
|
+
const stored = profile?.preferences?.[PI_PREFERENCE_KEY];
|
|
47096
|
+
const hooks = stored?.hooks;
|
|
47097
|
+
return Array.isArray(hooks) ? hooks.filter((entry) => typeof entry === "string") : [];
|
|
47098
|
+
}
|
|
47099
|
+
function writePiCommands(profileId, commands) {
|
|
47100
|
+
const profile = getProfile(profileId);
|
|
47101
|
+
if (!profile)
|
|
47102
|
+
return;
|
|
47103
|
+
updateProfile(profileId, {
|
|
47104
|
+
preferences: {
|
|
47105
|
+
...profile.preferences,
|
|
47106
|
+
[PI_PREFERENCE_KEY]: { hooks: commands, updated_at: new Date().toISOString() }
|
|
47107
|
+
}
|
|
47108
|
+
});
|
|
47109
|
+
}
|
|
47110
|
+
function installPi(hook, command, options) {
|
|
47111
|
+
const path = piPointerPath(options);
|
|
47112
|
+
const pointerRead = readPiPointer(options);
|
|
47113
|
+
if (pointerRead.status === "unreadable") {
|
|
47114
|
+
return { ok: false, configPath: path, error: `Refused: ${pointerRead.reason}, so Hooks wrote nothing. Repair the file or move it aside, then retry.` };
|
|
47115
|
+
}
|
|
47116
|
+
const profile = piProfile(options) ?? createProfile({ agent_type: "custom", name: "pi" });
|
|
47117
|
+
const commands = piCommands(profile);
|
|
47118
|
+
if (commands.some((entry) => hookFromCommand(entry) === hook)) {
|
|
47119
|
+
return { ok: true, configPath: path, note: "already registered; nothing was written" };
|
|
47120
|
+
}
|
|
47121
|
+
const pointer = pointerRead.status === "verified" ? pointerRead.value : {};
|
|
47122
|
+
writeText(path, `${JSON.stringify({ ...pointer, profile: profile.agent_id }, null, 2)}
|
|
47123
|
+
`);
|
|
47124
|
+
writePiCommands(profile.agent_id, [...commands, command]);
|
|
47125
|
+
return {
|
|
47126
|
+
ok: true,
|
|
47127
|
+
configPath: path,
|
|
47128
|
+
note: `registered through Hooks profile ${profile.agent_id}, which the hasna-pi-hooks bridge reads`
|
|
47129
|
+
};
|
|
47130
|
+
}
|
|
47131
|
+
function readCursorFile(path) {
|
|
47132
|
+
return readJsonObject(path);
|
|
47133
|
+
}
|
|
47134
|
+
function cursorRegistrations(config2) {
|
|
47135
|
+
const out = [];
|
|
47136
|
+
const hooks = config2?.hooks;
|
|
47137
|
+
if (!hooks || typeof hooks !== "object")
|
|
47138
|
+
return out;
|
|
47139
|
+
for (const key2 of Object.keys(hooks)) {
|
|
47140
|
+
const entries = hooks[key2];
|
|
47141
|
+
if (!Array.isArray(entries))
|
|
47142
|
+
continue;
|
|
47143
|
+
for (const entry of entries) {
|
|
47144
|
+
const command = typeof entry?.command === "string" ? entry.command : undefined;
|
|
47145
|
+
const hook = command ? hookFromCommand(command) : undefined;
|
|
47146
|
+
if (command && hook)
|
|
47147
|
+
out.push({ hook, command });
|
|
47148
|
+
}
|
|
47149
|
+
}
|
|
47150
|
+
return out;
|
|
47151
|
+
}
|
|
47152
|
+
function installCursor(hook, command, options, timeoutSeconds) {
|
|
47153
|
+
const path = adapterPath("cursor", options);
|
|
47154
|
+
const read3 = readCursorFile(path);
|
|
47155
|
+
if (read3.status === "unreadable") {
|
|
47156
|
+
return {
|
|
47157
|
+
ok: false,
|
|
47158
|
+
configPath: path,
|
|
47159
|
+
error: read3.kind === "file" ? `Refused: ${read3.reason}, so Hooks wrote nothing. Repair the file or move it aside, then retry.` : `Refused: ${path} is not a JSON object, so Hooks wrote nothing. Repair the file or move it aside, then retry.`
|
|
47160
|
+
};
|
|
47161
|
+
}
|
|
47162
|
+
const config2 = read3.status === "verified" ? read3.value : undefined;
|
|
47163
|
+
if (cursorRegistrations(config2).some((entry) => entry.hook === hook)) {
|
|
47164
|
+
return { ok: true, configPath: path, note: "already registered; nothing was written" };
|
|
47165
|
+
}
|
|
47166
|
+
const next = config2 ?? {};
|
|
47167
|
+
const hooks = next.hooks && typeof next.hooks === "object" ? { ...next.hooks } : {};
|
|
47168
|
+
const stop = Array.isArray(hooks.stop) ? [...hooks.stop] : [];
|
|
47169
|
+
stop.push({ command, timeout: Math.max(1, Math.min(600, timeoutSeconds)) });
|
|
47170
|
+
hooks.stop = stop;
|
|
47171
|
+
writeText(path, `${JSON.stringify({ version: 1, ...next, hooks }, null, 2)}
|
|
47172
|
+
`);
|
|
47173
|
+
return { ok: true, configPath: path };
|
|
47174
|
+
}
|
|
47175
|
+
function splitArgv(command) {
|
|
47176
|
+
return command.trim().split(/\s+/).filter(Boolean);
|
|
47177
|
+
}
|
|
47178
|
+
function hookFromMuseCommand(command) {
|
|
47179
|
+
if (!Array.isArray(command))
|
|
47180
|
+
return;
|
|
47181
|
+
const joined = command.filter((part) => typeof part === "string").join(" ");
|
|
47182
|
+
const match = joined.match(/(?:^|\s)\S*hooks run ([\w-]+)(?:\s|$)/);
|
|
47183
|
+
return match?.[1];
|
|
47184
|
+
}
|
|
47185
|
+
function museManifest(hook, command, options, timeoutSeconds) {
|
|
47186
|
+
return {
|
|
47187
|
+
name: `hasna-hook-${hook}`,
|
|
47188
|
+
version: options.version ?? "0.0.0",
|
|
47189
|
+
description: options.description ?? `Hasna hook ${hook}`,
|
|
47190
|
+
schemaVersion: 1,
|
|
47191
|
+
compat: { source: "native", manifestDir: ".muse-plugin" },
|
|
47192
|
+
capabilities: {
|
|
47193
|
+
hooks: [{
|
|
47194
|
+
id: `hasna-${hook}-stop`,
|
|
47195
|
+
event: "Stop",
|
|
47196
|
+
command: splitArgv(command),
|
|
47197
|
+
timeoutMs: timeoutSeconds * 1000,
|
|
47198
|
+
statusMessage: `Running Hasna hook ${hook}`
|
|
47199
|
+
}]
|
|
47200
|
+
}
|
|
47201
|
+
};
|
|
47202
|
+
}
|
|
47203
|
+
function museDiagnostic(outcome) {
|
|
47204
|
+
try {
|
|
47205
|
+
const parsed = JSON.parse(outcome.stdout);
|
|
47206
|
+
const message = parsed?.error?.message;
|
|
47207
|
+
if (typeof message === "string" && message.trim()) {
|
|
47208
|
+
const code2 = parsed.error?.code === undefined ? "" : `${redactedCode(parsed.error.code)}: `;
|
|
47209
|
+
return `${code2}${redactedDiagnostic(message)}`;
|
|
47210
|
+
}
|
|
47211
|
+
} catch {}
|
|
47212
|
+
return redactedDiagnostic(outcome.stderr) || redactedDiagnostic(outcome.stdout) || `exit ${outcome.status}`;
|
|
47213
|
+
}
|
|
47214
|
+
function removeMuseBundle(bundle) {
|
|
47215
|
+
const manifestPath = join22(bundle, ".muse-plugin", "plugin.json");
|
|
47216
|
+
try {
|
|
47217
|
+
if (existsSync13(manifestPath))
|
|
47218
|
+
unlinkSync3(manifestPath);
|
|
47219
|
+
} catch {}
|
|
47220
|
+
for (const dir of [join22(bundle, ".muse-plugin"), bundle]) {
|
|
47221
|
+
try {
|
|
47222
|
+
if (existsSync13(dir))
|
|
47223
|
+
rmdirSync2(dir);
|
|
47224
|
+
} catch {}
|
|
47225
|
+
}
|
|
47226
|
+
}
|
|
47227
|
+
function museBundles(options) {
|
|
47228
|
+
const root = musePluginRoot({ home: homeOf(options), env: options.env ?? process.env });
|
|
47229
|
+
if (!existsSync13(root))
|
|
47230
|
+
return [];
|
|
47231
|
+
const out = [];
|
|
47232
|
+
for (const entry of readdirSync8(root)) {
|
|
47233
|
+
const manifestPath = join22(root, entry, ".muse-plugin", "plugin.json");
|
|
47234
|
+
if (!existsSync13(manifestPath))
|
|
47235
|
+
continue;
|
|
47236
|
+
try {
|
|
47237
|
+
const manifest = JSON.parse(readFileSync8(manifestPath, "utf-8"));
|
|
47238
|
+
for (const capability of manifest.capabilities?.hooks ?? []) {
|
|
47239
|
+
const command = Array.isArray(capability.command) ? capability.command.join(" ") : "";
|
|
47240
|
+
const hook = hookFromCommand(command) ?? hookFromMuseCommand(capability.command);
|
|
47241
|
+
if (hook)
|
|
47242
|
+
out.push({ hook, command });
|
|
47243
|
+
}
|
|
47244
|
+
} catch {}
|
|
47245
|
+
}
|
|
47246
|
+
return out;
|
|
47247
|
+
}
|
|
47248
|
+
function museOwnFiles(options) {
|
|
47249
|
+
const home = homeOf(options);
|
|
47250
|
+
const env = options.env ?? process.env;
|
|
47251
|
+
const configBase = env.XDG_CONFIG_HOME?.startsWith("/") ? env.XDG_CONFIG_HOME : join22(home, ".config");
|
|
47252
|
+
const dataBase = env.XDG_DATA_HOME?.startsWith("/") ? env.XDG_DATA_HOME : join22(home, ".local", "share");
|
|
47253
|
+
const searched = [join22(configBase, "muse", "settings.json"), join22(dataBase, "muse")];
|
|
47254
|
+
const found = searched.filter((path) => existsSync13(path));
|
|
47255
|
+
return { present: found.length > 0, found, searched };
|
|
47256
|
+
}
|
|
47257
|
+
function readMuseState(options, runner = options.run ?? defaultRunner) {
|
|
47258
|
+
const source = "`muse plugins list --json`";
|
|
47259
|
+
const bundleRoot = musePluginRoot({ home: homeOf(options), env: options.env ?? process.env });
|
|
47260
|
+
const unverified = (reason2) => ({ status: "unverified", registrations: [], inactive: [], source, reason: reason2 });
|
|
47261
|
+
const result = runner("muse", ["plugins", "list", "--json"]);
|
|
47262
|
+
if (result.notFound) {
|
|
47263
|
+
const muse = museOwnFiles(options);
|
|
47264
|
+
if (muse.present) {
|
|
47265
|
+
return unverified(`Muse's own store is here (${muse.found.join(", ")}), but the \`muse\` command is not on PATH, so ${source} cannot say what Muse carries \u2014 ` + `a plugin Muse registered may still run, and a bundle under ${bundleRoot} is a file Hooks wrote, not proof either way`);
|
|
47266
|
+
}
|
|
47267
|
+
return {
|
|
47268
|
+
...unverified(`Muse is not installed here: neither its store (${muse.searched.join(" or ")}) nor the \`muse\` command exists, so it carries no registration`),
|
|
47269
|
+
absent: true
|
|
47270
|
+
};
|
|
47271
|
+
}
|
|
47272
|
+
if (result.status !== 0) {
|
|
47273
|
+
return unverified(`${source} failed (${museDiagnostic(result)})`);
|
|
47274
|
+
}
|
|
47275
|
+
let parsed;
|
|
47276
|
+
try {
|
|
47277
|
+
parsed = JSON.parse(result.stdout);
|
|
47278
|
+
} catch {
|
|
47279
|
+
return unverified(`${source} did not return JSON`);
|
|
47280
|
+
}
|
|
47281
|
+
const entries = parsed?.plugins;
|
|
47282
|
+
if (!Array.isArray(entries))
|
|
47283
|
+
return unverified(`${source} returned no plugin list`);
|
|
47284
|
+
const registrations = [];
|
|
47285
|
+
const inactive = [];
|
|
47286
|
+
for (const entry of entries) {
|
|
47287
|
+
const hooks = entry?.plugin?.capabilities?.hooks;
|
|
47288
|
+
if (!Array.isArray(hooks))
|
|
47289
|
+
continue;
|
|
47290
|
+
const enabled = entry?.record?.enabled === true;
|
|
47291
|
+
for (const capability of hooks) {
|
|
47292
|
+
const command = Array.isArray(capability?.command) ? capability.command.filter((part) => typeof part === "string").join(" ") : "";
|
|
47293
|
+
const hook = hookFromMuseCommand(capability?.command);
|
|
47294
|
+
if (!hook)
|
|
47295
|
+
continue;
|
|
47296
|
+
if (enabled) {
|
|
47297
|
+
registrations.push({ hook, command });
|
|
47298
|
+
} else if (!inactive.includes(hook)) {
|
|
47299
|
+
inactive.push(hook);
|
|
47300
|
+
}
|
|
47301
|
+
}
|
|
47302
|
+
}
|
|
47303
|
+
return { status: "verified", registrations, inactive, source };
|
|
47304
|
+
}
|
|
47305
|
+
function installMuse(hook, command, options, timeoutSeconds) {
|
|
47306
|
+
const runner = options.run ?? defaultRunner;
|
|
47307
|
+
const bundle = adapterPath("muse", options, hook);
|
|
47308
|
+
const manifestPath = join22(bundle, ".muse-plugin", "plugin.json");
|
|
47309
|
+
const existedBefore = existsSync13(manifestPath);
|
|
47310
|
+
const manifest = museManifest(hook, command, options, timeoutSeconds);
|
|
47311
|
+
writeText(manifestPath, `${JSON.stringify(manifest, null, 2)}
|
|
47312
|
+
`);
|
|
47313
|
+
const installCommand = `muse plugins install ${bundle} --scope user`;
|
|
47314
|
+
const approveCommand = `muse plugins approve ${manifest.name}`;
|
|
47315
|
+
const cleanup = () => {
|
|
47316
|
+
if (!existedBefore)
|
|
47317
|
+
removeMuseBundle(bundle);
|
|
47318
|
+
};
|
|
47319
|
+
const left = existedBefore ? ` The existing bundle at ${bundle} was left as it was.` : " The bundle was removed again, so nothing is left half-written.";
|
|
47320
|
+
const validated = runner("muse", ["plugins", "validate", bundle, "--json"]);
|
|
47321
|
+
if (validated.notFound) {
|
|
47322
|
+
cleanup();
|
|
47323
|
+
const muse = museOwnFiles(options);
|
|
47324
|
+
return {
|
|
47325
|
+
ok: false,
|
|
47326
|
+
configPath: bundle,
|
|
47327
|
+
error: muse.present ? `Refused: Muse's own store is here (${muse.found.join(", ")}), but the \`muse\` command is not on PATH, so \`muse plugins validate\` could not check the bundle.${left} Run it where the Muse command is available: \`${installCommand}\` then \`${approveCommand}\`.` : `Refused: Muse is not installed here, so \`muse plugins validate\` could not check the bundle.${left} On a station with Muse run \`${installCommand}\` then \`${approveCommand}\`.`
|
|
47328
|
+
};
|
|
47329
|
+
}
|
|
47330
|
+
if (validated.status !== 0) {
|
|
47331
|
+
cleanup();
|
|
47332
|
+
return {
|
|
47333
|
+
ok: false,
|
|
47334
|
+
configPath: bundle,
|
|
47335
|
+
error: `Refused: Muse rejected the plugin manifest (${museDiagnostic(validated)}).${left} Nothing was registered.`
|
|
47336
|
+
};
|
|
47337
|
+
}
|
|
47338
|
+
const installed = runner("muse", ["plugins", "install", bundle, "--scope", "user"]);
|
|
47339
|
+
if (installed.notFound) {
|
|
47340
|
+
cleanup();
|
|
47341
|
+
return {
|
|
47342
|
+
ok: false,
|
|
47343
|
+
configPath: bundle,
|
|
47344
|
+
error: `Refused: Muse disappeared between validation and install, so the plugin was not registered.${left} On a station with Muse run \`${installCommand}\` then \`${approveCommand}\`.`
|
|
47345
|
+
};
|
|
47346
|
+
}
|
|
47347
|
+
if (installed.status !== 0) {
|
|
47348
|
+
cleanup();
|
|
47349
|
+
return {
|
|
47350
|
+
ok: false,
|
|
47351
|
+
configPath: bundle,
|
|
47352
|
+
error: `Refused: \`${installCommand}\` failed (${museDiagnostic(installed)}).${left}`
|
|
47353
|
+
};
|
|
47354
|
+
}
|
|
47355
|
+
const approved = runner("muse", ["plugins", "approve", manifest.name]);
|
|
47356
|
+
if (approved.status !== 0) {
|
|
47357
|
+
const detail = museDiagnostic(approved);
|
|
47358
|
+
return {
|
|
47359
|
+
ok: false,
|
|
47360
|
+
configPath: bundle,
|
|
47361
|
+
error: `Refused: the plugin installed but Muse did not approve it (${detail}); an unapproved plugin does not run. Run \`${approveCommand}\` and retry.`
|
|
47362
|
+
};
|
|
47363
|
+
}
|
|
47364
|
+
return { ok: true, configPath: bundle, note: `installed at user scope and approved as ${manifest.name}` };
|
|
47365
|
+
}
|
|
47366
|
+
function readTextState(path) {
|
|
47367
|
+
try {
|
|
47368
|
+
return { status: "verified", text: readFileSync8(path, "utf-8") };
|
|
47369
|
+
} catch (error61) {
|
|
47370
|
+
if (error61?.code === "ENOENT")
|
|
47371
|
+
return { status: "absent" };
|
|
47372
|
+
return { status: "unreadable", reason: `cannot read ${path} (${redactedDiagnostic(messageOf(error61))})` };
|
|
47373
|
+
}
|
|
47374
|
+
}
|
|
47375
|
+
function readKimiState(options) {
|
|
47376
|
+
const read3 = readTextState(adapterPath("kimi", options));
|
|
47377
|
+
if (read3.status === "unreadable")
|
|
47378
|
+
return { status: "unverified", registrations: [], reason: read3.reason };
|
|
47379
|
+
if (read3.status === "absent")
|
|
47380
|
+
return { status: "verified", registrations: [] };
|
|
47381
|
+
return { status: "verified", registrations: kimiRegistrations(read3.text) };
|
|
47382
|
+
}
|
|
47383
|
+
function readPiState(options) {
|
|
47384
|
+
const read3 = readPiPointer(options);
|
|
47385
|
+
if (read3.status === "unreadable")
|
|
47386
|
+
return { status: "unverified", registrations: [], reason: read3.reason };
|
|
47387
|
+
const profile = piProfile(options, read3.status === "verified" ? read3.value : {});
|
|
47388
|
+
const registrations = piCommands(profile).flatMap((command) => {
|
|
47389
|
+
const hook = hookFromCommand(command);
|
|
47390
|
+
return hook ? [{ hook, command }] : [];
|
|
47391
|
+
});
|
|
47392
|
+
return { status: "verified", registrations };
|
|
47393
|
+
}
|
|
47394
|
+
function readCursorState(options) {
|
|
47395
|
+
const read3 = readCursorFile(adapterPath("cursor", options));
|
|
47396
|
+
if (read3.status === "unreadable")
|
|
47397
|
+
return { status: "unverified", registrations: [], reason: read3.reason };
|
|
47398
|
+
if (read3.status === "absent")
|
|
47399
|
+
return { status: "verified", registrations: [] };
|
|
47400
|
+
return { status: "verified", registrations: cursorRegistrations(read3.value) };
|
|
47401
|
+
}
|
|
47402
|
+
function readAdapterState(target, options, runner) {
|
|
47403
|
+
try {
|
|
47404
|
+
if (target === "kimi")
|
|
47405
|
+
return readKimiState(options);
|
|
47406
|
+
if (target === "pi")
|
|
47407
|
+
return readPiState(options);
|
|
47408
|
+
if (target === "cursor")
|
|
47409
|
+
return readCursorState(options);
|
|
47410
|
+
return readMuseState(options, runner ?? options.run ?? defaultRunner);
|
|
47411
|
+
} catch (error61) {
|
|
47412
|
+
return {
|
|
47413
|
+
status: "unverified",
|
|
47414
|
+
registrations: [],
|
|
47415
|
+
reason: `${target}: cannot read its registration state (${redactedDiagnostic(messageOf(error61))})`
|
|
47416
|
+
};
|
|
47417
|
+
}
|
|
47418
|
+
}
|
|
47419
|
+
function readAdapterRegistrations(target, options) {
|
|
47420
|
+
if (target === "muse") {
|
|
47421
|
+
try {
|
|
47422
|
+
return museBundles(options);
|
|
47423
|
+
} catch {
|
|
47424
|
+
return [];
|
|
47425
|
+
}
|
|
47426
|
+
}
|
|
47427
|
+
return readAdapterState(target, options).registrations;
|
|
47428
|
+
}
|
|
47429
|
+
function adapterStateCarries(state, hook) {
|
|
47430
|
+
if (state.registrations.some((entry) => entry.hook === hook))
|
|
47431
|
+
return true;
|
|
47432
|
+
const inactive = state.inactive;
|
|
47433
|
+
return Array.isArray(inactive) && inactive.includes(hook);
|
|
47434
|
+
}
|
|
47435
|
+
function installInAdapter(target, hook, command, options, timeoutSeconds = 10) {
|
|
47436
|
+
try {
|
|
47437
|
+
if (target === "kimi")
|
|
47438
|
+
return installKimi(hook, command, options, timeoutSeconds);
|
|
47439
|
+
if (target === "pi")
|
|
47440
|
+
return installPi(hook, command, options);
|
|
47441
|
+
if (target === "cursor")
|
|
47442
|
+
return installCursor(hook, command, options, timeoutSeconds);
|
|
47443
|
+
return installMuse(hook, command, options, timeoutSeconds);
|
|
47444
|
+
} catch (error61) {
|
|
47445
|
+
return { ok: false, configPath: "", error: `Refused: ${target}: the install did not complete (${redactedDiagnostic(messageOf(error61))})` };
|
|
47446
|
+
}
|
|
47447
|
+
}
|
|
47448
|
+
function removeFromAdapter(target, hook, options) {
|
|
47449
|
+
try {
|
|
47450
|
+
if (target === "kimi") {
|
|
47451
|
+
const path = adapterPath("kimi", options);
|
|
47452
|
+
const existing = readTextState(path);
|
|
47453
|
+
if (existing.status === "unreadable") {
|
|
47454
|
+
return { removed: false, reason: `${existing.reason}, so Hooks cannot tell whether '${hook}' is registered; nothing was removed` };
|
|
47455
|
+
}
|
|
47456
|
+
const { text: text3, removed } = removeKimiChunk(existing.status === "verified" ? existing.text : "", hook);
|
|
47457
|
+
if (!removed)
|
|
47458
|
+
return { removed: false, reason: `no '${hook}' entry in ${path}` };
|
|
47459
|
+
writeText(path, text3.endsWith(`
|
|
47460
|
+
`) ? text3 : `${text3}
|
|
47461
|
+
`);
|
|
47462
|
+
return { removed: true };
|
|
47463
|
+
}
|
|
47464
|
+
if (target === "pi") {
|
|
47465
|
+
const pointer = readPiPointer(options);
|
|
47466
|
+
if (pointer.status === "unreadable") {
|
|
47467
|
+
return { removed: false, reason: `${pointer.reason}, so Hooks cannot tell whether '${hook}' is registered; nothing was removed` };
|
|
47468
|
+
}
|
|
47469
|
+
const profile = piProfile(options);
|
|
47470
|
+
if (!profile)
|
|
47471
|
+
return { removed: false, reason: `no Hooks profile is registered for Pi; nothing to remove` };
|
|
47472
|
+
const commands = piCommands(profile);
|
|
47473
|
+
const kept = commands.filter((command) => hookFromCommand(command) !== hook);
|
|
47474
|
+
if (kept.length === commands.length)
|
|
47475
|
+
return { removed: false, reason: `no '${hook}' entry in profile ${profile.agent_id}` };
|
|
47476
|
+
writePiCommands(profile.agent_id, kept);
|
|
47477
|
+
return { removed: true };
|
|
47478
|
+
}
|
|
47479
|
+
if (target === "cursor") {
|
|
47480
|
+
const path = adapterPath("cursor", options);
|
|
47481
|
+
const read3 = readCursorFile(path);
|
|
47482
|
+
if (read3.status === "unreadable") {
|
|
47483
|
+
return { removed: false, reason: `${read3.reason}, so Hooks cannot tell whether '${hook}' is registered; nothing was removed` };
|
|
47484
|
+
}
|
|
47485
|
+
const config2 = read3.status === "verified" ? read3.value : undefined;
|
|
47486
|
+
if (!config2?.hooks)
|
|
47487
|
+
return { removed: false, reason: `no '${hook}' entry in ${path}` };
|
|
47488
|
+
let removed = false;
|
|
47489
|
+
const hooks = {};
|
|
47490
|
+
for (const key2 of Object.keys(config2.hooks)) {
|
|
47491
|
+
const entries = config2.hooks[key2];
|
|
47492
|
+
if (!Array.isArray(entries)) {
|
|
47493
|
+
hooks[key2] = entries;
|
|
47494
|
+
continue;
|
|
47495
|
+
}
|
|
47496
|
+
const kept = entries.filter((entry) => {
|
|
47497
|
+
if (typeof entry?.command !== "string" || hookFromCommand(entry.command) !== hook)
|
|
47498
|
+
return true;
|
|
47499
|
+
removed = true;
|
|
47500
|
+
return false;
|
|
47501
|
+
});
|
|
47502
|
+
if (kept.length > 0)
|
|
47503
|
+
hooks[key2] = kept;
|
|
47504
|
+
}
|
|
47505
|
+
if (!removed)
|
|
47506
|
+
return { removed: false, reason: `no '${hook}' entry in ${path}` };
|
|
47507
|
+
const next = { ...config2, hooks };
|
|
47508
|
+
if (Object.keys(hooks).length === 0)
|
|
47509
|
+
delete next.hooks;
|
|
47510
|
+
writeText(path, `${JSON.stringify(next, null, 2)}
|
|
47511
|
+
`);
|
|
47512
|
+
return { removed: true };
|
|
47513
|
+
}
|
|
47514
|
+
const bundle = adapterPath("muse", options, hook);
|
|
47515
|
+
const manifestPath = join22(bundle, ".muse-plugin", "plugin.json");
|
|
47516
|
+
const hadBundle = existsSync13(manifestPath);
|
|
47517
|
+
const bundleNote = hadBundle ? `the bundle at ${bundle} was left in place` : `no bundle was present at ${bundle}`;
|
|
47518
|
+
const pluginId2 = `hasna-hook-${hook}`;
|
|
47519
|
+
const result = (options.run ?? defaultRunner)("muse", ["plugins", "remove", pluginId2, "--json"]);
|
|
47520
|
+
if (result.notFound) {
|
|
47521
|
+
const muse = museOwnFiles(options);
|
|
47522
|
+
return {
|
|
47523
|
+
removed: false,
|
|
47524
|
+
reason: muse.present ? `Refused: Muse's own store is here (${muse.found.join(", ")}), but the \`muse\` command is not on PATH, so '${hook}' cannot be unregistered from it and may still run; ${bundleNote}. Run \`muse plugins remove ${pluginId2}\` where the Muse command is available.` : `Refused: Muse is not installed here, so '${hook}' cannot be unregistered from Muse's own store; ${bundleNote}. Run \`muse plugins remove ${pluginId2}\` on a station with Muse.`
|
|
47525
|
+
};
|
|
47526
|
+
}
|
|
47527
|
+
const refusal3 = result.status === 0 ? undefined : museDiagnostic(result);
|
|
47528
|
+
const alreadyGone = refusal3 !== undefined && /unknown-plugin|not installed/i.test(refusal3);
|
|
47529
|
+
if (refusal3 !== undefined && !alreadyGone) {
|
|
47530
|
+
return {
|
|
47531
|
+
removed: false,
|
|
47532
|
+
reason: `Refused: \`muse plugins remove ${pluginId2}\` failed (${refusal3}); ${bundleNote}.`
|
|
47533
|
+
};
|
|
47534
|
+
}
|
|
47535
|
+
removeMuseBundle(bundle);
|
|
47536
|
+
return {
|
|
47537
|
+
removed: true,
|
|
47538
|
+
bundleRemoved: hadBundle,
|
|
47539
|
+
reason: alreadyGone ? `Muse did not carry ${pluginId2}; ${hadBundle ? `the bundle at ${bundle} was removed` : `no bundle was present at ${bundle}`}` : hadBundle ? `unregistered ${pluginId2} from Muse and removed ${bundle}` : `unregistered ${pluginId2} from Muse; no bundle was present at ${bundle}`
|
|
47540
|
+
};
|
|
47541
|
+
} catch (error61) {
|
|
47542
|
+
return { removed: false, reason: `${target}: the removal failed (${redactedDiagnostic(messageOf(error61))}); nothing was removed` };
|
|
47543
|
+
}
|
|
46649
47544
|
}
|
|
46650
47545
|
|
|
46651
47546
|
// src/lib/installer.ts
|
|
46652
|
-
var __dirname2 =
|
|
46653
|
-
var HOOKS_DIR =
|
|
47547
|
+
var __dirname2 = dirname16(fileURLToPath3(import.meta.url));
|
|
47548
|
+
var HOOKS_DIR = existsSync14(join24(__dirname2, "..", "..", "hooks", "hook-gitguard")) ? join24(__dirname2, "..", "..", "hooks") : join24(__dirname2, "..", "hooks");
|
|
46654
47549
|
function normalizeHookName(name) {
|
|
46655
47550
|
return name.startsWith("hook-") ? name : `hook-${name}`;
|
|
46656
47551
|
}
|
|
@@ -46667,17 +47562,19 @@ function removeHookEntriesByName(entries, hookName) {
|
|
|
46667
47562
|
return kept.length === entry.hooks.length ? [entry] : kept.length ? [{ ...entry, hooks: kept }] : [];
|
|
46668
47563
|
});
|
|
46669
47564
|
}
|
|
47565
|
+
var CLAUDE_EVENT_MAP = {
|
|
47566
|
+
PreToolUse: "PreToolUse",
|
|
47567
|
+
PostToolUse: "PostToolUse",
|
|
47568
|
+
Stop: "Stop",
|
|
47569
|
+
Notification: "Notification",
|
|
47570
|
+
SessionStart: "SessionStart",
|
|
47571
|
+
SessionEnd: "SessionEnd",
|
|
47572
|
+
UserPromptSubmit: "UserPromptSubmit",
|
|
47573
|
+
SubagentStart: null
|
|
47574
|
+
};
|
|
46670
47575
|
var EVENT_MAP = {
|
|
46671
|
-
claude:
|
|
46672
|
-
|
|
46673
|
-
PostToolUse: "PostToolUse",
|
|
46674
|
-
Stop: "Stop",
|
|
46675
|
-
Notification: "Notification",
|
|
46676
|
-
SessionStart: "SessionStart",
|
|
46677
|
-
SessionEnd: "SessionEnd",
|
|
46678
|
-
UserPromptSubmit: "UserPromptSubmit",
|
|
46679
|
-
SubagentStart: null
|
|
46680
|
-
},
|
|
47576
|
+
claude: CLAUDE_EVENT_MAP,
|
|
47577
|
+
qwen: CLAUDE_EVENT_MAP,
|
|
46681
47578
|
gemini: {
|
|
46682
47579
|
PreToolUse: "BeforeTool",
|
|
46683
47580
|
PostToolUse: "AfterTool",
|
|
@@ -46709,15 +47606,6 @@ var EVENT_MAP = {
|
|
|
46709
47606
|
SubagentStart: "SubagentStart"
|
|
46710
47607
|
}
|
|
46711
47608
|
};
|
|
46712
|
-
function getTargetSettingsDir(target) {
|
|
46713
|
-
if (target === "codex")
|
|
46714
|
-
return ".codex";
|
|
46715
|
-
if (target === "codewith")
|
|
46716
|
-
return ".codewith";
|
|
46717
|
-
if (target === "gemini")
|
|
46718
|
-
return ".gemini";
|
|
46719
|
-
return ".claude";
|
|
46720
|
-
}
|
|
46721
47609
|
function getGlobalSettingsPathOverride(target) {
|
|
46722
47610
|
if (target === "codex")
|
|
46723
47611
|
return process.env.HASNA_HOOKS_CODEX_SETTINGS_PATH ?? (process.env.CODEX_HOME ? resolve28(process.env.CODEX_HOME, "hooks.json") : undefined);
|
|
@@ -46725,34 +47613,38 @@ function getGlobalSettingsPathOverride(target) {
|
|
|
46725
47613
|
return process.env.HASNA_HOOKS_CLAUDE_SETTINGS_PATH ?? (process.env.CLAUDE_CONFIG_DIR ? resolve28(process.env.CLAUDE_CONFIG_DIR, "settings.json") : undefined);
|
|
46726
47614
|
if (target === "gemini")
|
|
46727
47615
|
return process.env.HASNA_HOOKS_GEMINI_SETTINGS_PATH;
|
|
47616
|
+
if (target === "qwen")
|
|
47617
|
+
return process.env.HASNA_HOOKS_QWEN_SETTINGS_PATH;
|
|
47618
|
+
if (target === "codewith")
|
|
47619
|
+
return process.env.HASNA_HOOKS_CODEWITH_CONFIG_PATH;
|
|
46728
47620
|
return;
|
|
46729
47621
|
}
|
|
47622
|
+
function capabilityOptions(scope, hook) {
|
|
47623
|
+
return { scope, home: homedir8(), cwd: process.cwd(), env: process.env, ...hook ? { hook } : {} };
|
|
47624
|
+
}
|
|
47625
|
+
function adapterOptions(scope, home, run) {
|
|
47626
|
+
return { scope, home: home ?? process.env.HOME ?? homedir8(), cwd: process.cwd(), env: process.env, ...run ? { run } : {} };
|
|
47627
|
+
}
|
|
46730
47628
|
function getSettingsPath(scope = "global", target = "claude", codewithConfigPath) {
|
|
46731
47629
|
if (target === "codewith" && codewithConfigPath)
|
|
46732
47630
|
return codewithConfigPath;
|
|
46733
|
-
if (target === "codewith" && scope === "global" && process.env.HASNA_HOOKS_CODEWITH_CONFIG_PATH) {
|
|
46734
|
-
return process.env.HASNA_HOOKS_CODEWITH_CONFIG_PATH;
|
|
46735
|
-
}
|
|
46736
47631
|
const globalOverride = scope === "global" ? getGlobalSettingsPathOverride(target) : undefined;
|
|
46737
47632
|
if (globalOverride)
|
|
46738
47633
|
return globalOverride;
|
|
46739
|
-
const
|
|
46740
|
-
if (
|
|
46741
|
-
return
|
|
46742
|
-
|
|
46743
|
-
return target === "codewith" ? join17(process.cwd(), dir, "config.toml") : join17(process.cwd(), dir, "settings.json");
|
|
46744
|
-
}
|
|
46745
|
-
return target === "codewith" ? join17(homedir7(), dir, "config.toml") : join17(homedir7(), dir, "settings.json");
|
|
47634
|
+
const capabilities = getTargetCapabilities(target, capabilityOptions(scope));
|
|
47635
|
+
if (capabilities)
|
|
47636
|
+
return capabilities.configPath;
|
|
47637
|
+
return join24(homedir8(), ".claude", "settings.json");
|
|
46746
47638
|
}
|
|
46747
47639
|
function getHookPath(name) {
|
|
46748
47640
|
const shortName = shortHookName(name);
|
|
46749
47641
|
const resolved = resolveHookDir(shortName);
|
|
46750
47642
|
if (resolved)
|
|
46751
47643
|
return resolved;
|
|
46752
|
-
return
|
|
47644
|
+
return join24(HOOKS_DIR, normalizeHookName(shortName));
|
|
46753
47645
|
}
|
|
46754
47646
|
function hookExists(name) {
|
|
46755
|
-
return
|
|
47647
|
+
return existsSync14(getHookPath(name));
|
|
46756
47648
|
}
|
|
46757
47649
|
var settingsPreimages = new WeakMap;
|
|
46758
47650
|
var unreadableSettings = new WeakMap;
|
|
@@ -46762,8 +47654,8 @@ function readSettings(scope = "global", target = "claude") {
|
|
|
46762
47654
|
return readCodexSettings(path);
|
|
46763
47655
|
let reason2 = "cannot be read";
|
|
46764
47656
|
try {
|
|
46765
|
-
if (
|
|
46766
|
-
const text3 =
|
|
47657
|
+
if (existsSync14(path)) {
|
|
47658
|
+
const text3 = readFileSync13(path, "utf-8");
|
|
46767
47659
|
reason2 = "is not valid JSON";
|
|
46768
47660
|
const value = JSON.parse(text3);
|
|
46769
47661
|
reason2 = "does not hold a JSON object";
|
|
@@ -46779,7 +47671,7 @@ function readSettings(scope = "global", target = "claude") {
|
|
|
46779
47671
|
console.warn("[hooks] Cannot read hook settings; repair the selected file before installation.");
|
|
46780
47672
|
}
|
|
46781
47673
|
const empty = {};
|
|
46782
|
-
if (!
|
|
47674
|
+
if (!existsSync14(path))
|
|
46783
47675
|
settingsPreimages.set(empty, { path, text: null });
|
|
46784
47676
|
else
|
|
46785
47677
|
unreadableSettings.set(empty, { path, reason: reason2 });
|
|
@@ -46787,23 +47679,23 @@ function readSettings(scope = "global", target = "claude") {
|
|
|
46787
47679
|
}
|
|
46788
47680
|
function unreadableSettingsRefusal(settings, path) {
|
|
46789
47681
|
const captured = settingsPreimages.get(settings);
|
|
46790
|
-
if (captured?.path === path && captured.text !== null || !
|
|
47682
|
+
if (captured?.path === path && captured.text !== null || !existsSync14(path))
|
|
46791
47683
|
return;
|
|
46792
47684
|
const failed = unreadableSettings.get(settings);
|
|
46793
47685
|
const reason2 = captured?.path === path ? "was created after Hooks read the settings" : failed?.path === path ? failed.reason : "was not read as a settings object";
|
|
46794
47686
|
return new ClaudeSettingsRefusal("settings_unreadable", `${path} ${reason2}, so Hooks wrote nothing. Repair the file or move it aside, then retry.`);
|
|
46795
47687
|
}
|
|
46796
47688
|
function settingsIdentity(path) {
|
|
46797
|
-
if (
|
|
47689
|
+
if (existsSync14(path))
|
|
46798
47690
|
return realpathSync4(path);
|
|
46799
|
-
return
|
|
47691
|
+
return existsSync14(dirname16(path)) ? join24(realpathSync4(dirname16(path)), basename12(path)) : resolve28(path);
|
|
46800
47692
|
}
|
|
46801
47693
|
function coordinatesWithSkills(path, target) {
|
|
46802
|
-
return target === "claude" && settingsIdentity(path) === settingsIdentity(
|
|
47694
|
+
return target === "claude" && settingsIdentity(path) === settingsIdentity(join24(homedir8(), ".claude/settings.json"));
|
|
46803
47695
|
}
|
|
46804
47696
|
function planClaudeSettingsWrite(before, after, scope = "global") {
|
|
46805
47697
|
if (coordinatesWithSkills(getSettingsPath(scope, "claude"), "claude"))
|
|
46806
|
-
planCoordinatedClaudeSettings(before, after, { home:
|
|
47698
|
+
planCoordinatedClaudeSettings(before, after, { home: homedir8() });
|
|
46807
47699
|
}
|
|
46808
47700
|
function writerRefusal(error61) {
|
|
46809
47701
|
return { error: error61 instanceof Error ? error61.message : "Unknown error", ...error61 instanceof ClaudeSettingsRefusal ? { code: error61.code } : {} };
|
|
@@ -46825,21 +47717,21 @@ function writeSettings(settings, scope = "global", target = "claude", expectedSe
|
|
|
46825
47717
|
const before = settingsPreimages.get(settings);
|
|
46826
47718
|
if (!before || before.path !== path)
|
|
46827
47719
|
throw new Error("Claude settings require a valid captured preimage before installation.");
|
|
46828
|
-
const current2 =
|
|
47720
|
+
const current2 = existsSync14(path) ? readFileSync13(path, "utf8") : null;
|
|
46829
47721
|
if (current2 !== before.text)
|
|
46830
47722
|
throw new Error("Claude settings changed during installation; no settings were written.");
|
|
46831
47723
|
if (coordinateClaudeSettings(before.text, JSON.stringify(settings, null, 2) + `
|
|
46832
|
-
`, { home:
|
|
47724
|
+
`, { home: homedir8() }, beforeWrite))
|
|
46833
47725
|
return;
|
|
46834
47726
|
}
|
|
46835
47727
|
if (expectedSettingsSha256 !== undefined)
|
|
46836
47728
|
return writeHookSettingsPreimage(path, settings, captured.text, beforeWrite);
|
|
46837
47729
|
beforeWrite?.();
|
|
46838
|
-
const dir =
|
|
46839
|
-
if (!
|
|
46840
|
-
|
|
47730
|
+
const dir = dirname16(path);
|
|
47731
|
+
if (!existsSync14(dir)) {
|
|
47732
|
+
mkdirSync12(dir, { recursive: true });
|
|
46841
47733
|
}
|
|
46842
|
-
|
|
47734
|
+
writeFileSync10(path, JSON.stringify(settings, null, 2) + `
|
|
46843
47735
|
`);
|
|
46844
47736
|
}
|
|
46845
47737
|
function getTargetEventName(internalEvent, target) {
|
|
@@ -46848,7 +47740,7 @@ function getTargetEventName(internalEvent, target) {
|
|
|
46848
47740
|
function isEventSupported(internalEvent, target) {
|
|
46849
47741
|
return getTargetEventName(internalEvent, target) !== null;
|
|
46850
47742
|
}
|
|
46851
|
-
function
|
|
47743
|
+
function tomlString2(value) {
|
|
46852
47744
|
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
46853
47745
|
}
|
|
46854
47746
|
function codewithMatcher(matcher) {
|
|
@@ -46922,8 +47814,8 @@ function buildCodewithTomlFragment(name, profile, mementos) {
|
|
|
46922
47814
|
`[[hooks.${eventKey}]]`
|
|
46923
47815
|
];
|
|
46924
47816
|
if (matcher)
|
|
46925
|
-
lines.push(`matcher = ${
|
|
46926
|
-
lines.push("", `[[hooks.${eventKey}.hooks]]`, `type = "command"`, `command = ${
|
|
47817
|
+
lines.push(`matcher = ${tomlString2(matcher)}`);
|
|
47818
|
+
lines.push("", `[[hooks.${eventKey}.hooks]]`, `type = "command"`, `command = ${tomlString2(command)}`, `timeout = ${codewithTimeout(shortName)}`, `statusMessage = ${tomlString2(codewithStatusMessage(shortName))}`);
|
|
46927
47819
|
fragments.push(lines.join(`
|
|
46928
47820
|
`));
|
|
46929
47821
|
}
|
|
@@ -46935,21 +47827,21 @@ function buildCodewithTomlFragment(name, profile, mementos) {
|
|
|
46935
47827
|
function readCodewithConfig(scope, configPath) {
|
|
46936
47828
|
const path = getSettingsPath(scope, "codewith", configPath);
|
|
46937
47829
|
try {
|
|
46938
|
-
return
|
|
47830
|
+
return existsSync14(path) ? readFileSync13(path, "utf-8") : "";
|
|
46939
47831
|
} catch {
|
|
46940
47832
|
return "";
|
|
46941
47833
|
}
|
|
46942
47834
|
}
|
|
46943
47835
|
function appendCodewithFragment(fragment, scope, configPath) {
|
|
46944
47836
|
const path = getSettingsPath(scope, "codewith", configPath);
|
|
46945
|
-
const dir =
|
|
46946
|
-
if (!
|
|
46947
|
-
|
|
46948
|
-
const existing =
|
|
47837
|
+
const dir = dirname16(path);
|
|
47838
|
+
if (!existsSync14(dir))
|
|
47839
|
+
mkdirSync12(dir, { recursive: true });
|
|
47840
|
+
const existing = existsSync14(path) ? readFileSync13(path, "utf-8") : "";
|
|
46949
47841
|
const sep10 = existing.trim() ? `
|
|
46950
47842
|
|
|
46951
47843
|
` : "";
|
|
46952
|
-
|
|
47844
|
+
writeFileSync10(path, `${existing.replace(/\s*$/, "")}${sep10}${fragment}`);
|
|
46953
47845
|
return path;
|
|
46954
47846
|
}
|
|
46955
47847
|
function detectConflict(name, scope, target) {
|
|
@@ -46992,14 +47884,59 @@ function detectRewriteConflict(name, scope, target, lookup = getHook) {
|
|
|
46992
47884
|
}
|
|
46993
47885
|
return;
|
|
46994
47886
|
}
|
|
47887
|
+
function installForAdapter(shortName, scope, overwrite, target, events3, timeoutSeconds, profile, mementos) {
|
|
47888
|
+
const stopEvent = getTargetCapabilities(target, capabilityOptions(scope, shortName))?.stopEvent ?? "turn-end";
|
|
47889
|
+
const unsupported = events3.filter((event) => event !== "Stop");
|
|
47890
|
+
if (events3.length === 0 || unsupported.length > 0) {
|
|
47891
|
+
return {
|
|
47892
|
+
hook: shortName,
|
|
47893
|
+
success: false,
|
|
47894
|
+
scope,
|
|
47895
|
+
target,
|
|
47896
|
+
error: `Refused: '${shortName}' runs on ${events3.length > 0 ? events3.join(", ") : "no event"}, and target '${target}' only has the ${stopEvent} turn-end event. Install it on claude, codex, gemini, codewith or qwen; a harness config is never given an event it does not carry.`
|
|
47897
|
+
};
|
|
47898
|
+
}
|
|
47899
|
+
const options = adapterOptions(scope);
|
|
47900
|
+
const registered = readAdapterRegistrations(target, options).some((entry) => entry.hook === shortName);
|
|
47901
|
+
if (registered && !overwrite) {
|
|
47902
|
+
return { hook: shortName, success: false, error: "Already installed. Use --overwrite to replace.", scope, target };
|
|
47903
|
+
}
|
|
47904
|
+
if (registered)
|
|
47905
|
+
removeFromAdapter(target, shortName, options);
|
|
47906
|
+
const conflict = detectConflict(shortName, scope, target);
|
|
47907
|
+
const command = (profile ? `hooks run ${shortName} --profile ${profile}` : `hooks run ${shortName}`) + ` --target ${target}` + mementosCommandSuffix(mementos);
|
|
47908
|
+
const outcome = installInAdapter(target, shortName, command, options, timeoutSeconds);
|
|
47909
|
+
if (!outcome.ok) {
|
|
47910
|
+
return { hook: shortName, success: false, error: outcome.error, scope, target, ...outcome.configPath ? { configPath: outcome.configPath } : {} };
|
|
47911
|
+
}
|
|
47912
|
+
const note = [outcome.note, targetInstallNote(target)].filter(Boolean).join(" ");
|
|
47913
|
+
return {
|
|
47914
|
+
hook: shortName,
|
|
47915
|
+
success: true,
|
|
47916
|
+
scope,
|
|
47917
|
+
target,
|
|
47918
|
+
applied: true,
|
|
47919
|
+
conflict,
|
|
47920
|
+
configPath: outcome.configPath,
|
|
47921
|
+
...note ? { note } : {}
|
|
47922
|
+
};
|
|
47923
|
+
}
|
|
46995
47924
|
function installForTarget(name, scope, overwrite, target, profile, codewithMode = "fragment", codewithConfigPath, mementos, expectedSettingsSha256, beforeWrite) {
|
|
46996
47925
|
const shortName = shortHookName(name);
|
|
46997
|
-
if (isNativeSafetyName(shortName) && (profile || target === "gemini" || target === "codewith" && codewithMode === "write")) {
|
|
47926
|
+
if (isNativeSafetyName(shortName) && (profile || target === "gemini" || isAdapterTarget(target) || target === "codewith" && codewithMode === "write")) {
|
|
46998
47927
|
return { hook: shortName, success: false, error: "Bundled native safety guards support Claude/Codex and managed Codewith fragments, without registry profiles.", target };
|
|
46999
47928
|
}
|
|
47000
47929
|
if (!isNativeSafetyName(shortName) && !hookExists(shortName)) {
|
|
47001
47930
|
return { hook: shortName, success: false, error: `Hook '${shortName}' not found`, target };
|
|
47002
47931
|
}
|
|
47932
|
+
const meta3 = isNativeSafetyName(shortName) ? getHook(shortName) : resolveHookMeta(shortName);
|
|
47933
|
+
const events3 = meta3 ? getHookEvents(meta3) : [];
|
|
47934
|
+
const capabilityRefusal = installCapabilityRefusal(shortName, target, { events: events3, requiresFinalMessage: meta3?.requiresFinalMessage === true, requiresBlock: meta3?.requiresBlock === true }, capabilityOptions(scope, shortName));
|
|
47935
|
+
if (capabilityRefusal)
|
|
47936
|
+
return { hook: shortName, success: false, error: capabilityRefusal, scope, target };
|
|
47937
|
+
if (isAdapterTarget(target)) {
|
|
47938
|
+
return installForAdapter(shortName, scope, overwrite, target, events3, meta3?.timeoutSeconds ?? 10, profile, mementos);
|
|
47939
|
+
}
|
|
47003
47940
|
if (target === "codewith") {
|
|
47004
47941
|
try {
|
|
47005
47942
|
const fragment = buildCodewithTomlFragment(shortName, profile, mementos);
|
|
@@ -47050,7 +47987,8 @@ function installForTarget(name, scope, overwrite, target, profile, codewithMode
|
|
|
47050
47987
|
}
|
|
47051
47988
|
}
|
|
47052
47989
|
try {
|
|
47053
|
-
const
|
|
47990
|
+
const settingsTarget = target;
|
|
47991
|
+
const registered = getRegisteredHooksForTarget(scope, settingsTarget);
|
|
47054
47992
|
if (registered.includes(shortName) && !overwrite) {
|
|
47055
47993
|
return { hook: shortName, success: false, error: "Already installed. Use --overwrite to replace.", scope, target };
|
|
47056
47994
|
}
|
|
@@ -47059,7 +47997,7 @@ function installForTarget(name, scope, overwrite, target, profile, codewithMode
|
|
|
47059
47997
|
if (rewriteConflict) {
|
|
47060
47998
|
return { hook: shortName, success: false, error: `Refused: ${rewriteConflict}`, scope, target };
|
|
47061
47999
|
}
|
|
47062
|
-
registerHook(shortName, scope,
|
|
48000
|
+
registerHook(shortName, scope, settingsTarget, profile, mementos, (settings, selectedScope, selectedTarget) => writeSettings(settings, selectedScope, selectedTarget, expectedSettingsSha256, beforeWrite));
|
|
47063
48001
|
return {
|
|
47064
48002
|
hook: shortName,
|
|
47065
48003
|
success: true,
|
|
@@ -47283,6 +48221,9 @@ function installHooks(names, options = {}) {
|
|
|
47283
48221
|
return names.map((name) => installHook(name, options));
|
|
47284
48222
|
}
|
|
47285
48223
|
function getRegisteredHooksForTarget(scope = "global", target = "claude") {
|
|
48224
|
+
if (isAdapterTarget(target)) {
|
|
48225
|
+
return [...new Set(readAdapterRegistrations(target, adapterOptions(scope)).map((entry) => entry.hook))];
|
|
48226
|
+
}
|
|
47286
48227
|
if (target === "codewith") {
|
|
47287
48228
|
const config2 = readCodewithConfig(scope);
|
|
47288
48229
|
const registered2 = [];
|
|
@@ -47322,20 +48263,37 @@ function getRegisteredHooks(scope = "global") {
|
|
|
47322
48263
|
return getRegisteredHooksForTarget(scope, "claude");
|
|
47323
48264
|
}
|
|
47324
48265
|
var getInstalledHooks = getRegisteredHooks;
|
|
47325
|
-
function removeHook(name, scope = "global", target = "claude") {
|
|
48266
|
+
function removeHook(name, scope = "global", target = "claude", options = {}) {
|
|
47326
48267
|
const shortName = shortHookName(name);
|
|
47327
48268
|
if (target === "all") {
|
|
47328
|
-
|
|
47329
|
-
const
|
|
47330
|
-
|
|
47331
|
-
|
|
48269
|
+
let removedAny = false;
|
|
48270
|
+
for (const candidate of ALL_REMOVABLE_TARGETS) {
|
|
48271
|
+
if (removeHookForTarget(shortName, scope, candidate, options.run))
|
|
48272
|
+
removedAny = true;
|
|
48273
|
+
}
|
|
48274
|
+
return removedAny;
|
|
47332
48275
|
}
|
|
47333
48276
|
if (target === "codewith") {
|
|
47334
48277
|
return false;
|
|
47335
48278
|
}
|
|
47336
|
-
|
|
48279
|
+
if (isAdapterTarget(target)) {
|
|
48280
|
+
return removeFromAdapter(target, shortName, adapterOptions(scope, undefined, options.run)).removed;
|
|
48281
|
+
}
|
|
48282
|
+
if (target === "opencode" || target === "sumi") {
|
|
48283
|
+
return false;
|
|
48284
|
+
}
|
|
48285
|
+
return removeHookForTarget(shortName, scope, target, options.run);
|
|
47337
48286
|
}
|
|
47338
|
-
function removeHookForTarget(name, scope, target) {
|
|
48287
|
+
function removeHookForTarget(name, scope, target, run) {
|
|
48288
|
+
if (isAdapterTarget(target)) {
|
|
48289
|
+
const options = adapterOptions(scope, undefined, run);
|
|
48290
|
+
const state = readAdapterState(target, options);
|
|
48291
|
+
if (state.status !== "verified")
|
|
48292
|
+
return false;
|
|
48293
|
+
if (!adapterStateCarries(state, name))
|
|
48294
|
+
return false;
|
|
48295
|
+
return removeFromAdapter(target, name, options).removed;
|
|
48296
|
+
}
|
|
47339
48297
|
const registered = getRegisteredHooksForTarget(scope, target);
|
|
47340
48298
|
if (!registered.includes(name)) {
|
|
47341
48299
|
return false;
|
|
@@ -47343,7 +48301,8 @@ function removeHookForTarget(name, scope, target) {
|
|
|
47343
48301
|
unregisterHook(name, scope, target);
|
|
47344
48302
|
return true;
|
|
47345
48303
|
}
|
|
47346
|
-
var JSON_SETTINGS_TARGETS = ["claude", "gemini", "codex"];
|
|
48304
|
+
var JSON_SETTINGS_TARGETS = ["claude", "gemini", "codex", "qwen"];
|
|
48305
|
+
var ALL_REMOVABLE_TARGETS = [...JSON_SETTINGS_TARGETS, ...ADAPTER_TARGETS];
|
|
47347
48306
|
function removeCodewithHookEntry(configText, name) {
|
|
47348
48307
|
const sections = configText.split(/\n\s*\n/);
|
|
47349
48308
|
const commandRe = /^command\s*=\s*"([^"\n]+)"$/;
|
|
@@ -47400,70 +48359,149 @@ function codewithHasHookEntryInText(configText, name) {
|
|
|
47400
48359
|
}
|
|
47401
48360
|
function codewithHasHookEntry(name, scope = "global") {
|
|
47402
48361
|
const path = getSettingsPath(scope, "codewith");
|
|
47403
|
-
if (!
|
|
48362
|
+
if (!existsSync14(path))
|
|
47404
48363
|
return false;
|
|
47405
48364
|
try {
|
|
47406
|
-
return codewithHasHookEntryInText(
|
|
48365
|
+
return codewithHasHookEntryInText(readFileSync13(path, "utf-8"), name);
|
|
47407
48366
|
} catch {
|
|
47408
48367
|
return false;
|
|
47409
48368
|
}
|
|
47410
48369
|
}
|
|
47411
48370
|
function removeStoreDirFailClosed(dir) {
|
|
47412
|
-
const entries =
|
|
48371
|
+
const entries = readdirSync12(dir, { withFileTypes: true });
|
|
47413
48372
|
for (const entry of entries) {
|
|
47414
48373
|
if (entry.isDirectory()) {
|
|
47415
|
-
removeStoreDirFailClosed(
|
|
48374
|
+
removeStoreDirFailClosed(join24(dir, entry.name));
|
|
47416
48375
|
}
|
|
47417
48376
|
}
|
|
47418
48377
|
for (const entry of entries) {
|
|
47419
48378
|
if (!entry.isDirectory()) {
|
|
47420
|
-
|
|
48379
|
+
unlinkSync6(join24(dir, entry.name));
|
|
47421
48380
|
}
|
|
47422
48381
|
}
|
|
47423
|
-
|
|
48382
|
+
rmdirSync4(dir);
|
|
47424
48383
|
}
|
|
47425
|
-
function uninstallHook(name, scope = "global", target = "claude") {
|
|
48384
|
+
function uninstallHook(name, scope = "global", target = "claude", options = {}) {
|
|
47426
48385
|
const shortName = shortHookName(name);
|
|
48386
|
+
const adapterOptionsFor = (s) => adapterOptions(s, undefined, options.run);
|
|
47427
48387
|
const custom3 = readCustomManifest(shortName);
|
|
47428
48388
|
const bundledMeta = custom3 ? undefined : getHook(shortName);
|
|
47429
|
-
const
|
|
47430
|
-
const
|
|
47431
|
-
const
|
|
47432
|
-
const
|
|
48389
|
+
const configuredTargets = target === "all" ? ALL_REMOVABLE_TARGETS : [target];
|
|
48390
|
+
const adapterStates = new Map;
|
|
48391
|
+
const unreadable = [];
|
|
48392
|
+
const notes = [];
|
|
48393
|
+
const withNotes = (result) => notes.length > 0 ? { ...result, notes } : result;
|
|
48394
|
+
const adapterStateFor = (t, s) => {
|
|
48395
|
+
const key2 = `${s}:${t}`;
|
|
48396
|
+
const cached2 = adapterStates.get(key2);
|
|
48397
|
+
if (cached2)
|
|
48398
|
+
return cached2;
|
|
48399
|
+
const state = isAdapterTarget(t) ? readAdapterState(t, adapterOptionsFor(s)) : { status: "verified", registrations: [] };
|
|
48400
|
+
adapterStates.set(key2, state);
|
|
48401
|
+
return state;
|
|
48402
|
+
};
|
|
48403
|
+
const forgetAdapterState = (t, s) => {
|
|
48404
|
+
adapterStates.delete(`${s}:${t}`);
|
|
48405
|
+
};
|
|
48406
|
+
const isRegistered = (s, t) => {
|
|
48407
|
+
if (!isAdapterTarget(t))
|
|
48408
|
+
return getRegisteredHooksForTarget(s, t).includes(shortName);
|
|
48409
|
+
const state = adapterStateFor(t, s);
|
|
48410
|
+
if (state.status === "unverified") {
|
|
48411
|
+
if (state.absent && target === "all") {
|
|
48412
|
+
const note = `${t}: skipped, ${state.reason}`;
|
|
48413
|
+
if (!notes.includes(note))
|
|
48414
|
+
notes.push(note);
|
|
48415
|
+
return false;
|
|
48416
|
+
}
|
|
48417
|
+
const reason2 = `${t} (${s} scope): its config could not be read \u2014 ${state.reason}`;
|
|
48418
|
+
if (!unreadable.includes(reason2))
|
|
48419
|
+
unreadable.push(reason2);
|
|
48420
|
+
return false;
|
|
48421
|
+
}
|
|
48422
|
+
return adapterStateCarries(state, shortName);
|
|
48423
|
+
};
|
|
48424
|
+
const registeredGlobal = configuredTargets.some((t) => isRegistered("global", t));
|
|
48425
|
+
const registeredProject = configuredTargets.some((t) => isRegistered("project", t));
|
|
47433
48426
|
const codewithOnly = (target === "codewith" || target === "all") && codewithHasHookEntry(shortName, scope);
|
|
47434
|
-
if (!custom3 && !bundledMeta && !registeredGlobal && !registeredProject && !codewithOnly) {
|
|
47435
|
-
return { name: shortName, removed: false, source: null, settingsScopes: [], storeDirRemoved: false, pinRemoved: false, dbRecordRemoved: false, registrationsRemaining: [], error: `Hook '${shortName}' not found` };
|
|
48427
|
+
if (!custom3 && !bundledMeta && !registeredGlobal && !registeredProject && !codewithOnly && unreadable.length === 0) {
|
|
48428
|
+
return withNotes({ name: shortName, removed: false, source: null, settingsScopes: [], storeDirRemoved: false, pinRemoved: false, dbRecordRemoved: false, registrationsRemaining: [], error: `Hook '${shortName}' not found` });
|
|
47436
48429
|
}
|
|
47437
|
-
const writableTargets = target === "all" ?
|
|
48430
|
+
const writableTargets = target === "all" ? ALL_REMOVABLE_TARGETS : target === "codewith" ? [] : [target];
|
|
47438
48431
|
const scopes = [];
|
|
48432
|
+
const registrationsRemaining = [];
|
|
48433
|
+
const notRemoved = [];
|
|
48434
|
+
const attemptedStores = new Set;
|
|
47439
48435
|
for (const s of ["global", "project"]) {
|
|
47440
48436
|
for (const t of writableTargets) {
|
|
47441
|
-
if (
|
|
48437
|
+
if (!isRegistered(s, t))
|
|
48438
|
+
continue;
|
|
48439
|
+
if (isAdapterTarget(t)) {
|
|
48440
|
+
const storePath = adapterStorePath(t, adapterOptionsFor(s), shortName);
|
|
48441
|
+
if (attemptedStores.has(storePath))
|
|
48442
|
+
continue;
|
|
48443
|
+
attemptedStores.add(storePath);
|
|
48444
|
+
const before = adapterStateFor(t, s);
|
|
48445
|
+
const disabledInStore = t === "muse" && adapterStateCarries(before, shortName) && !before.registrations.some((entry) => entry.hook === shortName);
|
|
48446
|
+
const outcome = removeFromAdapter(t, shortName, adapterOptionsFor(s));
|
|
48447
|
+
forgetAdapterState(t, s);
|
|
48448
|
+
if (!outcome.removed) {
|
|
48449
|
+
if (!registrationsRemaining.includes(t))
|
|
48450
|
+
registrationsRemaining.push(t);
|
|
48451
|
+
const reason2 = `${t} (${s} scope): ${outcome.reason ?? "the adapter refused the removal"}`;
|
|
48452
|
+
if (!notRemoved.includes(reason2))
|
|
48453
|
+
notRemoved.push(reason2);
|
|
48454
|
+
continue;
|
|
48455
|
+
}
|
|
48456
|
+
if (disabledInStore) {
|
|
48457
|
+
const bundleClause = outcome.bundleRemoved === undefined ? "the plugin was removed from Muse" : outcome.bundleRemoved ? `the plugin and its bundle at ${storePath} were removed` : `the plugin was removed from Muse; no bundle was present at ${storePath}`;
|
|
48458
|
+
const note = `${t} (${s} scope): '${shortName}' is in Muse's store but is not enabled now, and Hooks cannot tell whether it ran; ${bundleClause}.`;
|
|
48459
|
+
if (!notes.includes(note))
|
|
48460
|
+
notes.push(note);
|
|
48461
|
+
} else if (outcome.reason) {
|
|
48462
|
+
const note = `${t} (${s} scope): ${outcome.reason}`;
|
|
48463
|
+
if (!notes.includes(note))
|
|
48464
|
+
notes.push(note);
|
|
48465
|
+
}
|
|
48466
|
+
} else {
|
|
47442
48467
|
unregisterHook(shortName, s, t);
|
|
47443
|
-
if (!scopes.includes(s))
|
|
47444
|
-
scopes.push(s);
|
|
47445
48468
|
}
|
|
48469
|
+
if (!scopes.includes(s))
|
|
48470
|
+
scopes.push(s);
|
|
47446
48471
|
}
|
|
47447
48472
|
}
|
|
47448
|
-
const registrationsRemaining = [];
|
|
47449
48473
|
if (target === "codewith" || target === "all") {
|
|
47450
48474
|
const codewithPath = getSettingsPath(scope, "codewith");
|
|
47451
|
-
if (
|
|
47452
|
-
const before =
|
|
48475
|
+
if (existsSync14(codewithPath)) {
|
|
48476
|
+
const before = readFileSync13(codewithPath, "utf-8");
|
|
47453
48477
|
const after = removeCodewithHookEntry(before, shortName);
|
|
47454
48478
|
if (after.removed) {
|
|
47455
|
-
|
|
48479
|
+
writeFileSync10(codewithPath, after.text, "utf-8");
|
|
47456
48480
|
}
|
|
47457
48481
|
if (codewithHasHookEntryInText(after.removed ? after.text : before, shortName)) {
|
|
47458
48482
|
registrationsRemaining.push("codewith");
|
|
47459
48483
|
}
|
|
47460
48484
|
}
|
|
47461
48485
|
}
|
|
48486
|
+
const blockers = [...unreadable, ...notRemoved];
|
|
48487
|
+
if (blockers.length > 0) {
|
|
48488
|
+
return withNotes({
|
|
48489
|
+
name: shortName,
|
|
48490
|
+
removed: false,
|
|
48491
|
+
source: custom3 ? "custom" : bundledMeta ? "bundled" : "registered-only",
|
|
48492
|
+
settingsScopes: scopes,
|
|
48493
|
+
storeDirRemoved: false,
|
|
48494
|
+
pinRemoved: false,
|
|
48495
|
+
dbRecordRemoved: false,
|
|
48496
|
+
registrationsRemaining,
|
|
48497
|
+
error: `Hook '${shortName}' was not fully uninstalled: ${blockers.join("; ")}. Its store, pin and DB record were preserved because that registration could still run; fix the named cause and retry.`
|
|
48498
|
+
});
|
|
48499
|
+
}
|
|
47462
48500
|
if (custom3) {
|
|
47463
48501
|
try {
|
|
47464
48502
|
removeStoreDirFailClosed(customHookDir(shortName));
|
|
47465
48503
|
} catch {
|
|
47466
|
-
return {
|
|
48504
|
+
return withNotes({
|
|
47467
48505
|
name: shortName,
|
|
47468
48506
|
removed: false,
|
|
47469
48507
|
source: "custom",
|
|
@@ -47473,11 +48511,11 @@ function uninstallHook(name, scope = "global", target = "claude") {
|
|
|
47473
48511
|
dbRecordRemoved: false,
|
|
47474
48512
|
registrationsRemaining,
|
|
47475
48513
|
error: `Hook '${shortName}' store directory could not be removed; trust records preserved (no fail-open)`
|
|
47476
|
-
};
|
|
48514
|
+
});
|
|
47477
48515
|
}
|
|
47478
48516
|
}
|
|
47479
48517
|
const { removedPin, removedRecord } = removeHookFromStore(shortName);
|
|
47480
|
-
return {
|
|
48518
|
+
return withNotes({
|
|
47481
48519
|
name: shortName,
|
|
47482
48520
|
removed: true,
|
|
47483
48521
|
source: custom3 ? "custom" : bundledMeta ? "bundled" : "registered-only",
|
|
@@ -47486,10 +48524,10 @@ function uninstallHook(name, scope = "global", target = "claude") {
|
|
|
47486
48524
|
pinRemoved: removedPin,
|
|
47487
48525
|
dbRecordRemoved: removedRecord,
|
|
47488
48526
|
registrationsRemaining
|
|
47489
|
-
};
|
|
48527
|
+
});
|
|
47490
48528
|
}
|
|
47491
48529
|
// src/index.ts
|
|
47492
|
-
import { readFileSync as
|
|
48530
|
+
import { readFileSync as readFileSync32 } from "fs";
|
|
47493
48531
|
init_profiles();
|
|
47494
48532
|
init_config();
|
|
47495
48533
|
|
|
@@ -47631,15 +48669,15 @@ init_manifest();
|
|
|
47631
48669
|
// src/lib/custom-install.ts
|
|
47632
48670
|
init_config();
|
|
47633
48671
|
init_manifest();
|
|
47634
|
-
import { existsSync as
|
|
47635
|
-
import { join as
|
|
47636
|
-
import { tmpdir } from "os";
|
|
48672
|
+
import { existsSync as existsSync15, mkdirSync as mkdirSync13, mkdtempSync as mkdtempSync2, readFileSync as readFileSync20, rmSync as rmSync5 } from "fs";
|
|
48673
|
+
import { join as join34 } from "path";
|
|
48674
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
47637
48675
|
function manifestAt(dir) {
|
|
47638
|
-
const path =
|
|
47639
|
-
if (!
|
|
48676
|
+
const path = join34(dir, "manifest.json");
|
|
48677
|
+
if (!existsSync15(path))
|
|
47640
48678
|
return;
|
|
47641
48679
|
try {
|
|
47642
|
-
return parseManifest(
|
|
48680
|
+
return parseManifest(readFileSync20(path, "utf-8"));
|
|
47643
48681
|
} catch {
|
|
47644
48682
|
return;
|
|
47645
48683
|
}
|
|
@@ -47658,8 +48696,8 @@ function writeFromManifest(manifest, srcDir, source, kind) {
|
|
|
47658
48696
|
return { name, version: manifest.version, kind, source, dir, scriptPath };
|
|
47659
48697
|
}
|
|
47660
48698
|
async function cloneToTemp(gitUrl) {
|
|
47661
|
-
const tmp = mkdtempSync2(
|
|
47662
|
-
const proc = Bun.spawn(["git", "clone", "--depth", "1", "--quiet", gitUrl,
|
|
48699
|
+
const tmp = mkdtempSync2(join34(tmpdir2(), "hooks-install-"));
|
|
48700
|
+
const proc = Bun.spawn(["git", "clone", "--depth", "1", "--quiet", gitUrl, join34(tmp, "repo")], {
|
|
47663
48701
|
stdout: "pipe",
|
|
47664
48702
|
stderr: "pipe"
|
|
47665
48703
|
});
|
|
@@ -47672,7 +48710,7 @@ async function cloneToTemp(gitUrl) {
|
|
|
47672
48710
|
rmSync5(tmp, { recursive: true, force: true });
|
|
47673
48711
|
throw new Error(`git clone failed (${exitCode}): ${stderr.trim()}`);
|
|
47674
48712
|
}
|
|
47675
|
-
return
|
|
48713
|
+
return join34(tmp, "repo");
|
|
47676
48714
|
}
|
|
47677
48715
|
function isGitUrl(value) {
|
|
47678
48716
|
return value.startsWith("git@") || value.startsWith("ssh://") || value.startsWith("file://") || value.endsWith(".git");
|
|
@@ -47682,7 +48720,7 @@ function isCustomSource(value) {
|
|
|
47682
48720
|
return true;
|
|
47683
48721
|
if (value.startsWith("http://") || value.startsWith("https://"))
|
|
47684
48722
|
return true;
|
|
47685
|
-
return
|
|
48723
|
+
return existsSync15(value);
|
|
47686
48724
|
}
|
|
47687
48725
|
async function installCustomSource(source) {
|
|
47688
48726
|
if (isGitUrl(source)) {
|
|
@@ -47696,11 +48734,11 @@ async function installCustomSource(source) {
|
|
|
47696
48734
|
if (source.startsWith("http://") || source.startsWith("https://")) {
|
|
47697
48735
|
return installManifestUrl(source);
|
|
47698
48736
|
}
|
|
47699
|
-
if (!
|
|
48737
|
+
if (!existsSync15(source)) {
|
|
47700
48738
|
throw new Error(`No such local path: ${source}`);
|
|
47701
48739
|
}
|
|
47702
48740
|
if (source.endsWith("manifest.json")) {
|
|
47703
|
-
const manifest = parseManifest(
|
|
48741
|
+
const manifest = parseManifest(readFileSync20(source, "utf-8"));
|
|
47704
48742
|
return writeFromManifest(manifest, source.substring(0, source.lastIndexOf("/")), source, "local");
|
|
47705
48743
|
}
|
|
47706
48744
|
return copyManifestDir(source, source, "local");
|
|
@@ -47731,9 +48769,9 @@ async function installManifestUrl(url2) {
|
|
|
47731
48769
|
}
|
|
47732
48770
|
// src/lib/sync.ts
|
|
47733
48771
|
init_config();
|
|
47734
|
-
import { createHash as createHash18, randomUUID as
|
|
47735
|
-
import { readFileSync as
|
|
47736
|
-
import { dirname as
|
|
48772
|
+
import { createHash as createHash18, randomUUID as randomUUID7 } from "crypto";
|
|
48773
|
+
import { readFileSync as readFileSync21, lstatSync as lstatSync18, mkdirSync as mkdirSync14, writeFileSync as writeFileSync11, accessSync as accessSync3, constants as constants5, existsSync as existsSync16 } from "fs";
|
|
48774
|
+
import { dirname as dirname27, join as join35, resolve as resolve30, sep as sep16 } from "path";
|
|
47737
48775
|
init_db();
|
|
47738
48776
|
init_store();
|
|
47739
48777
|
init_manifest();
|
|
@@ -47742,7 +48780,7 @@ function sha256OfText(text3) {
|
|
|
47742
48780
|
return createHash18("sha256").update(text3).digest("hex");
|
|
47743
48781
|
}
|
|
47744
48782
|
function sha256OfPath(path) {
|
|
47745
|
-
return createHash18("sha256").update(
|
|
48783
|
+
return createHash18("sha256").update(readFileSync21(path)).digest("hex");
|
|
47746
48784
|
}
|
|
47747
48785
|
function collectBundledCatalog() {
|
|
47748
48786
|
const out = [];
|
|
@@ -47902,7 +48940,7 @@ function prepareRegistryCommit(staged) {
|
|
|
47902
48940
|
if (!lstatSync18(path).isFile())
|
|
47903
48941
|
throw new Error("REGISTRY_SYNC_DESTINATION_INVALID");
|
|
47904
48942
|
accessSync3(path, constants5.W_OK);
|
|
47905
|
-
originals.set(path,
|
|
48943
|
+
originals.set(path, readFileSync21(path));
|
|
47906
48944
|
} catch (error61) {
|
|
47907
48945
|
if (error61.code !== "ENOENT")
|
|
47908
48946
|
throw error61;
|
|
@@ -47913,10 +48951,10 @@ function prepareRegistryCommit(staged) {
|
|
|
47913
48951
|
const script = resolve30(root, item.scriptRel);
|
|
47914
48952
|
if (script !== root && !script.startsWith(root + sep16))
|
|
47915
48953
|
throw new ScriptContainmentError(script, root, item.scriptRel);
|
|
47916
|
-
const manifest =
|
|
48954
|
+
const manifest = join35(root, "manifest.json");
|
|
47917
48955
|
if (script === manifest || script === root)
|
|
47918
48956
|
throw new Error("REGISTRY_SYNC_DESTINATION_INVALID");
|
|
47919
|
-
for (let path =
|
|
48957
|
+
for (let path = dirname27(script);; path = dirname27(path)) {
|
|
47920
48958
|
try {
|
|
47921
48959
|
if (!lstatSync18(path).isDirectory())
|
|
47922
48960
|
throw new Error("REGISTRY_SYNC_DESTINATION_INVALID");
|
|
@@ -47928,24 +48966,24 @@ function prepareRegistryCommit(staged) {
|
|
|
47928
48966
|
if (path === root)
|
|
47929
48967
|
break;
|
|
47930
48968
|
}
|
|
47931
|
-
if (
|
|
48969
|
+
if (existsSync16(root))
|
|
47932
48970
|
assertContained(script, root, item.scriptRel);
|
|
47933
48971
|
inspectFile(script);
|
|
47934
48972
|
inspectFile(manifest);
|
|
47935
48973
|
}
|
|
47936
48974
|
inspectFile(getLockPath());
|
|
47937
48975
|
if (originals.size) {
|
|
47938
|
-
const recovery =
|
|
47939
|
-
|
|
48976
|
+
const recovery = join35(getHooksDataDir(), "registry-preservation", randomUUID7());
|
|
48977
|
+
mkdirSync14(recovery, { recursive: true, mode: 448 });
|
|
47940
48978
|
const receipt = [];
|
|
47941
48979
|
for (const [target, bytes] of originals) {
|
|
47942
|
-
const path =
|
|
47943
|
-
|
|
47944
|
-
if (!
|
|
48980
|
+
const path = join35(recovery, `${receipt.length}.original`);
|
|
48981
|
+
writeFileSync11(path, bytes, { flag: "wx", mode: 384 });
|
|
48982
|
+
if (!readFileSync21(path).equals(bytes))
|
|
47945
48983
|
throw new Error("REGISTRY_PRESERVATION_READBACK_FAILED");
|
|
47946
48984
|
receipt.push({ target, preserved: path, sha256: createHash18("sha256").update(bytes).digest("hex"), readback: true });
|
|
47947
48985
|
}
|
|
47948
|
-
|
|
48986
|
+
writeFileSync11(join35(recovery, "receipt.json"), JSON.stringify(receipt, null, 2) + `
|
|
47949
48987
|
`, { flag: "wx", mode: 384 });
|
|
47950
48988
|
}
|
|
47951
48989
|
for (const item of staged)
|
|
@@ -48140,8 +49178,8 @@ async function installPinnedFromBundled(name, version3) {
|
|
|
48140
49178
|
// src/serve.ts
|
|
48141
49179
|
init_manifest();
|
|
48142
49180
|
init_store();
|
|
48143
|
-
import { readFileSync as
|
|
48144
|
-
import { dirname as
|
|
49181
|
+
import { readFileSync as readFileSync23 } from "fs";
|
|
49182
|
+
import { dirname as dirname29, join as join39 } from "path";
|
|
48145
49183
|
import { fileURLToPath as fileURLToPath5 } from "url";
|
|
48146
49184
|
|
|
48147
49185
|
// src/lib/secure-compare.ts
|
|
@@ -49487,8 +50525,8 @@ var SERVE_HOST = "127.0.0.1";
|
|
|
49487
50525
|
var SERVE_SERVICE_NAME = "hooks-registry";
|
|
49488
50526
|
function packageVersion() {
|
|
49489
50527
|
try {
|
|
49490
|
-
const pkgPath =
|
|
49491
|
-
return JSON.parse(
|
|
50528
|
+
const pkgPath = join39(dirname29(fileURLToPath5(import.meta.url)), "..", "package.json");
|
|
50529
|
+
return JSON.parse(readFileSync23(pkgPath, "utf-8")).version || "0.0.0";
|
|
49492
50530
|
} catch {
|
|
49493
50531
|
return "0.0.0";
|
|
49494
50532
|
}
|
|
@@ -49747,16 +50785,16 @@ async function provisionCloudflareResources(options) {
|
|
|
49747
50785
|
return result;
|
|
49748
50786
|
}
|
|
49749
50787
|
// src/lib/codex-safety-trust.ts
|
|
49750
|
-
import { createHash as createHash31, randomUUID as
|
|
49751
|
-
import { constants as constants21, closeSync as closeSync15, fsyncSync as fsyncSync5, fstatSync as fstatSync15, lstatSync as lstatSync33, mkdirSync as
|
|
49752
|
-
import { dirname as
|
|
49753
|
-
import { homedir as
|
|
50788
|
+
import { createHash as createHash31, randomUUID as randomUUID8 } from "crypto";
|
|
50789
|
+
import { constants as constants21, closeSync as closeSync15, fsyncSync as fsyncSync5, fstatSync as fstatSync15, lstatSync as lstatSync33, mkdirSync as mkdirSync16, openSync as openSync15, readFileSync as readFileSync29, realpathSync as realpathSync16, writeFileSync as writeFileSync13 } from "fs";
|
|
50790
|
+
import { dirname as dirname33, isAbsolute as isAbsolute29, join as join42, relative as relative2 } from "path";
|
|
50791
|
+
import { homedir as homedir10 } from "os";
|
|
49754
50792
|
|
|
49755
50793
|
// src/lib/codex-safety-check.ts
|
|
49756
50794
|
import { spawn as spawn2, execFile as execFile2 } from "child_process";
|
|
49757
50795
|
import { createHash as createHash25 } from "crypto";
|
|
49758
|
-
import { constants as constants16, openSync as openSync10, closeSync as closeSync10, fstatSync as fstatSync10, lstatSync as lstatSync29, readFileSync as
|
|
49759
|
-
import { dirname as
|
|
50796
|
+
import { constants as constants16, openSync as openSync10, closeSync as closeSync10, fstatSync as fstatSync10, lstatSync as lstatSync29, readFileSync as readFileSync27, realpathSync as realpathSync14 } from "fs";
|
|
50797
|
+
import { dirname as dirname31, isAbsolute as isAbsolute17, resolve as resolve33 } from "path";
|
|
49760
50798
|
import { promisify } from "util";
|
|
49761
50799
|
|
|
49762
50800
|
// src/lib/native-child-env.ts
|
|
@@ -49803,9 +50841,9 @@ function nativeChildEnv(source, explicit = {}, inherit = () => false) {
|
|
|
49803
50841
|
|
|
49804
50842
|
// src/native-safety.ts
|
|
49805
50843
|
import { execFile } from "child_process";
|
|
49806
|
-
import { closeSync as closeSync8, constants as constants9, existsSync as
|
|
49807
|
-
import { homedir as
|
|
49808
|
-
import { dirname as
|
|
50844
|
+
import { closeSync as closeSync8, constants as constants9, existsSync as existsSync17, fstatSync as fstatSync8, fsyncSync, linkSync, lstatSync as lstatSync26, mkdirSync as mkdirSync15, openSync as openSync8, readFileSync as readFileSync25, realpathSync as realpathSync11, renameSync as renameSync7, unlinkSync as unlinkSync8, writeFileSync as writeFileSync12 } from "fs";
|
|
50845
|
+
import { homedir as homedir9 } from "os";
|
|
50846
|
+
import { dirname as dirname30, isAbsolute as isAbsolute14, join as join41, resolve as resolve31 } from "path";
|
|
49809
50847
|
class NativeSafetyError extends Error {
|
|
49810
50848
|
code;
|
|
49811
50849
|
reason;
|
|
@@ -49819,11 +50857,11 @@ class NativeSafetyError extends Error {
|
|
|
49819
50857
|
var executionSchema = "hasna.hooks.execution.v1";
|
|
49820
50858
|
var owner2 = (uid2) => uid2 === 0 || uid2 === process.getuid?.();
|
|
49821
50859
|
function safeDirectory(path) {
|
|
49822
|
-
for (let cursor = path;; cursor =
|
|
50860
|
+
for (let cursor = path;; cursor = dirname30(cursor)) {
|
|
49823
50861
|
const stat2 = lstatSync26(cursor);
|
|
49824
50862
|
if (!stat2.isDirectory() || !owner2(stat2.uid) || stat2.mode & 18)
|
|
49825
50863
|
throw new NativeSafetyError("binding_invalid");
|
|
49826
|
-
if (
|
|
50864
|
+
if (dirname30(cursor) === cursor)
|
|
49827
50865
|
return;
|
|
49828
50866
|
}
|
|
49829
50867
|
}
|
|
@@ -49875,7 +50913,7 @@ async function evaluateRecord(event, record6, signal, executionAgent) {
|
|
|
49875
50913
|
} catch {}
|
|
49876
50914
|
throw new NativeSafetyError("guard_failed");
|
|
49877
50915
|
}
|
|
49878
|
-
async function verifyNativeSafetyCommand(command, home =
|
|
50916
|
+
async function verifyNativeSafetyCommand(command, home = homedir9(), guard = "trash-guard") {
|
|
49879
50917
|
if (guard !== "trash-guard" && guard !== "workspace-repos-guard")
|
|
49880
50918
|
throw new NativeSafetyError("input_invalid");
|
|
49881
50919
|
home = canonicalHome(home);
|
|
@@ -49883,7 +50921,7 @@ async function verifyNativeSafetyCommand(command, home = homedir8(), guard = "tr
|
|
|
49883
50921
|
const binding = nativeSafetyCommandBinding(command);
|
|
49884
50922
|
if (binding?.name !== guard || binding.home !== home)
|
|
49885
50923
|
throw new NativeSafetyError("binding_invalid");
|
|
49886
|
-
const root =
|
|
50924
|
+
const root = join41(home, ".hasna", "repos", "clones"), probe2 = join41(root, ".hasna-workspace-repos-verification-only");
|
|
49887
50925
|
const verdict2 = await evaluateRecord({
|
|
49888
50926
|
hook_event_name: "PreToolUse",
|
|
49889
50927
|
tool_name: "Bash",
|
|
@@ -49936,17 +50974,17 @@ function executable2(command) {
|
|
|
49936
50974
|
need11(found, "native_unavailable");
|
|
49937
50975
|
try {
|
|
49938
50976
|
const path = realpathSync14(found);
|
|
49939
|
-
for (let parent =
|
|
50977
|
+
for (let parent = dirname31(path);; parent = dirname31(parent)) {
|
|
49940
50978
|
const stat2 = lstatSync29(parent);
|
|
49941
50979
|
need11(stat2.isDirectory() && [0, process.getuid?.()].includes(stat2.uid) && !(stat2.mode & 18), "native_unsafe_executable");
|
|
49942
|
-
if (parent ===
|
|
50980
|
+
if (parent === dirname31(parent))
|
|
49943
50981
|
break;
|
|
49944
50982
|
}
|
|
49945
50983
|
const fd = openSync10(path, constants16.O_RDONLY | constants16.O_NOFOLLOW | constants16.O_NONBLOCK);
|
|
49946
50984
|
try {
|
|
49947
50985
|
const stat2 = fstatSync10(fd);
|
|
49948
50986
|
need11(stat2.isFile() && [0, process.getuid?.()].includes(stat2.uid) && !(stat2.mode & 18) && !!(stat2.mode & 64) && stat2.size <= CODEX_EXECUTABLE_MAX_BYTES, "native_unsafe_executable");
|
|
49949
|
-
const bytes =
|
|
50987
|
+
const bytes = readFileSync27(fd);
|
|
49950
50988
|
need11(bytes.length <= CODEX_EXECUTABLE_MAX_BYTES, "native_unavailable");
|
|
49951
50989
|
need11(nativeHeader(bytes), "native_executable_required");
|
|
49952
50990
|
return { path, sha256: digest4(bytes) };
|
|
@@ -50081,9 +51119,9 @@ function supportedDiscoveryWarnings(hooks, warnings) {
|
|
|
50081
51119
|
const sources = new Set(hooks.map((hook) => JSON.stringify([hook?.source, hook?.sourcePath])));
|
|
50082
51120
|
for (const hook of hooks) {
|
|
50083
51121
|
const path = hook?.sourcePath;
|
|
50084
|
-
if (typeof path !== "string" || path.length > 4096 || !isAbsolute17(path) || path !== resolve33(
|
|
51122
|
+
if (typeof path !== "string" || path.length > 4096 || !isAbsolute17(path) || path !== resolve33(dirname31(path), "hooks.json") || !["user", "project"].includes(hook.source))
|
|
50085
51123
|
continue;
|
|
50086
|
-
const toml = resolve33(
|
|
51124
|
+
const toml = resolve33(dirname31(path), "config.toml");
|
|
50087
51125
|
if (sources.has(JSON.stringify([hook.source, toml]))) {
|
|
50088
51126
|
allowed.add(`loading hooks from both ${path} and ${toml}; prefer a single representation for this layer`);
|
|
50089
51127
|
}
|
|
@@ -50199,20 +51237,20 @@ function mergedConfiguration(value) {
|
|
|
50199
51237
|
return canonical9(copy);
|
|
50200
51238
|
}
|
|
50201
51239
|
function safeDirectory2(path) {
|
|
50202
|
-
for (let p = path;; p =
|
|
51240
|
+
for (let p = path;; p = dirname33(p)) {
|
|
50203
51241
|
const s = lstatSync33(p);
|
|
50204
51242
|
need13(s.isDirectory() && [0, process.getuid?.()].includes(s.uid) && !(s.mode & 18), "preimage_unsafe");
|
|
50205
|
-
if (
|
|
51243
|
+
if (dirname33(p) === p)
|
|
50206
51244
|
return;
|
|
50207
51245
|
}
|
|
50208
51246
|
}
|
|
50209
51247
|
function readPrivate(path) {
|
|
50210
|
-
safeDirectory2(
|
|
51248
|
+
safeDirectory2(dirname33(path));
|
|
50211
51249
|
const fd = openSync15(path, constants21.O_RDONLY | constants21.O_NOFOLLOW | constants21.O_NONBLOCK);
|
|
50212
51250
|
try {
|
|
50213
51251
|
const s = fstatSync15(fd);
|
|
50214
51252
|
need13(s.isFile() && s.uid === process.getuid?.() && !(s.mode & 63) && s.size <= 4194304, "preimage_unsafe");
|
|
50215
|
-
const bytes =
|
|
51253
|
+
const bytes = readFileSync29(fd);
|
|
50216
51254
|
need13(bytes.length <= 4194304, "preimage_unsafe");
|
|
50217
51255
|
return bytes;
|
|
50218
51256
|
} finally {
|
|
@@ -50230,12 +51268,12 @@ function syncDirectory(path) {
|
|
|
50230
51268
|
function exclusive(path, bytes) {
|
|
50231
51269
|
const fd = openSync15(path, constants21.O_CREAT | constants21.O_EXCL | constants21.O_WRONLY | constants21.O_NOFOLLOW, 384);
|
|
50232
51270
|
try {
|
|
50233
|
-
|
|
51271
|
+
writeFileSync13(fd, bytes);
|
|
50234
51272
|
fsyncSync5(fd);
|
|
50235
51273
|
} finally {
|
|
50236
51274
|
closeSync15(fd);
|
|
50237
51275
|
}
|
|
50238
|
-
syncDirectory(
|
|
51276
|
+
syncDirectory(dirname33(path));
|
|
50239
51277
|
}
|
|
50240
51278
|
async function inspect(rpc, home, cwd, readiness = false, selected = "trash-guard") {
|
|
50241
51279
|
const observed = await readCodexSafety(rpc, home, cwd, { includeLayers: true, requireReady: false, readiness, guard: selected });
|
|
@@ -50273,7 +51311,7 @@ async function inspect(rpc, home, cwd, readiness = false, selected = "trash-guar
|
|
|
50273
51311
|
}
|
|
50274
51312
|
async function context2(options, dependencies) {
|
|
50275
51313
|
need13(options.readiness ? options.guard === undefined : options.guard === undefined || isNativeSafetyName(options.guard), "input_invalid");
|
|
50276
|
-
const home = realpathSync16(options.home ??
|
|
51314
|
+
const home = realpathSync16(options.home ?? homedir10()), cwd = realpathSync16(options.cwd ?? process.cwd());
|
|
50277
51315
|
need13(isAbsolute29(home) && isAbsolute29(cwd), "input_invalid");
|
|
50278
51316
|
return { home, cwd, rpc: await (dependencies.connect ?? connectCodexSafety)({ ...options, home }) };
|
|
50279
51317
|
}
|
|
@@ -50316,21 +51354,21 @@ async function applyTrust(options, dependencies = {}) {
|
|
|
50316
51354
|
const expectedGuard = expectedDiscovery.data[0].hooks.find((h) => h.key === before.guard.key);
|
|
50317
51355
|
expectedGuard.enabled = true;
|
|
50318
51356
|
expectedGuard.trustStatus = "trusted";
|
|
50319
|
-
operationId =
|
|
51357
|
+
operationId = randomUUID8();
|
|
50320
51358
|
let directory3 = home;
|
|
50321
51359
|
for (const part of [".hasna", "hooks", "native", selected.readiness ? "codex-readiness-trust" : (selected.guard ?? "trash-guard") === "trash-guard" ? "codex-trash-trust" : "codex-" + selected.guard + "-trust", operationId]) {
|
|
50322
|
-
directory3 =
|
|
51360
|
+
directory3 = join42(directory3, part);
|
|
50323
51361
|
try {
|
|
50324
|
-
|
|
50325
|
-
syncDirectory(
|
|
51362
|
+
mkdirSync16(directory3, { mode: 448 });
|
|
51363
|
+
syncDirectory(dirname33(directory3));
|
|
50326
51364
|
} catch (error61) {
|
|
50327
51365
|
if (error61.code !== "EEXIST")
|
|
50328
51366
|
throw error61;
|
|
50329
51367
|
}
|
|
50330
51368
|
safeDirectory2(directory3);
|
|
50331
51369
|
}
|
|
50332
|
-
exclusive(
|
|
50333
|
-
exclusive(
|
|
51370
|
+
exclusive(join42(directory3, "config.before"), before.before);
|
|
51371
|
+
exclusive(join42(directory3, "intent.json"), JSON.stringify({ operationId, plan: before.plan, configSHA256: sha7(before.before) }) + `
|
|
50334
51372
|
`);
|
|
50335
51373
|
journal = directory3;
|
|
50336
51374
|
need13(sha7(readPrivate(before.canonicalPath)) === sha7(before.before));
|
|
@@ -50358,7 +51396,7 @@ async function applyTrust(options, dependencies = {}) {
|
|
|
50358
51396
|
nativeAdoptionVerified: false,
|
|
50359
51397
|
unrelatedConfigurationPreserved: true
|
|
50360
51398
|
};
|
|
50361
|
-
exclusive(
|
|
51399
|
+
exclusive(join42(directory3, "receipt.json"), JSON.stringify(receipt) + `
|
|
50362
51400
|
`);
|
|
50363
51401
|
return receipt;
|
|
50364
51402
|
} catch (error61) {
|
|
@@ -50371,7 +51409,7 @@ async function applyTrust(options, dependencies = {}) {
|
|
|
50371
51409
|
if (!journal)
|
|
50372
51410
|
throw new CodexTrustError("trust_refused", undefined, refusal3);
|
|
50373
51411
|
try {
|
|
50374
|
-
exclusive(
|
|
51412
|
+
exclusive(join42(journal, "refused.json"), JSON.stringify({ ok: false, outcome: "refused", operationId, planDigest: selected.expectedPlanDigest, refusal: refusal3 }) + `
|
|
50375
51413
|
`);
|
|
50376
51414
|
} catch {
|
|
50377
51415
|
throw new CodexTrustError("trust_outcome_uncertain", operationId);
|
|
@@ -50384,10 +51422,10 @@ async function applyTrust(options, dependencies = {}) {
|
|
|
50384
51422
|
var planCodexSafetyTrust = (options = {}, dependencies = {}) => planTrust({ ...options, readiness: false }, dependencies);
|
|
50385
51423
|
var applyCodexSafetyTrust = (options, dependencies = {}) => applyTrust({ ...options, readiness: false }, dependencies);
|
|
50386
51424
|
// src/lib/claude-safety-check.ts
|
|
50387
|
-
import { constants as constants23, closeSync as closeSync20, fstatSync as fstatSync20, lstatSync as lstatSync35, openSync as openSync20, readFileSync as
|
|
51425
|
+
import { constants as constants23, closeSync as closeSync20, fstatSync as fstatSync20, lstatSync as lstatSync35, openSync as openSync20, readFileSync as readFileSync30, realpathSync as realpathSync17 } from "fs";
|
|
50388
51426
|
import { createHash as createHash33 } from "crypto";
|
|
50389
|
-
import { dirname as
|
|
50390
|
-
import { homedir as
|
|
51427
|
+
import { dirname as dirname35, isAbsolute as isAbsolute31, join as join43, resolve as resolve35 } from "path";
|
|
51428
|
+
import { homedir as homedir11 } from "os";
|
|
50391
51429
|
class ClaudeSafetyError extends Error {
|
|
50392
51430
|
code;
|
|
50393
51431
|
constructor(code2) {
|
|
@@ -50404,17 +51442,17 @@ var sha9 = (value) => createHash33("sha256").update(value).digest("hex");
|
|
|
50404
51442
|
function read3(path) {
|
|
50405
51443
|
try {
|
|
50406
51444
|
const resolved = realpathSync17(path);
|
|
50407
|
-
for (let parent =
|
|
51445
|
+
for (let parent = dirname35(resolved);; parent = dirname35(parent)) {
|
|
50408
51446
|
const s = lstatSync35(parent);
|
|
50409
51447
|
need14(s.isDirectory() && [0, process.getuid?.()].includes(s.uid) && !(s.mode & 18), "settings_unsafe");
|
|
50410
|
-
if (
|
|
51448
|
+
if (dirname35(parent) === parent)
|
|
50411
51449
|
break;
|
|
50412
51450
|
}
|
|
50413
51451
|
const fd = openSync20(resolved, constants23.O_RDONLY | constants23.O_NOFOLLOW | constants23.O_NONBLOCK);
|
|
50414
51452
|
try {
|
|
50415
51453
|
const s = fstatSync20(fd);
|
|
50416
51454
|
need14(s.isFile() && [0, process.getuid?.()].includes(s.uid) && !(s.mode & 18) && s.size <= 1048576, "settings_unsafe");
|
|
50417
|
-
const raw =
|
|
51455
|
+
const raw = readFileSync30(fd, "utf8");
|
|
50418
51456
|
need14(Buffer.byteLength(raw) <= 1048576, "settings_unsafe");
|
|
50419
51457
|
return { path, resolved, sha256: sha9(raw), raw };
|
|
50420
51458
|
} finally {
|
|
@@ -50429,11 +51467,11 @@ function read3(path) {
|
|
|
50429
51467
|
}
|
|
50430
51468
|
}
|
|
50431
51469
|
async function verifyClaudeSafetyConfiguration(options = {}, dependencies = {}) {
|
|
50432
|
-
const home = realpathSync17(options.home ??
|
|
51470
|
+
const home = realpathSync17(options.home ?? homedir11()), cwd = realpathSync17(options.cwd ?? process.cwd());
|
|
50433
51471
|
const paths = [
|
|
50434
|
-
options.userSettingsPath ??
|
|
50435
|
-
|
|
50436
|
-
options.localSettingsPath ??
|
|
51472
|
+
options.userSettingsPath ?? join43(home, ".claude/settings.json"),
|
|
51473
|
+
join43(cwd, ".claude/settings.json"),
|
|
51474
|
+
options.localSettingsPath ?? join43(cwd, ".claude/settings.local.json")
|
|
50437
51475
|
];
|
|
50438
51476
|
need14(paths.every((path) => isAbsolute31(path) && resolve35(path) === path), "settings_unsafe");
|
|
50439
51477
|
const inspect2 = () => [...new Set(paths)].map(read3);
|
|
@@ -50494,10 +51532,10 @@ async function verifyClaudeSafetyConfiguration(options = {}, dependencies = {})
|
|
|
50494
51532
|
};
|
|
50495
51533
|
}
|
|
50496
51534
|
// src/lib/native-safety-registration.ts
|
|
50497
|
-
import { createHash as createHash35, randomUUID as
|
|
50498
|
-
import { constants as constants24, closeSync as closeSync24, fstatSync as fstatSync24, fsyncSync as fsyncSync8, lstatSync as lstatSync36, mkdirSync as
|
|
50499
|
-
import { basename as
|
|
50500
|
-
import { homedir as
|
|
51535
|
+
import { createHash as createHash35, randomUUID as randomUUID9 } from "crypto";
|
|
51536
|
+
import { constants as constants24, closeSync as closeSync24, fstatSync as fstatSync24, fsyncSync as fsyncSync8, lstatSync as lstatSync36, mkdirSync as mkdirSync17, openSync as openSync24, readFileSync as readFileSync31, realpathSync as realpathSync18, writeFileSync as writeFileSync14 } from "fs";
|
|
51537
|
+
import { basename as basename18, dirname as dirname36, isAbsolute as isAbsolute33, join as join44, relative as relative7, resolve as resolve36 } from "path";
|
|
51538
|
+
import { homedir as homedir12 } from "os";
|
|
50501
51539
|
class NativeRegistrationError extends Error {
|
|
50502
51540
|
code;
|
|
50503
51541
|
operationId;
|
|
@@ -50523,10 +51561,10 @@ function canonical11(value) {
|
|
|
50523
51561
|
return JSON.stringify(value);
|
|
50524
51562
|
}
|
|
50525
51563
|
function safeDirectory3(path) {
|
|
50526
|
-
for (let p = path;; p =
|
|
51564
|
+
for (let p = path;; p = dirname36(p)) {
|
|
50527
51565
|
const s = lstatSync36(p);
|
|
50528
51566
|
need15(s.isDirectory() && [0, process.getuid?.()].includes(s.uid) && !(s.mode & 18), "settings_unsafe");
|
|
50529
|
-
if (
|
|
51567
|
+
if (dirname36(p) === p)
|
|
50530
51568
|
return;
|
|
50531
51569
|
}
|
|
50532
51570
|
}
|
|
@@ -50544,14 +51582,14 @@ function read5(path) {
|
|
|
50544
51582
|
if (missing2.code !== "ENOENT")
|
|
50545
51583
|
throw missing2;
|
|
50546
51584
|
}
|
|
50547
|
-
let ancestor =
|
|
50548
|
-
const suffix = [
|
|
51585
|
+
let ancestor = dirname36(path);
|
|
51586
|
+
const suffix = [basename18(path)];
|
|
50549
51587
|
for (;; ) {
|
|
50550
51588
|
try {
|
|
50551
|
-
resolved =
|
|
51589
|
+
resolved = join44(realpathSync18(ancestor), ...suffix);
|
|
50552
51590
|
break;
|
|
50553
51591
|
} catch (missing2) {
|
|
50554
|
-
if (missing2.code !== "ENOENT" ||
|
|
51592
|
+
if (missing2.code !== "ENOENT" || dirname36(ancestor) === ancestor || suffix.length >= 64)
|
|
50555
51593
|
throw new NativeRegistrationError("settings_unsafe");
|
|
50556
51594
|
try {
|
|
50557
51595
|
lstatSync36(ancestor);
|
|
@@ -50560,14 +51598,14 @@ function read5(path) {
|
|
|
50560
51598
|
if (absent.code !== "ENOENT")
|
|
50561
51599
|
throw absent;
|
|
50562
51600
|
}
|
|
50563
|
-
suffix.unshift(
|
|
50564
|
-
ancestor =
|
|
51601
|
+
suffix.unshift(basename18(ancestor));
|
|
51602
|
+
ancestor = dirname36(ancestor);
|
|
50565
51603
|
}
|
|
50566
51604
|
}
|
|
50567
51605
|
safeDirectory3(realpathSync18(ancestor));
|
|
50568
51606
|
return { resolved, raw: null, sha256: "absent" };
|
|
50569
51607
|
}
|
|
50570
|
-
safeDirectory3(
|
|
51608
|
+
safeDirectory3(dirname36(resolved));
|
|
50571
51609
|
let fd;
|
|
50572
51610
|
try {
|
|
50573
51611
|
fd = openSync24(resolved, constants24.O_RDONLY | constants24.O_NOFOLLOW | constants24.O_NONBLOCK);
|
|
@@ -50579,7 +51617,7 @@ function read5(path) {
|
|
|
50579
51617
|
try {
|
|
50580
51618
|
const s = fstatSync24(fd);
|
|
50581
51619
|
need15(s.isFile() && s.uid === process.getuid?.() && !(s.mode & 18) && s.nlink === 1 && s.size <= 1048576, "settings_unsafe");
|
|
50582
|
-
const bytes =
|
|
51620
|
+
const bytes = readFileSync31(fd);
|
|
50583
51621
|
need15(bytes.length <= 1048576, "settings_unsafe");
|
|
50584
51622
|
const raw = bytes.toString("utf8");
|
|
50585
51623
|
need15(Buffer.from(raw).equals(bytes), "settings_invalid");
|
|
@@ -50655,7 +51693,7 @@ function validateSettings(raw) {
|
|
|
50655
51693
|
function inspect2(options) {
|
|
50656
51694
|
need15(options?.target === "codex" || options?.target === "claude", "input_invalid");
|
|
50657
51695
|
need15(options.readiness ? options.guard === "trash-guard" : isNativeSafetyName(options.guard), "input_invalid");
|
|
50658
|
-
const home = realpathSync18(
|
|
51696
|
+
const home = realpathSync18(homedir12()), path = getSettingsPath("global", options.target);
|
|
50659
51697
|
need15(isAbsolute33(path) && resolve36(path) === path, "input_invalid");
|
|
50660
51698
|
const nativeHome = options.target === "codex" ? process.env.CODEX_HOME : process.env.CLAUDE_CONFIG_DIR;
|
|
50661
51699
|
if (nativeHome !== undefined) {
|
|
@@ -50704,12 +51742,12 @@ function syncDirectory2(path) {
|
|
|
50704
51742
|
function exclusive2(path, bytes) {
|
|
50705
51743
|
const fd = openSync24(path, constants24.O_CREAT | constants24.O_EXCL | constants24.O_WRONLY | constants24.O_NOFOLLOW, 384);
|
|
50706
51744
|
try {
|
|
50707
|
-
|
|
51745
|
+
writeFileSync14(fd, bytes);
|
|
50708
51746
|
fsyncSync8(fd);
|
|
50709
51747
|
} finally {
|
|
50710
51748
|
closeSync24(fd);
|
|
50711
51749
|
}
|
|
50712
|
-
syncDirectory2(
|
|
51750
|
+
syncDirectory2(dirname36(path));
|
|
50713
51751
|
}
|
|
50714
51752
|
function refusalOf(error61) {
|
|
50715
51753
|
const code2 = error61?.code;
|
|
@@ -50717,7 +51755,7 @@ function refusalOf(error61) {
|
|
|
50717
51755
|
}
|
|
50718
51756
|
function refuseApply(directory3, operationId, planDigest, refusal3) {
|
|
50719
51757
|
try {
|
|
50720
|
-
exclusive2(
|
|
51758
|
+
exclusive2(join44(directory3, "refused.json"), JSON.stringify({ ok: false, outcome: "refused", operationId, planDigest, refusal: refusal3 }) + `
|
|
50721
51759
|
`);
|
|
50722
51760
|
} catch {
|
|
50723
51761
|
throw new NativeRegistrationError("apply_outcome_uncertain", operationId);
|
|
@@ -50758,13 +51796,13 @@ async function applyRegistration(options, dependencies = {}) {
|
|
|
50758
51796
|
need15(before.plan.planDigest === plan.planDigest, "settings_changed");
|
|
50759
51797
|
if (plan.action === "unchanged")
|
|
50760
51798
|
return { ok: true, changed: false, ...plan };
|
|
50761
|
-
const operationId =
|
|
51799
|
+
const operationId = randomUUID9();
|
|
50762
51800
|
let directory3 = before.home;
|
|
50763
51801
|
for (const part of [".hasna", "hooks", "native", selected.readiness ? "trash-readiness-registration" : selected.guard === "trash-guard" ? "trash-registration" : selected.guard + "-registration", operationId]) {
|
|
50764
|
-
directory3 =
|
|
51802
|
+
directory3 = join44(directory3, part);
|
|
50765
51803
|
try {
|
|
50766
|
-
|
|
50767
|
-
syncDirectory2(
|
|
51804
|
+
mkdirSync17(directory3, { mode: 448 });
|
|
51805
|
+
syncDirectory2(dirname36(directory3));
|
|
50768
51806
|
} catch (error61) {
|
|
50769
51807
|
if (error61.code !== "EEXIST")
|
|
50770
51808
|
throw new NativeRegistrationError("settings_unsafe");
|
|
@@ -50772,8 +51810,8 @@ async function applyRegistration(options, dependencies = {}) {
|
|
|
50772
51810
|
safeDirectory3(directory3);
|
|
50773
51811
|
}
|
|
50774
51812
|
if (before.before.raw !== null)
|
|
50775
|
-
exclusive2(
|
|
50776
|
-
exclusive2(
|
|
51813
|
+
exclusive2(join44(directory3, "settings.before"), before.before.raw);
|
|
51814
|
+
exclusive2(join44(directory3, "intent.json"), JSON.stringify({ operationId, plan }) + `
|
|
50777
51815
|
`);
|
|
50778
51816
|
let writeMayHaveStarted = false;
|
|
50779
51817
|
const beforeWrite = () => {
|
|
@@ -50802,7 +51840,7 @@ async function applyRegistration(options, dependencies = {}) {
|
|
|
50802
51840
|
...selected.readiness ? { readinessRunnerVerified: true } : {},
|
|
50803
51841
|
nativeAdoptionVerified: false
|
|
50804
51842
|
};
|
|
50805
|
-
exclusive2(
|
|
51843
|
+
exclusive2(join44(directory3, "receipt.json"), JSON.stringify(receipt) + `
|
|
50806
51844
|
`);
|
|
50807
51845
|
return receipt;
|
|
50808
51846
|
} catch (error61) {
|
|
@@ -50838,7 +51876,7 @@ async function runHook(name, input2, options = {}) {
|
|
|
50838
51876
|
if (!script)
|
|
50839
51877
|
throw new Error(`Hook script not found: ${name}`);
|
|
50840
51878
|
const { sha256Of: sha256Of2, checkScriptHash: checkScriptHash2 } = await Promise.resolve().then(() => (init_store(), exports_store));
|
|
50841
|
-
const content =
|
|
51879
|
+
const content = readFileSync32(script);
|
|
50842
51880
|
const check2 = checkScriptHash2(name, sha256Of2(content));
|
|
50843
51881
|
if (!check2.ok) {
|
|
50844
51882
|
throw new Error(`Hook '${name}' script changed since it was trusted (sha256 ${check2.expected} != ${check2.actual}). Run 'hooks trust ${name}' to trust the new content.`);
|