Merge pull request #3939 from code-yeongyu/fix/ralph-loop-message-dispatch
fix(ralph-loop): commit iteration only after continuation is dispatched
This commit is contained in:
@@ -0,0 +1,412 @@
|
|||||||
|
/// <reference types="bun-types" />
|
||||||
|
import { afterEach, beforeEach, describe, expect, test } from "bun:test"
|
||||||
|
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"
|
||||||
|
import { tmpdir } from "node:os"
|
||||||
|
import { join } from "node:path"
|
||||||
|
import { createRalphLoopHook } from "./index"
|
||||||
|
import { ULTRAWORK_VERIFICATION_PROMISE } from "./constants"
|
||||||
|
import { clearState, writeState } from "./storage"
|
||||||
|
import { handleFailedVerification } from "./verification-failure-handler"
|
||||||
|
|
||||||
|
describe("ralph-loop dispatch failure invariants", () => {
|
||||||
|
const testDirectory = join(tmpdir(), `ralph-loop-dispatch-failure-${Date.now()}`)
|
||||||
|
let promptCalls: Array<{ sessionID: string; text: string }>
|
||||||
|
let toastCalls: Array<{ title: string; message: string; variant: string }>
|
||||||
|
let messagesCalls: Array<{ sessionID: string }>
|
||||||
|
let createSessionCalls: Array<{ parentID: string }>
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
promptCalls = []
|
||||||
|
toastCalls = []
|
||||||
|
messagesCalls = []
|
||||||
|
createSessionCalls = []
|
||||||
|
mkdirSync(testDirectory, { recursive: true })
|
||||||
|
clearState(testDirectory)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
clearState(testDirectory)
|
||||||
|
if (existsSync(testDirectory)) {
|
||||||
|
rmSync(testDirectory, { recursive: true, force: true })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test("#given idle path #when promptAsync throws #then no state or toast advance", async () => {
|
||||||
|
// given
|
||||||
|
const hook = createRalphLoopHook({
|
||||||
|
directory: testDirectory,
|
||||||
|
project: testDirectory,
|
||||||
|
worktree: testDirectory,
|
||||||
|
serverUrl: "http://localhost:4096",
|
||||||
|
$: async () => ({}),
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
messages: async (options: { path: { id: string } }) => {
|
||||||
|
messagesCalls.push({ sessionID: options.path.id })
|
||||||
|
return { data: [] }
|
||||||
|
},
|
||||||
|
promptAsync: async () => {
|
||||||
|
throw new Error("simulated dispatch failure")
|
||||||
|
},
|
||||||
|
prompt: async () => ({}),
|
||||||
|
create: async () => ({ data: { id: "new-session-id" } }),
|
||||||
|
},
|
||||||
|
tui: {
|
||||||
|
showToast: async (options: { body: { title: string; message: string; variant: string } }) => {
|
||||||
|
toastCalls.push(options.body)
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as never)
|
||||||
|
|
||||||
|
hook.startLoop("session-123", "Keep working", {
|
||||||
|
messageCountAtStart: 0,
|
||||||
|
maxIterations: 5,
|
||||||
|
})
|
||||||
|
expect(hook.getState()?.iteration).toBe(1)
|
||||||
|
|
||||||
|
// when
|
||||||
|
await hook.event({
|
||||||
|
event: { type: "session.idle", properties: { sessionID: "session-123" } },
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(toastCalls.some((toast) => toast.title === "Ralph Loop" && toast.message.includes("Iteration"))).toBe(false)
|
||||||
|
expect(hook.getState()).toBeNull()
|
||||||
|
expect(toastCalls.some((toast) => toast.title === "Ralph Loop Failed" && toast.message.includes("dispatch_rejected"))).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("#given error retry path #when promptAsync throws #then no state or toast advance", async () => {
|
||||||
|
// given
|
||||||
|
const hook = createRalphLoopHook({
|
||||||
|
directory: testDirectory,
|
||||||
|
project: testDirectory,
|
||||||
|
worktree: testDirectory,
|
||||||
|
serverUrl: "http://localhost:4096",
|
||||||
|
$: async () => ({}),
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
messages: async (options: { path: { id: string } }) => {
|
||||||
|
messagesCalls.push({ sessionID: options.path.id })
|
||||||
|
return { data: [] }
|
||||||
|
},
|
||||||
|
promptAsync: async () => {
|
||||||
|
throw new Error("simulated dispatch failure")
|
||||||
|
},
|
||||||
|
prompt: async () => ({}),
|
||||||
|
create: async () => ({ data: { id: "new-session-id" } }),
|
||||||
|
},
|
||||||
|
tui: {
|
||||||
|
showToast: async (options: { body: { title: string; message: string; variant: string } }) => {
|
||||||
|
toastCalls.push(options.body)
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as never)
|
||||||
|
|
||||||
|
hook.startLoop("session-123", "Keep working", {
|
||||||
|
messageCountAtStart: 0,
|
||||||
|
maxIterations: 5,
|
||||||
|
})
|
||||||
|
expect(hook.getState()?.iteration).toBe(1)
|
||||||
|
|
||||||
|
// when
|
||||||
|
await hook.event({
|
||||||
|
event: {
|
||||||
|
type: "session.error",
|
||||||
|
properties: {
|
||||||
|
sessionID: "session-123",
|
||||||
|
error: { name: "RuntimeError" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(toastCalls.some((toast) => toast.title === "Ralph Loop" && toast.message.includes("Iteration"))).toBe(false)
|
||||||
|
expect(hook.getState()).toBeNull()
|
||||||
|
expect(toastCalls.some((toast) => toast.title === "Ralph Loop Failed" && toast.message.includes("dispatch_rejected"))).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("#given verification-failure path #when promptAsync throws #then iteration not advanced", async () => {
|
||||||
|
// given
|
||||||
|
const parentTranscriptPath = join(testDirectory, "transcript-parent.jsonl")
|
||||||
|
const oracleTranscriptPath = join(testDirectory, "transcript-oracle.jsonl")
|
||||||
|
const hook = createRalphLoopHook({
|
||||||
|
directory: testDirectory,
|
||||||
|
project: testDirectory,
|
||||||
|
worktree: testDirectory,
|
||||||
|
serverUrl: "http://localhost:4096",
|
||||||
|
$: async () => ({}),
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
messages: async (options: { path: { id: string } }) => {
|
||||||
|
messagesCalls.push({ sessionID: options.path.id })
|
||||||
|
if (options.path.id === "session-123") {
|
||||||
|
return { data: [{}, {}, {}] }
|
||||||
|
}
|
||||||
|
return { data: [] }
|
||||||
|
},
|
||||||
|
promptAsync: async (options: { body: { parts: Array<{ type: string; text: string }> } }) => {
|
||||||
|
if (options.body.parts[0]?.text.includes("Verification failed")) {
|
||||||
|
throw new Error("simulated dispatch failure")
|
||||||
|
}
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
prompt: async () => ({}),
|
||||||
|
abort: async () => ({}),
|
||||||
|
create: async () => ({ data: { id: "new-session-id" } }),
|
||||||
|
},
|
||||||
|
tui: {
|
||||||
|
showToast: async (options: { body: { title: string; message: string; variant: string } }) => {
|
||||||
|
toastCalls.push(options.body)
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as never, {
|
||||||
|
getTranscriptPath: (sessionID): string => sessionID === "ses-oracle" ? oracleTranscriptPath : parentTranscriptPath,
|
||||||
|
})
|
||||||
|
|
||||||
|
hook.startLoop("session-123", "Build API", { ultrawork: true })
|
||||||
|
writeState(testDirectory, {
|
||||||
|
...hook.getState()!,
|
||||||
|
iteration: 2,
|
||||||
|
verification_pending: true,
|
||||||
|
verification_session_id: "ses-oracle",
|
||||||
|
completion_promise: ULTRAWORK_VERIFICATION_PROMISE,
|
||||||
|
initial_completion_promise: "DONE",
|
||||||
|
})
|
||||||
|
writeState(testDirectory, {
|
||||||
|
...hook.getState()!,
|
||||||
|
verification_session_id: "ses-oracle",
|
||||||
|
})
|
||||||
|
writeFileSync(
|
||||||
|
oracleTranscriptPath,
|
||||||
|
`${JSON.stringify({ type: "tool_result", timestamp: new Date().toISOString(), tool_output: { output: "verification failed" } })}\n`,
|
||||||
|
)
|
||||||
|
|
||||||
|
const preRestartIteration = hook.getState()?.iteration
|
||||||
|
|
||||||
|
// when
|
||||||
|
await hook.event({ event: { type: "session.idle", properties: { sessionID: "ses-oracle" } } })
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(preRestartIteration).toBe(2)
|
||||||
|
expect(hook.getState()).toBeNull()
|
||||||
|
expect(toastCalls.some((toast) => toast.title === "Ralph Loop Failed" && toast.message.includes("Verification continuation rejected"))).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("#given reset strategy #when createIterationSession returns null #then dispatch failure surfaces", async () => {
|
||||||
|
// given
|
||||||
|
const hook = createRalphLoopHook({
|
||||||
|
directory: testDirectory,
|
||||||
|
project: testDirectory,
|
||||||
|
worktree: testDirectory,
|
||||||
|
serverUrl: "http://localhost:4096",
|
||||||
|
$: async () => ({}),
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
messages: async (options: { path: { id: string } }) => {
|
||||||
|
messagesCalls.push({ sessionID: options.path.id })
|
||||||
|
return { data: [] }
|
||||||
|
},
|
||||||
|
promptAsync: async (options: { path: { id: string }; body: { parts: Array<{ type: string; text: string }> } }) => {
|
||||||
|
promptCalls.push({
|
||||||
|
sessionID: options.path.id,
|
||||||
|
text: options.body.parts[0]?.text ?? "",
|
||||||
|
})
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
prompt: async () => ({}),
|
||||||
|
create: async (options: { body: { parentID: string } }) => {
|
||||||
|
createSessionCalls.push({ parentID: options.body.parentID })
|
||||||
|
return { error: "fail", data: undefined }
|
||||||
|
},
|
||||||
|
},
|
||||||
|
tui: {
|
||||||
|
showToast: async (options: { body: { title: string; message: string; variant: string } }) => {
|
||||||
|
toastCalls.push(options.body)
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as never)
|
||||||
|
|
||||||
|
hook.startLoop("session-123", "Keep working", {
|
||||||
|
messageCountAtStart: 0,
|
||||||
|
maxIterations: 5,
|
||||||
|
strategy: "reset",
|
||||||
|
})
|
||||||
|
expect(hook.getState()?.iteration).toBe(1)
|
||||||
|
|
||||||
|
// when
|
||||||
|
await hook.event({
|
||||||
|
event: { type: "session.idle", properties: { sessionID: "session-123" } },
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(hook.getState()).toBeNull()
|
||||||
|
expect(promptCalls).toHaveLength(0)
|
||||||
|
expect(createSessionCalls).toHaveLength(1)
|
||||||
|
expect(toastCalls.some((toast) => toast.title === "Ralph Loop Failed" && toast.message.includes("session_creation_rejected"))).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("#given idle path #when state rebound during settle window #then no dispatch against new owner", async () => {
|
||||||
|
// given
|
||||||
|
const hook = createRalphLoopHook({
|
||||||
|
directory: testDirectory,
|
||||||
|
project: testDirectory,
|
||||||
|
worktree: testDirectory,
|
||||||
|
serverUrl: "http://localhost:4096",
|
||||||
|
$: async () => ({}),
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
messages: async () => ({ data: [] }),
|
||||||
|
promptAsync: async (options: { path: { id: string }; body: { parts: Array<{ type: string; text: string }> } }) => {
|
||||||
|
promptCalls.push({ sessionID: options.path.id, text: options.body.parts[0]?.text ?? "" })
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
prompt: async () => ({}),
|
||||||
|
create: async () => ({ data: { id: "new-session-id" } }),
|
||||||
|
},
|
||||||
|
tui: {
|
||||||
|
showToast: async (options: { body: { title: string; message: string; variant: string } }) => {
|
||||||
|
toastCalls.push(options.body)
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as never, {
|
||||||
|
idleSettleMs: 50,
|
||||||
|
})
|
||||||
|
|
||||||
|
hook.startLoop("session-A", "Keep working", { messageCountAtStart: 0, maxIterations: 5 })
|
||||||
|
expect(hook.getState()?.session_id).toBe("session-A")
|
||||||
|
|
||||||
|
// when
|
||||||
|
const eventPromise = hook.event({
|
||||||
|
event: { type: "session.idle", properties: { sessionID: "session-A" } },
|
||||||
|
})
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 10))
|
||||||
|
writeState(testDirectory, { ...hook.getState()!, session_id: "session-B" })
|
||||||
|
await eventPromise
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(promptCalls).toHaveLength(0)
|
||||||
|
expect(hook.getState()?.session_id).toBe("session-B")
|
||||||
|
expect(hook.getState()?.iteration).toBe(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("#given verification-failure path #when incrementIteration fails #then loud failure not success", async () => {
|
||||||
|
// given
|
||||||
|
const loopState = {
|
||||||
|
clearVerificationState: () => ({
|
||||||
|
active: true,
|
||||||
|
iteration: 2,
|
||||||
|
prompt: "Build API",
|
||||||
|
started_at: new Date().toISOString(),
|
||||||
|
session_id: "session-123",
|
||||||
|
completion_promise: ULTRAWORK_VERIFICATION_PROMISE,
|
||||||
|
message_count_at_start: 3,
|
||||||
|
}),
|
||||||
|
incrementIteration: () => null,
|
||||||
|
clear: () => true,
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await handleFailedVerification({
|
||||||
|
directory: testDirectory,
|
||||||
|
project: testDirectory,
|
||||||
|
worktree: testDirectory,
|
||||||
|
serverUrl: "http://localhost:4096",
|
||||||
|
$: async () => ({}),
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
messages: async () => ({ data: [{}, {}, {}] }),
|
||||||
|
promptAsync: async (options: { path: { id: string }; body: { parts: Array<{ type: string; text: string }> } }) => {
|
||||||
|
promptCalls.push({ sessionID: options.path.id, text: options.body.parts[0]?.text ?? "" })
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
abort: async () => ({}),
|
||||||
|
},
|
||||||
|
tui: {
|
||||||
|
showToast: async (options: { body: { title: string; message: string; variant: string } }) => {
|
||||||
|
toastCalls.push(options.body)
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as never, {
|
||||||
|
state: {
|
||||||
|
active: true,
|
||||||
|
iteration: 2,
|
||||||
|
prompt: "Build API",
|
||||||
|
started_at: new Date().toISOString(),
|
||||||
|
session_id: "session-123",
|
||||||
|
completion_promise: ULTRAWORK_VERIFICATION_PROMISE,
|
||||||
|
verification_pending: true,
|
||||||
|
verification_session_id: "ses-oracle",
|
||||||
|
},
|
||||||
|
directory: testDirectory,
|
||||||
|
apiTimeoutMs: 5000,
|
||||||
|
loopState,
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(result).toBe(false)
|
||||||
|
expect(promptCalls).toHaveLength(1)
|
||||||
|
expect(toastCalls.some((toast) => toast.title === "ULTRAWORK LOOP")).toBe(false)
|
||||||
|
expect(
|
||||||
|
toastCalls.some(
|
||||||
|
(toast) => toast.title === "Ralph Loop Failed" && toast.message.includes("iteration commit failed"),
|
||||||
|
),
|
||||||
|
).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("#given reset strategy #when session.create throws #then dispatch failure surfaces", async () => {
|
||||||
|
// given
|
||||||
|
const hook = createRalphLoopHook({
|
||||||
|
directory: testDirectory,
|
||||||
|
project: testDirectory,
|
||||||
|
worktree: testDirectory,
|
||||||
|
serverUrl: "http://localhost:4096",
|
||||||
|
$: async () => ({}),
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
messages: async () => ({ data: [] }),
|
||||||
|
promptAsync: async (options: { path: { id: string }; body: { parts: Array<{ type: string; text: string }> } }) => {
|
||||||
|
promptCalls.push({ sessionID: options.path.id, text: options.body.parts[0]?.text ?? "" })
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
prompt: async () => ({}),
|
||||||
|
create: async () => {
|
||||||
|
throw new Error("simulated network error during session.create")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
tui: {
|
||||||
|
showToast: async (options: { body: { title: string; message: string; variant: string } }) => {
|
||||||
|
toastCalls.push(options.body)
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as never)
|
||||||
|
|
||||||
|
hook.startLoop("session-123", "Keep working", {
|
||||||
|
messageCountAtStart: 0,
|
||||||
|
maxIterations: 5,
|
||||||
|
strategy: "reset",
|
||||||
|
})
|
||||||
|
|
||||||
|
// when
|
||||||
|
await hook.event({
|
||||||
|
event: { type: "session.idle", properties: { sessionID: "session-123" } },
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(hook.getState()).toBeNull()
|
||||||
|
expect(promptCalls).toHaveLength(0)
|
||||||
|
expect(toastCalls.some((toast) => toast.title === "Ralph Loop Failed" && toast.message.includes("session_creation_rejected"))).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -15,11 +15,16 @@ type ContinuationOptions = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ContinuationResult =
|
||||||
|
| { status: "dispatched" }
|
||||||
|
| { status: "session_creation_rejected" }
|
||||||
|
| { status: "dispatch_rejected"; error: unknown }
|
||||||
|
|
||||||
export async function continueIteration(
|
export async function continueIteration(
|
||||||
ctx: PluginInput,
|
ctx: PluginInput,
|
||||||
state: RalphLoopState,
|
state: RalphLoopState,
|
||||||
options: ContinuationOptions,
|
options: ContinuationOptions,
|
||||||
): Promise<void> {
|
): Promise<ContinuationResult> {
|
||||||
const strategy = state.strategy ?? "continue"
|
const strategy = state.strategy ?? "continue"
|
||||||
const continuationPrompt = buildContinuationPrompt(state)
|
const continuationPrompt = buildContinuationPrompt(state)
|
||||||
|
|
||||||
@@ -30,16 +35,20 @@ export async function continueIteration(
|
|||||||
options.directory,
|
options.directory,
|
||||||
)
|
)
|
||||||
if (!newSessionID) {
|
if (!newSessionID) {
|
||||||
return
|
return { status: "session_creation_rejected" }
|
||||||
}
|
}
|
||||||
|
|
||||||
await injectContinuationPrompt(ctx, {
|
try {
|
||||||
sessionID: newSessionID,
|
await injectContinuationPrompt(ctx, {
|
||||||
inheritFromSessionID: options.previousSessionID,
|
sessionID: newSessionID,
|
||||||
prompt: continuationPrompt,
|
inheritFromSessionID: options.previousSessionID,
|
||||||
directory: options.directory,
|
prompt: continuationPrompt,
|
||||||
apiTimeoutMs: options.apiTimeoutMs,
|
directory: options.directory,
|
||||||
})
|
apiTimeoutMs: options.apiTimeoutMs,
|
||||||
|
})
|
||||||
|
} catch (error: unknown) {
|
||||||
|
return { status: "dispatch_rejected", error }
|
||||||
|
}
|
||||||
|
|
||||||
await selectSessionInTui(ctx.client, newSessionID)
|
await selectSessionInTui(ctx.client, newSessionID)
|
||||||
|
|
||||||
@@ -49,16 +58,22 @@ export async function continueIteration(
|
|||||||
previousSessionID: options.previousSessionID,
|
previousSessionID: options.previousSessionID,
|
||||||
newSessionID,
|
newSessionID,
|
||||||
})
|
})
|
||||||
return
|
return { status: "dispatched" }
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return { status: "dispatched" }
|
||||||
}
|
}
|
||||||
|
|
||||||
await injectContinuationPrompt(ctx, {
|
try {
|
||||||
sessionID: options.previousSessionID,
|
await injectContinuationPrompt(ctx, {
|
||||||
prompt: continuationPrompt,
|
sessionID: options.previousSessionID,
|
||||||
directory: options.directory,
|
prompt: continuationPrompt,
|
||||||
apiTimeoutMs: options.apiTimeoutMs,
|
directory: options.directory,
|
||||||
})
|
apiTimeoutMs: options.apiTimeoutMs,
|
||||||
|
})
|
||||||
|
} catch (error: unknown) {
|
||||||
|
return { status: "dispatch_rejected", error }
|
||||||
|
}
|
||||||
|
|
||||||
|
return { status: "dispatched" }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -174,5 +174,27 @@ export function createLoopStateController(options: {
|
|||||||
|
|
||||||
return state
|
return state
|
||||||
},
|
},
|
||||||
|
|
||||||
|
clearVerificationState(sessionID: string, messageCountAtStart?: number): RalphLoopState | null {
|
||||||
|
const state = readState(directory, stateDir)
|
||||||
|
if (!state || state.session_id !== sessionID || !state.ultrawork || !state.verification_pending) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
state.started_at = new Date().toISOString()
|
||||||
|
state.completion_promise = state.initial_completion_promise ?? DEFAULT_COMPLETION_PROMISE
|
||||||
|
state.verification_pending = undefined
|
||||||
|
state.verification_attempt_id = undefined
|
||||||
|
state.verification_session_id = undefined
|
||||||
|
if (typeof messageCountAtStart === "number") {
|
||||||
|
state.message_count_at_start = messageCountAtStart
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!writeState(directory, state, stateDir)) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return state
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,6 +82,9 @@ async function detectOracleVerificationFromParentSession(
|
|||||||
|
|
||||||
type LoopStateController = {
|
type LoopStateController = {
|
||||||
restartAfterFailedVerification: (sessionID: string, messageCountAtStart?: number) => RalphLoopState | null
|
restartAfterFailedVerification: (sessionID: string, messageCountAtStart?: number) => RalphLoopState | null
|
||||||
|
clearVerificationState: (sessionID: string, messageCountAtStart?: number) => RalphLoopState | null
|
||||||
|
incrementIteration: () => RalphLoopState | null
|
||||||
|
clear: () => boolean
|
||||||
setVerificationSessionID: (sessionID: string, verificationSessionID: string) => RalphLoopState | null
|
setVerificationSessionID: (sessionID: string, verificationSessionID: string) => RalphLoopState | null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ type LoopStateController = {
|
|||||||
markVerificationPending: (sessionID: string) => RalphLoopState | null
|
markVerificationPending: (sessionID: string) => RalphLoopState | null
|
||||||
setVerificationSessionID: (sessionID: string, verificationSessionID: string) => RalphLoopState | null
|
setVerificationSessionID: (sessionID: string, verificationSessionID: string) => RalphLoopState | null
|
||||||
restartAfterFailedVerification: (sessionID: string, messageCountAtStart?: number) => RalphLoopState | null
|
restartAfterFailedVerification: (sessionID: string, messageCountAtStart?: number) => RalphLoopState | null
|
||||||
|
clearVerificationState: (sessionID: string, messageCountAtStart?: number) => RalphLoopState | null
|
||||||
}
|
}
|
||||||
type RalphLoopEventHandlerOptions = { directory: string; apiTimeoutMs: number; idleSettleMs: number; getTranscriptPath: (sessionID: string) => string | undefined; checkSessionExists?: RalphLoopOptions["checkSessionExists"]; backgroundManager?: RalphLoopOptions["backgroundManager"]; loopState: LoopStateController }
|
type RalphLoopEventHandlerOptions = { directory: string; apiTimeoutMs: number; idleSettleMs: number; getTranscriptPath: (sessionID: string) => string | undefined; checkSessionExists?: RalphLoopOptions["checkSessionExists"]; backgroundManager?: RalphLoopOptions["backgroundManager"]; loopState: LoopStateController }
|
||||||
|
|
||||||
@@ -272,34 +273,59 @@ export function createRalphLoopEventHandler(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const newState = options.loopState.incrementIteration()
|
await sleep(options.idleSettleMs)
|
||||||
if (!newState) {
|
const stateAfterSettle = options.loopState.getState()
|
||||||
log(`[${HOOK_NAME}] Failed to increment iteration`, { sessionID })
|
if (!stateAfterSettle || !stateAfterSettle.active) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (stateAfterSettle.session_id !== undefined && stateAfterSettle.session_id !== sessionID) {
|
||||||
|
log(`[${HOOK_NAME}] Skipped: state rebound during settle window`, {
|
||||||
|
sessionID,
|
||||||
|
currentOwner: stateAfterSettle.session_id,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (stateAfterSettle.verification_pending) {
|
||||||
|
log(`[${HOOK_NAME}] Skipped: state entered verification_pending during settle window`, { sessionID })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const nextIteration = stateAfterSettle.iteration + 1
|
||||||
|
const previewState: RalphLoopState = { ...stateAfterSettle, iteration: nextIteration }
|
||||||
|
|
||||||
log(`[${HOOK_NAME}] Continuing loop`, {
|
log(`[${HOOK_NAME}] Continuing loop`, {
|
||||||
sessionID,
|
sessionID,
|
||||||
iteration: newState.iteration,
|
iteration: nextIteration,
|
||||||
max: newState.max_iterations,
|
max: previewState.max_iterations,
|
||||||
})
|
})
|
||||||
|
|
||||||
showIterationToast(ctx, newState)
|
const result = await continueIteration(ctx, previewState, {
|
||||||
await sleep(options.idleSettleMs)
|
previousSessionID: sessionID,
|
||||||
|
directory: options.directory,
|
||||||
|
apiTimeoutMs: options.apiTimeoutMs,
|
||||||
|
loopState: options.loopState,
|
||||||
|
})
|
||||||
|
|
||||||
try {
|
if (result.status === "dispatched") {
|
||||||
await continueIteration(ctx, newState, {
|
const committed = options.loopState.incrementIteration()
|
||||||
previousSessionID: sessionID,
|
if (committed) {
|
||||||
directory: options.directory,
|
showIterationToast(ctx, committed)
|
||||||
apiTimeoutMs: options.apiTimeoutMs,
|
} else {
|
||||||
loopState: options.loopState,
|
log(`[${HOOK_NAME}] Dispatch succeeded but iteration commit failed`, { sessionID })
|
||||||
})
|
}
|
||||||
} catch (err) {
|
return
|
||||||
log(`[${HOOK_NAME}] Failed to inject continuation`, {
|
|
||||||
sessionID,
|
|
||||||
error: String(err),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log(`[${HOOK_NAME}] Dispatch failed`, { sessionID, status: result.status })
|
||||||
|
options.loopState.clear()
|
||||||
|
showToastBestEffort(ctx, {
|
||||||
|
title: "Ralph Loop Failed",
|
||||||
|
message: result.status === "dispatch_rejected"
|
||||||
|
? `Dispatch ${result.status}: ${String(result.error)}`
|
||||||
|
: `Dispatch ${result.status}`,
|
||||||
|
variant: "warning",
|
||||||
|
duration: 5000,
|
||||||
|
})
|
||||||
return
|
return
|
||||||
} finally {
|
} finally {
|
||||||
inFlightSessions.delete(sessionID)
|
inFlightSessions.delete(sessionID)
|
||||||
@@ -381,28 +407,54 @@ export function createRalphLoopEventHandler(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const newState = options.loopState.incrementIteration()
|
await sleep(options.idleSettleMs)
|
||||||
if (!newState) {
|
const stateAfterSettle = options.loopState.getState()
|
||||||
log(`[${HOOK_NAME}] Failed to increment iteration after runtime error`, { sessionID })
|
if (!stateAfterSettle || !stateAfterSettle.active) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (stateAfterSettle.session_id !== undefined && stateAfterSettle.session_id !== sessionID) {
|
||||||
|
log(`[${HOOK_NAME}] Skipped: state rebound during settle window`, {
|
||||||
|
sessionID,
|
||||||
|
currentOwner: stateAfterSettle.session_id,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (stateAfterSettle.verification_pending) {
|
||||||
|
log(`[${HOOK_NAME}] Skipped: state entered verification_pending during settle window`, { sessionID })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
showIterationToast(ctx, newState)
|
const nextIteration = stateAfterSettle.iteration + 1
|
||||||
await sleep(options.idleSettleMs)
|
const previewState: RalphLoopState = { ...stateAfterSettle, iteration: nextIteration }
|
||||||
try {
|
|
||||||
await continueIteration(ctx, newState, {
|
const result = await continueIteration(ctx, previewState, {
|
||||||
previousSessionID: sessionID,
|
previousSessionID: sessionID,
|
||||||
directory: options.directory,
|
directory: options.directory,
|
||||||
apiTimeoutMs: options.apiTimeoutMs,
|
apiTimeoutMs: options.apiTimeoutMs,
|
||||||
loopState: options.loopState,
|
loopState: options.loopState,
|
||||||
})
|
})
|
||||||
runtimeErrorRetriedSessions.set(sessionID, newState.iteration)
|
|
||||||
} catch (err) {
|
if (result.status === "dispatched") {
|
||||||
log(`[${HOOK_NAME}] Failed to retry after runtime error`, {
|
const committed = options.loopState.incrementIteration()
|
||||||
sessionID,
|
if (committed) {
|
||||||
error: String(err),
|
showIterationToast(ctx, committed)
|
||||||
})
|
runtimeErrorRetriedSessions.set(sessionID, committed.iteration)
|
||||||
|
} else {
|
||||||
|
log(`[${HOOK_NAME}] Dispatch succeeded but iteration commit failed after runtime error`, { sessionID })
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log(`[${HOOK_NAME}] Dispatch failed after runtime error`, { sessionID, status: result.status })
|
||||||
|
options.loopState.clear()
|
||||||
|
showToastBestEffort(ctx, {
|
||||||
|
title: "Ralph Loop Failed",
|
||||||
|
message: result.status === "dispatch_rejected"
|
||||||
|
? `Dispatch ${result.status}: ${String(result.error)}`
|
||||||
|
: `Dispatch ${result.status}`,
|
||||||
|
variant: "warning",
|
||||||
|
duration: 5000,
|
||||||
|
})
|
||||||
} finally {
|
} finally {
|
||||||
inFlightSessions.delete(sessionID)
|
inFlightSessions.delete(sessionID)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,23 +7,31 @@ export async function createIterationSession(
|
|||||||
parentSessionID: string,
|
parentSessionID: string,
|
||||||
directory: string,
|
directory: string,
|
||||||
): Promise<string | null> {
|
): Promise<string | null> {
|
||||||
const createResult = await ctx.client.session.create({
|
try {
|
||||||
body: {
|
const createResult = await ctx.client.session.create({
|
||||||
parentID: parentSessionID,
|
body: {
|
||||||
title: "Ralph Loop Iteration",
|
parentID: parentSessionID,
|
||||||
},
|
title: "Ralph Loop Iteration",
|
||||||
query: { directory },
|
},
|
||||||
})
|
query: { directory },
|
||||||
|
})
|
||||||
|
|
||||||
if (createResult.error || !createResult.data?.id) {
|
if (createResult.error || !createResult.data?.id) {
|
||||||
log("[ralph-loop] Failed to create iteration session", {
|
log("[ralph-loop] Failed to create iteration session", {
|
||||||
|
parentSessionID,
|
||||||
|
error: String(createResult.error ?? "No session ID returned"),
|
||||||
|
})
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return createResult.data.id
|
||||||
|
} catch (error: unknown) {
|
||||||
|
log("[ralph-loop] session.create threw during iteration session creation", {
|
||||||
parentSessionID,
|
parentSessionID,
|
||||||
error: String(createResult.error ?? "No session ID returned"),
|
error: String(error),
|
||||||
})
|
})
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
return createResult.data.id
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function selectSessionInTui(
|
export async function selectSessionInTui(
|
||||||
|
|||||||
@@ -6,10 +6,22 @@ import { injectContinuationPrompt } from "./continuation-prompt-injector"
|
|||||||
import type { RalphLoopState } from "./types"
|
import type { RalphLoopState } from "./types"
|
||||||
|
|
||||||
type LoopStateController = {
|
type LoopStateController = {
|
||||||
restartAfterFailedVerification: (
|
clearVerificationState: (
|
||||||
sessionID: string,
|
sessionID: string,
|
||||||
messageCountAtStart?: number,
|
messageCountAtStart?: number,
|
||||||
) => RalphLoopState | null
|
) => RalphLoopState | null
|
||||||
|
incrementIteration: () => RalphLoopState | null
|
||||||
|
clear: () => boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
function showToastBestEffort(
|
||||||
|
ctx: PluginInput,
|
||||||
|
body: { title: string; message: string; variant: "warning" | "info"; duration: number },
|
||||||
|
): void {
|
||||||
|
try {
|
||||||
|
void Promise.resolve(ctx.client.tui?.showToast?.({ body })).catch(() => {})
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMessageCountFromResponse(messagesResponse: unknown): number {
|
function getMessageCountFromResponse(messagesResponse: unknown): number {
|
||||||
@@ -72,23 +84,53 @@ export async function handleFailedVerification(
|
|||||||
ctx.client.session.abort({ path: { id: state.verification_session_id } }).catch(() => {})
|
ctx.client.session.abort({ path: { id: state.verification_session_id } }).catch(() => {})
|
||||||
}
|
}
|
||||||
|
|
||||||
const resumedState = loopState.restartAfterFailedVerification(
|
const clearedState = loopState.clearVerificationState(
|
||||||
parentSessionID,
|
parentSessionID,
|
||||||
messageCountAtStart,
|
messageCountAtStart,
|
||||||
)
|
)
|
||||||
if (!resumedState) {
|
if (!clearedState) {
|
||||||
log(`[${HOOK_NAME}] Failed to restart loop after verification failure`, {
|
log(`[${HOOK_NAME}] Failed to restart loop after verification failure`, {
|
||||||
parentSessionID,
|
parentSessionID,
|
||||||
})
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
await injectContinuationPrompt(ctx, {
|
const previewState: RalphLoopState = { ...clearedState, iteration: clearedState.iteration + 1 }
|
||||||
sessionID: parentSessionID,
|
|
||||||
prompt: buildVerificationFailurePrompt(resumedState),
|
try {
|
||||||
directory,
|
await injectContinuationPrompt(ctx, {
|
||||||
apiTimeoutMs,
|
sessionID: parentSessionID,
|
||||||
})
|
prompt: buildVerificationFailurePrompt(previewState),
|
||||||
|
directory,
|
||||||
|
apiTimeoutMs,
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
log(`[${HOOK_NAME}] Failed to inject verification failure prompt`, {
|
||||||
|
parentSessionID,
|
||||||
|
error: String(error),
|
||||||
|
})
|
||||||
|
loopState.clear()
|
||||||
|
showToastBestEffort(ctx, {
|
||||||
|
title: "Ralph Loop Failed",
|
||||||
|
message: `Verification continuation rejected: ${String(error)}`,
|
||||||
|
variant: "warning",
|
||||||
|
duration: 5000,
|
||||||
|
})
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const committed = loopState.incrementIteration()
|
||||||
|
if (!committed) {
|
||||||
|
log(`[${HOOK_NAME}] Failed to commit iteration after verification restart`, { parentSessionID })
|
||||||
|
loopState.clear()
|
||||||
|
showToastBestEffort(ctx, {
|
||||||
|
title: "Ralph Loop Failed",
|
||||||
|
message: "Verification continuation dispatched but iteration commit failed",
|
||||||
|
variant: "warning",
|
||||||
|
duration: 5000,
|
||||||
|
})
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
await ctx.client.tui?.showToast?.({
|
await ctx.client.tui?.showToast?.({
|
||||||
body: {
|
body: {
|
||||||
|
|||||||
Reference in New Issue
Block a user