Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/harness/harness.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ describe("harness stream protocol", () => {
])
})

it("accepts an idempotent continuation replay after the run is terminal", async () => {
const terminalEvent: HarnessEvent = {
conversationId: "conversation-1",
createdAt: new Date(0).toISOString(),
eventId: "event-terminal-replay",
payload: { status: "completed" },
runId: "run-1",
seq: "4",
stepId: null,
turnId: "turn-1",
type: "run/status",
}
const body = `event: harness\ndata: ${JSON.stringify(terminalEvent)}\n\n`

await expect(consumeHarnessStream(new Response(body))).resolves.toEqual({
assistantMessageId: undefined,
runId: "run-1",
status: "completed",
text: "",
})
})

it("collects OpenAI tool calls emitted in the final delta", async () => {
const toolStarts = vi.fn()
const response = new Response(
Expand Down
43 changes: 29 additions & 14 deletions server/routes/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,14 @@ function dispatchFromRequest(request: Request): ModelDispatcher {
async function streamRun(input: {
initialMessages?: ModelMessage[]
request: Request
runId: string
run: {
conversationId: string
id: string
status: string
}
userId: string
}): Promise<Response> {
const run = await prisma.agentRun.findFirst({ where: { id: input.runId, userId: input.userId } })
if (!run) return jsonErrorResponse(404, "Agent run not found")
const run = input.run
let lease: Awaited<ReturnType<typeof acquireRunLease>> = null
if (["queued", "yielded", "running"].includes(run.status)) {
lease = await acquireRunLease(run.id, input.userId)
Expand Down Expand Up @@ -177,12 +180,24 @@ async function streamRun(input: {
})
}

const statusEvent = await appendHarnessEvent({
conversationId: run.conversationId,
payload: { status },
runId: run.id,
type: "run/status",
})
const statusEvent: HarnessEvent = lease
? await appendHarnessEvent({
conversationId: run.conversationId,
payload: { status },
runId: run.id,
type: "run/status",
})
: {
conversationId: run.conversationId,
createdAt: new Date().toISOString(),
eventId: `replay_${run.id}_${status}`,
payload: { status },
runId: run.id,
seq: "0",
stepId: null,
turnId: null,
type: "run/status",
}
send(statusEvent)
controller.close()
} catch (error) {
Expand Down Expand Up @@ -386,7 +401,7 @@ app.post("/conversations/:id/turns", async (c) => {
? undefined
: normalizeRequestMessages(body.messages),
request: c.req.raw,
runId: run.id,
run,
userId,
})
})
Expand Down Expand Up @@ -666,10 +681,10 @@ app.post("/agent-runs/:id/continue", async (c) => {
if (typeof userId !== "string") return userId
const run = await prisma.agentRun.findFirst({ where: { id: c.req.param("id"), userId } })
if (!run) return jsonErrorResponse(404, "Agent run not found")
if (!["yielded", "queued", "running"].includes(run.status)) {
return jsonErrorResponse(409, `Run cannot continue from status ${run.status}`)
}
return await streamRun({ request: c.req.raw, runId: run.id, userId })
// A continuation can race with the worker that just made the run terminal.
// Replaying the terminal status over the normal stream keeps this endpoint
// idempotent and prevents a harmless retry from surfacing as a chat error.
return await streamRun({ request: c.req.raw, run, userId })
})

app.post("/agent-runs/:id/cancel", async (c) => {
Expand Down
Loading