fix: prevent app-wide crash on Pi messages with string content - #66
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Opening a Pi session that stores
system/developermessages with a stringcontentcrashes the whole UI: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.contentas a plain string instead of aContent[]:{"type":"message","id":"...","timestamp":"...","message":{"role":"system","content":"..."}}normalizeSessionEntry()returnstype === 'message'entries untouched, unlike the Claude/Codex converters, which normalise their content (src/utils/session.ts).SessionEntryRendererthen callscontentToText(entry.message.content)— i.e..filter(...)— forrole === "developer" | "system", andstripPreviewAssistantContent()in preview mode. A string has no.filter, so the throw unmounts the tree intoErrorBoundary.Reproduction
pi-session-cli+ browser, same frontend).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— newnormalizePiMessageEntry()/normalizePiContent(): coerce non-arraymessage.contentintoContent[]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— newasContentParts()guardingcontentToText(),stripPreviewAssistantContent()and thecontentprops passed toUserMessage/AssistantMessage. This is needed because live entries fromget_pi_agent_entriesbypass 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) — renderssystem,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:
npx tsc --noEmitis 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
pi-session-cli/ headless web UI.Array.isArraychecks also protectscomputeStats, session previews and branch/tree views, which all assumeContent[].