Skip to content

fix: guard limitTaskHistory against negative historyLength - #1039

Open
ez-lbz wants to merge 1 commit into
a2aproject:mainfrom
ez-lbz:fix/limit-task-history-negative
Open

fix: guard limitTaskHistory against negative historyLength#1039
ez-lbz wants to merge 1 commit into
a2aproject:mainfrom
ez-lbz:fix/limit-task-history-negative

Conversation

@ez-lbz

@ez-lbz ez-lbz commented Aug 10, 2026

Copy link
Copy Markdown

What changed

1. Guard limitTaskHistory against negative historyLength

Problem: The guard in limitTaskHistory only checked historyLength >= task.history().size(), which does not catch negative values. A negative historyLength fell through to subList(size - historyLength, size) — i.e. subList(size + 1, size) — throwing IndexOutOfBoundsException. While TaskQueryParams rejects negative values at construction today, limitTaskHistory lacked defensive validation and would crash if a negative value ever reached it.

Fix (server-common/src/main/java/org/a2aproject/sdk/server/requesthandlers/DefaultRequestHandler.java):

  • Extended the early-return guard to historyLength < 0, so a negative value returns the task untouched instead of crashing.
  • Semantics are aligned with the Python SDK (apply_history_length): negative → history untouched; 0 → empty history; N >= history size → no limiting. The existing 0 → empty history behavior is preserved (unlike a <= 0 guard, which would have broken it).

Fix (server-common/src/test/java/org/a2aproject/sdk/server/requesthandlers/DefaultRequestHandlerTest.java):

  • testLimitTaskHistoryNegativeHistoryLengthReturnsTaskUnchanged — invokes the private limitTaskHistory via reflection with -1 and asserts no exception is thrown and the task is returned unchanged.
  • testOnGetTaskHistoryLengthLimitsHistoryhistoryLength=2 returns only the 2 most recent messages.
  • testOnGetTaskHistoryLengthZeroReturnsEmptyHistory — locks in that historyLength=0 still returns an empty history.

Behavior change: none for valid inputs (>= 0). Negative historyLength previously crashed; it now returns the task unchanged.

Testing

  • mvn -pl server-common test450 tests run, 0 failures, 0 errors, 0 skipped (BUILD SUCCESS), including the 3 new regression tests.

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.

1 participant