Skip to content

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

fix(goals): correct continuation goal enrichment to use full goal context

Steven Enamakel44be859e5550
diff --git a/crates/openhuman-core/src/agent/goals/continuation.rs b/crates/openhuman-core/src/agent/goals/continuation.rs
deleted file mode 100644
index 9a90a8b7da..0000000000
--- a/crates/openhuman-core/src/agent/goals/continuation.rs
+++ /dev/null
@@ -1,222 +0,0 @@
-//! OpenHuman heartbeat adapter for tinyagents thread-goal continuation.
-//! `MaybeContinueIfIdle`).
-//!
-//! When a thread carries an **active** goal and goes idle — no in-flight turn
-//! and no activity for `goal_idle_minutes` — the heartbeat injects **one**
-//! continuation turn that keeps working the objective. The guards mirror Codex:
-//!
-//! - **Opt-in.** Off unless `heartbeat.goal_continuation_enabled` is set — an
-//! autonomous turn spends budget with no user present, so it defaults off
-//! (matching the project's stance on background automation).
-//! - **One-shot per idle period.** After a continuation fires, the goal is
-//! marked `continuation_suppressed`; a later user-initiated turn clears it
-//! (see [`super::runtime::account_turn_against_goal`]), so the next idle
-//! period can fire again. Prevents a tight self-driving loop.
-//! - **Serialised.** A process-wide `Semaphore(1)` ensures at most one
-//! continuation runs at a time.
-//! - **No in-flight turn.** Skipped while the thread has a live turn.
-//! - **Per-tick cap.** Bounds how many goals continue in a single tick.
-//!
-//! The continuation turn runs on the thread's own chat session (the cached
-//! agent, or one resumed from the thread's transcript) under a
-//! `TrustedAutomation { GoalContinuation }` origin, so the
-//! approval gate parks irreversible external actions (no present user to
-//! authorize) while read/compute work proceeds.
-
-use std::path::Path;
-use std::sync::OnceLock;
-
-use tokio::sync::Semaphore;
-
-use super::store;
-use super::{ThreadGoal, ThreadGoalStatus};
-use crate::agent::turn_origin::{AgentTurnOrigin, TrustedAutomationSource};
-use crate::config::Config;
-use crate::threads::turn_state::{TurnLifecycle, TurnStateStore};
-use crate::web_chat::SESSION_CHECKOUT_FAILURE;
-
-/// Serialise continuation dispatches so at most one autonomous goal turn runs at
-/// a time (Codex's `Semaphore(1)` guard).
-fn continuation_gate() -> &'static Semaphore {
- static GATE: OnceLock<Semaphore> = OnceLock::new();
- GATE.get_or_init(|| Semaphore::new(1))
-}
-
-/// Max continuation turns dispatched per heartbeat tick (bounds spend).
-const MAX_CONTINUATIONS_PER_TICK: usize = 2;
-
-/// Current unix time in milliseconds.
-fn now_ms() -> u64 {
- chrono::Utc::now().timestamp_millis().max(0) as u64
-}
-
-/// One heartbeat pass: find idle active goals and continue them. No-op unless
-/// `heartbeat.goal_continuation_enabled`.
-pub async fn run_continuation_tick(config: &Config) {
- if !config.heartbeat.goal_continuation_enabled {
- return;
- }
- let workspace_dir = config.workspace_dir.clone();
- let idle_ms = u64::from(config.heartbeat.goal_idle_minutes.max(1)) * 60_000;
- let now = now_ms();
-
- let goals = match store::list_all(&workspace_dir).await {
- Ok(g) => g,
- Err(e) => {
- tracing::warn!("[thread_goals] continuation tick: list_all failed: {e}");
- return;
- }
- };
-
- let mut candidates: Vec<ThreadGoal> = goals
- .into_iter()
- .filter(|g| g.status == ThreadGoalStatus::Active)
- .filter(|g| !g.continuation_suppressed)
- .filter(|g| now.saturating_sub(g.updated_at_ms) >= idle_ms)
- .collect();
- // Oldest-idle first so the most-neglected goal advances under the per-tick cap.
- candidates.sort_by_key(|g| g.updated_at_ms);
- candidates.truncate(MAX_CONTINUATIONS_PER_TICK);
-
- if candidates.is_empty() {
- tracing::debug!("[thread_goals] continuation tick: no idle active goals");
- return;
- }
- tracing::info!(
- "[thread_goals] continuation tick: {} idle candidate(s) (idle_min={})",
- candidates.len(),
- config.heartbeat.goal_idle_minutes.max(1)
- );
-
- for goal in candidates {
- if has_in_flight_turn(&workspace_dir, &goal.thread_id) {
- tracing::debug!(
- thread_id = %goal.thread_id,
- "[thread_goals] continuation skip: turn in flight"
- );
- continue;
- }
-
- // One continuation at a time. If the gate is busy, another continuation
- // is mid-flight — defer the rest to the next tick.
- let permit = match continuation_gate().try_acquire() {
- Ok(p) => p,
- Err(_) => {
- tracing::debug!("[thread_goals] continuation gate busy; deferring to next tick");
- return;
- }
- };
-
- let ran = dispatch_continuation(config, &goal).await;
-
- // One-shot: suppress further continuations on this goal until a
- // user-initiated turn clears the flag. Only when the turn actually ran,
- // and only if THIS goal is still current (compare-and-set on goal_id) —
- // a goal completed or replaced during the turn must not be suppressed.
- if ran {
- if let Err(e) = store::set_continuation_suppressed_if(
- &workspace_dir,
- &goal.thread_id,
- &goal.goal_id,
- true,
- )
- .await
- {
- tracing::debug!(
- thread_id = %goal.thread_id,
- error = %e,
- "[thread_goals] failed to set continuation_suppressed"
- );
- }
- }
- drop(permit);
- }
-}
-
-/// Whether `thread_id` currently has a live (started/streaming) turn.
-fn has_in_flight_turn(workspace_dir: &Path, thread_id: &str) -> bool {
- match TurnStateStore::new(workspace_dir.to_path_buf()).get(thread_id) {
- Ok(Some(ts)) => matches!(
- ts.lifecycle,
- TurnLifecycle::Started | TurnLifecycle::Streaming
- ),
- _ => false,
- }
-}
-
-/// The continuation message handed to the orchestrator.
-fn continuation_prompt(objective: &str) -> String {
- format!(
- "[goal continuation] You are resuming autonomous work toward this thread's goal — \
- no user is currently present.\n\nGoal: {objective}\n\n\
- Assess progress against concrete evidence, then take the next useful step. \
- Verify whether the goal is already satisfied. If yes, call `goal_complete` now. \
- If you are blocked or \
- need the user (e.g. an irreversible external action, missing input), stop and \
- summarise the blocker and the next step rather than guessing — external actions \
- are not auto-approved while you run unattended."
- )
-}
-
-/// Run a single continuation turn for `goal` on the thread's own chat session.
-/// Best-effort: failures are logged, never propagated (the heartbeat must keep
-/// ticking).
-///
-/// Returns `true` when a turn was attempted, `false` when the thread's session
-/// could not even be checked out (config or agent build failure) — the caller
-/// only suppresses further continuations when a turn actually ran.
-///
-/// Goes through `web_chat::run_system_turn_on_thread` rather than a throwaway
-/// orchestrator host: the continuation then resumes the thread's real history
-/// (a fresh host bound to the thread used to see nothing but its own prompt)
-/// and appends to the thread's transcript instead of writing a competing root
-/// transcript that the next cold-boot resume would prefer over the user's
-/// conversation.
-async fn dispatch_continuation(_config: &Config, goal: &ThreadGoal) -> bool {
- let thread_id = goal.thread_id.clone();
- tracing::info!(
- thread_id = %thread_id,
- goal_id = %goal.goal_id,
- "[thread_goals] dispatching continuation turn"
- );
-
- let prompt = continuation_prompt(&goal.objective);
- let run_id = format!("goal:{thread_id}");
- let origin = AgentTurnOrigin::TrustedAutomation {
- job_id: run_id.clone(),
- source: TrustedAutomationSource::GoalContinuation,
- };
-
- match crate::web_chat::run_system_turn_on_thread(&thread_id, &run_id, &prompt, origin).await {
- Ok(text) => {
- tracing::info!(
- thread_id = %thread_id,
- response_chars = text.chars().count(),
- "[thread_goals] continuation turn complete"
- );
- true
- }
- Err(e) if e.starts_with(SESSION_CHECKOUT_FAILURE) => {
- tracing::warn!(
- thread_id = %thread_id,
- error = %e,
- "[thread_goals] continuation: failed to check out the thread session"
- );
- false
- }
- Err(e) => {
- tracing::warn!(
- thread_id = %thread_id,
- error = %e,
- "[thread_goals] continuation turn failed"
- );
- // A turn was attempted regardless of Ok/Err — suppress so we don't
- // re-fire it every tick until the user re-engages.
- true
- }
- }
-}
-
-#[cfg(test)]
-#[path = "continuation_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-core/src/agent/goals/continuation_tests.rs b/crates/openhuman-core/src/agent/goals/continuation_tests.rs
deleted file mode 100644
index 36052538fc..0000000000
--- a/crates/openhuman-core/src/agent/goals/continuation_tests.rs
+++ /dev/null
@@ -1,47 +0,0 @@
-use super::*;
-
-fn config_with(enabled: bool, idle_minutes: u32, workspace: &Path) -> Config {
- let mut config = Config::default();
- config.workspace_dir = workspace.to_path_buf();
- config.heartbeat.goal_continuation_enabled = enabled;
- config.heartbeat.goal_idle_minutes = idle_minutes;
- config
-}
-
-#[tokio::test]
-async fn tick_is_noop_when_disabled() {
- let tmp = tempfile::tempdir().unwrap();
- // Active, long-idle goal present — but the feature is off.
- store::set(tmp.path(), "t", "obj", None).await.unwrap();
- let config = config_with(false, 1, tmp.path());
- // Must not panic / must return promptly; nothing to assert beyond no-op.
- run_continuation_tick(&config).await;
- // Goal untouched (not suppressed) since the feature is disabled.
- let g = store::get(tmp.path(), "t").await.unwrap().unwrap();
- assert!(!g.continuation_suppressed);
-}
-
-#[tokio::test]
-async fn candidate_filter_respects_status_idle_and_suppression() {
- // This exercises the selection predicate without dispatching a turn by
- // keeping the feature enabled but pointing at a fresh (non-idle) goal.
- let tmp = tempfile::tempdir().unwrap();
- // Fresh goal: updated_at is "now", so with a 60-min idle window it is
- // NOT a candidate and the tick stays a no-op (no agent build attempted).
- store::set(tmp.path(), "fresh", "obj", None).await.unwrap();
- let config = config_with(true, 60, tmp.path());
- run_continuation_tick(&config).await;
- let g = store::get(tmp.path(), "fresh").await.unwrap().unwrap();
- assert!(
- !g.continuation_suppressed,
- "fresh goal must not be continued/suppressed"
- );
-}
-
-#[test]
-fn continuation_prompt_names_objective_and_guards() {
- let p = continuation_prompt("ship the release");
- assert!(p.contains("ship the release"));
- assert!(p.contains("goal_complete"));
- assert!(p.contains("not auto-approved"));
-}
diff --git a/crates/openhuman-core/src/memory/goals/enrich.rs b/crates/openhuman-core/src/memory/goals/enrich.rs
index 2a6f9febe4..6f9053d756 100644
--- a/crates/openhuman-core/src/memory/goals/enrich.rs
+++ b/crates/openhuman-core/src/memory/goals/enrich.rs
@@ -7,9 +7,8 @@
//! several turns. On an empty list (first run) it bootstraps the list from
//! the context.
//!
-//! This mirrors the standalone background-agent spawn pattern used by the
-//! `subconscious` engine: build the agent from its registry definition, run
-//! a single external turn (which drives the full internal tool loop) under a
+//! It follows the standalone background-agent spawn pattern: build the agent
+//! from its registry definition, run a single external turn (which drives the full internal tool loop) under a
//! `TrustedAutomation` turn origin.
use std::path::Path;