openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

tech-debt: make sub-agent execution context explicit (retire the current_parent() task-local footgun)

Open oxoxDev opened this issue 5 months ago • 3 comments

Problem

Sub-agent spawning reads its required runtime context (provider, tools, memory, model, session) from a task-local global — PARENT_CONTEXT / current_parent() (src/openhuman/agent/harness/fork_context.rs). Any spawn entry point that runs without an enclosing agent turn must remember to install a root context first via build_root_parent + with_parent_context, or the spawn fails at runtime with SubagentRunError::NoParentContext (subagent_runner/ops/runner.rs:123) — there is no compile-time guarantee.

This is the Tangled Context / global-state anti-pattern: a required dependency is smuggled through ambient task-local state instead of being passed explicitly. Forgetting it is a silent production failure, not a build error.

Evidence it bites

  • TAURI-RUST-HMW (1.4k events / 71 users): the subconscious tick forgot the wrap → every world-changing tick's context-scout spawn died with NoParentContext. Fixed narrowly by #NNN_HMW (bounded — see below).
  • The wrap is copy-pasted across every config-from-scratch background surface: workflow_runs/engine.rs, agent_teams/runtime.rs, triage/escalation.rs, and now subconscious. Duplication is the smell.

Scope of a full fix (large)

current_parent() is read at ~35 sites across 24 files, including ordinary leaf tools (todo, update_task, monitor, agent_memory, delegate_to_personality) that read it inside Tool::execute. The task-local exists specifically to avoid widening the Tool trait (fork_context.rs:1-3). A true "explicit parameter everywhere" refactor therefore means threading &ParentExecutionContext (or an execution-environment handle) through the Tool::execute signature of every tool — a whole-surface, multi-week change with large blast radius. Needs maintainer buy-in and its own PR series; must not ride a bug-fix PR.

Bounded step already taken (#NNN_HMW)

Introduced with_root_parent(config, id, channel, prefix, fut) in agent_orchestration/parent_context — one blessed entry point that does build_root_parent + with_parent_context — and migrated the config-from-scratch root-parent surfaces (workflow_runs, agent_teams, subconscious) to it. This makes "forgot to install the root parent" structurally impossible for orchestration surfaces without touching the Tool trait. The cross-spawn inherited-parent re-install sites (spawn_async_subagent, spawn_worker_thread, ops) are a different pattern and stay.

Proposed full direction (this epic)

  1. Define a first-class ExecutionEnv (provider/tools/memory/model/session) passed explicitly into spawn entry points.
  2. Make run_subagent take it as an argument; keep a thin task-local shim only at the Tool::execute boundary during migration.
  3. Migrate leaf tools off current_parent() incrementally; delete the task-local once readers are gone.
  4. Turn NoParentContext from a runtime error into a type-level impossibility.

Acceptance

  • No spawn path can compile without an execution context in hand.
  • current_parent() / PARENT_CONTEXT removed (or reduced to a documented compatibility shim with a tracked removal date).

Implementation Plan

Triage Assessment

Valid epic tracking a real footgun: the current_parent() task-local global causes runtime NoParentContext failures (1.4k Sentry events / 71 users from the subconscious tick alone). The decomposition into sub-tasks is already correct and explicit in the issue comments: #4340 (bounded fix, merged), #4369 (collapse escalation+delegation onto with_root_parent, closed), #4370 (ExecutionEnv), #4371 (leaf-tool migration). This epic is NOT a duplicate of #4370 or #4371 -- it is the parent tracking issue.

Proposed Scope

  • Track completion of sub-tasks #4370 (ExecutionEnv definition + spawn entry points) and #4371 (leaf-tool migration).
  • Close this epic once all sub-tasks are resolved.
  • (Sub-tasks #4340 and #4369 are already done.)

Implementation Steps

  1. #4370 (follow-up B): define ExecutionEnv, thread into run_subagent / spawn_agent / follow_up entry points. Keep thin task-local shim at Tool::execute boundary. Waiting for maintainer buy-in per the design RFC in #4370.
  2. #4371 (follow-up C): migrate ~18 leaf-tool current_parent() readers off the task-local. Widen Tool::execute or use dispatch-level passing per maintainer decision from #4370. Delete PARENT_CONTEXT once migrated.
  3. Close #4336 once both sub-tasks are resolved.

Verification

  • #4370: entry points accept ExecutionEnv argument; no NoParentContext possible at those sites.
  • #4371: PARENT_CONTEXT / current_parent() / with_parent_context deleted; NoParentContext is compile-time impossible.

Risks and Open Questions

  • The epic's decomposition is already published and accurate. No additional planning needed.
  • Maintainer buy-in needed for #4370 before work can proceed.

Acceptance Criteria

  • [ ] #4370 and #4371 are resolved.
  • [ ] No spawn path compiles without explicit execution context.
  • [ ] current_parent() / PARENT_CONTEXT removed.

oxoxDev avatar Jun 30 '26 10:06 oxoxDev

Epic decomposition

The bounded first step is already in flight as #4340 (fix(subconscious): establish root parent for context_scout grounding, Closes #4337): it introduces with_root_parent(...) in agent_orchestration/parent_context and migrates the config-from-scratch background surfaces (workflow_runs, agent_teams, subconscious) onto it. That kills the copy-pasted build_root_parent → with_parent_context dance for those surfaces without touching the Tool trait — so this epic stays open for the remaining phases:

  • #4369 — collapse escalation + delegation onto the shared builder / with_root_parent (field-parity check). The inherited-parent sites #4340 intentionally left untouched are a different pattern; this is the remaining root-build duplication.
  • #4370 — define a first-class ExecutionEnv, thread it into the spawn entry points (large, needs maintainer buy-in).
  • #4371 — migrate ~18 leaf-tool current_parent() readers, delete the task-local, make NoParentContext a compile error (PR series, gated by #4370).

Order: #4340 (bounded, open) → #4369 (parallel-ish) → #4370 → #4371.

oxoxDev avatar Jul 01 '26 09:07 oxoxDev

Triage: valid — Current review confirms this is a concrete, in-scope engineering issue or request: “tech-debt: make sub-agent execution context explicit (retire the current_parent() task-local footgun)”. No complete resolution is evident in current main.

senamakel avatar Oct 09 '26 17:10 senamakel

Thanks for driving this. The main goal landed in #7823: the PARENT_CONTEXT task-local and current_parent() are gone, and the parent context is now passed explicitly (run_context.parent). One piece is left: NoParentContext is still a runtime error rather than impossible by construction. That's tracked in #4371, so we're closing this epic.

M3gA-Mind avatar Nov 16 '26 16:11 M3gA-Mind