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
23 changes: 23 additions & 0 deletions apps/server/src/orchestration-v2/EventSink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export interface EventSinkV2Shape {
readonly commandId?: CommandId;
readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
}) => Effect.Effect<ReadonlyArray<OrchestrationV2StoredEvent>, EventSinkV2Error>;
/** Builds the events inside the commit transaction, so they reflect what is stored; none commits nothing. */
readonly writeBuilt: <E>(
build: Effect.Effect<ReadonlyArray<OrchestrationV2DomainEvent>, E>,
) => Effect.Effect<ReadonlyArray<OrchestrationV2StoredEvent>, EventSinkV2Error>;
readonly writeWithEffects: (input: {
/**
* Rebuilds each run.updated from the stored row with only its delegatedCompletion replaced.
Expand Down Expand Up @@ -520,6 +524,21 @@ const layerBase: Layer.Layer<
return committed;
});

const writeBuiltEffect = Effect.fn("orchestrationV2.EventSink.writeBuilt")(function* <E>(
build: Effect.Effect<ReadonlyArray<OrchestrationV2DomainEvent>, E>,
) {
const { committed } = yield* commitThenPublish(
Effect.gen(function* () {
const events = yield* build;
if (events.length === 0) return { committed: [], outcomes: [] };
const committed = yield* eventStore.append({ events: yield* normalizeEvents(events) });
return { committed, outcomes: yield* applyStoredEvents(committed) };
}),
({ committed, outcomes }) => publishStoredEvents(committed, outcomes),
);
return committed;
});

const writeIfRunCurrentEffect = Effect.fn("orchestrationV2.EventSink.writeIfRunCurrent")(
function* (input: Parameters<EventSinkV2Shape["writeIfRunCurrent"]>[0]) {
yield* Effect.annotateCurrentSpan({
Expand Down Expand Up @@ -1137,6 +1156,10 @@ const layerBase: Layer.Layer<
}),
),
),
writeBuilt: (build) =>
writeBuiltEffect(build).pipe(
Effect.mapError((cause) => new EventSinkWriteError({ eventCount: 0, cause })),
),
writeWithEffects: (input) =>
writeEffect(input).pipe(
Effect.mapError(
Expand Down
38 changes: 37 additions & 1 deletion apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,19 @@ export function applyToProjection(
...projection,
thread: event.payload,
};
case "thread.history-trimmed": {
if (event.payload.sourceThreadId !== event.threadId) return projection;
const items = new Set<string>(event.payload.turnItemIds);
const nodes = new Set<string>(event.payload.nodeIds);
return {
...projection,
turnItems: projection.turnItems.filter((item) => !items.has(item.id)),
nodes: projection.nodes.filter((node) => !nodes.has(node.id)),
visibleTurnItems: projection.visibleTurnItems
.filter((row) => row.sourceThreadId !== event.threadId || !items.has(row.sourceItemId))
.map((row, position) => (row.position === position ? row : { ...row, position })),
};
}
case "thread.visit-recorded":
return {
...projection,
Expand Down Expand Up @@ -3337,6 +3350,28 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
}
case "checkpoint.rollback-requested":
break;
case "thread.history-trimmed": {
const { turnItemIds, nodeIds } = event.payload;
// A fork's copy only revises the fork; the source's own copy removed the rows.
if (event.payload.sourceThreadId !== event.threadId) break;
if (turnItemIds.length > 0) {
yield* sql`
DELETE FROM orchestration_v2_projection_turn_items
WHERE thread_id = ${event.threadId} AND turn_item_id IN ${sql.in(turnItemIds)}
`;
yield* sql`
DELETE FROM orchestration_v2_turn_item_positions
WHERE thread_id = ${event.threadId} AND turn_item_id IN ${sql.in(turnItemIds)}
`;
}
if (nodeIds.length > 0) {
yield* sql`
DELETE FROM orchestration_v2_projection_nodes
WHERE thread_id = ${event.threadId} AND node_id IN ${sql.in(nodeIds)}
`;
}
break;
}
case "context-handoff.updated": {
const payloadJson = yield* encodeContextHandoffPayload(event.payload);
const payload = parseEncodedPayload(payloadJson);
Expand Down Expand Up @@ -3437,7 +3472,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
event.type !== "thread.interaction-mode-updated" &&
event.type !== "thread.model-selection-updated" &&
event.type !== "thread.provider-switched" &&
event.type !== "thread.guest-share-updated"
event.type !== "thread.guest-share-updated" &&
event.type !== "thread.history-trimmed"
) {
const updatedAt = yield* encodeThreadUpdatedAt(event.occurredAt);
yield* sql`
Expand Down
Loading
Loading