bone-agent 1.3.2 → 1.4.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/README.md +19 -2
- package/config.yaml.example +13 -2
- package/package.json +3 -2
- package/prompts/main/ask_questions.md +31 -0
- package/prompts/main/batch_independent_calls.md +5 -0
- package/prompts/main/casual_interactions.md +11 -0
- package/prompts/main/code_references.md +8 -0
- package/prompts/main/communication_style.md +12 -0
- package/prompts/main/context_reliability.md +12 -0
- package/prompts/main/conversational_tool_calling.md +15 -0
- package/prompts/main/dream.md +50 -0
- package/prompts/main/editing_pattern.md +13 -0
- package/prompts/main/error_handling.md +6 -0
- package/prompts/main/exploration_pattern.md +21 -0
- package/prompts/main/intro.md +1 -0
- package/prompts/main/obsidian.md +16 -0
- package/prompts/main/obsidian_project.md +79 -0
- package/prompts/main/professional_objectivity.md +3 -0
- package/prompts/main/skills.md +3 -0
- package/prompts/main/targeted_searching.md +10 -0
- package/prompts/main/task_lists_pattern.md +8 -0
- package/prompts/main/temp_folder.md +9 -0
- package/prompts/main/think_before_acting.md +10 -0
- package/prompts/main/tone_and_style.md +4 -0
- package/prompts/main/tool_preferences.md +24 -0
- package/prompts/main/trust_subagent_context.md +21 -0
- package/prompts/main/when_to_use_sub_agent.md +7 -0
- package/prompts/micro/ask_questions.md +1 -0
- package/prompts/micro/batch_independent_calls.md +1 -0
- package/prompts/micro/casual_interactions.md +1 -0
- package/prompts/micro/code_references.md +1 -0
- package/prompts/micro/communication_style.md +1 -0
- package/prompts/micro/context_reliability.md +1 -0
- package/prompts/micro/conversational_tool_calling.md +1 -0
- package/prompts/micro/editing_pattern.md +1 -0
- package/prompts/micro/error_handling.md +1 -0
- package/prompts/micro/exploration_pattern.md +1 -0
- package/prompts/micro/intro.md +1 -0
- package/prompts/micro/obsidian.md +4 -0
- package/prompts/micro/obsidian_project.md +5 -0
- package/prompts/micro/professional_objectivity.md +1 -0
- package/prompts/micro/skills.md +1 -0
- package/prompts/micro/targeted_searching.md +1 -0
- package/prompts/micro/task_lists_pattern.md +1 -0
- package/prompts/micro/temp_folder.md +1 -0
- package/prompts/micro/think_before_acting.md +5 -0
- package/prompts/micro/tone_and_style.md +1 -0
- package/prompts/micro/tool_preferences.md +1 -0
- package/prompts/micro/trust_subagent_context.md +1 -0
- package/prompts/micro/when_to_use_sub_agent.md +1 -0
- package/src/core/agentic.py +134 -106
- package/src/core/chat_manager.py +60 -12
- package/src/core/config_manager.py +14 -1
- package/src/core/cron.py +57 -6
- package/src/core/memory.py +3 -90
- package/src/core/metadata.py +75 -0
- package/src/core/skills.py +463 -0
- package/src/core/sub_agent.py +93 -43
- package/src/core/tool_feedback.py +87 -76
- package/src/llm/client.py +7 -2
- package/src/llm/codex_provider.py +350 -0
- package/src/llm/config.py +74 -4
- package/src/llm/prompts.py +261 -502
- package/src/llm/providers.py +28 -7
- package/src/llm/token_tracker.py +32 -1
- package/src/tools/__init__.py +24 -85
- package/src/tools/create_file.py +1 -1
- package/src/tools/directory.py +1 -1
- package/src/tools/edit.py +13 -7
- package/src/tools/file_reader.py +1 -1
- package/src/tools/helpers/__init__.py +1 -7
- package/src/tools/helpers/base.py +65 -16
- package/src/tools/helpers/loader.py +2 -88
- package/src/tools/helpers/path_resolver.py +70 -13
- package/src/tools/helpers/plugin_manifest.py +99 -70
- package/src/tools/review_sub_agent.py +2 -1
- package/src/tools/rg_search.py +119 -35
- package/src/tools/search_plugins.py +140 -72
- package/src/tools/shell.py +3 -3
- package/src/ui/commands.py +470 -33
- package/src/ui/displays.py +27 -1
- package/src/ui/main.py +1 -4
- package/src/ui/tool_confirmation.py +16 -5
- package/src/utils/editor.py +88 -39
- package/src/utils/settings.py +25 -4
- package/src/utils/user_message_logger.py +120 -0
- package/src/utils/validation.py +10 -0
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
"""User skill storage and active session skill helpers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import os
|
|
7
|
+
import re
|
|
8
|
+
import tempfile
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Callable, Generic, TypeVar
|
|
12
|
+
|
|
13
|
+
import yaml
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
MAX_SKILL_BYTES = 32 * 1024
|
|
19
|
+
SKILL_NAME_RE = re.compile(r"^[a-z0-9][a-z0-9_-]{0,63}$")
|
|
20
|
+
_HEADING_RE = re.compile(r"^#\s+(.+?)\s*$")
|
|
21
|
+
_FRONTMATTER_RE = re.compile(r"^---\s*\n(.*?)\n---\s*\n", re.DOTALL)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass
|
|
25
|
+
class SkillSummary:
|
|
26
|
+
name: str
|
|
27
|
+
path: Path
|
|
28
|
+
preview: str
|
|
29
|
+
modified: float
|
|
30
|
+
description: str = ""
|
|
31
|
+
tags: list[str] = field(default_factory=list)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
T = TypeVar("T")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass
|
|
38
|
+
class SearchCandidate(Generic[T]):
|
|
39
|
+
item: T
|
|
40
|
+
text: str
|
|
41
|
+
compact_text: str
|
|
42
|
+
exact_text: str = ""
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass
|
|
46
|
+
class SearchMatch(Generic[T]):
|
|
47
|
+
item: T
|
|
48
|
+
score: float
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class SkillError(ValueError):
|
|
52
|
+
"""Raised when a skill operation cannot be completed."""
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def get_skills_dir() -> Path:
|
|
56
|
+
"""Return the configured skills directory."""
|
|
57
|
+
override = os.environ.get("BONE_SKILLS_DIR")
|
|
58
|
+
if override:
|
|
59
|
+
return Path(override).expanduser().resolve()
|
|
60
|
+
return Path.home() / ".bone" / "skills"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def ensure_skills_dir() -> Path:
|
|
64
|
+
"""Create and return the skills directory."""
|
|
65
|
+
path = get_skills_dir()
|
|
66
|
+
path.mkdir(parents=True, exist_ok=True)
|
|
67
|
+
return path
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def normalize_skill_name(raw: str) -> str:
|
|
71
|
+
"""Normalize a user-provided skill name for filesystem storage."""
|
|
72
|
+
return (raw or "").strip().lower().replace(" ", "_")
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def validate_skill_name(raw: str) -> str:
|
|
76
|
+
"""Validate and return a normalized skill name."""
|
|
77
|
+
name = normalize_skill_name(raw)
|
|
78
|
+
if not SKILL_NAME_RE.fullmatch(name):
|
|
79
|
+
raise SkillError(
|
|
80
|
+
"Invalid skill name. Use lowercase letters, numbers, underscores, "
|
|
81
|
+
"or hyphens; start with a letter or number."
|
|
82
|
+
)
|
|
83
|
+
if "/" in name or "\\" in name or name.startswith(".") or ".." in name:
|
|
84
|
+
raise SkillError("Invalid skill name.")
|
|
85
|
+
return name
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def get_skill_path(name: str) -> Path:
|
|
89
|
+
"""Return the safe path for a skill name."""
|
|
90
|
+
valid_name = validate_skill_name(name)
|
|
91
|
+
base = ensure_skills_dir().resolve()
|
|
92
|
+
return base / f"{valid_name}.md"
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _check_size(content: str) -> None:
|
|
96
|
+
if len(content.encode("utf-8")) > MAX_SKILL_BYTES:
|
|
97
|
+
raise SkillError(f"Skill is too large. Maximum size is {MAX_SKILL_BYTES} bytes.")
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _parse_frontmatter(content: str) -> tuple[dict, str]:
|
|
101
|
+
"""Extract YAML frontmatter and remaining body from content.
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
(metadata_dict, body_text). metadata_dict may be empty.
|
|
105
|
+
|
|
106
|
+
Notes:
|
|
107
|
+
If a frontmatter block is present but invalid, preserve the original content
|
|
108
|
+
as body so callers do not silently discard user-authored metadata.
|
|
109
|
+
"""
|
|
110
|
+
match = _FRONTMATTER_RE.match(content)
|
|
111
|
+
if not match:
|
|
112
|
+
return {}, content
|
|
113
|
+
try:
|
|
114
|
+
meta = yaml.safe_load(match.group(1)) or {}
|
|
115
|
+
except yaml.YAMLError:
|
|
116
|
+
return {}, content
|
|
117
|
+
if not isinstance(meta, dict):
|
|
118
|
+
return {}, content
|
|
119
|
+
body = content[match.end():]
|
|
120
|
+
return meta, body
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _normalize_description(value: object) -> str:
|
|
124
|
+
text = str(value or "").strip()
|
|
125
|
+
return text
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _normalize_tags(value: object) -> list[str]:
|
|
129
|
+
if value is None:
|
|
130
|
+
return []
|
|
131
|
+
if isinstance(value, str):
|
|
132
|
+
candidates = [value]
|
|
133
|
+
elif isinstance(value, (list, tuple, set)):
|
|
134
|
+
candidates = list(value)
|
|
135
|
+
else:
|
|
136
|
+
candidates = [value]
|
|
137
|
+
|
|
138
|
+
tags: list[str] = []
|
|
139
|
+
for candidate in candidates:
|
|
140
|
+
tag = str(candidate or "").strip()
|
|
141
|
+
if tag:
|
|
142
|
+
tags.append(tag)
|
|
143
|
+
return tags
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _render_frontmatter(description: str, tags: list[str]) -> str:
|
|
147
|
+
"""Render YAML frontmatter block for a skill file."""
|
|
148
|
+
if not description and not tags:
|
|
149
|
+
return ""
|
|
150
|
+
meta = {}
|
|
151
|
+
if description:
|
|
152
|
+
meta["description"] = description
|
|
153
|
+
if tags:
|
|
154
|
+
meta["tags"] = tags
|
|
155
|
+
return f"---\n{yaml.dump(meta, default_flow_style=False).strip()}\n---\n"
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _needs_metadata(meta: dict) -> bool:
|
|
159
|
+
"""Check if frontmatter is missing description or tags."""
|
|
160
|
+
return not meta.get("description") or not meta.get("tags")
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _strip_heading(name: str, content: str) -> str:
|
|
164
|
+
lines = content.splitlines()
|
|
165
|
+
if not lines:
|
|
166
|
+
return ""
|
|
167
|
+
match = _HEADING_RE.match(lines[0])
|
|
168
|
+
if match and normalize_skill_name(match.group(1)) == normalize_skill_name(name):
|
|
169
|
+
return "\n".join(lines[1:]).strip()
|
|
170
|
+
return content.strip()
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def format_skill_file(name: str, content: str, *, description: str = "", tags: list[str] | None = None) -> str:
|
|
174
|
+
"""Format a skill as a markdown file with optional frontmatter and title heading."""
|
|
175
|
+
valid_name = validate_skill_name(name)
|
|
176
|
+
body = _strip_heading(valid_name, content)
|
|
177
|
+
if not body:
|
|
178
|
+
raise SkillError("Skill prompt cannot be empty.")
|
|
179
|
+
|
|
180
|
+
frontmatter = _render_frontmatter(description, tags or [])
|
|
181
|
+
formatted = f"{frontmatter}# {valid_name}\n\n{body.strip()}\n"
|
|
182
|
+
_check_size(formatted)
|
|
183
|
+
return formatted
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def read_skill(name: str, strip_heading: bool = True) -> str:
|
|
187
|
+
"""Read a skill body by name.
|
|
188
|
+
|
|
189
|
+
Returns the prompt body without frontmatter or heading (unless strip_heading=False,
|
|
190
|
+
in which case frontmatter is still stripped but heading is kept).
|
|
191
|
+
"""
|
|
192
|
+
path = get_skill_path(name)
|
|
193
|
+
if path.is_symlink():
|
|
194
|
+
raise SkillError("Refusing to read a symlinked skill.")
|
|
195
|
+
if not path.is_file():
|
|
196
|
+
raise SkillError(f"Skill '{validate_skill_name(name)}' not found.")
|
|
197
|
+
content = path.read_text(encoding="utf-8")
|
|
198
|
+
_, body = _parse_frontmatter(content)
|
|
199
|
+
if strip_heading:
|
|
200
|
+
return _strip_heading(name, body)
|
|
201
|
+
return body.strip()
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def write_skill(name: str, content: str, overwrite: bool = False) -> Path:
|
|
205
|
+
"""Create or replace a skill file.
|
|
206
|
+
|
|
207
|
+
If the content contains YAML frontmatter with description and tags, those are
|
|
208
|
+
preserved. Otherwise, metadata is auto-generated from the content via the LLM.
|
|
209
|
+
"""
|
|
210
|
+
valid_name = validate_skill_name(name)
|
|
211
|
+
path = get_skill_path(valid_name)
|
|
212
|
+
if path.exists() and not overwrite:
|
|
213
|
+
raise SkillError(f"Skill '{valid_name}' already exists.")
|
|
214
|
+
|
|
215
|
+
# Parse any existing frontmatter from the content
|
|
216
|
+
body = content
|
|
217
|
+
description = ""
|
|
218
|
+
tags: list[str] = []
|
|
219
|
+
|
|
220
|
+
# Check if the raw content has frontmatter already
|
|
221
|
+
raw_meta, raw_body = _parse_frontmatter(content)
|
|
222
|
+
if raw_meta:
|
|
223
|
+
description = _normalize_description(raw_meta.get("description", ""))
|
|
224
|
+
tags = _normalize_tags(raw_meta.get("tags"))
|
|
225
|
+
body = raw_body
|
|
226
|
+
|
|
227
|
+
# If still missing metadata, try to preserve from existing file
|
|
228
|
+
if _needs_metadata({"description": description, "tags": tags}) and path.is_file():
|
|
229
|
+
existing_content = path.read_text(encoding="utf-8")
|
|
230
|
+
existing_meta, _ = _parse_frontmatter(existing_content)
|
|
231
|
+
if not description and existing_meta.get("description"):
|
|
232
|
+
description = _normalize_description(existing_meta["description"])
|
|
233
|
+
if not tags and existing_meta.get("tags"):
|
|
234
|
+
tags = _normalize_tags(existing_meta.get("tags"))
|
|
235
|
+
|
|
236
|
+
# If still missing, auto-generate
|
|
237
|
+
if _needs_metadata({"description": description, "tags": tags}):
|
|
238
|
+
prompt_body = _strip_heading(valid_name, body)
|
|
239
|
+
if prompt_body:
|
|
240
|
+
from core.metadata import generate_metadata
|
|
241
|
+
generated = generate_metadata(prompt_body, valid_name)
|
|
242
|
+
generated_description = _normalize_description(generated.get("description", ""))
|
|
243
|
+
generated_tags = _normalize_tags(generated.get("tags"))
|
|
244
|
+
if not description:
|
|
245
|
+
description = generated_description
|
|
246
|
+
if not tags:
|
|
247
|
+
tags = generated_tags
|
|
248
|
+
|
|
249
|
+
formatted = format_skill_file(valid_name, body, description=description, tags=tags)
|
|
250
|
+
_atomic_write(path, formatted)
|
|
251
|
+
return path
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def remove_skill(name: str) -> Path:
|
|
255
|
+
"""Remove a skill file."""
|
|
256
|
+
path = get_skill_path(name)
|
|
257
|
+
if not path.is_file():
|
|
258
|
+
raise SkillError(f"Skill '{validate_skill_name(name)}' not found.")
|
|
259
|
+
if path.is_symlink():
|
|
260
|
+
raise SkillError("Refusing to remove a symlinked skill.")
|
|
261
|
+
path.unlink()
|
|
262
|
+
return path
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def list_skills(query: str | None = None) -> list[SkillSummary]:
|
|
266
|
+
"""List stored skills, optionally filtering by name/body preview."""
|
|
267
|
+
return [match.item for match in search_skill_matches(query=query)]
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def iter_skill_summaries() -> list[SkillSummary]:
|
|
271
|
+
"""Return all valid stored skill summaries."""
|
|
272
|
+
base = ensure_skills_dir()
|
|
273
|
+
summaries: list[SkillSummary] = []
|
|
274
|
+
|
|
275
|
+
for path in sorted(base.glob("*.md")):
|
|
276
|
+
if not path.is_file() or path.is_symlink():
|
|
277
|
+
continue
|
|
278
|
+
try:
|
|
279
|
+
name = validate_skill_name(path.stem)
|
|
280
|
+
raw = path.read_text(encoding="utf-8")
|
|
281
|
+
meta, body_text = _parse_frontmatter(raw)
|
|
282
|
+
heading_stripped = _strip_heading(name, body_text)
|
|
283
|
+
except SkillError:
|
|
284
|
+
continue
|
|
285
|
+
|
|
286
|
+
summaries.append(
|
|
287
|
+
SkillSummary(
|
|
288
|
+
name=name,
|
|
289
|
+
path=path,
|
|
290
|
+
preview=_preview(heading_stripped),
|
|
291
|
+
modified=path.stat().st_mtime,
|
|
292
|
+
description=_normalize_description(meta.get("description", "")),
|
|
293
|
+
tags=_normalize_tags(meta.get("tags")),
|
|
294
|
+
)
|
|
295
|
+
)
|
|
296
|
+
return summaries
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def search_candidates(
|
|
300
|
+
query: str,
|
|
301
|
+
candidates: list[SearchCandidate[T]],
|
|
302
|
+
*,
|
|
303
|
+
max_results: int = 5,
|
|
304
|
+
item_key: Callable[[T], str] | None = None,
|
|
305
|
+
) -> list[SearchMatch[T]]:
|
|
306
|
+
"""Score and return matching candidates in descending relevance order."""
|
|
307
|
+
query_text = (query or "").strip().lower()
|
|
308
|
+
if not query_text:
|
|
309
|
+
matches = [SearchMatch(item=candidate.item, score=0.0) for candidate in candidates]
|
|
310
|
+
if item_key is not None:
|
|
311
|
+
matches.sort(key=lambda match: item_key(match.item))
|
|
312
|
+
return matches[:max_results]
|
|
313
|
+
|
|
314
|
+
query_compact = _compact_match_text(query_text)
|
|
315
|
+
query_terms = [term for term in query_text.split() if term]
|
|
316
|
+
scored: list[SearchMatch[T]] = []
|
|
317
|
+
|
|
318
|
+
for candidate in candidates:
|
|
319
|
+
text = candidate.text.lower()
|
|
320
|
+
compact_text = candidate.compact_text or _compact_match_text(text)
|
|
321
|
+
exact_text = (candidate.exact_text or "").lower()
|
|
322
|
+
score = 0.0
|
|
323
|
+
|
|
324
|
+
if exact_text and query_text == exact_text:
|
|
325
|
+
score += 120.0
|
|
326
|
+
if exact_text and query_text in exact_text:
|
|
327
|
+
score += 60.0
|
|
328
|
+
if query_text in text:
|
|
329
|
+
score += 40.0
|
|
330
|
+
if query_compact and query_compact in compact_text:
|
|
331
|
+
score += 25.0
|
|
332
|
+
|
|
333
|
+
for term in query_terms:
|
|
334
|
+
if exact_text and term in exact_text:
|
|
335
|
+
score += 15.0
|
|
336
|
+
if term in text:
|
|
337
|
+
score += 10.0
|
|
338
|
+
|
|
339
|
+
if score > 0:
|
|
340
|
+
scored.append(SearchMatch(item=candidate.item, score=score))
|
|
341
|
+
|
|
342
|
+
scored.sort(
|
|
343
|
+
key=lambda match: (
|
|
344
|
+
-match.score,
|
|
345
|
+
item_key(match.item) if item_key is not None else "",
|
|
346
|
+
)
|
|
347
|
+
)
|
|
348
|
+
return scored[:max_results]
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def search_skill_matches(query: str | None = None, max_results: int = 20) -> list[SearchMatch[SkillSummary]]:
|
|
352
|
+
"""Return scored skill matches for discovery surfaces."""
|
|
353
|
+
skills = iter_skill_summaries()
|
|
354
|
+
candidates = [
|
|
355
|
+
SearchCandidate(
|
|
356
|
+
item=skill,
|
|
357
|
+
text=" ".join(
|
|
358
|
+
part
|
|
359
|
+
for part in [
|
|
360
|
+
skill.name,
|
|
361
|
+
skill.description,
|
|
362
|
+
skill.preview,
|
|
363
|
+
" ".join(skill.tags),
|
|
364
|
+
]
|
|
365
|
+
if part
|
|
366
|
+
),
|
|
367
|
+
compact_text=_compact_match_text(
|
|
368
|
+
" ".join(
|
|
369
|
+
part
|
|
370
|
+
for part in [skill.name, skill.description, " ".join(skill.tags)]
|
|
371
|
+
if part
|
|
372
|
+
)
|
|
373
|
+
),
|
|
374
|
+
exact_text=skill.name,
|
|
375
|
+
)
|
|
376
|
+
for skill in skills
|
|
377
|
+
]
|
|
378
|
+
return search_candidates(
|
|
379
|
+
query or "",
|
|
380
|
+
candidates,
|
|
381
|
+
max_results=max_results,
|
|
382
|
+
item_key=lambda skill: skill.name,
|
|
383
|
+
)
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
def activate_skill(chat_manager, name: str, content: str | None = None, reload: bool = False) -> int:
|
|
387
|
+
"""Activate a skill in session state and refresh the system prompt."""
|
|
388
|
+
valid_name = validate_skill_name(name)
|
|
389
|
+
body = (content if content is not None else read_skill(valid_name)).strip()
|
|
390
|
+
if not body:
|
|
391
|
+
raise SkillError("Skill prompt cannot be empty.")
|
|
392
|
+
|
|
393
|
+
loaded_skills = getattr(chat_manager, "loaded_skills", None)
|
|
394
|
+
if loaded_skills is None:
|
|
395
|
+
loaded_skills = set()
|
|
396
|
+
setattr(chat_manager, "loaded_skills", loaded_skills)
|
|
397
|
+
if valid_name in loaded_skills and not reload:
|
|
398
|
+
raise SkillError(f"Skill '{valid_name}' is already active in this chat.")
|
|
399
|
+
|
|
400
|
+
loaded_skills.add(valid_name)
|
|
401
|
+
if hasattr(chat_manager, "update_system_prompt"):
|
|
402
|
+
chat_manager.update_system_prompt()
|
|
403
|
+
else:
|
|
404
|
+
chat_manager._update_context_tokens()
|
|
405
|
+
|
|
406
|
+
return chat_manager.token_tracker.estimate_tokens(render_active_skills_section([valid_name]))
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def get_active_skill_contents(skill_names: list[str] | set[str] | tuple[str, ...]) -> list[tuple[str, str]]:
|
|
410
|
+
"""Return validated active skill name/body pairs sorted by skill name."""
|
|
411
|
+
active_skills = []
|
|
412
|
+
for raw_name in sorted({validate_skill_name(name) for name in skill_names}):
|
|
413
|
+
body = read_skill(raw_name)
|
|
414
|
+
if body:
|
|
415
|
+
active_skills.append((raw_name, body))
|
|
416
|
+
return active_skills
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def render_active_skills_section(skill_names: list[str] | set[str] | tuple[str, ...]) -> str:
|
|
420
|
+
"""Render active skills for inclusion in the system prompt."""
|
|
421
|
+
try:
|
|
422
|
+
active_skills = get_active_skill_contents(skill_names)
|
|
423
|
+
except SkillError:
|
|
424
|
+
active_skills = []
|
|
425
|
+
if not active_skills:
|
|
426
|
+
return ""
|
|
427
|
+
|
|
428
|
+
sections = ["## Active skills", "Apply these active skill instructions in addition to the base prompt."]
|
|
429
|
+
for name, body in active_skills:
|
|
430
|
+
sections.append(f"### {name}\n{body}")
|
|
431
|
+
return "\n\n".join(sections)
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def _preview(content: str, max_chars: int = 90) -> str:
|
|
435
|
+
text = " ".join(content.split())
|
|
436
|
+
if len(text) <= max_chars:
|
|
437
|
+
return text
|
|
438
|
+
return text[: max_chars - 3].rstrip() + "..."
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
def _compact_match_text(text: str) -> str:
|
|
442
|
+
return re.sub(r"[^a-z0-9]+", "", (text or "").lower())
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def _atomic_write(path: Path, content: str) -> None:
|
|
446
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
447
|
+
fd, tmp_name = tempfile.mkstemp(
|
|
448
|
+
prefix=f".{path.stem}.",
|
|
449
|
+
suffix=".tmp",
|
|
450
|
+
dir=str(path.parent),
|
|
451
|
+
text=True,
|
|
452
|
+
)
|
|
453
|
+
tmp_path = Path(tmp_name)
|
|
454
|
+
try:
|
|
455
|
+
with os.fdopen(fd, "w", encoding="utf-8", newline="\n") as handle:
|
|
456
|
+
handle.write(content)
|
|
457
|
+
tmp_path.replace(path)
|
|
458
|
+
except Exception:
|
|
459
|
+
try:
|
|
460
|
+
tmp_path.unlink()
|
|
461
|
+
except OSError:
|
|
462
|
+
pass
|
|
463
|
+
raise
|
package/src/core/sub_agent.py
CHANGED
|
@@ -4,11 +4,12 @@ Uses existing AgenticOrchestrator with isolated message context
|
|
|
4
4
|
and read-only tools to execute generic delegated tasks.
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
|
-
from pathlib import Path
|
|
8
|
-
|
|
9
|
-
from core.chat_manager import ChatManager
|
|
10
|
-
from
|
|
11
|
-
from
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from core.chat_manager import ChatManager
|
|
10
|
+
from exceptions import LLMError
|
|
11
|
+
from llm.prompts import build_sub_agent_prompt
|
|
12
|
+
from utils.settings import sub_agent_settings
|
|
12
13
|
|
|
13
14
|
|
|
14
15
|
class HardLimitExceeded(Exception):
|
|
@@ -16,6 +17,11 @@ class HardLimitExceeded(Exception):
|
|
|
16
17
|
pass
|
|
17
18
|
|
|
18
19
|
|
|
20
|
+
class BilledLimitExceeded(Exception):
|
|
21
|
+
"""Raised when the sub-agent hits its cumulative billed token limit."""
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
|
|
19
25
|
def _format_messages_dump(messages) -> str:
|
|
20
26
|
"""Format sub-agent message history as a markdown dump.
|
|
21
27
|
|
|
@@ -89,7 +95,7 @@ def _inject_system_prompt(chat_manager, sub_agent_type: str = "research"):
|
|
|
89
95
|
chat_manager.messages = [{"role": "system", "content": base_prompt}]
|
|
90
96
|
|
|
91
97
|
|
|
92
|
-
def _load_codebase_map(chat_manager):
|
|
98
|
+
def _load_codebase_map(chat_manager):
|
|
93
99
|
"""Load agents.md codebase map into sub-agent context if available.
|
|
94
100
|
|
|
95
101
|
Args:
|
|
@@ -98,19 +104,13 @@ def _load_codebase_map(chat_manager):
|
|
|
98
104
|
agents_path = Path.cwd() / "agents.md"
|
|
99
105
|
if agents_path.exists():
|
|
100
106
|
map_content = agents_path.read_text(encoding="utf-8").strip()
|
|
101
|
-
user_msg = (
|
|
102
|
-
"Here is the codebase map for this project. "
|
|
103
|
-
"This provides an overview of the repository structure and file purposes. "
|
|
104
|
-
"Use this as a reference when exploring the codebase.\n\n"
|
|
105
|
-
f"## Codebase Map (auto-generated from agents.md)\n\n{map_content}"
|
|
106
|
-
)
|
|
107
|
-
|
|
108
|
-
"I've received the codebase map. I'll use this as a reference when "
|
|
109
|
-
"exploring the repository, but I'll always verify current state by "
|
|
110
|
-
"reading files and searching the codebase before making changes."
|
|
111
|
-
)
|
|
112
|
-
chat_manager.messages.append({"role": "user", "content": user_msg})
|
|
113
|
-
chat_manager.messages.append({"role": "assistant", "content": assistant_msg})
|
|
107
|
+
user_msg = (
|
|
108
|
+
"Here is the codebase map for this project. "
|
|
109
|
+
"This provides an overview of the repository structure and file purposes. "
|
|
110
|
+
"Use this as a reference when exploring the codebase.\n\n"
|
|
111
|
+
f"## Codebase Map (auto-generated from agents.md)\n\n{map_content}"
|
|
112
|
+
)
|
|
113
|
+
chat_manager.messages.append({"role": "user", "content": user_msg})
|
|
114
114
|
|
|
115
115
|
|
|
116
116
|
def _configure_isolation(chat_manager):
|
|
@@ -183,14 +183,11 @@ def run_sub_agent(
|
|
|
183
183
|
# Create fresh ChatManager for sub-agent
|
|
184
184
|
temp_chat_manager = _create_chat_manager(sub_agent_type=sub_agent_type)
|
|
185
185
|
|
|
186
|
-
# Inject initial context as a user/assistant exchange if provided
|
|
187
|
-
if initial_context:
|
|
188
|
-
temp_chat_manager.messages.append(
|
|
189
|
-
{"role": "user", "content": initial_context}
|
|
190
|
-
)
|
|
191
|
-
temp_chat_manager.messages.append(
|
|
192
|
-
{"role": "assistant", "content": "I've received the context. I'll analyze it and use the available tools to gather additional information as needed."}
|
|
193
|
-
)
|
|
186
|
+
# Inject initial context as a user/assistant exchange if provided
|
|
187
|
+
if initial_context:
|
|
188
|
+
temp_chat_manager.messages.append(
|
|
189
|
+
{"role": "user", "content": initial_context}
|
|
190
|
+
)
|
|
194
191
|
|
|
195
192
|
# Import here to avoid circular import with core.agentic
|
|
196
193
|
from core.agentic import AgenticOrchestrator
|
|
@@ -216,27 +213,40 @@ def run_sub_agent(
|
|
|
216
213
|
original_chat_completion = temp_chat_manager.client.chat_completion
|
|
217
214
|
|
|
218
215
|
_soft_limit_warned = False
|
|
216
|
+
_billed_warning_sent = False
|
|
219
217
|
|
|
220
218
|
def _chat_completion_with_token_hint(messages, **kwargs):
|
|
221
|
-
"""Prepend a system-level token budget hint
|
|
222
|
-
nonlocal _soft_limit_warned
|
|
219
|
+
"""Prepend a system-level token budget hint and one-time warnings to every LLM call."""
|
|
220
|
+
nonlocal _soft_limit_warned, _billed_warning_sent
|
|
223
221
|
tt = temp_chat_manager.token_tracker
|
|
224
|
-
hint = f"[Token budget: {tt.current_context_tokens:,} curr / {tt.conv_total_tokens:,} total]"
|
|
222
|
+
hint = f"[Token budget: {tt.current_context_tokens:,} curr / {tt.conv_total_tokens:,} total billed]"
|
|
223
|
+
warnings = []
|
|
225
224
|
|
|
226
225
|
if not _soft_limit_warned and tt.current_context_tokens >= sub_agent_settings.soft_limit_tokens:
|
|
227
226
|
_soft_limit_warned = True
|
|
228
|
-
|
|
229
|
-
f"WARNING: You have exceeded the soft token limit "
|
|
227
|
+
warnings.append(
|
|
228
|
+
f"WARNING: You have exceeded the current-context soft token limit "
|
|
230
229
|
f"({tt.current_context_tokens:,} / {sub_agent_settings.soft_limit_tokens:,}). "
|
|
231
|
-
"STOP exploring and return your findings immediately. Do NOT call any more tools.
|
|
232
|
-
+ hint
|
|
230
|
+
"STOP exploring and return your findings immediately. Do NOT call any more tools."
|
|
233
231
|
)
|
|
234
232
|
|
|
233
|
+
if not _billed_warning_sent and tt.conv_total_tokens >= sub_agent_settings.billed_warning_tokens:
|
|
234
|
+
_billed_warning_sent = True
|
|
235
|
+
warnings.append(
|
|
236
|
+
f"WARNING: You have exceeded the cumulative billed token warning limit "
|
|
237
|
+
f"({tt.conv_total_tokens:,} / {sub_agent_settings.billed_warning_tokens:,}). "
|
|
238
|
+
"This sub-agent may be running away. STOP exploring and return your findings immediately. "
|
|
239
|
+
"Do NOT call any more tools."
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
if warnings:
|
|
243
|
+
hint = "\n".join([*warnings, hint])
|
|
244
|
+
|
|
235
245
|
token_msg = {"role": "system", "content": hint}
|
|
236
246
|
return original_chat_completion([token_msg, *messages], **kwargs)
|
|
237
247
|
|
|
238
|
-
def _get_llm_response_with_hard_limit(allowed_tools=None):
|
|
239
|
-
"""Wrapper to check
|
|
248
|
+
def _get_llm_response_with_hard_limit(allowed_tools=None, allow_active_plugins=False):
|
|
249
|
+
"""Wrapper to check context and billed token limits and update panel state."""
|
|
240
250
|
tt = temp_chat_manager.token_tracker
|
|
241
251
|
|
|
242
252
|
# Check hard token limit before making LLM call
|
|
@@ -248,6 +258,19 @@ def run_sub_agent(
|
|
|
248
258
|
f"{tt.current_context_tokens:,} / {sub_agent_settings.hard_limit_tokens:,} tokens."
|
|
249
259
|
)
|
|
250
260
|
|
|
261
|
+
# Check cumulative billed tokens to stop runaway sub-agents even when
|
|
262
|
+
# current context remains below the prompt-size hard limit.
|
|
263
|
+
#
|
|
264
|
+
# Note: the billed warning is injected by _chat_completion_with_token_hint
|
|
265
|
+
# on the next chat_completion call. This hard stop runs before each LLM
|
|
266
|
+
# response, so once we hit the billed hard limit the warning may never be
|
|
267
|
+
# delivered if no further chat_completion call is made.
|
|
268
|
+
if tt.conv_total_tokens >= sub_agent_settings.billed_hard_limit_tokens:
|
|
269
|
+
raise BilledLimitExceeded(
|
|
270
|
+
f"Sub-agent billed token limit exceeded: "
|
|
271
|
+
f"{tt.conv_total_tokens:,} / {sub_agent_settings.billed_hard_limit_tokens:,} tokens."
|
|
272
|
+
)
|
|
273
|
+
|
|
251
274
|
# Update panel with live token counts
|
|
252
275
|
# Order: conversation length (current context) first, total tokens billed second
|
|
253
276
|
conv_length = tt.current_context_tokens
|
|
@@ -256,27 +279,45 @@ def run_sub_agent(
|
|
|
256
279
|
panel_updater.token_info = f"{conv_length:,} curr | {total_billed:,} total"
|
|
257
280
|
panel_updater.append("") # Refresh panel title
|
|
258
281
|
|
|
259
|
-
return original_get_llm_response(
|
|
282
|
+
return original_get_llm_response(
|
|
283
|
+
allowed_tools=allowed_tools,
|
|
284
|
+
allow_active_plugins=allow_active_plugins,
|
|
285
|
+
)
|
|
260
286
|
|
|
261
287
|
# Apply both patches once, before the orchestrator loop starts
|
|
262
288
|
orchestrator._get_llm_response = _get_llm_response_with_hard_limit
|
|
263
289
|
temp_chat_manager.client.chat_completion = _chat_completion_with_token_hint
|
|
264
290
|
|
|
265
291
|
hard_limit_exceeded = False
|
|
292
|
+
billed_limit_exceeded = False
|
|
266
293
|
|
|
267
294
|
try:
|
|
268
295
|
# Run sub-agent task
|
|
269
296
|
orchestrator.run(
|
|
270
297
|
task_query,
|
|
271
298
|
thinking_indicator=None,
|
|
272
|
-
allowed_tools=sub_agent_settings.allowed_tools
|
|
299
|
+
allowed_tools=sub_agent_settings.allowed_tools,
|
|
300
|
+
allow_active_plugins=sub_agent_settings.allow_active_plugins,
|
|
273
301
|
)
|
|
274
302
|
except HardLimitExceeded:
|
|
275
303
|
hard_limit_exceeded = True
|
|
276
|
-
except
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
return {
|
|
304
|
+
except BilledLimitExceeded:
|
|
305
|
+
billed_limit_exceeded = True
|
|
306
|
+
except LLMError as e:
|
|
307
|
+
return {
|
|
308
|
+
"result": "",
|
|
309
|
+
"usage": {
|
|
310
|
+
"prompt_tokens": 0,
|
|
311
|
+
"completion_tokens": 0,
|
|
312
|
+
"total_tokens": 0
|
|
313
|
+
},
|
|
314
|
+
"model": temp_chat_manager.client.model,
|
|
315
|
+
"error": str(e)
|
|
316
|
+
}
|
|
317
|
+
except Exception as e:
|
|
318
|
+
import traceback
|
|
319
|
+
error_details = f"{e}\n\nTraceback:\n{traceback.format_exc()}"
|
|
320
|
+
return {
|
|
280
321
|
"result": "",
|
|
281
322
|
"usage": {
|
|
282
323
|
"prompt_tokens": 0,
|
|
@@ -306,7 +347,15 @@ def run_sub_agent(
|
|
|
306
347
|
if msg.get("role") == "assistant" and msg.get("content"):
|
|
307
348
|
final_content = msg["content"].strip()
|
|
308
349
|
break
|
|
309
|
-
|
|
350
|
+
|
|
351
|
+
if billed_limit_exceeded:
|
|
352
|
+
prefix = (
|
|
353
|
+
"WARNING: Sub-agent billed token limit reached. "
|
|
354
|
+
"Returning current findings early to prevent runaway execution."
|
|
355
|
+
)
|
|
356
|
+
result = f"{prefix}\n\n{final_content}" if final_content else prefix
|
|
357
|
+
else:
|
|
358
|
+
result = final_content
|
|
310
359
|
|
|
311
360
|
usage = {
|
|
312
361
|
"prompt_tokens": delta_prompt,
|
|
@@ -323,4 +372,5 @@ def run_sub_agent(
|
|
|
323
372
|
"model": temp_chat_manager.client.model,
|
|
324
373
|
"error": None,
|
|
325
374
|
"hard_limit_exceeded": hard_limit_exceeded,
|
|
375
|
+
"billed_limit_exceeded": billed_limit_exceeded,
|
|
326
376
|
}
|