Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

72.2 KB
Raw Blame
import {
  createEvent as createEventBase,
  createEventsCh,
  createOrgProjectAndApiKey,
  EventsQueryBuilder,
  queryClickhouse,
} from "@langfuse/shared/src/server";
import {
  makeAPICall,
  makeZodVerifiedAPICall,
} from "@/src/__tests__/test-utils";
import {
  GetObservationsV2Response,
  encodeCursor,
} from "@/src/features/public-api/types/observations";
import { randomUUID } from "crypto";
import { env } from "@/src/env.mjs";
import waitForExpect from "wait-for-expect";

let projectId: string;
let auth: string;

// The events tables carry metadata as flattened `metadata_names` /
// `metadata_values` arrays. The fixtures below also pass the nested object
// form for readability; it is not a column and is ignored by the insert.
const createEvent = (
  event: Parameters<typeof createEventBase>[0] & {
    metadata?: Record<string, unknown>;
  },
) => createEventBase(event);

const getObservations = (url: string) =>
  makeZodVerifiedAPICall(
    GetObservationsV2Response,
    "GET",
    url,
    undefined,
    auth,
  );

const getRaw = (url: string) => makeAPICall("GET", url, undefined, auth);

const maybe =
  env.LANGFUSE_MIGRATION_V4_ALLOW_PREVIEW_OPT_IN === "true"
    ? describe
    : describe.skip;

describe("/api/public/v2/observations API Endpoint", () => {
  beforeEach(async () => {
    const fixture = await createOrgProjectAndApiKey();
    projectId = fixture.projectId;
    auth = fixture.auth;
  });

  it("should kill redis connection", () => {
    // we need at least one test case to avoid hanging
    // redis connection when everything else is skipped.
  });

  it("rejects matches filters on v1 observations", async () => {
    const filterParam = JSON.stringify([
      {
        type: "string",
        column: "output",
        operator: "matches",
        value: "needle",
      },
    ]);

    const response = await getRaw(
      `/api/public/observations?useEventsTable=true&filter=${encodeURIComponent(filterParam)}`,
    );

    expect(response.status).toBe(400);
  });

  maybe("GET /api/public/v2/observations", () => {
    it("clamps Hobby observation access to the last 30 days", async () => {
      const fixture = await createOrgProjectAndApiKey({ plan: "Hobby" });
      const oldId = randomUUID();
      const recentId = randomUUID();
      const createObservationAt = (id: string, timestamp: number) =>
        createEvent({
          id,
          span_id: id,
          trace_id: randomUUID(),
          project_id: fixture.projectId,
          name: `data-access-${id}`,
          type: "SPAN",
          level: "DEFAULT",
          start_time: timestamp * 1000,
          end_time: timestamp * 1000 + 1_000,
        });
      await createEventsCh([
        createObservationAt(oldId, Date.now() - 100 * 24 * 60 * 60 * 1000),
        createObservationAt(recentId, Date.now() - 24 * 60 * 60 * 1000),
      ]);

      const response = await makeZodVerifiedAPICall(
        GetObservationsV2Response,
        "GET",
        "/api/public/v2/observations",
        undefined,
        fixture.auth,
      );

      expect(response.body.data.map((observation) => observation.id)).toContain(
        recentId,
      );
      expect(
        response.body.data.map((observation) => observation.id),
      ).not.toContain(oldId);
    });

    it("allows legacy v1 contains filters on IO", async () => {
      const filterParam = JSON.stringify([
        {
          type: "string",
          column: "output",
          operator: "contains",
          value: "needle",
        },
      ]);

      const response = await getRaw(
        `/api/public/observations?useEventsTable=true&filter=${encodeURIComponent(filterParam)}`,
      );

      expect(response.status).toBe(200);
    });

    it("should fetch observations with only requested field groups", async () => {
      const traceId = randomUUID();
      const observationId = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000; // microseconds for events table

      // Create observation
      const observation = createEvent({
        id: observationId,
        span_id: observationId,
        trace_id: traceId,
        project_id: projectId,
        parent_span_id: "external-parent",
        is_app_root: true,
        name: "test-observation",
        type: "GENERATION",
        level: "DEFAULT",
        start_time: timeValue,
        end_time: timeValue + 1000 * 1000,
        input: "What is the capital of France?",
        output: "The capital of France is Paris.",
        metadata: { source: "API" },
        metadata_names: ["source"],
        metadata_values: ["API"],
        provided_model_name: "gpt-4",
      });

      await createEventsCh([observation]);

      // Request only basic and trace context field groups (core is always included)
      const response = await getObservations(
        `/api/public/v2/observations?fields=basic,trace_context&traceId=${traceId}`,
      );

      expect(response.status).toBe(200);
      expect(response.body.data).toBeDefined();
      expect(response.body.meta).toBeDefined();
      expect(response.body.data.length).toBeGreaterThanOrEqual(1);

      // Find our created observation
      const createdObs = response.body.data.find(
        (obs: any) => obs.id === observationId,
      );
      expect(createdObs).toBeDefined();

      // Verify core fields are always present
      expect(createdObs?.id).toBe(observationId);
      expect(createdObs?.traceId).toBe(traceId);
      expect(createdObs?.type).toBe("GENERATION");
      expect(createdObs?.startTime).toBeDefined();
      expect(createdObs?.endTime).toBeDefined();
      expect(createdObs?.projectId).toBe(projectId);

      // Verify basic fields are present
      expect(createdObs?.name).toBe("test-observation");
      expect(createdObs?.level).toBe("DEFAULT");
      expect(createdObs?.traceName).toBe("test-observation");

      // Verify fields from non-requested groups are not present
      expect(createdObs?.input).toBeUndefined();
      expect(createdObs?.output).toBeUndefined();
      expect(createdObs?.metadata).toBeUndefined();
      expect(createdObs?.providedModelName).toBeUndefined();
    });

    it("should return input/output as strings by default (parseIoAsJson=false)", async () => {
      const traceId = randomUUID();
      const observationId = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      // Create observation with JSON input/output
      const jsonInput = JSON.stringify({ question: "What is 2+2?" });
      const jsonOutput = JSON.stringify({ answer: 4 });

      const observation = createEvent({
        id: observationId,
        span_id: observationId,
        trace_id: traceId,
        project_id: projectId,
        name: "test-observation",
        type: "GENERATION",
        level: "DEFAULT",
        start_time: timeValue,
        end_time: timeValue + 1000 * 1000,
        input: jsonInput,
        output: jsonOutput,
      });

      await createEventsCh([observation]);

      // Request with parseIoAsJson=false (default)
      const response = await getObservations(
        `/api/public/v2/observations?fields=io&traceId=${traceId}&parseIoAsJson=false`,
      );

      expect(response.status).toBe(200);
      const obs = response.body.data.find((o: any) => o.id === observationId);
      expect(obs).toBeDefined();

      // Input and output should be strings, not parsed objects
      expect(typeof obs?.input).toBe("string");
      expect(typeof obs?.output).toBe("string");
      expect(obs?.input).toBe(jsonInput);
      expect(obs?.output).toBe(jsonOutput);
    });

    it("should return 400 when parseIoAsJson=true", async () => {
      const response = await getRaw(
        `/api/public/v2/observations?fields=io&parseIoAsJson=true`,
      );

      expect(response.status).toBe(400);
      expect(response.body).toHaveProperty("error");
      expect(JSON.stringify(response.body)).toContain("parseIoAsJson");
    });

    it("returns core and basic fields when fields is omitted", async () => {
      const traceId = randomUUID();
      const observationId = randomUUID();

      await createEventsCh([
        createEvent({
          id: observationId,
          span_id: observationId,
          trace_id: traceId,
          project_id: projectId,
          name: "default-fields-observation",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: Date.now() * 1000,
        }),
      ]);

      const response = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}`,
      );

      expect(response.status).toBe(200);
      expect(response.body.data).toContainEqual(
        expect.objectContaining({
          id: observationId,
          name: "default-fields-observation",
          level: "DEFAULT",
          isRootObservation: true,
        }),
      );
    });

    it("should respect limit parameter with default of 50", async () => {
      const timestamp = Date.now() * 1000;
      const observations = Array.from({ length: 6 }, (_, index) => {
        const observationId = randomUUID();
        return createEvent({
          id: observationId,
          span_id: observationId,
          trace_id: randomUUID(),
          project_id: projectId,
          name: `limit-test-observation-${index}`,
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timestamp + index,
        });
      });

      await createEventsCh(observations);

      // Test default limit
      const response1 = await getObservations("/api/public/v2/observations");

      expect(response1.status).toBe(200);
      expect(response1.body.data.length).toBe(6);

      // Test custom limit
      const response2 = await getObservations(
        "/api/public/v2/observations?limit=5",
      );

      expect(response2.status).toBe(200);
      expect(response2.body.data.length).toBe(5);
    });

    it("should support standard filters (name, type, level, etc.)", async () => {
      const traceId = randomUUID();
      const observationId = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      // Create observation with specific attributes
      const observation = createEvent({
        id: observationId,
        span_id: observationId,
        trace_id: traceId,
        project_id: projectId,
        name: "unique-observation-name",
        type: "GENERATION",
        level: "WARNING",
        start_time: timeValue,
      });

      await createEventsCh([observation]);

      // Test filtering by name
      const response = await getObservations(
        `/api/public/v2/observations?fields=basic&name=unique-observation-name`,
      );

      expect(response.status).toBe(200);
      const obs = response.body.data.find((o: any) => o.id === observationId);
      expect(obs).toBeDefined();
      expect(obs?.name).toBe("unique-observation-name");
      expect(obs?.type).toBe("GENERATION");
      expect(obs?.level).toBe("WARNING");
    });

    it("should filter observations by sessionId and prefer advanced filters", async () => {
      const sessionId = `session-${randomUUID()}`;
      const otherSessionId = `session-${randomUUID()}`;
      const userId = `user-${randomUUID()}`;
      const otherUserId = `user-${randomUUID()}`;
      const environment = `environment-${randomUUID()}`;
      const otherEnvironment = `environment-${randomUUID()}`;
      const observations = [
        [sessionId, userId, environment],
        [otherSessionId, otherUserId, otherEnvironment],
      ].map(([value, observationUserId, observationEnvironment]) => {
        const observationId = randomUUID();
        return createEvent({
          id: observationId,
          span_id: observationId,
          trace_id: randomUUID(),
          project_id: projectId,
          name: `session-filter-${value}`,
          type: "GENERATION",
          level: "DEFAULT",
          session_id: value,
          user_id: observationUserId,
          environment: observationEnvironment,
          start_time: Date.now() * 1000,
        });
      });
      const otherProject = await createOrgProjectAndApiKey();
      const crossProjectObservationId = randomUUID();
      const crossProjectObservation = createEvent({
        id: crossProjectObservationId,
        span_id: crossProjectObservationId,
        trace_id: randomUUID(),
        project_id: otherProject.projectId,
        name: "cross-project-session-filter",
        type: "GENERATION",
        level: "DEFAULT",
        session_id: sessionId,
        start_time: Date.now() * 1000,
      });

      await createEventsCh([...observations, crossProjectObservation]);

      await waitForExpect(
        async () => {
          const result = await queryClickhouse<{ count: string }>({
            query: `SELECT count() as count FROM events_core WHERE span_id IN ({ids: Array(String)})`,
            params: {
              ids: [
                ...observations.map((observation) => observation.span_id),
                crossProjectObservation.span_id,
              ],
            },
          });
          expect(Number(result[0]?.count)).toBeGreaterThanOrEqual(3);
        },
        5000,
        10,
      );

      const matchingResponse = await getObservations(
        `/api/public/v2/observations?fields=basic&sessionId=${encodeURIComponent(sessionId)}`,
      );

      expect(matchingResponse.status).toBe(200);
      expect(matchingResponse.body.data).toHaveLength(1);
      expect(matchingResponse.body.data[0]?.sessionId).toBe(sessionId);

      const missingResponse = await getObservations(
        "/api/public/v2/observations?fields=basic&sessionId=missing-session",
      );

      expect(missingResponse.status).toBe(200);
      expect(missingResponse.body.data).toHaveLength(0);

      const advancedFilter = JSON.stringify([
        {
          type: "string",
          column: "sessionId",
          operator: "=",
          value: otherSessionId,
        },
      ]);
      const advancedFilterResponse = await getObservations(
        `/api/public/v2/observations?fields=basic&sessionId=${encodeURIComponent(sessionId)}&filter=${encodeURIComponent(advancedFilter)}`,
      );

      expect(advancedFilterResponse.status).toBe(200);
      expect(advancedFilterResponse.body.data).toHaveLength(1);
      expect(advancedFilterResponse.body.data[0]?.sessionId).toBe(
        otherSessionId,
      );

      const userAdvancedFilter = JSON.stringify([
        {
          type: "string",
          column: "userId",
          operator: "=",
          value: otherUserId,
        },
      ]);
      const userFilterResponse = await getObservations(
        `/api/public/v2/observations?fields=basic&userId=${encodeURIComponent(userId)}&filter=${encodeURIComponent(userAdvancedFilter)}`,
      );

      expect(userFilterResponse.status).toBe(200);
      expect(userFilterResponse.body.data).toHaveLength(1);
      expect(userFilterResponse.body.data[0]?.id).toBe(
        observations[1]?.span_id,
      );

      const nameAdvancedFilter = JSON.stringify([
        {
          type: "string",
          column: "name",
          operator: "=",
          value: observations[1]!.name,
        },
      ]);
      const nameFilterResponse = await getObservations(
        `/api/public/v2/observations?fields=basic&name=${encodeURIComponent(observations[0]!.name)}&filter=${encodeURIComponent(nameAdvancedFilter)}`,
      );

      expect(nameFilterResponse.status).toBe(200);
      expect(nameFilterResponse.body.data).toHaveLength(1);
      expect(nameFilterResponse.body.data[0]?.id).toBe(
        observations[1]?.span_id,
      );

      const environmentAdvancedFilter = JSON.stringify([
        {
          type: "string",
          column: "traceEnvironment",
          operator: "=",
          value: otherEnvironment,
        },
      ]);
      const environmentFilterResponse = await getObservations(
        `/api/public/v2/observations?fields=basic&environment=${encodeURIComponent(environment)}&filter=${encodeURIComponent(environmentAdvancedFilter)}`,
      );

      expect(environmentFilterResponse.status).toBe(200);
      expect(environmentFilterResponse.body.data).toHaveLength(1);
      expect(environmentFilterResponse.body.data[0]?.id).toBe(
        observations[1]?.span_id,
      );
    });

    it("should filter semantic roots while preserving their physical parent", async () => {
      const traceId = randomUUID();
      const physicalRootId = randomUUID();
      const appRootId = randomUUID();
      const childId = randomUUID();
      const externalParentId = randomUUID();
      const timeValue = Date.now() * 1000;

      await createEventsCh(
        (
          [
            [physicalRootId, "", false],
            [appRootId, externalParentId, true],
            [childId, physicalRootId, false],
          ] as const
        ).map(([id, parentSpanId, isAppRoot], offset) =>
          createEvent({
            id,
            span_id: id,
            parent_span_id: parentSpanId,
            is_app_root: isAppRoot,
            trace_id: traceId,
            project_id: projectId,
            type: "SPAN",
            level: "DEFAULT",
            start_time: timeValue + offset,
          }),
        ),
      );

      const fetchTopology = async (filter: string) =>
        Object.fromEntries(
          (
            await getObservations(
              `/api/public/v2/observations?fields=basic&traceId=${traceId}&${filter}`,
            )
          ).body.data.map(({ id, isRootObservation, parentObservationId }) => [
            id,
            [isRootObservation, parentObservationId],
          ]),
        );
      const structuredFilter = encodeURIComponent(
        JSON.stringify([
          {
            type: "boolean",
            column: "isRootObservation",
            operator: "=",
            value: true,
          },
        ]),
      );
      const expectedRoots = {
        [physicalRootId]: [true, null],
        [appRootId]: [true, externalParentId],
      };

      expect(
        await Promise.all(
          [
            "isRootObservation=true",
            `filter=${structuredFilter}`,
            "parentObservationId=",
            "isRootObservation=false",
          ].map(fetchTopology),
        ),
      ).toEqual([
        expectedRoots,
        expectedRoots,
        { [physicalRootId]: [true, null] },
        { [childId]: [false, physicalRootId] },
      ]);
    });

    it("should filter by multiple environment query params (any-of semantics)", async () => {
      const traceId = randomUUID();
      const envA = `env-a-${randomUUID()}`;
      const envB = `env-b-${randomUUID()}`;
      const envC = `env-c-${randomUUID()}`;
      const observationIdA: string = randomUUID();
      const observationIdB: string = randomUUID();
      const observationIdC: string = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      const buildObservation = (
        id: string,
        environment: string,
        offsetMicros: number,
      ) =>
        createEvent({
          id,
          span_id: id,
          trace_id: traceId,
          project_id: projectId,
          name: `env-filter-obs-${environment}`,
          type: "GENERATION",
          level: "DEFAULT",
          environment,
          start_time: timeValue + offsetMicros,
          end_time: timeValue + offsetMicros + 1000 * 1000,
        });

      await createEventsCh([
        buildObservation(observationIdA, envA, 0),
        buildObservation(observationIdB, envB, 1000 * 1000),
        buildObservation(observationIdC, envC, 2000 * 1000),
      ]);

      await waitForExpect(
        async () => {
          const result = await queryClickhouse<{ count: string }>({
            query: `SELECT count() as count FROM events_core WHERE project_id = {projectId: String} AND span_id IN ({ids: Array(String)})`,
            params: {
              projectId,
              ids: [observationIdA, observationIdB, observationIdC],
            },
          });
          expect(Number(result[0]?.count)).toBeGreaterThanOrEqual(3);
        },
        5000,
        10,
      );

      const response = await getObservations(
        `/api/public/v2/observations?fields=basic&traceId=${traceId}&environment=${encodeURIComponent(envA)}&environment=${encodeURIComponent(envB)}`,
      );

      expect(response.status).toBe(200);
      const returnedIds = response.body.data
        .map((obs: any) => obs.id)
        .filter((id: string) =>
          [observationIdA, observationIdB, observationIdC].includes(id),
        );
      expect(returnedIds.sort()).toEqual(
        [observationIdA, observationIdB].sort(),
      );

      for (const obs of response.body.data) {
        expect([envA, envB]).toContain(obs.environment);
      }

      // Backwards-compat: single environment value (scalar string) must still
      // behave as exact-match equality (previously `environment = 'foo'`, now
      // `environment IN ('foo')`).
      const singleEnvResponse = await getObservations(
        `/api/public/v2/observations?fields=basic&traceId=${traceId}&environment=${encodeURIComponent(envA)}`,
      );

      expect(singleEnvResponse.status).toBe(200);
      const singleEnvReturnedIds = singleEnvResponse.body.data
        .map((obs: any) => obs.id)
        .filter((id: string) =>
          [observationIdA, observationIdB, observationIdC].includes(id),
        );
      expect(singleEnvReturnedIds).toEqual([observationIdA]);

      for (const obs of singleEnvResponse.body.data) {
        expect(obs.environment).toBe(envA);
      }
    });

    it("should support filter parameter on various columns without SQL crashes", async () => {
      const traceId = randomUUID();
      const observationId1 = randomUUID();
      const observationId2 = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      // Create observations with trace-level fields that require joins
      const observation1 = createEvent({
        id: observationId1,
        span_id: observationId1,
        trace_id: traceId,
        project_id: projectId,
        name: "filter-test-obs-1",
        type: "GENERATION",
        level: "DEFAULT",
        start_time: timeValue,
        end_time: timeValue + 2000 * 1000,
        provided_model_name: "gpt-4",
        // Trace-level fields (require join to traces table)
        user_id: "test-user-123",
        trace_name: "test-trace",
        tags: ["tag1", "tag2"],
        session_id: "session-abc",
      });

      const observation2 = createEvent({
        id: observationId2,
        span_id: observationId2,
        trace_id: traceId,
        project_id: projectId,
        name: "filter-test-obs-2",
        type: "SPAN",
        level: "WARNING",
        start_time: timeValue + 1000 * 1000,
        end_time: timeValue + 3000 * 1000,
        provided_model_name: "gpt-3.5-turbo",
        // Trace-level fields (require join to traces table)
        user_id: "test-user-456",
        trace_name: "different-trace",
        session_id: "session-xyz",
      });

      await createEventsCh([observation1, observation2]);

      // Wait for ClickHouse to process
      await waitForExpect(
        async () => {
          const result = await queryClickhouse<{ count: string }>({
            query: `SELECT count() as count FROM events_core WHERE project_id = {projectId: String} AND span_id IN ({ids: Array(String)})`,
            params: { projectId, ids: [observationId1, observationId2] },
          });
          expect(Number(result[0]?.count)).toBeGreaterThanOrEqual(2);
        },
        5000,
        10,
      );

      // Focus on testing filter columns that may have complex handling
      // (trace-level fields now read directly from events table: userId, traceName, sessionId, traceTags, traceEnvironment)
      // and score-related columns that may require special handling
      const filterTestCases = [
        // Trace-level fields (now read directly from events table)
        {
          description: "trace field: userId",
          filter: [
            {
              type: "string",
              column: "userId",
              operator: "=",
              value: "test-user-123",
            },
          ],
        },
        {
          description: "trace field: traceName",
          filter: [
            {
              type: "string",
              column: "traceName",
              operator: "contains",
              value: "test",
            },
          ],
        },
        {
          description: "trace field: sessionId",
          filter: [
            {
              type: "string",
              column: "sessionId",
              operator: "=",
              value: "session-abc",
            },
          ],
        },
        {
          description: "trace field: traceEnvironment",
          filter: [
            {
              type: "string",
              column: "traceEnvironment",
              operator: "=",
              value: "production",
            },
          ],
        },
        // Also test a few events table columns to ensure they still work
        {
          description: "events field: name",
          filter: [
            {
              type: "string",
              column: "name",
              operator: "contains",
              value: "filter-test",
            },
          ],
        },
        {
          description: "events field: type",
          filter: [
            {
              type: "string",
              column: "type",
              operator: "=",
              value: "GENERATION",
            },
          ],
        },
      ];

      // Test each filter to ensure no SQL crashes
      for (const testCase of filterTestCases) {
        const filterParam = JSON.stringify(testCase.filter);
        const response = await getObservations(
          `/api/public/v2/observations?traceId=${traceId}&fields=basic,io,cost,model,metadata&filter=${encodeURIComponent(filterParam)}`,
        );

        // Main assertion: should not crash (200 status)
        expect(response.status).toBe(200);
        expect(response.body.data).toBeDefined();

        // Response should be an array (even if empty)
        expect(Array.isArray(response.body.data)).toBe(true);
      }

      // Verify a trace-field filter (requires join) returns expected observations
      const userIdFilterParam = JSON.stringify([
        {
          type: "string",
          column: "userId",
          operator: "=",
          value: "test-user-123",
        },
      ]);
      const userIdFilterResponse = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&fields=basic&filter=${encodeURIComponent(userIdFilterParam)}`,
      );

      expect(userIdFilterResponse.status).toBe(200);
      // Only observation1 has user_id: "test-user-123"
      expect(userIdFilterResponse.body.data.length).toEqual(1);
      const userFilteredObs = userIdFilterResponse.body.data.find(
        (obs: any) => obs.id === observationId1,
      );
      expect(userFilteredObs).toBeDefined();

      // Verify trace name filter (requires join)
      const traceNameFilterParam = JSON.stringify([
        {
          type: "string",
          column: "traceName",
          operator: "contains",
          value: "test",
        },
      ]);
      const traceNameResponse = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&fields=basic&filter=${encodeURIComponent(traceNameFilterParam)}`,
      );

      expect(traceNameResponse.status).toBe(200);
      expect(traceNameResponse.body.data.length).toBeGreaterThanOrEqual(1);
    });

    it("should support nested metadata field filtering with stringObject", async () => {
      const traceId = randomUUID();
      const observationId1 = randomUUID();
      const observationId2 = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      // Create observations with nested metadata
      const observation1 = createEvent({
        id: observationId1,
        span_id: observationId1,
        trace_id: traceId,
        project_id: projectId,
        name: "nested-metadata-obs-1",
        type: "GENERATION",
        level: "DEFAULT",
        start_time: timeValue,
        // Nested metadata: { scope: { name: "api-server" }, region: "us-east" }
        metadata: { scope: { name: "api-server" }, region: "us-east" },
        metadata_names: ["scope.name", "region"],
        metadata_values: ["api-server", "us-east"],
      });

      const observation2 = createEvent({
        id: observationId2,
        span_id: observationId2,
        trace_id: traceId,
        project_id: projectId,
        name: "nested-metadata-obs-2",
        type: "SPAN",
        level: "DEFAULT",
        start_time: timeValue + 1000 * 1000,
        // Nested metadata: { scope: { name: "ui-client" }, region: "us-west" }
        metadata: { scope: { name: "ui-client" }, region: "us-west" },
        metadata_names: ["scope.name", "region"],
        metadata_values: ["ui-client", "us-west"],
      });

      await createEventsCh([observation1, observation2]);

      // Wait for ClickHouse to process
      await waitForExpect(
        async () => {
          const result = await queryClickhouse<{ count: string }>({
            query: `SELECT count() as count FROM events_core WHERE project_id = {projectId: String} AND span_id IN ({ids: Array(String)})`,
            params: { projectId, ids: [observationId1, observationId2] },
          });
          expect(Number(result[0]?.count)).toBeGreaterThanOrEqual(2);
        },
        5000,
        10,
      );

      // Filter using dot-notation key for nested metadata: scope.name contains "api"
      const filterParam = JSON.stringify([
        {
          type: "stringObject",
          column: "metadata",
          operator: "contains",
          key: "scope.name",
          value: "api",
        },
      ]);

      const response = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&fields=basic,metadata&filter=${encodeURIComponent(filterParam)}`,
      );

      expect(response.status).toBe(200);
      // Only observation1 should match (scope.name contains "api")
      expect(response.body.data.length).toBe(1);
      const matchedObs = response.body.data.find(
        (obs: any) => obs.id === observationId1,
      );
      expect(matchedObs).toBeDefined();
      expect(matchedObs?.name).toBe("nested-metadata-obs-1");
    });

    it("supports indexed literal matches filters for IO and metadata", async () => {
      const traceId = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;
      const tokenMatchId = randomUUID();
      const punctuationMatchId = randomUUID();
      const embeddedOnlyId = randomUUID();
      const ioPhraseMatchId = randomUUID();
      const ioPhraseReverseId = randomUUID();
      const ioPhraseGapId = randomUUID();
      const metadataCaseMatchId = randomUUID();
      const metadataCaseMismatchId = randomUUID();
      const metadataWrongKeyId = randomUUID();
      const metadataMultiTokenMatchId = randomUUID();
      const metadataMultiTokenReverseId = randomUUID();
      const metadataMultiTokenGapId = randomUUID();
      const metadataSplitAcrossValuesId = randomUUID();
      const metadataMultiTokenWrongKeyId = randomUUID();

      await createEventsCh([
        createEvent({
          id: tokenMatchId,
          span_id: tokenMatchId,
          trace_id: traceId,
          project_id: projectId,
          name: "io-token-match",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue,
          output: "Needle in mixed case",
        }),
        createEvent({
          id: punctuationMatchId,
          span_id: punctuationMatchId,
          trace_id: traceId,
          project_id: projectId,
          name: "io-punctuation-match",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 1000,
          output: "[email protected]",
        }),
        createEvent({
          id: embeddedOnlyId,
          span_id: embeddedOnlyId,
          trace_id: traceId,
          project_id: projectId,
          name: "io-embedded-only",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 2000,
          output: "foobarneedle cadabra",
        }),
        createEvent({
          id: ioPhraseMatchId,
          span_id: ioPhraseMatchId,
          trace_id: traceId,
          project_id: projectId,
          name: "io-phrase-match",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 3000,
          output: "prefix alpha beta suffix",
        }),
        createEvent({
          id: ioPhraseReverseId,
          span_id: ioPhraseReverseId,
          trace_id: traceId,
          project_id: projectId,
          name: "io-phrase-reverse",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 4000,
          output: "prefix beta alpha suffix",
        }),
        createEvent({
          id: ioPhraseGapId,
          span_id: ioPhraseGapId,
          trace_id: traceId,
          project_id: projectId,
          name: "io-phrase-gap",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 5000,
          output: "prefix alpha gap beta suffix",
        }),
        createEvent({
          id: metadataCaseMatchId,
          span_id: metadataCaseMatchId,
          trace_id: traceId,
          project_id: projectId,
          name: "metadata-case-match",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 6000,
          metadata: { source: "needle API" },
          metadata_names: ["source"],
          metadata_values: ["needle API"],
        }),
        createEvent({
          id: metadataCaseMismatchId,
          span_id: metadataCaseMismatchId,
          trace_id: traceId,
          project_id: projectId,
          name: "metadata-case-mismatch",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 7000,
          metadata: { source: "Needle API" },
          metadata_names: ["source"],
          metadata_values: ["Needle API"],
        }),
        createEvent({
          id: metadataWrongKeyId,
          span_id: metadataWrongKeyId,
          trace_id: traceId,
          project_id: projectId,
          name: "metadata-wrong-key",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 8000,
          metadata: { other: "needle API" },
          metadata_names: ["other"],
          metadata_values: ["needle API"],
        }),
        createEvent({
          id: metadataMultiTokenMatchId,
          span_id: metadataMultiTokenMatchId,
          trace_id: traceId,
          project_id: projectId,
          name: "metadata-multi-token-match",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 9000,
          metadata: { source: "alpha beta" },
          metadata_names: ["source"],
          metadata_values: ["alpha beta"],
        }),
        createEvent({
          id: metadataMultiTokenReverseId,
          span_id: metadataMultiTokenReverseId,
          trace_id: traceId,
          project_id: projectId,
          name: "metadata-multi-token-reverse",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 10000,
          metadata: { source: "beta alpha" },
          metadata_names: ["source"],
          metadata_values: ["beta alpha"],
        }),
        createEvent({
          id: metadataMultiTokenGapId,
          span_id: metadataMultiTokenGapId,
          trace_id: traceId,
          project_id: projectId,
          name: "metadata-multi-token-gap",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 11000,
          metadata: { source: "alpha gap beta" },
          metadata_names: ["source"],
          metadata_values: ["alpha gap beta"],
        }),
        createEvent({
          id: metadataSplitAcrossValuesId,
          span_id: metadataSplitAcrossValuesId,
          trace_id: traceId,
          project_id: projectId,
          name: "metadata-split-across-values",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 12000,
          metadata: { source: "alpha", other: "beta" },
          metadata_names: ["source", "other"],
          metadata_values: ["alpha", "beta"],
        }),
        createEvent({
          id: metadataMultiTokenWrongKeyId,
          span_id: metadataMultiTokenWrongKeyId,
          trace_id: traceId,
          project_id: projectId,
          name: "metadata-multi-token-wrong-key",
          type: "GENERATION",
          level: "DEFAULT",
          start_time: timeValue + 13000,
          metadata: { source: "unrelated", other: "alpha beta" },
          metadata_names: ["source", "other"],
          metadata_values: ["unrelated", "alpha beta"],
        }),
      ]);

      await waitForExpect(
        async () => {
          const result = await queryClickhouse<{ count: string }>({
            query: `SELECT count() as count FROM events_core WHERE project_id = {projectId: String} AND trace_id = {traceId: String}`,
            params: { projectId, traceId },
          });
          expect(Number(result[0]?.count)).toBeGreaterThanOrEqual(14);
        },
        5000,
        10,
      );

      const ioFilterParam = JSON.stringify([
        {
          type: "string",
          column: "output",
          operator: "matches",
          value: "needle",
        },
      ]);
      const ioResponse = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&fields=basic,io&filter=${encodeURIComponent(ioFilterParam)}`,
      );
      const ioIds = ioResponse.body.data.map((obs: any) => obs.id);

      expect(ioResponse.status).toBe(200);
      expect(ioIds).toEqual(
        expect.arrayContaining([tokenMatchId, punctuationMatchId]),
      );
      expect(ioIds).not.toContain(embeddedOnlyId);

      const ioPhraseFilterParam = JSON.stringify([
        {
          type: "string",
          column: "output",
          operator: "matches",
          value: "alpha beta",
        },
      ]);
      const ioPhraseResponse = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&fields=basic,io&filter=${encodeURIComponent(ioPhraseFilterParam)}`,
      );
      const ioPhraseIds = ioPhraseResponse.body.data.map((obs: any) => obs.id);

      expect(ioPhraseResponse.status).toBe(200);
      expect(ioPhraseIds).toContain(ioPhraseMatchId);
      expect(ioPhraseIds).not.toContain(ioPhraseReverseId);
      expect(ioPhraseIds).not.toContain(ioPhraseGapId);

      const metadataFilterParam = JSON.stringify([
        {
          type: "stringObject",
          column: "metadata",
          operator: "matches",
          key: "source",
          value: "needle",
        },
      ]);
      const metadataResponse = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&fields=basic,metadata&filter=${encodeURIComponent(metadataFilterParam)}`,
      );
      const metadataIds = metadataResponse.body.data.map((obs: any) => obs.id);

      expect(metadataResponse.status).toBe(200);
      expect(metadataIds).toContain(metadataCaseMatchId);
      expect(metadataIds).not.toContain(metadataCaseMismatchId);
      expect(metadataIds).not.toContain(metadataWrongKeyId);

      const multiTokenMetadataFilterParam = JSON.stringify([
        {
          type: "stringObject",
          column: "metadata",
          operator: "matches",
          key: "source",
          value: "alpha beta",
        },
      ]);
      const multiTokenMetadataResponse = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&fields=basic,metadata&filter=${encodeURIComponent(multiTokenMetadataFilterParam)}`,
      );
      const multiTokenMetadataIds = multiTokenMetadataResponse.body.data.map(
        (obs: any) => obs.id,
      );

      expect(multiTokenMetadataResponse.status).toBe(200);
      expect(multiTokenMetadataIds).toContain(metadataMultiTokenMatchId);
      expect(multiTokenMetadataIds).not.toContain(metadataMultiTokenReverseId);
      expect(multiTokenMetadataIds).not.toContain(metadataMultiTokenGapId);
      expect(multiTokenMetadataIds).not.toContain(metadataSplitAcrossValuesId);
      expect(multiTokenMetadataIds).not.toContain(metadataMultiTokenWrongKeyId);
    });

    it("rejects public v2 IO filters that are guaranteed to be slow", async () => {
      const containsOnlyFilter = JSON.stringify([
        {
          type: "string",
          column: "output",
          operator: "contains",
          value: "needle",
        },
      ]);
      const containsOnlyResponse = await getRaw(
        `/api/public/v2/observations?fields=basic&filter=${encodeURIComponent(containsOnlyFilter)}`,
      );
      expect(containsOnlyResponse.status).toBe(400);

      const matchesAndContainsFilter = JSON.stringify([
        {
          type: "string",
          column: "output",
          operator: "matches",
          value: "needle",
        },
        {
          type: "string",
          column: "output",
          operator: "contains",
          value: "needle",
        },
      ]);
      const matchesAndContainsResponse = await getRaw(
        `/api/public/v2/observations?fields=basic&filter=${encodeURIComponent(matchesAndContainsFilter)}`,
      );
      expect(matchesAndContainsResponse.status).toBe(200);

      const metadataMatchesAndIoContainsFilter = JSON.stringify([
        {
          type: "stringObject",
          column: "metadata",
          operator: "matches",
          key: "source",
          value: "needle",
        },
        {
          type: "string",
          column: "output",
          operator: "contains",
          value: "needle",
        },
      ]);
      const metadataMatchesAndIoContainsResponse = await getRaw(
        `/api/public/v2/observations?fields=basic&filter=${encodeURIComponent(metadataMatchesAndIoContainsFilter)}`,
      );
      expect(metadataMatchesAndIoContainsResponse.status).toBe(400);
    });

    it.each([
      {
        description: "null",
        filter: {
          type: "null",
          column: "output",
          operator: "is null",
          value: "",
        },
      },
      {
        description: "stringOptions",
        filter: {
          type: "stringOptions",
          column: "output",
          operator: "any of",
          value: ["needle"],
        },
      },
    ])(
      "rejects non-string public v2 IO $description filters",
      async ({ filter }) => {
        const filterParam = JSON.stringify([filter]);

        const response = await getRaw(
          `/api/public/v2/observations?fields=basic&filter=${encodeURIComponent(filterParam)}`,
        );

        expect(response.status).toBe(400);
        expect(JSON.stringify(response.body)).toContain(
          "Input/output filters only support filter type `string`.",
        );
      },
    );

    it.each([
      {
        description: "non-indexed event column",
        filter: [
          {
            type: "string",
            column: "name",
            operator: "matches",
            value: "needle",
          },
        ],
      },
      {
        description: "empty value",
        filter: [
          {
            type: "string",
            column: "output",
            operator: "matches",
            value: "",
          },
        ],
      },
      {
        description: "tokenless value",
        filter: [
          {
            type: "stringObject",
            column: "metadata",
            operator: "matches",
            key: "source",
            value: "!!!",
          },
        ],
      },
    ])("rejects matches filters with $description", async ({ filter }) => {
      const response = await getRaw(
        `/api/public/v2/observations?fields=basic&filter=${encodeURIComponent(JSON.stringify(filter))}`,
      );

      expect(response.status).toBe(400);
    });
  });

  maybe("Field group contract", () => {
    // Core fields always returned regardless of which group is requested
    const CORE_FIELDS = [
      "id",
      "traceId",
      "type",
      "startTime",
      "endTime",
      "projectId",
      "parentObservationId",
    ] as const;

    // All non-core fields across all groups — used to assert absence.
    // Note: latency is computed from start_time/end_time (both in core). timeToFirstToken is computed from start_time
    // and completion_start_time, but the converter spreads it when EITHER is defined; since start_time is in core and
    // always selected, timeToFirstToken is always present (as null when completion_start_time is not selected).
    const ALL_NON_CORE_FIELDS = [
      // basic
      "name",
      "level",
      "statusMessage",
      "version",
      "environment",
      "bookmarked",
      "public",
      "userId",
      "sessionId",
      "isRootObservation",
      // time
      "completionStartTime",
      "createdAt",
      "updatedAt",
      // io
      "input",
      "output",
      // metadata
      "metadata",
      // model — note: the API response uses "model" for the provided model name,
      // not "providedModelName" (the domain type maps provided_model_name → model)
      "model",
      "internalModelId",
      "modelParameters",
      // usage
      "usageDetails",
      "costDetails",
      "totalCost",
      "usagePricingTierName",
      // prompt
      "promptId",
      "promptName",
      "promptVersion",
      // trace_context
      "traceName",
      "tags",
      "release",
    ] as const;

    let sharedObsId: string;
    let sharedTraceId: string;

    beforeEach(async () => {
      sharedTraceId = randomUUID();
      sharedObsId = randomUUID();
      const now = new Date();
      const timeValue = now.getTime() * 1000;

      const obs = createEvent({
        id: sharedObsId,
        span_id: sharedObsId,
        trace_id: sharedTraceId,
        project_id: projectId,
        name: "field-group-contract-obs",
        type: "GENERATION",
        level: "DEFAULT",
        status_message: "ok",
        version: "1.0",
        environment: "production",
        bookmarked: true,
        public: true,
        user_id: "test-user",
        session_id: "test-session",
        start_time: timeValue,
        end_time: timeValue + 1000 * 1000,
        completion_start_time: timeValue + 500 * 1000,
        provided_model_name: "gpt-4",
        model_id: randomUUID(),
        model_parameters: '{"temperature":0.5}',
        usage_details: { input: 10, output: 20, total: 30 },
        cost_details: { input: 0.01, output: 0.02, total: 0.03 },
        usage_pricing_tier_name: "contract-tier",
        prompt_id: randomUUID(),
        prompt_name: "contract-prompt",
        prompt_version: 2,
        input: "contract input",
        output: "contract output",
        metadata_names: ["key"],
        metadata_values: ["val"],
        trace_name: "contract-trace",
        tags: ["tag-a", "tag-b"],
        release: "1.2.3",
      });

      await createEventsCh([obs]);
    });

    // Known fixture values — used to assert exact values, not just presence.
    // toBeDefined() passes for null; for fields the fixture explicitly sets we
    // assert the stored value so a null regression is caught.
    const knownValues: Record<string, unknown> = {
      name: "field-group-contract-obs",
      level: "DEFAULT",
      statusMessage: "ok",
      version: "1.0",
      environment: "production",
      bookmarked: true,
      public: true,
      userId: "test-user",
      sessionId: "test-session",
      model: "gpt-4",
      modelParameters: { temperature: 0.5 },
      promptName: "contract-prompt",
      promptVersion: 2,
      input: "contract input",
      output: "contract output",
      traceName: "contract-trace",
      tags: ["tag-a", "tag-b"],
      release: "1.2.3",
      usagePricingTierName: "contract-tier",
    };

    const fieldsForGroup: Record<string, readonly string[]> = {
      basic: [
        "name",
        "level",
        "statusMessage",
        "version",
        "environment",
        "bookmarked",
        "public",
        "userId",
        "sessionId",
        "isRootObservation",
      ],
      time: ["completionStartTime", "createdAt", "updatedAt"],
      io: ["input", "output"],
      metadata: ["metadata"],
      // "model" is the API response key for provided_model_name (see domain type)
      model: ["model", "internalModelId", "modelParameters"],
      usage: [
        "usageDetails",
        "costDetails",
        "totalCost",
        "usagePricingTierName",
      ],
      prompt: ["promptId", "promptName", "promptVersion"],
      // latency and timeToFirstToken are always returned (computed from core start_time, completion_start_time,
      // end_time), but the metrics group is still defined for documentation and to verify these fields are indeed
      // present
      metrics: ["latency", "timeToFirstToken"],
      trace_context: ["traceName", "tags", "release"],
    };

    for (const [group, expectedFields] of Object.entries(fieldsForGroup)) {
      it(`field group contract: ${group}`, async () => {
        const response = await getObservations(
          `/api/public/v2/observations?fields=${group}&traceId=${sharedTraceId}`,
        );

        expect(response.status).toBe(200);
        const obs = response.body.data.find((o: any) => o.id === sharedObsId);
        expect(obs).toBeDefined();
        if (!obs) return; // narrow type; expect above already fails the test

        // Core fields always present
        for (const field of CORE_FIELDS) {
          expect(obs[field]).toBeDefined();
        }

        // Requested group fields present — assert exact value when the fixture
        // set one, toBeDefined() otherwise (e.g. dates, generated IDs).
        for (const field of expectedFields) {
          if (field in knownValues) {
            expect(
              obs[field],
              `expected field "${field}" to equal fixture value for group "${group}"`,
            ).toStrictEqual(knownValues[field]);
          } else {
            expect(
              obs[field],
              `expected field "${field}" to be defined for group "${group}"`,
            ).toBeDefined();
          }
        }

        // Fields from other groups must be absent from the response
        const absentFields = ALL_NON_CORE_FIELDS.filter(
          (f) => !(expectedFields as readonly string[]).includes(f),
        );
        for (const field of absentFields) {
          expect(
            obs[field],
            `expected field "${field}" to be absent when only group "${group}" is requested`,
          ).toBeUndefined();
        }
      });
    }
  });

  maybe("trace_context group: unresolvable trace name", () => {
    // The events table ships "no resolvable trace name" as '' on the wire
    // (eventsTableTraceNameSelectSql, LFE-14924). The API contract is null, so
    // the partial (field-group) converter must map it back.
    it("returns null traceName for a child span without a stored trace name", async () => {
      const traceId = randomUUID();
      const observationId = randomUUID();

      await createEventsCh([
        createEvent({
          id: observationId,
          span_id: observationId,
          parent_span_id: randomUUID(),
          trace_id: traceId,
          project_id: projectId,
          type: "SPAN",
          name: "child-without-trace-name",
          trace_name: "",
        }),
      ]);

      const response = await getObservations(
        `/api/public/v2/observations?fields=trace_context&traceId=${traceId}`,
      );

      expect(response.status).toBe(200);
      const observation = response.body.data.find(
        (candidate: { id: string }) => candidate.id === observationId,
      );
      expect(observation).toBeDefined();
      expect(observation?.traceName).toBeNull();
    });
  });

  maybe("Metadata expansion with expandMetadata parameter", () => {
    it("should return full metadata values when expandMetadata is specified", async () => {
      const traceId = randomUUID();
      const observationId = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      // Create two long metadata values
      const longValue1 = "a".repeat(300);
      const longValue2 = "b".repeat(300);

      const observation = createEvent({
        id: observationId,
        span_id: observationId,
        trace_id: traceId,
        project_id: projectId,
        name: "selective-expansion-test",
        type: "GENERATION",
        level: "DEFAULT",
        start_time: timeValue,
        metadata: {
          expandMe: longValue1,
          otherKey: longValue2,
          shortKey: "shortValue",
        },
        metadata_names: ["expandMe", "otherKey", "shortKey"],
        metadata_values: [longValue1, longValue2, "shortValue"],
      });

      await createEventsCh([observation]);

      // Wait for ClickHouse to process
      await waitForExpect(
        async () => {
          const result = await queryClickhouse<{ count: string }>({
            query: `SELECT count() as count FROM events_core WHERE project_id = {projectId: String} AND span_id = {id: String}`,
            params: { projectId, id: observationId },
          });
          expect(Number(result[0]?.count)).toBeGreaterThanOrEqual(1);
        },
        5000,
        10,
      );

      // Request metadata with expansion - this switches to events_full table
      const response = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&fields=metadata&expandMetadata=expandMe`,
      );

      expect(response.status).toBe(200);
      const obs = response.body.data.find((o: any) => o.id === observationId);
      expect(obs).toBeDefined();

      // When expandMetadata is specified, the query uses events_full table which has
      // full (non-truncated) metadata values. All metadata values are returned in full.
      expect(obs?.metadata?.expandMe?.length).toBe(300);
      expect(obs?.metadata?.expandMe).toBe(longValue1);

      expect(obs?.metadata?.otherKey?.length).toBe(300);
      expect(obs?.metadata?.otherKey).toBe(longValue2);

      // 'shortValue' should be present as is
      expect(obs?.metadata?.shortKey).toBe("shortValue");
    });

    it("should handle expansion of non-existent metadata key gracefully", async () => {
      const traceId = randomUUID();
      const observationId = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      const observation = createEvent({
        id: observationId,
        span_id: observationId,
        trace_id: traceId,
        project_id: projectId,
        name: "non-existent-key-test",
        type: "GENERATION",
        level: "DEFAULT",
        start_time: timeValue,
        metadata: { existingKey: "value" },
        metadata_names: ["existingKey"],
        metadata_values: ["value"],
      });

      await createEventsCh([observation]);

      // Wait for ClickHouse to process
      await waitForExpect(
        async () => {
          const result = await queryClickhouse<{ count: string }>({
            query: `SELECT count() as count FROM events_core WHERE project_id = {projectId: String} AND span_id = {id: String}`,
            params: { projectId, id: observationId },
          });
          expect(Number(result[0]?.count)).toBeGreaterThanOrEqual(1);
        },
        5000,
        10,
      );

      // Request expansion for a key that doesn't exist
      const response = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&fields=metadata&expandMetadata=nonExistentKey`,
      );

      // Should not error, just return metadata without the non-existent key
      expect(response.status).toBe(200);
      const obs = response.body.data.find((o: any) => o.id === observationId);
      expect(obs).toBeDefined();

      // Existing key should still be present
      expect(obs?.metadata?.existingKey).toBe("value");
      // Non-existent key should not be in metadata
      expect(obs?.metadata?.nonExistentKey).toBeUndefined();
    });

    it("should return truncated metadata when expandMetadata is empty string", async () => {
      const traceId = randomUUID();
      const observationId = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      // Create a long metadata value (> 200 chars, the MV truncation limit)
      const longValue = "z".repeat(300);
      // events_core MV truncates metadata values to 200 chars
      const METADATA_CUTOFF = 200;

      const observation = createEvent({
        id: observationId,
        span_id: observationId,
        trace_id: traceId,
        project_id: projectId,
        name: "empty-expansion-test",
        type: "GENERATION",
        level: "DEFAULT",
        start_time: timeValue,
        metadata: { longKey: longValue },
        metadata_names: ["longKey"],
        metadata_values: [longValue],
      });

      await createEventsCh([observation]);

      // Wait for ClickHouse to process
      await waitForExpect(
        async () => {
          const result = await queryClickhouse<{ count: string }>({
            query: `SELECT count() as count FROM events_core WHERE project_id = {projectId: String} AND span_id = {id: String}`,
            params: { projectId, id: observationId },
          });
          expect(Number(result[0]?.count)).toBeGreaterThanOrEqual(1);
        },
        5000,
        10,
      );

      // Request metadata with empty expandMetadata - should use events_core (truncated)
      const response = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&fields=metadata&expandMetadata=`,
      );

      expect(response.status).toBe(200);
      const obs = response.body.data.find((o: any) => o.id === observationId);
      expect(obs).toBeDefined();

      // Metadata should still be present but truncated (empty expandMetadata uses events_core)
      expect(obs?.metadata?.longKey).toBeDefined();
      expect(obs?.metadata?.longKey?.length).toBe(METADATA_CUTOFF);
      expect(obs?.metadata?.longKey).toBe(
        longValue.substring(0, METADATA_CUTOFF),
      );
    });
  });

  maybe("Cursor-based pagination", () => {
    it("paginates across empty scan windows without repeating boundary rows", async () => {
      const traceId = randomUUID();
      const ids = [randomUUID(), randomUUID(), randomUUID()];
      const upper = new Date();
      upper.setUTCHours(0, 0, 0, 0);
      const daysBefore = (days: number) =>
        new Date(upper.getTime() - days * 24 * 60 * 60 * 1000);
      const timestamps = [7, 15, 17].map(daysBefore);
      await createEventsCh(
        ids.map((id, index) =>
          createEvent({
            id,
            span_id: id,
            project_id: projectId,
            trace_id: traceId,
            type: "SPAN",
            start_time: timestamps[index].getTime() * 1000,
          }),
        ),
      );

      const url = `/api/public/v2/observations?traceId=${traceId}&fields=core&limit=2&fromStartTime=${daysBefore(18).toISOString()}&toStartTime=${upper.toISOString()}`;
      const first = await getObservations(url);
      expect(first.status).toBe(200);
      expect(first.body.data.map((row) => row.id)).toEqual(ids.slice(0, 2));
      expect(first.body.meta.cursor).toBeDefined();
      const second = await getObservations(
        `${url}&cursor=${encodeURIComponent(first.body.meta.cursor!)}`,
      );
      expect(second.body.data.map((row) => row.id)).toEqual(ids.slice(2));
      expect(second.body.meta.cursor).toBeUndefined();

      const boundaryCursor = encodeCursor({
        lastStartTimeTo: timestamps[0],
        lastTraceId: "CUR00254FG5s",
        lastId: "",
      });
      const afterEmptyWindow = await getObservations(
        `${url}&cursor=${encodeURIComponent(boundaryCursor)}`,
      );
      expect(afterEmptyWindow.body.data.map((row) => row.id)).toEqual(
        ids.slice(1),
      );
      expect(afterEmptyWindow.body.meta.cursor).toBeUndefined();

      const [{ hash }] = await queryClickhouse<{ hash: number }>({
        query: "SELECT xxHash32({traceId: String}) AS hash",
        params: { traceId: "CUR00254FG5s" },
      });
      expect(hash).toBe(0);
    });

    it("scans sparse future windows without clamping customer timestamps", async () => {
      const traceId = randomUUID();
      const olderId = randomUUID();
      const futureId = randomUUID();
      const from = new Date("2026-10-01T00:00:00.000Z");
      const to = new Date("2100-02-01T00:00:00.000Z");
      const older = new Date("2026-10-15T00:00:00.000Z");
      const future = new Date("2099-12-15T00:00:00.000Z");

      await createEventsCh([
        createEvent({
          id: olderId,
          span_id: olderId,
          project_id: projectId,
          trace_id: traceId,
          type: "SPAN",
          start_time: older.getTime() * 1000,
        }),
        createEvent({
          id: futureId,
          span_id: futureId,
          project_id: projectId,
          trace_id: traceId,
          type: "SPAN",
          start_time: future.getTime() * 1000,
        }),
      ]);

      const countQuery = new EventsQueryBuilder({ projectId })
        .selectRaw("count() AS count")
        .whereRaw("e.trace_id = {traceId: String}", { traceId })
        .buildWithParams();

      await waitForExpect(
        async () => {
          const result = await queryClickhouse<{ count: string }>(countQuery);
          expect(Number(result[0]?.count)).toBeGreaterThanOrEqual(2);
        },
        5000,
        10,
      );

      const response = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&fields=core&limit=10&fromStartTime=${from.toISOString()}&toStartTime=${to.toISOString()}`,
      );

      expect(response.status).toBe(200);
      expect(response.body.data.map((row) => row.id)).toEqual([
        futureId,
        olderId,
      ]);
      expect(response.body.meta.cursor).toBeUndefined();
    });

    it("should apply LIMIT to query even on first request (no cursor)", async () => {
      // This test verifies the bug fix - the v2 API should respect limit
      // even on the first request without a cursor
      const traceId = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      // Create more observations than the limit
      const observations = [];
      for (let i = 0; i < 10; i++) {
        const obsId = randomUUID();
        observations.push(
          createEvent({
            id: obsId,
            span_id: obsId,
            trace_id: traceId,
            project_id: projectId,
            name: `limit-test-obs-${i}`,
            type: "GENERATION",
            level: "DEFAULT",
            start_time: timeValue + i * 1000 * 1000,
          }),
        );
      }

      await createEventsCh(observations);

      // Wait for ClickHouse to process
      await waitForExpect(
        async () => {
          const result = await queryClickhouse<{ count: string }>({
            query: `SELECT count() as count FROM events_core WHERE project_id = {projectId: String} AND trace_id = {traceId: String}`,
            params: { projectId, traceId },
          });
          expect(Number(result[0]?.count)).toBeGreaterThanOrEqual(10);
        },
        5000,
        10,
      );

      // Request with limit=5 - should return exactly 5, not all 10
      const response = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&limit=5`,
      );

      expect(response.status).toBe(200);
      // With the bug, this returns all 10 (no LIMIT applied)
      // After fix, this should return exactly 5
      expect(response.body.data.length).toBe(5);
      // Should have cursor since there are more results
      expect(response.body.meta.cursor).toBeDefined();
    });

    it("should return cursor when results equal limit", async () => {
      const traceId = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      // Create 3 observations
      const observations = [];
      for (let i = 0; i < 3; i++) {
        const obsId = randomUUID();
        observations.push(
          createEvent({
            id: obsId,
            span_id: obsId,
            trace_id: traceId,
            project_id: projectId,
            name: `cursor-test-obs-${i}`,
            type: "GENERATION",
            level: "DEFAULT",
            start_time: timeValue + i * 1000 * 1000, // 1 second apart
          }),
        );
      }

      await createEventsCh(observations);

      // Fetch with limit=2 (should have cursor since we have 3 observations)
      const response = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&limit=2`,
      );

      expect(response.status).toBe(200);
      expect(response.body.data.length).toBe(2);
      expect(response.body.meta.cursor).toBeDefined();
      expect(typeof response.body.meta.cursor).toBe("string");
    });

    it("should not return cursor when results less than limit", async () => {
      const traceId = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      // Create only 2 observations
      const observations = [];
      for (let i = 0; i < 2; i++) {
        const obsId = randomUUID();
        observations.push(
          createEvent({
            id: obsId,
            span_id: obsId,
            trace_id: traceId,
            project_id: projectId,
            name: `no-cursor-test-obs-${i}`,
            type: "GENERATION",
            level: "DEFAULT",
            start_time: timeValue + i * 1000 * 1000,
          }),
        );
      }

      await createEventsCh(observations);

      // Fetch with limit=5 (should not have cursor since we only have 2)
      const response = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&limit=5`,
      );

      expect(response.status).toBe(200);
      expect(response.body.data.length).toBe(2);
      expect(response.body.meta.cursor).toBeUndefined();
    });

    it("should paginate correctly using cursor without overlap", async () => {
      const traceId = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      // Create 5 observations with distinct timestamps
      const observations = [];
      for (let i = 0; i < 5; i++) {
        const obsId = randomUUID();
        observations.push(
          createEvent({
            id: obsId,
            span_id: obsId,
            trace_id: traceId,
            project_id: projectId,
            name: `pagination-test-obs-${i}`,
            type: "GENERATION",
            level: "DEFAULT",
            start_time: timeValue + i * 1000 * 1000, // 1 second apart
          }),
        );
      }

      await createEventsCh(observations);

      // Fetch first page with limit=2
      const page1 = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&limit=2`,
      );

      expect(page1.status).toBe(200);
      expect(page1.body.data.length).toBe(2);
      expect(page1.body.meta.cursor).toBeDefined();

      const page1Ids = page1.body.data.map((obs: any) => obs.id);

      // Fetch second page using cursor
      const page2 = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&limit=2&cursor=${page1.body.meta.cursor}`,
      );

      expect(page2.status).toBe(200);
      expect(page2.body.data.length).toBe(2);
      expect(page2.body.meta.cursor).toBeDefined(); // Should have cursor for third page

      const page2Ids = page2.body.data.map((obs: any) => obs.id);

      // Verify no overlap between pages
      const overlap = page1Ids.filter((id: string) => page2Ids.includes(id));
      expect(overlap.length).toBe(0);

      // Fetch third page
      const page3 = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&limit=2&cursor=${page2.body.meta.cursor}`,
      );

      expect(page3.status).toBe(200);
      expect(page3.body.data.length).toBe(1); // Only 1 remaining
      expect(page3.body.meta.cursor).toBeUndefined(); // No more pages

      const page3Ids = page3.body.data.map((obs: any) => obs.id);

      // Verify all observations retrieved exactly once
      const allIds = [...page1Ids, ...page2Ids, ...page3Ids];
      expect(allIds.length).toBe(5);
      expect(new Set(allIds).size).toBe(5); // All unique
    });

    it("should handle cursor with observations having same start_time", async () => {
      const traceId1 = randomUUID();
      const traceId2 = randomUUID();
      const traceId3 = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;
      const userId = randomUUID();

      // Create observations with SAME start_time but different trace_ids
      // This tests the xxHash32(trace_id) ordering component
      const obs1 = createEvent({
        trace_id: traceId1,
        project_id: projectId,
        name: "same-time-obs-1",
        type: "GENERATION",
        level: "DEFAULT",
        user_id: userId,
        start_time: timeValue,
      });

      const obs2 = createEvent({
        trace_id: traceId2,
        project_id: projectId,
        name: "same-time-obs-2",
        type: "GENERATION",
        level: "DEFAULT",
        user_id: userId,
        start_time: timeValue, // Same time
      });

      const obs3 = createEvent({
        trace_id: traceId3,
        project_id: projectId,
        name: "same-time-obs-3",
        type: "GENERATION",
        level: "DEFAULT",
        user_id: userId,
        start_time: timeValue, // Same time
      });

      await createEventsCh([obs1, obs2, obs3]);

      // Fetch first page
      const page1 = await getObservations(
        `/api/public/v2/observations?userId=${userId}&limit=2&fromStartTime=${new Date(timeValue / 1000).toISOString()}&toStartTime=${new Date(timeValue / 1000 + 1000).toISOString()}`,
      );

      expect(page1.status).toBe(200);
      expect(page1.body.data.length).toBe(2);

      const page1Ids = page1.body.data.map((obs: any) => obs.id);

      // Fetch second page using cursor
      const page2 = await getObservations(
        `/api/public/v2/observations?userId=${userId}&limit=2&cursor=${page1.body.meta.cursor}`,
      );

      expect(page2.status).toBe(200);
      expect(page2.body.data.length).toBeGreaterThan(0);

      const page2Ids = page2.body.data.map((obs: any) => obs.id);

      // Verify no overlap (tests that xxHash32 ordering works)
      const overlap = page1Ids.filter((id: string) => page2Ids.includes(id));
      expect(overlap.length).toBe(0);

      // Verify that all events were fetched
      expect([...page1Ids, ...page2Ids].sort()).toEqual(
        [obs1, obs2, obs3].map((o) => o.span_id).sort(),
      );
    });

    it("should work with cursor and other filters", async () => {
      const traceId = randomUUID();
      const timestamp = new Date();
      const timeValue = timestamp.getTime() * 1000;

      // Create observations with specific type
      const observations = [];
      for (let i = 0; i < 4; i++) {
        const obsId = randomUUID();
        observations.push(
          createEvent({
            id: obsId,
            span_id: obsId,
            trace_id: traceId,
            project_id: projectId,
            name: `cursor-filter-obs-${i}`,
            type: "SPAN", // All same type
            level: "DEFAULT",
            start_time: timeValue + i * 1000 * 1000,
          }),
        );
      }

      await createEventsCh(observations);

      // Fetch first page with type filter
      const page1 = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&type=SPAN&limit=2`,
      );

      expect(page1.status).toBe(200);
      expect(page1.body.data.length).toBe(2);
      expect(page1.body.data.every((obs: any) => obs.type === "SPAN")).toBe(
        true,
      );
      expect(page1.body.meta.cursor).toBeDefined();

      // Fetch second page with same filter and cursor
      const page2 = await getObservations(
        `/api/public/v2/observations?traceId=${traceId}&type=SPAN&limit=2&cursor=${page1.body.meta.cursor}`,
      );

      expect(page2.status).toBe(200);
      expect(page2.body.data.length).toBe(2);
      expect(page2.body.data.every((obs: any) => obs.type === "SPAN")).toBe(
        true,
      );

      // Verify no overlap
      const page1Ids = page1.body.data.map((obs: any) => obs.id);
      const page2Ids = page2.body.data.map((obs: any) => obs.id);
      const overlap = page1Ids.filter((id: string) => page2Ids.includes(id));
      expect(overlap.length).toBe(0);
    });

    it("should reject invalid cursor format", async () => {
      const response = await getRaw(
        `/api/public/v2/observations?fields=id&cursor=invalid-base64-string`,
      );

      // Should fail validation
      expect(response.status).toBe(400);
      expect(response.body).toHaveProperty("message");
      expect(
        (response.body as unknown as { message: string }).message,
      ).toContain("Invalid cursor format");
    });
  });
});