From ef88a2a8b4686bf16267186b1266ec334ea9c2d7 Mon Sep 17 00:00:00 2001 From: gbarnai Date: Sun, 29 Nov 2026 14:55:09 -0500 Subject: [PATCH 1/7] fix: with no system keyring a stored Allowed reads as Ask and Always allow is not offered When no system keyring answers, the outside-agents seal cannot be made, so a stored Allowed now reads as Ask, Allowed cannot be chosen, the question in the window offers only Deny and Allow once, and Preferences shows a note and greys out Allowed. With a keyring nothing changes. --- backend/app/core/seal.py | 24 ++++++++++++++++-------- backend/app/routers/mcp.py | 6 ++++-- backend/app/services/agent_access.py | 12 +++++++++++- frontend/src/dock-adapter.ts | 9 +++++---- frontend/src/ui/settings-dialog.ts | 6 +++++- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/backend/app/core/seal.py b/backend/app/core/seal.py index e001f80..1f85e7b 100644 --- a/backend/app/core/seal.py +++ b/backend/app/core/seal.py @@ -1,5 +1,6 @@ """The seal on the outside-agents choice in settings.json: an HMAC whose key lives in the system keychain, so a change -made to the file outside the app cannot turn outside agents on. FormDeck 0.46.3's core/seal.py with this app's names.""" +made to the file outside the app cannot turn outside agents on. FormDeck 0.46.3's core/seal.py with this app's names. +With no system keyring at all nothing can be sealed, so a stored Allowed reads as Ask.""" import hashlib import hmac import json @@ -24,18 +25,23 @@ def _no_ld_probe() -> None: ctypes.util._findLib_ld = lambda name: None +def _ring(): + _no_ld_probe() + import keyring + + return keyring + + def _load(create: bool) -> bytes | None: if _CACHE["tried"] and (_CACHE["key"] is not None or not _CACHE["available"] or not create): return _CACHE["key"] _CACHE["tried"] = True try: - _no_ld_probe() - import keyring - - stored = keyring.get_password(SERVICE, ACCOUNT) + ring = _ring() + stored = ring.get_password(SERVICE, ACCOUNT) if not stored and create: - keyring.set_password(SERVICE, ACCOUNT, secrets.token_hex(32)) - stored = keyring.get_password(SERVICE, ACCOUNT) + ring.set_password(SERVICE, ACCOUNT, secrets.token_hex(32)) + stored = ring.get_password(SERVICE, ACCOUNT) key = bytes.fromhex(stored) if stored else None except Exception: _CACHE.update(available=False, key=None) @@ -67,7 +73,9 @@ def effective(stored: dict) -> dict: out = {k: v for k, v in stored.items() if k != FIELD} key = _load(False) if not _CACHE["available"]: - return {**out, "seal": "unavailable", "sealBroken": False} + # fail closed: with no keyring a yes in the file cannot be told apart from one written outside the app + held = {"agentsAllowed": False} if loosened(stored) else {} + return {**out, **held, "seal": "unavailable", "sealBroken": False} mark = stored.get(FIELD) if key is not None and isinstance(mark, str) and hmac.compare_digest(mark, digest(stored, key)): return {**out, "seal": "ok", "sealBroken": False} diff --git a/backend/app/routers/mcp.py b/backend/app/routers/mcp.py index 92ac806..1f97b8d 100644 --- a/backend/app/routers/mcp.py +++ b/backend/app/routers/mcp.py @@ -243,14 +243,16 @@ async def _gate(a: Agent, kind: str, what: str) -> str | None: return OFF if time.monotonic() < _declined[0]: return DECLINED - offers = ["deny", "once"] if kind == "asks" else ["deny", "once", "always"] + # a standing yes needs a keyring to seal it, so without one the window offers only this once + always = kind == "changes" and agent_access.can_allow() + offers = ["deny", "once", "always"] if always else ["deny", "once"] said = await bridge.ask("" if a.inapp else a.name, what, offers, CONSENT_WAIT) if said is None: return f"Nobody answered in the FormMorpheus window within {int(CONSENT_WAIT // 60)} minutes, so nothing was done. Ask the person to answer there, then retry." if said == "deny": _declined[0] = time.monotonic() + DECLINE_PAUSE return "The person said no in the FormMorpheus window, so nothing was done." - if said == "always" and kind == "changes": + if said == "always" and always: agent_access.set_choice("allowed") return None diff --git a/backend/app/services/agent_access.py b/backend/app/services/agent_access.py index 39715b5..e148c14 100644 --- a/backend/app/services/agent_access.py +++ b/backend/app/services/agent_access.py @@ -5,10 +5,18 @@ CHOICES = ("ask", "allowed", "blocked") +class NeedsKeyring(ValueError): + """Allowed cannot be kept: with no system keyring it could not be sealed, and an unsealed yes reads as Ask.""" + + +def can_allow() -> bool: + return seal.available() + + def state() -> dict: s = seal.effective(config.read_settings()) value = "blocked" if s.get("agentsBlocked") is True else "allowed" if s.get("agentsAllowed") is True else "ask" - return {"choice": value, "changedOutside": bool(s.get("sealBroken")), "seal": s.get("seal")} + return {"choice": value, "changedOutside": bool(s.get("sealBroken")), "seal": s.get("seal"), "keyring": s.get("seal") != "unavailable"} def choice() -> str: @@ -18,6 +26,8 @@ def choice() -> str: def set_choice(value: str) -> dict: if value not in CHOICES: raise ValueError(f"The choice is one of {', '.join(CHOICES)}.") + if value == "allowed" and not can_allow(): + raise NeedsKeyring("Allowed needs a system keyring on this computer, and none was found, so outside agents keep asking.") mark = {"allowed": {"agentsAllowed": True}, "blocked": {"agentsBlocked": True}}.get(value, {}) def change(settings: dict) -> dict: diff --git a/frontend/src/dock-adapter.ts b/frontend/src/dock-adapter.ts index 6073b26..8092bcd 100644 --- a/frontend/src/dock-adapter.ts +++ b/frontend/src/dock-adapter.ts @@ -83,12 +83,13 @@ export const outsideAgents = { for (const request of had.requests ?? []) outsideAgents.show(request); } catch { return; } }, - async get(): Promise { + /** `keyring` false: this computer has no system keyring, so Allowed cannot be kept and the engine asks each time. */ + async get(): Promise { try { - const s = await call<{ choice?: unknown; changedOutside?: unknown }>('/api/agents'); + const s = await call<{ choice?: unknown; changedOutside?: unknown; keyring?: unknown }>('/api/agents'); choice = s.choice === 'blocked' || s.choice === 'allowed' ? s.choice : 'ask'; - return { value: choice, changedOutside: s.changedOutside === true }; - } catch { return { value: choice, changedOutside: false }; } + return { value: choice, changedOutside: s.changedOutside === true, keyring: s.keyring !== false }; + } catch { return { value: choice, changedOutside: false, keyring: true }; } }, async set(value: StoredChoice): Promise { await call('/api/agents/choice', 'POST', { value }); diff --git a/frontend/src/ui/settings-dialog.ts b/frontend/src/ui/settings-dialog.ts index 235beca..72f6f5b 100644 --- a/frontend/src/ui/settings-dialog.ts +++ b/frontend/src/ui/settings-dialog.ts @@ -80,6 +80,7 @@ export class SettingsDialog extends Dialog { void outsideAgents.set(v as StoredChoice).then(() => setHidden(this.agentsChanged, true), () => { toast(t('The engine did not keep the Outside agents setting. Start it with ./formmorpheus, then try again.'), 'error'); void this.loadAgents(); }); }); private readonly agentsChanged = el(``); + private readonly agentsNoKeyring = el(``); constructor(private readonly onPrefs: (p: Prefs) => void) { super('Preferences', 620); @@ -178,7 +179,7 @@ export class SettingsDialog extends Dialog { terminalRow.append(terminalBtn); if (platform.kind === 'ipad') terminalRow.hidden = true; if (platform.kind !== 'ipad') add('Agents', - grp('Outside Agents', this.agentsRow.el, this.agentsChanged, nt(t('Off by default. Ask: an outside agent such as Claude Code, Codex, Cursor or Antigravity asks you in this window before it changes a movie. Allowed: it works without asking. Blocked: every agent is refused, the ones in the terminal panel included.'))), + grp('Outside Agents', this.agentsRow.el, this.agentsChanged, this.agentsNoKeyring, nt(t('Off by default. Ask: an outside agent such as Claude Code, Codex, Cursor or Antigravity asks you in this window before it changes a movie. Allowed: it works without asking. Blocked: every agent is refused, the ones in the terminal panel included.'))), grp('Connect a Client', mcpRow('MCP address', `http://127.0.0.1:${mcpPort}/mcp`), mcpRow('Standard input and output', './formmorpheus mcp'), nt(t('Give an MCP client the address, or, for a client that starts its own server, the command, run in the FormMorpheus folder.'))), grp('Terminal', terminalRow, nt(t('The terminal panel runs a shell, Claude Code, Codex, Cursor and Antigravity beside the Stage and the Timeline.')))); for (const name of SECTION_ORDER) { @@ -310,6 +311,9 @@ export class SettingsDialog extends Dialog { const st = await outsideAgents.get(); this.agentsRow.set(st.value); setHidden(this.agentsChanged, !st.changedOutside); + setHidden(this.agentsNoKeyring, st.keyring); + const allowed = this.agentsRow.el.querySelector('option[value="allowed"]'); + if (allowed && allowed.disabled !== !st.keyring) allowed.disabled = !st.keyring; } private change(patch: Partial): void { From f1e86bd533cf84adf52e7ce0fe588b15751d7c9c Mon Sep 17 00:00:00 2001 From: gbarnai Date: Sun, 29 Nov 2026 14:55:09 -0500 Subject: [PATCH 2/7] test: a computer with no system keyring keeps outside agents asking Tests use an in-memory keyring by default, so none reaches the system one. --- backend/tests/agents/test_agents_routes.py | 30 +++++++++++++++ backend/tests/conftest.py | 31 +++++++++++++++ backend/tests/core/test_seal.py | 44 ++++++++-------------- backend/tests/mcp/test_mcp_tools.py | 25 ++++++++++++ 4 files changed, 101 insertions(+), 29 deletions(-) diff --git a/backend/tests/agents/test_agents_routes.py b/backend/tests/agents/test_agents_routes.py index 22245e3..2c3bfc1 100644 --- a/backend/tests/agents/test_agents_routes.py +++ b/backend/tests/agents/test_agents_routes.py @@ -101,3 +101,33 @@ def test_a_valid_answer_reaches_the_bridge(client, world): res = client.post("/api/agents/consent/q1", json={"choice": "once"}) assert res.status_code == 200 and res.json() == {"ok": True} assert world["answered"] == [("q1", "once")] + + +def test_state_says_a_keyring_is_there(client, world): + assert client.get("/api/agents").json()["keyring"] is True + + +def test_without_a_keyring_a_stored_allowed_reads_as_ask(data_dir, no_ring): + config.write_settings({"agentsAllowed": True}) + assert agent_access.state() == {"choice": "ask", "changedOutside": False, "seal": "unavailable", "keyring": False} + assert config.read_settings() == {"agentsAllowed": True} + + +def test_without_a_keyring_blocked_still_blocks(data_dir, no_ring): + agent_access.set_choice("blocked") + assert agent_access.choice() == "blocked" + + +def test_without_a_keyring_allowed_cannot_be_chosen(data_dir, no_ring): + agent_access.set_choice("ask") + with pytest.raises(agent_access.NeedsKeyring): + agent_access.set_choice("allowed") + assert "agentsAllowed" not in config.read_settings() + assert agent_access.choice() == "ask" + + +def test_without_a_keyring_the_route_refuses_allowed_with_a_plain_reason(client, world, no_ring): + res = client.post("/api/agents/choice", json={"value": "allowed"}) + assert res.status_code == 422 + assert "keyring" in res.json()["detail"] + assert client.get("/api/agents").json()["choice"] == "ask" diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index b368515..8ffbb0c 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -23,6 +23,37 @@ def data_dir(tmp_path, monkeypatch) -> Path: return path +@pytest.fixture(autouse=True) +def ring(monkeypatch) -> dict: + """An in-memory keyring for the settings seal, so no test reaches the system one.""" + from types import SimpleNamespace + + from app.core import seal + + store: dict = {} + fake = SimpleNamespace( + get_password=lambda service, account: store.get((service, account)), + set_password=lambda service, account, value: store.__setitem__((service, account), value), + ) + monkeypatch.setattr(seal, "_ring", lambda: fake) + monkeypatch.setattr(seal, "_CACHE", {"tried": False, "available": False, "key": None}) + return store + + +@pytest.fixture +def no_ring(monkeypatch) -> None: + """A computer with no system keyring at all.""" + from types import SimpleNamespace + + from app.core import seal + + def missing(*args): + raise RuntimeError("no keyring") + + monkeypatch.setattr(seal, "_ring", lambda: SimpleNamespace(get_password=missing, set_password=missing)) + monkeypatch.setattr(seal, "_CACHE", {"tried": False, "available": False, "key": None}) + + @pytest.fixture def window_headers() -> dict: from app.core import window_key diff --git a/backend/tests/core/test_seal.py b/backend/tests/core/test_seal.py index 550641c..2fd4542 100644 --- a/backend/tests/core/test_seal.py +++ b/backend/tests/core/test_seal.py @@ -1,32 +1,6 @@ -import sys -from types import SimpleNamespace - -import pytest - from app.core import seal - -@pytest.fixture -def ring(monkeypatch): - store = {} - fake = SimpleNamespace( - get_password=lambda service, account: store.get((service, account)), - set_password=lambda service, account, value: store.__setitem__((service, account), value), - ) - monkeypatch.setitem(sys.modules, "keyring", fake) - monkeypatch.setattr(seal, "_no_ld_probe", lambda: None) - monkeypatch.setattr(seal, "_CACHE", {"tried": False, "available": False, "key": None}) - return store - - -@pytest.fixture -def no_ring(monkeypatch): - def broken(*args): - raise RuntimeError("no keychain") - - monkeypatch.setitem(sys.modules, "keyring", SimpleNamespace(get_password=broken, set_password=broken)) - monkeypatch.setattr(seal, "_no_ld_probe", lambda: None) - monkeypatch.setattr(seal, "_CACHE", {"tried": False, "available": False, "key": None}) +# the ring and no_ring fixtures live in tests/conftest.py def test_only_the_agent_choice_is_sealed(): @@ -89,12 +63,24 @@ def test_agents_cannot_be_on_before_any_key_exists(ring): assert seen["seal"] == "broken" -def test_without_a_keychain_the_seal_is_reported_unavailable(no_ring): +def test_without_a_keyring_a_stored_allowed_reads_as_ask(no_ring): assert seal.available() is False - assert seal.effective({"agentsAllowed": True, "seal": "x"}) == {"agentsAllowed": True, "seal": "unavailable", "sealBroken": False} + assert seal.effective({"agentsAllowed": True, "seal": "x"}) == {"agentsAllowed": False, "seal": "unavailable", "sealBroken": False} + assert seal.effective({"agentsAllowed": True, "theme": "dark"})["agentsAllowed"] is False assert seal.sign({"agentsAllowed": True, "seal": "old"}) == {"agentsAllowed": True} +def test_without_a_keyring_ask_and_blocked_read_as_written(no_ring): + assert seal.effective({"theme": "dark"}) == {"theme": "dark", "seal": "unavailable", "sealBroken": False} + assert seal.effective({"agentsBlocked": True}) == {"agentsBlocked": True, "seal": "unavailable", "sealBroken": False} + + +def test_with_a_keyring_a_sealed_allowed_is_unchanged(ring): + signed = seal.sign({"agentsAllowed": True}) + assert seal.effective(signed)["agentsAllowed"] is True + assert seal.effective(signed)["seal"] == "ok" + + def test_reset_forgets_what_was_learnt_about_the_keychain(ring): seal.sign({"agentsAllowed": True}) assert seal._CACHE["key"] is not None diff --git a/backend/tests/mcp/test_mcp_tools.py b/backend/tests/mcp/test_mcp_tools.py index 7aa9bfb..916ed90 100644 --- a/backend/tests/mcp/test_mcp_tools.py +++ b/backend/tests/mcp/test_mcp_tools.py @@ -209,3 +209,28 @@ def test_inside_refuses_a_path_that_climbs_out_of_the_folder(tmp_path): assert mcp._inside(folder / "a.fmo", folder) is True assert mcp._inside(folder, folder) is True assert mcp._inside(folder / ".." / "secret.txt", folder) is False + + +def test_without_a_keyring_a_stored_allowed_still_asks(client, no_ring): + from app.core import config + + config.write_settings({"agentsAllowed": True}) + result = call(client, "manage_movie", {"action": "create", "name": "First"}) + assert result["isError"] is True + assert text(result) == mcp.OFF + assert names(client) == [] + + +def test_without_a_keyring_the_window_does_not_offer_always(client, monkeypatch, no_ring): + seen = [] + answer_with(monkeypatch, "once", seen) + call(client, "manage_movie", {"action": "create", "name": "First"}) + assert seen[0][2] == ["deny", "once"] + assert names(client) == ["First"] + + +def test_without_a_keyring_an_always_answer_counts_as_once(client, monkeypatch, no_ring): + answer_with(monkeypatch, "always") + result = call(client, "manage_movie", {"action": "create", "name": "First"}) + assert "isError" not in result + assert agent_access.choice() == "ask" From 028c8fd364d556515a7113b048d4eeb1eeecab27 Mon Sep 17 00:00:00 2001 From: gbarnai Date: Sun, 29 Nov 2026 14:55:09 -0500 Subject: [PATCH 3/7] test: Preferences learns from the engine when there is no system keyring --- frontend/tests/unit/dock-adapter.test.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/frontend/tests/unit/dock-adapter.test.ts b/frontend/tests/unit/dock-adapter.test.ts index 6956dcc..d397b61 100644 --- a/frontend/tests/unit/dock-adapter.test.ts +++ b/frontend/tests/unit/dock-adapter.test.ts @@ -84,15 +84,23 @@ describe('outsideAgents', () => { outsideAgents.show(req('a')); await outsideAgents.answer('a', 'always'); replies.push({ status: -1 }); - expect(await outsideAgents.get()).toEqual({ value: 'allowed', changedOutside: false }); + expect(await outsideAgents.get()).toEqual({ value: 'allowed', changedOutside: false, keyring: true }); }); it('reads the stored choice and treats unknown values as ask', async () => { const { outsideAgents } = await load(); replies.push({ body: { choice: 'blocked', changedOutside: true } }); - expect(await outsideAgents.get()).toEqual({ value: 'blocked', changedOutside: true }); + expect(await outsideAgents.get()).toEqual({ value: 'blocked', changedOutside: true, keyring: true }); replies.push({ body: { choice: 'whatever', changedOutside: 'yes' } }); - expect(await outsideAgents.get()).toEqual({ value: 'ask', changedOutside: false }); + expect(await outsideAgents.get()).toEqual({ value: 'ask', changedOutside: false, keyring: true }); + }); + + it('says when the engine has no system keyring and Allowed cannot be kept', async () => { + const { outsideAgents } = await load(); + replies.push({ body: { choice: 'ask', changedOutside: false, keyring: false } }); + expect(await outsideAgents.get()).toEqual({ value: 'ask', changedOutside: false, keyring: false }); + replies.push({ body: { choice: 'allowed', keyring: true } }); + expect((await outsideAgents.get()).keyring).toBe(true); }); it('withdraws every waiting request when agents are blocked', async () => { From 8ae15e6252b68ed9562121bf96ab31ecdc90cc09 Mon Sep 17 00:00:00 2001 From: gbarnai Date: Sun, 29 Nov 2026 14:55:09 -0500 Subject: [PATCH 4/7] docs: Allowed and Always allow need a system keyring on this computer --- .docs/wiki/Agents.md | 2 +- backend/HASHOUT_LOG.md | 9 +++++++++ docs/MANUAL.md | 4 ++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.docs/wiki/Agents.md b/.docs/wiki/Agents.md index 36ce674..2fb5592 100644 --- a/.docs/wiki/Agents.md +++ b/.docs/wiki/Agents.md @@ -29,7 +29,7 @@ The server is `POST /mcp` on the engine: Streamable HTTP in stateless JSON, prot | Allowed | Changes to the library go ahead. An export or an import still asks. | | Blocked | Every agent is refused, reads included, and so are the agents in the terminal panel. Questions that were waiting are answered Deny. | -The choice is kept in `settings.json` beside the library setting. Where the system has a keychain, it is sealed with a key kept there; a change made outside the app that allows agents is ignored, the app asks again, and Preferences says so. An agent in one of the panel's own tabs carries a token for that tab: its library changes are not asked about, and an export or an import still is. +The choice is kept in `settings.json` beside the library setting. It is sealed with a key kept in the system keychain; a change made outside the app that allows agents is ignored, the app asks again, and Preferences says so. With no system keyring at all (some Linux setups without a secret service), a stored Allowed reads as Ask, Allowed cannot be chosen, the question in the window has no Always allow, and Preferences says that these need a system keyring on this computer. An agent in one of the panel's own tabs carries a token for that tab: its library changes are not asked about, and an export or an import still is. ## Connecting a client diff --git a/backend/HASHOUT_LOG.md b/backend/HASHOUT_LOG.md index bf665a8..0374197 100644 --- a/backend/HASHOUT_LOG.md +++ b/backend/HASHOUT_LOG.md @@ -7,6 +7,15 @@ Last updated: 2026-11-27 · scenes of your own --- +## unreleased · with no system keyring a stored Allowed reads as Ask + +- refers-to: core/seal.py :: _ring, effective; services/agent_access.py :: NeedsKeyring, can_allow, state, set_choice; routers/mcp.py :: _gate +- kind: decision +- logged: 2026-11-28 + +- Replaces "without a keychain the value is used as written" below. When no system keyring answers at all (a Linux desktop with no secret service, for example), the seal cannot be made, so a yes in settings.json cannot be told apart from one written by another program. `effective` then reads `agentsAllowed` as false (`seal: unavailable`), Ask and Blocked read as written, and the file is left as it is. `set_choice("allowed")` raises `NeedsKeyring` (the route answers 422 with a plain reason), and the question in the window offers only Deny and Allow once, so an Always answer cannot be given. `GET /api/agents` carries `keyring: false`, and Preferences shows a note and greys out Allowed. When a keyring is there nothing changes. +- `_ring()` is the one place keyring is imported, so the tests swap in an in-memory keyring (tests/conftest.py, autouse) and never reach the system one. + ## unreleased · a save never replaces a different movie - refers-to: routers/library.py :: _all_movies, _lands_on_other_movie, write_project diff --git a/docs/MANUAL.md b/docs/MANUAL.md index e18a05e..77ce90a 100644 --- a/docs/MANUAL.md +++ b/docs/MANUAL.md @@ -850,7 +850,7 @@ Open Preferences (Ctrl+,) and choose **Agents**. **Outside agents** has three ch | Allowed | Changes to the library go ahead without asking. An export or an import still asks each time. | | Blocked | Every agent is refused, reads included, and so are the agents in the terminal panel. A question that was waiting is answered Deny. | -An agent you open yourself in the terminal panel is treated as yours: under Ask or Allowed it makes library changes without asking, and an export or an import still asks. Where the system has a keychain, FormMorpheus seals the choice with a key kept there. If the setting is changed from outside the app to allow agents, FormMorpheus ignores that change, asks again, and says so in Preferences. +An agent you open yourself in the terminal panel is treated as yours: under Ask or Allowed it makes library changes without asking, and an export or an import still asks. FormMorpheus seals the choice with a key kept in the system keychain. If the setting is changed from outside the app to allow agents, FormMorpheus ignores that change, asks again, and says so in Preferences. On a computer with no system keyring at all, such as some Linux setups without a secret service, Allowed cannot be sealed, so it is not offered: outside agents ask each time, the question in the window has no Always allow, and Preferences says that Allowed and Always allow need a system keyring on this computer. ### Connect a client @@ -872,7 +872,7 @@ When an agent needs your yes, a box opens in the middle of the FormMorpheus wind - **Deny**: nothing is done. For the next 2 minutes the agent's requests that need your yes are refused without asking again. Escape also denies. - **Allow once**: this one request goes ahead. -- **Always allow all outside agents**: sets Outside agents to Allowed. It is not offered for an export or an import, which always ask. +- **Always allow all outside agents**: sets Outside agents to Allowed. It is not offered for an export or an import, which always ask, nor on a computer with no system keyring. If nobody answers within 2 minutes, the question closes and nothing is done. From 90470c0e874dedc63561a423447e9d27f18ca27b Mon Sep 17 00:00:00 2001 From: gbarnai Date: Sun, 29 Nov 2026 15:00:05 -0500 Subject: [PATCH 5/7] fix: a keyring that does not keep the seal key counts as no keyring Allowed now makes the seal key before it is written and is refused when the keyring will not keep it, so an unsealed yes is never stored, and an Always answer in that case counts as Allow once. --- backend/app/core/seal.py | 9 +++++++++ backend/app/routers/mcp.py | 5 ++++- backend/app/services/agent_access.py | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/backend/app/core/seal.py b/backend/app/core/seal.py index 1f85e7b..ccbe049 100644 --- a/backend/app/core/seal.py +++ b/backend/app/core/seal.py @@ -44,6 +44,10 @@ def _load(create: bool) -> bytes | None: stored = ring.get_password(SERVICE, ACCOUNT) key = bytes.fromhex(stored) if stored else None except Exception: + key = None + create = True + if create and key is None: + # a keyring that refuses the key or does not keep it cannot seal anything, so it counts as none _CACHE.update(available=False, key=None) return None _CACHE.update(available=True, key=key) @@ -55,6 +59,11 @@ def available() -> bool: return _CACHE["available"] +def ready() -> bool: + """True when the key is in the keyring, made now if need be, so a choice can be sealed.""" + return _load(True) is not None + + def sealed(values: dict) -> dict: blocked = {"agentsBlocked": True} if values.get("agentsBlocked") is True else {} return {"agentsAllowed": bool(values.get("agentsAllowed")), **blocked} diff --git a/backend/app/routers/mcp.py b/backend/app/routers/mcp.py index 1f97b8d..fe7cae5 100644 --- a/backend/app/routers/mcp.py +++ b/backend/app/routers/mcp.py @@ -253,7 +253,10 @@ async def _gate(a: Agent, kind: str, what: str) -> str | None: _declined[0] = time.monotonic() + DECLINE_PAUSE return "The person said no in the FormMorpheus window, so nothing was done." if said == "always" and always: - agent_access.set_choice("allowed") + try: + agent_access.set_choice("allowed") + except agent_access.NeedsKeyring: + pass # the keyring would not keep the key, so this answer counts as once return None diff --git a/backend/app/services/agent_access.py b/backend/app/services/agent_access.py index e148c14..af59a7f 100644 --- a/backend/app/services/agent_access.py +++ b/backend/app/services/agent_access.py @@ -26,7 +26,7 @@ def choice() -> str: def set_choice(value: str) -> dict: if value not in CHOICES: raise ValueError(f"The choice is one of {', '.join(CHOICES)}.") - if value == "allowed" and not can_allow(): + if value == "allowed" and not seal.ready(): raise NeedsKeyring("Allowed needs a system keyring on this computer, and none was found, so outside agents keep asking.") mark = {"allowed": {"agentsAllowed": True}, "blocked": {"agentsBlocked": True}}.get(value, {}) From 58f79726071901b679f06fd4b800979156c78a2c Mon Sep 17 00:00:00 2001 From: gbarnai Date: Sun, 29 Nov 2026 15:00:05 -0500 Subject: [PATCH 6/7] test: a keyring that keeps nothing leaves outside agents asking --- backend/tests/agents/test_agents_routes.py | 6 ++++++ backend/tests/conftest.py | 11 +++++++++++ backend/tests/core/test_seal.py | 11 +++++++++++ backend/tests/mcp/test_mcp_tools.py | 8 ++++++++ 4 files changed, 36 insertions(+) diff --git a/backend/tests/agents/test_agents_routes.py b/backend/tests/agents/test_agents_routes.py index 2c3bfc1..1e25f09 100644 --- a/backend/tests/agents/test_agents_routes.py +++ b/backend/tests/agents/test_agents_routes.py @@ -131,3 +131,9 @@ def test_without_a_keyring_the_route_refuses_allowed_with_a_plain_reason(client, assert res.status_code == 422 assert "keyring" in res.json()["detail"] assert client.get("/api/agents").json()["choice"] == "ask" + + +def test_a_keyring_that_keeps_nothing_refuses_allowed(data_dir, forgetful_ring): + with pytest.raises(agent_access.NeedsKeyring): + agent_access.set_choice("allowed") + assert agent_access.state() == {"choice": "ask", "changedOutside": False, "seal": "unavailable", "keyring": False} diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 8ffbb0c..f9ec0ee 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -54,6 +54,17 @@ def missing(*args): monkeypatch.setattr(seal, "_CACHE", {"tried": False, "available": False, "key": None}) +@pytest.fixture +def forgetful_ring(monkeypatch) -> None: + """A keyring that answers but keeps nothing it is given.""" + from types import SimpleNamespace + + from app.core import seal + + monkeypatch.setattr(seal, "_ring", lambda: SimpleNamespace(get_password=lambda *a: None, set_password=lambda *a: None)) + monkeypatch.setattr(seal, "_CACHE", {"tried": False, "available": False, "key": None}) + + @pytest.fixture def window_headers() -> dict: from app.core import window_key diff --git a/backend/tests/core/test_seal.py b/backend/tests/core/test_seal.py index 2fd4542..3fbe483 100644 --- a/backend/tests/core/test_seal.py +++ b/backend/tests/core/test_seal.py @@ -86,3 +86,14 @@ def test_reset_forgets_what_was_learnt_about_the_keychain(ring): assert seal._CACHE["key"] is not None seal.reset() assert seal._CACHE == {"tried": False, "available": False, "key": None} + + +def test_a_keyring_that_keeps_nothing_counts_as_none(forgetful_ring): + assert seal.ready() is False + assert seal.available() is False + assert seal.effective({"agentsAllowed": True})["seal"] == "unavailable" + + +def test_ready_makes_the_key_when_a_keyring_is_there(ring): + assert seal.ready() is True + assert seal.available() is True diff --git a/backend/tests/mcp/test_mcp_tools.py b/backend/tests/mcp/test_mcp_tools.py index 916ed90..ae83dcc 100644 --- a/backend/tests/mcp/test_mcp_tools.py +++ b/backend/tests/mcp/test_mcp_tools.py @@ -234,3 +234,11 @@ def test_without_a_keyring_an_always_answer_counts_as_once(client, monkeypatch, result = call(client, "manage_movie", {"action": "create", "name": "First"}) assert "isError" not in result assert agent_access.choice() == "ask" + + +def test_an_always_answer_the_keyring_cannot_keep_counts_as_once(client, monkeypatch, forgetful_ring): + answer_with(monkeypatch, "always") + result = call(client, "manage_movie", {"action": "create", "name": "First"}) + assert "isError" not in result + assert names(client) == ["First"] + assert agent_access.state()["keyring"] is False From 8fbe43696c84bf31dacb56a52080cc0303667d4a Mon Sep 17 00:00:00 2001 From: gbarnai Date: Sun, 29 Nov 2026 15:00:05 -0500 Subject: [PATCH 7/7] docs: record that a keyring which keeps nothing counts as none --- backend/HASHOUT_LOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/HASHOUT_LOG.md b/backend/HASHOUT_LOG.md index 0374197..056cfe6 100644 --- a/backend/HASHOUT_LOG.md +++ b/backend/HASHOUT_LOG.md @@ -15,6 +15,7 @@ Last updated: 2026-11-27 · scenes of your own - Replaces "without a keychain the value is used as written" below. When no system keyring answers at all (a Linux desktop with no secret service, for example), the seal cannot be made, so a yes in settings.json cannot be told apart from one written by another program. `effective` then reads `agentsAllowed` as false (`seal: unavailable`), Ask and Blocked read as written, and the file is left as it is. `set_choice("allowed")` raises `NeedsKeyring` (the route answers 422 with a plain reason), and the question in the window offers only Deny and Allow once, so an Always answer cannot be given. `GET /api/agents` carries `keyring: false`, and Preferences shows a note and greys out Allowed. When a keyring is there nothing changes. - `_ring()` is the one place keyring is imported, so the tests swap in an in-memory keyring (tests/conftest.py, autouse) and never reach the system one. +- A keyring that answers but refuses the key or does not keep it counts as none: `_load(True)` then marks the seal unavailable. `set_choice("allowed")` first makes the key (`seal.ready()`) and raises `NeedsKeyring` when it cannot, so an unsealed yes is never written, and an Always answer in that case counts as Allow once. ## unreleased · a save never replaces a different movie