Skip to content

Open source CapCut alternative.

TypeScript

feat(app): structural feedback loop — escalate an unfinished action to vision

David Lef2fa40db7cef
diff --git a/apps/Donkey/Sources/Donkey/GeminiLiveVoiceController.swift b/apps/Donkey/Sources/Donkey/GeminiLiveVoiceController.swift
index 2406a96b..12bd7901 100644
--- a/apps/Donkey/Sources/Donkey/GeminiLiveVoiceController.swift
+++ b/apps/Donkey/Sources/Donkey/GeminiLiveVoiceController.swift
@@ -236,6 +236,23 @@ final class GeminiLiveVoiceController {
holdForConsent(call: toolCall, liveCall: call, session: session, result: result)
continue
}
+ // Structural feedback loop: a tool that attempted an action but couldn't confirm it can
+ // signal escalation with `escalate.app`/`escalate.goal` (e.g. the music script searched
+ // but didn't start playback). Rather than trusting the fast model to retry, the controller
+ // itself hands off to the vision observe-act loop, which sees the screen and finishes the
+ // task. This makes the loop part of the system, not the prompt.
+ if let escalateApp = result.metadata["escalate.app"]?
+ .trimmingCharacters(in: .whitespacesAndNewlines), !escalateApp.isEmpty {
+ let escalateGoal = result.metadata["escalate.goal"]?
+ .trimmingCharacters(in: .whitespacesAndNewlines)
+ onActed?("Adjusting — having the vision agent finish \"\(toolName)\" in \(escalateApp).")
+ launchVision(AIRealtimeToolCall(
+ id: call.id,
+ name: Self.visionControlToolName,
+ arguments: ["app": escalateApp, "goal": (escalateGoal?.isEmpty == false ? escalateGoal! : result.summary)]
+ ))
+ continue
+ }
onActed?(result.summary)
// Forward the full execution detail (stdout/stderr/exitCode/reason,
// plus tool-specific facts) so the model can read output and
diff --git a/apps/Donkey/Sources/DonkeyAI/DonkeyPrompts.swift b/apps/Donkey/Sources/DonkeyAI/DonkeyPrompts.swift
index 37d77a0c..7e6adad9 100644
--- a/apps/Donkey/Sources/DonkeyAI/DonkeyPrompts.swift
+++ b/apps/Donkey/Sources/DonkeyAI/DonkeyPrompts.swift
@@ -65,9 +65,13 @@ public enum DonkeyPrompts {
vision_control with the app and goal; a vision agent will operate the screen. For \
multi-step work the fast tools can't finish, call agent_run with the goal; the \
desktop agent reports to the user itself. Discover before guessing rather than \
- inventing names or values. Read each tool's returned output and retry or adjust on \
- failure; if an approach errors, do not repeat the same command — switch to \
- AppleScript or app_skill. Always end your turn by telling the user the answer or \
+ inventing names or values. Run a real feedback loop on EVERY task, whatever skill \
+ it uses: read each tool's output, and when an approach fails or you can't confirm \
+ it worked, ADJUST and try another — broaden the query, switch to AppleScript, or if \
+ a step left the app in a partial state (e.g. search results on screen) call \
+ vision_control to look and finish it; for multi-step work the fast tools can't \
+ complete, call agent_run. Never repeat the same failing command, and only ask the \
+ user once you have genuinely exhausted these paths. Always end your turn by telling the user the answer or \
what you did, concretely and briefly — e.g. name the files you found, confirm the \
app you opened.
"""
diff --git a/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift b/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift
index 249cb614..b4bdc995 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift
+++ b/apps/Donkey/Sources/DonkeyRuntime/DonkeyCommandBackends.swift
@@ -263,7 +263,18 @@ public enum DonkeyCommandBackends {
)
}
guard outcome.succeeded else {
- return failed(context, outcome.summary, reason: outcome.metadata["failureReason"] ?? "skillScriptFailed")
+ // Preserve the script's full output metadata on failure — it may carry an
+ // `escalate.app`/`escalate.goal` signal that the runtime's structural feedback loop acts
+ // on (hand the unfinished task to the vision agent) instead of dead-ending.
+ return HarnessToolResult(
+ callID: context.call.id,
+ toolName: context.call.name,
+ status: .failed,
+ summary: outcome.summary,
+ metadata: outcome.metadata.merging([
+ "reason": outcome.metadata["failureReason"] ?? "skillScriptFailed"
+ ]) { current, _ in current }
+ )
}
return success(
context,
diff --git a/apps/Donkey/Sources/DonkeyRuntime/LocalAppUserQueryHarnessServices.swift b/apps/Donkey/Sources/DonkeyRuntime/LocalAppUserQueryHarnessServices.swift
index 9e28557f..732abc0b 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/LocalAppUserQueryHarnessServices.swift
+++ b/apps/Donkey/Sources/DonkeyRuntime/LocalAppUserQueryHarnessServices.swift
@@ -95,7 +95,13 @@ public enum LocalAppUserQueryHarnessServices {
let output = result.metadata["appleScript.output"] ?? ""
let outputMetadata = structuredOutputMetadata(output)
let clarificationRequired = outputMetadata["clarification.required"] == "true"
- let succeeded = result.executed && !clarificationRequired
+ // Honor a status the script reports about its own outcome: a script can run cleanly (executed)
+ // yet report `status=not_found`/`status=failed` because the real-world effect didn't happen
+ // (e.g. playback never started). Treating that as success is a false positive — the agent
+ // would claim it did something it didn't.
+ let scriptStatus = outputMetadata["status"]
+ let statusReportsFailure = scriptStatus == "not_found" || scriptStatus == "failed"
+ let succeeded = result.executed && !clarificationRequired && !statusReportsFailure
return HarnessScriptExecutionOutcome(
succeeded: succeeded,
summary: succeeded
diff --git a/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/SKILL.md b/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/SKILL.md
index 97789c7c..1d433945 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/SKILL.md
+++ b/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/SKILL.md
@@ -20,7 +20,9 @@ Choosing the query is what matters:
Read the script's returned status:
- `status=played` → playback started; tell the user what's playing (`playedTitle`/`playedArtist`).
-- `status=not_found` with `clarification.required=true` → only then ask the included question (e.g. the term isn't in the library and streaming didn't start).
+- `status=not_found` → do NOT give up or ask yet. The script already searched, so Apple Music is showing results for the query on screen — finish the job: call `vision_control` with app "Music" and a goal like "play the top song result for <query>", and the vision agent will click a song. This is the feedback loop: an approach failed, so adjust and try another, don't stop.
+
+Only ask the user when you have genuinely exhausted the local play, the script, AND vision — and even then, play a reasonable default by the same artist rather than asking which exact song.
Verify with the script's own status fields or `osascript -e 'tell application "Music" to player state'`; don't use screenshots.
diff --git a/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/scripts/play-media-by-search.applescript b/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/scripts/play-media-by-search.applescript
index 45d0d8e1..5b825f11 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/scripts/play-media-by-search.applescript
+++ b/apps/Donkey/Sources/DonkeyRuntime/Resources/BuiltInSkills/music/scripts/play-media-by-search.applescript
@@ -58,4 +58,8 @@ tell application id "com.apple.Music"
end if
end tell
-return "status=not_found" & linefeed & "query=" & trimmedQuery & linefeed & "clarification.required=true" & linefeed & "clarification.question=I couldn't find " & trimmedQuery & " to play. What would you like me to play?"
+-- The library had no match and the streaming keystrokes didn't confirm playback, but the search
+-- results ARE on screen now. Don't dead-end: signal the runtime to escalate to the vision agent,
+-- which sees the results and clicks a song. This is the structural feedback loop — the system
+-- adjusts, it doesn't give up.
+return "status=not_found" & linefeed & "query=" & trimmedQuery & linefeed & "escalate.app=Music" & linefeed & "escalate.goal=Play the top song result for " & trimmedQuery & " from the search results on screen"