tech-debt: define first-class ExecutionEnv, thread it into sub-agent spawn entry points
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
- Define a first-class
ExecutionEnv(provider / tools / memory / model / session) — the explicit form of whatParentExecutionContextsmuggles through the task-local. - Make
run_subagent/spawn_agent/follow_uptakeExecutionEnv(or a handle) as an explicit argument. - Keep a thin task-local shim only at the
Tool::executeboundary during migration (leaf-tool readers are migrated in follow-up C). - 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::executesignature. - Deleting
PARENT_CONTEXT/current_parent().
Acceptance
- Spawn entry points accept an execution env argument;
NoParentContextat 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
ExecutionEnvstruct (provider/tools/memory/model/session -- the explicit form of whatParentExecutionContextsmuggles). - Thread into
run_subagent/spawn_agent/follow_upas explicit argument. - Keep thin task-local shim at
Tool::executeboundary for ~18 leaf-tool readers. - Update "stay" re-install sites to capture+move
ExecutionEnv. - Do NOT widen
Tool::executeor deletePARENT_CONTEXT(deferred to #4371).
Implementation Steps
- Define
ExecutionEnv(src/openhuman/agent/harness/): aClone+Sendstruct 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 ofParentExecutionContext-- not a new parallel type -- to prevent drift. - Thread into spawn entry points (
src/openhuman/agent/harness/subagent_runner/ops/runner.rs,src/openhuman/agent_orchestration/ops.rs): changerun_subagent,spawn_agent,follow_upsignatures to take&ExecutionEnv. The existingspawn_agent_with_definition(parent, definition, request)pattern already partially exists internally -- this formalizes it. - Task-local shim (
src/openhuman/agent/harness/fork_context.rs): keep a shimwith_current_env(env, fut)that installs the task-local for theTool::executeboundary. Leaf tools continue callingcurrent_env()(renamed fromcurrent_parent()). - "Stay" sites: rework
spawn_async_subagent,spawn_worker_thread, orchestration background tasks to capture+moveExecutionEnvinto 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_envdefault, or (c) dispatch-level passing). This choice defines #4371's shape but does not block this issue. ExecutionEnv= rename+relocate ofParentExecutionContext, or new inner struct? (Open question 1 in comment).
Acceptance Criteria
- [ ]
ExecutionEnvdefined as explicit struct. - [ ]
run_subagent,spawn_agent,follow_uptakeExecutionEnvargument. - [ ]
NoParentContextimpossible at entry points (compile-time). - [ ] Task-local shim at
Tool::executeboundary (leaf tools unaffected). - [ ] "Stay" sites capture+move env instead of re-installing inherited task-local.
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_CONTEXTtokio task-local viacurrent_parent(); if unset → runtimeNoParentContext:subagent_runner/ops/runner.rsrun_subagent()agent_orchestration/ops.rsspawn_agent()(l.103) andfollow_up()(l.297)
- 27 files read
current_parent()— the spawn entry points, ~18 leaf-toolTool::executebodies, 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 tospawn_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:
- Introduce
ExecutionEnv; thread it explicitly through the spawn entry points + orchestrator internals (which already passparentaround). - Keep a thin task-local shim only at the
Tool::executeboundary:with_current_env(env, fut)installs it, leaf tools keep callingcurrent_env()(renamedcurrent_parent()) unchanged. So no leaf-tool churn in B. - The "stay" re-install sites (
spawn_async_subagent,spawn_worker_thread, orchestrationopsbackground tasks) switch from re-installing an inherited task-local to capturing + moving theExecutionEnvinto 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 ignoreenv). - (b)
execute_with_envdefault method — new method defaulting toexecute(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
DelegatingToolsub-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 theTool::executeshim. Self-contained, no leaf-tool churn,NoParentContextgone from entry points. - C (#4371): widen/dispatch per §5, migrate the ~18 leaf readers, delete the task-local shim, make
NoParentContextunrepresentable.
7. Open questions for maintainer
ExecutionEnv= rename/relocate ofParentExecutionContext, or a new inner struct it wraps?- Preferred §5 mechanism (a/b/c) — sets #4371's scope.
- OK to land B as a single PR (entry points + "stay" sites + shim), or split entry-points vs stay-sites?
- Any surface I've missed that spawns outside the 3 entry points?
cc epic #4336. Will not start coding B until this is approved.
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.
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.