@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.
- package/.env.example +14 -0
- package/LICENSE +201 -0
- package/NOTICE +14 -0
- package/PRIVACY.md +66 -0
- package/README.md +878 -0
- package/SECURITY.md +88 -0
- package/THIRD_PARTY_NOTICES.md +100 -0
- package/agents/builtin/compactor.md +75 -0
- package/agents/builtin/explore.md +77 -0
- package/agents/builtin/grill-me.md +27 -0
- package/agents/builtin/initializer.md +94 -0
- package/agents/builtin/pipeline-reviewer.md +100 -0
- package/agents/builtin/plan.md +88 -0
- package/agents/builtin/review.md +74 -0
- package/agents/manager.py +195 -0
- package/agents/subagent_tool.py +215 -0
- package/agents/tui_commands.py +136 -0
- package/bin/venastine.mjs +382 -0
- package/config.py +1198 -0
- package/core/__init__.py +0 -0
- package/core/approval.py +115 -0
- package/core/client.py +1548 -0
- package/core/compaction.py +959 -0
- package/core/config_loader.py +1563 -0
- package/core/events.py +61 -0
- package/core/interaction.py +328 -0
- package/core/loop.py +1353 -0
- package/core/memory.py +370 -0
- package/core/model_windows.py +175 -0
- package/core/pipeline_models.py +176 -0
- package/core/reasoning/__init__.py +0 -0
- package/core/reasoning/authorization.py +130 -0
- package/core/reasoning/base.py +219 -0
- package/core/reasoning/confidence_scoring.py +468 -0
- package/core/reasoning/events.py +149 -0
- package/core/reasoning/json_retry.py +219 -0
- package/core/reasoning/orchestrator.py +1382 -0
- package/core/reasoning/output_writer.py +286 -0
- package/core/reasoning/payload_validation.py +398 -0
- package/core/reasoning/pipeline_storage.py +361 -0
- package/core/reasoning/review.py +615 -0
- package/core/reasoning/scholar.py +434 -0
- package/core/reasoning/source_corpus.py +327 -0
- package/core/reasoning/source_scoring.py +855 -0
- package/core/replay.py +167 -0
- package/core/session.py +153 -0
- package/core/workspace_trust.py +207 -0
- package/credentials.py +180 -0
- package/database.py +252 -0
- package/env_secrets.py +77 -0
- package/json_store.py +123 -0
- package/logging_setup.py +185 -0
- package/main.py +1959 -0
- package/mcp_client/__init__.py +15 -0
- package/mcp_client/client.py +499 -0
- package/mcp_client/config.py +379 -0
- package/mcp_client/registration.py +182 -0
- package/memories/manager.py +132 -0
- package/memories/tui_commands.py +232 -0
- package/package.json +73 -0
- package/project_init/config_files.py +278 -0
- package/project_init/doc_sets.py +330 -0
- package/project_init/generator.py +564 -0
- package/project_init/manifest.py +406 -0
- package/project_init/tui_commands.py +144 -0
- package/prompts/__init__.py +0 -0
- package/prompts/annotate.md +14 -0
- package/prompts/assumption_audit.md +27 -0
- package/prompts/claim_extraction.md +34 -0
- package/prompts/completeness.md +18 -0
- package/prompts/confidence_tiers.md +23 -0
- package/prompts/critic_pass.md +24 -0
- package/prompts/final_synthesis.md +13 -0
- package/prompts/initial_generation.md +14 -0
- package/prompts/preliminary_plan.md +23 -0
- package/prompts/revalidate.md +25 -0
- package/prompts/revise.md +15 -0
- package/prompts/source_grounding.md +66 -0
- package/prompts/system_prompts.py +262 -0
- package/prompts/universal_system_prompt +7 -0
- package/prompts/untrusted_content +1 -0
- package/providers.json.example +92 -0
- package/pyproject.toml +100 -0
- package/requirements.txt +72 -0
- package/safety/__init__.py +0 -0
- package/safety/policy_enforcement.py +682 -0
- package/security/__init__.py +0 -0
- package/security/capability.py +177 -0
- package/security/permissions.py +190 -0
- package/security/posture.py +302 -0
- package/security/protected_paths.py +309 -0
- package/security/sandbox.py +985 -0
- package/skills/builtin/crypto/cryptography-verification.md +66 -0
- package/skills/builtin/math/formal-specification.md +67 -0
- package/skills/builtin/math/numerical-methods.md +66 -0
- package/skills/builtin/math/proof-writing.md +61 -0
- package/skills/builtin/research/experiment-design.md +62 -0
- package/skills/builtin/research/literature-review.md +65 -0
- package/skills/builtin/research/reproducibility.md +55 -0
- package/skills/builtin/research/source-evaluation.md +70 -0
- package/skills/builtin/research/statistical-inference.md +63 -0
- package/skills/builtin/research/technical-writing.md +59 -0
- package/skills/builtin/security/cybersecurity-research.md +63 -0
- package/skills/builtin/software/code-review.md +61 -0
- package/skills/builtin/software/debugging.md +61 -0
- package/skills/builtin/software/test-design.md +67 -0
- package/skills/manager.py +138 -0
- package/skills/tui_commands.py +111 -0
- package/storage.py +1005 -0
- package/tools/base.py +305 -0
- package/tools/builtin/__init__.py +0 -0
- package/tools/builtin/_math_common.py +377 -0
- package/tools/builtin/_net_common.py +61 -0
- package/tools/builtin/arxiv.py +223 -0
- package/tools/builtin/ask_user.py +213 -0
- package/tools/builtin/discrete_math.py +134 -0
- package/tools/builtin/fetch_url.py +153 -0
- package/tools/builtin/file_ops.py +412 -0
- package/tools/builtin/geometry.py +148 -0
- package/tools/builtin/get_time.py +20 -0
- package/tools/builtin/linear_algebra.py +155 -0
- package/tools/builtin/load_skill.py +66 -0
- package/tools/builtin/logic.py +96 -0
- package/tools/builtin/pin.py +193 -0
- package/tools/builtin/probability_stats.py +228 -0
- package/tools/builtin/project_docs.py +405 -0
- package/tools/builtin/remember.py +158 -0
- package/tools/builtin/shell.py +461 -0
- package/tools/builtin/symbolic_math.py +119 -0
- package/tools/builtin/todo.py +147 -0
- package/tools/builtin/web_search.py +151 -0
- package/tools/context.py +136 -0
- package/tools/isolation.py +314 -0
- package/tools/registry.py +897 -0
- package/tui/app.py +3423 -0
- package/tui/app.tcss +458 -0
- package/tui/commands.py +61 -0
- package/tui/diffs.py +265 -0
- package/tui/preferences.py +182 -0
- package/tui/ravens.py +146 -0
- package/tui/screens.py +943 -0
- package/tui/themes.py +366 -0
- package/tui/widgets.py +1239 -0
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
"""
|
|
2
|
+
project_init/generator.py
|
|
3
|
+
|
|
4
|
+
ROADMAP_v2 §24: the one place that decides what `/init` does.
|
|
5
|
+
|
|
6
|
+
BOTH SHELLS CALL THIS. §26's L6 and §27's T5 again: a per-shell
|
|
7
|
+
implementation is how the CLI and the TUI come to disagree about whether an
|
|
8
|
+
existing document is overwritten, which is a policy question and not a
|
|
9
|
+
painting one. The shells supply consent and rendering as callbacks; every
|
|
10
|
+
decision about what happens lives here.
|
|
11
|
+
|
|
12
|
+
THE AGENT PRODUCES TEXT; THIS MODULE WRITES (I8). The initializer gets
|
|
13
|
+
`read_project_doc` and nothing else, and returns the hub document's prose as its
|
|
14
|
+
final answer. Diffing, asking and dispatching `write_project_doc` happen
|
|
15
|
+
here, because consent belongs to the shell (§20's V3) and a tool writing
|
|
16
|
+
from inside the loop could not show a human a diff first.
|
|
17
|
+
|
|
18
|
+
ONE CONSENT, A NAMED LIST. `write_project_doc` is approval-gated, so a naive
|
|
19
|
+
implementation would prompt eight times for one command -- which is the
|
|
20
|
+
shape of consent that gets clicked through. The user is asked once, shown
|
|
21
|
+
the hub's diff and the exact list of files that will be created, and
|
|
22
|
+
that answer is then passed as `approval_callback` to each dispatch. Same
|
|
23
|
+
move core/loop.py makes after a channel approval, and for the same reason.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
|
|
28
|
+
import difflib
|
|
29
|
+
import logging
|
|
30
|
+
import os
|
|
31
|
+
from typing import Callable, Optional
|
|
32
|
+
|
|
33
|
+
import config
|
|
34
|
+
from core import config_loader, workspace_trust
|
|
35
|
+
from project_init import (config_files, doc_sets,
|
|
36
|
+
manifest as manifest_mod)
|
|
37
|
+
|
|
38
|
+
logger = logging.getLogger(__name__)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class InitError(Exception):
|
|
42
|
+
"""Something /init cannot proceed past. Carries a message meant for a
|
|
43
|
+
human, not a traceback; both shells print it verbatim.
|
|
44
|
+
|
|
45
|
+
It may be several lines. A write that fails partway through has to say
|
|
46
|
+
what it already did (#95), and one line cannot carry "this failed" and
|
|
47
|
+
"these eight files exist now" without losing one of them.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def propose_kind(project_path: str) -> tuple:
|
|
52
|
+
"""(kind, reason) for an established project (I13).
|
|
53
|
+
|
|
54
|
+
A PYTHON HEURISTIC, NOT A MODEL CALL, and confirmed by the user either
|
|
55
|
+
way. A code manifest is a strong, checkable signal; the alternative --
|
|
56
|
+
an extra round trip so a model can read the same filename this function
|
|
57
|
+
reads -- spends a call to reach the same place with less certainty
|
|
58
|
+
about how it got there. The reason is returned so the confirmation
|
|
59
|
+
prompt can show its working.
|
|
60
|
+
|
|
61
|
+
IT BRANCHES ON THE MANIFEST SET, NOT ON THE STACK (#96). Those are two
|
|
62
|
+
questions -- "is this a codebase" and "what is it written in" -- and
|
|
63
|
+
answering the first with the second is what made a Java, Ruby, Gradle,
|
|
64
|
+
C or container project `research` with "no code manifests found"
|
|
65
|
+
printed as the reason, while the manifest shown to the agent listed the
|
|
66
|
+
manifest. `Makefile` and `Dockerfile` still carry no stack, which is
|
|
67
|
+
the point: they answer the first question and not the second.
|
|
68
|
+
|
|
69
|
+
The reason now names the files it found, because I13 asked for working
|
|
70
|
+
that can be shown and a filename is the whole of the evidence.
|
|
71
|
+
"""
|
|
72
|
+
facts = manifest_mod.detect_facts(project_path)
|
|
73
|
+
found = facts.get("code_manifests")
|
|
74
|
+
if not found:
|
|
75
|
+
return (doc_sets.RESEARCH,
|
|
76
|
+
"no code manifests found, so this looks like written work "
|
|
77
|
+
"rather than a codebase")
|
|
78
|
+
named = ", ".join(found)
|
|
79
|
+
if facts.get("stack"):
|
|
80
|
+
return (doc_sets.SOFTWARE,
|
|
81
|
+
f"{facts['stack']} project files are present ({named})")
|
|
82
|
+
return doc_sets.SOFTWARE, f"code manifests are present ({named})"
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _existing_documents(project_path: str, kind: str) -> set:
|
|
86
|
+
"""Which documents in the set the project ALREADY has (I12)."""
|
|
87
|
+
from tools.builtin.project_docs import doc_path
|
|
88
|
+
return {name for name in doc_sets.document_set(kind)
|
|
89
|
+
if os.path.exists(doc_path(project_path, name))}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _config_plan(project_path: str) -> dict:
|
|
93
|
+
"""What `--config` would create here: `{"files", "dirs", "existing"}`.
|
|
94
|
+
|
|
95
|
+
AN EXISTING FILE IS NEVER OVERWRITTEN (I12, applied to the file it was
|
|
96
|
+
written for). The document rule -- only the hub is diffed and asked
|
|
97
|
+
about, because it is the only file /init claims to author -- reads
|
|
98
|
+
even harder here: settings.json and mcp.json are configuration
|
|
99
|
+
somebody tuned, and a scaffold of defaults landing on top of a tuned
|
|
100
|
+
file would be this command destroying the thing it exists to help you
|
|
101
|
+
start. Left alone and reported, per file, so a project with a
|
|
102
|
+
settings.json and no mcp.json still gets the mcp.json.
|
|
103
|
+
|
|
104
|
+
Directories are listed separately because creating one is not writing
|
|
105
|
+
anything: they carry nothing into the D17 hash (`content_files()`
|
|
106
|
+
walks files) and git does not track an empty directory, so they are a
|
|
107
|
+
hint on this machine and never part of what a clone receives.
|
|
108
|
+
"""
|
|
109
|
+
root = workspace_trust.venastine_dir(project_path)
|
|
110
|
+
names = (config_files.SETTINGS_FILENAME, config_files.MCP_FILENAME)
|
|
111
|
+
return {
|
|
112
|
+
"files": [name for name in names
|
|
113
|
+
if not os.path.exists(os.path.join(root, name))],
|
|
114
|
+
"existing": [name for name in names
|
|
115
|
+
if os.path.exists(os.path.join(root, name))],
|
|
116
|
+
"dirs": [name for name in config_files.TIER_DIRS
|
|
117
|
+
if not os.path.isdir(os.path.join(root, name))],
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _write_config(project_path: str, name: str) -> None:
|
|
122
|
+
"""One configuration file, written DIRECTLY (I14).
|
|
123
|
+
|
|
124
|
+
Not through `write_project_doc`, which denies `.venastine/` by path
|
|
125
|
+
segment and names these two files as the reason. Extending that
|
|
126
|
+
allowlist was the alternative and is rejected in the record: it would
|
|
127
|
+
advertise, to every run and all ten research passes, a tool that can
|
|
128
|
+
author a file naming a local command to execute. What replaces the
|
|
129
|
+
tool's approval gate is the same single consent that already covers
|
|
130
|
+
the document set -- the shape `_settle_trust` already uses to write
|
|
131
|
+
the trust store from here.
|
|
132
|
+
|
|
133
|
+
OSError becomes InitError, so a full disk or a read-only mount takes
|
|
134
|
+
the same reporting path as a failed document write (#95) rather than
|
|
135
|
+
escaping as a traceback.
|
|
136
|
+
"""
|
|
137
|
+
from json_store import write_json_atomic
|
|
138
|
+
|
|
139
|
+
payload = (config_files.render_settings()
|
|
140
|
+
if name == config_files.SETTINGS_FILENAME
|
|
141
|
+
else config_files.render_mcp())
|
|
142
|
+
path = os.path.join(workspace_trust.venastine_dir(project_path), name)
|
|
143
|
+
try:
|
|
144
|
+
write_json_atomic(path, payload, trailing_newline=True)
|
|
145
|
+
except OSError as e:
|
|
146
|
+
raise InitError(f"Could not write {path}: {e}")
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _shadow_lines() -> list:
|
|
150
|
+
"""What the scaffolded settings.json takes over from the user's own.
|
|
151
|
+
|
|
152
|
+
A project settings.json beats the user's (D29) by PRESENCE and not by
|
|
153
|
+
difference, so even a file of pure defaults starts deciding every key
|
|
154
|
+
it names. Said BEFORE the consent, where it can still change the
|
|
155
|
+
answer, rather than in the report afterwards.
|
|
156
|
+
"""
|
|
157
|
+
shadowed = config_files.shadowed(config_loader.user_settings())
|
|
158
|
+
if not shadowed:
|
|
159
|
+
return []
|
|
160
|
+
lines = ["This project's settings.json will now decide "
|
|
161
|
+
f"{len(shadowed)} key{'' if len(shadowed) == 1 else 's'} your "
|
|
162
|
+
f"own settings.json was deciding:"]
|
|
163
|
+
lines += [f" {path}: {theirs!r} -> {ours!r}"
|
|
164
|
+
for path, theirs, ours in shadowed]
|
|
165
|
+
lines.append("Delete those lines from the new file to go back to "
|
|
166
|
+
"yours.")
|
|
167
|
+
return lines
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _read_existing_context(project_path: str) -> Optional[str]:
|
|
171
|
+
from tools.builtin.project_docs import HUB_FILENAME, doc_path
|
|
172
|
+
path = doc_path(project_path, HUB_FILENAME)
|
|
173
|
+
if not os.path.exists(path):
|
|
174
|
+
return None
|
|
175
|
+
try:
|
|
176
|
+
with open(path, "r", encoding="utf-8-sig") as f:
|
|
177
|
+
return f.read()
|
|
178
|
+
except (OSError, UnicodeDecodeError) as e:
|
|
179
|
+
# Degrade like config_loader does with an unreadable hub document.
|
|
180
|
+
# An undecodable existing file must not stop /init from writing a
|
|
181
|
+
# good one -- but it is also not something to revise, so the run
|
|
182
|
+
# proceeds as if there were none.
|
|
183
|
+
logger.warning("Could not read the existing %s at %s; "
|
|
184
|
+
"generating a fresh one: %s", HUB_FILENAME, path, e)
|
|
185
|
+
return None
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _task(manifest_text: str, existing: Optional[str], kind: str) -> str:
|
|
189
|
+
"""The user message: the manifest, plus the current file if there is
|
|
190
|
+
one (I4). Both go HERE and not in the system prompt -- see
|
|
191
|
+
project_init/manifest.py on why that distinction is load-bearing.
|
|
192
|
+
|
|
193
|
+
THE HUB'S NAME IS INTERPOLATED, NEVER SPELLED (§44 WS8). This function
|
|
194
|
+
said "Write CONTEXT.md" for two batches after the hub became the root
|
|
195
|
+
AGENTS.md: the shell wrote to the right path, and the model drafted a
|
|
196
|
+
document that called itself by the old name. Reading the constant means
|
|
197
|
+
the next relocation cannot leave the prompt behind again.
|
|
198
|
+
"""
|
|
199
|
+
from tools.builtin.project_docs import HUB_FILENAME
|
|
200
|
+
|
|
201
|
+
parts = [
|
|
202
|
+
f"Write {HUB_FILENAME} for this {kind} project.",
|
|
203
|
+
"",
|
|
204
|
+
manifest_text,
|
|
205
|
+
]
|
|
206
|
+
if existing:
|
|
207
|
+
parts += [
|
|
208
|
+
f"## The current {HUB_FILENAME}",
|
|
209
|
+
"",
|
|
210
|
+
"This already exists and may contain hand edits. Revise it: keep "
|
|
211
|
+
"everything still true, correct what the project has outgrown, "
|
|
212
|
+
"add what is missing. Do not reword something merely to phrase "
|
|
213
|
+
"it your way.",
|
|
214
|
+
"",
|
|
215
|
+
existing,
|
|
216
|
+
"",
|
|
217
|
+
]
|
|
218
|
+
return "\n".join(parts)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _run_initializer(project_path: str, kind: str, existing: Optional[str],
|
|
222
|
+
model: str, provider_name: str) -> str:
|
|
223
|
+
from agents.manager import manager
|
|
224
|
+
from core.loop import RunAgentLoop, DEFAULT_SYSTEM_PROMPT
|
|
225
|
+
from storage import THREAD_KIND_SUBAGENT
|
|
226
|
+
|
|
227
|
+
agent = manager.get(config.INITIALIZER_AGENT)
|
|
228
|
+
if agent is None:
|
|
229
|
+
raise InitError(
|
|
230
|
+
f"The {config.INITIALIZER_AGENT!r} agent is not available, so "
|
|
231
|
+
f"there is nothing to generate the hub document with.")
|
|
232
|
+
|
|
233
|
+
context = manager.active_context(agent)
|
|
234
|
+
system_prompt = manager.system_prompt_for(
|
|
235
|
+
agent, DEFAULT_SYSTEM_PROMPT, context=context)
|
|
236
|
+
|
|
237
|
+
response = RunAgentLoop.run_agent_conversation(
|
|
238
|
+
user_goal=_task(manifest_mod.build_manifest(project_path),
|
|
239
|
+
existing, kind),
|
|
240
|
+
model=agent.model or model,
|
|
241
|
+
provider_name=agent.provider or provider_name,
|
|
242
|
+
max_steps=agent.max_steps or config.INIT_MAX_STEPS,
|
|
243
|
+
# #4: no separate /init ceiling any more -- the wrapper resolves the
|
|
244
|
+
# configured spend cap, same as every other path.
|
|
245
|
+
context=context,
|
|
246
|
+
system_prompt=system_prompt,
|
|
247
|
+
# §27: the initializer is a SIXTH thread source. Left unlabelled it
|
|
248
|
+
# clutters the picker with a thread nobody will ever resume.
|
|
249
|
+
thread_kind=THREAD_KIND_SUBAGENT,
|
|
250
|
+
)
|
|
251
|
+
body = (response.text or "").strip()
|
|
252
|
+
if not body:
|
|
253
|
+
raise InitError(
|
|
254
|
+
"The initializer returned nothing, so there is no hub document "
|
|
255
|
+
"to write. Nothing has been changed.")
|
|
256
|
+
return body
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def build_diff(old: Optional[str], new: str, path: str) -> str:
|
|
260
|
+
"""Unified diff of what /init would change (I5). Empty string when the
|
|
261
|
+
file would be identical, which the caller reports rather than writing."""
|
|
262
|
+
old_lines = (old or "").splitlines(keepends=True)
|
|
263
|
+
new_lines = new.splitlines(keepends=True)
|
|
264
|
+
label = os.path.basename(path)
|
|
265
|
+
return "".join(difflib.unified_diff(
|
|
266
|
+
old_lines, new_lines,
|
|
267
|
+
fromfile=f"{label} (current)" if old else "/dev/null",
|
|
268
|
+
tofile=f"{label} (proposed)"))
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def generate(
|
|
272
|
+
project_path: Optional[str] = None,
|
|
273
|
+
model: Optional[str] = None,
|
|
274
|
+
provider_name: Optional[str] = None,
|
|
275
|
+
*,
|
|
276
|
+
kind: Optional[str] = None,
|
|
277
|
+
confirm: Optional[Callable] = None,
|
|
278
|
+
notify: Optional[Callable] = None,
|
|
279
|
+
choose_kind: Optional[Callable] = None,
|
|
280
|
+
scaffold_docs: bool = True,
|
|
281
|
+
scaffold_config: bool = False,
|
|
282
|
+
) -> dict:
|
|
283
|
+
"""Scaffold this project's documentation set, its configuration, or both.
|
|
284
|
+
|
|
285
|
+
confirm(summary) -> bool the one consent (I5). None means there is no
|
|
286
|
+
way to ask, and therefore nothing is written.
|
|
287
|
+
notify(text) -> None progress and the diff, rendered by the shell.
|
|
288
|
+
choose_kind(proposal, reason, blank) -> str | None
|
|
289
|
+
resolves the project kind. Called with
|
|
290
|
+
blank=True when there is nothing to infer
|
|
291
|
+
from (I13), in which case `proposal` is None.
|
|
292
|
+
scaffold_docs the AGENTS.md hub and the stubs it links.
|
|
293
|
+
scaffold_config `.venastine/settings.json`, `mcp.json` and
|
|
294
|
+
the two tier directories (§24 I17).
|
|
295
|
+
|
|
296
|
+
TWO FLAGS RATHER THAN TWO FUNCTIONS. `--config` on its own is a
|
|
297
|
+
different OPERATION -- no kind question, no initializer, no spend --
|
|
298
|
+
but it is not a different set of decisions, and the decisions are
|
|
299
|
+
what this module exists to hold in one place. A config-only entry
|
|
300
|
+
point beside this one would be a second answer to "is an existing
|
|
301
|
+
file overwritten" and a second place to forget the trust settle.
|
|
302
|
+
|
|
303
|
+
Returns a notice dict in the {"kind", "text"} shape both shells render.
|
|
304
|
+
"""
|
|
305
|
+
from tools.builtin.project_docs import HUB_FILENAME, doc_path
|
|
306
|
+
from tools.registry import registry, ToolCallDenied
|
|
307
|
+
|
|
308
|
+
root = project_path or config_loader.get_project_path()
|
|
309
|
+
if root is None:
|
|
310
|
+
raise InitError("No project directory has been resolved.")
|
|
311
|
+
root = os.path.realpath(root)
|
|
312
|
+
say = notify or (lambda _text: None)
|
|
313
|
+
if not scaffold_docs and not scaffold_config:
|
|
314
|
+
raise InitError("/init was asked to scaffold nothing.")
|
|
315
|
+
|
|
316
|
+
body = None
|
|
317
|
+
diff = ""
|
|
318
|
+
to_create = []
|
|
319
|
+
already_there = set()
|
|
320
|
+
context_path = doc_path(root, HUB_FILENAME)
|
|
321
|
+
|
|
322
|
+
# ---- 1. Which set --------------------------------------------------
|
|
323
|
+
# Only when there is a set. `--config` alone asks nothing and spends
|
|
324
|
+
# nothing, which is the whole of why it is a separate flag rather
|
|
325
|
+
# than an addition to a run that always costs a model call.
|
|
326
|
+
if scaffold_docs:
|
|
327
|
+
if kind is None:
|
|
328
|
+
blank = manifest_mod.is_blank(root)
|
|
329
|
+
proposal, reason = (None, "") if blank else propose_kind(root)
|
|
330
|
+
if choose_kind is None:
|
|
331
|
+
# No way to ask. Refusing beats scaffolding seven files of
|
|
332
|
+
# the wrong kind into someone's project -- V6's rule, and
|
|
333
|
+
# the wrong answer here is expensive to undo by hand.
|
|
334
|
+
raise InitError(
|
|
335
|
+
"The project type could not be determined and there is "
|
|
336
|
+
"no way to ask. Re-run with --software or --research.")
|
|
337
|
+
kind = choose_kind(proposal, reason, blank)
|
|
338
|
+
if kind is None:
|
|
339
|
+
return {"kind": "init",
|
|
340
|
+
"text": "Cancelled — nothing was written."}
|
|
341
|
+
if kind not in doc_sets.PROJECT_KINDS:
|
|
342
|
+
raise InitError(f"Unknown project type {kind!r}; expected one of "
|
|
343
|
+
f"{', '.join(doc_sets.PROJECT_KINDS)}.")
|
|
344
|
+
|
|
345
|
+
# ---- 2. Generate -----------------------------------------------
|
|
346
|
+
existing_context = _read_existing_context(root)
|
|
347
|
+
say(f"Reading the project and drafting {HUB_FILENAME}…")
|
|
348
|
+
body = _run_initializer(root, kind, existing_context, model,
|
|
349
|
+
provider_name)
|
|
350
|
+
|
|
351
|
+
already_there = _existing_documents(root, kind)
|
|
352
|
+
body = f"{body.rstrip()}\n\n{doc_sets.render_index(kind)}"
|
|
353
|
+
|
|
354
|
+
to_create = [name for name in doc_sets.document_set(kind)
|
|
355
|
+
if name not in already_there]
|
|
356
|
+
diff = build_diff(existing_context, body, context_path)
|
|
357
|
+
|
|
358
|
+
# ---- 3. Show, then ask once ----------------------------------------
|
|
359
|
+
plan = _config_plan(root) if scaffold_config else {"files": [],
|
|
360
|
+
"existing": [],
|
|
361
|
+
"dirs": []}
|
|
362
|
+
if not diff and not to_create and not plan["files"] and not plan["dirs"]:
|
|
363
|
+
return {"kind": "init", "text": _nothing_to_do(kind, scaffold_docs,
|
|
364
|
+
scaffold_config)}
|
|
365
|
+
|
|
366
|
+
if scaffold_docs:
|
|
367
|
+
if diff:
|
|
368
|
+
say(diff)
|
|
369
|
+
else:
|
|
370
|
+
say(f"{HUB_FILENAME} is unchanged.")
|
|
371
|
+
if to_create:
|
|
372
|
+
say("Will also create, as stubs: " + ", ".join(to_create))
|
|
373
|
+
if already_there:
|
|
374
|
+
say("Leaving existing documents untouched: "
|
|
375
|
+
+ ", ".join(sorted(already_there)))
|
|
376
|
+
if plan["files"]:
|
|
377
|
+
say("Will create, with default values: "
|
|
378
|
+
+ ", ".join(".venastine/" + name for name in plan["files"]))
|
|
379
|
+
if config_files.SETTINGS_FILENAME in plan["files"]:
|
|
380
|
+
for line in _shadow_lines():
|
|
381
|
+
say(line)
|
|
382
|
+
if plan["existing"]:
|
|
383
|
+
say("Leaving existing configuration untouched: "
|
|
384
|
+
+ ", ".join(".venastine/" + name for name in plan["existing"]))
|
|
385
|
+
|
|
386
|
+
if confirm is None:
|
|
387
|
+
# §25's V6, and the same rule §20 applies to review corrections: the
|
|
388
|
+
# inability to ask is not permission to proceed. A piped `--init`
|
|
389
|
+
# reports what it would do and writes nothing.
|
|
390
|
+
return {"kind": "init",
|
|
391
|
+
"text": "Nothing was written: there is no way to confirm "
|
|
392
|
+
"these changes here. Re-run on a terminal."}
|
|
393
|
+
if not confirm(_consent_summary(context_path, to_create, bool(diff),
|
|
394
|
+
plan)):
|
|
395
|
+
return {"kind": "init", "text": "Declined — nothing was written."}
|
|
396
|
+
|
|
397
|
+
# ---- 4. Write ------------------------------------------------------
|
|
398
|
+
# Captured BEFORE the write (I6). Afterwards the content hash has moved
|
|
399
|
+
# and is_trusted() answers False for a project that WAS trusted, which
|
|
400
|
+
# is exactly the state this distinction exists to tell apart.
|
|
401
|
+
was_trusted = workspace_trust.is_trusted(root)
|
|
402
|
+
|
|
403
|
+
granted = lambda _name, _params: True # noqa: E731 -- consent is above
|
|
404
|
+
written = []
|
|
405
|
+
made = []
|
|
406
|
+
|
|
407
|
+
failure = None
|
|
408
|
+
try:
|
|
409
|
+
if scaffold_docs:
|
|
410
|
+
facts = manifest_mod.detect_facts(root)
|
|
411
|
+
if diff:
|
|
412
|
+
_write(registry, granted, HUB_FILENAME, body)
|
|
413
|
+
written.append(HUB_FILENAME)
|
|
414
|
+
for name in to_create:
|
|
415
|
+
_write(registry, granted, name,
|
|
416
|
+
doc_sets.render_stub(name, facts))
|
|
417
|
+
written.append(name)
|
|
418
|
+
# The directories first, so a failure to write settings.json still
|
|
419
|
+
# leaves a `.venastine/` whose shape says where the rest goes.
|
|
420
|
+
for name in plan["dirs"]:
|
|
421
|
+
os.makedirs(
|
|
422
|
+
os.path.join(workspace_trust.venastine_dir(root), name),
|
|
423
|
+
exist_ok=True)
|
|
424
|
+
made.append(".venastine/" + name + "/")
|
|
425
|
+
for name in plan["files"]:
|
|
426
|
+
_write_config(root, name)
|
|
427
|
+
written.append(".venastine/" + name)
|
|
428
|
+
except OSError as e:
|
|
429
|
+
failure = f"Could not create a directory under .venastine/: {e}"
|
|
430
|
+
except ToolCallDenied as e:
|
|
431
|
+
failure = f"The write was denied: {e}"
|
|
432
|
+
except InitError as e:
|
|
433
|
+
# THE REACHABLE FAILURE, and the one that had no handler (#95).
|
|
434
|
+
# `ToolCallDenied` cannot arrive here -- consent is granted a few
|
|
435
|
+
# lines up -- while `_write` raises `InitError` for every error
|
|
436
|
+
# dict `write_run` returns, and `write_run` turns ANY OSError into
|
|
437
|
+
# one. Disk full, permissions, a read-only mount: all of them took
|
|
438
|
+
# this path, and this path did not exist.
|
|
439
|
+
failure = str(e)
|
|
440
|
+
|
|
441
|
+
# ---- 5. Trust ------------------------------------------------------
|
|
442
|
+
# ON BOTH PATHS (#95). The partial write has already moved the D17
|
|
443
|
+
# content hash, so a project the user trusted a moment ago is untrusted
|
|
444
|
+
# now -- exactly the state I6 exists to prevent, reached by the route
|
|
445
|
+
# I6 was not looking at. Its precondition is unchanged by the failure:
|
|
446
|
+
# they just authored this content, and that is as true after five of
|
|
447
|
+
# eight writes as after eight. Skipping this made the failure wrong
|
|
448
|
+
# about two things instead of one, and silent about the second.
|
|
449
|
+
trust_note = _settle_trust(root, was_trusted)
|
|
450
|
+
|
|
451
|
+
if failure is not None:
|
|
452
|
+
# NAME WHAT WAS CREATED. The success path reports the files so the
|
|
453
|
+
# user knows what changed; the failure path reported the single
|
|
454
|
+
# file that did NOT change -- the one thing they could already see
|
|
455
|
+
# -- and discarded the list of the ones that did. "Could not write
|
|
456
|
+
# TECHNICAL_DEBT.md" left no way to tell whether nothing had
|
|
457
|
+
# happened or most of it had.
|
|
458
|
+
#
|
|
459
|
+
# NOTHING IS ROLLED BACK, deliberately. The hub's previous
|
|
460
|
+
# content was overwritten at the first step and cannot be restored,
|
|
461
|
+
# so "rolled back" would be true of the stubs and false of the one
|
|
462
|
+
# file that mattered -- and deleting documents out of someone's
|
|
463
|
+
# project on an error path is a worse failure than the one being
|
|
464
|
+
# reported.
|
|
465
|
+
lines = [failure]
|
|
466
|
+
if written:
|
|
467
|
+
lines.append(f"Wrote {len(written)} file"
|
|
468
|
+
f"{'' if len(written) == 1 else 's'} before that: "
|
|
469
|
+
+ ", ".join(written))
|
|
470
|
+
else:
|
|
471
|
+
lines.append("Nothing was written.")
|
|
472
|
+
lines.append(trust_note)
|
|
473
|
+
raise InitError("\n".join(line for line in lines if line))
|
|
474
|
+
|
|
475
|
+
lines = [f"Wrote {len(written)} file{'' if len(written) == 1 else 's'}: "
|
|
476
|
+
+ ", ".join(written)]
|
|
477
|
+
if made:
|
|
478
|
+
lines.append("Created: " + ", ".join(made))
|
|
479
|
+
if already_there:
|
|
480
|
+
lines.append("Left alone: " + ", ".join(sorted(already_there)))
|
|
481
|
+
if plan["existing"]:
|
|
482
|
+
lines.append("Left alone: "
|
|
483
|
+
+ ", ".join(".venastine/" + name
|
|
484
|
+
for name in plan["existing"]))
|
|
485
|
+
lines.append(trust_note)
|
|
486
|
+
return {"kind": "init", "text": "\n".join(line for line in lines if line)}
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
def _nothing_to_do(kind, scaffold_docs: bool, scaffold_config: bool) -> str:
|
|
490
|
+
"""The message for a run that found everything already in place.
|
|
491
|
+
|
|
492
|
+
Assembled from the halves that actually ran, so a `--config` run in a
|
|
493
|
+
configured project does not report on a document set it never looked
|
|
494
|
+
at -- which is the same complaint #95 makes about a failure naming
|
|
495
|
+
the one file that did NOT change.
|
|
496
|
+
"""
|
|
497
|
+
from tools.builtin.project_docs import HUB_FILENAME
|
|
498
|
+
|
|
499
|
+
parts = []
|
|
500
|
+
if scaffold_docs:
|
|
501
|
+
parts.append(f"{HUB_FILENAME} is already up to date and every "
|
|
502
|
+
f"document in the {kind} set exists")
|
|
503
|
+
if scaffold_config:
|
|
504
|
+
parts.append(".venastine/ already has its settings.json and "
|
|
505
|
+
"mcp.json")
|
|
506
|
+
return " and ".join(parts) + ". Nothing to do."
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
def _consent_summary(context_path: str, to_create: list, changing: bool,
|
|
510
|
+
plan: Optional[dict] = None) -> str:
|
|
511
|
+
"""ONE consent covering everything the run will touch (I5).
|
|
512
|
+
|
|
513
|
+
The configuration files are named in it rather than asked about
|
|
514
|
+
separately: eight prompts for one command is the shape of consent that
|
|
515
|
+
gets clicked through, which is the reason this summary exists at all.
|
|
516
|
+
"""
|
|
517
|
+
plan = plan or {"files": [], "dirs": []}
|
|
518
|
+
parts = []
|
|
519
|
+
if changing:
|
|
520
|
+
parts.append(f"write {context_path}")
|
|
521
|
+
if to_create:
|
|
522
|
+
parts.append(f"create {len(to_create)} stub document"
|
|
523
|
+
f"{'' if len(to_create) == 1 else 's'} "
|
|
524
|
+
f"({', '.join(to_create)})")
|
|
525
|
+
if plan["files"]:
|
|
526
|
+
parts.append("create " + ", ".join(".venastine/" + name
|
|
527
|
+
for name in plan["files"])
|
|
528
|
+
+ " with default values")
|
|
529
|
+
if plan["dirs"]:
|
|
530
|
+
parts.append("create " + ", ".join(".venastine/" + name + "/"
|
|
531
|
+
for name in plan["dirs"]))
|
|
532
|
+
return "/init will " + " and ".join(parts) + ". Proceed?"
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
def _write(registry, granted, name: str, content: str) -> None:
|
|
536
|
+
result = registry.dispatch(
|
|
537
|
+
"write_project_doc", {"name": name, "content": content},
|
|
538
|
+
approval_callback=granted)
|
|
539
|
+
if isinstance(result, dict) and result.get("error"):
|
|
540
|
+
raise InitError(result["error"])
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
def _settle_trust(project_path: str, was_trusted: bool) -> str:
|
|
544
|
+
"""I6. Re-grant only for a project that was ALREADY trusted.
|
|
545
|
+
|
|
546
|
+
Writing the hub changes the D17 content hash, so a project the user
|
|
547
|
+
had approved becomes untrusted purely because they ran this command --
|
|
548
|
+
the trust system working as designed, producing a bad outcome. Since
|
|
549
|
+
they just authored the content, re-granting is unambiguous.
|
|
550
|
+
|
|
551
|
+
But ONLY then. In an untrusted project, `.venastine/` may already hold
|
|
552
|
+
agents, skills and an mcp.json that arrived with a repo someone cloned.
|
|
553
|
+
Granting trust as a side effect of /init would launder all of it in --
|
|
554
|
+
a hole in D17 opened from a direction the trust prompt never sees.
|
|
555
|
+
The hub is never special-cased out of the hash either; that would be
|
|
556
|
+
the same hole with a smaller mouth.
|
|
557
|
+
"""
|
|
558
|
+
if not was_trusted:
|
|
559
|
+
return ("This project is not trusted, so nothing here has changed "
|
|
560
|
+
"that. You will be asked about its .venastine/ content on "
|
|
561
|
+
"the next launch.")
|
|
562
|
+
workspace_trust.grant_trust(project_path)
|
|
563
|
+
return ("Workspace trust re-granted for the updated .venastine/ content, "
|
|
564
|
+
"so the next launch will not re-ask.")
|