@cultured_computer/sight 0.3.0 → 0.3.2
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 +62 -6
- package/dist/index.cjs +243 -46
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +295 -19
- package/dist/index.d.ts +295 -19
- package/dist/index.js +243 -46
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ var IngestClient = class {
|
|
|
10
10
|
debug;
|
|
11
11
|
dashboardUrl;
|
|
12
12
|
/**
|
|
13
|
-
* First-trace permalink
|
|
13
|
+
* First-trace permalink: the first
|
|
14
14
|
* SUCCESSFUL sendTurn prints one stderr line with a dashboard permalink,
|
|
15
15
|
* then never again for the life of this client (one IngestClient per
|
|
16
16
|
* process — one `init` per process; a restart prints once more, accepted).
|
|
@@ -51,7 +51,7 @@ var IngestClient = class {
|
|
|
51
51
|
const response = await this.retryIfRateLimited(await send(), send);
|
|
52
52
|
if (response === null) return;
|
|
53
53
|
if (!response.ok) {
|
|
54
|
-
this.log(`[
|
|
54
|
+
this.log(`[sight] ingest failed status=${response.status}`);
|
|
55
55
|
return;
|
|
56
56
|
}
|
|
57
57
|
if (!this.firstCaptureAnnounced) {
|
|
@@ -62,16 +62,16 @@ var IngestClient = class {
|
|
|
62
62
|
}
|
|
63
63
|
if (this.debug) {
|
|
64
64
|
const body = await response.json();
|
|
65
|
-
this.log(`[
|
|
65
|
+
this.log(`[sight] ingest ok id=${body.pendingId ?? "?"}`);
|
|
66
66
|
}
|
|
67
67
|
} catch (err) {
|
|
68
68
|
const msg = err instanceof Error ? err.message : String(err);
|
|
69
|
-
this.log(`[
|
|
69
|
+
this.log(`[sight] ingest error: ${msg}`);
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
73
|
* Send a persona-break flag to the ingest API. Fire-and-log, same as sendTurn.
|
|
74
|
-
* Populates the `user_flagged_break` outcome column
|
|
74
|
+
* Populates the `user_flagged_break` outcome column on the dashboard.
|
|
75
75
|
*/
|
|
76
76
|
async sendFlag(deploymentSlug, flag) {
|
|
77
77
|
try {
|
|
@@ -92,15 +92,15 @@ var IngestClient = class {
|
|
|
92
92
|
const response = await this.retryIfRateLimited(await send(), send);
|
|
93
93
|
if (response === null) return;
|
|
94
94
|
if (!response.ok) {
|
|
95
|
-
this.log(`[
|
|
95
|
+
this.log(`[sight] flag ingest failed status=${response.status}`);
|
|
96
96
|
return;
|
|
97
97
|
}
|
|
98
98
|
if (this.debug) {
|
|
99
|
-
this.log(`[
|
|
99
|
+
this.log(`[sight] flag ingest ok session=${flag.sessionExternalId}`);
|
|
100
100
|
}
|
|
101
101
|
} catch (err) {
|
|
102
102
|
const msg = err instanceof Error ? err.message : String(err);
|
|
103
|
-
this.log(`[
|
|
103
|
+
this.log(`[sight] flag ingest error: ${msg}`);
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
/**
|
|
@@ -124,20 +124,20 @@ var IngestClient = class {
|
|
|
124
124
|
const response = await this.retryIfRateLimited(await send(), send);
|
|
125
125
|
if (response === null) return;
|
|
126
126
|
if (!response.ok) {
|
|
127
|
-
this.log(`[
|
|
127
|
+
this.log(`[sight] session-end ingest failed status=${response.status}`);
|
|
128
128
|
return;
|
|
129
129
|
}
|
|
130
130
|
if (this.debug) {
|
|
131
|
-
this.log(`[
|
|
131
|
+
this.log(`[sight] session-end ingest ok session=${end.externalId}`);
|
|
132
132
|
}
|
|
133
133
|
} catch (err) {
|
|
134
134
|
const msg = err instanceof Error ? err.message : String(err);
|
|
135
|
-
this.log(`[
|
|
135
|
+
this.log(`[sight] session-end ingest error: ${msg}`);
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
/**
|
|
139
139
|
* Send an outcome event to the ingest API. Fire-and-log. Populates
|
|
140
|
-
*
|
|
140
|
+
* the outcome-events store.
|
|
141
141
|
*/
|
|
142
142
|
async sendOutcomeEvent(deploymentSlug, event) {
|
|
143
143
|
try {
|
|
@@ -159,17 +159,17 @@ var IngestClient = class {
|
|
|
159
159
|
const response = await this.retryIfRateLimited(await send(), send);
|
|
160
160
|
if (response === null) return;
|
|
161
161
|
if (!response.ok) {
|
|
162
|
-
this.log(`[
|
|
162
|
+
this.log(`[sight] outcome-event ingest failed status=${response.status}`);
|
|
163
163
|
return;
|
|
164
164
|
}
|
|
165
165
|
if (this.debug) {
|
|
166
166
|
this.log(
|
|
167
|
-
`[
|
|
167
|
+
`[sight] outcome-event ingest ok session=${event.sessionExternalId} kind=${event.kind}`
|
|
168
168
|
);
|
|
169
169
|
}
|
|
170
170
|
} catch (err) {
|
|
171
171
|
const msg = err instanceof Error ? err.message : String(err);
|
|
172
|
-
this.log(`[
|
|
172
|
+
this.log(`[sight] outcome-event ingest error: ${msg}`);
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
175
|
/**
|
|
@@ -190,15 +190,45 @@ var IngestClient = class {
|
|
|
190
190
|
const response = await this.retryIfRateLimited(await send(), send);
|
|
191
191
|
if (response === null) return;
|
|
192
192
|
if (!response.ok) {
|
|
193
|
-
this.log(`[
|
|
193
|
+
this.log(`[sight] environment-events ingest failed status=${response.status}`);
|
|
194
194
|
return;
|
|
195
195
|
}
|
|
196
196
|
if (this.debug) {
|
|
197
|
-
this.log(`[
|
|
197
|
+
this.log(`[sight] environment-events ingest ok events=${batch.events.length}`);
|
|
198
198
|
}
|
|
199
199
|
} catch (err) {
|
|
200
200
|
const msg = err instanceof Error ? err.message : String(err);
|
|
201
|
-
this.log(`[
|
|
201
|
+
this.log(`[sight] environment-events ingest error: ${msg}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Send a span batch to the ingest API. Fire-and-log, same as sendTurn.
|
|
206
|
+
* Spans carry sub-turn structure (llm_call / tool_call / framework_step /
|
|
207
|
+
* custom); the worker resolves each span's session/turn and wires in-batch
|
|
208
|
+
* parent references.
|
|
209
|
+
*/
|
|
210
|
+
async sendSpans(batch) {
|
|
211
|
+
try {
|
|
212
|
+
const send = () => fetch(`${this.endpoint}/v1/spans`, {
|
|
213
|
+
method: "POST",
|
|
214
|
+
headers: {
|
|
215
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
216
|
+
"content-type": "application/json"
|
|
217
|
+
},
|
|
218
|
+
body: JSON.stringify(batch)
|
|
219
|
+
});
|
|
220
|
+
const response = await this.retryIfRateLimited(await send(), send);
|
|
221
|
+
if (response === null) return;
|
|
222
|
+
if (!response.ok) {
|
|
223
|
+
this.log(`[sight] spans ingest failed status=${response.status}`);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (this.debug) {
|
|
227
|
+
this.log(`[sight] spans ingest ok spans=${batch.spans.length}`);
|
|
228
|
+
}
|
|
229
|
+
} catch (err) {
|
|
230
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
231
|
+
this.log(`[sight] spans ingest error: ${msg}`);
|
|
202
232
|
}
|
|
203
233
|
}
|
|
204
234
|
/**
|
|
@@ -219,10 +249,10 @@ var IngestClient = class {
|
|
|
219
249
|
"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
250
|
);
|
|
221
251
|
} else {
|
|
222
|
-
this.log("[
|
|
252
|
+
this.log("[sight] ingest rate limited (429), retrying once");
|
|
223
253
|
}
|
|
224
254
|
if (this.pendingRetries >= MAX_PENDING_RETRIES) {
|
|
225
|
-
this.log("[
|
|
255
|
+
this.log("[sight] retry buffer full, dropped");
|
|
226
256
|
return null;
|
|
227
257
|
}
|
|
228
258
|
const raw = response.headers.get("retry-after");
|
|
@@ -290,8 +320,8 @@ var ProbeConfigSchema = z.object({
|
|
|
290
320
|
apiKey: z.string().min(1),
|
|
291
321
|
/** Deployment slug registered in the Probe backend. Required. */
|
|
292
322
|
deployment: z.string().min(1),
|
|
293
|
-
/** Ingest endpoint. Defaults to
|
|
294
|
-
endpoint: z.string().url().default("https://ingest.
|
|
323
|
+
/** Ingest endpoint. Defaults to the production ingest service. */
|
|
324
|
+
endpoint: z.string().url().default("https://fieldwork-ingest.fly.dev"),
|
|
295
325
|
/**
|
|
296
326
|
* Dashboard base URL used by the one-time first-capture announcement
|
|
297
327
|
* (the stderr permalink printed after the first successful send).
|
|
@@ -333,7 +363,7 @@ var SessionStartSchema = z.object({
|
|
|
333
363
|
*/
|
|
334
364
|
persona: z.string().min(1).optional(),
|
|
335
365
|
/**
|
|
336
|
-
* How the user got into this session
|
|
366
|
+
* How the user got into this session.
|
|
337
367
|
* Retention semantics differ radically between a resume and a fresh
|
|
338
368
|
* notification landing.
|
|
339
369
|
*/
|
|
@@ -350,8 +380,8 @@ var SessionStartSchema = z.object({
|
|
|
350
380
|
*/
|
|
351
381
|
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
352
382
|
/**
|
|
353
|
-
* RLDS episode-level provenance
|
|
354
|
-
*
|
|
383
|
+
* RLDS episode-level provenance. Powers the version behavioral diff
|
|
384
|
+
* on the dashboard. All optional.
|
|
355
385
|
* agentId — model/policy version
|
|
356
386
|
* experimentId — A/B arm or feature flag
|
|
357
387
|
* environmentConfigHash — SHA of prompt/config file(s)
|
|
@@ -364,7 +394,9 @@ var SessionStartSchema = z.object({
|
|
|
364
394
|
});
|
|
365
395
|
z.object({
|
|
366
396
|
externalId: z.string().min(1),
|
|
367
|
-
endedReason: z.enum(["natural", "user_closed", "timeout", "agent_error", "user_flagged", "crashed"]).optional()
|
|
397
|
+
endedReason: z.enum(["natural", "user_closed", "timeout", "agent_error", "user_flagged", "crashed"]).optional(),
|
|
398
|
+
outcome: z.string().min(1).max(64).optional(),
|
|
399
|
+
outcomeDetail: z.record(z.string(), z.unknown()).optional()
|
|
368
400
|
});
|
|
369
401
|
var FlagPersonaBreakSchema = z.object({
|
|
370
402
|
sessionExternalId: z.string().min(1),
|
|
@@ -901,7 +933,7 @@ function registerAllInstrumentations(state2, captureFn) {
|
|
|
901
933
|
if (!state2.config.debug) return;
|
|
902
934
|
for (const result of results) {
|
|
903
935
|
if (result.status === "rejected") {
|
|
904
|
-
console.error(`[
|
|
936
|
+
console.error(`[sight] adapter register rejected: ${result.reason}`);
|
|
905
937
|
}
|
|
906
938
|
}
|
|
907
939
|
});
|
|
@@ -941,6 +973,13 @@ function matchSignaturePhrases(text, compiled) {
|
|
|
941
973
|
}
|
|
942
974
|
return matches;
|
|
943
975
|
}
|
|
976
|
+
|
|
977
|
+
// ../shared/src/effectiveness-gap.ts
|
|
978
|
+
new Set(
|
|
979
|
+
"the a an to of and or is are was were do does did how what where when who why which can could should would may might i you your my me it its this that these those in on for with at by from as be been being am not no yes okay ok um uh hi hello please thanks thank".split(" ")
|
|
980
|
+
);
|
|
981
|
+
|
|
982
|
+
// ../shared/src/index.ts
|
|
944
983
|
var REACTION_REGISTERS = ["heavy", "comic", "edge"];
|
|
945
984
|
var InterpretiveRecordSchema = z.object({
|
|
946
985
|
// Open vocabulary: any string tag. Seeds live in GENERAL_SITUATION_TAGS /
|
|
@@ -1070,6 +1109,15 @@ z.object({
|
|
|
1070
1109
|
*/
|
|
1071
1110
|
externalUserId: z.string().min(1).optional(),
|
|
1072
1111
|
turnIndex: z.number().int().nonnegative(),
|
|
1112
|
+
/**
|
|
1113
|
+
* Caller-asserted turn timestamp (ISO). OPTIONAL and honored by the
|
|
1114
|
+
* worker: when present it becomes turns.started_at; when absent the
|
|
1115
|
+
* insert defaults to processing time. This is PROVENANCE, not
|
|
1116
|
+
* verification — intended for historical imports (ZVA telemetry, log
|
|
1117
|
+
* backfills, the datadump importer); live SDK traffic leaves it unset
|
|
1118
|
+
* (arrival time is the honest clock there).
|
|
1119
|
+
*/
|
|
1120
|
+
startedAt: z.string().datetime().optional(),
|
|
1073
1121
|
/**
|
|
1074
1122
|
* Persona slug active for this turn. Customer-supplied (from SDK persona
|
|
1075
1123
|
* resolution: turn override / session / init default). Optional; deployments
|
|
@@ -1214,6 +1262,100 @@ var EnvironmentEventsBatchSchema = z.object({
|
|
|
1214
1262
|
events: z.array(EnvironmentEventInputSchema).min(1).max(100),
|
|
1215
1263
|
alignment: AlignmentProvenanceSchema
|
|
1216
1264
|
});
|
|
1265
|
+
var SPAN_TYPES = ["llm_call", "tool_call", "framework_step", "custom"];
|
|
1266
|
+
var SpanInputSchema = z.object({
|
|
1267
|
+
deploymentSlug: z.string().min(1),
|
|
1268
|
+
sessionExternalId: z.string().min(1),
|
|
1269
|
+
/**
|
|
1270
|
+
* Turn the span belongs to. Optional: when omitted the worker resolves the
|
|
1271
|
+
* session's latest turn at/under the span's startedAt (falling back to the
|
|
1272
|
+
* session's most recent turn when startedAt is also absent — the outcome
|
|
1273
|
+
* events' rule). A span whose turn cannot be resolved is SKIPPED by the
|
|
1274
|
+
* worker (logged, batch still processed): spans never land unattributed.
|
|
1275
|
+
*/
|
|
1276
|
+
turnIndex: z.number().int().nonnegative().optional(),
|
|
1277
|
+
spanType: z.enum(SPAN_TYPES),
|
|
1278
|
+
/** Human/otel-style span name (tool name, model call label). */
|
|
1279
|
+
name: z.string().optional(),
|
|
1280
|
+
/**
|
|
1281
|
+
* Caller-asserted span start (ISO). OPTIONAL and honored like
|
|
1282
|
+
* TurnInput.startedAt: provenance, not verification — when absent the
|
|
1283
|
+
* worker stamps processing time.
|
|
1284
|
+
*/
|
|
1285
|
+
startedAt: z.string().datetime().optional(),
|
|
1286
|
+
endedAt: z.string().datetime().optional(),
|
|
1287
|
+
durationMs: z.number().int().nonnegative().optional(),
|
|
1288
|
+
model: z.string().optional(),
|
|
1289
|
+
provider: z.string().optional(),
|
|
1290
|
+
tokensInput: z.number().int().nonnegative().optional(),
|
|
1291
|
+
tokensOutput: z.number().int().nonnegative().optional(),
|
|
1292
|
+
costUsd: z.number().nonnegative().optional(),
|
|
1293
|
+
error: z.string().optional(),
|
|
1294
|
+
/**
|
|
1295
|
+
* OpenInference / OpenTelemetry gen_ai.*-style attributes. String values
|
|
1296
|
+
* are Presidio-scrubbed by the worker before storage (see the PII rule
|
|
1297
|
+
* above). Capped at 16k JSON chars per span.
|
|
1298
|
+
*/
|
|
1299
|
+
attributes: z.record(z.string(), z.unknown()).optional().refine((v) => v === void 0 || JSON.stringify(v).length <= 16e3, {
|
|
1300
|
+
message: "attributes exceed 16k JSON chars \u2014 spans are telemetry, not blob storage"
|
|
1301
|
+
}),
|
|
1302
|
+
/**
|
|
1303
|
+
* Caller-local span id, unique within the batch. Exists only so sibling
|
|
1304
|
+
* spans can name this span as their parentSpanId; never stored (the DB
|
|
1305
|
+
* assigns the real uuid).
|
|
1306
|
+
*/
|
|
1307
|
+
spanId: z.string().min(1).optional(),
|
|
1308
|
+
/**
|
|
1309
|
+
* Caller-local reference to another span's `spanId` IN THE SAME BATCH (NOT
|
|
1310
|
+
* a database uuid). The worker wires spans.parent_span_id when the parent
|
|
1311
|
+
* resolves within the batch, stores null otherwise.
|
|
1312
|
+
*/
|
|
1313
|
+
parentSpanId: z.string().min(1).optional()
|
|
1314
|
+
});
|
|
1315
|
+
var SpansBatchSchema = z.object({
|
|
1316
|
+
/** Spans are self-addressed: a batch may span sessions/deployments. */
|
|
1317
|
+
spans: z.array(SpanInputSchema).min(1).max(50),
|
|
1318
|
+
alignment: AlignmentProvenanceSchema
|
|
1319
|
+
});
|
|
1320
|
+
var DEFINITION_SLUG = /^[a-z][a-z0-9_-]{0,63}$/;
|
|
1321
|
+
var DEFINITION_VALUE = z.string().trim().min(1).max(64);
|
|
1322
|
+
z.object({
|
|
1323
|
+
/** Measurement family the definition belongs to (e.g. 'voice-conformance'). */
|
|
1324
|
+
family: z.string().regex(DEFINITION_SLUG, "family must be a lowercase slug (a-z, 0-9, _-)."),
|
|
1325
|
+
/** Slug within the family. family+name identifies the definition across versions. */
|
|
1326
|
+
name: z.string().regex(DEFINITION_SLUG, "name must be a lowercase slug (a-z, 0-9, _-)."),
|
|
1327
|
+
/** The elicitation prompt — the queue row's plain-language question. */
|
|
1328
|
+
question: z.string().trim().min(1).max(280),
|
|
1329
|
+
/** Difference metric for α (Krippendorff). Elicit v1: nominal | ordinal. */
|
|
1330
|
+
metric: z.enum(["nominal", "ordinal"]),
|
|
1331
|
+
/** The chips (closed vocabulary the rater chooses from). */
|
|
1332
|
+
values: z.array(DEFINITION_VALUE).min(2, "A rubric needs at least two chips.").max(9, "The queue is keyboard-first \u2014 at most 9 chips."),
|
|
1333
|
+
/** Rank order (lowest first) — required iff metric is 'ordinal'. */
|
|
1334
|
+
ordinalScale: z.array(DEFINITION_VALUE).min(2).max(9).optional()
|
|
1335
|
+
}).refine((input) => new Set(input.values).size === input.values.length, {
|
|
1336
|
+
message: "Chips must be unique.",
|
|
1337
|
+
path: ["values"]
|
|
1338
|
+
}).superRefine((input, ctx) => {
|
|
1339
|
+
if (input.metric === "ordinal") {
|
|
1340
|
+
const scale = input.ordinalScale;
|
|
1341
|
+
if (!scale) {
|
|
1342
|
+
ctx.addIssue({
|
|
1343
|
+
code: z.ZodIssueCode.custom,
|
|
1344
|
+
message: "ordinalScale is required when metric is ordinal.",
|
|
1345
|
+
path: ["ordinalScale"]
|
|
1346
|
+
});
|
|
1347
|
+
return;
|
|
1348
|
+
}
|
|
1349
|
+
const same = scale.length === input.values.length && [...scale].sort().join("") === [...input.values].sort().join("");
|
|
1350
|
+
if (!same) {
|
|
1351
|
+
ctx.addIssue({
|
|
1352
|
+
code: z.ZodIssueCode.custom,
|
|
1353
|
+
message: "ordinalScale must be exactly the chips in rank order (lowest first).",
|
|
1354
|
+
path: ["ordinalScale"]
|
|
1355
|
+
});
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
});
|
|
1217
1359
|
z.object({
|
|
1218
1360
|
deploymentSlug: z.string().min(1),
|
|
1219
1361
|
sessionExternalId: z.string().min(1),
|
|
@@ -1235,6 +1377,10 @@ var DEFAULT_ENVIRONMENT_ALIGNMENT = {
|
|
|
1235
1377
|
clock_source: "sdk",
|
|
1236
1378
|
rule_version: "v1"
|
|
1237
1379
|
};
|
|
1380
|
+
var DEFAULT_SPAN_ALIGNMENT = {
|
|
1381
|
+
clock_source: "sdk",
|
|
1382
|
+
rule_version: "v1"
|
|
1383
|
+
};
|
|
1238
1384
|
var state = null;
|
|
1239
1385
|
var Probe = class {
|
|
1240
1386
|
/**
|
|
@@ -1243,7 +1389,7 @@ var Probe = class {
|
|
|
1243
1389
|
*/
|
|
1244
1390
|
static init(config) {
|
|
1245
1391
|
if (state !== null) {
|
|
1246
|
-
console.warn("[
|
|
1392
|
+
console.warn("[sight] already initialized; call shutdown() first");
|
|
1247
1393
|
return;
|
|
1248
1394
|
}
|
|
1249
1395
|
const validated = ProbeConfigSchema.parse(config);
|
|
@@ -1281,7 +1427,7 @@ var Probe = class {
|
|
|
1281
1427
|
*/
|
|
1282
1428
|
static async withSession(sessionId, fn) {
|
|
1283
1429
|
if (state === null) {
|
|
1284
|
-
console.warn("[
|
|
1430
|
+
console.warn("[sight] withSession called before init");
|
|
1285
1431
|
return fn();
|
|
1286
1432
|
}
|
|
1287
1433
|
return withSession(sessionId, fn);
|
|
@@ -1293,7 +1439,7 @@ var Probe = class {
|
|
|
1293
1439
|
*/
|
|
1294
1440
|
static identifyPersona(persona) {
|
|
1295
1441
|
if (state === null) {
|
|
1296
|
-
console.warn("[
|
|
1442
|
+
console.warn("[sight] identifyPersona called before init");
|
|
1297
1443
|
return;
|
|
1298
1444
|
}
|
|
1299
1445
|
state.persona = persona;
|
|
@@ -1316,7 +1462,7 @@ var Probe = class {
|
|
|
1316
1462
|
*/
|
|
1317
1463
|
static startSession(input) {
|
|
1318
1464
|
if (state === null) {
|
|
1319
|
-
console.warn("[
|
|
1465
|
+
console.warn("[sight] startSession called before init");
|
|
1320
1466
|
return;
|
|
1321
1467
|
}
|
|
1322
1468
|
const validated = SessionStartSchema.parse(input);
|
|
@@ -1387,7 +1533,7 @@ var Probe = class {
|
|
|
1387
1533
|
* for the diff table axis (a card keyed by a3f9c2… is unreadable).
|
|
1388
1534
|
*
|
|
1389
1535
|
* Ships as a helper because inconsistent hashing across restarts breaks
|
|
1390
|
-
*
|
|
1536
|
+
* The version behavioral diff — every customer would
|
|
1391
1537
|
* hash their own way and version-diff would become noise.
|
|
1392
1538
|
*
|
|
1393
1539
|
* The same hashing (src/hash.ts) backs the OTel processor's automatic
|
|
@@ -1419,7 +1565,18 @@ var Probe = class {
|
|
|
1419
1565
|
if (state === null) return;
|
|
1420
1566
|
const externalIdStr = typeof externalId === "string" ? externalId : externalId.externalId;
|
|
1421
1567
|
const endedReason = typeof externalId === "string" ? void 0 : externalId.endedReason;
|
|
1422
|
-
|
|
1568
|
+
const declared = typeof externalId === "string" ? void 0 : externalId.outcome;
|
|
1569
|
+
if (declared) {
|
|
1570
|
+
await this.emitOutcomeEvent({
|
|
1571
|
+
sessionExternalId: externalIdStr,
|
|
1572
|
+
kind: declared,
|
|
1573
|
+
detail: {
|
|
1574
|
+
declaredAt: "endSession",
|
|
1575
|
+
...typeof externalId === "object" && externalId.outcomeDetail ? externalId.outcomeDetail : {}
|
|
1576
|
+
}
|
|
1577
|
+
});
|
|
1578
|
+
}
|
|
1579
|
+
if (!declared && state.outcomeHook) {
|
|
1423
1580
|
try {
|
|
1424
1581
|
const result = await state.outcomeHook.resolve(externalIdStr);
|
|
1425
1582
|
if (result && state !== null) {
|
|
@@ -1435,7 +1592,7 @@ var Probe = class {
|
|
|
1435
1592
|
}
|
|
1436
1593
|
} catch (err) {
|
|
1437
1594
|
console.warn(
|
|
1438
|
-
"[
|
|
1595
|
+
"[sight] outcome hook resolve failed:",
|
|
1439
1596
|
err instanceof Error ? err.message : err
|
|
1440
1597
|
);
|
|
1441
1598
|
}
|
|
@@ -1443,12 +1600,12 @@ var Probe = class {
|
|
|
1443
1600
|
if (state === null) return;
|
|
1444
1601
|
if (endedReason) {
|
|
1445
1602
|
await state.client.sendSessionEnd(state.config.deployment, { externalId: externalIdStr, endedReason }).catch((err) => {
|
|
1446
|
-
console.warn("[
|
|
1603
|
+
console.warn("[sight] sendSessionEnd failed:", err instanceof Error ? err.message : err);
|
|
1447
1604
|
});
|
|
1448
1605
|
}
|
|
1449
1606
|
state.sessions.delete(externalIdStr);
|
|
1450
1607
|
}
|
|
1451
|
-
// ── User-side helpers
|
|
1608
|
+
// ── User-side helpers ──────────────────────────────────────────────────
|
|
1452
1609
|
// These accumulate user-side signals during a turn; captureTurn folds the
|
|
1453
1610
|
// accumulated state into TurnInput.userInput / userOutcome before send.
|
|
1454
1611
|
// Signals that arrive before any session entry exists (typing starts
|
|
@@ -1541,7 +1698,7 @@ var Probe = class {
|
|
|
1541
1698
|
*/
|
|
1542
1699
|
static wireOutcomeHook(hook) {
|
|
1543
1700
|
if (state === null) {
|
|
1544
|
-
console.warn("[
|
|
1701
|
+
console.warn("[sight] wireOutcomeHook called before init");
|
|
1545
1702
|
return;
|
|
1546
1703
|
}
|
|
1547
1704
|
state.outcomeHook = hook;
|
|
@@ -1572,7 +1729,7 @@ var Probe = class {
|
|
|
1572
1729
|
const contract = validatePropensityContract(propensity);
|
|
1573
1730
|
if (!contract.ok) {
|
|
1574
1731
|
console.warn(
|
|
1575
|
-
`[
|
|
1732
|
+
`[sight] propensity contract violation (${contract.class}): ${contract.reason}`
|
|
1576
1733
|
);
|
|
1577
1734
|
turn.payload.propensity = void 0;
|
|
1578
1735
|
validPropensity = void 0;
|
|
@@ -1671,7 +1828,7 @@ var Probe = class {
|
|
|
1671
1828
|
propensity: validPropensity,
|
|
1672
1829
|
// Forward RLDS provenance from the local session entry (populated
|
|
1673
1830
|
// by Probe.startSession). Worker persists at first-turn session
|
|
1674
|
-
// create; nulls stay null.
|
|
1831
|
+
// create; nulls stay null.
|
|
1675
1832
|
agentId: session.agentId ?? void 0,
|
|
1676
1833
|
experimentId: session.experimentId ?? void 0,
|
|
1677
1834
|
environmentConfigHash: session.environmentConfigHash ?? void 0,
|
|
@@ -1680,7 +1837,7 @@ var Probe = class {
|
|
|
1680
1837
|
}
|
|
1681
1838
|
/**
|
|
1682
1839
|
* Emit a custom outcome event on a session. Populates probe_outcome_events
|
|
1683
|
-
* with the customer-supplied `kind` string
|
|
1840
|
+
* with the customer-supplied `kind` string. Use for
|
|
1684
1841
|
* customer-defined kinds like "purchase_complete", "handoff_to_human",
|
|
1685
1842
|
* "character_break_severe". Universal seed kinds (break, escalation,
|
|
1686
1843
|
* resolution, thumbs_*, rating_*) are also emitted by worker-side
|
|
@@ -1689,14 +1846,14 @@ var Probe = class {
|
|
|
1689
1846
|
*/
|
|
1690
1847
|
static async emitOutcomeEvent(input) {
|
|
1691
1848
|
if (state === null) {
|
|
1692
|
-
console.warn("[
|
|
1849
|
+
console.warn("[sight] emitOutcomeEvent called before init");
|
|
1693
1850
|
return;
|
|
1694
1851
|
}
|
|
1695
1852
|
const validated = OutcomeEventEmitSchema.parse(input);
|
|
1696
1853
|
await state.client.sendOutcomeEvent(state.config.deployment, validated);
|
|
1697
1854
|
}
|
|
1698
1855
|
/**
|
|
1699
|
-
* Capture one environment event against a session
|
|
1856
|
+
* Capture one environment event against a session. The
|
|
1700
1857
|
* envelope is generalized: `source_type` and `event` are free strings whose
|
|
1701
1858
|
* meaning is defined per deployment — the instrument stores the event and
|
|
1702
1859
|
* attaches it to the session's record by time (the next turn's state
|
|
@@ -1710,7 +1867,7 @@ var Probe = class {
|
|
|
1710
1867
|
*/
|
|
1711
1868
|
static async captureEnvironmentEvent(event, alignment) {
|
|
1712
1869
|
if (state === null) {
|
|
1713
|
-
console.warn("[
|
|
1870
|
+
console.warn("[sight] captureEnvironmentEvent called before init");
|
|
1714
1871
|
return;
|
|
1715
1872
|
}
|
|
1716
1873
|
const validated = EnvironmentEventInputSchema.parse(event);
|
|
@@ -1726,23 +1883,63 @@ var Probe = class {
|
|
|
1726
1883
|
*/
|
|
1727
1884
|
static async captureEnvironmentEvents(events, alignment) {
|
|
1728
1885
|
if (state === null) {
|
|
1729
|
-
console.warn("[
|
|
1886
|
+
console.warn("[sight] captureEnvironmentEvents called before init");
|
|
1730
1887
|
return;
|
|
1731
1888
|
}
|
|
1732
1889
|
const batch = EnvironmentEventsBatchSchema.parse({ events, alignment });
|
|
1733
1890
|
await state.client.sendEnvironmentEvents(batch);
|
|
1734
1891
|
}
|
|
1892
|
+
/**
|
|
1893
|
+
* Capture one sub-turn span against a session: one LLM call, one tool
|
|
1894
|
+
* call, one framework step, or a custom unit inside a turn. The span lands
|
|
1895
|
+
* on the turn named by `turnIndex`, or — when omitted — on the session's
|
|
1896
|
+
* latest turn at/under the span's `startedAt` (most recent turn when
|
|
1897
|
+
* `startedAt` is also absent). A span whose turn cannot be resolved is
|
|
1898
|
+
* dropped server-side; spans never land unattributed. Fire-and-log, like
|
|
1899
|
+
* captureTurn: errors are debug-logged, never thrown.
|
|
1900
|
+
*
|
|
1901
|
+
* `attributes` are metadata only (tool names, argument shapes) — never
|
|
1902
|
+
* free-text conversation content. Alignment defaults to the SDK's own
|
|
1903
|
+
* clock claim; deployments with a stricter clock contract (or batches, or
|
|
1904
|
+
* parent references between spans) should call captureSpans.
|
|
1905
|
+
*/
|
|
1906
|
+
static async captureSpan(span, alignment) {
|
|
1907
|
+
if (state === null) {
|
|
1908
|
+
console.warn("[sight] captureSpan called before init");
|
|
1909
|
+
return;
|
|
1910
|
+
}
|
|
1911
|
+
const validated = SpanInputSchema.parse(span);
|
|
1912
|
+
const align = alignment ? AlignmentProvenanceSchema.parse(alignment) : DEFAULT_SPAN_ALIGNMENT;
|
|
1913
|
+
await state.client.sendSpans({ spans: [validated], alignment: align });
|
|
1914
|
+
}
|
|
1915
|
+
/**
|
|
1916
|
+
* Capture a batch of spans with explicit alignment provenance
|
|
1917
|
+
* (clock_source, skew_bound_ms, rule_version). Batches are capped at 50
|
|
1918
|
+
* spans and may span sessions — every span is self-addressed. Parent
|
|
1919
|
+
* structure rides the batch: a span's `parentSpanId` names another span's
|
|
1920
|
+
* caller-local `spanId` within the same batch and is wired server-side
|
|
1921
|
+
* (unresolvable references land null). Same fire-and-log discipline as the
|
|
1922
|
+
* single-span capture above.
|
|
1923
|
+
*/
|
|
1924
|
+
static async captureSpans(spans, alignment) {
|
|
1925
|
+
if (state === null) {
|
|
1926
|
+
console.warn("[sight] captureSpans called before init");
|
|
1927
|
+
return;
|
|
1928
|
+
}
|
|
1929
|
+
const batch = SpansBatchSchema.parse({ spans, alignment });
|
|
1930
|
+
await state.client.sendSpans(batch);
|
|
1931
|
+
}
|
|
1735
1932
|
/**
|
|
1736
1933
|
* Flag a persona break for a session. Wire this to the customer's in-product
|
|
1737
1934
|
* "that didn't sound like you" UX. Populates the `user_flagged_break`
|
|
1738
|
-
* outcome column
|
|
1935
|
+
* outcome column on the dashboard.
|
|
1739
1936
|
*
|
|
1740
1937
|
* If `turnIndex` is omitted, the flag attaches to the session's most
|
|
1741
1938
|
* recent turn.
|
|
1742
1939
|
*/
|
|
1743
1940
|
static async flagPersonaBreak(input) {
|
|
1744
1941
|
if (state === null) {
|
|
1745
|
-
console.warn("[
|
|
1942
|
+
console.warn("[sight] flagPersonaBreak called before init");
|
|
1746
1943
|
return;
|
|
1747
1944
|
}
|
|
1748
1945
|
const validated = FlagPersonaBreakSchema.parse(input);
|