Skip to content

Binding architecture

The AgentClaim controller keeps each AgentClaim bound to a kagent Session. This document describes how.

flowchart LR
CLAIM[AgentClaim] -->|watch| CONTROLLER[AgentClaim controller]
AGENT[kagent Agent] -->|watch| CONTROLLER
CONTROLLER -->|create, get, revert, delete| SESSIONS[kagent Session API]
CONTROLLER -->|status, events| CLAIM
SESSIONS --> RUNTIME[Session runtime on Agent Substrate]

The controller reads three things:

  • AgentClaims, through a watch. A claim is reconciled when its spec, finalizers or deletion timestamp change. Status writes, which the controller makes itself, do not trigger a reconcile.
  • kagent Agents, through a watch. A change to an Agent reconciles every claim that references it, which is how a claim learns that its Agent exists, is ready, or has compiled another revision.
  • kagent Sessions, by reading. kagent offers no way to watch Sessions, so each bound claim reads its Session again every --session-resync-period, one minute by default.

Every Session belongs to the identity that created it, and kagent lets only that identity send it work. The controller creates every Session under one identity, described in the installation guide. Anything that sends work to a seat uses the same identity.

kagent’s CreateSession is idempotent on a request ID: repeating a request returns the Session the first call created. A request ID stays reserved after its Session is deleted.

The controller numbers each claim’s requests. Request n of a claim has the ID <claim UID>-<n>, and status.bindings holds the number of the latest request. The controller always writes the number to the claim’s status before it sends the request:

  1. Increment status.bindings and clear status.session. Write the status.
  2. Call CreateSession with the request ID for that number.
  3. Record the Session in status.session. Write the status.

A controller that stops between steps 2 and 3 repeats step 2 on its next pass and receives the Session that already exists. No Session is created without a record that leads back to it, and a request number is never reused for a second Session.

Status writes use optimistic concurrency. A pass that read a stale claim fails at step 1 and starts again from current state.

When kagent answers that a request cannot produce a Session, because its Session was deleted or its ID names another Session, the controller moves to the next number.

On each pass the controller reads the claim’s Session and acts on its state.

Session Action
Ready, or suspended Record it as bound. kagent resumes a suspended runtime on the next interaction. Then compare revisions, as described below.
Creating, or a creation that failed part way Repeat the same request, which drives the creation forward.
Crashed Call RevertSession. On success the claim keeps the Session. If kagent answers that the runtime is beyond recovery, delete the Session and request another.
Failed for any other reason Delete the Session and request another.
Deleted, deleting, or not found Request another.

A Session whose Agent was deleted and created again belongs to an Agent that is gone. The controller compares the Agent’s UID with the UID recorded when the Session was requested, deletes the Session on a mismatch, and requests another.

While the referenced Agent does not exist, the controller leaves an existing Session alone and reports the claim as Pending.

A Session pins the Agent revision that was latest when it was created. The controller compares that revision with the Agent’s latestSuccessfulRevision.

When they differ and the claim’s policy is Always, the controller waits for any admitted Assignment to finish. Admission and the start of a revision replacement both update the claim using its resource version. The controller marks the binding pending before requesting deletion without force. If kagent still has an active turn, the controller restores the claim’s bound state and retries every thirty seconds. After deletion it requests a Session that pins the latest revision.

With OnLoss the controller reports the difference and leaves the Session in place.

When an Assignment reaches a terminal phase and the claim’s resetPolicy requires a clean Session (AfterAssignment, or OnFailure for a non-succeeded Assignment), the Assignment reconciler clears status.activeAssignment and records the Session ID in status.resetSessionID in the same status update. Admission and idle-seat selection skip claims whose status.resetSessionID matches the bound Session. The AgentClaim controller detects the release, deletes the Session without force once idle, clears status.resetSessionID, and binds a fresh Session.

A claim carries the kslip.dev/session finalizer from its first reconcile. When the claim is deleted, the controller deletes its Session with force and then removes the finalizer.

If the claim’s latest request is unconfirmed, the controller first repeats it to learn whether a Session exists, and deletes the one it finds.

kagent reports the outcome of a call as a gRPC status.

Status Meaning to the controller
NOT_FOUND The Session does not exist.
ABORTED Work in the Session conflicts with the call. Retry after a short wait.
FAILED_PRECONDITION The call can never succeed as made: a spent request, or a runtime beyond recovery.
ALREADY_EXISTS The request ID names another Session.
PERMISSION_DENIED, UNAUTHENTICATED kagent rejected kslip’s credentials. Report KagentAccessDenied and retry.
Any other status Retry the same call. Its effect may already have happened, which the request numbering makes safe.

A failed pass is retried with exponential backoff, from one second up to one minute. Each call to kagent is bounded by a thirty-second timeout.

A claim that holds a Session keeps its conditions through a failure that says nothing about the Session, so a kagent outage does not mark every seat as unbound.