Conversation
📝 WalkthroughSummary by CodeRabbit
WalkthroughThis PR implements the Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
packages/jam/in-core/externalities/refine.test.ts (1)
247-255: Assert the exact error value in the negative-path cases.These tests only check
isError, so they will still pass ifmachineInitstarts returning the wrong decoder error ormachineExpungereturns the wrong host-call error. Please pin the contract with the concrete error value as well.Also applies to: 257-265, 306-325
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/jam/in-core/externalities/refine.test.ts` around lines 247 - 255, Update the negative-path tests to assert the concrete error value (not just isError) returned by ext.machineInit and ext.machineExpunge: for the test "should return error for invalid program blob" (using createExt, BytesBlob.blobFrom, tryAsProgramCounter, and ext.machineInit) assert the specific error enum/variant or error.code/message expected from the decoder instead of only checking result.isError; make the analogous changes in the other failing tests referenced (lines 257-265 and 306-325) to pin the expected error values for each negative case so the assertions fail if the wrong error is returned.packages/jam/in-core/externalities/refine.ts (1)
118-120: Avoid reparsing the same program blob duringmachineInit.
ProgramDecoder.deblob(code.raw)already parses the blob once, andresetGeneric(code.raw, ...)immediately decodes it again. On larger inner programs this doubles the init-path decode/copy cost for no behavioral gain.As per coding guidelines, "be mindful of allocations/copying of large memory (prefer ArrayBuffer/views and subarray over slice)".
Also applies to: 123-125
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/jam/in-core/externalities/refine.ts` around lines 118 - 120, ProgramDecoder.deblob(code.raw) is being called twice during machineInit (once as deblobResult and again inside resetGeneric(code.raw, ...)), causing redundant parsing and copies for large inner programs; change the flow to reuse the already-parsed result from ProgramDecoder.deblob (deblobResult) when calling resetGeneric (or add/use an overload of resetGeneric that accepts the parsed/deblobbed representation) so you pass the decoded buffer/view instead of re-decoding code.raw, and apply the same change for the other occurrence around lines 123-125 to avoid double parsing/copying.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/jam/in-core/externalities/refine.ts`:
- Line 125: The code narrows a u64 program counter to a JS number in
innerPvm.resetGeneric(Number(programCounter)), risking precision loss for values
> 2^53-1; either (A) add a guard before calling innerPvm.resetGeneric that
rejects/non-accepts programCounter values that cannot round-trip (e.g. check
Number.isSafeInteger(Number(programCounter)) or compare to
Number.MAX_SAFE_INTEGER and throw/return an error from
machineExpunge/machineInit), or (B) refactor the runtime to preserve the full
u64 by changing innerPvm.resetGeneric (and any callers such as machineExpunge,
machineInit and places using tryAsProgramCounter/innerPvm.getPC) to accept a
BigInt/u64 representation instead of narrowing to number; update all affected
call sites (resetGeneric, tryAsProgramCounter, innerPvm.getPC, machineExpunge,
machineInit) to use the chosen approach.
---
Nitpick comments:
In `@packages/jam/in-core/externalities/refine.test.ts`:
- Around line 247-255: Update the negative-path tests to assert the concrete
error value (not just isError) returned by ext.machineInit and
ext.machineExpunge: for the test "should return error for invalid program blob"
(using createExt, BytesBlob.blobFrom, tryAsProgramCounter, and ext.machineInit)
assert the specific error enum/variant or error.code/message expected from the
decoder instead of only checking result.isError; make the analogous changes in
the other failing tests referenced (lines 257-265 and 306-325) to pin the
expected error values for each negative case so the assertions fail if the wrong
error is returned.
In `@packages/jam/in-core/externalities/refine.ts`:
- Around line 118-120: ProgramDecoder.deblob(code.raw) is being called twice
during machineInit (once as deblobResult and again inside resetGeneric(code.raw,
...)), causing redundant parsing and copies for large inner programs; change the
flow to reuse the already-parsed result from ProgramDecoder.deblob
(deblobResult) when calling resetGeneric (or add/use an overload of resetGeneric
that accepts the parsed/deblobbed representation) so you pass the decoded
buffer/view instead of re-decoding code.raw, and apply the same change for the
other occurrence around lines 123-125 to avoid double parsing/copying.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 7575fb9c-8fb3-47c7-b257-97983949cfcb
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (4)
packages/jam/in-core/externalities/refine.test.tspackages/jam/in-core/externalities/refine.tspackages/jam/in-core/package.jsonpackages/jam/jam-host-calls/externalities/refine-externalities.ts
View all
Benchmarks summary: 83/83 OK ✅ |
There was a problem hiding this comment.
Actionable comments posted: 2
♻️ Duplicate comments (1)
packages/jam/in-core/externalities/refine.ts (1)
98-100:⚠️ Potential issue | 🟠 MajorProgramCounter round-trip can still corrupt values above safe integer range.
machineExpungenow returns PC from interpreter, butmachineInitstill narrows at Line 150 viaNumber(programCounter). Values beyond2^53 - 1cannot round-trip exactly, so returned PC may differ from input.#!/bin/bash set -euo pipefail # Verify JS-safe-integer precision loss for ProgramCounter-like values. python - <<'PY' pc = 2**53 + 1 js_num = float(pc) # mirrors JS Number precision model roundtrip = int(js_num) print({"input_pc": pc, "roundtrip_pc": roundtrip, "equal": pc == roundtrip}) PY # Verify narrowing and retrieval sites in this file. rg -n -C2 'Number\(programCounter\)' packages/jam/in-core/externalities/refine.ts rg -n -C2 'tryAsProgramCounter\(entry\[1\]\.getPC\(\)\)|machineExpunge\(' packages/jam/in-core/externalities/refine.tsExpected: the Python output should show
"equal": False, confirming precision loss risk.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/jam/in-core/externalities/refine.ts` around lines 98 - 100, machineExpunge returns a ProgramCounter from the interpreter but machineInit still narrows incoming programCounter with Number(programCounter), which will corrupt values > 2^53-1; update the code paths (machineInit and any places that call Number(programCounter)) to preserve full-precision by treating program counters as BigInt or as string-backed BigInt semantics instead of Number, remove the Number(...) cast, use tryAsProgramCounter/tryAsProgramCounter(entry[1].getPC()) to validate/convert from BigInt/string safely, and add range/format validation where needed so round-trips via machineExpunge and machineInit do not lose precision.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/jam/in-core/externalities/refine.test.ts`:
- Around line 332-335: The test only asserts the second machineInit result
(initResult) and ignores the first and third, allowing partial setup failures to
pass; capture each machineInit return (e.g., initResult0 = await
ext.machineInit(code, tryAsProgramCounter(0)), initResult1 = await
ext.machineInit(code, tryAsProgramCounter(10)), initResult2 = await
ext.machineInit(code, tryAsProgramCounter(20))) and add assertions that
initResult0.isOk, initResult1.isOk, and initResult2.isOk are true (or fail test
with their error details) so every call to ext.machineInit is validated.
- Around line 307-327: The tests currently only check result.isError for
machineExpunge; update both assertions to assert the concrete error type
NoMachineError is returned: in the non-existent machine case (where
ext.machineExpunge(tryAsMachineId(999)) returns result) assert result.error is
an instance of or equals NoMachineError, and in the double-expunge case after r2
= await ext.machineExpunge(machineId) assert r2.error is NoMachineError; locate
uses of machineExpunge, createExt, tryAsMachineId, tryAsProgramCounter,
BytesBlob.blobFrom and MINIMAL_PROGRAM to modify the assertions accordingly so
the test fails for wrong error kinds.
---
Duplicate comments:
In `@packages/jam/in-core/externalities/refine.ts`:
- Around line 98-100: machineExpunge returns a ProgramCounter from the
interpreter but machineInit still narrows incoming programCounter with
Number(programCounter), which will corrupt values > 2^53-1; update the code
paths (machineInit and any places that call Number(programCounter)) to preserve
full-precision by treating program counters as BigInt or as string-backed BigInt
semantics instead of Number, remove the Number(...) cast, use
tryAsProgramCounter/tryAsProgramCounter(entry[1].getPC()) to validate/convert
from BigInt/string safely, and add range/format validation where needed so
round-trips via machineExpunge and machineInit do not lose precision.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 9d967cb2-9cf5-438f-8fe1-ed63ff691be3
📒 Files selected for processing (2)
packages/jam/in-core/externalities/refine.test.tspackages/jam/in-core/externalities/refine.ts
Merge after: #935