agent

package
v2.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package agent owns the v2.0 agent identity lifecycle: CreateAgent, MintAgentCredential, RotateAgentSecret, ListAgentsByOwner, GetAgent, SuspendAgent, ResumeAgent, RevokeAgent, and the agentBySubjectClaim resolver consulted by internal/as. Extracted from root service_agent.go in PR C of the 2026-06 architecture reorg.

Dependency direction: internal/agent is a leaf with respect to internal/as and internal/delegation. internal/as reaches back through the AgentLookup interface (set after construction so the wiring stays acyclic). The cache invalidation hook is a function-typed dependency so the agent package does not import internal/as.

Index

Constants

View Source
const ChainDepthCeiling = 3

ChainDepthCeiling is the v2.0 hard cap on the actor chain length. The spec explicitly forbids raising this above 3 in v2.0. Kept exported so root validation can refer to the same constant.

Variables

View Source
var ErrIdentityDisabled = errors.New("theauth: agent identity is not enabled in Config")

ErrIdentityDisabled is returned when RegisterAgent runs without Config.AgentIdentity.

Functions

This section is empty.

Types

type CacheInvalidator

type CacheInvalidator func(clientID string)

CacheInvalidator drops cached Argon2-verified entries for the supplied client_id. Used after the OAuthClient secret hash is rotated or after a failed insert so the next /oauth/token call sees the fresh state. Function-typed so the agent package never imports internal/as.

type ChainCacheInvalidator

type ChainCacheInvalidator func(agentID string)

ChainCacheInvalidator drops the per-agent introspection chain-walk cache entry for the supplied agent ID string. Called from changeAgentStatus on suspend/revoke so the next introspection re-walks the chain instead of serving a stale "active" result (perf re-audit 2026-06-21, item 3). Function-typed so the agent package never imports internal/as.

type Config

type Config struct {
	MaxChainDepth            int
	MaxDelegationDuration    time.Duration
	DefaultDelegatedTokenTTL time.Duration
	AgentSecretLength        int
}

Config bundles the validated agent-identity policy fields this package needs at lifecycle time. Mirrors the subset of root AgentConfig the agent service actually consults.

type RegisterInput

type RegisterInput struct {
	OwnerID     models.ULID
	Name        string
	Description string
	// Scope is the set of scopes the agent may ever act with.
	Scope []string
	// Resource, when set, also records a delegation grant from the owner to
	// the agent for Scope on that resource.
	Resource string
	// MaxDuration bounds a delegated token's life under the grant. Defaults to
	// the configured DefaultDelegatedTokenTTL.
	MaxDuration time.Duration
	// GrantExpiresAt optionally ends the grant.
	GrantExpiresAt *time.Time
}

RegisterInput describes an agent or MCP client to register under a human.

type Registration

type Registration struct {
	Agent  models.Agent
	Secret models.AgentSecret
	Grant  *models.DelegationGrant
}

Registration is the result of Register. Secret is shown once.

func Register

func Register(ctx context.Context, svc *Service, dsvc *delegation.Service, defaultTTL time.Duration, in RegisterInput) (Registration, error)

Register creates an agent owned by a user and, when Resource is set, the delegation grant that lets it exchange tokens for that user.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service holds the dependencies needed for agent lifecycle methods. Constructed once by root *theauth.TheAuth.New and reached via thin forwarders so the v2.0 public API surface keeps its exact signatures.

func New

func New(storage Storage, cfg *Config, em audit.Emitter, invalidate CacheInvalidator) *Service

New constructs an agent Service. cfg may be nil; in that case every public method returns the same "agent identity not configured" sentinel the legacy root code returned. em may be nil; the constructor swaps in audit.NoopEmitter. invalidate may be nil; the constructor swaps in a no-op so callers do not need to branch. invalidateChain may be nil; the constructor swaps in a no-op.

func (*Service) AgentByID

func (s *Service) AgentByID(ctx context.Context, id models.ULID) (*models.Agent, error)

AgentByID returns the persisted agent row. Exposed so handler-layer code can perform ownership checks (the legacy implementation reached through the root oauthStorage back-door; PR C removes that back-door in favour of this lookup). The storage error is returned unwrapped so callers can compare with models.ErrStorageNotFound.

func (*Service) AgentBySubjectClaim

func (s *Service) AgentBySubjectClaim(ctx context.Context, sub string) (*models.Agent, error)

AgentBySubjectClaim parses a sub or act.sub of the form "agent:<id>" and returns the corresponding agent row. Returns (nil, nil) when the claim does not name an agent so callers can branch cleanly. Satisfies internal/as.AgentLookup so the AS introspection / token-exchange paths can validate chain actors without importing this package directly.

func (*Service) CreateAgent

func (s *Service) CreateAgent(ctx context.Context, in models.CreateAgentInput) (agentRow models.Agent, agentSecret models.AgentSecret, err error)

CreateAgent persists a fresh agent owned by the supplied user or organization, mints a backing OAuthClient + client secret, records the secret on an agent_credentials row, and returns both the Agent record and the one-shot AgentSecret. Audit emission: agent.created (without secret material) plus agent_credential.minted.

func (*Service) GetAgent

func (s *Service) GetAgent(ctx context.Context, agentID models.ULID) (*models.Agent, error)

GetAgent returns the agent record by ID.

func (*Service) ListAgentsByOwner

func (s *Service) ListAgentsByOwner(ctx context.Context, owner models.AgentOwner) ([]models.Agent, error)

ListAgentsByOwner returns every agent the supplied owner has created.

func (*Service) MintAgentCredential

func (s *Service) MintAgentCredential(ctx context.Context, agentID models.ULID, kind string) (models.AgentSecret, error)

MintAgentCredential mints a fresh credential of the requested kind on an existing agent. Only kind = AgentCredentialKindSecret is implemented in phase 3; X.509 and JWK kinds return ErrNotImplemented so callers see a typed error rather than silent success. Audit emission: agent_credential.minted on success.

func (*Service) ResumeAgent

func (s *Service) ResumeAgent(ctx context.Context, agentID models.ULID) error

ResumeAgent flips status back to active. Only valid on a suspended agent; resuming a revoked agent returns ErrAgentInactive.

func (*Service) RevokeAgent

func (s *Service) RevokeAgent(ctx context.Context, agentID models.ULID, reason string) (err error)

RevokeAgent flips status to revoked. Terminal: a revoked agent cannot be resumed (operators create a fresh agent instead). Cascades to every token in flight via introspection on next refresh.

func (*Service) RotateAgentSecret

func (s *Service) RotateAgentSecret(ctx context.Context, agentID models.ULID) (models.AgentSecret, error)

RotateAgentSecret is an alias for MintAgentCredential(kind="secret") that also revokes every prior unrevoked credential. Use this on suspected compromise; use MintAgentCredential when overlapping live credentials are the desired migration shape.

func (*Service) SetChainCacheInvalidator

func (s *Service) SetChainCacheInvalidator(fn ChainCacheInvalidator)

SetChainCacheInvalidator wires the chain-walk cache invalidator. Must be called before any SuspendAgent / RevokeAgent call to ensure the AS chain cache does not serve stale results after a status change (perf re-audit 2026-06-21, item 3). Safe to call multiple times.

func (*Service) SetHooks

func (s *Service) SetHooks(h *obs.Hooks)

SetHooks installs the consumer-supplied observability bundle. Idempotent; safe to call before the first lifecycle method. Root *theauth.TheAuth calls this in New after the agent Service is constructed so the same bundle reaches every internal service.

func (*Service) SuspendAgent

func (s *Service) SuspendAgent(ctx context.Context, agentID models.ULID, reason string) (err error)

SuspendAgent flips status to suspended. Cascades: every existing access token mentioning this agent in its actor chain becomes inactive on the next introspection refresh.

type Storage

type Storage interface {
	InsertOAuthClient(ctx context.Context, c models.OAuthClient) (models.OAuthClient, error)
	OAuthClientByClientID(ctx context.Context, clientID string) (*models.OAuthClient, error)
	UpdateOAuthClient(ctx context.Context, c models.OAuthClient) (models.OAuthClient, error)
	DeleteOAuthClient(ctx context.Context, clientID string) error

	InsertAgent(ctx context.Context, a models.Agent) (models.Agent, error)
	AgentByID(ctx context.Context, id models.ULID) (*models.Agent, error)
	AgentsByOwner(ctx context.Context, owner models.AgentOwner) ([]models.Agent, error)
	UpdateAgentStatus(ctx context.Context, id models.ULID, status string, at time.Time) error

	InsertAgentCredential(ctx context.Context, c models.AgentCredential) error
	AgentCredentialsByAgentID(ctx context.Context, agentID models.ULID) ([]models.AgentCredential, error)
	RevokeAgentCredential(ctx context.Context, id models.ULID, at time.Time) error
}

Storage is the minimal persistence subset this package needs. Declared here (not imported from root) so internal/agent does not import github.com/glincker/theauth-go/v2.

Directories

Path Synopsis
Package handlers exposes the organization-scoped agent + delegation administration endpoints mounted under /admin/v1/organizations/{orgID}/agents and .../delegations.
Package handlers exposes the organization-scoped agent + delegation administration endpoints mounted under /admin/v1/organizations/{orgID}/agents and .../delegations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL