Files
t3code/apps/server/src/orchestration/Services/OrchestrationEngine.ts
T
Theo BrowneandClaude Fable 5 bc23ec5073 perf: fix sustained-CPU hot paths from the July 2026 web performance audit
Addresses the audit's findings with minimal, desktop-first changes (no
payload deferral, no streaming-path work since streaming is off by default):

- Sidebar: replace the 10-thread bulk detail prewarm with a single
  hover-based prewarm; at idle one shell stream + at most one detail stream
- Server replay: thread catch-up reads only the thread's aggregate stream
  via the (aggregate_kind, stream_id, sequence) index instead of scanning
  the global log; shell catch-up is bounded (500 events) with a shell
  snapshot fallback for stale cursors
- Shell summaries: thread.message-sent updates latestUserMessageAt
  incrementally; activity-appended triggers the history-wide summary
  rebuild only for approval/user-input activity kinds; streaming assistant
  chunks no longer fan out per-chunk shell upserts
- Client runtime: O(1) activity appends when order is preserved; skip
  IndexedDB full-snapshot encodes while a turn is running (settle event and
  scope finalizer persist the final state)
- Timeline derivation: share one ordered activities view across the four
  derivations (identity-cached, O(n) sorted check); k-way merge for the
  combined timeline instead of concat+sort
- Composer drafts: debounce JSON serialization itself, and cache per-draft
  partialization by object identity so typing doesn't re-clone every
  retained draft
- Shiki: negative-cache languages without grammars (sh, yaml) so those
  fences stop retrying and throwing on every render
- Bootstrap polling: one shared change-detecting poller instead of one
  2s interval per consumer; minimap only writes data-in-view on change

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-09 04:09:08 -07:00

88 lines
3.2 KiB
TypeScript

/**
* OrchestrationEngineService - Service interface for orchestration command handling.
*
* Owns command validation/dispatch and in-memory read-model updates backed by
* `OrchestrationEventStore` persistence. It does not own provider process
* management or transport concerns (e.g. websocket request parsing).
*
* Uses Effect `Context.Service` for dependency injection. Command dispatch,
* replay, and unknown-input decoding all return typed domain errors.
*
* @module OrchestrationEngineService
*/
import type { OrchestrationCommand, OrchestrationEvent } from "@t3tools/contracts";
import * as Context from "effect/Context";
import type * as Effect from "effect/Effect";
import type * as Stream from "effect/Stream";
import type { OrchestrationDispatchError } from "../Errors.ts";
import type { OrchestrationEventStoreError } from "../../persistence/Errors.ts";
/**
* OrchestrationEngineShape - Service API for orchestration command and event flow.
*/
export interface OrchestrationEngineShape {
/**
* Replay persisted orchestration events from an exclusive sequence cursor.
*
* @param fromSequenceExclusive - Sequence cursor (exclusive).
* @param limit - Maximum number of events to read. Defaults to the event
* store's page-bounded default; pass a higher value when the caller must
* read every event after the cursor (e.g. per-thread catch-up that filters
* a small subset out of a potentially larger global range).
* @returns Stream containing ordered events.
*/
readonly readEvents: (
fromSequenceExclusive: number,
limit?: number,
) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError, never>;
/**
* Replay a single aggregate's persisted events from an exclusive sequence
* cursor. Reads via the aggregate stream index, so per-thread catch-up does
* not scan the global event range after the cursor.
*/
readonly readAggregateEvents: (
aggregateKind: OrchestrationEvent["aggregateKind"],
aggregateId: string,
fromSequenceExclusive: number,
limit?: number,
) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError, never>;
/**
* Dispatch a validated orchestration command.
*
* @param command - Valid orchestration command.
* @returns Effect containing the sequence of the persisted event.
*
* Dispatch is serialized through an internal queue and deduplicated via
* command receipts.
*/
readonly dispatch: (
command: OrchestrationCommand,
) => Effect.Effect<{ sequence: number }, OrchestrationDispatchError, never>;
/**
* Stream persisted domain events in dispatch order.
*
* This is a hot runtime stream (new events only), not a historical replay.
*/
readonly streamDomainEvents: Stream.Stream<OrchestrationEvent>;
}
/**
* OrchestrationEngineService - Service tag for orchestration engine access.
*
* @example
* ```ts
* const program = Effect.gen(function* () {
* const engine = yield* OrchestrationEngineService
* return yield* engine.dispatch(command)
* })
* ```
*/
export class OrchestrationEngineService extends Context.Service<
OrchestrationEngineService,
OrchestrationEngineShape
>()("t3/orchestration/Services/OrchestrationEngine/OrchestrationEngineService") {}