Skip to content

fix(synthesis): keep the service worker alive during the reduce#197

Merged
bryanmatthewsimonson merged 1 commit into
mainfrom
fix/corpus-reduce-sw-keepalive
Jul 18, 2026
Merged

fix(synthesis): keep the service worker alive during the reduce#197
bryanmatthewsimonson merged 1 commit into
mainfrom
fix/corpus-reduce-sw-keepalive

Conversation

@bryanmatthewsimonson

Copy link
Copy Markdown
Owner

Problem

Immediately after #196 (raising the reduce output cap to 32768), corpus synthesis started failing with "Synthesis failed: no response".

Root cause is a service-worker lifetime interaction the previous fix exposed:

  • The reduce is the one long single fetch in the corpus flow (~350–455s for a large breadth brief; the 32768 cap makes it generate longer).
  • The map phase survives because its many short xray:llm:corpus-map messages naturally reset the MV3 idle timer.
  • After the last map lands, the reduce runs "cold" — no messages for minutes — so Chrome tears the background SW down mid-fetch. chrome.runtime.sendMessage's callback then fires with undefined (lastError = "message port closed"), which synthesis-block.js flattened to the opaque "no response."

Fix (mirrors the reader's proven pattern)

The reader already solves "long single LLM call in the SW" with startSwKeepalive() (src/reader/index.js ~2599, used by runQuickAudit). The corpus synthesis had no keepalive. This adds it:

  • startSwKeepalive() in synthesis-block.js — a 20s setInterval ping to the zero-cost xray:llm:corpus-config handler, spanning the whole run (declared before the try, stop() in the finally so it's cleaned up on every exit path). Each onMessage delivery resets the idle timer, keeping the SW alive through the cold reduce.
  • sendMessage() now reads chrome.runtime.lastError and resolves {ok:false, error, swLost:true} instead of a bare undefined — a lost SW surfaces a real reason plus an actionable "try again — the cached extracts make the retry cheap" hint, instead of "no response."
  • CORPUS_REDUCE_TIMEOUT_MS 420000 → 480000 — with the SW now kept alive, the AbortController is the sole limiter, so give the largest breadth briefs headroom.

Verification

  • 5-dimension adversarial review (keepalive lifecycle · sendMessage blast radius · MV3 semantics · render-guard interaction · timeout/abort), each finding independently verified → 0 confirmed, 0 uncertain (1 finding refuted as pre-existing/strictly-improved).
  • npm run build clean · npm test 1976 pass / 0 fail · npm run lint 0 errors.

🤖 Generated with Claude Code

Raising MAX_REDUCE_OUTPUT_TOKENS to 32768 made the reduce generate longer,
and the reduce is the one long single fetch in the corpus flow. The map
phase's many short messages naturally reset the MV3 idle timer, but the
reduce runs "cold" afterward — no messages for minutes — so Chrome tears
the background SW down mid-fetch and chrome.runtime.sendMessage's callback
fires with undefined (lastError: "message port closed"), surfaced as the
opaque "Synthesis failed: no response".

Mirror the reader's proven startSwKeepalive() (runQuickAudit uses it for
its single-shot audit): a 20s ping to the zero-cost xray:llm:corpus-config
handler spanning the whole run (declared before the try, stopped in the
finally on every exit path) keeps the SW alive during the cold reduce.

Also: sendMessage() now reads chrome.runtime.lastError and returns
{ok:false, error, swLost:true} instead of a bare undefined, so a lost SW
surfaces a real reason + a "try again, cached extracts make the retry
cheap" hint; and CORPUS_REDUCE_TIMEOUT_MS 420000 -> 480000 (with the SW
kept alive, the AbortController is the sole limiter).

Verified with a 5-dimension adversarial review (keepalive lifecycle,
sendMessage blast radius, MV3 semantics, render-guard interaction,
timeout) — 0 confirmed findings.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@bryanmatthewsimonson
bryanmatthewsimonson merged commit cf8820d into main Jul 18, 2026
1 check passed
@bryanmatthewsimonson
bryanmatthewsimonson deleted the fix/corpus-reduce-sw-keepalive branch July 18, 2026 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant