Make class proxies hashable and compare them as the sandbox does #1310
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1310"
general
weight: 0.35
rewitt94
Problem
MontyClassProxy(the host-side stand-in for a sandbox class instance) defined__eq__without__hash__, so CPython made it unhashable.A sandbox result that was a set or frozenset of class instances, or a dict keyed by them, failed to convert with
TypeError: unhashable type.MontyClassTypeProxyhad the same gap.The existing
__eq__(class name +is_dataclass+ every attribute) also disagreed with the sandbox: two distinct instances of a plain class compare unequal in the sandbox (identity) but compared equal on the host.Adding a hash on top of that
__eq__would have silently merged distinct sandbox objects in a returned set or dict.Change
The worker now reports how each sandbox class compares and hashes, and the host proxy follows that rule where it can replay it without running sandbox code.
monty-types:ClassTypeNodegainseq: ClassEq(Identity/Fields/Custom) andhash: ClassHash(Identity/Fields/Unhashable/Custom), defaulting to identity.monty(object_bridge.rs): sandbox classes fill them in on export, read the same wayInstance::py_hashandinstance_user_eqdispatch; the field lists come from the@dataclass-generated__eq__/__hash__. Host classes always send identity.monty-proto: four additive fields onType(6–9:eq,eq_fields,hash,hash_fields). The zero value is identity, so older peers keep identity behaviour; an unknown enum value decodes as custom.MontyClassProxy.__eq__/__hash__follow the rule; a non-proxy operand returnsNotImplemented.MontyClassTypeProxygains__hash__over the class id.__eq__or__hash__, in a plain class or a dataclass body), the proxy compares and hashes by instance id instead of approximating. The one exception is a user__hash__beside a generated__eq__(row 12): an id hash would give equal proxies different hashes, so it hashes the fields__eq__compares. The hash always follows the proxy's==: a frozen dataclass with a body__eq__(row 11) compares by id, so it hashes by id rather than by its generated field hash, which could differ between two snapshots of one object. A class with__eq__and no__hash__stays unhashable.==memoises equal proxy pairs, so a result graph of shared nested dataclasses hashes and compares in linear time on the host (it was exponential, outside any worker limit). Proxies share one class header per class node.RecursionErrorpast 1000 nested proxies. CPython'shash()has no recursion guard (==does), and a dataclass chain with a non-recursive sandbox__hash__returned as a dict key segfaulted the host at 100k nodes. The depth error is not cached.TypeErrorand missing-fieldAttributeErrorembed the sandbox-chosen class name, and one copy per instance let a small result grow the host by gigabytes.MontyClassProxy.attributes(andMontyClassTypeProxy.attributes) now returns a copy, so a host edit cannot change a proxy's==, or its hash while it is in a set or dict. Edits were already not applied to a live sandbox object; they no longer reach the copy a restored session rebuilds from the proxy either._monty.pyi,limitations/classes.md,limitations/host-values.md,docs/host-objects.md.Notes for review
PROTOCOL_VERSIONis not bumped: the change is additive and an older peer that ignores the fields gets identity, which fits its doc comment. Maintainer's call.monty-private: anyClassTypeNode { .. }struct literal there (e.g. inmonty-cpython) needseq/hash. A CPython worker could also fill in real rules from its classes.- Two distinct plain-class instances with equal attributes no longer compare equal on the host.
- A class with a user-defined
__eq__(rows 2, 3, 10, 11, 13, and a dataclass field holding one, row 18) now compares by id on the host. Before, it compared every attribute, which matched the sandbox only when the user__eq__did the same:Money(5) == Money(5)across two proxies wasTrue, it is nowFalse. Compare in the sandbox for the real answer.- Editing
proxy.attributesno longer changes anything.monty-js/ wasm changes are compile- and clippy-checked only;make test-js/make test-wasmwere not run.Host vs sandbox, before and after
"id" is the object's boundary uuid. "Before" is empty where the behaviour did not change.
Sandbox class instances (
MontyClassProxy)| # | Class | Sandbox
==/hash| Host now | Now | Before ||---|---|---|---|---|---|
| 1 | Plain class | identity / identity | id / id | Same |
==differed: distinct objects with equal attributes were equal. Hash differed:TypeError|| 2 |
__eq__only | user / unhashable | id /TypeError|==differs |==differed (attribute comparison; agreed only when the user__eq__compared all attributes). Hash same || 3 |
__eq__+__hash__| user / user | id / id |==differs |==differed (as 2). Hash differed:TypeError|| 4 |
__hash__only | identity / user | id / id | Same |==differed (as 1). Hash differed:TypeError|| 5 |
__hash__ = None, no__eq__| identity / unhashable | id /TypeError| Same |==differed (as 1). Hash same || 6 |
@dataclass| fields / unhashable | fields /TypeError| Same |==mostly same: it compared all attributes, not only fields, and compared classes by name. Hash same || 7 |
@dataclass(frozen=True)| fields / fields | fields / fields | Same |==mostly same (as 6). Hash differed:TypeError|| 8 |
@dataclass(eq=False)| identity / identity | id / id | Same |==differed (as 1). Hash differed || 9 |
@dataclass(eq=False, frozen=True)| identity / identity | id / id | Same |==differed. Hash differed || 10 |
@dataclass+ body__eq__| user / unhashable | id /TypeError|==differs |==differed (as 2). Hash same || 11 |
@dataclass(frozen=True)+ body__eq__| user / fields | id / id |==differs |==differed. Hash differed || 12 |
@dataclass+ body__hash__| fields / user | fields / hash of the hashable fields | Same |==mostly same. Hash differed || 13 |
@dataclass+ body__eq__+__hash__| user / user | id / id |==differs |==differed. Hash differed || 14 |
compare=False,hash=False,unsafe_hash,order| rejected by the sandbox | — | Not possible | |Host hash values never equal the sandbox's; only consistency with
==matters.Fields inside a dataclass
| # | Field holds | Now | Before |
|---|---|---|---|
| 15 | Builtin values | Same | |
| 16 | Another dataclass | Same | |
| 17 | A plain-class instance | Same | Differed: distinct inner objects with equal attributes were equal |
| 18 | An instance with a user
__eq__| Differs: that field compares by id | Differed, though it agreed when the user__eq__compared all attributes (e.g.P(Money(5)) == P(Money(5))wasTrue, as in the sandbox) || 19 | A value that crosses as repr text | Differs | |
| 20 | Nothing: a field the instance never set | Same (
AttributeError) when the class has no default for it; differs when it does: the sandbox reads the class-level default, the host raisesAttributeError| Differed: no error; compared as a missing dict key || 21 | A cycle back to itself | Differs: the back-reference crosses as the string
'...', so two distinct self-referencing instances compare equal on the host; the sandbox raisesRecursionError| |Other situations
| # | Situation | Now | Before |
|---|---|---|---|
| 22 | Same object returned in two separate results |
isdiffers;==and hash same unless a dataclass field changed in between or holds NaN: each proxy is a snapshot compared by its fields, where the sandbox'sself is othersays equal |isdiffered;==same unless attributes changed between the two results; hashTypeError|| 23 |
proxy == 5| Differs when the class has a user__eq__| || 24 | Proxy vs an instance of another class whose reflected
__eq__returnsTrue| Differs | || 25 | Two classes with the same name | Same | Differed: equal when their attributes matched |
| 26 | Host edits
proxy.attributesof a frozen dataclass | Same:attributesreturns a copy, so the proxy is unaffected | Differed:==changed || 27 | Returned set, frozenset or dict containing proxies | Same | Differed: conversion failed with
MontyRuntimeError: ... cannot convert the result to a Python value: ... unhashable|Host-defined classes sent in with
ClassInstance| # | Situation | Now | Before |
|---|---|---|---|
| 28 | Returned in the same session (the original object) | Same | |
| 29 | Returned after a restore (proxy) | Differs for host classes that compare by value; same for identity classes | Differed for host classes that compare by identity; mostly same for value classes (host dataclasses) |
Class objects
| # | Value | Now | Before |
|---|---|---|---|
| 30 | Host class, same session | Same | |
| 31 | Host class or
type(x)after a restore (MontyClassTypeProxy) | Same |==same; hash differed (TypeError, so they couldn't go in sets) || 32 | Sandbox class object (
A) | Differs (crosses as text) | |What still differs, and whether it can be fixed
__eq__): not fixable on the host without running sandbox code, so by design the proxy falls back to identity rather than guess. Comparing attributes instead (the old behaviour) would let a returned set or dict silently drop an element whenever a user__eq__calls attribute-equal objects unequal. The exact answer issession.feed_run('a == b', inputs=...)while both objects are still referenced in the sandbox.HostClassType(a dumped type, so a#[serde(default)]field).isacross results, via a per-session weak proxy cache) and 32 (sandbox class objects asMontyClassTypeProxyrather than text): fixable, but they change what users get back, so they need a maintainer decision.Tests
crates/monty-python/tests/test_class_instance.py: 18 new tests and a rewrittentest_proxy_equality(147 passed), covering rows 1, 2, 3, 6, 7, 10, 11, 12, 13, 16, 17, 20, 21, 22, 23, 25, 26, 27 and 31. Rows 4, 5, 8 and 9 have no dedicated test. Three more cover the hash depth guard and shared error messages.monty-proto: differential test for unknown rule values; the oracle mirror and budget tests carry the new fields.cargo nextest run -p monty -p monty-runtime -p monty-proto -p monty-types,make lint-rs,make lint-py(including stubtest),make lint-mdpass;make generate-protois in sync.