Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit db4b2d8

Browse files
RusiruSadathanacodexjuliusmarminge
authored
perf(orchestration): speed up new-chat propagation and offline catch-up (pingdotgg#4177)
Co-authored-by: codex <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 2c199aa commit db4b2d8

7 files changed

Lines changed: 749 additions & 96 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ describe("OrchestrationEngine", () => {
218218
const runtime = ManagedRuntime.make(layer);
219219

220220
const engine = await runtime.runPromise(Effect.service(OrchestrationEngineService));
221+
expect(await runtime.runPromise(engine.latestSequence)).toBe(7);
221222
const result = await runtime.runPromise(
222223
engine.dispatch({
223224
type: "thread.meta.update",
@@ -228,6 +229,7 @@ describe("OrchestrationEngine", () => {
228229
);
229230

230231
expect(result.sequence).toBe(8);
232+
expect(await runtime.runPromise(engine.latestSequence)).toBe(8);
231233
expect(fullSnapshotReadCount).toBe(0);
232234

233235
await runtime.dispose();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ const makeOrchestrationEngine = Effect.gen(function* () {
329329
get streamDomainEvents(): OrchestrationEngineShape["streamDomainEvents"] {
330330
return Stream.fromPubSub(eventPubSub);
331331
},
332+
// The command read model's snapshotSequence tracks the latest committed
333+
// event sequence (updated on the worker fiber). A plain property read is a
334+
// consistent, committed value — reassignment of `commandReadModel` is
335+
// atomic on the single-threaded event loop.
336+
latestSequence: Effect.sync(() => commandReadModel.snapshotSequence),
332337
} satisfies OrchestrationEngineShape;
333338
});
334339

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ export interface OrchestrationEngineShape {
5656
* This is a hot runtime stream (new events only), not a historical replay.
5757
*/
5858
readonly streamDomainEvents: Stream.Stream<OrchestrationEvent>;
59+
60+
/**
61+
* The latest sequence reflected in the engine's authoritative command read
62+
* model (0 if none). Used to gauge how far behind a resuming client is before
63+
* choosing between an incremental replay and a fresh projected snapshot.
64+
*/
65+
readonly latestSequence: Effect.Effect<number, never, never>;
5966
}
6067

6168
/**

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
470470
readEvents: () => Stream.empty,
471471
dispatch: () => Effect.succeed({ sequence: 1 }),
472472
streamDomainEvents: Stream.fromQueue(events),
473+
latestSequence: Effect.succeed(0),
473474
} satisfies OrchestrationEngineShape;
474475

475476
const snapshotQuery = {
@@ -659,6 +660,7 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
659660
readEvents: () => Stream.empty,
660661
dispatch: () => Effect.succeed({ sequence: 1 }),
661662
streamDomainEvents: Stream.fromQueue(events),
663+
latestSequence: Effect.succeed(0),
662664
} satisfies OrchestrationEngineShape),
663665
Layer.succeed(ProjectionSnapshotQuery, {
664666
getShellSnapshot: () =>

0 commit comments

Comments
 (0)