@yahyahammad/venastine 0.3.8 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/.env.example +19 -19
  2. package/CONFIG_ARCHITECTURE.md +2030 -2002
  3. package/LICENSE +201 -201
  4. package/README.md +1034 -1034
  5. package/agents/builtin/compactor.md +75 -75
  6. package/agents/builtin/explore.md +77 -77
  7. package/agents/builtin/pipeline-reviewer.md +100 -100
  8. package/agents/builtin/plan.md +106 -106
  9. package/agents/builtin/review.md +74 -74
  10. package/agents/manager.py +261 -261
  11. package/agents/subagent_tool.py +472 -472
  12. package/agents/tui_commands.py +238 -146
  13. package/bin/venastine.mjs +528 -528
  14. package/bin/venastine.py +71 -71
  15. package/cli_commands.py +838 -835
  16. package/config.yaml +13 -0
  17. package/config_edit.py +1301 -1301
  18. package/config_schema.py +971 -966
  19. package/config_update.py +749 -749
  20. package/core/agent_activity.py +256 -256
  21. package/core/ansi.py +452 -452
  22. package/core/approval.py +137 -137
  23. package/core/call_view.py +364 -364
  24. package/core/cancel.py +121 -121
  25. package/core/chatgpt_auth.py +747 -747
  26. package/core/claims_view.py +107 -107
  27. package/core/client.py +2715 -2715
  28. package/core/commands.py +460 -454
  29. package/core/compaction.py +1735 -1735
  30. package/core/config_loader.py +1982 -1982
  31. package/core/connect.py +686 -686
  32. package/core/events.py +123 -123
  33. package/core/file_mentions.py +280 -280
  34. package/core/interaction.py +411 -411
  35. package/core/loop.py +2566 -2566
  36. package/core/memory.py +482 -482
  37. package/core/pipeline_models.py +190 -190
  38. package/core/provider_errors.py +234 -234
  39. package/core/reasoning/authorization.py +130 -130
  40. package/core/reasoning/base.py +219 -219
  41. package/core/reasoning/confidence_scoring.py +484 -484
  42. package/core/reasoning/events.py +149 -149
  43. package/core/reasoning/orchestrator.py +1561 -1561
  44. package/core/reasoning/output_writer.py +345 -345
  45. package/core/reasoning/payload_validation.py +524 -524
  46. package/core/reasoning/pipeline_storage.py +427 -427
  47. package/core/reasoning/scholar.py +458 -458
  48. package/core/reasoning/source_corpus.py +433 -433
  49. package/core/reasoning/source_scoring.py +855 -855
  50. package/core/replay.py +315 -315
  51. package/core/shell_sessions.py +1553 -1553
  52. package/credentials.py +307 -307
  53. package/database.py +333 -333
  54. package/docs/PRIVACY.md +78 -78
  55. package/docs/SECURITY.md +108 -108
  56. package/docs/THIRD_PARTY_NOTICES.md +148 -148
  57. package/env_secrets.py +118 -118
  58. package/json_store.py +343 -343
  59. package/logging_setup.py +295 -295
  60. package/main.py +3433 -3433
  61. package/mcp_client/client.py +733 -733
  62. package/mcp_client/config.py +484 -484
  63. package/mcp_client/registration.py +343 -343
  64. package/memories/manager.py +147 -147
  65. package/memories/tui_commands.py +319 -239
  66. package/package.json +303 -303
  67. package/project_init/config_files.py +282 -282
  68. package/project_init/generator.py +728 -728
  69. package/project_init/manifest.py +418 -418
  70. package/project_init/tui_commands.py +147 -147
  71. package/prompts/critic_pass.md +24 -24
  72. package/prompts/revalidate.md +25 -25
  73. package/prompts/revise.md +17 -17
  74. package/prompts/source_grounding.md +66 -66
  75. package/prompts/system_prompts.py +316 -316
  76. package/providers.json.example +94 -94
  77. package/pyproject.toml +149 -149
  78. package/requirements.txt +295 -295
  79. package/safety/policy_enforcement.py +1082 -1082
  80. package/security/askpass.py +419 -419
  81. package/security/capability.py +207 -207
  82. package/security/confine.py +484 -484
  83. package/security/hosted.py +182 -182
  84. package/security/permissions.py +237 -237
  85. package/security/protected_paths.py +501 -501
  86. package/security/sandbox.py +4108 -4108
  87. package/skills/builtin/math/formal-specification.md +67 -67
  88. package/skills/builtin/math/numerical-methods.md +66 -66
  89. package/skills/builtin/research/experiment-design.md +62 -62
  90. package/skills/builtin/research/reproducibility.md +55 -55
  91. package/skills/builtin/research/source-evaluation.md +70 -70
  92. package/skills/builtin/research/statistical-inference.md +63 -63
  93. package/skills/builtin/research/technical-writing.md +59 -59
  94. package/skills/builtin/software/code-review.md +61 -61
  95. package/skills/builtin/software/debugging.md +61 -61
  96. package/skills/builtin/software/test-design.md +67 -67
  97. package/skills/tui_commands.py +110 -110
  98. package/storage.py +1338 -1338
  99. package/tools/base.py +465 -465
  100. package/tools/builtin/_math_common.py +667 -667
  101. package/tools/builtin/_net_common.py +267 -267
  102. package/tools/builtin/arxiv.py +681 -681
  103. package/tools/builtin/fetch_url.py +371 -371
  104. package/tools/builtin/file_ops.py +720 -720
  105. package/tools/builtin/geometry.py +184 -184
  106. package/tools/builtin/load_skill.py +76 -76
  107. package/tools/builtin/pin.py +227 -227
  108. package/tools/builtin/probability_stats.py +224 -224
  109. package/tools/builtin/project_docs.py +464 -464
  110. package/tools/builtin/remember.py +175 -175
  111. package/tools/builtin/shell.py +980 -980
  112. package/tools/builtin/todo.py +149 -149
  113. package/tools/builtin/web_search.py +328 -328
  114. package/tools/context.py +159 -159
  115. package/tools/isolation.py +387 -387
  116. package/tools/registry.py +1242 -1242
  117. package/tui/app.py +8318 -8307
  118. package/tui/app.tcss +747 -739
  119. package/tui/commands.py +26 -26
  120. package/tui/diffs.py +286 -286
  121. package/tui/history.py +128 -128
  122. package/tui/hosted_driver.py +102 -102
  123. package/tui/markdown.py +1329 -1329
  124. package/tui/meters.py +364 -364
  125. package/tui/ravens.py +146 -146
  126. package/tui/screens.py +1255 -1197
  127. package/tui/themes.py +599 -599
  128. package/tui/widgets.py +3813 -3796
@@ -1,484 +1,484 @@
1
- """
2
- mcp_client/config.py
3
-
4
- Discovery and merge of `mcp.json`, plus the "seen this user-level server
5
- before" store. ROADMAP_v2 §17.
6
-
7
- Two locations, both resolved at CALL time (not import time) so tests can
8
- redirect them and a moved config dir is picked up without a restart --
9
- the same rule core/config_loader.py and core/workspace_trust.py follow:
10
-
11
- user ~/.config/venastine/mcp.json -- you authored it
12
- project <project>/.venastine/mcp.json -- trust-gated (D17)
13
-
14
- THE USER ONE IS CREATED EMPTY AT STARTUP IF IT IS ABSENT (batch 121,
15
- owner request). "You authored it" was doing double duty: it is the
16
- security argument for why that tier needs no trust prompt, and it was
17
- also the reason nothing ever created the file -- which left the path
18
- discoverable only from this docstring. Two plausible homes (the harness
19
- root, and here) and no way to tell from inside a session means an edit
20
- goes into a file nothing reads. See ensure_user_config.
21
-
22
- THE PROJECT PATH IS `.venastine/mcp.json`, NOT `<project>/mcp.json`.
23
- §17/D11 put it at the project root to match Claude Code's convention, but
24
- workspace_trust hashes only `.venastine/`, and is_trusted() returns True
25
- outright when that directory is absent. A root-level mcp.json would
26
- therefore have been ungated and its edits would never have re-prompted --
27
- in a file whose whole purpose is naming a local command to execute. Under
28
- `.venastine/` the existing hash, trust prompt and file listing cover it
29
- with no change to workspace_trust.py at all.
30
-
31
- Validation is deliberately by CONNECTION, not by schema (§17 decision G):
32
- unknown keys are ignored so a config pasted from Claude Desktop or Cursor
33
- works, and an entry we cannot make sense of becomes that one server's
34
- named connect failure rather than a startup error for everything else.
35
- """
36
-
37
- import hashlib
38
- import json
39
- import logging
40
- import os
41
- from dataclasses import dataclass, field
42
- from typing import Any, Optional
43
-
44
- from json_store import read_regular_text, write_json_atomic
45
-
46
- logger = logging.getLogger(__name__)
47
-
48
- # Keys this harness actually reads. Anything else is ignored, not an
49
- # error -- see the module docstring. Kept as data so the DEBUG log can
50
- # name exactly what it skipped.
51
- _KNOWN_SERVER_KEYS = frozenset({
52
- "command", "args", "env", "cwd", # stdio
53
- "type", "url", "headers", # http
54
- "autoApprove", "auto_approve", # our extension (§17 D)
55
- "disabled",
56
- })
57
-
58
- # Declared `type` values that mean streamable HTTP. `sse` is NOT one of
59
- # them (#62): D4 promises SSE, the SDK v2 ships sse_client for it, and
60
- # aliasing it here is how a server that speaks only SSE got offered a
61
- # streamable handshake it could not answer.
62
- _STREAMABLE_TYPES = frozenset({"http", "https", "streamable-http", "streamablehttp"})
63
- _SSE_TYPES = frozenset({"sse"})
64
-
65
-
66
- @dataclass
67
- class ServerConfig:
68
- """One entry from an mcp.json `mcpServers` object."""
69
- name: str
70
- tier: str # "user" | "project"
71
- path: str # the file it came from
72
- transport: str # "stdio" | "http" | "sse" | "unknown"
73
- command: Optional[str] = None
74
- args: list = field(default_factory=list)
75
- env: dict = field(default_factory=dict)
76
- cwd: Optional[str] = None
77
- url: Optional[str] = None
78
- headers: dict = field(default_factory=dict)
79
- auto_approve: bool = False
80
- disabled: bool = False
81
- raw: dict = field(default_factory=dict)
82
- error: Optional[str] = None # why transport == "unknown"
83
-
84
- def describe(self) -> str:
85
- """One line naming what this server will actually DO -- shown in
86
- the trust prompt and the first-run warning, where 'informed' has
87
- to mean seeing the command, not the server's nickname.
88
-
89
- #60/F1: the fields that change the security posture ride along.
90
- `autoApprove` decides whether any later prompt ever exists, so a
91
- one-`y` acknowledgement that never named it consented blind to
92
- unprompted execution in every future session including all ten
93
- headless research passes. Env is disclosed by KEY NAME only --
94
- printing values would put a credential on the terminal, which is
95
- a different defect. The digest is computed from the raw entry
96
- (entry_digest), never from this text, so improving the disclosure
97
- cannot by itself re-ask an acknowledged server; F3's store bump
98
- is what forces the one re-consent pass.
99
-
100
- VENASTINE_MCP_ACK_FULL=1 (F2) appends the entry verbatim, for
101
- debugging and audits. Read at call time, like every other state
102
- path in this project: it is operator authority, deliberately not
103
- a settings.json key, because settings.json's precedence lets a
104
- project tier style a consent screen this flag renders.
105
- """
106
- if self.transport == "stdio":
107
- argv = " ".join([self.command or ""] + [str(a) for a in self.args])
108
- text = f"{self.name} [{self.tier}] runs: {argv.strip()}"
109
- elif self.transport == "http":
110
- text = f"{self.name} [{self.tier}] connects to: {self.url}"
111
- elif self.transport == "sse":
112
- text = f"{self.name} [{self.tier}] connects via SSE to: {self.url}"
113
- else:
114
- return f"{self.name} [{self.tier}] UNUSABLE: {self.error}"
115
-
116
- notes = []
117
- if self.auto_approve:
118
- notes.append("AUTO-APPROVED: its tools will run without asking")
119
- if self.cwd:
120
- notes.append(f"working directory: {self.cwd}")
121
- if self.disabled:
122
- notes.append("disabled")
123
- if self.env:
124
- notes.append("env keys: " + ", ".join(sorted(self.env)))
125
- if self.headers:
126
- # Same class of surface as env keys, and MORE live since M19
127
- # stopped discarding them: Authorization is where an http/sse
128
- # server's credential actually rides (#60 owner follow-up).
129
- notes.append("header keys: " + ", ".join(sorted(self.headers)))
130
- lines = [text] + [f" {n}" for n in notes]
131
-
132
- if os.environ.get("VENASTINE_MCP_ACK_FULL") == "1":
133
- rendered = json.dumps(self.raw, indent=2, sort_keys=True,
134
- default=str)
135
- lines.append(" full entry:")
136
- lines.extend(f" {line}" for line in rendered.splitlines())
137
- return "\n".join(lines)
138
-
139
-
140
- def user_config_path() -> str:
141
- return os.path.expanduser("~/.config/venastine/mcp.json")
142
-
143
-
144
- def project_config_path(project_path: str) -> str:
145
- return os.path.join(os.path.realpath(project_path), ".venastine", "mcp.json")
146
-
147
-
148
- def ensure_user_config() -> Optional[str]:
149
- """Create the user-level mcp.json if absent. Returns the path, or None.
150
-
151
- Batch 121, owner request: "users don't know whether user-level
152
- mcp.json is at the harness root or ~/.config/venastine". A file that
153
- exists answers that; a docstring does not.
154
-
155
- BEHAVIOUR-NEUTRAL BY CONSTRUCTION. `{"mcpServers": {}}` loads as zero
156
- servers, and it is the one shape _read_file does not warn about -- an
157
- empty `{}` trips its "no 'mcpServers' object" branch, so the obvious
158
- seed would have printed a warning about the harness's own file at
159
- every launch. The skeleton comes from §24's render_mcp() rather than
160
- being retyped here, so the two tiers cannot drift.
161
-
162
- NEVER OVERWRITES, and the check is existence rather than content: a
163
- file someone emptied by hand is still their file.
164
-
165
- An OSError is a DEBUG line and None. A read-only config directory is
166
- a reason not to have the convenience, not a reason not to start --
167
- every other store in that directory fails the same way, except the
168
- trust store, which raises because a security decision that silently
169
- did not persist is worse than a crash. This is not one of those.
170
-
171
- mode=0o600 like its two siblings there: the entries can carry an
172
- Authorization header, and on the temp file rather than the
173
- destination because os.replace carries the source's mode across
174
- (#19). trailing_newline because a human edits this one -- §24's
175
- scaffold passes it for exactly that reason.
176
- """
177
- from project_init.config_files import render_mcp
178
-
179
- path = user_config_path()
180
- if os.path.exists(path):
181
- return None
182
- try:
183
- write_json_atomic(path, render_mcp(), mode=0o600,
184
- trailing_newline=True)
185
- except OSError as e:
186
- logger.debug("Could not create %s: %s", path, e)
187
- return None
188
- logger.info("Created user-level mcp.json at %s", path)
189
- return path
190
-
191
-
192
- def _flag(server: str, key: str, value: Any,
193
- consequence: str) -> Optional[bool]:
194
- """A JSON boolean, or None with a warning naming the consequence.
195
-
196
- NOT bool(value): bool("false") is True, so `"autoApprove": "false"`
197
- -- a plausible hand edit, and a shape other MCP hosts tolerate --
198
- would turn the D28 approval gate OFF on a server the user was trying
199
- to keep gated.
200
-
201
- WHAT IT DOES NOT DO ANY MORE IS CHOOSE THE ANSWER (batch 150). It
202
- used to return False for both keys and say that failing closed was
203
- "the only safe direction for both flags: an unexpectedly-gated server
204
- prompts, an unexpectedly-enabled one runs third-party code
205
- unannounced". That is true of `autoApprove` and backwards for
206
- `disabled`, where False means NOT disabled -- so the sentence's own
207
- second clause was the outcome it produced: `"disabled": "true"` ran
208
- the server, and F1's consent line, which reads self.disabled, showed
209
- it as live.
210
-
211
- AND NEITHER CONSTANT IS SAFE FOR `disabled`, which is why this
212
- function no longer picks one. The two plausible misspellings want
213
- opposite answers -- `"true"` means stop and `"false"` means run --
214
- and the only thing that distinguishes them is the value, which this
215
- function exists to refuse to interpret. So the caller decides, and
216
- for `disabled` the decision is the one this module already makes for
217
- an entry it cannot make sense of: that one server's NAMED failure
218
- (decision G). `tests/test_mcp_config.py` pins both directions.
219
- """
220
- if isinstance(value, bool):
221
- return value
222
- logger.warning(
223
- "mcp.json server %r: %s must be true or false, got %r; %s.",
224
- server, key, value, consequence)
225
- return None
226
-
227
-
228
- def _parse_entry(name: str, entry: Any, tier: str, path: str) -> ServerConfig:
229
- """Turns one raw JSON value into a ServerConfig. Never raises: a bad
230
- entry is returned with transport == 'unknown' and an error string, so
231
- one malformed server cannot stop the others from connecting."""
232
- if not isinstance(entry, dict):
233
- return ServerConfig(name=name, tier=tier, path=path, transport="unknown",
234
- error=f"expected an object, got {type(entry).__name__}")
235
-
236
- unknown = set(entry) - _KNOWN_SERVER_KEYS
237
- if unknown:
238
- # DEBUG, not WARNING: configs shared with other MCP clients carry
239
- # keys we don't implement, and making that noisy on every startup
240
- # trains people to ignore the log.
241
- logger.debug("mcp.json server %r: ignoring unrecognized keys %s",
242
- name, sorted(unknown))
243
-
244
- # accept either spelling; ours is camelCase to match the file's
245
- # existing style, but a snake_case one shouldn't silently no-op
246
- auto_approve = _flag(
247
- name, "autoApprove",
248
- entry.get("autoApprove", entry.get("auto_approve", False)),
249
- "gating its tools, which is this flag's safe answer either way")
250
- disabled = _flag(
251
- name, "disabled", entry.get("disabled", False),
252
- "refusing the entry by name, because neither answer is safe for "
253
- "this flag")
254
-
255
- cfg = ServerConfig(
256
- name=name, tier=tier, path=path, transport="unknown",
257
- env=entry.get("env") or {},
258
- cwd=entry.get("cwd"),
259
- headers=entry.get("headers") or {},
260
- auto_approve=bool(auto_approve),
261
- disabled=bool(disabled),
262
- raw=entry,
263
- )
264
-
265
- if disabled is None:
266
- # A NAMED FAILURE, not a chosen answer (batch 150). `False` ran a
267
- # server the operator had written a key to stop, and `True` would
268
- # skip one they had written a key to keep -- which was a defect
269
- # this file's tests had already been written for once, under the
270
- # name "the mirror defect". Both directions are pinned now by
271
- # TestAnUnparseableDisabledIsANamedFailure, whose docstring
272
- # records why the older test was replaced rather than kept green:
273
- # it asserted `disabled is False`, which is STILL true here and no
274
- # longer means the server runs.
275
- # So the entry becomes unusable the same way a bad `args` does:
276
- # transport stays 'unknown', describe() prints
277
- # "UNUSABLE: 'disabled' must be true or false" on the
278
- # acknowledgement screen, _transport_for raises so the server
279
- # lands in failures by name, and main.py prints it. Nothing runs
280
- # and nothing is silent, which is the only pair of properties
281
- # both spellings can have at once. One edit fixes it.
282
- cfg.error = "'disabled' must be true or false"
283
- return cfg
284
-
285
- declared = str(entry.get("type") or "").lower()
286
- if entry.get("command"):
287
- raw_args = entry.get("args") or []
288
- if not isinstance(raw_args, list):
289
- # list("--verbose") is ['-', '-', 'v', ...]. A natural hand
290
- # edit therefore became a per-character argv and failed at
291
- # connect with an opaque error naming none of the cause,
292
- # defeating this module's rule that a bad entry becomes that
293
- # one server's NAMED failure.
294
- cfg.error = "'args' must be an array of strings"
295
- return cfg
296
- cfg.transport = "stdio"
297
- cfg.command = str(entry["command"])
298
- cfg.args = list(raw_args)
299
- # SSE only by EXPLICIT declaration (F5): a bare "url" keeps meaning
300
- # streamable HTTP, because that is what every config written before
301
- # this branch existed assumed, and URL sniffing would be magic. The
302
- # aliases below stay streamable; an unsupported declared type still
303
- # refuses rather than falling through to HTTP (#61's M7).
304
- elif entry.get("url") and declared in _SSE_TYPES:
305
- cfg.transport = "sse"
306
- cfg.url = str(entry["url"])
307
- elif entry.get("url") and (not declared or declared in _STREAMABLE_TYPES):
308
- cfg.transport = "http"
309
- cfg.url = str(entry["url"])
310
- elif entry.get("url"):
311
- cfg.error = f"unsupported transport type {declared!r}"
312
- else:
313
- cfg.error = "no 'command' (stdio) and no 'url' (http)"
314
- return cfg
315
-
316
-
317
- def _read_file(path: str, tier: str) -> dict:
318
- """Reads one mcp.json. A missing file is empty, not an error. A
319
- corrupt one is reported and treated as empty -- consistent with G:
320
- it must not take down a session over a file the user may not have
321
- written, and the message names the file."""
322
- if not os.path.exists(path):
323
- return {}
324
- try:
325
- # utf-8-sig: mcp.json is commonly copied between hosts and hand
326
- # edited, so a BOM is realistic and would otherwise be reported as
327
- # invalid JSON at character 0. Regular files only (batch 219, #214):
328
- # the project tier's copy arrives with a clone, and one linked to
329
- # /dev/zero would be read until memory ran out.
330
- data = json.loads(read_regular_text(path))
331
- except (OSError, ValueError) as e:
332
- logger.warning("Could not read %s-level mcp.json at %s: %s", tier, path, e)
333
- return {}
334
-
335
- servers = data.get("mcpServers") if isinstance(data, dict) else None
336
- if servers is None and isinstance(data, dict):
337
- # Tolerate a bare {"name": {...}} mapping; some tools write it.
338
- servers = {k: v for k, v in data.items() if isinstance(v, dict)}
339
- if not isinstance(servers, dict):
340
- logger.warning("%s-level mcp.json at %s has no 'mcpServers' object; "
341
- "ignoring it.", tier, path)
342
- return {}
343
-
344
- return {name: _parse_entry(name, entry, tier, path)
345
- for name, entry in servers.items()}
346
-
347
-
348
- def load_server_configs(project_path: str, trusted: bool) -> dict:
349
- """All configured servers, keyed by name.
350
-
351
- USER BEATS PROJECT on a name collision (§17 decision H), which is the
352
- OPPOSITE of a plain "more specific wins" rule and deliberately so:
353
- the user tier is what you authored, the project tier is what arrived
354
- with a repo you cloned. A shadowed agent changes a prompt; a shadowed
355
- MCP server changes which local command runs under a name you already
356
- trust. core/config_loader.py's tier order was changed to match, so
357
- agents, skills and MCP servers now all resolve the same way.
358
-
359
- Non-colliding servers from BOTH tiers load -- precedence only ever
360
- breaks same-name ties.
361
-
362
- Project entries are omitted entirely when `trusted` is False. Absent,
363
- not loaded-and-disabled: untrusted content should not be reachable by
364
- a later bug (§14's rule, applied here).
365
- """
366
- merged: dict = {}
367
-
368
- for cfg in _read_file(user_config_path(), "user").values():
369
- merged[cfg.name] = cfg
370
-
371
- if trusted:
372
- for cfg in _read_file(project_config_path(project_path), "project").values():
373
- if cfg.name in merged:
374
- logger.warning(
375
- "project-level MCP server %r at %s is shadowed by the "
376
- "user-level definition at %s, which wins.",
377
- cfg.name, cfg.path, merged[cfg.name].path,
378
- )
379
- continue
380
- merged[cfg.name] = cfg
381
-
382
- return merged
383
-
384
-
385
- # --- first-run acknowledgement for user-level servers (§17 decision I) ----
386
- #
387
- # Project-level servers are covered by D17's trust prompt. User-level ones
388
- # have no gate at all, on the assumption you authored them -- which is
389
- # exactly wrong when an installer or another tool appended to the file.
390
- # Same content-hash keying as workspace_trust, so an edited command
391
- # re-asks rather than inheriting an old acknowledgement.
392
-
393
- def known_servers_path() -> str:
394
- return os.path.expanduser("~/.config/venastine/known_mcp_servers.json")
395
-
396
-
397
- def entry_digest(cfg: ServerConfig) -> str:
398
- """Hash of what the entry will DO. sort_keys so key order in the file
399
- can't produce a different digest for identical configuration."""
400
- return hashlib.sha256(
401
- json.dumps(cfg.raw, sort_keys=True, default=str).encode("utf-8")
402
- ).hexdigest()
403
-
404
-
405
- # v2 (#60/F3): the acknowledgement prompt used to omit autoApprove -- the
406
- # one field that decides whether any future prompt exists -- so consents
407
- # recorded under v1 were given blind. The store carries a version; a
408
- # legacy store's entries are treated as UNKNOWN (one re-ask each, under
409
- # the improved disclosure) and a blind consent never survives the bump.
410
- KNOWN_STORE_VERSION = 2
411
-
412
-
413
- def _read_store() -> tuple:
414
- """(name -> digest mapping, is_legacy). A missing file is empty and
415
- current. An unreadable one fails CLOSED as empty-and-current: every
416
- server then looks new and gets re-confirmed, which is the safe
417
- direction. A readable store in the OLD flat shape is NOT corrupt --
418
- it is valid v1, and comes back marked legacy."""
419
- path = known_servers_path()
420
- if not os.path.exists(path):
421
- return {}, False
422
- try:
423
- with open(path, encoding="utf-8-sig") as f:
424
- data = json.load(f)
425
- except (OSError, ValueError):
426
- logger.warning("Could not read %s; re-confirming every user-level "
427
- "MCP server.", path)
428
- return {}, False
429
- if not isinstance(data, dict):
430
- logger.warning("%s is not an object; re-confirming every "
431
- "user-level MCP server.", path)
432
- return {}, False
433
- if data.get("version") == KNOWN_STORE_VERSION \
434
- and isinstance(data.get("servers"), dict):
435
- return dict(data["servers"]), False
436
- return {k: v for k, v in data.items()
437
- if isinstance(k, str)}, True
438
-
439
-
440
- def is_known(cfg: ServerConfig) -> bool:
441
- """Whether this exact server entry has already been acknowledged.
442
-
443
- Reads the store itself rather than taking one. It used to accept a
444
- pre-read `store=` for callers batching several checks, and no caller
445
- ever passed one -- so the branch was dead while the parameter went on
446
- advertising an optimisation nobody could see was unused.
447
- """
448
- servers, legacy = _read_store()
449
- # F3: a v1 consent was given under a prompt that never named the
450
- # field it most needed to name. It does not count.
451
- if legacy:
452
- return False
453
- return servers.get(cfg.name) == entry_digest(cfg)
454
-
455
-
456
- def remember_server(cfg: ServerConfig) -> None:
457
- path = known_servers_path()
458
- servers, legacy = _read_store()
459
- if legacy:
460
- # Carrying the old digests forward would silently convert blind
461
- # consents into disclosed ones for servers this session never
462
- # re-asked. They start over; answering y here records THIS server
463
- # only, and the others keep asking until they are answered.
464
- servers = {}
465
- servers[cfg.name] = entry_digest(cfg)
466
- # ATOMIC since batch 45, and this was the odd one out. Its three
467
- # sibling stores each wrote through a temp file and os.replace, and
468
- # each spent four to eight docstring lines on why -- a
469
- # truncate-then-write leaves the file empty for the length of the
470
- # write and permanently damaged if the process dies inside it. This
471
- # one used a bare open(path, "w") and said nothing, for a file that
472
- # records CONSENT: the same class of data as the trust store, which
473
- # is the sibling that argues hardest for the guarantee. Sharing the
474
- # write is what stops a convention four comments long from having a
475
- # fourth copy that does not follow it.
476
- #
477
- # AND ITS MODE, since batch 170: 0o600, as the trust store has. Without
478
- # one this took open()'s 0o666 less the umask, so under a umask of 002
479
- # anyone in the user's group could write a consent here and a server
480
- # would start without asking. Found by triaging CodeQL's alert on
481
- # json_store's default mode; the default itself is right for the files
482
- # the agent writes in a workspace.
483
- write_json_atomic(path, {"version": KNOWN_STORE_VERSION,
484
- "servers": servers}, mode=0o600)
1
+ """
2
+ mcp_client/config.py
3
+
4
+ Discovery and merge of `mcp.json`, plus the "seen this user-level server
5
+ before" store. ROADMAP_v2 §17.
6
+
7
+ Two locations, both resolved at CALL time (not import time) so tests can
8
+ redirect them and a moved config dir is picked up without a restart --
9
+ the same rule core/config_loader.py and core/workspace_trust.py follow:
10
+
11
+ user ~/.config/venastine/mcp.json -- you authored it
12
+ project <project>/.venastine/mcp.json -- trust-gated (D17)
13
+
14
+ THE USER ONE IS CREATED EMPTY AT STARTUP IF IT IS ABSENT (batch 121,
15
+ owner request). "You authored it" was doing double duty: it is the
16
+ security argument for why that tier needs no trust prompt, and it was
17
+ also the reason nothing ever created the file -- which left the path
18
+ discoverable only from this docstring. Two plausible homes (the harness
19
+ root, and here) and no way to tell from inside a session means an edit
20
+ goes into a file nothing reads. See ensure_user_config.
21
+
22
+ THE PROJECT PATH IS `.venastine/mcp.json`, NOT `<project>/mcp.json`.
23
+ §17/D11 put it at the project root to match Claude Code's convention, but
24
+ workspace_trust hashes only `.venastine/`, and is_trusted() returns True
25
+ outright when that directory is absent. A root-level mcp.json would
26
+ therefore have been ungated and its edits would never have re-prompted --
27
+ in a file whose whole purpose is naming a local command to execute. Under
28
+ `.venastine/` the existing hash, trust prompt and file listing cover it
29
+ with no change to workspace_trust.py at all.
30
+
31
+ Validation is deliberately by CONNECTION, not by schema (§17 decision G):
32
+ unknown keys are ignored so a config pasted from Claude Desktop or Cursor
33
+ works, and an entry we cannot make sense of becomes that one server's
34
+ named connect failure rather than a startup error for everything else.
35
+ """
36
+
37
+ import hashlib
38
+ import json
39
+ import logging
40
+ import os
41
+ from dataclasses import dataclass, field
42
+ from typing import Any, Optional
43
+
44
+ from json_store import read_regular_text, write_json_atomic
45
+
46
+ logger = logging.getLogger(__name__)
47
+
48
+ # Keys this harness actually reads. Anything else is ignored, not an
49
+ # error -- see the module docstring. Kept as data so the DEBUG log can
50
+ # name exactly what it skipped.
51
+ _KNOWN_SERVER_KEYS = frozenset({
52
+ "command", "args", "env", "cwd", # stdio
53
+ "type", "url", "headers", # http
54
+ "autoApprove", "auto_approve", # our extension (§17 D)
55
+ "disabled",
56
+ })
57
+
58
+ # Declared `type` values that mean streamable HTTP. `sse` is NOT one of
59
+ # them (#62): D4 promises SSE, the SDK v2 ships sse_client for it, and
60
+ # aliasing it here is how a server that speaks only SSE got offered a
61
+ # streamable handshake it could not answer.
62
+ _STREAMABLE_TYPES = frozenset({"http", "https", "streamable-http", "streamablehttp"})
63
+ _SSE_TYPES = frozenset({"sse"})
64
+
65
+
66
+ @dataclass
67
+ class ServerConfig:
68
+ """One entry from an mcp.json `mcpServers` object."""
69
+ name: str
70
+ tier: str # "user" | "project"
71
+ path: str # the file it came from
72
+ transport: str # "stdio" | "http" | "sse" | "unknown"
73
+ command: Optional[str] = None
74
+ args: list = field(default_factory=list)
75
+ env: dict = field(default_factory=dict)
76
+ cwd: Optional[str] = None
77
+ url: Optional[str] = None
78
+ headers: dict = field(default_factory=dict)
79
+ auto_approve: bool = False
80
+ disabled: bool = False
81
+ raw: dict = field(default_factory=dict)
82
+ error: Optional[str] = None # why transport == "unknown"
83
+
84
+ def describe(self) -> str:
85
+ """One line naming what this server will actually DO -- shown in
86
+ the trust prompt and the first-run warning, where 'informed' has
87
+ to mean seeing the command, not the server's nickname.
88
+
89
+ #60/F1: the fields that change the security posture ride along.
90
+ `autoApprove` decides whether any later prompt ever exists, so a
91
+ one-`y` acknowledgement that never named it consented blind to
92
+ unprompted execution in every future session including all ten
93
+ headless research passes. Env is disclosed by KEY NAME only --
94
+ printing values would put a credential on the terminal, which is
95
+ a different defect. The digest is computed from the raw entry
96
+ (entry_digest), never from this text, so improving the disclosure
97
+ cannot by itself re-ask an acknowledged server; F3's store bump
98
+ is what forces the one re-consent pass.
99
+
100
+ VENASTINE_MCP_ACK_FULL=1 (F2) appends the entry verbatim, for
101
+ debugging and audits. Read at call time, like every other state
102
+ path in this project: it is operator authority, deliberately not
103
+ a settings.json key, because settings.json's precedence lets a
104
+ project tier style a consent screen this flag renders.
105
+ """
106
+ if self.transport == "stdio":
107
+ argv = " ".join([self.command or ""] + [str(a) for a in self.args])
108
+ text = f"{self.name} [{self.tier}] runs: {argv.strip()}"
109
+ elif self.transport == "http":
110
+ text = f"{self.name} [{self.tier}] connects to: {self.url}"
111
+ elif self.transport == "sse":
112
+ text = f"{self.name} [{self.tier}] connects via SSE to: {self.url}"
113
+ else:
114
+ return f"{self.name} [{self.tier}] UNUSABLE: {self.error}"
115
+
116
+ notes = []
117
+ if self.auto_approve:
118
+ notes.append("AUTO-APPROVED: its tools will run without asking")
119
+ if self.cwd:
120
+ notes.append(f"working directory: {self.cwd}")
121
+ if self.disabled:
122
+ notes.append("disabled")
123
+ if self.env:
124
+ notes.append("env keys: " + ", ".join(sorted(self.env)))
125
+ if self.headers:
126
+ # Same class of surface as env keys, and MORE live since M19
127
+ # stopped discarding them: Authorization is where an http/sse
128
+ # server's credential actually rides (#60 owner follow-up).
129
+ notes.append("header keys: " + ", ".join(sorted(self.headers)))
130
+ lines = [text] + [f" {n}" for n in notes]
131
+
132
+ if os.environ.get("VENASTINE_MCP_ACK_FULL") == "1":
133
+ rendered = json.dumps(self.raw, indent=2, sort_keys=True,
134
+ default=str)
135
+ lines.append(" full entry:")
136
+ lines.extend(f" {line}" for line in rendered.splitlines())
137
+ return "\n".join(lines)
138
+
139
+
140
+ def user_config_path() -> str:
141
+ return os.path.expanduser("~/.config/venastine/mcp.json")
142
+
143
+
144
+ def project_config_path(project_path: str) -> str:
145
+ return os.path.join(os.path.realpath(project_path), ".venastine", "mcp.json")
146
+
147
+
148
+ def ensure_user_config() -> Optional[str]:
149
+ """Create the user-level mcp.json if absent. Returns the path, or None.
150
+
151
+ Batch 121, owner request: "users don't know whether user-level
152
+ mcp.json is at the harness root or ~/.config/venastine". A file that
153
+ exists answers that; a docstring does not.
154
+
155
+ BEHAVIOUR-NEUTRAL BY CONSTRUCTION. `{"mcpServers": {}}` loads as zero
156
+ servers, and it is the one shape _read_file does not warn about -- an
157
+ empty `{}` trips its "no 'mcpServers' object" branch, so the obvious
158
+ seed would have printed a warning about the harness's own file at
159
+ every launch. The skeleton comes from §24's render_mcp() rather than
160
+ being retyped here, so the two tiers cannot drift.
161
+
162
+ NEVER OVERWRITES, and the check is existence rather than content: a
163
+ file someone emptied by hand is still their file.
164
+
165
+ An OSError is a DEBUG line and None. A read-only config directory is
166
+ a reason not to have the convenience, not a reason not to start --
167
+ every other store in that directory fails the same way, except the
168
+ trust store, which raises because a security decision that silently
169
+ did not persist is worse than a crash. This is not one of those.
170
+
171
+ mode=0o600 like its two siblings there: the entries can carry an
172
+ Authorization header, and on the temp file rather than the
173
+ destination because os.replace carries the source's mode across
174
+ (#19). trailing_newline because a human edits this one -- §24's
175
+ scaffold passes it for exactly that reason.
176
+ """
177
+ from project_init.config_files import render_mcp
178
+
179
+ path = user_config_path()
180
+ if os.path.exists(path):
181
+ return None
182
+ try:
183
+ write_json_atomic(path, render_mcp(), mode=0o600,
184
+ trailing_newline=True)
185
+ except OSError as e:
186
+ logger.debug("Could not create %s: %s", path, e)
187
+ return None
188
+ logger.info("Created user-level mcp.json at %s", path)
189
+ return path
190
+
191
+
192
+ def _flag(server: str, key: str, value: Any,
193
+ consequence: str) -> Optional[bool]:
194
+ """A JSON boolean, or None with a warning naming the consequence.
195
+
196
+ NOT bool(value): bool("false") is True, so `"autoApprove": "false"`
197
+ -- a plausible hand edit, and a shape other MCP hosts tolerate --
198
+ would turn the D28 approval gate OFF on a server the user was trying
199
+ to keep gated.
200
+
201
+ WHAT IT DOES NOT DO ANY MORE IS CHOOSE THE ANSWER (batch 150). It
202
+ used to return False for both keys and say that failing closed was
203
+ "the only safe direction for both flags: an unexpectedly-gated server
204
+ prompts, an unexpectedly-enabled one runs third-party code
205
+ unannounced". That is true of `autoApprove` and backwards for
206
+ `disabled`, where False means NOT disabled -- so the sentence's own
207
+ second clause was the outcome it produced: `"disabled": "true"` ran
208
+ the server, and F1's consent line, which reads self.disabled, showed
209
+ it as live.
210
+
211
+ AND NEITHER CONSTANT IS SAFE FOR `disabled`, which is why this
212
+ function no longer picks one. The two plausible misspellings want
213
+ opposite answers -- `"true"` means stop and `"false"` means run --
214
+ and the only thing that distinguishes them is the value, which this
215
+ function exists to refuse to interpret. So the caller decides, and
216
+ for `disabled` the decision is the one this module already makes for
217
+ an entry it cannot make sense of: that one server's NAMED failure
218
+ (decision G). `tests/test_mcp_config.py` pins both directions.
219
+ """
220
+ if isinstance(value, bool):
221
+ return value
222
+ logger.warning(
223
+ "mcp.json server %r: %s must be true or false, got %r; %s.",
224
+ server, key, value, consequence)
225
+ return None
226
+
227
+
228
+ def _parse_entry(name: str, entry: Any, tier: str, path: str) -> ServerConfig:
229
+ """Turns one raw JSON value into a ServerConfig. Never raises: a bad
230
+ entry is returned with transport == 'unknown' and an error string, so
231
+ one malformed server cannot stop the others from connecting."""
232
+ if not isinstance(entry, dict):
233
+ return ServerConfig(name=name, tier=tier, path=path, transport="unknown",
234
+ error=f"expected an object, got {type(entry).__name__}")
235
+
236
+ unknown = set(entry) - _KNOWN_SERVER_KEYS
237
+ if unknown:
238
+ # DEBUG, not WARNING: configs shared with other MCP clients carry
239
+ # keys we don't implement, and making that noisy on every startup
240
+ # trains people to ignore the log.
241
+ logger.debug("mcp.json server %r: ignoring unrecognized keys %s",
242
+ name, sorted(unknown))
243
+
244
+ # accept either spelling; ours is camelCase to match the file's
245
+ # existing style, but a snake_case one shouldn't silently no-op
246
+ auto_approve = _flag(
247
+ name, "autoApprove",
248
+ entry.get("autoApprove", entry.get("auto_approve", False)),
249
+ "gating its tools, which is this flag's safe answer either way")
250
+ disabled = _flag(
251
+ name, "disabled", entry.get("disabled", False),
252
+ "refusing the entry by name, because neither answer is safe for "
253
+ "this flag")
254
+
255
+ cfg = ServerConfig(
256
+ name=name, tier=tier, path=path, transport="unknown",
257
+ env=entry.get("env") or {},
258
+ cwd=entry.get("cwd"),
259
+ headers=entry.get("headers") or {},
260
+ auto_approve=bool(auto_approve),
261
+ disabled=bool(disabled),
262
+ raw=entry,
263
+ )
264
+
265
+ if disabled is None:
266
+ # A NAMED FAILURE, not a chosen answer (batch 150). `False` ran a
267
+ # server the operator had written a key to stop, and `True` would
268
+ # skip one they had written a key to keep -- which was a defect
269
+ # this file's tests had already been written for once, under the
270
+ # name "the mirror defect". Both directions are pinned now by
271
+ # TestAnUnparseableDisabledIsANamedFailure, whose docstring
272
+ # records why the older test was replaced rather than kept green:
273
+ # it asserted `disabled is False`, which is STILL true here and no
274
+ # longer means the server runs.
275
+ # So the entry becomes unusable the same way a bad `args` does:
276
+ # transport stays 'unknown', describe() prints
277
+ # "UNUSABLE: 'disabled' must be true or false" on the
278
+ # acknowledgement screen, _transport_for raises so the server
279
+ # lands in failures by name, and main.py prints it. Nothing runs
280
+ # and nothing is silent, which is the only pair of properties
281
+ # both spellings can have at once. One edit fixes it.
282
+ cfg.error = "'disabled' must be true or false"
283
+ return cfg
284
+
285
+ declared = str(entry.get("type") or "").lower()
286
+ if entry.get("command"):
287
+ raw_args = entry.get("args") or []
288
+ if not isinstance(raw_args, list):
289
+ # list("--verbose") is ['-', '-', 'v', ...]. A natural hand
290
+ # edit therefore became a per-character argv and failed at
291
+ # connect with an opaque error naming none of the cause,
292
+ # defeating this module's rule that a bad entry becomes that
293
+ # one server's NAMED failure.
294
+ cfg.error = "'args' must be an array of strings"
295
+ return cfg
296
+ cfg.transport = "stdio"
297
+ cfg.command = str(entry["command"])
298
+ cfg.args = list(raw_args)
299
+ # SSE only by EXPLICIT declaration (F5): a bare "url" keeps meaning
300
+ # streamable HTTP, because that is what every config written before
301
+ # this branch existed assumed, and URL sniffing would be magic. The
302
+ # aliases below stay streamable; an unsupported declared type still
303
+ # refuses rather than falling through to HTTP (#61's M7).
304
+ elif entry.get("url") and declared in _SSE_TYPES:
305
+ cfg.transport = "sse"
306
+ cfg.url = str(entry["url"])
307
+ elif entry.get("url") and (not declared or declared in _STREAMABLE_TYPES):
308
+ cfg.transport = "http"
309
+ cfg.url = str(entry["url"])
310
+ elif entry.get("url"):
311
+ cfg.error = f"unsupported transport type {declared!r}"
312
+ else:
313
+ cfg.error = "no 'command' (stdio) and no 'url' (http)"
314
+ return cfg
315
+
316
+
317
+ def _read_file(path: str, tier: str) -> dict:
318
+ """Reads one mcp.json. A missing file is empty, not an error. A
319
+ corrupt one is reported and treated as empty -- consistent with G:
320
+ it must not take down a session over a file the user may not have
321
+ written, and the message names the file."""
322
+ if not os.path.exists(path):
323
+ return {}
324
+ try:
325
+ # utf-8-sig: mcp.json is commonly copied between hosts and hand
326
+ # edited, so a BOM is realistic and would otherwise be reported as
327
+ # invalid JSON at character 0. Regular files only (batch 219, #214):
328
+ # the project tier's copy arrives with a clone, and one linked to
329
+ # /dev/zero would be read until memory ran out.
330
+ data = json.loads(read_regular_text(path))
331
+ except (OSError, ValueError) as e:
332
+ logger.warning("Could not read %s-level mcp.json at %s: %s", tier, path, e)
333
+ return {}
334
+
335
+ servers = data.get("mcpServers") if isinstance(data, dict) else None
336
+ if servers is None and isinstance(data, dict):
337
+ # Tolerate a bare {"name": {...}} mapping; some tools write it.
338
+ servers = {k: v for k, v in data.items() if isinstance(v, dict)}
339
+ if not isinstance(servers, dict):
340
+ logger.warning("%s-level mcp.json at %s has no 'mcpServers' object; "
341
+ "ignoring it.", tier, path)
342
+ return {}
343
+
344
+ return {name: _parse_entry(name, entry, tier, path)
345
+ for name, entry in servers.items()}
346
+
347
+
348
+ def load_server_configs(project_path: str, trusted: bool) -> dict:
349
+ """All configured servers, keyed by name.
350
+
351
+ USER BEATS PROJECT on a name collision (§17 decision H), which is the
352
+ OPPOSITE of a plain "more specific wins" rule and deliberately so:
353
+ the user tier is what you authored, the project tier is what arrived
354
+ with a repo you cloned. A shadowed agent changes a prompt; a shadowed
355
+ MCP server changes which local command runs under a name you already
356
+ trust. core/config_loader.py's tier order was changed to match, so
357
+ agents, skills and MCP servers now all resolve the same way.
358
+
359
+ Non-colliding servers from BOTH tiers load -- precedence only ever
360
+ breaks same-name ties.
361
+
362
+ Project entries are omitted entirely when `trusted` is False. Absent,
363
+ not loaded-and-disabled: untrusted content should not be reachable by
364
+ a later bug (§14's rule, applied here).
365
+ """
366
+ merged: dict = {}
367
+
368
+ for cfg in _read_file(user_config_path(), "user").values():
369
+ merged[cfg.name] = cfg
370
+
371
+ if trusted:
372
+ for cfg in _read_file(project_config_path(project_path), "project").values():
373
+ if cfg.name in merged:
374
+ logger.warning(
375
+ "project-level MCP server %r at %s is shadowed by the "
376
+ "user-level definition at %s, which wins.",
377
+ cfg.name, cfg.path, merged[cfg.name].path,
378
+ )
379
+ continue
380
+ merged[cfg.name] = cfg
381
+
382
+ return merged
383
+
384
+
385
+ # --- first-run acknowledgement for user-level servers (§17 decision I) ----
386
+ #
387
+ # Project-level servers are covered by D17's trust prompt. User-level ones
388
+ # have no gate at all, on the assumption you authored them -- which is
389
+ # exactly wrong when an installer or another tool appended to the file.
390
+ # Same content-hash keying as workspace_trust, so an edited command
391
+ # re-asks rather than inheriting an old acknowledgement.
392
+
393
+ def known_servers_path() -> str:
394
+ return os.path.expanduser("~/.config/venastine/known_mcp_servers.json")
395
+
396
+
397
+ def entry_digest(cfg: ServerConfig) -> str:
398
+ """Hash of what the entry will DO. sort_keys so key order in the file
399
+ can't produce a different digest for identical configuration."""
400
+ return hashlib.sha256(
401
+ json.dumps(cfg.raw, sort_keys=True, default=str).encode("utf-8")
402
+ ).hexdigest()
403
+
404
+
405
+ # v2 (#60/F3): the acknowledgement prompt used to omit autoApprove -- the
406
+ # one field that decides whether any future prompt exists -- so consents
407
+ # recorded under v1 were given blind. The store carries a version; a
408
+ # legacy store's entries are treated as UNKNOWN (one re-ask each, under
409
+ # the improved disclosure) and a blind consent never survives the bump.
410
+ KNOWN_STORE_VERSION = 2
411
+
412
+
413
+ def _read_store() -> tuple:
414
+ """(name -> digest mapping, is_legacy). A missing file is empty and
415
+ current. An unreadable one fails CLOSED as empty-and-current: every
416
+ server then looks new and gets re-confirmed, which is the safe
417
+ direction. A readable store in the OLD flat shape is NOT corrupt --
418
+ it is valid v1, and comes back marked legacy."""
419
+ path = known_servers_path()
420
+ if not os.path.exists(path):
421
+ return {}, False
422
+ try:
423
+ with open(path, encoding="utf-8-sig") as f:
424
+ data = json.load(f)
425
+ except (OSError, ValueError):
426
+ logger.warning("Could not read %s; re-confirming every user-level "
427
+ "MCP server.", path)
428
+ return {}, False
429
+ if not isinstance(data, dict):
430
+ logger.warning("%s is not an object; re-confirming every "
431
+ "user-level MCP server.", path)
432
+ return {}, False
433
+ if data.get("version") == KNOWN_STORE_VERSION \
434
+ and isinstance(data.get("servers"), dict):
435
+ return dict(data["servers"]), False
436
+ return {k: v for k, v in data.items()
437
+ if isinstance(k, str)}, True
438
+
439
+
440
+ def is_known(cfg: ServerConfig) -> bool:
441
+ """Whether this exact server entry has already been acknowledged.
442
+
443
+ Reads the store itself rather than taking one. It used to accept a
444
+ pre-read `store=` for callers batching several checks, and no caller
445
+ ever passed one -- so the branch was dead while the parameter went on
446
+ advertising an optimisation nobody could see was unused.
447
+ """
448
+ servers, legacy = _read_store()
449
+ # F3: a v1 consent was given under a prompt that never named the
450
+ # field it most needed to name. It does not count.
451
+ if legacy:
452
+ return False
453
+ return servers.get(cfg.name) == entry_digest(cfg)
454
+
455
+
456
+ def remember_server(cfg: ServerConfig) -> None:
457
+ path = known_servers_path()
458
+ servers, legacy = _read_store()
459
+ if legacy:
460
+ # Carrying the old digests forward would silently convert blind
461
+ # consents into disclosed ones for servers this session never
462
+ # re-asked. They start over; answering y here records THIS server
463
+ # only, and the others keep asking until they are answered.
464
+ servers = {}
465
+ servers[cfg.name] = entry_digest(cfg)
466
+ # ATOMIC since batch 45, and this was the odd one out. Its three
467
+ # sibling stores each wrote through a temp file and os.replace, and
468
+ # each spent four to eight docstring lines on why -- a
469
+ # truncate-then-write leaves the file empty for the length of the
470
+ # write and permanently damaged if the process dies inside it. This
471
+ # one used a bare open(path, "w") and said nothing, for a file that
472
+ # records CONSENT: the same class of data as the trust store, which
473
+ # is the sibling that argues hardest for the guarantee. Sharing the
474
+ # write is what stops a convention four comments long from having a
475
+ # fourth copy that does not follow it.
476
+ #
477
+ # AND ITS MODE, since batch 170: 0o600, as the trust store has. Without
478
+ # one this took open()'s 0o666 less the umask, so under a umask of 002
479
+ # anyone in the user's group could write a consent here and a server
480
+ # would start without asking. Found by triaging CodeQL's alert on
481
+ # json_store's default mode; the default itself is right for the files
482
+ # the agent writes in a workspace.
483
+ write_json_atomic(path, {"version": KNOWN_STORE_VERSION,
484
+ "servers": servers}, mode=0o600)