Skip to content

TUI: stop sort-drift on navigation + load newest session on launch - #184

Merged
FMXExpress merged 2 commits into
mainfrom
claude/tui-session-load-fixes
Jun 8, 2026
Merged

FMXExpress merged 2 commits into
mainfrom
claude/tui-session-load-fixes

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

Two user-reported regressions against PR #182's newest-first session sort. Both live in the TUI session start/navigate path.

Bug A — sort drifts when picking older sessions

Symptom: open pasclaw tui, peek at an older session from the left pane, come back — and the order has reshuffled. The session you just left is now at the top.

Root cause: SelectSession called PersistSession on the outgoing session "to flush anything pending before the swap." By the time we reach SelectSession, every meaningful state change is already on disk — PollLoopWorker.ApplyLoopResultTo saves after each turn, ApplyModelSelection saves after /model, etc. Pure navigation had nothing to save — but PersistSession's unconditional FSession.Touch bumped Meta.UpdatedAt anyway, so the next RefreshSessions re-sorted the just-departed session to the top.

Fix: drop the Persist call in SelectSession. Added a comment listing what's already persisted at each entry point so a future maintainer doesn't re-add it on the theory it might miss something.

Bug B — TUI launches with empty chat, mismatched highlight

Symptom: pasclaw tui with no args shows the newest session highlighted in the left pane, but the chat pane is empty.

Root cause: when SessionId is empty, TSession.Create('') auto-allocates a brand-new session id and returns an in-memory blank — FSession.Meta.Id doesn't match any persisted session. RefreshSessions then can't find it in FSessions and falls back to FSelSessIdx := 0, which highlights the newest persisted session. So the highlight and the rendered chat diverge.

Fix: in TTUI.Run, resolve SessionId before creating the session:

  • non-empty (--session id): honor as-is (existing behavior — pre-seed if missing, resume if present).
  • empty + sessions on disk: resume the newest by UpdatedAt — pasclaw tui picks up where you left off, highlight matches chat.
  • empty + no sessions on disk: fresh id (original PR Persistent session management — durable conversations across restarts #117 default-persist behavior).

N still spawns a fresh session via StartNewSession → SelectSession('') — that path is unchanged.

Test plan

  • make rebuilds cleanly
  • make test — all 13 suites green
  • Manual: pasclaw tui against a workspace with several sessions — confirm the newest session loads in the chat pane, with its row highlighted in the left pane.
  • Manual: navigate to an older session with arrow keys + Enter, then arrow back to the top — confirm the order is stable (the session you left isn't re-promoted).
  • Manual: press N to start a fresh session — confirm a new blank session opens, persists on the first user turn.
  • Manual: pasclaw tui --session <existing-id> — confirm the explicit id still wins.
  • Manual: pasclaw tui against an empty workspace (no sessions/*.json) — confirm a fresh session is allocated, persists on first turn.

Generated by Claude Code

Two user-reported bugs against PR #182's newest-first session
sort, both in the TUI start/navigate path:

Bug A -- sort drift when picking older sessions
SelectSession used to call PersistSession on the outgoing session
"to flush anything pending before the swap." But by the time you
get there, every meaningful state change has already been
persisted: PollLoopWorker's ApplyLoopResultTo saves after each
turn, ApplyModelSelection saves after /model, and so on. Pure
navigation had nothing to save -- but PersistSession's
unconditional FSession.Touch bumped Meta.UpdatedAt anyway, so
peeking at an older session re-promoted the one you LEFT to the
top of the newest-first list. Dropped the Persist call. Added a
comment explaining what's already persisted at each entry point
so a future hand doesn't re-add it on the theory it might miss
something.

Bug B -- TUI launches with empty chat, mismatched highlight
With no --session argument, the left pane highlighted the newest
session but the chat pane was blank. TSession.Create('') auto-
allocates a brand-new session id, so FSession pointed at a fresh
empty in-memory record while FSelSessIdx tracked the newest
persisted session at index 0. SessionId resolution in TTUI.Run
now does: empty + sessions on disk -> resume the newest by
UpdatedAt; empty + no sessions on disk -> fresh id (original
PR #117 behavior); non-empty -> honour as-is. Pressing N still
spawns a fresh session via StartNewSession -> SelectSession('').

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 23f4d4a3c5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

{ Persist anything pending on the current session before swapping. }
if (FSession <> nil) and (Length(FSession.Messages) > 0) then
PersistSession;
FSession.Free;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve in-flight prompts before swapping sessions

When the user submits a prompt and then opens another session before the loop finishes, StartTurn has only appended the user message to the in-memory FSession.Messages; it is not saved until the loop succeeds. After this change frees that object without persisting it, the timeout/failure paths call AppendErrorTo for FLoopSessionId, which reloads the original session from disk because the current session is different, so the saved transcript contains only the error and loses the prompt that triggered it. This also affects quitting after navigating away while the request is still pending. Avoid touching UpdatedAt for pure navigation, but keep a targeted save for an in-flight unsaved user turn before discarding the session object.

Useful? React with 👍 / 👎.

Codex P2 -- Preserve in-flight prompts before swapping sessions.
The previous fix dropped PersistSession from SelectSession to
stop pure navigation from bumping UpdatedAt. But it ALSO dropped
the persist in a case where the persist actually mattered: when
a tool loop is in flight on the outgoing session, StartTurn has
appended the user's prompt to in-memory FSession.Messages but
not to disk yet. If the user navigates away and the loop later
times out or fails, PollLoopWorker's AppendErrorTo reloads the
session by id, sees only the disk state (no prompt), appends the
error, saves -- so the persisted transcript contains the error
with no record of the prompt that triggered it.

Fix: gate the PersistSession on (FLoopThread <> nil) AND
(FLoopSessionId = FSession.Meta.Id). Pure navigation still
doesn't Touch; in-flight navigation does (and the UpdatedAt
bump is correct in that case -- there IS new content).

Tab overflow -- "1-2 chars bleed to next line" on long markdown
content. VisibleLength counts a tab as 1 column, but the terminal
expands it to the next 8-column tab stop (1-7 extra columns
depending on the cursor's current column). Markdown code blocks
from the model frequently include tab-indented content, so the
wrap math was undercounting visible width and the line ran past
the chat pane's right edge -- the terminal wrapped to col 0 of
the next physical row.

Fix: replace tabs with 4 spaces in the rendered body before the
wrap pass. Space-indented content passes through unchanged; tab-
indented content widens slightly but stays within the pane.
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