feat(remote): implement proxy from-substitution and outbound ID routing

Inbound wire messages are now delivered FROM the proxy representing the
remote sender, so local actors see a replyable `from`. This enables full
two-way communication across the wire:

- endpoint: deliver send_named/send via proxy (deliver_named/deliver_pid)
  instead of sending raw; from_id=0 bypasses proxy for anonymous sends

- proxy: handle deliver_named and deliver_pid to send from within actor
  scope (providing from-substitution); cache one owned pid clone per sender
  keyed by stable instance_id() to avoid use-after-free when forwarding
  reply handles to the endpoint asynchronously

- test: add remote_endpoint_id_test covering the full inbound proxy table /
  from-substitution / outbound ID table / send-by-ID round-trip

- test: extend remote_child_endpoint with echo_id actor and send_wire_by_id
  to support the new test
This commit is contained in:
CJ van den Berg 2026-03-10 12:59:13 +01:00
parent 734bb6c784
commit e75eb2d6cd
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
5 changed files with 290 additions and 36 deletions

View file

@ -0,0 +1,118 @@
/// Tests inbound proxy table, from-substitution, outbound ID table, and
/// inbound send-by-ID routing in a single three-step round-trip:
///
/// 1. Parent sends send_named to "echo_id" (from_id=0, trigger mode).
/// 2. Child echo_id sends send_named back (from_id=3) proxy created,
/// delivered FROM proxy to TestActor (from-substitution).
/// 3. TestActor replies to proxy via from.send() outbound ID assigned,
/// wire send reaches child with from_id=wire_id_TA, to_id=3.
/// 4. Child echo_id replies via wire send to wire_id_TA parent routes
/// via local_actors to TestActor (inbound send-by-ID).
/// 5. TestActor receives ["done"] exits "success".
const std = @import("std");
const thespian = @import("thespian");
const endpoint = @import("endpoint");
const build_options = @import("build_options");
const Allocator = std.mem.Allocator;
const result = thespian.result;
const unexpected = thespian.unexpected;
const pid_ref = thespian.pid_ref;
const Receiver = thespian.Receiver;
const message = thespian.message;
const TestActor = struct {
allocator: Allocator,
ep: thespian.pid,
receiver: Receiver(*@This()),
state: enum { waiting_for_hello, waiting_for_done },
const Args = struct { allocator: Allocator };
fn start(args: Args) result {
return init(args) catch |e| return thespian.exit_error(e, @errorReturnTrace());
}
fn init(args: Args) !void {
thespian.trace_to_json_file("remote_endpoint_id_trace.json");
thespian.env.get().enable_all_channels();
thespian.env.get().proc_set("test_receiver", thespian.self_pid().ref());
const argv = try args.allocator.dupe(u8, message.fmt(.{build_options.remote_child_endpoint_path}).buf);
const ep = try thespian.spawn_link(
args.allocator,
endpoint.Args{
.allocator = args.allocator,
.argv = argv,
},
endpoint.start,
"endpoint",
);
// Trigger child echo_id in trigger mode (from_id=0).
try ep.send(.{ "send", @as(u64, 0), "echo_id", .{"hello"} });
const self = try args.allocator.create(@This());
self.* = .{
.allocator = args.allocator,
.ep = ep,
.receiver = .init(receive_fn, deinit, self),
.state = .waiting_for_hello,
};
errdefer self.deinit();
thespian.receive(&self.receiver);
}
fn deinit(self: *@This()) void {
self.ep.deinit();
self.allocator.destroy(self);
}
fn receive_fn(self: *@This(), from: pid_ref, m: message) result {
return self.receive(from, m) catch |e| return thespian.exit_error(e, @errorReturnTrace());
}
fn receive(self: *@This(), from: pid_ref, m: message) !void {
switch (self.state) {
.waiting_for_hello => {
if (try m.match(.{"hello"})) {
// Reply through the proxy this populates the outbound ID table
// and sends a wire send to the child with our assigned wire ID.
try from.send(.{"hello"});
self.state = .waiting_for_done;
} else return unexpected(m);
},
.waiting_for_done => {
if (try m.match(.{"done"})) {
// Arrived via inbound send-by-ID routing through local_actors.
return thespian.exit("success");
} else return unexpected(m);
},
}
}
};
test "remote: inbound proxy table, from-substitution, outbound ID table, and send-by-ID routing" {
const allocator = std.testing.allocator;
var ctx = try thespian.context.init(allocator);
defer ctx.deinit();
var success = false;
var exit_handler = thespian.make_exit_handler(&success, struct {
fn handle(ok: *bool, status: []const u8) void {
ok.* = std.mem.eql(u8, status, "success");
}
}.handle);
_ = try ctx.spawn_link(
TestActor.Args{ .allocator = allocator },
TestActor.start,
"test_actor",
&exit_handler,
null,
);
ctx.run();
if (!success) return error.TestFailed;
}