Repository navigation
Parse once in /v1/explain/all-levels - #7
Merged
Merged
Conversation
The endpoint's docstring says parsing and analysis are level-independent, which is the reason it exists, but it called explain() once per level and so parsed and analysed the snippet three times. It is the endpoint the web UI calls on every run. Re-running the whole pipeline also gave each level its own node ids. The response's `analysis` came from the ELI5 pass, so BEGINNER and DEVELOPER lines carried node ids that matched nothing in it. Explanation.at_level() re-narrates an existing parse and analysis for another audience, and the endpoint uses it for the other two levels. The response is unchanged apart from the now-consistent node ids, and it is ~30% faster on the web sample and 2x on a 195KB file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UigC7feaY8NkAy9oay3GDW
ethanzhoucool
marked this pull request as ready for review
November 3, 2026 17:17
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.
Summary
POST /v1/explain/all-levelsis the endpoint the web UI calls on every run. Its docstring gives the reason it exists: "The parse and analysis passes are the expensive part and are level-independent." But it calledexplain()once per level, so every request parsed and analysed the snippet three times.That also caused a correctness bug. Each pass created a fresh IR with new node ids. The response's shared
analysisblock came from the ELI5 pass, so the BEGINNER and DEVELOPERlines/annotations/conceptscarriednodeIds that matched nothing inanalysis. Fordef add(a, b): ...onmain:Changes
core.py: newExplanation.at_level(level). It re-runs only the narration (annotate,summarize,line_view) over the existing parse and analysis, for another audience. LLM enrichment is level-specific and is not carried over (all-levels never enriched anyway).api/main.py:explain_all_levelscallsexplain()once and usesat_level()for the other two levels. The response shape and key order are unchanged.Testing
pyteston Python 3.13, 3.12 and 3.11: 152 passed, 3 skipped.ruffis clean, andmypyreports the same 26 errors as before.Same output: I dumped the full all-levels JSON on
mainand on this branch for the 5 bundled web samples, a detection case, a syntax-error case and a 60 KB slice of stdlib code. WithnodeId/nodeIdsstripped, the two dumps are byte-for-byte identical (1.5 MB each).Speed (endpoint through
TestClient, Python 3.13, median):mainNew tests. All fail on
main:test_all_levels_parses_oncecounts adapterparsecalls;test_all_levels_node_ids_match_the_shared_analysis;test_at_level_matches_a_fresh_explanation, one per level: summary and line text/kind match a freshexplain()at that level.Generated by Claude Code