Skip to content

Improve metadata flushing efficiency by collapsing operations (fix #2104) #2106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ericallam
Copy link
Member

@ericallam ericallam commented May 24, 2025

This PR implements "operation collapsing" client-side to prevent run metadata flush failures because of too-large of a request body. Many operations, especially increment and decrement on a single key, can easily be "collapsed" into a single operation.

Copy link

changeset-bot bot commented May 24, 2025

🦋 Changeset detected

Latest commit: 3779960

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

coderabbitai bot commented May 24, 2025

Walkthrough

The changes introduce a new collapseOperations function that reduces metadata operation payload size by merging multiple operations on the same key—summing increments, keeping only the last set, and collapsing deletes—while preserving order-sensitive operations. This function is integrated into the metadata manager to collapse queued operations before flushing or returning them. The maximum allowed request body size for the run metadata update API route is increased from 1MB to 2MB. Debug logging is enhanced to include the applied operations during metadata updates. Extensive tests validate the correctness and robustness of the collapsing logic and its integration.

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3fab193 and 3779960.

📒 Files selected for processing (2)
  • apps/webapp/app/routes/api.v1.runs.$runId.metadata.ts (1 hunks)
  • packages/core/src/v3/runMetadata/operations.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • apps/webapp/app/routes/api.v1.runs.$runId.metadata.ts
  • packages/core/src/v3/runMetadata/operations.ts
⏰ Context from checks skipped due to timeout of 90000ms (25)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (10, 10)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (9, 10)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (8, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (8, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (7, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (7, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (6, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (6, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (5, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (5, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (4, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (4, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (3, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (3, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (2, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (2, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (1, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (1, 8)
  • GitHub Check: units / packages / 🧪 Unit Tests: Packages (1, 1)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - npm)
  • GitHub Check: Analyze (javascript-typescript)
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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: 2

🧹 Nitpick comments (1)
packages/core/test/standardMetadataManager.test.ts (1)

284-1087: Exceptional test suite for operation collapsing!

This test suite demonstrates excellent software engineering practices:

Consider breaking this large test suite into separate files for better organization (e.g., collapseOperations.test.ts for the pure logic tests and keeping integration tests here).

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e84ede9 and ca8b8a7.

📒 Files selected for processing (5)
  • apps/webapp/app/routes/api.v1.runs.$runId.metadata.ts (1 hunks)
  • apps/webapp/app/services/metadata/updateMetadata.server.ts (1 hunks)
  • packages/core/src/v3/runMetadata/manager.ts (6 hunks)
  • packages/core/src/v3/runMetadata/operations.ts (1 hunks)
  • packages/core/test/standardMetadataManager.test.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
packages/core/src/v3/runMetadata/operations.ts (1)
packages/core/src/v3/schemas/common.ts (2)
  • RunMetadataChangeOperation (53-60)
  • RunMetadataChangeOperation (62-62)
packages/core/src/v3/runMetadata/manager.ts (2)
packages/core/src/v3/runMetadata/types.ts (1)
  • RunMetadataUpdater (5-18)
packages/core/src/v3/runMetadata/operations.ts (1)
  • collapseOperations (169-236)
packages/core/test/standardMetadataManager.test.ts (1)
packages/core/src/v3/runMetadata/operations.ts (2)
  • applyMetadataOperations (10-130)
  • collapseOperations (169-236)
🪛 Biome (1.9.4)
packages/core/src/v3/runMetadata/operations.ts

[error] 186-187: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)

packages/core/src/v3/runMetadata/manager.ts

[error] 75-75: This aliasing of this is unnecessary.

Arrow functions inherits this from their enclosing scope.
Safe fix: Use this instead of an alias.

(lint/complexity/noUselessThisAlias)

⏰ Context from checks skipped due to timeout of 90000ms (25)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (10, 10)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (9, 10)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (8, 10)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (7, 10)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (6, 10)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (5, 10)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (4, 10)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (3, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (8, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (2, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (7, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (1, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (6, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (5, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (4, 8)
  • GitHub Check: units / packages / 🧪 Unit Tests: Packages (1, 1)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (3, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (2, 8)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (1, 8)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - npm)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (13)
packages/core/src/v3/runMetadata/operations.ts (1)

132-236: LGTM! Well-designed function with comprehensive documentation.

The collapseOperations function effectively addresses the payload size optimization goal by intelligently merging operations while preserving semantic correctness. The documentation is excellent and provides clear examples.

🧰 Tools
🪛 Biome (1.9.4)

[error] 186-187: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)

apps/webapp/app/services/metadata/updateMetadata.server.ts (1)

384-384: Great addition for debugging and observability.

Including the operations array in the debug logs provides valuable insight into the specific metadata changes being applied, which will be helpful for troubleshooting and monitoring.

packages/core/src/v3/runMetadata/manager.ts (4)

7-7: LGTM! Proper import of the collapseOperations function.

The import correctly brings in the new collapseOperations function alongside the existing applyMetadataOperations import.


356-366: Excellent integration of operation collapsing in flush method.

The implementation correctly applies collapseOperations to all three operation arrays before sending them to the API, which will significantly reduce payload sizes and improve performance.


422-425: Consistent application of operation collapsing in stopAndReturnLastFlush.

Good consistency in applying collapseOperations to all operation arrays when returning the last flush data, ensuring the same optimization benefits.


75-75: Static analysis false positive - this aliasing is necessary here.

The static analysis tool suggests removing the this aliasing, but this is actually necessary because the self variable is used within closure functions where this context could change. The current implementation is correct.

🧰 Tools
🪛 Biome (1.9.4)

[error] 75-75: This aliasing of this is unnecessary.

Arrow functions inherits this from their enclosing scope.
Safe fix: Use this instead of an alias.

(lint/complexity/noUselessThisAlias)

packages/core/test/standardMetadataManager.test.ts (7)

6-6: LGTM!

The import correctly brings in the necessary functions for testing operation collapsing functionality.


285-304: Well-structured test for increment collapsing!

The test correctly verifies that multiple increment operations are collapsed into a single operation with the summed value.


306-324: Good test for set operation collapsing!

The test correctly verifies that only the last set operation is retained when multiple sets target the same key.


350-378: Excellent test for append operation preservation!

The test correctly verifies that append operations are not collapsed, maintaining their order which is crucial for list operations.


380-430: Comprehensive test for mixed operations!

The test effectively covers the interaction between different operation types. The comment correctly explains why delete operations on non-existent keys are not queued.


591-815: Excellent comprehensive sanity checks!

These tests provide crucial verification that collapsed operations produce identical results to the original operations. The large-scale test (lines 756-814) effectively simulates the original issue scenario with 1000 operations, ensuring the solution scales properly.


817-1086: Outstanding edge case coverage!

This comprehensive suite of edge case tests demonstrates exceptional attention to detail. The tests cover:

  • Numeric edge cases (zero, negative, MAX_SAFE_INTEGER)
  • String edge cases (special characters, unicode, very long keys)
  • Type handling (null, undefined, mixed types, type changes)
  • Complex data structures (deeply nested paths, empty collections)
  • Conflicting operations

This level of testing significantly reduces the risk of bugs in production.

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.

2 participants