dsh-xdpool 2.6.5 → 2.6.7

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.d.ts CHANGED
@@ -577,8 +577,17 @@ export declare class WorkBuddyUpstreamClient {
577
577
  * list is missing half its entries for the rest of the session".
578
578
  */
579
579
  private fetchModelsOnce;
580
- /** Read-only credits query, aggregated by package. Does not consume credits. */
581
- fetchCredits(credential: WorkBuddyCredential): Promise<WorkBuddyCredits>;
580
+ /**
581
+ * Read-only credits query, aggregated by package. Does not consume credits.
582
+ *
583
+ * `signal` is the caller's cancellation, merged with the built-in timeout
584
+ * rather than replacing it: the pool's background warm must give up when the
585
+ * host shuts down, and a caller that has already gone away must not leave a
586
+ * 30 s request running. `AbortSignal.any` is already the repo's way of
587
+ * combining these (see the pricing probe in the shim), and the timeout stays
588
+ * in force so a signal-less caller keeps the old behaviour exactly.
589
+ */
590
+ fetchCredits(credential: WorkBuddyCredential, signal?: AbortSignal): Promise<WorkBuddyCredits>;
582
591
  /** Query today's check-in status without changing account state. */
583
592
  fetchCheckinStatus(credential: WorkBuddyCredential): Promise<WorkBuddyCheckinStatus>;
584
593
  /** Claim today's check-in reward. The browser route guards this mutation. */
@@ -842,6 +851,41 @@ interface TokenRefresher {
842
851
  domain?: string;
843
852
  }>;
844
853
  }
854
+ /**
855
+ * Minimal billing surface the pool needs to fill `creditExpiry` itself.
856
+ *
857
+ * Declared here for the same reason as {@link TokenRefresher}: the pool knows
858
+ * about accounts, not about HTTP, and importing `WorkBuddyUpstreamClient` would
859
+ * make the two modules circular. `fetchCredits` is OPTIONAL so every existing
860
+ * `TokenRefresher` — the test stubs, and any host that only wired token refresh
861
+ * — keeps working untouched; a client without it simply warms nothing.
862
+ */
863
+ interface CreditReader {
864
+ fetchCredits(credential: WorkBuddyCredential, signal?: AbortSignal): Promise<{
865
+ total: number;
866
+ /** Absent (or undefined) when no one-off pack has a deadline. */
867
+ nearestExpiryMs?: number | undefined;
868
+ }>;
869
+ }
870
+ /**
871
+ * What the pool knows about one account's one-off pack deadlines.
872
+ *
873
+ * The three `state`s are the whole point of the B+ fix: "never asked", "asked,
874
+ * nothing has a deadline" and "asked, here is the deadline" need different
875
+ * handling, and collapsing them into one absent-key predicate is what made a
876
+ * pure monthly-pack account be re-read forever (S1 in
877
+ * `EXPIRY-ADVERSARIAL-20261111.md`). `stale` is orthogonal: the reading is real
878
+ * but older than {@link EXPIRY_READING_TTL_MS}, so it describes the past.
879
+ */
880
+ interface ExpiryReading {
881
+ state: 'unread' | 'none' | 'at';
882
+ /** Deadline, epoch ms; present exactly when `state === 'at'`. */
883
+ at?: number;
884
+ /** When this reading was written, epoch ms; absent for `unread`. */
885
+ readAtMs?: number;
886
+ /** True when the reading is older than {@link EXPIRY_READING_TTL_MS}. */
887
+ stale: boolean;
888
+ }
845
889
  /** Live auth file name the WorkBuddy desktop app writes. */
846
890
  export declare const WORKBUDDY_LIVE_FILENAME = "workbuddy-desktop.info";
847
891
  /** Snapshot files left behind by previous logins share this prefix. */
@@ -970,9 +1014,16 @@ interface AccountPoolOptions {
970
1014
  */
971
1015
  credentialDeadCooldownMs?: number;
972
1016
  /** Upstream client used to refresh near-expiry tokens. */
973
- client?: TokenRefresher;
1017
+ client?: TokenRefresher & Partial<CreditReader>;
974
1018
  /** Refresh this long before actual expiry; default five minutes. */
975
1019
  refreshMarginMs?: number;
1020
+ /**
1021
+ * How long a credit-pack deadline reading is trusted, in ms.
1022
+ *
1023
+ * Defaults to {@link EXPIRY_READING_TTL_MS}; overridable so a test can age a
1024
+ * reading without sleeping for an hour.
1025
+ */
1026
+ expiryReadingTtlMs?: number;
976
1027
  /**
977
1028
  * How requests are spread across the pool.
978
1029
  *
@@ -1024,6 +1075,7 @@ export declare class WorkBuddyAccountPool {
1024
1075
  private credentialDeadCooldownMs;
1025
1076
  private readonly client;
1026
1077
  private readonly refreshMarginMs;
1078
+ private readonly expiryReadingTtlMs;
1027
1079
  private accounts;
1028
1080
  /**
1029
1081
  * Files the last scan could not read, with the reason.
@@ -1119,10 +1171,41 @@ export declare class WorkBuddyAccountPool {
1119
1171
  * their own cycle and are therefore never urgent — which is why this is
1120
1172
  * written from the same `fetchCredits` reading that fills `creditBalances`.
1121
1173
  *
1122
- * An absent entry means "never probed, or nothing is about to expire". Both
1123
- * are the same thing to `expiry` mode: not urgent, so it sorts last.
1174
+ * A positive value is a deadline. The two other states are kept in
1175
+ * {@link creditExpiryNone} and {@link creditExpiryReadAt} rather than as a
1176
+ * missing key, because a missing key cannot tell "we never looked" from "we
1177
+ * looked and nothing has a deadline" — and only the first is worth reading
1178
+ * again. An account whose packs are all monthly reads as `none` forever, which
1179
+ * is exactly right: asking again cannot change the answer (S1).
1124
1180
  */
1125
1181
  private creditExpiry;
1182
+ /**
1183
+ * Accounts whose last reading found NO one-off deadline (all monthly packs, or
1184
+ * none at all). Written instead of deleting the key, so the account stops
1185
+ * being a candidate for re-reading.
1186
+ */
1187
+ private creditExpiryNone;
1188
+ /** When each account's deadline reading was taken, epoch ms. */
1189
+ private creditExpiryReadAt;
1190
+ /**
1191
+ * Accounts whose deadline reading is being fetched right now.
1192
+ *
1193
+ * Same de-duplication idea as {@link refreshInflight}, for the same reason: a
1194
+ * scan-triggered warm and a request-triggered top-up must not both spend a
1195
+ * billing round trip on the same account. A Set rather than a Map of promises
1196
+ * because no caller ever awaits the warm — the point is only that a second
1197
+ * pass skips it.
1198
+ */
1199
+ private expiryWarmInflight;
1200
+ /**
1201
+ * Last time the pool tried to warm an account's deadline reading, epoch ms.
1202
+ *
1203
+ * The TTL is the success gate, not a retry gate: a failed read writes nothing,
1204
+ * so without this a permanently failing billing endpoint would be hit on every
1205
+ * scan and every served request. One attempt per account per minute is cheap
1206
+ * and still recovers quickly from a transient failure.
1207
+ */
1208
+ private expiryWarmAttemptAt;
1126
1209
  /**
1127
1210
  * Last time each account served a request, epoch ms. Drives the idle term
1128
1211
  * of the priority-mode weighting below: an account that just served loses to
@@ -1217,6 +1300,47 @@ export declare class WorkBuddyAccountPool {
1217
1300
  private pruneExpiredSnapshots;
1218
1301
  /** Rescan the auth directories and merge newly discovered accounts. */
1219
1302
  scan(): Promise<WorkBuddyAccount[]>;
1303
+ /**
1304
+ * Warm the deadline readings that `expiry` mode sorts on.
1305
+ *
1306
+ * Fire-and-forget on purpose, from `scan()`: the pool's first request arrives
1307
+ * before any reading exists, and the honest answer to that is the documented
1308
+ * fallback plus a log line, not a stalled request. Everything about the call
1309
+ * is chosen to be unable to make things worse:
1310
+ *
1311
+ * - `this.client` may not exist (a host that wired only token refresh), in
1312
+ * which case there is nothing to read with and the pass is a no-op — the
1313
+ * same guard `ensureFresh` uses;
1314
+ * - a dead sign-in is skipped, and a successful read NEVER calls
1315
+ * `clearCredentialDead`. That is the difference between this and
1316
+ * `reviveBenched`: the billing endpoint answers 200 for a banned account
1317
+ * (observed on 11140), so treating a read as proof of health is what got a
1318
+ * banned account revived six times in a day (S2). Only a token refresh or a
1319
+ * fresh sign-in may clear that mark;
1320
+ * - accounts whose reading is fresh enough are skipped, and so are accounts
1321
+ * already being read (S10's de-duplication, mirroring `refreshInflight`);
1322
+ * - a failed read writes nothing and only warns, so the next pass tries
1323
+ * again without the failure being mistaken for a reading.
1324
+ */
1325
+ private warmExpiryReadings;
1326
+ /** One account's warm read; see {@link warmExpiryReadings} for the rules. */
1327
+ private warmOneExpiryReading;
1328
+ /**
1329
+ * Top up one account's deadline reading, from wherever the pool notices it is
1330
+ * missing or stale.
1331
+ *
1332
+ * The request path is the second caller (after `scan()`), because a reading
1333
+ * ages out while the pool is busy too: the shim calls this for the account that
1334
+ * just served a request, which is the one whose spend is most likely to have
1335
+ * moved its deadline. No timer is created — the existing balance refresh is
1336
+ * already the natural heartbeat, and this rides on it.
1337
+ *
1338
+ * Every gate lives here rather than in the callers, so both paths obey exactly
1339
+ * the same rules: no client, no account, a rejected sign-in, an already-running
1340
+ * read, a finished "nothing expires" answer, a fresh reading, or an attempt
1341
+ * made within the last minute all mean "do nothing".
1342
+ */
1343
+ warmExpiryReadingFor(accountId: string): void;
1220
1344
  /** All accounts, cooldown state included. */
1221
1345
  list(region?: WorkBuddyRegion): readonly WorkBuddyAccount[];
1222
1346
  /**
@@ -1338,6 +1462,14 @@ export declare class WorkBuddyAccountPool {
1338
1462
  * A pool where nobody has a known expiry therefore behaves exactly like
1339
1463
  * priority, which is the honest degradation: without a reading there is
1340
1464
  * nothing to sort by.
1465
+ *
1466
+ * What this method must NOT do is pretend. With only SOME readings in hand it
1467
+ * still has to fall back to `pool[0]`, because picking the earliest of the
1468
+ * accounts that happen to have answered would silently drop the account that
1469
+ * really dies first — the failure mode S0 in `EXPIRY-ADVERSARIAL-20261111.md`
1470
+ * called "worse than not fixing it", since the pick looks plausible and is
1471
+ * wrong. So the incomplete case is announced instead: the caller keeps the old
1472
+ * semantics and the log says which accounts were not read.
1341
1473
  */
1342
1474
  private pickByExpiry;
1343
1475
  /**
@@ -1430,10 +1562,50 @@ export declare class WorkBuddyAccountPool {
1430
1562
  * merged: a pack that has been spent or has died disappears from the next
1431
1563
  * reading, and keeping the stale deadline would pin the pool to an account
1432
1564
  * whose credits are already gone — the opposite of what `expiry` mode wants.
1565
+ *
1566
+ * "Nothing expires" is a real answer, not the absence of one: it is stored as
1567
+ * {@link creditExpiryNone} so the account is not read again on every pass.
1433
1568
  */
1434
1569
  noteCredits(accountId: string, balance: number, nearestExpiryMs?: number): void;
1570
+ /**
1571
+ * Record ONLY the deadline, never the balance.
1572
+ *
1573
+ * The scan-triggered warm exists to fill `expiry` mode's sort key. Writing the
1574
+ * balance as a side effect would change what the reserve floor decides — an
1575
+ * account could be pulled out of rotation by a background read the user never
1576
+ * asked for, which is a behaviour change outside this fix's scope (S7 in
1577
+ * `EXPIRY-ADVERSARIAL-20261111.md`; the reserve's own cold-start hole is V4,
1578
+ * fixed separately).
1579
+ */
1580
+ noteExpiryOnly(accountId: string, nearestExpiryMs?: number): void;
1581
+ /**
1582
+ * The three-state writer both entry points funnel through.
1583
+ *
1584
+ * One place, so the invariant "a finished reading always leaves a state" can be
1585
+ * read off a single function: either a deadline is stored, or the account joins
1586
+ * `creditExpiryNone`. Nothing here deletes the read stamp — that is what makes
1587
+ * "read, found nothing" distinguishable from "never read".
1588
+ */
1589
+ private noteExpiryReading;
1435
1590
  /** Nearest one-off pack expiry for one account, or undefined when none known. */
1436
1591
  creditExpiryOf(accountId: string): number | undefined;
1592
+ /**
1593
+ * The full three-state reading for one account, including staleness.
1594
+ *
1595
+ * `stale` is evaluated against {@link EXPIRY_READING_TTL_MS} here rather than
1596
+ * baked into the stored value, so the TTL stays a constant of the policy and
1597
+ * not of the data.
1598
+ *
1599
+ * Only a real deadline can go stale. "We never asked" is not a reading, and
1600
+ * "nothing here has a deadline" does not become less true with age: a monthly
1601
+ * pack does not grow an expiry date by waiting. Keeping `none` permanently
1602
+ * valid is what makes the S1 loop impossible — an account that answered once
1603
+ * is never read again by the expiry machinery, so there is no predicate that
1604
+ * can stay true forever and re-read it on every pass. If such an account does
1605
+ * gain a one-off pack later, the card refresh (which writes through
1606
+ * `noteCredits` on every poll) is what notices.
1607
+ */
1608
+ expiryReadingOf(accountId: string, now?: number): ExpiryReading;
1437
1609
  /** Last known balance for one account, or undefined when never read. */
1438
1610
  creditsOf(accountId: string): number | undefined;
1439
1611
  /** The credit floor the user set for one account; 0 when unset. */