rigcode 0.1.0__py3-none-any.whl

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 (64) hide show
  1. rigcode/__init__.py +4 -0
  2. rigcode/__main__.py +4 -0
  3. rigcode/agent/__init__.py +0 -0
  4. rigcode/agent/loop.py +627 -0
  5. rigcode/agent/mentions.py +70 -0
  6. rigcode/agent/prompts.py +195 -0
  7. rigcode/agent/runtime.py +535 -0
  8. rigcode/agent/session.py +165 -0
  9. rigcode/agent/state.py +161 -0
  10. rigcode/cli.py +283 -0
  11. rigcode/config.py +284 -0
  12. rigcode/doctor.py +158 -0
  13. rigcode/extensions.py +275 -0
  14. rigcode/hooks.py +142 -0
  15. rigcode/llm/__init__.py +0 -0
  16. rigcode/llm/client.py +259 -0
  17. rigcode/llm/probe.py +229 -0
  18. rigcode/llm/stream.py +240 -0
  19. rigcode/llm/textmode.py +161 -0
  20. rigcode/llm/types.py +92 -0
  21. rigcode/llm/wire.py +76 -0
  22. rigcode/mcp.py +329 -0
  23. rigcode/permissions.py +221 -0
  24. rigcode/prompts/agents/coder.md +13 -0
  25. rigcode/prompts/agents/debugger.md +14 -0
  26. rigcode/prompts/agents/explore.md +22 -0
  27. rigcode/prompts/agents/plan.md +21 -0
  28. rigcode/prompts/agents/researcher.md +14 -0
  29. rigcode/prompts/agents/reviewer.md +18 -0
  30. rigcode/prompts/agents/verifier.md +15 -0
  31. rigcode/prompts/commit.md +5 -0
  32. rigcode/prompts/compact.md +24 -0
  33. rigcode/prompts/init.md +10 -0
  34. rigcode/prompts/system.md +27 -0
  35. rigcode/tools/__init__.py +0 -0
  36. rigcode/tools/base.py +245 -0
  37. rigcode/tools/bashsafety.py +262 -0
  38. rigcode/tools/editmatch.py +367 -0
  39. rigcode/tools/files.py +534 -0
  40. rigcode/tools/interact.py +69 -0
  41. rigcode/tools/search.py +309 -0
  42. rigcode/tools/shell.py +424 -0
  43. rigcode/tools/skill.py +37 -0
  44. rigcode/tools/task.py +54 -0
  45. rigcode/tools/todo.py +62 -0
  46. rigcode/tools/web.py +346 -0
  47. rigcode/ui/__init__.py +0 -0
  48. rigcode/ui/base.py +69 -0
  49. rigcode/ui/commands.py +581 -0
  50. rigcode/ui/plain.py +321 -0
  51. rigcode/ui/render.py +207 -0
  52. rigcode/ui/terminal.py +1154 -0
  53. rigcode/utils/__init__.py +0 -0
  54. rigcode/utils/files.py +176 -0
  55. rigcode/utils/frontmatter.py +38 -0
  56. rigcode/utils/globmatch.py +91 -0
  57. rigcode/utils/jsonrepair.py +110 -0
  58. rigcode/utils/paths.py +83 -0
  59. rigcode/utils/text.py +105 -0
  60. rigcode-0.1.0.dist-info/METADATA +203 -0
  61. rigcode-0.1.0.dist-info/RECORD +64 -0
  62. rigcode-0.1.0.dist-info/WHEEL +4 -0
  63. rigcode-0.1.0.dist-info/entry_points.txt +2 -0
  64. rigcode-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,535 @@
1
+ """Runtime: what one rigcode session shares - settings, the server connection,
2
+ tools, permissions, hooks, the main agent and subagent orchestration.
3
+
4
+ Prompt-cache strategy (llama.cpp). These server facts drive the design:
5
+ * Hybrid models like Kimi K3 can only rewind the cache to a checkpoint, and
6
+ checkpoints are made at the start of user messages (plus two at the end of
7
+ each prompt). So every agent sends the same tools and the same single
8
+ system message, and the history is append-only.
9
+ * With the default unified KV cache, starting a request saves every idle slot
10
+ to a RAM cache and clears it. Only an unpinned request gets the best saved
11
+ state loaded back; a request pinned to a cleared slot starts from scratch.
12
+ So requests are not pinned (unless provider.llama_cpp.slot_pinning is set).
13
+ * A RAM-cache load moves the entry out of the cache. A subagent that started
14
+ from the main agent's cached state would take it away. So by default a
15
+ subagent forks the main conversation instead: it continues from the main
16
+ agent's exact state (the brief arrives as the Task call's result), and when
17
+ it finishes the main agent continues from the fork's state, which still
18
+ begins with the whole main conversation.
19
+ """
20
+ from __future__ import annotations
21
+
22
+ import asyncio
23
+ import contextlib
24
+ import copy
25
+ import time
26
+ from pathlib import Path
27
+
28
+ from rigcode import __version__
29
+ from rigcode.agent.loop import Agent, TurnStats
30
+ from rigcode.agent.prompts import build_env_snapshot, build_project_context, load_prompt, subagent_brief
31
+ from rigcode.agent.session import SessionStore
32
+ from rigcode.agent.state import Checkpoints, FileStateTracker
33
+ from rigcode.extensions import load_agents, load_commands, load_memory, load_skills
34
+ from rigcode.hooks import HookRunner
35
+ from rigcode.llm.client import LLMClient
36
+ from rigcode.llm.probe import ServerInfo, discover, probe_server
37
+ from rigcode.llm.textmode import render_tools_prompt
38
+ from rigcode.llm.types import LLMError, Usage
39
+ from rigcode.llm.wire import to_wire
40
+ from rigcode.mcp import McpManager
41
+ from rigcode.permissions import PermissionManager
42
+ from rigcode.tools.base import ToolRegistry
43
+ from rigcode.tools.files import EditTool, MultiEditTool, ReadTool, WriteTool
44
+ from rigcode.tools.interact import AskUserTool, ExitPlanModeTool
45
+ from rigcode.tools.search import GlobTool, GrepTool
46
+ from rigcode.tools.shell import BackgroundShells, BashOutputTool, BashTool, KillShellTool, resolve_shell
47
+ from rigcode.tools.skill import SkillTool
48
+ from rigcode.tools.task import TaskTool
49
+ from rigcode.tools.todo import TodoWriteTool
50
+ from rigcode.tools.web import WebFetchTool, WebSearchTool
51
+ from rigcode.ui.base import BaseUI
52
+ from rigcode.utils.paths import rigcode_home
53
+ from rigcode.utils.text import fmt_duration
54
+
55
+ DEFAULT_CONTEXT_WINDOW = 131072
56
+ MIN_LOCAL_CONTEXT = 16384 # below this the system prompt and tools crowd out the task
57
+ EFFORTS = ("low", "medium", "high", "max", "none")
58
+
59
+ PLAN_MODE_ON = (
60
+ "Plan mode is now active. Investigate with read-only tools only (Read, Glob, Grep, read-only Bash commands, "
61
+ "web tools, read-only subagents). Do not edit files or run commands that change anything. When you have a "
62
+ "concrete plan, present it with ExitPlanMode. If the user only asked a question, just answer it.")
63
+ PLAN_MODE_OFF = "Plan mode is now off. You may make changes again (normal permission rules apply)."
64
+
65
+
66
+ class Runtime:
67
+ def __init__(self, settings, project_root: Path, cwd: Path, ui: BaseUI | None = None,
68
+ session_id: str | None = None):
69
+ self.settings = settings
70
+ self.project_root = project_root
71
+ self.cwd = cwd
72
+ self.ui: BaseUI = ui or BaseUI()
73
+ self.debug = bool(settings.get("debug", False))
74
+ self.shell = resolve_shell(settings.get("shell", "auto"))
75
+ self.server = ServerInfo()
76
+ self.session = SessionStore(project_root, session_id)
77
+ log_path = None
78
+ if self.debug:
79
+ log_dir = rigcode_home() / "logs"
80
+ log_dir.mkdir(parents=True, exist_ok=True)
81
+ log_path = log_dir / f"{self.session.id}.jsonl"
82
+ self.log_path = log_path
83
+ self.client = LLMClient(settings.provider, self.server, log_path)
84
+ self.permissions = PermissionManager(settings, project_root)
85
+ self.file_state = FileStateTracker()
86
+ self.checkpoints = Checkpoints()
87
+ self.shells = BackgroundShells()
88
+ self.agents = load_agents(settings, project_root)
89
+ self.skills = load_skills(settings, project_root)
90
+ self.memory = load_memory(settings, project_root, cwd)
91
+ self.commands = load_commands(settings, project_root)
92
+ self.mcp = McpManager(settings.get("mcp_servers", {}), project_root)
93
+ self.hooks = HookRunner(settings.get("hooks", {}), self.shell, self.session.id, self.session.path)
94
+ self.tools = ToolRegistry()
95
+ self.tool_schemas: list[dict] = []
96
+ self.prefix_messages: list[dict] = [] # [system: core prompt + project context], shared by all agents
97
+ self.project_context = ""
98
+ self.effort: str | None = self._normalize_effort(settings.get("provider.reasoning_effort", "low"))
99
+ self.context_window = DEFAULT_CONTEXT_WINDOW
100
+ self.main_slot: int | None = None
101
+ self.sub_slots: list[int] = []
102
+ self._free_slots: list[int] = []
103
+ self.subagent_concurrency = 1
104
+ self._sub_sem: asyncio.Semaphore | None = None
105
+ self._profile_clients: dict[str, LLMClient] = {}
106
+ self.main_agent: Agent | None = None
107
+ self.usage = Usage()
108
+ self.requests = 0
109
+ self.last_usage: Usage | None = None
110
+ self.started = time.time()
111
+ self.warmup_state = "off" # off | running | done | failed
112
+ self._warmup_task: asyncio.Task | None = None
113
+ self._turn_mode = self.permissions.mode
114
+ self._mode_reminder: str | None = None
115
+ self._meta_written = False
116
+ self.running_subagents: list[Agent] = []
117
+
118
+ # ------------------------------------------------------------------ startup
119
+ async def start(self, resume: Path | None = None) -> None:
120
+ results = await asyncio.gather(discover(self.settings.provider), self.mcp.start_all(),
121
+ return_exceptions=True)
122
+ if isinstance(results[0], BaseException):
123
+ server, found = ServerInfo(error=f"{type(results[0]).__name__}: {results[0]}"), None
124
+ else:
125
+ server, found = results[0]
126
+ self.server = server
127
+ self.client.server = server
128
+ if found and server.reachable:
129
+ self.ui.on_notice("info", found)
130
+ if server.kind in ("ollama", "lmstudio") and server.context_window and server.context_window < MIN_LOCAL_CONTEXT:
131
+ fix = ("set OLLAMA_CONTEXT_LENGTH=32768 (or more) and restart Ollama" if server.kind == "ollama"
132
+ else "raise the model's context length in LM Studio")
133
+ self.ui.on_notice("warn", f"The server gives {self.client.model} only {server.context_window:,} tokens of "
134
+ f"context, so long prompts get cut off. To fix it, {fix}.")
135
+ if isinstance(results[1], BaseException):
136
+ self.ui.on_notice("warn", f"MCP startup failed: {results[1]}")
137
+ for s in self.mcp.servers.values():
138
+ if s.status == "failed":
139
+ self.ui.on_notice("warn", f"MCP server '{s.name}' failed to start: {s.error}")
140
+
141
+ self.context_window = int(self.settings.get("provider.context_window") or server.context_window
142
+ or DEFAULT_CONTEXT_WINDOW)
143
+ self._plan_slots()
144
+ self._build_tools()
145
+ self.project_context = build_project_context(self)
146
+ self._build_prefix()
147
+
148
+ history: list[dict] = []
149
+ if resume is not None:
150
+ meta, msgs = SessionStore.load(resume)
151
+ self.session = SessionStore(self.project_root, meta.get("id") or resume.stem)
152
+ self.hooks.session_id, self.hooks.transcript_path = self.session.id, self.session.path
153
+ history = _history_only(msgs)
154
+ self._meta_written = True
155
+ self.main_agent = self._new_main_agent(history, resumed=resume is not None)
156
+ if self.hooks.has("SessionStart"):
157
+ out = await self.hooks.run("SessionStart", {"source": "resume" if resume else "startup"}, self.cwd)
158
+ for n in out.notices:
159
+ self.ui.on_notice("warn", n)
160
+ self.main_agent.pending_reminders.extend(out.additional_context)
161
+ if not server.reachable:
162
+ self.ui.on_notice("error", f"Cannot reach the model server at {self.client.base_url}: {server.error}. "
163
+ "Run `rigcode doctor` for help.")
164
+ else:
165
+ self.start_warmup()
166
+
167
+ def _new_main_agent(self, history: list[dict] | None = None, resumed: bool = False) -> Agent:
168
+ agent = Agent(self, slot=self.main_slot, messages=[dict(m) for m in self.prefix_messages] + (history or []))
169
+ agent.env_note = build_env_snapshot(self, resumed=resumed)
170
+ return agent
171
+
172
+ def _plan_slots(self) -> None:
173
+ lc = self.settings.get("provider.llama_cpp", {}) or {}
174
+ total = self.server.total_slots or 0
175
+ self.main_slot, self.sub_slots = None, []
176
+ if self.server.kind == "llama.cpp" and lc.get("slot_pinning", False) and total >= 1:
177
+ main = lc.get("main_slot")
178
+ main = int(main) if main is not None and 0 <= int(main) < total else total - 1
179
+ subs = lc.get("subagent_slots")
180
+ if subs is None:
181
+ subs = [s for s in range(total - 1, -1, -1) if s != main]
182
+ self.main_slot = main
183
+ self.sub_slots = [int(s) for s in subs if 0 <= int(s) < total and int(s) != main]
184
+ self._free_slots = list(self.sub_slots)
185
+ wanted = max(1, int(self.settings.get("agents.max_concurrency", 1) or 1))
186
+ if self.main_slot is not None:
187
+ self.subagent_concurrency = min(wanted, len(self.sub_slots)) if self.sub_slots else 1
188
+ elif self.server.kind == "llama.cpp" and total:
189
+ self.subagent_concurrency = max(1, min(wanted, total - 1))
190
+ else:
191
+ self.subagent_concurrency = wanted
192
+ self._sub_sem = asyncio.Semaphore(self.subagent_concurrency)
193
+ self._fork_lock = asyncio.Lock()
194
+
195
+ def _build_tools(self) -> None:
196
+ s = self.settings
197
+ reg = ToolRegistry()
198
+ tools = [ReadTool(), WriteTool(), EditTool(), MultiEditTool(), GlobTool(), GrepTool(), BashTool(self.shell)]
199
+ if s.get("tools.background_shells", True):
200
+ tools += [BashOutputTool(), KillShellTool()]
201
+ if s.get("tools.web", True):
202
+ tools += [WebFetchTool(), WebSearchTool()]
203
+ tools.append(TodoWriteTool())
204
+ if self.agents:
205
+ tools.append(TaskTool(self.agents))
206
+ if self.skills:
207
+ tools.append(SkillTool())
208
+ tools += [AskUserTool(), ExitPlanModeTool()]
209
+ tools += sorted(self.mcp.tools(), key=lambda t: t.name)
210
+ disabled = set(s.get("tools.disabled", []) or [])
211
+ for t in tools:
212
+ if t.name not in disabled:
213
+ reg.register(t)
214
+ self.tools = reg
215
+ self.tool_schemas = reg.schemas()
216
+
217
+ def _build_prefix(self) -> None:
218
+ core = load_prompt("system").strip()
219
+ if self.client.tool_mode == "text":
220
+ core += "\n\n" + render_tools_prompt(self.tool_schemas)
221
+ self.prefix_messages = [{"role": "system", "content": core + "\n\n" + self.project_context}]
222
+
223
+ def start_warmup(self) -> None:
224
+ """Pre-process the main agent's prompt (prefix, plus history when resuming) in the background."""
225
+ if (self.settings.get("provider.warmup", True) and self.server.kind == "llama.cpp"
226
+ and self.server.reachable and self.main_agent is not None
227
+ and (self._warmup_task is None or self._warmup_task.done())):
228
+ self.warmup_state = "running"
229
+ self._warmup_task = asyncio.create_task(self._warmup(list(self.main_agent.messages)))
230
+
231
+ async def _warmup(self, messages: list[dict]) -> None:
232
+ """Process the prompt prefix while the user types their first message.
233
+
234
+ The stub user message matters: llama.cpp can only reuse a cached prefix up to a
235
+ checkpoint, and it creates one at the start of the last user message. The real
236
+ first message starts at the same position, so its request reuses everything before it."""
237
+ me = asyncio.current_task()
238
+
239
+ def current() -> bool: # False once a newer warm-up (after /resume) has replaced this one
240
+ return self._warmup_task is me
241
+
242
+ self.warmup_state = "running"
243
+ self.ui.on_warmup("start")
244
+ keep = self.settings.get("provider.keep_reasoning", "all")
245
+ msgs = to_wire(messages + [{"role": "user", "content": "ok"}], tool_mode=self.client.tool_mode,
246
+ keep_reasoning=keep)
247
+ tools = self.tool_schemas if self.client.tool_mode == "native" else None
248
+
249
+ def on_delta(d) -> None:
250
+ if d.kind == "progress" and d.data and current():
251
+ self.ui.on_warmup("progress", d.data)
252
+
253
+ try:
254
+ turn = await self.client.complete(msgs, effort=self.effort, max_tokens=1, on_delta=on_delta,
255
+ tools=tools, slot=self.main_slot)
256
+ except asyncio.CancelledError:
257
+ if current():
258
+ self.warmup_state = "off"
259
+ self.ui.on_warmup("cancelled")
260
+ raise
261
+ except LLMError as e:
262
+ if current():
263
+ self.warmup_state = "failed"
264
+ self.ui.on_warmup("failed", {"error": str(e)})
265
+ return
266
+ if current():
267
+ self.warmup_state = "done"
268
+ self.ui.on_warmup("done", {"prompt_tokens": turn.usage.prompt_tokens,
269
+ "cached": turn.usage.cached_tokens, "seconds": turn.elapsed})
270
+
271
+ async def wait_warmup(self) -> None:
272
+ """Hold the main agent's request until the warm-up finishes. Sent alongside it, an unpinned
273
+ request would land on another slot and re-read the whole prompt in parallel."""
274
+ task = self._warmup_task
275
+ if task is None or task.done():
276
+ return
277
+ try:
278
+ await asyncio.shield(task)
279
+ except asyncio.CancelledError:
280
+ if not task.cancelled():
281
+ raise # our own turn was interrupted; the warm-up carries on
282
+ except Exception: # noqa: BLE001 - a failed warm-up only costs speed
283
+ pass
284
+
285
+ # ------------------------------------------------------------------ used by the agent loop and tools
286
+ def set_cwd(self, path: Path) -> None:
287
+ if path.is_dir():
288
+ self.cwd = path
289
+
290
+ def compact_threshold(self) -> int:
291
+ cw = self.context_window
292
+ frac = float(self.settings.get("context.compact_at", 0.85) or 0.85)
293
+ reserve = min(int(self.settings.get("provider.max_output_tokens") or 8192), cw // 4)
294
+ limits = [int(cw * frac), cw - reserve]
295
+ cap = self.settings.get("context.compact_at_tokens")
296
+ if cap:
297
+ limits.append(int(cap))
298
+ return max(4096, min(limits))
299
+
300
+ def context_limit(self) -> int:
301
+ """Hard ceiling for a prompt: the context window minus room for the reply."""
302
+ cw = self.context_window
303
+ return max(4096, cw - min(int(self.settings.get("provider.max_output_tokens") or 8192), cw // 4))
304
+
305
+ def add_usage(self, usage: Usage, agent: Agent) -> None:
306
+ self.usage.add(usage)
307
+ self.requests += 1
308
+ if agent.is_main:
309
+ self.last_usage = usage
310
+ self.ui.on_usage(agent, usage)
311
+
312
+ def set_mode(self, mode: str) -> str:
313
+ new = self.permissions.set_mode(mode)
314
+ if new == "plan" and self._turn_mode != "plan":
315
+ self._mode_reminder = PLAN_MODE_ON
316
+ elif new != "plan" and self._turn_mode == "plan":
317
+ self._mode_reminder = PLAN_MODE_OFF
318
+ else:
319
+ self._mode_reminder = None
320
+ return new
321
+
322
+ def cycle_mode(self) -> str:
323
+ current = self.permissions.mode
324
+ order = ["default", "acceptEdits", "plan"]
325
+ nxt = order[(order.index(current) + 1) % len(order)] if current in order else "default"
326
+ return self.set_mode(nxt)
327
+
328
+ @staticmethod
329
+ def _normalize_effort(value) -> str | None:
330
+ v = str(value or "").strip().lower()
331
+ if v in ("", "null"):
332
+ return None
333
+ return {"minimal": "low", "xhigh": "max", "off": "none"}.get(v, v)
334
+
335
+ def set_effort(self, value: str) -> str | None:
336
+ effort = self._normalize_effort(value)
337
+ if effort is not None and effort not in EFFORTS:
338
+ raise ValueError(f"unknown effort '{value}' (use {', '.join(EFFORTS)})")
339
+ self.effort = effort
340
+ return effort
341
+
342
+ async def switch_model(self, name: str) -> str:
343
+ """Switch to a settings profile (by name) or another model on the same server."""
344
+ profiles = self.settings.get("profiles", {}) or {}
345
+ old_url = self.client.base_url
346
+ if name in profiles:
347
+ self.settings.apply_profile(name)
348
+ else:
349
+ self.settings.provider["model"] = name
350
+ await self.client.aclose()
351
+ self.client = LLMClient(self.settings.provider, self.server, self.log_path)
352
+ if self.client.base_url != old_url:
353
+ self.server = await probe_server(self.client.base_url, self.settings.get("provider.api_key"))
354
+ self.client.server = self.server
355
+ self.context_window = int(self.settings.get("provider.context_window") or self.server.context_window
356
+ or DEFAULT_CONTEXT_WINDOW)
357
+ self._plan_slots()
358
+ if self.main_agent is not None:
359
+ self.main_agent.slot = self.main_slot
360
+ if self.client.tool_mode == "text" or self.settings.get("provider.tool_mode") == "text":
361
+ self.client.tool_mode = "text"
362
+ self.project_context = build_project_context(self) # it names the model
363
+ self._build_prefix()
364
+ if self.main_agent is not None:
365
+ self.main_agent._replace_prefix()
366
+ where = self.server.describe() if self.server.reachable else f"unreachable: {self.server.error}"
367
+ return f"{self.client.model} @ {self.client.base_url} ({where})"
368
+
369
+ def switch_to_text_tools(self) -> None:
370
+ self.client.tool_mode = "text"
371
+ for c in self._profile_clients.values():
372
+ c.tool_mode = "text"
373
+ self._build_prefix()
374
+
375
+ # ------------------------------------------------------------------ main agent
376
+ async def submit(self, text: str) -> TurnStats:
377
+ agent = self.main_agent
378
+ assert agent is not None, "Runtime.start() was not called"
379
+ if not self._meta_written:
380
+ self._meta_written = True
381
+ self.session.meta(id=self.session.id, version=__version__, cwd=str(self.cwd), model=self.client.model,
382
+ base_url=self.client.base_url, effort=self.effort, started=self.started)
383
+ if self._mode_reminder:
384
+ agent.add_reminder(self._mode_reminder)
385
+ self._mode_reminder = None
386
+ self._turn_mode = self.permissions.mode
387
+ return await agent.run_turn(text)
388
+
389
+ def new_conversation(self) -> None:
390
+ """/clear: fresh history and transcript; the shared prompt prefix (and its cache) stays."""
391
+ self.session = SessionStore(self.project_root)
392
+ self.hooks.session_id, self.hooks.transcript_path = self.session.id, self.session.path
393
+ self._meta_written = False
394
+ self.file_state.clear()
395
+ self.main_agent = self._new_main_agent()
396
+
397
+ def resume(self, path: Path) -> int:
398
+ """Load a saved transcript into a new main agent; returns the number of messages."""
399
+ meta, msgs = SessionStore.load(path)
400
+ history = _history_only(msgs)
401
+ self.session = SessionStore(self.project_root, meta.get("id") or path.stem)
402
+ self.hooks.session_id, self.hooks.transcript_path = self.session.id, self.session.path
403
+ self._meta_written = True
404
+ self.file_state.clear()
405
+ self.main_agent = self._new_main_agent(history, resumed=True)
406
+ if self._warmup_task and not self._warmup_task.done():
407
+ self._warmup_task.cancel() # it was warming the conversation we just replaced
408
+ self._warmup_task = None
409
+ self.start_warmup()
410
+ return len(history)
411
+
412
+ # ------------------------------------------------------------------ subagents
413
+ def _client_for(self, agent_def) -> LLMClient | None:
414
+ name = agent_def.model
415
+ if not name:
416
+ return None
417
+ if name in self._profile_clients:
418
+ return self._profile_clients[name]
419
+ profile = (self.settings.get("profiles", {}) or {}).get(name)
420
+ if not isinstance(profile, dict):
421
+ self.ui.on_notice("warn", f"Agent '{agent_def.name}' asks for unknown profile '{name}'; using the main model.")
422
+ return None
423
+ provider = copy.deepcopy(self.settings.provider)
424
+ provider.update(profile)
425
+ same_server = str(provider.get("base_url", "")).rstrip("/") == self.client.base_url
426
+ client = LLMClient(provider, self.server if same_server else ServerInfo(), self.log_path)
427
+ client.tool_mode = self.client.tool_mode
428
+ self._profile_clients[name] = client
429
+ return client
430
+
431
+ def subagent_mode(self) -> str:
432
+ mode = str(self.settings.get("agents.context", "auto") or "auto").lower()
433
+ return mode if mode in ("auto", "fork", "fresh") else "auto"
434
+
435
+ def parallel_subagents(self) -> bool:
436
+ """Task calls may run side by side only as fresh subagents: parallel forks of one
437
+ conversation would each need their own copy of the main agent's cache."""
438
+ return self.subagent_concurrency > 1 and self.subagent_mode() == "fresh"
439
+
440
+ def _should_fork(self, parent, agent_def, client) -> bool:
441
+ mode = self.subagent_mode()
442
+ if mode == "fresh" or parent is None or client is not None:
443
+ return False
444
+ if agent_def.effort and agent_def.effort != parent.current_effort():
445
+ return False # the effort setting sits before the conversation: a fork would re-read all of it
446
+ if mode == "fork":
447
+ return True
448
+ # A fork needs room on top of the conversation. With the defaults this is the compaction
449
+ # threshold, so in practice subagents always fork; fresh ones rely on the server's RAM cache
450
+ # holding the main conversation while they run.
451
+ limit = self.settings.get("agents.fork_max_tokens") or min(self.compact_threshold(), self.context_limit() // 2)
452
+ return parent.context_tokens() < int(limit)
453
+
454
+ async def run_subagent(self, agent_def, description: str, prompt: str, ctx) -> tuple[str, str]:
455
+ assert self._sub_sem is not None
456
+ parent = getattr(ctx, "agent", None)
457
+ call_id = getattr(ctx, "call_id", "") or ""
458
+ client = self._client_for(agent_def)
459
+ base = parent.fork_messages(call_id) if self._should_fork(parent, agent_def, client) else None
460
+ fork = base is not None
461
+ async with (self._fork_lock if fork else self._sub_sem):
462
+ if fork:
463
+ slot = parent.slot
464
+ else:
465
+ slot = self._free_slots.pop(0) if self._free_slots else None
466
+ base = [dict(m) for m in self.prefix_messages]
467
+ if client is not None and client.server is not self.server:
468
+ slot = None
469
+ max_steps = int(agent_def.max_turns or self.settings.get("agents.max_turns", 40) or 40)
470
+ sub = Agent(self, name=agent_def.name, depth=getattr(parent, "depth", 0) + 1, agent_def=agent_def,
471
+ slot=slot, effort=agent_def.effort, max_steps=max_steps, client=client, messages=base)
472
+ sub.parent_call_id = call_id
473
+ sub.forked = fork
474
+ if fork: # same messages as the parent's last request, so its token count still applies
475
+ sub.last_total_tokens, sub._counted_upto = parent.last_total_tokens, min(parent._counted_upto, len(base))
476
+ self.running_subagents.append(sub)
477
+ self.ui.on_subagent_start(sub, description)
478
+ stats = TurnStats()
479
+ report = ""
480
+ try:
481
+ if fork:
482
+ brief = subagent_brief(agent_def, description, prompt, forked=True)
483
+ stats = await sub.run_turn(brief, tool_call_id=call_id)
484
+ else:
485
+ brief = subagent_brief(agent_def, description, prompt, env=build_env_snapshot(self))
486
+ stats = await sub.run_turn(brief)
487
+ report = sub.last_assistant_text()
488
+ if stats.stopped_reason in ("max_steps", "loop", "context") or not report:
489
+ report = (await sub.final_report()) or report
490
+ if self.hooks.has("SubagentStop"):
491
+ await self.hooks.run("SubagentStop", {"agent": agent_def.name, "description": description},
492
+ self.cwd)
493
+ finally:
494
+ self.running_subagents.remove(sub)
495
+ if slot is not None and slot in self.sub_slots:
496
+ self._free_slots.insert(0, slot) # LIFO: reuse the slot whose cache is warm
497
+ self.ui.on_subagent_end(sub, stats)
498
+ if not fork and client is None and self.main_slot is None:
499
+ await self._flush_idle_slots()
500
+ summary = (f"{stats.steps} steps, {stats.tool_calls} tool calls, {fmt_duration(stats.elapsed)}"
501
+ + ("" if stats.stopped_reason == "done" else f", stopped: {stats.stopped_reason}"))
502
+ return report, summary
503
+
504
+ async def _flush_idle_slots(self) -> None:
505
+ """After a fresh subagent, start a tiny request so llama.cpp moves the subagent's idle
506
+ slot into its RAM cache. Otherwise the main agent's next request could be matched to
507
+ that slot (they share the prompt prefix) and re-read its own history instead of
508
+ getting its saved state back."""
509
+ if self.server.kind != "llama.cpp" or not self.server.reachable:
510
+ return
511
+ with contextlib.suppress(Exception):
512
+ await asyncio.wait_for(self.client.complete([{"role": "user", "content": "."}], effort="none",
513
+ max_tokens=1), 60)
514
+
515
+ # ------------------------------------------------------------------ shutdown
516
+ async def close(self) -> None:
517
+ if self._warmup_task and not self._warmup_task.done():
518
+ self._warmup_task.cancel()
519
+ with contextlib.suppress(asyncio.CancelledError, Exception):
520
+ await self._warmup_task
521
+ if self.hooks.has("SessionEnd"):
522
+ with contextlib.suppress(Exception):
523
+ await self.hooks.run("SessionEnd", {"reason": "exit"}, self.cwd)
524
+ with contextlib.suppress(Exception):
525
+ await self.shells.kill_all()
526
+ with contextlib.suppress(Exception):
527
+ await self.mcp.close_all()
528
+ await self.client.aclose()
529
+ for c in self._profile_clients.values():
530
+ await c.aclose()
531
+
532
+
533
+ def _history_only(msgs: list[dict]) -> list[dict]:
534
+ """A saved transcript without the prompt prefix (rebuilt fresh for every session)."""
535
+ return [m for m in msgs if m.get("role") != "system" and not m.get("_prefix")]