Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

Validate ANTHROPIC_WORKSPACE_ID at settings load

Claude61c17fa58f43
diff --git a/packages/core/openexecutive/config.py b/packages/core/openexecutive/config.py
index 2ba57059..4a757d12 100644
--- a/packages/core/openexecutive/config.py
+++ b/packages/core/openexecutive/config.py
@@ -1,3 +1,4 @@
+import re
from pathlib import Path
from typing import Annotated, Any, Literal
@@ -22,6 +23,14 @@ if not _FOUND_ENV:
_ENV_FILE = _ROOT / ".env"
+# An Anthropic workspace id is an opaque token. Pinning it to a token charset
+# is not about their format but about ours: a stray quote, space, control
+# character or non-ASCII byte from a .env line becomes an illegal header value,
+# which httpx/h11 reject as a *connection* error at the first Claude call —
+# two silent retries later, and nowhere near the setting that caused it.
+_WORKSPACE_ID_RE = re.compile(r"^[A-Za-z0-9._-]+$")
+
+
def _blank_or_comment(v: Any) -> bool:
"""True for unset, '', or a dotenv inline-comment captured as the value.
@@ -174,6 +183,29 @@ class Settings(BaseSettings):
_DEFAULT_OPENROUTER_CATALOG_REFRESH_S, alias="OPENROUTER_CATALOG_REFRESH_S"
)
+ @field_validator("anthropic_workspace_id", mode="before")
+ @classmethod
+ def _parse_anthropic_workspace_id(cls, v: Any) -> Any:
+ """Reject at boot what would otherwise 400 on the first Claude call.
+
+ `ANTHROPIC_WORKSPACE_ID=` with a trailing `# comment` parses the
+ comment as the VALUE (dotenv, verified), and that string is a legal
+ header — so an install that needs no workspace at all would start
+ sending one and fail every call with the very error this setting
+ exists to fix.
+ """
+ if _blank_or_comment(v):
+ return None
+ if isinstance(v, str):
+ v = v.strip()
+ if not _WORKSPACE_ID_RE.match(v):
+ raise ValueError(
+ "ANTHROPIC_WORKSPACE_ID must be a bare workspace id "
+ "(letters, digits, '.', '_', '-') with no quotes, spaces "
+ f"or trailing comment; got {v!r}"
+ )
+ return v
+
@field_validator("openrouter_catalog_providers", mode="before")
@classmethod
def _parse_openrouter_catalog_providers(cls, v: Any) -> list[str]:
diff --git a/packages/core/tests/unit/test_anthropic_workspace_id.py b/packages/core/tests/unit/test_anthropic_workspace_id.py
index 11d6bb7c..af346bdc 100644
--- a/packages/core/tests/unit/test_anthropic_workspace_id.py
+++ b/packages/core/tests/unit/test_anthropic_workspace_id.py
@@ -67,9 +67,13 @@ def test_the_header_is_purely_additive() -> None:
plain = AnthropicProvider(api_key="sk-test")
with_ws = AnthropicProvider(api_key="sk-test", workspace_id="wrkspc_123")
- added = set(_headers(with_ws)) - set(_headers(plain))
+ ours = _headers(with_ws)
+ theirs = _headers(plain)
- assert added == {"anthropic-workspace-id"}
+ assert set(ours) - set(theirs) == {"anthropic-workspace-id"}
+ assert {k: v for k, v in ours.items() if k != "anthropic-workspace-id"} == theirs, (
+ "every header the SDK sets must survive unchanged in value, not just in name"
+ )
assert with_ws._client.auth_headers == plain._client.auth_headers
@@ -119,3 +123,58 @@ def test_settings_default_is_none(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("ANTHROPIC_WORKSPACE_ID", raising=False)
assert get_settings().anthropic_workspace_id is None
+
+
+# ── what a .env can actually hand us ──────────────────────────────────────
+
+
+ "raw",
+ [
+ "# leave blank for a workspace-scoped key", # dotenv reads an inline
+ # comment after `KEY=` as
+ # the value (verified)
+ " ",
+ "",
+ ],
+)
+def test_a_blank_or_comment_value_reads_as_unset(raw: str, monkeypatch: pytest.MonkeyPatch) -> None:
+ """The failure mode this setting must not create.
+
+ Sending a comment string as the workspace id is a legal HTTP header, so it
+ reaches Anthropic and 400s every call — handing the exact symptom of #128
+ to someone whose key never needed a workspace at all.
+ """
+ from openexecutive.config import get_settings
+
+ monkeypatch.setenv("ANTHROPIC_WORKSPACE_ID", raw)
+
+ assert get_settings().anthropic_workspace_id is None
+
+
+ "raw",
+ ["wrkspc_1 trailing", "wrkspc\r\nx-injected: 1", '"wrkspc_1"', "wrkspc_é"],
+)
+def test_an_unusable_value_fails_at_boot(raw: str, monkeypatch: pytest.MonkeyPatch) -> None:
+ """Fail where the operator can act on it.
+
+ Each of these is either an illegal header value — which httpx/h11 reject as
+ a *connection* error two silent retries into the first Claude call — or a
+ quoted/padded copy-paste that Anthropic would simply reject. Neither points
+ at the .env line that caused it, so the settings load is the place to stop.
+ """
+ from openexecutive.config import get_settings
+
+ monkeypatch.setenv("ANTHROPIC_WORKSPACE_ID", raw)
+
+ with pytest.raises(Exception, match="ANTHROPIC_WORKSPACE_ID"):
+ get_settings()
+
+
+def test_surrounding_whitespace_is_trimmed(monkeypatch: pytest.MonkeyPatch) -> None:
+ from openexecutive.config import get_settings
+
+ monkeypatch.setenv("ANTHROPIC_WORKSPACE_ID", " wrkspc_123 ")
+
+ assert get_settings().anthropic_workspace_id == "wrkspc_123"