Skip to content

The agent that grows with you

fix: restore persisted assistant replies alongside tools and reasoning

Teknium42f83899871d
diff --git a/apps/desktop/src/lib/chat-messages.codex-json-sidecar.test.ts b/apps/desktop/src/lib/chat-messages.codex-json-sidecar.test.ts
new file mode 100644
index 00000000000..4bbe8a3e309
--- /dev/null
+++ b/apps/desktop/src/lib/chat-messages.codex-json-sidecar.test.ts
@@ -0,0 +1,32 @@
+import { expect, it } from 'vitest'
+
+import { chatMessageText, toChatMessages } from './chat-messages'
+
+it('hydrates the SQLite JSON-text sidecar returned by REST like the decoded RPC sidecar', () => {
+ const items = [{
+ type: 'message',
+ role: 'assistant',
+ phase: 'final_answer',
+ content: [{ type: 'output_text', text: 'Durable reply' }]
+ }]
+
+ const row = {
+ id: 1, role: 'assistant' as const, content: '', timestamp: 1, reasoning: 'Thinking before tool',
+ tool_calls: [{ id: 'call-1', type: 'function', function: { name: 'terminal', arguments: '{}' } }]
+ }
+
+ const decoded = toChatMessages([{ ...row, codex_message_items: items }])
+ const stored = toChatMessages([{ ...row, codex_message_items: JSON.stringify(items) }])
+
+ expect(stored).toEqual(decoded)
+ expect(chatMessageText(stored[0])).toBe('Durable reply')
+ const empty = { ...row, reasoning: null, tool_calls: undefined }
+ expect(toChatMessages([{ ...empty, codex_message_items: '{invalid' }])).toEqual([])
+ expect(toChatMessages([{ ...empty, display_kind: 'hidden', codex_message_items: JSON.stringify(items) }])).toEqual([])
+
+ for (const phase of ['analysis', 'commentary']) {
+ expect(toChatMessages([{ ...empty, codex_message_items: JSON.stringify([{ ...items[0], phase }]) }])).toEqual([])
+ }
+
+ expect(chatMessageText(toChatMessages([{ ...row, content: 'Canonical reply', codex_message_items: JSON.stringify(items) }])[0])).toBe('Canonical reply')
+})
diff --git a/apps/desktop/src/lib/chat-messages/hydration.ts b/apps/desktop/src/lib/chat-messages/hydration.ts
index 1321696b754..0d179c0f541 100644
--- a/apps/desktop/src/lib/chat-messages/hydration.ts
+++ b/apps/desktop/src/lib/chat-messages/hydration.ts
@@ -26,7 +26,16 @@ const CONTEXT_REF_RE = /@(file|folder|url|image|tool|terminal):(?:"[^"\n]+"|'[^'
* (codex_responses_adapter `_OutputScan._message`); the remaining phases are the reply.
*/
function codexMessageItemText(message: SessionMessage): string {
- const items = message.codex_message_items
+ let items = message.codex_message_items
+
+ // REST carries SQLite JSON text; RPC history carries the decoded list.
+ if (typeof items === 'string') {
+ try {
+ items = JSON.parse(items)
+ } catch {
+ return ''
+ }
+ }
if (!Array.isArray(items)) {
return ''
@@ -296,16 +305,9 @@ export function toChatMessages(messages: SessionMessage[]): ChatMessage[] {
)
}
- if (message.role === 'assistant' && Array.isArray(message.tool_calls)) {
- parts.push(
- ...message.tool_calls.map((call, callIndex) => toolPartFromStoredCall(call, callIndex, message.timestamp))
- )
- }
-
- // #68321: Responses-API turns can persist with `content` empty while the reply the
- // user saw lives only in codex_message_items; without this the rehydrated bubble
- // blanks and reconcileResumeMessages then strips the cached row at that ordinal.
- if (message.role === 'assistant' && !displayContent && !parts.length) {
+ // Reply text can live only in the sidecar alongside reasoning or tool parts.
+ // Those parts are not a substitute for the answer; canonical content still wins.
+ if (message.role === 'assistant' && message.display_kind !== 'hidden' && !displayContent) {
const codexText = codexMessageItemText(message)
if (codexText) {
@@ -313,6 +315,12 @@ export function toChatMessages(messages: SessionMessage[]): ChatMessage[] {
}
}
+ if (message.role === 'assistant' && Array.isArray(message.tool_calls)) {
+ parts.push(
+ ...message.tool_calls.map((call, callIndex) => toolPartFromStoredCall(call, callIndex, message.timestamp))
+ )
+ }
+
if (!parts.length && !extractedAttachmentRefs?.length) {
if (message.role !== 'assistant') {
flushPendingTools(index)
diff --git a/tests/tui_gateway/test_session_history_codex_sidecar.py b/tests/tui_gateway/test_session_history_codex_sidecar.py
index d20a7cb5c59..4ef23188d7f 100644
--- a/tests/tui_gateway/test_session_history_codex_sidecar.py
+++ b/tests/tui_gateway/test_session_history_codex_sidecar.py
@@ -24,6 +24,8 @@ def test_session_history_preserves_codex_message_items(tmp_path):
"assistant",
"",
codex_message_items=message_items,
+ reasoning="Thinking before the tool",
+ tool_calls=[{"id": "call-1", "type": "function", "function": {"name": "terminal", "arguments": "{}"}}],
)
previous_db = server._db
setattr(server, "_db", db)
diff --git a/tui_gateway/session_history.py b/tui_gateway/session_history.py
index 92f93739655..1a5cd76fca3 100644
--- a/tui_gateway/session_history.py
+++ b/tui_gateway/session_history.py
@@ -202,8 +202,6 @@ def _history_to_messages(history: list[dict]) -> list[dict]:
except (json.JSONDecodeError, TypeError):
args = {}
tool_call_args[tc_id] = (fn["name"], args)
- if not content_text.strip():
- continue
if role == "tool":
tc_name, tc_args = tool_call_args.get(m.get("tool_call_id") or "", (None, None))
name = tc_name or m.get("tool_name") or "tool"
diff --git a/website/docs/developer-guide/session-storage.md b/website/docs/developer-guide/session-storage.md
index 0d1b2e51b5e..36f4ed2fe26 100644
--- a/website/docs/developer-guide/session-storage.md
+++ b/website/docs/developer-guide/session-storage.md
@@ -138,7 +138,7 @@ CREATE INDEX IF NOT EXISTS idx_messages_session_id ON messages(session_id, id);
Notes:
- `tool_calls` is stored as a JSON string (serialized list of tool call objects)
- `reasoning_details`, `codex_reasoning_items`, and `codex_message_items` are stored as JSON strings
-- Desktop history hydration retains assistant sidecars in both REST and JSON-RPC (`session.resume`, `session.activate`, `session.history`) projections. A Responses reply may live only in `codex_message_items` while `content` is empty; that is not an empty transcript row.
+- Desktop history hydration retains assistant sidecars in both REST and JSON-RPC (`session.resume`, `session.activate`, `session.history`) projections, including rows with reasoning and tool calls. REST may return the SQLite JSON string while RPC returns decoded items; Desktop accepts both. A final Responses reply may live only in `codex_message_items` while `content` is empty. Canonical content still takes precedence, and analysis/commentary items are not promoted to reply text.
- `reasoning` stores the raw reasoning text for providers that expose it
- `api_content` is a byte-fidelity sidecar: the exact content string sent to the API for this message when it differs from `content` (ephemeral memory/plugin injections, persist overrides). It preserves the wire bytes for prompt-cache-stable replay — stored as sent, except lone surrogates, which sqlite3 cannot bind and which the conversation loop scrubs from every outgoing payload anyway. `NULL` means `content` was sent verbatim.
- Timestamps are Unix epoch floats (`time.time()`)