8000 Widgets (#317) PR 2: Arithmetic, computed inline, with "calculator" results beneath by daoudclarke · Pull Request #654 · mwmbl/mwmbl · GitHub
[go: up one dir, main page]

Skip to content

Widgets (#317) PR 2: Arithmetic, computed inline, with "calculator" results beneath - #654

Merged
daoudclarke merged 2 commits into
claude/issue-317-pr1from
claude/issue-317-pr2
Nov 15, 2026
Merged

daoudclarke merged 2 commits into
claude/issue-317-pr1from
claude/issue-317-pr2

Conversation

@daoudclarke
Copy link
Copy Markdown
Contributor

What changed

  • mwmbl/widgets/arithmetic.py: calculate(query) returns the formatted result of an arithmetic query, or None.
    • A regex (digits, spaces, .+-*/^%(), max 100 chars) rejects nearly every query before any parsing.
    • The rest go through ast.parse and an allowlisted node walk (numbers, + - * / % **, unary +/-, brackets). ^ means power. Never eval.
    • Exact arithmetic with Fraction (0.1+0.2 = 0.3). Result shown to 12 significant digits, scientific notation for very large or small values.
    • DoS bounds applied before computing: |exponent| <= 1000, every value (and every power, estimated from the base's bit width) limited to 4096 bits, final result limited to 10^100. So 9**9**9, 10**1000 * 10**1000 and long ** chains are rejected cheaply. Division/modulo by zero, roots of negatives and bare numbers give no widget.
  • mwmbl/tinysearchengine/search.py: SearchResponse gains widgets: list[CalculationWidget] (type, expression, result), shaped so PR 3 can widen it to a union with stubs. For an arithmetic query the results are those for "calculator", from ranker.search("calculator") cached in memory for an hour. Other queries pay only the regex.
  • test/test_widgets_arithmetic.py: precedence, rounding, rejected inputs and timing of rejections, parse not reached for ordinary queries, endpoint behaviour and the hourly cache.

Why

Part of #317. Arithmetic is the one widget computed in place, with no outbound call.

Verification

make check passes; tests for arithmetic, search API key, image search and widget tokens pass (192 passed).

Stacked on PR 1 (claude/issue-317-pr1).

Known limitation: date-like queries such as 2026-12-25 also match the arithmetic pattern and get a (meaningless) calculation widget; PR 3 can handle dates before arithmetic if wanted.


Generated by Claude Code

@daoudclarke
daoudclarke added this pull request to stack #659 November 13, 2026 20:47
Queries such as "164*8" are parsed to an AST, walked with an allowlist and
computed exactly with Fraction. Exponents, intermediate sizes and results are
bounded before computing, so 9**9**9 and 10**1000 * 10**1000 are rejected
cheaply. The v2 search response gains a `widgets` list holding a `calculation`
widget, and its results are those for "calculator", ranked once an hour.

No functional changes to the crawler.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DTbu4ortjqC5Z7Kw9377LE
The search.py wiring for the calculator widget was missing from the previous commit.

No functional changes to the crawler.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DTbu4ortjqC5Z7Kw9377LE
@codecov
codecov Bot commented Nov 13, 2026
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.33028% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
mwmbl/widgets/arithmetic.py 94.87% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

@daoudclarke
daoudclarke merged commit 71dcf5a into main Nov 15, 2026
7 checks passed
@daoudclarke
daoudclarke deleted the claude/issue-317-pr2 branch November 15, 2026 15:22
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.

2 participants

0