Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

perf(server): stop loading the whole database on hot orchestration paths (#297)

Leo01161ce529aa
diff --git a/.changeset/perf-server-hot-paths.md b/.changeset/perf-server-hot-paths.md
new file mode 100644
index 000000000..d1167712f
--- /dev/null
+++ b/.changeset/perf-server-hot-paths.md
@@ -0,0 +1,5 @@
+---
+"akeru-bot": patch
+---
+
+Faster chats on busy environments: the server no longer reloads the whole database when a bot session starts or a checkpoint is captured, and it stops re-reading secrets while a bot is replying.
diff --git a/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts b/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts
index 56366271a..6ced7c1ed 100644
--- a/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts
+++ b/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts
@@ -549,6 +549,89 @@ describe("CheckpointReactor", () => {
).toBe("v2\n");
});
+ it("links a completion checkpoint to the newest assistant message of its turn", async () => {
+ const harness = await createHarness({ seedFilesystemCheckpoints: false });
+ const threadId = ThreadId.make("thread-1");
+ const createdAt = "2026-01-01T00:00:00.000Z";
+
+ const assistantMessages = [
+ { id: "assistant-turn-1-first", turnId: "turn-1", at: "2026-01-01T00:00:01.000Z" },
+ { id: "assistant-turn-1-latest", turnId: "turn-1", at: "2026-01-01T00:00:02.000Z" },
+ { id: "assistant-other-turn", turnId: "turn-other", at: "2026-01-01T00:00:03.000Z" },
+ ] as const;
+ await Effect.runPromise(
+ Effect.gen(function* () {
+ yield* harness.engine.dispatch({
+ type: "thread.session.set",
+ commandId: CommandId.make("cmd-session-set-assistant-link"),
+ threadId,
+ session: {
+ threadId,
+ status: "ready",
+ providerName: "codex",
+ runtimeMode: "approval-required",
+ activeTurnId: null,
+ lastError: null,
+ updatedAt: createdAt,
+ },
+ createdAt,
+ });
+ harness.provider.emit({
+ type: "turn.started",
+ eventId: EventId.make("evt-turn-started-assistant-link"),
+ provider: ProviderDriverKind.make("codex"),
+ createdAt,
+ threadId,
+ turnId: asTurnId("turn-1"),
+ });
+ yield* Effect.promise(() =>
+ waitForGitRefExists(harness.cwd, checkpointRefForThreadTurn(threadId, 0)),
+ );
+ for (const message of assistantMessages) {
+ yield* harness.engine.dispatch({
+ type: "thread.message.assistant.delta",
+ commandId: CommandId.make(`cmd-delta-${message.id}`),
+ threadId,
+ messageId: MessageId.make(message.id),
+ delta: message.id,
+ turnId: asTurnId(message.turnId),
+ createdAt: message.at,
+ });
+ yield* harness.engine.dispatch({
+ type: "thread.message.assistant.complete",
+ commandId: CommandId.make(`cmd-complete-${message.id}`),
+ threadId,
+ messageId: MessageId.make(message.id),
+ turnId: asTurnId(message.turnId),
+ createdAt: message.at,
+ });
+ }
+ }),
+ );
+
+ NodeFS.writeFileSync(NodePath.join(harness.cwd, "README.md"), "v2\n", "utf8");
+ harness.provider.emit({
+ type: "turn.completed",
+ eventId: EventId.make("evt-turn-completed-assistant-link"),
+ provider: ProviderDriverKind.make("codex"),
+ createdAt: "2026-01-01T00:00:04.000Z",
+ threadId,
+ turnId: asTurnId("turn-1"),
+ payload: { state: "completed" },
+ });
+
+ const events = await waitForEvent(
+ harness.engine,
+ (event) => event.type === "thread.turn-diff-completed",
+ );
+ const diffCompleted = events.find((event) => event.type === "thread.turn-diff-completed");
+ expect(
+ diffCompleted?.type === "thread.turn-diff-completed"
+ ? diffCompleted.payload.assistantMessageId
+ : undefined,
+ ).toBe("assistant-turn-1-latest");
+ });
+
it("refreshes local git status state on turn completion using the session cwd", async () => {
const gitStatusRefreshCalls: string[] = [];
const harness = await createHarness({
diff --git a/apps/server/src/orchestration/Layers/CheckpointReactor.ts b/apps/server/src/orchestration/Layers/CheckpointReactor.ts
index 1ffebe906..093309273 100644
--- a/apps/server/src/orchestration/Layers/CheckpointReactor.ts
+++ b/apps/server/src/orchestration/Layers/CheckpointReactor.ts
@@ -163,19 +163,48 @@ const make = Effect.gen(function* () {
: Option.none();
});
- const resolveThreadDetail = Effect.fn("resolveThreadDetail")(function* (threadId: ThreadId) {
- return yield* projectionSnapshotQuery
- .getThreadDetailById(threadId, { activityKinds: [] })
+ // Reads only what checkpointing needs for an active thread: its session,
+ // workspace location, and checkpoint summaries. Message history stays in SQLite.
+ const resolveThreadCheckpointState = Effect.fn("resolveThreadCheckpointState")(function* (
+ threadId: ThreadId,
+ ) {
+ const runtime = yield* projectionSnapshotQuery
+ .getThreadRuntimeContext(threadId)
.pipe(Effect.map(Option.getOrUndefined));
+ if (!runtime) {
+ return undefined;
+ }
+ const context = yield* projectionSnapshotQuery
+ .getThreadCheckpointContext(threadId)
+ .pipe(Effect.map(Option.getOrUndefined));
+ if (!context) {
+ return undefined;
+ }
+ return {
+ id: context.threadId,
+ projectId: context.projectId,
+ worktreePath: context.worktreePath,
+ checkpoints: context.checkpoints,
+ session: runtime.session,
+ projects: [{ id: context.projectId, workspaceRoot: context.workspaceRoot }],
+ };
});
- const resolveThreadProjects = Effect.fn("resolveThreadProjects")(function* (
- projectId: ProjectId,
+ const resolveTurnAssistantMessageId = Effect.fn("resolveTurnAssistantMessageId")(function* (
+ threadId: ThreadId,
+ turnId: TurnId,
) {
- const project = yield* projectionSnapshotQuery
- .getProjectShellById(projectId)
+ if (projectionSnapshotQuery.getLatestAssistantMessageIdForTurn) {
+ return Option.getOrUndefined(
+ yield* projectionSnapshotQuery.getLatestAssistantMessageIdForTurn(threadId, turnId),
+ );
+ }
+ const thread = yield* projectionSnapshotQuery
+ .getThreadDetailById(threadId, { activityKinds: [] })
.pipe(Effect.map(Option.getOrUndefined));
- return project ? [project] : [];
+ return thread?.messages
+ .toReversed()
+ .find((entry) => entry.role === "assistant" && entry.turnId === turnId)?.id;
});
// Resolves the workspace CWD for checkpoint operations, preferring the
@@ -220,13 +249,6 @@ const make = Effect.gen(function* () {
const captureAndDispatchCheckpoint = Effect.fn("captureAndDispatchCheckpoint")(function* (input: {
readonly threadId: ThreadId;
readonly turnId: TurnId;
- readonly thread: {
- readonly messages: ReadonlyArray<{
- readonly id: MessageId;
- readonly role: string;
- readonly turnId: TurnId | null;
- }>;
- };
readonly cwd: string;
readonly turnCount: number;
readonly status: "ready" | "missing" | "error";
@@ -301,9 +323,7 @@ const make = Effect.gen(function* () {
const assistantMessageId =
input.assistantMessageId ??
- input.thread.messages
- .toReversed()
- .find((entry) => entry.role === "assistant" && entry.turnId === input.turnId)?.id ??
+ (yield* resolveTurnAssistantMessageId(input.threadId, input.turnId)) ??
MessageId.make(`assistant:${input.turnId}`);
yield* orchestrationEngine.dispatch({
@@ -364,7 +384,7 @@ const make = Effect.gen(function* () {
return;
}
- const thread = yield* resolveThreadDetail(event.threadId);
+ const thread = yield* resolveThreadCheckpointState(event.threadId);
if (!thread) {
return;
}
@@ -385,7 +405,7 @@ const make = Effect.gen(function* () {
return;
}
- const projects = yield* resolveThreadProjects(thread.projectId);
+ const projects = thread.projects;
const checkpointCwd = yield* resolveCheckpointCwd({
threadId: thread.id,
thread,
@@ -412,7 +432,6 @@ const make = Effect.gen(function* () {
yield* captureAndDispatchCheckpoint({
threadId: thread.id,
turnId,
- thread,
cwd: checkpointCwd,
turnCount: nextTurnCount,
status:
@@ -432,12 +451,12 @@ const make = Effect.gen(function* () {
return;
}
- const thread = yield* resolveThreadDetail(event.threadId);
+ const thread = yield* resolveThreadCheckpointState(event.threadId);
if (!thread) {
return;
}
- const projects = yield* resolveThreadProjects(thread.projectId);
+ const projects = thread.projects;
const checkpointCwd = yield* resolveCheckpointCwd({
threadId: thread.id,
thread,
@@ -609,12 +628,12 @@ const make = Effect.gen(function* () {
}
const threadId = event.payload.threadId;
- const thread = yield* resolveThreadDetail(threadId);
+ const thread = yield* resolveThreadCheckpointState(threadId);
if (!thread) {
return;
}
- const projects = yield* resolveThreadProjects(thread.projectId);
+ const projects = thread.projects;
const checkpointCwd = yield* resolveCheckpointCwd({
threadId,
thread,
@@ -656,7 +675,7 @@ const make = Effect.gen(function* () {
) {
const now = DateTime.formatIso(yield* DateTime.now);
- const thread = yield* resolveThreadDetail(event.payload.threadId);
+ const thread = yield* resolveThreadCheckpointState(event.payload.threadId);
if (!thread) {
yield* appendRevertFailureActivity({
threadId: event.payload.threadId,
@@ -829,7 +848,9 @@ const make = Effect.gen(function* () {
if (event.type === "turn.completed" || event.type === "turn.aborted") {
const turnId = toTurnId(event.turnId);
- const thread = yield* resolveThreadDetail(event.threadId);
+ const thread = yield* projectionSnapshotQuery
+ .getThreadRuntimeContext(event.threadId)
+ .pipe(Effect.map(Option.getOrUndefined));
const startedTurnId = startedTurns.get(event.threadId);
const isTrackedTurn = sameId(startedTurnId, turnId);
if (isTrackedTurn) startedTurns.delete(event.threadId);
diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
index c1e1f6741..9527d65ad 100644
--- a/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
@@ -2984,3 +2984,151 @@ engineLayer("OrchestrationProjectionPipeline via engine dispatch", (it) => {
}),
);
});
+
+it.layer(Layer.fresh(makeProjectionPipelinePrefixedTestLayer("t3-projection-skip-")))(
+ "OrchestrationProjectionPipeline projector skipping",
+ (it) => {
+ it.effect("advances every projector cursor while skipping unrelated projectors", () =>
+ Effect.gen(function* () {
+ const projectionPipeline = yield* OrchestrationProjectionPipeline;
+ const eventStore = yield* OrchestrationEventStore;
+ const sql = yield* SqlClient.SqlClient;
+ const now = "2026-01-01T00:00:00.000Z";
+ const later = "2026-01-01T00:00:05.000Z";
+
+ const appendAndProject = (event: Parameters<typeof eventStore.append>[0]) =>
+ eventStore.append(event).pipe(Effect.tap(projectionPipeline.projectEvent));
+
+ yield* appendAndProject({
+ type: "project.created",
+ eventId: EventId.make("evt-skip-1"),
+ aggregateKind: "project",
+ aggregateId: ProjectId.make("project-skip"),
+ occurredAt: now,
+ commandId: CommandId.make("cmd-skip-1"),
+ causationEventId: null,
+ correlationId: CorrelationId.make("cmd-skip-1"),
+ metadata: {},
+ payload: {
+ projectId: ProjectId.make("project-skip"),
+ title: "Skip Project",
+ workspaceRoot: "/tmp/project-skip",
+ defaultModelSelection: null,
+ scripts: [],
+ createdAt: now,
+ updatedAt: now,
+ },
+ });
+ yield* appendAndProject({
+ type: "thread.created",
+ eventId: EventId.make("evt-skip-2"),
+ aggregateKind: "thread",
+ aggregateId: ThreadId.make("thread-skip"),
+ occurredAt: now,
+ commandId: CommandId.make("cmd-skip-2"),
+ causationEventId: null,
+ correlationId: CorrelationId.make("cmd-skip-2"),
+ metadata: {},
+ payload: {
+ threadId: ThreadId.make("thread-skip"),
+ projectId: ProjectId.make("project-skip"),
+ title: "Skip Thread",
+ modelSelection: {
+ instanceId: ProviderInstanceId.make("codex"),
+ model: "gpt-5-codex",
+ },
+ runtimeMode: "full-access",
+ branch: null,
+ worktreePath: null,
+ createdAt: now,
+ updatedAt: now,
+ },
+ });
+ yield* appendAndProject({
+ type: "thread.message-sent",
+ eventId: EventId.make("evt-skip-3"),
+ aggregateKind: "thread",
+ aggregateId: ThreadId.make("thread-skip"),
+ occurredAt: now,
+ commandId: CommandId.make("cmd-skip-3"),
+ causationEventId: null,
+ correlationId: CorrelationId.make("cmd-skip-3"),
+ metadata: {},
+ payload: {
+ threadId: ThreadId.make("thread-skip"),
+ messageId: MessageId.make("message-skip"),
+ role: "assistant",
+ text: "kept",
+ turnId: null,
+ streaming: false,
+ createdAt: now,
+ updatedAt: now,
+ },
+ });
+ // Only the projects projector handles this event; every other projector
+ // must skip it yet still record the sequence.
+ const lastEvent = yield* appendAndProject({
+ type: "project.meta-updated",
+ eventId: EventId.make("evt-skip-4"),
+ aggregateKind: "project",
+ aggregateId: ProjectId.make("project-skip"),
+ occurredAt: later,
+ commandId: CommandId.make("cmd-skip-4"),
+ causationEventId: null,
+ correlationId: CorrelationId.make("cmd-skip-4"),
+ metadata: {},
+ payload: {
+ projectId: ProjectId.make("project-skip"),
+ title: "Renamed Project",
+ updatedAt: later,
+ },
+ });
+
+ const readStateRows = sql<{
+ readonly projector: string;
+ readonly lastAppliedSequence: number;
+ readonly updatedAt: string;
+ }>`
+ SELECT
+ projector,
+ last_applied_sequence AS "lastAppliedSequence",
+ updated_at AS "updatedAt"
+ FROM projection_state
+ ORDER BY projector ASC
+ `;
+ const stateRows = yield* readStateRows;
+ assert.deepEqual(
+ stateRows.map((row) => row.projector),
+ Object.values(ORCHESTRATION_PROJECTOR_NAMES).toSorted(),
+ );
+ for (const row of stateRows) {
+ assert.equal(row.lastAppliedSequence, lastEvent.sequence);
+ assert.equal(row.updatedAt, later);
+ }
+
+ const readRows = Effect.all({
+ projects: sql<{ readonly title: string }>`
+ SELECT title FROM projection_projects WHERE project_id = 'project-skip'
+ `,
+ messages: sql<{ readonly text: string }>`
+ SELECT text FROM projection_thread_messages WHERE thread_id = 'thread-skip'
+ `,
+ threads: sql<{ readonly title: string }>`
+ SELECT title FROM projection_threads WHERE thread_id = 'thread-skip'
+ `,
+ });
+ const rows = yield* readRows;
+ assert.deepEqual(rows, {
+ projects: [{ title: "Renamed Project" }],
+ messages: [{ text: "kept" }],
+ threads: [{ title: "Skip Thread" }],
+ });
+
+ // A restart resumes from the shared cursor without replaying anything.
+ yield* projectionPipeline.bootstrap;
+ assert.deepEqual(yield* readStateRows, stateRows);
+ assert.deepEqual(yield* readRows, rows);
+ }),
+ );
+ },
+);
diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
index cc0eff232..a637359c4 100644
--- a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
@@ -119,8 +119,18 @@ function settledTurnStateForSessionStatus(
}
}
+const projectorEventTypes = (
+ types: ReadonlyArray<OrchestrationEvent["type"]>,
+): ReadonlySet<OrchestrationEvent["type"]> => new Set(types);
+
interface ProjectorDefinition {
readonly name: ProjectorName;
+ /**
+ * Event types `apply` acts on. The live pipeline skips `apply` for other
+ * types but still advances this projector's cursor. Omit to receive every
+ * event. Keep in sync with the cases handled by `apply`.
+ */
+ readonly eventTypes?: ReadonlySet<OrchestrationEvent["type"]>;
readonly apply: (
event: OrchestrationEvent,
attachmentSideEffects: AttachmentSideEffects,
@@ -2143,58 +2153,164 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
const projectors: ReadonlyArray<ProjectorDefinition> = [
{
name: ORCHESTRATION_PROJECTOR_NAMES.projects,
+ eventTypes: projectorEventTypes([
+ "project.created",
+ "project.meta-updated",
+ "project.deleted",
+ ]),
apply: applyProjectsProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.bots,
+ eventTypes: projectorEventTypes([
+ "bot.created",
+ "bot.updated",
+ "bot.archived",
+ "bot.restored",
+ ]),
apply: applyBotsProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.groups,
+ eventTypes: projectorEventTypes([
+ "group.created",
+ "group.renamed",
+ "group.member-assigned",
+ "group.member-unassigned",
+ "group.person-assigned",
+ "group.person-unassigned",
+ "group.boss-set",
+ "group.deleted",
+ ]),
apply: applyGroupsProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.delegations,
+ eventTypes: projectorEventTypes(["delegation.created", "delegation.updated"]),
apply: applyDelegationsProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.mcpServers,
+ eventTypes: projectorEventTypes([
+ "mcp-server.created",
+ "mcp-server.updated",
+ "mcp-server.enabled",
+ "mcp-server.disabled",
+ "mcp-server.deleted",
+ ]),
apply: applyMcpServersProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.routines,
+ eventTypes: projectorEventTypes([
+ "skill-assignment.assigned",
+ "skill-assignment.unassigned",
+ "routine.drafted",
+ "routine.approved",
+ "routine.enabled",
+ "routine.running",
+ "routine.paused",
+ "routine.blocked",
+ "routine.failed",
+ "routine.completed",
+ "routine.run-canceled",
+ "routine.deleted",
+ ]),
apply: applyRoutinesProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.threadMessages,
+ eventTypes: projectorEventTypes([
+ "thread.created",
+ "thread.message-sent",
+ "thread.message-reaction-set",
+ "thread.reverted",
+ ]),
apply: applyThreadMessagesProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.threadProposedPlans,
+ eventTypes: projectorEventTypes([
+ "thread.created",
+ "thread.proposed-plan-upserted",
+ "thread.reverted",
+ ]),
apply: applyThreadProposedPlansProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.threadActivities,
+ eventTypes: projectorEventTypes([
+ "thread.created",
+ "thread.activity-appended",
+ "thread.reverted",
+ ]),
apply: applyThreadActivitiesProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.threadSessions,
+ eventTypes: projectorEventTypes([
+ "thread.created",
+ "thread.turn-resume-requested",
+ "thread.session-set",
+ ]),
apply: applyThreadSessionsProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.threadTurns,
+ eventTypes: projectorEventTypes([
+ "thread.created",
+ "thread.turn-start-requested",
+ "thread.session-set",
+ "thread.message-sent",
+ "thread.turn-interrupt-requested",
+ "thread.turn-diff-completed",
+ "thread.reverted",
+ ]),
apply: applyThreadTurnsProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.checkpoints,
+ eventTypes: projectorEventTypes([]),
apply: applyCheckpointsProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.pendingApprovals,
+ eventTypes: projectorEventTypes([
+ "thread.created",
+ "thread.activity-appended",
+ "thread.approval-response-requested",
+ ]),
apply: applyPendingApprovalsProjection,
},
{
name: ORCHESTRATION_PROJECTOR_NAMES.threads,
+ eventTypes: projectorEventTypes([
+ "thread.created",
+ "thread.ownership-updated",
+ "thread.archived",
+ "thread.unarchived",
+ "thread.settled",
+ "thread.unsettled",
+ "thread.snoozed",
+ "thread.unsnoozed",
+ "thread.pinned",
+ "thread.unpinned",
+ "thread.pin-reordered",
+ "thread.meta-updated",
+ "thread.runtime-mode-set",
+ "thread.interaction-mode-set",
+ "thread.deleted",
+ "thread.turn-start-requested",
+ "thread.message-sent",
+ "thread.message-reaction-set",
+ "thread.proposed-plan-upserted",
+ "thread.activity-appended",
+ "thread.approval-response-requested",
+ "thread.user-input-response-requested",
+ "thread.session-set",
+ "thread.turn-diff-completed",
+ "thread.reverted",
+ ]),
apply: applyThreadsProjection,
},
];
@@ -2250,12 +2366,17 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
),
);
+ const projectorHandlesEvent = (projector: ProjectorDefinition, event: OrchestrationEvent) =>
+ projector.eventTypes === undefined || projector.eventTypes.has(event.type);
+
const applyProjectorForEvent = Effect.fn("applyProjectorForEvent")(function* (
projector: ProjectorDefinition,
event: OrchestrationEvent,
attachmentSideEffects: AttachmentSideEffects,
) {
- yield* projector.apply(event, attachmentSideEffects);
+ if (projectorHandlesEvent(projector, event)) {
+ yield* projector.apply(event, attachmentSideEffects);
+ }
yield* projectionStateRepository.upsert({
projector: projector.name,
lastAppliedSequence: event.sequence,
@@ -2300,12 +2421,23 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
deletedThreadIds: new Set<string>(),
prunedThreadRelativePaths: new Map<string, Set<string>>(),
};
+ // Every cursor still advances to this event, so resume and snapshot
+ // sequences match running each projector; only no-op applies are skipped.
yield* sql.withTransaction(
- Effect.forEach(
- projectors,
- (projector) => applyProjectorForEvent(projector, event, attachmentSideEffects),
- { concurrency: 1, discard: true },
- ),
+ Effect.gen(function* () {
+ for (const projector of projectors) {
+ if (projectorHandlesEvent(projector, event)) {
+ yield* projector.apply(event, attachmentSideEffects);
+ }
+ }
+ yield* projectionStateRepository.upsertMany(
+ projectors.map((projector) => ({
+ projector: projector.name,
+ lastAppliedSequence: event.sequence,
+ updatedAt: event.occurredAt,
+ })),
+ );
+ }),
);
// Return the cleanup effect so the caller runs it after the outer transaction commits.
// @effect-diagnostics-next-line returnEffectInGen:off
diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts
index 77b519f79..24261716a 100644
--- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts
@@ -19,6 +19,10 @@ import * as SqlClient from "effect/unstable/sql/SqlClient";
import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
import * as RepositoryIdentityResolver from "../../project/RepositoryIdentityResolver.ts";
import { ORCHESTRATION_PROJECTOR_NAMES } from "./ProjectionPipeline.ts";
+import {
+ SHELL_DELEGATION_TEXT_MAX_CHARS,
+ SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD,
+} from "../ShellDelegations.ts";
import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";
import * as ThreadBackgroundLiveness from "../ThreadBackgroundLiveness.ts";
import * as ThreadPlanProgress from "../ThreadPlanProgress.ts";
@@ -105,6 +109,94 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
}),
);
+ it.effect("keeps open and recent delegations in the shell snapshot with capped text", () =>
+ Effect.gen(function* () {
+ const snapshotQuery = yield* ProjectionSnapshotQuery;
+ const sql = yield* SqlClient.SqlClient;
+ const makeRecord = (index: number, overrides: Record<string, unknown>) => ({
+ delegationId: `delegation-${String(index).padStart(3, "0")}`,
+ parentDelegationId: null,
+ parentBotId: "bot-parent",
+ childBotId: "bot-child",
+ parentThreadId: "thread-parent",
+ childThreadId: null,
+ parentTurnId: "turn-parent",
+ childTurnId: null,
+ ancestorBotIds: ["bot-parent"],
+ depth: 1,
+ task: `Task ${index}`,
+ expectedResult: "A short answer.",
+ deadline: null,
+ access: {
+ allowedToolIds: [],
+ memoryScopes: [],
+ sandbox: null,
+ runtimeMode: "approval-required",
+ hasUserComputer: false,
+ enabledMcpServerIds: [],
+ disabledMcpServerIds: [],
+ approvalCeiling: "none",
+ },
+ state: "completed",
+ billedBotId: "bot-child",
+ result: { summary: `Done ${index}`, childThreadId: "thread-child", childTurnId: null },
+ failure: null,
+ keep: false,
+ createdAt: `2026-01-01T00:00:${String(index % 60).padStart(2, "0")}.000Z`,
+ updatedAt: `2026-01-02T00:${String(index).padStart(2, "0")}:00.000Z`,
+ startedAt: null,
+ completedAt: null,
+ ...overrides,
+ });
+ const terminalCount = SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD + 5;
+ const records = [
+ // The oldest delegation is still running, so it survives the cap.
+ makeRecord(0, { state: "running", result: null }),
+ ...Array.from({ length: terminalCount }, (_, offset) => makeRecord(offset + 1, {})),
+ makeRecord(50, {
+ parentThreadId: "thread-other",
+ state: "failed",
+ result: null,
+ failure: { failureCode: "child_failed", message: "x".repeat(10_000) },
+ }),
+ ];
+
+ yield* sql`DELETE FROM projection_delegations`;
+ for (const record of records) {
+ const recordJson = yield* decodeDelegationRecord(record).pipe(
+ Effect.flatMap(encodeDelegationRecordJson),
+ );
+ yield* sql`
+ INSERT INTO projection_delegations (delegation_id, record_json)
+ VALUES (${record.delegationId}, ${recordJson})
+ `;
+ }
+
+ const snapshot = yield* snapshotQuery.getShellSnapshot();
+ const parentIds = snapshot.delegations
+ .filter((delegation) => delegation.parentThreadId === "thread-parent")
+ .map((delegation) => delegation.delegationId);
+ const newestTerminalIds = Array.from(
+ { length: SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD },
+ (_, offset) =>
+ `delegation-${String(terminalCount - SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD + offset + 1).padStart(3, "0")}`,
+ );
+ assert.deepEqual(parentIds, ["delegation-000", ...newestTerminalIds]);
+
+ const failed = snapshot.delegations.find(
+ (delegation) => delegation.delegationId === "delegation-050",
+ );
+ assert.equal(failed?.failure?.message.length, SHELL_DELEGATION_TEXT_MAX_CHARS);
+ assert.isTrue(failed?.failure?.message.endsWith("…"));
+ assert.equal(
+ snapshot.delegations.find((delegation) => delegation.delegationId === "delegation-025")
+ ?.result?.summary,
+ "Done 25",
+ );
+ yield* sql`DELETE FROM projection_delegations`;
+ }),
+ );
+
it.effect("hydrates read model from projection tables and computes snapshot sequence", () =>
Effect.gen(function* () {
const snapshotQuery = yield* ProjectionSnapshotQuery;
diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts
index 919ffc7a6..3bab2e08b 100644
--- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts
@@ -78,6 +78,10 @@ import {
encodeThreadDetailPageCursor,
} from "../threadDetailCursor.ts";
import { projectActivityPayload } from "../ActivityPayloadProjection.ts";
+import {
+ SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD,
+ toShellDelegation,
+} from "../ShellDelegations.ts";
import * as RepositoryIdentityResolver from "../../project/RepositoryIdentityResolver.ts";
import { ORCHESTRATION_PROJECTOR_NAMES } from "./ProjectionPipeline.ts";
import {
@@ -627,6 +631,92 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
`,
});
+ // Open delegations plus the newest terminal ones per parent thread, so the
+ // shell snapshot never hydrates a thread's whole delegation history.
+ const listShellDelegationRows = SqlSchema.findAll({
+ Request: Schema.Void,
+ Result: ProjectionDelegationDbRowSchema,
+ execute: () => sql`
+ SELECT delegation
+ FROM (
+ SELECT
+ record_json AS delegation,
+ delegation_id,
+ json_extract(record_json, '$.createdAt') AS created_at,
+ json_extract(record_json, '$.state') IN ('completed', 'failed', 'canceled') AS terminal,
+ ROW_NUMBER() OVER (
+ PARTITION BY
+ json_extract(record_json, '$.parentThreadId'),
+ json_extract(record_json, '$.state') IN ('completed', 'failed', 'canceled')
+ ORDER BY
+ json_extract(record_json, '$.updatedAt') DESC,
+ json_extract(record_json, '$.createdAt') ASC,
+ delegation_id ASC
+ ) AS recency
+ FROM projection_delegations
+ )
+ WHERE terminal = 0 OR recency <= ${SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD}
+ ORDER BY created_at ASC, delegation_id ASC
+ `,
+ });
+
+ const getBotRowById = SqlSchema.findOneOption({
+ Request: Schema.Struct({ botId: BotId }),
+ Result: ProjectionBotDbRowSchema,
+ execute: ({ botId }) => sql`
+ SELECT
+ bot_id AS "botId", name, title, label, description,
+ disabled_mcp_server_ids_json AS "disabledMcpServerIds", avatar_json AS "avatar",
+ engine_json AS "engine", sandbox, runtime_mode AS "runtimeMode",
+ usage_cap_json AS "usageCap", voice_enabled AS "voiceEnabled",
+ personality_tone AS "personalityTone",
+ channel_bindings_json AS "channelBindings", group_id AS "groupId",
+ archived_at AS "archivedAt", created_at AS "createdAt", updated_at AS "updatedAt"
+ FROM projection_bots
+ WHERE bot_id = ${botId}
+ LIMIT 1
+ `,
+ });
+
+ const getGroupRowById = SqlSchema.findOneOption({
+ Request: Schema.Struct({ groupId: GroupId }),
+ Result: ProjectionGroupDbRowSchema,
+ execute: ({ groupId }) => sql`
+ SELECT
+ group_id AS "groupId", name, boss_bot_id AS "bossBotId", members_json AS "members",
+ created_at AS "createdAt", updated_at AS "updatedAt"
+ FROM projection_groups
+ WHERE group_id = ${groupId}
+ LIMIT 1
+ `,
+ });
+
+ const listDelegationRowsByThread = SqlSchema.findAll({
+ Request: ThreadIdLookupInput,
+ Result: ProjectionDelegationDbRowSchema,
+ execute: ({ threadId }) => sql`
+ SELECT record_json AS delegation
+ FROM projection_delegations
+ WHERE json_extract(record_json, '$.childThreadId') = ${threadId}
+ OR json_extract(record_json, '$.parentThreadId') = ${threadId}
+ ORDER BY json_extract(record_json, '$.createdAt') ASC, delegation_id ASC
+ `,
+ });
+
+ const getLatestAssistantMessageRowForTurn = SqlSchema.findOneOption({
+ Request: Schema.Struct({ threadId: ThreadId, turnId: TurnId }),
+ Result: Schema.Struct({ messageId: MessageId }),
+ execute: ({ threadId, turnId }) => sql`
+ SELECT message_id AS "messageId"
+ FROM projection_thread_messages
+ WHERE thread_id = ${threadId}
+ AND turn_id = ${turnId}
+ AND role = 'assistant'
+ ORDER BY created_at DESC, message_id DESC
+ LIMIT 1
+ `,
+ });
+
const listThreadRows = SqlSchema.findAll({
Request: Schema.Void,
Result: ProjectionThreadDbRowSchema,
@@ -2609,7 +2699,7 @@ pending_approval_requests AS (
),
),
),
- listDelegationRows(undefined).pipe(
+ listShellDelegationRows(undefined).pipe(
Effect.mapError(
toPersistenceSqlOrDecodeError(
"ProjectionSnapshotQuery.getShellSnapshot:listDelegations:query",
@@ -2727,7 +2817,7 @@ pending_approval_requests AS (
),
bots: botRows.map(mapBotRow),
groups: groupRows.map(mapGroupRow),
- delegations: delegationRows.map((row) => row.delegation),
+ delegations: delegationRows.map((row) => toShellDelegation(row.delegation)),
mcpServers,
routines: routines.filter((routine) => routine.deletedAt === null),
routineRuns,
@@ -3286,6 +3376,54 @@ pending_approval_requests AS (
}));
});
+ const getBotById: NonNullable<ProjectionSnapshotQueryShape["getBotById"]> = (botId) =>
+ getBotRowById({ botId }).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionSnapshotQuery.getBotById:query",
+ "ProjectionSnapshotQuery.getBotById:decodeRow",
+ ),
+ ),
+ Effect.map(Option.map(mapBotRow)),
+ );
+
+ const getGroupById: NonNullable<ProjectionSnapshotQueryShape["getGroupById"]> = (groupId) =>
+ getGroupRowById({ groupId }).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionSnapshotQuery.getGroupById:query",
+ "ProjectionSnapshotQuery.getGroupById:decodeRow",
+ ),
+ ),
+ Effect.map(Option.map(mapGroupRow)),
+ );
+
+ const listThreadDelegations: NonNullable<
+ ProjectionSnapshotQueryShape["listThreadDelegations"]
+ > = (threadId) =>
+ listDelegationRowsByThread({ threadId }).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionSnapshotQuery.listThreadDelegations:query",
+ "ProjectionSnapshotQuery.listThreadDelegations:decodeRows",
+ ),
+ ),
+ Effect.map((rows) => rows.map((row) => row.delegation)),
+ );
+
+ const getLatestAssistantMessageIdForTurn: NonNullable<
+ ProjectionSnapshotQueryShape["getLatestAssistantMessageIdForTurn"]
+ > = (threadId, turnId) =>
+ getLatestAssistantMessageRowForTurn({ threadId, turnId }).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionSnapshotQuery.getLatestAssistantMessageIdForTurn:query",
+ "ProjectionSnapshotQuery.getLatestAssistantMessageIdForTurn:decodeRow",
+ ),
+ ),
+ Effect.map(Option.map((row) => row.messageId)),
+ );
+
const getTurnStartMessage: ProjectionSnapshotQueryShape["getTurnStartMessage"] = Effect.fn(
"ProjectionSnapshotQuery.getTurnStartMessage",
)(function* (input) {
@@ -3751,6 +3889,10 @@ pending_approval_requests AS (
getFullThreadDiffContext,
getThreadShellById,
getThreadRuntimeContext,
+ getBotById,
+ getGroupById,
+ listThreadDelegations,
+ getLatestAssistantMessageIdForTurn,
getTurnStartMessage,
listPendingTurnStarts,
getThreadDetailById,
diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts
index 3c12af15c..d548f6373 100644
--- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts
+++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts
@@ -193,6 +193,9 @@ describe("ProviderCommandReactor", () => {
readonly runningTurnBeforeReactor?: boolean;
readonly resumeBeforeReactor?: boolean;
readonly replayPersistedResumeOnSubscribe?: boolean;
+ readonly commitDuringSequenceRead?: 1 | 2;
+ readonly titleUpdatesBeforeStartupCommit?: number;
+ readonly failStartupReplay?: boolean;
readonly pendingRequestBeforeReactor?: "approval" | "user-input";
readonly interruptTurnEffect?: () => Effect.Effect<void, ProviderAdapterRequestError>;
readonly interruptTurnRemovesSession?: boolean;
@@ -478,12 +481,16 @@ describe("ProviderCommandReactor", () => {
Layer.provide(SqlitePersistenceMemory),
);
let titleRegenerationCompletionDispatchAttempts = 0;
+ let sequenceReads = 0;
const reactorOrchestrationLayer = Layer.effect(
OrchestrationEngineService,
Effect.gen(function* () {
const engine = yield* OrchestrationEngineService;
return {
- readEvents: engine.readEvents,
+ readEvents:
+ input?.failStartupReplay === true
+ ? () => Stream.die(new Error("Injected startup replay failure"))
+ : engine.readEvents,
readThreadEvents: engine.readThreadEvents,
getThreadReplayStats: engine.getThreadReplayStats,
dispatch: (command) => {
@@ -528,7 +535,44 @@ describe("ProviderCommandReactor", () => {
),
)
: engine.subscribeDomainEvents,
- latestSequence: engine.latestSequence,
+ latestSequence:
+ input?.commitDuringSequenceRead === undefined
+ ? engine.latestSequence
+ : Effect.suspend(() => {
+ sequenceReads += 1;
+ if (sequenceReads !== input.commitDuringSequenceRead)
+ return engine.latestSequence;
+ // The first read precedes the reactor's subscription, so a commit
+ // after it lands in the gap. The second read follows the
+ // subscription, so a commit before it is buffered and counted.
+ const commit = engine
+ .dispatch({
+ type: "thread.meta.update",
+ commandId: CommandId.make("cmd-commit-during-sequence-read"),
+ threadId: ThreadId.make("thread-1"),
+ regenerateTitle: true,
+ })
+ .pipe(Effect.orDie);
+ const titleUpdates = Effect.forEach(
+ Array.from(
+ { length: input.titleUpdatesBeforeStartupCommit ?? 0 },
+ (_, index) => index,
+ ),
+ (index) =>
+ engine
+ .dispatch({
+ type: "thread.meta.update",
+ commandId: CommandId.make(`cmd-startup-title-${index}`),
+ threadId: ThreadId.make("thread-1"),
+ title: `Startup title ${index}`,
+ })
+ .pipe(Effect.orDie),
+ { discard: true },
+ ).pipe(Effect.andThen(commit));
+ return sequenceReads === 1
+ ? engine.latestSequence.pipe(Effect.tap(() => titleUpdates))
+ : titleUpdates.pipe(Effect.andThen(engine.latestSequence));
+ }),
} satisfies OrchestrationEngineService["Service"];
}),
).pipe(Layer.provide(orchestrationLayer));
@@ -871,6 +915,41 @@ describe("ProviderCommandReactor", () => {
expect(thread?.session?.runtimeMode).toBe("approval-required");
});
+ it.each([
+ [1, "before"],
+ [2, "after"],
+ ] as const)(
+ "handles an intent committed on startup sequence read %i, %s subscribing",
+ async (commitDuringSequenceRead, _position) => {
+ const harness = await createHarness({ commitDuringSequenceRead });
+
+ await harness.drain();
+
+ const readModel = await harness.readModel();
+ const thread = readModel.threads.find((entry) => entry.id === ThreadId.make("thread-1"));
+ expect(thread?.titleRegeneration).toBeNull();
+ },
+ );
+
+ it("replays a startup gap longer than one event store page", async () => {
+ const harness = await createHarness({
+ commitDuringSequenceRead: 1,
+ titleUpdatesBeforeStartupCommit: 1_000,
+ });
+
+ await harness.drain();
+
+ const readModel = await harness.readModel();
+ const thread = readModel.threads.find((entry) => entry.id === ThreadId.make("thread-1"));
+ expect(thread?.titleRegeneration).toBeNull();
+ });
+
+ it("fails startup instead of dropping a gap it cannot replay", async () => {
+ await expect(
+ createHarness({ commitDuringSequenceRead: 1, failStartupReplay: true }),
+ ).rejects.toThrow("Injected startup replay failure");
+ });
+
it("replays a persisted turn start that predates reactor startup exactly once", async () => {
const harness = await createHarness({ turnStartBeforeReactor: true });
diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts
index de0a0c1c0..d84fdc62c 100644
--- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts
+++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts
@@ -34,8 +34,10 @@ import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
+import * as Schedule from "effect/Schedule";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
+import * as SubscriptionRef from "effect/SubscriptionRef";
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
import { resolveThreadWorkspaceCwd } from "../../checkpointing/Utils.ts";
@@ -927,9 +929,11 @@ const make = Effect.gen(function* () {
const respondingGroup =
thread.groupId == null
? undefined
- : (yield* projectionSnapshotQuery.getCommandReadModel()).groups.find(
- (group) => group.id === thread.groupId,
- );
+ : projectionSnapshotQuery.getGroupById
+ ? Option.getOrUndefined(yield* projectionSnapshotQuery.getGroupById(thread.groupId))
+ : (yield* projectionSnapshotQuery.getCommandReadModel()).groups.find(
+ (group) => group.id === thread.groupId,
+ );
const effectiveSandbox = respondingBot?.sandbox ?? serverSettings.sandbox.defaultProvider;
const sandboxEnvironment =
effectiveSandbox === "local"
@@ -2374,7 +2378,45 @@ const make = Effect.gen(function* () {
}
});
+ // Highest event sequence the subscriber has handed to the worker, so drain
+ // can wait for published events that are still in the subscription buffer.
+ const seenSequence = yield* SubscriptionRef.make(0);
+ const noteSeen = (sequence: number) =>
+ SubscriptionRef.update(seenSequence, (seen) => Math.max(seen, sequence));
+ const started = yield* SubscriptionRef.make(false);
+
+ // Subscribes before returning and yields every event after the baseline once.
+ // Events committed between the first sequence read and the subscription may
+ // have been published before it existed, so that gap is read from the store
+ // before start returns and the live stream skips everything it covered. A
+ // store that cannot replay the gap fails startup rather than drop intents.
+ const subscribeWithBaseline = Effect.fn("subscribeWithBaseline")(function* () {
+ const baselineSequence = yield* orchestrationEngine.latestSequence;
+ const liveEvents = yield* orchestrationEngine.subscribeDomainEvents;
+ const replayThrough = yield* orchestrationEngine.latestSequence;
+ if (replayThrough === baselineSequence) {
+ return { domainEvents: liveEvents, baselineSequence };
+ }
+ const gapEvents = yield* Stream.runCollect(
+ orchestrationEngine.readEvents(
+ baselineSequence,
+ replayThrough - baselineSequence,
+ replayThrough,
+ ),
+ ).pipe(
+ Effect.retry(Schedule.max([Schedule.exponential("100 millis"), Schedule.recurs(3)])),
+ Effect.orDie,
+ );
+ return {
+ domainEvents: Stream.fromIterable(gapEvents).pipe(
+ Stream.concat(liveEvents.pipe(Stream.filter((event) => event.sequence > replayThrough))),
+ ),
+ baselineSequence,
+ };
+ });
+
const start: ProviderCommandReactorShape["start"] = Effect.fn("start")(function* () {
+ yield* SubscriptionRef.set(started, true);
const interruptedTitleRegenerations = yield* findInterruptedThreadTitleRegenerations().pipe(
Effect.catchCause((cause) => {
if (Cause.hasInterruptsOnly(cause)) {
@@ -2387,6 +2429,12 @@ const make = Effect.gen(function* () {
}),
);
const processEvent = Effect.fn("processEvent")(function* (event: OrchestrationEvent) {
+ yield* enqueueProviderIntent(event);
+ yield* noteSeen(event.sequence);
+ });
+ const enqueueProviderIntent = Effect.fn("enqueueProviderIntent")(function* (
+ event: OrchestrationEvent,
+ ) {
if (
(event.type === "thread.meta-updated" && event.payload.regenerateTitle === true) ||
event.type === "thread.runtime-mode-set" ||
@@ -2403,7 +2451,8 @@ const make = Effect.gen(function* () {
});
// Subscribe before returning, even while event handling waits for server activation.
- const domainEvents = yield* orchestrationEngine.subscribeDomainEvents;
+ const { domainEvents, baselineSequence } = yield* subscribeWithBaseline();
+ yield* noteSeen(baselineSequence);
yield* forkParked(Stream.runForEach(domainEvents, processEvent));
yield* recoverStartupProviderWork().pipe(
@@ -2443,9 +2492,21 @@ const make = Effect.gen(function* () {
return {
start,
+ // Waits until the subscriber has taken every event published so far, then
+ // until both workers are idle. Repeats while that work publishes more events.
drain: Effect.gen(function* () {
- yield* worker.drain;
- yield* threadTitleRegenerationWorker.drain;
+ while (true) {
+ const target = (yield* SubscriptionRef.get(started))
+ ? yield* orchestrationEngine.latestSequence
+ : 0;
+ yield* SubscriptionRef.changes(seenSequence).pipe(
+ Stream.filter((seen) => seen >= target),
+ Stream.runHead,
+ );
+ yield* worker.drain;
+ yield* threadTitleRegenerationWorker.drain;
+ if (target === 0 || (yield* orchestrationEngine.latestSequence) === target) return;
+ }
}),
} satisfies ProviderCommandReactorShape;
});
diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
index e1e84f064..c487e1278 100644
--- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
+++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
@@ -49,7 +49,10 @@ import { ProjectionThreadProposedPlanRepositoryLive } from "../../persistence/La
import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
import { ThreadBackgroundLivenessService } from "../ThreadBackgroundLiveness.ts";
import { ThreadPlanProgressService } from "../ThreadPlanProgress.ts";
-import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
+import {
+ ProjectionSnapshotQuery,
+ type ProjectionThreadRuntimeContext,
+} from "../Services/ProjectionSnapshotQuery.ts";
import {
ProviderRuntimeIngestionService,
type ProviderRuntimeIngestionShape,
@@ -1125,6 +1128,28 @@ const make = Effect.gen(function* () {
.pipe(Effect.map(Option.getOrUndefined));
});
+ // Assistant text deltas only need to know the thread exists and is active,
+ // so they reuse the context read by the thread's most recent non-delta
+ // event. Every other runtime event re-reads and refreshes the entry; session
+ // exit and thread session/delete/archive domain events drop it.
+ const deltaRuntimeContextByThread = new Map<string, ProjectionThreadRuntimeContext>();
+ const resolveThreadRuntimeContextForEvent = Effect.fn("resolveThreadRuntimeContextForEvent")(
+ function* (event: ProviderRuntimeEvent) {
+ const key = String(event.threadId);
+ if (event.type === "content.delta") {
+ const cached = deltaRuntimeContextByThread.get(key);
+ if (cached) return cached;
+ }
+ const thread = yield* resolveThreadRuntimeContext(event.threadId);
+ if (thread && event.type !== "session.exited") {
+ deltaRuntimeContextByThread.set(key, thread);
+ } else {
+ deltaRuntimeContextByThread.delete(key);
+ }
+ return thread;
+ },
+ );
+
const getThreadMessageById = Effect.fn("getThreadMessageById")(function* (
threadId: ThreadId,
messageId: MessageId,
@@ -1696,7 +1721,7 @@ const make = Effect.gen(function* () {
if (event.type === "content.delta" && event.payload.streamKind !== "assistant_text") {
return;
}
- const thread = yield* resolveThreadRuntimeContext(event.threadId);
+ const thread = yield* resolveThreadRuntimeContextForEvent(event);
if (!thread) return;
if (event.type === "request.opened" || event.type === "request.resolved") {
yield* syncApprovalInbox(event, thread);
@@ -2472,9 +2497,13 @@ const make = Effect.gen(function* () {
);
yield* forkParked(
Stream.runForEach(orchestrationEngine.streamDomainEvents, (event) => {
+ if (event.type === "thread.deleted" || event.type === "thread.archived") {
+ deltaRuntimeContextByThread.delete(String(event.payload.threadId));
+ }
if (event.type !== "thread.session-set") {
return Effect.void;
}
+ deltaRuntimeContextByThread.delete(String(event.payload.threadId));
return worker.enqueue({ source: "domain", event });
}),
);
diff --git a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts
index 539393a5c..1f5779341 100644
--- a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts
+++ b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts
@@ -7,11 +7,14 @@
* @module ProjectionSnapshotQuery
*/
import type {
+ AkeruDelegationRecord,
BotId,
CheckpointRef,
GroupId,
MessageId,
+ OrchestrationBot,
OrchestrationCheckpointSummary,
+ OrchestrationGroup,
OrchestrationMessage,
OrchestrationProject,
OrchestrationProjectShell,
@@ -27,6 +30,7 @@ import type {
ProjectId,
RuntimeMode,
ThreadId,
+ TurnId,
} from "@t3tools/contracts";
import * as Context from "effect/Context";
import type * as Option from "effect/Option";
@@ -222,6 +226,38 @@ export interface ProjectionSnapshotQueryShape {
threadId: ThreadId,
) => Effect.Effect<Option.Option<ProjectionThreadRuntimeContext>, ProjectionRepositoryError>;
+ /**
+ * Read one bot by id, including archived bots. Optional so lightweight test
+ * doubles can omit it; callers fall back to a full read model.
+ */
+ readonly getBotById?: (
+ botId: BotId,
+ ) => Effect.Effect<Option.Option<OrchestrationBot>, ProjectionRepositoryError>;
+
+ /**
+ * Read one group by id. Optional for the same reason as `getBotById`.
+ */
+ readonly getGroupById?: (
+ groupId: GroupId,
+ ) => Effect.Effect<Option.Option<OrchestrationGroup>, ProjectionRepositoryError>;
+
+ /**
+ * List delegations where the thread is the parent or the child, oldest
+ * first. Optional for the same reason as `getBotById`.
+ */
+ readonly listThreadDelegations?: (
+ threadId: ThreadId,
+ ) => Effect.Effect<ReadonlyArray<AkeruDelegationRecord>, ProjectionRepositoryError>;
+
+ /**
+ * Read the id of the newest assistant message for a turn. Optional for the
+ * same reason as `getBotById`; callers fall back to thread detail.
+ */
+ readonly getLatestAssistantMessageIdForTurn?: (
+ threadId: ThreadId,
+ turnId: TurnId,
+ ) => Effect.Effect<Option.Option<MessageId>, ProjectionRepositoryError>;
+
/**
* Read the user prompt that starts a turn and whether the thread has any
* other non-compaction user message.
diff --git a/apps/server/src/orchestration/ShellDelegations.ts b/apps/server/src/orchestration/ShellDelegations.ts
new file mode 100644
index 000000000..8881dc55b
--- /dev/null
+++ b/apps/server/src/orchestration/ShellDelegations.ts
@@ -0,0 +1,32 @@
+import type { AkeruDelegationRecord } from "@t3tools/contracts";
+
+export { SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD } from "@t3tools/contracts";
+
+/** Longest delegation result summary or failure message the shell snapshot carries. */
+export const SHELL_DELEGATION_TEXT_MAX_CHARS = 2_000;
+
+function truncateShellText(text: string): string {
+ return text.length <= SHELL_DELEGATION_TEXT_MAX_CHARS
+ ? text
+ : `${text.slice(0, SHELL_DELEGATION_TEXT_MAX_CHARS - 1).trimEnd()}…`;
+}
+
+/** Caps a delegation's free-form result text for shell payloads. */
+export function toShellDelegation(delegation: AkeruDelegationRecord): AkeruDelegationRecord {
+ const summary = delegation.result?.summary;
+ const message = delegation.failure?.message;
+ const summaryFits = summary === undefined || summary.length <= SHELL_DELEGATION_TEXT_MAX_CHARS;
+ const messageFits = message === undefined || message.length <= SHELL_DELEGATION_TEXT_MAX_CHARS;
+ if (summaryFits && messageFits) return delegation;
+ return {
+ ...delegation,
+ result:
+ delegation.result === null || summaryFits
+ ? delegation.result
+ : { ...delegation.result, summary: truncateShellText(delegation.result.summary) },
+ failure:
+ delegation.failure === null || messageFits
+ ? delegation.failure
+ : { ...delegation.failure, message: truncateShellText(delegation.failure.message) },
+ };
+}
diff --git a/apps/server/src/orchestration/projector.test.ts b/apps/server/src/orchestration/projector.test.ts
index 3faeea372..63fc8044f 100644
--- a/apps/server/src/orchestration/projector.test.ts
+++ b/apps/server/src/orchestration/projector.test.ts
@@ -1180,4 +1180,84 @@ describe("orchestration projector", () => {
expect(thread?.checkpoints.at(-1)?.turnId).toBe("turn-599");
}),
);
+
+ it.effect("keeps activities sorted and updates messages in place", () =>
+ Effect.gen(function* () {
+ const createdAt = "2026-03-02T10:00:00.000Z";
+ let sequence = 0;
+ const event = (type: OrchestrationEvent["type"], payload: unknown) =>
+ makeEvent({
+ sequence: ++sequence,
+ type,
+ aggregateKind: "thread",
+ aggregateId: "thread-order",
+ occurredAt: createdAt,
+ commandId: null,
+ payload,
+ });
+ const activity = (id: string, at: string, summary = id) =>
+ event("thread.activity-appended", {
+ threadId: "thread-order",
+ activity: {
+ id,
+ tone: "info",
+ kind: "note",
+ summary,
+ payload: {},
+ turnId: null,
+ createdAt: at,
+ },
+ });
+ const message = (id: string, text: string, streaming: boolean) =>
+ event("thread.message-sent", {
+ threadId: "thread-order",
+ messageId: id,
+ role: "assistant",
+ text,
+ turnId: null,
+ streaming,
+ createdAt,
+ updatedAt: createdAt,
+ });
+
+ const events: ReadonlyArray<OrchestrationEvent> = [
+ event("thread.created", {
+ threadId: "thread-order",
+ projectId: "project-1",
+ title: "order",
+ modelSelection: { provider: ProviderDriverKind.make("codex"), model: "gpt-5-codex" },
+ runtimeMode: "full-access",
+ branch: null,
+ worktreePath: null,
+ createdAt,
+ updatedAt: createdAt,
+ }),
+ activity("activity-b", "2026-03-02T10:00:02.000Z"),
+ activity("activity-d", "2026-03-02T10:00:04.000Z"),
+ activity("activity-a", "2026-03-02T10:00:01.000Z"),
+ activity("activity-c", "2026-03-02T10:00:03.000Z"),
+ activity("activity-d", "2026-03-02T10:00:04.000Z", "activity-d replaced"),
+ message("message-1", "first ", true),
+ message("message-2", "second", false),
+ message("message-1", "part", true),
+ ];
+ const state = yield* Effect.reduce(
+ events,
+ () => createEmptyReadModel(createdAt),
+ projectEvent,
+ );
+
+ const thread = state.threads[0];
+ expect(thread?.activities.map((entry) => [entry.id, entry.summary])).toEqual([
+ ["activity-a", "activity-a"],
+ ["activity-b", "activity-b"],
+ ["activity-c", "activity-c"],
+ ["activity-d", "activity-d replaced"],
+ ]);
+ expect(thread?.messages.map((entry) => [entry.id, entry.text])).toEqual([
+ ["message-1", "first part"],
+ ["message-2", "second"],
+ ]);
+ }),
+ );
});
diff --git a/apps/server/src/orchestration/projector.ts b/apps/server/src/orchestration/projector.ts
index 4044e80af..a10e6ebd5 100644
--- a/apps/server/src/orchestration/projector.ts
+++ b/apps/server/src/orchestration/projector.ts
@@ -77,6 +77,7 @@ import {
type ThreadPatch = Partial<Omit<OrchestrationThread, "id" | "projectId">>;
const MAX_THREAD_MESSAGES = 2_000;
+const MAX_THREAD_ACTIVITIES = 500;
const MAX_THREAD_CHECKPOINTS = 500;
function checkpointStatusToLatestTurnState(status: "ready" | "missing" | "error") {
@@ -1044,31 +1045,29 @@ export function projectEvent(
"message",
);
- const existingMessage = thread.messages.find((entry) => entry.id === message.id);
- const messages = existingMessage
- ? thread.messages.map((entry) =>
- entry.id === message.id
- ? {
- ...entry,
- text: message.streaming
- ? `${entry.text}${message.text}`
- : message.text.length > 0
- ? message.text
- : entry.text,
- streaming: message.streaming,
- updatedAt: message.updatedAt,
- turnId: message.turnId,
- ...(message.respondingBotId !== undefined
- ? { respondingBotId: message.respondingBotId }
- : {}),
- ...(message.attachments !== undefined
- ? { attachments: message.attachments }
- : {}),
- }
- : entry,
- )
+ // Streaming updates target the newest messages, so search from the end
+ // and replace the one entry instead of mapping the whole history.
+ const existingIndex = thread.messages.findLastIndex((entry) => entry.id === message.id);
+ const entry = existingIndex >= 0 ? thread.messages[existingIndex] : undefined;
+ const messages = entry
+ ? thread.messages.with(existingIndex, {
+ ...entry,
+ text: message.streaming
+ ? `${entry.text}${message.text}`
+ : message.text.length > 0
+ ? message.text
+ : entry.text,
+ streaming: message.streaming,
+ updatedAt: message.updatedAt,
+ turnId: message.turnId,
+ ...(message.respondingBotId !== undefined
+ ? { respondingBotId: message.respondingBotId }
+ : {}),
+ ...(message.attachments !== undefined ? { attachments: message.attachments } : {}),
+ })
: [...thread.messages, message];
- const cappedMessages = messages.slice(-MAX_THREAD_MESSAGES);
+ const cappedMessages =
+ messages.length > MAX_THREAD_MESSAGES ? messages.slice(-MAX_THREAD_MESSAGES) : messages;
return {
...nextBase,
@@ -1404,12 +1403,22 @@ export function projectEvent(
return nextBase;
}
- const activities = [
- ...thread.activities.filter((entry) => entry.id !== payload.activity.id),
- payload.activity,
- ]
- .toSorted(compareThreadActivities)
- .slice(-500);
+ // Activities stay sorted, so an in-order new activity is a plain append.
+ const lastActivity = thread.activities.at(-1);
+ const appendsInOrder =
+ (lastActivity === undefined ||
+ compareThreadActivities(lastActivity, payload.activity) <= 0) &&
+ !thread.activities.some((entry) => entry.id === payload.activity.id);
+ const sortedActivities = appendsInOrder
+ ? [...thread.activities, payload.activity]
+ : [
+ ...thread.activities.filter((entry) => entry.id !== payload.activity.id),
+ payload.activity,
+ ].toSorted(compareThreadActivities);
+ const activities =
+ sortedActivities.length > MAX_THREAD_ACTIVITIES
+ ? sortedActivities.slice(-MAX_THREAD_ACTIVITIES)
+ : sortedActivities;
return {
...nextBase,
diff --git a/apps/server/src/provider/Layers/AgentController.ts b/apps/server/src/provider/Layers/AgentController.ts
index eb612506d..8f3255646 100644
--- a/apps/server/src/provider/Layers/AgentController.ts
+++ b/apps/server/src/provider/Layers/AgentController.ts
@@ -949,7 +949,7 @@ const make = (options?: AgentControllerLiveOptions) =>
readonly botId: BotId;
readonly parentDelegation: AkeruDelegationRecord | undefined;
readonly access: AkeruDelegationAccessGrant;
- readonly snapshot: OrchestrationReadModel | undefined;
+ readonly activeChildDelegations: number;
}): NonNullable<AkeruToolSession["delegation"]> => {
const parent = () => {
const turnId = sessions.get(String(input.threadId))?.activeTurn?.turnId;
@@ -971,14 +971,7 @@ const make = (options?: AgentControllerLiveOptions) =>
const stopAgent = delegationRuntime?.stop;
return {
depth: input.parentDelegation?.depth ?? 0,
- activeDelegations:
- input.snapshot?.delegations.filter(
- (candidate) =>
- candidate.parentThreadId === input.threadId &&
- candidate.state !== "completed" &&
- candidate.state !== "failed" &&
- candidate.state !== "canceled",
- ).length ?? 0,
+ activeDelegations: input.activeChildDelegations,
access: input.access,
...(createAgent ? { create: (request) => createAgent(parent(), request) } : {}),
...(checkAgent ? { check: (request) => checkAgent(parent(), request) } : {}),
@@ -1185,7 +1178,8 @@ const make = (options?: AgentControllerLiveOptions) =>
delegationRuntime ??=
Option.isSome(orchestrationEngine) && Option.isSome(projectionSnapshotQuery)
? makeDelegationRuntime({
- readSnapshot: () => Effect.runPromise(projectionSnapshotQuery.value.getSnapshot()),
+ readSnapshot: () =>
+ Effect.runPromise(projectionSnapshotQuery.value.getCommandReadModel()),
dispatch: (command) => Effect.runPromise(orchestrationEngine.value.dispatch(command)),
})
: undefined;
@@ -2001,27 +1995,71 @@ const make = (options?: AgentControllerLiveOptions) =>
}),
);
+ const isOpenDelegation = (delegation: AkeruDelegationRecord) =>
+ delegation.state !== "completed" &&
+ delegation.state !== "failed" &&
+ delegation.state !== "canceled";
+
+ // Resolves the thread's bot, group boss, and delegation links with by-id
+ // reads. Falls back to the command read model for query doubles that do
+ // not implement the narrow lookups.
+ const readSessionStartContext = Effect.fn("AgentController.readSessionStartContext")(function* (
+ threadId: ThreadId,
+ fallbackBotId: BotId | null,
+ ) {
+ if (Option.isNone(projectionSnapshotQuery)) {
+ return {
+ parentDelegation: undefined,
+ bot: undefined,
+ botId: fallbackBotId,
+ activeChildDelegations: 0,
+ };
+ }
+ const query = projectionSnapshotQuery.value;
+ const { getBotById, getGroupById, listThreadDelegations } = query;
+ if (getBotById && getGroupById && listThreadDelegations) {
+ const thread = Option.getOrUndefined(yield* query.getThreadRuntimeContext(threadId));
+ const delegations = (yield* listThreadDelegations(threadId)).filter(isOpenDelegation);
+ const group = thread?.groupId
+ ? Option.getOrUndefined(yield* getGroupById(thread.groupId))
+ : undefined;
+ const botId =
+ thread?.respondingBotId ?? thread?.botId ?? fallbackBotId ?? group?.bossBotId ?? null;
+ const bot = botId ? Option.getOrUndefined(yield* getBotById(botId)) : undefined;
+ return {
+ parentDelegation: delegations.find((candidate) => candidate.childThreadId === threadId),
+ bot,
+ botId,
+ activeChildDelegations: delegations.filter(
+ (candidate) => candidate.parentThreadId === threadId,
+ ).length,
+ };
+ }
+ const snapshot = yield* query.getCommandReadModel();
+ const thread = snapshot.threads.find((candidate) => candidate.id === threadId);
+ const group = thread?.groupId
+ ? snapshot.groups.find((candidate) => candidate.id === thread.groupId)
+ : undefined;
+ const botId =
+ thread?.respondingBotId ?? thread?.botId ?? fallbackBotId ?? group?.bossBotId ?? null;
+ return {
+ parentDelegation: snapshot.delegations.find(
+ (candidate) => candidate.childThreadId === threadId && isOpenDelegation(candidate),
+ ),
+ bot: snapshot.bots.find((candidate) => candidate.id === botId),
+ botId,
+ activeChildDelegations: snapshot.delegations.filter(
+ (candidate) => candidate.parentThreadId === threadId && isOpenDelegation(candidate),
+ ).length,
+ };
+ });
+
const startSession: AgentControllerShape["startSession"] = Effect.fn(
"AgentController.startSession",
)(function* (threadId, input) {
const key = String(threadId);
- const snapshot = Option.isSome(projectionSnapshotQuery)
- ? yield* projectionSnapshotQuery.value.getSnapshot()
- : undefined;
- const thread = snapshot?.threads.find((candidate) => candidate.id === threadId);
- const parentDelegation = snapshot?.delegations.find(
- (candidate) =>
- candidate.childThreadId === threadId &&
- candidate.state !== "completed" &&
- candidate.state !== "failed" &&
- candidate.state !== "canceled",
- );
- const respondingBotId = thread?.respondingBotId ?? thread?.botId ?? input.botId ?? null;
- const group = thread?.groupId
- ? snapshot?.groups.find((candidate) => candidate.id === thread.groupId)
- : undefined;
- const botId = respondingBotId ?? group?.bossBotId ?? null;
- const bot = snapshot?.bots.find((candidate) => candidate.id === botId);
+ const { parentDelegation, bot, botId, activeChildDelegations } =
+ yield* readSessionStartContext(threadId, input.botId ?? null);
const delegatedAccess =
delegationRuntime?.accessForThread(threadId) ?? parentDelegation?.access;
const access: AkeruDelegationAccessGrant = delegatedAccess ?? {
@@ -2182,7 +2220,7 @@ const make = (options?: AgentControllerLiveOptions) =>
botId,
parentDelegation,
access,
- snapshot,
+ activeChildDelegations,
}),
}
: {}),
@@ -2396,7 +2434,13 @@ const make = (options?: AgentControllerLiveOptions) =>
request,
);
},
- delegation: delegationFor({ threadId, botId, parentDelegation, access, snapshot }),
+ delegation: delegationFor({
+ threadId,
+ botId,
+ parentDelegation,
+ access,
+ activeChildDelegations,
+ }),
}
: {}),
...(input.botId && channelRuntime
diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts
index ac5cff2b2..d8f8951a2 100644
--- a/apps/server/src/server.test.ts
+++ b/apps/server/src/server.test.ts
@@ -116,6 +116,7 @@ import * as OrchestrationEngine from "./orchestration/Services/OrchestrationEngi
import { ThreadDeletionReactor } from "./orchestration/Services/ThreadDeletionReactor.ts";
import { OrchestrationListenerCallbackError } from "./orchestration/Errors.ts";
import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts";
+import { toShellDelegation } from "./orchestration/ShellDelegations.ts";
import { SqlitePersistenceMemory } from "./persistence/Layers/Sqlite.ts";
import { OrchestrationEventStoreLive } from "./persistence/Layers/OrchestrationEventStore.ts";
import { OrchestrationEventStore } from "./persistence/Services/OrchestrationEventStore.ts";
@@ -5320,7 +5321,12 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
const created = record(DelegationId.make("delegation-created"));
const updated = {
...record(DelegationId.make("delegation-updated")),
- state: "running",
+ state: "completed",
+ result: {
+ summary: "y".repeat(10_000),
+ childThreadId: ThreadId.make("thread-child"),
+ childTurnId: null,
+ },
} as const;
const events = [
{
@@ -5370,10 +5376,16 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
),
);
+ // Streamed upserts carry the same capped result text as the shell snapshot.
assert.deepEqual(Array.from(items), [
{ kind: "delegation-upserted", sequence: 1, delegation: created },
- { kind: "delegation-upserted", sequence: 2, delegation: updated },
+ { kind: "delegation-upserted", sequence: 2, delegation: toShellDelegation(updated) },
]);
+ const streamedUpdate = Array.from(items)[1];
+ assert.isTrue(
+ streamedUpdate?.kind === "delegation-upserted" &&
+ (streamedUpdate.delegation.result?.summary.length ?? 0) < updated.result.summary.length,
+ );
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts
index fb9be5b6e..192c2b413 100644
--- a/apps/server/src/serverSettings.test.ts
+++ b/apps/server/src/serverSettings.test.ts
@@ -1277,6 +1277,76 @@ it.layer(NodeServices.layer)("server settings", (it) => {
}).pipe(Effect.provide(makeServerSettingsLayer())),
);
+ it.effect("reuses materialized secrets until settings or secrets change", () => {
+ const secretReads = { count: 0 };
+ const countingSecretStoreLayer = Layer.effect(
+ ServerSecretStore.ServerSecretStore,
+ Effect.gen(function* () {
+ const store = yield* ServerSecretStore.ServerSecretStore;
+ return ServerSecretStore.ServerSecretStore.of({
+ ...store,
+ get: (name) => {
+ secretReads.count += 1;
+ return store.get(name);
+ },
+ });
+ }),
+ ).pipe(Layer.provide(ServerSecretStore.layer));
+ const configLayer = Layer.fresh(
+ ServerConfig.layerTest(process.cwd(), {
+ prefix: "t3code-server-settings-materialize-cache-test-",
+ }),
+ );
+ const settingsLayer = ServerSettingsModule.layer.pipe(
+ Layer.provide(countingSecretStoreLayer),
+ Layer.provideMerge(Layer.fresh(SqlitePersistenceMemory)),
+ Layer.provideMerge(configLayer),
+ );
+ const instanceId = ProviderInstanceId.make("codex_personal");
+ const withSecret = (value: string) => ({
+ providerInstances: {
+ [instanceId]: {
+ driver: ProviderDriverKind.make("codex"),
+ environment: [{ name: "OPENROUTER_API_KEY", value, sensitive: true }],
+ config: {},
+ },
+ },
+ });
+
+ return Effect.gen(function* () {
+ const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
+ yield* serverSettings.updateSettings(withSecret("sk-first"));
+
+ const readsBeforeFirst = secretReads.count;
+ const first = yield* serverSettings.getSettings;
+ assert.equal(first.providerInstances[instanceId]?.environment?.[0]?.value, "sk-first");
+ const readsAfterFirst = secretReads.count;
+ const readsPerMaterialize = readsAfterFirst - readsBeforeFirst;
+ const second = yield* serverSettings.getSettings;
+ assert.equal(second.providerInstances[instanceId]?.environment?.[0]?.value, "sk-first");
+ assert.equal(secretReads.count, readsAfterFirst);
+
+ yield* serverSettings.updateSettings(withSecret("sk-second"));
+ const updated = yield* serverSettings.getSettings;
+ assert.equal(updated.providerInstances[instanceId]?.environment?.[0]?.value, "sk-second");
+ assert.isAbove(secretReads.count, readsAfterFirst);
+
+ // Concurrent misses after a change share one materialization.
+ yield* serverSettings.updateSettings(withSecret("sk-third"));
+ const readsBeforeBurst = secretReads.count;
+ const burst = yield* Effect.all(
+ Array.from({ length: 5 }, () => serverSettings.getSettings),
+ {
+ concurrency: "unbounded",
+ },
+ );
+ for (const settings of burst) {
+ assert.equal(settings.providerInstances[instanceId]?.environment?.[0]?.value, "sk-third");
+ }
+ assert.equal(secretReads.count - readsBeforeBurst, readsPerMaterialize);
+ }).pipe(Effect.provide(settingsLayer));
+ });
+
it.effect("rejects plaintext sandbox secrets loaded from settings.json", () =>
Effect.gen(function* () {
const serverConfig = yield* ServerConfig.ServerConfig;
diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts
index 80228b95a..9738a0480 100644
--- a/apps/server/src/serverSettings.ts
+++ b/apps/server/src/serverSettings.ts
@@ -720,6 +720,53 @@ const make = Effect.gen(function* () {
Effect.flatMap(materializeBrowserProviderSecret),
);
+ // Hot paths (runtime ingestion reads settings per streamed delta) must not
+ // hit the secret store every call. The materialized result is reused while
+ // the cached settings object is unchanged and no update has started since
+ // the read began. Every settings secret write goes through updateSettings,
+ // which bumps the generation before and after touching secrets.
+ type MaterializedState = {
+ readonly generation: number;
+ readonly entry?: {
+ readonly source: ServerSettings;
+ readonly materialized: ServerSettings;
+ };
+ };
+ const materializedRef = yield* Ref.make<MaterializedState>({ generation: 0 });
+ const bumpMaterializedGeneration = Ref.update(materializedRef, (state) => ({
+ generation: state.generation + 1,
+ }));
+
+ const readMaterializedEntry = Effect.gen(function* () {
+ const settings = yield* getSettingsFromCache;
+ const { generation, entry } = yield* Ref.get(materializedRef);
+ return {
+ settings,
+ generation,
+ cached: entry?.source === settings ? entry.materialized : undefined,
+ };
+ });
+ // Misses run one at a time so a burst of reads after a change shares one
+ // secret read instead of each materializing the same settings.
+ const materializeSemaphore = yield* Semaphore.make(1);
+ const getMaterializedSettings = Effect.gen(function* () {
+ const first = yield* readMaterializedEntry;
+ if (first.cached) return first.cached;
+ return yield* materializeSemaphore.withPermits(1)(
+ Effect.gen(function* () {
+ const { settings, generation, cached } = yield* readMaterializedEntry;
+ if (cached) return cached;
+ const materialized = yield* materializeAllSecrets(settings);
+ yield* Ref.update(materializedRef, (state) =>
+ state.generation === generation
+ ? { generation, entry: { source: settings, materialized } }
+ : state,
+ );
+ return materialized;
+ }),
+ );
+ });
+
type SecretSnapshot = {
readonly name: string;
readonly previous: Option.Option<Uint8Array>;
@@ -1159,13 +1206,11 @@ const make = Effect.gen(function* () {
return {
start,
ready: Deferred.await(startedDeferred),
- getSettings: getSettingsFromCache.pipe(
- Effect.flatMap(materializeAllSecrets),
- Effect.map(resolveTextGenerationProvider),
- ),
+ getSettings: getMaterializedSettings.pipe(Effect.map(resolveTextGenerationProvider)),
updateSettings: (patch) =>
writeSemaphore.withPermits(1)(
Effect.gen(function* () {
+ yield* bumpMaterializedGeneration;
const current = yield* getSettingsFromCache;
const patched = applyServerSettingsPatch(current, patch);
const sandboxMaterialized = yield* materializeSandboxEnvironmentSecrets(patched);
@@ -1223,7 +1268,7 @@ const make = Effect.gen(function* () {
}
const materialized = yield* materializeAllSecrets(next);
return resolveTextGenerationProvider(materialized);
- }),
+ }).pipe(Effect.ensuring(bumpMaterializedGeneration)),
),
get streamChanges() {
return materializeChanges(Stream.fromPubSub(changesPubSub));
diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts
index 27b2909f8..fcfbcf8aa 100644
--- a/apps/server/src/ws.ts
+++ b/apps/server/src/ws.ts
@@ -118,6 +118,7 @@ import {
import { decideCommandSequence } from "./orchestration/decider.ts";
import * as OrchestrationEngine from "./orchestration/Services/OrchestrationEngine.ts";
import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts";
+import { toShellDelegation } from "./orchestration/ShellDelegations.ts";
import { ThreadDeletionReactor } from "./orchestration/Services/ThreadDeletionReactor.ts";
import * as ProjectionBots from "./persistence/Services/ProjectionBots.ts";
import * as ProjectionGroups from "./persistence/Services/ProjectionGroups.ts";
@@ -986,7 +987,7 @@ const makeWsRpcLayer = (
Option.some({
kind: "delegation-upserted" as const,
sequence: event.sequence,
- delegation: event.payload.delegation,
+ delegation: toShellDelegation(event.payload.delegation),
}),
);
case "routine.drafted":
diff --git a/docs/user/bots.md b/docs/user/bots.md
index 10e74d4dc..7e1b4bab3 100644
--- a/docs/user/bots.md
+++ b/docs/user/bots.md
@@ -82,6 +82,11 @@ can change **Right Panel: Toggle** in keybinding settings. Narrow screens open t
Bot replies support headings, links, tables, task lists, code blocks, math, and Mermaid diagrams.
During longer bot work, the bot posts short status notes after meaningful progress.
+When a bot hands work to another bot or group, the chat shows a card for each handoff. Cards for
+work that is still running always stay. For finished, failed, or canceled work, the chat keeps the
+20 most recent cards, and a long result shows its first 2,000 characters. Open the delegated chat to
+read the full result.
+
## Voice calls
1. Open **Settings > Voice**.
diff --git a/packages/client-runtime/src/state/shellReducer.test.ts b/packages/client-runtime/src/state/shellReducer.test.ts
index 53ee5787f..cd70b13a5 100644
--- a/packages/client-runtime/src/state/shellReducer.test.ts
+++ b/packages/client-runtime/src/state/shellReducer.test.ts
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vite-plus/test";
import {
AkeruDelegationRecord,
BotId,
+ DelegationId,
GroupId,
McpServerId,
ProjectId,
@@ -10,6 +11,7 @@ import {
RoutineId,
RoutineRunId,
SkillAssignmentId,
+ SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD,
SkillId,
ThreadId,
} from "@t3tools/contracts";
@@ -404,6 +406,72 @@ describe("applyShellStreamEvent", () => {
expect(updated.delegations[0]?.state).toBe("running");
expect(updated.snapshotSequence).toBe(10);
});
+
+ it("keeps only the newest finished delegations per parent thread", () => {
+ const finished = (index: number) => ({
+ ...stubDelegation,
+ delegationId: DelegationId.make(`delegation-finished-${index}`),
+ state: "completed" as const,
+ updatedAt: `2026-04-02T00:${String(index).padStart(2, "0")}:00.000Z`,
+ });
+ const open = { ...stubDelegation, state: "running" as const };
+ const otherThread = {
+ ...finished(0),
+ delegationId: DelegationId.make("delegation-other-thread"),
+ parentThreadId: ThreadId.make("thread-other"),
+ };
+ let snapshot = applyShellStreamEvent(baseSnapshot, {
+ kind: "delegation-upserted",
+ sequence: 1,
+ delegation: open,
+ });
+ snapshot = applyShellStreamEvent(snapshot, {
+ kind: "delegation-upserted",
+ sequence: 2,
+ delegation: otherThread,
+ });
+ const finishedCount = SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD + 5;
+ for (let index = 0; index < finishedCount; index++) {
+ snapshot = applyShellStreamEvent(snapshot, {
+ kind: "delegation-upserted",
+ sequence: index + 3,
+ delegation: finished(index),
+ });
+ }
+
+ const ids = snapshot.delegations.map((delegation) => delegation.delegationId);
+ expect(ids).toContain(open.delegationId);
+ expect(ids).toContain(otherThread.delegationId);
+ expect(ids).not.toContain("delegation-finished-4");
+ expect(ids).toContain("delegation-finished-5");
+ expect(ids).toHaveLength(SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD + 2);
+ });
+
+ it("breaks finished delegation ties the same way as the shell snapshot", () => {
+ const tied = (index: number) => ({
+ ...stubDelegation,
+ delegationId: DelegationId.make(`delegation-tied-${String(index).padStart(2, "0")}`),
+ state: "completed" as const,
+ createdAt: `2026-04-01T00:${String(index).padStart(2, "0")}:00.000Z`,
+ updatedAt: "2026-04-02T00:00:00.000Z",
+ });
+ let snapshot = baseSnapshot;
+ // Arrive newest-created first, so arrival order disagrees with the ranking.
+ for (let index = SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD; index >= 0; index--) {
+ snapshot = applyShellStreamEvent(snapshot, {
+ kind: "delegation-upserted",
+ sequence: SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD - index + 1,
+ delegation: tied(index),
+ });
+ }
+
+ const ids = snapshot.delegations.map((delegation) => delegation.delegationId);
+ expect(ids).toHaveLength(SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD);
+ expect(ids).toContain("delegation-tied-00");
+ expect(ids).not.toContain(
+ `delegation-tied-${String(SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD).padStart(2, "0")}`,
+ );
+ });
});
describe("thread-upserted", () => {
diff --git a/packages/client-runtime/src/state/shellReducer.ts b/packages/client-runtime/src/state/shellReducer.ts
index 7f3872f86..1fe7269a9 100644
--- a/packages/client-runtime/src/state/shellReducer.ts
+++ b/packages/client-runtime/src/state/shellReducer.ts
@@ -1,5 +1,11 @@
import * as Arr from "effect/Array";
-import type { OrchestrationShellSnapshot, OrchestrationShellStreamEvent } from "@t3tools/contracts";
+import {
+ isTerminalDelegationState,
+ SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD,
+ type AkeruDelegationRecord,
+ type OrchestrationShellSnapshot,
+ type OrchestrationShellStreamEvent,
+} from "@t3tools/contracts";
/**
* Reduce a single shell stream event into an existing snapshot, returning a new
@@ -74,7 +80,13 @@ export function applyShellStreamEvent(
: delegation,
)
: Arr.append(snapshot.delegations, event.delegation);
- return { ...snapshot, delegations, snapshotSequence: event.sequence };
+ return {
+ ...snapshot,
+ delegations: isTerminalDelegationState(event.delegation.state)
+ ? capTerminalDelegations(delegations, event.delegation.parentThreadId)
+ : delegations,
+ snapshotSequence: event.sequence,
+ };
}
case "routine-upserted": {
const routines = snapshot.routines ?? [];
@@ -147,3 +159,34 @@ export function applyShellStreamEvent(
return snapshot;
}
}
+
+/**
+ * Keeps the newest finished delegations for one parent thread, matching the
+ * limit the server applies to shell snapshots.
+ */
+// Binary order, matching SQLite's default collation in the shell snapshot query.
+const compareText = (left: string, right: string) => (left < right ? -1 : left > right ? 1 : 0);
+
+// Keeps the same finished delegations as the shell snapshot, which ranks them
+// by updatedAt DESC, createdAt ASC, delegation_id ASC.
+function capTerminalDelegations(
+ delegations: ReadonlyArray<AkeruDelegationRecord>,
+ parentThreadId: AkeruDelegationRecord["parentThreadId"],
+): ReadonlyArray<AkeruDelegationRecord> {
+ const terminal = delegations.filter(
+ (delegation) =>
+ delegation.parentThreadId === parentThreadId && isTerminalDelegationState(delegation.state),
+ );
+ if (terminal.length <= SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD) return delegations;
+ const dropped = new Set(
+ terminal
+ .toSorted(
+ (left, right) =>
+ compareText(right.updatedAt, left.updatedAt) ||
+ compareText(left.createdAt, right.createdAt) ||
+ compareText(left.delegationId, right.delegationId),
+ )
+ .slice(SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD),
+ );
+ return delegations.filter((delegation) => !dropped.has(delegation));
+}
diff --git a/packages/contracts/src/akeruDelegation.ts b/packages/contracts/src/akeruDelegation.ts
index 64243d0f5..f27edbae9 100644
--- a/packages/contracts/src/akeruDelegation.ts
+++ b/packages/contracts/src/akeruDelegation.ts
@@ -30,6 +30,16 @@ export const AkeruDelegationState = Schema.Literals([
]);
export type AkeruDelegationState = typeof AkeruDelegationState.Type;
+/** Whether a delegation has finished and can no longer change state. */
+export const isTerminalDelegationState = (state: AkeruDelegationState): boolean =>
+ state === "completed" || state === "failed" || state === "canceled";
+
+/**
+ * Finished delegations a shell snapshot keeps per parent thread, newest first.
+ * Open delegations are always kept.
+ */
+export const SHELL_RECENT_TERMINAL_DELEGATIONS_PER_THREAD = 20;
+
export const AkeruDelegationAccessGrant = Schema.Struct({
allowedToolIds: Schema.Array(Schema.suspend(() => AkeruToolId)),
memoryScopes: Schema.Array(AkeruMemoryTargetScope),