pi-agy-acp 0.2.1 → 0.2.2

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/src/quota.ts CHANGED
@@ -194,6 +194,27 @@ export interface QuotaSnapshot {
194
194
  account?: AccountInfo
195
195
  }
196
196
 
197
+ function parseBucket(raw: unknown): QuotaBucket | undefined {
198
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw))
199
+ return undefined
200
+ const bucket = raw as Record<string, unknown>
201
+ if (bucket.disabled === true) return undefined
202
+ const id = String(bucket.id ?? '').trim()
203
+ const window = String(bucket.window ?? '').trim()
204
+ if (id.length === 0 || window.length === 0) return undefined
205
+ return {
206
+ id,
207
+ name: String(bucket.name ?? ''),
208
+ window,
209
+ remainingFraction:
210
+ typeof bucket.remaining_fraction === 'number' &&
211
+ Number.isFinite(bucket.remaining_fraction)
212
+ ? bucket.remaining_fraction
213
+ : undefined,
214
+ resetTime: String(bucket.reset_time ?? ''),
215
+ }
216
+ }
217
+
197
218
  /**
198
219
  * Parse `agy -p /usage --output-format json` output.
199
220
  * @param stdout - command stdout.
@@ -215,20 +236,9 @@ export function parseUsage(stdout: string): QuotaGroup[] {
215
236
  return {
216
237
  name: String(group.name ?? ''),
217
238
  description: String(group.description ?? ''),
218
- buckets: (Array.isArray(group.buckets) ? group.buckets : []).map((b) => {
219
- const bucket = b as Record<string, unknown>
220
- return {
221
- id: String(bucket.id ?? ''),
222
- name: String(bucket.name ?? ''),
223
- window: String(bucket.window ?? ''),
224
- remainingFraction:
225
- typeof bucket.remaining_fraction === 'number' &&
226
- Number.isFinite(bucket.remaining_fraction)
227
- ? bucket.remaining_fraction
228
- : undefined,
229
- resetTime: String(bucket.reset_time ?? ''),
230
- }
231
- }),
239
+ buckets: (Array.isArray(group.buckets) ? group.buckets : [])
240
+ .map(parseBucket)
241
+ .filter((bucket): bucket is QuotaBucket => bucket !== undefined),
232
242
  }
233
243
  })
234
244
  }
@@ -236,27 +246,36 @@ export function parseUsage(stdout: string): QuotaGroup[] {
236
246
  /** Cached quota reader with request coalescing. */
237
247
  export class QuotaReader {
238
248
  private cached: QuotaSnapshot | undefined
239
- private cachedAt = 0
249
+ // Monotonic `performance.now()` starts near 0, so `-Infinity` guarantees an empty cache is never fresh.
250
+ private cachedAt = -Infinity
240
251
  private inflight: Promise<QuotaSnapshot> | undefined
241
252
  private readonly binary: () => string
242
253
  private readonly run: CommandRunner
243
254
  private readonly ttlMs: number
255
+ private readonly now: () => number
244
256
 
245
257
  /**
246
258
  * @param binary - returns the agy executable.
247
259
  * @param run - helper command runner.
248
260
  * @param ttlMs - cache lifetime.
261
+ * @param now - monotonic clock in milliseconds.
249
262
  */
250
- constructor(binary: () => string, run: CommandRunner, ttlMs: number) {
263
+ constructor(
264
+ binary: () => string,
265
+ run: CommandRunner,
266
+ ttlMs: number,
267
+ now: () => number = () => performance.now(),
268
+ ) {
251
269
  this.binary = binary
252
270
  this.run = run
253
271
  this.ttlMs = ttlMs
272
+ this.now = now
254
273
  }
255
274
 
256
275
  /** Fresh successful data only; never starts a command. */
257
276
  peek(): QuotaSnapshot | undefined {
258
277
  return this.cached?.status === 'ok' &&
259
- Date.now() - this.cachedAt < this.ttlMs
278
+ this.now() - this.cachedAt < this.ttlMs
260
279
  ? this.cached
261
280
  : undefined
262
281
  }
@@ -274,7 +293,7 @@ export class QuotaReader {
274
293
  if (
275
294
  !force &&
276
295
  this.cached !== undefined &&
277
- Date.now() - this.cachedAt < this.ttlMs
296
+ this.now() - this.cachedAt < this.ttlMs
278
297
  )
279
298
  return this.cached
280
299
  this.inflight ??= this.fetch().finally(() => {
@@ -317,7 +336,7 @@ export class QuotaReader {
317
336
  groups: parseUsage(result.stdout),
318
337
  }
319
338
  this.cached = snapshot
320
- this.cachedAt = Date.now()
339
+ this.cachedAt = this.now()
321
340
  return snapshot
322
341
  } catch (error) {
323
342
  const snapshot: QuotaSnapshot = {
@@ -328,7 +347,7 @@ export class QuotaReader {
328
347
  }
329
348
  // Apply the same TTL to failures so settled events cannot hammer an unavailable CLI.
330
349
  this.cached = snapshot
331
- this.cachedAt = Date.now()
350
+ this.cachedAt = this.now()
332
351
  return snapshot
333
352
  }
334
353
  }