# AgentConnection
Source: https://docs.maelstrom-ai.co/dev/reference/engine/interfaces/agentconnection/
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/agent-session/connection.ts:78](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/engine/src/lib/agent-session/connection.ts#L78)

One connection’s ordered Agent Session event stream.

Capacity is bounded per connection:

-   At most 32 utterances are pending, including the one being planned or awaiting acknowledgement. A further utterance receives an `error` event with code `queue_full` and is not remembered, so it can be sent again.
-   At most 4,096 input IDs are remembered, with no eviction, so a repeated input is always ignored. After that, a new utterance receives an `error` event with code `session_capacity`. The host must open a new connection to continue.
-   Each UI change waits for acknowledgement for at most [OpenConnectionOptions.acknowledgementTimeoutMs](https://docs.maelstrom-ai.co/dev/reference/engine/interfaces/openconnectionoptions/#acknowledgementtimeoutms), 30 seconds by default. On the UI-only route a timeout fails the run and cancels queued work. On an agent connection a timeout of a `change_screen` change answers the goals that depend on it `unknown`, leaves a goal with a clarification verdict at `needs_clarification`, and lets the next queued Screen Change start.

The agent loop is experimental in 0.3 and may change in any minor release.

> [!WARNING]
> Experimental
> 
> This API should not be used in production and may be trimmed from a public release.

## Methods

### close()

> **close**(): `void`

Defined in: [packages/engine/src/lib/agent-session/connection.ts:92](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/engine/src/lib/agent-session/connection.ts#L92)

Aborts pending work and releases listeners. Idempotent; no later events are emitted.

> [!WARNING]
> Experimental
> 
> This API should not be used in production and may be trimmed from a public release.

#### Returns

`void`

* * *

### send()

> **send**(`input`): `Promise`<`void`\>

Defined in: [packages/engine/src/lib/agent-session/connection.ts:88](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/engine/src/lib/agent-session/connection.ts#L88)

Accepts untrusted input. An utterance resolves according to its route: the UI-only route resolves when it emits `ui_change`, before the later `ui_applied`; an Agent Run route resolves when the run finishes, so a `change_screen` call can keep it pending until `ui_applied` or the acknowledgement timeout. Sends queued behind earlier work can also remain pending. Input sent after `close()` is dropped without an event or a diagnostic.

> [!WARNING]
> Experimental
> 
> This API should not be used in production and may be trimmed from a public release.

#### Parameters

##### input

`unknown`

#### Returns

`Promise`<`void`\>

* * *

### subscribe()

> **subscribe**(`listener`): () => `void`

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

Observes future events in emission order. Returns an unsubscribe function; there is no replay.

> [!WARNING]
> Experimental
> 
> This API should not be used in production and may be trimmed from a public release.

#### Parameters

##### listener

(`event`) => `void`

#### Returns

() => `void`
