Skip to content

ruflet_server: test every protocol action on both receivers and every codec format; normalize invoke replies like ConnectionProtocol - #89

Merged
AdamMusa merged 11 commits into
mainfrom
test/server-protocol-contracts
Nov 21, 2026
Merged

AdamMusa merged 11 commits into
mainfrom
test/server-protocol-contracts

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Part of the release-readiness test pass. This PR covers ruflet_server.

What's tested

ProtocolActionContractTest sends every Protocol::ACTIONS entry, legacy JSON spellings included, through both receivers:

  • Ruflet::Server, the standalone server ruflet run starts;
  • a Ruflet::ConnectionProtocol host, the loop ruflet_rails includes.

It asserts the client gets the same result from either:

Action Asserted
register_client, registerWebClient reply (session id, empty error), route and client details on the page, app controls published
control_event, pageEventFromWeb handler runs with the event data, for target/name/data and eventTarget/eventName/eventData payloads
update_control, updateControlProps client props applied, with the control named by id, target or eventTarget
invoke_control_method the engine's success and error replies reach on_result; invoke_sync returns the result or raises the error; the callId and value aliases are delivered, each alone, together, and callId with a nil call_id; call_id wins over callId and result over value; a reply that is not a map is ignored, logs nothing, and the call stays pending
python_output accepted and ignored, nothing logged; session kept
patch_control (outbound) page.update sends it to the control's id
session_crashed (outbound) sent when the app raises during register

Each test records the action it covers. test_every_action_has_a_test_on_both_receivers fails if a Protocol::ACTIONS entry has no test on either receiver. It checks that a test exists, not what the test asserts. Log checks ignore the incoming action= line RUFLET_DEBUG=1 adds, so the suite also passes with it set.

WireCodecContractTest covers every MessagePack format the codec writes or reads, on both sides of each size limit:

  • fixint through int64/uint64, f32/f64;
  • fixstr through str32, bin8 through bin32;
  • arrays and maps up to 32-bit sizes (16-bit forms at 16 and 65535 entries);
  • the three Flet extension types, written as ext8/16/32 and read from fixext too.

For each format it checks the spec's marker byte and the round trip. When the msgpack gem is installed, it also checks agreement with that gem in both directions; without the gem those checks skip. A new test-only step in build-ruflet-client.yml installs msgpack 1.8.5 so they run in CI. No Ruflet package depends on it.

Coverage: ruflet_server goes from 64 to 227 tests, and the codec from 9 cases to 142. On main, form_wire_test.rb and event_client_value_wire_test.rb drive register_client, update_control and control_event through Ruflet::Server#handle_message (the second also through a ConnectionProtocol host), and connection_protocol_test.rb sends python_output and a crashing register to a ConnectionProtocol host. No ruflet_server test sends the legacy spellings or an invoke reply through either receiver, or python_output or a crashing register through Ruflet::Server. (Outside ruflet_server, ruflet_rails/test/protocol_endpoint_test.rb and the H1 rails_endpoint goldens also drive register_client and control_event through ConnectionProtocol.)

Fix

Ruflet::Server#on_invoke_control_method passed the client's reply through unchanged. ConnectionProtocol normalizes it with Protocol.normalize_invoke_method_result_payload, and now the standalone server does too. The regression test, test_standalone_server_accepts_a_callId_and_value_invoke_reply in protocol_compatibility_test.rb, fails without the fix.

The first commit's message says older clients send callId/value. That is wrong (see below); the code comments now say so.

Behavior changes

Both come from the fix in Ruflet::Server#on_invoke_control_method. ruflet_server/lib is also compiled into the embedded VM (ruby_runtime/vm/mrbgems/ruflet-framework/mrbgem.rake), so embedded apps get them at the next VM rebuild. Replies in the engine's shape behave exactly as before (see Verified).

  1. The standalone server now applies the normalizer's two aliases, as ConnectionProtocol (ruflet_rails) already did. callId is read when call_id is missing, nil or false. value is read when the reply has no result key. On Ruflet::Server, for page.invoke(..., on_result:) and invoke_sync:

    Reply Before After
    callId + value reply ignored: on_result got (nil, "execution expired") once the call's timeout passed (10 s by default; it never ran with timeout: nil); invoke_sync raised Timeout::Error on_result(v, nil) at once; invoke_sync returns v
    callId + result/error, or call_id nil or false + callId reply ignored, as above delivered at once, like the same reply keyed call_id
    call_id + value, no result key delivered without the value: on_result(nil, nil); invoke_sync returned nil on_result(v, nil); invoke_sync returns v
    • A reply with both result and value still uses result; one with both call_id and callId uses call_id.
    • No shipped client sends either alias. InvokeMethodResponseBody#toMap writes control_id/call_id/result/error; the file is the same blob in ruflet-engine codex/engine-initialization (98e41857, and f44e7df7 before it) and template main (8b085697). _onInvokeMethod in ruflet_backend.dart is the only place either sends an invoke reply. Those replies always carry call_id and a result key, so neither alias can fire for them.
    • git log --all -G"[\"']callId[\"'] *[:=]" -- '*.dart' finds nothing in ruflet-engine, ruflet-template or ruflet. Ruflet's history includes the former bundled ruflet_client.
    • The aliases entered the normalizer in ac394fc, and no client ever used them.
    • Justification: the two receivers gave different results for the same reply. Now they match.
  2. A reply that is an array, integer, float or true no longer logs invoke callback error: .... Before, Page#handle_invoke_method_result indexed it with "call_id". That raised TypeError (array, integer) or NoMethodError (float, true) internally, and the error was rescued and logged.

    • Before and after, that reply runs no callback and the call stays pending until its timeout, when on_result gets (nil, "execution expired"). Only the log line is gone.
    • This matches ConnectionProtocol.
    • nil and false (which handle_message turns into {}), strings and extension values were ignored silently before and still are.
    • Justification: the log described an internal indexing error, not the reply.

Verified on 71b1c6a (includes main 06c6e28)

  • ruflet_server, one process per file: 17 files, 227 runs, 0 failures, 0 skips on Ruby 4.0.5 (msgpack 1.8.5), on 3.4.5 (msgpack 1.8.7), and on 4.0.5 with RUFLET_DEBUG=1. Main: 15 files, 64 runs.
  • All files in one process (4.0.5, seeds 11 and 54321): 227 runs, 0 failures. The contract test with seeds 1, 7 and 42: 29 runs, 0 failures each.
  • CI (Build Ruflet Client Prebuilds, run 44858929920, Verify Ruflet Explorer): installed msgpack 1.8.5; the 17 ruflet_server files ran 227 runs, 0 skips; wire_codec_contract_test ran 133 runs, 0 skips.
  • H1 goldens: compat.rb --jobs 4 checked 320 goldens, exit 0. compat_goldens_test ran 328 runs, 0 failures.
  • Planted bugs: the tests catch 32 of 33. Caught bugs include the fix reverted, each alias dropped, callId preferred over call_id, callId read only when the call_id key is absent, value preferred over result, the normalizer dropping error, the eventData, target and eventTarget fallbacks dropped, python_output raising on Ruflet::Server (which only logs it), each 16-bit map, array and bin limit lowered to 65534, a new Protocol::ACTIONS entry with no test, and invoke_sync ignoring the error.
    • The survivor weakens the coverage guard so it no longer tells the two receivers apart. Every action has a test on both, so nothing exposes it.
  • Default timeout, main vs this head, page.invoke with no timeout: and one reply each. On main's Ruflet::Server, callId + value, callId + result, and callId with call_id nil or false each got (nil, "execution expired") at 10.1 s; with timeout: nil on_result never ran; invoke_sync(timeout: 2) raised Timeout::Error. On this head each is delivered at once. ConnectionProtocol gives this head's results on both. An array reply with timeout: 2 got the timeout error at 2.1 s on both receivers, before and after.
  • I drove Ruflet::Server#handle_message on main and on this head with 33 reply shapes. 25 gave the same result on both, including every engine shape: success, error, false, a nested map with ext types, extra keys, a duplicate reply, and invoke_sync success, error and empty error. The 8 that differ are the alias rows in Behavior change 1. On main, the 5 shapes with an array, integer, float or true payload logged invoke callback error; on this head none do (Behavior change 2).
  • Embedded VM: in the packaged embedded_mruby harness (framework 0.0.21, --preload) with this head's on_invoke_control_method loaded, engine replies give the same results as without it; callId + value, call_id + value, and callId with call_id nil or false are delivered; the array reply no longer logs. mrbc compiles server.rb, connection_protocol.rb and protocol.rb.
  • Template main and the published gems are unchanged. Their clients send control_id/call_id/result/error, and this PR touches only ruflet_server and a CI test step.

Ruflet::ConnectionProtocol (ruflet_rails and other host adapters)
normalizes the client's invoke_control_method reply with
Protocol.normalize_invoke_method_result_payload. Ruflet::Server, which
`ruflet run` starts, has its own receiver and passed the payload through
as is. So a reply spelled callId/value, as older clients send it, reached
its on_result under Rails but was silently dropped by the standalone
server.

Normalize it the same way.
…c's formats

ProtocolActionContractTest sends every Protocol action, legacy JSON
spellings included, through Ruflet::Server and through a
Ruflet::ConnectionProtocol host. It checks each gives the client the
same result:
- register: the reply, the route and client details, and the app's
  controls published;
- control events: the handler runs, with event data, including
  eventTarget/eventName payloads;
- update_control: props applied;
- invoke replies: delivered;
- python_output: ignored;
- page updates: sent as patch_control;
- an app that raises on register: session_crashed.

It also fails if an action is added to Protocol::ACTIONS without a test
here.

WireCodecContractTest covers every MessagePack format the codec writes
or reads, at each size boundary (fixint to int64/uint64, f32/f64,
fixstr to str32, bin8 to bin32, arrays and maps to 32-bit sizes, and the
three Flet extension types as ext8/16/32 and fixext). For each it checks
the marker byte, the round trip and, when the msgpack gem is installed,
agreement with it in both directions.
The comments called callId/value the spelling older clients send. No
shipped client has sent it: the engine's InvokeMethodResponseBody and
the template's client write control_id/call_id/result/error. The fix
makes Ruflet::Server treat a reply the way ConnectionProtocol already
does; say that instead.
… action

The invoke contract test sent only successful replies, so dropping the
reply's error in Protocol.normalize_invoke_method_result_payload passed
every test. Send the engine's error reply too, to on_result and to
invoke_sync, which must raise it, and invoke_sync's success reply.

The coverage guard compared Protocol::ACTIONS with a hand-written list,
so naming an action there without a test passed it. Each test now
records the action it covers, and the guard checks every action has one
on both receivers.
wire_codec_contract_test skips its checks against the msgpack gem when
the gem is missing, and CI never installed it, so 45 of them skipped.
Install it (test-only; no Ruflet package depends on it).
Ruflet::Server used to index such a reply directly, which raised
TypeError inside Page#handle_invoke_method_result and logged
"invoke callback error". Normalized, it is ignored silently, as
ConnectionProtocol already did, and the pending call stays pending.
@AdamMusa AdamMusa changed the title ruflet_server: test every protocol action on both receivers and every codec format; fix dropped callId/value replies ruflet_server: test every protocol action on both receivers and every codec format; normalize invoke replies like ConnectionProtocol Nov 20, 2026
…and the 16-bit size limits

The contract test now sends the callId and value aliases separately and a
reply carrying both result and value, the eventData key and the target and
eventTarget ids, and maps, arrays and bins of 65535 entries. Warnings are
checked without the RUFLET_DEBUG trace, so the suite passes with
RUFLET_DEBUG=1, and python_output on Ruflet::Server must log nothing.
The alias contract test now sends a reply carrying call_id and a different
callId, and one with call_id nil and callId set. The compatibility test is
renamed to say it sends callId, and its comment says what the standalone
server did before: it ignored the reply, and on_result got the timeout
error once the call's timeout passed.
@AdamMusa
AdamMusa merged commit 3370544 into main Nov 21, 2026
19 of 20 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.

1 participant