@cultured_computer/sight 0.2.1 → 0.3.0

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/README.md CHANGED
@@ -76,6 +76,26 @@ await Sight.emitOutcomeEvent({
76
76
  })
77
77
  ```
78
78
 
79
+ ## Capture environment events
80
+
81
+ Deployments that run alongside a non-dialogue channel — a stimulus feed, a
82
+ game state stream, an editor buffer — can record that channel's events
83
+ against the conversation:
84
+
85
+ ```ts
86
+ await Sight.captureEnvironmentEvents(events, {
87
+ clock_source: 'my-event-clock',
88
+ skew_bound_ms: 25,
89
+ rule_version: 'v1',
90
+ })
91
+ ```
92
+
93
+ The envelope is generalized: `source_type` and `event` are free strings whose
94
+ meaning is defined per deployment. The instrument attaches events to turns by
95
+ time — the next turn's state corner, plus the causing turn's outcome corner
96
+ when the event carries a `causeProvenance` turn reference — and never
97
+ interprets them.
98
+
79
99
  ## Scope
80
100
 
81
101
  Sight is a measurement system: it reports what a deployment did. It does not
package/dist/index.cjs CHANGED
@@ -174,6 +174,35 @@ var IngestClient = class {
174
174
  this.log(`[probe] outcome-event ingest error: ${msg}`);
175
175
  }
176
176
  }
177
+ /**
178
+ * Send an environment-event batch to the ingest API. Fire-and-log, same as
179
+ * sendTurn. The envelope is generalized: the worker attaches each event to
180
+ * its session's record by time; meaning is per-deployment.
181
+ */
182
+ async sendEnvironmentEvents(batch) {
183
+ try {
184
+ const send = () => fetch(`${this.endpoint}/v1/environment-events`, {
185
+ method: "POST",
186
+ headers: {
187
+ Authorization: `Bearer ${this.apiKey}`,
188
+ "content-type": "application/json"
189
+ },
190
+ body: JSON.stringify(batch)
191
+ });
192
+ const response = await this.retryIfRateLimited(await send(), send);
193
+ if (response === null) return;
194
+ if (!response.ok) {
195
+ this.log(`[probe] environment-events ingest failed status=${response.status}`);
196
+ return;
197
+ }
198
+ if (this.debug) {
199
+ this.log(`[probe] environment-events ingest ok events=${batch.events.length}`);
200
+ }
201
+ } catch (err) {
202
+ const msg = err instanceof Error ? err.message : String(err);
203
+ this.log(`[probe] environment-events ingest error: ${msg}`);
204
+ }
205
+ }
177
206
  /**
178
207
  * Route a send response through the 429 retry policy. Non-429 responses
179
208
  * pass through untouched. A 429 is re-sent exactly once after Retry-After
@@ -1158,6 +1187,35 @@ zod.z.object({
1158
1187
  seed: zod.z.number().int(),
1159
1188
  startDate: zod.z.string()
1160
1189
  });
1190
+ var EnvironmentEventInputSchema = zod.z.object({
1191
+ /** Free-form channel label (e.g. a stimulus feed, a game state stream). Open vocabulary — NOT an enum. */
1192
+ source_type: zod.z.string().min(1),
1193
+ /** Free-form event name. Open vocabulary — NOT an enum. */
1194
+ event: zod.z.string().min(1),
1195
+ /** ISO timestamp of the event on the deployer's clock (see alignment). */
1196
+ at: zod.z.string().datetime(),
1197
+ /** Optional detail payload (deployer-defined). */
1198
+ meta: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
1199
+ sessionExternalId: zod.z.string().min(1),
1200
+ deploymentSlug: zod.z.string().min(1),
1201
+ /**
1202
+ * Optional reference to the causing turn. v1 resolves a turn index within
1203
+ * the same session (string-encoded integer); any other value is stored
1204
+ * verbatim on the event record without an outcome-corner link. The
1205
+ * instrument records the reference only — no causal inference.
1206
+ */
1207
+ causeProvenance: zod.z.string().min(1).optional()
1208
+ });
1209
+ var AlignmentProvenanceSchema = zod.z.object({
1210
+ clock_source: zod.z.string().min(1),
1211
+ skew_bound_ms: zod.z.number().nonnegative().optional(),
1212
+ rule_version: zod.z.string().min(1)
1213
+ });
1214
+ var EnvironmentEventsBatchSchema = zod.z.object({
1215
+ /** Events are self-addressed: a batch may span sessions/deployments. */
1216
+ events: zod.z.array(EnvironmentEventInputSchema).min(1).max(100),
1217
+ alignment: AlignmentProvenanceSchema
1218
+ });
1161
1219
  zod.z.object({
1162
1220
  deploymentSlug: zod.z.string().min(1),
1163
1221
  sessionExternalId: zod.z.string().min(1),
@@ -1175,6 +1233,10 @@ zod.z.object({
1175
1233
 
1176
1234
  // src/probe.ts
1177
1235
  var PRE_SESSION_BUFFER_MAX = 100;
1236
+ var DEFAULT_ENVIRONMENT_ALIGNMENT = {
1237
+ clock_source: "sdk",
1238
+ rule_version: "v1"
1239
+ };
1178
1240
  var state = null;
1179
1241
  var Probe = class {
1180
1242
  /**
@@ -1635,6 +1697,43 @@ var Probe = class {
1635
1697
  const validated = OutcomeEventEmitSchema.parse(input);
1636
1698
  await state.client.sendOutcomeEvent(state.config.deployment, validated);
1637
1699
  }
1700
+ /**
1701
+ * Capture one environment event against a session (Arc D-rail). The
1702
+ * envelope is generalized: `source_type` and `event` are free strings whose
1703
+ * meaning is defined per deployment — the instrument stores the event and
1704
+ * attaches it to the session's record by time (the next turn's state
1705
+ * corner, plus the causing turn's outcome corner when `causeProvenance`
1706
+ * names a turn index), and never interprets it. Fire-and-log, like
1707
+ * captureTurn: errors are debug-logged, never thrown.
1708
+ *
1709
+ * Alignment defaults to the SDK's own clock claim; deployments with a
1710
+ * stricter clock contract (or batches) should call
1711
+ * captureEnvironmentEvents with explicit alignment provenance.
1712
+ */
1713
+ static async captureEnvironmentEvent(event, alignment) {
1714
+ if (state === null) {
1715
+ console.warn("[probe] captureEnvironmentEvent called before init");
1716
+ return;
1717
+ }
1718
+ const validated = EnvironmentEventInputSchema.parse(event);
1719
+ const align = alignment ? AlignmentProvenanceSchema.parse(alignment) : DEFAULT_ENVIRONMENT_ALIGNMENT;
1720
+ await state.client.sendEnvironmentEvents({ events: [validated], alignment: align });
1721
+ }
1722
+ /**
1723
+ * Capture a batch of environment events with explicit alignment provenance
1724
+ * (clock_source, skew_bound_ms, rule_version). Batches are capped at 100
1725
+ * events and may span sessions — every event is self-addressed. Same
1726
+ * generalized envelope and fire-and-log discipline as the single-event
1727
+ * capture above.
1728
+ */
1729
+ static async captureEnvironmentEvents(events, alignment) {
1730
+ if (state === null) {
1731
+ console.warn("[probe] captureEnvironmentEvents called before init");
1732
+ return;
1733
+ }
1734
+ const batch = EnvironmentEventsBatchSchema.parse({ events, alignment });
1735
+ await state.client.sendEnvironmentEvents(batch);
1736
+ }
1638
1737
  /**
1639
1738
  * Flag a persona break for a session. Wire this to the customer's in-product
1640
1739
  * "that didn't sound like you" UX. Populates the `user_flagged_break`