The best way to control Claude Code, Codex and more from any computer or phone
fix(server): creating a thread no longer records a failed projection read span (#37763)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Martin Bergo committed
Nov 16, 2026 at 22:16 UTC
e05b087a34b71a8fe14cc99d4f5b5041e1096e51
4 files changed
+47
-37
apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+17
-2
@@ -23,14 +23,15 @@ import {
23
import * as DateTime from "effect/DateTime";
24
import * as Deferred from "effect/Deferred";
25
import * as Effect from "effect/Effect";
26
+import * as Exit from "effect/Exit";
27
import * as Fiber from "effect/Fiber";
28
import * as Layer from "effect/Layer";
29
import * as Metric from "effect/Metric";
30
import * as Option from "effect/Option";
31
import * as Result from "effect/Result";
32
import * as Stream from "effect/Stream";
32
-import type * as Tracer from "effect/Tracer";
33
import * as TestClock from "effect/testing/TestClock";
34
+import * as Tracer from "effect/Tracer";
35
import * as SqlClient from "effect/sql/SqlClient";
36
import * as Statement from "effect/sql/Statement";
37
import {
@@ -1880,7 +1881,21 @@ it.effect.each([false, true])("rejects thread.create for an existing id (deleted
1881
ref: { driver: adapter.driver, nativeId: "native-original", strength: "strong" },
1882
},
1883
} as const;
1883
- yield* orchestrator.dispatch(create);
1884
+ const failedSpans: Array<string> = [];
1885
+ const tracer = Tracer.make({
1886
+ span(options) {
1887
+ const span = new Tracer.NativeSpan(options);
1888
+ const end = span.end.bind(span);
1889
+ span.end = (time, exit) => {
1890
+ end(time, exit);
1891
+ if (Exit.isFailure(exit)) failedSpans.push(span.name);
1892
+ };
1893
+ return span;
1894
+ },
1895
+ });
1896
+ yield* orchestrator.dispatch(create).pipe(Effect.withTracer(tracer));
1897
+ // The id check expects a miss; a failed span there reads as an error in Tempo.
1898
+ assert.deepEqual(failedSpans, []);
1899
if (deleted) {
1900
yield* orchestrator.dispatch({
1901
type: "thread.delete",
apps/server/src/orchestration-v2/Orchestrator.ts
+6
-19
@@ -1821,10 +1821,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1821
1822
/** Includes tombstones, which still reserve their id. */
1823
const findThread = (threadId: ThreadId) =>
1824
- projectionStore.getThread(threadId).pipe(
1825
- Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }),
1826
- Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })),
1827
- );
1824
+ projectionStore
1825
+ .findThread(threadId)
1826
+ .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
1827
1828
/** Side chats live inside their parent, so deleting the parent deletes them too. */
1829
const liveSideChatIds = Effect.fn("Orchestrator.liveSideChatIds")(
@@ -1839,11 +1838,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1838
const sideChatIds: Array<ThreadId> = [];
1839
for (const transfer of contextTransfers) {
1840
if (transfer.type !== "fork" || transfer.sourceThreadId !== threadId) continue;
1842
- const child = yield* projectionStore
1843
- .getThread(transfer.targetThreadId)
1844
- .pipe(
1845
- Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }),
1846
- );
1841
+ const child = yield* projectionStore.findThread(transfer.targetThreadId);
1842
if (child?.lineage.sideChat === true && child.deletedAt === null) {
1843
sideChatIds.push(child.id);
1844
}
@@ -3762,12 +3757,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
3757
});
3758
3759
// Tombstones reserve their ids too; recreating one would overwrite its history.
3765
- const existing = yield* projectionStore.getThread(command.threadId).pipe(
3766
- Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }),
3767
- Effect.mapError(
3768
- (cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
3769
- ),
3770
- );
3760
+ const existing = yield* findThread(command.threadId);
3761
if (existing !== null) {
3762
return yield* new OrchestratorThreadAlreadyExistsError({
3763
commandId: command.commandId,
@@ -5643,10 +5633,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
5633
if (runtimeModeCeiling === undefined) return;
5634
const runtimeModes = new Map<ThreadId, RuntimeMode>();
5635
for (const threadId of threadsStartingWork(events)) {
5646
- const thread = yield* projectionStore.getThread(threadId).pipe(
5647
- Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }),
5648
- Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })),
5649
- );
5636
+ const thread = yield* findThread(threadId);
5637
if (thread !== null) runtimeModes.set(threadId, thread.runtimeMode);
5638
}
5639
const refusal = runtimeModeCeilingRefusal(
apps/server/src/orchestration-v2/ProjectionStore.ts
+23
-16
@@ -625,6 +625,10 @@ export interface ProjectionStoreV2Shape {
625
readonly getThread: (
626
threadId: ThreadId,
627
) => Effect.Effect<OrchestrationV2AppThread, ProjectionStoreV2Error>;
628
+ /** Like getThread, but a missing thread is null, so an expected miss is not an error span. */
629
+ readonly findThread: (
630
+ threadId: ThreadId,
631
+ ) => Effect.Effect<OrchestrationV2AppThread | null, ProjectionStoreV2Error>;
632
readonly getLimitRecoveryCandidates: (options: {
633
readonly now: DateTime.Utc;
634
readonly autoResume: boolean;
@@ -5611,27 +5615,29 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5615
),
5616
);
5617
5618
+ const selectThread = (threadId: ThreadId) =>
5619
+ sql<PayloadRow>`
5620
+ SELECT payload_json FROM orchestration_v2_projection_threads
5621
+ WHERE thread_id = ${threadId}
5622
+ `.pipe(
5623
+ Effect.flatMap((rows) =>
5624
+ rows[0] === undefined ? Effect.succeed(null) : decodeThreadPayload(rows[0].payload_json),
5625
+ ),
5626
+ Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause })),
5627
+ );
5628
+
5629
const getThread: ProjectionStoreV2Shape["getThread"] = Effect.fn("ProjectionStore.getThread")(
5630
function* (threadId) {
5616
- const rows = yield* sql<PayloadRow>`
5617
- SELECT payload_json FROM orchestration_v2_projection_threads
5618
- WHERE thread_id = ${threadId}
5619
- `;
5620
- if (rows[0] === undefined) {
5621
- return yield* new ProjectionStoreThreadNotFoundError({ threadId });
5622
- }
5623
- return yield* decodeThreadPayload(rows[0].payload_json);
5631
+ const thread = yield* selectThread(threadId);
5632
+ if (thread === null) return yield* new ProjectionStoreThreadNotFoundError({ threadId });
5633
+ return thread;
5634
},
5625
- (effect, threadId) =>
5626
- effect.pipe(
5627
- Effect.mapError((cause) =>
5628
- isProjectionStoreThreadNotFoundError(cause)
5629
- ? cause
5630
- : new ProjectionStoreReadError({ threadId, cause }),
5631
- ),
5632
- ),
5635
);
5636
5637
+ const findThread: ProjectionStoreV2Shape["findThread"] = Effect.fn(
5638
+ "ProjectionStore.findThread",
5639
+ )(selectThread);
5640
+
5641
const requireThread = Effect.fn("ProjectionStore.requireThread")(function* (
5642
threadId: ThreadId,
5643
) {
@@ -9292,6 +9298,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
9298
getThreadShell: readOnReader(getThreadShell),
9299
findGuestShareThreadId: readOnReader(findGuestShareThreadId),
9300
getThread: readOnReader(getThread),
9301
+ findThread: readOnReader(findThread),
9302
getSettlementCandidates: readOnReader(getSettlementCandidates),
9303
getThreadsWithPullRequests: readOnReader(getThreadsWithPullRequests),
9304
getThreadTitles: readOnReader(getThreadTitles),
apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
+1
@@ -226,6 +226,7 @@ it.effect(
226
getThreadLinkMetadata: () => Effect.die("unused getThreadLinkMetadata"),
227
findGuestShareThreadId: () => Effect.die("unused findGuestShareThreadId"),
228
getThread: () => Ref.get(projection).pipe(Effect.map((state) => state.thread)),
229
+ findThread: () => Effect.die("unused findThread"),
230
getSettlementCandidates: () => Effect.die("unused getSettlementCandidates"),
231
getThreadsWithPullRequests: () => Effect.die("unused getThreadsWithPullRequests"),
232
getThreadWorktreePaths: () => Effect.die("unused getThreadWorktreePaths"),