@cultured_computer/sight 0.3.2 → 0.3.3
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.cjs +119 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -2
- package/dist/index.d.ts +12 -2
- package/dist/index.js +119 -31
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -6,6 +6,9 @@ var api = require('@opentelemetry/api');
|
|
|
6
6
|
// src/client.ts
|
|
7
7
|
var DEFAULT_DASHBOARD_URL = "https://field.cultured.computer/dashboard";
|
|
8
8
|
var MAX_PENDING_RETRIES = 50;
|
|
9
|
+
var SEND_TIMEOUT_MS = 15e3;
|
|
10
|
+
var MAX_INFLIGHT = 64;
|
|
11
|
+
var MAX_TURN_BODY_BYTES = 9e5;
|
|
9
12
|
var IngestClient = class {
|
|
10
13
|
apiKey;
|
|
11
14
|
endpoint;
|
|
@@ -30,6 +33,31 @@ var IngestClient = class {
|
|
|
30
33
|
*/
|
|
31
34
|
rateLimitAnnounced = false;
|
|
32
35
|
pendingRetries = 0;
|
|
36
|
+
inFlight = 0;
|
|
37
|
+
shedAnnounced = false;
|
|
38
|
+
/**
|
|
39
|
+
* Every network send goes through here: adds the per-send timeout and
|
|
40
|
+
* the in-flight cap (audit B1). At the cap, the send is SHED — thrown
|
|
41
|
+
* into the caller's existing fire-and-log catch — rather than queued;
|
|
42
|
+
* the first shed per process warns on stderr, the rest debug-log.
|
|
43
|
+
*/
|
|
44
|
+
async guardedFetch(url, init) {
|
|
45
|
+
if (this.inFlight >= MAX_INFLIGHT) {
|
|
46
|
+
if (!this.shedAnnounced) {
|
|
47
|
+
this.shedAnnounced = true;
|
|
48
|
+
console.error(
|
|
49
|
+
`[sight] ${MAX_INFLIGHT} sends already in flight \u2014 shedding new sends (ingest slow or unreachable; this warning prints once)`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
throw new Error("in-flight cap reached \u2014 send shed");
|
|
53
|
+
}
|
|
54
|
+
this.inFlight++;
|
|
55
|
+
try {
|
|
56
|
+
return await fetch(url, { ...init, signal: AbortSignal.timeout(SEND_TIMEOUT_MS) });
|
|
57
|
+
} finally {
|
|
58
|
+
this.inFlight--;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
33
61
|
constructor(opts) {
|
|
34
62
|
this.apiKey = opts.apiKey;
|
|
35
63
|
this.endpoint = opts.endpoint.replace(/\/$/, "");
|
|
@@ -42,13 +70,20 @@ var IngestClient = class {
|
|
|
42
70
|
*/
|
|
43
71
|
async sendTurn(turn) {
|
|
44
72
|
try {
|
|
45
|
-
const
|
|
73
|
+
const body = JSON.stringify(turn);
|
|
74
|
+
if (body.length > MAX_TURN_BODY_BYTES) {
|
|
75
|
+
this.log(
|
|
76
|
+
`[sight] turn dropped: payload ${body.length} bytes exceeds the ${MAX_TURN_BODY_BYTES}-byte cap (session=${turn.sessionExternalId} turn=${turn.turnIndex}) \u2014 trim the payload you attach`
|
|
77
|
+
);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const send = () => this.guardedFetch(`${this.endpoint}/v1/turns`, {
|
|
46
81
|
method: "POST",
|
|
47
82
|
headers: {
|
|
48
83
|
Authorization: `Bearer ${this.apiKey}`,
|
|
49
84
|
"content-type": "application/json"
|
|
50
85
|
},
|
|
51
|
-
body
|
|
86
|
+
body
|
|
52
87
|
});
|
|
53
88
|
const response = await this.retryIfRateLimited(await send(), send);
|
|
54
89
|
if (response === null) return;
|
|
@@ -63,8 +98,8 @@ var IngestClient = class {
|
|
|
63
98
|
);
|
|
64
99
|
}
|
|
65
100
|
if (this.debug) {
|
|
66
|
-
const
|
|
67
|
-
this.log(`[sight] ingest ok id=${
|
|
101
|
+
const body2 = await response.json();
|
|
102
|
+
this.log(`[sight] ingest ok id=${body2.pendingId ?? "?"}`);
|
|
68
103
|
}
|
|
69
104
|
} catch (err) {
|
|
70
105
|
const msg = err instanceof Error ? err.message : String(err);
|
|
@@ -77,7 +112,7 @@ var IngestClient = class {
|
|
|
77
112
|
*/
|
|
78
113
|
async sendFlag(deploymentSlug, flag) {
|
|
79
114
|
try {
|
|
80
|
-
const send = () =>
|
|
115
|
+
const send = () => this.guardedFetch(`${this.endpoint}/v1/flags`, {
|
|
81
116
|
method: "POST",
|
|
82
117
|
headers: {
|
|
83
118
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -111,7 +146,7 @@ var IngestClient = class {
|
|
|
111
146
|
*/
|
|
112
147
|
async sendSessionEnd(deploymentSlug, end) {
|
|
113
148
|
try {
|
|
114
|
-
const send = () =>
|
|
149
|
+
const send = () => this.guardedFetch(`${this.endpoint}/v1/sessions/end`, {
|
|
115
150
|
method: "POST",
|
|
116
151
|
headers: {
|
|
117
152
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -143,7 +178,7 @@ var IngestClient = class {
|
|
|
143
178
|
*/
|
|
144
179
|
async sendOutcomeEvent(deploymentSlug, event) {
|
|
145
180
|
try {
|
|
146
|
-
const send = () =>
|
|
181
|
+
const send = () => this.guardedFetch(`${this.endpoint}/v1/outcome-events`, {
|
|
147
182
|
method: "POST",
|
|
148
183
|
headers: {
|
|
149
184
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -181,7 +216,7 @@ var IngestClient = class {
|
|
|
181
216
|
*/
|
|
182
217
|
async sendEnvironmentEvents(batch) {
|
|
183
218
|
try {
|
|
184
|
-
const send = () =>
|
|
219
|
+
const send = () => this.guardedFetch(`${this.endpoint}/v1/environment-events`, {
|
|
185
220
|
method: "POST",
|
|
186
221
|
headers: {
|
|
187
222
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -211,7 +246,7 @@ var IngestClient = class {
|
|
|
211
246
|
*/
|
|
212
247
|
async sendSpans(batch) {
|
|
213
248
|
try {
|
|
214
|
-
const send = () =>
|
|
249
|
+
const send = () => this.guardedFetch(`${this.endpoint}/v1/spans`, {
|
|
215
250
|
method: "POST",
|
|
216
251
|
headers: {
|
|
217
252
|
Authorization: `Bearer ${this.apiKey}`,
|
|
@@ -468,6 +503,16 @@ function collectClientContext(includes = DEFAULT_INCLUDES) {
|
|
|
468
503
|
return out;
|
|
469
504
|
}
|
|
470
505
|
|
|
506
|
+
// src/session-bounds.ts
|
|
507
|
+
var MAX_TRACKED_SESSIONS = 5e3;
|
|
508
|
+
function boundedSessionSet(sessions, key, session) {
|
|
509
|
+
if (!sessions.has(key) && sessions.size >= MAX_TRACKED_SESSIONS) {
|
|
510
|
+
const oldest = sessions.keys().next().value;
|
|
511
|
+
if (oldest !== void 0) sessions.delete(oldest);
|
|
512
|
+
}
|
|
513
|
+
sessions.set(key, session);
|
|
514
|
+
}
|
|
515
|
+
|
|
471
516
|
// src/hash.ts
|
|
472
517
|
async function hashEnvironmentConfig(input) {
|
|
473
518
|
if (typeof globalThis.crypto !== "undefined" && globalThis.crypto.subtle) {
|
|
@@ -694,7 +739,7 @@ function resolveSessionAndTurn(state2, sessionId) {
|
|
|
694
739
|
environmentConfigHash: null,
|
|
695
740
|
environmentConfigLabel: null
|
|
696
741
|
};
|
|
697
|
-
state2.sessions
|
|
742
|
+
boundedSessionSet(state2.sessions, sessionId, session);
|
|
698
743
|
}
|
|
699
744
|
const turnIndex = session.turnIndex;
|
|
700
745
|
session.turnIndex += 1;
|
|
@@ -712,10 +757,23 @@ var ProbeSpanProcessor = class {
|
|
|
712
757
|
}
|
|
713
758
|
state;
|
|
714
759
|
captureFn;
|
|
760
|
+
/**
|
|
761
|
+
* Re-point the processor at a new Probe state (or null after shutdown).
|
|
762
|
+
* A registered span processor cannot be removed from a provider, so
|
|
763
|
+
* re-init REBINDS instead of orphaning: without this, autocapture after
|
|
764
|
+
* shutdown()/re-init kept mutating the dead state's sessions map and
|
|
765
|
+
* stamping the old deployment slug (production-safety audit R1).
|
|
766
|
+
*/
|
|
767
|
+
rebind(state2, captureFn) {
|
|
768
|
+
this.state = state2;
|
|
769
|
+
if (captureFn) this.captureFn = captureFn;
|
|
770
|
+
}
|
|
715
771
|
onStart(_span, _parentContext) {
|
|
716
772
|
}
|
|
717
773
|
onEnd(span) {
|
|
718
774
|
try {
|
|
775
|
+
if (this.state === null) return;
|
|
776
|
+
const state2 = this.state;
|
|
719
777
|
const a = span.attributes;
|
|
720
778
|
if (isVercelAgentRollup(span.name, a)) return;
|
|
721
779
|
if (!isLLMCall(a)) return;
|
|
@@ -723,7 +781,7 @@ var ProbeSpanProcessor = class {
|
|
|
723
781
|
if (shouldSkipAsDuplicate(traceId)) return;
|
|
724
782
|
const sessionId = getCurrentSessionId();
|
|
725
783
|
if (!sessionId) {
|
|
726
|
-
if (
|
|
784
|
+
if (state2.config.debug) {
|
|
727
785
|
console.warn(
|
|
728
786
|
`[probe/otel] span with no session context; skipped (wrap LLM calls in Probe.withSession()). span=${span.name}`
|
|
729
787
|
);
|
|
@@ -731,11 +789,11 @@ var ProbeSpanProcessor = class {
|
|
|
731
789
|
return;
|
|
732
790
|
}
|
|
733
791
|
const { session, sessionExternalId, turnIndex, personaSlug } = resolveSessionAndTurn(
|
|
734
|
-
|
|
792
|
+
state2,
|
|
735
793
|
sessionId
|
|
736
794
|
);
|
|
737
795
|
const captured = {
|
|
738
|
-
deploymentSlug:
|
|
796
|
+
deploymentSlug: state2.config.deployment,
|
|
739
797
|
sessionExternalId,
|
|
740
798
|
turnIndex,
|
|
741
799
|
personaSlug: personaSlug ?? void 0,
|
|
@@ -745,14 +803,14 @@ var ProbeSpanProcessor = class {
|
|
|
745
803
|
await maybeAutoStampEnvironmentConfig(session, a);
|
|
746
804
|
await this.captureFn(captured);
|
|
747
805
|
})().catch((err) => {
|
|
748
|
-
if (
|
|
806
|
+
if (state2.config.debug) {
|
|
749
807
|
console.error(
|
|
750
808
|
`[probe/otel] capture error: ${err instanceof Error ? err.message : err}`
|
|
751
809
|
);
|
|
752
810
|
}
|
|
753
811
|
});
|
|
754
812
|
} catch (err) {
|
|
755
|
-
if (this.state
|
|
813
|
+
if (this.state?.config.debug) {
|
|
756
814
|
console.error(
|
|
757
815
|
`[probe/otel] onEnd error: ${err instanceof Error ? err.message : err}`
|
|
758
816
|
);
|
|
@@ -772,6 +830,7 @@ var ProbeSpanProcessor = class {
|
|
|
772
830
|
var installedProcessor = null;
|
|
773
831
|
function setupOtelProvider(state2, captureFn) {
|
|
774
832
|
if (installedProcessor !== null) {
|
|
833
|
+
installedProcessor.rebind(state2, captureFn);
|
|
775
834
|
return;
|
|
776
835
|
}
|
|
777
836
|
installedProcessor = new ProbeSpanProcessor(state2, captureFn);
|
|
@@ -787,6 +846,9 @@ function setupOtelProvider(state2, captureFn) {
|
|
|
787
846
|
provider.register();
|
|
788
847
|
});
|
|
789
848
|
}
|
|
849
|
+
function detachOtelState() {
|
|
850
|
+
installedProcessor?.rebind(null);
|
|
851
|
+
}
|
|
790
852
|
|
|
791
853
|
// src/instrumentation/openai.ts
|
|
792
854
|
var registered = false;
|
|
@@ -1384,6 +1446,17 @@ var DEFAULT_SPAN_ALIGNMENT = {
|
|
|
1384
1446
|
rule_version: "v1"
|
|
1385
1447
|
};
|
|
1386
1448
|
var state = null;
|
|
1449
|
+
function validateOrDrop(schema, input, method) {
|
|
1450
|
+
const result = schema.safeParse(input);
|
|
1451
|
+
if (!result.success) {
|
|
1452
|
+
const issue = result.error?.issues?.[0];
|
|
1453
|
+
console.warn(
|
|
1454
|
+
`[sight] ${method}: invalid input dropped \u2014 ${issue ? `${issue.path?.join(".") || "(root)"}: ${issue.message}` : "schema mismatch"}`
|
|
1455
|
+
);
|
|
1456
|
+
return null;
|
|
1457
|
+
}
|
|
1458
|
+
return result.data;
|
|
1459
|
+
}
|
|
1387
1460
|
var Probe = class {
|
|
1388
1461
|
/**
|
|
1389
1462
|
* Initialize the Probe SDK. Call once at process start. Subsequent calls are
|
|
@@ -1467,7 +1540,8 @@ var Probe = class {
|
|
|
1467
1540
|
console.warn("[sight] startSession called before init");
|
|
1468
1541
|
return;
|
|
1469
1542
|
}
|
|
1470
|
-
const validated = SessionStartSchema
|
|
1543
|
+
const validated = validateOrDrop(SessionStartSchema, input, "startSession");
|
|
1544
|
+
if (validated === null) return;
|
|
1471
1545
|
const existing = state.sessions.get(validated.externalId);
|
|
1472
1546
|
const now = Date.now();
|
|
1473
1547
|
const session = {
|
|
@@ -1490,7 +1564,7 @@ var Probe = class {
|
|
|
1490
1564
|
pendingUserOutcome: existing?.pendingUserOutcome ?? null
|
|
1491
1565
|
};
|
|
1492
1566
|
this.flushPreSessionSignals(validated.externalId, session);
|
|
1493
|
-
state.sessions
|
|
1567
|
+
boundedSessionSet(state.sessions, validated.externalId, session);
|
|
1494
1568
|
}
|
|
1495
1569
|
/**
|
|
1496
1570
|
* Move buffered pre-session interaction signals (recorded before any
|
|
@@ -1725,6 +1799,9 @@ var Probe = class {
|
|
|
1725
1799
|
if (!this.shouldSample(turn.sessionExternalId, turn.turnIndex)) {
|
|
1726
1800
|
return;
|
|
1727
1801
|
}
|
|
1802
|
+
if (turn.payload === void 0 || turn.payload === null) {
|
|
1803
|
+
turn = { ...turn, payload: {} };
|
|
1804
|
+
}
|
|
1728
1805
|
const propensity = turn.propensity ?? turn.payload.propensity;
|
|
1729
1806
|
let validPropensity = propensity;
|
|
1730
1807
|
if (propensity) {
|
|
@@ -1741,10 +1818,14 @@ var Probe = class {
|
|
|
1741
1818
|
const resolvedPersona = turn.personaSlug ?? sessionPersona ?? state.persona?.slug ?? void 0;
|
|
1742
1819
|
let signatureMatches = turn.signaturePhraseMatches;
|
|
1743
1820
|
if (!signatureMatches && resolvedPersona) {
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1821
|
+
try {
|
|
1822
|
+
const matcher = state.signatureMatchers.get(resolvedPersona);
|
|
1823
|
+
if (matcher && matcher.length > 0) {
|
|
1824
|
+
const text = this.extractTextForMatching(turn.payload);
|
|
1825
|
+
signatureMatches = text ? matchSignaturePhrases(text, matcher) : [];
|
|
1826
|
+
}
|
|
1827
|
+
} catch {
|
|
1828
|
+
signatureMatches = [];
|
|
1748
1829
|
}
|
|
1749
1830
|
}
|
|
1750
1831
|
let session = state.sessions.get(turn.sessionExternalId);
|
|
@@ -1768,7 +1849,7 @@ var Probe = class {
|
|
|
1768
1849
|
pendingUserInput: null,
|
|
1769
1850
|
pendingUserOutcome: null
|
|
1770
1851
|
};
|
|
1771
|
-
state.sessions
|
|
1852
|
+
boundedSessionSet(state.sessions, turn.sessionExternalId, session);
|
|
1772
1853
|
}
|
|
1773
1854
|
this.flushPreSessionSignals(turn.sessionExternalId, session);
|
|
1774
1855
|
let userInput = turn.userInput;
|
|
@@ -1851,7 +1932,8 @@ var Probe = class {
|
|
|
1851
1932
|
console.warn("[sight] emitOutcomeEvent called before init");
|
|
1852
1933
|
return;
|
|
1853
1934
|
}
|
|
1854
|
-
const validated = OutcomeEventEmitSchema
|
|
1935
|
+
const validated = validateOrDrop(OutcomeEventEmitSchema, input, "emitOutcomeEvent");
|
|
1936
|
+
if (validated === null) return;
|
|
1855
1937
|
await state.client.sendOutcomeEvent(state.config.deployment, validated);
|
|
1856
1938
|
}
|
|
1857
1939
|
/**
|
|
@@ -1872,8 +1954,9 @@ var Probe = class {
|
|
|
1872
1954
|
console.warn("[sight] captureEnvironmentEvent called before init");
|
|
1873
1955
|
return;
|
|
1874
1956
|
}
|
|
1875
|
-
const validated = EnvironmentEventInputSchema
|
|
1876
|
-
|
|
1957
|
+
const validated = validateOrDrop(EnvironmentEventInputSchema, event, "captureEnvironmentEvent");
|
|
1958
|
+
if (validated === null) return;
|
|
1959
|
+
const align = alignment ? validateOrDrop(AlignmentProvenanceSchema, alignment, "captureEnvironmentEvent") ?? DEFAULT_ENVIRONMENT_ALIGNMENT : DEFAULT_ENVIRONMENT_ALIGNMENT;
|
|
1877
1960
|
await state.client.sendEnvironmentEvents({ events: [validated], alignment: align });
|
|
1878
1961
|
}
|
|
1879
1962
|
/**
|
|
@@ -1883,12 +1966,13 @@ var Probe = class {
|
|
|
1883
1966
|
* generalized envelope and fire-and-log discipline as the single-event
|
|
1884
1967
|
* capture above.
|
|
1885
1968
|
*/
|
|
1886
|
-
static async captureEnvironmentEvents(events, alignment) {
|
|
1969
|
+
static async captureEnvironmentEvents(events, alignment = DEFAULT_ENVIRONMENT_ALIGNMENT) {
|
|
1887
1970
|
if (state === null) {
|
|
1888
1971
|
console.warn("[sight] captureEnvironmentEvents called before init");
|
|
1889
1972
|
return;
|
|
1890
1973
|
}
|
|
1891
|
-
const batch = EnvironmentEventsBatchSchema
|
|
1974
|
+
const batch = validateOrDrop(EnvironmentEventsBatchSchema, { events, alignment }, "captureEnvironmentEvents");
|
|
1975
|
+
if (batch === null) return;
|
|
1892
1976
|
await state.client.sendEnvironmentEvents(batch);
|
|
1893
1977
|
}
|
|
1894
1978
|
/**
|
|
@@ -1910,8 +1994,9 @@ var Probe = class {
|
|
|
1910
1994
|
console.warn("[sight] captureSpan called before init");
|
|
1911
1995
|
return;
|
|
1912
1996
|
}
|
|
1913
|
-
const validated = SpanInputSchema
|
|
1914
|
-
|
|
1997
|
+
const validated = validateOrDrop(SpanInputSchema, span, "captureSpan");
|
|
1998
|
+
if (validated === null) return;
|
|
1999
|
+
const align = alignment ? validateOrDrop(AlignmentProvenanceSchema, alignment, "captureSpan") ?? DEFAULT_SPAN_ALIGNMENT : DEFAULT_SPAN_ALIGNMENT;
|
|
1915
2000
|
await state.client.sendSpans({ spans: [validated], alignment: align });
|
|
1916
2001
|
}
|
|
1917
2002
|
/**
|
|
@@ -1928,7 +2013,8 @@ var Probe = class {
|
|
|
1928
2013
|
console.warn("[sight] captureSpans called before init");
|
|
1929
2014
|
return;
|
|
1930
2015
|
}
|
|
1931
|
-
const batch = SpansBatchSchema
|
|
2016
|
+
const batch = validateOrDrop(SpansBatchSchema, { spans, alignment: alignment ?? DEFAULT_SPAN_ALIGNMENT }, "captureSpans");
|
|
2017
|
+
if (batch === null) return;
|
|
1932
2018
|
await state.client.sendSpans(batch);
|
|
1933
2019
|
}
|
|
1934
2020
|
/**
|
|
@@ -1944,7 +2030,8 @@ var Probe = class {
|
|
|
1944
2030
|
console.warn("[sight] flagPersonaBreak called before init");
|
|
1945
2031
|
return;
|
|
1946
2032
|
}
|
|
1947
|
-
const validated = FlagPersonaBreakSchema
|
|
2033
|
+
const validated = validateOrDrop(FlagPersonaBreakSchema, input, "flagPersonaBreak");
|
|
2034
|
+
if (validated === null) return;
|
|
1948
2035
|
await state.client.sendFlag(state.config.deployment, validated);
|
|
1949
2036
|
}
|
|
1950
2037
|
/**
|
|
@@ -1972,6 +2059,7 @@ var Probe = class {
|
|
|
1972
2059
|
if (state === null) {
|
|
1973
2060
|
return;
|
|
1974
2061
|
}
|
|
2062
|
+
detachOtelState();
|
|
1975
2063
|
state = null;
|
|
1976
2064
|
}
|
|
1977
2065
|
// ── Internal helpers ─────────────────────────────────────────────────────
|