From e58a5cef6964e204e345d2a323da1541357b60b5 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 30 Jul 2026 16:04:54 +0200 Subject: [PATCH] fix(relay): stop replaying the whole event store into the awareness relay MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Live profiling of the built app (debugger pause inside the hot sqlite call) showed AgentAwarenessRelay.publishThread grinding at ~8 full shell snapshots per second after boot. Two causes: - streamDomainEvents was eventSink.stream() with no cursor, which replays every stored event from genesis before going live — so each boot fed the relay the entire event history as fresh activity, and it queued a publish per activity-relevant event for hours. It now tails from the current high-water mark; the relay's startup snapshot publish already covers initial state, and the sink subscribes before reading the cursor so no live event is lost. - publishThreadUnsafe materialized the full shell snapshot to find one thread; it now uses getThreadShell (archived/deleted map to the tombstone path exactly as the active-list lookup did). Co-Authored-By: Claude Fable 5 --- apps/server/src/orchestration-v2/Orchestrator.ts | 10 +++++++++- apps/server/src/relay/AgentAwarenessRelay.ts | 11 ++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 7d494ca89a..c43de6f1d5 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -6028,7 +6028,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio }), ), ), - streamDomainEvents: eventSink.stream().pipe( + // Live tail only. eventSink.stream() with no cursor replays the whole + // store from genesis first; domain-event subscribers (the awareness relay) + // react to new activity, and startup replay made them grind through the + // entire event history doing per-event work after every boot. + streamDomainEvents: Stream.unwrap( + eventSink + .latestSequence() + .pipe(Effect.map((latest) => eventSink.stream({ afterSequence: latest }))), + ).pipe( Stream.map((stored) => stored.event), Stream.mapError( (cause) => diff --git a/apps/server/src/relay/AgentAwarenessRelay.ts b/apps/server/src/relay/AgentAwarenessRelay.ts index 99161b7bc7..2613037cac 100644 --- a/apps/server/src/relay/AgentAwarenessRelay.ts +++ b/apps/server/src/relay/AgentAwarenessRelay.ts @@ -373,9 +373,14 @@ export const make = Effect.gen(function* () { }); }); - const shell = yield* threads.getShellSnapshot(); - const threadValue = shell.threads.find((candidate) => candidate.id === threadId); - const thread = threadValue === undefined ? Option.none() : Option.some(threadValue); + // Per-thread shell read: this publish runs for every activity-relevant + // domain event, so materializing the full shell here would make the cost + // of one thread's activity proportional to how many threads exist. + const threadShell = yield* threads.getThreadShell(threadId); + const thread = + threadShell === null || threadShell.archivedAt !== null + ? Option.none() + : Option.some(threadShell); const project = Option.isSome(thread) ? yield* projects.getById(thread.value.projectId) : Option.none();