Class: Thread<TContext, TOutput>
Defined in: packages/kernl/src/thread/thread.ts:90
A thread drives the execution loop for an agent.
Ground principles:
-
Event log is source of truth.
- Persistent storage (e.g. Postgres) is treated as an append-only per-thread log of
ThreadEvents: monotonicseq, no gaps, no updates/deletes. Thread.state,tick, etc. are projections of that log, not an alternative source of truth.
- Persistent storage (e.g. Postgres) is treated as an append-only per-thread log of
-
Single writer per thread.
- At most one executor is allowed for a given
tidat a time. - Callers are responsible for enforcing this (e.g. locking/versioning) so two processes cannot
interleave or race on
seqor state.
- At most one executor is allowed for a given
-
Persist before use / observation.
- Before an event can:
- influence a future tick (i.e. be part of
historyfed back into the model), or - be considered “delivered” to a client, it SHOULD be durably written to storage when storage is configured.
- influence a future tick (i.e. be part of
- Before an event can:
-
Transaction boundaries match semantic steps.
- The intended strategy is to buffer within a tick, then atomically persist all new events + state
at the end of
tick(). - After a crash, you only ever see whole ticks or none, never half a tick, from the store’s point of view.
- The intended strategy is to buffer within a tick, then atomically persist all new events + state
at the end of
-
Recovery is replay.
- On restart, callers rebuild a
Threadfrom the stored event log (plus optional snapshots). - Any incomplete tick or pending tool call is handled by a clear, deterministic policy at a higher layer (e.g. re-run, mark failed, or require manual intervention).
- On restart, callers rebuild a
On storage failures:
“If storage is configured, it is authoritative” → fail hard on persist errors rather than treating persistence as best-effort.
If a storage implementation is present, persist(...) is expected to throw on failure, and
that error should bubble out of _execute() / stream() and stop the thread.
Type Parameters
| Type Parameter | Default type |
|---|---|
TContext | unknown |
TOutput extends AgentOutputType | "text" |
Constructors
Constructor
new Thread<TContext, TOutput>(options: ThreadOptions<TContext, TOutput>): Thread<TContext, TOutput>;Defined in: packages/kernl/src/thread/thread.ts:117
Parameters
| Parameter | Type |
|---|---|
options | ThreadOptions<TContext, TOutput> |
Returns
Thread<TContext, TOutput>
Properties
| Property | Modifier | Type | Defined in |
|---|---|---|---|
_seq | public | number | packages/kernl/src/thread/thread.ts:107 |
_tick | public | number | packages/kernl/src/thread/thread.ts:106 |
agent | readonly | Agent<TContext, TOutput> | packages/kernl/src/thread/thread.ts:96 |
context | public | Context<TContext> | packages/kernl/src/thread/thread.ts:97 |
createdAt | readonly | Date | packages/kernl/src/thread/thread.ts:100 |
metadata | readonly | Record<string, unknown> | null | packages/kernl/src/thread/thread.ts:102 |
model | public | LanguageModel | packages/kernl/src/thread/thread.ts:98 |
namespace | readonly | string | packages/kernl/src/thread/thread.ts:95 |
parent | readonly | Task<TContext, unknown> | null | packages/kernl/src/thread/thread.ts:99 |
state | public | ThreadState | packages/kernl/src/thread/thread.ts:108 |
tid | readonly | string | packages/kernl/src/thread/thread.ts:94 |
updatedAt | readonly | Date | packages/kernl/src/thread/thread.ts:101 |
Methods
append()
append(...items: LanguageModelItem[]): ThreadEvent[];Defined in: packages/kernl/src/thread/thread.ts:375
Append one or more items to history + enrich w/ runtime headers.
Core rule:
An event becomes a ThreadEvent (and gets seq/timestamp) exactly when it is appended to history. <
Parameters
| Parameter | Type |
|---|---|
...items | LanguageModelItem[] |
Returns
cancel()
cancel(): void;Defined in: packages/kernl/src/thread/thread.ts:397
Cancel the running thread
TODO: Emit thread.stop when cancelled (neither result nor error set)
Returns
void
execute()
execute(): Promise<ThreadExecuteResult<ResolvedAgentResponse<TOutput>>>;Defined in: packages/kernl/src/thread/thread.ts:152
Blocking execution - runs until terminal state or interruption
Returns
Promise<ThreadExecuteResult<ResolvedAgentResponse<TOutput>>>
stream()
stream(): AsyncIterable<ThreadStreamEvent>;Defined in: packages/kernl/src/thread/thread.ts:170
Streaming execution - returns async iterator of events
All runs (new or resumed) emit:
- Exactly one thread.start
- Zero or more model.call.* and tool.call.*
- Exactly one thread.stop (with result on success, error on failure)
Returns
AsyncIterable<ThreadStreamEvent>