Files
ruflo/v3/@claude-flow
Dragan SpiridonovandRuflo & AQE 6776bd2d7a fix(federation): read the untrusted-relay envelope the gateway actually sends (#3304)
* fix(federation): read the untrusted-relay envelope the gateway actually sends

#3300 started wrapping relay-sourced results in a nonce-fenced provenance
envelope. The server shipped; the client kept calling JSON.parse on the whole
string, so it died on the first word of the gateway's own prose:

  Unexpected token 'T', "The block "... is not valid JSON

That killed `ruflo federation sync`, `roster` and `claims`, and the
x_federation_* MCP tools behind them — 3 of 4 federation reads, on 3.41.2.
`registry` kept working because that response is not fenced, which made the
outage read as intermittent rather than total.

The suite stayed green throughout because every mock returned bare JSON — a
contract the server had stopped honouring. The new fixtures are built with the
gateway's own fenceUntrusted(), so the next envelope change fails here instead
of in production.

Two things this deliberately does NOT do:

- It does not unwrap to `.data`. The whole point of #3300 is that a caller can
  tell whose words these are; quietly returning the payload would restore valid
  JSON by discarding the labelling that made it safe to read. The full envelope
  (untrusted/provenance/relay/retrievedAt/data) is returned.
- It does not match the fence loosely. The token backreference is what stops a
  publisher forging an END marker to make later text read as gateway narration,
  so a mismatched token does not close the region. An unterminated envelope
  throws rather than silently dropping relay content.

Verified against live x.ruv.io: federation_sync, ruv://swarm/roster and
ruv://claims/board are all fenced and all failed the old parser with the exact
error above; the unfenced registry resource is unaffected. Mutating the parser
back to plain JSON.parse turns 6 of the new tests red with that same string.

Full @claude-flow/cli suite: 90 failed files / 112 failed tests both before and
after (pre-existing), passing 2426 -> 2433. tsc: 463 errors before and after.

Co-Authored-By: Ruflo & AQE
Claude-Session: https://claude.ai/code/session_01NpXyuvm9QACWhD8egqw8zU

* fix(federation): Seraphina had the same bug, and three review findings

An independent adversarial review of the first commit found one blocker and
three should-fixes. All four are addressed here.

BLOCKER — seraphina-tools.ts had the identical defect, unfixed.
`askSeraphina` runs its own gateway client and reads three fenced endpoints
(roster, claims board, federation_sync), so `seraphina_guidance` — registered at
mcp-client.ts:186 — was dead on 3.41.2 with the same error. The first commit's
claim of "3 of 4 federation reads" was 3 of 4 tools in ONE file.

Its test file carried the same trap the first commit diagnosed: bare-JSON mocks
for endpoints the server had started fencing. Those fixtures now use the
gateway's own fenceUntrusted(), which turns the existing context assertion into
a real regression guard.

The obvious fix there would have corrupted silently. seraphina-tools.ts reads
`recent.messages` and `Object.keys(roster)` directly, so returning the envelope
(right at the MCP boundary) would have given Seraphina an empty message list and
a roster miscounted as 5 — no error, and she assigns work on it. Added
`relayPayload()` for internal consumers, and said plainly in the comment why the
two shapes exist.

S2 — the two "adversarial" tests did not test what they claimed. Both passed
with the backreference deleted, with the newline anchors deleted, and with the
quantifier made greedy. The real neutraliser today is that the body is
JSON.stringify'd onto a single line, so publisher text cannot contain a raw
newline. The backreference is the defence that survives a switch to pretty
printing, so it is now pinned by a test built on a hand-made envelope; deleting
\1 turns exactly that test red. The code comment named the wrong mechanism as
load-bearing and has been corrected.

S3 — exec() is non-global and first match wins. fenceUntrusted splices its
`note` verbatim BEFORE the fence, so a caller interpolating relay-derived text
into a note could smuggle in an earlier complete envelope and have it returned
with untrusted:false. Now refuses a response carrying more than one envelope.

The first attempt at that guard counted raw marker occurrences and introduced a
denial of service: a publisher typing the literal marker into a message made
every read throw. Caught by the existing hostile-payload test. The count is
newline-anchored, matching the fence itself.

S4 — `parsed` was computed before `r.isError` was consulted, so an SDK
createToolError (raw text, not JSON) surfaced as "Unexpected token 'p'" instead
of its message. isError is checked first, falling back to the raw text.

Reviewer confirmed and I verified: no fenced response can carry a top-level
error, and x-federation-channels.ts / x-federation-join.ts speak Nostr directly
rather than /mcp, so they were correctly out of scope.

Every new guard has a mutant that reddens its own test: backreference removed,
seraphina reverted to bare parse, seraphina returning the envelope, and isError
checked after parsing. Live against x.ruv.io the internal path reads 25 messages
where the envelope bug reads 0, and an empty roster where it reads 5.

Full @claude-flow/cli suite: 90 failed files / 112 failed tests before and after
(pre-existing); passing 2426 -> 2439. tsc: 463 before and after.

Co-Authored-By: Ruflo & AQE
Claude-Session: https://claude.ai/code/session_01NpXyuvm9QACWhD8egqw8zU
2026-09-16 02:21:20 +00:00
..