zeroclaw-labs/zeroclaw · warning · anyhow::Error

apply_env only works when proxy.scope is 'environment' (curr

Error message

apply_env only works when proxy.scope is 'environment' (current: {:?})

What it means

apply_env mutates process-wide environment variables, which is only correct when the proxy is configured with scope 'environment'. handle_apply_env compares the stored proxy.scope against ProxyScope::Environment and bails with the current value when it differs, preventing a session- or otherwise-scoped proxy from leaking into global env.

Source

Thrown at crates/zeroclaw-tools/src/proxy_config.rs:384

                "proxy": Self::proxy_json(&cfg.proxy),
                "environment": Self::env_snapshot(),
            }))?
            .into(),
            error: None,
        })
    }

    fn handle_apply_env(&self) -> anyhow::Result<ToolResult> {
        let cfg = self.load_config_without_env()?;
        let proxy = cfg.proxy.clone();
        proxy.validate()?;

        if !proxy.enabled {
            anyhow::bail!("Proxy is disabled. Use action 'set' with enabled=true first");
        }

        if proxy.scope != ProxyScope::Environment {
            anyhow::bail!(
                "apply_env only works when proxy.scope is 'environment' (current: {:?})",
                proxy.scope
            );
        }

        proxy.apply_to_process_env();
        set_runtime_proxy_config(proxy.clone());
        let warnings = Self::dns_pinned_tool_warnings(&cfg);

        Ok(ToolResult {
            success: true,
            output: serde_json::to_string_pretty(&json!({
                "message": "Proxy environment variables applied",
                "proxy": Self::proxy_json(&proxy),
                "environment": Self::env_snapshot(),
                "warnings": warnings,
            }))?
            .into(),

View on GitHub (pinned to 88bb9c8533)

Solutions

  1. Re-run {"action":"set"} with "scope":"environment" in the proxy block, then apply_env
  2. If you actually want scoped behavior, use the mechanism for that scope instead of apply_env
  3. Run {"action":"get"} to see the current scope value quoted in the error

Example fix

// before
{"action":"set","proxy":{"enabled":true,"url":"http://127.0.0.1:7890","scope":"session"}}
{"action":"apply_env"}  // bails: scope is 'session'
// after
{"action":"set","proxy":{"enabled":true,"url":"http://127.0.0.1:7890","scope":"environment"}}
{"action":"apply_env"}
Defensive patterns

Strategy: validation

Validate before calling

let cfg = proxy_tool.execute(json!({"action":"get"})).await?;
let scope_ok = cfg.output["proxy"]["scope"].as_str() == Some("environment");
if scope_ok { proxy_tool.execute(json!({"action":"apply_env"})).await?; }

Type guard

fn is_environment_scope(cfg: &serde_json::Value) -> bool {
    cfg.pointer("/proxy/scope").and_then(|v| v.as_str()) == Some("environment")
}

Try / catch

Err(e) if e.to_string().starts_with("apply_env only works") => {
    // parse current scope from the message or a get call; decide set-scope vs skip
}

Prevention

When it happens

Trigger: Running {"action":"apply_env"} after the proxy was configured with any scope other than 'environment' (e.g. per-session or tool-scoped settings).

Common situations: Copying a config tuned for per-session proxying and then trying to export it globally; evolving a setup from scoped to environment-wide without changing scope first.

Related errors


AI-assisted analysis of zeroclaw-labs/zeroclaw@88bb9c8533 (2026-08-23). Data as JSON: /api/errors/dc1493099cbdd527. Report an issue: GitHub.