Skip to content

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

fix(chat): use persisted message id for proactive replies

Steven Enamakelf9ffa5603af5
diff --git a/app/src/providers/ChatRuntimeProvider.tsx b/app/src/providers/ChatRuntimeProvider.tsx
index b9eade0e81..ee14ea7395 100644
--- a/app/src/providers/ChatRuntimeProvider.tsx
+++ b/app/src/providers/ChatRuntimeProvider.tsx
@@ -340,13 +340,17 @@ function deliveredReplyMessageId(event: {
* Message id for a proactive message addressed to a real thread.
*
* Origin-bound cron delivery persists its reply into the conversation that
- * asked for it under `agent:<request_id>` before emitting `proactive_message`
- * with that thread id. Reusing the id collapses our append onto the core's
- * row. Synthetic `proactive:*` ids have no core row, so they keep generated ids.
+ * asked for it and names that row in `persisted_message_id` before emitting
+ * `proactive_message`. Reusing the id collapses our append onto the core's
+ * row. Without the field no row exists (a turn id is not unique per message),
+ * so a generated id is used.
*/
-function proactiveMessageId(event: { thread_id: string; request_id?: string }): string | undefined {
- if (event.thread_id.startsWith('proactive:') || !event.request_id) return undefined;
- return `agent:${event.request_id}`;
+function proactiveMessageId(event: {
+ thread_id: string;
+ persisted_message_id?: string;
+}): string | undefined {
+ if (event.thread_id.startsWith('proactive:')) return undefined;
+ return event.persisted_message_id || undefined;
}
/**
diff --git a/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx b/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx
index 2cf16e1d2e..dc4d677dc6 100644
--- a/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx
+++ b/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx
@@ -1133,6 +1133,7 @@ describe('ChatRuntimeProvider — dedupe, proactive resolution, mid-turn invaria
thread_id: 'asking-thread',
request_id: 'cron:job-1:run-1',
full_response: 'time to drink water',
+ persisted_message_id: 'agent:cron:job-1:run-1',
});
});
@@ -1146,6 +1147,30 @@ describe('ChatRuntimeProvider — dedupe, proactive resolution, mid-turn invaria
expect(store.getState().thread.selectedThreadId).toBe('other-thread');
});
+ it('generates its own id for a real-thread proactive message the core did not persist', async () => {
+ store.dispatch(
+ loadThreads.fulfilled(
+ { threads: [{ id: 'real-thread', title: 'x', messageCount: 2 }] as never, count: 1 },
+ 'req-id',
+ undefined
+ )
+ );
+ store.dispatch(setSelectedThread('real-thread'));
+ const listeners = renderProvider();
+
+ await act(async () => {
+ listeners.onProactiveMessage?.({
+ thread_id: 'real-thread',
+ request_id: 'turn-1',
+ full_response: 'first',
+ });
+ });
+
+ await waitFor(() => expect(threadApi.appendMessage).toHaveBeenCalled());
+ const sent = vi.mocked(threadApi.appendMessage).mock.calls.at(-1)?.[1] as { id: string };
+ expect(sent.id).not.toBe('agent:turn-1');
+ });
+
it('creates a new thread when no visible thread exists for proactive handoff', async () => {
vi.mocked(threadApi.createNewThread).mockResolvedValue({
id: 'created-thread',
diff --git a/app/src/services/chatService.ts b/app/src/services/chatService.ts
index 2f6f2f2139..7a58331138 100644
--- a/app/src/services/chatService.ts
+++ b/app/src/services/chatService.ts
@@ -300,6 +300,8 @@ export interface ProactiveMessageEvent {
thread_id: string;
request_id?: string;
full_response: string;
+ /** Id of the thread row the core already stored for this message, if any. */
+ persisted_message_id?: string;
}
/**
diff --git a/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs b/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs
index 1995f2133f..0e5e2f0c97 100644
--- a/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs
+++ b/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs
@@ -217,6 +217,7 @@ async fn deliver_to_web_thread(
request_id: request_id.clone(),
full_response: Some(text.to_string()),
success: Some(true),
+ persisted_message_id: Some(crate::threads::store::run_reply_message_id(&request_id)),
..Default::default()
});
diff --git a/crates/openhuman-core/src/web_chat/channel_event.rs b/crates/openhuman-core/src/web_chat/channel_event.rs
index 3444c05a64..3daa411eae 100644
--- a/crates/openhuman-core/src/web_chat/channel_event.rs
+++ b/crates/openhuman-core/src/web_chat/channel_event.rs
@@ -96,6 +96,12 @@ pub struct WebChannelEvent {
/// Total number of segments in a segmented delivery.
#[serde(skip_serializing_if = "Option::is_none")]
pub segment_total: Option<u32>,
+ /// Id of the thread row the producer already persisted for this message
+ /// (`proactive_message` only). A client appending the message reuses it so
+ /// its append collapses onto the stored row; absent means no row exists
+ /// and the client generates its own id.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub persisted_message_id: Option<String>,
/// Fine-grained streaming payload for `text_delta`, `thinking_delta`,
/// and `tool_args_delta` events. Concatenating `delta`s in order
/// yields the full text/thinking/arguments string.