Skip to content

feat(rpc): measure saved conversion journeys by cohort - #741

Merged
izadoesdev merged 2 commits into
stagingfrom
codex/insights-breakthrough-tools
Sep 6, 2026
Merged

feat(rpc): measure saved conversion journeys by cohort#741
izadoesdev merged 2 commits into
stagingfrom
codex/insights-breakthrough-tools

Conversation

@izadoesdev

@izadoesdev izadoesdev commented Sep 6, 2026

Copy link
Copy Markdown
Member

Saved funnel analytics could not answer whether a completion decline was concentrated among visitors entering from a particular browser, device, country or campaign. The native tools now accept a bounded read-only cohort and apply it through the existing authenticated goal/funnel query path, preserving the saved definition separately from the measured filters.

Eight fresh synthetic model invocations were audited: three baseline investigations could not measure the browser-specific ordered completions; all five candidate invocations identified Safari 100→20/500 with Chrome 80/500 unchanged, in two model turns each. Two extra candidate invocations were an orchestration mistake and are included, not selected away; their accepted drafts and token usage survive, but two overwritten timing wrappers do not. These are capability results, not proof of automatic discovery, causality or production latency.

Validation: nine tests against an isolated ClickHouse 25.10 instance cover ordered events, saved-filter intersection, tenant collisions, changing later-step context, cohort unions/exclusions, source breakdowns and goal counts. Native tool, RPC/cache and investigation repair-provenance regressions pass. An independent review found a test mock leak; replacing the global RPC mock with a restored scoped spy makes the new tests and existing dry-run/cancellation tests pass together. Root lint, all 33 workspace typechecks, and the full repository test suite pass. The first pre-push attempt also included an unshipped experimental test with a global mock; isolating that experiment from the shipping suite removed its interference. CI lint, types, tests, dashboard E2E, CodeQL and service health checks pass.

Entry cohorts identify visitors by their first-step context; subsequent steps may occur in another browser/session. Missing context and overlapping cohorts are not proof of zero or a partition of all visitors. No saved definitions, database schema or agent strategy change.

Scope: one conversion-analytics read capability. No dependencies. Known overlap: #680 changes goals bulkAnalytics; this changes individual getAnalytics and shared measurement provenance. The broader investigation-discovery experiment will land separately and be refreshed after this PR. AI-assisted maintainer contribution, independently reviewed.

Review resolution: the native forwarding test now uses SDK-inferred spy calls; fixture branches are explicit and new non-null assertions are removed. The warehouse insertion boundary retains unknown JSON values, which matches the ClickHouse transport rather than inventing another event schema. Test names/assertions distinguish the browser-only population from its intersection with the saved country filter, rejected websites assert zero RPC calls, and per-test mock defaults prevent contamination. Link analytics keeps its original non-cohort contract and explicitly rejects the new cohort input; the two native cohort endpoints return measured provenance. This avoids expanding an unrelated endpoint without population metadata.

@vercel

vercel Bot commented Sep 6, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
dashboard Ready Ready Preview Sep 6, 2026 11:49pm UTC
databuddy-status Ready Ready Preview Sep 6, 2026 11:49pm UTC
1 Skipped Deployment
Project Deployment Actions Updated
documentation Skipped Skipped Sep 6, 2026 11:49pm UTC

@coderabbitai

coderabbitai Bot commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Team

Run ID: e6dd4b99-aead-4563-9f76-c726299e68c7

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds bounded cohort filtering to saved goal and funnel analytics while preserving the saved definition separately from the measured definition.

  • Extends native AI tools and website-scoped RPC procedures with browser, device, country, referrer, and campaign cohorts.
  • Applies cohort filters to entry context and includes measured filters in analytics cache identity.
  • Updates Insights repair provenance to prefer the unmodified saved definition.
  • Adds RPC, tool, and ClickHouse integration coverage for cohort isolation and ordered conversions.
  • The implementation appears behaviorally coherent, but several changed tests violate explicit repository type-safety and formatting requirements.

Confidence Score: 4/5

The cohort behavior appears sound, but the explicit repository type-safety and formatting requirements must be satisfied before merging.

No behavioral or security failure remains; the accepted findings are confined to concrete repository-rule violations in the newly added and modified tests.

Files Needing Attention: packages/ai/src/ai/tools/cohort-read.test.ts, packages/rpc/src/lib/analytics-cohort.integration.test.ts, packages/rpc/src/routers/analytics-measurement.test.ts, apps/insights/src/investigation-flow.test.ts

Important Files Changed

Filename Overview
packages/shared/src/analytics-filters.ts Defines a strict, bounded schema for read-only analytics cohort fields and operators.
packages/rpc/src/routers/funnels.ts Applies cohorts to funnel entry analytics, separates saved provenance, and isolates cohort cache entries.
packages/rpc/src/routers/goals.ts Applies cohorts consistently to goal entrants and contextual completions while returning the saved definition.
packages/ai/src/ai/tools/funnels.ts Exposes optional funnel cohorts through native analytics tools and forwards them through the authorized RPC path.
packages/ai/src/ai/tools/goals.ts Exposes optional goal cohorts through the existing website-resolved RPC path.
apps/insights/src/agent.ts Uses saved definitions rather than cohort-augmented measurements when validating definition repairs.
packages/ai/src/ai/tools/cohort-read.test.ts Covers tool forwarding, schema restrictions, and tenancy, but introduces a prohibited unknown type.
packages/rpc/src/lib/analytics-cohort.integration.test.ts Adds broad ClickHouse cohort coverage but introduces prohibited broad types, nested ternaries, and a non-null assertion.
packages/rpc/src/routers/analytics-measurement.test.ts Verifies measurement provenance and cache isolation, with repository-rule violations in fixture construction.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[AI analytics tool] --> B[Resolve authorized website]
  B --> C[Goal or funnel RPC]
  C --> D[Load saved definition]
  D --> E[Combine saved filters with read-only cohort]
  E --> F[Build cohort-specific cache key]
  F --> G[Query ClickHouse analytics]
  G --> H[Return measurement and counts]
  D --> I[Return savedDefinition separately]
  H --> J[Insights investigation]
  I --> J
  J --> K[Validate repairs against saved definition]
Loading

Reviews (1): Last reviewed commit: "feat(rpc): measure saved conversion jour..." | Re-trigger Greptile

Comment thread packages/ai/src/ai/tools/cohort-read.test.ts Outdated
Comment thread apps/insights/src/investigation-flow.test.ts Outdated
Comment thread packages/rpc/src/lib/analytics-cohort.integration.test.ts Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 10 files

Architecture diagram
sequenceDiagram
    participant Agent as Intelligence Agent
    participant FunnelTool as Funnel Tools
    participant GoalTool as Goal Tools
    participant RPC as RPC Layer
    participant Cache as Analytics Cache
    participant DB as Database
    participant CH as ClickHouse

    Note over Agent,CH: Cohort-Based Saved Funnel/Goal Analytics Flow

    Agent->>FunnelTool: get_funnel_analytics(funnelId, dates, cohort)
    Agent->>GoalTool: get_goal_analytics(goalId, dates, cohort)
    
    Note over FunnelTool,GoalTool: Validate cohort schema (browser/device/country/campaign only, no tenant/step selectors)
    
    FunnelTool->>RPC: callRPCProcedure("funnels", "getAnalytics", {funnelId, websiteId, cohort})
    GoalTool->>RPC: callRPCProcedure("goals", "getAnalytics", {goalId, websiteId, cohort})
    
    Note over RPC: withWebsiteRead middleware validates tenant access
    
    alt Inaccessible website
        RPC-->>FunnelTool: Error: "not in this workspace"
        RPC-->>GoalTool: Error: "not in this workspace"
    else Valid tenant access
        RPC->>DB: Load saved funnel/goal definition from DB
        DB-->>RPC: Saved definition (filters, steps)
        RPC->>RPC: Build measurement definition
        Note over RPC: Combine saved filters + cohort filters (ANDed)
        RPC->>RPC: Cache key based on full measurement JSON
        
        alt Cache hit
            Cache-->>RPC: Cached analytics result
        else Cache miss
            RPC->>CH: processFunnelAnalytics(steps, combinedFilters, params)
            RPC->>CH: processGoalAnalytics(steps, combinedFilters, params, entrants)
            CH-->>RPC: Analytics metrics (entrants, completions, rate)
            RPC->>Cache: Store result with cohort-aware key
        end
        
        RPC-->>FunnelTool: {analytics, measurement, savedDefinition, cohort}
        RPC-->>GoalTool: {analytics, measurement, savedDefinition, cohort}
    end
    
    FunnelTool-->>Agent: Metrics with cohort applied
    GoalTool-->>Agent: Metrics with cohort applied
    
    Note over Agent: Validate measurement matches saved definition ID/website
    
    Agent->>Agent: Compare cohorts/periods via parallel calls
    
    opt Agent needs saved definition restored
        Agent->>Agent: Use savedDefinition field (without cohort filters)
    end
Loading

Shadow auto-approve: would not auto-approve because issues were found.

Re-trigger cubic

Comment thread packages/rpc/src/lib/analytics-cohort.integration.test.ts
Comment thread packages/ai/src/ai/tools/cohort-read.test.ts
Comment thread packages/rpc/src/routers/analytics-measurement.test.ts
Comment thread packages/rpc/src/routers/funnels.ts Outdated
Comment thread packages/ai/src/ai/tools/cohort-read.test.ts Outdated
@unkey-deploy

unkey-deploy Bot commented Sep 6, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Unkey Deploy

Name Status Preview Inspect Updated (UTC)
links (preview) Ready Visit Preview Inspect Sep 6, 2026 11:48pm

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 5 files (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Shadow auto-approve: would auto-approve. Adds optional read-only cohort segmentation to funnel and goal analytics tools and RPC, reusing the authenticated path and restricting the new schema to context fields. Bounded feature; no schema or breaking changes.

Re-trigger cubic

@izadoesdev
izadoesdev merged commit 737b485 into staging Sep 6, 2026
20 checks passed
@izadoesdev
izadoesdev deleted the codex/insights-breakthrough-tools branch September 6, 2026 23:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant