Skip to content

engine: add ModelEnvelope for message metadata outside the value, exposed via ModelController #2384

Closed

Description

@jfallows

Describe the desired outcome from the user's perspective

As a Zilla contributor building a model extension, I want a protocol-neutral way to read and write metadata that travels alongside a message but isn't part of the value's own encoding, so the same extension works across any binding that supplies this channel, not just one protocol's specific wire mechanism.

Acceptance criteria

1. The problem this solves

ModelTransform's contract is deliberately protocol-neutral — ModelSource/ModelSink describe the value being transformed, nothing else, and stay usable by any binding with fields to transform. Some model extensions need a place to carry data that isn't part of the value's own bytes at all — data a specific protocol usually carries in its own out-of-band channel (a Kafka record's headers, an HTTP response's headers or trailers, whatever a given protocol offers). No such seam exists today, and adding a protocol-specific one directly to ModelController would tie the model layer to one protocol's vocabulary — exactly what the SPI-neutrality convention (runtime/engine/AGENTS.md) exists to prevent.

2. ModelEnvelope — a generic, named, repeatable metadata channel

public interface ModelEnvelope
{
    int count(String name);
    DirectBufferEx get(String name, int index);
    void set(String name, DirectBufferEx value); // accumulates: repeated calls under one name add occurrences, not overwrite
}

Deliberately minimal and deliberately ignorant of what any name means — ModelEnvelope never interprets its own contents; it's a named, ordered, repeatable byte-value list, nothing more. Whether a given name occurs zero, one, or many times per message is entirely up to whatever populates and consumes it. count/indexed get already imply the multi-valued shape, so set accumulating on repeat calls follows from the interface as a whole rather than needing a separate verb to say so — worth an explicit Javadoc note on set regardless, since "set" read in isolation more commonly connotes overwrite.

ModelController gains envelope(): ModelEnvelope, mirroring exactly how #2310 added authorization() — a generic accessor for something message-scoped that oss doesn't itself interpret, so a composed ModelTransform can reach for it without threading a new parameter through ModelTransform.transform(...)'s signature. (#2310 itself did widen the outer ModelPipeline.transform(...) signature to get authorization in from the binding in the first place — the part it avoided widening was the nested, per-stage ModelTransform.transform(...) signature, by exposing the value through ModelController instead. ModelEnvelope mirrors that second half: §3 below has the binding's own pipeline layer populate/drain the envelope directly, so no ModelPipeline.transform(...) signature change is needed here at all — an easier bar to clear than #2310's, not an identical one.)

3. Who populates it, and how — binding-specific, not model-specific

A binding's own pipeline layer (whatever already bridges model-level events into that protocol's own wire concepts) is responsible for supplying the real ModelEnvelope implementation:

  • On the read/decode side, before invoking the value model pipeline, it populates the envelope from whatever out-of-band data the incoming message actually carries.
  • On the write/encode side, it starts with an empty envelope and, once the value model pipeline completes, materializes whatever accumulated into it as real output on the wire, in whatever form that protocol's own out-of-band channel takes.

ModelEnvelope itself never needs to know which protocol is backing it — a binding built around a different transport could supply its own implementation against the identical interface, and any ModelTransform written against ModelEnvelope works unmodified against it.

4. Relationship to #2311

Complementary, distinguished by lifecycle rather than by which one carries out-of-value bytes — both can. #2311's onBlock/BlockEvent reports a rewritten region's position, always relative to the value, plus optional out-of-value bytes for a fixed-width target that can't carry its ciphertext-or-equivalent in place — but that segment() data is valid only for the duration of the single callback that reports it, the same ephemeral lifecycle ModelEvent's own buffer already has; a consumer that needs it beyond the call must copy it out immediately. ModelEnvelope is the opposite shape: a named, repeatable, accumulating store that persists across the whole transform and is read back (on decode) or drained (on encode) as a unit, not delivered as a stream of per-callback events. An extension needing both — reporting where in the value it rewrote something, while separately accumulating reference data addressed by name rather than position — uses both mechanisms together, each suited to a different access pattern over data that, in both cases, lives outside the value's own semantic content.

5. Coverage

A test ModelTransform reading and writing named metadata via ModelController.envelope(); a repeated set() call under one name producing an additional entry retrievable by get(name, index) rather than overwriting the prior one; a name with zero occurrences returning count() == 0 and no matching get; a transform that never touches the envelope leaving it empty on encode and unread on decode, with no cost.

Additional context

Mirrors #2310's precedent in spirit — a generic, message-scoped accessor added to ModelController — though not in mechanism, per §2's correction: #2310 widened ModelPipeline.transform(...)'s signature to bring authorization in; this issue avoids any ModelPipeline.transform(...) change at all, relying entirely on the binding's own pipeline layer to populate/drain the envelope.

Activity

  1. self-assigned this
    on Aug 15, 2026
  2. jfallows commented on Aug 16, 2026

    @jfallows
    ContributorAuthor

    §2's "no ModelPipeline.transform(...) change is needed here at all" holds narrowly, and it's worth recording where the boundary falls — it wasn't obvious until the code was walked.

    It holds for a caller whose own stage is in the ModelTransform chain. A mediating stage supplies its own ModelController downstream, so it can override envelope() for every stage after it, with no signature change anywhere. That is exactly §3's mechanism and it works.

    It does not hold once the stage that needs the envelope is not in that chain, for two reasons:

    1. No seam to supply one. ModelController isn't passed into a model — each model builds its own and pushes per-message state in. model-avro is representative: AvroModelDecoderPipeline.transform(...) takes authorization and forwards it with mediating.authorization(authorization) into AvroModelTransform's private Control implements ModelController. A caller holding an envelope has nowhere to hand it over.

    2. A model's format-native stages are upstream of the adapter. Every model with a *ModelExtFactorySpi folds an extension's stages into the format-native stream ahead of the adapter that bridges to the generic vocabulary:

      // AvroModelHandlerImpl.newPipeline
      extendDecode(Avro.stream(Avro.parser(schema)), schema)
          .transform(adapter)
          .into(generator);

      AvroTransformable.transform appends "in data-flow order", so those stages run first. The controller one of them receives is AvroPipelineImpl.Control, private to common-avro and holding only the parser; AvroModelTransform.Control is built inside model-avro and handed downstream. So a format-native stage sees no ModelController at all, and the mediating-stage seam above can't reach it. common-json and common-protobuf are shaped the same way.

    None of this changes what shipped — ModelEnvelope and ModelController.envelope() are right as they are, and the default NONE keeps it additive. It's the two halves either side that are missing: a per-message supply seam, and reach into each format layer (which also means deciding whether common-avro/common-json/common-protobuf each declare their own envelope interface, since none of them depends on engine, or whether the interface moves somewhere both layers see — it needs only DirectBufferEx).

    Filed as #2389 rather than reopening this one. Suggest amending §2 here to scope the claim to the in-chain case and point at #2389 for the rest.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions