ravensight-playtest 0.6.0 → 0.7.2

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/src/run/report.js CHANGED
@@ -57,6 +57,60 @@ export function lintMarkdownSections(markdown) {
57
57
  return REQUIRED_MARKDOWN_SECTIONS.filter(section => !text.includes(section));
58
58
  }
59
59
 
60
+ /** The tool-call tags a model has been seen to leak into a written file. */
61
+ const TOOL_TAG = '(?:parameter|invoke|content|function_calls|function_results|tool_use|tool_call|antml:[\\w-]+)';
62
+
63
+ /** A line made only of tags: closers of any name, or tool-call openers. */
64
+ const MARKUP_LINE = new RegExp(`^(?:\\s*(?:<\\/[A-Za-z][\\w:.-]*\\s*>|<${TOOL_TAG}\\b[^>]*>))+\\s*$`);
65
+
66
+ /** Tool-call closers glued to the end of the last line of real text. */
67
+ const MARKUP_SUFFIX = new RegExp(`(?:\\s*<\\/${TOOL_TAG}\\s*>)+\\s*$`);
68
+
69
+ const FENCE = /^\s*(?:```|~~~)/;
70
+
71
+ /**
72
+ * Takes tool-call markup off the end of a file the report step wrote. A real
73
+ * report.md ended "</content>\n</invoke>" because the model closed its tool
74
+ * call inside the file's content, and the runner appended its own sections
75
+ * after the stray tags.
76
+ *
77
+ * Only the end of the text is touched, and only outside a code fence: HTML a
78
+ * report quotes in backticks or in a fenced block is kept.
79
+ *
80
+ * @param {string} text
81
+ * @returns {string}
82
+ */
83
+ export function stripTrailingToolMarkup(text) {
84
+ if (typeof text !== 'string' || text === '') return text;
85
+ const lines = text.split('\n');
86
+ const insideFence = count => lines.slice(0, count).filter(line => FENCE.test(line)).length % 2 === 1;
87
+ let end = lines.length;
88
+ let changed = false;
89
+ while (end > 0) {
90
+ const line = lines[end - 1];
91
+ if (line.trim() === '') {
92
+ end -= 1;
93
+ } else if (MARKUP_LINE.test(line) && !insideFence(end - 1)) {
94
+ end -= 1;
95
+ changed = true;
96
+ } else {
97
+ break;
98
+ }
99
+ }
100
+ const kept = lines.slice(0, end);
101
+ if (kept.length > 0) {
102
+ const index = kept.length - 1;
103
+ const last = kept[index];
104
+ const backticks = (last.match(/`/g) || []).length;
105
+ if (MARKUP_SUFFIX.test(last) && backticks % 2 === 0 && !FENCE.test(last) && !insideFence(index)) {
106
+ kept[index] = last.replace(MARKUP_SUFFIX, '');
107
+ changed = true;
108
+ }
109
+ }
110
+ if (!changed) return text;
111
+ return `${kept.join('\n').replace(/\s+$/, '')}\n`;
112
+ }
113
+
60
114
  export class ReportInvalidError extends Error {
61
115
  /**
62
116
  * @param {Array<{instancePath: string, message: string}>} details
package/src/run/usage.js CHANGED
@@ -32,6 +32,14 @@ export function createUsageLedger() {
32
32
  let modelMs = 0;
33
33
  let serverTotalUsd = null;
34
34
  let serverCalls = null;
35
+ // The run's tokens in input-token units (see weightedUnits), and the last
36
+ // server spend that covered every call this ledger had recorded when it
37
+ // arrived. Together they say what one unit costs on this run, which is how
38
+ // the persona loop keeps enough budget back for the report without ever
39
+ // holding a price: ./budgetReserve.js.
40
+ let units = 0;
41
+ let lastCallUnits = 0;
42
+ let sample = null;
35
43
 
36
44
  return {
37
45
  /**
@@ -50,6 +58,8 @@ export function createUsageLedger() {
50
58
  entry.cache_read_tokens += Number(usage.cache_read_input_tokens || 0);
51
59
  entry.output_tokens += Number(usage.output_tokens || 0);
52
60
  byModel.set(key, entry);
61
+ lastCallUnits = weightedUnits(usage);
62
+ units += lastCallUnits;
53
63
  },
54
64
 
55
65
  /**
@@ -61,6 +71,31 @@ export function createUsageLedger() {
61
71
  if (!runUsage) return;
62
72
  if (Number.isFinite(Number(runUsage.total_usd))) serverTotalUsd = Number(runUsage.total_usd);
63
73
  if (Number.isFinite(Number(runUsage.calls))) serverCalls = Number(runUsage.calls);
74
+ // A figure that has not caught up with every call recorded here would
75
+ // make a unit look cheaper than it is, so only one that covers them all
76
+ // (the server may count more, which only makes a unit look dearer) is
77
+ // kept as the price sample.
78
+ const covers = !Number.isFinite(Number(runUsage.calls)) || Number(runUsage.calls) >= calls;
79
+ if (serverTotalUsd !== null && serverTotalUsd > 0 && units > 0 && covers) sample = { usd: serverTotalUsd, units };
80
+ },
81
+
82
+ /** The weighted units of the last call recorded, 0 before the first. */
83
+ get lastCallUnits() {
84
+ return lastCallUnits;
85
+ },
86
+
87
+ /**
88
+ * What the run has spent and what one weighted unit costs on it, or null
89
+ * before the server has priced a call this ledger recorded. The spend is
90
+ * the server's figure plus the calls made since, priced at the sample's
91
+ * rate, so it never reads lower than the server's own.
92
+ *
93
+ * @returns {{usd: number, usdPerUnit: number}|null}
94
+ */
95
+ spendEstimate() {
96
+ if (!sample) return null;
97
+ const usdPerUnit = sample.usd / sample.units;
98
+ return { usd: Math.max(serverTotalUsd || 0, sample.usd + usdPerUnit * Math.max(0, units - sample.units)), usdPerUnit };
64
99
  },
65
100
 
66
101
  get modelCalls() {
@@ -114,6 +149,25 @@ export function createUsageLedger() {
114
149
  };
115
150
  }
116
151
 
152
+ /** Input-token units per cache write, cache read and output token. */
153
+ export const CACHE_WRITE_WEIGHT = 1.25;
154
+ export const CACHE_READ_WEIGHT = 0.1;
155
+ export const OUTPUT_WEIGHT = 5;
156
+
157
+ /**
158
+ * One call's tokens in input-token units: what the server's price for the call
159
+ * is proportional to on every tier, since each tier prices a cache write at
160
+ * 1.25 input tokens, a cache read at 0.1 and an output token at 5. No price,
161
+ * only the shape of one.
162
+ *
163
+ * @param {Object} usage
164
+ * @returns {number}
165
+ */
166
+ export function weightedUnits(usage = {}) {
167
+ const n = key => Math.max(0, Number(usage && usage[key]) || 0);
168
+ return n('input_tokens') + n('cache_creation_input_tokens') * CACHE_WRITE_WEIGHT + n('cache_read_input_tokens') * CACHE_READ_WEIGHT + n('output_tokens') * OUTPUT_WEIGHT;
169
+ }
170
+
117
171
  /**
118
172
  * @param {string} runDirectory
119
173
  * @param {ReturnType<typeof createUsageLedger>} ledger