Skip to content

fix: assign the result of Context.add in transform/atransform - #1477

Open
planetf1 wants to merge 1 commit into
generative-computing:mainfrom
planetf1:fix/1475
Open

fix: assign the result of Context.add in transform/atransform#1477
planetf1 wants to merge 1 commit into
generative-computing:mainfrom
planetf1:fix/1475

Conversation

@planetf1

@planetf1 planetf1 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Pull Request

Issue

Fixes #1475

Description

transform() and atransform() both call new_ctx.add(chosen_tool) as a bare statement. Context.add is non-mutating, so the returned context is discarded and the chosen tool message never reaches the context handed back to the caller — despite an info log claiming it was added.

Fix: assign the result at both call sites (mellea/stdlib/functional.py:502 in transform(), :1271 in atransform()).

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code was added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used

Adding a new component, requirement, sampling strategy, or tool?

If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.

Context.add is non-mutating, but new_ctx.add(chosen_tool) in transform()
and atransform() was called as a bare statement, discarding the returned
context. The chosen tool message never reached the context handed back
to callers, despite an info log claiming it had been added.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <jonesn@uk.ibm.com>
@github-actions github-actions Bot added the bug Something isn't working label Jul 30, 2026
@planetf1
planetf1 marked this pull request as ready for review July 31, 2026 14:51
@planetf1
planetf1 requested a review from a team as a code owner July 31, 2026 14:51

@AngeloDanducci AngeloDanducci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, one non blocking enhancement.

Comment on lines +349 to +351
_, new_ctx = transform(MObject(), "transform it", ctx, MagicMock())

assert tool_message in new_ctx.as_list()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be nice to verify ordering? Not a blocker though.

Suggested change
_, new_ctx = transform(MObject(), "transform it", ctx, MagicMock())
assert tool_message in new_ctx.as_list()
_, new_ctx = transform(MObject(), "transform it", ctx, MagicMock())
# The tool message must be present and land as the most-recent turn.
assert tool_message in new_ctx.as_list()
assert new_ctx.as_list()[-1] is tool_message

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion to verify order and I would add also that it should verify that add was additive and did not clobber the initial contents.

Unfortunately I think the current mock setup makes the result a list of one (it is added to empty) so it does not really verify order or additive.

This concern also makes me think this might be more than an nit/good-idea and actually quite valuable.

   assert len(new_ctx.as_list()) > 1
   assert tool_message == new_ctx.as_list()[-1]

@markstur markstur left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good 2-line fix

Test fails before fix and passes after 🆒

Testing order is a good idea. I'd add that it needs to also test that we are adding to a non-empty list.

Comment on lines +349 to +351
_, new_ctx = transform(MObject(), "transform it", ctx, MagicMock())

assert tool_message in new_ctx.as_list()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion to verify order and I would add also that it should verify that add was additive and did not clobber the initial contents.

Unfortunately I think the current mock setup makes the result a list of one (it is added to empty) so it does not really verify order or additive.

This concern also makes me think this might be more than an nit/good-idea and actually quite valuable.

   assert len(new_ctx.as_list()) > 1
   assert tool_message == new_ctx.as_list()[-1]

"""
from mellea.stdlib.functional import transform

ctx = ChatContext()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If testing order and non-clobbering of existing entries. Maybe this?

ctx = ChatContext().add(Message("user", "test"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transform()/atransform() discard the result of Context.add, so the tool message never reaches the returned context

3 participants