Skip to content

Open source CapCut alternative.

TypeScript

fix(app): resolve shell_exec PATH from the user's login shell

David Lee83d604dde0c
diff --git a/apps/Donkey/Sources/DonkeyAI/DonkeyPrompts.swift b/apps/Donkey/Sources/DonkeyAI/DonkeyPrompts.swift
index dd96d9d0..5eb80dd3 100644
--- a/apps/Donkey/Sources/DonkeyAI/DonkeyPrompts.swift
+++ b/apps/Donkey/Sources/DonkeyAI/DonkeyPrompts.swift
@@ -373,6 +373,8 @@ public enum DonkeyPrompts {
Python packages and not optional: never invoke one through `python3 -m …` or `pip`, never
run `which`/`--version`/any check to confirm it exists, and never probe first — just use it
directly (e.g. `yt-dlp -P ~/Downloads 'URL'`, then `ffmpeg -i in.mp4 …`). Single-quote any URL.
+ A download, transcode, or network call can run far past the default timeout, so pass a generous
+ `timeoutSeconds` (up to 120) on the shell_exec call or it is killed mid-run.
- Discover what ELSE is available by DOING, not by guessing or pre-checking. Run the command you
need by bare name; don't probe whether a tool exists first. If it fails with `command not
found`, adapt: reach for another tool that does the job, or — if the task genuinely needs
diff --git a/apps/Donkey/Sources/DonkeyRuntime/BundledTools.swift b/apps/Donkey/Sources/DonkeyRuntime/BundledTools.swift
index 065d77e9..12e2c9f1 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/BundledTools.swift
+++ b/apps/Donkey/Sources/DonkeyRuntime/BundledTools.swift
@@ -9,7 +9,7 @@ import Foundation
public enum BundledTools {
/// Bare executable names of the tools shipped in the bundle — the mandatory media set from
/// scripts/publish-bundled-tools.sh plus the document tools fetched in fetch-bundled-tools.sh. Donkey
- /// resolves these from `installDirectory`, which `shellEnvironment()` puts first on PATH, so capability
+ /// resolves these from `installDirectory`, which `shellEnvironment()` puts on PATH, so capability
/// skills run them by bare name. This is the single Swift home for the list; keep it in step with the
/// fetch/publish scripts when the bundle gains or drops a tool.
public static let executableNames: Set<String> = [
diff --git a/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift b/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift
index 8f662511..a41b721f 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift
+++ b/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift
@@ -700,20 +700,81 @@ public enum DonkeyCommandBackends {
return nil
}
- /// The environment for a `shell_exec` child: the app's environment with the bundled
- /// tools directory appended to PATH. Appended, not prepended, so a tool the user has
- /// installed (e.g. a newer Homebrew `yt-dlp`) still wins, while the bundled copy is the
- /// guaranteed fallback that keeps a capability working with nothing installed. Returns
- /// `nil` when there is no bundled tools directory, so `Process` inherits the environment
- /// exactly as before.
- static func shellEnvironment() -> [String: String]? {
- guard let toolsDirectory = bundledToolsDirectory else { return nil }
+ /// The environment for a `shell_exec` child: the app's environment, but with PATH rebuilt
+ /// from the user's *login-shell* PATH plus the bundled tools directory.
+ ///
+ /// A GUI-launched app (Finder/Dock) inherits only launchd's minimal PATH —
+ /// `/usr/bin:/bin:/usr/sbin:/sbin` — which omits `/opt/homebrew/bin` and everything else the
+ /// user's profile adds. Building the child's PATH from that minimal value made every
+ /// Homebrew-installed tool (`brew`, `ffmpeg`, `yt-dlp`, …) read as `command not found`, even
+ /// though the user clearly had them in Terminal. That mismatch is what sent the planner into a
+ /// loop: it was told a tool was available, the shell disagreed, and it never reconciled the two.
+ /// Anchoring on the login-shell PATH instead means the agent sees exactly what the user sees.
+ ///
+ /// The bundled tools directory is appended (not prepended), so a tool the user installed (e.g. a
+ /// newer Homebrew `yt-dlp`) still wins, while the bundled copy is the guaranteed fallback that
+ /// keeps a capability working with nothing installed.
+ static func shellEnvironment() -> [String: String] {
var environment = ProcessInfo.processInfo.environment
- let existing = environment["PATH"] ?? "/usr/bin:/bin:/usr/sbin:/sbin"
- environment["PATH"] = existing + ":" + toolsDirectory.path
+ var path = loginShellPath
+ if let toolsDirectory = bundledToolsDirectory {
+ path += ":" + toolsDirectory.path
+ }
+ environment["PATH"] = path
return environment
}
+ /// The PATH the user's interactive shell would have, resolved once. Asking the login shell
+ /// (`$SHELL -lc …`) runs their `.zprofile`/`.zshrc`, so the result includes `/opt/homebrew/bin`,
+ /// `~/.local/bin`, language version managers, and anything else they rely on — the same PATH they
+ /// get in Terminal. Resolved lazily and cached: it is stable for the process lifetime and spawning
+ /// a login shell is not free.
+ private static let loginShellPath: String = resolveLoginShellPath()
+
+ /// A PATH that already covers both Homebrew prefixes, used when the login shell can't be queried so
+ /// the agent still degrades to a useful PATH rather than launchd's bare minimum.
+ private static let fallbackShellPath =
+ "/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
+
+ private static func resolveLoginShellPath() -> String {
+ let shell = ProcessInfo.processInfo.environment["SHELL"] ?? "/bin/zsh"
+ // Markers fence the value so output printed by the user's rc files (version-manager banners,
+ // etc.) can't contaminate the PATH we extract.
+ let start = "__DONKEY_PATH_START__"
+ let end = "__DONKEY_PATH_END__"
+ let process = Process()
+ process.executableURL = URL(fileURLWithPath: shell)
+ process.arguments = ["-lc", "printf '%s%s%s' '\(start)' \"$PATH\" '\(end)'"]
+ let pipe = Pipe()
+ process.standardOutput = pipe
+ process.standardError = FileHandle.nullDevice
+
+ let finished = DispatchSemaphore(value: 0)
+ process.terminationHandler = { _ in finished.signal() }
+ do {
+ try process.run()
+ } catch {
+ return fallbackShellPath
+ }
+ // Bound it: a login shell normally exits in well under a second, but a pathological profile
+ // must not hang the first shell_exec forever.
+ if finished.wait(timeout: .now() + 5.0) == .timedOut {
+ process.terminate()
+ return fallbackShellPath
+ }
+
+ let data = pipe.fileHandleForReading.readDataToEndOfFile()
+ guard
+ let output = String(data: data, encoding: .utf8),
+ let lower = output.range(of: start),
+ let upper = output.range(of: end)
+ else {
+ return fallbackShellPath
+ }
+ let resolved = String(output[lower.upperBound..<upper.lowerBound])
+ return resolved.isEmpty ? fallbackShellPath : resolved
+ }
+
// MARK: - Result helpers
private static func success(