From 89bea998df0d5e5438bc59dbae6bdb546f59fcc2 Mon Sep 17 00:00:00 2001 From: kartsan03 Date: Fri, 13 Nov 2026 14:14:53 +0000 Subject: [PATCH 1/3] fix(integrations): keep shared scripts executable when the default integration changes `integration use`, every `integration switch` path, and `integration uninstall` of the default integration refresh shared infrastructure through `_set_default_integration()`. The refresh rewrites `.specify/scripts/bash/*.sh` with the packaged mode (0644) and nothing restored the execute bits, so the scripts that generated commands and skills run directly failed with "Permission denied". Restore them after the refresh, as init, install and upgrade already do. Assisted-by: Codex (model: gpt-6.1-sol, autonomous) Assisted-by: Claude Code (model: claude-opus-5-5, autonomous) --- src/specify_cli/integrations/_helpers.py | 5 +++++ .../integrations/test_command_switch.py | 22 +++++++++++++++++++ .../integrations/test_command_uninstall.py | 17 ++++++++++++++ .../integrations/test_command_use.py | 17 ++++++++++++++ 4 files changed, 61 insertions(+) diff --git a/src/specify_cli/integrations/_helpers.py b/src/specify_cli/integrations/_helpers.py index 4a7bc6b346..ca7db4cbf8 100644 --- a/src/specify_cli/integrations/_helpers.py +++ b/src/specify_cli/integrations/_helpers.py @@ -369,6 +369,11 @@ def _set_default_integration( raise _SharedTemplateRefreshError( f"Failed to refresh shared infrastructure for '{key}': {exc}" ) from exc + # The refresh rewrites shared scripts with the packaged (non-executable) + # mode, so restore execute bits as init, install and upgrade do. + if os.name != "nt": + from .. import ensure_executable_scripts + ensure_executable_scripts(project_root) _write_integration_json(project_root, key, installed_keys, settings) _update_init_options_for_integration( diff --git a/tests/specify_cli/integrations/test_command_switch.py b/tests/specify_cli/integrations/test_command_switch.py index 0c928a44a1..c13bb29c2e 100644 --- a/tests/specify_cli/integrations/test_command_switch.py +++ b/tests/specify_cli/integrations/test_command_switch.py @@ -60,6 +60,28 @@ def _project_file_state(project): class TestIntegrationSwitch: + @pytest.mark.skipif( + os.name == "nt", reason="POSIX execute bits are not meaningful on Windows" + ) + @pytest.mark.parametrize( + ("target_state", "flags"), + [("new", []), ("installed", []), ("default", ["--force"]), ("default", ["--refresh-shared-infra"])], + ) + def test_switch_keeps_shared_scripts_executable(self, tmp_path, target_state, flags): + """Every switch path refreshes shared scripts; they must stay runnable.""" + if target_state == "default": + project = _init_project(tmp_path, "codex") + else: + project = _init_codex_switch_project(tmp_path, target_state) + scripts = sorted((project / ".specify" / "scripts" / "bash").glob("*.sh")) + assert scripts and all(os.access(script, os.X_OK) for script in scripts) + + result = _run_in_project(project, ["integration", "switch", "codex", *flags]) + + assert result.exit_code == 0, result.output + not_executable = [script.name for script in scripts if not os.access(script, os.X_OK)] + assert not not_executable, f"switch left shared scripts non-executable: {not_executable}" + @pytest.mark.parametrize("target_state", ["new", "installed", "default"]) @pytest.mark.parametrize( "flags", [[], ["--force"], ["--refresh-shared-infra"], ["--force", "--refresh-shared-infra"]], diff --git a/tests/specify_cli/integrations/test_command_uninstall.py b/tests/specify_cli/integrations/test_command_uninstall.py index 02994cb259..492e99bc96 100644 --- a/tests/specify_cli/integrations/test_command_uninstall.py +++ b/tests/specify_cli/integrations/test_command_uninstall.py @@ -48,6 +48,23 @@ def _registered_agents(project, kind, package_id): return set(registry[kind][package_id]["registered_commands"]) class TestIntegrationUninstall: + @pytest.mark.skipif( + os.name == "nt", reason="POSIX execute bits are not meaningful on Windows" + ) + def test_uninstall_default_keeps_fallback_scripts_executable(self, tmp_path): + """Falling back to another installed integration must keep shared scripts runnable.""" + project = _init_project(tmp_path, "claude") + result = _run_in_project(project, ["integration", "install", "codex", "--script", "sh"]) + assert result.exit_code == 0, result.output + scripts = sorted((project / ".specify" / "scripts" / "bash").glob("*.sh")) + assert scripts and all(os.access(script, os.X_OK) for script in scripts) + + result = _run_in_project(project, ["integration", "uninstall", "claude"]) + + assert result.exit_code == 0, result.output + not_executable = [script.name for script in scripts if not os.access(script, os.X_OK)] + assert not not_executable, f"uninstall left shared scripts non-executable: {not_executable}" + @pytest.mark.parametrize("command", ["uninstall", "switch"]) def test_unreadable_dispatcher_preserves_other_cleanup(self, tmp_path, monkeypatch, caplog, command): from specify_cli.events import install_integration_events diff --git a/tests/specify_cli/integrations/test_command_use.py b/tests/specify_cli/integrations/test_command_use.py index ad47da7d44..8019926616 100644 --- a/tests/specify_cli/integrations/test_command_use.py +++ b/tests/specify_cli/integrations/test_command_use.py @@ -23,6 +23,23 @@ ) class TestIntegrationUse: + @pytest.mark.skipif( + os.name == "nt", reason="POSIX execute bits are not meaningful on Windows" + ) + def test_use_keeps_shared_scripts_executable(self, tmp_path): + """Making an installed integration the default must keep shared scripts runnable.""" + project = _init_project(tmp_path, "claude") + result = _run_in_project(project, ["integration", "install", "codex", "--script", "sh"]) + assert result.exit_code == 0, result.output + scripts = sorted((project / ".specify" / "scripts" / "bash").glob("*.sh")) + assert scripts and all(os.access(script, os.X_OK) for script in scripts) + + result = _run_in_project(project, ["integration", "use", "codex"]) + + assert result.exit_code == 0, result.output + not_executable = [script.name for script in scripts if not os.access(script, os.X_OK)] + assert not not_executable, f"use left shared scripts non-executable: {not_executable}" + @pytest.mark.parametrize("edited_command", [None, "plan", "tasks"]) def test_use_syncs_manifest_without_blessing_user_edits( self, tmp_path, tasks_preset_src, edited_command From 13ff151dc2c9f0141e75ebee46cf2853afd3025d Mon Sep 17 00:00:00 2001 From: kartsan03 Date: Mon, 16 Nov 2026 21:06:18 +0000 Subject: [PATCH 2/3] fix(integrations): set shared script execute bits in the refresh's own write The previous commit restored execute bits with `ensure_executable_scripts()` after the default-integration refresh. That scan follows a symlinked `.specify/scripts` or `.specify/extensions` root and changes modes outside the lifecycle journal, so a rollback could not undo it. Give `.sh` files that start with a shebang their execute bits in the mode `install_shared_infra()` already passes to `_write_shared_bytes()`. The mode is applied to the temp file before the atomic replace, behind the same symlink checks and journal calls as the content, and only files the refresh writes are touched. Drop the scan from `_set_default_integration()`. Assisted-by: Claude Code (model: claude-fable-5-2, autonomous) --- src/specify_cli/integrations/_helpers.py | 5 --- src/specify_cli/shared_infra.py | 22 +++++++++---- .../integrations/test_command_use.py | 31 +++++++++++++++++++ 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/specify_cli/integrations/_helpers.py b/src/specify_cli/integrations/_helpers.py index ca7db4cbf8..4a7bc6b346 100644 --- a/src/specify_cli/integrations/_helpers.py +++ b/src/specify_cli/integrations/_helpers.py @@ -369,11 +369,6 @@ def _set_default_integration( raise _SharedTemplateRefreshError( f"Failed to refresh shared infrastructure for '{key}': {exc}" ) from exc - # The refresh rewrites shared scripts with the packaged (non-executable) - # mode, so restore execute bits as init, install and upgrade do. - if os.name != "nt": - from .. import ensure_executable_scripts - ensure_executable_scripts(project_root) _write_integration_json(project_root, key, installed_keys, settings) _update_init_options_for_integration( diff --git a/src/specify_cli/shared_infra.py b/src/specify_cli/shared_infra.py index 44960706a1..fc989f9abe 100644 --- a/src/specify_cli/shared_infra.py +++ b/src/specify_cli/shared_infra.py @@ -317,6 +317,20 @@ def _ensure_safe_shared_destination( raise ValueError(f"Shared infrastructure destination escapes project root: {label}") from None +def _shared_script_mode(src_path: Path, content: bytes) -> int: + """Return the mode a shared script is written with. + + Packaged scripts are 0644. A POSIX shell script that starts with a shebang + gets an execute bit for each class that can read it (always for the owner), + the rule ``ensure_executable_scripts`` applies, so a refresh that rewrites + it leaves it runnable without a separate chmod pass. + """ + mode = src_path.stat().st_mode & 0o777 + if os.name != "nt" and src_path.suffix == ".sh" and content.startswith(b"#!"): + mode |= ((mode & 0o444) >> 2) | 0o100 + return mode + + def _write_shared_text(project_path: Path, dest: Path, content: str) -> None: _write_shared_bytes(project_path, dest, content.encode("utf-8")) @@ -636,13 +650,9 @@ def _ensure_or_bucket_dir(directory: Path) -> bool: content = _resolve_dynamic_command_refs( content, invoke_separator, invoke_prefix ) + data = content.encode("utf-8") planned_copies.append( - ( - dst_path, - rel, - content.encode("utf-8"), - src_path.stat().st_mode & 0o777, - ) + (dst_path, rel, data, _shared_script_mode(src_path, data)) ) templates_src = shared_templates_source(core_pack=core_pack, repo_root=repo_root) diff --git a/tests/specify_cli/integrations/test_command_use.py b/tests/specify_cli/integrations/test_command_use.py index 8019926616..ea85864799 100644 --- a/tests/specify_cli/integrations/test_command_use.py +++ b/tests/specify_cli/integrations/test_command_use.py @@ -40,6 +40,37 @@ def test_use_keeps_shared_scripts_executable(self, tmp_path): not_executable = [script.name for script in scripts if not os.access(script, os.X_OK)] assert not not_executable, f"use left shared scripts non-executable: {not_executable}" + @pytest.mark.skipif( + os.name == "nt", reason="POSIX execute bits are not meaningful on Windows" + ) + @pytest.mark.parametrize("linked_scripts_root", [False, True]) + def test_use_changes_modes_only_on_scripts_it_writes(self, tmp_path, linked_scripts_root): + """Execute bits come from the refresh's own writes, never from a scan. + + A user's script next to the shared ones keeps its mode, and nothing + behind a symlinked ``.specify/scripts`` (which the refresh skips) is touched. + """ + project = _init_project(tmp_path, "claude") + result = _run_in_project(project, ["integration", "install", "codex", "--script", "sh"]) + assert result.exit_code == 0, result.output + scripts_root = project / ".specify" / "scripts" + if linked_scripts_root: + outside = tmp_path / "outside-scripts" + scripts_root.rename(outside) + scripts_root.symlink_to(outside, target_is_directory=True) + user_script = scripts_root.resolve() / "bash" / "local-helper.sh" + user_script.write_bytes(b"#!/usr/bin/env bash\necho local\n") + user_script.chmod(0o644) + + result = _run_in_project(project, ["integration", "use", "codex"]) + + assert result.exit_code == 0, result.output + assert user_script.stat().st_mode & 0o777 == 0o644 + if not linked_scripts_root: + shared = sorted((scripts_root / "bash").glob("*.sh")) + not_executable = [s.name for s in shared if s != user_script and not os.access(s, os.X_OK)] + assert not not_executable, f"use left shared scripts non-executable: {not_executable}" + @pytest.mark.parametrize("edited_command", [None, "plan", "tasks"]) def test_use_syncs_manifest_without_blessing_user_edits( self, tmp_path, tasks_preset_src, edited_command From 3f7bdc03ab719840a3b2c456151a39f998989462 Mon Sep 17 00:00:00 2001 From: kartsan03 Date: Tue, 17 Nov 2026 01:28:47 +0000 Subject: [PATCH 3/3] test(integrations): cover the shared script mode rule and its rollback Unit tests for `_shared_script_mode()`: an execute bit for each class that can read a `.sh` file with a shebang, and the packaged mode for a shebang-less `.sh`, other file types and Windows. A failed switch away from an external adapter restores the mode and content of the scripts the refresh rewrote. Assisted-by: Claude Code (model: claude-fable-5-2, autonomous) --- .../integrations/test_installed_adapters.py | 26 ++++++++++ tests/test_shared_infra_script_mode.py | 52 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 tests/test_shared_infra_script_mode.py diff --git a/tests/specify_cli/integrations/test_installed_adapters.py b/tests/specify_cli/integrations/test_installed_adapters.py index 1b097d7eb2..a0130bee8d 100644 --- a/tests/specify_cli/integrations/test_installed_adapters.py +++ b/tests/specify_cli/integrations/test_installed_adapters.py @@ -1006,6 +1006,32 @@ def fail_commit(*args, **kwargs): assert snapshot(project) == before +@pytest.mark.skipif(os.name == "nt", reason="POSIX execute bits are not meaningful on Windows") +def test_failed_switch_restores_shared_script_mode_and_content(tmp_path, server, monkeypatch): + """A refresh that makes a managed script executable is undone with the rest of the switch.""" + from specify_cli.integrations import installer + + publish(server) + project = catalog_project(tmp_path, server) + install(project) + scripts = sorted((project / ".specify/scripts/bash").glob("*.sh")) + assert scripts + for script in scripts: + script.chmod(0o644) + before = snapshot(project) + + def fail_commit(*args, **kwargs): + assert all(stat.S_IMODE(script.stat().st_mode) == 0o755 for script in scripts) + raise OSError("sample package commit failure") + + monkeypatch.setattr(installer, "write_records", fail_commit) + result = run(project, ["integration", "switch", "copilot", "--script", "sh"]) + assert result.exit_code == 1, result.output + assert "sample package commit failure" in result.output + assert snapshot(project) == before + assert all(stat.S_IMODE(script.stat().st_mode) == 0o644 for script in scripts) + + @pytest.mark.parametrize("install_allowed", [False, True]) def test_round4_search_advertises_only_allowed_external_installation(tmp_path, server, install_allowed): publish(server, code='raise RuntimeError("discovery must not import sample code")') diff --git a/tests/test_shared_infra_script_mode.py b/tests/test_shared_infra_script_mode.py new file mode 100644 index 0000000000..c66f440fa1 --- /dev/null +++ b/tests/test_shared_infra_script_mode.py @@ -0,0 +1,52 @@ +"""Unit tests for the mode shared scripts are written with.""" + +from __future__ import annotations + +import os + +import pytest + +from specify_cli import shared_infra +from specify_cli.shared_infra import _shared_script_mode + +SHEBANG = b"#!/usr/bin/env bash\necho hi\n" + + +def _source(tmp_path, name, mode): + path = tmp_path / name + path.write_bytes(SHEBANG) + path.chmod(mode) + return path + + +@pytest.mark.skipif(os.name == "nt", reason="POSIX execute bits are not meaningful on Windows") +@pytest.mark.parametrize( + ("source_mode", "expected"), + [(0o644, 0o755), (0o640, 0o750), (0o600, 0o700), (0o755, 0o755), (0o200, 0o300)], +) +def test_shell_script_gets_execute_bit_for_each_class_that_reads(tmp_path, source_mode, expected): + source = _source(tmp_path, "setup-plan.sh", source_mode) + assert _shared_script_mode(source, SHEBANG) == expected + + +@pytest.mark.skipif(os.name == "nt", reason="POSIX execute bits are not meaningful on Windows") +@pytest.mark.parametrize( + ("name", "content"), + [ + ("common.sh", b"echo sourced, no shebang\n"), + ("common.sh", b""), + ("setup-plan.ps1", SHEBANG), + ("setup_plan.py", SHEBANG), + ("sh", SHEBANG), + ], +) +def test_other_files_keep_the_packaged_mode(tmp_path, name, content): + source = _source(tmp_path, name, 0o644) + assert _shared_script_mode(source, content) == 0o644 + + +def test_windows_keeps_the_packaged_mode(tmp_path, monkeypatch): + source = _source(tmp_path, "setup-plan.sh", 0o644) + packaged = source.stat().st_mode & 0o777 + monkeypatch.setattr(shared_infra.os, "name", "nt") + assert _shared_script_mode(source, SHEBANG) == packaged