@smthrs/observability 0.33.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/LICENSE +21 -0
- package/docker-compose.otel.yml +104 -0
- package/grafana/dashboards/smithers-dashboard.json +453 -0
- package/grafana/provisioning/dashboards/dashboards.yml +12 -0
- package/grafana/provisioning/datasources/datasources.yml +49 -0
- package/loki/loki-config.yaml +35 -0
- package/otel-collector-config.yml +38 -0
- package/package.json +47 -0
- package/prometheus/prometheus.yml +8 -0
- package/src/MetricName.ts +1 -0
- package/src/MetricsServiceLive.js +9 -0
- package/src/README.md +28 -0
- package/src/ResolvedSmithersObservabilityOptions.ts +12 -0
- package/src/SmithersEvent.ts +773 -0
- package/src/SmithersLogFormat.ts +1 -0
- package/src/SmithersMetricDefinition.ts +15 -0
- package/src/SmithersMetricType.ts +1 -0
- package/src/SmithersMetricUnit.ts +1 -0
- package/src/SmithersObservability.js +15 -0
- package/src/SmithersObservabilityOptions.ts +12 -0
- package/src/SmithersObservabilityService.ts +13 -0
- package/src/_coreCorrelation/CorrelationContext.ts +10 -0
- package/src/_coreCorrelation/CorrelationContextLive.js +11 -0
- package/src/_coreCorrelation/CorrelationContextService.js +15 -0
- package/src/_coreCorrelation/CorrelationContextServiceShape.ts +12 -0
- package/src/_coreCorrelation/CorrelationPatch.ts +3 -0
- package/src/_coreCorrelation/README.md +20 -0
- package/src/_coreCorrelation/_correlationStorage.js +4 -0
- package/src/_coreCorrelation/correlationContextFiberRef.js +5 -0
- package/src/_coreCorrelation/correlationContextToLogAnnotations.js +18 -0
- package/src/_coreCorrelation/getCurrentCorrelationContext.js +9 -0
- package/src/_coreCorrelation/getCurrentCorrelationContextEffect.js +11 -0
- package/src/_coreCorrelation/index.js +14 -0
- package/src/_coreCorrelation/mergeCorrelationContext.js +51 -0
- package/src/_coreCorrelation/runWithCorrelationContext.js +14 -0
- package/src/_coreCorrelation/updateCurrentCorrelationContext.js +17 -0
- package/src/_coreCorrelation/withCorrelationContext.js +38 -0
- package/src/_coreCorrelation/withCurrentCorrelationContext.js +11 -0
- package/src/_coreMetrics.js +14 -0
- package/src/_coreMetricsShape.ts +44 -0
- package/src/_corePrometheus.js +91 -0
- package/src/_corePrometheusShape.ts +11 -0
- package/src/_coreTracing.js +132 -0
- package/src/_coreTracingShape.ts +17 -0
- package/src/_otelLogBuilders.js +102 -0
- package/src/_sessionFileResolvers.js +164 -0
- package/src/_smithersSpanAttributeAliases.js +19 -0
- package/src/_smithersTraceSpanStorage.js +3 -0
- package/src/_traceEventNormalizers.js +499 -0
- package/src/_traceRedaction.js +162 -0
- package/src/agentSessionEventToOtelLogRecord.js +55 -0
- package/src/agentTrace.ts +144 -0
- package/src/agentTraceCapabilities.js +151 -0
- package/src/annotateSmithersTrace.js +11 -0
- package/src/canonicalTraceEventToOtelLogRecord.js +55 -0
- package/src/correlation.js +56 -0
- package/src/createSmithersObservabilityLayer.js +59 -0
- package/src/createSmithersOtelLayer.js +23 -0
- package/src/createSmithersRuntimeLayer.js +2 -0
- package/src/detectAgentFamily.js +26 -0
- package/src/detectCaptureMode.js +24 -0
- package/src/emitOtelLogRecord.js +19 -0
- package/src/getCurrentSmithersTraceAnnotations.js +14 -0
- package/src/getCurrentSmithersTraceSpan.js +7 -0
- package/src/index.d.ts +1321 -0
- package/src/index.js +136 -0
- package/src/kindPhase.js +17 -0
- package/src/logging.js +248 -0
- package/src/makeSmithersSpanAttributes.js +20 -0
- package/src/metrics/README.md +22 -0
- package/src/metrics/SmithersMetricDefinition.ts +17 -0
- package/src/metrics/SmithersMetricType.ts +1 -0
- package/src/metrics/SmithersMetricUnit.ts +1 -0
- package/src/metrics/_asyncExternalWaitCounts.js +4 -0
- package/src/metrics/_buckets.js +37 -0
- package/src/metrics/_incrementGauge.js +11 -0
- package/src/metrics/_processStartMs.js +1 -0
- package/src/metrics/activeNodes.js +2 -0
- package/src/metrics/activeRuns.js +2 -0
- package/src/metrics/agentActionsTotal.js +2 -0
- package/src/metrics/agentDurationMs.js +3 -0
- package/src/metrics/agentErrorsTotal.js +2 -0
- package/src/metrics/agentEventsTotal.js +2 -0
- package/src/metrics/agentInvocationsTotal.js +2 -0
- package/src/metrics/agentRetriesTotal.js +2 -0
- package/src/metrics/agentSessionsTotal.js +2 -0
- package/src/metrics/agentTokensTotal.js +2 -0
- package/src/metrics/alertDeliveriesAttempted.js +2 -0
- package/src/metrics/alertDeliveriesSuppressed.js +2 -0
- package/src/metrics/alertsAcknowledgedTotal.js +2 -0
- package/src/metrics/alertsActive.js +2 -0
- package/src/metrics/alertsEscalatedTotal.js +2 -0
- package/src/metrics/alertsFiredTotal.js +2 -0
- package/src/metrics/alertsReopenedTotal.js +2 -0
- package/src/metrics/alertsResolvedTotal.js +2 -0
- package/src/metrics/alertsSilencedTotal.js +2 -0
- package/src/metrics/approvalPending.js +2 -0
- package/src/metrics/approvalWaitDuration.js +5 -0
- package/src/metrics/approvalsDenied.js +2 -0
- package/src/metrics/approvalsGranted.js +2 -0
- package/src/metrics/approvalsRequested.js +2 -0
- package/src/metrics/attemptDuration.js +3 -0
- package/src/metrics/attentionBacklog.js +2 -0
- package/src/metrics/cacheHits.js +2 -0
- package/src/metrics/cacheMisses.js +2 -0
- package/src/metrics/dbQueryDuration.js +3 -0
- package/src/metrics/dbRetries.js +2 -0
- package/src/metrics/dbTransactionDuration.js +3 -0
- package/src/metrics/dbTransactionRetries.js +2 -0
- package/src/metrics/dbTransactionRollbacks.js +2 -0
- package/src/metrics/devtoolsActiveSubscribers.js +2 -0
- package/src/metrics/devtoolsBackpressureDisconnectTotal.js +2 -0
- package/src/metrics/devtoolsDeltaBuildMs.js +3 -0
- package/src/metrics/devtoolsEventBytes.js +3 -0
- package/src/metrics/devtoolsEventTotal.js +2 -0
- package/src/metrics/devtoolsSnapshotBuildMs.js +5 -0
- package/src/metrics/devtoolsSubscribeTotal.js +2 -0
- package/src/metrics/errorsTotal.js +2 -0
- package/src/metrics/eventsEmittedTotal.js +2 -0
- package/src/metrics/externalWaitAsyncPending.js +2 -0
- package/src/metrics/gatewayApprovalDecisionsTotal.js +2 -0
- package/src/metrics/gatewayAuthEventsTotal.js +2 -0
- package/src/metrics/gatewayConnectionsActive.js +2 -0
- package/src/metrics/gatewayConnectionsClosedTotal.js +2 -0
- package/src/metrics/gatewayConnectionsTotal.js +2 -0
- package/src/metrics/gatewayCronTriggersTotal.js +2 -0
- package/src/metrics/gatewayErrorsTotal.js +2 -0
- package/src/metrics/gatewayHeartbeatTicksTotal.js +2 -0
- package/src/metrics/gatewayMessagesReceivedTotal.js +2 -0
- package/src/metrics/gatewayMessagesSentTotal.js +2 -0
- package/src/metrics/gatewayRpcCallsTotal.js +2 -0
- package/src/metrics/gatewayRpcDuration.js +3 -0
- package/src/metrics/gatewayRunEventBackpressureDisconnectTotal.js +4 -0
- package/src/metrics/gatewayRunsCompletedTotal.js +2 -0
- package/src/metrics/gatewayRunsStartedTotal.js +2 -0
- package/src/metrics/gatewaySignalsTotal.js +2 -0
- package/src/metrics/gatewayWebhooksReceivedTotal.js +2 -0
- package/src/metrics/gatewayWebhooksRejectedTotal.js +2 -0
- package/src/metrics/gatewayWebhooksVerifiedTotal.js +2 -0
- package/src/metrics/heartbeatDataSizeBytes.js +5 -0
- package/src/metrics/heartbeatIntervalMs.js +3 -0
- package/src/metrics/hotReloadDuration.js +3 -0
- package/src/metrics/hotReloadFailures.js +2 -0
- package/src/metrics/hotReloads.js +2 -0
- package/src/metrics/httpRequestDuration.js +3 -0
- package/src/metrics/httpRequests.js +2 -0
- package/src/metrics/index.d.ts +1163 -0
- package/src/metrics/index.js +168 -0
- package/src/metrics/memoryFactReads.js +3 -0
- package/src/metrics/memoryFactWrites.js +3 -0
- package/src/metrics/memoryMessageSaves.js +3 -0
- package/src/metrics/memoryRecallDuration.js +11 -0
- package/src/metrics/memoryRecallQueries.js +3 -0
- package/src/metrics/metricsServiceAdapter.js +181 -0
- package/src/metrics/nodeDuration.js +3 -0
- package/src/metrics/nodeRetriesTotal.js +2 -0
- package/src/metrics/nodesFailed.js +2 -0
- package/src/metrics/nodesFinished.js +2 -0
- package/src/metrics/nodesStarted.js +2 -0
- package/src/metrics/openApiToolCallErrorsTotal.js +3 -0
- package/src/metrics/openApiToolCallsTotal.js +3 -0
- package/src/metrics/openApiToolDuration.js +9 -0
- package/src/metrics/processHeapUsedBytes.js +2 -0
- package/src/metrics/processMemoryRssBytes.js +2 -0
- package/src/metrics/processUptimeSeconds.js +2 -0
- package/src/metrics/promptSizeBytes.js +3 -0
- package/src/metrics/replaysStarted.js +3 -0
- package/src/metrics/responseSizeBytes.js +3 -0
- package/src/metrics/rewindDurationMs.js +3 -0
- package/src/metrics/rewindFramesDeleted.js +3 -0
- package/src/metrics/rewindRollbackTotal.js +2 -0
- package/src/metrics/rewindSandboxesReverted.js +3 -0
- package/src/metrics/rewindTotal.js +2 -0
- package/src/metrics/runDuration.js +3 -0
- package/src/metrics/runForksCreated.js +3 -0
- package/src/metrics/runsAncestryDepth.js +3 -0
- package/src/metrics/runsCancelledTotal.js +2 -0
- package/src/metrics/runsCarriedStateBytes.js +5 -0
- package/src/metrics/runsContinuedTotal.js +2 -0
- package/src/metrics/runsFailedTotal.js +2 -0
- package/src/metrics/runsFinishedTotal.js +2 -0
- package/src/metrics/runsResumedTotal.js +2 -0
- package/src/metrics/runsTotal.js +2 -0
- package/src/metrics/sandboxActive.js +2 -0
- package/src/metrics/sandboxBundleSizeBytes.js +5 -0
- package/src/metrics/sandboxCompletedTotal.js +2 -0
- package/src/metrics/sandboxCreatedTotal.js +2 -0
- package/src/metrics/sandboxDurationMs.js +3 -0
- package/src/metrics/sandboxPatchCount.js +3 -0
- package/src/metrics/sandboxTransportDurationMs.js +5 -0
- package/src/metrics/schedulerConcurrencyUtilization.js +2 -0
- package/src/metrics/schedulerQueueDepth.js +2 -0
- package/src/metrics/schedulerWaitDuration.js +5 -0
- package/src/metrics/scorerDuration.js +9 -0
- package/src/metrics/scorerEventsFailed.js +2 -0
- package/src/metrics/scorerEventsFinished.js +2 -0
- package/src/metrics/scorerEventsStarted.js +2 -0
- package/src/metrics/scorersFailed.js +3 -0
- package/src/metrics/scorersFinished.js +3 -0
- package/src/metrics/scorersStarted.js +3 -0
- package/src/metrics/smithersMetricCatalog.js +893 -0
- package/src/metrics/smithersMetricCatalogByKey.js +2 -0
- package/src/metrics/smithersMetricCatalogByName.js +2 -0
- package/src/metrics/smithersMetricCatalogByPrometheusName.js +4 -0
- package/src/metrics/snapshotDuration.js +9 -0
- package/src/metrics/snapshotsCaptured.js +3 -0
- package/src/metrics/supervisorPollDuration.js +5 -0
- package/src/metrics/supervisorPollsTotal.js +2 -0
- package/src/metrics/supervisorResumeLag.js +5 -0
- package/src/metrics/supervisorResumedTotal.js +2 -0
- package/src/metrics/supervisorSkippedTotal.js +2 -0
- package/src/metrics/supervisorStaleDetected.js +2 -0
- package/src/metrics/taskHeartbeatTimeoutTotal.js +2 -0
- package/src/metrics/taskHeartbeatsTotal.js +2 -0
- package/src/metrics/timerDelayDuration.js +3 -0
- package/src/metrics/timersCancelled.js +2 -0
- package/src/metrics/timersCreated.js +2 -0
- package/src/metrics/timersFired.js +2 -0
- package/src/metrics/timersPending.js +2 -0
- package/src/metrics/toPrometheusMetricName.js +8 -0
- package/src/metrics/tokensCacheReadTotal.js +2 -0
- package/src/metrics/tokensCacheWriteTotal.js +2 -0
- package/src/metrics/tokensContextWindowBucketTotal.js +2 -0
- package/src/metrics/tokensContextWindowPerCall.js +5 -0
- package/src/metrics/tokensInputPerCall.js +3 -0
- package/src/metrics/tokensInputTotal.js +2 -0
- package/src/metrics/tokensOutputPerCall.js +3 -0
- package/src/metrics/tokensOutputTotal.js +2 -0
- package/src/metrics/tokensReasoningTotal.js +2 -0
- package/src/metrics/toolCallErrorsTotal.js +2 -0
- package/src/metrics/toolCallsTotal.js +2 -0
- package/src/metrics/toolDuration.js +3 -0
- package/src/metrics/toolOutputTruncatedTotal.js +2 -0
- package/src/metrics/trackEvent.js +608 -0
- package/src/metrics/updateAsyncExternalWaitPending.js +18 -0
- package/src/metrics/updateProcessMetrics.js +20 -0
- package/src/metrics/vcsDuration.js +3 -0
- package/src/normalizeStructuredEvent.js +15 -0
- package/src/prometheusContentType.js +1 -0
- package/src/renderPrometheusMetrics.js +212 -0
- package/src/resolveAgentTraceCapabilities.js +59 -0
- package/src/resolveSmithersObservabilityOptions.js +109 -0
- package/src/smithersMetrics.js +2 -0
- package/src/smithersSpanNames.js +6 -0
- package/src/unsupportedKindsForCapabilities.js +32 -0
- package/src/withSmithersSpan.js +17 -0
- package/tempo/tempo.yml +17 -0
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('./agentTrace.ts').AgentFamily} AgentFamily
|
|
3
|
+
* @typedef {import('./agentTrace.ts').CanonicalAgentTraceEventKind} CanonicalAgentTraceEventKind
|
|
4
|
+
*
|
|
5
|
+
* @typedef {"message" | "tool" | "pi" | "none"} PayloadKind
|
|
6
|
+
*
|
|
7
|
+
* @typedef {{
|
|
8
|
+
* kind: CanonicalAgentTraceEventKind;
|
|
9
|
+
* payloadKind: PayloadKind;
|
|
10
|
+
* expect?: CanonicalAgentTraceEventKind;
|
|
11
|
+
* }} MappedStructuredEvent
|
|
12
|
+
*
|
|
13
|
+
* @typedef {{
|
|
14
|
+
* kind: CanonicalAgentTraceEventKind;
|
|
15
|
+
* payload: Record<string, unknown> | null;
|
|
16
|
+
* raw: unknown;
|
|
17
|
+
* rawType?: string;
|
|
18
|
+
* observed?: boolean;
|
|
19
|
+
* }} NormalizedTraceEvent
|
|
20
|
+
*
|
|
21
|
+
* @typedef {{
|
|
22
|
+
* events: NormalizedTraceEvent[];
|
|
23
|
+
* expectedKinds?: CanonicalAgentTraceEventKind[];
|
|
24
|
+
* }} NormalizedTraceBatch
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {unknown} value
|
|
29
|
+
* @returns {string | undefined}
|
|
30
|
+
*/
|
|
31
|
+
function extractTextFromJsonValue(value) {
|
|
32
|
+
if (typeof value === "string") return value;
|
|
33
|
+
if (Array.isArray(value)) {
|
|
34
|
+
const text = value.map((item) => extractTextFromJsonValue(item) ?? "").join("");
|
|
35
|
+
return text || undefined;
|
|
36
|
+
}
|
|
37
|
+
if (!value || typeof value !== "object") return undefined;
|
|
38
|
+
const record = /** @type {Record<string, unknown>} */ (value);
|
|
39
|
+
if (typeof record.text === "string") return record.text;
|
|
40
|
+
if (typeof record.content === "string") return record.content;
|
|
41
|
+
if (typeof record.output_text === "string") return record.output_text;
|
|
42
|
+
if (Array.isArray(record.content)) {
|
|
43
|
+
const text = record.content.map((part) => extractTextFromJsonValue(part) ?? "").join("");
|
|
44
|
+
if (text) return text;
|
|
45
|
+
}
|
|
46
|
+
if (record.type === "text" && record.part) return extractTextFromJsonValue(record.part);
|
|
47
|
+
for (const field of ["response", "message", "result", "output", "data", "item"]) {
|
|
48
|
+
const text = extractTextFromJsonValue(record[field]);
|
|
49
|
+
if (text) return text;
|
|
50
|
+
}
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** @type {Record<string, ReadonlyArray<ReadonlyArray<string>>>} */
|
|
55
|
+
const _usageFieldAliases = {
|
|
56
|
+
inputTokens: [
|
|
57
|
+
["inputTokens"],
|
|
58
|
+
["promptTokens"],
|
|
59
|
+
["prompt_tokens"],
|
|
60
|
+
["input_tokens"],
|
|
61
|
+
["input"],
|
|
62
|
+
["models", "gemini", "tokens", "input"],
|
|
63
|
+
],
|
|
64
|
+
outputTokens: [
|
|
65
|
+
["outputTokens"],
|
|
66
|
+
["completionTokens"],
|
|
67
|
+
["completion_tokens"],
|
|
68
|
+
["output_tokens"],
|
|
69
|
+
["output"],
|
|
70
|
+
["models", "gemini", "tokens", "output"],
|
|
71
|
+
],
|
|
72
|
+
cacheReadTokens: [
|
|
73
|
+
["cacheReadTokens"],
|
|
74
|
+
["cache_read_input_tokens"],
|
|
75
|
+
["cached_input_tokens"],
|
|
76
|
+
["cache_read_tokens"],
|
|
77
|
+
["inputTokenDetails", "cacheReadTokens"],
|
|
78
|
+
],
|
|
79
|
+
cacheWriteTokens: [
|
|
80
|
+
["cacheWriteTokens"],
|
|
81
|
+
["cache_write_input_tokens"],
|
|
82
|
+
["cache_creation_input_tokens"],
|
|
83
|
+
["cache_write_tokens"],
|
|
84
|
+
["inputTokenDetails", "cacheWriteTokens"],
|
|
85
|
+
],
|
|
86
|
+
reasoningTokens: [["reasoningTokens"], ["reasoning_tokens"], ["outputTokenDetails", "reasoningTokens"]],
|
|
87
|
+
totalTokens: [["totalTokens"], ["total_tokens"]],
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @param {unknown} value
|
|
92
|
+
* @param {ReadonlyArray<string>} path
|
|
93
|
+
* @returns {unknown}
|
|
94
|
+
*/
|
|
95
|
+
function _readUsagePath(value, path) {
|
|
96
|
+
let current = value;
|
|
97
|
+
for (const segment of path) {
|
|
98
|
+
if (!current || typeof current !== "object") return undefined;
|
|
99
|
+
current = /** @type {Record<string, unknown>} */ (current)[segment];
|
|
100
|
+
}
|
|
101
|
+
return current;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @param {unknown} usage
|
|
106
|
+
* @returns {Record<string, number> | null}
|
|
107
|
+
*/
|
|
108
|
+
function normalizeTokenUsage(usage) {
|
|
109
|
+
if (!usage || typeof usage !== "object") return null;
|
|
110
|
+
/** @type {Record<string, number>} */
|
|
111
|
+
const normalized = {};
|
|
112
|
+
for (const [field, aliases] of Object.entries(_usageFieldAliases)) {
|
|
113
|
+
for (const path of aliases) {
|
|
114
|
+
const value = _readUsagePath(usage, path);
|
|
115
|
+
if (typeof value === "number") {
|
|
116
|
+
normalized[field] = value;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return Object.values(normalized).some((v) => Number.isFinite(v) && v > 0) ? normalized : null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** @type {Record<string, MappedStructuredEvent>} */
|
|
125
|
+
const piSimpleEventMap = {
|
|
126
|
+
session: { kind: "session.start", payloadKind: "pi" },
|
|
127
|
+
agent_start: { kind: "session.start", payloadKind: "pi" },
|
|
128
|
+
agent_end: { kind: "session.end", payloadKind: "pi" },
|
|
129
|
+
turn_start: { kind: "turn.start", payloadKind: "pi", expect: "turn.end" },
|
|
130
|
+
message_start: { kind: "message.start", payloadKind: "pi" },
|
|
131
|
+
tool_execution_start: { kind: "tool.execution.start", payloadKind: "tool", expect: "tool.execution.end" },
|
|
132
|
+
tool_execution_update: { kind: "tool.execution.update", payloadKind: "tool", expect: "tool.execution.end" },
|
|
133
|
+
tool_execution_end: { kind: "tool.execution.end", payloadKind: "tool" },
|
|
134
|
+
auto_compaction_start: { kind: "compaction.start", payloadKind: "pi" },
|
|
135
|
+
auto_compaction_end: { kind: "compaction.end", payloadKind: "pi" },
|
|
136
|
+
auto_retry_start: { kind: "retry.start", payloadKind: "pi" },
|
|
137
|
+
auto_retry_end: { kind: "retry.end", payloadKind: "pi" },
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/** @type {Record<string, MappedStructuredEvent>} */
|
|
141
|
+
const genericStructuredEventMap = {
|
|
142
|
+
message_start: { kind: "message.start", payloadKind: "message" },
|
|
143
|
+
assistant_message_start: { kind: "message.start", payloadKind: "message" },
|
|
144
|
+
"response.started": { kind: "message.start", payloadKind: "message" },
|
|
145
|
+
tool_call_start: { kind: "tool.execution.start", payloadKind: "tool", expect: "tool.execution.end" },
|
|
146
|
+
tool_execution_start: { kind: "tool.execution.start", payloadKind: "tool", expect: "tool.execution.end" },
|
|
147
|
+
"tool_call.started": { kind: "tool.execution.start", payloadKind: "tool", expect: "tool.execution.end" },
|
|
148
|
+
tool_call_delta: { kind: "tool.execution.update", payloadKind: "tool", expect: "tool.execution.end" },
|
|
149
|
+
tool_call_update: { kind: "tool.execution.update", payloadKind: "tool", expect: "tool.execution.end" },
|
|
150
|
+
tool_execution_update: { kind: "tool.execution.update", payloadKind: "tool", expect: "tool.execution.end" },
|
|
151
|
+
"tool_call.delta": { kind: "tool.execution.update", payloadKind: "tool", expect: "tool.execution.end" },
|
|
152
|
+
tool_call_end: { kind: "tool.execution.end", payloadKind: "tool" },
|
|
153
|
+
tool_execution_end: { kind: "tool.execution.end", payloadKind: "tool" },
|
|
154
|
+
"tool_call.completed": { kind: "tool.execution.end", payloadKind: "tool" },
|
|
155
|
+
tool_result: { kind: "tool.result", payloadKind: "tool" },
|
|
156
|
+
"tool_result.completed": { kind: "tool.result", payloadKind: "tool" },
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @param {any} parsed
|
|
161
|
+
* @returns {string | undefined}
|
|
162
|
+
*/
|
|
163
|
+
function extractGenericDeltaText(parsed) {
|
|
164
|
+
const candidates = [
|
|
165
|
+
parsed?.delta?.text,
|
|
166
|
+
parsed?.delta,
|
|
167
|
+
parsed?.text,
|
|
168
|
+
parsed?.content_block?.text,
|
|
169
|
+
parsed?.contentBlock?.text,
|
|
170
|
+
parsed?.message?.text,
|
|
171
|
+
parsed?.message?.content,
|
|
172
|
+
parsed?.output_text,
|
|
173
|
+
];
|
|
174
|
+
for (const candidate of candidates) {
|
|
175
|
+
if (typeof candidate === "string" && candidate) return candidate;
|
|
176
|
+
}
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* @param {any} parsed
|
|
182
|
+
* @returns {string | undefined}
|
|
183
|
+
*/
|
|
184
|
+
function extractGenericMessageText(parsed) {
|
|
185
|
+
return extractTextFromJsonValue(parsed?.message ?? parsed?.response ?? parsed?.item ?? parsed);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @param {any} parsed
|
|
190
|
+
* @returns {Record<string, unknown>}
|
|
191
|
+
*/
|
|
192
|
+
function extractGenericMessagePayload(parsed) {
|
|
193
|
+
/** @type {Record<string, unknown>} */
|
|
194
|
+
const payload = {};
|
|
195
|
+
const role = parsed?.message?.role ?? parsed?.role ?? parsed?.response?.role;
|
|
196
|
+
if (typeof role === "string") payload.role = role;
|
|
197
|
+
const text = extractGenericMessageText(parsed);
|
|
198
|
+
if (text) payload.text = text;
|
|
199
|
+
if (parsed?.id) payload.id = parsed.id;
|
|
200
|
+
return payload;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* @param {any} parsed
|
|
205
|
+
* @returns {Record<string, unknown>}
|
|
206
|
+
*/
|
|
207
|
+
function extractGenericToolPayload(parsed) {
|
|
208
|
+
const tool = parsed?.tool ?? parsed?.toolCall ?? parsed?.tool_call ?? parsed?.toolExecution ?? parsed;
|
|
209
|
+
return {
|
|
210
|
+
toolCallId: tool?.id ?? tool?.toolCallId ?? parsed?.id,
|
|
211
|
+
toolName: tool?.name ?? tool?.toolName ?? parsed?.toolName,
|
|
212
|
+
argsPreview: tool?.args ?? tool?.arguments ?? parsed?.args,
|
|
213
|
+
resultPreview: tool?.result ?? parsed?.result,
|
|
214
|
+
isError: Boolean(tool?.isError ?? parsed?.isError ?? parsed?.error),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* @param {any} parsed
|
|
220
|
+
* @returns {Record<string, unknown>}
|
|
221
|
+
*/
|
|
222
|
+
function extractPiPayload(parsed) {
|
|
223
|
+
/** @type {Record<string, unknown>} */
|
|
224
|
+
const payload = {};
|
|
225
|
+
if (parsed?.message?.role) payload.role = parsed.message.role;
|
|
226
|
+
const text = extractGenericMessageText(parsed?.message);
|
|
227
|
+
if (text) payload.text = text;
|
|
228
|
+
if (parsed?.id) payload.id = parsed.id;
|
|
229
|
+
return payload;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* @param {any} parsed
|
|
234
|
+
* @param {PayloadKind} payloadKind
|
|
235
|
+
* @returns {Record<string, unknown> | null}
|
|
236
|
+
*/
|
|
237
|
+
function extractMappedPayload(parsed, payloadKind) {
|
|
238
|
+
if (payloadKind === "message") return extractGenericMessagePayload(parsed);
|
|
239
|
+
if (payloadKind === "tool") return extractGenericToolPayload(parsed);
|
|
240
|
+
if (payloadKind === "pi") return extractPiPayload(parsed);
|
|
241
|
+
return {};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* @param {CanonicalAgentTraceEventKind} kind
|
|
246
|
+
* @param {Record<string, unknown> | null} payload
|
|
247
|
+
* @param {unknown} raw
|
|
248
|
+
* @param {string | undefined} rawType
|
|
249
|
+
* @param {boolean} [observed]
|
|
250
|
+
* @returns {NormalizedTraceEvent}
|
|
251
|
+
*/
|
|
252
|
+
function buildNormalizedEvent(kind, payload, raw, rawType, observed = false) {
|
|
253
|
+
return { kind, payload, raw, rawType, observed };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* @param {any} parsed
|
|
258
|
+
* @param {string} rawType
|
|
259
|
+
* @param {MappedStructuredEvent} mapped
|
|
260
|
+
* @returns {NormalizedTraceBatch}
|
|
261
|
+
*/
|
|
262
|
+
function normalizeMappedEvent(parsed, rawType, mapped) {
|
|
263
|
+
return {
|
|
264
|
+
events: [buildNormalizedEvent(mapped.kind, extractMappedPayload(parsed, mapped.payloadKind), parsed, rawType)],
|
|
265
|
+
expectedKinds: mapped.expect ? [mapped.expect] : undefined,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* @param {any} parsed
|
|
271
|
+
* @param {string} rawType
|
|
272
|
+
* @returns {NormalizedTraceBatch | null}
|
|
273
|
+
*/
|
|
274
|
+
function normalizeClaudeStructuredEvent(parsed, rawType) {
|
|
275
|
+
if (rawType === "assistant") {
|
|
276
|
+
const text = extractGenericMessageText(parsed?.message ?? parsed);
|
|
277
|
+
const events = text
|
|
278
|
+
? [
|
|
279
|
+
buildNormalizedEvent(
|
|
280
|
+
"message.update",
|
|
281
|
+
extractGenericMessagePayload(parsed?.message ?? parsed),
|
|
282
|
+
parsed,
|
|
283
|
+
rawType,
|
|
284
|
+
),
|
|
285
|
+
]
|
|
286
|
+
: [buildNormalizedEvent("stdout", { eventType: rawType }, parsed, rawType, true)];
|
|
287
|
+
const usage = normalizeTokenUsage(parsed?.message?.usage);
|
|
288
|
+
if (usage) events.push(buildNormalizedEvent("usage", usage, parsed, rawType));
|
|
289
|
+
return { events };
|
|
290
|
+
}
|
|
291
|
+
if (rawType === "result") {
|
|
292
|
+
/** @type {NormalizedTraceEvent[]} */
|
|
293
|
+
const events = [];
|
|
294
|
+
const usage = normalizeTokenUsage(parsed?.usage);
|
|
295
|
+
if (usage) events.push(buildNormalizedEvent("usage", usage, parsed, rawType));
|
|
296
|
+
const text = extractGenericMessageText(parsed);
|
|
297
|
+
if (text) {
|
|
298
|
+
events.push(buildNormalizedEvent("assistant.message.final", { text }, parsed, rawType));
|
|
299
|
+
}
|
|
300
|
+
return events.length > 0 ? { events } : null;
|
|
301
|
+
}
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* @param {any} parsed
|
|
307
|
+
* @param {string} rawType
|
|
308
|
+
* @returns {NormalizedTraceBatch | null}
|
|
309
|
+
*/
|
|
310
|
+
function normalizeGeminiStructuredEvent(parsed, rawType) {
|
|
311
|
+
if (rawType === "message") {
|
|
312
|
+
const text = extractGenericMessageText(parsed);
|
|
313
|
+
if (parsed?.role === "assistant" && typeof text === "string" && text) {
|
|
314
|
+
return {
|
|
315
|
+
events: [
|
|
316
|
+
buildNormalizedEvent(
|
|
317
|
+
parsed?.delta ? "assistant.text.delta" : "assistant.message.final",
|
|
318
|
+
{ text },
|
|
319
|
+
parsed,
|
|
320
|
+
rawType,
|
|
321
|
+
),
|
|
322
|
+
],
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
if (rawType === "result" && parsed?.stats) {
|
|
327
|
+
const usage = normalizeTokenUsage(parsed.stats);
|
|
328
|
+
return usage ? { events: [buildNormalizedEvent("usage", usage, parsed, rawType)] } : null;
|
|
329
|
+
}
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* @param {any} parsed
|
|
335
|
+
* @param {string} rawType
|
|
336
|
+
* @returns {NormalizedTraceBatch | null}
|
|
337
|
+
*/
|
|
338
|
+
function normalizeCodexStructuredEvent(parsed, rawType) {
|
|
339
|
+
if (rawType === "thread.started") {
|
|
340
|
+
return { events: [buildNormalizedEvent("stdout", { eventType: rawType }, parsed, rawType, true)] };
|
|
341
|
+
}
|
|
342
|
+
if (rawType === "turn.started") {
|
|
343
|
+
return { events: [buildNormalizedEvent("turn.start", {}, parsed, rawType)], expectedKinds: ["turn.end"] };
|
|
344
|
+
}
|
|
345
|
+
if (rawType === "item.completed" && parsed?.item?.type === "agent_message") {
|
|
346
|
+
const text = extractGenericMessageText(parsed.item);
|
|
347
|
+
if (typeof text === "string" && text) {
|
|
348
|
+
return { events: [buildNormalizedEvent("assistant.message.final", { text }, parsed, rawType)] };
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (rawType === "turn.completed") {
|
|
352
|
+
/** @type {NormalizedTraceEvent[]} */
|
|
353
|
+
const events = [];
|
|
354
|
+
const usage = normalizeTokenUsage(parsed?.usage);
|
|
355
|
+
if (usage) events.push(buildNormalizedEvent("usage", usage, parsed, rawType));
|
|
356
|
+
events.push(buildNormalizedEvent("turn.end", {}, parsed, rawType));
|
|
357
|
+
const text = extractGenericMessageText(parsed);
|
|
358
|
+
if (text) {
|
|
359
|
+
events.push(buildNormalizedEvent("assistant.message.final", { text }, parsed, rawType));
|
|
360
|
+
}
|
|
361
|
+
return { events };
|
|
362
|
+
}
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* @param {any} parsed
|
|
368
|
+
* @param {string} rawType
|
|
369
|
+
* @returns {NormalizedTraceBatch | null}
|
|
370
|
+
*/
|
|
371
|
+
function normalizePiStructuredEvent(parsed, rawType) {
|
|
372
|
+
const simple = piSimpleEventMap[rawType];
|
|
373
|
+
if (simple) return normalizeMappedEvent(parsed, rawType, simple);
|
|
374
|
+
if (rawType === "turn_end") {
|
|
375
|
+
/** @type {NormalizedTraceEvent[]} */
|
|
376
|
+
const events = [buildNormalizedEvent("turn.end", extractPiPayload(parsed), parsed, rawType)];
|
|
377
|
+
const text = extractGenericMessageText(parsed?.message);
|
|
378
|
+
if (text) {
|
|
379
|
+
events.push(buildNormalizedEvent("assistant.message.final", { text }, parsed?.message, rawType));
|
|
380
|
+
}
|
|
381
|
+
const usage = normalizeTokenUsage(parsed?.message?.usage);
|
|
382
|
+
if (usage) events.push(buildNormalizedEvent("usage", usage, parsed.message.usage, "usage"));
|
|
383
|
+
return { events };
|
|
384
|
+
}
|
|
385
|
+
if (rawType === "message_end") {
|
|
386
|
+
/** @type {NormalizedTraceEvent[]} */
|
|
387
|
+
const events = [buildNormalizedEvent("message.end", extractPiPayload(parsed), parsed, rawType)];
|
|
388
|
+
const text = extractGenericMessageText(parsed?.message);
|
|
389
|
+
if (parsed?.message?.role === "assistant" && text) {
|
|
390
|
+
events.push(buildNormalizedEvent("assistant.message.final", { text }, parsed?.message, rawType));
|
|
391
|
+
}
|
|
392
|
+
return { events };
|
|
393
|
+
}
|
|
394
|
+
if (rawType === "message_update") {
|
|
395
|
+
const evt = parsed?.assistantMessageEvent;
|
|
396
|
+
if (evt?.type === "text_delta" && typeof evt.delta === "string") {
|
|
397
|
+
return { events: [buildNormalizedEvent("assistant.text.delta", { text: evt.delta }, parsed, evt.type)] };
|
|
398
|
+
}
|
|
399
|
+
if ((evt?.type === "thinking_delta" || evt?.type === "reasoning_delta") && typeof evt.delta === "string") {
|
|
400
|
+
return { events: [buildNormalizedEvent("assistant.thinking.delta", { text: evt.delta }, parsed, evt.type)] };
|
|
401
|
+
}
|
|
402
|
+
return { events: [buildNormalizedEvent("message.update", extractPiPayload(parsed), parsed, rawType)] };
|
|
403
|
+
}
|
|
404
|
+
return { events: [buildNormalizedEvent("stdout", { eventType: rawType }, parsed, rawType, true)] };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* @param {any} parsed
|
|
409
|
+
* @param {string} rawType
|
|
410
|
+
* @returns {NormalizedTraceBatch | null}
|
|
411
|
+
*/
|
|
412
|
+
function normalizeSharedStructuredEvent(parsed, rawType) {
|
|
413
|
+
const mapped = genericStructuredEventMap[rawType];
|
|
414
|
+
if (mapped) return normalizeMappedEvent(parsed, rawType, mapped);
|
|
415
|
+
if (
|
|
416
|
+
[
|
|
417
|
+
"message_delta",
|
|
418
|
+
"assistant_message.delta",
|
|
419
|
+
"assistant_message_delta",
|
|
420
|
+
"response.output_text.delta",
|
|
421
|
+
"content_block_delta",
|
|
422
|
+
].includes(rawType)
|
|
423
|
+
) {
|
|
424
|
+
const text = extractGenericDeltaText(parsed);
|
|
425
|
+
if (typeof text === "string" && text) {
|
|
426
|
+
return { events: [buildNormalizedEvent("assistant.text.delta", { text }, parsed, rawType)] };
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
if (["thinking_delta", "reasoning_delta", "response.reasoning.delta"].includes(rawType)) {
|
|
430
|
+
const text = extractGenericDeltaText(parsed);
|
|
431
|
+
if (typeof text === "string" && text) {
|
|
432
|
+
return { events: [buildNormalizedEvent("assistant.thinking.delta", { text }, parsed, rawType)] };
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
if (["message_end", "assistant_message_end", "response.completed", "message_stop"].includes(rawType)) {
|
|
436
|
+
/** @type {NormalizedTraceEvent[]} */
|
|
437
|
+
const events = [buildNormalizedEvent("message.end", extractGenericMessagePayload(parsed), parsed, rawType)];
|
|
438
|
+
const text = extractGenericMessageText(parsed);
|
|
439
|
+
if (text) {
|
|
440
|
+
events.push(buildNormalizedEvent("assistant.message.final", { text }, parsed, rawType));
|
|
441
|
+
}
|
|
442
|
+
const usage = normalizeTokenUsage(parsed?.usage);
|
|
443
|
+
if (usage) events.push(buildNormalizedEvent("usage", usage, parsed, rawType));
|
|
444
|
+
return { events };
|
|
445
|
+
}
|
|
446
|
+
return null;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* @param {AgentFamily} agentFamily
|
|
451
|
+
* @param {any} parsed
|
|
452
|
+
* @param {string} rawType
|
|
453
|
+
* @returns {NormalizedTraceBatch}
|
|
454
|
+
*/
|
|
455
|
+
export function normalizeStructuredEventForFamily(agentFamily, parsed, rawType) {
|
|
456
|
+
if (agentFamily === "pi") {
|
|
457
|
+
return (
|
|
458
|
+
normalizePiStructuredEvent(parsed, rawType) ?? {
|
|
459
|
+
events: [buildNormalizedEvent("stdout", { eventType: rawType }, parsed, rawType, true)],
|
|
460
|
+
}
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
if (agentFamily === "claude-code") {
|
|
464
|
+
const normalized = normalizeClaudeStructuredEvent(parsed, rawType);
|
|
465
|
+
if (normalized) return normalized;
|
|
466
|
+
}
|
|
467
|
+
if (agentFamily === "gemini" || agentFamily === "antigravity") {
|
|
468
|
+
const normalized = normalizeGeminiStructuredEvent(parsed, rawType);
|
|
469
|
+
if (normalized) return normalized;
|
|
470
|
+
}
|
|
471
|
+
if (agentFamily === "codex") {
|
|
472
|
+
const normalized = normalizeCodexStructuredEvent(parsed, rawType);
|
|
473
|
+
if (normalized) return normalized;
|
|
474
|
+
}
|
|
475
|
+
const shared = normalizeSharedStructuredEvent(parsed, rawType);
|
|
476
|
+
if (shared) return shared;
|
|
477
|
+
return { events: [buildNormalizedEvent("stdout", { eventType: rawType }, parsed, rawType, true)] };
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* @param {AgentFamily} agentFamily
|
|
482
|
+
* @param {any} parsed
|
|
483
|
+
* @returns {{ sessionId?: string; threadId?: string }}
|
|
484
|
+
*/
|
|
485
|
+
export function extractProviderSessionCorrelation(agentFamily, parsed) {
|
|
486
|
+
if (agentFamily === "codex") {
|
|
487
|
+
const threadId = typeof parsed?.thread_id === "string" ? parsed.thread_id : undefined;
|
|
488
|
+
return { threadId };
|
|
489
|
+
}
|
|
490
|
+
const sessionId =
|
|
491
|
+
typeof parsed?.session_id === "string"
|
|
492
|
+
? parsed.session_id
|
|
493
|
+
: typeof parsed?.sessionId === "string"
|
|
494
|
+
? parsed.sessionId
|
|
495
|
+
: agentFamily === "pi" && typeof parsed?.id === "string"
|
|
496
|
+
? parsed.id
|
|
497
|
+
: undefined;
|
|
498
|
+
return { sessionId };
|
|
499
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {{ value: unknown; applied: boolean; ruleIds: string[] }} RedactionResult
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/** @type {Array<{ id: string; pattern: RegExp; replace?: string }>} */
|
|
6
|
+
const rules = [
|
|
7
|
+
{
|
|
8
|
+
id: "api-key",
|
|
9
|
+
// Covers Stripe-style `sk_`/`pk_` AND the hyphenated provider keys
|
|
10
|
+
// Smithers actually drives: OpenAI `sk-…`/`sk-proj-…` and Anthropic
|
|
11
|
+
// `sk-ant-api03-…`. The separator after sk/pk may be `-` or `_`, and the
|
|
12
|
+
// body may contain further `-`/`_` segments (namespaces like `proj-`,
|
|
13
|
+
// `ant-`, `api03-`).
|
|
14
|
+
pattern: /\b(?:sk|pk)[-_][A-Za-z0-9][A-Za-z0-9_-]{7,}\b/g,
|
|
15
|
+
replace: "[REDACTED_API_KEY]",
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
id: "bearer-token",
|
|
19
|
+
pattern: /Bearer\s+[A-Za-z0-9._-]{8,}/gi,
|
|
20
|
+
replace: "Bearer [REDACTED_TOKEN]",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: "auth-header",
|
|
24
|
+
pattern: /"authorization"\s*:\s*"[^"]+"/gi,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
id: "cookie-header",
|
|
28
|
+
pattern: /"cookie"\s*:\s*"[^"]+"/gi,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: "secret-ish",
|
|
32
|
+
// Negative lookbehind (not `\b`) so an underscore-joined prefix like
|
|
33
|
+
// `ANTHROPIC_API_KEY=` still matches: `_` is a word char, so `\bapi`
|
|
34
|
+
// never fired after it, leaking env-style key dumps.
|
|
35
|
+
pattern: /(?<![A-Za-z0-9])(?:api[_-]?key|token|secret|password)=([^\s"']+)/gi,
|
|
36
|
+
// No `replace` field: redactValue special-cases this rule by id and
|
|
37
|
+
// rewrites the captured value itself, so a top-level replace is never read.
|
|
38
|
+
},
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
const sensitiveKeySuffixes = ["authorization", "cookie", "apikey", "token", "password", "secret"];
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @param {string} key
|
|
45
|
+
* @returns {boolean}
|
|
46
|
+
*/
|
|
47
|
+
function isSensitiveKey(key) {
|
|
48
|
+
const canonicalKey = key.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
49
|
+
return sensitiveKeySuffixes.some((suffix) => canonicalKey.endsWith(suffix));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {unknown} value
|
|
54
|
+
* @param {Set<string>} applied
|
|
55
|
+
* @param {WeakSet<object>} ancestors
|
|
56
|
+
* @param {string} key
|
|
57
|
+
* @returns {unknown}
|
|
58
|
+
*/
|
|
59
|
+
function redactStructuredFields(value, applied, ancestors = new WeakSet(), key = "") {
|
|
60
|
+
if (value === null || typeof value !== "object") return value;
|
|
61
|
+
if (ancestors.has(value)) return "[Circular]";
|
|
62
|
+
|
|
63
|
+
ancestors.add(value);
|
|
64
|
+
try {
|
|
65
|
+
// JSON.stringify invokes toJSON before it visits object fields. Materialize
|
|
66
|
+
// that result here so sensitive fields introduced by a custom serializer are
|
|
67
|
+
// still seen by the structural redactor.
|
|
68
|
+
let skipOwnToJSON = false;
|
|
69
|
+
try {
|
|
70
|
+
const toJSON = /** @type {{ toJSON?: unknown }} */ (value).toJSON;
|
|
71
|
+
if (typeof toJSON === "function") {
|
|
72
|
+
let jsonValue;
|
|
73
|
+
try {
|
|
74
|
+
jsonValue = toJSON.call(value, key);
|
|
75
|
+
} catch {
|
|
76
|
+
// Fall back to enumerable fields, but remove the throwing
|
|
77
|
+
// serializer from the copied object so JSON.stringify cannot
|
|
78
|
+
// invoke it again later.
|
|
79
|
+
skipOwnToJSON = true;
|
|
80
|
+
}
|
|
81
|
+
if (!skipOwnToJSON) {
|
|
82
|
+
return redactStructuredFields(jsonValue, applied, ancestors, key);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
} catch {
|
|
86
|
+
// A throwing toJSON getter should be treated the same way as a
|
|
87
|
+
// throwing serializer: preserve the remaining enumerable fields.
|
|
88
|
+
skipOwnToJSON = true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (Array.isArray(value)) {
|
|
92
|
+
let next = value;
|
|
93
|
+
for (let index = 0; index < value.length; index++) {
|
|
94
|
+
const redacted = redactStructuredFields(value[index], applied, ancestors, String(index));
|
|
95
|
+
if (redacted === value[index]) continue;
|
|
96
|
+
if (next === value) next = [...value];
|
|
97
|
+
next[index] = redacted;
|
|
98
|
+
}
|
|
99
|
+
if (skipOwnToJSON && next === value) return [...value];
|
|
100
|
+
return next;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const record = /** @type {Record<string, unknown>} */ (value);
|
|
104
|
+
const entries = Object.keys(record)
|
|
105
|
+
.filter((fieldKey) => !(skipOwnToJSON && fieldKey === "toJSON"))
|
|
106
|
+
.map((fieldKey) => [fieldKey, record[fieldKey]]);
|
|
107
|
+
let next = record;
|
|
108
|
+
for (const [fieldKey, fieldValue] of entries) {
|
|
109
|
+
let redacted = fieldValue;
|
|
110
|
+
if (isSensitiveKey(fieldKey)) {
|
|
111
|
+
redacted = "[REDACTED]";
|
|
112
|
+
applied.add("sensitive-field");
|
|
113
|
+
} else {
|
|
114
|
+
redacted = redactStructuredFields(fieldValue, applied, ancestors, fieldKey);
|
|
115
|
+
}
|
|
116
|
+
if (redacted === fieldValue) continue;
|
|
117
|
+
if (next === record) next = Object.fromEntries(entries);
|
|
118
|
+
next[fieldKey] = redacted;
|
|
119
|
+
}
|
|
120
|
+
if (skipOwnToJSON && next === record) {
|
|
121
|
+
next = Object.fromEntries(entries);
|
|
122
|
+
}
|
|
123
|
+
return next;
|
|
124
|
+
} finally {
|
|
125
|
+
ancestors.delete(value);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {unknown} value
|
|
131
|
+
* @returns {RedactionResult}
|
|
132
|
+
*/
|
|
133
|
+
export function redactValue(value) {
|
|
134
|
+
/** @type {Set<string>} */
|
|
135
|
+
const applied = new Set();
|
|
136
|
+
const structured = redactStructuredFields(value, applied);
|
|
137
|
+
const input = typeof structured === "string" ? structured : (JSON.stringify(structured ?? null) ?? "null");
|
|
138
|
+
let next = input;
|
|
139
|
+
for (const rule of rules) {
|
|
140
|
+
next = next.replace(rule.pattern, (match) => {
|
|
141
|
+
applied.add(rule.id);
|
|
142
|
+
if (rule.id === "secret-ish") {
|
|
143
|
+
const idx = match.indexOf("=");
|
|
144
|
+
return `${match.slice(0, idx + 1)}[REDACTED_SECRET]`;
|
|
145
|
+
}
|
|
146
|
+
if (rule.id === "auth-header" || rule.id === "cookie-header") {
|
|
147
|
+
const idx = match.indexOf(":");
|
|
148
|
+
return `${match.slice(0, idx + 1)}"[REDACTED]"`;
|
|
149
|
+
}
|
|
150
|
+
return rule.replace ?? "[REDACTED]";
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
if (applied.size === 0) return { value: structured, applied: false, ruleIds: [] };
|
|
154
|
+
if (typeof structured === "string") {
|
|
155
|
+
return { value: next, applied: true, ruleIds: [...applied] };
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
return { value: JSON.parse(next), applied: true, ruleIds: [...applied] };
|
|
159
|
+
} catch {
|
|
160
|
+
return { value: next, applied: true, ruleIds: [...applied] };
|
|
161
|
+
}
|
|
162
|
+
}
|