Skip to content

fix(app): ignore stale assistant messages for running state#109

Merged
Astro-Han merged 3 commits into
devfrom
codex/fix-session-running-state
Apr 22, 2026
Merged

fix(app): ignore stale assistant messages for running state#109
Astro-Han merged 3 commits into
devfrom
codex/fix-session-running-state

Conversation

@Astro-Han

@Astro-Han Astro-Han commented Apr 21, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add a shared isSessionRunning helper for app session running-state checks.
  • Treat live non-idle session_status as authoritative, and only fall back to message history when the latest message is an incomplete assistant message.
  • Replace the broad historical incomplete-assistant scans in the session timeline, sidebar item, and session busy helper.

Why

A stale incomplete assistant message from an older turn can remain in session history even after later turns complete. The previous UI checks treated any historical incomplete assistant message as current work, which kept completed sessions showing progress/spinners.

Related Issue

Fixes #95

How To Verify

cd packages/app && bun run test:unit src/pages/session/session-running-state.test.ts
cd packages/app && bun run test:unit
cd packages/app && bun run typecheck
bun turbo typecheck
bun turbo test:ci

Screenshots or Recordings

Not included. This is a state-derivation fix covered by unit tests, with no intentional visual design change.

Checklist

  • I ran the relevant verification steps
  • I tested visible changes manually when needed
  • I am targeting the dev branch

Summary by CodeRabbit

  • Refactor

    • Consolidated session state determination logic into a centralized shared helper function, reducing code duplication and improving maintainability across multiple application components.
  • Tests

    • Added comprehensive test coverage for session running state evaluation, including edge cases and various status conditions.

@Astro-Han Astro-Han added bug Something isn't working P2 Medium priority app Application behavior and product flows labels Apr 21, 2026
@coderabbitai

coderabbitai Bot commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@Astro-Han has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 14 minutes and 47 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 14 minutes and 47 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 26181787-1b5b-4430-8d2a-4fd82b5ef69f

📥 Commits

Reviewing files that changed from the base of the PR and between de0bffb and 7b44979.

📒 Files selected for processing (2)
  • packages/app/src/pages/session/session-running-state.test.ts
  • packages/app/src/pages/session/session-running-state.ts
📝 Walkthrough

Walkthrough

This change consolidates session "running" state logic from three UI components into a single, tested helper function isSessionRunning. The helper correctly checks only the latest assistant message for incompleteness, resolving a bug where stale incomplete messages kept completed sessions marked as running.

Changes

Cohort / File(s) Summary
Session Running State Helper
packages/app/src/pages/session/session-running-state.ts, packages/app/src/pages/session/session-running-state.test.ts
New helper function isSessionRunning that determines session status by checking non-idle session status or the latest assistant message's incompleteness. Includes comprehensive test fixtures and five test cases covering stale incomplete messages, varied session statuses, and edge cases.
Component Refactoring
packages/app/src/pages/layout/sidebar-items.tsx, packages/app/src/pages/session.tsx, packages/app/src/pages/session/message-timeline.tsx
Replaced inline session running logic with calls to isSessionRunning, removing redundant status and message inspection code across three files. All three components now delegate to the centralized helper.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 One stale message made sessions spin round and round,
But now we extract truth from the latest we've found,
isSessionRunning checks what matters today—
No phantom spinners dancing in yesterday's way! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% 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 summarizes the main change: introducing a helper to ignore stale assistant messages when determining session running state, directly addressing the linked issue.
Description check ✅ Passed The description includes all required sections: Summary, Why, Related Issue (#95), How To Verify with test commands, and a completed Checklist confirming verification was performed.
Linked Issues check ✅ Passed The pull request introduces the isSessionRunning helper and refactors three components to use it, treating non-idle session_status as authoritative and only checking incomplete assistant messages when idle. This directly implements the fix for issue #95.
Out of Scope Changes check ✅ Passed All changes are directly in scope: new isSessionRunning helper with tests, and refactoring three existing components to use it. No unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-session-running-state

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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request centralizes the logic for determining if a session is active by introducing a new utility function, isSessionRunning, and applying it across several components including the sidebar, session page, and message timeline. Comprehensive unit tests were also added for this new utility. Feedback was provided regarding potential TypeScript errors when accessing message completion times, suggesting the use of optional chaining for better type safety.

Comment thread packages/app/src/pages/session/session-running-state.ts Outdated
@Astro-Han Astro-Han force-pushed the codex/fix-session-running-state branch from 8faf1bb to 8ca5931 Compare April 21, 2026 16:21

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

总体逻辑正确,测试覆盖充分。几点 inline 供参考(无阻塞问题)。

Comment thread packages/app/src/pages/layout/sidebar-items.tsx
Comment thread packages/app/src/pages/session.tsx
Comment thread packages/app/src/pages/session/message-timeline.tsx
Comment thread packages/app/src/pages/session/session-running-state.ts
Comment thread packages/app/src/pages/session/session-running-state.test.ts

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Overall: Clean fix with good test coverage. One edge case suggestion.

Core Logic Review

The new isSessionRunning helper consolidates duplicate logic across 3 files — good refactor. The logic is:

if ((status ?? idle).type !== "idle") return true
const latest = messages?.at(-1)
return latest?.role === "assistant" && typeof latest.time?.completed !== "number"

Key insight: Trust live session_status over message history. Only fallback to checking the latest message, not scanning all history. This correctly fixes the stale-incomplete-assistant bug.

Edge Case: Empty Message Array

When messages is an empty array []:

  • messages?.at(-1) returns undefined
  • undefined?.role === "assistant" is false
  • Returns false ✓ (idle state)

This is correct behavior, but worth adding a test case for clarity.

Minor Nits

  1. Test file import path: import { isSessionRunning } from "./session-running-state" — works, but explicit extension would be clearer for future tooling.

  2. Test helper naming: The user and assistant factory functions are minimal. Consider adding JSDoc or renaming to makeUserMessage/makeAssistantMessage for discoverability.

These are nits; not blocking.

Suggestion: Add Empty Array Test

test("returns false when messages array is empty", () => {
  expect(isSessionRunning(idle, [])).toBe(false)
})

This documents the edge case and ensures future refactorers don't accidentally change behavior.

@Astro-Han

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Apr 22, 2026

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/app/src/pages/session/session-running-state.test.ts`:
- Around line 64-70: Add explicit tests calling isSessionRunning with undefined
inputs to lock the helper contract: add assertions in
session-running-state.test.ts that isSessionRunning(undefined, undefined)
returns false, isSessionRunning(undefined, []) returns false, and
isSessionRunning(undefined, [user("msg_user",1)]) behaves like the idle-case
expectation; reference the isSessionRunning helper to implement these new test
cases so callers passing undefined status or messages are covered.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 2c11aa1a-f426-497b-b23d-51c709408429

📥 Commits

Reviewing files that changed from the base of the PR and between 63a8a52 and de0bffb.

📒 Files selected for processing (5)
  • packages/app/src/pages/layout/sidebar-items.tsx
  • packages/app/src/pages/session.tsx
  • packages/app/src/pages/session/message-timeline.tsx
  • packages/app/src/pages/session/session-running-state.test.ts
  • packages/app/src/pages/session/session-running-state.ts

Comment thread packages/app/src/pages/session/session-running-state.test.ts
@Astro-Han Astro-Han force-pushed the codex/fix-session-running-state branch from de0bffb to 7b44979 Compare April 22, 2026 02:17
@Astro-Han Astro-Han merged commit 718e1c9 into dev Apr 22, 2026
14 checks passed
@Astro-Han Astro-Han deleted the codex/fix-session-running-state branch April 22, 2026 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app Application behavior and product flows bug Something isn't working P2 Medium priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Completed sessions can stay marked as running after stale incomplete assistant messages

1 participant