Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): anchor isPatchCommand to real command segments

liuzixiange9d4ecc71e4a
diff --git a/packages/agent-runtime/src/mutation-recovery.test.ts b/packages/agent-runtime/src/mutation-recovery.test.ts
index ec3b34680..e14461b6a 100644
--- a/packages/agent-runtime/src/mutation-recovery.test.ts
+++ b/packages/agent-runtime/src/mutation-recovery.test.ts
@@ -2,7 +2,7 @@ import { mkdtemp, realpath, rm, stat, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { describe, expect, it } from "vitest";
-import { mutationFailureKey } from "./mutation-recovery.js";
+import { isPatchCommand, mutationFailureKey } from "./mutation-recovery.js";
describe("mutationFailureKey", () => {
it("uses a stable lexical identity when the target does not exist", async () => {
@@ -49,3 +49,41 @@ describe("mutationFailureKey", () => {
}
});
});
+
+describe("isPatchCommand", () => {
+ // #1985: the git check used to anchor `git` and `apply` to the same blob of
+ // text, so any command that mentioned git and later carried the word apply
+ // counted as a patch — three failed searches terminated the turn.
+ it("does not count searches and other tools that merely mention apply", () => {
+ expect(isPatchCommand("git log --oneline | grep apply")).toBe(false);
+ expect(isPatchCommand("git pull && kubectl apply -f k8s/")).toBe(false);
+ expect(isPatchCommand("git diff --stat; terraform apply")).toBe(false);
+ expect(isPatchCommand("git grep apply")).toBe(false);
+ expect(isPatchCommand("echo git apply p.diff")).toBe(false);
+ expect(isPatchCommand("git log | grep apply | wc -l")).toBe(false);
+ expect(isPatchCommand("git format-patch -1")).toBe(false);
+ expect(isPatchCommand("echo apply_patch")).toBe(false);
+ expect(isPatchCommand(42)).toBe(false);
+ expect(isPatchCommand(undefined)).toBe(false);
+ });
+
+ it("still counts real patch invocations", () => {
+ expect(isPatchCommand("git apply p.diff")).toBe(true);
+ expect(isPatchCommand("git apply --check p.diff")).toBe(true);
+ expect(isPatchCommand("git -C repo apply x.diff")).toBe(true);
+ expect(isPatchCommand("git --git-dir .git apply x.diff")).toBe(true);
+ expect(isPatchCommand("git --work-tree=/tmp apply x.diff")).toBe(true);
+ expect(isPatchCommand("cd x && git apply p.diff")).toBe(true);
+ expect(isPatchCommand("/usr/bin/git apply p.diff")).toBe(true);
+ expect(isPatchCommand("env git apply p.diff")).toBe(true);
+ expect(isPatchCommand("FOO=1 git apply p.diff")).toBe(true);
+ expect(isPatchCommand("git status\ngit apply x.diff")).toBe(true);
+ expect(isPatchCommand("git apply")).toBe(true);
+ expect(isPatchCommand("apply_patch <<'EOF'\n*** Begin Patch\nEOF")).toBe(true);
+ expect(isPatchCommand("env apply_patch <<'EOF'\nEOF")).toBe(true);
+ expect(isPatchCommand("/opt/tools/apply_patch < p.diff")).toBe(true);
+ expect(isPatchCommand("patch -p1 < p.diff")).toBe(true);
+ expect(isPatchCommand("command patch -p1 < p.diff")).toBe(true);
+ expect(isPatchCommand("cd x && patch -p1 < p.diff")).toBe(true);
+ });
+});
diff --git a/packages/agent-runtime/src/mutation-recovery.ts b/packages/agent-runtime/src/mutation-recovery.ts
index 9c51d5cf6..0ba8be0e4 100644
--- a/packages/agent-runtime/src/mutation-recovery.ts
+++ b/packages/agent-runtime/src/mutation-recovery.ts
@@ -57,17 +57,67 @@ export async function mutationFailureKey(path: string, projectPath?: string): Pr
}
}
+/**
+ * Shell operators that begin another command. Every check runs on a segment
+ * after splitting on them: tokens behind a pipe, `&&`, `;`, or a newline
+ * belong to a different process, so `git log | grep apply` is not a patch and
+ * `kubectl apply` after `git pull` is not a git patch (#1985).
+ */
+const COMMAND_SEGMENTS = /(?:\|\||&&|[|;&])|\n/;
+
+/**
+ * Global git flags that consume the following token as their value; every
+ * other flag stands alone. This keeps `git -C repo apply` a patch while
+ * `git grep apply` (whose subcommand is `grep`) stays a search.
+ */
+const GIT_FLAGS_WITH_VALUE = new Set([
+ "-C",
+ "-c",
+ "--git-dir",
+ "--work-tree",
+ "--namespace",
+ "--super-prefix",
+]);
+
+/**
+ * Whether one command segment invokes `git apply`: `git` at the command
+ * position (optionally after a path prefix, an `env`/`command` wrapper, or
+ * leading `VAR=value` assignments), only git's own global flags between it
+ * and the subcommand, and `apply` as that subcommand.
+ */
+function segmentInvokesGitApply(segment: string): boolean {
+ const tokens = segment
+ .replace(/^\s*[({]+/, "")
+ .replace(/[)}\s]+$/, "")
+ .split(/\s+/)
+ .filter(Boolean);
+ while (
+ tokens.length > 1 &&
+ (/^(?:env|command)$/.test(tokens[0]) || /^[A-Za-z_][A-Za-z0-9_]*=/.test(tokens[0]))
+ ) {
+ tokens.shift();
+ }
+ if (!/^(?:\S+\/)?git$/.test(tokens[0] ?? "")) return false;
+ let index = 1;
+ while (index < tokens.length && tokens[index].startsWith("-")) {
+ if (GIT_FLAGS_WITH_VALUE.has(tokens[index])) index += 1;
+ index += 1;
+ }
+ return tokens[index] === "apply";
+}
+
export function isPatchCommand(command: unknown): boolean {
if (typeof command !== "string") return false;
- return (
- /(?:^|[;&|]\s*)(?:env\s+|command\s+)?(?:\S+\/)?apply_patch(?:\s|$)/m.test(
- command,
- ) ||
- /\bgit(?:\s+\S+)*\s+apply(?:\s|$)/m.test(command) ||
- /(?:^|[;&|]\s*)(?:env\s+|command\s+)?(?:\S+\/)?patch(?:\s|$)/m.test(
- command,
- )
- );
+ return command.split(COMMAND_SEGMENTS).some((segment) => {
+ const head = segment.replace(/^\s+/, "");
+ if (
+ /^(?:env\s+|command\s+)?(?:\S+\/)?apply_patch(?:\s|$)/.test(head) ||
+ /^(?:env\s+|command\s+)?(?:\S+\/)?patch(?:\s|$)/.test(head)
+ ) {
+ return true;
+ }
+ return segmentInvokesGitApply(head);
+ });
}
export function mutationTerminationMessage(