Skip to content

Replace assistant-prose heuristic with explicit turn lifecycle markers - #9121

Merged
AmethystLiang merged 6 commits into
mainfrom
native-chat-still-shows-loading-when-ai-done
Jul 17, 2026
Merged

AmethystLiang merged 6 commits into
mainfrom
native-chat-still-shows-loading-when-ai-done

Conversation

@AmethystLiang

Copy link
Copy Markdown
Contributor

Summary

Fixes native chat showing 'loading' after AI completion by replacing an unreliable prose-based heuristic with explicit provider-authored turn lifecycle records. Adds support for Claude terminal stop_reason events and Codex task boundaries.

Problem

The live-session merge relied on detecting trailing assistant prose (timestamp comparison) to settle a dropped working hook. This failed when:

  • Claude's final message never landed in the transcript (hook 'working' persisted)
  • Codex emitted task completion with no visible messages
  • A remote host lacked timely message delivery

Solution

Added NativeChatTurnLifecycle — provider-authored turn boundaries recovered from the transcript itself:

  • Claude: Terminal stop_reason fields (end_turn, max_tokens, stop_sequence, refusal) and interruptedMessageId records
  • Codex: Explicit task_started, task_complete, and turn_aborted event records
  • Grok: No explicit boundaries (existing prose fallback remains)

The merge now reconciles live hook 'working' against these boundaries: hook persists until an explicit terminal marker lands (completion/interruption) or the hook state exits.

Key Changes

  • New transcript-turn-lifecycle.ts module with decoders for each provider
  • Updated native-chat-live-status.ts merge logic to check lifecycle state against hook timing
  • Contract updates across IPC, RPC, and transport layers to forward lifecycle metadata
  • New hooks useNativeChatTranscriptLifecycle() and useNativeChatHookStatus() for renderer reconciliation
  • Validation in web preload and runtime transport layers to safely parse lifecycle from untrusted hosts
  • Interruption messages normalized to a quiet system row and marked as lifecycle (not prose)

Testing

  • Added transcript-turn-lifecycle.test.ts covering Claude/Codex decoder logic
  • Added transcript-interruption-message.test.ts for interruption normalization
  • Updated use-native-chat-live-session.test.ts with lifecycle reconciliation scenarios
  • Updated native-chat-session-transport.test.ts to validate lifecycle metadata from remotes
  • Added web-preload-api.test.ts cases for web runtime validation
  • Updated working-suppression logic tests to reflect single reconciled working signal

Notes

  • Capable hosts (Claude, Codex with transcript lifecycle support) emit turnLifecycleCapable: true so consumers know whether to trust explicit boundaries
  • Pagination reads that resolve after live completion do not rewind the lifecycle (revision tracking)
  • Older hosts (Grok, legacy remotes) fall back to prose detection without breaking behavior
  • Interruption records are decoded into visible system messages and replayable lifecycle state

@coderabbitai

coderabbitai Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 4b0a132d-c170-4bea-9e1d-dbf8d4d65013

📥 Commits

Reviewing files that changed from the base of the PR and between 2df2e19 and 4710b80.

📒 Files selected for processing (6)
  • src/preload/api-types.ts
  • src/renderer/src/components/native-chat/NativeChatView.tsx
  • src/renderer/src/components/native-chat/native-chat-runtime-send.test.ts
  • src/renderer/src/components/native-chat/native-chat-runtime-send.ts
  • src/renderer/src/components/settings/AppearanceWindowSidebarSection.tsx
  • src/renderer/src/web/web-preload-api.ts
🚧 Files skipped from review as they are similar to previous changes (5)
  • src/preload/api-types.ts
  • src/renderer/src/web/web-preload-api.ts
  • src/renderer/src/components/native-chat/NativeChatView.tsx
  • src/renderer/src/components/native-chat/native-chat-runtime-send.test.ts
  • src/renderer/src/components/native-chat/native-chat-runtime-send.ts

📝 Walkthrough

Walkthrough

Adds provider-specific native-chat turn lifecycle decoding for Claude and Codex, including completion, interruption, and working boundaries. Lifecycle metadata now flows through transcript readers, watchers, IPC frames, runtime validation, web preload adapters, and renderer session state. Renderer logic tracks lifecycle revisions, working subagents, terminal boundaries, interruption rows, and working suppression. Pending-send handling detects glued transcript rows, while PTY sends are serialized with cancellation and idle coordination. Tests cover decoding, transport propagation, pagination ordering, reconnects, pending messages, and UI status behavior.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.85% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: replacing prose-based heuristics with explicit turn lifecycle markers.
Description check ✅ Passed The description covers summary, problem, solution, testing, and notes, but it omits the screenshots, AI review report, and security audit sections.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 3


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6f2b700e-3de9-4922-9463-384108189e8e

📥 Commits

Reviewing files that changed from the base of the PR and between 3b227d0 and cff6f41.

📒 Files selected for processing (32)
  • src/main/ipc/native-chat-subscribe-lifecycle.test.ts
  • src/main/ipc/native-chat.ts
  • src/main/native-chat/transcript-incremental-reader.ts
  • src/main/native-chat/transcript-interruption-message.test.ts
  • src/main/native-chat/transcript-line-decoders-claude.ts
  • src/main/native-chat/transcript-line-decoders-codex.ts
  • src/main/native-chat/transcript-reader.ts
  • src/main/native-chat/transcript-tail-reader.ts
  • src/main/native-chat/transcript-turn-lifecycle.test.ts
  • src/main/native-chat/transcript-turn-lifecycle.ts
  • src/main/native-chat/transcript-watch-contract.ts
  • src/main/native-chat/transcript-watch-engine.ts
  • src/main/native-chat/transcript-watch.test.ts
  • src/main/native-chat/transcript-watch.ts
  • src/main/runtime/rpc/methods/native-chat.test.ts
  • src/main/runtime/rpc/methods/native-chat.ts
  • src/preload/api-types.ts
  • src/renderer/src/components/native-chat/NativeChatView.tsx
  • src/renderer/src/components/native-chat/native-chat-live-status.ts
  • src/renderer/src/components/native-chat/native-chat-noise.test.ts
  • src/renderer/src/components/native-chat/native-chat-runtime-contract.ts
  • src/renderer/src/components/native-chat/native-chat-session-transport.test.ts
  • src/renderer/src/components/native-chat/native-chat-session-transport.ts
  • src/renderer/src/components/native-chat/native-chat-working-suppression.test.ts
  • src/renderer/src/components/native-chat/native-chat-working-suppression.ts
  • src/renderer/src/components/native-chat/use-native-chat-hook-status.ts
  • src/renderer/src/components/native-chat/use-native-chat-live-session.test.ts
  • src/renderer/src/components/native-chat/use-native-chat-live-session.ts
  • src/renderer/src/components/native-chat/use-native-chat-transcript-lifecycle.ts
  • src/renderer/src/web/web-preload-api.test.ts
  • src/renderer/src/web/web-preload-api.ts
  • src/shared/native-chat-types.ts

Extract provider-authored turn boundaries (completion, interruption) directly
from Claude/Codex transcripts so the chat view knows when work ends without
guessing from message presence. Reconciles live hook state with transcript
lifecycle: when a terminal boundary lands, it settles a dropped Stop hook
instead of letting prose mislead the UI into showing 'working' after done.
Treat max_tokens/stop_sequence/refusal as completed markers so capable hosts
do not stay working after a dropped Stop, and assert lifecycle payloads on
runtime subscribe/read frames plus mid-turn non-terminal stop_reason cases.
Add type assertion and comment documenting that lifecycle field is optional and can be omitted in truncation-gating test fixtures.
When Claude's turn is explicitly interrupted, the session should show ready
immediately — even if background subagents are still running. Add an
interruption check before consulting the hook's working-subagents flag so
interruptions take precedence. Also normalize omitted lifecycle timestamps
to null instead of leaving them undefined, and add test coverage for both
cases.
Explicit transcript turn-lifecycle markers now fully replace the prose-fallback
settlement path. Remove the now-unused `turnLifecycleCapable` flag and wire
lifecycle to suppress spinner even when hook status lingers. Refine Claude
lifecycle detection to distinguish terminal stops from mid-turn tool_use rows,
exclude harness noise from new-generation detection, and apply clock-skew slack
over SSH/relay. Serialize PTY sends per line to prevent rapid prompts from gluing
before Enter, clearing unsubmitted input on cancel. Update working suppression to
detect epoch rollovers so interrupt+next-turn without a ready gap resets the
spinner correctly.
@AmethystLiang
AmethystLiang force-pushed the native-chat-still-shows-loading-when-ai-done branch from ab0ac05 to 2df2e19 Compare July 17, 2026 06:07

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 5

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/renderer/src/components/native-chat/native-chat-runtime-send.ts (1)

134-142: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Aborted verified sends leave their accepted command body in the composer. Clear that body when the delay aborts or Enter is rejected.

  • src/renderer/src/components/native-chat/native-chat-runtime-send.ts#L134-L142: perform best-effort cleanup after body acceptance when submission does not complete.
  • src/renderer/src/components/native-chat/native-chat-runtime-send.test.ts#L222-L233: assert both that Enter is suppressed and that the clear write occurs.
src/renderer/src/components/native-chat/use-native-chat-session-option-command.ts (1)

51-60: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Synchronously prevent concurrent option-command dispatches.

setIsDispatching(true) does not guard re-entry before React renders. Two calls can therefore run verified PTY writes and confirmation observers concurrently, concatenating commands or associating output with the wrong observer. Check activeSendsRef before registering another controller.

Proposed fix
     async (command, options) => {
       const target = resolveTarget()
       if (!target || disabled) {
         throw new Error('No live terminal is available.')
       }
+      if (activeSendsRef.current.size > 0) {
+        throw new Error('Another native chat option command is already in progress.')
+      }
       const sendController = new AbortController()
🧹 Nitpick comments (3)
src/renderer/src/components/native-chat/native-chat-pending.test.ts (1)

154-161: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Exercise the glued-prefix branch in this negative test.

With one pending entry, selection returns early at pending.length < 2, so this test cannot detect prefix-matching regressions. Use at least two fragments.

Proposed test adjustment
-    const pending = [pendingOf('p1', 'hi')]
+    const pending = [pendingOf('p1', 'hi'), pendingOf('p2', 'there')]
src/renderer/src/components/native-chat/native-chat-runtime-send.test.ts (2)

107-113: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover queue accounting after completed-handle cancellation.

This test only verifies that no PTY write occurs. Keep another send queued, cancel the completed handle, and assert that a newly enqueued handle retains the correct settleAfterMs.


222-233: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert that abort removes the accepted command body.

The body has already been written when this abort occurs. Verify that cancellation emits the clear sequence as well as suppressing Enter, preventing the next verified command from concatenating with stale input.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f0980522-9620-41c1-86e3-b4192a44222d

📥 Commits

Reviewing files that changed from the base of the PR and between ab0ac05 and 2df2e19.

📒 Files selected for processing (40)
  • src/main/ipc/native-chat-subscribe-lifecycle.test.ts
  • src/main/ipc/native-chat.ts
  • src/main/native-chat/transcript-incremental-reader.ts
  • src/main/native-chat/transcript-interruption-message.test.ts
  • src/main/native-chat/transcript-line-decoders-claude.ts
  • src/main/native-chat/transcript-line-decoders-codex.ts
  • src/main/native-chat/transcript-reader.ts
  • src/main/native-chat/transcript-tail-reader.ts
  • src/main/native-chat/transcript-turn-lifecycle.test.ts
  • src/main/native-chat/transcript-turn-lifecycle.ts
  • src/main/native-chat/transcript-turn-markers.ts
  • src/main/native-chat/transcript-watch-contract.ts
  • src/main/native-chat/transcript-watch-engine.ts
  • src/main/native-chat/transcript-watch.test.ts
  • src/main/native-chat/transcript-watch.ts
  • src/main/runtime/rpc/methods/native-chat.test.ts
  • src/main/runtime/rpc/methods/native-chat.ts
  • src/preload/api-types.ts
  • src/renderer/src/components/native-chat/NativeChatView.tsx
  • src/renderer/src/components/native-chat/native-chat-live-status.ts
  • src/renderer/src/components/native-chat/native-chat-noise.test.ts
  • src/renderer/src/components/native-chat/native-chat-pending-occurrence.ts
  • src/renderer/src/components/native-chat/native-chat-pending.test.ts
  • src/renderer/src/components/native-chat/native-chat-pending.ts
  • src/renderer/src/components/native-chat/native-chat-pty-send-queue.ts
  • src/renderer/src/components/native-chat/native-chat-runtime-contract.ts
  • src/renderer/src/components/native-chat/native-chat-runtime-send.test.ts
  • src/renderer/src/components/native-chat/native-chat-runtime-send.ts
  • src/renderer/src/components/native-chat/native-chat-session-transport.test.ts
  • src/renderer/src/components/native-chat/native-chat-session-transport.ts
  • src/renderer/src/components/native-chat/native-chat-working-suppression.test.ts
  • src/renderer/src/components/native-chat/native-chat-working-suppression.ts
  • src/renderer/src/components/native-chat/use-native-chat-hook-status.ts
  • src/renderer/src/components/native-chat/use-native-chat-live-session.test.ts
  • src/renderer/src/components/native-chat/use-native-chat-live-session.ts
  • src/renderer/src/components/native-chat/use-native-chat-session-option-command.ts
  • src/renderer/src/components/native-chat/use-native-chat-transcript-lifecycle.ts
  • src/renderer/src/web/web-preload-api.test.ts
  • src/renderer/src/web/web-preload-api.ts
  • src/shared/native-chat-types.ts
🚧 Files skipped from review as they are similar to previous changes (17)
  • src/main/native-chat/transcript-interruption-message.test.ts
  • src/main/native-chat/transcript-watch-contract.ts
  • src/renderer/src/components/native-chat/native-chat-noise.test.ts
  • src/renderer/src/components/native-chat/use-native-chat-hook-status.ts
  • src/main/ipc/native-chat-subscribe-lifecycle.test.ts
  • src/main/native-chat/transcript-watch.ts
  • src/main/native-chat/transcript-incremental-reader.ts
  • src/shared/native-chat-types.ts
  • src/renderer/src/components/native-chat/native-chat-session-transport.ts
  • src/renderer/src/web/web-preload-api.test.ts
  • src/main/native-chat/transcript-line-decoders-codex.ts
  • src/main/native-chat/transcript-line-decoders-claude.ts
  • src/main/native-chat/transcript-watch.test.ts
  • src/main/native-chat/transcript-turn-lifecycle.test.ts
  • src/main/native-chat/transcript-tail-reader.ts
  • src/main/native-chat/transcript-watch-engine.ts
  • src/renderer/src/components/native-chat/NativeChatView.tsx

@AmethystLiang
AmethystLiang merged commit ba25e43 into main Jul 17, 2026
2 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
stablyai#9121)

* Replace assistant-prose heuristic with explicit turn lifecycle markers

Extract provider-authored turn boundaries (completion, interruption) directly
from Claude/Codex transcripts so the chat view knows when work ends without
guessing from message presence. Reconciles live hook state with transcript
lifecycle: when a terminal boundary lands, it settles a dropped Stop hook
instead of letting prose mislead the UI into showing 'working' after done.

* fix(review): cover Claude terminal stop_reasons and RPC lifecycle frames

Treat max_tokens/stop_sequence/refusal as completed markers so capable hosts
do not stay working after a dropped Stop, and assert lifecycle payloads on
runtime subscribe/read frames plus mid-turn non-terminal stop_reason cases.

* test(native-chat): clarify that lifecycle field is optional

Add type assertion and comment documenting that lifecycle field is optional and can be omitted in truncation-gating test fixtures.

* fix(native-chat): settle status on interruption despite working subagent

When Claude's turn is explicitly interrupted, the session should show ready
immediately — even if background subagents are still running. Add an
interruption check before consulting the hook's working-subagents flag so
interruptions take precedence. Also normalize omitted lifecycle timestamps
to null instead of leaving them undefined, and add test coverage for both
cases.

* fix(native-chat): settle loading spinner on explicit turn boundaries

Explicit transcript turn-lifecycle markers now fully replace the prose-fallback
settlement path. Remove the now-unused `turnLifecycleCapable` flag and wire
lifecycle to suppress spinner even when hook status lingers. Refine Claude
lifecycle detection to distinguish terminal stops from mid-turn tool_use rows,
exclude harness noise from new-generation detection, and apply clock-skew slack
over SSH/relay. Serialize PTY sends per line to prevent rapid prompts from gluing
before Enter, clearing unsubmitted input on cancel. Update working suppression to
detect epoch rollovers so interrupt+next-turn without a ready gap resets the
spinner correctly.
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.

1 participant