Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Reuse a stored decision key only for the same backend and endpoint

agent-runner353007c1319c7d8
diff --git a/console/apps/switch-console-desktop/src/renderer/features/admin/decision-routing.ts b/console/apps/switch-console-desktop/src/renderer/features/admin/decision-routing.ts
index b033ccfc6..85b85d2c5 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/admin/decision-routing.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/admin/decision-routing.ts
@@ -123,10 +123,30 @@ function parsedThreshold(text: string): number | null {
return Number(trimmed);
}
+/**
+ * Whether the form moves the saved endpoint to another URL with its key left
+ * blank. The server never sends the stored key to a URL other than the one it
+ * was saved for, so the form asks for a key, or for the stored one to go.
+ */
+function endpointMovedWithoutKey(form: DecisionForm, settings: DecisionSettings): boolean {
+ const own = settings.workspace;
+ return (
+ form.choice === 'endpoint' &&
+ own?.backend === 'endpoint' &&
+ own.hasApiKey &&
+ own.endpoint !== form.endpoint.trim() &&
+ form.apiKey.trim() === '' &&
+ !form.clearApiKey
+ );
+}
+
/** What stops the form being saved or tested, in a sentence; null when nothing does. */
export function formProblem(form: DecisionForm, settings: DecisionSettings): string | null {
const fields = choiceFields(form.choice);
if (fields.endpoint && form.endpoint.trim() === '') return 'Enter the endpoint’s URL.';
+ if (endpointMovedWithoutKey(form, settings)) {
+ return 'The endpoint changed: enter its key, or remove the stored key to call it without one.';
+ }
if (form.choice === 'haiku_anthropic' && form.apiKey.trim() === '') {
const own = settings.workspace;
const usableKey = own?.hasApiKey && !own.apiKeyUnreadable && !form.clearApiKey;
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/decision-routing.test.ts b/console/apps/switch-console-desktop/src/renderer/tests/decision-routing.test.ts
index 60e1fdd65..cc5192713 100644
--- a/console/apps/switch-console-desktop/src/renderer/tests/decision-routing.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/tests/decision-routing.test.ts
@@ -179,6 +179,28 @@ describe('the form', () => {
expect(formProblem({ ...base, choice: 'none' }, settings())).toBeNull();
});
+ it('asks for a key when the saved endpoint moves to another URL', () => {
+ const own: DecisionSettings['workspace'] = {
+ ...OWN_HAIKU!,
+ backend: 'endpoint',
+ endpoint: 'https://d.example/decide',
+ };
+ const saved = settings({ workspace: own });
+ const moved = {
+ ...formFor(saved),
+ choice: 'endpoint' as const,
+ endpoint: 'https://e.example/decide',
+ };
+
+ expect(formProblem(moved, saved)).toBe(
+ 'The endpoint changed: enter its key, or remove the stored key to call it without one.'
+ );
+ expect(formProblem({ ...moved, apiKey: 'fake-key-NewA' }, saved)).toBeNull();
+ expect(formProblem({ ...moved, clearApiKey: true }, saved)).toBeNull();
+ expect(formProblem({ ...moved, endpoint: 'https://d.example/decide' }, saved)).toBeNull();
+ expect(formProblem(moved, settings({ workspace: { ...own!, hasApiKey: false } }))).toBeNull();
+ });
+
it('sends only the fields a backend takes, keeping the stored key when none is typed', () => {
const base = formFor(settings());
expect(inputFor(base)).toBeNull();
diff --git a/core/switch_core/gateway/decision_settings.py b/core/switch_core/gateway/decision_settings.py
index 217cb7bfa..92e2b9727 100644
--- a/core/switch_core/gateway/decision_settings.py
+++ b/core/switch_core/gateway/decision_settings.py
@@ -139,17 +139,53 @@ async def _refuse_unreachable(endpoint: str, policy: OutboundPolicy) -> None:
) from refused
+_ENDPOINT_MOVED = (
+ "The endpoint changed, so the stored key isn't sent to it. Enter the new "
+ "endpoint's key, or remove the stored key to call it without one."
+)
+
+
+def _same_target(
+ req: DecisionSettingsUpdateRequest | DecisionSettingsTestRequest,
+ existing: DecisionSettings,
+) -> bool:
+ """Whether a request names the backend, and for an endpoint the URL, the
+ stored key was saved for."""
+ if existing.backend != req.backend:
+ return False
+ return req.backend != "endpoint" or existing.endpoint == req.endpoint
+
+
def _kept_key(
req: DecisionSettingsUpdateRequest | DecisionSettingsTestRequest,
existing: DecisionSettings | None,
) -> str | None:
- """The stored key a request leaves in place: only when it names the same
- backend, so an endpoint's token is never sent to Anthropic as a key."""
- if req.clear_api_key or existing is None or existing.backend != req.backend:
+ """The stored key a request leaves in place: only for the same backend and
+ endpoint, so an endpoint's token is never sent to Anthropic as a key, nor
+ to a URL other than the one it was saved for."""
+ if req.clear_api_key or existing is None or not _same_target(req, existing):
return None
return existing.api_key_encrypted
+def _endpoint_moved(
+ req: DecisionSettingsUpdateRequest | DecisionSettingsTestRequest,
+ existing: DecisionSettings | None,
+) -> bool:
+ """Whether a request moves the saved endpoint to another URL without saying
+ what becomes of its key: neither a new key nor `clear_api_key`. Taken
+ silently as "no key", it would look as if the key had been kept."""
+ return (
+ req.backend == "endpoint"
+ and req.api_key is None
+ and not req.clear_api_key
+ and existing is not None
+ and existing.backend == "endpoint"
+ and existing.api_key_encrypted is not None
+ and existing.endpoint != req.endpoint
+ )
+
+
@router.get("/decision-settings")
async def get_decision_settings(
session: Annotated[AsyncSession, Depends(get_session)],
@@ -175,6 +211,8 @@ async def update_decision_settings(
await _refuse_unreachable(req.endpoint, client.outbound_policy)
tenant_id = require_tenant_id()
existing = await _STORE.get_for_update(session, tenant_id)
+ if _endpoint_moved(req, existing):
+ raise HTTPException(status_code=422, detail=_ENDPOINT_MOVED)
api_key_encrypted = (
config.keyring.encrypt(req.api_key)
if req.api_key is not None
@@ -223,10 +261,21 @@ async def test_decision_settings(
"""Run the sample decision against the values entered, saved or not; an
omitted key means the stored one. `backend: "default"` tests the server
default, with the server's own endpoint and key and never the
- workspace's. Nothing is saved, and no room's data is sent."""
+ workspace's. The stored key is used only for the backend and endpoint it
+ was saved for. Nothing is saved, and no room's data is sent."""
existing = await _STORE.get(session, require_tenant_id())
# Not held across the call to the decision service.
await session.commit()
+ if _endpoint_moved(req, existing):
+ return DecisionTestResponse(
+ ok=False,
+ agent=None,
+ model_choice=None,
+ confidence=None,
+ latency_ms=None,
+ backend=req.backend,
+ error=DecisionTestError(code="not_configured", message=_ENDPOINT_MOVED),
+ )
api_key = req.api_key
if api_key is None and req.backend != "default":
stored = _kept_key(req, existing)
diff --git a/core/switch_core/gateway/schemas.py b/core/switch_core/gateway/schemas.py
index 5babe32ce..7466d27c5 100644
--- a/core/switch_core/gateway/schemas.py
+++ b/core/switch_core/gateway/schemas.py
@@ -2143,8 +2143,9 @@ class DecisionSettingsResponse(BaseModel):
class DecisionSettingsUpdateRequest(BaseModel):
- """`api_key` omitted keeps the stored key when the backend stays the
- same; `clear_api_key` drops it. A key is the bearer token for an
+ """`api_key` omitted keeps the stored key when the backend, and for an
+ `endpoint` its URL, stay the same; moving a keyed endpoint to another URL
+ needs a new key or `clear_api_key`, which drops it. A key is the bearer token for an
`endpoint`, and the Anthropic key for `haiku_anthropic`."""
backend: DecisionBackendName
diff --git a/core/tests/switch_core/gateway/test_decision_settings.py b/core/tests/switch_core/gateway/test_decision_settings.py
index 8b407b93c..358385e1a 100644
--- a/core/tests/switch_core/gateway/test_decision_settings.py
+++ b/core/tests/switch_core/gateway/test_decision_settings.py
@@ -290,6 +290,59 @@ async def test_haiku_needs_a_key_new_or_kept_for_the_same_backend(
assert cleared.status_code == 422
+ENDPOINT_A = "http://10.9.0.5/decide"
+ENDPOINT_B = "http://10.9.0.6/decide"
+
+
+async def test_an_endpoint_key_is_kept_only_for_the_same_url(
+ session_factory: async_sessionmaker[AsyncSession], service: _Service
+) -> None:
+ admin = await _person(session_factory, "ada", role="admin")
+ async with _as(_app(session_factory, service), admin) as client:
+ await client.put(
+ "/decision-settings",
+ json={
+ "backend": "endpoint",
+ "endpoint": ENDPOINT_A,
+ "api_key": "fake-key-EndA",
+ },
+ )
+ same_url = await client.put(
+ "/decision-settings",
+ json={"backend": "endpoint", "endpoint": ENDPOINT_A, "threshold": 0.5},
+ )
+ moved = await client.put(
+ "/decision-settings", json={"backend": "endpoint", "endpoint": ENDPOINT_B}
+ )
+ after_moved = await _row(session_factory, TENANT_ZERO_ID)
+ moved_keyless = await client.put(
+ "/decision-settings",
+ json={"backend": "endpoint", "endpoint": ENDPOINT_B, "clear_api_key": True},
+ )
+ moved_with_key = await client.put(
+ "/decision-settings",
+ json={
+ "backend": "endpoint",
+ "endpoint": ENDPOINT_A,
+ "api_key": "fake-key-NewA",
+ },
+ )
+
+ assert same_url.status_code == 200
+ assert same_url.json()["workspace"]["api_key_last4"] == "EndA"
+ assert moved.status_code == 422
+ assert "endpoint changed" in moved.json()["detail"]
+ assert after_moved is not None
+ assert after_moved.endpoint == ENDPOINT_A
+ assert KEYRING.decrypt(after_moved.api_key_encrypted or "") == "fake-key-EndA"
+ assert moved_keyless.status_code == 200
+ assert moved_keyless.json()["workspace"]["endpoint"] == ENDPOINT_B
+ assert moved_keyless.json()["workspace"]["has_api_key"] is False
+ assert moved_with_key.status_code == 200
+ assert moved_with_key.json()["workspace"]["api_key_last4"] == "NewA"
+ assert service.calls == []
+
+
@pytest.mark.parametrize(
"body",
[
@@ -470,6 +523,66 @@ class TestTheTestButton:
assert headers["x-decision-backend"] == "anthropic"
assert headers["x-decision-anthropic-key"] == "fake-key-SAVD"
+ async def test_the_stored_endpoint_key_is_used_for_its_own_url(
+ self, session_factory: async_sessionmaker[AsyncSession], service: _Service
+ ) -> None:
+ admin = await _person(session_factory, "ada", role="admin")
+ async with _as(_app(session_factory, service), admin) as client:
+ await client.put(
+ "/decision-settings",
+ json={
+ "backend": "endpoint",
+ "endpoint": ENDPOINT_A,
+ "api_key": "fake-key-EndA",
+ },
+ )
+ tried = await client.post(
+ "/decision-settings/test",
+ json={"backend": "endpoint", "endpoint": ENDPOINT_A},
+ )
+
+ assert tried.json()["ok"] is True
+ [(url, headers, _)] = service.calls
+ assert url == ENDPOINT_A
+ assert headers["authorization"] == "Bearer fake-key-EndA"
+
+ async def test_another_url_asks_for_a_key_and_sends_nothing(
+ self, session_factory: async_sessionmaker[AsyncSession], service: _Service
+ ) -> None:
+ admin = await _person(session_factory, "ada", role="admin")
+ async with _as(_app(session_factory, service), admin) as client:
+ await client.put(
+ "/decision-settings",
+ json={
+ "backend": "endpoint",
+ "endpoint": ENDPOINT_A,
+ "api_key": "fake-key-EndA",
+ },
+ )
+ moved = await client.post(
+ "/decision-settings/test",
+ json={"backend": "endpoint", "endpoint": ENDPOINT_B},
+ )
+ keyless = await client.post(
+ "/decision-settings/test",
+ json={
+ "backend": "endpoint",
+ "endpoint": ENDPOINT_B,
+ "clear_api_key": True,
+ },
+ )
+
+ assert moved.status_code == 200
+ assert moved.json()["ok"] is False
+ assert moved.json()["error"]["code"] == "not_configured"
+ assert "Enter the new endpoint's key" in moved.json()["error"]["message"]
+ # Only the keyless try reached the new URL, and it carried no key.
+ [(url, headers, _)] = service.calls
+ assert keyless.json()["ok"] is True
+ assert url == ENDPOINT_B
+ assert "authorization" not in headers
+ assert "fake-key-EndA" not in json.dumps(headers)
+
async def test_default_uses_the_servers_key_never_the_workspaces(
self, session_factory: async_sessionmaker[AsyncSession], service: _Service
) -> None: