diff --git a/next/kernel/http_server.erl b/next/kernel/http_server.erl index 1cc5e49a..cf1a7482 100644 --- a/next/kernel/http_server.erl +++ b/next/kernel/http_server.erl @@ -10,7 +10,8 @@ post_activity_response/0, validation_failed_response/0, cid_response/1, - accept_format/1, accept_format_from/1]). + accept_format/1, accept_format_from/1, + capabilities_body_for/1]). %% HTTP request router per design §16.1. %% @@ -43,6 +44,11 @@ route(Req, Cfg) -> case {M, P} of {<<80,79,83,84>>, <<47,97,99,116,105,118,105,116,121>>} -> handle_post_activity(Req, Cfg); + {<<71,69,84>>, + <<47,46,119,101,108,108,45,107,110,111,119,110, + 47,115,120,45,99,97,112,97,98,105,108,105,116,105,101,115>>} -> + F = accept_format_from(Req), + ok_response(capabilities_body_for(F)); _ -> dispatch(M, P) end. @@ -356,3 +362,30 @@ accept_format_from(Req) -> not_found -> text end end. + +%% capabilities_body_for/1 — content-negotiated capability bodies. +%% Each format returns a distinct byte sequence so dispatch can be +%% observed end-to-end. Real serialisation (JSON-LD, dag-cbor, etc.) +%% lands once the corresponding encoder BIFs are wired; v1 uses +%% tagged stubs that are syntactically the right shape. +capabilities_body_for(text) -> + capabilities_body(); +%% `{"caps":"fed-sx-m1"}\n` — 21 bytes +capabilities_body_for(json) -> + <<123,34,99,97,112,115,34,58,34, + 102,101,100,45,115,120,45,109,49,34,125,10>>; +capabilities_body_for(activity_json) -> + %% Same payload as :json — the difference is the Content-Type + %% header (Step 8d-content-type follow-up); body shape matches. + capabilities_body_for(json); +%% `(caps "fed-sx-m1")\n` — 19 bytes +capabilities_body_for(sx) -> + <<40,99,97,112,115,32,34, + 102,101,100,45,115,120,45,109,49,34,41,10>>; +%% A minimal CBOR map: 0xA1 0x64 "caps" 0x69 "fed-sx-m1" +%% A1 = map(1); 64 = text(4) "caps"; 69 = text(9) "fed-sx-m1" +capabilities_body_for(cbor) -> + <<161,100,99,97,112,115,105, + 102,101,100,45,115,120,45,109,49>>; +capabilities_body_for(_) -> + capabilities_body(). diff --git a/next/tests/http_capabilities_format.sh b/next/tests/http_capabilities_format.sh new file mode 100755 index 00000000..40942a7b --- /dev/null +++ b/next/tests/http_capabilities_format.sh @@ -0,0 +1,133 @@ +#!/usr/bin/env bash +# next/tests/http_capabilities_format.sh — Step 8d-dispatch-cap test. +# +# Proves Accept header dispatch end-to-end on the +# /.well-known/sx-capabilities route. 12 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 + +# Shared bindings for the test: +# AK = "accept" header key +# CapPath = capabilities path (looked up from the module) +PRELUDE='AK = <<97,99,99,101,112,116>>, CapPath = http_server:capabilities_path(),' + +cat > "$TMPFILE" <> -> ok; _ -> bad end\") :name)") + +;; sx body starts with '(' (40) +(epoch 13) +(eval "(get (erlang-eval-ast \"case http_server:capabilities_body_for(sx) of <<40, _/binary>> -> ok; _ -> bad end\") :name)") + +;; cbor body starts with 0xA1 (161) — map(1) +(epoch 14) +(eval "(get (erlang-eval-ast \"case http_server:capabilities_body_for(cbor) of <<161, _/binary>> -> ok; _ -> bad end\") :name)") + +;; activity_json shares its body with json +(epoch 15) +(eval "(get (erlang-eval-ast \"http_server:capabilities_body_for(activity_json) =:= http_server:capabilities_body_for(json)\") :name)") + +;; Unknown format falls back to text +(epoch 16) +(eval "(get (erlang-eval-ast \"http_server:capabilities_body_for(weird_format) =:= http_server:capabilities_body()\") :name)") + +;; Route with Accept: application/json -> json body +(epoch 17) +(eval "(get (erlang-eval-ast \"${PRELUDE} AV = <<97,112,112,108,105,99,97,116,105,111,110,47,106,115,111,110>>, Req = [{method, <<71,69,84>>}, {path, CapPath}, {headers, [{AK, AV}]}], R = http_server:route(Req), case R of [_, _, {body, B}] -> B =:= http_server:capabilities_body_for(json); _ -> false end\") :name)") + +;; Route with Accept: application/sx -> sx body +(epoch 18) +(eval "(get (erlang-eval-ast \"${PRELUDE} AV = <<97,112,112,108,105,99,97,116,105,111,110,47,115,120>>, Req = [{method, <<71,69,84>>}, {path, CapPath}, {headers, [{AK, AV}]}], R = http_server:route(Req), case R of [_, _, {body, B}] -> B =:= http_server:capabilities_body_for(sx); _ -> false end\") :name)") + +;; Route with Accept: application/cbor -> cbor body +(epoch 19) +(eval "(get (erlang-eval-ast \"${PRELUDE} AV = <<97,112,112,108,105,99,97,116,105,111,110,47,99,98,111,114>>, Req = [{method, <<71,69,84>>}, {path, CapPath}, {headers, [{AK, AV}]}], R = http_server:route(Req), case R of [_, _, {body, B}] -> B =:= http_server:capabilities_body_for(cbor); _ -> false end\") :name)") + +;; No Accept header -> text body +(epoch 20) +(eval "(get (erlang-eval-ast \"${PRELUDE} Req = [{method, <<71,69,84>>}, {path, CapPath}], R = http_server:route(Req), case R of [_, _, {body, B}] -> B =:= http_server:capabilities_body(); _ -> false end\") :name)") + +;; POST capabilities still 404 +(epoch 21) +(eval "(get (erlang-eval-ast \"${PRELUDE} Req = [{method, <<80,79,83,84>>}, {path, CapPath}], case http_server:route(Req) of [{status, 404} | _] -> ok; _ -> bad end\") :name)") +EPOCHS + +OUTPUT=$(timeout 60 "$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 2 "module load name" "http_server" +check 10 "text format = existing body" "true" +check 11 "all format stubs distinct" "true" +check 12 "json body starts with '{'" "ok" +check 13 "sx body starts with '('" "ok" +check 14 "cbor body starts with 0xA1" "ok" +check 15 "activity_json == json body" "true" +check 16 "unknown format -> text" "true" +check 17 "Accept: json -> json body" "true" +check 18 "Accept: sx -> sx body" "true" +check 19 "Accept: cbor -> cbor body" "true" +check 20 "no Accept -> text body" "true" +check 21 "POST capabilities still 404" "ok" + +TOTAL=$((PASS+FAIL)) +if [ $FAIL -eq 0 ]; then + echo "ok $PASS/$TOTAL next/tests/http_capabilities_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 2e6f5365..c0e07381 100644 --- a/plans/fed-sx-milestone-1.md +++ b/plans/fed-sx-milestone-1.md @@ -518,7 +518,8 @@ publish(ActorId, ActivityRequest) -> - [x] **8c-post-publish-srv** — gen_server wrapper around nx_kernel: `start_link/3`, named-process `publish/1`, `query/0`, `log_tip/0`, `with_projections/1`, `stop/0`. `next/tests/nx_kernel_server.sh` (11 cases). HTTP layer integration follows. - [x] **8c-post-publish-http** — POST `/activity` handler now calls `nx_kernel:publish/1` when the kernel process is registered; falls back to the existing stub when not. Success → 200 with `cid: \n` body via `cid_response/1`; sig/replay failures → 422 via `validation_failed_response/0`. `next/tests/http_publish.sh` (10 cases). - [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). -- [ ] **8d-dispatch** — Wire format dispatch into responses (e.g., the `cid_response`/`actor_doc_response`/etc. emit body shapes matching the chosen format). +- [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). +- [ ] **8d-dispatch-rest** — Same treatment for `actor_doc_response`, `artifact_response`, `projection_response`, `cid_response` (plus Content-Type headers). **Deliverables:** @@ -997,6 +998,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-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. - **2026-05-28** — Step 5c-populate: `bootstrap:populate_registry/0` walks `read_genesis` output and calls `registry:register/3` (gen_server API) for each entry. Total return is 31 = 3 + 10 + 7 + 3 + 3 + 2 + 3 across the seven kinds, matching the manifest authored in Step 4. `next/tests/bootstrap_populate.sh` 14/14 verifies per-kind counts + lookups against known names (`activity_types/create`, `object_types/define-activity`, `validators/envelope-shape`). Erlang conformance 729/729. - **2026-05-28** — Step 9-pre-fold: in-process integration test proving the full POST → publish → broadcast → projection-fold chain. With `projection:start_link` + `nx_kernel:start_link` + `nx_kernel:with_projections([p_count, p_collect])`, three authorized POST `/activity` calls advance both projections to 3 — and the kernel's log to 3 entries — and the projection's collected activity carries the POST body as `:object`. Unauthorized or sig-failed POSTs leave projection state unchanged. Step 9a/b proper (curl-driven smoke tests) wait on Step 8b-start (TCP) + Define\* SX-source eval bridge, but the structural chain is already verified end-to-end. `next/tests/http_publish_fold.sh` 10/10. Erlang conformance 729/729. Step 9 split into 9-pre-fold (done) + 9a + 9b.