openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

tech-debt: define first-class ExecutionEnv, thread it into sub-agent spawn entry points

Open oxoxDev opened this issue 5 months ago • 3 comments

Parent

Follow-up B of epic #4336. Large — needs maintainer buy-in before code. This is phases 1–2 of the epic's proposed direction. Should land as its own PR (series), not bundled with a bug fix.

Problem

Spawn entry points read their required runtime context (provider, tools, memory, model, session) from the PARENT_CONTEXT task-local via current_parent(). There is no compile-time guarantee a context is installed, so any entry point reachable without an enclosing agent turn fails at runtime with NoParentContext:

  • subagent_runner/ops/runner.rs:122 — run_subagent()
  • agent_orchestration/ops.rs:103 — spawn_agent()
  • agent_orchestration/ops.rs:297 — follow_up()

Scope

  1. Define a first-class ExecutionEnv (provider / tools / memory / model / session) — the explicit form of what ParentExecutionContext smuggles through the task-local.
  2. Make run_subagent / spawn_agent / follow_up take ExecutionEnv (or a handle) as an explicit argument.
  3. Keep a thin task-local shim only at the Tool::execute boundary during migration (leaf-tool readers are migrated in follow-up C).
  4. Update the "stay" re-install sites (spawn_async_subagent, spawn_worker_thread, orchestration background tasks) to capture + move the env into the spawned task explicitly instead of re-installing the task-local.

Non-goals (deferred to C)

  • Widening the Tool::execute signature.
  • Deleting PARENT_CONTEXT / current_parent().

Acceptance

  • Spawn entry points accept an execution env argument; NoParentContext at those entry points becomes an "arg in hand" invariant.
  • Task-local remains only as the documented Tool::execute-boundary shim.

Related

  • Epic: #4336
  • Gates: #4336 follow-up C (leaf-tool migration)

Implementation Plan

Triage Assessment

Valid sub-task of epic #4336. NOT a duplicate of #4336 (epic) or #4371 (sub-task C). The issue body has a detailed design RFC from oxoxDev awaiting maintainer buy-in before coding. The scope is clear: define ExecutionEnv, thread into the 3 spawn entry points (run_subagent, spawn_agent, follow_up), and keep a thin task-local shim at the Tool::execute boundary.

Proposed Scope

  • Define ExecutionEnv struct (provider/tools/memory/model/session -- the explicit form of what ParentExecutionContext smuggles).
  • Thread into run_subagent / spawn_agent / follow_up as explicit argument.
  • Keep thin task-local shim at Tool::execute boundary for ~18 leaf-tool readers.
  • Update "stay" re-install sites to capture+move ExecutionEnv.
  • Do NOT widen Tool::execute or delete PARENT_CONTEXT (deferred to #4371).

Implementation Steps

  1. Define ExecutionEnv (src/openhuman/agent/harness/): a Clone+Send struct carrying provider, tool registry, tool specs, visible-tool set, memory handle, model/temperature, workspace, session identity, channel, integrations, progress sink, run queue. This is a rename+relocate of ParentExecutionContext -- not a new parallel type -- to prevent drift.
  2. Thread into spawn entry points (src/openhuman/agent/harness/subagent_runner/ops/runner.rs, src/openhuman/agent_orchestration/ops.rs): change run_subagent, spawn_agent, follow_up signatures to take &ExecutionEnv. The existing spawn_agent_with_definition(parent, definition, request) pattern already partially exists internally -- this formalizes it.
  3. Task-local shim (src/openhuman/agent/harness/fork_context.rs): keep a shim with_current_env(env, fut) that installs the task-local for the Tool::execute boundary. Leaf tools continue calling current_env() (renamed from current_parent()).
  4. "Stay" sites: rework spawn_async_subagent, spawn_worker_thread, orchestration background tasks to capture+move ExecutionEnv into spawned task instead of re-installing inherited task-local.

Verification

  • Unit: spawn entry points called without env fails to compile (type error, not runtime).
  • Unit: leaf tools still work through the shim (no regression for ~18 readers).
  • Integration: recursive sub-agent spawns propagate env correctly.

Risks and Open Questions

  • Maintainer feedback needed on the design RFC's §5 question (how to eventually widen Tool::execute -- (a) widen trait, (b) execute_with_env default, or (c) dispatch-level passing). This choice defines #4371's shape but does not block this issue.
  • ExecutionEnv = rename+relocate of ParentExecutionContext, or new inner struct? (Open question 1 in comment).

Acceptance Criteria

  • [ ] ExecutionEnv defined as explicit struct.
  • [ ] run_subagent, spawn_agent, follow_up take ExecutionEnv argument.
  • [ ] NoParentContext impossible at entry points (compile-time).
  • [ ] Task-local shim at Tool::execute boundary (leaf tools unaffected).
  • [ ] "Stay" sites capture+move env instead of re-installing inherited task-local.

oxoxDev avatar Jul 01 '26 09:07 oxoxDev

Design / RFC — ExecutionEnv (for maintainer buy-in before code)

Posting the design here first per this issue's own gate ("needs maintainer buy-in and its own PR series"). No code until this is signed off. Grounded in the current tree (post-#4340, which adds with_root_parent).

1. What exists today

  • Spawn entry points read runtime deps (provider / tools / memory / model / session) from the PARENT_CONTEXT tokio task-local via current_parent(); if unset → runtime NoParentContext:
    • subagent_runner/ops/runner.rs run_subagent()
    • agent_orchestration/ops.rs spawn_agent() (l.103) and follow_up() (l.297)
  • 27 files read current_parent() — the spawn entry points, ~18 leaf-tool Tool::execute bodies, and the re-install sites.
  • Tool::execute(&self, args) -> Result<ToolResult> (tools/traits.rs) is deliberately not widened — the task-local exists precisely to avoid touching 100+ tool impls.
  • Encouraging: the explicit-parameter pattern already partially exists internally — spawn_agent() immediately delegates to spawn_agent_with_definition(parent, definition, request). The public method is just a thin task-local reader in front of an already-explicit core.

2. Proposed ExecutionEnv

A first-class, cloneable handle carrying exactly the runtime deps ParentExecutionContext smuggles today (provider, tool registry + specs, visible-tool set, memory, model/temperature, workspace, session identity, channel, integrations, progress sink, run queue). Conceptually ParentExecutionContext becomes (or wraps) ExecutionEnv — this is a rename+relocate, not a new parallel type, to avoid drift.

3. Spawn entry points take it explicitly

run_subagent(env: &ExecutionEnv, definition, prompt, opts)
Orchestrator::spawn_agent(&self, env: &ExecutionEnv, request)
Orchestrator::follow_up(&self, env: &ExecutionEnv, request)

NoParentContext at these three sites becomes a compile-time "env in hand" invariant (arg, not runtime lookup). with_root_parent (from #4340) keeps building the root env for background surfaces; it just hands the env in explicitly instead of installing a task-local.

4. Migration strategy (the crux — keeps the Tool trait untouched during B)

Phase B does not widen Tool::execute. Instead:

  1. Introduce ExecutionEnv; thread it explicitly through the spawn entry points + orchestrator internals (which already pass parent around).
  2. Keep a thin task-local shim only at the Tool::execute boundary: with_current_env(env, fut) installs it, leaf tools keep calling current_env() (renamed current_parent()) unchanged. So no leaf-tool churn in B.
  3. The "stay" re-install sites (spawn_async_subagent, spawn_worker_thread, orchestration ops background tasks) switch from re-installing an inherited task-local to capturing + moving the ExecutionEnv into the spawned task, then re-installing the shim at the boundary.

Result after B: every spawn entry point is explicit and compile-checked; the task-local survives only as the documented Tool::execute-boundary shim. That shim's removal is #4371 (leaf-tool migration), which needs the Tool::execute widening decision below.

5. The Tool::execute widening question (deferred to #4371, but decide the mechanism now)

Options for how leaf tools eventually receive the env explicitly:

  • (a) Widen the trait — execute(&self, args, env: &ExecutionEnv). Cleanest end-state, but touches 100+ impls (big mechanical PR, most tools ignore env).
  • (b) execute_with_env default method — new method defaulting to execute(args); only delegating tools override. Incremental, no mass churn, but two methods coexist.
  • (c) Env handle at dispatch — pass env through the dispatcher to only the ~18 tools that actually need it, via a narrower DelegatingTool sub-trait. Smallest blast radius; keeps plain tools untouched forever.

Recommendation: (b) or (c) to avoid a 100-impl churn; (a) only if the maintainer wants the uniform signature. This choice defines #4371's shape.

6. Phasing

  • B (this issue #4370): define ExecutionEnv, thread into the 3 spawn entry points + "stay" sites, keep the Tool::execute shim. Self-contained, no leaf-tool churn, NoParentContext gone from entry points.
  • C (#4371): widen/dispatch per §5, migrate the ~18 leaf readers, delete the task-local shim, make NoParentContext unrepresentable.

7. Open questions for maintainer

  1. ExecutionEnv = rename/relocate of ParentExecutionContext, or a new inner struct it wraps?
  2. Preferred §5 mechanism (a/b/c) — sets #4371's scope.
  3. OK to land B as a single PR (entry points + "stay" sites + shim), or split entry-points vs stay-sites?
  4. Any surface I've missed that spawns outside the 3 entry points?

cc epic #4336. Will not start coding B until this is approved.

oxoxDev avatar Jul 02 '26 08:07 oxoxDev

Triage: needs opinion — “tech-debt: define first-class ExecutionEnv, thread it into sub-agent spawn entry points” needs a maintainer decision on product scope/priority or fresh reproduction evidence before its status can be settled. Please advise whether to pursue, narrow, or close it.

senamakel avatar Oct 09 '26 17:10 senamakel

Thanks for the RFC. The same problem was solved a different way in #7823: spawn entry points now receive the parent context explicitly (run_context.parent: Option<ParentExecutionContext>) instead of reading a task-local, so a separate ExecutionEnv type isn't needed. The last gap (the missing-parent case is still a runtime error) is in #4371. Closing this one as superseded.

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