{"record":{"id":"dc1493099cbdd527","repo":"zeroclaw-labs/zeroclaw","slug":"apply-env-only-works-when-proxy-scope-is-environm","errorCode":null,"errorMessage":"apply_env only works when proxy.scope is 'environment' (current: {:?})","messagePattern":"apply_env only works when proxy\\.scope is 'environment' \\(current: (.+?)\\)","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"warning","filePath":"crates/zeroclaw-tools/src/proxy_config.rs","lineNumber":384,"sourceCode":"                \"proxy\": Self::proxy_json(&cfg.proxy),\n                \"environment\": Self::env_snapshot(),\n            }))?\n            .into(),\n            error: None,\n        })\n    }\n\n    fn handle_apply_env(&self) -> anyhow::Result<ToolResult> {\n        let cfg = self.load_config_without_env()?;\n        let proxy = cfg.proxy.clone();\n        proxy.validate()?;\n\n        if !proxy.enabled {\n            anyhow::bail!(\"Proxy is disabled. Use action 'set' with enabled=true first\");\n        }\n\n        if proxy.scope != ProxyScope::Environment {\n            anyhow::bail!(\n                \"apply_env only works when proxy.scope is 'environment' (current: {:?})\",\n                proxy.scope\n            );\n        }\n\n        proxy.apply_to_process_env();\n        set_runtime_proxy_config(proxy.clone());\n        let warnings = Self::dns_pinned_tool_warnings(&cfg);\n\n        Ok(ToolResult {\n            success: true,\n            output: serde_json::to_string_pretty(&json!({\n                \"message\": \"Proxy environment variables applied\",\n                \"proxy\": Self::proxy_json(&proxy),\n                \"environment\": Self::env_snapshot(),\n                \"warnings\": warnings,\n            }))?\n            .into(),","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/zeroclaw-labs/zeroclaw/blob/88bb9c8533fc57ed7a03e36ca7c9ed2bf8336dcc/crates/zeroclaw-tools/src/proxy_config.rs#L366-L402","documentation":"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.","triggerScenarios":"Running {\"action\":\"apply_env\"} after the proxy was configured with any scope other than 'environment' (e.g. per-session or tool-scoped settings).","commonSituations":"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.","solutions":["Re-run {\"action\":\"set\"} with \"scope\":\"environment\" in the proxy block, then apply_env","If you actually want scoped behavior, use the mechanism for that scope instead of apply_env","Run {\"action\":\"get\"} to see the current scope value quoted in the error"],"exampleFix":"// before\n{\"action\":\"set\",\"proxy\":{\"enabled\":true,\"url\":\"http://127.0.0.1:7890\",\"scope\":\"session\"}}\n{\"action\":\"apply_env\"}  // bails: scope is 'session'\n// after\n{\"action\":\"set\",\"proxy\":{\"enabled\":true,\"url\":\"http://127.0.0.1:7890\",\"scope\":\"environment\"}}\n{\"action\":\"apply_env\"}","handlingStrategy":"validation","validationCode":"let cfg = proxy_tool.execute(json!({\"action\":\"get\"})).await?;\nlet scope_ok = cfg.output[\"proxy\"][\"scope\"].as_str() == Some(\"environment\");\nif scope_ok { proxy_tool.execute(json!({\"action\":\"apply_env\"})).await?; }","typeGuard":"fn is_environment_scope(cfg: &serde_json::Value) -> bool {\n    cfg.pointer(\"/proxy/scope\").and_then(|v| v.as_str()) == Some(\"environment\")\n}","tryCatchPattern":"Err(e) if e.to_string().starts_with(\"apply_env only works\") => {\n    // parse current scope from the message or a get call; decide set-scope vs skip\n}","preventionTips":["Decide the proxy scope once at provisioning time and encode it in config templates","Assert scope==environment in any deployment that relies on exported env vars","Document which components depend on env-var proxying so scope changes get reviewed"],"tags":["proxy","config","scope","precondition"],"backgroundTag":"invalid-config-state","analyzedSha":"88bb9c8533fc57ed7a03e36ca7c9ed2bf8336dcc","analyzedAt":"2026-08-23T01:07:41.857Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}