# Sessions & Stores

`core/session` defines conversation identity (`session.Session`), context propagation (`session.NewContext` / `session.FromContext`), and the minimal two-method `session.Store` contract (`Load` and atomic `Append`). Keeping persistence intentionally narrow allows in-memory testing and durable SQL backends to share one contract without leaking database concepts into the core.

## The two-method Store contract

A `session.Store` has exactly two methods:

```go no-compile
type Store interface {
	Load(ctx context.Context, session Session) ([]llm.Message, error)
	Append(ctx context.Context, session Session, messages ...llm.Message) error
}
```

`Append` must be **atomic**: either every passed message is persisted or none is. During each step, the runner persists the assistant message (including any tool calls) together with all corresponding tool results in a single `Append` call. A crash mid-turn therefore cannot leave a stored transcript with unanswered tool calls.

Durability features beyond transcript storage—such as checkpoints, leases, search indexes, or conversation titles—belong in store modules or your application layer rather than `core/session`.

## Session identity and tenant isolation

`session.Session` pairs a conversation `ID` with an owner or tenant `ResourceID`:

```go no-compile
type Session struct {
	ID         string `json:"id"`
	ResourceID string `json:"resource_id,omitempty"`
}
```

Even when a `Store` is configured once on `agent.Config.Store`, the `Session` identity is always supplied **per run** (`agent.WithSession(id, resourceID)` or `RunOptions.Session`). If a store is configured without a session, the runner fails fast with `agent.ErrSessionRequired`; an empty `ID` returns `agent.ErrSessionIDRequired`.

Stores must scope queries by both `ID` and `ResourceID` so two tenants that happen to use the same conversation `ID` never see each other's messages. During execution, the runner also attaches the active `Session` to `ctx` via `session.NewContext`, allowing tools and `HeaderFunc` callbacks to read it with `session.FromContext(ctx)`.

## Ephemeral vs. durable stores

Use `session.NewMemory()` for unit tests and ephemeral processes, or connect `stores/sqlite` / `stores/postgres` when transcripts must survive restarts:

```go
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/ijaidev/gojent/core/agent"
	"github.com/ijaidev/gojent/core/session"
	"github.com/ijaidev/gojent/providers/mockllm"
)

func main() {
	ctx := context.Background()
	store := session.NewMemory()
	defer func() { _ = store.Close() }()

	assistant, err := agent.New(agent.Config{
		Name:  "assistant",
		Model: mockllm.New("First reply.", "Follow-up reply."),
		Store: store,
	})
	if err != nil {
		log.Fatal(err)
	}

	_, err = assistant.Generate(ctx, "Hello", agent.WithSession("thread-1", "tenant-a"))
	if err != nil {
		log.Fatal(err)
	}
	resp, err := assistant.Generate(ctx, "Continue", agent.WithSession("thread-1", "tenant-a"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp.Text)
}
```

See [Session Stores](/docs/modules/stores) for `stores/sqlite` and `stores/postgres` configuration, or [pkg.go.dev](https://pkg.go.dev/github.com/ijaidev/gojent/core/session) for full API details.
