-
Notifications
You must be signed in to change notification settings - Fork 228
feat(plugins): fall back to user-level ~/.claude settings for Claude Code hook config #924
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
Merged
phernandez
merged 5 commits into
basicmachines-co:main
from
samuellawerentz:global-settings-fallback
Jul 15, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aa454ee
feat(plugins): fall back to user-level ~/.claude settings for hook co…
samuellawerentz da548f1
fix(plugins): address Codex review on user-level settings fallback
samuellawerentz 6c0b27f
refactor(plugins): simplify settings resolution helpers
samuellawerentz d28d731
docs(plugins): clarify hooks resolve nearest ancestor with a .claude …
samuellawerentz ed1de8d
test(plugins): cover Claude hook settings precedence
phernandez 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
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,201 @@ | ||
| import json | ||
| import os | ||
| import shutil | ||
| import subprocess | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| HOOK_RUNTIME_AVAILABLE = shutil.which("bash") is not None and shutil.which("python3") is not None | ||
| pytestmark = pytest.mark.skipif( | ||
| not HOOK_RUNTIME_AVAILABLE, | ||
| reason="Claude Code hook tests require bash and python3", | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class HookHarness: | ||
| repo_root: Path | ||
| home: Path | ||
| bin_dir: Path | ||
| command_log: Path | ||
|
|
||
| def write_settings(self, directory: Path, name: str, basic_memory: dict[str, object]) -> None: | ||
| settings_dir = directory / ".claude" | ||
| settings_dir.mkdir(parents=True, exist_ok=True) | ||
| (settings_dir / name).write_text( | ||
| json.dumps({"basicMemory": basic_memory}), | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| def run_hook(self, hook_name: str, payload: dict[str, str]) -> subprocess.CompletedProcess[str]: | ||
| env = os.environ.copy() | ||
| env.update( | ||
| { | ||
| "BM_TEST_COMMAND_LOG": str(self.command_log), | ||
| "HOME": str(self.home), | ||
| "PATH": f"{self.bin_dir}{os.pathsep}{env['PATH']}", | ||
| } | ||
| ) | ||
| return subprocess.run( | ||
| ["bash", str(self.repo_root / "plugins/claude-code/hooks" / hook_name)], | ||
| input=json.dumps(payload), | ||
| capture_output=True, | ||
| check=False, | ||
| env=env, | ||
| text=True, | ||
| ) | ||
|
|
||
| def logged_commands(self) -> list[list[str]]: | ||
| if not self.command_log.exists(): | ||
| return [] | ||
| return [json.loads(line) for line in self.command_log.read_text().splitlines()] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def hook_harness(tmp_path: Path) -> HookHarness: | ||
| repo_root = Path(__file__).resolve().parents[1] | ||
| home = tmp_path / "home" | ||
| bin_dir = tmp_path / "bin" | ||
| home.mkdir() | ||
| bin_dir.mkdir() | ||
| command_log = tmp_path / "basic-memory-commands.jsonl" | ||
|
|
||
| fake_basic_memory = bin_dir / "basic-memory" | ||
| fake_basic_memory.write_text( | ||
| """#!/usr/bin/env python3 | ||
| import json | ||
| import os | ||
| import sys | ||
|
|
||
| with open(os.environ["BM_TEST_COMMAND_LOG"], "a", encoding="utf-8") as command_log: | ||
| command_log.write(json.dumps(sys.argv[1:]) + "\\n") | ||
|
|
||
| if sys.argv[1:3] == ["tool", "search-notes"]: | ||
| print(json.dumps({"results": []})) | ||
| """, | ||
| encoding="utf-8", | ||
| ) | ||
| fake_basic_memory.chmod(0o755) | ||
|
|
||
| return HookHarness( | ||
| repo_root=repo_root, | ||
| home=home, | ||
| bin_dir=bin_dir, | ||
| command_log=command_log, | ||
| ) | ||
|
|
||
|
|
||
| def test_session_start_uses_user_settings_without_project_config( | ||
| hook_harness: HookHarness, | ||
| ) -> None: | ||
| hook_harness.write_settings( | ||
| hook_harness.home, | ||
| "settings.json", | ||
| {"primaryProject": "global-project", "captureFolder": "global-sessions"}, | ||
| ) | ||
| # Claude Code does not treat this as a user-level settings source. A stale | ||
| # file must not silently reroute hooks away from the visible global config. | ||
| hook_harness.write_settings( | ||
| hook_harness.home, | ||
| "settings.local.json", | ||
| {"primaryProject": "stale-project"}, | ||
| ) | ||
| cwd = hook_harness.home / "work/repo/src" | ||
| cwd.mkdir(parents=True) | ||
|
|
||
| result = hook_harness.run_hook("session-start.sh", {"cwd": str(cwd)}) | ||
|
|
||
| assert result.returncode == 0, result.stderr | ||
| assert "**Project:** global-project" in result.stdout | ||
| assert "`global-sessions/`" in result.stdout | ||
| assert "stale-project" not in result.stdout | ||
|
|
||
|
|
||
| def test_session_start_merges_nearest_ancestor_project_settings_over_user_settings( | ||
| hook_harness: HookHarness, | ||
| ) -> None: | ||
| hook_harness.write_settings( | ||
| hook_harness.home, | ||
| "settings.json", | ||
| {"primaryProject": "global-project", "captureFolder": "global-sessions"}, | ||
| ) | ||
| project_root = hook_harness.home / "work/repo" | ||
| hook_harness.write_settings( | ||
| project_root, | ||
| "settings.json", | ||
| {"primaryProject": "project-override"}, | ||
| ) | ||
| hook_harness.write_settings( | ||
| project_root, | ||
| "settings.local.json", | ||
| {"captureFolder": "local-sessions"}, | ||
| ) | ||
| cwd = project_root / "packages/client/src" | ||
| cwd.mkdir(parents=True) | ||
|
|
||
| result = hook_harness.run_hook("session-start.sh", {"cwd": str(cwd)}) | ||
|
|
||
| assert result.returncode == 0, result.stderr | ||
| assert "**Project:** project-override" in result.stdout | ||
| assert "`local-sessions/`" in result.stdout | ||
| search_commands = [ | ||
| command | ||
| for command in hook_harness.logged_commands() | ||
| if command[:2] == ["tool", "search-notes"] | ||
| ] | ||
| assert len(search_commands) == 3 | ||
| assert all( | ||
| command[command.index("--project") + 1] == "project-override" for command in search_commands | ||
| ) | ||
|
|
||
|
|
||
| def test_pre_compact_uses_merged_project_and_capture_folder( | ||
| hook_harness: HookHarness, | ||
| tmp_path: Path, | ||
| ) -> None: | ||
| hook_harness.write_settings( | ||
| hook_harness.home, | ||
| "settings.json", | ||
| {"primaryProject": "global-project", "captureFolder": "global-sessions"}, | ||
| ) | ||
| project_root = hook_harness.home / "work/repo" | ||
| hook_harness.write_settings( | ||
| project_root, | ||
| "settings.json", | ||
| {"primaryProject": "project-override"}, | ||
| ) | ||
| hook_harness.write_settings( | ||
| project_root, | ||
| "settings.local.json", | ||
| {"captureFolder": "local-sessions"}, | ||
| ) | ||
| cwd = project_root / "src" | ||
| cwd.mkdir(parents=True) | ||
| transcript = tmp_path / "transcript.jsonl" | ||
| transcript.write_text( | ||
| json.dumps({"message": {"role": "user", "content": "Ship the settings fallback"}}) + "\n", | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| result = hook_harness.run_hook( | ||
| "pre-compact.sh", | ||
| { | ||
| "cwd": str(cwd), | ||
| "session_id": "session-123", | ||
| "transcript_path": str(transcript), | ||
| }, | ||
| ) | ||
|
|
||
| assert result.returncode == 0, result.stderr | ||
| write_commands = [ | ||
| command | ||
| for command in hook_harness.logged_commands() | ||
| if command[:2] == ["tool", "write-note"] | ||
| ] | ||
| assert len(write_commands) == 1 | ||
| write_command = write_commands[0] | ||
| assert write_command[write_command.index("--project") + 1] == "project-override" | ||
| assert write_command[write_command.index("--folder") + 1] == "local-sessions" |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.