Documentation
¶
Overview ¶
Package session orchestrates one conversation on top of store (persistence) and kernel (pure decisions). It owns no SQL and no protocol parsing.
Index ¶
- Constants
- Variables
- func SpanPayload(spans []kernel.Span) []map[string]any
- type CompactOptions
- type CompactResult
- type CompressArgs
- type CompressRange
- type CompressResult
- type DecompressResult
- type ExportResult
- type FoldRange
- type Options
- type Plan
- type Retrieval
- type Session
- func (s *Session) Compact(ctx context.Context, opts CompactOptions) (CompactResult, error)
- func (s *Session) Decompress(ctx context.Context, id string) (DecompressResult, error)
- func (s *Session) DeferNudge(ctx context.Context, p Plan) error
- func (s *Session) ExportBlock(ctx context.Context, blockID, dir string) (ExportResult, error)
- func (s *Session) HandleCompress(ctx context.Context, args CompressArgs) (CompressResult, error)
- func (s *Session) Ingest(ctx context.Context, msgs []kernel.Message) (int, error)
- func (s *Session) Plan(ctx context.Context, tokenCount int) (Plan, error)
- func (s *Session) Recall(ctx context.Context, ref string) (string, error)
- func (s *Session) Retrieve(ctx context.Context, target, rng string) (Retrieval, error)
- func (s *Session) Search(ctx context.Context, query string, limit int) ([]store.SearchHit, error)
- func (s *Session) SearchProject(ctx context.Context, projectID, query string, limit int) ([]store.SearchHit, error)
- func (s *Session) SetOptions(opts Options)
- func (s *Session) Settings() Options
Constants ¶
const ( DefaultSearchHits = 10 MaxSearchHits = 20 MaxSnippetRunes = 300 MaxRetrieveTokens = 32 << 10 )
Limits of the read-side tools search_context and retrieve (spec §13.1, §16). They sit here, below the proxy, so that no caller can let one tool result fill the window.
const MaxExportBytes = 8 << 20
MaxExportBytes caps the text one export writes. A longer block is cut at a rune boundary and reported as truncated.
const RecentKeep = recentKeep
RecentKeep exports recentKeep so the proxy's nudge lists the same spans that HandleCompress accepts.
Variables ¶
var ( MinCompressRange = defaults.MinCompressRange MinSummaryChars = defaults.MinSummaryChars )
MinCompressRange and MinSummaryChars are the §10.2 minimums that HandleCompress enforces. The proxy must filter the spans it advertises (kernel.CompressibleSpans has no token floor) by MinCompressRange, or it offers ranges that compress then rejects.
var ErrNothingToRetrieve = errors.New("retrieve: no message in the requested span")
ErrNothingToRetrieve reports a retrieve whose target has no message in its span.
var ErrNudgeStale = errors.New("nudge undo: trigger state changed after plan")
ErrNudgeStale reports that the trigger state moved after the Plan, so a deferral cannot be undone without also discarding that later change.
Functions ¶
Types ¶
type CompactOptions ¶
type CompactOptions struct {
// MinTokens is the smallest span offered. Zero takes MinCompressRange, the
// smallest range HandleCompress accepts, so every offered span can be compressed.
MinTokens int
}
CompactOptions tunes a forced compaction.
type CompactResult ¶
type CompactResult struct {
Spans []kernel.Span // compressible ranges, ledger order
NudgePending bool // true when the next Plan nudges
}
CompactResult is what a forced compaction offers and whether it nudges.
type CompressArgs ¶
type CompressArgs struct {
Ranges []CompressRange
Folds []FoldRange
CloseIntents []int64 // open intents completed by the summarized ranges (§11.3)
}
CompressArgs is the validated-shape input of the compress tool.
type CompressRange ¶
CompressRange is one range of a compress tool call (spec §16), by message refs. It creates a T1 block.
type CompressResult ¶
CompressResult reports the blocks written and the token count after the fold.
type DecompressResult ¶
type DecompressResult struct {
Target string // the block that became inactive
Reactivated []string // its direct sources, visible again
Redirected bool // the requested block was folded; its active ancestor was expanded
}
DecompressResult reports what a decompress call expanded.
type ExportResult ¶
type ExportResult struct {
BlockID string
Path string
Messages int // messages in the block's range
Bytes int // bytes written
Truncated bool // the text was cut at MaxExportBytes
}
ExportResult describes the file written by ExportBlock.
type FoldRange ¶
FoldRange is a T2 or T3 fold (spec §12), by block ids. StartBlock and EndBlock name the first and last of a contiguous run of active blocks one tier below Tier. Every active block of that tier between them is consumed.
type Options ¶
type Options struct {
Kernel kernel.Config // trigger thresholds (§8)
MinCompressRange int // smallest range or fold in tokens (§10.2)
MinSummaryChars int // shortest accepted summary in characters (§10.2)
RecentKeep int // trailing messages no compress call may touch (§10.2)
FramingCheck bool // reject imperative-mood summaries (§11.2, opt-in)
}
Options are the per-session settings that the configuration supplies. A zero field takes its default; a zero Kernel (StepBase 0) means kernel.DefaultConfig.
type Plan ¶
type Plan struct {
Decision kernel.Decision
LowWater int
Visible []kernel.Message
Hints []kernel.RecallHint
// Before is the trigger state before this Plan, and After the state it wrote.
// DeferNudge uses them to undo the change without losing a later one.
Before kernel.TriggerState
After kernel.TriggerState
}
Plan is the per-turn result: the trigger decision, the visible window and the recall hints for this turn (spec §13.2). Hints are ephemeral: they are returned for the proxy to inject, and also logged as 'recall_hint' events.
type Retrieval ¶
type Retrieval struct {
Target string // the block id or message ref asked for
Messages int // messages in the returned text
Text string // one "[ref] text" entry per message, capped at MaxRetrieveTokens
Truncated bool // the text was cut to fit the cap
}
Retrieval is the original text returned by retrieve.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session binds one store session id to the orchestration steps.
func New ¶
New wraps an existing store session with the spec defaults. The caller creates the session row (store.CreateSession) before calling New.
func NewWithOptions ¶
NewWithOptions wraps an existing store session with opts (zero fields take their defaults). See New for the session row.
func (*Session) Compact ¶
func (s *Session) Compact(ctx context.Context, opts CompactOptions) (CompactResult, error)
Compact offers the compressible spans of the session and, when there are any, arms a forced nudge so the next Plan asks the model to compress them (spec §19.4). Without spans nothing is armed: there is nothing to nudge about.
func (*Session) Decompress ¶
Decompress expands one level of folding (spec §13.1, mode "context"): the target block is deactivated and its direct sources are reactivated. Nothing is deleted, and the change commits only if the invariants hold.
func (*Session) DeferNudge ¶
DeferNudge undoes the trigger change of p for a nudge that the cache gate deferred (spec §9.3). The nudge never reached the client, so it counts as neither shown nor refused. The undo runs under the session lock. It fails with ErrNudgeStale when another change came after p; the caller then keeps the nudge.
func (*Session) ExportBlock ¶
ExportBlock writes the original text of every message covered by block blockID into dir (spec §13.1, decompress mode file) and returns the path. The file is named <blockID>-<hash of the session>.txt, is created owner-only (0600) through a temporary file and an atomic rename, and never follows a link already at the target. dir is created with 0700 when missing.
func (*Session) HandleCompress ¶
func (s *Session) HandleCompress(ctx context.Context, args CompressArgs) (CompressResult, error)
HandleCompress validates every range and fold against the current snapshot and then writes all of them in one store transaction. Invalid input writes nothing. Folds are checked by kernel.ApplyTierPlan, so the same invariants (I1, I2, I5, I6) guard the in-memory plan and the stored one.
A call that is refused still answers the nudge: the model tried. It clears the pending nudge and the refusal count, so a run of rejected ranges (for example while a barrier made every range invalid) does not suspend nudges as if the model had ignored them (spec §8.3 refusal row).
func (*Session) Ingest ¶
Ingest appends the messages that the store does not have yet. msgs is the full client transcript: the first len(stored) entries must match the stored ledger (role, kind, tool fields, text) or Ingest fails without writing. Returns the number of newly appended messages.
func (*Session) Plan ¶
Plan updates the low-water mark for tokenCount, asks the kernel whether to nudge, persists the trigger state and renders the visible window.
func (*Session) Recall ¶
Recall returns the original content of the message identified by ref, including messages folded into a block (spec §13.1 retrieve, simplified).
func (*Session) Retrieve ¶
Retrieve returns the original messages behind target, which is a message ref (m00042) or a block id (b12). Messages folded into blocks are returned as they were recorded. rng, for example m00340..m00412, narrows a block to that span, and for a ref it selects the span instead. This session only.
func (*Session) Search ¶
Search runs search_context over this conversation. At most MaxSearchHits hits come back, each snippet is at most MaxSnippetRunes runes, and an empty limit means DefaultSearchHits.
func (*Session) SearchProject ¶
func (s *Session) SearchProject(ctx context.Context, projectID, query string, limit int) ([]store.SearchHit, error)
SearchProject runs search_context over every conversation of projectID. The caller passes the project of the request, so a scope never crosses projects.
func (*Session) SetOptions ¶
SetOptions replaces the options of a live session. The proxy calls it with the configuration of the session's project, which may change between requests.