Skip to content

[Bug]: A follow-up SendMessage can overwrite a task update the event bus just persisted (terminal state or artifact lost) #1388

Backlog

Description

@Shay-Deng

What happened?

When a client sends a follow-up message to a task while its agent is still running, DefaultRequestHandler.initMessageSend reads the task, appends the message, and saves the whole task outside the event bus:

  • authorizeTaskAccess reads a snapshot with taskStore.get (DefaultRequestHandler.java:1745) and rejects only a snapshot that is already terminal.
  • TaskManager.updateWithMessage copies status and history from that snapshot, adds the message, and calls taskStore.save with the full task (TaskManager.java:220-234).

If MainEventBusProcessor persists an event for the same task between that read and that save, the save writes the old snapshot back over it. I reproduced two outcomes on main bc7f9d7 with the real DefaultRequestHandler, InMemoryQueueManager, MainEventBus/MainEventBusProcessor and an InMemoryTaskStore subclass. The subclass holds the follow-up's read (asserted to be the one in authorizeTaskAccess) until the bus has persisted execution #1's next event, which makes the window deterministic:

  1. Execution Implement error types #1 completes in the window. The follow-up's save moves the task from TASK_STATE_COMPLETED back to TASK_STATE_WORKING. The terminal check passed because it ran on the stale snapshot.
  2. Execution Implement error types #1 adds an artifact in the window. The follow-up's save drops the artifact from the stored task.

Follow-ups while an execution is running are accepted today (testVersionStored_OnMessageSend_ExistingTask relies on it), so this only needs an agent that emits an event at the same moment a client sends a follow-up. The window is short with the in-memory store and two database round trips with JpaDatabaseTaskStore. onMessageSendStream goes through the same initMessageSend (DefaultRequestHandler.java:1064); I have only read that path, not run it.

Case 1 also conflicts with the spec: a task in a terminal state cannot accept further messages (§3.1.1, UnsupportedOperationError) and cannot restart (Life of a Task, "Task Immutability").

For comparison, a2a-js does the same read/append/save for a follow-up under the per-task write lock its event processing also takes (withTaskWriteLock in default_request_handler.ts), and a2a-go rejects a second execution while one is in progress and saves the history update with a version check against the read (ErrConcurrentModification).

Possible fixes, smallest first:

  • Run the read, the terminal check and the history save under the same per-task ordering the bus uses to persist events. The per-task lock proposed in fix(server-common): read and tap a subscribed task under the event bus lock #1371 (withEventProcessingPaused) would fit if that PR lands.
  • Send the history append through the bus as its own operation, re-reading the task when it is processed.
  • Reject a follow-up while an execution for the task is still running. This changes current behaviour, and runningAgents on its own is not enough, since non-blocking sends remove the entry before the agent finishes (DefaultRequestHandler.java:1023).

I have the reproduction as a JUnit test in server-common and am happy to open a PR with it and a fix, once you're fine with the direction.

Relevant log output

case A: follow-up must preserve the bus-persisted terminal state (or be rejected) ==> expected: <TASK_STATE_COMPLETED> but was: <TASK_STATE_WORKING>
case B: follow-up must preserve the bus-persisted artifact ==> expected: <true> but was: <false>

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions