Skip to content

Conversation

@atticusofsparta
Copy link
Contributor

@atticusofsparta atticusofsparta commented Oct 18, 2025

Summary by CodeRabbit

  • New Features

    • Adds in-memory Hyperbeam sync for primary-name and balance changes, batching and sending patches only when substantive; supports removal semantics and resets sync state after sending.
    • Patch flow refined to conditionally send Hyperbeam state patches after balance updates and include device metadata.
  • Tests

    • Expanded tests verifying patch emission, payload contents (device tag, primary-names entries), per-caller/request data, and updated message counts for request/approve/remove flows.

@coderabbitai
Copy link

coderabbitai bot commented Oct 18, 2025

Walkthrough

Adds a global HyperbeamSync table and new hb APIs to build/send primary-names patches and reset sync state; primary-names flows now record changes into HyperbeamSync; main.lua triggers hb.patchHyperbeamState() only when a balance patch occurred; tests updated/added to assert patch emission and payload shape.

Changes

Cohort / File(s) Summary
Hyperbeam core
src/hb.lua, src/globals.lua
Adds global HyperbeamSync (lazy init) and hb APIs: createPrimaryNamesPatch(), resetHyperbeamSync(), patchHyperbeamState(). Builds structured primary-names patches (names/owners/requests), encodes removal semantics via empty values, attaches device metadata ([email protected]) when sending, and clears sync state after sending.
Event handler integration
src/main.lua
Adds local shouldPatchHbState flag around balance-patching flow and calls hb.patchHyperbeamState() only when a balance patch occurred.
Primary names synchronization
src/primary_names.lua
Updates HyperbeamSync.primaryNames maps (names, owners, requests) across request/create/approve/remove/prune flows to reflect additions, removals, and pending requests used to generate patches.
Tests — primary & patching
tests/primary.test.mjs, tests/patch-primary-names.test.mjs
Adjusts expected message counts to include additional patch emissions and adds tests verifying patch emission, presence of device tag ([email protected]), primary-names payload contents (owners, names, requests) and removal semantics.

Sequence Diagram(s)

sequenceDiagram
    participant H as Handler
    participant M as main.lua
    participant PN as primary_names.lua
    participant HB as hb.lua

    H->>M: Event triggers handler
    M->>M: shouldPatchHbState = false
    M->>M: capture oldBalances
    alt handler modifies state (not "continue")
        M->>PN: primary-name operations (request/approve/remove/prune)
        note right of PN #F0F4C3: PN updates HyperbeamSync.primaryNames maps
        PN-->>HB: HyperbeamSync populated in-memory
        M->>HB: hb.patchBalances(oldBalances)
        HB->>HB: compute/send balance patch (if any)
        alt balance patch occurred
            M->>HB: hb.patchHyperbeamState()
            HB->>HB: createPrimaryNamesPatch()
            alt primary-names patch non-empty
                HB->>HB: attach device metadata ([email protected]) and send patch
                HB->>HB: resetHyperbeamSync()
            end
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Inspect removal semantics in createPrimaryNamesPatch() (empty string/table vs nil) for correctness.
  • Verify resetHyperbeamSync() timing so concurrent primary-name updates aren’t lost.
  • Confirm all primary-name codepaths in src/primary_names.lua consistently update HyperbeamSync.primaryNames as tests expect.

Possibly related PRs

Suggested reviewers

  • dtfiedler

Poem

🐇 I hop through names and owners bright,

I mark each request by day and night,
I bundle patches, tag them neat,
I send a hop and clear my seat,
Small rabbit syncs to keep things right.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The PR title "chore(PE-8643): hb patch system for primary names" is directly aligned with the changes in the pull request. The changeset introduces a complete patch system for primary names, including new methods (createPrimaryNamesPatch(), resetHyperbeamSync(), patchHyperbeamState()), a new global state variable (HyperbeamSync), and integrations throughout the primary names module to track and synchronize state changes. The title is concise, specific, and clearly summarizes the primary objective without unnecessary details or vague terminology.
Docstring Coverage ✅ Passed Docstring coverage is 91.67% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch PE-8643-hb-patch-system

📜 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 627b426 and e8c79e8.

📒 Files selected for processing (1)
  • src/hb.lua (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/hb.lua
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: sdk

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 and usage tips.

Copy link

@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: 3

🧹 Nitpick comments (2)
src/hb.lua (2)

41-58: Send balance patches only when something changed

You always include ao.id, so next(patchMessage.balances) is never nil; patches go out even when nothing changed. Gate on the delta instead. Keep using global Balances and the single-arg signature.

Apply (no behavior change to “protocol balance always included when sending”):

-  -- only send the patch message if there are affected balances, otherwise we'll end up deleting the entire hyperbeam balances table
-  if next(patchMessage.balances) == nil then
+  -- only send if there are affected addresses
+  if next(affectedBalancesAddresses) == nil then
     return {}
   else
     ao.send(patchMessage)
   end

Based on learnings.


4-24: Minor: tidy HyperbeamSync docs

The comments say “addresses that have had name changes” for names; keys are names, not addresses.

Suggested:

  • Update inline comments to reflect correct key types (names keyed by name; owners and requests keyed by address).
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d23e713 and 3619567.

📒 Files selected for processing (4)
  • src/hb.lua (2 hunks)
  • src/main.lua (2 hunks)
  • src/primary_names.lua (7 hunks)
  • tests/primary.test.mjs (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2025-10-08T16:45:02.923Z
Learnt from: atticusofsparta
PR: ar-io/ar-io-network-process#435
File: src/main.lua:0-0
Timestamp: 2025-10-08T16:45:02.923Z
Learning: In `src/main.lua`, the owner-triggered manual `Patch-Hyperbeam-Balances` handler deliberately omits the `info.supply` field that `hb.patchBalances()` includes. The manual patch is for one-time initialization of Hyperbeam state, while automatic patches handle ongoing state synchronization with supply information.

Applied to files:

  • src/main.lua
  • src/hb.lua
📚 Learning: 2025-10-08T16:44:07.797Z
Learnt from: atticusofsparta
PR: ar-io/ar-io-network-process#435
File: src/hb.lua:0-0
Timestamp: 2025-10-08T16:44:07.797Z
Learning: In `src/hb.lua`, the `hb.patchBalances()` function intentionally uses the global `Balances` variable directly and only accepts `oldBalances` as a parameter, rather than accepting both old and new balances as parameters.

Applied to files:

  • src/main.lua
  • src/hb.lua
🧬 Code graph analysis (2)
tests/primary.test.mjs (1)
tests/helpers.mjs (3)
  • buyRecord (810-842)
  • buyRecord (810-842)
  • assertNoResultError (62-68)
src/main.lua (1)
src/utils.lua (1)
  • utils.deepCopy (393-476)
🔇 Additional comments (6)
src/primary_names.lua (5)

149-151: LGTM: request tracking on create

Marking HyperbeamSync.primaryNames.requests[initiator] = true on async request creation is correct and minimal.


235-239: LGTM: comprehensive sync on set-from-request

Tracking names, owners, and clearing requests aligns with patch semantics.


289-293: LGTM: precise sync on single-name removal

These flags target the correct keys (name, primaryName.owner, and requests).


371-373: LGTM: sync on base-name cascade removal

Correctly flags each removed pair under names/owners.


436-438: LGTM: sync on request prune

Marking requests[initiator] = true ensures patches reflect expiry.

tests/primary.test.mjs (1)

686-1002: Tests align with patch semantics

The new suite asserts device and primary-names payloads across request/approve/remove flows. After fixing createPrimaryNamesPatch and the function call in main.lua, these should pass.

To run just this suite locally, filter on “hyperbeam patch”.

@atticusofsparta atticusofsparta changed the title Pe 8643 hb patch system chore(PE-8643): hb patch system for primary names Oct 28, 2025
@codecov-commenter
Copy link

codecov-commenter commented Oct 28, 2025

Codecov Report

❌ Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.36%. Comparing base (bb71487) to head (e8c79e8).

Files with missing lines Patch % Lines
src/primary_names.lua 91.66% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           develop     #438   +/-   ##
========================================
  Coverage    91.36%   91.36%           
========================================
  Files           10       10           
  Lines         3218     3230   +12     
========================================
+ Hits          2940     2951   +11     
- Misses         278      279    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

@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: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3619567 and 3ca3ed0.

📒 Files selected for processing (5)
  • src/globals.lua (1 hunks)
  • src/hb.lua (1 hunks)
  • src/main.lua (2 hunks)
  • tests/patch-primary-names.test.mjs (1 hunks)
  • tests/primary.test.mjs (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/main.lua
  • src/hb.lua
  • tests/primary.test.mjs
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: atticusofsparta
PR: ar-io/ar-io-network-process#435
File: src/main.lua:0-0
Timestamp: 2025-10-08T16:45:02.923Z
Learning: In `src/main.lua`, the owner-triggered manual `Patch-Hyperbeam-Balances` handler deliberately omits the `info.supply` field that `hb.patchBalances()` includes. The manual patch is for one-time initialization of Hyperbeam state, while automatic patches handle ongoing state synchronization with supply information.
📚 Learning: 2025-10-08T16:45:02.923Z
Learnt from: atticusofsparta
PR: ar-io/ar-io-network-process#435
File: src/main.lua:0-0
Timestamp: 2025-10-08T16:45:02.923Z
Learning: In `src/main.lua`, the owner-triggered manual `Patch-Hyperbeam-Balances` handler deliberately omits the `info.supply` field that `hb.patchBalances()` includes. The manual patch is for one-time initialization of Hyperbeam state, while automatic patches handle ongoing state synchronization with supply information.

Applied to files:

  • src/globals.lua
🧬 Code graph analysis (1)
tests/patch-primary-names.test.mjs (1)
tests/primary.test.mjs (9)
  • totalTokenSupplyMemory (21-23)
  • patchMessage (713-713)
  • patchMessage (783-783)
  • patchMessage (885-885)
  • patchMessage (955-955)
  • deviceTag (716-716)
  • deviceTag (786-786)
  • deviceTag (888-888)
  • deviceTag (958-958)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: sdk
  • GitHub Check: unit
🔇 Additional comments (3)
src/globals.lua (1)

15-27: LGTM: Clean state tracking structure.

The HyperbeamSync initialization follows idiomatic Lua patterns with proper state preservation using the or operator. The nested structure with boolean flags is an efficient approach for tracking dirty state that needs synchronization. Type annotations add helpful documentation.

tests/patch-primary-names.test.mjs (2)

14-68: LGTM: Comprehensive test setup.

The test setup is well-structured and thorough:

  • Properly initializes token supply and transfers balances to test addresses
  • Creates an ArNS record owned by a different address than the requester, which correctly tests the primary name request flow
  • Uses incremental timestamps to avoid timing conflicts
  • Follows established testing patterns from the helper functions

71-104: LGTM: Thorough patch message validation.

The patch message assertions are well-structured:

  • Correctly searches backwards through messages to find the most recent patch with device tag "[email protected]"
  • Validates the presence of the primary-names tag
  • Thoroughly checks the patch data structure, including the requests map and the specific entry for the test caller
  • The comment on line 92 about not JSON-encoding patch messages is a helpful implementation detail

The test provides good coverage for the primary name request patching functionality.

@atticusofsparta atticusofsparta force-pushed the PE-8643-hb-patch-system branch from 3ca3ed0 to 9876f91 Compare October 28, 2025 16:08
Copy link

@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: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3ca3ed0 and 9876f91.

📒 Files selected for processing (5)
  • src/globals.lua (1 hunks)
  • src/hb.lua (1 hunks)
  • src/main.lua (2 hunks)
  • tests/patch-primary-names.test.mjs (1 hunks)
  • tests/primary.test.mjs (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • src/hb.lua
  • src/main.lua
  • src/globals.lua
  • tests/patch-primary-names.test.mjs
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: atticusofsparta
PR: ar-io/ar-io-network-process#435
File: src/main.lua:0-0
Timestamp: 2025-10-08T16:45:02.923Z
Learning: In `src/main.lua`, the owner-triggered manual `Patch-Hyperbeam-Balances` handler deliberately omits the `info.supply` field that `hb.patchBalances()` includes. The manual patch is for one-time initialization of Hyperbeam state, while automatic patches handle ongoing state synchronization with supply information.
🧬 Code graph analysis (1)
tests/primary.test.mjs (3)
tests/patch-balances.test.mjs (6)
  • patchMessage (38-38)
  • patchMessage (69-69)
  • patchMessage (119-119)
  • patchData (39-41)
  • patchData (70-72)
  • patchData (120-122)
tests/helpers.mjs (3)
  • buyRecord (810-842)
  • buyRecord (810-842)
  • assertNoResultError (62-68)
tests/patch-primary-names.test.mjs (2)
  • patchMessage (73-73)
  • deviceTag (76-76)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: sdk
🔇 Additional comments (3)
tests/primary.test.mjs (3)

331-332: LGTM!

The updated message count correctly accounts for the additional patch message emitted during primary name approval.


456-457: LGTM!

The updated message count correctly accounts for the patch message emitted when the owner immediately approves their own primary name request.


567-568: LGTM!

The updated message count correctly accounts for the patch message emitted when primary names are removed.

Copy link

@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: 0

🧹 Nitpick comments (2)
src/primary_names.lua (2)

259-261: Remove redundant tracking (already handled by removePrimaryName).

Lines 259-261 track names and owners again after calling removePrimaryName (line 258), which already tracks all three maps (names, owners, requests) at lines 288-292. This creates duplicate tracking entries for the same changes.

Apply this diff to remove the redundant tracking:

     local removedPrimaryNameAndOwner = primaryNames.removePrimaryName(name, from)
-    -- track the changes in the hyperbeam sync
-    HyperbeamSync.primaryNames.names[name] = true
-    HyperbeamSync.primaryNames.owners[removedPrimaryNameAndOwner.owner] = true
-
     table.insert(removedPrimaryNamesAndOwners, removedPrimaryNameAndOwner)

370-372: Remove redundant tracking (already handled by removePrimaryName).

Lines 370-372 track names and owners after calling removePrimaryName (line 369), which already tracks all three maps at lines 288-292. This creates duplicate tracking entries.

Apply this diff to remove the redundant tracking:

     local removedName = primaryNames.removePrimaryName(nameData.name, nameData.owner)
-    -- track the changes in the hyperbeam sync
-    HyperbeamSync.primaryNames.names[nameData.name] = true
-    HyperbeamSync.primaryNames.owners[nameData.owner] = true
     table.insert(removedNames, removedName)
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9876f91 and c820c3e.

📒 Files selected for processing (1)
  • src/primary_names.lua (7 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: atticusofsparta
PR: ar-io/ar-io-network-process#435
File: src/main.lua:0-0
Timestamp: 2025-10-08T16:45:02.923Z
Learning: In `src/main.lua`, the owner-triggered manual `Patch-Hyperbeam-Balances` handler deliberately omits the `info.supply` field that `hb.patchBalances()` includes. The manual patch is for one-time initialization of Hyperbeam state, while automatic patches handle ongoing state synchronization with supply information.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: sdk
🔇 Additional comments (4)
src/primary_names.lua (4)

149-150: LGTM: Request tracking is correct.

The HyperbeamSync tracking correctly captures the initiator when an asynchronous request is stored.


234-238: LGTM: Comprehensive tracking after state updates.

All three maps (names, owners, requests) are correctly tracked after the internal state changes.


288-292: LGTM: Complete tracking in base removal function.

The base removal function correctly tracks all three maps after state modifications.


435-437: LGTM: Pruning tracked correctly.

The HyperbeamSync tracking correctly captures pruned requests after state modifications.

src/globals.lua Outdated

--[[
HyperbeamSync is a table that is used to track changes to our lua state that need to be synced to the Hyperbeam.
the principal of using it is to set the key:value pairs that need to be synced, then
Copy link
Contributor

@arielmelendez arielmelendez Oct 28, 2025

Choose a reason for hiding this comment

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

Nits: The principle and weird tabbing on the line below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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


-- set the primary name
local newPrimaryName = primaryNames.setPrimaryNameFromRequest(recipient, request, timestamp)

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: stray whitespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

src/hb.lua Outdated

function hb.resetHyperbeamSync()
HyperbeamSync = {
balances = {},
Copy link
Contributor

Choose a reason for hiding this comment

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

Excise

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@atticusofsparta atticusofsparta merged commit e8c79e8 into develop Nov 4, 2025
6 checks passed
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.

4 participants