session

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package session implements the session-prefix construction and canonical-hashing described in ARCHITECTURE § 5.1 and § 5.2.

Three protocols, one algorithm — the parser is dispatch-free in the algorithm sense; only the *prefix construction* is per-protocol.

Index

Constants

View Source
const HashLen = 16

HashLen is the byte length of canonical hashes used for the SQLite prefix_canonical_hash column and the IN-clause lookup.

View Source
const VirtualSystemRole = "__apilog_system__"

VirtualSystemRole is the reserved role name used to fold non-messages session state (Anthropic `system`, OpenAI Responses `instructions`) into the prefix as a virtual turn 0.

The leading double-underscore prefix + apilog namespace makes it safe against client-supplied real role values: Anthropic / OpenAI reject unknown roles at the API boundary, so a real client cannot produce a turn with this role on the wire.

Variables

This section is empty.

Functions

func Build

func Build(path string, reqBody json.RawMessage) (turns []json.RawMessage, ok bool)

Build returns the session-prefix turns array for one trace, per ARCHITECTURE § 5.1's per-protocol table.

path is the request path (e.g. "/v1/messages"). reqBody is the parsed JSON object from trace.Req.Body (json.RawMessage). If reqBody is nil or empty, or the path is not one of the three supported session-bearing protocols, Build returns (nil, false) — the trace has no session concept and the writer treats it as a self-root.

func HashHex

func HashHex(prefix []json.RawMessage) string

HashHex computes the canonical hash of a session prefix. canonicalize(x) = json.Marshal with sorted keys, no whitespace. Returns lowercase hex truncated to HashLen chars.

func StrictPrefixHashes

func StrictPrefixHashes(turns []json.RawMessage) []string

StrictPrefixHashes returns the hashes of all strict prefixes of `turns`, ordered from longest (len(turns)-1) down to shortest (1).

Used by the IN-clause session lookup in ARCHITECTURE § 5.2: each hash is a "could this be my parent's full prefix?" question. The first match SQL returns (ORDER BY prefix_len DESC) is our parent.

Returns empty slice when len(turns) <= 1 (no strict prefix exists).

Types

This section is empty.

Jump to

Keyboard shortcuts

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