Repository navigation
feat(python): loss codes as Python warnings and MCP losses (#2371) - #2755
Merged
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Python and MCP get the loss codes that
rustgis-cli run --strictuses, 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,ApproximatedWarningorAssumedWarning, all subclasses ofrustgis.DataLossWarning. The warning hascodeandkind, and its message is the notice's text followed by the code. Python's own filters decide what happens, sowarnings.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 itsRustGISErrorstays 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 existingnoticesstrings. Theruntool and the file bridge always includelosses, 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) andlossesTrimmedsays so. A result too large by itself is cut to leave room for one entry per code, keeping itssuccess, so the file bridge still reads it. Losses whose notices the session no longer keeps come as one entry per code with acount. A failed command's error text gains alosses: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. Therun_commandandrundescriptions say so, and suggest asking the user for anassumed.*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
logginghandler for therustgislogger that calls back into the same session while a call is still logging can raise that call's losses itself.Testing
DroppedWarningwithcode == "dropped.ring"at the caller's line, and the notice stays innotices(). An error filter raises it, after the layer was added. A fraction written to a whole-number field raisesChangedWarning(changed.truncated), and a clean calculation raises nothing.RustGISErrorwith theDroppedWarningas a note under an error filter, and both otherwise.service.addwith a legend address too long to keep returnslosseswithdropped.legend, its action and the notice's text. A clean add has nolosses, and theruntool returns the loss in a script and[]for a clean one. A failedscript.runkeepsdropped.legendin itslosses:line, and a failedrunin its JSON. A login value oflegendcleans the notice's text but not the code. Losses past the output budget keep their codes.successand 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.-D warnings) on the assistant and the Python bindings.UI evidence
No visible UI change.
Related issue
Part of #2371.
Summary by CodeRabbit
lossesfield even when empty; details may be compacted when output space is limited.