@buoy-gg/agent-core 7.0.36 → 7.0.39
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/README.md +13 -9
- package/lib/commonjs/catalog/catalog.g.js +93 -6
- package/lib/commonjs/catalog/catalog.g.js.map +1 -1
- package/lib/commonjs/catalog/catalog.source.json +106 -4
- package/lib/commonjs/catalog/catalog.types.g.js +3 -3
- package/lib/commonjs/catalog/catalog.types.g.js.map +1 -1
- package/lib/commonjs/catalog/toProviderTools.js +3 -1
- package/lib/commonjs/catalog/toProviderTools.js.map +1 -1
- package/lib/commonjs/effects/ledger.js +13 -1
- package/lib/commonjs/effects/ledger.js.map +1 -1
- package/lib/commonjs/engine/evidence.js +113 -0
- package/lib/commonjs/engine/evidence.js.map +1 -0
- package/lib/commonjs/engine/historyBudget.js +158 -55
- package/lib/commonjs/engine/historyBudget.js.map +1 -1
- package/lib/commonjs/engine/retrieve.js +214 -0
- package/lib/commonjs/engine/retrieve.js.map +1 -0
- package/lib/commonjs/engine/runAgentTurn.js +364 -94
- package/lib/commonjs/engine/runAgentTurn.js.map +1 -1
- package/lib/commonjs/engine/systemPrompt.js +31 -1
- package/lib/commonjs/engine/systemPrompt.js.map +1 -1
- package/lib/commonjs/engine/tokenCalibration.js +81 -0
- package/lib/commonjs/engine/tokenCalibration.js.map +1 -0
- package/lib/commonjs/engine/verify.js +300 -0
- package/lib/commonjs/engine/verify.js.map +1 -0
- package/lib/commonjs/index.js +16 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/providers/anthropic.js +12 -3
- package/lib/commonjs/providers/anthropic.js.map +1 -1
- package/lib/commonjs/providers/openai.js +11 -3
- package/lib/commonjs/providers/openai.js.map +1 -1
- package/lib/commonjs/providers/problem.js +98 -0
- package/lib/commonjs/providers/problem.js.map +1 -0
- package/lib/commonjs/providers/sse.js +6 -5
- package/lib/commonjs/providers/sse.js.map +1 -1
- package/lib/commonjs/providers/transport.js +20 -4
- package/lib/commonjs/providers/transport.js.map +1 -1
- package/lib/commonjs/providers/xhrStream.js +3 -0
- package/lib/commonjs/providers/xhrStream.js.map +1 -1
- package/lib/commonjs/session.js +32 -5
- package/lib/commonjs/session.js.map +1 -1
- package/lib/module/catalog/catalog.g.js +93 -6
- package/lib/module/catalog/catalog.g.js.map +1 -1
- package/lib/module/catalog/catalog.source.json +106 -4
- package/lib/module/catalog/catalog.types.g.js +3 -3
- package/lib/module/catalog/catalog.types.g.js.map +1 -1
- package/lib/module/catalog/toProviderTools.js +3 -1
- package/lib/module/catalog/toProviderTools.js.map +1 -1
- package/lib/module/effects/ledger.js +13 -1
- package/lib/module/effects/ledger.js.map +1 -1
- package/lib/module/engine/evidence.js +107 -0
- package/lib/module/engine/evidence.js.map +1 -0
- package/lib/module/engine/historyBudget.js +158 -54
- package/lib/module/engine/historyBudget.js.map +1 -1
- package/lib/module/engine/retrieve.js +210 -0
- package/lib/module/engine/retrieve.js.map +1 -0
- package/lib/module/engine/runAgentTurn.js +365 -96
- package/lib/module/engine/runAgentTurn.js.map +1 -1
- package/lib/module/engine/systemPrompt.js +30 -1
- package/lib/module/engine/systemPrompt.js.map +1 -1
- package/lib/module/engine/tokenCalibration.js +75 -0
- package/lib/module/engine/tokenCalibration.js.map +1 -0
- package/lib/module/engine/verify.js +292 -0
- package/lib/module/engine/verify.js.map +1 -0
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/providers/anthropic.js +12 -3
- package/lib/module/providers/anthropic.js.map +1 -1
- package/lib/module/providers/openai.js +11 -3
- package/lib/module/providers/openai.js.map +1 -1
- package/lib/module/providers/problem.js +92 -0
- package/lib/module/providers/problem.js.map +1 -0
- package/lib/module/providers/sse.js +6 -5
- package/lib/module/providers/sse.js.map +1 -1
- package/lib/module/providers/transport.js +20 -4
- package/lib/module/providers/transport.js.map +1 -1
- package/lib/module/providers/xhrStream.js +3 -0
- package/lib/module/providers/xhrStream.js.map +1 -1
- package/lib/module/session.js +33 -6
- package/lib/module/session.js.map +1 -1
- package/lib/typescript/catalog/catalog.g.d.ts +2 -2
- package/lib/typescript/catalog/catalog.g.d.ts.map +1 -1
- package/lib/typescript/catalog/catalog.types.g.d.ts +3 -3
- package/lib/typescript/catalog/catalog.types.g.d.ts.map +1 -1
- package/lib/typescript/catalog/toProviderTools.d.ts.map +1 -1
- package/lib/typescript/effects/ledger.d.ts +18 -0
- package/lib/typescript/effects/ledger.d.ts.map +1 -1
- package/lib/typescript/engine/evidence.d.ts +67 -0
- package/lib/typescript/engine/evidence.d.ts.map +1 -0
- package/lib/typescript/engine/historyBudget.d.ts +31 -8
- package/lib/typescript/engine/historyBudget.d.ts.map +1 -1
- package/lib/typescript/engine/retrieve.d.ts +32 -0
- package/lib/typescript/engine/retrieve.d.ts.map +1 -0
- package/lib/typescript/engine/runAgentTurn.d.ts +96 -2
- package/lib/typescript/engine/runAgentTurn.d.ts.map +1 -1
- package/lib/typescript/engine/systemPrompt.d.ts +39 -0
- package/lib/typescript/engine/systemPrompt.d.ts.map +1 -1
- package/lib/typescript/engine/tokenCalibration.d.ts +46 -0
- package/lib/typescript/engine/tokenCalibration.d.ts.map +1 -0
- package/lib/typescript/engine/verify.d.ts +67 -0
- package/lib/typescript/engine/verify.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +4 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/providers/anthropic.d.ts.map +1 -1
- package/lib/typescript/providers/openai.d.ts.map +1 -1
- package/lib/typescript/providers/problem.d.ts +50 -0
- package/lib/typescript/providers/problem.d.ts.map +1 -0
- package/lib/typescript/providers/sse.d.ts +4 -1
- package/lib/typescript/providers/sse.d.ts.map +1 -1
- package/lib/typescript/providers/transport.d.ts +8 -0
- package/lib/typescript/providers/transport.d.ts.map +1 -1
- package/lib/typescript/providers/types.d.ts +14 -0
- package/lib/typescript/providers/types.d.ts.map +1 -1
- package/lib/typescript/providers/xhrStream.d.ts +2 -0
- package/lib/typescript/providers/xhrStream.d.ts.map +1 -1
- package/lib/typescript/session.d.ts +15 -2
- package/lib/typescript/session.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +6 -0
- package/lib/typescript/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -3,12 +3,14 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.MAX_REQUEST_CHARS = exports.MAX_HISTORY_CHARS = void 0;
|
|
6
|
+
exports.MAX_REQUEST_TOKENS = exports.MAX_REQUEST_CHARS = exports.MAX_HISTORY_TOKENS = exports.MAX_HISTORY_CHARS = void 0;
|
|
7
7
|
exports.budgetForRequest = budgetForRequest;
|
|
8
8
|
exports.compressConsumed = compressConsumed;
|
|
9
9
|
exports.messageSize = messageSize;
|
|
10
10
|
exports.trimHistory = trimHistory;
|
|
11
11
|
exports.trimHistoryReport = trimHistoryReport;
|
|
12
|
+
var _evidence = require("./evidence");
|
|
13
|
+
var _tokenCalibration = require("./tokenCalibration");
|
|
12
14
|
/**
|
|
13
15
|
* How much conversation the model is allowed to be sent, and what gets cut.
|
|
14
16
|
*
|
|
@@ -52,14 +54,22 @@ exports.trimHistoryReport = trimHistoryReport;
|
|
|
52
54
|
* before — a round starts at a user message, so the assistant/tool-result
|
|
53
55
|
* pairing every provider requires stays intact.
|
|
54
56
|
*/
|
|
55
|
-
|
|
57
|
+
/**
|
|
58
|
+
* The limits are in TOKENS — the unit the provider enforces — and turned into
|
|
59
|
+
* characters at the ratio the session has measured (engine/tokenCalibration.ts).
|
|
60
|
+
* The `_CHARS` constants below are those limits at the default 4 chars/token,
|
|
61
|
+
* kept for callers that have no calibration in hand (tests, one-shot use).
|
|
62
|
+
*/
|
|
63
|
+
const MAX_HISTORY_TOKENS = exports.MAX_HISTORY_TOKENS = 37_500;
|
|
56
64
|
/**
|
|
57
65
|
* The ceiling on a WHOLE request: tools, system prompt, messages and the room
|
|
58
|
-
* reserved for the reply.
|
|
59
|
-
*
|
|
60
|
-
*
|
|
66
|
+
* reserved for the reply. 100k tokens fits the smallest context Ask Buoy is
|
|
67
|
+
* pointed at in practice (a 128k-token gateway) with headroom, and leaves
|
|
68
|
+
* Anthropic's 200k far from the limit.
|
|
61
69
|
*/
|
|
62
|
-
const
|
|
70
|
+
const MAX_REQUEST_TOKENS = exports.MAX_REQUEST_TOKENS = 100_000;
|
|
71
|
+
const MAX_HISTORY_CHARS = exports.MAX_HISTORY_CHARS = MAX_HISTORY_TOKENS * _tokenCalibration.DEFAULT_CHARS_PER_TOKEN;
|
|
72
|
+
const MAX_REQUEST_CHARS = exports.MAX_REQUEST_CHARS = MAX_REQUEST_TOKENS * _tokenCalibration.DEFAULT_CHARS_PER_TOKEN;
|
|
63
73
|
const MIN_KEEP_MESSAGES = 4;
|
|
64
74
|
/** Below this a marker costs more than the payload it replaces. */
|
|
65
75
|
const MIN_COMPRESS_CHARS = 400;
|
|
@@ -75,16 +85,21 @@ function messageSize(m) {
|
|
|
75
85
|
* Replace consumed tool results with a marker. See the note above
|
|
76
86
|
* MAX_HISTORY_CHARS for what "consumed" means and why the exemptions exist.
|
|
77
87
|
*/
|
|
78
|
-
function compressConsumed(messages) {
|
|
88
|
+
function compressConsumed(messages, exemptNewest = true) {
|
|
79
89
|
// The newest tool-results message: the model is about to answer from it.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
90
|
+
// Unless the caller says otherwise — `budgetForRequest` hands in the rounds
|
|
91
|
+
// BEFORE the current one, whose last result was answered long ago.
|
|
92
|
+
let newest = messages.length;
|
|
93
|
+
if (exemptNewest) {
|
|
94
|
+
newest = -1;
|
|
95
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
96
|
+
if (messages[i].role === "tool-results") {
|
|
97
|
+
newest = i;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
85
100
|
}
|
|
101
|
+
if (newest <= 0) return messages;
|
|
86
102
|
}
|
|
87
|
-
if (newest <= 0) return messages;
|
|
88
103
|
|
|
89
104
|
// The reads a later write depended on. An assistant turn that called a
|
|
90
105
|
// write was matching a shape it read in the round before, and that is the
|
|
@@ -109,9 +124,11 @@ function compressConsumed(messages) {
|
|
|
109
124
|
if (r.isError || r.content.length < MIN_COMPRESS_CHARS) return r;
|
|
110
125
|
if (r.content.startsWith("[earlier result")) return r;
|
|
111
126
|
touched = true;
|
|
127
|
+
// The ref survives the compression: the marker names it, and a later
|
|
128
|
+
// `retrieve` reads the full text back. See engine/evidence.ts.
|
|
112
129
|
return {
|
|
113
130
|
...r,
|
|
114
|
-
content:
|
|
131
|
+
content: (0, _evidence.compressedMarker)(r.content.length, r.ref)
|
|
115
132
|
};
|
|
116
133
|
});
|
|
117
134
|
if (!touched) return m;
|
|
@@ -131,6 +148,70 @@ function compressConsumed(messages) {
|
|
|
131
148
|
*/
|
|
132
149
|
const WRITE_ISH = /set|write|dispatch|navigate|tap|override|restore|delete|remove|clear|save|run|impersonat|reload/i;
|
|
133
150
|
|
|
151
|
+
/**
|
|
152
|
+
* A round: a user message and everything up to the next one. `pinned` when
|
|
153
|
+
* one of its tool calls made a change that is still applied — see
|
|
154
|
+
* `dropRounds`.
|
|
155
|
+
*/
|
|
156
|
+
|
|
157
|
+
function splitRounds(messages, pinnedCallIds) {
|
|
158
|
+
const rounds = [];
|
|
159
|
+
for (const m of messages) {
|
|
160
|
+
if (m.role === "user" || rounds.length === 0) rounds.push({
|
|
161
|
+
messages: [],
|
|
162
|
+
size: 0,
|
|
163
|
+
pinned: false
|
|
164
|
+
});
|
|
165
|
+
const round = rounds[rounds.length - 1];
|
|
166
|
+
round.messages.push(m);
|
|
167
|
+
round.size += messageSize(m);
|
|
168
|
+
if (pinnedCallIds?.size && m.role === "assistant" && m.toolCalls?.some(c => pinnedCallIds.has(c.id))) round.pinned = true;
|
|
169
|
+
}
|
|
170
|
+
return rounds;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Drop whole rounds, oldest first, until `keepSize + dropped` fits — but the
|
|
175
|
+
* PINNED rounds last.
|
|
176
|
+
*
|
|
177
|
+
* Dropping oldest-first was the whole rule, and in a QA session the oldest
|
|
178
|
+
* round is usually the SETUP: the override that was armed, the store that
|
|
179
|
+
* was written, the user someone is impersonating. Lose it and the model can
|
|
180
|
+
* no longer explain the screen it is looking at, or that a "wrong" response
|
|
181
|
+
* is its own mock — and the changes list in the prompt names the change but
|
|
182
|
+
* not the reasoning around it. The ledger knows which changes are still
|
|
183
|
+
* applied, so a round that made one is kept (compressed, like any other) for
|
|
184
|
+
* as long as an unpinned round remains to drop instead. Only when nothing
|
|
185
|
+
* else is left do pinned rounds go, oldest first, and the caller says so.
|
|
186
|
+
* Strands' `pin-message.ts` is the same idea, keyed on message metadata.
|
|
187
|
+
*
|
|
188
|
+
* `minMessages` is the floor the session-level trim keeps (a history that
|
|
189
|
+
* opens mid-exchange is rejected by every provider; the floor is applied on
|
|
190
|
+
* STARTING a drop, so a drop always runs to the next user boundary).
|
|
191
|
+
*/
|
|
192
|
+
function dropRounds(rounds, budget, keepSize, minMessages) {
|
|
193
|
+
const remaining = [...rounds];
|
|
194
|
+
let size = remaining.reduce((n, r) => n + r.size, 0);
|
|
195
|
+
let droppedRounds = 0;
|
|
196
|
+
let droppedPinned = 0;
|
|
197
|
+
const messageCount = () => remaining.reduce((n, r) => n + r.messages.length, 0);
|
|
198
|
+
while (remaining.length > 0 && size + keepSize > budget && messageCount() > minMessages) {
|
|
199
|
+
let i = remaining.findIndex(r => !r.pinned);
|
|
200
|
+
if (i < 0) {
|
|
201
|
+
i = 0;
|
|
202
|
+
droppedPinned += 1;
|
|
203
|
+
}
|
|
204
|
+
size -= remaining[i].size;
|
|
205
|
+
remaining.splice(i, 1);
|
|
206
|
+
droppedRounds += 1;
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
kept: remaining.flatMap(r => r.messages),
|
|
210
|
+
droppedRounds,
|
|
211
|
+
droppedPinned
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
134
215
|
/**
|
|
135
216
|
* Trim, and say how many whole rounds were dropped — the number the UI turns
|
|
136
217
|
* into "older messages were trimmed from the agent's memory".
|
|
@@ -141,37 +222,35 @@ const WRITE_ISH = /set|write|dispatch|navigate|tap|override|restore|delete|remov
|
|
|
141
222
|
* assistant or tool-results message — which every provider rejects. The
|
|
142
223
|
* minimum-keep guard belongs on STARTING a drop, not on finishing one.
|
|
143
224
|
*/
|
|
144
|
-
function trimHistoryReport(messages
|
|
225
|
+
function trimHistoryReport(messages, charsPerToken = _tokenCalibration.DEFAULT_CHARS_PER_TOKEN, /** Tool-call ids whose change is still applied — their rounds are dropped last. See dropRounds. */
|
|
226
|
+
pinnedCallIds) {
|
|
227
|
+
const limit = Math.floor(MAX_HISTORY_TOKENS * charsPerToken);
|
|
145
228
|
let total = 0;
|
|
146
229
|
for (const m of messages) total += messageSize(m);
|
|
147
|
-
if (total <=
|
|
230
|
+
if (total <= limit) return {
|
|
148
231
|
messages,
|
|
149
|
-
droppedRounds: 0
|
|
232
|
+
droppedRounds: 0,
|
|
233
|
+
droppedPinned: 0
|
|
150
234
|
};
|
|
151
235
|
|
|
152
236
|
// Compress before dropping — a compressed round still explains the screen.
|
|
153
237
|
const compressed = compressConsumed(messages);
|
|
154
238
|
let after = 0;
|
|
155
239
|
for (const m of compressed) after += messageSize(m);
|
|
156
|
-
if (after <=
|
|
240
|
+
if (after <= limit) return {
|
|
157
241
|
messages: compressed,
|
|
158
|
-
droppedRounds: 0
|
|
242
|
+
droppedRounds: 0,
|
|
243
|
+
droppedPinned: 0
|
|
159
244
|
};
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
out.shift();
|
|
166
|
-
while (out.length > 0 && out[0].role !== "user") {
|
|
167
|
-
total -= messageSize(out[0]);
|
|
168
|
-
out.shift();
|
|
169
|
-
}
|
|
170
|
-
droppedRounds += 1;
|
|
171
|
-
}
|
|
245
|
+
const {
|
|
246
|
+
kept,
|
|
247
|
+
droppedRounds,
|
|
248
|
+
droppedPinned
|
|
249
|
+
} = dropRounds(splitRounds(compressed, pinnedCallIds), limit, 0, MIN_KEEP_MESSAGES);
|
|
172
250
|
return {
|
|
173
|
-
messages:
|
|
174
|
-
droppedRounds
|
|
251
|
+
messages: kept,
|
|
252
|
+
droppedRounds,
|
|
253
|
+
droppedPinned
|
|
175
254
|
};
|
|
176
255
|
}
|
|
177
256
|
|
|
@@ -194,7 +273,15 @@ function trimHistory(messages) {
|
|
|
194
273
|
* installed), the system prompt, and the reply the model is allowed to write.
|
|
195
274
|
* Budgeting messages alone would leave the biggest fixed cost out of the sum.
|
|
196
275
|
*/
|
|
197
|
-
function budgetForRequest(messages, reserve
|
|
276
|
+
function budgetForRequest(messages, reserve,
|
|
277
|
+
/**
|
|
278
|
+
* A tighter ceiling on the messages than MAX_HISTORY_CHARS — set by the
|
|
279
|
+
* engine after the provider refused a request as too big. The proactive
|
|
280
|
+
* budget is an estimate; the provider's refusal is the truth.
|
|
281
|
+
*/
|
|
282
|
+
cap = MAX_HISTORY_CHARS, /** The session's measured ratio; scales the whole-request ceiling. */
|
|
283
|
+
charsPerToken = _tokenCalibration.DEFAULT_CHARS_PER_TOKEN, /** Tool-call ids whose change is still applied — their rounds are dropped last. See dropRounds. */
|
|
284
|
+
pinnedCallIds) {
|
|
198
285
|
/**
|
|
199
286
|
* `MAX_HISTORY_CHARS` is a MESSAGES budget and always was — subtracting the
|
|
200
287
|
* reserve from it is wrong by a wide margin. The tool block alone is ~94,000
|
|
@@ -208,12 +295,13 @@ function budgetForRequest(messages, reserve) {
|
|
|
208
295
|
* the fixed cost is counted. In an ordinary install the first wins and
|
|
209
296
|
* nothing changes; the second only bites when the tools are huge.
|
|
210
297
|
*/
|
|
211
|
-
const budget = Math.min(
|
|
298
|
+
const budget = Math.min(cap, Math.floor(MAX_REQUEST_TOKENS * charsPerToken) - reserve);
|
|
212
299
|
let total = 0;
|
|
213
300
|
for (const m of messages) total += messageSize(m);
|
|
214
301
|
if (total <= budget) return {
|
|
215
302
|
messages,
|
|
216
|
-
droppedRounds: 0
|
|
303
|
+
droppedRounds: 0,
|
|
304
|
+
droppedPinned: 0
|
|
217
305
|
};
|
|
218
306
|
|
|
219
307
|
// Where the current round begins. Nothing from here on is eligible.
|
|
@@ -224,37 +312,52 @@ function budgetForRequest(messages, reserve) {
|
|
|
224
312
|
break;
|
|
225
313
|
}
|
|
226
314
|
}
|
|
227
|
-
if (currentRound <= 0)
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
315
|
+
if (currentRound <= 0) {
|
|
316
|
+
// The whole history is this one round and it is over the limit by
|
|
317
|
+
// itself. Its consumed results are compressed, the newest kept — see the
|
|
318
|
+
// last-resort note below; this is the same move with nothing to drop.
|
|
319
|
+
return {
|
|
320
|
+
messages: compressConsumed(messages),
|
|
321
|
+
droppedRounds: 0,
|
|
322
|
+
droppedPinned: 0
|
|
323
|
+
};
|
|
324
|
+
}
|
|
231
325
|
const earlier = messages.slice(0, currentRound);
|
|
232
326
|
const current = messages.slice(currentRound);
|
|
233
327
|
let currentSize = 0;
|
|
234
328
|
for (const m of current) currentSize += messageSize(m);
|
|
235
|
-
|
|
329
|
+
|
|
330
|
+
// `false`: the earlier rounds' last result was answered long ago — the
|
|
331
|
+
// newest-result exemption is about the round being answered NOW.
|
|
332
|
+
const compressed = compressConsumed(earlier, false);
|
|
236
333
|
let earlierSize = 0;
|
|
237
334
|
for (const m of compressed) earlierSize += messageSize(m);
|
|
238
335
|
if (earlierSize + currentSize <= budget) {
|
|
239
336
|
return {
|
|
240
337
|
messages: [...compressed, ...current],
|
|
241
|
-
droppedRounds: 0
|
|
338
|
+
droppedRounds: 0,
|
|
339
|
+
droppedPinned: 0
|
|
242
340
|
};
|
|
243
341
|
}
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
342
|
+
const {
|
|
343
|
+
kept: out,
|
|
344
|
+
droppedRounds,
|
|
345
|
+
droppedPinned
|
|
346
|
+
} = dropRounds(splitRounds(compressed, pinnedCallIds), budget, currentSize, 0);
|
|
347
|
+
earlierSize = out.reduce((n, m) => n + messageSize(m), 0);
|
|
348
|
+
/**
|
|
349
|
+
* LAST RESORT, inside the current round. Everything earlier is gone and the
|
|
350
|
+
* turn's own reads are still over the limit — a twelve-step investigation
|
|
351
|
+
* of 24k-character results does that by itself. The consumed ones (an
|
|
352
|
+
* assistant message came after them) are compressed the same way earlier
|
|
353
|
+
* rounds were, newest result and the reads before a write still exempt.
|
|
354
|
+
* Their refs make this lossless for the model: `retrieve` reads them back.
|
|
355
|
+
*/
|
|
356
|
+
const currentOut = out.length === 0 && earlierSize + currentSize > budget ? compressConsumed(current) : current;
|
|
255
357
|
return {
|
|
256
|
-
messages: [...out, ...
|
|
257
|
-
droppedRounds
|
|
358
|
+
messages: [...out, ...currentOut],
|
|
359
|
+
droppedRounds,
|
|
360
|
+
droppedPinned
|
|
258
361
|
};
|
|
259
362
|
}
|
|
260
363
|
//# sourceMappingURL=historyBudget.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["_evidence","require","_tokenCalibration","MAX_HISTORY_TOKENS","exports","MAX_REQUEST_TOKENS","MAX_HISTORY_CHARS","DEFAULT_CHARS_PER_TOKEN","MAX_REQUEST_CHARS","MIN_KEEP_MESSAGES","MIN_COMPRESS_CHARS","messageSize","m","JSON","stringify","length","compressConsumed","messages","exemptNewest","newest","i","role","beforeWrite","Set","toolCalls","some","c","WRITE_ISH","test","input","name","j","add","changed","out","map","has","touched","results","r","isError","content","startsWith","compressedMarker","ref","splitRounds","pinnedCallIds","rounds","push","size","pinned","round","id","dropRounds","budget","keepSize","minMessages","remaining","reduce","n","droppedRounds","droppedPinned","messageCount","findIndex","splice","kept","flatMap","trimHistoryReport","charsPerToken","limit","Math","floor","total","compressed","after","trimHistory","budgetForRequest","reserve","cap","min","currentRound","earlier","slice","current","currentSize","earlierSize","currentOut"],"sourceRoot":"../../../src","sources":["engine/historyBudget.ts"],"mappings":";;;;;;;;;;;AAaA,IAAAA,SAAA,GAAAC,OAAA;AACA,IAAAC,iBAAA,GAAAD,OAAA;AAdA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAKA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAME,kBAAkB,GAAAC,OAAA,CAAAD,kBAAA,GAAG,MAAM;AACxC;AACA;AACA;AACA;AACA;AACA;AACO,MAAME,kBAAkB,GAAAD,OAAA,CAAAC,kBAAA,GAAG,OAAO;AAClC,MAAMC,iBAAiB,GAAAF,OAAA,CAAAE,iBAAA,GAAGH,kBAAkB,GAAGI,yCAAuB;AACtE,MAAMC,iBAAiB,GAAAJ,OAAA,CAAAI,iBAAA,GAAGH,kBAAkB,GAAGE,yCAAuB;AAC7E,MAAME,iBAAiB,GAAG,CAAC;AAC3B;AACA,MAAMC,kBAAkB,GAAG,GAAG;AAEvB,SAASC,WAAWA,CAACC,CAAe,EAAU;EACnD,IAAI;IACF,OAAOC,IAAI,CAACC,SAAS,CAACF,CAAC,CAAC,CAACG,MAAM;EACjC,CAAC,CAAC,MAAM;IACN,OAAO,CAAC;EACV;AACF;;AAEA;AACA;AACA;AACA;AACO,SAASC,gBAAgBA,CAACC,QAAwB,EAAEC,YAAY,GAAG,IAAI,EAAkB;EAC9F;EACA;EACA;EACA,IAAIC,MAAM,GAAGF,QAAQ,CAACF,MAAM;EAC5B,IAAIG,YAAY,EAAE;IAChBC,MAAM,GAA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A;AACA;AACA;AACA;EACE,MAAM0E,UAAU,GAAGpD,GAAG,CAACnB,MAAM,KAAK,CAAC,IAAIsE,WAAW,GAAGD,WAAW,GAAG9B,MAAM,GAAGtC,gBAAgB,CAACmE,OAAO,CAAC,GAAGA,OAAO;EAC/G,OAAO;IAAElE,QAAQ,EAAE,CAAC,GAAGiB,GAAG,EAAE,GAAGoD,UAAU,CAAC;IAAE1B,aAAa;IAAEC;EAAc,CAAC;AAC5E","ignoreList":[]}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.retrieveEvidence = retrieveEvidence;
|
|
7
|
+
/**
|
|
8
|
+
* `ask-buoy.retrieve` — read part of a kept result back. See evidence.ts.
|
|
9
|
+
*
|
|
10
|
+
* Four ways in, chosen so the model never needs the whole thing again:
|
|
11
|
+
*
|
|
12
|
+
* - nothing but `ref` → the SHAPE: top-level keys with type and size, so the
|
|
13
|
+
* next call can name a path. Handing back the full
|
|
14
|
+
* text would only hit the same cap that cut it.
|
|
15
|
+
* - `path` → the value at a dotted / slash path ("stats.0.base_stat",
|
|
16
|
+
* "/moves/3/move/name"). Arrays index by number.
|
|
17
|
+
* - `slice: [a, b]` → a window: items of an array, characters of a string.
|
|
18
|
+
* Combines with `path`.
|
|
19
|
+
* - `pattern` → a literal, case-insensitive substring search over the
|
|
20
|
+
* pretty-printed text; matching lines with
|
|
21
|
+
* `contextLines` (default 2) around each. Not a regex:
|
|
22
|
+
* a model that means "height" should not have to
|
|
23
|
+
* escape anything, and an executable pattern over a
|
|
24
|
+
* store of app data is a surface nobody asked for.
|
|
25
|
+
*
|
|
26
|
+
* Every result is capped at the same size as any other tool result, and the
|
|
27
|
+
* engine never stashes a retrieve result — a retrieve of a retrieve is a loop.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
const MAX_MATCHES = 20;
|
|
31
|
+
const MAX_SHAPE_KEYS = 60;
|
|
32
|
+
const DEFAULT_CONTEXT_LINES = 2;
|
|
33
|
+
function retrieveEvidence(store, params) {
|
|
34
|
+
if (!store) {
|
|
35
|
+
return {
|
|
36
|
+
ok: false,
|
|
37
|
+
error: "There is no evidence store in this session, so earlier results cannot be re-read. Call the tool again for the current value."
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
const entry = store.get(params.ref);
|
|
41
|
+
if (!entry) {
|
|
42
|
+
return store.wasEvicted(params.ref) ? {
|
|
43
|
+
ok: false,
|
|
44
|
+
error: `${params.ref} was kept but has since been evicted to make room (the store holds the most recent ~4 MB of results). Call the tool again for the current value.`
|
|
45
|
+
} : {
|
|
46
|
+
ok: false,
|
|
47
|
+
error: `No result with ref ${params.ref}. Refs look like ev_12 and appear in [truncated — …] and [earlier result — …] markers.`
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const meta = {
|
|
51
|
+
ref: entry.ref,
|
|
52
|
+
from: `${entry.toolId}.${entry.action}`,
|
|
53
|
+
capturedAt: new Date(entry.capturedAt).toISOString(),
|
|
54
|
+
totalChars: entry.text.length
|
|
55
|
+
};
|
|
56
|
+
let parsed;
|
|
57
|
+
let isJson = true;
|
|
58
|
+
try {
|
|
59
|
+
parsed = JSON.parse(entry.text);
|
|
60
|
+
} catch {
|
|
61
|
+
isJson = false;
|
|
62
|
+
}
|
|
63
|
+
if (params.pattern !== undefined && params.pattern !== "") {
|
|
64
|
+
return {
|
|
65
|
+
...meta,
|
|
66
|
+
...search(isJson ? JSON.stringify(parsed, null, 1) : entry.text, params.pattern, params.contextLines ?? DEFAULT_CONTEXT_LINES)
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
if (!isJson) {
|
|
70
|
+
const text = params.slice ? entry.text.slice(params.slice[0], params.slice[1]) : entry.text;
|
|
71
|
+
return {
|
|
72
|
+
...meta,
|
|
73
|
+
text
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
let value = parsed;
|
|
77
|
+
let at = "(root)";
|
|
78
|
+
if (params.path) {
|
|
79
|
+
const resolved = resolvePath(parsed, params.path);
|
|
80
|
+
if (!resolved.ok) return {
|
|
81
|
+
...meta,
|
|
82
|
+
ok: false,
|
|
83
|
+
error: resolved.error
|
|
84
|
+
};
|
|
85
|
+
value = resolved.value;
|
|
86
|
+
at = params.path;
|
|
87
|
+
}
|
|
88
|
+
if (params.slice) {
|
|
89
|
+
const [a, b] = params.slice;
|
|
90
|
+
if (Array.isArray(value)) return {
|
|
91
|
+
...meta,
|
|
92
|
+
at,
|
|
93
|
+
sliced: [a, b],
|
|
94
|
+
of: value.length,
|
|
95
|
+
value: value.slice(a, b)
|
|
96
|
+
};
|
|
97
|
+
if (typeof value === "string") return {
|
|
98
|
+
...meta,
|
|
99
|
+
at,
|
|
100
|
+
sliced: [a, b],
|
|
101
|
+
of: value.length,
|
|
102
|
+
value: value.slice(a, b)
|
|
103
|
+
};
|
|
104
|
+
return {
|
|
105
|
+
...meta,
|
|
106
|
+
ok: false,
|
|
107
|
+
error: `slice applies to an array or a string; the value at ${at} is ${typeOf(value)}.`
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (!params.path) return {
|
|
111
|
+
...meta,
|
|
112
|
+
shape: shapeOf(parsed),
|
|
113
|
+
hint: "Pass path (e.g. \"stats.0\") or pattern to read a part."
|
|
114
|
+
};
|
|
115
|
+
return {
|
|
116
|
+
...meta,
|
|
117
|
+
at,
|
|
118
|
+
value
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ── helpers ──────────────────────────────────────────────────────────────────
|
|
123
|
+
|
|
124
|
+
function typeOf(v) {
|
|
125
|
+
if (v === null) return "null";
|
|
126
|
+
if (Array.isArray(v)) return "array";
|
|
127
|
+
return typeof v;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Top-level keys (or array items) with a type and a size, for choosing a path. */
|
|
131
|
+
function shapeOf(v) {
|
|
132
|
+
if (Array.isArray(v)) {
|
|
133
|
+
return {
|
|
134
|
+
type: "array",
|
|
135
|
+
length: v.length,
|
|
136
|
+
first: v.length ? describe(v[0]) : undefined
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
if (v && typeof v === "object") {
|
|
140
|
+
const keys = Object.keys(v);
|
|
141
|
+
const shown = keys.slice(0, MAX_SHAPE_KEYS);
|
|
142
|
+
const out = {};
|
|
143
|
+
for (const k of shown) out[k] = describe(v[k]);
|
|
144
|
+
return keys.length > shown.length ? {
|
|
145
|
+
...out,
|
|
146
|
+
"…": `${keys.length - shown.length} more keys`
|
|
147
|
+
} : out;
|
|
148
|
+
}
|
|
149
|
+
return describe(v);
|
|
150
|
+
}
|
|
151
|
+
function describe(v) {
|
|
152
|
+
if (v === null || v === undefined) return String(v);
|
|
153
|
+
if (Array.isArray(v)) return `array(${v.length})`;
|
|
154
|
+
if (typeof v === "object") {
|
|
155
|
+
const keys = Object.keys(v);
|
|
156
|
+
const size = JSON.stringify(v).length;
|
|
157
|
+
return `object{${keys.slice(0, 6).join(", ")}${keys.length > 6 ? ", …" : ""}} ~${size.toLocaleString("en-US")} chars`;
|
|
158
|
+
}
|
|
159
|
+
if (typeof v === "string") return v.length > 40 ? `string(${v.length})` : JSON.stringify(v);
|
|
160
|
+
return `${typeof v}: ${String(v)}`;
|
|
161
|
+
}
|
|
162
|
+
function resolvePath(root, path) {
|
|
163
|
+
const segs = path.replace(/^\/+/, "").split(/[./]/).filter(s => s.length > 0);
|
|
164
|
+
let cur = root;
|
|
165
|
+
const walked = [];
|
|
166
|
+
for (const seg of segs) {
|
|
167
|
+
if (cur === null || typeof cur !== "object") {
|
|
168
|
+
return {
|
|
169
|
+
ok: false,
|
|
170
|
+
error: `Cannot go into ${walked.join(".") || "(root)"} — it is ${typeOf(cur)}.`
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
if (Array.isArray(cur)) {
|
|
174
|
+
const i = Number(seg);
|
|
175
|
+
if (!Number.isInteger(i) || i < 0 || i >= cur.length) {
|
|
176
|
+
return {
|
|
177
|
+
ok: false,
|
|
178
|
+
error: `${walked.join(".") || "(root)"} is an array of ${cur.length}; "${seg}" is not an index in it.`
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
cur = cur[i];
|
|
182
|
+
} else {
|
|
183
|
+
const rec = cur;
|
|
184
|
+
if (!(seg in rec)) {
|
|
185
|
+
return {
|
|
186
|
+
ok: false,
|
|
187
|
+
error: `No field "${seg}" at ${walked.join(".") || "(root)"}. It has: ${Object.keys(rec).slice(0, 30).join(", ")}.`
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
cur = rec[seg];
|
|
191
|
+
}
|
|
192
|
+
walked.push(seg);
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
ok: true,
|
|
196
|
+
value: cur
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function search(text, pattern, contextLines) {
|
|
200
|
+
const lines = text.split("\n");
|
|
201
|
+
const needle = pattern.toLowerCase();
|
|
202
|
+
const hits = [];
|
|
203
|
+
for (let i = 0; i < lines.length; i++) if (lines[i].toLowerCase().includes(needle)) hits.push(i);
|
|
204
|
+
const ctx = Math.max(0, Math.min(10, contextLines));
|
|
205
|
+
const matches = hits.slice(0, MAX_MATCHES).map(i => ({
|
|
206
|
+
line: i + 1,
|
|
207
|
+
text: lines.slice(Math.max(0, i - ctx), i + ctx + 1).join("\n")
|
|
208
|
+
}));
|
|
209
|
+
return {
|
|
210
|
+
matches,
|
|
211
|
+
totalMatches: hits.length
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
//# sourceMappingURL=retrieve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["MAX_MATCHES","MAX_SHAPE_KEYS","DEFAULT_CONTEXT_LINES","retrieveEvidence","store","params","ok","error","entry","get","ref","wasEvicted","meta","from","toolId","action","capturedAt","Date","toISOString","totalChars","text","length","parsed","isJson","JSON","parse","pattern","undefined","search","stringify","contextLines","slice","value","at","path","resolved","resolvePath","a","b","Array","isArray","sliced","of","typeOf","shape","shapeOf","hint","v","type","first","describe","keys","Object","shown","out","k","String","size","join","toLocaleString","root","segs","replace","split","filter","s","cur","walked","seg","i","Number","isInteger","rec","push","lines","needle","toLowerCase","hits","includes","ctx","Math","max","min","matches","map","line","totalMatches"],"sourceRoot":"../../../src","sources":["engine/retrieve.ts"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAWA,MAAMA,WAAW,GAAG,EAAE;AACtB,MAAMC,cAAc,GAAG,EAAE;AACzB,MAAMC,qBAAqB,GAAG,CAAC;AAExB,SAASC,gBAAgBA,CAACC,KAAgC,EAAEC,MAAsB,EAAW;EAClG,IAAI,CAACD,KAAK,EAAE;IACV,OAAO;MAAEE,EAAE,EAAE,KAAK;MAAEC,KAAK,EAAE;IAA+H,CAAC;EAC7J;EACA,MAAMC,KAAK,GAAGJ,KAAK,CAACK,GAAG,CAACJ,MAAM,CAACK,GAAG,CAAC;EACnC,IAAI,CAACF,KAAK,EAAE;IACV,OAAOJ,KAAK,CAACO,UAAU,CAACN,MAAM,CAACK,GAAG,CAAC,GAC/B;MAAEJ,EAAE,EAAE,KAAK;MAAEC,KAAK,EAAE,GAAGF,MAAM,CAACK,GAAG;IAAmJ,CAAC,GACrL;MAAEJ,EAAE,EAAE,KAAK;MAAEC,KAAK,EAAE,sBAAsBF,MAAM,CAACK,GAAG;IAAyF,CAAC;EACpJ;EAEA,MAAME,IAAI,GAAG;IACXF,GAAG,EAAEF,KAAK,CAACE,GAAG;IACdG,IAAI,EAAE,GAAGL,KAAK,CAACM,MAAM,IAAIN,KAAK,CAACO,MAAM,EAAE;IACvCC,UAAU,EAAE,IAAIC,IAAI,CAACT,KAAK,CAACQ,UAAU,CAAC,CAACE,WAAW,CAAC,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