@steve31415/baselib 3.62.1 → 3.62.3

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.
@@ -8,6 +8,10 @@ export declare const HUB_DEGRADED_SLEEP_MS = 2000;
8
8
  export declare const HUB_MINT_FLOOR_MS: number;
9
9
  /** A token is replaced once it is this old (the hub's TTL is one hour). */
10
10
  export declare const HUB_TOKEN_REFRESH_MS: number;
11
+ /** The longest a park waits for a refresh in flight before parking on the
12
+ * old token anyway; above the callers' 10 s bound on one mint, so in
13
+ * practice the mint always settles first. */
14
+ export declare const HUB_REFRESH_WAIT_MS = 15000;
11
15
  /** Added to the caller's lookback (review S2), and the hub's cap on it. */
12
16
  export declare const HUB_LOOKBACK_SLACK_MS = 1000;
13
17
  export declare const HUB_LOOKBACK_MAX_MS = 60000;
@@ -36,6 +40,7 @@ export interface HubDaemonClientOptions {
36
40
  degradedSleepMs?: number;
37
41
  mintFloorMs?: number;
38
42
  refreshAfterMs?: number;
43
+ refreshWaitMs?: number;
39
44
  /** Injectable for tests. */
40
45
  fetch?: typeof fetch;
41
46
  /** Monotonic milliseconds (tests). */
@@ -53,6 +58,7 @@ export declare class HubDaemonClient {
53
58
  private readonly degradedSleepMs;
54
59
  private readonly mintFloorMs;
55
60
  private readonly refreshAfterMs;
61
+ private readonly refreshWaitMs;
56
62
  private readonly fetchFn;
57
63
  private readonly now;
58
64
  private readonly sleepFn;
@@ -76,6 +82,8 @@ export declare class HubDaemonClient {
76
82
  * caller's monotonic clock. Never throws. */
77
83
  park(lookbackMs: number, signal?: AbortSignal): Promise<HubParkOutcome>;
78
84
  private usableToken;
85
+ /** Wait for the mint in flight, at most refreshWaitMs or until `signal`. */
86
+ private awaitMint;
79
87
  private maybeMint;
80
88
  private enterDegraded;
81
89
  private leaveDegraded;
@@ -15,9 +15,13 @@
15
15
  // - It never sends Authorization: the app's device bearer stays on the
16
16
  // daemon ↔ app path (review S4). The hub token goes in X-Hub-Token.
17
17
  // - One attempt per park, bounded at the park plus 10 s; no retry budget.
18
- // - The token is minted off the loop's critical path, at most once per
19
- // HUB_MINT_FLOOR_MS whatever the outcome (review S3); until a token
18
+ // - The first token is minted off the loop's critical path, at most once
19
+ // per HUB_MINT_FLOOR_MS whatever the outcome (review S3); until a token
20
20
  // exists the loop runs degraded (direct requests every 2 s).
21
+ // - The park that starts a refresh waits for it (bounded) before sending
22
+ // its wait: the hub kills the old token the moment the new one
23
+ // registers, so a wait sent on the old token alongside the refresh
24
+ // could reach the hub's token lookup after that and be refused (3.62.3).
21
25
  // - After a 401 it never presents that token again until a new mint
22
26
  // (§14.4: the hub's per-address limit on bad tokens is shared by every
23
27
  // daemon on the host).
@@ -36,6 +40,10 @@ export const HUB_DEGRADED_SLEEP_MS = 2_000;
36
40
  export const HUB_MINT_FLOOR_MS = 5 * 60_000;
37
41
  /** A token is replaced once it is this old (the hub's TTL is one hour). */
38
42
  export const HUB_TOKEN_REFRESH_MS = 50 * 60_000;
43
+ /** The longest a park waits for a refresh in flight before parking on the
44
+ * old token anyway; above the callers' 10 s bound on one mint, so in
45
+ * practice the mint always settles first. */
46
+ export const HUB_REFRESH_WAIT_MS = 15_000;
39
47
  /** Added to the caller's lookback (review S2), and the hub's cap on it. */
40
48
  export const HUB_LOOKBACK_SLACK_MS = 1_000;
41
49
  export const HUB_LOOKBACK_MAX_MS = 60_000;
@@ -50,6 +58,7 @@ export class HubDaemonClient {
50
58
  degradedSleepMs;
51
59
  mintFloorMs;
52
60
  refreshAfterMs;
61
+ refreshWaitMs;
53
62
  fetchFn;
54
63
  now;
55
64
  sleepFn;
@@ -68,6 +77,7 @@ export class HubDaemonClient {
68
77
  this.degradedSleepMs = opts.degradedSleepMs ?? HUB_DEGRADED_SLEEP_MS;
69
78
  this.mintFloorMs = opts.mintFloorMs ?? HUB_MINT_FLOOR_MS;
70
79
  this.refreshAfterMs = opts.refreshAfterMs ?? HUB_TOKEN_REFRESH_MS;
80
+ this.refreshWaitMs = opts.refreshWaitMs ?? HUB_REFRESH_WAIT_MS;
71
81
  this.fetchFn = opts.fetch ?? fetch;
72
82
  this.now = opts.now ?? (() => performance.now());
73
83
  this.sleepFn = opts.sleep ?? sleep;
@@ -91,6 +101,19 @@ export class HubDaemonClient {
91
101
  if (signal?.aborted)
92
102
  return 'stopped';
93
103
  this.maybeMint();
104
+ if (this.minting && this.usableToken()) {
105
+ // A refresh is in flight (we hold a token, so it is not the first
106
+ // mint). Its register replaces our row and kills the token we hold the
107
+ // instant it commits, while a wait sent now on that token could reach
108
+ // the hub's lookup after that commit and be refused with 401 — the
109
+ // daemon racing itself (task 105aa5f6: four WARNs a day on lurch). So
110
+ // park on the new token: one mint's latency every 50 minutes, in idle
111
+ // time. A refresh that fails or overruns leaves the old token, still
112
+ // valid, in place.
113
+ await this.awaitMint(signal);
114
+ if (signal?.aborted)
115
+ return 'stopped';
116
+ }
94
117
  const token = this.usableToken();
95
118
  if (!token) {
96
119
  // Quiet while the very first mint is still in flight; degraded after.
@@ -153,6 +176,19 @@ export class HubDaemonClient {
153
176
  this.token = null;
154
177
  return this.token;
155
178
  }
179
+ /** Wait for the mint in flight, at most refreshWaitMs or until `signal`. */
180
+ async awaitMint(signal) {
181
+ const minting = this.minting;
182
+ if (!minting)
183
+ return;
184
+ const settled = new AbortController();
185
+ try {
186
+ await Promise.race([minting, sleep(this.refreshWaitMs, signal ? AbortSignal.any([signal, settled.signal]) : settled.signal)]);
187
+ }
188
+ finally {
189
+ settled.abort();
190
+ }
191
+ }
156
192
  maybeMint() {
157
193
  const t = this.now();
158
194
  const needed = this.token === null || t - this.token.mintedAt >= this.refreshAfterMs;
@@ -2,7 +2,7 @@ import type { Runner } from '../deploy/types.js';
2
2
  import { type CliIo } from './cli.js';
3
3
  export declare const AXIOM_QUERY_URL = "https://api.axiom.co/v1/datasets/_apl?format=legacy";
4
4
  export declare const QUERY_TOKEN_SECRET = "shared--axiom-query-token";
5
- export declare const USAGE = "usage: pw-logs [options] [APL tail]\nQuery the fleet's Axiom logs (query-only token from Secret Manager).\n\n -d, --dataset <name> apps (default) | platform | rum\n -a, --app <name> app filter; default: the enclosing repo's deploy.config.json; \"all\" for none\n -l, --level <level> debug | info | warn | error; add \"+\" for that level and above (warn+)\n -s, --since <when> window start: duration back from now (30m, 6h, 3d) or timestamp (default 1h)\n -u, --until <when> window end, same forms (default now)\n -n, --limit <n> rows to return (default 200); events are the newest, printed oldest first\n --json print events (or aggregation rows) as a JSON array\n --all text mode: show every field (batch ids, full build_id)\n --apl print the query that would run and exit\n -h, --help\n\nThe tail is appended to the generated pipeline. The newest-first ordering\nis added only when the tail just filters or reshapes events (where, extend,\nproject-away/keep/rename, mv-expand, parse); a summarize, project, order or\ntake stage of your own decides the order. The limit always applies.\nExamples:\n pw-logs -l error -s 6h\n pw-logs -a chime \"where meta.user == 'x' | where message has 'sync'\"\n pw-logs -a all -l warn+ -s 24h \"summarize n=count() by app, message\"\n pw-logs -d platform -s 3d \"where severity != 'INFO'\"\nFields: apps = app, level, message, source, build_id, meta.* (a map: query\nsub-keys with dot notation, cast for summarize by \u2014 tostring(meta.user));\nplatform = service, severity, text, revision; rum = app, metric, value,\nrating, path, build_id.";
5
+ export declare const USAGE = "usage: pw-logs [options] [APL tail]\nQuery the fleet's Axiom logs (query-only token from Secret Manager).\n\n -d, --dataset <name> apps (default) | platform | rum\n -a, --app <name> app filter; default: the enclosing repo's deploy.config.json for\n apps/rum (\"all\" for none); platform has no default \u2014 it is keyed\n by GCP service name, not app, and a Cloud Run Job's service (e.g.\n radar-cf-probe, corpus-build) rarely equals its owning app's repo\n name, so pass -a explicitly there or you get every service\n -l, --level <level> debug | info | warn | error; add \"+\" for that level and above (warn+)\n -s, --since <when> window start: duration back from now (30m, 6h, 3d) or timestamp (default 1h)\n -u, --until <when> window end, same forms (default now)\n -n, --limit <n> rows to return (default 200); events are the newest, printed oldest first\n --json print events (or aggregation rows) as a JSON array\n --all text mode: show every field (batch ids, full build_id)\n --apl print the query that would run and exit\n -h, --help\n\nThe tail is appended to the generated pipeline. The newest-first ordering\nis added only when the tail just filters or reshapes events (where, extend,\nproject-away/keep/rename, mv-expand, parse); a summarize, project, order or\ntake stage of your own decides the order. The limit always applies.\nExamples:\n pw-logs -l error -s 6h\n pw-logs -a chime \"where meta.user == 'x' | where message has 'sync'\"\n pw-logs -a all -l warn+ -s 24h \"summarize n=count() by app, message\"\n pw-logs -d platform -s 3d \"where severity != 'INFO'\"\nFields: apps = app, level, message, source, build_id, meta.* (a map: query\nsub-keys with dot notation, cast for summarize by \u2014 tostring(meta.user));\nplatform = service, severity, text, revision; rum = app, metric, value,\nrating, path, build_id.";
6
6
  type Fields = Record<string, unknown>;
7
7
  interface DatasetSpec {
8
8
  appField: string;
package/dist/ops/logs.js CHANGED
@@ -23,7 +23,11 @@ export const USAGE = `usage: pw-logs [options] [APL tail]
23
23
  Query the fleet's Axiom logs (query-only token from Secret Manager).
24
24
 
25
25
  -d, --dataset <name> apps (default) | platform | rum
26
- -a, --app <name> app filter; default: the enclosing repo's deploy.config.json; "all" for none
26
+ -a, --app <name> app filter; default: the enclosing repo's deploy.config.json for
27
+ apps/rum ("all" for none); platform has no default — it is keyed
28
+ by GCP service name, not app, and a Cloud Run Job's service (e.g.
29
+ radar-cf-probe, corpus-build) rarely equals its owning app's repo
30
+ name, so pass -a explicitly there or you get every service
27
31
  -l, --level <level> debug | info | warn | error; add "+" for that level and above (warn+)
28
32
  -s, --since <when> window start: duration back from now (30m, 6h, 3d) or timestamp (default ${DEFAULT_SINCE})
29
33
  -u, --until <when> window end, same forms (default now)
@@ -245,7 +249,20 @@ export async function runLogs(options) {
245
249
  const limit = Number(v.limit);
246
250
  if (!Number.isInteger(limit) || limit < 1)
247
251
  throw new Error(`--limit must be a positive integer, got ${v.limit}`);
248
- let app = v.app ?? appConfig?.app;
252
+ // The enclosing repo's app name is only a sensible default when the
253
+ // dataset's own app-like field is literally named `app` (apps, rum).
254
+ // `platform` keys events by `service` — a GCP-level identifier that
255
+ // often diverges from the repo name (a Cloud Run Job such as
256
+ // `radar-cf-probe` or `corpus-build` belongs to an app's repo but
257
+ // never equals it) — so defaulting to the repo name there silently
258
+ // hides most of the dataset with no indication anything was filtered,
259
+ // unless the caller remembers --apl. Investigating from inside an
260
+ // app's repo with no explicit -a must see that app's own platform
261
+ // events too, which an exact-match default on the repo name would
262
+ // also miss. Explicit -a (any value, including a service/job name)
263
+ // always wins; only the implicit repo-name default is skipped.
264
+ const appsLikeDataset = datasetSpec(v.dataset).appField === 'app';
265
+ let app = v.app ?? (appsLikeDataset ? appConfig?.app : undefined);
249
266
  if (app === 'all')
250
267
  app = undefined;
251
268
  if (app !== undefined && !NAME.test(app))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve31415/baselib",
3
- "version": "3.62.1",
3
+ "version": "3.62.3",
4
4
  "description": "Plasticine new-world shared platform library: logging, auth, service-to-service auth, db, HTTP, sync, app updates",
5
5
  "type": "module",
6
6
  "license": "MIT",