Skip to content

fix:soften longToNumber overflow to prevent stream teardown - #50

Open
Ishaan Kesarwani (IshaanXCoder) wants to merge 2 commits into
photon-hq:mainfrom
IshaanXCoder:fix/23-int64-stream-overflow
Open

fix:soften longToNumber overflow to prevent stream teardown#50
Ishaan Kesarwani (IshaanXCoder) wants to merge 2 commits into
photon-hq:mainfrom
IshaanXCoder:fix/23-int64-stream-overflow

Conversation

@IshaanXCoder

@IshaanXCoder Ishaan Kesarwani (IshaanXCoder) commented Jul 18, 2026

Copy link
Copy Markdown

Summary

this pr fixes #23

  • Add scripts/soften-int64.mjs: post-codegen script, replaces the throwing helper body (identical across all 9 generated files) with a lossy-but-non-throwing version.
  • Wire into generate so verify:codegen stays reproducible in CI.
  • No public API change — fields stay typed number.

View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.

Summary by CodeRabbit

  • Bug Fixes

    • Improved decoding of oversized 64-bit values to avoid failures and return finite numeric results.
    • Preserved nested response content when handling large sequence values.
  • Tests

    • Added regression tests covering overflow behavior for event responses and timestamps.
  • Chores

    • Updated the generation workflow to adjust tool PATH and automatically apply compatibility “softening” after code generation.

Copilot AI review requested due to automatic review settings July 18, 2026 21:50
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9da1c990-5e9c-4756-8bc8-f88d313a4e5f

📥 Commits

Reviewing files that changed from the base of the PR and between f15993b and 9dd765a.

📒 Files selected for processing (1)
  • packages/core/tests/long-to-number.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/core/tests/long-to-number.test.ts

📝 Walkthrough

Walkthrough

The code-generation workflow now patches generated longToNumber helpers to allow oversized int64 values, and regression tests cover decoding oversized sequence and timestamp fields without throwing.

Changes

Int64 overflow fix

Layer / File(s) Summary
Soften generated int64 conversions
scripts/soften-int64.mjs
Scans generated TypeScript files, replaces throwing longToNumber helpers, detects already-softened helpers, validates expected output, and logs patch counts.
Generation wiring and regression coverage
package.json, packages/core/tests/long-to-number.test.ts
Runs the softening script after buf generate and tests decoding oversized sequence and timestamp values.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: lingjueya

Poem

I’m a rabbit with numbers too wide,
No safe-integer thumps can divide.
The generator hops,
The decoder now stops
Throwing when long values arrive.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main fix: softening longToNumber overflow to avoid stream teardown.
Linked Issues check ✅ Passed The changes address #23 by preventing unsafe int64 decoding from throwing and adding regression coverage.
Out of Scope Changes check ✅ Passed The added script, generate hook, and tests all directly support the overflow fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@IshaanXCoder

Copy link
Copy Markdown
Author

hey LingJueYa~ (@LingJueYa) checkout this pr, let me know if we need any fixes/changes here

@coderabbitai coderabbitai 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.

🧹 Nitpick comments (1)
scripts/soften-int64.mjs (1)

49-55: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Prefer template literals over string concatenation.

As per coding guidelines, use template literals instead of string concatenation to format the error message.

♻️ Proposed refactor
-if (patched === 0 && alreadySoft === 0) {
-  throw new Error(
-    "soften-int64: found no longToNumber helper under " +
-      ROOT +
-      " — ts-proto's emitted shape may have changed; update THROWING_BODY."
-  );
-}
+if (patched === 0 && alreadySoft === 0) {
+  throw new Error(
+    `soften-int64: found no longToNumber helper under ${ROOT} — ts-proto's emitted shape may have changed; update THROWING_BODY.`
+  );
+}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@scripts/soften-int64.mjs` around lines 49 - 55, The error message in the
soften-int64 failure check should use a template literal instead of
concatenating strings. Update the Error construction within the
patched/alreadySoft guard, preserving the existing message content and ROOT
interpolation.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@scripts/soften-int64.mjs`:
- Around line 49-55: The error message in the soften-int64 failure check should
use a template literal instead of concatenating strings. Update the Error
construction within the patched/alreadySoft guard, preserving the existing
message content and ROOT interpolation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7a45b4f3-9bc2-4c8d-bea0-2ffcb92b390f

📥 Commits

Reviewing files that changed from the base of the PR and between 75d52ba and f15993b.

⛔ Files ignored due to path filters (9)
  • packages/core/src/generated/google/protobuf/descriptor.ts is excluded by !**/generated/**
  • packages/core/src/generated/google/protobuf/timestamp.ts is excluded by !**/generated/**
  • packages/core/src/generated/photon/imessage/v1/attachment_types.ts is excluded by !**/generated/**
  • packages/core/src/generated/photon/imessage/v1/chat_service.ts is excluded by !**/generated/**
  • packages/core/src/generated/photon/imessage/v1/event_service.ts is excluded by !**/generated/**
  • packages/core/src/generated/photon/imessage/v1/group_service.ts is excluded by !**/generated/**
  • packages/core/src/generated/photon/imessage/v1/location_types.ts is excluded by !**/generated/**
  • packages/core/src/generated/photon/imessage/v1/message_service.ts is excluded by !**/generated/**
  • packages/core/src/generated/photon/imessage/v1/poll_service.ts is excluded by !**/generated/**
📒 Files selected for processing (3)
  • package.json
  • packages/core/tests/long-to-number.test.ts
  • scripts/soften-int64.mjs
📜 Review details
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

**/*.{ts,tsx,js,jsx}: Use explicit types for function parameters and return values when they enhance clarity
Prefer unknown over any when the type is genuinely unknown
Use const assertions (as const) for immutable values and literal types
Leverage TypeScript's type narrowing instead of type assertions
Use meaningful variable names instead of magic numbers - extract constants with descriptive names
Use arrow functions for callbacks and short functions
Prefer for...of loops over .forEach() and indexed for loops
Use optional chaining (?.) and nullish coalescing (??) for safer property access
Prefer template literals over string concatenation
Use destructuring for object and array assignments
Use const by default, let only when reassignment is needed, never var
Always await promises in async functions - don't forget to use the return value
Use async/await syntax instead of promise chains for better readability
Handle errors appropriately in async code with try-catch blocks
Don't use async functions as Promise executors
Remove console.log, debugger, and alert statements from production code
Throw Error objects with descriptive messages, not strings or other values
Use try-catch blocks meaningfully - don't catch errors just to rethrow them
Prefer early returns over nested conditionals for error cases
Keep functions focused and under reasonable cognitive complexity limits
Extract complex conditions into well-named boolean variables
Use early returns to reduce nesting
Prefer simple conditionals over nested ternary operators
Group related code together and separate concerns
Don't use eval() or assign directly to document.cookie
Validate and sanitize user input
Avoid spread syntax in accumulators within loops
Use top-level regex literals instead of creating them in loops
Prefer specific imports over namespace imports
Avoid barrel files (index files that re-export everything)
Write assertions inside it() or test() blocks

`**/*.{ts...

Files:

  • packages/core/tests/long-to-number.test.ts
**/*.test.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

**/*.test.{ts,tsx,js,jsx}: Avoid done callbacks in async tests - use async/await instead
Don't use .only or .skip in committed code
Keep test suites reasonably flat - avoid excessive describe nesting

Files:

  • packages/core/tests/long-to-number.test.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc)

Use TypeScript for all new projects and modules

Files:

  • packages/core/tests/long-to-number.test.ts
**/*.{test,spec}.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc)

Write comprehensive unit tests for all functions and components

Files:

  • packages/core/tests/long-to-number.test.ts
🔇 Additional comments (2)
package.json (1)

23-23: LGTM!

packages/core/tests/long-to-number.test.ts (1)

1-45: LGTM!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses issue #23 by preventing generated proto decoders from throwing when an int64/uint64 exceeds Number.MAX_SAFE_INTEGER, avoiding gRPC stream teardown during event consumption.

Changes:

  • Adds a post-codegen patch script to rewrite ts-proto’s longToNumber() helper into a non-throwing (lossy) conversion.
  • Wires the patch script into npm run generate to keep verify:codegen reproducible.
  • Adds a unit regression test ensuring oversized int64/uint64 values decode without throwing.

Reviewed changes

Copilot reviewed 3 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
scripts/soften-int64.mjs New post-codegen script that replaces ts-proto’s throwing longToNumber() helper with a non-throwing version.
package.json Runs the new soften script after buf generate to keep codegen deterministic.
packages/core/tests/long-to-number.test.ts Regression tests for decoding oversized uint64 and Timestamp.seconds values without throwing.
packages/core/src/generated/photon/imessage/v1/poll_service.ts Softens longToNumber() in generated poll service decoder paths.
packages/core/src/generated/photon/imessage/v1/message_service.ts Softens longToNumber() in generated message service decoder paths.
packages/core/src/generated/photon/imessage/v1/location_types.ts Softens longToNumber() in generated location types decoder paths.
packages/core/src/generated/photon/imessage/v1/group_service.ts Softens longToNumber() in generated group service decoder paths.
packages/core/src/generated/photon/imessage/v1/event_service.ts Softens longToNumber() in generated event service decoder paths.
packages/core/src/generated/photon/imessage/v1/chat_service.ts Softens longToNumber() in generated chat service decoder paths.
packages/core/src/generated/photon/imessage/v1/attachment_types.ts Softens longToNumber() in generated attachment types decoder paths.
packages/core/src/generated/google/protobuf/timestamp.ts Softens longToNumber() used for Timestamp.seconds.
packages/core/src/generated/google/protobuf/descriptor.ts Softens longToNumber() in generated descriptor types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread scripts/soften-int64.mjs
Comment thread packages/core/tests/long-to-number.test.ts Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 18, 2026 22:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 12 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

scripts/soften-int64.mjs:46

  • The fallback else if (contents.includes("function longToNumber")) treats any longToNumber helper as “already softened”. If ts-proto changes the emitted helper body (but it’s still throwing), this script will exit successfully without patching, and CI will keep publishing the throwing decoder. Consider only counting as already-soft when the softened body is present; otherwise fail fast so verify:codegen catches the drift.
  } else if (contents.includes("function longToNumber")) {
    alreadySoft++;
  }

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.

SubscribePollEvents: ClientError INTERNAL "Value is larger than Number.MAX_SAFE_INTEGER" (v0.5.1)

2 participants