Skip to content

fix(workflows): isolate init step process state - #5487

Open
WOLIKIMCHENG wants to merge 4 commits into
github:mainfrom
WOLIKIMCHENG:fix/workflow-init-process-isolation
Open

WOLIKIMCHENG wants to merge 4 commits into
github:mainfrom
WOLIKIMCHENG:fix/workflow-init-process-isolation

Conversation

@WOLIKIMCHENG

Copy link
Copy Markdown
Contributor

Description

Prevent concurrent workflow init steps from redirecting each other's relative project paths or capturing another step's output. Invoke init in an isolated child process pinned to the current Python interpreter and Spec-Kit source, leaving the parent working directory and streams untouched.

Preserve initialization options, failure diagnostics, target-local adapter loading and completed-step replay. Regression tests reproduce wrong-target scaffolding before the fix and verify real generated projects afterward.

Testing

  • 59 focused InitStep, concurrent workflow, resume and adapter-trust tests passed.
  • 169 existing init-command tests and 2 adapter compatibility tests passed.
  • 10 follow-up checks covering invalid arguments, launch failures, stream diagnostics, source pinning and integration errors passed.
  • Ruff 0.15.0 (ruff check --no-cache src tests) passed.
  • Tested locally with .venv/bin/specify --help
  • Ran relevant existing tests with .venv/bin/python -m pytest using focused paths
  • Tested with a sample project

Manual test results

Agent: Codex CLI | OS/Shell: Linux/Bash

Command tested Notes
/speckit.specify generated skill Followed the generated skill in a scratch project scaffolded with this checkout's .venv/bin/specify. Template resolution, specification and quality checklist creation completed; no extension hooks were installed. A separate slash-command host invocation was not exercised.

The init execution-boundary change maps to specify init and the generated /speckit.specify workflow. The other changed files are regression tests. Real InitStep scaffolding was exercised by pytest.

AI Disclosure

  • I did use AI assistance (described below)

AI disclosure: AI-assisted tools were used to implement the fix and develop tests.

Run workflow init in a child process to keep concurrent project paths and output independent while preserving CLI behavior and adapter loading.

Assisted-by: Codex CLI (model: gpt-6.1-sol, autonomous)
@WOLIKIMCHENG
WOLIKIMCHENG requested a review from mnriem as a code owner November 19, 2026 07:04
Copilot AI adaptive review requested due to automatic review settings November 19, 2026 07:04

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

The init subprocess retains PYTHONPATH, allowing project-local startup code to execute before bootstrap protections and bypass adapter trust checks.

4 open issues
What changed in this PR

Moves Spec Kit workflow init steps into child processes so concurrent initialization does not share the parent’s working directory or streams.

Changes:

  • Uses the current Python interpreter and Spec Kit source, with an explicit working directory and captured diagnostics.
  • Adds real-scaffolding regressions for concurrent execution, target paths, initialization options, and completed-step replay.
  • Tests target-local adapter loading and rejection of untrusted or modified adapters.

Validation was limited to source inspection and isolated standard-library checks; project tests and before/after comparisons were not run.

File Description
tests/​workflows/​test_composition_execution.py Tests concurrent target and stream isolation, plus resume replay.
tests/​test_workflows.py Adds initialization regression helpers, path and option coverage, and failure checks.
tests/​specify_cli/​integrations/​test_installed_adapters.py Tests adapter loading and trust enforcement across the child-process boundary.
src/​specify_cli/​workflows/​step/​init/​__init__.py Replaces in-process initialization with a pinned child-process invocation.

Review effort: Max (adaptive)

Comment thread tests/test_workflows.py Resolved
Comment thread tests/test_workflows.py Resolved
Comment thread tests/test_workflows.py Resolved
@mnriem

mnriem commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Please address Copilot feedback

@mnriem mnriem added the triage-must-have Verdict: high-value, important work for Spec Kit — do first label Nov 19, 2026
Drop inherited PYTHONPATH before Python startup and explicitly disable prompting.
Preserve trusted source path spelling and reject ambiguous module metadata.
Cover startup injection, source layouts and Windows-safe diagnostics.

Assisted-by: Codex (model: gpt-6-astra, autonomous)
Copilot AI adaptive review requested due to automatic review settings November 20, 2026 05:52
@WOLIKIMCHENG

WOLIKIMCHENG commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I've addressed the feedback in 6908013, including startup isolation, non-interactive execution, and Windows path handling.

Focused Linux checks and Ruff 0.15.0 passed. Native Windows testing (Python 3.14.6) completed with 75 passed and 6 skipped, plus 3 passing concurrency/resume tests. The generated resolver also passed under PowerShell 5.1. The full CI matrix was not rerun locally.

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.

Comment thread tests/test_workflows.py Resolved
Preserve interpreter restrictions, validate cached package trees, and restore
trusted dependency paths after startup. Keep child status and diagnostics
when output contains invalid UTF-8.

Assisted-by: Codex (model: gpt-6-astra, autonomous)
Copilot AI adaptive review requested due to automatic review settings November 23, 2026 07:36

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.

Comment thread tests/test_workflows.py Resolved
Comment thread tests/test_workflows.py Resolved
Comment thread tests/test_workflows.py Resolved
@mnriem

mnriem commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the detailed reproduction and work on this. Our intended model is that a project is initialized once, as a sequential bootstrap step; separate CI/CD runs already have process isolation. Could we address the fan-out scenario through sub-workflows instead, with an explicit subprocess execution boundary where isolation is needed? Sub-workflows currently share the parent process, so that would require a separate design change, but it may be a more appropriate place to address working-directory and stream isolation generally rather than introducing that machinery specifically for init. Is there a supported use case that would still require this init-specific change?

AI disclosure: Posted on behalf of @mnriem by GitHub Copilot (model: GPT-6.1 Sol; interactive mode). AI assessed the implementation and drafted this comment; @mnriem approved posting it.

Validate effective startup paths, restore approved dependencies after
startup, and use cancellable capture with bounded post-exit draining.

Assisted-by: Codex (model: gpt-6.1-sol, autonomous)
Copilot AI adaptive review requested due to automatic review settings November 24, 2026 05:35

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

Unresolved critical defects can bypass adapter trust checks and terminate initialization before rollback, risking unauthorized adapter execution and partially modified projects.

11 open issues
10 resolved since last review

Review effort: Max (adaptive)

Comment on lines +172 to +173
if not os.path.isabs(base):
base = os.path.join(os.getcwd(), base)
# bootstrap source pinning happens too late to protect that boundary.
child_env.pop("PYTHONPATH", None)
child_env["PYTHONIOENCODING"] = "utf-8"
try:
# Preserve the loaded package tree, including symlink/.. spelling.
# Resolving the initializer leaf can select a different package.
package_parent = os.path.dirname(os.path.dirname(source))
result = run_captured(
# Preserve the loaded package tree, including symlink/.. spelling.
# Resolving the initializer leaf can select a different package.
package_parent = os.path.dirname(os.path.dirname(source))
result = run_captured(
options["env"] = env
if stdin is not None:
options["stdin"] = stdin
tree = _ProcessTree(args, cwd, **options)
# -P omits the unsafe cwd entry but still honors PYTHONPATH. Drop it
# before startup can import project-controlled sitecustomize code;
# bootstrap source pinning happens too late to protect that boundary.
child_env.pop("PYTHONPATH", None)

stdout = result.stdout or ""
stderr = result.stderr or ""
if result.timed_out:
Comment thread tests/test_workflows.py
Comment on lines +4388 to +4389
except ProcessLookupError:
pass
Comment thread tests/test_workflows.py
monkeypatch.setattr(gate_module, "sys", _FakeSys(tty=tty))


def _isolate_init_home(monkeypatch, tmp_path_factory):
Comment thread tests/test_workflows.py
Comment on lines +3635 to +3648
original = subprocess.run
capture = init.run_captured

def bounded_run(*args, **kwargs):
kwargs.setdefault("timeout", 60)
return original(*args, **kwargs)

def bounded_capture(*args, **kwargs):
# Harness guard only; production init has no overall runtime limit.
kwargs.setdefault("timeout", 45)
return capture(*args, **kwargs)

monkeypatch.setattr(subprocess, "run", bounded_run)
monkeypatch.setattr(init, "run_captured", bounded_capture)
@WOLIKIMCHENG

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Updated in 3faedc0.

The changes validate effective startup paths and restore approved dependencies only after startup. Init now uses cancellable capture with bounded post-exit draining; the Windows APPDATA fixture and symlink skips are also corrected.

For the design question: one reproducible case is a controller initializing two distinct projects once each, concurrently, then running downstream work against each scaffold. The new composition test exercises real init calls and verifies both projects. Current workflow calls share the parent root and process, so wrapping init in a subworkflow does not currently provide isolation. This change leaves the general subworkflow boundary unchanged. Does this distinct-project case fit the intended workflow model, or would you prefer isolation at a general subprocess boundary?

Validation: final focused startup checks passed (57 passed, 1 skipped), with earlier capture and composition checks retained. Bounded native Windows 3.12 capture checks and all 33 user-base selection oracle cases passed. Full native InitStep and Windows 3.14/3.15 CI remain unverified locally.

@mnriem

mnriem commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Thanks for clarifying the concurrent initialization use case. We can’t take the init-specific isolation change, but we’d be happy to repurpose this PR to add an explicit subprocess execution boundary for sub-workflows, so each isolated invocation has its own process and project root. Your use case is a good example: the existing fan-out step could invoke one isolated sub-workflow per project, and each sub-workflow would run init once before its downstream steps. Please keep both fan-out and init unchanged.

AI disclosure: Posted on behalf of @mnriem by GitHub Copilot (model: GPT-6.2 Sol; interactive mode). Copilot assessed the PR and drafted this comment; @mnriem directed the scope and approved posting it.

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