Files
donlucasandClaude Fable 5.1 9711a9f7b4 The agent's erase-displays are counted into the event log
The filter counts every erase it confined to the band or dropped; the
host reads the count and the scape writes it on the event log's stats line
beside the bus's dropped and bad counts, whenever any of them changes. The
next blank row is attributed to a clear from the log instead of inferred
from a screenshot (his ruling, 2026-09-18).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-18 13:22:32 -07:00

325 lines
12 KiB
Go

package main
import (
"math"
"time"
"github.com/donlucasx/xscapes/internal/canvas"
"github.com/donlucasx/xscapes/internal/companion"
"github.com/donlucasx/xscapes/internal/envx"
"github.com/donlucasx/xscapes/internal/event"
"github.com/donlucasx/xscapes/internal/notify"
"github.com/donlucasx/xscapes/internal/reduce"
"github.com/donlucasx/xscapes/internal/scape"
"github.com/donlucasx/xscapes/internal/scenes"
"github.com/donlucasx/xscapes/internal/spend"
"github.com/donlucasx/xscapes/internal/term"
)
// frames paints the scape one frame at a time.
//
// It exists because there are now two places that need a frame -- the pane
// that owns its whole window, and the band below a hosted agent -- and a
// scene assembled twice is a scene that drifts. Everything about what the
// scape looks like lives here; the callers only decide where to put it.
type frames struct {
// spend sums the tokens off the agent's transcript, polled every two
// seconds from state(); the counter in the top-right corner.
spend *spend.Tally
spendAt time.Time
c *canvas.Canvas
sh *scape.Shore
cat *companion.Cat
lay layout
ccw, chh int
mirror bool
profile term.Profile
seed int64
// Set once a session is found. Until then the demo cycle runs.
bus *event.Bus
red *reduce.Reducer
player *notify.Player
knocker notify.Knocker
ctxUsed, tod float64
// level pins the demo at one activity level when >= 0 (-live -level):
// the instrument for looking at the scape at full stretch with no
// agent in the loop (his A/B on the wind, 2026-09-18). -1 is the cycle.
level float64
start time.Time
// nextCompanionCheck paces refreshCompanion; see it for why this is not
// read every frame.
nextCompanionCheck time.Time
// lastFrame is when the previous frame was painted, and it exists only to
// give Approach a real dt. It cannot be derived from the frame rate: -fps
// is a flag, the two launchers disagree about it already (12 in inside.go,
// 20 in main.go), and a frame that arrives late after a resize or a
// compaction would otherwise advance the walk by the wrong amount. Zero on
// the first frame, which yields dt = 0 and no movement.
lastFrame time.Time
// The scape. sh is always built -- the shore is the default and the
// demo, the site and the mockups draw it -- and vista is set while the
// preference names it (refreshScape). ascii is kept so a switch can
// build the vista the same way the shore was.
vista *scenes.Vista
scapeName string
ascii bool
nextScapeCheck time.Time
// evlog is XSCAPES_EVENTLOG, the file every applied event is appended
// to (eventlog.go); evDropped/evBad are the bus counts last written.
evlog string
evDropped, evBad int64
// erases reports the agent's erase-displays the host confined, for the
// same log line as the bus counts; nil when nothing hosts an agent.
erases func() int64
evErased int64
}
// companionPoll is how often a running scape re-reads the saved companion.
// Half a second is under the threshold where a switch feels like it did not
// work, and it is 24x less file reading than doing it every frame.
const companionPoll = 500 * time.Millisecond
// refreshCompanion swaps the animal when the saved preference changes, so
// `xscapes companion cat` takes effect in a scape that is already running.
//
// It did not, and the CLI and the README both promised it did (2026-09-08, he
// switched to the cat and the crab stayed on the beach). companionPref() was
// read exactly once, inside newFrames, which runs at startup; even resize()
// recomputed the layout from the companion width it already had.
//
// The layout is recomputed here exactly as newFrames and resize do it. Today
// that is a no-op: both animals report the same Size(), because Size() returns
// the BOX and not the ink, and the cat's ink uses only nine of its twelve
// columns. It is done anyway because compose() takes the companion's width and
// places the companion's margin, the litter's room and the moon's column from
// it -- a companion with a different box would otherwise be drawn into the old
// one's hole, and nothing in the swap would say so.
func (f *frames) refreshCompanion(now time.Time) {
if now.Before(f.nextCompanionCheck) {
return
}
f.nextCompanionCheck = now.Add(companionPoll)
name := companionPref()
if name == f.cat.Name() {
return
}
c := companion.New(name)
c.FaceLeft(f.mirror)
f.cat = c
f.ccw, f.chh = c.Size()
f.lay = compose(f.c.W, f.ccw, f.mirror)
f.sh.MoonX = f.lay.MoonX
}
func newFrames(w, h int, seed int64, ascii, mirror bool, ctxUsed, tod float64) *frames {
c := canvas.New(w, h, canvas.AlphaFar, canvas.AlphaMid, canvas.AlphaNear)
sh := scape.NewShore(seed, ascii)
cat := companion.New(companionPref())
cat.FaceLeft(mirror)
ccw, chh := cat.Size()
lay := compose(w, ccw, mirror)
sh.MoonX = lay.MoonX
f := &frames{
c: c, sh: sh, cat: cat, lay: lay, ccw: ccw, chh: chh,
mirror: mirror, profile: term.DetectProfile(), seed: seed,
player: notify.New(), ctxUsed: ctxUsed, tod: tod, level: -1, start: time.Now(),
ascii: ascii, scapeName: ScapeShore,
evlog: logPath(envx.Lookup("EVENTLOG"), "events.jsonl"),
}
f.refreshScape(time.Time{})
return f
}
// refreshScape re-reads the scape preference on the companion's clock, so
// `xscapes scape vista` reaches a running scape without a restart -- the
// same defect the companion switch had until 2026-09-08, not repeated.
func (f *frames) refreshScape(now time.Time) {
if !now.IsZero() && now.Before(f.nextScapeCheck) {
return
}
f.nextScapeCheck = now.Add(companionPoll)
name := scapePref()
if name == f.scapeName && (name == ScapeShore || f.vista != nil) {
return
}
f.scapeName = name
if name == ScapeVista {
f.vista = scenes.NewVista(f.seed, f.ascii)
} else {
f.vista = nil
}
}
// resize refits the scene, and says whether the window got smaller -- the only
// case that needs the terminal cleared, since growing paints strictly more
// cells than the frame before it.
func (f *frames) resize(w, h int) (shrank bool) {
shrank = w < f.c.W || h < f.c.H
f.c.Resize(w, h)
f.lay = compose(w, f.ccw, f.mirror)
f.sh.MoonX = f.lay.MoonX
return shrank
}
func (f *frames) size() (w, h int) { return f.c.W, f.c.H }
// follow attaches the scene to a session's event bus.
func (f *frames) follow(bus *event.Bus, red *reduce.Reducer) { f.bus, f.red = bus, red }
func (f *frames) following() bool { return f.red != nil }
// state drains everything that has arrived since the last frame and returns
// what the scene should show now. Non-blocking: a frame must never wait on
// the bus.
func (f *frames) state(now time.Time, t float64) reduce.State {
if f.red == nil {
// The sky is the world: time of day is the wall clock whether or not
// there is a session to follow. Without this the demo ran at tod 0 --
// midnight, which is deliberately monochrome -- so every launch showed
// a black-and-white scene for the second or two before the agent
// announced itself, and then jumped to colour.
tod := f.tod
if tod == 0 {
tod = timeOfDay(now)
}
if f.level >= 0 {
return pinnedState(f.level, f.ctxUsed, tod)
}
return demoState(t, f.ctxUsed, tod)
}
// A reducer with no bus is fed by the launcher itself: the generic
// adapter (internal/watch) applies what it synthesised before each
// frame, so there is nothing here to drain.
for draining := f.bus != nil; draining; {
select {
case e := <-f.bus.C:
f.red.Apply(e, now)
if f.evlog != "" {
appendEventLog(f.evlog, e, now)
}
default:
draining = false
}
}
if f.evlog != "" && f.bus != nil {
dropped, bad := f.bus.Stats()
var erased int64
if f.erases != nil {
erased = f.erases()
}
if dropped != f.evDropped || bad != f.evBad || erased != f.evErased {
f.evDropped, f.evBad, f.evErased = dropped, bad, erased
appendEventLogStats(f.evlog, dropped, bad, erased, now)
}
}
st := f.red.State(now)
// The spend, summed off the transcript every two seconds (his ask of
// 2026-09-16, drawTokens). An adapter that sends a figure of its own
// wins; the Claude adapter sends none, its payload has no such total.
if now.Sub(f.spendAt) >= 2*time.Second {
if f.spend == nil {
f.spend = spend.New()
}
f.spend.Poll(st.Transcript)
f.spendAt = now
}
if st.Act.Tokens == 0 && f.spend != nil {
st.Act.Tokens = f.spend.Total()
}
// The moon, from the transcript, when no status line reported it. Kimi's
// wire records the context in use and the window (internal/spend/kimi.go);
// Claude Code's transcript does not, and its status line does.
if !st.ContextKnown && f.spend != nil {
if used, window, ok := f.spend.Context(); ok {
frac := float64(used) / float64(window)
st.Act.ContextUsed = math.Max(0, math.Min(1, frac))
st.Act.Window = window
}
}
// Re-render the sand to the columns this layout actually leaves it, so a
// narrow pane loses whole pieces of a line rather than getting a path
// chopped mid-word. The vista's band runs the whole width.
budget := f.lay.SandTo - f.lay.SandFrom
if f.vista != nil {
budget = f.c.W - 4
}
st.Tail = st.FitTail(now, budget)
// The sky is the world: time of day is the wall clock, never anything the
// agent did.
st.Act.TimeOfDay = timeOfDay(now)
if f.tod != 0 {
st.Act.TimeOfDay = f.tod
}
if f.ctxUsed != 0 {
st.Act.ContextUsed = f.ctxUsed
}
return st
}
// frame paints one frame and returns it as ANSI, rows joined by newlines.
//
// It also rings the audible half of the nudge, keyed off the bubble rather
// than the pose and edge-detected, so a sixty-second permission nag knocks
// once rather than once a minute. Only when following a real session: the
// demo cycles every state on a timer, and a scape nobody attached to has
// nothing to announce.
func (f *frames) frame(now time.Time) string {
f.refreshCompanion(now)
t := now.Sub(f.start).Seconds()
st := f.state(now, t)
if f.red != nil {
// The pose is consulted for one thing only: a finish while the
// companion is still worried is the third knock (his ruling of
// 2026-09-15). The ask and the edge detection stay keyed off the bubble.
if k, ring := f.knocker.Knock(st.Bubble, st.BubbleAsk, st.Pose == companion.Worried); ring {
f.player.Play(k)
}
}
// The approach. His idea, 2026-09-10: when the agent needs him, the
// companion should "walk up closer to the screen and get bigger" before it
// prompts -- so it is driven by the POSE, eased over real seconds, and it
// retreats through the same call the moment the ask is answered.
//
// It is NOT a delay in front of the notification. The knock above has
// already rung and the balloon goes up this same frame; the walk happens
// underneath it. His asks stay open a median 130 s (28 closed ask windows
// over 30 recordings), so there is no shortage of time to cross the sand.
//
// Only NeedsYou brings it forward. Done is a knock he can answer whenever
// he likes, and Worried persists for a median 795 s -- a companion parked
// at full size for thirteen minutes is not an approach, it is a new
// default size.
dt := 0.0
if !f.lastFrame.IsZero() {
dt = now.Sub(f.lastFrame).Seconds()
}
f.lastFrame = now
f.cat.Approach(dt, st.Pose == companion.NeedsYou)
f.refreshScape(now)
f.compose(st, t)
return f.c.Render(f.profile)
}
// compose paints one state at one time into f.c through the scape in use:
// the vista or the shore, each through its own composer. The live frame
// and the tests share it, so a test composes exactly what the loop draws.
func (f *frames) compose(st reduce.State, t float64) {
if f.vista != nil {
f.vista.OwlX = f.lay.CatX
f.vista.Update(f.c, t, st.Act)
drawVista(f.c, f.vista, f.lay, st, t)
return
}
f.sh.Update(f.c, t, st.Act)
top := f.c.H - 2 - f.chh
drawScene(f.c, f.sh, f.cat, f.lay, st, t, f.seed, top)
}