@agent-inspect/eval 6.31.38 → 6.31.40

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
@@ -1502,12 +1502,19 @@ function numericAttribute(node, keys) {
1502
1502
  }
1503
1503
  return void 0;
1504
1504
  }
1505
- function totalTokenCount(events) {
1506
- return events.reduce((total, event) => {
1507
- const usage = event.tokenUsage;
1508
- if (usage?.total !== void 0) return total + usage.total;
1509
- return total + (usage?.input ?? 0) + (usage?.output ?? 0);
1510
- }, 0);
1505
+ function measuredTotalTokens(event) {
1506
+ const usage = event.tokenUsage;
1507
+ if (!usage) return void 0;
1508
+ if (typeof usage.total === "number" && Number.isFinite(usage.total)) return usage.total;
1509
+ const input = usage.input;
1510
+ const output = usage.output;
1511
+ if (typeof input === "number" && Number.isFinite(input) && typeof output === "number" && Number.isFinite(output)) {
1512
+ return input + output;
1513
+ }
1514
+ return void 0;
1515
+ }
1516
+ function llmEventsForTokenBudget(events) {
1517
+ return events.filter((event) => event.kind === "LLM");
1511
1518
  }
1512
1519
  function createRule(id, category, evaluate) {
1513
1520
  return { id, category, severity: "error", evaluate };
@@ -1765,16 +1772,63 @@ var checks = {
1765
1772
  },
1766
1773
  maxTotalTokens(maxTotalTokens) {
1767
1774
  return createRule("eval.maxTotalTokens", "llm", (context) => {
1768
- const total = totalTokenCount(context.events);
1769
- return total > maxTotalTokens ? [
1770
- fail2(
1771
- "eval.maxTotalTokens",
1772
- `Total token usage exceeded ${maxTotalTokens}.`,
1773
- evidenceForRun(context.run, "tokenUsage.total"),
1774
- { maxTotalTokens },
1775
- total
1776
- )
1777
- ] : [];
1775
+ const llms = llmEventsForTokenBudget(context.events);
1776
+ const findings = [];
1777
+ const missing = llms.filter((event) => measuredTotalTokens(event) === void 0);
1778
+ if (missing.length > 0) {
1779
+ const errored = missing.filter((event) => event.status === "error");
1780
+ const other = missing.filter((event) => event.status !== "error");
1781
+ const evidenceForPersisted = (events) => events.map((event) => ({
1782
+ runId: event.runId,
1783
+ eventId: event.eventId,
1784
+ kind: event.kind,
1785
+ name: event.name,
1786
+ path: "tokenUsage.total"
1787
+ }));
1788
+ if (errored.length > 0) {
1789
+ findings.push(
1790
+ fail2(
1791
+ "eval.maxTotalTokens",
1792
+ `Unverifiable total token budget: ${errored.length} of ${llms.length} LLM call(s) ended in error without measurable total usage.`,
1793
+ evidenceForPersisted(errored),
1794
+ "measurable total token usage on every LLM call",
1795
+ {
1796
+ reason: "unverifiable",
1797
+ callsWithoutUsage: errored.length,
1798
+ calls: llms.length
1799
+ }
1800
+ )
1801
+ );
1802
+ }
1803
+ if (other.length > 0) {
1804
+ findings.push(
1805
+ fail2(
1806
+ "eval.maxTotalTokens",
1807
+ `No measurable total token usage was read for ${other.length} of ${llms.length} LLM call(s), so the total token limit cannot be verified.`,
1808
+ evidenceForPersisted(other),
1809
+ "measurable total token usage on every LLM call",
1810
+ {
1811
+ reason: "unverifiable",
1812
+ callsWithoutUsage: other.length,
1813
+ calls: llms.length
1814
+ }
1815
+ )
1816
+ );
1817
+ }
1818
+ }
1819
+ const total = llms.reduce((sum, event) => sum + (measuredTotalTokens(event) ?? 0), 0);
1820
+ if (total > maxTotalTokens) {
1821
+ findings.push(
1822
+ fail2(
1823
+ "eval.maxTotalTokens",
1824
+ `Total token usage exceeded ${maxTotalTokens}.`,
1825
+ evidenceForRun(context.run, "tokenUsage.total"),
1826
+ { maxTotalTokens },
1827
+ total
1828
+ )
1829
+ );
1830
+ }
1831
+ return findings;
1778
1832
  });
1779
1833
  },
1780
1834
  noFailedSteps() {