pi-agy-acp 0.2.7 → 0.2.8

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/session-db.ts CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  } from 'node:fs'
14
14
  import { isAbsolute, join, relative } from 'node:path'
15
15
  import { DatabaseSync } from 'node:sqlite'
16
+ import type { LlmErrorCode } from './protocol.ts'
16
17
 
17
18
  const EMPTY_BYTES = new Uint8Array(0)
18
19
  const MAX_VARINT_SHIFT = 63n
@@ -47,13 +48,15 @@ export type HiddenTurnErrorKind =
47
48
 
48
49
  export interface HiddenTurnError {
49
50
  kind: HiddenTurnErrorKind
50
- code:
51
+ code: Extract<
52
+ LlmErrorCode,
51
53
  | 'QUOTA'
52
54
  | 'PROVIDER_ERROR'
53
55
  | 'RATE_LIMIT'
54
56
  | 'SERVER'
55
57
  | 'UNKNOWN_MODEL'
56
58
  | 'INVALID_REQUEST'
59
+ >
57
60
  message: string
58
61
  dirtySession?: boolean
59
62
  resetTimestamp?: string
@@ -395,12 +398,28 @@ function extractPayloadField(
395
398
  return undefined
396
399
  }
397
400
 
398
- const QUOTA_EXHAUSTED_RE =
399
- /(?:"reason"\s*:\s*"QUOTA_EXHAUSTED"|\breason:QUOTA_EXHAUSTED\b)/
401
+ /** Context-window overflow phrases shared with runtime error classification. */
402
+ export const CONTEXT_WINDOW_RE =
403
+ /context.*(length|window|limit|overflow|exceeded)|too long|token limit|maximum.*tokens/i
404
+ const HARD_QUOTA_RE =
405
+ /\bquota_exhausted\b|quota|prepaid\s+balance|spend\s+cap|insufficient\s+(?:funds|credits?|balance)|billing\s+(?:limit|exhausted|disabled|required)/i
406
+ const SOFT_QUOTA_RE = /\bexceeded your\b|\blimit reached\b/i
407
+ const RATE_LIMIT_PHRASE_RE = /\brate[\s-]+limit\b/i
408
+
409
+ /**
410
+ * Whether an error payload or message explicitly indicates quota/billing
411
+ * exhaustion rather than a rate limit or context-window overflow.
412
+ */
413
+ export function isExplicitQuotaError(text: string): boolean {
414
+ if (HARD_QUOTA_RE.test(text)) return true
415
+ if (!SOFT_QUOTA_RE.test(text)) return false
416
+ return !RATE_LIMIT_PHRASE_RE.test(text) && !CONTEXT_WINDOW_RE.test(text)
417
+ }
418
+
400
419
  const INVALID_THINKING_SIG_RE =
401
420
  /\bmessages\.\d+\.content\.\d+(?:\.thinking\.signature: Field required|: Invalid `signature` in `thinking` block)/
402
421
  const RATE_LIMIT_RE =
403
- /(?:HTTP\s+429|\(code\s+429\)|"code"\s*:\s*429|\bRESOURCE_EXHAUSTED\b)/
422
+ /(?:HTTP\s+429|\(code\s+429\)|"code"\s*:\s*429|\bRESOURCE_EXHAUSTED\b|\brate[\s-]+limit\b|\btoo many requests\b|\boverloaded\b|\bcapacity\b)/i
404
423
  const NOT_FOUND_RE =
405
424
  /(?:HTTP\s+404|\(code\s+404\)|"code"\s*:\s*404|\bNOT_FOUND\b)/
406
425
  const INVALID_ARGUMENT_RE =
@@ -416,7 +435,7 @@ export function classifyStepErrorPayload(
416
435
  ): HiddenTurnError | undefined {
417
436
  if (typeof payload !== 'string' || payload.length === 0) return undefined
418
437
 
419
- if (QUOTA_EXHAUSTED_RE.test(payload)) {
438
+ if (isExplicitQuotaError(payload)) {
420
439
  const resetTimestamp = extractPayloadField(payload, 'quotaResetTimeStamp')
421
440
  const rawDelay =
422
441
  extractPayloadField(payload, 'quotaResetDelay') ??
package/src/util.ts CHANGED
@@ -18,6 +18,11 @@ import {
18
18
  } from 'node:fs'
19
19
  import { basename, dirname, join } from 'node:path'
20
20
 
21
+ /** Whether `value` is a non-null, non-array object. */
22
+ export function isRecord(value: unknown): value is Record<string, unknown> {
23
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
24
+ }
25
+
21
26
  /** A promise with externally exposed settlement. */
22
27
  export interface Deferred<T> {
23
28
  promise: Promise<T>