dsh-wtq-pool 1.3.0 → 1.3.6

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/lib/index.js CHANGED
@@ -12,7 +12,7 @@ import { resolveImageAttachmentAccess, resolveRetryPolicy } from "@deepseek-ai/d
12
12
  import { PiAiAdapter } from "@deepseek-ai/dsh-llm-pi-ai";
13
13
  import z from "@deepseek-ai/schemastery";
14
14
  import { createServer } from "node:http";
15
- import { constants, createCipheriv, createDecipheriv, createHash, publicEncrypt, randomBytes, randomUUID } from "node:crypto";
15
+ import { constants, createCipheriv, createDecipheriv, createHash, pbkdf2Sync, publicEncrypt, randomBytes, randomUUID } from "node:crypto";
16
16
  import * as net from "node:net";
17
17
  import { BlockList, isIP } from "node:net";
18
18
  import * as fs$1 from "node:fs";
@@ -1425,7 +1425,7 @@ function retryAfterSeconds(headers) {
1425
1425
  * read as a clean end, which the relay would otherwise report as a finished
1426
1426
  * turn with a truncated reply.
1427
1427
  */
1428
- function withIdleWatchdog(body, idleMs = STREAM_IDLE_TIMEOUT_MS$1, signal) {
1428
+ function withIdleWatchdog(body, idleMs = STREAM_IDLE_TIMEOUT_MS$1, signal, abortTransport) {
1429
1429
  const reader = body.getReader();
1430
1430
  let timer;
1431
1431
  let idle;
@@ -1446,6 +1446,7 @@ function withIdleWatchdog(body, idleMs = STREAM_IDLE_TIMEOUT_MS$1, signal) {
1446
1446
  const stop = (reason) => {
1447
1447
  if (idle !== void 0) return;
1448
1448
  idle = reason;
1449
+ abortTransport?.(reason);
1449
1450
  reader.cancel(reason).catch(() => {});
1450
1451
  disarm();
1451
1452
  detach();
@@ -1495,6 +1496,7 @@ function withIdleWatchdog(body, idleMs = STREAM_IDLE_TIMEOUT_MS$1, signal) {
1495
1496
  },
1496
1497
  cancel(reason) {
1497
1498
  finish();
1499
+ abortTransport?.(reason);
1498
1500
  return reader.cancel(reason);
1499
1501
  }
1500
1502
  });
@@ -1528,6 +1530,7 @@ function headersDeadline(caller, ms) {
1528
1530
  timer.unref?.();
1529
1531
  return {
1530
1532
  signal: controller.signal,
1533
+ abort: (reason) => controller.abort(reason),
1531
1534
  arrived: () => {
1532
1535
  clearTimeout(timer);
1533
1536
  caller?.removeEventListener("abort", onCallerAbort);
@@ -1666,6 +1669,17 @@ function globalBase(credential) {
1666
1669
  function chatBase(credential) {
1667
1670
  return regionOf(credential.domain) === "global" ? globalBase(credential) : CN_CHAT_BASE;
1668
1671
  }
1672
+ /**
1673
+ * The host a sign-in's chat traffic actually reaches.
1674
+ *
1675
+ * The sign-in domain is not the chat host: every CN spelling (including an
1676
+ * empty one) is served by one gateway, and the two international brand domains
1677
+ * have gateways of their own. Failover keys transport failures on this value so
1678
+ * that one unreachable gateway condemns exactly the accounts it serves.
1679
+ */
1680
+ function chatHostOf(credential) {
1681
+ return new URL(chatBase(credential)).host;
1682
+ }
1669
1683
  function billingBase(credential) {
1670
1684
  return regionOf(credential.domain) === "global" ? globalBase(credential) : CN_BILLING_BASE;
1671
1685
  }
@@ -2376,7 +2390,7 @@ var WorkBuddyUpstreamClient = class {
2376
2390
  status: 502,
2377
2391
  message: "upstream returned 200 without a stream"
2378
2392
  };
2379
- const relayed = withIdleWatchdog(response.body, timeouts?.idleMs, signal);
2393
+ const relayed = withIdleWatchdog(response.body, timeouts?.idleMs, signal, deadline.abort);
2380
2394
  return {
2381
2395
  ok: true,
2382
2396
  response: withRelayStream(new Response(relayed, {
@@ -2386,6 +2400,7 @@ var WorkBuddyUpstreamClient = class {
2386
2400
  };
2387
2401
  }
2388
2402
  const text = await readErrorBody(response, ERROR_BODY_LIMIT, signal);
2403
+ deadline.abort();
2389
2404
  const retryAfter = retryAfterSeconds(response.headers);
2390
2405
  if (attempt === 0 && response.status === 400 && isChannelCheckText(text)) {
2391
2406
  const folded = foldSystemIntoUser(body);
@@ -3474,33 +3489,32 @@ async function encryptedFieldOpener() {
3474
3489
  }
3475
3490
  //#endregion
3476
3491
  //#region src/trusted-process.ts
3477
- /** Resolve the OS directory without trusting inherited Windows environment variables. */
3478
- function trustedSystemRootOf(env) {
3479
- if (process.platform === "win32") try {
3492
+ /** Resolve the OS directory from the kernel, never from an inherited variable. */
3493
+ function trustedSystemRootOf() {
3494
+ if (process.platform !== "win32") return void 0;
3495
+ try {
3480
3496
  return realpathSync.native("\\\\?\\GLOBALROOT\\SystemRoot");
3481
3497
  } catch {
3482
3498
  return;
3483
3499
  }
3484
- const value = env["SystemRoot"]?.trim() || env["windir"]?.trim();
3485
- return value === void 0 || value === "" ? void 0 : value;
3486
3500
  }
3487
- function trustedWindowsPowerShell(env) {
3488
- const systemRoot = trustedSystemRootOf(env);
3501
+ function trustedWindowsPowerShell() {
3502
+ const systemRoot = trustedSystemRootOf();
3489
3503
  if (systemRoot === void 0) return void 0;
3490
3504
  return join(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
3491
3505
  }
3492
3506
  /** Build a PowerShell environment with fixed executable and module search paths. */
3493
- function trustedWindowsChildEnv(env, options = {}) {
3494
- const systemRoot = trustedSystemRootOf(env);
3507
+ function trustedWindowsChildEnv(env) {
3508
+ const systemRoot = trustedSystemRootOf();
3495
3509
  if (systemRoot === void 0) return {};
3496
3510
  const result = {};
3497
- if (options.inheritTemp === true) for (const name of ["TEMP", "TMP"]) {
3511
+ for (const name of ["TEMP", "TMP"]) {
3498
3512
  const value = env[name];
3499
3513
  if (value !== void 0 && value !== "") result[name] = value;
3500
3514
  }
3501
3515
  result.SystemRoot = systemRoot;
3502
3516
  result.windir = systemRoot;
3503
- if (options.includeSystemDrive === true && /^[A-Za-z]:\\/.test(systemRoot)) result.SystemDrive = systemRoot.slice(0, 2);
3517
+ if (/^[A-Za-z]:\\/.test(systemRoot)) result.SystemDrive = systemRoot.slice(0, 2);
3504
3518
  result.PATH = join(systemRoot, "System32");
3505
3519
  result.PSModulePath = join(systemRoot, "System32", "WindowsPowerShell", "v1.0", "Modules");
3506
3520
  return result;
@@ -3607,10 +3621,10 @@ function processIsAlive(pid) {
3607
3621
  return;
3608
3622
  }
3609
3623
  }
3610
- function processOutput(command, args, env, cwd) {
3624
+ function processOutput(command, args, env, cwd, timeoutMs) {
3611
3625
  return new Promise((resolve) => {
3612
3626
  execFile(command, [...args], {
3613
- timeout: 2e3,
3627
+ timeout: timeoutMs,
3614
3628
  maxBuffer: 1024,
3615
3629
  windowsHide: true,
3616
3630
  env,
@@ -3621,11 +3635,11 @@ function processOutput(command, args, env, cwd) {
3621
3635
  });
3622
3636
  });
3623
3637
  }
3624
- async function processStartedAt(pid) {
3638
+ async function processStartedAt(pid, timeoutMs = 2e3) {
3625
3639
  if (pid === process.pid) return PROCESS_STARTED_AT_MS;
3626
3640
  if (process.platform === "win32") {
3627
- const systemRoot = trustedSystemRootOf(process.env);
3628
- const powershell = trustedWindowsPowerShell(process.env);
3641
+ const systemRoot = trustedSystemRootOf();
3642
+ const powershell = trustedWindowsPowerShell();
3629
3643
  if (systemRoot === void 0 || powershell === void 0) return void 0;
3630
3644
  const match = (await processOutput(powershell, [
3631
3645
  "-NoLogo",
@@ -3633,10 +3647,7 @@ async function processStartedAt(pid) {
3633
3647
  "-NonInteractive",
3634
3648
  "-Command",
3635
3649
  `$env:PSModulePath=[IO.Path]::Combine($env:SystemRoot,'System32\\WindowsPowerShell\\v1.0\\Modules'); $d=(Get-Process -Id ${pid} -ErrorAction Stop).StartTime.ToUniversalTime(); ([DateTimeOffset]$d).ToUnixTimeMilliseconds()`
3636
- ], trustedWindowsChildEnv(process.env, {
3637
- inheritTemp: true,
3638
- includeSystemDrive: true
3639
- }), join(systemRoot, "System32")))?.match(/\d{10,}/);
3650
+ ], trustedWindowsChildEnv(process.env), join(systemRoot, "System32"), timeoutMs))?.match(/\d{10,}/);
3640
3651
  return match === void 0 || match === null ? void 0 : Number(match[0]);
3641
3652
  }
3642
3653
  const ps = ["/usr/bin/ps", "/bin/ps"].find(existsSync);
@@ -3650,11 +3661,11 @@ async function processStartedAt(pid) {
3650
3661
  PATH: "/usr/bin:/bin",
3651
3662
  LC_ALL: "C",
3652
3663
  TZ: "UTC"
3653
- }, "/");
3664
+ }, "/", timeoutMs);
3654
3665
  const started = output === void 0 ? NaN : Date.parse(`${output.trim()} UTC`);
3655
3666
  return Number.isFinite(started) ? started : void 0;
3656
3667
  }
3657
- async function lockState(file) {
3668
+ async function lockState(file, probeTimeoutMs = 2e3, probe = processStartedAt) {
3658
3669
  let text;
3659
3670
  try {
3660
3671
  text = await readFile(file, "utf8");
@@ -3679,7 +3690,7 @@ async function lockState(file) {
3679
3690
  if (ownerAlive === true && typeof owner?.pid === "number") {
3680
3691
  const recordedStart = typeof owner.processStartedAtMs === "number" ? owner.processStartedAtMs : typeof owner.createdAtMs === "number" ? owner.createdAtMs : void 0;
3681
3692
  if (recordedStart !== void 0 && Math.abs(fileAge) > PROCESS_START_TOLERANCE_MS) {
3682
- const actualStart = await processStartedAt(owner.pid);
3693
+ const actualStart = await probe(owner.pid, probeTimeoutMs);
3683
3694
  if (actualStart !== void 0 && actualStart - recordedStart > PROCESS_START_TOLERANCE_MS) return "stale";
3684
3695
  }
3685
3696
  if (recordedStart === void 0) return fileAge > UNOWNED_FILE_LOCK_STALE_MS ? "stale" : "current";
@@ -3818,7 +3829,7 @@ async function cleanupTerminalReclaims(lock) {
3818
3829
  }
3819
3830
  return !blocked;
3820
3831
  }
3821
- async function claimStaleReclaim(lock) {
3832
+ async function claimStaleReclaim(lock, probeTimeoutMs = 2e3, probe = processStartedAt) {
3822
3833
  const directory = dirname(lock);
3823
3834
  const prefix = `${basename(lock)}.reclaim.`;
3824
3835
  const names = await readdir(directory);
@@ -3846,7 +3857,7 @@ async function claimStaleReclaim(lock) {
3846
3857
  }
3847
3858
  if (owner.state === "complete" || owner.state === "abandoned") return void 0;
3848
3859
  if (owner.token !== void 0 && ABANDONED_RECLAIMS.get(claim) === owner.token) return void 0;
3849
- if (await lockState(claim) === "current") return void 0;
3860
+ if (await lockState(claim, probeTimeoutMs, probe) === "current") return void 0;
3850
3861
  try {
3851
3862
  await unlink(claim);
3852
3863
  } catch (error) {
@@ -3932,14 +3943,23 @@ async function withFileLock(lock, work) {
3932
3943
  mode: 448
3933
3944
  });
3934
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
+ };
3935
3955
  let token;
3936
3956
  while (token === void 0) {
3937
3957
  token = await createOwnedFile(lock);
3938
3958
  if (token !== void 0) break;
3939
- if (await lockState(lock) === "stale") {
3940
- const reclaim = await cleanupTerminalReclaims(lock) ? await claimStaleReclaim(lock) : void 0;
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;
3941
3961
  if (reclaim !== void 0) try {
3942
- if (await newestReclaimGeneration(lock) === reclaim.generation && await lockState(lock) === "stale" && await newestReclaimGeneration(lock) === reclaim.generation) await unlink(lock).catch(() => {});
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(() => {});
3943
3963
  } finally {
3944
3964
  try {
3945
3965
  await completeReclaim(reclaim);
@@ -4951,12 +4971,16 @@ function sameDirs(a, b) {
4951
4971
  * over every sign-in it still holds.
4952
4972
  */
4953
4973
  async function readArchivedCredentials(file) {
4954
- const stored = /* @__PURE__ */ new Map();
4974
+ const result = {
4975
+ stored: /* @__PURE__ */ new Map(),
4976
+ pruned: false
4977
+ };
4978
+ const { stored } = result;
4955
4979
  let text;
4956
4980
  try {
4957
4981
  text = await readFile(file, "utf8");
4958
4982
  } catch (error) {
4959
- if (error.code === "ENOENT") return stored;
4983
+ if (error.code === "ENOENT") return result;
4960
4984
  throw error;
4961
4985
  }
4962
4986
  let parsed;
@@ -4965,9 +4989,9 @@ async function readArchivedCredentials(file) {
4965
4989
  } catch {
4966
4990
  throw new Error("credential archive is not valid JSON");
4967
4991
  }
4968
- if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return stored;
4992
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return result;
4969
4993
  const entries = parsed.entries;
4970
- if (!Array.isArray(entries)) return stored;
4994
+ if (!Array.isArray(entries)) return result;
4971
4995
  const horizon = Date.now() - ARCHIVE_RETENTION_MS;
4972
4996
  for (const entry of entries) {
4973
4997
  if (typeof entry !== "object" || entry === null) continue;
@@ -4978,7 +5002,10 @@ async function readArchivedCredentials(file) {
4978
5002
  const refreshExpiresAt = typeof record["refreshExpiresAtMs"] === "number" ? expiryToMs(record["refreshExpiresAtMs"]) : 0;
4979
5003
  const lastRefreshAt = typeof record["lastRefreshAtMs"] === "number" ? expiryToMs(record["lastRefreshAtMs"]) : 0;
4980
5004
  const lastUsable = Math.max(expiresAt, refreshExpiresAt);
4981
- if (lastUsable > 0 && lastUsable < horizon) continue;
5005
+ if (lastUsable > 0 && lastUsable < horizon) {
5006
+ result.pruned = true;
5007
+ continue;
5008
+ }
4982
5009
  stored.set(record["id"], {
4983
5010
  accessToken: record["accessToken"],
4984
5011
  refreshToken: typeof record["refreshToken"] === "string" ? record["refreshToken"] : "",
@@ -4993,7 +5020,7 @@ async function readArchivedCredentials(file) {
4993
5020
  sourcePath: typeof record["sourcePath"] === "string" ? record["sourcePath"] : ""
4994
5021
  });
4995
5022
  }
4996
- return stored;
5023
+ return result;
4997
5024
  }
4998
5025
  /** How old a leftover write temp must be before another writer removes it. */
4999
5026
  const ABANDONED_TEMP_MAX_AGE_MS = 6e5;
@@ -5104,23 +5131,33 @@ var WorkBuddyAccountPool = class {
5104
5131
  * from settings and only filters at pick time.
5105
5132
  */
5106
5133
  disabledIds = /* @__PURE__ */ new Set();
5107
- /** Cursor for round-robin and the next new sticky conversation. */
5108
- cursor = 0;
5134
+ /**
5135
+ * Last account handed out per region, for round-robin.
5136
+ *
5137
+ * Both region shims share one pool, and filtering to the caller's region
5138
+ * before indexing used to make each region renormalise the same cursor
5139
+ * against its own length: with two accounts per region, alternating turns
5140
+ * served the same account in each region and never reached the others.
5141
+ * A stored id also survives the pool shrinking between picks: an account
5142
+ * whose refresh fails is excluded from the retry, and a numeric position
5143
+ * would then point at an account that already had its turn.
5144
+ */
5145
+ lastServed = /* @__PURE__ */ new Map();
5109
5146
  /** Sticky mode: conversation key → account id. Content stays out of memory. */
5110
5147
  sticky = /* @__PURE__ */ new Map();
5111
5148
  /**
5112
5149
  * Account id → the order in which it last served. A readout only: the composer
5113
5150
  * shows the account a sticky conversation is on, and that account is the one
5114
5151
  * that just answered. A counter rather than a clock, because two acquires can
5115
- * land in the same millisecond. Not persisted; after a restart the cursor
5116
- * stands in.
5152
+ * land in the same millisecond. Not persisted; after a restart the rotation
5153
+ * starts over.
5117
5154
  */
5118
5155
  servedOrder = /* @__PURE__ */ new Map();
5119
5156
  servedCount = 0;
5120
5157
  lastScanAtMs = 0;
5121
5158
  preferredIds = /* @__PURE__ */ new Map();
5122
5159
  /**
5123
- * Account ids the user removed from the pool for good.
5160
+ * Account ids the user removed from the pool.
5124
5161
  *
5125
5162
  * Enforced while scan() assembles its result, so a removed account cannot
5126
5163
  * re-enter the pool when the desktop app rewrites its sign-in file. That is
@@ -5423,30 +5460,34 @@ var WorkBuddyAccountPool = class {
5423
5460
  current: true
5424
5461
  }, deletingCredential);
5425
5462
  }
5426
- const onDisk = await readCredential(ownedPath);
5427
- if (!replaced) replaced = onDisk.kind === "sealed" || onDisk.kind === "unreadable" || onDisk.kind === "credential" && (workbuddyAccountId(onDisk.credential) !== account.id || !sameCredential(onDisk.credential));
5428
- if (replaced) {
5429
- await fail();
5430
- return false;
5431
- }
5432
5463
  try {
5433
- const tombstoneFile = this.deletedSignInsFile();
5434
- if (tombstoneFile === void 0) await unlink(ownedPath);
5435
- else await withFileLock(`${tombstoneFile}.lock`, async () => {
5436
- await unlink(ownedPath);
5464
+ await withSignInWriteLock(ownedPath, async () => {
5465
+ const onDisk = await readCredential(ownedPath);
5466
+ if (!replaced) replaced = onDisk.kind === "sealed" || onDisk.kind === "unreadable" || onDisk.kind === "credential" && (workbuddyAccountId(onDisk.credential) !== account.id || !sameCredential(onDisk.credential));
5467
+ if (replaced) return;
5468
+ try {
5469
+ const tombstoneFile = this.deletedSignInsFile();
5470
+ if (tombstoneFile === void 0) await unlink(ownedPath);
5471
+ else await withFileLock(`${tombstoneFile}.lock`, async () => {
5472
+ await unlink(ownedPath);
5473
+ });
5474
+ } catch (error) {
5475
+ if (!(error instanceof FileLockReleaseError) && error.code !== "ENOENT") throw error;
5476
+ }
5477
+ await removeAbandonedTemps(ownedPath, 0);
5437
5478
  });
5438
5479
  } catch (error) {
5439
- const code = error.code;
5440
- if (!(error instanceof FileLockReleaseError) && code !== "ENOENT") {
5441
- await fail();
5442
- this.rosterRevision += 1;
5443
- await this.rescan().catch((rescanError) => {
5444
- this.logger?.warn?.(`${LOG_PREFIX}: could not restore the roster after a failed delete: ${String(rescanError)}`);
5445
- });
5446
- throw error;
5447
- }
5480
+ await fail();
5481
+ this.rosterRevision += 1;
5482
+ await this.rescan().catch((rescanError) => {
5483
+ this.logger?.warn?.(`${LOG_PREFIX}: could not restore the roster after a failed delete: ${String(rescanError)}`);
5484
+ });
5485
+ throw error;
5486
+ }
5487
+ if (replaced) {
5488
+ await fail();
5489
+ return false;
5448
5490
  }
5449
- await removeAbandonedTemps(ownedPath, 0);
5450
5491
  this.rosterRevision += 1;
5451
5492
  const archivedIds = this.storedIds(account.id);
5452
5493
  if (!settled) running.catch(() => {}).then(() => this.forgetArchived(archivedIds));
@@ -5754,8 +5795,8 @@ var WorkBuddyAccountPool = class {
5754
5795
  if (file === void 0) return;
5755
5796
  const run = this.archiveQueue.then(async () => {
5756
5797
  await withFileLock(`${file}.lock`, async () => {
5757
- const stored = await readArchivedCredentials(file);
5758
- let dropped = false;
5798
+ const { stored, pruned } = await readArchivedCredentials(file);
5799
+ let dropped = pruned;
5759
5800
  for (const id of ids) dropped = stored.delete(id) || dropped;
5760
5801
  if (!dropped) return;
5761
5802
  await writeArchivedCredentials(file, stored);
@@ -5833,11 +5874,11 @@ var WorkBuddyAccountPool = class {
5833
5874
  const run = this.archiveQueue.then(async () => {
5834
5875
  await withFileLock(`${file}.lock`, async () => {
5835
5876
  if (signal?.aborted) return;
5836
- const stored = await readArchivedCredentials(file);
5877
+ const { stored, pruned } = await readArchivedCredentials(file);
5837
5878
  if (signal?.aborted) return;
5838
5879
  const tombstones = await this.loadDeletedSignIns(signal);
5839
5880
  if (tombstones === void 0) return;
5840
- let changed = false;
5881
+ let changed = pruned;
5841
5882
  for (const credential of credentials) {
5842
5883
  const id = workbuddyAccountId(credential);
5843
5884
  const entry = tombstoneOf(tombstones, credential, id);
@@ -5865,20 +5906,22 @@ var WorkBuddyAccountPool = class {
5865
5906
  }
5866
5907
  }
5867
5908
  /** Credentials the archive kept for a directory this walk can read. */
5868
- async archivedCredentials(homes) {
5909
+ async archivedCredentials(homes, signal) {
5869
5910
  const file = this.archiveFile();
5870
5911
  if (file === void 0) return [];
5912
+ let stored = /* @__PURE__ */ new Map();
5871
5913
  try {
5872
- let stored = /* @__PURE__ */ new Map();
5873
5914
  await withFileLock(`${file}.lock`, async () => {
5915
+ if (signal?.aborted) return;
5874
5916
  await removeAbandonedTemps(file);
5875
- stored = await readArchivedCredentials(file);
5917
+ const archive = await readArchivedCredentials(file);
5918
+ stored = archive.stored;
5919
+ if (archive.pruned) await writeArchivedCredentials(file, stored, signal);
5876
5920
  });
5877
- return [...stored.values()].filter((credential) => homes.has(this.homeOf(credential)));
5878
5921
  } catch (error) {
5879
- this.logger?.warn?.(`${LOG_PREFIX}: could not read the credential archive: ${String(error)}`);
5880
- return [];
5922
+ if (!signal?.aborted) this.logger?.warn?.(`${LOG_PREFIX}: could not read or prune the credential archive: ${String(error)}`);
5881
5923
  }
5924
+ return [...stored.values()].filter((credential) => homes.has(this.homeOf(credential)));
5882
5925
  }
5883
5926
  /** The auth directory a credential was read from. */
5884
5927
  homeOf(credential) {
@@ -6008,7 +6051,7 @@ var WorkBuddyAccountPool = class {
6008
6051
  const refreshBlockedIds = /* @__PURE__ */ new Set();
6009
6052
  for (const [file, id] of appHeld) if (sealedFiles.has(file) || unreadableFiles.has(file) || unavailableDirs.has(resolve(dirname(file)))) refreshBlockedIds.add(id);
6010
6053
  for (const account of this.accounts) if (sealedFiles.has(resolve(account.credential.sourcePath)) || unreadableFiles.has(resolve(account.credential.sourcePath)) || unavailableDirs.has(this.homeOf(account.credential))) refreshBlockedIds.add(account.id);
6011
- const archived = await this.archivedCredentials(readableDirs);
6054
+ const archived = await this.archivedCredentials(readableDirs, signal);
6012
6055
  if (signal.aborted || rosterRevision !== this.rosterRevision) return this.accounts;
6013
6056
  for (const credential of archived) {
6014
6057
  const id = workbuddyAccountId(credential);
@@ -6243,7 +6286,7 @@ var WorkBuddyAccountPool = class {
6243
6286
  const preferredId = this.preferredIdFor(region);
6244
6287
  const remaining = new Set(pool.map((account) => account.id));
6245
6288
  while (remaining.size > 0) {
6246
- const account = pool.find((item) => item.id === preferredId) ?? this.pick(pool, conversationKey);
6289
+ const account = pool.find((item) => item.id === preferredId) ?? this.pick(pool, conversationKey, region);
6247
6290
  if (account === void 0) return void 0;
6248
6291
  remaining.delete(account.id);
6249
6292
  await this.ensureFresh(account);
@@ -6256,16 +6299,21 @@ var WorkBuddyAccountPool = class {
6256
6299
  pool = eligible.filter((item) => remaining.has(item.id));
6257
6300
  }
6258
6301
  }
6259
- pick(pool, conversationKey) {
6302
+ pick(pool, conversationKey, region) {
6260
6303
  if (this.distribution === "expiry") return this.pickByExpiry(pool);
6261
6304
  if (this.distribution === "sticky" && conversationKey !== void 0) {
6262
6305
  const bound = this.sticky.get(conversationKey);
6263
6306
  const kept = bound === void 0 ? void 0 : pool.find((account) => account.id === bound);
6264
- if (kept !== void 0) return kept;
6265
- const account = pool[this.cursor % pool.length];
6307
+ if (kept !== void 0) {
6308
+ this.sticky.delete(conversationKey);
6309
+ this.sticky.set(conversationKey, kept.id);
6310
+ return kept;
6311
+ }
6312
+ const account = this.successor(pool, this.lastServedFor(region));
6266
6313
  if (account === void 0) return void 0;
6314
+ this.sticky.delete(conversationKey);
6267
6315
  this.sticky.set(conversationKey, account.id);
6268
- this.cursor = (this.cursor + 1) % pool.length;
6316
+ this.setLastServed(region, account.id);
6269
6317
  while (this.sticky.size > 200) {
6270
6318
  const oldest = this.sticky.keys().next().value;
6271
6319
  if (oldest === void 0) break;
@@ -6273,37 +6321,63 @@ var WorkBuddyAccountPool = class {
6273
6321
  }
6274
6322
  return account;
6275
6323
  }
6276
- const index = this.distribution === "round-robin" ? this.cursor % pool.length : 0;
6277
- const account = pool[index];
6278
- if (account !== void 0 && this.distribution === "round-robin") this.cursor = (index + 1) % pool.length;
6324
+ const account = this.distribution === "round-robin" ? this.successor(pool, this.lastServedFor(region)) : pool[0];
6325
+ if (account !== void 0 && this.distribution === "round-robin") this.setLastServed(region, account.id);
6279
6326
  return account;
6280
6327
  }
6328
+ /**
6329
+ * The first account after `previous` in roster order that is still in `pool`.
6330
+ *
6331
+ * `previous` can be missing from `pool` because this request already skipped
6332
+ * it: a sign-in whose refresh failed is excluded from the retry. Walking the
6333
+ * full roster keeps the rotation moving past it; looking only inside `pool`
6334
+ * would restart at its head and retry the same account every turn while the
6335
+ * accounts behind the skipped one never served.
6336
+ */
6337
+ successor(pool, previous) {
6338
+ if (pool.length === 0) return void 0;
6339
+ const start = previous === void 0 ? -1 : this.accounts.findIndex((account) => account.id === previous);
6340
+ if (start === -1) return pool[0];
6341
+ const present = new Set(pool.map((account) => account.id));
6342
+ for (let step = 1; step <= this.accounts.length; step += 1) {
6343
+ const candidate = this.accounts[(start + step) % this.accounts.length];
6344
+ if (candidate !== void 0 && present.has(candidate.id)) return candidate;
6345
+ }
6346
+ return pool[0];
6347
+ }
6348
+ /** The last account served through one region; absent regions share one. */
6349
+ lastServedFor(region) {
6350
+ return this.lastServed.get(region ?? "");
6351
+ }
6352
+ setLastServed(region, accountId) {
6353
+ this.lastServed.set(region ?? "", accountId);
6354
+ }
6281
6355
  pickByExpiry(pool) {
6282
6356
  return pickSoonestExpiry(pool, this.creditExpiries, Date.now());
6283
6357
  }
6284
6358
  /**
6285
- * Next account the panel may name as "next up". Does not move the cursor.
6359
+ * Next account the panel may name as "next up". Does not move the rotation.
6286
6360
  *
6287
6361
  * Sticky has no single next account - it is per conversation - so it names
6288
6362
  * nothing rather than pointing at a guess. The composer readout uses
6289
6363
  * `readoutPreview`, which can name the account that served last instead.
6290
6364
  */
6291
- preview(eligible) {
6365
+ preview(eligible, region) {
6292
6366
  if (eligible.length === 0) return void 0;
6293
6367
  if (this.distribution === "expiry") return this.pickByExpiry(eligible);
6294
6368
  if (this.distribution === "priority") return eligible[0];
6295
- if (this.distribution === "round-robin") return eligible[this.cursor % eligible.length];
6369
+ if (this.distribution === "round-robin") return this.successor(eligible, this.lastServedFor(region));
6296
6370
  }
6297
6371
  /**
6298
- * The account the composer readout names. Does not move the cursor.
6372
+ * The account the composer readout names. Does not move the rotation.
6299
6373
  *
6300
6374
  * Round-robin names the account the next request takes. Sticky names the one
6301
6375
  * that served most recently, because the current conversation is most likely
6302
- * on it; before the first turn the cursor stands in.
6376
+ * on it; before the first turn the rotation stands in.
6303
6377
  */
6304
- readoutPreview(eligible) {
6378
+ readoutPreview(eligible, region) {
6305
6379
  if (eligible.length === 0) return void 0;
6306
- if (this.distribution !== "sticky") return this.preview(eligible);
6380
+ if (this.distribution !== "sticky") return this.preview(eligible, region);
6307
6381
  let latest;
6308
6382
  let latestAt = -1;
6309
6383
  for (const account of eligible) {
@@ -6313,7 +6387,7 @@ var WorkBuddyAccountPool = class {
6313
6387
  latestAt = servedAt;
6314
6388
  }
6315
6389
  }
6316
- return latest ?? eligible[this.cursor % eligible.length];
6390
+ return latest ?? this.successor(eligible, this.lastServedFor(region));
6317
6391
  }
6318
6392
  /** Prefer one billing account per gateway; undefined restores automatic use. */
6319
6393
  prefer(accountId, region) {
@@ -6852,8 +6926,6 @@ var SseDecoder = class {
6852
6926
  onFrame;
6853
6927
  buffer = "";
6854
6928
  event;
6855
- id;
6856
- retry;
6857
6929
  data = [];
6858
6930
  dataChars = 0;
6859
6931
  rawFrame = "";
@@ -6924,23 +6996,18 @@ var SseDecoder = class {
6924
6996
  else if (field === "data") {
6925
6997
  this.data.push(value);
6926
6998
  this.dataChars += value.length + 1;
6927
- } else if (field === "id" && !value.includes("\0")) this.id = value;
6928
- else if (field === "retry" && /^\d+$/.test(value)) this.retry = Number(value);
6999
+ }
6929
7000
  }
6930
7001
  dispatch() {
6931
7002
  if (this.data.length === 0) {
6932
7003
  this.event = void 0;
6933
- this.retry = void 0;
6934
7004
  return;
6935
7005
  }
6936
7006
  const result = {
6937
7007
  ...this.event === void 0 || this.event === "" ? {} : { event: this.event },
6938
- data: this.data.join("\n"),
6939
- ...this.id === void 0 ? {} : { id: this.id },
6940
- ...this.retry === void 0 ? {} : { retry: this.retry }
7008
+ data: this.data.join("\n")
6941
7009
  };
6942
7010
  this.event = void 0;
6943
- this.retry = void 0;
6944
7011
  this.data = [];
6945
7012
  this.dataChars = 0;
6946
7013
  return result;
@@ -7151,6 +7218,18 @@ function conversationKeyOf(raw) {
7151
7218
  return;
7152
7219
  }
7153
7220
  }
7221
+ /**
7222
+ * The upstream a sign-in's chat traffic goes to.
7223
+ *
7224
+ * Transport failures are a property of the host, not of the account: a pool can
7225
+ * hold sign-ins served by different gateways, and one unreachable host says
7226
+ * nothing about the next one. The sign-in domain is not the chat host (every CN
7227
+ * spelling lands on one gateway, and each international brand has its own), so
7228
+ * the host is read from the same mapping the request itself uses.
7229
+ */
7230
+ function upstreamHostOf(account) {
7231
+ return chatHostOf(account.credential);
7232
+ }
7154
7233
  const EMPTY_STREAM_REFUSAL = "workbuddy returned an empty answer stream";
7155
7234
  /**
7156
7235
  * Read enough of a 200 body to tell an SSE stream from a refusal wearing a 200.
@@ -7412,7 +7491,8 @@ function createWorkBuddyShim(options) {
7412
7491
  res.on("close", () => controller.abort());
7413
7492
  const raw = (await readRequestBody(req)).toString("utf8");
7414
7493
  const prepared = client.prepareChatBody(raw);
7415
- const conversationKey = conversationKeyOf(raw);
7494
+ const conversationHash = conversationKeyOf(raw);
7495
+ const conversationKey = conversationHash === void 0 || region === void 0 ? conversationHash : `${region}\n${conversationHash}`;
7416
7496
  let modelId;
7417
7497
  let bodyIsJson = true;
7418
7498
  try {
@@ -7437,14 +7517,52 @@ function createWorkBuddyShim(options) {
7437
7517
  let last;
7438
7518
  let accountSkipped = false;
7439
7519
  let transportAttempts = 0;
7520
+ /** Hosts that failed transport in this request; one failure condemns none of the others. */
7521
+ const transportHosts = /* @__PURE__ */ new Set();
7522
+ /** Whether any eligible account this request has not tried belongs to a host that has not failed. */
7523
+ const hasUntriedHost = () => {
7524
+ for (const peer of pool.available(Date.now(), modelId, region)) if (!skipped.has(peer.id) && !transportHosts.has(upstreamHostOf(peer))) return true;
7525
+ return false;
7526
+ };
7527
+ /**
7528
+ * Stop trying accounts served by a host that has already timed out.
7529
+ *
7530
+ * Used only once the transport cap is reached and another host is still
7531
+ * waiting: the first failure gets one retry, and after that a peer on the
7532
+ * same host only pays the same timeout again while a healthy account behind
7533
+ * it waits. A single-host pool never reaches this branch because no other
7534
+ * host is left to justify it.
7535
+ */
7536
+ const skipHost = (host) => {
7537
+ for (const peer of pool.list(region)) if (upstreamHostOf(peer) === host) skipped.add(peer.id);
7538
+ };
7539
+ /**
7540
+ * Record one transport failure and answer whether the turn is spent.
7541
+ *
7542
+ * Up to the cap the next account gets its turn, including a peer on the
7543
+ * same host. Past the cap another host still waiting is what keeps the turn
7544
+ * alive, and the failed host's remaining peers are skipped so the healthy
7545
+ * host is reached without paying their timeouts first.
7546
+ */
7547
+ const transportSpent = (host) => {
7548
+ transportHosts.add(host);
7549
+ transportAttempts += 1;
7550
+ if (transportAttempts < MAX_TRANSPORT_ATTEMPTS) return false;
7551
+ if (!hasUntriedHost()) return true;
7552
+ skipHost(host);
7553
+ return false;
7554
+ };
7440
7555
  let emptyStreams = 0;
7556
+ const refreshedSessions = /* @__PURE__ */ new Set();
7557
+ let retryAccountId;
7441
7558
  let effortFallback;
7442
7559
  let effortFallbackBuilt = false;
7443
- const maxAttempts = Math.max(8, pool.list(region).length + 2);
7560
+ const maxAttempts = Math.max(8, pool.list(region).length * 2 + 2);
7444
7561
  const maxEmptyStreams = emptyStreamAttemptsFor(pool.list(region).length);
7445
7562
  for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
7446
7563
  if (controller.signal.aborted) return;
7447
- const account = await pool.acquire(modelId, region, conversationKey, skipped);
7564
+ 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);
7565
+ retryAccountId = void 0;
7448
7566
  if (account === void 0) {
7449
7567
  if (accountSkipped && last !== void 0) {
7450
7568
  const model = ` for model ${modelId}`;
@@ -7478,7 +7596,7 @@ function createWorkBuddyShim(options) {
7478
7596
  tried += 1;
7479
7597
  let result = await client.chatStream(account.credential, prepared, controller.signal);
7480
7598
  let refreshOutcome;
7481
- if (!result.ok && result.kind === "session_dead") {
7599
+ if (!result.ok && result.kind === "session_dead" && !refreshedSessions.has(account.id)) {
7482
7600
  const failure = result;
7483
7601
  refreshOutcome = await pool.refreshAccountOutcome(account.id);
7484
7602
  if (refreshOutcome === "failed") {
@@ -7494,6 +7612,7 @@ function createWorkBuddyShim(options) {
7494
7612
  }
7495
7613
  }
7496
7614
  if (refreshOutcome === "refreshed") {
7615
+ refreshedSessions.add(account.id);
7497
7616
  if (controller.signal.aborted) return;
7498
7617
  result = await client.chatStream(account.credential, prepared, controller.signal);
7499
7618
  }
@@ -7550,7 +7669,7 @@ function createWorkBuddyShim(options) {
7550
7669
  */
7551
7670
  const failInBand = (error) => {
7552
7671
  if (controller.signal.aborted || !res.writable) return;
7553
- logger?.warn?.("wtq-pool: upstream stream failed mid-flight", safeMessage(error));
7672
+ logger?.warn?.("wtq-pool: upstream stream failed mid-flight", error instanceof Error ? error.name : "unknown error");
7554
7673
  try {
7555
7674
  res.write(`data: ${JSON.stringify({ error: {
7556
7675
  message: safeMessage(error),
@@ -7607,14 +7726,14 @@ function createWorkBuddyShim(options) {
7607
7726
  head = peeked.head;
7608
7727
  ended = peeked.ended;
7609
7728
  if (peeked.refusal !== void 0) {
7610
- await reader.cancel().catch(() => {});
7729
+ reader.cancel().catch(() => {});
7611
7730
  if (peeked.ended && peeked.refusal === EMPTY_STREAM_REFUSAL) {
7612
7731
  headFailure = peeked.refusal;
7613
7732
  headIsEmptyStream = true;
7614
7733
  logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} answered 200 with an empty stream; rotating`);
7615
7734
  return;
7616
7735
  }
7617
- logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} answered 200 with no stream: ${peeked.refusal}`);
7736
+ logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} answered 200 with no stream`);
7618
7737
  if (peeked.code === "11128" || unapprovedChannelRefusal(peeked.refusal) !== void 0) {
7619
7738
  writeOpenAIError(res, 400, "upstream_channel_check", channelCheckRefusal());
7620
7739
  return;
@@ -7635,15 +7754,25 @@ function createWorkBuddyShim(options) {
7635
7754
  pool.penalize(account.id, resetAtMs, modelId);
7636
7755
  } else if (envelopeKind === "model_tier") pool.blockModel(account.id, modelId, Date.now() + MODEL_TIER_HOLD_MS);
7637
7756
  else if (envelopeKind === "session_dead") {
7638
- const refreshOutcome = await pool.refreshAccountOutcome(account.id);
7639
- if (refreshOutcome === "refused") pool.penalize(account.id, holdUntil(), void 0, "session");
7640
- else if (refreshOutcome === "failed") {
7757
+ if (refreshedSessions.has(account.id)) {
7758
+ pool.penalize(account.id, holdUntil(), void 0, "session");
7641
7759
  skipped.add(account.id);
7642
- last = {
7643
- kind: "transport",
7644
- status: 502,
7645
- message: `could not refresh the sign-in: ${peeked.refusal.slice(0, 200)}`
7646
- };
7760
+ } else {
7761
+ const refreshOutcome = await pool.refreshAccountOutcome(account.id);
7762
+ if (refreshOutcome === "refused") {
7763
+ pool.penalize(account.id, holdUntil(), void 0, "session");
7764
+ skipped.add(account.id);
7765
+ } else if (refreshOutcome === "failed") {
7766
+ skipped.add(account.id);
7767
+ last = {
7768
+ kind: "transport",
7769
+ status: 502,
7770
+ message: `could not refresh the sign-in: ${peeked.refusal.slice(0, 200)}`
7771
+ };
7772
+ } else {
7773
+ refreshedSessions.add(account.id);
7774
+ retryAccountId = account.id;
7775
+ }
7647
7776
  }
7648
7777
  } else pool.penalize(account.id, holdUntil(), void 0, envelopeKind === "hard_credit" ? "spent" : "restricted");
7649
7778
  return;
@@ -7702,10 +7831,7 @@ function createWorkBuddyShim(options) {
7702
7831
  if (headIsEmptyStream) {
7703
7832
  emptyStreams += 1;
7704
7833
  spent = emptyStreams >= maxEmptyStreams;
7705
- } else {
7706
- transportAttempts += 1;
7707
- spent = transportAttempts >= MAX_TRANSPORT_ATTEMPTS;
7708
- }
7834
+ } else spent = transportSpent(upstreamHostOf(account));
7709
7835
  if (spent) break;
7710
7836
  continue;
7711
7837
  }
@@ -7757,8 +7883,7 @@ function createWorkBuddyShim(options) {
7757
7883
  };
7758
7884
  accountSkipped = true;
7759
7885
  skipped.add(account.id);
7760
- transportAttempts += 1;
7761
- if (transportAttempts >= MAX_TRANSPORT_ATTEMPTS) break;
7886
+ if (transportSpent(upstreamHostOf(account))) break;
7762
7887
  logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} transport failure; rotating`);
7763
7888
  continue;
7764
7889
  }
@@ -8718,10 +8843,6 @@ function traeIdentityHeaders(identity) {
8718
8843
  "x-app-version": identity.appVersion,
8719
8844
  "x-ide-version": identity.appVersion
8720
8845
  },
8721
- ...identity.buildVersion === void 0 ? {} : {
8722
- "x-app-version-code": identity.buildVersion,
8723
- "x-ide-version-code": identity.buildVersion
8724
- },
8725
8846
  "x-ide-version-type": "stable"
8726
8847
  };
8727
8848
  }
@@ -8729,15 +8850,15 @@ function traeIdentityHeaders(identity) {
8729
8850
  * Headers shared by every Trae edition. The same shape is accepted by both
8730
8851
  * gateways (identical `x-app-id`, identity headers, and `Cloud-IDE-JWT` auth).
8731
8852
  */
8732
- function buildTraeHeaders(credential, identity, options = {}) {
8733
- const requestId = options.requestId ?? randomUUID();
8853
+ function buildTraeHeaders(credential, identity) {
8854
+ const requestId = randomUUID();
8734
8855
  const traceId = requestId.replaceAll("-", "").slice(0, 32);
8735
8856
  return {
8736
8857
  "Authorization": `Cloud-IDE-JWT ${credential.accessToken}`,
8737
8858
  "X-Ide-Token": credential.accessToken,
8738
8859
  "x-plugin-channel": "icube-ai",
8739
8860
  "User-Agent": `Trae/${identity.appVersion ?? identity.buildVersion ?? "unknown"}`,
8740
- "x-app-id": options.appId ?? "6eefa01c-1036-4c7e-9ca5-d891f63bfcd8",
8861
+ "x-app-id": "6eefa01c-1036-4c7e-9ca5-d891f63bfcd8",
8741
8862
  ...traeIdentityHeaders(identity),
8742
8863
  "x-app-version-code": normalizeTraeVersionCode(identity.buildVersion),
8743
8864
  "x-ide-version-code": normalizeTraeVersionCode(identity.buildVersion),
@@ -9416,12 +9537,13 @@ var TraeSoloUpstreamClient = class {
9416
9537
  }
9417
9538
  if (response.ok) return {
9418
9539
  ok: true,
9419
- response: response.body === null ? response : new Response(withIdleWatchdog(response.body, timeouts?.idleMs, signal), {
9540
+ response: response.body === null ? response : new Response(withIdleWatchdog(response.body, timeouts?.idleMs, signal, deadline.abort), {
9420
9541
  status: response.status,
9421
9542
  headers: response.headers
9422
9543
  })
9423
9544
  };
9424
9545
  const text = await readErrorBody(response, 1024, signal);
9546
+ deadline.abort();
9425
9547
  return {
9426
9548
  ok: false,
9427
9549
  status: response.status,
@@ -9921,7 +10043,7 @@ async function openTraeResponse(response, model, options = {}) {
9921
10043
  throw error;
9922
10044
  }
9923
10045
  if (failure !== void 0 && !sawOutput) {
9924
- await reader.cancel().catch(() => {});
10046
+ reader.cancel().catch(() => {});
9925
10047
  const { status, kind, retryAfterSeconds } = classifyTraeFailure(failure);
9926
10048
  const wait = retryAfterHeader(retryAfterSeconds);
9927
10049
  return Response.json({ error: {
@@ -10130,7 +10252,8 @@ function createTraeGateway(options) {
10130
10252
  * a subscription gate), which is what the next sign-in is for.
10131
10253
  */
10132
10254
  const attemptOnce = async (credential, functionName) => {
10133
- const result = await clientFor(credential).chatStream(raw, watcher.signal, functionName, capabilities.get(model), void 0, published !== void 0 && serving !== void 0 && serving.contextWindow !== void 0 && published > serving.contextWindow);
10255
+ const variant = entry?.variants?.find((row) => row.function === functionName) ?? serving;
10256
+ const result = await clientFor(credential).chatStream(raw, watcher.signal, functionName, capabilities.get(model), void 0, published !== void 0 && variant?.contextWindow !== void 0 && published > variant.contextWindow);
10134
10257
  if (!result.ok) return {
10135
10258
  ok: false,
10136
10259
  status: result.status,
@@ -10141,7 +10264,8 @@ function createTraeGateway(options) {
10141
10264
  let relayed;
10142
10265
  try {
10143
10266
  relayed = await openTraeResponse(result.response, model, { onFailure: (error) => {
10144
- logger?.warn?.("wtq-pool: trae relay stream failed mid-flight", error);
10267
+ const failure = error.traeFailure;
10268
+ logger?.warn?.("wtq-pool: trae relay stream failed mid-flight", failure?.code === void 0 ? error instanceof Error ? error.name : "unknown error" : `code ${failure.code}`);
10145
10269
  if (!(error instanceof Error && error.name === "AbortError") && !watcher.signal.aborted) {
10146
10270
  const failure = error.traeFailure;
10147
10271
  if (failure !== void 0) {
@@ -10222,10 +10346,6 @@ function createTraeGateway(options) {
10222
10346
  return;
10223
10347
  }
10224
10348
  const relayed = opened.relayed;
10225
- if (relayed.body === null) {
10226
- writeOpenAIError(res, 502, "upstream_error", "Trae SOLO returned no stream");
10227
- return;
10228
- }
10229
10349
  if (watcher.signal.aborted) {
10230
10350
  relayStreamOf(relayed).cancel().catch(() => {});
10231
10351
  return;
@@ -10238,7 +10358,7 @@ function createTraeGateway(options) {
10238
10358
  });
10239
10359
  res.flushHeaders();
10240
10360
  pumpToResponse(relayStreamOf(relayed), res).catch((error) => {
10241
- if (!(watcher.signal.aborted || error instanceof Error && error.name === "AbortError")) logger?.warn?.("wtq-pool: trae relay stream failed mid-flight", error);
10361
+ if (!(watcher.signal.aborted || error instanceof Error && error.name === "AbortError")) logger?.warn?.("wtq-pool: trae relay stream failed mid-flight", error instanceof Error ? error.name : "unknown error");
10242
10362
  if (res.writable) res.end();
10243
10363
  });
10244
10364
  }
@@ -10323,10 +10443,23 @@ function osVersionOf(platform) {
10323
10443
  function deviceCpuOf() {
10324
10444
  return nonEmpty(cpus()[0]?.model.split(" ")[0]);
10325
10445
  }
10326
- /** `x-device-id` when exactly one device-center id is persisted. */
10446
+ /**
10447
+ * `x-device-id`: the device-center number the official clients send.
10448
+ *
10449
+ * That value is the numeric suffix of `iCubeAuthInfo://icube-dc:<id>`. The app
10450
+ * also leaves entries of another shape behind — for example an `aha-…` key it
10451
+ * writes when it migrates its device id — and those are not device numbers, so
10452
+ * they are never what this header wants. One numeric id beside such leftovers
10453
+ * is still the machine's own and must be used; two numeric ids are two sign-ins
10454
+ * with no way to tell which one this account owns, so the caller still has to
10455
+ * fall back. Keeping the single-entry case shape-blind preserves installs that
10456
+ * persist no numeric id at all.
10457
+ */
10327
10458
  function traeDeviceCenterId(storage) {
10328
10459
  const ids = Object.keys(storage).filter((key) => key.startsWith(TRAE_DEVICE_CENTER_PREFIX)).map((key) => key.slice(25)).filter(Boolean);
10329
- return ids.length === 1 ? ids[0] : void 0;
10460
+ const numeric = ids.filter((id) => /^\d+$/.test(id));
10461
+ if (numeric.length === 1) return numeric[0];
10462
+ if (numeric.length === 0 && ids.length === 1) return ids[0];
10330
10463
  }
10331
10464
  async function readJsonFile(path) {
10332
10465
  try {
@@ -10601,6 +10734,19 @@ function decryptQoderOscrypt(blob, key) {
10601
10734
  return;
10602
10735
  }
10603
10736
  }
10737
+ /** Chromium's macOS OSCrypt uses a fixed IV of sixteen ASCII spaces. */
10738
+ const MAC_OSCRYPT_IV = Buffer.alloc(16, 32);
10739
+ /** A macOS `v10` blob stores the prefix followed by AES-CBC ciphertext. */
10740
+ function decryptQoderOscryptMac(blob, key) {
10741
+ if (blob.length < 19) return void 0;
10742
+ if (blob.subarray(0, 3).toString("latin1") !== "v10") return void 0;
10743
+ try {
10744
+ const decipher = createDecipheriv("aes-128-cbc", key, MAC_OSCRYPT_IV);
10745
+ return Buffer.concat([decipher.update(blob.subarray(3)), decipher.final()]).toString("utf8");
10746
+ } catch {
10747
+ return;
10748
+ }
10749
+ }
10604
10750
  //#endregion
10605
10751
  //#region src/qoder-credentials.ts
10606
10752
  /**
@@ -10699,7 +10845,7 @@ async function unwrapQoderOscryptKey(localStatePath, options = {}) {
10699
10845
  } catch {}
10700
10846
  const cached = oscryptKeyCache.get(localStatePath);
10701
10847
  if (cached !== void 0 && cached.mtimeMs === mtimeMs) return Buffer.from(cached.key);
10702
- const exe = trustedWindowsPowerShell(env);
10848
+ const exe = trustedWindowsPowerShell();
10703
10849
  if (exe === void 0) throw new QoderKeyError("unavailable", "SystemRoot is not set, cannot locate PowerShell");
10704
10850
  const stdout = await new Promise((resolve, reject) => {
10705
10851
  const child = spawn(exe, [
@@ -10708,12 +10854,9 @@ async function unwrapQoderOscryptKey(localStatePath, options = {}) {
10708
10854
  "-Command",
10709
10855
  DPAPI_SCRIPT
10710
10856
  ], {
10711
- env: trustedWindowsChildEnv(env, {
10712
- inheritTemp: true,
10713
- includeSystemDrive: true
10714
- }),
10857
+ env: trustedWindowsChildEnv(env),
10715
10858
  cwd: (() => {
10716
- const root = trustedSystemRootOf(env);
10859
+ const root = trustedSystemRootOf();
10717
10860
  return root === void 0 ? void 0 : join(root, "System32");
10718
10861
  })(),
10719
10862
  windowsHide: true,
@@ -10811,16 +10954,82 @@ async function firstAuthBlob(paths) {
10811
10954
  return await readFile(path);
10812
10955
  } catch {}
10813
10956
  }
10957
+ /** Keychain service names for the current and older Qoder builds. */
10958
+ const QODER_KEYCHAIN_SERVICES = {
10959
+ cn: [
10960
+ "Qoder CN App Safe Storage",
10961
+ "Qoder CN Safe Storage",
10962
+ "QoderWork CN Safe Storage"
10963
+ ],
10964
+ global: [
10965
+ "Qoder App Safe Storage",
10966
+ "Qoder Safe Storage",
10967
+ "QoderWork Safe Storage"
10968
+ ]
10969
+ };
10970
+ /**
10971
+ * PBKDF2 parameters Chromium's macOS OSCrypt uses to turn the Keychain
10972
+ * password into the 16-byte AES-128 key. Fixed by Chromium, not secret.
10973
+ */
10974
+ const MAC_OSCRYPT_SALT = "saltysalt";
10975
+ const MAC_OSCRYPT_ITERATIONS = 1003;
10976
+ /** Derived macOS keys, keyed on the Keychain service they came from. */
10977
+ const darwinKeyCache = /* @__PURE__ */ new Map();
10978
+ /** Pin the system helper and bound the wait for a Keychain prompt. */
10979
+ function darwinKeychainPassword(service) {
10980
+ return new Promise((resolve) => {
10981
+ execFile("/usr/bin/security", [
10982
+ "find-generic-password",
10983
+ "-s",
10984
+ service,
10985
+ "-w"
10986
+ ], { timeout: 8e3 }, (error, stdout) => {
10987
+ resolve(error ? void 0 : stdout.trim() || void 0);
10988
+ });
10989
+ });
10990
+ }
10991
+ /** One app directory's credential on macOS, or the reason there is none. */
10992
+ async function readQoderCredentialMac(authBlob, region, appDir) {
10993
+ const base = { region };
10994
+ let decrypted = false;
10995
+ for (const service of QODER_KEYCHAIN_SERVICES[region]) {
10996
+ let key = darwinKeyCache.get(service);
10997
+ if (key === void 0) {
10998
+ const password = await darwinKeychainPassword(service);
10999
+ if (password === void 0) continue;
11000
+ key = pbkdf2Sync(password, MAC_OSCRYPT_SALT, MAC_OSCRYPT_ITERATIONS, 16, "sha1");
11001
+ darwinKeyCache.set(service, key);
11002
+ }
11003
+ const plain = decryptQoderOscryptMac(authBlob, key);
11004
+ if (plain === void 0) continue;
11005
+ decrypted = true;
11006
+ const session = parseQoderAuthSession(plain);
11007
+ if (session === void 0) continue;
11008
+ const machineID = await machineIdOf(appDir);
11009
+ return { credential: {
11010
+ ...base,
11011
+ ...session,
11012
+ ...machineID === void 0 ? {} : { machineID }
11013
+ } };
11014
+ }
11015
+ return { failure: {
11016
+ ...base,
11017
+ reason: decrypted ? "invalid" : "no-key",
11018
+ ...decrypted ? {} : { message: "no macOS Keychain key opened the sign-in (sign in to the Qoder app, then allow the Keychain prompt and rescan)" }
11019
+ } };
11020
+ }
10814
11021
  /** Read one app directory's credential, or the reason there is none. */
10815
11022
  async function readQoderCredential(candidates, options = {}) {
10816
11023
  const byKind = (kind) => candidates.filter((candidate) => candidate.kind === kind).map((candidate) => candidate.path);
10817
11024
  const appDir = candidates[0]?.appDir ?? "";
10818
- const base = { region: candidates[0]?.region ?? "cn" };
11025
+ const region = candidates[0]?.region ?? "cn";
11026
+ const base = { region };
10819
11027
  const authBlob = await firstAuthBlob(byKind("auth-v1"));
10820
11028
  if (authBlob === void 0) return { failure: {
10821
11029
  ...base,
10822
11030
  reason: "no-auth"
10823
11031
  } };
11032
+ if ((options.platform ?? process.platform) === "darwin") return readQoderCredentialMac(authBlob, region, appDir);
10824
11033
  let key;
10825
11034
  try {
10826
11035
  const localState = byKind("local-state")[0];
@@ -10866,9 +11075,16 @@ async function readQoderCredential(candidates, options = {}) {
10866
11075
  */
10867
11076
  const DISCOVERY_TTL_MS = 3e4;
10868
11077
  let discoveryCache;
10869
- /** Drop the cached discovery answer, e.g. when the user asks for a rescan. */
11078
+ /**
11079
+ * Drop the cached discovery answer, e.g. when the user asks for a rescan.
11080
+ *
11081
+ * The macOS Keychain keys go with it: a desktop re-login can rotate the stored
11082
+ * password, and a rescan is exactly when a rotated key has to be re-read
11083
+ * instead of failing every decrypt with the stale copy.
11084
+ */
10870
11085
  function resetQoderCredentialCache() {
10871
11086
  discoveryCache = void 0;
11087
+ darwinKeyCache.clear();
10872
11088
  }
10873
11089
  /**
10874
11090
  * Every Qoder sign-in on this machine. One app directory that is missing or
@@ -12164,6 +12380,7 @@ var QoderUpstreamClient = class {
12164
12380
  }
12165
12381
  if (!response.ok) {
12166
12382
  const text = await readErrorBody(response, 300, options.signal);
12383
+ deadline.abort();
12167
12384
  return {
12168
12385
  ok: false,
12169
12386
  status: response.status,
@@ -12174,7 +12391,7 @@ var QoderUpstreamClient = class {
12174
12391
  }
12175
12392
  return {
12176
12393
  ok: true,
12177
- response: response.body === null ? response : new Response(withIdleWatchdog(response.body, options.timeouts?.idleMs, options.signal), {
12394
+ response: response.body === null ? response : new Response(withIdleWatchdog(response.body, options.timeouts?.idleMs, options.signal, deadline.abort), {
12178
12395
  status: response.status,
12179
12396
  headers: response.headers
12180
12397
  })
@@ -12360,19 +12577,16 @@ function classifyQoderFailure(failure) {
12360
12577
  * caller states it (a fabricated `false` is refused with 1210 on models that
12361
12578
  * always think).
12362
12579
  */
12363
- function qoderRequestBody(openAiBody, model, entry, options = {}) {
12580
+ function qoderRequestBody(openAiBody, model, entry) {
12364
12581
  const requested = readReasoningEffort(openAiBody);
12365
12582
  const off = requested === "off" || requested === "none";
12366
12583
  const wire = requested === void 0 || off ? void 0 : qoderWireEffort(requested, entry);
12367
12584
  return {
12368
12585
  body: openAiBody,
12369
12586
  model,
12370
- options: {
12371
- ...options,
12372
- ...requested === void 0 ? {} : {
12373
- enableThinking: !off,
12374
- ...wire === void 0 ? {} : { reasoningEffort: wire }
12375
- }
12587
+ options: requested === void 0 ? {} : {
12588
+ enableThinking: !off,
12589
+ ...wire === void 0 ? {} : { reasoningEffort: wire }
12376
12590
  }
12377
12591
  };
12378
12592
  }
@@ -12782,7 +12996,7 @@ async function openQoderResponse(response, model, options = {}) {
12782
12996
  throw error;
12783
12997
  }
12784
12998
  if (failure !== void 0 && !sawOutput) {
12785
- await reader.cancel().catch(() => {});
12999
+ reader.cancel().catch(() => {});
12786
13000
  const { status, kind, error } = classifyQoderFailure(failure);
12787
13001
  const retryAfter = error instanceof QoderQueueRejection || error instanceof QoderDailyLimitRejection ? error.retryAfterSeconds : void 0;
12788
13002
  const wait = retryAfter === void 0 ? void 0 : retryAfterHeader(retryAfter);
@@ -12928,7 +13142,6 @@ function createQoderGateway(options) {
12928
13142
  return cachedModels;
12929
13143
  });
12930
13144
  if (watcher.signal.aborted) return;
12931
- if (roster.length === 0) roster = cachedModels;
12932
13145
  if (roster.length === 0) {
12933
13146
  writeOpenAIError(res, 503, "upstream_unavailable", reason);
12934
13147
  return;
@@ -12978,7 +13191,7 @@ function createQoderGateway(options) {
12978
13191
  let relayed;
12979
13192
  try {
12980
13193
  relayed = await openQoderResponse(result.response, model, { onFailure: (error) => {
12981
- logger?.warn?.("wtq-pool: qoder relay stream failed mid-flight", error);
13194
+ logger?.warn?.("wtq-pool: qoder relay stream failed mid-flight", error instanceof Error ? error.name : "unknown error");
12982
13195
  if (!(error instanceof Error && error.name === "AbortError") && !watcher.signal.aborted) {
12983
13196
  const seconds = error.retryAfterSeconds;
12984
13197
  options.onAttemptFailure?.(credential, { ...typeof seconds === "number" && seconds > 0 ? { retryAfter: String(seconds) } : {} });
@@ -13050,10 +13263,6 @@ function createQoderGateway(options) {
13050
13263
  return;
13051
13264
  }
13052
13265
  const relayed = opened.relayed;
13053
- if (relayed.body === null) {
13054
- writeOpenAIError(res, 502, "upstream_error", "Qoder returned no stream");
13055
- return;
13056
- }
13057
13266
  if (watcher.signal.aborted) {
13058
13267
  relayStreamOf(relayed).cancel().catch(() => {});
13059
13268
  return;
@@ -13066,7 +13275,7 @@ function createQoderGateway(options) {
13066
13275
  });
13067
13276
  res.flushHeaders();
13068
13277
  pumpToResponse(relayStreamOf(relayed), res).catch((error) => {
13069
- if (!(watcher.signal.aborted || error instanceof Error && error.name === "AbortError")) logger?.warn?.("wtq-pool: qoder relay stream failed mid-flight", error);
13278
+ if (!(watcher.signal.aborted || error instanceof Error && error.name === "AbortError")) logger?.warn?.("wtq-pool: qoder relay stream failed mid-flight", error instanceof Error ? error.name : "unknown error");
13070
13279
  if (res.writable) res.end();
13071
13280
  });
13072
13281
  }
@@ -13447,7 +13656,6 @@ function createSourcePool(host) {
13447
13656
  sourceCredentialExpired,
13448
13657
  accountStateKey,
13449
13658
  noteSourceFailure,
13450
- packUrgency,
13451
13659
  orderSourceCredentials
13452
13660
  };
13453
13661
  }
@@ -14297,8 +14505,14 @@ function saveCheckinSlots() {
14297
14505
  const target = path.join(dataPath, CHECKIN_SLOTS_FILE);
14298
14506
  const staging = target + ".tmp";
14299
14507
  try {
14300
- fs$1.mkdirSync(dataPath, { recursive: true });
14301
- fs$1.writeFileSync(staging, JSON.stringify(byDay), "utf8");
14508
+ fs$1.mkdirSync(dataPath, {
14509
+ recursive: true,
14510
+ mode: 448
14511
+ });
14512
+ fs$1.writeFileSync(staging, JSON.stringify(byDay), {
14513
+ encoding: "utf8",
14514
+ mode: 384
14515
+ });
14302
14516
  fs$1.renameSync(staging, target);
14303
14517
  } catch (error) {
14304
14518
  try {
@@ -14399,8 +14613,8 @@ function creditReserves() {
14399
14613
  return clean;
14400
14614
  }
14401
14615
  /**
14402
- * Accounts removed from the pool for good, with label and gateway kept so the
14403
- * panel can still offer a restore after the account stops being scanned.
14616
+ * Accounts removed from the pool, with label and gateway kept so the panel can
14617
+ * still offer a restore after the account stops being scanned.
14404
14618
  */
14405
14619
  function removedAccounts() {
14406
14620
  const value = settings.removedAccounts;
@@ -14489,6 +14703,7 @@ function sourceModelView(key, model, native) {
14489
14703
  nativeSupportsImages: native.supportsImages,
14490
14704
  ...Array.isArray(native.thinkingLevels) && native.thinkingLevels.length > 0 ? { nativeThinkingLevels: [...native.thinkingLevels] } : {},
14491
14705
  ...Array.isArray(override.thinkingLevels) && override.thinkingLevels.length > 0 ? { thinkingLevels: override.thinkingLevels } : {},
14706
+ ...typeof override.defaultThinkingLevel === "string" && override.defaultThinkingLevel !== "" ? { defaultThinkingLevel: override.defaultThinkingLevel } : {},
14492
14707
  ...native.multiplier === void 0 ? {} : { multiplier: native.multiplier },
14493
14708
  advanced: Object.keys(override).length > 0
14494
14709
  };
@@ -15233,14 +15448,17 @@ function traeDeclarationModels(region) {
15233
15448
  const advertised = native.thinkingLevels;
15234
15449
  const edited = Array.isArray(view.thinkingLevels) && view.thinkingLevels.length > 0 ? view.thinkingLevels.filter((level) => HOST_THINKING_LEVELS.includes(level)) : [];
15235
15450
  const levels = edited.length > 0 ? HOST_THINKING_LEVELS.filter((level) => edited.includes(level)) : advertised;
15236
- models.push({
15451
+ const published = offersThinking(levels) ? levels : ["off"];
15452
+ const entry = {
15237
15453
  id: view.id,
15238
15454
  name: modelLabel(view),
15239
15455
  contextWindow: view.contextWindow,
15240
15456
  maxTokens: view.maxOutputTokens,
15241
15457
  supportsImages: view.supportsImages,
15242
- thinkingLevels: offersThinking(levels) ? levels : ["off"]
15243
- });
15458
+ thinkingLevels: published
15459
+ };
15460
+ if (typeof view.defaultThinkingLevel === "string" && published.includes(view.defaultThinkingLevel)) entry.defaultThinkingLevel = view.defaultThinkingLevel;
15461
+ models.push(entry);
15244
15462
  if (models.length >= MAX_MODELS) break;
15245
15463
  }
15246
15464
  return models;
@@ -15403,14 +15621,17 @@ function qoderDeclarationModels(region) {
15403
15621
  const levels = native.thinkingLevels;
15404
15622
  const edited = Array.isArray(view.thinkingLevels) && view.thinkingLevels.length > 0 ? view.thinkingLevels.filter((level) => HOST_THINKING_LEVELS.includes(level)) : [];
15405
15623
  const published = edited.length > 0 ? HOST_THINKING_LEVELS.filter((level) => edited.includes(level)) : levels;
15406
- models.push({
15624
+ const entry = {
15407
15625
  id: view.id,
15408
15626
  name: view.name,
15409
15627
  contextWindow: view.contextWindow,
15410
15628
  maxTokens: view.maxOutputTokens,
15411
15629
  supportsImages: view.supportsImages,
15412
15630
  thinkingLevels: offersThinking(published) ? published : ["off"]
15413
- });
15631
+ };
15632
+ const levelsOffered = entry.thinkingLevels;
15633
+ if (typeof view.defaultThinkingLevel === "string" && levelsOffered.includes(view.defaultThinkingLevel)) entry.defaultThinkingLevel = view.defaultThinkingLevel;
15634
+ models.push(entry);
15414
15635
  if (models.length >= MAX_MODELS) break;
15415
15636
  }
15416
15637
  return models;
@@ -15694,7 +15915,7 @@ function cooldownPending() {
15694
15915
  function probesStale() {
15695
15916
  const now = Date.now();
15696
15917
  for (const account of core.pool.list()) {
15697
- if (account.cooldownUntilMs > now || core.pool.isDisabled(account.id) || !regionEnabled("workbuddy", regionOf(account.credential.domain))) continue;
15918
+ if (account.cooldownUntilMs > now && core.pool.activeHold?.(account.id)?.reason !== "spent" || core.pool.isDisabled(account.id) || !regionEnabled("workbuddy", regionOf(account.credential.domain))) continue;
15698
15919
  const cached = probeCache.get(account.id);
15699
15920
  if (cached === void 0) return true;
15700
15921
  const ttl = cached.value.creditsError !== void 0 ? PROBE_RETRY_MS : PROBE_TTL_MS;
@@ -16051,7 +16272,7 @@ async function refreshSourceAccounts(generation) {
16051
16272
  const rows = await Promise.all(wanted.map(async ({ credential, label }) => {
16052
16273
  const region = traeRegionOfCredential(credential);
16053
16274
  const probe = await traeRowProbe(credential, traeAccountRows.find((row) => row.label === label && row.region === region));
16054
- noteSourceExpiry("trae", region, label, probe?.usage?.credits?.packages);
16275
+ if (generation === loadGeneration) noteSourceExpiry("trae", region, label, probe?.usage?.credits?.packages);
16055
16276
  return {
16056
16277
  label,
16057
16278
  region,
@@ -16066,7 +16287,10 @@ async function refreshSourceAccounts(generation) {
16066
16287
  if (generation !== loadGeneration) return;
16067
16288
  traeLabelOverrides = overrides;
16068
16289
  traeAccountRows = rows.filter((row) => regionEnabled("trae", row.region));
16069
- } else traeAccountRows = [];
16290
+ } else {
16291
+ if (generation !== loadGeneration) return;
16292
+ traeAccountRows = [];
16293
+ }
16070
16294
  } catch (error) {
16071
16295
  log("warn", "listing Trae sign-ins failed: " + safeMessage(error));
16072
16296
  }
@@ -16074,6 +16298,7 @@ async function refreshSourceAccounts(generation) {
16074
16298
  const regions = activeQoderRegions();
16075
16299
  if (regions.length > 0) {
16076
16300
  const found = await discoverQoderCredentials();
16301
+ if (generation !== loadGeneration) return;
16077
16302
  const allLabels = uniqueSourceLabels(found.credentials, qoderBaseLabel, (credential) => credential.userID);
16078
16303
  const overrides = /* @__PURE__ */ new Map();
16079
16304
  found.credentials.forEach((credential, index) => {
@@ -16087,10 +16312,12 @@ async function refreshSourceAccounts(generation) {
16087
16312
  const { left } = budget(SOURCE_ACCOUNTS_BUDGET_MS);
16088
16313
  let seen = 0;
16089
16314
  for (const { credential, label } of wanted) {
16315
+ if (generation !== loadGeneration) return;
16090
16316
  if (seen > 0) await delay(QODER_ACCOUNT_DELAY_MS);
16091
16317
  seen += 1;
16092
16318
  const previous = qoderAccountRows.find((row) => row.label === label && row.region === credential.region);
16093
16319
  const probe = left() > 0 ? await qoderRowProbe(credential, previous) : previous;
16320
+ if (generation !== loadGeneration) return;
16094
16321
  noteSourceExpiry("qoder", credential.region, label, probe?.usage?.credits?.packages);
16095
16322
  rows.push({
16096
16323
  label,
@@ -16104,10 +16331,14 @@ async function refreshSourceAccounts(generation) {
16104
16331
  if (generation !== loadGeneration) return;
16105
16332
  qoderLabelOverrides = overrides;
16106
16333
  qoderAccountRows = rows.filter((row) => regionEnabled("qoder", row.region));
16107
- } else qoderAccountRows = [];
16334
+ } else {
16335
+ if (generation !== loadGeneration) return;
16336
+ qoderAccountRows = [];
16337
+ }
16108
16338
  } catch (error) {
16109
16339
  log("warn", "listing Qoder sign-ins failed: " + safeMessage(error));
16110
16340
  }
16341
+ if (generation !== loadGeneration) return;
16111
16342
  pruneSourceState();
16112
16343
  }
16113
16344
  /**
@@ -16375,7 +16606,7 @@ async function statusDocument(options) {
16375
16606
  selectionByRegion[region] = effectiveSelectionFor(region, runtimes.get(region)?.catalog);
16376
16607
  if (selectionStale(region, runtimes.get(region)?.catalog)) staleSelectionRegions.push(region);
16377
16608
  const available = core.pool.available(now, void 0, region);
16378
- const nextUp = available.find((account) => account.id === preferredAccountIds[region]) ?? core.pool.preview(available);
16609
+ const nextUp = available.find((account) => account.id === preferredAccountIds[region]) ?? core.pool.preview(available, region);
16379
16610
  runtimeNextUpId.set(region, nextUp === void 0 ? void 0 : nextUp.id);
16380
16611
  for (const account of core.pool.list(region)) {
16381
16612
  const cooling = account.cooldownUntilMs > now;
@@ -16402,7 +16633,7 @@ async function statusDocument(options) {
16402
16633
  ...creditReserve === void 0 ? {} : { creditReserve }
16403
16634
  };
16404
16635
  if (!core.pool.isDisabled(account.id)) {
16405
- const checkinOnly = cooling;
16636
+ const checkinOnly = cooling && core.pool.activeHold?.(account.id)?.reason !== "spent";
16406
16637
  pendingProbes.push(accountProbe(account, allowNetwork, checkinOnly).then((probe) => {
16407
16638
  applyProbe(row, probe);
16408
16639
  }));
@@ -16441,7 +16672,51 @@ async function statusDocument(options) {
16441
16672
  }
16442
16673
  }
16443
16674
  }
16444
- if (pendingProbes.length > 0) await settleWithin(Promise.all(pendingProbes).catch(() => {}), probeBudget);
16675
+ if (pendingProbes.length > 0) {
16676
+ await settleWithin(Promise.all(pendingProbes).catch(() => {}), probeBudget);
16677
+ const liveAccounts = new Map(core.pool.list().map((account) => [account.id, account]));
16678
+ const heldNow = Date.now();
16679
+ const nextUpNow = /* @__PURE__ */ new Map();
16680
+ for (const region of activeRegions()) {
16681
+ const available = core.pool.available(heldNow, void 0, region);
16682
+ const preferredNow = available.find((account) => account.id === preferredAccountIds[region]);
16683
+ nextUpNow.set(region, (preferredNow ?? core.pool.preview(available, region))?.id);
16684
+ }
16685
+ for (const row of accounts) {
16686
+ const account = liveAccounts.get(row.id);
16687
+ if (account === void 0) continue;
16688
+ const cooling = account.cooldownUntilMs > heldNow;
16689
+ row.cooling = cooling;
16690
+ if (cooling) row.cooldownUntil = new Date(account.cooldownUntilMs).toISOString();
16691
+ else delete row.cooldownUntil;
16692
+ const hold = core.pool.activeHold(account.id);
16693
+ if (hold === null || hold.untilMs <= heldNow) delete row.hold;
16694
+ else row.hold = hold.reason;
16695
+ row.active = nextUpNow.get(row.region) === row.id;
16696
+ }
16697
+ }
16698
+ const sourceDocument = (source) => {
16699
+ const sourceRegions = source === "trae" ? activeTraeRegions : activeQoderRegions;
16700
+ const accounts = source === "trae" ? traeAccountRows : qoderAccountRows;
16701
+ const gateways = source === "trae" ? traeGateways : qoderGateways;
16702
+ const declarationModels = source === "trae" ? traeDeclarationModels : qoderDeclarationModels;
16703
+ const nativeOf = source === "trae" ? traeNativeOf : qoderNativeOf;
16704
+ return {
16705
+ regions: sourceRegions(),
16706
+ distribution: sourceDistribution(source),
16707
+ accounts,
16708
+ preferred: sourcePreferredMap(source),
16709
+ gateways: Array.from(gateways.values()).map((entry) => ({
16710
+ region: entry.region,
16711
+ port: entry.gateway.port(),
16712
+ models: declarationModels(entry.region).length,
16713
+ portFallback: entry.portFallback,
16714
+ preferredPort: entry.preferredPort,
16715
+ lastError: entry.lastError,
16716
+ ...sourcePublishedModels(source + ":" + entry.region, entry.gateway.models(), nativeOf)
16717
+ }))
16718
+ };
16719
+ };
16445
16720
  return {
16446
16721
  regions: activeRegions(),
16447
16722
  sources: {
@@ -16450,36 +16725,8 @@ async function statusDocument(options) {
16450
16725
  qoder: sourceEnabled("qoder")
16451
16726
  },
16452
16727
  disabledRegions: disabledRegionKeys(),
16453
- trae: {
16454
- regions: activeTraeRegions(),
16455
- distribution: sourceDistribution("trae"),
16456
- accounts: traeAccountRows,
16457
- preferred: sourcePreferredMap("trae"),
16458
- gateways: Array.from(traeGateways.values()).map((entry) => ({
16459
- region: entry.region,
16460
- port: entry.gateway.port(),
16461
- models: traeDeclarationModels(entry.region).length,
16462
- portFallback: entry.portFallback,
16463
- preferredPort: entry.preferredPort,
16464
- lastError: entry.lastError,
16465
- ...sourcePublishedModels("trae:" + entry.region, entry.gateway.models(), traeNativeOf)
16466
- }))
16467
- },
16468
- qoder: {
16469
- regions: activeQoderRegions(),
16470
- distribution: sourceDistribution("qoder"),
16471
- accounts: qoderAccountRows,
16472
- preferred: sourcePreferredMap("qoder"),
16473
- gateways: Array.from(qoderGateways.values()).map((entry) => ({
16474
- region: entry.region,
16475
- port: entry.gateway.port(),
16476
- models: qoderDeclarationModels(entry.region).length,
16477
- portFallback: entry.portFallback,
16478
- preferredPort: entry.preferredPort,
16479
- lastError: entry.lastError,
16480
- ...sourcePublishedModels("qoder:" + entry.region, entry.gateway.models(), qoderNativeOf)
16481
- }))
16482
- },
16728
+ trae: sourceDocument("trae"),
16729
+ qoder: sourceDocument("qoder"),
16483
16730
  enabled: settings.enabled !== false,
16484
16731
  unpublishedDeclarations: unpublishedDeclarations(),
16485
16732
  modelCap: MAX_MODELS,
@@ -16616,39 +16863,38 @@ function addressDrift() {
16616
16863
  return false;
16617
16864
  }
16618
16865
  /**
16619
- * Trae discovery for the doctor: count only, never token material.
16866
+ * Discovery detail shared by Trae and Qoder.
16620
16867
  *
16621
- * Answers `{ok, detail}` so the finding can carry the verdict instead of
16622
- * hard-coding `true`: a source that could not be read, or that holds no
16623
- * sign-in at all, is a failed check, and the report used to pass it.
16868
+ * Counts and served-model figures only: the report is copyable out of the panel,
16869
+ * so no token material or filesystem path reaches it.
16624
16870
  */
16625
- async function traeDoctorDetail() {
16626
- if (!sourceEnabled("trae")) return {
16871
+ async function sourceDoctorDetail(spec) {
16872
+ if (!sourceEnabled(spec.source)) return {
16627
16873
  ok: true,
16628
16874
  detail: "switched off in plugin settings (deliberate; not a failure)"
16629
16875
  };
16630
16876
  try {
16631
- const found = await discoverTraeCredentials();
16877
+ const found = await spec.discover();
16878
+ const failures = found.failures.length > 0 ? `; ${found.failures.length} ${spec.unreadable}` : "";
16632
16879
  if (found.credentials.length === 0) return {
16633
16880
  ok: false,
16634
- detail: "on; no Trae sign-in found (sign in to Trae CN / TRAE SOLO CN, then run the check again)"
16881
+ detail: `on; no ${spec.label} sign-in found (${spec.hint})${failures}`
16635
16882
  };
16636
16883
  const byRegion = {};
16637
16884
  for (const credential of found.credentials) {
16638
- const region = traeRegionOfCredential(credential);
16885
+ const region = spec.regionOf(credential);
16639
16886
  byRegion[region] = (byRegion[region] || 0) + 1;
16640
16887
  }
16641
16888
  const parts = Object.entries(byRegion).map(([region, count]) => `${region} x${count}`).join(", ");
16642
- const failures = found.failures.length > 0 ? `; ${found.failures.length} unreadable file(s)` : "";
16643
- const servedRegions = activeTraeRegions();
16644
- const served = servedRegions.map((region) => `${region}: ${traeDeclarationModels(region).length} model(s)`).join(", ");
16889
+ const servedRegions = spec.activeRegions();
16890
+ const served = servedRegions.map((region) => `${region}: ${spec.declarationModels(region).length} model(s)`).join(", ");
16645
16891
  const stillReading = [];
16646
16892
  const problems = [];
16647
16893
  for (const region of servedRegions) {
16648
- const entry = traeGateways.get(region);
16894
+ const entry = spec.gateways.get(region);
16649
16895
  if (entry === void 0 && !byRegion[region]) problems.push(`${region}: no sign-in for this region`);
16650
16896
  else if (entry === void 0 || entry.warmSettled !== true) stillReading.push(region);
16651
- else if (traeDeclarationModels(region).length === 0) problems.push(entry.lastError !== "" ? `${region}: ${entry.lastError}` : `${region}: no models selected`);
16897
+ else if (spec.declarationModels(region).length === 0) problems.push(entry.lastError !== "" ? `${region}: ${entry.lastError}` : `${region}: no models selected`);
16652
16898
  }
16653
16899
  return {
16654
16900
  ok: problems.length === 0,
@@ -16661,42 +16907,33 @@ async function traeDoctorDetail() {
16661
16907
  };
16662
16908
  }
16663
16909
  }
16664
- /** Qoder credentials for the doctor: count only, never token material. */
16665
- async function qoderDoctorDetail() {
16666
- if (!sourceEnabled("qoder")) return {
16667
- ok: true,
16668
- detail: "switched off in plugin settings (deliberate; not a failure)"
16669
- };
16670
- try {
16671
- const found = await discoverQoderCredentials();
16672
- if (found.credentials.length === 0) return {
16673
- ok: false,
16674
- detail: `on; no Qoder sign-in found (sign in to Qoder, then run the check again)${found.failures.length > 0 ? `; ${found.failures.length} unreadable app dir(s)` : ""}`
16675
- };
16676
- const byRegion = {};
16677
- for (const credential of found.credentials) byRegion[credential.region] = (byRegion[credential.region] || 0) + 1;
16678
- const parts = Object.entries(byRegion).map(([region, count]) => `${region} x${count}`).join(", ");
16679
- const failures = found.failures.length > 0 ? `; ${found.failures.length} unreadable app dir(s)` : "";
16680
- const servedRegions = activeQoderRegions();
16681
- const served = servedRegions.map((region) => `${region}: ${qoderDeclarationModels(region).length} model(s)`).join(", ");
16682
- const stillReading = [];
16683
- const problems = [];
16684
- for (const region of servedRegions) {
16685
- const entry = qoderGateways.get(region);
16686
- if (entry === void 0 && !byRegion[region]) problems.push(`${region}: no sign-in for this region`);
16687
- else if (entry === void 0 || entry.warmSettled !== true) stillReading.push(region);
16688
- else if (qoderDeclarationModels(region).length === 0) problems.push(entry.lastError !== "" ? `${region}: ${entry.lastError}` : `${region}: no models selected`);
16689
- }
16690
- return {
16691
- ok: problems.length === 0,
16692
- detail: `on; ${found.credentials.length} account(s) (${parts})${failures}; serving ${served}` + (stillReading.length > 0 ? `; still reading models for ${stillReading.join(", ")}` : "") + (problems.length > 0 ? `; ${problems.join("; ")}` : "")
16693
- };
16694
- } catch (error) {
16695
- return {
16696
- ok: false,
16697
- detail: `on; discovery failed (${errorCodeOf(error)})`
16698
- };
16699
- }
16910
+ /** Trae discovery for the doctor. */
16911
+ function traeDoctorDetail() {
16912
+ return sourceDoctorDetail({
16913
+ source: "trae",
16914
+ label: "Trae",
16915
+ hint: "sign in to Trae CN / TRAE SOLO CN, then run the check again",
16916
+ unreadable: "unreadable file(s)",
16917
+ discover: discoverTraeCredentials,
16918
+ regionOf: traeRegionOfCredential,
16919
+ activeRegions: activeTraeRegions,
16920
+ declarationModels: traeDeclarationModels,
16921
+ gateways: traeGateways
16922
+ });
16923
+ }
16924
+ /** Qoder discovery for the doctor. */
16925
+ function qoderDoctorDetail() {
16926
+ return sourceDoctorDetail({
16927
+ source: "qoder",
16928
+ label: "Qoder",
16929
+ hint: "sign in to Qoder, then run the check again",
16930
+ unreadable: "unreadable app dir(s)",
16931
+ discover: discoverQoderCredentials,
16932
+ regionOf: (credential) => credential.region,
16933
+ activeRegions: activeQoderRegions,
16934
+ declarationModels: qoderDeclarationModels,
16935
+ gateways: qoderGateways
16936
+ });
16700
16937
  }
16701
16938
  /** A readable report of everything that has to hold for a chat to reach upstream. */
16702
16939
  async function doctorReport() {
@@ -16818,7 +17055,10 @@ async function doctorReport() {
16818
17055
  ""
16819
17056
  ].join("\n");
16820
17057
  if (dataPath !== "") try {
16821
- fs$1.writeFileSync(path.join(dataPath, "doctor.txt"), report, "utf8");
17058
+ fs$1.writeFileSync(path.join(dataPath, "doctor.txt"), report, {
17059
+ encoding: "utf8",
17060
+ mode: 384
17061
+ });
16822
17062
  } catch {}
16823
17063
  return {
16824
17064
  ok,
@@ -17480,15 +17720,24 @@ async function onPanelInvoke(channel, payload) {
17480
17720
  */
17481
17721
  case "xd.startLogin": {
17482
17722
  const realm = payload?.realm === "global" ? "global" : "cn";
17483
- const start = await startDeviceLogin(realm);
17484
- deviceLogins.set(realm, {
17485
- state: start.state,
17486
- at: Date.now()
17487
- });
17488
- return {
17489
- realm,
17490
- authUrl: start.authUrl
17491
- };
17723
+ try {
17724
+ const start = await startDeviceLogin(realm);
17725
+ deviceLogins.set(realm, {
17726
+ state: start.state,
17727
+ at: Date.now()
17728
+ });
17729
+ return {
17730
+ realm,
17731
+ authUrl: start.authUrl
17732
+ };
17733
+ } catch (error) {
17734
+ deviceLogins.delete(realm);
17735
+ log("warn", "could not start a WorkBuddy sign-in: " + safeMessage(error));
17736
+ return {
17737
+ realm,
17738
+ authUrl: ""
17739
+ };
17740
+ }
17492
17741
  }
17493
17742
  /**
17494
17743
  * Poll the login the panel started on this realm.
@@ -17616,31 +17865,7 @@ async function onPanelInvoke(channel, payload) {
17616
17865
  */
17617
17866
  case "xd.composerRefresh": {
17618
17867
  const hit = matchComposerModel(payload);
17619
- if (hit === void 0) return composerPoints(payload);
17620
- const { left, settle } = budget(COMPOSER_REFRESH_BUDGET_MS);
17621
- if (hit.source === "workbuddy") {
17622
- for (const account of core.pool.list(hit.region)) probeCache.delete(account.id);
17623
- await settle(statusDocument({
17624
- allowNetwork: true,
17625
- budgetMs: COMPOSER_REFRESH_BUDGET_MS
17626
- }));
17627
- } else {
17628
- const rows = hit.source === "trae" ? traeAccountRows : qoderAccountRows;
17629
- const found = hit.source === "trae" ? await discoverTraeCredentials() : await discoverQoderCredentials();
17630
- for (const row of rows) {
17631
- if (row.region !== hit.region) continue;
17632
- if (left() === 0) break;
17633
- const credential = found.credentials.find((item) => {
17634
- const itemLabel = hit.source === "trae" ? traeRowLabel(item) : qoderRowLabel(item);
17635
- const itemRegion = hit.source === "trae" ? traeRegionOfCredential(item) : item.region;
17636
- return itemLabel === row.label && itemRegion === row.region;
17637
- });
17638
- if (credential === void 0) continue;
17639
- const probe = hit.source === "trae" ? await traeRowProbe(credential, row) : await qoderRowProbe(credential, row);
17640
- if (probe?.usage) row.usage = probe.usage;
17641
- if (probe?.checkin) row.checkin = probe.checkin;
17642
- }
17643
- }
17868
+ if (hit !== void 0) await settleWithin(composerRefreshWork(hit), COMPOSER_REFRESH_BUDGET_MS);
17644
17869
  return composerPoints(payload);
17645
17870
  }
17646
17871
  default: throw panelError("unsupported panel channel: " + String(channel), "UNSUPPORTED");
@@ -17807,6 +18032,32 @@ const GROWTH_BUDGET_MS = 9e4;
17807
18032
  * credentials read from turning the click into a client-side timeout.
17808
18033
  */
17809
18034
  const COMPOSER_REFRESH_BUDGET_MS = 15e3;
18035
+ async function composerRefreshWork(hit) {
18036
+ if (hit.source === "workbuddy") {
18037
+ for (const account of core.pool.list(hit.region)) probeCache.delete(account.id);
18038
+ await statusDocument({
18039
+ allowNetwork: true,
18040
+ budgetMs: COMPOSER_REFRESH_BUDGET_MS
18041
+ });
18042
+ return;
18043
+ }
18044
+ const { left } = budget(COMPOSER_REFRESH_BUDGET_MS);
18045
+ const rows = hit.source === "trae" ? traeAccountRows : qoderAccountRows;
18046
+ const found = hit.source === "trae" ? await discoverTraeCredentials() : await discoverQoderCredentials();
18047
+ for (const row of rows) {
18048
+ if (row.region !== hit.region) continue;
18049
+ if (left() === 0) break;
18050
+ const credential = found.credentials.find((item) => {
18051
+ const itemLabel = hit.source === "trae" ? traeRowLabel(item) : qoderRowLabel(item);
18052
+ const itemRegion = hit.source === "trae" ? traeRegionOfCredential(item) : item.region;
18053
+ return itemLabel === row.label && itemRegion === row.region;
18054
+ });
18055
+ if (credential === void 0) continue;
18056
+ const probe = hit.source === "trae" ? await traeRowProbe(credential, row) : await qoderRowProbe(credential, row);
18057
+ if (probe?.usage) row.usage = probe.usage;
18058
+ if (probe?.checkin) row.checkin = probe.checkin;
18059
+ }
18060
+ }
17810
18061
  /**
17811
18062
  * Light one account's growth streak, then redeem every tier it opened.
17812
18063
  *
@@ -18990,7 +19241,7 @@ function workbuddyComposerAccounts(region, modelId) {
18990
19241
  const list = core.pool.list(region);
18991
19242
  const available = core.pool.available(now, modelId, region);
18992
19243
  const preferredId = core.pool.resolvedPreferredId(region);
18993
- const next = available.find((account) => account.id === preferredId) ?? core.pool.readoutPreview(available);
19244
+ const next = available.find((account) => account.id === preferredId) ?? core.pool.readoutPreview(available, region);
18994
19245
  return list.map((account) => {
18995
19246
  const cached = probeCache.get(account.id)?.value?.credits;
18996
19247
  return {
@@ -19178,16 +19429,6 @@ const INERT_AUTH = {
19178
19429
  /** The sentinel every loopback gateway accepts. */
19179
19430
  const LOOPBACK_BEARER = "dsh-wtq-pool-loopback";
19180
19431
  /**
19181
- * Whether the installed schemastery can mark a field volatile.
19182
- *
19183
- * `Config` is built at module evaluation, before any `ctx` exists to log
19184
- * through, so the finding is recorded here and reported by the activation's
19185
- * first logger-reaching call rather than dropped.
19186
- */
19187
- let volatileSupported = true;
19188
- /** Whether the finding above has already been named, so it is named once. */
19189
- let degradationReported = false;
19190
- /**
19191
19432
  * Which activation currently owns the module-global state the core keeps.
19192
19433
  *
19193
19434
  * A re-provided service re-runs `apply()`, and the core's state (runtimes,
@@ -19196,53 +19437,32 @@ let degradationReported = false;
19196
19437
  * gateways the newer one just brought up.
19197
19438
  */
19198
19439
  let activationToken = 0;
19199
- /**
19200
- * Wrap one schema in `volatile()` when the installed schemastery has it.
19201
- *
19202
- * `volatile()` landed in schemastery 3.18.3. Hand-writing `meta.volatile` on an
19203
- * older copy would bypass the host's own `validateVolatileSchema`, so an older
19204
- * builder degrades to the plain schema instead.
19205
- */
19206
- function asVolatile(schema) {
19207
- const candidate = schema;
19208
- if (typeof candidate.volatile !== "function") {
19209
- volatileSupported = false;
19210
- return schema;
19211
- }
19212
- return candidate.volatile();
19213
- }
19214
- /** Report the schema degradation once, when a logger is finally in reach. */
19215
- function reportSchemaDegradation(logger) {
19216
- if (volatileSupported || degradationReported) return;
19217
- degradationReported = true;
19218
- 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");
19219
- }
19220
19440
  const Config = z.object({
19221
- enabled: asVolatile(z.boolean().default(true).description("Serve this pool at all")),
19222
- sourceWorkbuddy: asVolatile(z.boolean().default(true).description("Merge the WorkBuddy desktop sign-ins")),
19223
- sourceTrae: asVolatile(z.boolean().default(true).description("Merge the Trae desktop sign-ins")),
19224
- sourceQoder: asVolatile(z.boolean().default(true).description("Merge the Qoder desktop sign-ins")),
19225
- authFile: asVolatile(z.string().description("WorkBuddy desktop auth file (defaults to the app own location)")),
19226
- autoCheckin: asVolatile(z.object({
19441
+ enabled: z.boolean().default(true).description("Serve this pool at all").volatile(),
19442
+ sourceWorkbuddy: z.boolean().default(true).description("Merge the WorkBuddy desktop sign-ins").volatile(),
19443
+ sourceTrae: z.boolean().default(true).description("Merge the Trae desktop sign-ins").volatile(),
19444
+ sourceQoder: z.boolean().default(true).description("Merge the Qoder desktop sign-ins").volatile(),
19445
+ authFile: z.string().description("WorkBuddy desktop auth file (defaults to the app's own location)").volatile(),
19446
+ autoCheckin: z.object({
19227
19447
  enabled: z.boolean().default(false),
19228
19448
  hours: z.array(z.number()).default([])
19229
- }).description("Daily check-in across every discovered sign-in")),
19230
- cooldownSeconds: asVolatile(z.number().default(60).description("Rate-limit cooldown per account, in seconds")),
19231
- creditReserves: asVolatile(z.dict(z.number()).default({}).description("Per-account credit floor, keyed by account id")),
19232
- disabledAccountIds: asVolatile(z.array(z.string()).default([]).description("Account ids the pool must never pick")),
19233
- disabledRegions: asVolatile(z.array(z.string()).default([]).description("Regions parked on their own, as source:region")),
19234
- distribution: asVolatile(z.union([
19449
+ }).description("Daily check-in across every discovered sign-in").volatile(),
19450
+ cooldownSeconds: z.number().default(60).description("Rate-limit cooldown per account, in seconds").volatile(),
19451
+ creditReserves: z.dict(z.number()).default({}).description("Per-account credit floor, keyed by account id").volatile(),
19452
+ disabledAccountIds: z.array(z.string()).default([]).description("Account ids the pool must never pick").volatile(),
19453
+ disabledRegions: z.array(z.string()).default([]).description("Regions parked on their own, as source:region").volatile(),
19454
+ distribution: z.union([
19235
19455
  "priority",
19236
19456
  "round-robin",
19237
19457
  "expiry",
19238
19458
  "sticky"
19239
- ]).default("priority").description("How the WorkBuddy pool spreads requests")),
19240
- modelSelection: asVolatile(z.any().description("Per-gateway model selection")),
19241
- preferredAccountIds: asVolatile(z.any().description("Picked WorkBuddy account per region")),
19242
- preferredSourceAccounts: asVolatile(z.any().description("Picked Trae/Qoder account per region")),
19243
- removedAccounts: asVolatile(z.any().description("Accounts removed from the pool for good")),
19244
- showComposerPoints: asVolatile(z.boolean().default(false).description("Show the active account credits beside the composer")),
19245
- sourceDistributions: asVolatile(z.any().description("How each Trae/Qoder region spreads requests"))
19459
+ ]).default("priority").description("How the WorkBuddy pool spreads requests").volatile(),
19460
+ modelSelection: z.any().description("Per-gateway model selection").volatile(),
19461
+ preferredAccountIds: z.any().description("Picked WorkBuddy account per region").volatile(),
19462
+ preferredSourceAccounts: z.any().description("Picked Trae/Qoder account per region").volatile(),
19463
+ removedAccounts: z.any().description("Accounts taken out of the pool; their label and region are kept so the panel can restore them").volatile(),
19464
+ showComposerPoints: z.boolean().default(false).description("Show the active account credits beside the composer").volatile(),
19465
+ sourceDistributions: z.any().description("How each Trae/Qoder region spreads requests").volatile()
19246
19466
  });
19247
19467
  /** Peel one volatile reference, if the running line wrapped it. */
19248
19468
  function unwrapVolatile(value) {
@@ -19495,22 +19715,47 @@ function apply(ctx, config = {}) {
19495
19715
  let writesInFlight = 0;
19496
19716
  let writesStarted = 0;
19497
19717
  let droppedPushes = 0;
19718
+ /**
19719
+ * Whether two setting values are the same document.
19720
+ *
19721
+ * Object key order carries no meaning in the stored document, and the loader's
19722
+ * own comparison ignores it, so a key-order-sensitive check would predict an
19723
+ * echo the host never emits.
19724
+ */
19725
+ function sameSetting(a, b) {
19726
+ if (Object.is(a, b)) return true;
19727
+ if (typeof a !== "object" || a === null || typeof b !== "object" || b === null) return false;
19728
+ if (Array.isArray(a) !== Array.isArray(b)) return false;
19729
+ const aRecord = a;
19730
+ const bRecord = b;
19731
+ const keys = Object.keys(aRecord).filter((key) => aRecord[key] !== void 0);
19732
+ if (keys.length !== Object.keys(bRecord).filter((key) => bRecord[key] !== void 0).length) return false;
19733
+ for (const key of keys) {
19734
+ if (!Object.prototype.hasOwnProperty.call(bRecord, key)) return false;
19735
+ if (!sameSetting(aRecord[key], bRecord[key])) return false;
19736
+ }
19737
+ return true;
19738
+ }
19498
19739
  /** Replace patched top-level fields without merging their dictionaries. */
19499
19740
  const writeSettings = async (patch) => {
19500
19741
  const mutate = settingsService?.mutate;
19501
19742
  if (mutate === void 0) throw new Error("this host exposes no settings mutate()");
19743
+ const before = current();
19502
19744
  writesStarted += 1;
19503
19745
  writesInFlight += 1;
19504
- let echoed = false;
19746
+ let echoExpected = false;
19505
19747
  try {
19748
+ echoExpected = Object.keys(patch).some((key) => !sameSetting(before[key], patch[key]));
19506
19749
  await mutate.call(settingsService, settingsNamespace, Object.entries(patch).map(([key, value]) => ({
19507
19750
  op: "set",
19508
19751
  path: [key],
19509
19752
  value
19510
19753
  })));
19511
- echoed = true;
19754
+ } catch (error) {
19755
+ echoExpected = false;
19756
+ throw error;
19512
19757
  } finally {
19513
- if (!echoed) writesStarted -= 1;
19758
+ if (!echoExpected) writesStarted -= 1;
19514
19759
  writesInFlight -= 1;
19515
19760
  if (writesInFlight === 0) {
19516
19761
  if (droppedPushes > writesStarted) {
@@ -19529,7 +19774,6 @@ function apply(ctx, config = {}) {
19529
19774
  getSettings: () => current(),
19530
19775
  setSettings: writeSettings,
19531
19776
  getDataPath: async () => {
19532
- reportSchemaDegradation(ctx.logger);
19533
19777
  const dir = path.join(dshHome(), "dsh-wtq-pool");
19534
19778
  await fs.mkdir(dir, {
19535
19779
  recursive: true,