Skip to content

feat(python): loss codes as Python warnings and MCP losses (#2371) - #2755

Merged
Mavrick-F merged 21 commits into
opengeos:mainfrom
Mavrick-F:feat/loss-codes-python-mcp
Nov 23, 2026
Merged

Mavrick-F merged 21 commits into
opengeos:mainfrom
Mavrick-F:feat/loss-codes-python-mcp

Conversation

@Mavrick-F

@Mavrick-F Mavrick-F commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Python and MCP get the loss codes that rustgis-cli run --strict uses, so a script or an agent can tell a data loss from other notices without matching their wording.

Python. Each notice with a loss code is also raised as a warning: rustgis.DroppedWarning, ChangedWarning, ApproximatedWarning or AssumedWarning, all subclasses of rustgis.DataLossWarning. The warning has code and kind, and its message is the notice's text followed by the code. Python's own filters decide what happens, so warnings.filterwarnings("error", category=rustgis.DataLossWarning) turns losses into exceptions. The warning points at the caller's line. s.notices() is unchanged and still returns every notice. A command that failed still raises the losses that outlived its rollback, but its RustGISError stays the exception raised: a warning a filter turns into an error is added to it as a note.

Each native call sets aside its own losses under the session's lock, for the thread that made it, so a call on another thread never raises them. A loss whose notice the session no longer keeps (past its last 200) still raises one warning per code with the count.

MCP and the Assistant. A result whose notices report a classified loss also has losses: [{code, kind, text, action}], beside the existing notices strings. The run tool and the file bridge always include losses, empty when nothing was lost. They are added after the result is cleaned of login values, so a login can't rewrite a code, and their text is cleaned on its own. When the whole list doesn't fit the output budget beside the result, it shrinks (each entry's code and kind, then one entry per code, then the codes alone) and lossesTrimmed says so. A result too large by itself is cut to leave room for one entry per code, keeping its success, so the file bridge still reads it. Losses whose notices the session no longer keeps come as one entry per code with a count. A failed command's error text gains a losses: line of JSON, so a loss that outlived the rollback keeps its code there too. The error gives way first, measured in the bytes of the Assistant's {"error": …} within its output budget. The run_command and run descriptions say so, and suggest asking the user for an assumed.* code instead of guessing.

This adds an optional output field and Python classes, with no change to existing results (any maintainer). It reaches Python, MCP, the Assistant and the file bridge, which share the same tool results. The desktop, browser and phone show the same notices as before, and the command line has --strict. Losses that don't have a code yet are only in the notices. Classifying them is the rest of #2371.

One known limit: a Python logging handler for the rustgis logger that calls back into the same session while a call is still logging can raise that call's losses itself.

Testing

  • Python: a GeoRSS polygon with an unreadable hole raises DroppedWarning with code == "dropped.ring" at the caller's line, and the notice stays in notices(). An error filter raises it, after the layer was added. A fraction written to a whole-number field raises ChangedWarning (changed.truncated), and a clean calculation raises nothing.
  • Python: a lossy read on one thread isn't raised on another thread's call. A script that loses a legend and then fails raises RustGISError with the DroppedWarning as a note under an error filter, and both otherwise.
  • Assistant/MCP: service.add with a legend address too long to keep returns losses with dropped.legend, its action and the notice's text. A clean add has no losses, and the run tool returns the loss in a script and [] for a clean one. A failed script.run keeps dropped.legend in its losses: line, and a failed run in its JSON. A login value of legend cleans the notice's text but not the code. Losses past the output budget keep their codes.
  • Python: with an error filter, a script that loses a ring and a legend raises the first warning with the other as a note.
  • Assistant/MCP: at 200, 32 and 16 tokens a result too large by itself keeps its success and the codes within the budget, and an 8-token budget stays within its bytes. A long error with several bytes a character still ends with the codes inside a 200-token budget.
  • Clippy (-D warnings) on the assistant and the Python bindings.
  • Fork CI passed on the submitted commit, including the Python tests and the browser build.

UI evidence

No visible UI change.

Related issue

Part of #2371.

Summary by CodeRabbit

  • New Features
    • Python commands now emit categorized warnings for coded data loss while retaining details in session notices. Warning filters can promote warnings to errors or suppress selected types; promoted warnings are raised after the command completes.
    • Assistant command and script results now include classified loss details, including for failures. Script results include a losses field even when empty; details may be compacted when output space is limited.
  • Documentation
    • Updated the roadmap and Python and MCP documentation to describe loss notices, warnings, and reports, including that strict mode does not stop at loss notices.

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

1 participant