Skip to content

Open source CapCut alternative.

TypeScript

fix(harness): complete the run on a conversational reply

David Leed72ac8d0ea3
diff --git a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessCoordinator.swift b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessCoordinator.swift
index 8cd19d0f..6eedcfef 100644
--- a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessCoordinator.swift
+++ b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessCoordinator.swift
@@ -305,10 +305,9 @@ public actor HarnessAgentCoordinator {
// spiraled on the (truthful) empty read. The tool history keeps the full record for
// context; the facts re-derive from the fresh reads the completion gate already
// requires. Gate resumes (clarify/permission) continue the SAME run — their history
- // does not end in a terminal lifecycle call — and keep their facts.
- let terminalNames: Set<String> = ["run.complete", "run.failSafe", "run.cancel"]
+ // does not end in a terminal call — and keep their facts.
if let last = task.toolHistory.last,
- terminalNames.contains(last.call.name), last.resultStatus == .succeeded {
+ BuiltInHarnessToolCatalog.terminalToolNames.contains(last.call.name), last.resultStatus == .succeeded {
task.worldModel.facts = [:]
}
}
diff --git a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessRuntime.swift b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessRuntime.swift
index ce023bd9..7771d725 100644
--- a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessRuntime.swift
+++ b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessRuntime.swift
@@ -239,13 +239,12 @@ public struct GenericHarnessRuntime: Sendable {
}
/// The slice of tool history belonging to the current run: everything after the last succeeded
- /// terminal lifecycle call (run.complete / run.failSafe / run.cancel). A task resumed for a new
+ /// terminal call (see `BuiltInHarnessToolCatalog.terminalToolNames`). A task resumed for a new
/// user turn keeps its full history for context, but guards that reason about "this run" —
/// duplicate actions, repeated failures — must not match records from a run that already ended.
private static func currentRunHistory(_ history: [HarnessToolCallRecord]) -> ArraySlice<HarnessToolCallRecord> {
- let terminalNames: Set<String> = ["run.complete", "run.failSafe", "run.cancel"]
guard let lastTerminal = history.lastIndex(where: {
- terminalNames.contains($0.call.name) && $0.resultStatus == .succeeded
+ BuiltInHarnessToolCatalog.terminalToolNames.contains($0.call.name) && $0.resultStatus == .succeeded
}) else { return history[...] }
return history[history.index(after: lastTerminal)...]
}
@@ -577,6 +576,14 @@ public struct GenericHarnessRuntime: Sendable {
agentID: agentID,
reason: call.input["reason"] ?? "Task completed by lifecycle tool"
)
+ case "conversation.respond":
+ // A chat reply ends its turn: complete the run so the planner can't loop re-issuing the same
+ // answer until the stall guard fails it safe. Conversation-only turns have no state-changing
+ // step, so this bypasses the completion-evidence gate the same way a pure-read task does.
+ return await coordinator.complete(
+ agentID: agentID,
+ reason: "Task completed by conversation response"
+ )
case "run.failSafe":
return await coordinator.failSafe(
agentID: agentID,
diff --git a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessToolRegistry.swift b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessToolRegistry.swift
index 1f3c748c..367e46e3 100644
--- a/apps/Donkey/Sources/DonkeyHarness/GenericHarnessToolRegistry.swift
+++ b/apps/Donkey/Sources/DonkeyHarness/GenericHarnessToolRegistry.swift
@@ -161,6 +161,15 @@ public actor HarnessToolRegistry {
}
public enum BuiltInHarnessToolCatalog {
+ /// Tool calls whose success ends the current run, used both to map a call to task completion and to
+ /// slice "this run" out of a resumed task's history. The `run.*` lifecycle terminals, plus
+ /// `conversation.respond`: a chat reply is the terminal action of a conversational turn — the answer
+ /// is the result, with nothing left to act on or verify — so it completes the run rather than leaving
+ /// the planner to loop re-issuing the same reply until the stall guard fails it safe.
+ public static let terminalToolNames: Set<String> = [
+ "run.complete", "run.failSafe", "run.cancel", "conversation.respond"
+ ]
+
public static var descriptors: [HarnessToolDescriptor] {
[
descriptor(
diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/GenericHarnessTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/GenericHarnessTests.swift
index 8cf97b87..738f2b5d 100644
--- a/apps/Donkey/Tests/DonkeyRuntimeTests/GenericHarnessTests.swift
+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/GenericHarnessTests.swift
@@ -211,6 +211,35 @@ struct GenericHarnessTests {
#expect(finalTask?.status == .completed)
}
+ @Test
+ func aConversationResponseCompletesTheRunInsteadOfLooping() async {
+ // A chat reply is the terminal action of a conversational turn. A planner that only ever responds
+ // (never emits run.complete) must still complete cleanly after one response — not loop the
+ // identical reply until the stall guard fails it safe. Regression for a bare "hi" spiraling into
+ // three repeated "Hello!" steps and ending failedSafe.
+ struct AlwaysRespondsPlanner: HarnessNextStepPlanning {
+ func planNextStep(for task: HarnessAgentState, rollingContext: String?) async -> HarnessToolCall? {
+ HarnessToolCall(name: "conversation.respond", input: ["response": "Hello! How can I assist you today?"])
+ }
+ }
+
+ let coordinator = HarnessAgentCoordinator()
+ let registry = BuiltInHarnessToolCatalog.registryWithBuiltInExecutors()
+ let runtime = GenericHarnessRuntime(coordinator: coordinator, registry: registry)
+ let task = await coordinator.createAgent(
+ id: "task-respond",
+ conversationID: "thread-respond",
+ goal: "respond to a greeting",
+ grantedPermissions: [.conversation, .lifecycle]
+ )
+
+ let steps = await runtime.run(agentID: task.id, planner: AlwaysRespondsPlanner(), maxSteps: 5)
+
+ #expect(steps.map { $0.toolResult?.toolName } == ["conversation.respond"])
+ let finalTask = await coordinator.agent(id: task.id)
+ #expect(finalTask?.status == .completed)
+ }
+
@Test
func compactorRollingContextReachesThePlanner() async {
// The runtime must hand each step's planner the rolling context the compactor produced — the