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): skip full thread loads on projection and ingestion (#230)

Leo8c430fc36fdd
diff --git a/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts b/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts
index de8ca1925..ee215bb04 100644
--- a/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts
+++ b/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts
@@ -107,6 +107,8 @@ describe("CheckpointDiffQuery.layer", () => {
});
}),
getThreadShellById: () => Effect.succeed(Option.none()),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.succeed(Option.none()),
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
searchThreads: () => Effect.succeed({ matches: [] }),
@@ -202,6 +204,8 @@ describe("CheckpointDiffQuery.layer", () => {
getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
getFullThreadDiffContext: () => Effect.die("unused"),
getThreadShellById: () => Effect.succeed(Option.none()),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.succeed(Option.none()),
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
searchThreads: () => Effect.succeed({ matches: [] }),
@@ -287,6 +291,8 @@ describe("CheckpointDiffQuery.layer", () => {
getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
getFullThreadDiffContext: () => Effect.die("unused"),
getThreadShellById: () => Effect.succeed(Option.none()),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.succeed(Option.none()),
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
searchThreads: () => Effect.succeed({ matches: [] }),
@@ -357,6 +363,8 @@ describe("CheckpointDiffQuery.layer", () => {
getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
getFullThreadDiffContext: () => Effect.die("unused"),
getThreadShellById: () => Effect.succeed(Option.none()),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.succeed(Option.none()),
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
searchThreads: () => Effect.succeed({ matches: [] }),
@@ -412,6 +420,8 @@ describe("CheckpointDiffQuery.layer", () => {
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.succeed(Option.none()),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.succeed(Option.none()),
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
searchThreads: () => Effect.succeed({ matches: [] }),
diff --git a/apps/server/src/orchestration/Layers/CheckpointReactor.ts b/apps/server/src/orchestration/Layers/CheckpointReactor.ts
index e32c84e30..251ce7512 100644
--- a/apps/server/src/orchestration/Layers/CheckpointReactor.ts
+++ b/apps/server/src/orchestration/Layers/CheckpointReactor.ts
@@ -164,7 +164,7 @@ const make = Effect.gen(function* () {
const resolveThreadDetail = Effect.fn("resolveThreadDetail")(function* (threadId: ThreadId) {
return yield* projectionSnapshotQuery
- .getThreadDetailById(threadId)
+ .getThreadDetailById(threadId, { activityKinds: [] })
.pipe(Effect.map(Option.getOrUndefined));
});
diff --git a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts
index a061fc326..3b1ac0d10 100644
--- a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts
+++ b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts
@@ -215,6 +215,8 @@ describe("OrchestrationEngine", () => {
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.succeed(Option.none()),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.succeed(Option.none()),
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
searchThreads: () => Effect.succeed({ matches: [] }),
diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
index 0518fff97..bb309d226 100644
--- a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
@@ -156,12 +156,8 @@ function isStalePendingApprovalFailureDetail(detail: string | null): boolean {
);
}
-// A full refresh loads all thread history, so skip events that cannot change the summary.
+// A refresh reads each persisted summary source, so skip activities that cannot change the result.
function shouldRefreshThreadShellSummary(event: OrchestrationEvent): boolean {
- if (event.type === "thread.message-sent") {
- return event.payload.role === "user";
- }
-
if (event.type !== "thread.activity-appended") {
return true;
}
@@ -225,33 +221,6 @@ function derivePendingUserInputCountFromActivities(
return openRequestIds.size;
}
-function deriveHasActionableProposedPlan(input: {
- readonly latestTurnId: string | null;
- readonly proposedPlans: ReadonlyArray<ProjectionThreadProposedPlan>;
-}): boolean {
- const sorted = [...input.proposedPlans].toSorted(
- (left, right) =>
- left.updatedAt.localeCompare(right.updatedAt) || left.planId.localeCompare(right.planId),
- );
-
- let latestForTurn: ProjectionThreadProposedPlan | null = null;
- if (input.latestTurnId !== null) {
- for (let index = sorted.length - 1; index >= 0; index -= 1) {
- const plan = sorted[index];
- if (plan?.turnId === input.latestTurnId) {
- latestForTurn = plan;
- break;
- }
- }
- }
- if (latestForTurn !== null) {
- return latestForTurn.implementedAt === null;
- }
-
- const latestPlan = sorted.at(-1) ?? null;
- return latestPlan !== null && latestPlan.implementedAt === null;
-}
-
function retainProjectionMessagesAfterRevert(
messages: ReadonlyArray<ProjectionThreadMessage>,
turns: ReadonlyArray<ProjectionTurn>,
@@ -986,31 +955,18 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
return;
}
- const [messages, proposedPlans, activities, pendingApprovals] = yield* Effect.all([
- projectionThreadMessageRepository.listByThreadId({ threadId }),
- projectionThreadProposedPlanRepository.listByThreadId({ threadId }),
- projectionThreadActivityRepository.listByThreadId({ threadId }),
- projectionPendingApprovalRepository.listByThreadId({ threadId }),
- ]);
-
- let latestUserMessageAt: string | null = null;
- for (const message of messages) {
- if (
- message.role === "user" &&
- (latestUserMessageAt === null || message.createdAt > latestUserMessageAt)
- ) {
- latestUserMessageAt = message.createdAt;
- }
- }
+ const [latestUserMessageAt, hasActionableProposedPlan, activities, pendingApprovalCount] =
+ yield* Effect.all([
+ projectionThreadMessageRepository.getLatestUserMessageAt({ threadId }),
+ projectionThreadProposedPlanRepository.hasActionableByThreadId({
+ threadId,
+ latestTurnId: existingRow.value.latestTurnId,
+ }),
+ projectionThreadActivityRepository.listUserInputLifecycleByThreadId({ threadId }),
+ projectionPendingApprovalRepository.countPendingByThreadId({ threadId }),
+ ]);
- const pendingApprovalCount = pendingApprovals.filter(
- (approval) => approval.status === "pending",
- ).length;
const pendingUserInputCount = derivePendingUserInputCountFromActivities(activities);
- const hasActionableProposedPlan = deriveHasActionableProposedPlan({
- latestTurnId: existingRow.value.latestTurnId,
- proposedPlans,
- });
yield* projectionThreadRepository.upsert({
...existingRow.value,
@@ -1317,7 +1273,29 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
return;
}
- case "thread.message-sent":
+ // A message cannot change any summary field except latestUserMessageAt,
+ // which is a monotonic maximum that folds in directly. The full refresh
+ // would re-read every message body in the thread per user message.
+ case "thread.message-sent": {
+ const existingRow = yield* projectionThreadRepository.getById({
+ threadId: event.payload.threadId,
+ });
+ if (Option.isNone(existingRow)) {
+ return;
+ }
+ const previousLatest = existingRow.value.latestUserMessageAt;
+ yield* projectionThreadRepository.upsert({
+ ...existingRow.value,
+ updatedAt: event.occurredAt,
+ latestUserMessageAt:
+ event.payload.role === "user" &&
+ (previousLatest === null || event.payload.createdAt > previousLatest)
+ ? event.payload.createdAt
+ : previousLatest,
+ });
+ return;
+ }
+
case "thread.message-reaction-set":
case "thread.proposed-plan-upserted":
case "thread.activity-appended":
diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts
index 88a3fd87e..5bbb4dd06 100644
--- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts
@@ -570,6 +570,17 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
if (threadDetail._tag === "Some") {
assert.deepEqual(threadDetail.value, snapshot.threads[0]);
}
+
+ const turnStart = yield* snapshotQuery.getTurnStartMessage({
+ threadId: ThreadId.make("thread-1"),
+ messageId: MessageId.make("message-1"),
+ });
+ assert.equal(turnStart._tag, "Some");
+ if (turnStart._tag === "Some") {
+ assert.equal(turnStart.value.message.role, "assistant");
+ assert.equal(turnStart.value.message.text, "hello from projection");
+ assert.equal(turnStart.value.hasOtherUserMessages, false);
+ }
}),
);
diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts
index 62cd23768..65f9b3023 100644
--- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts
@@ -4,6 +4,7 @@ import {
BotEngine,
BotId,
BotUsageCap,
+ GroupId,
ChannelBinding,
ChannelMessageOrigin,
ChatAttachment,
@@ -75,6 +76,7 @@ import {
decodeThreadDetailPageCursor,
encodeThreadDetailPageCursor,
} from "../threadDetailCursor.ts";
+import { projectActivityPayload } from "../ActivityPayloadProjection.ts";
import * as RepositoryIdentityResolver from "../../project/RepositoryIdentityResolver.ts";
import { ORCHESTRATION_PROJECTOR_NAMES } from "./ProjectionPipeline.ts";
import {
@@ -82,6 +84,7 @@ import {
type ProjectionFullThreadDiffContext,
type ProjectionSnapshotCounts,
type ProjectionThreadCheckpointContext,
+ type ProjectionThreadDetailQuery,
type ProjectionSnapshotQueryShape,
} from "../Services/ProjectionSnapshotQuery.ts";
@@ -92,6 +95,9 @@ const decodeThread = Schema.decodeUnknownEffect(OrchestrationThread);
// activity window. Applying the limit in SQL avoids decoding an unbounded
// payload_json set before the projector can enforce that invariant.
const THREAD_DETAIL_ACTIVITY_LIMIT = 500;
+// Snapshot payloads are decoded and projected in small sequential batches so
+// one client read does not retain the raw payloads for the full activity window.
+const THREAD_DETAIL_ACTIVITY_PAYLOAD_BATCH_SIZE = 25;
const ProjectionBotDbRowSchema = ProjectionBot.mapFields(
Struct.assign({
avatar: Schema.fromJsonString(BotAvatar),
@@ -135,9 +141,34 @@ const ProjectionThreadActivityDbRowSchema = ProjectionThreadActivity.mapFields(
sequence: Schema.NullOr(NonNegativeInt),
}),
);
+const ProjectionThreadActivityIdRowSchema = Schema.Struct({
+ activityId: ProjectionThreadActivity.fields.activityId,
+});
+const ProjectionTurnStartMessageDbRowSchema = Schema.Struct({
+ messageId: ProjectionThreadMessage.fields.messageId,
+ threadId: ProjectionThreadMessage.fields.threadId,
+ turnId: ProjectionThreadMessage.fields.turnId,
+ role: ProjectionThreadMessage.fields.role,
+ text: ProjectionThreadMessage.fields.text,
+ attachments: Schema.NullOr(Schema.fromJsonString(Schema.Array(ChatAttachment))),
+ isStreaming: Schema.Number,
+ createdAt: ProjectionThreadMessage.fields.createdAt,
+ updatedAt: ProjectionThreadMessage.fields.updatedAt,
+ hasOtherUserMessages: Schema.Union([Schema.Number, Schema.Boolean]),
+});
const ProjectionThreadSessionDbRowSchema = ProjectionThreadSession.mapFields(
Struct.assign({ mcpServerIds: Schema.fromJsonString(Schema.Array(McpServerId)) }),
);
+const ProjectionThreadRuntimeContextDbRowSchema = Schema.Struct({
+ id: ThreadId,
+ title: Schema.String,
+ projectId: ProjectId,
+ botId: Schema.NullOr(BotId),
+ groupId: Schema.NullOr(GroupId),
+ respondingBotId: Schema.NullOr(BotId),
+ runtimeMode: ProjectionThread.fields.runtimeMode,
+ session: Schema.NullOr(ProjectionThreadSessionDbRowSchema),
+});
const ProjectionCheckpointDbRowSchema = ProjectionCheckpoint.mapFields(
Struct.assign({
files: Schema.fromJsonString(Schema.Array(OrchestrationCheckpointFile)),
@@ -181,6 +212,17 @@ const ProjectIdLookupInput = Schema.Struct({
const ThreadIdLookupInput = Schema.Struct({
threadId: ThreadId,
});
+const ThreadActivityKindsLookupInput = Schema.Struct({
+ threadId: ThreadId,
+ activityKinds: Schema.Array(Schema.String),
+});
+const ThreadActivityIdsLookupInput = Schema.Struct({
+ activityIds: Schema.Array(ProjectionThreadActivity.fields.activityId),
+});
+const TurnStartMessageLookupInput = Schema.Struct({
+ threadId: ThreadId,
+ messageId: MessageId,
+});
// Windowed reads order turns by the stable keyset (anchor, turn key), where
// anchor is requested_at and turn key is
// COALESCE(turn_id, ''). Both are event-derived, so cursors survive the
@@ -428,6 +470,44 @@ function mapProposedPlanRow(
};
}
+function mapThreadActivityRow(
+ row: Schema.Schema.Type<typeof ProjectionThreadActivityDbRowSchema>,
+): OrchestrationThreadActivity {
+ return {
+ id: row.activityId,
+ tone: row.tone,
+ kind: row.kind,
+ summary: row.summary,
+ payload: row.payload,
+ turnId: row.turnId,
+ createdAt: row.createdAt,
+ ...(row.sequence !== null ? { sequence: row.sequence } : {}),
+ };
+}
+
+function mapThreadMessageRow(
+ row: Schema.Schema.Type<typeof ProjectionThreadMessageDbRowSchema>,
+): OrchestrationMessage {
+ const message = {
+ id: row.messageId,
+ role: row.role,
+ text: row.text,
+ turnId: row.turnId,
+ respondingBotId: row.respondingBotId ?? null,
+ ...(row.authorPersonId ? { authorPersonId: row.authorPersonId } : {}),
+ ...(row.authorDisplayName ? { authorDisplayName: row.authorDisplayName } : {}),
+ ...(row.channelOrigin ? { channelOrigin: row.channelOrigin } : {}),
+ reactions: row.reactions,
+ streaming: row.isStreaming === 1,
+ createdAt: row.createdAt,
+ updatedAt: row.updatedAt,
+ };
+ if (row.attachments !== null) {
+ return Object.assign(message, { attachments: row.attachments });
+ }
+ return message;
+}
+
function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
return (cause: unknown): ProjectionRepositoryError =>
Schema.isSchemaError(cause)
@@ -1225,6 +1305,175 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
`,
});
+ const listThreadActivityIdsByThread = SqlSchema.findAll({
+ Request: ThreadIdLookupInput,
+ Result: ProjectionThreadActivityIdRowSchema,
+ execute: ({ threadId }) =>
+ sql`
+ SELECT activity_id AS "activityId"
+ FROM projection_thread_activities
+ WHERE thread_id = ${threadId}
+ ORDER BY
+ sequence DESC,
+ created_at DESC,
+ activity_id DESC
+ LIMIT ${THREAD_DETAIL_ACTIVITY_LIMIT}
+ `,
+ });
+
+ const listThreadActivityRowsByIds = SqlSchema.findAll({
+ Request: ThreadActivityIdsLookupInput,
+ Result: ProjectionThreadActivityDbRowSchema,
+ execute: ({ activityIds }) =>
+ sql`
+ SELECT
+ activity_id AS "activityId",
+ thread_id AS "threadId",
+ turn_id AS "turnId",
+ tone,
+ kind,
+ summary,
+ payload_json AS "payload",
+ sequence,
+ created_at AS "createdAt"
+ FROM projection_thread_activities
+ -- The selectors already scoped these globally unique ids to the
+ -- thread inside this transaction. Keep this as a primary-key lookup.
+ WHERE ${sql.in("activity_id", activityIds)}
+ `,
+ });
+
+ const listThreadActivityRowsByThreadAndKinds = SqlSchema.findAll({
+ Request: ThreadActivityKindsLookupInput,
+ Result: ProjectionThreadActivityDbRowSchema,
+ execute: ({ threadId, activityKinds }) =>
+ sql`
+ SELECT
+ activity_id AS "activityId",
+ thread_id AS "threadId",
+ turn_id AS "turnId",
+ tone,
+ kind,
+ summary,
+ payload_json AS "payload",
+ sequence,
+ created_at AS "createdAt"
+ FROM (
+ SELECT
+ activity_id,
+ thread_id,
+ turn_id,
+ tone,
+ kind,
+ summary,
+ payload_json,
+ sequence,
+ created_at
+ FROM projection_thread_activities
+ WHERE thread_id = ${threadId}
+ AND ${sql.in("kind", activityKinds)}
+ ORDER BY
+ sequence DESC,
+ created_at DESC,
+ activity_id DESC
+ LIMIT ${THREAD_DETAIL_ACTIVITY_LIMIT}
+ ) AS recent_activities
+ ORDER BY
+ sequence ASC,
+ created_at ASC,
+ activity_id ASC
+ `,
+ });
+
+ const getThreadRuntimeContextRow = SqlSchema.findOneOption({
+ Request: ThreadIdLookupInput,
+ Result: ProjectionThreadRuntimeContextDbRowSchema,
+ execute: ({ threadId }) =>
+ sql`
+ SELECT
+ threads.thread_id AS id,
+ threads.title,
+ threads.project_id AS "projectId",
+ threads.bot_id AS "botId",
+ threads.group_id AS "groupId",
+ threads.responding_bot_id AS "respondingBotId",
+ threads.runtime_mode AS "runtimeMode",
+ sessions.thread_id AS "threadId",
+ sessions.status,
+ sessions.provider_name AS "providerName",
+ sessions.provider_instance_id AS "providerInstanceId",
+ sessions.runtime_mode AS "sessionRuntimeMode",
+ sessions.mcp_server_ids_json AS "mcpServerIds",
+ sessions.active_turn_id AS "activeTurnId",
+ sessions.last_error AS "lastError",
+ sessions.updated_at AS "updatedAt"
+ FROM projection_threads AS threads
+ LEFT JOIN projection_thread_sessions AS sessions
+ ON sessions.thread_id = threads.thread_id
+ WHERE threads.thread_id = ${threadId}
+ AND threads.deleted_at IS NULL
+ AND threads.archived_at IS NULL
+ LIMIT 1
+ `.pipe(
+ Effect.map((rows) =>
+ rows.map((row) => ({
+ id: row.id,
+ title: row.title,
+ projectId: row.projectId,
+ botId: row.botId,
+ groupId: row.groupId,
+ respondingBotId: row.respondingBotId,
+ runtimeMode: row.runtimeMode,
+ session:
+ row.threadId === null
+ ? null
+ : {
+ threadId: row.threadId,
+ status: row.status,
+ providerName: row.providerName,
+ providerInstanceId: row.providerInstanceId,
+ runtimeMode: row.sessionRuntimeMode,
+ mcpServerIds: row.mcpServerIds,
+ activeTurnId: row.activeTurnId,
+ lastError: row.lastError,
+ updatedAt: row.updatedAt,
+ },
+ })),
+ ),
+ ),
+ });
+
+ const getTurnStartMessageRow = SqlSchema.findOneOption({
+ Request: TurnStartMessageLookupInput,
+ Result: ProjectionTurnStartMessageDbRowSchema,
+ execute: ({ threadId, messageId }) => sql`
+ SELECT
+ message_id AS "messageId",
+ thread_id AS "threadId",
+ turn_id AS "turnId",
+ role,
+ text,
+ attachments_json AS "attachments",
+ is_streaming AS "isStreaming",
+ created_at AS "createdAt",
+ updated_at AS "updatedAt",
+ EXISTS (
+ SELECT 1
+ FROM projection_thread_messages AS other
+ WHERE other.thread_id = ${threadId}
+ AND other.message_id != ${messageId}
+ AND other.role = 'user'
+ AND (
+ LOWER(TRIM(other.text)) != '/compact'
+ OR COALESCE(json_array_length(other.attachments_json), 0) > 0
+ )
+ ) AS "hasOtherUserMessages"
+ FROM projection_thread_messages
+ WHERE thread_id = ${threadId} AND message_id = ${messageId}
+ LIMIT 1
+ `,
+ });
+
const getThreadSessionRowByThread = SqlSchema.findOneOption({
Request: ThreadIdLookupInput,
Result: ProjectionThreadSessionDbRowSchema,
@@ -1442,15 +1691,8 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
`,
});
- // Blocking request payloads must remain available even if they predate the
- // recent activity window. Each CTE returns at most one unresolved row per
- // request, so the merge below stays bounded by actionable work.
- const listPinnedThreadActivityRowsByThread = SqlSchema.findAll({
- Request: ThreadIdLookupInput,
- Result: ProjectionThreadActivityDbRowSchema,
- execute: ({ threadId }) =>
- sql`
- WITH pending_approval_requests AS (
+ const pinnedThreadActivityIdsCte = (threadId: string) => sql`
+pending_approval_requests AS (
SELECT request_id, thread_id
FROM projection_pending_approvals
WHERE thread_id = ${threadId}
@@ -1514,6 +1756,17 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
WHERE request_order = 1
AND kind = 'user-input.requested'
)
+ `;
+
+ // Blocking request payloads must remain available even if they predate the
+ // recent activity window. Each CTE returns at most one unresolved row per
+ // request, so the merge below stays bounded by actionable work.
+ const listPinnedThreadActivityRowsByThread = SqlSchema.findAll({
+ Request: ThreadIdLookupInput,
+ Result: ProjectionThreadActivityDbRowSchema,
+ execute: ({ threadId }) =>
+ sql`
+ WITH ${pinnedThreadActivityIdsCte(threadId)}
SELECT
activity.activity_id AS "activityId",
activity.thread_id AS "threadId",
@@ -1531,6 +1784,17 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
`,
});
+ const listPinnedThreadActivityIdsByThread = SqlSchema.findAll({
+ Request: ThreadIdLookupInput,
+ Result: ProjectionThreadActivityIdRowSchema,
+ execute: ({ threadId }) =>
+ sql`
+ WITH ${pinnedThreadActivityIdsCte(threadId)}
+ SELECT activity_id AS "activityId"
+ FROM pinned_activity_ids
+ `,
+ });
+
const listThreadActivityRowsByThreadWindow = SqlSchema.findAll({
Request: ThreadTurnRangeLookupInput,
Result: ProjectionThreadActivityDbRowSchema,
@@ -1598,6 +1862,48 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
`,
});
+ const listThreadActivityIdsByThreadWindow = SqlSchema.findAll({
+ Request: ThreadTurnRangeLookupInput,
+ Result: ProjectionThreadActivityIdRowSchema,
+ execute: ({ threadId, minAnchorAt, minTurnKey, beforeAnchorAt, beforeTurnKey }) =>
+ sql`
+ SELECT activity_id AS "activityId"
+ FROM projection_thread_activities
+ WHERE thread_id = ${threadId}
+ AND (
+ turn_id IN (
+ SELECT turn_id FROM projection_turns
+ WHERE thread_id = ${threadId}
+ AND turn_id IS NOT NULL
+ AND (
+ requested_at > ${minAnchorAt}
+ OR (
+ requested_at = ${minAnchorAt}
+ AND turn_id >= ${minTurnKey}
+ )
+ )
+ AND (
+ requested_at < ${beforeAnchorAt}
+ OR (
+ requested_at = ${beforeAnchorAt}
+ AND turn_id < ${beforeTurnKey}
+ )
+ )
+ )
+ OR (
+ turn_id IS NULL
+ AND created_at >= ${minAnchorAt}
+ AND created_at < ${beforeAnchorAt}
+ )
+ )
+ ORDER BY
+ sequence DESC,
+ created_at DESC,
+ activity_id DESC
+ LIMIT ${THREAD_DETAIL_ACTIVITY_LIMIT}
+ `,
+ });
+
const getFullThreadDiffContextRow = SqlSchema.findOneOption({
Request: FullThreadDiffContextLookupInput,
Result: ProjectionFullThreadDiffContextRowSchema,
@@ -2894,6 +3200,54 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
} satisfies OrchestrationThreadShell);
});
+ const getThreadRuntimeContext: ProjectionSnapshotQueryShape["getThreadRuntimeContext"] =
+ Effect.fn("ProjectionSnapshotQuery.getThreadRuntimeContext")(function* (threadId) {
+ const context = yield* getThreadRuntimeContextRow({ threadId }).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionSnapshotQuery.getThreadRuntimeContext:query",
+ "ProjectionSnapshotQuery.getThreadRuntimeContext:decodeRow",
+ ),
+ ),
+ );
+ return Option.map(context, (row) => ({
+ id: row.id,
+ title: row.title,
+ projectId: row.projectId,
+ botId: row.botId,
+ groupId: row.groupId,
+ respondingBotId: row.respondingBotId ?? null,
+ runtimeMode: row.runtimeMode,
+ session: row.session === null ? null : mapSessionRow(row.session),
+ }));
+ });
+
+ const getTurnStartMessage: ProjectionSnapshotQueryShape["getTurnStartMessage"] = Effect.fn(
+ "ProjectionSnapshotQuery.getTurnStartMessage",
+ )(function* (input) {
+ const message = yield* getTurnStartMessageRow(input).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionSnapshotQuery.getTurnStartMessage:query",
+ "ProjectionSnapshotQuery.getTurnStartMessage:decodeRow",
+ ),
+ ),
+ );
+ return Option.map(message, (row) => ({
+ message: {
+ id: row.messageId,
+ role: row.role,
+ text: row.text,
+ turnId: row.turnId,
+ streaming: row.isStreaming === 1,
+ createdAt: row.createdAt,
+ updatedAt: row.updatedAt,
+ ...(row.attachments !== null ? { attachments: row.attachments } : {}),
+ },
+ hasOtherUserMessages: row.hasOtherUserMessages === 1 || row.hasOtherUserMessages === true,
+ }));
+ });
+
// Contiguous turn range bounding a windowed detail read; undefined loads the
// full thread. Resolved from a window request inside the snapshot
// transaction (see getThreadDetailSnapshot).
@@ -2904,14 +3258,136 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
readonly beforeTurnKey: string;
}
- const getThreadDetailByIdBounded = (threadId: ThreadId, bounds: ThreadDetailBounds | undefined) =>
+ type ThreadDetailActivityRead =
+ | {
+ readonly mode: "raw";
+ readonly query?: ProjectionThreadDetailQuery;
+ }
+ | {
+ readonly mode: "client";
+ };
+
+ const listProjectedThreadActivities = Effect.fn(
+ "ProjectionSnapshotQuery.listProjectedThreadActivities",
+ )(function* (threadId: ThreadId, bounds: ThreadDetailBounds | undefined) {
+ const [activityIdRows, pinnedActivityIdRows] = yield* Effect.all([
+ (bounds === undefined
+ ? listThreadActivityIdsByThread({ threadId })
+ : listThreadActivityIdsByThreadWindow({ threadId, ...bounds })
+ ).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionSnapshotQuery.getThreadDetailById:listActivityIds:query",
+ "ProjectionSnapshotQuery.getThreadDetailById:listActivityIds:decodeRows",
+ ),
+ ),
+ ),
+ listPinnedThreadActivityIdsByThread({ threadId }).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivityIds:query",
+ "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivityIds:decodeRows",
+ ),
+ ),
+ ),
+ ]);
+ const activityIds = [
+ ...new Set([...activityIdRows, ...pinnedActivityIdRows].map(({ activityId }) => activityId)),
+ ];
+ const activities: OrchestrationThreadActivity[] = [];
+
+ for (
+ let offset = 0;
+ offset < activityIds.length;
+ offset += THREAD_DETAIL_ACTIVITY_PAYLOAD_BATCH_SIZE
+ ) {
+ const batchIds = activityIds.slice(
+ offset,
+ offset + THREAD_DETAIL_ACTIVITY_PAYLOAD_BATCH_SIZE,
+ );
+ const batchRows = yield* listThreadActivityRowsByIds({ activityIds: batchIds }).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionSnapshotQuery.getThreadDetailById:listActivityPayloadBatch:query",
+ "ProjectionSnapshotQuery.getThreadDetailById:listActivityPayloadBatch:decodeRows",
+ ),
+ ),
+ );
+ for (const row of batchRows) {
+ activities.push(projectActivityPayload(mapThreadActivityRow(row)));
+ }
+ }
+
+ return activities.toSorted(
+ (left, right) =>
+ (left.sequence ?? -1) - (right.sequence ?? -1) ||
+ left.createdAt.localeCompare(right.createdAt) ||
+ left.id.localeCompare(right.id),
+ );
+ });
+
+ const getThreadDetailByIdBounded = (
+ threadId: ThreadId,
+ bounds: ThreadDetailBounds | undefined,
+ activityRead: ThreadDetailActivityRead = { mode: "raw" },
+ ) =>
Effect.gen(function* () {
+ const activitiesEffect =
+ activityRead.mode === "client"
+ ? listProjectedThreadActivities(threadId, bounds)
+ : Effect.all([
+ (activityRead.query?.activityKinds === undefined
+ ? bounds === undefined
+ ? listThreadActivityRowsByThread({ threadId })
+ : listThreadActivityRowsByThreadWindow({ threadId, ...bounds })
+ : activityRead.query.activityKinds.length === 0
+ ? Effect.succeed([])
+ : listThreadActivityRowsByThreadAndKinds({
+ threadId,
+ activityKinds: activityRead.query.activityKinds,
+ })
+ ).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionSnapshotQuery.getThreadDetailById:listActivities:query",
+ "ProjectionSnapshotQuery.getThreadDetailById:listActivities:decodeRows",
+ ),
+ ),
+ ),
+ activityRead.query?.activityKinds === undefined
+ ? listPinnedThreadActivityRowsByThread({ threadId }).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivities:query",
+ "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivities:decodeRows",
+ ),
+ ),
+ )
+ : Effect.succeed([]),
+ ]).pipe(
+ Effect.map(([activityRows, pinnedActivityRows]) =>
+ [
+ ...new Map(
+ [...activityRows, ...pinnedActivityRows].map(
+ (row) => [row.activityId, row] as const,
+ ),
+ ).values(),
+ ]
+ .toSorted(
+ (left, right) =>
+ (left.sequence ?? -1) - (right.sequence ?? -1) ||
+ left.createdAt.localeCompare(right.createdAt) ||
+ left.activityId.localeCompare(right.activityId),
+ )
+ .map(mapThreadActivityRow),
+ ),
+ );
+
const [
threadRow,
messageRows,
proposedPlanRows,
- activityRows,
- pinnedActivityRows,
+ activities,
checkpointRows,
latestTurnRow,
sessionRow,
@@ -2943,25 +3419,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
),
),
),
- (bounds === undefined
- ? listThreadActivityRowsByThread({ threadId })
- : listThreadActivityRowsByThreadWindow({ threadId, ...bounds })
- ).pipe(
- Effect.mapError(
- toPersistenceSqlOrDecodeError(
- "ProjectionSnapshotQuery.getThreadDetailById:listActivities:query",
- "ProjectionSnapshotQuery.getThreadDetailById:listActivities:decodeRows",
- ),
- ),
- ),
- listPinnedThreadActivityRowsByThread({ threadId }).pipe(
- Effect.mapError(
- toPersistenceSqlOrDecodeError(
- "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivities:query",
- "ProjectionSnapshotQuery.getThreadDetailById:listPinnedActivities:decodeRows",
- ),
- ),
- ),
+ activitiesEffect,
listCheckpointRowsByThread({ threadId }).pipe(
Effect.mapError(
toPersistenceSqlOrDecodeError(
@@ -2992,17 +3450,6 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
return Option.none<OrchestrationThread>();
}
- const selectedActivityRows = [
- ...new Map(
- [...activityRows, ...pinnedActivityRows].map((row) => [row.activityId, row] as const),
- ).values(),
- ].toSorted(
- (left, right) =>
- (left.sequence ?? -1) - (right.sequence ?? -1) ||
- left.createdAt.localeCompare(right.createdAt) ||
- left.activityId.localeCompare(right.activityId),
- );
-
const thread = {
id: threadRow.value.threadId,
projectId: threadRow.value.projectId,
@@ -3031,42 +3478,9 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
pinOrderKey: threadRow.value.pinOrderKey ?? null,
titleRegeneration: mapTitleRegeneration(threadRow.value),
deletedAt: null,
- messages: messageRows.map((row) => {
- const message = {
- id: row.messageId,
- role: row.role,
- text: row.text,
- turnId: row.turnId,
- respondingBotId: row.respondingBotId ?? null,
- ...(row.authorPersonId ? { authorPersonId: row.authorPersonId } : {}),
- ...(row.authorDisplayName ? { authorDisplayName: row.authorDisplayName } : {}),
- ...(row.channelOrigin ? { channelOrigin: row.channelOrigin } : {}),
- reactions: row.reactions,
- streaming: row.isStreaming === 1,
- createdAt: row.createdAt,
- updatedAt: row.updatedAt,
- };
- if (row.attachments !== null) {
- return Object.assign(message, { attachments: row.attachments });
- }
- return message;
- }),
+ messages: messageRows.map(mapThreadMessageRow),
proposedPlans: proposedPlanRows.map(mapProposedPlanRow),
- activities: selectedActivityRows.map((row) => {
- const activity = {
- id: row.activityId,
- tone: row.tone,
- kind: row.kind,
- summary: row.summary,
- payload: row.payload,
- turnId: row.turnId,
- createdAt: row.createdAt,
- };
- if (row.sequence !== null) {
- return Object.assign(activity, { sequence: row.sequence });
- }
- return activity;
- }),
+ activities,
checkpoints: checkpointRows.map((row) => ({
turnId: row.turnId,
checkpointTurnCount: row.checkpointTurnCount,
@@ -3088,8 +3502,14 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
);
});
- const getThreadDetailById: ProjectionSnapshotQueryShape["getThreadDetailById"] = (threadId) =>
- getThreadDetailByIdBounded(threadId, undefined);
+ const getThreadDetailById: ProjectionSnapshotQueryShape["getThreadDetailById"] = (
+ threadId,
+ query,
+ ) =>
+ getThreadDetailByIdBounded(threadId, undefined, {
+ mode: "raw",
+ ...(query === undefined ? {} : { query }),
+ });
// Bounds pathological fan-out: one user turn that spawned hundreds of
// subagent turns still pages in bounded chunks, at the cost of splitting the
@@ -3113,7 +3533,9 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
.withTransaction(
Effect.gen(function* () {
if (window?.turnLimit === undefined) {
- const thread = yield* getThreadDetailById(threadId);
+ const thread = yield* getThreadDetailByIdBounded(threadId, undefined, {
+ mode: "client",
+ });
if (Option.isNone(thread)) {
return Option.none<OrchestrationThreadDetailSnapshot>();
}
@@ -3166,7 +3588,9 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
? { minAnchorAt: "", minTurnKey: "", beforeAnchorAt: "", beforeTurnKey: "" }
: undefined;
- const thread = yield* getThreadDetailByIdBounded(threadId, emptyBounds ?? bounds);
+ const thread = yield* getThreadDetailByIdBounded(threadId, emptyBounds ?? bounds, {
+ mode: "client",
+ });
if (Option.isNone(thread)) {
return Option.none<OrchestrationThreadDetailSnapshot>();
}
@@ -3248,6 +3672,8 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
getThreadCheckpointContext,
getFullThreadDiffContext,
getThreadShellById,
+ getThreadRuntimeContext,
+ getTurnStartMessage,
getThreadDetailById,
getThreadDetailSnapshot,
} satisfies ProjectionSnapshotQueryShape;
diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts
index 8011810cd..e791594dd 100644
--- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts
+++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts
@@ -19,6 +19,7 @@ import {
CommandId,
DEFAULT_PROVIDER_INTERACTION_MODE,
DelegationId,
+ GroupId,
EventId,
MessageId,
ProjectId,
@@ -62,6 +63,7 @@ import {
providerErrorLabel,
providerErrorLabelFromInstanceHint,
ProviderCommandReactorLive,
+ type ControllerEngineThread,
resolveControllerBotId,
} from "./ProviderCommandReactor.ts";
import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
@@ -114,6 +116,21 @@ describe("ProviderCommandReactor", () => {
"bot-owner",
);
});
+
+ it("accepts a thread shell without messages, activities, or checkpoints", () => {
+ const shell: ControllerEngineThread = {
+ id: ThreadId.make("thread-shell"),
+ botId: BotId.make("bot-owner"),
+ groupId: GroupId.make("group-1"),
+ respondingBotId: BotId.make("bot-specialist"),
+ interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
+ };
+ expect(resolveControllerBotId(shell)).toBe("bot-specialist");
+ expect("messages" in shell).toBe(false);
+ expect("activities" in shell).toBe(false);
+ expect("checkpoints" in shell).toBe(false);
+ expect("deletedAt" in shell).toBe(false);
+ });
let runtime: ManagedRuntime.ManagedRuntime<
OrchestrationEngineService | ProviderCommandReactor | ProjectionSnapshotQuery | BotUsageLedger,
unknown
diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts
index ccdff0c85..532aa8eae 100644
--- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts
+++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts
@@ -14,7 +14,7 @@ import {
ProviderDriverKind,
type ProjectId,
type OrchestrationSession,
- type OrchestrationThread,
+ type OrchestrationThreadShell,
ThreadId,
type ProviderSession,
resolveBotMcpServers,
@@ -80,8 +80,16 @@ const isBotUsageCapExceeded = Schema.is(BotUsageCapExceeded);
const isComposioOperationError = Schema.is(ComposioOperationError);
const isProviderDriverKind = Schema.is(ProviderDriverKind);
+export type ControllerThreadIdentity = Pick<
+ OrchestrationThreadShell,
+ "id" | "botId" | "groupId" | "respondingBotId"
+>;
+
+export type ControllerEngineThread = ControllerThreadIdentity &
+ Pick<OrchestrationThreadShell, "interactionMode">;
+
export function resolveControllerBotId(
- thread: Pick<OrchestrationThread, "botId" | "respondingBotId">,
+ thread: Pick<OrchestrationThreadShell, "botId" | "respondingBotId">,
) {
return thread.respondingBotId ?? thread.botId ?? null;
}
@@ -383,7 +391,7 @@ const make = Effect.gen(function* () {
readThread: (threadId) =>
runPromise(
projectionSnapshotQuery
- .getThreadDetailById(threadId)
+ .getThreadDetailById(threadId, { activityKinds: [] })
.pipe(Effect.map(Option.getOrUndefined)),
),
dispatch: (command) => runPromise(orchestrationEngine.dispatch(command)),
@@ -456,7 +464,7 @@ const make = Effect.gen(function* () {
readonly requestId: string;
readonly createdAt: string;
}) {
- const thread = yield* resolveThread(input.threadId);
+ const thread = yield* resolveThreadDetail(input.threadId);
if (!thread) return;
const messageId = MessageId.make(
`assistant:user-input-response-failed:${input.threadId}:${input.requestId}`,
@@ -492,7 +500,7 @@ const make = Effect.gen(function* () {
readonly requestId: string;
readonly createdAt: string;
}) {
- const thread = yield* resolveThread(input.threadId);
+ const thread = yield* resolveThreadDetail(input.threadId);
if (!thread) return;
const messageId = MessageId.make(
`assistant:approval-response-failed:${input.threadId}:${input.requestId}`,
@@ -563,7 +571,7 @@ const make = Effect.gen(function* () {
readonly detail: string;
readonly createdAt: string;
}) {
- const thread = yield* resolveThread(input.threadId);
+ const thread = yield* resolveThreadShell(input.threadId);
if (!thread) {
return;
}
@@ -638,15 +646,21 @@ const make = Effect.gen(function* () {
);
});
- const resolveThread = Effect.fnUntraced(function* (threadId: ThreadId) {
+ const resolveThreadShell = Effect.fnUntraced(function* (threadId: ThreadId) {
+ return yield* projectionSnapshotQuery
+ .getThreadShellById(threadId)
+ .pipe(Effect.map(Option.getOrUndefined));
+ });
+
+ const resolveThreadDetail = Effect.fnUntraced(function* (threadId: ThreadId) {
return yield* projectionSnapshotQuery
- .getThreadDetailById(threadId)
+ .getThreadDetailById(threadId, { activityKinds: [] })
.pipe(Effect.map(Option.getOrUndefined));
});
const reconcileRestrictiveSessionCleanup = Effect.fn("reconcileRestrictiveSessionCleanup")(
function* (threadId: ThreadId) {
- const thread = yield* resolveThread(threadId);
+ const thread = yield* resolveThreadShell(threadId);
const persistedCleanupRequired =
thread?.runtimeMode === "approval-required" &&
thread.session?.runtimeMode === "full-access" &&
@@ -706,7 +720,7 @@ const make = Effect.gen(function* () {
agentController.inspectEngine(modelSelection);
const resolveControllerEngine = Effect.fnUntraced(function* (
- thread: OrchestrationThread,
+ thread: ControllerEngineThread,
fallback: ModelSelection,
) {
const respondingBotId = resolveControllerBotId(thread);
@@ -727,7 +741,9 @@ const make = Effect.gen(function* () {
return { ...selection, configured: engine !== null };
});
- const resolveControllerMcpServers = Effect.fnUntraced(function* (thread: OrchestrationThread) {
+ const resolveControllerMcpServers = Effect.fnUntraced(function* (
+ thread: ControllerThreadIdentity,
+ ) {
const respondingBotId = resolveControllerBotId(thread);
const bot =
respondingBotId === null
@@ -783,7 +799,7 @@ const make = Effect.gen(function* () {
readonly pendingTurnStart?: boolean;
},
) {
- const thread = yield* resolveThread(threadId);
+ const thread = yield* resolveThreadShell(threadId);
if (!thread) {
return yield* Effect.die(new Error(`Thread '${threadId}' was not found in read model.`));
}
@@ -1124,7 +1140,7 @@ const make = Effect.gen(function* () {
readonly timezone?: string;
readonly createdAt: string;
}) {
- const thread = yield* resolveThread(input.threadId);
+ const thread = yield* resolveThreadShell(input.threadId);
if (!thread) {
return yield* Effect.die(
new Error(`Thread '${input.threadId}' was not found in read model.`),
@@ -1251,7 +1267,7 @@ const make = Effect.gen(function* () {
return { _tag: "Superseded" } as const;
}
- const thread = yield* resolveThread(event.payload.threadId);
+ const thread = yield* resolveThreadDetail(event.payload.threadId);
if (!thread || thread.titleRegeneration?.requestId !== requestId) {
return { _tag: "Superseded" } as const;
}
@@ -1288,7 +1304,7 @@ const make = Effect.gen(function* () {
return { _tag: "Completed", title: undefined } as const;
}
- const latestThread = yield* resolveThread(event.payload.threadId);
+ const latestThread = yield* resolveThreadShell(event.payload.threadId);
if (
!latestThread ||
latestThread.titleRegeneration?.requestId !== requestId ||
@@ -1425,13 +1441,16 @@ const make = Effect.gen(function* () {
return;
}
- const thread = yield* resolveThread(event.payload.threadId);
+ const thread = yield* resolveThreadShell(event.payload.threadId);
if (!thread) {
return;
}
- const message = thread.messages.find((entry) => entry.id === event.payload.messageId);
- if (!message || message.role !== "user") {
+ const turnStart = yield* projectionSnapshotQuery.getTurnStartMessage({
+ threadId: thread.id,
+ messageId: event.payload.messageId,
+ });
+ if (Option.isNone(turnStart) || turnStart.value.message.role !== "user") {
yield* appendProviderFailureActivity({
threadId: event.payload.threadId,
kind: "provider.turn.start.failed",
@@ -1450,11 +1469,11 @@ const make = Effect.gen(function* () {
);
return;
}
+ const { message, hasOtherUserMessages } = turnStart.value;
yield* ensureThreadWorktree(thread);
- const isFirstUserMessageTurn =
- thread.messages.filter((entry) => entry.role === "user").length === 1;
+ const isFirstUserMessageTurn = !hasOtherUserMessages;
if (isFirstUserMessageTurn) {
const generationInput = {
messageText: message.text,
@@ -1617,7 +1636,7 @@ const make = Effect.gen(function* () {
const processTurnInterruptRequested = Effect.fn("processTurnInterruptRequested")(function* (
event: Extract<ProviderIntentEvent, { type: "thread.turn-interrupt-requested" }>,
) {
- const thread = yield* resolveThread(event.payload.threadId);
+ const thread = yield* resolveThreadShell(event.payload.threadId);
if (!thread) {
return;
}
@@ -1640,7 +1659,7 @@ const make = Effect.gen(function* () {
const detail = formatFailureDetail(cause);
return Effect.gen(function* () {
- const latestThread = yield* resolveThread(event.payload.threadId);
+ const latestThread = yield* resolveThreadShell(event.payload.threadId);
const latestSession = latestThread?.session;
if (
!latestSession ||
@@ -1668,7 +1687,7 @@ const make = Effect.gen(function* () {
);
}),
);
- const stoppedThread = yield* resolveThread(event.payload.threadId);
+ const stoppedThread = yield* resolveThreadShell(event.payload.threadId);
const stoppedSession = stoppedThread?.session;
if (
!stoppedSession ||
@@ -1712,7 +1731,7 @@ const make = Effect.gen(function* () {
const processApprovalResponseRequested = Effect.fn("processApprovalResponseRequested")(function* (
event: Extract<ProviderIntentEvent, { type: "thread.approval-response-requested" }>,
) {
- const thread = yield* resolveThread(event.payload.threadId);
+ const thread = yield* resolveThreadShell(event.payload.threadId);
if (!thread) {
return;
}
@@ -1780,7 +1799,7 @@ const make = Effect.gen(function* () {
function* (
event: Extract<ProviderIntentEvent, { type: "thread.user-input-response-requested" }>,
) {
- const thread = yield* resolveThread(event.payload.threadId);
+ const thread = yield* resolveThreadShell(event.payload.threadId);
if (!thread) {
return;
}
@@ -1851,7 +1870,7 @@ const make = Effect.gen(function* () {
event: Extract<ProviderIntentEvent, { type: "thread.session-stop-requested" }>,
restrictiveSessionCleanupConfirmed: boolean,
) {
- const thread = yield* resolveThread(event.payload.threadId);
+ const thread = yield* resolveThreadShell(event.payload.threadId);
if (!thread) {
return;
}
@@ -1913,7 +1932,7 @@ const make = Effect.gen(function* () {
yield* threadTitleRegenerationWorker.enqueue(event);
return;
case "thread.runtime-mode-set": {
- const thread = yield* resolveThread(event.payload.threadId);
+ const thread = yield* resolveThreadShell(event.payload.threadId);
if (!thread?.session || thread.session.status === "stopped") {
return;
}
diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts
index 27e23d8fd..df694fef2 100644
--- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts
+++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts
@@ -57,7 +57,10 @@ import { OrchestrationProjectionPipelineLive } from "./ProjectionPipeline.ts";
import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";
import * as ThreadBackgroundLiveness from "../ThreadBackgroundLiveness.ts";
import * as ThreadPlanProgress from "../ThreadPlanProgress.ts";
-import { ProviderRuntimeIngestionLive } from "./ProviderRuntimeIngestion.ts";
+import {
+ findTaskTitleInActivities,
+ ProviderRuntimeIngestionLive,
+} from "./ProviderRuntimeIngestion.ts";
import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
import { ProviderRuntimeIngestionService } from "../Services/ProviderRuntimeIngestion.ts";
import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
@@ -72,6 +75,44 @@ function makeTestServerSettingsLayer(overrides: Partial<ServerSettings> = {}) {
return ServerSettingsService.layerTest(overrides);
}
+describe("findTaskTitleInActivities", () => {
+ it("reads a title from a projection activity record that uses activityId instead of id", () => {
+ expect(
+ findTaskTitleInActivities(
+ [
+ {
+ kind: "task.started",
+ payload: { taskId: "task-1", title: "Typecheck mobile app" },
+ },
+ ],
+ "task-1",
+ ),
+ ).toBe("Typecheck mobile app");
+ });
+
+ it("prefers the latest matching progress title and ignores other tasks", () => {
+ expect(
+ findTaskTitleInActivities(
+ [
+ {
+ kind: "task.started",
+ payload: { taskId: "task-1", title: "first name" },
+ },
+ {
+ kind: "task.progress",
+ payload: { taskId: "task-2", title: "other task" },
+ },
+ {
+ kind: "task.progress",
+ payload: { taskId: "task-1", title: "latest name" },
+ },
+ ],
+ "task-1",
+ ),
+ ).toBe("latest name");
+ });
+});
+
const asProjectId = (value: string): ProjectId => ProjectId.make(value);
const asItemId = (value: string): ProviderItemId => ProviderItemId.make(value);
const asEventId = (value: string): EventId => EventId.make(value);
diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
index 75fab6570..37402dc94 100644
--- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
+++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
@@ -8,8 +8,7 @@ import {
CommandId,
MessageId,
type OrchestrationEvent,
- type OrchestrationMessage,
- type OrchestrationProposedPlanId,
+ OrchestrationProposedPlanId,
CheckpointRef,
classifyTaskAgentKind,
EventId,
@@ -18,8 +17,6 @@ import {
type ThreadTokenUsageSnapshot,
TurnId,
type OrchestrationCheckpointSummary,
- type OrchestrationProposedPlan,
- type OrchestrationThread,
type OrchestrationThreadActivity,
type OrchestrationThreadShell,
type ProviderRuntimeEvent,
@@ -41,6 +38,12 @@ import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
import { AgentController } from "../../provider/Services/AgentController.ts";
import { ProjectionTurnRepository } from "../../persistence/Services/ProjectionTurns.ts";
import { ProjectionTurnRepositoryLive } from "../../persistence/Layers/ProjectionTurns.ts";
+import { ProjectionThreadActivityRepository } from "../../persistence/Services/ProjectionThreadActivities.ts";
+import { ProjectionThreadActivityRepositoryLive } from "../../persistence/Layers/ProjectionThreadActivities.ts";
+import { ProjectionThreadMessageRepository } from "../../persistence/Services/ProjectionThreadMessages.ts";
+import { ProjectionThreadMessageRepositoryLive } from "../../persistence/Layers/ProjectionThreadMessages.ts";
+import { ProjectionThreadProposedPlanRepository } from "../../persistence/Services/ProjectionThreadProposedPlans.ts";
+import { ProjectionThreadProposedPlanRepositoryLive } from "../../persistence/Layers/ProjectionThreadProposedPlans.ts";
import { isGitRepository } from "../../git/Utils.ts";
import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
import { ThreadBackgroundLivenessService } from "../ThreadBackgroundLiveness.ts";
@@ -71,8 +74,13 @@ const providerTaskKey = (threadId: ThreadId, taskId: string) => `${threadId}:${t
// Fallback when the in-memory description cache no longer has the task name
// (server restart, session-exit sweep, TTL/capacity eviction): earlier
// task.started/task.progress activities for the task are persisted with it.
-function findTaskTitleInActivities(
- activities: ReadonlyArray<OrchestrationThreadActivity> | undefined,
+export function findTaskTitleInActivities(
+ activities:
+ | ReadonlyArray<{
+ readonly kind: string;
+ readonly payload: unknown;
+ }>
+ | undefined,
taskId: string,
): string | undefined {
if (!activities) {
@@ -175,57 +183,6 @@ function sameId(left: string | null | undefined, right: string | null | undefine
return left === right;
}
-function hasAssistantMessageForTurn(
- messages: ReadonlyArray<OrchestrationMessage>,
- turnId: TurnId,
- options?: { readonly streamingOnly?: boolean },
-): boolean {
- for (let index = 0; index < messages.length; index += 1) {
- const message = messages[index];
- if (!message) {
- continue;
- }
- if (message.role !== "assistant" || message.turnId !== turnId) {
- continue;
- }
- if (options?.streamingOnly === true && !message.streaming) {
- continue;
- }
- return true;
- }
- return false;
-}
-
-function findMessageById(
- messages: ReadonlyArray<OrchestrationMessage>,
- messageId: MessageId,
-): OrchestrationMessage | undefined {
- for (let index = 0; index < messages.length; index += 1) {
- const message = messages[index];
- if (message?.id === messageId) {
- return message;
- }
- }
- return undefined;
-}
-
-function findProposedPlanById(
- proposedPlans: ReadonlyArray<
- Pick<OrchestrationProposedPlan, "id" | "createdAt" | "implementedAt" | "implementationThreadId">
- >,
- planId: string,
-):
- | Pick<OrchestrationProposedPlan, "id" | "createdAt" | "implementedAt" | "implementationThreadId">
- | undefined {
- for (let index = 0; index < proposedPlans.length; index += 1) {
- const proposedPlan = proposedPlans[index];
- if (proposedPlan?.id === planId) {
- return proposedPlan;
- }
- }
- return undefined;
-}
-
function hasCheckpointForTurn(
checkpoints: ReadonlyArray<OrchestrationCheckpointSummary>,
turnId: TurnId,
@@ -969,6 +926,9 @@ const make = Effect.gen(function* () {
const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
const agentController = yield* AgentController;
const projectionTurnRepository = yield* ProjectionTurnRepository;
+ const projectionThreadMessages = yield* ProjectionThreadMessageRepository;
+ const projectionThreadActivities = yield* ProjectionThreadActivityRepository;
+ const projectionThreadProposedPlans = yield* ProjectionThreadProposedPlanRepository;
const botUsageLedger = yield* BotUsageLedger;
const serverSettingsService = yield* ServerSettingsService;
const channelSecretStore = yield* Effect.serviceOption(ServerSecretStore.ServerSecretStore);
@@ -986,7 +946,7 @@ const make = Effect.gen(function* () {
readThread: (threadId: ThreadId) =>
Effect.runPromise(
projectionSnapshotQuery
- .getThreadDetailById(threadId)
+ .getThreadDetailById(threadId, { activityKinds: [] })
.pipe(Effect.map(Option.getOrNull)),
),
nowIso: () => Effect.runPromise(DateTime.now.pipe(Effect.map(DateTime.formatIso))),
@@ -1087,19 +1047,35 @@ const make = Effect.gen(function* () {
const resolveThreadDetail = Effect.fn("resolveThreadDetail")(function* (threadId: ThreadId) {
return yield* projectionSnapshotQuery
- .getThreadDetailById(threadId)
+ .getThreadDetailById(threadId, { activityKinds: [] })
.pipe(Effect.map(Option.getOrUndefined));
});
- const resolveThreadShell = Effect.fn("resolveThreadShell")(function* (threadId: ThreadId) {
+ const resolveThreadRuntimeContext = Effect.fn("resolveThreadRuntimeContext")(function* (
+ threadId: ThreadId,
+ ) {
return yield* projectionSnapshotQuery
- .getThreadShellById(threadId)
+ .getThreadRuntimeContext(threadId)
.pipe(Effect.map(Option.getOrUndefined));
});
+ const getThreadMessageById = Effect.fn("getThreadMessageById")(function* (
+ threadId: ThreadId,
+ messageId: MessageId,
+ ) {
+ const message = yield* projectionThreadMessages.getByMessageId({ messageId });
+ return Option.filter(message, (entry) => entry.threadId === threadId).pipe(
+ Option.getOrUndefined,
+ );
+ });
+
const syncApprovalInbox = Effect.fn("syncApprovalInbox")(function* (
event: Extract<ProviderRuntimeEvent, { type: "request.opened" | "request.resolved" }>,
- thread: OrchestrationThreadShell,
+ thread: {
+ readonly title: string;
+ readonly botId: OrchestrationThreadShell["botId"];
+ readonly respondingBotId: OrchestrationThreadShell["respondingBotId"];
+ },
) {
const botId = thread.respondingBotId ?? thread.botId;
if (!botId) return;
@@ -1456,12 +1432,6 @@ const make = Effect.gen(function* () {
const upsertProposedPlan = (input: {
event: ProviderRuntimeEvent;
threadId: ThreadId;
- threadProposedPlans: ReadonlyArray<{
- id: string;
- createdAt: string;
- implementedAt: string | null;
- implementationThreadId: ThreadId | null;
- }>;
planId: string;
turnId?: TurnId;
planMarkdown: string | undefined;
@@ -1474,7 +1444,10 @@ const make = Effect.gen(function* () {
return;
}
- const existingPlan = findProposedPlanById(input.threadProposedPlans, input.planId);
+ const existingPlan = yield* projectionThreadProposedPlans.getByPlanId({
+ threadId: input.threadId,
+ planId: OrchestrationProposedPlanId.make(input.planId),
+ });
yield* orchestrationEngine.dispatch({
type: "thread.proposed-plan.upsert",
commandId: yield* providerCommandId(input.event, "proposed-plan-upsert"),
@@ -1483,9 +1456,10 @@ const make = Effect.gen(function* () {
id: input.planId,
turnId: input.turnId ?? null,
planMarkdown,
- implementedAt: existingPlan?.implementedAt ?? null,
- implementationThreadId: existingPlan?.implementationThreadId ?? null,
- createdAt: existingPlan?.createdAt ?? input.createdAt,
+ implementedAt: Option.getOrUndefined(existingPlan)?.implementedAt ?? null,
+ implementationThreadId:
+ Option.getOrUndefined(existingPlan)?.implementationThreadId ?? null,
+ createdAt: Option.getOrUndefined(existingPlan)?.createdAt ?? input.createdAt,
updatedAt: input.updatedAt,
},
createdAt: input.updatedAt,
@@ -1495,12 +1469,6 @@ const make = Effect.gen(function* () {
const finalizeBufferedProposedPlan = (input: {
event: ProviderRuntimeEvent;
threadId: ThreadId;
- threadProposedPlans: ReadonlyArray<{
- id: string;
- createdAt: string;
- implementedAt: string | null;
- implementationThreadId: ThreadId | null;
- }>;
planId: string;
turnId?: TurnId;
fallbackMarkdown?: string;
@@ -1518,7 +1486,6 @@ const make = Effect.gen(function* () {
yield* upsertProposedPlan({
event: input.event,
threadId: input.threadId,
- threadProposedPlans: input.threadProposedPlans,
planId: input.planId,
...(input.turnId ? { turnId: input.turnId } : {}),
planMarkdown,
@@ -1663,22 +1630,12 @@ const make = Effect.gen(function* () {
if (event.type === "content.delta" && event.payload.streamKind !== "assistant_text") {
return;
}
- const thread = yield* resolveThreadShell(event.threadId);
+ const thread = yield* resolveThreadRuntimeContext(event.threadId);
if (!thread) return;
if (event.type === "request.opened" || event.type === "request.resolved") {
yield* syncApprovalInbox(event, thread);
}
- let loadedThreadDetail: OrchestrationThread | null | undefined;
- const getLoadedThreadDetail = () =>
- Effect.gen(function* () {
- if (loadedThreadDetail !== undefined) {
- return loadedThreadDetail;
- }
- loadedThreadDetail = (yield* resolveThreadDetail(thread.id)) ?? null;
- return loadedThreadDetail;
- });
-
const now = event.createdAt;
const eventTurnId = toTurnId(event.turnId);
const respondingBotId = resolveControllerBotId(thread);
@@ -1986,7 +1943,6 @@ const make = Effect.gen(function* () {
? toTurnId(event.turnId)
: undefined;
if (pauseForUserTurnId) {
- const detailedThread = yield* getLoadedThreadDetail();
const assistantDeliveryMode: AssistantDeliveryMode = yield* Effect.map(
serverSettingsService.getSettings,
(settings) => (settings.enableLegacyTokenStreaming ? "streaming" : "buffered"),
@@ -2017,11 +1973,11 @@ const make = Effect.gen(function* () {
event.type === "request.opened"
? "assistant-delta-finalize-on-request-opened"
: "assistant-delta-finalize-on-user-input-requested",
- hasProjectedMessage:
- detailedThread !== null &&
- hasAssistantMessageForTurn(detailedThread.messages, pauseForUserTurnId, {
- streamingOnly: true,
- }),
+ hasProjectedMessage: yield* projectionThreadMessages.hasAssistantMessageForTurn({
+ threadId: thread.id,
+ turnId: pauseForUserTurnId,
+ streamingOnly: true,
+ }),
flushedMessageIds,
});
}
@@ -2050,19 +2006,23 @@ const make = Effect.gen(function* () {
: undefined;
if (assistantCompletion) {
- const detailedThread = yield* getLoadedThreadDetail();
- const messages = detailedThread?.messages ?? [];
const turnId = toTurnId(event.turnId);
const activeAssistantMessageId = turnId
? yield* getActiveAssistantMessageIdForTurn(thread.id, turnId)
: Option.none<MessageId>();
const hasAssistantMessagesForTurn =
- turnId !== undefined ? hasAssistantMessageForTurn(messages, turnId) : false;
+ turnId !== undefined
+ ? yield* projectionThreadMessages.hasAssistantMessageForTurn({
+ threadId: thread.id,
+ turnId,
+ streamingOnly: false,
+ })
+ : false;
const assistantMessageId = Option.getOrElse(
activeAssistantMessageId,
() => assistantCompletion.messageId,
);
- const existingAssistantMessage = findMessageById(messages, assistantMessageId);
+ const existingAssistantMessage = yield* getThreadMessageById(thread.id, assistantMessageId);
const shouldApplyFallbackCompletionText =
!existingAssistantMessage || existingAssistantMessage.text.length === 0;
@@ -2102,11 +2062,9 @@ const make = Effect.gen(function* () {
}
if (proposedPlanCompletion) {
- const detailedThread = yield* getLoadedThreadDetail();
yield* finalizeBufferedProposedPlan({
event,
threadId: thread.id,
- threadProposedPlans: detailedThread?.proposedPlans ?? [],
planId: proposedPlanCompletion.planId,
...(proposedPlanCompletion.turnId ? { turnId: proposedPlanCompletion.turnId } : {}),
fallbackMarkdown: proposedPlanCompletion.planMarkdown,
@@ -2115,24 +2073,24 @@ const make = Effect.gen(function* () {
}
if (event.type === "turn.completed") {
- const detailedThread = yield* getLoadedThreadDetail();
- const messages = detailedThread?.messages ?? [];
- const proposedPlans = detailedThread?.proposedPlans ?? [];
const turnId = toTurnId(event.turnId);
if (turnId) {
const assistantMessageIds = yield* getAssistantMessageIdsForTurn(thread.id, turnId);
const finalizedReplyVisibility = yield* Effect.forEach(
assistantMessageIds,
(assistantMessageId) =>
- finalizeAssistantMessage({
- event,
- threadId: thread.id,
- messageId: assistantMessageId,
- turnId,
- createdAt: now,
- commandTag: "assistant-complete-finalize",
- finalDeltaCommandTag: "assistant-delta-finalize-fallback",
- hasProjectedMessage: findMessageById(messages, assistantMessageId) !== undefined,
+ Effect.gen(function* () {
+ const existing = yield* getThreadMessageById(thread.id, assistantMessageId);
+ return yield* finalizeAssistantMessage({
+ event,
+ threadId: thread.id,
+ messageId: assistantMessageId,
+ turnId,
+ createdAt: now,
+ commandTag: "assistant-complete-finalize",
+ finalDeltaCommandTag: "assistant-delta-finalize-fallback",
+ hasProjectedMessage: existing !== undefined,
+ });
}),
{ concurrency: 1 },
);
@@ -2142,15 +2100,17 @@ const make = Effect.gen(function* () {
yield* finalizeBufferedProposedPlan({
event,
threadId: thread.id,
- threadProposedPlans: proposedPlans,
planId: proposedPlanIdForTurn(thread.id, turnId),
turnId,
updatedAt: now,
});
- const hasVisibleAssistantReply = messages.some(
- (message) => message.role === "assistant" && sameId(message.turnId, turnId),
- );
+ const hasVisibleAssistantReply =
+ yield* projectionThreadMessages.hasAssistantMessageForTurn({
+ threadId: thread.id,
+ turnId,
+ streamingOnly: false,
+ });
if (!hasVisibleAssistantReply && !finalizedReplyVisibility.some(Boolean)) {
const fallbackText =
event.payload.state === "failed"
@@ -2304,8 +2264,14 @@ const make = Effect.gen(function* () {
if (event.type === "task.completed") {
taskTitle = yield* lookupTaskDescription(thread.id, event.payload.taskId);
if (!taskTitle) {
- const threadDetail = yield* getLoadedThreadDetail();
- taskTitle = findTaskTitleInActivities(threadDetail?.activities, event.payload.taskId);
+ const latestTask = yield* projectionThreadActivities.getLatestTaskActivity({
+ threadId: thread.id,
+ taskId: event.payload.taskId,
+ });
+ taskTitle = findTaskTitleInActivities(
+ Option.match(latestTask, { onNone: () => [], onSome: (activity) => [activity] }),
+ event.payload.taskId,
+ );
}
}
@@ -2441,4 +2407,9 @@ const make = Effect.gen(function* () {
export const ProviderRuntimeIngestionLive = Layer.effect(
ProviderRuntimeIngestionService,
make,
-).pipe(Layer.provide(ProjectionTurnRepositoryLive));
+).pipe(
+ Layer.provide(ProjectionTurnRepositoryLive),
+ Layer.provide(ProjectionThreadMessageRepositoryLive),
+ Layer.provide(ProjectionThreadActivityRepositoryLive),
+ Layer.provide(ProjectionThreadProposedPlanRepositoryLive),
+);
diff --git a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts
index 08cf11ea8..30ba972f8 100644
--- a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts
+++ b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts
@@ -7,19 +7,25 @@
* @module ProjectionSnapshotQuery
*/
import type {
+ BotId,
CheckpointRef,
+ GroupId,
+ MessageId,
OrchestrationCheckpointSummary,
+ OrchestrationMessage,
OrchestrationProject,
OrchestrationProjectShell,
OrchestrationReadModel,
OrchestrationSearchThreadsInput,
OrchestrationSearchThreadsResult,
+ OrchestrationSession,
OrchestrationShellSnapshot,
OrchestrationThread,
OrchestrationThreadDetailSnapshot,
OrchestrationThreadDetailWindow,
OrchestrationThreadShell,
ProjectId,
+ RuntimeMode,
ThreadId,
} from "@t3tools/contracts";
import * as Context from "effect/Context";
@@ -54,6 +60,31 @@ export interface ProjectionFullThreadDiffContext {
readonly toCheckpointRef: CheckpointRef | null;
}
+export interface ProjectionThreadRuntimeContext {
+ readonly id: ThreadId;
+ readonly title: string;
+ readonly session: OrchestrationSession | null;
+ readonly projectId: ProjectId;
+ readonly botId: BotId | null;
+ readonly groupId: GroupId | null;
+ readonly respondingBotId: BotId | null;
+ readonly runtimeMode: RuntimeMode;
+}
+
+export interface ProjectionThreadDetailQuery {
+ /**
+ * Limit activities before SQLite returns and decodes their payloads.
+ * Any explicit filter omits pinned-request reads. An empty list also skips
+ * the activity query. Omit this option to preserve the full detail response.
+ */
+ readonly activityKinds?: ReadonlyArray<string>;
+}
+
+export interface ProjectionTurnStartMessage {
+ readonly message: OrchestrationMessage;
+ readonly hasOtherUserMessages: boolean;
+}
+
/**
* ProjectionSnapshotQueryShape - Service API for read-model snapshots.
*/
@@ -170,11 +201,28 @@ export interface ProjectionSnapshotQueryShape {
threadId: ThreadId,
) => Effect.Effect<Option.Option<OrchestrationThreadShell>, ProjectionRepositoryError>;
+ /**
+ * Read thread id, title, and session in one query for buffered provider events.
+ */
+ readonly getThreadRuntimeContext: (
+ threadId: ThreadId,
+ ) => Effect.Effect<Option.Option<ProjectionThreadRuntimeContext>, ProjectionRepositoryError>;
+
+ /**
+ * Read the user prompt that starts a turn and whether the thread has any
+ * other non-compaction user message.
+ */
+ readonly getTurnStartMessage: (input: {
+ readonly threadId: ThreadId;
+ readonly messageId: MessageId;
+ }) => Effect.Effect<Option.Option<ProjectionTurnStartMessage>, ProjectionRepositoryError>;
+
/**
* Read a single active thread detail snapshot by id.
*/
readonly getThreadDetailById: (
threadId: ThreadId,
+ query?: ProjectionThreadDetailQuery,
) => Effect.Effect<Option.Option<OrchestrationThread>, ProjectionRepositoryError>;
/**
diff --git a/apps/server/src/persistence/Layers/ProjectionPendingApprovals.ts b/apps/server/src/persistence/Layers/ProjectionPendingApprovals.ts
index 253f6e13b..617410dd1 100644
--- a/apps/server/src/persistence/Layers/ProjectionPendingApprovals.ts
+++ b/apps/server/src/persistence/Layers/ProjectionPendingApprovals.ts
@@ -2,6 +2,7 @@ import * as SqlClient from "effect/unstable/sql/SqlClient";
import * as SqlSchema from "effect/unstable/sql/SqlSchema";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
+import * as Schema from "effect/Schema";
import { toPersistenceSqlError } from "../Errors.ts";
import {
@@ -68,6 +69,16 @@ const makeProjectionPendingApprovalRepository = Effect.gen(function* () {
`,
});
+ const countPendingApprovalRows = SqlSchema.findOne({
+ Request: ListProjectionPendingApprovalsInput,
+ Result: Schema.Struct({ count: Schema.Number }),
+ execute: ({ threadId }) => sql`
+ SELECT COUNT(*) AS count
+ FROM projection_pending_approvals
+ WHERE thread_id = ${threadId} AND status = 'pending'
+ `,
+ });
+
const getProjectionPendingApprovalRow = SqlSchema.findOneOption({
Request: GetProjectionPendingApprovalInput,
Result: ProjectionPendingApproval,
@@ -107,6 +118,15 @@ const makeProjectionPendingApprovalRepository = Effect.gen(function* () {
),
);
+ const countPendingByThreadId: ProjectionPendingApprovalRepositoryShape["countPendingByThreadId"] =
+ (input) =>
+ countPendingApprovalRows(input).pipe(
+ Effect.mapError(
+ toPersistenceSqlError("ProjectionPendingApprovalRepository.countPendingByThreadId:query"),
+ ),
+ Effect.map((row) => row.count),
+ );
+
const getByRequestId: ProjectionPendingApprovalRepositoryShape["getByRequestId"] = (input) =>
getProjectionPendingApprovalRow(input).pipe(
Effect.mapError(
@@ -126,6 +146,7 @@ const makeProjectionPendingApprovalRepository = Effect.gen(function* () {
return {
upsert,
listByThreadId,
+ countPendingByThreadId,
getByRequestId,
deleteByRequestId,
} satisfies ProjectionPendingApprovalRepositoryShape;
diff --git a/apps/server/src/persistence/Layers/ProjectionState.ts b/apps/server/src/persistence/Layers/ProjectionState.ts
index f630fe51e..a6fb7f3be 100644
--- a/apps/server/src/persistence/Layers/ProjectionState.ts
+++ b/apps/server/src/persistence/Layers/ProjectionState.ts
@@ -42,6 +42,26 @@ const makeProjectionStateRepository = Effect.gen(function* () {
`,
});
+ const upsertProjectionStateRows = SqlSchema.void({
+ Request: Schema.Array(ProjectionState),
+ execute: (rows) =>
+ rows.length === 0
+ ? Effect.void
+ : sql`
+ INSERT INTO projection_state ${sql.insert(
+ rows.map((row) => ({
+ projector: row.projector,
+ last_applied_sequence: row.lastAppliedSequence,
+ updated_at: row.updatedAt,
+ })),
+ )}
+ ON CONFLICT (projector)
+ DO UPDATE SET
+ last_applied_sequence = excluded.last_applied_sequence,
+ updated_at = excluded.updated_at
+ `,
+ });
+
const getProjectionStateRow = SqlSchema.findOneOption({
Request: GetProjectionStateInput,
Result: ProjectionState,
@@ -86,6 +106,11 @@ const makeProjectionStateRepository = Effect.gen(function* () {
Effect.mapError(toPersistenceSqlError("ProjectionStateRepository.upsert:query")),
);
+ const upsertMany: ProjectionStateRepositoryShape["upsertMany"] = (rows) =>
+ upsertProjectionStateRows(rows).pipe(
+ Effect.mapError(toPersistenceSqlError("ProjectionStateRepository.upsertMany:query")),
+ );
+
const getByProjector: ProjectionStateRepositoryShape["getByProjector"] = (input) =>
getProjectionStateRow(input).pipe(
Effect.mapError(toPersistenceSqlError("ProjectionStateRepository.getByProjector:query")),
@@ -106,6 +131,7 @@ const makeProjectionStateRepository = Effect.gen(function* () {
return {
upsert,
+ upsertMany,
getByProjector,
listAll,
minLastAppliedSequence,
diff --git a/apps/server/src/persistence/Layers/ProjectionThreadActivities.ts b/apps/server/src/persistence/Layers/ProjectionThreadActivities.ts
index 2f4815f96..fc93e5f3c 100644
--- a/apps/server/src/persistence/Layers/ProjectionThreadActivities.ts
+++ b/apps/server/src/persistence/Layers/ProjectionThreadActivities.ts
@@ -3,6 +3,7 @@ import * as SqlSchema from "effect/unstable/sql/SqlSchema";
import { NonNegativeInt } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
+import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import * as Struct from "effect/Struct";
@@ -10,6 +11,7 @@ import { toPersistenceDecodeError, toPersistenceSqlError } from "../Errors.ts";
import {
DeleteProjectionThreadActivitiesInput,
+ GetLatestProjectionThreadTaskActivityInput,
ListProjectionThreadActivitiesInput,
ProjectionThreadActivity,
ProjectionThreadActivityRepository,
@@ -23,6 +25,22 @@ const ProjectionThreadActivityDbRowSchema = ProjectionThreadActivity.mapFields(
}),
);
+function toProjectionThreadActivity(
+ row: Schema.Schema.Type<typeof ProjectionThreadActivityDbRowSchema>,
+): ProjectionThreadActivity {
+ return {
+ activityId: row.activityId,
+ threadId: row.threadId,
+ turnId: row.turnId,
+ tone: row.tone,
+ kind: row.kind,
+ summary: row.summary,
+ payload: row.payload,
+ ...(row.sequence !== null ? { sequence: row.sequence } : {}),
+ createdAt: row.createdAt,
+ };
+}
+
function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
return (cause: unknown) =>
Schema.isSchemaError(cause)
@@ -30,6 +48,10 @@ function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: st
: toPersistenceSqlError(sqlOperation)(cause);
}
+// Match String.trim so blank saved titles cannot hide an earlier task name.
+const taskTitleWhitespace =
+ "\u0009\u000a\u000b\u000c\u000d\u0020\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff";
+
const makeProjectionThreadActivityRepository = Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
@@ -73,6 +95,37 @@ const makeProjectionThreadActivityRepository = Effect.gen(function* () {
});
const listProjectionThreadActivityRows = SqlSchema.findAll({
+ Request: ListProjectionThreadActivitiesInput,
+ Result: ProjectionThreadActivityDbRowSchema,
+ execute: ({ threadId, activityKinds, limit }) =>
+ sql`
+ SELECT
+ activity_id AS "activityId",
+ thread_id AS "threadId",
+ turn_id AS "turnId",
+ tone,
+ kind,
+ summary,
+ payload_json AS "payload",
+ sequence,
+ created_at AS "createdAt"
+ FROM (
+ SELECT *
+ FROM projection_thread_activities
+ WHERE thread_id = ${threadId}
+ ${activityKinds === undefined ? sql`` : sql`AND ${sql.in("kind", activityKinds)}`}
+ ORDER BY sequence DESC, created_at DESC, activity_id DESC
+ ${limit === undefined ? sql`` : sql`LIMIT ${limit}`}
+ ) AS recent_activities
+ ORDER BY
+ CASE WHEN sequence IS NULL THEN 0 ELSE 1 END ASC,
+ sequence ASC,
+ created_at ASC,
+ activity_id ASC
+ `,
+ });
+
+ const listUserInputLifecycleActivityRows = SqlSchema.findAll({
Request: ListProjectionThreadActivitiesInput,
Result: ProjectionThreadActivityDbRowSchema,
execute: ({ threadId }) =>
@@ -89,6 +142,11 @@ const makeProjectionThreadActivityRepository = Effect.gen(function* () {
created_at AS "createdAt"
FROM projection_thread_activities
WHERE thread_id = ${threadId}
+ AND kind IN (
+ 'user-input.requested',
+ 'user-input.resolved',
+ 'provider.user-input.respond.failed'
+ )
ORDER BY
CASE WHEN sequence IS NULL THEN 0 ELSE 1 END ASC,
sequence ASC,
@@ -97,6 +155,40 @@ const makeProjectionThreadActivityRepository = Effect.gen(function* () {
`,
});
+ const getLatestProjectionThreadTaskActivityRow = SqlSchema.findOneOption({
+ Request: GetLatestProjectionThreadTaskActivityInput,
+ Result: ProjectionThreadActivityDbRowSchema,
+ execute: ({ threadId, taskId }) =>
+ sql`
+ SELECT
+ activity_id AS "activityId",
+ thread_id AS "threadId",
+ turn_id AS "turnId",
+ tone,
+ kind,
+ summary,
+ payload_json AS "payload",
+ sequence,
+ created_at AS "createdAt"
+ FROM projection_thread_activities
+ WHERE thread_id = ${threadId}
+ AND kind IN ('task.started', 'task.progress')
+ AND json_extract(payload_json, '$.taskId') = ${taskId}
+ AND length(trim(
+ CASE
+ WHEN json_type(payload_json, '$.title') = 'text'
+ THEN json_extract(payload_json, '$.title')
+ WHEN kind = 'task.started' AND json_type(payload_json, '$.detail') = 'text'
+ THEN json_extract(payload_json, '$.detail')
+ ELSE ''
+ END,
+ ${taskTitleWhitespace}
+ )) > 0
+ ORDER BY sequence DESC, created_at DESC, activity_id DESC
+ LIMIT 1
+ `,
+ });
+
const deleteProjectionThreadActivityRows = SqlSchema.void({
Request: DeleteProjectionThreadActivitiesInput,
execute: ({ threadId }) =>
@@ -124,19 +216,32 @@ const makeProjectionThreadActivityRepository = Effect.gen(function* () {
"ProjectionThreadActivityRepository.listByThreadId:decodeRows",
),
),
- Effect.map((rows) =>
- rows.map((row) => ({
- activityId: row.activityId,
- threadId: row.threadId,
- turnId: row.turnId,
- tone: row.tone,
- kind: row.kind,
- summary: row.summary,
- payload: row.payload,
- ...(row.sequence !== null ? { sequence: row.sequence } : {}),
- createdAt: row.createdAt,
- })),
+ Effect.map((rows) => rows.map(toProjectionThreadActivity)),
+ );
+
+ const listUserInputLifecycleByThreadId: ProjectionThreadActivityRepositoryShape["listUserInputLifecycleByThreadId"] =
+ (input) =>
+ listUserInputLifecycleActivityRows(input).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionThreadActivityRepository.listUserInputLifecycleByThreadId:query",
+ "ProjectionThreadActivityRepository.listUserInputLifecycleByThreadId:decodeRows",
+ ),
+ ),
+ Effect.map((rows) => rows.map(toProjectionThreadActivity)),
+ );
+
+ const getLatestTaskActivity: ProjectionThreadActivityRepositoryShape["getLatestTaskActivity"] = (
+ input,
+ ) =>
+ getLatestProjectionThreadTaskActivityRow(input).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionThreadActivityRepository.getLatestTaskActivity:query",
+ "ProjectionThreadActivityRepository.getLatestTaskActivity:decodeRow",
+ ),
),
+ Effect.map(Option.map(toProjectionThreadActivity)),
);
const deleteByThreadId: ProjectionThreadActivityRepositoryShape["deleteByThreadId"] = (input) =>
@@ -149,6 +254,8 @@ const makeProjectionThreadActivityRepository = Effect.gen(function* () {
return {
upsert,
listByThreadId,
+ listUserInputLifecycleByThreadId,
+ getLatestTaskActivity,
deleteByThreadId,
} satisfies ProjectionThreadActivityRepositoryShape;
});
diff --git a/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts b/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts
index 0854ab96a..c4a3f251e 100644
--- a/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts
+++ b/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts
@@ -1,4 +1,4 @@
-import { BotId, MessageId, ThreadId } from "@t3tools/contracts";
+import { BotId, MessageId, ThreadId, TurnId } from "@t3tools/contracts";
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
@@ -283,4 +283,84 @@ layer("ProjectionThreadMessageRepository", (it) => {
assert.equal(rows[0]?.channelOrigin, null);
}),
);
+
+ it.effect("finds the latest user-message time within one thread", () =>
+ Effect.gen(function* () {
+ const repository = yield* ProjectionThreadMessageRepository;
+ const threadId = ThreadId.make("thread-latest-user-message");
+ assert.isNull(yield* repository.getLatestUserMessageAt({ threadId }));
+
+ const messages = [
+ { role: "user", createdAt: "2026-02-28T19:05:02.000Z" },
+ { role: "user", createdAt: "2026-02-28T19:05:01.000Z" },
+ { role: "assistant", createdAt: "2026-02-28T19:05:03.000Z" },
+ { role: "system", createdAt: "2026-02-28T19:05:04.000Z" },
+ ] as const;
+ for (const [index, message] of messages.entries()) {
+ yield* repository.upsert({
+ messageId: MessageId.make(`latest-user-message-${index}`),
+ threadId,
+ turnId: null,
+ ...message,
+ text: "Message body",
+ isStreaming: false,
+ updatedAt: "2026-02-28T19:06:00.000Z",
+ });
+ }
+ yield* repository.upsert({
+ messageId: MessageId.make("latest-user-message-other-thread"),
+ threadId: ThreadId.make("thread-latest-user-message-other"),
+ turnId: null,
+ role: "user",
+ text: "Other thread",
+ isStreaming: false,
+ createdAt: "2026-02-28T19:05:05.000Z",
+ updatedAt: "2026-02-28T19:05:05.000Z",
+ });
+
+ assert.strictEqual(
+ yield* repository.getLatestUserMessageAt({ threadId }),
+ "2026-02-28T19:05:02.000Z",
+ );
+ yield* repository.deleteByThreadId({ threadId });
+ assert.isNull(yield* repository.getLatestUserMessageAt({ threadId }));
+ }),
+ );
+
+ it.effect("checks assistant turn state without hydrating message text", () =>
+ Effect.gen(function* () {
+ const repository = yield* ProjectionThreadMessageRepository;
+ const threadId = ThreadId.make("thread-assistant-turn-state");
+ const turnId = TurnId.make("turn-assistant-state");
+ const createdAt = "2026-03-01T00:00:00.000Z";
+
+ yield* repository.upsert({
+ messageId: MessageId.make("message-assistant-turn-state"),
+ threadId,
+ turnId,
+ role: "assistant",
+ text: "large text that the existence query must not select",
+ isStreaming: false,
+ createdAt,
+ updatedAt: createdAt,
+ });
+
+ assert.equal(
+ yield* repository.hasAssistantMessageForTurn({
+ threadId,
+ turnId,
+ streamingOnly: false,
+ }),
+ true,
+ );
+ assert.equal(
+ yield* repository.hasAssistantMessageForTurn({
+ threadId,
+ turnId,
+ streamingOnly: true,
+ }),
+ false,
+ );
+ }),
+ );
});
diff --git a/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts b/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts
index 9dcde65d0..a6e8b7d31 100644
--- a/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts
+++ b/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts
@@ -15,6 +15,7 @@ import { toPersistenceSqlError } from "../Errors.ts";
import {
AppendStreamingProjectionThreadMessage,
GetProjectionThreadMessageInput,
+ HasProjectionThreadAssistantMessageInput,
ProjectionThreadMessageRepository,
type ProjectionThreadMessageRepositoryShape,
DeleteProjectionThreadMessagesInput,
@@ -30,6 +31,7 @@ const ProjectionThreadMessageDbRowSchema = ProjectionThreadMessage.mapFields(
reactions: Schema.fromJsonString(Schema.Array(OrchestrationMessageReaction)),
}),
);
+const ProjectionThreadMessageExistsDbRowSchema = Schema.Struct({ exists: Schema.Number });
function toProjectionThreadMessage(
row: Schema.Schema.Type<typeof ProjectionThreadMessageDbRowSchema>,
@@ -228,6 +230,23 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
`,
});
+ const hasProjectionThreadAssistantMessageRow = SqlSchema.findOne({
+ Request: HasProjectionThreadAssistantMessageInput,
+ Result: ProjectionThreadMessageExistsDbRowSchema,
+ execute: ({ threadId, turnId, streamingOnly }) =>
+ sql`
+ SELECT EXISTS (
+ SELECT 1
+ FROM projection_thread_messages
+ WHERE thread_id = ${threadId}
+ AND turn_id = ${turnId}
+ AND role = 'assistant'
+ AND (${streamingOnly ? 1 : 0} = 0 OR is_streaming = 1)
+ LIMIT 1
+ ) AS "exists"
+ `,
+ });
+
const listProjectionThreadMessageRows = SqlSchema.findAll({
Request: ListProjectionThreadMessagesInput,
Result: ProjectionThreadMessageDbRowSchema,
@@ -254,6 +273,18 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
`,
});
+ const getLatestUserMessageAtRow = SqlSchema.findOne({
+ Request: ListProjectionThreadMessagesInput,
+ Result: Schema.Struct({
+ latestUserMessageAt: Schema.NullOr(ProjectionThreadMessage.fields.createdAt),
+ }),
+ execute: ({ threadId }) => sql`
+ SELECT MAX(created_at) AS "latestUserMessageAt"
+ FROM projection_thread_messages
+ WHERE thread_id = ${threadId} AND role = 'user'
+ `,
+ });
+
const deleteProjectionThreadMessageRows = SqlSchema.void({
Request: DeleteProjectionThreadMessagesInput,
execute: ({ threadId }) =>
@@ -283,6 +314,17 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
Effect.map(Option.map(toProjectionThreadMessage)),
);
+ const hasAssistantMessageForTurn: ProjectionThreadMessageRepositoryShape["hasAssistantMessageForTurn"] =
+ (input) =>
+ hasProjectionThreadAssistantMessageRow(input).pipe(
+ Effect.mapError(
+ toPersistenceSqlError(
+ "ProjectionThreadMessageRepository.hasAssistantMessageForTurn:query",
+ ),
+ ),
+ Effect.map((row) => row.exists === 1),
+ );
+
const listByThreadId: ProjectionThreadMessageRepositoryShape["listByThreadId"] = (input) =>
listProjectionThreadMessageRows(input).pipe(
Effect.mapError(
@@ -291,6 +333,16 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
Effect.map((rows) => rows.map(toProjectionThreadMessage)),
);
+ const getLatestUserMessageAt: ProjectionThreadMessageRepositoryShape["getLatestUserMessageAt"] = (
+ input,
+ ) =>
+ getLatestUserMessageAtRow(input).pipe(
+ Effect.mapError(
+ toPersistenceSqlError("ProjectionThreadMessageRepository.getLatestUserMessageAt:query"),
+ ),
+ Effect.map((row) => row.latestUserMessageAt),
+ );
+
const deleteByThreadId: ProjectionThreadMessageRepositoryShape["deleteByThreadId"] = (input) =>
deleteProjectionThreadMessageRows(input).pipe(
Effect.mapError(
@@ -302,7 +354,9 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
upsert,
appendStreaming,
getByMessageId,
+ hasAssistantMessageForTurn,
listByThreadId,
+ getLatestUserMessageAt,
deleteByThreadId,
} satisfies ProjectionThreadMessageRepositoryShape;
});
diff --git a/apps/server/src/persistence/Layers/ProjectionThreadProposedPlans.ts b/apps/server/src/persistence/Layers/ProjectionThreadProposedPlans.ts
index 63aed1a16..609d389e4 100644
--- a/apps/server/src/persistence/Layers/ProjectionThreadProposedPlans.ts
+++ b/apps/server/src/persistence/Layers/ProjectionThreadProposedPlans.ts
@@ -1,11 +1,14 @@
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
+import * as Schema from "effect/Schema";
import * as SqlClient from "effect/unstable/sql/SqlClient";
import * as SqlSchema from "effect/unstable/sql/SqlSchema";
import { toPersistenceSqlError } from "../Errors.ts";
import {
DeleteProjectionThreadProposedPlansInput,
+ GetProjectionThreadProposedPlanInput,
+ HasActionableProjectionThreadProposedPlanInput,
ListProjectionThreadProposedPlansInput,
ProjectionThreadProposedPlan,
ProjectionThreadProposedPlanRepository,
@@ -50,6 +53,49 @@ const makeProjectionThreadProposedPlanRepository = Effect.gen(function* () {
`,
});
+ const getProjectionThreadProposedPlanRow = SqlSchema.findOneOption({
+ Request: GetProjectionThreadProposedPlanInput,
+ Result: ProjectionThreadProposedPlan,
+ execute: ({ threadId, planId }) => sql`
+ SELECT
+ plan_id AS "planId",
+ thread_id AS "threadId",
+ turn_id AS "turnId",
+ plan_markdown AS "planMarkdown",
+ implemented_at AS "implementedAt",
+ implementation_thread_id AS "implementationThreadId",
+ created_at AS "createdAt",
+ updated_at AS "updatedAt"
+ FROM projection_thread_proposed_plans
+ WHERE thread_id = ${threadId} AND plan_id = ${planId}
+ `,
+ });
+
+ const listPlanStatusCandidates = SqlSchema.findAll({
+ Request: HasActionableProjectionThreadProposedPlanInput,
+ Result: Schema.Struct({
+ planId: ProjectionThreadProposedPlan.fields.planId,
+ implementedAt: ProjectionThreadProposedPlan.fields.implementedAt,
+ updatedAt: ProjectionThreadProposedPlan.fields.updatedAt,
+ }),
+ execute: ({ threadId, latestTurnId }) => sql`
+ SELECT
+ plan_id AS "planId",
+ implemented_at AS "implementedAt",
+ updated_at AS "updatedAt"
+ FROM projection_thread_proposed_plans
+ WHERE thread_id = ${threadId}
+ AND (
+ turn_id = ${latestTurnId}
+ OR NOT EXISTS (
+ SELECT 1 FROM projection_thread_proposed_plans
+ WHERE thread_id = ${threadId} AND turn_id = ${latestTurnId}
+ )
+ )
+ ORDER BY created_at ASC, plan_id ASC
+ `,
+ });
+
const listProjectionThreadProposedPlanRows = SqlSchema.findAll({
Request: ListProjectionThreadProposedPlansInput,
Result: ProjectionThreadProposedPlan,
@@ -82,6 +128,37 @@ const makeProjectionThreadProposedPlanRepository = Effect.gen(function* () {
Effect.mapError(toPersistenceSqlError("ProjectionThreadProposedPlanRepository.upsert:query")),
);
+ const getByPlanId: ProjectionThreadProposedPlanRepositoryShape["getByPlanId"] = (input) =>
+ getProjectionThreadProposedPlanRow(input).pipe(
+ Effect.mapError(
+ toPersistenceSqlError("ProjectionThreadProposedPlanRepository.getByPlanId:query"),
+ ),
+ );
+
+ const hasActionableByThreadId = Effect.fn(
+ "ProjectionThreadProposedPlanRepository.hasActionableByThreadId",
+ )(
+ function* (input: HasActionableProjectionThreadProposedPlanInput) {
+ const candidates = yield* listPlanStatusCandidates(input);
+ let selected: (typeof candidates)[number] | undefined;
+ // Timestamps and IDs use localeCompare, not SQLite byte order. Replace
+ // equal candidates to preserve the stable order of listByThreadId.
+ for (const candidate of candidates) {
+ if (
+ selected === undefined ||
+ (candidate.updatedAt.localeCompare(selected.updatedAt) ||
+ candidate.planId.localeCompare(selected.planId)) >= 0
+ ) {
+ selected = candidate;
+ }
+ }
+ return selected?.implementedAt === null;
+ },
+ Effect.mapError(
+ toPersistenceSqlError("ProjectionThreadProposedPlanRepository.hasActionableByThreadId:query"),
+ ),
+ );
+
const listByThreadId: ProjectionThreadProposedPlanRepositoryShape["listByThreadId"] = (input) =>
listProjectionThreadProposedPlanRows(input).pipe(
Effect.mapError(
@@ -100,7 +177,9 @@ const makeProjectionThreadProposedPlanRepository = Effect.gen(function* () {
return {
upsert,
+ getByPlanId,
listByThreadId,
+ hasActionableByThreadId,
deleteByThreadId,
} satisfies ProjectionThreadProposedPlanRepositoryShape;
});
diff --git a/apps/server/src/persistence/Services/ProjectionPendingApprovals.ts b/apps/server/src/persistence/Services/ProjectionPendingApprovals.ts
index c7a6439e5..197914821 100644
--- a/apps/server/src/persistence/Services/ProjectionPendingApprovals.ts
+++ b/apps/server/src/persistence/Services/ProjectionPendingApprovals.ts
@@ -69,6 +69,11 @@ export interface ProjectionPendingApprovalRepositoryShape {
input: ListProjectionPendingApprovalsInput,
) => Effect.Effect<ReadonlyArray<ProjectionPendingApproval>, ProjectionRepositoryError>;
+ /** Count pending approvals without loading resolved request history. */
+ readonly countPendingByThreadId: (
+ input: ListProjectionPendingApprovalsInput,
+ ) => Effect.Effect<number, ProjectionRepositoryError>;
+
/**
* Read a pending approval row by request id.
*/
diff --git a/apps/server/src/persistence/Services/ProjectionState.ts b/apps/server/src/persistence/Services/ProjectionState.ts
index 959c4ecd6..695432024 100644
--- a/apps/server/src/persistence/Services/ProjectionState.ts
+++ b/apps/server/src/persistence/Services/ProjectionState.ts
@@ -37,6 +37,11 @@ export interface ProjectionStateRepositoryShape {
*/
readonly upsert: (row: ProjectionState) => Effect.Effect<void, ProjectionRepositoryError>;
+ /** Insert or replace projector cursors in one statement. Empty batches do nothing. */
+ readonly upsertMany: (
+ rows: ReadonlyArray<ProjectionState>,
+ ) => Effect.Effect<void, ProjectionRepositoryError>;
+
/**
* Read projection cursor state for a projector key.
*/
diff --git a/apps/server/src/persistence/Services/ProjectionThreadActivities.ts b/apps/server/src/persistence/Services/ProjectionThreadActivities.ts
index 49cbebbdb..802ca1a96 100644
--- a/apps/server/src/persistence/Services/ProjectionThreadActivities.ts
+++ b/apps/server/src/persistence/Services/ProjectionThreadActivities.ts
@@ -17,6 +17,7 @@ import {
import * as Schema from "effect/Schema";
import * as Context from "effect/Context";
import type * as Effect from "effect/Effect";
+import type * as Option from "effect/Option";
import type { ProjectionRepositoryError } from "../Errors.ts";
@@ -35,9 +36,18 @@ export type ProjectionThreadActivity = typeof ProjectionThreadActivity.Type;
export const ListProjectionThreadActivitiesInput = Schema.Struct({
threadId: ThreadId,
+ activityKinds: Schema.optional(Schema.Array(Schema.String)),
+ limit: Schema.optional(NonNegativeInt),
});
export type ListProjectionThreadActivitiesInput = typeof ListProjectionThreadActivitiesInput.Type;
+export const GetLatestProjectionThreadTaskActivityInput = Schema.Struct({
+ threadId: ThreadId,
+ taskId: Schema.String,
+});
+export type GetLatestProjectionThreadTaskActivityInput =
+ typeof GetLatestProjectionThreadTaskActivityInput.Type;
+
export const DeleteProjectionThreadActivitiesInput = Schema.Struct({
threadId: ThreadId,
});
@@ -61,12 +71,28 @@ export interface ProjectionThreadActivityRepositoryShape {
* List projected thread activity rows for a thread.
*
* Returned in ascending runtime sequence order (or creation order when
- * sequence is unavailable).
+ * sequence is unavailable). A limit selects the newest matching rows.
*/
readonly listByThreadId: (
input: ListProjectionThreadActivitiesInput,
) => Effect.Effect<ReadonlyArray<ProjectionThreadActivity>, ProjectionRepositoryError>;
+ /**
+ * List activity rows used to derive pending user-input state.
+ *
+ * Filters in SQLite so unrelated payloads do not enter server memory.
+ */
+ readonly listUserInputLifecycleByThreadId: (
+ input: ListProjectionThreadActivitiesInput,
+ ) => Effect.Effect<ReadonlyArray<ProjectionThreadActivity>, ProjectionRepositoryError>;
+
+ /**
+ * Read the latest task-start or task-progress activity with a usable title.
+ */
+ readonly getLatestTaskActivity: (
+ input: GetLatestProjectionThreadTaskActivityInput,
+ ) => Effect.Effect<Option.Option<ProjectionThreadActivity>, ProjectionRepositoryError>;
+
/**
* Delete projected thread activity rows by thread.
*/
diff --git a/apps/server/src/persistence/Services/ProjectionThreadMessages.ts b/apps/server/src/persistence/Services/ProjectionThreadMessages.ts
index f89271c4f..8e6ecafec 100644
--- a/apps/server/src/persistence/Services/ProjectionThreadMessages.ts
+++ b/apps/server/src/persistence/Services/ProjectionThreadMessages.ts
@@ -60,6 +60,14 @@ export const GetProjectionThreadMessageInput = Schema.Struct({
});
export type GetProjectionThreadMessageInput = typeof GetProjectionThreadMessageInput.Type;
+export const HasProjectionThreadAssistantMessageInput = Schema.Struct({
+ threadId: ThreadId,
+ turnId: TurnId,
+ streamingOnly: Schema.Boolean,
+});
+export type HasProjectionThreadAssistantMessageInput =
+ typeof HasProjectionThreadAssistantMessageInput.Type;
+
export const DeleteProjectionThreadMessagesInput = Schema.Struct({
threadId: ThreadId,
});
@@ -90,6 +98,13 @@ export interface ProjectionThreadMessageRepositoryShape {
input: GetProjectionThreadMessageInput,
) => Effect.Effect<Option.Option<ProjectionThreadMessage>, ProjectionRepositoryError>;
+ /**
+ * Check for an assistant message in a turn without hydrating message text.
+ */
+ readonly hasAssistantMessageForTurn: (
+ input: HasProjectionThreadAssistantMessageInput,
+ ) => Effect.Effect<boolean, ProjectionRepositoryError>;
+
/**
* List projected thread messages for a thread.
*
@@ -99,6 +114,11 @@ export interface ProjectionThreadMessageRepositoryShape {
input: ListProjectionThreadMessagesInput,
) => Effect.Effect<ReadonlyArray<ProjectionThreadMessage>, ProjectionRepositoryError>;
+ /** Read the latest user-message timestamp without loading message bodies. */
+ readonly getLatestUserMessageAt: (
+ input: ListProjectionThreadMessagesInput,
+ ) => Effect.Effect<ProjectionThreadMessage["createdAt"] | null, ProjectionRepositoryError>;
+
/**
* Delete projected thread messages by thread.
*/
diff --git a/apps/server/src/persistence/Services/ProjectionThreadProposedPlans.ts b/apps/server/src/persistence/Services/ProjectionThreadProposedPlans.ts
index ecc5e04dc..1ac6a65f1 100644
--- a/apps/server/src/persistence/Services/ProjectionThreadProposedPlans.ts
+++ b/apps/server/src/persistence/Services/ProjectionThreadProposedPlans.ts
@@ -8,6 +8,7 @@ import {
import * as Schema from "effect/Schema";
import * as Context from "effect/Context";
import type * as Effect from "effect/Effect";
+import type * as Option from "effect/Option";
import type { ProjectionRepositoryError } from "../Errors.ts";
@@ -29,6 +30,19 @@ export const ListProjectionThreadProposedPlansInput = Schema.Struct({
export type ListProjectionThreadProposedPlansInput =
typeof ListProjectionThreadProposedPlansInput.Type;
+export const HasActionableProjectionThreadProposedPlanInput = Schema.Struct({
+ threadId: ThreadId,
+ latestTurnId: Schema.NullOr(TurnId),
+});
+export type HasActionableProjectionThreadProposedPlanInput =
+ typeof HasActionableProjectionThreadProposedPlanInput.Type;
+
+export const GetProjectionThreadProposedPlanInput = Schema.Struct({
+ threadId: ThreadId,
+ planId: OrchestrationProposedPlanId,
+});
+export type GetProjectionThreadProposedPlanInput = typeof GetProjectionThreadProposedPlanInput.Type;
+
export const DeleteProjectionThreadProposedPlansInput = Schema.Struct({
threadId: ThreadId,
});
@@ -39,9 +53,16 @@ export interface ProjectionThreadProposedPlanRepositoryShape {
readonly upsert: (
proposedPlan: ProjectionThreadProposedPlan,
) => Effect.Effect<void, ProjectionRepositoryError>;
+ /** Read one plan without loading the thread's other plans. */
+ readonly getByPlanId: (
+ input: GetProjectionThreadProposedPlanInput,
+ ) => Effect.Effect<Option.Option<ProjectionThreadProposedPlan>, ProjectionRepositoryError>;
readonly listByThreadId: (
input: ListProjectionThreadProposedPlansInput,
) => Effect.Effect<ReadonlyArray<ProjectionThreadProposedPlan>, ProjectionRepositoryError>;
+ readonly hasActionableByThreadId: (
+ input: HasActionableProjectionThreadProposedPlanInput,
+ ) => Effect.Effect<boolean, ProjectionRepositoryError>;
readonly deleteByThreadId: (
input: DeleteProjectionThreadProposedPlansInput,
) => Effect.Effect<void, ProjectionRepositoryError>;
diff --git a/apps/server/src/project/ProjectSetupScriptRunner.test.ts b/apps/server/src/project/ProjectSetupScriptRunner.test.ts
index 1863d1b3b..0d51e517c 100644
--- a/apps/server/src/project/ProjectSetupScriptRunner.test.ts
+++ b/apps/server/src/project/ProjectSetupScriptRunner.test.ts
@@ -43,6 +43,8 @@ const makeProjectionSnapshotQueryLayer = (project: OrchestrationProject) =>
getThreadCheckpointContext: () => Effect.die("unused"),
getFullThreadDiffContext: () => Effect.die("unused"),
getThreadShellById: () => Effect.die("unused"),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.die("unused"),
getThreadDetailSnapshot: () => Effect.die("unused"),
searchThreads: () => Effect.succeed({ matches: [] }),
diff --git a/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts b/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts
index 926d03929..eb9c17ef1 100644
--- a/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts
+++ b/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts
@@ -204,6 +204,8 @@ describe("ProviderSessionReaper", () => {
? Option.some(input.readModel.threads.find((thread) => thread.id === threadId)!)
: Option.none(),
),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.die("unused"),
getThreadDetailSnapshot: () => Effect.die("unused"),
searchThreads: () => Effect.succeed({ matches: [] }),
diff --git a/apps/server/src/routines/RoutineDraftDispatcher.test.ts b/apps/server/src/routines/RoutineDraftDispatcher.test.ts
index a5dab621b..28d78f622 100644
--- a/apps/server/src/routines/RoutineDraftDispatcher.test.ts
+++ b/apps/server/src/routines/RoutineDraftDispatcher.test.ts
@@ -170,6 +170,8 @@ function deleteHarness(input: {
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.succeed(Option.none()),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailSnapshot: () => Effect.die("unused"),
};
const layer = RoutineDraftDispatcherLive.pipe(
diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts
index 75af41f85..7e2daec42 100644
--- a/apps/server/src/server.test.ts
+++ b/apps/server/src/server.test.ts
@@ -939,6 +939,8 @@ const buildAppUnderTest = (options?: {
getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
getProjectShellById: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.succeed(Option.none()),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.succeed(Option.none()),
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
@@ -5360,6 +5362,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
shellFetches.push(threadId);
return Option.some(makeDefaultOrchestrationThreadShell({ id: threadId }));
}),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
},
},
});
@@ -5438,6 +5442,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
shellFetches.push(threadId);
return Option.some(makeDefaultOrchestrationThreadShell({ id: threadId }));
}),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
},
},
});
@@ -5528,6 +5534,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
},
projectionSnapshotQuery: {
getThreadShellById: () => Effect.succeed(Option.none()),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
},
},
});
@@ -5589,6 +5597,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
Option.some(makeDefaultOrchestrationThreadShell({ id: threadId })),
);
}),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
},
},
});
@@ -5710,6 +5720,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
}),
),
),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
},
},
});
@@ -5788,6 +5800,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
}),
);
}),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
},
},
});
@@ -5844,6 +5858,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
Effect.succeed(
Option.some(makeDefaultOrchestrationThreadShell({ id: threadId, session: null })),
),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
},
},
});
@@ -5912,6 +5928,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
}),
),
),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
},
},
});
@@ -5978,6 +5996,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
}),
),
),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
},
},
});
@@ -6032,6 +6052,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
Effect.succeed(
Option.some(makeDefaultOrchestrationThreadShell({ id: threadId, session: null })),
),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
},
},
});
@@ -6105,6 +6127,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
}),
),
),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
},
},
});
@@ -6177,6 +6201,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
}),
),
),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
},
},
});
diff --git a/apps/server/src/serverRuntimeStartup.test.ts b/apps/server/src/serverRuntimeStartup.test.ts
index 0b9831419..e96dcbb6b 100644
--- a/apps/server/src/serverRuntimeStartup.test.ts
+++ b/apps/server/src/serverRuntimeStartup.test.ts
@@ -121,6 +121,8 @@ it.effect("resolveAutoBootstrapWelcomeTargets returns existing project and threa
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.die("unused"),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.die("unused"),
getThreadDetailSnapshot: () => Effect.die("unused"),
searchThreads: () => Effect.succeed({ matches: [] }),
@@ -167,6 +169,8 @@ it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.die("unused"),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.die("unused"),
getThreadDetailSnapshot: () => Effect.die("unused"),
searchThreads: () => Effect.succeed({ matches: [] }),
@@ -213,6 +217,8 @@ it.effect(
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.die("unused"),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.die("unused"),
getThreadDetailSnapshot: () => Effect.die("unused"),
searchThreads: () => Effect.succeed({ matches: [] }),
@@ -260,6 +266,8 @@ it.effect(
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.die("unused"),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.die("unused"),
getThreadDetailSnapshot: () => Effect.die("unused"),
searchThreads: () => Effect.succeed({ matches: [] }),
@@ -311,6 +319,8 @@ it.effect("resolveAutoBootstrapWelcomeTargets preserves typed UUID generation fa
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.die("unused"),
+ getThreadRuntimeContext: () => Effect.die("unused"),
+ getTurnStartMessage: () => Effect.die("unused"),
getThreadDetailById: () => Effect.die("unused"),
getThreadDetailSnapshot: () => Effect.die("unused"),
searchThreads: () => Effect.succeed({ matches: [] }),
diff --git a/docs/internals/overview.md b/docs/internals/overview.md
index d7ae80f4e..51bea3d65 100644
--- a/docs/internals/overview.md
+++ b/docs/internals/overview.md
@@ -79,6 +79,16 @@ Because persistence and projection share a transaction, the read model cannot du
the event log. On dispatch failure the engine rereads persisted events past the starting sequence and
reconciles.
+[`ProjectionSnapshotQuery.ts`][snapshot] is the persisted read model. Client thread snapshots load
+activity payloads in 25-row primary-key batches and project each batch before the next read. Shell
+summary refresh uses SQL aggregates for latest user-message time, pending approval count, and
+actionable-plan status, plus user-input lifecycle rows only. Provider command metadata paths use
+thread shells; turn start uses a single-message query. Runtime ingestion uses a joined thread
+context and keyed message, plan, and task-activity lookups instead of hydrating a full thread.
+`upsertMany` exists for projector cursors. Runtime `projectEvent` still commits each projector
+separately because attachment cleanup runs after each projector transaction. Combining those
+commits is shared with checkpoint attachment-transaction work and is not done here.
+
Command and event names live in [`orchestration.ts`][contracts]. Some commands are client
dispatchable (`thread.create`, `thread.turn.start`, `thread.approval.respond`); others are internal
and produced only by server-side reactors (`thread.message.assistant.delta`,
@@ -142,6 +152,7 @@ already dispatch.
[session]: ../../packages/client-runtime/src/rpc/session.ts
[startup]: ../../apps/server/src/serverRuntimeStartup.ts
[engine]: ../../apps/server/src/orchestration/Layers/OrchestrationEngine.ts
+[snapshot]: ../../apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts
[decider]: ../../apps/server/src/orchestration/decider.ts
[projector]: ../../apps/server/src/orchestration/projector.ts
[worker]: ../../packages/shared/src/DrainableWorker.ts
diff --git a/packages/client-runtime/src/state/threadReducer.test.ts b/packages/client-runtime/src/state/threadReducer.test.ts
index 1fa481dd1..50d2f6f91 100644
--- a/packages/client-runtime/src/state/threadReducer.test.ts
+++ b/packages/client-runtime/src/state/threadReducer.test.ts
@@ -845,6 +845,124 @@ describe("applyThreadDetailEvent", () => {
}
});
+ it("appends in-order live activities without dropping earlier rows", () => {
+ const makeActivity = (id: string, sequence: number | null) => ({
+ id: EventId.make(id),
+ tone: "tool" as const,
+ kind: "command",
+ summary: id,
+ payload: {},
+ turnId: TurnId.make("turn-1"),
+ ...(sequence === null ? {} : { sequence }),
+ createdAt: "2026-04-01T11:00:00.000Z",
+ });
+ const first = applyThreadDetailEvent(baseThread, {
+ ...baseEventFields,
+ sequence: 133,
+ occurredAt: "2026-04-01T11:01:00.000Z",
+ aggregateKind: "thread",
+ aggregateId: ThreadId.make("thread-1"),
+ type: "thread.activity-appended",
+ payload: {
+ threadId: ThreadId.make("thread-1"),
+ activity: makeActivity("activity-a", 1),
+ },
+ });
+ expect(first.kind).toBe("updated");
+ if (first.kind !== "updated") {
+ return;
+ }
+ const second = applyThreadDetailEvent(first.thread, {
+ ...baseEventFields,
+ sequence: 134,
+ occurredAt: "2026-04-01T11:01:00.000Z",
+ aggregateKind: "thread",
+ aggregateId: ThreadId.make("thread-1"),
+ type: "thread.activity-appended",
+ payload: {
+ threadId: ThreadId.make("thread-1"),
+ activity: makeActivity("activity-null", null),
+ },
+ });
+ expect(second.kind).toBe("updated");
+ if (second.kind !== "updated") {
+ return;
+ }
+ const result = applyThreadDetailEvent(second.thread, {
+ ...baseEventFields,
+ sequence: 135,
+ occurredAt: "2026-04-01T11:01:00.000Z",
+ aggregateKind: "thread",
+ aggregateId: ThreadId.make("thread-1"),
+ type: "thread.activity-appended",
+ payload: {
+ threadId: ThreadId.make("thread-1"),
+ activity: makeActivity("activity-b", 2),
+ },
+ });
+
+ expect(result.kind).toBe("updated");
+ if (result.kind === "updated") {
+ expect(result.thread.activities.map((activity) => activity.id)).toEqual([
+ "activity-a",
+ "activity-b",
+ "activity-null",
+ ]);
+ }
+ });
+
+ it("dedupes a re-delivery arriving right after an in-order append", () => {
+ const makeActivity = (id: string, sequence: number, summary: string) => ({
+ id: EventId.make(id),
+ tone: "tool" as const,
+ kind: "command",
+ summary,
+ payload: {},
+ turnId: TurnId.make("turn-1"),
+ sequence,
+ createdAt: "2026-04-01T11:00:00.000Z",
+ });
+ const makeEvent = (sequence: number, activity: ReturnType<typeof makeActivity>) =>
+ ({
+ ...baseEventFields,
+ sequence,
+ occurredAt: "2026-04-01T11:01:00.000Z",
+ aggregateKind: "thread",
+ aggregateId: ThreadId.make("thread-1"),
+ type: "thread.activity-appended",
+ payload: { threadId: ThreadId.make("thread-1"), activity },
+ }) as const;
+ const first = applyThreadDetailEvent(
+ { ...baseThread, activities: [makeActivity("activity-a", 1, "first")] },
+ makeEvent(133, makeActivity("activity-b", 2, "second")),
+ );
+ expect(first.kind).toBe("updated");
+ if (first.kind !== "updated") {
+ return;
+ }
+ const second = applyThreadDetailEvent(
+ first.thread,
+ makeEvent(134, makeActivity("activity-c", 3, "third")),
+ );
+ expect(second.kind).toBe("updated");
+ if (second.kind !== "updated") {
+ return;
+ }
+ const third = applyThreadDetailEvent(
+ second.thread,
+ makeEvent(135, makeActivity("activity-c", 4, "third (redelivered)")),
+ );
+ expect(third.kind).toBe("updated");
+ if (third.kind === "updated") {
+ expect(third.thread.activities.map((activity) => activity.id)).toEqual([
+ "activity-a",
+ "activity-b",
+ "activity-c",
+ ]);
+ expect(third.thread.activities[2]?.summary).toBe("third (redelivered)");
+ }
+ });
+
it("preserves the complete activity history when live events arrive", () => {
const existingActivities = Array.from({ length: 129 }, (_, index) => ({
id: EventId.make(`activity-${index}`),
diff --git a/packages/client-runtime/src/state/threadReducer.ts b/packages/client-runtime/src/state/threadReducer.ts
index 4c5545831..b6e6b2df9 100644
--- a/packages/client-runtime/src/state/threadReducer.ts
+++ b/packages/client-runtime/src/state/threadReducer.ts
@@ -35,6 +35,15 @@ const activityOrder = O.combineAll<OrchestrationThreadActivity>([
O.mapInput(O.String, (a) => a.id),
]);
+// Per-array id index so the streaming append path can reject a re-delivered
+// id without rescanning the history. Only arrays this reducer produced are
+// indexed: presence also proves the array is activityOrder-sorted, which
+// snapshot-loaded arrays (DB order, null sequences first) are not.
+const activityIdIndex = new WeakMap<
+ ReadonlyArray<OrchestrationThreadActivity>,
+ Set<OrchestrationThreadActivity["id"]>
+>();
+
/**
* Matches the validity rule in `deriveLatestContextWindowSnapshot` (and the
* server's snapshot-side `dropStaleContextWindowActivities`): rows without a
@@ -624,6 +633,31 @@ export function applyThreadDetailEvent(
// thread.reverted that discards turns can still resolve a value from
// the turns that survive.
const supersedesContextWindow = isResolvableContextWindowActivity(activity);
+ // Live streams append in order: an unseen id sorting at/after the tail
+ // of a known-sorted array appends without re-filtering and re-sorting
+ // the whole history on every event. The id set moves forward to the new
+ // array; a superseded array falls back to the sorting path.
+ const ids = activityIdIndex.get(thread.activities);
+ const lastActivity = thread.activities.at(-1);
+ if (
+ !supersedesContextWindow &&
+ ids !== undefined &&
+ (lastActivity === undefined || activityOrder(lastActivity, activity) <= 0) &&
+ !ids.has(activity.id)
+ ) {
+ const activities = Arr.append(thread.activities, activity);
+ activityIdIndex.delete(thread.activities);
+ ids.add(activity.id);
+ activityIdIndex.set(activities, ids);
+ return {
+ kind: "updated",
+ thread: {
+ ...thread,
+ activities,
+ updatedAt: event.occurredAt,
+ },
+ };
+ }
const activities = pipe(
thread.activities,
Arr.filter(
@@ -638,6 +672,7 @@ export function applyThreadDetailEvent(
Arr.append(activity),
Arr.sort(activityOrder),
);
+ activityIdIndex.set(activities, new Set(activities.map((entry) => entry.id)));
return {
kind: "updated",