Skip to content

fix(wake): a wake turn knows the conversation that delegated and replies into it (#4537) - #4606

Merged
vybe merged 6 commits into
devfrom
fix/4537-wake-reply-reaches-chat
Nov 24, 2026
Merged

vybe merged 6 commits into
devfrom
fix/4537-wake-reply-reaches-chat

Conversation

@dolho

@dolho dolho commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A wake turn was cut off from the conversation that caused it, at both ends (#4537, found in v1.0.5 testing, row A5):

  • The agent had lost the plan. The wake frame carried only the callee's ending. It now also quotes the delegating turn: what the agent was asked, and what it replied when it delegated.
  • The person never saw the reply. The wake's reply stayed on the execution row. When the delegation came from a person's turn in an agent chat thread, the reply is now written into that thread as the agent's next message, and an open Chat tab shows it without a reload.

How

  • caller_wake_service._delegating_turns follows continues_execution_id (already written by ent#1225) from the callee row. The first row is the turn that delegated (its reply is the plan). The chain is followed through platform continuations to the turn at its head, whose message is the request, so round two of a delegate→wake loop still reads the person's request. At most 8 hops, and every row must be the delegator's own. Text is sanitized and capped from the end.
  • _post_wake_reply runs at the wake row's own terminal (the hook _drain_missed already uses, reached by every terminal writer). SUCCESS only, once per wake (idempotency key caller_wake_reply:{wake_id}, released if the write fails).
  • The thread is written only when the head turn is a person's turn (source_user_id, no source_agent_name) and the session is that person's thread of that agent.
  • chat_persistence_service.persist_chat_session now names the thread on the row (chat_session_id). /chat already did (bug: a watchdog-recovered chat turn never writes its assistant message — row says SUCCESS, thread shows nothing #3380); the Chat tab sends /task, which did not.
  • The reply is announced with the existing thin chat_response_ready trigger (identifiers only). ChatPanel re-reads the session it shows, in place.
    • A reply that lands while a turn is in flight is read when that turn ends (a turn's end does not refetch messages by itself).
    • Whether anything is new is decided by identity, not length (utils/chatSessionMerge.js): a stored row by its id, a row the panel pushed itself by role and text. After a failed turn the panel holds a message the server never saved; the reply is still shown, after it.

No schema change: both columns exist.

Not changed

  • Workspace, Slack, Telegram. A delegated run already reports into those through its inherited channel context (channel_completion_report). The wake's own reply is not posted there. Stamping a destination on the wake changes who the turn is for (feat(workspace): Discuss and Dismiss on asks (abilityai/trinity-enterprise#747, #748) #3181 review F1), so that is left for its own change.
  • A wake that ends before the delegating /task turn is saved posts nothing: the thread is named on the row only once that turn's own messages are written.
  • A callee row with no link (the ent#1216 fallback) gets the old frame and no post.
  • A failed wake posts nothing; it stays on the execution row.

Test Plan

  • cd tests && pytest unit/test_4537_wake_conversation.py — 17 passed
  • Neighbouring wake and chat-persistence suites (test_4536_wake_window, test_ent1102_caller_wake, test_4357_wake_chain_depth, test_ent1216_wake_turn_reads, test_ent1225_continuation_tier, test_1444_chat_session_persistence, test_2329_pull_delivery_parity, and others) — 393 passed
  • Frontend: chatPanelWakeReply4537.mount.spec.js — 16 passed. The real ChatPanel, driven from the chat_response_ready frame on /ws through the store to the re-read. Nine mutations (handler, store, watcher, mid-turn wait, length compare, other-session guard, id, local-row matching) each turn a test red
  • Frontend: raw-color and loading-gate ratchet specs pass; neighbouring ChatPanel specs pass
  • Live, on an isolated stack from this branch (PostgreSQL, Redis, no agent containers): the real wake hook produced the frame with the request and the plan; the wake's terminal, written through the real agent result callback (POST /api/agents/{name}/executions/{id}/result), put the reply in the thread once (a repeated callback added nothing) and one chat_response_ready event arrived on /ws
  • Not run: a real two-agent delegate→wake round with model turns, the Chat tab refresh in a real browser, and the full frontend suite locally (left to CI). The live stack had no agent containers, so the wake's dispatch to a container was stubbed and the thread was read from the database.

Scope

This is part of #4537, so the issue stays open. Not in this PR:

  • wakes on an answered ask (operator_response / operator_ending);
  • delegations that started in the Workspace (see Not changed).

Journey Impact

Journey Impact: none: no journey in tests/journeys/catalog.yaml covers agent-to-agent delegation or the operator Chat tab

Related to #4537

🤖 Generated with Claude Code

…ies into it (#4537)

A person asks an agent in chat to "ask X and tell me what it said". The
agent delegates asynchronously and replies that it will report back. The
wake turn then ran cut off from that conversation at both ends: its frame
carried only the callee's ending, so the agent had lost the request and
its own plan, and its reply stayed on the execution row, so the person
never saw it.

The wake frame now quotes the delegating turn, read off the link the
callee row already records (continues_execution_id): what the agent was
asked and what it replied then. A wake that delegates again links the
next callee to the wake itself, so the request is read from the head of
that chain and the plan from the turn that delegated last. Every row on
the way must be the delegator's own; the text is sanitized and capped.

When the head of the chain is a person's turn in an agent chat thread,
the wake's reply is written into that thread as the agent's next message.
It is written at the wake row's own terminal, which every terminal writer
reaches, once per wake, on SUCCESS only, and announced with the existing
thin chat_response_ready trigger. The open ChatPanel refetches its
session in place on that trigger.

The Chat tab sends /task, which never named its thread on the row, so
persist_chat_session now does, after its own messages are saved.

Workspace, Slack and Telegram conversations are unchanged: a delegated
run already reports into those through its inherited channel context.

Fixes #4537

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@dolho dolho added the ui PR touches the frontend UI — triggers Playwright e2e tests label Nov 16, 2026
@dolho
dolho requested a review from obasilakis November 16, 2026 13:26
)

The new chat writer copied the persisted row's response without calling the
runtime secret-scrub seam, which the ent#279 parity guard requires of every
message-table writer.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@vybe

vybe commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

merge-train (2026-11-17): not on this train — the frontend half needs work. Validated at 7552b42f8.

The backend half is strong: the 17 tests drive maybe_wake_caller / persist_chat_session against a real DB, eight mutations each turn a test red, and chat_response_ready stays an identifier-only trigger.

Findings, all in the frontend half:

  1. No executing test. Nothing covers ChatPanel.vue:851-876, stores/agents.js:343 or utils/websocket.js:317; deleting the noteChatReply call or the watcher leaves every test green. One jsdom mount spec would pin it.
  2. A wake reply that lands mid-turn is dropped (from /review, by reading — not reproduced). ChatPanel.vue:856 returns while loading is true, and the comment at :854 says a turn in flight "reloads on its own when it ends". It does not: turn end pushes the reply locally and calls loadSessions(false), which never refetches messages.
  3. The "only when it grew" check compares lengths (ChatPanel.vue:869; from /review, by reading). After a failed or cancelled turn the panel holds a user message the server never saved, so the next wake reply makes the counts equal and is not shown. Comparing by last-message identity avoids it.

2 and 3 are the symptom #4537 reports surviving on two paths, which is why this does not ride.

Smaller: docs/user-docs/collaboration/agent-network.md:21 and the report-back table at collaboration/event-subscriptions.md:74 still describe the old behaviour; the body has no Journey Impact: line.

Scope note for when it comes back: #4537 also names wakes on an answered ask (operator_response / operator_ending) and Workspace-originated delegations, which this PR does not touch. Either say so with a follow-up issue, or use Refs #4537 so the issue stays open.

Rides the next train once fixed; status-needs-fix is set and clears on your next push.

@vybe vybe added the status-needs-fix PR has an unaddressed review/validation finding; cleared by the author's next push (#2815) label Nov 17, 2026
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

✅ Nightly unit-suite clean when this PR is merged into dev, all 3 seeds (head_sha: 39dab1f4312aa8de74a35eea873ae4a2588574f6).

dolho and others added 2 commits November 23, 2026 16:25
…a failed turn (#4537)

Review of the frontend half found the reply still lost on two paths, and
nothing executing any of it:

- A reply that landed while a turn was in flight was skipped, and a turn's
  end never refetches messages. The re-read now waits for the turn and runs
  when it ends.
- 'Only when it grew' compared lengths. After a failed or cancelled turn the
  panel holds a message the server never saved, the counts are equal, and
  the reply was not shown. utils/chatSessionMerge decides by identity: a
  stored row by its id, a row the panel pushed itself by role and text.
  Rows only the screen has stay where they were.
- A mounted spec drives the whole path: the chat_response_ready frame on
  /ws, the store, the panel's watcher, the session re-read and the merge.

Docs: the user docs for wake-on-ending and report-back describe the reply
landing in the Chat tab.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@github-actions github-actions Bot removed the status-needs-fix PR has an unaddressed review/validation finding; cleared by the author's next push (#2815) label Nov 23, 2026
@dolho

dolho commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Review findings addressed (74cd71b4a)

dev is merged in. All three frontend findings are fixed, with a mounted spec.

  1. No executing test. chatPanelWakeReply4537.mount.spec.js mounts the real ChatPanel and drives the chat_response_ready frame through utils/websocket.js, the store and the watcher to the session re-read. Removing the handler, the store write or the watcher each turns a test red.
  2. A reply that lands mid-turn was dropped. Confirmed by test. The re-read now waits for the turn and runs when it ends.
  3. Length compare. Confirmed by test: after a failed turn the counts were equal and the reply was not shown. utils/chatSessionMerge.js now decides by identity (a stored row by id, a row the panel pushed itself by role and text). Messages only the screen has stay where they were.

Smaller items:

  • agent-network.md and the report-back table in event-subscriptions.md now describe the reply landing in the Chat tab.
  • The body has a Journey Impact: line (none, with the reason).
  • The body now says Related to #4537 and lists what is out of scope, so the issue stays open.

One limit: a locally pushed reply is matched to its saved row by exact text. If the server ever stores a reply differently from what the execution returned, that reply would show twice until the next load.

The test_subprocess_pgroup failure on the nightly run is the known flake, unrelated to this PR.

The sync-on-loop ratchet (#4680, now on dev) counted six blocking calls
this PR added inside async def: the delegating-turn and reply-thread reads
in _post_wake_reply and _claimed_wake, the chat-message write in
post_platform_turn_reply and the execution/session link in
persist_chat_session. Each now hops to a thread; the ratchet holds at its
baseline with no entry raised.

test_4536_wake_window steps the wake window one loop tick at a time, so its
fixture runs that read inline: the choreography it pins is the window's.

Also repoints agentDetailTalkDoor.spec.js at utils/chatSessionMerge.js,
where the row mapping it pins now lives.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@dolho

dolho commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Two CI failures fixed (8dddecfaf)

  • Sync-on-loop ratchet (new on dev since test(backend): ratchet on blocking calls inside async def (#4674) #4680): this PR added six blocking database calls inside async def in caller_wake_service.py and chat_persistence_service.py. Each now runs in a thread, and the ratchet holds at its baseline with no entry raised.
    • test_4536_wake_window steps the wake window one loop tick at a time, so its fixture runs the new read inline. That suite pins the window's ordering, not the thread hop.
  • agentDetailTalkDoor.spec.js asserted a line of source that moved to utils/chatSessionMerge.js; it now checks it there.

Run locally: the wake and chat-persistence suites (137 passed) and the two frontend specs (38 passed).

Still red until #4728 is on dev: the 15 test_4537_wake_conversation errors in the regression diff are the redis 8.2.0 / fakeredis break (#4727), not this PR. They pass locally with redis 8.1.0.

@vybe

vybe commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

merge-train (2026-11-24): on this train. Validated at 39dab1f43; no critical findings. Thirteen of fourteen deletions turn a test red on the backend and the mounted panel; the fourteenth (session.agent_name != caller in _reply_thread) is repeated in post_platform_turn_reply.

Nothing was pushed to this branch. The body keeps Related to #4537, since answered-ask wakes, Workspace-started delegations and the save-ordering race are still open there.

This PR and #4745 both add a bullet to docs/user-docs/collaboration/agent-network.md. Whichever merges second will get one dev merge from the train that keeps both bullets.

Follow-ups, none blocking:

  • A held-request notice can show twice. After a skill-gated request is decided, the next re-read adds the server's notice while the panel's own id-less "needs approval" row stays (utils/chatSessionMerge.js:44-46). It clears on reload. Before this PR nothing re-read the session.
  • The race the body lists under "Not changed" covers the issue's own scenario. The thread is named on the /task row only after the delegating turn is saved (chat_persistence_service.py:187-190), so a fast callee can end first and its wake has no reply thread.
  • Every async /task turn now triggers one extra session GET through chat_response_ready (websocket.js:317), not only wakes.

@vybe vybe 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.

merge-train: batch validated on train/20261124-0752

@vybe
vybe merged commit 9c014a6 into dev Nov 24, 2026
27 checks passed
vybe pushed a commit that referenced this pull request Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ui PR touches the frontend UI — triggers Playwright e2e tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants