Skip to content

feat(binding-kafka): wire ModelEnvelope into kafka cache storage's read and write paths - #2455

Merged
jfallows merged 12 commits into
developfrom
claude/zilla-plus-pii-status-nfzbgr
Aug 30, 2026
Merged

jfallows merged 12 commits into
developfrom
claude/zilla-plus-pii-status-nfzbgr

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

ModelEnvelope (#2384) already lets a ModelHandler's composed transform read and write named, out-of-value metadata during encode/decode, but every call site in the kafka cache storage layer (KafkaCacheModel) passed ModelEnvelope.NONE -- no transform ever got a real envelope, so there was nowhere for a transform to read metadata present on produce, or write metadata a consumer needs on fetch.

This wires a real envelope into both directions of the kafka cache storage layer:

Fetch path -- KafkaCacheClientFetchFactory now supplies a new KafkaCacheHeadersEnvelope, backed by the fetched record's real headers() and trailers() (concatenated in the same order the record's own wire headers already are, matching what the consumer sees). count/get scan by name; set() has nowhere to go on this path and discards, same as ModelEnvelope.NONE.

Produce path -- KafkaCacheClientProduceFactory now supplies a new KafkaCacheTrailerEnvelope, which accumulates a composed transform's set() calls in call order during encode (repeated names simply add further entries, so they materialize as that many repeated Kafka headers, in order). Once encode completes, its collected entries are merged with any wire-supplied late headers into the record's trailers.

KafkaCacheModel.encoder(...)/.reader(...) widen to take the envelope; decoder/writer (the NONE/WRITE cache contexts) are unchanged -- the populate-time decode path has no per-consumer envelope to supply, and is out of this issue's scope.

To make the wiring observable without a production model dependency, the engine's shared TestModel test double gains an additive envelopeDisclose config option, mirroring the existing discloseAuthorized mechanism: a value discloses only when the supplied envelope actually holds a value under the configured name, which is only possible if the envelope it was given is real rather than ModelEnvelope.NONE.

Testing

  • New unit tests for both envelope classes (KafkaCacheHeadersEnvelopeTest, KafkaCacheTrailerEnvelopeTest), written and confirmed failing (missing class) before implementation.
  • New unit tests on KafkaCacheModel confirming the supplied envelope is forwarded to supplyEncoder/supplyDecoder unchanged.
  • Three new k3po scenarios, each with a runtime CacheFetchIT/CacheProduceIT test and config, proving the wiring end-to-end through a live engine:
    • fetch, header present under the disclosed name -> real value disclosed
    • fetch, header present under a different name -> value redacted (envelope correctly reports zero for the absent name)
    • produce, no wire header written by the producer at all -> a real Kafka header appears on the record, observed by the downstream broker relay, sourced entirely from what the transform wrote to the envelope during encode
  • Full runtime/binding-kafka module (clean verify): all unit tests, all k3po ITs, checkstyle, license, and jacoco coverage pass with no regressions. runtime/engine, config/engine.conf, specs/engine.spec, and specs/binding-kafka.spec also verified clean.

Fixes #1689


Generated by Claude Code

claude added 12 commits August 25, 2026 18:16
…ch path

KafkaCacheServerFetchFactory decoded a fetched Kafka record's value once,
while populating the local cache from the broker, with authorization
hardcoded to NO_AUTHORIZATION -- there is no requesting consumer yet at
that point. The decoded result was stored in the partition's convertedFile
and served to every future reader verbatim, including the per-consumer
fetch stream (KafkaCacheClientFetchFactory), which constructed no model
pipeline of its own at all and never threaded its own live authorization
into anything.

ModelHandler gains a third pipeline-vending method, supplyCacheable,
alongside supplyDecoder/supplyEncoder -- deliberately abstract, not
defaulted, so every implementor must explicitly decide its behavior rather
than silently inheriting a delegate that could leak a consumer-specific
value into the shared cache. Every existing implementor (model-core,
model-json, model-protobuf, model-avro, engine's TestModel, and the
anonymous test doubles in binding-kafka's own unit tests) now implements
it explicitly; all but TestModel simply delegate to supplyDecoder, so
today's behavior is unchanged byte-for-byte everywhere except the new test
coverage.

KafkaCacheServerFetchFactory's populate-time pipeline now uses
supplyCacheable instead of supplyDecoder. KafkaCacheClientFetchFactory
gains a genuine per-stream decode: it resolves the topic's value model at
stream construction (mirroring KafkaCacheClientProduceStream's existing
per-stream pattern), and when that model's decoder isn't a byte-for-byte
identity, decodes each cached entry's value against the requesting
stream's own live authorization before forwarding it -- exactly once per
message, so a consumer whose authorization changes mid-stream sees the
change reflected starting with the next fetched message. A model with
nothing consumer-specific configured never triggers the extra decode at
all (identity() stays true), so there is no behavior change or added cost
for the common case.

Verified test-first: CacheFetchIT#shouldReceiveMessageValueAuthorizationDistinct
fails cleanly (both consumers see the same undisclosed value) before the
fix and passes after. Extended the engine's TestModel with
discloseAuthorized/discloseRedacted config to simulate consumer-specific
disclosure without depending on any production model module. Full
runtime/binding-kafka suite (583 tests), specs/binding-kafka.spec peer-to-peer
suite (667 tests), and runtime/engine + config/engine.conf + all model
module unit tests (509 tests) all pass with no regressions; checkstyle and
license checks clean.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CzxN4tJLhiHQ8ikNeuN8WS
ModelHandler.supplyCacheable already vends a separate pipeline from
supplyDecoder, but every *ModelExtHandler's own extension-stage fold
(model-avro, model-protobuf, model-json, model-core) still built that
pipeline by unconditionally invoking each installed extension's decode()
stage — so an extension had no way to behave differently between a
pipeline populating shared storage ahead of any specific reader's
request and one resolving the view for the reader making a request
right now.

Add a cacheable() method to each *ModelExtHandler interface, defaulting
to decode() for full backward compatibility, and thread a cacheable
flag through each handler's pipeline construction so supplyCacheable
folds installed extensions via cacheable() while supplyDecoder
continues to fold via decode().
…Decoder

PR #2431 (edb34fd, 011d0bf) added a two-way supplyCacheable()/
supplyDecoder() split to give the Kafka local cache per-consumer
authorization decode without re-fetching from Kafka. Both methods used
the same frontend parser and output format, differing only in which
ext-handler fold ran -- but any model whose decode does real,
format-converting work (view: json, disclosed/encrypted fields, ...)
produces output that differs from its wire-format input, so caching
that output and later re-decoding it with the wire-format frontend is
never correct. This surfaced in CI as a permanent hang on the
http.kafka.avro.json example: the avro model's `view: json` config
made the populate-time decode cache JSON text, and the new
per-consumer fetch-time decode then fed that JSON into a parser
expecting avro wire bytes.

Replace the two-method split with a single supplyDecoder(envelope,
transform, ModelCache) parameterized by a three-way cache context:
NONE (today's plain decode, no cache), WRITE (decode ahead of any
reader, producing the value a cache persists), and READ (decode a
value already in whatever form WRITE produced, for the reader
requesting it now). WRITE and NONE share the same wire-format
frontend; READ's frontend matches whatever WRITE actually emitted --
for avro/protobuf's `view: json`, that's AvroJson.stream/
ProtobufJson.parser (the same json-view frontend already used on the
encode-shaped pipeline), reused rather than duplicated.

Mirror the same three-way split into each model's own *ModelExtHandler
SPI as a single decode(T, <Model>Cache) method -- a local enum per
model (AvroCache/ProtobufCache/JsonCache/CoreCache), not the shared
engine ModelCache, so each model's extension surface stays independent
-- replacing the previous decode(T)/cacheable(T) pair.

Also close a related gap found while building this: protobuf's own
message-index framing (separate from catalog framing) has no
magic-byte-style validity guard, so a cached view:json value with no
index framing at all would misparse its leading JSON bytes as an index
varint on READ. Resolve the message via the same static catalog.record
path the encode side already uses whenever cache == READ.

Persist the schema id WRITE resolves as catalog framing ahead of its
cached output (reusing the same CatalogHandler.encode(...) machinery
the encode-direction pipeline already uses), so READ recovers it
directly from the cached bytes via the existing resolveSchemaId/prefix
logic, with no changes needed on the read side. This closes the one
case that otherwise stays broken even with the frontend fix: a
strategy: encoded schema id is per-message authoritative (schema
evolution means two cached messages on the same topic can carry
different ids), and a view-converting WRITE output previously
discarded it entirely with no static fallback available -- unlike
strategy: topic/subject-pinned configs, whose schema was never
message-embedded to begin with.

Add a CacheFetchIT scenario driving a real avro + view: json config
through a live engine populate-then-fetch round trip -- the exact
shape of the original regression -- since TestModel's identity decode
cannot exercise this failure mode.

Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_01CzxN4tJLhiHQ8ikNeuN8WS
…pii-status-nfzbgr

# Conflicts:
#	runtime/binding-mcp/src/main/java/io/aklivity/zilla/runtime/binding/mcp/internal/config/McpBindingConfig.java
…ad and write paths

Fetch path: KafkaCacheClientFetchFactory now supplies a
KafkaCacheHeadersEnvelope backed by the fetched record's real headers()
and trailers() (concatenated in the same order the record's own wire
headers already are), so a composed transform can read metadata that
was present on produce.

Produce path: KafkaCacheClientProduceFactory now supplies a
KafkaCacheTrailerEnvelope that collects a composed transform's
envelope.set() calls in order during encode, and materializes them as
real Kafka headers -- merged with any wire-supplied late headers --
once encode completes.

KafkaCacheModel.encoder/reader widen to accept the envelope; decoder/
writer are unchanged (out of scope -- populate-time decode has no
per-consumer envelope to supply).

Extends the engine's TestModel test-jar with an envelopeDisclose
config option (additive, mirrors the existing discloseAuthorized
mechanism) so k3po coverage can observe the wiring without a
production model dependency: a value is disclosed only when the
supplied envelope actually holds a value under the configured name,
proving the envelope is real rather than ModelEnvelope.NONE.
…pii-status-nfzbgr

# Conflicts:
#	config/engine.conf/src/test/java/io/aklivity/zilla/config/engine/test/internal/model/config/TestModelConfig.java
#	config/engine.conf/src/test/java/io/aklivity/zilla/config/engine/test/internal/model/config/TestModelConfigAdapter.java
#	config/engine.conf/src/test/java/io/aklivity/zilla/config/engine/test/internal/model/config/TestModelConfigBuilder.java
#	runtime/binding-kafka/src/main/java/io/aklivity/zilla/runtime/binding/kafka/internal/cache/KafkaCacheModel.java
#	runtime/binding-kafka/src/main/java/io/aklivity/zilla/runtime/binding/kafka/internal/stream/KafkaCacheClientFetchFactory.java
#	runtime/binding-kafka/src/test/java/io/aklivity/zilla/runtime/binding/kafka/internal/stream/CacheFetchIT.java
#	runtime/engine/src/test/java/io/aklivity/zilla/runtime/engine/test/internal/model/TestModelHandler.java
#	runtime/engine/src/test/java/io/aklivity/zilla/runtime/engine/test/internal/model/TestModelPipeline.java
#	specs/engine.spec/src/main/scripts/io/aklivity/zilla/specs/engine/schema/model/test.schema.patch.json
@jfallows
jfallows merged commit 94ef90e into develop Aug 30, 2026
42 checks passed
jfallows pushed a commit that referenced this pull request Aug 30, 2026
…ked extraction

KafkaPipeline provided a bespoke whole-message lane vocabulary (SWITCH_KEY/
SWITCH_VALUE/SWITCH_HEADERS/FIELD) so extract-key/extract-headers topic
transforms could route a matched field to a different destination. This
predates ModelEnvelope (#1689/#2455), which already lets a composed
ModelTransform read/write named, out-of-value metadata directly and is
already used by the produce and client-fetch paths without any lane
vocabulary at all.

Extends the same envelope mechanism to the key's own model via a reserved
":key" pseudo-name (HTTP/2-pseudo-header style), letting extract-key resolve
the same way extract-headers already does through KafkaCacheHeadersEnvelope/
KafkaCacheTrailerEnvelope. KafkaExtractTransform is rewritten as a plain
ModelTransform that calls envelope.set() on a path match; KafkaCacheServerFetchFactory
composes it directly per extract-key/extract-headers config entry, replacing
KafkaPipeline's lane-switch dispatch and KafkaCachePartition's KafkaEntrySink
single-slot key buffering and heap-resident trailers builder.

KafkaCachePartition's populate-path writeEntryStart/writeEntryFinish now take
KafkaCacheModel transformKey/transformValue directly (matching the produce
path's existing shape) instead of a KafkaPipeline, and reuse a claimed block
in the log file for header collection during the value drive -- mirroring
the produce path's claim-per-message design -- rather than a heap-resident
builder reused across every fetched message on a partition. A new
cache.server.trailers.size.max config bound sizes this claim, gated on
whether the topic actually configures a value model.

KafkaPipeline, KafkaEvent, KafkaSink, KafkaSource, KafkaController, and
KafkaTransform are removed entirely along with their tests and benchmarks,
which exercised the deleted lane-switch mechanism directly.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CzxN4tJLhiHQ8ikNeuN8WS
jfallows added a commit that referenced this pull request Aug 30, 2026
…ked extraction (#2458)

KafkaPipeline provided a bespoke whole-message lane vocabulary (SWITCH_KEY/
SWITCH_VALUE/SWITCH_HEADERS/FIELD) so extract-key/extract-headers topic
transforms could route a matched field to a different destination. This
predates ModelEnvelope (#1689/#2455), which already lets a composed
ModelTransform read/write named, out-of-value metadata directly and is
already used by the produce and client-fetch paths without any lane
vocabulary at all.

Extends the same envelope mechanism to the key's own model via a reserved
":key" pseudo-name (HTTP/2-pseudo-header style), letting extract-key resolve
the same way extract-headers already does through KafkaCacheHeadersEnvelope/
KafkaCacheTrailerEnvelope. KafkaExtractTransform is rewritten as a plain
ModelTransform that calls envelope.set() on a path match; KafkaCacheServerFetchFactory
composes it directly per extract-key/extract-headers config entry, replacing
KafkaPipeline's lane-switch dispatch and KafkaCachePartition's KafkaEntrySink
single-slot key buffering and heap-resident trailers builder.

KafkaCachePartition's populate-path writeEntryStart/writeEntryFinish now take
KafkaCacheModel transformKey/transformValue directly (matching the produce
path's existing shape) instead of a KafkaPipeline, and reuse a claimed block
in the log file for header collection during the value drive -- mirroring
the produce path's claim-per-message design -- rather than a heap-resident
builder reused across every fetched message on a partition. A new
cache.server.trailers.size.max config bound sizes this claim, gated on
whether the topic actually configures a value model.

KafkaPipeline, KafkaEvent, KafkaSink, KafkaSource, KafkaController, and
KafkaTransform are removed entirely along with their tests and benchmarks,
which exercised the deleted lane-switch mechanism directly.


Claude-Session: https://claude.ai/code/session_01CzxN4tJLhiHQ8ikNeuN8WS

Co-authored-by: Claude <[email protected]>
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.

Wire ModelEnvelope into kafka cache storage's read and write paths

2 participants