Skip to content

Commit acdc0de

Browse files
authored
fix: return entire history when history_length=0 (#537)
# Description The specification states that a history length of 0 should return unlimited results (see [code](https://github.com/a2aproject/A2A/blob/202aa069e66f701bacf2156d42d8916fc96a5188/specification/grpc/a2a.proto#L128-L130)). However, this was recently changed to return 0 results. This fix restores the correct behavior. Please note that there is an outstanding proposal to change this behavior. See a2aproject/A2A#1071 for more details. Prerequisites: - [x] Follow the [`CONTRIBUTING` Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md). - [x] Make your Pull Request title in the <https://www.conventionalcommits.org/> specification. - Important Prefixes for [release-please](https://github.com/googleapis/release-please): - `fix:` which represents bug fixes, and correlates to a [SemVer](https://semver.org/) 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. - [x] Ensure the tests and linter pass (Run `bash scripts/format.sh` from the repository root to format) - [x] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> 🦕
1 parent 45ff871 commit acdc0de

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/a2a/utils/task.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ def apply_history_length(task: Task, history_length: int | None) -> Task:
8383
A new task object with limited history
8484
"""
8585
# Apply historyLength parameter if specified
86-
if history_length is not None and task.history:
86+
if history_length is not None and history_length > 0 and task.history:
8787
# Limit history to the most recent N messages
88-
limited_history = (
89-
task.history[-history_length:] if history_length > 0 else []
90-
)
88+
limited_history = task.history[-history_length:]
9189
# Create a new task instance with limited history
9290
return task.model_copy(update={'history': limited_history})
9391

tests/server/request_handlers/test_default_request_handler.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,11 @@ async def test_on_message_send_non_blocking():
834834

835835
assert task is not None
836836
assert task.status.state == TaskState.completed
837+
assert (
838+
result.history
839+
and task.history
840+
and len(result.history) == len(task.history)
841+
)
837842

838843

839844
@pytest.mark.asyncio
@@ -855,7 +860,7 @@ async def test_on_message_send_limit_history():
855860
configuration=MessageSendConfiguration(
856861
blocking=True,
857862
accepted_output_modes=['text/plain'],
858-
history_length=0,
863+
history_length=1,
859864
),
860865
)
861866

@@ -866,17 +871,17 @@ async def test_on_message_send_limit_history():
866871
# verify that history_length is honored
867872
assert result is not None
868873
assert isinstance(result, Task)
869-
assert result.history is not None and len(result.history) == 0
874+
assert result.history is not None and len(result.history) == 1
870875
assert result.status.state == TaskState.completed
871876

872877
# verify that history is still persisted to the store
873878
task = await task_store.get(result.id)
874879
assert task is not None
875-
assert task.history is not None and len(task.history) > 0
880+
assert task.history is not None and len(task.history) > 1
876881

877882

878883
@pytest.mark.asyncio
879-
async def test_on_task_get_limit_history():
884+
async def test_on_get_task_limit_history():
880885
task_store = InMemoryTaskStore()
881886
push_store = InMemoryPushNotificationConfigStore()
882887

@@ -892,7 +897,8 @@ async def test_on_task_get_limit_history():
892897
parts=[Part(root=TextPart(text='Hi'))],
893898
),
894899
configuration=MessageSendConfiguration(
895-
blocking=True, accepted_output_modes=['text/plain']
900+
blocking=True,
901+
accepted_output_modes=['text/plain'],
896902
),
897903
)
898904

@@ -904,14 +910,14 @@ async def test_on_task_get_limit_history():
904910
assert isinstance(result, Task)
905911

906912
get_task_result = await request_handler.on_get_task(
907-
TaskQueryParams(id=result.id, history_length=0),
913+
TaskQueryParams(id=result.id, history_length=1),
908914
create_server_call_context(),
909915
)
910916
assert get_task_result is not None
911917
assert isinstance(get_task_result, Task)
912918
assert (
913919
get_task_result.history is not None
914-
and len(get_task_result.history) == 0
920+
and len(get_task_result.history) == 1
915921
)
916922

917923

0 commit comments

Comments
 (0)