openclaw-windows-node icon indicating copy to clipboard operation
openclaw-windows-node copied to clipboard

[deep-review][duplication] D4: _useV2Signature fallback state machine duplicated across operator and node clients

Open codemonkeychris opened this issue 6 months ago • 2 comments

Summary

The v3→v2 signature fallback (see also C10) is implemented twice — once in OpenClawGatewayClient, once in WindowsNodeClient — with the same three-piece state (flag declaration, set-on-error, read-on-connect).

Where

  • src/OpenClaw.Shared/OpenClawGatewayClient.cs:51, 881-899, 1494-1499 (operator)
  • src/OpenClaw.Shared/WindowsNodeClient.cs:37, 568-575, 828-830 (node)

The base class WebSocketClientBase exists and is the natural home for shared connection state, but currently only owns the WS transport and reconnect backoff.

Why it matters

Three concrete problems:

  1. Future drift risk. If C10 (reset-after-N-successes) is implemented in one client but not the other, the operator and node would have different downgrade behaviours for the same gateway. The two clients share an identity directory and a v2 flag in GatewayConnectionManager._gatewayNeedsV2Signature, so a divergent client implementation introduces subtle staleness.
  2. Test surface is doubled. Every signature-fallback test currently covers one client; the other might silently drift.
  3. Audit cost. A security reviewer looking at the v3-to-v2 fallback has to read both implementations and verify they agree.

Proposed fix

Lift the state to WebSocketClientBase (or a small SignatureNegotiationState helper):

protected bool UseV2Signature { get; private set; }
protected void OnSignatureRejected();   // bumps state machine
protected void OnHandshakeSucceeded();  // resets counters, may probe v3 again

Both OpenClawGatewayClient and WindowsNodeClient call the protected method on error / success. The state machine (including C10's reset-after-N-successes) lives in one place.

Make the migration in two PRs:

  1. Land the helper; have both clients call it without changing behaviour.
  2. Implement C10's reset-after-N-successes inside the helper. Both clients get the fix.

Severity

Low — duplication that becomes Medium when C10 is addressed (because the fix would have to be done twice otherwise).

Related

  • C10 (sticky downgrade).

codemonkeychris avatar May 14 '26 00:05 codemonkeychris


[!NOTE] The pull request was not created — a fallback review issue was created instead due to protected file changes: #431

🤖 This is an automated response from Repo Assist.

Good analysis. A two-step migration is the right call here — it avoids introducing risk before the behaviour is proven stable.

A concrete sketch for step 1 using an inner type in WebSocketClientBase:

// WebSocketClientBase.cs
protected sealed class SignatureNegotiationState
{
    private volatile bool _useV2;

    public bool UseV2 => _useV2;

    /// Call when the gateway rejects the v3 signature.
    public void OnRejected() => _useV2 = true;

    /// Call on each successful authenticated handshake.
    /// Returns true when the state was reset (useful for logging).
    public bool OnHandshakeSucceeded()
    {
        if (!_useV2) return false;
        _useV2 = false;
        return true;
    }
}

protected readonly SignatureNegotiationState SignatureNegotiation = new();

Both OpenClawGatewayClient and WindowsNodeClient can replace their _useV2Signature field with SignatureNegotiation.UseV2 / .OnRejected() / .OnHandshakeSucceeded(). The state machine is now in one place.

One thing to be aware of: _useV2Signature currently lives as a per-instance bool, but GatewayConnectionManager._gatewayNeedsV2Signature is a separate field that acts as a persistent memo across reconnects. Make sure step 1 doesn't silently drop that cross-reconnect persistence — the per-instance state in SignatureNegotiationState should be re-seeded from GatewayConnectionManager on each reconnect, the same way the existing booleans are today.

For step 2 (C10: reset-after-N-successes), the OnHandshakeSucceeded method is the natural place to add a counter.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@97143ac59cb3a13ef2a77581f929f06719c7402a

github-actions[bot] avatar May 17 '26 12:05 github-actions[bot]

Closing during the post-refactor deep-review cleanup. This duplicates the v3/v2 fallback concern tracked by #350 and #372. This keeps the signal while reducing stale one-off issue noise.

shanselman avatar May 18 '26 00:05 shanselman