Skip to content

Conversation

@lbobinski
Copy link

This PR fixes a bug where setting historyLength=0 would return the full history instead of an empty list.

Thank you for opening a Pull Request!
Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Follow the CONTRIBUTING Guide.
  • Make your Pull Request title in the https://www.conventionalcommits.org/ specification.
    • Important Prefixes for release-please:
      • fix: which represents bug fixes, and correlates to a SemVer patch.
      • feat: represents a new feature, and correlates to a SemVer minor.
      • feat!:, or fix!:, refactor!:, etc., which represent a breaking change (indicated by the !) and will result in a SemVer major.
  • Ensure the tests and linter pass (Run bash scripts/format.sh from the repository root to format)
  • Appropriate docs were updated (if necessary)

Fixes #573 🦕

@lbobinski lbobinski requested a review from a team as a code owner January 8, 2026 16:24
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @lbobinski, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug in the apply_history_length utility function, ensuring that when a history length of zero is specified, an empty history is correctly returned. This change improves the precision of history management and is validated by comprehensive new unit tests covering various history length scenarios.

Highlights

  • Bug Fix: Corrected an issue where setting historyLength=0 in the apply_history_length function would inadvertently return the full history instead of an empty list.
  • Logic Update: The apply_history_length function in src/a2a/utils/task.py was updated to explicitly return an empty list when historyLength is set to 0.
  • Test Coverage: New unit tests were added in tests/utils/test_task.py to ensure the apply_history_length function behaves as expected for various historyLength values, including 0, 1, 2, None, and values exceeding the history size.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly fixes the bug where historyLength=0 would return the full history instead of an empty list. The logic change in apply_history_length is sound, and the new test cases in test_apply_history_length_cases effectively verify the fix. I have one suggestion to refactor the new test to make it more robust and maintainable by using subTest and parameterization.

Comment on lines +192 to +228
# Setup task with 3 messages
history = [
Message(role=Role.user, parts=[Part(root=TextPart(text='1'))], message_id='1'),
Message(role=Role.agent, parts=[Part(root=TextPart(text='2'))], message_id='2'),
Message(role=Role.user, parts=[Part(root=TextPart(text='3'))], message_id='3'),
]
task_id = str(uuid.uuid4())
context_id = str(uuid.uuid4())
task = completed_task(
task_id=task_id,
context_id=context_id,
artifacts=[Artifact(artifact_id='a', parts=[Part(root=TextPart(text='a'))])],
history=history
)

# historyLength = 0 -> empty
t0 = apply_history_length(task, 0)
self.assertEqual(len(t0.history), 0)

# historyLength = 1 -> last one
t1 = apply_history_length(task, 1)
self.assertEqual(len(t1.history), 1)
self.assertEqual(t1.history[0].message_id, '3')

# historyLength = 2 -> last two
t2 = apply_history_length(task, 2)
self.assertEqual(len(t2.history), 2)
self.assertEqual(t2.history[0].message_id, '2')
self.assertEqual(t2.history[1].message_id, '3')

# historyLength = None -> all
tn = apply_history_length(task, None)
self.assertEqual(len(tn.history), 3)

# historyLength = 10 -> all
t10 = apply_history_length(task, 10)
self.assertEqual(len(t10.history), 3)
Copy link
Contributor

Choose a reason for hiding this comment

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

low

This test is great for covering the main scenarios. It could be made more robust and maintainable by parameterizing the test cases and using unittest.subTest. This approach has a few benefits:

  • Clarity: It separates the test data from the test logic, making it easier to see what's being tested.
  • Maintainability: Adding new test cases is as simple as adding a new entry to the test_cases dictionary.
  • Better Failure Reporting: subTest ensures that all cases are run, and it reports failures for each subtest individually, rather than stopping at the first failure.
  • Improved Assertions: The assertions can be made more consistent and thorough by checking the exact sequence of message IDs for every case.
        # Setup task with 3 messages
        history = [
            Message(role=Role.user, parts=[Part(root=TextPart(text='1'))], message_id='1'),
            Message(role=Role.agent, parts=[Part(root=TextPart(text='2'))], message_id='2'),
            Message(role=Role.user, parts=[Part(root=TextPart(text='3'))], message_id='3'),
        ]
        task = completed_task(
            task_id=str(uuid.uuid4()),
            context_id=str(uuid.uuid4()),
            artifacts=[Artifact(artifact_id='a', parts=[Part(root=TextPart(text='a'))])],
            history=history,
        )

        test_cases = {
            'zero_length': (0, []),
            'one_item': (1, ['3']),
            'two_items': (2, ['2', '3']),
            'none_length_is_full_history': (None, ['1', '2', '3']),
            'length_greater_than_history_is_full_history': (10, ['1', '2', '3']),
        }

        for name, (length, expected_ids) in test_cases.items():
            with self.subTest(msg=name):
                new_task = apply_history_length(task, length)
                actual_ids = [m.message_id for m in new_task.history]
                self.assertEqual(actual_ids, expected_ids)

@herczyn
Copy link
Member

herczyn commented Jan 8, 2026

this PR will break current agents, because if history_length is unset it defaults to zero. we should wait for the "optional" tag on the field to propagate, before introducing this.

@lkawka
Copy link
Member

lkawka commented Jan 9, 2026

+1 to what Kuba said. FYI I left a similar comment on the referenced issue a month ago.

@holtskinner
Copy link
Member

1.0 Migration PR #572

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.

[Bug]: History Length = 0, is returning all the history

4 participants