feat(shell): add configurable key sequence callbacks for raw PTY mode#401
Draft
bassmanitram wants to merge 2 commits intostrands-agents:mainfrom
Draft
feat(shell): add configurable key sequence callbacks for raw PTY mode#401bassmanitram wants to merge 2 commits intostrands-agents:mainfrom
bassmanitram wants to merge 2 commits intostrands-agents:mainfrom
Conversation
Add KeySequenceMatcher class that detects configured key sequences during interactive PTY execution, with 50ms timeout to disambiguate between standalone keypresses (like Escape) and multi-character sequences (like Alt+C). Key features: - Pass key_sequence_callbacks via tool_context.invocation_state - Maps byte sequences (e.g., b'\\x03' for Ctrl+C) to callables - Longer sequences have priority over shorter ones - Optimized for minimal allocations in hot path This enables callers to receive callbacks for specific key sequences even when the terminal is in raw mode and normal keyboard handling is unavailable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds support for configurable key sequence detection during interactive PTY execution. When the shell tool runs in raw mode, callers can now register callbacks for specific key sequences (e.g., Ctrl+C, Alt+C) that will be invoked when those sequences are detected in stdin.
Motivation
When the shell tool executes commands interactively with PTY in raw mode, the terminal is owned by the subprocess. This means the calling application cannot intercept keypresses through normal means. This PR provides a mechanism for callers to register key sequence callbacks via
tool_context.invocation_state, enabling features like cancellation to work even during raw PTY execution.Implementation
New
KeySequenceMatcherclass:__slots__, pre-computed lookups,bytearraybuffer)API:
Changes
src/strands_tools/shell.py: AddedKeySequenceMatcherclass and integrated key sequence detection into PTY execution looptests/test_shell_key_sequences.py: 27 new tests covering matcher behavior, timeout handling, and integrationBackward Compatibility
Fully backward compatible. When no
key_sequence_callbacksare provided, behavior is identical to before.Documentation PR
Type of Change
Other (please describe):
Non-breaking enhancement
Testing
All 27 new tests pass
All 31 existing shell tests pass
Tested key scenarios:
I ran
hatch run prepareWell, no, 'cause there appears to be something wrong upstream - but I did run ruff locally.
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.