Skip to content

fix(viz): explain the state with the facts basal-serve adds ("facts": "auto") - #73

Merged
rkinas merged 2 commits into
rkinas:mainfrom
pawelkiszczak:pr/viz-facts
Nov 2, 2026
Merged

rkinas merged 2 commits into
rkinas:mainfrom
pawelkiszczak:pr/viz-facts

Conversation

@pawelkiszczak

@pawelkiszczak pawelkiszczak commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Closes #72

What

  • Explainer.explain (basal/viz.py) builds the items from the state basal-serve reads: with "facts": "auto" the state goes through basal.facts.inject first (a JSON state is dumped with ensure_ascii=False), the same two lines as Server.decide.
  • tests/test_viz_facts.py: the state handed to to_items for a text and a JSON state with "facts": "auto", and unchanged without facts or 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_served reported differences for every "facts": "auto" request whose state has dates or amounts.

Compatibility

Requests without facts or 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 existing F821 Undefined name kpos in untouched basal/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:

  • Align basal-viz explanations with basal-serve by injecting computed facts when requests use facts: "auto", including JSON states.

Enhancements:

  • Validate the facts option consistently and preserve existing behavior for requests without facts or with facts: "off".

Documentation:

  • Document the updated facts: "auto" behavior and its alignment with basal-serve comparisons.

Tests:

  • Add coverage for fact injection across text and JSON states, unchanged off/default behavior, and invalid option handling.

@sourcery-ai

sourcery-ai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Updates Explainer.explain so facts: auto injects the same computed facts into text or JSON state that basal-serve reads, while preserving existing behavior for other requests; adds regression tests and release-note documentation.

Sequence diagram for basal-viz facts auto explanation

sequenceDiagram
    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
Loading

File-Level Changes

Change Details Files
Align visualization explanations with basal-serve’s facts-enriched state for automatic facts requests.
  • Import the facts injector and apply it before building explanation items when facts is auto.
  • Serialize object states as UTF-8 JSON with ensure_ascii=False, matching server behavior.
  • Preserve the original state path when facts are absent or off.
  • Add focused coverage for text and JSON states, including unchanged non-auto behavior.
basal/viz.py
tests/test_viz_facts.py
Document the facts synchronization fix for visualization and served-answer comparisons.
  • Add an Unreleased note describing the computed facts block and comparison behavior.
docs/WHATS_NEW.md

Assessment against linked issues

Issue Objective Addressed Explanation
#72 When facts is "auto", have basal-viz explain the state after applying the computed facts block, matching the state basal-serve uses. ✅
#72 When facts is absent or "off", leave the state passed to to_items unchanged. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread basal/viz.py Resolved
Comment thread basal/viz.py
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))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

@rkinas

rkinas commented Oct 29, 2026

Copy link
Copy Markdown
Owner

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.

@rkinas
rkinas merged commit a3121cd into rkinas:main Nov 2, 2026
1 check passed
rkinas added a commit that referenced this pull request Nov 2, 2026
…loses #74)

Conflicts: basal/viz.py (#73's facts import and check next to #75's Gemma 4 imports and media refusal; both kept),
docs/WHATS_NEW.md (the bullet moved into 2.5 with credit).
rkinas added a commit that referenced this pull request Nov 2, 2026
… 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.
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.

basal-viz explains a different prompt than basal-serve serves for facts=auto

2 participants