From dd7b7d7a2d52ce22bf26615421efd05f8fd3b175 Mon Sep 17 00:00:00 2001 From: giles Date: Thu, 28 May 2026 15:39:23 +0000 Subject: [PATCH] =?UTF-8?q?fed-sx-m1:=20Step=208d-dispatch-post=20?= =?UTF-8?q?=E2=80=94=20format-aware=20POST=20/activity=20(cid=5Fresponse?= =?UTF-8?q?=5Ffor=20+=20post=5Factivity=5Fresponse=5Ffor)=20+=2013=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- next/kernel/http_server.erl | 75 +++++++++++++++-- next/tests/http_post_format.sh | 142 +++++++++++++++++++++++++++++++++ plans/fed-sx-milestone-1.md | 4 +- 3 files changed, 212 insertions(+), 9 deletions(-) create mode 100755 next/tests/http_post_format.sh diff --git a/next/kernel/http_server.erl b/next/kernel/http_server.erl index 7f7fe6e5..dc29a3ad 100644 --- a/next/kernel/http_server.erl +++ b/next/kernel/http_server.erl @@ -12,7 +12,8 @@ cid_response/1, accept_format/1, accept_format_from/1, capabilities_body_for/1, - content_type_for/1, ok_response/2]). + content_type_for/1, ok_response/2, + cid_response_for/2, post_activity_response_for/1]). %% HTTP request router per design §16.1. %% @@ -209,27 +210,30 @@ post_activity_response() -> handle_post_activity(Req, Cfg) -> case check_bearer(Req, Cfg) of ok -> - publish_if_kernel(Req); + F = accept_format_from(Req), + publish_if_kernel(Req, F); {error, _} -> unauthorized_response() end. -%% publish_if_kernel/1 — if the nx_kernel gen_server is registered, +%% publish_if_kernel/2 — if the nx_kernel gen_server is registered, %% delegate the publish there and translate the result. Otherwise %% keep the stub response so the auth-only tests stay green without -%% having to spin up a kernel process. -publish_if_kernel(Req) -> +%% having to spin up a kernel process. Format threads through to +%% both stub and CID responses so the Content-Type matches what +%% the client asked for via Accept. +publish_if_kernel(Req, F) -> case erlang:whereis(nx_kernel) of undefined -> - post_activity_response(); + post_activity_response_for(F); _Pid -> Body = field(body, Req), Request = [{type, create}, {object, Body}], case nx_kernel:publish(Request) of {ok, Result} -> case envelope:get_field(cid, Result) of - {ok, Cid} -> cid_response(Cid); - _ -> post_activity_response() + {ok, Cid} -> cid_response_for(Cid, F); + _ -> post_activity_response_for(F) end; {error, _} -> validation_failed_response() @@ -423,3 +427,58 @@ ok_response(Body, Format) -> [{status, 200}, {headers, [{CTKey, content_type_for(Format)}]}, {body, Body}]. + +%% cid_response_for/2 — format-aware version of cid_response/1. +%% Each variant emits a syntactically appropriate body for the +%% chosen format and tags the response with the matching +%% Content-Type via ok_response/2. + +cid_response_for(Cid, text) -> + cid_response(Cid); +%% `{"cid":""}\n` — 8-byte prefix + cid + 3-byte suffix +cid_response_for(Cid, json) -> + Pre = <<123,34,99,105,100,34,58,34>>, % '{"cid":"' + Suf = <<34,125,10>>, % '"}\n' + ok_response(<
>, json);
+cid_response_for(Cid, activity_json) ->
+    Pre = <<123,34,99,105,100,34,58,34>>,
+    Suf = <<34,125,10>>,
+    ok_response(<
>, activity_json);
+%% `(cid "")\n` — 6-byte prefix + cid + 3-byte suffix
+cid_response_for(Cid, sx) ->
+    Pre = <<40,99,105,100,32,34>>,          % '(cid "'
+    Suf = <<34,41,10>>,                      % '")\n'
+    ok_response(<
>, sx);
+%% v1 cbor stub: the raw CID bytes with the application/cbor CT.
+%% Real cbor encoding (A1 63 cid 78  ...) lands later.
+cid_response_for(Cid, cbor) ->
+    ok_response(Cid, cbor);
+cid_response_for(Cid, _) ->
+    cid_response(Cid).
+
+%% post_activity_response_for/1 — format-aware version of
+%% post_activity_response/0 (the kernel-absent stub).
+
+post_activity_response_for(text) ->
+    post_activity_response();
+%% `{"status":"stub"}\n` — hand-spelled
+post_activity_response_for(json) ->
+    Body = <<123,34,115,116,97,116,117,115,34,58,34,
+             115,116,117,98,34,125,10>>,
+    ok_response(Body, json);
+post_activity_response_for(activity_json) ->
+    Body = <<123,34,115,116,97,116,117,115,34,58,34,
+             115,116,117,98,34,125,10>>,
+    ok_response(Body, activity_json);
+%% `(status "stub")\n`
+post_activity_response_for(sx) ->
+    Body = <<40,115,116,97,116,117,115,32,34,
+             115,116,117,98,34,41,10>>,
+    ok_response(Body, sx);
+post_activity_response_for(cbor) ->
+    %% Same body as text but with cbor CT — clients see the same
+    %% bytes as the text fallback. Step 8d-cbor encoder will replace.
+    [_, _, {body, Body}] = post_activity_response(),
+    ok_response(Body, cbor);
+post_activity_response_for(_) ->
+    post_activity_response().
diff --git a/next/tests/http_post_format.sh b/next/tests/http_post_format.sh
new file mode 100755
index 00000000..eb6d6be5
--- /dev/null
+++ b/next/tests/http_post_format.sh
@@ -0,0 +1,142 @@
+#!/usr/bin/env bash
+# next/tests/http_post_format.sh — Step 8d-dispatch-post test.
+#
+# Verifies POST /activity returns format-specific bodies + the
+# right Content-Type, both for the kernel-absent stub path and
+# the kernel-present cid response. 14 cases.
+
+set -uo pipefail
+cd "$(git rev-parse --show-toplevel)"
+
+SX_SERVER="${SX_SERVER:-hosts/ocaml/_build/default/bin/sx_server.exe}"
+if [ ! -x "$SX_SERVER" ]; then
+  SX_SERVER="/root/rose-ash/hosts/ocaml/_build/default/bin/sx_server.exe"
+fi
+if [ ! -x "$SX_SERVER" ]; then
+  echo "ERROR: sx_server.exe not found." >&2
+  exit 1
+fi
+
+VERBOSE="${1:-}"
+PASS=0; FAIL=0; ERRORS=""
+TMPFILE=$(mktemp); trap "rm -f $TMPFILE" EXIT
+
+cat > "$TMPFILE" <<'EPOCHS'
+(epoch 1)
+(load "lib/erlang/tokenizer.sx")
+(load "lib/erlang/parser.sx")
+(load "lib/erlang/parser-core.sx")
+(load "lib/erlang/parser-expr.sx")
+(load "lib/erlang/parser-module.sx")
+(load "lib/erlang/transpile.sx")
+(load "lib/erlang/runtime.sx")
+(load "lib/erlang/vm/dispatcher.sx")
+(epoch 2)
+(eval "(er-load-gen-server!)")
+(epoch 3)
+(eval "(get (erlang-load-module (file-read \"next/kernel/envelope.erl\")) :name)")
+(epoch 4)
+(eval "(get (erlang-load-module (file-read \"next/kernel/log.erl\")) :name)")
+(epoch 5)
+(eval "(get (erlang-load-module (file-read \"next/kernel/pipeline.erl\")) :name)")
+(epoch 6)
+(eval "(get (erlang-load-module (file-read \"next/kernel/outbox.erl\")) :name)")
+(epoch 7)
+(eval "(get (erlang-load-module (file-read \"next/kernel/nx_kernel.erl\")) :name)")
+(epoch 8)
+(eval "(get (erlang-load-module (file-read \"next/kernel/http_server.erl\")) :name)")
+
+;; cid_response_for(json) body: {"cid":"foo"}\n
+(epoch 10)
+(eval "(get (erlang-eval-ast \"R = http_server:cid_response_for(<<102,111,111>>, json), case R of [_, _, {body, B}] -> B =:= <<123,34,99,105,100,34,58,34,102,111,111,34,125,10>>; _ -> false end\") :name)")
+
+;; cid_response_for(json) CT is application/json
+(epoch 11)
+(eval "(get (erlang-eval-ast \"R = http_server:cid_response_for(<<102,111,111>>, json), case R of [_, {headers, [{_, CT}]}, _] -> CT =:= http_server:content_type_for(json); _ -> false end\") :name)")
+
+;; cid_response_for(sx) body: (cid "foo")\n
+(epoch 12)
+(eval "(get (erlang-eval-ast \"R = http_server:cid_response_for(<<102,111,111>>, sx), case R of [_, _, {body, B}] -> B =:= <<40,99,105,100,32,34,102,111,111,34,41,10>>; _ -> false end\") :name)")
+
+;; cid_response_for(text) matches cid_response/1
+(epoch 13)
+(eval "(get (erlang-eval-ast \"http_server:cid_response_for(<<102,111,111>>, text) =:= http_server:cid_response(<<102,111,111>>)\") :name)")
+
+;; cid_response_for(activity_json) body == cid_response_for(json) body
+(epoch 14)
+(eval "(get (erlang-eval-ast \"[_, _, {body, BJ}] = http_server:cid_response_for(<<102,111,111>>, json), [_, _, {body, BAJ}] = http_server:cid_response_for(<<102,111,111>>, activity_json), BJ =:= BAJ\") :name)")
+
+;; cid_response_for(activity_json) CT is application/activity+json
+(epoch 15)
+(eval "(get (erlang-eval-ast \"R = http_server:cid_response_for(<<102,111,111>>, activity_json), case R of [_, {headers, [{_, CT}]}, _] -> CT =:= http_server:content_type_for(activity_json); _ -> false end\") :name)")
+
+;; cid_response_for(cbor) carries the raw CID as body
+(epoch 16)
+(eval "(get (erlang-eval-ast \"R = http_server:cid_response_for(<<102,111,111>>, cbor), case R of [_, _, {body, B}] -> B =:= <<102,111,111>>; _ -> false end\") :name)")
+
+;; post_activity_response_for(json) has json CT
+(epoch 17)
+(eval "(get (erlang-eval-ast \"R = http_server:post_activity_response_for(json), case R of [_, {headers, [{_, CT}]}, _] -> CT =:= http_server:content_type_for(json); _ -> false end\") :name)")
+
+;; post_activity_response_for(text) matches the original
+(epoch 18)
+(eval "(get (erlang-eval-ast \"http_server:post_activity_response_for(text) =:= http_server:post_activity_response()\") :name)")
+
+;; End-to-end: POST /activity with Accept: application/json returns
+;; the json stub when nx_kernel is not running
+(epoch 19)
+(eval "(get (erlang-eval-ast \"Token = <<102,111,111>>, AuthKey = <<97,117,116,104,111,114,105,122,97,116,105,111,110>>, AuthVal = <<66,101,97,114,101,114,32,102,111,111>>, AcceptKey = <<97,99,99,101,112,116>>, AcceptVal = <<97,112,112,108,105,99,97,116,105,111,110,47,106,115,111,110>>, Req = [{method, <<80,79,83,84>>}, {path, http_server:activity_path()}, {headers, [{AuthKey, AuthVal}, {AcceptKey, AcceptVal}]}, {body, <<>>}], Cfg = [{publish_token, Token}], R = http_server:route(Req, Cfg), case R of [_, {headers, [{_, CT}]}, _] -> CT =:= http_server:content_type_for(json); _ -> false end\") :name)")
+
+;; End-to-end: POST /activity with kernel running + Accept: application/sx
+;; returns body shaped as (cid "...")
+(epoch 20)
+(eval "(get (erlang-eval-ast \"KM = <<1,2,3,4>>, KS = [{key_id,k1},{algorithm,ed25519},{value,KM}], AS = [{public_keys,[[{id,k1},{created,0},{value,KM}]]}], nx_kernel:start_link(alice, KS, AS), Token = <<102,111,111>>, AuthKey = <<97,117,116,104,111,114,105,122,97,116,105,111,110>>, AuthVal = <<66,101,97,114,101,114,32,102,111,111>>, AcceptKey = <<97,99,99,101,112,116>>, AcceptVal = <<97,112,112,108,105,99,97,116,105,111,110,47,115,120>>, Req = [{method, <<80,79,83,84>>}, {path, http_server:activity_path()}, {headers, [{AuthKey, AuthVal}, {AcceptKey, AcceptVal}]}, {body, <<104,105>>}], Cfg = [{publish_token, Token}], R = http_server:route(Req, Cfg), case R of [_, _, {body, B}] -> http_server:match_prefix(<<40,99,105,100,32,34>>, B) =/= nomatch; _ -> false end\") :name)")
+
+;; End-to-end CT for kernel-publish with json Accept matches application/json
+(epoch 21)
+(eval "(get (erlang-eval-ast \"KM = <<1,2,3,4>>, KS = [{key_id,k1},{algorithm,ed25519},{value,KM}], AS = [{public_keys,[[{id,k1},{created,0},{value,KM}]]}], nx_kernel:start_link(alice, KS, AS), Token = <<102,111,111>>, AuthKey = <<97,117,116,104,111,114,105,122,97,116,105,111,110>>, AuthVal = <<66,101,97,114,101,114,32,102,111,111>>, AcceptKey = <<97,99,99,101,112,116>>, AcceptVal = <<97,112,112,108,105,99,97,116,105,111,110,47,106,115,111,110>>, Req = [{method, <<80,79,83,84>>}, {path, http_server:activity_path()}, {headers, [{AuthKey, AuthVal}, {AcceptKey, AcceptVal}]}, {body, <<104,105>>}], Cfg = [{publish_token, Token}], R = http_server:route(Req, Cfg), case R of [_, {headers, [{_, CT}]}, _] -> CT =:= http_server:content_type_for(json); _ -> false end\") :name)")
+EPOCHS
+
+OUTPUT=$(timeout 240 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
+
+check() {
+  local epoch="$1" desc="$2" expected="$3"
+  local actual
+  actual=$(echo "$OUTPUT" | awk -v e="$epoch" '
+    $0 ~ "^\\(ok-len " e " " { getline; print; exit }
+    $0 ~ "^\\(ok " e " "     { print; exit }
+    $0 ~ "^\\(error " e " "  { print; exit }
+  ')
+  [ -z "$actual" ] && actual=""
+  if echo "$actual" | grep -qF -- "$expected"; then
+    PASS=$((PASS+1))
+    [ "$VERBOSE" = "-v" ] && echo "  ok $desc"
+  else
+    FAIL=$((FAIL+1))
+    ERRORS+="  FAIL [$desc] (epoch $epoch) expected: $expected | actual: $actual
+"
+  fi
+}
+
+check  8  "http_server loaded"                "http_server"
+check 10  "cid_response_for(json) body"       "true"
+check 11  "cid_response_for(json) CT"         "true"
+check 12  "cid_response_for(sx) body"         "true"
+check 13  "cid_response_for(text) preserves"  "true"
+check 14  "activity_json body == json body"   "true"
+check 15  "activity_json CT differs"          "true"
+check 16  "cbor carries raw cid"              "true"
+check 17  "post_activity stub json CT"        "true"
+check 18  "post_activity stub text preserves" "true"
+check 19  "POST kernel-absent json CT"        "true"
+check 20  "POST kernel-publish sx body"       "true"
+check 21  "POST kernel-publish json CT"       "true"
+
+TOTAL=$((PASS+FAIL))
+if [ $FAIL -eq 0 ]; then
+  echo "ok $PASS/$TOTAL next/tests/http_post_format.sh passed"
+else
+  echo "FAIL $PASS/$TOTAL passed, $FAIL failed:"
+  echo "$ERRORS"
+fi
+[ $FAIL -eq 0 ]
diff --git a/plans/fed-sx-milestone-1.md b/plans/fed-sx-milestone-1.md
index 14807149..e9df0c59 100644
--- a/plans/fed-sx-milestone-1.md
+++ b/plans/fed-sx-milestone-1.md
@@ -520,7 +520,8 @@ publish(ActorId, ActivityRequest) ->
 - [x] **8d-accept** — `accept_format/1` + `accept_format_from/1` parse the Accept header into `:activity_json | :json | :sx | :cbor | :text`. Priority: activity+json > json > sx > cbor; everything else falls to text. `next/tests/http_accept.sh` (13 cases).
 - [x] **8d-dispatch-cap** — `capabilities_body_for/1` returns distinct stubs per format (json `{...}`, sx `(...)`, cbor `A1 64 caps 69 fed-sx-m1`); activity_json shares the json body. Route intercepts GET capabilities to thread the Accept format through `accept_format_from/1`. `next/tests/http_capabilities_format.sh` (13 cases).
 - [x] **8d-content-type** — `content_type_for/1` maps format atoms to MIME-type binaries (text/plain, application/json, application/activity+json, application/sx, application/cbor). `ok_response/2(Body, Format)` builds a 200 response with the right Content-Type header. `next/tests/http_content_type.sh` (13 cases).
-- [ ] **8d-dispatch-rest** — Wire `ok_response/2` + format dispatch into `actor_doc_response`, `artifact_response`, `projection_response`, `cid_response`.
+- [x] **8d-dispatch-post** — POST `/activity` now threads the Accept format through both kernel-present (`cid_response_for/2` → `{"cid":""}` for json / `(cid "")` for sx / raw bytes for cbor) and kernel-absent (`post_activity_response_for/1` → `{"status":"stub"}` / `(status "stub")` / etc.) paths. `next/tests/http_post_format.sh` (13 cases) covers shape + Content-Type for both stub and publish paths.
+- [ ] **8d-dispatch-get** — Same treatment for `actor_doc_response`, `artifact_response`, `projection_response` on the GET paths.
 
 **Deliverables:**
 
@@ -999,6 +1000,7 @@ A few things still under-specified; resolve as work begins.
 Newest first. One line per sub-deliverable commit. Erlang conformance gate
 (`bash lib/erlang/conformance.sh`) must remain 729/729 on every entry.
 
+- **2026-05-28** — Step 8d-dispatch-post: `handle_post_activity` extracts the Accept format via `accept_format_from/1` and threads it into `publish_if_kernel/2`. Both success paths emit format-specific bodies: `cid_response_for/2` produces `{"cid":""}\n` (json/activity_json), `(cid "")\n` (sx), raw CID bytes (cbor), or the existing text form; `post_activity_response_for/1` mirrors for the kernel-absent stub. Each response carries the matching Content-Type. End-to-end POSTs with `Accept: application/json` / `application/sx` verified through the full HTTP→nx_kernel→publish→cid_response_for chain. `next/tests/http_post_format.sh` 13/13. Erlang conformance 729/729.
 - **2026-05-28** — Step 8d-content-type: `content_type_for/1` maps format atoms to MIME-type binaries — text/plain (10b), application/json (16b), application/activity+json (25b), application/sx (14b), application/cbor (16b); unknown formats fall through to text/plain. `ok_response/2(Body, Format)` constructs a 200 response with `{headers, [{<<"content-type">>, MIME}]}`. Lowercase header key matches how the BIF wrapper normalises request headers. `ok_response/1` still produces the empty-headers shape — backward compat preserved. `next/tests/http_content_type.sh` 13/13. Erlang conformance 729/729.
 - **2026-05-28** — Step 8d-dispatch-cap: `capabilities_body_for/1` returns distinct byte sequences per format — text reuses the existing `capabilities_body/0`; json/activity_json share `{"caps":"fed-sx-m1"}`; sx returns `(caps "fed-sx-m1")`; cbor returns a minimal `A1 64 caps 69 fed-sx-m1` map. Route now intercepts GET `/.well-known/sx-capabilities` to pull the Accept format via `accept_format_from/1` and dispatch through `capabilities_body_for`. Unknown formats fall back to text. POST capabilities still 404 (only GET handled). `next/tests/http_capabilities_format.sh` 13/13 verifies all formats + the intercept + no-Accept default. Content-Type headers not yet set (8d-dispatch-rest covers headers + applying the same shape to actor/artifact/projection/cid responses). Erlang conformance 729/729.
 - **2026-05-28** — Step 8d-accept: `accept_format/1` + `accept_format_from/1` parse the Accept header into a content-negotiation atom. Priority order via successive `match_prefix` checks: application/activity+json → `activity_json`; application/json → `json`; application/sx → `sx`; application/cbor → `cbor`; everything else (including nil / empty / non-binary) → `text`. Comma-separated lists with activity+json first still resolve to activity_json — leading-prefix match is sufficient for v1 envelopes. Step 8d split into 8d-accept (done) + 8d-dispatch (wire into response bodies). `next/tests/http_accept.sh` 13/13. Erlang conformance 729/729.