Preserve integral precision when parsing decimal Duration inputs - #6945
Preserve integral precision when parsing decimal Duration inputs#6945fubhy wants to merge 1 commit into
Conversation
|
There was a problem hiding this comment.
Important
The regression test is a good spec, but the implementation fix still needs to land on this branch before the PR can merge.
Reviewed changes
I reviewed the single regression test added in packages/effect/test/DurationParsePrecision.test.ts and ran it against the current main-based implementation. As expected, the test fails because parseNanos in packages/effect/src/Duration.ts converts the whole decimal string through Number before scaling, which corrupts integral digits above Number.MAX_SAFE_INTEGER.
The reproduction command in the PR body confirmed the failure, and the existing Duration.test.ts suite still passes (no source changes yet).
⚠️ Implementation fix is missing
The diff only adds the failing test; packages/effect/src/Duration.ts:41-42 still uses roundTiesAwayFromZero(Number(input) * Number(scale)) for decimal nano/micro strings. Merging this branch as-is will keep CI red.
Technical details
# Missing `parseNanos` precision fix
## Affected sites
- `packages/effect/src/Duration.ts:36-42` — `roundTiesAwayFromZero` and `parseNanos`
- `packages/effect/src/Duration.ts:244-249` — nano/micro parsing call sites
- `packages/effect/test/DurationParsePrecision.test.ts:7-10` — correct regression assertion
## Required outcome
- Decimal nano and micro strings must be parsed into bigint nanoseconds without first coercing the full string through `Number`.
- Rounding rules (ties away from zero) should still apply to the final nanosecond value.
- The existing `Duration.test.ts` assertions for `1.5 nanos`, `1.5 micros`, etc., must continue to pass.
## Suggested approach (optional)
Split `valueStr` on `.`, scale the integral part with `BigInt` arithmetic, scale the fractional part (right-padded/truncated to the unit's scale), and add them before rounding. For example, for micros scale `1_000` and fractional string `1`, compute `(BigInt(int) * 1_000n) + BigInt(frac.padEnd(3, "0").slice(0, 3))`, then round. Handle the sign separately so negative inputs round away from zero correctly.@v0 or keep the SHA fresh with Dependabot | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using Kimi K2 (free via Pullfrog for OSS) | 𝕏
|
|
||
| describe("Duration decimal parsing", () => { | ||
| it("preserves integer precision before rounding nanos", () => { | ||
| strictEqual( |
There was a problem hiding this comment.
This is a correct regression assertion — it failed exactly as described, returning 9007199254740994n instead of the expected 9007199254740993n. Keep it when the parseNanos fix is added.
| "decimal nanos should be rounded without first losing integer precision" | ||
| ) | ||
| }) | ||
| }) |
There was a problem hiding this comment.
Consider adding a micro case as well (e.g., "9007199254740993.1 micros" with the corresponding scaled expectation) since parseNanos is also used for the micro path with a scale of 1_000. Negative tie cases would also tighten the regression suite.

Summary
Fractional nano and micro strings above Number.MAX_SAFE_INTEGER can change integral digits before nanosecond rounding.
Important
This PR starts with focused failing reproduction tests. Add the implementation fix to this same branch; CI is expected to fail until that fix is included.
Decimal nano parsing corrupts integral precision
Module:
DurationAudit ID:
core-a-f-duration-decimal-precisionSeverity / confidence: medium / high
What happens
Fractional nano and micro strings above Number.MAX_SAFE_INTEGER can change integral digits before nanosecond rounding.
Why it happens
parseNanos converts the complete decimal and scale through Number before rounding to bigint, losing precision that remains available in the original string.
Expected behavior
Decimal duration strings are parsed into the nanosecond-backed representation and rounded to integer nanoseconds without corrupting the integral component.
Relevant implementation
These links and excerpts are pinned to audit base
c9b56ab507f224426ee8388dc450da447ec4715f.packages/effect/src/Duration.ts:36-42packages/effect/src/Duration.ts:241-249View problematic code at
packages/effect/src/Duration.ts:36-42View exact lines on GitHub
View problematic code at
packages/effect/src/Duration.ts:241-249View exact lines on GitHub
Reproduction
pnpm test --run packages/effect/test/DurationParsePrecision.test.tsObserved failure: A direct Node 24 source probe parsed 9007199254740993.1 nanos as 9007199254740994n instead of 9007199254740993n.
Implementation handoff
The initial reproduction tests on this branch are the regression specification for the implementation fix that should follow in this PR.
pnpm test --run packages/effect/test/DurationParsePrecision.test.tsAudit provenance
c9b56ab507f224426ee8388dc450da447ec4715fc9b56ab507f224426ee8388dc450da447ec4715fcore-a-f-duration-decimal-precision