Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): honour only a retry window the main process vetted
Alfredo Lopezbd759126279b
diff --git a/desktop-app/ui/src/lib/__tests__/gfsGrantErrors.test.ts b/desktop-app/ui/src/lib/__tests__/gfsGrantErrors.test.tsindex 7225cad6c..d29cffd16 100644--- a/desktop-app/ui/src/lib/__tests__/gfsGrantErrors.test.ts+++ b/desktop-app/ui/src/lib/__tests__/gfsGrantErrors.test.ts@@ -182,11 +182,24 @@ describe('describeGfsReadError', () => {parseRetryAfterSeconds('429 Too Many Requests: {"retryAfterSeconds":null,"limit":100}')).toBeNull()// Witness: the same parser does find the field when it is genuinely there,- // in both shapes the wire uses.+ // in the one shape the main process emits.expect(parseRetryAfterSeconds('429 Too Many Requests retryAfterSeconds=7')).toBe(7)+ })++ it('refuses a retry window the main process declined to trust', () => {+ // `parseGfsGrantErrorFields` reads the body field at the TOP level only, so+ // a nested one yields no `retryAfterSeconds=` suffix. httpClient then puts+ // the RAW body into the message (it carries no top-level `error`/`message`+ // key), and a separator-agnostic pattern read the nested value back out —+ // gating Retry and focus revalidation for the full 300s clamp on a number+ // the authoritative parser had already rejected.expect(- parseRetryAfterSeconds('429: {"error":"Too Many Requests","retryAfterSeconds": 3}')- ).toBe(3)+ parseRetryAfterSeconds('429 Too Many Requests: {"policy":{"retryAfterSeconds":3600}}')+ ).toBeNull()+ // Witness: the identical window IS honoured once the main process has+ // vetted it and republished it as its own suffix, so the assertion above+ // is about provenance and not about a parser that stopped working.+ expect(parseRetryAfterSeconds('429 Too Many Requests retryAfterSeconds=3600')).toBe(300)})it('bounds a retry window the server could never legitimately be asking for', () => {diff --git a/desktop-app/ui/src/lib/gfsGrantErrors.ts b/desktop-app/ui/src/lib/gfsGrantErrors.tsindex 55f0b3b1d..ec3b3e294 100644--- a/desktop-app/ui/src/lib/gfsGrantErrors.ts+++ b/desktop-app/ui/src/lib/gfsGrantErrors.ts@@ -71,16 +71,25 @@ const MAX_RETRY_AFTER_SECONDS = 300/*** Lift a retry window out of an error message.*- * The separator is matched explicitly (`=` from our own IPC suffix, `:` from a- * JSON body, with optional quote and spaces) rather than skipped with a- * permissive `[^\d]*`. A skipping pattern binds to the next number ANYWHERE in- * the message, so `{"retryAfterSeconds":null,"limit":100}` would yield 100 —- * a 100-second gate invented out of an unrelated field. It is also quadratic- * on long non-JSON bodies (an upstream proxy's HTML 429 page reaches this- * function verbatim), which freezes the renderer's main thread.+ * Only the main process may declare one. It parses the response body itself+ * (uriHandler `parseGfsGrantErrorFields`), accepts the field only at the TOP+ * level, and republishes what it accepted as its own ` retryAfterSeconds=N`+ * suffix. This pattern matches that suffix and nothing else.+ *+ * Matching `:` as well would re-admit exactly what the main process rejected.+ * `httpClient` puts the RAW response body into the error message whenever the+ * JSON carries no top-level `error`/`message` key, so a body such as+ * `{"policy":{"retryAfterSeconds":3600},"limit":100}` crosses IPC verbatim;+ * a separator-agnostic pattern reads the nested field and gates the UI for+ * five minutes on a value the authoritative parser refused to trust.+ *+ * The leading `(?:^|\s)` anchors to the suffix boundary, so a longer token+ * cannot smuggle the field in. Nothing scans for "the next number ANYWHERE",+ * which would both invent a window out of an unrelated field and run+ * quadratically over an upstream proxy's HTML 429 page.*/export function parseRetryAfterSeconds(message: string): number | null {- const match = message.match(/retryAfterSeconds["']?\s*[:=]\s*(\d{1,7})/)+ const match = message.match(/(?:^|\s)retryAfterSeconds=(\d{1,7})/)if (!match?.[1]) return nullconst seconds = Number.parseInt(match[1], 10)if (!Number.isInteger(seconds) || seconds < 0) return null