agent-inspect 1.0.2 → 1.1.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/CHANGELOG.md +57 -10
- package/README.md +28 -4
- package/SECURITY.md +5 -2
- package/docs/ADAPTERS.md +28 -1
- package/docs/API.md +16 -3
- package/docs/ARCHITECTURE.md +43 -8
- package/docs/CLI.md +1 -0
- package/docs/EXPORTS.md +1 -1
- package/docs/GETTING-STARTED.md +36 -10
- package/docs/LIMITATIONS.md +7 -1
- package/docs/LOG-TO-TREE-QUICKSTART.md +2 -0
- package/docs/LOGS.md +3 -2
- package/docs/SCHEMA.md +8 -2
- package/package.json +20 -18
- package/packages/cli/dist/index.cjs +310 -90
- package/packages/cli/dist/index.cjs.map +1 -1
- package/packages/cli/dist/index.mjs +310 -90
- package/packages/cli/dist/index.mjs.map +1 -1
- package/packages/core/dist/index.cjs +409 -141
- package/packages/core/dist/index.cjs.map +1 -1
- package/packages/core/dist/index.d.cts +130 -76
- package/packages/core/dist/index.d.ts +130 -76
- package/packages/core/dist/index.mjs +401 -142
- package/packages/core/dist/index.mjs.map +1 -1
|
@@ -1173,136 +1173,6 @@ function warn(message, error) {
|
|
|
1173
1173
|
}
|
|
1174
1174
|
console.warn(`${base}: ${formatError(error).message}`);
|
|
1175
1175
|
}
|
|
1176
|
-
var storage = new AsyncLocalStorage();
|
|
1177
|
-
function toPublicContext(ctx) {
|
|
1178
|
-
return {
|
|
1179
|
-
runId: ctx.runId,
|
|
1180
|
-
runName: ctx.runName,
|
|
1181
|
-
traceDir: ctx.traceDir,
|
|
1182
|
-
silent: ctx.silent,
|
|
1183
|
-
metadata: ctx.metadata
|
|
1184
|
-
};
|
|
1185
|
-
}
|
|
1186
|
-
function invoke(fn) {
|
|
1187
|
-
return new Promise((resolve, reject) => {
|
|
1188
|
-
try {
|
|
1189
|
-
Promise.resolve(fn()).then(resolve, reject);
|
|
1190
|
-
} catch (e) {
|
|
1191
|
-
reject(e);
|
|
1192
|
-
}
|
|
1193
|
-
});
|
|
1194
|
-
}
|
|
1195
|
-
function getCurrentContext() {
|
|
1196
|
-
try {
|
|
1197
|
-
const s = storage.getStore();
|
|
1198
|
-
if (!s) return void 0;
|
|
1199
|
-
return toPublicContext(s);
|
|
1200
|
-
} catch {
|
|
1201
|
-
return void 0;
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
function getCurrentRunId() {
|
|
1205
|
-
try {
|
|
1206
|
-
return storage.getStore()?.runId;
|
|
1207
|
-
} catch {
|
|
1208
|
-
return void 0;
|
|
1209
|
-
}
|
|
1210
|
-
}
|
|
1211
|
-
function getCurrentRunName() {
|
|
1212
|
-
try {
|
|
1213
|
-
return storage.getStore()?.runName;
|
|
1214
|
-
} catch {
|
|
1215
|
-
return void 0;
|
|
1216
|
-
}
|
|
1217
|
-
}
|
|
1218
|
-
function getCurrentStepId() {
|
|
1219
|
-
try {
|
|
1220
|
-
return storage.getStore()?.currentStepId;
|
|
1221
|
-
} catch {
|
|
1222
|
-
return void 0;
|
|
1223
|
-
}
|
|
1224
|
-
}
|
|
1225
|
-
function getParentStepId() {
|
|
1226
|
-
return getCurrentStepId();
|
|
1227
|
-
}
|
|
1228
|
-
function getCurrentDepth() {
|
|
1229
|
-
try {
|
|
1230
|
-
const d = storage.getStore()?.currentDepth;
|
|
1231
|
-
return typeof d === "number" && Number.isFinite(d) ? d : 0;
|
|
1232
|
-
} catch {
|
|
1233
|
-
return 0;
|
|
1234
|
-
}
|
|
1235
|
-
}
|
|
1236
|
-
function hasActiveContext() {
|
|
1237
|
-
try {
|
|
1238
|
-
return storage.getStore() !== void 0;
|
|
1239
|
-
} catch {
|
|
1240
|
-
return false;
|
|
1241
|
-
}
|
|
1242
|
-
}
|
|
1243
|
-
function getTraceDirFromContext() {
|
|
1244
|
-
try {
|
|
1245
|
-
return storage.getStore()?.traceDir;
|
|
1246
|
-
} catch {
|
|
1247
|
-
return void 0;
|
|
1248
|
-
}
|
|
1249
|
-
}
|
|
1250
|
-
function isSilentContext() {
|
|
1251
|
-
try {
|
|
1252
|
-
const s = storage.getStore();
|
|
1253
|
-
return s ? s.silent : false;
|
|
1254
|
-
} catch {
|
|
1255
|
-
return false;
|
|
1256
|
-
}
|
|
1257
|
-
}
|
|
1258
|
-
function runWithContext(context, fn) {
|
|
1259
|
-
const runtime = {
|
|
1260
|
-
runId: context.runId,
|
|
1261
|
-
runName: context.runName,
|
|
1262
|
-
traceDir: context.traceDir,
|
|
1263
|
-
silent: context.silent,
|
|
1264
|
-
metadata: context.metadata,
|
|
1265
|
-
currentDepth: 0
|
|
1266
|
-
};
|
|
1267
|
-
return new Promise((resolve, reject) => {
|
|
1268
|
-
storage.run(runtime, () => {
|
|
1269
|
-
try {
|
|
1270
|
-
Promise.resolve(fn()).then(resolve, reject);
|
|
1271
|
-
} catch (e) {
|
|
1272
|
-
reject(e);
|
|
1273
|
-
}
|
|
1274
|
-
});
|
|
1275
|
-
});
|
|
1276
|
-
}
|
|
1277
|
-
function runWithStepContext(stepId, fn) {
|
|
1278
|
-
let parent;
|
|
1279
|
-
try {
|
|
1280
|
-
parent = storage.getStore();
|
|
1281
|
-
} catch {
|
|
1282
|
-
parent = void 0;
|
|
1283
|
-
}
|
|
1284
|
-
if (!parent) {
|
|
1285
|
-
return invoke(fn);
|
|
1286
|
-
}
|
|
1287
|
-
const derived = {
|
|
1288
|
-
runId: parent.runId,
|
|
1289
|
-
runName: parent.runName,
|
|
1290
|
-
traceDir: parent.traceDir,
|
|
1291
|
-
silent: parent.silent,
|
|
1292
|
-
metadata: parent.metadata,
|
|
1293
|
-
currentStepId: stepId,
|
|
1294
|
-
currentDepth: parent.currentDepth + 1
|
|
1295
|
-
};
|
|
1296
|
-
return new Promise((resolve, reject) => {
|
|
1297
|
-
storage.run(derived, () => {
|
|
1298
|
-
try {
|
|
1299
|
-
Promise.resolve(fn()).then(resolve, reject);
|
|
1300
|
-
} catch (e) {
|
|
1301
|
-
reject(e);
|
|
1302
|
-
}
|
|
1303
|
-
});
|
|
1304
|
-
});
|
|
1305
|
-
}
|
|
1306
1176
|
function isRecord6(value) {
|
|
1307
1177
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1308
1178
|
}
|
|
@@ -1384,12 +1254,20 @@ async function initializeTraceFile(runId, traceDir) {
|
|
|
1384
1254
|
return void 0;
|
|
1385
1255
|
}
|
|
1386
1256
|
}
|
|
1257
|
+
function ensureEventWithinBounds(event) {
|
|
1258
|
+
const line = serializeEvent(event);
|
|
1259
|
+
if (line === "") return event;
|
|
1260
|
+
const bytes = Buffer.byteLength(line, "utf8");
|
|
1261
|
+
if (bytes <= DEFAULT_MAX_EVENT_BYTES) return event;
|
|
1262
|
+
return prepareTraceEventForDisk(event, resolveTraceSafetyOptions());
|
|
1263
|
+
}
|
|
1387
1264
|
async function writeTraceEvent(event, traceDir) {
|
|
1388
|
-
|
|
1265
|
+
const bounded = ensureEventWithinBounds(event);
|
|
1266
|
+
if (!validateEvent(bounded)) {
|
|
1389
1267
|
warn("Skipped invalid trace event (validation failed)");
|
|
1390
1268
|
return;
|
|
1391
1269
|
}
|
|
1392
|
-
const line = serializeEvent(
|
|
1270
|
+
const line = serializeEvent(bounded);
|
|
1393
1271
|
if (line === "") {
|
|
1394
1272
|
warn("Skipped trace event (serialization failed)");
|
|
1395
1273
|
return;
|
|
@@ -1490,6 +1368,351 @@ function getRunIdFromTraceFileName(fileName) {
|
|
|
1490
1368
|
return void 0;
|
|
1491
1369
|
}
|
|
1492
1370
|
}
|
|
1371
|
+
|
|
1372
|
+
// packages/core/src/trace-event-safety.ts
|
|
1373
|
+
var DEFAULT_MAX_METADATA_VALUE_LENGTH = 2e3;
|
|
1374
|
+
var DEFAULT_MAX_PREVIEW_LENGTH = 500;
|
|
1375
|
+
var DEFAULT_MAX_EVENT_BYTES = 65536;
|
|
1376
|
+
function isRecord7(value) {
|
|
1377
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1378
|
+
}
|
|
1379
|
+
function isPreviewKey(key) {
|
|
1380
|
+
return key.toLowerCase().includes("preview");
|
|
1381
|
+
}
|
|
1382
|
+
function truncateString(value, maxLen) {
|
|
1383
|
+
if (maxLen <= 0) return "\u2026";
|
|
1384
|
+
if (value.length <= maxLen) return value;
|
|
1385
|
+
return `${value.slice(0, maxLen)}\u2026`;
|
|
1386
|
+
}
|
|
1387
|
+
function byteLength(text) {
|
|
1388
|
+
return Buffer.byteLength(text, "utf8");
|
|
1389
|
+
}
|
|
1390
|
+
function resolveTraceSafetyOptions(options) {
|
|
1391
|
+
const redact = options?.redact;
|
|
1392
|
+
let redactEnabled = true;
|
|
1393
|
+
let redactionRules;
|
|
1394
|
+
if (redact === false) {
|
|
1395
|
+
redactEnabled = false;
|
|
1396
|
+
} else if (redact === true || redact === void 0) {
|
|
1397
|
+
redactEnabled = true;
|
|
1398
|
+
} else if (isRecord7(redact)) {
|
|
1399
|
+
redactEnabled = true;
|
|
1400
|
+
redactionRules = redact.rules;
|
|
1401
|
+
}
|
|
1402
|
+
return {
|
|
1403
|
+
redactEnabled,
|
|
1404
|
+
redactionRules,
|
|
1405
|
+
maxMetadataValueLength: typeof options?.maxMetadataValueLength === "number" && Number.isFinite(options.maxMetadataValueLength) && options.maxMetadataValueLength >= 0 ? Math.floor(options.maxMetadataValueLength) : DEFAULT_MAX_METADATA_VALUE_LENGTH,
|
|
1406
|
+
maxPreviewLength: typeof options?.maxPreviewLength === "number" && Number.isFinite(options.maxPreviewLength) && options.maxPreviewLength >= 0 ? Math.floor(options.maxPreviewLength) : DEFAULT_MAX_PREVIEW_LENGTH,
|
|
1407
|
+
maxEventBytes: typeof options?.maxEventBytes === "number" && Number.isFinite(options.maxEventBytes) && options.maxEventBytes > 0 ? Math.floor(options.maxEventBytes) : DEFAULT_MAX_EVENT_BYTES
|
|
1408
|
+
};
|
|
1409
|
+
}
|
|
1410
|
+
function boundMetadataValue(key, value, opts, seen, depth) {
|
|
1411
|
+
if (depth > 32) return "[MaxDepth]";
|
|
1412
|
+
if (value === null || typeof value !== "object") {
|
|
1413
|
+
if (typeof value === "string") {
|
|
1414
|
+
const max = isPreviewKey(key) ? opts.maxPreviewLength : opts.maxMetadataValueLength;
|
|
1415
|
+
return truncateString(value, max);
|
|
1416
|
+
}
|
|
1417
|
+
return value;
|
|
1418
|
+
}
|
|
1419
|
+
if (seen.has(value)) return "[Circular]";
|
|
1420
|
+
seen.add(value);
|
|
1421
|
+
if (Array.isArray(value)) {
|
|
1422
|
+
const maxItems = 50;
|
|
1423
|
+
const out2 = value.slice(0, maxItems).map(
|
|
1424
|
+
(item, index) => boundMetadataValue(String(index), item, opts, seen, depth + 1)
|
|
1425
|
+
);
|
|
1426
|
+
if (value.length > maxItems) {
|
|
1427
|
+
out2.push(`\u2026(+${value.length - maxItems} more)`);
|
|
1428
|
+
}
|
|
1429
|
+
return out2;
|
|
1430
|
+
}
|
|
1431
|
+
const record = value;
|
|
1432
|
+
const out = {};
|
|
1433
|
+
for (const [k, v] of Object.entries(record)) {
|
|
1434
|
+
out[k] = boundMetadataValue(k, v, opts, seen, depth + 1);
|
|
1435
|
+
}
|
|
1436
|
+
return out;
|
|
1437
|
+
}
|
|
1438
|
+
function redactMetadata(metadata, opts) {
|
|
1439
|
+
if (!opts.redactEnabled) return { ...metadata };
|
|
1440
|
+
const redactor = new Redactor({ rules: opts.redactionRules });
|
|
1441
|
+
return redactor.redactRecord(metadata);
|
|
1442
|
+
}
|
|
1443
|
+
function prepareMetadataForDisk(metadata, opts) {
|
|
1444
|
+
try {
|
|
1445
|
+
const redacted = redactMetadata(metadata, opts);
|
|
1446
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
1447
|
+
const bounded = boundMetadataValue(
|
|
1448
|
+
"metadata",
|
|
1449
|
+
redacted,
|
|
1450
|
+
opts,
|
|
1451
|
+
seen,
|
|
1452
|
+
0
|
|
1453
|
+
);
|
|
1454
|
+
return isRecord7(bounded) ? bounded : {};
|
|
1455
|
+
} catch {
|
|
1456
|
+
return { truncated: true, reason: "metadataPreparationFailed" };
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
function truncateErrorStack(event, maxLen) {
|
|
1460
|
+
if (event.event !== "run_completed" && event.event !== "step_completed") {
|
|
1461
|
+
return event;
|
|
1462
|
+
}
|
|
1463
|
+
if (!event.error?.stack || typeof event.error.stack !== "string") {
|
|
1464
|
+
return event;
|
|
1465
|
+
}
|
|
1466
|
+
return {
|
|
1467
|
+
...event,
|
|
1468
|
+
error: {
|
|
1469
|
+
...event.error,
|
|
1470
|
+
stack: truncateString(event.error.stack, maxLen)
|
|
1471
|
+
}
|
|
1472
|
+
};
|
|
1473
|
+
}
|
|
1474
|
+
function replaceMetadataWithTruncationMarker(event, originalApproxBytes) {
|
|
1475
|
+
const marker = {
|
|
1476
|
+
truncated: true,
|
|
1477
|
+
reason: "maxEventBytes",
|
|
1478
|
+
originalApproxBytes
|
|
1479
|
+
};
|
|
1480
|
+
if (event.event === "run_started") {
|
|
1481
|
+
return { ...event, metadata: marker };
|
|
1482
|
+
}
|
|
1483
|
+
if (event.event === "step_started") {
|
|
1484
|
+
return { ...event, metadata: marker };
|
|
1485
|
+
}
|
|
1486
|
+
return event;
|
|
1487
|
+
}
|
|
1488
|
+
function shrinkMetadataLimits(opts, factor) {
|
|
1489
|
+
return {
|
|
1490
|
+
...opts,
|
|
1491
|
+
maxMetadataValueLength: Math.max(32, Math.floor(opts.maxMetadataValueLength * factor)),
|
|
1492
|
+
maxPreviewLength: Math.max(16, Math.floor(opts.maxPreviewLength * factor))
|
|
1493
|
+
};
|
|
1494
|
+
}
|
|
1495
|
+
function applyMetadataToEvent(event, metadata) {
|
|
1496
|
+
if (event.event === "run_started") {
|
|
1497
|
+
return { ...event, metadata };
|
|
1498
|
+
}
|
|
1499
|
+
if (event.event === "step_started") {
|
|
1500
|
+
return { ...event, metadata };
|
|
1501
|
+
}
|
|
1502
|
+
return event;
|
|
1503
|
+
}
|
|
1504
|
+
function eventHasMetadata(event) {
|
|
1505
|
+
return (event.event === "run_started" || event.event === "step_started") && event.metadata !== void 0;
|
|
1506
|
+
}
|
|
1507
|
+
function getEventMetadata(event) {
|
|
1508
|
+
if (event.event === "run_started" || event.event === "step_started") {
|
|
1509
|
+
return event.metadata;
|
|
1510
|
+
}
|
|
1511
|
+
return void 0;
|
|
1512
|
+
}
|
|
1513
|
+
function prepareTraceEventForDisk(event, opts) {
|
|
1514
|
+
try {
|
|
1515
|
+
let working = { ...event };
|
|
1516
|
+
const rawMetadata = getEventMetadata(working);
|
|
1517
|
+
if (rawMetadata !== void 0) {
|
|
1518
|
+
const safe = prepareMetadataForDisk(rawMetadata, opts);
|
|
1519
|
+
working = applyMetadataToEvent(working, safe);
|
|
1520
|
+
}
|
|
1521
|
+
let serialized = serializeEvent(working);
|
|
1522
|
+
if (serialized === "") {
|
|
1523
|
+
return working;
|
|
1524
|
+
}
|
|
1525
|
+
let bytes = byteLength(serialized);
|
|
1526
|
+
if (bytes <= opts.maxEventBytes) {
|
|
1527
|
+
return working;
|
|
1528
|
+
}
|
|
1529
|
+
if (rawMetadata !== void 0) {
|
|
1530
|
+
for (const factor of [0.5, 0.25, 0.1]) {
|
|
1531
|
+
const tighter = shrinkMetadataLimits(opts, factor);
|
|
1532
|
+
const shrunk = prepareMetadataForDisk(rawMetadata, tighter);
|
|
1533
|
+
working = applyMetadataToEvent(working, shrunk);
|
|
1534
|
+
serialized = serializeEvent(working);
|
|
1535
|
+
if (serialized !== "" && byteLength(serialized) <= opts.maxEventBytes) {
|
|
1536
|
+
return working;
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
working = replaceMetadataWithTruncationMarker(working, bytes);
|
|
1540
|
+
serialized = serializeEvent(working);
|
|
1541
|
+
if (serialized !== "" && byteLength(serialized) <= opts.maxEventBytes) {
|
|
1542
|
+
return working;
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
working = truncateErrorStack(working, Math.min(opts.maxMetadataValueLength, 500));
|
|
1546
|
+
serialized = serializeEvent(working);
|
|
1547
|
+
if (serialized !== "" && byteLength(serialized) <= opts.maxEventBytes) {
|
|
1548
|
+
return working;
|
|
1549
|
+
}
|
|
1550
|
+
if (eventHasMetadata(working)) {
|
|
1551
|
+
working = replaceMetadataWithTruncationMarker(working, bytes);
|
|
1552
|
+
serialized = serializeEvent(working);
|
|
1553
|
+
if (serialized !== "" && byteLength(serialized) <= opts.maxEventBytes) {
|
|
1554
|
+
return working;
|
|
1555
|
+
}
|
|
1556
|
+
if (working.event === "run_started") {
|
|
1557
|
+
const { metadata: _meta, ...rest } = working;
|
|
1558
|
+
working = rest;
|
|
1559
|
+
} else if (working.event === "step_started") {
|
|
1560
|
+
const { metadata: _meta, ...rest } = working;
|
|
1561
|
+
working = rest;
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
return working;
|
|
1565
|
+
} catch {
|
|
1566
|
+
if (event.event === "run_started" || event.event === "step_started") {
|
|
1567
|
+
return applyMetadataToEvent(event, {
|
|
1568
|
+
truncated: true,
|
|
1569
|
+
reason: "prepareTraceEventFailed"
|
|
1570
|
+
});
|
|
1571
|
+
}
|
|
1572
|
+
return event;
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
// packages/core/src/context.ts
|
|
1577
|
+
var storage = new AsyncLocalStorage();
|
|
1578
|
+
function toPublicContext(ctx) {
|
|
1579
|
+
return {
|
|
1580
|
+
runId: ctx.runId,
|
|
1581
|
+
runName: ctx.runName,
|
|
1582
|
+
traceDir: ctx.traceDir,
|
|
1583
|
+
silent: ctx.silent,
|
|
1584
|
+
metadata: ctx.metadata
|
|
1585
|
+
};
|
|
1586
|
+
}
|
|
1587
|
+
function invoke(fn) {
|
|
1588
|
+
return new Promise((resolve, reject) => {
|
|
1589
|
+
try {
|
|
1590
|
+
Promise.resolve(fn()).then(resolve, reject);
|
|
1591
|
+
} catch (e) {
|
|
1592
|
+
reject(e);
|
|
1593
|
+
}
|
|
1594
|
+
});
|
|
1595
|
+
}
|
|
1596
|
+
function getCurrentContext() {
|
|
1597
|
+
try {
|
|
1598
|
+
const s = storage.getStore();
|
|
1599
|
+
if (!s) return void 0;
|
|
1600
|
+
return toPublicContext(s);
|
|
1601
|
+
} catch {
|
|
1602
|
+
return void 0;
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
function getCurrentRunId() {
|
|
1606
|
+
try {
|
|
1607
|
+
return storage.getStore()?.runId;
|
|
1608
|
+
} catch {
|
|
1609
|
+
return void 0;
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
function getCurrentRunName() {
|
|
1613
|
+
try {
|
|
1614
|
+
return storage.getStore()?.runName;
|
|
1615
|
+
} catch {
|
|
1616
|
+
return void 0;
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
function getCurrentStepId() {
|
|
1620
|
+
try {
|
|
1621
|
+
return storage.getStore()?.currentStepId;
|
|
1622
|
+
} catch {
|
|
1623
|
+
return void 0;
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
function getParentStepId() {
|
|
1627
|
+
return getCurrentStepId();
|
|
1628
|
+
}
|
|
1629
|
+
function getCurrentDepth() {
|
|
1630
|
+
try {
|
|
1631
|
+
const d = storage.getStore()?.currentDepth;
|
|
1632
|
+
return typeof d === "number" && Number.isFinite(d) ? d : 0;
|
|
1633
|
+
} catch {
|
|
1634
|
+
return 0;
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
function hasActiveContext() {
|
|
1638
|
+
try {
|
|
1639
|
+
return storage.getStore() !== void 0;
|
|
1640
|
+
} catch {
|
|
1641
|
+
return false;
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
function getTraceDirFromContext() {
|
|
1645
|
+
try {
|
|
1646
|
+
return storage.getStore()?.traceDir;
|
|
1647
|
+
} catch {
|
|
1648
|
+
return void 0;
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
function isSilentContext() {
|
|
1652
|
+
try {
|
|
1653
|
+
const s = storage.getStore();
|
|
1654
|
+
return s ? s.silent : false;
|
|
1655
|
+
} catch {
|
|
1656
|
+
return false;
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
function getTraceSafetyFromContext() {
|
|
1660
|
+
try {
|
|
1661
|
+
return storage.getStore()?.traceSafety;
|
|
1662
|
+
} catch {
|
|
1663
|
+
return void 0;
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
function runWithContext(context, fn, traceSafety = resolveTraceSafetyOptions()) {
|
|
1667
|
+
const runtime = {
|
|
1668
|
+
runId: context.runId,
|
|
1669
|
+
runName: context.runName,
|
|
1670
|
+
traceDir: context.traceDir,
|
|
1671
|
+
silent: context.silent,
|
|
1672
|
+
metadata: context.metadata,
|
|
1673
|
+
traceSafety,
|
|
1674
|
+
currentDepth: 0
|
|
1675
|
+
};
|
|
1676
|
+
return new Promise((resolve, reject) => {
|
|
1677
|
+
storage.run(runtime, () => {
|
|
1678
|
+
try {
|
|
1679
|
+
Promise.resolve(fn()).then(resolve, reject);
|
|
1680
|
+
} catch (e) {
|
|
1681
|
+
reject(e);
|
|
1682
|
+
}
|
|
1683
|
+
});
|
|
1684
|
+
});
|
|
1685
|
+
}
|
|
1686
|
+
function runWithStepContext(stepId, fn) {
|
|
1687
|
+
let parent;
|
|
1688
|
+
try {
|
|
1689
|
+
parent = storage.getStore();
|
|
1690
|
+
} catch {
|
|
1691
|
+
parent = void 0;
|
|
1692
|
+
}
|
|
1693
|
+
if (!parent) {
|
|
1694
|
+
return invoke(fn);
|
|
1695
|
+
}
|
|
1696
|
+
const derived = {
|
|
1697
|
+
runId: parent.runId,
|
|
1698
|
+
runName: parent.runName,
|
|
1699
|
+
traceDir: parent.traceDir,
|
|
1700
|
+
silent: parent.silent,
|
|
1701
|
+
metadata: parent.metadata,
|
|
1702
|
+
traceSafety: parent.traceSafety,
|
|
1703
|
+
currentStepId: stepId,
|
|
1704
|
+
currentDepth: parent.currentDepth + 1
|
|
1705
|
+
};
|
|
1706
|
+
return new Promise((resolve, reject) => {
|
|
1707
|
+
storage.run(derived, () => {
|
|
1708
|
+
try {
|
|
1709
|
+
Promise.resolve(fn()).then(resolve, reject);
|
|
1710
|
+
} catch (e) {
|
|
1711
|
+
reject(e);
|
|
1712
|
+
}
|
|
1713
|
+
});
|
|
1714
|
+
});
|
|
1715
|
+
}
|
|
1493
1716
|
function resolveTraceDir(options = {}) {
|
|
1494
1717
|
if (typeof options.dir === "string" && options.dir.trim() !== "") {
|
|
1495
1718
|
return options.dir.trim();
|
|
@@ -1754,7 +1977,7 @@ var KNOWN_EVENTS = /* @__PURE__ */ new Set([
|
|
|
1754
1977
|
"step_started",
|
|
1755
1978
|
"step_completed"
|
|
1756
1979
|
]);
|
|
1757
|
-
function
|
|
1980
|
+
function isRecord8(value) {
|
|
1758
1981
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1759
1982
|
}
|
|
1760
1983
|
function safeParse(line) {
|
|
@@ -1776,7 +1999,7 @@ async function isAgentInspectTrace(filePath) {
|
|
|
1776
1999
|
if (trimmed === "") continue;
|
|
1777
2000
|
const parsed = safeParse(trimmed);
|
|
1778
2001
|
if (!parsed) continue;
|
|
1779
|
-
if (!
|
|
2002
|
+
if (!isRecord8(parsed)) continue;
|
|
1780
2003
|
checked += 1;
|
|
1781
2004
|
if (isTraceEvent(parsed)) return true;
|
|
1782
2005
|
const ev = parsed.event;
|
|
@@ -2528,9 +2751,13 @@ async function inspectRun(name, fn, options) {
|
|
|
2528
2751
|
if (typeof fn !== "function") {
|
|
2529
2752
|
throw new TypeError("inspectRun requires `fn` to be a function");
|
|
2530
2753
|
}
|
|
2754
|
+
if (options?.enabled === false) {
|
|
2755
|
+
return Promise.resolve(fn());
|
|
2756
|
+
}
|
|
2531
2757
|
const runName = normalizeRunName(name);
|
|
2532
2758
|
const runId = createRunId();
|
|
2533
2759
|
const traceDir = resolveTraceDir({ dir: options?.traceDir });
|
|
2760
|
+
const traceSafety = resolveTraceSafetyOptions(options);
|
|
2534
2761
|
const context = {
|
|
2535
2762
|
runId,
|
|
2536
2763
|
runName,
|
|
@@ -2554,7 +2781,10 @@ async function inspectRun(name, fn, options) {
|
|
|
2554
2781
|
startTime,
|
|
2555
2782
|
...options?.metadata !== void 0 ? { metadata: options.metadata } : {}
|
|
2556
2783
|
};
|
|
2557
|
-
await writeTraceEvent(
|
|
2784
|
+
await writeTraceEvent(
|
|
2785
|
+
prepareTraceEventForDisk(started, traceSafety),
|
|
2786
|
+
traceDir
|
|
2787
|
+
);
|
|
2558
2788
|
});
|
|
2559
2789
|
await safeInstrumentation("printRunStart", () => {
|
|
2560
2790
|
printRunStart(runId, runName);
|
|
@@ -2578,7 +2808,10 @@ async function inspectRun(name, fn, options) {
|
|
|
2578
2808
|
durationMs: durationMs2,
|
|
2579
2809
|
error: formatted
|
|
2580
2810
|
};
|
|
2581
|
-
await writeTraceEvent(
|
|
2811
|
+
await writeTraceEvent(
|
|
2812
|
+
prepareTraceEventForDisk(completed, traceSafety),
|
|
2813
|
+
traceDir
|
|
2814
|
+
);
|
|
2582
2815
|
});
|
|
2583
2816
|
await safeInstrumentation("printRunComplete(error)", () => {
|
|
2584
2817
|
printRunComplete(runName, runId, durationMs2, "error", printPath2);
|
|
@@ -2598,13 +2831,35 @@ async function inspectRun(name, fn, options) {
|
|
|
2598
2831
|
endTime,
|
|
2599
2832
|
durationMs
|
|
2600
2833
|
};
|
|
2601
|
-
await writeTraceEvent(
|
|
2834
|
+
await writeTraceEvent(
|
|
2835
|
+
prepareTraceEventForDisk(completed, traceSafety),
|
|
2836
|
+
traceDir
|
|
2837
|
+
);
|
|
2602
2838
|
});
|
|
2603
2839
|
await safeInstrumentation("printRunComplete(success)", () => {
|
|
2604
2840
|
printRunComplete(runName, runId, durationMs, "success", printPath);
|
|
2605
2841
|
});
|
|
2606
2842
|
return result;
|
|
2607
|
-
});
|
|
2843
|
+
}, traceSafety);
|
|
2844
|
+
}
|
|
2845
|
+
|
|
2846
|
+
// packages/core/src/maybe-inspect-run.ts
|
|
2847
|
+
var ENABLED_ENV_VALUES = /* @__PURE__ */ new Set(["1", "true", "yes", "on", "enabled"]);
|
|
2848
|
+
function isAgentInspectEnabled(value) {
|
|
2849
|
+
if (value === void 0) return false;
|
|
2850
|
+
const normalized = value.trim().toLowerCase();
|
|
2851
|
+
if (normalized === "") return false;
|
|
2852
|
+
return ENABLED_ENV_VALUES.has(normalized);
|
|
2853
|
+
}
|
|
2854
|
+
async function maybeInspectRun(name, fn, options) {
|
|
2855
|
+
if (typeof fn !== "function") {
|
|
2856
|
+
throw new TypeError("maybeInspectRun requires `fn` to be a function");
|
|
2857
|
+
}
|
|
2858
|
+
const enabled = options?.enabled !== void 0 ? options.enabled : isAgentInspectEnabled(process.env.AGENT_INSPECT);
|
|
2859
|
+
if (!enabled) {
|
|
2860
|
+
return Promise.resolve(fn());
|
|
2861
|
+
}
|
|
2862
|
+
return inspectRun(name, fn, options);
|
|
2608
2863
|
}
|
|
2609
2864
|
|
|
2610
2865
|
// packages/core/src/step.ts
|
|
@@ -2636,6 +2891,7 @@ async function stepImpl(name, fn, options) {
|
|
|
2636
2891
|
const parentId = getParentStepId();
|
|
2637
2892
|
const stepType = options?.type ?? "logic";
|
|
2638
2893
|
const metadata = options?.metadata;
|
|
2894
|
+
const traceSafety = getTraceSafetyFromContext();
|
|
2639
2895
|
const startTime = Date.now();
|
|
2640
2896
|
await safeInstrumentation2("writeTraceEvent(step_started)", async () => {
|
|
2641
2897
|
const started = {
|
|
@@ -2650,7 +2906,8 @@ async function stepImpl(name, fn, options) {
|
|
|
2650
2906
|
startTime,
|
|
2651
2907
|
...metadata !== void 0 ? { metadata } : {}
|
|
2652
2908
|
};
|
|
2653
|
-
|
|
2909
|
+
const safe = traceSafety !== void 0 ? prepareTraceEventForDisk(started, traceSafety) : started;
|
|
2910
|
+
await writeTraceEvent(safe, context.traceDir);
|
|
2654
2911
|
});
|
|
2655
2912
|
await safeInstrumentation2("printStepStart", () => {
|
|
2656
2913
|
printStepStart(stepName, renderDepth);
|
|
@@ -2676,7 +2933,8 @@ async function stepImpl(name, fn, options) {
|
|
|
2676
2933
|
durationMs: durationMs2,
|
|
2677
2934
|
error: formatted
|
|
2678
2935
|
};
|
|
2679
|
-
|
|
2936
|
+
const safe = traceSafety !== void 0 ? prepareTraceEventForDisk(completed, traceSafety) : completed;
|
|
2937
|
+
await writeTraceEvent(safe, context.traceDir);
|
|
2680
2938
|
});
|
|
2681
2939
|
await safeInstrumentation2("printStepComplete(error)", () => {
|
|
2682
2940
|
printStepComplete(stepName, durationMs2, "error", renderDepth);
|
|
@@ -2702,7 +2960,8 @@ async function stepImpl(name, fn, options) {
|
|
|
2702
2960
|
endTime,
|
|
2703
2961
|
durationMs
|
|
2704
2962
|
};
|
|
2705
|
-
|
|
2963
|
+
const safe = traceSafety !== void 0 ? prepareTraceEventForDisk(completed, traceSafety) : completed;
|
|
2964
|
+
await writeTraceEvent(safe, context.traceDir);
|
|
2706
2965
|
});
|
|
2707
2966
|
await safeInstrumentation2("printStepComplete(success)", () => {
|
|
2708
2967
|
printStepComplete(stepName, durationMs, "success", renderDepth);
|
|
@@ -3571,6 +3830,6 @@ function validateExport(result) {
|
|
|
3571
3830
|
};
|
|
3572
3831
|
}
|
|
3573
3832
|
|
|
3574
|
-
export { DEFAULT_LOG_INGEST_CONFIG, DEFAULT_REDACT_KEYS, DEFAULT_TRACE_DIR_NAME, EXPORT_PAYLOAD_VERSION, EventNormalizer, FALLBACK_TRACE_DIR, JsonLogParser, LiveLogAccumulator, Log4jsParser, MAX_NAME_LENGTH, MAX_TERMINAL_DEPTH, MAX_TERMINAL_NAME_LENGTH, RUNS_DIR_NAME, Redactor, TERMINAL_INDENT, TraceDirectory, TreeBuilder, buildRunSummary, compactAttributes, createRunId, createStepId, diffRuns, diffTraceEvents, ensureTraceDir, escapeHtml, escapeMarkdown, exportHtml, exportMarkdown, exportOpenInference, exportOtlpJson, exportRunTree, extractMetadata, filterTraces, flattenTree, formatDuration2 as formatDuration, formatError, formatTerminalName, formatTimestamp, getCurrentContext, getCurrentDepth, getCurrentRunId, getCurrentRunName, getCurrentStepId, getDefaultTraceDir, getIndent, getParentStepId, getRunIdFromTraceFileName, getTraceDirFromContext, getTraceFilePath, hasActiveContext, initializeTraceFile, inspectRun, isAgentInspectTrace, isSilentContext, isStepStatus, isStepType, isTraceEvent, listTraceFiles, loadLogIngestConfig, manualTraceEventsToComparableRun, manualTraceEventsToRunTree, matchMapping, mergeExportDefaults, mergeLogIngestConfig, observe, parseDuration, parseLogLine, parseLogsToTrees, printError, printFailedAt, printRunComplete, printRunStart, printStepComplete, printStepStart, readTraceEvents, readTraceFile, renderErrorLine, renderRunDiff, renderRunSummary, renderRunTree, renderRunTrees, renderStepLine, resolveTraceDir, runWithContext, runWithStepContext, safeString2 as safeString, serializeEvent, stableJson, step, summarizeTree, truncateName, validateEvent, validateExport, validateExportContent, warn, wildcardMatch, writeTraceEvent };
|
|
3833
|
+
export { DEFAULT_LOG_INGEST_CONFIG, DEFAULT_MAX_EVENT_BYTES, DEFAULT_MAX_METADATA_VALUE_LENGTH, DEFAULT_MAX_PREVIEW_LENGTH, DEFAULT_REDACT_KEYS, DEFAULT_TRACE_DIR_NAME, EXPORT_PAYLOAD_VERSION, EventNormalizer, FALLBACK_TRACE_DIR, JsonLogParser, LiveLogAccumulator, Log4jsParser, MAX_NAME_LENGTH, MAX_TERMINAL_DEPTH, MAX_TERMINAL_NAME_LENGTH, RUNS_DIR_NAME, Redactor, TERMINAL_INDENT, TraceDirectory, TreeBuilder, buildRunSummary, compactAttributes, createRunId, createStepId, diffRuns, diffTraceEvents, ensureTraceDir, escapeHtml, escapeMarkdown, exportHtml, exportMarkdown, exportOpenInference, exportOtlpJson, exportRunTree, extractMetadata, filterTraces, flattenTree, formatDuration2 as formatDuration, formatError, formatTerminalName, formatTimestamp, getCurrentContext, getCurrentDepth, getCurrentRunId, getCurrentRunName, getCurrentStepId, getDefaultTraceDir, getIndent, getParentStepId, getRunIdFromTraceFileName, getTraceDirFromContext, getTraceFilePath, getTraceSafetyFromContext, hasActiveContext, initializeTraceFile, inspectRun, isAgentInspectEnabled, isAgentInspectTrace, isSilentContext, isStepStatus, isStepType, isTraceEvent, listTraceFiles, loadLogIngestConfig, manualTraceEventsToComparableRun, manualTraceEventsToRunTree, matchMapping, maybeInspectRun, mergeExportDefaults, mergeLogIngestConfig, observe, parseDuration, parseLogLine, parseLogsToTrees, prepareMetadataForDisk, prepareTraceEventForDisk, printError, printFailedAt, printRunComplete, printRunStart, printStepComplete, printStepStart, readTraceEvents, readTraceFile, renderErrorLine, renderRunDiff, renderRunSummary, renderRunTree, renderRunTrees, renderStepLine, resolveTraceDir, resolveTraceSafetyOptions, runWithContext, runWithStepContext, safeString2 as safeString, serializeEvent, stableJson, step, summarizeTree, truncateName, validateEvent, validateExport, validateExportContent, warn, wildcardMatch, writeTraceEvent };
|
|
3575
3834
|
//# sourceMappingURL=index.mjs.map
|
|
3576
3835
|
//# sourceMappingURL=index.mjs.map
|