Skip to content

fix: prevent app-wide crash on Pi messages with string content - #66

Merged
Dwsy merged 1 commit into
Dwsy:mainfrom
lincooln-ai:fix/pi-string-message-content
Sep 20, 2026
Merged

Dwsy merged 1 commit into
Dwsy:mainfrom
lincooln-ai:fix/pi-string-message-content

Conversation

@lincooln-ai

Copy link
Copy Markdown
Contributor

Summary

Opening a Pi session that stores system / developer messages with a string content crashes the whole UI:

Something went wrong
e.filter is not a function

The error boundary replaces the entire app, so the session list becomes unreachable until a reload. This PR fixes it once at the parse boundary (root cause) and once in the entry renderer (last line of defence).

Root cause

Pi persists some roles with message.content as a plain string instead of a Content[]:

{"type":"message","id":"...","timestamp":"...","message":{"role":"system","content":"..."}}
  1. normalizeSessionEntry() returns type === 'message' entries untouched, unlike the Claude/Codex converters, which normalise their content (src/utils/session.ts).
  2. SessionEntryRenderer then calls contentToText(entry.message.content) — i.e. .filter(...) — for role === "developer" | "system", and stripPreviewAssistantContent() in preview mode. A string has no .filter, so the throw unmounts the tree into ErrorBoundary.
TypeError: e.filter is not a function
    at contentToText (SessionEntryRenderer.tsx)
    at renderSessionEntry (SessionEntryRenderer.tsx)

Reproduction

  1. Open a Pi session whose JSONL contains a message with a string body — e.g. any session written by a current Pi version that persists the system prompt.
  2. Click it in the session list (Tauri desktop app or pi-session-cli + browser, same frontend).
  3. Result: Something went wrong / e.filter is not a function.

Affected sessions are not rare: 4 of 55 sessions in my library have this shape, and the crash is deterministic per session.

Fix

  • src/utils/session.ts — new normalizePiMessageEntry() / normalizePiContent(): coerce non-array message.content into Content[] while parsing, so every consumer (renderer, previews, stats, plugins) sees a single shape. Already-array content is returned untouched, so the hot path is unaffected.
  • src/components/session-viewer/SessionEntryRenderer.tsx — new asContentParts() guarding contentToText(), stripPreviewAssistantContent() and the content props passed to UserMessage / AssistantMessage. This is needed because live entries from get_pi_agent_entries bypass the JSONL parser, and a renderer should never be able to blank the whole app on unexpected session data.

Tests

  • src/utils/session.test.ts — parser invariant: string bodies become text parts, empty strings become [], arrays stay untouched, and no emitted entry carries a non-array body.
  • src/components/session-viewer/SessionEntryRenderer.test.tsx (new) — renders system, developer, empty-string, user and assistant-preview entries whose content is a raw string.

Verified the tests are meaningful — with the source fix reverted, all six fail with the production error:

# with fix
Test Files  2 passed (2)
     Tests  21 passed (21)

# fix reverted
Test Files  2 failed (2)
     Tests  6 failed | 15 passed (21)
  TypeError: content.filter is not a function

npx tsc --noEmit is clean. Full frontend suite: 768 passed; the 4 failures (extensions/pi-session-bridge/src/kanban-store.test.ts, extensions/pi-session-bridge/src/tools.test.ts, src/hooks/app/useUpdateChecker.test.tsx) also fail on a clean tree and are unrelated to this change.

Notes

  • No Rust changes — the frontend fix covers both the Tauri build and the pi-session-cli / headless web UI.
  • Normalising at the parse boundary rather than sprinkling Array.isArray checks also protects computeStats, session previews and branch/tree views, which all assume Content[].

Pi persists some message roles (`system` / `developer`) with
`message.content` as a plain string instead of a `Content[]`. Every consumer
of `entry.message.content` assumes an array, so opening such a session tripped
the app-wide error boundary with:

    TypeError: e.filter is not a function
      at contentToText (SessionEntryRenderer.tsx)
      at renderSessionEntry (SessionEntryRenderer.tsx)

Normalize non-array message content once at the parse boundary
(`normalizeSessionEntry` -> `normalizePiMessageEntry`) so renderers, previews
and stats all see a consistent shape. Already-array content is returned
untouched, so the hot path is unaffected.

Also coerce defensively inside `SessionEntryRenderer`, which is the last line
of defence before a bad payload blanks the UI: live Pi entries from
`get_pi_agent_entries` bypass the JSONL parser entirely.

Repro entry (one per affected session):

    {"type":"message","message":{"role":"system","content":"..."}}

Tests: parser normalization + renderer regression tests; all six fail with
`content.filter is not a function` when the fix is reverted.
Copilot AI lite review requested due to automatic review settings September 20, 2026 03:47

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Dwsy
Dwsy merged commit bdc5a20 into Dwsy:main Sep 20, 2026
1 check passed
@lincooln-ai
lincooln-ai deleted the fix/pi-string-message-content branch September 20, 2026 06:18
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.

3 participants