Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(chat): settle orphaned timeline entries when turn snapshot fetch fails

Steven Enamakele4bdd7699c99
diff --git a/app/src/providers/ChatRuntimeProvider.tsx b/app/src/providers/ChatRuntimeProvider.tsx
index b1af79468d..1825bee57f 100644
--- a/app/src/providers/ChatRuntimeProvider.tsx
+++ b/app/src/providers/ChatRuntimeProvider.tsx
@@ -33,6 +33,7 @@ import { store } from '../store';
import {
appendSubagentStreamDelta,
bumpInferenceHeartbeatForThread,
+ cancelUnresolvedTurnTimeline,
clearInferenceStatusForThread,
clearParallelRequest,
clearPendingApprovalForThread,
@@ -645,7 +646,12 @@ const ChatRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
// parent and sub-agent event from the whole turn. Doing this here (after
// ending the live lifecycle) matters: `hydrateRuntimeFromSnapshot`
// intentionally refuses to overwrite an actively streaming turn.
- await dispatch(fetchAndHydrateCompletedTurnState(event.thread_id));
+ const completedSnapshot = await dispatch(
+ fetchAndHydrateCompletedTurnState(event.thread_id)
+ ).unwrap();
+ if (!completedSnapshot) {
+ dispatch(cancelUnresolvedTurnTimeline({ threadId: event.thread_id }));
+ }
};
rtLog('subscribe_chat_events', { socket: socketStatus });
diff --git a/app/src/store/chatRuntimeSlice.ts b/app/src/store/chatRuntimeSlice.ts
index ca9b978029..a95431f12c 100644
--- a/app/src/store/chatRuntimeSlice.ts
+++ b/app/src/store/chatRuntimeSlice.ts
@@ -1783,6 +1783,20 @@ const chatRuntimeSlice = createSlice({
entry.status = 'cancelled';
if (entry.subagent) entry.subagent.status = 'cancelled';
},
+ /**
+ * Settle rows whose terminal turn snapshot could not be fetched.
+ *
+ * `chat_done` means their event driver has stopped. A non-async row still
+ * marked `running` therefore has no remaining source that can truthfully
+ * complete it, while detached sub-agents intentionally outlive the parent
+ * turn and must remain owned by their run ledger.
+ */
+ cancelUnresolvedTurnTimeline: (state, action: PayloadAction<{ threadId: string }>) => {
+ const { threadId } = action.payload;
+ const entries = state.toolTimelineByThread[threadId];
+ if (!entries) return;
+ state.toolTimelineByThread[threadId] = entries.map(settleOrphanedTimelineEntry);
+ },
/**
* Append a streamed `subagent_text_delta` / `subagent_thinking_delta`
* chunk to the ordered transcript of the matching subagent row. The row
@@ -2479,6 +2493,7 @@ export const {
clearProcessingForThread,
appendProcessingProse,
markSubagentCancelled,
+ cancelUnresolvedTurnTimeline,
appendSubagentStreamDelta,
recordSubagentTranscriptTool,
resolveSubagentTranscriptTool,
diff --git a/crates/openhuman-core/src/threads/transcript_view/project.rs b/crates/openhuman-core/src/threads/transcript_view/project.rs
index 27dcd5f44b..ac863c3fb1 100644
--- a/crates/openhuman-core/src/threads/transcript_view/project.rs
+++ b/crates/openhuman-core/src/threads/transcript_view/project.rs
@@ -96,13 +96,17 @@ pub fn project_from_files(
retained.len(),
kept.len()
);
- let request_id = kept.iter().find_map(|record| match record {
- DisplayRecord::Message(msg) => msg.request_id.clone(),
+ let first_kept = kept.iter().find_map(|record| match record {
+ DisplayRecord::Message(msg) => Some(msg),
DisplayRecord::Compaction(_) => None,
});
+ let request_id = first_kept.and_then(|message| message.request_id.clone());
records.push(DisplayRecord::Compaction(CompactionMarker {
replacement: retained,
- ts: Some(display.meta.created.clone()).filter(|ts| !ts.is_empty()),
+ ts: first_kept
+ .and_then(|message| message.ts.clone())
+ .or_else(|| Some(display.meta.created.clone()))
+ .filter(|ts| !ts.is_empty()),
request_id,
}));
records.extend(kept);
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge.rs b/crates/openhuman-core/src/web_chat/progress_bridge.rs
index d32d9f8442..f2fcf9ddb3 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge.rs
@@ -1435,6 +1435,10 @@ pub(crate) fn spawn_progress_bridge(
// #3886: seal any spans still open after the stream closed and hand the
// run's trace to the configured tracing sink. Best-effort and gated;
// never affects the turn outcome.
+ // The response presenter waits only briefly for this signal before
+ // publishing an error. Trace export is best-effort I/O and must not
+ // hold up terminal delivery after the progress stream closed.
+ let _ = drained_tx.send(true);
if let Some(mut collector) = span_collector.take() {
collector.finish(unix_epoch_ms());
let live_spans = collector.spans().to_vec();
@@ -1473,7 +1477,6 @@ pub(crate) fn spawn_progress_bridge(
round,
events_seen,
);
- let _ = drained_tx.send(true);
});
ProgressBridgeHandle {
drained: drained_rx,