Skip to content

Commit 70833b2

Browse files
NotReliableSpikemaria-rcksjuliusmarminge
authored
fix(server): tool calls a released session cuts off no longer keep the thread Waiting (#29036)
Co-authored-by: Spike <[email protected]> Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent cbf9e26 commit 70833b2

6 files changed

Lines changed: 672 additions & 122 deletions

File tree

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

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ export interface EventSinkV2Shape {
169169
},
170170
EventSinkV2Error
171171
>;
172+
/** Commits only if the stored turn item still equals expected. */
173+
readonly writeIfTurnItemUnchanged: (input: {
174+
readonly expected: OrchestrationV2TurnItem;
175+
readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
176+
}) => Effect.Effect<
177+
{
178+
readonly committed: boolean;
179+
readonly storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>;
180+
},
181+
EventSinkV2Error
182+
>;
172183
readonly commitCommand: (input: {
173184
readonly commandId: CommandId;
174185
readonly threadId: ThreadId;
@@ -634,31 +645,23 @@ const layerBase: Layer.Layer<
634645
);
635646
});
636647

637-
const writeIfProviderThreadUnchangedEffect = Effect.fn(
638-
"orchestrationV2.EventSink.writeIfProviderThreadUnchanged",
639-
)(function* (input: Parameters<EventSinkV2Shape["writeIfProviderThreadUnchanged"]>[0]) {
640-
yield* Effect.annotateCurrentSpan({
641-
"orchestration_v2.event_count": input.events.length,
642-
"orchestration_v2.provider_thread_id": input.expected.id,
643-
});
644-
const expectedJson = yield* ProjectionStore.encodeProviderThreadPayload(input.expected);
645-
646-
return yield* commitThenPublish(
648+
// Appends the events only if the stored payload still matches, in one transaction.
649+
const writeIfStoredPayloadMatches = <E>(
650+
readStoredPayload: Effect.Effect<ReadonlyArray<{ readonly payload_json: string }>, E>,
651+
expectedJson: string,
652+
events: ReadonlyArray<OrchestrationV2DomainEvent>,
653+
) =>
654+
commitThenPublish(
647655
Effect.gen(function* () {
648-
const rows = yield* sql<{ readonly payload_json: string }>`
649-
SELECT payload_json
650-
FROM orchestration_v2_projection_provider_threads
651-
WHERE provider_thread_id = ${input.expected.id}
652-
LIMIT 1
653-
`;
656+
const rows = yield* readStoredPayload;
654657
if (rows[0]?.payload_json !== expectedJson) {
655658
return {
656659
committed: false as const,
657660
storedEvents: [] as ReadonlyArray<OrchestrationV2StoredEvent>,
658661
};
659662
}
660663

661-
const normalized = yield* normalizeEvents(input.events);
664+
const normalized = yield* normalizeEvents(events);
662665
const storedEvents = yield* eventStore.append({ events: normalized });
663666
const outcomes = yield* applyStoredEvents(storedEvents);
664667
return { committed: true as const, storedEvents, outcomes };
@@ -668,6 +671,45 @@ const layerBase: Layer.Layer<
668671
? publishStoredEvents(result.storedEvents, result.outcomes)
669672
: Effect.void,
670673
);
674+
675+
const writeIfProviderThreadUnchangedEffect = Effect.fn(
676+
"orchestrationV2.EventSink.writeIfProviderThreadUnchanged",
677+
)(function* (input: Parameters<EventSinkV2Shape["writeIfProviderThreadUnchanged"]>[0]) {
678+
yield* Effect.annotateCurrentSpan({
679+
"orchestration_v2.event_count": input.events.length,
680+
"orchestration_v2.provider_thread_id": input.expected.id,
681+
});
682+
const expectedJson = yield* ProjectionStore.encodeProviderThreadPayload(input.expected);
683+
return yield* writeIfStoredPayloadMatches(
684+
sql<{ readonly payload_json: string }>`
685+
SELECT payload_json
686+
FROM orchestration_v2_projection_provider_threads
687+
WHERE provider_thread_id = ${input.expected.id}
688+
LIMIT 1
689+
`,
690+
expectedJson,
691+
input.events,
692+
);
693+
});
694+
695+
const writeIfTurnItemUnchangedEffect = Effect.fn(
696+
"orchestrationV2.EventSink.writeIfTurnItemUnchanged",
697+
)(function* (input: Parameters<EventSinkV2Shape["writeIfTurnItemUnchanged"]>[0]) {
698+
yield* Effect.annotateCurrentSpan({
699+
"orchestration_v2.event_count": input.events.length,
700+
"orchestration_v2.turn_item_id": input.expected.id,
701+
});
702+
const expectedJson = yield* ProjectionStore.encodeTurnItemPayload(input.expected);
703+
return yield* writeIfStoredPayloadMatches(
704+
sql<{ readonly payload_json: string }>`
705+
SELECT payload_json
706+
FROM orchestration_v2_projection_turn_items
707+
WHERE turn_item_id = ${input.expected.id}
708+
LIMIT 1
709+
`,
710+
expectedJson,
711+
input.events,
712+
);
671713
});
672714

673715
const existingCommandResult =
@@ -1070,6 +1112,12 @@ const layerBase: Layer.Layer<
10701112
(cause) => new EventSinkWriteError({ eventCount: input.events.length, cause }),
10711113
),
10721114
),
1115+
writeIfTurnItemUnchanged: (input) =>
1116+
writeIfTurnItemUnchangedEffect(input).pipe(
1117+
Effect.mapError(
1118+
(cause) => new EventSinkWriteError({ eventCount: input.events.length, cause }),
1119+
),
1120+
),
10731121
commitCommand: (input) =>
10741122
commitCommandEffect(input).pipe(
10751123
Effect.mapError(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ const encodeMessagePayload = Schema.encodeEffect(
11921192
const encodePlanPayload = Schema.encodeEffect(
11931193
Schema.fromJsonString(OrchestrationV2PlanArtifactSchema),
11941194
);
1195-
const encodeTurnItemPayload = Schema.encodeEffect(
1195+
export const encodeTurnItemPayload = Schema.encodeEffect(
11961196
Schema.fromJsonString(OrchestrationV2TurnItemJsonSchema),
11971197
);
11981198
// The codec every *Json payload schema uses for timestamps, so index columns match payload_json.

0 commit comments

Comments
 (0)