Sessions, Threads, And Events
HPD Agent uses sessions, threads, and events as the durable coordination model for agent work. They are not only chat-history storage. The same model is used by normal turns, hosted runtimes, subagents, multi-agent workflows, content projection, compaction, replay, and UI navigation.
The short model is:
session: the workspace container for metadata and shared middleware state
thread: a replayable transcript path with thread state, its relations to other threads, and event history
event: the canonical vocabulary for rendering, replay, tools, middleware, and nested workIn direct in-process code, the AgentRuntime owns the session store, and methods such as CreateSessionAsync(...), RunAsync(...), and ForkThreadAsync(...) operate against it. An input names the agent that runs it, or runs the runtime's default agent.
In ASP.NET Core hosted applications, routes select the thread and each input selects its agent:
/sessions/{sessionId}/threads/{threadId}/inputs { "text": "...", "agentId": "reviewer" }The sessionId and threadId select the state and thread path. The input's optional agentId selects an agent definition of the hosted runtime, a code agent or a stored one; without it the default agent runs.
Sessions
A session is the top-level state scope for an interaction. It becomes restart-durable when the application configures a durable session store. It has:
- an id
- creation and last-activity timestamps
- metadata
- session-scoped middleware state
Hosted session creation and AgentRuntime.CreateSessionAsync(...) create an initial thread named main. The lower-level session store can also create a session without any threads. Session metadata updates are merge patches: new keys are added, existing keys are overwritten, and null values remove keys.
A session is the shared context of a set of related threads. Its threads fork from and create one another only inside it, its session-scoped middleware state is shared by all of them, and it is created, deleted, and expired as one unit. For example, permission choices that apply across a user's thread tree, and the coding harness's view of one workspace, are session-scoped middleware state.
A session can represent a product workspace or conversation. Bots may map a platform thread to one session, and a multi-agent workflow run uses one session. Session metadata belongs to the application, such as a customer id, project id, or platform key; HPD writes none.
Threads
A thread is the replayable path inside a session. A thread has:
- an id
- optional name, description, tags, and metadata
- relations to other threads: the thread it was forked from and the fork point, ancestors, total forks, and the thread that created it, when one did
- thread-scoped middleware state
- a durable thread event log
Threads are the unit for forking, replay, thread-specific state, and hosted runtime ownership. main is the conventional ID for an automatically created first thread, not a protected identity. It can be deleted like any other leaf thread. Deleting the last thread leaves the session and its session-scoped state intact.
When an in-process RunAsync(...) omits threadId:
- a session with a
mainthread uses it, however many other threads it has; - a session with no threads creates and uses
main; - a session with threads but no
main, for example aftermainwas deleted, raisesAmbiguousThreadException, which lists the threads to choose from.
An explicitly supplied missing thread ID still fails rather than creating a replacement.
A thread is not bound to an agent. Each run chooses its agent: a run that names an agentId uses that agent, and a run that names none uses the runtime's default agent. To keep a conversation with the agent that last ran on a thread, read it with store.GetLastExecutingAgentIdAsync(sessionId, threadId) and pass it as the agentId.
Forking creates a new thread from a message id on an existing thread. Thread-scoped persistent middleware state is copied at fork time and then diverges between threads.
Threads are also how HPD keeps nested agent work understandable. A subagent's first invocation creates a child thread in the parent's session. A fresh child starts with no parent history; the default handoff child starts with the parent conversation written in as text, so it has the current context without interleaving its transcript with the parent's. Like any thread, the child thread belongs to no agent. It records only the thread that created it; the subagent's agent id, tool call, and delegation invocation are in the parent's registry and events. A later run against the completed child thread names the agent it wants through the normal thread APIs; the subagent tool itself does not target an existing thread.
Multi-agent workflows use the same thread model, but the application chooses the policy. A workflow can write every node into one shared thread, give each node its own thread, or fork one thread per node from a root workflow thread. Node thread ids are derived from the workflow execution id and the node id, and each node's turns record the agent that ran them; the workflow writes nothing into session or thread metadata.
Media-heavy features make this thread scope visible. For example, audio transcript projection, content uploads, and assistant audio artifacts use the active sessionId + threadId so replay and forks can keep durable text, media references, and artifacts attached to the correct path. See Content Upload And Resolution for the upload and resolver flow.
How Features Use The Model
| Feature | Session Use | Thread Use |
|---|---|---|
| Normal chat | User, case, project, or platform thread container | Active transcript and thread-scoped middleware state |
| Forking | Keeps alternatives grouped together | Creates a new path from a message id |
| Subagents | The parent's session | A newly created fresh or handoff child thread |
| Multi-agent workflows | Workflow/workspace container with run metadata | Shared, per-agent, or forked node transcripts |
| Compaction | Shared policy can remain stable | Model-visible history or a fork target can be reduced; the journal keeps every source event |
| Content and audio | Groups durable references by workspace/session | Attaches uploads, artifacts, transcript projection, and replay to one path |
| Hosted runtimes | agentId + sessionId selects the runtime scope | threadId selects the active path and thread execution |
Thread History Projection
Threads load as projections from durable thread events. The thread event log is the ordered record; the projected thread is the current messages, thread state, and metadata rebuilt from that record.
Thread-history compaction is one canonical lifecycle. Every compaction appends a checkpoint to the append-only journal, which preserves the source events; the history projection applies the checkpoint. Replacement messages may contain a summary, or removal compaction may contribute no replacement.
Fork compaction is especially important for nested work. A subagent or multi-agent node can fork from a rich parent/root thread while starting from a smaller model-visible history. That lets a child or node inherit the useful context without copying every durable message into its prompt.
Events
Events are the framework vocabulary for input, streaming output, lifecycle, tool calls, interactive middleware, retry, background work, and thread-run tracking.
Thread-scoped projectable events have one canonical surface. The framework commits the event first, assigns its authoritative ThreadSequenceNumber, and publishes that exact value to subscribers and replay readers. Canonical scope and publication define durability for built-in and application-defined events.
Process-local events still exist for stateless output, diagnostics, struct-event samples, and runtime integrations that are intentionally outside thread history. A stateless AgentEvent has no replay guarantee and keeps ThreadSequenceNumber == 0.
Nested agents and workflows choose which thread owns their durable facts. Parent observation can still expose routed child activity, while child journals remain independently replayable according to conversation policy. Use event correlation fields for hierarchy and session/thread descriptors for durable lookup.
Runtime Scope Across Hosts
The same model applies across runtime surfaces:
- local agents use the same event vocabulary through typed subscriptions
- hosted APIs expose sessions, threads, thread executions, authoritative runtime state, inputs, resumable SSE, interruption, and the bidirectional response route
- TUI runtimes bind UI state to a runtime scope
- bot adapters map platform conversation identity to a session and thread before invoking an agent
- subagents create a child thread in the parent's session using the fresh or handoff context policy
- multi-agent workflows use conversation policy to decide where node-agent transcripts are written
This means UI and integration code should treat events as the common rendering and coordination language, while treating hosting routes and local subscriptions as transport choices.
Concurrency Model
Thread executions and thread operation locks are separate controls.
A thread execution is one accepted input owning the thread exclusively, through the session store's one execution controller. Only one active execution is allowed for a given sessionId + threadId; the route agentId selects the agent for that execution. A second simultaneous input submission to the same thread returns a conflict, while submissions to different threads can execute at the same time.
A thread operation lock protects exclusive thread mutations. Current hosting code uses the explicit thread operation lock for thread deletion. Some other thread operations use broader session-level coordination. Treat operation-lock conflicts as thread mutation conflicts, not as active agent runs.
Middleware responses are also separate from thread executions. A permission, continuation, clarification, or client-tool response is accepted only when the target thread runtime is active and a waiter can accept that response.
Shared thread policies need additional coordination. If two agents write to the same durable thread at once, their thread snapshots and thread middleware state can race. HPD serializes same-thread multi-agent node executions at the conversation route. Separate threads can still execute in parallel.