@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.
Files changed (130) hide show
  1. package/.env.example +19 -19
  2. package/CONFIG_ARCHITECTURE.md +2139 -2002
  3. package/LICENSE +201 -201
  4. package/README.md +1043 -1034
  5. package/agents/builtin/compactor.md +75 -75
  6. package/agents/builtin/explore.md +77 -77
  7. package/agents/builtin/pipeline-reviewer.md +100 -100
  8. package/agents/builtin/plan.md +106 -106
  9. package/agents/builtin/review.md +74 -74
  10. package/agents/manager.py +261 -261
  11. package/agents/subagent_tool.py +472 -472
  12. package/agents/tui_commands.py +238 -146
  13. package/bin/venastine.mjs +584 -528
  14. package/bin/venastine.py +73 -71
  15. package/cli_commands.py +848 -835
  16. package/config.yaml +64 -0
  17. package/config_edit.py +1301 -1301
  18. package/config_schema.py +987 -966
  19. package/config_update.py +749 -749
  20. package/core/agent_activity.py +256 -256
  21. package/core/ansi.py +452 -452
  22. package/core/approval.py +137 -137
  23. package/core/call_view.py +364 -364
  24. package/core/cancel.py +121 -121
  25. package/core/chatgpt_auth.py +747 -747
  26. package/core/claims_view.py +107 -107
  27. package/core/client.py +2756 -2715
  28. package/core/commands.py +464 -454
  29. package/core/compaction.py +1735 -1735
  30. package/core/config_loader.py +1982 -1982
  31. package/core/connect.py +686 -686
  32. package/core/events.py +123 -123
  33. package/core/file_mentions.py +280 -280
  34. package/core/interaction.py +411 -411
  35. package/core/loop.py +2566 -2566
  36. package/core/memory.py +482 -482
  37. package/core/pipeline_models.py +190 -190
  38. package/core/provider_errors.py +274 -234
  39. package/core/reasoning/authorization.py +130 -130
  40. package/core/reasoning/base.py +219 -219
  41. package/core/reasoning/confidence_scoring.py +484 -484
  42. package/core/reasoning/events.py +149 -149
  43. package/core/reasoning/orchestrator.py +1561 -1561
  44. package/core/reasoning/output_writer.py +345 -345
  45. package/core/reasoning/payload_validation.py +524 -524
  46. package/core/reasoning/pipeline_storage.py +427 -427
  47. package/core/reasoning/scholar.py +467 -458
  48. package/core/reasoning/source_corpus.py +433 -433
  49. package/core/reasoning/source_scoring.py +855 -855
  50. package/core/replay.py +315 -315
  51. package/core/shell_sessions.py +1553 -1553
  52. package/credentials.py +307 -307
  53. package/database.py +333 -333
  54. package/docs/PRIVACY.md +78 -78
  55. package/docs/SECURITY.md +114 -108
  56. package/docs/THIRD_PARTY_NOTICES.md +148 -148
  57. package/env_secrets.py +118 -118
  58. package/json_store.py +343 -343
  59. package/logging_setup.py +295 -295
  60. package/main.py +3433 -3433
  61. package/mcp_client/client.py +780 -733
  62. package/mcp_client/config.py +484 -484
  63. package/mcp_client/registration.py +343 -343
  64. package/memories/manager.py +147 -147
  65. package/memories/tui_commands.py +319 -239
  66. package/package.json +303 -303
  67. package/project_init/config_files.py +282 -282
  68. package/project_init/generator.py +728 -728
  69. package/project_init/manifest.py +418 -418
  70. package/project_init/tui_commands.py +147 -147
  71. package/prompts/critic_pass.md +24 -24
  72. package/prompts/revalidate.md +25 -25
  73. package/prompts/revise.md +17 -17
  74. package/prompts/source_grounding.md +66 -66
  75. package/prompts/system_prompts.py +316 -316
  76. package/providers.json.example +94 -94
  77. package/pyproject.toml +149 -149
  78. package/requirements.txt +314 -295
  79. package/safety/policy_enforcement.py +1082 -1082
  80. package/security/askpass.py +419 -419
  81. package/security/capability.py +207 -207
  82. package/security/confine.py +484 -484
  83. package/security/hosted.py +182 -182
  84. package/security/permissions.py +237 -237
  85. package/security/protected_paths.py +501 -501
  86. package/security/sandbox.py +4108 -4108
  87. package/skills/builtin/math/formal-specification.md +67 -67
  88. package/skills/builtin/math/numerical-methods.md +66 -66
  89. package/skills/builtin/research/experiment-design.md +62 -62
  90. package/skills/builtin/research/reproducibility.md +55 -55
  91. package/skills/builtin/research/source-evaluation.md +70 -70
  92. package/skills/builtin/research/statistical-inference.md +63 -63
  93. package/skills/builtin/research/technical-writing.md +59 -59
  94. package/skills/builtin/software/code-review.md +61 -61
  95. package/skills/builtin/software/debugging.md +61 -61
  96. package/skills/builtin/software/test-design.md +67 -67
  97. package/skills/tui_commands.py +110 -110
  98. package/storage.py +1338 -1338
  99. package/tools/base.py +465 -465
  100. package/tools/builtin/_math_common.py +667 -667
  101. package/tools/builtin/_net_common.py +1145 -267
  102. package/tools/builtin/_page_text.py +490 -0
  103. package/tools/builtin/_pdf_text.py +113 -0
  104. package/tools/builtin/arxiv.py +685 -681
  105. package/tools/builtin/fetch_url.py +639 -371
  106. package/tools/builtin/file_ops.py +720 -720
  107. package/tools/builtin/geometry.py +184 -184
  108. package/tools/builtin/load_skill.py +76 -76
  109. package/tools/builtin/pin.py +227 -227
  110. package/tools/builtin/probability_stats.py +224 -224
  111. package/tools/builtin/project_docs.py +464 -464
  112. package/tools/builtin/remember.py +175 -175
  113. package/tools/builtin/shell.py +980 -980
  114. package/tools/builtin/todo.py +149 -149
  115. package/tools/builtin/web_search.py +402 -328
  116. package/tools/context.py +159 -159
  117. package/tools/isolation.py +387 -387
  118. package/tools/registry.py +1252 -1242
  119. package/tui/app.py +8372 -8307
  120. package/tui/app.tcss +754 -739
  121. package/tui/commands.py +26 -26
  122. package/tui/diffs.py +286 -286
  123. package/tui/history.py +128 -128
  124. package/tui/hosted_driver.py +102 -102
  125. package/tui/markdown.py +1329 -1329
  126. package/tui/meters.py +364 -364
  127. package/tui/ravens.py +146 -146
  128. package/tui/screens.py +1255 -1197
  129. package/tui/themes.py +599 -599
  130. package/tui/widgets.py +3861 -3796
package/agents/manager.py CHANGED
@@ -1,261 +1,261 @@
1
- """
2
- agents/manager.py
3
-
4
- ROADMAP_v2 §18. AgentManager discovers agent `.md` files across the three
5
- tiers and exposes lookup by name plus the §18 scope-composition rules.
6
-
7
- Thin by design: discovery, frontmatter parsing, and tier precedence
8
- (D29: harness > user > project) all live in core/config_loader.py, which
9
- runs at initialize() time. This file adds only the agent-specific policy
10
- composition that §18 owns and that §20 (subagent review) will call:
11
-
12
- * active_context() -- the ToolContext for a session-scoped active
13
- agent (/agent switch): the agent's own restrictions at depth 0.
14
- * child_context() -- the ToolContext for a spawned subagent: C6's
15
- intersection-with-parent (never union) plus a depth increment (C3).
16
- * system_prompt_for() -- base + catalogs + the agent's body + opt-in
17
- project context, assembled in one place so the TUI worker and
18
- spawn_subagent cannot drift apart.
19
- """
20
-
21
- from typing import Optional
22
-
23
- from core import config_loader
24
- from core.config_loader import AgentDef
25
- from memories.manager import manager as memory_manager
26
- from tools.context import ToolContext
27
-
28
- #: What /grill-me asks, in either shell (§18; shared since ROADMAP_v3 §89).
29
- GRILL_ME_TASK = "Grill this thread: surface what still needs a decision."
30
-
31
-
32
- class AgentManager:
33
- """Lookup + scope composition over the loader's discovered agents."""
34
-
35
- def get(self, name: str) -> Optional[AgentDef]:
36
- return config_loader.get_agent(name)
37
-
38
- def names(self) -> list:
39
- return sorted(config_loader.get_agents())
40
-
41
- def all(self) -> dict:
42
- return config_loader.get_agents()
43
-
44
- @staticmethod
45
- def active_context(agent: AgentDef) -> ToolContext:
46
- """ToolContext for running AS this agent (session-scoped /agent
47
- switch). The agent's own restrictions at depth 0 -- no parent to
48
- intersect with, no nesting."""
49
- allowed = (
50
- set(agent.allowed_tools)
51
- if agent.allowed_tools is not None else None
52
- )
53
- targets = (
54
- set(agent.spawn_targets)
55
- if agent.spawn_targets is not None else None
56
- )
57
- return ToolContext(
58
- allowed_tools=allowed,
59
- approval_overrides=dict(agent.approval_overrides),
60
- subagent_depth=0,
61
- spawn_targets=targets,
62
- )
63
-
64
- @staticmethod
65
- def child_context(
66
- agent: AgentDef, parent_context: Optional[ToolContext],
67
- ) -> ToolContext:
68
- """ToolContext for a subagent spawned WHILE running under
69
- parent_context. C6: intersection, never union -- a restricted
70
- parent must not escape its restriction by spawning a permissive
71
- child. C3: depth increments so the nesting limit is enforceable.
72
-
73
- An agent that declares no allowed_tools inherits the parent's
74
- restriction unchanged (it may not widen it either).
75
-
76
- APPROVAL COMPOSES THE SAME WAY (§14-§18 review f12, decision S2).
77
- §18's spec sketch built the child's overrides from the agent
78
- definition alone, which drops the parent's. C6 reasoned about
79
- allowed_tools and never considered the approval axis, so the same
80
- escalation it exists to prevent was open one field over: a parent
81
- declaring `approval_overrides: {web_search: true}` prompts on
82
- every search, then spawns a child that computes
83
- approval_needed(web_search) == False and runs it ungated.
84
-
85
- Union rather than replace. Only True entries carry -- a False is
86
- indistinguishable from absent under D14's OR, so unioning can
87
- only ever tighten, which is what makes it safe to do silently.
88
-
89
- BOTH SIDES ARE FILTERED, and batch 150 is why. The parent's dict
90
- was filtered and the agent's was merged over it raw, so a `False`
91
- in the child's frontmatter OVERWROTE the parent's `True` and the
92
- sentence above was false of the line below it: a parent that
93
- prompts on every search spawned a child that did not, which is
94
- the exact escalation the paragraph before this one describes.
95
- It did not heal one hop further out either -- the next `if v`
96
- dropped the False rather than restoring the True, so the
97
- tightening was gone permanently rather than suspended for one
98
- run. Three of the four quadrants had tests; the one the code got
99
- wrong was the one with none.
100
-
101
- The cost is not only the gate. `candidate_approvals` reads this
102
- same dict, and it is BOTH the sign-off notice and the grant the
103
- child receives -- measured, the loosened tool left the candidate
104
- list entirely, so the user was neither asked at call time nor
105
- told at sign-off time.
106
-
107
- `active_context` needs no filter and deliberately has none: at
108
- depth 0 there is no parent to overwrite, a False is inert under
109
- D14's OR, and the next hop's filter drops it. Measured both
110
- ways rather than reasoned about, because "the other composer
111
- looks similar" is how a fix grows past its finding."""
112
- parent = parent_context or ToolContext()
113
- if agent.allowed_tools is not None:
114
- own = set(agent.allowed_tools)
115
- child_allowed = own & (
116
- parent.allowed_tools if parent.allowed_tools is not None
117
- else own
118
- )
119
- else:
120
- child_allowed = (
121
- set(parent.allowed_tools)
122
- if parent.allowed_tools is not None else None
123
- )
124
- overrides = {k: v for k, v in parent.approval_overrides.items() if v}
125
- overrides.update(
126
- {k: v for k, v in agent.approval_overrides.items() if v})
127
- # SPAWN TARGETS COMPOSE THE SAME WAY, for C6's own reason one
128
- # field further out. The refusal that reads this field is checked
129
- # against the context a run holds, so a field that applied only at
130
- # depth 0 would be escapable by nesting: an agent forbidden to
131
- # reach `build` could spawn something that was not, and have that
132
- # one spawn it. Intersection makes the restriction survive the
133
- # hop, and an agent declaring none inherits its parent's roster
134
- # unchanged rather than widening back to everything.
135
- if agent.spawn_targets is not None:
136
- own_targets = set(agent.spawn_targets)
137
- child_targets = own_targets & (
138
- parent.spawn_targets if parent.spawn_targets is not None
139
- else own_targets
140
- )
141
- else:
142
- child_targets = (
143
- set(parent.spawn_targets)
144
- if parent.spawn_targets is not None else None
145
- )
146
- return ToolContext(
147
- allowed_tools=child_allowed,
148
- approval_overrides=overrides,
149
- subagent_depth=parent.subagent_depth + 1,
150
- spawn_targets=child_targets,
151
- )
152
-
153
- @staticmethod
154
- def candidate_approvals(child_context: ToolContext) -> list:
155
- """Tools the subagent could reach that would each need approval.
156
-
157
- ONE helper, TWO callers -- the sign-off prompt's notice text and
158
- the grant the child actually receives. Computing the list twice
159
- would let what the user approved drift from what the child got,
160
- which is the whole substance of the sign-off.
161
-
162
- Params are empty because there are none yet: a path-dependent
163
- approval_check (file_ops outside the workspace, shell on a
164
- non-inert command) cannot be resolved before the call exists, so
165
- those still prompt at call time through the inherited channel.
166
-
167
- §25 (R2) filters those tools OUT of the list rather than merely
168
- letting them re-prompt. They were never grantable by name, and the
169
- loop now enforces that -- so leaving them here would make the
170
- sign-off notice promise authority the grant does not carry, which
171
- is worse than not offering it. What the user reads and what the
172
- child receives have to be the same list; that is the whole
173
- substance of a sign-off, and this helper exists to keep them one.
174
-
175
- §25 (R13) adds the second filter, and #67 is why it is here rather
176
- than only in core/reasoning/authorization.py. Both functions
177
- answer "what may be pre-granted", both were built from
178
- headless_hidden + grantable, and only the pipeline's one then
179
- subtracted an exclusion list -- which lived in the pipeline's
180
- module where this one could not see it. So this list offered
181
- `spawn_subagent` and `remember`, the two tools R4 and M17 exist to
182
- keep out of a grant, and R4's argument is ABOUT this sign-off.
183
- Now both read one per-tool declaration.
184
-
185
- EMPTY FOR EVERY SHIPPED AGENT, on every install, and that is a
186
- ruled scope rather than a gap (TECHNICAL_DEBT 83, batch 139).
187
- The pool this returns from is the gated-with-no-approval_check
188
- set -- the MCP tools plus `write_project_doc` -- and the
189
- intersection with `child_context` is what empties it: all six
190
- spawnable shipped agents narrow `allowed_tools` to built-ins,
191
- and an agent file cannot name `mcp__server__tool` for a server
192
- that did not exist when it was written. So the sign-off's
193
- PICKER branch belongs to an agent that declines to narrow, or
194
- one naming a gated tool outright, which is a user- or
195
- project-tier file. A shipped spawn gets the no-candidates
196
- branch, which `tui/screens.py` builds deliberately.
197
-
198
- The LOOSE half of the pair: GRANT_SIGNOFF_ONLY passes here and is
199
- refused by the pipeline. A human answering in the moment, about
200
- one named agent, for one turn, is the consent write_project_doc's
201
- notice was written for; ten unattended passes on one launch-time
202
- tick is not.
203
- """
204
- from tools.base import GRANT_NEVER
205
- from tools.registry import registry
206
-
207
- return sorted(
208
- name for name in registry.headless_hidden(child_context)
209
- if registry.grantable(name)
210
- and registry.grant_policy(name) != GRANT_NEVER
211
- )
212
-
213
- @staticmethod
214
- def system_prompt_for(agent: AgentDef, base_prompt: str,
215
- active_skills=None, context=None,
216
- callable_only: bool = False,
217
- granted=None) -> str:
218
- """The full system prompt for a run AS this agent: base + skill /
219
- agent catalogs, then the agent's own body, then the project's
220
- AGENTS.md iff the agent opts in (use_project_context). One
221
- assembly point so every caller produces the same prompt.
222
-
223
- active_skills (§19) reaches only the catalog, where it MARKS the
224
- skills whose bodies the shell pins separately. Bodies are not
225
- added here -- see with_catalogs' K6 note.
226
-
227
- context (§20 review f19, generalised): the ToolContext this run
228
- will actually use. It reaches with_catalogs, which suppresses a
229
- catalog whose tool the context excludes -- otherwise a restricted
230
- agent is told to "call the load_skill tool" it does not have.
231
- Pass the SAME context the run gets: child_context() for a spawned
232
- subagent, so a parent's restriction suppresses the invitation too;
233
- active_context() for a /agent switch. None means the run is under
234
- global policy only, and both catalogs apply."""
235
- import prompts.system_prompts as system_prompts
236
-
237
- # callable_only / granted (#68): forwarded, not decided here.
238
- # This function knows which agent is running; whether anything
239
- # can answer an approval question is the RUN's fact, and the
240
- # caller is the one holding it. Defaulting to the attended
241
- # answer keeps both TUI call sites correct with no argument.
242
- prompt = system_prompts.with_catalogs(
243
- base_prompt, active_skills, context,
244
- callable_only=callable_only, granted=granted)
245
- prompt = f"{prompt}\n\n## Agent: {agent.name}\n\n{agent.body}"
246
- context = config_loader.context_for_agent(agent)
247
- if context:
248
- prompt = f"{prompt}\n\n## Project context\n\n{context}"
249
- # ROADMAP_v2 §21b: durable memories, the third context tier beside
250
- # the project's AGENTS.md and gated the same way (use_memory).
251
- # Appended HERE and not inside with_catalogs, which feeds
252
- # pass_prompt() -- injecting there would put every memory into all
253
- # ten research passes, which is §19's K6 trap in its second
254
- # instance.
255
- memories = memory_manager.prompt_fragment(agent)
256
- if memories:
257
- prompt = f"{prompt}\n\n{memories}"
258
- return prompt
259
-
260
-
261
- manager = AgentManager()
1
+ """
2
+ agents/manager.py
3
+
4
+ ROADMAP_v2 §18. AgentManager discovers agent `.md` files across the three
5
+ tiers and exposes lookup by name plus the §18 scope-composition rules.
6
+
7
+ Thin by design: discovery, frontmatter parsing, and tier precedence
8
+ (D29: harness > user > project) all live in core/config_loader.py, which
9
+ runs at initialize() time. This file adds only the agent-specific policy
10
+ composition that §18 owns and that §20 (subagent review) will call:
11
+
12
+ * active_context() -- the ToolContext for a session-scoped active
13
+ agent (/agent switch): the agent's own restrictions at depth 0.
14
+ * child_context() -- the ToolContext for a spawned subagent: C6's
15
+ intersection-with-parent (never union) plus a depth increment (C3).
16
+ * system_prompt_for() -- base + catalogs + the agent's body + opt-in
17
+ project context, assembled in one place so the TUI worker and
18
+ spawn_subagent cannot drift apart.
19
+ """
20
+
21
+ from typing import Optional
22
+
23
+ from core import config_loader
24
+ from core.config_loader import AgentDef
25
+ from memories.manager import manager as memory_manager
26
+ from tools.context import ToolContext
27
+
28
+ #: What /grill-me asks, in either shell (§18; shared since ROADMAP_v3 §89).
29
+ GRILL_ME_TASK = "Grill this thread: surface what still needs a decision."
30
+
31
+
32
+ class AgentManager:
33
+ """Lookup + scope composition over the loader's discovered agents."""
34
+
35
+ def get(self, name: str) -> Optional[AgentDef]:
36
+ return config_loader.get_agent(name)
37
+
38
+ def names(self) -> list:
39
+ return sorted(config_loader.get_agents())
40
+
41
+ def all(self) -> dict:
42
+ return config_loader.get_agents()
43
+
44
+ @staticmethod
45
+ def active_context(agent: AgentDef) -> ToolContext:
46
+ """ToolContext for running AS this agent (session-scoped /agent
47
+ switch). The agent's own restrictions at depth 0 -- no parent to
48
+ intersect with, no nesting."""
49
+ allowed = (
50
+ set(agent.allowed_tools)
51
+ if agent.allowed_tools is not None else None
52
+ )
53
+ targets = (
54
+ set(agent.spawn_targets)
55
+ if agent.spawn_targets is not None else None
56
+ )
57
+ return ToolContext(
58
+ allowed_tools=allowed,
59
+ approval_overrides=dict(agent.approval_overrides),
60
+ subagent_depth=0,
61
+ spawn_targets=targets,
62
+ )
63
+
64
+ @staticmethod
65
+ def child_context(
66
+ agent: AgentDef, parent_context: Optional[ToolContext],
67
+ ) -> ToolContext:
68
+ """ToolContext for a subagent spawned WHILE running under
69
+ parent_context. C6: intersection, never union -- a restricted
70
+ parent must not escape its restriction by spawning a permissive
71
+ child. C3: depth increments so the nesting limit is enforceable.
72
+
73
+ An agent that declares no allowed_tools inherits the parent's
74
+ restriction unchanged (it may not widen it either).
75
+
76
+ APPROVAL COMPOSES THE SAME WAY (§14-§18 review f12, decision S2).
77
+ §18's spec sketch built the child's overrides from the agent
78
+ definition alone, which drops the parent's. C6 reasoned about
79
+ allowed_tools and never considered the approval axis, so the same
80
+ escalation it exists to prevent was open one field over: a parent
81
+ declaring `approval_overrides: {web_search: true}` prompts on
82
+ every search, then spawns a child that computes
83
+ approval_needed(web_search) == False and runs it ungated.
84
+
85
+ Union rather than replace. Only True entries carry -- a False is
86
+ indistinguishable from absent under D14's OR, so unioning can
87
+ only ever tighten, which is what makes it safe to do silently.
88
+
89
+ BOTH SIDES ARE FILTERED, and batch 150 is why. The parent's dict
90
+ was filtered and the agent's was merged over it raw, so a `False`
91
+ in the child's frontmatter OVERWROTE the parent's `True` and the
92
+ sentence above was false of the line below it: a parent that
93
+ prompts on every search spawned a child that did not, which is
94
+ the exact escalation the paragraph before this one describes.
95
+ It did not heal one hop further out either -- the next `if v`
96
+ dropped the False rather than restoring the True, so the
97
+ tightening was gone permanently rather than suspended for one
98
+ run. Three of the four quadrants had tests; the one the code got
99
+ wrong was the one with none.
100
+
101
+ The cost is not only the gate. `candidate_approvals` reads this
102
+ same dict, and it is BOTH the sign-off notice and the grant the
103
+ child receives -- measured, the loosened tool left the candidate
104
+ list entirely, so the user was neither asked at call time nor
105
+ told at sign-off time.
106
+
107
+ `active_context` needs no filter and deliberately has none: at
108
+ depth 0 there is no parent to overwrite, a False is inert under
109
+ D14's OR, and the next hop's filter drops it. Measured both
110
+ ways rather than reasoned about, because "the other composer
111
+ looks similar" is how a fix grows past its finding."""
112
+ parent = parent_context or ToolContext()
113
+ if agent.allowed_tools is not None:
114
+ own = set(agent.allowed_tools)
115
+ child_allowed = own & (
116
+ parent.allowed_tools if parent.allowed_tools is not None
117
+ else own
118
+ )
119
+ else:
120
+ child_allowed = (
121
+ set(parent.allowed_tools)
122
+ if parent.allowed_tools is not None else None
123
+ )
124
+ overrides = {k: v for k, v in parent.approval_overrides.items() if v}
125
+ overrides.update(
126
+ {k: v for k, v in agent.approval_overrides.items() if v})
127
+ # SPAWN TARGETS COMPOSE THE SAME WAY, for C6's own reason one
128
+ # field further out. The refusal that reads this field is checked
129
+ # against the context a run holds, so a field that applied only at
130
+ # depth 0 would be escapable by nesting: an agent forbidden to
131
+ # reach `build` could spawn something that was not, and have that
132
+ # one spawn it. Intersection makes the restriction survive the
133
+ # hop, and an agent declaring none inherits its parent's roster
134
+ # unchanged rather than widening back to everything.
135
+ if agent.spawn_targets is not None:
136
+ own_targets = set(agent.spawn_targets)
137
+ child_targets = own_targets & (
138
+ parent.spawn_targets if parent.spawn_targets is not None
139
+ else own_targets
140
+ )
141
+ else:
142
+ child_targets = (
143
+ set(parent.spawn_targets)
144
+ if parent.spawn_targets is not None else None
145
+ )
146
+ return ToolContext(
147
+ allowed_tools=child_allowed,
148
+ approval_overrides=overrides,
149
+ subagent_depth=parent.subagent_depth + 1,
150
+ spawn_targets=child_targets,
151
+ )
152
+
153
+ @staticmethod
154
+ def candidate_approvals(child_context: ToolContext) -> list:
155
+ """Tools the subagent could reach that would each need approval.
156
+
157
+ ONE helper, TWO callers -- the sign-off prompt's notice text and
158
+ the grant the child actually receives. Computing the list twice
159
+ would let what the user approved drift from what the child got,
160
+ which is the whole substance of the sign-off.
161
+
162
+ Params are empty because there are none yet: a path-dependent
163
+ approval_check (file_ops outside the workspace, shell on a
164
+ non-inert command) cannot be resolved before the call exists, so
165
+ those still prompt at call time through the inherited channel.
166
+
167
+ §25 (R2) filters those tools OUT of the list rather than merely
168
+ letting them re-prompt. They were never grantable by name, and the
169
+ loop now enforces that -- so leaving them here would make the
170
+ sign-off notice promise authority the grant does not carry, which
171
+ is worse than not offering it. What the user reads and what the
172
+ child receives have to be the same list; that is the whole
173
+ substance of a sign-off, and this helper exists to keep them one.
174
+
175
+ §25 (R13) adds the second filter, and #67 is why it is here rather
176
+ than only in core/reasoning/authorization.py. Both functions
177
+ answer "what may be pre-granted", both were built from
178
+ headless_hidden + grantable, and only the pipeline's one then
179
+ subtracted an exclusion list -- which lived in the pipeline's
180
+ module where this one could not see it. So this list offered
181
+ `spawn_subagent` and `remember`, the two tools R4 and M17 exist to
182
+ keep out of a grant, and R4's argument is ABOUT this sign-off.
183
+ Now both read one per-tool declaration.
184
+
185
+ EMPTY FOR EVERY SHIPPED AGENT, on every install, and that is a
186
+ ruled scope rather than a gap (TECHNICAL_DEBT 83, batch 139).
187
+ The pool this returns from is the gated-with-no-approval_check
188
+ set -- the MCP tools plus `write_project_doc` -- and the
189
+ intersection with `child_context` is what empties it: all six
190
+ spawnable shipped agents narrow `allowed_tools` to built-ins,
191
+ and an agent file cannot name `mcp__server__tool` for a server
192
+ that did not exist when it was written. So the sign-off's
193
+ PICKER branch belongs to an agent that declines to narrow, or
194
+ one naming a gated tool outright, which is a user- or
195
+ project-tier file. A shipped spawn gets the no-candidates
196
+ branch, which `tui/screens.py` builds deliberately.
197
+
198
+ The LOOSE half of the pair: GRANT_SIGNOFF_ONLY passes here and is
199
+ refused by the pipeline. A human answering in the moment, about
200
+ one named agent, for one turn, is the consent write_project_doc's
201
+ notice was written for; ten unattended passes on one launch-time
202
+ tick is not.
203
+ """
204
+ from tools.base import GRANT_NEVER
205
+ from tools.registry import registry
206
+
207
+ return sorted(
208
+ name for name in registry.headless_hidden(child_context)
209
+ if registry.grantable(name)
210
+ and registry.grant_policy(name) != GRANT_NEVER
211
+ )
212
+
213
+ @staticmethod
214
+ def system_prompt_for(agent: AgentDef, base_prompt: str,
215
+ active_skills=None, context=None,
216
+ callable_only: bool = False,
217
+ granted=None) -> str:
218
+ """The full system prompt for a run AS this agent: base + skill /
219
+ agent catalogs, then the agent's own body, then the project's
220
+ AGENTS.md iff the agent opts in (use_project_context). One
221
+ assembly point so every caller produces the same prompt.
222
+
223
+ active_skills (§19) reaches only the catalog, where it MARKS the
224
+ skills whose bodies the shell pins separately. Bodies are not
225
+ added here -- see with_catalogs' K6 note.
226
+
227
+ context (§20 review f19, generalised): the ToolContext this run
228
+ will actually use. It reaches with_catalogs, which suppresses a
229
+ catalog whose tool the context excludes -- otherwise a restricted
230
+ agent is told to "call the load_skill tool" it does not have.
231
+ Pass the SAME context the run gets: child_context() for a spawned
232
+ subagent, so a parent's restriction suppresses the invitation too;
233
+ active_context() for a /agent switch. None means the run is under
234
+ global policy only, and both catalogs apply."""
235
+ import prompts.system_prompts as system_prompts
236
+
237
+ # callable_only / granted (#68): forwarded, not decided here.
238
+ # This function knows which agent is running; whether anything
239
+ # can answer an approval question is the RUN's fact, and the
240
+ # caller is the one holding it. Defaulting to the attended
241
+ # answer keeps both TUI call sites correct with no argument.
242
+ prompt = system_prompts.with_catalogs(
243
+ base_prompt, active_skills, context,
244
+ callable_only=callable_only, granted=granted)
245
+ prompt = f"{prompt}\n\n## Agent: {agent.name}\n\n{agent.body}"
246
+ context = config_loader.context_for_agent(agent)
247
+ if context:
248
+ prompt = f"{prompt}\n\n## Project context\n\n{context}"
249
+ # ROADMAP_v2 §21b: durable memories, the third context tier beside
250
+ # the project's AGENTS.md and gated the same way (use_memory).
251
+ # Appended HERE and not inside with_catalogs, which feeds
252
+ # pass_prompt() -- injecting there would put every memory into all
253
+ # ten research passes, which is §19's K6 trap in its second
254
+ # instance.
255
+ memories = memory_manager.prompt_fragment(agent)
256
+ if memories:
257
+ prompt = f"{prompt}\n\n{memories}"
258
+ return prompt
259
+
260
+
261
+ manager = AgentManager()