Skip to content

Comments

Store match metadata#876

Merged
elie222 merged 3 commits intomainfrom
feat/store-reasoning-metadata
Oct 29, 2025
Merged

Store match metadata#876
elie222 merged 3 commits intomainfrom
feat/store-reasoning-metadata

Conversation

@elie222
Copy link
Owner

@elie222 elie222 commented Oct 29, 2025

Summary by CodeRabbit

Release v2.17.12

  • New Features

    • Implemented comprehensive rule match reason tracking with metadata storage for improved visibility into AI-based rule selection decisions.
  • Bug Fixes

    • Fixed empty feedback messages when no rules are available for evaluation or when AI rule selection determines no matches.
    • Corrected date-based sorting behavior for messages with missing date metadata.

@vercel
Copy link

vercel bot commented Oct 29, 2025

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

Project Deployment Preview Updated (UTC)
inbox-zero Ready Ready Preview Oct 29, 2025 10:15am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 29, 2025

Walkthrough

This PR adds match metadata persistence to executed rules by introducing a new matchMetadata JSON column to the database schema, implementing serialization logic to convert match reasons to storable format, and updating the AI rule-choosing pipeline to populate this field with detailed match information. Tests and utilities are updated accordingly, with version bumped to v2.17.12.

Changes

Cohort / File(s) Summary
Database Schema & Migration
apps/web/prisma/migrations/20251024092349_match_metadata/migration.sql, apps/web/prisma/schema.prisma
Added matchMetadata JSONB column to ExecutedRule table; updated Prisma schema with new optional field and explicit relation to Rule model.
Match Metadata Serialization
apps/web/utils/ai/choose-rule/types.ts
Introduced SerializedMatchReason type with variants (STATIC, LEARNED_PATTERN, AI, PRESET) and serializeMatchReasons() utility function to convert MatchReason[] to storable format.
AI Rule-Choosing Pipeline
apps/web/utils/ai/choose-rule/ai-choose-rule.ts, apps/web/utils/ai/choose-rule/match-rules.ts, apps/web/utils/ai/choose-rule/run-rules.ts
Updated to track match reasons throughout pipeline; ai-choose-rule.ts now returns meaningful reason strings instead of empty ones; match-rules.ts captures AI-based matches; run-rules.ts populates matchMetadata on executed rules using serialized match reasons.
Tests & Test Helpers
apps/web/__tests__/ai-choose-rule.test.ts, apps/web/__tests__/helpers.ts
Updated test expectations to reflect non-empty reason strings; refactored helper to use nullish coalescing for multiRuleSelectionEnabled override.
Utilities & Version
apps/web/utils/date.ts, version.txt
Fixed sortByInternalDate to explicitly check for internalDate presence before computing timestamp; bumped version from v2.17.11 to v2.17.12.

Sequence Diagram

sequenceDiagram
    participant App as Application
    participant AI as AI Rule Chooser
    participant Matcher as Match Rules
    participant Runner as Run Rules
    participant DB as Prisma/Database

    App->>AI: Choose rule for message
    AI->>Matcher: Find matching rules
    alt Rules Match
        Matcher->>Matcher: Generate matchReasons<br/>(type, source)
        Matcher-->>AI: Return matched rule +<br/>matchReasons
        AI-->>Runner: Execute rule +<br/>matchReasons
        Runner->>Runner: serializeMatchReasons()
        Runner->>DB: Create ExecutedRule with<br/>matchMetadata (serialized)
        DB-->>Runner: Persisted
    else No Rules Match
        Matcher->>Matcher: Capture AI reasoning
        Matcher-->>AI: No matches, reasoning
        AI-->>Runner: Skip execution +<br/>reason string
        Runner->>DB: Create ExecutedRule<br/>status=SKIPPED,<br/>matchMetadata=undefined
        DB-->>Runner: Persisted
    end
    Runner-->>App: Rule applied/skipped
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

  • Database changes: Verify migration script and schema alignment; ensure matchMetadata JSON structure compatibility with serialization logic.
  • Serialization logic in types.ts: Review mapping of MatchReason variants to SerializedMatchReason; confirm all reason types are handled and edge cases (null/undefined inputs) are addressed.
  • Pipeline integration in run-rules.ts: Ensure serializeMatchReasons() is correctly applied in both matched and skipped paths; verify matchMetadata field usage is consistent.
  • Test updates: Confirm new reason string expectations align with implementation; verify helper refactoring preserves intended behavior.

Possibly related PRs

  • PR #312: Both PRs modify the AI rule-matching pipeline (types, match-rules.ts, run-rules.ts) and touch serialization/storage of match reasons.
  • PR #837: Both PRs modify apps/web/utils/ai/choose-rule/run-rules.ts for database persistence of rule execution records.
  • PR #854: Both PRs enhance the AI rule-matching pipeline and update match reason persistence in the database.

Poem

🐰 Hop along, dear metadata!
Stored in JSON, neat and tidy,
Match reasons now have places to hide,
No more empty strings to confide,
The rules know why they were applied! 🎯✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "Store match metadata" accurately and directly describes the primary change in this pull request. The changeset introduces a new matchMetadata field to the ExecutedRule database model, creates serialization logic (SerializedMatchReason type and serializeMatchReasons function) to convert match reasons into a storable format, and updates the rule execution logic to populate this field when rules are matched or skipped. The title is concise, clear, and specific enough that a reviewer scanning the commit history would immediately understand the main objective without ambiguity.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/store-reasoning-metadata

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 and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
apps/web/utils/ai/choose-rule/run-rules.ts (1)

98-98: Consider omitting the field instead of explicit undefined.

Since matchMetadata is an optional field in the Prisma schema, explicitly setting it to undefined has the same effect as omitting it from the data object. While the current approach is clear about intent, omitting the field is more concise:

         data: {
           threadId: message.threadId,
           messageId: message.id,
           automated: true,
           reason,
-          matchMetadata: undefined,
           status: ExecutedRuleStatus.SKIPPED,
           emailAccount: { connect: { id: emailAccount.id } },
         },

Either approach is valid; the current code is more explicit while omitting is more idiomatic for optional fields.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f085fea and bcae074.

📒 Files selected for processing (10)
  • apps/web/__tests__/ai-choose-rule.test.ts (1 hunks)
  • apps/web/__tests__/helpers.ts (1 hunks)
  • apps/web/prisma/migrations/20251024092349_match_metadata/migration.sql (1 hunks)
  • apps/web/prisma/schema.prisma (1 hunks)
  • apps/web/utils/ai/choose-rule/ai-choose-rule.ts (2 hunks)
  • apps/web/utils/ai/choose-rule/match-rules.ts (2 hunks)
  • apps/web/utils/ai/choose-rule/run-rules.ts (3 hunks)
  • apps/web/utils/ai/choose-rule/types.ts (1 hunks)
  • apps/web/utils/date.ts (1 hunks)
  • version.txt (1 hunks)
🧰 Additional context used
📓 Path-based instructions (17)
apps/web/**/*.{ts,tsx}

📄 CodeRabbit inference engine (apps/web/CLAUDE.md)

apps/web/**/*.{ts,tsx}: Use TypeScript with strict null checks
Path aliases: Use @/ for imports from project root
Use proper error handling with try/catch blocks
Format code with Prettier
Leverage TypeScript inference for better DX

Files:

  • apps/web/__tests__/helpers.ts
  • apps/web/utils/ai/choose-rule/types.ts
  • apps/web/utils/ai/choose-rule/ai-choose-rule.ts
  • apps/web/utils/ai/choose-rule/match-rules.ts
  • apps/web/utils/date.ts
  • apps/web/utils/ai/choose-rule/run-rules.ts
  • apps/web/__tests__/ai-choose-rule.test.ts
!{.cursor/rules/*.mdc}

📄 CodeRabbit inference engine (.cursor/rules/cursor-rules.mdc)

Never place rule files in the project root, in subdirectories outside .cursor/rules, or in any other location

Files:

  • apps/web/__tests__/helpers.ts
  • apps/web/utils/ai/choose-rule/types.ts
  • version.txt
  • apps/web/utils/ai/choose-rule/ai-choose-rule.ts
  • apps/web/utils/ai/choose-rule/match-rules.ts
  • apps/web/prisma/migrations/20251024092349_match_metadata/migration.sql
  • apps/web/prisma/schema.prisma
  • apps/web/utils/date.ts
  • apps/web/utils/ai/choose-rule/run-rules.ts
  • apps/web/__tests__/ai-choose-rule.test.ts
**/*.ts

📄 CodeRabbit inference engine (.cursor/rules/form-handling.mdc)

**/*.ts: The same validation should be done in the server action too
Define validation schemas using Zod

Files:

  • apps/web/__tests__/helpers.ts
  • apps/web/utils/ai/choose-rule/types.ts
  • apps/web/utils/ai/choose-rule/ai-choose-rule.ts
  • apps/web/utils/ai/choose-rule/match-rules.ts
  • apps/web/utils/date.ts
  • apps/web/utils/ai/choose-rule/run-rules.ts
  • apps/web/__tests__/ai-choose-rule.test.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/logging.mdc)

**/*.{ts,tsx}: Use createScopedLogger for logging in backend TypeScript files
Typically add the logger initialization at the top of the file when using createScopedLogger
Only use .with() on a logger instance within a specific function, not for a global logger

Import Prisma in the project using import prisma from "@/utils/prisma";

**/*.{ts,tsx}: Don't use TypeScript enums.
Don't use TypeScript const enum.
Don't use the TypeScript directive @ts-ignore.
Don't use primitive type aliases or misleading types.
Don't use empty type parameters in type aliases and interfaces.
Don't use any or unknown as type constraints.
Don't use implicit any type on variable declarations.
Don't let variables evolve into any type through reassignments.
Don't use non-null assertions with the ! postfix operator.
Don't misuse the non-null assertion operator (!) in TypeScript files.
Don't use user-defined types.
Use as const instead of literal types and type annotations.
Use export type for types.
Use import type for types.
Don't declare empty interfaces.
Don't merge interfaces and classes unsafely.
Don't use overload signatures that aren't next to each other.
Use the namespace keyword instead of the module keyword to declare TypeScript namespaces.
Don't use TypeScript namespaces.
Don't export imported variables.
Don't add type annotations to variables, parameters, and class properties that are initialized with literal expressions.
Don't use parameter properties in class constructors.
Use either T[] or Array consistently.
Initialize each enum member value explicitly.
Make sure all enum members are literal values.

Files:

  • apps/web/__tests__/helpers.ts
  • apps/web/utils/ai/choose-rule/types.ts
  • apps/web/utils/ai/choose-rule/ai-choose-rule.ts
  • apps/web/utils/ai/choose-rule/match-rules.ts
  • apps/web/utils/date.ts
  • apps/web/utils/ai/choose-rule/run-rules.ts
  • apps/web/__tests__/ai-choose-rule.test.ts
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/ultracite.mdc)

**/*.{js,jsx,ts,tsx}: Don't use elements in Next.js projects.
Don't use elements in Next.js projects.
Don't use namespace imports.
Don't access namespace imports dynamically.
Don't use global eval().
Don't use console.
Don't use debugger.
Don't use var.
Don't use with statements in non-strict contexts.
Don't use the arguments object.
Don't use consecutive spaces in regular expression literals.
Don't use the comma operator.
Don't use unnecessary boolean casts.
Don't use unnecessary callbacks with flatMap.
Use for...of statements instead of Array.forEach.
Don't create classes that only have static members (like a static namespace).
Don't use this and super in static contexts.
Don't use unnecessary catch clauses.
Don't use unnecessary constructors.
Don't use unnecessary continue statements.
Don't export empty modules that don't change anything.
Don't use unnecessary escape sequences in regular expression literals.
Don't use unnecessary labels.
Don't use unnecessary nested block statements.
Don't rename imports, exports, and destructured assignments to the same name.
Don't use unnecessary string or template literal concatenation.
Don't use String.raw in template literals when there are no escape sequences.
Don't use useless case statements in switch statements.
Don't use ternary operators when simpler alternatives exist.
Don't use useless this aliasing.
Don't initialize variables to undefined.
Don't use the void operators (they're not familiar).
Use arrow functions instead of function expressions.
Use Date.now() to get milliseconds since the Unix Epoch.
Use .flatMap() instead of map().flat() when possible.
Use literal property access instead of computed property access.
Don't use parseInt() or Number.parseInt() when binary, octal, or hexadecimal literals work.
Use concise optional chaining instead of chained logical expressions.
Use regular expression literals instead of the RegExp constructor when possible.
Don't use number literal object member names th...

Files:

  • apps/web/__tests__/helpers.ts
  • apps/web/utils/ai/choose-rule/types.ts
  • apps/web/utils/ai/choose-rule/ai-choose-rule.ts
  • apps/web/utils/ai/choose-rule/match-rules.ts
  • apps/web/utils/date.ts
  • apps/web/utils/ai/choose-rule/run-rules.ts
  • apps/web/__tests__/ai-choose-rule.test.ts
!pages/_document.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/ultracite.mdc)

!pages/_document.{js,jsx,ts,tsx}: Don't import next/document outside of pages/_document.jsx in Next.js projects.
Don't import next/document outside of pages/_document.jsx in Next.js projects.

Files:

  • apps/web/__tests__/helpers.ts
  • apps/web/utils/ai/choose-rule/types.ts
  • version.txt
  • apps/web/utils/ai/choose-rule/ai-choose-rule.ts
  • apps/web/utils/ai/choose-rule/match-rules.ts
  • apps/web/prisma/migrations/20251024092349_match_metadata/migration.sql
  • apps/web/prisma/schema.prisma
  • apps/web/utils/date.ts
  • apps/web/utils/ai/choose-rule/run-rules.ts
  • apps/web/__tests__/ai-choose-rule.test.ts
apps/web/__tests__/**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/llm.mdc)

Place LLM-specific tests under apps/web/tests/

Files:

  • apps/web/__tests__/helpers.ts
  • apps/web/__tests__/ai-choose-rule.test.ts
**/__tests__/**

📄 CodeRabbit inference engine (.cursor/rules/testing.mdc)

Place AI tests in the __tests__ directory and exclude them from the default test run (they use a real LLM)

Files:

  • apps/web/__tests__/helpers.ts
  • apps/web/__tests__/ai-choose-rule.test.ts
apps/web/utils/**

📄 CodeRabbit inference engine (.cursor/rules/project-structure.mdc)

Create utility functions in utils/ folder for reusable logic

Files:

  • apps/web/utils/ai/choose-rule/types.ts
  • apps/web/utils/ai/choose-rule/ai-choose-rule.ts
  • apps/web/utils/ai/choose-rule/match-rules.ts
  • apps/web/utils/date.ts
  • apps/web/utils/ai/choose-rule/run-rules.ts
apps/web/utils/**/*.ts

📄 CodeRabbit inference engine (.cursor/rules/project-structure.mdc)

apps/web/utils/**/*.ts: Use lodash utilities for common operations (arrays, objects, strings)
Import specific lodash functions to minimize bundle size

Files:

  • apps/web/utils/ai/choose-rule/types.ts
  • apps/web/utils/ai/choose-rule/ai-choose-rule.ts
  • apps/web/utils/ai/choose-rule/match-rules.ts
  • apps/web/utils/date.ts
  • apps/web/utils/ai/choose-rule/run-rules.ts
apps/web/utils/ai/**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/llm.mdc)

apps/web/utils/ai/**/*.{ts,tsx}: Place main LLM feature implementations under apps/web/utils/ai/
LLM feature functions should follow the provided TypeScript pattern (separate system/user prompts, use createGenerateObject, Zod schema validation, early validation, return result.object)
Keep system prompts and user prompts separate
System prompt should define the LLM's role and task specifications
User prompt should contain the actual data and context
Always define a Zod schema for response validation
Make Zod schemas as specific as possible to guide LLM output
Use descriptive scoped loggers for each feature
Log inputs and outputs with appropriate log levels and include relevant context
Implement early returns for invalid inputs
Use proper error types and logging for failures
Implement fallbacks for AI failures
Add retry logic for transient failures using withRetry
Use XML-like tags to structure data in prompts
Remove excessive whitespace and truncate long inputs in prompts
Format prompt data consistently across similar functions
Use TypeScript types for all parameters and return values in LLM features
Define clear interfaces for complex input/output structures in LLM features

Files:

  • apps/web/utils/ai/choose-rule/types.ts
  • apps/web/utils/ai/choose-rule/ai-choose-rule.ts
  • apps/web/utils/ai/choose-rule/match-rules.ts
  • apps/web/utils/ai/choose-rule/run-rules.ts
apps/web/utils/{ai,llms}/**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/llm.mdc)

Keep related AI functions co-located and extract common patterns into utilities; document complex AI logic with clear comments

Files:

  • apps/web/utils/ai/choose-rule/types.ts
  • apps/web/utils/ai/choose-rule/ai-choose-rule.ts
  • apps/web/utils/ai/choose-rule/match-rules.ts
  • apps/web/utils/ai/choose-rule/run-rules.ts
apps/web/prisma/schema.prisma

📄 CodeRabbit inference engine (.cursor/rules/prisma.mdc)

The Prisma schema file must be located at apps/web/prisma/schema.prisma

Files:

  • apps/web/prisma/schema.prisma
**/*.test.{ts,js}

📄 CodeRabbit inference engine (.cursor/rules/security.mdc)

Include security tests in your test suites to verify authentication, authorization, and error handling.

Files:

  • apps/web/__tests__/ai-choose-rule.test.ts
**/*.{test,spec}.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/ultracite.mdc)

**/*.{test,spec}.{js,jsx,ts,tsx}: Don't use export or module.exports in test files.
Don't use focused tests.
Don't use disabled tests.
Make sure the assertion function, like expect, is placed inside an it() function call.
Don't nest describe() blocks too deeply in test files.
Don't use focused tests.
Don't use disabled tests.
Don't use export or module.exports in test files.

Files:

  • apps/web/__tests__/ai-choose-rule.test.ts
**/*.test.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/testing.mdc)

**/*.test.{ts,tsx}: Use Vitest (vitest) as the testing framework
Colocate tests next to the file under test (e.g., dir/format.ts with dir/format.test.ts)
In tests, mock the server-only module with vi.mock("server-only", () => ({}));
When testing code that uses Prisma, mock it with vi.mock("@/utils/prisma") and use the mock from @/utils/__mocks__/prisma
Use provided helpers for mocks: import { getEmail, getEmailAccount, getRule } from @/__tests__/helpers
Each test should be independent
Use descriptive test names
Mock external dependencies in tests
Clean up mocks between tests (e.g., vi.clearAllMocks() in beforeEach)
Avoid testing implementation details; focus on observable behavior
Do not mock the Logger

Files:

  • apps/web/__tests__/ai-choose-rule.test.ts
apps/web/__tests__/**/*.test.ts

📄 CodeRabbit inference engine (.cursor/rules/llm-test.mdc)

apps/web/__tests__/**/*.test.ts: Place all LLM-related tests under apps/web/tests/
Use Vitest in LLM tests and import { describe, expect, test, vi, beforeEach } from "vitest"
Mock the Next.js server runtime marker by adding vi.mock("server-only", () => ({})) in LLM tests
Gate LLM tests behind RUN_AI_TESTS using describe.runIf(process.env.RUN_AI_TESTS === "true")
Call vi.clearAllMocks() in a beforeEach for LLM tests
Set a TIMEOUT of 15_000ms for LLM-related tests and pass it to long-running tests/describe blocks
Create helper functions for common test data (e.g., getUser, getTestData) to reduce duplication
Include standard test cases: happy path, error handling, edge cases (empty/null), different user configurations, and various input formats
Use console.debug to log generated LLM content for inspection (e.g., console.debug("Generated content:\n", result.content))
Do not mock the actual LLM call in these tests; exercise real LLM integrations
Test both AI and non-AI paths, including cases where no AI processing is required
Prefer existing helpers from @/tests/helpers.ts (getEmailAccount, getEmail, getRule, getMockMessage, getMockExecutedRule) over custom helpers

Files:

  • apps/web/__tests__/ai-choose-rule.test.ts
🧠 Learnings (2)
📚 Learning: 2025-06-23T12:26:53.882Z
Learnt from: CR
PR: elie222/inbox-zero#0
File: .cursor/rules/prisma.mdc:0-0
Timestamp: 2025-06-23T12:26:53.882Z
Learning: In this project, Prisma should be imported using 'import prisma from "@/utils/prisma";' in TypeScript files.

Applied to files:

  • apps/web/utils/ai/choose-rule/run-rules.ts
📚 Learning: 2025-10-02T23:23:48.064Z
Learnt from: CR
PR: elie222/inbox-zero#0
File: .cursor/rules/llm-test.mdc:0-0
Timestamp: 2025-10-02T23:23:48.064Z
Learning: Applies to apps/web/__tests__/**/*.test.ts : Test both AI and non-AI paths, including cases where no AI processing is required

Applied to files:

  • apps/web/__tests__/ai-choose-rule.test.ts
🧬 Code graph analysis (1)
apps/web/utils/ai/choose-rule/run-rules.ts (1)
apps/web/utils/ai/choose-rule/types.ts (1)
  • serializeMatchReasons (61-90)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: cubic · AI code reviewer
🔇 Additional comments (13)
apps/web/__tests__/helpers.ts (1)

14-14: LGTM!

The nullish coalescing operator correctly preserves explicit boolean values from overrides while defaulting to false when undefined or null.

apps/web/utils/ai/choose-rule/match-rules.ts (2)

78-83: LGTM!

Cold email matches are now correctly tagged with AI-type match reasons, enabling proper metadata tracking for this rule type.


353-354: LGTM!

The AI match reason case provides a clear, user-facing description consistent with other match types.

apps/web/__tests__/ai-choose-rule.test.ts (1)

20-20: LGTM!

Test expectation correctly validates the new behavior where an empty rules array returns a descriptive reason instead of an empty string.

apps/web/prisma/migrations/20251024092349_match_metadata/migration.sql (1)

1-2: LGTM!

The migration correctly adds a nullable JSONB column for storing structured match metadata. JSONB is the appropriate PostgreSQL type for JSON data with efficient querying support.

apps/web/utils/ai/choose-rule/ai-choose-rule.ts (2)

32-32: LGTM!

Returning a descriptive reason when no rules are provided improves observability and aligns with test expectations.


41-46: LGTM!

Populating the reason field with AI reasoning (or a descriptive fallback) when no match is found provides better observability and debugging information.

apps/web/prisma/schema.prisma (2)

480-480: LGTM!

The optional matchMetadata JSON field is appropriately typed for storing structured match information. The nullable constraint correctly handles existing records and cases without metadata.


483-484: Verify the implicit onDelete behavior for the rule relation.

Adding the explicit relation improves type safety and query capabilities. However, no onDelete action is specified. Ensure the default behavior (likely SetNull since ruleId is nullable) aligns with your expectations for when rules are deleted.

apps/web/utils/ai/choose-rule/types.ts (2)

40-56: LGTM! Well-structured serialization type.

The SerializedMatchReason type properly converts the Prisma-based MatchReason into a JSON-safe format suitable for database storage. The discriminated union structure is clear, and the conversion of SystemType enum to string is correct for serialization.


61-90: LGTM! Clean and type-safe serialization logic.

The serializeMatchReasons function correctly handles all MatchReason variants with an exhaustive switch statement. The early return for empty/falsy input and the selective field extraction (especially for LEARNED_PATTERN) ensure clean, minimal JSON storage.

apps/web/utils/ai/choose-rule/run-rules.ts (2)

12-12: LGTM! Correct import of serialization function.

The import of serializeMatchReasons is properly placed and uses the correct syntax for importing a function (as opposed to import type used for type imports on line 11).


264-264: LGTM! Proper integration of match metadata serialization.

The matchMetadata field is correctly populated using serializeMatchReasons(matchReasons). The function handles the optional matchReasons parameter gracefully and returns a value compatible with the Prisma Json? field type, ensuring match reasoning is persisted to the database.

Comment on lines +98 to +103
const aTime = a.internalDate
? internalDateToDate(a.internalDate).getTime()
: 0;
const bTime = b.internalDate
? internalDateToDate(b.internalDate).getTime()
: 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Verify the behavior change for items without internalDate.

This changes sort behavior:

  • Previously: Missing internalDate was treated as current date (via internalDateToDate(undefined) returning new Date())
  • Now: Missing internalDate is treated as epoch (timestamp 0)

Items without internalDate will now sort to the beginning with asc or end with desc, rather than being treated as "now". Ensure this is intentional and that callers expecting the old behavior are updated.


🏁 Script executed:

#!/bin/bash
# Find all usages of sortByInternalDate to assess impact
rg -n "sortByInternalDate" --type=ts -C3

Length of output: 3256




🏁 Script executed:

#!/bin/bash
# Get full context on internalDateToDate function
rg -n "function internalDateToDate|export.*internalDateToDate" --type=ts -A10

Length of output: 729


🏁 Script executed:

#!/bin/bash
# Check what happens with undefined/null internalDate in actual data flow
rg -n "internalDate\?" --type=ts -B2 -A2 apps/web/utils/date.ts

Length of output: 369


🏁 Script executed:

#!/bin/bash
# Get more context around the outbound.ts logic to understand the latestMessage selection
rg -n "actualLatestMessage" --type=ts -B5 -A5 apps/web/utils/reply-tracker/outbound.ts

Length of output: 779


🏁 Script executed:

#!/bin/bash
# Check if there's validation or filtering for internalDate before sorting
rg -n "internalDate" --type=ts apps/web/utils/reply-tracker/ -B2 -A2 | head -60

Length of output: 1811


🏁 Script executed:

#!/bin/bash
# Get the complete sortByInternalDate function to see the exact comparison logic
cat apps/web/utils/date.ts | sed -n '94,110p'

Length of output: 420




🏁 Script executed:

#!/bin/bash
# Check where threadMessages parameter comes from and if internalDate is guaranteed
rg -n "getLatestMessageFromThread|isLatest.*boolean" --type=ts -B10 apps/web/utils/reply-tracker/outbound.ts | head -100

Length of output: 277


🏁 Script executed:

#!/bin/bash
# Look for where messages are queried/loaded and if internalDate is filtered
rg -n "threadMessages|\.include|\.select" --type=ts apps/web/utils/reply-tracker/ -B2 -A2 | head -80

Length of output: 5901


🏁 Script executed:

#!/bin/bash
# Check the ParsedMessage type to understand internalDate requirement
rg -n "type ParsedMessage|interface ParsedMessage" --type=ts -A15

Length of output: 5774


🏁 Script executed:

#!/bin/bash
# Search for any validation or filtering of internalDate before sorting
rg -n "filter.*internalDate|internalDate.*filter" --type=ts -C3

Length of output: 0


Fix the sort logic or validate internalDate presence before sorting.

This change introduces a critical bug in how "latest" messages are identified. Both outbound.ts (line 117-118) and handle-conversation-status.ts (line 50+) assume that in an ascending sort, the last element is the latest message. This assumption breaks when any message lacks internalDate:

  • Old behavior: Missing internalDate → treated as current date → sorted to end (correct latest)
  • New behavior: Missing internalDate → timestamp 0 → sorted to beginning (incorrect latest)

Since ParsedMessage.internalDate is optional and there's no validation ensuring it exists, any missing date causes actualLatestMessage to be wrong, leading to incorrect "not the latest" warnings or skipped processing.

Required fixes:

  1. Either ensure internalDate is always populated before sorting, or
  2. Change the "latest" detection logic to handle missing dates (e.g., use Date field or fallback to date header), or
  3. Revert to treating missing internalDate as current date if that was intentional
🤖 Prompt for AI Agents
In apps/web/utils/date.ts around lines 98 to 103, the comparator sets missing
internalDate to 0 which pushes messages without internalDate to the beginning
(breaking callers that expect the last item to be the latest); fix by computing
a fallback timestamp for missing internalDate instead of 0 — either populate
internalDate before sorting (use the message Date header or parsed date) or
update the comparator to derive time = internalDate ?
internalDateToDate(internalDate).getTime() : (DateHeaderIfPresentParsedToMs) ||
Date.now(); ensure the fallback makes messages with missing internalDate sort as
latest so the last element in an ascending sort is the actual latest message and
update any related callers if you change the contract.

Copy link
Contributor

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

Choose a reason for hiding this comment

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

1 issue found across 10 files

Prompt for AI agents (all 1 issues)

Understand the root cause of the following 1 issues and fix them.


<file name="apps/web/utils/date.ts">

<violation number="1" location="apps/web/utils/date.ts:98">
Defaulting falsy internalDate values to 0 changes the comparator semantics: previously internalDateToDate(undefined) returned the current time, so records without an internalDate still sorted chronologically. Now they sort as the Unix epoch, pushing them to the front of ascending lists (and the end of descending ones) and breaking message ordering for those cases.</violation>
</file>

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

return (a: T, b: T): number => {
const aTime = internalDateToDate(a.internalDate).getTime() || 0;
const bTime = internalDateToDate(b.internalDate).getTime() || 0;
const aTime = a.internalDate
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Oct 29, 2025

Choose a reason for hiding this comment

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

Defaulting falsy internalDate values to 0 changes the comparator semantics: previously internalDateToDate(undefined) returned the current time, so records without an internalDate still sorted chronologically. Now they sort as the Unix epoch, pushing them to the front of ascending lists (and the end of descending ones) and breaking message ordering for those cases.

Prompt for AI agents
Address the following comment on apps/web/utils/date.ts at line 98:

<comment>Defaulting falsy internalDate values to 0 changes the comparator semantics: previously internalDateToDate(undefined) returned the current time, so records without an internalDate still sorted chronologically. Now they sort as the Unix epoch, pushing them to the front of ascending lists (and the end of descending ones) and breaking message ordering for those cases.</comment>

<file context>
@@ -95,8 +95,12 @@ export function sortByInternalDate&lt;T extends { internalDate?: string | null }&gt;(
   return (a: T, b: T): number =&gt; {
-    const aTime = internalDateToDate(a.internalDate).getTime() || 0;
-    const bTime = internalDateToDate(b.internalDate).getTime() || 0;
+    const aTime = a.internalDate
+      ? internalDateToDate(a.internalDate).getTime()
+      : 0;
</file context>
Fix with Cubic

Copy link
Contributor

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

Choose a reason for hiding this comment

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

No issues found across 10 files

@elie222 elie222 merged commit 89e2db6 into main Oct 29, 2025
20 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Nov 11, 2025
This was referenced Nov 24, 2025
@elie222 elie222 deleted the feat/store-reasoning-metadata branch December 18, 2025 23:04
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