langstage-cli 0.6.55__tar.gz → 0.6.57__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.
- {langstage_cli-0.6.55/langstage_cli.egg-info → langstage_cli-0.6.57}/PKG-INFO +2 -1
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/README.md +1 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/langstage_cli/cli.py +183 -33
- {langstage_cli-0.6.55 → langstage_cli-0.6.57/langstage_cli.egg-info}/PKG-INFO +2 -1
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/langstage_cli.egg-info/SOURCES.txt +2 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/pyproject.toml +1 -1
- langstage_cli-0.6.57/tests/test_approval_menu_keys_211.py +201 -0
- langstage_cli-0.6.57/tests/test_decision_shapes_209.py +197 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_interrupt_answer_text_206.py +11 -5
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/LICENSE +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/deepagent_code/__init__.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/langstage_cli/__init__.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/langstage_cli/agui_stream.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/langstage_cli/config.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/langstage_cli/exit_codes.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/langstage_cli/sessions.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/langstage_cli.egg-info/dependency_links.txt +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/langstage_cli.egg-info/entry_points.txt +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/langstage_cli.egg-info/requires.txt +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/langstage_cli.egg-info/top_level.txt +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/setup.cfg +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_advertised_wave4.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_agui_stream.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_approval_menu_abort.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_async_mode.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_checkpointer.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_cli.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_cli_help.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_codeconfig.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_config.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_config_command.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_configurable.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_ctrl_c_sync_tool.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_custom_decision_195.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_deferred_pass.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_dumb_term.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_exit_codes.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_generic_interrupt.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_help_render.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_hidden_option_suggestions.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_history_persist.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_hitl_approval_preview.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_hitl_resume_warning.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_init.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_interrupt_non_tty.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_legacy_config_home.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_load_error.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_menu_allowed_197.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_message_boundaries.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_no_color.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_no_interactive_approve.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_no_persist_resume.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_paused_session_message.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_persist_env.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_quiet_output.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_quiet_tool_result_204.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_reasoning_render.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_rename_shim.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_render_markdown.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_routine_1024.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_session_lifecycle.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_sessions.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_show_config.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_show_config_malformed.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_spec_loading_core.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_spec_resolution.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_spinner_reply_survives.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_stdin_interactive.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_stdin_single_shot.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_stream_mode.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_streaming_marker.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_terminal_width.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_toml_relative_paths.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_tool_card_args.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_tool_output_escape.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_turn_error_verbose.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_turn_exit_and_render.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_unicode_console.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_usage_hint_186.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_verbose_command.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_verify_flag.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_workspace.py +0 -0
- {langstage_cli-0.6.55 → langstage_cli-0.6.57}/tests/test_wrong_type_graph.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: langstage-cli
|
|
3
|
-
Version: 0.6.
|
|
3
|
+
Version: 0.6.57
|
|
4
4
|
Summary: The terminal stage for your LangGraph agent — Claude Code-style CLI for any CompiledGraph
|
|
5
5
|
Author-email: Kedar Dabhadkar <kdabhadk@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -213,6 +213,7 @@ In the interactive loop:
|
|
|
213
213
|
- `/reset` (`/restart`) - Reset the session (clear history and start a new thread)
|
|
214
214
|
- `/quit` (`/q`, `/exit`) - Exit
|
|
215
215
|
- **Tab** autocompletes commands; **Ctrl+C** exits
|
|
216
|
+
- At an approval menu, **↑/↓** or a number moves the highlight and **Enter** confirms; **Esc**, **q**, **Ctrl+D** or **Ctrl+C** leave it with the turn still paused (exit 2)
|
|
216
217
|
|
|
217
218
|
## Environment Variables
|
|
218
219
|
|
|
@@ -175,6 +175,7 @@ In the interactive loop:
|
|
|
175
175
|
- `/reset` (`/restart`) - Reset the session (clear history and start a new thread)
|
|
176
176
|
- `/quit` (`/q`, `/exit`) - Exit
|
|
177
177
|
- **Tab** autocompletes commands; **Ctrl+C** exits
|
|
178
|
+
- At an approval menu, **↑/↓** or a number moves the highlight and **Enter** confirms; **Esc**, **q**, **Ctrl+D** or **Ctrl+C** leave it with the turn still paused (exit 2)
|
|
178
179
|
|
|
179
180
|
## Environment Variables
|
|
180
181
|
|
|
@@ -10,6 +10,7 @@ import json
|
|
|
10
10
|
import os
|
|
11
11
|
import queue
|
|
12
12
|
import re
|
|
13
|
+
import select
|
|
13
14
|
import signal
|
|
14
15
|
import subprocess
|
|
15
16
|
import sys
|
|
@@ -1622,10 +1623,53 @@ print_chunk._streaming_reasoning = False
|
|
|
1622
1623
|
print_chunk._reasoning_node = None
|
|
1623
1624
|
|
|
1624
1625
|
|
|
1626
|
+
# How long a bare Esc waits for the rest of an escape sequence. Terminals send an arrow
|
|
1627
|
+
# key's bytes together, so 50 ms is plenty locally and still fine over ssh; anything
|
|
1628
|
+
# slower is treated as a lone Esc (gh #211).
|
|
1629
|
+
_ESC_SEQUENCE_TIMEOUT = 0.05
|
|
1630
|
+
|
|
1631
|
+
|
|
1632
|
+
def _read_escape_sequence(read_byte, pending) -> str:
|
|
1633
|
+
"""Map the bytes after an ``ESC`` to a key name.
|
|
1634
|
+
|
|
1635
|
+
``pending()`` says whether another byte arrives within the escape timeout, and
|
|
1636
|
+
``read_byte()`` reads one. A lone Esc (nothing follows) is ``"esc"``. ``ESC [ A`` /
|
|
1637
|
+
``ESC O A`` (normal and application cursor mode) are ``"up"``, ``... B`` is
|
|
1638
|
+
``"down"``. Any other sequence (Alt+key, Home, F-keys, modified arrows) is read to
|
|
1639
|
+
its end and ignored (``""``), so its bytes can't leak into the next key.
|
|
1640
|
+
"""
|
|
1641
|
+
if not pending():
|
|
1642
|
+
return "esc"
|
|
1643
|
+
ch2 = read_byte()
|
|
1644
|
+
if ch2 not in ("[", "O"):
|
|
1645
|
+
return ""
|
|
1646
|
+
body = ""
|
|
1647
|
+
# A CSI sequence ends at a byte in 0x40-0x7E; parameters (digits, ';') come before.
|
|
1648
|
+
for _ in range(16):
|
|
1649
|
+
if not pending():
|
|
1650
|
+
return ""
|
|
1651
|
+
ch = read_byte()
|
|
1652
|
+
body += ch
|
|
1653
|
+
if "@" <= ch <= "~":
|
|
1654
|
+
break
|
|
1655
|
+
if body == "A":
|
|
1656
|
+
return "up"
|
|
1657
|
+
if body == "B":
|
|
1658
|
+
return "down"
|
|
1659
|
+
return ""
|
|
1660
|
+
|
|
1661
|
+
|
|
1625
1662
|
def get_key() -> str:
|
|
1626
|
-
"""Read a single keypress from stdin (cross-platform).
|
|
1663
|
+
"""Read a single keypress from stdin (cross-platform).
|
|
1664
|
+
|
|
1665
|
+
Returns ``"up"``, ``"down"``, ``"enter"``, ``"ctrl-c"``, ``"ctrl-d"``, ``"esc"``,
|
|
1666
|
+
the character typed, or ``""`` for a key the menus don't use. A bare Esc returns
|
|
1667
|
+
at once instead of blocking for a second byte, which used to swallow the next
|
|
1668
|
+
keypress (often the Ctrl-C meant to leave, gh #211).
|
|
1669
|
+
"""
|
|
1627
1670
|
if IS_WINDOWS:
|
|
1628
|
-
# Windows implementation using msvcrt
|
|
1671
|
+
# Windows implementation using msvcrt. Special keys come as a \x00 / \xe0
|
|
1672
|
+
# prefix plus a scan code, so a lone \x1b is always Esc.
|
|
1629
1673
|
ch = msvcrt.getch()
|
|
1630
1674
|
if ch in (b"\x00", b"\xe0"): # Special keys (arrows, function keys)
|
|
1631
1675
|
ch2 = msvcrt.getch()
|
|
@@ -1633,41 +1677,64 @@ def get_key() -> str:
|
|
|
1633
1677
|
return "up"
|
|
1634
1678
|
elif ch2 == b"P":
|
|
1635
1679
|
return "down"
|
|
1636
|
-
return
|
|
1637
|
-
elif ch
|
|
1680
|
+
return "" # Home, F-keys, ...: a scan code, not a character
|
|
1681
|
+
elif ch in (b"\r", b"\n"):
|
|
1638
1682
|
return "enter"
|
|
1639
1683
|
elif ch == b"\x03": # Ctrl+C
|
|
1640
1684
|
return "ctrl-c"
|
|
1685
|
+
elif ch in (b"\x04", b"\x1a"): # Ctrl+D, or Ctrl+Z (the Windows console's EOF)
|
|
1686
|
+
return "ctrl-d"
|
|
1687
|
+
elif ch == b"\x1b":
|
|
1688
|
+
return "esc"
|
|
1641
1689
|
return ch.decode("utf-8", errors="ignore")
|
|
1642
1690
|
else:
|
|
1643
|
-
# Unix implementation using termios/tty
|
|
1691
|
+
# Unix implementation using termios/tty. Bytes are read straight from the fd:
|
|
1692
|
+
# sys.stdin's text buffer could hold the rest of an escape sequence where
|
|
1693
|
+
# select() can't see it, which would turn an arrow key into an Esc.
|
|
1644
1694
|
fd = sys.stdin.fileno()
|
|
1645
1695
|
old_settings = termios.tcgetattr(fd)
|
|
1696
|
+
|
|
1697
|
+
def read_byte() -> str:
|
|
1698
|
+
return os.read(fd, 1).decode("latin-1")
|
|
1699
|
+
|
|
1700
|
+
def pending() -> bool:
|
|
1701
|
+
return bool(select.select([fd], [], [], _ESC_SEQUENCE_TIMEOUT)[0])
|
|
1702
|
+
|
|
1646
1703
|
try:
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1704
|
+
# TCSANOW, not setraw's default TCSAFLUSH: a flush would throw away keys
|
|
1705
|
+
# typed ahead (the Enter of a quick "3⏎") before this read could see them.
|
|
1706
|
+
tty.setraw(fd, termios.TCSANOW)
|
|
1707
|
+
ch = read_byte()
|
|
1708
|
+
if ch == "": # end of input
|
|
1709
|
+
return "ctrl-d"
|
|
1650
1710
|
if ch == "\x1b":
|
|
1651
|
-
|
|
1652
|
-
if ch2 == "[":
|
|
1653
|
-
ch3 = sys.stdin.read(1)
|
|
1654
|
-
if ch3 == "A":
|
|
1655
|
-
return "up"
|
|
1656
|
-
elif ch3 == "B":
|
|
1657
|
-
return "down"
|
|
1711
|
+
return _read_escape_sequence(read_byte, pending)
|
|
1658
1712
|
elif ch == "\r" or ch == "\n":
|
|
1659
1713
|
return "enter"
|
|
1660
1714
|
elif ch == "\x03": # Ctrl+C
|
|
1661
1715
|
return "ctrl-c"
|
|
1716
|
+
elif ch == "\x04": # Ctrl+D
|
|
1717
|
+
return "ctrl-d"
|
|
1662
1718
|
return ch
|
|
1663
1719
|
finally:
|
|
1664
1720
|
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
|
|
1665
1721
|
|
|
1666
1722
|
|
|
1723
|
+
# Keys that leave the arrow-key menu without deciding anything, like the dumb-terminal
|
|
1724
|
+
# menu's Ctrl-C / end of input: exit 2, the turn stays paused (gh #174, gh #211).
|
|
1725
|
+
_MENU_LEAVE_KEYS = ("ctrl-c", "ctrl-d", "esc", "q", "Q")
|
|
1726
|
+
|
|
1727
|
+
|
|
1667
1728
|
def select_option(options: List[str], prompt: str = "Select an option:") -> int:
|
|
1668
1729
|
"""
|
|
1669
1730
|
Interactive option selector using arrow keys.
|
|
1670
1731
|
|
|
1732
|
+
Up/Down move the highlight, a digit ``1``-``N`` moves it to that option (the dumb
|
|
1733
|
+
terminal menu's numbering), and Enter picks the highlighted option. Ctrl-C,
|
|
1734
|
+
Ctrl-D, Esc and ``q`` leave the menu: exit 2, the turn stays paused. Any other key
|
|
1735
|
+
is ignored. Before gh #211 every key but the arrows was ignored, so typing ``3``
|
|
1736
|
+
then Enter to pick **Exit** approved the action that was highlighted.
|
|
1737
|
+
|
|
1671
1738
|
Args:
|
|
1672
1739
|
options: List of option strings to display
|
|
1673
1740
|
prompt: Prompt to show above options
|
|
@@ -1685,6 +1752,7 @@ def select_option(options: List[str], prompt: str = "Select an option:") -> int:
|
|
|
1685
1752
|
|
|
1686
1753
|
try:
|
|
1687
1754
|
print(f"\n{BOLD}{prompt}{RESET}")
|
|
1755
|
+
print(f"{DIM} ↑/↓ or 1-{num_options} to choose · Enter to confirm · Esc to leave{RESET}")
|
|
1688
1756
|
|
|
1689
1757
|
# Print initial options
|
|
1690
1758
|
for i, opt in enumerate(options):
|
|
@@ -1703,15 +1771,20 @@ def select_option(options: List[str], prompt: str = "Select an option:") -> int:
|
|
|
1703
1771
|
selected -= 1
|
|
1704
1772
|
elif key == "down" and selected < num_options - 1:
|
|
1705
1773
|
selected += 1
|
|
1774
|
+
elif len(key) == 1 and key.isdigit() and 1 <= int(key) <= num_options:
|
|
1775
|
+
selected = int(key) - 1
|
|
1706
1776
|
elif key == "enter":
|
|
1707
1777
|
break
|
|
1708
|
-
elif key
|
|
1709
|
-
#
|
|
1710
|
-
# was decided and the turn stays paused on the interrupt, so
|
|
1711
|
-
# (ADR 0007).
|
|
1712
|
-
# `langstage-cli ... && next-step` run on an aborted approval (gh #174)
|
|
1778
|
+
elif key in _MENU_LEAVE_KEYS:
|
|
1779
|
+
# Leaving abandons the prompt the same way the menu's Exit does:
|
|
1780
|
+
# nothing was decided and the turn stays paused on the interrupt, so
|
|
1781
|
+
# exit 2 (ADR 0007). Ctrl-C used to exit 0, "turn completed", which let
|
|
1782
|
+
# `langstage-cli ... && next-step` run on an aborted approval (gh #174);
|
|
1783
|
+
# Ctrl-D and Esc were ignored (gh #211).
|
|
1713
1784
|
print("\033[?25h", end="") # Show cursor
|
|
1714
1785
|
sys.exit(EXIT_PAUSED)
|
|
1786
|
+
else:
|
|
1787
|
+
continue # an ignored key: nothing to redraw
|
|
1715
1788
|
|
|
1716
1789
|
# Move cursor up to redraw options
|
|
1717
1790
|
print(f"\033[{num_options}A", end="")
|
|
@@ -1968,12 +2041,13 @@ def _all_actions_allow(verb: str, num_actions: int, interrupt: Optional[Dict[str
|
|
|
1968
2041
|
def _decision_shapes(allowed: List[str], interrupt: Optional[Dict[str, Any]] = None) -> str:
|
|
1969
2042
|
"""One line showing the JSON shape of each allowed decision.
|
|
1970
2043
|
|
|
1971
|
-
The fields are LangChain HumanInTheLoopMiddleware's
|
|
1972
|
-
|
|
2044
|
+
The fields are LangChain HumanInTheLoopMiddleware's. Every interrupt core checks
|
|
2045
|
+
as one (:func:`_is_hitl_request`) gets them, so a shape the hint shows is a shape
|
|
2046
|
+
core accepts (gh #209). Other interrupts get the bare ``{"type": ...}`` of each verb.
|
|
1973
2047
|
"""
|
|
1974
2048
|
shapes = (
|
|
1975
2049
|
{}
|
|
1976
|
-
if not
|
|
2050
|
+
if not _is_hitl_request(interrupt)
|
|
1977
2051
|
else {
|
|
1978
2052
|
"approve": '{"type": "approve"}',
|
|
1979
2053
|
"reject": '{"type": "reject", "message": "why"}',
|
|
@@ -1987,9 +2061,28 @@ def _decision_shapes(allowed: List[str], interrupt: Optional[Dict[str, Any]] = N
|
|
|
1987
2061
|
return " | ".join(shapes.get(v, f'{{"type": "{v}"}}') for v in order)
|
|
1988
2062
|
|
|
1989
2063
|
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
2064
|
+
# Key under which _stream_turn puts the graph's own pending interrupt value on the
|
|
2065
|
+
# interrupt payload it hands the approval prompt (gh #209).
|
|
2066
|
+
_PENDING_VALUE = "_pending_value"
|
|
2067
|
+
|
|
2068
|
+
|
|
2069
|
+
def _is_hitl_request(interrupt: Optional[Dict[str, Any]]) -> bool:
|
|
2070
|
+
"""Whether core checks answers to this interrupt as a HITL request (gh #209).
|
|
2071
|
+
|
|
2072
|
+
Core's resume path validates a decision when the graph's pending interrupt value
|
|
2073
|
+
is a dict with an ``action_requests`` list (``langstage_core.resume``), whether
|
|
2074
|
+
or not it carries ``review_configs``. The frame can't tell: core normalizes every
|
|
2075
|
+
interrupt (a legacy ``HumanInterrupt`` list, a plain dict) into ``action_requests``.
|
|
2076
|
+
So the turn loop reads the graph's own value and puts it on the payload under
|
|
2077
|
+
``_PENDING_VALUE``; this applies core's test to it. Without it (no checkpointer,
|
|
2078
|
+
or more than one interrupt pending) a non-empty ``review_configs`` is the sign.
|
|
2079
|
+
"""
|
|
2080
|
+
if not isinstance(interrupt, dict):
|
|
2081
|
+
return False
|
|
2082
|
+
if _PENDING_VALUE in interrupt:
|
|
2083
|
+
value = interrupt[_PENDING_VALUE]
|
|
2084
|
+
return isinstance(value, dict) and isinstance(value.get("action_requests"), list)
|
|
2085
|
+
configs = interrupt.get("review_configs")
|
|
1993
2086
|
return isinstance(configs, list) and bool(configs)
|
|
1994
2087
|
|
|
1995
2088
|
|
|
@@ -2026,13 +2119,14 @@ def _check_custom_decision(
|
|
|
2026
2119
|
f"allowed: {', '.join(_allowed_verbs(interrupt, i))}."
|
|
2027
2120
|
)
|
|
2028
2121
|
|
|
2029
|
-
#
|
|
2030
|
-
#
|
|
2031
|
-
#
|
|
2032
|
-
if
|
|
2122
|
+
# A HITL request is checked by core, with the same rules and against the same
|
|
2123
|
+
# value its resume path checks (core gh #227, gh #209). Other interrupts read
|
|
2124
|
+
# their own payload, so only the verb is checked here.
|
|
2125
|
+
if _is_hitl_request(interrupt):
|
|
2033
2126
|
from langstage_core import decision_problems
|
|
2034
2127
|
|
|
2035
|
-
|
|
2128
|
+
target = interrupt.get(_PENDING_VALUE, interrupt)
|
|
2129
|
+
problems = decision_problems({"decisions": list(items)}, target)
|
|
2036
2130
|
if problems:
|
|
2037
2131
|
return [], "; ".join(problems) + "."
|
|
2038
2132
|
return list(items), None
|
|
@@ -2068,7 +2162,9 @@ def print_help():
|
|
|
2068
2162
|
print(f"{DIM}{'─' * 40}{RESET}")
|
|
2069
2163
|
print(f" {CYAN}Tab{RESET} Autocomplete commands")
|
|
2070
2164
|
print(f" {CYAN}Ctrl+C{RESET} Exit at any time")
|
|
2071
|
-
print(
|
|
2165
|
+
print(
|
|
2166
|
+
f" {CYAN}↑/↓{RESET}, {CYAN}1-9{RESET} Choose an approval option ({CYAN}Esc{RESET} leaves)"
|
|
2167
|
+
)
|
|
2072
2168
|
print()
|
|
2073
2169
|
|
|
2074
2170
|
|
|
@@ -2594,6 +2690,33 @@ async def _pending_interrupt(agent: Any, thread_id: str) -> bool:
|
|
|
2594
2690
|
return any(getattr(task, "interrupts", None) for task in getattr(state, "tasks", ()) or ())
|
|
2595
2691
|
|
|
2596
2692
|
|
|
2693
|
+
async def _pending_interrupt_value(agent: Any, thread_id: str) -> Tuple[bool, Any]:
|
|
2694
|
+
"""``(True, value)`` for the thread's one pending interrupt, else ``(False, None)``.
|
|
2695
|
+
|
|
2696
|
+
This is the value core's resume path checks a decision against, before the
|
|
2697
|
+
frame normalizes it for display (gh #209). Like core, only a single pending
|
|
2698
|
+
interrupt is read; with none, several, or no readable state nothing is found.
|
|
2699
|
+
"""
|
|
2700
|
+
graph = getattr(agent, "graph", None)
|
|
2701
|
+
if not thread_id or graph is None or not hasattr(graph, "aget_state"):
|
|
2702
|
+
return False, None
|
|
2703
|
+
try:
|
|
2704
|
+
state = await graph.aget_state({"configurable": {"thread_id": thread_id}})
|
|
2705
|
+
except Exception: # noqa: BLE001 - no checkpointer / no state: nothing to read
|
|
2706
|
+
return False, None
|
|
2707
|
+
tasks = getattr(state, "tasks", None) or ()
|
|
2708
|
+
pending = [i for t in tasks for i in (getattr(t, "interrupts", None) or ())]
|
|
2709
|
+
if len(pending) != 1:
|
|
2710
|
+
return False, None
|
|
2711
|
+
return True, getattr(pending[0], "value", None)
|
|
2712
|
+
|
|
2713
|
+
|
|
2714
|
+
# The prefix of the error core raises when it refuses a decision before resuming
|
|
2715
|
+
# (``langstage_core.resume.InvalidDecisionError``): nothing was sent, and the same
|
|
2716
|
+
# approval is still pending (core gh #227).
|
|
2717
|
+
_REFUSED_DECISION = "InvalidDecisionError:"
|
|
2718
|
+
|
|
2719
|
+
|
|
2597
2720
|
async def _stream_turn(
|
|
2598
2721
|
agent: Any, message: str, thread_id: str, interactive: bool, verbose: bool
|
|
2599
2722
|
) -> tuple[bool, bool, bool]:
|
|
@@ -2622,8 +2745,11 @@ async def _stream_turn(
|
|
|
2622
2745
|
tool_results: List[str] = [] # results since the last reply text / tool call
|
|
2623
2746
|
resume = None # first pass sends the message; later passes carry the decision
|
|
2624
2747
|
|
|
2748
|
+
pending_prompt = None # the last interrupt asked, to ask again if refused (gh #209)
|
|
2749
|
+
|
|
2625
2750
|
while True:
|
|
2626
2751
|
has_interrupt = False
|
|
2752
|
+
refused_decision = False
|
|
2627
2753
|
num_pending_actions = 0
|
|
2628
2754
|
is_generic_interrupt = False
|
|
2629
2755
|
first_chunk = True
|
|
@@ -2639,6 +2765,21 @@ async def _stream_turn(
|
|
|
2639
2765
|
if spinner:
|
|
2640
2766
|
spinner.stop()
|
|
2641
2767
|
first_chunk = False
|
|
2768
|
+
if (
|
|
2769
|
+
resume is not None
|
|
2770
|
+
and interactive
|
|
2771
|
+
and pending_prompt is not None
|
|
2772
|
+
and chunk.get("status") == "error"
|
|
2773
|
+
and str(chunk.get("error", "")).startswith(_REFUSED_DECISION)
|
|
2774
|
+
):
|
|
2775
|
+
# Core refused the answer before resuming, so nothing was sent
|
|
2776
|
+
# and the approval is still pending. Say so in place and ask it
|
|
2777
|
+
# again, rather than failing the turn and dropping the user at
|
|
2778
|
+
# the main prompt with the approval unanswered (gh #209).
|
|
2779
|
+
refused = str(chunk["error"])[len(_REFUSED_DECISION) :].strip()
|
|
2780
|
+
print(f"{RED}⏺ {_escape_controls(refused)}{RESET}")
|
|
2781
|
+
refused_decision = True
|
|
2782
|
+
continue
|
|
2642
2783
|
print_chunk(chunk, verbose=verbose)
|
|
2643
2784
|
if chunk.get("status") == "error":
|
|
2644
2785
|
had_error = True
|
|
@@ -2664,7 +2805,16 @@ async def _stream_turn(
|
|
|
2664
2805
|
spinner.stop()
|
|
2665
2806
|
_flush_markdown() # a stream that ends without `complete` keeps no held text
|
|
2666
2807
|
|
|
2667
|
-
if
|
|
2808
|
+
if refused_decision and not has_interrupt:
|
|
2809
|
+
num, generic, data = pending_prompt
|
|
2810
|
+
resume = handle_interrupt_input(num, is_generic=generic, interrupt=data)
|
|
2811
|
+
elif has_interrupt and interactive:
|
|
2812
|
+
# Check a custom decision against the value core will check it against,
|
|
2813
|
+
# not the frame's display copy (gh #209).
|
|
2814
|
+
found, value = await _pending_interrupt_value(agent, thread_id)
|
|
2815
|
+
if found and isinstance(interrupt_data, dict):
|
|
2816
|
+
interrupt_data = {**interrupt_data, _PENDING_VALUE: value}
|
|
2817
|
+
pending_prompt = (num_pending_actions, is_generic_interrupt, interrupt_data)
|
|
2668
2818
|
resume = handle_interrupt_input(
|
|
2669
2819
|
num_pending_actions, is_generic=is_generic_interrupt, interrupt=interrupt_data
|
|
2670
2820
|
)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: langstage-cli
|
|
3
|
-
Version: 0.6.
|
|
3
|
+
Version: 0.6.57
|
|
4
4
|
Summary: The terminal stage for your LangGraph agent — Claude Code-style CLI for any CompiledGraph
|
|
5
5
|
Author-email: Kedar Dabhadkar <kdabhadk@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -213,6 +213,7 @@ In the interactive loop:
|
|
|
213
213
|
- `/reset` (`/restart`) - Reset the session (clear history and start a new thread)
|
|
214
214
|
- `/quit` (`/q`, `/exit`) - Exit
|
|
215
215
|
- **Tab** autocompletes commands; **Ctrl+C** exits
|
|
216
|
+
- At an approval menu, **↑/↓** or a number moves the highlight and **Enter** confirms; **Esc**, **q**, **Ctrl+D** or **Ctrl+C** leave it with the turn still paused (exit 2)
|
|
216
217
|
|
|
217
218
|
## Environment Variables
|
|
218
219
|
|
|
@@ -17,6 +17,7 @@ langstage_cli.egg-info/top_level.txt
|
|
|
17
17
|
tests/test_advertised_wave4.py
|
|
18
18
|
tests/test_agui_stream.py
|
|
19
19
|
tests/test_approval_menu_abort.py
|
|
20
|
+
tests/test_approval_menu_keys_211.py
|
|
20
21
|
tests/test_async_mode.py
|
|
21
22
|
tests/test_checkpointer.py
|
|
22
23
|
tests/test_cli.py
|
|
@@ -27,6 +28,7 @@ tests/test_config_command.py
|
|
|
27
28
|
tests/test_configurable.py
|
|
28
29
|
tests/test_ctrl_c_sync_tool.py
|
|
29
30
|
tests/test_custom_decision_195.py
|
|
31
|
+
tests/test_decision_shapes_209.py
|
|
30
32
|
tests/test_deferred_pass.py
|
|
31
33
|
tests/test_dumb_term.py
|
|
32
34
|
tests/test_exit_codes.py
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "langstage-cli"
|
|
7
|
-
version = "0.6.
|
|
7
|
+
version = "0.6.57"
|
|
8
8
|
description = "The terminal stage for your LangGraph agent — Claude Code-style CLI for any CompiledGraph"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.11"
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"""The arrow-key approval menu never approves on a key that doesn't mean "approve"
|
|
2
|
+
(gh #211).
|
|
3
|
+
|
|
4
|
+
- Digits were ignored, so typing ``3`` then Enter (meaning **Exit**, the dumb-terminal
|
|
5
|
+
menu's numbering) confirmed the highlighted **Approve**. A digit now moves the
|
|
6
|
+
highlight to that option; Enter still confirms.
|
|
7
|
+
- Ctrl-D was ignored (the menu redrew) and Esc blocked waiting for a CSI byte, so it
|
|
8
|
+
swallowed the next Ctrl-C. Ctrl-D, Esc and ``q`` now leave the menu like Ctrl-C:
|
|
9
|
+
exit 2, the turn stays paused.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import io
|
|
13
|
+
import os
|
|
14
|
+
import sys
|
|
15
|
+
import textwrap
|
|
16
|
+
from contextlib import redirect_stdout
|
|
17
|
+
|
|
18
|
+
import pytest
|
|
19
|
+
from click.testing import CliRunner
|
|
20
|
+
|
|
21
|
+
from langstage_cli import cli
|
|
22
|
+
from langstage_cli.cli import main
|
|
23
|
+
|
|
24
|
+
_OPTIONS = ["Approve all actions", "Provide custom decision (JSON)", "Exit"]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _keys(monkeypatch, *keys):
|
|
28
|
+
it = iter(keys)
|
|
29
|
+
seen = []
|
|
30
|
+
|
|
31
|
+
def fake_get_key():
|
|
32
|
+
key = next(it)
|
|
33
|
+
seen.append(key)
|
|
34
|
+
return key
|
|
35
|
+
|
|
36
|
+
monkeypatch.setattr(cli, "get_key", fake_get_key)
|
|
37
|
+
return seen
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _select(options=_OPTIONS):
|
|
41
|
+
buf = io.StringIO()
|
|
42
|
+
with redirect_stdout(buf):
|
|
43
|
+
choice = cli.select_option(options, "How would you like to proceed?")
|
|
44
|
+
return choice, buf.getvalue()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# ---- digits ------------------------------------------------------------------------
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@pytest.mark.parametrize("digit, index", [("1", 0), ("2", 1), ("3", 2)])
|
|
51
|
+
def test_a_digit_then_enter_picks_that_option(digit, index, monkeypatch):
|
|
52
|
+
_keys(monkeypatch, digit, "enter")
|
|
53
|
+
assert _select()[0] == index
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_three_enter_picks_exit_not_approve(monkeypatch):
|
|
57
|
+
# The issue's repro: "3" means Exit; it used to approve.
|
|
58
|
+
_keys(monkeypatch, "3", "enter")
|
|
59
|
+
choice, out = _select()
|
|
60
|
+
assert _OPTIONS[choice] == "Exit"
|
|
61
|
+
assert "❯ Exit" in out # the highlight moved, so the user sees what Enter picks
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@pytest.mark.parametrize("key", ["0", "4", "9", "x", "", " "])
|
|
65
|
+
def test_out_of_range_digits_and_other_keys_are_ignored(key, monkeypatch):
|
|
66
|
+
# Ignored keys don't change the highlight, and don't redraw.
|
|
67
|
+
_keys(monkeypatch, "down", key, "enter")
|
|
68
|
+
choice, out = _select()
|
|
69
|
+
assert choice == 1
|
|
70
|
+
assert out.count("\033[3A") == 1 # one redraw, for the "down"
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_digit_then_arrows_still_navigate(monkeypatch):
|
|
74
|
+
_keys(monkeypatch, "3", "up", "enter")
|
|
75
|
+
assert _select()[0] == 1
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_the_menu_shows_its_keys(monkeypatch):
|
|
79
|
+
_keys(monkeypatch, "enter")
|
|
80
|
+
_, out = _select()
|
|
81
|
+
assert "1-3 to choose" in out
|
|
82
|
+
assert "Esc to leave" in out
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# ---- leaving -----------------------------------------------------------------------
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@pytest.mark.parametrize("key", ["ctrl-d", "esc", "q", "Q"])
|
|
89
|
+
def test_leave_keys_exit_paused_and_restore_the_cursor(key, monkeypatch):
|
|
90
|
+
_keys(monkeypatch, "down", key)
|
|
91
|
+
buf = io.StringIO()
|
|
92
|
+
with redirect_stdout(buf), pytest.raises(SystemExit) as exc:
|
|
93
|
+
cli.select_option(_OPTIONS, "How would you like to proceed?")
|
|
94
|
+
assert exc.value.code == 2
|
|
95
|
+
assert buf.getvalue().endswith("\033[?25h")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
_ACTION_AGENT = textwrap.dedent(
|
|
99
|
+
"""
|
|
100
|
+
from langgraph.graph import StateGraph, START, END
|
|
101
|
+
from langgraph.graph.message import MessagesState
|
|
102
|
+
from langgraph.checkpoint.memory import MemorySaver
|
|
103
|
+
from langgraph.types import interrupt
|
|
104
|
+
from langchain_core.messages import AIMessage
|
|
105
|
+
|
|
106
|
+
def ask(state):
|
|
107
|
+
decision = interrupt({"action": "delete_file", "path": "/etc/hosts"})
|
|
108
|
+
return {"messages": [AIMessage(content=f"Decision was: {decision!r}")]}
|
|
109
|
+
|
|
110
|
+
g = StateGraph(MessagesState)
|
|
111
|
+
g.add_node("ask", ask)
|
|
112
|
+
g.add_edge(START, "ask")
|
|
113
|
+
g.add_edge("ask", END)
|
|
114
|
+
graph = g.compile(checkpointer=MemorySaver())
|
|
115
|
+
"""
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@pytest.mark.parametrize(
|
|
120
|
+
"keys", [("3", "enter"), ("esc",), ("ctrl-d",)], ids=["3-enter", "esc", "ctrl-d"]
|
|
121
|
+
)
|
|
122
|
+
def test_single_shot_never_resumes_on_a_leave(keys, tmp_path, monkeypatch):
|
|
123
|
+
monkeypatch.chdir(tmp_path)
|
|
124
|
+
monkeypatch.setattr(cli, "_is_a_tty", lambda *a, **k: True)
|
|
125
|
+
(tmp_path / "hitl_agent.py").write_text(_ACTION_AGENT, encoding="utf-8")
|
|
126
|
+
_keys(monkeypatch, *keys)
|
|
127
|
+
r = CliRunner().invoke(main, ["-a", "hitl_agent.py:graph", "go"])
|
|
128
|
+
assert r.exit_code == 2, r.output
|
|
129
|
+
assert "Decision was" not in r.output # nothing was resumed
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
# ---- escape sequences ----------------------------------------------------------------
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _feed(data: str):
|
|
136
|
+
"""A ``read_byte``/``pending`` pair over a fixed byte string (the rest of the input)."""
|
|
137
|
+
buf = list(data)
|
|
138
|
+
|
|
139
|
+
def read_byte():
|
|
140
|
+
return buf.pop(0)
|
|
141
|
+
|
|
142
|
+
def pending():
|
|
143
|
+
return bool(buf)
|
|
144
|
+
|
|
145
|
+
return read_byte, pending, buf
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
@pytest.mark.parametrize(
|
|
149
|
+
"rest, key",
|
|
150
|
+
[
|
|
151
|
+
("", "esc"), # a lone Esc returns at once
|
|
152
|
+
("[A", "up"),
|
|
153
|
+
("[B", "down"),
|
|
154
|
+
("OA", "up"), # application cursor mode
|
|
155
|
+
("OB", "down"),
|
|
156
|
+
("[1;5A", ""), # Ctrl+Up: read whole, ignored
|
|
157
|
+
("[H", ""), # Home
|
|
158
|
+
("[15~", ""), # F5
|
|
159
|
+
("x", ""), # Alt+x
|
|
160
|
+
],
|
|
161
|
+
)
|
|
162
|
+
def test_escape_sequences(rest, key):
|
|
163
|
+
read_byte, pending, buf = _feed(rest)
|
|
164
|
+
assert cli._read_escape_sequence(read_byte, pending) == key
|
|
165
|
+
assert buf == [] # the whole sequence was consumed, nothing leaks into the next key
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def test_escape_sequence_stops_at_its_final_byte():
|
|
169
|
+
read_byte, pending, buf = _feed("[A3")
|
|
170
|
+
assert cli._read_escape_sequence(read_byte, pending) == "up"
|
|
171
|
+
assert buf == ["3"]
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
@pytest.mark.skipif(sys.platform == "win32", reason="POSIX pty")
|
|
175
|
+
def test_get_key_lone_esc_does_not_swallow_the_next_key(monkeypatch):
|
|
176
|
+
# The real get_key over a pty: Esc alone returns "esc", and the Ctrl-C typed after
|
|
177
|
+
# it is still there for the next read (it used to be consumed as the CSI byte).
|
|
178
|
+
import pty
|
|
179
|
+
import tty
|
|
180
|
+
|
|
181
|
+
master, slave = pty.openpty()
|
|
182
|
+
# Raw from the start, as get_key leaves it mid-menu: in the pty's default cooked
|
|
183
|
+
# mode the line discipline would eat the Ctrl-C byte as a signal before get_key
|
|
184
|
+
# switched to raw, and the read would block forever.
|
|
185
|
+
tty.setraw(slave)
|
|
186
|
+
try:
|
|
187
|
+
stdin = os.fdopen(slave, "r", closefd=False)
|
|
188
|
+
monkeypatch.setattr(sys, "stdin", stdin)
|
|
189
|
+
os.write(master, b"\x1b")
|
|
190
|
+
assert cli.get_key() == "esc"
|
|
191
|
+
os.write(master, b"\x03")
|
|
192
|
+
assert cli.get_key() == "ctrl-c"
|
|
193
|
+
os.write(master, b"\x1b[B")
|
|
194
|
+
assert cli.get_key() == "down"
|
|
195
|
+
os.write(master, b"3")
|
|
196
|
+
assert cli.get_key() == "3"
|
|
197
|
+
os.write(master, b"\x04")
|
|
198
|
+
assert cli.get_key() == "ctrl-d"
|
|
199
|
+
finally:
|
|
200
|
+
os.close(master)
|
|
201
|
+
os.close(slave)
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"""The custom decision prompt shows shapes core accepts, and checks them the way core
|
|
2
|
+
does, for every interrupt core checks (gh #209).
|
|
3
|
+
|
|
4
|
+
The keyless demo's approval (``langstage_core.demo.tools``, "ask me") is a dict with
|
|
5
|
+
``action_requests`` and ``allowed_decisions`` but no ``review_configs``. Core checks an
|
|
6
|
+
answer to it as a HITL request, so ``respond`` needs a text ``message``. The CLI only
|
|
7
|
+
treated interrupts with ``review_configs`` that way: the prompt hinted a bare
|
|
8
|
+
``{"type": "respond"}``, let it through, and core's refusal came back as a failed turn
|
|
9
|
+
that dropped the user at the main prompt with the approval still pending.
|
|
10
|
+
|
|
11
|
+
Now the CLI reads the graph's own pending value and applies core's test to it, and a
|
|
12
|
+
decision core still refuses brings the approval menu back instead of failing the turn.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import io
|
|
16
|
+
import os
|
|
17
|
+
import subprocess
|
|
18
|
+
import sys
|
|
19
|
+
import time
|
|
20
|
+
from contextlib import redirect_stdout
|
|
21
|
+
|
|
22
|
+
import pytest
|
|
23
|
+
|
|
24
|
+
from langstage_cli import cli
|
|
25
|
+
|
|
26
|
+
# What core puts on the frame for the demo's approval, and the graph's own value.
|
|
27
|
+
_DEMO_VALUE = {
|
|
28
|
+
"action_requests": [{"action": "ask_user", "args": {"question": "What should I call you?"}}],
|
|
29
|
+
"allowed_decisions": ["respond", "approve"],
|
|
30
|
+
}
|
|
31
|
+
_DEMO_FRAME = {**_DEMO_VALUE, "review_configs": []}
|
|
32
|
+
_DEMO = {**_DEMO_FRAME, cli._PENDING_VALUE: _DEMO_VALUE}
|
|
33
|
+
|
|
34
|
+
# A legacy HumanInterrupt list: core normalizes it into action_requests on the frame,
|
|
35
|
+
# but doesn't check answers to it (it reads its own payload).
|
|
36
|
+
_LEGACY = {
|
|
37
|
+
"action_requests": [{"action": "write_file", "args": {"path": "x"}}],
|
|
38
|
+
"review_configs": [],
|
|
39
|
+
"allowed_decisions": ["approve", "respond"],
|
|
40
|
+
cli._PENDING_VALUE: [{"action_request": {"action": "write_file", "args": {"path": "x"}}}],
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_core_test_is_applied_to_the_graphs_own_value():
|
|
45
|
+
assert cli._is_hitl_request(_DEMO)
|
|
46
|
+
assert not cli._is_hitl_request(_LEGACY)
|
|
47
|
+
# Without the graph's value (no checkpointer), review_configs is still the sign.
|
|
48
|
+
assert not cli._is_hitl_request(_DEMO_FRAME)
|
|
49
|
+
assert cli._is_hitl_request({**_DEMO_FRAME, "review_configs": [{"action_name": "a"}]})
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_the_hint_shows_the_message_respond_needs():
|
|
53
|
+
shapes = cli._decision_shapes(cli._allowed_verbs(_DEMO, 0), _DEMO)
|
|
54
|
+
assert shapes == '{"type": "approve"} | {"type": "respond", "message": "reply"}'
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def test_respond_without_a_message_is_caught_in_the_prompt():
|
|
58
|
+
decisions, problem = cli._check_custom_decision({"type": "respond"}, 1, _DEMO)
|
|
59
|
+
assert decisions == []
|
|
60
|
+
assert 'respond needs a text "message"' in problem
|
|
61
|
+
ok = {"type": "respond", "message": "Ada"}
|
|
62
|
+
assert cli._check_custom_decision(ok, 1, _DEMO) == ([ok], None)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_an_interrupt_core_doesnt_check_still_gets_only_the_verb_check():
|
|
66
|
+
assert cli._check_custom_decision({"type": "respond"}, 1, _LEGACY)[1] is None
|
|
67
|
+
assert "not allowed" in cli._check_custom_decision({"type": "reject"}, 1, _LEGACY)[1]
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# ---- End to end, in process, on the keyless demo ------------------------------------
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _demo_agent():
|
|
74
|
+
pytest.importorskip("ag_ui_langgraph")
|
|
75
|
+
from langgraph.checkpoint.memory import MemorySaver
|
|
76
|
+
from langstage_core.demo.tools import graph
|
|
77
|
+
|
|
78
|
+
from langstage_cli.agui_stream import build_session_agent
|
|
79
|
+
|
|
80
|
+
return build_session_agent(graph.copy(update={"checkpointer": MemorySaver()}))
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _menu(monkeypatch, choices, typed):
|
|
84
|
+
monkeypatch.setattr(cli, "_is_a_tty", lambda *a, **k: True)
|
|
85
|
+
picks, lines, shown = iter(choices), iter(typed), []
|
|
86
|
+
|
|
87
|
+
def select(options, prompt=""):
|
|
88
|
+
shown.append(list(options))
|
|
89
|
+
return next(picks)
|
|
90
|
+
|
|
91
|
+
monkeypatch.setattr(cli, "select_option", select)
|
|
92
|
+
monkeypatch.setattr("builtins.input", lambda *_a, **_k: next(lines))
|
|
93
|
+
return shown
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
async def test_the_demo_approval_refuses_a_bare_respond_in_place(monkeypatch):
|
|
97
|
+
agent = _demo_agent()
|
|
98
|
+
shown = _menu(
|
|
99
|
+
monkeypatch, [1], ['{"type": "respond"}', '{"type": "respond", "message": "Ada"}']
|
|
100
|
+
)
|
|
101
|
+
buf = io.StringIO()
|
|
102
|
+
with redirect_stdout(buf):
|
|
103
|
+
_elapsed, had_error = await cli.run_single_turn_agui(agent, "ask me", "t-209", True)
|
|
104
|
+
out = buf.getvalue()
|
|
105
|
+
assert had_error is False, out
|
|
106
|
+
assert '{"type": "respond", "message": "reply"}' in out, out
|
|
107
|
+
assert 'respond needs a text "message". Nothing was sent; try again.' in out, out
|
|
108
|
+
assert "Error" not in out, out
|
|
109
|
+
assert "Ada" in out.split("try again.")[-1], out # the agent got the second answer
|
|
110
|
+
assert len(shown) == 1, out
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
async def test_a_decision_core_refuses_brings_the_approval_back(monkeypatch):
|
|
114
|
+
# Whatever slips past the CLI's check (an older or different rule in core), core
|
|
115
|
+
# refuses it before resuming. The approval is still pending, so ask it again.
|
|
116
|
+
agent = _demo_agent()
|
|
117
|
+
monkeypatch.setattr(cli, "_check_custom_decision", lambda v, n, i: ([v] * n, None))
|
|
118
|
+
shown = _menu(monkeypatch, [1, 0], ['{"type": "respond"}'])
|
|
119
|
+
buf = io.StringIO()
|
|
120
|
+
with redirect_stdout(buf):
|
|
121
|
+
_elapsed, had_error = await cli.run_single_turn_agui(agent, "ask me", "t-209b", True)
|
|
122
|
+
out = buf.getvalue()
|
|
123
|
+
assert had_error is False, out
|
|
124
|
+
assert "answer the approval again" in out, out
|
|
125
|
+
assert "✗ Error" not in out and "Re-run with -v" not in out, out
|
|
126
|
+
assert len(shown) == 2, out # the menu came back; "Approve all" then resumed
|
|
127
|
+
assert "Resumed" in out, out
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
# ---- On a real terminal --------------------------------------------------------------
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _type_on_pty(tmp_path, args, steps):
|
|
134
|
+
"""Run the CLI on a real pty, sending each ``(wait_for, send)`` once the CLI waits."""
|
|
135
|
+
import pty
|
|
136
|
+
import select
|
|
137
|
+
|
|
138
|
+
env = {k: v for k, v in os.environ.items() if k not in ("NO_COLOR", "TERM")}
|
|
139
|
+
env["LANGSTAGE_CLI_SESSIONS_DIR"] = str(tmp_path / "_sessions")
|
|
140
|
+
env["LANGSTAGE_CONFIG_HOME"] = str(tmp_path / "_home")
|
|
141
|
+
env["TERM"] = "dumb"
|
|
142
|
+
master, slave = pty.openpty()
|
|
143
|
+
proc = subprocess.Popen(
|
|
144
|
+
[sys.executable, "-c", "from langstage_cli.cli import main; main()", *args],
|
|
145
|
+
stdin=slave,
|
|
146
|
+
stdout=slave,
|
|
147
|
+
stderr=slave,
|
|
148
|
+
cwd=tmp_path,
|
|
149
|
+
env=env,
|
|
150
|
+
close_fds=True,
|
|
151
|
+
)
|
|
152
|
+
os.close(slave)
|
|
153
|
+
out, steps, seen_upto = b"", list(steps), 0
|
|
154
|
+
deadline = time.monotonic() + 90
|
|
155
|
+
try:
|
|
156
|
+
while time.monotonic() < deadline:
|
|
157
|
+
ready, _, _ = select.select([master], [], [], 0.5)
|
|
158
|
+
if not ready:
|
|
159
|
+
if proc.poll() is not None:
|
|
160
|
+
break
|
|
161
|
+
if steps and steps[0][0] in out[seen_upto:]:
|
|
162
|
+
wait_for, send = steps.pop(0)
|
|
163
|
+
seen_upto = out.index(wait_for, seen_upto) + len(wait_for)
|
|
164
|
+
os.write(master, send)
|
|
165
|
+
continue
|
|
166
|
+
try:
|
|
167
|
+
data = os.read(master, 4096)
|
|
168
|
+
except OSError: # EIO: the child closed the terminal
|
|
169
|
+
break
|
|
170
|
+
if not data:
|
|
171
|
+
break
|
|
172
|
+
out += data
|
|
173
|
+
proc.wait(timeout=10)
|
|
174
|
+
finally:
|
|
175
|
+
if proc.poll() is None:
|
|
176
|
+
proc.kill()
|
|
177
|
+
os.close(master)
|
|
178
|
+
return proc.returncode, out.decode("utf-8", errors="replace")
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@pytest.mark.skipif(sys.platform == "win32", reason="needs a POSIX pty")
|
|
182
|
+
def test_the_demo_approval_on_a_real_terminal(tmp_path):
|
|
183
|
+
pytest.importorskip("ag_ui_langgraph")
|
|
184
|
+
code, out = _type_on_pty(
|
|
185
|
+
tmp_path,
|
|
186
|
+
["-a", "langstage_core.demo.tools:graph", "ask me"],
|
|
187
|
+
[
|
|
188
|
+
(b"Choose 1-", b"2\n"),
|
|
189
|
+
(b"go back to the menu", b'{"type": "respond"}\n'),
|
|
190
|
+
(b"try again", b'{"type": "respond", "message": "Ada"}\n'),
|
|
191
|
+
],
|
|
192
|
+
)
|
|
193
|
+
assert '{"type": "respond", "message": "reply"}' in out, repr(out)
|
|
194
|
+
assert 'respond needs a text "message". Nothing was sent; try again.' in out, repr(out)
|
|
195
|
+
assert "Error" not in out and "Re-run with -v" not in out, repr(out)
|
|
196
|
+
assert "Resumed" in out, repr(out)
|
|
197
|
+
assert code == 0, out
|
|
@@ -234,7 +234,13 @@ def test_a_paused_session_answered_later_gets_text(tmp_path, monkeypatch):
|
|
|
234
234
|
|
|
235
235
|
|
|
236
236
|
def _type_on_pty(tmp_path, term, args, steps):
|
|
237
|
-
"""Run the CLI on a real pty
|
|
237
|
+
"""Run the CLI on a real pty and answer it: one ``(wait_for, send)`` step at a time.
|
|
238
|
+
|
|
239
|
+
A step is sent once its text has appeared and the terminal has then been quiet for
|
|
240
|
+
a moment, so the CLI is waiting for input. The arrow-key menu flushes pending
|
|
241
|
+
input when it switches the terminal to raw mode, so a key sent the instant the
|
|
242
|
+
menu is drawn can be dropped.
|
|
243
|
+
"""
|
|
238
244
|
import pty
|
|
239
245
|
import select
|
|
240
246
|
|
|
@@ -262,6 +268,10 @@ def _type_on_pty(tmp_path, term, args, steps):
|
|
|
262
268
|
if not ready:
|
|
263
269
|
if proc.poll() is not None:
|
|
264
270
|
break
|
|
271
|
+
if steps and steps[0][0] in out[seen_upto:]:
|
|
272
|
+
wait_for, send = steps.pop(0)
|
|
273
|
+
seen_upto = out.index(wait_for, seen_upto) + len(wait_for)
|
|
274
|
+
os.write(master, send)
|
|
265
275
|
continue
|
|
266
276
|
try:
|
|
267
277
|
data = os.read(master, 4096)
|
|
@@ -270,10 +280,6 @@ def _type_on_pty(tmp_path, term, args, steps):
|
|
|
270
280
|
if not data:
|
|
271
281
|
break
|
|
272
282
|
out += data
|
|
273
|
-
while steps and steps[0][0] in out[seen_upto:]:
|
|
274
|
-
wait_for, send = steps.pop(0)
|
|
275
|
-
seen_upto = out.index(wait_for, seen_upto) + len(wait_for)
|
|
276
|
-
os.write(master, send)
|
|
277
283
|
proc.wait(timeout=10)
|
|
278
284
|
finally:
|
|
279
285
|
if proc.poll() 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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|