Merge pull request #3965 from code-yeongyu/fix/ralph-loop-prompt-result
fix(ralph-loop): return prompt dispatch failures
This commit is contained in:
@@ -10,6 +10,16 @@ type LoopStateController = {
|
|||||||
markVerificationPending: (sessionID: string) => RalphLoopState | null
|
markVerificationPending: (sessionID: string) => RalphLoopState | null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function showToastBestEffort(
|
||||||
|
ctx: PluginInput,
|
||||||
|
body: { title: string; message: string; variant: "error" | "info" | "success"; duration: number },
|
||||||
|
): void {
|
||||||
|
try {
|
||||||
|
void Promise.resolve(ctx.client.tui?.showToast?.({ body })).catch(() => {})
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function handleDetectedCompletion(
|
export async function handleDetectedCompletion(
|
||||||
ctx: PluginInput,
|
ctx: PluginInput,
|
||||||
input: {
|
input: {
|
||||||
@@ -35,21 +45,33 @@ export async function handleDetectedCompletion(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
await injectContinuationPrompt(ctx, {
|
const promptResult = await injectContinuationPrompt(ctx, {
|
||||||
sessionID,
|
sessionID,
|
||||||
prompt: buildContinuationPrompt(verificationState),
|
prompt: buildContinuationPrompt(verificationState),
|
||||||
directory,
|
directory,
|
||||||
apiTimeoutMs,
|
apiTimeoutMs,
|
||||||
})
|
})
|
||||||
|
if (promptResult.status === "rejected") {
|
||||||
await ctx.client.tui?.showToast?.({
|
log(`[${HOOK_NAME}] Failed to inject ultrawork verification prompt`, {
|
||||||
body: {
|
sessionID,
|
||||||
title: "ULTRAWORK LOOP",
|
error: String(promptResult.error),
|
||||||
message: "DONE detected. Oracle verification is now required.",
|
})
|
||||||
variant: "info",
|
loopState.clear()
|
||||||
|
showToastBestEffort(ctx, {
|
||||||
|
title: "Ralph Loop Failed",
|
||||||
|
message: `Verification dispatch rejected: ${String(promptResult.error)}`,
|
||||||
|
variant: "error",
|
||||||
duration: 5000,
|
duration: 5000,
|
||||||
},
|
})
|
||||||
}).catch(() => {})
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
showToastBestEffort(ctx, {
|
||||||
|
title: "ULTRAWORK LOOP",
|
||||||
|
message: "DONE detected. Oracle verification is now required.",
|
||||||
|
variant: "info",
|
||||||
|
duration: 5000,
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +81,5 @@ export async function handleDetectedCompletion(
|
|||||||
const message = state.ultrawork
|
const message = state.ultrawork
|
||||||
? `JUST ULW ULW! Task completed after ${state.iteration} iteration(s)`
|
? `JUST ULW ULW! Task completed after ${state.iteration} iteration(s)`
|
||||||
: `Task completed after ${state.iteration} iteration(s)`
|
: `Task completed after ${state.iteration} iteration(s)`
|
||||||
await ctx.client.tui?.showToast?.({
|
showToastBestEffort(ctx, { title, message, variant: "success", duration: 5000 })
|
||||||
body: { title, message, variant: "success", duration: 5000 },
|
|
||||||
}).catch(() => {})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,63 @@ import { describe, expect, test } from "bun:test"
|
|||||||
import { injectContinuationPrompt } from "./continuation-prompt-injector"
|
import { injectContinuationPrompt } from "./continuation-prompt-injector"
|
||||||
|
|
||||||
describe("ralph-loop continuation prompt injector", () => {
|
describe("ralph-loop continuation prompt injector", () => {
|
||||||
|
test("#given promptAsync resolves SDK error #when injecting continuation prompt #then it returns rejection without throwing", async () => {
|
||||||
|
// given
|
||||||
|
const ctx = {
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
messages: async () => ({ data: [] }),
|
||||||
|
promptAsync: async () => ({
|
||||||
|
error: { message: "prompt rejected by OpenCode" },
|
||||||
|
response: { status: 400 },
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// when
|
||||||
|
const result = await injectContinuationPrompt(ctx as never, {
|
||||||
|
sessionID: "ses_rejected_fields_response",
|
||||||
|
prompt: "continue",
|
||||||
|
directory: "/tmp/test",
|
||||||
|
apiTimeoutMs: 50,
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(result.status).toBe("rejected")
|
||||||
|
if (result.status === "rejected") {
|
||||||
|
expect(String(result.error)).toContain("prompt rejected by OpenCode")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test("#given promptAsync rejects #when injecting continuation prompt #then it returns rejection without throwing", async () => {
|
||||||
|
// given
|
||||||
|
const ctx = {
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
messages: async () => ({ data: [] }),
|
||||||
|
promptAsync: async () => {
|
||||||
|
throw new Error("network rejected promptAsync")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// when
|
||||||
|
const result = await injectContinuationPrompt(ctx as never, {
|
||||||
|
sessionID: "ses_rejected_promise",
|
||||||
|
prompt: "continue",
|
||||||
|
directory: "/tmp/test",
|
||||||
|
apiTimeoutMs: 50,
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(result.status).toBe("rejected")
|
||||||
|
if (result.status === "rejected") {
|
||||||
|
expect(String(result.error)).toContain("network rejected promptAsync")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
test("#given inherited message agent has ZWSP prefix #when injecting continuation prompt #then promptAsync receives normalized agent", async () => {
|
test("#given inherited message agent has ZWSP prefix #when injecting continuation prompt #then promptAsync receives normalized agent", async () => {
|
||||||
// given
|
// given
|
||||||
let promptBody: { agent?: string } | undefined
|
let promptBody: { agent?: string } | undefined
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ type MessageInfo = {
|
|||||||
tools?: Record<string, boolean | "allow" | "deny" | "ask">
|
tools?: Record<string, boolean | "allow" | "deny" | "ask">
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ContinuationPromptResult =
|
||||||
|
| { status: "dispatched" }
|
||||||
|
| { status: "rejected"; error: Error }
|
||||||
|
|
||||||
function extractPromptAsyncError(response: unknown): unknown | undefined {
|
function extractPromptAsyncError(response: unknown): unknown | undefined {
|
||||||
if (!isRecord(response) || !Object.hasOwn(response, "error")) {
|
if (!isRecord(response) || !Object.hasOwn(response, "error")) {
|
||||||
return undefined
|
return undefined
|
||||||
@@ -50,6 +54,10 @@ function describePromptAsyncError(error: unknown): string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createPromptAsyncError(prefix: string, error: unknown): Error {
|
||||||
|
return new Error(`${prefix}: ${describePromptAsyncError(error)}`)
|
||||||
|
}
|
||||||
|
|
||||||
export async function injectContinuationPrompt(
|
export async function injectContinuationPrompt(
|
||||||
ctx: PluginInput,
|
ctx: PluginInput,
|
||||||
options: {
|
options: {
|
||||||
@@ -59,7 +67,7 @@ export async function injectContinuationPrompt(
|
|||||||
apiTimeoutMs: number
|
apiTimeoutMs: number
|
||||||
inheritFromSessionID?: string
|
inheritFromSessionID?: string
|
||||||
},
|
},
|
||||||
): Promise<void> {
|
): Promise<ContinuationPromptResult> {
|
||||||
let agent: string | undefined
|
let agent: string | undefined
|
||||||
let model: { providerID: string; modelID: string; variant?: string } | undefined
|
let model: { providerID: string; modelID: string; variant?: string } | undefined
|
||||||
let tools: Record<string, boolean | "allow" | "deny" | "ask"> | undefined
|
let tools: Record<string, boolean | "allow" | "deny" | "ask"> | undefined
|
||||||
@@ -109,21 +117,39 @@ export async function injectContinuationPrompt(
|
|||||||
: undefined
|
: undefined
|
||||||
const launchVariant = model?.variant
|
const launchVariant = model?.variant
|
||||||
|
|
||||||
const response = await ctx.client.session.promptAsync({
|
let response: unknown
|
||||||
path: { id: options.sessionID },
|
try {
|
||||||
body: {
|
response = await ctx.client.session.promptAsync({
|
||||||
...(cleanAgent !== undefined ? { agent: cleanAgent } : {}),
|
path: { id: options.sessionID },
|
||||||
...(launchModel ? { model: launchModel } : {}),
|
body: {
|
||||||
...(launchVariant ? { variant: launchVariant } : {}),
|
...(cleanAgent !== undefined ? { agent: cleanAgent } : {}),
|
||||||
...(inheritedTools ? { tools: inheritedTools } : {}),
|
...(launchModel ? { model: launchModel } : {}),
|
||||||
parts: [createInternalAgentTextPart(options.prompt)],
|
...(launchVariant ? { variant: launchVariant } : {}),
|
||||||
},
|
...(inheritedTools ? { tools: inheritedTools } : {}),
|
||||||
query: { directory: options.directory },
|
parts: [createInternalAgentTextPart(options.prompt)],
|
||||||
})
|
},
|
||||||
|
query: { directory: options.directory },
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
const promptError = error instanceof Error
|
||||||
|
? error
|
||||||
|
: createPromptAsyncError("promptAsync rejected", error)
|
||||||
|
log("[ralph-loop] continuation prompt rejected", {
|
||||||
|
sessionID: options.sessionID,
|
||||||
|
error: String(promptError),
|
||||||
|
})
|
||||||
|
return { status: "rejected", error: promptError }
|
||||||
|
}
|
||||||
const promptError = extractPromptAsyncError(response)
|
const promptError = extractPromptAsyncError(response)
|
||||||
if (promptError !== undefined) {
|
if (promptError !== undefined) {
|
||||||
throw new Error(`promptAsync returned error: ${describePromptAsyncError(promptError)}`)
|
const error = createPromptAsyncError("promptAsync returned error", promptError)
|
||||||
|
log("[ralph-loop] continuation prompt rejected", {
|
||||||
|
sessionID: options.sessionID,
|
||||||
|
error: String(error),
|
||||||
|
})
|
||||||
|
return { status: "rejected", error }
|
||||||
}
|
}
|
||||||
|
|
||||||
log("[ralph-loop] continuation injected", { sessionID: options.sessionID })
|
log("[ralph-loop] continuation injected", { sessionID: options.sessionID })
|
||||||
|
return { status: "dispatched" }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { tmpdir } from "node:os"
|
|||||||
import { join } from "node:path"
|
import { join } from "node:path"
|
||||||
import { createRalphLoopHook } from "./index"
|
import { createRalphLoopHook } from "./index"
|
||||||
import { ULTRAWORK_VERIFICATION_PROMISE } from "./constants"
|
import { ULTRAWORK_VERIFICATION_PROMISE } from "./constants"
|
||||||
|
import { handleDetectedCompletion } from "./completion-handler"
|
||||||
import { clearState, writeState } from "./storage"
|
import { clearState, writeState } from "./storage"
|
||||||
import { handleFailedVerification } from "./verification-failure-handler"
|
import { handleFailedVerification } from "./verification-failure-handler"
|
||||||
|
|
||||||
@@ -483,6 +484,75 @@ describe("ralph-loop dispatch failure invariants", () => {
|
|||||||
).toBe(true)
|
).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test("#given ultrawork completion path #when verification prompt resolves SDK error #then oracle-required toast is not shown", async () => {
|
||||||
|
// given
|
||||||
|
let cleared = false
|
||||||
|
const loopState = {
|
||||||
|
clear: () => {
|
||||||
|
cleared = true
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
markVerificationPending: (sessionID: string) => ({
|
||||||
|
active: true,
|
||||||
|
iteration: 2,
|
||||||
|
prompt: "Build API",
|
||||||
|
started_at: new Date().toISOString(),
|
||||||
|
session_id: sessionID,
|
||||||
|
completion_promise: ULTRAWORK_VERIFICATION_PROMISE,
|
||||||
|
verification_pending: true,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
await handleDetectedCompletion({
|
||||||
|
directory: testDirectory,
|
||||||
|
project: testDirectory,
|
||||||
|
worktree: testDirectory,
|
||||||
|
serverUrl: "http://localhost:4096",
|
||||||
|
$: async () => ({}),
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
messages: async () => ({ data: [] }),
|
||||||
|
promptAsync: async () => ({
|
||||||
|
error: { message: "verification prompt rejected by OpenCode" },
|
||||||
|
response: { status: 400 },
|
||||||
|
}),
|
||||||
|
abort: async () => ({}),
|
||||||
|
},
|
||||||
|
tui: {
|
||||||
|
showToast: (options: { body: { title: string; message: string; variant: string } }) => {
|
||||||
|
toastCalls.push(options.body)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as never, {
|
||||||
|
sessionID: "session-123",
|
||||||
|
state: {
|
||||||
|
active: true,
|
||||||
|
iteration: 2,
|
||||||
|
prompt: "Build API",
|
||||||
|
started_at: new Date().toISOString(),
|
||||||
|
session_id: "session-123",
|
||||||
|
completion_promise: "DONE",
|
||||||
|
ultrawork: true,
|
||||||
|
},
|
||||||
|
loopState,
|
||||||
|
directory: testDirectory,
|
||||||
|
apiTimeoutMs: 5000,
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(cleared).toBe(true)
|
||||||
|
expect(toastCalls.some((toast) => toast.title === "ULTRAWORK LOOP")).toBe(false)
|
||||||
|
expect(
|
||||||
|
toastCalls.some(
|
||||||
|
(toast) =>
|
||||||
|
toast.title === "Ralph Loop Failed"
|
||||||
|
&& toast.message.includes("verification prompt rejected by OpenCode"),
|
||||||
|
),
|
||||||
|
).toBe(true)
|
||||||
|
expect(toastCalls.some((toast) => toast.title === "Ralph Loop Failed" && toast.variant === "error")).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
test("#given reset strategy #when session.create throws #then dispatch failure surfaces", async () => {
|
test("#given reset strategy #when session.create throws #then dispatch failure surfaces", async () => {
|
||||||
// given
|
// given
|
||||||
const hook = createRalphLoopHook({
|
const hook = createRalphLoopHook({
|
||||||
|
|||||||
@@ -39,13 +39,16 @@ export async function continueIteration(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await injectContinuationPrompt(ctx, {
|
const promptResult = await injectContinuationPrompt(ctx, {
|
||||||
sessionID: newSessionID,
|
sessionID: newSessionID,
|
||||||
inheritFromSessionID: options.previousSessionID,
|
inheritFromSessionID: options.previousSessionID,
|
||||||
prompt: continuationPrompt,
|
prompt: continuationPrompt,
|
||||||
directory: options.directory,
|
directory: options.directory,
|
||||||
apiTimeoutMs: options.apiTimeoutMs,
|
apiTimeoutMs: options.apiTimeoutMs,
|
||||||
})
|
})
|
||||||
|
if (promptResult.status === "rejected") {
|
||||||
|
return { status: "dispatch_rejected", error: promptResult.error }
|
||||||
|
}
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
return { status: "dispatch_rejected", error }
|
return { status: "dispatch_rejected", error }
|
||||||
}
|
}
|
||||||
@@ -65,12 +68,15 @@ export async function continueIteration(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await injectContinuationPrompt(ctx, {
|
const promptResult = await injectContinuationPrompt(ctx, {
|
||||||
sessionID: options.previousSessionID,
|
sessionID: options.previousSessionID,
|
||||||
prompt: continuationPrompt,
|
prompt: continuationPrompt,
|
||||||
directory: options.directory,
|
directory: options.directory,
|
||||||
apiTimeoutMs: options.apiTimeoutMs,
|
apiTimeoutMs: options.apiTimeoutMs,
|
||||||
})
|
})
|
||||||
|
if (promptResult.status === "rejected") {
|
||||||
|
return { status: "dispatch_rejected", error: promptResult.error }
|
||||||
|
}
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
return { status: "dispatch_rejected", error }
|
return { status: "dispatch_rejected", error }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,12 +98,26 @@ export async function handleFailedVerification(
|
|||||||
const previewState: RalphLoopState = { ...clearedState, iteration: clearedState.iteration + 1 }
|
const previewState: RalphLoopState = { ...clearedState, iteration: clearedState.iteration + 1 }
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await injectContinuationPrompt(ctx, {
|
const promptResult = await injectContinuationPrompt(ctx, {
|
||||||
sessionID: parentSessionID,
|
sessionID: parentSessionID,
|
||||||
prompt: buildVerificationFailurePrompt(previewState),
|
prompt: buildVerificationFailurePrompt(previewState),
|
||||||
directory,
|
directory,
|
||||||
apiTimeoutMs,
|
apiTimeoutMs,
|
||||||
})
|
})
|
||||||
|
if (promptResult.status === "rejected") {
|
||||||
|
log(`[${HOOK_NAME}] Failed to inject verification failure prompt`, {
|
||||||
|
parentSessionID,
|
||||||
|
error: String(promptResult.error),
|
||||||
|
})
|
||||||
|
loopState.clear()
|
||||||
|
showToastBestEffort(ctx, {
|
||||||
|
title: "Ralph Loop Failed",
|
||||||
|
message: `Verification continuation rejected: ${String(promptResult.error)}`,
|
||||||
|
variant: "warning",
|
||||||
|
duration: 5000,
|
||||||
|
})
|
||||||
|
return false
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log(`[${HOOK_NAME}] Failed to inject verification failure prompt`, {
|
log(`[${HOOK_NAME}] Failed to inject verification failure prompt`, {
|
||||||
parentSessionID,
|
parentSessionID,
|
||||||
|
|||||||
Reference in New Issue
Block a user