Skip to content

test+doc: harden ForwardIfCurrent dispatch-under-lock invariant (#1010) - #1022

Merged
shanselman merged 1 commit into
mainfrom
shanselman-plan-nodeconnector-dispatch
Jul 18, 2026
Merged

shanselman merged 1 commit into
mainfrom
shanselman-plan-nodeconnector-dispatch

Conversation

@shanselman

@shanselman shanselman commented Jul 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Tests and documentation hardening for the NodeConnector.ForwardIfCurrent dispatch-under-lock invariant (issue #1010 hazard 2). No production code behavior is changed.

After thorough investigation of all lock ordering paths, subscriber behavior, and reentrancy scenarios, the conclusion is that ForwardIfCurrent's current design is correct: it holds _clientLifecycleLock during validation+dispatch to prevent a TOCTOU race where a retired client could publish events after replacement. All subscribers are fast (µs synchronous preambles with fire-and-forget async dispatch), lock ordering is consistent with no reverse acquisitions, and no deadlock or reentrancy corruption is possible.

Changes

Tests (NodeConnectorTests.cs)

Three tests exercise the real ForwardIfCurrent event path using a reflection helper that invokes WebSocketClientBase.RaiseStatusChanged on captured WindowsNodeClient instances:

  • Reconnect_SuppressesStatusFromRetiredClient — After reconnect, fires StatusChanged on retired client A → suppressed (generation mismatch). Then fires on current client B → forwarded. Proves both the negative and positive contract.
  • Disconnect_RetiresClient_SuppressesForwarding — After disconnect, fires StatusChanged on the retired client → suppressed (generation mismatch + null _client).
  • CurrentClientStatusHandler_CanReadConnectorProperties_WithoutBlocking — Fires StatusChanged on the current client through ForwardIfCurrent. Subscriber reads IsConnected, PairingStatus, Mode, Client and asserts exact values — proving no blocking and consistent state.

Documentation (NodeConnector.cs)

Enhanced the ForwardIfCurrent comment to document:

  • The atomicity invariant: validation+dispatch stay atomic under lock
  • Lock ordering: _connectSemaphore (async) → _clientLifecycleLock (monitor, outermost among monitors) → subscriber-owned locks
  • Subscriber contract: handlers must return promptly; current subscribers use fire-and-forget async dispatch

Validation

Suite Result
build.ps1 ✅ All 5 projects
Connection.Tests (focused) ✅ 14 passed
Shared.Tests ✅ 2901 passed, 31 skipped
Tray.Tests ✅ 1823 passed

Baseline disposition

ReadmeValidationTests failures are a pre-existing worktree environment issue (OPENCLAW_REPO_ROOT not set), not caused by this diff. Tests pass with OPENCLAW_REPO_ROOT set.

Review

Rubber-duck review identified and addressed:

  1. ✅ Lock ordering comment imprecision — fixed: documents _connectSemaphore → _clientLifecycleLock ordering
  2. ✅ Reconnect test missing positive assertion — fixed: now fires on both retired A (suppressed) and current B (forwarded)
  3. ⏭️ Concurrent lock-holding proof — deferred: would require fragile timing-dependent test; current tests prove the behavioral contract
  4. ⏭️ Non-generic EventHandler overload coverage — follow-up: same ForwardIfCurrent logic, low risk

Ref: #1010

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P3 Low-risk cleanup, docs, polish, ergonomics, or speculative feature. labels Jul 18, 2026
@clawsweeper

clawsweeper Bot commented Jul 18, 2026 •

Copy link
Copy Markdown

Codex review: needs real behavior proof before merge. Reviewed July 18, 2026, 5:56 PM ET / 21:56 UTC.

Summary
The PR adds three NodeConnector.ForwardIfCurrent event-path regression tests and expands the method's lock-ordering and subscriber-contract documentation, without changing production behavior.

Reproducibility: yes. at source level: the added focused tests invoke the captured clients' status event path and assert stale-client suppression plus current-client forwarding. A current-head inspectable run has not been supplied for this review.

Review metrics: 2 noteworthy metrics.

  • Changed surface: 2 files affected; 113 additions; 0 deletions. The branch is limited to one connection test file and one source-comment block, with no production implementation change.
  • Focused coverage: 3 event-path tests added. The tests cover retired-client suppression after reconnect and disconnect plus current-client forwarding.

Merge readiness
Overall: 🦪 silver shellfish
Proof: 🦪 silver shellfish
Patch quality: 🐚 platinum hermit
Result: blocked until real behavior proof is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Add a redacted current-head terminal or live-output capture running the focused event-path tests and showing the observed forwarding assertions.
  • Update the PR body with that evidence; if a fresh review does not trigger, ask a maintainer to comment @clawsweeper re-review.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR reports builds and test totals, but neither the body nor the supplied discussion includes inspectable current-head terminal/live output, a recording, or redacted runtime logs demonstrating the improved event behavior; add redacted evidence before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] The expanded comment makes broad lock-order and subscriber-behavior assertions; an inspectable current-head run of the focused event-path tests is needed before relying on those assertions as merge evidence.

Maintainer options:

  1. Decide the mitigation before merge
    Keep the focused regression tests and documentation, then attach a redacted current-head terminal or live-output capture that visibly runs the focused event-path checks and shows the retired-client suppression and current-client forwarding assertions passing.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • [P1] The remaining merge gate is contributor-owned current-head real-behavior proof, not a narrow source repair an automated worker can safely supply.

Security
Cleared: The diff is limited to C# tests and a source comment, with no dependency, workflow, permission, secret, or executable supply-chain change.

Review details

Best possible solution:

Keep the focused regression tests and documentation, then attach a redacted current-head terminal or live-output capture that visibly runs the focused event-path checks and shows the retired-client suppression and current-client forwarding assertions passing.

Do we have a high-confidence way to reproduce the issue?

Yes, at source level: the added focused tests invoke the captured clients' status event path and assert stale-client suppression plus current-client forwarding. A current-head inspectable run has not been supplied for this review.

Is this the best way to solve the issue?

Yes, conditionally: focused event-path regression tests and an invariant comment are a maintainable way to preserve the existing design, provided the PR adds direct current-head behavior proof rather than relying only on reported suite totals.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against ee4817d2c2e7.

Label changes

Label justifications:

  • P3: This is low-risk test and documentation hardening around an existing connection-lifecycle invariant, with no production behavior change.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR reports builds and test totals, but neither the body nor the supplied discussion includes inspectable current-head terminal/live output, a recording, or redacted runtime logs demonstrating the improved event behavior; add redacted evidence before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

What I checked:

  • Current branch scope: The supplied PR diff changes only the ForwardIfCurrent explanatory comment and NodeConnector tests; it adds no production control-flow change. (src/OpenClaw.Connection/NodeConnector.cs:221, 3e09e566f7e9)
  • Behavioral regression coverage: The added tests exercise the captured client event path for reconnect suppression, disconnect suppression, and current-client forwarding with a property-reading subscriber. (tests/OpenClaw.Connection.Tests/NodeConnectorTests.cs:137, 3e09e566f7e9)
  • Review continuity: The contributor's July 18, 2026 follow-up states that the tests were amended from structural preconditions to the actual subscribed RaiseStatusChanged path, addressing the prior stale-client forwarding finding. (tests/OpenClaw.Connection.Tests/NodeConnectorTests.cs:137, 3e09e566f7e9)
  • Repository proof policy: The repository policy requires PR real-behavior proof to directly show current-head behavior; the PR body and comments provide validation claims but no inspectable terminal capture, live output, recording, or redacted runtime log. (AGENTS.md, ee4817d2c2e7)

Likely related people:

  • unknown: The available review context identifies the NodeConnector connection surface but contains no current-main blame, log, reviewer, or merger attribution for this path; ownership should be routed through the recent connection-area history before merge. (role: likely connection-area owner; confidence: low; files: src/OpenClaw.Connection/NodeConnector.cs, tests/OpenClaw.Connection.Tests/NodeConnectorTests.cs)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.
Review history (2 earlier review cycles)
  • reviewed 2026-07-18T21:38:02.832Z sha fad5531 :: needs real behavior proof before merge. :: [P2] Exercise stale-client forwarding rather than only replacement identity
  • reviewed 2026-07-18T21:45:09.844Z sha 700d98c :: needs real behavior proof before merge. :: none

@shanselman

Copy link
Copy Markdown
Collaborator Author

GitHub Copilot maintainer-assistant update — automated, not written personally by Scott.

PR #1022 was amended at 700d98c4 so the tests now exercise the actual subscribed event path rather than only structural preconditions:

  • retired client after reconnect raises StatusChanged(Connected) through protected WebSocketClientBase.RaiseStatusChanged; ForwardIfCurrent suppresses it;
  • retired client after disconnect raises the same event; it is suppressed;
  • current client raises StatusChanged(Connecting) through ForwardIfCurrent; the subscriber reads connector properties under the lock and observes exact expected values without blocking.

Validation after amendment:

  • ./build.ps1 — all 5 projects passed;
  • Connection tests — 446 passed;
  • Shared tests — 2,901 passed, 31 skipped;
  • Tray tests — 1,823 passed;
  • focused authentic event-path tests — 3 passed.

This replaces the earlier tests that only proved client replacement/nulling and overclaimed direct forwarding coverage.

Exercises the real ForwardIfCurrent event path by invoking
WebSocketClientBase.RaiseStatusChanged via reflection on captured
WindowsNodeClient instances. This proves:

- Reconnect: StatusChanged on retired client A is suppressed by the
  generation check; events on current client B are forwarded.
- Disconnect: StatusChanged on a retired client is suppressed after
  disconnect (generation mismatch + null _client).
- Reentrancy safety: a StatusChanged subscriber can read
  IsConnected/PairingStatus/Mode/Client without blocking, observing
  consistent values from inside the _clientLifecycleLock scope.

Documents the lock ordering invariant at ForwardIfCurrent: this lock
is outermost; subscriber-owned locks must not re-enter connector
lifecycle operations.

Ref: #1010 (hazard 2)

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 102b1f54-0707-4f84-9f7f-dc254b38de70
@shanselman
shanselman force-pushed the shanselman-plan-nodeconnector-dispatch branch from 700d98c to 3e09e56 Compare July 18, 2026 21:53
@shanselman
shanselman merged commit 15ed439 into main Jul 18, 2026
19 checks passed
@shanselman
shanselman deleted the shanselman-plan-nodeconnector-dispatch branch July 18, 2026 22:16
@clawsweeper clawsweeper Bot mentioned this pull request Nov 19, 2026
4 of 16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P3 Low-risk cleanup, docs, polish, ergonomics, or speculative feature. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant