<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hu Qiyu</title>
    <description>The latest articles on DEV Community by Hu Qiyu (@hu_qiyu_73fc5da4c0fe227c1).</description>
    <link>https://dev.to/hu_qiyu_73fc5da4c0fe227c1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4281965%2F55e5caca-de39-4a89-a533-473ec7297eb7.png</url>
      <title>DEV Community: Hu Qiyu</title>
      <link>https://dev.to/hu_qiyu_73fc5da4c0fe227c1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hu_qiyu_73fc5da4c0fe227c1"/>
    <language>en</language>
    <item>
      <title>Running Stockfish and Maia in WebAssembly for Browser Chess Coaching</title>
      <dc:creator>Hu Qiyu</dc:creator>
      <pubDate>Fri, 04 Dec 2026 07:43:14 +0000</pubDate>
      <link>https://dev.to/hu_qiyu_73fc5da4c0fe227c1/running-stockfish-and-maia-in-webassembly-for-browser-chess-coaching-3e50</link>
      <guid>https://dev.to/hu_qiyu_73fc5da4c0fe227c1/running-stockfish-and-maia-in-webassembly-for-browser-chess-coaching-3e50</guid>
      <description>&lt;p&gt;A chess engine can tell you that a tempting capture is a mistake. It can also recommend a defense so hard to find that it barely comes up in your games. If you are building a coach, both answers matter. The engine shows what survives good play. A human-move model helps you choose which mistakes and replies the student should practice first.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.propchess.com/coach" rel="noopener noreferrer"&gt;PropChess Position Coach&lt;/a&gt; runs Stockfish and Maia in separate browser workers. A single position can produce three distinct pieces of evidence: legal moves from chess.js, searched variations from Stockfish, and probabilities for human moves from Maia. The browser can compute all three without an inference request to a server. Here is how to keep those results in sync when a player edits the board halfway through a search.&lt;/p&gt;

&lt;h2&gt;
  
  
  Put Stockfish behind a UCI session
&lt;/h2&gt;

&lt;p&gt;PropChess ships the single-threaded Lite build of Stockfish 17.1. A setup script copies its JavaScript loader and WebAssembly binary into Next.js's public assets as &lt;code&gt;/stockfish/stockfish.js&lt;/code&gt; and &lt;code&gt;/stockfish/stockfish.wasm&lt;/code&gt;. The default worker starts directly from the same-origin script URL. The Emscripten loader resolves the binary relative to its script, so an apparently harmless change to a blob-backed worker can break that lookup. When testing a deploy, check the binary request as well as the JavaScript response. A missing binary can turn into a worker initialization timeout.&lt;/p&gt;

&lt;p&gt;The transport reads and writes UCI text. Keep handshake, search ownership, and parsing in the session above it. Send &lt;code&gt;uci&lt;/code&gt; and wait for &lt;code&gt;uciok&lt;/code&gt;, then use &lt;code&gt;isready&lt;/code&gt; and wait for &lt;code&gt;readyok&lt;/code&gt; before searching. Send &lt;code&gt;position fen ...&lt;/code&gt;, followed by a bounded command such as &lt;code&gt;go depth 12&lt;/code&gt;. Treat &lt;code&gt;info&lt;/code&gt; lines as incremental results and &lt;code&gt;bestmove&lt;/code&gt; as the search terminator. Some worker builds put several lines into one message. Split at newlines before handling individual lines or a trailing &lt;code&gt;bestmove&lt;/code&gt; can disappear into an &lt;code&gt;info&lt;/code&gt; event.&lt;/p&gt;

&lt;p&gt;When the user replaces a position, invalidate its request before drawing the new board. For a worker you intend to reuse, stop the search and drain its terminal response before dispatching another job. UCI output carries no application request ID. On a timeout or a position reset where the session cannot safely recover, dispose of it and create a fresh worker. Attach the current board generation to callbacks. FEN equality alone misses a user who goes from position A to B and back to A.&lt;/p&gt;

&lt;h2&gt;
  
  
  Give Maia its own lifecycle
&lt;/h2&gt;

&lt;p&gt;Maia-3 answers a different question. Its move policy estimates which legal moves players at the supplied ratings are likely to choose. In PropChess the worker uses the &lt;code&gt;maia3-5m&lt;/code&gt; ONNX model through ONNX Runtime Web's WASM provider. The model receives square features and two rating inputs, for the mover and the opponent. The Coach offers five presets on a Lichess rating scale, from 800 through 2100, and initializes both players at the selected rating for a guided session. A likely mistake is a teaching opportunity even when Stockfish scores it poorly.&lt;/p&gt;

&lt;p&gt;Separate model download progress from readiness. The worker reads a same-origin manifest with the model filename, byte length, and SHA-256. It uses a hash-versioned cache key and checks the bytes on both the network and Cache Storage paths. The ONNX runtime binary lives in a directory tied to the runtime version. Set the WASM runtime to one thread before creating the session. That setup avoids a SharedArrayBuffer dependency for Maia. On a cold visit, downloading, hashing, and compiling still take time. On a warm visit, measure session initialization and inference separately so a fast cache hit does not hide slow execution on an older phone.&lt;/p&gt;

&lt;p&gt;PropChess encodes the position from the mover's perspective and builds the policy's legal move list with chess.js. Decode probabilities over that list before taking the top candidates. Taking a global top five first can fill the list with illegal moves and hide candidates the player could actually choose. Include both ratings and the model version in any prediction cache key. A probability at one level is a different answer from a probability for the same board at another level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Replay every published variation
&lt;/h2&gt;

&lt;p&gt;Use one authoritative position snapshot when starting search and applying results. This small TypeScript example converts a UCI principal variation into SAN and returns the resulting FEN. The caller should catch a bad line and drop the entire variation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Chess&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"chess.js"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;replayPrincipalVariation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;uciLine&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Chess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;san&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uciLine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uci&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!/^&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;qrbn&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;$&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uci&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`Invalid UCI token: ${uci}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;uci&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;uci&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;promotion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;uci&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;san&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;san&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;finalFen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;replayPrincipalVariation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Chess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"e2e4"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"e7e5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"g1f3"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sample yields &lt;code&gt;e4&lt;/code&gt;, &lt;code&gt;e5&lt;/code&gt;, and &lt;code&gt;Nf3&lt;/code&gt;. Try the same wrapper with a promotion, a legal castle, and a move that exposes the mover's king. Then switch positions mid-search in the UI. A valid line from yesterday's board is just as wrong on today's board as an illegal line. Show a result only when both the legal replay and the generation check succeed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Turn the two readings into a lesson
&lt;/h2&gt;

&lt;p&gt;At a decision point, show a playable branch for a natural candidate and ask the learner to predict the strongest reply. Keep the engine's evaluation, the model's likelihood, and the actual move history in distinct fields. Let learners replay the branch and compare it with the one they chose. If Maia fails to load, engine-only practice can still proceed with a visible unavailable state for the human model.&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://www.propchess.com/" rel="noopener noreferrer"&gt;PropChess study suite&lt;/a&gt;, the Coach can call server routes for some wording and variation-tree reports. Local search and human-move inference remove server GPU work for those two computations and eliminate their network round trips once the assets are ready. Measure cold start, first playable feedback, and final report separately. They answer three different questions about how long a learner waits at the board.&lt;/p&gt;

&lt;p&gt;Note: Portions of the code examples, formatting, and editorial drafting were assisted by AI tools and reviewed/verified by the author.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>frontend</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Building a macOS selection toolbar with an explicit network boundary</title>
      <dc:creator>Hu Qiyu</dc:creator>
      <pubDate>Fri, 04 Dec 2026 07:42:08 +0000</pubDate>
      <link>https://dev.to/hu_qiyu_73fc5da4c0fe227c1/building-a-macos-selection-toolbar-with-an-explicit-network-boundary-382g</link>
      <guid>https://dev.to/hu_qiyu_73fc5da4c0fe227c1/building-a-macos-selection-toolbar-with-an-explicit-network-boundary-382g</guid>
      <description>&lt;p&gt;The awkward case for a Mac selection toolbar is a footnote in a scan. In a browser, the user can drag across a sentence and an app may expose the string through Accessibility. In a scanned PDF, the same drag may yield a rectangle of pixels. You want the toolbar to feel familiar in both cases, while keeping that rectangle on the Mac and making the translation request a deliberate choice.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://lassoly.app/" rel="noopener noreferrer"&gt;Lassoly’s selection toolbar and browser playground&lt;/a&gt; illustrate that interaction. Its Mac client builds on GPL-3.0 Easydict for selection capture and on-device OCR. The design below is a reference architecture for anyone building a similar tool. The Swift example shows the OCR step. &lt;a href="https://github.com/tisfeng/Easydict" rel="noopener noreferrer"&gt;Easydict source and license&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let selection create a candidate
&lt;/h2&gt;

&lt;p&gt;Check Accessibility permission during setup and explain the feature that needs it. Use the focused Accessibility element to ask for the selected string. When the application exposes a selection range and useful bounds, you can position the toolbar around it. If the bounds are missing, a pointer-relative fallback is often better than a popup anchored to the wrong display. Treat an unsupported selection as a regular outcome, including PDF viewers with custom text layers and fields that deliberately hide their contents.&lt;/p&gt;

&lt;p&gt;Keep the candidate and its screen position in memory. A new selection replaces it. A focus change dismisses it. The toolbar may briefly debounce selection events to avoid flicker while someone drags. Give the selection observer access to UI state so it can decide what to show, and reserve the provider client for the action dispatcher.&lt;/p&gt;

&lt;p&gt;That separation fixes a common race. A user highlights one sentence, changes windows, and presses a shortcut while the previous toolbar is still disappearing. A toolbar button should carry the generation of the candidate that drew it. For a global shortcut, resolve the intent against the current focus. Either way, reject a stale candidate before assembling a network request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Give pixels a separate path
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://lassoly.app/screenshot-translate" rel="noopener noreferrer"&gt;Lassoly’s Mac screenshot OCR workflow&lt;/a&gt; uses Option–S (&lt;code&gt;⌥S&lt;/code&gt;). Pause a video, open a scanned PDF, or find the label in an image and drag a rectangle around it. Screen Recording permission is required for capture. Tools that only act on selectable text can leave that permission off.&lt;/p&gt;

&lt;p&gt;Once a capture succeeds, feed the cropped bitmap to Apple Vision on the device. This helper illustrates recognition of a single crop. Run it away from the UI thread and preserve the recognized lines so the caller can decide how to display them. &lt;a href="https://developer.apple.com/documentation/vision/recognizing-text-in-images" rel="noopener noreferrer"&gt;Apple Vision text recognition documentation&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;CoreGraphics&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Vision&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;recognizeLines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;crop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CGImage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;VNRecognizeTextRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recognitionLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accurate&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="nc"&gt;VNImageRequestHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cgImage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;crop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compactMap&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;topCandidates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Put image buffers on the capture side of the design and release them when recognition finishes. Define how the overlay’s coordinates map to image pixels before doing any OCR work. Test on a Retina display and an external monitor with a different scale factor. A crop that lands a few lines too high can include precisely the text the user meant to exclude.&lt;/p&gt;

&lt;p&gt;A Vision result is a best guess. Narrow crops help with multi-column PDFs, and pausing a moving subtitle can improve legibility. For a code sample or part number, an OCR substitution such as &lt;code&gt;O&lt;/code&gt; for &lt;code&gt;0&lt;/code&gt; can change the answer downstream. Start your test set with receipts, mixed scripts, and a two-column scan. Use examples where reading a line from the wrong column would change the meaning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build the request after a user action
&lt;/h2&gt;

&lt;p&gt;Route each action button, translation shortcut, or submitted follow-up through a single dispatcher. The dispatcher receives only the fields needed by the chosen action. A minimal first-turn payload might carry an action identifier and the selected or recognized text. A follow-up can also carry the conversation context needed by the provider. A dedicated request type makes it harder to accidentally serialize app names, window titles, browser URLs, clipboard contents, or image buffers that happen to be available elsewhere in the app.&lt;/p&gt;

&lt;p&gt;Lassoly’s documented selection flow sends content after an explicit action. In its screenshot translation flow, completing the capture triggers Vision recognition locally. The recognized text can then go to Youdao, an AI provider, or both, according to settings. Make that behavior clear near the shortcut or capture overlay. In a tool that offers a separate OCR-only action, it helps to name the actions distinctly so a user can predict which one sends text.&lt;/p&gt;

&lt;p&gt;Allow the dispatcher to choose a provider after the boundary check. Lassoly includes account-based AI access and offers optional DeepSeek bring-your-own-key (BYOK) for selection actions. DeepSeek bills those requests directly and they use zero Lassoly credits. The text still goes to a remote provider. A key changes who bills the request and which service processes it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test the boundary from the outside
&lt;/h2&gt;

&lt;p&gt;Replace your transport with a recorder. Drag through several sentences, move the pointer, switch windows, and modify the clipboard. Assert that the selection flow produces zero provider requests. Click a toolbar action and assert that exactly one request contains the expected string. Give the window title, URL and clipboard distinct test markers, then check the serialized request and diagnostic events for those markers. Keep unrelated app activity such as sign-in outside the recorder’s selection-request assertions.&lt;/p&gt;

&lt;p&gt;Then simulate a screenshot drag that the user cancels and a completed crop that yields no text. Neither case needs a translation call. For a successful capture, inspect the exact payload and confirm that the image stays in the local OCR path. Repeat with delayed OCR completing after the user dismisses the overlay. Cancel or invalidate obsolete work so a late result cannot translate on its own.&lt;/p&gt;

&lt;p&gt;Finally, drive the UI against a real browser, PDF viewer, editor, and a field that intentionally exposes no selection. The useful acceptance test is temporal. You should be able to point to the user event that authorized every outgoing text request and explain which text it carried.&lt;/p&gt;

&lt;p&gt;Note: Portions of the code examples, formatting, and editorial drafting were assisted by AI tools and reviewed/verified by the author.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>design</category>
      <category>opensource</category>
      <category>swift</category>
    </item>
    <item>
      <title>Why Python's round() Uses Banker's Rounding (And When It Breaks Financial Totals)</title>
      <dc:creator>Hu Qiyu</dc:creator>
      <pubDate>Fri, 04 Dec 2026 07:31:42 +0000</pubDate>
      <link>https://dev.to/hu_qiyu_73fc5da4c0fe227c1/why-pythons-round-uses-bankers-rounding-and-when-it-breaks-financial-totals-d2p</link>
      <guid>https://dev.to/hu_qiyu_73fc5da4c0fe227c1/why-pythons-round-uses-bankers-rounding-and-when-it-breaks-financial-totals-d2p</guid>
      <description>&lt;h1&gt;
  
  
  Why Python's round() Uses Banker's Rounding (And When It Breaks Financial Totals)
&lt;/h1&gt;

&lt;p&gt;If you ask ten engineers what &lt;code&gt;round(2.5)&lt;/code&gt; returns in Python 3, most will say &lt;code&gt;3&lt;/code&gt;. Run it in a REPL and you get &lt;code&gt;2&lt;/code&gt;. Ask for &lt;code&gt;round(3.5)&lt;/code&gt; and you get &lt;code&gt;4&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That is not a bug. Python 3 follows IEEE 754 &lt;strong&gt;round half to even&lt;/strong&gt; (often called banker's rounding): whenever a value sits exactly halfway between two representable numbers, it rounds to the nearest &lt;em&gt;even&lt;/em&gt; digit rather than always rounding up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why IEEE 754 Chose Half-to-Even Over Half-Up
&lt;/h2&gt;

&lt;p&gt;Standard schoolbook rounding (round half away from zero) introduces a systematic upward bias on positive datasets. If you sum 10,000 half-cent values rounded half-up, every single tie moves in the same direction, inflating the aggregate by &lt;code&gt;$50.00&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Half-to-even cancels that bias across symmetric data because half of all midpoint ties end in an even digit and half end in an odd digit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;2.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;3.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;4.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;5.5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;exact_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                    &lt;span class="c1"&gt;# 18.0&lt;/span&gt;
&lt;span class="n"&gt;bankers_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# 0 + 2 + 2 + 4 + 4 + 6 = 18&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For large statistical aggregations, that zero-bias property is a clear win. Problems start when you mix banker's rounding with two other realities: &lt;strong&gt;binary floating-point representation&lt;/strong&gt; and &lt;strong&gt;legal or domain rounding rules&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Binary Float Trap Nobody Expects
&lt;/h2&gt;

&lt;p&gt;Even if you &lt;em&gt;want&lt;/em&gt; banker's rounding, binary floats rarely represent decimal midpoints accurately:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;2.675&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 2.67, NOT 2.68!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why? Because &lt;code&gt;2.675&lt;/code&gt; cannot be stored exactly in IEEE 754 binary64. Its actual stored value is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2.67499999999999982236431605997495353221893310546875
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because that number is strictly less than &lt;code&gt;2.675&lt;/code&gt;, Python does not even treat it as a tie—it simply rounds down to &lt;code&gt;2.67&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When debugging rounding mismatches between backend services and spreadsheets, comparing exact decimal behavior side by side saves hours. The &lt;a href="https://www.mathisimple.com/calculator/rounding-calculator" rel="noopener noreferrer"&gt;interactive rounding calculator on MathIsimple&lt;/a&gt; lets you switch between half-up, half-down, half-to-even, ceiling, and floor modes on the same input so you can immediately verify whether a discrepancy comes from the rounding rule or binary float drift.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixing Rounding Deterministically With &lt;code&gt;decimal&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;For billing, tax lines, or lab instrumentation outputs, never pass binary &lt;code&gt;float&lt;/code&gt; values into &lt;code&gt;round()&lt;/code&gt;. Parse strings into &lt;code&gt;Decimal&lt;/code&gt; and declare the rounding mode explicitly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ROUND_HALF_UP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ROUND_HALF_EVEN&lt;/span&gt;

&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"2.675"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;invoice_val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;quantize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0.01"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rounding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;ROUND_HALF_UP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# Decimal('2.68')&lt;/span&gt;
&lt;span class="n"&gt;bankers_val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;quantize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0.01"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rounding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;ROUND_HALF_EVEN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Decimal('2.68')&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice both modes give &lt;code&gt;2.68&lt;/code&gt; once the exact decimal value &lt;code&gt;2.675&lt;/code&gt; is preserved—because &lt;code&gt;8&lt;/code&gt; is even. The divergence only shows up when the preceding digit is even (such as &lt;code&gt;2.665&lt;/code&gt; → &lt;code&gt;2.66&lt;/code&gt; under half-to-even vs &lt;code&gt;2.67&lt;/code&gt; under half-up).&lt;/p&gt;

&lt;h2&gt;
  
  
  Decimal Places vs. Significant Figures in Scientific Code
&lt;/h2&gt;

&lt;p&gt;A second production mistake is confusing fixed decimal places (&lt;code&gt;quantize(Decimal("0.01"))&lt;/code&gt;) with &lt;strong&gt;significant figures&lt;/strong&gt;. Sensor pipelines and chemistry labs care about relative precision across orders of magnitude (&lt;code&gt;0.00452&lt;/code&gt; has 3 significant figures, just like &lt;code&gt;4520&lt;/code&gt;).&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;2 Decimal Places&lt;/th&gt;
&lt;th&gt;3 Significant Figures&lt;/th&gt;
&lt;th&gt;Common Mistake&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0.004567&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0.00&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0.00457&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Fixed decimals erase small measurements&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1234.5&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1234.50&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1230&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Keeping trailing noise digits implies false accuracy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;2.665&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;2.66&lt;/code&gt; (even) / &lt;code&gt;2.67&lt;/code&gt; (half-up)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;2.67&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Silent mismatch between Python &lt;code&gt;round()&lt;/code&gt; and Excel &lt;code&gt;ROUND()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you are validating scientific telemetry or unit-test fixtures that enforce measurement precision, you can cross-check edge cases (like trailing zeros after a decimal point) with the &lt;a href="https://www.mathisimple.com/calculator/significant-figures-calculator" rel="noopener noreferrer"&gt;significant figures calculator on MathIsimple&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Checklist Before Shipping Numerical Code
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Specify the rule in the spec&lt;/strong&gt;: write &lt;code&gt;ROUND_HALF_UP&lt;/code&gt; or &lt;code&gt;ROUND_HALF_EVEN&lt;/code&gt; explicitly—never just "round to 2 decimals".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep money in integer minor units or &lt;code&gt;Decimal&lt;/code&gt; from string inputs&lt;/strong&gt;: never construct &lt;code&gt;Decimal(2.675)&lt;/code&gt; from a float literal; always use &lt;code&gt;Decimal("2.675")&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Round at presentation or invoice-line boundaries, not mid-pipeline&lt;/strong&gt;: cumulative intermediate rounding causes off-by-one penny reconciliation failures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test midpoint ties explicitly&lt;/strong&gt;: include both odd-adjacent (&lt;code&gt;1.25&lt;/code&gt;, &lt;code&gt;2.675&lt;/code&gt;) and even-adjacent (&lt;code&gt;1.245&lt;/code&gt;, &lt;code&gt;2.665&lt;/code&gt;) fixtures in your test suite.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Note: Portions of the formatting and editorial drafting for this article were assisted by AI tools; all code examples, IEEE 754 outputs, and edge cases were reviewed and verified by the author.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>python</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
