Skip to content

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

fix(chat): handle plan exit tool result in chat service

Steven Enamakel01df231c12d5
diff --git a/app/src/services/chatService.ts b/app/src/services/chatService.ts
index c66d47dd04..3118d69eec 100644
--- a/app/src/services/chatService.ts
+++ b/app/src/services/chatService.ts
@@ -1763,6 +1763,55 @@ export function subscribeQueueEvents(listeners: QueueEventListeners): () => void
};
}
+/** One follow-up suggestion (`ChatSuggestion` in `core/socketio.rs`). */
+export interface ChatSuggestionWire {
+ /** The message sent when the chip is picked. */
+ prompt: string;
+ /** A short 2-4 word button label for the chip (`web_chat/suggestions.rs`). */
+ label?: string | null;
+}
+
+/**
+ * `chat_suggestions`: follow-up prompts for the turn that just finished,
+ * emitted by the core after `chat_done` (`web_chat/suggestions.rs`). Best
+ * effort — a disabled, slow or malformed suggestions call means the event
+ * simply never arrives for that turn.
+ */
+export interface ChatSuggestionsEvent {
+ thread_id: string;
+ client_id?: string;
+ request_id?: string;
+ /** The turn the suggestions follow; the event fires outside its request. */
+ turn_request_id?: string;
+ suggestions: ChatSuggestionWire[];
+}
+
+export interface SuggestionEventListeners {
+ onSuggestions?: (event: ChatSuggestionsEvent) => void;
+}
+
+/** Subscribe to the core's `chat_suggestions` events; returns the unsubscribe. */
+export function subscribeSuggestionEvents(listeners: SuggestionEventListeners): () => void {
+ const eventName = 'chat_suggestions';
+ const cb = (payload: unknown) => {
+ const e = payload as Partial<ChatSuggestionsEvent> | null;
+ if (!e?.thread_id || !Array.isArray(e.suggestions)) {
+ chatLog('%s thread_id=%s dropped: malformed payload', eventName, e?.thread_id);
+ return;
+ }
+ chatLog(
+ '%s thread_id=%s turn_request_id=%s count=%d',
+ eventName,
+ e.thread_id,
+ e.turn_request_id,
+ e.suggestions.length
+ );
+ listeners.onSuggestions?.(e as ChatSuggestionsEvent);
+ };
+ socketService.on(eventName, cb);
+ return () => socketService.off(eventName, cb);
+}
+
/**
* Take one message out of a running turn's queue so it is never sent.
* `true` only when the core confirmed it; on `false` the item is still queued
diff --git a/crates/openhuman-core/src/agent/tools/plan_exit.rs b/crates/openhuman-core/src/agent/tools/plan_exit.rs
index 64486b1f04..db966f3605 100644
--- a/crates/openhuman-core/src/agent/tools/plan_exit.rs
+++ b/crates/openhuman-core/src/agent/tools/plan_exit.rs
@@ -93,14 +93,33 @@ impl PlanExitTool {
return Ok(ToolResult::error("`plan` must not be empty"));
}
if let Some(thread_id) = context.and_then(ToolRunContext::thread_id) {
- tracing::info!(
- thread_id = %thread_id,
- "[tool][plan_exit] flipping thread run mode to build"
- );
- crate::agent::tinyagents::run_mode::set_mode(
- thread_id,
- tinyagents_harness::middleware::RunMode::Build,
- );
+ // Only flip to Build when there is no plan review still parked on
+ // this thread. A review resolves (approve/reject/revise) before
+ // `request_plan_review` returns control to the agent, so by the
+ // time a well-behaved agent calls `plan_exit` after an approval
+ // the review is already gone from the gate's parked map — this
+ // check is a no-op there. It only matters for a mis-timed or
+ // concurrent `plan_exit` call that races a still-pending review:
+ // flipping the mode early would unlock every tool for the thread
+ // before the user has actually approved anything.
+ if crate::agent::plan_review::gate::global()
+ .parked_review_for_thread(thread_id)
+ .is_some()
+ {
+ tracing::warn!(
+ thread_id = %thread_id,
+ "[tool][plan_exit] a plan review is still parked on this thread — not flipping to build"
+ );
+ } else {
+ tracing::info!(
+ thread_id = %thread_id,
+ "[tool][plan_exit] flipping thread run mode to build"
+ );
+ crate::agent::tinyagents::run_mode::set_mode(
+ thread_id,
+ tinyagents_harness::middleware::RunMode::Build,
+ );
+ }
} else {
tracing::debug!("[tool][plan_exit] no thread id on this run context — nothing to flip");
}