Repository navigation
Breaking: refuse Google ADK method references whose class was rebound - #363
Conversation
activity_as_tool tells a method from a staticmethod by reading the attribute that the function's __qualname__ names in its class. If the class's name or that attribute was rebound after the reference was taken, the attribute is another object, and the reference was classified by it: a staticmethod could lose its self, so the model's value reached the wrong parameter. Check that the attribute is the function, or a staticmethod of it, and raise ValueError otherwise, as for a class that can't be reached.
DABH
left a comment
There was a problem hiding this comment.
Review at head 53ba069 (two commits on 88745dc, current main). On a worktree at the head: make lint is clean and make test gives 180 passed, 5 skipped (Python 3.14.3, temporalio 1.34.0, google-adk 2.12.0); CI is green. Both findings were reproduced with probes against installed builds of 0.0.1, main, the head and a one-line fix; the first also end to end with real Workers on the pinned dev server under the sandboxed runner, with histories recorded on 0.0.1 and on main replayed across the builds. The body's claims hold: #362's reproduction exits 1 on main and 0 on the head; the rejection test fails on main with DID NOT RAISE ValueError and the metadata test passes there; four single-line mutants of the new check (no check, the first commit's is not fn, ignoring a staticmethod's __func__, unwrapping without stop) are each caught by the new tests. Checked and refuted: false refusals for functools.wraps-style decorators (tenacity's @retry, warnings.deprecated, pydantic's validate_call), for dataclass(slots=True) and attrs @define classes, which rebuild the class, and inside the Workflow sandbox (the suite's integration test builds the method, staticmethod, decorated and private-class forms in Workflow code); any change to a tool the head still builds (_is_method returns main's value whenever it does not raise). No other Python plugin has this classification, and Go and TypeScript take function values or registered names, so the languages stay consistent. Nothing material was left unverified.
Severity: P1 = wrong result or stuck run in realistic use; P2 = correctness or robustness in an edge case; P3 = docs, tests, cleanup.
Requesting changes for one item: the new check also refuses wrapt-decorated methods that nothing rebound, so 0.0.1 runs that build such a tool stop replaying on upgrade, a break the body does not disclose; the one-line fix in the inline comment removes it and keeps every refusal this PR adds.
Replay, at the head: for every tool the head still builds, nothing changes. The 60 replays of the #312 review's histories (function, bound-method, staticmethod, method, callable-instance, class, tool_context and module-placement forms, recorded on #312's base and head, sandboxed and unsandboxed, finished and held) give identical outcomes on main and on the head: 49 replay, and the same 11 fail on #312's own changes. That agrees with the body's 16-history check, and the disclosed break holds as described (a staticmethod taking self, referenced after its class was rebound, now raises when the tool is built). The undisclosed break is the P2: histories recorded on 0.0.1 with a wrapt-decorated Class.method tool, including two runs that completed without calling it, replay on 0.0.1, on main and on the patched copy, and fail on the head. Those replays ran with debug_mode=True, because 0.0.1's own slow first activation intermittently trips the deadlock detector.
| target = attr.__func__ if static else attr | ||
| # fn may wrap the attribute's function, as a functools.wraps decorator | ||
| # applied to Class.method does. | ||
| if inspect.unwrap(fn, stop=lambda f: f is target) is not target: |
There was a problem hiding this comment.
P2: the identity check also refuses methods decorated with a wrapt-based decorator, which nothing rebound, so 0.0.1 runs that build such a tool stop replaying on upgrade, and the body does not disclose it. wrapt keeps a FunctionWrapper in the class __dict__, and class access returns a new BoundFunctionWrapper whose __wrapped__ is the plain function, so the attribute is never on fn's unwrap chain and this line raises. Probe, wrapt 2.4.1, with class WraptHolder: @activity.defn @traced async def meth(self, trip: str) (traced a @wrapt.decorator): main offers ['trip']; the head raises Cannot tell whether 'WraptHolder.meth' is a method or a staticmethod: 'WraptHolder.meth' reachable from the function's globals is another object, in either decorator order. The Deprecated package's @deprecated(reason=...) (3.0.1, a wrapt.FunctionWrapper subclass) does the same. Replay, with real Workers on the pinned dev server: 0.0.1 offers the tool as ['self', 'trip'] and completes a run whose tools are track and WraptHolder.meth when the model calls only track, and a run with only WraptHolder.meth that calls nothing. Both histories replay on 0.0.1 and on main, and fail on the head with that ValueError, so a run still open at the upgrade fails every Workflow Task until the code changes. On main the same tool runs end to end (the Worker's WraptHolder("worker").meth returns wrapt-meth:worker:meth.trip); on the head a new run fails its first Workflow Task. A wrapt staticmethod works on all three. wrapt is already in this plugin's lockfile through the dev group's OpenInference instrumentation. Fix: compare the innermost functions, if inspect.unwrap(fn) is not inspect.unwrap(target):, and reword the comments here and the _is_method docstring to match. On a patched copy the wrapt and Deprecated methods offer ['trip'] again, the 0.0.1 and main histories replay, #362's reproduction still exits 0, references kept across a reload are still refused, and make lint and the full suite pass (180 passed, 5 skipped). To pin it without depending on wrapt, a 12-line decorator class in tests/helpers/decorators.py whose __get__ returns a fresh functools.wraps wrapper of self.__wrapped__.__get__(obj, cls) reproduces the refusal (main and the patched copy offer ['passengers'], the head raises) and fits as a row in the metadata loop. If you keep the stricter check instead, the Breaking change paragraph and the docstring need to name wrapt-style decorated methods and their 0.0.1 runs.
There was a problem hiding this comment.
Good catch, thanks. Fixed in 3beefd3 with your one-liner: the check now compares inspect.unwrap(fn) with inspect.unwrap() of the attribute (or of the staticmethod's __func__), and the comment and the _is_method docstring say so. A method under a real @wrapt.decorator (2.4.1, from the lockfile) passed as Class.method offers ['trip'] again, as on main. #362's reproduction and the kept references after a rebind, bare or wrapped, are still refused. To pin it without wrapt, I used your suggestion: WrapOnAccess in tests/helpers/decorators.py returns a new functools.wraps wrapper on each class access, and ComplexActivityMethodHolder.count_wrapped_on_access is a row in the metadata loop. With 53ba069's workflow.py it fails with ... is another object. Since these methods build again, the Breaking change paragraph doesn't need to name them.
| raise ValueError( | ||
| f"Cannot tell whether '{fn.__qualname__}' is a method or a" | ||
| f" staticmethod: '{'.'.join(qualname)}' reachable from the" | ||
| " function's globals is another object. Pass a bound method for a" |
There was a problem hiding this comment.
P3: the refusal fires after any in-process reload, not only one that changed the class, and neither remedy in the message fits that case. importlib.reload re-executes the module into the same globals, so every reference taken before it fails the check even when the file is unchanged. Probe: from acts import Holder, keep Holder.count (method) and Holder.echo (staticmethod taking self), then reload the unchanged acts: main offers ['passengers'] and ['self']; the head raises for both, and also for Holder.count read again through the caller's Holder, which is still the old class (a re-run notebook cell does exactly this). Refusing is the trade the body chooses, but 'as after importlib.reload of a changed module' understates it for the release notes, and that user needs to take the reference again, not pass a bound method or rename a parameter. Suggested: say 'after a reload' in the body, and end the message with 'Take the reference again if its module was reloaded, pass a bound method for a method, or rename a staticmethod's parameter.'
There was a problem hiding this comment.
Agreed. In 3beefd3 the message ends with "Take the reference again if its module was reloaded, pass a bound method for a method, or rename a staticmethod's parameter." The body now says any importlib.reload of the module, changed or not, refuses references taken before it, in the change description and under Breaking change.
…a reload The identity check compared the reference's unwrap chain with the class's attribute. A wrapt-style decorator stores a wrapper in the class and returns a new one on each access, so the attribute was never on the chain, and methods under such decorators, which nothing rebound, were refused. Compare the innermost functions instead. Any reload of the module refuses references taken before it, so the message now also says to take the reference again.
DABH
left a comment
There was a problem hiding this comment.
Second round, at head 16df276 (3beefd3 plus a merge of main at ca669af, which changes nothing under python/google_adk). On a worktree at the head: make lint is clean and make test gives 180 passed, 5 skipped (Python 3.14.3, temporalio 1.34.0, google-adk 2.12.0), with xdist and again in a single process; CI is green on all four Python cells, Ubuntu 3.11 included. Last round's reproductions were rerun against installed builds of 0.0.1, main, the head and 53ba069 as a control, and its recorded histories were replayed on each. The body's claims hold: #362's reproduction exits 1 on main and 0 on the head; with main's workflow.py the rejection test fails with DID NOT RAISE ValueError and the metadata test passes; with 53ba069's workflow.py the metadata test fails at the count_wrapped_on_access row with ... is another object; a @wrapt.decorator method offers ['trip'] on main and on the head. Single-line mutants of the new comparison are caught (unwrapping only one side, 53ba069's stop-based check, or no check each fail one of the two changed tests); dropping __func__ is an equivalent mutant, because a staticmethod carries __wrapped__ since Python 3.10, so that conditional is redundant but harmless. Checked and refuted: new false refusals for tenacity's @retry, warnings.deprecated, pydantic's validate_call, a descriptor that wraps on each access, and dataclass(slots=True) and attrs @define classes including a method that uses super() (all give main's parameters); the new asyncio.run in a sync test disturbing later async tests in the same process (pytest-asyncio 0.21.2 hands each test the session loop explicitly, and the single-process run passes). No other Python plugin classifies methods this way (LangGraph's __qualname__ walk is an importability check by identity), Go takes a function value and TypeScript an activity name, so the languages stay consistent. Nothing material was left unverified.
Severity: P1 = wrong result or stuck run in realistic use; P2 = correctness or robustness in an edge case; P3 = docs, tests, cleanup.
Previous rounds. Addressed: the blocking P2. The check now compares innermost functions and WrapOnAccess pins it without wrapt. Rerunning the original reproduction: @wrapt.decorator methods in either decorator order offer ['trip'] on the head as on main (53ba069 still raises), so do Deprecated 3.0.1's @deprecated(reason=...) methods, and wrapt staticmethods keep ['self']. The three 0.0.1 histories with a wrapt Class.method tool (called with the model's self, never called, and next to a called track) and the two main histories all replay on the head; 53ba069 still fails all four method histories with its ValueError. The P3 is addressed: the body now says any reload, changed or not, refuses earlier references, and the message now tells the user to take the reference again. That remedy still has a gap, the one inline comment below.
Replay, at the head: for every tool the head builds, nothing changes. _is_method returns main's value whenever it does not raise, and the 60 replays of the #312 review's histories give identical results on main and on the head (49 replay; the same 11 fail on #312's own changes). The disclosed break (a staticmethod taking self, referenced before its class's name or attribute was rebound or its module reloaded) holds as described, and 0.0.1 runs that build a wrapt-decorated method tool replay again.
Approving: last round's blocking item is fixed and verified by rerunning its reproduction and replays, CI is green, and the only remaining item is a non-blocking wording fix in the new error message.
| raise ValueError( | ||
| f"Cannot tell whether '{fn.__qualname__}' is a method or a" | ||
| f" staticmethod: '{'.'.join(qualname)}' reachable from the" | ||
| " function's globals is another object. Take the reference again" |
There was a problem hiding this comment.
P3 (non-blocking): 'Take the reference again if its module was reloaded' does not work for a caller that imported the class by name; that caller has to import the class again. importlib.reload rebinds the module's Holder, but a name bound earlier with from acts import Holder still holds the old class. Probe at the head with last round's acts (unchanged file): after importlib.reload(acts), Holder.count and Holder.echo read again through the caller's Holder are the old functions and are refused with this message again, while acts.Holder.count, or Holder.count and Holder.echo after a fresh from acts import Holder, build with ['passengers'] and ['self'], as on main. That is the wording I suggested last round, and it falls short for this reason. Suggested: 'Import the class again if its module was reloaded, pass a bound method for a method, or rename a staticmethod's parameter.', with the body's matching sentence updated to match.
There was a problem hiding this comment.
Good point, thanks. In 12b0028 the message ends with "Import the class again if its module was reloaded, pass a bound method for a method, or rename a staticmethod's parameter.", and the body's sentence matches. Separately, the body now names one case the check still accepts, under "Not covered": a reference kept before its class attribute was replaced by a wrapper of that same function (for example Holder.echo = traced(Holder.echo) on a staticmethod). It's still classified by the replacement, as on main. It unwraps the same way as an unchanged wrapt-style method, so catching it would take a much more complex check.
A name bound with `from module import Class` keeps the old class after importlib.reload, so taking the reference again through it is refused again. The caller has to import the class again.
What was changed
activity_as_toolingoogle_adktells a method passed asClass.methodfrom a staticmethod by finding the class that the function's__qualname__names, in the function's globals, and reading that attribute (#312). It now also checks that the attribute (or the staticmethod's function) and the reference wrap the same innermost function. So afunctools.wrapswrapper such asdecorate(Class.method)still counts, and so does a method under a wrapt-style decorator, which keeps one wrapper in the class and returns a new one on each access. If the class's name or that attribute now holds another object, as after anyimportlib.reloadof its module, changed or not,activity_as_toolcan't tell what the reference is, so it raisesValueError, as it does for a class it can't reach. The message says to import the class again after a reload, pass a bound method for a method, or rename a staticmethod's parameter.The
activity_as_tooldocstring names the new case.Breaking change: compared with 0.0.1, a staticmethod taking a parameter named
self, referenced before its class's name or attribute was rebound (including by any reload of its module), now fails withValueErrorwhen the tool is built, and its runs stop replaying. On 0.0.1 its tool offeredself. #312 already does the same for a class that can't be reached. A method referenced this way didn't work on 0.0.1 either. #312 isn't released yet, so both land in the same release, and the release notes can list this case with #312's. No version gate.Not covered: a reference kept before its class attribute was replaced by a wrapper of that same function, such as
Holder.echo = traced(Holder.echo)on a staticmethod, is still accepted and classified by the replacement, as onmain. Both unwrap to the same function, which is also how an unchanged wrapt-style method looks, and telling them apart would take a much more complex check. Such a patch already makes the staticmethod a method for the rest of the code, including a Worker that registers a bound method.Why?
See #362. On
mainsince #312, such a reference is classified by the object now under the name. A staticmethodecho(self, value), kept before its class's name was rebound to a class with a methodecho, gets a tool withoutself. The model'svaluereachesself, and the call returns a wrong result with no error. In the reverse case, a method keepsselfin its tool.Checklist
Closes [Bug] google_adk:
activity_as_toolclassifies a method reference by its class's rebound attribute (main only) #362. Follow-up to Breaking: support method and callable-class Activities in Google ADK activity_as_tool #312.How was this tested:
test_activity_as_tool_rejects_method_of_unreachable_classnow also rebinds the class's module-level name to a class with the opposite kinds. It checks that a kept staticmethod reference and a kept method reference both raiseValueError, bare and wrapped with afunctools.wrapsdecorator. Withmain'sworkflow.py(88745dc) installed, it fails withDID NOT RAISE ValueError.test_activity_as_tool_preserves_metadatanow passes afunctools.wrapswrapper of an unchangedClass.staticmethod, of aClass.method, and of an already decorated staticmethod. It checks their parameters, the wrapped staticmethod's ADK schema, and that calling its tool returns the model'sself. It passes onmaintoo.activity_as_toolclassifies a method reference by its class's rebound attribute (main only) #362's reproduction exits 0 on 0.0.1 (the tool offers['self', 'value']and returnsself=model-self value=model-value), 1 onmain(['value'],self=model-value value=default), and 0 with this change, whereactivity_as_toolraisesValueError.test_activity_as_tool_preserves_metadataalso checks a method under a decorator that returns a newfunctools.wrapswrapper on each class access, as wrapt's do (WrapOnAccessintests/helpers/decorators.py). With53ba069'sworkflow.pyit fails withValueError: ... is another object.@wrapt.decorator(wrapt 2.4.1, already in the lockfile), passed asClass.method, offers['trip']onmainand with this change;53ba069refused it.uv.lock(not throughmake): ruff, pyright, mypy, basedpyright, pydocstyle and the conventions check pass, and the suite gives 180 passed and 5 skipped.main, and so does the control. No new histories were recorded: the change only adds a refusal when the tool is built, and changes no command of a tool it builds.Any docs updates needed?
The
activity_as_tooldocstring is updated here. The release notes should list this case with Breaking: support method and callable-class Activities in Google ADK activity_as_tool #312's breaking changes.