lithe-cli 0.9.2__tar.gz → 0.9.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.

Potentially problematic release.


This version of lithe-cli might be problematic. Click here for more details.

Files changed (35) hide show
  1. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/PKG-INFO +2 -2
  2. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/__init__.py +1 -1
  3. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/agent.py +35 -4
  4. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/main.py +5 -1
  5. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/ttui.py +53 -1
  6. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/workbench.py +5 -0
  7. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli.egg-info/PKG-INFO +2 -2
  8. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli.egg-info/SOURCES.txt +1 -0
  9. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli.egg-info/requires.txt +1 -1
  10. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/pyproject.toml +1 -1
  11. lithe_cli-0.9.3/tests/test_cli_guard.py +67 -0
  12. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/LICENSE +0 -0
  13. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/README.md +0 -0
  14. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/__main__.py +0 -0
  15. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/commands.py +0 -0
  16. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/config.py +0 -0
  17. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/profiles.py +0 -0
  18. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/prompts.py +0 -0
  19. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/py.typed +0 -0
  20. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/sessions.py +0 -0
  21. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/setup.py +0 -0
  22. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/tui.py +0 -0
  23. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli/ui.py +0 -0
  24. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli.egg-info/dependency_links.txt +0 -0
  25. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli.egg-info/entry_points.txt +0 -0
  26. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/lithe_cli.egg-info/top_level.txt +0 -0
  27. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/setup.cfg +0 -0
  28. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/tests/test_cli.py +0 -0
  29. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/tests/test_cli_budget_steer.py +0 -0
  30. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/tests/test_cli_sessions.py +0 -0
  31. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/tests/test_cli_subagents.py +0 -0
  32. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/tests/test_profiles.py +0 -0
  33. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/tests/test_ttui.py +0 -0
  34. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/tests/test_tui_v2.py +0 -0
  35. {lithe_cli-0.9.2 → lithe_cli-0.9.3}/tests/test_workbench.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lithe-cli
3
- Version: 0.9.2
3
+ Version: 0.9.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
 
@@ -39,7 +39,7 @@ Classifier: Typing :: Typed
39
39
  Requires-Python: >=3.10
40
40
  Description-Content-Type: text/markdown
41
41
  License-File: LICENSE
42
- Requires-Dist: lithe>=0.9.22
42
+ Requires-Dist: lithe>=0.9.24
43
43
  Requires-Dist: prompt-toolkit>=3.0.38
44
44
  Requires-Dist: textual>=1.0
45
45
  Provides-Extra: dev
@@ -1,3 +1,3 @@
1
1
  """lithe-cli: the command-line interface for the lithe agent kernel."""
2
2
 
3
- __version__ = "0.9.2"
3
+ __version__ = "0.9.3"
@@ -20,12 +20,14 @@ Everything here maps lithe's host contract onto CLI defaults:
20
20
 
21
21
  from __future__ import annotations
22
22
 
23
+ import asyncio
23
24
  import hashlib
24
25
  import json
25
26
  import shutil
26
27
  import sys
27
28
  import time
28
29
  import uuid
30
+ from collections.abc import Awaitable, Callable
29
31
  from pathlib import Path
30
32
  from typing import Any
31
33
 
@@ -37,7 +39,7 @@ from lithe.bundles import (
37
39
  Workspace,
38
40
  undo_run,
39
41
  )
40
- from lithe.bundles.command import register_command_tools
42
+ from lithe.bundles.command import make_command_guard, register_command_tools
41
43
  from lithe.bundles.download import register_download_tools
42
44
  from lithe.bundles.patch import register_apply_patch_tool
43
45
  from lithe.bundles.workspace import register_file_tools
@@ -141,8 +143,13 @@ def build_llm(cfg: Config) -> LLMConfig:
141
143
  return LLMConfig(**overrides)
142
144
 
143
145
 
144
- def build_registry(cfg: Config) -> ToolRegistry:
145
- """Register the CLI's tool set against the configured workspace."""
146
+ def build_registry(cfg: Config, approver: Callable[[str], Awaitable[bool]] | None = None) -> ToolRegistry:
147
+ """Register the CLI's tool set against the configured workspace.
148
+
149
+ ``approver`` is the human-confirmation channel for destructive
150
+ ``run_command`` calls (the kernel guard's "approve" tier); without one
151
+ those are denied with guidance instead of running silently.
152
+ """
146
153
  reg = ToolRegistry()
147
154
 
148
155
  def workspace_for(ctx) -> Workspace:
@@ -165,6 +172,10 @@ def build_registry(cfg: Config) -> ToolRegistry:
165
172
  register_command_tools(
166
173
  reg, lambda ctx: cfg.workspace_dir.resolve(), CommandRunner()
167
174
  )
175
+ # Dangerous-command guard: catastrophic commands never run; the
176
+ # destructive-but-scoped tier (rm -r <path>, git push --force,
177
+ # sudo, ...) asks the approver first.
178
+ reg.add_middleware(make_command_guard(approver))
168
179
  if cfg.skills_dir is not None:
169
180
  from lithe.bundles.skills import SkillLibrary, register_skill_tool
170
181
 
@@ -350,6 +361,25 @@ def render_event(
350
361
  print(ui.reasoning(digest[:120]))
351
362
 
352
363
 
364
+ def interactive_approver() -> Callable[[str], Awaitable[bool]] | None:
365
+ """A stdin y/n approver for interactive line-mode front-ends.
366
+
367
+ Returns None on a non-TTY stdin (piped/scripted runs): nobody can
368
+ answer, so the guard's destructive tier denies instead of hanging.
369
+ """
370
+ if not sys.stdin.isatty():
371
+ return None
372
+
373
+ async def approver(command: str) -> bool:
374
+ from . import prompts
375
+
376
+ preview = command if len(command) <= 90 else command[:87] + "..."
377
+ return await asyncio.to_thread(
378
+ prompts.yes_no, f"⚠ 破坏性命令需确认,允许执行:{preview}", False)
379
+
380
+ return approver
381
+
382
+
353
383
  async def execute(
354
384
  cfg: Config,
355
385
  task: str,
@@ -359,6 +389,7 @@ async def execute(
359
389
  stop=None,
360
390
  conversation_id: int | None = None,
361
391
  inbox=None,
392
+ approver: Callable[[str], Awaitable[bool]] | None = None,
362
393
  ) -> tuple[str, dict, Any]:
363
394
  """Run one task end-to-end; returns (run_id, done_event, host).
364
395
 
@@ -371,7 +402,7 @@ async def execute(
371
402
  the steering channel: a queue of user texts the kernel drains at step
372
403
  boundaries and injects as user messages (see AgentRuntime.run).
373
404
  """
374
- reg = build_registry(cfg)
405
+ reg = build_registry(cfg, approver)
375
406
  host = build_host(cfg, reg)
376
407
  if cfg.subagents:
377
408
  register_subagents(cfg, host, reg)
@@ -498,7 +498,9 @@ def _cmd_doctor() -> int:
498
498
 
499
499
 
500
500
  def _cmd_run(cfg: Any, task: str) -> int:
501
- _, done, _ = asyncio.run(execute(cfg, task))
501
+ from .agent import interactive_approver
502
+
503
+ _, done, _ = asyncio.run(execute(cfg, task, approver=interactive_approver()))
502
504
  return 0 if done.get("status") == "done" else 1
503
505
 
504
506
 
@@ -611,9 +613,11 @@ def _chat_loop(
611
613
  title: str | None = None,
612
614
  ) -> int:
613
615
  """Plain (non-TTY) chat: same Workbench and commands as the full screen."""
616
+ from .agent import interactive_approver
614
617
  from .workbench import Workbench
615
618
 
616
619
  wb = Workbench(cfg)
620
+ wb.approver = interactive_approver() # y/n on stdin when a TTY exists
617
621
  opening = wb.open(
618
622
  resume=resume, continue_latest=continue_latest, title=title
619
623
  )
@@ -352,6 +352,34 @@ class PickerModal(ModalScreen):
352
352
  row.children[0].update(label)
353
353
 
354
354
 
355
+ # -- command approval --------------------------------------------------------
356
+
357
+ class ConfirmModal(ModalScreen):
358
+ """y/n on a destructive run_command: y allows, n/Esc refuses."""
359
+
360
+ BINDINGS = [
361
+ Binding("y", "allow", "允许"),
362
+ Binding("n", "refuse", "拒绝"),
363
+ Binding("escape", "refuse", "拒绝", show=False),
364
+ ]
365
+
366
+ def __init__(self, command: str):
367
+ super().__init__()
368
+ self.command = command
369
+
370
+ def compose(self) -> ComposeResult:
371
+ with Vertical(id="picker"):
372
+ yield Static("⚠ 破坏性命令需确认", id="picker-title")
373
+ yield Static(self.command, id="picker-hint")
374
+ yield Static("y 允许执行 · n/Esc 拒绝", id="picker-hint")
375
+
376
+ def action_allow(self) -> None:
377
+ self.dismiss(True)
378
+
379
+ def action_refuse(self) -> None:
380
+ self.dismiss(False)
381
+
382
+
355
383
  # -- the app ----------------------------------------------------------------------
356
384
 
357
385
  class LitheApp(App):
@@ -418,6 +446,9 @@ class LitheApp(App):
418
446
  from .workbench import Workbench
419
447
 
420
448
  self.wb = Workbench(self.cfg)
449
+ # Destructive run_command approval rides a modal; the turn task and
450
+ # the app share one loop, so the middleware can await the human.
451
+ self.wb.approver = self.confirm_command
421
452
  self.todo_store = JsonTodoStore(todo_store_path(self.cfg))
422
453
  self.version = __version__
423
454
  prompt = self.query_one("#prompt", HistoryInput)
@@ -940,6 +971,27 @@ class LitheApp(App):
940
971
 
941
972
  # -- one-shot run mode --------------------------------------------------------
942
973
 
974
+ async def confirm_command(self, command: str) -> bool:
975
+ """Await a human y/n on a destructive command (guard middleware).
976
+
977
+ Bounded: an abandoned modal times out as a refusal instead of
978
+ wedging the turn forever; any UI failure also refuses.
979
+ """
980
+ fut: asyncio.Future[bool] = asyncio.get_running_loop().create_future()
981
+
982
+ def _resolved(result) -> None:
983
+ if not fut.done():
984
+ fut.set_result(bool(result))
985
+
986
+ try:
987
+ self.push_screen(ConfirmModal(command), _resolved)
988
+ except Exception: # noqa: BLE001 — a broken UI denies, never runs
989
+ return False
990
+ try:
991
+ return await asyncio.wait_for(fut, timeout=300)
992
+ except Exception: # noqa: BLE001 — timeout / cancelled → refuse
993
+ return False
994
+
943
995
  async def _run_one_shot(self, text: str) -> None:
944
996
  from .agent import execute
945
997
 
@@ -952,7 +1004,7 @@ class LitheApp(App):
952
1004
  self._refresh_chrome()
953
1005
  try:
954
1006
  await execute(self.cfg, text, on_event=self.state.on_event,
955
- stop=stop)
1007
+ stop=stop, approver=self.confirm_command)
956
1008
  except Exception as exc: # noqa: BLE001
957
1009
  self.state.say("err", f"运行出错:{exc}")
958
1010
  finally:
@@ -76,6 +76,10 @@ class Workbench:
76
76
  self.store = JsonlRunStore(cfg.store_dir)
77
77
  self.sessions = SessionManager(self.store, cfg.user_id)
78
78
  self.current: dict | None = None
79
+ # Human-confirmation channel for destructive run_command calls
80
+ # (kernel guard). Line-mode front-ends install a stdin y/n approver;
81
+ # the Textual TUI installs a modal one. None → deny with guidance.
82
+ self.approver = None
79
83
  self.turns: dict[int, dict] = {}
80
84
  self.subscribers: list[Callable[[int, dict], None]] = []
81
85
  self._tool_names: list[str] | None = None
@@ -391,6 +395,7 @@ class Workbench:
391
395
  on_event=lambda ev: self._emit(cid, ev),
392
396
  stop=stop,
393
397
  inbox=inbox,
398
+ approver=self.approver,
394
399
  )
395
400
  status = done.get("status")
396
401
  if self.current and self.current["id"] == cid:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lithe-cli
3
- Version: 0.9.2
3
+ Version: 0.9.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
 
@@ -39,7 +39,7 @@ Classifier: Typing :: Typed
39
39
  Requires-Python: >=3.10
40
40
  Description-Content-Type: text/markdown
41
41
  License-File: LICENSE
42
- Requires-Dist: lithe>=0.9.22
42
+ Requires-Dist: lithe>=0.9.24
43
43
  Requires-Dist: prompt-toolkit>=3.0.38
44
44
  Requires-Dist: textual>=1.0
45
45
  Provides-Extra: dev
@@ -24,6 +24,7 @@ lithe_cli.egg-info/requires.txt
24
24
  lithe_cli.egg-info/top_level.txt
25
25
  tests/test_cli.py
26
26
  tests/test_cli_budget_steer.py
27
+ tests/test_cli_guard.py
27
28
  tests/test_cli_sessions.py
28
29
  tests/test_cli_subagents.py
29
30
  tests/test_profiles.py
@@ -1,4 +1,4 @@
1
- lithe>=0.9.22
1
+ lithe>=0.9.24
2
2
  prompt-toolkit>=3.0.38
3
3
  textual>=1.0
4
4
 
@@ -9,7 +9,7 @@ description = "Command-line interface for the lithe agent kernel: chat, one-shot
9
9
  readme = "README.md"
10
10
  license = { file = "LICENSE" }
11
11
  requires-python = ">=3.10"
12
- dependencies = ["lithe>=0.9.22", "prompt-toolkit>=3.0.38", "textual>=1.0"]
12
+ dependencies = ["lithe>=0.9.24", "prompt-toolkit>=3.0.38", "textual>=1.0"]
13
13
  keywords = ["agent", "llm", "react", "cli", "tool-calling"]
14
14
  classifiers = [
15
15
  "Development Status :: 4 - Beta",
@@ -0,0 +1,67 @@
1
+ """lithe-cli: the dangerous-command guard is wired into build_registry.
2
+
3
+ End-to-end through registry dispatch: catastrophic commands never reach a
4
+ runner (safe to test — nothing executes), and the destructive tier asks
5
+ the injected approver. Kernel-side classification tables live in the
6
+ kernel's own test suite; this pins the CLI wiring. Sync tests drive
7
+ coroutines with asyncio.run, the suite's convention (no async plugin).
8
+ """
9
+ from __future__ import annotations
10
+
11
+ import asyncio
12
+
13
+ from lithe import AgentContext
14
+
15
+ from lithe_cli.agent import build_registry, interactive_approver
16
+ from lithe_cli.config import Config
17
+
18
+
19
+ def _ctx() -> AgentContext:
20
+ return AgentContext(run_id="r", user_id="u")
21
+
22
+
23
+ def test_guard_active_when_shell_enabled(tmp_path):
24
+ cfg = Config(store_dir=tmp_path / "s", workspace_dir=tmp_path / "ws",
25
+ shell=True)
26
+ reg = build_registry(cfg)
27
+ res = asyncio.run(reg.dispatch("run_command",
28
+ {"command": "rm -rf /"}, _ctx()))
29
+ assert res.ok is False and res.summary == "危险命令已拦截"
30
+
31
+ # destructive tier with no approver channel: denied with guidance
32
+ res = asyncio.run(reg.dispatch("run_command",
33
+ {"command": "rm -rf node_modules"}, _ctx()))
34
+ assert res.ok is False and res.summary == "需用户审批"
35
+
36
+
37
+ def test_guard_asks_injected_approver(tmp_path):
38
+ cfg = Config(store_dir=tmp_path / "s", workspace_dir=tmp_path / "ws",
39
+ shell=True)
40
+ asked: list[str] = []
41
+
42
+ async def approver(command: str) -> bool:
43
+ asked.append(command)
44
+ return False
45
+
46
+ reg = build_registry(cfg, approver)
47
+ res = asyncio.run(reg.dispatch(
48
+ "run_command", {"command": "git push --force origin main"}, _ctx()))
49
+ assert res.ok is False and res.summary == "用户已拒绝"
50
+ assert asked == ["git push --force origin main"]
51
+
52
+
53
+ def test_guard_absent_without_shell(tmp_path):
54
+ cfg = Config(store_dir=tmp_path / "s", workspace_dir=tmp_path / "ws")
55
+ reg = build_registry(cfg)
56
+ assert "run_command" not in reg.names()
57
+
58
+
59
+ def test_interactive_approver_none_without_tty(monkeypatch):
60
+ import sys as _sys
61
+
62
+ class _FakeStdin:
63
+ def isatty(self) -> bool:
64
+ return False
65
+
66
+ monkeypatch.setattr(_sys, "stdin", _FakeStdin())
67
+ assert interactive_approver() is None
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