Skip to content

Open source CapCut alternative.

TypeScript

fix(app): run cd-scoped workspace cleanup chains unprompted

David Le7f7e52745b4c
diff --git a/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift b/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift
index ac1dfd7f..af8943a3 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift
+++ b/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift
@@ -78,6 +78,12 @@ public enum ShellCommandClassifier {
workspaceFileToolSignatures.contains(signature)
}
+ /// Pure directory-navigation builtins. They change only the shell's working directory and touch no file,
+ /// so they ride an owned-folder mutation chain as reads — `everySegmentMutatesOnlyOwnedRoots` skips them
+ /// and advances the directory later relative operands resolve against. (They are also in `readExecutables`
+ /// so a read-only `cd <dir> && <read>` chain classifies as a read on its own.)
+ static let navigationBuiltins: Set<String> = ["cd", "pushd", "popd"]
+
/// The scripting interpreters whose effect is the opaque code they run, not their argv — so operand
/// analysis can't clear them the way it clears a bounded mutator. The kernel jail can: confined to the
/// owned folders, an interpreter can only WRITE inside the workspace (reads and the network stay open,
@@ -137,19 +143,25 @@ public enum ShellCommandClassifier {
return sawInterpreter
}
- /// Whether EVERY command in the line is a bare bounded file mutator (`mv`, `rm`, `mkdir`, `chmod`, …)
- /// AND every path operand it names resolves inside one of `ownedRoots` — the folders Donkey created.
- /// Such a line runs WITHOUT a consent prompt: it only creates, moves, or deletes files, and only inside
- /// Donkey's own folder, so there is nothing to ask about. The moment an operand is an absolute path
- /// outside those roots, a `~` path, an unresolved `$VAR`/substitution, or a `..` climb that escapes, the
- /// line falls back to the normal prompt — a mutation of a file the user owns is the user's to approve.
+ /// Whether the line only mutates files inside the folders Donkey created (`ownedRoots`) — so it runs
+ /// WITHOUT a consent prompt. It qualifies when every segment is either a directory-navigation builtin
+ /// (`cd`/`pushd`/`popd`), a pure read (`echo`, a read subcommand, a bundled tool), or a bare bounded file
+ /// mutator (`mv`, `rm`, `mkdir`, `chmod`, …) whose every path operand resolves inside an owned root — and
+ /// at least one segment is a mutator (a read-only line is already handled by the read path). The moment a
+ /// mutator operand is an absolute path outside those roots, a `~` path, an unresolved `$VAR`/substitution,
+ /// or a `..` climb that escapes, the line falls back to the normal prompt — a mutation of a file the user
+ /// owns is the user's to approve.
///
- /// A chain (`cp <ws>/a … && rm <ws>/tmp`, move then clean up) qualifies when each link is a bounded
- /// mutator with in-folder operands. A wrapper (`xargs rm` reads paths from stdin), an interpreter
- /// (`python3`), or any non-mutator segment (`curl`, `osascript`) disqualifies the line; command
- /// substitution surfaces its inner program as its own segment (split on `(`/backtick), so `rm $(curl …)`
- /// fails on `curl`. Argv tokens only — never natural-language intent. Operands resolve relative to
- /// `workingDirectory` (the workspace), and any resolution ambiguity errs toward prompting, never away.
+ /// A leading `cd <dir>` is the idiom the agent uses to scope its own folder (`cd <ws> && mv a b && rm c`,
+ /// move then clean up). It rides the chain as a read AND advances the directory that later relative
+ /// operands resolve against, so `cd <ws> && rm tmp` stays in-bounds while `cd /etc && rm passwd` resolves
+ /// the operand at `/etc/passwd` and prompts. A navigation whose target can't be pinned down statically
+ /// (`popd`, `cd -`, `cd $VAR`) makes the working directory unknown, after which any relative mutator
+ /// operand fails containment and the line prompts. A wrapper (`xargs rm` reads paths from stdin) or any
+ /// non-read, non-mutator segment (`curl`, `osascript`) disqualifies the line; command substitution
+ /// surfaces its inner program as its own segment (split on `(`/backtick), so `rm $(curl …)` fails on
+ /// `curl`. Argv tokens only — never natural-language intent. Any resolution ambiguity errs toward
+ /// prompting, never away.
public static func everySegmentMutatesOnlyOwnedRoots(
_ command: String,
ownedRoots: [String],
@@ -157,17 +169,42 @@ public enum ShellCommandClassifier {
) -> Bool {
let owned = ownedRoots.compactMap(canonicalDirectory)
guard !owned.isEmpty else { return false }
- let workdir = canonicalDirectory(workingDirectory) ?? (workingDirectory as NSString).expandingTildeInPath
let segments = splitSegments(command).map(tokenize).filter { !$0.isEmpty }
guard !segments.isEmpty else { return false }
+
+ // The directory later relative operands resolve against. When a navigation segment can't be pinned
+ // down, the working directory becomes this base, which sits under no owned root — so a relative
+ // operand resolved against it fails containment and the line prompts, while an absolute owned operand
+ // still resolves correctly (resolveOperandPath ignores the base for an absolute path).
+ let unresolvedBase = "/dev/null/donkey-unresolved-workdir"
+ var workdir = canonicalDirectory(workingDirectory) ?? (workingDirectory as NSString).expandingTildeInPath
+ var sawMutator = false
for tokens in segments {
- guard workspaceFileToolSignatures.contains(executableName(tokens[0])) else { return false }
+ let exe = executableName(tokens[0])
+ // Directory navigation rides the chain and moves the working directory later operands resolve
+ // against. `popd`/`cd -`/`cd $VAR` can't be resolved statically, so the directory goes unknown.
+ if navigationBuiltins.contains(exe) {
+ let operands = pathOperands(of: tokens)
+ if exe != "popd", operands.count == 1,
+ let resolved = resolveOperandPath(operands[0], workingDirectory: workdir) {
+ workdir = canonicalDirectory(resolved) ?? resolved
+ } else {
+ workdir = unresolvedBase
+ }
+ continue
+ }
+ // Any other pure read (`echo`, a read subcommand, a bundled tool) touches no file, so it rides
+ // along — symmetric with the interpreter path's read passthrough.
+ if classifySegment(tokens).tier == .read { continue }
+ // Every remaining segment must be a bounded mutator whose operands all sit in an owned root.
+ guard workspaceFileToolSignatures.contains(exe) else { return false }
for operand in pathOperands(of: tokens) {
guard let resolved = resolveOperandPath(operand, workingDirectory: workdir),
owned.contains(where: { isWithin(resolved, root: $0) }) else { return false }
}
+ sawMutator = true
}
- return true
+ return sawMutator
}
/// The path operands of a bounded-mutator segment: every token after the executable that is not an
@@ -613,9 +650,9 @@ public enum ShellCommandClassifier {
/// touch no file or state, so they are reads. This matters because `cd <dir> && <read or bundled tool>`
/// scopes a read into the agent's folder — without `cd` here it classified as an "unrecognized command"
/// (reversibleWrite) and the most-restrictive-segment rule dragged the whole chain to a consent prompt,
- /// gating even a bundled tool reworking files the agent itself created. (A file MUTATION never rides a
- /// `cd` chain unprompted — `everySegmentMutatesOnlyOwnedRoots` requires every segment to be a bounded
- /// mutator, and `cd` is not one.)
+ /// gating even a bundled tool reworking files the agent itself created. A file MUTATION also rides a `cd`
+ /// chain unprompted when it stays in an owned folder — see `everySegmentMutatesOnlyOwnedRoots`, which
+ /// skips the navigation segment and advances the working directory the later operands resolve against.
private static let readExecutables: Set<String> = [
"cd", "pushd", "popd",
"ls", "cat", "head", "tail", "grep", "egrep", "fgrep", "rg", "find", "mdfind", "mdls",
diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift
index 1626326a..2c815a6d 100644
--- a/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift
+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift
@@ -374,6 +374,51 @@ struct ShellCommandClassifierTests {
}
}
+ @Test
+ func cdIntoTheOwnedFolderLetsTheCleanupChainRunUnprompted() throws {
+ // The idiom the planner emits — `cd <workspace> && mv … && rm …`, move then clean up — must run
+ // without a prompt. The leading `cd` rides the chain as a read and anchors the later relative
+ // operands inside the owned folder. This is the exact shape that wrongly prompted on the video clip.
+ let ws = try makeOwnedRoot()
+ defer { try? FileManager.default.removeItem(atPath: ws) }
+ func unprompted(_ c: String) -> Bool {
+ ShellCommandClassifier.everySegmentMutatesOnlyOwnedRoots(c, ownedRoots: [ws], workingDirectory: ws)
+ }
+ for command in [
+ "cd \"\(ws)\" && mv output.mp4 KoreanVideoClipCozy.mp4 && rm -f test.mp4 final.mp4",
+ "cd \"\(ws)\" && rm scratch.txt",
+ "cd \"\(ws)\" && echo done && rm a.txt", // a pure read between mutators rides along
+ "pushd \"\(ws)\" && rm a.txt", // pushd <dir> advances like cd
+ "cd \"\(ws)\" && mkdir out && mv a.txt out/a.txt",
+ ] {
+ #expect(unprompted(command), "should run unprompted: \(command)")
+ }
+ }
+
+ @Test
+ func cdThatLeavesOrCannotBeResolvedKeepsTheChainPrompting() throws {
+ // The security boundary for the navigation passthrough: a `cd` out of the owned folder resolves the
+ // later relative operand at its real destination (outside) and prompts; a navigation whose target
+ // can't be pinned down statically (`popd`, `cd -`, `cd $VAR`) makes the working directory unknown, so
+ // a relative mutator after it can no longer be proven in-bounds and also prompts. A bare `cd` with no
+ // mutator declines here (the read path clears it instead).
+ let ws = try makeOwnedRoot()
+ defer { try? FileManager.default.removeItem(atPath: ws) }
+ func unprompted(_ c: String) -> Bool {
+ ShellCommandClassifier.everySegmentMutatesOnlyOwnedRoots(c, ownedRoots: [ws], workingDirectory: ws)
+ }
+ for command in [
+ "cd /etc && rm passwd", // operand resolves at /etc/passwd, outside the folder
+ "cd \"\(ws)\" && rm /etc/hosts", // absolute operand outside, despite cd into the folder
+ "popd && rm a.txt", // popd target is unknowable → relative operand unanchored
+ "cd - && rm a.txt", // `cd -` (previous dir) is unknowable
+ "cd \"$HOME\" && rm a.txt", // unresolved expansion → working directory unknown
+ "cd \"\(ws)\"", // no mutator → declines (the read path runs it)
+ ] {
+ #expect(!unprompted(command), "should prompt: \(command)")
+ }
+ }
+
/// A real, existing directory under `/private/tmp` to stand in for a Donkey-created workspace — the
/// anchor check canonicalizes owned roots with `realpath`, so the root must exist on disk.
private func makeOwnedRoot() throws -> String {