LAYER 03 · Trace the agent spine

Agent creation, ownership, and cancellation

An agent is not a while loop; it is a live object with identity, session, inbox, context, and quiescence

19 min3 source anchorsupstream@47f9438
create/resume → setup transaction → publish → dispose
The question this chapter resolves

Who may destroy an agent, and how does a half-failed creation roll back?

Build intuition

AgentHandle is like the key and lease contract for a vehicle. The registry can locate it, but only the owner holding the contract can terminate the lease and complete teardown.

MECHANISM

Mechanism

ctx.agents.create/resume returns an AgentHandle. setup builds agent.ctx before agent and session publication and may return a synchronous commit; failure rolls both identities back.

cancel requests convergence of current activity; whenIdle follows replacement work until true quiescence; dispose stops the loop, unregisters, removes the in-memory session, then unwinds local context.

Step 1 of 5

Allocate identity

AgentId and SessionId share one branded identity.

INVARIANTS

What extensions must preserve

  • Agent and session identities publish together or not at all
  • Cancellation cause is first-wins and never arms future work
  • Holding dispose authority is itself a capability
FAILURE MODES

The tempting wrong turns

  • ×Treating a bare Agent from ctx.agents.get() as disposal authority
  • ×Treating whenIdle as acknowledgement for one message
  • ×Removing provider services immediately after cancellation
VERIFY IN SOURCE

Do not trust the conclusion—verify it

These anchors are pinned to official deepseek-harness@47f9438; account for later breaking changes when reading current upstream.

KNOWLEDGE CHECK

Pause for ten seconds before revealing

How do Agent.cancel() and AgentHandle.dispose() differ?

Why the next chapter follows

An agent's durable identity depends on its session. Next we unpack SessionEvent as the system's source of truth.

Learn DeepSeek Harness

An independent learning project. Explanations derive from source; upstream remains authoritative.