dsh-wtq-pool 1.2.3 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/LICENSE +21 -21
- package/README.en.md +15 -15
- package/README.md +16 -14
- package/lib/client.js +8 -13
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1308 -363
- package/package.json +4 -7
- package/renderer/panel.css +2 -2
- package/renderer/panel.js +28 -27
package/lib/index.js
CHANGED
|
@@ -151,9 +151,9 @@ function writeOpenAIError(res, status, kind, message) {
|
|
|
151
151
|
* the token after it. The scheme rule in `REDACTIONS` covers those, and it runs
|
|
152
152
|
* on text the array scan has already cleared.
|
|
153
153
|
*/
|
|
154
|
-
const BARE_CREDENTIAL_KEYS = "access_?token|refresh_?token|id_?token|auth_?token|oauth_?token|api_?key|secret|token";
|
|
154
|
+
const BARE_CREDENTIAL_KEYS = "access_?token|refresh_?token|id_?token|auth_?token|oauth_?token|api_?key|secret|token|uid|uin|user[-_]?id|cookie|set-cookie";
|
|
155
155
|
/** Where a credential value begins, when that value is an array. */
|
|
156
|
-
const CREDENTIAL_ARRAY_OPEN = new RegExp(`(${`(?:\\\\{0,2}["']?)(?:${`${BARE_CREDENTIAL_KEYS}|authorization|x-ide-token|x-refresh-token|x-api-key|x-cloudide-token|cloud-ide-jwt|cosy-key|cosy-machinetoken|cosy-machineid|cosy-user|x-user-id|x-device-id|x-machine-id|x-uid`})(?:\\\\{0,2}["']?\\s*[:=]\\s*)`})\\\\?\\[`, "gi");
|
|
156
|
+
const CREDENTIAL_ARRAY_OPEN = new RegExp(`(${`(?:\\\\{0,2}["']?)\\b(?:${`${BARE_CREDENTIAL_KEYS}|authorization|x-ide-token|x-refresh-token|x-api-key|x-cloudide-token|cloud-ide-jwt|cosy-key|cosy-machinetoken|cosy-machineid|cosy-user|x-user-id|x-device-id|x-machine-id|x-uid`})(?:\\\\{0,2}["']?\\s*[:=]\\s*)`})\\\\?\\[`, "gi");
|
|
157
157
|
/**
|
|
158
158
|
* Credential shapes in any text that leaves this process.
|
|
159
159
|
*
|
|
@@ -191,7 +191,9 @@ const REDACTIONS = [
|
|
|
191
191
|
[/\beyJ[A-Za-z0-9_+/.=-]+(?:\.[A-Za-z0-9_+/.=-]+){0,2}/g, "[redacted token]"],
|
|
192
192
|
[/\b(qoder-oauth-token-)[A-Za-z0-9_+/.=-]+/g, "$1[redacted]"],
|
|
193
193
|
[/\b(rt_)[A-Za-z0-9_+/.=-]+/g, "$1[redacted]"],
|
|
194
|
-
[
|
|
194
|
+
[/\b((?:set-)?cookie\s*:\s*)[^\r\n]+/gi, "$1[redacted]"],
|
|
195
|
+
[/\b((?:set-)?cookie)\b((?:\\{0,2}["']?\s*[:=]\s*\\{0,2}))("(?:\\.|[^"\\])*"(?=\s*[,}]|$)|[^\r\n]+)/gi, "$1$2[redacted]"],
|
|
196
|
+
[new RegExp(`((?:\\\\{0,2}["']?)\\b(?:${BARE_CREDENTIAL_KEYS})(?:\\\\{0,2}["']?\\s*[:=]\\s*\\\\{0,2}["']?))[A-Za-z0-9_+/.=-]{6,}`, "gi"), "$1[redacted]"],
|
|
195
197
|
[/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/g, "[account]"],
|
|
196
198
|
[/\b[A-Za-z]:[\\/][^\r\n"'<>|]*/g, "[redacted path]"],
|
|
197
199
|
[/\\\\[^\r\n"'<>|]+/g, "[redacted path]"],
|
|
@@ -325,8 +327,10 @@ function redactCredentialArrays(text) {
|
|
|
325
327
|
* Applied at the one place every gateway error passes through, so a new caller
|
|
326
328
|
* cannot introduce a second unredacted path.
|
|
327
329
|
*/
|
|
330
|
+
const REDACT_MAX_CHARS = 16384;
|
|
328
331
|
function redactSecrets(text) {
|
|
329
|
-
|
|
332
|
+
const raw = typeof text === "string" ? text : String(text);
|
|
333
|
+
let out = raw.length > REDACT_MAX_CHARS ? raw.slice(0, REDACT_MAX_CHARS) : raw;
|
|
330
334
|
out = redactCredentialArrays(out);
|
|
331
335
|
for (const [pattern, replacement] of REDACTIONS) out = out.replace(pattern, replacement);
|
|
332
336
|
return out;
|
|
@@ -1385,8 +1389,6 @@ const STREAM_IDLE_TIMEOUT_MS$1 = 3e5;
|
|
|
1385
1389
|
* real lifetime always replaces it in time.
|
|
1386
1390
|
*/
|
|
1387
1391
|
const ASSUMED_TOKEN_LIFETIME_MS = 36e5;
|
|
1388
|
-
/** Longest expiry `Date` can represent; anything past it is not a date. */
|
|
1389
|
-
const MAX_EXPIRY_MS$1 = 41024448e5;
|
|
1390
1392
|
/**
|
|
1391
1393
|
* Absolute expiry for a token the vendor issued, from its `expiresIn`.
|
|
1392
1394
|
*
|
|
@@ -1402,7 +1404,7 @@ function tokenExpiryMs(expiresInSec) {
|
|
|
1402
1404
|
const assumed = () => Date.now() + ASSUMED_TOKEN_LIFETIME_MS;
|
|
1403
1405
|
if (typeof expiresInSec !== "number" || !Number.isFinite(expiresInSec) || expiresInSec <= 0) return assumed();
|
|
1404
1406
|
const at = Date.now() + expiresInSec * 1e3;
|
|
1405
|
-
if (!Number.isFinite(at) || at >
|
|
1407
|
+
if (!Number.isFinite(at) || at > 41024448e5) return assumed();
|
|
1406
1408
|
return at;
|
|
1407
1409
|
}
|
|
1408
1410
|
/**
|
|
@@ -1664,6 +1666,17 @@ function globalBase(credential) {
|
|
|
1664
1666
|
function chatBase(credential) {
|
|
1665
1667
|
return regionOf(credential.domain) === "global" ? globalBase(credential) : CN_CHAT_BASE;
|
|
1666
1668
|
}
|
|
1669
|
+
/**
|
|
1670
|
+
* The host a sign-in's chat traffic actually reaches.
|
|
1671
|
+
*
|
|
1672
|
+
* The sign-in domain is not the chat host: every CN spelling (including an
|
|
1673
|
+
* empty one) is served by one gateway, and the two international brand domains
|
|
1674
|
+
* have gateways of their own. Failover keys transport failures on this value so
|
|
1675
|
+
* that one unreachable gateway condemns exactly the accounts it serves.
|
|
1676
|
+
*/
|
|
1677
|
+
function chatHostOf(credential) {
|
|
1678
|
+
return new URL(chatBase(credential)).host;
|
|
1679
|
+
}
|
|
1667
1680
|
function billingBase(credential) {
|
|
1668
1681
|
return regionOf(credential.domain) === "global" ? globalBase(credential) : CN_BILLING_BASE;
|
|
1669
1682
|
}
|
|
@@ -1850,8 +1863,8 @@ async function readErrorBody(response, limitBytes, signal, timeoutMs = ERROR_BOD
|
|
|
1850
1863
|
}
|
|
1851
1864
|
return text.length > limitBytes ? text.slice(0, limitBytes) : text;
|
|
1852
1865
|
}
|
|
1853
|
-
async function readEnvelope(response) {
|
|
1854
|
-
const text = await response.text();
|
|
1866
|
+
async function readEnvelope(response, signal) {
|
|
1867
|
+
const text = response.ok ? await response.text() : await readErrorBody(response, ERROR_BODY_LIMIT, signal);
|
|
1855
1868
|
if (isGatewayHtmlRejection(response.status, text)) throw Object.assign(new Error(gatewayRejectionText(response.status)), { kind: classifyUpstreamError(response.status, text) });
|
|
1856
1869
|
let parsed;
|
|
1857
1870
|
try {
|
|
@@ -3517,8 +3530,67 @@ const RENAME_RETRY_DELAYS_MS = [
|
|
|
3517
3530
|
200,
|
|
3518
3531
|
400
|
|
3519
3532
|
];
|
|
3533
|
+
/**
|
|
3534
|
+
* The work committed, but the lock file could not be removed.
|
|
3535
|
+
*
|
|
3536
|
+
* Callers that treat an acquisition failure as a reason to roll their own
|
|
3537
|
+
* change back must not do that here: the change is already on disk, and only
|
|
3538
|
+
* the bookkeeping file is left behind.
|
|
3539
|
+
*/
|
|
3540
|
+
var FileLockReleaseError = class extends Error {
|
|
3541
|
+
constructor(message) {
|
|
3542
|
+
super(message);
|
|
3543
|
+
this.name = "FileLockReleaseError";
|
|
3544
|
+
}
|
|
3545
|
+
};
|
|
3520
3546
|
const ABANDONED_RECLAIMS = /* @__PURE__ */ new Map();
|
|
3521
3547
|
/**
|
|
3548
|
+
* Locks this process failed to release, by resolved path.
|
|
3549
|
+
*
|
|
3550
|
+
* A holder that shares only reads blocks the write that would mark the lock
|
|
3551
|
+
* abandoned as well as the unlink. A sidecar records the same fact for other
|
|
3552
|
+
* processes; this map is the fallback when even creating a new file is denied.
|
|
3553
|
+
*/
|
|
3554
|
+
const ABANDONED_LOCKS = /* @__PURE__ */ new Map();
|
|
3555
|
+
function abandonedMarker(file, token) {
|
|
3556
|
+
return `${file}.abandoned.${token}`;
|
|
3557
|
+
}
|
|
3558
|
+
async function markAbandoned(file, token) {
|
|
3559
|
+
ABANDONED_LOCKS.set(resolve(file), token);
|
|
3560
|
+
try {
|
|
3561
|
+
await writeFile(abandonedMarker(file, token), JSON.stringify({
|
|
3562
|
+
token,
|
|
3563
|
+
createdAtMs: Date.now()
|
|
3564
|
+
}), {
|
|
3565
|
+
flag: "wx",
|
|
3566
|
+
mode: 384
|
|
3567
|
+
});
|
|
3568
|
+
} catch {}
|
|
3569
|
+
}
|
|
3570
|
+
async function hasAbandonedMarker(file, token) {
|
|
3571
|
+
try {
|
|
3572
|
+
await stat(abandonedMarker(file, token));
|
|
3573
|
+
return true;
|
|
3574
|
+
} catch {
|
|
3575
|
+
return false;
|
|
3576
|
+
}
|
|
3577
|
+
}
|
|
3578
|
+
async function cleanupAbandonedMarkers(file) {
|
|
3579
|
+
const directory = dirname(file);
|
|
3580
|
+
const prefix = `${basename(file)}.abandoned.`;
|
|
3581
|
+
let names;
|
|
3582
|
+
try {
|
|
3583
|
+
names = await readdir(directory);
|
|
3584
|
+
} catch {
|
|
3585
|
+
return;
|
|
3586
|
+
}
|
|
3587
|
+
for (const name of names) {
|
|
3588
|
+
if (!name.startsWith(prefix)) continue;
|
|
3589
|
+
await unlink(join(directory, name)).catch(() => {});
|
|
3590
|
+
}
|
|
3591
|
+
ABANDONED_LOCKS.delete(resolve(file));
|
|
3592
|
+
}
|
|
3593
|
+
/**
|
|
3522
3594
|
* Commit a complete temporary file over its target.
|
|
3523
3595
|
*
|
|
3524
3596
|
* Windows can report EPERM or EBUSY while another reader is between opening
|
|
@@ -3546,10 +3618,10 @@ function processIsAlive(pid) {
|
|
|
3546
3618
|
return;
|
|
3547
3619
|
}
|
|
3548
3620
|
}
|
|
3549
|
-
function processOutput(command, args, env, cwd) {
|
|
3621
|
+
function processOutput(command, args, env, cwd, timeoutMs) {
|
|
3550
3622
|
return new Promise((resolve) => {
|
|
3551
3623
|
execFile(command, [...args], {
|
|
3552
|
-
timeout:
|
|
3624
|
+
timeout: timeoutMs,
|
|
3553
3625
|
maxBuffer: 1024,
|
|
3554
3626
|
windowsHide: true,
|
|
3555
3627
|
env,
|
|
@@ -3560,7 +3632,7 @@ function processOutput(command, args, env, cwd) {
|
|
|
3560
3632
|
});
|
|
3561
3633
|
});
|
|
3562
3634
|
}
|
|
3563
|
-
async function processStartedAt(pid) {
|
|
3635
|
+
async function processStartedAt(pid, timeoutMs = 2e3) {
|
|
3564
3636
|
if (pid === process.pid) return PROCESS_STARTED_AT_MS;
|
|
3565
3637
|
if (process.platform === "win32") {
|
|
3566
3638
|
const systemRoot = trustedSystemRootOf(process.env);
|
|
@@ -3575,7 +3647,7 @@ async function processStartedAt(pid) {
|
|
|
3575
3647
|
], trustedWindowsChildEnv(process.env, {
|
|
3576
3648
|
inheritTemp: true,
|
|
3577
3649
|
includeSystemDrive: true
|
|
3578
|
-
}), join(systemRoot, "System32")))?.match(/\d{10,}/);
|
|
3650
|
+
}), join(systemRoot, "System32"), timeoutMs))?.match(/\d{10,}/);
|
|
3579
3651
|
return match === void 0 || match === null ? void 0 : Number(match[0]);
|
|
3580
3652
|
}
|
|
3581
3653
|
const ps = ["/usr/bin/ps", "/bin/ps"].find(existsSync);
|
|
@@ -3589,11 +3661,11 @@ async function processStartedAt(pid) {
|
|
|
3589
3661
|
PATH: "/usr/bin:/bin",
|
|
3590
3662
|
LC_ALL: "C",
|
|
3591
3663
|
TZ: "UTC"
|
|
3592
|
-
}, "/");
|
|
3664
|
+
}, "/", timeoutMs);
|
|
3593
3665
|
const started = output === void 0 ? NaN : Date.parse(`${output.trim()} UTC`);
|
|
3594
3666
|
return Number.isFinite(started) ? started : void 0;
|
|
3595
3667
|
}
|
|
3596
|
-
async function lockState(file) {
|
|
3668
|
+
async function lockState(file, probeTimeoutMs = 2e3, probe = processStartedAt) {
|
|
3597
3669
|
let text;
|
|
3598
3670
|
try {
|
|
3599
3671
|
text = await readFile(file, "utf8");
|
|
@@ -3608,13 +3680,17 @@ async function lockState(file) {
|
|
|
3608
3680
|
owner = JSON.parse(text);
|
|
3609
3681
|
} catch {}
|
|
3610
3682
|
if (owner?.state === "abandoned") return "stale";
|
|
3683
|
+
if (typeof owner?.token === "string") {
|
|
3684
|
+
if (ABANDONED_LOCKS.get(resolve(file)) === owner.token) return "stale";
|
|
3685
|
+
if (await hasAbandonedMarker(file, owner.token)) return "stale";
|
|
3686
|
+
}
|
|
3611
3687
|
const fileAge = await stat(file).then((entry) => Date.now() - entry.mtimeMs, () => 0);
|
|
3612
3688
|
const ownerAlive = typeof owner?.pid === "number" ? processIsAlive(owner.pid) : void 0;
|
|
3613
3689
|
if (ownerAlive === false) return "stale";
|
|
3614
3690
|
if (ownerAlive === true && typeof owner?.pid === "number") {
|
|
3615
3691
|
const recordedStart = typeof owner.processStartedAtMs === "number" ? owner.processStartedAtMs : typeof owner.createdAtMs === "number" ? owner.createdAtMs : void 0;
|
|
3616
3692
|
if (recordedStart !== void 0 && Math.abs(fileAge) > PROCESS_START_TOLERANCE_MS) {
|
|
3617
|
-
const actualStart = await
|
|
3693
|
+
const actualStart = await probe(owner.pid, probeTimeoutMs);
|
|
3618
3694
|
if (actualStart !== void 0 && actualStart - recordedStart > PROCESS_START_TOLERANCE_MS) return "stale";
|
|
3619
3695
|
}
|
|
3620
3696
|
if (recordedStart === void 0) return fileAge > UNOWNED_FILE_LOCK_STALE_MS ? "stale" : "current";
|
|
@@ -3636,7 +3712,9 @@ async function createOwnedFile(file) {
|
|
|
3636
3712
|
});
|
|
3637
3713
|
return token;
|
|
3638
3714
|
} catch (error) {
|
|
3639
|
-
|
|
3715
|
+
const code = error.code;
|
|
3716
|
+
if (code === "EEXIST") return void 0;
|
|
3717
|
+
if (process.platform === "win32" && (code === "EPERM" || code === "EACCES")) return void 0;
|
|
3640
3718
|
throw error;
|
|
3641
3719
|
}
|
|
3642
3720
|
}
|
|
@@ -3650,6 +3728,10 @@ async function releaseOwnedFile(file, token) {
|
|
|
3650
3728
|
} catch (error) {
|
|
3651
3729
|
const code = error.code;
|
|
3652
3730
|
if (code === "ENOENT" || code === "ENOTDIR") return "missing";
|
|
3731
|
+
if (code === "EBUSY" || code === "EPERM" || code === "EACCES") {
|
|
3732
|
+
await markAbandoned(file, token);
|
|
3733
|
+
return "failed";
|
|
3734
|
+
}
|
|
3653
3735
|
}
|
|
3654
3736
|
if (owner !== void 0) {
|
|
3655
3737
|
if (owner.token !== token) return "replaced";
|
|
@@ -3667,7 +3749,10 @@ async function releaseOwnedFile(file, token) {
|
|
|
3667
3749
|
} catch {}
|
|
3668
3750
|
}
|
|
3669
3751
|
}
|
|
3670
|
-
if (Date.now() >= deadline)
|
|
3752
|
+
if (Date.now() >= deadline) {
|
|
3753
|
+
await markAbandoned(file, token);
|
|
3754
|
+
return "failed";
|
|
3755
|
+
}
|
|
3671
3756
|
await new Promise((resolve) => setTimeout(resolve, RETRY_MS));
|
|
3672
3757
|
}
|
|
3673
3758
|
}
|
|
@@ -3684,6 +3769,14 @@ async function newestReclaimGeneration(lock) {
|
|
|
3684
3769
|
}
|
|
3685
3770
|
return generation;
|
|
3686
3771
|
}
|
|
3772
|
+
/**
|
|
3773
|
+
* Drop reclaim generations that are no longer fencing anything.
|
|
3774
|
+
*
|
|
3775
|
+
* Answers false when a terminal generation could not be removed. Creating the
|
|
3776
|
+
* next generation then would leave both files behind, and a filesystem that
|
|
3777
|
+
* keeps refusing the delete would grow one more on every retry. The caller
|
|
3778
|
+
* waits instead; the cleanup above succeeds once the deletion does.
|
|
3779
|
+
*/
|
|
3687
3780
|
async function cleanupTerminalReclaims(lock) {
|
|
3688
3781
|
const directory = dirname(lock);
|
|
3689
3782
|
const prefix = `${basename(lock)}.reclaim.`;
|
|
@@ -3691,13 +3784,14 @@ async function cleanupTerminalReclaims(lock) {
|
|
|
3691
3784
|
try {
|
|
3692
3785
|
names = await readdir(directory);
|
|
3693
3786
|
} catch {
|
|
3694
|
-
return;
|
|
3787
|
+
return true;
|
|
3695
3788
|
}
|
|
3696
3789
|
const claimNames = names.filter((name) => {
|
|
3697
3790
|
if (!name.startsWith(prefix)) return false;
|
|
3698
3791
|
return /^\d+$/.test(name.slice(prefix.length));
|
|
3699
3792
|
});
|
|
3700
3793
|
const claimNameSet = new Set(claimNames);
|
|
3794
|
+
let blocked = false;
|
|
3701
3795
|
for (const claimName of claimNames) {
|
|
3702
3796
|
const claim = join(directory, claimName);
|
|
3703
3797
|
const sidecars = names.filter((name) => name.startsWith(`${claimName}.abandoned.`));
|
|
@@ -3708,7 +3802,7 @@ async function cleanupTerminalReclaims(lock) {
|
|
|
3708
3802
|
owner = typeof parsed === "object" && parsed !== null ? parsed : {};
|
|
3709
3803
|
} catch {}
|
|
3710
3804
|
const terminalState = owner?.state === "complete" || owner?.state === "abandoned";
|
|
3711
|
-
const rememberedAbandonment = ABANDONED_RECLAIMS.get(claim) === owner
|
|
3805
|
+
const rememberedAbandonment = owner?.token !== void 0 && ABANDONED_RECLAIMS.get(claim) === owner.token;
|
|
3712
3806
|
if (sidecars.length === 0 && !terminalState && !rememberedAbandonment) continue;
|
|
3713
3807
|
let claimRemoved = false;
|
|
3714
3808
|
try {
|
|
@@ -3718,7 +3812,10 @@ async function cleanupTerminalReclaims(lock) {
|
|
|
3718
3812
|
const code = error.code;
|
|
3719
3813
|
claimRemoved = code === "ENOENT" || code === "ENOTDIR";
|
|
3720
3814
|
}
|
|
3721
|
-
if (!claimRemoved)
|
|
3815
|
+
if (!claimRemoved) {
|
|
3816
|
+
blocked = true;
|
|
3817
|
+
continue;
|
|
3818
|
+
}
|
|
3722
3819
|
for (const sidecar of sidecars) await unlink(join(directory, sidecar)).catch(() => {});
|
|
3723
3820
|
for (const temporary of temporaryFiles) await unlink(join(directory, temporary)).catch(() => {});
|
|
3724
3821
|
ABANDONED_RECLAIMS.delete(claim);
|
|
@@ -3730,8 +3827,9 @@ async function cleanupTerminalReclaims(lock) {
|
|
|
3730
3827
|
if (claimNameSet.has(name.slice(0, abandoned))) continue;
|
|
3731
3828
|
await unlink(join(directory, name)).catch(() => {});
|
|
3732
3829
|
}
|
|
3830
|
+
return !blocked;
|
|
3733
3831
|
}
|
|
3734
|
-
async function claimStaleReclaim(lock) {
|
|
3832
|
+
async function claimStaleReclaim(lock, probeTimeoutMs = 2e3, probe = processStartedAt) {
|
|
3735
3833
|
const directory = dirname(lock);
|
|
3736
3834
|
const prefix = `${basename(lock)}.reclaim.`;
|
|
3737
3835
|
const names = await readdir(directory);
|
|
@@ -3746,7 +3844,7 @@ async function claimStaleReclaim(lock) {
|
|
|
3746
3844
|
}).map((name) => join(directory, name));
|
|
3747
3845
|
for (const claim of claims) {
|
|
3748
3846
|
const claimName = basename(claim);
|
|
3749
|
-
if (names.some((name) => name.startsWith(`${claimName}.abandoned.`)))
|
|
3847
|
+
if (names.some((name) => name.startsWith(`${claimName}.abandoned.`))) return void 0;
|
|
3750
3848
|
let owner = {};
|
|
3751
3849
|
try {
|
|
3752
3850
|
const parsed = JSON.parse(await readFile(claim, "utf8"));
|
|
@@ -3757,9 +3855,20 @@ async function claimStaleReclaim(lock) {
|
|
|
3757
3855
|
continue;
|
|
3758
3856
|
}
|
|
3759
3857
|
}
|
|
3760
|
-
if (owner.state === "complete" || owner.state === "abandoned")
|
|
3761
|
-
if (ABANDONED_RECLAIMS.get(claim) === owner.token)
|
|
3762
|
-
if (await lockState(claim) === "current") return void 0;
|
|
3858
|
+
if (owner.state === "complete" || owner.state === "abandoned") return void 0;
|
|
3859
|
+
if (owner.token !== void 0 && ABANDONED_RECLAIMS.get(claim) === owner.token) return void 0;
|
|
3860
|
+
if (await lockState(claim, probeTimeoutMs, probe) === "current") return void 0;
|
|
3861
|
+
try {
|
|
3862
|
+
await unlink(claim);
|
|
3863
|
+
} catch (error) {
|
|
3864
|
+
const code = error.code;
|
|
3865
|
+
if (code !== "ENOENT" && code !== "ENOTDIR") return void 0;
|
|
3866
|
+
}
|
|
3867
|
+
for (const name of names) {
|
|
3868
|
+
if (!name.startsWith(`${claimName}.`) || !name.endsWith(".tmp")) continue;
|
|
3869
|
+
await unlink(join(directory, name)).catch(() => {});
|
|
3870
|
+
}
|
|
3871
|
+
ABANDONED_RECLAIMS.delete(claim);
|
|
3763
3872
|
}
|
|
3764
3873
|
const file = `${lock}.reclaim.${String(generation + 1).padStart(12, "0")}`;
|
|
3765
3874
|
const token = await createOwnedFile(file);
|
|
@@ -3829,42 +3938,55 @@ async function abandonReclaim(reclaim, cause) {
|
|
|
3829
3938
|
}
|
|
3830
3939
|
/** Run `work` while holding a lock file shared by every writer to the same document. */
|
|
3831
3940
|
async function withFileLock(lock, work) {
|
|
3832
|
-
await mkdir(dirname(lock), {
|
|
3941
|
+
await mkdir(dirname(lock), {
|
|
3942
|
+
recursive: true,
|
|
3943
|
+
mode: 448
|
|
3944
|
+
});
|
|
3833
3945
|
const deadline = Date.now() + FILE_LOCK_WAIT_MS;
|
|
3946
|
+
const probes = /* @__PURE__ */ new Map();
|
|
3947
|
+
const probe = (pid, timeoutMs) => {
|
|
3948
|
+
let pending = probes.get(pid);
|
|
3949
|
+
if (pending === void 0) {
|
|
3950
|
+
pending = processStartedAt(pid, timeoutMs);
|
|
3951
|
+
probes.set(pid, pending);
|
|
3952
|
+
}
|
|
3953
|
+
return pending;
|
|
3954
|
+
};
|
|
3834
3955
|
let token;
|
|
3835
3956
|
while (token === void 0) {
|
|
3836
3957
|
token = await createOwnedFile(lock);
|
|
3837
3958
|
if (token !== void 0) break;
|
|
3838
|
-
|
|
3839
|
-
|
|
3840
|
-
|
|
3841
|
-
|
|
3842
|
-
|
|
3959
|
+
if (await lockState(lock, Math.max(250, deadline - Date.now()), probe) === "stale") {
|
|
3960
|
+
const reclaim = await cleanupTerminalReclaims(lock) ? await claimStaleReclaim(lock, Math.max(250, deadline - Date.now()), probe) : void 0;
|
|
3961
|
+
if (reclaim !== void 0) try {
|
|
3962
|
+
if (await newestReclaimGeneration(lock) === reclaim.generation && await lockState(lock, Math.max(250, deadline - Date.now()), probe) === "stale" && await newestReclaimGeneration(lock) === reclaim.generation) await unlink(lock).catch(() => {});
|
|
3963
|
+
} finally {
|
|
3843
3964
|
try {
|
|
3844
|
-
|
|
3845
|
-
}
|
|
3846
|
-
|
|
3847
|
-
await completeReclaim(reclaim);
|
|
3848
|
-
} catch (error) {
|
|
3849
|
-
await abandonReclaim(reclaim, error);
|
|
3850
|
-
}
|
|
3965
|
+
await completeReclaim(reclaim);
|
|
3966
|
+
} catch (error) {
|
|
3967
|
+
await abandonReclaim(reclaim, error);
|
|
3851
3968
|
}
|
|
3852
|
-
continue;
|
|
3853
3969
|
}
|
|
3854
3970
|
}
|
|
3855
3971
|
if (Date.now() >= deadline) throw new Error("timed out waiting for the credential document lock");
|
|
3856
3972
|
await new Promise((resolve) => setTimeout(resolve, RETRY_MS));
|
|
3857
3973
|
}
|
|
3974
|
+
await cleanupAbandonedMarkers(lock);
|
|
3858
3975
|
await cleanupTerminalReclaims(lock);
|
|
3859
3976
|
try {
|
|
3860
3977
|
await work();
|
|
3861
3978
|
} catch (error) {
|
|
3862
3979
|
const released = await releaseOwnedFile(lock, token);
|
|
3863
|
-
if (released !== "released" && released !== "missing")
|
|
3980
|
+
if (released !== "released" && released !== "missing") {
|
|
3981
|
+
const wrapped = new Error("work failed and the credential document lock was not released", { cause: error });
|
|
3982
|
+
const code = error.code;
|
|
3983
|
+
if (code !== void 0) wrapped.code = code;
|
|
3984
|
+
throw wrapped;
|
|
3985
|
+
}
|
|
3864
3986
|
throw error;
|
|
3865
3987
|
}
|
|
3866
3988
|
const released = await releaseOwnedFile(lock, token);
|
|
3867
|
-
if (released !== "released" && released !== "missing") throw new
|
|
3989
|
+
if (released !== "released" && released !== "missing") throw new FileLockReleaseError("work completed but the credential document lock was not released");
|
|
3868
3990
|
}
|
|
3869
3991
|
//#endregion
|
|
3870
3992
|
//#region src/accounts.ts
|
|
@@ -3962,7 +4084,6 @@ function defaultDesktopAuthDirs(platform = process.platform, home = homedir(), e
|
|
|
3962
4084
|
* 8.64e15 `new Date(…).toISOString()` also throws, which failed the whole
|
|
3963
4085
|
* panel document rather than the one row.
|
|
3964
4086
|
*/
|
|
3965
|
-
const MAX_EXPIRY_MS = 41024448e5;
|
|
3966
4087
|
/**
|
|
3967
4088
|
* Normalize an expiry that may arrive in seconds or milliseconds.
|
|
3968
4089
|
*
|
|
@@ -3973,7 +4094,7 @@ const MAX_EXPIRY_MS = 41024448e5;
|
|
|
3973
4094
|
function expiryToMs(value) {
|
|
3974
4095
|
if (!Number.isFinite(value) || value <= 0) return 0;
|
|
3975
4096
|
const ms = value > 0xe8d4a51000 ? value : value * 1e3;
|
|
3976
|
-
return ms >
|
|
4097
|
+
return ms > 41024448e5 ? 0 : ms;
|
|
3977
4098
|
}
|
|
3978
4099
|
function optionalString$1(value) {
|
|
3979
4100
|
return typeof value === "string" && value !== "" ? value : void 0;
|
|
@@ -4192,20 +4313,31 @@ async function readDeletedSignInsFile(file) {
|
|
|
4192
4313
|
const entries = parsed?.entries;
|
|
4193
4314
|
if (entries === null || typeof entries !== "object" || Array.isArray(entries)) return tombstones;
|
|
4194
4315
|
for (const [id, value] of Object.entries(entries)) {
|
|
4195
|
-
const
|
|
4196
|
-
if (
|
|
4197
|
-
const digest = entry.tokenDigest;
|
|
4198
|
-
if (typeof digest !== "string" || digest === "") continue;
|
|
4199
|
-
const issued = entry.lastRefreshAtMs;
|
|
4200
|
-
const deletedAt = entry.deletedAtMs;
|
|
4201
|
-
tombstones.set(id, {
|
|
4202
|
-
tokenDigest: digest,
|
|
4203
|
-
lastRefreshAtMs: typeof issued === "number" && Number.isFinite(issued) ? issued : 0,
|
|
4204
|
-
deletedAtMs: typeof deletedAt === "number" && Number.isFinite(deletedAt) ? deletedAt : 0
|
|
4205
|
-
});
|
|
4316
|
+
const record = deletedSignInFrom(value);
|
|
4317
|
+
if (record !== void 0) tombstones.set(id, record);
|
|
4206
4318
|
}
|
|
4207
4319
|
return tombstones;
|
|
4208
4320
|
}
|
|
4321
|
+
function deletedSignInFrom(value) {
|
|
4322
|
+
const entry = value;
|
|
4323
|
+
if (entry === null || typeof entry !== "object") return void 0;
|
|
4324
|
+
const digest = entry.tokenDigest;
|
|
4325
|
+
if (typeof digest !== "string" || digest === "") return void 0;
|
|
4326
|
+
const issued = entry.lastRefreshAtMs;
|
|
4327
|
+
const deletedAt = entry.deletedAtMs;
|
|
4328
|
+
const nonce = entry.nonce;
|
|
4329
|
+
const alternates = Array.isArray(entry.alternates) ? entry.alternates.flatMap((alternate) => {
|
|
4330
|
+
const record = deletedSignInFrom(alternate);
|
|
4331
|
+
return record === void 0 ? [] : [record];
|
|
4332
|
+
}) : [];
|
|
4333
|
+
return {
|
|
4334
|
+
tokenDigest: digest,
|
|
4335
|
+
lastRefreshAtMs: typeof issued === "number" && Number.isFinite(issued) ? issued : 0,
|
|
4336
|
+
deletedAtMs: typeof deletedAt === "number" && Number.isFinite(deletedAt) ? deletedAt : 0,
|
|
4337
|
+
...typeof nonce === "string" && nonce !== "" ? { nonce } : {},
|
|
4338
|
+
...alternates.length > 0 ? { alternates } : {}
|
|
4339
|
+
};
|
|
4340
|
+
}
|
|
4209
4341
|
/**
|
|
4210
4342
|
* Whether `credential` is a sign-in the user made after deleting the account.
|
|
4211
4343
|
*
|
|
@@ -4230,6 +4362,98 @@ function endsTombstone(tombstone, credential) {
|
|
|
4230
4362
|
return isLiveAuthFile(credential.sourcePath);
|
|
4231
4363
|
}
|
|
4232
4364
|
/**
|
|
4365
|
+
* The tombstone that names a credential, under either of its pool keys.
|
|
4366
|
+
*
|
|
4367
|
+
* A key written by 1.2.3 for an identity-less sign-in is not the key this build
|
|
4368
|
+
* computes, so looking only at the current one let the client's snapshot bring
|
|
4369
|
+
* the account straight back after an upgrade. The record is returned with the
|
|
4370
|
+
* key it was stored under and whether that key is the current one: only a
|
|
4371
|
+
* current-key record may be released, and only by removing the entry that
|
|
4372
|
+
* actually holds it.
|
|
4373
|
+
*/
|
|
4374
|
+
function tombstoneOf(tombstones, credential, currentId = workbuddyAccountId(credential)) {
|
|
4375
|
+
const record = tombstones.get(currentId);
|
|
4376
|
+
if (record !== void 0) return {
|
|
4377
|
+
key: currentId,
|
|
4378
|
+
record,
|
|
4379
|
+
current: true
|
|
4380
|
+
};
|
|
4381
|
+
for (const previous of previousAccountIds(credential)) {
|
|
4382
|
+
const old = tombstones.get(previous);
|
|
4383
|
+
if (old !== void 0) return {
|
|
4384
|
+
key: previous,
|
|
4385
|
+
record: old,
|
|
4386
|
+
current: false
|
|
4387
|
+
};
|
|
4388
|
+
}
|
|
4389
|
+
}
|
|
4390
|
+
/**
|
|
4391
|
+
* Which of two deletes names the newer sign-in.
|
|
4392
|
+
*
|
|
4393
|
+
* A delete from a stale roster can arrive after another pool already deleted a
|
|
4394
|
+
* newer sign-in for the same account. The record naming the newer sign-in is
|
|
4395
|
+
* the one that still has to hold, whichever delete committed last: an older
|
|
4396
|
+
* record would let the newer sign-in back in as a re-login. Records that name
|
|
4397
|
+
* the same (or an undated) sign-in fall back to the later delete.
|
|
4398
|
+
*/
|
|
4399
|
+
function namesNewerSignIn(candidate, current) {
|
|
4400
|
+
if (candidate.lastRefreshAtMs > 0 && current.lastRefreshAtMs === 0) return true;
|
|
4401
|
+
if (candidate.lastRefreshAtMs === 0 && current.lastRefreshAtMs > 0) return false;
|
|
4402
|
+
if (candidate.lastRefreshAtMs > 0 && current.lastRefreshAtMs > 0 && candidate.lastRefreshAtMs !== current.lastRefreshAtMs) return candidate.lastRefreshAtMs > current.lastRefreshAtMs;
|
|
4403
|
+
return candidate.deletedAtMs > current.deletedAtMs;
|
|
4404
|
+
}
|
|
4405
|
+
function recordsOf(entry) {
|
|
4406
|
+
if (entry.alternates === void 0) return [entry];
|
|
4407
|
+
return [entry, ...entry.alternates.flatMap((record) => recordsOf(record))];
|
|
4408
|
+
}
|
|
4409
|
+
function withoutAlternates(record) {
|
|
4410
|
+
const { alternates: _ignored, ...bare } = record;
|
|
4411
|
+
return bare;
|
|
4412
|
+
}
|
|
4413
|
+
function sameDeletedSignIn(left, right) {
|
|
4414
|
+
if (left.nonce !== void 0 || right.nonce !== void 0) return left.nonce === right.nonce;
|
|
4415
|
+
return left.tokenDigest === right.tokenDigest && left.lastRefreshAtMs === right.lastRefreshAtMs && left.deletedAtMs === right.deletedAtMs;
|
|
4416
|
+
}
|
|
4417
|
+
function deletedSignInFromRecords(records) {
|
|
4418
|
+
if (records.length === 0) return void 0;
|
|
4419
|
+
const [primary, ...alternates] = records;
|
|
4420
|
+
const bare = withoutAlternates(primary);
|
|
4421
|
+
return alternates.length === 0 ? bare : {
|
|
4422
|
+
...bare,
|
|
4423
|
+
alternates: alternates.map(withoutAlternates)
|
|
4424
|
+
};
|
|
4425
|
+
}
|
|
4426
|
+
function mergeDeletedSignIns(existing, candidate) {
|
|
4427
|
+
if (existing === void 0) return withoutAlternates(candidate);
|
|
4428
|
+
const records = [...recordsOf(existing)];
|
|
4429
|
+
const bare = withoutAlternates(candidate);
|
|
4430
|
+
if (!records.some((record) => sameDeletedSignIn(record, bare))) records.push(bare);
|
|
4431
|
+
let primary = 0;
|
|
4432
|
+
for (let index = 1; index < records.length; index += 1) if (namesNewerSignIn(records[index], records[primary])) primary = index;
|
|
4433
|
+
const [chosen] = records.splice(primary, 1);
|
|
4434
|
+
return deletedSignInFromRecords([chosen, ...records]);
|
|
4435
|
+
}
|
|
4436
|
+
function dropDeletedSignIns(entry, targets) {
|
|
4437
|
+
const kept = recordsOf(entry).filter((record) => !targets.some((target) => sameDeletedSignIn(record, target)));
|
|
4438
|
+
if (kept.length === recordsOf(entry).length) return entry;
|
|
4439
|
+
return deletedSignInFromRecords(kept);
|
|
4440
|
+
}
|
|
4441
|
+
/**
|
|
4442
|
+
* Whether a tombstone still keeps this credential out of the pool.
|
|
4443
|
+
*
|
|
4444
|
+
* A record under the current key is judged by the re-login rule. A key written
|
|
4445
|
+
* by 1.2.3 cannot tell two same-nickname sign-ins apart, so it may only keep
|
|
4446
|
+
* the sign-in it was written for out: the token digest is the one fact that
|
|
4447
|
+
* belongs to the deleted session alone. That record is never released here,
|
|
4448
|
+
* because releasing it on another account's token would resurrect the deleted
|
|
4449
|
+
* row on the next scan.
|
|
4450
|
+
*/
|
|
4451
|
+
function tombstoneBlocks(entry, credential) {
|
|
4452
|
+
if (entry.current) return recordsOf(entry.record).some((record) => !endsTombstone(record, credential));
|
|
4453
|
+
const digest = tokenDigest(credential.accessToken);
|
|
4454
|
+
return recordsOf(entry.record).some((record) => digest === record.tokenDigest);
|
|
4455
|
+
}
|
|
4456
|
+
/**
|
|
4233
4457
|
* Whether `sourcePath` sits inside the directory the plugin writes into.
|
|
4234
4458
|
*
|
|
4235
4459
|
* A walk that escapes the root is not inside it, and `..` is how a path gets
|
|
@@ -4241,14 +4465,98 @@ function ownSignInPath(sourcePath, ownDir) {
|
|
|
4241
4465
|
return walked !== "" && !walked.startsWith("..") && !isAbsolute(walked);
|
|
4242
4466
|
}
|
|
4243
4467
|
function workbuddyAccountId(credential) {
|
|
4468
|
+
const stable = credential.uin ?? credential.uid ?? `${credential.nickname ?? ""}\u0000${credential.sourcePath}`;
|
|
4469
|
+
return createHash("sha256").update(`workbuddy\0${stable}`).digest("hex").slice(0, 16);
|
|
4470
|
+
}
|
|
4471
|
+
/**
|
|
4472
|
+
* The pool key a 1.2.3 build gave a credential with no vendor id.
|
|
4473
|
+
*
|
|
4474
|
+
* That build hashed the nickname alone, or the path when there was none. This
|
|
4475
|
+
* build hashes both so two sign-ins sharing one nickname stay two accounts,
|
|
4476
|
+
* which means an upgraded install still carries the old key in its settings and
|
|
4477
|
+
* its deleted-sign-in file. The pool answers to either key: a switch the user
|
|
4478
|
+
* turned off does not come back on, and a delete does not resurrect its row.
|
|
4479
|
+
*/
|
|
4480
|
+
function legacyWorkBuddyAccountId(credential) {
|
|
4244
4481
|
const stable = credential.uin ?? credential.uid ?? credential.nickname ?? `path:${credential.sourcePath}`;
|
|
4245
4482
|
return createHash("sha256").update(`workbuddy\0${stable}`).digest("hex").slice(0, 16);
|
|
4246
4483
|
}
|
|
4484
|
+
/**
|
|
4485
|
+
* The 1.2.3 key for a credential, when it differs from the current one.
|
|
4486
|
+
*
|
|
4487
|
+
* A vendor id hashes the same in both builds, so only a sign-in with no
|
|
4488
|
+
* identity has any older key to read. The current key is not repeated.
|
|
4489
|
+
*/
|
|
4490
|
+
function previousAccountIds(credential) {
|
|
4491
|
+
if ((credential.uin ?? credential.uid) !== void 0) return [];
|
|
4492
|
+
const previous = legacyWorkBuddyAccountId(credential);
|
|
4493
|
+
return previous === workbuddyAccountId(credential) ? [] : [previous];
|
|
4494
|
+
}
|
|
4247
4495
|
/** Human label; the nickname alone, so the pool reads as names rather than keys. */
|
|
4248
4496
|
function accountLabel(credential) {
|
|
4249
4497
|
return credential.nickname ?? "WorkBuddy";
|
|
4250
4498
|
}
|
|
4251
4499
|
/**
|
|
4500
|
+
* Collapse identity-less copies of one session.
|
|
4501
|
+
*
|
|
4502
|
+
* A sign-in with no uin/uid is keyed by its file, and an earlier build adopted
|
|
4503
|
+
* the desktop's superseded snapshot under a second path. Both copies carry one
|
|
4504
|
+
* session, so renewing either spends the other's refresh token. Copies sharing
|
|
4505
|
+
* a token are folded, and once the plugin owns an identity-less copy, the
|
|
4506
|
+
* desktop snapshot with the same domain and nickname stays folded even after a
|
|
4507
|
+
* renewal rotates the token away. That name-based half is migration-only: a
|
|
4508
|
+
* newer build does not adopt identity-less snapshots, and `foldSessionCopies`
|
|
4509
|
+
* only applies it where an own copy exists. The live file wins because the app
|
|
4510
|
+
* renews it, then the plugin's own copy, then the freshest snapshot.
|
|
4511
|
+
*/
|
|
4512
|
+
function foldSessionCopies(credentials, ownDir) {
|
|
4513
|
+
const isOwn = (credential) => ownSignInPath(credential.sourcePath, ownDir);
|
|
4514
|
+
const identityLess = (credential) => (credential.uin ?? credential.uid ?? "") === "";
|
|
4515
|
+
const tokenKey = (credential) => {
|
|
4516
|
+
if (!identityLess(credential)) return "";
|
|
4517
|
+
const token = credential.refreshToken || credential.accessToken;
|
|
4518
|
+
return token === "" ? "" : credential.domain + "\0" + token;
|
|
4519
|
+
};
|
|
4520
|
+
const nameKey = (credential) => {
|
|
4521
|
+
if (!identityLess(credential) || (credential.nickname ?? "") === "") return "";
|
|
4522
|
+
return credential.domain + "\0" + credential.nickname;
|
|
4523
|
+
};
|
|
4524
|
+
const copiedTokens = /* @__PURE__ */ new Set();
|
|
4525
|
+
for (const credential of credentials) {
|
|
4526
|
+
const key = tokenKey(credential);
|
|
4527
|
+
if (key !== "" && isOwn(credential)) copiedTokens.add(key);
|
|
4528
|
+
}
|
|
4529
|
+
const rankOf = (credential) => isLiveAuthFile(credential.sourcePath) ? 3 : ownSignInPath(credential.sourcePath, ownDir) ? 2 : 1;
|
|
4530
|
+
const best = /* @__PURE__ */ new Map();
|
|
4531
|
+
for (const credential of credentials) {
|
|
4532
|
+
const key = tokenKey(credential);
|
|
4533
|
+
if (key === "" || !copiedTokens.has(key)) continue;
|
|
4534
|
+
const current = best.get(key);
|
|
4535
|
+
if (current === void 0 || rankOf(credential) > rankOf(current) || rankOf(credential) === rankOf(current) && isFresher(credential, current, ownDir)) best.set(key, credential);
|
|
4536
|
+
}
|
|
4537
|
+
const ownedNames = /* @__PURE__ */ new Set();
|
|
4538
|
+
for (const credential of credentials) {
|
|
4539
|
+
const key = nameKey(credential);
|
|
4540
|
+
if (key !== "" && isOwn(credential)) ownedNames.add(key);
|
|
4541
|
+
}
|
|
4542
|
+
const bestOwned = /* @__PURE__ */ new Map();
|
|
4543
|
+
for (const credential of credentials) {
|
|
4544
|
+
const key = nameKey(credential);
|
|
4545
|
+
if (key === "" || !ownedNames.has(key) || !isOwn(credential)) continue;
|
|
4546
|
+
const current = bestOwned.get(key);
|
|
4547
|
+
if (current === void 0 || isFresher(credential, current, ownDir)) bestOwned.set(key, credential);
|
|
4548
|
+
}
|
|
4549
|
+
return credentials.filter((credential) => {
|
|
4550
|
+
const key = tokenKey(credential);
|
|
4551
|
+
if (key !== "" && copiedTokens.has(key) && best.get(key) !== credential) return false;
|
|
4552
|
+
const name = nameKey(credential);
|
|
4553
|
+
if (name === "" || !ownedNames.has(name)) return true;
|
|
4554
|
+
if (isLiveAuthFile(credential.sourcePath)) return true;
|
|
4555
|
+
if (isOwn(credential)) return bestOwned.get(name) === credential;
|
|
4556
|
+
return false;
|
|
4557
|
+
});
|
|
4558
|
+
}
|
|
4559
|
+
/**
|
|
4252
4560
|
* Whether a credential can no longer serve a request at all.
|
|
4253
4561
|
*
|
|
4254
4562
|
* Only a token that has already died AND that carries nothing to renew it with.
|
|
@@ -4529,8 +4837,12 @@ function ownSignInFile(directory, signIn) {
|
|
|
4529
4837
|
}
|
|
4530
4838
|
async function writeOwnSignInDocument(file, signIn, signal) {
|
|
4531
4839
|
signal?.throwIfAborted();
|
|
4532
|
-
await mkdir(dirname(file), {
|
|
4840
|
+
await mkdir(dirname(file), {
|
|
4841
|
+
recursive: true,
|
|
4842
|
+
mode: 448
|
|
4843
|
+
});
|
|
4533
4844
|
signal?.throwIfAborted();
|
|
4845
|
+
await removeAbandonedTemps(file);
|
|
4534
4846
|
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
4535
4847
|
try {
|
|
4536
4848
|
await writeFile(temporary, JSON.stringify({
|
|
@@ -4565,6 +4877,7 @@ async function writeOwnSignInDocument(file, signIn, signal) {
|
|
|
4565
4877
|
*/
|
|
4566
4878
|
async function renewOwnSignInFile(file, credential, previous, createIfMissing = false, beforeCommit) {
|
|
4567
4879
|
return await withSignInWriteLock(file, async () => {
|
|
4880
|
+
await removeAbandonedTemps(file);
|
|
4568
4881
|
let text;
|
|
4569
4882
|
try {
|
|
4570
4883
|
text = await readFile(file, "utf8");
|
|
@@ -4572,6 +4885,10 @@ async function renewOwnSignInFile(file, credential, previous, createIfMissing =
|
|
|
4572
4885
|
if (!createIfMissing || error.code !== "ENOENT") throw error;
|
|
4573
4886
|
if (beforeCommit !== void 0 && !await beforeCommit()) return false;
|
|
4574
4887
|
await writeOwnSignInDocument(file, ownSignInOf(credential));
|
|
4888
|
+
if (beforeCommit !== void 0 && !await beforeCommit()) {
|
|
4889
|
+
await unlink(file).catch(() => {});
|
|
4890
|
+
return false;
|
|
4891
|
+
}
|
|
4575
4892
|
return true;
|
|
4576
4893
|
}
|
|
4577
4894
|
const document = JSON.parse(text);
|
|
@@ -4698,7 +5015,9 @@ async function readArchivedCredentials(file) {
|
|
|
4698
5015
|
}
|
|
4699
5016
|
return stored;
|
|
4700
5017
|
}
|
|
4701
|
-
|
|
5018
|
+
/** How old a leftover write temp must be before another writer removes it. */
|
|
5019
|
+
const ABANDONED_TEMP_MAX_AGE_MS = 6e5;
|
|
5020
|
+
async function removeAbandonedTemps(file, minAgeMs = ABANDONED_TEMP_MAX_AGE_MS) {
|
|
4702
5021
|
const prefix = `${basename(file)}.`;
|
|
4703
5022
|
let names;
|
|
4704
5023
|
try {
|
|
@@ -4710,7 +5029,35 @@ async function removeAbandonedArchiveTemps(file) {
|
|
|
4710
5029
|
if (!name.startsWith(prefix) || !name.endsWith(".tmp")) continue;
|
|
4711
5030
|
const id = name.slice(prefix.length, -4);
|
|
4712
5031
|
if (!/^[a-f\d]{8}-(?:[a-f\d]{4}-){3}[a-f\d]{12}$/iu.test(id)) continue;
|
|
4713
|
-
|
|
5032
|
+
const candidate = join(dirname(file), name);
|
|
5033
|
+
if (minAgeMs > 0) {
|
|
5034
|
+
const info = await stat(candidate).catch(() => void 0);
|
|
5035
|
+
if (info === void 0 || Date.now() - info.mtimeMs < minAgeMs) continue;
|
|
5036
|
+
}
|
|
5037
|
+
await unlink(candidate).catch(() => {});
|
|
5038
|
+
}
|
|
5039
|
+
}
|
|
5040
|
+
/**
|
|
5041
|
+
* Remove sign-in temps a crashed write left in the plugin's own directory.
|
|
5042
|
+
*
|
|
5043
|
+
* A private file is written as `<name>.info.<uuid>.tmp` and renamed into place.
|
|
5044
|
+
* A process killed between the two leaves a full token in that temp, and no
|
|
5045
|
+
* later write of the same account necessarily happens. The age floor keeps a
|
|
5046
|
+
* scan from removing a write another process is still preparing.
|
|
5047
|
+
*/
|
|
5048
|
+
async function removeAbandonedSignInTemps(directory) {
|
|
5049
|
+
let names;
|
|
5050
|
+
try {
|
|
5051
|
+
names = await readdir(directory);
|
|
5052
|
+
} catch {
|
|
5053
|
+
return;
|
|
5054
|
+
}
|
|
5055
|
+
for (const name of names) {
|
|
5056
|
+
if (!/^workbuddy-panel-.*\.info\.[a-f\d]{8}-(?:[a-f\d]{4}-){3}[a-f\d]{12}\.tmp$/iu.test(name)) continue;
|
|
5057
|
+
const file = join(directory, name);
|
|
5058
|
+
const info = await stat(file).catch(() => void 0);
|
|
5059
|
+
if (info === void 0 || Date.now() - info.mtimeMs < ABANDONED_TEMP_MAX_AGE_MS) continue;
|
|
5060
|
+
await unlink(file).catch(() => {});
|
|
4714
5061
|
}
|
|
4715
5062
|
}
|
|
4716
5063
|
/**
|
|
@@ -4722,8 +5069,12 @@ async function removeAbandonedArchiveTemps(file) {
|
|
|
4722
5069
|
*/
|
|
4723
5070
|
async function writeArchivedCredentials(file, entries, signal) {
|
|
4724
5071
|
signal?.throwIfAborted();
|
|
4725
|
-
await mkdir(dirname(file), {
|
|
5072
|
+
await mkdir(dirname(file), {
|
|
5073
|
+
recursive: true,
|
|
5074
|
+
mode: 448
|
|
5075
|
+
});
|
|
4726
5076
|
signal?.throwIfAborted();
|
|
5077
|
+
await removeAbandonedTemps(file);
|
|
4727
5078
|
const body = JSON.stringify({
|
|
4728
5079
|
version: 1,
|
|
4729
5080
|
entries: [...entries].map(([id, credential]) => ({
|
|
@@ -4773,14 +5124,33 @@ var WorkBuddyAccountPool = class {
|
|
|
4773
5124
|
* from settings and only filters at pick time.
|
|
4774
5125
|
*/
|
|
4775
5126
|
disabledIds = /* @__PURE__ */ new Set();
|
|
4776
|
-
/**
|
|
4777
|
-
|
|
5127
|
+
/**
|
|
5128
|
+
* Last account handed out per region, for round-robin.
|
|
5129
|
+
*
|
|
5130
|
+
* Both region shims share one pool, and filtering to the caller's region
|
|
5131
|
+
* before indexing used to make each region renormalise the same cursor
|
|
5132
|
+
* against its own length: with two accounts per region, alternating turns
|
|
5133
|
+
* served the same account in each region and never reached the others.
|
|
5134
|
+
* A stored id also survives the pool shrinking between picks: an account
|
|
5135
|
+
* whose refresh fails is excluded from the retry, and a numeric position
|
|
5136
|
+
* would then point at an account that already had its turn.
|
|
5137
|
+
*/
|
|
5138
|
+
lastServed = /* @__PURE__ */ new Map();
|
|
4778
5139
|
/** Sticky mode: conversation key → account id. Content stays out of memory. */
|
|
4779
5140
|
sticky = /* @__PURE__ */ new Map();
|
|
5141
|
+
/**
|
|
5142
|
+
* Account id → the order in which it last served. A readout only: the composer
|
|
5143
|
+
* shows the account a sticky conversation is on, and that account is the one
|
|
5144
|
+
* that just answered. A counter rather than a clock, because two acquires can
|
|
5145
|
+
* land in the same millisecond. Not persisted; after a restart the rotation
|
|
5146
|
+
* starts over.
|
|
5147
|
+
*/
|
|
5148
|
+
servedOrder = /* @__PURE__ */ new Map();
|
|
5149
|
+
servedCount = 0;
|
|
4780
5150
|
lastScanAtMs = 0;
|
|
4781
5151
|
preferredIds = /* @__PURE__ */ new Map();
|
|
4782
5152
|
/**
|
|
4783
|
-
* Account ids the user removed from the pool
|
|
5153
|
+
* Account ids the user removed from the pool.
|
|
4784
5154
|
*
|
|
4785
5155
|
* Enforced while scan() assembles its result, so a removed account cannot
|
|
4786
5156
|
* re-enter the pool when the desktop app rewrites its sign-in file. That is
|
|
@@ -4850,6 +5220,14 @@ var WorkBuddyAccountPool = class {
|
|
|
4850
5220
|
*/
|
|
4851
5221
|
heldIds = /* @__PURE__ */ new Map();
|
|
4852
5222
|
/**
|
|
5223
|
+
* Account id → when an upstream 5xx cooldown ends.
|
|
5224
|
+
*
|
|
5225
|
+
* A 5xx is not a frequency wall, and reporting it as one on the next request
|
|
5226
|
+
* sent the user looking for a limit that never existed. Kept beside the
|
|
5227
|
+
* cooldown so `ineligible` can name the cause while the account is out.
|
|
5228
|
+
*/
|
|
5229
|
+
serverCooldowns = /* @__PURE__ */ new Map();
|
|
5230
|
+
/**
|
|
4853
5231
|
* Accounts the desktop client is signed into right now.
|
|
4854
5232
|
*
|
|
4855
5233
|
* The pool renews one of these into its own directory when the app's session
|
|
@@ -4945,15 +5323,61 @@ var WorkBuddyAccountPool = class {
|
|
|
4945
5323
|
this.logger?.info?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} has credits again; releasing the hold`);
|
|
4946
5324
|
}
|
|
4947
5325
|
/** True when a known balance has reached the floor set for this account. */
|
|
4948
|
-
reserveHolds(
|
|
4949
|
-
const floor = this.creditReserves
|
|
5326
|
+
reserveHolds(account) {
|
|
5327
|
+
const floor = this.storedValue(this.creditReserves, account);
|
|
4950
5328
|
if (floor === void 0 || floor <= 0) return false;
|
|
4951
|
-
const balance = this.creditBalances.get(
|
|
5329
|
+
const balance = this.creditBalances.get(account.id);
|
|
4952
5330
|
return balance !== void 0 && balance <= floor;
|
|
4953
5331
|
}
|
|
4954
5332
|
/** Whether this account was switched off on the panel. */
|
|
4955
5333
|
isDisabled(accountId) {
|
|
4956
|
-
|
|
5334
|
+
const account = this.accounts.find((item) => item.id === accountId);
|
|
5335
|
+
return account === void 0 ? this.disabledIds.has(accountId) : this.storedIdMatches(this.disabledIds, account);
|
|
5336
|
+
}
|
|
5337
|
+
/** The credit floor in force for one account. */
|
|
5338
|
+
creditReserve(accountId) {
|
|
5339
|
+
const account = this.accounts.find((item) => item.id === accountId);
|
|
5340
|
+
return account === void 0 ? this.creditReserves.get(accountId) : this.storedValue(this.creditReserves, account);
|
|
5341
|
+
}
|
|
5342
|
+
/**
|
|
5343
|
+
* Every key this account may still be recorded under in persisted settings.
|
|
5344
|
+
*
|
|
5345
|
+
* The panel writes by current id, so a toggle has to clear the older keys
|
|
5346
|
+
* too; otherwise an old entry stays behind and takes effect again the moment
|
|
5347
|
+
* the current one is removed. An old key shared with another row is not a key
|
|
5348
|
+
* for this account: settings already ignore it, and purging its archive entry
|
|
5349
|
+
* on delete would take the other row's copy with it.
|
|
5350
|
+
*/
|
|
5351
|
+
storedIds(accountId) {
|
|
5352
|
+
const account = this.accounts.find((item) => item.id === accountId);
|
|
5353
|
+
if (account === void 0) return [accountId];
|
|
5354
|
+
return [accountId, ...previousAccountIds(account.credential).filter((id) => this.legacyKeyUnique(account, id))];
|
|
5355
|
+
}
|
|
5356
|
+
/** Whether a stored id set names this account under any of its keys. */
|
|
5357
|
+
storedIdMatches(stored, account) {
|
|
5358
|
+
if (stored.has(account.id)) return true;
|
|
5359
|
+
return previousAccountIds(account.credential).some((id) => stored.has(id) && this.legacyKeyUnique(account, id));
|
|
5360
|
+
}
|
|
5361
|
+
/** A value stored under any of this account's keys; current wins. */
|
|
5362
|
+
storedValue(stored, account) {
|
|
5363
|
+
const current = stored.get(account.id);
|
|
5364
|
+
if (current !== void 0) return current;
|
|
5365
|
+
for (const id of previousAccountIds(account.credential)) {
|
|
5366
|
+
if (!this.legacyKeyUnique(account, id)) continue;
|
|
5367
|
+
const value = stored.get(id);
|
|
5368
|
+
if (value !== void 0) return value;
|
|
5369
|
+
}
|
|
5370
|
+
}
|
|
5371
|
+
/**
|
|
5372
|
+
* Whether a 1.2.3 key names this row and no other one.
|
|
5373
|
+
*
|
|
5374
|
+
* That key hashed a nickname alone, so two identity-less sign-ins can share
|
|
5375
|
+
* it. Applying it to both would switch off or remove an account the user
|
|
5376
|
+
* never touched; an ambiguous key is therefore ignored rather than guessed.
|
|
5377
|
+
*/
|
|
5378
|
+
legacyKeyUnique(account, key) {
|
|
5379
|
+
const claimants = this.accounts.filter((item) => item.id === key || previousAccountIds(item.credential).includes(key));
|
|
5380
|
+
return claimants.length === 1 && claimants[0]?.id === account.id;
|
|
4957
5381
|
}
|
|
4958
5382
|
/**
|
|
4959
5383
|
* Replace the permanent removal list.
|
|
@@ -5003,24 +5427,36 @@ var WorkBuddyAccountPool = class {
|
|
|
5003
5427
|
const deletingCredential = account.credential;
|
|
5004
5428
|
const sameCredential = (credential) => credential.accessToken === deletingCredential.accessToken && credential.refreshToken === deletingCredential.refreshToken;
|
|
5005
5429
|
const running = this.scanInflight;
|
|
5006
|
-
const
|
|
5007
|
-
if (
|
|
5008
|
-
const
|
|
5009
|
-
|
|
5430
|
+
const recorded = await this.rememberDeletedSignIn(account.id, account.credential);
|
|
5431
|
+
if (recorded === void 0) throw new Error("could not record the deleted sign-in");
|
|
5432
|
+
const rollbackTombstone = async () => {
|
|
5433
|
+
const drop = async () => await this.updateDeletedSignIns((tombstones) => {
|
|
5434
|
+
const current = tombstones.get(account.id);
|
|
5435
|
+
if (current === void 0) return true;
|
|
5436
|
+
const kept = dropDeletedSignIns(current, [recorded.written]);
|
|
5437
|
+
if (kept === void 0) tombstones.delete(account.id);
|
|
5438
|
+
else if (kept !== current) tombstones.set(account.id, kept);
|
|
5439
|
+
return true;
|
|
5440
|
+
}) === true;
|
|
5441
|
+
return await drop() || await drop();
|
|
5442
|
+
};
|
|
5443
|
+
const fail = async () => {
|
|
5444
|
+
if (!await rollbackTombstone()) this.logger?.error?.(`${LOG_PREFIX}: could not remove the tombstone of a failed delete for ${accountLogTag(account.label)}; the account stays hidden until the deleted sign-ins can be written again`);
|
|
5445
|
+
};
|
|
5010
5446
|
const settled = running === void 0 ? true : await raceWithTimeout(running.catch(() => {}), DELETE_SCAN_WAIT_MS);
|
|
5011
5447
|
const current = this.accounts.find((item) => item.id === account.id);
|
|
5012
5448
|
let replaced = current !== void 0 && (!this.ownsSignIn(account.id) || resolve(current.credential.sourcePath) !== resolve(ownedPath) || !sameCredential(current.credential));
|
|
5013
5449
|
if (current === void 0) {
|
|
5014
5450
|
const tombstone = (await this.loadDeletedSignIns())?.get(account.id);
|
|
5015
|
-
replaced = tombstone === void 0 ||
|
|
5451
|
+
replaced = tombstone === void 0 || !tombstoneBlocks({
|
|
5452
|
+
record: tombstone,
|
|
5453
|
+
current: true
|
|
5454
|
+
}, deletingCredential);
|
|
5016
5455
|
}
|
|
5017
5456
|
const onDisk = await readCredential(ownedPath);
|
|
5018
5457
|
if (!replaced) replaced = onDisk.kind === "sealed" || onDisk.kind === "unreadable" || onDisk.kind === "credential" && (workbuddyAccountId(onDisk.credential) !== account.id || !sameCredential(onDisk.credential));
|
|
5019
5458
|
if (replaced) {
|
|
5020
|
-
|
|
5021
|
-
knownTombstones.delete(account.id);
|
|
5022
|
-
await this.writeDeletedSignIns([account.id]);
|
|
5023
|
-
}
|
|
5459
|
+
await fail();
|
|
5024
5460
|
return false;
|
|
5025
5461
|
}
|
|
5026
5462
|
try {
|
|
@@ -5030,11 +5466,9 @@ var WorkBuddyAccountPool = class {
|
|
|
5030
5466
|
await unlink(ownedPath);
|
|
5031
5467
|
});
|
|
5032
5468
|
} catch (error) {
|
|
5033
|
-
|
|
5034
|
-
|
|
5035
|
-
|
|
5036
|
-
await this.writeDeletedSignIns([account.id]);
|
|
5037
|
-
}
|
|
5469
|
+
const code = error.code;
|
|
5470
|
+
if (!(error instanceof FileLockReleaseError) && code !== "ENOENT") {
|
|
5471
|
+
await fail();
|
|
5038
5472
|
this.rosterRevision += 1;
|
|
5039
5473
|
await this.rescan().catch((rescanError) => {
|
|
5040
5474
|
this.logger?.warn?.(`${LOG_PREFIX}: could not restore the roster after a failed delete: ${String(rescanError)}`);
|
|
@@ -5042,9 +5476,11 @@ var WorkBuddyAccountPool = class {
|
|
|
5042
5476
|
throw error;
|
|
5043
5477
|
}
|
|
5044
5478
|
}
|
|
5479
|
+
await removeAbandonedTemps(ownedPath, 0);
|
|
5045
5480
|
this.rosterRevision += 1;
|
|
5046
|
-
|
|
5047
|
-
|
|
5481
|
+
const archivedIds = this.storedIds(account.id);
|
|
5482
|
+
if (!settled) running.catch(() => {}).then(() => this.forgetArchived(archivedIds));
|
|
5483
|
+
await this.forgetArchived(archivedIds);
|
|
5048
5484
|
const remaining = this.accounts.find((item) => item.id === account.id);
|
|
5049
5485
|
if (remaining === void 0 || this.isOwnSignInPath(remaining.credential.sourcePath) && resolve(remaining.credential.sourcePath) === resolve(ownedPath)) this.forgetAccount(account.id);
|
|
5050
5486
|
return true;
|
|
@@ -5058,27 +5494,27 @@ var WorkBuddyAccountPool = class {
|
|
|
5058
5494
|
this.heldIds.delete(accountId);
|
|
5059
5495
|
this.signInTokens.delete(accountId);
|
|
5060
5496
|
this.refusedSignIns.delete(accountId);
|
|
5497
|
+
this.serverCooldowns.delete(accountId);
|
|
5061
5498
|
}
|
|
5062
5499
|
/**
|
|
5063
5500
|
* Record one deleted sign-in, so no later scan puts the account back.
|
|
5064
5501
|
*
|
|
5065
5502
|
* The file is written before the delete is reported: a tombstone that only
|
|
5066
5503
|
* lived in memory would be gone after a restart, and the snapshot it has to
|
|
5067
|
-
* hold off is still sitting in the client's directory.
|
|
5504
|
+
* hold off is still sitting in the client's directory. Answers undefined when
|
|
5505
|
+
* the record could not be written, so the delete can refuse to report success.
|
|
5068
5506
|
*/
|
|
5069
5507
|
async rememberDeletedSignIn(id, credential) {
|
|
5070
|
-
const
|
|
5071
|
-
if (tombstones === void 0) return false;
|
|
5072
|
-
const previous = tombstones.get(id);
|
|
5073
|
-
tombstones.set(id, {
|
|
5508
|
+
const written = {
|
|
5074
5509
|
tokenDigest: tokenDigest(credential.accessToken),
|
|
5075
5510
|
lastRefreshAtMs: credential.lastRefreshAtMs ?? 0,
|
|
5076
|
-
deletedAtMs: Date.now()
|
|
5511
|
+
deletedAtMs: Date.now(),
|
|
5512
|
+
nonce: randomUUID()
|
|
5513
|
+
};
|
|
5514
|
+
return await this.updateDeletedSignIns((tombstones) => {
|
|
5515
|
+
tombstones.set(id, mergeDeletedSignIns(tombstones.get(id), written));
|
|
5516
|
+
return { written };
|
|
5077
5517
|
});
|
|
5078
|
-
if (await this.writeDeletedSignIns()) return true;
|
|
5079
|
-
if (previous === void 0) tombstones.delete(id);
|
|
5080
|
-
else tombstones.set(id, previous);
|
|
5081
|
-
return false;
|
|
5082
5518
|
}
|
|
5083
5519
|
/**
|
|
5084
5520
|
* Read the tombstones, re-reading when the file changed underneath us.
|
|
@@ -5113,26 +5549,28 @@ var WorkBuddyAccountPool = class {
|
|
|
5113
5549
|
return this.deletedSignIns;
|
|
5114
5550
|
}
|
|
5115
5551
|
this.logger?.warn?.(`${LOG_PREFIX}: could not read the deleted sign-ins: ${String(error)}`);
|
|
5116
|
-
return
|
|
5552
|
+
return;
|
|
5117
5553
|
}
|
|
5118
5554
|
}
|
|
5119
5555
|
/**
|
|
5120
|
-
*
|
|
5121
|
-
*
|
|
5122
|
-
* What is on disk is merged in first, except for the ids this call is removing:
|
|
5123
|
-
* a second pool over the same data directory writes this same file, and writing
|
|
5124
|
-
* our map verbatim dropped a delete it had already recorded. The removals are the
|
|
5125
|
-
* other half of that: a re-login ends a tombstone and a failed delete rolls one
|
|
5126
|
-
* back, and a pure union put both straight back.
|
|
5556
|
+
* Read-modify-write the tombstone document under its file lock.
|
|
5127
5557
|
*
|
|
5128
|
-
*
|
|
5129
|
-
*
|
|
5558
|
+
* The change starts from what is on disk, never from the in-memory copy: a
|
|
5559
|
+
* second pool may have released a record this process still holds, and writing
|
|
5560
|
+
* the cached map back put that release in force again. `mutate` gets the current
|
|
5561
|
+
* records and answers whatever the caller needs; undefined means the document
|
|
5562
|
+
* could not be read or written, and nothing was persisted.
|
|
5130
5563
|
*/
|
|
5131
|
-
async
|
|
5132
|
-
const tombstones = this.deletedSignIns;
|
|
5564
|
+
async updateDeletedSignIns(mutate, signal) {
|
|
5133
5565
|
const file = this.deletedSignInsFile();
|
|
5134
|
-
if (
|
|
5135
|
-
|
|
5566
|
+
if (file === void 0) {
|
|
5567
|
+
const tombstones = this.deletedSignIns ?? /* @__PURE__ */ new Map();
|
|
5568
|
+
const answer = mutate(tombstones);
|
|
5569
|
+
this.deletedSignIns = tombstones;
|
|
5570
|
+
this.deletedSignInsStamp = MISSING_FILE_STAMP;
|
|
5571
|
+
return answer;
|
|
5572
|
+
}
|
|
5573
|
+
let answer;
|
|
5136
5574
|
let written = false;
|
|
5137
5575
|
try {
|
|
5138
5576
|
await withFileLock(`${file}.lock`, async () => {
|
|
@@ -5142,31 +5580,36 @@ var WorkBuddyAccountPool = class {
|
|
|
5142
5580
|
onDisk = await readDeletedSignInsFile(file);
|
|
5143
5581
|
} catch (error) {
|
|
5144
5582
|
if (error.code !== "ENOENT") {
|
|
5145
|
-
if (!signal?.aborted) this.logger?.warn?.(`${LOG_PREFIX}: could not
|
|
5583
|
+
if (!signal?.aborted) this.logger?.warn?.(`${LOG_PREFIX}: could not read the deleted sign-ins to update them: ${String(error)}`);
|
|
5146
5584
|
return;
|
|
5147
5585
|
}
|
|
5148
5586
|
onDisk = /* @__PURE__ */ new Map();
|
|
5149
5587
|
}
|
|
5150
5588
|
if (signal?.aborted) return;
|
|
5151
|
-
|
|
5152
|
-
|
|
5153
|
-
const ours = tombstones.get(id);
|
|
5154
|
-
if (ours === void 0 || entry.deletedAtMs > ours.deletedAtMs) tombstones.set(id, entry);
|
|
5155
|
-
}
|
|
5589
|
+
const result = mutate(onDisk);
|
|
5590
|
+
if (signal?.aborted) return;
|
|
5156
5591
|
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
5157
5592
|
try {
|
|
5158
5593
|
signal?.throwIfAborted();
|
|
5159
|
-
await mkdir(dirname(file), {
|
|
5594
|
+
await mkdir(dirname(file), {
|
|
5595
|
+
recursive: true,
|
|
5596
|
+
mode: 448
|
|
5597
|
+
});
|
|
5160
5598
|
signal?.throwIfAborted();
|
|
5599
|
+
await removeAbandonedTemps(file);
|
|
5161
5600
|
await writeFile(temporary, JSON.stringify({
|
|
5162
5601
|
version: 1,
|
|
5163
|
-
entries: Object.fromEntries(
|
|
5602
|
+
entries: Object.fromEntries(onDisk)
|
|
5164
5603
|
}, null, 2), { mode: 384 });
|
|
5165
5604
|
signal?.throwIfAborted();
|
|
5166
5605
|
await replaceFile(temporary, file);
|
|
5167
5606
|
const stamp = await signInFileStamp(file);
|
|
5168
|
-
|
|
5607
|
+
answer = result;
|
|
5169
5608
|
written = true;
|
|
5609
|
+
if (!signal?.aborted) {
|
|
5610
|
+
this.deletedSignIns = onDisk;
|
|
5611
|
+
this.deletedSignInsStamp = stamp;
|
|
5612
|
+
}
|
|
5170
5613
|
} catch (error) {
|
|
5171
5614
|
await unlink(temporary).catch(() => {});
|
|
5172
5615
|
if (!signal?.aborted) this.logger?.warn?.(`${LOG_PREFIX}: could not record the deleted sign-ins: ${String(error)}`);
|
|
@@ -5175,7 +5618,7 @@ var WorkBuddyAccountPool = class {
|
|
|
5175
5618
|
} catch (error) {
|
|
5176
5619
|
if (!signal?.aborted) this.logger?.warn?.(written ? `${LOG_PREFIX}: the deleted sign-ins were saved but their lock was not released: ${String(error)}` : `${LOG_PREFIX}: could not lock the deleted sign-ins: ${String(error)}`);
|
|
5177
5620
|
}
|
|
5178
|
-
return
|
|
5621
|
+
return answer;
|
|
5179
5622
|
}
|
|
5180
5623
|
deletedSignInsFile() {
|
|
5181
5624
|
const directory = this.dataPath?.();
|
|
@@ -5225,6 +5668,8 @@ var WorkBuddyAccountPool = class {
|
|
|
5225
5668
|
async adoptOrphanedSignIns(found, tornDown, sealedLiveIds, unknownLiveDirs, signal) {
|
|
5226
5669
|
const directory = ownSignInDir(this.dataPath?.() ?? "");
|
|
5227
5670
|
if (directory === "") return [];
|
|
5671
|
+
const latest = await this.loadDeletedSignIns(signal);
|
|
5672
|
+
if (latest === void 0) return [];
|
|
5228
5673
|
const liveIds = new Set(sealedLiveIds);
|
|
5229
5674
|
const ownedIds = /* @__PURE__ */ new Set();
|
|
5230
5675
|
for (const credential of found) {
|
|
@@ -5239,7 +5684,9 @@ var WorkBuddyAccountPool = class {
|
|
|
5239
5684
|
if (ownSignInPath(credential.sourcePath, directory)) continue;
|
|
5240
5685
|
const id = workbuddyAccountId(credential);
|
|
5241
5686
|
if (unknownLiveDirs.has(this.homeOf(credential)) || liveIds.has(id) || ownedIds.has(id) || tornDown.has(id)) continue;
|
|
5242
|
-
|
|
5687
|
+
const entry = tombstoneOf(latest, credential, id);
|
|
5688
|
+
if (entry !== void 0 && tombstoneBlocks(entry, credential)) continue;
|
|
5689
|
+
if ((credential.uin ?? credential.uid ?? "") === "") continue;
|
|
5243
5690
|
let file;
|
|
5244
5691
|
try {
|
|
5245
5692
|
file = await writeOwnSignIn(directory, {
|
|
@@ -5251,11 +5698,18 @@ var WorkBuddyAccountPool = class {
|
|
|
5251
5698
|
this.logger?.warn?.(`${LOG_PREFIX}: could not take over ${accountLogTag(accountLabel(credential))}: ${String(error)}`);
|
|
5252
5699
|
continue;
|
|
5253
5700
|
}
|
|
5254
|
-
|
|
5255
|
-
adopted.push({
|
|
5701
|
+
const adoptedCredential = {
|
|
5256
5702
|
...credential,
|
|
5257
5703
|
sourcePath: file
|
|
5258
|
-
}
|
|
5704
|
+
};
|
|
5705
|
+
const after = await this.loadDeletedSignIns(signal);
|
|
5706
|
+
const afterEntry = after === void 0 ? void 0 : tombstoneOf(after, adoptedCredential, id);
|
|
5707
|
+
if (after === void 0 || afterEntry !== void 0 && tombstoneBlocks(afterEntry, adoptedCredential)) {
|
|
5708
|
+
await unlink(file).catch(() => {});
|
|
5709
|
+
continue;
|
|
5710
|
+
}
|
|
5711
|
+
ownedIds.add(id);
|
|
5712
|
+
adopted.push(adoptedCredential);
|
|
5259
5713
|
}
|
|
5260
5714
|
return adopted;
|
|
5261
5715
|
}
|
|
@@ -5267,10 +5721,9 @@ var WorkBuddyAccountPool = class {
|
|
|
5267
5721
|
* directory is left alone - the desktop app owns its files.
|
|
5268
5722
|
*/
|
|
5269
5723
|
async renewOwnSignIn(previous, credential) {
|
|
5270
|
-
const
|
|
5271
|
-
const guard = async () => await this.deleteDidNotTake(credential, sinceMs);
|
|
5724
|
+
const guard = async () => await this.deleteDidNotTake(credential);
|
|
5272
5725
|
if (this.isOwnSignInPath(previous.sourcePath)) return await renewOwnSignInFile(previous.sourcePath, credential, previous, false, guard);
|
|
5273
|
-
if ((credential.uin ?? credential.uid ??
|
|
5726
|
+
if ((credential.uin ?? credential.uid ?? "") === "") return true;
|
|
5274
5727
|
const directory = ownSignInDir(this.dataPath?.() ?? "");
|
|
5275
5728
|
if (directory === "") return true;
|
|
5276
5729
|
const file = ownSignInFile(directory, ownSignInOf(credential));
|
|
@@ -5280,33 +5733,43 @@ var WorkBuddyAccountPool = class {
|
|
|
5280
5733
|
return written;
|
|
5281
5734
|
}
|
|
5282
5735
|
/**
|
|
5283
|
-
* Whether a delete
|
|
5736
|
+
* Whether a delete still stands against this sign-in.
|
|
5284
5737
|
*
|
|
5285
5738
|
* A delete records a tombstone, unlinks the file, and reports success. A
|
|
5286
|
-
* renewal
|
|
5287
|
-
*
|
|
5288
|
-
*
|
|
5289
|
-
*
|
|
5739
|
+
* renewal over the same sign-in used to race that: its rename landed after
|
|
5740
|
+
* the unlink, the file came back with a token issued after the tombstone, and
|
|
5741
|
+
* `endsTombstone` read the recreation as a re-login and cleared the record -
|
|
5742
|
+
* a deleted account came back.
|
|
5290
5743
|
*
|
|
5291
|
-
* The
|
|
5292
|
-
*
|
|
5293
|
-
*
|
|
5294
|
-
*
|
|
5295
|
-
*
|
|
5296
|
-
*
|
|
5744
|
+
* The token's issue time cannot order the two acts. A rotation that started
|
|
5745
|
+
* after the delete, from an account still held in memory, also mints a token
|
|
5746
|
+
* newer than the record. What is known instead is which sign-in a record
|
|
5747
|
+
* names: a current-key record names this exact account, and only a scan
|
|
5748
|
+
* releases it for a sign-in the user made after the delete - before that
|
|
5749
|
+
* account can be refreshed again. So the presence of the record is enough.
|
|
5750
|
+
* A 1.2.3 record cannot tell two sign-ins apart and stays digest-matched.
|
|
5297
5751
|
*
|
|
5298
5752
|
* The set is read under the lock a delete writes it with, so a delete that
|
|
5299
5753
|
* committed a moment ago is visible. Nothing blocking runs under that lock.
|
|
5300
5754
|
*/
|
|
5301
|
-
async deleteDidNotTake(credential
|
|
5755
|
+
async deleteDidNotTake(credential) {
|
|
5302
5756
|
const file = this.deletedSignInsFile();
|
|
5303
5757
|
if (file === void 0) return true;
|
|
5304
5758
|
if (!await stat(file).then(() => true, (error) => error.code !== "ENOENT")) return true;
|
|
5305
5759
|
let alive = true;
|
|
5306
|
-
|
|
5307
|
-
|
|
5308
|
-
|
|
5309
|
-
|
|
5760
|
+
try {
|
|
5761
|
+
await withFileLock(`${file}.lock`, async () => {
|
|
5762
|
+
const tombstones = await this.loadDeletedSignIns();
|
|
5763
|
+
if (tombstones === void 0) {
|
|
5764
|
+
alive = false;
|
|
5765
|
+
return;
|
|
5766
|
+
}
|
|
5767
|
+
const entry = tombstoneOf(tombstones, credential);
|
|
5768
|
+
if (entry !== void 0 && (entry.current || tombstoneBlocks(entry, credential))) alive = false;
|
|
5769
|
+
});
|
|
5770
|
+
} catch (error) {
|
|
5771
|
+
if (!(error instanceof FileLockReleaseError)) throw error;
|
|
5772
|
+
}
|
|
5310
5773
|
return alive;
|
|
5311
5774
|
}
|
|
5312
5775
|
/**
|
|
@@ -5316,13 +5779,15 @@ var WorkBuddyAccountPool = class {
|
|
|
5316
5779
|
* of one document, and a delete that raced a scan's write would otherwise be
|
|
5317
5780
|
* overwritten by it.
|
|
5318
5781
|
*/
|
|
5319
|
-
async forgetArchived(
|
|
5782
|
+
async forgetArchived(ids) {
|
|
5320
5783
|
const file = this.archiveFile();
|
|
5321
5784
|
if (file === void 0) return;
|
|
5322
5785
|
const run = this.archiveQueue.then(async () => {
|
|
5323
5786
|
await withFileLock(`${file}.lock`, async () => {
|
|
5324
5787
|
const stored = await readArchivedCredentials(file);
|
|
5325
|
-
|
|
5788
|
+
let dropped = false;
|
|
5789
|
+
for (const id of ids) dropped = stored.delete(id) || dropped;
|
|
5790
|
+
if (!dropped) return;
|
|
5326
5791
|
await writeArchivedCredentials(file, stored);
|
|
5327
5792
|
});
|
|
5328
5793
|
});
|
|
@@ -5344,6 +5809,8 @@ var WorkBuddyAccountPool = class {
|
|
|
5344
5809
|
this.unknownLiveSignIn = false;
|
|
5345
5810
|
this.unknownLiveFiles = /* @__PURE__ */ new Set();
|
|
5346
5811
|
this.refreshBlockedIds = /* @__PURE__ */ new Set();
|
|
5812
|
+
this.serverCooldowns.clear();
|
|
5813
|
+
this.refreshInflight.clear();
|
|
5347
5814
|
}
|
|
5348
5815
|
/** Read after any scan already running, so a manual request gets a fresh snapshot. */
|
|
5349
5816
|
async rescan() {
|
|
@@ -5391,27 +5858,40 @@ var WorkBuddyAccountPool = class {
|
|
|
5391
5858
|
const file = this.archiveFile();
|
|
5392
5859
|
if (file === void 0 || credentials.length === 0) return;
|
|
5393
5860
|
const ownDir = ownSignInDir(this.dataPath?.() ?? "");
|
|
5861
|
+
let committed = false;
|
|
5862
|
+
let wrote = false;
|
|
5394
5863
|
const run = this.archiveQueue.then(async () => {
|
|
5395
5864
|
await withFileLock(`${file}.lock`, async () => {
|
|
5396
5865
|
if (signal?.aborted) return;
|
|
5397
5866
|
const stored = await readArchivedCredentials(file);
|
|
5398
5867
|
if (signal?.aborted) return;
|
|
5868
|
+
const tombstones = await this.loadDeletedSignIns(signal);
|
|
5869
|
+
if (tombstones === void 0) return;
|
|
5399
5870
|
let changed = false;
|
|
5400
5871
|
for (const credential of credentials) {
|
|
5401
5872
|
const id = workbuddyAccountId(credential);
|
|
5873
|
+
const entry = tombstoneOf(tombstones, credential, id);
|
|
5874
|
+
if (entry !== void 0 && (entry.current || tombstoneBlocks(entry, credential))) continue;
|
|
5402
5875
|
const previous = stored.get(id);
|
|
5403
5876
|
if (previous !== void 0 && !isFresher(credential, previous, ownDir)) continue;
|
|
5404
5877
|
stored.set(id, credential);
|
|
5405
5878
|
changed = true;
|
|
5406
5879
|
}
|
|
5407
|
-
if (changed)
|
|
5880
|
+
if (changed) {
|
|
5881
|
+
await writeArchivedCredentials(file, stored, signal);
|
|
5882
|
+
wrote = true;
|
|
5883
|
+
}
|
|
5884
|
+
committed = true;
|
|
5408
5885
|
});
|
|
5409
5886
|
});
|
|
5410
5887
|
this.archiveQueue = run.catch(() => {});
|
|
5411
5888
|
try {
|
|
5412
5889
|
await run;
|
|
5413
5890
|
} catch (error) {
|
|
5414
|
-
if (!signal?.aborted)
|
|
5891
|
+
if (!signal?.aborted) {
|
|
5892
|
+
const message = !committed ? "could not update the credential archive" : wrote ? "the credential archive was saved but its lock was not released" : "the credential archive is current but its lock was not released";
|
|
5893
|
+
this.logger?.warn?.(`${LOG_PREFIX}: ${message}: ${String(error)}`);
|
|
5894
|
+
}
|
|
5415
5895
|
}
|
|
5416
5896
|
}
|
|
5417
5897
|
/** Credentials the archive kept for a directory this walk can read. */
|
|
@@ -5421,7 +5901,7 @@ var WorkBuddyAccountPool = class {
|
|
|
5421
5901
|
try {
|
|
5422
5902
|
let stored = /* @__PURE__ */ new Map();
|
|
5423
5903
|
await withFileLock(`${file}.lock`, async () => {
|
|
5424
|
-
await
|
|
5904
|
+
await removeAbandonedTemps(file);
|
|
5425
5905
|
stored = await readArchivedCredentials(file);
|
|
5426
5906
|
});
|
|
5427
5907
|
return [...stored.values()].filter((credential) => homes.has(this.homeOf(credential)));
|
|
@@ -5447,8 +5927,10 @@ var WorkBuddyAccountPool = class {
|
|
|
5447
5927
|
const sealedFileIds = /* @__PURE__ */ new Map();
|
|
5448
5928
|
const unreadableFiles = /* @__PURE__ */ new Set();
|
|
5449
5929
|
const unavailableDirs = /* @__PURE__ */ new Set();
|
|
5930
|
+
const ownDir = ownSignInDir(this.dataPath?.() ?? "");
|
|
5450
5931
|
for (const dir of dirs) {
|
|
5451
5932
|
if (signal.aborted) return this.accounts;
|
|
5933
|
+
if (ownDir !== "" && resolve(dir) === resolve(ownDir)) await removeAbandonedSignInTemps(dir);
|
|
5452
5934
|
const read = await authFilesIn(dir);
|
|
5453
5935
|
if (signal.aborted) return this.accounts;
|
|
5454
5936
|
if (read.files === void 0) {
|
|
@@ -5478,28 +5960,52 @@ var WorkBuddyAccountPool = class {
|
|
|
5478
5960
|
return this.accounts;
|
|
5479
5961
|
}
|
|
5480
5962
|
const tornDown = /* @__PURE__ */ new Set();
|
|
5481
|
-
const
|
|
5963
|
+
const released = /* @__PURE__ */ new Map();
|
|
5482
5964
|
const held = [];
|
|
5483
5965
|
for (const credential of found) {
|
|
5484
5966
|
const id = workbuddyAccountId(credential);
|
|
5485
|
-
const
|
|
5486
|
-
if (
|
|
5967
|
+
const entry = tombstoneOf(tombstones, credential, id);
|
|
5968
|
+
if (entry === void 0) {
|
|
5487
5969
|
held.push(credential);
|
|
5488
5970
|
continue;
|
|
5489
5971
|
}
|
|
5490
|
-
if (
|
|
5972
|
+
if (tombstoneBlocks(entry, credential)) {
|
|
5491
5973
|
tornDown.add(id);
|
|
5492
5974
|
continue;
|
|
5493
5975
|
}
|
|
5494
|
-
|
|
5495
|
-
releasedIds.push(id);
|
|
5976
|
+
if (entry.current) released.set(entry.key, entry.record);
|
|
5496
5977
|
held.push(credential);
|
|
5497
5978
|
}
|
|
5498
5979
|
found.length = 0;
|
|
5499
5980
|
found.push(...held);
|
|
5500
|
-
for (const id of tombstones.keys()) tornDown.add(id);
|
|
5501
|
-
if (
|
|
5981
|
+
for (const id of tombstones.keys()) if (!released.has(id)) tornDown.add(id);
|
|
5982
|
+
if (released.size > 0) await this.updateDeletedSignIns((entries) => {
|
|
5983
|
+
for (const [id, record] of released) {
|
|
5984
|
+
const current = entries.get(id);
|
|
5985
|
+
if (current === void 0) continue;
|
|
5986
|
+
const kept = dropDeletedSignIns(current, recordsOf(record));
|
|
5987
|
+
if (kept === void 0) entries.delete(id);
|
|
5988
|
+
else if (kept !== current) entries.set(id, kept);
|
|
5989
|
+
}
|
|
5990
|
+
}, signal);
|
|
5991
|
+
if (signal.aborted || rosterRevision !== this.rosterRevision) return this.accounts;
|
|
5992
|
+
const latest = await this.loadDeletedSignIns(signal);
|
|
5502
5993
|
if (signal.aborted || rosterRevision !== this.rosterRevision) return this.accounts;
|
|
5994
|
+
if (latest === void 0) {
|
|
5995
|
+
this.lastScanAtMs = Date.now();
|
|
5996
|
+
return this.accounts;
|
|
5997
|
+
}
|
|
5998
|
+
const walkedIds = new Set(found.map((credential) => workbuddyAccountId(credential)));
|
|
5999
|
+
for (const id of latest.keys()) if (!walkedIds.has(id)) tornDown.add(id);
|
|
6000
|
+
for (let index = found.length - 1; index >= 0; index -= 1) {
|
|
6001
|
+
const credential = found[index];
|
|
6002
|
+
const id = workbuddyAccountId(credential);
|
|
6003
|
+
const entry = tombstoneOf(latest, credential, id);
|
|
6004
|
+
if (entry !== void 0 && tombstoneBlocks(entry, credential)) {
|
|
6005
|
+
tornDown.add(id);
|
|
6006
|
+
found.splice(index, 1);
|
|
6007
|
+
}
|
|
6008
|
+
}
|
|
5503
6009
|
const sealedLiveIds = new Set(sealedFileIds.values());
|
|
5504
6010
|
for (let index = found.length - 1; index >= 0; index -= 1) {
|
|
5505
6011
|
const credential = found[index];
|
|
@@ -5537,6 +6043,8 @@ var WorkBuddyAccountPool = class {
|
|
|
5537
6043
|
for (const credential of archived) {
|
|
5538
6044
|
const id = workbuddyAccountId(credential);
|
|
5539
6045
|
if (tornDown.has(id)) continue;
|
|
6046
|
+
const entry = tombstoneOf(latest, credential, id);
|
|
6047
|
+
if (entry !== void 0 && tombstoneBlocks(entry, credential)) continue;
|
|
5540
6048
|
if (!this.isOwnSignInPath(credential.sourcePath) && (sealedLiveIds.has(id) || sealedDirs.has(this.homeOf(credential)))) continue;
|
|
5541
6049
|
if (found.some((entry) => workbuddyAccountId(entry) === id && entry.accessToken === credential.accessToken)) continue;
|
|
5542
6050
|
found.push(credential);
|
|
@@ -5545,6 +6053,9 @@ var WorkBuddyAccountPool = class {
|
|
|
5545
6053
|
const home = this.homeOf(credential);
|
|
5546
6054
|
if (sealedDirs.has(home) || unavailableDirs.has(home)) refreshBlockedIds.add(workbuddyAccountId(credential));
|
|
5547
6055
|
}
|
|
6056
|
+
const folded = foldSessionCopies(found, ownSignInDir(this.dataPath?.() ?? ""));
|
|
6057
|
+
found.length = 0;
|
|
6058
|
+
found.push(...folded);
|
|
5548
6059
|
const byId = /* @__PURE__ */ new Map();
|
|
5549
6060
|
for (const account of this.accounts) byId.set(account.id, account);
|
|
5550
6061
|
for (const credential of found) {
|
|
@@ -5555,7 +6066,7 @@ var WorkBuddyAccountPool = class {
|
|
|
5555
6066
|
id,
|
|
5556
6067
|
label: accountLabel(credential),
|
|
5557
6068
|
credential,
|
|
5558
|
-
cooldownUntilMs: 0,
|
|
6069
|
+
cooldownUntilMs: Math.max(0, this.heldIds.get(id)?.until ?? 0),
|
|
5559
6070
|
modelCooldowns: Object.create(null),
|
|
5560
6071
|
modelBlocks: Object.create(null),
|
|
5561
6072
|
rateLimitHits: 0
|
|
@@ -5616,14 +6127,43 @@ var WorkBuddyAccountPool = class {
|
|
|
5616
6127
|
}
|
|
5617
6128
|
disambiguateLabels(ordered);
|
|
5618
6129
|
if (!sameDirs(this.scanDirsFor(this.authDirs), dirs) || rosterRevision !== this.rosterRevision) return this.accounts;
|
|
5619
|
-
|
|
5620
|
-
|
|
5621
|
-
|
|
6130
|
+
const tombstoneFile = this.deletedSignInsFile();
|
|
6131
|
+
const commit = () => {
|
|
6132
|
+
this.accounts = ordered;
|
|
6133
|
+
this.signInTokens = signInTokens;
|
|
6134
|
+
this.lastScanAtMs = Date.now();
|
|
6135
|
+
};
|
|
6136
|
+
if (tombstoneFile === void 0) {
|
|
6137
|
+
commit();
|
|
6138
|
+
return this.accounts;
|
|
6139
|
+
}
|
|
6140
|
+
let committed = false;
|
|
6141
|
+
try {
|
|
6142
|
+
await withFileLock(`${tombstoneFile}.lock`, async () => {
|
|
6143
|
+
const finalTombstones = await this.loadDeletedSignIns(signal);
|
|
6144
|
+
if (signal.aborted || !sameDirs(this.scanDirsFor(this.authDirs), dirs) || rosterRevision !== this.rosterRevision) return;
|
|
6145
|
+
if (finalTombstones === void 0) return;
|
|
6146
|
+
if (await signInFileStamp(tombstoneFile) !== this.deletedSignInsStamp) return;
|
|
6147
|
+
for (let index = ordered.length - 1; index >= 0; index -= 1) {
|
|
6148
|
+
const account = ordered[index];
|
|
6149
|
+
const entry = tombstoneOf(finalTombstones, account.credential, account.id);
|
|
6150
|
+
if (entry !== void 0 && tombstoneBlocks(entry, account.credential)) {
|
|
6151
|
+
ordered.splice(index, 1);
|
|
6152
|
+
this.forgetAccount(account.id);
|
|
6153
|
+
}
|
|
6154
|
+
}
|
|
6155
|
+
commit();
|
|
6156
|
+
committed = true;
|
|
6157
|
+
});
|
|
6158
|
+
} catch (error) {
|
|
6159
|
+
if (!signal.aborted) this.logger?.warn?.(`${LOG_PREFIX}: could not lock the deleted sign-ins for the final roster check: ${String(error)}`);
|
|
6160
|
+
}
|
|
6161
|
+
if (!committed && !signal.aborted) this.lastScanAtMs = Date.now();
|
|
5622
6162
|
return this.accounts;
|
|
5623
6163
|
}
|
|
5624
6164
|
/** All accounts, cooldown state included. */
|
|
5625
6165
|
list(region) {
|
|
5626
|
-
const kept = this.accounts.filter((account) => !this.removedIds
|
|
6166
|
+
const kept = this.accounts.filter((account) => !this.storedIdMatches(this.removedIds, account));
|
|
5627
6167
|
if (region === void 0) return kept;
|
|
5628
6168
|
return kept.filter((account) => regionOf(account.credential.domain) === region);
|
|
5629
6169
|
}
|
|
@@ -5636,11 +6176,12 @@ var WorkBuddyAccountPool = class {
|
|
|
5636
6176
|
* panel call an account available that the rotation would never pick.
|
|
5637
6177
|
*/
|
|
5638
6178
|
ineligible(account, now, modelId, region) {
|
|
5639
|
-
if (this.removedIds
|
|
5640
|
-
if (this.disabledIds
|
|
5641
|
-
if (this.reserveHolds(account
|
|
6179
|
+
if (this.storedIdMatches(this.removedIds, account)) return "removed";
|
|
6180
|
+
if (this.storedIdMatches(this.disabledIds, account)) return "switched off";
|
|
6181
|
+
if (this.reserveHolds(account)) return "under its credit reserve";
|
|
5642
6182
|
if (credentialUnusable(account.credential, now)) return "sign-in expired";
|
|
5643
6183
|
if (region !== void 0 && regionOf(account.credential.domain) !== region) return "other region";
|
|
6184
|
+
if ((this.serverCooldowns.get(account.id) ?? 0) > now) return "server";
|
|
5644
6185
|
if (account.cooldownUntilMs > now) {
|
|
5645
6186
|
const reason = this.heldIds.get(account.id)?.reason;
|
|
5646
6187
|
return reason === "session" ? "sign-in refused" : reason === "restricted" ? "restricted by the upstream" : "on hold";
|
|
@@ -5729,29 +6270,37 @@ var WorkBuddyAccountPool = class {
|
|
|
5729
6270
|
pool = eligibleNow();
|
|
5730
6271
|
}
|
|
5731
6272
|
if (pool.length === 0) return void 0;
|
|
5732
|
-
const preferredId = this.
|
|
6273
|
+
const preferredId = this.preferredIdFor(region);
|
|
5733
6274
|
const remaining = new Set(pool.map((account) => account.id));
|
|
5734
6275
|
while (remaining.size > 0) {
|
|
5735
|
-
const account = pool.find((item) => item.id === preferredId) ?? this.pick(pool, conversationKey);
|
|
6276
|
+
const account = pool.find((item) => item.id === preferredId) ?? this.pick(pool, conversationKey, region);
|
|
5736
6277
|
if (account === void 0) return void 0;
|
|
5737
6278
|
remaining.delete(account.id);
|
|
5738
6279
|
await this.ensureFresh(account);
|
|
5739
6280
|
if (signal.aborted) return void 0;
|
|
5740
6281
|
const eligible = eligibleNow();
|
|
5741
|
-
if (eligible.includes(account))
|
|
6282
|
+
if (eligible.includes(account)) {
|
|
6283
|
+
this.servedOrder.set(account.id, ++this.servedCount);
|
|
6284
|
+
return account;
|
|
6285
|
+
}
|
|
5742
6286
|
pool = eligible.filter((item) => remaining.has(item.id));
|
|
5743
6287
|
}
|
|
5744
6288
|
}
|
|
5745
|
-
pick(pool, conversationKey) {
|
|
6289
|
+
pick(pool, conversationKey, region) {
|
|
5746
6290
|
if (this.distribution === "expiry") return this.pickByExpiry(pool);
|
|
5747
6291
|
if (this.distribution === "sticky" && conversationKey !== void 0) {
|
|
5748
6292
|
const bound = this.sticky.get(conversationKey);
|
|
5749
6293
|
const kept = bound === void 0 ? void 0 : pool.find((account) => account.id === bound);
|
|
5750
|
-
if (kept !== void 0)
|
|
5751
|
-
|
|
6294
|
+
if (kept !== void 0) {
|
|
6295
|
+
this.sticky.delete(conversationKey);
|
|
6296
|
+
this.sticky.set(conversationKey, kept.id);
|
|
6297
|
+
return kept;
|
|
6298
|
+
}
|
|
6299
|
+
const account = this.successor(pool, this.lastServedFor(region));
|
|
5752
6300
|
if (account === void 0) return void 0;
|
|
6301
|
+
this.sticky.delete(conversationKey);
|
|
5753
6302
|
this.sticky.set(conversationKey, account.id);
|
|
5754
|
-
this.
|
|
6303
|
+
this.setLastServed(region, account.id);
|
|
5755
6304
|
while (this.sticky.size > 200) {
|
|
5756
6305
|
const oldest = this.sticky.keys().next().value;
|
|
5757
6306
|
if (oldest === void 0) break;
|
|
@@ -5759,19 +6308,73 @@ var WorkBuddyAccountPool = class {
|
|
|
5759
6308
|
}
|
|
5760
6309
|
return account;
|
|
5761
6310
|
}
|
|
5762
|
-
const
|
|
5763
|
-
|
|
5764
|
-
if (account !== void 0 && this.distribution === "round-robin") this.cursor = (index + 1) % pool.length;
|
|
6311
|
+
const account = this.distribution === "round-robin" ? this.successor(pool, this.lastServedFor(region)) : pool[0];
|
|
6312
|
+
if (account !== void 0 && this.distribution === "round-robin") this.setLastServed(region, account.id);
|
|
5765
6313
|
return account;
|
|
5766
6314
|
}
|
|
6315
|
+
/**
|
|
6316
|
+
* The first account after `previous` in roster order that is still in `pool`.
|
|
6317
|
+
*
|
|
6318
|
+
* `previous` can be missing from `pool` because this request already skipped
|
|
6319
|
+
* it: a sign-in whose refresh failed is excluded from the retry. Walking the
|
|
6320
|
+
* full roster keeps the rotation moving past it; looking only inside `pool`
|
|
6321
|
+
* would restart at its head and retry the same account every turn while the
|
|
6322
|
+
* accounts behind the skipped one never served.
|
|
6323
|
+
*/
|
|
6324
|
+
successor(pool, previous) {
|
|
6325
|
+
if (pool.length === 0) return void 0;
|
|
6326
|
+
const start = previous === void 0 ? -1 : this.accounts.findIndex((account) => account.id === previous);
|
|
6327
|
+
if (start === -1) return pool[0];
|
|
6328
|
+
const present = new Set(pool.map((account) => account.id));
|
|
6329
|
+
for (let step = 1; step <= this.accounts.length; step += 1) {
|
|
6330
|
+
const candidate = this.accounts[(start + step) % this.accounts.length];
|
|
6331
|
+
if (candidate !== void 0 && present.has(candidate.id)) return candidate;
|
|
6332
|
+
}
|
|
6333
|
+
return pool[0];
|
|
6334
|
+
}
|
|
6335
|
+
/** The last account served through one region; absent regions share one. */
|
|
6336
|
+
lastServedFor(region) {
|
|
6337
|
+
return this.lastServed.get(region ?? "");
|
|
6338
|
+
}
|
|
6339
|
+
setLastServed(region, accountId) {
|
|
6340
|
+
this.lastServed.set(region ?? "", accountId);
|
|
6341
|
+
}
|
|
5767
6342
|
pickByExpiry(pool) {
|
|
5768
6343
|
return pickSoonestExpiry(pool, this.creditExpiries, Date.now());
|
|
5769
6344
|
}
|
|
5770
|
-
/**
|
|
5771
|
-
|
|
6345
|
+
/**
|
|
6346
|
+
* Next account the panel may name as "next up". Does not move the rotation.
|
|
6347
|
+
*
|
|
6348
|
+
* Sticky has no single next account - it is per conversation - so it names
|
|
6349
|
+
* nothing rather than pointing at a guess. The composer readout uses
|
|
6350
|
+
* `readoutPreview`, which can name the account that served last instead.
|
|
6351
|
+
*/
|
|
6352
|
+
preview(eligible, region) {
|
|
5772
6353
|
if (eligible.length === 0) return void 0;
|
|
5773
6354
|
if (this.distribution === "expiry") return this.pickByExpiry(eligible);
|
|
5774
6355
|
if (this.distribution === "priority") return eligible[0];
|
|
6356
|
+
if (this.distribution === "round-robin") return this.successor(eligible, this.lastServedFor(region));
|
|
6357
|
+
}
|
|
6358
|
+
/**
|
|
6359
|
+
* The account the composer readout names. Does not move the rotation.
|
|
6360
|
+
*
|
|
6361
|
+
* Round-robin names the account the next request takes. Sticky names the one
|
|
6362
|
+
* that served most recently, because the current conversation is most likely
|
|
6363
|
+
* on it; before the first turn the rotation stands in.
|
|
6364
|
+
*/
|
|
6365
|
+
readoutPreview(eligible, region) {
|
|
6366
|
+
if (eligible.length === 0) return void 0;
|
|
6367
|
+
if (this.distribution !== "sticky") return this.preview(eligible, region);
|
|
6368
|
+
let latest;
|
|
6369
|
+
let latestAt = -1;
|
|
6370
|
+
for (const account of eligible) {
|
|
6371
|
+
const servedAt = this.servedOrder.get(account.id) ?? -1;
|
|
6372
|
+
if (servedAt > latestAt) {
|
|
6373
|
+
latest = account;
|
|
6374
|
+
latestAt = servedAt;
|
|
6375
|
+
}
|
|
6376
|
+
}
|
|
6377
|
+
return latest ?? this.successor(eligible, this.lastServedFor(region));
|
|
5775
6378
|
}
|
|
5776
6379
|
/** Prefer one billing account per gateway; undefined restores automatic use. */
|
|
5777
6380
|
prefer(accountId, region) {
|
|
@@ -5779,6 +6382,32 @@ var WorkBuddyAccountPool = class {
|
|
|
5779
6382
|
else this.preferredIds.set(region, accountId);
|
|
5780
6383
|
}
|
|
5781
6384
|
/**
|
|
6385
|
+
* The stored preference for one region, translated from an older key.
|
|
6386
|
+
*
|
|
6387
|
+
* A preference saved before an identity change names a row that no rescan
|
|
6388
|
+
* produces any more. Leaving it untranslated meant the panel kept a selection
|
|
6389
|
+
* that could never be handed out, so automatic distribution silently served
|
|
6390
|
+
* another account while the panel named the old one.
|
|
6391
|
+
*/
|
|
6392
|
+
preferredIdFor(region) {
|
|
6393
|
+
const stored = this.preferredIds.get(region);
|
|
6394
|
+
if (stored === void 0) return void 0;
|
|
6395
|
+
if (this.accounts.some((account) => account.id === stored)) return stored;
|
|
6396
|
+
const claimants = this.accounts.filter((item) => (region === void 0 || regionOf(item.credential.domain) === region) && previousAccountIds(item.credential).includes(stored));
|
|
6397
|
+
return claimants.length === 1 && claimants[0] !== void 0 ? claimants[0].id : stored;
|
|
6398
|
+
}
|
|
6399
|
+
/**
|
|
6400
|
+
* The current id a stored preference resolves to.
|
|
6401
|
+
*
|
|
6402
|
+
* The panel and the composer show this rather than the raw settings value:
|
|
6403
|
+
* after an upgrade that value can still be a 1.2.3 key, and showing it made
|
|
6404
|
+
* the row read as "unavailable" while `acquire` had already translated it and
|
|
6405
|
+
* served another account.
|
|
6406
|
+
*/
|
|
6407
|
+
resolvedPreferredId(region) {
|
|
6408
|
+
return this.preferredIdFor(region);
|
|
6409
|
+
}
|
|
6410
|
+
/**
|
|
5782
6411
|
* Refresh one account after a session-dead upstream answer, ignoring the
|
|
5783
6412
|
* stored expiry: a revocation does not rewrite it. False when the access
|
|
5784
6413
|
* token is unchanged, so the caller stops retrying the same dead token.
|
|
@@ -5865,15 +6494,59 @@ var WorkBuddyAccountPool = class {
|
|
|
5865
6494
|
}
|
|
5866
6495
|
}
|
|
5867
6496
|
/**
|
|
6497
|
+
* Whether a refresh's rotated token still belongs to a sign-in on disk.
|
|
6498
|
+
*
|
|
6499
|
+
* The pool running the refresh need not be the one that deleted the sign-in,
|
|
6500
|
+
* so its roster cannot answer this. The file can: it holds the token the
|
|
6501
|
+
* refresh started from, the one it just minted, or something else (a re-login,
|
|
6502
|
+
* or nothing after the delete). Only the first two are still this pool's to
|
|
6503
|
+
* archive; anything else belongs to a session the user replaced or removed.
|
|
6504
|
+
*/
|
|
6505
|
+
async refreshedSignInIsOnDisk(previous, refreshed) {
|
|
6506
|
+
const read = await readCredential(refreshed.sourcePath);
|
|
6507
|
+
if (read.kind !== "credential") return false;
|
|
6508
|
+
return read.credential.accessToken === previous.accessToken || read.credential.accessToken === refreshed.accessToken || read.credential.refreshToken !== "" && read.credential.refreshToken === refreshed.refreshToken;
|
|
6509
|
+
}
|
|
6510
|
+
/**
|
|
6511
|
+
* Archive a rotated token only while its sign-in is still on disk.
|
|
6512
|
+
*
|
|
6513
|
+
* Checked under the tombstone lock so a delete cannot commit between the
|
|
6514
|
+
* check and the archive write: before the lock, its record blocks the write;
|
|
6515
|
+
* after it, the re-login has already replaced the file the check sees.
|
|
6516
|
+
* Answers whether the rotated token still belongs to a live sign-in; false
|
|
6517
|
+
* means the caller must not keep serving it.
|
|
6518
|
+
*/
|
|
6519
|
+
async archiveRefreshedCredential(previous, refreshed) {
|
|
6520
|
+
let kept = false;
|
|
6521
|
+
const archive = async () => {
|
|
6522
|
+
if (await this.refreshedSignInIsOnDisk(previous, refreshed)) {
|
|
6523
|
+
await this.archiveCredentials([refreshed]);
|
|
6524
|
+
kept = true;
|
|
6525
|
+
}
|
|
6526
|
+
};
|
|
6527
|
+
const file = this.deletedSignInsFile();
|
|
6528
|
+
if (file === void 0) {
|
|
6529
|
+
await archive();
|
|
6530
|
+
return kept;
|
|
6531
|
+
}
|
|
6532
|
+
try {
|
|
6533
|
+
await withFileLock(`${file}.lock`, archive);
|
|
6534
|
+
} catch (error) {
|
|
6535
|
+
this.logger?.warn?.(`${LOG_PREFIX}: could not lock the deleted sign-ins to archive a renewed token: ${String(error)}`);
|
|
6536
|
+
return false;
|
|
6537
|
+
}
|
|
6538
|
+
return kept;
|
|
6539
|
+
}
|
|
6540
|
+
/**
|
|
5868
6541
|
* Refresh the account's access token when it is within the margin (or already
|
|
5869
6542
|
* expired), in-flight de-duped per account. A failed refresh keeps the
|
|
5870
6543
|
* existing token when it has not yet expired, so an unreachable refresh
|
|
5871
6544
|
* endpoint never takes down a working session.
|
|
5872
6545
|
*
|
|
5873
6546
|
* Reports what it did, so a caller walking the pool for keep-alive can tell a
|
|
5874
|
-
* pass that refreshed something from one that had nothing to do.
|
|
5875
|
-
*
|
|
5876
|
-
*
|
|
6547
|
+
* pass that refreshed something from one that had nothing to do. An in-flight
|
|
6548
|
+
* refresh another call started is awaited and its result is shared: this call
|
|
6549
|
+
* spent no upstream call of its own, but the account's outcome is the same.
|
|
5877
6550
|
*/
|
|
5878
6551
|
async ensureFresh(account, forced = false) {
|
|
5879
6552
|
if (this.client === void 0) return "fresh";
|
|
@@ -5895,7 +6568,7 @@ var WorkBuddyAccountPool = class {
|
|
|
5895
6568
|
}
|
|
5896
6569
|
try {
|
|
5897
6570
|
const outcome = await this.client.refreshToken(credential);
|
|
5898
|
-
if (account.credential !== credential) return "
|
|
6571
|
+
if (account.credential !== credential || !this.accounts.includes(account)) return "failed";
|
|
5899
6572
|
account.credential = {
|
|
5900
6573
|
...credential,
|
|
5901
6574
|
accessToken: outcome.accessToken,
|
|
@@ -5908,10 +6581,11 @@ var WorkBuddyAccountPool = class {
|
|
|
5908
6581
|
try {
|
|
5909
6582
|
written = await this.renewOwnSignIn(credential, account.credential);
|
|
5910
6583
|
} catch (error) {
|
|
5911
|
-
this.logger?.warn?.(`${LOG_PREFIX}: could not persist the renewed token for ${accountLogTag(account.label)}
|
|
5912
|
-
await this.
|
|
5913
|
-
|
|
5914
|
-
|
|
6584
|
+
this.logger?.warn?.(`${LOG_PREFIX}: could not persist the renewed token for ${accountLogTag(account.label)}`, error);
|
|
6585
|
+
if (!await this.archiveRefreshedCredential(credential, account.credential)) {
|
|
6586
|
+
account.credential = credential;
|
|
6587
|
+
this.forgetAccount(account.id);
|
|
6588
|
+
}
|
|
5915
6589
|
throw error;
|
|
5916
6590
|
}
|
|
5917
6591
|
if (!written) {
|
|
@@ -5919,7 +6593,11 @@ var WorkBuddyAccountPool = class {
|
|
|
5919
6593
|
await this.scan();
|
|
5920
6594
|
return "fresh";
|
|
5921
6595
|
}
|
|
5922
|
-
await this.
|
|
6596
|
+
if (!await this.archiveRefreshedCredential(credential, account.credential)) {
|
|
6597
|
+
account.credential = credential;
|
|
6598
|
+
this.forgetAccount(account.id);
|
|
6599
|
+
return "failed";
|
|
6600
|
+
}
|
|
5923
6601
|
this.noRefreshWarned.delete(account.id);
|
|
5924
6602
|
const sessionHold = this.heldIds.get(account.id);
|
|
5925
6603
|
if (sessionHold !== void 0 && sessionHold.reason === "session") {
|
|
@@ -5939,7 +6617,7 @@ var WorkBuddyAccountPool = class {
|
|
|
5939
6617
|
try {
|
|
5940
6618
|
return await run;
|
|
5941
6619
|
} finally {
|
|
5942
|
-
this.refreshInflight.delete(account.id);
|
|
6620
|
+
if (this.refreshInflight.get(account.id) === run) this.refreshInflight.delete(account.id);
|
|
5943
6621
|
}
|
|
5944
6622
|
}
|
|
5945
6623
|
/**
|
|
@@ -5956,20 +6634,21 @@ var WorkBuddyAccountPool = class {
|
|
|
5956
6634
|
* exists to stop a mis-parsed reset from parking an account all day, and
|
|
5957
6635
|
* neither of these conditions clears by itself.
|
|
5958
6636
|
*/
|
|
5959
|
-
penalize(accountId, resetAtMs, modelId, hold = false) {
|
|
6637
|
+
penalize(accountId, resetAtMs, modelId, hold = false, failure = "limit") {
|
|
5960
6638
|
const account = this.accounts.find((item) => item.id === accountId);
|
|
5961
6639
|
if (account === void 0) return;
|
|
5962
|
-
|
|
6640
|
+
const countsAsLimit = hold === false && failure === "limit";
|
|
6641
|
+
if (countsAsLimit) account.rateLimitHits += 1;
|
|
5963
6642
|
const now = Date.now();
|
|
5964
6643
|
const stated = resetAtMs !== void 0 && resetAtMs > now;
|
|
5965
|
-
if (
|
|
6644
|
+
if (countsAsLimit && !stated) {
|
|
5966
6645
|
account.rateLimitStreak = now - (account.rateLimitStreakAtMs ?? 0) > RATE_LIMIT_STREAK_WINDOW_MS ? 1 : (account.rateLimitStreak ?? 0) + 1;
|
|
5967
6646
|
account.rateLimitStreakAtMs = now;
|
|
5968
6647
|
}
|
|
5969
6648
|
const holdReason = hold === false ? void 0 : hold;
|
|
5970
6649
|
const cap = holdReason !== void 0 ? POOL_HOLD_MS : PENALIZE_CAP_MS;
|
|
5971
6650
|
const streak = Math.min(Math.max((account.rateLimitStreak ?? 1) - 1, 0), RATE_LIMIT_STREAK_STEPS);
|
|
5972
|
-
const backoff = Math.min(this.cooldownMs * 2 ** streak, Math.max(this.cooldownMs, RATE_LIMIT_STREAK_CAP));
|
|
6651
|
+
const backoff = countsAsLimit ? Math.min(this.cooldownMs * 2 ** streak, Math.max(this.cooldownMs, RATE_LIMIT_STREAK_CAP)) : this.cooldownMs;
|
|
5973
6652
|
const until = stated ? Math.min(resetAtMs, now + cap) : holdReason !== void 0 ? now + cap : now + backoff;
|
|
5974
6653
|
if (modelId !== void 0 && modelId !== "") {
|
|
5975
6654
|
pruneModelCooldowns(account, now);
|
|
@@ -5978,6 +6657,8 @@ var WorkBuddyAccountPool = class {
|
|
|
5978
6657
|
return;
|
|
5979
6658
|
}
|
|
5980
6659
|
account.cooldownUntilMs = Math.max(account.cooldownUntilMs, until);
|
|
6660
|
+
if (failure === "server" && hold === false) this.serverCooldowns.set(account.id, until);
|
|
6661
|
+
else this.serverCooldowns.delete(account.id);
|
|
5981
6662
|
if (holdReason !== void 0) {
|
|
5982
6663
|
const prior = this.heldIds.get(accountId);
|
|
5983
6664
|
const carried = prior !== void 0 && prior.until > now && prior.reason === "session" ? prior : void 0;
|
|
@@ -6019,6 +6700,7 @@ var WorkBuddyAccountPool = class {
|
|
|
6019
6700
|
}
|
|
6020
6701
|
this.heldIds.clear();
|
|
6021
6702
|
this.refusedSignIns.clear();
|
|
6703
|
+
this.serverCooldowns.clear();
|
|
6022
6704
|
}
|
|
6023
6705
|
/** Diagnostics snapshot. Account-wide cooling count (per-model cooling excluded:
|
|
6024
6706
|
* the account as a whole stays usable when only one model is limited). */
|
|
@@ -6530,6 +7212,18 @@ function conversationKeyOf(raw) {
|
|
|
6530
7212
|
return;
|
|
6531
7213
|
}
|
|
6532
7214
|
}
|
|
7215
|
+
/**
|
|
7216
|
+
* The upstream a sign-in's chat traffic goes to.
|
|
7217
|
+
*
|
|
7218
|
+
* Transport failures are a property of the host, not of the account: a pool can
|
|
7219
|
+
* hold sign-ins served by different gateways, and one unreachable host says
|
|
7220
|
+
* nothing about the next one. The sign-in domain is not the chat host (every CN
|
|
7221
|
+
* spelling lands on one gateway, and each international brand has its own), so
|
|
7222
|
+
* the host is read from the same mapping the request itself uses.
|
|
7223
|
+
*/
|
|
7224
|
+
function upstreamHostOf(account) {
|
|
7225
|
+
return chatHostOf(account.credential);
|
|
7226
|
+
}
|
|
6533
7227
|
const EMPTY_STREAM_REFUSAL = "workbuddy returned an empty answer stream";
|
|
6534
7228
|
/**
|
|
6535
7229
|
* Read enough of a 200 body to tell an SSE stream from a refusal wearing a 200.
|
|
@@ -6716,6 +7410,11 @@ function unavailableAnswer(pool, live, now, modelId) {
|
|
|
6716
7410
|
code: "model_tier",
|
|
6717
7411
|
detail: `no WorkBuddy account has model ${modelId} in its plan; pick another model, or add an account whose plan includes it`
|
|
6718
7412
|
};
|
|
7413
|
+
if (only === "server") return {
|
|
7414
|
+
status: 503,
|
|
7415
|
+
code: "server",
|
|
7416
|
+
detail: `every WorkBuddy account is cooling after upstream server errors for model ${modelId}; retry shortly`
|
|
7417
|
+
};
|
|
6719
7418
|
if (only === "restricted by the upstream") return {
|
|
6720
7419
|
status: 403,
|
|
6721
7420
|
code: "account_restricted",
|
|
@@ -6786,7 +7485,8 @@ function createWorkBuddyShim(options) {
|
|
|
6786
7485
|
res.on("close", () => controller.abort());
|
|
6787
7486
|
const raw = (await readRequestBody(req)).toString("utf8");
|
|
6788
7487
|
const prepared = client.prepareChatBody(raw);
|
|
6789
|
-
const
|
|
7488
|
+
const conversationHash = conversationKeyOf(raw);
|
|
7489
|
+
const conversationKey = conversationHash === void 0 || region === void 0 ? conversationHash : `${region}\n${conversationHash}`;
|
|
6790
7490
|
let modelId;
|
|
6791
7491
|
let bodyIsJson = true;
|
|
6792
7492
|
try {
|
|
@@ -6811,19 +7511,57 @@ function createWorkBuddyShim(options) {
|
|
|
6811
7511
|
let last;
|
|
6812
7512
|
let accountSkipped = false;
|
|
6813
7513
|
let transportAttempts = 0;
|
|
7514
|
+
/** Hosts that failed transport in this request; one failure condemns none of the others. */
|
|
7515
|
+
const transportHosts = /* @__PURE__ */ new Set();
|
|
7516
|
+
/** Whether any eligible account this request has not tried belongs to a host that has not failed. */
|
|
7517
|
+
const hasUntriedHost = () => {
|
|
7518
|
+
for (const peer of pool.available(Date.now(), modelId, region)) if (!skipped.has(peer.id) && !transportHosts.has(upstreamHostOf(peer))) return true;
|
|
7519
|
+
return false;
|
|
7520
|
+
};
|
|
7521
|
+
/**
|
|
7522
|
+
* Stop trying accounts served by a host that has already timed out.
|
|
7523
|
+
*
|
|
7524
|
+
* Used only once the transport cap is reached and another host is still
|
|
7525
|
+
* waiting: the first failure gets one retry, and after that a peer on the
|
|
7526
|
+
* same host only pays the same timeout again while a healthy account behind
|
|
7527
|
+
* it waits. A single-host pool never reaches this branch because no other
|
|
7528
|
+
* host is left to justify it.
|
|
7529
|
+
*/
|
|
7530
|
+
const skipHost = (host) => {
|
|
7531
|
+
for (const peer of pool.list(region)) if (upstreamHostOf(peer) === host) skipped.add(peer.id);
|
|
7532
|
+
};
|
|
7533
|
+
/**
|
|
7534
|
+
* Record one transport failure and answer whether the turn is spent.
|
|
7535
|
+
*
|
|
7536
|
+
* Up to the cap the next account gets its turn, including a peer on the
|
|
7537
|
+
* same host. Past the cap another host still waiting is what keeps the turn
|
|
7538
|
+
* alive, and the failed host's remaining peers are skipped so the healthy
|
|
7539
|
+
* host is reached without paying their timeouts first.
|
|
7540
|
+
*/
|
|
7541
|
+
const transportSpent = (host) => {
|
|
7542
|
+
transportHosts.add(host);
|
|
7543
|
+
transportAttempts += 1;
|
|
7544
|
+
if (transportAttempts < MAX_TRANSPORT_ATTEMPTS) return false;
|
|
7545
|
+
if (!hasUntriedHost()) return true;
|
|
7546
|
+
skipHost(host);
|
|
7547
|
+
return false;
|
|
7548
|
+
};
|
|
6814
7549
|
let emptyStreams = 0;
|
|
7550
|
+
const refreshedSessions = /* @__PURE__ */ new Set();
|
|
7551
|
+
let retryAccountId;
|
|
6815
7552
|
let effortFallback;
|
|
6816
7553
|
let effortFallbackBuilt = false;
|
|
6817
|
-
const maxAttempts = Math.max(8, pool.list(region).length + 2);
|
|
7554
|
+
const maxAttempts = Math.max(8, pool.list(region).length * 2 + 2);
|
|
6818
7555
|
const maxEmptyStreams = emptyStreamAttemptsFor(pool.list(region).length);
|
|
6819
7556
|
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
6820
7557
|
if (controller.signal.aborted) return;
|
|
6821
|
-
const account = await pool.acquire(modelId, region, conversationKey, skipped);
|
|
7558
|
+
const account = retryAccountId !== void 0 ? pool.available(Date.now(), modelId, region).find((candidate) => candidate.id === retryAccountId) ?? await pool.acquire(modelId, region, conversationKey, skipped) : await pool.acquire(modelId, region, conversationKey, skipped);
|
|
7559
|
+
retryAccountId = void 0;
|
|
6822
7560
|
if (account === void 0) {
|
|
6823
7561
|
if (accountSkipped && last !== void 0) {
|
|
6824
7562
|
const model = ` for model ${modelId}`;
|
|
6825
|
-
const subject = last.kind === "waf_block" ? "every WorkBuddy account was blocked by the upstream gateway (WAF)" : last.kind === "hard_credit" ? `every WorkBuddy account is out of credits${model}` : last.kind === "session_dead" ? `every WorkBuddy sign-in is no longer accepted${model}` : last.kind === "model_tier" ? `no WorkBuddy account can use model ${modelId}` : last.kind === "account_restricted" ? "every WorkBuddy account was restricted by the upstream" : last.kind === "transport" ? emptyStreams > 0 && transportAttempts === 0 ? `every WorkBuddy account answered${model} with an empty stream` : `the upstream host could not be reached on any account${model}` : `every account is rate-limited${model}`;
|
|
6826
|
-
const advice = last.kind === "soft_rate" ? "resets at the upstream window" : last.kind === "model_tier" ? "pick another model, or add an account whose plan includes it" : last.kind === "account_restricted" ? "appeal the restriction in the WorkBuddy console, or add another account" : last.kind === "waf_block" ? "wait for the upstream gateway block to clear, or change network" : last.kind === "transport" ? emptyStreams > 0 && transportAttempts === 0 ? "the upstream answered and then produced no content; retry the request" : "check the connection to the upstream host" : last.kind === "hard_credit" ? "top up the account, or add another account" : "sign in again in the desktop app";
|
|
7563
|
+
const subject = last.kind === "waf_block" ? "every WorkBuddy account was blocked by the upstream gateway (WAF)" : last.kind === "hard_credit" ? `every WorkBuddy account is out of credits${model}` : last.kind === "session_dead" ? `every WorkBuddy sign-in is no longer accepted${model}` : last.kind === "model_tier" ? `no WorkBuddy account can use model ${modelId}` : last.kind === "account_restricted" ? "every WorkBuddy account was restricted by the upstream" : last.kind === "transport" ? emptyStreams > 0 && transportAttempts === 0 ? `every WorkBuddy account answered${model} with an empty stream` : `the upstream host could not be reached on any account${model}` : last.kind === "server" ? `every WorkBuddy account answered${model} with an upstream server error` : `every account is rate-limited${model}`;
|
|
7564
|
+
const advice = last.kind === "soft_rate" ? "resets at the upstream window" : last.kind === "model_tier" ? "pick another model, or add an account whose plan includes it" : last.kind === "account_restricted" ? "appeal the restriction in the WorkBuddy console, or add another account" : last.kind === "waf_block" ? "wait for the upstream gateway block to clear, or change network" : last.kind === "transport" ? emptyStreams > 0 && transportAttempts === 0 ? "the upstream answered and then produced no content; retry the request" : "check the connection to the upstream host" : last.kind === "server" ? "retry shortly; the upstream service is down" : last.kind === "hard_credit" ? "top up the account, or add another account" : "sign in again in the desktop app";
|
|
6827
7565
|
if (last.kind === "soft_rate") {
|
|
6828
7566
|
const live = pool.list(region).filter((candidate) => !pool.isDisabled(candidate.id));
|
|
6829
7567
|
const wait = unavailableAnswer(pool, live, Date.now(), modelId).retryAfterSeconds;
|
|
@@ -6852,7 +7590,7 @@ function createWorkBuddyShim(options) {
|
|
|
6852
7590
|
tried += 1;
|
|
6853
7591
|
let result = await client.chatStream(account.credential, prepared, controller.signal);
|
|
6854
7592
|
let refreshOutcome;
|
|
6855
|
-
if (!result.ok && result.kind === "session_dead") {
|
|
7593
|
+
if (!result.ok && result.kind === "session_dead" && !refreshedSessions.has(account.id)) {
|
|
6856
7594
|
const failure = result;
|
|
6857
7595
|
refreshOutcome = await pool.refreshAccountOutcome(account.id);
|
|
6858
7596
|
if (refreshOutcome === "failed") {
|
|
@@ -6868,6 +7606,7 @@ function createWorkBuddyShim(options) {
|
|
|
6868
7606
|
}
|
|
6869
7607
|
}
|
|
6870
7608
|
if (refreshOutcome === "refreshed") {
|
|
7609
|
+
refreshedSessions.add(account.id);
|
|
6871
7610
|
if (controller.signal.aborted) return;
|
|
6872
7611
|
result = await client.chatStream(account.credential, prepared, controller.signal);
|
|
6873
7612
|
}
|
|
@@ -6988,7 +7727,7 @@ function createWorkBuddyShim(options) {
|
|
|
6988
7727
|
logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} answered 200 with an empty stream; rotating`);
|
|
6989
7728
|
return;
|
|
6990
7729
|
}
|
|
6991
|
-
logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} answered 200 with no stream: ${peeked.refusal}`);
|
|
7730
|
+
logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} answered 200 with no stream: ${safeMessage(peeked.refusal)}`);
|
|
6992
7731
|
if (peeked.code === "11128" || unapprovedChannelRefusal(peeked.refusal) !== void 0) {
|
|
6993
7732
|
writeOpenAIError(res, 400, "upstream_channel_check", channelCheckRefusal());
|
|
6994
7733
|
return;
|
|
@@ -7009,15 +7748,25 @@ function createWorkBuddyShim(options) {
|
|
|
7009
7748
|
pool.penalize(account.id, resetAtMs, modelId);
|
|
7010
7749
|
} else if (envelopeKind === "model_tier") pool.blockModel(account.id, modelId, Date.now() + MODEL_TIER_HOLD_MS);
|
|
7011
7750
|
else if (envelopeKind === "session_dead") {
|
|
7012
|
-
|
|
7013
|
-
|
|
7014
|
-
else if (refreshOutcome === "failed") {
|
|
7751
|
+
if (refreshedSessions.has(account.id)) {
|
|
7752
|
+
pool.penalize(account.id, holdUntil(), void 0, "session");
|
|
7015
7753
|
skipped.add(account.id);
|
|
7016
|
-
|
|
7017
|
-
|
|
7018
|
-
|
|
7019
|
-
|
|
7020
|
-
|
|
7754
|
+
} else {
|
|
7755
|
+
const refreshOutcome = await pool.refreshAccountOutcome(account.id);
|
|
7756
|
+
if (refreshOutcome === "refused") {
|
|
7757
|
+
pool.penalize(account.id, holdUntil(), void 0, "session");
|
|
7758
|
+
skipped.add(account.id);
|
|
7759
|
+
} else if (refreshOutcome === "failed") {
|
|
7760
|
+
skipped.add(account.id);
|
|
7761
|
+
last = {
|
|
7762
|
+
kind: "transport",
|
|
7763
|
+
status: 502,
|
|
7764
|
+
message: `could not refresh the sign-in: ${peeked.refusal.slice(0, 200)}`
|
|
7765
|
+
};
|
|
7766
|
+
} else {
|
|
7767
|
+
refreshedSessions.add(account.id);
|
|
7768
|
+
retryAccountId = account.id;
|
|
7769
|
+
}
|
|
7021
7770
|
}
|
|
7022
7771
|
} else pool.penalize(account.id, holdUntil(), void 0, envelopeKind === "hard_credit" ? "spent" : "restricted");
|
|
7023
7772
|
return;
|
|
@@ -7076,10 +7825,7 @@ function createWorkBuddyShim(options) {
|
|
|
7076
7825
|
if (headIsEmptyStream) {
|
|
7077
7826
|
emptyStreams += 1;
|
|
7078
7827
|
spent = emptyStreams >= maxEmptyStreams;
|
|
7079
|
-
} else
|
|
7080
|
-
transportAttempts += 1;
|
|
7081
|
-
spent = transportAttempts >= MAX_TRANSPORT_ATTEMPTS;
|
|
7082
|
-
}
|
|
7828
|
+
} else spent = transportSpent(upstreamHostOf(account));
|
|
7083
7829
|
if (spent) break;
|
|
7084
7830
|
continue;
|
|
7085
7831
|
}
|
|
@@ -7109,7 +7855,10 @@ function createWorkBuddyShim(options) {
|
|
|
7109
7855
|
* 402 back at the head of the queue every minute. A WAF page is the
|
|
7110
7856
|
* gateway's own and clears on its own within minutes, so it takes the
|
|
7111
7857
|
* configured window rather than a hold that would park a healthy account
|
|
7112
|
-
* for hours over a transient block.
|
|
7858
|
+
* for hours over a transient block. A 5xx is the upstream's own fault, but
|
|
7859
|
+
* it is still served per account: one broken deployment must not lead
|
|
7860
|
+
* every turn, so it takes the configured window and the next account gets
|
|
7861
|
+
* the request.
|
|
7113
7862
|
*
|
|
7114
7863
|
* A `transport` failure is the network path's. A global pool need not
|
|
7115
7864
|
* share a host: `codebuddy.ai` and `workbuddy.ai` logins can be served by
|
|
@@ -7128,12 +7877,11 @@ function createWorkBuddyShim(options) {
|
|
|
7128
7877
|
};
|
|
7129
7878
|
accountSkipped = true;
|
|
7130
7879
|
skipped.add(account.id);
|
|
7131
|
-
|
|
7132
|
-
if (transportAttempts >= MAX_TRANSPORT_ATTEMPTS) break;
|
|
7880
|
+
if (transportSpent(upstreamHostOf(account))) break;
|
|
7133
7881
|
logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} transport failure; rotating`);
|
|
7134
7882
|
continue;
|
|
7135
7883
|
}
|
|
7136
|
-
if (result.kind !== "soft_rate" && result.kind !== "waf_block" && result.kind !== "hard_credit" && result.kind !== "session_dead" && result.kind !== "model_tier" && result.kind !== "account_restricted") break;
|
|
7884
|
+
if (result.kind !== "soft_rate" && result.kind !== "waf_block" && result.kind !== "hard_credit" && result.kind !== "session_dead" && result.kind !== "model_tier" && result.kind !== "account_restricted" && result.kind !== "server") break;
|
|
7137
7885
|
accountSkipped = true;
|
|
7138
7886
|
if (result.kind === "soft_rate") {
|
|
7139
7887
|
const bodyReset = parseRateLimitReset(result.message);
|
|
@@ -7143,7 +7891,7 @@ function createWorkBuddyShim(options) {
|
|
|
7143
7891
|
} else if (result.kind === "model_tier") pool.blockModel(account.id, modelId, Date.now() + MODEL_TIER_HOLD_MS);
|
|
7144
7892
|
else if (result.kind === "account_restricted") pool.penalize(account.id, holdUntil(), void 0, "restricted");
|
|
7145
7893
|
else if (result.kind === "hard_credit" || result.kind === "session_dead") pool.penalize(account.id, holdUntil(), void 0, result.kind === "hard_credit" ? "spent" : "session");
|
|
7146
|
-
else pool.penalize(account.id);
|
|
7894
|
+
else pool.penalize(account.id, void 0, void 0, false, result.kind === "server" || result.kind === "waf_block" ? "server" : "limit");
|
|
7147
7895
|
logger?.warn(`${LOG_PREFIX}: ${accountLogTag(account.label)} ${result.kind} on ${modelId} (attempt ${attempt + 1}/${maxAttempts}); rotating`);
|
|
7148
7896
|
}
|
|
7149
7897
|
if (last === void 0) {
|
|
@@ -10083,6 +10831,10 @@ async function unwrapQoderOscryptKey(localStatePath, options = {}) {
|
|
|
10083
10831
|
inheritTemp: true,
|
|
10084
10832
|
includeSystemDrive: true
|
|
10085
10833
|
}),
|
|
10834
|
+
cwd: (() => {
|
|
10835
|
+
const root = trustedSystemRootOf(env);
|
|
10836
|
+
return root === void 0 ? void 0 : join(root, "System32");
|
|
10837
|
+
})(),
|
|
10086
10838
|
windowsHide: true,
|
|
10087
10839
|
stdio: [
|
|
10088
10840
|
"pipe",
|
|
@@ -10112,6 +10864,7 @@ async function unwrapQoderOscryptKey(localStatePath, options = {}) {
|
|
|
10112
10864
|
if (code === 0) resolve(out);
|
|
10113
10865
|
else reject(new QoderKeyError("unavailable", `PowerShell exited ${code}: ${err.trim().slice(0, 160)}`));
|
|
10114
10866
|
});
|
|
10867
|
+
child.stdin.on("error", () => {});
|
|
10115
10868
|
child.stdin.end(payload.toString("base64"));
|
|
10116
10869
|
});
|
|
10117
10870
|
const key = Buffer.from(stdout.trim(), "base64");
|
|
@@ -12537,6 +13290,12 @@ function originFor(realm) {
|
|
|
12537
13290
|
* upstream answer that named `javascript:` or `file:` would otherwise be handed
|
|
12538
13291
|
* straight to the desktop shell.
|
|
12539
13292
|
*/
|
|
13293
|
+
const LOGIN_AUTH_HOSTS = /* @__PURE__ */ new Set([
|
|
13294
|
+
"copilot.tencent.com",
|
|
13295
|
+
"www.codebuddy.cn",
|
|
13296
|
+
"www.codebuddy.ai",
|
|
13297
|
+
"www.workbuddy.ai"
|
|
13298
|
+
]);
|
|
12540
13299
|
function checkedAuthUrl(value) {
|
|
12541
13300
|
let parsed;
|
|
12542
13301
|
try {
|
|
@@ -12545,6 +13304,7 @@ function checkedAuthUrl(value) {
|
|
|
12545
13304
|
throw new Error("workbuddy login start returned an invalid url");
|
|
12546
13305
|
}
|
|
12547
13306
|
if (parsed.protocol !== "https:") throw new Error("workbuddy login start returned a non-https url");
|
|
13307
|
+
if (parsed.username !== "" || parsed.password !== "" || !LOGIN_AUTH_HOSTS.has(parsed.hostname)) throw new Error("workbuddy login start returned an unexpected host");
|
|
12548
13308
|
return value;
|
|
12549
13309
|
}
|
|
12550
13310
|
/** Browser-shaped headers; `Origin` is the one the endpoints check. */
|
|
@@ -12682,6 +13442,26 @@ async function pollDeviceLogin(realm, state, fetchImpl = fetch) {
|
|
|
12682
13442
|
*
|
|
12683
13443
|
* @module dsh-wtq-pool/source-pool
|
|
12684
13444
|
*/
|
|
13445
|
+
/**
|
|
13446
|
+
* One label per credential, unique inside a source region.
|
|
13447
|
+
*
|
|
13448
|
+
* Trae and Qoder list an account by its display name, and two accounts can
|
|
13449
|
+
* share one. Every per-account decision - the picked account, a failure hold,
|
|
13450
|
+
* a check-in row - is keyed by that label, so a duplicate let one account's
|
|
13451
|
+
* failure hide the other and put a balance under the wrong name. A repeated
|
|
13452
|
+
* name gets the vendor's own user id appended; a name used once is unchanged.
|
|
13453
|
+
*/
|
|
13454
|
+
function uniqueSourceLabels(items, labelOf, idOf) {
|
|
13455
|
+
const labels = items.map((item) => labelOf(item));
|
|
13456
|
+
const counts = /* @__PURE__ */ new Map();
|
|
13457
|
+
for (const label of labels) counts.set(label, (counts.get(label) ?? 0) + 1);
|
|
13458
|
+
return labels.map((label, index) => {
|
|
13459
|
+
if ((counts.get(label) ?? 0) < 2) return label;
|
|
13460
|
+
const item = items[index];
|
|
13461
|
+
const id = item === void 0 ? "" : idOf(item).replace(/[^A-Za-z0-9]/gu, "").slice(-8);
|
|
13462
|
+
return id === "" ? label : `${label}#${id}`;
|
|
13463
|
+
});
|
|
13464
|
+
}
|
|
12685
13465
|
/** Build the pool the Trae and Qoder gateways share, wired to the host's readers. */
|
|
12686
13466
|
function createSourcePool(host) {
|
|
12687
13467
|
/**
|
|
@@ -12985,6 +13765,13 @@ const PLACEHOLDER_BASE_URL = "http://127.0.0.1:1/v1";
|
|
|
12985
13765
|
* paint, so opening it never waits on a billing call per account.
|
|
12986
13766
|
*/
|
|
12987
13767
|
const PROBE_TTL_MS = 6e4;
|
|
13768
|
+
/**
|
|
13769
|
+
* How long a failed billing read is reused.
|
|
13770
|
+
*
|
|
13771
|
+
* A failure at startup is usually transient, and holding it for the full cache
|
|
13772
|
+
* window left the readout on "—" until the next TTL or a manual refresh.
|
|
13773
|
+
*/
|
|
13774
|
+
const PROBE_RETRY_MS = 15e3;
|
|
12988
13775
|
/** How often the desktop sign-ins are re-read while the service runs. */
|
|
12989
13776
|
const ACCOUNT_WATCH_MS = 6e4;
|
|
12990
13777
|
/**
|
|
@@ -13016,6 +13803,8 @@ const SERVICE_ROSTER_BUDGET_MS = 3e3;
|
|
|
13016
13803
|
*/
|
|
13017
13804
|
const LOAD_BOOTSTRAP_BUDGET_MS = 5e3;
|
|
13018
13805
|
const SERVICE_BOOTSTRAP_BUDGET_MS = 1500;
|
|
13806
|
+
/** Bound on closing source gateways when the whole plugin is switched off. */
|
|
13807
|
+
const DISABLED_CLOSE_BUDGET_MS = 2e3;
|
|
13019
13808
|
/**
|
|
13020
13809
|
* One whole refresh pass, and how long a forced one waits for the pass it found
|
|
13021
13810
|
* already running.
|
|
@@ -13134,6 +13923,14 @@ let refreshTimer = null;
|
|
|
13134
13923
|
/** The background refresh in flight, so two passes never overlap. */
|
|
13135
13924
|
let refreshInflight = null;
|
|
13136
13925
|
/**
|
|
13926
|
+
* A refresh asked for while a pass held the slot.
|
|
13927
|
+
*
|
|
13928
|
+
* The pass that is running read settings and rosters when it started, so a
|
|
13929
|
+
* change that arrived after that point was otherwise dropped and took effect on
|
|
13930
|
+
* the next unrelated pass. One follow-up consumes every such request.
|
|
13931
|
+
*/
|
|
13932
|
+
let refreshPending = false;
|
|
13933
|
+
/**
|
|
13137
13934
|
* Counts activations, so a pass from a retired one can tell that it is stale.
|
|
13138
13935
|
*
|
|
13139
13936
|
* A refresh suspends at network awaits, and unload cannot wait for it. Without
|
|
@@ -13200,6 +13997,11 @@ let growthLast = null;
|
|
|
13200
13997
|
* instead of asking the user to read again; the panel refreshes it when it is old.
|
|
13201
13998
|
*/
|
|
13202
13999
|
let growthRead = null;
|
|
14000
|
+
/** Drop the held growth read; the epoch rejects one that lands after this. */
|
|
14001
|
+
function invalidateGrowthRead() {
|
|
14002
|
+
growthRead = null;
|
|
14003
|
+
growthReadEpoch += 1;
|
|
14004
|
+
}
|
|
13203
14005
|
/**
|
|
13204
14006
|
* Which read the answer in flight belongs to.
|
|
13205
14007
|
*
|
|
@@ -13487,6 +14289,8 @@ async function applySettingsEverywhere(toggled) {
|
|
|
13487
14289
|
const generation = loadGeneration;
|
|
13488
14290
|
if (!stillLoaded(generation)) return false;
|
|
13489
14291
|
applySettingsToCore();
|
|
14292
|
+
if (settings.enabled === false) stopAccountWatch();
|
|
14293
|
+
else startAccountWatch();
|
|
13490
14294
|
traeAccountRows = traeAccountRows.filter((row) => regionEnabled("trae", row.region));
|
|
13491
14295
|
qoderAccountRows = qoderAccountRows.filter((row) => regionEnabled("qoder", row.region));
|
|
13492
14296
|
await reconcileTraeGateways();
|
|
@@ -13495,10 +14299,7 @@ async function applySettingsEverywhere(toggled) {
|
|
|
13495
14299
|
if (!stillLoaded(generation)) return false;
|
|
13496
14300
|
syncDeclaration();
|
|
13497
14301
|
if (toggled) probeCache.clear();
|
|
13498
|
-
if (toggled)
|
|
13499
|
-
growthRead = null;
|
|
13500
|
-
growthReadEpoch += 1;
|
|
13501
|
-
}
|
|
14302
|
+
if (toggled) invalidateGrowthRead();
|
|
13502
14303
|
scheduleRefresh("settings");
|
|
13503
14304
|
return true;
|
|
13504
14305
|
}
|
|
@@ -13717,8 +14518,8 @@ function creditReserves() {
|
|
|
13717
14518
|
return clean;
|
|
13718
14519
|
}
|
|
13719
14520
|
/**
|
|
13720
|
-
* Accounts removed from the pool
|
|
13721
|
-
*
|
|
14521
|
+
* Accounts removed from the pool, with label and gateway kept so the panel can
|
|
14522
|
+
* still offer a restore after the account stops being scanned.
|
|
13722
14523
|
*/
|
|
13723
14524
|
function removedAccounts() {
|
|
13724
14525
|
const value = settings.removedAccounts;
|
|
@@ -13807,6 +14608,7 @@ function sourceModelView(key, model, native) {
|
|
|
13807
14608
|
nativeSupportsImages: native.supportsImages,
|
|
13808
14609
|
...Array.isArray(native.thinkingLevels) && native.thinkingLevels.length > 0 ? { nativeThinkingLevels: [...native.thinkingLevels] } : {},
|
|
13809
14610
|
...Array.isArray(override.thinkingLevels) && override.thinkingLevels.length > 0 ? { thinkingLevels: override.thinkingLevels } : {},
|
|
14611
|
+
...typeof override.defaultThinkingLevel === "string" && override.defaultThinkingLevel !== "" ? { defaultThinkingLevel: override.defaultThinkingLevel } : {},
|
|
13810
14612
|
...native.multiplier === void 0 ? {} : { multiplier: native.multiplier },
|
|
13811
14613
|
advanced: Object.keys(override).length > 0
|
|
13812
14614
|
};
|
|
@@ -14351,12 +15153,15 @@ async function warmSourceDirectory(entry, signal) {
|
|
|
14351
15153
|
if (signal) bounds.push(signal);
|
|
14352
15154
|
try {
|
|
14353
15155
|
const warm = await fetch(entry.baseUrl + "/v1/models", { signal: bounds.length === 1 ? bounds[0] : AbortSignal.any(bounds) });
|
|
15156
|
+
if (warm.body !== null) await warm.body.cancel().catch(() => {});
|
|
15157
|
+
if (signal && signal.aborted) return false;
|
|
14354
15158
|
entry.lastError = warm.ok ? "" : "directory answered HTTP " + warm.status;
|
|
14355
15159
|
} catch (error) {
|
|
14356
15160
|
if (signal && signal.aborted) return false;
|
|
14357
15161
|
entry.lastError = entry.gateway.models().length > 0 ? "" : "directory unavailable: " + safeMessage(error);
|
|
14358
15162
|
if (entry.lastError !== "") log("warn", "[" + entry.source + ":" + entry.region + "] " + entry.lastError);
|
|
14359
15163
|
}
|
|
15164
|
+
if (signal && signal.aborted) return false;
|
|
14360
15165
|
entry.warmSettled = true;
|
|
14361
15166
|
if (entry.lastError === "" && entry.gateway.models().length > 0) syncDeclaration();
|
|
14362
15167
|
return entry.lastError === "";
|
|
@@ -14548,14 +15353,17 @@ function traeDeclarationModels(region) {
|
|
|
14548
15353
|
const advertised = native.thinkingLevels;
|
|
14549
15354
|
const edited = Array.isArray(view.thinkingLevels) && view.thinkingLevels.length > 0 ? view.thinkingLevels.filter((level) => HOST_THINKING_LEVELS.includes(level)) : [];
|
|
14550
15355
|
const levels = edited.length > 0 ? HOST_THINKING_LEVELS.filter((level) => edited.includes(level)) : advertised;
|
|
14551
|
-
|
|
15356
|
+
const published = offersThinking(levels) ? levels : ["off"];
|
|
15357
|
+
const entry = {
|
|
14552
15358
|
id: view.id,
|
|
14553
15359
|
name: modelLabel(view),
|
|
14554
15360
|
contextWindow: view.contextWindow,
|
|
14555
15361
|
maxTokens: view.maxOutputTokens,
|
|
14556
15362
|
supportsImages: view.supportsImages,
|
|
14557
|
-
thinkingLevels:
|
|
14558
|
-
}
|
|
15363
|
+
thinkingLevels: published
|
|
15364
|
+
};
|
|
15365
|
+
if (typeof view.defaultThinkingLevel === "string" && published.includes(view.defaultThinkingLevel)) entry.defaultThinkingLevel = view.defaultThinkingLevel;
|
|
15366
|
+
models.push(entry);
|
|
14559
15367
|
if (models.length >= MAX_MODELS) break;
|
|
14560
15368
|
}
|
|
14561
15369
|
return models;
|
|
@@ -14602,7 +15410,7 @@ async function createQoderRuntime(region) {
|
|
|
14602
15410
|
},
|
|
14603
15411
|
listedIds: () => qoderDeclarationModels(region).map((model) => model.id),
|
|
14604
15412
|
onAttemptFailure: (credential, outcome) => {
|
|
14605
|
-
const label = typeof credential.name === "string" && credential.name !== "" ? credential.name : credential.userID;
|
|
15413
|
+
const label = qoderLabelOverrides.get(qoderLabelKey(credential, region)) ?? (typeof credential.name === "string" && credential.name !== "" ? credential.name : credential.userID);
|
|
14606
15414
|
noteSourceFailure("qoder", region, label, outcome.retryAfter);
|
|
14607
15415
|
},
|
|
14608
15416
|
port: bound,
|
|
@@ -14718,14 +15526,17 @@ function qoderDeclarationModels(region) {
|
|
|
14718
15526
|
const levels = native.thinkingLevels;
|
|
14719
15527
|
const edited = Array.isArray(view.thinkingLevels) && view.thinkingLevels.length > 0 ? view.thinkingLevels.filter((level) => HOST_THINKING_LEVELS.includes(level)) : [];
|
|
14720
15528
|
const published = edited.length > 0 ? HOST_THINKING_LEVELS.filter((level) => edited.includes(level)) : levels;
|
|
14721
|
-
|
|
15529
|
+
const entry = {
|
|
14722
15530
|
id: view.id,
|
|
14723
15531
|
name: view.name,
|
|
14724
15532
|
contextWindow: view.contextWindow,
|
|
14725
15533
|
maxTokens: view.maxOutputTokens,
|
|
14726
15534
|
supportsImages: view.supportsImages,
|
|
14727
15535
|
thinkingLevels: offersThinking(published) ? published : ["off"]
|
|
14728
|
-
}
|
|
15536
|
+
};
|
|
15537
|
+
const levelsOffered = entry.thinkingLevels;
|
|
15538
|
+
if (typeof view.defaultThinkingLevel === "string" && levelsOffered.includes(view.defaultThinkingLevel)) entry.defaultThinkingLevel = view.defaultThinkingLevel;
|
|
15539
|
+
models.push(entry);
|
|
14729
15540
|
if (models.length >= MAX_MODELS) break;
|
|
14730
15541
|
}
|
|
14731
15542
|
return models;
|
|
@@ -14754,7 +15565,9 @@ async function createRuntime(region) {
|
|
|
14754
15565
|
connecting: null,
|
|
14755
15566
|
/** A stopped gateway cannot commit a catalog fetch that finishes later. */
|
|
14756
15567
|
retired: false,
|
|
14757
|
-
catalogFetchedAtMs: 0
|
|
15568
|
+
catalogFetchedAtMs: 0,
|
|
15569
|
+
catalogRetryAttempt: 0,
|
|
15570
|
+
catalogRetryTimer: null
|
|
14758
15571
|
};
|
|
14759
15572
|
applyEffectiveSelection(region, runtime);
|
|
14760
15573
|
const preferred = preferredPort(region);
|
|
@@ -14895,20 +15708,25 @@ function trackRuntimeStop(runtime) {
|
|
|
14895
15708
|
* pass nobody asked for.
|
|
14896
15709
|
*/
|
|
14897
15710
|
const modelRetryTimers = /* @__PURE__ */ new Set();
|
|
14898
|
-
function scheduleModelRetries(
|
|
14899
|
-
if (serviceStopped) return;
|
|
15711
|
+
function scheduleModelRetries(runtime) {
|
|
15712
|
+
if (serviceStopped || runtime.retired) return;
|
|
15713
|
+
if (runtime.catalogRetryTimer !== null || runtime.catalogRetryAttempt >= MODEL_RETRY_ATTEMPTS) return;
|
|
14900
15714
|
const timer = setTimeout(() => {
|
|
14901
15715
|
modelRetryTimers.delete(timer);
|
|
15716
|
+
runtime.catalogRetryTimer = null;
|
|
15717
|
+
if (runtime.retired || serviceStopped) return;
|
|
15718
|
+
runtime.catalogRetryAttempt += 1;
|
|
14902
15719
|
scheduleRefresh("model-list-retry");
|
|
14903
|
-
if (attempt < MODEL_RETRY_ATTEMPTS) scheduleModelRetries(attempt + 1);
|
|
14904
15720
|
}, CATALOG_RETRY_MS);
|
|
14905
15721
|
if (typeof timer.unref === "function") timer.unref();
|
|
15722
|
+
runtime.catalogRetryTimer = timer;
|
|
14906
15723
|
modelRetryTimers.add(timer);
|
|
14907
15724
|
}
|
|
14908
15725
|
/** Drop every pending roster retry. */
|
|
14909
15726
|
function clearModelRetries() {
|
|
14910
15727
|
for (const timer of modelRetryTimers) clearTimeout(timer);
|
|
14911
15728
|
modelRetryTimers.clear();
|
|
15729
|
+
for (const runtime of runtimes.values()) runtime.catalogRetryTimer = null;
|
|
14912
15730
|
}
|
|
14913
15731
|
/**
|
|
14914
15732
|
* Bring one gateway online: seed its catalog from that gateway's own account.
|
|
@@ -14930,7 +15748,7 @@ async function connect(runtime, force) {
|
|
|
14930
15748
|
runtime.lastError = "no " + runtime.region + " WorkBuddy sign-in found";
|
|
14931
15749
|
if (core.pool.status().lastScanAtMs === 0) {
|
|
14932
15750
|
runtime.catalogFetchedAtMs = Date.now() - CATALOG_TTL_MS + CATALOG_RETRY_MS;
|
|
14933
|
-
scheduleModelRetries();
|
|
15751
|
+
scheduleModelRetries(runtime);
|
|
14934
15752
|
return false;
|
|
14935
15753
|
}
|
|
14936
15754
|
runtime.catalogFetchedAtMs = Date.now();
|
|
@@ -14942,10 +15760,16 @@ async function connect(runtime, force) {
|
|
|
14942
15760
|
if (runtime.retired) return false;
|
|
14943
15761
|
runtime.catalog.update(catalogFromUpstream(models));
|
|
14944
15762
|
runtime.catalogFetchedAtMs = Date.now();
|
|
15763
|
+
runtime.catalogRetryAttempt = 0;
|
|
15764
|
+
if (runtime.catalogRetryTimer !== null) {
|
|
15765
|
+
clearTimeout(runtime.catalogRetryTimer);
|
|
15766
|
+
modelRetryTimers.delete(runtime.catalogRetryTimer);
|
|
15767
|
+
runtime.catalogRetryTimer = null;
|
|
15768
|
+
}
|
|
14945
15769
|
} catch (error) {
|
|
14946
15770
|
runtime.lastWarning = "model list failed: " + safeMessage(error);
|
|
14947
15771
|
runtime.catalogFetchedAtMs = Date.now() - CATALOG_TTL_MS + CATALOG_RETRY_MS;
|
|
14948
|
-
scheduleModelRetries();
|
|
15772
|
+
scheduleModelRetries(runtime);
|
|
14949
15773
|
}
|
|
14950
15774
|
applyEffectiveSelection(runtime.region, runtime);
|
|
14951
15775
|
if (runtime.retired) return false;
|
|
@@ -14967,7 +15791,7 @@ function catalogFresh(runtime) {
|
|
|
14967
15791
|
* keeps the poll alive until the numbers actually land.
|
|
14968
15792
|
*/
|
|
14969
15793
|
function refreshNeeded() {
|
|
14970
|
-
if (serviceStopped) return false;
|
|
15794
|
+
if (serviceStopped || settings.enabled === false) return false;
|
|
14971
15795
|
if (refreshInflight !== null) return true;
|
|
14972
15796
|
for (const runtime of runtimes.values()) {
|
|
14973
15797
|
if (runtime.connecting) return true;
|
|
@@ -14996,9 +15820,11 @@ function cooldownPending() {
|
|
|
14996
15820
|
function probesStale() {
|
|
14997
15821
|
const now = Date.now();
|
|
14998
15822
|
for (const account of core.pool.list()) {
|
|
14999
|
-
if (account.cooldownUntilMs > now || core.pool.isDisabled(account.id) || !regionEnabled("workbuddy", regionOf(account.credential.domain))) continue;
|
|
15823
|
+
if (account.cooldownUntilMs > now && core.pool.activeHold?.(account.id)?.reason !== "spent" || core.pool.isDisabled(account.id) || !regionEnabled("workbuddy", regionOf(account.credential.domain))) continue;
|
|
15000
15824
|
const cached = probeCache.get(account.id);
|
|
15001
|
-
if (cached === void 0
|
|
15825
|
+
if (cached === void 0) return true;
|
|
15826
|
+
const ttl = cached.value.creditsError !== void 0 ? PROBE_RETRY_MS : PROBE_TTL_MS;
|
|
15827
|
+
if (now - cached.atMs >= ttl) return true;
|
|
15002
15828
|
if (cached.checkinOnly === true) return true;
|
|
15003
15829
|
}
|
|
15004
15830
|
return false;
|
|
@@ -15013,10 +15839,17 @@ function probesStale() {
|
|
|
15013
15839
|
*/
|
|
15014
15840
|
function scheduleRefresh(reason) {
|
|
15015
15841
|
if (serviceStopped) return;
|
|
15016
|
-
if (refreshTimer !== null || refreshInflight !== null)
|
|
15842
|
+
if (refreshTimer !== null || refreshInflight !== null) {
|
|
15843
|
+
if (reason !== "panel") refreshPending = true;
|
|
15844
|
+
return;
|
|
15845
|
+
}
|
|
15017
15846
|
refreshTimer = setTimeout(() => {
|
|
15018
15847
|
refreshTimer = null;
|
|
15019
|
-
refreshAll(reason, false).catch((error) => log("warn", "background refresh failed: " + safeMessage(error)))
|
|
15848
|
+
refreshAll(reason, false).catch((error) => log("warn", "background refresh failed: " + safeMessage(error))).finally(() => {
|
|
15849
|
+
if (!refreshPending || serviceStopped) return;
|
|
15850
|
+
refreshPending = false;
|
|
15851
|
+
scheduleRefresh("pending");
|
|
15852
|
+
});
|
|
15020
15853
|
}, 0);
|
|
15021
15854
|
if (typeof refreshTimer.unref === "function") refreshTimer.unref();
|
|
15022
15855
|
}
|
|
@@ -15066,7 +15899,7 @@ function stalePass() {
|
|
|
15066
15899
|
* would start a pass beside it. The slot therefore outlives both.
|
|
15067
15900
|
*/
|
|
15068
15901
|
async function refreshAll(reason, force) {
|
|
15069
|
-
if (serviceStopped) return await statusDocument({ allowNetwork: false });
|
|
15902
|
+
if (serviceStopped || settings.enabled === false) return await statusDocument({ allowNetwork: false });
|
|
15070
15903
|
const wantForce = force === true;
|
|
15071
15904
|
let previous = null;
|
|
15072
15905
|
if (refreshInflight !== null) {
|
|
@@ -15113,7 +15946,7 @@ async function refreshAll(reason, force) {
|
|
|
15113
15946
|
if (!stillLoaded(generation)) return stalePass();
|
|
15114
15947
|
await settle(reconcileQoderGateways());
|
|
15115
15948
|
if (!stillLoaded(generation)) return stalePass();
|
|
15116
|
-
await settle(refreshSourceAccounts());
|
|
15949
|
+
await settle(refreshSourceAccounts(generation));
|
|
15117
15950
|
if (!stillLoaded(generation)) return stalePass();
|
|
15118
15951
|
await settle(Promise.all([...Array.from(traeGateways.values()), ...Array.from(qoderGateways.values())].filter((entry) => entry.lastError !== "" || entry.gateway.models().length === 0).map((entry) => warmSourceDirectory(entry))));
|
|
15119
15952
|
if (!stillLoaded(generation)) return stalePass();
|
|
@@ -15139,6 +15972,10 @@ async function refreshAll(reason, force) {
|
|
|
15139
15972
|
} finally {
|
|
15140
15973
|
if (previous !== null) await previous;
|
|
15141
15974
|
if (refreshInflight === entry) refreshInflight = null;
|
|
15975
|
+
if (refreshPending && !serviceStopped && stillLoaded(generation)) {
|
|
15976
|
+
refreshPending = false;
|
|
15977
|
+
scheduleRefresh("pending");
|
|
15978
|
+
}
|
|
15142
15979
|
}
|
|
15143
15980
|
})();
|
|
15144
15981
|
refreshInflight = entry;
|
|
@@ -15151,16 +15988,26 @@ async function refreshAll(reason, force) {
|
|
|
15151
15988
|
*/
|
|
15152
15989
|
let traeAccountRows = [];
|
|
15153
15990
|
let qoderAccountRows = [];
|
|
15991
|
+
/** Disambiguated labels by `region + "\n" + user id`, so a gateway that only has
|
|
15992
|
+
* a credential names the same row the panel does. Reset on every activation. */
|
|
15993
|
+
let traeLabelOverrides = /* @__PURE__ */ new Map();
|
|
15994
|
+
let qoderLabelOverrides = /* @__PURE__ */ new Map();
|
|
15154
15995
|
/**
|
|
15155
|
-
* The name one Trae account is listed
|
|
15996
|
+
* The name one Trae account is listed under before duplicates are resolved.
|
|
15156
15997
|
*
|
|
15157
15998
|
* A Trae-generated `username` can read as a stranger (`zhang san xyz`), so the
|
|
15158
|
-
* email is what the panel shows next to it; the label
|
|
15159
|
-
*
|
|
15999
|
+
* email is what the panel shows next to it; the base label stays the app's own
|
|
16000
|
+
* name so it matches what the desktop app displays.
|
|
15160
16001
|
*/
|
|
15161
|
-
function
|
|
16002
|
+
function traeBaseLabel(credential) {
|
|
15162
16003
|
return credential.accountName ?? credential.email ?? credential.userId;
|
|
15163
16004
|
}
|
|
16005
|
+
function traeLabelKey(credential) {
|
|
16006
|
+
return traeRegionOfCredential(credential) + "\n" + (credential.userId || credential.accountName || credential.email || "");
|
|
16007
|
+
}
|
|
16008
|
+
function traeRowLabel(credential) {
|
|
16009
|
+
return traeLabelOverrides.get(traeLabelKey(credential)) ?? traeBaseLabel(credential);
|
|
16010
|
+
}
|
|
15164
16011
|
/**
|
|
15165
16012
|
* The app one Trae sign-in came from.
|
|
15166
16013
|
*
|
|
@@ -15173,10 +16020,16 @@ function traeAppLabelOf(credential) {
|
|
|
15173
16020
|
if (credential.source === "cli") return "Trae CLI";
|
|
15174
16021
|
return traeWindowsAppNames(credential.edition)[0];
|
|
15175
16022
|
}
|
|
15176
|
-
/** The name one Qoder account is listed
|
|
15177
|
-
function
|
|
16023
|
+
/** The name one Qoder account is listed under before duplicates are resolved. */
|
|
16024
|
+
function qoderBaseLabel(credential) {
|
|
15178
16025
|
return credential.name === "" ? credential.userID : credential.name;
|
|
15179
16026
|
}
|
|
16027
|
+
function qoderLabelKey(credential, region) {
|
|
16028
|
+
return (region ?? credential.region) + "\n" + (credential.userID || credential.name || "");
|
|
16029
|
+
}
|
|
16030
|
+
function qoderRowLabel(credential, region) {
|
|
16031
|
+
return qoderLabelOverrides.get(qoderLabelKey(credential, region)) ?? qoderBaseLabel(credential);
|
|
16032
|
+
}
|
|
15180
16033
|
/**
|
|
15181
16034
|
* How long the Trae and Qoder sign-in lists may take in total.
|
|
15182
16035
|
*
|
|
@@ -15306,17 +16159,25 @@ async function qoderRowProbe(credential, previous) {
|
|
|
15306
16159
|
return previous;
|
|
15307
16160
|
}
|
|
15308
16161
|
}
|
|
15309
|
-
async function refreshSourceAccounts() {
|
|
16162
|
+
async function refreshSourceAccounts(generation) {
|
|
15310
16163
|
const now = Date.now();
|
|
15311
16164
|
try {
|
|
15312
16165
|
const regions = activeTraeRegions();
|
|
15313
16166
|
if (regions.length > 0) {
|
|
15314
16167
|
const found = await discoverTraeCredentials();
|
|
15315
|
-
|
|
15316
|
-
|
|
16168
|
+
const allLabels = uniqueSourceLabels(found.credentials, traeBaseLabel, (credential) => credential.userId);
|
|
16169
|
+
const overrides = /* @__PURE__ */ new Map();
|
|
16170
|
+
found.credentials.forEach((credential, index) => {
|
|
16171
|
+
overrides.set(traeLabelKey(credential), allLabels[index]);
|
|
16172
|
+
});
|
|
16173
|
+
const wanted = found.credentials.map((credential, index) => ({
|
|
16174
|
+
credential,
|
|
16175
|
+
label: allLabels[index]
|
|
16176
|
+
})).filter(({ credential }) => regions.includes(traeRegionOfCredential(credential)));
|
|
16177
|
+
const rows = await Promise.all(wanted.map(async ({ credential, label }) => {
|
|
15317
16178
|
const region = traeRegionOfCredential(credential);
|
|
15318
16179
|
const probe = await traeRowProbe(credential, traeAccountRows.find((row) => row.label === label && row.region === region));
|
|
15319
|
-
noteSourceExpiry("trae", region, label, probe?.usage?.credits?.packages);
|
|
16180
|
+
if (generation === loadGeneration) noteSourceExpiry("trae", region, label, probe?.usage?.credits?.packages);
|
|
15320
16181
|
return {
|
|
15321
16182
|
label,
|
|
15322
16183
|
region,
|
|
@@ -15327,8 +16188,14 @@ async function refreshSourceAccounts() {
|
|
|
15327
16188
|
...probe?.checkin ? { checkin: probe.checkin } : {},
|
|
15328
16189
|
...probe?.usage ? { usage: probe.usage } : {}
|
|
15329
16190
|
};
|
|
15330
|
-
}))
|
|
15331
|
-
|
|
16191
|
+
}));
|
|
16192
|
+
if (generation !== loadGeneration) return;
|
|
16193
|
+
traeLabelOverrides = overrides;
|
|
16194
|
+
traeAccountRows = rows.filter((row) => regionEnabled("trae", row.region));
|
|
16195
|
+
} else {
|
|
16196
|
+
if (generation !== loadGeneration) return;
|
|
16197
|
+
traeAccountRows = [];
|
|
16198
|
+
}
|
|
15332
16199
|
} catch (error) {
|
|
15333
16200
|
log("warn", "listing Trae sign-ins failed: " + safeMessage(error));
|
|
15334
16201
|
}
|
|
@@ -15336,16 +16203,26 @@ async function refreshSourceAccounts() {
|
|
|
15336
16203
|
const regions = activeQoderRegions();
|
|
15337
16204
|
if (regions.length > 0) {
|
|
15338
16205
|
const found = await discoverQoderCredentials();
|
|
16206
|
+
if (generation !== loadGeneration) return;
|
|
16207
|
+
const allLabels = uniqueSourceLabels(found.credentials, qoderBaseLabel, (credential) => credential.userID);
|
|
16208
|
+
const overrides = /* @__PURE__ */ new Map();
|
|
16209
|
+
found.credentials.forEach((credential, index) => {
|
|
16210
|
+
overrides.set(qoderLabelKey(credential), allLabels[index]);
|
|
16211
|
+
});
|
|
16212
|
+
const wanted = found.credentials.map((credential, index) => ({
|
|
16213
|
+
credential,
|
|
16214
|
+
label: allLabels[index]
|
|
16215
|
+
})).filter(({ credential }) => regions.includes(credential.region));
|
|
15339
16216
|
const rows = [];
|
|
15340
16217
|
const { left } = budget(SOURCE_ACCOUNTS_BUDGET_MS);
|
|
15341
16218
|
let seen = 0;
|
|
15342
|
-
for (const credential of
|
|
15343
|
-
if (
|
|
16219
|
+
for (const { credential, label } of wanted) {
|
|
16220
|
+
if (generation !== loadGeneration) return;
|
|
15344
16221
|
if (seen > 0) await delay(QODER_ACCOUNT_DELAY_MS);
|
|
15345
16222
|
seen += 1;
|
|
15346
|
-
const label = qoderRowLabel(credential);
|
|
15347
16223
|
const previous = qoderAccountRows.find((row) => row.label === label && row.region === credential.region);
|
|
15348
16224
|
const probe = left() > 0 ? await qoderRowProbe(credential, previous) : previous;
|
|
16225
|
+
if (generation !== loadGeneration) return;
|
|
15349
16226
|
noteSourceExpiry("qoder", credential.region, label, probe?.usage?.credits?.packages);
|
|
15350
16227
|
rows.push({
|
|
15351
16228
|
label,
|
|
@@ -15356,11 +16233,17 @@ async function refreshSourceAccounts() {
|
|
|
15356
16233
|
...probe?.usage ? { usage: probe.usage } : {}
|
|
15357
16234
|
});
|
|
15358
16235
|
}
|
|
16236
|
+
if (generation !== loadGeneration) return;
|
|
16237
|
+
qoderLabelOverrides = overrides;
|
|
15359
16238
|
qoderAccountRows = rows.filter((row) => regionEnabled("qoder", row.region));
|
|
15360
|
-
} else
|
|
16239
|
+
} else {
|
|
16240
|
+
if (generation !== loadGeneration) return;
|
|
16241
|
+
qoderAccountRows = [];
|
|
16242
|
+
}
|
|
15361
16243
|
} catch (error) {
|
|
15362
16244
|
log("warn", "listing Qoder sign-ins failed: " + safeMessage(error));
|
|
15363
16245
|
}
|
|
16246
|
+
if (generation !== loadGeneration) return;
|
|
15364
16247
|
pruneSourceState();
|
|
15365
16248
|
}
|
|
15366
16249
|
/**
|
|
@@ -15480,7 +16363,7 @@ function applyProbe(row, probe) {
|
|
|
15480
16363
|
async function accountProbe(account, allowNetwork, checkinOnly = false) {
|
|
15481
16364
|
const cached = probeCache.get(account.id);
|
|
15482
16365
|
if (allowNetwork !== true) return cached === void 0 ? { pending: true } : cached.value;
|
|
15483
|
-
const cachedFresh = cached !== void 0 && Date.now() - cached.atMs < PROBE_TTL_MS;
|
|
16366
|
+
const cachedFresh = cached !== void 0 && Date.now() - cached.atMs < (cached.value.creditsError !== void 0 ? PROBE_RETRY_MS : PROBE_TTL_MS);
|
|
15484
16367
|
const cachedHasCredits = cached !== void 0 && (cached.value.credits !== void 0 || cached.value.creditsError !== void 0);
|
|
15485
16368
|
if (cachedFresh && (checkinOnly || cached.checkinOnly !== true && cachedHasCredits)) return cached.value;
|
|
15486
16369
|
if (signInRefused(account)) return {};
|
|
@@ -15612,7 +16495,6 @@ async function statusDocument(options) {
|
|
|
15612
16495
|
const allowNetwork = options?.allowNetwork === true;
|
|
15613
16496
|
const probeBudget = typeof options?.budgetMs === "number" ? Math.min(PROBE_BATCH_BUDGET_MS, Math.max(0, options.budgetMs)) : PROBE_BATCH_BUDGET_MS;
|
|
15614
16497
|
const now = Date.now();
|
|
15615
|
-
const reserves = creditReserves();
|
|
15616
16498
|
const campaignClock = zonedDayHour(new Date(now));
|
|
15617
16499
|
const accounts = [];
|
|
15618
16500
|
const models = [];
|
|
@@ -15625,14 +16507,15 @@ async function statusDocument(options) {
|
|
|
15625
16507
|
/** Row -> the credits/check-in read that row is still waiting on. */
|
|
15626
16508
|
const pendingProbes = [];
|
|
15627
16509
|
for (const region of activeRegions()) {
|
|
15628
|
-
preferredAccountIds[region] =
|
|
16510
|
+
preferredAccountIds[region] = core.pool.resolvedPreferredId(region) || "";
|
|
15629
16511
|
selectionByRegion[region] = effectiveSelectionFor(region, runtimes.get(region)?.catalog);
|
|
15630
16512
|
if (selectionStale(region, runtimes.get(region)?.catalog)) staleSelectionRegions.push(region);
|
|
15631
16513
|
const available = core.pool.available(now, void 0, region);
|
|
15632
|
-
const nextUp = available.find((account) => account.id === preferredAccountIds[region]) ?? core.pool.preview(available);
|
|
16514
|
+
const nextUp = available.find((account) => account.id === preferredAccountIds[region]) ?? core.pool.preview(available, region);
|
|
15633
16515
|
runtimeNextUpId.set(region, nextUp === void 0 ? void 0 : nextUp.id);
|
|
15634
16516
|
for (const account of core.pool.list(region)) {
|
|
15635
16517
|
const cooling = account.cooldownUntilMs > now;
|
|
16518
|
+
const creditReserve = core.pool.creditReserve(account.id);
|
|
15636
16519
|
const modelCooldowns = Object.entries(account.modelCooldowns).filter(([, until]) => until > now).sort((a, b) => a[1] - b[1]).map(([modelId, until]) => ({
|
|
15637
16520
|
modelId,
|
|
15638
16521
|
until: new Date(until).toISOString()
|
|
@@ -15648,14 +16531,14 @@ async function statusDocument(options) {
|
|
|
15648
16531
|
rateLimitHits: account.rateLimitHits,
|
|
15649
16532
|
...activeHoldOf(account.id, now),
|
|
15650
16533
|
active: runtimeNextUpId.get(region) === account.id,
|
|
15651
|
-
preferred: preferredAccountIds[region] === account.id,
|
|
16534
|
+
preferred: preferredAccountIds[region] === account.id || core.pool.storedIds(account.id).includes(preferredAccountIds[region]),
|
|
15652
16535
|
disabled: core.pool.isDisabled(account.id),
|
|
15653
16536
|
...account.archived === true ? { archived: true } : {},
|
|
15654
16537
|
...core.pool.ownsSignIn(account.id) ? { own: true } : {},
|
|
15655
|
-
...
|
|
16538
|
+
...creditReserve === void 0 ? {} : { creditReserve }
|
|
15656
16539
|
};
|
|
15657
16540
|
if (!core.pool.isDisabled(account.id)) {
|
|
15658
|
-
const checkinOnly = cooling;
|
|
16541
|
+
const checkinOnly = cooling && core.pool.activeHold?.(account.id)?.reason !== "spent";
|
|
15659
16542
|
pendingProbes.push(accountProbe(account, allowNetwork, checkinOnly).then((probe) => {
|
|
15660
16543
|
applyProbe(row, probe);
|
|
15661
16544
|
}));
|
|
@@ -15694,7 +16577,29 @@ async function statusDocument(options) {
|
|
|
15694
16577
|
}
|
|
15695
16578
|
}
|
|
15696
16579
|
}
|
|
15697
|
-
if (pendingProbes.length > 0)
|
|
16580
|
+
if (pendingProbes.length > 0) {
|
|
16581
|
+
await settleWithin(Promise.all(pendingProbes).catch(() => {}), probeBudget);
|
|
16582
|
+
const liveAccounts = new Map(core.pool.list().map((account) => [account.id, account]));
|
|
16583
|
+
const heldNow = Date.now();
|
|
16584
|
+
const nextUpNow = /* @__PURE__ */ new Map();
|
|
16585
|
+
for (const region of activeRegions()) {
|
|
16586
|
+
const available = core.pool.available(heldNow, void 0, region);
|
|
16587
|
+
const preferredNow = available.find((account) => account.id === preferredAccountIds[region]);
|
|
16588
|
+
nextUpNow.set(region, (preferredNow ?? core.pool.preview(available, region))?.id);
|
|
16589
|
+
}
|
|
16590
|
+
for (const row of accounts) {
|
|
16591
|
+
const account = liveAccounts.get(row.id);
|
|
16592
|
+
if (account === void 0) continue;
|
|
16593
|
+
const cooling = account.cooldownUntilMs > heldNow;
|
|
16594
|
+
row.cooling = cooling;
|
|
16595
|
+
if (cooling) row.cooldownUntil = new Date(account.cooldownUntilMs).toISOString();
|
|
16596
|
+
else delete row.cooldownUntil;
|
|
16597
|
+
const hold = core.pool.activeHold(account.id);
|
|
16598
|
+
if (hold === null || hold.untilMs <= heldNow) delete row.hold;
|
|
16599
|
+
else row.hold = hold.reason;
|
|
16600
|
+
row.active = nextUpNow.get(row.region) === row.id;
|
|
16601
|
+
}
|
|
16602
|
+
}
|
|
15698
16603
|
return {
|
|
15699
16604
|
regions: activeRegions(),
|
|
15700
16605
|
sources: {
|
|
@@ -16071,7 +16976,10 @@ async function doctorReport() {
|
|
|
16071
16976
|
""
|
|
16072
16977
|
].join("\n");
|
|
16073
16978
|
if (dataPath !== "") try {
|
|
16074
|
-
fs$1.writeFileSync(path.join(dataPath, "doctor.txt"), report,
|
|
16979
|
+
fs$1.writeFileSync(path.join(dataPath, "doctor.txt"), report, {
|
|
16980
|
+
encoding: "utf8",
|
|
16981
|
+
mode: 384
|
|
16982
|
+
});
|
|
16075
16983
|
} catch {}
|
|
16076
16984
|
return {
|
|
16077
16985
|
ok,
|
|
@@ -16312,13 +17220,13 @@ async function onPanelInvoke(channel, payload) {
|
|
|
16312
17220
|
* gateway's roster is refetched even inside its refresh window.
|
|
16313
17221
|
*/
|
|
16314
17222
|
case "xd.rescan": {
|
|
17223
|
+
const generation = loadGeneration;
|
|
16315
17224
|
const { left, settle } = budget(RESCAN_BUDGET_MS);
|
|
16316
17225
|
resetDiscoveryCaches();
|
|
16317
|
-
|
|
16318
|
-
growthReadEpoch += 1;
|
|
17226
|
+
invalidateGrowthRead();
|
|
16319
17227
|
if (activeRegions().length > 0) await settle(core.pool.rescan());
|
|
16320
17228
|
await settle(startRuntimes());
|
|
16321
|
-
await settle(refreshSourceAccounts());
|
|
17229
|
+
await settle(refreshSourceAccounts(generation));
|
|
16322
17230
|
await settle(reconcileTraeGateways());
|
|
16323
17231
|
await settle(reconcileQoderGateways());
|
|
16324
17232
|
await settle(Promise.all([...Array.from(traeGateways.values()), ...Array.from(qoderGateways.values())].filter((entry) => entry.lastError !== "" || entry.gateway.models().length === 0).map((entry) => warmSourceDirectory(entry))));
|
|
@@ -16439,25 +17347,27 @@ async function onPanelInvoke(channel, payload) {
|
|
|
16439
17347
|
"deleted"
|
|
16440
17348
|
].includes(state)) throw panelError("region, accountId and a known state are required", "INVALID");
|
|
16441
17349
|
const account = core.pool.list(region).find((item) => item.id === accountId);
|
|
16442
|
-
const
|
|
17350
|
+
const ids = core.pool.storedIds(accountId);
|
|
17351
|
+
const stored = removedAccounts().find((entry) => ids.includes(entry.id));
|
|
16443
17352
|
if (state !== "restored" && account === void 0 && stored === void 0) throw panelError("account does not belong to this gateway", "NOT_FOUND");
|
|
16444
17353
|
if (state === "deleted") {
|
|
16445
17354
|
if (!core.pool.ownsSignIn(accountId)) throw panelError("only an account added from this panel can be deleted", "NOT_OWN");
|
|
16446
17355
|
if (await core.pool.removeOwnSignIn(accountId) !== true) throw panelError("the sign-in file could not be removed", "DELETE_FAILED");
|
|
16447
|
-
|
|
17356
|
+
probeCache.delete(accountId);
|
|
17357
|
+
await settleWithin(core.pool.rescan(), POOL_SCAN_BUDGET_MS);
|
|
16448
17358
|
if (await mutateSettings("disabledAccountIds", (current) => {
|
|
16449
|
-
return (Array.isArray(current) ? current.filter((id) => typeof id === "string") : []).filter((id) => id
|
|
17359
|
+
return (Array.isArray(current) ? current.filter((id) => typeof id === "string") : []).filter((id) => !ids.includes(id));
|
|
16450
17360
|
}) !== true) log("warn", "could not clear the disabled state for " + accountId);
|
|
16451
17361
|
if (await mutateSettings("creditReserves", (current) => {
|
|
16452
17362
|
const next = { ...current && typeof current === "object" && !Array.isArray(current) ? current : {} };
|
|
16453
|
-
delete next[
|
|
17363
|
+
for (const id of ids) delete next[id];
|
|
16454
17364
|
return next;
|
|
16455
17365
|
}) !== true) log("warn", "could not clear the credit reserve for " + accountId);
|
|
16456
17366
|
core.pool.applyConfig({
|
|
16457
17367
|
disabledAccountIds: disabledAccountIds(),
|
|
16458
17368
|
creditReserves: creditReserves()
|
|
16459
17369
|
});
|
|
16460
|
-
if (preferredAccountId(region)
|
|
17370
|
+
if (ids.includes(preferredAccountId(region))) {
|
|
16461
17371
|
if (await mutateSettings("preferredAccountIds", (current) => {
|
|
16462
17372
|
const next = { ...current && typeof current === "object" && !Array.isArray(current) ? current : {} };
|
|
16463
17373
|
delete next[region];
|
|
@@ -16465,17 +17375,16 @@ async function onPanelInvoke(channel, payload) {
|
|
|
16465
17375
|
}) !== true) log("warn", "could not clear the preferred account for " + region);
|
|
16466
17376
|
core.pool.prefer(void 0, region);
|
|
16467
17377
|
}
|
|
16468
|
-
|
|
16469
|
-
growthReadEpoch += 1;
|
|
17378
|
+
invalidateGrowthRead();
|
|
16470
17379
|
return await statusDocument({ allowNetwork: false });
|
|
16471
17380
|
}
|
|
16472
17381
|
if (await mutateSettings("disabledAccountIds", (current) => {
|
|
16473
|
-
const kept = (Array.isArray(current) ? current.filter((id) => typeof id === "string") : []).filter((id) => id
|
|
17382
|
+
const kept = (Array.isArray(current) ? current.filter((id) => typeof id === "string") : []).filter((id) => !ids.includes(id));
|
|
16474
17383
|
if (state === "disabled") kept.push(accountId);
|
|
16475
17384
|
return kept;
|
|
16476
17385
|
}) !== true) throw panelError("the account setting could not be saved", "SAVE_FAILED");
|
|
16477
17386
|
if (await mutateSettings("removedAccounts", (current) => {
|
|
16478
|
-
const kept = (Array.isArray(current) ? current.filter((entry) => entry !== null && typeof entry === "object" && !Array.isArray(entry)) : []).filter((entry) => entry.id
|
|
17387
|
+
const kept = (Array.isArray(current) ? current.filter((entry) => entry !== null && typeof entry === "object" && !Array.isArray(entry)) : []).filter((entry) => !ids.includes(entry.id));
|
|
16479
17388
|
if (state === "removed") kept.push({
|
|
16480
17389
|
id: accountId,
|
|
16481
17390
|
label: account === void 0 ? stored.label : account.label,
|
|
@@ -16487,7 +17396,7 @@ async function onPanelInvoke(channel, payload) {
|
|
|
16487
17396
|
const removed = removedAccounts();
|
|
16488
17397
|
core.pool.applyConfig({ disabledAccountIds: disabled });
|
|
16489
17398
|
core.pool.applyRemoved(removed.map((entry) => entry.id));
|
|
16490
|
-
if (accountId !== "" && preferredAccountId(region)
|
|
17399
|
+
if (accountId !== "" && ids.includes(preferredAccountId(region)) && state !== "enabled" && state !== "restored") {
|
|
16491
17400
|
if (await mutateSettings("preferredAccountIds", (current) => {
|
|
16492
17401
|
const next = { ...current && typeof current === "object" && !Array.isArray(current) ? current : {} };
|
|
16493
17402
|
delete next[region];
|
|
@@ -16495,8 +17404,7 @@ async function onPanelInvoke(channel, payload) {
|
|
|
16495
17404
|
}) !== true) log("warn", "could not clear the preferred account for " + region);
|
|
16496
17405
|
core.pool.prefer(void 0, region);
|
|
16497
17406
|
}
|
|
16498
|
-
|
|
16499
|
-
growthReadEpoch += 1;
|
|
17407
|
+
invalidateGrowthRead();
|
|
16500
17408
|
return await statusDocument({ allowNetwork: false });
|
|
16501
17409
|
}
|
|
16502
17410
|
/** The credits to keep back on one account: 0 clears the floor. */
|
|
@@ -16509,8 +17417,8 @@ async function onPanelInvoke(channel, payload) {
|
|
|
16509
17417
|
if (!core.pool.list(region).some((account) => account.id === accountId)) throw panelError("account does not belong to this gateway", "NOT_FOUND");
|
|
16510
17418
|
if (await mutateSettings("creditReserves", (current) => {
|
|
16511
17419
|
const next = { ...current && typeof current === "object" && !Array.isArray(current) ? current : {} };
|
|
16512
|
-
|
|
16513
|
-
|
|
17420
|
+
for (const id of core.pool.storedIds(accountId)) delete next[id];
|
|
17421
|
+
if (reserve !== 0) next[accountId] = Math.floor(reserve);
|
|
16514
17422
|
return next;
|
|
16515
17423
|
}) !== true) throw panelError("the reserve could not be saved", "SAVE_FAILED");
|
|
16516
17424
|
core.pool.applyConfig({ creditReserves: creditReserves() });
|
|
@@ -16702,10 +17610,7 @@ async function onPanelInvoke(channel, payload) {
|
|
|
16702
17610
|
await applySettingsEverywhere(false);
|
|
16703
17611
|
if (payload?.enabled === true) await refreshAll("region-on", true);
|
|
16704
17612
|
else syncDeclaration();
|
|
16705
|
-
if (source === "workbuddy")
|
|
16706
|
-
growthRead = null;
|
|
16707
|
-
growthReadEpoch += 1;
|
|
16708
|
-
}
|
|
17613
|
+
if (source === "workbuddy") invalidateGrowthRead();
|
|
16709
17614
|
return await statusDocument({ allowNetwork: false });
|
|
16710
17615
|
}
|
|
16711
17616
|
/**
|
|
@@ -16724,10 +17629,7 @@ async function onPanelInvoke(channel, payload) {
|
|
|
16724
17629
|
await applySettingsEverywhere(false);
|
|
16725
17630
|
if (payload?.enabled === true) await refreshAll("source-on", true);
|
|
16726
17631
|
else syncDeclaration();
|
|
16727
|
-
if (payload?.source === "workbuddy")
|
|
16728
|
-
growthRead = null;
|
|
16729
|
-
growthReadEpoch += 1;
|
|
16730
|
-
}
|
|
17632
|
+
if (payload?.source === "workbuddy") invalidateGrowthRead();
|
|
16731
17633
|
return await statusDocument({ allowNetwork: false });
|
|
16732
17634
|
}
|
|
16733
17635
|
/**
|
|
@@ -16739,15 +17641,24 @@ async function onPanelInvoke(channel, payload) {
|
|
|
16739
17641
|
*/
|
|
16740
17642
|
case "xd.startLogin": {
|
|
16741
17643
|
const realm = payload?.realm === "global" ? "global" : "cn";
|
|
16742
|
-
|
|
16743
|
-
|
|
16744
|
-
|
|
16745
|
-
|
|
16746
|
-
|
|
16747
|
-
|
|
16748
|
-
|
|
16749
|
-
|
|
16750
|
-
|
|
17644
|
+
try {
|
|
17645
|
+
const start = await startDeviceLogin(realm);
|
|
17646
|
+
deviceLogins.set(realm, {
|
|
17647
|
+
state: start.state,
|
|
17648
|
+
at: Date.now()
|
|
17649
|
+
});
|
|
17650
|
+
return {
|
|
17651
|
+
realm,
|
|
17652
|
+
authUrl: start.authUrl
|
|
17653
|
+
};
|
|
17654
|
+
} catch (error) {
|
|
17655
|
+
deviceLogins.delete(realm);
|
|
17656
|
+
log("warn", "could not start a WorkBuddy sign-in: " + safeMessage(error));
|
|
17657
|
+
return {
|
|
17658
|
+
realm,
|
|
17659
|
+
authUrl: ""
|
|
17660
|
+
};
|
|
17661
|
+
}
|
|
16751
17662
|
}
|
|
16752
17663
|
/**
|
|
16753
17664
|
* Poll the login the panel started on this realm.
|
|
@@ -16774,9 +17685,8 @@ async function onPanelInvoke(channel, payload) {
|
|
|
16774
17685
|
const directory = ownSignInDir(dataPath);
|
|
16775
17686
|
if (directory === "") throw panelError("no plugin data directory to write the sign-in into", "NO_DATA_DIR");
|
|
16776
17687
|
await writeOwnSignIn(directory, answer.signIn);
|
|
16777
|
-
|
|
16778
|
-
|
|
16779
|
-
await settleWithin(core.pool.scan(), POOL_SCAN_BUDGET_MS);
|
|
17688
|
+
invalidateGrowthRead();
|
|
17689
|
+
await settleWithin(core.pool.rescan(), POOL_SCAN_BUDGET_MS);
|
|
16780
17690
|
await refreshAll("panel-login", false);
|
|
16781
17691
|
return {
|
|
16782
17692
|
status: "ok",
|
|
@@ -17945,10 +18855,27 @@ function stopAccountWatch() {
|
|
|
17945
18855
|
* Note this is the only place the roster is fetched during load: opening the
|
|
17946
18856
|
* panel no longer triggers detection of its own.
|
|
17947
18857
|
*/
|
|
18858
|
+
/**
|
|
18859
|
+
* Close every source gateway when the whole plugin is switched off.
|
|
18860
|
+
*
|
|
18861
|
+
* A reload can arrive without the previous activation's stop. Those gateways
|
|
18862
|
+
* hold live credentials and keep answering on loopback, so switching the plugin
|
|
18863
|
+
* off must close them here rather than waiting for a teardown that may not run.
|
|
18864
|
+
*/
|
|
18865
|
+
async function closeSourceGatewaysForDisabled() {
|
|
18866
|
+
if (core.pool !== null) {
|
|
18867
|
+
applySettingsToCore();
|
|
18868
|
+
reconcileRuntimes();
|
|
18869
|
+
}
|
|
18870
|
+
await settleWithin(reconcileTraeGateways(), DISABLED_CLOSE_BUDGET_MS);
|
|
18871
|
+
await settleWithin(reconcileQoderGateways(), DISABLED_CLOSE_BUDGET_MS);
|
|
18872
|
+
}
|
|
17948
18873
|
async function publishDeclarationEarly(generation) {
|
|
17949
18874
|
await adoptHostSettings();
|
|
17950
18875
|
if (!stillLoaded(generation)) return;
|
|
17951
18876
|
if (settings.enabled === false) {
|
|
18877
|
+
await closeSourceGatewaysForDisabled();
|
|
18878
|
+
if (!stillLoaded(generation)) return;
|
|
17952
18879
|
const cleared = syncDeclaration();
|
|
17953
18880
|
if (cleared.ok !== true) log("warn", "declaration not cleared: " + String(cleared.error));
|
|
17954
18881
|
return;
|
|
@@ -17966,7 +18893,7 @@ async function publishDeclarationEarly(generation) {
|
|
|
17966
18893
|
if (!stillLoaded(generation)) return;
|
|
17967
18894
|
await settleWithin(Promise.all(Array.from(runtimes.values()).map(async (runtime) => {
|
|
17968
18895
|
try {
|
|
17969
|
-
await
|
|
18896
|
+
await refreshRuntime(runtime, "load", true);
|
|
17970
18897
|
} catch (error) {
|
|
17971
18898
|
runtime.lastError = safeMessage(error);
|
|
17972
18899
|
log("warn", runtime.region + " failed to start during load: " + runtime.lastError);
|
|
@@ -17986,6 +18913,8 @@ async function onServiceStart() {
|
|
|
17986
18913
|
await adoptHostSettings();
|
|
17987
18914
|
if (!stillLoaded(generation)) return;
|
|
17988
18915
|
if (settings.enabled === false) {
|
|
18916
|
+
await closeSourceGatewaysForDisabled();
|
|
18917
|
+
if (!stillLoaded(generation)) return;
|
|
17989
18918
|
syncDeclaration();
|
|
17990
18919
|
log("info", "plugin disabled in settings; the provider host stays down");
|
|
17991
18920
|
return;
|
|
@@ -18004,7 +18933,7 @@ async function onServiceStart() {
|
|
|
18004
18933
|
if (!stillLoaded(generation)) return;
|
|
18005
18934
|
await settleWithin(Promise.all(Array.from(runtimes.values()).map(async (runtime) => {
|
|
18006
18935
|
try {
|
|
18007
|
-
await
|
|
18936
|
+
await refreshRuntime(runtime, "service", !catalogFresh(runtime));
|
|
18008
18937
|
} catch (error) {
|
|
18009
18938
|
runtime.lastError = safeMessage(error);
|
|
18010
18939
|
log("warn", runtime.region + " failed to start: " + runtime.lastError);
|
|
@@ -18022,6 +18951,7 @@ async function onServiceStop() {
|
|
|
18022
18951
|
clearTimeout(refreshTimer);
|
|
18023
18952
|
refreshTimer = null;
|
|
18024
18953
|
}
|
|
18954
|
+
refreshPending = false;
|
|
18025
18955
|
refreshInflight = null;
|
|
18026
18956
|
stopAutoCheckin();
|
|
18027
18957
|
stopAccountWatch();
|
|
@@ -18041,6 +18971,19 @@ async function onServiceStop() {
|
|
|
18041
18971
|
sourceGatewayStarts.clear();
|
|
18042
18972
|
await Promise.all(closing);
|
|
18043
18973
|
}
|
|
18974
|
+
/**
|
|
18975
|
+
* Drop the Trae/Qoder state keyed to the previous activation's sign-ins.
|
|
18976
|
+
*
|
|
18977
|
+
* Called from both load and unload: a reload can start a new activation while
|
|
18978
|
+
* the old stop is still awaited, and that path skips the old `onUnload`.
|
|
18979
|
+
*/
|
|
18980
|
+
function resetSourceActivationState() {
|
|
18981
|
+
sourceFailureHold.clear();
|
|
18982
|
+
sourceCreditExpiry.clear();
|
|
18983
|
+
sourceRotation.clear();
|
|
18984
|
+
travelAdoptTried.clear();
|
|
18985
|
+
traeIdentityBlockers.clear();
|
|
18986
|
+
}
|
|
18044
18987
|
async function onLoad() {
|
|
18045
18988
|
serviceStopped = false;
|
|
18046
18989
|
loadGeneration += 1;
|
|
@@ -18057,6 +19000,7 @@ async function onLoad() {
|
|
|
18057
19000
|
clearTimeout(refreshTimer);
|
|
18058
19001
|
refreshTimer = null;
|
|
18059
19002
|
}
|
|
19003
|
+
refreshPending = false;
|
|
18060
19004
|
refreshInflight = null;
|
|
18061
19005
|
for (const runtime of runtimes.values()) trackRuntimeStop(runtime);
|
|
18062
19006
|
runtimes.clear();
|
|
@@ -18069,6 +19013,14 @@ async function onLoad() {
|
|
|
18069
19013
|
autoCheckinLast = null;
|
|
18070
19014
|
growthPassRunning = false;
|
|
18071
19015
|
growthLast = null;
|
|
19016
|
+
invalidateGrowthRead();
|
|
19017
|
+
traeAccountRows = [];
|
|
19018
|
+
qoderAccountRows = [];
|
|
19019
|
+
traeLabelOverrides = /* @__PURE__ */ new Map();
|
|
19020
|
+
qoderLabelOverrides = /* @__PURE__ */ new Map();
|
|
19021
|
+
resetSourceActivationState();
|
|
19022
|
+
probeCache.clear();
|
|
19023
|
+
deviceLogins.clear();
|
|
18072
19024
|
keepAliveLast = null;
|
|
18073
19025
|
qoderRetryAt = 0;
|
|
18074
19026
|
core.client = new WorkBuddyUpstreamClient({});
|
|
@@ -18119,11 +19071,7 @@ async function onUnload() {
|
|
|
18119
19071
|
abortWarmups();
|
|
18120
19072
|
const stopping = onServiceStop();
|
|
18121
19073
|
probeCache.clear();
|
|
18122
|
-
|
|
18123
|
-
sourceCreditExpiry.clear();
|
|
18124
|
-
sourceRotation.clear();
|
|
18125
|
-
travelAdoptTried.clear();
|
|
18126
|
-
traeIdentityBlockers.clear();
|
|
19074
|
+
resetSourceActivationState();
|
|
18127
19075
|
gatewayStartFailures.clear();
|
|
18128
19076
|
sourceStartFailures.clear();
|
|
18129
19077
|
sourceGatewayStarts.clear();
|
|
@@ -18211,8 +19159,8 @@ function workbuddyComposerAccounts(region, modelId) {
|
|
|
18211
19159
|
const now = Date.now();
|
|
18212
19160
|
const list = core.pool.list(region);
|
|
18213
19161
|
const available = core.pool.available(now, modelId, region);
|
|
18214
|
-
const preferredId =
|
|
18215
|
-
const next = available.find((account) => account.id === preferredId) ?? core.pool.
|
|
19162
|
+
const preferredId = core.pool.resolvedPreferredId(region);
|
|
19163
|
+
const next = available.find((account) => account.id === preferredId) ?? core.pool.readoutPreview(available, region);
|
|
18216
19164
|
return list.map((account) => {
|
|
18217
19165
|
const cached = probeCache.get(account.id)?.value?.credits;
|
|
18218
19166
|
return {
|
|
@@ -18400,16 +19348,6 @@ const INERT_AUTH = {
|
|
|
18400
19348
|
/** The sentinel every loopback gateway accepts. */
|
|
18401
19349
|
const LOOPBACK_BEARER = "dsh-wtq-pool-loopback";
|
|
18402
19350
|
/**
|
|
18403
|
-
* Whether the installed schemastery can mark a field volatile.
|
|
18404
|
-
*
|
|
18405
|
-
* `Config` is built at module evaluation, before any `ctx` exists to log
|
|
18406
|
-
* through, so the finding is recorded here and reported by the activation's
|
|
18407
|
-
* first logger-reaching call rather than dropped.
|
|
18408
|
-
*/
|
|
18409
|
-
let volatileSupported = true;
|
|
18410
|
-
/** Whether the finding above has already been named, so it is named once. */
|
|
18411
|
-
let degradationReported = false;
|
|
18412
|
-
/**
|
|
18413
19351
|
* Which activation currently owns the module-global state the core keeps.
|
|
18414
19352
|
*
|
|
18415
19353
|
* A re-provided service re-runs `apply()`, and the core's state (runtimes,
|
|
@@ -18418,53 +19356,32 @@ let degradationReported = false;
|
|
|
18418
19356
|
* gateways the newer one just brought up.
|
|
18419
19357
|
*/
|
|
18420
19358
|
let activationToken = 0;
|
|
18421
|
-
/**
|
|
18422
|
-
* Wrap one schema in `volatile()` when the installed schemastery has it.
|
|
18423
|
-
*
|
|
18424
|
-
* `volatile()` landed in schemastery 3.18.3. Hand-writing `meta.volatile` on an
|
|
18425
|
-
* older copy would bypass the host's own `validateVolatileSchema`, so an older
|
|
18426
|
-
* builder degrades to the plain schema instead.
|
|
18427
|
-
*/
|
|
18428
|
-
function asVolatile(schema) {
|
|
18429
|
-
const candidate = schema;
|
|
18430
|
-
if (typeof candidate.volatile !== "function") {
|
|
18431
|
-
volatileSupported = false;
|
|
18432
|
-
return schema;
|
|
18433
|
-
}
|
|
18434
|
-
return candidate.volatile();
|
|
18435
|
-
}
|
|
18436
|
-
/** Report the schema degradation once, when a logger is finally in reach. */
|
|
18437
|
-
function reportSchemaDegradation(logger) {
|
|
18438
|
-
if (volatileSupported || degradationReported) return;
|
|
18439
|
-
degradationReported = true;
|
|
18440
|
-
logger.warn("dsh-wtq-pool: the installed schemastery has no volatile(), so no settings field can be marked volatile: the settings card cannot render its section and every settings write this plugin makes is refused");
|
|
18441
|
-
}
|
|
18442
19359
|
const Config = z.object({
|
|
18443
|
-
enabled:
|
|
18444
|
-
sourceWorkbuddy:
|
|
18445
|
-
sourceTrae:
|
|
18446
|
-
sourceQoder:
|
|
18447
|
-
authFile:
|
|
18448
|
-
autoCheckin:
|
|
19360
|
+
enabled: z.boolean().default(true).description("Serve this pool at all").volatile(),
|
|
19361
|
+
sourceWorkbuddy: z.boolean().default(true).description("Merge the WorkBuddy desktop sign-ins").volatile(),
|
|
19362
|
+
sourceTrae: z.boolean().default(true).description("Merge the Trae desktop sign-ins").volatile(),
|
|
19363
|
+
sourceQoder: z.boolean().default(true).description("Merge the Qoder desktop sign-ins").volatile(),
|
|
19364
|
+
authFile: z.string().description("WorkBuddy desktop auth file (defaults to the app's own location)").volatile(),
|
|
19365
|
+
autoCheckin: z.object({
|
|
18449
19366
|
enabled: z.boolean().default(false),
|
|
18450
19367
|
hours: z.array(z.number()).default([])
|
|
18451
|
-
}).description("Daily check-in across every discovered sign-in")),
|
|
18452
|
-
cooldownSeconds:
|
|
18453
|
-
creditReserves:
|
|
18454
|
-
disabledAccountIds:
|
|
18455
|
-
disabledRegions:
|
|
18456
|
-
distribution:
|
|
19368
|
+
}).description("Daily check-in across every discovered sign-in").volatile(),
|
|
19369
|
+
cooldownSeconds: z.number().default(60).description("Rate-limit cooldown per account, in seconds").volatile(),
|
|
19370
|
+
creditReserves: z.dict(z.number()).default({}).description("Per-account credit floor, keyed by account id").volatile(),
|
|
19371
|
+
disabledAccountIds: z.array(z.string()).default([]).description("Account ids the pool must never pick").volatile(),
|
|
19372
|
+
disabledRegions: z.array(z.string()).default([]).description("Regions parked on their own, as source:region").volatile(),
|
|
19373
|
+
distribution: z.union([
|
|
18457
19374
|
"priority",
|
|
18458
19375
|
"round-robin",
|
|
18459
19376
|
"expiry",
|
|
18460
19377
|
"sticky"
|
|
18461
|
-
]).default("priority").description("How the WorkBuddy pool spreads requests")),
|
|
18462
|
-
modelSelection:
|
|
18463
|
-
preferredAccountIds:
|
|
18464
|
-
preferredSourceAccounts:
|
|
18465
|
-
removedAccounts:
|
|
18466
|
-
showComposerPoints:
|
|
18467
|
-
sourceDistributions:
|
|
19378
|
+
]).default("priority").description("How the WorkBuddy pool spreads requests").volatile(),
|
|
19379
|
+
modelSelection: z.any().description("Per-gateway model selection").volatile(),
|
|
19380
|
+
preferredAccountIds: z.any().description("Picked WorkBuddy account per region").volatile(),
|
|
19381
|
+
preferredSourceAccounts: z.any().description("Picked Trae/Qoder account per region").volatile(),
|
|
19382
|
+
removedAccounts: z.any().description("Accounts taken out of the pool; their label and region are kept so the panel can restore them").volatile(),
|
|
19383
|
+
showComposerPoints: z.boolean().default(false).description("Show the active account credits beside the composer").volatile(),
|
|
19384
|
+
sourceDistributions: z.any().description("How each Trae/Qoder region spreads requests").volatile()
|
|
18468
19385
|
});
|
|
18469
19386
|
/** Peel one volatile reference, if the running line wrapped it. */
|
|
18470
19387
|
function unwrapVolatile(value) {
|
|
@@ -18717,22 +19634,47 @@ function apply(ctx, config = {}) {
|
|
|
18717
19634
|
let writesInFlight = 0;
|
|
18718
19635
|
let writesStarted = 0;
|
|
18719
19636
|
let droppedPushes = 0;
|
|
19637
|
+
/**
|
|
19638
|
+
* Whether two setting values are the same document.
|
|
19639
|
+
*
|
|
19640
|
+
* Object key order carries no meaning in the stored document, and the loader's
|
|
19641
|
+
* own comparison ignores it, so a key-order-sensitive check would predict an
|
|
19642
|
+
* echo the host never emits.
|
|
19643
|
+
*/
|
|
19644
|
+
function sameSetting(a, b) {
|
|
19645
|
+
if (Object.is(a, b)) return true;
|
|
19646
|
+
if (typeof a !== "object" || a === null || typeof b !== "object" || b === null) return false;
|
|
19647
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
19648
|
+
const aRecord = a;
|
|
19649
|
+
const bRecord = b;
|
|
19650
|
+
const keys = Object.keys(aRecord).filter((key) => aRecord[key] !== void 0);
|
|
19651
|
+
if (keys.length !== Object.keys(bRecord).filter((key) => bRecord[key] !== void 0).length) return false;
|
|
19652
|
+
for (const key of keys) {
|
|
19653
|
+
if (!Object.prototype.hasOwnProperty.call(bRecord, key)) return false;
|
|
19654
|
+
if (!sameSetting(aRecord[key], bRecord[key])) return false;
|
|
19655
|
+
}
|
|
19656
|
+
return true;
|
|
19657
|
+
}
|
|
18720
19658
|
/** Replace patched top-level fields without merging their dictionaries. */
|
|
18721
19659
|
const writeSettings = async (patch) => {
|
|
18722
19660
|
const mutate = settingsService?.mutate;
|
|
18723
19661
|
if (mutate === void 0) throw new Error("this host exposes no settings mutate()");
|
|
19662
|
+
const before = current();
|
|
18724
19663
|
writesStarted += 1;
|
|
18725
19664
|
writesInFlight += 1;
|
|
18726
|
-
let
|
|
19665
|
+
let echoExpected = false;
|
|
18727
19666
|
try {
|
|
19667
|
+
echoExpected = Object.keys(patch).some((key) => !sameSetting(before[key], patch[key]));
|
|
18728
19668
|
await mutate.call(settingsService, settingsNamespace, Object.entries(patch).map(([key, value]) => ({
|
|
18729
19669
|
op: "set",
|
|
18730
19670
|
path: [key],
|
|
18731
19671
|
value
|
|
18732
19672
|
})));
|
|
18733
|
-
|
|
19673
|
+
} catch (error) {
|
|
19674
|
+
echoExpected = false;
|
|
19675
|
+
throw error;
|
|
18734
19676
|
} finally {
|
|
18735
|
-
if (!
|
|
19677
|
+
if (!echoExpected) writesStarted -= 1;
|
|
18736
19678
|
writesInFlight -= 1;
|
|
18737
19679
|
if (writesInFlight === 0) {
|
|
18738
19680
|
if (droppedPushes > writesStarted) {
|
|
@@ -18751,9 +19693,11 @@ function apply(ctx, config = {}) {
|
|
|
18751
19693
|
getSettings: () => current(),
|
|
18752
19694
|
setSettings: writeSettings,
|
|
18753
19695
|
getDataPath: async () => {
|
|
18754
|
-
reportSchemaDegradation(ctx.logger);
|
|
18755
19696
|
const dir = path.join(dshHome(), "dsh-wtq-pool");
|
|
18756
|
-
await fs.mkdir(dir, {
|
|
19697
|
+
await fs.mkdir(dir, {
|
|
19698
|
+
recursive: true,
|
|
19699
|
+
mode: 448
|
|
19700
|
+
});
|
|
18757
19701
|
return dir;
|
|
18758
19702
|
},
|
|
18759
19703
|
onSettingsChanged: (handler) => {
|
|
@@ -18878,7 +19822,7 @@ function apply(ctx, config = {}) {
|
|
|
18878
19822
|
pluginRoot: PLUGIN_ROOT,
|
|
18879
19823
|
invoke: (channel, payload) => onPanelInvoke(channel, payload),
|
|
18880
19824
|
effect: (callback) => webCtx.effect(callback, "dsh-wtq-pool: panel routes"),
|
|
18881
|
-
logger: ctx.logger
|
|
19825
|
+
logger: { warn: (...args) => ctx.logger.warn(...args.map(safeMessage)) }
|
|
18882
19826
|
});
|
|
18883
19827
|
});
|
|
18884
19828
|
ctx.effect(() => async () => {
|
|
@@ -18890,10 +19834,11 @@ function apply(ctx, config = {}) {
|
|
|
18890
19834
|
disposeRegistration = void 0;
|
|
18891
19835
|
if (token !== activationToken) return;
|
|
18892
19836
|
await stopRegisteredService();
|
|
19837
|
+
if (token !== activationToken) return;
|
|
18893
19838
|
await onUnload();
|
|
18894
19839
|
});
|
|
18895
19840
|
onLoad().then(() => {
|
|
18896
|
-
if (!stopped) return startRegisteredService();
|
|
19841
|
+
if (!stopped && token === activationToken) return startRegisteredService();
|
|
18897
19842
|
}).catch((error) => {
|
|
18898
19843
|
if (stopped) return;
|
|
18899
19844
|
ctx.logger.error("dsh-wtq-pool: activation failed", safeMessage(error));
|