mirror of
https://github.com/donlucasx/xscapes.git
synced 2026-10-20 08:43:32 +02:00
The visual vocabulary was complete and driven entirely by hardcoded numbers.
This is the plumbing: a protocol, a Claude Code adapter, a reducer, and an
installer.
internal/event is the protocol -- one JSON object per line, per-session unix
datagram socket with a JSON-lines spool as the fallback. The scape never learns
what a hook is and the adapter never learns what a wave is, which is the point:
a second adapter should need nothing from the renderer but this file.
internal/reduce folds events into the Activity and companion state the renderer
already consumes. Every constant is written down with the claim about human
attention that justifies it, and the whole package takes an explicit clock so
the constants can be tuned against a recorded session rather than a guess.
Three things were verified against the Claude Code binary's own embedded
schema rather than guessed (notes/claude-hooks-verified.md):
- Subagent identity DOES arrive. SubagentStart/SubagentStop carry agent_id
and agent_type. RESUME item 3 is answered; the field is agent_type, not
subagent_type.
- The 60-second nag needs no timing heuristic. notification_type is a
required field and the nag has its own value, idle_prompt. The measured
log (2,220 Notifications at exactly 60s after Stop, 41.6% of all of them)
is what said a filter was needed; the schema says how to write it. The
allow list fails closed, so a notification type invented next year is
ignored rather than knocked.
- Context for the moon comes from the statusline, not a hook. No hook
carries it, and summing transcript usage against a hardcoded 200k window
is measurably wrong on this machine: 164,450 tokens is 82% of 200k and
16% of the 1M window actually in use.
The installer edits the file holding the secret-expansion guard, so it prints
a plan by default and only writes with --apply. It splices bytes instead of
re-marshalling, refuses to write unless every entry it did not author survives
unchanged, backs up, and renames atomically. Install then uninstall is
byte-for-byte identical on the real 60KB settings.json.
Also fixes a latent renderer bug that only appears once activity moves: the
wave clock scaled absolute elapsed time by level, so a change in activity
teleported the sea, worse the longer the session ran -- 447 of 1920 cells at
ten minutes in, against 44 at five seconds. Phase is integrated now. The
regression test uses the waterline rather than the glyph field, because the
first version measured glyphs and passed against the broken code.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AU2ieNpECMF7S4go2CwcDX
86 lines
2.7 KiB
Go
86 lines
2.7 KiB
Go
package event
|
|
|
|
import (
|
|
"net"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// writeDeadline bounds the socket attempt.
|
|
//
|
|
// A unix datagram send is not the "never blocks" call it is often assumed to
|
|
// be: when the receiver's buffer is full, Go's netpoller parks the write until
|
|
// the socket is writable. That is fine for a normal program and fatal here,
|
|
// because this code runs inside a hook on every tool call, and a scape that
|
|
// stopped draining would become a brake on the agent. The deadline turns the
|
|
// worst case from "as long as the reader is wedged" into two milliseconds,
|
|
// after which we take the file path instead.
|
|
const writeDeadline = 2 * time.Millisecond
|
|
|
|
// Emit sends one event, best effort, and tells you which path it took.
|
|
//
|
|
// The contract that matters to the caller is the one it does NOT express: this
|
|
// never blocks meaningfully, never panics, and its error is advisory. A hook
|
|
// must not fail the agent's turn because a decoration is not running.
|
|
func Emit(e Event) (viaSocket bool, err error) {
|
|
if e.TS == 0 {
|
|
e.TS = time.Now().UnixMilli()
|
|
}
|
|
line := Encode(e)
|
|
|
|
if sendSock(e.Session, line) == nil {
|
|
return true, nil
|
|
}
|
|
return false, appendSpool(e.Session, line)
|
|
}
|
|
|
|
// sendSock tries the live engine. Every failure mode here is a normal state of
|
|
// the world, not an anomaly: no engine has ever run (ENOENT), one ran and was
|
|
// killed (ECONNREFUSED on a stale inode), or one is alive but behind
|
|
// (deadline). All three mean the same thing to us -- use the file.
|
|
func sendSock(session string, line []byte) error {
|
|
p, err := SockPath(session)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
addr, err := net.ResolveUnixAddr("unixgram", p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c, err := net.DialUnix("unixgram", nil, addr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer c.Close()
|
|
if err := c.SetWriteDeadline(time.Now().Add(writeDeadline)); err != nil {
|
|
return err
|
|
}
|
|
_, err = c.Write(line)
|
|
return err
|
|
}
|
|
|
|
// appendSpool writes the line to the session's JSON-lines file.
|
|
//
|
|
// One write(2) of one line under O_APPEND is what keeps concurrent hooks from
|
|
// interleaving halves of two events. POSIX guarantees the offset moves to EOF
|
|
// atomically but not that the write itself is indivisible; in practice a
|
|
// sub-page append to a local file is, on both darwin and linux, and MaxLine
|
|
// keeps every line far under a page. The reader is built to survive the case
|
|
// anyway -- it holds a partial trailing line rather than parsing it.
|
|
func appendSpool(session string, line []byte) error {
|
|
if _, err := EnsureRunDir(); err != nil {
|
|
return err
|
|
}
|
|
p, err := SpoolPath(session)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
f, err := os.OpenFile(p, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
_, err = f.Write(append(line, '\n'))
|
|
return err
|
|
}
|