@capybara-hq/kernel 0.1.0-rc.30 → 0.1.0-rc.31
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/bootstrap.json +3 -3
- package/dist/catalog/marketplace-fetch.d.ts +5 -2
- package/dist/catalog/marketplace-fetch.js +4 -1
- package/dist/catalog/requests.service.d.ts +6 -5
- package/dist/catalog/requests.service.js +7 -6
- package/dist/config/load.d.ts +6 -0
- package/dist/config/load.js +26 -0
- package/dist/config/schema.js +5 -5
- package/dist/conversations/events.d.ts +10 -0
- package/dist/conversations/events.js +23 -1
- package/dist/devices/devices.controller.js +3 -2
- package/dist/devices/devices.service.d.ts +17 -5
- package/dist/devices/devices.service.js +24 -13
- package/dist/devices/dto.d.ts +7 -6
- package/dist/devices/dto.js +6 -5
- package/dist/errors.d.ts +6 -1
- package/dist/errors.js +6 -1
- package/dist/evals/reconstruct.js +18 -8
- package/dist/extensions/bootstrap.d.ts +1 -1
- package/dist/extensions/bootstrap.js +7 -1
- package/dist/extensions/installs.js +4 -1
- package/dist/lifecycle/kernel-lifecycle.js +1 -3
- package/dist/net/public-address.d.ts +14 -4
- package/dist/net/public-address.js +22 -12
- package/dist/net/safe-fetch.d.ts +2 -1
- package/dist/net/safe-fetch.js +16 -4
- package/dist/packages/npm.d.ts +28 -9
- package/dist/packages/npm.js +30 -19
- package/dist/packages/provenance.d.ts +3 -1
- package/dist/packages/provenance.js +7 -3
- package/dist/platform/ops/devices.ops.js +1 -1
- package/dist/platform/ops/platform.ops.d.ts +3 -1
- package/dist/platform/ops/platform.ops.js +11 -13
- package/dist/runtime/context/build.d.ts +10 -1
- package/dist/runtime/context/build.js +44 -10
- package/dist/runtime/context/compaction.d.ts +27 -8
- package/dist/runtime/context/compaction.js +133 -58
- package/dist/runtime/context/fit.d.ts +54 -22
- package/dist/runtime/context/fit.js +116 -40
- package/dist/runtime/model-call.d.ts +0 -8
- package/dist/runtime/model-call.js +185 -83
- package/dist/sandbox/review/classifier.d.ts +2 -0
- package/dist/sandbox/review/classifier.js +12 -1
- package/dist/sandbox/review/index.d.ts +3 -3
- package/dist/sandbox/review/index.js +3 -3
- package/dist/sandbox/review/review.service.d.ts +12 -4
- package/dist/sandbox/review/review.service.js +88 -19
- package/dist/sandbox/review/rules.d.ts +6 -0
- package/dist/sandbox/review/rules.js +1013 -95
- package/dist/sandbox/review/types.d.ts +21 -4
- package/dist/sandbox/review/types.js +41 -5
- package/dist/sandbox/sandbox.service.d.ts +8 -7
- package/dist/sandbox/sandbox.service.js +16 -16
- package/dist/setup/kernel-checks.js +3 -2
- package/dist/setup/oauth-token-probe.d.ts +1 -8
- package/dist/setup/oauth-token-probe.js +0 -16
- package/dist/tools/builtins/sandbox/common.d.ts +1 -1
- package/dist/tools/builtins/sandbox/common.js +1 -1
- package/dist/tools/builtins/tool-search.js +4 -3
- package/dist/tools/oauth/discovery.d.ts +38 -16
- package/dist/tools/oauth/discovery.js +59 -33
- package/dist/tools/pipeline.js +17 -13
- package/package.json +1 -1
|
@@ -2,63 +2,79 @@ import { estimateTokens } from "./tokens.js";
|
|
|
2
2
|
/** The note an oversized block carries once truncated (model-visible, English; spec §4.3). */
|
|
3
3
|
export const FIT_TRUNCATION_NOTE = "[content truncated to fit the model's context window]";
|
|
4
4
|
/**
|
|
5
|
-
* The
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* The planning bounds of a window (context compaction spec §4.1, revision 2026-11-15): every plan
|
|
6
|
+
* (the trigger, the compaction tail, the fit guard, the reactive trim) uses `O_p = min(O, ⌊W/2⌋)`
|
|
7
|
+
* (at least 1) and `R_p = min(R, ⌊W/8⌋)` in place of the output cap and the reserve, so the input
|
|
8
|
+
* target `W - O_p - R_p` is at least `3W/8` on any model. Without them a model whose output cap is
|
|
9
|
+
* about its window, or whose window is smaller than the reserve, had a target of about one token.
|
|
10
|
+
* Common models are unchanged (W 200k, O 32k, R 8k: `O_p = O`, `R_p = R`).
|
|
8
11
|
*/
|
|
9
|
-
export function
|
|
10
|
-
|
|
12
|
+
export function planningBounds(contextWindow, maxOutputTokens, reserveTokens) {
|
|
13
|
+
const output = Math.max(1, Math.min(maxOutputTokens, Math.floor(contextWindow / 2)));
|
|
14
|
+
const reserve = Math.max(0, Math.min(reserveTokens, Math.floor(contextWindow / 8)));
|
|
15
|
+
return { output, reserve, target: contextWindow - output - reserve };
|
|
11
16
|
}
|
|
12
17
|
/**
|
|
13
|
-
* The
|
|
14
|
-
* the
|
|
15
|
-
*
|
|
18
|
+
* The output cap actually sent (`O'`, spec §4.1 / §4.3 point 4): `clamp(O, 1, W - input - R_p)`,
|
|
19
|
+
* from the input actually sent (after the fit plan). The configured cap whenever it fits beside the
|
|
20
|
+
* input and the reserve; otherwise what is left, the reserve kept. Every path (ordinary, squeezed,
|
|
21
|
+
* reactive) clamps with it.
|
|
16
22
|
*/
|
|
17
|
-
export function
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/** The input room left below the window: `W - O' - R_eff` with the clamped output and reserve. */
|
|
21
|
-
export function fitTarget(contextWindow, clampedOutput, reserveTokens) {
|
|
22
|
-
return contextWindow - clampedOutput - reserveTokens;
|
|
23
|
+
export function sentOutputCap(contextWindow, maxOutputTokens, inputSent, reserveTokens) {
|
|
24
|
+
const { reserve } = planningBounds(contextWindow, maxOutputTokens, reserveTokens);
|
|
25
|
+
return Math.max(1, Math.min(maxOutputTokens, contextWindow - inputSent - reserve));
|
|
23
26
|
}
|
|
24
27
|
const SUMMARY_OPEN = "<conversation_summary>";
|
|
25
28
|
const NOTE_TOKENS = estimateTokens([{ role: "user", content: [{ type: "text", text: FIT_TRUNCATION_NOTE }] }], []);
|
|
26
|
-
/**
|
|
27
|
-
const
|
|
29
|
+
/** The summary text block (never truncated: it is the retained history). */
|
|
30
|
+
const isSummaryText = (b) => b.type === "text" && b.text.startsWith(SUMMARY_OPEN);
|
|
31
|
+
/**
|
|
32
|
+
* The conversation summary message (never dropped; its summary text is never truncated, the
|
|
33
|
+
* re-rendered opening user message after it may be: spec §4.3 point 5).
|
|
34
|
+
*/
|
|
35
|
+
const isSummary = (m) => m.role === "user" && m.content.some(isSummaryText);
|
|
36
|
+
/**
|
|
37
|
+
* A typed user turn (a person's message): not a tool-result carrier and not the summary, which may
|
|
38
|
+
* cover the latest typed message (spec §4.2, revision 2026-11-15).
|
|
39
|
+
*/
|
|
40
|
+
const isTypedUser = (m) => m.role === "user" && m.content.some((b) => b.type !== "tool_result") && !isSummary(m);
|
|
28
41
|
/** A message that carries only tool results (the answer to a tool call). */
|
|
29
42
|
const isResults = (m) => m.role === "user" && m.content.length > 0 && m.content.every((b) => b.type === "tool_result");
|
|
30
43
|
const hasToolCall = (m) => m.role === "assistant" && m.content.some((b) => b.type === "tool_call");
|
|
31
|
-
/** The conversation summary message (never dropped or truncated: it is the retained history). */
|
|
32
|
-
const isSummary = (m) => m.role === "user" && m.content.some((b) => b.type === "text" && b.text.startsWith(SUMMARY_OPEN));
|
|
33
44
|
const messageTokens = (m) => estimateTokens([m], [], []);
|
|
34
45
|
const blockTokens = (b) => estimateTokens([{ role: "user", content: [b] }], [], []);
|
|
35
46
|
const truncatable = (b) => (b.type === "text" && b.text !== FIT_TRUNCATION_NOTE) ||
|
|
36
47
|
(b.type === "tool_result" && b.content !== FIT_TRUNCATION_NOTE);
|
|
37
48
|
/**
|
|
38
|
-
* The deterministic fit plan for a request over `W -
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* `run.context_too_large` before any provider call.
|
|
49
|
+
* The deterministic fit plan for a request over the planning target `W - O_p - R_p`
|
|
50
|
+
* ({@link planningBounds}), or null when it already fits. A plan that cannot reach the target even after dropping every droppable
|
|
51
|
+
* message and truncating every kept text / `tool_result` block is still returned: the caller decides
|
|
52
|
+
* (the ordinary guard fails before any provider call).
|
|
43
53
|
*
|
|
44
|
-
* Drops, in this order,
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* (
|
|
54
|
+
* Drops, in this order, until the estimate is at or below the target: before the latest user turn,
|
|
55
|
+
* complete tool pairs first (a tool call with the results message that follows it), then assistant
|
|
56
|
+
* text, then complete older user turns; then the complete tool pairs inside the latest user turn,
|
|
57
|
+
* oldest first, stopping before the latest complete pair (spec §4.3, revision 2026-11-15). The
|
|
58
|
+
* latest typed message stays (when the summary covers it, the turn starts after the summary). Never
|
|
59
|
+
* a pair's half, never the summary or a system message. If that is not enough, it truncates the kept
|
|
60
|
+
* messages' largest text / `tool_result` blocks (replacing the content with
|
|
61
|
+
* {@link FIT_TRUNCATION_NOTE}); in the summary message only the blocks after the summary text (the
|
|
62
|
+
* re-rendered opening user message) are candidates.
|
|
49
63
|
*/
|
|
50
64
|
export function planFit(input) {
|
|
51
|
-
const {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
65
|
+
const { model, maxOutputTokens, reserveTokens } = input;
|
|
66
|
+
return planFitTo(input, planningBounds(model.contextWindow, maxOutputTokens, reserveTokens).target);
|
|
67
|
+
}
|
|
68
|
+
/** {@link planFit} against an explicit input target (the squeezed fit, the reactive trim). */
|
|
69
|
+
function planFitTo(input, target) {
|
|
70
|
+
const { request, estimate } = input;
|
|
55
71
|
if (target <= 0 || estimate <= target)
|
|
56
72
|
return null;
|
|
57
73
|
const messages = request.messages;
|
|
58
|
-
// the latest user turn
|
|
59
|
-
// messages may go
|
|
74
|
+
// the latest user turn starts at the latest typed message, or (when the summary covers it) at the
|
|
75
|
+
// start: older messages may go whole, the turn itself only as complete tool pairs
|
|
60
76
|
const lastTyped = messages.findLastIndex(isTypedUser);
|
|
61
|
-
const keepFrom = lastTyped < 0 ?
|
|
77
|
+
const keepFrom = lastTyped < 0 ? 0 : lastTyped;
|
|
62
78
|
const droppable = (i) => {
|
|
63
79
|
const m = messages[i];
|
|
64
80
|
return i < keepFrom && m !== undefined && m.role !== "system" && !isSummary(m);
|
|
@@ -109,17 +125,40 @@ export function planFit(input) {
|
|
|
109
125
|
continue;
|
|
110
126
|
drop([i]);
|
|
111
127
|
}
|
|
128
|
+
// complete tool pairs inside the latest user turn, oldest first, keeping the latest complete pair
|
|
129
|
+
// (the one the next step answers)
|
|
130
|
+
const isPairAt = (i) => {
|
|
131
|
+
const m = messages[i];
|
|
132
|
+
const next = messages[i + 1];
|
|
133
|
+
return m !== undefined && hasToolCall(m) && next !== undefined && isResults(next);
|
|
134
|
+
};
|
|
135
|
+
let latestPair = -1;
|
|
136
|
+
for (let i = messages.length - 2; i >= keepFrom; i--) {
|
|
137
|
+
if (isPairAt(i)) {
|
|
138
|
+
latestPair = i;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
for (let i = keepFrom; i < latestPair && current > target; i++) {
|
|
143
|
+
if (!isPairAt(i))
|
|
144
|
+
continue;
|
|
145
|
+
drop([i, i + 1]);
|
|
146
|
+
i++;
|
|
147
|
+
}
|
|
112
148
|
if (current <= target)
|
|
113
149
|
return plan;
|
|
114
|
-
//
|
|
115
|
-
//
|
|
150
|
+
// still too large: replace the kept messages' largest text / tool_result blocks with the note,
|
|
151
|
+
// largest first, ties by position. In the summary message only the summary text is protected:
|
|
152
|
+
// the turn's opening message re-rendered after it (spec §4.2) is a kept block like any other
|
|
116
153
|
const candidates = [];
|
|
117
|
-
for (let i =
|
|
154
|
+
for (let i = 0; i < messages.length; i++) {
|
|
118
155
|
const m = messages[i];
|
|
119
|
-
if (m === undefined || m.role === "system"
|
|
156
|
+
if (m === undefined || dropped.has(i) || m.role === "system")
|
|
157
|
+
continue;
|
|
158
|
+
if (i < keepFrom && !isSummary(m))
|
|
120
159
|
continue;
|
|
121
160
|
m.content.forEach((b, block) => {
|
|
122
|
-
if (!truncatable(b))
|
|
161
|
+
if (!truncatable(b) || isSummaryText(b))
|
|
123
162
|
return;
|
|
124
163
|
const tokens = blockTokens(b);
|
|
125
164
|
if (tokens > NOTE_TOKENS)
|
|
@@ -139,6 +178,43 @@ export function planFit(input) {
|
|
|
139
178
|
}
|
|
140
179
|
return plan;
|
|
141
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* The fallback when {@link planFit} cannot reach its target, even after one forced compaction
|
|
183
|
+
* (spec §4.3 point 5): plan against `W - 1 - R_p`, giving the output whatever the input leaves
|
|
184
|
+
* while the reserve stays. The caller sends the cap {@link sentOutputCap} gives from the post-fit
|
|
185
|
+
* estimate. A plan that cannot reach this target either means the system prompt, tools, summary and
|
|
186
|
+
* latest pair are too large (`run.context_too_large`).
|
|
187
|
+
*/
|
|
188
|
+
export function planSqueezedFit(input) {
|
|
189
|
+
const { model, maxOutputTokens, reserveTokens } = input;
|
|
190
|
+
const { reserve } = planningBounds(model.contextWindow, maxOutputTokens, reserveTokens);
|
|
191
|
+
const target = model.contextWindow - 1 - reserve;
|
|
192
|
+
return { plan: planFitTo(input, target), target };
|
|
193
|
+
}
|
|
194
|
+
/** The reactive trim's fractions of the fit target, most aggressive first (spec §4.3 point 6). */
|
|
195
|
+
export const REACTIVE_TRIM_FACTORS = [0.7, 0.8, 0.9];
|
|
196
|
+
/**
|
|
197
|
+
* The deterministic plan for a reactive trim (spec §4.3 point 6 / §4.4): the provider rejected a
|
|
198
|
+
* request the estimate thought fit, so the retry is trimmed against 70%, 80% or 90% of the fit
|
|
199
|
+
* target `W - O_p - R_p`, the most aggressive one the plan actually reaches (its estimate after the
|
|
200
|
+
* plan at or below it). Null when none is reached (a plan that cannot reach even 90% would replace
|
|
201
|
+
* every kept block with notes) or when the request already fits 70% (nothing to trim, the same
|
|
202
|
+
* request would be rejected again): no trim retry, the step surfaces `model.context_overflow`. The
|
|
203
|
+
* plan is ordinary and replays byte-for-byte like any other.
|
|
204
|
+
*/
|
|
205
|
+
export function planReactiveTrim(input) {
|
|
206
|
+
const { request, model, maxOutputTokens, reserveTokens, estimate } = input;
|
|
207
|
+
const { target } = planningBounds(model.contextWindow, maxOutputTokens, reserveTokens);
|
|
208
|
+
for (const factor of REACTIVE_TRIM_FACTORS) {
|
|
209
|
+
const t = Math.floor(factor * target);
|
|
210
|
+
const plan = planFitTo(input, t);
|
|
211
|
+
if (plan === null)
|
|
212
|
+
return null;
|
|
213
|
+
if (estimateAfterFit(request, estimate, plan) <= t)
|
|
214
|
+
return plan;
|
|
215
|
+
}
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
142
218
|
/**
|
|
143
219
|
* Applies a plan to an assembled request (pure): drops the named messages and replaces the named
|
|
144
220
|
* blocks' content with their note. Message and block indexes refer to the request as given, so the
|
|
@@ -188,14 +188,6 @@ export declare const MAX_MODEL_ATTEMPTS = 3;
|
|
|
188
188
|
* so a persistent `model.context_overflow` cannot loop.
|
|
189
189
|
*/
|
|
190
190
|
export declare const MAX_STEP_MODEL_CALLS: number;
|
|
191
|
-
/**
|
|
192
|
-
* The progressively smaller fit targets of a reactive trim, as fractions of the model's window
|
|
193
|
-
* (context compaction spec §4.3 point 5 / §4.4 point 3). The trim fix-up tries them most
|
|
194
|
-
* aggressive first (70%, then 80%, then 90% of the window) and uses the first target a fit plan
|
|
195
|
-
* actually reaches, so the retry really shrinks a request the provider rejected. `null` when the
|
|
196
|
-
* request already fits every target (nothing to trim).
|
|
197
|
-
*/
|
|
198
|
-
export declare const REACTIVE_TRIM_FACTORS: readonly [0.9, 0.8, 0.7];
|
|
199
191
|
/** 本步解析出的工具与是否启用 ToolSearch(见 `ToolRegistry.forAgent`),以及本步已取的附件字节。 */
|
|
200
192
|
export interface StepTools {
|
|
201
193
|
list: RequestTool[];
|