-
Notifications
You must be signed in to change notification settings - Fork 52k
fix(cli): honor safe-mode isolation in one-shot #73005
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
AlambritoDito
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
AlambritoDito:fix/oneshot-safe-mode-isolation
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.
+216
−5
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
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,194 @@ | ||
| """Regression tests for ``hermes -z`` isolation env gates.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
| import types | ||
|
|
||
| from hermes_cli import config as config_mod | ||
| from hermes_cli import oneshot | ||
|
|
||
|
|
||
| SENTINEL_MODEL = "sentinel-user-config-model-73191" | ||
| SENTINEL_SECRET = "sentinel-user-config-secret-73191" | ||
|
|
||
|
|
||
| class CapturingAgent: | ||
| calls: list[dict] = [] | ||
|
|
||
| def __init__(self, **kwargs): | ||
| self.kwargs = kwargs | ||
| self.__class__.calls.append(kwargs) | ||
|
|
||
| def run_conversation(self, prompt): | ||
| return { | ||
| "final_response": f"model={self.kwargs.get('model')}", | ||
| "completed": True, | ||
| "failed": False, | ||
| "model": self.kwargs.get("model"), | ||
| "provider": self.kwargs.get("provider"), | ||
| } | ||
|
|
||
| def shutdown_memory_provider(self, *args, **kwargs): | ||
| return None | ||
|
|
||
| def close(self): | ||
| return None | ||
|
|
||
|
|
||
| def _write_user_config(home): | ||
| home.mkdir(parents=True, exist_ok=True) | ||
| (home / "config.yaml").write_text( | ||
| "\n".join([ | ||
| "model:", | ||
| f" default: {SENTINEL_MODEL}", | ||
| " provider: openrouter", | ||
| "toolsets:", | ||
| " cli:", | ||
| " - shell", | ||
| "agent:", | ||
| f" system_prompt: {SENTINEL_SECRET}", | ||
| "fallback:", | ||
| " enabled: true", | ||
| " chain:", | ||
| " - provider: openrouter", | ||
| f" model: {SENTINEL_MODEL}-fallback", | ||
| "", | ||
| ]), | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
|
|
||
| def _install_fakes(monkeypatch): | ||
| CapturingAgent.calls.clear() | ||
| fake_run_agent = types.ModuleType("run_agent") | ||
| fake_run_agent.AIAgent = CapturingAgent | ||
| monkeypatch.setitem(sys.modules, "run_agent", fake_run_agent) | ||
|
|
||
| monkeypatch.setattr(oneshot, "_create_session_db_for_oneshot", lambda: None) | ||
|
|
||
| import hermes_cli.runtime_provider as runtime_provider | ||
| import hermes_cli.tools_config as tools_config | ||
|
|
||
| monkeypatch.setattr( | ||
| runtime_provider, | ||
| "resolve_runtime_provider", | ||
| lambda **kwargs: { | ||
| "api_key": "fake-key", | ||
| "base_url": "https://example.invalid/v1", | ||
| "provider": kwargs.get("requested") or "openrouter", | ||
| "requested_provider": kwargs.get("requested") or "openrouter", | ||
| "api_mode": "chat_completions", | ||
| "credential_pool": None, | ||
| }, | ||
| ) | ||
| monkeypatch.setattr( | ||
| tools_config, "_get_platform_tools", lambda cfg, platform: {"files"} | ||
| ) | ||
|
|
||
|
|
||
| def _clear_config_caches(): | ||
| config_mod._LOAD_CONFIG_CACHE.clear() | ||
| config_mod._RAW_CONFIG_CACHE.clear() | ||
| config_mod._LAST_EXPANDED_CONFIG_BY_PATH.clear() | ||
|
|
||
|
|
||
| def test_oneshot_ignore_rules_env_passes_skip_flags(monkeypatch, tmp_path): | ||
| _install_fakes(monkeypatch) | ||
| monkeypatch.setenv("HERMES_HOME", str(tmp_path)) | ||
| monkeypatch.setenv("HERMES_IGNORE_RULES", "1") | ||
| _clear_config_caches() | ||
|
|
||
| oneshot._run_agent( | ||
| "hello", | ||
| model="explicit/model", | ||
| provider="openrouter", | ||
| toolsets=["files"], | ||
| use_config_toolsets=False, | ||
| ) | ||
|
|
||
| kwargs = CapturingAgent.calls[-1] | ||
| assert kwargs["skip_context_files"] is True | ||
| assert kwargs["skip_memory"] is True | ||
|
|
||
|
|
||
| def test_oneshot_without_ignore_rules_passes_false_skip_flags(monkeypatch, tmp_path): | ||
| _install_fakes(monkeypatch) | ||
| monkeypatch.setenv("HERMES_HOME", str(tmp_path)) | ||
| monkeypatch.delenv("HERMES_IGNORE_RULES", raising=False) | ||
| _clear_config_caches() | ||
|
|
||
| oneshot._run_agent( | ||
| "hello", | ||
| model="explicit/model", | ||
| provider="openrouter", | ||
| toolsets=["files"], | ||
| use_config_toolsets=False, | ||
| ) | ||
|
|
||
| kwargs = CapturingAgent.calls[-1] | ||
| assert kwargs["skip_context_files"] is False | ||
| assert kwargs["skip_memory"] is False | ||
|
|
||
|
|
||
| def test_oneshot_ignore_user_config_skips_cached_user_config(monkeypatch, tmp_path): | ||
| _install_fakes(monkeypatch) | ||
| _write_user_config(tmp_path) | ||
| monkeypatch.setenv("HERMES_HOME", str(tmp_path)) | ||
| monkeypatch.delenv("HERMES_IGNORE_USER_CONFIG", raising=False) | ||
| _clear_config_caches() | ||
|
|
||
| assert config_mod.load_config()["model"]["default"] == SENTINEL_MODEL | ||
|
|
||
| monkeypatch.setenv("HERMES_IGNORE_USER_CONFIG", "1") | ||
| response, _result = oneshot._run_agent("hello") | ||
|
|
||
| kwargs = CapturingAgent.calls[-1] | ||
| assert kwargs["model"] != SENTINEL_MODEL | ||
| assert SENTINEL_MODEL not in response | ||
| assert all(SENTINEL_MODEL not in repr(call) for call in CapturingAgent.calls) | ||
|
|
||
|
|
||
| def test_oneshot_explicit_runtime_options_survive_isolation(monkeypatch, tmp_path): | ||
| _install_fakes(monkeypatch) | ||
| _write_user_config(tmp_path) | ||
| monkeypatch.setenv("HERMES_HOME", str(tmp_path)) | ||
| monkeypatch.setenv("HERMES_IGNORE_USER_CONFIG", "1") | ||
| monkeypatch.setenv("HERMES_IGNORE_RULES", "1") | ||
| _clear_config_caches() | ||
|
|
||
| oneshot._run_agent( | ||
| "hello", | ||
| model="explicit/model", | ||
| provider="openrouter", | ||
| toolsets="files,shell", | ||
| use_config_toolsets=False, | ||
| ) | ||
|
|
||
| kwargs = CapturingAgent.calls[-1] | ||
| assert kwargs["model"] == "explicit/model" | ||
| assert kwargs["provider"] == "openrouter" | ||
| assert kwargs["enabled_toolsets"] == ["files", "shell"] | ||
| assert kwargs["skip_context_files"] is True | ||
| assert kwargs["skip_memory"] is True | ||
|
|
||
|
|
||
| def test_oneshot_isolation_does_not_leak_user_config_sentinel( | ||
| monkeypatch, | ||
| tmp_path, | ||
| capsys, | ||
| ): | ||
| _install_fakes(monkeypatch) | ||
| _write_user_config(tmp_path) | ||
| monkeypatch.setenv("HERMES_HOME", str(tmp_path)) | ||
| monkeypatch.setenv("HERMES_IGNORE_USER_CONFIG", "1") | ||
| _clear_config_caches() | ||
|
|
||
| rc = oneshot.run_oneshot("hello") | ||
|
|
||
| captured = capsys.readouterr() | ||
| assert rc == 0 | ||
| assert SENTINEL_MODEL not in captured.out | ||
| assert SENTINEL_MODEL not in captured.err | ||
| assert SENTINEL_SECRET not in captured.out | ||
| assert SENTINEL_SECRET not in captured.err |
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 consumes the environment flag, but top-level
-z --ignore-rulesnever sets it: one-shot bypassescmd_chat(), where explicit ignore flags are currently exported. Please wire those arguments before one-shot startup and add a top-level dispatch regression.