agent-shim 11.72.0 → 11.72.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.cjs CHANGED
@@ -37387,7 +37387,7 @@ var init_package = __esm({
37387
37387
  "package.json"() {
37388
37388
  package_default = {
37389
37389
  name: "agent-shim",
37390
- version: "11.72.0",
37390
+ version: "11.72.1",
37391
37391
  description: "A profile manager and launcher for Claude Code that lets one person run multiple logins from one machine while controlling what gets shared between them.",
37392
37392
  license: "MIT",
37393
37393
  author: "Joseph Mearman <joseph@mearman.co.uk>",
@@ -38399,36 +38399,71 @@ function readUsageLog(fs31, logDir, options = {}) {
38399
38399
  if (sinceMs !== void 0 && segment.endsAt <= sinceMs) {
38400
38400
  continue;
38401
38401
  }
38402
- const contents = fs31.readFileUtf8(import_node_path26.default.join(logDir, segment.name));
38403
- if (contents === void 0) {
38402
+ const read = readSegment(fs31, logDir, segment);
38403
+ if (read === void 0) {
38404
38404
  continue;
38405
38405
  }
38406
- const lines = contents.split("\n");
38407
- lines.pop();
38408
- for (const line of lines) {
38409
- if (line === "") {
38410
- continue;
38411
- }
38412
- let raw;
38413
- try {
38414
- raw = JSON.parse(line);
38415
- } catch {
38416
- invalidLines += 1;
38417
- continue;
38418
- }
38419
- const parsed = UsageRecordSchema.safeParse(raw);
38420
- if (!parsed.success) {
38421
- invalidLines += 1;
38422
- continue;
38423
- }
38424
- if (sinceMs === void 0 || Date.parse(parsed.data.at) >= sinceMs) {
38425
- records.push(parsed.data);
38406
+ invalidLines += read.invalidLines;
38407
+ for (const record2 of read.records) {
38408
+ if (sinceMs === void 0 || Date.parse(record2.at) >= sinceMs) {
38409
+ records.push(record2);
38426
38410
  }
38427
38411
  }
38428
38412
  }
38429
38413
  records.sort((left, right) => left.at.localeCompare(right.at));
38430
38414
  return { records, invalidLines };
38431
38415
  }
38416
+ function readSegment(fs31, logDir, segment) {
38417
+ const contents = fs31.readFileUtf8(import_node_path26.default.join(logDir, segment.name));
38418
+ if (contents === void 0) {
38419
+ return void 0;
38420
+ }
38421
+ const records = [];
38422
+ let invalidLines = 0;
38423
+ const lines = contents.split("\n");
38424
+ lines.pop();
38425
+ for (const line of lines) {
38426
+ if (line === "") {
38427
+ continue;
38428
+ }
38429
+ let raw;
38430
+ try {
38431
+ raw = JSON.parse(line);
38432
+ } catch {
38433
+ invalidLines += 1;
38434
+ continue;
38435
+ }
38436
+ const parsed = UsageRecordSchema.safeParse(raw);
38437
+ if (!parsed.success) {
38438
+ invalidLines += 1;
38439
+ continue;
38440
+ }
38441
+ records.push(parsed.data);
38442
+ }
38443
+ return { records, invalidLines };
38444
+ }
38445
+ function readRecentUsageRecords(fs31, logDir, count2) {
38446
+ const byDay = /* @__PURE__ */ new Map();
38447
+ for (const segment of listLogSegments(fs31, logDir)) {
38448
+ byDay.set(segment.day, [...byDay.get(segment.day) ?? [], segment]);
38449
+ }
38450
+ const days = [...byDay.values()];
38451
+ let records = [];
38452
+ for (let index = days.length - 1; index >= 0; index -= 1) {
38453
+ const dayRecords = [];
38454
+ for (const segment of days[index] ?? []) {
38455
+ dayRecords.push(...readSegment(fs31, logDir, segment)?.records ?? []);
38456
+ }
38457
+ records = [...dayRecords, ...records];
38458
+ records.sort((left, right) => left.at.localeCompare(right.at));
38459
+ const olderDay = days[index - 1]?.[0];
38460
+ const newestHeld = records[records.length - count2];
38461
+ if (olderDay === void 0 || newestHeld !== void 0 && Date.parse(newestHeld.at) >= olderDay.endsAt) {
38462
+ break;
38463
+ }
38464
+ }
38465
+ return records.slice(-count2);
38466
+ }
38432
38467
  function snapshotPath(snapshotsDir, identity) {
38433
38468
  if (!isIdentityName(identity)) {
38434
38469
  throw new Error(`"${identity}" is not a valid identity name.`);
@@ -119889,7 +119924,7 @@ function blendedPricePerMTok(prices, mix) {
119889
119924
  var SEED_LOOKBACK_RECORDS = 500;
119890
119925
  function createTargetObservationStore(fs31, paths) {
119891
119926
  let observations = /* @__PURE__ */ new Map();
119892
- const seeded = readUsageLog(fs31, paths.usageLogDir, {}).records.slice(-SEED_LOOKBACK_RECORDS);
119927
+ const seeded = readRecentUsageRecords(fs31, paths.usageLogDir, SEED_LOOKBACK_RECORDS);
119893
119928
  for (const record2 of seeded) {
119894
119929
  observations = observeRecord(observations, record2);
119895
119930
  }
@@ -134286,6 +134321,10 @@ async function collectBody(body) {
134286
134321
  resolve3(result);
134287
134322
  }
134288
134323
  };
134324
+ if (body.destroyed) {
134325
+ settle2({ kind: "failed" });
134326
+ return;
134327
+ }
134289
134328
  body.on("data", (chunk2) => {
134290
134329
  if (settled) {
134291
134330
  return;
@@ -134309,6 +134348,9 @@ async function collectBody(body) {
134309
134348
  body.on("error", () => {
134310
134349
  settle2({ kind: "failed" });
134311
134350
  });
134351
+ body.on("close", () => {
134352
+ settle2({ kind: "failed" });
134353
+ });
134312
134354
  });
134313
134355
  }
134314
134356
  function requestedModelOf(body) {
@@ -139201,6 +139243,7 @@ function createRcEventFanout() {
139201
139243
  function createRcStreamHub(deps) {
139202
139244
  const attachments = /* @__PURE__ */ new Map();
139203
139245
  let closed = false;
139246
+ const refusedAuthorizations = /* @__PURE__ */ new Map();
139204
139247
  const lastSavedSequenceNums = /* @__PURE__ */ new Map();
139205
139248
  const persistCursor = (sessionId) => {
139206
139249
  if (deps.saveSequenceNum === void 0) {
@@ -139304,7 +139347,6 @@ function createRcStreamHub(deps) {
139304
139347
  };
139305
139348
  const runLoop = async (attachment) => {
139306
139349
  let retriedUnauthorized = false;
139307
- let refusedAuthorization;
139308
139350
  for (; ; ) {
139309
139351
  if (attachment.isStopped()) {
139310
139352
  return;
@@ -139320,11 +139362,11 @@ function createRcStreamHub(deps) {
139320
139362
  }
139321
139363
  return;
139322
139364
  }
139323
- if (refusedAuthorization !== void 0 && credential.authorization === refusedAuthorization) {
139365
+ if (credential.authorization === refusedAuthorizations.get(attachment.sessionId)) {
139324
139366
  await deps.sleep(deps.backoffMs);
139325
139367
  continue;
139326
139368
  }
139327
- refusedAuthorization = void 0;
139369
+ refusedAuthorizations.delete(attachment.sessionId);
139328
139370
  let outcome;
139329
139371
  try {
139330
139372
  outcome = await attempt(attachment, credential);
@@ -139352,7 +139394,7 @@ function createRcStreamHub(deps) {
139352
139394
  continue;
139353
139395
  }
139354
139396
  deps.log?.(`rc stream ${attachment.sessionId}: the client read stream refused the observed Authorization bearer past one retry; waiting for a fresh one`);
139355
- refusedAuthorization = credential.authorization;
139397
+ refusedAuthorizations.set(attachment.sessionId, credential.authorization);
139356
139398
  await deps.sleep(deps.backoffMs);
139357
139399
  retriedUnauthorized = false;
139358
139400
  }
@@ -139392,6 +139434,7 @@ function createRcStreamHub(deps) {
139392
139434
  attachment.stop();
139393
139435
  }
139394
139436
  attachments.clear();
139437
+ refusedAuthorizations.clear();
139395
139438
  }
139396
139439
  };
139397
139440
  }
@@ -140022,10 +140065,8 @@ function realFrontDoorSupervisorPorts(paths, respawnGeneration, lifecycle, track
140022
140065
  catalogueKeeper.tick();
140023
140066
  everyFreshnessInterval(catalogueKeeper.tick);
140024
140067
  const headroomDecision = createHeadroomDecision(createRoutingSettingReader(paths));
140025
- const targetObservations = createTargetObservationStore(realFarmFs, paths);
140026
140068
  const recordUsage = (record2) => {
140027
140069
  withQuotaRefresh(usageStore.record, quotaRefresher)(record2);
140028
- targetObservations.observe(record2);
140029
140070
  };
140030
140071
  const usageMiddleware = createUsageMiddleware({
140031
140072
  // Each finished response also tells the per-session headroom decision when the session's cache was last touched and whether it read from it.
package/dist/index.cjs CHANGED
@@ -4762,7 +4762,7 @@ var init_package = __esm({
4762
4762
  "package.json"() {
4763
4763
  package_default = {
4764
4764
  name: "agent-shim",
4765
- version: "11.72.0",
4765
+ version: "11.72.1",
4766
4766
  description: "A profile manager and launcher for Claude Code that lets one person run multiple logins from one machine while controlling what gets shared between them.",
4767
4767
  license: "MIT",
4768
4768
  author: "Joseph Mearman <joseph@mearman.co.uk>",
@@ -7606,36 +7606,49 @@ function readUsageLog(fs14, logDir, options = {}) {
7606
7606
  if (sinceMs !== void 0 && segment.endsAt <= sinceMs) {
7607
7607
  continue;
7608
7608
  }
7609
- const contents = fs14.readFileUtf8(import_node_path20.default.join(logDir, segment.name));
7610
- if (contents === void 0) {
7609
+ const read = readSegment(fs14, logDir, segment);
7610
+ if (read === void 0) {
7611
7611
  continue;
7612
7612
  }
7613
- const lines = contents.split("\n");
7614
- lines.pop();
7615
- for (const line of lines) {
7616
- if (line === "") {
7617
- continue;
7618
- }
7619
- let raw;
7620
- try {
7621
- raw = JSON.parse(line);
7622
- } catch {
7623
- invalidLines += 1;
7624
- continue;
7625
- }
7626
- const parsed = UsageRecordSchema.safeParse(raw);
7627
- if (!parsed.success) {
7628
- invalidLines += 1;
7629
- continue;
7630
- }
7631
- if (sinceMs === void 0 || Date.parse(parsed.data.at) >= sinceMs) {
7632
- records.push(parsed.data);
7613
+ invalidLines += read.invalidLines;
7614
+ for (const record of read.records) {
7615
+ if (sinceMs === void 0 || Date.parse(record.at) >= sinceMs) {
7616
+ records.push(record);
7633
7617
  }
7634
7618
  }
7635
7619
  }
7636
7620
  records.sort((left, right) => left.at.localeCompare(right.at));
7637
7621
  return { records, invalidLines };
7638
7622
  }
7623
+ function readSegment(fs14, logDir, segment) {
7624
+ const contents = fs14.readFileUtf8(import_node_path20.default.join(logDir, segment.name));
7625
+ if (contents === void 0) {
7626
+ return void 0;
7627
+ }
7628
+ const records = [];
7629
+ let invalidLines = 0;
7630
+ const lines = contents.split("\n");
7631
+ lines.pop();
7632
+ for (const line of lines) {
7633
+ if (line === "") {
7634
+ continue;
7635
+ }
7636
+ let raw;
7637
+ try {
7638
+ raw = JSON.parse(line);
7639
+ } catch {
7640
+ invalidLines += 1;
7641
+ continue;
7642
+ }
7643
+ const parsed = UsageRecordSchema.safeParse(raw);
7644
+ if (!parsed.success) {
7645
+ invalidLines += 1;
7646
+ continue;
7647
+ }
7648
+ records.push(parsed.data);
7649
+ }
7650
+ return { records, invalidLines };
7651
+ }
7639
7652
  function snapshotPath(snapshotsDir, identity) {
7640
7653
  if (!isIdentityName(identity)) {
7641
7654
  throw new Error(`"${identity}" is not a valid identity name.`);
@@ -19527,6 +19540,7 @@ function createRcEventFanout() {
19527
19540
  function createRcStreamHub(deps) {
19528
19541
  const attachments = /* @__PURE__ */ new Map();
19529
19542
  let closed = false;
19543
+ const refusedAuthorizations = /* @__PURE__ */ new Map();
19530
19544
  const lastSavedSequenceNums = /* @__PURE__ */ new Map();
19531
19545
  const persistCursor = (sessionId) => {
19532
19546
  if (deps.saveSequenceNum === void 0) {
@@ -19630,7 +19644,6 @@ function createRcStreamHub(deps) {
19630
19644
  };
19631
19645
  const runLoop = async (attachment) => {
19632
19646
  let retriedUnauthorized = false;
19633
- let refusedAuthorization;
19634
19647
  for (; ; ) {
19635
19648
  if (attachment.isStopped()) {
19636
19649
  return;
@@ -19646,11 +19659,11 @@ function createRcStreamHub(deps) {
19646
19659
  }
19647
19660
  return;
19648
19661
  }
19649
- if (refusedAuthorization !== void 0 && credential.authorization === refusedAuthorization) {
19662
+ if (credential.authorization === refusedAuthorizations.get(attachment.sessionId)) {
19650
19663
  await deps.sleep(deps.backoffMs);
19651
19664
  continue;
19652
19665
  }
19653
- refusedAuthorization = void 0;
19666
+ refusedAuthorizations.delete(attachment.sessionId);
19654
19667
  let outcome;
19655
19668
  try {
19656
19669
  outcome = await attempt(attachment, credential);
@@ -19678,7 +19691,7 @@ function createRcStreamHub(deps) {
19678
19691
  continue;
19679
19692
  }
19680
19693
  deps.log?.(`rc stream ${attachment.sessionId}: the client read stream refused the observed Authorization bearer past one retry; waiting for a fresh one`);
19681
- refusedAuthorization = credential.authorization;
19694
+ refusedAuthorizations.set(attachment.sessionId, credential.authorization);
19682
19695
  await deps.sleep(deps.backoffMs);
19683
19696
  retriedUnauthorized = false;
19684
19697
  }
@@ -19718,6 +19731,7 @@ function createRcStreamHub(deps) {
19718
19731
  attachment.stop();
19719
19732
  }
19720
19733
  attachments.clear();
19734
+ refusedAuthorizations.clear();
19721
19735
  }
19722
19736
  };
19723
19737
  }
package/dist/index.mjs CHANGED
@@ -4738,7 +4738,7 @@ var init_package = __esm({
4738
4738
  "package.json"() {
4739
4739
  package_default = {
4740
4740
  name: "agent-shim",
4741
- version: "11.72.0",
4741
+ version: "11.72.1",
4742
4742
  description: "A profile manager and launcher for Claude Code that lets one person run multiple logins from one machine while controlling what gets shared between them.",
4743
4743
  license: "MIT",
4744
4744
  author: "Joseph Mearman <joseph@mearman.co.uk>",
@@ -7582,36 +7582,49 @@ function readUsageLog(fs14, logDir, options = {}) {
7582
7582
  if (sinceMs !== void 0 && segment.endsAt <= sinceMs) {
7583
7583
  continue;
7584
7584
  }
7585
- const contents = fs14.readFileUtf8(path20.join(logDir, segment.name));
7586
- if (contents === void 0) {
7585
+ const read = readSegment(fs14, logDir, segment);
7586
+ if (read === void 0) {
7587
7587
  continue;
7588
7588
  }
7589
- const lines = contents.split("\n");
7590
- lines.pop();
7591
- for (const line of lines) {
7592
- if (line === "") {
7593
- continue;
7594
- }
7595
- let raw;
7596
- try {
7597
- raw = JSON.parse(line);
7598
- } catch {
7599
- invalidLines += 1;
7600
- continue;
7601
- }
7602
- const parsed = UsageRecordSchema.safeParse(raw);
7603
- if (!parsed.success) {
7604
- invalidLines += 1;
7605
- continue;
7606
- }
7607
- if (sinceMs === void 0 || Date.parse(parsed.data.at) >= sinceMs) {
7608
- records.push(parsed.data);
7589
+ invalidLines += read.invalidLines;
7590
+ for (const record of read.records) {
7591
+ if (sinceMs === void 0 || Date.parse(record.at) >= sinceMs) {
7592
+ records.push(record);
7609
7593
  }
7610
7594
  }
7611
7595
  }
7612
7596
  records.sort((left, right) => left.at.localeCompare(right.at));
7613
7597
  return { records, invalidLines };
7614
7598
  }
7599
+ function readSegment(fs14, logDir, segment) {
7600
+ const contents = fs14.readFileUtf8(path20.join(logDir, segment.name));
7601
+ if (contents === void 0) {
7602
+ return void 0;
7603
+ }
7604
+ const records = [];
7605
+ let invalidLines = 0;
7606
+ const lines = contents.split("\n");
7607
+ lines.pop();
7608
+ for (const line of lines) {
7609
+ if (line === "") {
7610
+ continue;
7611
+ }
7612
+ let raw;
7613
+ try {
7614
+ raw = JSON.parse(line);
7615
+ } catch {
7616
+ invalidLines += 1;
7617
+ continue;
7618
+ }
7619
+ const parsed = UsageRecordSchema.safeParse(raw);
7620
+ if (!parsed.success) {
7621
+ invalidLines += 1;
7622
+ continue;
7623
+ }
7624
+ records.push(parsed.data);
7625
+ }
7626
+ return { records, invalidLines };
7627
+ }
7615
7628
  function snapshotPath(snapshotsDir, identity) {
7616
7629
  if (!isIdentityName(identity)) {
7617
7630
  throw new Error(`"${identity}" is not a valid identity name.`);
@@ -19112,6 +19125,7 @@ function createRcEventFanout() {
19112
19125
  function createRcStreamHub(deps) {
19113
19126
  const attachments = /* @__PURE__ */ new Map();
19114
19127
  let closed = false;
19128
+ const refusedAuthorizations = /* @__PURE__ */ new Map();
19115
19129
  const lastSavedSequenceNums = /* @__PURE__ */ new Map();
19116
19130
  const persistCursor = (sessionId) => {
19117
19131
  if (deps.saveSequenceNum === void 0) {
@@ -19215,7 +19229,6 @@ function createRcStreamHub(deps) {
19215
19229
  };
19216
19230
  const runLoop = async (attachment) => {
19217
19231
  let retriedUnauthorized = false;
19218
- let refusedAuthorization;
19219
19232
  for (; ; ) {
19220
19233
  if (attachment.isStopped()) {
19221
19234
  return;
@@ -19231,11 +19244,11 @@ function createRcStreamHub(deps) {
19231
19244
  }
19232
19245
  return;
19233
19246
  }
19234
- if (refusedAuthorization !== void 0 && credential.authorization === refusedAuthorization) {
19247
+ if (credential.authorization === refusedAuthorizations.get(attachment.sessionId)) {
19235
19248
  await deps.sleep(deps.backoffMs);
19236
19249
  continue;
19237
19250
  }
19238
- refusedAuthorization = void 0;
19251
+ refusedAuthorizations.delete(attachment.sessionId);
19239
19252
  let outcome;
19240
19253
  try {
19241
19254
  outcome = await attempt(attachment, credential);
@@ -19263,7 +19276,7 @@ function createRcStreamHub(deps) {
19263
19276
  continue;
19264
19277
  }
19265
19278
  deps.log?.(`rc stream ${attachment.sessionId}: the client read stream refused the observed Authorization bearer past one retry; waiting for a fresh one`);
19266
- refusedAuthorization = credential.authorization;
19279
+ refusedAuthorizations.set(attachment.sessionId, credential.authorization);
19267
19280
  await deps.sleep(deps.backoffMs);
19268
19281
  retriedUnauthorized = false;
19269
19282
  }
@@ -19303,6 +19316,7 @@ function createRcStreamHub(deps) {
19303
19316
  attachment.stop();
19304
19317
  }
19305
19318
  attachments.clear();
19319
+ refusedAuthorizations.clear();
19306
19320
  }
19307
19321
  };
19308
19322
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-shim",
3
- "version": "11.72.0",
3
+ "version": "11.72.1",
4
4
  "description": "A profile manager and launcher for Claude Code that lets one person run multiple logins from one machine while controlling what gets shared between them.",
5
5
  "license": "MIT",
6
6
  "author": "Joseph Mearman <joseph@mearman.co.uk>",