🪢 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(trace-batching): admit Topics traces only when Topics processing is active (#18705)
Lysander Kieseld03eed2cc3d5
diff --git a/packages/shared/AGENTS.md b/packages/shared/AGENTS.mdindex 0042823fef..212d41d7bb 100644--- a/packages/shared/AGENTS.md+++ b/packages/shared/AGENTS.md@@ -102,6 +102,12 @@exist. `setup.ts` tests the models, then commits the single Topics rule,models, and per-project trace idle time together. Refresh the Redis readinesssetting only after that transaction commits.+ - `processing-gate.ts`: `isTopicsProcessingActive`, the ingestion gate for+ trace batching (allowlisted, enabled with all three models, and an active+ rule with an unblocked facet), cached in Redis both ways. Any write to a+ project's Topics config, Topics rule or facet evaluators calls+ `invalidateTopicsProcessingActive` after it commits;+ `invalidateProjectEvalConfigCaches` already does.- `loadTopicTranscript`: shared in-memory source assembly for worker and inspector.Returns the shared `Transcript | null`, capped at 10,000 serialized characters.Historical reuse must match `TOPICS_TRANSCRIPT_VERSION`; accepted Redis resultsdiff --git a/packages/shared/src/server/evalJobConfigCache.ts b/packages/shared/src/server/evalJobConfigCache.tsindex d5cb1bf9ba..c3e1ea354a 100644--- a/packages/shared/src/server/evalJobConfigCache.ts+++ b/packages/shared/src/server/evalJobConfigCache.ts@@ -1,5 +1,6 @@import { logger } from "./logger";import { redis } from "./";+import { invalidateTopicsProcessingActive } from "./topics/processing-gate";/*** Redis cache utilities for eval job configuration optimization.@@ -90,10 +91,13 @@ export const clearNoEvalConfigsCache = async (}};-export const invalidateProjectEvalConfigCaches = (+/** Also clears the Topics processing gate, which reads the same rules. */+export const invalidateProjectEvalConfigCaches = async (projectId: string,-): Promise<[void, void]> =>- Promise.all([+): Promise<void> => {+ await Promise.all([clearNoEvalConfigsCache(projectId, "traceBased"),clearNoEvalConfigsCache(projectId, "eventBased"),+ invalidateTopicsProcessingActive(projectId),]);+};diff --git a/packages/shared/src/server/topics/index.ts b/packages/shared/src/server/topics/index.tsindex f43942a76d..068bd2a264 100644--- a/packages/shared/src/server/topics/index.ts+++ b/packages/shared/src/server/topics/index.ts@@ -7,6 +7,7 @@ export { emitTopicStagingMetrics } from "./staging-metrics";export * from "./embeddings";export * from "./text";export * from "./config";+export * from "./processing-gate";export * from "./model-config";export * from "./setup";export * from "./trace-input";diff --git a/packages/shared/src/server/topics/model-config.ts b/packages/shared/src/server/topics/model-config.tsindex bfd6f77282..5a9035220f 100644--- a/packages/shared/src/server/topics/model-config.ts+++ b/packages/shared/src/server/topics/model-config.ts@@ -20,6 +20,7 @@ import {} from "../../topics";import { hasTopicEmbeddings } from "./clickhouse";import { generateTopicEmbedding } from "./embeddings";+import { invalidateTopicsProcessingActive } from "./processing-gate";import { z } from "zod";export type TopicsModel = {@@ -140,9 +141,14 @@ export async function pauseTopicsModels(blockMessage: block.blockMessage,},});+ await invalidateTopicsProcessingActive(projectId);}-/** Pauses Topics in every config that uses a connection about to be deleted. */+/**+ * Pauses Topics in every config that uses a connection about to be deleted.+ * Callers that pass a transaction call `invalidateTopicsProcessingActive`+ * after it commits.+ */export async function pauseTopicsModelsUsingConnection(params: { projectId: string; llmApiKeyId: string; provider: string },tx: Prisma.TransactionClient = prisma,diff --git a/packages/shared/src/server/topics/postgres.ts b/packages/shared/src/server/topics/postgres.tsindex 105784bbc2..64046aec24 100644--- a/packages/shared/src/server/topics/postgres.ts+++ b/packages/shared/src/server/topics/postgres.ts@@ -16,6 +16,10 @@ import { isEqual } from "lodash";import { getTopicDefinitions, writeTopicDefinitions } from "./clickhouse";import { InvalidRequestError } from "../../errors";import { EvalTargetObject } from "../../features/evals/types";+import {+ invalidateTopicsProcessingActive,+ topicRuleWhere,+} from "./processing-gate";type FacetVersionRow = Prisma.EvaluatorVersionGetPayload<object>;type FacetRow = Prisma.EvaluatorGetPayload<{ include: { versions: true } }>;@@ -175,17 +179,6 @@ const topicRule = (row: RuleRow): TopicRule => ({updatedAt: row.updatedAt.toISOString(),});-function topicRuleWhere(projectId: string): Prisma.EvaluationRuleWhereInput {- return {- projectId,- targetObject: EvalTargetObject.TRACE,- assignments: {- some: {},- every: { projectId, evaluator: { projectId, type: "FACET" } },- },- };-}-export async function listTopicRules(projectId: string): Promise<TopicRule[]> {return (await prisma.evaluationRule.findMany({@@ -232,7 +225,9 @@ export type TopicRuleInput = Omit<};export async function saveTopicRule(input: TopicRuleInput): Promise<TopicRule> {- return prisma.$transaction((tx) => writeTopicRule(tx, input));+ const rule = await prisma.$transaction((tx) => writeTopicRule(tx, input));+ await invalidateTopicsProcessingActive(input.projectId);+ return rule;}/** Writes the project's rule inside the caller's transaction, under its lock. */diff --git a/packages/shared/src/server/topics/processing-gate.test.ts b/packages/shared/src/server/topics/processing-gate.test.tsnew file mode 100644index 0000000000..184ef6f4dc--- /dev/null+++ b/packages/shared/src/server/topics/processing-gate.test.ts@@ -0,0 +1,114 @@+import { beforeEach, describe, expect, it, vi } from "vitest";+import {+ invalidateTopicsProcessingActive,+ isTopicsProcessingActive,+} from "./processing-gate";++const mocks = vi.hoisted(() => ({+ allowlisted: vi.fn(),+ configFind: vi.fn(),+ ruleFind: vi.fn(),+ redis: { get: vi.fn(), set: vi.fn(), del: vi.fn() },+}));+vi.mock("./config", () => ({ isTopicsProjectEnabled: mocks.allowlisted }));+vi.mock("../redis/redis", () => ({ redis: mocks.redis }));+vi.mock("../../db", () => ({+ JobConfigState: { ACTIVE: "ACTIVE" },+ prisma: {+ topicConfig: { findFirst: mocks.configFind },+ evaluationRule: { findFirst: mocks.ruleFind },+ },+}));++const key = "langfuse:topics:processing-active:project-a";++describe("isTopicsProcessingActive", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ mocks.allowlisted.mockReturnValue(true);+ mocks.redis.get.mockResolvedValue(null);+ mocks.configFind.mockResolvedValue({ id: "config-a" });+ mocks.ruleFind.mockResolvedValue({ id: "rule-a" });+ });++ it("skips Redis and Postgres for projects outside the allowlist", async () => {+ mocks.allowlisted.mockReturnValue(false);++ await expect(isTopicsProcessingActive("project-a")).resolves.toBe(false);+ expect(mocks.redis.get).not.toHaveBeenCalled();+ expect(mocks.configFind).not.toHaveBeenCalled();+ });++ it.each([+ ["1", true],+ ["0", false],+ ])("returns the cached answer %s without Postgres", async (cached, want) => {+ mocks.redis.get.mockResolvedValue(cached);++ await expect(isTopicsProcessingActive("project-a")).resolves.toBe(want);+ expect(mocks.configFind).not.toHaveBeenCalled();+ expect(mocks.ruleFind).not.toHaveBeenCalled();+ });++ it("requires an enabled, fully configured Topics config and an active facet rule", async () => {+ await expect(isTopicsProcessingActive("project-a")).resolves.toBe(true);++ expect(mocks.configFind.mock.calls[0][0].where).toMatchObject({+ projectId: "project-a",+ enabled: true,+ summaryModel: { not: null },+ embeddingLlmApiKeyId: { not: null },+ clusteringModel: { not: null },+ });+ expect(mocks.ruleFind.mock.calls[0][0].where.AND[1]).toEqual({+ status: "ACTIVE",+ assignments: {+ some: { evaluator: { type: "FACET", blockedAt: null } },+ },+ });+ expect(mocks.redis.set).toHaveBeenCalledWith(key, "1", "EX", 300);+ });++ it.each([+ ["Topics is disabled or a model is missing", "config"],+ ["no active facet rule exists", "rule"],+ ])("caches a negative answer when %s", async (_case, missing) => {+ (missing === "config"+ ? mocks.configFind+ : mocks.ruleFind+ ).mockResolvedValue(null);++ await expect(isTopicsProcessingActive("project-a")).resolves.toBe(false);+ expect(mocks.redis.set).toHaveBeenCalledWith(key, "0", "EX", 300);+ });++ it("propagates a Postgres error and caches the allowlist answer briefly", async () => {+ mocks.ruleFind.mockRejectedValue(new Error("postgres down"));++ await expect(isTopicsProcessingActive("project-a")).rejects.toThrow(+ "postgres down",+ );+ expect(mocks.redis.set).toHaveBeenCalledWith(key, "1", "EX", 30);+ });++ it("stops waiting on a hung Postgres lookup after the deadline", async () => {+ vi.useFakeTimers();+ try {+ mocks.configFind.mockReturnValue(new Promise(() => {}));+ const lookup = isTopicsProcessingActive("project-a");+ const assertion = expect(lookup).rejects.toThrow("timed out");++ await vi.advanceTimersByTimeAsync(2_000);+ await assertion;+ expect(mocks.redis.set).toHaveBeenCalledWith(key, "1", "EX", 30);+ } finally {+ vi.useRealTimers();+ }+ });++ it("invalidation deletes the cached answer", async () => {+ await invalidateTopicsProcessingActive("project-a");++ expect(mocks.redis.del).toHaveBeenCalledWith(key);+ });+});diff --git a/packages/shared/src/server/topics/processing-gate.ts b/packages/shared/src/server/topics/processing-gate.tsnew file mode 100644index 0000000000..f595255f18--- /dev/null+++ b/packages/shared/src/server/topics/processing-gate.ts@@ -0,0 +1,155 @@+import { JobConfigState, prisma, type Prisma } from "../../db";+import { EvalTargetObject } from "../../features/evals/types";+import { logger } from "../logger";+import { redis } from "../redis/redis";+import { isTopicsProjectEnabled } from "./config";++// Saves and pauses delete the key; the TTL bounds a missed delete or a fill+// that read Postgres before a concurrent save committed.+const CACHE_TTL_SECONDS = 300;+// Ingestion awaits this lookup, so a slow or unreachable Postgres must not hold it.+const LOOKUP_TIMEOUT_MS = 2_000;+// After a failed lookup the allowlist answer is cached briefly, so an outage+// costs one slow lookup per project per window instead of one per batch.+const FAILURE_TTL_SECONDS = 30;+const CACHE_KEY_PREFIX = "langfuse:topics:processing-active";++const cacheKey = (projectId: string) => `${CACHE_KEY_PREFIX}:${projectId}`;++/** The project's Topics rule: a trace rule whose evaluators are all facets. */+export function topicRuleWhere(+ projectId: string,+): Prisma.EvaluationRuleWhereInput {+ return {+ projectId,+ targetObject: EvalTargetObject.TRACE,+ assignments: {+ some: {},+ every: { projectId, evaluator: { projectId, type: "FACET" } },+ },+ };+}++/**+ * Whether new traces of the project enter trace-batch processing for Topics:+ * the project is allowlisted, Topics is on with all three models configured,+ * and an active rule runs at least one facet that is not blocked. Both answers+ * are cached in Redis. A failed or timed-out lookup caches the allowlist answer+ * briefly and propagates.+ */+export async function isTopicsProcessingActive(+ projectId: string,+): Promise<boolean> {+ if (!isTopicsProjectEnabled(projectId)) return false;+ const cached = await readCache(projectId);+ if (cached !== null) return cached;+ let active: boolean;+ try {+ active = await withTimeout(lookupProcessingActive(projectId));+ } catch (error) {+ await writeCache(projectId, true, FAILURE_TTL_SECONDS);+ throw error;+ }+ await writeCache(projectId, active, CACHE_TTL_SECONDS);+ return active;+}++/** Call after a committed change to the project's Topics config or rule. */+export async function invalidateTopicsProcessingActive(+ projectId: string,+): Promise<void> {+ try {+ await redis?.del(cacheKey(projectId));+ } catch (error) {+ logger.warn("Failed to invalidate Topics processing state", {+ projectId,+ error,+ });+ }+}++async function readCache(projectId: string): Promise<boolean | null> {+ try {+ const cached = await redis?.get(cacheKey(projectId));+ if (cached === "1") return true;+ if (cached === "0") return false;+ return null;+ } catch (error) {+ logger.warn("Failed to read cached Topics processing state", {+ projectId,+ error,+ });+ return null;+ }+}++async function lookupProcessingActive(projectId: string): Promise<boolean> {+ const [config, rule] = await Promise.all([+ findActiveTopicsConfig(projectId),+ findActiveTopicRule(projectId),+ ]);+ return Boolean(config && rule);+}++function findActiveTopicsConfig(projectId: string) {+ return prisma.topicConfig.findFirst({+ where: {+ projectId,+ enabled: true,+ summaryLlmApiKeyId: { not: null },+ summaryModel: { not: null },+ embeddingLlmApiKeyId: { not: null },+ embeddingModel: { not: null },+ clusteringLlmApiKeyId: { not: null },+ clusteringModel: { not: null },+ },+ select: { id: true },+ });+}++function findActiveTopicRule(projectId: string) {+ return prisma.evaluationRule.findFirst({+ where: {+ AND: [+ topicRuleWhere(projectId),+ {+ status: JobConfigState.ACTIVE,+ assignments: {+ some: { evaluator: { type: "FACET", blockedAt: null } },+ },+ },+ ],+ },+ select: { id: true },+ });+}++async function withTimeout<T>(lookup: Promise<T>): Promise<T> {+ let timer: NodeJS.Timeout | undefined;+ const deadline = new Promise<never>((_, reject) => {+ timer = setTimeout(+ () => reject(new Error("Topics processing state lookup timed out")),+ LOOKUP_TIMEOUT_MS,+ );+ });+ try {+ return await Promise.race([lookup, deadline]);+ } finally {+ clearTimeout(timer);+ }+}++async function writeCache(+ projectId: string,+ active: boolean,+ ttlSeconds: number,+): Promise<void> {+ try {+ await redis?.set(cacheKey(projectId), active ? "1" : "0", "EX", ttlSeconds);+ } catch (error) {+ logger.warn("Failed to cache Topics processing state", {+ projectId,+ error,+ });+ }+}diff --git a/packages/shared/src/server/topics/setup.ts b/packages/shared/src/server/topics/setup.tsindex 7e5e307016..4fba401402 100644--- a/packages/shared/src/server/topics/setup.ts+++ b/packages/shared/src/server/topics/setup.ts@@ -15,6 +15,7 @@ import {writeTopicRule,type TopicRuleInput,} from "./postgres";+import { invalidateTopicsProcessingActive } from "./processing-gate";/** One Topics setup: models, the four built-in facets, and a single rule. */export async function saveTopicsSetup(@@ -85,5 +86,6 @@ export async function saveTopicRuleAndModels(return saved;});await cacheTracingReadinessSettings(rule.projectId, { traceIdleMs });+ await invalidateTopicsProcessingActive(rule.projectId);return saved;}diff --git a/web/src/features/llm-api-key/server/router.ts b/web/src/features/llm-api-key/server/router.tsindex 60754d28b6..81bf910f0c 100644--- a/web/src/features/llm-api-key/server/router.ts+++ b/web/src/features/llm-api-key/server/router.ts@@ -51,7 +51,10 @@ import {validateLlmConnectionBaseURL,} from "@langfuse/shared/src/server";import { env } from "@/src/env.mjs";-import { pauseTopicsModelsUsingConnection } from "@langfuse/shared/topics/server";+import {+ invalidateTopicsProcessingActive,+ pauseTopicsModelsUsingConnection,+} from "@langfuse/shared/topics/server";import { TRPCError } from "@trpc/server";export function getDisplaySecretKey(secretKey: string) {@@ -490,6 +493,7 @@ export const llmApiKeyRouter = createTRPCRouter({throw error;}+ await invalidateTopicsProcessingActive(input.projectId);await finalizeEvaluatorBlocks({projectId: input.projectId,source: EvaluatorBlockSource.LLM_API_KEY_DELETION,diff --git a/web/src/pages/api/public/llm-connections/[id].ts b/web/src/pages/api/public/llm-connections/[id].tsindex b0352aa22a..65cc606ea7 100644--- a/web/src/pages/api/public/llm-connections/[id].ts+++ b/web/src/pages/api/public/llm-connections/[id].ts@@ -6,7 +6,10 @@ import {DeleteLlmConnectionV1Response,} from "@/src/features/public-api/types/llm-connections";import { auditLog } from "@/src/features/audit-logs/server";-import { pauseTopicsModelsUsingConnection } from "@langfuse/shared/topics/server";+import {+ invalidateTopicsProcessingActive,+ pauseTopicsModelsUsingConnection,+} from "@langfuse/shared/topics/server";import { EvaluatorBlockReason, LangfuseNotFoundError } from "@langfuse/shared";import {blockEvaluatorsUsingDefaultModel,@@ -82,6 +85,7 @@ export default withMiddlewares({return { providerBlock, defaultModelBlock };});+ await invalidateTopicsProcessingActive(projectId);await finalizeEvaluatorBlocks({projectId,source: EvaluatorBlockSource.LLM_API_KEY_DELETION,diff --git a/worker/src/__tests__/traceBatching.test.ts b/worker/src/__tests__/traceBatching.test.tsindex b3cf129b78..2e1cf38fd7 100644--- a/worker/src/__tests__/traceBatching.test.ts+++ b/worker/src/__tests__/traceBatching.test.ts@@ -46,6 +46,7 @@ import { traceBatchQueueProcessor } from "../queues/traceBatchQueue";import { ClickhouseWriter } from "../services/ClickhouseWriter";import {ensureDefaultTopicFacets,+ isTopicsProcessingActive,listTopicSummaries,type TopicsModels,} from "@langfuse/shared/topics/server";@@ -74,6 +75,9 @@ const topicsProjects = vi.hoisted(() => new Set<string>());vi.mock("@langfuse/shared/topics/server", async (importOriginal) => ({...(await importOriginal<typeof import("@langfuse/shared/topics/server")>()),isTopicsProjectEnabled: (projectId: string) => topicsProjects.has(projectId),+ isTopicsProcessingActive: vi.fn(async (projectId: string) =>+ topicsProjects.has(projectId),+ ),getEnabledTopicsModels: async (projectIds: string[]) =>new Map(projectIds@@ -1749,14 +1753,33 @@ describe("trace micro-batch scheduling with Redis", () => {const samplingRate = env.LANGFUSE_TRACE_BATCH_SAMPLING_RATE;env.LANGFUSE_TRACE_BATCH_SAMPLING_RATE = 0;topicsProjects.add("topics-project");+ topicsProjects.add("inactive-project");try {vi.mocked(recordIncrement).mockClear();await trackTraceBatchActivity("other-project", [event("other-trace")]);+ vi.mocked(isTopicsProcessingActive).mockResolvedValueOnce(false);+ await trackTraceBatchActivity("inactive-project", [+ event("inactive-trace"),+ ]);expect(recordIncrement).not.toHaveBeenCalled();await trackTraceBatchActivity("topics-project", [event("topics-trace")]);- expect(await client().hkeys(stateKey)).toEqual([- member("topics-project", "topics-trace"),+ // An unreadable gate falls back to the allowlist.+ vi.mocked(isTopicsProcessingActive).mockRejectedValueOnce(+ new Error("postgres down"),+ );+ await trackTraceBatchActivity("topics-project", [+ event("fallback-trace"),]);+ expect(recordIncrement).toHaveBeenCalledWith(+ "langfuse.trace_batch.topics_gate_failures",+ 1,+ );+ expect((await client().hkeys(stateKey)).sort()).toEqual(+ [+ member("topics-project", "topics-trace"),+ member("topics-project", "fallback-trace"),+ ].sort(),+ );} finally {topicsProjects.clear();env.LANGFUSE_TRACE_BATCH_SAMPLING_RATE = samplingRate;diff --git a/worker/src/features/traceBatching/README.md b/worker/src/features/traceBatching/README.mdindex 95662d8a69..e414f1421c 100644--- a/worker/src/features/traceBatching/README.md+++ b/worker/src/features/traceBatching/README.md@@ -37,7 +37,7 @@ templates. The local dev template sets the idle time below.| `QUEUE_CONSUMER_TRACE_BATCH_QUEUE_IS_ENABLED` | `false` | Register the batch worker. || `LANGFUSE_TRACE_BATCH_READ_ENABLED` | `false` | Allow the worker to query ClickHouse; otherwise discard jobs. || `LANGFUSE_TRACE_BATCH_TRANSCRIPT_METRICS_ENABLED` | `false` | Emit transcript size distributions. Span attributes and failure counters stay on either way. |-| `LANGFUSE_TRACE_BATCH_SAMPLING_RATE` | `0` | Fraction of other projects admitted by ingestion, from 0 to 1. Unset or `0` is off. Topics-enabled projects are always admitted. |+| `LANGFUSE_TRACE_BATCH_SAMPLING_RATE` | `0` | Fraction of other projects admitted by ingestion, from 0 to 1. Unset or `0` is off. Topics-active projects are always admitted. || `LANGFUSE_TRACE_BATCH_STRATEGY` | `project` | Choose project-order packing or opt-in locality grouping. || `LANGFUSE_TRACE_BATCH_MAX_SIZE` | `60` | Maximum traces per job, up to 10,000. || `LANGFUSE_TRACE_BATCH_MAX_BATCH_EVENT_UPDATES` | `1500` | Estimated event updates per multi-trace job; `0` disables. |@@ -370,10 +370,16 @@ root I/O, observation markers and inline errors, excluding section headings andtool definitions. Keepnumerator and denominator on the same basis when calculating shares. Ingestionapplies `LANGFUSE_TRACE_BATCH_SAMPLING_RATE` by trace ID before the dispatcher-and worker; every admitted trace receives Topics measurements. Projects in the-Topics allowlist (`LANGFUSE_TOPICS_ENABLED_PROJECT_IDS`) bypass sampling so every-trace is summarized; a rate of `0` limits the flow to those projects and skips-all tracking work (no per-trace state, metrics or Redis calls) for the rest.+and worker; every admitted trace receives Topics measurements. Projects where+`isTopicsProcessingActive` holds (in the Topics allowlist+`LANGFUSE_TOPICS_ENABLED_PROJECT_IDS`, Topics enabled with all three models, and+an active rule with an unblocked facet) bypass sampling so every trace is+summarized; a rate of `0` limits the flow to those projects and skips all+per-trace tracking work (no per-trace state or metrics) for the rest. Projects+outside the allowlist make no Redis or Postgres calls; allowlisted ones read the+cached gate answer (5 minutes), and a miss queries Postgres with a 2-second+deadline. If that lookup fails or times out, ingestion falls back to the+allowlist, caches that answer for 30 seconds and increments `topics_gate_failures`.Transcript token counts use the existing local worker-thread pool and bundledtiktoken WASM, without a network or model API call. The `gpt-4o` configurationdiff --git a/worker/src/features/traceBatching/traceBatching.ts b/worker/src/features/traceBatching/traceBatching.tsindex 7961916c7d..85e57fe8b9 100644--- a/worker/src/features/traceBatching/traceBatching.ts+++ b/worker/src/features/traceBatching/traceBatching.ts@@ -13,7 +13,10 @@ import {type QueueName,type TQueueJobTypes,} from "@langfuse/shared/src/server";-import { isTopicsProjectEnabled } from "@langfuse/shared/topics/server";+import {+ isTopicsProcessingActive,+ isTopicsProjectEnabled,+} from "@langfuse/shared/topics/server";import { env } from "../../env";import { PeriodicExclusiveRunner } from "../../utils/PeriodicExclusiveRunner";import {@@ -817,8 +820,8 @@ export async function trackTraceBatchActivity()return;const samplingRate = env.LANGFUSE_TRACE_BATCH_SAMPLING_RATE;- // Topics summarizes every trace of an enabled project, so those bypass sampling.- const topicsProject = isTopicsProjectEnabled(projectId);+ // Topics summarizes every trace of an active project, so those bypass sampling.+ const topicsProject = await resolveTopicsProcessingActive(projectId);// Rate 0 admits only Topics projects; skip all tracking work for the rest.if (samplingRate === 0 && !topicsProject) return;@@ -976,6 +979,23 @@ export async function trackTraceBatchActivity(}}+async function resolveTopicsProcessingActive(+ projectId: string,+): Promise<boolean> {+ try {+ return await isTopicsProcessingActive(projectId);+ } catch (error) {+ // The allowlist keeps Topics traces during an outage; the consumer still+ // loads each project's models and skips projects without them.+ recordIncrement("langfuse.trace_batch.topics_gate_failures", 1);+ logger.warn("Failed to read Topics processing state, using allowlist", {+ projectId,+ error,+ });+ return isTopicsProjectEnabled(projectId);+ }+}+async function resolveTraceIdleMs(projectId: string): Promise<number> {let traceIdleMs: number | null = null;try {