Skip to content

fix(portal): isolate concurrent A2A tasks#435

Open
LikiosSedo wants to merge 1 commit into
mainfrom
agent/fix-a2a-task-lifecycle
Open

fix(portal): isolate concurrent A2A tasks#435
LikiosSedo wants to merge 1 commit into
mainfrom
agent/fix-a2a-task-lifecycle

Conversation

@LikiosSedo

Copy link
Copy Markdown
Collaborator

Summary

Prevent overlapping A2A tasks in one context from consuming the same Runtime event stream, and make in-progress artifacts recoverable through polling and reconnects.

Problem

A2A contexts reuse one Runtime session, while task trackers filter chat.event only by session ID. Two non-terminal tasks in the same context could therefore consume the same deltas and terminal event, producing duplicated or mixed artifacts.

The task projection also persisted artifact text only at terminal events. A Portal restart could lose already-streamed output, and message_end could not repair missing or divergent deltas.

Solution

  • Add a nullable, database-backed active-context lease with a unique index. Admission is atomic across concurrent requests and Portal replicas; terminal transitions release the lease in the same SQL update.
  • Return 409 CONTEXT_BUSY for a second non-terminal task in the same (agent, API key, contextId). Sequential calls continue to reuse the same Runtime session.
  • Checkpoint partial artifacts on the first text update and then at a configurable interval (SICLAW_A2A_PROGRESS_FLUSH_MS, default 3s).
  • Reconcile each assistant message_end with streamed deltas and emit append: false when a replacement is required.
  • Await the final artifact write before terminal status emission and preserve partial output on cancellation/orphan failure.
  • Document the task/artifact ownership and streaming consumer contract.

This PR covers the Siclaw Portal A2A backend. Sicore's native Go A2A facade is a separate implementation and needs a separate parity change.

Test Plan

  • npm test — 232 files, 4537 passed, 2 skipped
  • npm run build
  • npx vitest run src/portal/a2a-gateway.test.ts src/portal/migrate-sqlite.test.ts
  • Concurrent and racing same-context submissions dispatch exactly one Runtime turn
  • Sequential same-context tasks reuse the Runtime session after terminal release
  • Partial and reconciled artifacts remain queryable from a2a_tasks
  • SQLite migration is additive and idempotent

A2A runtime events are session-scoped, so overlapping tasks in one context could consume the same stream and corrupt both artifacts. Add a database-backed active-context lease and release it atomically on terminal transitions.\n\nCheckpoint partial artifacts while working and reconcile complete assistant messages so polling, reconnecting, and streaming clients converge.\n\nConfidence: high\nScope-risk: moderate\nTested: npm test; npm run build; targeted A2A and SQLite migration tests
@LikiosSedo
LikiosSedo marked this pull request as ready for review July 22, 2026 08:17
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