contextwindow

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package contextwindow reports the active claude session's context-window occupancy from its resolved transcript, so a consumer (the screen_snapshot handler, and any future push path) can surface a "context window used" gauge without each consumer re-parsing the JSONL.

It reuses internal/agentrun/jsonl to decode the per-turn usage blocks rather than re-implementing a JSONL scanner. The transcript path it consumes is already canonicalised and confined upstream by the sessions resolver; this package opens the path it is given and does not re-resolve, re-confine, or re-canonicalise it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Usage

type Usage struct {
	// UsedTokens is input+cache_read+cache_creation+output on the latest
	// assistant entry that carried a usage block — the current context size,
	// not a running total across turns. Zero when no such entry exists yet.
	UsedTokens int

	// WindowTokens is the context-window size resolved for the model that
	// produced UsedTokens — the window observed for that model when the caller
	// supplied one, and defaultWindowTokens otherwise — or 0 when UsedTokens
	// disproved whichever of the two was resolved. A used count above the
	// resolved window is proof it is wrong, and 0 is this package's "no
	// trustworthy window reading" report. UsedTokens stays meaningful in that
	// case; only the denominator is withheld.
	WindowTokens int
}

Usage is the current context-window occupancy derived from a transcript's latest usage-bearing assistant entry.

func Read

func Read(path string, windows map[string]int) (Usage, error)

Read scans the claude transcript at path and reports current context-window usage, sizing the window from windows when the transcript's latest usage-bearing entry names a model that map has an entry for.

The join (#2107)

windows maps a model id to the context window observed for it, as claude reported it on a `result` line — the transcript never carries a window, so the two halves arrive on different channels and this is where they meet. The key is the model on the SAME entry the used count came from, so the reported window belongs to the model that produced the reading rather than to the largest, the first or the only model the session has ever used.

The match is EXACT and VERBATIM: no lowercasing, no date stripping, no alias expansion. Claude keys its map by whatever string each caller used, and no rule relating two spellings survives the observed data — the same model appears under a dated and an undated id at one window, while a genuinely different model appears at another. A model the transcript names that windows has no entry for is a MISS, and a miss falls back to defaultWindowTokens, which is the pre-#2107 reading exactly.

ONE SPELLING IS TOLERATED, AND ONLY AFTER THE EXACT MATCH HAS MISSED (#2118): a key that is the transcript's id followed by exactly one trailing variant group. The two channels genuinely disagree on this one axis, measured end to end on 2026-09-05 — claude keys its result-line map "claude-opus-5[1m]" while every usage-bearing transcript entry of the same session writes "claude-opus-5" — so the exact match alone could never fire on a 1M session, and the disproved default then collapsed the reading to 0. That is a BLANK gauge on precisely the session shape this family exists to serve. The rule above is otherwise unchanged and its evidence still forbids everything else: a date suffix, a case change and an alias are all still misses.

AMBIGUITY REFUSES RATHER THAN GUESSES: two distinct suffixed keys sharing one base is a miss. Beyond being the safe answer it is the only DETERMINISTIC one — Go randomises map iteration, so "report the first match" would answer differently between two runs on identical input. An exact key always wins outright, so a variant key can never displace one that is present.

The tolerance is ONE-DIRECTIONAL: a transcript id carrying a group is not stripped to match an unsuffixed key. The measured divergence runs one way only, and a rule for a direction nobody has observed is a defense for a failure that has not happened.

AN EMPTY MODEL ID IS A MISS ON EITHER SIDE, and one guard closes both: an assistant entry with no message.model decodes to "", so this never looks up "" at all — which also makes a windows entry keyed "" unreachable. That entry is not hypothetical; internal/streamsup keeps a modelUsage entry keyed by the empty string when its window is positive, and it sorts first. An exact match without this rule would pair two independent empties into a confident wrong answer.

A non-positive observed value is not a window and is treated as absent. The producer never emits one, but windows is caller-supplied on an exported signature and this contract is defended here rather than assumed upstream.

windows is READ ONLY: Read neither mutates nor retains it, so a caller may build one per call and reuse or discard it freely. A nil map is legal and misses every lookup, which is exactly "nothing observed".

Nothing to report

Two "nothing to report" inputs return Usage{UsedTokens: 0, WindowTokens: defaultWindowTokens} with a nil error: an empty path (the resolver's "no transcript resolved yet" signal, which opens nothing) and a scanned transcript with no assistant/usage entry yet (a fresh, pre-first-turn session). This deterministic zero is kept distinct from an I/O failure: a non-empty path that cannot be opened, or a genuine read failure mid-scan, returns a wrapped error with a zero Usage, so a consumer can tell "fresh session" from "couldn't read".

A used count ABOVE the resolved window disproves it, and Read then reports WindowTokens 0 rather than a window its own data contradicts — the daemon can see the contradiction without knowing anything about models (#2100). THE CHECK RUNS AFTER THE JOIN, against the window actually reported: resolve first and a 223075-used session on an observed 1M window reads 22%; check first and the same session reads "no window". Do not reorder them. This is deliberately NOT an error: a wrong belief is a fact about the data, not a read failure, and routing it through the error path would collapse it back onto the disproved window. Equality is not a contradiction — a session exactly at its window is full, not evidence of a wrong belief — so it keeps its window.

Last-usage-wins is the whole of the compaction behaviour. After an auto-compaction, claude's next turn records a smaller input_tokens (the shrunk context); because Read always reports the latest usage-bearing entry (no max, no running total), a post-compaction read returns the smaller, current figure for free — no dedicated marker needed.

Safe for concurrent use: each call opens its own file and reader, neither of which is shared, and no state is hung off the package. windows is read but never written, so two concurrent calls over one map do not race.

Jump to

Keyboard shortcuts

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