fix(relay): stop replaying the whole event store into the awareness relay

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 <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Fable 5 committed 2026-07-30 16:04:54 +02:00
1 parent 1c24c650c7
commit e58a5cef69
2 files changed
+17 -4

No files matched your search

@@ -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) =>
+8 -3
View File
@@ -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<OrchestrationV2ThreadShell>()
: Option.some(threadShell);
const project = Option.isSome(thread)
? yield* projects.getById(thread.value.projectId)
: Option.none<Project>();