Documentation
¶
Overview ¶
Package session builds hook JSON responses and manages session token tracking.
Index ¶
- Constants
- func ClaudeSettingsHas1M() bool
- func EffectiveContextWindow(model string) int
- func FindJSONLPath(sessionID string) (string, error)
- func FormatContext(event, context string) string
- func FormatTokenCount(tokens int) string
- func FormatWindowSize(size int) string
- func ModelContextWindow(model string) int
- func ParseLastUsageAndModel(path string) (entity.TokenInfo, error)
- func ReadInput(r io.Reader) entity.HookInput
- func ReadSessionID(stdin *os.File) string
- func ReadSessionTokenInfo(sessionID string) (entity.TokenInfo, error)
- func WriteSessionStats(sessionID string, stats entity.Stats)
Constants ¶
const ContextWindow1M = 1_000_000
ContextWindow1M is the context window size for 1M-capable models.
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 ¶
EffectiveContextWindow returns the context window size using a four-tier fallback:
- Explicit .ctxrc context_window (non-default value wins)
- Claude Code ~/.claude/settings.json model selection ([1m] suffix → 1M)
- JSONL model ID prefix (all claude-* → 200k)
- rc.ContextWindow() default (200k)
Parameters:
- model: Model ID string from JSONL (may be empty)
Returns:
- int: Effective context window size in tokens
func FindJSONLPath ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.