agent-shim 7.12.0 → 7.13.0
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/dist/cli.cjs +445 -424
- package/dist/index.cjs +1471 -140
- package/dist/index.mjs +1468 -140
- package/dist/types/cli/pairs.d.ts +19 -0
- package/dist/types/frontdoor/realFrontDoorPort.d.ts +5 -0
- package/dist/types/frontdoor/trust.d.ts +30 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/launchWiring.d.ts +49 -0
- package/dist/types/launcher/argv.d.ts +42 -0
- package/dist/types/launcher/cliOverride.d.ts +38 -0
- package/dist/types/launcher/extraFlags.d.ts +6 -0
- package/dist/types/launcher/spawn.d.ts +20 -0
- package/dist/types/launcher.d.ts +98 -0
- package/dist/types/realCredentialCache.d.ts +11 -0
- package/dist/types/realPorts.d.ts +8 -1
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -55406,16 +55406,16 @@ function parseJson(raw, schema) {
|
|
|
55406
55406
|
const parsed = schema.safeParse(JSON.parse(raw));
|
|
55407
55407
|
return parsed.success ? parsed.data : void 0;
|
|
55408
55408
|
}
|
|
55409
|
-
function readHeadroomState(
|
|
55409
|
+
function readHeadroomState(fs19, stateFile) {
|
|
55410
55410
|
try {
|
|
55411
|
-
return parseJson(
|
|
55411
|
+
return parseJson(fs19.readFileUtf8(stateFile), HeadroomStateSchema);
|
|
55412
55412
|
} catch {
|
|
55413
55413
|
return void 0;
|
|
55414
55414
|
}
|
|
55415
55415
|
}
|
|
55416
|
-
function writeHeadroomState(
|
|
55417
|
-
|
|
55418
|
-
|
|
55416
|
+
function writeHeadroomState(fs19, stateFile, state) {
|
|
55417
|
+
fs19.mkdirp(import_node_path5.default.dirname(stateFile));
|
|
55418
|
+
fs19.writeFilePrivate(stateFile, `${JSON.stringify(state, null, 2)}
|
|
55419
55419
|
`);
|
|
55420
55420
|
}
|
|
55421
55421
|
function headroomAllowlist(providers) {
|
|
@@ -55435,14 +55435,14 @@ function headroomUpstreams(providers, frontDoorOrigin2) {
|
|
|
55435
55435
|
function hashAllowlist(allowlist) {
|
|
55436
55436
|
return (0, import_node_crypto2.createHash)("sha256").update(allowlist.join("\n")).digest("hex");
|
|
55437
55437
|
}
|
|
55438
|
-
function readAllProviders(
|
|
55438
|
+
function readAllProviders(fs19, providersDir) {
|
|
55439
55439
|
const result = [];
|
|
55440
|
-
for (const name of
|
|
55440
|
+
for (const name of fs19.readdir(providersDir)) {
|
|
55441
55441
|
if (!name.endsWith(".json")) {
|
|
55442
55442
|
continue;
|
|
55443
55443
|
}
|
|
55444
55444
|
try {
|
|
55445
|
-
const parsed = parseJson(
|
|
55445
|
+
const parsed = parseJson(fs19.readFileUtf8(import_node_path5.default.join(providersDir, name)), ProviderSchema);
|
|
55446
55446
|
if (parsed !== void 0) {
|
|
55447
55447
|
result.push({ name: name.slice(0, -".json".length), provider: parsed });
|
|
55448
55448
|
}
|
|
@@ -55452,22 +55452,22 @@ function readAllProviders(fs18, providersDir) {
|
|
|
55452
55452
|
}
|
|
55453
55453
|
return result;
|
|
55454
55454
|
}
|
|
55455
|
-
function writeSession(
|
|
55456
|
-
|
|
55457
|
-
|
|
55455
|
+
function writeSession(fs19, sessionsDir, session) {
|
|
55456
|
+
fs19.mkdirp(sessionsDir);
|
|
55457
|
+
fs19.writeFileUtf8(import_node_path5.default.join(sessionsDir, `${String(session.pid)}.json`), `${JSON.stringify(session, null, 2)}
|
|
55458
55458
|
`);
|
|
55459
55459
|
}
|
|
55460
|
-
function removeSession(
|
|
55461
|
-
|
|
55460
|
+
function removeSession(fs19, sessionsDir, pid) {
|
|
55461
|
+
fs19.removeRecursive(import_node_path5.default.join(sessionsDir, `${String(pid)}.json`));
|
|
55462
55462
|
}
|
|
55463
|
-
function listSessions(
|
|
55463
|
+
function listSessions(fs19, sessionsDir) {
|
|
55464
55464
|
const sessions = [];
|
|
55465
|
-
for (const name of
|
|
55465
|
+
for (const name of fs19.readdir(sessionsDir)) {
|
|
55466
55466
|
if (!name.endsWith(".json")) {
|
|
55467
55467
|
continue;
|
|
55468
55468
|
}
|
|
55469
55469
|
try {
|
|
55470
|
-
const parsed = parseJson(
|
|
55470
|
+
const parsed = parseJson(fs19.readFileUtf8(import_node_path5.default.join(sessionsDir, name)), HeadroomSessionSchema);
|
|
55471
55471
|
if (parsed !== void 0) {
|
|
55472
55472
|
sessions.push(parsed);
|
|
55473
55473
|
}
|
|
@@ -55477,22 +55477,22 @@ function listSessions(fs18, sessionsDir) {
|
|
|
55477
55477
|
}
|
|
55478
55478
|
return sessions.sort((a2, b) => a2.pid - b.pid);
|
|
55479
55479
|
}
|
|
55480
|
-
function sessionsForSupervisor(
|
|
55481
|
-
return listSessions(
|
|
55480
|
+
function sessionsForSupervisor(fs19, sessionsDir, supervisorPid) {
|
|
55481
|
+
return listSessions(fs19, sessionsDir).filter((session) => session.supervisorPid === supervisorPid);
|
|
55482
55482
|
}
|
|
55483
|
-
function pruneDeadSessions(
|
|
55483
|
+
function pruneDeadSessions(fs19, sessionsDir, isRunning) {
|
|
55484
55484
|
const removed = [];
|
|
55485
|
-
for (const session of listSessions(
|
|
55485
|
+
for (const session of listSessions(fs19, sessionsDir)) {
|
|
55486
55486
|
if (!isRunning(session.pid)) {
|
|
55487
|
-
removeSession(
|
|
55487
|
+
removeSession(fs19, sessionsDir, session.pid);
|
|
55488
55488
|
removed.push(session.pid);
|
|
55489
55489
|
}
|
|
55490
55490
|
}
|
|
55491
55491
|
return removed;
|
|
55492
55492
|
}
|
|
55493
|
-
function readStartLock(
|
|
55493
|
+
function readStartLock(fs19, lockFile) {
|
|
55494
55494
|
try {
|
|
55495
|
-
return parseJson(
|
|
55495
|
+
return parseJson(fs19.readFileUtf8(lockFile), HeadroomLockSchema);
|
|
55496
55496
|
} catch {
|
|
55497
55497
|
return void 0;
|
|
55498
55498
|
}
|
|
@@ -55783,10 +55783,12 @@ function detachedDaemonSpawnOptions(logFd, env) {
|
|
|
55783
55783
|
return { detached: true, stdio: ["ignore", logFd, logFd], env };
|
|
55784
55784
|
}
|
|
55785
55785
|
function spawnDetachedSupervisor(paths, subcommand, logPath) {
|
|
55786
|
+
return spawnDetachedDaemon(selfInvocation([subcommand]), paths, subcommand, logPath);
|
|
55787
|
+
}
|
|
55788
|
+
function spawnDetachedDaemon(invocation, paths, subcommand, logPath) {
|
|
55786
55789
|
import_node_fs3.default.mkdirSync(import_node_path6.default.dirname(logPath), { recursive: true });
|
|
55787
55790
|
const logFd = import_node_fs3.default.openSync(logPath, "a");
|
|
55788
55791
|
try {
|
|
55789
|
-
const invocation = selfInvocation([subcommand]);
|
|
55790
55792
|
const child = (0, import_node_child_process2.spawn)(invocation.command, invocation.args, detachedDaemonSpawnOptions(logFd, { ...process.env, AGENT_SHIM_HOME: paths.root }));
|
|
55791
55793
|
child.unref();
|
|
55792
55794
|
if (child.pid === void 0) {
|
|
@@ -55797,8 +55799,8 @@ function spawnDetachedSupervisor(paths, subcommand, logPath) {
|
|
|
55797
55799
|
import_node_fs3.default.closeSync(logFd);
|
|
55798
55800
|
}
|
|
55799
55801
|
}
|
|
55800
|
-
function spawnHeadroomSupervisor(paths) {
|
|
55801
|
-
return
|
|
55802
|
+
function spawnHeadroomSupervisor(paths, spawnDaemon) {
|
|
55803
|
+
return spawnDaemon(paths, "__headroom-supervisor", paths.headroomLogPath);
|
|
55802
55804
|
}
|
|
55803
55805
|
async function realIsPortFree(port) {
|
|
55804
55806
|
return await new Promise((resolve) => {
|
|
@@ -55815,7 +55817,7 @@ async function realIsPortFree(port) {
|
|
|
55815
55817
|
});
|
|
55816
55818
|
});
|
|
55817
55819
|
}
|
|
55818
|
-
function realHeadroomPort(paths) {
|
|
55820
|
+
function realHeadroomPort(paths, options) {
|
|
55819
55821
|
return {
|
|
55820
55822
|
ensure: () => {
|
|
55821
55823
|
const up = ensureHeadroom({
|
|
@@ -55827,12 +55829,12 @@ function realHeadroomPort(paths) {
|
|
|
55827
55829
|
isRunning: realIsProcessRunning,
|
|
55828
55830
|
now: () => Date.now(),
|
|
55829
55831
|
sleep: realSleepSync,
|
|
55830
|
-
spawnSupervisor: spawnHeadroomSupervisor
|
|
55832
|
+
spawnSupervisor: (layout) => spawnHeadroomSupervisor(layout, options.spawnDaemon)
|
|
55831
55833
|
}
|
|
55832
55834
|
});
|
|
55833
55835
|
return {
|
|
55834
55836
|
port: up.port,
|
|
55835
|
-
projectId: resolveGitRoot(realRunPort,
|
|
55837
|
+
projectId: resolveGitRoot(realRunPort, options.cwd) ?? options.cwd
|
|
55836
55838
|
};
|
|
55837
55839
|
},
|
|
55838
55840
|
release: () => {
|
|
@@ -57521,8 +57523,8 @@ function decideConfigProfile(params) {
|
|
|
57521
57523
|
}
|
|
57522
57524
|
return { source: "none" };
|
|
57523
57525
|
}
|
|
57524
|
-
function loadIdentity(identitiesDir, name,
|
|
57525
|
-
return loadConfigFile(import_node_path8.default.join(identitiesDir, name, "identity.json"), IdentitySchema,
|
|
57526
|
+
function loadIdentity(identitiesDir, name, fs19) {
|
|
57527
|
+
return loadConfigFile(import_node_path8.default.join(identitiesDir, name, "identity.json"), IdentitySchema, fs19.readConfigFile);
|
|
57526
57528
|
}
|
|
57527
57529
|
|
|
57528
57530
|
// src/configProfilesStore.ts
|
|
@@ -59109,8 +59111,8 @@ var FarmManifestSchema = external_exports.strictObject({
|
|
|
59109
59111
|
materialised: external_exports.array(external_exports.string()),
|
|
59110
59112
|
links: external_exports.array(external_exports.strictObject({ rel: external_exports.string(), target: external_exports.string() }))
|
|
59111
59113
|
});
|
|
59112
|
-
function readFarmManifest(
|
|
59113
|
-
const raw =
|
|
59114
|
+
function readFarmManifest(fs19, farmRoot) {
|
|
59115
|
+
const raw = fs19.readFileUtf8(import_node_path16.default.join(farmRoot, FARM_MANIFEST_FILENAME)) ?? fs19.readFileUtf8(import_node_path16.default.join(farmRoot, LEGACY_FARM_MANIFEST_FILENAME));
|
|
59114
59116
|
if (raw === void 0) {
|
|
59115
59117
|
return void 0;
|
|
59116
59118
|
}
|
|
@@ -59168,9 +59170,9 @@ function buildEntryFacts(params) {
|
|
|
59168
59170
|
entries
|
|
59169
59171
|
};
|
|
59170
59172
|
}
|
|
59171
|
-
function listSubtree(
|
|
59173
|
+
function listSubtree(fs19, root, rel) {
|
|
59172
59174
|
const absolute = import_node_path16.default.join(root, rel);
|
|
59173
|
-
const stat =
|
|
59175
|
+
const stat = fs19.lstat(absolute);
|
|
59174
59176
|
if (stat === void 0) {
|
|
59175
59177
|
return [];
|
|
59176
59178
|
}
|
|
@@ -59178,10 +59180,10 @@ function listSubtree(fs18, root, rel) {
|
|
|
59178
59180
|
return [{ rel, kind: "symlink" }];
|
|
59179
59181
|
}
|
|
59180
59182
|
if (stat.kind === "dir") {
|
|
59181
|
-
const children = [...
|
|
59183
|
+
const children = [...fs19.readdir(absolute)].sort().flatMap((name) => listSubtree(fs19, root, `${rel}/${name}`));
|
|
59182
59184
|
return [{ rel, kind: "dir" }, ...children];
|
|
59183
59185
|
}
|
|
59184
|
-
const contentHash =
|
|
59186
|
+
const contentHash = fs19.hashFile(absolute);
|
|
59185
59187
|
return [{ rel, kind: "file", ...contentHash === void 0 ? {} : { contentHash } }];
|
|
59186
59188
|
}
|
|
59187
59189
|
function topLevelScopeRoots(roots) {
|
|
@@ -59195,25 +59197,25 @@ function topLevelScopeRoots(roots) {
|
|
|
59195
59197
|
}
|
|
59196
59198
|
return kept;
|
|
59197
59199
|
}
|
|
59198
|
-
function reconciliationScope(
|
|
59200
|
+
function reconciliationScope(fs19, farmRoot, manifest) {
|
|
59199
59201
|
if (manifest !== void 0) {
|
|
59200
59202
|
return topLevelScopeRoots(manifest.materialised);
|
|
59201
59203
|
}
|
|
59202
|
-
return [...
|
|
59204
|
+
return [...fs19.readdir(farmRoot)].filter((name) => name !== FARM_MANIFEST_FILENAME && name !== LEGACY_FARM_MANIFEST_FILENAME && fs19.lstat(import_node_path16.default.join(farmRoot, name))?.kind === "dir").sort();
|
|
59203
59205
|
}
|
|
59204
|
-
function collectFarmListing(
|
|
59205
|
-
return scopeRoots.flatMap((root) => listSubtree(
|
|
59206
|
+
function collectFarmListing(fs19, farmRoot, scopeRoots) {
|
|
59207
|
+
return scopeRoots.flatMap((root) => listSubtree(fs19, farmRoot, root));
|
|
59206
59208
|
}
|
|
59207
|
-
function collectCanonicalCounterparts(
|
|
59209
|
+
function collectCanonicalCounterparts(fs19, claudeHome, farmListing) {
|
|
59208
59210
|
const out = [];
|
|
59209
59211
|
for (const entry of farmListing) {
|
|
59210
59212
|
const absolute = import_node_path16.default.join(claudeHome, entry.rel);
|
|
59211
|
-
const stat =
|
|
59213
|
+
const stat = fs19.lstat(absolute);
|
|
59212
59214
|
if (stat === void 0) {
|
|
59213
59215
|
continue;
|
|
59214
59216
|
}
|
|
59215
59217
|
if (stat.kind === "file") {
|
|
59216
|
-
const contentHash =
|
|
59218
|
+
const contentHash = fs19.hashFile(absolute);
|
|
59217
59219
|
out.push({ rel: entry.rel, kind: "file", ...contentHash === void 0 ? {} : { contentHash } });
|
|
59218
59220
|
continue;
|
|
59219
59221
|
}
|
|
@@ -60455,7 +60457,7 @@ async function resolveFarmConflicts(params) {
|
|
|
60455
60457
|
return { resolved, autoResolved, removed, retained };
|
|
60456
60458
|
}
|
|
60457
60459
|
|
|
60458
|
-
// src/cli/
|
|
60460
|
+
// src/cli/pairs.ts
|
|
60459
60461
|
function splitCommas(input2) {
|
|
60460
60462
|
if (input2 === "") {
|
|
60461
60463
|
return [];
|
|
@@ -60489,6 +60491,8 @@ function parseBoolPairList(input2) {
|
|
|
60489
60491
|
}
|
|
60490
60492
|
return result;
|
|
60491
60493
|
}
|
|
60494
|
+
|
|
60495
|
+
// src/cli/parsers.ts
|
|
60492
60496
|
function asInvalidArgument(parse3) {
|
|
60493
60497
|
try {
|
|
60494
60498
|
return parse3();
|
|
@@ -60985,7 +60989,7 @@ function registerIdentityCommand(program2, deps) {
|
|
|
60985
60989
|
// package.json
|
|
60986
60990
|
var package_default = {
|
|
60987
60991
|
name: "agent-shim",
|
|
60988
|
-
version: "7.
|
|
60992
|
+
version: "7.13.0",
|
|
60989
60993
|
description: "A profile manager and launcher for Claude Code that lets one person run multiple logins from one machine while controlling what gets shared between them.",
|
|
60990
60994
|
license: "MIT",
|
|
60991
60995
|
author: "Joseph Mearman <joseph@mearman.co.uk>",
|
|
@@ -61307,12 +61311,12 @@ function isIdentityName(name) {
|
|
|
61307
61311
|
function claudeJsonPath(identitiesDir, identity) {
|
|
61308
61312
|
return import_node_path21.default.join(identitiesDir, identity, CLAUDE_JSON);
|
|
61309
61313
|
}
|
|
61310
|
-
function readAccountMetadata(
|
|
61314
|
+
function readAccountMetadata(fs19, identitiesDir, identity) {
|
|
61311
61315
|
if (!isIdentityName(identity)) {
|
|
61312
61316
|
throw new Error(`"${identity}" is not a valid identity name.`);
|
|
61313
61317
|
}
|
|
61314
61318
|
const file2 = claudeJsonPath(identitiesDir, identity);
|
|
61315
|
-
const raw =
|
|
61319
|
+
const raw = fs19.readFileUtf8(file2);
|
|
61316
61320
|
if (raw === void 0) {
|
|
61317
61321
|
return void 0;
|
|
61318
61322
|
}
|
|
@@ -61342,19 +61346,19 @@ function readAccountMetadata(fs18, identitiesDir, identity) {
|
|
|
61342
61346
|
}
|
|
61343
61347
|
return account.data;
|
|
61344
61348
|
}
|
|
61345
|
-
function createAccountReader(
|
|
61349
|
+
function createAccountReader(fs19, identitiesDir) {
|
|
61346
61350
|
const cache = /* @__PURE__ */ new Map();
|
|
61347
61351
|
return (identity) => {
|
|
61348
61352
|
if (!isIdentityName(identity)) {
|
|
61349
61353
|
throw new Error(`"${identity}" is not a valid identity name.`);
|
|
61350
61354
|
}
|
|
61351
|
-
const stat =
|
|
61355
|
+
const stat = fs19.lstat(claudeJsonPath(identitiesDir, identity));
|
|
61352
61356
|
const stamp = stat === void 0 ? "absent" : `${String(stat.mtimeMs)}:${String(stat.sizeBytes)}`;
|
|
61353
61357
|
const cached2 = cache.get(identity);
|
|
61354
61358
|
if (cached2?.stamp === stamp) {
|
|
61355
61359
|
return cached2.account;
|
|
61356
61360
|
}
|
|
61357
|
-
const account = readAccountMetadata(
|
|
61361
|
+
const account = readAccountMetadata(fs19, identitiesDir, identity);
|
|
61358
61362
|
cache.set(identity, { stamp, account });
|
|
61359
61363
|
return account;
|
|
61360
61364
|
};
|
|
@@ -62122,8 +62126,8 @@ function segmentDay(epochMs) {
|
|
|
62122
62126
|
function segmentName(day, pid) {
|
|
62123
62127
|
return `${day}.${String(pid)}.jsonl`;
|
|
62124
62128
|
}
|
|
62125
|
-
function listLogSegments(
|
|
62126
|
-
return
|
|
62129
|
+
function listLogSegments(fs19, logDir) {
|
|
62130
|
+
return fs19.readdir(logDir).flatMap((name) => {
|
|
62127
62131
|
const match = SEGMENT_RE.exec(name);
|
|
62128
62132
|
const day = match?.[1];
|
|
62129
62133
|
if (day === void 0) {
|
|
@@ -62132,15 +62136,15 @@ function listLogSegments(fs18, logDir) {
|
|
|
62132
62136
|
return [{ name, day, endsAt: Date.parse(`${day}T00:00:00.000Z`) + DAY_MS }];
|
|
62133
62137
|
}).sort((left, right) => left.day.localeCompare(right.day) || left.name.localeCompare(right.name));
|
|
62134
62138
|
}
|
|
62135
|
-
function readUsageLog(
|
|
62139
|
+
function readUsageLog(fs19, logDir, options = {}) {
|
|
62136
62140
|
const { sinceMs } = options;
|
|
62137
62141
|
const records = [];
|
|
62138
62142
|
let invalidLines = 0;
|
|
62139
|
-
for (const segment of listLogSegments(
|
|
62143
|
+
for (const segment of listLogSegments(fs19, logDir)) {
|
|
62140
62144
|
if (sinceMs !== void 0 && segment.endsAt <= sinceMs) {
|
|
62141
62145
|
continue;
|
|
62142
62146
|
}
|
|
62143
|
-
const contents =
|
|
62147
|
+
const contents = fs19.readFileUtf8(import_node_path22.default.join(logDir, segment.name));
|
|
62144
62148
|
if (contents === void 0) {
|
|
62145
62149
|
continue;
|
|
62146
62150
|
}
|
|
@@ -62184,9 +62188,9 @@ function snapshotPath(snapshotsDir, identity) {
|
|
|
62184
62188
|
}
|
|
62185
62189
|
return import_node_path22.default.join(snapshotsDir, `${identity}.json`);
|
|
62186
62190
|
}
|
|
62187
|
-
function readUsageSnapshot(
|
|
62191
|
+
function readUsageSnapshot(fs19, snapshotsDir, identity) {
|
|
62188
62192
|
const file2 = snapshotPath(snapshotsDir, identity);
|
|
62189
|
-
const raw =
|
|
62193
|
+
const raw = fs19.readFileUtf8(file2);
|
|
62190
62194
|
if (raw === void 0) {
|
|
62191
62195
|
return void 0;
|
|
62192
62196
|
}
|
|
@@ -62202,8 +62206,8 @@ function readUsageSnapshot(fs18, snapshotsDir, identity) {
|
|
|
62202
62206
|
}
|
|
62203
62207
|
return snapshot.data;
|
|
62204
62208
|
}
|
|
62205
|
-
function listUsageSnapshots(
|
|
62206
|
-
return
|
|
62209
|
+
function listUsageSnapshots(fs19, snapshotsDir) {
|
|
62210
|
+
return fs19.readdir(snapshotsDir).flatMap((name) => {
|
|
62207
62211
|
if (!name.endsWith(".json")) {
|
|
62208
62212
|
return [];
|
|
62209
62213
|
}
|
|
@@ -62211,7 +62215,7 @@ function listUsageSnapshots(fs18, snapshotsDir) {
|
|
|
62211
62215
|
if (!isIdentityName(identity)) {
|
|
62212
62216
|
return [];
|
|
62213
62217
|
}
|
|
62214
|
-
const snapshot = readUsageSnapshot(
|
|
62218
|
+
const snapshot = readUsageSnapshot(fs19, snapshotsDir, identity);
|
|
62215
62219
|
return snapshot === void 0 ? [] : [snapshot];
|
|
62216
62220
|
}).sort((left, right) => left.identity.localeCompare(right.identity));
|
|
62217
62221
|
}
|
|
@@ -62270,11 +62274,11 @@ var import_node_path23 = __toESM(require("node:path"), 1);
|
|
|
62270
62274
|
var DAY_MS2 = 864e5;
|
|
62271
62275
|
var DAYS_IN_WEEK = 7;
|
|
62272
62276
|
var USAGE_RETENTION_MS = DAYS_IN_WEEK * DAY_MS2;
|
|
62273
|
-
function pruneUsageLog(
|
|
62277
|
+
function pruneUsageLog(fs19, logDir, nowMs) {
|
|
62274
62278
|
const cutoff = nowMs - USAGE_RETENTION_MS;
|
|
62275
|
-
for (const segment of listLogSegments(
|
|
62279
|
+
for (const segment of listLogSegments(fs19, logDir)) {
|
|
62276
62280
|
if (segment.endsAt <= cutoff) {
|
|
62277
|
-
|
|
62281
|
+
fs19.removeRecursive(import_node_path23.default.join(logDir, segment.name));
|
|
62278
62282
|
}
|
|
62279
62283
|
}
|
|
62280
62284
|
}
|
|
@@ -62303,7 +62307,7 @@ function foldProviderState(current, record2) {
|
|
|
62303
62307
|
};
|
|
62304
62308
|
}
|
|
62305
62309
|
function createUsageStore(deps) {
|
|
62306
|
-
const { fs:
|
|
62310
|
+
const { fs: fs19, paths } = deps;
|
|
62307
62311
|
let prunedDay;
|
|
62308
62312
|
const rewriteSnapshot = (identity, fold) => {
|
|
62309
62313
|
if (!isIdentityName(identity)) {
|
|
@@ -62312,7 +62316,7 @@ function createUsageStore(deps) {
|
|
|
62312
62316
|
}
|
|
62313
62317
|
let current;
|
|
62314
62318
|
try {
|
|
62315
|
-
current = readUsageSnapshot(
|
|
62319
|
+
current = readUsageSnapshot(fs19, paths.usageSnapshotsDir, identity);
|
|
62316
62320
|
} catch (error62) {
|
|
62317
62321
|
if (!(error62 instanceof UsageSnapshotError)) {
|
|
62318
62322
|
throw error62;
|
|
@@ -62339,8 +62343,8 @@ function createUsageStore(deps) {
|
|
|
62339
62343
|
...account === void 0 ? {} : { account },
|
|
62340
62344
|
providers
|
|
62341
62345
|
});
|
|
62342
|
-
|
|
62343
|
-
|
|
62346
|
+
fs19.mkdirPrivate(paths.usageSnapshotsDir);
|
|
62347
|
+
fs19.writeFilePrivate(snapshotPath(paths.usageSnapshotsDir, identity), `${JSON.stringify(snapshot, null, 2)}
|
|
62344
62348
|
`);
|
|
62345
62349
|
return true;
|
|
62346
62350
|
};
|
|
@@ -62348,14 +62352,14 @@ function createUsageStore(deps) {
|
|
|
62348
62352
|
record: (input2) => {
|
|
62349
62353
|
const record2 = UsageRecordSchema.parse(input2);
|
|
62350
62354
|
const nowMs = deps.now();
|
|
62351
|
-
|
|
62352
|
-
|
|
62355
|
+
fs19.mkdirPrivate(paths.usageDir);
|
|
62356
|
+
fs19.mkdirPrivate(paths.usageLogDir);
|
|
62353
62357
|
const day = segmentDay(nowMs);
|
|
62354
62358
|
if (prunedDay !== day) {
|
|
62355
|
-
pruneUsageLog(
|
|
62359
|
+
pruneUsageLog(fs19, paths.usageLogDir, nowMs);
|
|
62356
62360
|
prunedDay = day;
|
|
62357
62361
|
}
|
|
62358
|
-
|
|
62362
|
+
fs19.appendFilePrivate(import_node_path23.default.join(paths.usageLogDir, segmentName(day, deps.pid)), `${JSON.stringify(record2)}
|
|
62359
62363
|
`);
|
|
62360
62364
|
if (record2.identity !== void 0) {
|
|
62361
62365
|
rewriteSnapshot(record2.identity, (current) => ({ ...current?.providers, [record2.provider]: foldProviderState(current?.providers[record2.provider], record2) }));
|
|
@@ -62382,14 +62386,14 @@ var PicksFileSchema = external_exports.strictObject({
|
|
|
62382
62386
|
function errorMessage(error62) {
|
|
62383
62387
|
return error62 instanceof Error ? error62.message : String(error62);
|
|
62384
62388
|
}
|
|
62385
|
-
function loadPoolMembers(
|
|
62389
|
+
function loadPoolMembers(fs19, paths, identities, nowMs) {
|
|
62386
62390
|
const windowStartMs = nowMs - FIVE_HOUR_WINDOW_MS;
|
|
62387
|
-
const log = readUsageLog(
|
|
62391
|
+
const log = readUsageLog(fs19, paths.usageLogDir, { sinceMs: windowStartMs });
|
|
62388
62392
|
return identities.map((identity) => {
|
|
62389
62393
|
const records = log.records.filter((record2) => record2.identity === identity && record2.provider === ANTHROPIC_PROVIDER && Date.parse(record2.at) >= windowStartMs);
|
|
62390
62394
|
try {
|
|
62391
|
-
const snapshot = readUsageSnapshot(
|
|
62392
|
-
const account = readAccountMetadata(
|
|
62395
|
+
const snapshot = readUsageSnapshot(fs19, paths.usageSnapshotsDir, identity);
|
|
62396
|
+
const account = readAccountMetadata(fs19, paths.identitiesDir, identity) ?? snapshot?.account;
|
|
62393
62397
|
return { identity, records, ...snapshot === void 0 ? {} : { snapshot }, ...account === void 0 ? {} : { account } };
|
|
62394
62398
|
} catch (error62) {
|
|
62395
62399
|
if (error62 instanceof UsageSnapshotError || error62 instanceof AccountMetadataError) {
|
|
@@ -62407,9 +62411,9 @@ function rankPoolFromStore(params) {
|
|
|
62407
62411
|
...params.sticky === void 0 ? {} : { sticky: params.sticky }
|
|
62408
62412
|
});
|
|
62409
62413
|
}
|
|
62410
|
-
function readPicksFile(
|
|
62414
|
+
function readPicksFile(fs19, file2) {
|
|
62411
62415
|
const empty = { schemaVersion: SCHEMA_VERSION, picks: {} };
|
|
62412
|
-
const raw =
|
|
62416
|
+
const raw = fs19.readFileUtf8(file2);
|
|
62413
62417
|
if (raw === void 0) {
|
|
62414
62418
|
return { file: empty };
|
|
62415
62419
|
}
|
|
@@ -62422,17 +62426,17 @@ function readPicksFile(fs18, file2) {
|
|
|
62422
62426
|
const result = PicksFileSchema.safeParse(parsed);
|
|
62423
62427
|
return result.success ? { file: result.data } : { file: empty, problem: `${file2} is not a picks file this agent-shim can read` };
|
|
62424
62428
|
}
|
|
62425
|
-
function readStickyPick(
|
|
62426
|
-
const read = readPicksFile(
|
|
62429
|
+
function readStickyPick(fs19, picksFile, directory) {
|
|
62430
|
+
const read = readPicksFile(fs19, picksFile);
|
|
62427
62431
|
const entry = read.file.picks[directory];
|
|
62428
62432
|
return { ...entry === void 0 ? {} : { sticky: entry }, ...read.problem === void 0 ? {} : { problem: read.problem } };
|
|
62429
62433
|
}
|
|
62430
|
-
function recordStickyPick(
|
|
62431
|
-
const current = readPicksFile(
|
|
62434
|
+
function recordStickyPick(fs19, picksFile, directory, identity, nowMs) {
|
|
62435
|
+
const current = readPicksFile(fs19, picksFile).file.picks;
|
|
62432
62436
|
const kept = Object.entries(current).filter(([, entry]) => nowMs - Date.parse(entry.at) <= USAGE_RETENTION_MS);
|
|
62433
62437
|
const next = { schemaVersion: SCHEMA_VERSION, picks: { ...Object.fromEntries(kept), [directory]: { identity, at: new Date(nowMs).toISOString() } } };
|
|
62434
|
-
|
|
62435
|
-
|
|
62438
|
+
fs19.mkdirPrivate(import_node_path24.default.dirname(picksFile));
|
|
62439
|
+
fs19.writeFilePrivate(picksFile, `${JSON.stringify(next, null, 2)}
|
|
62436
62440
|
`);
|
|
62437
62441
|
}
|
|
62438
62442
|
|
|
@@ -62440,8 +62444,8 @@ function recordStickyPick(fs18, picksFile, directory, identity, nowMs) {
|
|
|
62440
62444
|
var RESUME_FLAGS = /* @__PURE__ */ new Set(["--continue", "-c", "--resume", "-r"]);
|
|
62441
62445
|
var TERMINATOR = "--";
|
|
62442
62446
|
var REASONS_LOGGED = 3;
|
|
62443
|
-
function splitMembers(pool, paths,
|
|
62444
|
-
const exists = (name) => loadIdentity(paths.identitiesDir, name,
|
|
62447
|
+
function splitMembers(pool, paths, fs19) {
|
|
62448
|
+
const exists = (name) => loadIdentity(paths.identitiesDir, name, fs19) !== void 0;
|
|
62445
62449
|
return { present: pool.identities.filter(exists), missing: pool.identities.filter((name) => !exists(name)) };
|
|
62446
62450
|
}
|
|
62447
62451
|
function isResuming(passthrough) {
|
|
@@ -63284,10 +63288,10 @@ var FrontDoorStateSchema = external_exports.strictObject({
|
|
|
63284
63288
|
/** The last fatal error, kept across shutdowns so `frontdoor status` can explain a front door that is not running. */
|
|
63285
63289
|
lastError: external_exports.string().optional()
|
|
63286
63290
|
});
|
|
63287
|
-
function readFrontDoorState(
|
|
63291
|
+
function readFrontDoorState(fs19, stateFile) {
|
|
63288
63292
|
let raw;
|
|
63289
63293
|
try {
|
|
63290
|
-
raw =
|
|
63294
|
+
raw = fs19.readFileUtf8(stateFile);
|
|
63291
63295
|
} catch {
|
|
63292
63296
|
return void 0;
|
|
63293
63297
|
}
|
|
@@ -63301,28 +63305,28 @@ function readFrontDoorState(fs18, stateFile) {
|
|
|
63301
63305
|
return void 0;
|
|
63302
63306
|
}
|
|
63303
63307
|
}
|
|
63304
|
-
function writeFrontDoorState(
|
|
63305
|
-
|
|
63306
|
-
|
|
63308
|
+
function writeFrontDoorState(fs19, stateFile, state) {
|
|
63309
|
+
fs19.mkdirp(import_node_path28.default.dirname(stateFile));
|
|
63310
|
+
fs19.writeFileUtf8(stateFile, `${JSON.stringify(state, null, 2)}
|
|
63307
63311
|
`);
|
|
63308
63312
|
}
|
|
63309
63313
|
function frontDoorOrigin(state) {
|
|
63310
63314
|
const port = state?.directPort ?? state?.lastDirectPort;
|
|
63311
63315
|
return port === void 0 ? void 0 : `http://127.0.0.1:${String(port)}`;
|
|
63312
63316
|
}
|
|
63313
|
-
function writeFrontDoorSession(
|
|
63314
|
-
|
|
63315
|
-
|
|
63317
|
+
function writeFrontDoorSession(fs19, sessionsDir, session) {
|
|
63318
|
+
fs19.mkdirPrivate(sessionsDir);
|
|
63319
|
+
fs19.writeFilePrivate(import_node_path28.default.join(sessionsDir, `${String(session.pid)}.json`), `${JSON.stringify(session, null, 2)}
|
|
63316
63320
|
`);
|
|
63317
63321
|
}
|
|
63318
|
-
function listFrontDoorSessions(
|
|
63322
|
+
function listFrontDoorSessions(fs19, sessionsDir) {
|
|
63319
63323
|
const sessions = [];
|
|
63320
|
-
for (const name of
|
|
63324
|
+
for (const name of fs19.readdir(sessionsDir)) {
|
|
63321
63325
|
if (!name.endsWith(".json")) {
|
|
63322
63326
|
continue;
|
|
63323
63327
|
}
|
|
63324
63328
|
try {
|
|
63325
|
-
const parsed = FrontDoorSessionSchema.safeParse(JSON.parse(
|
|
63329
|
+
const parsed = FrontDoorSessionSchema.safeParse(JSON.parse(fs19.readFileUtf8(import_node_path28.default.join(sessionsDir, name)) ?? ""));
|
|
63326
63330
|
if (parsed.success) {
|
|
63327
63331
|
sessions.push({ pid: parsed.data.pid, startedAt: parsed.data.startedAt });
|
|
63328
63332
|
}
|
|
@@ -63332,27 +63336,27 @@ function listFrontDoorSessions(fs18, sessionsDir) {
|
|
|
63332
63336
|
}
|
|
63333
63337
|
return sessions.sort((a2, b) => a2.pid - b.pid);
|
|
63334
63338
|
}
|
|
63335
|
-
function removeFrontDoorSession(
|
|
63336
|
-
|
|
63339
|
+
function removeFrontDoorSession(fs19, sessionsDir, pid) {
|
|
63340
|
+
fs19.removeRecursive(import_node_path28.default.join(sessionsDir, `${String(pid)}.json`));
|
|
63337
63341
|
}
|
|
63338
|
-
function pruneDeadFrontDoorSessions(
|
|
63342
|
+
function pruneDeadFrontDoorSessions(fs19, sessionsDir, isRunning) {
|
|
63339
63343
|
const removed = [];
|
|
63340
|
-
for (const session of listFrontDoorSessions(
|
|
63344
|
+
for (const session of listFrontDoorSessions(fs19, sessionsDir)) {
|
|
63341
63345
|
if (!isRunning(session.pid)) {
|
|
63342
|
-
removeFrontDoorSession(
|
|
63346
|
+
removeFrontDoorSession(fs19, sessionsDir, session.pid);
|
|
63343
63347
|
removed.push(session.pid);
|
|
63344
63348
|
}
|
|
63345
63349
|
}
|
|
63346
63350
|
return removed;
|
|
63347
63351
|
}
|
|
63348
|
-
function liveSessionTokens(
|
|
63352
|
+
function liveSessionTokens(fs19, sessionsDir) {
|
|
63349
63353
|
const tokens = /* @__PURE__ */ new Set();
|
|
63350
|
-
for (const name of
|
|
63354
|
+
for (const name of fs19.readdir(sessionsDir)) {
|
|
63351
63355
|
if (!name.endsWith(".json")) {
|
|
63352
63356
|
continue;
|
|
63353
63357
|
}
|
|
63354
63358
|
try {
|
|
63355
|
-
const parsed = FrontDoorSessionSchema.safeParse(JSON.parse(
|
|
63359
|
+
const parsed = FrontDoorSessionSchema.safeParse(JSON.parse(fs19.readFileUtf8(import_node_path28.default.join(sessionsDir, name)) ?? ""));
|
|
63356
63360
|
if (parsed.success) {
|
|
63357
63361
|
tokens.add(parsed.data.token);
|
|
63358
63362
|
}
|
|
@@ -63717,8 +63721,8 @@ function registerCodexCommand(program2, deps) {
|
|
|
63717
63721
|
}
|
|
63718
63722
|
|
|
63719
63723
|
// src/frontdoor/commands.ts
|
|
63720
|
-
var
|
|
63721
|
-
var
|
|
63724
|
+
var import_node_crypto11 = require("node:crypto");
|
|
63725
|
+
var import_node_path30 = __toESM(require("node:path"), 1);
|
|
63722
63726
|
var import_node_fs11 = __toESM(require("node:fs"), 1);
|
|
63723
63727
|
|
|
63724
63728
|
// src/frontdoor/custody.ts
|
|
@@ -64324,8 +64328,8 @@ var FRONTDOOR_SUPERVISOR_STILL_RUNNING = -1;
|
|
|
64324
64328
|
var FRONTDOOR_POLL_MS = 1e3;
|
|
64325
64329
|
var MS_PER_MINUTE = 6e4;
|
|
64326
64330
|
async function runFrontDoorSupervisor(idleShutdownMinutes, ports, options = {}) {
|
|
64327
|
-
const { fs:
|
|
64328
|
-
const previous = readFrontDoorState(
|
|
64331
|
+
const { fs: fs19, paths } = ports;
|
|
64332
|
+
const previous = readFrontDoorState(fs19, paths.frontdoorStateFile);
|
|
64329
64333
|
let lastPort = previous?.lastPort;
|
|
64330
64334
|
let lastConnectPort = previous?.lastConnectPort;
|
|
64331
64335
|
let lastDirectPort = previous?.lastDirectPort;
|
|
@@ -64335,12 +64339,12 @@ async function runFrontDoorSupervisor(idleShutdownMinutes, ports, options = {})
|
|
|
64335
64339
|
...lastDirectPort === void 0 ? {} : { lastDirectPort }
|
|
64336
64340
|
});
|
|
64337
64341
|
const fail = (message) => {
|
|
64338
|
-
writeFrontDoorState(
|
|
64339
|
-
|
|
64342
|
+
writeFrontDoorState(fs19, paths.frontdoorStateFile, { lastError: message, ...sticky() });
|
|
64343
|
+
fs19.removeRecursive(paths.frontdoorLockFile);
|
|
64340
64344
|
ports.log(`agent-shim frontdoor supervisor: ${message}`);
|
|
64341
64345
|
return 1;
|
|
64342
64346
|
};
|
|
64343
|
-
writeFrontDoorState(
|
|
64347
|
+
writeFrontDoorState(fs19, paths.frontdoorStateFile, { supervisorPid: ports.ownPid, ...sticky() });
|
|
64344
64348
|
const previousSticky = lastPort;
|
|
64345
64349
|
let http5;
|
|
64346
64350
|
try {
|
|
@@ -64377,8 +64381,8 @@ async function runFrontDoorSupervisor(idleShutdownMinutes, ports, options = {})
|
|
|
64377
64381
|
ports.log(`agent-shim frontdoor supervisor: sticky direct port ${String(previousStickyDirect)} was occupied; moving to ${String(direct.port)} (headroom must be restarted to admit the new address)`);
|
|
64378
64382
|
}
|
|
64379
64383
|
lastDirectPort = direct.port;
|
|
64380
|
-
writeFrontDoorState(
|
|
64381
|
-
|
|
64384
|
+
writeFrontDoorState(fs19, paths.frontdoorStateFile, { protocol: FRONT_DOOR_PROTOCOL, supervisorPid: ports.ownPid, port: http5.port, connectPort: connect3.port, directPort: direct.port, ...sticky() });
|
|
64385
|
+
fs19.removeRecursive(paths.frontdoorLockFile);
|
|
64382
64386
|
ports.log(`agent-shim frontdoor supervisor ${String(ports.ownPid)}: front door on 127.0.0.1:${String(http5.port)}, CONNECT surface on 127.0.0.1:${String(connect3.port)}`);
|
|
64383
64387
|
let idleSince;
|
|
64384
64388
|
let ticks = 0;
|
|
@@ -64387,15 +64391,15 @@ async function runFrontDoorSupervisor(idleShutdownMinutes, ports, options = {})
|
|
|
64387
64391
|
return FRONTDOOR_SUPERVISOR_STILL_RUNNING;
|
|
64388
64392
|
}
|
|
64389
64393
|
ticks += 1;
|
|
64390
|
-
pruneDeadFrontDoorSessions(
|
|
64391
|
-
if (listFrontDoorSessions(
|
|
64394
|
+
pruneDeadFrontDoorSessions(fs19, paths.frontdoorSessionsDir, ports.isRunning);
|
|
64395
|
+
if (listFrontDoorSessions(fs19, paths.frontdoorSessionsDir).length === 0) {
|
|
64392
64396
|
idleSince ??= ports.now();
|
|
64393
64397
|
if (ports.now() - idleSince >= idleShutdownMinutes * MS_PER_MINUTE) {
|
|
64394
64398
|
ports.log(`agent-shim frontdoor supervisor: no sessions for ${String(idleShutdownMinutes)} minute(s); closing the front door and exiting`);
|
|
64395
64399
|
await direct.close();
|
|
64396
64400
|
await connect3.close();
|
|
64397
64401
|
await http5.close();
|
|
64398
|
-
writeFrontDoorState(
|
|
64402
|
+
writeFrontDoorState(fs19, paths.frontdoorStateFile, sticky());
|
|
64399
64403
|
return 0;
|
|
64400
64404
|
}
|
|
64401
64405
|
} else {
|
|
@@ -64945,80 +64949,6 @@ function realConnectEffects() {
|
|
|
64945
64949
|
};
|
|
64946
64950
|
}
|
|
64947
64951
|
|
|
64948
|
-
// src/frontdoor/ensure.ts
|
|
64949
|
-
var import_node_crypto10 = require("node:crypto");
|
|
64950
|
-
var FRONTDOOR_START_TIMEOUT_MS = 3e4;
|
|
64951
|
-
var FRONTDOOR_LAUNCHER_POLL_MS = 100;
|
|
64952
|
-
var FrontDoorStartError = class extends CliError {
|
|
64953
|
-
constructor(message) {
|
|
64954
|
-
super(message);
|
|
64955
|
-
this.name = "FrontDoorStartError";
|
|
64956
|
-
}
|
|
64957
|
-
};
|
|
64958
|
-
function ensureFrontDoor(params) {
|
|
64959
|
-
const { paths, ports } = params;
|
|
64960
|
-
const deadline = ports.now() + FRONTDOOR_START_TIMEOUT_MS;
|
|
64961
|
-
let spawned = false;
|
|
64962
|
-
let replaced = false;
|
|
64963
|
-
const distrusted = /* @__PURE__ */ new Set();
|
|
64964
|
-
let lastRejection;
|
|
64965
|
-
for (; ; ) {
|
|
64966
|
-
const state = readFrontDoorState(ports.fs, paths.frontdoorStateFile);
|
|
64967
|
-
const supervisorPid = state?.supervisorPid;
|
|
64968
|
-
if (supervisorPid !== void 0 && !distrusted.has(supervisorPid) && ports.isRunning(supervisorPid)) {
|
|
64969
|
-
if (state?.port !== void 0 && state.connectPort !== void 0) {
|
|
64970
|
-
const verdict = ports.verifyListener(state.port);
|
|
64971
|
-
if (verdict.ok && (state.protocol ?? 0) < FRONT_DOOR_PROTOCOL && !replaced) {
|
|
64972
|
-
ports.stopSupervisor(supervisorPid);
|
|
64973
|
-
while (ports.isRunning(supervisorPid)) {
|
|
64974
|
-
if (ports.now() >= deadline) {
|
|
64975
|
-
throw new FrontDoorStartError(`agent-shim: the front door (pid ${String(supervisorPid)}, protocol ${String(state.protocol ?? 0)}) is older than this launcher (protocol ${String(FRONT_DOOR_PROTOCOL)}) and did not exit when asked. Daemon log: ${paths.frontdoorLogPath}`);
|
|
64976
|
-
}
|
|
64977
|
-
ports.sleep(FRONTDOOR_LAUNCHER_POLL_MS);
|
|
64978
|
-
}
|
|
64979
|
-
replaced = true;
|
|
64980
|
-
continue;
|
|
64981
|
-
}
|
|
64982
|
-
if (verdict.ok) {
|
|
64983
|
-
const token = (0, import_node_crypto10.randomUUID)();
|
|
64984
|
-
writeFrontDoorSession(ports.fs, paths.frontdoorSessionsDir, { pid: params.launcherPid, startedAt: ports.now(), token });
|
|
64985
|
-
return { port: state.port, connectPort: state.connectPort, token };
|
|
64986
|
-
}
|
|
64987
|
-
lastRejection = `the listener on 127.0.0.1:${String(state.port)} (recorded for pid ${String(supervisorPid)}) failed authentication: ${verdict.reason}`;
|
|
64988
|
-
if (spawned) {
|
|
64989
|
-
throw new FrontDoorStartError(`agent-shim: ${lastRejection}; refusing to send this session's credentials to it. Daemon log: ${paths.frontdoorLogPath}`);
|
|
64990
|
-
}
|
|
64991
|
-
distrusted.add(supervisorPid);
|
|
64992
|
-
continue;
|
|
64993
|
-
}
|
|
64994
|
-
if (state?.lastError !== void 0) {
|
|
64995
|
-
throw new FrontDoorStartError(`agent-shim: the front door reported a fatal error and is not serving: ${state.lastError} (daemon log: ${paths.frontdoorLogPath})`);
|
|
64996
|
-
}
|
|
64997
|
-
} else if (!spawned) {
|
|
64998
|
-
ports.fs.mkdirp(paths.frontdoorDir);
|
|
64999
|
-
const created = ports.fs.writeFileExclusive(paths.frontdoorLockFile, `${JSON.stringify({ pid: params.launcherPid, at: ports.now() })}
|
|
65000
|
-
`);
|
|
65001
|
-
if (created) {
|
|
65002
|
-
ports.spawnSupervisor(paths);
|
|
65003
|
-
spawned = true;
|
|
65004
|
-
} else {
|
|
65005
|
-
const lock = readStartLock(ports.fs, paths.frontdoorLockFile);
|
|
65006
|
-
if (lock === void 0 || !ports.isRunning(lock.pid)) {
|
|
65007
|
-
ports.fs.removeRecursive(paths.frontdoorLockFile);
|
|
65008
|
-
continue;
|
|
65009
|
-
}
|
|
65010
|
-
}
|
|
65011
|
-
}
|
|
65012
|
-
if (ports.now() >= deadline) {
|
|
65013
|
-
const lastError = state?.lastError;
|
|
65014
|
-
throw new FrontDoorStartError(
|
|
65015
|
-
`agent-shim: the front door did not become ready within ${String(FRONTDOOR_START_TIMEOUT_MS)}ms` + (lastError === void 0 ? "" : ` (last error: ${lastError})`) + (lastRejection === void 0 ? "" : ` (${lastRejection})`) + `. Daemon log: ${paths.frontdoorLogPath}`
|
|
65016
|
-
);
|
|
65017
|
-
}
|
|
65018
|
-
ports.sleep(FRONTDOOR_LAUNCHER_POLL_MS);
|
|
65019
|
-
}
|
|
65020
|
-
}
|
|
65021
|
-
|
|
65022
64952
|
// src/frontdoor/headroomHop.ts
|
|
65023
64953
|
var http3 = __toESM(require("node:http"), 1);
|
|
65024
64954
|
|
|
@@ -65213,7 +65143,10 @@ async function forwardThroughHeadroom(request6, response, target) {
|
|
|
65213
65143
|
if (!request6.signal.aborted) {
|
|
65214
65144
|
response.end();
|
|
65215
65145
|
}
|
|
65216
|
-
} catch {
|
|
65146
|
+
} catch (error62) {
|
|
65147
|
+
if (!request6.signal.aborted) {
|
|
65148
|
+
target.log(`front door: headroom hop to 127.0.0.1:${String(port)} ended mid-stream (${request6.method} ${new URL(request6.url, "http://127.0.0.1").pathname}): ${error62 instanceof Error ? error62.message : String(error62)}`);
|
|
65149
|
+
}
|
|
65217
65150
|
response.destroy();
|
|
65218
65151
|
}
|
|
65219
65152
|
resolve(void 0);
|
|
@@ -65222,7 +65155,9 @@ async function forwardThroughHeadroom(request6, response, target) {
|
|
|
65222
65155
|
};
|
|
65223
65156
|
const hop = http3.request({ host: "127.0.0.1", port, method: request6.method, path: request6.url, headers, agent: HOP_AGENT }, onUpstreamResponse);
|
|
65224
65157
|
hop.on("error", (error62) => {
|
|
65225
|
-
|
|
65158
|
+
const phase = response.headersSent ? "mid-stream" : "before the response started";
|
|
65159
|
+
const code = "code" in error62 && typeof error62.code === "string" ? ` ${error62.code}` : "";
|
|
65160
|
+
target.log(`front door: headroom hop to 127.0.0.1:${String(port)} failed (${request6.method} ${new URL(request6.url, "http://127.0.0.1").pathname}, ${phase}): ${error62.message}${code}`);
|
|
65226
65161
|
if (!response.headersSent) {
|
|
65227
65162
|
response.start(HTTP_BAD_GATEWAY2, { "Content-Type": "application/json" });
|
|
65228
65163
|
void response.write(JSON.stringify({ type: "error", error: { type: "api_error", message: `agent-shim front door: the headroom daemon is unreachable (${error62.message})` } })).then(() => {
|
|
@@ -65406,72 +65341,8 @@ async function serveRouted(request6, deps) {
|
|
|
65406
65341
|
}
|
|
65407
65342
|
}
|
|
65408
65343
|
|
|
65409
|
-
// src/frontdoor/probe.ts
|
|
65410
|
-
var import_node_worker_threads = require("node:worker_threads");
|
|
65411
|
-
var PROBE_TIMEOUT_MS = 2e3;
|
|
65412
|
-
var PROBE_WAIT_MARGIN_MS = 1e3;
|
|
65413
|
-
var REASON_BYTES = 1024;
|
|
65414
|
-
var HEADER_SLOTS = 2;
|
|
65415
|
-
var INT32_BYTES = 4;
|
|
65416
|
-
var VERDICT_PENDING = 0;
|
|
65417
|
-
var VERDICT_OK = 1;
|
|
65418
|
-
var VERDICT_FAILED = 2;
|
|
65419
|
-
var PROBE_SOURCE = `
|
|
65420
|
-
const { workerData } = require("node:worker_threads");
|
|
65421
|
-
const https = require("node:https");
|
|
65422
|
-
const { port, ca, timeoutMs, shared, ok, failed, headerSlots } = workerData;
|
|
65423
|
-
const header = new Int32Array(shared, 0, headerSlots);
|
|
65424
|
-
const reason = new Uint8Array(shared, headerSlots * 4);
|
|
65425
|
-
let done = false;
|
|
65426
|
-
const finish = (verdict, message) => {
|
|
65427
|
-
if (done) return;
|
|
65428
|
-
done = true;
|
|
65429
|
-
const bytes = Buffer.from(String(message), "utf8").subarray(0, reason.length);
|
|
65430
|
-
reason.set(bytes);
|
|
65431
|
-
Atomics.store(header, 1, bytes.length);
|
|
65432
|
-
Atomics.store(header, 0, verdict);
|
|
65433
|
-
Atomics.notify(header, 0);
|
|
65434
|
-
};
|
|
65435
|
-
const request = https.get({ host: "127.0.0.1", port, path: "/healthz", ca: [ca], agent: false }, (response) => {
|
|
65436
|
-
let body = "";
|
|
65437
|
-
response.setEncoding("utf8");
|
|
65438
|
-
response.on("data", (chunk) => { body += chunk; });
|
|
65439
|
-
response.on("end", () => {
|
|
65440
|
-
if (response.statusCode === 200 && body === "ok") finish(ok, "");
|
|
65441
|
-
else finish(failed, "the listener answered its health probe with HTTP " + String(response.statusCode));
|
|
65442
|
-
});
|
|
65443
|
-
response.on("error", (error) => finish(failed, error.message));
|
|
65444
|
-
});
|
|
65445
|
-
// A hard deadline over the whole exchange, handshake included: a listener that accepts the connection and then says nothing must still fail the probe.
|
|
65446
|
-
setTimeout(() => { request.destroy(new Error("no answer within " + String(timeoutMs) + "ms")); }, timeoutMs);
|
|
65447
|
-
request.on("error", (error) => finish(failed, (error.code ? error.code + ": " : "") + error.message));
|
|
65448
|
-
`;
|
|
65449
|
-
function probeFrontDoorSync(port, caPem) {
|
|
65450
|
-
const shared = new SharedArrayBuffer(HEADER_SLOTS * INT32_BYTES + REASON_BYTES);
|
|
65451
|
-
const header = new Int32Array(shared, 0, HEADER_SLOTS);
|
|
65452
|
-
const worker = new import_node_worker_threads.Worker(PROBE_SOURCE, {
|
|
65453
|
-
eval: true,
|
|
65454
|
-
workerData: { port, ca: caPem, timeoutMs: PROBE_TIMEOUT_MS, shared, ok: VERDICT_OK, failed: VERDICT_FAILED, headerSlots: HEADER_SLOTS }
|
|
65455
|
-
});
|
|
65456
|
-
worker.unref();
|
|
65457
|
-
try {
|
|
65458
|
-
const waited = Atomics.wait(header, 0, VERDICT_PENDING, PROBE_TIMEOUT_MS + PROBE_WAIT_MARGIN_MS);
|
|
65459
|
-
const verdict = Atomics.load(header, 0);
|
|
65460
|
-
if (waited === "timed-out" || verdict === VERDICT_PENDING) {
|
|
65461
|
-
return { ok: false, reason: `the probe did not finish within ${String(PROBE_TIMEOUT_MS + PROBE_WAIT_MARGIN_MS)}ms` };
|
|
65462
|
-
}
|
|
65463
|
-
if (verdict === VERDICT_OK) {
|
|
65464
|
-
return { ok: true };
|
|
65465
|
-
}
|
|
65466
|
-
const length = Atomics.load(header, 1);
|
|
65467
|
-
return { ok: false, reason: Buffer.from(new Uint8Array(shared, HEADER_SLOTS * INT32_BYTES, length)).toString("utf8") };
|
|
65468
|
-
} finally {
|
|
65469
|
-
void worker.terminate();
|
|
65470
|
-
}
|
|
65471
|
-
}
|
|
65472
|
-
|
|
65473
65344
|
// src/codex/translate.ts
|
|
65474
|
-
var
|
|
65345
|
+
var import_node_crypto10 = require("node:crypto");
|
|
65475
65346
|
|
|
65476
65347
|
// src/codex/anthropic.ts
|
|
65477
65348
|
var TextBlockSchema = external_exports.looseObject({ type: external_exports.literal("text"), text: external_exports.string() });
|
|
@@ -65583,7 +65454,7 @@ function shortToolName(name, names) {
|
|
|
65583
65454
|
if (name.length <= CODEX_TOOL_NAME_LIMIT) {
|
|
65584
65455
|
return name;
|
|
65585
65456
|
}
|
|
65586
|
-
const hash2 = (0,
|
|
65457
|
+
const hash2 = (0, import_node_crypto10.createHash)("sha256").update(name).digest("hex").slice(0, TOOL_NAME_HASH_CHARS);
|
|
65587
65458
|
const shortened = `${name.slice(0, CODEX_TOOL_NAME_LIMIT - TOOL_NAME_HASH_CHARS - 1)}_${hash2}`;
|
|
65588
65459
|
names.set(shortened, name);
|
|
65589
65460
|
return shortened;
|
|
@@ -66424,38 +66295,6 @@ function createProviderRouteResolver(deps) {
|
|
|
66424
66295
|
return async (request6) => await Promise.resolve(resolve(request6));
|
|
66425
66296
|
}
|
|
66426
66297
|
|
|
66427
|
-
// src/frontdoor/trust.ts
|
|
66428
|
-
var import_node_crypto12 = require("node:crypto");
|
|
66429
|
-
var import_node_path30 = __toESM(require("node:path"), 1);
|
|
66430
|
-
var BUNDLE_NAME_HEX_DIGITS = 16;
|
|
66431
|
-
function resolveTrustBundle(params) {
|
|
66432
|
-
const { caCertFile, bundlesDir, inherited, fs: fs18 } = params;
|
|
66433
|
-
if (inherited === void 0 || inherited === "" || import_node_path30.default.resolve(inherited) === import_node_path30.default.resolve(caCertFile)) {
|
|
66434
|
-
return { path: caCertFile };
|
|
66435
|
-
}
|
|
66436
|
-
const caPem = fs18.readFileUtf8(caCertFile);
|
|
66437
|
-
let inheritedPem;
|
|
66438
|
-
try {
|
|
66439
|
-
inheritedPem = fs18.readFileUtf8(inherited);
|
|
66440
|
-
} catch (error62) {
|
|
66441
|
-
return {
|
|
66442
|
-
path: caCertFile,
|
|
66443
|
-
warning: `agent-shim: NODE_EXTRA_CA_CERTS names ${inherited}, which could not be read (${error62 instanceof Error ? error62.message : String(error62)}); the child trusts agent-shim's front-door CA on top of the system store, and nothing from that file`
|
|
66444
|
-
};
|
|
66445
|
-
}
|
|
66446
|
-
if (inheritedPem.includes(caPem.trim())) {
|
|
66447
|
-
return { path: inherited };
|
|
66448
|
-
}
|
|
66449
|
-
const combined = `${inheritedPem.trimEnd()}
|
|
66450
|
-
${caPem}`;
|
|
66451
|
-
const bundle = import_node_path30.default.join(bundlesDir, `${(0, import_node_crypto12.createHash)("sha256").update(combined).digest("hex").slice(0, BUNDLE_NAME_HEX_DIGITS)}.pem`);
|
|
66452
|
-
if (!fs18.exists(bundle)) {
|
|
66453
|
-
fs18.mkdirp(bundlesDir);
|
|
66454
|
-
fs18.writeFileAtomic(bundle, combined);
|
|
66455
|
-
}
|
|
66456
|
-
return { path: bundle };
|
|
66457
|
-
}
|
|
66458
|
-
|
|
66459
66298
|
// src/usage/providerQuota.ts
|
|
66460
66299
|
var ProviderQuotaError = class extends Error {
|
|
66461
66300
|
constructor(source, detail) {
|
|
@@ -66778,8 +66617,8 @@ function realFrontDoorSupervisorPorts(paths) {
|
|
|
66778
66617
|
now: () => Date.now(),
|
|
66779
66618
|
headroomPort: () => liveHeadroomPort(paths),
|
|
66780
66619
|
// The per-generation capability the direct listener demands: held only in this process's memory, so a loopback process that discovers the direct port still cannot use it.
|
|
66781
|
-
hopSecret: (0,
|
|
66782
|
-
custody: createCredentialCustody(() => (0,
|
|
66620
|
+
hopSecret: (0, import_node_crypto11.randomUUID)(),
|
|
66621
|
+
custody: createCredentialCustody(() => (0, import_node_crypto11.randomUUID)()),
|
|
66783
66622
|
log
|
|
66784
66623
|
});
|
|
66785
66624
|
let ca;
|
|
@@ -66816,14 +66655,14 @@ function realFrontDoorSupervisorPorts(paths) {
|
|
|
66816
66655
|
},
|
|
66817
66656
|
startConnectListener: async (preferredPort) => {
|
|
66818
66657
|
const transparentPort = process.env.AGENT_SHIM_TRANSPARENT_SURFACE === void 0 ? void 0 : Number(process.env.AGENT_SHIM_TRANSPARENT_SURFACE);
|
|
66819
|
-
const transparentCapability = transparentPort === void 0 || Number.isNaN(transparentPort) ? void 0 : (0,
|
|
66658
|
+
const transparentCapability = transparentPort === void 0 || Number.isNaN(transparentPort) ? void 0 : (0, import_node_crypto11.randomUUID)();
|
|
66820
66659
|
if (transparentCapability !== void 0) {
|
|
66821
66660
|
writeFrontDoorSession(realFarmFs, paths.frontdoorSessionsDir, { pid: process.pid, startedAt: Date.now(), token: transparentCapability });
|
|
66822
66661
|
}
|
|
66823
66662
|
const leafFor = createLeafCache(loadCa(), () => /* @__PURE__ */ new Date());
|
|
66824
66663
|
const capture = captureFromEnv(process.env, paths.logsDir);
|
|
66825
66664
|
if (capture !== void 0) {
|
|
66826
|
-
log(`capture enabled, recording CONNECT targets and piped exchanges to ${
|
|
66665
|
+
log(`capture enabled, recording CONNECT targets and piped exchanges to ${import_node_path30.default.join(paths.logsDir, "frontdoor-capture.jsonl")}`);
|
|
66827
66666
|
}
|
|
66828
66667
|
const server = await startConnectServer(
|
|
66829
66668
|
{
|
|
@@ -66879,49 +66718,6 @@ function realFrontDoorSupervisorPorts(paths) {
|
|
|
66879
66718
|
}
|
|
66880
66719
|
};
|
|
66881
66720
|
}
|
|
66882
|
-
var realTrustBundleFs = {
|
|
66883
|
-
readFileUtf8: (file2) => import_node_fs11.default.readFileSync(file2, "utf8"),
|
|
66884
|
-
exists: (file2) => import_node_fs11.default.existsSync(file2),
|
|
66885
|
-
mkdirp: (dir) => {
|
|
66886
|
-
import_node_fs11.default.mkdirSync(dir, { recursive: true });
|
|
66887
|
-
},
|
|
66888
|
-
writeFileAtomic: (file2, contents) => {
|
|
66889
|
-
realFarmFs.writeFilePrivate(file2, contents);
|
|
66890
|
-
}
|
|
66891
|
-
};
|
|
66892
|
-
function realFrontDoorPort(paths) {
|
|
66893
|
-
return {
|
|
66894
|
-
ensure: (inheritedExtraCaCerts) => {
|
|
66895
|
-
const up = ensureFrontDoor({
|
|
66896
|
-
paths,
|
|
66897
|
-
launcherPid: process.pid,
|
|
66898
|
-
ports: {
|
|
66899
|
-
fs: realFarmFs,
|
|
66900
|
-
isRunning: realIsProcessRunning,
|
|
66901
|
-
now: () => Date.now(),
|
|
66902
|
-
sleep: realSleepSync,
|
|
66903
|
-
spawnSupervisor: (layout) => spawnDetachedSupervisor(layout, "__frontdoor-supervisor", layout.frontdoorLogPath),
|
|
66904
|
-
stopSupervisor: (pid) => {
|
|
66905
|
-
process.kill(pid, "SIGTERM");
|
|
66906
|
-
},
|
|
66907
|
-
// Read fresh for each probe: a replacement supervisor may have regenerated an unparseable CA, and the probe must trust exactly what the serving door's leaf chains to.
|
|
66908
|
-
verifyListener: (port) => probeFrontDoorSync(port, import_node_fs11.default.readFileSync(paths.frontdoorCaCertFile, "utf8"))
|
|
66909
|
-
}
|
|
66910
|
-
});
|
|
66911
|
-
const trust = resolveTrustBundle({ caCertFile: paths.frontdoorCaCertFile, bundlesDir: paths.frontdoorCaBundlesDir, inherited: inheritedExtraCaCerts, fs: realTrustBundleFs });
|
|
66912
|
-
return {
|
|
66913
|
-
port: up.port,
|
|
66914
|
-
connectPort: up.connectPort,
|
|
66915
|
-
trustBundlePath: trust.path,
|
|
66916
|
-
...trust.warning === void 0 ? {} : { trustWarning: trust.warning },
|
|
66917
|
-
sessionToken: up.token
|
|
66918
|
-
};
|
|
66919
|
-
},
|
|
66920
|
-
release: () => {
|
|
66921
|
-
removeFrontDoorSession(realFarmFs, paths.frontdoorSessionsDir, process.pid);
|
|
66922
|
-
}
|
|
66923
|
-
};
|
|
66924
|
-
}
|
|
66925
66721
|
function collectFrontDoorStatus(fsPort, paths, isRunning) {
|
|
66926
66722
|
const state = readFrontDoorState(fsPort, paths.frontdoorStateFile) ?? {};
|
|
66927
66723
|
return {
|
|
@@ -67274,15 +67070,15 @@ var import_node_child_process4 = require("node:child_process");
|
|
|
67274
67070
|
|
|
67275
67071
|
// src/identityToken.ts
|
|
67276
67072
|
var import_node_fs12 = __toESM(require("node:fs"), 1);
|
|
67277
|
-
var
|
|
67073
|
+
var import_node_path31 = __toESM(require("node:path"), 1);
|
|
67278
67074
|
var TOKEN_FILE_MODE = 384;
|
|
67279
67075
|
var TOKEN_DIR_MODE = 448;
|
|
67280
67076
|
var OAUTH_TOKEN_RE = /^sk-ant-oat\d+-[A-Za-z0-9_-]+$/;
|
|
67281
67077
|
function identityTokenDir(paths) {
|
|
67282
|
-
return
|
|
67078
|
+
return import_node_path31.default.join(paths.root, "credentials");
|
|
67283
67079
|
}
|
|
67284
67080
|
function identityTokenPath(paths, identityName) {
|
|
67285
|
-
return
|
|
67081
|
+
return import_node_path31.default.join(identityTokenDir(paths), `${identityName}.token`);
|
|
67286
67082
|
}
|
|
67287
67083
|
function storeIdentityToken(paths, identityName, raw) {
|
|
67288
67084
|
const token = raw.trim();
|
|
@@ -67293,7 +67089,7 @@ function storeIdentityToken(paths, identityName, raw) {
|
|
|
67293
67089
|
import_node_fs12.default.mkdirSync(dir, { recursive: true, mode: TOKEN_DIR_MODE });
|
|
67294
67090
|
import_node_fs12.default.chmodSync(dir, TOKEN_DIR_MODE);
|
|
67295
67091
|
const target = identityTokenPath(paths, identityName);
|
|
67296
|
-
const temp =
|
|
67092
|
+
const temp = import_node_path31.default.join(dir, `.${identityName}.${String(process.pid)}.tmp`);
|
|
67297
67093
|
import_node_fs12.default.writeFileSync(temp, `${token}
|
|
67298
67094
|
`, { mode: TOKEN_FILE_MODE });
|
|
67299
67095
|
import_node_fs12.default.renameSync(temp, target);
|
|
@@ -67303,7 +67099,7 @@ function storeIdentityToken(paths, identityName, raw) {
|
|
|
67303
67099
|
// src/realCredentialCache.ts
|
|
67304
67100
|
var import_node_child_process3 = require("node:child_process");
|
|
67305
67101
|
var import_node_fs13 = __toESM(require("node:fs"), 1);
|
|
67306
|
-
var
|
|
67102
|
+
var import_node_path32 = __toESM(require("node:path"), 1);
|
|
67307
67103
|
var KEYCHAIN_CACHE_SERVICE = "agent-shim-credential-cache";
|
|
67308
67104
|
var CACHE_DIR_MODE = 448;
|
|
67309
67105
|
var CACHE_FILE_MODE = 384;
|
|
@@ -67330,8 +67126,8 @@ function security(args, stdin2) {
|
|
|
67330
67126
|
return { status: result.status, stdout: result.stdout };
|
|
67331
67127
|
}
|
|
67332
67128
|
function createRealCredentialCache(root) {
|
|
67333
|
-
const dir =
|
|
67334
|
-
const filePath = (owner) =>
|
|
67129
|
+
const dir = import_node_path32.default.join(root, "credential-cache");
|
|
67130
|
+
const filePath = (owner) => import_node_path32.default.join(dir, cacheFileNameFor(owner));
|
|
67335
67131
|
return {
|
|
67336
67132
|
read(store, owner) {
|
|
67337
67133
|
if (store === "file") {
|
|
@@ -67510,7 +67306,7 @@ function subjectName(positional, provider) {
|
|
|
67510
67306
|
|
|
67511
67307
|
// src/doctorReport.ts
|
|
67512
67308
|
var import_node_fs14 = __toESM(require("node:fs"), 1);
|
|
67513
|
-
var
|
|
67309
|
+
var import_node_path33 = __toESM(require("node:path"), 1);
|
|
67514
67310
|
|
|
67515
67311
|
// src/headroom/source.ts
|
|
67516
67312
|
var GIT_SPEC_RE = /git\+[^@\s]+(?:@([^\s#]+))?/;
|
|
@@ -67561,7 +67357,7 @@ function validateJson(schema, input2) {
|
|
|
67561
67357
|
return { ok: true, data: result.data };
|
|
67562
67358
|
}
|
|
67563
67359
|
function pushPathResolution(push, resolution) {
|
|
67564
|
-
const ownDir =
|
|
67360
|
+
const ownDir = import_node_path33.default.dirname(resolution.ownExecutablePath);
|
|
67565
67361
|
switch (resolution.agentShim.status) {
|
|
67566
67362
|
case "ok":
|
|
67567
67363
|
push("path-resolution", "pass", `\`agent-shim\` on PATH resolves to this running executable, ${resolution.ownExecutablePath}.`, "agent-shim");
|
|
@@ -67629,14 +67425,14 @@ function pushProvider(push, entry) {
|
|
|
67629
67425
|
}
|
|
67630
67426
|
function pushLegacyName(push, params) {
|
|
67631
67427
|
const legacyVariables = Object.keys(params.env).filter((name) => name.startsWith(LEGACY_ENV_PREFIX) && params.env[name] !== void 0).sort();
|
|
67632
|
-
if (legacyVariables.length === 0 &&
|
|
67428
|
+
if (legacyVariables.length === 0 && import_node_path33.default.basename(params.rootPath) !== LEGACY_HOME_DIRNAME) {
|
|
67633
67429
|
push("legacy-name", "pass", "Nothing here still uses the former `claude-use` name.");
|
|
67634
67430
|
return;
|
|
67635
67431
|
}
|
|
67636
67432
|
for (const name of legacyVariables) {
|
|
67637
67433
|
push("legacy-name", "warn", `${name} is the former name of ${name.replace(LEGACY_ENV_PREFIX, ENV_PREFIX)}. Rename it; the old name still works but is only a fallback.`, name);
|
|
67638
67434
|
}
|
|
67639
|
-
if (
|
|
67435
|
+
if (import_node_path33.default.basename(params.rootPath) === LEGACY_HOME_DIRNAME) {
|
|
67640
67436
|
push(
|
|
67641
67437
|
"legacy-name",
|
|
67642
67438
|
"warn",
|
|
@@ -67944,18 +67740,18 @@ function collectDoctorReport(params) {
|
|
|
67944
67740
|
const { paths } = params;
|
|
67945
67741
|
const identityNames = import_node_fs14.default.existsSync(paths.identitiesDir) ? import_node_fs14.default.readdirSync(paths.identitiesDir, { withFileTypes: true }).filter((entry) => entry.isDirectory() && isIdentityDirectoryName(entry.name)).map((entry) => entry.name).sort() : [];
|
|
67946
67742
|
const identities = identityNames.map((name) => {
|
|
67947
|
-
const farmRoot =
|
|
67948
|
-
const identityPath =
|
|
67743
|
+
const farmRoot = import_node_path33.default.join(paths.identitiesDir, name);
|
|
67744
|
+
const identityPath = import_node_path33.default.join(farmRoot, "identity.json");
|
|
67949
67745
|
return { name, path: identityPath, raw: realFsPort.readFileUtf8(identityPath), farmRoot };
|
|
67950
67746
|
});
|
|
67951
67747
|
const profileNames = import_node_fs14.default.existsSync(paths.configProfilesDir) ? import_node_fs14.default.readdirSync(paths.configProfilesDir, { withFileTypes: true }).filter((entry) => entry.isFile() && entry.name.endsWith(".json")).map((entry) => entry.name.slice(0, -".json".length)).sort() : [];
|
|
67952
67748
|
const configProfiles = profileNames.map((name) => {
|
|
67953
|
-
const profilePath =
|
|
67749
|
+
const profilePath = import_node_path33.default.join(paths.configProfilesDir, `${name}.json`);
|
|
67954
67750
|
return { name, path: profilePath, raw: realFsPort.readFileUtf8(profilePath) };
|
|
67955
67751
|
});
|
|
67956
67752
|
const providerNames = import_node_fs14.default.existsSync(paths.providersDir) ? import_node_fs14.default.readdirSync(paths.providersDir, { withFileTypes: true }).filter((entry) => entry.isFile() && entry.name.endsWith(".json")).map((entry) => entry.name.slice(0, -".json".length)).sort() : [];
|
|
67957
67753
|
const providers = providerNames.map((name) => {
|
|
67958
|
-
const providerPath =
|
|
67754
|
+
const providerPath = import_node_path33.default.join(paths.providersDir, `${name}.json`);
|
|
67959
67755
|
return { name, path: providerPath, raw: realFsPort.readFileUtf8(providerPath) };
|
|
67960
67756
|
});
|
|
67961
67757
|
const ownExecutablePath = realOwnExecutablePath();
|
|
@@ -67967,12 +67763,12 @@ function collectDoctorReport(params) {
|
|
|
67967
67763
|
binaryDiscovery = { ok: false, message: error62 instanceof Error ? error62.message : String(error62) };
|
|
67968
67764
|
}
|
|
67969
67765
|
const shimState = readJson(paths.claudeShimFile, ClaudeShimStateSchema);
|
|
67970
|
-
const pathDirs = (params.env.PATH ?? "").split(
|
|
67766
|
+
const pathDirs = (params.env.PATH ?? "").split(import_node_path33.default.delimiter).filter((dir) => dir !== "");
|
|
67971
67767
|
const claudeShimShadow = shimState === void 0 ? void 0 : refinePathShadow(
|
|
67972
67768
|
findPathShadow({
|
|
67973
67769
|
pathDirs,
|
|
67974
|
-
targetDir:
|
|
67975
|
-
targetFilename:
|
|
67770
|
+
targetDir: import_node_path33.default.dirname(shimState.targetPath),
|
|
67771
|
+
targetFilename: import_node_path33.default.basename(shimState.targetPath),
|
|
67976
67772
|
findExecutableInDir
|
|
67977
67773
|
}),
|
|
67978
67774
|
shimState.targetPath,
|
|
@@ -67995,7 +67791,7 @@ function collectDoctorReport(params) {
|
|
|
67995
67791
|
agentShim: refinePathShadow(
|
|
67996
67792
|
findPathShadow({
|
|
67997
67793
|
pathDirs,
|
|
67998
|
-
targetDir:
|
|
67794
|
+
targetDir: import_node_path33.default.dirname(ownExecutablePath),
|
|
67999
67795
|
targetFilename: commandFilename(ownExecutablePath, "agent-shim"),
|
|
68000
67796
|
findExecutableInDir
|
|
68001
67797
|
}),
|
|
@@ -68038,11 +67834,11 @@ function registerDoctorCommand(program2, deps) {
|
|
|
68038
67834
|
// src/headroom/commands.ts
|
|
68039
67835
|
var import_node_fs15 = __toESM(require("node:fs"), 1);
|
|
68040
67836
|
var import_node_net3 = __toESM(require("node:net"), 1);
|
|
68041
|
-
var
|
|
67837
|
+
var import_node_path34 = __toESM(require("node:path"), 1);
|
|
68042
67838
|
var import_node_child_process5 = require("node:child_process");
|
|
68043
67839
|
|
|
68044
67840
|
// src/headroom/settings.ts
|
|
68045
|
-
var
|
|
67841
|
+
var import_node_crypto12 = require("node:crypto");
|
|
68046
67842
|
function settingsOf(config2) {
|
|
68047
67843
|
return {
|
|
68048
67844
|
...config2.mode === void 0 ? {} : { mode: config2.mode },
|
|
@@ -68067,7 +67863,7 @@ function settingsEnv(settings2) {
|
|
|
68067
67863
|
return settings2.rolloutChannel === void 0 ? {} : { HEADROOM_ROLLOUT_CHANNEL: settings2.rolloutChannel };
|
|
68068
67864
|
}
|
|
68069
67865
|
function hashSettings(settings2) {
|
|
68070
|
-
return (0,
|
|
67866
|
+
return (0, import_node_crypto12.createHash)("sha256").update(JSON.stringify([...settingsArgs(settings2), JSON.stringify(settingsEnv(settings2))])).digest("hex");
|
|
68071
67867
|
}
|
|
68072
67868
|
|
|
68073
67869
|
// src/headroom/supervisor.ts
|
|
@@ -68169,9 +67965,9 @@ function compareVersions2(a2, b) {
|
|
|
68169
67965
|
}
|
|
68170
67966
|
var HEADROOM_SUPERVISOR_STILL_RUNNING = -1;
|
|
68171
67967
|
async function runSupervisor(initialConfig, ports, options = {}) {
|
|
68172
|
-
const { fs:
|
|
67968
|
+
const { fs: fs19, paths } = ports;
|
|
68173
67969
|
let config2 = initialConfig;
|
|
68174
|
-
const previousState = readHeadroomState(
|
|
67970
|
+
const previousState = readHeadroomState(fs19, paths.headroomStateFile);
|
|
68175
67971
|
if (previousState?.supervisorPid !== void 0 && previousState.supervisorPid !== ports.ownPid && ports.isRunning(previousState.supervisorPid)) {
|
|
68176
67972
|
ports.log(`agent-shim headroom supervisor ${String(ports.ownPid)}: supervisor ${String(previousState.supervisorPid)} already owns the daemon; exiting`);
|
|
68177
67973
|
return 0;
|
|
@@ -68180,19 +67976,19 @@ async function runSupervisor(initialConfig, ports, options = {}) {
|
|
|
68180
67976
|
let lastPort = previousState?.lastPort;
|
|
68181
67977
|
const sticky = () => lastPort === void 0 ? {} : { lastPort };
|
|
68182
67978
|
const ownsState = () => {
|
|
68183
|
-
const owner = readHeadroomState(
|
|
67979
|
+
const owner = readHeadroomState(fs19, paths.headroomStateFile)?.supervisorPid;
|
|
68184
67980
|
return owner === void 0 || owner === ports.ownPid || !ports.isRunning(owner);
|
|
68185
67981
|
};
|
|
68186
67982
|
const writeOwnState = (state) => {
|
|
68187
67983
|
if (!ownsState()) {
|
|
68188
67984
|
return false;
|
|
68189
67985
|
}
|
|
68190
|
-
writeHeadroomState(
|
|
67986
|
+
writeHeadroomState(fs19, paths.headroomStateFile, state);
|
|
68191
67987
|
return true;
|
|
68192
67988
|
};
|
|
68193
67989
|
const fail = (message) => {
|
|
68194
67990
|
if (writeOwnState({ lastError: message, ...sticky() })) {
|
|
68195
|
-
|
|
67991
|
+
fs19.removeRecursive(paths.headroomLockFile);
|
|
68196
67992
|
}
|
|
68197
67993
|
ports.log(`agent-shim headroom supervisor: ${message}`);
|
|
68198
67994
|
return 1;
|
|
@@ -68241,7 +68037,7 @@ async function runSupervisor(initialConfig, ports, options = {}) {
|
|
|
68241
68037
|
const settingsHash = hashSettings(config2.settings);
|
|
68242
68038
|
const allowlist = allowlistOf(ports);
|
|
68243
68039
|
const allowlistHash = hashAllowlist(allowlist);
|
|
68244
|
-
pruneDeadSessions(
|
|
68040
|
+
pruneDeadSessions(fs19, paths.headroomSessionsDir, ports.isRunning);
|
|
68245
68041
|
const ownsNow = ownsState();
|
|
68246
68042
|
if (!ownsNow && !superseded) {
|
|
68247
68043
|
ports.log("agent-shim headroom supervisor: another supervisor now owns state.json; serving this daemon's own sessions until they end");
|
|
@@ -68293,7 +68089,7 @@ async function runSupervisor(initialConfig, ports, options = {}) {
|
|
|
68293
68089
|
...sticky()
|
|
68294
68090
|
});
|
|
68295
68091
|
if (recorded) {
|
|
68296
|
-
|
|
68092
|
+
fs19.removeRecursive(paths.headroomLockFile);
|
|
68297
68093
|
}
|
|
68298
68094
|
idleSince = void 0;
|
|
68299
68095
|
ports.log(`agent-shim headroom supervisor: headroom pid ${String(pid)} ready on 127.0.0.1:${String(port)}`);
|
|
@@ -68308,7 +68104,7 @@ async function runSupervisor(initialConfig, ports, options = {}) {
|
|
|
68308
68104
|
}
|
|
68309
68105
|
} else {
|
|
68310
68106
|
if (runningHash !== allowlistHash || runningSettingsHash !== settingsHash || installedSource !== config2.source) {
|
|
68311
|
-
if (sessionsForSupervisor(
|
|
68107
|
+
if (sessionsForSupervisor(fs19, paths.headroomSessionsDir, ports.ownPid).length === 0) {
|
|
68312
68108
|
ports.log("agent-shim headroom supervisor: configuration drifted and no sessions are live; restarting headroom");
|
|
68313
68109
|
ports.stopProcess(headroomPid);
|
|
68314
68110
|
headroomPid = void 0;
|
|
@@ -68317,7 +68113,7 @@ async function runSupervisor(initialConfig, ports, options = {}) {
|
|
|
68317
68113
|
continue;
|
|
68318
68114
|
}
|
|
68319
68115
|
}
|
|
68320
|
-
const sessions = sessionsForSupervisor(
|
|
68116
|
+
const sessions = sessionsForSupervisor(fs19, paths.headroomSessionsDir, ports.ownPid);
|
|
68321
68117
|
if (sessions.length === 0) {
|
|
68322
68118
|
idleSince ??= ports.now();
|
|
68323
68119
|
const idleMs = ports.now() - idleSince;
|
|
@@ -68339,9 +68135,9 @@ async function runSupervisor(initialConfig, ports, options = {}) {
|
|
|
68339
68135
|
await ports.sleep(HEADROOM_POLL_MS);
|
|
68340
68136
|
}
|
|
68341
68137
|
}
|
|
68342
|
-
function currentHeadroomAllowlist(
|
|
68343
|
-
const providers = readAllProviders(
|
|
68344
|
-
return headroomAllowlist(headroomUpstreams(providers, frontDoorOrigin(readFrontDoorState(
|
|
68138
|
+
function currentHeadroomAllowlist(fs19, paths) {
|
|
68139
|
+
const providers = readAllProviders(fs19, paths.providersDir).map((entry) => entry.provider);
|
|
68140
|
+
return headroomAllowlist(headroomUpstreams(providers, frontDoorOrigin(readFrontDoorState(fs19, paths.frontdoorStateFile))));
|
|
68345
68141
|
}
|
|
68346
68142
|
function allowlistOf(ports) {
|
|
68347
68143
|
return currentHeadroomAllowlist(ports.fs, ports.paths);
|
|
@@ -68453,12 +68249,12 @@ function readInstalledHeadroomCommit() {
|
|
|
68453
68249
|
if (toolDir.status !== 0) {
|
|
68454
68250
|
return void 0;
|
|
68455
68251
|
}
|
|
68456
|
-
const libDir =
|
|
68252
|
+
const libDir = import_node_path34.default.join(toolDir.stdout.trim(), HEADROOM_TOOL_NAME, "lib");
|
|
68457
68253
|
for (const python of realFarmFs.readdir(libDir)) {
|
|
68458
|
-
const sitePackages =
|
|
68254
|
+
const sitePackages = import_node_path34.default.join(libDir, python, "site-packages");
|
|
68459
68255
|
for (const entry of realFarmFs.readdir(sitePackages)) {
|
|
68460
68256
|
if (entry.startsWith("headroom_ai-") && entry.endsWith(".dist-info")) {
|
|
68461
|
-
const directUrl = realFarmFs.readFileUtf8(
|
|
68257
|
+
const directUrl = realFarmFs.readFileUtf8(import_node_path34.default.join(sitePackages, entry, "direct_url.json"));
|
|
68462
68258
|
return directUrl === void 0 ? void 0 : parseInstalledCommit(directUrl);
|
|
68463
68259
|
}
|
|
68464
68260
|
}
|
|
@@ -68988,14 +68784,14 @@ function requireIdentity(paths, name) {
|
|
|
68988
68784
|
throw new IdentityNotFoundError(name);
|
|
68989
68785
|
}
|
|
68990
68786
|
}
|
|
68991
|
-
function collectUsageReport(
|
|
68787
|
+
function collectUsageReport(fs19, paths, filters) {
|
|
68992
68788
|
const { identity, provider, sinceMs } = filters;
|
|
68993
68789
|
if (identity !== void 0) {
|
|
68994
68790
|
requireIdentity(paths, identity);
|
|
68995
68791
|
}
|
|
68996
|
-
const log = readUsageLog(
|
|
68792
|
+
const log = readUsageLog(fs19, paths.usageLogDir, sinceMs === void 0 ? {} : { sinceMs });
|
|
68997
68793
|
const records = log.records.filter((record2) => (identity === void 0 || record2.identity === identity) && (provider === void 0 || record2.provider === provider));
|
|
68998
|
-
const snapshots = (identity === void 0 ? listUsageSnapshots(
|
|
68794
|
+
const snapshots = (identity === void 0 ? listUsageSnapshots(fs19, paths.usageSnapshotsDir) : [readUsageSnapshot(fs19, paths.usageSnapshotsDir, identity)].flatMap((snapshot) => snapshot === void 0 ? [] : [snapshot])).map((snapshot) => provider === void 0 ? snapshot : { ...snapshot, providers: Object.fromEntries(Object.entries(snapshot.providers).filter(([name]) => name === provider)) }).filter((snapshot) => Object.keys(snapshot.providers).length > 0);
|
|
68999
68795
|
return {
|
|
69000
68796
|
since: sinceMs === void 0 ? void 0 : new Date(sinceMs).toISOString(),
|
|
69001
68797
|
logDir: paths.usageLogDir,
|
|
@@ -69091,15 +68887,15 @@ function formatUsageReport(report) {
|
|
|
69091
68887
|
}
|
|
69092
68888
|
return lines;
|
|
69093
68889
|
}
|
|
69094
|
-
function collectAccounts(
|
|
68890
|
+
function collectAccounts(fs19, paths, identity) {
|
|
69095
68891
|
if (identity !== void 0) {
|
|
69096
68892
|
requireIdentity(paths, identity);
|
|
69097
68893
|
}
|
|
69098
68894
|
const names = identity === void 0 ? listIdentities(paths).map((entry) => entry.name) : [identity];
|
|
69099
68895
|
return names.map((name) => ({
|
|
69100
68896
|
identity: name,
|
|
69101
|
-
account: readAccountMetadata(
|
|
69102
|
-
usage: readUsageSnapshot(
|
|
68897
|
+
account: readAccountMetadata(fs19, paths.identitiesDir, name),
|
|
68898
|
+
usage: readUsageSnapshot(fs19, paths.usageSnapshotsDir, name)
|
|
69103
68899
|
}));
|
|
69104
68900
|
}
|
|
69105
68901
|
function formatAccounts(views) {
|
|
@@ -69260,10 +69056,228 @@ function buildProgram(deps) {
|
|
|
69260
69056
|
return program2;
|
|
69261
69057
|
}
|
|
69262
69058
|
|
|
69263
|
-
// src/
|
|
69059
|
+
// src/launchWiring.ts
|
|
69264
69060
|
var import_node_os8 = __toESM(require("node:os"), 1);
|
|
69265
69061
|
var import_node_crypto16 = require("node:crypto");
|
|
69266
69062
|
|
|
69063
|
+
// src/frontdoor/realFrontDoorPort.ts
|
|
69064
|
+
var import_node_fs16 = __toESM(require("node:fs"), 1);
|
|
69065
|
+
|
|
69066
|
+
// src/frontdoor/ensure.ts
|
|
69067
|
+
var import_node_crypto13 = require("node:crypto");
|
|
69068
|
+
var FRONTDOOR_START_TIMEOUT_MS = 3e4;
|
|
69069
|
+
var FRONTDOOR_LAUNCHER_POLL_MS = 100;
|
|
69070
|
+
var FrontDoorStartError = class extends CliError {
|
|
69071
|
+
constructor(message) {
|
|
69072
|
+
super(message);
|
|
69073
|
+
this.name = "FrontDoorStartError";
|
|
69074
|
+
}
|
|
69075
|
+
};
|
|
69076
|
+
function ensureFrontDoor(params) {
|
|
69077
|
+
const { paths, ports } = params;
|
|
69078
|
+
const deadline = ports.now() + FRONTDOOR_START_TIMEOUT_MS;
|
|
69079
|
+
let spawned = false;
|
|
69080
|
+
let replaced = false;
|
|
69081
|
+
const distrusted = /* @__PURE__ */ new Set();
|
|
69082
|
+
let lastRejection;
|
|
69083
|
+
for (; ; ) {
|
|
69084
|
+
const state = readFrontDoorState(ports.fs, paths.frontdoorStateFile);
|
|
69085
|
+
const supervisorPid = state?.supervisorPid;
|
|
69086
|
+
if (supervisorPid !== void 0 && !distrusted.has(supervisorPid) && ports.isRunning(supervisorPid)) {
|
|
69087
|
+
if (state?.port !== void 0 && state.connectPort !== void 0) {
|
|
69088
|
+
const verdict = ports.verifyListener(state.port);
|
|
69089
|
+
if (verdict.ok && (state.protocol ?? 0) < FRONT_DOOR_PROTOCOL && !replaced) {
|
|
69090
|
+
ports.stopSupervisor(supervisorPid);
|
|
69091
|
+
while (ports.isRunning(supervisorPid)) {
|
|
69092
|
+
if (ports.now() >= deadline) {
|
|
69093
|
+
throw new FrontDoorStartError(`agent-shim: the front door (pid ${String(supervisorPid)}, protocol ${String(state.protocol ?? 0)}) is older than this launcher (protocol ${String(FRONT_DOOR_PROTOCOL)}) and did not exit when asked. Daemon log: ${paths.frontdoorLogPath}`);
|
|
69094
|
+
}
|
|
69095
|
+
ports.sleep(FRONTDOOR_LAUNCHER_POLL_MS);
|
|
69096
|
+
}
|
|
69097
|
+
replaced = true;
|
|
69098
|
+
continue;
|
|
69099
|
+
}
|
|
69100
|
+
if (verdict.ok) {
|
|
69101
|
+
const token = (0, import_node_crypto13.randomUUID)();
|
|
69102
|
+
writeFrontDoorSession(ports.fs, paths.frontdoorSessionsDir, { pid: params.launcherPid, startedAt: ports.now(), token });
|
|
69103
|
+
return { port: state.port, connectPort: state.connectPort, token };
|
|
69104
|
+
}
|
|
69105
|
+
lastRejection = `the listener on 127.0.0.1:${String(state.port)} (recorded for pid ${String(supervisorPid)}) failed authentication: ${verdict.reason}`;
|
|
69106
|
+
if (spawned) {
|
|
69107
|
+
throw new FrontDoorStartError(`agent-shim: ${lastRejection}; refusing to send this session's credentials to it. Daemon log: ${paths.frontdoorLogPath}`);
|
|
69108
|
+
}
|
|
69109
|
+
distrusted.add(supervisorPid);
|
|
69110
|
+
continue;
|
|
69111
|
+
}
|
|
69112
|
+
if (state?.lastError !== void 0) {
|
|
69113
|
+
throw new FrontDoorStartError(`agent-shim: the front door reported a fatal error and is not serving: ${state.lastError} (daemon log: ${paths.frontdoorLogPath})`);
|
|
69114
|
+
}
|
|
69115
|
+
} else if (!spawned) {
|
|
69116
|
+
ports.fs.mkdirp(paths.frontdoorDir);
|
|
69117
|
+
const created = ports.fs.writeFileExclusive(paths.frontdoorLockFile, `${JSON.stringify({ pid: params.launcherPid, at: ports.now() })}
|
|
69118
|
+
`);
|
|
69119
|
+
if (created) {
|
|
69120
|
+
ports.spawnSupervisor(paths);
|
|
69121
|
+
spawned = true;
|
|
69122
|
+
} else {
|
|
69123
|
+
const lock = readStartLock(ports.fs, paths.frontdoorLockFile);
|
|
69124
|
+
if (lock === void 0 || !ports.isRunning(lock.pid)) {
|
|
69125
|
+
ports.fs.removeRecursive(paths.frontdoorLockFile);
|
|
69126
|
+
continue;
|
|
69127
|
+
}
|
|
69128
|
+
}
|
|
69129
|
+
}
|
|
69130
|
+
if (ports.now() >= deadline) {
|
|
69131
|
+
const lastError = state?.lastError;
|
|
69132
|
+
throw new FrontDoorStartError(
|
|
69133
|
+
`agent-shim: the front door did not become ready within ${String(FRONTDOOR_START_TIMEOUT_MS)}ms` + (lastError === void 0 ? "" : ` (last error: ${lastError})`) + (lastRejection === void 0 ? "" : ` (${lastRejection})`) + `. Daemon log: ${paths.frontdoorLogPath}`
|
|
69134
|
+
);
|
|
69135
|
+
}
|
|
69136
|
+
ports.sleep(FRONTDOOR_LAUNCHER_POLL_MS);
|
|
69137
|
+
}
|
|
69138
|
+
}
|
|
69139
|
+
|
|
69140
|
+
// src/frontdoor/probe.ts
|
|
69141
|
+
var import_node_worker_threads = require("node:worker_threads");
|
|
69142
|
+
var PROBE_TIMEOUT_MS = 2e3;
|
|
69143
|
+
var PROBE_WAIT_MARGIN_MS = 1e3;
|
|
69144
|
+
var REASON_BYTES = 1024;
|
|
69145
|
+
var HEADER_SLOTS = 2;
|
|
69146
|
+
var INT32_BYTES = 4;
|
|
69147
|
+
var VERDICT_PENDING = 0;
|
|
69148
|
+
var VERDICT_OK = 1;
|
|
69149
|
+
var VERDICT_FAILED = 2;
|
|
69150
|
+
var PROBE_SOURCE = `
|
|
69151
|
+
const { workerData } = require("node:worker_threads");
|
|
69152
|
+
const https = require("node:https");
|
|
69153
|
+
const { port, ca, timeoutMs, shared, ok, failed, headerSlots } = workerData;
|
|
69154
|
+
const header = new Int32Array(shared, 0, headerSlots);
|
|
69155
|
+
const reason = new Uint8Array(shared, headerSlots * 4);
|
|
69156
|
+
let done = false;
|
|
69157
|
+
const finish = (verdict, message) => {
|
|
69158
|
+
if (done) return;
|
|
69159
|
+
done = true;
|
|
69160
|
+
const bytes = Buffer.from(String(message), "utf8").subarray(0, reason.length);
|
|
69161
|
+
reason.set(bytes);
|
|
69162
|
+
Atomics.store(header, 1, bytes.length);
|
|
69163
|
+
Atomics.store(header, 0, verdict);
|
|
69164
|
+
Atomics.notify(header, 0);
|
|
69165
|
+
};
|
|
69166
|
+
const request = https.get({ host: "127.0.0.1", port, path: "/healthz", ca: [ca], agent: false }, (response) => {
|
|
69167
|
+
let body = "";
|
|
69168
|
+
response.setEncoding("utf8");
|
|
69169
|
+
response.on("data", (chunk) => { body += chunk; });
|
|
69170
|
+
response.on("end", () => {
|
|
69171
|
+
if (response.statusCode === 200 && body === "ok") finish(ok, "");
|
|
69172
|
+
else finish(failed, "the listener answered its health probe with HTTP " + String(response.statusCode));
|
|
69173
|
+
});
|
|
69174
|
+
response.on("error", (error) => finish(failed, error.message));
|
|
69175
|
+
});
|
|
69176
|
+
// A hard deadline over the whole exchange, handshake included: a listener that accepts the connection and then says nothing must still fail the probe.
|
|
69177
|
+
setTimeout(() => { request.destroy(new Error("no answer within " + String(timeoutMs) + "ms")); }, timeoutMs);
|
|
69178
|
+
request.on("error", (error) => finish(failed, (error.code ? error.code + ": " : "") + error.message));
|
|
69179
|
+
`;
|
|
69180
|
+
function probeFrontDoorSync(port, caPem) {
|
|
69181
|
+
const shared = new SharedArrayBuffer(HEADER_SLOTS * INT32_BYTES + REASON_BYTES);
|
|
69182
|
+
const header = new Int32Array(shared, 0, HEADER_SLOTS);
|
|
69183
|
+
const worker = new import_node_worker_threads.Worker(PROBE_SOURCE, {
|
|
69184
|
+
eval: true,
|
|
69185
|
+
workerData: { port, ca: caPem, timeoutMs: PROBE_TIMEOUT_MS, shared, ok: VERDICT_OK, failed: VERDICT_FAILED, headerSlots: HEADER_SLOTS }
|
|
69186
|
+
});
|
|
69187
|
+
worker.unref();
|
|
69188
|
+
try {
|
|
69189
|
+
const waited = Atomics.wait(header, 0, VERDICT_PENDING, PROBE_TIMEOUT_MS + PROBE_WAIT_MARGIN_MS);
|
|
69190
|
+
const verdict = Atomics.load(header, 0);
|
|
69191
|
+
if (waited === "timed-out" || verdict === VERDICT_PENDING) {
|
|
69192
|
+
return { ok: false, reason: `the probe did not finish within ${String(PROBE_TIMEOUT_MS + PROBE_WAIT_MARGIN_MS)}ms` };
|
|
69193
|
+
}
|
|
69194
|
+
if (verdict === VERDICT_OK) {
|
|
69195
|
+
return { ok: true };
|
|
69196
|
+
}
|
|
69197
|
+
const length = Atomics.load(header, 1);
|
|
69198
|
+
return { ok: false, reason: Buffer.from(new Uint8Array(shared, HEADER_SLOTS * INT32_BYTES, length)).toString("utf8") };
|
|
69199
|
+
} finally {
|
|
69200
|
+
void worker.terminate();
|
|
69201
|
+
}
|
|
69202
|
+
}
|
|
69203
|
+
|
|
69204
|
+
// src/frontdoor/trust.ts
|
|
69205
|
+
var import_node_crypto14 = require("node:crypto");
|
|
69206
|
+
var import_node_path35 = __toESM(require("node:path"), 1);
|
|
69207
|
+
var BUNDLE_NAME_HEX_DIGITS = 16;
|
|
69208
|
+
function resolveTrustBundle(params) {
|
|
69209
|
+
const { caCertFile, bundlesDir, inherited, fs: fs19 } = params;
|
|
69210
|
+
if (inherited === void 0 || inherited === "" || import_node_path35.default.resolve(inherited) === import_node_path35.default.resolve(caCertFile)) {
|
|
69211
|
+
return { path: caCertFile };
|
|
69212
|
+
}
|
|
69213
|
+
const caPem = fs19.readFileUtf8(caCertFile);
|
|
69214
|
+
let inheritedPem;
|
|
69215
|
+
try {
|
|
69216
|
+
inheritedPem = fs19.readFileUtf8(inherited);
|
|
69217
|
+
} catch (error62) {
|
|
69218
|
+
return {
|
|
69219
|
+
path: caCertFile,
|
|
69220
|
+
warning: `agent-shim: NODE_EXTRA_CA_CERTS names ${inherited}, which could not be read (${error62 instanceof Error ? error62.message : String(error62)}); the child trusts agent-shim's front-door CA on top of the system store, and nothing from that file`
|
|
69221
|
+
};
|
|
69222
|
+
}
|
|
69223
|
+
if (inheritedPem.includes(caPem.trim())) {
|
|
69224
|
+
return { path: inherited };
|
|
69225
|
+
}
|
|
69226
|
+
const combined = `${inheritedPem.trimEnd()}
|
|
69227
|
+
${caPem}`;
|
|
69228
|
+
const bundle = import_node_path35.default.join(bundlesDir, `${(0, import_node_crypto14.createHash)("sha256").update(combined).digest("hex").slice(0, BUNDLE_NAME_HEX_DIGITS)}.pem`);
|
|
69229
|
+
if (!fs19.exists(bundle)) {
|
|
69230
|
+
fs19.mkdirp(bundlesDir);
|
|
69231
|
+
fs19.writeFileAtomic(bundle, combined);
|
|
69232
|
+
}
|
|
69233
|
+
return { path: bundle };
|
|
69234
|
+
}
|
|
69235
|
+
|
|
69236
|
+
// src/frontdoor/realFrontDoorPort.ts
|
|
69237
|
+
var realTrustBundleFs = {
|
|
69238
|
+
readFileUtf8: (file2) => import_node_fs16.default.readFileSync(file2, "utf8"),
|
|
69239
|
+
exists: (file2) => import_node_fs16.default.existsSync(file2),
|
|
69240
|
+
mkdirp: (dir) => {
|
|
69241
|
+
import_node_fs16.default.mkdirSync(dir, { recursive: true });
|
|
69242
|
+
},
|
|
69243
|
+
writeFileAtomic: (file2, contents) => {
|
|
69244
|
+
realFarmFs.writeFilePrivate(file2, contents);
|
|
69245
|
+
}
|
|
69246
|
+
};
|
|
69247
|
+
function realFrontDoorPort(paths, spawnDaemon) {
|
|
69248
|
+
return {
|
|
69249
|
+
ensure: (inheritedExtraCaCerts) => {
|
|
69250
|
+
const up = ensureFrontDoor({
|
|
69251
|
+
paths,
|
|
69252
|
+
launcherPid: process.pid,
|
|
69253
|
+
ports: {
|
|
69254
|
+
fs: realFarmFs,
|
|
69255
|
+
isRunning: realIsProcessRunning,
|
|
69256
|
+
now: () => Date.now(),
|
|
69257
|
+
sleep: realSleepSync,
|
|
69258
|
+
spawnSupervisor: (layout) => spawnDaemon(layout, "__frontdoor-supervisor", layout.frontdoorLogPath),
|
|
69259
|
+
stopSupervisor: (pid) => {
|
|
69260
|
+
process.kill(pid, "SIGTERM");
|
|
69261
|
+
},
|
|
69262
|
+
// Read fresh for each probe: a replacement supervisor may have regenerated an unparseable CA, and the probe must trust exactly what the serving door's leaf chains to.
|
|
69263
|
+
verifyListener: (port) => probeFrontDoorSync(port, import_node_fs16.default.readFileSync(paths.frontdoorCaCertFile, "utf8"))
|
|
69264
|
+
}
|
|
69265
|
+
});
|
|
69266
|
+
const trust = resolveTrustBundle({ caCertFile: paths.frontdoorCaCertFile, bundlesDir: paths.frontdoorCaBundlesDir, inherited: inheritedExtraCaCerts, fs: realTrustBundleFs });
|
|
69267
|
+
return {
|
|
69268
|
+
port: up.port,
|
|
69269
|
+
connectPort: up.connectPort,
|
|
69270
|
+
trustBundlePath: trust.path,
|
|
69271
|
+
...trust.warning === void 0 ? {} : { trustWarning: trust.warning },
|
|
69272
|
+
sessionToken: up.token
|
|
69273
|
+
};
|
|
69274
|
+
},
|
|
69275
|
+
release: () => {
|
|
69276
|
+
removeFrontDoorSession(realFarmFs, paths.frontdoorSessionsDir, process.pid);
|
|
69277
|
+
}
|
|
69278
|
+
};
|
|
69279
|
+
}
|
|
69280
|
+
|
|
69267
69281
|
// src/launcher.ts
|
|
69268
69282
|
var import_node_crypto15 = require("node:crypto");
|
|
69269
69283
|
var import_node_path37 = __toESM(require("node:path"), 1);
|
|
@@ -69512,7 +69526,7 @@ function resolveIdentityCredential(identity, credential, env, port) {
|
|
|
69512
69526
|
return resolution.ok ? resolution : { ok: false, status: CREDENTIAL_UNAVAILABLE_EXIT, message: resolution.message };
|
|
69513
69527
|
}
|
|
69514
69528
|
function pickFromPool(decided, params, parsedArgv) {
|
|
69515
|
-
const { paths, fs:
|
|
69529
|
+
const { paths, fs: fs19, proc, log } = params;
|
|
69516
69530
|
if (decided.pool === void 0) {
|
|
69517
69531
|
return { identityDecision: decided };
|
|
69518
69532
|
}
|
|
@@ -69526,7 +69540,7 @@ function pickFromPool(decided, params, parsedArgv) {
|
|
|
69526
69540
|
selectedVia: decided.source,
|
|
69527
69541
|
pools: params.pools,
|
|
69528
69542
|
paths,
|
|
69529
|
-
fs:
|
|
69543
|
+
fs: fs19,
|
|
69530
69544
|
usageFs: farmRuntime.fs,
|
|
69531
69545
|
cwd: farmRuntime.cwd,
|
|
69532
69546
|
now: farmRuntime.now,
|
|
@@ -69541,8 +69555,8 @@ function pickFromPool(decided, params, parsedArgv) {
|
|
|
69541
69555
|
}
|
|
69542
69556
|
return { identityDecision: { ...decided, name: resolution.identity }, poolExplanation: resolution.explanation };
|
|
69543
69557
|
}
|
|
69544
|
-
function
|
|
69545
|
-
const { paths, fs:
|
|
69558
|
+
function prepareLaunch(params) {
|
|
69559
|
+
const { paths, fs: fs19, proc, log } = params;
|
|
69546
69560
|
const { env, argv } = proc;
|
|
69547
69561
|
const parsedArgv = parseLauncherArgv(argv);
|
|
69548
69562
|
const cliOverride = buildCliOverride({
|
|
@@ -69557,7 +69571,7 @@ function runLauncher(params) {
|
|
|
69557
69571
|
argv0Identity: parsedArgv.identity,
|
|
69558
69572
|
directoryPinnedIdentity: params.directoryPinnedIdentity,
|
|
69559
69573
|
readActiveIdentityFile: () => {
|
|
69560
|
-
const raw =
|
|
69574
|
+
const raw = fs19.readFileUtf8(paths.activeIdentityFile);
|
|
69561
69575
|
if (raw === void 0) {
|
|
69562
69576
|
return void 0;
|
|
69563
69577
|
}
|
|
@@ -69592,7 +69606,7 @@ function runLauncher(params) {
|
|
|
69592
69606
|
}
|
|
69593
69607
|
let loadedIdentity;
|
|
69594
69608
|
if (identityDecision.name !== void 0) {
|
|
69595
|
-
loadedIdentity = loadIdentity(paths.identitiesDir, identityDecision.name,
|
|
69609
|
+
loadedIdentity = loadIdentity(paths.identitiesDir, identityDecision.name, fs19);
|
|
69596
69610
|
if (loadedIdentity === void 0) {
|
|
69597
69611
|
log.error(
|
|
69598
69612
|
`agent-shim: no identity named "${identityDecision.name}" (selected via ${identityDecision.source}). Run \`agent-shim identity add ${identityDecision.name}\` first.`
|
|
@@ -69607,7 +69621,7 @@ function runLauncher(params) {
|
|
|
69607
69621
|
identityDefaultConfigProfile: loadedIdentity?.config.defaultConfigProfile,
|
|
69608
69622
|
globalDefaultConfigProfile: params.globalDefaultConfigProfile
|
|
69609
69623
|
});
|
|
69610
|
-
if (configProfileDecision.name !== void 0 && params.allowMissingConfigProfile !== true &&
|
|
69624
|
+
if (configProfileDecision.name !== void 0 && params.allowMissingConfigProfile !== true && fs19.readConfigFile(import_node_path37.default.join(paths.configProfilesDir, `${configProfileDecision.name}.json`)) === void 0) {
|
|
69611
69625
|
log.error(
|
|
69612
69626
|
`agent-shim: no configuration profile named "${configProfileDecision.name}" (selected via ${configProfileDecision.source}). Run \`agent-shim profile add ${configProfileDecision.name}\` first.`
|
|
69613
69627
|
);
|
|
@@ -69616,7 +69630,7 @@ function runLauncher(params) {
|
|
|
69616
69630
|
const farmContext = farm !== void 0 && farmIdentity !== void 0 ? { farm, identity: farmIdentity, cascade: farm.loadCascade(configProfileDecision.name, cliOverride) } : void 0;
|
|
69617
69631
|
const provider = parsedArgv.provider === false ? void 0 : resolveProvider({
|
|
69618
69632
|
paths,
|
|
69619
|
-
port:
|
|
69633
|
+
port: fs19,
|
|
69620
69634
|
env,
|
|
69621
69635
|
...params.credentials === void 0 ? {} : { credentials: params.credentials },
|
|
69622
69636
|
...parsedArgv.provider === void 0 ? {} : { cliProvider: parsedArgv.provider },
|
|
@@ -69768,32 +69782,25 @@ function runLauncher(params) {
|
|
|
69768
69782
|
sessionId: (0, import_node_crypto15.randomUUID)()
|
|
69769
69783
|
});
|
|
69770
69784
|
const releases = [...headroom === void 0 || headroomPort === void 0 ? [] : [headroomPort.release], ...frontDoorRelease === void 0 ? [] : [frontDoorRelease]];
|
|
69771
|
-
|
|
69772
|
-
|
|
69773
|
-
|
|
69774
|
-
const releaseAll = () => {
|
|
69775
|
-
for (const release of releases) {
|
|
69776
|
-
release();
|
|
69785
|
+
const release = () => {
|
|
69786
|
+
for (const registered of releases) {
|
|
69787
|
+
registered();
|
|
69777
69788
|
}
|
|
69778
69789
|
};
|
|
69790
|
+
return { bin: discovered.path, args: finalArgv, env: finalEnv, release };
|
|
69791
|
+
}
|
|
69792
|
+
function runLauncher(params) {
|
|
69793
|
+
const plan = prepareLaunch(params);
|
|
69779
69794
|
try {
|
|
69780
|
-
spawnClaude({
|
|
69781
|
-
bin: discovered.path,
|
|
69782
|
-
args: finalArgv,
|
|
69783
|
-
env: finalEnv,
|
|
69784
|
-
spawn: spawn3,
|
|
69785
|
-
proc,
|
|
69786
|
-
beforeExit: releaseAll
|
|
69787
|
-
});
|
|
69795
|
+
spawnClaude({ bin: plan.bin, args: plan.args, env: plan.env, spawn: params.spawn, proc: params.proc, beforeExit: plan.release });
|
|
69788
69796
|
} finally {
|
|
69789
|
-
|
|
69797
|
+
plan.release();
|
|
69790
69798
|
}
|
|
69791
69799
|
}
|
|
69792
69800
|
|
|
69793
|
-
// src/
|
|
69794
|
-
function buildFarmRuntime(paths) {
|
|
69801
|
+
// src/launchWiring.ts
|
|
69802
|
+
function buildFarmRuntime(paths, cwd) {
|
|
69795
69803
|
const home = import_node_os8.default.homedir();
|
|
69796
|
-
const cwd = process.cwd();
|
|
69797
69804
|
const read = cosmiconfigReader();
|
|
69798
69805
|
const classification = loadClassification(paths);
|
|
69799
69806
|
const loaded = loadCascadeInput({ paths, home, cwd, read });
|
|
@@ -69827,9 +69834,30 @@ function buildFarmRuntime(paths) {
|
|
|
69827
69834
|
...loaded.globalConfig?.pools === void 0 ? {} : { pools: loaded.globalConfig.pools }
|
|
69828
69835
|
};
|
|
69829
69836
|
}
|
|
69837
|
+
function realPrepareLaunchParams(paths, options) {
|
|
69838
|
+
const { farm } = options;
|
|
69839
|
+
return {
|
|
69840
|
+
paths,
|
|
69841
|
+
fs: realFsPort,
|
|
69842
|
+
proc: options.proc,
|
|
69843
|
+
log: options.log,
|
|
69844
|
+
resolveClaudeBinary: realResolveClaudeBinary(resolveOwnInstallDirs(paths, realOwnExecutablePath())),
|
|
69845
|
+
farm: farm.runtime,
|
|
69846
|
+
headroom: realHeadroomPort(paths, { spawnDaemon: options.spawnDaemon, cwd: farm.runtime.cwd }),
|
|
69847
|
+
frontdoor: realFrontDoorPort(paths, options.spawnDaemon),
|
|
69848
|
+
credentials: { ...realCredentialPort, cache: realCredentialCacheEnv(paths) },
|
|
69849
|
+
...farm.directoryIdentity === void 0 ? {} : { directoryPinnedIdentity: farm.directoryIdentity },
|
|
69850
|
+
...farm.directoryConfigProfile === void 0 ? {} : { directoryRuleConfigProfile: farm.directoryConfigProfile },
|
|
69851
|
+
...farm.globalDefaultConfigProfile === void 0 ? {} : { globalDefaultConfigProfile: farm.globalDefaultConfigProfile },
|
|
69852
|
+
...farm.pools === void 0 ? {} : { pools: farm.pools },
|
|
69853
|
+
...options.allowMissingConfigProfile === void 0 ? {} : { allowMissingConfigProfile: options.allowMissingConfigProfile }
|
|
69854
|
+
};
|
|
69855
|
+
}
|
|
69856
|
+
|
|
69857
|
+
// src/runClaude.ts
|
|
69830
69858
|
async function runClaude(argvOverride) {
|
|
69831
69859
|
const paths = resolveLayoutPaths();
|
|
69832
|
-
const farm = buildFarmRuntime(paths);
|
|
69860
|
+
const farm = buildFarmRuntime(paths, process.cwd());
|
|
69833
69861
|
let allowMissingConfigProfile = false;
|
|
69834
69862
|
if (process.stdin.isTTY) {
|
|
69835
69863
|
const procForDecision = argvOverride === void 0 ? realProcPort : { ...realProcPort, argv: argvOverride };
|
|
@@ -69879,21 +69907,14 @@ async function runClaude(argvOverride) {
|
|
|
69879
69907
|
}
|
|
69880
69908
|
}
|
|
69881
69909
|
runLauncher({
|
|
69882
|
-
paths,
|
|
69883
|
-
|
|
69884
|
-
|
|
69885
|
-
|
|
69886
|
-
|
|
69887
|
-
|
|
69888
|
-
|
|
69889
|
-
|
|
69890
|
-
frontdoor: realFrontDoorPort(paths),
|
|
69891
|
-
credentials: { ...realCredentialPort, cache: realCredentialCacheEnv(paths) },
|
|
69892
|
-
...farm.directoryIdentity === void 0 ? {} : { directoryPinnedIdentity: farm.directoryIdentity },
|
|
69893
|
-
...farm.directoryConfigProfile === void 0 ? {} : { directoryRuleConfigProfile: farm.directoryConfigProfile },
|
|
69894
|
-
...farm.globalDefaultConfigProfile === void 0 ? {} : { globalDefaultConfigProfile: farm.globalDefaultConfigProfile },
|
|
69895
|
-
...farm.pools === void 0 ? {} : { pools: farm.pools },
|
|
69896
|
-
allowMissingConfigProfile
|
|
69910
|
+
...realPrepareLaunchParams(paths, {
|
|
69911
|
+
proc: argvOverride === void 0 ? realProcPort : { ...realProcPort, argv: argvOverride },
|
|
69912
|
+
log: realLogPort,
|
|
69913
|
+
spawnDaemon: spawnDetachedSupervisor,
|
|
69914
|
+
farm,
|
|
69915
|
+
allowMissingConfigProfile
|
|
69916
|
+
}),
|
|
69917
|
+
spawn: realSpawnPort
|
|
69897
69918
|
});
|
|
69898
69919
|
}
|
|
69899
69920
|
|