@spellkave/agent-observability 0.0.0-bootstrap → 0.0.0-stage

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/events.ts DELETED
@@ -1,399 +0,0 @@
1
- // `agent-run-events/v1` — Spellkave's OWN versioned JSONL contract for WORLD
2
- // ACTOR runs (ADR-0074, #1857 S2). The schema's spine is the RUN with a
3
- // `run_kind` discriminator — "episode" was wanderer vocabulary, one run kind
4
- // among several, never the schema's spine.
5
- //
6
- // Naming disambiguation (binding, ADR-0074): this package observes WORLD
7
- // ACTORS. It is NOT ADR-0030 coding-agent trace/metric telemetry. The OTel sink
8
- // uses distinct resource/service names and never writes into coding-agent
9
- // streams; the canonical store is file-based run artifacts.
10
- //
11
- // Contract rules:
12
- // - The version field (`v`) is MANDATORY on every line.
13
- // - Readers are unknown-field-tolerant: they ignore fields they don't know
14
- // and never throw on extra data (see `reader.ts`).
15
- // - LLM actors (via agent-runtime, S3) and deterministic NPC/companion
16
- // runners (ADR-0034 — no agent-runtime dependency) emit the SAME stream,
17
- // which is why this package depends on nothing LLM-specific.
18
-
19
- /** The one schema version this package writes. Readers accept only this. */
20
- export const AGENT_RUN_EVENTS_VERSION = "agent-run-events/v1" as const;
21
-
22
- /** What KIND of run this is. Open set — the union names today's known kinds,
23
- * but the contract tolerates future kinds (readers must not exhaustively
24
- * switch without a default). */
25
- export type RunKind = "campaign_playthrough" | "world_observation" | "combat_beat" | (string & {});
26
-
27
- /** The actor's role profile (ADR-0074 role seam). */
28
- export type RunRole = "PLAYER" | "DM" | "ADMIN" | (string & {});
29
-
30
- /** Who is driving the actor. `service` marks a host process (the remote MCP
31
- * gateway) reporting its OWN lifecycle rather than an actor it drives
32
- * (sk-0m4.120.12) — such rows carry no actor decision. */
33
- export type ControllerKind = "llm_player" | "deterministic_npc" | "companion" | "human" | "service";
34
-
35
- /** The per-run envelope stamped onto every event line. */
36
- export interface AgentRunEnvelope {
37
- v: typeof AGENT_RUN_EVENTS_VERSION;
38
- run_id: string;
39
- run_kind: RunKind;
40
- actor_id: string;
41
- role: RunRole;
42
- controller_kind: ControllerKind;
43
- /** The driver behind the runtime interface (e.g. "pi-agent") or the
44
- * deterministic runner's name (e.g. "npc-fleet"). */
45
- driver: string;
46
- model?: string;
47
- /** World/session seed for reproducibility; empty string when seedless. */
48
- seed: string;
49
- /** ISO-8601 emission timestamp. */
50
- ts: string;
51
- }
52
-
53
- export type AgentRunEventType =
54
- | "run_start"
55
- | "run_end"
56
- | "turn_start"
57
- | "turn_end"
58
- | "tool_call"
59
- | "budget_tick"
60
- | "terminal"
61
- | "gap_report"
62
- // N2 (@spellkave/npc's seat library): one per perceive→decide→act cycle,
63
- // carrying the brain's `SeatDecision.reasoning` (summary + optional scored
64
- // candidates). Additive only — readers are unknown-type-tolerant by
65
- // contract (see the module doc above), so no consumer needs a matching
66
- // change to keep working.
67
- | "brain_decision"
68
- // ADR-0093 P2 (D-2/D-4): one per delivered `WorldEvent` (session lifecycle,
69
- // turn transition, self-hit, reaction window) — emitted by
70
- // `@spellkave/agent-runtime`'s push-delivery adapter at DELIVERY time (the
71
- // turn boundary `steer`/idle `followUp` fires), never at observation time;
72
- // the tick the underlying change was OBSERVED travels inside the payload's
73
- // `observed_at_tick` field instead (D-4 record determinism). Additive only,
74
- // same precedent as `brain_decision` above.
75
- | "world_event"
76
- // sk-jay0.6.31.7: one per visible `world_event` TABLE header row, emitted
77
- // by the adventure NPC host over the existing world-actor OTLP path.
78
- // Distinct from `world_event` above (player-facing delivery vocabulary in
79
- // `@spellkave/world-events`). Payload is the scalar allowlist only.
80
- | "world_table_event"
81
- // A self-driven actor cycle failed after its trigger had already been
82
- // accepted. This is distinct from the initiating transport/tool result.
83
- | "cycle_error"
84
- // sk-jay0.7.389.3: one per consumed world-module shape the SDK's connect()
85
- // could not decode or call, emitted when the shape check runs in "warn"
86
- // mode (it connected anyway). Additive, same precedent as above.
87
- | "contract_shape_mismatch"
88
- // sk-jay0.6.15.56.24.9: exactly one per scheduled self-driven seat run,
89
- // naming the subscribed tables whose cache mutation woke it and what the
90
- // seat did with that wake (ran, or skipped at which gate). The only record
91
- // of WHY a seat cycled; without it "every seat perceives on every tick"
92
- // could not be attributed to a table. Additive only, same precedent as
93
- // `brain_decision` above.
94
- | "cycle_wake"
95
- // One completed passive-reader phase change. This is intentionally separate
96
- // from `cycle_wake`: a quiet watch seat emits no cycles, so phase/liveness
97
- // evidence needs its own bounded JSONL record.
98
- | "subscription_transition"
99
- // Hardening (2026-08-06, wedged-session incident): emitted exactly once by
100
- // a size-capped sink (`createMemorySink`'s optional `maxEvents`, `emit.ts`)
101
- // in place of the event that would have exceeded the cap — bounds
102
- // worst-case memory growth from a runaway/wedged run without silently
103
- // losing the fact that truncation happened. Additive only, same precedent
104
- // as `brain_decision`/`world_event` above.
105
- | "sink_truncated"
106
- // Remote MCP `spellkave_feedback` (sk-rax8.7): one per submit attempt on the
107
- // same run as the tool_call envelope. Payload carries issue reference +
108
- // bundle hash only — never the must-gather body.
109
- | "player_feedback"
110
- // Gateway HTTP edge (sk-0m4.120.11): one per handled request on the run
111
- // `mcp-<correlation_id>` (the same run the tool_call uses), carrying the
112
- // matched route pattern, status, duration and the Cloudflare `CF-Ray` id —
113
- // never a URL, query string, body, or bearer token.
114
- | "http_request"
115
- // Host / session lifecycle (sk-0m4.120.12) — scalars only. Emitted by the
116
- // remote MCP gateway (`gateway_start`, `session_attach`,
117
- // `subscription_lost`, `session_resubscribed`, `session_detach`,
118
- // `session_token_refreshed`) and by the
119
- // PLAYER session daemon itself (`daemon_exit`) so a pod restart, a dead
120
- // daemon, and a dropped world subscription stop sharing one doctor signature.
121
- | "gateway_start"
122
- | "session_attach"
123
- | "subscription_lost"
124
- | "session_resubscribed"
125
- | "session_detach"
126
- // sk-jay0.5.14.15: the gateway accepted a refreshed OAuth access token for
127
- // the SAME subject and kept the live session and route (no detach, no
128
- // re-probe). Additive only, same precedent as `brain_decision` above.
129
- | "session_token_refreshed"
130
- | "daemon_exit"
131
- // World lifecycle (sk-jay0.6.38): one `world_lifecycle` event type with a
132
- // `phase` field carries every step of load → boot → ready → unload,
133
- // emitted by three services (the Account API, npc-host, and the gateway)
134
- // rather than one event type per phase — see `WorldLifecycleData` below.
135
- // Additive only — readers are unknown-type-tolerant by contract (see the
136
- // module doc above), so no consumer needs a matching change to keep
137
- // working.
138
- | "world_lifecycle";
139
-
140
- /** One JSONL line: envelope + event type + type-specific payload. Payloads
141
- * are open records — emitters may add fields; readers tolerate unknowns. */
142
- export interface AgentRunEvent extends AgentRunEnvelope {
143
- type: AgentRunEventType;
144
- data: Record<string, unknown>;
145
- }
146
-
147
- // --- Well-known payload shapes (advisory: emitters SHOULD provide these
148
- // fields; readers MUST NOT assume they are present). ---
149
-
150
- export interface ToolCallData {
151
- tool_name: string;
152
- is_error?: boolean;
153
- /** Emitter-chosen compact summary — never the full result payload. */
154
- summary?: string;
155
- duration_ms?: number;
156
- }
157
-
158
- export interface BudgetTickData {
159
- tool_calls_used?: number;
160
- max_tool_calls?: number;
161
- dry_streak?: number;
162
- max_dry_streak?: number;
163
- tokens_used?: number;
164
- token_budget?: number;
165
- }
166
-
167
- export interface TurnEndData {
168
- turn?: number;
169
- input_tokens?: number;
170
- output_tokens?: number;
171
- tool_calls?: number;
172
- }
173
-
174
- /** Payload for the one `sink_truncated` line a capped sink ever emits. */
175
- export interface SinkTruncatedData {
176
- /** The cap that was hit (`maxEvents` the sink was constructed with). */
177
- max_events: number;
178
- /** How many events this sink had already accepted before truncating. */
179
- events_written: number;
180
- }
181
-
182
- /** Payload for `player_feedback` — join key to the issue/local reference;
183
- * never includes the must-gather bundle body. Envelope already carries
184
- * `run_id` / `actor_id`; `world_slug` may also appear on the envelope data
185
- * identity bits from the remote-MCP observer. */
186
- export interface PlayerFeedbackData {
187
- /** Issue URL or local reference id returned by the feedback sink. */
188
- reference?: string;
189
- /** Player-selected category when provided. */
190
- category?: "bug" | "confusing" | "idea" | string;
191
- /** UTF-16 code-unit length of the player message (not the text). */
192
- message_chars?: number;
193
- /** SHA-256 hex of the must-gather JSON when a report was attached. */
194
- bundle_sha256?: string;
195
- world_slug?: string;
196
- correlation_id?: string;
197
- is_error?: boolean;
198
- }
199
-
200
- export interface TerminalData {
201
- /** Terminal classification, e.g. "explored_enough" | "converged" |
202
- * "budget_exhausted" | "timeout" | "error" — an open set. */
203
- terminal: string;
204
- reason?: string;
205
- }
206
-
207
- export interface RunEndData {
208
- terminal?: string;
209
- turns?: number;
210
- tool_calls?: number;
211
- input_tokens?: number;
212
- output_tokens?: number;
213
- duration_ms?: number;
214
- }
215
-
216
- /** Safe, stable diagnostics for asynchronous actor-cycle failures. Arbitrary
217
- * error text remains host-local and is never part of this shared event. */
218
- export interface CycleErrorData {
219
- step: string;
220
- tool_name?: string;
221
- failure_class: string;
222
- error_code?: string;
223
- retry_count: number;
224
- cause_kind?: string;
225
- cause_id?: string;
226
- }
227
-
228
- /** Payload for `cycle_wake`. `tables` are the snake_case table names (or the
229
- * SDK's `__applied` marker) accumulated by `notify()` since the previous
230
- * scheduled run — empty for an explicit table-less `notify()`. `outcome`:
231
- * `run` (perceive + decide ran), `skip_baseline` (only baseline singletons
232
- * moved), `skip_revision` (cache revision unchanged), `skip_fingerprint`
233
- * (perceived, view identical, decision skipped), `skip_parked` (authoring
234
- * rejections parked the seat), `coalesced` (a run was in flight; folded into
235
- * its rerun). Bounded vocabulary — safe as a metric dimension. */
236
- export interface CycleWakeData {
237
- tables: string[];
238
- outcome: CycleWakeOutcome;
239
- revision?: number;
240
- }
241
-
242
- export type CycleWakeOutcome =
243
- | "run"
244
- | "skip_baseline"
245
- | "skip_revision"
246
- | "skip_fingerprint"
247
- | "skip_parked"
248
- | "coalesced";
249
-
250
- export const CYCLE_WAKE_OUTCOMES: readonly CycleWakeOutcome[] = [
251
- "run",
252
- "skip_baseline",
253
- "skip_revision",
254
- "skip_fingerprint",
255
- "skip_parked",
256
- "coalesced",
257
- ];
258
-
259
- /** Payload for `brain_decision`. `rule` and admitted input keys are hoisted
260
- * to the top of `data` by `emitBrainDecision`. `cause_kind`/`cause_id` name
261
- * the world row the brain acted on when it can attribute one; they stay
262
- * absent when several identifiable events were in the batch and the brain
263
- * did not name which, or when the typed view supplied no stable reference. */
264
- export interface BrainDecisionData {
265
- view_kind?: string;
266
- intent?: unknown;
267
- rule?: string;
268
- cause_kind?: string;
269
- cause_id?: string;
270
- reasoning?: unknown;
271
- }
272
-
273
- /** Runtime guard for one parsed line. Tolerance means UNKNOWN EXTRA fields
274
- * are preserved — it never means required envelope fields may be absent: a
275
- * line missing any envelope field is skipped, so strict consumers (the run
276
- * index, aggregations) can rely on every accepted event being complete. */
277
- export function isAgentRunEvent(value: unknown): value is AgentRunEvent {
278
- if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
279
- const record = value as Record<string, unknown>;
280
- return (
281
- record.v === AGENT_RUN_EVENTS_VERSION &&
282
- typeof record.run_id === "string" &&
283
- typeof record.run_kind === "string" &&
284
- typeof record.actor_id === "string" &&
285
- typeof record.role === "string" &&
286
- typeof record.controller_kind === "string" &&
287
- typeof record.driver === "string" &&
288
- typeof record.seed === "string" &&
289
- typeof record.ts === "string" &&
290
- typeof record.type === "string"
291
- );
292
- }
293
-
294
- /** Payload for `http_request` (sk-0m4.120.11). `route` is the matched route
295
- * PATTERN (e.g. `/mcp`, `/w/*`), never the raw URL. */
296
- export interface HttpRequestData {
297
- http_status: number;
298
- route: string;
299
- duration_ms: number;
300
- correlation_id: string;
301
- /** Cloudflare request id from the `CF-Ray` header, when present. */
302
- cf_ray?: string;
303
- http_method?: string;
304
- }
305
-
306
- /** Payloads for the lifecycle events (sk-0m4.120.12). Every field is a
307
- * closed-vocabulary scalar or a boolean; `image_tag` is the deployed
308
- * revision (`SPELLKAVE_IMAGE_TAG`), never a build path. */
309
- export interface GatewayStartData {
310
- image_tag: string;
311
- }
312
- export interface SessionAttachData {
313
- /** `true` when the attach spawned a new daemon; `false` when it joined one. */
314
- spawned: boolean;
315
- }
316
- export interface SubscriptionLostData {
317
- /** Transport code (`ECONNREFUSED`, `ETIMEDOUT`, …) or a lifecycle word. */
318
- cause: string;
319
- }
320
- export interface SessionDetachData {
321
- reason: string;
322
- }
323
- export interface DaemonExitData {
324
- /** The run terminal the daemon closed with (`shutdown`, `idle_timeout`,
325
- * `world_connection_lost`, `error`). */
326
- reason: string;
327
- }
328
-
329
- // --- World lifecycle (sk-jay0.6.38.2) ---------------------------------------
330
- // Vocabulary shared by every emitter of `world_lifecycle` rows (Account API
331
- // job watcher, npc-host boot path, gateway route probe) and by the doctor/
332
- // dashboard that reads them back. Field names and phases are canonical in
333
- // `openspec/changes/archive/2026-10-09-world-lifecycle-telemetry/design.md` § "The event
334
- // family" and `ledger/designs/world-lifecycle-telemetry.md` § "The event
335
- // family" — do not fork the vocabulary here.
336
-
337
- /** Every phase of load → boot → ready → unload, across all three emitters.
338
- * Exported as a runtime array (not just a type) so emitters and the
339
- * doctor/dashboard can validate against and iterate the same vocabulary. */
340
- export const WORLD_LIFECYCLE_PHASES = [
341
- "load_requested",
342
- "job_created",
343
- "job_running",
344
- "job_succeeded",
345
- "job_failed",
346
- "npc_host_restart_requested",
347
- "world_ready",
348
- "unloaded",
349
- "host_start",
350
- "host_boot_failed",
351
- "seat_boot_failed",
352
- "host_ready",
353
- "route_probe_failed",
354
- ] as const;
355
-
356
- export type WorldLifecyclePhase = (typeof WORLD_LIFECYCLE_PHASES)[number];
357
-
358
- /** Bounded `failure_class` vocabulary for `host_boot_failed` (npc-host).
359
- * Runtime array for the same reason as `WORLD_LIFECYCLE_PHASES`. */
360
- export const WORLD_LIFECYCLE_FAILURE_CLASSES = [
361
- "env",
362
- "node_unreachable",
363
- "binding_drift",
364
- "not_provisioned",
365
- "bootstrap_failed",
366
- "no_seats",
367
- "all_seats_failed",
368
- ] as const;
369
-
370
- export type WorldLifecycleFailureClass = (typeof WORLD_LIFECYCLE_FAILURE_CLASSES)[number];
371
-
372
- /** Payload for `world_lifecycle` (sk-jay0.6.38.2). Scalars only — matches the
373
- * OTLP disclosure boundary in `otel.ts`. `world_slug` is required on every
374
- * row; job-owned phases (`load_requested` … `world_ready`, `unloaded`) also
375
- * carry `job_id`/`job_kind`. `boot_ms` is set only on `world_ready`
376
- * (`ready_ts − load_requested_ts`). */
377
- export interface WorldLifecycleData {
378
- phase: WorldLifecyclePhase;
379
- outcome: "started" | "ok" | "failed";
380
- world_slug: string;
381
- /** `load.<slug>.<ms>` / `reset.<slug>.<ms>`; absent on host/gateway rows. */
382
- job_id?: string;
383
- job_kind?: "load" | "reset";
384
- /** This phase's duration where it has one. */
385
- duration_ms?: number;
386
- /** `world_ready` only: `ready_ts − load_requested_ts`. */
387
- boot_ms?: number;
388
- /** `SPELLKAVE_IMAGE_TAG` of the emitting pod. */
389
- image_tag?: string;
390
- /** Pinned `sha256:` digest (≤ 256 chars, not secret). */
391
- artifact_digest?: string;
392
- /** Bounded per-emitter vocabulary; see `WORLD_LIFECYCLE_FAILURE_CLASSES`
393
- * for the npc-host `host_boot_failed` set. */
394
- failure_class?: WorldLifecycleFailureClass | string;
395
- /** Constructor name, never a message (sk-0m4.120.36 rule). */
396
- error_class?: string;
397
- /** k8s condition `reason` or SDK code. */
398
- error_code?: string;
399
- }
package/src/index.ts DELETED
@@ -1,13 +0,0 @@
1
- // @spellkave/agent-observability — world-actor run observability (ADR-0074).
2
- // Browser-safe barrel: schema + emit API + gateway/memory sinks + tolerant
3
- // readers/aggregations. Node-only pieces (JSONL file sink, run index over a
4
- // directory tree) live behind the "./node" subpath export.
5
-
6
- export * from "./correlation.ts";
7
- export * from "./emit.ts";
8
- export * from "./events.ts";
9
- export * from "./metrics.ts";
10
- export * from "./otel.ts";
11
- export * from "./reader.ts";
12
- export * from "./traces.ts";
13
- export * from "./world-table-event.ts";