claudesession

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package claudesession implements agent.Adapter for Claude Code with deterministic session-id reuse (anthropic/claude-code-session). It is the PersistentSession counterpart to agent/claude (which is the fresh-per-launch, gate-safe adapter).

Design (M2 + the 2026-06-26 native-config-isolation revision):

  • Session UUID is deterministic: sha256(runID|epoch|nodePath), first 16 bytes formatted as a UUID string. Same inputs → same UUID; different nodePath → different UUID. It is passed to claude's --session-id / --resume.
  • Reuses the base claude.Adapter for all shared logic (env, backend, version, stream parsing). Launch appends --session-id <uuid> and drops --no-session-persistence so claude records the session journal.
  • Per-run isolation: the engine computes a per-run CLAUDE_CONFIG_DIR (<staging-root>/claude-session/<run-id>) and threads it via inv.SessionConfigDir; Launch sets it on the exec env so claude relocates its whole config tree per run. The engine captures/restores the <CLAUDE_CONFIG_DIR>/projects subtree as the SessionRef (no path derivation here — the adapter no longer computes a transcript path).
  • Capabilities(): NativeSchema:true, PersistentSession:true.
  • The adapter is a container-backed CLI adapter (NOT Containerless) — subtree capture requires a container filesystem.

Index

Constants

View Source
const AdapterRef = "anthropic/claude-code-session"

AdapterRef is the agent-runtime identifier this package's Adapter returns from Ref(). Constant so cli/agent_registry.go and unit tests can refer to it without typing the string literal.

Variables

View Source
var DefaultEnvAllowlist = []string{
	"ANTHROPIC_API_KEY",
	"ANTHROPIC_AUTH_TOKEN",
	"CLAUDE_CODE_OAUTH_TOKEN",
}

DefaultEnvAllowlist is the same credential set as agent/claude since claude-code-session launches the same `claude -p` binary with the same auth environment. Single source of truth for cli/agent_registry.go's adapterEnvAllowlists entry and the integ tests' skipIfNoAuthEnv helper.

Functions

This section is empty.

Types

type Adapter

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

Adapter is the agent.Adapter implementation for Claude Code with deterministic session-id reuse. It wraps a *claude.Adapter for shared functionality (env passthrough, backend wiring, version, stream parsing) and adds:

  • Capabilities().PersistentSession = true
  • --session-id <deterministic-uuid> injected into every launch command
  • --no-session-persistence is OMITTED (so the session journal is recorded under the per-run CLAUDE_CONFIG_DIR the engine injects via inv.SessionConfigDir)

The base *claude.Adapter is a named field (not embedded) used for delegation; Launch is overridden because the command construction differs (session-id flag, no --no-session-persistence). All other methods delegate to the base.

func New

func New(opts ...Option) (*Adapter, error)

New constructs an Adapter. The base *claude.Adapter is constructed with the same env and backend options so that all shared logic (Version, stream parsing, env redaction) is delegated there.

func (*Adapter) Capabilities

func (*Adapter) Capabilities() agent.Caps

Capabilities returns Caps{NativeSchema: true, PersistentSession: true}. Not Containerless — this adapter requires a container (like the base claude adapter). Gate independence is enforced by the engine's PR0a guard that rejects PersistentSession adapters in gate.evaluate.

func (*Adapter) Launch

func (a *Adapter) Launch(ctx context.Context, handle container.Handle, inv agent.AgentInvocation) (<-chan agent.AgentEvent, <-chan agent.AgentOutcome, error)

Launch runs `claude -p ... --session-id <uuid>` inside handle. It uses the same streaming Backend.Exec / io.Pipe / bufio.Scanner pattern as the base claude.Adapter.Launch (copied here because the command construction differs and the base launch function is not exported).

Key differences from base claude.Adapter.Launch:

  • --session-id <sessionUUID(inv)> is appended
  • --no-session-persistence is OMITTED (so Claude records the session)

func (*Adapter) PreflightResume

func (a *Adapter) PreflightResume(_ context.Context, _ agent.LiveResumePreflightRequest) error

PreflightResume is a no-op for now — session restore is driven by the engine's content-addressed SessionRef (transcript blob), not a live provider session, so there is nothing to preflight at this stage. Real preflight logic (if any) lands with the live-integration task (M2d).

func (*Adapter) Ref

func (*Adapter) Ref() string

Ref returns the agent-runtime identifier.

func (*Adapter) RequiredEnv

func (*Adapter) RequiredEnv() []string

RequiredEnv implements agent.CredentialNamer.

func (*Adapter) ValidateConfig

func (a *Adapter) ValidateConfig(with ir.RawConfig) error

ValidateConfig enforces the same with-schema as the claude adapter (it accepts the same keys), but ALLOWS session_id / continue / resume — those keys are managed internally by the adapter via the deterministic UUID, not by the workflow author. Unknown keys and missing prompt are still rejected.

The difference from the base adapter: session reuse keys are NOT rejected (since this adapter IS the session adapter). We therefore reimplement validation rather than delegating to avoid the ErrSessionReuseAttempted path in the base.

func (*Adapter) Version

func (a *Adapter) Version(ctx context.Context, handle container.Handle) (string, error)

Version delegates to the base claude adapter.

type ErrBareRequiresAPIKey

type ErrBareRequiresAPIKey struct {
	AvailableKeys []string
}

ErrBareRequiresAPIKey is returned by ValidateConfig when `with.bare` is true but neither ANTHROPIC_API_KEY nor ANTHROPIC_AUTH_TOKEN is present in the adapter's env allowlist. Mirrors the error from agent/claude for this adapter's Ref.

func (*ErrBareRequiresAPIKey) Error

func (e *ErrBareRequiresAPIKey) Error() string

type Option

type Option func(*Adapter)

Option configures the Adapter at construction time.

func WithBackend

func WithBackend(b container.Backend) Option

WithBackend supplies the container.Backend used for Version and Launch.

func WithEnv

func WithEnv(env map[string]string) Option

WithEnv supplies the env-var allowlist forwarded into each `claude -p` invocation. Same semantics as claude.WithEnv.

Jump to

Keyboard shortcuts

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