Skip to content

feat(engine): add pluggable per-field ModelTransform hook to the model pipeline - #2326

Merged
jfallows merged 10 commits into
developfrom
claude/github-issue-2325-sr722q
Aug 7, 2026
Merged

jfallows merged 10 commits into
developfrom
claude/github-issue-2325-sr722q

Conversation

@jfallows

@jfallows jfallows commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Description

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), ModelEvent, ModelSource, ModelSink, 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 rather than by a mandatory copy.
  • The existing ModelStatus is shared by the transform chain rather than a second enum being introduced, mirroring common-avro, where one AvroPipeline.Status serves the pipeline, its sinks and its transform stages. UNDERFLOW is documented as the one value a transform chain never reports, since a field always arrives whole.
  • ModelTransform.andThen composes any number of stages into the single transform a ModelHandler accepts — first.andThen(second).andThen(third), or transforms.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.
  • 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 — AvroModelEncoderPipeline had no field-level touchpoint before.
  • Two modes, selected by transform.identity():
    • observing forwards every event verbatim as it arrives, keeping the extraction fast path byte-identical;
    • mediating withholds a top-level scalar field's events until its value is whole, 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 are follow-up work, as scoped in the issue.

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.

Deviations from the issue, worth a look

  1. ModelEvent carries 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.
  2. One adapter class, not two. The issue names AvroModelDecoderTransform and AvroModelEncoderTransform; both would be identical AvroTransform implementations over the same event stream, so this is a single AvroModelTransform used at both insertion points. Easy to split if the two names are preferred.
  3. andThen instead of a CompositeModelTransform class. 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.
  4. ModelEvent did not exist. The issue says "reuse the existing FieldEvent type", but there is no such type in the tree — today's ModelVisitor.onField takes path, 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:

  • The adapter's reset() does not reset the wired transform. Doing so would clear KafkaExtractor's captures before KafkaCachePartition replays them into the cache entry's key and trailing headers.
  • A value rejected part-way now discards its partial captures. Avro delivers fields inline as they complete, so without this the old "nothing captured on reject" behavior would have changed.

Testing

./mvnw clean verify green on engine, binding-kafka, model-core, model-json, model-protobuf, model-avro — including the 394 Kafka ITs, where CacheFetchIT's extractKey / extractHeaders cases 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 b09a4b284 fixes a defect found while tracing the flow: flush and END_VALUE were driven off AvroEvent.END_MESSAGE, which the pump never pulls — it stops the moment the terminal sink closes the top-level record and returns COMPLETED. That left a transform's field run opened and never closed, disagreeing with ModelFieldBridge. The run now closes off COMPLETED, 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.

claude added 4 commits August 6, 2026 23:47
…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
claude added 6 commits August 7, 2026 00:33
…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
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 a pluggable per-field ModelTransform hook to the model pipeline (decode + encode)

2 participants