Motivation
The planning context's env map — the full session environment, typically hundreds of entries — was copied at every boundary it crossed during planning:
- once into
PlanContext per plan (plan_query),
- once per
&&-item by PlanContext::duplicate (each command gets a scoped context),
- once more per
&&-item into ScriptCommand::envs for program lookup and plan-request callbacks,
- and once per spawn into
all_envs.
Of these, only the last one does real work — the spawn env is filtered in place by EnvFingerprints::resolve. Every other copy duplicates an almost-always-identical map just to hand it to the next stage, and the cost scales with task count × command count, not with actual env changes.
The immediate trigger is the upcoming runner-aware caching work (#430/#431): it saves each spawn's full env context into the plan (SpawnCommand::full_envs) so getEnv/getEnvs and tracked-env validation resolve against the plan rather than the live process env. Without sharing, that would add yet another full map copy per spawn; with this change it costs a pointer.
Approach
Wrap the map in Arc with copy-on-write semantics:
- Duplication and hand-off points (
duplicate, ScriptCommand::envs, PlanContext::new) share the map — O(1).
- The two mutation points pay for a copy only when they change something:
prepend_path once per task node (package .bin PATH prepend), and add_envs only when a command actually has prefix envs — an empty prefix set is guarded so it doesn't break sharing.
- The per-spawn
all_envs clone remains, with a comment marking it as the one place the map's contents are genuinely copied.
No behavior change; Session already held the map in an Arc, so its call sites are untouched.