Skip to content

Preserve resumed session ids after transient Codex failures#38

Open
smf-h wants to merge 1 commit into
tt-a1i:mainfrom
smf-h:fix/ignore-resize-after-pty-exit
Open

Preserve resumed session ids after transient Codex failures#38
smf-h wants to merge 1 commit into
tt-a1i:mainfrom
smf-h:fix/ignore-resize-after-pty-exit

Conversation

@smf-h

@smf-h smf-h commented Jul 2, 2026

Copy link
Copy Markdown

Summary

This PR fixes a session recovery edge case where Hive can clear last_session_id after a resumed CLI exits non-zero, even when the underlying Codex session still exists.

The original failure was observed with Codex startup/configuration errors, but a later Windows runtime crash showed the same root cause:

  • A resumed Codex process exited with 0xC0000409 during image generation under low virtual memory pressure.
  • The old .codex/sessions/**/*.jsonl file still existed and contained the detailed history.
  • Hive started a new bare Codex session afterward and injected a recovery summary, making the previous detailed context appear lost.

The important distinction is that a non-zero resumed process exit is not enough evidence that the persisted session id is stale.

Changes

  • Preserve a resumed session id when the captured session still exists after a non-zero exit.
  • Keep the existing recoverable startup/configuration failure handling.
  • Keep clearing stale session ids when there is no evidence the old session is still usable.
  • Add regression coverage for a Codex resumed run crash where the session JSONL still exists.

Verification

  • vitest run tests/unit/claude-session-resume-failure.test.ts passed: 7 tests.
  • biome check passed for the changed files.

Notes

The goal is not to hide real stale-session failures. It is to avoid treating transient CLI failures, dependency/configuration errors, or native crashes as proof that the saved session id is invalid.

@smf-h smf-h changed the title 修复 PTY 退出后的 resize 竞态 Preserve resumed session ids after transient Codex failures Jul 3, 2026
@smf-h

smf-h commented Jul 3, 2026

Copy link
Copy Markdown
Author

补充一次新的真实触发场景后,我把这个 PR 从「启动错误文案白名单」扩展成了更通用的 session invalidation 策略。

这次不是依赖缺失或 hooks 配置错误,而是 Windows 上 resumed Codex 在运行中以 0xC0000409 退出,同时系统日志显示 low virtual memory。旧 .codex/sessions JSONL 仍然存在,但 Hive 清掉/替换了 last_session_id,导致下一次启动变成裸 Codex,只剩 recovery summary。

因此这个问题的核心是:resumed run 非 0 退出不能直接等价于 session id 无效。现在的补充提交会在 captured session 仍存在时保留 last_session_id,同时保留原来对明确 stale session 的清理能力。

@tt-a1i

tt-a1i commented Jul 7, 2026

Copy link
Copy Markdown
Owner

Thanks for the detailed follow-up and the concrete repro.

I agree with the root cause and the repair direction here: a non-zero exit from a resumed Codex process is not enough evidence that the persisted last_session_id is invalid. Hive should only clear that pointer when it has positive evidence that the session is stale, for example when the captured session file is gone or the CLI explicitly reports that the session/conversation cannot be found. For transient startup/config/runtime failures, especially when the Codex JSONL still exists, preserving last_session_id and retrying native resume on the next start is the safer behavior.

The public repo is currently behind the newer private source, so this may not be mergeable as-is against the latest implementation. I’ll use this issue/PR as the reference when migrating the fix into the current codebase. Thanks for documenting the Windows crash/resource-pressure case and the regression-test direction.

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