Skip to content

refactor(cli): one process runner owns execution, process groups and dry-run #775

Closed

Description

@dan-petty

Context & Rationale

Running external tools has no single owner. run_subprocess and run_subprocess_async (core/process.py:218-343, :345-498) are near-copies that have drifted. Process-group termination is written four times (ai/harness/shell.py:314, ai/review/review_environment.py:225, k8s/port_forward_daemon.py:41, sandbox/host.py:41). Under dry-run the runner only prints "Would run command" and still executes, so 219 is_dry_run() calls across 64 files make the decision instead. The uv --preview-features flags are injected in three places with different sets (commands/ci.py:180, :271, commands/uv.py:54).

Five command modules copy a ~25-line _get/_LAZY_OBJECT_MAPPING block (138 call sites) whose main use is letting tests patch run_subprocess by name. Tests patch it in 29 modules (117 patches), and 12 test files patch subprocess.run globally.

Scope & Deliverables

  • Deliverable: core/process owns running tools: one implementation for sync and async, the process-group lifecycle and kill on timeout, the dry-run decision based on intent the caller declares, and tool argv preparation such as the uv flags.
    • Commands receive the runner through one seam; tests replace it with one shared recording fake.
    • The _get blocks become function-local imports.
    • Staged: process groups and the sync/async merge first.
  • Constraint: --dry-run never executes a mutating command.

Strategic Alignment

  • Design: none
  • Depends on: none

Test design: the process fake (2026-10-07)

The owner decided on 2026-10-07, in the test design #1307, that tests double only process edges, each through one seam, and that every fake ships with a contract suite. This item's runner is the seam for the process edge: every program except git and bwrap, with shutil.which as part of the fake. Its "one shared recording fake" is that edge's fake.

The four process-group copies stay this item's to consolidate. At 5c45b1e they sit at ai/harness/shell.py:338, ai/review/review_environment.py:396 (which has no caller), k8s/port_forward_daemon.py:79 and sandbox/host.py:58. The package migrations (#1330, #1328, #1322 and #1335) only confirm that none remains in their packages.

Acceptance criteria (added).

Housekeeping (added; the repository's standard gates, not new scope).

  • uv run devops ci passes.

Strategic alignment (added).

Update 2026-10-08: refinement

The constraint is weaker than the owner's dry-run rule (#412). It becomes: under a dry run the runner starts no child that makes an external request or changes state and returns its PlannedRequest; only a declared local read runs (core/process.py:363-393).

  • With the process fake, gh, kubectl and git commit start nothing under a dry run; a declared git rev-parse runs.

Proposed design

Problem statement

The devops-cli project has inconsistent and duplicated process execution logic across multiple files. There are near-identical functions for running subprocesses (run_subprocess and run_subprocess_async), multiple implementations of process-group termination, widespread use of a dry-run flag scattered across 64 files, and inconsistent injection of uv flags in three places.

Acceptance criteria

  • Criterion 1: One process runner owns execution, process groups, and dry-run logic
    Verification: All external tool execution goes through a single core/process.py implementation for both sync and async operations
  • Criterion 2: Process-group lifecycle is consolidated into one location
    Verification: Four existing process-group termination implementations (ai/harness/shell.py:314, ai/review/review_environment.py:225, k8s/port_forward_daemon.py:41, sandbox/host.py:41) are consolidated into core/process.py
  • Criterion 3: Dry-run decision is made consistently based on caller intent
    Verification: The dry-run behavior properly handles the declared local read operations while preventing external requests under --dry-run mode
  • Criterion 4: Tool argv preparation with uv flags is centralized
    Verification: Three places injecting uv --preview-features flags are consolidated into one location in core/process.py
  • Criterion 5: Tests can patch run_subprocess through a single seam
    Verification: Tests replace the shared runner with one recording fake, rather than patching 29 modules or globally patching subprocess.run

Key questions

  • What is the test design approach?: Tests double only process edges, each through one seam, and every fake ships with a contract suite. The runner is the seam for the process edge. [sources: docs/adr/0007-tests-double-only-process-edges.md]
  • Should the dry-run behavior execute external requests?: Under a dry run, the runner starts no child that makes an external request or changes state and returns its PlannedRequest; only a declared local read runs (proposed)

Open questions

  • What is the current structure of process execution in the codebase? (missing verified source)
  • How are dry-run decisions currently made? (missing verified source)
  • What is the scope of changes needed for this refactoring? (missing verified source)

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/refactor
    scope/cli
    priority/p2-medium
    on Oct 1, 2026
  2. dan-petty commented on Oct 2, 2026

    @dan-petty
    OwnerAuthor

    More evidence for this issue from the devsecops review session 20261001-224227 (2026-10-02). Each finding below was triaged against release/v0.2.25 (c279c18), judged real and covered by this issue, and then upheld by a second agent that tried to refute it.

    • Insecure Process Group Termination in _terminate_process_group (src/devops_cli/ai/review/review_environment.py:225, re-rated LOW): review_environment._terminate_process_group (lines 225-230) calls os.killpg(os.getpgid(pid), SIGTERM) with no guards. Nothing calls it: a grep of src and tests finds only the definition, since criterion commands run through sandbox.host.HostSandbox. The claimed zombies and failed cleanup therefore have no path. It is dead code that duplicates the guarded helper in sandbox/host.py:41-56, which refuses PID 0/1, its own PID and its own PGID. Open issue refactor(cli): one process runner owns execution, process groups and dry-run #775 lists ai/review/review_environment.py:225 among the four copies of process-group termination that one process runner should own.
  3. Jayanthpulisheri commented on Oct 3, 2026

    @Jayanthpulisheri

    Hey @dan-petty! Read your refactor issue around unifying process ownership, process groups, and dry-run execution. Check out veltra-agent—it acts as a single CLI execution harness that manages process lifecycles, dry-run policies, and command boundaries at the OS edge. Would love to get your thoughts on it.

  4. dan-petty commented on Oct 3, 2026

    @dan-petty
    OwnerAuthor

    Consolidation (2026-10-03): #780 is a sub-issue of this item and its first stage. #780 brings the sync and async runners together on telemetry redaction and on killing the process group at timeout, using one helper in core/process. This item then:

    • replaces the other hand-written group kills: ai/harness/shell.py:118, k8s/port_forward_daemon.py:79-102, and ai/review/review_environment.py:396, which is dead code;
    • merges the two runners;
    • adds the dry-run decision based on declared intent.

    Linked:

  5. 1 remaining item

  6. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Amendment from the 2026-10-08 client-library audit (at 05e9388): four git children call subprocess.run directly. They skip run_subprocess, and with it the environment rule, the session's GH_TOKEN pin, its timeout handling and its span:

    • github/metrics.py:147 (git tag -l v* --format=...), :190 (git rev-list --count) and :204-205 (git log --pretty=format:%s%n%b);
    • ai/rag/drift.py:55 (git rev-parse HEAD).

    If this item's runner is the one execution path, these move onto it.

    Folded here instead of a new issue: the agent filing quota (#1153) is spent for this cycle, and this refactor shares this item's change surface (every git child through the one runner). It is recorded here in full so it is not lost.

    refactor(git): GitPython is removed and every git child runs through run_subprocess

    Problem

    Lines cited at 05e9388 (release/v0.2.29).

    GitPython runs git in seven functions, while every other git child in src (about 90 argv sites) runs through core/process.run_subprocess:

    • git/operations.py:16 imports it for clone_repo (:195-199), fetch_all (:202-209), pull_tracking (:212-221), create_branch (:224-231), list_branches (:234-247) and delete_merged_branches (:250-270).
    • commands/repos.py:62-69 (_current_branch).

    What that costs:

    • No identity pin. Clones, fetches and pulls there get the ambient environment instead of refactor(github): one github session per identity owning token, quota and transport #767's GH_TOKEN pin (docs/agent/tasks/task-767-github-session-per-identity.md:63), and GitPython's custom_environment(NAME=None) cannot remove an inherited GITHUB_TOKEN (docs/agent/tasks/task-1124-release-push-person-environment.md:33, checked with GitPython 3.2.0).
    • No timeout. Repo.clone_from, Remote.fetch and Remote.pull run with GitPython's default kill_after_timeout=None, so one unreachable remote stalls devops branches sync or devops repos clone-org. run_subprocess applies a timeout.
    • A process on import. Importing GitPython runs git version (docs/agent/tasks/task-836-mcp-command-contract.md:46).
    • Porcelain parsing anyway. delete_merged_branches parses git branch --merged with .lstrip("* ") and takes main, master or the first branch as the default (:250-263), while ai/review/runner.py:1646 reads refs/remotes/origin/HEAD.
    • Maintenance mode. GitPython's README: "This project is in maintenance mode": no feature development or bug fixes unless contributed or relevant to users' safety. 3.2.0 (2026-09-30) is pinned.
    • The agent guidance says the reverse. ai/knowledge_base/devops_cli/libraries/gitpython.md:32 lists subprocess git as "Rejected: GitPython provides structured objects", and :19-21 credit GitPython with review diffs and repo.is_dirty(), which ai/review/runner.py and git/operations.py:273-279 do with subprocess git.

    pygit2 and dulwich are not the alternative: neither runs git's credential helpers (gh auth git-credential, or the person's own helper that #1124's pushes rely on), nor the OpenSSH host-key pinning #1114 builds on. The git CLI is the reference client, and the codebase already reads it in machine-readable form (git/operations.py:321, :380-414).

    Proposed remedy

    Port the seven functions to run_subprocess with git's machine-readable output, and remove GitPython:

    • git clone -- <url> <dest>; git fetch --all --prune; the same pull of the tracking branch as today.
    • git check-ref-format --branch <name>, then git switch -c <name>.
    • git for-each-ref --format=%(refname:short) refs/heads/ (and refs/remotes/ for --all), and git symbolic-ref --short -q HEAD for the current branch.
    • git for-each-ref --merged=<default> --format=%(refname:short) refs/heads/, then git branch -d, with the default from git symbolic-ref --short refs/remotes/origin/HEAD.
    • Remove gitpython from pyproject.toml and uv.lock, ai/knowledge_base/devops_cli/libraries/gitpython.md, its row in docs/KNOWLEDGE_BASE.md and its entry in ai/knowledge_base/devops_cli/python_packages.md.

    #1399 and #900 change three of these functions; whichever lands second rebases. #1333's criterion "git and GitPython run for real" becomes "git runs for real".

    Acceptance criteria

    • rg -n "^\s*(import git\b|from git( |\.))" src tests and rg -n -i gitpython pyproject.toml uv.lock src docs print nothing.
    • Tests run real git in temporary repositories: a clone from a local bare repository (no network), branch create and list, and merged-branch cleanup where origin/HEAD names a default other than main.
    • With a fake git on PATH (the process edge) and GITHUB_TOKEN=other in the environment, the clone, fetch and pull children receive the session's GH_TOKEN and no GITHUB_TOKEN.
    • Every new test runs in under 1 s and opens no socket; uv run devops ci passes.
    • changelog.d/<issue>.md records the removed dependency under ### Removed.

    Related: #767, #775, #900, #1114, #1124, #1333, #1399.

    Found by the 2026-10-08 client-library audit.

  7. modified the milestone: on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p2-mediumscope/clitype/refactor

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions