Documentation
¶
Overview ¶
Package event defines the single normalized event type that every part of Caprock consumes — the UI, stats, the loop detector, the orchestrator, and any future avatar skin. See .ai/02-architecture.md § Event model.
Index ¶
Constants ¶
const MaxAssistantText = 16000
MaxAssistantText caps the assistant prose kept per turn, in runes. See ingest.MaxAssistantText, which is this value.
const MaxToolField = 32 << 10
MaxToolField bounds what one tool field — a call's tool_input or its tool_response — keeps in the database, in bytes of JSON.
Every plane that stores a tool call goes through CapToolField: the transcript parser and the hook receiver both, so the row that wins the dedup race (the hook copy, nearly always) is held to the same rule as the one that loses it. Before this the hook copy was stored verbatim up to the 4 MiB body limit: on the owner's machine (2026-10-13) hook tool results were 1.1 GB of a 2.2 GB database, 3,808 rows over 64 KB held 790 MB of it, and the largest were base64 screenshots nobody reads back from the database.
What is kept is what the screens read: the structure, every short field (paths, commands, exit codes, flags, ids, `gitOperation`), and the first MaxToolField bytes of long text. The size the call really handed back is carried separately (Event.ToolBytes), so the tool-size figures still count the original.
Variables ¶
This section is empty.
Functions ¶
func CapToolField ¶ added in v0.113.0
func CapToolField(raw json.RawMessage) json.RawMessage
CapToolField returns raw bounded to MaxToolField. The value keeps its shape:
- a string longer than the budget is clipped on a rune boundary and ends with a marker naming what was dropped: `…[truncated 81234 bytes]`, plus `, 312 lines` when the dropped part held line breaks (ClipString);
- base64 content — an image block's `data`, Claude Code's `file.base64`, an MCP screenshot, a `data:` URI — is replaced by a marker naming its media type and decoded size, at any size: `[image image/png 512 KB not stored]`;
- objects keep every key, and their short values whole: fields are given the budget smallest first, so one huge field cannot crowd out an exit code;
- arrays keep their elements until the budget is spent, then end with `[N more items not stored]`.
A value already within the budget and with nothing base64 in it is returned as it is, without being decoded.
func ClipString ¶ added in v0.113.0
ClipString keeps at most n bytes of s, cut on a rune boundary, and says what it dropped: `…[truncated 81234 bytes]`, or `…[truncated 81234 bytes, 312 lines]` when the dropped part held line breaks — so a count of lines written (the week's lines added) can still be made from a clipped Write.
func HumanBytes ¶ added in v0.113.0
HumanBytes spells a size the way the markers do: 300 B, 512 KB, 1.4 MB.
Types ¶
type Event ¶
type Event struct {
ID int64 `json:"id"`
Ts time.Time `json:"ts"`
AgentID string `json:"agent_id,omitempty"` // harness agent id (Phase 2)
SessionID string `json:"session_id"`
Source Source `json:"source"`
Kind Kind `json:"kind"`
Tool string `json:"tool,omitempty"` // for tool.* (Bash, Edit, Read, mcp__…)
Payload json.RawMessage `json:"payload"`
Tokens *TokenDelta `json:"tokens,omitempty"` // for turn.assistant
CostUSD *float64 `json:"cost_usd,omitempty"` // for turn.assistant, once priced
Model string `json:"model,omitempty"` // for turn.assistant
// Dedupe key: hook payloads carry tool_use_id / prompt_id; transcript lines
// carry uuid. Same key + same session ⇒ same logical event.
Key string `json:"key,omitempty"`
// MsgID is the assistant message this event belongs to. On turn.assistant it
// is the message whose usage is billed; on tool.pre it is the message whose
// content block asked for the tool. Equal ids therefore mean "this tool call
// was paid for by that turn" — the linkage per-directory attribution needs.
//
// Empty when unknown, which is the honest answer rather than a guess: the
// hook plane never sees a message id (the PreToolUse payload does not carry
// one), so tool calls captured by hooks before the transcript caught up are
// unlinkable and are reported as such instead of being attributed.
MsgID string `json:"msg_id,omitempty"`
// TouchDir is the directory this tool call touched, resolved at ingest from
// the tool's own input. Empty when the tool named no path.
TouchDir string `json:"touch_dir,omitempty"`
// ToolBytes is, on tool.post, the size of the tool_response as the tool
// handed it back, before CapToolField bounded the stored copy. The store
// writes it to events.tool_bytes; zero means "measure the stored
// payload", which is the same number for a response that was not capped.
ToolBytes int64 `json:"-"`
}
Event is the normalized event. ID is assigned by the store (SQLite rowid).
func (Event) Subagent ¶ added in v0.65.0
Subagent reports whether the event belongs to a subagent working inside the session rather than to the session's main thread: a Claude Code sidechain, a Codex subagent filed under its parent, a hook fired inside a subagent. Two marks say so and either is enough: an agent id (hooks, transcripts and Codex set one on a subagent's events) or `sidechain: true` in the payload, the key every source with subagents writes.
The SQL twin is store.MainThreadWhere; the two must agree.
type Kind ¶
type Kind string
Kind is the normalized event kind. Unknown kinds must be tolerated by every consumer (logged and ignored, never fatal).
const ( KindToolPre Kind = "tool.pre" KindToolPost Kind = "tool.post" KindTurnUser Kind = "turn.user" KindTurnAssistant Kind = "turn.assistant" KindAgentStop Kind = "agent.stop" KindAgentSpawn Kind = "agent.spawn" // KindSessionEnd is emitted for SessionEnd hooks — the user left the // session. It does not by itself end one: the hook also fires on /clear and // on Escape, so treating it as the end buried sessions their owner was // still working in. What ends a session is its process exiting; see // hookd.endsTheSession and ADR-028. KindSessionEnd Kind = "session.end" KindMailSent Kind = "mail.sent" KindMailDelivered Kind = "mail.delivered" KindTaskCreated Kind = "task.created" KindTaskDone Kind = "task.done" KindApprovalRequested Kind = "approval.requested" KindCostTick Kind = "cost.tick" // KindThrottle is emitted for StopFailure hooks (rate_limit / overloaded / // billing) — the honest throttle signal for the limit forecast. KindThrottle Kind = "throttle" // KindContextCompact is emitted for PreCompact hooks (context is about to be // summarized). Not in the spec's original list; consumers may ignore it. KindContextCompact Kind = "context.compact" // KindContextClear is emitted for a `SessionEnd` whose reason is `clear` — // the user ran /clear. It is a context reset like a compact, and like a // compact it does not end the session, but it is not the same event and // must not borrow the other's name: a compact summarizes the context and // keeps its substance, while /clear discards it outright. Folding the two // together made the dashboard narrate "compacting context" at a session // whose owner had just cleared it — a claim about what Claude was doing // that was never true. KindContextClear Kind = "context.clear" // KindSessionContinue is a `SessionEnd` hook whose reason leaves the // session running — `prompt_input_exit` (Escape at the prompt) or `other`. // The fact is worth keeping on the timeline, but it may not be stored as // session.end, which rollup treats as "retire this session", nor as a // context event, since neither reason touches the context. KindSessionContinue Kind = "session.continue" // KindPermissionPrompt is Claude Code's `PermissionRequest` hook: the // session is showing a permission dialog and nothing moves until somebody // answers it. Distinct from approval.requested, which is the orchestrator's // own approval queue. KindPermissionPrompt Kind = "permission.prompt" )
type Source ¶
type Source string
Source says which data plane produced the event.
const ( SourceHook Source = "hook" SourceTranscript Source = "transcript" // SourceOpenCode marks events imported from OpenCode's own database. They // arrive already priced by OpenCode, so the pricing table is not applied to // them; the source is what tells the reader which arithmetic produced a // figure. SourceOpenCode Source = "opencode" // SourceGemini marks calls Caprock made to Google's Gemini on the user's // own key. Unlike OpenCode these are priced by our own table, because the // figures come from the response's usageMetadata and there is no vendor // cost to carry — Google reports per project, not per call (ADR-023). SourceGemini Source = "gemini" // SourceCodex marks events read from OpenAI Codex's rollout transcripts. // Like Gemini and unlike OpenCode they are priced by our own table: Codex // reports token counts but never a cost of its own, which is why the // pricing table had to grow OpenAI rows to support it. SourceCodex Source = "codex" // SourceDeepseek marks events read from DeepSeek Harness (DSH) session // transcripts. Like Codex they are priced by our own table: DSH reports // token counts but never a cost of its own. SourceDeepseek Source = "deepseek" )
type TokenDelta ¶
type TokenDelta struct {
In int64 `json:"in"`
Out int64 `json:"out"`
CacheRead int64 `json:"cache_read"`
CacheWrite int64 `json:"cache_write"`
// CacheWrite1h is the subset of CacheWrite billed at the 1h-TTL rate, when the
// transcript reports the split. Zero when unknown.
CacheWrite1h int64 `json:"cache_write_1h,omitempty"`
}
TokenDelta carries per-turn token usage (turn.assistant only).
func (TokenDelta) Total ¶
func (t TokenDelta) Total() int64
Total returns all tokens that entered or left the model for this turn.