@agent-inspect/viewer 4.0.0 → 4.2.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/dist/index.cjs CHANGED
@@ -1031,6 +1031,163 @@ function sessionKeyForRun(meta, options) {
1031
1031
  return void 0;
1032
1032
  }
1033
1033
 
1034
+ // packages/core/src/sessions/status.ts
1035
+ var DEFAULT_STALE_THRESHOLD_MS = 864e5;
1036
+ var EXPLICIT_STATUS_PRIORITY = {
1037
+ error: 5,
1038
+ waiting_input: 4,
1039
+ idle: 3,
1040
+ stale: 2,
1041
+ completed: 1
1042
+ };
1043
+ var EXPLICIT_SESSION_STATUSES = /* @__PURE__ */ new Set([
1044
+ "running",
1045
+ "waiting_input",
1046
+ "idle",
1047
+ "completed",
1048
+ "error",
1049
+ "stale",
1050
+ "unknown"
1051
+ ]);
1052
+ function isExplicitSessionStatus(value) {
1053
+ return typeof value === "string" && EXPLICIT_SESSION_STATUSES.has(value);
1054
+ }
1055
+ function activityMs(run) {
1056
+ return run.endedAt ?? run.startedAt ?? 0;
1057
+ }
1058
+ function latestActivityMs(runs) {
1059
+ let latest = 0;
1060
+ for (const run of runs) {
1061
+ const ms = activityMs(run);
1062
+ if (ms > latest) latest = ms;
1063
+ }
1064
+ return latest;
1065
+ }
1066
+ function earliestStart(runs) {
1067
+ let earliest;
1068
+ for (const run of runs) {
1069
+ if (run.startedAt === void 0) continue;
1070
+ if (earliest === void 0 || run.startedAt < earliest) {
1071
+ earliest = run.startedAt;
1072
+ }
1073
+ }
1074
+ return earliest;
1075
+ }
1076
+ function latestEndWhenAllEnded(runs) {
1077
+ if (runs.length === 0) return void 0;
1078
+ let latest;
1079
+ for (const run of runs) {
1080
+ if (run.endedAt === void 0) return void 0;
1081
+ if (latest === void 0 || run.endedAt > latest) latest = run.endedAt;
1082
+ }
1083
+ return latest;
1084
+ }
1085
+ function pickExplicitStatus(runs) {
1086
+ let best;
1087
+ let bestPriority = 0;
1088
+ for (const run of runs) {
1089
+ const raw = run.metadata?.sessionStatus;
1090
+ if (!isExplicitSessionStatus(raw)) continue;
1091
+ const priority = EXPLICIT_STATUS_PRIORITY[raw] ?? 0;
1092
+ if (priority > bestPriority) {
1093
+ bestPriority = priority;
1094
+ best = raw;
1095
+ }
1096
+ }
1097
+ return best;
1098
+ }
1099
+ function deriveLastError(runs) {
1100
+ const errorRuns = runs.filter((run) => run.status === "error").sort((a, b) => activityMs(b) - activityMs(a));
1101
+ const latest = errorRuns[0];
1102
+ if (!latest) return void 0;
1103
+ const meta = latest.metadata ?? {};
1104
+ const message = typeof meta.errorMessage === "string" && meta.errorMessage.trim() !== "" ? meta.errorMessage.trim() : latest.name ?? latest.runId;
1105
+ const code = typeof meta.errorCode === "string" && meta.errorCode.trim() !== "" ? meta.errorCode.trim() : void 0;
1106
+ return { runId: latest.runId, message, code };
1107
+ }
1108
+ function deriveCheckSummary(runs) {
1109
+ let pass = 0;
1110
+ let fail = 0;
1111
+ let warn2 = 0;
1112
+ let found = false;
1113
+ for (const run of runs) {
1114
+ const summary = run.metadata?.checkSummary;
1115
+ if (!summary || typeof summary !== "object") continue;
1116
+ const record = summary;
1117
+ if (typeof record.pass === "number") {
1118
+ pass += record.pass;
1119
+ found = true;
1120
+ }
1121
+ if (typeof record.fail === "number") {
1122
+ fail += record.fail;
1123
+ found = true;
1124
+ }
1125
+ if (typeof record.warn === "number") {
1126
+ warn2 += record.warn;
1127
+ found = true;
1128
+ }
1129
+ }
1130
+ return found ? { pass, fail, warn: warn2 } : void 0;
1131
+ }
1132
+ function deriveObservationSummary(runs) {
1133
+ for (const run of [...runs].sort((a, b) => activityMs(b) - activityMs(a))) {
1134
+ const value = run.metadata?.observationSummary;
1135
+ if (typeof value === "string" && value.trim() !== "") {
1136
+ return value.trim();
1137
+ }
1138
+ }
1139
+ return void 0;
1140
+ }
1141
+ function deriveSessionStatus(runs, options = {}) {
1142
+ if (runs.length === 0) return "unknown";
1143
+ if (runs.some((run) => run.status === "running")) return "running";
1144
+ const explicit = pickExplicitStatus(runs);
1145
+ if (explicit && explicit !== "running") return explicit;
1146
+ if (runs.some((run) => run.status === "error")) return "error";
1147
+ if (runs.every((run) => run.status === "success")) return "completed";
1148
+ const nowMs = options.nowMs ?? Date.now();
1149
+ const staleThresholdMs = options.staleThresholdMs ?? DEFAULT_STALE_THRESHOLD_MS;
1150
+ const lastMs = latestActivityMs(runs);
1151
+ if (lastMs > 0 && nowMs - lastMs > staleThresholdMs) return "stale";
1152
+ return "unknown";
1153
+ }
1154
+ function enrichSessionSummary(summary, runs, options = {}) {
1155
+ const sessionRuns = runs.filter((run) => summary.runIds.includes(run.runId)).sort((a, b) => a.runId.localeCompare(b.runId));
1156
+ const startedAt = earliestStart(sessionRuns);
1157
+ const endedAt = latestEndWhenAllEnded(sessionRuns);
1158
+ const durationMs = startedAt !== void 0 && endedAt !== void 0 ? endedAt - startedAt : void 0;
1159
+ let correlationId;
1160
+ let jobId;
1161
+ let workflowId;
1162
+ for (const run of sessionRuns) {
1163
+ const meta = extractSessionWorkflowMetadata(run.metadata);
1164
+ if (!correlationId && meta?.correlationId) correlationId = meta.correlationId;
1165
+ if (!jobId && meta?.jobId) jobId = meta.jobId;
1166
+ if (!workflowId && meta?.workflowName) workflowId = meta.workflowName;
1167
+ else if (!workflowId && meta?.workflowStep) workflowId = meta.workflowStep;
1168
+ }
1169
+ const lastMs = latestActivityMs(sessionRuns);
1170
+ const lastActivity = lastMs > 0 ? new Date(lastMs).toISOString() : (/* @__PURE__ */ new Date(0)).toISOString();
1171
+ const retryCount = summary.retries.filter(
1172
+ (retry) => retry.retryOf !== void 0 || (retry.attempt ?? 0) > 1
1173
+ ).length;
1174
+ return {
1175
+ ...summary,
1176
+ status: deriveSessionStatus(sessionRuns, options),
1177
+ startedAt,
1178
+ endedAt,
1179
+ durationMs,
1180
+ correlationId,
1181
+ jobId,
1182
+ workflowId,
1183
+ lastError: deriveLastError(sessionRuns),
1184
+ lastActivity,
1185
+ retryCount,
1186
+ observationSummary: deriveObservationSummary(sessionRuns),
1187
+ checkSummary: deriveCheckSummary(sessionRuns)
1188
+ };
1189
+ }
1190
+
1034
1191
  // packages/core/src/sessions/load.ts
1035
1192
  async function enrichSessionRunRecord(meta) {
1036
1193
  let metadata;
@@ -1263,14 +1420,21 @@ function buildSessionIndex(inputRuns, options = {}) {
1263
1420
  sessionId
1264
1421
  });
1265
1422
  }
1266
- return {
1267
- sessionId,
1268
- runIds,
1269
- groups,
1270
- handoffs,
1271
- retries,
1272
- criticalPath
1273
- };
1423
+ return enrichSessionSummary(
1424
+ {
1425
+ sessionId,
1426
+ runIds,
1427
+ groups,
1428
+ handoffs,
1429
+ retries,
1430
+ criticalPath
1431
+ },
1432
+ runs,
1433
+ {
1434
+ nowMs: options.nowMs,
1435
+ staleThresholdMs: options.staleThresholdMs
1436
+ }
1437
+ );
1274
1438
  });
1275
1439
  if (sessions.length === 0 && runs.length > 0) {
1276
1440
  warnings.push({