@beeos-ai/bma-dsh-adapter 0.3.0 → 0.3.4
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 +1 -1
- package/lib/adapter.d.ts +24 -1
- package/lib/adapter.js +231 -27
- package/lib/conformance/index.js +2 -2
- package/lib/context-store.d.ts +39 -0
- package/lib/context-store.js +95 -0
- package/lib/index.d.ts +5 -2
- package/lib/index.js +4 -2
- package/lib/kernel.d.ts +7 -0
- package/lib/kernel.js +11 -1
- package/lib/model.d.ts +70 -7
- package/lib/model.js +278 -42
- package/lib/request-budget.d.ts +51 -0
- package/lib/request-budget.js +246 -0
- package/lib/spi.d.ts +62 -7
- package/lib/stop.d.ts +1 -1
- package/lib/stop.js +26 -2
- package/lib/testing/scripted-model.d.ts +6 -0
- package/lib/testing/scripted-model.js +5 -1
- package/lib/testing/tmp.js +3 -1
- package/package.json +1 -1
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// Fits one Chat Completions request into the model's window (tokens) and the model endpoint's body
|
|
2
|
+
// limit (bytes). Only the request is rewritten; dsh's durable history stays as it is. The levels of
|
|
3
|
+
// the ladder, each used only when the one before is not enough:
|
|
4
|
+
// 1 estimate tokens and body bytes before sending (fitRequest measures every candidate)
|
|
5
|
+
// 2 trim bulk: older images out, old tool results cleared, the largest tool texts cut head + tail
|
|
6
|
+
// 3 dsh summary compaction (not here: model.ts hands the step to dsh, see ChatCompletionsAdapter)
|
|
7
|
+
// 4 sliding window: the oldest whole messages go, tool calls together with their results
|
|
8
|
+
// The newest user input is never cut and keeps its images: a request still over budget is not sent
|
|
9
|
+
// and fails with REQUEST_TOO_LARGE or the context overflow code, naming the budget (model.ts).
|
|
10
|
+
// Every request, over budget or not, also keeps only the RECENT_IMAGES newest images and caps each
|
|
11
|
+
// tool result at toolTextCap characters.
|
|
12
|
+
/**
|
|
13
|
+
* Body budget. The model gateway accepts 64 MiB (HTTP 413 request_too_large above it), but upstream
|
|
14
|
+
* providers are tighter: Bedrock Claude and Gemini refuse a request over 20 MB. 16 MiB stays under
|
|
15
|
+
* those with room for the gateway's format conversion and estimate drift. The one source is
|
|
16
|
+
* contracts/bma/model-request-budget.vectors.json (the Go brain and legacy are locked to it too).
|
|
17
|
+
*/
|
|
18
|
+
export const MAX_REQUEST_BODY_BYTES = 16 << 20;
|
|
19
|
+
/**
|
|
20
|
+
* Images sent with a request, newest first, besides those of the newest user input (always sent).
|
|
21
|
+
* Older ones are a text placeholder. Basis: Anthropic computer use reference implementation
|
|
22
|
+
* (only_n_most_recent_images = 3). Anthropic also removes them in batches so the cached prefix
|
|
23
|
+
* moves less often; a batch would carry up to 3 + interval images, which is what overflowed the
|
|
24
|
+
* gateway, so the cap is exact: each new image changes the request from the 4th newest image on,
|
|
25
|
+
* the prefix before it stays cached.
|
|
26
|
+
*/
|
|
27
|
+
export const RECENT_IMAGES = 3;
|
|
28
|
+
/** Tool results never cleared by level 2 (Anthropic context editing clear_tool_uses: keep 3). */
|
|
29
|
+
export const RECENT_TOOL_RESULTS = 3;
|
|
30
|
+
/** Token estimate of one image: about 1.15 megapixels at 750 px per token (Anthropic vision guide). */
|
|
31
|
+
export const IMAGE_TOKENS = 1_600;
|
|
32
|
+
/** Floor of the per-result tool text cap; the cap itself is the window in characters (about a quarter of it in tokens). */
|
|
33
|
+
export const TOOL_TEXT_FLOOR = 16_384;
|
|
34
|
+
/** Level 2 never cuts a tool text below this many characters. */
|
|
35
|
+
const MIN_TOOL_TEXT = 1_024;
|
|
36
|
+
export const OLDER_IMAGE = "[image omitted: older image, not resent]";
|
|
37
|
+
const DROPPED_IMAGE = "[image omitted: not resent, to fit the model request]";
|
|
38
|
+
const CLEARED_TOOL_RESULT = "[tool result cleared to fit the model request; run the tool again if it is needed]";
|
|
39
|
+
const omitted = (n) => `[${n} earlier message(s) omitted to fit the model request]`;
|
|
40
|
+
/** Messages toOpenAi made to carry the images of tool results (they belong to the assistant step before them). */
|
|
41
|
+
const toolImageMessages = new WeakSet();
|
|
42
|
+
export function toolImageMessage(content) {
|
|
43
|
+
const m = { role: "user", content };
|
|
44
|
+
toolImageMessages.add(m);
|
|
45
|
+
return m;
|
|
46
|
+
}
|
|
47
|
+
/** "<head>\n[... N characters truncated ...]\n<tail>" keeping `keep` characters, or the text when it is short enough. */
|
|
48
|
+
export function headTail(text, keep) {
|
|
49
|
+
if (text.length <= keep)
|
|
50
|
+
return text;
|
|
51
|
+
const head = Math.ceil(keep / 2);
|
|
52
|
+
return `${text.slice(0, head)}\n[... ${text.length - keep} characters truncated ...]\n${text.slice(text.length - (keep - head))}`;
|
|
53
|
+
}
|
|
54
|
+
function measure(m) {
|
|
55
|
+
let text = 0;
|
|
56
|
+
let images = 0;
|
|
57
|
+
if (typeof m.content === "string")
|
|
58
|
+
text += Buffer.byteLength(m.content);
|
|
59
|
+
else if (Array.isArray(m.content)) {
|
|
60
|
+
for (const p of m.content) {
|
|
61
|
+
if (p.type === "image_url")
|
|
62
|
+
images++;
|
|
63
|
+
else if (typeof p.text === "string")
|
|
64
|
+
text += Buffer.byteLength(p.text);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (const c of m.tool_calls ?? [])
|
|
68
|
+
text += Buffer.byteLength(c.function.name) + Buffer.byteLength(c.function.arguments);
|
|
69
|
+
// UTF-8 bytes / 4: ASCII is about 4 characters a token; CJK (3 bytes a character) comes out near 1 token a character.
|
|
70
|
+
return { bytes: Buffer.byteLength(JSON.stringify(m)), tokens: Math.ceil(text / 4) + 4 + images * IMAGE_TOKENS };
|
|
71
|
+
}
|
|
72
|
+
const isImage = (p) => p.type === "image_url";
|
|
73
|
+
const isSummary = (m) => m.role === "user" && textOf(m).includes("<compacted-summary>");
|
|
74
|
+
function textOf(m) {
|
|
75
|
+
if (typeof m.content === "string")
|
|
76
|
+
return m.content;
|
|
77
|
+
return Array.isArray(m.content) ? m.content.map((p) => (typeof p.text === "string" ? p.text : "")).join("") : "";
|
|
78
|
+
}
|
|
79
|
+
/** A copy with new content; a tool image message stays one. */
|
|
80
|
+
function withContent(m, content) {
|
|
81
|
+
const copy = { ...m, content };
|
|
82
|
+
if (toolImageMessages.has(m))
|
|
83
|
+
toolImageMessages.add(copy);
|
|
84
|
+
return copy;
|
|
85
|
+
}
|
|
86
|
+
/** Index of the newest user input: the last user message that is neither a tool image message nor a summary. */
|
|
87
|
+
function latestInput(ms) {
|
|
88
|
+
for (let i = ms.length - 1; i >= 0; i--)
|
|
89
|
+
if (ms[i].role === "user" && !toolImageMessages.has(ms[i]) && !isSummary(ms[i]))
|
|
90
|
+
return i;
|
|
91
|
+
return -1;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Fits `messages` (system first, as toOpenAi made them) into `budget`. `rest` is the request body
|
|
95
|
+
* without its messages (tools and parameters count too). `lastResort` allows level 4: the
|
|
96
|
+
* caller sets it once dsh compaction (level 3) could not make the request fit.
|
|
97
|
+
*/
|
|
98
|
+
export function fitRequest(messages, rest, budget, toolTextCap, lastResort) {
|
|
99
|
+
const sizes = new WeakMap();
|
|
100
|
+
const size = (m) => {
|
|
101
|
+
let s = sizes.get(m);
|
|
102
|
+
if (!s)
|
|
103
|
+
sizes.set(m, (s = measure(m)));
|
|
104
|
+
return s;
|
|
105
|
+
};
|
|
106
|
+
const base = { bytes: Buffer.byteLength(JSON.stringify({ ...rest, messages: [] })), tokens: Math.ceil(Buffer.byteLength(JSON.stringify(rest.tools ?? [])) / 4) };
|
|
107
|
+
const total = (ms) => {
|
|
108
|
+
const t = { bytes: base.bytes + Math.max(0, ms.length - 1), tokens: base.tokens };
|
|
109
|
+
for (const m of ms) {
|
|
110
|
+
const s = size(m);
|
|
111
|
+
t.bytes += s.bytes;
|
|
112
|
+
t.tokens += s.tokens;
|
|
113
|
+
}
|
|
114
|
+
return t;
|
|
115
|
+
};
|
|
116
|
+
const overOf = (ms) => {
|
|
117
|
+
const t = total(ms);
|
|
118
|
+
return t.bytes > budget.bytes ? "bytes" : t.tokens > budget.tokens ? "tokens" : undefined;
|
|
119
|
+
};
|
|
120
|
+
let ms = keepRecentImages(capToolText([...messages], toolTextCap));
|
|
121
|
+
const levels = [];
|
|
122
|
+
const level = (n) => { if (!levels.includes(n))
|
|
123
|
+
levels.push(n); };
|
|
124
|
+
// Level 2: images first (they are most of the bytes), then old tool results, then the largest tool texts.
|
|
125
|
+
for (const keep of [1, 0]) {
|
|
126
|
+
if (overOf(ms) !== "bytes")
|
|
127
|
+
break;
|
|
128
|
+
const next = dropImages(ms, keep, DROPPED_IMAGE);
|
|
129
|
+
if (next !== ms) {
|
|
130
|
+
ms = next;
|
|
131
|
+
level(2);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (overOf(ms)) {
|
|
135
|
+
const tools = ms.flatMap((m, i) => (m.role === "tool" ? [i] : [])).slice(0, -RECENT_TOOL_RESULTS || undefined);
|
|
136
|
+
for (const i of tools) {
|
|
137
|
+
if (!overOf(ms))
|
|
138
|
+
break;
|
|
139
|
+
if (ms[i].content === CLEARED_TOOL_RESULT)
|
|
140
|
+
continue;
|
|
141
|
+
ms[i] = withContent(ms[i], CLEARED_TOOL_RESULT);
|
|
142
|
+
level(2);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (overOf(ms)) {
|
|
146
|
+
const original = new Map();
|
|
147
|
+
for (;;) {
|
|
148
|
+
if (!overOf(ms))
|
|
149
|
+
break;
|
|
150
|
+
let largest = -1;
|
|
151
|
+
for (let i = 0; i < ms.length; i++)
|
|
152
|
+
if (ms[i].role === "tool" && typeof ms[i].content === "string" && (largest < 0 || ms[i].content.length > ms[largest].content.length))
|
|
153
|
+
largest = i;
|
|
154
|
+
if (largest < 0)
|
|
155
|
+
break;
|
|
156
|
+
const text = original.get(largest) ?? ms[largest].content;
|
|
157
|
+
const keep = Math.floor(ms[largest].content.length / 2);
|
|
158
|
+
if (keep < MIN_TOOL_TEXT)
|
|
159
|
+
break;
|
|
160
|
+
original.set(largest, text);
|
|
161
|
+
ms[largest] = withContent(ms[largest], headTail(text, keep));
|
|
162
|
+
level(2);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (lastResort && overOf(ms)) {
|
|
166
|
+
const windowed = slidingWindow(ms, (candidate) => !overOf(candidate));
|
|
167
|
+
if (windowed !== ms) {
|
|
168
|
+
ms = windowed;
|
|
169
|
+
level(4);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const t = total(ms);
|
|
173
|
+
return { messages: ms, bytes: t.bytes, tokens: t.tokens, levels, ...(overOf(ms) ? { over: overOf(ms) } : {}) };
|
|
174
|
+
}
|
|
175
|
+
/** Every tool result at most `cap` characters, head + tail. */
|
|
176
|
+
function capToolText(ms, cap) {
|
|
177
|
+
return ms.map((m) => (m.role === "tool" && typeof m.content === "string" && m.content.length > cap ? withContent(m, headTail(m.content, cap)) : m));
|
|
178
|
+
}
|
|
179
|
+
/** RECENT_IMAGES newest images kept besides those of the newest user input; older ones are a placeholder. */
|
|
180
|
+
function keepRecentImages(ms) {
|
|
181
|
+
return dropImages(ms, RECENT_IMAGES, OLDER_IMAGE);
|
|
182
|
+
}
|
|
183
|
+
/** Keeps the `keep` newest images outside the newest user input; the others become `placeholder`. Same array when nothing changed. */
|
|
184
|
+
function dropImages(ms, keep, placeholder) {
|
|
185
|
+
const input = latestInput(ms);
|
|
186
|
+
let seen = 0;
|
|
187
|
+
let out = ms;
|
|
188
|
+
for (let i = ms.length - 1; i >= 0; i--) {
|
|
189
|
+
if (i === input || !Array.isArray(ms[i].content))
|
|
190
|
+
continue;
|
|
191
|
+
const parts = ms[i].content;
|
|
192
|
+
let changed;
|
|
193
|
+
for (let j = parts.length - 1; j >= 0; j--) {
|
|
194
|
+
if (!isImage(parts[j]) || ++seen <= keep)
|
|
195
|
+
continue;
|
|
196
|
+
changed ??= [...parts];
|
|
197
|
+
changed[j] = { type: "text", text: placeholder };
|
|
198
|
+
}
|
|
199
|
+
if (changed) {
|
|
200
|
+
if (out === ms)
|
|
201
|
+
out = [...ms];
|
|
202
|
+
out[i] = withContent(ms[i], changed);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Level 4. Drops the fewest oldest units so that `fits`, each unit whole: an assistant message with
|
|
209
|
+
* its tool results and their image message, or one user message. System messages, summaries, the
|
|
210
|
+
* newest user input and the last unit stay. Each run of dropped units becomes one user note, so no
|
|
211
|
+
* tool result is left without its call (or a call without its result) and the model knows.
|
|
212
|
+
*/
|
|
213
|
+
function slidingWindow(ms, fits) {
|
|
214
|
+
const units = [];
|
|
215
|
+
for (let i = 0; i < ms.length; i++) {
|
|
216
|
+
const m = ms[i];
|
|
217
|
+
const joins = (m.role === "tool" || toolImageMessages.has(m)) && units.length > 0 && ms[units.at(-1)[0]].role === "assistant";
|
|
218
|
+
if (joins)
|
|
219
|
+
units.at(-1).push(i);
|
|
220
|
+
else
|
|
221
|
+
units.push([i]);
|
|
222
|
+
}
|
|
223
|
+
const input = latestInput(ms);
|
|
224
|
+
const droppable = units.slice(0, -1).filter((u) => !u.includes(input) && ms[u[0]].role !== "system" && !isSummary(ms[u[0]]));
|
|
225
|
+
let candidate = ms;
|
|
226
|
+
for (let k = 1; k <= droppable.length; k++) {
|
|
227
|
+
const dropped = new Set(droppable.slice(0, k).flat());
|
|
228
|
+
candidate = [];
|
|
229
|
+
let run = 0;
|
|
230
|
+
for (let i = 0; i <= ms.length; i++) {
|
|
231
|
+
if (i < ms.length && dropped.has(i)) {
|
|
232
|
+
run++;
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if (run) {
|
|
236
|
+
candidate.push({ role: "user", content: omitted(run) });
|
|
237
|
+
run = 0;
|
|
238
|
+
}
|
|
239
|
+
if (i < ms.length)
|
|
240
|
+
candidate.push(ms[i]);
|
|
241
|
+
}
|
|
242
|
+
if (fits(candidate))
|
|
243
|
+
return candidate;
|
|
244
|
+
}
|
|
245
|
+
return candidate;
|
|
246
|
+
}
|
package/lib/spi.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CompactRequest, CompactReceipt } from "./context-store.ts";
|
|
1
2
|
import type { InputSkillDocument, InputSkillReceipt } from "./input-skills.ts";
|
|
2
3
|
/** The six official agent_toolset tools a server-side brain may call (ADR 0005). */
|
|
3
4
|
export type ToolName = "bash" | "read" | "write" | "edit" | "glob" | "grep";
|
|
@@ -34,6 +35,8 @@ export interface PlatformTool {
|
|
|
34
35
|
}
|
|
35
36
|
/** Where one session's model requests go: the platform model endpoint with a session-scoped token. */
|
|
36
37
|
export interface ModelRoute {
|
|
38
|
+
/** Maintenance requests have no executable tools; unexpected tool calls fail closed. */
|
|
39
|
+
readonly toolsDisabled?: boolean;
|
|
37
40
|
/** OpenAI-compatible base URL ending in /v1. */
|
|
38
41
|
readonly baseUrl: string;
|
|
39
42
|
/** Session-scoped bearer token; the platform attributes every request carrying it to the session. */
|
|
@@ -82,14 +85,52 @@ export interface ModelPort {
|
|
|
82
85
|
* Hosts count it as model_stream_idle_retry_total{result} and log it.
|
|
83
86
|
*/
|
|
84
87
|
idleRetry?(sessionId: string, result: "retry" | "recovered" | "exhausted"): void;
|
|
88
|
+
/**
|
|
89
|
+
* A transient failure before any content (HTTP 502/503/504, `retryable: true`, a
|
|
90
|
+
* connection that failed before any response) and the adapter's reaction: "retry"
|
|
91
|
+
* (a fresh request follows after the backoff), "recovered" (a retry delivered
|
|
92
|
+
* content), "exhausted" (TRANSIENT_RETRIES used up, or the server's Retry-After is
|
|
93
|
+
* longer than the backoff cap; the error ends the turn as before). `reason` is short
|
|
94
|
+
* and safe to log: "HTTP 503 llm_gateway_unavailable" or "connection ECONNREFUSED",
|
|
95
|
+
* never the response body or a token. Hosts count it as
|
|
96
|
+
* model_transient_retry_total{result} and log it, like idleRetry.
|
|
97
|
+
*/
|
|
98
|
+
transientRetry?(sessionId: string, result: "retry" | "recovered" | "exhausted", reason?: string): void;
|
|
99
|
+
/** A step of the request size ladder (see RequestReduction); hosts count and log it. */
|
|
100
|
+
requestReduced?(sessionId: string, reduction: RequestReduction): void;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* How the adapter kept a model request inside the model window and the endpoint's body limit
|
|
104
|
+
* (dsh-adapter src/request-budget.ts). Levels: 2 older images, old tool results and long tool
|
|
105
|
+
* texts trimmed; 3 handed to dsh summary compaction; 4 oldest messages dropped (sliding window);
|
|
106
|
+
* 5 the newest user input shortened. Sizes and counts only, safe to log.
|
|
107
|
+
*/
|
|
108
|
+
export interface RequestReduction {
|
|
109
|
+
/**
|
|
110
|
+
* reduced: the request was trimmed and sent; compact: not sent, dsh compaction runs first;
|
|
111
|
+
* rejected: the endpoint refused the request for size, a smaller one follows unless exhausted
|
|
112
|
+
* comes next; exhausted: nothing fits, the turn fails.
|
|
113
|
+
*/
|
|
114
|
+
readonly step: "reduced" | "compact" | "rejected" | "exhausted";
|
|
115
|
+
readonly levels: readonly number[];
|
|
116
|
+
/** Body bytes and estimated input tokens of the request (as sent, or as it would have been). */
|
|
117
|
+
readonly bytes: number;
|
|
118
|
+
readonly tokens: number;
|
|
119
|
+
readonly byteBudget: number;
|
|
120
|
+
readonly tokenBudget: number;
|
|
121
|
+
/** HTTP status of a refusal. */
|
|
122
|
+
readonly status?: number;
|
|
85
123
|
}
|
|
86
124
|
export interface HarnessCapabilities {
|
|
87
125
|
/** Continues a session from the harness's own durable state in a fresh process. */
|
|
88
126
|
readonly resume: boolean;
|
|
89
127
|
/** Streams "delta" events before each message/thinking event. */
|
|
90
128
|
readonly deltas: boolean;
|
|
91
|
-
/**
|
|
92
|
-
|
|
129
|
+
/**
|
|
130
|
+
* Takes user input in the running turn at its next step boundary (turn.input → appendInput; agent.steering):
|
|
131
|
+
* after the results of the current tool calls, before the next model request.
|
|
132
|
+
*/
|
|
133
|
+
readonly steering: boolean;
|
|
93
134
|
/** Passes HarnessInput.images to the model. */
|
|
94
135
|
readonly imageInput: boolean;
|
|
95
136
|
}
|
|
@@ -133,7 +174,15 @@ export type StopCode =
|
|
|
133
174
|
/** The conversation no longer fits the context window after compaction. */
|
|
134
175
|
| "context_too_large"
|
|
135
176
|
/** The model stream sent no data for the idle limit. */
|
|
136
|
-
| "model_stream_idle"
|
|
177
|
+
| "model_stream_idle"
|
|
178
|
+
/** The model endpoint answered 5xx or 529. */
|
|
179
|
+
| "model_overloaded_error"
|
|
180
|
+
/** The model endpoint answered 429. */
|
|
181
|
+
| "model_rate_limited_error"
|
|
182
|
+
/** The model endpoint answered 402: the account has no credit. */
|
|
183
|
+
| "billing_error"
|
|
184
|
+
/** The model endpoint refused the request otherwise (any other 4xx, including a rejected credential). */
|
|
185
|
+
| "model_request_failed_error";
|
|
137
186
|
export declare class TurnStopError extends Error {
|
|
138
187
|
readonly name = "TurnStopError";
|
|
139
188
|
readonly code: StopCode;
|
|
@@ -175,9 +224,9 @@ export interface HarnessAdapter {
|
|
|
175
224
|
/** Loads a session this or an earlier process created (capabilities().resume). */
|
|
176
225
|
resumeSession(init: SessionInit): Promise<void>;
|
|
177
226
|
/**
|
|
178
|
-
* Replaces the session's
|
|
179
|
-
* turn; empty: none) next to the registered tools. Only an adapter
|
|
180
|
-
* offers it takes a turn with host tools.
|
|
227
|
+
* Replaces the session's turn tools (AgentTurnConfig.host_tools and mcp_tools
|
|
228
|
+
* of the next turn; empty: none) next to the registered tools. Only an adapter
|
|
229
|
+
* that offers it takes a turn with host or MCP tools.
|
|
181
230
|
*/
|
|
182
231
|
setHostTools?(sessionId: string, tools: readonly PlatformTool[]): void;
|
|
183
232
|
/** The tool names the model will see in this session. */
|
|
@@ -193,7 +242,13 @@ export interface HarnessAdapter {
|
|
|
193
242
|
prepareInputSkills?(sessionId: string, inputId: string, documents: readonly InputSkillDocument[]): InputSkillReceipt;
|
|
194
243
|
clearPreparedInputSkills?(sessionId: string, inputId: string): void;
|
|
195
244
|
submitInput(sessionId: string, input: HarnessInput, signal: AbortSignal): AsyncIterable<HarnessEvent>;
|
|
196
|
-
/**
|
|
245
|
+
/** Optional internal maintenance seam. A platform must reserve admission before calling. */
|
|
246
|
+
compact?(sessionId: string, request: CompactRequest, signal: AbortSignal): Promise<CompactReceipt>;
|
|
247
|
+
/** Readback never resumes, generates a summary or changes a selected pointer. */
|
|
248
|
+
compactReceipt?(sessionId: string, request: CompactRequest): CompactReceipt | undefined;
|
|
249
|
+
/** Atomically returns the applied receipt or fences future publication. No model call. */
|
|
250
|
+
abortCompact?(sessionId: string, request: CompactRequest): CompactReceipt | undefined;
|
|
251
|
+
/** Steers the running turn with input (read at its next step boundary); only when capabilities().steering. */
|
|
197
252
|
appendInput(sessionId: string, input: HarnessInput): void;
|
|
198
253
|
/** Durability barrier over the session's harness state (used before an approval wait). */
|
|
199
254
|
checkpoint(sessionId: string): Promise<void>;
|
package/lib/stop.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare function stopForTurnEnd(reason: DshTurnEndReason, context?: {
|
|
|
10
10
|
streamIdleMs?: number;
|
|
11
11
|
}): Error | undefined;
|
|
12
12
|
/** The contract's stop code table (AgentErrorCode), in contract order. */
|
|
13
|
-
export declare const AGENT_ERROR_CODES: readonly ["output_limit_reached", "context_too_large", "model_stream_idle", "agent_error", "agent_unavailable", "agent_auth_required", "agent_protocol_error"];
|
|
13
|
+
export declare const AGENT_ERROR_CODES: readonly ["output_limit_reached", "context_too_large", "model_stream_idle", "agent_error", "agent_unavailable", "agent_auth_required", "agent_protocol_error", "model_overloaded_error", "model_rate_limited_error", "model_request_failed_error", "billing_error"];
|
|
14
14
|
export type AgentErrorCode = (typeof AGENT_ERROR_CODES)[number];
|
|
15
15
|
/**
|
|
16
16
|
* The contract code a failed turn reports: a typed stop is its own code, any
|
package/lib/stop.js
CHANGED
|
@@ -7,13 +7,30 @@
|
|
|
7
7
|
// max-tokens output_limit_reached
|
|
8
8
|
// error, code CONTEXT_WINDOW_EXCEEDED context_too_large (compaction could not make it fit)
|
|
9
9
|
// error, code MODEL_STREAM_IDLE model_stream_idle
|
|
10
|
+
// error, code SERVER (5xx, 529) model_overloaded_error (model.ts names the class
|
|
11
|
+
// error, code RATE_LIMIT (429) model_rate_limited_error of a failed model request;
|
|
12
|
+
// error, code QUOTA (402) billing_error the detail is its message)
|
|
13
|
+
// error, code AUTH / PROVIDER_HTTP model_request_failed_error
|
|
14
|
+
// error, code REQUEST_TOO_LARGE model_request_failed_error (body over the endpoint limit after
|
|
15
|
+
// every reduction; the contract has
|
|
16
|
+
// no code of its own, the detail
|
|
17
|
+
// starts with request_too_large)
|
|
10
18
|
// anything else not completed agent_error: "dsh turn ended: <kind>[: <message>]"
|
|
11
19
|
//
|
|
12
20
|
// The codes are the executor contract's stop code table (contracts/bma/schemas/agent.json,
|
|
13
21
|
// $defs.AgentErrorCode); stop-contract.test.ts fails when the two drift.
|
|
14
|
-
import { CONTEXT_WINDOW_EXCEEDED_CODE } from "@deepseek-ai/dsh-llm";
|
|
22
|
+
import { CONTEXT_WINDOW_EXCEEDED_CODE, QUOTA_EXCEEDED_CODE } from "@deepseek-ai/dsh-llm";
|
|
15
23
|
import { TurnStopError } from "./spi.js";
|
|
16
|
-
import { MODEL_STREAM_IDLE } from "./model.js";
|
|
24
|
+
import { MODEL_STREAM_IDLE, REQUEST_TOO_LARGE } from "./model.js";
|
|
25
|
+
/** The stop code of a failed model request, by the class model.ts gave it. */
|
|
26
|
+
const MODEL_FAILURE_STOPS = {
|
|
27
|
+
SERVER: "model_overloaded_error",
|
|
28
|
+
RATE_LIMIT: "model_rate_limited_error",
|
|
29
|
+
[QUOTA_EXCEEDED_CODE]: "billing_error",
|
|
30
|
+
AUTH: "model_request_failed_error",
|
|
31
|
+
PROVIDER_HTTP: "model_request_failed_error",
|
|
32
|
+
[REQUEST_TOO_LARGE]: "model_request_failed_error",
|
|
33
|
+
};
|
|
17
34
|
export function stopForTurnEnd(reason, context = {}) {
|
|
18
35
|
switch (reason.kind) {
|
|
19
36
|
case "completed":
|
|
@@ -26,6 +43,9 @@ export function stopForTurnEnd(reason, context = {}) {
|
|
|
26
43
|
return new TurnStopError("context_too_large", "the conversation no longer fits the model context window even after compaction");
|
|
27
44
|
if (code === MODEL_STREAM_IDLE)
|
|
28
45
|
return new TurnStopError("model_stream_idle", `the model stream sent no data${context.streamIdleMs ? ` for ${context.streamIdleMs / 1000}s` : ""}`);
|
|
46
|
+
const modelStop = code !== undefined && Object.hasOwn(MODEL_FAILURE_STOPS, code) ? MODEL_FAILURE_STOPS[code] : undefined;
|
|
47
|
+
if (modelStop)
|
|
48
|
+
return new TurnStopError(modelStop, reason.error.message);
|
|
29
49
|
return new Error(`dsh turn ended: error${reason.error ? `: ${reason.error.message}` : ""}`);
|
|
30
50
|
}
|
|
31
51
|
default:
|
|
@@ -41,6 +61,10 @@ export const AGENT_ERROR_CODES = [
|
|
|
41
61
|
"agent_unavailable",
|
|
42
62
|
"agent_auth_required",
|
|
43
63
|
"agent_protocol_error",
|
|
64
|
+
"model_overloaded_error",
|
|
65
|
+
"model_rate_limited_error",
|
|
66
|
+
"model_request_failed_error",
|
|
67
|
+
"billing_error",
|
|
44
68
|
];
|
|
45
69
|
/**
|
|
46
70
|
* The contract code a failed turn reports: a typed stop is its own code, any
|
|
@@ -43,6 +43,12 @@ export interface Reply {
|
|
|
43
43
|
status?: number;
|
|
44
44
|
/** HTTP status plus this error text (default "scripted failure"). */
|
|
45
45
|
errorText?: string;
|
|
46
|
+
/** With status: send this body as is instead of {error: {message}} (for example a retryable envelope). */
|
|
47
|
+
errorBody?: string;
|
|
48
|
+
/** With status: extra response headers (for example Retry-After). */
|
|
49
|
+
headers?: Record<string, string>;
|
|
50
|
+
/** Close the connection without any response (a stopping backend: the client sees a connection error). */
|
|
51
|
+
drop?: boolean;
|
|
46
52
|
/** finish_reason of a non-tool reply (default "stop"); "length" simulates a max_tokens cut. */
|
|
47
53
|
finishReason?: string;
|
|
48
54
|
/** Usage of the reply (default 11 prompt / 7 completion, no cache); cachedTokens is prompt_tokens_details.cached_tokens, part of promptTokens. */
|
|
@@ -29,8 +29,12 @@ export async function startScriptedModel(script, listen = "127.0.0.1") {
|
|
|
29
29
|
const request = { token: (req.headers.authorization ?? "").replace(/^Bearer /, ""), body: JSON.parse(Buffer.concat(chunks).toString("utf8")) };
|
|
30
30
|
requests.push(request);
|
|
31
31
|
const reply = model.script(request, requests.length - 1);
|
|
32
|
+
if (reply.drop) {
|
|
33
|
+
req.socket.destroy();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
32
36
|
if (reply.status) {
|
|
33
|
-
res.writeHead(reply.status).end(JSON.stringify({ error: { message: reply.errorText ?? "scripted failure" } }));
|
|
37
|
+
res.writeHead(reply.status, reply.headers).end(reply.errorBody ?? JSON.stringify({ error: { message: reply.errorText ?? "scripted failure" } }));
|
|
34
38
|
return;
|
|
35
39
|
}
|
|
36
40
|
res.writeHead(200, { "content-type": "text/event-stream" });
|
package/lib/testing/tmp.js
CHANGED
|
@@ -7,7 +7,9 @@ import { tmpdir } from "node:os";
|
|
|
7
7
|
import { basename, dirname, isAbsolute, join } from "node:path";
|
|
8
8
|
/** Name prefixes a test directory may carry; the clean script refuses any other. */
|
|
9
9
|
export const TEST_TMP_PREFIXES = ["bma-conf-", "bma-shell-", "bma-acp-"];
|
|
10
|
-
|
|
10
|
+
// One ledger per OS user: CI hosts run several runner users over one /tmp, and a ledger another user
|
|
11
|
+
// created is not appendable (EACCES), which would fail every test that makes a temp dir.
|
|
12
|
+
export const ledgerPath = () => join(tmpdir(), `bma-test-tmp-${process.getuid?.() ?? "shared"}.ledger`);
|
|
11
13
|
/** Creates a temp dir under tmpdir() and records its exact path in the ledger. */
|
|
12
14
|
export function createTestTempDir(prefix) {
|
|
13
15
|
const dir = mkdtempSync(join(tmpdir(), prefix));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@beeos-ai/bma-dsh-adapter",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "BMA's shared DSH adapter: kernel assembly, platform-tools-only catalog gate, event translation, approval ids, JSONL resume, typed stop reasons, plus a conformance suite for hosts.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|