@yahyahammad/venastine 0.3.8 → 0.3.10
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 -19
- package/CONFIG_ARCHITECTURE.md +2139 -2002
- package/LICENSE +201 -201
- package/README.md +1043 -1034
- package/agents/builtin/compactor.md +75 -75
- package/agents/builtin/explore.md +77 -77
- package/agents/builtin/pipeline-reviewer.md +100 -100
- package/agents/builtin/plan.md +106 -106
- package/agents/builtin/review.md +74 -74
- package/agents/manager.py +261 -261
- package/agents/subagent_tool.py +472 -472
- package/agents/tui_commands.py +238 -146
- package/bin/venastine.mjs +584 -528
- package/bin/venastine.py +73 -71
- package/cli_commands.py +848 -835
- package/config.yaml +64 -0
- package/config_edit.py +1301 -1301
- package/config_schema.py +987 -966
- package/config_update.py +749 -749
- package/core/agent_activity.py +256 -256
- package/core/ansi.py +452 -452
- package/core/approval.py +137 -137
- package/core/call_view.py +364 -364
- package/core/cancel.py +121 -121
- package/core/chatgpt_auth.py +747 -747
- package/core/claims_view.py +107 -107
- package/core/client.py +2756 -2715
- package/core/commands.py +464 -454
- package/core/compaction.py +1735 -1735
- package/core/config_loader.py +1982 -1982
- package/core/connect.py +686 -686
- package/core/events.py +123 -123
- package/core/file_mentions.py +280 -280
- package/core/interaction.py +411 -411
- package/core/loop.py +2566 -2566
- package/core/memory.py +482 -482
- package/core/pipeline_models.py +190 -190
- package/core/provider_errors.py +274 -234
- package/core/reasoning/authorization.py +130 -130
- package/core/reasoning/base.py +219 -219
- package/core/reasoning/confidence_scoring.py +484 -484
- package/core/reasoning/events.py +149 -149
- package/core/reasoning/orchestrator.py +1561 -1561
- package/core/reasoning/output_writer.py +345 -345
- package/core/reasoning/payload_validation.py +524 -524
- package/core/reasoning/pipeline_storage.py +427 -427
- package/core/reasoning/scholar.py +467 -458
- package/core/reasoning/source_corpus.py +433 -433
- package/core/reasoning/source_scoring.py +855 -855
- package/core/replay.py +315 -315
- package/core/shell_sessions.py +1553 -1553
- package/credentials.py +307 -307
- package/database.py +333 -333
- package/docs/PRIVACY.md +78 -78
- package/docs/SECURITY.md +114 -108
- package/docs/THIRD_PARTY_NOTICES.md +148 -148
- package/env_secrets.py +118 -118
- package/json_store.py +343 -343
- package/logging_setup.py +295 -295
- package/main.py +3433 -3433
- package/mcp_client/client.py +780 -733
- package/mcp_client/config.py +484 -484
- package/mcp_client/registration.py +343 -343
- package/memories/manager.py +147 -147
- package/memories/tui_commands.py +319 -239
- package/package.json +303 -303
- package/project_init/config_files.py +282 -282
- package/project_init/generator.py +728 -728
- package/project_init/manifest.py +418 -418
- package/project_init/tui_commands.py +147 -147
- package/prompts/critic_pass.md +24 -24
- package/prompts/revalidate.md +25 -25
- package/prompts/revise.md +17 -17
- package/prompts/source_grounding.md +66 -66
- package/prompts/system_prompts.py +316 -316
- package/providers.json.example +94 -94
- package/pyproject.toml +149 -149
- package/requirements.txt +314 -295
- package/safety/policy_enforcement.py +1082 -1082
- package/security/askpass.py +419 -419
- package/security/capability.py +207 -207
- package/security/confine.py +484 -484
- package/security/hosted.py +182 -182
- package/security/permissions.py +237 -237
- package/security/protected_paths.py +501 -501
- package/security/sandbox.py +4108 -4108
- package/skills/builtin/math/formal-specification.md +67 -67
- package/skills/builtin/math/numerical-methods.md +66 -66
- package/skills/builtin/research/experiment-design.md +62 -62
- package/skills/builtin/research/reproducibility.md +55 -55
- package/skills/builtin/research/source-evaluation.md +70 -70
- package/skills/builtin/research/statistical-inference.md +63 -63
- package/skills/builtin/research/technical-writing.md +59 -59
- package/skills/builtin/software/code-review.md +61 -61
- package/skills/builtin/software/debugging.md +61 -61
- package/skills/builtin/software/test-design.md +67 -67
- package/skills/tui_commands.py +110 -110
- package/storage.py +1338 -1338
- package/tools/base.py +465 -465
- package/tools/builtin/_math_common.py +667 -667
- package/tools/builtin/_net_common.py +1145 -267
- package/tools/builtin/_page_text.py +490 -0
- package/tools/builtin/_pdf_text.py +113 -0
- package/tools/builtin/arxiv.py +685 -681
- package/tools/builtin/fetch_url.py +639 -371
- package/tools/builtin/file_ops.py +720 -720
- package/tools/builtin/geometry.py +184 -184
- package/tools/builtin/load_skill.py +76 -76
- package/tools/builtin/pin.py +227 -227
- package/tools/builtin/probability_stats.py +224 -224
- package/tools/builtin/project_docs.py +464 -464
- package/tools/builtin/remember.py +175 -175
- package/tools/builtin/shell.py +980 -980
- package/tools/builtin/todo.py +149 -149
- package/tools/builtin/web_search.py +402 -328
- package/tools/context.py +159 -159
- package/tools/isolation.py +387 -387
- package/tools/registry.py +1252 -1242
- package/tui/app.py +8372 -8307
- package/tui/app.tcss +754 -739
- package/tui/commands.py +26 -26
- package/tui/diffs.py +286 -286
- package/tui/history.py +128 -128
- package/tui/hosted_driver.py +102 -102
- package/tui/markdown.py +1329 -1329
- package/tui/meters.py +364 -364
- package/tui/ravens.py +146 -146
- package/tui/screens.py +1255 -1197
- package/tui/themes.py +599 -599
- package/tui/widgets.py +3861 -3796
package/tools/builtin/pin.py
CHANGED
|
@@ -1,227 +1,227 @@
|
|
|
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
|
-
"default": 1,
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
"required": [],
|
|
53
|
-
},
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
UNPIN_TOOL_SCHEMA = {
|
|
57
|
-
"name": "unpin",
|
|
58
|
-
"description": (
|
|
59
|
-
"Release the compaction protection on recent turns (#89). The "
|
|
60
|
-
"mirror of pin: use it when something you pinned earlier no longer "
|
|
61
|
-
"needs to survive verbatim -- a resolved question, a superseded "
|
|
62
|
-
"decision, stale detail. Pinned material is permanent until "
|
|
63
|
-
"released, so unpinning is how the conversation stays condensable."
|
|
64
|
-
),
|
|
65
|
-
"input_schema": {
|
|
66
|
-
"type": "object",
|
|
67
|
-
"properties": {
|
|
68
|
-
"last_n": {
|
|
69
|
-
"type": "integer",
|
|
70
|
-
"description": "How many recent turns to release. Defaults "
|
|
71
|
-
"to 1.",
|
|
72
|
-
"minimum": 1,
|
|
73
|
-
"default": 1,
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
"required": [],
|
|
77
|
-
},
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def _validated_turns(params: dict) -> object:
|
|
82
|
-
"""pin/unpin's shared argument validation: the error, or the int.
|
|
83
|
-
|
|
84
|
-
An explicit null is ABSENT (batch 190). The ChatGPT plan's model sends
|
|
85
|
-
each optional parameter at its declared default (ROADMAP_v3 §83, the
|
|
86
|
-
owner's live check), so the schema now declares 1; a model that sends
|
|
87
|
-
null instead means the same thing, and refusing it cost a turn. Every
|
|
88
|
-
other non-integer is still refused, never coerced.
|
|
89
|
-
"""
|
|
90
|
-
last_n = params.get("last_n")
|
|
91
|
-
if last_n is None:
|
|
92
|
-
last_n = 1
|
|
93
|
-
if isinstance(last_n, bool) or not isinstance(last_n, int):
|
|
94
|
-
# isinstance(True, int) is True in Python, and a model emitting
|
|
95
|
-
# `last_n: true` would otherwise pin exactly one turn while looking
|
|
96
|
-
# like it had been understood.
|
|
97
|
-
return "requires 'last_n' to be a whole number of turns."
|
|
98
|
-
if last_n < 1:
|
|
99
|
-
return "requires 'last_n' to be at least 1."
|
|
100
|
-
return last_n
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
def _pinned_share(memory, parent_run=None) -> tuple:
|
|
104
|
-
"""(tokens still protected, cap) for result text -- M14's rule that a
|
|
105
|
-
cap is stated in what the model reads, not only in a log.
|
|
106
|
-
|
|
107
|
-
`parent_run` is threaded through for the CAP, which is a fraction of
|
|
108
|
-
the trigger THIS model folds at (batch 148): a result sentence
|
|
109
|
-
naming a different number than the refusal would be worse than naming
|
|
110
|
-
none.
|
|
111
|
-
"""
|
|
112
|
-
from core.compaction import pin_measurements
|
|
113
|
-
|
|
114
|
-
m = pin_measurements(memory.thread_id, 1, *_pair(parent_run))
|
|
115
|
-
return m["pinned_tokens"], m["max_tokens"]
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
def _pair(parent_run) -> tuple:
|
|
119
|
-
"""(model, provider_name) off the enclosing run, or (None, None).
|
|
120
|
-
|
|
121
|
-
A RunInfo is built once per turn (core/loop.py) so this is available on
|
|
122
|
-
every path a model can call `pin` from -- chat, the CLI and a research
|
|
123
|
-
pass. None is not a failure: pin_measurements then measures the cap
|
|
124
|
-
against the configured trigger, which is what it did everywhere before
|
|
125
|
-
#89's cap started depending on the model.
|
|
126
|
-
"""
|
|
127
|
-
if parent_run is None:
|
|
128
|
-
return None, None
|
|
129
|
-
return (getattr(parent_run, "model", None),
|
|
130
|
-
getattr(parent_run, "provider_name", None))
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
def run(params: dict, memory=None, parent_run=None) -> dict:
|
|
134
|
-
# params.get, not params[]: no provider validates tool inputs against
|
|
135
|
-
# the schema. tools/base.py carries the one copy of why, under
|
|
136
|
-
# "WHAT A CALLABLE REGISTERED BELOW MAY RAISE": a handler's exception IS
|
|
137
|
-
# contained, so what this buys is an error dict the model can READ rather
|
|
138
|
-
# than the generic wrapper dispatch() would return (batch 146).
|
|
139
|
-
last_n = _validated_turns(params)
|
|
140
|
-
if isinstance(last_n, str):
|
|
141
|
-
return {"error": f"pin {last_n}"}
|
|
142
|
-
if memory is None:
|
|
143
|
-
# Reachable only if the tool is dispatched outside a run. Better an
|
|
144
|
-
# explained error than a silent no-op reported as success -- the
|
|
145
|
-
# model would otherwise believe something is protected when nothing
|
|
146
|
-
# is, which is worse than not having the tool. pin.py's reasoning.
|
|
147
|
-
return {"error": "pin is only available inside a conversation."}
|
|
148
|
-
|
|
149
|
-
# #89's cap, checked BEFORE anything is applied. A pin is a permanent
|
|
150
|
-
# floor under the derived view -- pinned rows re-enter it verbatim
|
|
151
|
-
# forever (M9), nothing folds them -- so one ungated call used to be
|
|
152
|
-
# able to put a thread permanently past the compaction trigger with no
|
|
153
|
-
# way back. Over-cap REFUSES rather than trimming (M15's rule in the
|
|
154
|
-
# tool direction): silently protecting less than was asked would leave
|
|
155
|
-
# the model believing material is safe when it is not. The refusal
|
|
156
|
-
# states the cap, the request, and the current share, so the retry is
|
|
157
|
-
# informed rather than blind.
|
|
158
|
-
from core.compaction import pin_measurements
|
|
159
|
-
|
|
160
|
-
m = pin_measurements(memory.thread_id, last_n, *_pair(parent_run))
|
|
161
|
-
if m["requested_tokens"] > m["max_tokens"]:
|
|
162
|
-
already = (f"{m['pinned_tokens']} tokens are already protected "
|
|
163
|
-
f"({100 * m['pinned_tokens'] // max(1, m['max_tokens'])}% "
|
|
164
|
-
f"of the cap).")
|
|
165
|
-
return {
|
|
166
|
-
"error": (
|
|
167
|
-
f"Pinning the last {last_n} turn(s) would protect about "
|
|
168
|
-
f"{m['requested_tokens']} tokens, over the cap of "
|
|
169
|
-
f"{m['max_tokens']}. Nothing was changed. {already} "
|
|
170
|
-
f"Pin fewer turns, or unpin older material with `unpin`."
|
|
171
|
-
),
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
changed = memory.pin_last(last_n)
|
|
175
|
-
if not changed:
|
|
176
|
-
# Distinguish "already pinned" from "nothing to pin". Both return
|
|
177
|
-
# zero and they call for different next moves.
|
|
178
|
-
return {"result": f"Nothing to pin -- the last {last_n} turn(s) are "
|
|
179
|
-
f"already protected, or the conversation has not "
|
|
180
|
-
f"got that far yet."}
|
|
181
|
-
pinned, cap = _pinned_share(memory, parent_run)
|
|
182
|
-
return {
|
|
183
|
-
"result": f"Pinned the last {last_n} turn(s) ({changed} messages). "
|
|
184
|
-
f"They will be kept verbatim rather than summarized when "
|
|
185
|
-
f"this conversation is compacted. {pinned} tokens "
|
|
186
|
-
f"protected of the {cap}-token cap.",
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
def unpin_run(params: dict, memory=None, parent_run=None) -> dict:
|
|
191
|
-
"""The mirror of run(): releases the ordinal window instead of
|
|
192
|
-
protecting it (#89). D26 called a pin reversible; until batch 16
|
|
193
|
-
nothing anywhere reversed one, and `set_pinned(pinned=False)` was
|
|
194
|
-
written and unreachable. Long sessions accumulate stale pins -- an
|
|
195
|
-
identifier resolved, a decision superseded -- and every one of them is
|
|
196
|
-
a row no future compaction can touch, so release has to be as
|
|
197
|
-
reachable as protection, for the same ungated reasons."""
|
|
198
|
-
if memory is None:
|
|
199
|
-
return {"error": "unpin is only available inside a conversation."}
|
|
200
|
-
last_n = _validated_turns(params)
|
|
201
|
-
if isinstance(last_n, str):
|
|
202
|
-
return {"error": f"unpin {last_n}"}
|
|
203
|
-
|
|
204
|
-
released = memory.unpin_last(last_n)
|
|
205
|
-
if not released:
|
|
206
|
-
return {"result": f"Nothing to release -- the last {last_n} "
|
|
207
|
-
f"turn(s) were not pinned."}
|
|
208
|
-
pinned, cap = _pinned_share(memory, parent_run)
|
|
209
|
-
return {
|
|
210
|
-
"result": f"Released the last {last_n} turn(s) ({released} message"
|
|
211
|
-
f"{'s' if released != 1 else ''}). {pinned} tokens still "
|
|
212
|
-
f"protected of the {cap}-token cap.",
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
def available() -> bool:
|
|
217
|
-
"""ToolSpec.available_check.
|
|
218
|
-
|
|
219
|
-
Hidden when compaction cannot happen at all. Pinning is protection
|
|
220
|
-
FROM compaction, so with the compactor agent missing there is nothing
|
|
221
|
-
to be protected from -- and advertising a tool whose only effect is to
|
|
222
|
-
set a flag nothing reads is the fetch_url defect shape (D24): a schema
|
|
223
|
-
the model can see, choose, and never get value from.
|
|
224
|
-
"""
|
|
225
|
-
from core import config_loader
|
|
226
|
-
|
|
227
|
-
return config.COMPACTOR_AGENT in config_loader.get_agents()
|
|
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
|
+
"default": 1,
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
"required": [],
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
UNPIN_TOOL_SCHEMA = {
|
|
57
|
+
"name": "unpin",
|
|
58
|
+
"description": (
|
|
59
|
+
"Release the compaction protection on recent turns (#89). The "
|
|
60
|
+
"mirror of pin: use it when something you pinned earlier no longer "
|
|
61
|
+
"needs to survive verbatim -- a resolved question, a superseded "
|
|
62
|
+
"decision, stale detail. Pinned material is permanent until "
|
|
63
|
+
"released, so unpinning is how the conversation stays condensable."
|
|
64
|
+
),
|
|
65
|
+
"input_schema": {
|
|
66
|
+
"type": "object",
|
|
67
|
+
"properties": {
|
|
68
|
+
"last_n": {
|
|
69
|
+
"type": "integer",
|
|
70
|
+
"description": "How many recent turns to release. Defaults "
|
|
71
|
+
"to 1.",
|
|
72
|
+
"minimum": 1,
|
|
73
|
+
"default": 1,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
"required": [],
|
|
77
|
+
},
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _validated_turns(params: dict) -> object:
|
|
82
|
+
"""pin/unpin's shared argument validation: the error, or the int.
|
|
83
|
+
|
|
84
|
+
An explicit null is ABSENT (batch 190). The ChatGPT plan's model sends
|
|
85
|
+
each optional parameter at its declared default (ROADMAP_v3 §83, the
|
|
86
|
+
owner's live check), so the schema now declares 1; a model that sends
|
|
87
|
+
null instead means the same thing, and refusing it cost a turn. Every
|
|
88
|
+
other non-integer is still refused, never coerced.
|
|
89
|
+
"""
|
|
90
|
+
last_n = params.get("last_n")
|
|
91
|
+
if last_n is None:
|
|
92
|
+
last_n = 1
|
|
93
|
+
if isinstance(last_n, bool) or not isinstance(last_n, int):
|
|
94
|
+
# isinstance(True, int) is True in Python, and a model emitting
|
|
95
|
+
# `last_n: true` would otherwise pin exactly one turn while looking
|
|
96
|
+
# like it had been understood.
|
|
97
|
+
return "requires 'last_n' to be a whole number of turns."
|
|
98
|
+
if last_n < 1:
|
|
99
|
+
return "requires 'last_n' to be at least 1."
|
|
100
|
+
return last_n
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _pinned_share(memory, parent_run=None) -> tuple:
|
|
104
|
+
"""(tokens still protected, cap) for result text -- M14's rule that a
|
|
105
|
+
cap is stated in what the model reads, not only in a log.
|
|
106
|
+
|
|
107
|
+
`parent_run` is threaded through for the CAP, which is a fraction of
|
|
108
|
+
the trigger THIS model folds at (batch 148): a result sentence
|
|
109
|
+
naming a different number than the refusal would be worse than naming
|
|
110
|
+
none.
|
|
111
|
+
"""
|
|
112
|
+
from core.compaction import pin_measurements
|
|
113
|
+
|
|
114
|
+
m = pin_measurements(memory.thread_id, 1, *_pair(parent_run))
|
|
115
|
+
return m["pinned_tokens"], m["max_tokens"]
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _pair(parent_run) -> tuple:
|
|
119
|
+
"""(model, provider_name) off the enclosing run, or (None, None).
|
|
120
|
+
|
|
121
|
+
A RunInfo is built once per turn (core/loop.py) so this is available on
|
|
122
|
+
every path a model can call `pin` from -- chat, the CLI and a research
|
|
123
|
+
pass. None is not a failure: pin_measurements then measures the cap
|
|
124
|
+
against the configured trigger, which is what it did everywhere before
|
|
125
|
+
#89's cap started depending on the model.
|
|
126
|
+
"""
|
|
127
|
+
if parent_run is None:
|
|
128
|
+
return None, None
|
|
129
|
+
return (getattr(parent_run, "model", None),
|
|
130
|
+
getattr(parent_run, "provider_name", None))
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def run(params: dict, memory=None, parent_run=None) -> dict:
|
|
134
|
+
# params.get, not params[]: no provider validates tool inputs against
|
|
135
|
+
# the schema. tools/base.py carries the one copy of why, under
|
|
136
|
+
# "WHAT A CALLABLE REGISTERED BELOW MAY RAISE": a handler's exception IS
|
|
137
|
+
# contained, so what this buys is an error dict the model can READ rather
|
|
138
|
+
# than the generic wrapper dispatch() would return (batch 146).
|
|
139
|
+
last_n = _validated_turns(params)
|
|
140
|
+
if isinstance(last_n, str):
|
|
141
|
+
return {"error": f"pin {last_n}"}
|
|
142
|
+
if memory is None:
|
|
143
|
+
# Reachable only if the tool is dispatched outside a run. Better an
|
|
144
|
+
# explained error than a silent no-op reported as success -- the
|
|
145
|
+
# model would otherwise believe something is protected when nothing
|
|
146
|
+
# is, which is worse than not having the tool. pin.py's reasoning.
|
|
147
|
+
return {"error": "pin is only available inside a conversation."}
|
|
148
|
+
|
|
149
|
+
# #89's cap, checked BEFORE anything is applied. A pin is a permanent
|
|
150
|
+
# floor under the derived view -- pinned rows re-enter it verbatim
|
|
151
|
+
# forever (M9), nothing folds them -- so one ungated call used to be
|
|
152
|
+
# able to put a thread permanently past the compaction trigger with no
|
|
153
|
+
# way back. Over-cap REFUSES rather than trimming (M15's rule in the
|
|
154
|
+
# tool direction): silently protecting less than was asked would leave
|
|
155
|
+
# the model believing material is safe when it is not. The refusal
|
|
156
|
+
# states the cap, the request, and the current share, so the retry is
|
|
157
|
+
# informed rather than blind.
|
|
158
|
+
from core.compaction import pin_measurements
|
|
159
|
+
|
|
160
|
+
m = pin_measurements(memory.thread_id, last_n, *_pair(parent_run))
|
|
161
|
+
if m["requested_tokens"] > m["max_tokens"]:
|
|
162
|
+
already = (f"{m['pinned_tokens']} tokens are already protected "
|
|
163
|
+
f"({100 * m['pinned_tokens'] // max(1, m['max_tokens'])}% "
|
|
164
|
+
f"of the cap).")
|
|
165
|
+
return {
|
|
166
|
+
"error": (
|
|
167
|
+
f"Pinning the last {last_n} turn(s) would protect about "
|
|
168
|
+
f"{m['requested_tokens']} tokens, over the cap of "
|
|
169
|
+
f"{m['max_tokens']}. Nothing was changed. {already} "
|
|
170
|
+
f"Pin fewer turns, or unpin older material with `unpin`."
|
|
171
|
+
),
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
changed = memory.pin_last(last_n)
|
|
175
|
+
if not changed:
|
|
176
|
+
# Distinguish "already pinned" from "nothing to pin". Both return
|
|
177
|
+
# zero and they call for different next moves.
|
|
178
|
+
return {"result": f"Nothing to pin -- the last {last_n} turn(s) are "
|
|
179
|
+
f"already protected, or the conversation has not "
|
|
180
|
+
f"got that far yet."}
|
|
181
|
+
pinned, cap = _pinned_share(memory, parent_run)
|
|
182
|
+
return {
|
|
183
|
+
"result": f"Pinned the last {last_n} turn(s) ({changed} messages). "
|
|
184
|
+
f"They will be kept verbatim rather than summarized when "
|
|
185
|
+
f"this conversation is compacted. {pinned} tokens "
|
|
186
|
+
f"protected of the {cap}-token cap.",
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def unpin_run(params: dict, memory=None, parent_run=None) -> dict:
|
|
191
|
+
"""The mirror of run(): releases the ordinal window instead of
|
|
192
|
+
protecting it (#89). D26 called a pin reversible; until batch 16
|
|
193
|
+
nothing anywhere reversed one, and `set_pinned(pinned=False)` was
|
|
194
|
+
written and unreachable. Long sessions accumulate stale pins -- an
|
|
195
|
+
identifier resolved, a decision superseded -- and every one of them is
|
|
196
|
+
a row no future compaction can touch, so release has to be as
|
|
197
|
+
reachable as protection, for the same ungated reasons."""
|
|
198
|
+
if memory is None:
|
|
199
|
+
return {"error": "unpin is only available inside a conversation."}
|
|
200
|
+
last_n = _validated_turns(params)
|
|
201
|
+
if isinstance(last_n, str):
|
|
202
|
+
return {"error": f"unpin {last_n}"}
|
|
203
|
+
|
|
204
|
+
released = memory.unpin_last(last_n)
|
|
205
|
+
if not released:
|
|
206
|
+
return {"result": f"Nothing to release -- the last {last_n} "
|
|
207
|
+
f"turn(s) were not pinned."}
|
|
208
|
+
pinned, cap = _pinned_share(memory, parent_run)
|
|
209
|
+
return {
|
|
210
|
+
"result": f"Released the last {last_n} turn(s) ({released} message"
|
|
211
|
+
f"{'s' if released != 1 else ''}). {pinned} tokens still "
|
|
212
|
+
f"protected of the {cap}-token cap.",
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def available() -> bool:
|
|
217
|
+
"""ToolSpec.available_check.
|
|
218
|
+
|
|
219
|
+
Hidden when compaction cannot happen at all. Pinning is protection
|
|
220
|
+
FROM compaction, so with the compactor agent missing there is nothing
|
|
221
|
+
to be protected from -- and advertising a tool whose only effect is to
|
|
222
|
+
set a flag nothing reads is the fetch_url defect shape (D24): a schema
|
|
223
|
+
the model can see, choose, and never get value from.
|
|
224
|
+
"""
|
|
225
|
+
from core import config_loader
|
|
226
|
+
|
|
227
|
+
return config.COMPACTOR_AGENT in config_loader.get_agents()
|