Skip to content

model-core: align bytes/string extension stages with the streaming stage design, and make them bidirectional #2393

Closed

Description

@jfallows

Describe the desired outcome from the user's perspective

As a Zilla contributor writing a bytes or string model extension, I want the same streaming stage contract every other model's extensions already have — a stage that sees a value as it flows rather than only once it is whole, that can suspend against a bounded destination, that can reject with a diagnostic, and that can reach whatever the pipeline offers a stage — so an extension for these models is not limited by a stage shape that predates the rest.

Acceptance criteria

1. The gap

BytesTransform and StringTransform are whole-value calls:

int transform(
    DirectBufferEx value,
    int index,
    int length,
    MutableDirectBufferEx dst,
    int dstIndex);

No controller, no source, no sink, no status — a length back, or OMIT. Four consequences follow from that shape:

  • The whole value is materialized before any stage runs. CoreExtModelPipeline accumulates into its own value buffer first, because the contract promises "one complete decoded value". Every other model's stages see a value in fragments and keep the pipeline streaming.
  • A chain of N stages costs N copies of the value. The fold alternates through two scratch buffers per stage, with only the last writing the output buffer. A sink-chained design costs one pass with no intermediate materialization.
  • There is no back-pressure. An int return cannot say "destination full, resume me", so the entire transformed value must fit the destination at the offset given. For a model whose values are arbitrary bytes, that is a hard ceiling rather than a tuning question.
  • OMIT conflates withholding with rejecting. It surfaces as REJECTED with no diagnostic, so a stage declining a value and a stage rejecting one are indistinguishable to anything downstream.

2. Realign the stage contract

Give these stages the same shape the other model pipelines use: a stage consumes one event with a read-only view of the current value and forwards what it keeps to its downstream, steering its upstream through a control handle and reporting a status. Chains bind sink-to-sink at assembly, so a composition is one pass.

Two things this issue should settle rather than assume:

  • The event vocabulary for a value with no internal structure. Presumably value-start, segment, value-end, with no field event — a strict subset of what a structured format needs. Whether bytes and string share one vocabulary is part of the same question, given they differ only in whether the bytes are validated as text in the configured encoding.
  • Whether the machinery is extracted. Every other format keeps its pipeline machinery in a module of its own, with the model module adapting it to the engine's vocabulary. If these stages stay inside model-core, they can keep naming engine types directly; if the machinery moves out, they cannot, and the interfaces they need must move with it. §4 depends on the answer.

Withholding a value and rejecting one with a diagnostic become distinct outcomes.

3. Directionality

BytesModelExtHandler and StringModelExtHandler declare a single transform(...) with no direction at all. Every other model's extension handler declares decode and encode independently, each defaulting to a pass-through, so an extension that applies to one direction overrides that method alone. The decode-only behavior here is not in the contract — it comes from the handler delegating the encode direction to the plain handler and never folding a stage in, with a class comment asserting the restriction as policy.

  • Split the extension handler into decode and encode with pass-through defaults, matching the other models.
  • Fold installed stages into the encode pipeline as well, and drop the comment asserting the policy. An extension that applies only on read then overrides decode alone — its own decision rather than a rule this model imposes.
  • padding() carries no direction today. If a stage can expand a value on write, the encode side needs its own accounting.

4. Where the metadata envelope lands

Once a control handle exists, the metadata channel from #2389 reaches these stages the same way it reaches every other model's — an accessor on the handle, defaulting to the empty envelope. The choice to settle alongside §2: name the engine's ModelEnvelope directly, which is available while these types live in a module that depends on the engine, or declare one of these models' own with an adapter, which becomes necessary rather than optional if §2 extracts the machinery. Retrofitting the envelope onto the current signature is not worth doing — the only place it could go is stage construction, which is the shape this issue replaces.

5. Coverage

A stage observing a value delivered across fragments rather than whole; a bounded destination suspending and resuming until drained; a chain of stages applied in one pass with no intermediate materialization; withholding distinguished from rejecting with a diagnostic; an extension applying on the encode direction, and one overriding only decode leaving encode unchanged; and the identity fast path preserved when no stage is installed.

Additional context

This changes an exported extension SPI. Its composition mechanism landed recently in #2382, so the cost of reshaping it is as low now as it will be.

Related: #2389 (reaching the metadata channel from a model's own stage vocabulary, which this unblocks for these two models), #2384 (the channel itself).

Activity

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

Metadata

Metadata

Assignees

No one assigned

    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