Parallelize live synthesis fan-out (~3x faster), determinism preserved - #9
Merged
Merged
Conversation
…terminism preserved
The discovery agent loop is inherently serial (each turn feeds the next), but the
synthesis fan-out was running ~10-13 independent LLM calls one after another for no
reason. Run them on a thread pool instead — the calls are I/O-bound, so threads give
real wall-clock parallelism. Synthesis stage ~3x faster; the discovery loop is
untouched.
Design was vetted by an adversarial review pass before implementing; all of its
load-bearing safety fixes are folded in:
fanout.py — run_synthesis_fanout now submits the 7 report sections + per-opportunity
calls to a ThreadPoolExecutor and consumes the futures in SUBMISSION ORDER (never
as_completed). _merge / planning stay single-threaded on the main thread, so output
is byte-identical regardless of thread scheduling. The one real dependency
(pain-points pre-pass → opportunity seeds) is still a hard barrier in the caller.
- DISCOVERY_MAX_WORKERS env knob (default 4; =1 reverts to fully serial).
- _safe_report/_safe_opp catch failures INSIDE the worker so one bad section omits
itself, never aborts the suite — and they catch only LLMRetryableError, NOT a
plain LLMError, so a missing/invalid API key still aborts loudly instead of
silently producing an empty report.
llm.py — LLMRetryableError subclass; _provider_error classifies transient (429/529/
connection → retryable) vs hard (auth/config → plain LLMError, aborts). Locked
double-checked lazy client init; anthropic client built with max_retries=2 to absorb
transient blips without wedging pool slots. _write_cache is now atomic (temp in a
sibling .cache.tmp/ on the same FS, then os.replace) so concurrent same-key writes
can't tear a file or corrupt a --save-golden snapshot.
Rate-limit-adaptive omission: a section that can't be produced is omitted, warned
loudly, and on --save-golden is a hard error (a polished snapshot is never silently
short). Structural placeholder report specs (04's empty-instruction portfolio spec,
whose content comes from the opp seeds) are excluded from omission tracking to avoid
a false-positive warning.
VERIFIED:
- Golden byte-identical: o2c + p2p, workers=1 vs =4, AND vs the committed baseline —
zero behavior change, determinism preserved.
- 261 tests pass, 100% branch coverage, pyrefly clean. New tests: order-stable
output at workers 1 vs 4, real-concurrency proof (barrier), omission reporting,
placeholder exclusion, _provider_error classification, atomic concurrent cache
writes, single locked client init.
- One real --fresh p2p live run at workers=4 completed end-to-end.
Out of scope: the discovery agent loop (inherently sequential).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
A live run is dominated by sequential LLM calls. The discovery agent loop is inherently serial (each turn feeds the next — can't parallelize without breaking the logic). But the synthesis fan-out was running ~10–13 independent calls (7 report sections + one per opportunity) one after another for no reason. They're I/O-bound API calls, so a thread pool gives real wall-clock parallelism.
Result: synthesis stage ~3× faster. Discovery loop untouched. Zero behavior change to output.
The design was vetted by an adversarial review pass before implementing; every load-bearing safety fix it surfaced is folded in.
What changed
fanout.py—run_synthesis_fanoutsubmits the report sections + per-opportunity calls to aThreadPoolExecutorand consumes futures in submission order (neveras_completed)._merge/ planning stay single-threaded on the main thread, so output is byte-identical regardless of thread scheduling. The one real dependency (pain-points pre-pass → opportunity seeds) is still a hard barrier in the caller.DISCOVERY_MAX_WORKERSenv knob (default 4;=1reverts to fully serial — the regression baseline)._safe_report/_safe_oppcatch failures inside the worker so one bad section omits itself, never aborts the suite — and they catch onlyLLMRetryableError, not a plainLLMError, so a missing/invalid API key still aborts loudly instead of silently producing an empty report.llm.py—LLMRetryableErrorsubclass;_provider_errornow classifies transient (429/529/connection → retryable) vs hard (auth/config → aborts). Locked double-checked lazy client init;max_retries=2on the SDK client to absorb transient blips without wedging pool slots._write_cacheis atomic (temp in a sibling.cache.tmp/on the same filesystem, thenos.replace) so concurrent same-key writes can't tear a file or corrupt a--save-goldensnapshot.Rate-limit-adaptive omission — a section that can't be produced is omitted, warned loudly, and on
--save-goldenis a hard error (a polished snapshot is never silently short). Structural placeholder report specs (04's empty-instruction portfolio spec, whose content comes from the opportunity seeds) are excluded from omission tracking to avoid a false-positive warning.Verified
workers=1vsworkers=4, and vs the committed baseline → zero behavior change, determinism fully preserved. (This was the non-negotiable gate.)_provider_errorclassification, atomic concurrent cache writes, single locked client init.--freshp2p live run atworkers=4completed end-to-end.Residual risks (accept)
max_retries=2) will omit a section rather than abort — now surfaced via the loud warning + the--save-goldenhard-fail, so it's never silent.DISCOVERY_MAX_WORKERS=1avoids bursting the API if a tier is tight.--goldenreplay, not fresh-run bytes.Out of scope
The discovery agent loop (inherently sequential).