lithe-cli 0.4.2__tar.gz → 0.4.3__tar.gz

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 (54) hide show
  1. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/PKG-INFO +5 -5
  2. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/README.md +4 -4
  3. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/__init__.py +1 -1
  4. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/agent.py +17 -3
  5. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/cli_parser.py +4 -3
  6. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/config.py +32 -9
  7. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/main.py +3 -1
  8. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/workbench/turns.py +13 -1
  9. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli.egg-info/PKG-INFO +5 -5
  10. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_cli.py +50 -0
  11. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_workbench.py +40 -0
  12. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/LICENSE +0 -0
  13. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/__main__.py +0 -0
  14. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/clipboard.py +0 -0
  15. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/commands.py +0 -0
  16. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/diag.py +0 -0
  17. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/present.py +0 -0
  18. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/profiles.py +0 -0
  19. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/prompts.py +0 -0
  20. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/py.typed +0 -0
  21. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/sessions.py +0 -0
  22. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/setup.py +0 -0
  23. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/ttui/__init__.py +0 -0
  24. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/ttui/app.py +0 -0
  25. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/ttui/copy.py +0 -0
  26. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/ttui/pickers.py +0 -0
  27. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/ttui_render.py +0 -0
  28. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/ttui_widgets.py +0 -0
  29. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/ui.py +0 -0
  30. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/workbench/__init__.py +0 -0
  31. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/workbench/core.py +0 -0
  32. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/workbench/dispatch.py +0 -0
  33. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/workbench/endpoints.py +0 -0
  34. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli/workbench/settings.py +0 -0
  35. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli.egg-info/SOURCES.txt +0 -0
  36. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli.egg-info/dependency_links.txt +0 -0
  37. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli.egg-info/entry_points.txt +0 -0
  38. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli.egg-info/requires.txt +0 -0
  39. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/lithe_cli.egg-info/top_level.txt +0 -0
  40. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/pyproject.toml +0 -0
  41. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/setup.cfg +0 -0
  42. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_agent_instructions.py +0 -0
  43. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_cli_budget_steer.py +0 -0
  44. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_cli_clipboard.py +0 -0
  45. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_cli_guard.py +0 -0
  46. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_cli_sessions.py +0 -0
  47. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_cli_subagents.py +0 -0
  48. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_cli_surface.py +0 -0
  49. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_diag.py +0 -0
  50. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_present.py +0 -0
  51. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_profiles.py +0 -0
  52. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_retention.py +0 -0
  53. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_setup.py +0 -0
  54. {lithe_cli-0.4.2 → lithe_cli-0.4.3}/tests/test_ttui.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lithe-cli
3
- Version: 0.4.2
3
+ Version: 0.4.3
4
4
  Summary: Command-line interface for the lithe agent kernel: chat, one-shot runs, undo, and run inspection.
5
5
  License: MIT License
6
6
 
@@ -347,13 +347,13 @@ kernel maps it per protocol (`reasoning_effort` on chat-completions,
347
347
 
348
348
  ### Extra capabilities
349
349
 
350
- The kernel ships these as bundles. Default policy: only the execution
351
- surfaces are off until asked for — the analysis/ingress bundles are on
352
- out of the box:
350
+ The kernel ships these as bundles. Default policy: `--shell` is off until
351
+ asked for, `--code` is on only where bubblewrap isolates it, and the
352
+ analysis/ingress bundles are on out of the box:
353
353
 
354
354
  | Flag | Tools granted | Notes |
355
355
  | --- | --- | --- |
356
- | `--code` | `run_code` / `run_file` | Python under bubblewrap when installed (passthrough otherwise); `doctor` shows which; **off by default** |
356
+ | `--code` / `--no-code` | `run_code` / `run_file` | Python under bubblewrap when installed (passthrough otherwise); `doctor` shows which; **on by default under bwrap, off where it would run unsandboxed** (the verification loop needs it: pass `--code` on such hosts) |
357
357
  | `--shell` | `run_command` | Native bash/sh on Linux and macOS, PowerShell/cmd on Windows; runs with the current user's host permissions, is not sandboxed, and cannot be undone; **off by default** |
358
358
  | `--skills DIR` | `load_skill` | markdown skill library; defaults to `$LITHE_HOME/skills` when it exists, `--skills ""` disables |
359
359
  | `--download` / `--no-download` | `download_file` | SSRF-guarded, size-capped network fetch; **on by default** |
@@ -296,13 +296,13 @@ kernel maps it per protocol (`reasoning_effort` on chat-completions,
296
296
 
297
297
  ### Extra capabilities
298
298
 
299
- The kernel ships these as bundles. Default policy: only the execution
300
- surfaces are off until asked for — the analysis/ingress bundles are on
301
- out of the box:
299
+ The kernel ships these as bundles. Default policy: `--shell` is off until
300
+ asked for, `--code` is on only where bubblewrap isolates it, and the
301
+ analysis/ingress bundles are on out of the box:
302
302
 
303
303
  | Flag | Tools granted | Notes |
304
304
  | --- | --- | --- |
305
- | `--code` | `run_code` / `run_file` | Python under bubblewrap when installed (passthrough otherwise); `doctor` shows which; **off by default** |
305
+ | `--code` / `--no-code` | `run_code` / `run_file` | Python under bubblewrap when installed (passthrough otherwise); `doctor` shows which; **on by default under bwrap, off where it would run unsandboxed** (the verification loop needs it: pass `--code` on such hosts) |
306
306
  | `--shell` | `run_command` | Native bash/sh on Linux and macOS, PowerShell/cmd on Windows; runs with the current user's host permissions, is not sandboxed, and cannot be undone; **off by default** |
307
307
  | `--skills DIR` | `load_skill` | markdown skill library; defaults to `$LITHE_HOME/skills` when it exists, `--skills ""` disables |
308
308
  | `--download` / `--no-download` | `download_file` | SSRF-guarded, size-capped network fetch; **on by default** |
@@ -1,3 +1,3 @@
1
1
  """lithe-cli: the command-line interface for the lithe agent kernel."""
2
2
 
3
- __version__ = "0.4.2"
3
+ __version__ = "0.4.3"
@@ -12,9 +12,11 @@ Everything here maps lithe's host contract onto CLI defaults:
12
12
  from ``--document-format`` > profile ``document_format`` > provider
13
13
  preset) and ``--download`` (SSRF-guarded network fetch) are ON by
14
14
  default (``--no-vision`` / ``--no-document`` / ``--no-download`` or
15
- in-session ``/set`` disable); the execution surfaces stay opt-in —
16
- ``--code`` (sandboxed Python) and ``--shell`` (native host commands)
17
- are off until asked for. ``--skills`` (markdown skill library),
15
+ in-session ``/set`` disable); the execution surfaces — ``--code``
16
+ (Python under bwrap where installed; on by default only there, since
17
+ without bwrap it is an unsandboxed subprocess) and ``--shell`` (native
18
+ host commands, off until asked for) — let the agent run tests, lint and
19
+ type checks after an edit; ``--skills`` (markdown skill library),
18
20
  ``--mcp`` (external MCP servers) and ``--subagents`` (kernel
19
21
  delegation on the CLI's default roster, on by default) complete the
20
22
  roster;
@@ -81,6 +83,9 @@ SYSTEM_PROMPT_BASE = (
81
83
  "修改已有文件时优先用 edit_file 或 apply_patch 做局部修改,"
82
84
  "仅在新建文件或确需整体重写时使用 write_file。\n"
83
85
  "工具调用失败时,先读返回的错误信息,修正参数后重试,不要原样重发。\n"
86
+ "写代码前先弄清现有实现、调用方和仓库既有的做法,沿用其风格与既有的工具函数,"
87
+ "只做任务需要的最小改动,不顺手重构无关代码;"
88
+ "不要臆造不存在的库、函数或参数——不确定就先 search_files 或 read_file 确认。\n"
84
89
  "只有用户明确要求计划/跟踪,或任务确有多个需要追踪的独立阶段时才调用 update_todos;"
85
90
  "普通问答、解释、单步操作和小改动不创建待办。已有清单仅在用户继续相关工作时更新,无关请求保持不变;"
86
91
  "更新前读取并保留相关的现有任务,条数按实际步骤确定,不使用固定数量。\n"
@@ -285,6 +290,15 @@ def build_system_prompt(cfg: Config) -> str:
285
290
  extras.append("可以用 delegate 把独立子任务委派给子代理(可用名单见工具说明:检索/编码/操作),"
286
291
  "互不依赖的子任务用 delegate_parallel 并行委派;"
287
292
  "子代理与本对话共享预算,汇报时合并它们的结果。")
293
+ if cfg.code or cfg.shell:
294
+ # The verification loop is the point of an execution surface on a
295
+ # coding turn: without it the model ships edits it never ran.
296
+ verify_with = " / ".join(
297
+ name for name, on in (("run_command", cfg.shell), ("run_code", cfg.code))
298
+ if on)
299
+ extras.append(f"改动代码后,用 {verify_with} 运行与改动直接相关的测试、lint 或类型检查,"
300
+ "按输出修正后再汇报;确实无法运行时,"
301
+ "在汇报里明确说明改动未经执行验证。")
288
302
  parts = [SYSTEM_PROMPT_BASE + "\n" + "\n".join(extras)]
289
303
  block = instructions_block(cfg)
290
304
  if block is not None:
@@ -54,9 +54,10 @@ def build_parser() -> argparse.ArgumentParser:
54
54
  "default (--no-download disables)",
55
55
  )
56
56
  sp.add_argument(
57
- "--code", action="store_true",
58
- help="grant run_code/run_file (sandboxed Python; "
59
- "bwrap when available, passthrough otherwise); off by default",
57
+ "--code", action=argparse.BooleanOptionalAction, default=None,
58
+ help="grant run_code/run_file (sandboxed Python; bwrap when "
59
+ "available, passthrough otherwise); default: on where bwrap "
60
+ "isolates it, off otherwise (--no-code / --code to force)",
60
61
  )
61
62
  sp.add_argument(
62
63
  "--shell", action="store_true",
@@ -9,12 +9,17 @@ silently hitting some third-party URL. Resolution order is flag > env >
9
9
  interactive terminal a missing endpoint triggers the wizard instead of
10
10
  the refusal — see :mod:`lithe_cli.setup`.
11
11
 
12
- Capability flags (only the execution surfaces — shell and code — are off
13
- unless asked for; the analysis/ingress bundles below default to ON and
14
- turn off with their ``--no-*`` flag or in-session ``/set``):
15
-
16
- - ``--code`` sandboxed ``run_code`` / ``run_file`` tools (bwrap when
17
- available, passthrough otherwise); **off by default**;
12
+ Capability flags (``--code`` is on only where bwrap really isolates it and
13
+ ``--shell`` is off unless asked for; the analysis/ingress bundles below
14
+ default to ON and turn off with their ``--no-*`` flag or in-session
15
+ ``/set``):
16
+
17
+ - ``--code`` ``run_code`` / ``run_file`` tools (bwrap when available,
18
+ passthrough otherwise); **on by default where bwrap
19
+ isolates the run, off where it would be a plain
20
+ subprocess with the user's authority** (see
21
+ :func:`code_default`); ``--code`` forces it on,
22
+ ``--no-code`` off;
18
23
  - ``--shell`` native ``run_command`` (host permissions, not sandboxed,
19
24
  not undoable); **off by default**;
20
25
  - ``--download`` ``download_file`` (SSRF-guarded network fetch); on by
@@ -83,8 +88,10 @@ class Config:
83
88
  # in-session profile switches re-resolve it). None = not explicit.
84
89
  document_format: str | None = None
85
90
  # Capability bundles: the analysis/ingress ones default ON (--no-* to
86
- # disable); the execution surfaces (code/shell) default OFF — see the
87
- # module docstring for the per-flag rationale.
91
+ # disable); shell defaults OFF, and code — a direct `Config()` — does
92
+ # too, while the CLI resolves it through code_default() (ON only where
93
+ # bwrap isolates it) — see the module docstring for the per-flag
94
+ # rationale.
88
95
  document: bool = True
89
96
  # Subagent delegation (kernel bundles.subagents): grants delegate /
90
97
  # delegate_parallel on the CLI's default roster. ON by default — the
@@ -174,6 +181,18 @@ def default_skills_dir() -> Path:
174
181
  return lithe_home() / "skills"
175
182
 
176
183
 
184
+ def code_default() -> bool:
185
+ """``run_code`` when nothing asked either way: ON only where bwrap would
186
+ isolate it. Elsewhere the runner is a plain subprocess with the user's
187
+ authority (``lithe.bundles.sandbox``), which stays opt-in — ``--code``
188
+ or ``/set code on`` — so a fresh install never hands the model
189
+ unsandboxed code execution over a workspace it was merely asked to read.
190
+ That is also the verification loop's price on such hosts: tests, lint
191
+ and type checks need an execution surface, so a coding session there
192
+ wants ``--code`` (or ``--shell``) by choice."""
193
+ return shutil.which("bwrap") is not None
194
+
195
+
177
196
  def load_mcp_spec(raw: str) -> list:
178
197
  """Parse an MCP spec (raw JSON or ``@file``) into server configs.
179
198
 
@@ -389,6 +408,10 @@ def load_config(args: Any) -> Config:
389
408
  # provider preset's default fills in inside build_llm (apply_preset
390
409
  # skips the None override), so in-session profile switches re-resolve.
391
410
  document_format = g("document_format", None) or saved.get("document_format")
411
+ # run_code: the flag decides when given (--code / --no-code); otherwise
412
+ # the host does (on only where bwrap isolates it).
413
+ code = g("code", None)
414
+ code = code_default() if code is None else bool(code)
392
415
  return Config(
393
416
  api_key=(flag_key or os.environ.get(ENV_API_KEY) or saved.get("api_key")),
394
417
  base_url=base_url,
@@ -425,7 +448,7 @@ def load_config(args: Any) -> Config:
425
448
  sleep_err=g("sleep_err", 1.0),
426
449
  download=g("download", True),
427
450
  verbose=g("verbose", False),
428
- code=g("code", False),
451
+ code=code,
429
452
  shell=g("shell", False),
430
453
  skills_dir=skills_dir,
431
454
  mcp_spec=mcp_raw,
@@ -371,7 +371,9 @@ def _cmd_doctor() -> int:
371
371
  sandbox_line = ui.s("bwrap(隔离执行)", GREEN)
372
372
  else:
373
373
  sandbox_line = ui.s("passthrough(未找到 bwrap,无隔离)", RED)
374
- print(ui.kv("sandbox", sandbox_line + ";--code 启用 run_code"))
374
+ print(ui.kv("sandbox", sandbox_line + (
375
+ ";run_code 已启用(--no-code 关闭)" if cfg.code
376
+ else ";--code 启用 run_code")))
375
377
  command_line = "已启用(run_command 可执行主机命令)" if cfg.shell else "未启用(--shell 显式授权)"
376
378
  print(ui.kv("shell", command_line))
377
379
  caps = []
@@ -20,6 +20,17 @@ if TYPE_CHECKING:
20
20
  from ..sessions import SessionManager
21
21
  from .core import Approver
22
22
 
23
+ # Replay bound for stored tool results. It must be at least as large as the
24
+ # biggest result the live loop sends, or the replayed turn differs from what
25
+ # the previous turn sent — and the first differing block ends the
26
+ # prompt-cache prefix, so every later user message re-pays the whole
27
+ # history at full input price. Since kernel 0.2.3 the runtime itself bounds
28
+ # every tool result at DEFAULT_TOOL_RESULT_CAP (40000) when it enters the
29
+ # conversation, which this equals; the literal stays because the CLI's
30
+ # dependency floor (0.2.2) still carries the kernel's old 8000 default,
31
+ # which rewrote nearly every file read of a coding session on replay.
32
+ _REPLAY_TOOL_RESULT_CAP = 40_000
33
+
23
34
 
24
35
  class TurnMixin:
25
36
  # Provided by the class the Workbench combines this mixin with
@@ -68,7 +79,8 @@ class TurnMixin:
68
79
  log.info("轮次开始 session=%s task_chars=%s", cid, len(task))
69
80
  try:
70
81
  rows = self.history_rows(cid)
71
- history = replay_messages(rows) if rows else None
82
+ history = (replay_messages(rows, tool_result_cap=_REPLAY_TOOL_RESULT_CAP)
83
+ if rows else None)
72
84
  rid, done, _ = await execute(
73
85
  self.cfg,
74
86
  task,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lithe-cli
3
- Version: 0.4.2
3
+ Version: 0.4.3
4
4
  Summary: Command-line interface for the lithe agent kernel: chat, one-shot runs, undo, and run inspection.
5
5
  License: MIT License
6
6
 
@@ -347,13 +347,13 @@ kernel maps it per protocol (`reasoning_effort` on chat-completions,
347
347
 
348
348
  ### Extra capabilities
349
349
 
350
- The kernel ships these as bundles. Default policy: only the execution
351
- surfaces are off until asked for — the analysis/ingress bundles are on
352
- out of the box:
350
+ The kernel ships these as bundles. Default policy: `--shell` is off until
351
+ asked for, `--code` is on only where bubblewrap isolates it, and the
352
+ analysis/ingress bundles are on out of the box:
353
353
 
354
354
  | Flag | Tools granted | Notes |
355
355
  | --- | --- | --- |
356
- | `--code` | `run_code` / `run_file` | Python under bubblewrap when installed (passthrough otherwise); `doctor` shows which; **off by default** |
356
+ | `--code` / `--no-code` | `run_code` / `run_file` | Python under bubblewrap when installed (passthrough otherwise); `doctor` shows which; **on by default under bwrap, off where it would run unsandboxed** (the verification loop needs it: pass `--code` on such hosts) |
357
357
  | `--shell` | `run_command` | Native bash/sh on Linux and macOS, PowerShell/cmd on Windows; runs with the current user's host permissions, is not sandboxed, and cannot be undone; **off by default** |
358
358
  | `--skills DIR` | `load_skill` | markdown skill library; defaults to `$LITHE_HOME/skills` when it exists, `--skills ""` disables |
359
359
  | `--download` / `--no-download` | `download_file` | SSRF-guarded, size-capped network fetch; **on by default** |
@@ -121,6 +121,30 @@ def test_tools_command_with_code(tmp_path, capsys):
121
121
  assert "run_file" in out
122
122
 
123
123
 
124
+ def test_code_default_follows_the_sandbox(tmp_path, monkeypatch, capsys):
125
+ """run_code is on by default only where bwrap isolates it: a plain
126
+ subprocess with the user's authority stays opt-in."""
127
+ from lithe_cli.cli_parser import build_parser
128
+ from lithe_cli.config import load_config
129
+ from lithe_cli.main import main
130
+
131
+ monkeypatch.setenv("LITHE_HOME", str(tmp_path / "home"))
132
+ monkeypatch.setattr("lithe_cli.config.code_default", lambda: True)
133
+ tools = ["tools", "--workspace", str(tmp_path), "--store", str(tmp_path / "s")]
134
+ assert main(tools) == 0
135
+ assert "run_code" in capsys.readouterr().out
136
+ assert main([*tools, "--no-code"]) == 0
137
+ assert "run_code" not in capsys.readouterr().out
138
+ # the parser's tri-state: no flag asks the host, a flag decides
139
+ args = build_parser().parse_args(["tools"])
140
+ assert load_config(args).code is True
141
+ assert load_config(build_parser().parse_args(["tools", "--no-code"])).code is False
142
+
143
+ monkeypatch.setattr("lithe_cli.config.code_default", lambda: False)
144
+ assert load_config(args).code is False
145
+ assert load_config(build_parser().parse_args(["tools", "--code"])).code is True
146
+
147
+
124
148
  def test_tools_command_with_skills(tmp_path, capsys):
125
149
  skills = tmp_path / "skills"
126
150
  skills.mkdir()
@@ -364,6 +388,32 @@ def test_system_prompt_mentions_capabilities(tmp_path):
364
388
  assert "run_command" in with_shell and "run_command" not in base
365
389
 
366
390
 
391
+ def test_system_prompt_asks_for_verification_with_the_enabled_tools():
392
+ from lithe_cli.agent import build_system_prompt
393
+ from lithe_cli.config import Config
394
+
395
+ def verify_line(prompt):
396
+ return next(line for line in prompt.splitlines() if "改动代码后" in line)
397
+
398
+ assert "改动代码后" not in build_system_prompt(Config())
399
+ assert "用 run_code 运行" in verify_line(build_system_prompt(Config(code=True)))
400
+ assert "用 run_command 运行" in verify_line(build_system_prompt(Config(shell=True)))
401
+ both = verify_line(build_system_prompt(Config(code=True, shell=True)))
402
+ assert "用 run_command / run_code 运行" in both
403
+
404
+
405
+ def test_system_prompt_carries_the_coding_discipline():
406
+ """The coding rules live in the base prompt — every turn, whatever the
407
+ capability flags: understand before editing, the smallest change, no
408
+ invented APIs."""
409
+ from lithe_cli.agent import SYSTEM_PROMPT_BASE, build_system_prompt
410
+ from lithe_cli.config import Config
411
+
412
+ prompt = build_system_prompt(Config())
413
+ for rule in ("先弄清现有实现", "最小改动", "不要臆造不存在的库"):
414
+ assert rule in SYSTEM_PROMPT_BASE and rule in prompt
415
+
416
+
367
417
  def test_ui_degrades_without_color():
368
418
  from lithe_cli import ui as ui_mod
369
419
 
@@ -70,6 +70,46 @@ def test_cross_process_resume_replays_history(tmp_path):
70
70
  assert wb2.sessions.summary(cid)["n_runs"] == 2
71
71
 
72
72
 
73
+ def test_turn_replay_sends_what_the_live_loop_sent(tmp_path):
74
+ """A tool result rewritten on replay (the kernel's 8000-char default did
75
+ that to every sizeable file read) ends the prompt-cache prefix at that
76
+ block, so each new user message re-paid the whole history at full price."""
77
+ ws = tmp_path / "ws"
78
+ ws.mkdir()
79
+ (ws / "big.py").write_text(
80
+ "\n".join(f"def f{i}(): pass" for i in range(1500)), encoding="utf-8")
81
+ seen: list[list[dict]] = []
82
+
83
+ class Recorder:
84
+ def __init__(self, responses):
85
+ self.it = iter(responses)
86
+
87
+ async def complete(self, client, **kw):
88
+ seen.append([dict(m) for m in (kw.get("messages") or [])])
89
+ r = next(self.it)
90
+ return {"content": r.get("content", ""), "tool_calls": r.get("tool_calls", []),
91
+ "usage": {}}
92
+
93
+ def wb_with(responses):
94
+ return Workbench(Config(store_dir=tmp_path / "store", workspace_dir=ws,
95
+ transport=Recorder(responses)))
96
+
97
+ wb = wb_with([{"tool_calls": [_tc("read_file", {"path": "big.py"}, "r1")]},
98
+ {"content": "读完了"}])
99
+ wb.open()
100
+ cid = wb.current["id"]
101
+ asyncio.run(wb.run_turn("读 big.py"))
102
+ sent = next(m for m in seen[1] if m.get("role") == "tool")
103
+ assert 8000 < len(sent["content"]) < 21_000 # read_file's cap, cut at a line
104
+ assert "已截断" in sent["content"]
105
+
106
+ wb2 = wb_with([{"content": "第二轮回答"}])
107
+ wb2.open(resume=str(cid))
108
+ asyncio.run(wb2.run_turn("继续"))
109
+ replayed = next(m for m in seen[2] if m.get("role") == "tool")
110
+ assert replayed == sent
111
+
112
+
73
113
  def test_new_resume_rename_delete_dispatch(tmp_path):
74
114
  wb = _wb(tmp_path, [])
75
115
  wb.open()
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes