Skip to content

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

Open
dolho wants to merge 2 commits into
devfrom
fix/4537-wake-reply-reaches-chat
Open

dolho wants to merge 2 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 refetches the session it shows, in place, and only when it grew.

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: raw-color and loading-gate ratchet specs pass; vite build succeeds
  • 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, and the Chat tab refresh in a browser. 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.

Fixes #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 <noreply@anthropic.com>
@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 <noreply@anthropic.com>
@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

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

status-needs-fix PR has an unaddressed review/validation finding; cleared by the author's next push (#2815) 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