session

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package session builds hook JSON responses and manages session token tracking.

Index

Constants

View Source
const ContextWindow1M = 1_000_000

ContextWindow1M is the context window size for 1M-capable models.

View Source
const MaxTailBytes = 32768

MaxTailBytes is the maximum number of bytes to read from the end of a JSONL file when scanning for the last usage block.

Variables

This section is empty.

Functions

func ClaudeSettingsHas1M

func ClaudeSettingsHas1M() bool

ClaudeSettingsHas1M reads ~/.claude/settings.json and returns true if the selected model name contains "[1m]", indicating the user has opted into the 1M extended context window. Returns false on any error.

Returns:

  • bool: True if 1M context is enabled

func EffectiveContextWindow

func EffectiveContextWindow(model string) int

EffectiveContextWindow returns the context window size using a four-tier fallback:

  1. Explicit .ctxrc context_window (non-default value wins)
  2. Claude Code ~/.claude/settings.json model selection ([1m] suffix → 1M)
  3. JSONL model ID prefix (all claude-* → 200k)
  4. rc.ContextWindow() default (200k)

Parameters:

  • model: Model ID string from JSONL (may be empty)

Returns:

  • int: Effective context window size in tokens

func FindJSONLPath

func FindJSONLPath(sessionID string) (string, error)

FindJSONLPath locates the JSONL file for a session ID.

Uses glob: ~/.claude/projects/*/{sessionID}.jsonl Caches the result in StateDir()/jsonl-path-{sessionID} so the glob runs once per session.

Parameters:

  • sessionID: The Claude Code session ID

Returns:

  • string: Path to the JSONL file, or empty if not found
  • error: Non-nil only on unexpected errors

func FormatContext

func FormatContext(event, context string) string

FormatContext builds a JSON Response with additionalContext for the given hook event. This is the standard way for non-blocking hooks to inject directives that the agent will actually process (plain text gets ignored).

Parameters:

  • event: Hook event name
  • context: Additional context string

Returns:

  • string: JSON-encoded hook response

func FormatTokenCount

func FormatTokenCount(tokens int) string

FormatTokenCount formats a token count as a human-readable abbreviated string: "1.2k", "52k", "164k".

Parameters:

  • tokens: Token count to format

Returns:

  • string: Abbreviated token count

func FormatWindowSize

func FormatWindowSize(size int) string

FormatWindowSize formats the context window size as a human-readable abbreviated string for display in token usage lines: "200k", "128k".

Parameters:

  • size: Window size in tokens

Returns:

  • string: Abbreviated window size

func ModelContextWindow

func ModelContextWindow(model string) int

ModelContextWindow returns the context window size for a known model ID. Returns 0 if the model is not recognized, signaling callers to fall back to rc.ContextWindow() or the default.

Parameters:

  • model: Model ID string from the JSONL (e.g., "claude-opus-4-6-20260205")

Returns:

  • int: Context window size in tokens, or 0 if unknown

func ParseLastUsageAndModel

func ParseLastUsageAndModel(path string) (entity.TokenInfo, error)

ParseLastUsageAndModel reads the tail of a JSONL file and extracts the last assistant message's usage data and model ID.

Parameters:

  • path: Absolute path to the JSONL file

Returns:

  • SessionTokenInfo: Token count and model, or zero value if not found
  • error: Non-nil only on I/O errors

func ReadInput

func ReadInput(r io.Reader) entity.HookInput

ReadInput reads and parses the JSON hook input from r. Returns a zero-value HookInput on any error (graceful degradation).

Guards against blocking forever on stdin:

  • Terminal (character device): returns immediately
  • Pipe/file with no EOF within 2s: times out and returns zero value

Both cases are harmless - hooks degrade gracefully with zero input.

Parameters:

  • r: Reader to read hook input from

Returns:

  • entity.HookInput: Parsed input or zero value

func ReadSessionID

func ReadSessionID(stdin *os.File) string

ReadSessionID reads the session ID from stdin JSON, returning the fallback "unknown" if stdin is empty or unparseable.

Parameters:

  • stdin: File to read input from

Returns:

  • string: Session ID or config.IDSessionUnknown

func ReadSessionTokenInfo

func ReadSessionTokenInfo(sessionID string) (entity.TokenInfo, error)

ReadSessionTokenInfo finds the current session's JSONL file and returns the most recent total input token count and model ID from the last assistant message. Returns zero value if the file isn't found or has no usage data.

Parameters:

  • sessionID: The Claude Code session ID

Returns:

  • SessionTokenInfo: Token count and model from the last assistant message
  • error: Non-nil only on unexpected I/O errors

func WriteSessionStats

func WriteSessionStats(sessionID string, stats entity.Stats)

WriteSessionStats appends a JSONL line to .context/state/stats-{sessionID}.jsonl. The file is designed for `tail -f` monitoring of token usage across prompts. Best-effort: errors are silently ignored.

Parameters:

  • sessionID: Session identifier
  • stats: Stats entry to write

Types

This section is empty.

Jump to

Keyboard shortcuts

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