fix: return TaskNotFoundError when SendMessage references non-existent taskId by yyy9942 · Pull Request #788 · a2aproject/a2a-java · GitHub
Skip to content

fix: return TaskNotFoundError when SendMessage references non-existent taskId - #788

Merged
ehsavoie merged 1 commit into
a2aproject:mainfrom
yyy9942:issue-766
Apr 15, 2026
Merged

ehsavoie merged 1 commit into
a2aproject:mainfrom
yyy9942:issue-766

Conversation

@yyy9942

@yyy9942 yyy9942 commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

Per A2A spec section 3.4.2, when a client includes a taskId in a Message, it MUST reference an existing task. The SDK was creating a new task with that id instead.

Added a guard in DefaultRequestHandler.initMessageSend() that throws TaskNotFoundError when the provided taskId does not reference an existing task.

This fixes the issue on all three transports (JSON-RPC, gRPC, HTTP+JSON) since they all use the same DefaultRequestHandler code path.

Also fixes a related contextId bug: when a follow-up message had a taskId but no contextId, RequestContext.Builder.build() was generating a fresh UUID, so the agent executor saw the wrong contextId. The rebuild now seeds contextId from the stored task.

Existing tests that relied on the old taskId behavior have been updated.

Fixes #766

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

Copy link
Copy Markdown
Contributor

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 enforces A2A spec section 3.4.2 by prohibiting client-provided taskId values for new task creation. The DefaultRequestHandler now validates that any provided taskId references an existing task, throwing a TaskNotFoundError otherwise. It also ensures that follow-up messages maintain the original contextId of a task. Correspondingly, integration and unit tests across multiple modules have been updated to rely on server-generated IDs and content-based routing for test scenarios. I have no feedback to provide.

@yyy9942

yyy9942 commented Apr 13, 2026

Copy link
Copy Markdown
Contributor Author

// Per A2A spec section 3.4.2: when a client includes a taskId in a
// Message, it MUST reference an existing task. Client-provided taskId
// for creating new tasks is not supported.
if (task == null && params.message().taskId() != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I find this check a bit convoluted.
It can be simplified by verifying at the start of the method that if params.message().taskId() is not null, there is a task returned by taskStore.get(params.message().taskId()).
After that, the request context can be safely created from either a correct message's taskId or an autogenerated taskId.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@jmesnil Simplified the check as suggested. Extracted the taskId validation into validateRequestedTask() at the top of the method - if params.message().taskId() is non-null, it verifies the task exists, validates contextId consistency, and rejects terminal-state tasks upfront. After that, initMessageSend() can safely work with either a validated existing task or proceed with auto-generated identifiers for new tasks.

Also pulled out normalizeRequestParamsForTask() to align message params with the task's canonical IDs before building the RequestContext, and merged the two consecutive if (task != null) blocks into one.

@yyy9942
yyy9942 force-pushed the issue-766 branch 2 times, most recently from da31489 to 668792f Compare April 13, 2026 11:42
…t taskId

Per A2A spec section 3.4.2, when a client includes a taskId in a Message,
it MUST reference an existing task. Previously, the SDK silently created a
new task using the client-provided taskId, which the spec explicitly
forbids.

Applies to all three transports (JSON-RPC, gRPC, HTTP+JSON) via the common
DefaultRequestHandler.initMessageSend() code path.

Existing tests that relied on the buggy behaviour have been updated to
either pre-populate the task store (when testing follow-up messages) or
omit the client-provided taskId on the initial message (letting the
server generate it, matching the spec-correct flow).

This fixes a2aproject#766
@ehsavoie

Copy link
Copy Markdown
Collaborator

/gemini review

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

Copy link
Copy Markdown
Contributor

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 refactors task creation and message handling to strictly enforce server-side task ID generation, aligning with the A2A specification (CORE-MULTI-004). Key changes include the introduction of task validation in DefaultRequestHandler to reject client-provided IDs for new tasks and the update of various integration tests to handle server-generated IDs. I have no feedback to provide.

@ehsavoie
ehsavoie merged commit 786e5fd into a2aproject:main Apr 15, 2026
11 checks passed
@ehsavoie

Copy link
Copy Markdown
Collaborator

salaboy pushed a commit to salaboy/a2a-java that referenced this pull request Jun 19, 2026
…t taskId (a2aproject#788)

Per A2A spec section 3.4.2, when a client includes a taskId in a
Message, it MUST reference an existing task. The SDK was creating a new
task with that id instead.

Added a guard in DefaultRequestHandler.initMessageSend() that throws
TaskNotFoundError when the provided taskId does not reference an
existing task.

This fixes the issue on all three transports (JSON-RPC, gRPC, HTTP+JSON)
since they all use the same DefaultRequestHandler code path.

Also fixes a related contextId bug: when a follow-up message had a
taskId but no contextId, RequestContext.Builder.build() was generating a
fresh UUID, so the agent executor saw the wrong contextId. The rebuild
now seeds contextId from the stored task.

Existing tests that relied on the old taskId behavior have been updated.

Fixes a2aproject#766
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]: SendMessage with unknown taskId creates a new task instead of returning TaskNotFoundError

3 participants