Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/server/src/checkpointing/Layers/CheckpointDiffQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ describe("CheckpointDiffQueryLive", () => {
listThreadShells: () => Effect.succeed([]),
getThreadShellById: () => Effect.succeed(Option.none()),
getTurnActivities: () => Effect.succeed([]),
getThreadMessage: () => Effect.succeed(Option.none()),
hasAssistantMessageForTurn: () => Effect.succeed(false),
getThreadDetailById: () => Effect.succeed(Option.none()),
}),
),
Expand Down Expand Up @@ -235,6 +237,8 @@ describe("CheckpointDiffQueryLive", () => {
listThreadShells: () => Effect.succeed([]),
getThreadShellById: () => Effect.succeed(Option.none()),
getTurnActivities: () => Effect.succeed([]),
getThreadMessage: () => Effect.succeed(Option.none()),
hasAssistantMessageForTurn: () => Effect.succeed(false),
getThreadDetailById: () => Effect.succeed(Option.none()),
}),
),
Expand Down Expand Up @@ -367,6 +371,8 @@ describe("CheckpointDiffQueryLive", () => {
listThreadShells: () => Effect.succeed([]),
getThreadShellById: () => Effect.succeed(Option.none()),
getTurnActivities: () => Effect.succeed([]),
getThreadMessage: () => Effect.succeed(Option.none()),
hasAssistantMessageForTurn: () => Effect.succeed(false),
getThreadDetailById: () => Effect.succeed(Option.none()),
}),
),
Expand Down Expand Up @@ -449,6 +455,8 @@ describe("CheckpointDiffQueryLive", () => {
listThreadShells: () => Effect.succeed([]),
getThreadShellById: () => Effect.succeed(Option.none()),
getTurnActivities: () => Effect.succeed([]),
getThreadMessage: () => Effect.succeed(Option.none()),
hasAssistantMessageForTurn: () => Effect.succeed(false),
getThreadDetailById: () => Effect.succeed(Option.none()),
}),
),
Expand Down Expand Up @@ -527,6 +535,8 @@ describe("CheckpointDiffQueryLive", () => {
listThreadShells: () => Effect.succeed([]),
getThreadShellById: () => Effect.succeed(Option.none()),
getTurnActivities: () => Effect.succeed([]),
getThreadMessage: () => Effect.succeed(Option.none()),
hasAssistantMessageForTurn: () => Effect.succeed(false),
getThreadDetailById: () => Effect.succeed(Option.none()),
}),
),
Expand Down Expand Up @@ -588,6 +598,8 @@ describe("CheckpointDiffQueryLive", () => {
listThreadShells: () => Effect.succeed([]),
getThreadShellById: () => Effect.succeed(Option.none()),
getTurnActivities: () => Effect.succeed([]),
getThreadMessage: () => Effect.succeed(Option.none()),
hasAssistantMessageForTurn: () => Effect.succeed(false),
getThreadDetailById: () => Effect.succeed(Option.none()),
}),
),
Expand Down
16 changes: 10 additions & 6 deletions apps/server/src/observability/Layers/Observability.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
makeLocalFileTracer,
makeTraceSink,
type TraceRecord,
type TraceRecordHead,
} from "@threadlines/shared/observability";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
Expand All @@ -28,7 +28,7 @@ const PROVIDER_MESSAGE_TRACE_SAMPLE_MODULO = 100;
// 4.1.2 a transaction's BEGIN, COMMIT and the like are spans of their own, so
// any span the SQL client tags with its database is sampled too.
const sampledSqlSpanNames = new Set(["sqlite", "sql.execute", "sql.transaction"]);
const isSqlSpan = (record: TraceRecord): boolean =>
const isSqlSpan = (record: TraceRecordHead): boolean =>
sampledSqlSpanNames.has(record.name) || record.attributes["db.system.name"] !== undefined;
const sampledProjectionSpanNames = new Set([
"applyPendingApprovalsProjection",
Expand Down Expand Up @@ -70,11 +70,11 @@ function traceSampleHash(input: string): number {
return hash >>> 0;
}

function shouldKeepSampledTraceRecord(record: TraceRecord, modulo: number): boolean {
function shouldKeepSampledTraceRecord(record: TraceRecordHead, modulo: number): boolean {
return traceSampleHash(`${record.traceId}:${record.spanId}:${record.name}`) % modulo === 0;
}

function hasWarningOrErrorEvent(record: TraceRecord): boolean {
function hasWarningOrErrorEvent(record: TraceRecordHead): boolean {
return record.events.some((event) => {
const level = event.attributes["effect.logLevel"];
if (typeof level !== "string") {
Expand All @@ -90,12 +90,16 @@ function hasWarningOrErrorEvent(record: TraceRecord): boolean {
});
}

export function shouldRecordServerLocalTrace(record: TraceRecord): boolean {
export function shouldRecordServerLocalTrace(record: TraceRecordHead): boolean {
if (record.durationMs >= SLOW_TRACE_RECORD_THRESHOLD_MS || hasWarningOrErrorEvent(record)) {
return true;
}

if (record.type === "effect-span" && record.exit._tag !== "Success") {
if (
record.type === "effect-span" &&
record.exit !== undefined &&
record.exit._tag !== "Success"
) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ describe("OrchestrationEngine", () => {
listThreadShells: () => Effect.succeed([]),
getThreadShellById: () => Effect.succeed(Option.none()),
getTurnActivities: () => Effect.succeed([]),
getThreadMessage: () => Effect.succeed(Option.none()),
hasAssistantMessageForTurn: () => Effect.succeed(false),
getThreadDetailById: () => Effect.succeed(Option.none()),
}),
),
Expand Down
26 changes: 19 additions & 7 deletions apps/server/src/orchestration/Layers/ProjectionPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,12 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
}
: {}),
});
// An agent's reply changes nothing the summary counts (the user's
// last message, plans, prompts, approvals), and while it streams it
// arrives many times a second.
if (event.type === "thread.message-sent" && event.payload.role === "assistant") {
return;
}
yield* refreshThreadShellSummary(event.payload.threadId);
return;
}
Expand Down Expand Up @@ -1583,16 +1589,19 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
}

case "thread.message-sent": {
const existingMessage = yield* projectionThreadMessageRepository.getByMessageId({
messageId: event.payload.messageId,
});
// A streaming reply's words are added to the stored text where it
// lies, never read back: a long reply would otherwise be copied out
// and back in full every time a few words arrive.
const streamedOnly = event.payload.streaming;
const existingMessage = streamedOnly
? Option.none()
: yield* projectionThreadMessageRepository.getByMessageId({
messageId: event.payload.messageId,
});
const previousMessage = Option.getOrUndefined(existingMessage);
const nextText = Option.match(existingMessage, {
onNone: () => event.payload.text,
onSome: (message) => {
if (event.payload.streaming) {
return `${message.text}${event.payload.text}`;
}
if (event.payload.text.length === 0) {
return message.text;
}
Expand All @@ -1606,7 +1615,10 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
})
: previousMessage?.attachments;
const nextSkills = event.payload.skills ?? previousMessage?.skills;
yield* projectionThreadMessageRepository.upsert({
const write = streamedOnly
? projectionThreadMessageRepository.appendStreamedText
: projectionThreadMessageRepository.upsert;
yield* write({
messageId: event.payload.messageId,
eventSequence: previousMessage?.eventSequence ?? event.sequence,
threadId: event.payload.threadId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,52 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
}),
);

it.effect("reads one message of a thread and whether a turn has a reply", () =>
Effect.gen(function* () {
const snapshotQuery = yield* ProjectionSnapshotQuery;
const sql = yield* SqlClient.SqlClient;

yield* sql`DELETE FROM projection_thread_messages`;
for (const [messageId, threadId, turnId, role] of [
["one-user", "thread-one", "turn-a", "user"],
["one-reply", "thread-one", "turn-a", "assistant"],
["one-question", "thread-one", "turn-b", "user"],
["other-reply", "thread-other", "turn-b", "assistant"],
]) {
yield* sql`
INSERT INTO projection_thread_messages (
message_id, thread_id, turn_id, role, text, is_streaming, created_at, updated_at
) VALUES (
${messageId}, ${threadId}, ${turnId}, ${role}, ${`${messageId} text`}, 0,
'2026-04-01T00:00:00.000Z', '2026-04-01T00:00:00.000Z'
)
`;
}
const threadId = ThreadId.make("thread-one");

const reply = yield* snapshotQuery.getThreadMessage({
threadId,
messageId: asMessageId("one-reply"),
});
assert.equal(reply._tag === "Some" ? reply.value.text : null, "one-reply text");
// Another thread's message is not this thread's.
const elsewhere = yield* snapshotQuery.getThreadMessage({
threadId,
messageId: asMessageId("other-reply"),
});
assert.equal(elsewhere._tag, "None");

assert.equal(
yield* snapshotQuery.hasAssistantMessageForTurn({ threadId, turnId: asTurnId("turn-a") }),
true,
);
assert.equal(
yield* snapshotQuery.hasAssistantMessageForTurn({ threadId, turnId: asTurnId("turn-b") }),
false,
);
}),
);

it.effect("hydrates thread messages into the command read model", () =>
Effect.gen(function* () {
const snapshotQuery = yield* ProjectionSnapshotQuery;
Expand Down
65 changes: 59 additions & 6 deletions apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1940,12 +1940,8 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
`,
});

const listThreadMessageRowsByThread = SqlSchema.findAll({
Request: ThreadIdLookupInput,
Result: ProjectionThreadMessageDbRowSchema,
execute: ({ threadId }) =>
sql`
SELECT
/** Every column a thread message row is read with. */
const threadMessageColumns = sql`
message_id AS "messageId",
event_sequence AS "eventSequence",
thread_id AS "threadId",
Expand Down Expand Up @@ -1974,12 +1970,43 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
is_streaming AS "isStreaming",
created_at AS "createdAt",
updated_at AS "updatedAt"
`;

const listThreadMessageRowsByThread = SqlSchema.findAll({
Request: ThreadIdLookupInput,
Result: ProjectionThreadMessageDbRowSchema,
execute: ({ threadId }) =>
sql`
SELECT ${threadMessageColumns}
FROM projection_thread_messages
WHERE thread_id = ${threadId}
ORDER BY event_sequence ASC, created_at ASC, message_id ASC
`,
});

const getThreadMessageRow = SqlSchema.findOneOption({
Request: Schema.Struct({ threadId: ThreadId, messageId: MessageId }),
Result: ProjectionThreadMessageDbRowSchema,
execute: ({ threadId, messageId }) =>
sql`
SELECT ${threadMessageColumns}
FROM projection_thread_messages
WHERE thread_id = ${threadId} AND message_id = ${messageId}
`,
});

const getAssistantMessageIdForTurnRow = SqlSchema.findOneOption({
Request: Schema.Struct({ threadId: ThreadId, turnId: TurnId }),
Result: Schema.Struct({ messageId: MessageId }),
execute: ({ threadId, turnId }) =>
sql`
SELECT message_id AS "messageId"
FROM projection_thread_messages
WHERE thread_id = ${threadId} AND turn_id = ${turnId} AND role = 'assistant'
LIMIT 1
`,
});

// Workflow runs: the run is stored as JSON (see ProjectionThreadWorkflowRuns).
const listThreadWorkflowRunRows = SqlSchema.findAll({
Request: Schema.Void,
Expand Down Expand Up @@ -3886,6 +3913,30 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
);
});

const getThreadMessage: ProjectionSnapshotQueryShape["getThreadMessage"] = (input) =>
getThreadMessageRow(input).pipe(
Effect.map(Option.map(mapThreadMessageRow)),
Effect.mapError(
toPersistenceSqlOrDecodeError(
"ProjectionSnapshotQuery.getThreadMessage:query",
"ProjectionSnapshotQuery.getThreadMessage:decodeRow",
),
),
);

const hasAssistantMessageForTurn: ProjectionSnapshotQueryShape["hasAssistantMessageForTurn"] = (
input,
) =>
getAssistantMessageIdForTurnRow(input).pipe(
Effect.map(Option.isSome),
Effect.mapError(
toPersistenceSqlOrDecodeError(
"ProjectionSnapshotQuery.hasAssistantMessageForTurn:query",
"ProjectionSnapshotQuery.hasAssistantMessageForTurn:decodeRow",
),
),
);

const getTurnActivities: ProjectionSnapshotQueryShape["getTurnActivities"] = (input) =>
listThreadActivityRowsByTurn(input).pipe(
Effect.map((rows) => rows.map(mapThreadActivityRow)),
Expand Down Expand Up @@ -4118,6 +4169,8 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
getThreadShellById,
getThreadDetailById,
getTurnActivities,
getThreadMessage,
hasAssistantMessageForTurn,
} satisfies ProjectionSnapshotQueryShape;
});

Expand Down
Loading
Loading