Repository navigation
Description
Problem
The model pipeline's only per-field hook today is ModelVisitor.onField(FieldEvent), and it is purely observational: it fires once per field, only after the whole value has reached ModelStatus.COMPLETE, returns void, and has no attachment point on encode at all — AvroModelEncoderPipeline doesn't carry a ModelVisitor parameter through anywhere.
This already limits an existing, shipped feature. binding-kafka's KafkaTopicTransformsType (extractKey / extractHeaders) promotes payload field values into the Kafka message key or headers, so downstream bindings can filter/route on them without decoding the full payload. It's implemented via KafkaExtractor implements ModelVisitor, wired through KafkaCacheModel.decoder(handler, extractPaths, scratch) — and it only ever works on decode. KafkaCacheModel.encoder() never attaches a visitor, because there's no hook to attach on the encode side even if a future use case needed one there.
Meanwhile, common-avro/common-json/common-protobuf already define a per-event stage contract for exactly this kind of thing — AvroTransform / JsonTransform / ProtobufTransform, composable left-to-right in each format's own pipeline, with javadoc explicitly anticipating "forwarding, dropping, or substituting events" and third-party implementations. No mutating implementation of it exists yet; the only implementor, AvroExtractor, is deliberately transparent/read-only.
Proposal
Add a new engine SPI type, ModelTransform, as a real per-field mutation point that composes into each format's own pipeline (not a second top-level pipeline concept), plus a thin adapter per format that lets a single, format-agnostic ModelTransform implementation plug into avro/json/protobuf without knowing anything about their wire representation.
Shape — pull-based, mirroring AvroTransform, not the buffer-loop shape of ModelPipeline:
public interface ModelTransform
{
ModelTransform NONE = ...; // identity passthrough
Status transform(
ModelController control,
ModelSource source,
FieldEvent event, // reuse the existing FieldEvent type
ModelSink sink);
default Status resume(...) { return sink.resume(control, source, event); }
default Status flush(...) { return sink.flush(control, source); }
default void reset() { }
default boolean identity() { return false; }
}Reusing ModelPipeline's existing buffer-in/buffer-out contract for this was considered and rejected — it's a real paradigm mismatch against the pull-based, resumable (control, source, event, sink) shape every format-level *Transform already uses, and bridging the two would mean inventing a second resumability protocol at every adapter boundary. Matching the existing shape means resumability composes uniformly with no extra translation, and an unchanged field can be forwarded by reference instead of a mandatory buffer copy.
Format adapters — thin, always one-to-one:
AvroModelDecoderTransform / AvroModelEncoderTransform (implements AvroTransform), and equivalents for json/protobuf, do pure presentation: derive the field's path/bytes from the format-specific event on the way in, and turn the generic transform's answer (unchanged / replacement / declined) back into a valid format-specific event on the way out. Declining is the only case with real format-specific work — synthesizing a structurally valid placeholder for that field's type — since only the format side knows what "valid" means there; the generic transform never needs to know.
Insertion point mirrors where AvroExtractor already sits on decode today: [format parse] → [ModelTransform adapter] → [format-specific final stage] on decode, [format-specific first stage] → [ModelTransform adapter] → [format generate] on encode. The encode-side insertion point is new — AvroModelEncoderPipeline has no field-level touchpoint today, so this isn't a rewire of something existing there.
Composing more than one transform: rather than one adapter per transform (which pays a format↔generic context-switch cost per transform per field, redundant for N>1), a CompositeModelTransform composes an ordered list of transforms entirely in the generic domain — transform K's output feeds transform K+1's input. The format adapter is always handed exactly one ModelTransform — a leaf or a composite — and always does exactly one context switch regardless of how many policies it represents.
Side-output / accumulation: no new shared channel needed. A transform that needs to accumulate something for its owner to query afterward can do so the same way KafkaExtractor already does today (implements the generic interface for pipeline-plumbing uniformity, plus its own extra methods that only its concrete owner calls) — fully outside the shared contract.
Migration plan
- Add the
ModelTransformSPI plus the avro adapter pair, with no consumer changes yet. - Re-platform
KafkaExtractorontoModelTransform, decode-only, behavior-preserving — existing k3po IT coverage forextractKey/extractHeadersbecomes the regression oracle. This is deliberately the first consumer: smallest, best-isolated, already tested, no encode side needed. - Simplify
KafkaCachePartition— the current transform-then-separately-check-extracted code collapses into onetransform()call plus the existing post-hoc replay, unchanged in behavior. - JSON/protobuf adapter parity as follow-up work once the avro path is proven.
Scope
In scope: runtime/engine/.../model/ (new SPI), runtime/model-avro (reference adapter implementation), runtime/binding-kafka (re-platform KafkaExtractor, decode-only, no behavior change).
Out of scope for this issue: json/protobuf adapter parity (follow-up), and any consumer that needs genuine encode-side field mutation — that lands as its own issue once this mechanism exists and is proven, from whichever component needs it.
Activity
feat(engine): add pluggable per-field ModelTransform hook to the mode…