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
2 changes: 2 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9065,6 +9065,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
getModelContextWindow: (selection) =>
resolveClaudeCatalogContextWindowTokens(BUNDLED_CLAUDE_MODEL_CATALOG, selection),
events: Stream.fromEffectRepeat(Queue.take(events)),
// Background shells, agents, monitors, and workflows end with the CLI.
backgroundTasksEndWithProcess: true,
hasPendingBackgroundWork: Effect.gen(function* () {
// Session capability: any native thread with pending work pins idle.
for (const roster of (yield* Ref.get(pendingBackgroundTasksByNativeThread)).values()) {
Expand Down
56 changes: 56 additions & 0 deletions apps/server/src/orchestration-v2/EventSink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
CommandId,
type OrchestrationV2Run,
OrchestrationV2DomainEvent,
type OrchestrationV2ProviderThread,
OrchestrationV2StoredEvent,
ProviderThreadId,
RunAttemptId,
Expand Down Expand Up @@ -119,6 +120,22 @@ export interface EventSinkV2Shape {
},
EventSinkV2Error
>;
/**
* Atomically commit only while the stored provider thread still equals
* `expected`, the row its writer read, so a read-modify-write of the row
* cannot overwrite an update published in between. The whole row is
* compared because not every writer moves `updatedAt`.
*/
readonly writeIfProviderThreadUnchanged: (input: {
readonly expected: OrchestrationV2ProviderThread;
readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
}) => Effect.Effect<
{
readonly committed: boolean;
readonly storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>;
},
EventSinkV2Error
>;
readonly commitCommand: (input: {
readonly commandId: CommandId;
readonly threadId: ThreadId;
Expand Down Expand Up @@ -509,6 +526,39 @@ const layerBase: Layer.Layer<
);
});

const writeIfProviderThreadUnchangedEffect = Effect.fn(
"orchestrationV2.EventSink.writeIfProviderThreadUnchanged",
)(function* (input: Parameters<EventSinkV2Shape["writeIfProviderThreadUnchanged"]>[0]) {
yield* Effect.annotateCurrentSpan({
"orchestration_v2.event_count": input.events.length,
"orchestration_v2.provider_thread_id": input.expected.id,
});
const expectedJson = yield* ProjectionStore.encodeProviderThreadPayload(input.expected);

return yield* commitThenPublish(
Effect.gen(function* () {
const rows = yield* sql<{ readonly payload_json: string }>`
SELECT payload_json
FROM orchestration_v2_projection_provider_threads
WHERE provider_thread_id = ${input.expected.id}
LIMIT 1
`;
if (rows[0]?.payload_json !== expectedJson) {
return {
committed: false as const,
storedEvents: [] as ReadonlyArray<OrchestrationV2StoredEvent>,
};
}

const normalized = yield* normalizeEvents(input.events);
const storedEvents = yield* eventStore.append({ events: normalized });
yield* applyStoredEvents(storedEvents);
return { committed: true as const, storedEvents };
}),
(result) => (result.committed ? publishStoredEvents(result.storedEvents) : Effect.void),
);
});

const existingCommandResult = (commandId: CommandId) =>
Effect.gen(function* () {
const existing = yield* commandReceipts.getByCommandId(commandId);
Expand Down Expand Up @@ -804,6 +854,12 @@ const layerBase: Layer.Layer<
}),
),
),
writeIfProviderThreadUnchanged: (input) =>
writeIfProviderThreadUnchangedEffect(input).pipe(
Effect.mapError(
(cause) => new EventSinkWriteError({ eventCount: input.events.length, cause }),
),
),
commitCommand: (input) =>
commitCommandEffect(input).pipe(
Effect.mapError(
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,8 @@ const encodeSubagentPayload = Schema.encodeEffect(
const encodeProviderSessionPayload = Schema.encodeEffect(
Schema.fromJsonString(OrchestrationV2ProviderSessionJsonSchema),
);
const encodeProviderThreadPayload = Schema.encodeEffect(
/** Encodes a provider thread exactly as its projection row stores `payload_json`. */
export const encodeProviderThreadPayload = Schema.encodeEffect(
Schema.fromJsonString(OrchestrationV2ProviderThreadJsonSchema),
);
const encodeProviderTurnPayload = Schema.encodeEffect(
Expand Down
Loading
Loading