openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

History diff radio buttons not pre-selected leading to silent fallback on partial selection

Open Saad259 opened this issue 7 months ago • 7 comments

Problem

On the revision history page (e.g., https://openlibrary.org/help?m=history&page=2), the diff comparison feature behaves incorrectly when not on page 1. Two related issues occur:

  1. Radio buttons are not pre-selected on subsequent pages (unlike page 1, which pre-selects revisions)
  2. Submitting the Compare form with only one radio button selected silently falls back to an undiscoverable default, giving the user no feedback about what is being compared

This affects any user attempting to diff revisions on page 2 or beyond of a history view.

Reproducing the bug

Pre-selection issue:

  1. Navigate to https://openlibrary.org/help?m=history
  2. Observe that the two latest revisions (161 and 160) have their radio buttons pre-selected
  3. Navigate to page 2 (/help?m=history&page=2)
  4. Observe that no radio buttons are pre-selected

Silent fallback issue:

  1. On page 2, select only the left radio button ("Compare this version...") for a revision, e.g. revision 120
  2. Click 'Compare'. The URL becomes /help?_compare=Compare&a=120&m=diff
  3. Observe that a diff is shown against the latest revision (161), with no explanation
  4. Repeat selecting only the right radio button ("...to this version") for revision 120
  5. Click 'Compare'. The URL becomes /help?_compare=Compare&b=120&m=diff
  6. Observe that a diff is shown against revision 119 (the one before it), with no explanation

Expected behaviour

  • On page 2 (and any subsequent page), the first two revisions shown should be pre-selected by default, consistent with page 1 behaviour
  • If a user submits the Compare form with only one radio button selected, they should receive a clear message prompting them to select a second revision, rather than silently comparing against an arbitrary default

Actual behaviour

  • Page 2 renders with no pre-selected radio buttons
  • Submitting with only the left radio selected compares against the latest revision silently
  • Submitting with only the right radio selected compares against the immediately preceding revision silently

Context

  • Browser (Chrome, Safari, Firefox, etc):
  • OS (Windows, Mac, etc):
  • Logged in (Y/N):
  • Environment (prod, dev, local): prod

Breakdown

Implementation Details (for maintainers)

Requirements Checklist

  • [ ]

Related files

Stakeholders


Instructions for Contributors

  • Please run these commands to ensure your repository is up to date before creating a new branch to work on this issue and each time after pushing code to Github, because the pre-commit bot may add commits to your PRs upstream.

Saad259 avatar Apr 17 '26 14:04 Saad259

I've looked into this. In openlibrary/templates/history.html, the pre-selection logic in the history template hardcodes checks against rev, which is always the latest revision of the document. Further, in vendor/infogami/infogami/core/code.py, the diff handler defaults missing a or b parameters silently. This is documented in a code comment but invisible to the user. I'm happy to take this on and open to hearing about potential directions for this.

Saad259 avatar Apr 17 '26 14:04 Saad259

Thank you @Saad259 for submitting this issue!

⚠️ Contributors, this issue will be ready to work on once:

  • [ ] Triage — maintainers will add Priority: * and Lead: @* to replace Needs: Triage and Needs: Lead
  • [ ] Needs: Breakdown — issue lacks concrete implementation steps; not yet actionable

Context

Codebase research confirmed both bugs:

  • Pre-selection logic is in openlibrary/templates/history.html:15,53–63. The variable rev = page.latest_revision or page.revision holds the document's absolute latest revision, not the first revision displayed on the current page. On page 1, rev and rev - 1 are both visible in the rendered list, so the checks match. On page 2+, those revision numbers are not in h (the paginated history slice), so no radio button passes either check and none are pre-selected.

  • Silent fallback originates in vendor/infogami/infogami/core/code.py:224–230. When b is absent from the query string, it silently resolves to the latest revision (line 224: b = get(path, safeint(i.b, None))). When a is absent, it silently resolves to b.revision - 1 (line 230: a = get(path, max(1, safeint(i.a, b.revision - 1)))). There is no validation that rejects or flags a one-sided submission.

  • The compare feature was added in commit 95aaffd63 (Oct 2019) and the pre-selection logic has had no changes since.


Issue Assessment

Action items

  • [ ] Fill in the Requirements Checklist and Related Files in the Breakdown section
  • [ ] Add browser/OS/login context (likely not required to reproduce, but useful for completeness)

Suggested breakdown

  • [ ] Fix pre-selection in openlibrary/templates/history.html:15,53–63: replace v.revision == rev / v.revision == rev - 1 checks with checks against the first two items in h (the paginated history list), so pre-selection is relative to the current page rather than the document's global latest. Self-contained template-only change; candidate for Good First Issue once triage is complete.
  • [ ] Fix silent fallback: add client-side form validation in history.html to prevent submission when fewer than two radio buttons (one a, one b) are selected, with a user-visible prompt. This avoids modifying the vendored vendor/infogami/infogami/core/code.py. Alternatively, extend the diff handler in that file (lines 203–231) to detect partial selection server-side and return an error response.

Full triage checklist (maintainers / Pam)
  • [x] Triage — correct labels applied or removed: Type: Bug present; Needs: Breakdown, Needs: Triage, Needs: Lead retained; Needs: Investigation not added (root cause confirmed from code); Needs: Design not added (bug fix); Good First Issue deferred until triage complete; no Blocked, Blocker, Needs: Staff Decision, or Needs: Staff / Admin warranted
  • [x] Problem / opportunity — two distinct bugs clearly described with reproduction steps
  • [x] Scope — issue is well-scoped; two closely related bugs in the same feature, reasonable to address together
  • [ ] Justification — no measurable impact stated; no urgency or frequency data
  • [ ] Success & testing criteria — expected behavior is described but no explicit acceptance criteria or test plan
  • [ ] Proposal & breakdown — breakdown section present but empty (requirements checklist and related files not filled in)
  • [x] If bug: environment & repro steps — steps to reproduce are clear; prod environment confirmed; browser/OS/login fields left blank (likely not impactful)
  • [ ] Risks, concerns, open questions — not addressed by author; modifying vendored infogami code may need a decision on scope
  • [x] Pam context — history.html and code.py reviewed; no duplicate issue found; compare feature origin commit identified
  • [ ] References — no screenshots or links to affected pages beyond the reproduction steps

[!NOTE] This comment was automatically generated by Pam, Open Library's Project AI Manager, on behalf of @mekarpeles. Pam is designed to provide status visibility, perform basic project management functions and relevant codebase research, and provide actionable feedback so contributors aren't left waiting.

mekarpeles avatar Apr 17 '26 14:04 mekarpeles

@Saad259 Just dropping a suggestion here!

To handle this cleanly without the complexity of cross-page state management, a good approach might be updating history.html to pre-select the top two revisions of the current page rather than the global latest, combined with adding simple client-side JS to prevent the silent fallback by requiring two checked radio buttons before submission.

Additionally, to easily handle the edge case of cross-page comparisons, we could also add a simple "Advanced Compare" form with native type="number" inputs, giving users a straightforward way to manually input revisions across large gaps. Feel free to use this approach if you'd like! @lokesh @mekarpeles thoughts?

Also, if you end up not being available to work on this anymore, just let me know and I'd be happy to take it on.

Sadashii avatar Apr 20 '26 16:04 Sadashii

@Sadashii Thanks for the input. I've already investigated both root causes and have a clear direction (Replacing the rev/rev-1 checks with checks against the first two items in h, so pre-selection is relative to the current page; and the silent fallback fix would be to add client-side JS validation to require both an a and b selection before form submission with a user-visible message, keeping the fix self-contained and out of vendored code).

The "Advanced Compare" idea is interesting but I'd treat it as a separate enhancement rather than part of this bug fix. I'm still committed to working on this!

Saad259 avatar Apr 22 '26 04:04 Saad259

Not sure this bug is worth addressing as the core comparison selection UI has bigger issues.

The three use cases I imagine for comparison selection are poorly served by the current UI:

  1. "What changed in this edit?" (X vs. X−1) - requires radio gymnastics. Should be a one-click link per row.
  2. "What's changed since X?" (X vs. current) - impossible when X isn't on page 1.
  3. "X vs. Y arbitrary" - the only case the radios target, and it's the rarest.

AI recommendation to provide some UI direction for discussion:

The Wikipedia pattern is the natural fit and the lowest-risk path — OL users coming from Wikipedia will recognize it, and it addresses the three use cases without removing power-user functionality:

  • Add per-row (changes | vs current) links (direct equivalent of Wikipedia's cur |prev).
  • Persist the a selection in URL state so radios work across pages.
  • Fix the silent fallback — if only one is selected and user hits Compare, either error out or treat it as "vs current" and label it that way on the diff page.

If someone is interested in taking this bigger, but still relatively small, task, feel free to create an issue.

lokesh avatar Apr 24 '26 02:04 lokesh

Thanks for the feedback @lokesh. I had a look and the Wikipedia pattern makes sense here. I've also looked into the codebase and have a good understanding of the three pieces. I'd like to take this bigger but want to share my thinking before starting.

Per row "prev" and "cur" links All the data needed is already available in the template (v.revision, rev, page.key), so these can be constructed as direct links:

  • prev -> page.key?a=<v.revision-1>&b=<v.revision>&m=diff
  • curr -> page.key?a=<v.revision>&b=<rev>&m=diff

For edge cases, I think following Wikipedia's convention of disabling "curr" on the latest revision and "prev" on revision 1 makes sense (since a=0 would diff against an empty object)

Silent fallback fix Handling this client-side in index.html where it validates both an a and b radio are selected before allowing form submission and show a user-visible message if not. This would keep the fix self-contained.

Persisting a selection in URL state Now this is the one I want to make sure is actually the right approach. My understanding is when a user selects a radio button (a), updating the URL with ?a=<revision> so the selection survives pagination could be a way to do it. On page load, if a is present in the URL, that radio gets pre-selected. Could you clarify what you had in mind for this? (specifically, whether b should also persist and whether the intent is even to support cross-page comparison in the first place, or something different?)

Happy to open a new issue for this as suggested and work through it there. @RayBB

Saad259 avatar Apr 25 '26 12:04 Saad259

@lokesh do you know if this is resolved in the latest history page redesign? It seems likely it is. If not lets reopen https://github.com/internetarchive/openlibrary/issues/12628

RayBB avatar Jun 23 '26 00:06 RayBB