Skip to content

fix: isolate sequential hook execution state - #243

Open
priyanshuuu777 wants to merge 3 commits into
sciops-org:mainfrom
priyanshuuu777:fix/isolate-sequential-hook-execution
Open

priyanshuuu777 wants to merge 3 commits into
sciops-org:mainfrom
priyanshuuu777:fix/isolate-sequential-hook-execution

Conversation

@priyanshuuu777

@priyanshuuu777 priyanshuuu777 commented Nov 27, 2026 •

Copy link
Copy Markdown

This PR fixes state corruption during sequential hook execution in Makim.

Previously, _run_hooks() created a single copied Makim instance and reused it across hook iterations. Each hook was executed through _execute_hook(), which called run() directly on that shared instance.

Since Makim.run() invokes _change_task() and updates task-scoped execution state, executing the first hook could mutate the instance subsequently used by other hooks. As a result, later hooks could resolve variables and environment values from the previous hook's scope instead of the parent task's scope.

This could lead to incorrect conditional evaluation, unexpected environment-variable leakage, and subsequent hooks being skipped or executed with incorrect context.

This change isolates each hook execution by creating a dedicated deep copy of the hook runner inside _execute_hook(). Mutations performed by run() are therefore confined to that individual execution, preserving the state used by the hook-processing loop and exception-handling path.

Changes made

  • Create a dedicated deepcopy(makim_hook) for each hook execution in _execute_hook().
  • Execute the hook using the isolated runner rather than mutating the shared hook-processing instance.
  • Preserve existing hook ordering, argument handling, and exception-handling behavior.
  • Add regression coverage for sequential hooks, parent and hook-specific variables, environment isolation, conditional hooks, nested hooks, and failure scenarios.
  • Add end-to-end CLI coverage to verify sequential hook execution.

How to test these changes

Run the complete test suite:

pytest -s -vv tests

Run linting:

ruff check --no-fix src/makim tests

Verify formatting:

ruff format --check src/makim tests

Run static type checking:

mypy --python-version 3.13 src/makim

Run the security scan:

bandit --configfile pyproject.toml -r src/makim/

Regression verification:

The newly added regression tests were also executed against an untouched baseline worktree (48e09d4).

  • Before the fix: 9 failed, 2 passed
  • After the fix: 25 passed

The failures on the baseline demonstrate the affected behavior, while the patched implementation passes the complete test suite.

Smoke testing:

20/20 macOS-applicable smoke-test targets from the project's main workflow passed, including run-hooks, failure-hook, vars-env, validate-conditionals, scheduler, and ignore-error-option.

Linux-only Docker-dependent smoke tests were not executed in the macOS environment.

Pull Request checklists

This PR is a:

  • bug-fix
  • new feature
  • maintenance

About this PR:

  • it includes tests.
  • the tests are executed on CI.
  • the tests generate log file(s) (path).
  • pre-commit hooks were executed locally.
  • this PR requires a project documentation update.

Author's checklist:

  • I have reviewed the changes and it contains no misspelling.
  • The code is well commented, especially in the parts that contain more complexity.
  • New and old tests passed locally.

Additional information

Baseline reproduction:

The issue was reproduced on the original main implementation before applying the fix.

The failure was not limited to hook output: subsequent hook executions could observe mutated task and group context, affecting scoped variables, environment values, and conditional evaluation.

Implementation scope:

The fix is intentionally localized to _execute_hook() and does not introduce a new public API, dependency, or configuration option.

CLI verification:

The same demonstration configuration was executed against the baseline and patched implementations.

  • Before: subsequent hooks inherited incorrect scope, and a conditional hook was skipped.
  • After: each hook received the expected parent context, hook-specific values remained isolated, and all expected hooks executed.

No frontend or unrelated UI changes were introduced, as this is a CLI execution-state issue.

@priyanshuuu777

Copy link
Copy Markdown
Author

Hi @xmnlab, could you take a look at this PR when you get a chance?

@xmnlab

xmnlab commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

thanks @priyanshuuu777 i am reviewin this right now

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants