next: the real durable-execution KERNEL SERVICE (host_kernel) — RA-live substrate

Promotes the persistent-kernel spike into a real service. next/kernel/host_kernel.erl: boots
flow_store, registers named behavior flows (blog_digest), then blocks in http:listen so the
er-scheduler + gen_server stay alive across requests. Parameterised flow routes (paths matched by
byte prefix — binary =:= is buggy): GET /flow/start/<category> starts the flow with that category and
returns '<InstanceId>:<status>' (suspended|done); GET /flow/resume/<id> resumes that instance. Path
plumbing (starts_with / last_seg / field) is byte-level for portability.

next/kernel/serve.sh: the persistent service launcher (container entrypoint / local) — loads the
runtime + next/flow + the kernel, then host_kernel:start(); sleep infinity holds stdin so
the listener serves forever. next/tests/host_kernel.sh: drives it over HTTP — 4/4: newsletter →
instance 1 SUSPENDED, urgent → 2 DONE, draft → 3 DONE (skipped), resume 1 in a SEPARATE request →
DONE (durable state persists across requests). serve.sh launcher verified live (bind + start +
resume).

This is the RA-live substrate: a working durable-execution service the host drives over HTTP.
Remaining for RA-live: deploy it (a container/placement), point host/ra.sx's real-eval at it (POST
/flow instead of in-process erlang-eval-ast), route a durable binding to RA. TA-live adds inbox/
outbox routes on the same kernel.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-02 17:11:47 +00:00
parent 836d32474f
commit 22e2835bdb
3 changed files with 178 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
%% next/kernel/host_kernel.erl — the persistent DURABLE-EXECUTION kernel the host talks to (RA-live).
%% A long-lived next/ service: boots flow_store + registers named behavior flows, then blocks in
%% http:listen so the er-scheduler (and flow_store's gen_server) stay alive across requests. The host
%% drives durable flows over HTTP; instances persist between requests (spike-proven).
%%
%% Routes (path-parameterised — binary =:= is buggy in this port, so paths are matched by byte
%% prefix, not equality):
%;; GET /flow/start/<category> -> start blog_digest with that category -> "<InstanceId>:<status>"
%% (status = suspended | done)
%% GET /flow/resume/<id> -> resume instance <id> (morning timer) -> "resume:<status>"
%% This is the substrate for RA-live; TA-live adds inbox/outbox routes on the same kernel.
-module(host_kernel).
-export([start/1]).
start(Port) ->
flow_store:start_link(),
FF = fun (_) -> [f1, f2, f3] end,
flow_store:register_flow(blog_digest, blog_publish_digest:build([{fetch_followers, FF}])),
http:listen(Port, fun (Req) -> route(Req) end).
route(Req) ->
[{status, 200}, {headers, []}, {body, respond(field(path, Req))}].
respond(P) ->
case starts_with(P, [47,102,108,111,119,47,115,116,97,114,116,47]) of % "/flow/start/"
true -> do_start(last_seg(P));
false ->
case starts_with(P, [47,102,108,111,119,47,114,101,115,117,109,101,47]) of % "/flow/resume/"
true -> do_resume(last_seg(P));
false -> <<"path:unknown">>
end
end.
%% start blog_digest with the path's <category> -> "<Id>:suspended" | "<Id>:done"
do_start(CatChars) ->
Cat = list_to_atom(CatChars),
Env = [{activity, [{type, create}, {actor, site}, {id, <<110,49>>},
{object, [{type, article}, {category, Cat}]}]},
{actor, site}],
case flow_store:start(blog_digest, Env) of
{ok, Id, {flow_suspended, _}} -> id_status(Id, [115,117,115,112,101,110,100,101,100]); % "suspended"
{ok, Id, {flow_done, _}} -> id_status(Id, [100,111,110,101]); % "done"
_ -> <<"start:other">>
end.
%% resume instance <id> -> "resume:done" | "resume:other"
do_resume(IdChars) ->
case flow_store:resume(list_to_integer(IdChars), morning_ts) of
{ok, {flow_done, _}} -> <<"resume:done">>;
{flow_done, _} -> <<"resume:done">>;
_ -> <<"resume:other">>
end.
%% "<Id>:<statusChars>" as a response binary
id_status(Id, StatusChars) ->
list_to_binary(integer_to_list(Id) ++ [58] ++ StatusChars). % ++ ":" ++
%% ── plumbing (byte-level; string-literal charlists avoided for portability) ──
starts_with(Bin, Prefix) -> starts_with_(binary_to_list(Bin), Prefix).
starts_with_(_, []) -> true;
starts_with_([C | T], [C | P]) -> starts_with_(T, P);
starts_with_(_, _) -> false.
last_seg(Bin) -> last_seg_(binary_to_list(Bin), []).
last_seg_([], Acc) -> lists:reverse(Acc);
last_seg_([47 | T], _) -> last_seg_(T, []); % reset at each '/'
last_seg_([C | T], Acc) -> last_seg_(T, [C | Acc]).
field(K, [{K, V} | _]) -> V;
field(K, [_ | Rest]) -> field(K, Rest);
field(_, []) -> nil.