Skip to content

fix(git-shim): gate classify() denies on agent-invocation, not just tracked-repo scope - #389

Merged
getappz merged 4 commits into
masterfrom
task/371
Aug 4, 2026
Merged

fix(git-shim): gate classify() denies on agent-invocation, not just tracked-repo scope#389
getappz merged 4 commits into
masterfrom
task/371

Conversation

@getappz

@getappz getappz commented Aug 4, 2026

Copy link
Copy Markdown
Owner

Summary

  • The git-shim's main() applied its full deny policy (protected-branch checkout/switch/delete/rename, trust-root push, plumbing block, worktree block, scope-check) to every git invocation inside an agentflare-tracked repo, regardless of whether an agent CLI or a regular human was driving the shell. Only the narrower canonical-detach guard checked agent invocation; the main classify() path did not.
  • main.rs now bails out to real git immediately when classify::agent_invocation_detected() is false, before any classify/scope-check/audit work runs — a human interactively running git in a tracked repo now sees zero restrictions from this shim.
  • agent_invocation_detected() now delegates to the agent-detector crate (already a dependency, used in provenance.rs) instead of a second hardcoded env-var list — fixes two env var names that don't actually exist (CODEX_CLI_SESSION, GEMINI_SESSION) and covers a much broader agent catalog. Kept the AGENTFLARE_AGENT OR-clause since agent-detector doesn't know about agentflare's own internal marker. Disabled agent-detector's process-tree feature, since this shim runs from inside various agent CLIs by design and ancestor-process matching would false-positive on a human's terminal merely because their editor (Cursor, VS Code) is an ancestor process.
  • shim_test.rs: shim() helper now sets CLAUDECODE=1 so existing deny-path tests stay deterministic instead of relying on ambient agent-marker leakage; added two new tests covering human passthrough on the two main deny paths (protected-branch checkout, default-branch push).

Test plan

  • cargo build --workspace
  • cargo test -p flare-git-core -p flare-git-shim (162 tests)
  • cargo fmt --check
  • cargo clippy -p flare-git-core -p flare-git-shim --all-targets -- -A unsafe_code -A clippy::pedantic -D warnings
  • Merged current master in; no conflicts with the recent push-refspec classification fix (fix(git-shim): pushed_branch() mishandles src:dest push refspecs #387)

Summary by CodeRabbit

  • New Features

    • Added clearer separation between agent-initiated and human Git operations.
    • Human Git commands now pass through without agent-specific restrictions, repository checks, auditing, or snapshots.
    • Protected-branch checkouts and default-branch pushes are blocked for agent-initiated operations while remaining available for human use.
  • Bug Fixes

    • Improved detection of agent invocations using standardized signals and environment settings.
    • Ensured agent policy behavior is applied consistently across supported Git operations.

getappz added 2 commits July 26, 2026 07:00
…tracked-repo scope

flare-git-shim previously applied its full deny policy (protected-branch
checkout/switch/delete/rename, trust-root push, plumbing block, worktree
block, scope-check) to every git invocation inside an agentflare-tracked
repo regardless of whether an agent CLI or a regular human was driving the
shell. Only the narrower canonical-detach guard checked agent invocation;
the main classify() path did not.

- flare-git-shim/main.rs: bail out to real git immediately when
  classify::agent_invocation_detected() is false, before any
  classify/scope-check/audit work runs.
- flare-git-core/classify.rs: agent_invocation_detected() now delegates to
  the agent-detector crate (already a dependency, used in provenance.rs)
  instead of a second hardcoded env-var list -- fixes two wrong var names
  the old list had (CODEX_CLI_SESSION, GEMINI_SESSION aren't real vars
  those tools set) and covers a much broader agent catalog. Kept the
  AGENTFLARE_AGENT OR-clause since agent-detector doesn't know about
  agentflare's own internal marker. Disabled agent-detector's process-tree
  feature: this shim runs from inside various agent CLIs by design, so
  ancestor-process matching would false-positive on a human's terminal
  merely because their editor (Cursor, VS Code) is an ancestor process.
- shim_test.rs: shim() helper now sets CLAUDECODE=1 so existing deny-path
  tests stay deterministic instead of relying on ambient agent-marker
  leakage; added two new tests covering human passthrough on the two main
  deny paths (protected-branch checkout, default-branch push).
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The change centralizes agent detection using the agent-detector library, gates Git shim policy enforcement on that detection, and adds tests for agent denial and human passthrough behavior.

Changes

Agent-aware Git dispatch

Layer / File(s) Summary
Agent detection and dependency configuration
crates/flare-git-core/Cargo.toml, crates/flare-git-core/src/classify.rs
Disables default agent-detector features. Uses agent_detector::is_agent() and a non-empty AGENTFLARE_AGENT value for detection.
Agent-gated shim dispatch
crates/flare-git-shim/src/main.rs
Documents agent-only restrictions. Non-agent invocations execute real Git before classification, policy checks, auditing, or snapshots.
Agent and human policy coverage
crates/flare-git-shim/tests/shim_test.rs
Marks agent tests explicitly with environment variables. Adds human_shim to test non-agent passthrough. Verifies protected-branch checkout and default-branch push behavior for agent and human invocations.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant GitInvocation
  participant GitShim
  participant AgentDetector
  participant RealGit
  GitInvocation->>GitShim: Start git command
  GitShim->>AgentDetector: Detect agent environment
  AgentDetector-->>GitShim: Return agent status
  alt Non-agent invocation
    GitShim->>RealGit: Execute git immediately
  else Agent invocation
    GitShim->>GitShim: Run classification and policy checks
  end
Loading

Possibly related PRs

  • getappz/agentflare#279: Introduces the agent_invocation_detected function in classify.rs that this PR refactors to use the agent-detector library.
  • getappz/agentflare#359: Refactors Git shim execution paths in the same file to manage real Git invocation through a new exec_real() helper, overlapping with this PR's early-exit gate.
  • getappz/agentflare#320: Modifies agent-aware classification and Git shim policy behavior in related files.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: applying classify() denial only to agent invocations rather than all tracked-repository commands.
Description check ✅ Passed The description includes a detailed summary and completed test plan that match the pull request changes; only the optional reviewer notes section is absent.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch task/371

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/flare-git-shim/tests/shim_test.rs`:
- Around line 38-40: The human-path tests do not fully isolate agent-detection
environment variables. In crates/flare-git-shim/tests/shim_test.rs at lines
312-313, 368-375, and 404-411, add and use a shared helper that clears the child
environment and restores only required variables such as PATH and
AGENTFLARE_HOME_OVERRIDE; update the documentation at lines 38-40 and 341-346 to
describe this controlled environment and apply it consistently to all three
human-path test blocks.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: ac5cd6a0-4f13-4953-84d1-9e4c6c5a42ae

📥 Commits

Reviewing files that changed from the base of the PR and between 158ad92 and 73d7a78.

📒 Files selected for processing (4)
  • crates/flare-git-core/Cargo.toml
  • crates/flare-git-core/src/classify.rs
  • crates/flare-git-shim/src/main.rs
  • crates/flare-git-shim/tests/shim_test.rs

Comment thread crates/flare-git-shim/tests/shim_test.rs Outdated
@getappz getappz changed the title flare-git-shim: gate classify() denies on agent-invocation, not just tracked-repo scope fix(git-shim): gate classify() denies on agent-invocation, not just tracked-repo scope Aug 4, 2026
Human-path tests only stripped the old, narrow hardcoded agent-marker
list before asserting passthrough, but agent_invocation_detected now
delegates to the agent-detector crate's much wider catalog. Replace
the manual env_remove chains with a shared human_shim helper that
strips agent-detector's full env-var catalog (both its 'standard' tier
-- AI_AGENT/AGENT -- and its ~26 tool-specific vars) plus
AGENTFLARE_AGENT, so these tests actually prove human passthrough
instead of only working by ambient coincidence. An env_clear-based
approach was tried first but broke real-git resolution (which_in's
path filtering needs more of the environment than PATH alone), so this
keeps the full inherited environment and only removes known markers.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/flare-git-shim/tests/shim_test.rs`:
- Around line 366-368: Update the comment near the human_shim usage to
accurately state that human_shim inherits the child environment while removing
selected agent markers; do not describe it as clearing the whole environment.
Preserve the explanation for choosing human_shim over reliance on ambient marker
absence.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c7753ce2-1f03-4a08-9fc9-f82bf4d3703c

📥 Commits

Reviewing files that changed from the base of the PR and between 73d7a78 and 8e5a36a.

📒 Files selected for processing (1)
  • crates/flare-git-shim/tests/shim_test.rs

Comment thread crates/flare-git-shim/tests/shim_test.rs Outdated
Leftover phrasing from the earlier env_clear attempt said human_shim
clears the whole environment; it now inherits it and strips known
agent markers instead.
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.

1 participant