Skip to content

Commit 519d64f

Browse files
t3dotggclaude
andauthored
fix(server): settled and idle-released threads no longer stay Waiting (pingdotgg#19355)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent adb0420 commit 519d64f

7 files changed

Lines changed: 448 additions & 7 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9065,6 +9065,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
90659065
getModelContextWindow: (selection) =>
90669066
resolveClaudeCatalogContextWindowTokens(BUNDLED_CLAUDE_MODEL_CATALOG, selection),
90679067
events: Stream.fromEffectRepeat(Queue.take(events)),
9068+
// Background shells, agents, monitors, and workflows end with the CLI.
9069+
backgroundTasksEndWithProcess: true,
90689070
hasPendingBackgroundWork: Effect.gen(function* () {
90699071
// Session capability: any native thread with pending work pins idle.
90709072
for (const roster of (yield* Ref.get(pendingBackgroundTasksByNativeThread)).values()) {

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
CommandId,
33
type OrchestrationV2Run,
44
OrchestrationV2DomainEvent,
5+
type OrchestrationV2ProviderThread,
56
OrchestrationV2StoredEvent,
67
ProviderThreadId,
78
RunAttemptId,
@@ -119,6 +120,22 @@ export interface EventSinkV2Shape {
119120
},
120121
EventSinkV2Error
121122
>;
123+
/**
124+
* Atomically commit only while the stored provider thread still equals
125+
* `expected`, the row its writer read, so a read-modify-write of the row
126+
* cannot overwrite an update published in between. The whole row is
127+
* compared because not every writer moves `updatedAt`.
128+
*/
129+
readonly writeIfProviderThreadUnchanged: (input: {
130+
readonly expected: OrchestrationV2ProviderThread;
131+
readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
132+
}) => Effect.Effect<
133+
{
134+
readonly committed: boolean;
135+
readonly storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>;
136+
},
137+
EventSinkV2Error
138+
>;
122139
readonly commitCommand: (input: {
123140
readonly commandId: CommandId;
124141
readonly threadId: ThreadId;
@@ -509,6 +526,39 @@ const layerBase: Layer.Layer<
509526
);
510527
});
511528

529+
const writeIfProviderThreadUnchangedEffect = Effect.fn(
530+
"orchestrationV2.EventSink.writeIfProviderThreadUnchanged",
531+
)(function* (input: Parameters<EventSinkV2Shape["writeIfProviderThreadUnchanged"]>[0]) {
532+
yield* Effect.annotateCurrentSpan({
533+
"orchestration_v2.event_count": input.events.length,
534+
"orchestration_v2.provider_thread_id": input.expected.id,
535+
});
536+
const expectedJson = yield* ProjectionStore.encodeProviderThreadPayload(input.expected);
537+
538+
return yield* commitThenPublish(
539+
Effect.gen(function* () {
540+
const rows = yield* sql<{ readonly payload_json: string }>`
541+
SELECT payload_json
542+
FROM orchestration_v2_projection_provider_threads
543+
WHERE provider_thread_id = ${input.expected.id}
544+
LIMIT 1
545+
`;
546+
if (rows[0]?.payload_json !== expectedJson) {
547+
return {
548+
committed: false as const,
549+
storedEvents: [] as ReadonlyArray<OrchestrationV2StoredEvent>,
550+
};
551+
}
552+
553+
const normalized = yield* normalizeEvents(input.events);
554+
const storedEvents = yield* eventStore.append({ events: normalized });
555+
yield* applyStoredEvents(storedEvents);
556+
return { committed: true as const, storedEvents };
557+
}),
558+
(result) => (result.committed ? publishStoredEvents(result.storedEvents) : Effect.void),
559+
);
560+
});
561+
512562
const existingCommandResult = (commandId: CommandId) =>
513563
Effect.gen(function* () {
514564
const existing = yield* commandReceipts.getByCommandId(commandId);
@@ -804,6 +854,12 @@ const layerBase: Layer.Layer<
804854
}),
805855
),
806856
),
857+
writeIfProviderThreadUnchanged: (input) =>
858+
writeIfProviderThreadUnchangedEffect(input).pipe(
859+
Effect.mapError(
860+
(cause) => new EventSinkWriteError({ eventCount: input.events.length, cause }),
861+
),
862+
),
807863
commitCommand: (input) =>
808864
commitCommandEffect(input).pipe(
809865
Effect.mapError(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,8 @@ const encodeSubagentPayload = Schema.encodeEffect(
10561056
const encodeProviderSessionPayload = Schema.encodeEffect(
10571057
Schema.fromJsonString(OrchestrationV2ProviderSessionJsonSchema),
10581058
);
1059-
const encodeProviderThreadPayload = Schema.encodeEffect(
1059+
/** Encodes a provider thread exactly as its projection row stores `payload_json`. */
1060+
export const encodeProviderThreadPayload = Schema.encodeEffect(
10601061
Schema.fromJsonString(OrchestrationV2ProviderThreadJsonSchema),
10611062
);
10621063
const encodeProviderTurnPayload = Schema.encodeEffect(

0 commit comments

Comments
 (0)