> Every Taji docs page as Markdown: https://docs.taji.is/llms.txt

# How an indicator executes

This page explains how the kernel runs an indicator, why a tick never changes a closed bar, and what a fault does. The kernel runs every indicator, built-in or custom, and serves the same rows to every client: the window draws them, your agent reads them, an alert fires on them. No client runs a script ([0001](https://github.com/TajiAI/Taji/blob/main/docs/decisions/0001-kernel-computes.md)). A closed bar's value is computed from the state committed by the bar before it, so a replay of the same bars ends where live ended. A script that throws, runs long or grows past its memory faults alone, and the kernel goes on. This page explains how, and what that asks of the code your agent writes. The budgets, fault fields and write report are listed in [the reference](https://docs.taji.is/skill/references/indicators.md#faults-and-budgets).

## Where does your script run?

A script runs on the lane: one V8 isolate on its own kernel thread, which every Desk's scripts share (`engine/crates/taji-script/src/lane.rs`). The kernel hands the lane jobs and waits on each. One job compiles a source, one runs a whole history, one advances a tick or a close. V8 is entered once per job, never once per bar. Since one thread serves every script, scripts run one job at a time.

Each distinct source gets its own V8 context, with its own copy of `@taji/indicators` and its own `ta` WebAssembly instance. Two scripts never see each other's globals. Every instance of one definition shares its context, though, so a variable at a module's top level is shared by every chart that shows it.

`@taji/indicators` captures the built-ins it relies on before any script runs, and its driver cannot be overwritten. A script that replaces `Array.prototype.map` changes nothing the kernel reads back.

## Who shares a computation?

A binding, the indicator as you added it to a chart, does not own its computation. The kernel runs an instance, keyed by everything that decides its values:

| Part of the key | Example |
|---|---|
| The definition at its version, and its params | `pulse@2` with `length: 9` |
| The series it computes on | symbol, interval, price basis, sessions, provider |
| `origin`, when set | the bar it starts from |
| The instances it reads | another indicator's plot as its `source` |
| The Desk, for a custom | a Desk's `pulse` is not another Desk's |

Every binding, read and alert operand with the same key shares one instance, so a chart and an alert on the same indicator read the same numbers. A binding's `style` and `offset` are not part of the key: a style edit resends the look and recomputes nothing ([Outputs](https://docs.taji.is/indicators/outputs.md)). An instance nothing reads for 60 seconds is dropped.

Each bar event on a series feeds every live instance on it once, upstreams first, so an indicator reading another's plot reads a row that already exists ([Inputs](https://docs.taji.is/indicators/inputs.md)).

## What runs when an instance starts?

A new instance runs `setup` and then `next` over every held bar, oldest first, in one job: the history run. `ctx.live` is false throughout, and `ctx.last` is true on the last bar. Then the instance waits for events.

An instance computes from its first bar: the first held bar at or after its `origin` when the binding sets one, else the first bar the series holds. A bar before `origin` adds no row.

## What happens on a tick, and on a close?

A tick is an update to the forming bar. A close is the bar's final value.

```mermaid
sequenceDiagram
  participant K as Kernel
  participant I as Instance
  Note over I: committed: state after bar n−1
  K->>I: tick 1 of bar n
  I-->>K: row n from committed state
  K->>I: tick 2 of bar n
  Note over I: restore committed, then next
  I-->>K: row n replaced
  K->>I: close of bar n
  Note over I: restore committed, then next
  I->>I: commit: state after bar n
  I-->>K: row n final
```

Read it from the top. Each tick runs `next` from the state committed by bar n−1, never from the previous tick's result, and replaces row n. The close runs `next` from that same committed state once more, then commits: only a close commits.

So a tick never leaves a trace. The state `setup` returned is restored, its `ta` handles restore their own committed state, drawing changes roll back, and `log` keeps no line from a tick. The forming bar can run many times and each run starts clean, so a closed bar's value is the same whether the bar arrived as one close or as a hundred ticks before it.

## Why must state live in what `setup` returns?

Because only that state is restored. The kernel cannot restore a variable it does not hold. Here is Pulse's percent change from [your first indicator](https://docs.taji.is/indicators/first-indicator.md), with the previous close kept at the module's top level instead of in the state:

```ts file="examples/execution/leaky_change.ts" title="leaky_change.ts"
import { indicator, plot } from "@taji/indicators";

let prev = NaN;

export default indicator({
  name: "Leaky change",
  pane: "own",
  plots: { change: plot.line({ title: "Change %" }) },
  setup: () => {
    prev = NaN;
    return {};
  },
  next(bar) {
    const change = (bar.close / prev - 1) * 100;
    prev = bar.close;
    return { change };
  },
});
```

1. **`let prev = NaN`** sits outside the state, at the module's top level.
2. **`setup`** resets `prev` and returns an empty state, so a fresh instance starts clean.
3. **`next`** computes the change from `prev`, then moves `prev` to this bar's close. On a close, that is right. On a tick it is not: the tick moves `prev`, nothing moves it back, and the close computes its change from the last tick's price instead of the bar before's close.

On a chart's history, which arrives as closes, this script draws the right line. Live, a bar whose last tick carried its close reads a change of 0. The write check finds it before any chart draws it. `indicators/check` runs the same checks as a write and stores nothing:

```json file="examples/execution/leaky.json"
{
  "errors": [],
  "report": {
    "bars": 1024,
    "drawings": {
      "bar_categories": 0,
      "boxes": 0,
      "cards": 0,
      "labels": 0,
      "lines": 0,
      "polylines": 0,
      "tables": 0
    },
    "fault": null,
    "invariants": [
      {
        "detail": null,
        "name": "determinism",
        "ok": true
      },
      {
        "detail": null,
        "name": "prefix",
        "ok": true
      },
      {
        "detail": "row 2024-02-09T20:00:00Z: change 0 vs 0.1676270298585525",
        "name": "ticks_then_close",
        "ok": false
      }
    ],
    "logs": [],
    "ms": 3.246042,
    "plots": [
      {
        "first": 1705655700,
        "last": -0.2274607113316729,
        "max": 0.4196255648805769,
        "min": -0.4309880400818922,
        "na": 1,
        "name": "change"
      }
    ],
    "tests": []
  }
}
```

- `determinism` and `prefix` hold: `setup` resets `prev`, and a run over closes alone is right.
- `ticks_then_close` fails. Its `detail` names the first row that differs: its time, the plot, then the value the ticked run gave against the value closes alone gave. The check's third tick is the whole bar, so the ticked run reads `change 0` where closes alone read `0.1676270298585525`.

Pulse keeps `prev` in `s`, the state `setup` returns, and the same check passes it:

```json file="examples/execution/pulse.json"
{
  "errors": [],
  "report": {
    "bars": 1024,
    "drawings": {
      "bar_categories": 0,
      "boxes": 0,
      "cards": 0,
      "labels": 0,
      "lines": 0,
      "polylines": 0,
      "tables": 0
    },
    "fault": null,
    "invariants": [
      {
        "detail": null,
        "name": "determinism",
        "ok": true
      },
      {
        "detail": null,
        "name": "prefix",
        "ok": true
      },
      {
        "detail": null,
        "name": "ticks_then_close",
        "ok": true
      }
    ],
    "logs": [],
    "ms": 2.787958,
    "plots": [
      {
        "first": 1705655700,
        "last": -0.2274607113316729,
        "max": 0.4196255648805769,
        "min": -0.4309880400818922,
        "na": 1,
        "name": "change"
      },
      {
        "first": 1705662900,
        "last": -0.03842140794957199,
        "max": 0.21412616231653991,
        "min": -0.24276065717267192,
        "na": 9,
        "name": "avg"
      }
    ],
    "tests": []
  }
}
```

*Captured 2026-12-03 on a keyless headless kernel. A check runs on the kernel's own fixture bars, so your report matches but for `ms`.*

A write refuses the leaky script with 422 `check` and stores nothing. The same rule covers a counter at the top level, which fails `determinism`: a second fresh instance in the same context counts on from where the first stopped.

## When does a closed bar's value change?

Only when its instance recomputes. A close that repeats the last with equal values changes nothing. Any other event, such as a bar out of order, a backfill or a restated bar, recomputes the instance from its first bar. The recompute starts empty, drawings included, and rebuilds.

Recompute is the one repair, never a patch at the changed bar. A value can depend on any earlier bar: Pivot Points for a year read the year before, so a backfill changes the first full year inside the old range.

`origin` decides whether scrolling back moves its values. With `origin` set, scrolling back loads earlier bars and the recompute that follows skips them, so values from `origin` on never depend on what the screen shows. Without one, the earlier bars become the new first bar and the recompute starts there, so a value that warms up from its first bar, such as an exponential average, can shift.

Instance rows live in memory only. A restart recomputes them from the held bars.

## What does `calc` change?

Any indicator takes `calc`, an interval at or above the chart's. The instance computes on that series, and its rows are copied onto the chart's bars by the instrument's calendar:

| `calc_closes` | A chart bar shows |
|---|---|
| `true`, the default | a calc bar's value only on the chart bar whose close closes it, and on the first chart bar |
| `false` | the newest calc bar its close has closed; the live bar, the forming calc row |

## Which indicators repaint?

A binding repaints when what its live bar showed can differ from what its history shows. Two things mark a binding `repaints` in its served metadata:

- reading `data.<n>.forming`, a higher bar still in progress ([Inputs](https://docs.taji.is/indicators/inputs.md));
- `calc` with `calc_closes` false, whose live bar follows the forming calc row while its history holds closed calc bars only.

## What does a write check?

Every write checks the tick model before it stores a version. The source compiles, runs over a fixed fixture of 15-minute bars at the definition's default inputs, and three invariants must hold (`engine/crates/taji-script/src/check.rs`):

| Invariant | It compares | It catches |
|---|---|---|
| `determinism` | a second fresh instance's rows with the first's, bitwise | state at the module's top level |
| `prefix` | a history over the first half with the first half of the full run | a value that reads a bar after its own |
| `ticks_then_close` | the last 20 bars arriving as three ticks each and then their close, with closes alone | state a tick moves and nothing restores |

Each compares the final drawings too, and a failure names the first differing row. A compile error, a fault, a failed invariant or a failed test stores nothing. Other inputs and other bars are not checked before they are bound.

`indicators/check` runs the same checks and stores nothing.

A write blocks the kernel's turn: the indicator pass runs before the answer, so the next `GET /get/indicators` reads the new version. Every job in the wait has its budget, so the wait is bounded.

## How long may a script run?

Each job has a budget of CPU time on the lane's thread:

| Job | Budget |
|---|---|
| Compile, a history run, one test | 1 second |
| `setup`, one tick, one close | 50 ms |

The kernel measures the thread's own CPU time and terminates the job past its budget. Time the thread spends preempted is not charged, so a busy machine never faults a script. The kernel waits on the lane's jobs, so the budgets bound every wait: a script never holds the kernel longer than its budget. `Atomics.wait` is disabled, since a job that blocks spends no CPU time and so would never meet its budget.

A history longer than one second of CPU time faults `budget`. Set `origin` or a coarser `calc` to compute over fewer bars.

The lane's heap is 256 MB, and array buffers have 256 MB of their own. A job that reaches either is terminated instead of letting V8 abort the process.

## What happens when a script faults?

| Kind | Cause |
|---|---|
| `threw` | the script threw |
| `budget` | a job ran past its budget |
| `memory` | the heap or array buffers reached their limit |
| `compile` | a stored version the kernel cannot compile for an instance |

A fault carries the bar it hit, where there is one, and the line and column where the throw is in the script. On a write, the fault is the report and nothing is stored. On a bound instance, its rows clear, the legend shows a red `!` with the message, and `GET /get/indicators` carries it as `meta.fault`. A faulted instance stays inert until its key changes or it is dropped, since a retried budget fault would block the kernel's turn on every reload. The kernel and every other instance go on.

## What happens to an older version?

Nothing: every distinct write is the next version, immutable, `name@1`, `name@2`, stored as files in the Desk's directory and never rewritten. A write identical to the latest stores nothing. A binding on the bare name follows the latest version and recomputes on the next pass after a write; a binding on `name@N` stays on that one. An alert pins the version its indicator resolves to when the alert is set, so a later write never changes what an existing alert watches.

## See also

- [The indicator SDK](https://docs.taji.is/indicators/sdk.md): what a script can say, state and missing values.
- [Inputs](https://docs.taji.is/indicators/inputs.md): inputs, anchors and reading other series.
- [Outputs](https://docs.taji.is/indicators/outputs.md): plots, colours, fills and drawings.
- [Indicator reference](https://docs.taji.is/skill/references/indicators.md): the report, budgets, limits and tests in full.
- [0001: The kernel computes](https://github.com/TajiAI/Taji/blob/main/docs/decisions/0001-kernel-computes.md): why no client runs a script.
