You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@magpie.apache.org by po...@apache.org on 2026/10/16 02:08:19 UTC

(magpie) branch main updated: fix(agent-guard): recognize the +refspec force spelling in the empty-rebase guard (#1607)

This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/magpie.git


The following commit(s) were added to refs/heads/main by this push:
     new b947598a fix(agent-guard): recognize the +refspec force spelling in the empty-rebase guard (#1607)
b947598a is described below

commit b947598af2f0b7d8e9fbebda14b00d7c1145a25d
Author: kuse <31...@qq.com>
AuthorDate: Fri Oct 16 10:08:13 2026 +0800

    fix(agent-guard): recognize the +refspec force spelling in the empty-rebase guard (#1607)
    
    * fix(agent-guard): recognize the +refspec force spelling in the empty-rebase guard
    
    The bundled empty-rebase guard only recognized -f/--force/--force-with-lease as a
    force-push. git-push(1) documents a leading + on a refspec as equivalent to
    --force for that refspec, so `git push origin +empty:main` bypassed the guard
    entirely. Recognize such refspecs as forced and strip the prefix before
    resolving the source ref, since `git rev-parse +ref` fails and would fail open.
    
    Closes #1606
    
    * chore(agent-guard): sync marketplace plugin copy of agent_guard
    
    The check-family-plugins prek hook regenerates the per-family plugin
    copy from the tool sources and rewrote it in CI to pick up the
    empty-rebase `+refspec` change. Commit the hook-applied result.
    
    * chore(setup-preflight): refresh isolated-setup fingerprint
    
    Generated-by: GitHub Copilot (Claude Sonnet 5.5)
    
    ---------
    
    Co-authored-by: Jarek Potiuk <po...@apache.org>
---
 .../tools/agent-guard/src/agent_guard/__init__.py  |  8 +++--
 .../setup/setup_preflight/isolated_fingerprint.py  |  2 +-
 tools/agent-guard/src/agent_guard/__init__.py      |  8 +++--
 tools/agent-guard/tests/test_guards.py             | 34 ++++++++++++++++++++++
 4 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/plugins/magpie-agent-guard/tools/agent-guard/src/agent_guard/__init__.py b/plugins/magpie-agent-guard/tools/agent-guard/src/agent_guard/__init__.py
index f9b4fc83..13fcb888 100644
--- a/plugins/magpie-agent-guard/tools/agent-guard/src/agent_guard/__init__.py
+++ b/plugins/magpie-agent-guard/tools/agent-guard/src/agent_guard/__init__.py
@@ -879,17 +879,21 @@ def guard_empty_rebase(seg: Segment, cwd: str | None) -> str | None:
     idx = git_subcommand_index(seg.argv)
     if idx is None or seg.argv[idx] != "push":
         return None
+    # `+refspec` is git's other force spelling: git-push(1) documents it as
+    # equivalent to `--force` for that refspec, so it must be recognized here
+    # and stripped from the refs the resolution below reads.
+    refspecs = [t for t in seg.argv[idx + 1 :] if not t.startswith("-")]
     forced = any(
         t in ("-f", "--force") or t == "--force-with-lease" or t.startswith("--force-with-lease=")
         for t in seg.argv
-    )
+    ) or any(r.startswith("+") for r in refspecs)
     if not forced:
         return None
     if seg.override("MAGPIE_ALLOW_EMPTY_PUSH"):
         return None
 
     # Resolve the source ref being pushed: last `src[:dst]` positional, else HEAD.
-    positionals = [t for t in seg.argv[idx + 1 :] if not t.startswith("-")]
+    positionals = [t.removeprefix("+") for t in refspecs]
     src = "HEAD"
     if len(positionals) >= 2:
         src = positionals[1].split(":", 1)[0] or "HEAD"
diff --git a/plugins/magpie-setup/skills/setup/setup_preflight/isolated_fingerprint.py b/plugins/magpie-setup/skills/setup/setup_preflight/isolated_fingerprint.py
index 07fd2316..7ca20eb9 100644
--- a/plugins/magpie-setup/skills/setup/setup_preflight/isolated_fingerprint.py
+++ b/plugins/magpie-setup/skills/setup/setup_preflight/isolated_fingerprint.py
@@ -21,4 +21,4 @@ The isolated-setup fingerprint of the framework this package shipped with,
 for installs that do not carry the framework source (see `isolated.py`).
 """
 
-FRAMEWORK_FINGERPRINT = "sha256:15155ba8258ca985"
+FRAMEWORK_FINGERPRINT = "sha256:29f8657dadee25f7"
diff --git a/tools/agent-guard/src/agent_guard/__init__.py b/tools/agent-guard/src/agent_guard/__init__.py
index f9b4fc83..13fcb888 100644
--- a/tools/agent-guard/src/agent_guard/__init__.py
+++ b/tools/agent-guard/src/agent_guard/__init__.py
@@ -879,17 +879,21 @@ def guard_empty_rebase(seg: Segment, cwd: str | None) -> str | None:
     idx = git_subcommand_index(seg.argv)
     if idx is None or seg.argv[idx] != "push":
         return None
+    # `+refspec` is git's other force spelling: git-push(1) documents it as
+    # equivalent to `--force` for that refspec, so it must be recognized here
+    # and stripped from the refs the resolution below reads.
+    refspecs = [t for t in seg.argv[idx + 1 :] if not t.startswith("-")]
     forced = any(
         t in ("-f", "--force") or t == "--force-with-lease" or t.startswith("--force-with-lease=")
         for t in seg.argv
-    )
+    ) or any(r.startswith("+") for r in refspecs)
     if not forced:
         return None
     if seg.override("MAGPIE_ALLOW_EMPTY_PUSH"):
         return None
 
     # Resolve the source ref being pushed: last `src[:dst]` positional, else HEAD.
-    positionals = [t for t in seg.argv[idx + 1 :] if not t.startswith("-")]
+    positionals = [t.removeprefix("+") for t in refspecs]
     src = "HEAD"
     if len(positionals) >= 2:
         src = positionals[1].split(":", 1)[0] or "HEAD"
diff --git a/tools/agent-guard/tests/test_guards.py b/tools/agent-guard/tests/test_guards.py
index 8b293b4d..8348a869 100644
--- a/tools/agent-guard/tests/test_guards.py
+++ b/tools/agent-guard/tests/test_guards.py
@@ -351,6 +351,40 @@ def test_empty_rebase_denied_past_a_global_flag(monkeypatch):
     assert reason and "0 commits" in reason
 
 
+def test_empty_rebase_denied_plus_refspec(monkeypatch):
+    # `+refspec` is git's other force spelling (git-push(1) documents it as
+    # equivalent to --force for that refspec); without it the guard is
+    # bypassed by `git push origin +branch` and `git push origin +b:d`.
+    monkeypatch.setattr(agent_guard, "_run", fake_run(_push_handler("0")))
+    reason = dispatch("git push origin +mybranch:mybranch")
+    assert reason and "0 commits" in reason
+
+
+def test_empty_rebase_denied_plus_refspec_short(monkeypatch):
+    monkeypatch.setattr(agent_guard, "_run", fake_run(_push_handler("0")))
+    reason = dispatch("git push origin +mybranch")
+    assert reason and "0 commits" in reason
+
+
+def test_plus_refspec_still_guarded_behind_a_global_flag(monkeypatch):
+    monkeypatch.setattr(agent_guard, "_run", fake_run(_push_handler("0")))
+    reason = dispatch("git -C /tmp push origin +mybranch:mybranch")
+    assert reason and "0 commits" in reason
+
+
+def test_plus_refspec_nonempty_push_allowed(monkeypatch):
+    monkeypatch.setattr(agent_guard, "_run", fake_run(_push_handler("3")))
+    assert dispatch("git push origin +mybranch:mybranch") is None
+
+
+def test_plain_refspec_push_not_guarded_as_force(monkeypatch):
+    def boom(args):
+        raise AssertionError("non-force push is not guarded")
+
+    monkeypatch.setattr(agent_guard, "_run", fake_run(boom))
+    assert dispatch("git push origin mybranch:mybranch") is None
+
+
 # --------------------------------------------------------------------------- #
 # bundled example contributed guard (guards.d/no_verify_commit.py)
 # --------------------------------------------------------------------------- #