# RFC 0001: Window framing and count windows

- **State:** proposed
- **Author(s):** OpenFlange specification lead
- **Created:** 2026-12-03
- **Affected specifications:** `openflange.org/pipeline/v0.1` (stream
  semantics, `windows` port kind), `openflange.org/std/v0.1` (`std:window`,
  `std:window-processing-time`), `openflange.org/pipe/v0.1` (`requires:
  [windows]`)
- **Feature maturity requested:** proposed
- **Supersedes / superseded by:** none

## Summary

Define how logical windows travel on a stream: a `windows` port carries one
complete window per NDJSON line, as a *window frame* with a small metadata
object and the window's items. Define record-count tumbling, sliding and
partitioned windows as produced by `std:window`, including what happens to
unfilled windows at end of stream. Processing-time windows are sketched as
a draft only.

Everything in this RFC is **proposed**: it is described in the v0.1 drafts
so that it can be implemented and validated, and it may change
incompatibly before it becomes `experimental`.

## Motivation

Pipes with `scope: window` (moving statistics, per-group summaries) need to
know where a window begins and ends. Three things must not be confused:

- a **batch** is a transport and processing unit. Batch boundaries carry no
  meaning and may be changed by any runner or adapter;
- a **window** is a semantic group of items with a defined boundary;
- a **dataset** is a materialised collection; materialisation is explicit.

Windowing in OpenFlange is done by ordinary reusable pipes with window-aware
port contracts, not by hidden per-node behaviour. That requires a framing
that an R script, a shell tool or a remote HTTP pipe can consume with
nothing more than a line reader and a JSON parser.

## Proposal

### 1. The `windows` port kind

A port of kind `windows` carries a sequence of window frames. Its default
media type is `application/x-ndjson; profile="openflange-windows"`. All
NDJSON rules of the pipeline specification apply (UTF-8, `\n`-terminated
lines, no blank lines, no BOM, lossless numbers).

A pipe whose contract depends on window framing MUST list `windows` in
`requires`. A runner that does not implement this RFC MUST reject such a
manifest with `E_UNSUPPORTED`.

A `windows` port is compatible only with another `windows` port. Plain
`records` are never implicitly framed, and frames are never implicitly
flattened: both directions need an explicit pipe (`std:window`, and
`std:unbatch`-style flattening of `items` by an explicit transformation).

### 2. Window frame

Each line is one complete window:

```ndjson openflange:window-frame
{"window":{"index":0,"partition":null,"start":0,"end":2,"final":false,"partial":false},"items":[{"v":1},{"v":2},{"v":3}]}
```

| Member | Type | Meaning |
|---|---|---|
| `window.index` | integer >= 0 | 0-based index of the window within its partition. |
| `window.partition` | any JSON value | The value of the partition key for this window; `null` when the stream is not partitioned. |
| `window.start` | integer >= 0 | Ordinal, within the partition, of the first item actually present in `items`. |
| `window.end` | integer >= `start` | Ordinal, within the partition, of the last item actually present in `items`. |
| `window.final` | boolean | `true` if the frame was emitted by the end-of-stream flush. |
| `window.partial` | boolean | `true` if the window holds fewer items than its nominal size. |
| `items` | array | The window's items, in stream order. Never empty. |

Producers SHOULD write the members of `window` in the order `index`,
`partition`, `start`, `end`, `final`, `partial`. Consumers MUST NOT depend
on member order and MUST ignore unknown members of `window`.

A frame is self-contained: a window is never split across lines, and a
line never contains more than one window. The end of the windows stream is
the stream's EOF; there is no end-marker frame.

Empty windows are not emitted for count windows. A stream with no items
therefore produces a `windows` stream with no frames, which is a valid
empty stream.

### 3. Count windows (`std:window`)

Parameters: `size` (integer >= 1), `step` (integer, default = `size`),
`partitionBy` (optional Starlark expression over `item` and `params`),
`emitPartial` (boolean, default `true`), `maxPartitions` (integer, default
10000).

`1 <= step <= size` MUST hold; otherwise the configuration is rejected
with `E_PARAM`. `step == size` gives tumbling windows; `step < size` gives
sliding windows whose consecutive windows overlap by `size - step` items.

Semantics, per partition:

1. Items of a partition receive ordinals 0, 1, 2, ... in arrival order.
   Ordinals are per partition and 0-based. Without `partitionBy` the whole
   stream is one partition whose `partition` value is `null`.
2. Window *k* (its `index`, 0-based, per partition) nominally covers the
   ordinals `[k * step, k * step + size - 1]`.
3. A window is emitted as soon as it fills, that is, when the item with
   ordinal `k * step + size - 1` arrives. Such a frame has `partial:
   false` and `final: false`.
4. At end of stream, every window that has started (at least one item) but
   has not filled is handled according to `emitPartial`:
   - `true` (default): **every** such window is emitted, with `partial:
     true` and `final: true`. Partitions are flushed in order of their
     first appearance in the stream, and within a partition windows are
     flushed in ascending `index`.
   - `false`: they are dropped.
5. `start` and `end` are the ordinals of the first and last item actually
   in the frame; for a partial window `end` is the last ordinal seen in
   the partition, not the nominal end.
6. `partition` is the JSON value produced by `partitionBy` for the items of
   that window.

`final` therefore means exactly "emitted by the end-of-stream flush", and
`partial` means "shorter than `size`". In this proposal, for count windows,
the two are always equal; they are kept as separate members because they
answer different questions and will diverge for other window types
(section 5).

Frames of different partitions are interleaved in the order in which their
windows fill. Order within a partition is ascending `index`.

#### Example: sliding, size 3, step 1

Input items `"a"`, `"b"`, `"c"`, `"d"`, `emitPartial: true`:

```ndjson openflange:window-frame
{"window":{"index":0,"partition":null,"start":0,"end":2,"final":false,"partial":false},"items":["a","b","c"]}
{"window":{"index":1,"partition":null,"start":1,"end":3,"final":false,"partial":false},"items":["b","c","d"]}
{"window":{"index":2,"partition":null,"start":2,"end":3,"final":true,"partial":true},"items":["c","d"]}
{"window":{"index":3,"partition":null,"start":3,"end":3,"final":true,"partial":true},"items":["d"]}
```

Windows 0 and 1 fill. At end of stream windows 2 (nominally ordinals 2-4)
and 3 (nominally 3-5) have started but not filled, so both are flushed.
With `emitPartial: false` only the first two frames are produced.

#### Example: tumbling, size 2

Input items `1` to `5`:

```ndjson openflange:window-frame
{"window":{"index":0,"partition":null,"start":0,"end":1,"final":false,"partial":false},"items":[1,2]}
{"window":{"index":1,"partition":null,"start":2,"end":3,"final":false,"partial":false},"items":[3,4]}
{"window":{"index":2,"partition":null,"start":4,"end":4,"final":true,"partial":true},"items":[5]}
```

With four input items the stream ends exactly on a window boundary: only
the first two frames are produced and no frame has `final: true`.

#### Example: partitioned, tumbling, size 2

`partitionBy: item["k"]`, input `{"k":"a","v":1}`, `{"k":"b","v":2}`,
`{"k":"a","v":3}`, `{"k":"b","v":4}`, `{"k":"a","v":5}`:

```ndjson openflange:window-frame
{"window":{"index":0,"partition":"a","start":0,"end":1,"final":false,"partial":false},"items":[{"k":"a","v":1},{"k":"a","v":3}]}
{"window":{"index":0,"partition":"b","start":0,"end":1,"final":false,"partial":false},"items":[{"k":"b","v":2},{"k":"b","v":4}]}
{"window":{"index":1,"partition":"a","start":2,"end":2,"final":true,"partial":true},"items":[{"k":"a","v":5}]}
```

Partitions are isolated: each has its own ordinals and its own `index`
sequence.

### 4. Bounded state

A window pipe holds at most `size` items per open partition (plus what its
overlap requires) and at most `maxPartitions` partitions. When a new
partition key would exceed `maxPartitions`, the pipe MUST fail with
`E_RESOURCE`; it MUST NOT evict partitions silently or drop items. There is
no partition expiry in this proposal, so an unbounded key space fails
explicitly rather than growing without limit.

Because a frame is one line, a consumer must be able to hold one whole
window in memory. Producers and runners MAY impose a maximum frame size
and fail with `E_RESOURCE` when it is exceeded.

### 5. Processing-time windows (draft)

`std:window-processing-time` with `durationMs` groups items by the wall
clock of the pipe that performs the windowing. It is a **draft within a
proposal**: the pipe name and parameter are reserved, and the frame format
above is intended to be reused, but the semantics are not specified well
enough to test portably. Known intent:

- a window closes when `durationMs` has elapsed since it opened, or at end
  of stream;
- results are inherently non-deterministic and depend on scheduling and
  backpressure, so portable tests can only make structural assertions;
- a window closed by the timer may be shorter than any nominal size
  without being the last one, and the last window of the stream is flushed
  by EOF: this is where `partial` and `final` stop being equal;
- whether empty intervals produce frames is undecided (see open questions).

A pipe that depends on this behaviour lists `processing-time-windows` in
`requires`. Runners are not expected to implement it for v0.1.

## Compatibility

New, additive, and gated by `requires: [windows]`. Runners that do not
implement it reject manifests that require it (`E_UNSUPPORTED`) and reject
`std:window` references as unsupported. Nothing in plain `records` streams
changes.

While this RFC is `proposed`, the frame members and the end-of-stream
policy may change without a deprecation period.

## Security considerations

Windowing holds state. The bounds in section 4 exist so that a hostile or
merely high-cardinality input cannot exhaust memory: partition count is
capped with an explicit failure, and window size is a declared parameter.
Frames do not introduce new permissions or data flows.

## Conformance

Portable run fixtures under `conformance/runs/` use `std:window` for:
tumbling windows, sliding windows, partitioned windows and the final
partial window (both `emitPartial` settings). Exceeding `maxPartitions`
and invalid `step` are covered as failure cases.

## Alternatives considered

**`final` marks the last window of the stream (or partition).** This is the
intuitive reading, but a producer only knows that a filled window was the
last one when EOF arrives, after the frame should already have been sent.
Supporting it requires holding back one frame per partition (look-ahead),
which delays every window by one step, complicates backpressure, and for
partitioned streams delays the last full window of every partition until
the whole stream ends. Rejected in favour of "emitted by the end-of-stream
flush", which needs no holdback. The cost is that a stream ending exactly
on a window boundary has no frame with `final: true`; consumers detect end
of stream by EOF, not by a flag.

**At end of stream, emit only partial windows that contain items not
covered by any full window.** For sliding windows the trailing partials
(`["c","d"]`, `["d"]` above) contain only items that already appeared in a
full window, so one could suppress them and emit a partial only when some
item would otherwise appear in no window at all. That minimises redundant
output, but makes the number of frames depend on a coverage rule that is
harder to state and test, and it gives a different answer for tumbling and
sliding windows of the same data. The proposal instead emits every started
window, which is one uniform rule; consumers that do not want trailing
partials set `emitPartial: false` or filter on `partial`.

**Global ordinals and a global window index.** `start`/`end` could be
positions in the whole input stream and `index` a single counter across
partitions. Global ordinals let a consumer relate a window back to the
source stream, but they make partitions depend on each other (a
partition's frames change when unrelated keys are interleaved differently),
and windows of a partition would no longer be contiguous ranges.
Per-partition, 0-based ordinals and indices keep partitions isolated and
make `start`/`end` of consecutive windows predictable (`start = index *
step`). A global sequence number may be added later as an additional
member without breaking consumers, since unknown members are ignored.

**Begin/end marker lines around plain records.** Framing windows as marker
lines interleaved with ordinary record lines would stream very large
windows without buffering. It cannot represent overlapping sliding windows
without repeating items between markers anyway, mixes control and data on
one stream, and breaks every line-oriented tool that expects one kind of
line. Rejected for v0.1.

**Windowing as a per-node option in `pipeline.yaml`.** Rejected: windowing
is a reusable pipe with a declared contract, not hidden runner behaviour.

**Reusing batches as windows.** Rejected: batches are transport units and
may be re-chunked by any runner.

## Open questions

1. Should a stream that ends exactly on a window boundary be distinguishable
   from one that was truncated, other than by the run's terminal status?
2. Should `partial` and `final` remain two members for count windows, where
   they are always equal under this proposal?
3. Should frames carry the nominal bounds (`k * step`, `k * step + size -
   1`) in addition to the actual `start`/`end`?
4. Is a per-frame size limit needed in the specification, or is it a runner
   capability limit?
5. For processing-time windows: are empty intervals emitted, which clock is
   used behind a remote HTTP pipe, and how are they tested portably?
6. Is an Arrow representation of windows (one record batch per window with
   schema metadata) wanted, and does it belong to this RFC?
7. Does flush order "partitions by first appearance, then by index" need to
   be normative, or should tests compare unordered at end of stream?

## Deferred

Not part of this RFC and not planned for v0.1:

- event-time windows and watermarks;
- session windows;
- checkpointing, recovery and persistent window state;
- partition expiry and late data handling;
- distributed windowing.

SQL analytical window functions (for example in DuckDB) operate on
materialised datasets and are unrelated to stream window framing.

## Decision

None yet. State: proposed.
