Repository navigation
fix(viz): explain the state with the facts basal-serve adds ("facts": "auto") - #73
Conversation
Reviewer's GuideUpdates Sequence diagram for basal-viz facts auto explanationsequenceDiagram
participant Client
participant Explainer
participant inject_facts
participant to_items
participant Server
Client->>Explainer: explain(body)
alt body.facts == auto
Explainer->>inject_facts: inject_facts(state)
inject_facts-->>Explainer: state with computed facts
end
Explainer->>to_items: to_items(state, questions, max_options, force_lang)
to_items-->>Explainer: question items
Explainer->>Explainer: explain_branch(q, bi)
Explainer-->>Client: model, questions, total_ms
Note over Server,to_items: Server.decide reads the same facts-enriched state
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="basal/viz.py" line_range="171" />
<code_context>
t0 = time.perf_counter()
- items = to_items(body["state"], body["questions"], self.srv.max_options, force_lang=body.get("lang"))
+ state = body["state"]
+ if body.get("facts") == "auto":
+ state = inject_facts(state if isinstance(state, str) else json.dumps(state, ensure_ascii=False))
+ items = to_items(state, body["questions"], self.srv.max_options, force_lang=body.get("lang"))
</code_context>
<issue_to_address>
**Invalid facts requests waste inference**
When `facts` is neither omitted, `"off"`, nor `"auto"`, `Explainer.explain` treats the unsupported value as facts-off and runs the model forwards; `/api/explain` then calls `Server.decide`, which rejects the request, so the caller waits and GPU work is spent before the error is returned.
Validate the `facts` value before running `explain_branch`, using the same allowed values as `Server.decide`.
</issue_to_address>
### Comment 2
<location path="basal/viz.py" line_range="172" />
<code_context>
- items = to_items(body["state"], body["questions"], self.srv.max_options, force_lang=body.get("lang"))
+ state = body["state"]
+ if body.get("facts") == "auto":
+ state = inject_facts(state if isinstance(state, str) else json.dumps(state, ensure_ascii=False))
+ items = to_items(state, body["questions"], self.srv.max_options, force_lang=body.get("lang"))
qs = [self.explain_branch(q, bi) for q in items for bi in range(len(q["branches"]))]
</code_context>
<issue_to_address>
**Page requests omit automatic facts**
When a user relies on the bundled page to compare a request that basal-serve handles with `"facts": "auto"`, the bundled page’s `readRequest()` in `basal/viz.html` returns only `state` and `questions`, so its `/api/explain` request never reaches this branch with `facts: "auto"`; both explanation and comparison run without the facts block, and the page still shows the mismatch this change is meant to resolve.
Add a facts option to the page and include its value in `readRequest()` so `/api/explain` and the served comparison receive the same request flag.
</issue_to_address>| items = to_items(body["state"], body["questions"], self.srv.max_options, force_lang=body.get("lang")) | ||
| state = body["state"] | ||
| if body.get("facts") == "auto": | ||
| state = inject_facts(state if isinstance(state, str) else json.dumps(state, ensure_ascii=False)) |
There was a problem hiding this comment.
🟡 Medium · Page requests omit automatic facts
When a user relies on the bundled page to compare a request that basal-serve handles with "facts": "auto", the bundled page’s readRequest() in basal/viz.html returns only state and questions, so its /api/explain request never reaches this branch with facts: "auto"; both explanation and comparison run without the facts block, and the page still shows the mismatch this change is meant to resolve.
Add a facts option to the page and include its value in readRequest() so /api/explain and the served comparison receive the same request flag.
Prompt for AI agents
In `basal/viz.py` at line 172:
**Page requests omit automatic facts**
When a user relies on the bundled page to compare a request that basal-serve handles with `"facts": "auto"`, the bundled page’s `readRequest()` in `basal/viz.html` returns only `state` and `questions`, so its `/api/explain` request never reaches this branch with `facts: "auto"`; both explanation and comparison run without the facts block, and the page still shows the mismatch this change is meant to resolve.
Add a facts option to the page and include its value in `readRequest()` so `/api/explain` and the served comparison receive the same request flag.|
Thank you, Paweł! Planned for basal 2.5: we are merging it into the 2.5 release branch now, after our checks (full CPU suite, your tests, serving code unchanged). The PR will close when 2.5 is published on main. |
… version CONTRIBUTING.md credits the 2.5 pull requests as the release notes' community line lists them (Paweł Kiszczak #52, #62, #64, #66, #68, #71, #73, #75; Marcin Brzeżański #56; Grzegorz Brzezinka #76, #78) and adds Dariusz Koryto (#53, #58, #59, #60 and #55); the README's list names him too. docs/changes/README.md and the changelog.py help use 2.5.1, the next release, as the example.
Closes #72
What
Explainer.explain(basal/viz.py) builds the items from the state basal-serve reads: with"facts": "auto"the state goes throughbasal.facts.injectfirst (a JSON state is dumped withensure_ascii=False), the same two lines asServer.decide.tests/test_viz_facts.py: the state handed toto_itemsfor a text and a JSON state with"facts": "auto", and unchanged withoutfactsor with"off".docs/WHATS_NEW.md: a bullet under# Unreleased.Why
basal-viz explained the prompt without the facts block while basal-serve answered with it, so
compare_servedreported differences for every"facts": "auto"request whose state has dates or amounts.Compatibility
Requests without
factsor with"facts": "off"are explained exactly as before. No API change.Tests run
uv run --no-sync pytest -q tests/test_viz_facts.py— 4 passed (the two"auto"cases fail without the fix).uv run --no-sync pytest -q -x— 765 passed, 53 skipped, 1 warning.uvx [email protected] check --select E9,F63,F7,F82 --target-version py310 .— only the existingF821 Undefined name kposin untouchedbasal/gemma4.py:471.Summary by Sourcery
Make basal-viz explain the same fact-enriched state that basal-serve uses for
facts: "auto"requests.Bug Fixes:
facts: "auto", including JSON states.Enhancements:
factsoption consistently and preserve existing behavior for requests without facts or withfacts: "off".Documentation:
facts: "auto"behavior and its alignment with basal-serve comparisons.Tests: