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();