@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,193 @@
|
|
|
1
|
+
"""
|
|
2
|
+
tools/builtin/pin.py
|
|
3
|
+
|
|
4
|
+
ROADMAP_v2 §21: mark recent turns compaction-exempt.
|
|
5
|
+
|
|
6
|
+
Thread-scoped and reversible, which is why D26 leaves it ungated while
|
|
7
|
+
requiring approval for §21b's `remember`. They differ on the axis that
|
|
8
|
+
already separates `read` from `write`: a wrong pin costs some context
|
|
9
|
+
budget inside a conversation the user is watching, while a durable memory
|
|
10
|
+
outlives the thread and silently shapes conversations that have not
|
|
11
|
+
started yet.
|
|
12
|
+
|
|
13
|
+
That decision has a consequence worth naming rather than discovering: this
|
|
14
|
+
tool works on the CLI and inside a research pass, where an approval-gated
|
|
15
|
+
tool is not merely denied but never advertised (§13).
|
|
16
|
+
|
|
17
|
+
THE INTERFACE IS ORDINAL, NOT AN ID. §21 keeps MessageLog.id out of the
|
|
18
|
+
provider-neutral message shape -- the model never sees one, so it cannot
|
|
19
|
+
name one -- and putting it there would mean a persistence concept in the
|
|
20
|
+
contract the memory design rests on, with all three provider translation
|
|
21
|
+
branches then needing to strip it before the wire. "Pin the last N turns"
|
|
22
|
+
needs no id and is how the tool would realistically be used mid-conversation.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
import config
|
|
26
|
+
|
|
27
|
+
TOOL_SCHEMA = {
|
|
28
|
+
"name": "pin",
|
|
29
|
+
"description": (
|
|
30
|
+
"Protect the most recent turns of this conversation from being "
|
|
31
|
+
"summarized away when the context gets long. Use this when "
|
|
32
|
+
"something established recently must survive verbatim -- an exact "
|
|
33
|
+
"identifier, a constraint the user stated once, a decision the rest "
|
|
34
|
+
"of the work depends on. A turn is one user message and everything "
|
|
35
|
+
"that answered it. Pinned messages stay in full; everything else "
|
|
36
|
+
"may eventually be condensed into a summary. There is a cap on how "
|
|
37
|
+
"much may be pinned at once; the error states it if you reach it."
|
|
38
|
+
),
|
|
39
|
+
"input_schema": {
|
|
40
|
+
"type": "object",
|
|
41
|
+
"properties": {
|
|
42
|
+
"last_n": {
|
|
43
|
+
"type": "integer",
|
|
44
|
+
"description": "How many recent turns to pin. Defaults to 1 "
|
|
45
|
+
"(the exchange in progress and the one before "
|
|
46
|
+
"it are already protected automatically, so "
|
|
47
|
+
"reach further back than you think you need).",
|
|
48
|
+
"minimum": 1,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
"required": [],
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
UNPIN_TOOL_SCHEMA = {
|
|
56
|
+
"name": "unpin",
|
|
57
|
+
"description": (
|
|
58
|
+
"Release the compaction protection on recent turns (#89). The "
|
|
59
|
+
"mirror of pin: use it when something you pinned earlier no longer "
|
|
60
|
+
"needs to survive verbatim -- a resolved question, a superseded "
|
|
61
|
+
"decision, stale detail. Pinned material is permanent until "
|
|
62
|
+
"released, so unpinning is how the conversation stays condensable."
|
|
63
|
+
),
|
|
64
|
+
"input_schema": {
|
|
65
|
+
"type": "object",
|
|
66
|
+
"properties": {
|
|
67
|
+
"last_n": {
|
|
68
|
+
"type": "integer",
|
|
69
|
+
"description": "How many recent turns to release. Defaults "
|
|
70
|
+
"to 1.",
|
|
71
|
+
"minimum": 1,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
"required": [],
|
|
75
|
+
},
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _validated_turns(params: dict) -> object:
|
|
80
|
+
"""pin/unpin's shared argument validation: the error, or the int."""
|
|
81
|
+
last_n = params.get("last_n", 1)
|
|
82
|
+
if isinstance(last_n, bool) or not isinstance(last_n, int):
|
|
83
|
+
# isinstance(True, int) is True in Python, and a model emitting
|
|
84
|
+
# `last_n: true` would otherwise pin exactly one turn while looking
|
|
85
|
+
# like it had been understood.
|
|
86
|
+
return "requires 'last_n' to be a whole number of turns."
|
|
87
|
+
if last_n < 1:
|
|
88
|
+
return "requires 'last_n' to be at least 1."
|
|
89
|
+
return last_n
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _pinned_share(memory) -> tuple:
|
|
93
|
+
"""(tokens still protected, cap) for result text -- M14's rule that a
|
|
94
|
+
cap is stated in what the model reads, not only in a log."""
|
|
95
|
+
from core.compaction import pin_measurements
|
|
96
|
+
|
|
97
|
+
m = pin_measurements(memory.thread_id, 1)
|
|
98
|
+
return m["pinned_tokens"], m["max_tokens"]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def run(params: dict, memory=None) -> dict:
|
|
102
|
+
# params.get, not params[]: no provider validates tool inputs against
|
|
103
|
+
# the schema, and a bare KeyError escapes the loop's ToolCallDenied
|
|
104
|
+
# handler and aborts the whole turn. Same reasoning as load_skill's.
|
|
105
|
+
last_n = _validated_turns(params)
|
|
106
|
+
if isinstance(last_n, str):
|
|
107
|
+
return {"error": f"pin {last_n}"}
|
|
108
|
+
if memory is None:
|
|
109
|
+
# Reachable only if the tool is dispatched outside a run. Better an
|
|
110
|
+
# explained error than a silent no-op reported as success -- the
|
|
111
|
+
# model would otherwise believe something is protected when nothing
|
|
112
|
+
# is, which is worse than not having the tool. pin.py's reasoning.
|
|
113
|
+
return {"error": "pin is only available inside a conversation."}
|
|
114
|
+
|
|
115
|
+
# #89's cap, checked BEFORE anything is applied. A pin is a permanent
|
|
116
|
+
# floor under the derived view -- pinned rows re-enter it verbatim
|
|
117
|
+
# forever (M9), nothing folds them -- so one ungated call used to be
|
|
118
|
+
# able to put a thread permanently past the compaction trigger with no
|
|
119
|
+
# way back. Over-cap REFUSES rather than trimming (M15's rule in the
|
|
120
|
+
# tool direction): silently protecting less than was asked would leave
|
|
121
|
+
# the model believing material is safe when it is not. The refusal
|
|
122
|
+
# states the cap, the request, and the current share, so the retry is
|
|
123
|
+
# informed rather than blind.
|
|
124
|
+
from core.compaction import pin_measurements
|
|
125
|
+
|
|
126
|
+
m = pin_measurements(memory.thread_id, last_n)
|
|
127
|
+
if m["requested_tokens"] > m["max_tokens"]:
|
|
128
|
+
already = (f"{m['pinned_tokens']} tokens are already protected "
|
|
129
|
+
f"({100 * m['pinned_tokens'] // max(1, m['max_tokens'])}% "
|
|
130
|
+
f"of the cap).")
|
|
131
|
+
return {
|
|
132
|
+
"error": (
|
|
133
|
+
f"Pinning the last {last_n} turn(s) would protect about "
|
|
134
|
+
f"{m['requested_tokens']} tokens, over the cap of "
|
|
135
|
+
f"{m['max_tokens']}. Nothing was changed. {already} "
|
|
136
|
+
f"Pin fewer turns, or unpin older material with `unpin`."
|
|
137
|
+
),
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
changed = memory.pin_last(last_n)
|
|
141
|
+
if not changed:
|
|
142
|
+
# Distinguish "already pinned" from "nothing to pin". Both return
|
|
143
|
+
# zero and they call for different next moves.
|
|
144
|
+
return {"result": f"Nothing to pin -- the last {last_n} turn(s) are "
|
|
145
|
+
f"already protected, or the conversation has not "
|
|
146
|
+
f"got that far yet."}
|
|
147
|
+
pinned, cap = _pinned_share(memory)
|
|
148
|
+
return {
|
|
149
|
+
"result": f"Pinned the last {last_n} turn(s) ({changed} messages). "
|
|
150
|
+
f"They will be kept verbatim rather than summarized when "
|
|
151
|
+
f"this conversation is compacted. {pinned} tokens "
|
|
152
|
+
f"protected of the {cap}-token cap.",
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def unpin_run(params: dict, memory=None) -> dict:
|
|
157
|
+
"""The mirror of run(): releases the ordinal window instead of
|
|
158
|
+
protecting it (#89). D26 called a pin reversible; until batch 16
|
|
159
|
+
nothing anywhere reversed one, and `set_pinned(pinned=False)` was
|
|
160
|
+
written and unreachable. Long sessions accumulate stale pins -- an
|
|
161
|
+
identifier resolved, a decision superseded -- and every one of them is
|
|
162
|
+
a row no future compaction can touch, so release has to be as
|
|
163
|
+
reachable as protection, for the same ungated reasons."""
|
|
164
|
+
if memory is None:
|
|
165
|
+
return {"error": "unpin is only available inside a conversation."}
|
|
166
|
+
last_n = _validated_turns(params)
|
|
167
|
+
if isinstance(last_n, str):
|
|
168
|
+
return {"error": f"unpin {last_n}"}
|
|
169
|
+
|
|
170
|
+
released = memory.unpin_last(last_n)
|
|
171
|
+
if not released:
|
|
172
|
+
return {"result": f"Nothing to release -- the last {last_n} "
|
|
173
|
+
f"turn(s) were not pinned."}
|
|
174
|
+
pinned, cap = _pinned_share(memory)
|
|
175
|
+
return {
|
|
176
|
+
"result": f"Released the last {last_n} turn(s) ({released} message"
|
|
177
|
+
f"{'s' if released != 1 else ''}). {pinned} tokens still "
|
|
178
|
+
f"protected of the {cap}-token cap.",
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def available() -> bool:
|
|
183
|
+
"""ToolSpec.available_check.
|
|
184
|
+
|
|
185
|
+
Hidden when compaction cannot happen at all. Pinning is protection
|
|
186
|
+
FROM compaction, so with the compactor agent missing there is nothing
|
|
187
|
+
to be protected from -- and advertising a tool whose only effect is to
|
|
188
|
+
set a flag nothing reads is the fetch_url defect shape (D24): a schema
|
|
189
|
+
the model can see, choose, and never get value from.
|
|
190
|
+
"""
|
|
191
|
+
from core import config_loader
|
|
192
|
+
|
|
193
|
+
return config.COMPACTOR_AGENT in config_loader.get_agents()
|
|
@@ -0,0 +1,228 @@
|
|
|
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
|