Skip to content

feat(events): add portable hook interpreter fallback and pre-flight checks - #5721

Open
dlwlgnsrhy wants to merge 2 commits into
github:mainfrom
dlwlgnsrhy:feat/portable-hook-interpreter
Open

dlwlgnsrhy wants to merge 2 commits into
github:mainfrom
dlwlgnsrhy:feat/portable-hook-interpreter

Conversation

@dlwlgnsrhy

Copy link
Copy Markdown
Contributor

Summary

Addresses #5619 by implementing portable interpreter fallback for generated event hooks, unified interpreter availability checks (PATH, project-relative .venv, and absolute paths), install/upgrade/refresh diagnostics, a specify event check CLI command, and workflow dispatch pre-flight checks.

(Note: #5714 implements install-time warnings only and explicitly leaves portable Windows py -3 fallback and workflow dispatch pre-flight out of scope; this PR implements the full #5619 scope end-to-end.)

Changes

1. Portable Runtime Fallback (src/specify_cli/events/__init__.py)

  • Windows PowerShell 5.1 (copilot-json powershell / target_os="windows"): emits a PowerShell 5.1–compatible & { ... } command (no &&/||/??, $null | stdin isolation on probe checks, $global:LASTEXITCODE propagation, Get-Command -All filtering out Microsoft Store AppExecutionAlias stubs) trying .venv\Scripts\python.exe -> python -> py -3 without hard-coding host-specific paths.
  • POSIX sh (copilot-json bash / target_os="posix"): emits set -- <quoted_args>; if ... exec ... trying .venv/bin/python -> python3 -> python -> py -3 (covering Git Bash on Windows) with </dev/null on probes and exec preserving stdin and exit codes.
  • Single-command native hooks & ts-plugin: preserves project .venv preference, falls back to portable py -3 on Windows when python3/python are absent or Store stubs, and avoids baking ephemeral host sys.executable paths into generated configs.

2. Shared Interpreter Resolution & Install Diagnostics (src/specify_cli/events/__init__.py, src/specify_cli/events/command_check.py)

  • Added HookInterpreterStatus, HookInterpreterError, check_hook_interpreter, check_integration_hook_interpreter, preflight_hook_interpreter, and check_project_event_interpreters.
  • Resolves bare PATH commands (matching IntegrationBase._interpreter_runs Store-stub rejection on Windows), project-relative .venv paths against project_root, absolute paths, and py -3.
  • install_integration_events warns on stderr with the missing interpreter, tried candidates, and exact remedy (add Python to PATH, or create a project .venv and rerun specify integration upgrade <key>) without failing installation.
  • Added specify event check [--integration KEY] [--json] diagnostic command.

3. Workflow Dispatch Pre-Flight (src/specify_cli/workflows/step/_hook_preflight.py, command/__init__.py, prompt/__init__.py)

  • Runs hook-interpreter pre-flight before spawning the agent CLI whenever the integration has active event hooks installed in project_root or the step declares hook-dependent config (additional_context), failing fast as StepStatus.FAILED (dispatched=False) with an actionable error message. Projects without hooks skip the check.

Verification

  • pytest tests/specify_cli/events/ tests/workflows/test_hook_preflight.py tests/integrations/test_base.py tests/integrations/test_integration_vibe.py -q: 574 passed, 1 skipped (conditional live PowerShell test skipped on macOS; PowerShell 5.1 syntax/quoting invariants and live /bin/sh fallback execution tests pass; Windows CI exercises the live PowerShell test).
  • pytest -n auto tests/specify_cli/events/ tests/integrations/ tests/workflows/ tests/test_workflows.py tests/specify_cli/workflows/ -q: 7,291 passed, 38 skipped (0 failed).
  • ruff@0.15.0 check src tests: All checks passed.

…hecks

Emit portable Python 3 fallback chains in generated event hooks so hooks
keep working across Windows PowerShell 5.1 (.venv -> python -> py -3),
POSIX sh (.venv -> python3 -> python -> py -3), host/cmd Windows targets
(py -3 when python is absent or a Microsoft Store stub), and OpenCode's
TypeScript plugin without hard-coding host-specific paths.

Add shared interpreter availability helpers (check_hook_interpreter,
check_integration_hook_interpreter, preflight_hook_interpreter) that match
hook runtime resolution for PATH commands (including Windows Store stub
rejection), project-relative .venv paths, and absolute paths:
- Warn with actionable remediation during hook install/upgrade/refresh.
- Fail fast before spawning agent CLIs in workflow command/prompt steps
  that depend on hooks.
- Expose a diagnostic 'specify event check' CLI command with --json support.
Copilot AI adaptive review requested due to automatic review settings December 5, 2026 23:30
@dlwlgnsrhy
dlwlgnsrhy requested a review from mnriem as a code owner December 5, 2026 23:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Windows checks can execute untrusted project-controlled binaries without consent, while unresolved interpreter fallback mismatches and Windows test flaws undermine reliability.

12 open issues
What changed in this PR

Adds portable Python resolution and early diagnostics for Spec Kit event hooks, extending #5619 beyond #5714’s install-only warnings.

Changes:

  • Adds POSIX, PowerShell 5.1, and OpenCode interpreter fallbacks.
  • Adds installation warnings and specify event check.
  • Adds workflow preflight checks and resolution/runtime tests.

AI-generated review by GitHub Copilot (model unknown; automated) for the requesting contributor.

File Description
tests/​workflows/​test_hook_preflight.py Tests command and prompt preflight behavior.
tests/​specify_cli/​events/​test_hook_interpreter.py Tests resolution, fallback execution, and warnings.
tests/​specify_cli/​events/​test_events.py Updates Copilot hook-generation expectations.
tests/​specify_cli/​events/​test_command_check.py Tests diagnostic CLI and JSON output.
src/​specify_cli/​workflows/​step/​prompt/​__init__.py Checks interpreters before prompt dispatch.
src/​specify_cli/​workflows/​step/​command/​__init__.py Checks interpreters before command dispatch.
src/​specify_cli/​workflows/​step/​_hook_preflight.py Shares hook-dependency and failure handling.
src/​specify_cli/​events/​command_check.py Adds the interpreter diagnostic command.
src/​specify_cli/​events/​__init__.py Implements fallbacks, availability checks, and warnings.

Review effort: Max (adaptive)

Comment thread src/specify_cli/events/__init__.py Resolved
Comment thread src/specify_cli/events/__init__.py Resolved
Comment thread src/specify_cli/events/__init__.py Resolved
Comment thread src/specify_cli/events/__init__.py Resolved
Comment thread src/specify_cli/events/__init__.py Resolved
Comment thread src/specify_cli/events/command_check.py Resolved
@mnriem mnriem added the triage-must-have Verdict: high-value, important work for Spec Kit — do first label Dec 6, 2026
…tory

- Avoid executing untrusted project-controlled or explicit hook binaries during static interpreter checks without explicit opt-in (allow_probe_untrusted=False); validate regular files and reject Windows Store AppExecutionAlias stubs statically.
- Probe POSIX portable fallback PATH candidates (including py -3) with stdin=DEVNULL while preserving fixed-command lookup semantics.
- Enumerate all Windows PATH/PATHEXT matches for the portable PowerShell fallback chain so Store aliases preceding a working python.exe do not falsely fail preflight.
- Honor inspect_installed for Copilot hooks and OpenCode plugins, using the portable fallback chain only when the installed config or plugin implements it.
- Probe .venv Python binaries before selection in both PowerShell 5.1 and POSIX sh generated fallback commands so damaged venvs or directories fall through to PATH fallbacks.
- Replace _read_integration_json in check_project_event_interpreters with try_read_integration_json and structured EventCheckError so 'specify event check --json' serializes malformed state cleanly.
- Resolve Path.cwd() when context.project_root is omitted in workflow hook preflight.
- Add explicit hierarchy-owned MCP inventory dispositions for 'specify event' (event.check, event.run) wired into create_server with parity tests.
- Gate POSIX /bin/sh test fixture on non-Windows executable /bin/sh, prefer Windows PowerShell 5.1 over pwsh on Windows, and isolate PATH in the Windows py-launcher execution test.
Copilot AI adaptive review requested due to automatic review settings December 6, 2026 14:04
@dlwlgnsrhy

Copy link
Copy Markdown
Contributor Author

Addressed all 12 review findings in 4f69687:

  • Trust boundary for static checks: check_hook_interpreter, check_integration_hook_interpreter, preflight_hook_interpreter, and check_project_event_interpreters now avoid spawning project-controlled or repo-configured executables unless allow_probe_untrusted=True; explicit/relative paths and PATH matches inside project_root are validated statically (including Windows Store AppExecutionAlias detection), while system PATH Python binaries outside project_root continue to be probed.
  • Fallback chain & installed-hook parity:
    • Generated PowerShell 5.1 (.venv\Scripts\python.exe) and POSIX sh ([ -f ] && [ -x ]) fallback commands now probe the project venv with stdin isolated before selecting it, falling through to PATH fallbacks if damaged.
    • POSIX fallback chain checking probes system PATH candidates (including py -3), and Windows portable chain checking enumerates all PATH x PATHEXT entries (matching Get-Command -All) without changing fixed-command semantics.
    • inspect_installed=True now inspects the installed Copilot hook and OpenCode plugin on disk and only applies the portable fallback chain when the installed file actually uses it.
  • Structured errors, workflow pre-flight & MCP inventory:
    • check_project_event_interpreters uses try_read_integration_json and raises transport-neutral EventCheckError, which specify event check --json serializes cleanly.
    • _hook_preflight.py resolves Path.cwd() when context.project_root is omitted (matching project_dispatch).
    • Added hierarchy-owned src/specify_cli/events/_mcp.py with explicit MCP inventory dispositions for event.check and event.run, registered in mcp_server/server.py with inventory tests in tests/specify_cli/events/test_mcp.py.
  • Windows CI test hardening: gated /bin/sh fixture on non-Windows hosts, isolated PATH in the py-only Windows test, and selected Windows PowerShell 5.1 (powershell.exe) ahead of pwsh on Windows.

Verification: 4,118 passed across events/preflight/MCP/integrations, 1,014 passed across tests/workflows/, and ruff@0.15.0 check src tests clean.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

triage-must-have Verdict: high-value, important work for Spec Kit — do first

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants