# HistoryStore
Source: https://docs.maelstrom-ai.co/dev/reference/engine/interfaces/historystore/
Documentation revision: 89a8f5e9d10cedcf0b30376d7846113f06f26120
SDK manifests in this build: @maelstrom-co/react@0.2.1, @maelstrom-co/client@0.2.1, @maelstrom-co/connectors@0.2.1, @maelstrom-co/protocol@0.2.1, @maelstrom-co/engine@0.2.1
These docs follow this source revision. Manifest versions do not prove release compatibility; verify the installed package exports before using an API.

Defined in: [packages/engine/src/lib/history/history-store.ts:55](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/engine/src/lib/history/history-store.ts#L55)

Backing persistence for [HistoryManager](https://docs.maelstrom-ai.co/dev/reference/engine/interfaces/historymanager/). The engine ships an in-memory reference implementation; production deployments provide their own (Redis, Postgres, SQLite, etc.).

Implementations MUST guarantee that [append](https://docs.maelstrom-ai.co/dev/reference/engine/interfaces/historystore/#append) is atomic with respect to concurrent calls sharing the same `(sessionId, entry.input.id)` pair — exactly one wins and the duplicate reports `{ inserted: false }`. In single-process JavaScript this is free; in multi-process deployments use the backend’s native primitives (`INSERT OR IGNORE`, `ON CONFLICT DO NOTHING`, Redis `SETNX`, etc.).

## Type Parameters

### TMeta

`TMeta` = `unknown`

## Methods

### append()

> **append**(`sessionId`, `entry`): `Promise`<{ `inserted`: `boolean`; }>

Defined in: [packages/engine/src/lib/history/history-store.ts:60](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/engine/src/lib/history/history-store.ts#L60)

Atomically check-and-insert. Returns `{ inserted: false }` if an entry with the same MessageId already exists in the given session.

#### Parameters

##### sessionId

`SessionId`

##### entry

[`HistoryEntry`](https://docs.maelstrom-ai.co/dev/reference/engine/type-aliases/historyentry/)<`TMeta`\>

#### Returns

`Promise`<{ `inserted`: `boolean`; }>

* * *

### clear()

> **clear**(`sessionId`): `Promise`<`void`\>

Defined in: [packages/engine/src/lib/history/history-store.ts:75](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/engine/src/lib/history/history-store.ts#L75)

Wipes all entries for a session. No-op on an unknown session.

#### Parameters

##### sessionId

`SessionId`

#### Returns

`Promise`<`void`\>

* * *

### get()

> **get**(`sessionId`): `Promise`<readonly [`HistoryEntry`](https://docs.maelstrom-ai.co/dev/reference/engine/type-aliases/historyentry/)<`TMeta`\>\[\]>

Defined in: [packages/engine/src/lib/history/history-store.ts:66](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/engine/src/lib/history/history-store.ts#L66)

Returns all entries for a session in insertion order.

#### Parameters

##### sessionId

`SessionId`

#### Returns

`Promise`<readonly [`HistoryEntry`](https://docs.maelstrom-ai.co/dev/reference/engine/type-aliases/historyentry/)<`TMeta`\>\[\]>

* * *

### getResponseById()

> **getResponseById**(`sessionId`, `messageId`): `Promise`<`GetResponseByIdResult`\>

Defined in: [packages/engine/src/lib/history/history-store.ts:90](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/engine/src/lib/history/history-store.ts#L90)

Point-lookup of a previously recorded `turn`’s response.

Returns one of three semantically distinct outcomes (see GetResponseByIdResult). The engine’s replay path treats `miss` and `kind-mismatch` identically (both fall through to a fresh LLM call), but the type-level distinction lets the consumer decide what to do about a `messageId` reuse across kinds — surface it as a warning, page on it, etc. Stores backed by a remote backend should implement this against backend-native point-lookup primitives (Redis `HGET`, Postgres `SELECT response WHERE …`) to avoid materializing the full [HistoryEntry](https://docs.maelstrom-ai.co/dev/reference/engine/type-aliases/historyentry/).

#### Parameters

##### sessionId

`SessionId`

##### messageId

`MessageId`

#### Returns

`Promise`<`GetResponseByIdResult`\>

* * *

### replace()

> **replace**(`sessionId`, `entries`): `Promise`<`void`\>

Defined in: [packages/engine/src/lib/history/history-store.ts:69](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/engine/src/lib/history/history-store.ts#L69)

Overwrites the session’s entry list. Used by truncation.

#### Parameters

##### sessionId

`SessionId`

##### entries

readonly [`HistoryEntry`](https://docs.maelstrom-ai.co/dev/reference/engine/type-aliases/historyentry/)<`TMeta`\>\[\]

#### Returns

`Promise`<`void`\>
