@cortexkit/anthropic-auth-core 2.16.4 → 2.16.5
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/accounts.d.ts +2 -0
- package/dist/index.js +446 -206
- package/dist/insula-quota.d.ts +2 -1
- package/dist/message-thread-tail-store.d.ts +36 -0
- package/dist/message-threads.d.ts +8 -13
- package/dist/native-account-runtime.d.ts +1 -1
- package/dist/native-vault-runtime.d.ts +3 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -12748,6 +12748,16 @@ function quotaFieldSource(snapshot, field) {
|
|
|
12748
12748
|
return;
|
|
12749
12749
|
return snapshot.fieldSources?.[field] ?? (snapshot.source === "poll" || snapshot.source === "headers" ? snapshot.source : undefined);
|
|
12750
12750
|
}
|
|
12751
|
+
function quotaPollCheckedAt(quota) {
|
|
12752
|
+
if (!quota)
|
|
12753
|
+
return 0;
|
|
12754
|
+
const standard = ["five_hour", "seven_day"];
|
|
12755
|
+
const hasPoll = quota.source === "poll" || quotaSnapshotOmitsFiveHour(quota) || quota.scoped !== undefined || quota.extraUsage !== undefined || standard.some((key) => quotaFieldSource(quota, key) === "poll");
|
|
12756
|
+
if (!hasPoll)
|
|
12757
|
+
return 0;
|
|
12758
|
+
const topLevelIsHeader = standard.some((key) => quotaFieldSource(quota, key) === "headers" && quota[key]?.checkedAt === quota.checkedAt);
|
|
12759
|
+
return Math.max(!topLevelIsHeader ? quota.checkedAt ?? 0 : 0, ...(quota.scoped ?? []).map((window) => window.checkedAt), ...standard.map((key) => quotaFieldSource(quota, key) === "poll" ? quota[key]?.checkedAt ?? 0 : 0));
|
|
12760
|
+
}
|
|
12751
12761
|
function fieldSourcesForMergedQuota(existing, incoming, merged) {
|
|
12752
12762
|
const fieldSources = {};
|
|
12753
12763
|
for (const field of QUOTA_FIELD_NAMES) {
|
|
@@ -12779,18 +12789,20 @@ function mergeHeaderQuotaForPersistence(existing, incoming) {
|
|
|
12779
12789
|
if (!existing || incoming.source !== "headers")
|
|
12780
12790
|
return incoming;
|
|
12781
12791
|
const preservePollBinding = existing.bindingWindowSource === "poll";
|
|
12792
|
+
const pollCheckedAt = quotaPollCheckedAt(existing);
|
|
12793
|
+
const preservePollFields = pollCheckedAt > 0 && quotaPollCheckedAt(incoming) <= pollCheckedAt;
|
|
12782
12794
|
const merged = {
|
|
12783
12795
|
...existing,
|
|
12784
12796
|
...incoming,
|
|
12785
12797
|
five_hour: mergeHeaderOwnedWindow(existing, incoming, "five_hour"),
|
|
12786
12798
|
seven_day: mergeHeaderOwnedWindow(existing, incoming, "seven_day"),
|
|
12787
|
-
scoped: mergeHeaderScopedQuota(existing, incoming),
|
|
12788
|
-
extraUsage: existing.extraUsage ?? incoming.extraUsage,
|
|
12799
|
+
scoped: preservePollFields && quotaFieldSource(incoming, "scoped") !== "headers" ? existing.scoped : mergeHeaderScopedQuota(existing, incoming),
|
|
12800
|
+
extraUsage: preservePollFields ? existing.extraUsage : existing.extraUsage ?? incoming.extraUsage,
|
|
12789
12801
|
fallbackAdvised: incoming.fieldSources?.fallbackAdvised !== undefined ? incoming.fallbackAdvised : existing.fallbackAdvised ?? incoming.fallbackAdvised,
|
|
12790
12802
|
bindingWindow: preservePollBinding ? existing.bindingWindow : incoming.bindingWindow ?? existing.bindingWindow,
|
|
12791
12803
|
bindingWindowSource: preservePollBinding ? "poll" : incoming.bindingWindowSource ?? existing.bindingWindowSource,
|
|
12792
|
-
...
|
|
12793
|
-
checkedAt:
|
|
12804
|
+
...pollCheckedAt > 0 && {
|
|
12805
|
+
checkedAt: pollCheckedAt
|
|
12794
12806
|
}
|
|
12795
12807
|
};
|
|
12796
12808
|
const fieldSources = fieldSourcesForMergedQuota(existing, incoming, merged);
|
|
@@ -26018,13 +26030,22 @@ var nativeQuotaCodec = {
|
|
|
26018
26030
|
return toNativeQuotaMap(mergeHeaderQuotaForPersistence(previous, incoming));
|
|
26019
26031
|
const previousTime = quotaSnapshotCheckedAt(previous);
|
|
26020
26032
|
const incomingTime = quotaSnapshotCheckedAt(incoming);
|
|
26021
|
-
|
|
26033
|
+
const previousPollAt = quotaPollCheckedAt(previous);
|
|
26034
|
+
if (incoming.source === "poll" && incoming.checkedAt !== undefined && incoming.checkedAt < previousPollAt)
|
|
26022
26035
|
return toNativeQuotaMap(previous);
|
|
26023
26036
|
if (incoming.source === "poll" && incoming.seven_day && !incoming.five_hour && incoming.checkedAt !== undefined && incoming.checkedAt > (previous.five_hour?.checkedAt ?? 0) && (previous.source !== "poll" || incoming.checkedAt >= (previous.checkedAt ?? 0)))
|
|
26024
26037
|
return toNativeQuotaMap(previous.source === "headers" ? mergeHeaderQuotaForPersistence(incoming, {
|
|
26025
26038
|
...previous,
|
|
26026
|
-
five_hour: undefined
|
|
26039
|
+
five_hour: undefined,
|
|
26040
|
+
scoped: undefined,
|
|
26041
|
+
extraUsage: undefined
|
|
26027
26042
|
}) : incoming);
|
|
26043
|
+
if (incoming.source === "poll" && incoming.checkedAt !== undefined && previous.source === "headers" && previousPollAt > 0 && previousTime > incomingTime)
|
|
26044
|
+
return toNativeQuotaMap(mergeHeaderQuotaForPersistence(incoming, {
|
|
26045
|
+
...previous,
|
|
26046
|
+
scoped: undefined,
|
|
26047
|
+
extraUsage: undefined
|
|
26048
|
+
}));
|
|
26028
26049
|
return toNativeQuotaMap(previousTime > incomingTime || previousTime === incomingTime && previous.source === "poll" && incoming.source !== "poll" ? previous : incoming);
|
|
26029
26050
|
}
|
|
26030
26051
|
};
|
|
@@ -33268,10 +33289,198 @@ function executeLoggingCommand(input) {
|
|
|
33268
33289
|
`);
|
|
33269
33290
|
}
|
|
33270
33291
|
// src/message-threads.ts
|
|
33292
|
+
import { createHash as createHash24, randomUUID as randomUUID21 } from "node:crypto";
|
|
33293
|
+
|
|
33294
|
+
// src/message-thread-tail-store.ts
|
|
33271
33295
|
import { createHash as createHash23, randomUUID as randomUUID20 } from "node:crypto";
|
|
33296
|
+
import { constants as constants4 } from "node:fs";
|
|
33297
|
+
import { lstat as lstat17, mkdir as mkdir14, open as open12, readdir as readdir7, rm as rm9, writeFile as writeFile6 } from "node:fs/promises";
|
|
33298
|
+
import { join as join28 } from "node:path";
|
|
33299
|
+
var FILE2 = /^[a-f0-9]{64}\.json$/;
|
|
33300
|
+
var TEMP2 = /^[a-f0-9]{64}\.json\.[a-f0-9-]+\.tmp$/;
|
|
33301
|
+
var HASH = /^[a-f0-9]{64}$/;
|
|
33302
|
+
var MAX_RECORD_BYTES2 = 4096;
|
|
33303
|
+
var MAX_SESSIONS = 1024;
|
|
33304
|
+
var READ_TIMEOUT_MS = 50;
|
|
33305
|
+
var SWEEP_INTERVAL_MS = 60000;
|
|
33306
|
+
var LONG_LIFETIME_MS = 3600000;
|
|
33307
|
+
function fullCacheTailExpired(tail, now) {
|
|
33308
|
+
return now < tail.acceptedAt || now >= tail.acceptedAt + (tail.control.ttl === "1h" ? LONG_LIFETIME_MS : 300000);
|
|
33309
|
+
}
|
|
33310
|
+
function entryFromBytes(bytes) {
|
|
33311
|
+
if (bytes.length > MAX_RECORD_BYTES2)
|
|
33312
|
+
return;
|
|
33313
|
+
const value = JSON.parse(bytes.toString("utf8"));
|
|
33314
|
+
const tail = value?.tail;
|
|
33315
|
+
if (value?.v !== 1 || typeof value.continued !== "boolean" || !tail || typeof tail.identity !== "string" || !HASH.test(tail.identity) || typeof tail.prefix !== "string" || !HASH.test(tail.prefix) || !Number.isSafeInteger(tail.messageIndex) || tail.messageIndex < 0 || !Array.isArray(tail.blockPath) || !tail.blockPath.length || !tail.blockPath.every((index) => Number.isSafeInteger(index) && index >= 0) || tail.control?.type !== "ephemeral" || tail.control.ttl !== undefined && tail.control.ttl !== "1h" && tail.control.ttl !== "5m" || !Number.isSafeInteger(tail.acceptedAt) || tail.acceptedAt < 0)
|
|
33316
|
+
return;
|
|
33317
|
+
return {
|
|
33318
|
+
continued: value.continued,
|
|
33319
|
+
tail: {
|
|
33320
|
+
identity: tail.identity,
|
|
33321
|
+
messageIndex: tail.messageIndex,
|
|
33322
|
+
blockPath: tail.blockPath,
|
|
33323
|
+
prefix: tail.prefix,
|
|
33324
|
+
control: {
|
|
33325
|
+
type: "ephemeral",
|
|
33326
|
+
...tail.control.ttl && { ttl: tail.control.ttl }
|
|
33327
|
+
},
|
|
33328
|
+
acceptedAt: tail.acceptedAt
|
|
33329
|
+
}
|
|
33330
|
+
};
|
|
33331
|
+
}
|
|
33332
|
+
|
|
33333
|
+
class MessageThreadTailStore {
|
|
33334
|
+
now;
|
|
33335
|
+
directory;
|
|
33336
|
+
writes = Promise.resolve();
|
|
33337
|
+
nextSweep = 0;
|
|
33338
|
+
constructor(host, root = getHostDataHome(), now = Date.now) {
|
|
33339
|
+
this.now = now;
|
|
33340
|
+
this.directory = join28(root, "cortexkit", "anthropic-auth", "thread-tails", host);
|
|
33341
|
+
}
|
|
33342
|
+
pathFor(session) {
|
|
33343
|
+
const key = createHash23("sha256").update(session).digest("hex");
|
|
33344
|
+
return join28(this.directory, `${key}.json`);
|
|
33345
|
+
}
|
|
33346
|
+
async directoryReady(create) {
|
|
33347
|
+
if (create && !await ensureWindowsOwnerOnlyDirectory(this.directory))
|
|
33348
|
+
await mkdir14(this.directory, { recursive: true, mode: 448 });
|
|
33349
|
+
const stat13 = await lstat17(this.directory);
|
|
33350
|
+
return stat13.isDirectory() && await isOwnerOnly(this.directory, () => stat13.uid === process.getuid?.() && (stat13.mode & 511) === 448);
|
|
33351
|
+
}
|
|
33352
|
+
async read(path2, stat13) {
|
|
33353
|
+
if (!stat13.isFile() || stat13.size > MAX_RECORD_BYTES2 || !await isOwnerOnly(path2, () => stat13.uid === process.getuid?.() && (stat13.mode & 511) === 384))
|
|
33354
|
+
return;
|
|
33355
|
+
try {
|
|
33356
|
+
const windows = await readWindowsOwnerOnly(path2);
|
|
33357
|
+
if (windows)
|
|
33358
|
+
return entryFromBytes(windows);
|
|
33359
|
+
const file = await open12(path2, constants4.O_RDONLY | constants4.O_NOFOLLOW | constants4.O_NONBLOCK);
|
|
33360
|
+
try {
|
|
33361
|
+
const actual = await file.stat();
|
|
33362
|
+
if (!actual.isFile() || actual.uid !== process.getuid?.() || (actual.mode & 511) !== 384 || actual.size > MAX_RECORD_BYTES2)
|
|
33363
|
+
return;
|
|
33364
|
+
const bytes = Buffer.alloc(MAX_RECORD_BYTES2 + 1);
|
|
33365
|
+
const { bytesRead } = await file.read(bytes);
|
|
33366
|
+
return entryFromBytes(bytes.subarray(0, bytesRead));
|
|
33367
|
+
} finally {
|
|
33368
|
+
await file.close();
|
|
33369
|
+
}
|
|
33370
|
+
} catch {
|
|
33371
|
+
return;
|
|
33372
|
+
}
|
|
33373
|
+
}
|
|
33374
|
+
async load(session) {
|
|
33375
|
+
const path2 = this.pathFor(session);
|
|
33376
|
+
const read = async () => {
|
|
33377
|
+
if (!await this.directoryReady(false))
|
|
33378
|
+
return;
|
|
33379
|
+
const stat13 = await lstat17(path2);
|
|
33380
|
+
const entry2 = await this.read(path2, stat13);
|
|
33381
|
+
if (entry2 && fullCacheTailExpired(entry2.tail, this.now()))
|
|
33382
|
+
this.enqueue(() => this.removeUnchanged(path2, stat13));
|
|
33383
|
+
return entry2;
|
|
33384
|
+
};
|
|
33385
|
+
let cancelTimeout;
|
|
33386
|
+
try {
|
|
33387
|
+
return await Promise.race([
|
|
33388
|
+
read().catch(() => {
|
|
33389
|
+
return;
|
|
33390
|
+
}),
|
|
33391
|
+
new Promise((resolve18) => {
|
|
33392
|
+
const timeout = setTimeout(resolve18, READ_TIMEOUT_MS);
|
|
33393
|
+
timeout.unref();
|
|
33394
|
+
cancelTimeout = () => clearTimeout(timeout);
|
|
33395
|
+
})
|
|
33396
|
+
]);
|
|
33397
|
+
} finally {
|
|
33398
|
+
cancelTimeout?.();
|
|
33399
|
+
}
|
|
33400
|
+
}
|
|
33401
|
+
enqueue(action) {
|
|
33402
|
+
this.writes = this.writes.then(action).catch(() => {});
|
|
33403
|
+
}
|
|
33404
|
+
changed(session, tail, continued) {
|
|
33405
|
+
const path2 = this.pathFor(session);
|
|
33406
|
+
this.enqueue(async () => {
|
|
33407
|
+
if (!await this.directoryReady(Boolean(tail)))
|
|
33408
|
+
return;
|
|
33409
|
+
const now = this.now();
|
|
33410
|
+
if (tail && !fullCacheTailExpired(tail, now)) {
|
|
33411
|
+
const data = JSON.stringify({ v: 1, continued, tail });
|
|
33412
|
+
if (Buffer.byteLength(data) <= MAX_RECORD_BYTES2)
|
|
33413
|
+
await this.publish(path2, data);
|
|
33414
|
+
else
|
|
33415
|
+
await rm9(path2, { force: true });
|
|
33416
|
+
} else
|
|
33417
|
+
await rm9(path2, { force: true });
|
|
33418
|
+
await this.prune(now);
|
|
33419
|
+
});
|
|
33420
|
+
}
|
|
33421
|
+
async publish(path2, data) {
|
|
33422
|
+
const temp = `${path2}.${randomUUID20()}.tmp`;
|
|
33423
|
+
try {
|
|
33424
|
+
if (!await createWindowsOwnerOnly(temp, data))
|
|
33425
|
+
await writeFile6(temp, data, { flag: "wx", mode: 384 });
|
|
33426
|
+
await renameWithWindowsSharingRetry(temp, path2);
|
|
33427
|
+
} finally {
|
|
33428
|
+
await rm9(temp, { force: true });
|
|
33429
|
+
}
|
|
33430
|
+
}
|
|
33431
|
+
async removeUnchanged(path2, stat13) {
|
|
33432
|
+
const current = await lstat17(path2).catch(() => {
|
|
33433
|
+
return;
|
|
33434
|
+
});
|
|
33435
|
+
if (current?.dev === stat13.dev && current.ino === stat13.ino && current.ctimeMs === stat13.ctimeMs)
|
|
33436
|
+
await rm9(path2, { force: true });
|
|
33437
|
+
}
|
|
33438
|
+
async prune(now) {
|
|
33439
|
+
const names = await readdir7(this.directory);
|
|
33440
|
+
const files = names.filter((name) => FILE2.test(name));
|
|
33441
|
+
const sweep = now >= this.nextSweep;
|
|
33442
|
+
if (!sweep && files.length <= MAX_SESSIONS)
|
|
33443
|
+
return;
|
|
33444
|
+
if (sweep)
|
|
33445
|
+
this.nextSweep = now + SWEEP_INTERVAL_MS;
|
|
33446
|
+
const candidates = sweep ? [...files, ...names.filter((name) => TEMP2.test(name))] : files;
|
|
33447
|
+
const live = [];
|
|
33448
|
+
for (let i = 0;i < candidates.length; i += 32) {
|
|
33449
|
+
await Promise.all(candidates.slice(i, i + 32).map(async (name) => {
|
|
33450
|
+
const path2 = join28(this.directory, name);
|
|
33451
|
+
const stat13 = await lstat17(path2).catch(() => {
|
|
33452
|
+
return;
|
|
33453
|
+
});
|
|
33454
|
+
if (!stat13?.isFile())
|
|
33455
|
+
return;
|
|
33456
|
+
if (TEMP2.test(name)) {
|
|
33457
|
+
if (stat13.mtimeMs <= Date.now() - LONG_LIFETIME_MS)
|
|
33458
|
+
await this.removeUnchanged(path2, stat13);
|
|
33459
|
+
return;
|
|
33460
|
+
}
|
|
33461
|
+
if (sweep) {
|
|
33462
|
+
const entry2 = await this.read(path2, stat13);
|
|
33463
|
+
if (!entry2 || fullCacheTailExpired(entry2.tail, now)) {
|
|
33464
|
+
await this.removeUnchanged(path2, stat13);
|
|
33465
|
+
return;
|
|
33466
|
+
}
|
|
33467
|
+
}
|
|
33468
|
+
live.push({ path: path2, stat: stat13 });
|
|
33469
|
+
}));
|
|
33470
|
+
}
|
|
33471
|
+
live.sort((a, b) => b.stat.mtimeMs - a.stat.mtimeMs);
|
|
33472
|
+
for (const { path: path2, stat: stat13 } of live.slice(MAX_SESSIONS))
|
|
33473
|
+
await this.removeUnchanged(path2, stat13);
|
|
33474
|
+
}
|
|
33475
|
+
async drain() {
|
|
33476
|
+
await this.writes;
|
|
33477
|
+
}
|
|
33478
|
+
}
|
|
33479
|
+
|
|
33480
|
+
// src/message-threads.ts
|
|
33272
33481
|
var MESSAGE_THREADS_BETA = "message-threads-2026-08-12";
|
|
33273
33482
|
var MAX_AGE_MS = 5 * 60000;
|
|
33274
|
-
var
|
|
33483
|
+
var MAX_SESSIONS2 = 2048;
|
|
33275
33484
|
var MAX_MESSAGE_HASHES = 131072;
|
|
33276
33485
|
function localMessageThreadCredential(binding2) {
|
|
33277
33486
|
return JSON.stringify([
|
|
@@ -33282,7 +33491,7 @@ function localMessageThreadCredential(binding2) {
|
|
|
33282
33491
|
]);
|
|
33283
33492
|
}
|
|
33284
33493
|
function hash4(value) {
|
|
33285
|
-
return
|
|
33494
|
+
return createHash24("sha256").update(JSON.stringify(value)).digest("hex");
|
|
33286
33495
|
}
|
|
33287
33496
|
function normalizedReplyName(name) {
|
|
33288
33497
|
const bare = name.startsWith("mcp_") ? name.slice(4) : name;
|
|
@@ -33329,7 +33538,7 @@ class ReplyFingerprint {
|
|
|
33329
33538
|
layout = new Map;
|
|
33330
33539
|
textBlocks = new Map;
|
|
33331
33540
|
signatures = new Map;
|
|
33332
|
-
text =
|
|
33541
|
+
text = createHash24("sha256");
|
|
33333
33542
|
committedText = this.text.copy();
|
|
33334
33543
|
hasText = false;
|
|
33335
33544
|
activeText;
|
|
@@ -33351,7 +33560,7 @@ class ReplyFingerprint {
|
|
|
33351
33560
|
this.text.update(part.slice(visible.length));
|
|
33352
33561
|
}
|
|
33353
33562
|
appendBlockText(state, value) {
|
|
33354
|
-
state.text ??=
|
|
33563
|
+
state.text ??= createHash24("sha256");
|
|
33355
33564
|
state.committedText ??= state.text.copy();
|
|
33356
33565
|
const part = state.textHasContent ? value : value.trimStart();
|
|
33357
33566
|
const visible = part.trimEnd();
|
|
@@ -33402,7 +33611,7 @@ class ReplyFingerprint {
|
|
|
33402
33611
|
state.input = block.input;
|
|
33403
33612
|
}
|
|
33404
33613
|
if (block.type === "thinking" || block.type === "redacted_thinking") {
|
|
33405
|
-
state.signature =
|
|
33614
|
+
state.signature = createHash24("sha256");
|
|
33406
33615
|
const signature = block.type === "redacted_thinking" ? block.data : block.signature;
|
|
33407
33616
|
if (typeof signature === "string") {
|
|
33408
33617
|
state.signature.update(signature);
|
|
@@ -33822,7 +34031,7 @@ function restoreFullCacheTail(input, history, now) {
|
|
|
33822
34031
|
return { reason: "no_full_tail" };
|
|
33823
34032
|
if (tail.identity !== hash4([input.account, input.credential]))
|
|
33824
34033
|
return { reason: "account_changed" };
|
|
33825
|
-
if (
|
|
34034
|
+
if (fullCacheTailExpired(tail, now))
|
|
33826
34035
|
return { reason: "expired" };
|
|
33827
34036
|
if (input.body.cache_control)
|
|
33828
34037
|
return { reason: "slot_taken" };
|
|
@@ -33875,16 +34084,46 @@ class MessageThreadTracker {
|
|
|
33875
34084
|
sessions = new Map;
|
|
33876
34085
|
messageHashes = 0;
|
|
33877
34086
|
dispositions = new Map;
|
|
33878
|
-
instance =
|
|
34087
|
+
instance = randomUUID21();
|
|
34088
|
+
#tailStore;
|
|
33879
34089
|
fullTails = new Map;
|
|
33880
|
-
constructor(capacity =
|
|
34090
|
+
constructor(capacity = MAX_SESSIONS2, now = Date.now, hashBudget = MAX_MESSAGE_HASHES, tailStore) {
|
|
33881
34091
|
this.capacity = capacity;
|
|
33882
34092
|
this.now = now;
|
|
33883
34093
|
this.hashBudget = hashBudget;
|
|
34094
|
+
this.#tailStore = tailStore;
|
|
34095
|
+
}
|
|
34096
|
+
static withTailPersistence(host) {
|
|
34097
|
+
return new MessageThreadTracker(MAX_SESSIONS2, Date.now, MAX_MESSAGE_HASHES, new MessageThreadTailStore(host));
|
|
34098
|
+
}
|
|
34099
|
+
canLoadFullTail(session) {
|
|
34100
|
+
const disposition = this.dispositions.get(session);
|
|
34101
|
+
return !this.fullTails.has(session) && !this.sessions.has(session) && disposition !== "session_deleted" && disposition !== "evicted";
|
|
34102
|
+
}
|
|
34103
|
+
loadFullTail(session) {
|
|
34104
|
+
if (!this.#tailStore || !this.canLoadFullTail(session))
|
|
34105
|
+
return;
|
|
34106
|
+
const disposition = this.dispositions.get(session);
|
|
34107
|
+
return this.#tailStore.load(session).then((entry2) => {
|
|
34108
|
+
if (entry2 && this.dispositions.get(session) === disposition && this.canLoadFullTail(session))
|
|
34109
|
+
this.rememberFullTail(session, { hadThread: true, ...entry2 });
|
|
34110
|
+
});
|
|
34111
|
+
}
|
|
34112
|
+
rememberFullTail(session, history) {
|
|
34113
|
+
this.fullTails.delete(session);
|
|
34114
|
+
this.fullTails.set(session, history);
|
|
34115
|
+
while (this.fullTails.size > this.capacity) {
|
|
34116
|
+
const oldest = this.fullTails.keys().next().value;
|
|
34117
|
+
if (oldest === undefined)
|
|
34118
|
+
break;
|
|
34119
|
+
this.fullTails.delete(oldest);
|
|
34120
|
+
}
|
|
33884
34121
|
}
|
|
33885
34122
|
forget(session, disposition = "session_deleted") {
|
|
33886
34123
|
if (disposition === "session_deleted" || disposition === "evicted")
|
|
33887
34124
|
this.fullTails.delete(session);
|
|
34125
|
+
if (disposition === "session_deleted")
|
|
34126
|
+
this.#tailStore?.changed(session, undefined, false);
|
|
33888
34127
|
this.messageHashes -= this.sessions.get(session)?.anchor?.messages.length ?? 0;
|
|
33889
34128
|
this.sessions.delete(session);
|
|
33890
34129
|
this.dispositions.delete(session);
|
|
@@ -34027,25 +34266,18 @@ class MessageThreadTracker {
|
|
|
34027
34266
|
const completedAt = this.now();
|
|
34028
34267
|
accepted = true;
|
|
34029
34268
|
const sentInput = { ...input, body: tailResult?.body ?? input.body };
|
|
34030
|
-
|
|
34031
|
-
|
|
34032
|
-
|
|
34033
|
-
|
|
34034
|
-
hadThread: true,
|
|
34035
|
-
continued: false,
|
|
34036
|
-
tail: fullCacheTail(sentInput, completedAt)
|
|
34037
|
-
});
|
|
34038
|
-
} else
|
|
34039
|
-
this.fullTails.set(input.session, {
|
|
34269
|
+
if (decision.type === "continue" || !input.stateless || oldTail?.hadThread) {
|
|
34270
|
+
const continued = decision.type === "continue";
|
|
34271
|
+
const tail = continued ? oldTail?.tail : fullCacheTail(sentInput, completedAt);
|
|
34272
|
+
this.rememberFullTail(input.session, {
|
|
34040
34273
|
hadThread: true,
|
|
34041
|
-
continued
|
|
34042
|
-
tail
|
|
34274
|
+
continued,
|
|
34275
|
+
tail
|
|
34043
34276
|
});
|
|
34044
|
-
|
|
34045
|
-
|
|
34046
|
-
|
|
34047
|
-
|
|
34048
|
-
this.fullTails.delete(oldest);
|
|
34277
|
+
}
|
|
34278
|
+
if (this.#tailStore) {
|
|
34279
|
+
const history = this.fullTails.get(input.session);
|
|
34280
|
+
this.#tailStore.changed(input.session, history?.tail, history?.continued ?? false);
|
|
34049
34281
|
}
|
|
34050
34282
|
if (input.stateless)
|
|
34051
34283
|
return;
|
|
@@ -34290,6 +34522,11 @@ async function prepareMessageThreadRequest(tracker, input) {
|
|
|
34290
34522
|
const fullText = input.bodyText;
|
|
34291
34523
|
let full;
|
|
34292
34524
|
const fullBody = () => full ??= JSON.parse(fullText);
|
|
34525
|
+
if (input.session) {
|
|
34526
|
+
const loading = tracker.loadFullTail(input.session);
|
|
34527
|
+
if (loading)
|
|
34528
|
+
await loading;
|
|
34529
|
+
}
|
|
34293
34530
|
const accountKey = input.account;
|
|
34294
34531
|
let threading = !disabledThreadAccounts.has(accountKey);
|
|
34295
34532
|
if (threading)
|
|
@@ -34563,7 +34800,7 @@ function remapRequestBodyModel(parsed) {
|
|
|
34563
34800
|
return true;
|
|
34564
34801
|
}
|
|
34565
34802
|
// src/native-account-runtime.ts
|
|
34566
|
-
import { randomUUID as
|
|
34803
|
+
import { randomUUID as randomUUID24 } from "node:crypto";
|
|
34567
34804
|
import { resolve as resolve22 } from "node:path";
|
|
34568
34805
|
|
|
34569
34806
|
// src/native-account-view.ts
|
|
@@ -35350,7 +35587,7 @@ function createNativeLocalRuntimeReaders(options) {
|
|
|
35350
35587
|
}
|
|
35351
35588
|
|
|
35352
35589
|
// src/native-refresh-coordinator.ts
|
|
35353
|
-
import { createHash as
|
|
35590
|
+
import { createHash as createHash25 } from "node:crypto";
|
|
35354
35591
|
|
|
35355
35592
|
// src/native-transient-account.ts
|
|
35356
35593
|
var TRANSIENT_ACCOUNT_MESSAGE = "Anthropic account update in progress; retry the request";
|
|
@@ -35468,7 +35705,7 @@ function pause(ms, signals) {
|
|
|
35468
35705
|
var NATIVE_REFRESH_MAX_ATTEMPTS = 3;
|
|
35469
35706
|
var NATIVE_REFRESH_JOIN_BUDGET_MS = 2000;
|
|
35470
35707
|
function nativeAccountProviderLock(paths, identity5) {
|
|
35471
|
-
const key =
|
|
35708
|
+
const key = createHash25("sha256").update(JSON.stringify([paths.storageId, identity5])).digest("hex");
|
|
35472
35709
|
return {
|
|
35473
35710
|
path: paths.state,
|
|
35474
35711
|
name: `native-account-provider-${key}`,
|
|
@@ -36282,9 +36519,9 @@ function createNativeRefreshCoordinator(options) {
|
|
|
36282
36519
|
import { isAbsolute as isAbsolute5, normalize, parse as parse3, sep } from "node:path";
|
|
36283
36520
|
|
|
36284
36521
|
// src/migration-source.ts
|
|
36285
|
-
import { createHash as
|
|
36286
|
-
import { constants as
|
|
36287
|
-
import { lstat as
|
|
36522
|
+
import { createHash as createHash26 } from "node:crypto";
|
|
36523
|
+
import { constants as constants5 } from "node:fs";
|
|
36524
|
+
import { lstat as lstat18, open as open13 } from "node:fs/promises";
|
|
36288
36525
|
class NativeMigrationSourceError extends Error {
|
|
36289
36526
|
role;
|
|
36290
36527
|
code;
|
|
@@ -36327,7 +36564,7 @@ async function readNativeMigrationSource(role, path2, hooks = {}) {
|
|
|
36327
36564
|
});
|
|
36328
36565
|
let initial;
|
|
36329
36566
|
try {
|
|
36330
|
-
initial = await
|
|
36567
|
+
initial = await lstat18(path2, { bigint: true });
|
|
36331
36568
|
} catch (error) {
|
|
36332
36569
|
if (error instanceof Error && "code" in error && error.code === "ENOENT")
|
|
36333
36570
|
return missing2;
|
|
@@ -36337,7 +36574,7 @@ async function readNativeMigrationSource(role, path2, hooks = {}) {
|
|
|
36337
36574
|
throw new NativeMigrationSourceError(role, "unsafe-source");
|
|
36338
36575
|
let handle;
|
|
36339
36576
|
try {
|
|
36340
|
-
handle = await
|
|
36577
|
+
handle = await open13(path2, constants5.O_RDONLY | (constants5.O_NOFOLLOW ?? 0) | (constants5.O_NONBLOCK ?? 0));
|
|
36341
36578
|
const before = await handle.stat({ bigint: true });
|
|
36342
36579
|
if (!before.isFile() || before.dev !== initial.dev || before.ino !== initial.ino) {
|
|
36343
36580
|
throw new NativeMigrationSourceError(role, "source-changed");
|
|
@@ -36365,7 +36602,7 @@ async function readNativeMigrationSource(role, path2, hooks = {}) {
|
|
|
36365
36602
|
chunks.push(chunk.subarray(0, read.bytesRead));
|
|
36366
36603
|
}
|
|
36367
36604
|
const after = await handle.stat({ bigint: true });
|
|
36368
|
-
const current = await
|
|
36605
|
+
const current = await lstat18(path2, { bigint: true });
|
|
36369
36606
|
if (before.uid !== after.uid || before.gid !== after.gid || before.mode !== after.mode || current.uid !== before.uid || current.gid !== before.gid || current.mode !== before.mode || before.dev !== after.dev || before.ino !== after.ino || before.mtimeNs !== after.mtimeNs || before.ctimeNs !== after.ctimeNs || before.size !== after.size || current.dev !== before.dev || current.ino !== before.ino || current.isSymbolicLink() || current.mtimeNs !== before.mtimeNs || current.ctimeNs !== before.ctimeNs || current.size !== before.size || BigInt(bytes) !== before.size) {
|
|
36370
36607
|
throw new NativeMigrationSourceError(role, "source-changed");
|
|
36371
36608
|
}
|
|
@@ -36381,7 +36618,7 @@ async function readNativeMigrationSource(role, path2, hooks = {}) {
|
|
|
36381
36618
|
return sourceSnapshot({
|
|
36382
36619
|
role,
|
|
36383
36620
|
path: path2,
|
|
36384
|
-
digest:
|
|
36621
|
+
digest: createHash26("sha256").update(content).digest("hex"),
|
|
36385
36622
|
data: freezeJson(data),
|
|
36386
36623
|
metadata: Object.freeze({
|
|
36387
36624
|
uid: Number(before.uid),
|
|
@@ -37824,8 +38061,8 @@ import { resolve as resolve21 } from "node:path";
|
|
|
37824
38061
|
import { parse as parse4 } from "jsonc-parser";
|
|
37825
38062
|
|
|
37826
38063
|
// src/settings-protection-copy.ts
|
|
37827
|
-
import { randomUUID as
|
|
37828
|
-
import { readFile as readFile16, rm as
|
|
38064
|
+
import { randomUUID as randomUUID22 } from "node:crypto";
|
|
38065
|
+
import { readFile as readFile16, rm as rm10 } from "node:fs/promises";
|
|
37829
38066
|
function record13(value) {
|
|
37830
38067
|
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
37831
38068
|
}
|
|
@@ -37914,7 +38151,7 @@ async function writeSettingsProtectionCopy(file, host2, entry2, lockTimeoutMs =
|
|
|
37914
38151
|
...Object.hasOwn(copy, other) ? { [other]: copy[other] } : {},
|
|
37915
38152
|
[host2]: entry2
|
|
37916
38153
|
};
|
|
37917
|
-
const temp = `${path2}.${process.pid}.${
|
|
38154
|
+
const temp = `${path2}.${process.pid}.${randomUUID22()}.tmp`;
|
|
37918
38155
|
try {
|
|
37919
38156
|
await writeOwnerOnlyExclusive(temp, Buffer.from(`${JSON.stringify(merged, null, 2)}
|
|
37920
38157
|
`), process.platform);
|
|
@@ -37924,7 +38161,7 @@ async function writeSettingsProtectionCopy(file, host2, entry2, lockTimeoutMs =
|
|
|
37924
38161
|
return "publish";
|
|
37925
38162
|
});
|
|
37926
38163
|
} finally {
|
|
37927
|
-
await
|
|
38164
|
+
await rm10(temp, { force: true });
|
|
37928
38165
|
}
|
|
37929
38166
|
});
|
|
37930
38167
|
}
|
|
@@ -39286,10 +39523,10 @@ function settingsNotImportedText(source2, target, differing) {
|
|
|
39286
39523
|
}
|
|
39287
39524
|
|
|
39288
39525
|
// src/settings-mode.ts
|
|
39289
|
-
import { lstat as
|
|
39526
|
+
import { lstat as lstat19, mkdir as mkdir15 } from "node:fs/promises";
|
|
39290
39527
|
async function exists4(path2) {
|
|
39291
39528
|
try {
|
|
39292
|
-
await
|
|
39529
|
+
await lstat19(path2);
|
|
39293
39530
|
return true;
|
|
39294
39531
|
} catch (error) {
|
|
39295
39532
|
if (error.code === "ENOENT")
|
|
@@ -39337,7 +39574,7 @@ async function writeSettingsRecord(target, path2, record15, options) {
|
|
|
39337
39574
|
...options.ownership,
|
|
39338
39575
|
platform: context.platform
|
|
39339
39576
|
}))
|
|
39340
|
-
await
|
|
39577
|
+
await mkdir15(parent, { recursive: true, mode: 448 });
|
|
39341
39578
|
try {
|
|
39342
39579
|
if (options.fs?.writeMarker)
|
|
39343
39580
|
await options.fs.writeMarker(path2, bytes);
|
|
@@ -39712,10 +39949,10 @@ function createNativeRuntimeSettings(options) {
|
|
|
39712
39949
|
}
|
|
39713
39950
|
const before = fallback;
|
|
39714
39951
|
const legacy = before ? undefined : reopenLegacy;
|
|
39715
|
-
const
|
|
39716
|
-
if (!
|
|
39952
|
+
const open14 = before?.reopen ?? legacy;
|
|
39953
|
+
if (!open14)
|
|
39717
39954
|
return;
|
|
39718
|
-
const next = await
|
|
39955
|
+
const next = await open14();
|
|
39719
39956
|
if (!next) {
|
|
39720
39957
|
if (legacy && reopenLegacy === legacy && legacy.separate?.()) {
|
|
39721
39958
|
probedMode = "separate";
|
|
@@ -40130,7 +40367,7 @@ function legacyNativeSettings(settings) {
|
|
|
40130
40367
|
}
|
|
40131
40368
|
|
|
40132
40369
|
// src/native-vault-runtime.ts
|
|
40133
|
-
import { randomUUID as
|
|
40370
|
+
import { randomUUID as randomUUID23 } from "node:crypto";
|
|
40134
40371
|
import { SubcClient as SubcClient2 } from "@cortexkit/subc-client";
|
|
40135
40372
|
|
|
40136
40373
|
// src/insula-quota.ts
|
|
@@ -40269,27 +40506,23 @@ function newer(reading2, stored) {
|
|
|
40269
40506
|
return stored.checkedAt > reading2.checkedAt ? stored : reading2;
|
|
40270
40507
|
}
|
|
40271
40508
|
function mergeInsulaQuota(reading2, stored) {
|
|
40272
|
-
if (!stored
|
|
40509
|
+
if (!stored)
|
|
40510
|
+
return reading2;
|
|
40511
|
+
if (quotaPollCheckedAt(stored) > reading2.checkedAt)
|
|
40512
|
+
return stored;
|
|
40513
|
+
if (quotaSnapshotCheckedAt(stored) <= reading2.checkedAt)
|
|
40273
40514
|
return reading2;
|
|
40274
|
-
const five_hour =
|
|
40515
|
+
const five_hour = !reading2.five_hour && (stored.five_hour?.checkedAt ?? 0) <= reading2.checkedAt ? undefined : newer(reading2.five_hour, stored.five_hour);
|
|
40275
40516
|
const seven_day = newer(reading2.seven_day, stored.seven_day);
|
|
40276
|
-
const scoped = new Map(reading2.scoped.map((window2) => [window2.id, window2]));
|
|
40277
|
-
for (const window2 of stored.scoped ?? []) {
|
|
40278
|
-
const merged = newer(scoped.get(window2.id), window2);
|
|
40279
|
-
if (merged)
|
|
40280
|
-
scoped.set(window2.id, merged);
|
|
40281
|
-
}
|
|
40282
40517
|
const fieldSources2 = { ...reading2.fieldSources };
|
|
40283
|
-
if (five_hour
|
|
40284
|
-
fieldSources2.five_hour = stored
|
|
40285
|
-
if (seven_day !== reading2.seven_day
|
|
40286
|
-
fieldSources2.seven_day = stored
|
|
40518
|
+
if (five_hour && five_hour !== reading2.five_hour)
|
|
40519
|
+
fieldSources2.five_hour = quotaFieldSource(stored, "five_hour");
|
|
40520
|
+
if (seven_day !== reading2.seven_day)
|
|
40521
|
+
fieldSources2.seven_day = quotaFieldSource(stored, "seven_day");
|
|
40287
40522
|
return {
|
|
40288
40523
|
...reading2,
|
|
40289
40524
|
five_hour,
|
|
40290
40525
|
...seven_day && { seven_day },
|
|
40291
|
-
scoped: [...scoped.values()],
|
|
40292
|
-
checkedAt: quotaSnapshotOmitsFiveHour(stored) ? stored.checkedAt : quotaSnapshotOmitsFiveHour(reading2) ? reading2.checkedAt : Math.max(reading2.checkedAt, quotaSnapshotCheckedAt(stored)),
|
|
40293
40526
|
fieldSources: fieldSources2
|
|
40294
40527
|
};
|
|
40295
40528
|
}
|
|
@@ -41996,7 +42229,7 @@ function createNativeVaultRuntime(options) {
|
|
|
41996
42229
|
async getOrCreateAuthLineage(receipt) {
|
|
41997
42230
|
let lineage;
|
|
41998
42231
|
const kept = await mutateReceipt(receipt, (old) => {
|
|
41999
|
-
lineage = old.authLineageId ??
|
|
42232
|
+
lineage = old.authLineageId ?? randomUUID23();
|
|
42000
42233
|
return { ...old, authLineageId: lineage };
|
|
42001
42234
|
});
|
|
42002
42235
|
return kept ? lineage : undefined;
|
|
@@ -42018,9 +42251,11 @@ function createNativeVaultRuntime(options) {
|
|
|
42018
42251
|
},
|
|
42019
42252
|
async fetchQuota(routeId, fetchImpl = fetch, signal, dispatch, purpose) {
|
|
42020
42253
|
const row = await rowFor(await runtime2.read(), routeId);
|
|
42254
|
+
const cacheOnly = purpose === "background-cache";
|
|
42021
42255
|
let swept;
|
|
42022
|
-
if (purpose === "background" && row) {
|
|
42023
|
-
|
|
42256
|
+
if ((purpose === "background" || cacheOnly) && row) {
|
|
42257
|
+
if (!cacheOnly)
|
|
42258
|
+
await refreshInsula();
|
|
42024
42259
|
check(signal);
|
|
42025
42260
|
swept = insulaLookup(row.accountIdentity);
|
|
42026
42261
|
if (swept.miss === "cold" || swept.miss === "upstream-failed") {
|
|
@@ -42028,6 +42263,8 @@ function createNativeVaultRuntime(options) {
|
|
|
42028
42263
|
throw new QuotaRefreshDeferred;
|
|
42029
42264
|
}
|
|
42030
42265
|
}
|
|
42266
|
+
if (cacheOnly && (swept?.miss || !swept?.reading))
|
|
42267
|
+
throw new QuotaRefreshDeferred;
|
|
42031
42268
|
const state = await readNativeRuntime(paths2.runtime, paths2.storageId);
|
|
42032
42269
|
const before = state.status === "ready" ? state.state.accounts[row?.routeId ?? ""] : undefined;
|
|
42033
42270
|
const matchingBefore = (receipt) => {
|
|
@@ -42041,7 +42278,7 @@ function createNativeVaultRuntime(options) {
|
|
|
42041
42278
|
miss: "unreachable"
|
|
42042
42279
|
};
|
|
42043
42280
|
let accountChanged = false;
|
|
42044
|
-
if (purpose === "background") {
|
|
42281
|
+
if (purpose === "background" || cacheOnly) {
|
|
42045
42282
|
if (swept && row?.accountIdentity && receipt.assertedAccountIdentity === row.accountIdentity)
|
|
42046
42283
|
lookup = swept;
|
|
42047
42284
|
else if (swept)
|
|
@@ -42056,7 +42293,7 @@ function createNativeVaultRuntime(options) {
|
|
|
42056
42293
|
const stored = matchingBefore(receipt)?.quota;
|
|
42057
42294
|
return mergeInsulaQuota(reading2, stored ? fromNativeQuotaMap(stored) : undefined);
|
|
42058
42295
|
}
|
|
42059
|
-
if (purpose === "admission" && lookup.miss === "upstream-failed")
|
|
42296
|
+
if (cacheOnly || purpose === "admission" && lookup.miss === "upstream-failed")
|
|
42060
42297
|
return;
|
|
42061
42298
|
return fetchOAuthQuotaSnapshot({
|
|
42062
42299
|
accessToken: receipt.accessToken,
|
|
@@ -42859,7 +43096,7 @@ function buildNativeAccountRuntime(options, env, runtimeSettings) {
|
|
|
42859
43096
|
const sameAccount = old?.kind === "local" && old.rowId === binding2.rowId && binding2.identity !== undefined && old.identity === binding2.identity;
|
|
42860
43097
|
const next = {
|
|
42861
43098
|
binding: binding2,
|
|
42862
|
-
...row.credential?.type === "oauth" ? { authLineageId:
|
|
43099
|
+
...row.credential?.type === "oauth" ? { authLineageId: randomUUID24() } : {}
|
|
42863
43100
|
};
|
|
42864
43101
|
if (sameAccount && previous) {
|
|
42865
43102
|
for (const key of [
|
|
@@ -43049,7 +43286,7 @@ function buildNativeAccountRuntime(options, env, runtimeSettings) {
|
|
|
43049
43286
|
throw new NativeRuntimeError("publication-refused");
|
|
43050
43287
|
const previous = relayFlags(read.settings.relay);
|
|
43051
43288
|
const previousGeneration = relayGeneration(read.settings.relay);
|
|
43052
|
-
const generation =
|
|
43289
|
+
const generation = randomUUID24();
|
|
43053
43290
|
const metadata2 = await readNativeRuntime(paths3.runtime, paths3.storageId);
|
|
43054
43291
|
const token2 = patch.token === undefined ? metadata2.status === "ready" ? metadata2.state.relay?.token : undefined : patch.token?.trim();
|
|
43055
43292
|
const next = relayFlags({
|
|
@@ -43290,7 +43527,7 @@ function buildNativeAccountRuntime(options, env, runtimeSettings) {
|
|
|
43290
43527
|
const snapshot = await runtime2.read();
|
|
43291
43528
|
if (snapshot.mode !== "local")
|
|
43292
43529
|
throw new NativeCustodyError("route-unavailable");
|
|
43293
|
-
const id2 = input.routeId === "main" ? await physicalId("main").catch(() =>
|
|
43530
|
+
const id2 = input.routeId === "main" ? await physicalId("main").catch(() => randomUUID24()) : input.routeId;
|
|
43294
43531
|
if (input.credential.type === "api" && (input.routeId === "main" || id2 === snapshot.settings.mainAccountId))
|
|
43295
43532
|
throw new NativeCustodyError("route-unavailable");
|
|
43296
43533
|
const old = await poolRow(id2);
|
|
@@ -43329,7 +43566,7 @@ function buildNativeAccountRuntime(options, env, runtimeSettings) {
|
|
|
43329
43566
|
return row;
|
|
43330
43567
|
},
|
|
43331
43568
|
addApi: (input) => runtime2.putAccount({
|
|
43332
|
-
routeId: input.routeId ??
|
|
43569
|
+
routeId: input.routeId ?? randomUUID24(),
|
|
43333
43570
|
label: input.label,
|
|
43334
43571
|
replace: input.replace,
|
|
43335
43572
|
credential: {
|
|
@@ -43651,7 +43888,7 @@ function buildNativeAccountRuntime(options, env, runtimeSettings) {
|
|
|
43651
43888
|
return;
|
|
43652
43889
|
let lineage;
|
|
43653
43890
|
const kept = await metadataPublications.run(() => localMetadataWrite(fence, (old) => {
|
|
43654
|
-
lineage = old.authLineageId ??
|
|
43891
|
+
lineage = old.authLineageId ?? randomUUID24();
|
|
43655
43892
|
return {
|
|
43656
43893
|
...old,
|
|
43657
43894
|
authLineageId: lineage
|
|
@@ -43681,6 +43918,8 @@ function buildNativeAccountRuntime(options, env, runtimeSettings) {
|
|
|
43681
43918
|
const snapshot = await runtime2.read();
|
|
43682
43919
|
if (snapshot.mode === "claustrum")
|
|
43683
43920
|
return vault.fetchQuota(routeId, fetchImpl, signal, dispatch, purpose);
|
|
43921
|
+
if (purpose === "background-cache")
|
|
43922
|
+
throw new QuotaRefreshDeferred;
|
|
43684
43923
|
const before = snapshot.accounts.find((row) => row.id === routeId);
|
|
43685
43924
|
const result = await fetchReading(routeId, fetchImpl, signal, (attempt, transport) => fetchOAuthQuotaSnapshot({
|
|
43686
43925
|
accessToken: attempt.access,
|
|
@@ -43871,9 +44110,9 @@ function relayGeneration(input) {
|
|
|
43871
44110
|
return generation;
|
|
43872
44111
|
}
|
|
43873
44112
|
// src/native-host-auth-write.ts
|
|
43874
|
-
import { randomUUID as
|
|
43875
|
-
import { lstat as
|
|
43876
|
-
import { dirname as dirname22, isAbsolute as isAbsolute6, join as
|
|
44113
|
+
import { randomUUID as randomUUID25 } from "node:crypto";
|
|
44114
|
+
import { lstat as lstat20, open as open14, rename as rename15, unlink as unlink9 } from "node:fs/promises";
|
|
44115
|
+
import { dirname as dirname22, isAbsolute as isAbsolute6, join as join29 } from "node:path";
|
|
43877
44116
|
class NativeHostAuthWriteError extends Error {
|
|
43878
44117
|
code;
|
|
43879
44118
|
effect;
|
|
@@ -43934,11 +44173,11 @@ async function writeNativeHostAuth(input, hooks = {}) {
|
|
|
43934
44173
|
const removingFile = host2 === "pi" && Object.keys(next).length === 0;
|
|
43935
44174
|
const outcome = Object.freeze(host2 === "pi" ? { status: "removed", file: removingFile ? "unlinked" : "replaced" } : { status: snapshot.data === undefined ? "created" : "replaced" });
|
|
43936
44175
|
if (!removingFile) {
|
|
43937
|
-
stage =
|
|
44176
|
+
stage = join29(dirname22(authPath), `.native-host-auth-write.${randomUUID25()}.tmp`);
|
|
43938
44177
|
const bytes = `${JSON.stringify(next, null, 2)}
|
|
43939
44178
|
`;
|
|
43940
44179
|
const nativeCreated = await createWindowsOwnerOnly(stage, bytes);
|
|
43941
|
-
const handle = await
|
|
44180
|
+
const handle = await open14(stage, nativeCreated ? "r+" : "wx", 384);
|
|
43942
44181
|
try {
|
|
43943
44182
|
const identity5 = await handle.stat();
|
|
43944
44183
|
ownedStage = { dev: identity5.dev, ino: identity5.ino };
|
|
@@ -43984,7 +44223,7 @@ async function writeNativeHostAuth(input, hooks = {}) {
|
|
|
43984
44223
|
} finally {
|
|
43985
44224
|
if (stage && ownedStage) {
|
|
43986
44225
|
try {
|
|
43987
|
-
const current = await
|
|
44226
|
+
const current = await lstat20(stage);
|
|
43988
44227
|
if (current.dev === ownedStage.dev && current.ino === ownedStage.ino)
|
|
43989
44228
|
await unlink9(stage);
|
|
43990
44229
|
} catch {}
|
|
@@ -43993,8 +44232,8 @@ async function writeNativeHostAuth(input, hooks = {}) {
|
|
|
43993
44232
|
}
|
|
43994
44233
|
|
|
43995
44234
|
// src/native-migration.ts
|
|
43996
|
-
import { createHash as
|
|
43997
|
-
import { lstat as
|
|
44235
|
+
import { createHash as createHash27 } from "node:crypto";
|
|
44236
|
+
import { lstat as lstat21, mkdir as mkdir16, unlink as unlink10 } from "node:fs/promises";
|
|
43998
44237
|
import { dirname as dirname23, isAbsolute as isAbsolute7 } from "node:path";
|
|
43999
44238
|
class NativeMigrationError extends Error {
|
|
44000
44239
|
code;
|
|
@@ -44052,7 +44291,7 @@ function owned(snapshot) {
|
|
|
44052
44291
|
}
|
|
44053
44292
|
async function absent(path2) {
|
|
44054
44293
|
try {
|
|
44055
|
-
await
|
|
44294
|
+
await lstat21(path2);
|
|
44056
44295
|
return false;
|
|
44057
44296
|
} catch (error) {
|
|
44058
44297
|
if (error instanceof Error && "code" in error && error.code === "ENOENT")
|
|
@@ -44385,7 +44624,7 @@ function routingBytes(captured) {
|
|
|
44385
44624
|
`;
|
|
44386
44625
|
}
|
|
44387
44626
|
function bytesDigest(bytes) {
|
|
44388
|
-
return bytes === undefined ? null :
|
|
44627
|
+
return bytes === undefined ? null : createHash27("sha256").update(bytes).digest("hex");
|
|
44389
44628
|
}
|
|
44390
44629
|
function expectedAuth(input, snapshot) {
|
|
44391
44630
|
const entry2 = inspectNativeHostAuthEntry(input.host, snapshot.data);
|
|
@@ -44782,7 +45021,7 @@ async function runNativeMigration(input, hooks = {}) {
|
|
|
44782
45021
|
refuse3();
|
|
44783
45022
|
await step("before:host");
|
|
44784
45023
|
await recheck(false);
|
|
44785
|
-
await
|
|
45024
|
+
await mkdir16(dirname23(operation.hostAuthPath), {
|
|
44786
45025
|
recursive: true,
|
|
44787
45026
|
mode: 448
|
|
44788
45027
|
});
|
|
@@ -47645,7 +47884,7 @@ function waitForRequest(pending, signal) {
|
|
|
47645
47884
|
});
|
|
47646
47885
|
}
|
|
47647
47886
|
// src/native-vault-connection.ts
|
|
47648
|
-
import { dirname as dirname25, join as
|
|
47887
|
+
import { dirname as dirname25, join as join30, resolve as resolve24 } from "node:path";
|
|
47649
47888
|
// ../../node_modules/@cortexkit/common-auth/dist/opencode2/integration.js
|
|
47650
47889
|
var PLACEHOLDER_PREFIX = "common-auth-placeholder";
|
|
47651
47890
|
var PLACEHOLDER_LIFETIME_MS = 365 * 24 * 60 * 60 * 1000;
|
|
@@ -47653,14 +47892,14 @@ function placeholderSecret(integrationID) {
|
|
|
47653
47892
|
return `${PLACEHOLDER_PREFIX}.${integrationID}`;
|
|
47654
47893
|
}
|
|
47655
47894
|
// ../../node_modules/@cortexkit/common-auth/dist/pi-slot/slot.js
|
|
47656
|
-
import { constants as
|
|
47657
|
-
import { lstat as
|
|
47895
|
+
import { constants as constants6 } from "node:fs";
|
|
47896
|
+
import { lstat as lstat22, mkdir as mkdir18, open as open15, unlink as unlink11 } from "node:fs/promises";
|
|
47658
47897
|
import { dirname as dirname24, resolve as resolve23 } from "node:path";
|
|
47659
47898
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
47660
47899
|
|
|
47661
47900
|
// ../../node_modules/@cortexkit/common-auth/dist/pi-slot/lock.js
|
|
47662
47901
|
import { rmdirSync, statSync as statSync7, utimesSync } from "node:fs";
|
|
47663
|
-
import { mkdir as
|
|
47902
|
+
import { mkdir as mkdir17, rmdir, stat as stat13, utimes } from "node:fs/promises";
|
|
47664
47903
|
var STALE = 30000;
|
|
47665
47904
|
var RENEWAL_STALE = 1e4;
|
|
47666
47905
|
var UPDATE = 3000;
|
|
@@ -47696,7 +47935,7 @@ async function remove(path2) {
|
|
|
47696
47935
|
}
|
|
47697
47936
|
async function acquire(path2, stale) {
|
|
47698
47937
|
try {
|
|
47699
|
-
await
|
|
47938
|
+
await mkdir17(path2);
|
|
47700
47939
|
} catch (error) {
|
|
47701
47940
|
if (errorCode4(error) !== "EEXIST")
|
|
47702
47941
|
throw error;
|
|
@@ -47942,13 +48181,13 @@ async function safeRead(path2, afterReadCheck) {
|
|
|
47942
48181
|
}
|
|
47943
48182
|
let file;
|
|
47944
48183
|
try {
|
|
47945
|
-
file = await
|
|
48184
|
+
file = await open15(path2, constants6.O_RDONLY | constants6.O_NOFOLLOW);
|
|
47946
48185
|
const stat14 = await file.stat();
|
|
47947
48186
|
if (!stat14.isFile()) {
|
|
47948
48187
|
throw new PiSlotError("unsafe-path");
|
|
47949
48188
|
}
|
|
47950
48189
|
await afterReadCheck?.(path2);
|
|
47951
|
-
const current = await
|
|
48190
|
+
const current = await lstat22(path2);
|
|
47952
48191
|
if (!current.isFile() || current.dev !== stat14.dev || current.ino !== stat14.ino) {
|
|
47953
48192
|
throw new PiSlotError("unsafe-path");
|
|
47954
48193
|
}
|
|
@@ -48016,7 +48255,7 @@ function replaceEntry(auth, provider, next) {
|
|
|
48016
48255
|
async function syncDir(path2) {
|
|
48017
48256
|
if (windowsPrivateFiles)
|
|
48018
48257
|
return;
|
|
48019
|
-
const dir = await
|
|
48258
|
+
const dir = await open15(path2, "r");
|
|
48020
48259
|
try {
|
|
48021
48260
|
await dir.sync();
|
|
48022
48261
|
} finally {
|
|
@@ -48063,7 +48302,7 @@ function createPiSlot(options, onStep, hooks) {
|
|
|
48063
48302
|
if (windowsPrivateFiles)
|
|
48064
48303
|
await ensurePrivateDirectory(dirname24(authPath));
|
|
48065
48304
|
else
|
|
48066
|
-
await
|
|
48305
|
+
await mkdir18(dirname24(authPath), { recursive: true, mode: 448 });
|
|
48067
48306
|
const deadline = Date.now() + timeout;
|
|
48068
48307
|
let retry = 0;
|
|
48069
48308
|
while (!release2) {
|
|
@@ -48100,7 +48339,7 @@ function createPiSlot(options, onStep, hooks) {
|
|
|
48100
48339
|
}
|
|
48101
48340
|
async function deleteStash(assertLock) {
|
|
48102
48341
|
try {
|
|
48103
|
-
const file = await
|
|
48342
|
+
const file = await lstat22(stashPath);
|
|
48104
48343
|
if (!file.isFile())
|
|
48105
48344
|
throw new PiSlotError("unsafe-path");
|
|
48106
48345
|
} catch (error) {
|
|
@@ -48111,7 +48350,7 @@ function createPiSlot(options, onStep, hooks) {
|
|
|
48111
48350
|
if (windowsPrivateFiles)
|
|
48112
48351
|
await verifySafeDirectory(dirname24(stashPath));
|
|
48113
48352
|
else {
|
|
48114
|
-
const dir = await
|
|
48353
|
+
const dir = await lstat22(dirname24(stashPath));
|
|
48115
48354
|
if (!dir.isDirectory() || (dir.mode & 511) !== 448)
|
|
48116
48355
|
throw new PiSlotError("unsafe-path");
|
|
48117
48356
|
}
|
|
@@ -48150,7 +48389,7 @@ function createPiSlot(options, onStep, hooks) {
|
|
|
48150
48389
|
function nativePiAuthSlotPaths(options) {
|
|
48151
48390
|
return {
|
|
48152
48391
|
authPath: resolve24(options.authPath),
|
|
48153
|
-
stashPath:
|
|
48392
|
+
stashPath: join30(dirname25(getHostClaustrumEnrollmentPaths("pi", options.env).tokenPath), "pi-auth-slot", "anthropic.json"),
|
|
48154
48393
|
provider: "anthropic",
|
|
48155
48394
|
placeholderKey: placeholderSecret("anthropic")
|
|
48156
48395
|
};
|
|
@@ -48318,11 +48557,11 @@ function requestMethod(input) {
|
|
|
48318
48557
|
return input instanceof Request ? input.method : "GET";
|
|
48319
48558
|
}
|
|
48320
48559
|
// src/prime.ts
|
|
48321
|
-
import { createHash as
|
|
48560
|
+
import { createHash as createHash28 } from "node:crypto";
|
|
48322
48561
|
import * as fs4 from "node:fs";
|
|
48323
|
-
import { mkdir as
|
|
48562
|
+
import { mkdir as mkdir19, readdir as readdir8, rm as rm11, stat as stat14, writeFile as writeFile7 } from "node:fs/promises";
|
|
48324
48563
|
import { tmpdir as tmpdir8 } from "node:os";
|
|
48325
|
-
import { join as
|
|
48564
|
+
import { join as join31, resolve as resolve25 } from "node:path";
|
|
48326
48565
|
var CLAUDE_PRIME_COMMAND_NAME = "claude-prime";
|
|
48327
48566
|
var PRIME_TICK_MS = 60000;
|
|
48328
48567
|
var PRIME_DUE_OFFSET_MS = 60000;
|
|
@@ -48510,7 +48749,7 @@ function buildPrimeRequestBody() {
|
|
|
48510
48749
|
return JSON.parse(JSON.stringify(PRIME_REQUEST_BODY));
|
|
48511
48750
|
}
|
|
48512
48751
|
function defaultMarkerDir() {
|
|
48513
|
-
return
|
|
48752
|
+
return join31(tmpdir8(), "opencode-anthropic-auth", "prime");
|
|
48514
48753
|
}
|
|
48515
48754
|
function primeStorageFingerprint(storagePath) {
|
|
48516
48755
|
const absolutePath = resolve25(storagePath);
|
|
@@ -48527,22 +48766,22 @@ function primeStorageFingerprint(storagePath) {
|
|
|
48527
48766
|
throw error;
|
|
48528
48767
|
canonicalPath3 = absolutePath;
|
|
48529
48768
|
}
|
|
48530
|
-
return
|
|
48769
|
+
return createHash28("sha256").update(canonicalPath3).digest("hex").slice(0, 12);
|
|
48531
48770
|
}
|
|
48532
48771
|
function primeMarkerNamespaceDir(markerDir, storagePath) {
|
|
48533
|
-
return
|
|
48772
|
+
return join31(markerDir, primeStorageFingerprint(storagePath));
|
|
48534
48773
|
}
|
|
48535
48774
|
function primeAccountMarkerDir(markerDir, storagePath, accountFingerprint) {
|
|
48536
48775
|
if (!/^[a-f0-9]{12,}$/i.test(accountFingerprint)) {
|
|
48537
48776
|
throw new Error("Prime account fingerprint must be a hexadecimal digest");
|
|
48538
48777
|
}
|
|
48539
|
-
return
|
|
48778
|
+
return join31(primeMarkerNamespaceDir(markerDir, storagePath), accountFingerprint.toLowerCase().slice(0, 12));
|
|
48540
48779
|
}
|
|
48541
48780
|
function primeMarkerPath(markerDir, accountId2, resetsAtEpochMs) {
|
|
48542
|
-
return
|
|
48781
|
+
return join31(markerDir, `${encodeURIComponent(accountId2)}-${resetsAtEpochMs}`);
|
|
48543
48782
|
}
|
|
48544
48783
|
function primeBootstrapMarkerPath(markerDir, accountId2) {
|
|
48545
|
-
return
|
|
48784
|
+
return join31(markerDir, `${encodeURIComponent(accountId2)}-bootstrap`);
|
|
48546
48785
|
}
|
|
48547
48786
|
function primeAccountLabel(id2, fallback) {
|
|
48548
48787
|
if (id2 === "main")
|
|
@@ -48819,16 +49058,16 @@ class PrimeManager {
|
|
|
48819
49058
|
}
|
|
48820
49059
|
const postClaimStorage = await this.options.loadStorage();
|
|
48821
49060
|
if (this.stopped || postClaimStorage?.prime?.enabled !== true) {
|
|
48822
|
-
await Promise.all(markerPaths.map((path2) =>
|
|
49061
|
+
await Promise.all(markerPaths.map((path2) => rm11(path2, { force: true })));
|
|
48823
49062
|
return;
|
|
48824
49063
|
}
|
|
48825
49064
|
if (markerEpoch !== undefined) {
|
|
48826
|
-
await
|
|
49065
|
+
await rm11(bootstrapMarkerPath, { force: true });
|
|
48827
49066
|
if (accountIdentity) {
|
|
48828
|
-
await
|
|
49067
|
+
await rm11(this.verifiedMarkerPath(accountIdentity), { force: true });
|
|
48829
49068
|
}
|
|
48830
49069
|
if (this.stopped) {
|
|
48831
|
-
await Promise.all(markerPaths.map((path2) =>
|
|
49070
|
+
await Promise.all(markerPaths.map((path2) => rm11(path2, { force: true })));
|
|
48832
49071
|
return;
|
|
48833
49072
|
}
|
|
48834
49073
|
}
|
|
@@ -48850,7 +49089,7 @@ class PrimeManager {
|
|
|
48850
49089
|
return [markerPath];
|
|
48851
49090
|
const verifiedPath = this.verifiedMarkerPath(accountIdentity, resetsAtEpochMs);
|
|
48852
49091
|
if (resetsAtEpochMs === undefined) {
|
|
48853
|
-
const digest2 =
|
|
49092
|
+
const digest2 = createHash28("sha256").update(accountIdentity).digest("hex");
|
|
48854
49093
|
if ((await this.readMarkerNames(this.verifiedMarkerDir())).some((name) => name.startsWith(`${digest2}-`)))
|
|
48855
49094
|
return null;
|
|
48856
49095
|
}
|
|
@@ -48859,15 +49098,15 @@ class PrimeManager {
|
|
|
48859
49098
|
return [markerPath, verifiedPath];
|
|
48860
49099
|
}
|
|
48861
49100
|
verifiedMarkerDir() {
|
|
48862
|
-
return
|
|
49101
|
+
return join31(this.options.markerDir ?? defaultMarkerDir(), "accounts");
|
|
48863
49102
|
}
|
|
48864
49103
|
verifiedMarkerPath(accountIdentity, resetsAtEpochMs) {
|
|
48865
|
-
const digest2 =
|
|
48866
|
-
return
|
|
49104
|
+
const digest2 = createHash28("sha256").update(accountIdentity).digest("hex");
|
|
49105
|
+
return join31(this.verifiedMarkerDir(), `${digest2}-${resetsAtEpochMs ?? "bootstrap"}`);
|
|
48867
49106
|
}
|
|
48868
49107
|
async readMarkerNames(dir) {
|
|
48869
49108
|
try {
|
|
48870
|
-
return await
|
|
49109
|
+
return await readdir8(dir);
|
|
48871
49110
|
} catch (error) {
|
|
48872
49111
|
if (error.code === "ENOENT")
|
|
48873
49112
|
return [];
|
|
@@ -48876,10 +49115,10 @@ class PrimeManager {
|
|
|
48876
49115
|
}
|
|
48877
49116
|
async tryClaimPath(dir, markerPath) {
|
|
48878
49117
|
try {
|
|
48879
|
-
await
|
|
49118
|
+
await mkdir19(dir, { recursive: true });
|
|
48880
49119
|
} catch {}
|
|
48881
49120
|
try {
|
|
48882
|
-
await
|
|
49121
|
+
await writeFile7(markerPath, "", { encoding: "utf8", flag: "wx" });
|
|
48883
49122
|
return markerPath;
|
|
48884
49123
|
} catch (error) {
|
|
48885
49124
|
const code2 = error.code;
|
|
@@ -48892,7 +49131,7 @@ class PrimeManager {
|
|
|
48892
49131
|
const dir = this.markerDir(accountFingerprint);
|
|
48893
49132
|
const prefix = `${encodeURIComponent(accountId2)}-`;
|
|
48894
49133
|
try {
|
|
48895
|
-
return (await
|
|
49134
|
+
return (await readdir8(dir)).some((entry2) => entry2.startsWith(prefix));
|
|
48896
49135
|
} catch (error) {
|
|
48897
49136
|
if (error.code === "ENOENT")
|
|
48898
49137
|
return false;
|
|
@@ -49004,21 +49243,21 @@ class PrimeManager {
|
|
|
49004
49243
|
async sweepMarkerDirectory(dir, now) {
|
|
49005
49244
|
let entries;
|
|
49006
49245
|
try {
|
|
49007
|
-
entries = await
|
|
49246
|
+
entries = await readdir8(dir);
|
|
49008
49247
|
} catch {
|
|
49009
49248
|
return 0;
|
|
49010
49249
|
}
|
|
49011
49250
|
let swept = 0;
|
|
49012
49251
|
for (const entry2 of entries) {
|
|
49013
49252
|
try {
|
|
49014
|
-
const path2 =
|
|
49253
|
+
const path2 = join31(dir, entry2);
|
|
49015
49254
|
const s = await stat14(path2);
|
|
49016
49255
|
if (s.isDirectory()) {
|
|
49017
49256
|
swept += await this.sweepMarkerDirectory(path2, now);
|
|
49018
49257
|
continue;
|
|
49019
49258
|
}
|
|
49020
49259
|
if (now - s.mtimeMs > PRIME_MARKER_MAX_AGE_MS) {
|
|
49021
|
-
await
|
|
49260
|
+
await rm11(path2, { force: true });
|
|
49022
49261
|
swept += 1;
|
|
49023
49262
|
}
|
|
49024
49263
|
} catch {}
|
|
@@ -49033,24 +49272,24 @@ class PrimeManager {
|
|
|
49033
49272
|
}
|
|
49034
49273
|
}
|
|
49035
49274
|
// src/quota-header-feed.ts
|
|
49036
|
-
import { randomUUID as
|
|
49275
|
+
import { randomUUID as randomUUID26 } from "node:crypto";
|
|
49037
49276
|
import {
|
|
49038
49277
|
chmod as chmod3,
|
|
49039
|
-
lstat as
|
|
49040
|
-
mkdir as
|
|
49041
|
-
readdir as
|
|
49278
|
+
lstat as lstat23,
|
|
49279
|
+
mkdir as mkdir20,
|
|
49280
|
+
readdir as readdir9,
|
|
49042
49281
|
readFile as readFile18,
|
|
49043
49282
|
rename as rename16,
|
|
49044
|
-
rm as
|
|
49283
|
+
rm as rm12,
|
|
49045
49284
|
stat as stat15,
|
|
49046
|
-
writeFile as
|
|
49285
|
+
writeFile as writeFile8
|
|
49047
49286
|
} from "node:fs/promises";
|
|
49048
49287
|
import { tmpdir as tmpdir9 } from "node:os";
|
|
49049
|
-
import { join as
|
|
49288
|
+
import { join as join32 } from "node:path";
|
|
49050
49289
|
var QUOTA_HEADER_FEED_SCHEMA_VERSION = 3;
|
|
49051
49290
|
var QUOTA_HEADER_FEED_LEASE_MS = 180000;
|
|
49052
49291
|
function getDefaultQuotaHeaderFeedDirectory() {
|
|
49053
|
-
return process.env.OPENCODE_ANTHROPIC_AUTH_QUOTA_FEED_DIR ??
|
|
49292
|
+
return process.env.OPENCODE_ANTHROPIC_AUTH_QUOTA_FEED_DIR ?? join32(tmpdir9(), "opencode-anthropic-auth", "quota-header-feed");
|
|
49054
49293
|
}
|
|
49055
49294
|
function configuredAnthropicOAuthAccountCount(input) {
|
|
49056
49295
|
const fallbackCount = input.storage?.accounts.filter(isOAuthAccount).length ?? 0;
|
|
@@ -49208,8 +49447,8 @@ class QuotaHeaderFeedRegistry {
|
|
|
49208
49447
|
writeChain = Promise.resolve();
|
|
49209
49448
|
constructor(options = {}) {
|
|
49210
49449
|
this.options = options;
|
|
49211
|
-
const instanceId = options.instanceId ?? `${process.pid}-${
|
|
49212
|
-
this.filePath =
|
|
49450
|
+
const instanceId = options.instanceId ?? `${process.pid}-${randomUUID26()}`;
|
|
49451
|
+
this.filePath = join32(options.directory ?? getDefaultQuotaHeaderFeedDirectory(), `${instanceId}.json`);
|
|
49213
49452
|
}
|
|
49214
49453
|
publish(entry2) {
|
|
49215
49454
|
const { accountKey, quota: quota2 } = entry2;
|
|
@@ -49238,7 +49477,7 @@ class QuotaHeaderFeedRegistry {
|
|
|
49238
49477
|
return Promise.reject(new Error("Invalid quota header feed account key"));
|
|
49239
49478
|
this.writeChain = this.writeChain.catch(() => {}).then(async () => {
|
|
49240
49479
|
const directory = this.options.directory ?? getDefaultQuotaHeaderFeedDirectory();
|
|
49241
|
-
await
|
|
49480
|
+
await mkdir20(directory, { recursive: true, mode: 448 });
|
|
49242
49481
|
await chmod3(directory, 448);
|
|
49243
49482
|
await this.reapStaleSiblingLeases(directory);
|
|
49244
49483
|
let entries = {};
|
|
@@ -49248,14 +49487,14 @@ class QuotaHeaderFeedRegistry {
|
|
|
49248
49487
|
entries = record17.entries;
|
|
49249
49488
|
} catch {}
|
|
49250
49489
|
entries[accountKey] = cleanEntry;
|
|
49251
|
-
const tempPath = `${this.filePath}.${
|
|
49490
|
+
const tempPath = `${this.filePath}.${randomUUID26()}.tmp`;
|
|
49252
49491
|
try {
|
|
49253
|
-
await
|
|
49492
|
+
await writeFile8(tempPath, `${JSON.stringify({ version: QUOTA_HEADER_FEED_SCHEMA_VERSION, lease_horizon_ms: this.options.leaseMs ?? QUOTA_HEADER_FEED_LEASE_MS, entries })}
|
|
49254
49493
|
`, { mode: 384 });
|
|
49255
49494
|
await chmod3(tempPath, 384);
|
|
49256
49495
|
await rename16(tempPath, this.filePath);
|
|
49257
49496
|
} finally {
|
|
49258
|
-
await
|
|
49497
|
+
await rm12(tempPath, { force: true });
|
|
49259
49498
|
}
|
|
49260
49499
|
});
|
|
49261
49500
|
return this.writeChain;
|
|
@@ -49265,12 +49504,12 @@ class QuotaHeaderFeedRegistry {
|
|
|
49265
49504
|
const leaseMs = this.options.leaseMs ?? QUOTA_HEADER_FEED_LEASE_MS;
|
|
49266
49505
|
let names2;
|
|
49267
49506
|
try {
|
|
49268
|
-
names2 = await
|
|
49507
|
+
names2 = await readdir9(directory);
|
|
49269
49508
|
} catch {
|
|
49270
49509
|
return;
|
|
49271
49510
|
}
|
|
49272
49511
|
await Promise.all(names2.filter((name) => /^\d+-[0-9a-f-]+\.json$/i.test(name)).map(async (name) => {
|
|
49273
|
-
const path2 =
|
|
49512
|
+
const path2 = join32(directory, name);
|
|
49274
49513
|
if (path2 === this.filePath)
|
|
49275
49514
|
return;
|
|
49276
49515
|
try {
|
|
@@ -49278,10 +49517,10 @@ class QuotaHeaderFeedRegistry {
|
|
|
49278
49517
|
if (file.mtimeMs > now || now - file.mtimeMs < leaseMs)
|
|
49279
49518
|
return;
|
|
49280
49519
|
await this.options.beforeRemoveFile?.(path2);
|
|
49281
|
-
const current = await
|
|
49520
|
+
const current = await lstat23(path2);
|
|
49282
49521
|
if (current.ino !== file.ino || current.mtimeMs !== file.mtimeMs)
|
|
49283
49522
|
return;
|
|
49284
|
-
await (this.options.removeFile ?? ((target2) =>
|
|
49523
|
+
await (this.options.removeFile ?? ((target2) => rm12(target2)))(path2);
|
|
49285
49524
|
} catch {}
|
|
49286
49525
|
}));
|
|
49287
49526
|
}
|
|
@@ -49292,14 +49531,14 @@ class QuotaHeaderFeedRegistry {
|
|
|
49292
49531
|
const leaseMs = this.options.leaseMs ?? QUOTA_HEADER_FEED_LEASE_MS;
|
|
49293
49532
|
let names2;
|
|
49294
49533
|
try {
|
|
49295
|
-
names2 = (await
|
|
49534
|
+
names2 = (await readdir9(directory)).filter((name) => name.endsWith(".json"));
|
|
49296
49535
|
} catch {
|
|
49297
49536
|
return [];
|
|
49298
49537
|
}
|
|
49299
49538
|
const newest = new Map;
|
|
49300
49539
|
await Promise.all(names2.map(async (name) => {
|
|
49301
49540
|
try {
|
|
49302
|
-
const record17 = JSON.parse(await readFile18(
|
|
49541
|
+
const record17 = JSON.parse(await readFile18(join32(directory, name), "utf8"));
|
|
49303
49542
|
if (record17.version !== QUOTA_HEADER_FEED_SCHEMA_VERSION || !record17.entries || typeof record17.entries !== "object")
|
|
49304
49543
|
return;
|
|
49305
49544
|
for (const [accountKey, candidate] of Object.entries(record17.entries)) {
|
|
@@ -49318,7 +49557,7 @@ class QuotaHeaderFeedRegistry {
|
|
|
49318
49557
|
}
|
|
49319
49558
|
async dispose() {
|
|
49320
49559
|
await this.writeChain.catch(() => {});
|
|
49321
|
-
await
|
|
49560
|
+
await rm12(this.filePath, { force: true });
|
|
49322
49561
|
}
|
|
49323
49562
|
}
|
|
49324
49563
|
// src/quotas.ts
|
|
@@ -49479,8 +49718,8 @@ function buildClaudeQuotaSummary(input) {
|
|
|
49479
49718
|
`).trimEnd();
|
|
49480
49719
|
}
|
|
49481
49720
|
// src/request-gzip.ts
|
|
49482
|
-
import { mkdir as
|
|
49483
|
-
import { basename as basename14, dirname as dirname26, join as
|
|
49721
|
+
import { mkdir as mkdir21, readFile as readFile19, rename as rename17, rm as rm13, writeFile as writeFile9 } from "node:fs/promises";
|
|
49722
|
+
import { basename as basename14, dirname as dirname26, join as join33 } from "node:path";
|
|
49484
49723
|
import { promisify as promisify2 } from "node:util";
|
|
49485
49724
|
import { gzip as gzip2 } from "node:zlib";
|
|
49486
49725
|
var gzipAsync = promisify2(gzip2);
|
|
@@ -49594,7 +49833,7 @@ class RequestGzipLatch {
|
|
|
49594
49833
|
const age = typeof latchedAt === "number" && Number.isFinite(latchedAt) ? this.now() - latchedAt : -1;
|
|
49595
49834
|
if (age >= 0 && age < GZIP_LATCH_TTL_MS)
|
|
49596
49835
|
return true;
|
|
49597
|
-
await
|
|
49836
|
+
await rm13(this.path, { force: true }).catch(() => {});
|
|
49598
49837
|
return false;
|
|
49599
49838
|
}
|
|
49600
49839
|
async persist(status) {
|
|
@@ -49604,12 +49843,12 @@ class RequestGzipLatch {
|
|
|
49604
49843
|
return;
|
|
49605
49844
|
const temporary = `${this.path}.${process.pid}.tmp`;
|
|
49606
49845
|
try {
|
|
49607
|
-
await
|
|
49608
|
-
await
|
|
49846
|
+
await mkdir21(dirname26(this.path), { recursive: true });
|
|
49847
|
+
await writeFile9(temporary, `${JSON.stringify({ latchedAt: this.now(), status })}
|
|
49609
49848
|
`, { mode: 384 });
|
|
49610
49849
|
await rename17(temporary, this.path);
|
|
49611
49850
|
} catch {
|
|
49612
|
-
await
|
|
49851
|
+
await rm13(temporary, { force: true }).catch(() => {});
|
|
49613
49852
|
}
|
|
49614
49853
|
}
|
|
49615
49854
|
}
|
|
@@ -49628,7 +49867,7 @@ function getRequestGzipLatchPath(accountStoragePath, layout = LEGACY_STATE_LAYOU
|
|
|
49628
49867
|
return stateHomeFilePaths(layout.dir).requestGzip;
|
|
49629
49868
|
const name = basename14(accountStoragePath);
|
|
49630
49869
|
const stem = name.endsWith(".json") ? name.slice(0, -5) : name;
|
|
49631
|
-
return
|
|
49870
|
+
return join33(dirname26(accountStoragePath), `${stem}-request-gzip.json`);
|
|
49632
49871
|
}
|
|
49633
49872
|
async function decideRequestGzip(options) {
|
|
49634
49873
|
if (!CLAUDE_CODE_GZIPS_REQUEST_BODIES)
|
|
@@ -50039,7 +50278,7 @@ function createRoutingRelay(quotaFor) {
|
|
|
50039
50278
|
};
|
|
50040
50279
|
}
|
|
50041
50280
|
// src/routing-wait.ts
|
|
50042
|
-
import { randomUUID as
|
|
50281
|
+
import { randomUUID as randomUUID27 } from "node:crypto";
|
|
50043
50282
|
var messages = {
|
|
50044
50283
|
account_limit: "Waiting for the current account limit to reset.",
|
|
50045
50284
|
account_free: "Waiting for an account to become available.",
|
|
@@ -50052,7 +50291,7 @@ function startRoutingWait(input) {
|
|
|
50052
50291
|
const report2 = {
|
|
50053
50292
|
sessionId: input.sessionId,
|
|
50054
50293
|
kind: "wait",
|
|
50055
|
-
waitId:
|
|
50294
|
+
waitId: randomUUID27(),
|
|
50056
50295
|
reason: input.reason,
|
|
50057
50296
|
message: messages[input.reason],
|
|
50058
50297
|
...input.nextMs !== undefined ? { nextMs: Math.round(input.nextMs) } : {},
|
|
@@ -50071,9 +50310,9 @@ function startRoutingWait(input) {
|
|
|
50071
50310
|
};
|
|
50072
50311
|
}
|
|
50073
50312
|
// src/layout-fence.ts
|
|
50074
|
-
import { open as
|
|
50313
|
+
import { open as open16, readdir as readdir10 } from "node:fs/promises";
|
|
50075
50314
|
import { homedir as homedir7 } from "node:os";
|
|
50076
|
-
import { basename as basename15, join as
|
|
50315
|
+
import { basename as basename15, join as join34, resolve as resolve26 } from "node:path";
|
|
50077
50316
|
var STATE_PATH_OVERRIDE_VARIABLES = [
|
|
50078
50317
|
"OPENCODE_ANTHROPIC_AUTH_FILE",
|
|
50079
50318
|
"OPENCODE_ANTHROPIC_AUTH_STATE_FILE",
|
|
@@ -50109,7 +50348,7 @@ async function holderFenceBlockers(input) {
|
|
|
50109
50348
|
const files = new Set(input.files);
|
|
50110
50349
|
const blockers = [];
|
|
50111
50350
|
for (const dir of [...new Set(input.directories)].sort()) {
|
|
50112
|
-
const holderFile =
|
|
50351
|
+
const holderFile = join34(dir, HOLDER_FILE_NAME);
|
|
50113
50352
|
let contents;
|
|
50114
50353
|
try {
|
|
50115
50354
|
contents = await readHolderFile(holderFile);
|
|
@@ -50159,7 +50398,7 @@ function defaultPreNArtifactLocations(env = process.env, home = homedir7()) {
|
|
|
50159
50398
|
});
|
|
50160
50399
|
const rpcOverride = env.OPENCODE_ANTHROPIC_AUTH_RPC_DIR?.trim();
|
|
50161
50400
|
return {
|
|
50162
|
-
rpcRoots: [
|
|
50401
|
+
rpcRoots: [join34(root, "rpc")],
|
|
50163
50402
|
rpcDirs: rpcOverride ? [resolve26(rpcOverride)] : [],
|
|
50164
50403
|
cacheKeepDirs: [
|
|
50165
50404
|
...new Set([
|
|
@@ -50175,7 +50414,7 @@ function defaultPreNArtifactLocations(env = process.env, home = homedir7()) {
|
|
|
50175
50414
|
}
|
|
50176
50415
|
async function listDir(dir, blockers) {
|
|
50177
50416
|
try {
|
|
50178
|
-
return await
|
|
50417
|
+
return await readdir10(dir);
|
|
50179
50418
|
} catch (error) {
|
|
50180
50419
|
if (errorCode5(error) === "ENOENT")
|
|
50181
50420
|
return [];
|
|
@@ -50199,7 +50438,7 @@ async function readArtifact(path2, blockers, malformedIsStale) {
|
|
|
50199
50438
|
let mtimeMs;
|
|
50200
50439
|
let regular;
|
|
50201
50440
|
try {
|
|
50202
|
-
const file = await
|
|
50441
|
+
const file = await open16(path2, "r");
|
|
50203
50442
|
try {
|
|
50204
50443
|
text6 = await file.readFile("utf8");
|
|
50205
50444
|
const stat16 = await file.stat();
|
|
@@ -50247,12 +50486,12 @@ async function preNArtifactBlockers(locations, pidAlive2 = defaultLiveness2.pidA
|
|
|
50247
50486
|
const portDirs = [...locations.rpcDirs];
|
|
50248
50487
|
for (const root of locations.rpcRoots)
|
|
50249
50488
|
for (const name of await listDir(root, blockers))
|
|
50250
|
-
portDirs.push(
|
|
50489
|
+
portDirs.push(join34(root, name));
|
|
50251
50490
|
for (const dir of [...new Set(portDirs)]) {
|
|
50252
50491
|
for (const name of await listDir(dir, blockers)) {
|
|
50253
50492
|
if (!/^port-\d+\.json$/.test(name))
|
|
50254
50493
|
continue;
|
|
50255
|
-
const path2 =
|
|
50494
|
+
const path2 = join34(dir, name);
|
|
50256
50495
|
const filenamePid = publisherPid("rpc", name);
|
|
50257
50496
|
const isStale = (startedAt2) => malformedIsStale(filenamePid, startedAt2);
|
|
50258
50497
|
const record17 = await readArtifact(path2, blockers, isStale);
|
|
@@ -50274,7 +50513,7 @@ async function preNArtifactBlockers(locations, pidAlive2 = defaultLiveness2.pidA
|
|
|
50274
50513
|
const match = /^(\d+)-.+\.json$/.exec(name);
|
|
50275
50514
|
if (!match)
|
|
50276
50515
|
continue;
|
|
50277
|
-
const path2 =
|
|
50516
|
+
const path2 = join34(dir, name);
|
|
50278
50517
|
const filenamePid = publisherPid("cachekeep", name);
|
|
50279
50518
|
const record17 = await readArtifact(path2, blockers, (startedAt) => malformedIsStale(filenamePid, startedAt));
|
|
50280
50519
|
if (record17 === undefined || exempt(record17.value))
|
|
@@ -50543,14 +50782,14 @@ import { isDeepStrictEqual as isDeepStrictEqual7 } from "node:util";
|
|
|
50543
50782
|
import { parse as parse6 } from "jsonc-parser";
|
|
50544
50783
|
|
|
50545
50784
|
// src/settings-move-sources.ts
|
|
50546
|
-
import { lstat as
|
|
50785
|
+
import { lstat as lstat24, readFile as readFile20 } from "node:fs/promises";
|
|
50547
50786
|
var loggedSkips = new Set;
|
|
50548
50787
|
async function peerStoreAbsent(host2, store, options) {
|
|
50549
50788
|
try {
|
|
50550
50789
|
const pool = await resolveNativePoolPaths(store.account, store.state, await peerLayout(host2, options));
|
|
50551
50790
|
for (const config of [pool.config, nativeVaultPaths(pool).config]) {
|
|
50552
50791
|
try {
|
|
50553
|
-
await
|
|
50792
|
+
await lstat24(config);
|
|
50554
50793
|
return false;
|
|
50555
50794
|
} catch (error) {
|
|
50556
50795
|
if (error.code !== "ENOENT")
|
|
@@ -50703,7 +50942,7 @@ async function prepareSettingsMoveSources(options) {
|
|
|
50703
50942
|
const pool = own && options.ownPoolPaths ? options.ownPoolPaths : await resolveNativePoolPaths(store.account, store.state, own ? undefined : await peerLayout(host2, options));
|
|
50704
50943
|
const selected = mode === "claustrum" ? nativeVaultPaths(pool) : pool;
|
|
50705
50944
|
try {
|
|
50706
|
-
await
|
|
50945
|
+
await lstat24(selected.config);
|
|
50707
50946
|
} catch (error) {
|
|
50708
50947
|
if (error.code === "ENOENT") {
|
|
50709
50948
|
skip(host2, selected.config, mode === "claustrum" ? "vault settings have not been seeded" : "file is missing");
|
|
@@ -51007,7 +51246,7 @@ async function startSettings(options) {
|
|
|
51007
51246
|
refused2 = false;
|
|
51008
51247
|
};
|
|
51009
51248
|
const move = options.start?.move ?? runSettingsMoveAtStartup;
|
|
51010
|
-
const
|
|
51249
|
+
const open17 = options.start?.open ?? openSettingsService;
|
|
51011
51250
|
const diagnostic = {
|
|
51012
51251
|
outcome: refused2 ? "authority-refused" : "startup-failed"
|
|
51013
51252
|
};
|
|
@@ -51067,7 +51306,7 @@ async function startSettings(options) {
|
|
|
51067
51306
|
adopted = moved && !started.importPending;
|
|
51068
51307
|
if (!started.mayOpenService)
|
|
51069
51308
|
return { mode: started.mode, file, diagnostic };
|
|
51070
|
-
const service = track(await
|
|
51309
|
+
const service = track(await open17(serviceOptions(file)));
|
|
51071
51310
|
if (!service)
|
|
51072
51311
|
return { mode: started.mode, file, diagnostic };
|
|
51073
51312
|
if (started.importPending)
|
|
@@ -51084,7 +51323,7 @@ async function startSettings(options) {
|
|
|
51084
51323
|
if (!file)
|
|
51085
51324
|
return;
|
|
51086
51325
|
try {
|
|
51087
|
-
return track(await
|
|
51326
|
+
return track(await open17(serviceOptions(file)));
|
|
51088
51327
|
} catch {
|
|
51089
51328
|
return;
|
|
51090
51329
|
}
|
|
@@ -51215,20 +51454,20 @@ function executeLaneStartCommand(input) {
|
|
|
51215
51454
|
};
|
|
51216
51455
|
}
|
|
51217
51456
|
// src/state-layout-decision.ts
|
|
51218
|
-
import { mkdir as
|
|
51457
|
+
import { mkdir as mkdir22, readdir as readdir12 } from "node:fs/promises";
|
|
51219
51458
|
import { homedir as homedir9 } from "node:os";
|
|
51220
|
-
import { basename as basename17, dirname as dirname28, join as
|
|
51459
|
+
import { basename as basename17, dirname as dirname28, join as join37 } from "node:path";
|
|
51221
51460
|
|
|
51222
51461
|
// src/state-layout-move.ts
|
|
51223
|
-
import { randomUUID as
|
|
51224
|
-
import { lstat as
|
|
51225
|
-
import { basename as basename16, dirname as dirname27, join as
|
|
51462
|
+
import { randomUUID as randomUUID28 } from "node:crypto";
|
|
51463
|
+
import { lstat as lstat25, readdir as readdir11, readFile as readFile22, stat as stat16, unlink as unlink12 } from "node:fs/promises";
|
|
51464
|
+
import { basename as basename16, dirname as dirname27, join as join35, sep as sep2 } from "node:path";
|
|
51226
51465
|
var ACCOUNT_FILE = "anthropic-auth.json";
|
|
51227
51466
|
var ACCOUNT_STATE_FILE = "anthropic-auth-state.json";
|
|
51228
51467
|
var LAYOUT_LOCK_NAME = "runtime-layout";
|
|
51229
51468
|
var LAYOUT_LOCK_TIMEOUT_MS = 120000;
|
|
51230
51469
|
async function resolveLegacyDatasetFiles(configDir) {
|
|
51231
|
-
const oldPaths = await resolveNativePoolPaths(
|
|
51470
|
+
const oldPaths = await resolveNativePoolPaths(join35(configDir, ACCOUNT_FILE), join35(configDir, ACCOUNT_STATE_FILE), { kind: "legacy" });
|
|
51232
51471
|
return {
|
|
51233
51472
|
oldDir: await canonicalPath2(configDir),
|
|
51234
51473
|
oldPaths,
|
|
@@ -51236,8 +51475,8 @@ async function resolveLegacyDatasetFiles(configDir) {
|
|
|
51236
51475
|
};
|
|
51237
51476
|
}
|
|
51238
51477
|
async function resolveRelocationDataset(input) {
|
|
51239
|
-
const legacyConfig =
|
|
51240
|
-
const legacyState =
|
|
51478
|
+
const legacyConfig = join35(input.configDir, ACCOUNT_FILE);
|
|
51479
|
+
const legacyState = join35(input.configDir, ACCOUNT_STATE_FILE);
|
|
51241
51480
|
const { oldDir, oldPaths, oldVault } = await resolveLegacyDatasetFiles(input.configDir);
|
|
51242
51481
|
const stateHome = { kind: "state-home", dir: input.sub };
|
|
51243
51482
|
const newPaths = await resolveNativePoolPaths(legacyConfig, legacyState, stateHome);
|
|
@@ -51266,7 +51505,7 @@ async function resolveRelocationDataset(input) {
|
|
|
51266
51505
|
role,
|
|
51267
51506
|
from: pairs[role][0],
|
|
51268
51507
|
to: pairs[role][1],
|
|
51269
|
-
configured:
|
|
51508
|
+
configured: join35(input.configDir, STATE_HOME_FILE_NAMES[role])
|
|
51270
51509
|
})),
|
|
51271
51510
|
storageIds: { local: oldPaths.storageId, vault: oldVault.storageId }
|
|
51272
51511
|
};
|
|
@@ -51288,8 +51527,8 @@ function errorCode6(error) {
|
|
|
51288
51527
|
}
|
|
51289
51528
|
function transitionHolderFiles(dataset) {
|
|
51290
51529
|
return [
|
|
51291
|
-
|
|
51292
|
-
|
|
51530
|
+
join35(dataset.oldDir, HOLDER_FILE_NAME),
|
|
51531
|
+
join35(dataset.sub, HOLDER_FILE_NAME)
|
|
51293
51532
|
].sort();
|
|
51294
51533
|
}
|
|
51295
51534
|
async function acquireHolderLock(ctx, holderFile, timeoutMs) {
|
|
@@ -51306,7 +51545,7 @@ async function acquireHolderLock(ctx, holderFile, timeoutMs) {
|
|
|
51306
51545
|
}
|
|
51307
51546
|
}
|
|
51308
51547
|
async function acquireHolderLocks(dataset, ctx) {
|
|
51309
|
-
const oldHolders =
|
|
51548
|
+
const oldHolders = join35(dataset.oldDir, HOLDER_FILE_NAME);
|
|
51310
51549
|
for (const file of transitionHolderFiles(dataset)) {
|
|
51311
51550
|
if (file === oldHolders)
|
|
51312
51551
|
await stateLayoutStep(ctx, "old-holders-locking");
|
|
@@ -51512,7 +51751,7 @@ function restoringPath(path2, attempt) {
|
|
|
51512
51751
|
}
|
|
51513
51752
|
async function exists5(path2) {
|
|
51514
51753
|
try {
|
|
51515
|
-
await
|
|
51754
|
+
await lstat25(path2);
|
|
51516
51755
|
return true;
|
|
51517
51756
|
} catch (error) {
|
|
51518
51757
|
if (errorCode6(error) === "ENOENT")
|
|
@@ -51522,7 +51761,7 @@ async function exists5(path2) {
|
|
|
51522
51761
|
}
|
|
51523
51762
|
async function removeOwnStaging(path2) {
|
|
51524
51763
|
try {
|
|
51525
|
-
const info = await
|
|
51764
|
+
const info = await lstat25(path2);
|
|
51526
51765
|
if (info.isFile())
|
|
51527
51766
|
await unlink12(path2);
|
|
51528
51767
|
} catch (error) {
|
|
@@ -51565,7 +51804,7 @@ async function movePreconditionFailure(dataset, platform) {
|
|
|
51565
51804
|
for (const path2 of new Set([slot.configured, slot.from])) {
|
|
51566
51805
|
let info;
|
|
51567
51806
|
try {
|
|
51568
|
-
info = await
|
|
51807
|
+
info = await lstat25(path2);
|
|
51569
51808
|
} catch (error) {
|
|
51570
51809
|
if (errorCode6(error) === "ENOENT")
|
|
51571
51810
|
continue;
|
|
@@ -51582,7 +51821,7 @@ async function movePreconditionFailure(dataset, platform) {
|
|
|
51582
51821
|
anyOld ||= fromExists;
|
|
51583
51822
|
if (await exists5(slot.to)) {
|
|
51584
51823
|
if (fromExists) {
|
|
51585
|
-
const [a, b] = await Promise.all([
|
|
51824
|
+
const [a, b] = await Promise.all([lstat25(slot.from), lstat25(slot.to)]);
|
|
51586
51825
|
if (a.dev === b.dev && a.ino === b.ino)
|
|
51587
51826
|
return `${slot.from} and ${slot.to} are the same file`;
|
|
51588
51827
|
}
|
|
@@ -51720,9 +51959,9 @@ async function rollbackPreparing(record17, dataset, ctx) {
|
|
|
51720
51959
|
continue;
|
|
51721
51960
|
await owned2(ctx, { kind: "unlink", path: file.to }, expectDigest(file.to, file.sha256));
|
|
51722
51961
|
}
|
|
51723
|
-
for (const name of await
|
|
51962
|
+
for (const name of await readdir11(dataset.sub))
|
|
51724
51963
|
if (isLayoutRecordStage(name, attempt))
|
|
51725
|
-
await removeOwnStaging(
|
|
51964
|
+
await removeOwnStaging(join35(dataset.sub, name));
|
|
51726
51965
|
await stateLayoutStep(ctx, "rollback:destination-removed");
|
|
51727
51966
|
await ctx.fs.syncDirectory(dataset.sub);
|
|
51728
51967
|
await stateLayoutStep(ctx, "rollback:sub-flushed");
|
|
@@ -51825,7 +52064,7 @@ class MoveRefused extends Error {
|
|
|
51825
52064
|
}
|
|
51826
52065
|
}
|
|
51827
52066
|
async function moveDataset(dataset, ctx, checkArtifacts) {
|
|
51828
|
-
const attempt =
|
|
52067
|
+
const attempt = randomUUID28();
|
|
51829
52068
|
const files = [];
|
|
51830
52069
|
for (const slot of dataset.slots)
|
|
51831
52070
|
files.push({
|
|
@@ -51836,7 +52075,7 @@ async function moveDataset(dataset, ctx, checkArtifacts) {
|
|
|
51836
52075
|
});
|
|
51837
52076
|
const sha = (role) => files.find((file) => file.role === role)?.sha256 ?? null;
|
|
51838
52077
|
await ctx.stack.assertAll();
|
|
51839
|
-
const configDirPath =
|
|
52078
|
+
const configDirPath = join35(dataset.sub, STATE_HOME_FILE_NAMES.configDirMarker);
|
|
51840
52079
|
if (!await exists5(configDirPath)) {
|
|
51841
52080
|
const stage = `${configDirPath}.${attempt}.tmp`;
|
|
51842
52081
|
await createOwnerOnlyFile(ctx.fs, stage, Buffer.from(`${JSON.stringify({ version: 1, identity: dataset.identity })}
|
|
@@ -51847,7 +52086,7 @@ async function moveDataset(dataset, ctx, checkArtifacts) {
|
|
|
51847
52086
|
await removeOwnStaging(stage);
|
|
51848
52087
|
}
|
|
51849
52088
|
}
|
|
51850
|
-
const holderFile =
|
|
52089
|
+
const holderFile = join35(dataset.sub, HOLDER_FILE_NAME);
|
|
51851
52090
|
if (!await exists5(holderFile))
|
|
51852
52091
|
await writeJsonAtomic(holderFile, { version: 1, entries: [] }, {
|
|
51853
52092
|
beforeRename: () => ctx.stack.assertAll(),
|
|
@@ -51998,10 +52237,10 @@ async function rollbackAfterFailure(preparing, dataset, ctx) {
|
|
|
51998
52237
|
}
|
|
51999
52238
|
|
|
52000
52239
|
// src/state-layout-restore.ts
|
|
52001
|
-
import { randomUUID as
|
|
52240
|
+
import { randomUUID as randomUUID29 } from "node:crypto";
|
|
52002
52241
|
import { readFile as readFile23 } from "node:fs/promises";
|
|
52003
52242
|
import { homedir as homedir8 } from "node:os";
|
|
52004
|
-
import { join as
|
|
52243
|
+
import { join as join36 } from "node:path";
|
|
52005
52244
|
var DATA_ROLES = [
|
|
52006
52245
|
"localPoolState",
|
|
52007
52246
|
"localRoster",
|
|
@@ -52277,7 +52516,7 @@ async function newRestoreProblems(record17, dataset, options) {
|
|
|
52277
52516
|
if (await exists5(path2))
|
|
52278
52517
|
problems.push(`${path2} changed after the move and was kept; move it aside or delete it`);
|
|
52279
52518
|
}
|
|
52280
|
-
problems.push(...await holderProblems(
|
|
52519
|
+
problems.push(...await holderProblems(join36(dataset.sub, HOLDER_FILE_NAME), options.liveness, true), ...await holderProblems(join36(dataset.oldDir, HOLDER_FILE_NAME), options.liveness, false));
|
|
52281
52520
|
const artifacts = options.artifacts ?? defaultPreNArtifactLocations(options.env ?? process.env, options.home ?? homedir8());
|
|
52282
52521
|
for (const blocker of await preNArtifactBlockers(artifacts, options.liveness?.pidAlive, options.liveness))
|
|
52283
52522
|
problems.push(`${describeFenceBlocker(blocker)} is still running`);
|
|
@@ -52299,7 +52538,7 @@ async function startRestore(record17, dataset, ctx, options) {
|
|
|
52299
52538
|
const problems = await newRestoreProblems(record17, dataset, options);
|
|
52300
52539
|
if (problems.length)
|
|
52301
52540
|
throw refused2(`anthropic-auth can't restore its state to ${dataset.oldDir} yet: ${problems.join("; ")}. Then run ${RESTORE_LEGACY_COMMAND} again.`, [dataset.oldDir, dataset.sub]);
|
|
52302
|
-
const attempt =
|
|
52541
|
+
const attempt = randomUUID29();
|
|
52303
52542
|
const files = [];
|
|
52304
52543
|
for (const slot of dataset.slots) {
|
|
52305
52544
|
let digest2;
|
|
@@ -52466,7 +52705,7 @@ async function restoreLegacyLayout(options = {}) {
|
|
|
52466
52705
|
break;
|
|
52467
52706
|
case "restore-staged": {
|
|
52468
52707
|
await syncObservedRestoreRecord(dataset, ctx);
|
|
52469
|
-
const problems = await holderProblems(
|
|
52708
|
+
const problems = await holderProblems(join36(dataset.sub, HOLDER_FILE_NAME), options.liveness, true);
|
|
52470
52709
|
if (problems.length)
|
|
52471
52710
|
throw refused2(`anthropic-auth can't finish restoring its state to ${dataset.oldDir}: ${problems.join("; ")}. Close them, then run ${RESTORE_LEGACY_COMMAND} again.`, [dataset.sub]);
|
|
52472
52711
|
result = await finishStagedRestore(record17, dataset, ctx);
|
|
@@ -52588,8 +52827,8 @@ async function preflightOpenCodeAuthority(configDir, env, home) {
|
|
|
52588
52827
|
return;
|
|
52589
52828
|
}
|
|
52590
52829
|
}
|
|
52591
|
-
const config = env.OPENCODE_ANTHROPIC_AUTH_FILE?.trim() ||
|
|
52592
|
-
const state = env.OPENCODE_ANTHROPIC_AUTH_STATE_FILE?.trim() || (config.endsWith(ACCOUNT_FILE2) ?
|
|
52830
|
+
const config = env.OPENCODE_ANTHROPIC_AUTH_FILE?.trim() || join37(configDir, ACCOUNT_FILE2);
|
|
52831
|
+
const state = env.OPENCODE_ANTHROPIC_AUTH_STATE_FILE?.trim() || (config.endsWith(ACCOUNT_FILE2) ? join37(dirname28(config), ACCOUNT_STATE_FILE2) : `${config}.state.json`);
|
|
52593
52832
|
await preflightExistingAuthority(await resolveNativePoolPaths(config, state, LEGACY_STATE_LAYOUT), "opencode", env);
|
|
52594
52833
|
}
|
|
52595
52834
|
async function registerHeld(holderFile, paths2, kind, identity5, options, ctx) {
|
|
@@ -52624,7 +52863,7 @@ function deferLegacyRegistration(paths2, dir, options, error) {
|
|
|
52624
52863
|
}
|
|
52625
52864
|
async function registerLegacyHeld(oldDir, paths2, identity5, options, ctx) {
|
|
52626
52865
|
try {
|
|
52627
|
-
await registerHeld(
|
|
52866
|
+
await registerHeld(join37(oldDir, HOLDER_FILE_NAME), paths2, "legacy", identity5, options, ctx);
|
|
52628
52867
|
return {
|
|
52629
52868
|
status: "registered",
|
|
52630
52869
|
settled: Promise.resolve(),
|
|
@@ -52661,14 +52900,14 @@ async function admitHeld(dataset, options, ctx, moved, allowPendingCleanup) {
|
|
|
52661
52900
|
throw conflict;
|
|
52662
52901
|
}
|
|
52663
52902
|
await warnReappearedMetadata(record17);
|
|
52664
|
-
await registerHeld(
|
|
52903
|
+
await registerHeld(join37(dataset.sub, HOLDER_FILE_NAME), dataset.newPaths, "state-home", dataset.identity, options, ctx);
|
|
52665
52904
|
const layout = { kind: "state-home", dir: dataset.sub };
|
|
52666
52905
|
recordProcessStateLayout(dataset.oldPaths.legacyConfig, layout);
|
|
52667
52906
|
return { kind: "state-home", layout, dataset, record: record17, moved };
|
|
52668
52907
|
}
|
|
52669
52908
|
async function admitFast(dataset, options, ctx) {
|
|
52670
52909
|
try {
|
|
52671
|
-
await acquireHolderLock(ctx,
|
|
52910
|
+
await acquireHolderLock(ctx, join37(dataset.sub, HOLDER_FILE_NAME), ctx.holderLockTimeoutMs);
|
|
52672
52911
|
} catch (error) {
|
|
52673
52912
|
if (error instanceof LockContentionError)
|
|
52674
52913
|
busy(dataset.configDir, error);
|
|
@@ -52733,7 +52972,7 @@ async function oldFolderTransition(old, recordedMovingJournal) {
|
|
|
52733
52972
|
}
|
|
52734
52973
|
let names2;
|
|
52735
52974
|
try {
|
|
52736
|
-
names2 = await
|
|
52975
|
+
names2 = await readdir12(dirname28(journal));
|
|
52737
52976
|
} catch (error) {
|
|
52738
52977
|
if (error?.code === "ENOENT")
|
|
52739
52978
|
return false;
|
|
@@ -52776,7 +53015,7 @@ async function legacyWhileMoveOff(configDir, identity5, options, ctx, stayReason
|
|
|
52776
53015
|
await stateLayoutStep(ctx, "old-holders-locking");
|
|
52777
53016
|
let lockFailure2;
|
|
52778
53017
|
try {
|
|
52779
|
-
await acquireHolderLock(ctx,
|
|
53018
|
+
await acquireHolderLock(ctx, join37(old.oldDir, HOLDER_FILE_NAME), ctx.holderLockTimeoutMs);
|
|
52780
53019
|
} catch (error) {
|
|
52781
53020
|
lockFailure2 = { error };
|
|
52782
53021
|
}
|
|
@@ -52808,8 +53047,8 @@ async function decideOpenCodeStateLayout(options) {
|
|
|
52808
53047
|
holderLockTimeoutMs: options.timeouts?.holderMs
|
|
52809
53048
|
});
|
|
52810
53049
|
if (isExplicit(env)) {
|
|
52811
|
-
const legacyConfig = env.OPENCODE_ANTHROPIC_AUTH_FILE?.trim() ||
|
|
52812
|
-
const legacyState = env.OPENCODE_ANTHROPIC_AUTH_STATE_FILE?.trim() || (legacyConfig.endsWith(ACCOUNT_FILE2) ?
|
|
53050
|
+
const legacyConfig = env.OPENCODE_ANTHROPIC_AUTH_FILE?.trim() || join37(configDir, ACCOUNT_FILE2);
|
|
53051
|
+
const legacyState = env.OPENCODE_ANTHROPIC_AUTH_STATE_FILE?.trim() || (legacyConfig.endsWith(ACCOUNT_FILE2) ? join37(dirname28(legacyConfig), ACCOUNT_STATE_FILE2) : `${legacyConfig}.state.json`);
|
|
52813
53052
|
const paths2 = await resolveNativePoolPaths(legacyConfig, legacyState, LEGACY_STATE_LAYOUT);
|
|
52814
53053
|
const registration = await registerNativePoolHolder({
|
|
52815
53054
|
paths: paths2,
|
|
@@ -52832,7 +53071,7 @@ async function decideOpenCodeStateLayout(options) {
|
|
|
52832
53071
|
} catch (error) {
|
|
52833
53072
|
if (unmovedRegistrationPolicy(options) === "mandatory")
|
|
52834
53073
|
throw new HolderRegistrationError("unavailable", configDir, error);
|
|
52835
|
-
const paths2 = await resolveNativePoolPaths(
|
|
53074
|
+
const paths2 = await resolveNativePoolPaths(join37(configDir, ACCOUNT_FILE2), join37(configDir, ACCOUNT_STATE_FILE2), LEGACY_STATE_LAYOUT);
|
|
52836
53075
|
return chooseLegacy(paths2, [reason], true, deferLegacyRegistration(paths2, configDir, options, error));
|
|
52837
53076
|
}
|
|
52838
53077
|
identity5 = configIdentity(configDir);
|
|
@@ -52902,7 +53141,7 @@ async function decideOpenCodeStateLayout(options) {
|
|
|
52902
53141
|
try {
|
|
52903
53142
|
await stateLayoutStep(ctx, "old-holders-locking");
|
|
52904
53143
|
try {
|
|
52905
|
-
await acquireHolderLock(ctx,
|
|
53144
|
+
await acquireHolderLock(ctx, join37(dataset.oldDir, HOLDER_FILE_NAME), ctx.holderLockTimeoutMs);
|
|
52906
53145
|
} catch (error) {
|
|
52907
53146
|
if (unmovedRegistrationPolicy(options) === "mandatory")
|
|
52908
53147
|
throw error;
|
|
@@ -52966,7 +53205,7 @@ async function ensureSubfolder(sub, fs5) {
|
|
|
52966
53205
|
await ensureWindowsOwnerOnlyDirectory(sub);
|
|
52967
53206
|
return;
|
|
52968
53207
|
}
|
|
52969
|
-
const created = await
|
|
53208
|
+
const created = await mkdir22(sub, { recursive: true, mode: 448 });
|
|
52970
53209
|
if (created === undefined)
|
|
52971
53210
|
return;
|
|
52972
53211
|
for (let dir = dirname28(sub);; dir = dirname28(dir)) {
|
|
@@ -53489,6 +53728,7 @@ export {
|
|
|
53489
53728
|
quotaRefusalTiming,
|
|
53490
53729
|
quotaRefusalRetryAfterSeconds,
|
|
53491
53730
|
quotaRefusalDetails,
|
|
53731
|
+
quotaPollCheckedAt,
|
|
53492
53732
|
quotaFieldSource,
|
|
53493
53733
|
quotaExhaustionFromHeaders,
|
|
53494
53734
|
quotaEnabled,
|