@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.
- package/.env.example +19 -14
- package/CONFIG_ARCHITECTURE.md +2002 -0
- package/NOTICE +1 -1
- package/README.md +233 -94
- package/agents/builtin/build.md +73 -0
- package/agents/builtin/general.md +80 -0
- package/agents/builtin/plan.md +23 -5
- package/agents/builtin/test.md +65 -0
- package/agents/builtin/writer.md +67 -0
- package/agents/manager.py +261 -195
- package/agents/subagent_tool.py +277 -43
- package/agents/tui_commands.py +7 -12
- package/bin/venastine.mjs +528 -382
- package/bin/venastine.py +71 -0
- package/cli_commands.py +835 -0
- package/config.py +95 -1198
- package/config.yaml +1090 -0
- package/config_edit.py +1301 -0
- package/config_schema.py +966 -0
- package/config_update.py +749 -0
- package/core/agent_activity.py +158 -20
- package/core/ansi.py +452 -0
- package/core/approval.py +27 -5
- package/core/call_view.py +364 -0
- package/core/cancel.py +121 -0
- package/core/chatgpt_auth.py +747 -0
- package/core/claims_view.py +107 -0
- package/core/client.py +1238 -82
- package/core/commands.py +454 -0
- package/core/compaction.py +830 -66
- package/core/config_loader.py +516 -97
- package/core/connect.py +686 -0
- package/core/events.py +68 -6
- package/core/file_mentions.py +280 -0
- package/core/interaction.py +87 -4
- package/core/line_pattern.py +128 -0
- package/core/loop.py +1516 -440
- package/core/memory.py +120 -8
- package/core/pipeline_models.py +23 -9
- package/core/provider_errors.py +234 -0
- package/core/reasoning/confidence_scoring.py +70 -54
- package/core/reasoning/json_retry.py +19 -1
- package/core/reasoning/orchestrator.py +232 -53
- package/core/reasoning/output_writer.py +90 -31
- package/core/reasoning/payload_validation.py +133 -7
- package/core/reasoning/pipeline_storage.py +427 -361
- package/core/reasoning/review.py +659 -615
- package/core/reasoning/scholar.py +38 -14
- package/core/reasoning/source_corpus.py +110 -4
- package/core/replay.py +158 -26
- package/core/session.py +36 -12
- package/core/session_view.py +259 -0
- package/core/session_wake.py +259 -0
- package/core/shell_sessions.py +1553 -0
- package/core/workspace_trust.py +1 -1
- package/credentials.py +308 -180
- package/database.py +82 -1
- package/{PRIVACY.md → docs/PRIVACY.md} +19 -7
- package/docs/SECURITY.md +108 -0
- package/docs/THIRD_PARTY_NOTICES.md +148 -0
- package/env_secrets.py +118 -77
- package/json_store.py +236 -16
- package/logging_setup.py +121 -11
- package/main.py +1671 -199
- package/mcp_client/client.py +257 -23
- package/mcp_client/config.py +484 -379
- package/mcp_client/registration.py +163 -2
- package/memories/manager.py +15 -0
- package/memories/tui_commands.py +7 -12
- package/package.json +234 -4
- package/project_init/config_files.py +4 -0
- package/project_init/generator.py +201 -37
- package/project_init/manifest.py +418 -406
- package/project_init/tui_commands.py +7 -4
- package/prompts/revalidate.md +1 -1
- package/prompts/revise.md +17 -15
- package/prompts/system_prompts.py +64 -10
- package/providers.json.example +94 -92
- package/pyproject.toml +51 -2
- package/requirements.txt +252 -29
- package/safety/policy_enforcement.py +353 -17
- package/security/askpass.py +419 -0
- package/security/capability.py +37 -7
- package/security/confine.py +484 -0
- package/security/hosted.py +182 -0
- package/security/permissions.py +54 -7
- package/security/posture.py +124 -12
- package/security/protected_paths.py +198 -6
- package/security/sandbox.py +3262 -139
- package/security/secrets.py +855 -0
- package/skills/tui_commands.py +3 -4
- package/storage.py +368 -35
- package/tools/base.py +465 -305
- package/tools/builtin/_math_common.py +306 -16
- package/tools/builtin/_net_common.py +267 -61
- package/tools/builtin/arxiv.py +681 -223
- package/tools/builtin/ask_user.py +231 -213
- package/tools/builtin/discrete_math.py +35 -4
- package/tools/builtin/fetch_url.py +239 -21
- package/tools/builtin/file_ops.py +341 -33
- package/tools/builtin/geometry.py +184 -148
- package/tools/builtin/get_time.py +2 -2
- package/tools/builtin/linear_algebra.py +66 -23
- package/tools/builtin/load_skill.py +10 -0
- package/tools/builtin/logic.py +171 -12
- package/tools/builtin/pin.py +46 -12
- package/tools/builtin/probability_stats.py +224 -228
- package/tools/builtin/project_docs.py +80 -21
- package/tools/builtin/remember.py +21 -4
- package/tools/builtin/shell.py +586 -67
- package/tools/builtin/shell_sessions.py +613 -0
- package/tools/builtin/symbolic_math.py +80 -10
- package/tools/builtin/todo.py +5 -3
- package/tools/builtin/web_search.py +329 -151
- package/tools/context.py +23 -0
- package/tools/isolation.py +387 -314
- package/tools/registry.py +347 -30
- package/tui/app.py +4645 -438
- package/tui/app.tcss +185 -2
- package/tui/commands.py +10 -154
- package/tui/diffs.py +286 -265
- package/tui/hosted_driver.py +102 -0
- package/tui/markdown.py +60 -10
- package/tui/meters.py +103 -7
- package/tui/screens.py +346 -92
- package/tui/themes.py +59 -19
- package/tui/widgets.py +1575 -111
- package/SECURITY.md +0 -88
- package/THIRD_PARTY_NOTICES.md +0 -100
|
@@ -1,213 +1,231 @@
|
|
|
1
|
-
"""
|
|
2
|
-
tools/builtin/ask_user.py
|
|
3
|
-
|
|
4
|
-
ROADMAP_v2 §23 slice 2: the question tool. "Up to 4 options, multi-select, a
|
|
5
|
-
write-your-own answer, and a 'chat about this' escape that returns control to
|
|
6
|
-
the conversation instead of forcing a choice."
|
|
7
|
-
|
|
8
|
-
IT ASKS THROUGH THE INJECTED CHANNEL, NOT THROUGH THE APPROVAL BRIDGE. The
|
|
9
|
-
loop's `_obtain_approval` fires only under `if needs_approval:`, builds a
|
|
10
|
-
payload of {"tool_name", "params"}, and coerces the answer to (bool, grant) --
|
|
11
|
-
a free-text or multi-select answer has nowhere to go in it. It does not need
|
|
12
|
-
one: `response_channel` is already an injectable parameter, handed to every
|
|
13
|
-
handler that names it on BOTH dispatch branches, which is what
|
|
14
|
-
tools/registry.py's `_INJECTABLE_PARAMS` comment anticipated -- "generalised
|
|
15
|
-
so §23's response-channel tools add a third injectable name without reopening
|
|
16
|
-
dispatch()". So core/loop.py is untouched by this tool.
|
|
17
|
-
|
|
18
|
-
UNGATED, DELIBERATELY (J12). Requiring approval to ask a question would mean
|
|
19
|
-
approving a prompt in order to be shown a prompt -- and worse, §13 does not
|
|
20
|
-
merely deny a gated tool where nothing can ask, it stops ADVERTISING it. A
|
|
21
|
-
gated question tool would be invisible in every headless run rather than
|
|
22
|
-
denied with a reason, and §23 AC2 wants the opposite: the model must see the tool,
|
|
23
|
-
call it, and receive "there is nobody to ask" as something it can work around.
|
|
24
|
-
Same shape as `pin`'s ungating (D26), for the same reason.
|
|
25
|
-
|
|
26
|
-
NO CHANNEL MEANS DENIED, AND THE DENIAL EXPLAINS ITSELF (§23 AC2). An unattended
|
|
27
|
-
ten-pass pipeline is exactly where a blocking prompt nobody will answer turns
|
|
28
|
-
into a hang, and where a question injected by fetched web content would be
|
|
29
|
-
least noticed. An attended run carries a channel and can ask -- that is §25's
|
|
30
|
-
amendment, and it is why the condition tested here is "is there a channel"
|
|
31
|
-
rather than "is this the pipeline".
|
|
32
|
-
"""
|
|
33
|
-
|
|
34
|
-
import logging
|
|
35
|
-
|
|
36
|
-
from core import interaction
|
|
37
|
-
|
|
38
|
-
logger = logging.getLogger(__name__)
|
|
39
|
-
|
|
40
|
-
# The spec's cap, enforced rather than advisory. See run() for why a fifth
|
|
41
|
-
# option is an error and not a truncation.
|
|
42
|
-
MAX_OPTIONS = 4
|
|
43
|
-
|
|
44
|
-
#: Longest option the modal can show whole (batch 49). QuestionScreen is a
|
|
45
|
-
#: fixed 66 columns and an option button 58 of them, so 100 characters is
|
|
46
|
-
#: two wrapped lines for every shape measured -- prose, twenty-character
|
|
47
|
-
#: words, and an unbroken run -- and 105 is three for two of them. It is a
|
|
48
|
-
#: BUDGET, not a guess: the options region scrolls, so exceeding it costs
|
|
49
|
-
#: legibility rather than correctness, and the number is re-derivable by
|
|
50
|
-
#: the test that measures the drawn rows rather than trusting this
|
|
51
|
-
#: comment.
|
|
52
|
-
MAX_OPTION_CHARS = 100
|
|
53
|
-
|
|
54
|
-
TOOL_SCHEMA = {
|
|
55
|
-
"name": "ask_user",
|
|
56
|
-
"description": (
|
|
57
|
-
"Ask the user a question and wait for their answer. Use this when a "
|
|
58
|
-
"decision is genuinely theirs to make and getting it wrong would "
|
|
59
|
-
"waste work -- an ambiguous requirement, a choice between "
|
|
60
|
-
"approaches with different tradeoffs, a missing fact only they "
|
|
61
|
-
"have. Do NOT use it for things you can determine yourself, or to "
|
|
62
|
-
"confirm work you have already been asked to do. The user can pick "
|
|
63
|
-
"one or more of your options, write their own answer, or ask to "
|
|
64
|
-
"discuss it instead of choosing."
|
|
65
|
-
),
|
|
66
|
-
"input_schema": {
|
|
67
|
-
"type": "object",
|
|
68
|
-
"properties": {
|
|
69
|
-
"question": {
|
|
70
|
-
"type": "string",
|
|
71
|
-
"description": "The question, in one or two sentences. State "
|
|
72
|
-
"what you need and why it changes what you "
|
|
73
|
-
"will do.",
|
|
74
|
-
},
|
|
75
|
-
"options": {
|
|
76
|
-
"type": "array",
|
|
77
|
-
"items": {"type": "string"},
|
|
78
|
-
"description": f"Up to {MAX_OPTIONS} concrete answers to "
|
|
79
|
-
f"choose from. Optional -- omit it to ask an "
|
|
80
|
-
f"open question. Each must be at most "
|
|
81
|
-
f"{MAX_OPTION_CHARS} characters, so it can be "
|
|
82
|
-
f"shown whole -- put the reasoning in the "
|
|
83
|
-
f"question, not in the options.",
|
|
84
|
-
},
|
|
85
|
-
"multi_select": {
|
|
86
|
-
"type": "boolean",
|
|
87
|
-
"description": "True if several options can be chosen "
|
|
88
|
-
"together. Defaults to false.",
|
|
89
|
-
},
|
|
90
|
-
"allow_text": {
|
|
91
|
-
"type": "boolean",
|
|
92
|
-
"description": "Whether the user may write their own answer "
|
|
93
|
-
"instead of picking. Defaults to true.",
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
"required": ["question"],
|
|
97
|
-
},
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
def run(params: dict, response_channel=None) -> dict:
|
|
102
|
-
# params.get throughout, never params[]: no provider validates tool
|
|
103
|
-
# inputs against the schema
|
|
104
|
-
#
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
#
|
|
118
|
-
# the
|
|
119
|
-
#
|
|
120
|
-
#
|
|
121
|
-
#
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
#
|
|
133
|
-
#
|
|
134
|
-
#
|
|
135
|
-
#
|
|
136
|
-
#
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
f"
|
|
143
|
-
f"{longest
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
#
|
|
150
|
-
#
|
|
151
|
-
#
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
#
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
"
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
1
|
+
"""
|
|
2
|
+
tools/builtin/ask_user.py
|
|
3
|
+
|
|
4
|
+
ROADMAP_v2 §23 slice 2: the question tool. "Up to 4 options, multi-select, a
|
|
5
|
+
write-your-own answer, and a 'chat about this' escape that returns control to
|
|
6
|
+
the conversation instead of forcing a choice."
|
|
7
|
+
|
|
8
|
+
IT ASKS THROUGH THE INJECTED CHANNEL, NOT THROUGH THE APPROVAL BRIDGE. The
|
|
9
|
+
loop's `_obtain_approval` fires only under `if needs_approval:`, builds a
|
|
10
|
+
payload of {"tool_name", "params"}, and coerces the answer to (bool, grant) --
|
|
11
|
+
a free-text or multi-select answer has nowhere to go in it. It does not need
|
|
12
|
+
one: `response_channel` is already an injectable parameter, handed to every
|
|
13
|
+
handler that names it on BOTH dispatch branches, which is what
|
|
14
|
+
tools/registry.py's `_INJECTABLE_PARAMS` comment anticipated -- "generalised
|
|
15
|
+
so §23's response-channel tools add a third injectable name without reopening
|
|
16
|
+
dispatch()". So core/loop.py is untouched by this tool.
|
|
17
|
+
|
|
18
|
+
UNGATED, DELIBERATELY (J12). Requiring approval to ask a question would mean
|
|
19
|
+
approving a prompt in order to be shown a prompt -- and worse, §13 does not
|
|
20
|
+
merely deny a gated tool where nothing can ask, it stops ADVERTISING it. A
|
|
21
|
+
gated question tool would be invisible in every headless run rather than
|
|
22
|
+
denied with a reason, and §23 AC2 wants the opposite: the model must see the tool,
|
|
23
|
+
call it, and receive "there is nobody to ask" as something it can work around.
|
|
24
|
+
Same shape as `pin`'s ungating (D26), for the same reason.
|
|
25
|
+
|
|
26
|
+
NO CHANNEL MEANS DENIED, AND THE DENIAL EXPLAINS ITSELF (§23 AC2). An unattended
|
|
27
|
+
ten-pass pipeline is exactly where a blocking prompt nobody will answer turns
|
|
28
|
+
into a hang, and where a question injected by fetched web content would be
|
|
29
|
+
least noticed. An attended run carries a channel and can ask -- that is §25's
|
|
30
|
+
amendment, and it is why the condition tested here is "is there a channel"
|
|
31
|
+
rather than "is this the pipeline".
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
import logging
|
|
35
|
+
|
|
36
|
+
from core import agent_activity, interaction
|
|
37
|
+
|
|
38
|
+
logger = logging.getLogger(__name__)
|
|
39
|
+
|
|
40
|
+
# The spec's cap, enforced rather than advisory. See run() for why a fifth
|
|
41
|
+
# option is an error and not a truncation.
|
|
42
|
+
MAX_OPTIONS = 4
|
|
43
|
+
|
|
44
|
+
#: Longest option the modal can show whole (batch 49). QuestionScreen is a
|
|
45
|
+
#: fixed 66 columns and an option button 58 of them, so 100 characters is
|
|
46
|
+
#: two wrapped lines for every shape measured -- prose, twenty-character
|
|
47
|
+
#: words, and an unbroken run -- and 105 is three for two of them. It is a
|
|
48
|
+
#: BUDGET, not a guess: the options region scrolls, so exceeding it costs
|
|
49
|
+
#: legibility rather than correctness, and the number is re-derivable by
|
|
50
|
+
#: the test that measures the drawn rows rather than trusting this
|
|
51
|
+
#: comment.
|
|
52
|
+
MAX_OPTION_CHARS = 100
|
|
53
|
+
|
|
54
|
+
TOOL_SCHEMA = {
|
|
55
|
+
"name": "ask_user",
|
|
56
|
+
"description": (
|
|
57
|
+
"Ask the user a question and wait for their answer. Use this when a "
|
|
58
|
+
"decision is genuinely theirs to make and getting it wrong would "
|
|
59
|
+
"waste work -- an ambiguous requirement, a choice between "
|
|
60
|
+
"approaches with different tradeoffs, a missing fact only they "
|
|
61
|
+
"have. Do NOT use it for things you can determine yourself, or to "
|
|
62
|
+
"confirm work you have already been asked to do. The user can pick "
|
|
63
|
+
"one or more of your options, write their own answer, or ask to "
|
|
64
|
+
"discuss it instead of choosing."
|
|
65
|
+
),
|
|
66
|
+
"input_schema": {
|
|
67
|
+
"type": "object",
|
|
68
|
+
"properties": {
|
|
69
|
+
"question": {
|
|
70
|
+
"type": "string",
|
|
71
|
+
"description": "The question, in one or two sentences. State "
|
|
72
|
+
"what you need and why it changes what you "
|
|
73
|
+
"will do.",
|
|
74
|
+
},
|
|
75
|
+
"options": {
|
|
76
|
+
"type": "array",
|
|
77
|
+
"items": {"type": "string"},
|
|
78
|
+
"description": f"Up to {MAX_OPTIONS} concrete answers to "
|
|
79
|
+
f"choose from. Optional -- omit it to ask an "
|
|
80
|
+
f"open question. Each must be at most "
|
|
81
|
+
f"{MAX_OPTION_CHARS} characters, so it can be "
|
|
82
|
+
f"shown whole -- put the reasoning in the "
|
|
83
|
+
f"question, not in the options.",
|
|
84
|
+
},
|
|
85
|
+
"multi_select": {
|
|
86
|
+
"type": "boolean",
|
|
87
|
+
"description": "True if several options can be chosen "
|
|
88
|
+
"together. Defaults to false.",
|
|
89
|
+
},
|
|
90
|
+
"allow_text": {
|
|
91
|
+
"type": "boolean",
|
|
92
|
+
"description": "Whether the user may write their own answer "
|
|
93
|
+
"instead of picking. Defaults to true.",
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
"required": ["question"],
|
|
97
|
+
},
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def run(params: dict, response_channel=None) -> dict:
|
|
102
|
+
# params.get throughout, never params[]: no provider validates tool
|
|
103
|
+
# inputs against the schema. tools/base.py carries the one copy of why, under
|
|
104
|
+
# "WHAT A CALLABLE REGISTERED BELOW MAY RAISE": a handler's exception IS
|
|
105
|
+
# contained, so what this buys is an error dict the model can READ rather
|
|
106
|
+
# than the generic wrapper dispatch() would return (batch 146).
|
|
107
|
+
question = params.get("question")
|
|
108
|
+
if not question or not isinstance(question, str):
|
|
109
|
+
return {"error": "ask_user requires a 'question' string."}
|
|
110
|
+
|
|
111
|
+
options = params.get("options") or []
|
|
112
|
+
if not isinstance(options, (list, tuple)):
|
|
113
|
+
return {"error": "ask_user requires 'options' to be a list of strings."}
|
|
114
|
+
options = [str(o) for o in options]
|
|
115
|
+
|
|
116
|
+
if len(options) > MAX_OPTIONS:
|
|
117
|
+
# AN ERROR, NOT A TRUNCATION. Silently dropping the fifth option
|
|
118
|
+
# would show the user a different question from the one the model
|
|
119
|
+
# asked, and the model would then read the answer as a response to
|
|
120
|
+
# the full list. M15's rule -- never silently drop something
|
|
121
|
+
# somebody named -- with the model in the naming role. An error it
|
|
122
|
+
# can correct costs one step; a mis-answered question costs the
|
|
123
|
+
# decision.
|
|
124
|
+
return {
|
|
125
|
+
"error": f"ask_user allows at most {MAX_OPTIONS} options; "
|
|
126
|
+
f"{len(options)} were given. Ask a narrower question, "
|
|
127
|
+
f"or drop the options and ask it openly.",
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
too_long = [o for o in options if len(o) > MAX_OPTION_CHARS]
|
|
131
|
+
if too_long:
|
|
132
|
+
# AN ERROR, NOT A TRUNCATION, for the reason directly above: an
|
|
133
|
+
# option shortened here is not the option the model offered, and
|
|
134
|
+
# the answer would come back as though it were. The modal used to
|
|
135
|
+
# truncate this way by accident -- an option wider than the dialog
|
|
136
|
+
# was CUT at its edge, so a user chose between two options whose
|
|
137
|
+
# visible halves were identical -- which is the failure this
|
|
138
|
+
# refusal exists to make impossible rather than merely unlikely.
|
|
139
|
+
longest = max(too_long, key=len)
|
|
140
|
+
return {
|
|
141
|
+
"error": f"ask_user allows at most {MAX_OPTION_CHARS} characters "
|
|
142
|
+
f"per option so it can be read whole; one is "
|
|
143
|
+
f"{len(longest)}. Shorten it to the choice itself and "
|
|
144
|
+
f"put the reasoning in the question: "
|
|
145
|
+
f"{longest[:60]!r}...",
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if not options and params.get("allow_text", True) is False:
|
|
149
|
+
# #114, same family as the two refusals above: a question with no
|
|
150
|
+
# options and no text box renders as a modal whose only button is
|
|
151
|
+
# "Discuss instead", so a defer means "that was my only button"
|
|
152
|
+
# rather than "let's talk about it" -- collapsing exactly the
|
|
153
|
+
# distinction QuestionScreen's escape/defer pair exists to keep.
|
|
154
|
+
return {
|
|
155
|
+
"error": "ask_user needs something to answer with: give "
|
|
156
|
+
"options, or leave allow_text true so the user can "
|
|
157
|
+
"write a reply.",
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if response_channel is None:
|
|
161
|
+
# §23 AC2. Not an exception: dispatch would convert one to an error dict
|
|
162
|
+
# anyway, and this way the message is written for the reader who
|
|
163
|
+
# needs it -- a model mid-run that must now decide something itself.
|
|
164
|
+
return {
|
|
165
|
+
"error": "ask_user cannot reach anyone in this run -- there is "
|
|
166
|
+
"nobody to answer. Decide it yourself, state the "
|
|
167
|
+
"assumption you are proceeding on, and carry on.",
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
# §47, added by batch 75's review of it. WHICH run is asking, read off
|
|
171
|
+
# the open span rather than threaded through the tool's parameters --
|
|
172
|
+
# the same source and the same reason as `_obtain_approval`'s: params
|
|
173
|
+
# are the MODEL'S, and a model that could name its own asker could
|
|
174
|
+
# claim to be a run it is not.
|
|
175
|
+
#
|
|
176
|
+
# THE PRODUCER, because `core/interaction.py` cannot be. That module is
|
|
177
|
+
# the one place every question goes through and would be the tidy home
|
|
178
|
+
# for this, but it is a stdlib-only leaf -- `tests/test_interaction.py`
|
|
179
|
+
# pins its import list -- so reaching `core.agent_activity` from there
|
|
180
|
+
# would break a boundary to save one line here.
|
|
181
|
+
#
|
|
182
|
+
# None at depth 0, where the asking run is the conversation on screen.
|
|
183
|
+
asking = agent_activity.current()
|
|
184
|
+
answer = interaction.ask(response_channel, interaction.Request(
|
|
185
|
+
kind=interaction.QUESTION,
|
|
186
|
+
payload={
|
|
187
|
+
"question": question,
|
|
188
|
+
"options": options,
|
|
189
|
+
"multi_select": bool(params.get("multi_select")),
|
|
190
|
+
# Defaults TRUE: the spec lists a write-your-own answer as one of
|
|
191
|
+
# the four affordances, so absence of the key is not absence of
|
|
192
|
+
# the affordance.
|
|
193
|
+
"allow_text": params.get("allow_text", True) is not False,
|
|
194
|
+
"asking_agent": asking.name if asking is not None else None,
|
|
195
|
+
"asking_depth": asking.depth if asking is not None else 0,
|
|
196
|
+
},
|
|
197
|
+
))
|
|
198
|
+
|
|
199
|
+
if answer is None:
|
|
200
|
+
# Nobody answered -- dismissed, timed out, or a shell returning
|
|
201
|
+
# something decode could not read. Distinct from a deferral below,
|
|
202
|
+
# and reported as such: "they declined to answer" and "they want to
|
|
203
|
+
# discuss it" call for different next moves.
|
|
204
|
+
return {
|
|
205
|
+
"error": "The user did not answer the question. Proceed on your "
|
|
206
|
+
"best judgement and say which assumption you made.",
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if answer.get("defer"):
|
|
210
|
+
# The spec's escape, "returns control to the conversation instead of
|
|
211
|
+
# forcing a choice". A result rather than any change of control
|
|
212
|
+
# flow: the loop has one shape, and a tool that could abort a turn
|
|
213
|
+
# would be a second one. The model simply talks next.
|
|
214
|
+
return {
|
|
215
|
+
"result": "The user would rather discuss this than pick an "
|
|
216
|
+
"answer. Raise it in conversation -- do not call "
|
|
217
|
+
"ask_user again for the same question.",
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
chosen, text = answer.get("options") or [], answer.get("text") or ""
|
|
221
|
+
if not chosen and not text:
|
|
222
|
+
# A blank submission. They were present and said nothing, which is
|
|
223
|
+
# not the same as never being asked -- so this is not the no-channel
|
|
224
|
+
# error, and the wording does not tell the model to assume anything.
|
|
225
|
+
return {
|
|
226
|
+
"result": "The user answered without choosing an option or "
|
|
227
|
+
"writing anything. Ask again more specifically, or "
|
|
228
|
+
"raise it in conversation.",
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return {"result": {"chosen": chosen, "text": text}}
|
|
@@ -12,6 +12,8 @@ from typing import Literal, Optional
|
|
|
12
12
|
import sympy
|
|
13
13
|
from pydantic import BaseModel, Field
|
|
14
14
|
|
|
15
|
+
from tools.builtin._math_common import MathParseError, parse_params
|
|
16
|
+
|
|
15
17
|
_OPERATIONS = Literal[
|
|
16
18
|
"gcd", "lcm", "is_prime", "prime_factors", "next_prime",
|
|
17
19
|
"factorial", "permutations", "combinations",
|
|
@@ -42,7 +44,10 @@ TOOL_SCHEMA = {
|
|
|
42
44
|
|
|
43
45
|
|
|
44
46
|
def run(params: dict) -> dict:
|
|
45
|
-
|
|
47
|
+
try:
|
|
48
|
+
parsed = parse_params(DiscreteMathParams, params)
|
|
49
|
+
except MathParseError as e:
|
|
50
|
+
return {"error": str(e)}
|
|
46
51
|
|
|
47
52
|
try:
|
|
48
53
|
op = parsed.operation
|
|
@@ -72,11 +77,37 @@ def run(params: dict) -> dict:
|
|
|
72
77
|
return {"error": "factorial requires 'n' (integer)."}
|
|
73
78
|
result = sympy.factorial(parsed.n)
|
|
74
79
|
elif op == "permutations":
|
|
75
|
-
# nPr
|
|
76
|
-
#
|
|
80
|
+
# nPr AS THE FALLING FACTORIAL, which is O(r). It used to be
|
|
81
|
+
# `factorial(n) / factorial(n - r)`, so its cost was in `n`
|
|
82
|
+
# while the operation's cost is in `r` -- and `combinations`,
|
|
83
|
+
# four lines below in this same chain, was already O(r) via
|
|
84
|
+
# `binomial`. Measured, with `combinations` on identical
|
|
85
|
+
# inputs as the control in every row:
|
|
86
|
+
#
|
|
87
|
+
# permutations n=100000 r=1 0.14s
|
|
88
|
+
# permutations n=1000000 r=1 15.27s of a 20s budget
|
|
89
|
+
# permutations n=3000000 r=1 killed at the wall clock,
|
|
90
|
+
# 48.07s uncontained
|
|
91
|
+
# combinations n=3000000 r=1 0.0002s -> 3000000
|
|
92
|
+
#
|
|
93
|
+
# At r=1 the two operations return the same number, and the
|
|
94
|
+
# model was told "the inputs given are too large for this
|
|
95
|
+
# operation" about one multiplication. `ff` agrees with the
|
|
96
|
+
# factorial form everywhere both are defined, checked
|
|
97
|
+
# including the edges: ff(5,7)=0 (the old form divided by
|
|
98
|
+
# factorial(-2)=zoo) and ff(10,-1)=1/11.
|
|
99
|
+
#
|
|
100
|
+
# This is H5's trade -- paying a full budget to learn the
|
|
101
|
+
# answer is the wrong answer -- and it does NOT contradict H4,
|
|
102
|
+
# which refuses a per-parameter CEILING. The fix is an
|
|
103
|
+
# algorithm. H4's rationale ("cost is joint, so no
|
|
104
|
+
# per-parameter ceiling expresses it") was true of
|
|
105
|
+
# `combinations` and false of this line as it stood; the
|
|
106
|
+
# comment that stood here preferred a common sympy name to the
|
|
107
|
+
# right complexity class.
|
|
77
108
|
if parsed.n is None or parsed.r is None:
|
|
78
109
|
return {"error": "permutations requires 'n' and 'r' (integers)."}
|
|
79
|
-
result = sympy.
|
|
110
|
+
result = sympy.ff(parsed.n, parsed.r)
|
|
80
111
|
elif op == "combinations":
|
|
81
112
|
if parsed.n is None or parsed.r is None:
|
|
82
113
|
return {"error": "combinations requires 'n' and 'r' (integers)."}
|