Skip to content

Commit bd3b240

Browse files
committed
fix: add history length and page size validations
1 parent 1d328e1 commit bd3b240

3 files changed

Lines changed: 140 additions & 43 deletions

File tree

src/a2a/server/request_handlers/default_request_handler.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@
5252
TaskNotFoundError,
5353
UnsupportedOperationError,
5454
)
55-
from a2a.utils.task import apply_history_length
55+
from a2a.utils.task import (
56+
apply_history_length,
57+
validate_history_length,
58+
validate_page_size,
59+
)
5660
from a2a.utils.telemetry import SpanKind, trace_class
5761

5862

@@ -122,6 +126,8 @@ async def on_get_task(
122126
context: ServerCallContext | None = None,
123127
) -> Task | None:
124128
"""Default handler for 'tasks/get'."""
129+
validate_history_length(params)
130+
125131
task_id = params.id
126132
task: Task | None = await self.task_store.get(task_id, context)
127133
if not task:
@@ -135,6 +141,10 @@ async def on_list_tasks(
135141
context: ServerCallContext | None = None,
136142
) -> ListTasksResponse:
137143
"""Default handler for 'tasks/list'."""
144+
validate_history_length(params)
145+
if params.HasField('page_size'):
146+
validate_page_size(params.page_size)
147+
138148
page = await self.task_store.list(params, context)
139149
for task in page.tasks:
140150
if not params.include_artifacts:
@@ -327,6 +337,8 @@ async def on_message_send(
327337
Starts the agent execution for the message and waits for the final
328338
result (Task or Message).
329339
"""
340+
validate_history_length(params.configuration)
341+
330342
(
331343
_task_manager,
332344
task_id,

src/a2a/utils/task.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
TaskState,
1414
TaskStatus,
1515
)
16+
from a2a.utils.errors import InvalidParamsError, ServerError
1617

1718

1819
def new_task(request: Message) -> Task:
@@ -96,6 +97,16 @@ def HasField(self, field_name: Literal['history_length']) -> bool: # noqa: N802
9697
...
9798

9899

100+
def validate_history_length(config: HistoryLengthConfig | None) -> None:
101+
"""Validates that history_length is non-negative."""
102+
if config.history_length < 0:
103+
raise ServerError(
104+
error=InvalidParamsError(
105+
message='history length must be non-negative'
106+
)
107+
)
108+
109+
99110
def apply_history_length(
100111
task: Task, config: HistoryLengthConfig | None
101112
) -> Task:
@@ -136,6 +147,22 @@ def apply_history_length(
136147
return task
137148

138149

150+
def validate_page_size(page_size: int) -> None:
151+
"""Validates that page_size is in range [1, 100].
152+
153+
See Also:
154+
https://a2a-protocol.org/latest/specification/#314-list-tasks
155+
"""
156+
if page_size < 1:
157+
raise ServerError(
158+
error=InvalidParamsError(message='minimum page size is 1')
159+
)
160+
if page_size > 100:
161+
raise ServerError(
162+
error=InvalidParamsError(message='maximum page size is 100')
163+
)
164+
165+
139166
_ENCODING = 'utf-8'
140167

141168

0 commit comments

Comments
 (0)