Skip to content

fix: restore terminal and flush session on fatal REPL errors - #54

Open
syf2211 wants to merge 1 commit into
KlaatAI:mainfrom
syf2211:fix/crash-handler-terminal-restore
Open

fix: restore terminal and flush session on fatal REPL errors#54
syf2211 wants to merge 1 commit into
KlaatAI:mainfrom
syf2211:fix/crash-handler-terminal-restore

Conversation

@syf2211

@syf2211 syf2211 commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a process-level crash handler for the TUI so fatal errors during an interactive session restore the terminal, fsync the session JSONL, and print a resume hint before exiting non-zero. A boot marker under ~/.klaatai/ detects startup crash loops so the next launch can skip plugins and MCP in safe mode.

Motivation

Fixes #30. A fatal error mid-session currently leaves the terminal in a broken state (alt-screen, hidden cursor, mouse capture) and makes it harder for users to recover their session.

Changes

  • src/engine/crash-handler.ts: installCrashHandler() handles uncaughtException / unhandledRejection with terminal teardown, session fsync, and resume hint; boot marker helpers for crash-loop detection.
  • src/screens/repl.ts: Install handler at REPL entry (before MCP/plugins); gate MCP and plugin loading behind safe mode; clear boot marker once the REPL is ready.
  • src/main.tsx: Detect boot crash loop, write boot marker only after auth succeeds, pass safeMode to REPL; align normal-exit resume hint to klaatai --resume.
  • src/engine/crash-handler.test.ts: Unit tests for helpers and handler exit path.

Tests

  • bun run typecheck — pass
  • bun test — 253 pass (including 5 new crash-handler tests)
  • bun run build — pass

Notes

  • Session messages are already appended incrementally to JSONL; the crash handler fsyncs the file for durability.
  • Safe mode is triggered only when the boot marker remains from a prior crash during REPL startup (after auth), not from auth or directory validation failures.

Fixes #30

Add a process-level crash handler that tears down the TUI, fsyncs the
session JSONL, and prints a resume hint on uncaughtException or
unhandledRejection. Detect startup crash loops via a boot marker so the
next launch can skip plugins and MCP in safe mode.

Fixes KlaatAI#30
@aakashrajput

Copy link
Copy Markdown
Contributor

@syf2211 please fix the merge conflicts

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.

Crash handler — restore terminal, flush session, offer resume

2 participants