Skip to content

Commit c420e9e

Browse files
Address review feedback on HTTP thread snapshot loading
- ws.ts (afterSequence resume): attach the live PubSub subscription into a scope-bound buffer BEFORE draining the catch-up replay, so events published during the replay window are not lost; read the full range after the cursor (readEvents limit) so the per-thread filter can't be starved by a global cap. - ProjectionSnapshotQuery: add getThreadDetailSnapshot that reads thread detail + snapshot sequence in one transaction, so the sequence is consistent with the returned state; use it in the HTTP handler and the WS snapshot path. - OrchestrationEngine.readEvents: accept an optional limit. - client loader: bound the HTTP snapshot timeout (cached data renders meanwhile) and treat a 404 as an expected defer-to-socket case rather than a warning. Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent fad44e1 commit c420e9e

13 files changed

Lines changed: 130 additions & 47 deletions

‎apps/server/src/checkpointing/CheckpointDiffQuery.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ describe("CheckpointDiffQuery.layer", () => {
107107
}),
108108
getThreadShellById: () => Effect.succeed(Option.none()),
109109
getThreadDetailById: () => Effect.succeed(Option.none()),
110+
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
110111
}),
111112
),
112113
);
@@ -199,6 +200,7 @@ describe("CheckpointDiffQuery.layer", () => {
199200
getFullThreadDiffContext: () => Effect.die("unused"),
200201
getThreadShellById: () => Effect.succeed(Option.none()),
201202
getThreadDetailById: () => Effect.succeed(Option.none()),
203+
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
202204
}),
203205
),
204206
);
@@ -281,6 +283,7 @@ describe("CheckpointDiffQuery.layer", () => {
281283
getFullThreadDiffContext: () => Effect.die("unused"),
282284
getThreadShellById: () => Effect.succeed(Option.none()),
283285
getThreadDetailById: () => Effect.succeed(Option.none()),
286+
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
284287
}),
285288
),
286289
);
@@ -348,6 +351,7 @@ describe("CheckpointDiffQuery.layer", () => {
348351
getFullThreadDiffContext: () => Effect.die("unused"),
349352
getThreadShellById: () => Effect.succeed(Option.none()),
350353
getThreadDetailById: () => Effect.succeed(Option.none()),
354+
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
351355
}),
352356
),
353357
);
@@ -400,6 +404,7 @@ describe("CheckpointDiffQuery.layer", () => {
400404
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
401405
getThreadShellById: () => Effect.succeed(Option.none()),
402406
getThreadDetailById: () => Effect.succeed(Option.none()),
407+
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
403408
}),
404409
),
405410
);

‎apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ describe("OrchestrationEngine", () => {
200200
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
201201
getThreadShellById: () => Effect.succeed(Option.none()),
202202
getThreadDetailById: () => Effect.succeed(Option.none()),
203+
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
203204
}),
204205
),
205206
Layer.provide(

‎apps/server/src/orchestration/Layers/OrchestrationEngine.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ const makeOrchestrationEngine = Effect.gen(function* () {
306306
Effect.annotateLogs({ sequence: commandReadModel.snapshotSequence }),
307307
);
308308

309-
const readEvents: OrchestrationEngineShape["readEvents"] = (fromSequenceExclusive) =>
310-
eventStore.readFromSequence(fromSequenceExclusive);
309+
const readEvents: OrchestrationEngineShape["readEvents"] = (fromSequenceExclusive, limit) =>
310+
eventStore.readFromSequence(fromSequenceExclusive, limit);
311311

312312
const dispatch: OrchestrationEngineShape["dispatch"] = (command) =>
313313
Effect.gen(function* () {

‎apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
OrchestrationReadModel,
1010
OrchestrationShellSnapshot,
1111
OrchestrationThread,
12+
OrchestrationThreadDetailSnapshot,
1213
ProjectScript,
1314
TurnId,
1415
type OrchestrationCheckpointSummary,
@@ -2033,6 +2034,35 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
20332034
);
20342035
});
20352036

2037+
const getThreadDetailSnapshot: ProjectionSnapshotQueryShape["getThreadDetailSnapshot"] = (
2038+
threadId,
2039+
) =>
2040+
// Read the thread detail and the snapshot sequence within a single
2041+
// transaction so the sequence is consistent with the returned state; a
2042+
// projector update landing between two separate reads could otherwise return
2043+
// a sequence ahead of the thread detail, causing the client to resume from
2044+
// too far and drop events.
2045+
sql
2046+
.withTransaction(
2047+
Effect.gen(function* () {
2048+
const thread = yield* getThreadDetailById(threadId);
2049+
if (Option.isNone(thread)) {
2050+
return Option.none<OrchestrationThreadDetailSnapshot>();
2051+
}
2052+
const { snapshotSequence } = yield* getSnapshotSequence();
2053+
return Option.some({ snapshotSequence, thread: thread.value });
2054+
}),
2055+
)
2056+
.pipe(
2057+
Effect.mapError((error) =>
2058+
isPersistenceError(error)
2059+
? error
2060+
: toPersistenceSqlError("ProjectionSnapshotQuery.getThreadDetailSnapshot:transaction")(
2061+
error,
2062+
),
2063+
),
2064+
);
2065+
20362066
return {
20372067
getCommandReadModel,
20382068
getSnapshot,
@@ -2047,6 +2077,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
20472077
getFullThreadDiffContext,
20482078
getThreadShellById,
20492079
getThreadDetailById,
2080+
getThreadDetailSnapshot,
20502081
} satisfies ProjectionSnapshotQueryShape;
20512082
});
20522083

‎apps/server/src/orchestration/Services/OrchestrationEngine.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ export interface OrchestrationEngineShape {
2626
* Replay persisted orchestration events from an exclusive sequence cursor.
2727
*
2828
* @param fromSequenceExclusive - Sequence cursor (exclusive).
29+
* @param limit - Maximum number of events to read. Defaults to the event
30+
* store's page-bounded default; pass a higher value when the caller must
31+
* read every event after the cursor (e.g. per-thread catch-up that filters
32+
* a small subset out of a potentially larger global range).
2933
* @returns Stream containing ordered events.
3034
*/
3135
readonly readEvents: (
3236
fromSequenceExclusive: number,
37+
limit?: number,
3338
) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError, never>;
3439

3540
/**

‎apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
OrchestrationReadModel,
1515
OrchestrationShellSnapshot,
1616
OrchestrationThread,
17+
OrchestrationThreadDetailSnapshot,
1718
OrchestrationThreadShell,
1819
ProjectId,
1920
ThreadId,
@@ -157,6 +158,16 @@ export interface ProjectionSnapshotQueryShape {
157158
readonly getThreadDetailById: (
158159
threadId: ThreadId,
159160
) => Effect.Effect<Option.Option<OrchestrationThread>, ProjectionRepositoryError>;
161+
162+
/**
163+
* Read a single active thread detail together with the projection snapshot
164+
* sequence in one consistent transaction, so the returned `snapshotSequence`
165+
* exactly matches the state reflected in `thread` (no interleaving projector
166+
* update between the two reads).
167+
*/
168+
readonly getThreadDetailSnapshot: (
169+
threadId: ThreadId,
170+
) => Effect.Effect<Option.Option<OrchestrationThreadDetailSnapshot>, ProjectionRepositoryError>;
160171
}
161172

162173
/**

‎apps/server/src/orchestration/http.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,17 @@ export const orchestrationHttpApiLayer = HttpApiBuilder.group(
4545
Effect.fn("environment.orchestration.threadSnapshot")(function* (args) {
4646
yield* annotateEnvironmentRequest(args.endpoint.name);
4747
yield* requireEnvironmentScope(AuthOrchestrationReadScope);
48-
const [threadDetail, { snapshotSequence }] = yield* Effect.all([
49-
projectionSnapshotQuery.getThreadDetailById(args.params.threadId),
50-
projectionSnapshotQuery.getSnapshotSequence(),
51-
]).pipe(
52-
Effect.catch((cause) =>
53-
failEnvironmentInternal("orchestration_thread_snapshot_failed", cause),
54-
),
55-
);
56-
if (Option.isNone(threadDetail)) {
48+
const snapshot = yield* projectionSnapshotQuery
49+
.getThreadDetailSnapshot(args.params.threadId)
50+
.pipe(
51+
Effect.catch((cause) =>
52+
failEnvironmentInternal("orchestration_thread_snapshot_failed", cause),
53+
),
54+
);
55+
if (Option.isNone(snapshot)) {
5756
return yield* failEnvironmentNotFound("thread_not_found");
5857
}
59-
return { snapshotSequence, thread: threadDetail.value };
58+
return snapshot.value;
6059
}),
6160
)
6261
.handle(

‎apps/server/src/project/ProjectSetupScriptRunner.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const makeProjectionSnapshotQueryLayer = (project: OrchestrationProject) =>
4343
getFullThreadDiffContext: () => Effect.die("unused"),
4444
getThreadShellById: () => Effect.die("unused"),
4545
getThreadDetailById: () => Effect.die("unused"),
46+
getThreadDetailSnapshot: () => Effect.die("unused"),
4647
});
4748

4849
const makeTerminalManagerLayer = (

‎apps/server/src/provider/Layers/ProviderSessionReaper.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ describe("ProviderSessionReaper", () => {
209209
: Option.none(),
210210
),
211211
getThreadDetailById: () => Effect.die("unused"),
212+
getThreadDetailSnapshot: () => Effect.die("unused"),
212213
}),
213214
),
214215
Layer.provideMerge(NodeServices.layer),

‎apps/server/src/server.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ const buildAppUnderTest = (options?: {
702702
getProjectShellById: () => Effect.succeed(Option.none()),
703703
getThreadShellById: () => Effect.succeed(Option.none()),
704704
getThreadDetailById: () => Effect.succeed(Option.none()),
705+
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
705706
getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
706707
getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
707708
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),

0 commit comments

Comments
 (0)