Skip to content

[Communication] PR5: IADS.cs now supports Mailbox#217

Open
Joseph0120 wants to merge 5 commits into
joseph/communication_delay_PR4from
joseph/communication_delay_PR5
Open

[Communication] PR5: IADS.cs now supports Mailbox#217
Joseph0120 wants to merge 5 commits into
joseph/communication_delay_PR4from
joseph/communication_delay_PR5

Conversation

@Joseph0120

Copy link
Copy Markdown
Collaborator

Modified Files:

IADS.cs -> IADS can now send, receive, handle messages, and subscriptions go through mailbox instead of straight to agents.

@stacklane-pr-stack-visualizer

stacklane-pr-stack-visualizer Bot commented Apr 1, 2026

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request refactors the IADS system to use a proxy-based communication model via the Mailbox system, replacing direct method calls with asynchronous messaging for interceptor and target assignments. While the architectural shift is clear, the implementation contains several critical issues: the removal of asset registration logic breaks the hierarchy building process, a missing closing brace in the ReassignTarget method causes a compilation error, and logic regressions in AssignSubInterceptor ignore remaining capacity and target evaluation. Furthermore, redundant registration calls for launchers should be streamlined to improve code clarity.

Comment thread Assets/Scripts/IADS/IADS.cs
Comment thread Assets/Scripts/IADS/IADS.cs Outdated
Comment thread Assets/Scripts/IADS/IADS.cs Outdated
Comment thread Assets/Scripts/IADS/IADS.cs Outdated
@coderabbitai

coderabbitai Bot commented Apr 1, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 47c318e5-fd7c-470d-9858-d42d75328855

📥 Commits

Reviewing files that changed from the base of the PR and between 83f3218 and a52ffb2.

📒 Files selected for processing (1)
  • Assets/Scripts/IADS/IADS.cs

📝 Walkthrough

Walkthrough

IADS gains mailbox-based inter-agent coordination for interceptor assignment and target reassignment. Lifecycle methods (Awake/Start/OnDestroy) now initialize an IadsCommsAgent, conditionally subscribe to SimManager events, and establish mailbox subscriptions. Asset/launcher/threat registration is hardened with null-guards and duplicate prevention. New mailbox message routing dispatches AssignTargetRequestMessage and ReassignTargetRequestMessage to handlers that select appropriate launchers and send responses back through the mailbox. Comprehensive test coverage validates three mailbox delivery scenarios using reflection-injected stubs.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • PisterLab/micromissiles-unity#212: Introduces the foundational mailbox messaging primitives (Message, MessageType, IMessagePayload, PendingMessage) that this PR's IADS request/response handling directly depends on.

Suggested reviewers

  • tryuan99
  • daniellovell
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.77% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: IADS.cs now supports mailbox communication functionality, which is the primary focus of the changeset.
Description check ✅ Passed The description is related to the changeset and explains that IADS can now send, receive, and handle messages through a mailbox instead of direct agent communication.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch joseph/communication_delay_PR5

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.

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

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@Assets/Scripts/IADS/IADS.cs`:
- Around line 57-64: The loop over SimManager.Instance.Interceptors calls
RegisterNewAsset for every interceptor and then calls RegisterNewLauncher which
also calls RegisterNewAsset for LauncherBase instances, causing duplicate
registration; modify the loop in IADS.cs so that for interceptors that are
LauncherBase you only call RegisterNewLauncher(interceptor) (skip the prior
RegisterNewAsset call), and for all other IInterceptor instances call
RegisterNewAsset(interceptor) as before—identify the code by the foreach over
SimManager.Instance.Interceptors, the RegisterNewAsset method, the
RegisterNewLauncher method, and the LauncherBase type to implement this
conditional change.
- Around line 228-230: The if statement checking closestLauncher.Interceptor is
missing its closing brace; add a closing "}" immediately after the
SendMessage(...) call to properly terminate the if block that contains
SendMessage(new ReassignTargetRequestMessage(_commsAgent,
closestLauncher.Interceptor, target)); ensure indentation is corrected so the
method's existing closing brace still closes the method and not the if block
(look for the if (closestLauncher.Interceptor != null) { ... } around the
SendMessage call).
- Around line 25-29: The two fields 'private IadsCommsAgent _commsAgent;' and
'private bool _mailboxSubscribed = false;' in IADS.cs are indented with 4 spaces
while the file uses 2-space indentation; update their leading indentation to
match the file style (2 spaces) so the declarations for
IadsCommsAgent/_commsAgent and _mailboxSubscribed align with the rest of the
file.
- Around line 232-237: The method SendMessage shadows Unity's
Component.SendMessage; rename this method to SendMailboxMessage and update all
internal call sites that currently call SendMessage to call SendMailboxMessage
instead (preserve the parameter type Message and behavior). Locate the method
body using the existing identifiers (SendMessage, Mailbox.GetOrCreateInstance,
mailbox.Send) and perform the rename, then update any references inside this
class that invoke SendMessage to use SendMailboxMessage so Unity's API is not
shadowed.
- Around line 204-209: Override InterceptorBase.OnMessage to handle
AssignTargetMessage: detect when the incoming message is an AssignTargetMessage,
verify it targets this interceptor (compare message.SubInterceptor or target
owner if present), extract the IHierarchical target from the message and assign
it to the interceptor by calling the existing assign method or setting the
interceptor's target property (e.g., call AssignTarget/SetTarget or set
Target/HierarchicalAgent.Target on this interceptor). After handling, skip base
processing/return so the message is not discarded. Ensure you reference the
AssignTargetMessage type and InterceptorBase.OnMessage in your change.
🪄 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: ASSERTIVE

Plan: Pro

Run ID: 1253d2b1-5db2-473d-bc7b-fb19f92d3961

📥 Commits

Reviewing files that changed from the base of the PR and between f132eca and cfb9e96.

📒 Files selected for processing (1)
  • Assets/Scripts/IADS/IADS.cs

Comment thread Assets/Scripts/IADS/IADS.cs Outdated
Comment thread Assets/Scripts/IADS/IADS.cs
Comment thread Assets/Scripts/IADS/IADS.cs Outdated
Comment thread Assets/Scripts/IADS/IADS.cs Outdated
Comment thread Assets/Scripts/IADS/IADS.cs

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@Assets/Scripts/IADS/IADS.cs`:
- Line 50: The if-statement using SimManager.Instance in IADS.cs is indented
with 6 spaces instead of the file's 2-space style; locate the line containing
"if (SimManager.Instance != null) {" and adjust its leading whitespace to 2
spaces to match the surrounding code style so the file's indentation is
consistent.
🪄 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: ASSERTIVE

Plan: Pro

Run ID: f2676bee-aff4-4164-8fa7-3e6b31fd1e39

📥 Commits

Reviewing files that changed from the base of the PR and between cfb9e96 and a15710f.

📒 Files selected for processing (4)
  • Assets/Scripts/Agents/Messaging/Mailbox.cs.meta
  • Assets/Scripts/Agents/Messaging/PendingMessage.cs.meta
  • Assets/Scripts/IADS/IADS.cs
  • Assets/Scripts/IADS/IadsCommsAgent.cs.meta

Comment thread Assets/Scripts/IADS/IADS.cs Outdated
@Joseph0120 Joseph0120 requested a review from tryuan99 April 1, 2026 06:16
@Joseph0120 Joseph0120 force-pushed the joseph/communication_delay_PR4 branch from f132eca to 0edbb50 Compare April 28, 2026 21:56
@Joseph0120 Joseph0120 force-pushed the joseph/communication_delay_PR5 branch from a15710f to 833b973 Compare April 28, 2026 22:34

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@Assets/Scripts/IADS/IADS.cs`:
- Around line 205-217: AssignSubInterceptor currently accesses
subInterceptor.HierarchicalAgent without a null check which can cause crashes
during mailbox/assignment; add a guard at the start of AssignSubInterceptor to
return early if subInterceptor.HierarchicalAgent is null (similar to existing
checks for _commsAgent and subInterceptor.CapacityRemaining), and ensure callers
of launcher.FindNewTarget(subInterceptor.HierarchicalAgent,
subInterceptor.Capacity) only run when HierarchicalAgent is non-null so
FindNewTarget is never invoked with a null agent.
- Line 138: Check for a null threat before dereferencing its HierarchicalAgent:
update the conditional that currently reads use of threat.HierarchicalAgent so
it first ensures threat != null, then threat.HierarchicalAgent != null, and then
checks !_newThreats.Contains(threat.HierarchicalAgent); this prevents a
NullReferenceException when an upstream event passes a null threat (references:
symbol threat, property HierarchicalAgent, collection _newThreats).
- Around line 132-134: The launcher delegate subscriptions are added each time a
launcher is registered causing duplicate calls after resets; before adding
handlers for launcher.OnAssignSubInterceptor and launcher.OnReassignTarget (and
before adding launcher.HierarchicalAgent to _launchers) either unsubscribe
existing handlers or ensure idempotency by performing
"launcher.OnAssignSubInterceptor -= AssignSubInterceptor" and
"launcher.OnReassignTarget -= ReassignTarget" prior to the "+=" calls, and also
remove/unsubscribe these handlers when launchers are unregistered or when the
simulation resets to prevent duplicate routing.
🪄 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: ASSERTIVE

Plan: Pro

Run ID: ddef23c7-0440-406d-9963-20e556e3b8e5

📥 Commits

Reviewing files that changed from the base of the PR and between a15710f and 833b973.

📒 Files selected for processing (5)
  • Assets/Scripts/Agents/Messaging/Mailbox.cs.meta
  • Assets/Scripts/Agents/Messaging/PendingMessage.cs.meta
  • Assets/Scripts/IADS/IADS.cs
  • Assets/Scripts/IADS/IadsCommsAgent.cs.meta
  • Assets/Tests/EditMode/IADSMailboxTests.cs

Comment thread Assets/Scripts/IADS/IADS.cs
Comment thread Assets/Scripts/IADS/IADS.cs Outdated
Comment thread Assets/Scripts/IADS/IADS.cs Outdated
@Joseph0120 Joseph0120 force-pushed the joseph/communication_delay_PR5 branch from 9fa2435 to 83f3218 Compare June 7, 2026 22:36

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
Assets/Scripts/IADS/IADS.cs (1)

218-236: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Missing null guard for subInterceptor.HierarchicalAgent.

Line 230 passes subInterceptor.HierarchicalAgent to FindNewTarget, but the guard at line 219 doesn't check if it's null. If HierarchicalAgent is null, this could cause unexpected behavior in FindNewTarget.

🛠️ Proposed fix
 private void AssignSubInterceptor(IInterceptor subInterceptor) {
-  if (subInterceptor == null || subInterceptor.CapacityRemaining <= 0) {
+  if (subInterceptor == null || subInterceptor.HierarchicalAgent == null ||
+      subInterceptor.CapacityRemaining <= 0) {
     return;
   }
🤖 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 `@Assets/Scripts/IADS/IADS.cs` around lines 218 - 236, AssignSubInterceptor
currently uses subInterceptor.HierarchicalAgent without guarding it; add a null
check for subInterceptor.HierarchicalAgent before calling launcher.FindNewTarget
(or skip that launcher) to avoid passing null into FindNewTarget. Specifically,
in AssignSubInterceptor ensure subInterceptor.HierarchicalAgent != null (either
return early or continue the loop) before invoking
launcher.FindNewTarget(subInterceptor.HierarchicalAgent,
subInterceptor.CapacityRemaining), keeping the existing behavior that still
evaluates the returned target with subInterceptor.EvaluateReassignedTarget.
♻️ Duplicate comments (1)
Assets/Scripts/IADS/IADS.cs (1)

58-65: 🧹 Nitpick | 🔵 Trivial | 💤 Low value

Redundant RegisterNewAsset call for launcher interceptors persists.

For LauncherBase interceptors, RegisterNewAsset is called at line 60, then RegisterNewLauncher at line 62 calls RegisterNewAsset again at line 132. While harmless due to the duplicate guard in RegisterNewAsset, this is wasteful.

♻️ Proposed fix
       foreach (var agent in SimManager.Instance.Interceptors) {
         if (agent is IInterceptor interceptor) {
-          RegisterNewAsset(interceptor);
           if (interceptor is LauncherBase) {
             RegisterNewLauncher(interceptor);
+          } else {
+            RegisterNewAsset(interceptor);
           }
         }
       }
🤖 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 `@Assets/Scripts/IADS/IADS.cs` around lines 58 - 65, In the
SimManager.Instance.Interceptors loop avoid calling RegisterNewAsset twice for
LauncherBase interceptors: detect launcher interceptors (interceptor is
LauncherBase) and call RegisterNewLauncher for them, otherwise call
RegisterNewAsset; alternatively remove the RegisterNewAsset call inside
RegisterNewLauncher and ensure all callers of RegisterNewLauncher still result
in asset registration — update the loop around
RegisterNewAsset/RegisterNewLauncher and the methods
RegisterNewLauncher/RegisterNewAsset (symbols: SimManager.Instance.Interceptors,
IInterceptor, LauncherBase, RegisterNewAsset, RegisterNewLauncher) so a launcher
asset is only registered once.
🤖 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.

Inline comments:
In `@Assets/Scripts/IADS/IADS.cs`:
- Around line 260-279: HandleMailboxAssignTargetRequest can pass a null
subInterceptor.HierarchicalAgent into launcher.FindNewTarget; add a null guard
so we don't call FindNewTarget with a null hierarchical agent. Update the
early-return condition in HandleMailboxAssignTargetRequest (or add a check
before the loop) to ensure subInterceptor.HierarchicalAgent != null (matching
the pattern used in AssignSubInterceptor), and if it's null simply
return/continue so FindNewTarget is only invoked with a valid hierarchical
agent.

In `@Assets/Tests/EditMode/IADSMailboxTests.cs`:
- Around line 13-30: The Mailbox singleton isn't being set before IADS.Start may
call TrySubscribeMailbox; after creating the Mailbox component in SetUp,
explicitly set the singleton (e.g., call the existing test helper
SetMailboxInstance(_mailbox) or assign Mailbox.Instance = _mailbox) so
Mailbox.GetOrCreateInstance()/FindFirstObjectByType<Mailbox>() is deterministic
when IADS.Start invokes TrySubscribeMailbox.

---

Outside diff comments:
In `@Assets/Scripts/IADS/IADS.cs`:
- Around line 218-236: AssignSubInterceptor currently uses
subInterceptor.HierarchicalAgent without guarding it; add a null check for
subInterceptor.HierarchicalAgent before calling launcher.FindNewTarget (or skip
that launcher) to avoid passing null into FindNewTarget. Specifically, in
AssignSubInterceptor ensure subInterceptor.HierarchicalAgent != null (either
return early or continue the loop) before invoking
launcher.FindNewTarget(subInterceptor.HierarchicalAgent,
subInterceptor.CapacityRemaining), keeping the existing behavior that still
evaluates the returned target with subInterceptor.EvaluateReassignedTarget.

---

Duplicate comments:
In `@Assets/Scripts/IADS/IADS.cs`:
- Around line 58-65: In the SimManager.Instance.Interceptors loop avoid calling
RegisterNewAsset twice for LauncherBase interceptors: detect launcher
interceptors (interceptor is LauncherBase) and call RegisterNewLauncher for
them, otherwise call RegisterNewAsset; alternatively remove the RegisterNewAsset
call inside RegisterNewLauncher and ensure all callers of RegisterNewLauncher
still result in asset registration — update the loop around
RegisterNewAsset/RegisterNewLauncher and the methods
RegisterNewLauncher/RegisterNewAsset (symbols: SimManager.Instance.Interceptors,
IInterceptor, LauncherBase, RegisterNewAsset, RegisterNewLauncher) so a launcher
asset is only registered once.
🪄 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: ASSERTIVE

Plan: Pro

Run ID: f5b2d25f-02a8-4770-bb05-c56a0ad1427d

📥 Commits

Reviewing files that changed from the base of the PR and between 833b973 and 83f3218.

📒 Files selected for processing (2)
  • Assets/Scripts/IADS/IADS.cs
  • Assets/Tests/EditMode/IADSMailboxTests.cs

Comment thread Assets/Scripts/IADS/IADS.cs
Comment thread Assets/Tests/EditMode/IADSMailboxTests.cs

@tryuan99 tryuan99 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please check every change in this PR and ensure that it's absolutely necessary. No need for extra checks and overly preventative code that makes readability worse. If a problem happens, it'll be logged and we can fix then :)

Comment on lines +27 to +28
private Mailbox _mailboxInstance;
private bool _mailboxSubscribed = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ditto from previous PR

SimManager.Instance.OnNewAsset += RegisterNewAsset;
SimManager.Instance.OnNewLauncher += RegisterNewLauncher;
SimManager.Instance.OnNewThreat += RegisterNewThreat;
if (SimManager.Instance != null) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why do you need this check now?

SimManager.Instance.OnNewLauncher += RegisterNewLauncher;
SimManager.Instance.OnNewThreat += RegisterNewThreat;

// Just in case the simulation started before IADS subscribed to events.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

will this ever happen? no need to add if it will never happen

Comment on lines +81 to +82
if (!_mailboxSubscribed) {
TrySubscribeMailbox();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

only call it once. if there's race condition, this doesn't fix the problem

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.

2 participants