This repository was archived by the owner on Sep 23, 2026. It is now read-only.
Repository navigation
fix(shell): adapt timeouts for long commands #2200
Closed
he-yufeng
wants to merge
1
commit into
MoonshotAI:main
from
he-yufeng:fix/shell-command-timeout-hints
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(shell): adapt timeouts for long commands
- Loading branch information
commit bbadafd975d6075acea60e0a7295adc6df283841
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from types import SimpleNamespace | ||
|
|
||
| from kimi_cli.tools.shell import DEFAULT_TIMEOUT, Params, Shell, _effective_timeout | ||
|
|
||
|
|
||
| def test_effective_timeout_keeps_normal_commands_at_default() -> None: | ||
| assert _effective_timeout("echo ok", DEFAULT_TIMEOUT, max_timeout=300) == DEFAULT_TIMEOUT | ||
|
|
||
|
|
||
| def test_effective_timeout_extends_known_long_commands() -> None: | ||
| assert _effective_timeout("git submodule deinit -f deps/hal", 60, max_timeout=300) == 300 | ||
| assert _effective_timeout("git show HEAD -- vendor/sdk", 60, max_timeout=300) == 120 | ||
| assert _effective_timeout("npm run build", 60, max_timeout=300) == 180 | ||
|
|
||
|
|
||
| def test_effective_timeout_keeps_larger_explicit_timeout() -> None: | ||
| assert _effective_timeout("npm install", 240, max_timeout=300) == 240 | ||
|
|
||
|
|
||
| async def test_shell_uses_adaptive_timeout(shell_tool: Shell, monkeypatch) -> None: | ||
| seen: dict[str, int] = {} | ||
|
|
||
| async def fake_run_shell_command(command, stdout_cb, stderr_cb, timeout): | ||
| seen["timeout"] = timeout | ||
| return 0 | ||
|
|
||
| monkeypatch.setattr(shell_tool, "_run_shell_command", fake_run_shell_command) | ||
|
|
||
| result = await shell_tool(Params(command="git submodule deinit -f deps/hal")) | ||
|
|
||
| assert not result.is_error | ||
| assert seen["timeout"] == 300 | ||
|
|
||
|
|
||
| async def test_background_shell_uses_adaptive_timeout(shell_tool: Shell, monkeypatch) -> None: | ||
| seen: dict[str, int] = {} | ||
|
|
||
| def fake_create_bash_task(**kwargs): | ||
| seen["timeout"] = kwargs["timeout_s"] | ||
| return SimpleNamespace( | ||
| spec=SimpleNamespace( | ||
| id="bash-test", | ||
| kind="shell", | ||
| kind_payload=None, | ||
| description=kwargs["description"], | ||
| command=kwargs["command"], | ||
| ), | ||
| runtime=SimpleNamespace(status="running", exit_code=None, failure_reason=None), | ||
| ) | ||
|
|
||
| monkeypatch.setattr( | ||
| shell_tool._runtime.background_tasks, "create_bash_task", fake_create_bash_task | ||
| ) | ||
|
|
||
| result = await shell_tool( | ||
| Params( | ||
| command="npm install", | ||
| run_in_background=True, | ||
| description="install dependencies", | ||
| ) | ||
| ) | ||
|
|
||
| assert not result.is_error | ||
| assert seen["timeout"] == 180 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not unconditionally raise the timeout for matched commands:
_effective_timeoutcurrently returnsmax(timeout, suggested_timeout), soShell(timeout=5)on commands likenpm installwill actually run for 180s. This changes the semantics of thetimeoutparameter from an explicit limit to a minimum and can cause callers that rely on fast-fail behavior to block for minutes unexpectedly; consider only applying adaptive extension when the caller kept the default timeout.Useful? React with 👍 / 👎.