Skip to content

feat(engine): add REDIRECT control frame primitive - #1755

Merged
jfallows merged 7 commits into
developfrom
feature/1753-redirect-frame
May 8, 2026
Merged

jfallows merged 7 commits into
developfrom
feature/1753-redirect-frame

Conversation

@jfallows

@jfallows jfallows commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Introduces a new engine-level control frame REDIRECT (type id 0x40000005)
that flows upstream from consumer to producer, mirroring RESET /
CHALLENGE direction. REDIRECT carries a new affinity (the hash result
from a protocol-aware consumer) and the round-tripped BEGIN extension,
giving a consumer binding a way to abort a misrouted stream and instruct
the parent to reissue BEGIN with a different affinity.

This PR lands the foundational primitive only — the frame ABI plus
engine throttle dispatch and k3po-zilla test-transport support.

Frame shape

struct Redirect extends Frame [0x40000005]
{
    int64 affinity;     // the new affinity to use
    octets extension;   // round-tripped from BEGIN
}

Design note (vs. the original issue): REDIRECT carries the hash result
as int64 affinity, not the bytes-to-hash. This avoids any coupling
between protocol-aware bindings and the choice of hash function — the
consumer pre-hashes and the parent's logic becomes "use this affinity
instead of the one on the original BEGIN." The frame shape mirrors
Begin.affinity exactly, so the parent's resolver call shape doesn't
change. The original BEGIN's affinity is dropped from the frame entirely
— the parent already knows it because it dispatched the BEGIN.

Engine internals

  • EngineWorker throttle dispatch recognizes RedirectFW.TYPE_ID on both
    initial and reply paths and removes the dispatcher (REDIRECT closes the
    stream, like RESET).
  • Target write paths recognize RedirectFW.TYPE_ID, remove the stream
    from the streams map, and drop any registered streamId-set entry; on
    the reply path also drop any pending correlation.

k3po-zilla test transport

  • New ADVISORY_REDIRECT StructuredTypeInfo registered as a
    read-advisory + write-advisory pair (mirroring ADVISORY_CHALLENGE).
  • New ZillaExtensionKind.REDIRECT (unaligned) with overrides routing
    both encodeBuffer and decodeBuffer to writeExtBuffer. This keeps
    the script-supplied affinity isolated from the BEGIN ext, which lives
    in readExtBuffer(BEGIN) for the round-trip.
  • ZillaStreamFactory.doRedirect: reads the script-supplied affinity via
    redirectEx.getLong(redirectEx.readerIndex()) and pulls the BEGIN ext
    bytes from readExtBuffer(BEGIN) (absolute-index reads, so it survives
    any prior read zilla:begin.ext advancing the reader index).
  • ZillaTarget.doRedirect(...): builds a RedirectFW with affinity and
    round-tripped extension, plus carry-through authorization.
  • Throttle.onRedirect: surfaces only RedirectFW.affinity() to
    writeExtBuffer(REDIRECT, false).writeLong(affinity), unregisters the
    throttle, and fires ADVISORY_REDIRECT. The frame's extension is
    consumed but not surfaced to the script — the round-trip is silent.

DSL form

read advise zilla:redirect  0x00000000000000b1L
write advised zilla:redirect 0x00000000000000b1L

A single long literal (Long.decode() parsed). Native byte order on the
channel buffer; round-trip is consistent regardless of platform.

Test plan

  • simplex/server.sent.read.advise.redirect.ext/{client,server}.rpt
    peer-to-peer scripts exercise the full encode/decode path with a
    non-empty zilla:begin.ext round-trip.
  • SimplexIT#shouldReceiveServerSentReadAdviseRedirectWithExtension
    runs the scripts; 37/37 SimplexIT tests pass.
  • DuplexIT/HalfDuplexIT continue to pass (no regressions in CHALLENGE
    handling).
  • 316/316 engine unit tests pass.
  • ./mvnw checkstyle:check -pl runtime/engine clean.

Acceptance criteria addressed

  • REDIRECT frame defined in engine ABI with documented fields,
    including round-tripped extension.
  • traceId preserved across redirect (carried on Frame).
  • authorization preserved (carried on Frame and forwarded in
    k3po doRedirect).
  • k3po-zilla transport supports read advise zilla:redirect /
    write advised zilla:redirect.
  • Test: consumer emits REDIRECT; original stream is closed at the
    engine throttle level (dispatcher removal); ext is round-tripped
    onto the new frame.

Fixes #1753

Claude and others added 7 commits May 8, 2026 06:39
Introduce a new engine-level control frame `REDIRECT` (type id 0x40000005)
that flows upstream from consumer to producer, mirroring `RESET` /
`CHALLENGE` direction. `REDIRECT` carries the original `BEGIN`'s
`affinity` plus a new `hash` and round-tripped `extension`, giving a
consumer binding a way to abort the current stream and request the
parent reissue the `BEGIN` through the affinity resolver with a
different routing key.

This commit lands the foundational primitive only — the frame ABI plus
engine throttle dispatch and k3po-zilla test transport support — so
downstream PRs (binding-http parent handler, binding-mcp emitter,
AffinityResolver caller updates) have something concrete to build
against. The cluster-side AffinityResolver SPI does not yet exist on
develop, so the parent-handler / opt-in capability discovery / loop
bound enforcement bullets from the issue are deferred.

Engine ABI (specs/engine.spec/.../core.idl):
- new struct `Redirect extends Frame [0x40000005]` with `affinity`,
  `hash : string16`, and round-tripped `extension : octets`

Engine internals:
- `EngineWorker` throttle dispatch recognizes `RedirectFW.TYPE_ID` on
  both initial and reply paths and removes the dispatcher (REDIRECT
  closes the stream, like RESET)
- `Target` write paths recognize `RedirectFW.TYPE_ID`, remove the
  stream from the streams map, and drop any registered streamId set
  entry; on the reply path, also drop any pending correlation

k3po-zilla transport (test scope):
- new `ADVISORY_REDIRECT` `StructuredTypeInfo` registered as a
  read-advisory + write-advisory pair, mirroring `ADVISORY_CHALLENGE`
- new `ZillaExtensionKind.REDIRECT` (unaligned) for ext buffer routing
- `ZillaTarget.doRedirect(...)` encodes a `RedirectFW` carrying the
  channel's affinity and the extension bytes from the read-ext buffer
- `Throttle.handleThrottle` decodes incoming `RedirectFW`, copies the
  ext bytes into the write-ext buffer for `REDIRECT`, unregisters the
  throttle, and fires `ADVISORY_REDIRECT` so the producer-side script
  observes `write advised zilla:redirect`
- `ZillaStreamFactory.doRedirect(...)` resolves the sender, hands off
  to `ZillaTarget.doRedirect`, and unregisters the stream
- `ZillaSource.doAdviseInput` dispatches `ADVISORY_REDIRECT` to
  `ZillaStreamFactory.doRedirect` so `read advise zilla:redirect`
  triggers the encode path
- `ZillaBehaviorSystem` registers `newReadAdviseRedirectHandler` and
  `newWriteAdvisedRedirectHandler` so the existing
  `read advise / write advised` DSL accepts `zilla:redirect` with the
  same matcher/value shape as `zilla:challenge`

Test coverage:
- new peer-to-peer scripts under
  `simplex/server.sent.read.advise.redirect.ext/{client,server}.rpt`
  exercise the full encode/decode path with a non-empty extension
- new `SimplexIT.shouldReceiveServerSentReadAdviseRedirectWithExtension`
  test method runs the scripts; `shouldReceiveClientSent...` and the
  duplex/half.duplex equivalents are intentionally not added yet —
  Phase 1 only seeds the primitive and one canonical test

DSL form for now:

    read advise zilla:redirect <matcher>
    write advised zilla:redirect <value>

The richer indented `hash` / `ext` sub-element form sketched in the
issue would require k3po grammar work in `aklivity/k3po` and is
deferred. The current shape carries an opaque extension blob which
matches the wire `extension` field; the wire `hash` is currently
emitted as the empty string and is reserved for the follow-on PR that
introduces the sub-field DSL alongside the binding handler.

Acceptance-criteria status from #1753:
- [x] `REDIRECT` frame defined in engine ABI with documented fields
- [x] `traceId` preserved across redirect (carried on `Frame`)
- [x] `authorization` preserved (carried on `Frame` and forwarded in
       k3po `doRedirect`)
- [x] k3po-zilla transport supports `read advise zilla:redirect` /
       `write advised zilla:redirect` (ext value form)
- [x] Test: consumer emits `REDIRECT`; original stream is closed at
       the engine throttle level (dispatcher removal); ext is
       roundtripped into the producer's read view
- [ ] `binding-http` server `onRedirect` handler — deferred, depends
       on AffinityResolver SPI not yet on develop
- [ ] `binding-mcp` server emits `REDIRECT` — deferred
- [ ] `AffinityResolver` callers accept external hash key — deferred
- [ ] One-redirect-per-lineage enforcement — deferred to handler PR
- [ ] Capability discovery / non-supporting parent error — deferred
- [ ] `hash` / `ext` indented sub-element DSL — deferred (k3po grammar)
…via readExtBuffer

Per review feedback on the Phase 1 commit:

1. The BEGIN extension round-trip on REDIRECT is mechanical (whatever
   arrived on BEGIN is what's emitted on REDIRECT), so test scripts
   shouldn't have to restate it. Only the `hash` is interesting at the
   script level.

2. The BEGIN ext bytes are already stashed in `readExtBuffer(BEGIN)` by
   `ZillaStreamFactory.onBegin` — no separate snapshot field needed.

3. Since REDIRECT flows in the reverse direction (consumer → producer,
   like CHALLENGE), the script-provided hash for `read advise
   zilla:redirect` should land in `writeExtBuffer` so it never collides
   with the BEGIN ext that's living in `readExtBuffer`.

DSL is now:

    read advise zilla:redirect "abc123xyz"
    write advised zilla:redirect "abc123xyz"

with a single value/matcher representing the hash bytes.

Implementation:

- `ZillaExtensionKind.REDIRECT` overrides `encodeBuffer` and
  `decodeBuffer` to both return `writeExtBuffer`. Encode-side this
  isolates the hash from the BEGIN ext on the consumer; decode-side
  it lines up with `Throttle.onRedirect` writing the incoming hash
  bytes into `writeExtBuffer(REDIRECT, false)` (mirror of CHALLENGE's
  reverse-direction handling).

- `ZillaStreamFactory.doRedirect`: read the script-provided hash from
  `writeExtBuffer(REDIRECT, true)`, and the BEGIN ext bytes from
  `readExtBuffer(BEGIN, true)`. Both buffers passed through to
  `ZillaTarget.doRedirect`.

- `ZillaTarget.doRedirect`: takes the begin-ext as a `ChannelBuffer`
  and copies via a new `roundTripExtCopy` helper that reads from
  absolute index 0 to `writerIndex()`. Necessary because the script
  may have already consumed the BEGIN ext via `read zilla:begin.ext`
  (advancing `readerIndex`), and we still need to round-trip the
  underlying bytes onto `REDIRECT.extension`.

- `Throttle.onRedirect`: surfaces only `RedirectFW.hash().value()` to
  `writeExtBuffer(REDIRECT, false)`. The frame's `extension` field is
  consumed internally and not exposed to the script — the round-trip
  is silent.

Updated peer-to-peer scripts demonstrate the full mechanic:
- producer-side `client.rpt` writes a non-empty `zilla:begin.ext` and
  later expects `write advised zilla:redirect "abc123xyz"`
- consumer-side `server.rpt` reads the same `begin.ext` and issues
  `read advise zilla:redirect "abc123xyz"`

The producer never sees the round-tripped ext at the script level
(it's already what it sent on BEGIN), but the engine does mechanically
carry it on the REDIRECT frame so a real binding can reissue BEGIN
with the original ext when the AffinityResolver hook lands in a
follow-on PR.

Tests: 37/37 SimplexIT pass; checkstyle clean.
…or hash

Companion to the previous commit. Updates the actual hash-extraction
and ext-round-trip code paths in ZillaStreamFactory and ZillaTarget so
they read the script-provided hash from `writeExtBuffer(REDIRECT)` and
the BEGIN ext from `readExtBuffer(BEGIN)` (via absolute index reads
since the script may have already consumed the readerIndex with
`read zilla:begin.ext`).

Tests: 37/37 SimplexIT pass; checkstyle clean.
… writeExtBuffer

Companion to the prior two commits. Adds the actual flyweight
encode/decode for `RedirectFW` in the k3po-zilla `ZillaTarget`:

- `doRedirect(channel, ..., hash, beginExt)` builds a `RedirectFW`
  with `affinity` and `authorization` carried through, the script's
  `hash` bytes (read from `writeExtBuffer(REDIRECT)` upstream), and
  the round-tripped `extension` bytes (read from
  `readExtBuffer(BEGIN)` via absolute index 0 to `writerIndex()`,
  surviving any prior `read zilla:begin.ext` on the script).

- `Throttle.handleThrottle` recognizes `RedirectFW.TYPE_ID` and
  invokes a new `onRedirect`, which copies only the frame's
  `hash().value()` into `writeExtBuffer(REDIRECT, false)`,
  unregisters the throttle (REDIRECT closes the stream), and fires
  `ADVISORY_REDIRECT` so the script's `write advised zilla:redirect`
  step matches against the hash bytes alone.

The frame's `extension` is consumed but not surfaced to the script,
since the round-trip is mechanical — the producer originally sent
those bytes on its own BEGIN.

Tests: 37/37 SimplexIT pass; checkstyle clean.
… hash field

The hash result is what the parent binding actually needs to route on,
and it shape-matches `Begin.affinity` which is already an `int64`.
Pre-hashing in the consumer binding eliminates any coupling between
protocol-aware bindings and the hash function, and keeps the parent's
logic to "use this affinity instead of the one on the original BEGIN"
— no resolver call shape change.

Frame becomes:

    struct Redirect extends Frame [0x40000005]
    {
        int64 affinity;     // the new affinity to use
        octets extension;   // round-tripped from BEGIN
    }

The original-BEGIN affinity is dropped — the parent already knows it
because it dispatched the BEGIN.

DSL form for the k3po-zilla transport: 8 bytes big-endian
(most-significant byte first, so it reads naturally as a hex value):

    read advise zilla:redirect [0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xb1]
    write advised zilla:redirect [0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xb1]

This commit lands the IDL change and the script update; the wiring
changes in `ZillaStreamFactory` and `ZillaTarget` follow in a
companion commit.
…8 BE bytes

Companion to the IDL change. Updates the actual encode/decode paths so
the script-provided affinity (8 bytes big-endian) round-trips correctly
through the `int64 affinity` field on the `RedirectFW`.

- `ZillaStreamFactory.doRedirect`: reads 8 bytes from
  `writeExtBuffer(REDIRECT)` via `ByteBuffer.wrap(...).order(BIG_ENDIAN)
  .getLong()` and passes the resulting long to `ZillaTarget.doRedirect`.
  Defaults to 0L when the buffer length is anything other than 8 (i.e.,
  no affinity supplied by the script).

- `ZillaTarget.doRedirect`: signature simplified — takes the long
  affinity directly, drops the previous `ChannelBuffer hash` parameter,
  removes the `RedirectFW.Builder.hash(...)` call, removes the local
  `UnsafeBuffer hashBuffer`. Local var renamed to `redirectEx` to match
  the broader `*Ex` ext-buffer naming convention.

- `Throttle.onRedirect`: packs `RedirectFW.affinity()` into an 8-byte
  array via `ByteBuffer.wrap(...).order(BIG_ENDIAN).putLong(...)` and
  writes it to `writeExtBuffer(REDIRECT, false)` so the script's
  `write advised zilla:redirect` matcher sees those bytes.

Tests: 37/37 SimplexIT pass; checkstyle clean.
…ong/writeLong

Per review: when both ends are long-typed (script literal + flyweight
field), there's no human-readability advantage to forcing big-endian
— round-trip correctness is what matters and native is simpler.

DSL form is now a long literal (parsed via `Long.decode()`):

    read advise zilla:redirect  0x00000000000000b1L
    write advised zilla:redirect 0x00000000000000b1L

Plumbing:
- `ZillaStreamFactory.doRedirect`: reads affinity via
  `redirectEx.getLong(redirectEx.readerIndex())` — no `ByteBuffer.wrap`,
  no `ByteOrder` import.
- `Throttle.onRedirect`: writes via
  `channel.writeExtBuffer(REDIRECT, false).writeLong(affinity)` —
  no intermediate `byte[8]`, no `ByteBuffer.wrap`.

Both ends use the channel buffer's own byte order (NATIVE by default,
NETWORK when `option zilla:byteorder network` is set on the channel),
so the round-trip is consistent regardless of platform.

Tests: 37/37 SimplexIT pass; checkstyle clean.
@jfallows
jfallows marked this pull request as ready for review May 8, 2026 19:19

@jfallows jfallows left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jfallows
jfallows merged commit be0b1bd into develop May 8, 2026
109 of 110 checks passed
jfallows pushed a commit that referenced this pull request May 10, 2026
Capture the engine's REDIRECT frame (type id 0x40000005, introduced
in #1755) in the dump command's pcap output and surface it in the
zilla.lua Wireshark dissector. Like RESET, REDIRECT terminates the
stream, so it is mapped to a TCP PSH segment. Like BEGIN, the frame
carries an int64 affinity and a round-tripped extension, so each
binding's extension dissector treats REDIRECT the same as BEGIN.

Adds k3po scripts for the client-sent redirect scenario; the IT method
is wired but @ignore'd until its tshark-generated expected fixture is
regenerated on a docker-enabled host.

https://claude.ai/code/session_01K4wd5z8Ejgq4ejTsmL88oh
jfallows pushed a commit that referenced this pull request May 10, 2026
Per the engine ABI from #1755, REDIRECT terminates the stream like RESET
(EngineWorker dispatcher.remove + Target streams[].remove for both
RedirectFW.TYPE_ID and ResetFW.TYPE_ID). The k3po-zilla test transport
was leaving the channel in an inconsistent state on both ends, so
follow-up cleanup or script steps could emit END / ABORT frames against
a stream the engine had already torn down. This produced inconsistent
zilla dump pcap output for redirect scenarios.

Mirror the existing RESET pattern exactly:

- `ZillaSource.doAdviseInput` (ADVISORY_REDIRECT branch) — the caller of
  `streamFactory.doRedirect` now closes the local READ side after the
  frame is written, mirroring `doAbortInputAfterBegin` for RESET. The
  frame writer (`ZillaTarget.doRedirect`) stays state-free, mirroring
  `ZillaTarget.doReset` / `doAbortInput`.
- `Throttle.onRedirect` — on the receiver side, mirror `Throttle.onReset`
  exactly: `setWriteAborted` + `setWriteClosed` + `fireOutputAborted` +
  the disconnect / unbind / close events. The receiver is the source of
  the stream (REDIRECT travels in the throttle direction, opposite the
  BEGIN), so its WRITE side is what the redirect terminates — same as
  RESET semantics.

The pre-existing `binding-http` `RedirectIT` (`read advise zilla:redirect`
+ `write aborted` on the application side, added in #1758) was authored
against the previous buggy transport behavior and has been failing on
develop ever since. With this fix in place it will need to be revisited
in a follow-up — that test is unaffected by this commit and remains in
the same failure state as on develop's HEAD.

Verified: engine surefire 320/320 + failsafe 204/204
(incl. SimplexIT 37/37 with the PR #1755 REDIRECT test).

https://claude.ai/code/session_01K4wd5z8Ejgq4ejTsmL88oh
jfallows added a commit that referenced this pull request May 10, 2026
* feat(command-dump): add REDIRECT control frame support

Capture the engine's REDIRECT frame (type id 0x40000005, introduced
in #1755) in the dump command's pcap output and surface it in the
zilla.lua Wireshark dissector. Like RESET, REDIRECT terminates the
stream, so it is mapped to a TCP PSH segment. Like BEGIN, the frame
carries an int64 affinity and a round-tripped extension, so each
binding's extension dissector treats REDIRECT the same as BEGIN.

Adds k3po scripts for the client-sent redirect scenario; the IT method
is wired but @ignore'd until its tshark-generated expected fixture is
regenerated on a docker-enabled host.

https://claude.ai/code/session_01K4wd5z8Ejgq4ejTsmL88oh

* fix(engine): align REDIRECT teardown with RESET in k3po-zilla transport

Per the engine ABI from #1755, REDIRECT terminates the stream like RESET
(EngineWorker dispatcher.remove + Target streams[].remove for both
RedirectFW.TYPE_ID and ResetFW.TYPE_ID). The k3po-zilla test transport
was leaving the channel in an inconsistent state on both ends, so
follow-up cleanup or script steps could emit END / ABORT frames against
a stream the engine had already torn down. This produced inconsistent
zilla dump pcap output for redirect scenarios.

Mirror the existing RESET pattern exactly:

- `ZillaSource.doAdviseInput` (ADVISORY_REDIRECT branch) — the caller of
  `streamFactory.doRedirect` now closes the local READ side after the
  frame is written, mirroring `doAbortInputAfterBegin` for RESET. The
  frame writer (`ZillaTarget.doRedirect`) stays state-free, mirroring
  `ZillaTarget.doReset` / `doAbortInput`.
- `Throttle.onRedirect` — on the receiver side, mirror `Throttle.onReset`
  exactly: `setWriteAborted` + `setWriteClosed` + `fireOutputAborted` +
  the disconnect / unbind / close events. The receiver is the source of
  the stream (REDIRECT travels in the throttle direction, opposite the
  BEGIN), so its WRITE side is what the redirect terminates — same as
  RESET semantics.

The pre-existing `binding-http` `RedirectIT` (`read advise zilla:redirect`
+ `write aborted` on the application side, added in #1758) was authored
against the previous buggy transport behavior and has been failing on
develop ever since. With this fix in place it will need to be revisited
in a follow-up — that test is unaffected by this commit and remains in
the same failure state as on develop's HEAD.

Verified: engine surefire 320/320 + failsafe 204/204
(incl. SimplexIT 37/37 with the PR #1755 REDIRECT test).

https://claude.ai/code/session_01K4wd5z8Ejgq4ejTsmL88oh

* test(command-dump): enable shouldReceiveClientSentRedirect

Pair the redirect rpt scripts with `read abort` (client) and
`write aborted` (server) so post-REDIRECT teardown is deterministic
once the k3po-zilla transport aligns REDIRECT with RESET (#13ad202a1).
Add the tshark fixture and unignore the IT method.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
@jfallows
jfallows deleted the feature/1753-redirect-frame branch July 1, 2026 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add REDIRECT control frame to defer affinity routing to sender

1 participant