Skip to content

Commit 0d5473c

Browse files
AndreyGatsukGatsuk Andreymykytanetipa
authored
fix: compare in-memory task timestamps numerically (a2aproject#1233)
# Description Fix timestamp filtering and descending ordering in `InMemoryTaskStore.list()`. Comparing `ToJsonString()` values lexicographically reverses chronological order when fractional-second precision differs. For example, `2025-01-01T00:00:00.123Z` incorrectly passes a filter whose threshold is `2025-01-01T00:00:00.123000001Z`, despite being one nanosecond earlier. The same comparison also produces incorrectly ordered pages. Compare integer nanoseconds using `ToNanoseconds()` for both filtering and sorting. Preserve the inclusive filter boundary, descending ID tie-breaker, and placement of tasks without timestamps after timestamped tasks. [A2A specification §5.6.1, Timestamps](https://a2a-protocol.org/latest/specification/#561-timestamps) states: “Millisecond precision SHOULD be used where available”. It also specifies `google.protobuf.Timestamp`, whose representation contains seconds and nanoseconds, and permits omitted fractional seconds. Numeric comparison handles these precisions without discarding sub-millisecond information. Add 15 regression cases covering seconds, milliseconds, microseconds, nanoseconds, equal timestamps, second boundaries, pagination, ID tie-breaking, absent timestamps/status, and timestamps before/at the Unix epoch. Before the fix, 11 cases fail and four pass; after the fix, all 35 in-memory task store tests pass. Validation on Python 3.10 with PostgreSQL 15 and MySQL 8: - `./scripts/lint.sh`: Ruff check/format and ty passed (three existing unused-ignore warnings outside this change). - `uv run pytest`: 2,092 passed, 3 xfailed, 1 xpassed. The xfail/xpass cases are already marked for a2aproject#869. - `uv run pytest --cov=src --cov-report=term-missing --cov-fail-under=88`: same test results, 93.17% coverage. - JSCPD: 2.61% duplication, below the 3% threshold. - Built the wheel and ran `tests.install_smoke` in six clean environments: base, http-server, fastapi, grpc, telemetry, and sql all passed. Local transport checks used proxy exclusions for loopback addresses. Cross-version `uv --with` environments were prepared before the full runs to keep dependency downloads outside server-startup timeouts. - [x] Follow the CONTRIBUTING guide: feature branch based on current upstream main. - [x] Conventional Commit title: `fix: compare in-memory task timestamps numerically`. - [x] Tests and linters pass. - [x] Protocol reference included above; no public API changes. Co-authored-by: Gatsuk Andrey <GatsukAS@zdrav.mos.ru> Co-authored-by: mykytanetipa <mykytanetipa@google.com>
1 parent f3ac824 commit 0d5473c

2 files changed

Lines changed: 91 additions & 6 deletions

File tree

‎src/a2a/server/tasks/inmemory_task_store.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ async def list(
9393
task for task in tasks if task.status.state == params.status
9494
]
9595
if params.HasField('status_timestamp_after'):
96-
last_updated_after_iso = (
97-
params.status_timestamp_after.ToJsonString()
96+
last_updated_after_ns = (
97+
params.status_timestamp_after.ToNanoseconds()
9898
)
9999
tasks = [
100100
task
101101
for task in tasks
102102
if (
103103
task.HasField('status')
104104
and task.status.HasField('timestamp')
105-
and task.status.timestamp.ToJsonString()
106-
>= last_updated_after_iso
105+
and task.status.timestamp.ToNanoseconds()
106+
>= last_updated_after_ns
107107
)
108108
]
109109

@@ -113,9 +113,9 @@ async def list(
113113
task.status.HasField('timestamp')
114114
if task.HasField('status')
115115
else False,
116-
task.status.timestamp.ToJsonString()
116+
task.status.timestamp.ToNanoseconds()
117117
if task.HasField('status') and task.status.HasField('timestamp')
118-
else '',
118+
else 0,
119119
task.id,
120120
),
121121
reverse=True,

‎tests/server/tasks/test_inmemory_task_store.py‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,91 @@ async def test_list_tasks_fails(
257257
await store.delete(task.id, TEST_CONTEXT)
258258

259259

260+
@pytest.mark.asyncio
261+
@pytest.mark.parametrize(
262+
'task_time, threshold, included',
263+
[
264+
('00Z', '00.001Z', False),
265+
('00.001Z', '00Z', True),
266+
('00.123Z', '00.123001Z', False),
267+
('00.123001Z', '00.123Z', True),
268+
('00.123Z', '00.123000001Z', False),
269+
('00.123000001Z', '00.123Z', True),
270+
('00.123001Z', '00.123001001Z', False),
271+
('00.123001001Z', '00.123001Z', True),
272+
('00.123Z', '00.123Z', True),
273+
('00.123000001Z', '00.123000001Z', True),
274+
('00.999999999Z', '01Z', False),
275+
('01Z', '00.999999999Z', True),
276+
],
277+
)
278+
async def test_list_tasks_timestamp_filter_precision(
279+
task_time: str, threshold: str, included: bool
280+
) -> None:
281+
"""Compare timestamp values across JSON fractional-second precisions."""
282+
store = InMemoryTaskStore()
283+
task = create_minimal_task()
284+
task.status.timestamp.FromJsonString(f'2025-01-01T00:00:{task_time}')
285+
await store.save(task, TEST_CONTEXT)
286+
await store.save(create_minimal_task('no-timestamp'), TEST_CONTEXT)
287+
await store.save(Task(id='no-status'), TEST_CONTEXT)
288+
params = ListTasksRequest()
289+
params.status_timestamp_after.FromJsonString(
290+
f'2025-01-01T00:00:{threshold}'
291+
)
292+
293+
page = await store.list(params, TEST_CONTEXT)
294+
295+
assert [result.id for result in page.tasks] == (
296+
[task.id] if included else []
297+
)
298+
assert page.total_size == int(included)
299+
300+
301+
@pytest.mark.asyncio
302+
@pytest.mark.parametrize('page_size', [1, 3, 20])
303+
async def test_list_tasks_timestamp_ordering_and_pagination(
304+
page_size: int,
305+
) -> None:
306+
"""Keep chronological order and ID tie-breaking across page boundaries."""
307+
store = InMemoryTaskStore()
308+
# Chronological order, including timestamps before and at the Unix epoch.
309+
timestamps = [
310+
'1969-12-31T23:59:59.999999999Z',
311+
'1970-01-01T00:00:00Z',
312+
'2025-01-01T00:00:00Z',
313+
'2025-01-01T00:00:00.123Z',
314+
'2025-01-01T00:00:00.123000001Z',
315+
'2025-01-01T00:00:00.123001Z',
316+
'2025-01-01T00:00:00.123001001Z',
317+
'2025-01-01T00:00:01Z',
318+
'2025-01-01T00:00:01Z',
319+
]
320+
for index, timestamp in enumerate(timestamps):
321+
task = create_minimal_task(f'task-{index}')
322+
task.status.timestamp.FromJsonString(timestamp)
323+
await store.save(task, TEST_CONTEXT)
324+
await store.save(create_minimal_task('no-timestamp'), TEST_CONTEXT)
325+
await store.save(Task(id='no-status'), TEST_CONTEXT)
326+
expected_ids = [
327+
*(f'task-{index}' for index in reversed(range(len(timestamps)))),
328+
'no-timestamp',
329+
'no-status',
330+
]
331+
params = ListTasksRequest(page_size=page_size)
332+
333+
for start in range(0, len(expected_ids), page_size):
334+
page = await store.list(params, TEST_CONTEXT)
335+
assert [task.id for task in page.tasks] == expected_ids[
336+
start : start + page_size
337+
]
338+
assert page.total_size == len(expected_ids)
339+
assert bool(page.next_page_token) == (
340+
start + page_size < len(expected_ids)
341+
)
342+
params.page_token = page.next_page_token
343+
344+
260345
@pytest.mark.asyncio
261346
async def test_in_memory_task_store_delete() -> None:
262347
"""Test deleting a task from the store."""

0 commit comments

Comments
 (0)