← Back to Pull requests

sdk: dial routers over WebSocket, so both SDKs join sam-one #515

Closed
unknown closed this pull request without merging on Sep 26, 2026 12:17 UTC
View on GitHub
unknown unknown commented on Sep 26, 2026 10:06 UTC
Author

Both SDKs now join sam-one, directly and behind --tunnel cloudflare, and tests hold them to both.

Why

sam-one's embedded router listens on WebSocket alone, on the single port that also serves its control plane (/ws, or /wss behind --tunnel). Both SDKs dialed TCP only, so the "Run your own" path of the Native SDKs guide admitted neither of them. Nothing tested that path.

Change

  • JS (sdk/js/src/host.ts): @libp2p/websockets joins tcp() in the host's transports. It is the maintained transport (backpressure, /ws, /wss, /tls/sni/…/ws, listeners), declared in package.json; the lockfile gains it and its four dependencies.

  • Python (sdk/python/src/agent_mesh/host.py): new_host(enable_websocket=True) adds py-libp2p 0.7's WebSocket transport; the address filter no longer drops /ws and /wss.

  • Python behind a TLS edge: with sam-one --tunnel cloudflare the router is /dns4/<host>.trycloudflare.com/tcp/443/wss/p2p/<id>, and Cloudflare selects the origin by TLS SNI and the Host header. py-libp2p 0.7 resolves the name to its IP before dialing and then puts the IP in both, which Cloudflare answers with 403 Forbidden. The Python host now dials a WebSocket address by its name (what go-libp2p and js-libp2p do), and dial_addrs no longer pre-resolves /dns4 on WebSocket addresses. This is a py-libp2p bug, present on its main too; to be reported upstream.

  • Python verifies the wss certificate: py-libp2p 0.7 dials wss with verify_mode=CERT_NONE unless given a TLS client context. The host passes ssl.create_default_context(), the system roots, as the JS (ws → Node TLS) and Go hosts verify.
  • Verified against a real tunnel

    bin/sam-one --tunnel cloudflare --tunnel-install, then through https://<x>.trycloudflare.com: JS agent ← Python caller (2.4 s) and Python agent ← JS caller (3.0 s), both answering with the verified caller. The first Python attempt was the 403 above; the fix is what made it pass.

    Tests

  • TestStandaloneSDKAgents (~4.5 s): starts sam-one, runs each language's A2A agent example against it and calls it with the other language's A2A caller (js-calls-python, python-calls-js). Mutation-checked: with the JS transport removed the JS agent fails with "no router admitted this member"; with the Python transport removed the Python agent fails with "no transport found for …/ws/…".

  • TestStandaloneSDKAgentsBehindTLSEdge (~4.5 s): the same pair behind an in-test TLS-terminating reverse proxy reached as https://localhost:<port> that, like Cloudflare, fails the handshake for any other SNI and answers any other Host with 403; sam-one gets it as ExternalURL and advertises /dns4/localhost/…/wss. Both SDKs trust the edge certificate through NODE_EXTRA_CA_CERTS / SSL_CERT_FILE. Mutation-checked: without the name-preserving dial the Python agent fails the edge's TLS handshake ("unknown server name"); without SSL_CERT_FILE it fails with CERTIFICATE_VERIFY_FAILED.

  • host.test.ts: a mesh host dials /ws and /tcp listeners with TLS+yamux; a /dns4 WebSocket dial carries the name in Host.

  • test_host.py: /ws and /tls/ws addresses are dialable; a host reaches a /ws listener; a /dns4 WebSocket dial carries the name in Host (fails with 127.0.0.1 without the fix); wss is dialed with CERT_REQUIRED and hostname checking (fails without the context).

  • runExample takes the repository root instead of an sdkMesh.
  • Docs

    sdk/README.md and the Native SDKs guide say the SDKs dial TCP and WebSocket and what sam-one listens on. The browser stays out of scope, stated precisely: routers and nodes accept libp2p TLS alone, which a browser cannot speak (that needs Noise on the Go side), and the SDK keeps its state and speaks HTTP on streams with Node's modules.

    Not in this PR (was in the first version)

  • The hand-written WebSocket transport, replaced by @libp2p/websockets.

  • The Buffer/node:crypto/node:fs portability refactor: it left libp2p-http.ts on node:http, so it did not make the package browser-loadable, and it imported @noble/curves and @multiformats/multiaddr-matcher without declaring them.

  • tests/ui/agents.spec.js and the runner split: the sessions ran in the Playwright Node process, not in the browser, and the console coverage exists in console.spec.js.

  • The demo page and videos; the page documented session.serve, which the SDK no longer has.
  • Validation: npm run typecheck, npm test (70), pytest (82), TestStandaloneSDKAgents*|TestStandaloneNodeJoin|TestNativeSDKsMesh|TestNativeSDKA2A|TestNativeSDKExamples|TestSDKCanaryScript, hack/verify-sdk-generated.sh, hack/verify-secrets.sh, golangci-lint v2.14.0 on tests/integration.

    Diff: 595 lines (+446 / -149) across 7 files Reviewer: aojea Review duration: 130m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 595 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.382
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #515"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50% (MEDIUM) · Higher = larger blast radius
    50%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.8% · Safe to merge
    19.8%