Skip to content

Application Composition ​

An agent application has one immutable event composition. The AgentRuntime selects it when it is built, and every agent the runtime runs uses it. It is the complete compile-time schema used for:

  • live event publication;
  • durable thread journals and replay;
  • event and input catalogs;
  • OpenAPI and generated client contracts;
  • Native AOT JSON metadata;
  • module ownership and durability classification.

The composition is not a runtime subscription list. It is the exact contract that determines which event types the application can publish, persist, replay, and expose.

Durability Is Explicit ​

Every event is either Durable or LiveOnly.

  • Durable means the event is part of the canonical thread journal, replay contract, and compatibility digest. Use durable events for thread state, conversation content, tool and operation facts, requests and responses, goals, subagent authority, and recovery decisions.
  • LiveOnly means the event may be observed through the runtime event coordinator but cannot enter a thread journal. Use it for diagnostics, progress, retries, snapshots, provider-call mechanics, and other information that can be re-derived or is only useful while the runtime is live.

Custom AgentEvent records are live-only unless annotated with [DurableEvent]:

csharp
using HPD.Agent;
using HPD.Agent.Serialization;

[DurableEvent]
public sealed record RetrievalCompletedEvent(
    string Query,
    int DocumentCount) : AgentEvent;

Compositions fail closed when a live-only event is submitted to a durable journal operation. They also fail when a session store and the selected composition own different codecs.

Host Durability Policy ​

An application may override generated event defaults while building its runtime. The resulting policy is immutable for that runtime, every agent it runs, and its session store:

csharp
await using var runtime = await new AgentRuntimeBuilder()
    .WithEventDurability<ContentReadEvent>(AgentEventDurability.Durable)
    .WithEventDurability<MyAuditEvent>(AgentEventDurability.LiveOnly)
    .AddAgent("assistant", assistant)
    .BuildAsync();

The string overload selects an event by stable wire discriminator:

csharp
.WithEventDurability("CONTENT_READ", AgentEventDurability.Durable)

Use WithEventDurabilityPolicy(...) when the host builds one reusable policy:

csharp
var policy = AgentEventDurabilityPolicy.Empty
    .Set<ContentReadEvent>(AgentEventDurability.Durable)
    .Set("STATE_SNAPSHOT", AgentEventDurability.Durable);

await using var runtime = await new AgentRuntimeBuilder()
    .WithEventDurabilityPolicy(policy)
    .AddAgent("assistant", assistant)
    .BuildAsync();

BuildAsync() applies the policy after selecting the generated application composition and before creating a runtime-owned session store. Therefore the store and every agent share the effective codec. Unknown event types or discriminators fail rather than being ignored.

Recovery-critical events marked [DurableEvent(Required = true)] cannot be demoted to live-only. Libraries can opt into this protection when journal admission is required for their subsystem to recover correctly.

Changing durability changes the composition's behavior digest, not its structural event catalog digest (AgentEventCatalog.Digest). A TUI or protocol client can continue decoding the same event schema while the host chooses where events are persisted.

Normal Applications Need No Setup ​

Executables are application compositions by default. A project can also opt in:

xml
<PropertyGroup>
  <HpdAgentApplication>true</HpdAgentApplication>
</PropertyGroup>

The source generator closes the application's event and input modules into one composition and registers it under the application assembly name.

An automatically selected application needs no builder call:

csharp
await using var runtime = await new AgentRuntimeBuilder()
    .AddAgent("assistant", assistant)
    .BuildAsync();

If exactly one generated application composition is present, AgentRuntimeBuilder selects it.

Libraries Contribute Modules ​

A library normally generates an event module rather than a complete application:

xml
<PropertyGroup>
  <HpdAgentApplication>false</HpdAgentApplication>
</PropertyGroup>

Libraries still define and generate their concrete AgentEvent records. When no complete application composition exists, AgentRuntimeBuilder composes the generated modules loaded by the process.

An application composition includes its referenced library modules. A module that is not part of the selected composition is not a valid runtime or persistence contract.

Selection Order ​

AgentRuntimeBuilder.BuildAsync() resolves the output event composition in this order:

  1. An explicit WithEventComposition(...).
  2. The exact identity passed to WithEventApplicationIdentity(...).
  3. The registered application whose codec is owned by the configured session store.
  4. The process default application.
  5. The sole registered application composition.
  6. The combined generated library modules when no application composition is available.

If none is available, BuildAsync() fails with an actionable error. If several applications are registered and no rule selects one, it also fails rather than guessing.

Multiple Applications In One Process ​

Use WithEventApplicationIdentity(...) only when more than one complete generated application composition is loaded in the same process:

csharp
await using var runtime = await new AgentRuntimeBuilder()
    .WithEventApplicationIdentity(typeof(Contoso.WorkerApp).Assembly.GetName().Name!)
    .AddAgent("assistant", assistant)
    .BuildAsync();

The value is the generated application assembly identity. It is not an agent ID, tenant ID, model ID, or arbitrary display name. If no composition is registered under that identity, BuildAsync() fails.

The session store and selected composition must share the exact event codec. Changing compositions can make an existing journal incompatible even when both compositions contain events with the same names.

Explicit Composition ​

Use WithEventComposition(...) when the host intentionally owns a closed schema, such as a core-only surface or an application-defined composition:

csharp
var composition = AgentEventComposition.Create([...]);

await using var runtime = await new AgentRuntimeBuilder()
    .WithEventComposition(composition)
    .AddAgent("assistant", assistant)
    .BuildAsync();

The runtime creates its default in-memory session store, or the store of WithFileSessionStore(...) and WithInMemorySessionStore(), with the selected composition's codec. If you supply a custom store with WithSessionStore(...), BuildAsync() validates that it uses the same codec instance.

Reading The Composition ​

The runtime reports what it selected through runtime.Composition:

csharp
AgentEventCatalog events = runtime.Composition.Events;
AgentInputCatalog inputs = runtime.Composition.Inputs;

Console.WriteLine($"{events.Events.Count} events, digest {events.Digest}");

Each AgentEventCatalogEntry carries the discriminator, contract name, effective durability, owning module, and contract digest. The catalogs are captured by BuildAsync(); reading them builds no agent. Hosted runtimes serve the same catalogs at GET /composition/events and GET /composition/inputs.

The input catalog comes from the application's generated input composition. Select another one with AgentRuntimeBuilder.WithInputComposition(...).

Serialization ​

Serialize output events through the selected composition codec, which the runtime's session store owns:

csharp
var codec = runtime.SessionStore.EventCodec;
var json = codec.Serialize(new TextDeltaEvent("hello", "message-1"));
var evt = codec.DeserializeEvent(json);

Semantic inputs use the application's input composition through AgentInputCodec. Hosted endpoints normally perform this work for you.

Do not introduce a process-global event registry or register event metadata at runtime. The selected generated composition is the only event serialization authority.

FFI ​

HPD-Agent.FFI is itself generated as an application composition. Its native create_agent_with_ToolHarnesses entry point selects that composition internally. Native callers do not call WithEventApplicationIdentity(...).

Built for production .NET agent applications.