🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(topics): assign automatic summaries to the serving map and start first discovery (#18811)
Lysander Kiesel4f172d3f0483
diff --git a/packages/shared/src/server/queues.ts b/packages/shared/src/server/queues.tsindex 1533abb8ad..de728ba436 100644--- a/packages/shared/src/server/queues.ts+++ b/packages/shared/src/server/queues.ts@@ -394,6 +394,7 @@ export enum QueueName {Topics = "topics",TopicsUpdate = "topics-update",TopicsEmbedding = "topics-embedding",+ TopicsAutomation = "topics-automation",TraceBatch = "trace-batch",TraceUpsert = "trace-upsert", // Ingestion pipeline adds events on each Trace upsertTraceDelete = "trace-delete",@@ -439,6 +440,7 @@ export enum QueueName {export enum QueueJobs {Topics = "topics",TopicsEmbedding = "topics-embedding",+ TopicsAutomation = "topics-automation",TraceBatch = "trace-batch",TraceUpsert = "trace-upsert",TraceDelete = "trace-delete",@@ -502,6 +504,25 @@ export const TopicEmbeddingBatchSchema = z.object({.min(1),});+// Summaries one trace batch stored for a project, with the facets and vector space they used.+export const TopicAutomationBatchSchema = z.object({+ projectId: z.string().min(1),+ traceIds: z.array(z.string().min(1)).min(1).max(100),+ facets: z+ .array(+ z.object({+ facetId: z.string().min(1),+ version: z.number().int().positive(),+ }),+ )+ .min(1),+ embeddingConfig: z.object({+ embeddingModel: z.string().min(1),+ embeddingDimensions: z.number().int().positive(),+ }),+ timeRange: z.object({ from: z.coerce.date(), to: z.coerce.date() }),+});+export const TraceBatchEventSchema = z.object({timestamp: z.coerce.date(),id: z.string(),@@ -547,6 +568,12 @@ export type TQueueJobTypes = {name: QueueJobs.TopicsEmbedding;payload: z.infer<typeof TopicEmbeddingBatchSchema>;};+ [QueueName.TopicsAutomation]: {+ timestamp: Date;+ id: string;+ name: QueueJobs.TopicsAutomation;+ payload: z.infer<typeof TopicAutomationBatchSchema>;+ };[QueueName.TraceBatch]: z.infer<typeof TraceBatchEventSchema>;[QueueName.TraceUpsert]: {timestamp: Date;diff --git a/packages/shared/src/server/redis/getQueue.ts b/packages/shared/src/server/redis/getQueue.tsindex 84d909830e..aefc939dff 100644--- a/packages/shared/src/server/redis/getQueue.ts+++ b/packages/shared/src/server/redis/getQueue.ts@@ -9,6 +9,7 @@ import { ExperimentCreateQueue } from "./experimentCreateQueue";import { TraceDeleteQueue } from "./traceDelete";import { TopicsQueue, TopicsUpdateQueue } from "../topics/queue";import { TopicsEmbeddingQueue } from "../topics/embedding-queue";+import { TopicsAutomationQueue } from "../topics/automation-queue";import { TraceBatchQueue } from "./traceBatch";import { ProjectDeleteQueue } from "./projectDelete";import { PostHogIntegrationQueue } from "./postHogIntegrationQueue";@@ -57,6 +58,8 @@ export function getQueue(return TopicsUpdateQueue.getInstance();case QueueName.TopicsEmbedding:return TopicsEmbeddingQueue.getInstance();+ case QueueName.TopicsAutomation:+ return TopicsAutomationQueue.getInstance();case QueueName.TraceBatch:return TraceBatchQueue.getInstance();case QueueName.BatchExport:diff --git a/packages/shared/src/server/topics/automation-queue.ts b/packages/shared/src/server/topics/automation-queue.tsnew file mode 100644index 0000000000..c69aa1b860--- /dev/null+++ b/packages/shared/src/server/topics/automation-queue.ts@@ -0,0 +1,37 @@+import { Queue } from "bullmq";+import { logger } from "../logger";+import { QueueName, type TQueueJobTypes } from "../queues";+import { createBullMQQueueOptionsWithRedis } from "../redis/redis";+import { isTopicsEnabled } from "./config";++/** Assigns automatically summarized traces to their facet's serving map. */+export class TopicsAutomationQueue {+ private static instance: Queue<+ TQueueJobTypes[QueueName.TopicsAutomation]+ > | null = null;++ static getInstance() {+ if (!isTopicsEnabled()) return null;+ if (this.instance) return this.instance;+ const options = createBullMQQueueOptionsWithRedis(+ QueueName.TopicsAutomation,+ );+ if (!options) return null;+ this.instance = new Queue<TQueueJobTypes[QueueName.TopicsAutomation]>(+ QueueName.TopicsAutomation,+ {+ ...options,+ defaultJobOptions: {+ attempts: 3,+ backoff: { type: "exponential", delay: 5000 },+ removeOnComplete: { age: 86400, count: 1000 },+ removeOnFail: { age: 604800, count: 1000 },+ },+ },+ );+ this.instance.on("error", (error) =>+ logger.error("TopicsAutomationQueue error", error),+ );+ return this.instance;+ }+}diff --git a/packages/shared/src/server/topics/execution-store.ts b/packages/shared/src/server/topics/execution-store.tsindex 0841c7bd09..7a26102fff 100644--- a/packages/shared/src/server/topics/execution-store.ts+++ b/packages/shared/src/server/topics/execution-store.ts@@ -14,6 +14,8 @@ import {} from "../../topics";export const TOPICS_ACTION = "topics";+// Actor of executions Topics starts on its own, such as automatic discovery.+export const TOPICS_SYSTEM_USER_ID = "system:topics";type BatchMetadata = {inputSettings:| Extract<TopicExecutionSummary["input"], { operation: "process" }>diff --git a/packages/shared/src/server/topics/index.ts b/packages/shared/src/server/topics/index.tsindex 068bd2a264..bba9dc39ed 100644--- a/packages/shared/src/server/topics/index.ts+++ b/packages/shared/src/server/topics/index.ts@@ -3,6 +3,7 @@ export * from "./postgres";export * from "./clickhouse";export * from "./queue";export * from "./embedding-queue";+export * from "./automation-queue";export { emitTopicStagingMetrics } from "./staging-metrics";export * from "./embeddings";export * from "./text";diff --git a/web/src/features/topics/AutomaticProcessingDialog.tsx b/web/src/features/topics/AutomaticProcessingDialog.tsxindex b85829d841..55283fb9eb 100644--- a/web/src/features/topics/AutomaticProcessingDialog.tsx+++ b/web/src/features/topics/AutomaticProcessingDialog.tsx@@ -67,6 +67,13 @@ export function AutomaticProcessingDialog({rate below applies only to automatic processing. For manualprocessing, such as backfills, you set the sampling for each run.</p>+ <p className="text-sm">+ Each summarized trace joins the closest existing topic when it is+ similar; otherwise it appears under Outliers. A facet without topics+ gets its first ones once 100 traces from the last seven days are+ summarized. Topics are not refreshed automatically; run{" "}+ <em>Regenerate topics</em> to refresh them.+ </p>{paused ? (<Alert variant="warning"><Alert.Title>Paused: {paused.shortLabel}</Alert.Title>diff --git a/worker/src/app.ts b/worker/src/app.tsindex ad27dc7e31..d6c2a52ce4 100644--- a/worker/src/app.ts+++ b/worker/src/app.ts@@ -114,6 +114,7 @@ import { InAppAgentDlqRetryRunner } from "./features/in-app-agent-dlq-retry-runnimport { isTopicsEnabled } from "@langfuse/shared/topics/server";import { topicsQueueProcessor } from "./queues/topicsQueue";import { topicsEmbeddingQueueProcessor } from "./queues/topicsEmbeddingQueue";+import { topicsAutomationQueueProcessor } from "./queues/topicsAutomationQueue";const app = express();@@ -469,19 +470,28 @@ if (env.QUEUE_CONSUMER_MONITOR_QUEUE_IS_ENABLED === "true") {export let inAppAgentDlqRetryRunner: InAppAgentDlqRetryRunner | null = null;if (isTopicsEnabled()) {- WorkerManager.register(QueueName.Topics, topicsQueueProcessor, {- concurrency: 1,- });- WorkerManager.register(QueueName.TopicsUpdate, topicsQueueProcessor, {- concurrency: 1,- });- WorkerManager.register(- QueueName.TopicsEmbedding,- topicsEmbeddingQueueProcessor,- {- concurrency: 2,- },- );+ if (env.QUEUE_CONSUMER_TOPICS_QUEUE_IS_ENABLED === "true")+ WorkerManager.register(QueueName.Topics, topicsQueueProcessor, {+ concurrency: 1,+ });+ if (env.QUEUE_CONSUMER_TOPICS_UPDATE_QUEUE_IS_ENABLED === "true")+ WorkerManager.register(QueueName.TopicsUpdate, topicsQueueProcessor, {+ concurrency: 1,+ });+ if (env.QUEUE_CONSUMER_TOPICS_EMBEDDING_QUEUE_IS_ENABLED === "true")+ WorkerManager.register(+ QueueName.TopicsEmbedding,+ topicsEmbeddingQueueProcessor,+ {+ concurrency: 2,+ },+ );+ if (env.QUEUE_CONSUMER_TOPICS_AUTOMATION_QUEUE_IS_ENABLED === "true")+ WorkerManager.register(+ QueueName.TopicsAutomation,+ topicsAutomationQueueProcessor,+ { concurrency: 2 },+ );}if (diff --git a/worker/src/env.ts b/worker/src/env.tsindex e2dc1fb014..d17ef9bdc4 100644--- a/worker/src/env.ts+++ b/worker/src/env.ts@@ -423,6 +423,18 @@ const EnvSchema = z.object({QUEUE_CONSUMER_BATCH_ACTION_QUEUE_IS_ENABLED: z.enum(["true", "false"]).default("true"),+ QUEUE_CONSUMER_TOPICS_QUEUE_IS_ENABLED: z+ .enum(["true", "false"])+ .default("true"),+ QUEUE_CONSUMER_TOPICS_UPDATE_QUEUE_IS_ENABLED: z+ .enum(["true", "false"])+ .default("true"),+ QUEUE_CONSUMER_TOPICS_EMBEDDING_QUEUE_IS_ENABLED: z+ .enum(["true", "false"])+ .default("true"),+ QUEUE_CONSUMER_TOPICS_AUTOMATION_QUEUE_IS_ENABLED: z+ .enum(["true", "false"])+ .default("true"),QUEUE_CONSUMER_EVAL_EXECUTION_QUEUE_IS_ENABLED: z.enum(["true", "false"]).default("true"),diff --git a/worker/src/features/queue-metrics-runner/index.ts b/worker/src/features/queue-metrics-runner/index.tsindex 98c89c3f25..68e162f6d8 100644--- a/worker/src/features/queue-metrics-runner/index.ts+++ b/worker/src/features/queue-metrics-runner/index.ts@@ -32,6 +32,7 @@ const TOPICS_QUEUES = new Set([QueueName.Topics,QueueName.TopicsUpdate,QueueName.TopicsEmbedding,+ QueueName.TopicsAutomation,]);async function emitPendingHeadAge(diff --git a/worker/src/features/topics/README.md b/worker/src/features/topics/README.mdindex 237eb2fdf3..8c7b5f4e30 100644--- a/worker/src/features/topics/README.md+++ b/worker/src/features/topics/README.md@@ -181,6 +181,25 @@ traces that arrive meanwhile are not queued. An authentication or billing error(HTTP 401, 402, 403) turns the project's automatic processing off with thefailing slot as the reason, and the rest of the batch skips that project.+After the write, the batch queues its stored complete summaries on+`topics-automation`, one job per project and 100 traces; a retried batch reuses+its job IDs. A failed enqueue is logged and does not fail the batch. The job+rereads those summaries and assigns the ones not yet assigned to their facet's+compatible serving map with origin `online`; BullMQ retries a failed job three+times. Each Topics queue has its own `QUEUE_CONSUMER_<QUEUE>_QUEUE_IS_ENABLED`+switch (`TOPICS`, `TOPICS_UPDATE`, `TOPICS_EMBEDDING`, `TOPICS_AUTOMATION`); set+them to `false` on fleets that only run trace batches.++A facet version without any map starts one automatic **Update topics** execution+once it has 100 compatible summaries from the last seven days, while automatic+processing is on with the embedding model those summaries used. It appears in+History under the `system:topics` actor. A queued execution is queued again by+the next job; a failed one is never restarted automatically and is resumed from+History. An existing map or fit in progress for the version, even an+incompatible one, blocks discovery; replace it with **Update topics**. Summaries+stored while the first map is being fitted stay unassigned until the next+**Update topics**.+Required for a local run, in addition to Postgres, ClickHouse, and Redis:| Variable | Role |@@ -465,8 +484,9 @@ the completed attempt without fitting again. Skipped attempts also resumewithout recomputation, using the previously saved cohort count to distinguishno applicable summaries from insufficient data.-There is no automatic discovery schedule or stream producer yet. The planned-six-hour scheduler should invoke the same update path as the manual button.+Automatic processing starts only a facet version's first discovery; there is no+refit schedule yet. The planned six-hour scheduler should invoke the same update+path as the manual button.Large per-project discovery, all-member naming, and distributed stream processingremain follow-up work; this is not a 100-million-traces/day throughput validation.diff --git a/worker/src/features/topics/assignments.ts b/worker/src/features/topics/assignments.tsnew file mode 100644index 0000000000..c7443a7ea8--- /dev/null+++ b/worker/src/features/topics/assignments.ts@@ -0,0 +1,64 @@+import type {+ TopicAssignment,+ TopicEmbeddingConfig,+ TopicFacetRef,+ TopicRun,+ TopicSummary,+} from "@langfuse/shared/topics";+import { classifyTopic } from "./classifier";++/** Classifies trace summaries onto a map built from the same facet version and vector space. */+export function topicAssignmentRows(input: {+ projectId: string;+ facet: TopicFacetRef;+ embeddingConfig: TopicEmbeddingConfig;+ summaries: TopicSummary[];+ run: TopicRun;+ origin: TopicAssignment["origin"];+ assignedAt: string;+ coordinates?: Map<string, [number, number]>;+}): TopicAssignment[] {+ const { projectId, facet, embeddingConfig, run } = input;+ if (+ run.projectId !== projectId ||+ run.facetId !== facet.facetId ||+ run.facetVersion !== facet.version ||+ run.topics.some(+ (topic) => topic.centroid.length !== embeddingConfig.embeddingDimensions,+ ) ||+ run.config.embeddingModel !== embeddingConfig.embeddingModel+ )+ throw new Error("Target map is incompatible with this facet version.");+ const prototypes = run.topics.map((topic) => ({+ id: topic.topicVersionId,+ centroid: topic.centroid,+ radius: topic.radius,+ }));+ return input.summaries.map((summary) => {+ if (summary.traceId === null)+ throw new Error("Topics processing requires a trace summary.");+ const assigned = classifyTopic(summary.embedding, prototypes);+ const topic = run.topics.find(+ (candidate) => candidate.topicVersionId === assigned.topicId,+ );+ return {+ coordinates: input.coordinates?.get(summary.traceId) ?? null,+ projectId,+ facetId: facet.facetId,+ facetVersion: facet.version,+ sessionId: summary.sessionId,+ traceId: summary.traceId,+ environment: summary.environment,+ traceName: summary.traceName,+ unitStartTime: summary.unitStartTime,+ summaryProcessedAt: summary.processedAt,+ runId: run.id,+ topicId: topic?.topicId ?? null,+ topicVersionId: topic?.topicVersionId ?? null,+ distance: assigned.distance,+ runnerUpDistance: assigned.runnerUpDistance,+ origin: input.origin,+ assignedAt: input.assignedAt,+ };+ });+}diff --git a/worker/src/features/topics/processTopicsExecution.ts b/worker/src/features/topics/processTopicsExecution.tsindex 81b16a3c88..761b803112 100644--- a/worker/src/features/topics/processTopicsExecution.ts+++ b/worker/src/features/topics/processTopicsExecution.ts@@ -32,7 +32,6 @@ import {type TopicFacetVersion,type TopicSummary,type TopicRun,- type TopicAssignment,type TopicFacetProgress,type TopicDefinition,type TopicProcessBatchState,@@ -55,6 +54,7 @@ import {retainPopulatedTopics,topicHash,} from "./classifier";+import { topicAssignmentRows } from "./assignments";import {runTopicClustering,topicClusterSettings,@@ -347,52 +347,15 @@ async function assignSummaries(assignedAt = new Date().toISOString(),) {return metrics.measure("assignment", async () => {- if (- run.projectId !== execution.projectId ||- run.facetId !== facet.facetId ||- run.facetVersion !== facet.version ||- run.topics.some(- (topic) =>- topic.centroid.length !==- execution.input.embeddingConfig.embeddingDimensions,- ) ||- run.config.embeddingModel !==- execution.input.embeddingConfig.embeddingModel- )- throw new Error("Target map is incompatible with this facet version.");- const prototypes = run.topics.map((topic) => ({- id: topic.topicVersionId,- centroid: topic.centroid,- radius: topic.radius,- }));- const rows = summaries.map<TopicAssignment>((summary) => {- if (summary.traceId === null)- throw new Error("Topics processing requires a trace summary.");- const assigned = classifyTopic(summary.embedding, prototypes);- const topic = run.topics.find(- (candidate) => candidate.topicVersionId === assigned.topicId,- );- const origin =- execution.input.operation === "process" ? "online" : "initial";- return {- coordinates: coordinates.get(summary.traceId) ?? null,- projectId: execution.projectId,- facetId: facet.facetId,- facetVersion: facet.version,- sessionId: summary.sessionId,- traceId: summary.traceId,- environment: summary.environment,- traceName: summary.traceName,- unitStartTime: summary.unitStartTime,- summaryProcessedAt: summary.processedAt,- runId: run.id,- topicId: topic?.topicId ?? null,- topicVersionId: topic?.topicVersionId ?? null,- distance: assigned.distance,- runnerUpDistance: assigned.runnerUpDistance,- origin,- assignedAt,- };+ const rows = topicAssignmentRows({+ projectId: execution.projectId,+ facet: { facetId: facet.facetId, version: facet.version },+ embeddingConfig: execution.input.embeddingConfig,+ summaries,+ run,+ origin: execution.input.operation === "process" ? "online" : "initial",+ assignedAt,+ coordinates,});await metrics.measure("storage",diff --git a/worker/src/features/topics/topicsAutomation.integration.test.ts b/worker/src/features/topics/topicsAutomation.integration.test.tsnew file mode 100644index 0000000000..141b461d4a--- /dev/null+++ b/worker/src/features/topics/topicsAutomation.integration.test.ts@@ -0,0 +1,271 @@+import { randomUUID } from "node:crypto";+import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";+import type { TopicSummary } from "@langfuse/shared/topics";+import {+ createTopicFacet,+ createTopicRun,+ enqueueTopicExecution,+ readTopicAssignments,+ saveTopicRun,+ writeTopicSummaries,+} from "@langfuse/shared/topics/server";+import { clickhouseClient } from "@langfuse/shared/src/server/clickhouse";+import { prisma } from "@langfuse/shared/src/db";+import { processTopicsAutomation } from "./topicsAutomation";++// Discovery's queue handoff and model settings are not under test; its execution row is.+vi.mock("@langfuse/shared/topics/server", async (importOriginal) => ({+ ...(await importOriginal<typeof import("@langfuse/shared/topics/server")>()),+ enqueueTopicExecution: vi.fn(),+ getEnabledTopicsModels: vi.fn(+ async (projectIds: string[]) =>+ new Map(+ projectIds.map((id) => [+ id,+ { embedding: { model: "embed", dimensions: 64 } },+ ]),+ ),+ ),+}));++const DIMENSIONS = 64;+const vector = (...head: number[]) => [+ ...head,+ ...Array<number>(DIMENSIONS - head.length).fill(0),+];+const embeddingConfig = {+ embeddingModel: "embed",+ embeddingDimensions: DIMENSIONS,+};++describe("automatic Topics assignment against storage", () => {+ const orgId = randomUUID();+ const projectId = randomUUID();++ const summary = (+ facetId: string,+ unitStartTime: string,+ embedding: number[],+ ): TopicSummary => ({+ projectId,+ facetId,+ facetVersion: 1,+ traceId: randomUUID(),+ sessionId: null,+ triggerType: "manual_poc",+ environment: "default",+ traceName: "trace",+ unitStartTime,+ state: "complete",+ summary: "summary",+ embedding,+ transcriptId: "transcript",+ transcriptVersion: "v1",+ summaryModel: "summary",+ embeddingModel: embeddingConfig.embeddingModel,+ providedUsageDetails: {},+ usageDetails: {},+ providedCostDetails: {},+ costDetails: {},+ processedAt: new Date().toISOString(),+ metadata: {},+ });++ beforeAll(async () => {+ await prisma.organization.create({+ data: {+ id: orgId,+ name: "Automatic Topics test",+ projects: { create: [{ id: projectId, name: "Automatic Topics" }] },+ },+ });+ });++ afterAll(async () => {+ for (const table of [+ "topics",+ "topic_facet_summaries",+ "topic_assignments",+ ])+ await clickhouseClient().command({+ query: `DELETE FROM ${table} WHERE project_id = {projectId:String}`,+ query_params: { projectId },+ clickhouse_settings: { mutations_sync: "1" },+ });+ await prisma.organization.deleteMany({ where: { id: orgId } });+ });++ it("assigns a batch's stored summaries to the serving map once, even when the job repeats", async () => {+ const facet = await createTopicFacet({+ projectId,+ name: "Assignment",+ description: "",+ prompt: "Describe the request.",+ });+ const pending = await createTopicRun({+ projectId,+ facetId: facet.id,+ facetVersion: 1,+ config: { embeddingModel: "embed", dimensions: DIMENSIONS },+ });+ await saveTopicRun({+ ...pending,+ status: "completed",+ finishedAt: new Date().toISOString(),+ topics: [+ {+ projectId,+ topicVersionId: randomUUID(),+ topicId: randomUUID(),+ createdByRunId: pending.id,+ createdAt: new Date().toISOString(),+ tags: [],+ name: "Billing",+ description: "",+ centroid: vector(1),+ radius: 0.1,+ representativeSummaries: [],+ metadata: {},+ },+ ],+ });+ // The batch window ends 1 ms after its latest summary, as the enqueue computes it.+ const near = summary(facet.id, "2026-10-14T10:00:00.000Z", vector(1, 0.01));+ const far = summary(facet.id, "2026-10-14T10:05:00.000Z", vector(0, 1));+ await writeTopicSummaries([near, far]);+ const timeRange = {+ from: new Date(near.unitStartTime),+ to: new Date(Date.parse(far.unitStartTime) + 1),+ };+ const batch = {+ projectId,+ traceIds: [near.traceId!, far.traceId!],+ facets: [{ facetId: facet.id, version: 1 }],+ embeddingConfig,+ timeRange,+ };++ await processTopicsAutomation(batch);+ await processTopicsAutomation(batch);++ const assignments = await readTopicAssignments(+ projectId,+ { facetId: facet.id, version: 1 },+ [near, far],+ pending.id,+ timeRange,+ );+ expect(+ Object.fromEntries(+ assignments.map((row) => [row.traceId, row.topicId !== null]),+ ),+ ).toEqual({ [near.traceId!]: true, [far.traceId!]: false });+ const stored = await clickhouseClient().query({+ query: `SELECT count() AS count FROM topic_assignments+ WHERE project_id = {projectId:String} AND facet_id = {facetId:String}`,+ query_params: { projectId, facetId: facet.id },+ format: "JSONEachRow",+ });+ const [{ count }] = await stored.json<{ count: number | string }>();+ expect(Number(count)).toBe(2);+ });++ it("starts one discovery when concurrent jobs reach the threshold, and queues it again if its job is lost", async () => {+ const facet = await createTopicFacet({+ projectId,+ name: "Discovery",+ description: "",+ prompt: "Describe the request.",+ });+ const now = Date.now();+ const rows = Array.from({ length: 100 }, (_, index) =>+ summary(+ facet.id,+ new Date(now - (index + 1) * 60_000).toISOString(),+ vector(1, index / 100),+ ),+ );+ await writeTopicSummaries(rows);+ const batch = {+ projectId,+ traceIds: [rows[0].traceId!],+ facets: [{ facetId: facet.id, version: 1 }],+ embeddingConfig,+ timeRange: {+ from: new Date(rows[0].unitStartTime),+ to: new Date(Date.parse(rows[0].unitStartTime) + 1),+ },+ };++ const startedAt = new Date();++ await Promise.all([+ processTopicsAutomation(batch),+ processTopicsAutomation(batch),+ ]);++ const executions = await prisma.batchAction.findMany({+ where: {+ projectId,+ userId: "system:topics",+ createdAt: { gte: startedAt },+ },+ });+ expect(executions).toHaveLength(1);+ expect(executions[0].status).toBe("QUEUED");++ // Its job was lost: the next job queues the same execution again despite its pending run.+ vi.mocked(enqueueTopicExecution).mockClear();+ await processTopicsAutomation(batch);+ expect(vi.mocked(enqueueTopicExecution).mock.calls).toEqual([+ [projectId, executions[0].id],+ ]);+ });++ it("does not start discovery while a fit for the facet is still running", async () => {+ const facet = await createTopicFacet({+ projectId,+ name: "Running fit",+ description: "",+ prompt: "Describe the request.",+ });+ // A manual Update topics that has not published yet.+ await createTopicRun({+ projectId,+ facetId: facet.id,+ facetVersion: 1,+ config: {},+ });+ const now = Date.now();+ const rows = Array.from({ length: 100 }, (_, index) =>+ summary(+ facet.id,+ new Date(now - (index + 1) * 60_000).toISOString(),+ vector(1, index / 100),+ ),+ );+ await writeTopicSummaries(rows);+ const startedAt = new Date();++ await processTopicsAutomation({+ projectId,+ traceIds: [rows[0].traceId!],+ facets: [{ facetId: facet.id, version: 1 }],+ embeddingConfig,+ timeRange: {+ from: new Date(rows[0].unitStartTime),+ to: new Date(Date.parse(rows[0].unitStartTime) + 1),+ },+ });++ expect(+ await prisma.batchAction.count({+ where: {+ projectId,+ userId: "system:topics",+ createdAt: { gte: startedAt },+ },+ }),+ ).toBe(0);+ });+});diff --git a/worker/src/features/topics/topicsAutomation.test.ts b/worker/src/features/topics/topicsAutomation.test.tsnew file mode 100644index 0000000000..8644abfd2d--- /dev/null+++ b/worker/src/features/topics/topicsAutomation.test.ts@@ -0,0 +1,248 @@+import { beforeEach, describe, expect, it, vi } from "vitest";+import { prisma } from "@langfuse/shared/src/db";+import {+ createTopicExecution,+ enqueueTopicExecution,+ getTopicProcessingMapIds,+ getTopicRun,+ getTopicSummaryCounts,+ getEnabledTopicsModels,+ listTopicSummaries,+ readTopicAssignments,+ readTopicExecutionForRequest,+ TopicsAutomationQueue,+ writeTopicAssignments,+} from "@langfuse/shared/topics/server";+import type {+ TopicAssignment,+ TopicRun,+ TopicSummary,+} from "@langfuse/shared/topics";+import {+ enqueueTopicsAutomation,+ processTopicsAutomation,+} from "./topicsAutomation";++vi.mock("@langfuse/shared/src/db", () => ({+ prisma: { topicClusteringRun: { findFirst: vi.fn(async () => null) } },+}));++const add = vi.fn();+vi.mock("@langfuse/shared/topics/server", () => ({+ TOPICS_SYSTEM_USER_ID: "system:topics",+ TOPICS_TRACE_BATCH_SIZE: 100,+ TopicsAutomationQueue: { getInstance: vi.fn(() => ({ add })) },+ createTopicExecution: vi.fn(async () => ({ id: "execution" })),+ enqueueTopicExecution: vi.fn(),+ getTopicProcessingMapIds: vi.fn(),+ getTopicRun: vi.fn(),+ getTopicSummaryCounts: vi.fn(),+ getEnabledTopicsModels: vi.fn(+ async () =>+ new Map([["project", { embedding: { model: "embed", dimensions: 2 } }]]),+ ),+ listTopicSummaries: vi.fn(),+ readTopicAssignments: vi.fn(async () => []),+ readTopicExecutionForRequest: vi.fn(async () => null),+ writeTopicAssignments: vi.fn(),+}));++const summary = (+ traceId: string,+ embedding: number[],+ overrides: Partial<TopicSummary> = {},+) =>+ ({+ projectId: "project",+ facetId: "facet",+ facetVersion: 2,+ traceId,+ sessionId: null,+ state: "complete",+ embedding,+ embeddingModel: "embed",+ environment: "default",+ traceName: "trace",+ unitStartTime: "2026-10-14T00:00:00.000Z",+ processedAt: "2026-10-14T00:01:00.000Z",+ ...overrides,+ }) as TopicSummary;++const batch = {+ projectId: "project",+ traceIds: ["near", "far", "assigned"],+ facets: [{ facetId: "facet", version: 2 }],+ embeddingConfig: { embeddingModel: "embed", embeddingDimensions: 2 },+ timeRange: {+ from: new Date("2026-10-14T00:00:00.000Z"),+ to: new Date("2026-10-14T00:00:00.001Z"),+ },+};++const run: TopicRun = {+ id: "run",+ projectId: "project",+ facetId: "facet",+ facetVersion: 2,+ status: "completed",+ createdAt: "2026-10-13T00:00:00.000Z",+ startedAt: null,+ finishedAt: null,+ config: { embeddingModel: "embed", dimensions: 2 },+ error: null,+ topics: [+ {+ topicId: "billing",+ topicVersionId: "billing-v1",+ centroid: [1, 0],+ radius: 0.1,+ } as TopicRun["topics"][number],+ ],+};++describe("automatic Topics assignment", () => {+ beforeEach(() => vi.clearAllMocks());++ it("queues each project's stored complete summaries once per batch, 100 traces per job", async () => {+ const rows = [+ ...Array.from({ length: 101 }, (_, index) =>+ summary(`trace-${index}`, [1, 0]),+ ),+ summary("skipped", [1, 0], { state: "not_applicable" }),+ summary("other", [1, 0], { projectId: "other-project" }),+ ];++ await enqueueTopicsAutomation("batch", rows);+ await enqueueTopicsAutomation("batch", rows);++ const jobs = add.mock.calls.map(([, data, options]) => ({+ jobId: options.jobId,+ projectId: data.payload.projectId,+ traceCount: data.payload.traceIds.length,+ }));+ expect(jobs.slice(0, 3)).toEqual([+ { jobId: expect.any(String), projectId: "project", traceCount: 100 },+ { jobId: expect.any(String), projectId: "project", traceCount: 1 },+ { jobId: expect.any(String), projectId: "other-project", traceCount: 1 },+ ]);+ // A retried batch reuses its job IDs, so BullMQ queues nothing twice.+ expect(jobs.slice(3)).toEqual(jobs.slice(0, 3));++ // A retry that stores a facet the first attempt failed queues a new job.+ add.mockClear();+ await enqueueTopicsAutomation("batch", [+ summary("other", [1, 0], { projectId: "other-project" }),+ summary("other", [1, 0], { projectId: "other-project", facetId: "b" }),+ ]);+ expect(add.mock.calls[0][2].jobId).not.toBe(jobs[2].jobId);+ expect(TopicsAutomationQueue.getInstance).toHaveBeenCalled();+ });++ it("assigns not-yet-assigned complete summaries onto the compatible serving map", async () => {+ vi.mocked(listTopicSummaries).mockResolvedValue([+ summary("near", [1, 0.01]),+ summary("far", [0, 1]),+ summary("assigned", [1, 0]),+ summary("old-space", [1, 0], { embeddingModel: "other" }),+ ]);+ vi.mocked(readTopicAssignments).mockResolvedValue([+ {+ traceId: "assigned",+ summaryProcessedAt: "2026-10-14T00:01:00.000Z",+ } as TopicAssignment,+ ]);+ vi.mocked(getTopicProcessingMapIds).mockResolvedValue([+ { facetId: "facet", facetVersion: 2, runId: "run" },+ ]);+ vi.mocked(getTopicRun).mockResolvedValue(run);++ await processTopicsAutomation(batch);++ expect(+ vi+ .mocked(writeTopicAssignments)+ .mock.calls[0][0].map(({ traceId, topicId, origin }) => ({+ traceId,+ topicId,+ origin,+ })),+ ).toEqual([+ { traceId: "near", topicId: "billing", origin: "online" },+ { traceId: "far", topicId: null, origin: "online" },+ ]);+ expect(createTopicExecution).not.toHaveBeenCalled();+ });++ it("starts one first discovery per facet version once enough summaries exist", async () => {+ vi.mocked(listTopicSummaries).mockResolvedValue([summary("near", [1, 0])]);+ vi.mocked(getTopicProcessingMapIds).mockResolvedValue([+ { facetId: "facet", facetVersion: 2, runId: null },+ ]);+ vi.mocked(getTopicSummaryCounts).mockResolvedValue([+ { facetId: "facet", facetVersion: 2, count: 99 },+ ]);++ await processTopicsAutomation(batch);+ expect(createTopicExecution).not.toHaveBeenCalled();++ vi.mocked(getTopicSummaryCounts).mockResolvedValue([+ { facetId: "facet", facetVersion: 2, count: 100 },+ ]);+ await processTopicsAutomation(batch);+ expect(createTopicExecution).toHaveBeenCalledWith(+ expect.objectContaining({+ projectId: "project",+ operation: "update",+ facets: [{ facetId: "facet", version: 2 }],+ embeddingConfig: { embeddingModel: "embed", embeddingDimensions: 2 },+ exploratory: false,+ }),+ expect.any(String),+ "system:topics",+ );+ expect(enqueueTopicExecution).toHaveBeenCalledWith("project", "execution");++ // Started before: a queued one is queued again, a failed one is left for Resume.+ vi.mocked(createTopicExecution).mockClear();+ vi.mocked(enqueueTopicExecution).mockClear();+ vi.mocked(readTopicExecutionForRequest)+ .mockResolvedValueOnce({ id: "queued", status: "queued" } as Awaited<+ ReturnType<typeof readTopicExecutionForRequest>+ >)+ .mockResolvedValueOnce({ id: "failed", status: "failed" } as Awaited<+ ReturnType<typeof readTopicExecutionForRequest>+ >);+ await processTopicsAutomation(batch);+ await processTopicsAutomation(batch);+ expect(vi.mocked(enqueueTopicExecution).mock.calls).toEqual([+ ["project", "queued"],+ ]);+ vi.mocked(prisma.topicClusteringRun.findFirst).mockResolvedValueOnce({+ id: "incompatible",+ } as never);+ await processTopicsAutomation(batch);+ expect(createTopicExecution).not.toHaveBeenCalled();+ });++ it("does not start discovery once automatic processing is off or the embedding model changed", async () => {+ vi.mocked(listTopicSummaries).mockResolvedValue([summary("near", [1, 0])]);+ vi.mocked(getTopicProcessingMapIds).mockResolvedValue([+ { facetId: "facet", facetVersion: 2, runId: null },+ ]);+ vi.mocked(getTopicSummaryCounts).mockResolvedValue([+ { facetId: "facet", facetVersion: 2, count: 100 },+ ]);+ vi.mocked(getEnabledTopicsModels)+ .mockResolvedValueOnce(new Map())+ .mockResolvedValueOnce(+ new Map([+ ["project", { embedding: { model: "new-embed", dimensions: 2 } }],+ ]) as Awaited<ReturnType<typeof getEnabledTopicsModels>>,+ );++ await processTopicsAutomation(batch);+ await processTopicsAutomation(batch);++ expect(createTopicExecution).not.toHaveBeenCalled();+ });+});diff --git a/worker/src/features/topics/topicsAutomation.ts b/worker/src/features/topics/topicsAutomation.tsnew file mode 100644index 0000000000..f196560e8d--- /dev/null+++ b/worker/src/features/topics/topicsAutomation.ts@@ -0,0 +1,293 @@+import { createHash } from "node:crypto";+import { prisma } from "@langfuse/shared/src/db";+import { InvalidRequestError } from "@langfuse/shared";+import {+ logger,+ QueueJobs,+ recordIncrement,+ type QueueName,+ type TQueueJobTypes,+} from "@langfuse/shared/src/server";+import {+ createTopicExecution,+ enqueueTopicExecution,+ getTopicProcessingMapIds,+ getTopicRun,+ getTopicSummaryCounts,+ getEnabledTopicsModels,+ listTopicSummaries,+ readTopicAssignments,+ readTopicExecutionForRequest,+ TOPICS_SYSTEM_USER_ID,+ TOPICS_TRACE_BATCH_SIZE,+ TopicsAutomationQueue,+ writeTopicAssignments,+} from "@langfuse/shared/topics/server";+import type {+ TopicEmbeddingConfig,+ TopicFacetRef,+ TopicRun,+ TopicSummary,+} from "@langfuse/shared/topics";+import { topicAssignmentRows } from "./assignments";+import { TopicMetrics } from "./metrics";+import { topicClusterSettings } from "./numeric";++type AutomationBatch = TQueueJobTypes[QueueName.TopicsAutomation]["payload"];++const DISCOVERY_WINDOW_MS = 7 * 24 * 60 * 60_000;+const hash = (value: unknown) =>+ createHash("sha256").update(JSON.stringify(value)).digest("hex");++/**+ * Queues assignment for the complete summaries a trace batch stored, one job per project,+ * vector space and 100 traces. Job IDs repeat for a retried batch, so it queues nothing twice.+ */+export async function enqueueTopicsAutomation(+ batchId: string,+ rows: TopicSummary[],+): Promise<void> {+ const queue = TopicsAutomationQueue.getInstance();+ if (!queue) return;+ for (const batch of groupStoredSummaries(rows).flatMap(splitTraces)) {+ // Facets belong in the ID: a retry can store a facet the first attempt failed.+ const id = hash([+ batchId,+ batch.projectId,+ batch.embeddingConfig,+ batch.facets,+ batch.traceIds,+ ]);+ await queue.add(+ QueueJobs.TopicsAutomation,+ {+ id,+ timestamp: new Date(),+ name: QueueJobs.TopicsAutomation,+ payload: batch,+ },+ { jobId: id },+ );+ }+}++/**+ * Assigns the batch's current complete summaries to each facet's compatible serving map,+ * skipping ones already assigned, or starts the facet's first discovery once enough exist.+ */+export async function processTopicsAutomation(+ batch: AutomationBatch,+): Promise<void> {+ const { projectId, embeddingConfig } = batch;+ for (const facet of batch.facets) {+ const summaries = await currentSummaries(batch, facet);+ if (summaries.length === 0) continue;+ const [map] = await getTopicProcessingMapIds(+ projectId,+ [facet],+ embeddingConfig,+ );+ const run = map?.runId ? await getTopicRun(projectId, map.runId) : null;+ if (run?.status === "completed") await assign(batch, facet, summaries, run);+ else await startDiscovery(projectId, facet, embeddingConfig);+ }+}++/** One batch per project and vector space, spanning its summaries' unit start times. */+function groupStoredSummaries(rows: TopicSummary[]): AutomationBatch[] {+ const groups = new Map<string, AutomationBatch>();+ for (const row of rows) {+ if (row.state !== "complete" || row.traceId === null) continue;+ const embeddingConfig = {+ embeddingModel: row.embeddingModel,+ embeddingDimensions: row.embedding.length,+ };+ const key = JSON.stringify([row.projectId, embeddingConfig]);+ const start = new Date(row.unitStartTime);+ const end = new Date(start.getTime() + 1);+ const group = groups.get(key) ?? {+ projectId: row.projectId,+ embeddingConfig,+ timeRange: { from: start, to: end },+ traceIds: [],+ facets: [],+ };+ if (start < group.timeRange.from) group.timeRange.from = start;+ if (end > group.timeRange.to) group.timeRange.to = end;+ if (!group.traceIds.includes(row.traceId)) group.traceIds.push(row.traceId);+ if (+ !group.facets.some(+ (facet) =>+ facet.facetId === row.facetId && facet.version === row.facetVersion,+ )+ )+ group.facets.push({ facetId: row.facetId, version: row.facetVersion });+ groups.set(key, group);+ }+ return [...groups.values()].map((group) => ({+ ...group,+ facets: group.facets.sort(+ (a, b) => a.facetId.localeCompare(b.facetId) || a.version - b.version,+ ),+ }));+}++function splitTraces(batch: AutomationBatch): AutomationBatch[] {+ const batches: AutomationBatch[] = [];+ for (+ let index = 0;+ index < batch.traceIds.length;+ index += TOPICS_TRACE_BATCH_SIZE+ )+ batches.push({+ ...batch,+ traceIds: batch.traceIds.slice(index, index + TOPICS_TRACE_BATCH_SIZE),+ });+ return batches;+}++async function currentSummaries(batch: AutomationBatch, facet: TopicFacetRef) {+ const { embeddingConfig } = batch;+ const rows = await listTopicSummaries(+ batch.projectId,+ {+ traceIds: batch.traceIds,+ facetId: facet.facetId,+ facetVersion: facet.version,+ },+ batch.timeRange,+ );+ return rows.filter(+ (row) =>+ row.state === "complete" &&+ row.traceId !== null &&+ row.embeddingModel === embeddingConfig.embeddingModel &&+ row.embedding.length === embeddingConfig.embeddingDimensions,+ );+}++async function assign(+ batch: AutomationBatch,+ facet: TopicFacetRef,+ summaries: TopicSummary[],+ run: TopicRun,+) {+ const metrics = new TopicMetrics();+ await metrics.measure("assignment", async () => {+ const assigned = new Set(+ (+ await readTopicAssignments(+ batch.projectId,+ facet,+ summaries,+ run.id,+ batch.timeRange,+ )+ ).map((row) => JSON.stringify([row.traceId, row.summaryProcessedAt])),+ );+ const pending = summaries.filter(+ (row) => !assigned.has(JSON.stringify([row.traceId, row.processedAt])),+ );+ if (pending.length === 0) return;+ const rows = topicAssignmentRows({+ projectId: batch.projectId,+ facet,+ embeddingConfig: batch.embeddingConfig,+ summaries: pending,+ run,+ origin: "online",+ assignedAt: new Date().toISOString(),+ });+ await writeTopicAssignments(rows);+ const topics = rows.filter((row) => row.topicId !== null).length;+ metrics.result("assignment", "assigned", topics);+ metrics.result("assignment", "outlier", rows.length - topics);+ });+}++/** One deterministic execution per facet version and vector space; History keeps its outcome. */+async function startDiscovery(+ projectId: string,+ facet: TopicFacetRef,+ embeddingConfig: TopicEmbeddingConfig,+) {+ if (!(await usesCurrentEmbedding(projectId, embeddingConfig))) return;+ const requestId = hash(["topics-discovery", facet, embeddingConfig]);+ const existing = await readTopicExecutionForRequest(+ projectId,+ requestId,+ requestId,+ );+ // Checked before the fit guard, which this execution's own pending run would trip.+ if (existing) {+ // Like the web trigger: a queued execution may have lost its job; failed ones resume from History.+ if (existing.status === "queued")+ await enqueueTopicExecution(projectId, existing.id);+ return;+ }+ if (await hasMapOrFit(projectId, facet)) return;+ const to = new Date();+ const timeRange = { from: new Date(to.getTime() - DISCOVERY_WINDOW_MS), to };+ const [counts] = await getTopicSummaryCounts(+ projectId,+ [facet],+ embeddingConfig,+ timeRange,+ );+ if (!counts || counts.count < topicClusterSettings(false).minimumCount)+ return;+ let execution;+ try {+ execution = await createTopicExecution(+ {+ projectId,+ requestId,+ operation: "update",+ facets: [facet],+ embeddingConfig,+ timeRange,+ exploratory: false,+ },+ requestId,+ TOPICS_SYSTEM_USER_ID,+ );+ } catch (error) {+ // A concurrent job created it first with its own time range.+ if (error instanceof InvalidRequestError) return;+ throw error;+ }+ await enqueueTopicExecution(projectId, execution.id);+ recordIncrement("langfuse.topics.automatic_discoveries", 1);+ logger.info("Started automatic Topics discovery", {+ projectId,+ facetId: facet.facetId,+ facetVersion: facet.version,+ });+}++/** Automatic processing is on with the embedding model these summaries used. */+async function usesCurrentEmbedding(+ projectId: string,+ embeddingConfig: TopicEmbeddingConfig,+) {+ const models = (await getEnabledTopicsModels([projectId])).get(projectId);+ return (+ models !== undefined &&+ models.embedding.model === embeddingConfig.embeddingModel &&+ models.embedding.dimensions === embeddingConfig.embeddingDimensions+ );+}++/** A map or fit, finished or in progress, exists for this or a newer facet version. */+async function hasMapOrFit(projectId: string, facet: TopicFacetRef) {+ const run = await prisma.topicClusteringRun.findFirst({+ where: {+ projectId,+ facetId: facet.facetId,+ facetVersion: { gte: facet.version },+ status: { in: ["pending", "running", "completed"] },+ },+ select: { id: true },+ });+ return run !== null;+}diff --git a/worker/src/queues/__tests__/traceBatchQueue.test.ts b/worker/src/queues/__tests__/traceBatchQueue.test.tsindex 13b3dff61f..fff55c5bd1 100644--- a/worker/src/queues/__tests__/traceBatchQueue.test.ts+++ b/worker/src/queues/__tests__/traceBatchQueue.test.ts@@ -30,6 +30,7 @@ import { tokenCountAsync } from "../../features/tokenisation/async-usage";import { tokenCount } from "../../features/tokenisation/usage";import { TopicsProviderUnavailable } from "../../features/topics/provider-error";import { summarizeAssembledTrace } from "../../features/topics/summarizeAssembledTrace";+import { enqueueTopicsAutomation } from "../../features/topics/topicsAutomation";import {getEnabledTopicsModels,listTopicFacets,@@ -67,6 +68,10 @@ vi.mock("../../features/topics/summarizeAssembledTrace", () => ({})),}));+vi.mock("../../features/topics/topicsAutomation", () => ({+ enqueueTopicsAutomation: vi.fn(async () => {}),+}));+vi.mock("@langfuse/shared/topics/server", () => ({isTopicsProjectEnabled: vi.fn(() => true),getEnabledTopicsModels: vi.fn(@@ -820,6 +825,12 @@ describe("trace batch queue", () => {.mocked(writeTopicSummaries).mock.lastCall![0].map((row) => row.projectId),).toEqual(["a", "b"]);+ // Only stored rows are queued for assignment.+ expect(+ vi+ .mocked(enqueueTopicsAutomation)+ .mock.lastCall![1].map((row) => row.projectId),+ ).toEqual(["a", "b"]);// Like ClickhouseWriter, an outage drops the insert once instead of splitting it.vi.mocked(recordIncrement).mockClear();@@ -834,6 +845,7 @@ describe("trace batch queue", () => {2,{ outcome: "failed", reason: "storage" },);+ expect(vi.mocked(enqueueTopicsAutomation).mock.lastCall![1]).toEqual([]);vi.mocked(writeTopicSummaries).mockReset();vi.mocked(summarizeAssembledTrace).mockImplementation(async () => ({outcome: "unchanged",@@ -841,7 +853,7 @@ describe("trace batch queue", () => {}));});- it("stores completed traces' rows before rethrowing a failed stream, so a retry skips them", async () => {+ it("stores and queues completed traces' rows before rethrowing a failed stream, so a retry skips them", async () => {const failure = new Error("query failed");const event = (projectId: string) => ({project_id: projectId,@@ -898,6 +910,73 @@ describe("trace batch queue", () => {expect(vi.mocked(writeTopicSummaries).mock.calls[0][0]).toEqual([{ projectId: "a", state: "complete" },]);+ // A retry skips stored rows, so they are queued before the failure rethrows.+ expect(enqueueTopicsAutomation).toHaveBeenCalledWith("stream-failure", [+ { projectId: "a", state: "complete" },+ ]);+ vi.mocked(summarizeAssembledTrace).mockImplementation(async () => ({+ outcome: "unchanged",+ rows: [],+ }));+ });++ it("keeps the batch successful when Topics assignment cannot be queued", async () => {+ vi.mocked(getTraceBatchEventStream).mockImplementation(async function* () {+ yield {+ project_id: "a",+ trace_id: "trace",+ environment: "default",+ span_id: "span",+ parent_span_id: null,+ start_time: "2026-09-11 00:00:00.000000",+ event_ts: "2026-09-11 00:00:00.000000",+ type: "GENERATION",+ name: "generation",+ input: JSON.stringify([{ role: "user", content: "question" }]),+ output: JSON.stringify({ role: "assistant", content: "answer" }),+ metadata: {},+ level: "DEFAULT",+ status_message: null,+ tool_definitions: {},+ tool_calls: [],+ tool_call_names: [],+ };+ });+ vi.mocked(summarizeAssembledTrace).mockImplementation(async () => ({+ outcome: "summarized",+ rows: [{ projectId: "a", state: "complete" } as TopicSummary],+ }));+ vi.mocked(enqueueTopicsAutomation).mockRejectedValueOnce(+ new Error("Redis unavailable"),+ );+ const job = {+ data: {+ id: "enqueue-failure",+ name: QueueJobs.TraceBatch,+ timestamp: new Date(),+ payload: {+ traces: [+ {+ projectId: "a",+ traceId: "trace",+ minStart: 0,+ maxStart: 1,+ revision: "r",+ },+ ],+ },+ },+ } as Job<TQueueJobTypes[QueueName.TraceBatch]>;++ await expect(traceBatchQueueProcessor(job, undefined)).resolves.toEqual(+ expect.objectContaining({ traceCount: 1 }),+ );+ expect(writeTopicSummaries).toHaveBeenCalledOnce();+ expect(recordIncrement).toHaveBeenCalledWith(+ "langfuse.topics.trace_outcomes",+ 1,+ { outcome: "summarized" },+ );vi.mocked(summarizeAssembledTrace).mockImplementation(async () => ({outcome: "unchanged",rows: [],diff --git a/worker/src/queues/topicsAutomationQueue.ts b/worker/src/queues/topicsAutomationQueue.tsnew file mode 100644index 0000000000..d5d0a50c06--- /dev/null+++ b/worker/src/queues/topicsAutomationQueue.ts@@ -0,0 +1,17 @@+import { type Processor } from "bullmq";+import {+ TopicAutomationBatchSchema,+ type QueueName,+ type TQueueJobTypes,+} from "@langfuse/shared/src/server";+import { isTopicsProjectEnabled } from "@langfuse/shared/topics/server";+import { processTopicsAutomation } from "../features/topics/topicsAutomation";++export const topicsAutomationQueueProcessor: Processor<+ TQueueJobTypes[QueueName.TopicsAutomation]+> = async (job) => {+ const batch = TopicAutomationBatchSchema.parse(job.data.payload);+ // A disabled project's traces are caught up by its next Update topics.+ if (!isTopicsProjectEnabled(batch.projectId)) return;+ await processTopicsAutomation(batch);+};diff --git a/worker/src/queues/traceBatchQueue.ts b/worker/src/queues/traceBatchQueue.tsindex da72da5776..a35250d511 100644--- a/worker/src/queues/traceBatchQueue.ts+++ b/worker/src/queues/traceBatchQueue.ts@@ -39,6 +39,7 @@ import {import { TopicMetrics } from "../features/topics/metrics";import { isRetryableClickhouseInsertError } from "../services/ClickhouseWriter";import { summarizeAssembledTrace } from "../features/topics/summarizeAssembledTrace";+import { enqueueTopicsAutomation } from "../features/topics/topicsAutomation";import { recordTraceBatchTranscript } from "../features/traceBatching/traceBatchTranscript";type TraceBatchObservation = Observation & { eventTimestamp: string };@@ -484,6 +485,17 @@ export const traceBatchQueueProcessor: Processor<activeReads--;recordTraceBatchActiveReads();}+ // Stored summaries skip summarization on retry, so queue them before a failure rethrows.+ await enqueueTopicsAutomation(+ event.id,+ topicsOutcomes+ .flatMap(({ rows }) => rows)+ .filter((row) => !droppedRows.has(row)),+ ).catch((error: unknown) =>+ logger.warn("Topics assignment could not be queued for trace batch", {+ error: error instanceof Error ? error.message : String(error),+ }),+ );}if (summaryFailed) throw summaryError;