Spaces:
Sleeping

Yuchen Yuan commited on
Commit
4ee621f
·
1 Parent(s): 6962cb3

Polish pass: dedupe pipeline runners, shared chip component, emitter map

Browse files

Three concrete refactors that reduce surface area for future bugs and
make the codebase easier to maintain by hand or by LLM.

1. backend/app/services/workflow_service.py — run_pipeline and
run_pipeline_stream used to be two 100+ line copies of the same
dispatch loop. Extract a single _execute_pipeline generator and
make both functions thin wrappers (one collects into a list, the
other yields). Per-node dispatch (input / built-in / plugin /
skipped) and the post-step circuit_shape stamping are factored
into _dispatch_one_node. Plugin hydration becomes a named helper
instead of an inline block duplicated on both paths.

Also pulled out _clamp_shots and _set_backend_ctx so the two
backend handlers share their shots-clamping and ctx-writing
patterns; _cache_put's prefix_types argument becomes required
(all call sites pass it; the dead 'None means skip sidecar'
branch is gone); the run_cache.qpy_dump_bytes import moves to
the top of the file with the other module-level imports.

2. frontend/src/components/RunStatusChips.tsx — QNode and
ResultsPane both rendered cached/live chips with the same
conditional logic and similar markup; extract one component
with a 'tile' | 'card' variant. Adding a future chip (queued,
stale, dry-run, ...) is now one file.

3. frontend/src/lib/exportPython.ts — the per-node-kind switch
block was ~200 lines of repeated 'lines.push(...)' calls.
Refactor to one Emitter per kind in an EMITTERS lookup table,
so the main loop is six lines and adding a new node-kind block
is one map entry. Local asStr / asNum helpers replace the
String/Number coercion pattern that was duplicated per case.
Verified the generated script byte-for-byte against a sample
input (ry_chain + FakeFez + QuBound threshold + sampled
fidelity) and confirmed it still compiles with python3.

Net diff: -82 LOC in workflow_service.py (1221 → 1139, then minor
helpers brought it to 1143); -139 lines after counting QNode +
ResultsPane chip extraction; exportPython went from ~320 LOC of
repetitive switch to ~340 LOC of named emitters (slight LOC
increase, big drop in cyclomatic complexity).

11 unit tests still green; frontend tsc clean; generated Python
script compiles.

5 files changed +526 -550
Files changed (5)
backend/app/services/workflow_service.py+212-290
frontend/src/components/QNode.tsx+2-16
frontend/src/components/ResultsPane.tsx+2-13
frontend/src/components/RunStatusChips.tsxAdded+58
frontend/src/lib/exportPython.ts+252-231