Files
donlucasandClaude Fable 5.1 9189a4a2d1 The spend counter: a number top-right in every scape, summed off the transcript
His ask: "can we add the token spent counter? Maybe it can be a simple
number on the top right of the xscape? should we do it xxxx/1M? can xscapes
automatically detect the max context window?"

The figure comes off the agent's transcript (internal/spend): every
assistant line carries the API's usage, a streamed message repeats it once
per content block (six times, measured), so a response counts once per
message id; subagents' transcripts under the session's directory count too.
Claude Code's status payload carries only the WINDOW's tokens from the last
response and a dollar figure, per its docs, so the statusline event now
carries the window size alone (parked) and the hook forwards the
transcript path. The frame loop polls the tally every two seconds, bytes
past the last offset only. This session's own transcript: 22.77M tokens
over 79 responses, cache reads 97% of it, which is why "/1M" would mislead:
the spend passes the window many times over, and the window's fill is the
moon's channel already. No denominator.

drawTokens, both scapes: row one, one cell in from the right, the balloon's
darkened ground, the readout's dim ink; "23M tokens" from 60 columns, the
bare number from 40, nothing under the shore's floor (the star-touch sweep
at 30x8 went 54 -> 56 with even four cells reserved). Placed stars keep off
its cells: the shore re-throws only the darts that land there, so every
other star stays where it was (the golden constellation holds); the vista
moves such a star one row down. A falling star shows through it: over the
arrival sweep 2,232 of 26,694 flights (8.4%) cross the counter's cells on
row 1, and blocking them cost his flight floors (88.6 -> 84.3%), so the
label yields a cell to a star on it for the frame or two it is there.

Hidden under the vista's floor, where the owl's box holds the corner.

Tests: TestFormatTokens (four characters at every magnitude; 999,999 is
"1.0M"), TestNoPlacedStarSitsUnderTheSpendCounter,
TestNoVistaStarSitsUnderTheSpendCounter,
TestTheSpendCounterSitsTopRightInEveryScape (both scapes, four sizes, four
hours, at least 100 luma over the ground), TestTheTallyCountsEachResponseOnce,
TestTheStatuslineCarriesTheWindow. frames.compose is shared by the live
frame and the tests.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-16 16:24:57 -07:00

73 lines
3.1 KiB
Go

// Package scape holds the scenes. A scape reads only an Activity — it never
// knows which agent is running or what a "tool call" is. That separation is
// what lets one renderer serve every adapter.
package scape
import "github.com/donlucasx/xscapes/internal/canvas"
// Activity is the agent's state, normalised. Level is 0 (idle) to 1 (flat out).
type Activity struct {
Working bool
Level float64
// ContextUsed is 0 for a fresh session and 1 when the window is full.
// Deliberately phrased as "used" rather than "left" so the zero value --
// what you get if nobody sets it -- means a fresh session, not an
// exhausted one.
ContextUsed float64
// Tokens is the session's running spend, every token the agent's
// transcript records as processed (fresh input, cache writes, cache
// reads and output), and Window the model's context window as the
// agent reports it; zero means unknown and nothing is drawn. The spend is a total with no
// ceiling: it passes the window many times over in a long session, so
// the window is NOT its denominator (his question of 2026-09-16,
// "xxxx/1M?"); the window's fill is the moon's channel already.
Tokens int64
Window int
// TimeOfDay runs 0 at midnight, .25 dawn, .5 noon, .75 dusk. Midnight is
// the zero value on purpose: it is the look the scape was designed around,
// so forgetting to set this yields the good palette rather than a broken one.
TimeOfDay float64
// TodoDone and TodoTotal are the agent's checklist. Zero total means there
// is no list, which is not the same as a list with nothing done -- the sky
// shows nothing in the first case and an unlit constellation in the second.
TodoDone, TodoTotal int
// Arriving says the newest finished todo's star is still in flight: it has
// not reached its place yet, so the sky draws one fewer settled star and
// the missing one is falling into it. See arrival.go.
//
// ⚠ IT IS A SEPARATE BOOL ON PURPOSE, and ArrivalPhase must never be read
// without it. A bare phase would make the zero value -- what every caller
// that has never heard of this feature passes -- mean "launching right
// now", so every pinned frame in the repo would fire a fall at once.
Arriving bool
// ArrivalPhase is 0 at launch and 1 the instant the star lands, and it is
// meaningless unless Arriving is set. The reducer drives it from WALL-CLOCK
// age rather than integrating dt: Shore.Update clamps a frame gap over a
// second, so an integrated fall would freeze mid-air on a stalled render
// and a suspended laptop would wake with the star still falling.
ArrivalPhase float64
}
type Scape interface {
Name() string
Update(c *canvas.Canvas, t float64, act Activity)
}
// Hash2 is a cheap deterministic hash. Scene detail placed through it is stable
// for a given seed, so the same repo always gets the same shoreline.
func Hash2(x, y int, seed int64) uint32 {
h := uint32(int64(x)*374761393 + int64(y)*668265263 + seed*2654435761)
h ^= h >> 13
h *= 1274126177
return h ^ (h >> 16)
}
func HashF(x, y int, seed int64) float64 {
return float64(Hash2(x, y, seed)) / 4294967296.0
}