Repository navigation
feat(engine): add pluggable per-field ModelTransform hook to the model pipeline - #2326
Merged
Merged
Conversation
…l pipeline Replaces the purely observational ModelVisitor with ModelTransform, a format-agnostic per-field stage that can observe, substitute, or decline each field of a value, on decode and on encode. A transform never sees a format's wire representation: a thin adapter inside each model derives the field's path and value bytes on the way in and turns the answer back into a valid format-specific event on the way out. The answer is the event the stage forwards — FIELD keeps the value, REPLACED substitutes it, DECLINED drops it so the adapter writes a structurally valid placeholder for that field's type, the only case that needs format-specific work. engine: - ModelTransform (plus NONE), FieldEvent, FieldStatus, ModelSource, ModelSink and ModelController, shaped after the pull-based, resumable contract each format's own *Transform already uses, so resumability composes with no extra translation and an unchanged field forwards by reference - CompositeModelTransform chains an ordered list of stages entirely in the generic domain, so an adapter pays exactly one format-to-generic context switch per field however many policies the transform represents - ModelFieldBridge drives a transform from the field values a model surfaces once a value completes, for a model with no native adapter yet (observation-only) - ModelHandler.supplyDecoder / supplyEncoder now take a ModelTransform; ModelVisitor is removed model-avro: - AvroModelTransform replaces AvroExtractor and is wired into both the decode and the encode pipeline; the encode-side attachment point is new - observing mode (transform.identity()) forwards every event verbatim as it arrives, keeping the extraction fast path byte-identical; mediating mode withholds a top-level scalar field's events until its value is whole and then re-emits the answer, streaming and resuming across bounded output model-json, model-protobuf, model-core: - adapted to the new SPI through ModelFieldBridge; native adapters follow binding-kafka: - KafkaExtractor re-platformed onto ModelTransform, decode-only and behavior preserving; extracted() now takes a ValueConsumer - a value rejected part-way discards its partial captures, so a field surfaced inline before the rejection cannot reach the cache entry Closes #2325 — json/protobuf adapter parity remains follow-up work. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mrsus1hsPV6BM4AJESLeLH
… END_MESSAGE The adapter delivered flush and END_VALUE on AvroEvent.END_MESSAGE, which the pump never pulls: it stops the moment the terminal sink closes the top-level record and returns COMPLETED, so the parser's END_MESSAGE is left unread and that branch was unreachable. A transform wired through model-avro therefore saw its field run opened and never closed, disagreeing with ModelFieldBridge, which does deliver both. Close off COMPLETED instead — the one signal every datum shape reaches, records, bare scalars and segment runs alike — from the resume path as well, since a resumed emission can be what completes the datum. A close that merely advances leaves the datum's COMPLETED intact for the caller. Both modes now deliver START_VALUE, one FIELD per top-level field, then flush and END_VALUE; a test per mode pins the sequence. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mrsus1hsPV6BM4AJESLeLH
…s into ModelStatus FieldEvent was the one type in the format-agnostic domain not carrying the Model prefix every other one does (ModelTransform, ModelSource, ModelSink, ModelController), and its avro analog is AvroEvent. Renamed. FieldStatus existed only because ModelStatus was already taken by ModelPipeline#transform. That was the wrong call: common-avro already shares one AvroPipeline.Status across its pipeline, its sinks and its transform stages, and that enum likewise carries STARVED, which no transform stage ever returns. The same shape applies here, so the two collapse into ModelStatus — ADVANCED becomes OK, SUSPENDED becomes OVERFLOW — with UNDERFLOW documented as the one value a transform chain never reports, since a field always arrives whole. Sharing the enum also removes a workaround. AvroModelTransform threaded an `emitted` field through deliver, resume, Terminal.emit/decline/resume and reset purely to carry the terminal sink's COMPLETED out of band, which FieldStatus had no way to express. ModelStatus has COMPLETE, so map/unmap round-trip it and the side channel is gone. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mrsus1hsPV6BM4AJESLeLH
…pping CompositeModelTransform
Chaining is now a default method on the interface, matching the Function.andThen
idiom, so any number of stages compose into the single ModelTransform a
ModelHandler accepts:
first.andThen(second).andThen(third)
transforms.stream().reduce(ModelTransform::andThen)
The composed transform holds the terminal for the current call and hands the
first stage a bridge sink that invokes the second with it, so the chain re-binds
per call and neither stage retains the caller's sink. reset fans out to both;
identity is the conjunction.
This removes a public class along with its parallel transform/stage arrays, its
index-walking Stage, and the null/empty/single special-casing in its factory.
The property the composition exists for is unchanged: it stays entirely in the
format-agnostic domain, so an adapter is still handed exactly one transform and
still pays one format-to-generic context switch per field.
NONE is not special-cased, so NONE.andThen(x) composes rather than yielding x;
reducing without an identity seed avoids the wrapper and keeps andThen free of
branches.
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mrsus1hsPV6BM4AJESLeLH
jfallows
commented
Aug 7, 2026
…E instead of null checks Adds AvroTransform.NONE and has AvroStream drop it at assembly rather than bind it, so a caller with no stage to insert passes it unconditionally and the assembled pipeline carries no stage at all. Binding it would have cost a BoundSink frame per event on every Avro stream, so dropping is what makes the unconditional call free. AvroModelTransform.of returns AvroTransform.NONE for ModelTransform.NONE and is now the only way to construct the adapter. Both model pipelines therefore hold a non-null AvroTransform, which removes the null checks, the duplicated decode newPipeline overload that existed purely to omit the stage, and the conditional transform in the encode variant. supplyDecoder and supplyEncoder requireNonNull their transform. The guard is added where the transform is consumed — avro both directions, json and protobuf decode — and not where it is ignored outright, which would guard an unused parameter; ModelHandler now states the contract once instead: never null, pass NONE, and an implementation may wire it away. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mrsus1hsPV6BM4AJESLeLH
…ecoder The guard added in 381d940 referenced requireNonNull without its static import, breaking compilation of both modules. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mrsus1hsPV6BM4AJESLeLH
…e other stage NONE.andThen(transform) now returns transform, and andThen returns the receiver when handed NONE, so composing with the identity sentinel yields the other stage rather than a wrapper and reducing a list from NONE costs nothing. The short-circuit keys on NONE itself, never on identity(). An identity stage still runs and may be accumulating for its owner while it forwards — KafkaExtractor reports identity() yet copies the configured paths aside for KafkaCachePartition to replay — so collapsing one away by that property would silently drop its work. A test pins that a non-NONE identity stage still composes and still sees its fields. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mrsus1hsPV6BM4AJESLeLH
Composing with NONE collapses only where NONE is the receiver, through the override on the NONE instance itself. The reverse case in the default method was unasked scope on a marginal path: the override already makes reducing a list from NONE free, and a caller conditionally appending NONE is not a shape any consumer has. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mrsus1hsPV6BM4AJESLeLH
Composing with NONE now yields the other stage from either side: the default returns this for a NONE argument, and the NONE instance overrides andThen to return next. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mrsus1hsPV6BM4AJESLeLH
This was referenced Aug 7, 2026
This was referenced Aug 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Replaces the purely observational
ModelVisitorwithModelTransform, a format-agnostic per-field stage that can observe, substitute, or decline each field of a value, on decode and on encode.A transform never sees a format's wire representation: a thin adapter inside each model derives the field's path and value bytes on the way in and turns the answer back into a valid format-specific event on the way out. The answer is the event the stage forwards —
FIELDkeeps the value,REPLACEDsubstitutes it,DECLINEDdrops it so the adapter writes a structurally valid placeholder for that field's type, the only case that needs format-specific work.engine
ModelTransform(plusNONE),ModelEvent,ModelSource,ModelSink,ModelController— shaped after the pull-based, resumable contract each format's own*Transformalready uses, so resumability composes with no extra translation and an unchanged field forwards by reference rather than by a mandatory copy.ModelStatusis shared by the transform chain rather than a second enum being introduced, mirroringcommon-avro, where oneAvroPipeline.Statusserves the pipeline, its sinks and its transform stages.UNDERFLOWis documented as the one value a transform chain never reports, since a field always arrives whole.ModelTransform.andThencomposes any number of stages into the single transform aModelHandleraccepts —first.andThen(second).andThen(third), ortransforms.stream().reduce(ModelTransform::andThen). Composition stays entirely in the format-agnostic domain, so an adapter is handed exactly one transform and pays one format-to-generic context switch per field however many policies it represents.ModelFieldBridgedrives a transform from the field values a model surfaces once a value completes, for a model with no native adapter yet (observation-only).ModelHandler.supplyDecoder/supplyEncodernow take aModelTransform;ModelVisitoris removed.model-avro
AvroModelTransformreplacesAvroExtractorand is wired into both the decode and the encode pipeline. The encode-side attachment point is new —AvroModelEncoderPipelinehad no field-level touchpoint before.transform.identity():model-json, model-protobuf, model-core
Adapted to the new SPI through
ModelFieldBridge; native adapters are follow-up work, as scoped in the issue.binding-kafka
KafkaExtractorre-platformed ontoModelTransform, decode-only and behavior preserving;extracted()now takes aValueConsumer.Deviations from the issue, worth a look
ModelEventcarries the answer. Unchanged / replacement / declined are expressed as the event the stage forwards, rather than a status code or a sentinel source. Composed stages pass them through unchanged and the avro adapter's three branches map 1:1.AvroModelDecoderTransformandAvroModelEncoderTransform; both would be identicalAvroTransformimplementations over the same event stream, so this is a singleAvroModelTransformused at both insertion points. Easy to split if the two names are preferred.andTheninstead of aCompositeModelTransformclass. The issue describes a composite type composing an ordered list; a default method on the interface gives the same property with no public class, no parallel stage arrays, and no null/empty/single special-casing in a factory.ModelEventdid not exist. The issue says "reuse the existingFieldEventtype", but there is no such type in the tree — today'sModelVisitor.onFieldtakespath, buffer, index, length— so it is defined here along with the rest of the generic domain.Behaviors preserved deliberately
Neither is obvious from the diff:
reset()does not reset the wired transform. Doing so would clearKafkaExtractor's captures beforeKafkaCachePartitionreplays them into the cache entry's key and trailing headers.Testing
./mvnw clean verifygreen onengine,binding-kafka,model-core,model-json,model-protobuf,model-avro— including the 394 Kafka ITs, whereCacheFetchIT'sextractKey/extractHeaderscases are the regression oracle for the re-platform, plus checkstyle, license and coverage gates.New coverage:
ModelTransformTest—NONE, chaining order two and three deep, composition by reduction, decline propagation, identity composition, reset and flush fan-out, bridge delivery.AvroModelTransformTest— replace and decline across string, scalar, bytes, fixed, boolean, float, double and union-branch fields; byte-for-byte reproduction when substituting identical values; resume across bounded output; substitute streamed across a bounded destination; reject with diagnostic; identity reporting in both modes; and the field-run framing sequence in both modes.Commit
b09a4b284fixes a defect found while tracing the flow:flushandEND_VALUEwere driven offAvroEvent.END_MESSAGE, which the pump never pulls — it stops the moment the terminal sink closes the top-level record and returnsCOMPLETED. That left a transform's field run opened and never closed, disagreeing withModelFieldBridge. The run now closes offCOMPLETED, the one signal every datum shape reaches, with a test per mode pinning the sequence.Fixes #2325 — json/protobuf adapter parity remains follow-up work, as scoped in the issue.