Skip to content

Commit b372af4

Browse files
kiiweejuliusmarminge
authored andcommitted
fix(server): clear streaming items during runtime recovery
1 parent cc1e634 commit b372af4

2 files changed

Lines changed: 97 additions & 4 deletions

File tree

‎apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.test.ts‎

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,23 @@ it.effect(
671671
const providerThreadId = ProviderThreadId.make("provider_thread_recovery_cancel");
672672
const providerTurnId = ProviderTurnId.make("provider_turn_recovery_cancel");
673673
const providerSessionId = ProviderSessionId.make("provider_session_recovery_cancel");
674+
const assistantItemId = TurnItemId.make("turn_item_recovery_cancel_assistant");
675+
const reasoningItemId = TurnItemId.make("turn_item_recovery_cancel_reasoning");
676+
const commandItemId = TurnItemId.make("turn_item_recovery_cancel");
677+
const asyncQuestionNodeId = NodeId.make("node_recovery_cancel_async_question");
678+
const asyncQuestionItemId = TurnItemId.make("turn_item_recovery_cancel_async_question");
674679
let committedInput: Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0] | null =
675680
null;
676681
const projection = {
677682
thread: { id: threadId },
678-
runtimeRequests: [],
683+
runtimeRequests: [
684+
{
685+
id: RuntimeRequestId.make("request_recovery_cancel_async_question"),
686+
nodeId: asyncQuestionNodeId,
687+
status: "pending",
688+
responseCapability: { type: "message" },
689+
},
690+
],
679691
providerSessions: [
680692
{
681693
id: providerSessionId,
@@ -716,15 +728,42 @@ it.effect(
716728
status: "running",
717729
},
718730
],
719-
nodes: [{ id: rootNodeId, runId, status: "running" }],
731+
nodes: [
732+
{ id: rootNodeId, runId, status: "running" },
733+
{ id: asyncQuestionNodeId, runId, status: "waiting" },
734+
],
720735
subagents: [],
721736
messages: [{ id: MessageId.make("message_recovery_cancel"), runId, streaming: true }],
722737
turnItems: [
723738
{
724-
id: TurnItemId.make("turn_item_recovery_cancel"),
739+
id: commandItemId,
740+
runId,
741+
nodeId: rootNodeId,
742+
type: "command_execution",
743+
status: "running",
744+
},
745+
{
746+
id: assistantItemId,
747+
runId,
748+
nodeId: rootNodeId,
749+
type: "assistant_message",
750+
status: "running",
751+
streaming: true,
752+
},
753+
{
754+
id: reasoningItemId,
725755
runId,
726756
nodeId: rootNodeId,
757+
type: "reasoning",
727758
status: "running",
759+
streaming: true,
760+
},
761+
{
762+
id: asyncQuestionItemId,
763+
runId,
764+
nodeId: asyncQuestionNodeId,
765+
type: "user_input_request",
766+
status: "waiting",
728767
},
729768
],
730769
} as unknown as OrchestrationV2ThreadProjection;
@@ -779,12 +818,58 @@ it.effect(
779818
["provider-turn.updated", "cancelled"],
780819
["message.updated", null],
781820
["turn-item.updated", "cancelled"],
821+
["turn-item.updated", "cancelled"],
822+
["turn-item.updated", "cancelled"],
782823
["provider-thread.updated", "idle"],
783824
["provider-session.updated", "stopped"],
784825
],
785826
);
786827
const messageEvent = events.find((event) => event.type === "message.updated");
787828
assert.isFalse(messageEvent?.type === "message.updated" && messageEvent.payload.streaming);
829+
const turnItemEventById = (id: TurnItemId) =>
830+
events.find((event) => event.type === "turn-item.updated" && event.payload.id === id);
831+
const commandEvent = turnItemEventById(commandItemId);
832+
assert.equal(
833+
commandEvent?.type === "turn-item.updated" ? commandEvent.payload.status : null,
834+
"cancelled",
835+
);
836+
assert.equal(
837+
commandEvent?.type === "turn-item.updated" ? commandEvent.payload.type : null,
838+
"command_execution",
839+
);
840+
assert.isFalse(
841+
commandEvent?.type === "turn-item.updated" && "streaming" in commandEvent.payload,
842+
);
843+
const assistantEvent = turnItemEventById(assistantItemId);
844+
assert.equal(
845+
assistantEvent?.type === "turn-item.updated" ? assistantEvent.payload.status : null,
846+
"cancelled",
847+
);
848+
assert.equal(
849+
assistantEvent?.type === "turn-item.updated" &&
850+
assistantEvent.payload.type === "assistant_message"
851+
? assistantEvent.payload.streaming
852+
: null,
853+
false,
854+
);
855+
const reasoningEvent = turnItemEventById(reasoningItemId);
856+
assert.equal(
857+
reasoningEvent?.type === "turn-item.updated" ? reasoningEvent.payload.status : null,
858+
"cancelled",
859+
);
860+
assert.equal(
861+
reasoningEvent?.type === "turn-item.updated" && reasoningEvent.payload.type === "reasoning"
862+
? reasoningEvent.payload.streaming
863+
: null,
864+
false,
865+
);
866+
assert.isUndefined(turnItemEventById(asyncQuestionItemId));
867+
assert.isUndefined(events.find((event) => event.type === "runtime-request.updated"));
868+
assert.isUndefined(
869+
events.find(
870+
(event) => event.type === "node.updated" && event.payload.id === asyncQuestionNodeId,
871+
),
872+
);
788873
}).pipe(Effect.provide(layer));
789874
},
790875
);

‎apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,15 @@ export const make = Effect.gen(function* () {
419419
...(item.nodeId === null ? {} : { nodeId: item.nodeId }),
420420
providerInstanceId: run.providerInstanceId,
421421
occurredAt: now,
422-
payload: { ...item, status: "cancelled", completedAt: now, updatedAt: now },
422+
payload: {
423+
...item,
424+
status: "cancelled",
425+
completedAt: now,
426+
updatedAt: now,
427+
...(item.type === "reasoning" || item.type === "assistant_message"
428+
? { streaming: false }
429+
: {}),
430+
},
423431
});
424432
}
425433
}

0 commit comments

Comments
 (0)