Skip to content

fix(chat): reconnect after a question pending across a restart - #7919

Merged
Meraj-08 merged 2 commits into
OrchestratorInc:mainfrom
ApexYash11:fix/7658-question-card-turn-link
Nov 9, 2026
Merged

Meraj-08 merged 2 commits into
OrchestratorInc:mainfrom
ApexYash11:fix/7658-question-card-turn-link

Conversation

@ApexYash11

@ApexYash11 ApexYash11 commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

Code changes: +53 -0
Tests: +100 -0
Others: +73 -0 (generated sqlc)

Fixes #7944. Surfaced by #7658; that report's no-card symptom is not reproduced by this change (see the review discussion).

What was wrong

The chat host survives a daemon restart and replays its pending requests. An elicited question reaches UpsertActivity with only its RequestID — it has no ProviderItemID — and UpsertActivity matched existing rows by provider item id alone. The replay therefore went to insert and hit the partial unique index on conversation_activities (conversation_id, request_id). The controller treats a failed projection as fatal: it stops with the provider retained for replay, the turn stays running, resolving the question returns CHAT_CONTROLLER_NOT_READY, and every retry — including Resume agent — replays into the same wall (#7944).

The fix

A replay now refreshes the row AO already stored for that request id, in UpsertActivity:

  • match by request_id when the activity carries no provider item, and settle that row instead of inserting a duplicate;
  • only an in-flight row (pending or running) is brought up to date;
  • any settled row stays settled, so a late replay cannot reopen it: a resolved row (an answered question), and equally a failed one — settleOrphanedWork fails a stranded question together with its turn, and reviving it would park a pending question on a failed turn, one the dock never shows.

Approaches explored and dropped after review:

  • controller-side turn anchoring: dropped — ActivateLiveReconnect installs the turn correlation before the replay gate opens, and the stored row is already linked to the running turn in every observed run.
  • frontend dock fallback for turn-less questions: dropped — main already docks a turn-less pending question while a turn runs (the : !turn branch of the skip condition is false when a turn exists), and docking one while nothing runs would surface stale questions.

Tests

TestUpsertActivityReplayRefreshesTheStoredQuestionInsteadOfColliding — a replayed input.requested lands on the stored row (fails on main with exactly UNIQUE constraint failed: conversation_activities.conversation_id, conversation_activities.request_id, verified on pristine main), stays pending, and a late replay after the user answered keeps the row resolved.

TestUpsertActivityReplayDoesNotReopenAFailedQuestion — after FailPendingInputs (the settleOrphanedWork path: row and turn fail in the same pass), a replay keeps the row failed instead of reviving it to a pending question on a failed turn.

Validation

  • go test ./internal/storage/sqlite/store/ green, including both new tests; focused internal/service/chat failures are pre-existing Windows-environment issues (drive-letter mkdir syntax, symlink privilege, POSIX file mode) and reproduce identically on pristine main.
  • golangci-lint v2.13.2: 0 issues for ./internal/storage/sqlite/....
  • npm run sqlc regenerated; the committed gen/ output matches (CI sqlc-drift validates).

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

🏆 Review leaderboard

Oct 31, 2026–Nov 7, 2026 · UTC

Rank Reviewer PRs reviewed Review rounds PR comments
🥇 @ronishrohan @ronishrohan 63 96 13
🥈 @Rishet11 @Rishet11 26 49 19
🥉 @Vaibhaav-Tiwari @Vaibhaav-Tiwari 23 29 3
4 @nikhilachale @nikhilachale 12 64 5
5 @somewherelostt @somewherelostt 11 48 1
6 @illegalcall @illegalcall 10 20 4
7 @Prasad-D-Ware @Prasad-D-Ware 10 14 0
8 @Meraj-08 @Meraj-08 7 13 0
9 @Pritom14 @Pritom14 7 9 4
10 @Annieeeee11 @Annieeeee11 5 7 2

Ranked by distinct external PRs reviewed, then review rounds, then PR comments. Self-activity and bot activity are excluded.

Show 8 more reviewers
Rank Reviewer PRs reviewed Review rounds PR comments
11 @ApexYash11 @ApexYash11 4 14 2
12 @neversettle17-101 @neversettle17-101 4 5 3
13 @mohakchakraborty2004 @mohakchakraborty2004 4 5 2
14 @codebanditssss @codebanditssss 4 4 0
15 @harshitsinghbhandari @harshitsinghbhandari 2 3 0
16 @Vansh-Singhal @Vansh-Singhal 1 3 0
17 @wesleymatosdev @wesleymatosdev 1 2 1
18 @AgentWrapper @AgentWrapper 1 1 0

@ApexYash11
ApexYash11 requested a review from Meraj-08 November 7, 2026 13:45
@OrcInc OrcInc added bug Something isn't working comp/daemon Go daemon, process lifecycle, and backend control plane. labels Nov 7, 2026
@OrcInc OrcInc added this to the Agents & orchestration milestone Nov 7, 2026

@Meraj-08 Meraj-08 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for digging into this. I ran #7658's exact question call on real OpenCode 2.2.5 through the daemon and the desktop app, on main and on this branch, and the stuck state comes from somewhere else.

[P1] The stuck question comes from a replay collision, not a missing turn link. With the question pending, I restarted the daemon and let the surviving chat host reconnect. The replayed input.requested fails with UNIQUE constraint failed: conversation_activities.conversation_id, conversation_activities.request_id and the projection stops, so the turn stays running, resolving the input returns CHAT_CONTROLLER_NOT_READY, and Resume agent hits the same error again. It happens the same way on b3f658811e. UpsertActivity only matches an existing row by ProviderItemID, and a question carries only a RequestID, so the replay inserts a second row. In every run, including both restarts, the original row was linked to the running turn. Details are in #7944. Could the fix make the replayed request update the existing row by request_id instead?

[P2] The dock change only affects the case where no turn is running. On main, latestPendingInteraction already docks a question with no turn link while a turn runs, and the new ChatWorkspace test passes against main's component unchanged. The only behavior this changes is docking such a question when nothing is running, which could show a stale question whose answer has nowhere to go. Could the description and the doc comment say which case this is for, with a test that fails on main?

The chat host replays a pending interaction after the daemon reconnects,
and an elicited question reaches UpsertActivity with only its request
id. UpsertActivity matched existing rows by provider item id alone, so
the replay went to insert and hit the (conversation_id, request_id)
index. The controller treats a failed projection as fatal: it stops with
the provider retained for replay, the turn stays running, resolving the
question returns CHAT_CONTROLLER_NOT_READY, and every retry - including
Resume agent - replays into the same wall (OrchestratorInc#7944).

A replay now refreshes the row already stored for that request id. A
pending or failed row is brought back up to date: a failed row is a
question the previous controller's death stranded, and the replayed
request is live again. A resolved row is left alone, so a late replay
cannot reopen an answered question.

Fixes OrchestratorInc#7944
@ApexYash11
ApexYash11 force-pushed the fix/7658-question-card-turn-link branch from b3f6588 to 55f43d0 Compare November 8, 2026 05:01
@ApexYash11 ApexYash11 changed the title fix(chat): dock a pending question even when its turn link is missing fix(chat): reconnect after a question pending across a restart Nov 8, 2026
@ApexYash11

Copy link
Copy Markdown
Collaborator Author

Thank you — this review found the real mechanism, and the PR is reworked around it. Both points addressed, plus one correction on my side.

P1 — done as requested. UpsertActivity now matches an existing row by request_id (falling through after the provider-item lookup misses) and settles it instead of inserting a duplicate, so the replayed input.requested can no longer trip the (conversation_id, request_id) index and stop the projection. One deliberate extension beyond the skip-insert you validated: a row a dead controller left failed is revived too, since the replayed request is live again — otherwise a graceful ao stop / start would swap the crash for an invisible question. A resolved row is never touched, so a late replay cannot reopen an answered question. New store test TestUpsertActivityReplayRefreshesTheStoredQuestionInsteadOfColliding fails on main with exactly the UNIQUE constraint failed: conversation_activities.conversation_id, conversation_activities.request_id you reported (verified against pristine main), and passes with the fix.

P2 — reverted. You were right and I had the condition backwards: : !turn is the skip branch of the ternary, so main already docks a turn-less pending question while a turn runs, and my test passed against main unchanged. The only behavior my change added was docking such a question while nothing runs, which is the stale-question risk you flagged. Both the dock change and the controller-side turn anchoring are removed; the branch is rebased onto current main (5cde191aad) and now touches only the store path.

Description rewritten accordingly (mechanism, fix, and the two dropped approaches).

@ApexYash11
ApexYash11 requested a review from Meraj-08 November 8, 2026 05:26

@Meraj-08 Meraj-08 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Re-tested 55f43d0dd5 on real OpenCode 2.2.5 through the daemon against main at 5cde191aad. With a question pending, a hard daemon kill and an ao stop/start now both reconnect within 5 seconds, the answer returns 204 and the turn completes, where main stops on the unique constraint every time. One P2 on the failed-row path.

settleOrphanedWork fails a pending question and its turn in the same pass,
so the replay guard (status <> resolved) turned that row back to pending on
a failed turn - a pending question latestPendingInteraction never docks,
invisible to the user. Only an in-flight row (pending/running) is refreshed
now, so a replay cannot reopen any settled question: answered (resolved),
failed, cancelled, or completed. Covers the failed case with a test.

Addresses the review P2 on PR OrchestratorInc#7919.
@ApexYash11
ApexYash11 requested a review from Meraj-08 November 8, 2026 15:07

@Meraj-08 Meraj-08 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Reviewed through 89922c542d: with a question pending, both a killed daemon and ao stop/start now reconnect within 5s, the answer returns 204, and the agent continues, where main stops the projection on the duplicate insert.

One non-blocking note: the description still says a pending or failed row is refreshed, but the second commit narrows that to pending and running rows. Could the description match? Fine as a follow-up.

@ApexYash11

Copy link
Copy Markdown
Collaborator Author

Done - the description now matches the second commit: only pending/running rows refresh, and settled rows (resolved, failed, cancelled, completed) stay settled, with the failed-turn rationale spelled out. Counts recalculated for the three-commit diff; golangci-lint still 0 issues.

@Meraj-08
Meraj-08 merged commit 32fd8d2 into OrchestratorInc:main Nov 9, 2026
17 checks passed
axisrow pushed a commit to axisrow/agent-orchestrator that referenced this pull request Nov 11, 2026
…stratorInc#7919)

* fix(chat): reconnect after a question pending across a restart

The chat host replays a pending interaction after the daemon reconnects,
and an elicited question reaches UpsertActivity with only its request
id. UpsertActivity matched existing rows by provider item id alone, so
the replay went to insert and hit the (conversation_id, request_id)
index. The controller treats a failed projection as fatal: it stops with
the provider retained for replay, the turn stays running, resolving the
question returns CHAT_CONTROLLER_NOT_READY, and every retry - including
Resume agent - replays into the same wall (OrchestratorInc#7944).

A replay now refreshes the row already stored for that request id. A
pending or failed row is brought back up to date: a failed row is a
question the previous controller's death stranded, and the replayed
request is live again. A resolved row is left alone, so a late replay
cannot reopen an answered question.

Fixes OrchestratorInc#7944

* fix(chat): do not reopen a failed question when the chat host replays it

settleOrphanedWork fails a pending question and its turn in the same pass,
so the replay guard (status <> resolved) turned that row back to pending on
a failed turn - a pending question latestPendingInteraction never docks,
invisible to the user. Only an in-flight row (pending/running) is refreshed
now, so a replay cannot reopen any settled question: answered (resolved),
failed, cancelled, or completed. Covers the failed case with a test.

Addresses the review P2 on PR OrchestratorInc#7919.

(cherry picked from commit 32fd8d2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working comp/daemon Go daemon, process lifecycle, and backend control plane.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(chat): a question pending across a daemon restart leaves the session stuck for good

3 participants