Skip to content

fix(capture): total sameValue in trace de-dup (circular/BigInt) - #135

Merged
krassx merged 2 commits into
mainfrom
fix/issue-56
Oct 28, 2026
Merged

krassx merged 2 commits into
mainfrom
fix/issue-56

Conversation

@krassx

@krassx krassx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #56

sameValue in TraceProducerBase.emitIfChanged no longer throws: BigInt inside objects is compared by value via a replacer; any other uncomparable value (circular, throwing toJSON) counts as changed, so the trace is emitted instead of dropped. Tests: circular, BigInt, BigInt-vs-string, throwing toJSON.

🤖 Generated with Claude Code

@claude

claude Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Claude finished @krassx's task in 18s —— View job


Review of PR #135: total sameValue in trace de-dup

I read the diff and the surrounding TraceProducerBase code (emitIfChanged, #send, #last). I did not run the tests, lint or typecheck.

No real issues found, so there are no inline findings.

What I checked

  • Throw paths closed. JSON.stringify can throw on circular structures, on a throwing toJSON, and on a bare BigInt nested in an object. The try/catch returns false for all of these, so the trace is emitted instead of the producer throwing out of a tracker callback. Emitting is the safe default: a spurious emit costs a duplicate trace, while a drop would hide a change.
  • Same reference still de-dups. Object.is(a, b) runs before any stringify, so a repeated circular reference is treated as unchanged and never reaches the try. The circular test covers this (a, a, b → [a, b]).
  • Top-level BigInt. typeof a !== 'object' returns false without stringifying. Object.is(1n, 1n) is true, so equal BigInts still de-dup, and different ones count as changed.
  • BigInt replacer. JSON.stringify calls toJSON before the replacer, and BigInt has no built-in toJSON, so the replacer sees the raw bigint. Tagging it as { $bigint: '…' } keeps 1n distinct from '1'. The accepted collision with a user object of that exact shape is noted in the code comment and is negligible for trace payloads.
  • #last bookkeeping. #send stores the emitted value by reference after emitting, so the failure path leaves state consistent. After a "changed" decision on an uncomparable value, #last holds that value. The next distinct circular or throwing value re-emits, as intended.
  • Tests. The four new tests cover circular, throwing toJSON, BigInt by value, and BigInt vs string. The assertions check outcomes (emitted values and counts), not just "doesn't throw".

Pre-existing behaviour, not caused by this PR

  • Comparison is by JSON shape, so Map/Set (serialised as {}) and undefined/function-valued properties compare as equal. This is unchanged and out of scope for system traces: sameValue JSON.stringify guard can throw on circular/BigInt values #56.
  • #last keeps a reference, so an object mutated in place and re-emitted is deduped by Object.is. This is also unchanged.
  • A circular value that does get emitted could still fail later at bundle serialisation. That is outside this change. If trace values can plausibly be circular, it may be worth checking how the sink or exporter handles them.

Summary

  1. Overall risk: Low. The change is small, local, and strictly reduces failure modes.
  2. Merge recommendation: Approve. This is a comment, not a formal review approval.
  3. Most important issues to fix: None.
  4. Positives:
    • Small, targeted fix with a clear fail-safe choice.
    • Tests assert behaviour, including the BigInt-vs-string edge.
    • The code comment documents the accepted $bigint tag collision.
      · branch fix/issue-56

@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 35 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3e056e4a-6476-4558-b572-8b707c3c841c
Commits

Reviewing files that changed from the base of the PR and between 9407c8d and 88fd4b5.

Files selected for processing: 2
  • packages/capture/src/trace-producer.test.ts
  • packages/capture/src/trace-producer.ts

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f4a60fb4-086d-45b0-9d24-74ba554af274
Commits

Reviewing files that changed from the base of the PR and between 769cbbe and 9407c8d.

Files selected for processing: 2
  • packages/capture/src/trace-producer.test.ts
  • packages/capture/src/trace-producer.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Change detection now handles circular objects, BigInt values, and objects that cannot be serialized without throwing.
    • BigInt values are distinguished from strings with the same text, and repeated equivalent values are deduplicated correctly.

sameValue now uses JSON serialization with a BigInt replacer to compare object values. If serialization fails, it returns false instead of throwing. Tests cover circular objects, objects with throwing toJSON methods, BigInts, and distinguishing a BigInt from a matching string.

Possibly related PRs

Suggested labels: javascript

Priority: Low

Change: Bug fix · Severity of issue fixed: Low

Merge Risk: Low · previous review 9407c

Custom producers can cause repeated error callbacks until the invalid trace is replaced or capture stops. This is a bounded risk for merge.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly identifies the fix to sameValue de-duplication for circular values and BigInt values in capture traces.
Description check Passed The description directly explains the sameValue fix, the handling of circular, BigInt, and throwing toJSON values, and the related tests.
Linked Issues check Passed Issue #56 is the only directly linked issue. Its requirement: make the sameValue de-dup in TraceProducerBase.emitIfChanged total, using structural comparison or try/catch that treats the value as …
Out of Scope Changes check Passed The whole-PR diff against merge base 769cbbe changes two files only:
- packages/capture/src/trace-producer.ts: replaces the sameValue helper with the total version and adds the bigintSafe replac…
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Linked Issues check

Explanation

Issue #56 is the only directly linked issue. Its requirement: make the sameValue de-dup in TraceProducerBase.emitIfChanged total, using structural comparison or try/catch that treats the value as changed, with circular and BigInt test cases.

At head 9407c8d, packages/capture/src/trace-producer.ts implements the total path:

  • sameValue returns true on Object.is equality.
  • It returns false when either value is not a non-null object, so top-level values are not serialized. Top-level BigInt values still compare by Object.is.
  • For object pairs, it compares JSON.stringify(a, bigintSafe) with JSON.stringify(b, bigintSafe) inside try. The catch returns false, so circular values and throwing toJSON count as changed.
  • bigintSafe maps BigInt to { $bigint: string }, so BigInt inside objects compares by value and differs from the same-looking string.

packages/capture/src/trace-producer.test.ts adds four emitChanged tests (the emitIfChanged path) covering circular values, throwing toJSON, BigInt inside objects, and BigInt versus string. These match the issue's test-first request for circular and BigInt cases.

The requirement is met by code and tests. Tests were not executed in this review; the assessment relies on source reading.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against merge base 769cbbe changes two files only:

  • packages/capture/src/trace-producer.ts: replaces the sameValue helper with the total version and adds the bigintSafe replacer and its comment. This is the change the issue requests.
  • packages/capture/src/trace-producer.test.ts: adds four tests for the circular, throwing toJSON, and BigInt cases. These are test coverage for the same requirement.

No other files are changed, and no exported or public declarations change. The { $bigint } tag collision is a documented, accepted edge case inside the same helper. It is not an unrelated change. The commit-summary item for throwing toJSON coverage supports the total-comparison requirement, so it is in scope.

Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 2 files.

Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@coderabbitai coderabbitai Bot added the javascript Pull requests that update javascript code label Oct 28, 2026
krassx and others added 2 commits October 28, 2026 21:21
…igInt (#56)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@krassx

krassx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main. Review notes: the three 'pre-existing' items (Map/Set shape, in-place mutation dedup, a circular value that is emitted but later fails bundle serialisation) are out of scope for #56 (which asked for a total comparison); an emitted-circular failure is isolated by the existing provider/exporter error handling, and was dropped silently before this PR, so this is a strict improvement. No findings to fix. Gate: capture tests 975 pass, tsc/biome/cycles clean.

@claude

claude Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Claude finished @krassx's task in 17s —— View job


Re-review of PR #135 after the latest push

I re-read the full diff against origin/main (2 commits) and the surrounding TraceProducerBase code (emitIfChanged, #send, #last). I did not run tests, lint or typecheck.

No real issues found, so there are no inline findings. My earlier review had no findings either, so nothing is carried over.

What I checked

  • Throw paths. sameValue now returns false (changed, so emit) for circular structures, a throwing toJSON and a throwing getter or Proxy trap. The try/catch covers all of them. A toJSON that returns a bigint is also handled, because the replacer runs on the toJSON result.
  • Top-level non-objects. Object.is runs first, then any non-object returns false. Equal BigInts still de-dup and different ones emit.
  • State consistency. #send only writes #last after sink.emit, so the failure path leaves state consistent. A repeated circular reference is still de-duped by Object.is.
  • BigInt tag. { $bigint: '1' } keeps 1n distinct from '1'. The one accepted collision, a user object of that exact shape, is documented in the code comment and is negligible for trace payloads.
  • Tests. The four new tests cover circular values, a throwing toJSON, BigInt by value and BigInt vs string. They assert emitted values and counts, not just "doesn't throw". The throwing-toJSON test only asserts the count, which is enough for that case.

Not caused by this PR

Summary

  1. Overall risk: Low. The change is small and local, and it only removes failure modes.
  2. Merge recommendation: Approve. This is a comment, not a formal review approval.
  3. Most important issues to fix: None.
  4. Positives: The fix is small and the fail-safe choice is sensible: a spurious emit costs a duplicate, while a drop would hide a change. The BigInt-vs-string edge is tested. The accepted tag collision is documented.
    · branch fix/issue-56

@krassx
krassx merged commit 854b5fe into main Oct 28, 2026
8 checks passed
@krassx
krassx deleted the fix/issue-56 branch October 28, 2026 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

javascript Pull requests that update javascript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

system traces: sameValue JSON.stringify guard can throw on circular/BigInt values

1 participant