-
Notifications
You must be signed in to change notification settings - Fork 48.6k
fix(cli): recover detached background processes from checkpoint on CLI startup and shutdown #69918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mantenie
wants to merge
2
commits into
NousResearch:main
Choose a base branch
from
mantenie:fix/process-registry-recovery-current
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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,19 @@ | ||
| """CLI shutdown must flush a final process checkpoint so a crash that bypasses | ||
| atexit still leaves the most recent background-process state for the next | ||
| startup recovery.""" | ||
|
|
||
|
|
||
| def test_run_cleanup_writes_final_checkpoint(monkeypatch): | ||
| writes = [] | ||
| monkeypatch.setattr( | ||
| "tools.process_registry.process_registry.flush_checkpoint", | ||
| lambda: writes.append(1), | ||
| ) | ||
| # _run_cleanup touches many subsystems; stub the heavy ones so the test | ||
| # only asserts the checkpoint write happened. | ||
| monkeypatch.setattr("cli._cleanup_all_terminals", lambda *a, **k: None) | ||
| monkeypatch.setattr("cli._cleanup_all_browsers", lambda *a, **k: None) | ||
| monkeypatch.setattr("tools.async_delegation.interrupt_all", lambda reason="": 0, raising=False) | ||
| from cli import _run_cleanup | ||
| _run_cleanup(notify_session_finalize=False) | ||
| assert len(writes) == 1 |
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,38 @@ | ||
| """CLI startup must recover detached background processes from checkpoint. | ||
|
|
||
| Regression test for the second root cause of disappearing pollers: the CLI | ||
| never called recover_from_checkpoint(), so background processes spawned in a | ||
| previous CLI session (pollers, servers) became invisible to | ||
| process(action='list') while their OS processes kept running. | ||
| """ | ||
|
|
||
|
|
||
| def test_cli_run_conversation_recovers_background_processes(monkeypatch): | ||
| """The CLI conversation loop must call process_registry.recover_and_log() | ||
| once at startup, after the agent is built and before the first user turn.""" | ||
| calls = [] | ||
|
|
||
| def fake_recover_and_log(): | ||
| calls.append(1) | ||
| return 0 | ||
|
|
||
| monkeypatch.setattr( | ||
| "tools.process_registry.process_registry.recover_and_log", | ||
| fake_recover_and_log, | ||
| ) | ||
| from cli import _recover_background_processes_on_startup | ||
| _recover_background_processes_on_startup() | ||
| assert len(calls) == 1 | ||
|
|
||
|
|
||
| def test_cli_startup_recovery_swallows_errors(monkeypatch): | ||
| """A failure in recover_and_log must never raise into CLI startup.""" | ||
| def fake_recover_and_log(): | ||
| raise RuntimeError("checkpoint disk exploded") | ||
| monkeypatch.setattr( | ||
| "tools.process_registry.process_registry.recover_and_log", | ||
| fake_recover_and_log, | ||
| ) | ||
| from cli import _recover_background_processes_on_startup | ||
| # Must not raise. | ||
| _recover_background_processes_on_startup() | ||
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
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.
This invokes the helper directly, so it cannot catch a regression where
HermesCLI.run()stops calling it. Please add a controlled run-startup behavior test that asserts the wiring.