# Sequence-Gap Detection and Recovery

`D01-F05-A04` · Market Data Engineering → Order-Book Feed Engineering · archetype `record-transform` · difficulty 3/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/market-data-engineering/order-book-feed-engineering/sequence-gap-detection-and-recovery/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

```ts
import { recoverSequenceStream } from "fintech-algorithms/market-data-engineering/order-book-feed-engineering/sequence-gap-detection-and-recovery";
```

## Signature

```ts
recoverSequenceStream(arrivals)
```

Detects missing sequence numbers and decides whether the stream can continue or must resynchronise from a snapshot. A gap is not a nuisance — every message after it is applied to a book that is already wrong.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `arrivals` | `Arrival[]` | yes | Messages with their sequence numbers, in arrival order — which is not necessarily sequence order. |

## Returns

`{ status, gaps, recovered, resync_required, … }`

Each gap with whether it was recoverable from buffered messages or requires a snapshot resync.

## Errors

- When sequence numbers are absent — reported as a status rather than thrown

## Complexity

Time `O(n log n)`, space `O(n)`.

## Worked example

Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

### Input

`arrivals`:

```json
[
  {
    "sequence": 100,
    "source": "live"
  },
  {
    "sequence": 102,
    "source": "live"
  },
  {
    "sequence": 103,
    "source": "live"
  }
]
```

Showing 3 of 18 elements.

`argument 2`:

```json
{
  "start_sequence": 100
}
```

### Call

```ts
recoverSequenceStream(arrivals)
```

### Returns

object with 11 fields: start_sequence, applied_sequences, next_expected, missing_sequences, buffered_sequences, duplicate_sequences, recovery_requests, recovery_request_count, …

```json
{
  "start_sequence": 100,
  "applied_sequences": [100, 101, 102, 103, 104, 105],
  "next_expected": 117,
  "missing_sequences": [],
  "buffered_sequences": [],
  "duplicate_sequences": [107],
  "recovery_requests": [
    {
      "from_sequence": 101,
      "to_sequence": 101
    },
    {
      "from_sequence": 104,
      "to_sequence": 104
    },
    {
      "from_sequence": 106,
      "to_sequence": 106
    }
  ],
  "recovery_request_count": 5,
  "replay_arrival_count": 6,
  "trace": [
    {
      "arrival_sequence": 100,
      "source": "live",
      "next_expected": 101,
      "missing": [],
      "buffered": []
    },
    {
      "arrival_sequence": 102,
      "source": "live",
      "next_expected": 101,
      "missing": [101],
      "buffered": [102]
    },
    {
      "arrival_sequence": 103,
      "source": "live",
      "next_expected": 101,
      "missing": [101],
      "buffered": [102, 103]
    }
  ],
  "state": "current"
}
```

## Other exports

`normalizeEvents`, `reconstructL2`, `aggregatePriceLevels`, `reconstructL3`, `reconcileSnapshotIncrementals`, `consolidateVenues`, `calculate`. Every module additionally exports `run` as an alias of its primary
function, and a `meta` object carrying its catalog id, domain, family, shape and article URL.

## Verification and provenance

Tier: **verified** (via D).

The worked example below is the figure published in this algorithm's article, replayed and asserted by the test suite on every build. The arithmetic cannot drift without the build failing.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.1.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/market-data-engineering/order-book-feed-engineering/sequence-gap-detection-and-recovery/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/order-book-feed-engineering/sequence-gap-detection-and-recovery/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-data-engineering/llms.txt
