Skip to content

fix(prompt_agent): a re-run closes the conversations a dead run of the step left active - #937

Open
psinghal20 wants to merge 4 commits into
mainfrom
pratyush/close-leaked-conversations
Open

psinghal20 wants to merge 4 commits into
mainfrom
pratyush/close-leaked-conversations

Conversation

@psinghal20

@psinghal20 psinghal20 commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #951, which closes a prompt step's conversation however the step ends and reopens it on a re-run with the same conversation id.

A step without context_id mints a new conversation id on every run. When its worker dies mid-step, nothing in that process closes the conversation, and the re-run's reopen never finds it, so it stays active and pending in the hub worklist.

The record now carries the step_id that created it, and the step closes the active conversations an earlier run of it left for the same task instance (close_active_for_step) at the start of _execute, before anything reaches the agent, so a re-run that fails early still closes them. A step with context_id shares its conversation across runs, gets no step_id and is left to #951's reopen. Records written before this have no step_id and are never swept.

Not covered: a worker that dies with no later run of the step, and cancelling the earlier agent turn. An earlier run that is stuck but alive when the step is re-run has its conversation closed, and #951's guard refuses its next turn.

Validation: 3 step tests (re-run closes the leftover, a re-run failing before its own conversation still closes it, context_id not swept), the first two failing with the sweep disabled; 2 store tests; with #951's 11 conversation tests, 16 pass; ruff check clean. The tst/unit/task_step, a2a_agent and store suites pass except 6 failures in computer_exec_test.py and test_mcp_cli_builder.py that also fail on main.

Once #951 lands, this branch is rebased onto main.

🤖 Generated with Claude Code

Confidence Score: 5/5 · The PR appears safe to merge; no new actionable issues remain.RetriggerView in GreptileConfidence Score: 5/5

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

Summary

Adds step_id to conversations owned by prompt_agent and closes active leftovers before a new attempt contacts the agent.

  • A prompt_agent rerun closes active conversations left by the same step.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Start prompt step] --> B{Fixed context_id?}
  B -->|No| C[Close active leftovers for this task instance and step]
  B -->|Yes| D[Keep shared conversation behavior]
  C --> E[Configure agent and start conversation]
  D --> E
Loading

Reviews (3) · Last reviewed commit: "fix(prompt_agent): a re-run closes the c..." · Reviewed by Greptile

@psinghal20
psinghal20 requested a review from a team as a code owner November 27, 2026 15:25
@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 a newer open PR: #951 (fix(prompt_agent): close the A2A conversation however the step ends) fixes the same leaked-active conversation in PromptAgentTaskStep. It moves the create_conversation(...) call that this PR adds step_id= to (prompt_agent.py:679 here) into a new reopen-then-create block (#951 prompt_agent.py:706-717). If either PR rebases over the other and that hunk loses step_id, close_active_for_step (conversation_store.py:362 here) matches nothing, and no test fails. #951 also closes in a finally, which overlaps this PR's except BaseException → _close_left_open. Details are on #951. You may want to coordinate on which one lands first.


Generated by Claude Code

andrewpark-scaleai and others added 4 commits November 29, 2026 06:38
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.
…e step left active

A step without context_id mints a new conversation id on every run, so when
its worker dies and the step is re-run, the earlier conversation stays active
and reopen never finds it. The record now carries the step_id that created it,
and the step closes the active conversations an earlier run of it left for the
same task instance before anything reaches the agent (close_active_for_step).
A step with context_id shares its conversation across runs and is left to
reopen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@psinghal20
psinghal20 force-pushed the pratyush/close-leaked-conversations branch from 73e19d8 to cbc10d5 Compare November 30, 2026 13:16
@psinghal20
psinghal20 changed the base branch from main to andrew-park/a2a-conversation-close November 30, 2026 13:16
@psinghal20 psinghal20 changed the title fix(task-step): prompt_agent closes the conversation a failed, cancelled or dead run leaves active fix(prompt_agent): a re-run closes the conversations a dead run of the step left active Nov 30, 2026
@kaihackney-scale

Copy link
Copy Markdown
Collaborator

close_active_for_step matches only on instance, step and active, so a superseded attempt that reaches this step late closes the live attempt's conversation. A local probe showed the live run's in-flight task canceled and its later turn refused. Could the record carry the attempt epoch (or creation time) so the sweep closes only conversations from earlier attempts? Also, should the sweep's store errors be logged and swallowed, as #951 does for its close, rather than failing the re-run before it starts?

@andrewpark-scaleai
andrewpark-scaleai force-pushed the andrew-park/a2a-conversation-close branch from 66f45fa to fb1f009 Compare December 2, 2026 20:30
Base automatically changed from andrew-park/a2a-conversation-close to main December 2, 2026 21:51

This branch has not been deployed

No deployments
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