@yahyahammad/venastine 0.2.0 → 0.3.8

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 (129) hide show
  1. package/.env.example +19 -14
  2. package/CONFIG_ARCHITECTURE.md +2002 -0
  3. package/NOTICE +1 -1
  4. package/README.md +233 -94
  5. package/agents/builtin/build.md +73 -0
  6. package/agents/builtin/general.md +80 -0
  7. package/agents/builtin/plan.md +23 -5
  8. package/agents/builtin/test.md +65 -0
  9. package/agents/builtin/writer.md +67 -0
  10. package/agents/manager.py +261 -195
  11. package/agents/subagent_tool.py +277 -43
  12. package/agents/tui_commands.py +7 -12
  13. package/bin/venastine.mjs +528 -382
  14. package/bin/venastine.py +71 -0
  15. package/cli_commands.py +835 -0
  16. package/config.py +95 -1198
  17. package/config.yaml +1090 -0
  18. package/config_edit.py +1301 -0
  19. package/config_schema.py +966 -0
  20. package/config_update.py +749 -0
  21. package/core/agent_activity.py +158 -20
  22. package/core/ansi.py +452 -0
  23. package/core/approval.py +27 -5
  24. package/core/call_view.py +364 -0
  25. package/core/cancel.py +121 -0
  26. package/core/chatgpt_auth.py +747 -0
  27. package/core/claims_view.py +107 -0
  28. package/core/client.py +1238 -82
  29. package/core/commands.py +454 -0
  30. package/core/compaction.py +830 -66
  31. package/core/config_loader.py +516 -97
  32. package/core/connect.py +686 -0
  33. package/core/events.py +68 -6
  34. package/core/file_mentions.py +280 -0
  35. package/core/interaction.py +87 -4
  36. package/core/line_pattern.py +128 -0
  37. package/core/loop.py +1516 -440
  38. package/core/memory.py +120 -8
  39. package/core/pipeline_models.py +23 -9
  40. package/core/provider_errors.py +234 -0
  41. package/core/reasoning/confidence_scoring.py +70 -54
  42. package/core/reasoning/json_retry.py +19 -1
  43. package/core/reasoning/orchestrator.py +232 -53
  44. package/core/reasoning/output_writer.py +90 -31
  45. package/core/reasoning/payload_validation.py +133 -7
  46. package/core/reasoning/pipeline_storage.py +427 -361
  47. package/core/reasoning/review.py +659 -615
  48. package/core/reasoning/scholar.py +38 -14
  49. package/core/reasoning/source_corpus.py +110 -4
  50. package/core/replay.py +158 -26
  51. package/core/session.py +36 -12
  52. package/core/session_view.py +259 -0
  53. package/core/session_wake.py +259 -0
  54. package/core/shell_sessions.py +1553 -0
  55. package/core/workspace_trust.py +1 -1
  56. package/credentials.py +308 -180
  57. package/database.py +82 -1
  58. package/{PRIVACY.md → docs/PRIVACY.md} +19 -7
  59. package/docs/SECURITY.md +108 -0
  60. package/docs/THIRD_PARTY_NOTICES.md +148 -0
  61. package/env_secrets.py +118 -77
  62. package/json_store.py +236 -16
  63. package/logging_setup.py +121 -11
  64. package/main.py +1671 -199
  65. package/mcp_client/client.py +257 -23
  66. package/mcp_client/config.py +484 -379
  67. package/mcp_client/registration.py +163 -2
  68. package/memories/manager.py +15 -0
  69. package/memories/tui_commands.py +7 -12
  70. package/package.json +234 -4
  71. package/project_init/config_files.py +4 -0
  72. package/project_init/generator.py +201 -37
  73. package/project_init/manifest.py +418 -406
  74. package/project_init/tui_commands.py +7 -4
  75. package/prompts/revalidate.md +1 -1
  76. package/prompts/revise.md +17 -15
  77. package/prompts/system_prompts.py +64 -10
  78. package/providers.json.example +94 -92
  79. package/pyproject.toml +51 -2
  80. package/requirements.txt +252 -29
  81. package/safety/policy_enforcement.py +353 -17
  82. package/security/askpass.py +419 -0
  83. package/security/capability.py +37 -7
  84. package/security/confine.py +484 -0
  85. package/security/hosted.py +182 -0
  86. package/security/permissions.py +54 -7
  87. package/security/posture.py +124 -12
  88. package/security/protected_paths.py +198 -6
  89. package/security/sandbox.py +3262 -139
  90. package/security/secrets.py +855 -0
  91. package/skills/tui_commands.py +3 -4
  92. package/storage.py +368 -35
  93. package/tools/base.py +465 -305
  94. package/tools/builtin/_math_common.py +306 -16
  95. package/tools/builtin/_net_common.py +267 -61
  96. package/tools/builtin/arxiv.py +681 -223
  97. package/tools/builtin/ask_user.py +231 -213
  98. package/tools/builtin/discrete_math.py +35 -4
  99. package/tools/builtin/fetch_url.py +239 -21
  100. package/tools/builtin/file_ops.py +341 -33
  101. package/tools/builtin/geometry.py +184 -148
  102. package/tools/builtin/get_time.py +2 -2
  103. package/tools/builtin/linear_algebra.py +66 -23
  104. package/tools/builtin/load_skill.py +10 -0
  105. package/tools/builtin/logic.py +171 -12
  106. package/tools/builtin/pin.py +46 -12
  107. package/tools/builtin/probability_stats.py +224 -228
  108. package/tools/builtin/project_docs.py +80 -21
  109. package/tools/builtin/remember.py +21 -4
  110. package/tools/builtin/shell.py +586 -67
  111. package/tools/builtin/shell_sessions.py +613 -0
  112. package/tools/builtin/symbolic_math.py +80 -10
  113. package/tools/builtin/todo.py +5 -3
  114. package/tools/builtin/web_search.py +329 -151
  115. package/tools/context.py +23 -0
  116. package/tools/isolation.py +387 -314
  117. package/tools/registry.py +347 -30
  118. package/tui/app.py +4645 -438
  119. package/tui/app.tcss +185 -2
  120. package/tui/commands.py +10 -154
  121. package/tui/diffs.py +286 -265
  122. package/tui/hosted_driver.py +102 -0
  123. package/tui/markdown.py +60 -10
  124. package/tui/meters.py +103 -7
  125. package/tui/screens.py +346 -92
  126. package/tui/themes.py +59 -19
  127. package/tui/widgets.py +1575 -111
  128. package/SECURITY.md +0 -88
  129. package/THIRD_PARTY_NOTICES.md +0 -100
@@ -1,228 +1,224 @@
1
- """
2
- tools/builtin/probability_stats.py
3
-
4
- Descriptive statistics on raw data (using the stdlib `statistics` module
5
- -- deterministic, no need for sympy here) and probability distributions
6
- (using sympy.stats, kept exact/symbolic where possible).
7
-
8
- Named probability_stats rather than statistics.py specifically to avoid
9
- any reader confusion with the stdlib module of the same name, even though
10
- Python 3's absolute-import default would resolve `import statistics`
11
- correctly regardless.
12
- """
13
-
14
- from __future__ import annotations
15
-
16
- import statistics as pystats
17
- from typing import Literal, Optional
18
-
19
- import sympy
20
- import sympy.stats as sstats
21
- from pydantic import BaseModel, Field
22
-
23
- _OPERATIONS = Literal["describe", "distribution_stats", "distribution_pdf", "distribution_cdf"]
24
- _DISTRIBUTIONS = Literal["normal", "binomial", "poisson", "uniform", "exponential"]
25
-
26
-
27
- class ProbabilityStatsParams(BaseModel):
28
- operation: _OPERATIONS = Field(..., description="Which operation to perform")
29
- data: Optional[list[float]] = Field(None, description="Dataset, for the 'describe' operation")
30
- distribution: Optional[_DISTRIBUTIONS] = Field(
31
- None, description="Distribution family, for distribution_* operations"
32
- )
33
- distribution_params: Optional[dict[str, float]] = Field(
34
- None,
35
- description=(
36
- "Parameters for the chosen distribution: normal={mu, sigma}, "
37
- "binomial={n, p}, poisson={lambda}, uniform={a, b}, "
38
- "exponential={rate}"
39
- ),
40
- )
41
- x: Optional[float] = Field(None, description="Point to evaluate pdf/cdf at")
42
- evaluate: bool = Field(False, description="If true, return numeric approximation (evalf) instead of exact symbolic form (e.g. 0.84... instead of erf(...)). Default false keeps exact symbolic.")
43
- precision: int = Field(15, ge=1, le=50, description="Significant digits when evaluate is true")
44
-
45
-
46
- TOOL_SCHEMA = {
47
- "name": "probability_stats",
48
- "description": (
49
- "Compute descriptive statistics on a dataset (mean, median, "
50
- "variance, stdev, min, max), or work with probability "
51
- "distributions (normal, binomial, poisson, uniform, exponential): "
52
- "expected value, variance, pdf at a point, or cdf at a point. "
53
- "Results are exact symbolic expressions (e.g. erf(...), sqrt(pi)) unless 'evaluate' is true, "
54
- "which returns a numeric approximation with 'precision' digits. When distribution parameters "
55
- "are omitted, defaults are used (normal mu=0 sigma=1, uniform a=0 b=1, exponential rate=1) and a "
56
- "'warnings' field explains what was defaulted."
57
- ),
58
- "input_schema": ProbabilityStatsParams.model_json_schema(),
59
- }
60
-
61
-
62
- def _build_distribution(name: str, dp: dict):
63
- if name == "normal":
64
- return sstats.Normal("X", dp.get("mu", 0), dp.get("sigma", 1))
65
- if name == "binomial":
66
- return sstats.Binomial("X", int(dp["n"]), dp["p"])
67
- if name == "poisson":
68
- return sstats.Poisson("X", dp["lambda"])
69
- if name == "uniform":
70
- return sstats.Uniform("X", dp.get("a", 0), dp.get("b", 1))
71
- if name == "exponential":
72
- return sstats.Exponential("X", dp.get("rate", 1))
73
- raise ValueError(f"Unsupported distribution: {name}")
74
-
75
-
76
- def _build_distribution_with_warnings(name: str, dp_orig: Optional[dict]):
77
- """Build distribution and collect warnings for defaulted params.
78
-
79
- Returns (dist, warnings). Warnings is a list of strings for each param
80
- that fell back to a default, per decision 2 (keep forgiving but explain).
81
- Binomial/poisson have no defaults — missing required keys become a
82
- friendly error at the caller, not a warning.
83
- """
84
- dp = dp_orig or {}
85
- warnings: list[str] = []
86
- if name == "normal":
87
- if "mu" not in dp:
88
- warnings.append("mu not supplied - defaulted to 0")
89
- if "sigma" not in dp:
90
- warnings.append("sigma not supplied - defaulted to 1")
91
- return sstats.Normal("X", dp.get("mu", 0), dp.get("sigma", 1)), warnings
92
- if name == "uniform":
93
- if "a" not in dp:
94
- warnings.append("a not supplied - defaulted to 0")
95
- if "b" not in dp:
96
- warnings.append("b not supplied - defaulted to 1")
97
- return sstats.Uniform("X", dp.get("a", 0), dp.get("b", 1)), warnings
98
- if name == "exponential":
99
- if "rate" not in dp:
100
- warnings.append("rate not supplied - defaulted to 1")
101
- return sstats.Exponential("X", dp.get("rate", 1)), warnings
102
- if name == "binomial":
103
- # No defaults — caller validates presence
104
- return sstats.Binomial("X", int(dp["n"]), dp["p"]), warnings
105
- if name == "poisson":
106
- return sstats.Poisson("X", dp["lambda"]), warnings
107
- raise ValueError(f"Unsupported distribution: {name}")
108
-
109
-
110
- def _maybe_evalf(value, do_eval: bool, precision: int):
111
- if not do_eval or value is None:
112
- return value
113
- try:
114
- if hasattr(value, "evalf"):
115
- return value.evalf(precision)
116
- except Exception:
117
- pass
118
- return value
119
-
120
-
121
- def _cdf_at(dist, x):
122
- """Evaluate CDF at x, handling discrete binomial's dict return."""
123
- try:
124
- return sstats.cdf(dist)(x)
125
- except TypeError as e:
126
- if "'dict' object is not callable" in str(e):
127
- # Binomial (and some discretes) return a dict table via two-arg form
128
- table = sstats.cdf(dist, x)
129
- if isinstance(table, dict):
130
- import math
131
-
132
- try:
133
- # Discrete CDF at x is sum up to floor(x)
134
- key = int(math.floor(float(x)))
135
- except Exception:
136
- return table
137
- if key in table:
138
- return table[key]
139
- # find greatest key <= x
140
- sorted_keys = sorted(table.keys())
141
- candidate = None
142
- for k in sorted_keys:
143
- if k <= key:
144
- candidate = table[k]
145
- else:
146
- break
147
- if candidate is not None:
148
- return candidate
149
- if key < sorted_keys[0]:
150
- return sympy.Integer(0)
151
- return sympy.Integer(1)
152
- return table
153
- raise
154
-
155
-
156
- def run(params: dict) -> dict:
157
- parsed = ProbabilityStatsParams(**params)
158
-
159
- try:
160
- if parsed.operation == "describe":
161
- data = parsed.data
162
- if not data:
163
- return {"error": "data is required for 'describe'"}
164
- result = {
165
- "mean": pystats.mean(data),
166
- "median": pystats.median(data),
167
- "stdev": pystats.stdev(data) if len(data) > 1 else 0.0,
168
- "variance": pystats.variance(data) if len(data) > 1 else 0.0,
169
- "min": min(data),
170
- "max": max(data),
171
- "n": len(data),
172
- }
173
- return {"result": result, "operation": parsed.operation}
174
-
175
- if not parsed.distribution:
176
- return {"error": "distribution is required for this operation"}
177
-
178
- dp = parsed.distribution_params or {}
179
- # Friendly validation for distributions with no defaults
180
- if parsed.distribution == "binomial":
181
- if "n" not in dp or "p" not in dp:
182
- return {"error": "binomial requires 'n' (number of trials) and 'p' (success probability) in distribution_params."}
183
- if parsed.distribution == "poisson":
184
- if "lambda" not in dp:
185
- return {"error": "poisson requires 'lambda' in distribution_params."}
186
-
187
- dist, warnings = _build_distribution_with_warnings(parsed.distribution, parsed.distribution_params)
188
-
189
- if parsed.operation == "distribution_stats":
190
- mean_v = _maybe_evalf(sstats.E(dist), parsed.evaluate, parsed.precision)
191
- var_v = _maybe_evalf(sstats.variance(dist), parsed.evaluate, parsed.precision)
192
- # std is sqrt(var) — eval the sqrt when numeric
193
- raw_var = sstats.variance(dist)
194
- std_v = _maybe_evalf(sympy.sqrt(raw_var), parsed.evaluate, parsed.precision)
195
- result = {
196
- "mean": str(mean_v),
197
- "variance": str(var_v),
198
- "std": str(std_v),
199
- }
200
- elif parsed.operation == "distribution_pdf":
201
- if parsed.x is None:
202
- return {"error": "x is required for distribution_pdf"}
203
- x_arg = parsed.x
204
- # Discrete distributions expect int; float 5.0 should be treated as 5
205
- if parsed.distribution in ("binomial", "poisson") and isinstance(x_arg, float) and x_arg.is_integer():
206
- x_arg = int(x_arg)
207
- raw = sstats.density(dist)(x_arg)
208
- val = _maybe_evalf(raw, parsed.evaluate, parsed.precision)
209
- result = str(val)
210
- elif parsed.operation == "distribution_cdf":
211
- if parsed.x is None:
212
- return {"error": "x is required for distribution_cdf"}
213
- x_arg = parsed.x
214
- if parsed.distribution in ("binomial", "poisson") and isinstance(x_arg, float) and x_arg.is_integer():
215
- x_arg = int(x_arg)
216
- raw = _cdf_at(dist, x_arg)
217
- val = _maybe_evalf(raw, parsed.evaluate, parsed.precision)
218
- result = str(val)
219
- else:
220
- return {"error": f"Unknown operation: {parsed.operation}"}
221
-
222
- except Exception as e:
223
- return {"error": f"Computation failed: {e}"}
224
-
225
- out: dict = {"result": result, "operation": parsed.operation}
226
- if warnings:
227
- out["warnings"] = warnings
228
- return out
1
+ """
2
+ tools/builtin/probability_stats.py
3
+
4
+ Descriptive statistics on raw data (using the stdlib `statistics` module
5
+ -- deterministic, no need for sympy here) and probability distributions
6
+ (using sympy.stats, kept exact/symbolic where possible).
7
+
8
+ Named probability_stats rather than statistics.py specifically to avoid
9
+ any reader confusion with the stdlib module of the same name, even though
10
+ Python 3's absolute-import default would resolve `import statistics`
11
+ correctly regardless.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import statistics as pystats
17
+ from typing import Literal, Optional
18
+
19
+ import sympy
20
+ import sympy.stats as sstats
21
+ from pydantic import BaseModel, Field
22
+
23
+ from tools.builtin._math_common import MathParseError, parse_params
24
+
25
+ _OPERATIONS = Literal["describe", "distribution_stats", "distribution_pdf", "distribution_cdf"]
26
+ _DISTRIBUTIONS = Literal["normal", "binomial", "poisson", "uniform", "exponential"]
27
+
28
+
29
+ class ProbabilityStatsParams(BaseModel):
30
+ operation: _OPERATIONS = Field(..., description="Which operation to perform")
31
+ data: Optional[list[float]] = Field(None, description="Dataset, for the 'describe' operation")
32
+ distribution: Optional[_DISTRIBUTIONS] = Field(
33
+ None, description="Distribution family, for distribution_* operations"
34
+ )
35
+ distribution_params: Optional[dict[str, float]] = Field(
36
+ None,
37
+ description=(
38
+ "Parameters for the chosen distribution: normal={mu, sigma}, "
39
+ "binomial={n, p}, poisson={lambda}, uniform={a, b}, "
40
+ "exponential={rate}"
41
+ ),
42
+ )
43
+ x: Optional[float] = Field(None, description="Point to evaluate pdf/cdf at")
44
+ evaluate: bool = Field(False, description="If true, return numeric approximation (evalf) instead of exact symbolic form (e.g. 0.84... instead of erf(...)). Default false keeps exact symbolic.")
45
+ precision: int = Field(15, ge=1, le=50, description="Significant digits when evaluate is true")
46
+
47
+
48
+ TOOL_SCHEMA = {
49
+ "name": "probability_stats",
50
+ "description": (
51
+ "Compute descriptive statistics on a dataset (mean, median, "
52
+ "variance, stdev, min, max), or work with probability "
53
+ "distributions (normal, binomial, poisson, uniform, exponential): "
54
+ "expected value, variance, pdf at a point, or cdf at a point. "
55
+ "Results are exact symbolic expressions (e.g. erf(...), sqrt(pi)) unless 'evaluate' is true, "
56
+ "which returns a numeric approximation with 'precision' digits. When distribution parameters "
57
+ "are omitted, defaults are used (normal mu=0 sigma=1, uniform a=0 b=1, exponential rate=1) and a "
58
+ "'warnings' field explains what was defaulted."
59
+ ),
60
+ "input_schema": ProbabilityStatsParams.model_json_schema(),
61
+ }
62
+
63
+
64
+ def _build_distribution_with_warnings(name: str, dp_orig: Optional[dict]):
65
+ """Build distribution and collect warnings for defaulted params.
66
+
67
+ Returns (dist, warnings). Warnings is a list of strings for each param
68
+ that fell back to a default, per decision 2 (keep forgiving but explain).
69
+ Binomial/poisson have no defaults — missing required keys become a
70
+ friendly error at the caller, not a warning.
71
+
72
+ THE ONE BUILDER (#208). A warning-free twin, `_build_distribution`, sat
73
+ above this with no caller and the same five distributions and defaults,
74
+ so a change to one would have left the other silently wrong. It is
75
+ deleted; a caller that wants no warnings takes `[0]` of this.
76
+ """
77
+ dp = dp_orig or {}
78
+ warnings: list[str] = []
79
+ if name == "normal":
80
+ if "mu" not in dp:
81
+ warnings.append("mu not supplied - defaulted to 0")
82
+ if "sigma" not in dp:
83
+ warnings.append("sigma not supplied - defaulted to 1")
84
+ return sstats.Normal("X", dp.get("mu", 0), dp.get("sigma", 1)), warnings
85
+ if name == "uniform":
86
+ if "a" not in dp:
87
+ warnings.append("a not supplied - defaulted to 0")
88
+ if "b" not in dp:
89
+ warnings.append("b not supplied - defaulted to 1")
90
+ return sstats.Uniform("X", dp.get("a", 0), dp.get("b", 1)), warnings
91
+ if name == "exponential":
92
+ if "rate" not in dp:
93
+ warnings.append("rate not supplied - defaulted to 1")
94
+ return sstats.Exponential("X", dp.get("rate", 1)), warnings
95
+ if name == "binomial":
96
+ # No defaults — caller validates presence
97
+ return sstats.Binomial("X", int(dp["n"]), dp["p"]), warnings
98
+ if name == "poisson":
99
+ return sstats.Poisson("X", dp["lambda"]), warnings
100
+ raise ValueError(f"Unsupported distribution: {name}")
101
+
102
+
103
+ def _maybe_evalf(value, do_eval: bool, precision: int):
104
+ if not do_eval or value is None:
105
+ return value
106
+ try:
107
+ if hasattr(value, "evalf"):
108
+ return value.evalf(precision)
109
+ except Exception:
110
+ pass # no numeric form; the exact value is still a correct answer
111
+ return value
112
+
113
+
114
+ def _cdf_at(dist, x):
115
+ """Evaluate CDF at x, handling discrete binomial's dict return."""
116
+ try:
117
+ return sstats.cdf(dist)(x)
118
+ except TypeError as e:
119
+ if "'dict' object is not callable" in str(e):
120
+ # Binomial (and some discretes) return a dict table via two-arg form
121
+ table = sstats.cdf(dist, x)
122
+ if isinstance(table, dict):
123
+ import math
124
+
125
+ try:
126
+ # Discrete CDF at x is sum up to floor(x)
127
+ key = int(math.floor(float(x)))
128
+ except Exception:
129
+ return table
130
+ if key in table:
131
+ return table[key]
132
+ # find greatest key <= x
133
+ sorted_keys = sorted(table.keys())
134
+ candidate = None
135
+ for k in sorted_keys:
136
+ if k <= key:
137
+ candidate = table[k]
138
+ else:
139
+ break
140
+ if candidate is not None:
141
+ return candidate
142
+ if key < sorted_keys[0]:
143
+ return sympy.Integer(0)
144
+ return sympy.Integer(1)
145
+ return table
146
+ raise
147
+
148
+
149
+ def run(params: dict) -> dict:
150
+ try:
151
+ parsed = parse_params(ProbabilityStatsParams, params)
152
+ except MathParseError as e:
153
+ return {"error": str(e)}
154
+
155
+ try:
156
+ if parsed.operation == "describe":
157
+ data = parsed.data
158
+ if not data:
159
+ return {"error": "data is required for 'describe'"}
160
+ result = {
161
+ "mean": pystats.mean(data),
162
+ "median": pystats.median(data),
163
+ "stdev": pystats.stdev(data) if len(data) > 1 else 0.0,
164
+ "variance": pystats.variance(data) if len(data) > 1 else 0.0,
165
+ "min": min(data),
166
+ "max": max(data),
167
+ "n": len(data),
168
+ }
169
+ return {"result": result, "operation": parsed.operation}
170
+
171
+ if not parsed.distribution:
172
+ return {"error": "distribution is required for this operation"}
173
+
174
+ dp = parsed.distribution_params or {}
175
+ # Friendly validation for distributions with no defaults
176
+ if parsed.distribution == "binomial":
177
+ if "n" not in dp or "p" not in dp:
178
+ return {"error": "binomial requires 'n' (number of trials) and 'p' (success probability) in distribution_params."}
179
+ if parsed.distribution == "poisson":
180
+ if "lambda" not in dp:
181
+ return {"error": "poisson requires 'lambda' in distribution_params."}
182
+
183
+ dist, warnings = _build_distribution_with_warnings(parsed.distribution, parsed.distribution_params)
184
+
185
+ if parsed.operation == "distribution_stats":
186
+ mean_v = _maybe_evalf(sstats.E(dist), parsed.evaluate, parsed.precision)
187
+ var_v = _maybe_evalf(sstats.variance(dist), parsed.evaluate, parsed.precision)
188
+ # std is sqrt(var) — eval the sqrt when numeric
189
+ raw_var = sstats.variance(dist)
190
+ std_v = _maybe_evalf(sympy.sqrt(raw_var), parsed.evaluate, parsed.precision)
191
+ result = {
192
+ "mean": str(mean_v),
193
+ "variance": str(var_v),
194
+ "std": str(std_v),
195
+ }
196
+ elif parsed.operation == "distribution_pdf":
197
+ if parsed.x is None:
198
+ return {"error": "x is required for distribution_pdf"}
199
+ x_arg = parsed.x
200
+ # Discrete distributions expect int; float 5.0 should be treated as 5
201
+ if parsed.distribution in ("binomial", "poisson") and isinstance(x_arg, float) and x_arg.is_integer():
202
+ x_arg = int(x_arg)
203
+ raw = sstats.density(dist)(x_arg)
204
+ val = _maybe_evalf(raw, parsed.evaluate, parsed.precision)
205
+ result = str(val)
206
+ elif parsed.operation == "distribution_cdf":
207
+ if parsed.x is None:
208
+ return {"error": "x is required for distribution_cdf"}
209
+ x_arg = parsed.x
210
+ if parsed.distribution in ("binomial", "poisson") and isinstance(x_arg, float) and x_arg.is_integer():
211
+ x_arg = int(x_arg)
212
+ raw = _cdf_at(dist, x_arg)
213
+ val = _maybe_evalf(raw, parsed.evaluate, parsed.precision)
214
+ result = str(val)
215
+ else:
216
+ return {"error": f"Unknown operation: {parsed.operation}"}
217
+
218
+ except Exception as e:
219
+ return {"error": f"Computation failed: {e}"}
220
+
221
+ out: dict = {"result": result, "operation": parsed.operation}
222
+ if warnings:
223
+ out["warnings"] = warnings
224
+ return out
@@ -52,13 +52,25 @@ right for `packages/foo/package.json` in a monorepo and wrong for
52
52
  source lives, and it is the one place these five are not this project's own
53
53
  statement about itself.
54
54
 
55
- THE RESIDUAL, STATED RATHER THAN LEFT TO BE FOUND: `check_output_policy`
56
- scans every value (#47), so `sk-ant-...`, `ghp_...`, `AKIA...` and a PEM
57
- header are redacted out of a result. A plaintext `<password>` element, a
58
- Gradle `password "..."` and a `user:token@host` URL are NOT, and those are
59
- the credential shapes that occur in exactly these five files. Filed against
60
- the redactor rather than fixed here, because a pattern added there redacts
61
- content out of every tool result in the harness, not only this one.
55
+ THE RESIDUAL THAT WAS STATED HERE IS CLOSED, and what is left of the
56
+ paragraph is the part that was always the point. `check_output_policy` scans
57
+ every value (#47), so `sk-ant-...`, `ghp_...`, `AKIA...` and a PEM header
58
+ are redacted out of a result. A plaintext `<password>` element, a Gradle
59
+ `password "..."` and a `user:token@host` URL were NOT, and those are the
60
+ credential shapes that occur in exactly these five files -- so they were
61
+ filed against the REDACTOR rather than fixed here, because a pattern added
62
+ there redacts content out of every tool result in the harness and one added
63
+ here would serve one tool.
64
+
65
+ That filing is #167, closed 2026-08-23: `safety/policy_enforcement.py` now
66
+ carries `_URL_USERINFO_RE`, `_XML_PASSWORD_RE` and `_ASSIGNMENT_RE`, whose
67
+ `[ \t]` alternative is there for Gradle's space form specifically. All
68
+ three shapes are redacted out of a `read_project_doc` result today, driven
69
+ on all four file types, with a credential-free document passing through
70
+ byte-identical as the control. This paragraph went on claiming the leak for
71
+ seven weeks after the leak was fixed, which is why it now names the issue
72
+ and the patterns: a security residual that reads as OPEN costs every later
73
+ reader what it cost the reader who re-measured it (batch 146).
62
74
  """
63
75
 
64
76
  from __future__ import annotations
@@ -69,6 +81,7 @@ from typing import Optional
69
81
 
70
82
  import config
71
83
  from core import config_loader, workspace_trust
84
+ from json_store import write_text_atomic
72
85
  from security import protected_paths
73
86
 
74
87
  logger = logging.getLogger(__name__)
@@ -100,6 +113,13 @@ _ROOT_ONLY_FILENAMES = frozenset({
100
113
  _DENIED_SEGMENTS = (protected_paths.PROTECTED_SEGMENTS
101
114
  | {".git", "node_modules", ".venv", "venv",
102
115
  "__pycache__", ".pytest_cache"})
116
+
117
+ #: And compared the way the FILESYSTEM will compare them (batch 146),
118
+ #: through the one spelling of the fold that `protected_segment` uses. This
119
+ #: set unioned the segment list in and then compared it case-sensitively,
120
+ #: so the two consumers of one fact disagreed about the only thing left to
121
+ #: disagree about. `.VENASTINE/agents/foo.md` was a readable `.md`.
122
+ _DENIED_FOLDED = protected_paths.folded_segments(_DENIED_SEGMENTS)
103
123
  _DENIED_FILENAMES = frozenset({"providers.json"})
104
124
 
105
125
  #: Re-exported so callers name one constant. The name lives in
@@ -153,7 +173,7 @@ def _resolve_in_project(root: str, user_path: str) -> Optional[str]:
153
173
  def _is_readable_doc(root: str, resolved: str) -> bool:
154
174
  rel = os.path.relpath(resolved, root)
155
175
  parts = rel.split(os.sep)
156
- if any(segment in _DENIED_SEGMENTS for segment in parts):
176
+ if any(segment.casefold() in _DENIED_FOLDED for segment in parts):
157
177
  return False
158
178
  name = os.path.basename(resolved).lower()
159
179
  if name in _DENIED_FILENAMES or name.startswith(".env"):
@@ -239,9 +259,12 @@ def _skip(handle, offset: int) -> None:
239
259
 
240
260
 
241
261
  def read_run(params: dict) -> dict:
242
- # params.get throughout, not params[]: no provider validates tool inputs
243
- # against the schema, and a bare KeyError escapes the loop's
244
- # ToolCallDenied handler and aborts the whole turn (remember.py's note).
262
+ # params.get throughout, not params[]. tools/base.py carries the one
263
+ # copy of why, under "WHAT A CALLABLE REGISTERED BELOW MAY RAISE": a
264
+ # handler's exception IS contained, so the reason this is right is that
265
+ # an error dict the model can read beats the generic wrapper it would
266
+ # get -- not the turn-ending raise six comments in this directory used
267
+ # to claim (batch 146).
245
268
  root = _project_root()
246
269
  if root is None:
247
270
  return {"error": "read_project_doc: no project directory has been "
@@ -283,7 +306,7 @@ def read_run(params: dict) -> dict:
283
306
  # streams, so the offset is walked rather than jumped: correctness
284
307
  # over cleverness, and it still holds one chunk rather than the file.
285
308
  try:
286
- with open(resolved, "r", encoding="utf-8", errors="replace") as f:
309
+ with open(resolved, encoding="utf-8", errors="replace") as f:
287
310
  _skip(f, offset)
288
311
  chunk = f.read(config.INIT_READ_CHARS)
289
312
  more = f.read(1) != ""
@@ -361,10 +384,22 @@ def approval_notice(params: dict, context) -> str:
361
384
  """
362
385
  root = _project_root()
363
386
  name = params.get("name") or HUB_FILENAME
364
- where = doc_path(root, name) if root else name
365
387
  size = len(params.get("content") or "")
366
- verb = "Overwrite" if root and os.path.exists(where) else "Create"
367
- return f"{verb} {where} ({size} characters)."
388
+ if not root:
389
+ return f"Create {name} ({size} characters)."
390
+
391
+ # RESOLVED, so the consent can never name a file the bytes will not
392
+ # land on (batch 146). `_project_root()` is already a realpath, so for
393
+ # an ordinary document this is the same string the join produced and
394
+ # the prompt is unchanged; where it differs, the difference IS the
395
+ # thing the person needs to see.
396
+ resolved = _resolve_in_project(root, name)
397
+ if resolved is None:
398
+ return (f"REFUSE {doc_path(root, name)} ({size} characters): that "
399
+ f"name resolves outside the project, so write_project_doc "
400
+ f"will not write it.")
401
+ verb = "Overwrite" if os.path.exists(resolved) else "Create"
402
+ return f"{verb} {resolved} ({size} characters)."
368
403
 
369
404
 
370
405
  def write_run(params: dict) -> dict:
@@ -393,13 +428,37 @@ def write_run(params: dict) -> dict:
393
428
  f"maximum writable size "
394
429
  f"({config.MAX_FILE_SIZE_BYTES} bytes)."}
395
430
 
431
+ # RESOLVED BEFORE WRITING, which the read path has always done and this
432
+ # one did not (batch 146). The module's invariant is that the
433
+ # destination is derived from a name "so it cannot be aimed elsewhere",
434
+ # and a document name that is a LINK aims it elsewhere: a cloned project
435
+ # carrying `ARCHITECTURE.md -> ~/.bashrc` took the bytes to the link
436
+ # target, under a consent that named the in-project path. git stores
437
+ # symlinks natively, so the layout needs no privilege to arrive.
438
+ #
439
+ # An error rather than a coercion, in the sense the unknown-name branch
440
+ # above means: a name that leaves the project is not one of this
441
+ # project's documents.
396
442
  destination = doc_path(root, name)
443
+ resolved = _resolve_in_project(root, name)
444
+ if resolved is None:
445
+ logger.warning("write_project_doc refused %s: resolves outside the "
446
+ "project", destination)
447
+ return {"error": f"write_project_doc: {name!r} resolves outside the "
448
+ f"project directory, so it is not one of this "
449
+ f"project's documents."}
450
+
397
451
  try:
398
- os.makedirs(os.path.dirname(destination), exist_ok=True)
399
- with open(destination, "w", encoding="utf-8") as f:
400
- f.write(content)
452
+ # REPLACES THE NAME RATHER THAN WRITING THROUGH IT, which closes the
453
+ # half `realpath` cannot see: a hardlink is not a link to anything,
454
+ # so the resolution above admits one. And `newline=""`, so the bytes
455
+ # the model supplied are the bytes that land -- this tool is the one
456
+ # write path in the harness reachable with no config edit, and its
457
+ # whole purpose is to overwrite a document in place, under an
458
+ # approval that was shown a diff of the lines that changed.
459
+ write_text_atomic(resolved, content)
401
460
  except OSError as e:
402
- logger.warning("write_project_doc failed for %s: %s", destination, e)
403
- return {"error": f"Could not write {destination}: {e}"}
461
+ logger.warning("write_project_doc failed for %s: %s", resolved, e)
462
+ return {"error": f"Could not write {resolved}: {e}"}
404
463
 
405
- return {"result": f"Wrote {len(content)} characters to {destination}"}
464
+ return {"result": f"Wrote {len(content)} characters to {resolved}"}