diff --git a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts index a8a86b89a4..b5e8a4f8b7 100644 --- a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts +++ b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts @@ -316,6 +316,86 @@ describe("OrchestrationEngine", () => { await system.dispose(); }); + it("treats normalized thread.turn.start as a prioritized client command", async () => { + const system = await createOrchestrationSystem(); + const { engine } = system; + const createdAt = now(); + const threadId = ThreadId.makeUnsafe("thread-priority-turn-start"); + + await system.run( + engine.dispatch({ + type: "project.create", + commandId: CommandId.makeUnsafe("cmd-project-priority-turn-start-create"), + projectId: asProjectId("project-priority-turn-start"), + title: "Priority Turn Start Project", + workspaceRoot: "/tmp/project-priority-turn-start", + defaultModelSelection: { + provider: "codex", + model: "gpt-5-codex", + }, + createdAt, + }), + ); + await system.run( + engine.dispatch({ + type: "thread.create", + commandId: CommandId.makeUnsafe("cmd-thread-priority-turn-start-create"), + threadId, + projectId: asProjectId("project-priority-turn-start"), + title: "Priority Turn Start Thread", + modelSelection: { + provider: "codex", + model: "gpt-5-codex", + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "full-access", + branch: null, + worktreePath: null, + createdAt, + }), + ); + + const lowPriorityCommandCount = 150; + const lowPriorityDispatches = Array.from({ length: lowPriorityCommandCount }, (_, index) => + system.run( + engine.dispatch({ + type: "thread.message.assistant.delta", + commandId: CommandId.makeUnsafe(`cmd-thread-priority-turn-start-delta-${index}`), + threadId, + messageId: asMessageId("assistant-priority-turn-start"), + delta: `chunk-${index}`, + turnId: asTurnId("turn-priority-turn-start-internal"), + createdAt, + }), + ), + ); + + const turnStartResult = await system.run( + engine.dispatch({ + type: "thread.turn.start", + commandId: CommandId.makeUnsafe("cmd-thread-priority-turn-start"), + threadId, + message: { + messageId: asMessageId("user-priority-turn-start"), + role: "user", + text: "hello", + attachments: [], + }, + interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE, + runtimeMode: "full-access", + createdAt, + }), + ); + const lowPriorityResults = await Promise.all(lowPriorityDispatches); + const lowPriorityCommandsAheadOfTurnStart = lowPriorityResults.filter( + (result) => result.sequence < turnStartResult.sequence, + ).length; + + expect(lowPriorityCommandsAheadOfTurnStart).toBeLessThan(lowPriorityCommandCount / 3); + expect(turnStartResult.sequence).toBeLessThan(lowPriorityResults.at(-1)?.sequence ?? Infinity); + await system.dispose(); + }); + it("streams persisted domain events in order", async () => { const system = await createOrchestrationSystem(); const { engine } = system; diff --git a/apps/server/src/orchestration/Layers/OrchestrationEngine.ts b/apps/server/src/orchestration/Layers/OrchestrationEngine.ts index accbd4a015..97388783a5 100644 --- a/apps/server/src/orchestration/Layers/OrchestrationEngine.ts +++ b/apps/server/src/orchestration/Layers/OrchestrationEngine.ts @@ -4,7 +4,7 @@ import type { ProjectId, ThreadId, } from "@t3tools/contracts"; -import { OrchestrationCommand } from "@t3tools/contracts"; +import { DispatchableClientOrchestrationCommand, OrchestrationCommand } from "@t3tools/contracts"; import { Deferred, Effect, @@ -78,18 +78,10 @@ function commandToAggregateRef(command: OrchestrationCommand): { } function commandPriority(command: OrchestrationCommand): CommandPriority { - switch (command.type) { - case "thread.session.set": - case "thread.message.assistant.delta": - case "thread.message.assistant.complete": - case "thread.proposed-plan.upsert": - case "thread.turn.diff.complete": - case "thread.activity.append": - case "thread.revert.complete": - return COMMAND_PRIORITY.stream; - default: - return COMMAND_PRIORITY.control; + if (Schema.is(DispatchableClientOrchestrationCommand)(command)) { + return COMMAND_PRIORITY.control; } + return COMMAND_PRIORITY.stream; } const makeOrchestrationEngine = Effect.gen(function* () { diff --git a/packages/contracts/src/orchestration.ts b/packages/contracts/src/orchestration.ts index bd7e374f32..e48b3fb7b7 100644 --- a/packages/contracts/src/orchestration.ts +++ b/packages/contracts/src/orchestration.ts @@ -463,7 +463,7 @@ const ThreadSessionStopCommand = Schema.Struct({ createdAt: IsoDateTime, }); -const DispatchableClientOrchestrationCommand = Schema.Union([ +export const DispatchableClientOrchestrationCommand = Schema.Union([ ProjectCreateCommand, ProjectMetaUpdateCommand, ProjectDeleteCommand, diff --git a/performance.md b/performance.md index 01ea2391c8..5a25177c24 100644 --- a/performance.md +++ b/performance.md @@ -34,7 +34,24 @@ Measure end-to-end server latency for control-plane commands through the real we | `t3code/perf-control-plane-priority-lane` | Spam canary rerun | completed | [control-plane-stream-baseline.json](/Users/julius/.t3/worktrees/codething-mvp/t3code-93392e80/artifacts/perf/server/server-latency-critical-commands-burst_base-1775121334761/control-plane-stream-baseline.json) | | `t3code/perf-control-plane-priority-lane` | Terminal + mixed git rerun | completed | [terminal-mixed-git-baseline.json](/Users/julius/.t3/worktrees/codething-mvp/t3code-93392e80/artifacts/perf/server/server-latency-critical-commands-burst_base-1775121336380/terminal-mixed-git-baseline.json) | -## Results +## Best So Far + +- `create-turn-spam-8x` `thread.create dispatch -> thread.created` + Baseline: `3.61ms / 58.38ms` + Best so far: `2.38ms / 20.36ms` on `t3code/perf-projection-thread-message-hot-path` + Tail improvement: `-65%` on p95 +- `create-turn-spam-8x` `thread.archive dispatch -> thread.archived` + Baseline: `5.71ms / 50.80ms` + Best so far: `1.09ms / 12.60ms` on `t3code/perf-control-plane-priority-lane` + Tail improvement: `-75%` on p95 +- `mixed-stream-terminal-git` `thread.create dispatch -> thread.created` + Baseline: `0.89ms / 1.57ms` + Best so far: `1.16ms / 1.33ms` on `t3code/perf-control-plane-priority-lane` + Tail improvement: `-15%` on p95 +- `git.status` + No clear improvement yet. The measurements move around, but there is no stable win I would claim from these changes. + +## Detailed Results | Branch | Change | Profile | Metric | Before p50 / p95 | After p50 / p95 | Notes | | ------------------------------------------------ | -------------- | --------------------------- | -------------------------------------------- | --------------------- | --------------------- | ---------------------------------------------------------------------------------- |