Skip to content

Fix session deletion bug when quitting with incomplete tasks#266

Merged
subsy merged 7 commits intomainfrom
claude/investigate-issue-247-Jviz6
Feb 3, 2026
Merged

Fix session deletion bug when quitting with incomplete tasks#266
subsy merged 7 commits intomainfrom
claude/investigate-issue-247-Jviz6

Conversation

@subsy
Copy link
Copy Markdown
Owner

@subsy subsy commented Feb 3, 2026

Summary

Fixes a critical bug (#247) where session files were incorrectly deleted when users quit the application with incomplete tasks. The bug was caused by checking engine.status === 'idle' as a completion condition, which is always true after the engine finishes regardless of why it stopped.

Changes

  • Fixed completion logic (src/commands/run.tsx): Removed the finalState.status === 'idle' condition that was always evaluating to true. Now only checks if tasksCompleted >= totalTasks to determine if a session should be deleted.

  • Added validation warning (src/commands/resume.tsx): When resuming a session, now detects and warns users if the tracker returns no tasks while the session has task history. Provides helpful guidance on how to recover by explicitly specifying the tracker source.

  • Added regression tests (tests/commands/run.test.ts): Comprehensive test suite covering the completion logic, including the exact scenario from the bug report (108/130 tasks completed).

  • Added investigation documentation (docs/investigations/issue-247-session-resume-failures.md): Detailed analysis of the root cause, affected code paths, and recommendations for future improvements.

Root Cause

The engine's runLoop() sets status = 'idle' in a finally block that executes regardless of why the loop exits (user quit, max iterations, no tasks, or actual completion). The completion check incorrectly used this status as an indicator of task completion, causing sessions to be deleted even when tasks were incomplete.

Impact

  • Sessions are now preserved when users quit with incomplete tasks
  • Users can resume interrupted work without losing progress
  • Better error messaging guides users when tracker state doesn't match session expectations

https://claude.ai/code/session_01E6dQhS1es6Tdm3p5Y2XxaK

Summary by CodeRabbit

  • Bug Fixes

    • Prevented premature session deletion during idle states and made completion detection rely on task progress, not engine idle.
    • Emit a clear warning when a resumed session’s task counts and the runtime tracker disagree, with guidance on likely causes and remediation.
  • Documentation

    • Added an investigation document detailing session resume issues, root causes, recovery behaviour and testing recommendations.
  • Tests

    • Added tests covering completion logic and tracker-mismatch scenarios to prevent regressions.

Identifies root cause of session file deletion bug: the completion check
at run.tsx:1975-1976 incorrectly uses engine status 'idle' which is
always true after execution finishes, regardless of why it stopped.

Also documents why "No task loaded" occurs after manual recovery -
tasks are loaded from tracker, not session.json.

https://claude.ai/code/session_01E6dQhS1es6Tdm3p5Y2XxaK
Fixes #247 - Session resume failures

The bug was in the session completion check at run.tsx:1975-1976.
The condition `finalState.status === 'idle'` was always true after
the engine finished (set in finally block), regardless of WHY it
stopped. This caused sessions to be incorrectly deleted even when
the user quit normally with incomplete tasks.

Changes:
- Remove buggy `|| finalState.status === 'idle'` from completion check
- Add helpful warning when resume finds no tasks but session has history
- Add regression tests for the completion condition

The fix ensures session.json is only deleted when all tasks are
actually complete (tasksCompleted >= totalTasks).

https://claude.ai/code/session_01E6dQhS1es6Tdm3p5Y2XxaK
@vercel
Copy link
Copy Markdown

vercel bot commented Feb 3, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
ralph-tui Ignored Ignored Preview Feb 3, 2026 10:15pm

Request Review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 3, 2026

Walkthrough

Adds an investigation doc for Issue #247, introduces a tracker-parity warning on resume, stops using engine 'idle' as a session-complete signal by adding explicit completion logic, and exposes two helpers (shouldWarnAboutTrackerMismatch, isSessionComplete) with corresponding tests.

Changes

Cohort / File(s) Summary
Investigation Documentation
docs/investigations/issue-247-session-resume-failures.md
New investigation document describing session resume failures, root causes (including premature deletion when finalState.status === 'idle'), impacted locations, immediate fix, recovery behaviour, testing scenarios, and future improvements.
Session Resume Validation
src/commands/resume.tsx
Adds and exports shouldWarnAboutTrackerMismatch(engineTotalTasks, sessionTotalTasks); emits a multi-line warning during resume when engine/tracker reports 0 total tasks but the resumed session has task history.
Run Completion Logic
src/commands/run.tsx
Adds and exports isSessionComplete(parallelAllComplete, tasksCompleted, totalTasks) and replaces previous check relying on engine.status === 'idle' with isSessionComplete to determine session completion (handles parallel and sequential modes explicitly).
Run Command Tests
tests/commands/run.test.ts
Adds tests for isSessionComplete, covering sequential and parallel modes, edge cases (0/0, overcomplete), and confirms completion decision no longer depends on engine status.
Resume Command Tests
tests/commands/resume.test.ts
Adds tests for shouldWarnAboutTrackerMismatch, covering mismatch and non-mismatch scenarios and boundary conditions.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • fix: improve Windows/WSL compatibility #36 — Addresses session resume / tracker-state issues and defends against missing or corrupt trackerState during session load; likely related to tracker-parity and resume behaviour.
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title 'Fix session deletion bug when quitting with incomplete tasks' accurately and specifically describes the main change: addressing a bug where sessions were incorrectly deleted when users quit with incomplete tasks.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch claude/investigate-issue-247-Jviz6

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 and usage tips.

Resolved conflict in src/commands/run.tsx:
- Main added parallel mode support with `parallelAllComplete` variable
- My fix removed the buggy `|| finalState.status === 'idle'` condition
- Merged both: kept parallel mode support AND applied the bug fix

https://claude.ai/code/session_01E6dQhS1es6Tdm3p5Y2XxaK
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 3, 2026

Codecov Report

❌ Patch coverage is 31.42857% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 43.69%. Comparing base (e94bdd8) to head (bf6ed0a).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
src/commands/resume.tsx 21.73% 18 Missing ⚠️
src/commands/run.tsx 50.00% 6 Missing ⚠️

❌ Your patch status has failed because the patch coverage (31.42%) is below the target coverage (50.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #266      +/-   ##
==========================================
- Coverage   43.70%   43.69%   -0.01%     
==========================================
  Files          94       94              
  Lines       28978    29009      +31     
==========================================
+ Hits        12665    12676      +11     
- Misses      16313    16333      +20     
Files with missing lines Coverage Δ
src/commands/run.tsx 7.28% <50.00%> (+0.23%) ⬆️
src/commands/resume.tsx 27.56% <21.73%> (-0.24%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Add tests covering:
- Tracker validation warning logic in resume command
- Parallel mode completion condition (from merged main)
- Sequential mode fallback with fix verification
- Edge cases: empty projects, partial completion, task count mismatches

All tests verify that the old buggy logic would have failed,
documenting the fix for future reference.

https://claude.ai/code/session_01E6dQhS1es6Tdm3p5Y2XxaK
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@tests/commands/resume.test.ts`:
- Around line 35-37: The test comment uses time‑bound phrasing ("This was the
user's scenario - session showed 108/130 but tracker returned nothing") which
should be made evergreen; update the comment in the test around the
expect(shouldWarnAboutTrackerMismatch(0, 130)) to describe the scenario
neutrally (e.g., "Scenario: session reports 108/130 but tracker reports 0
tasks") without referencing time or "user" language so it remains relevant
long-term and clearly documents the intent for the
shouldWarnAboutTrackerMismatch test.

In `@tests/commands/run.test.ts`:
- Around line 454-588: Update comments in the tests so they avoid temporal
phrasing (e.g., "was", "previously", "old", "would have been", "This was the
bug!") and instead use evergreen wording; specifically edit the commentary
inside the describe blocks for session completion and parallel mode (the
comments around the describe titles and inside the tests that explain the bug)
as well as inline comments near the helper functions isAllComplete and
computeAllComplete and the two tests named "completion does NOT depend on engine
status" and "sequential fallback does NOT use engine status" to describe the bug
and fixed logic in present-tense, timeless language.

Extract helper functions for better testability and coverage:
- isSessionComplete() in run.tsx for completion logic
- shouldWarnAboutTrackerMismatch() in resume.tsx for validation

Update tests to:
- Import and test actual exported functions (improves coverage)
- Use evergreen comments without temporal language
- Document invariants in present tense

Addresses PR feedback on test comments.

https://claude.ai/code/session_01E6dQhS1es6Tdm3p5Y2XxaK
Expand test coverage for isSessionComplete() and shouldWarnAboutTrackerMismatch():

- Reorganize tests into logical groups (mismatch detection, no mismatch, boundary conditions)
- Add edge cases: large task counts, zero scenarios, minimal mismatches
- Add explicit tests for nullish coalescing behavior in parallel mode
- Add tests demonstrating parallel mode completely overrides task count logic
- Document key invariants for both functions

https://claude.ai/code/session_01E6dQhS1es6Tdm3p5Y2XxaK
@subsy subsy merged commit ef3ace0 into main Feb 3, 2026
8 of 9 checks passed
sakaman pushed a commit to sakaman/ralph-tui that referenced this pull request Feb 15, 2026
…Jviz6

Fix session deletion bug when quitting with incomplete tasks
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.

2 participants