@yahyahammad/venastine 0.1.0

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 (143) hide show
  1. package/.env.example +14 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +14 -0
  4. package/PRIVACY.md +66 -0
  5. package/README.md +878 -0
  6. package/SECURITY.md +88 -0
  7. package/THIRD_PARTY_NOTICES.md +100 -0
  8. package/agents/builtin/compactor.md +75 -0
  9. package/agents/builtin/explore.md +77 -0
  10. package/agents/builtin/grill-me.md +27 -0
  11. package/agents/builtin/initializer.md +94 -0
  12. package/agents/builtin/pipeline-reviewer.md +100 -0
  13. package/agents/builtin/plan.md +88 -0
  14. package/agents/builtin/review.md +74 -0
  15. package/agents/manager.py +195 -0
  16. package/agents/subagent_tool.py +215 -0
  17. package/agents/tui_commands.py +136 -0
  18. package/bin/venastine.mjs +382 -0
  19. package/config.py +1198 -0
  20. package/core/__init__.py +0 -0
  21. package/core/approval.py +115 -0
  22. package/core/client.py +1548 -0
  23. package/core/compaction.py +959 -0
  24. package/core/config_loader.py +1563 -0
  25. package/core/events.py +61 -0
  26. package/core/interaction.py +328 -0
  27. package/core/loop.py +1353 -0
  28. package/core/memory.py +370 -0
  29. package/core/model_windows.py +175 -0
  30. package/core/pipeline_models.py +176 -0
  31. package/core/reasoning/__init__.py +0 -0
  32. package/core/reasoning/authorization.py +130 -0
  33. package/core/reasoning/base.py +219 -0
  34. package/core/reasoning/confidence_scoring.py +468 -0
  35. package/core/reasoning/events.py +149 -0
  36. package/core/reasoning/json_retry.py +219 -0
  37. package/core/reasoning/orchestrator.py +1382 -0
  38. package/core/reasoning/output_writer.py +286 -0
  39. package/core/reasoning/payload_validation.py +398 -0
  40. package/core/reasoning/pipeline_storage.py +361 -0
  41. package/core/reasoning/review.py +615 -0
  42. package/core/reasoning/scholar.py +434 -0
  43. package/core/reasoning/source_corpus.py +327 -0
  44. package/core/reasoning/source_scoring.py +855 -0
  45. package/core/replay.py +167 -0
  46. package/core/session.py +153 -0
  47. package/core/workspace_trust.py +207 -0
  48. package/credentials.py +180 -0
  49. package/database.py +252 -0
  50. package/env_secrets.py +77 -0
  51. package/json_store.py +123 -0
  52. package/logging_setup.py +185 -0
  53. package/main.py +1959 -0
  54. package/mcp_client/__init__.py +15 -0
  55. package/mcp_client/client.py +499 -0
  56. package/mcp_client/config.py +379 -0
  57. package/mcp_client/registration.py +182 -0
  58. package/memories/manager.py +132 -0
  59. package/memories/tui_commands.py +232 -0
  60. package/package.json +73 -0
  61. package/project_init/config_files.py +278 -0
  62. package/project_init/doc_sets.py +330 -0
  63. package/project_init/generator.py +564 -0
  64. package/project_init/manifest.py +406 -0
  65. package/project_init/tui_commands.py +144 -0
  66. package/prompts/__init__.py +0 -0
  67. package/prompts/annotate.md +14 -0
  68. package/prompts/assumption_audit.md +27 -0
  69. package/prompts/claim_extraction.md +34 -0
  70. package/prompts/completeness.md +18 -0
  71. package/prompts/confidence_tiers.md +23 -0
  72. package/prompts/critic_pass.md +24 -0
  73. package/prompts/final_synthesis.md +13 -0
  74. package/prompts/initial_generation.md +14 -0
  75. package/prompts/preliminary_plan.md +23 -0
  76. package/prompts/revalidate.md +25 -0
  77. package/prompts/revise.md +15 -0
  78. package/prompts/source_grounding.md +66 -0
  79. package/prompts/system_prompts.py +262 -0
  80. package/prompts/universal_system_prompt +7 -0
  81. package/prompts/untrusted_content +1 -0
  82. package/providers.json.example +92 -0
  83. package/pyproject.toml +100 -0
  84. package/requirements.txt +72 -0
  85. package/safety/__init__.py +0 -0
  86. package/safety/policy_enforcement.py +682 -0
  87. package/security/__init__.py +0 -0
  88. package/security/capability.py +177 -0
  89. package/security/permissions.py +190 -0
  90. package/security/posture.py +302 -0
  91. package/security/protected_paths.py +309 -0
  92. package/security/sandbox.py +985 -0
  93. package/skills/builtin/crypto/cryptography-verification.md +66 -0
  94. package/skills/builtin/math/formal-specification.md +67 -0
  95. package/skills/builtin/math/numerical-methods.md +66 -0
  96. package/skills/builtin/math/proof-writing.md +61 -0
  97. package/skills/builtin/research/experiment-design.md +62 -0
  98. package/skills/builtin/research/literature-review.md +65 -0
  99. package/skills/builtin/research/reproducibility.md +55 -0
  100. package/skills/builtin/research/source-evaluation.md +70 -0
  101. package/skills/builtin/research/statistical-inference.md +63 -0
  102. package/skills/builtin/research/technical-writing.md +59 -0
  103. package/skills/builtin/security/cybersecurity-research.md +63 -0
  104. package/skills/builtin/software/code-review.md +61 -0
  105. package/skills/builtin/software/debugging.md +61 -0
  106. package/skills/builtin/software/test-design.md +67 -0
  107. package/skills/manager.py +138 -0
  108. package/skills/tui_commands.py +111 -0
  109. package/storage.py +1005 -0
  110. package/tools/base.py +305 -0
  111. package/tools/builtin/__init__.py +0 -0
  112. package/tools/builtin/_math_common.py +377 -0
  113. package/tools/builtin/_net_common.py +61 -0
  114. package/tools/builtin/arxiv.py +223 -0
  115. package/tools/builtin/ask_user.py +213 -0
  116. package/tools/builtin/discrete_math.py +134 -0
  117. package/tools/builtin/fetch_url.py +153 -0
  118. package/tools/builtin/file_ops.py +412 -0
  119. package/tools/builtin/geometry.py +148 -0
  120. package/tools/builtin/get_time.py +20 -0
  121. package/tools/builtin/linear_algebra.py +155 -0
  122. package/tools/builtin/load_skill.py +66 -0
  123. package/tools/builtin/logic.py +96 -0
  124. package/tools/builtin/pin.py +193 -0
  125. package/tools/builtin/probability_stats.py +228 -0
  126. package/tools/builtin/project_docs.py +405 -0
  127. package/tools/builtin/remember.py +158 -0
  128. package/tools/builtin/shell.py +461 -0
  129. package/tools/builtin/symbolic_math.py +119 -0
  130. package/tools/builtin/todo.py +147 -0
  131. package/tools/builtin/web_search.py +151 -0
  132. package/tools/context.py +136 -0
  133. package/tools/isolation.py +314 -0
  134. package/tools/registry.py +897 -0
  135. package/tui/app.py +3423 -0
  136. package/tui/app.tcss +458 -0
  137. package/tui/commands.py +61 -0
  138. package/tui/diffs.py +265 -0
  139. package/tui/preferences.py +182 -0
  140. package/tui/ravens.py +146 -0
  141. package/tui/screens.py +943 -0
  142. package/tui/themes.py +366 -0
  143. package/tui/widgets.py +1239 -0
package/core/replay.py ADDED
@@ -0,0 +1,167 @@
1
+ """
2
+ core/replay.py
3
+
4
+ Turns a stored thread back into something a shell can display
5
+ (ROADMAP_v2 §27, bug 1).
6
+
7
+ WHY THIS EXISTS AT ALL. Resuming a thread loaded its history correctly and
8
+ showed NOTHING: `ConversationMemory(thread_id=...)` reads the full history
9
+ at construction, but the TUI's picker callback swapped the object and wrote
10
+ one line, and `main.py --thread` printed "Resuming thread: <uuid>" and
11
+ dropped straight into the prompt. In the TUI it was worse than blank -- the
12
+ PREVIOUS thread's transcript stayed on screen under the new thread's id, so
13
+ the screen actively lied about which conversation was loaded.
14
+
15
+ WHY IT IS ONE FUNCTION AND NOT TWO RENDERERS. D12 makes the CLI a permanent
16
+ fallback, so both shells replay (§27 T5, §26's L6 again). What may be shown,
17
+ and in what form, is a policy decision that must not differ between them --
18
+ so the policy lives here and each shell only decides how to paint the
19
+ entries it is handed. §26 spent a decision (L2, "one translation site") on
20
+ exactly this shape.
21
+
22
+ THE ARCHIVE, NEVER THE DERIVED VIEW (T3). `storage.archive_history()`
23
+ returns every message ever written, compaction or not.
24
+ `ConversationMemory.messages` on a compacted thread BEGINS with the
25
+ synthesized summary that §21's M8 says must never be mistaken for something
26
+ a person said -- and replaying it would render harness-generated text under
27
+ a `you ›` label, which is precisely the mistake M8 exists to prevent. It
28
+ also means a replayed thread shows what was originally said rather than a
29
+ condensation of it, which is what a human resuming a conversation is looking
30
+ for.
31
+
32
+ THE LIVE VIEW SHOWS MORE THAN THIS ONE, DELIBERATELY (§41). Since batch 41
33
+ the TUI renders a `write` or an `edit` as an inline diff, and a REPLAYED one
34
+ here still shows `param_digest`. That is not a gap to close: a replay reads
35
+ the archive, which records what was called and never what the file held at
36
+ the time, so the digest is genuinely all this function has. The live view is
37
+ better informed because it was there.
38
+
39
+ REASONING IS REPLAYED, SINCE §44. §38 captured thinking for display and
40
+ persisted none of it, so reopening a thread showed the answers with the
41
+ reasoning silently missing -- the same conversation rendered two different
42
+ ways depending on whether the session had been closed. The blocks are
43
+ stored now (storage.MessageLog.thinking) and _reasoning_text pulls the
44
+ prose back out of them. What a shell does with the entry is still the
45
+ shell's: the TUI honours tui.show_thinking, because that is a display
46
+ setting rather than a fact about the thread.
47
+
48
+ TOOL RESULTS ARE SKIPPED, TOOL CALLS ARE ONE LINE (T4). A grounding-heavy
49
+ thread carries hundreds of kilobytes of fetched page text in its
50
+ `tool_result` rows; replaying that would bury the conversation in the
51
+ material the conversation was about. The call is what says what happened,
52
+ and `param_digest` says WHICH url or query -- redacted, because a tool
53
+ argument can carry a credential and this is now the third place one could
54
+ surface.
55
+ """
56
+
57
+ from __future__ import annotations
58
+
59
+ from typing import List, Optional, Tuple
60
+ from uuid import UUID
61
+
62
+ from tools.registry import registry
63
+ from storage import archive_history
64
+
65
+ #: One replayed entry: (role, text). The roles are transcript palette roles
66
+ #: (tui/themes.role_styles), so the TUI can paint them with what it already
67
+ #: has and the CLI can label them. Deliberately NOT the neutral message
68
+ #: shape -- a caller of this function is rendering, not reasoning about
69
+ #: history, and handing it messages would invite a second policy decision
70
+ #: about tool results at the call site.
71
+ ReplayEntry = Tuple[str, str]
72
+
73
+
74
+ def replay_entries(thread_id: UUID) -> List[ReplayEntry]:
75
+ """A stored thread as display entries, oldest first (§27 T3/T4).
76
+
77
+ Empty for a thread with no messages -- a `/new` thread that was never
78
+ used is a real state, and the shells write their own "nothing here yet"
79
+ line rather than this function inventing one.
80
+ """
81
+ entries: List[ReplayEntry] = []
82
+ for message in archive_history(thread_id):
83
+ role = message.get("role")
84
+ if role == "user":
85
+ text = _as_text(message.get("content"))
86
+ if text:
87
+ entries.append(("user", text))
88
+ elif role == "assistant":
89
+ # BEFORE the answer, because that is the order it happened in
90
+ # and the order the live transcript drew it in (§43 RM1 puts
91
+ # one `venastine ›` above whichever came first). A replay that
92
+ # reordered the turn would be the same defect as one that
93
+ # reflowed it.
94
+ reasoning = _reasoning_text(message.get("thinking"))
95
+ if reasoning:
96
+ entries.append(("thinking", reasoning))
97
+ text = _as_text(message.get("text"))
98
+ if text:
99
+ entries.append(("assistant", text))
100
+ for call in message.get("tool_calls") or []:
101
+ entries.append(("tool", _tool_marker(call)))
102
+ # role == "tool": skipped by T4. Not a gap -- see the module
103
+ # docstring. The CALL above is the record that it happened.
104
+ return entries
105
+
106
+
107
+ def _reasoning_text(record) -> str:
108
+ """The displayable prose out of a stored thinking record (§44).
109
+
110
+ The record holds the PROVIDER'S blocks, because that is what goes back
111
+ on the wire; this is the other half of the same data, for a human. Two
112
+ shapes reach here -- Anthropic's `thinking` blocks and the
113
+ v1-compatible `reasoning_content` one -- and both are read by field
114
+ name rather than by provider, so a third costs a name and not a
115
+ branch.
116
+
117
+ `redacted_thinking` contributes NOTHING and that is correct: it is
118
+ opaque ciphertext meaningful only to the model, so rendering it would
119
+ put a wall of base64 under a `venastine ›` label. It still travels on
120
+ the wire; this function is about what a person sees.
121
+ """
122
+ if not isinstance(record, dict):
123
+ return ""
124
+ parts = []
125
+ for block in record.get("blocks") or []:
126
+ if not isinstance(block, dict):
127
+ continue
128
+ value = block.get("thinking") or block.get("text")
129
+ if isinstance(value, str) and value.strip():
130
+ parts.append(value.strip())
131
+ return "\n\n".join(parts)
132
+
133
+
134
+ def _tool_marker(call: dict) -> str:
135
+ """One line for one tool call: name, then a redacted param digest.
136
+
137
+ The digest comes from `registry.call_digest` rather than from a
138
+ local copy -- it redacts BEFORE truncating, and a second
139
+ implementation is how that ordering regresses in one place and
140
+ not the other (§27's note on _param_digest, and the project's
141
+ canonical producer/consumer bug).
142
+
143
+ Through the REGISTRY since §42 (RA3), not `param_digest` direct,
144
+ because dropping a tool's declared rationale param is part of the
145
+ same rule and belongs at the same producer. Importing the registry
146
+ here costs nothing measurable: `main.py` is this module's only
147
+ caller and has already imported it, so it is in `sys.modules`
148
+ (1.9s cold at process start, 0.0s here).
149
+ """
150
+ name = call.get("name") or "tool"
151
+ digest = registry.call_digest(name, call.get("input"))
152
+ # Standardised to ▸ like the live transcript (tui/app.py both modes).
153
+ # Replay stays redacted via param_digest, same producer as live.
154
+ return f"▸ {name} {digest}".rstrip() if digest else f"▸ {name}"
155
+
156
+
157
+ def _as_text(value: Optional[object]) -> str:
158
+ """Message content as a displayable string.
159
+
160
+ `content` is whatever was persisted through json.dumps, so it is
161
+ normally a string but need not be; str() rather than a type assertion,
162
+ because a replay that raises on one odd row would take the whole resumed
163
+ conversation with it.
164
+ """
165
+ if value is None:
166
+ return ""
167
+ return value.strip() if isinstance(value, str) else str(value)
@@ -0,0 +1,153 @@
1
+ """
2
+ core/session.py
3
+
4
+ Ephemeral, per-session override of ONE compaction number, and nothing
5
+ else. ROADMAP_v2 §21's revisit, batch 31; halved in batch 44.
6
+
7
+ WHAT THIS MODULE OWNS
8
+ set_trigger record an override against a (provider, model)
9
+ trigger_for read it back, but only for that same pair
10
+ trigger_any the binding-free read, for #89's pin cap
11
+ clear drop it, on a /model switch
12
+
13
+ WHAT IT DOES NOT OWN
14
+ Resolving the number -- core/compaction.py:thresholds() does that, and
15
+ treats what is here as the most local source rather than as the answer.
16
+ Persisting anything: NOTHING here reaches disk, which is the whole point
17
+ (see NEVER PERSISTED below).
18
+
19
+ ONE NUMBER NOW, AND THE OTHER ONE LEFT FOR A GOOD REASON. Batch 31 held
20
+ two, `window` and `trigger`, and was careful to say they were not the same
21
+ number. They still are not -- but they no longer have the same LIFETIME,
22
+ which is what this module is really about:
23
+
24
+ trigger the working-set ceiling. Ephemeral, because it is a knob you
25
+ reach for while a particular thread is behaving badly, and
26
+ restarting should give the derived number back. Still here.
27
+ window what context_limit() returns. It is not a preference at all --
28
+ it is a FACT about a deployment, and a fact does not want to be
29
+ re-entered every launch. Batch 44 moved it to
30
+ core/model_windows.py, a user-tier store keyed on every
31
+ (provider, model) the user has answered for. Keeping an
32
+ ephemeral tier ABOVE that store was the rejected alternative:
33
+ /model would clear a value the store immediately re-supplied,
34
+ so "override cleared" would be true and change nothing.
35
+
36
+ BOUND TO A (provider, model) PAIR, AND THAT IS THE INTERESTING RULE.
37
+ An override applies only when the model that was named is the model
38
+ actually running. A read from any other pair returns None WITHOUT
39
+ clearing, so:
40
+
41
+ * an agent that pins its own model (§18) uses that model's derived
42
+ value, rather than a number tuned for a different one;
43
+ * a critic-routed research pass (§11) does the same -- and, because a
44
+ mismatch does not clear, one routine critic pass cannot silently
45
+ destroy an override set for the chat;
46
+ * a research pass or subagent on the SESSION's model matches, so it
47
+ inherits with no plumbing at all. Propagation is a consequence of the
48
+ binding, not a separate mechanism.
49
+
50
+ Clearing is a separate act, and only /model does it. That is what makes
51
+ "switch away and come back" return the DEFAULT rather than the old
52
+ override: the pair-binding alone would restore it, so both rules are
53
+ needed and they do different jobs.
54
+
55
+ NEVER PERSISTED. Not settings.json (project tier beats user tier there,
56
+ D29 -- and a value that survives a restart is not what was asked for),
57
+ not extra_data, not anywhere. Restarting the harness is the guaranteed
58
+ way back to the derived number.
59
+
60
+ MODULE STATE, written from the UI thread and read from worker threads.
61
+ Same posture as core/compaction.py's `_compacting` flag and for the same
62
+ reason: these are single assignments of an int or a tuple, so there is no
63
+ half-set state to observe. Tests clear it in an autouse fixture, which is
64
+ the rule every other module-level cache in this project follows.
65
+ """
66
+
67
+ import logging
68
+ from typing import Optional
69
+
70
+ logger = logging.getLogger(__name__)
71
+
72
+ # key -> (provider_name, model, value). One slot each; setting replaces.
73
+ _overrides: dict = {}
74
+
75
+ TRIGGER = "trigger"
76
+
77
+
78
+ def _set(key: str, provider_name: str, model: str, value: int) -> None:
79
+ _overrides[key] = (provider_name, model, int(value))
80
+ logger.info("Session %s override: %s for %s/%s (this session only).",
81
+ key, value, provider_name, model)
82
+
83
+
84
+ def _get(key: str, provider_name: Optional[str],
85
+ model: Optional[str]) -> Optional[int]:
86
+ """The override for exactly this pair, or None.
87
+
88
+ A pair mismatch is not an error and does not clear -- see the module
89
+ docstring. `provider_name` is Optional because compaction.context_limit
90
+ still accepts callers that do not know it; without a provider no
91
+ override can match, which is the same answer as having none.
92
+ """
93
+ entry = _overrides.get(key)
94
+ if entry is None or not provider_name:
95
+ return None
96
+ stored_provider, stored_model, value = entry
97
+ if stored_provider == provider_name and stored_model == model:
98
+ return value
99
+ return None
100
+
101
+
102
+ def set_trigger(provider_name: str, model: str, tokens: int) -> None:
103
+ """Override the working-set compaction ceiling for this pair."""
104
+ _set(TRIGGER, provider_name, model, tokens)
105
+
106
+
107
+ def trigger_for(provider_name: Optional[str],
108
+ model: Optional[str]) -> Optional[int]:
109
+ return _get(TRIGGER, provider_name, model)
110
+
111
+
112
+ def trigger_any() -> Optional[int]:
113
+ """The trigger override IGNORING the pair binding.
114
+
115
+ Exists for exactly one caller: compaction.pin_measurements, which
116
+ computes #89's cap as PIN_MAX_TRIGGER_FRACTION x the trigger and has
117
+ no model in scope to match against. That caller takes the MINIMUM of
118
+ this and the configured trigger, which is safe under every binding --
119
+ a raised session trigger leaves the cap stricter than it needs to be,
120
+ and a lowered one pulls the cap down with it. Reading the configured
121
+ trigger alone would let `/trigger 12k` leave pins capped at 20k, able
122
+ to floor a thread permanently above its own trigger, which is the
123
+ defect #89 exists to prevent.
124
+
125
+ Do not reach for this anywhere the pair IS available.
126
+ """
127
+ entry = _overrides.get(TRIGGER)
128
+ return entry[2] if entry else None
129
+
130
+
131
+ def clear(key: Optional[str] = None) -> list:
132
+ """Drop overrides and report which were actually dropped.
133
+
134
+ Called with no key by /model. `/trigger off` clears the same one by
135
+ name; the list form survives a second key being added rather than
136
+ being collapsed to the one that exists today. The return value is what
137
+ the shell says out loud -- a switch that silently discarded a number
138
+ the user set would be exactly the invisible state this feature is
139
+ careful not to introduce.
140
+
141
+ /window is NOT here since batch 44: a remembered window survives the
142
+ switch, and clearing it is `/window off`.
143
+ """
144
+ keys = [key] if key else [TRIGGER]
145
+ dropped = [name for name in keys if _overrides.pop(name, None) is not None]
146
+ if dropped:
147
+ logger.info("Session overrides cleared: %s.", ", ".join(dropped))
148
+ return dropped
149
+
150
+
151
+ def state() -> dict:
152
+ """{key: (provider, model, value)} for the bare-command report."""
153
+ return dict(_overrides)
@@ -0,0 +1,207 @@
1
+ """
2
+ core/workspace_trust.py
3
+
4
+ Workspace trust gate (ROADMAP_v2 §14, D17). Project-level `.venastine/`
5
+ content (agents, skills, settings.json, AGENTS.md, mcp.json) only loads
6
+ after a one-time explicit user confirmation, keyed by resolved project
7
+ path + a content hash of everything under `.venastine/`. Changed content
8
+ after trust was granted produces a different hash and re-triggers the
9
+ prompt rather than silently inheriting stale trust.
10
+
11
+ The store lives at user level (~/.config/venastine/trusted_projects.json).
12
+ Until trust is granted, project-level content does not load AT ALL --
13
+ absent, not loaded-but-disabled. No partial trust.
14
+ """
15
+
16
+ import hashlib
17
+ import json
18
+ import os
19
+
20
+ from json_store import write_json_atomic
21
+
22
+
23
+ def _trust_store_path() -> str:
24
+ # Resolved at call time, not import time, so tests can redirect the
25
+ # store via HOME/USERPROFILE and a moved config dir is picked up
26
+ # without a restart.
27
+ return os.path.expanduser("~/.config/venastine/trusted_projects.json")
28
+
29
+
30
+ def venastine_dir(project_path: str) -> str:
31
+ return os.path.join(os.path.realpath(project_path), ".venastine")
32
+
33
+
34
+ #: The project's context document, and the ONE file outside .venastine/
35
+ #: that the loader injects into a system prompt (§44, replacing
36
+ #: .venastine/CONTEXT.md). Defined HERE rather than beside /init, because
37
+ #: the interesting fact about it is that it is inside the trust boundary:
38
+ #: config_loader reads it, tools/builtin/project_docs writes it, and
39
+ #: project_init/doc_sets names it, all from this one constant.
40
+ PROJECT_CONTEXT_FILENAME = "AGENTS.md"
41
+
42
+
43
+ def content_files(project_path: str) -> list[str]:
44
+ """Sorted PROJECT-RELATIVE paths of everything a grant covers.
45
+
46
+ Everything under .venastine/, plus the root AGENTS.md when it exists.
47
+
48
+ THE ROOT FILE IS IN HERE BECAUSE IT REACHES THE SYSTEM PROMPT (§44).
49
+ D17 gates .venastine/ for one reason: its content becomes instructions,
50
+ and "project" means "arrived with a repo you cloned". Moving the hub
51
+ out of that directory without moving the boundary would have left a
52
+ cloned AGENTS.md entering an opted-in agent's system prompt with no
53
+ prompt at all -- D17's own stated threat, through the door §44 opened.
54
+ This module's invariant is the one that says so: the loader must read
55
+ no file the trust listing omits.
56
+
57
+ PATHS ARE RELATIVE TO THE PROJECT, not to .venastine/, which they were
58
+ before. They have to be: "AGENTS.md" and ".venastine/AGENTS.md" are
59
+ different files and a listing that could not tell them apart would be
60
+ a listing you cannot trust. The strings feed the hash, so every
61
+ existing grant re-prompts exactly once -- correct, because the set
62
+ being consented to has genuinely changed.
63
+ """
64
+ resolved = os.path.realpath(project_path)
65
+ out = []
66
+ if os.path.isfile(os.path.join(resolved, PROJECT_CONTEXT_FILENAME)):
67
+ out.append(PROJECT_CONTEXT_FILENAME)
68
+ root = venastine_dir(project_path)
69
+ if os.path.isdir(root):
70
+ for dirpath, dirs, files in os.walk(root):
71
+ dirs.sort()
72
+ for fname in sorted(files):
73
+ # posix separators so the hash (and the trust-prompt
74
+ # listing) are identical regardless of platform path
75
+ # conventions
76
+ rel = os.path.relpath(os.path.join(dirpath, fname), resolved)
77
+ out.append(rel.replace(os.sep, "/"))
78
+ return out
79
+
80
+
81
+ def _content_hash(project_path: str) -> str:
82
+ """Hashes relative path + content of everything content_files() lists.
83
+
84
+ ONE TRAVERSAL, NOT TWO THAT MUST AGREE (§44). This walked the tree a
85
+ second time and had to arrive at the same set as content_files() by
86
+ construction rather than by sharing code -- which #18 already showed
87
+ is not a safe way to keep two traversals aligned, and which AGENTS.md
88
+ recorded as the deeper fix still open. It iterates the listing now, so
89
+ the trust PROMPT and the trust DIGEST are one set of files by
90
+ definition: a file the listing omits cannot enter the hash, and this
91
+ module's invariant is that the loader may read no such file.
92
+
93
+ Two corrections survive from §14, both of which matter because this
94
+ hash is a security control rather than a cache key (Rev. 3):
95
+
96
+ 1. `os.walk` yields subdirectories in filesystem order, not sorted
97
+ order. Sorting `files` alone is not enough -- `dirs` must be
98
+ sorted IN PLACE so os.walk itself descends deterministically, or
99
+ the same unchanged project can hash differently between runs and
100
+ re-prompt for trust that was already granted. That sort now lives
101
+ in content_files(), which is the traversal.
102
+ 2. Hashing file CONTENT only makes the path invisible to the hash.
103
+ Two files swapping names produces an identical digest -- and the
104
+ directory a definition lives in is what determines how it's
105
+ loaded. The relative path is fed in too, with \\0 separators, so
106
+ concatenation boundaries can't be forged.
107
+ """
108
+ resolved = os.path.realpath(project_path)
109
+ hasher = hashlib.sha256()
110
+ for rel in content_files(project_path):
111
+ path = os.path.join(resolved, rel.replace("/", os.sep))
112
+ hasher.update(rel.encode("utf-8"))
113
+ hasher.update(b"\0")
114
+ # Bounded reads, and regular files only. This hash runs BEFORE the
115
+ # user has consented to anything, over content that arrived with a
116
+ # directory they cloned: f.read() on a multi-GB blob is one
117
+ # unbounded allocation at startup, and open() on a FIFO (os.walk
118
+ # lists it, and a symlink to one resolves) blocks forever. Neither
119
+ # is content worth reading to decide whether to trust it -- its
120
+ # presence and name are enough.
121
+ if os.path.isfile(path):
122
+ with open(path, "rb") as f:
123
+ for chunk in iter(lambda: f.read(1 << 16), b""):
124
+ hasher.update(chunk)
125
+ else:
126
+ hasher.update(b"<non-regular file>")
127
+ hasher.update(b"\0")
128
+ return hasher.hexdigest()
129
+
130
+
131
+ def is_trusted(project_path: str) -> bool:
132
+ """Whether this project's content may load.
133
+
134
+ "Nothing to trust" is now asked of the LISTING rather than of the
135
+ .venastine/ directory alone (§44). A project can have a root AGENTS.md
136
+ and no .venastine/ at all -- that is the ordinary shape of a repo
137
+ someone shares without their configuration, and it is exactly the
138
+ shape that must not walk into a system prompt unasked. The shortcut
139
+ narrows accordingly: a cloned repo shipping an AGENTS.md now prompts,
140
+ where before it was silent because the directory was absent.
141
+
142
+ An EMPTY .venastine/ also reads as nothing to trust now, where it
143
+ previously hashed to a constant and compared. Nothing loads from an
144
+ empty directory, so there was never a question to ask there.
145
+ """
146
+ resolved = os.path.realpath(project_path)
147
+ if not content_files(project_path):
148
+ return True # nothing to trust -- no project-level content at all
149
+ current_hash = _content_hash(project_path)
150
+ store = _load_trust_store()
151
+ return store.get(resolved) == current_hash
152
+
153
+
154
+ def grant_trust(project_path: str) -> None:
155
+ resolved = os.path.realpath(project_path)
156
+ store = _load_trust_store()
157
+ store[resolved] = _content_hash(project_path)
158
+ _save_trust_store(store)
159
+
160
+
161
+ def _load_trust_store() -> dict:
162
+ """The store, or {} for anything unreadable.
163
+
164
+ Fails CLOSED, in both senses. An empty store means nothing is
165
+ trusted, so a damaged file re-triggers the prompt -- which is this
166
+ module's stated posture for unknown state. Previously a partial write
167
+ (a kill, a Ctrl+C, a full disk) raised JSONDecodeError out of
168
+ is_trusted() at EVERY startup for EVERY project until the user found
169
+ and deleted the file by hand, and a valid-JSON-but-non-object store
170
+ escaped as an AttributeError on .get instead.
171
+ """
172
+ path = _trust_store_path()
173
+ if not os.path.exists(path):
174
+ return {}
175
+ try:
176
+ with open(path, "r", encoding="utf-8-sig") as f:
177
+ data = json.load(f)
178
+ except (OSError, ValueError):
179
+ return {}
180
+ return data if isinstance(data, dict) else {}
181
+
182
+
183
+ def _save_trust_store(store: dict) -> None:
184
+ """Write atomically, via json_store, which the three sibling user-tier
185
+ stores share. A truncate-then-write leaves the store empty for the
186
+ length of the write, and permanently damaged if the process dies
187
+ inside it -- for a file whose whole job is remembering a security
188
+ decision.
189
+
190
+ RAISES, unlike model_windows and preferences, which warn and return
191
+ False. That policy stays here rather than in the shared helper: a
192
+ trust grant that silently did not persist is worse than a crash, and
193
+ a remembered theme that did not is not.
194
+
195
+ 0600 on the TEMP FILE, not on the destination (#19). os.replace
196
+ carries the source file's mode across, so setting it afterwards would
197
+ be both a no-op and a lie; setting it on the destination beforehand
198
+ is worse, since replace overwrites that too.
199
+
200
+ This store holds no secret, so the exposure it closes is only "which
201
+ projects this user has trusted, and their content hashes" --
202
+ information disclosure, not a breach. It was never group- or
203
+ other-WRITABLE, so no one could forge a trust entry. Done for
204
+ consistency with credentials.py rather than because this file is the
205
+ dangerous one.
206
+ """
207
+ write_json_atomic(_trust_store_path(), store, mode=0o600)