Skip to content

fix(prompt_agent): close the A2A conversation however the step ends - #951

Merged
andrewpark-scaleai merged 3 commits into
mainfrom
andrew-park/a2a-conversation-close
Dec 2, 2026
Merged

andrewpark-scaleai merged 3 commits into
mainfrom
andrew-park/a2a-conversation-close

Conversation

@andrewpark-scaleai

@andrewpark-scaleai andrewpark-scaleai commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Why

A prompt_agent step could leave its A2A conversation open forever. The step closed the conversation only on its normal path, so a timeout, cancel, or exception left it active, and pending if a turn was in flight. Nothing else closes it, so list_pending kept listing it. Two related writes were also wrong: add_a2a_task appended to a closed conversation, and complete_a2a_task recorded a failed agent turn as completed.

What changed

  • The step closes its conversation in a finally. A store error during that close is logged and does not hide why the step failed.
  • add_a2a_task returns None unless the conversation is still active. It checks under the same rev compare-and-swap as the write. The step then stops without sending. A refusal before the first turn fails the step instead of recording an empty answer.
  • complete_a2a_task(state=...) records the agent task's real end state.
  • A re-run calls a new reopen, which cancels tasks an earlier run left working, before it creates the conversation. If another run creates it first, the step reopens and continues.
  • A TLA+ model of the step, the store writers, and a peer lives in tst/formal/a2a_conversation/, with AGENTS.md and a check.sh that pins TLC.

Scope

  • The model covers steps that set context_id. Only those steps keep one conversation id across the runs of an instance, so only their re-run reopens a killed run's conversation. A step without context_id gets a new id on each run, and the killed run's record stays active; fix(prompt_agent): a re-run closes the conversations a dead run of the step left active #937 closes it when the step runs again.
  • Known gap, not new: reopen cancels working tasks, and complete_a2a_task ends the oldest one, without checking which attempt owns it. A superseded attempt that is still running, or a sibling step that shares the context_id, can end the other's task. Fencing it needs an attempt id on each A2A task, so it is left for a follow-up.
  • Known gap, not new, kept as an expected-break config: if an earlier run dies while its question to a human peer is still in flight, and the peer's endpoint records it after the retry reopens, the retry's reply is stored against the old question. The fix would end a task by its id instead of taking the oldest working one. That changes the store API.
  • A process outside this repo that calls add_a2a_task on a closed conversation now gets None. The docstring already allowed None.

Blast Radius

always_on ignores the return of add_a2a_task and closes its own conversation last, so it behaves as before. On a retry, the step now shares one conversation record with the earlier attempt on every store. Before, stores without the unique index got a second record.

Verification

  • tst/unit/task_step/test_prompt_agent_conversation_close.py: 8 of its 10 tests fail on main (for example ('active', True) != ('closed', False)), and all 10 pass here. tst/unit/task_step and tst/unit/a2a_agent give 4998 passed. make lint passes.
  • tst/formal/a2a_conversation/check.sh exits 0 with 66 checks as expected. The fixed config holds all six rules (39,211 states). Each guard has a mutant config that breaks its rule.

Confidence Score: 5/5RetriggerView in Greptile

The PR appears safe to merge; no new actionable issues were found.

Summary

prompt_agent now closes its conversation in finally, refuses turns after closure, and records each agent task’s actual end state. Retries reopen the existing conversation and cancel tasks left working.

  • Prompt-agent conversations close when the step exits.
  • Closed conversations reject new turns before they are sent.
  • Conversation tasks record the agent’s actual terminal state.
  • Retries reuse their conversation and clear tasks left working.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Finish setup] --> B{Existing conversation?}
  B -->|Yes| C[Reopen and cancel leftover tasks]
  B -->|No| D[Create conversation]
  C --> E{Record turn accepted?}
  D --> E
  E -->|Yes| F[Send message and wait]
  E -->|No, first turn| G[Fail step]
  E -->|No, later turn| H[End loop]
  F --> I[Record actual end state]
  I --> J[Continue or end loop]
  F -->|Timeout, cancellation, or error| K[Finally: close conversation]
  G --> K
  H --> K
  J --> K
  K --> L[End live trajectory]
Loading

Reviews (7) · Last reviewed commit: "chore: model-check a prompt step's A2A c..." · Reviewed by Greptile

@andrewpark-scaleai
andrewpark-scaleai requested a review from a team as a code owner November 29, 2026 05:48
@andrewpark-scaleai
andrewpark-scaleai force-pushed the andrew-park/a2a-conversation-close branch 2 times, most recently from 002b4a8 to efb6747 Compare November 29, 2026 05:57
@earakely-scale

Copy link
Copy Markdown
Collaborator

🤖 Automated Claude routine — Env Pod PR Context Review. This is a bot, not Edgar Arakelyan; not a human review.

Context from another open PR: #937 (fix(task-step): prompt_agent closes the conversation a failed, cancelled or dead run leaves active) fixes the same leaked-active conversation in the same function, and the two PRs overlap in ways neither description mentions:

Probably simplest to decide whether #937 folds into this PR or lands first.


Generated by Claude Code

@kaihackney-scale

Copy link
Copy Markdown
Collaborator

The close in finally looks right: it runs before any await and survives cancellation, and a failed close is only logged. I confirmed the conversation ends closed when CancelledError lands mid-turn. One question on the TLA+ model: StepKilled has the re-run reopen the same conversation, but without context_id each run mints a new id, so a killed run's record stays active. Could the model state that it covers only steps that set context_id? Separately, reopen cancels working tasks without checking which attempt owns them. A superseded attempt that is still alive, or a sibling step sharing the context_id, can cancel or complete the other's task and close the conversation under it. Is fencing this by attempt in scope?

A step that raises mid-turn leaves the conversation active and pending, a
failed agent turn is recorded completed, a closed conversation still takes a
new task, and a retried step's reply completes the dead attempt's turn.

A conversation closed before the first turn must fail the step without
sending anything. A retry after a run killed before its close must cancel the
task that run left and complete its own. Two runs racing to create the
conversation must share one record, and a close that fails in the step's
cleanup must not replace the step's own exception.
The step closed its conversation only after the loop returned normally, so a
timeout, cancellation or any other exception left it active, and pending when
the turn was recorded, for list_pending to keep listing. The close now runs in
the loop's finally, and the conversation is created just before that try. A
store error in that close is logged, so it never replaces the step's own
exception.

add_a2a_task appended to a closed conversation, so a turn recorded after a
peer closed it, or a peer recording a message after the step closed it, left a
working task no one would end. It now refuses unless the conversation is
active, read under the same rev swap, and returns None. The step then stops
without sending the turn. A refusal of the first turn fails the step, since
the agent was sent nothing and an empty answer must not pass as one.

complete_a2a_task recorded every reply as completed. It takes the state the
responder's task ended in, and the step passes the agent's.

A re-run of the step under the same instance id (a retry, or a later step
continuing the same context_id) found its conversation through
create_conversation raising DuplicateKeyError. Only the unique index that
conversation_store.ensure_indexes creates raises it, and nothing in this
repository calls ensure_indexes, so on a store without the index a re-run
inserted a second document with the same conversation_id. The step now calls
reopen first, which reactivates the conversation and cancels any task an
earlier run left working, and creates it only when reopen finds none. If two
runs race to create it, the loser's DuplicateKeyError leads to reopen too.

Tests that stub the store return {} from add_a2a_task, a recorded turn, and
stub reopen with the other calls. The user-side test that pinned a failed turn
as completed now expects failed.
tst/formal/a2a_conversation holds a TLA+ model of a prompt_agent step's A2A
conversation record, the step that owns it, a retry of the step, a process
that dies mid-step, and a human peer's endpoint writing the same store.
check.sh runs the fixed code, the code before the fix, six mutants and one
known gap against six invariants, with the TLC jar pinned by sha256.
AGENTS.md says which code the model covers, when to run it and how to read a
failure.

The known gap: an earlier run's ask that the peer's endpoint records after a
retry reopened the conversation sits ahead of the retry's turn, and the
retry's reply ends it, because complete_a2a_task ends the oldest working task.
@andrewpark-scaleai
andrewpark-scaleai force-pushed the andrew-park/a2a-conversation-close branch from 66f45fa to fb1f009 Compare December 2, 2026 20:30
@andrewpark-scaleai

Copy link
Copy Markdown
Contributor Author

Thanks for the probes.

On StepKilled: you're right. A step without context_id gets a new conversation id on each run, so only steps that set it reopen the same record. The model's header and AGENTS.md now say it covers only steps that set context_id. For steps without it, the killed run's record is closed by #937, which closes a step's leftover records when the step runs again.

On fencing reopen by attempt: I'm keeping it out of this PR. It needs an attempt id on each A2A task, so reopen and complete_a2a_task can tell an earlier attempt's task from a live one, and #937 needs the same field for its sweep. It's listed in AGENTS.md under what the model leaves out, as a known gap in the code. Happy to do it as a follow-up that adds the attempt id and models two live attempts.

@psinghal20 psinghal20 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good, I don't have much context on the TLA+ changes, but they are not part of the CI so should be good.

@andrewpark-scaleai
andrewpark-scaleai merged commit bbe557e into main Dec 2, 2026
36 of 37 checks passed
@andrewpark-scaleai
andrewpark-scaleai deleted the andrew-park/a2a-conversation-close branch December 2, 2026 21:51
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.

4 participants