Handle failed pending turns
This commit is contained in:
4 files changed
+140
-26
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
ApprovalRequestId,
|
||||
CheckpointRef,
|
||||
CommandId,
|
||||
CorrelationId,
|
||||
@@ -2463,7 +2464,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("clears a pending turn start when provider startup fails", () =>
|
||||
it.effect("only clears the pending turn start that failed", () =>
|
||||
Effect.gen(function* () {
|
||||
const projectionPipeline = yield* OrchestrationProjectionPipeline;
|
||||
const eventStore = yield* OrchestrationEventStore;
|
||||
@@ -2486,44 +2487,95 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
|
||||
metadata: {},
|
||||
payload: {
|
||||
threadId,
|
||||
messageId: MessageId.make("message-start-failed"),
|
||||
messageId: MessageId.make("message-start-old"),
|
||||
runtimeMode: "full-access",
|
||||
createdAt: "2026-02-26T13:00:00.000Z",
|
||||
},
|
||||
});
|
||||
|
||||
yield* appendAndProject({
|
||||
type: "thread.session-set",
|
||||
type: "thread.turn-start-requested",
|
||||
eventId: EventId.make("evt-start-failed-2"),
|
||||
aggregateKind: "thread",
|
||||
aggregateId: threadId,
|
||||
occurredAt: "2026-02-26T13:00:01.000Z",
|
||||
occurredAt: "2026-02-26T13:00:00.100Z",
|
||||
commandId: CommandId.make("cmd-start-failed-2"),
|
||||
causationEventId: null,
|
||||
correlationId: CorrelationId.make("cmd-start-failed-2"),
|
||||
metadata: {},
|
||||
payload: {
|
||||
threadId,
|
||||
session: {
|
||||
threadId,
|
||||
status: "ready",
|
||||
providerName: "codex",
|
||||
runtimeMode: "full-access",
|
||||
activeTurnId: null,
|
||||
lastError: "Provider startup failed",
|
||||
updatedAt: "2026-02-26T13:00:01.000Z",
|
||||
messageId: MessageId.make("message-start-new"),
|
||||
runtimeMode: "full-access",
|
||||
createdAt: "2026-02-26T13:00:00.100Z",
|
||||
},
|
||||
});
|
||||
|
||||
yield* appendAndProject({
|
||||
type: "thread.activity-appended",
|
||||
eventId: EventId.make("evt-start-failed-3"),
|
||||
aggregateKind: "thread",
|
||||
aggregateId: threadId,
|
||||
occurredAt: "2026-02-26T13:00:00.200Z",
|
||||
commandId: CommandId.make("cmd-start-failed-3"),
|
||||
causationEventId: null,
|
||||
correlationId: CorrelationId.make("cmd-start-failed-3"),
|
||||
metadata: { requestId: ApprovalRequestId.make("message-start-old") },
|
||||
payload: {
|
||||
threadId,
|
||||
activity: {
|
||||
id: EventId.make("activity-start-failed-old"),
|
||||
tone: "error",
|
||||
kind: "provider.turn.start.failed",
|
||||
summary: "Provider turn start failed",
|
||||
payload: { requestId: "message-start-old", detail: "Provider startup failed" },
|
||||
turnId: null,
|
||||
createdAt: "2026-02-26T13:00:00.200Z",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const pendingRows = yield* sql<{ readonly threadId: string }>`
|
||||
SELECT thread_id AS "threadId"
|
||||
const pendingRows = yield* sql<{ readonly messageId: string }>`
|
||||
SELECT pending_message_id AS "messageId"
|
||||
FROM projection_turns
|
||||
WHERE thread_id = ${threadId}
|
||||
AND turn_id IS NULL
|
||||
AND state = 'pending'
|
||||
`;
|
||||
assert.deepEqual(pendingRows, []);
|
||||
assert.deepEqual(pendingRows, [{ messageId: "message-start-new" }]);
|
||||
|
||||
yield* appendAndProject({
|
||||
type: "thread.activity-appended",
|
||||
eventId: EventId.make("evt-start-failed-4"),
|
||||
aggregateKind: "thread",
|
||||
aggregateId: threadId,
|
||||
occurredAt: "2026-02-26T13:00:00.300Z",
|
||||
commandId: CommandId.make("cmd-start-failed-4"),
|
||||
causationEventId: null,
|
||||
correlationId: CorrelationId.make("cmd-start-failed-4"),
|
||||
metadata: { requestId: ApprovalRequestId.make("message-start-new") },
|
||||
payload: {
|
||||
threadId,
|
||||
activity: {
|
||||
id: EventId.make("activity-start-failed-new"),
|
||||
tone: "error",
|
||||
kind: "provider.turn.start.failed",
|
||||
summary: "Provider turn start failed",
|
||||
payload: { requestId: "message-start-new", detail: "Provider startup failed" },
|
||||
turnId: null,
|
||||
createdAt: "2026-02-26T13:00:00.300Z",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const clearedRows = yield* sql<{ readonly messageId: string }>`
|
||||
SELECT pending_message_id AS "messageId"
|
||||
FROM projection_turns
|
||||
WHERE thread_id = ${threadId}
|
||||
AND turn_id IS NULL
|
||||
AND state = 'pending'
|
||||
`;
|
||||
assert.deepEqual(clearedRows, []);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1056,20 +1056,32 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
|
||||
return;
|
||||
}
|
||||
|
||||
case "thread.activity-appended": {
|
||||
if (event.payload.activity.kind !== "provider.turn.start.failed") {
|
||||
return;
|
||||
}
|
||||
const requestId = event.metadata.requestId;
|
||||
if (requestId === undefined) {
|
||||
return;
|
||||
}
|
||||
const pendingTurnStart = yield* projectionTurnRepository.getPendingTurnStartByThreadId({
|
||||
threadId: event.payload.threadId,
|
||||
});
|
||||
if (
|
||||
Option.isNone(pendingTurnStart) ||
|
||||
String(requestId) !== pendingTurnStart.value.messageId
|
||||
) {
|
||||
return;
|
||||
}
|
||||
yield* projectionTurnRepository.deletePendingTurnStartByThreadId({
|
||||
threadId: event.payload.threadId,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
case "thread.session-set": {
|
||||
const turnId = event.payload.session.activeTurnId;
|
||||
if (turnId === null || event.payload.session.status !== "running") {
|
||||
if (
|
||||
event.payload.session.lastError !== null ||
|
||||
event.payload.session.status === "error" ||
|
||||
event.payload.session.status === "interrupted" ||
|
||||
event.payload.session.status === "stopped"
|
||||
) {
|
||||
yield* projectionTurnRepository.deletePendingTurnStartByThreadId({
|
||||
threadId: event.payload.threadId,
|
||||
});
|
||||
}
|
||||
|
||||
// Leaving the "running" session status is the turn-end signal:
|
||||
// settle still-running turns so their duration reflects the whole
|
||||
// turn rather than the last assistant message.
|
||||
|
||||
@@ -411,6 +411,7 @@ describe("ProviderCommandReactor", () => {
|
||||
return {
|
||||
engine,
|
||||
readModel: () => Effect.runPromise(snapshotQuery.getSnapshot()),
|
||||
snapshotQuery,
|
||||
startSession,
|
||||
sendTurn,
|
||||
interruptTurn,
|
||||
@@ -466,6 +467,53 @@ describe("ProviderCommandReactor", () => {
|
||||
expect(thread?.session?.runtimeMode).toBe("approval-required");
|
||||
});
|
||||
|
||||
effectIt.effect("clears pending work when session startup fails before binding", () =>
|
||||
Effect.gen(function* () {
|
||||
const harness = yield* Effect.promise(() => createHarness());
|
||||
const now = "2026-01-01T00:00:00.000Z";
|
||||
const messageId = asMessageId("user-message-start-failed");
|
||||
harness.startSession.mockImplementationOnce(
|
||||
(_: unknown, __: unknown) => Effect.fail("simulated startup failure") as never,
|
||||
);
|
||||
|
||||
yield* harness.engine.dispatch({
|
||||
type: "thread.turn.start",
|
||||
commandId: CommandId.make("cmd-turn-start-failed"),
|
||||
threadId: ThreadId.make("thread-1"),
|
||||
message: {
|
||||
messageId,
|
||||
role: "user",
|
||||
text: "hello reactor",
|
||||
attachments: [],
|
||||
},
|
||||
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
|
||||
runtimeMode: "approval-required",
|
||||
createdAt: now,
|
||||
});
|
||||
|
||||
yield* Effect.promise(() =>
|
||||
waitFor(async () => {
|
||||
const readModel = await harness.readModel();
|
||||
return (
|
||||
readModel.threads[0]?.activities.some(
|
||||
(activity) => activity.kind === "provider.turn.start.failed",
|
||||
) ?? false
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
const readModel = yield* harness.snapshotQuery.getSnapshot();
|
||||
expect(readModel.threads[0]?.session).toBeNull();
|
||||
expect(
|
||||
readModel.threads[0]?.activities.find(
|
||||
(activity) => activity.kind === "provider.turn.start.failed",
|
||||
),
|
||||
).toMatchObject({ payload: { requestId: messageId } });
|
||||
const shellSnapshot = yield* harness.snapshotQuery.getShellSnapshot();
|
||||
expect(shellSnapshot.threads[0]?.hasPendingTurnStart).toBe(false);
|
||||
}),
|
||||
);
|
||||
|
||||
it("generates a thread title on the first turn", async () => {
|
||||
const harness = await createHarness();
|
||||
const now = "2026-01-01T00:00:00.000Z";
|
||||
|
||||
@@ -766,6 +766,7 @@ const make = Effect.gen(function* () {
|
||||
detail: `User message '${event.payload.messageId}' was not found for turn start request.`,
|
||||
turnId: null,
|
||||
createdAt: event.payload.createdAt,
|
||||
requestId: event.payload.messageId,
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -819,6 +820,7 @@ const make = Effect.gen(function* () {
|
||||
detail,
|
||||
turnId: null,
|
||||
createdAt: event.payload.createdAt,
|
||||
requestId: event.payload.messageId,
|
||||
}),
|
||||
),
|
||||
Effect.asVoid,
|
||||
|
||||
Reference in new issue
Block a user