@yahyahammad/venastine 0.2.0 → 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 (152) hide show
  1. package/.env.example +9 -4
  2. package/CONFIG_ARCHITECTURE.md +2030 -0
  3. package/LICENSE +201 -201
  4. package/NOTICE +1 -1
  5. package/README.md +1034 -895
  6. package/agents/builtin/build.md +73 -0
  7. package/agents/builtin/compactor.md +75 -75
  8. package/agents/builtin/explore.md +77 -77
  9. package/agents/builtin/general.md +80 -0
  10. package/agents/builtin/pipeline-reviewer.md +100 -100
  11. package/agents/builtin/plan.md +106 -88
  12. package/agents/builtin/review.md +74 -74
  13. package/agents/builtin/test.md +65 -0
  14. package/agents/builtin/writer.md +67 -0
  15. package/agents/manager.py +68 -2
  16. package/agents/subagent_tool.py +472 -238
  17. package/agents/tui_commands.py +238 -151
  18. package/bin/venastine.mjs +150 -4
  19. package/bin/venastine.py +71 -0
  20. package/cli_commands.py +838 -0
  21. package/config.py +95 -1198
  22. package/config.yaml +1103 -0
  23. package/config_edit.py +1301 -0
  24. package/config_schema.py +971 -0
  25. package/config_update.py +749 -0
  26. package/core/agent_activity.py +256 -118
  27. package/core/ansi.py +452 -0
  28. package/core/approval.py +137 -115
  29. package/core/call_view.py +364 -0
  30. package/core/cancel.py +121 -0
  31. package/core/chatgpt_auth.py +747 -0
  32. package/core/claims_view.py +107 -0
  33. package/core/client.py +2715 -1559
  34. package/core/commands.py +460 -0
  35. package/core/compaction.py +1735 -971
  36. package/core/config_loader.py +1982 -1563
  37. package/core/connect.py +686 -0
  38. package/core/events.py +123 -61
  39. package/core/file_mentions.py +280 -0
  40. package/core/interaction.py +411 -328
  41. package/core/line_pattern.py +128 -0
  42. package/core/loop.py +2566 -1490
  43. package/core/memory.py +482 -370
  44. package/core/pipeline_models.py +190 -176
  45. package/core/provider_errors.py +234 -0
  46. package/core/reasoning/authorization.py +130 -130
  47. package/core/reasoning/base.py +219 -219
  48. package/core/reasoning/confidence_scoring.py +484 -468
  49. package/core/reasoning/events.py +149 -149
  50. package/core/reasoning/json_retry.py +19 -1
  51. package/core/reasoning/orchestrator.py +1561 -1382
  52. package/core/reasoning/output_writer.py +345 -286
  53. package/core/reasoning/payload_validation.py +524 -398
  54. package/core/reasoning/pipeline_storage.py +88 -22
  55. package/core/reasoning/review.py +659 -615
  56. package/core/reasoning/scholar.py +458 -434
  57. package/core/reasoning/source_corpus.py +433 -327
  58. package/core/reasoning/source_scoring.py +855 -855
  59. package/core/replay.py +315 -183
  60. package/core/session.py +36 -12
  61. package/core/session_view.py +259 -0
  62. package/core/session_wake.py +259 -0
  63. package/core/shell_sessions.py +1553 -0
  64. package/core/workspace_trust.py +1 -1
  65. package/credentials.py +148 -20
  66. package/database.py +333 -252
  67. package/{PRIVACY.md → docs/PRIVACY.md} +78 -66
  68. package/docs/SECURITY.md +108 -0
  69. package/docs/THIRD_PARTY_NOTICES.md +148 -0
  70. package/env_secrets.py +50 -9
  71. package/json_store.py +343 -123
  72. package/logging_setup.py +295 -185
  73. package/main.py +3433 -1961
  74. package/mcp_client/client.py +733 -499
  75. package/mcp_client/config.py +126 -21
  76. package/mcp_client/registration.py +343 -182
  77. package/memories/manager.py +147 -132
  78. package/memories/tui_commands.py +319 -244
  79. package/package.json +303 -73
  80. package/project_init/config_files.py +282 -278
  81. package/project_init/generator.py +728 -564
  82. package/project_init/manifest.py +18 -6
  83. package/project_init/tui_commands.py +147 -144
  84. package/prompts/critic_pass.md +24 -24
  85. package/prompts/revalidate.md +25 -25
  86. package/prompts/revise.md +2 -0
  87. package/prompts/source_grounding.md +66 -66
  88. package/prompts/system_prompts.py +316 -262
  89. package/providers.json.example +4 -2
  90. package/pyproject.toml +149 -100
  91. package/requirements.txt +295 -72
  92. package/safety/policy_enforcement.py +1082 -746
  93. package/security/askpass.py +419 -0
  94. package/security/capability.py +207 -177
  95. package/security/confine.py +484 -0
  96. package/security/hosted.py +182 -0
  97. package/security/permissions.py +237 -190
  98. package/security/posture.py +124 -12
  99. package/security/protected_paths.py +501 -309
  100. package/security/sandbox.py +4108 -985
  101. package/security/secrets.py +855 -0
  102. package/skills/builtin/math/formal-specification.md +67 -67
  103. package/skills/builtin/math/numerical-methods.md +66 -66
  104. package/skills/builtin/research/experiment-design.md +62 -62
  105. package/skills/builtin/research/reproducibility.md +55 -55
  106. package/skills/builtin/research/source-evaluation.md +70 -70
  107. package/skills/builtin/research/statistical-inference.md +63 -63
  108. package/skills/builtin/research/technical-writing.md +59 -59
  109. package/skills/builtin/software/code-review.md +61 -61
  110. package/skills/builtin/software/debugging.md +61 -61
  111. package/skills/builtin/software/test-design.md +67 -67
  112. package/skills/tui_commands.py +110 -111
  113. package/storage.py +1338 -1005
  114. package/tools/base.py +161 -1
  115. package/tools/builtin/_math_common.py +667 -377
  116. package/tools/builtin/_net_common.py +225 -19
  117. package/tools/builtin/arxiv.py +497 -39
  118. package/tools/builtin/ask_user.py +231 -213
  119. package/tools/builtin/discrete_math.py +35 -4
  120. package/tools/builtin/fetch_url.py +371 -153
  121. package/tools/builtin/file_ops.py +720 -412
  122. package/tools/builtin/geometry.py +48 -12
  123. package/tools/builtin/get_time.py +2 -2
  124. package/tools/builtin/linear_algebra.py +66 -23
  125. package/tools/builtin/load_skill.py +76 -66
  126. package/tools/builtin/logic.py +171 -12
  127. package/tools/builtin/pin.py +227 -193
  128. package/tools/builtin/probability_stats.py +12 -16
  129. package/tools/builtin/project_docs.py +464 -405
  130. package/tools/builtin/remember.py +175 -158
  131. package/tools/builtin/shell.py +980 -461
  132. package/tools/builtin/shell_sessions.py +613 -0
  133. package/tools/builtin/symbolic_math.py +80 -10
  134. package/tools/builtin/todo.py +149 -147
  135. package/tools/builtin/web_search.py +191 -13
  136. package/tools/context.py +159 -136
  137. package/tools/isolation.py +107 -34
  138. package/tools/registry.py +1242 -925
  139. package/tui/app.py +8318 -4100
  140. package/tui/app.tcss +747 -556
  141. package/tui/commands.py +26 -170
  142. package/tui/diffs.py +34 -13
  143. package/tui/history.py +128 -128
  144. package/tui/hosted_driver.py +102 -0
  145. package/tui/markdown.py +1329 -1279
  146. package/tui/meters.py +364 -268
  147. package/tui/ravens.py +146 -146
  148. package/tui/screens.py +1255 -943
  149. package/tui/themes.py +599 -559
  150. package/tui/widgets.py +3813 -2332
  151. package/SECURITY.md +0 -88
  152. package/THIRD_PARTY_NOTICES.md +0 -100
@@ -1,309 +1,501 @@
1
- """
2
- security/protected_paths.py
3
-
4
- The trees a sandboxed command must never be able to WRITE: the harness's
5
- own source, and the user-tier state that decides what the harness trusts
6
- and what it spawns.
7
-
8
- WHY THIS EXISTS. security/sandbox.py's Docker backend binds
9
- config.WORKSPACE_DIR read-write at /workspace and never asked what was on
10
- the other side of that mount. A SANDBOXED command under CONTAINED with no
11
- network is AUTO-APPROVED (security/capability.auto_approved), so nothing
12
- prompts -- which meant a workspace pointed at the harness's own directory
13
- handed every run unattended read-write access to the code it was about to
14
- execute next, and a workspace pointed at a home directory did the same to
15
- `~/.config/venastine/`. That directory holds `trusted_projects.json` (the
16
- D17 gate: a write there marks arbitrary projects trusted) and `mcp.json`
17
- (which NAMES COMMANDS the harness spawns at startup). Neither is a file
18
- the model should be able to edit through a tool it did not have to ask
19
- about.
20
-
21
- The agent cannot cause this by itself -- WORKSPACE_DIR is bound once at
22
- import (config.py) and a child shell's `export` dies with the subprocess,
23
- which was measured rather than assumed. This is a MISCONFIGURATION
24
- boundary: it makes a workspace that overlaps the harness a startup error
25
- instead of a silent grant.
26
-
27
- TWO CLASSES, and the split is the design rather than tidiness:
28
-
29
- CRITICAL the harness install tree and the user config dir. A
30
- workspace that IS one of these, or sits inside one, is
31
- refused outright -- there is no mount arrangement that
32
- rescues it, and nobody has such a layout on purpose.
33
-
34
- SENSITIVE providers.json, the database, the log directory. These are
35
- ro-mounted when they fall inside the workspace but NEVER
36
- refuse it, because each is user-configurable to anywhere:
37
- AGENT_LOG_FILE can legitimately point at a file in $HOME,
38
- and letting that refuse a $HOME workspace would turn a
39
- logging preference into a launch failure.
40
-
41
- This generalises security/sandbox._venastine_readonly_mount, which does
42
- the same job for a `.venastine/` nested in the workspace and whose
43
- docstring already named this gap: "does not cover ... the harness's own
44
- `agents/builtin` when the workspace is the harness repo."
45
-
46
- THE PROTECTED SEGMENT at the bottom of this file is the module's second
47
- job, added because everything above answered only half the question. The
48
- trees and ro-mounts stop a SANDBOXED command from writing what it should
49
- not; nothing stopped the agent's own read/write/edit tools from reaching
50
- the same material once AGENT_WORKSPACE was pointed at the project --
51
- workspace membership is the only rule those tools apply, so `.venastine/`
52
- was an auto-approved path the moment it sat inside one. PROTECTED_SEGMENTS
53
- is the one list every consumer reads (the file tools' refusal_check and
54
- handlers, and shell.py's approval check), so the segment cannot drift
55
- between them.
56
-
57
- KNOWN LIMITS, stated because a half-understood control is worse than
58
- none. This covers WRITES on the DOCKER path. It does not cover reads, and
59
- it does not cover the subprocess fallback, which mounts nothing and has
60
- no filesystem isolation at all -- see SECURITY.md, where that combination
61
- is recorded as a documented risk rather than a defect.
62
- """
63
-
64
- from __future__ import annotations
65
-
66
- import logging
67
- import os
68
- import re
69
-
70
- import config
71
- import credentials
72
-
73
- logger = logging.getLogger(__name__)
74
-
75
- # The directories inside the install tree that the harness itself writes
76
- # artifacts to, and which therefore stay usable as a workspace. This is
77
- # an ALLOWLIST and that direction is load-bearing: a module added in a
78
- # later batch is protected the day it lands, whereas a denylist of source
79
- # directories would leave it mounted read-write until somebody remembered
80
- # to extend the list. `./workspace` is also the shipped default, so this
81
- # is the branch that keeps the ordinary layout working.
82
- WRITABLE_INSIDE_HARNESS = ("workspace", "output")
83
-
84
-
85
- def harness_root() -> str:
86
- """The install tree of the harness that is RUNNING, not any copy.
87
-
88
- Derived from this file rather than from the cwd, so it is correct
89
- under every launch route: `python main.py` from a clone, and
90
- bin/venastine.mjs spawning `python <ROOT>/main.py` from a global npm
91
- install while the user's cwd is somewhere else entirely.
92
-
93
- That it names the running install is deliberate and is what keeps
94
- development workable. An npm-installed harness aimed at a separate
95
- clone of its own source sees two disjoint trees and allows it: writing
96
- to a clone you are not executing cannot escalate the privileges of the
97
- process doing the writing. Only pointing the workspace at the tree
98
- currently being executed is refused.
99
- """
100
- return os.path.realpath(os.path.dirname(os.path.dirname(
101
- os.path.abspath(__file__))))
102
-
103
-
104
- def user_config_dir() -> str:
105
- """`~/.config/venastine`, resolved at CALL time.
106
-
107
- Mirrors core.config_loader._user_config_dir rather than importing it.
108
- security/ imports config, credentials and the stdlib; reaching into
109
- core/ from here would invert the layering the sandbox module's
110
- `_within` docstring sets out. The duplication is pinned by
111
- `TestTheMirroredResolversMatchTheirOwners` in tests/test_shell.py, so
112
- drift is a test failure rather than a silent hole.
113
-
114
- That sentence named a private function without its underscore and a
115
- test that has never existed under the name given, for two batches --
116
- the guard was real and the citation was not, which is the worse half
117
- of the pair to get wrong: a reader who greps the name finds nothing
118
- and concludes the duplication is unguarded.
119
- """
120
- return os.path.realpath(os.path.expanduser("~/.config/venastine"))
121
-
122
-
123
- def _log_dir() -> str:
124
- """The directory the rotating log handler writes into.
125
-
126
- Mirrors logging_setup's resolution -- AGENT_LOG_FILE, else
127
- `logs/app.log` -- rather than importing it, and here the reason is
128
- concrete rather than stylistic: logging_setup imports
129
- safety.policy_enforcement, so an import would be the first
130
- `security/ -> safety/` edge in the project.
131
-
132
- The DIRECTORY, not the file: RotatingFileHandler writes `app.log.1`,
133
- `.2` and so on beside it, and protecting only the live file would
134
- leave every rotated copy writable -- which is the half that matters
135
- if what you are worried about is a run editing its own trail.
136
- """
137
- log_file = os.environ.get("AGENT_LOG_FILE", "logs/app.log")
138
- return os.path.realpath(os.path.dirname(log_file) or ".")
139
-
140
-
141
- def critical_paths() -> list[tuple[str, str]]:
142
- """(realpath, label) for the trees that also REFUSE a workspace."""
143
- return [
144
- (harness_root(), "the harness's own install tree"),
145
- (user_config_dir(), "the user config directory"),
146
- ]
147
-
148
-
149
- def sensitive_paths() -> list[tuple[str, str]]:
150
- """(realpath, label) for the paths that are only ever ro-mounted."""
151
- return [
152
- (os.path.realpath(credentials.LLM_PROVIDERS_FILE),
153
- "the provider credentials file"),
154
- (os.path.realpath(config.DB_PATH), "the conversation database"),
155
- (_log_dir(), "the log directory"),
156
- ]
157
-
158
-
159
- def _relation(candidate: str, other: str) -> str:
160
- """How *candidate* sits against *other*: equal, inside, contains, or
161
- disjoint. Both are realpath'd by the callers already.
162
-
163
- `+ os.sep` on both comparisons for the reason test_file_ops.py:314
164
- records about `_is_within_workspace`: a bare startswith makes
165
- `/workspace-evil` look like it is inside `/workspace`.
166
- """
167
- if candidate == other:
168
- return "equal"
169
- if candidate.startswith(other + os.sep):
170
- return "inside"
171
- if other.startswith(candidate + os.sep):
172
- return "contains"
173
- return "disjoint"
174
-
175
-
176
- def _exempt_from_harness_refusal(workspace_real: str) -> bool:
177
- """Whether a workspace inside the install tree is one of the harness's
178
- own artifact directories (or something under one)."""
179
- root = harness_root()
180
- for name in WRITABLE_INSIDE_HARNESS:
181
- allowed = os.path.realpath(os.path.join(root, name))
182
- if _relation(workspace_real, allowed) in ("equal", "inside"):
183
- return True
184
- return False
185
-
186
-
187
- def check_workspace(workspace_dir: str) -> str | None:
188
- """Why *workspace_dir* may not be used as a sandbox workspace, or None.
189
-
190
- Returns a REASON STRING rather than raising, so the two callers can
191
- fail in the shape each needs -- main.py prints it and exits, and
192
- security/sandbox.py raises SandboxUnavailable. Raising a sandbox
193
- exception from here would mean importing sandbox.py, which imports
194
- this module.
195
-
196
- The message is written for the person who has to fix it, and names
197
- both what overlaps and what to do instead: a workspace refusal is
198
- always a configuration error, never something the model did.
199
- """
200
- workspace_real = os.path.realpath(workspace_dir)
201
-
202
- for protected, label in critical_paths():
203
- relation = _relation(workspace_real, protected)
204
- if relation not in ("equal", "inside"):
205
- continue
206
- if protected == harness_root() and _exempt_from_harness_refusal(
207
- workspace_real):
208
- continue
209
- where = (label if relation == "equal"
210
- else f"inside {label} ({protected})")
211
- return (
212
- f"AGENT_WORKSPACE ({workspace_real}) is {where}. The sandbox "
213
- f"mounts the workspace read-write and non-inert commands are "
214
- f"auto-approved inside it, so this would let a shell command "
215
- f"rewrite the harness's own code, or the state that decides "
216
- f"what it trusts and what it spawns. Point AGENT_WORKSPACE at "
217
- f"a directory outside it -- the default is ./workspace."
218
- )
219
- return None
220
-
221
-
222
- def readonly_mounts(workspace_real: str) -> list[str]:
223
- """Docker `-v` arguments binding every protected path NESTED inside
224
- *workspace_real* read-only, in the order docker expects them.
225
-
226
- Only what already EXISTS is mounted, copying the `isdir` half of
227
- _venastine_readonly_mount and for the same reason: docker CREATES a
228
- missing bind source as a root-owned directory on the host, so an
229
- unconditional mount would conjure a `.config/venastine/` into the
230
- user's tree, or an `app.db` that is a DIRECTORY where the harness
231
- expects a SQLite file.
232
-
233
- Nested protected paths are collapsed to the outermost -- the database
234
- and the log directory both live under `~/.config/venastine` on a
235
- global install -- because a second bind underneath a read-only one
236
- buys nothing and only makes the command line harder to read.
237
- """
238
- candidates = []
239
- for protected, _label in critical_paths() + sensitive_paths():
240
- if _relation(protected, workspace_real) != "inside":
241
- continue
242
- if not os.path.exists(protected):
243
- continue
244
- candidates.append(protected)
245
-
246
- # Shortest first, so an ancestor is always decided before anything
247
- # underneath it and the collapse below only ever has to look backwards.
248
- kept: list[str] = []
249
- for protected in sorted(set(candidates), key=len):
250
- if any(_relation(protected, outer) == "inside" for outer in kept):
251
- continue
252
- kept.append(protected)
253
-
254
- args: list[str] = []
255
- for protected in kept:
256
- rel = os.path.relpath(protected, workspace_real).replace(os.sep, "/")
257
- args.extend(["-v", f"{protected}:/workspace/{rel}:ro"])
258
- return args
259
-
260
-
261
- # ===========================================================================
262
- # ---- Protected path segments ----------------------------------------------
263
- # ===========================================================================
264
-
265
- # Path components the agent's own tools may never touch, wherever they sit
266
- # in a RESOLVED path. `.venastine/` holds settings.json and mcp.json --
267
- # which names commands the harness spawns at startup -- and, under skills/
268
- # and agents/, content that is injected into system prompts. The trees
269
- # above answer what a SANDBOXED command may write; this answers what the
270
- # agent's read/write/edit tools may reach at all, which workspace
271
- # membership alone does not: with AGENT_WORKSPACE pointed at the project,
272
- # `.venastine/` is inside the workspace and file_ops'
273
- # `_file_approval_check` auto-approves it.
274
- #
275
- # ONE LIST, THREE CONSUMERS -- the same discipline as the local-secret
276
- # exclusion lists, which exist because three copies of one fact drift.
277
- # tools/builtin/file_ops.py refuses any path carrying one of these
278
- # segments (refusal_check AND the three handlers); tools/builtin/shell.py
279
- # forces a prompt for any command whose tokens name one; and
280
- # project_docs.py unions this set into its own wider denylist rather than
281
- # re-spelling the segment.
282
- #
283
- # KNOWN LIMITS, in the house style. The file tools DENY; the shell only
284
- # ever ASKS, because a token check that learned shell syntax would be a
285
- # parser whose bugs auto-approve (G2), and an approval prompt leaves the
286
- # whole command text in front of a human instead. The auto-approved INERT
287
- # tier executes argv with no shell (§46 EP5), so its tokens ARE its
288
- # arguments and a token check is sound there; every tier that could
289
- # expand a glob or a variable into the segment is asked about anyway.
290
- # MCP servers exposing their own file tools are a separate surface this
291
- # list does not reach, and the user-tier `~/.config/venastine` stays
292
- # where it always was -- outside a default workspace it is prompt-gated
293
- # by the workspace boundary, not denied.
294
- PROTECTED_SEGMENTS = frozenset({".venastine"})
295
-
296
-
297
- def protected_segment(resolved_path: str) -> str | None:
298
- """The protected segment *resolved_path* carries, or None.
299
-
300
- Callers pass a realpath'd path (file_ops' ``resolve_path``, the
301
- sandbox's ``_within``), so `..` travel and symlinks are already
302
- collapsed: a link pointing at `.venastine/settings.json` arrives
303
- here as the real thing, and on Windows so does a case-spelled one.
304
- Split on both separators because a raw shell token may carry either.
305
- """
306
- for part in re.split(r"[\\/]", resolved_path):
307
- if part in PROTECTED_SEGMENTS:
308
- return part
309
- return None
1
+ """
2
+ security/protected_paths.py
3
+
4
+ The trees a sandboxed command must never be able to WRITE: the harness's
5
+ own source, and the user-tier state that decides what the harness trusts
6
+ and what it spawns.
7
+
8
+ WHY THIS EXISTS. security/sandbox.py's Docker backend binds
9
+ config.WORKSPACE_DIR read-write at /workspace and never asked what was on
10
+ the other side of that mount. A SANDBOXED command under CONTAINED with no
11
+ network is AUTO-APPROVED (security/capability.auto_approved), so nothing
12
+ prompts -- which meant a workspace pointed at the harness's own directory
13
+ handed every run unattended read-write access to the code it was about to
14
+ execute next, and a workspace pointed at a home directory did the same to
15
+ `~/.config/venastine/`. That directory holds `trusted_projects.json` (the
16
+ D17 gate: a write there marks arbitrary projects trusted) and `mcp.json`
17
+ (which NAMES COMMANDS the harness spawns at startup). Neither is a file
18
+ the model should be able to edit through a tool it did not have to ask
19
+ about.
20
+
21
+ The agent cannot cause this by itself -- WORKSPACE_DIR is bound once at
22
+ import (config.py) and a child shell's `export` dies with the subprocess,
23
+ which was measured rather than assumed. This is a MISCONFIGURATION
24
+ boundary: it makes a workspace that overlaps the harness a startup error
25
+ instead of a silent grant.
26
+
27
+ TWO CLASSES, and the split is the design rather than tidiness:
28
+
29
+ CRITICAL the harness install tree and the user config dir. A
30
+ workspace that IS one of these, or sits inside one, is
31
+ refused outright -- there is no mount arrangement that
32
+ rescues it, and nobody has such a layout on purpose.
33
+
34
+ SENSITIVE providers.json, the database, the log directory. These are
35
+ ro-mounted when they fall inside the workspace but NEVER
36
+ refuse it, because each is user-configurable to anywhere:
37
+ AGENT_LOG_FILE can legitimately point at a file in $HOME,
38
+ and letting that refuse a $HOME workspace would turn a
39
+ logging preference into a launch failure.
40
+
41
+ This generalises security/sandbox._venastine_readonly_mount, which does
42
+ the same job for a `.venastine/` nested in the workspace and whose
43
+ docstring already named this gap: "does not cover ... the harness's own
44
+ `agents/builtin` when the workspace is the harness repo."
45
+
46
+ THE PROTECTED SEGMENT at the bottom of this file is the module's second
47
+ job, added because everything above answered only half the question. The
48
+ trees and ro-mounts stop a SANDBOXED command from writing what it should
49
+ not; nothing stopped the agent's own read/write/edit tools from reaching
50
+ the same material once AGENT_WORKSPACE was pointed at the project --
51
+ workspace membership is the only rule those tools apply, so `.venastine/`
52
+ was an auto-approved path the moment it sat inside one. PROTECTED_SEGMENTS
53
+ is the one list every consumer reads (the file tools' refusal_check and
54
+ handlers, and shell.py's approval check), so the segment cannot drift
55
+ between them.
56
+
57
+ KNOWN LIMITS, stated because a half-understood control is worse than
58
+ none. This covers WRITES on the DOCKER path. It does not cover reads, and
59
+ it does not cover the subprocess fallback, which mounts nothing and has
60
+ no filesystem isolation at all -- see SECURITY.md, where that combination
61
+ is recorded as a documented risk rather than a defect.
62
+ """
63
+
64
+ from __future__ import annotations
65
+
66
+ import logging
67
+ import os
68
+ import re
69
+ import stat
70
+
71
+ import config
72
+ import credentials
73
+
74
+ logger = logging.getLogger(__name__)
75
+
76
+ # The directories inside the install tree that the harness itself writes
77
+ # artifacts to, and which therefore stay usable as a workspace. This is
78
+ # an ALLOWLIST and that direction is load-bearing: a module added in a
79
+ # later batch is protected the day it lands, whereas a denylist of source
80
+ # directories would leave it mounted read-write until somebody remembered
81
+ # to extend the list. `./workspace` is also the shipped default, so this
82
+ # is the branch that keeps the ordinary layout working.
83
+ WRITABLE_INSIDE_HARNESS = ("workspace", "output")
84
+
85
+
86
+ def harness_root() -> str:
87
+ """The install tree of the harness that is RUNNING, not any copy.
88
+
89
+ Derived from this file rather than from the cwd, so it is correct
90
+ under every launch route: `python main.py` from a clone, and
91
+ bin/venastine.mjs spawning `python <ROOT>/main.py` from a global npm
92
+ install while the user's cwd is somewhere else entirely.
93
+
94
+ That it names the running install is deliberate and is what keeps
95
+ development workable. An npm-installed harness aimed at a separate
96
+ clone of its own source sees two disjoint trees and allows it: writing
97
+ to a clone you are not executing cannot escalate the privileges of the
98
+ process doing the writing. Only pointing the workspace at the tree
99
+ currently being executed is refused.
100
+ """
101
+ return os.path.realpath(os.path.dirname(os.path.dirname(
102
+ os.path.abspath(__file__))))
103
+
104
+
105
+ def user_config_dir() -> str:
106
+ """`~/.config/venastine`, resolved at CALL time.
107
+
108
+ Mirrors core.config_loader._user_config_dir rather than importing it.
109
+ security/ imports config, credentials and the stdlib; reaching into
110
+ core/ from here would invert the layering the sandbox module's
111
+ `_within` docstring sets out. The duplication is pinned by
112
+ `TestTheMirroredResolversMatchTheirOwners` in tests/test_shell.py, so
113
+ drift is a test failure rather than a silent hole.
114
+
115
+ That sentence named a private function without its underscore and a
116
+ test that has never existed under the name given, for two batches --
117
+ the guard was real and the citation was not, which is the worse half
118
+ of the pair to get wrong: a reader who greps the name finds nothing
119
+ and concludes the duplication is unguarded.
120
+ """
121
+ return os.path.realpath(os.path.expanduser("~/.config/venastine"))
122
+
123
+
124
+ def _log_dir() -> str:
125
+ """The directory the rotating log handler writes into.
126
+
127
+ Mirrors logging_setup's resolution -- AGENT_LOG_FILE, else
128
+ `logs/app.log` -- rather than importing it, and here the reason is
129
+ concrete rather than stylistic: logging_setup imports
130
+ safety.policy_enforcement, so an import would be the first
131
+ `security/ -> safety/` edge in the project.
132
+
133
+ The DIRECTORY, not the file: RotatingFileHandler writes `app.log.1`,
134
+ `.2` and so on beside it, and protecting only the live file would
135
+ leave every rotated copy writable -- which is the half that matters
136
+ if what you are worried about is a run editing its own trail.
137
+ """
138
+ log_file = os.environ.get("AGENT_LOG_FILE", "logs/app.log")
139
+ return os.path.realpath(os.path.dirname(log_file) or ".")
140
+
141
+
142
+ def critical_paths() -> list[tuple[str, str]]:
143
+ """(realpath, label) for the trees that also REFUSE a workspace."""
144
+ return [
145
+ (harness_root(), "the harness's own install tree"),
146
+ (user_config_dir(), "the user config directory"),
147
+ ]
148
+
149
+
150
+ def sensitive_paths() -> list[tuple[str, str]]:
151
+ """(realpath, label) for the paths that are only ever ro-mounted."""
152
+ return [
153
+ (os.path.realpath(credentials.LLM_PROVIDERS_FILE),
154
+ "the provider credentials file"),
155
+ (os.path.realpath(config.DB_PATH), "the conversation database"),
156
+ (_log_dir(), "the log directory"),
157
+ ]
158
+
159
+
160
+ def _relation(candidate: str, other: str) -> str:
161
+ """How *candidate* sits against *other*: equal, inside, contains, or
162
+ disjoint. Both are realpath'd by the callers already.
163
+
164
+ `+ os.sep` on both comparisons for the reason test_file_ops.py:314
165
+ records about `_is_within_workspace`: a bare startswith makes
166
+ `/workspace-evil` look like it is inside `/workspace`.
167
+ """
168
+ if candidate == other:
169
+ return "equal"
170
+ if candidate.startswith(other + os.sep):
171
+ return "inside"
172
+ if other.startswith(candidate + os.sep):
173
+ return "contains"
174
+ return "disjoint"
175
+
176
+
177
+ def _exempt_from_harness_refusal(workspace_real: str) -> bool:
178
+ """Whether a workspace inside the install tree is one of the harness's
179
+ own artifact directories (or something under one)."""
180
+ root = harness_root()
181
+ for name in WRITABLE_INSIDE_HARNESS:
182
+ allowed = os.path.realpath(os.path.join(root, name))
183
+ if _relation(workspace_real, allowed) in ("equal", "inside"):
184
+ return True
185
+ return False
186
+
187
+
188
+ def check_workspace(workspace_dir: str) -> str | None:
189
+ """Why *workspace_dir* may not be used as a sandbox workspace, or None.
190
+
191
+ Returns a REASON STRING rather than raising, so the two callers can
192
+ fail in the shape each needs -- main.py prints it and exits, and
193
+ security/sandbox.py raises SandboxUnavailable. Raising a sandbox
194
+ exception from here would mean importing sandbox.py, which imports
195
+ this module.
196
+
197
+ The message is written for the person who has to fix it, and names
198
+ both what overlaps and what to do instead: a workspace refusal is
199
+ always a configuration error, never something the model did.
200
+
201
+ AN EMPTY NAME IS REFUSED BEFORE IT IS RESOLVED (TECHNICAL_DEBT 93,
202
+ batch 160), because `os.path.realpath("")` is the LAUNCH DIRECTORY.
203
+ `config_schema._overlay_env` honours a variable that is set at all --
204
+ `if override is not None`, deliberately generic over its table -- so
205
+ `AGENT_WORKSPACE=` made the launch directory the auto-approved
206
+ workspace, and `WORKSPACE_DIR_EXPLICIT`, which reads the variable's
207
+ presence and never its value, made it the project as well. Measured
208
+ in batch 146: `_file_approval_check('write', ...)` answered no-prompt
209
+ for a file in the launch directory. Here rather than in the merge, so
210
+ the merge stays generic and the one place a bad workspace is refused
211
+ stays the one place; `main()` calls this before anything reads the
212
+ workspace or the project.
213
+ """
214
+ if not workspace_dir or not workspace_dir.strip():
215
+ return (
216
+ "The workspace is an empty path -- AGENT_WORKSPACE is set to an "
217
+ "empty value, or workspace_dir in config.yaml is blank -- and an "
218
+ "empty path resolves to the directory this was launched from, "
219
+ "which would make it the auto-approved workspace and the "
220
+ "project. Unset AGENT_WORKSPACE, or name a directory; the "
221
+ "default is ./workspace."
222
+ )
223
+ workspace_real = os.path.realpath(workspace_dir)
224
+
225
+ for protected, label in critical_paths():
226
+ relation = _relation(workspace_real, protected)
227
+ if relation not in ("equal", "inside"):
228
+ continue
229
+ if protected == harness_root() and _exempt_from_harness_refusal(
230
+ workspace_real):
231
+ continue
232
+ where = (label if relation == "equal"
233
+ else f"inside {label} ({protected})")
234
+ return (
235
+ f"AGENT_WORKSPACE ({workspace_real}) is {where}. The sandbox "
236
+ f"mounts the workspace read-write and non-inert commands are "
237
+ f"auto-approved inside it, so this would let a shell command "
238
+ f"rewrite the harness's own code, or the state that decides "
239
+ f"what it trusts and what it spawns. Point AGENT_WORKSPACE at "
240
+ f"a directory outside it -- the default is ./workspace."
241
+ )
242
+ return None
243
+
244
+
245
+ def check_project(project_path: str) -> str | None:
246
+ """Why *project_path* may not be the PROJECT, or None (debt item 26).
247
+
248
+ The project is what `config_loader.get_project_path()` resolves, and
249
+ everything downstream follows it: D17 workspace trust, the
250
+ `.venastine/` config tier, `/init`'s destination and `UserMemory`'s
251
+ project scope (WS7, D25/M12). It may not be the harness's own install
252
+ tree.
253
+
254
+ THE DISPARITY THIS CLOSES, and it is why this is a guard rather than a
255
+ convention. Naming that directory was ALREADY refused --
256
+ `check_workspace` above returns a reason for `AGENT_WORKSPACE=<harness
257
+ root>`, and for every non-exempt subfolder of it. But `main()` falls
258
+ back to `os.getcwd()` when nothing names a workspace as the project, so
259
+ launching from a clone reached the same state by saying nothing:
260
+ measured in batch 96 from an owner report and reproduced in batch 107,
261
+ the trust prompt listed the harness's own `AGENTS.md`. The rule was
262
+ enforced when you said it and skipped when you did not.
263
+
264
+ THE MESSAGE NAMES THREE WAYS OUT since batch 111, and used to name two
265
+ and a trap. It said `workspace_dir` in `config.yaml` "is not enough on
266
+ its own" -- true, and no help at all to the person reading it, who had
267
+ just written the directory they meant into the file this refusal is
268
+ about. `workspace_is_project` is the third way (TECHNICAL_DEBT 28), and
269
+ the sentence is now about what to do rather than about what does not
270
+ work.
271
+
272
+ EQUALITY, NOT CONTAINMENT, which is the one place this deliberately
273
+ differs from `check_workspace`. That one is a WRITE boundary and
274
+ refuses anything inside the tree except `workspace/` and `output/`;
275
+ this one answers "is the project the harness", and `<harness>/workspace`
276
+ is a perfectly good project. It is the shipped layout, and it is the
277
+ answer for someone working on the harness itself.
278
+
279
+ A REASON STRING rather than an exception, for `check_workspace`'s
280
+ reason: the caller prints it and exits. REALPATH'D HERE rather than at
281
+ the caller, because `main()`'s implicit branch hands over a bare
282
+ `os.getcwd()` -- and because the realpath is what makes this a question
283
+ about a directory instead of about a spelling. `_relation` compares
284
+ strings, so `<root>/workspace/..` and a Windows path differing only in
285
+ case both read as `disjoint` to it; `os.path.realpath` resolves the
286
+ first on every platform and canonicalises the second on Windows
287
+ (measured, batch 107).
288
+ """
289
+ root = harness_root()
290
+ if _relation(os.path.realpath(project_path), root) != "equal":
291
+ return None
292
+ return (
293
+ f"The project would be the harness's own install tree ({root}), "
294
+ f"because nothing named a workspace as the project and this was "
295
+ f"launched from there. Workspace trust, .venastine/, /init's "
296
+ f"destination and project-scoped memories all follow the project, "
297
+ f"so the harness would be asked to trust its own AGENTS.md and "
298
+ f"/init would scaffold documentation into its own source. Three "
299
+ f"ways out, any one of them enough: launch from the directory you "
300
+ f"want to work in; set AGENT_WORKSPACE to it; or, if you have "
301
+ f"already written it as workspace_dir in config.yaml, set "
302
+ f"workspace_is_project: true beside it. To work on the harness "
303
+ f"itself, AGENT_WORKSPACE=./workspace."
304
+ )
305
+
306
+
307
+ def readonly_mounts(workspace_real: str) -> list[str]:
308
+ """Docker `-v` arguments binding every protected path NESTED inside
309
+ *workspace_real* read-only, in the order docker expects them.
310
+
311
+ Only what already EXISTS is mounted, copying the `isdir` half of
312
+ _venastine_readonly_mount and for the same reason: docker CREATES a
313
+ missing bind source as a root-owned directory on the host, so an
314
+ unconditional mount would conjure a `.config/venastine/` into the
315
+ user's tree, or an `app.db` that is a DIRECTORY where the harness
316
+ expects a SQLite file.
317
+
318
+ Nested protected paths are collapsed to the outermost -- the database
319
+ and the log directory both live under `~/.config/venastine` on a
320
+ global install -- because a second bind underneath a read-only one
321
+ buys nothing and only makes the command line harder to read.
322
+ """
323
+ candidates = []
324
+ for protected, _label in critical_paths() + sensitive_paths():
325
+ if _relation(protected, workspace_real) != "inside":
326
+ continue
327
+ if not os.path.exists(protected):
328
+ continue
329
+ candidates.append(protected)
330
+
331
+ # Shortest first, so an ancestor is always decided before anything
332
+ # underneath it and the collapse below only ever has to look backwards.
333
+ kept: list[str] = []
334
+ for protected in sorted(set(candidates), key=len):
335
+ if any(_relation(protected, outer) == "inside" for outer in kept):
336
+ continue
337
+ kept.append(protected)
338
+
339
+ args: list[str] = []
340
+ for protected in kept:
341
+ rel = os.path.relpath(protected, workspace_real).replace(os.sep, "/")
342
+ args.extend(["-v", f"{protected}:/workspace/{rel}:ro"])
343
+ return args
344
+
345
+
346
+ # ===========================================================================
347
+ # ---- Protected path segments ----------------------------------------------
348
+ # ===========================================================================
349
+
350
+ # Path components the agent's own tools may never touch, wherever they sit
351
+ # in a RESOLVED path. `.venastine/` holds settings.json and mcp.json --
352
+ # which names commands the harness spawns at startup -- and, under skills/
353
+ # and agents/, content that is injected into system prompts. The trees
354
+ # above answer what a SANDBOXED command may write; this answers what the
355
+ # agent's read/write/edit tools may reach at all, which workspace
356
+ # membership alone does not: with AGENT_WORKSPACE pointed at the project,
357
+ # `.venastine/` is inside the workspace and file_ops'
358
+ # `_file_approval_check` auto-approves it.
359
+ #
360
+ # ONE LIST, THREE CONSUMERS -- the same discipline as the local-secret
361
+ # exclusion lists, which exist because three copies of one fact drift.
362
+ # tools/builtin/file_ops.py refuses any path carrying one of these
363
+ # segments (refusal_check AND the three handlers); tools/builtin/shell.py
364
+ # forces a prompt for any command whose tokens name one; and
365
+ # project_docs.py unions this set into its own wider denylist rather than
366
+ # re-spelling the segment.
367
+ #
368
+ # AND ONE COMPARISON, since batch 146 -- `folded_segments` below, because
369
+ # the fact is not the spelling on its own but the spelling together with
370
+ # how a filesystem will match it. Two of the three consumers had their own
371
+ # comparison and only one of them folded case, which is not a thing either
372
+ # file said; it was a thing you could only find by writing `.VENASTINE/`.
373
+ #
374
+ # KNOWN LIMITS, in the house style. The file tools DENY; the shell only
375
+ # ever ASKS, because a token check that learned shell syntax would be a
376
+ # parser whose bugs auto-approve (G2), and an approval prompt leaves the
377
+ # whole command text in front of a human instead. The auto-approved INERT
378
+ # tier executes argv with no shell (§46 EP5), so its tokens ARE its
379
+ # arguments and a token check is sound there; under `tiered` every tier
380
+ # that could expand a glob or a variable into the segment is asked about
381
+ # anyway. That sentence was FALSE until §48 (CE1): a contained command was
382
+ # auto-approved, and `cat .ven*/settings.json; true` read the file unasked.
383
+ # Under `contained` and `never` it is still not true -- such a spelling,
384
+ # or code that opens the file, runs unasked; the ro mount stops a write,
385
+ # and the read is recorded in SECURITY.md as documented risk.
386
+ # MCP servers exposing their own file tools are a separate surface this
387
+ # list does not reach, and the user-tier `~/.config/venastine` stays
388
+ # where it always was -- outside a default workspace it is prompt-gated
389
+ # by the workspace boundary, not denied.
390
+ PROTECTED_SEGMENTS = frozenset({".venastine"})
391
+
392
+
393
+ def hardlinked_segment(resolved_path: str, roots) -> str | None:
394
+ """The protected segment a HARD LINK at *resolved_path* is a second
395
+ name for, or None (TECHNICAL_DEBT 90, batch 160).
396
+
397
+ `protected_segment` reads a resolved PATH, and a hardlink is not a link
398
+ to anything: `realpath` returns the name it was given and `islink` is
399
+ False, so a second name for `.venastine/settings.json` sitting in the
400
+ workspace read as an ordinary file. A `read` through one served the
401
+ protected file -- measured in batch 146, along with the write half that
402
+ `write_text_atomic` already closed by replacing the name.
403
+
404
+ SAME FILE, NOT SAME NAME: a file with more than one name is compared by
405
+ `(st_dev, st_ino)` against every file under the `.venastine/` of each of
406
+ *roots* -- the project and the workspace, which are the directories that
407
+ tree is read from. Both fields are real on Windows as well as POSIX
408
+ (measured: an NTFS hardlink pair reports `st_nlink == 2` and one file
409
+ id). The two rejected alternatives are why it is this one: enumerating a
410
+ file's other names is per-platform, and refusing every `st_nlink > 1`
411
+ denies ordinary files to protect against a layout the write path
412
+ already handled. A file with one name pays one `stat`.
413
+
414
+ A `.venastine/` elsewhere on the disk is not looked for, so a hardlink
415
+ into some other project's tree is not caught; the deny exists for the
416
+ one this harness reads.
417
+ """
418
+ try:
419
+ target = os.stat(resolved_path)
420
+ except OSError:
421
+ return None
422
+ if not stat.S_ISREG(target.st_mode) or target.st_nlink < 2:
423
+ return None
424
+ identity = (target.st_dev, target.st_ino)
425
+ for root in roots:
426
+ try:
427
+ entries = os.listdir(root)
428
+ except OSError:
429
+ continue
430
+ for entry in entries:
431
+ if entry.casefold() not in PROTECTED_SEGMENTS_FOLDED:
432
+ continue
433
+ for dirpath, _dirnames, filenames in os.walk(
434
+ os.path.join(root, entry)):
435
+ for name in filenames:
436
+ try:
437
+ other = os.stat(os.path.join(dirpath, name))
438
+ except OSError:
439
+ continue
440
+ if (other.st_dev, other.st_ino) == identity:
441
+ return entry
442
+ return None
443
+
444
+
445
+ def folded_segments(names) -> frozenset:
446
+ """`names` prepared for the comparison a filesystem will actually make.
447
+
448
+ THE ONE SPELLING OF THE FOLD (batch 146). Both consumers that compare a
449
+ path's segments against a denylist call this -- `protected_segment`
450
+ below and `project_docs._is_readable_doc` -- because the alternative is
451
+ what was there before: two comparisons, one of them case-sensitive, and
452
+ nothing in either file able to say they disagreed.
453
+
454
+ `casefold` rather than `lower`, which is the comparison Python offers
455
+ for "are these the same name to a human"; for ASCII segment names they
456
+ are identical and the difference costs nothing.
457
+ """
458
+ return frozenset(name.casefold() for name in names)
459
+
460
+
461
+ #: PUBLIC, because `shell.py`'s raw-token branch compares against it too
462
+ #: and a second `folded_segments(PROTECTED_SEGMENTS)` at that module's top
463
+ #: would be one derived value computed twice.
464
+ PROTECTED_SEGMENTS_FOLDED = folded_segments(PROTECTED_SEGMENTS)
465
+
466
+
467
+ def protected_segment(resolved_path: str) -> str | None:
468
+ """The protected segment *resolved_path* carries, or None.
469
+
470
+ Callers pass a realpath'd path (file_ops' ``resolve_path``, the
471
+ sandbox's ``_within``), so `..` travel and symlinks are already
472
+ collapsed: a link pointing at `.venastine/settings.json` arrives here
473
+ as the real thing. Split on both separators because a raw shell token
474
+ may carry either.
475
+
476
+ CASE-FOLDED ON EVERY PLATFORM, and the word "realpath" above is exactly
477
+ why it has to be (batch 146). This used to say that on Windows a
478
+ case-spelled path "arrives here as the real thing" too, and that is
479
+ true only where the path ALREADY EXISTS for realpath to canonicalise --
480
+ which is not the state a `write` is in. With no `.venastine/` yet,
481
+ `.VENASTINE/mcp.json` resolved to itself, carried no known segment, was
482
+ auto-approved, and created the protected directory under the spelling
483
+ the guard did not know. After that realpath canonicalised every later
484
+ path to the ON-DISK name, so BOTH spellings were admitted in that
485
+ project from then on, and the file `config_loader` opens at startup was
486
+ one of them. Measured, including the control: a project whose directory
487
+ was created lowercase refuses both.
488
+
489
+ Folding unconditionally rather than per platform is the fail-closed
490
+ direction and costs what it costs: on a case-SENSITIVE filesystem a
491
+ directory genuinely named `.VENASTINE` is now denied to the file tools
492
+ with no prompt and no override, because for them this list is the hard
493
+ deny rather than a reason to ask. The shell consumer only ever ASKS, so
494
+ there the same fold costs one prompt.
495
+ """
496
+ for part in re.split(r"[\\/]", resolved_path):
497
+ if part.casefold() in PROTECTED_SEGMENTS_FOLDED:
498
+ # The spelling from the PATH, not from the list, so the refusal
499
+ # names what the caller actually wrote.
500
+ return part
501
+ return None