> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Maintain a windowed derivation

> Derive a series from aligned windows over series and timelines.

`windowed({...})` from `@bijectionhq/library-series` publishes one output
sample per subject and window start. It returns a subtree that owns its
output, and publishes each window and its input evidence in one transaction.

## Declare inputs, grid and plan

Every input shares the business key `by`. Use a `series` for samples and a
`timeline` for values in force over a period. An input may not be family labelled.

```ts bijection/energy.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, label, role, series, t, timeline } from "@bijectionhq/bijection";
import { windowed } from "@bijectionhq/library-series";

export const operator = role({});
export const maintainer = role({});

export const power = series({ meter: t.string, watts: t.number }, { key: ["meter"] });
export const tariff = timeline({ meter: t.string, rate: t.number }, { key: ["meter"] });

export const energy = windowed({
  inputs: { series: { power }, timeline: { tariff } },
  by: { meter: t.string },
  subjects: [{ meter: "m1" }],
  output: { wattSeconds: t.number, rate: t.maybe(t.number) },
  every: { hours: 1 },
  context: { before: { minutes: 5 } },
  lateness: { minutes: 15 },
  finality: "settled",
  plan: (w) => ({
    wattSeconds: w.integral("power", "watts", { interpolate: "previous", maxGap: { minutes: 5 } }),
    rate: w.atStart("tariff", "rate"),
  }),
  label: label.public,
  roles: { read: [operator], track: [operator], maintain: [maintainer] },
});

export default app({ operator, maintainer, power, tariff, energy });
```

The subtree is `energy.output` (the result series), `energy.state`,
`energy.track`, `energy.status`, `energy.input` and the clock reaction
`energy.tick`. `roles.track` may register ranges, `roles.read` may read
status and worker inputs, and `roles.maintain` is what the clock reaction
holds.

The builder serializes the plan and puts its digest in the definition. Use
`reduce` for count, sum, mean, min or max, `integral` and `coverage` for
bounded gaps, and `atStart` for a timeline value. `number` and `combine`
supply constants and binary arithmetic.

## Track windows and read results

From a client, call `track` with grid-aligned instants:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { api } from "@bijectionhq/bijection/client";
const result = await client.mutate(api().energy.track, { range: { from, to } });
```

The end is exclusive. The grid begins at `origin`, which defaults to the Unix
epoch, and a call tracks up to 64 subject-window pairs. The clock reaction
revisits due windows to pick up changed inputs. Read results from
`energy.output` by the business key and the window start. Each records
`through` (the version its inputs were read at, `ctx.frame.version`),
`definition`, `series` and `final`.

With `finality: "available"`, a window publishes once its full context closes
and may be revised as inputs arrive. With `finality: "settled"`, it also
waits for `lateness`. Supply `frontier: { revision, read }` when the source
must attest its progress through the context, lookahead included.

## Place large windows on external compute

Local evaluation admits up to 256 input facts and 128 KiB. Add
`job: { revision, target, within }` for larger windows or an external
implementation. `revision` is the worker artifact's SHA-256. `target`
receives the `windowContract(by, output)` value types and the revision, and
answers the [target](https://docs.bijection.com/transforms/external-runtimes.md) the window submits to.

While an attempt is pending, the window pins its input version, so the
worker reads exactly that input version. The worker receives the window and context
bounds and the serialized plan and its digest, and pages each input through
`energy.input` for that attempt. A TypeScript worker can evaluate the same
plan with `evaluateWindow` from `@bijectionhq/library-series/algebra`. The
library accepts the result only for the current attempt and publishes it with
its evidence.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.