@cultured_computer/sight 0.2.0 → 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 +20 -0
- package/dist/index.cjs +159 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +191 -4
- package/dist/index.d.ts +191 -4
- package/dist/index.js +159 -4
- package/dist/index.js.map +1 -1
- package/package.json +13 -5
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import { propagation, context, trace } from '@opentelemetry/api';
|
|
|
3
3
|
|
|
4
4
|
// src/client.ts
|
|
5
5
|
var DEFAULT_DASHBOARD_URL = "https://field.cultured.computer/dashboard";
|
|
6
|
+
var MAX_PENDING_RETRIES = 50;
|
|
6
7
|
var IngestClient = class {
|
|
7
8
|
apiKey;
|
|
8
9
|
endpoint;
|
|
@@ -18,6 +19,15 @@ var IngestClient = class {
|
|
|
18
19
|
* arriving before any turn is not a captured session).
|
|
19
20
|
*/
|
|
20
21
|
firstCaptureAnnounced = false;
|
|
22
|
+
/**
|
|
23
|
+
* Rate-limit (429) handling: the first 429 per process prints one stderr
|
|
24
|
+
* warning (not debug-gated, same channel and lifetime as the first-capture
|
|
25
|
+
* announce); subsequent 429s only debug-log. Each rate-limited request is
|
|
26
|
+
* retried exactly once after Retry-After; pendingRetries counts retries
|
|
27
|
+
* waiting on their timers so the cap can shed load.
|
|
28
|
+
*/
|
|
29
|
+
rateLimitAnnounced = false;
|
|
30
|
+
pendingRetries = 0;
|
|
21
31
|
constructor(opts) {
|
|
22
32
|
this.apiKey = opts.apiKey;
|
|
23
33
|
this.endpoint = opts.endpoint.replace(/\/$/, "");
|
|
@@ -30,7 +40,7 @@ var IngestClient = class {
|
|
|
30
40
|
*/
|
|
31
41
|
async sendTurn(turn) {
|
|
32
42
|
try {
|
|
33
|
-
const
|
|
43
|
+
const send = () => fetch(`${this.endpoint}/v1/turns`, {
|
|
34
44
|
method: "POST",
|
|
35
45
|
headers: {
|
|
36
46
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -38,6 +48,8 @@ var IngestClient = class {
|
|
|
38
48
|
},
|
|
39
49
|
body: JSON.stringify(turn)
|
|
40
50
|
});
|
|
51
|
+
const response = await this.retryIfRateLimited(await send(), send);
|
|
52
|
+
if (response === null) return;
|
|
41
53
|
if (!response.ok) {
|
|
42
54
|
this.log(`[probe] ingest failed status=${response.status}`);
|
|
43
55
|
return;
|
|
@@ -63,7 +75,7 @@ var IngestClient = class {
|
|
|
63
75
|
*/
|
|
64
76
|
async sendFlag(deploymentSlug, flag) {
|
|
65
77
|
try {
|
|
66
|
-
const
|
|
78
|
+
const send = () => fetch(`${this.endpoint}/v1/flags`, {
|
|
67
79
|
method: "POST",
|
|
68
80
|
headers: {
|
|
69
81
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -77,6 +89,8 @@ var IngestClient = class {
|
|
|
77
89
|
turnIndex: flag.turnIndex
|
|
78
90
|
})
|
|
79
91
|
});
|
|
92
|
+
const response = await this.retryIfRateLimited(await send(), send);
|
|
93
|
+
if (response === null) return;
|
|
80
94
|
if (!response.ok) {
|
|
81
95
|
this.log(`[probe] flag ingest failed status=${response.status}`);
|
|
82
96
|
return;
|
|
@@ -95,7 +109,7 @@ var IngestClient = class {
|
|
|
95
109
|
*/
|
|
96
110
|
async sendSessionEnd(deploymentSlug, end) {
|
|
97
111
|
try {
|
|
98
|
-
const
|
|
112
|
+
const send = () => fetch(`${this.endpoint}/v1/sessions/end`, {
|
|
99
113
|
method: "POST",
|
|
100
114
|
headers: {
|
|
101
115
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -107,6 +121,8 @@ var IngestClient = class {
|
|
|
107
121
|
endedReason: end.endedReason
|
|
108
122
|
})
|
|
109
123
|
});
|
|
124
|
+
const response = await this.retryIfRateLimited(await send(), send);
|
|
125
|
+
if (response === null) return;
|
|
110
126
|
if (!response.ok) {
|
|
111
127
|
this.log(`[probe] session-end ingest failed status=${response.status}`);
|
|
112
128
|
return;
|
|
@@ -125,7 +141,7 @@ var IngestClient = class {
|
|
|
125
141
|
*/
|
|
126
142
|
async sendOutcomeEvent(deploymentSlug, event) {
|
|
127
143
|
try {
|
|
128
|
-
const
|
|
144
|
+
const send = () => fetch(`${this.endpoint}/v1/outcome-events`, {
|
|
129
145
|
method: "POST",
|
|
130
146
|
headers: {
|
|
131
147
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -140,6 +156,8 @@ var IngestClient = class {
|
|
|
140
156
|
detail: event.detail
|
|
141
157
|
})
|
|
142
158
|
});
|
|
159
|
+
const response = await this.retryIfRateLimited(await send(), send);
|
|
160
|
+
if (response === null) return;
|
|
143
161
|
if (!response.ok) {
|
|
144
162
|
this.log(`[probe] outcome-event ingest failed status=${response.status}`);
|
|
145
163
|
return;
|
|
@@ -154,6 +172,73 @@ var IngestClient = class {
|
|
|
154
172
|
this.log(`[probe] outcome-event ingest error: ${msg}`);
|
|
155
173
|
}
|
|
156
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* Send an environment-event batch to the ingest API. Fire-and-log, same as
|
|
177
|
+
* sendTurn. The envelope is generalized: the worker attaches each event to
|
|
178
|
+
* its session's record by time; meaning is per-deployment.
|
|
179
|
+
*/
|
|
180
|
+
async sendEnvironmentEvents(batch) {
|
|
181
|
+
try {
|
|
182
|
+
const send = () => fetch(`${this.endpoint}/v1/environment-events`, {
|
|
183
|
+
method: "POST",
|
|
184
|
+
headers: {
|
|
185
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
186
|
+
"content-type": "application/json"
|
|
187
|
+
},
|
|
188
|
+
body: JSON.stringify(batch)
|
|
189
|
+
});
|
|
190
|
+
const response = await this.retryIfRateLimited(await send(), send);
|
|
191
|
+
if (response === null) return;
|
|
192
|
+
if (!response.ok) {
|
|
193
|
+
this.log(`[probe] environment-events ingest failed status=${response.status}`);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (this.debug) {
|
|
197
|
+
this.log(`[probe] environment-events ingest ok events=${batch.events.length}`);
|
|
198
|
+
}
|
|
199
|
+
} catch (err) {
|
|
200
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
201
|
+
this.log(`[probe] environment-events ingest error: ${msg}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Route a send response through the 429 retry policy. Non-429 responses
|
|
206
|
+
* pass through untouched. A 429 is re-sent exactly once after Retry-After
|
|
207
|
+
* seconds (default 1s when the header is missing or unparseable; clamped
|
|
208
|
+
* to [1, 60]s); the retry's response is returned as-is, so a second 429 is
|
|
209
|
+
* never retried again — callers log and drop it like any other failed
|
|
210
|
+
* send. Returns null when the retry was dropped at the pending-retry cap.
|
|
211
|
+
* The timer is unref'd (where supported) so a pending retry never holds
|
|
212
|
+
* the process open.
|
|
213
|
+
*/
|
|
214
|
+
async retryIfRateLimited(response, resend) {
|
|
215
|
+
if (response.status !== 429) return response;
|
|
216
|
+
if (!this.rateLimitAnnounced) {
|
|
217
|
+
this.rateLimitAnnounced = true;
|
|
218
|
+
console.error(
|
|
219
|
+
"Sight: ingest rate limit (429) \u2014 retrying once per Retry-After; sustained limiting drops captures. See https://field.cultured.computer/sight/sdk-reference#rate-limits"
|
|
220
|
+
);
|
|
221
|
+
} else {
|
|
222
|
+
this.log("[probe] ingest rate limited (429), retrying once");
|
|
223
|
+
}
|
|
224
|
+
if (this.pendingRetries >= MAX_PENDING_RETRIES) {
|
|
225
|
+
this.log("[probe] retry buffer full, dropped");
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
const raw = response.headers.get("retry-after");
|
|
229
|
+
const parsed = raw === null ? Number.NaN : Number(raw);
|
|
230
|
+
const seconds = Number.isFinite(parsed) ? Math.min(Math.max(parsed, 1), 60) : 1;
|
|
231
|
+
this.pendingRetries += 1;
|
|
232
|
+
try {
|
|
233
|
+
await new Promise((resolve) => {
|
|
234
|
+
const timer = setTimeout(resolve, seconds * 1e3);
|
|
235
|
+
timer.unref?.();
|
|
236
|
+
});
|
|
237
|
+
return await resend();
|
|
238
|
+
} finally {
|
|
239
|
+
this.pendingRetries -= 1;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
157
242
|
log(msg) {
|
|
158
243
|
if (this.debug) {
|
|
159
244
|
console.error(msg);
|
|
@@ -1100,6 +1185,35 @@ z.object({
|
|
|
1100
1185
|
seed: z.number().int(),
|
|
1101
1186
|
startDate: z.string()
|
|
1102
1187
|
});
|
|
1188
|
+
var EnvironmentEventInputSchema = z.object({
|
|
1189
|
+
/** Free-form channel label (e.g. a stimulus feed, a game state stream). Open vocabulary — NOT an enum. */
|
|
1190
|
+
source_type: z.string().min(1),
|
|
1191
|
+
/** Free-form event name. Open vocabulary — NOT an enum. */
|
|
1192
|
+
event: z.string().min(1),
|
|
1193
|
+
/** ISO timestamp of the event on the deployer's clock (see alignment). */
|
|
1194
|
+
at: z.string().datetime(),
|
|
1195
|
+
/** Optional detail payload (deployer-defined). */
|
|
1196
|
+
meta: z.record(z.string(), z.unknown()).optional(),
|
|
1197
|
+
sessionExternalId: z.string().min(1),
|
|
1198
|
+
deploymentSlug: z.string().min(1),
|
|
1199
|
+
/**
|
|
1200
|
+
* Optional reference to the causing turn. v1 resolves a turn index within
|
|
1201
|
+
* the same session (string-encoded integer); any other value is stored
|
|
1202
|
+
* verbatim on the event record without an outcome-corner link. The
|
|
1203
|
+
* instrument records the reference only — no causal inference.
|
|
1204
|
+
*/
|
|
1205
|
+
causeProvenance: z.string().min(1).optional()
|
|
1206
|
+
});
|
|
1207
|
+
var AlignmentProvenanceSchema = z.object({
|
|
1208
|
+
clock_source: z.string().min(1),
|
|
1209
|
+
skew_bound_ms: z.number().nonnegative().optional(),
|
|
1210
|
+
rule_version: z.string().min(1)
|
|
1211
|
+
});
|
|
1212
|
+
var EnvironmentEventsBatchSchema = z.object({
|
|
1213
|
+
/** Events are self-addressed: a batch may span sessions/deployments. */
|
|
1214
|
+
events: z.array(EnvironmentEventInputSchema).min(1).max(100),
|
|
1215
|
+
alignment: AlignmentProvenanceSchema
|
|
1216
|
+
});
|
|
1103
1217
|
z.object({
|
|
1104
1218
|
deploymentSlug: z.string().min(1),
|
|
1105
1219
|
sessionExternalId: z.string().min(1),
|
|
@@ -1117,6 +1231,10 @@ z.object({
|
|
|
1117
1231
|
|
|
1118
1232
|
// src/probe.ts
|
|
1119
1233
|
var PRE_SESSION_BUFFER_MAX = 100;
|
|
1234
|
+
var DEFAULT_ENVIRONMENT_ALIGNMENT = {
|
|
1235
|
+
clock_source: "sdk",
|
|
1236
|
+
rule_version: "v1"
|
|
1237
|
+
};
|
|
1120
1238
|
var state = null;
|
|
1121
1239
|
var Probe = class {
|
|
1122
1240
|
/**
|
|
@@ -1577,6 +1695,43 @@ var Probe = class {
|
|
|
1577
1695
|
const validated = OutcomeEventEmitSchema.parse(input);
|
|
1578
1696
|
await state.client.sendOutcomeEvent(state.config.deployment, validated);
|
|
1579
1697
|
}
|
|
1698
|
+
/**
|
|
1699
|
+
* Capture one environment event against a session (Arc D-rail). The
|
|
1700
|
+
* envelope is generalized: `source_type` and `event` are free strings whose
|
|
1701
|
+
* meaning is defined per deployment — the instrument stores the event and
|
|
1702
|
+
* attaches it to the session's record by time (the next turn's state
|
|
1703
|
+
* corner, plus the causing turn's outcome corner when `causeProvenance`
|
|
1704
|
+
* names a turn index), and never interprets it. Fire-and-log, like
|
|
1705
|
+
* captureTurn: errors are debug-logged, never thrown.
|
|
1706
|
+
*
|
|
1707
|
+
* Alignment defaults to the SDK's own clock claim; deployments with a
|
|
1708
|
+
* stricter clock contract (or batches) should call
|
|
1709
|
+
* captureEnvironmentEvents with explicit alignment provenance.
|
|
1710
|
+
*/
|
|
1711
|
+
static async captureEnvironmentEvent(event, alignment) {
|
|
1712
|
+
if (state === null) {
|
|
1713
|
+
console.warn("[probe] captureEnvironmentEvent called before init");
|
|
1714
|
+
return;
|
|
1715
|
+
}
|
|
1716
|
+
const validated = EnvironmentEventInputSchema.parse(event);
|
|
1717
|
+
const align = alignment ? AlignmentProvenanceSchema.parse(alignment) : DEFAULT_ENVIRONMENT_ALIGNMENT;
|
|
1718
|
+
await state.client.sendEnvironmentEvents({ events: [validated], alignment: align });
|
|
1719
|
+
}
|
|
1720
|
+
/**
|
|
1721
|
+
* Capture a batch of environment events with explicit alignment provenance
|
|
1722
|
+
* (clock_source, skew_bound_ms, rule_version). Batches are capped at 100
|
|
1723
|
+
* events and may span sessions — every event is self-addressed. Same
|
|
1724
|
+
* generalized envelope and fire-and-log discipline as the single-event
|
|
1725
|
+
* capture above.
|
|
1726
|
+
*/
|
|
1727
|
+
static async captureEnvironmentEvents(events, alignment) {
|
|
1728
|
+
if (state === null) {
|
|
1729
|
+
console.warn("[probe] captureEnvironmentEvents called before init");
|
|
1730
|
+
return;
|
|
1731
|
+
}
|
|
1732
|
+
const batch = EnvironmentEventsBatchSchema.parse({ events, alignment });
|
|
1733
|
+
await state.client.sendEnvironmentEvents(batch);
|
|
1734
|
+
}
|
|
1580
1735
|
/**
|
|
1581
1736
|
* Flag a persona break for a session. Wire this to the customer's in-product
|
|
1582
1737
|
* "that didn't sound like you" UX. Populates the `user_flagged_break`
|