@cultured_computer/sight 0.2.0 → 0.2.1

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/dist/index.d.cts CHANGED
@@ -563,7 +563,8 @@ type OutcomeEventEmit = z.infer<typeof OutcomeEventEmitSchema>;
563
563
  * HTTP client for the Probe ingest API.
564
564
  *
565
565
  * Sends captured turns to POST /v1/turns with a Bearer token. Async, fire-and-log:
566
- * SDK never blocks the customer's LLM path on ingest.
566
+ * SDK never blocks the customer's LLM path on ingest. Rate-limited sends (429)
567
+ * are retried exactly once after Retry-After; sustained limiting drops captures.
567
568
  */
568
569
 
569
570
  type IngestClientOptions = {
@@ -588,6 +589,15 @@ declare class IngestClient {
588
589
  * arriving before any turn is not a captured session).
589
590
  */
590
591
  private firstCaptureAnnounced;
592
+ /**
593
+ * Rate-limit (429) handling: the first 429 per process prints one stderr
594
+ * warning (not debug-gated, same channel and lifetime as the first-capture
595
+ * announce); subsequent 429s only debug-log. Each rate-limited request is
596
+ * retried exactly once after Retry-After; pendingRetries counts retries
597
+ * waiting on their timers so the cap can shed load.
598
+ */
599
+ private rateLimitAnnounced;
600
+ private pendingRetries;
591
601
  constructor(opts: IngestClientOptions);
592
602
  /**
593
603
  * Send a turn to the ingest API. Fire-and-log: errors are logged but never
@@ -609,6 +619,17 @@ declare class IngestClient {
609
619
  * probe_outcome_events per Amendment I Method D.
610
620
  */
611
621
  sendOutcomeEvent(deploymentSlug: string, event: OutcomeEventEmit): Promise<void>;
622
+ /**
623
+ * Route a send response through the 429 retry policy. Non-429 responses
624
+ * pass through untouched. A 429 is re-sent exactly once after Retry-After
625
+ * seconds (default 1s when the header is missing or unparseable; clamped
626
+ * to [1, 60]s); the retry's response is returned as-is, so a second 429 is
627
+ * never retried again — callers log and drop it like any other failed
628
+ * send. Returns null when the retry was dropped at the pending-retry cap.
629
+ * The timer is unref'd (where supported) so a pending retry never holds
630
+ * the process open.
631
+ */
632
+ private retryIfRateLimited;
612
633
  private log;
613
634
  }
614
635
 
package/dist/index.d.ts CHANGED
@@ -563,7 +563,8 @@ type OutcomeEventEmit = z.infer<typeof OutcomeEventEmitSchema>;
563
563
  * HTTP client for the Probe ingest API.
564
564
  *
565
565
  * Sends captured turns to POST /v1/turns with a Bearer token. Async, fire-and-log:
566
- * SDK never blocks the customer's LLM path on ingest.
566
+ * SDK never blocks the customer's LLM path on ingest. Rate-limited sends (429)
567
+ * are retried exactly once after Retry-After; sustained limiting drops captures.
567
568
  */
568
569
 
569
570
  type IngestClientOptions = {
@@ -588,6 +589,15 @@ declare class IngestClient {
588
589
  * arriving before any turn is not a captured session).
589
590
  */
590
591
  private firstCaptureAnnounced;
592
+ /**
593
+ * Rate-limit (429) handling: the first 429 per process prints one stderr
594
+ * warning (not debug-gated, same channel and lifetime as the first-capture
595
+ * announce); subsequent 429s only debug-log. Each rate-limited request is
596
+ * retried exactly once after Retry-After; pendingRetries counts retries
597
+ * waiting on their timers so the cap can shed load.
598
+ */
599
+ private rateLimitAnnounced;
600
+ private pendingRetries;
591
601
  constructor(opts: IngestClientOptions);
592
602
  /**
593
603
  * Send a turn to the ingest API. Fire-and-log: errors are logged but never
@@ -609,6 +619,17 @@ declare class IngestClient {
609
619
  * probe_outcome_events per Amendment I Method D.
610
620
  */
611
621
  sendOutcomeEvent(deploymentSlug: string, event: OutcomeEventEmit): Promise<void>;
622
+ /**
623
+ * Route a send response through the 429 retry policy. Non-429 responses
624
+ * pass through untouched. A 429 is re-sent exactly once after Retry-After
625
+ * seconds (default 1s when the header is missing or unparseable; clamped
626
+ * to [1, 60]s); the retry's response is returned as-is, so a second 429 is
627
+ * never retried again — callers log and drop it like any other failed
628
+ * send. Returns null when the retry was dropped at the pending-retry cap.
629
+ * The timer is unref'd (where supported) so a pending retry never holds
630
+ * the process open.
631
+ */
632
+ private retryIfRateLimited;
612
633
  private log;
613
634
  }
614
635
 
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 response = await fetch(`${this.endpoint}/v1/turns`, {
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 response = await fetch(`${this.endpoint}/v1/flags`, {
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 response = await fetch(`${this.endpoint}/v1/sessions/end`, {
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 response = await fetch(`${this.endpoint}/v1/outcome-events`, {
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,44 @@ var IngestClient = class {
154
172
  this.log(`[probe] outcome-event ingest error: ${msg}`);
155
173
  }
156
174
  }
175
+ /**
176
+ * Route a send response through the 429 retry policy. Non-429 responses
177
+ * pass through untouched. A 429 is re-sent exactly once after Retry-After
178
+ * seconds (default 1s when the header is missing or unparseable; clamped
179
+ * to [1, 60]s); the retry's response is returned as-is, so a second 429 is
180
+ * never retried again — callers log and drop it like any other failed
181
+ * send. Returns null when the retry was dropped at the pending-retry cap.
182
+ * The timer is unref'd (where supported) so a pending retry never holds
183
+ * the process open.
184
+ */
185
+ async retryIfRateLimited(response, resend) {
186
+ if (response.status !== 429) return response;
187
+ if (!this.rateLimitAnnounced) {
188
+ this.rateLimitAnnounced = true;
189
+ console.error(
190
+ "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"
191
+ );
192
+ } else {
193
+ this.log("[probe] ingest rate limited (429), retrying once");
194
+ }
195
+ if (this.pendingRetries >= MAX_PENDING_RETRIES) {
196
+ this.log("[probe] retry buffer full, dropped");
197
+ return null;
198
+ }
199
+ const raw = response.headers.get("retry-after");
200
+ const parsed = raw === null ? Number.NaN : Number(raw);
201
+ const seconds = Number.isFinite(parsed) ? Math.min(Math.max(parsed, 1), 60) : 1;
202
+ this.pendingRetries += 1;
203
+ try {
204
+ await new Promise((resolve) => {
205
+ const timer = setTimeout(resolve, seconds * 1e3);
206
+ timer.unref?.();
207
+ });
208
+ return await resend();
209
+ } finally {
210
+ this.pendingRetries -= 1;
211
+ }
212
+ }
157
213
  log(msg) {
158
214
  if (this.debug) {
159
215
  console.error(msg);