maka-agent 0.2.0-dev.35.20260915 → 0.2.0-dev.37.20260915
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 +6 -0
- package/README.zh-CN.md +5 -0
- package/native/runtime-host-windows-task-launcher/prebuilds/win32-x64/maka-runtime-host-task-launcher.exe +0 -0
- package/node_modules/@maka/core/dist/model-metadata.generated.js +175 -116
- package/node_modules/@maka/core/dist/models-dev-projection.js +1 -0
- package/node_modules/@maka/core/dist/provider-registry.js +17 -0
- package/node_modules/@maka/eval/dist/harbor-maka-subject.js +13 -3
- package/node_modules/@maka/eval/dist/maka-artifacts.js +28 -14
- package/node_modules/@maka/eval/dist/maka-subject.js +28 -3
- package/node_modules/@maka/eval/harbor/relay_agent.py +42 -2
- package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +0 -1
- package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +40 -53
- package/node_modules/@maka/runtime/dist/ai-sdk-turn.js +26 -29
- package/node_modules/@maka/runtime/dist/builtin-tools.js +20 -12
- package/node_modules/@maka/runtime/dist/runtime-kernel.js +1 -0
- package/node_modules/@maka/runtime/dist/telemetry/model-pricing.generated.js +144 -77
- package/node_modules/@maka/runtime/node_modules/@ai-sdk/code-mode/dist/run-code-mode.js +6 -19
- package/node_modules/@maka/runtime-host/dist/client/connect-or-spawn.js +24 -3
- package/node_modules/@maka/runtime-host/dist/client/connection.js +20 -2
- package/node_modules/@maka/runtime-host/dist/client/hosted-execution-target.js +27 -6
- package/node_modules/@maka/runtime-host/dist/client/hosted-execution.js +10 -28
- package/node_modules/@maka/runtime-host/dist/client/startup-error.js +1 -1
- package/node_modules/@maka/runtime-host/dist/execution-candidate-main.js +8 -1
- package/node_modules/@maka/runtime-host/dist/protocol/agent-graph.js +1 -11
- package/node_modules/@maka/runtime-host/dist/protocol/client-capability.js +2 -2
- package/node_modules/@maka/runtime-host/dist/protocol/codec.js +11 -0
- package/node_modules/@maka/runtime-host/dist/protocol/index.js +1 -1
- package/node_modules/@maka/runtime-host/dist/protocol/workhub-coordination.js +10 -2
- package/node_modules/@maka/runtime-host/dist/server/bootstrap-runtime-policy.js +65 -0
- package/node_modules/@maka/runtime-host/dist/server/execution-candidate.js +4 -1
- package/node_modules/@maka/runtime-host/dist/server/execution-composition-factory.js +9 -3
- package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +11 -0
- package/node_modules/@maka/runtime-host/dist/server/hosted-execution-tool-profile.js +3 -15
- package/node_modules/@maka/runtime-host/dist/server/interactive-run-composer.js +9 -4
- package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +1 -2
- package/node_modules/@maka/runtime-host/dist/server/session-catalog-coordinator.js +4 -1
- package/node_modules/@maka/storage/dist/agent-run-store-contract.js +11 -8
- package/node_modules/@maka/storage/dist/agent-run-store.js +1 -1
- package/node_modules/@maka/storage/dist/execution-stores.js +1 -1
- package/node_modules/@maka/storage/dist/message-admission-store.js +5 -3
- package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +14 -6
- package/package.json +1 -1
- package/node_modules/@maka/eval/dist/maka-runtime-policy.js +0 -42
|
@@ -400,6 +400,10 @@ const moonshotModelIds = toolCallingModelIds('Moonshot', GENERATED_MODELS_DEV_ME
|
|
|
400
400
|
'kimi-k2.6',
|
|
401
401
|
'kimi-k2.7-code',
|
|
402
402
|
]).filter((id) => GENERATED_MODELS_DEV_METADATA.moonshot[id]?.lifecycle !== 'deprecated');
|
|
403
|
+
const moonshotGlobal = GENERATED_MODELS_DEV_PROVIDER_FACTS['moonshot-global'];
|
|
404
|
+
if (!moonshotGlobal.api)
|
|
405
|
+
throw new Error('models.dev Moonshot Global provider is missing its API');
|
|
406
|
+
const moonshotGlobalModelIds = toolCallingModelIds('Moonshot Global', GENERATED_MODELS_DEV_METADATA['moonshot-global'], ['kimi-k3']).filter((id) => GENERATED_MODELS_DEV_METADATA['moonshot-global'][id]?.lifecycle !== 'deprecated');
|
|
403
407
|
const cloudflareWorkersAi = GENERATED_MODELS_DEV_PROVIDER_FACTS['cloudflare-workers-ai'];
|
|
404
408
|
if (cloudflareWorkersAi.id !== 'cloudflare-workers-ai') {
|
|
405
409
|
throw new Error('models.dev Cloudflare Workers AI provider facts are missing stable id cloudflare-workers-ai');
|
|
@@ -656,6 +660,19 @@ const providerRegistry = {
|
|
|
656
660
|
signupUrl: 'https://platform.kimi.com/console/api-keys',
|
|
657
661
|
catalogOrder: 4,
|
|
658
662
|
},
|
|
663
|
+
'moonshot-global': {
|
|
664
|
+
label: 'Moonshot Global',
|
|
665
|
+
baseUrl: moonshotGlobal.api,
|
|
666
|
+
authKind: 'api_key',
|
|
667
|
+
fallbackModels: moonshotGlobalModelIds,
|
|
668
|
+
status: 'ready',
|
|
669
|
+
runtimeAdapter: { kind: 'openai', apiProtocol: 'openai-responses' },
|
|
670
|
+
modelDiscovery: { kind: 'protocol' },
|
|
671
|
+
category: 'overseas',
|
|
672
|
+
catalogGroup: 'api',
|
|
673
|
+
signupUrl: 'https://platform.kimi.ai/console/api-keys',
|
|
674
|
+
catalogOrder: 4.1,
|
|
675
|
+
},
|
|
659
676
|
'zai-coding-plan': {
|
|
660
677
|
label: 'Z.AI Coding Plan',
|
|
661
678
|
menuLabel: 'Z.AI',
|
|
@@ -16,11 +16,10 @@
|
|
|
16
16
|
* specific language governing permissions and limitations
|
|
17
17
|
* under the License.
|
|
18
18
|
*/
|
|
19
|
-
import { mkdir
|
|
19
|
+
import { mkdir } from 'node:fs/promises';
|
|
20
20
|
import { dirname, join } from 'node:path';
|
|
21
21
|
import { runHostedExecution } from '@maka/runtime-host/client';
|
|
22
22
|
import { captureMakaRuntimeArtifacts, writeMakaArtifactCollectionError } from './maka-artifacts.js';
|
|
23
|
-
import { makaEvalRuntimePolicyDocument } from './maka-runtime-policy.js';
|
|
24
23
|
import { takeRelayResultToken, writeRelayResult } from './relay-result-frame.js';
|
|
25
24
|
const resultToken = takeRelayResultToken();
|
|
26
25
|
const payload = JSON.parse(Buffer.from(process.argv[2] ?? '', 'base64url').toString());
|
|
@@ -50,12 +49,23 @@ process.once('SIGTERM', stop);
|
|
|
50
49
|
const runtimeHome = join(dirname(payload.rootPath), `${process.pid}-home`);
|
|
51
50
|
await mkdir(payload.rootPath, { recursive: true });
|
|
52
51
|
await mkdir(runtimeHome, { recursive: true, mode: 0o700 });
|
|
53
|
-
await writeFile(join(payload.rootPath, 'runtime-policy.json'), `${JSON.stringify(makaEvalRuntimePolicyDocument(process.env.HTTPS_PROXY))}\n`, { flag: 'wx', mode: 0o600 });
|
|
54
52
|
process.env.HOME = runtimeHome;
|
|
55
53
|
process.env.DEEPSEEK_BASE_URL = payload.baseUrl;
|
|
56
54
|
let result;
|
|
57
55
|
try {
|
|
58
56
|
result = await runHostedExecution({
|
|
57
|
+
initialization: {
|
|
58
|
+
incognito: true,
|
|
59
|
+
...(process.env.HTTPS_PROXY ? { proxyUrl: process.env.HTTPS_PROXY } : {}),
|
|
60
|
+
},
|
|
61
|
+
...(payload.connection
|
|
62
|
+
? {
|
|
63
|
+
connection: {
|
|
64
|
+
providerType: payload.connection.providerType,
|
|
65
|
+
apiKey: process.env[payload.connection.apiKeyEnvironment] ?? '',
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
: {}),
|
|
59
69
|
rootPath: payload.rootPath,
|
|
60
70
|
baseUrl: payload.baseUrl,
|
|
61
71
|
execution: payload.execution,
|
|
@@ -33,24 +33,33 @@ export async function captureMakaRuntimeArtifacts(input) {
|
|
|
33
33
|
await mkdir(stagingRoot, { recursive: true, mode: 0o700 });
|
|
34
34
|
const sourcePath = join(stateRoot, 'runtime.sqlite');
|
|
35
35
|
const destinationPath = join(stagingRoot, 'runtime.sqlite');
|
|
36
|
-
const source = new DatabaseSync(sourcePath, { readOnly: true });
|
|
37
36
|
try {
|
|
38
|
-
await
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
snapshot
|
|
37
|
+
await stat(sourcePath);
|
|
38
|
+
const source = new DatabaseSync(sourcePath, { readOnly: true });
|
|
39
|
+
try {
|
|
40
|
+
await backup(source, destinationPath);
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
source.close();
|
|
44
|
+
}
|
|
45
|
+
const snapshot = new DatabaseSync(destinationPath);
|
|
46
|
+
try {
|
|
47
|
+
snapshot.exec('PRAGMA wal_checkpoint(TRUNCATE)');
|
|
48
|
+
snapshot.exec('PRAGMA journal_mode=DELETE');
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
snapshot.close();
|
|
52
|
+
}
|
|
53
|
+
await chmod(destinationPath, 0o600);
|
|
47
54
|
}
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
catch (error) {
|
|
56
|
+
await rm(destinationPath, { force: true });
|
|
57
|
+
if (error.code !== 'ENOENT') {
|
|
58
|
+
await writeMakaArtifactCollectionError(stagingRoot, error);
|
|
59
|
+
}
|
|
50
60
|
}
|
|
51
61
|
await rm(`${destinationPath}-wal`, { force: true });
|
|
52
62
|
await rm(`${destinationPath}-shm`, { force: true });
|
|
53
|
-
await chmod(destinationPath, 0o600);
|
|
54
63
|
for (const name of ['runtime-host-candidate.log', 'runtime-policy.json']) {
|
|
55
64
|
const source = join(stateRoot, name);
|
|
56
65
|
try {
|
|
@@ -66,7 +75,12 @@ export async function captureMakaRuntimeArtifacts(input) {
|
|
|
66
75
|
throw error;
|
|
67
76
|
}
|
|
68
77
|
}
|
|
69
|
-
const files = await Promise.all([
|
|
78
|
+
const files = await Promise.all([
|
|
79
|
+
'runtime.sqlite',
|
|
80
|
+
'runtime-host-candidate.log',
|
|
81
|
+
'runtime-policy.json',
|
|
82
|
+
'collection-error.json',
|
|
83
|
+
].map((name) => describeFile(join(stagingRoot, name), name)));
|
|
70
84
|
const manifest = {
|
|
71
85
|
schemaVersion: 'maka.eval.runtime_artifacts.v1',
|
|
72
86
|
reason: input.reason,
|
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
* under the License.
|
|
18
18
|
*/
|
|
19
19
|
import { randomUUID } from 'node:crypto';
|
|
20
|
+
import { PROVIDER_REGISTRY } from '@maka/core/llm-connections';
|
|
21
|
+
import { isThinkingLevel } from '@maka/core/model-thinking';
|
|
20
22
|
import { isSessionToolProfile } from '@maka/core/session';
|
|
21
23
|
import { decodeHostedExecutionProjection } from '@maka/runtime-host/protocol';
|
|
22
24
|
import { MAKA_RUNTIME_ARTIFACT_PATH, MAKA_SUBJECT_STDERR_PATH, MAKA_SUBJECT_STDOUT_PATH, } from './maka-artifacts.js';
|
|
@@ -24,7 +26,14 @@ import { deepSeekCostUsd } from './provider-metering.js';
|
|
|
24
26
|
export function createMakaSubjectAdapter() {
|
|
25
27
|
return {
|
|
26
28
|
kind: 'maka',
|
|
27
|
-
validate: (cell) =>
|
|
29
|
+
validate: (cell) => {
|
|
30
|
+
const config = decodeConfig(cell.subject.config);
|
|
31
|
+
if (config.apiKeyEnvironment &&
|
|
32
|
+
!cell.subject.credentials.includes(config.apiKeyEnvironment)) {
|
|
33
|
+
throw new Error('Maka apiKeyEnvironment must name a declared subject credential');
|
|
34
|
+
}
|
|
35
|
+
return config;
|
|
36
|
+
},
|
|
28
37
|
async execute({ cell, context }) {
|
|
29
38
|
const config = decodeConfig(cell.subject.config);
|
|
30
39
|
const executionId = randomUUID();
|
|
@@ -37,7 +46,7 @@ export function createMakaSubjectAdapter() {
|
|
|
37
46
|
connectionSlug: config.connectionSlug,
|
|
38
47
|
model: config.model,
|
|
39
48
|
},
|
|
40
|
-
thinkingLevel: config.thinkingLevel,
|
|
49
|
+
...(config.thinkingLevel === undefined ? {} : { thinkingLevel: config.thinkingLevel }),
|
|
41
50
|
permissionMode: config.permissionMode,
|
|
42
51
|
collaborationMode: config.collaborationMode,
|
|
43
52
|
orchestrationMode: config.orchestrationMode,
|
|
@@ -50,6 +59,14 @@ export function createMakaSubjectAdapter() {
|
|
|
50
59
|
rootPath: `${config.runtimeHostsPath}/${executionId}`,
|
|
51
60
|
artifactRoot: MAKA_RUNTIME_ARTIFACT_PATH,
|
|
52
61
|
baseUrl: config.baseUrl,
|
|
62
|
+
...(config.providerType
|
|
63
|
+
? {
|
|
64
|
+
connection: {
|
|
65
|
+
providerType: config.providerType,
|
|
66
|
+
apiKeyEnvironment: config.apiKeyEnvironment,
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
: {}),
|
|
53
70
|
hostSettlementTimeoutMs: config.hostSettlementTimeoutMs,
|
|
54
71
|
execution: input,
|
|
55
72
|
})).toString('base64url');
|
|
@@ -219,7 +236,8 @@ function decodeConfig(value) {
|
|
|
219
236
|
'baseUrl',
|
|
220
237
|
'connectionSlug',
|
|
221
238
|
'model',
|
|
222
|
-
'
|
|
239
|
+
...(Object.hasOwn(value, 'providerType') ? ['providerType', 'apiKeyEnvironment'] : []),
|
|
240
|
+
...(Object.hasOwn(value, 'thinkingLevel') ? ['thinkingLevel'] : []),
|
|
223
241
|
'permissionMode',
|
|
224
242
|
'collaborationMode',
|
|
225
243
|
'orchestrationMode',
|
|
@@ -227,6 +245,13 @@ function decodeConfig(value) {
|
|
|
227
245
|
'toolProfile',
|
|
228
246
|
];
|
|
229
247
|
const config = exact(value, fields);
|
|
248
|
+
if (config.providerType !== undefined &&
|
|
249
|
+
!Object.hasOwn(PROVIDER_REGISTRY, String(config.providerType))) {
|
|
250
|
+
throw new Error('Maka config.providerType is invalid');
|
|
251
|
+
}
|
|
252
|
+
if (config.thinkingLevel !== undefined && !isThinkingLevel(config.thinkingLevel)) {
|
|
253
|
+
throw new Error('Maka config.thinkingLevel is invalid');
|
|
254
|
+
}
|
|
230
255
|
if (!URL.canParse(String(config.baseUrl)))
|
|
231
256
|
throw new Error('Maka baseUrl is invalid');
|
|
232
257
|
const hostSettlementTimeoutMs = positiveInteger(config.hostSettlementTimeoutMs, 'Maka config.hostSettlementTimeoutMs');
|
|
@@ -100,6 +100,7 @@ class RelayAgent(BaseAgent):
|
|
|
100
100
|
if not isinstance(teardown_timeout_ms, int) or teardown_timeout_ms <= 0:
|
|
101
101
|
raise RuntimeError("Maka Eval teardown timeout is invalid")
|
|
102
102
|
self._teardown_timeout = teardown_timeout_ms / 1000
|
|
103
|
+
self._subject_owner: str | None = None
|
|
103
104
|
|
|
104
105
|
@staticmethod
|
|
105
106
|
def name() -> str:
|
|
@@ -109,7 +110,35 @@ class RelayAgent(BaseAgent):
|
|
|
109
110
|
return "1"
|
|
110
111
|
|
|
111
112
|
async def setup(self, environment: Any) -> None:
|
|
112
|
-
|
|
113
|
+
identity = await environment.exec('printf "%s:%s" "$(id -u)" "$(id -g)"')
|
|
114
|
+
owner = str(identity.stdout or "").strip()
|
|
115
|
+
if identity.return_code != 0 or re.fullmatch(r"[0-9]+:[0-9]+", owner) is None:
|
|
116
|
+
raise RuntimeError("Maka Eval could not resolve the task user")
|
|
117
|
+
prepared = await environment.exec(
|
|
118
|
+
"mkdir -p /logs/agent /logs/artifacts && "
|
|
119
|
+
f"chown {owner} /logs/agent /logs/artifacts && "
|
|
120
|
+
"chmod 700 /logs/agent /logs/artifacts",
|
|
121
|
+
user="root",
|
|
122
|
+
)
|
|
123
|
+
if prepared.return_code != 0:
|
|
124
|
+
raise RuntimeError("Maka Eval could not prepare task-owned artifact directories")
|
|
125
|
+
# Storage coordinates by the system account home, not the subject's
|
|
126
|
+
# temporary HOME. Provision an absent home without changing task identity
|
|
127
|
+
# or taking ownership of an existing directory.
|
|
128
|
+
uid = owner.split(":", 1)[0]
|
|
129
|
+
home_script = (
|
|
130
|
+
f"set -eu; account=$(getent passwd {uid}); "
|
|
131
|
+
"task_home=$(printf '%s' \"$account\" | cut -d: -f6) && "
|
|
132
|
+
'case "$task_home" in /*) ;; *) exit 1 ;; esac; '
|
|
133
|
+
'if [ ! -e "$task_home" ] && [ ! -L "$task_home" ]; then '
|
|
134
|
+
f'mkdir -p -- "$task_home" && chown {owner} "$task_home" && '
|
|
135
|
+
'chmod 700 "$task_home" || exit 1; fi; '
|
|
136
|
+
'test -d "$task_home"'
|
|
137
|
+
)
|
|
138
|
+
prepared_home = await environment.exec(home_script, user="root")
|
|
139
|
+
if prepared_home.return_code != 0:
|
|
140
|
+
raise RuntimeError("Maka Eval could not prepare the task account home")
|
|
141
|
+
self._subject_owner = owner
|
|
113
142
|
|
|
114
143
|
async def run(self, instruction: str, environment: Any, context: Any) -> None:
|
|
115
144
|
reader, writer = await asyncio.open_connection(self._host, self._port)
|
|
@@ -145,7 +174,9 @@ class RelayAgent(BaseAgent):
|
|
|
145
174
|
raise RelayTransportClosed("Maka Eval relay transport closed before ready")
|
|
146
175
|
request = await _receive(reader)
|
|
147
176
|
_require_message(request, self._token, "execute")
|
|
148
|
-
command = await _prepare_command(
|
|
177
|
+
command = await _prepare_command(
|
|
178
|
+
environment, request, self._token, scope_path, self._subject_owner
|
|
179
|
+
)
|
|
149
180
|
execution = asyncio.create_task(environment.exec(command, cwd=cwd))
|
|
150
181
|
decision = asyncio.create_task(_receive(reader))
|
|
151
182
|
done, _ = await asyncio.wait({execution, decision}, return_when=asyncio.FIRST_COMPLETED)
|
|
@@ -278,6 +309,7 @@ async def _prepare_command(
|
|
|
278
309
|
request: dict[str, Any],
|
|
279
310
|
token: str,
|
|
280
311
|
scope_path: str,
|
|
312
|
+
subject_owner: str | None = None,
|
|
281
313
|
) -> str:
|
|
282
314
|
credentials = request.get("credentials")
|
|
283
315
|
public_environment = request.get("environment", {})
|
|
@@ -317,6 +349,14 @@ async def _prepare_command(
|
|
|
317
349
|
}.items():
|
|
318
350
|
secret.write(f"export {key}={shlex.quote(value)}\n")
|
|
319
351
|
await environment.upload_file(secret_path, container_path)
|
|
352
|
+
if subject_owner is not None:
|
|
353
|
+
prepared = await environment.exec(
|
|
354
|
+
f"chown {subject_owner} {shlex.quote(container_path)} && "
|
|
355
|
+
f"chmod 600 {shlex.quote(container_path)}",
|
|
356
|
+
user="root",
|
|
357
|
+
)
|
|
358
|
+
if prepared.return_code != 0:
|
|
359
|
+
raise RuntimeError("Maka Eval could not prepare task-owned credentials")
|
|
320
360
|
finally:
|
|
321
361
|
if secret_path is not None:
|
|
322
362
|
secret_path.unlink(missing_ok=True)
|
|
@@ -162,7 +162,6 @@ export class AiSdkBackend {
|
|
|
162
162
|
targetConnectionId: input.header.llmConnectionId,
|
|
163
163
|
targetProviderStateIdentity: input.providerStateIdentity,
|
|
164
164
|
now: this.now,
|
|
165
|
-
modelAdapter: this.modelAdapter,
|
|
166
165
|
createProviderRequestTracker: (trackerInput) => this.providerTelemetry.createTracker(trackerInput),
|
|
167
166
|
materializeRuntimeReplayPlan: (plan, imageBudget, checkpoint, providerReasoningReplayEventIds) => this.messageProjection.materializeRuntimeReplayPlan(plan, imageBudget, checkpoint, providerReasoningReplayEventIds),
|
|
168
167
|
canReplayProviderNative: (plan) => this.messageProjection.canReplayProviderNative(plan),
|
|
@@ -35,7 +35,6 @@ export class AiSdkCompaction {
|
|
|
35
35
|
targetConnectionId;
|
|
36
36
|
targetProviderStateIdentity;
|
|
37
37
|
now;
|
|
38
|
-
modelAdapter;
|
|
39
38
|
createProviderRequestTracker;
|
|
40
39
|
materializeRuntimeReplayPlan;
|
|
41
40
|
canReplayProviderNative;
|
|
@@ -52,7 +51,6 @@ export class AiSdkCompaction {
|
|
|
52
51
|
this.targetConnectionId = deps.targetConnectionId;
|
|
53
52
|
this.targetProviderStateIdentity = deps.targetProviderStateIdentity;
|
|
54
53
|
this.now = deps.now;
|
|
55
|
-
this.modelAdapter = deps.modelAdapter;
|
|
56
54
|
this.createProviderRequestTracker = deps.createProviderRequestTracker;
|
|
57
55
|
this.materializeRuntimeReplayPlan = deps.materializeRuntimeReplayPlan;
|
|
58
56
|
this.canReplayProviderNative = deps.canReplayProviderNative;
|
|
@@ -556,7 +554,7 @@ export class AiSdkCompaction {
|
|
|
556
554
|
* the next request neither describes (tool results, user text, images
|
|
557
555
|
* appended since) is judged by the provider when the request goes out. This
|
|
558
556
|
* hook never terminates the turn: every failure fails open with a diagnostic
|
|
559
|
-
* and the request is sent; a rejection is recovered by
|
|
557
|
+
* and the request is sent; a rejection is recovered by a reactive fold
|
|
560
558
|
* (#4559).
|
|
561
559
|
*/
|
|
562
560
|
buildMidTurnCapacityCompactProjection(turnId, state, queue, providerTools, onDiagnosticPatch, origin, memoryCompactionDecision, onMemoryCompaction, abortSignal) {
|
|
@@ -616,10 +614,9 @@ export class AiSdkCompaction {
|
|
|
616
614
|
const overWindow = state.capacity !== undefined &&
|
|
617
615
|
state.baselineTokens !== undefined &&
|
|
618
616
|
state.baselineTokens + state.replyReserveTokens >= state.capacity;
|
|
619
|
-
if (!overWindow
|
|
617
|
+
if (!overWindow) {
|
|
620
618
|
return keepProjection();
|
|
621
619
|
}
|
|
622
|
-
state.compactionAttemptedThisSend = true;
|
|
623
620
|
const activeToolsForStep = options.resolveDispatch(options.activeTools).activeTools;
|
|
624
621
|
// Fold a safe completed prefix of the durable turn ledger into a
|
|
625
622
|
// replacement projection (validate → persist), shared with the reactive
|
|
@@ -642,13 +639,12 @@ export class AiSdkCompaction {
|
|
|
642
639
|
}
|
|
643
640
|
// The fold replaced the request; the baseline described the old one.
|
|
644
641
|
// The next accepted request is the first measurement of the new shape.
|
|
645
|
-
state.compactionAppliedThisSend = true;
|
|
646
642
|
state.baselineTokens = undefined;
|
|
647
643
|
acceptedProjection = {
|
|
648
644
|
sourceSignatures: incomingMessages.map(modelMessageSignature),
|
|
649
645
|
projectedMessages: outcome.replacementMessages,
|
|
650
646
|
};
|
|
651
|
-
state.
|
|
647
|
+
state.stepShaping.add('fold');
|
|
652
648
|
onDiagnosticPatch(buildActiveRequestCompactionDiagnosticPatch({
|
|
653
649
|
checkpoint: outcome.checkpoint,
|
|
654
650
|
estimatedTokensBefore: outcome.estimatedTokensBefore,
|
|
@@ -664,10 +660,10 @@ export class AiSdkCompaction {
|
|
|
664
660
|
* messages — the compaction core shared by the proactive projection stage
|
|
665
661
|
* (issue #882 PR 1) and the reactive overflow recovery (PR 2). It waits for
|
|
666
662
|
* the seq-ack durability boundary, reads the ledger, plans the fold, then
|
|
667
|
-
* validates (materializable ∧
|
|
668
|
-
*
|
|
669
|
-
*
|
|
670
|
-
*
|
|
663
|
+
* validates (materializable ∧ replay-admissible) and persists BEFORE
|
|
664
|
+
* returning the replacement, so a recovery re-projection never re-injects a
|
|
665
|
+
* covered raw span. It only shapes: the pass/terminate verdict and the
|
|
666
|
+
* diagnostic emission are the caller's.
|
|
671
667
|
*/
|
|
672
668
|
async compactActiveRequestHistory(input) {
|
|
673
669
|
const { turnId, state, queue, activeToolsForStep, abortSignal } = input;
|
|
@@ -810,7 +806,8 @@ export class AiSdkCompaction {
|
|
|
810
806
|
// next step evaluates the same condition and dispatches the same doomed
|
|
811
807
|
// summarizer call: a provider that answers slowly and fails (kimi's HTTP
|
|
812
808
|
// 200 with an error body) produced 15 such calls over 47 minutes before
|
|
813
|
-
// one main request (#4634).
|
|
809
|
+
// one main request (#4634). The latch covers the whole Turn and only
|
|
810
|
+
// fail-opens; a successful fold spends no part of it (#4559).
|
|
814
811
|
state.summarizerFailure = diagnosticReason;
|
|
815
812
|
return {
|
|
816
813
|
decision: 'fail',
|
|
@@ -860,7 +857,6 @@ export class AiSdkCompaction {
|
|
|
860
857
|
// Memory extraction is fail-open and must never perturb Compaction.
|
|
861
858
|
}
|
|
862
859
|
}
|
|
863
|
-
state.previousCheckpoint = plan.checkpoint;
|
|
864
860
|
state.projectionCheckpoint = plan.checkpoint;
|
|
865
861
|
return {
|
|
866
862
|
decision: 'compacted',
|
|
@@ -873,36 +869,38 @@ export class AiSdkCompaction {
|
|
|
873
869
|
/**
|
|
874
870
|
* Reactive overflow recovery (issue #882 PR 2): the second line of defense.
|
|
875
871
|
* When a provider rejects a request with a context-length error, fold the
|
|
876
|
-
* durable turn ledger
|
|
877
|
-
*
|
|
878
|
-
*
|
|
879
|
-
*
|
|
880
|
-
*
|
|
881
|
-
*
|
|
882
|
-
*
|
|
883
|
-
*
|
|
872
|
+
* durable turn ledger and resend — once per step: a step that a fold or an
|
|
873
|
+
* image omission has already reshaped gets no second recovery, so the
|
|
874
|
+
* resend's own rejection is terminal, while a step the provider has since
|
|
875
|
+
* accepted earns a fresh attempt. Returns the compacted messages to resend,
|
|
876
|
+
* or undefined when recovery is impossible or already spent, in which case
|
|
877
|
+
* the caller surfaces the real provider error rather than a fabricated
|
|
878
|
+
* success or a locally synthesized verdict (the provider — not the runtime —
|
|
879
|
+
* rejected the request). Non-context-length errors and turns without the
|
|
880
|
+
* mid-turn seam never reach compaction, so the default (no seam) behavior is
|
|
881
|
+
* already better than the old fake end_turn.
|
|
884
882
|
*/
|
|
885
883
|
async recoverFromOverflowError(input) {
|
|
886
884
|
const state = input.midTurnState;
|
|
887
|
-
if (!state || state.
|
|
885
|
+
if (!state || state.stepShaping.has('fold') || state.stepShaping.has('omit_images')) {
|
|
888
886
|
return undefined;
|
|
889
887
|
}
|
|
890
|
-
if (this.modelAdapter.classifyError(input.error) !== 'context_overflow')
|
|
891
|
-
return undefined;
|
|
892
888
|
const eligibleImages = collectHistoricalImageToolResults(state.priorContentEvents);
|
|
893
889
|
const imageOmission = omitHistoricalImageToolResults(input.currentMessages, eligibleImages);
|
|
894
890
|
if (imageOmission.omittedParts > 0) {
|
|
895
|
-
state.
|
|
891
|
+
state.stepShaping.add('omit_images');
|
|
892
|
+
for (const toolCallId of imageOmission.omittedToolCallIds) {
|
|
896
893
|
const image = eligibleImages.get(toolCallId);
|
|
897
|
-
|
|
898
|
-
|
|
894
|
+
// Earlier steps' omissions stay omitted: every later request is
|
|
895
|
+
// projected through this map, so losing an entry would put its image
|
|
896
|
+
// back into the very history the provider just rejected.
|
|
897
|
+
if (image)
|
|
898
|
+
state.omittedImageToolResults.set(toolCallId, image);
|
|
899
|
+
}
|
|
899
900
|
state.baselineTokens = undefined;
|
|
900
901
|
return { messages: imageOmission.messages };
|
|
901
902
|
}
|
|
902
903
|
const phase = state.compactionPhase(input.stepNumber);
|
|
903
|
-
// Entering the module spends the send's one attempt whether or not a fold
|
|
904
|
-
// comes out of it; only a selected projection sets `applied`.
|
|
905
|
-
state.compactionAttemptedThisSend = true;
|
|
906
904
|
const outcome = await this.compactActiveRequestHistory({
|
|
907
905
|
turnId: input.turnId,
|
|
908
906
|
phase,
|
|
@@ -946,7 +944,7 @@ export class AiSdkCompaction {
|
|
|
946
944
|
reason: 'overflow',
|
|
947
945
|
}));
|
|
948
946
|
// The fold replaced the request; the baseline described the rejected one.
|
|
949
|
-
state.
|
|
947
|
+
state.stepShaping.add('fold');
|
|
950
948
|
state.baselineTokens = undefined;
|
|
951
949
|
return { messages: outcome.replacementMessages };
|
|
952
950
|
}
|
|
@@ -1026,16 +1024,21 @@ export class MidTurnCapacityCompactState {
|
|
|
1026
1024
|
* be accepted but leave the reply no room (#4559).
|
|
1027
1025
|
*/
|
|
1028
1026
|
replyReserveTokens = 0;
|
|
1029
|
-
/**
|
|
1030
|
-
|
|
1027
|
+
/** Pre-turn checkpoint the turn starts from, before any fold of its own. */
|
|
1028
|
+
seedCheckpoint;
|
|
1031
1029
|
/** Checkpoint accepted during this send; pins every later durable projection. */
|
|
1032
1030
|
projectionCheckpoint;
|
|
1031
|
+
/** Latest durable checkpoint (loaded or written) for roll-forward summaries. */
|
|
1032
|
+
get previousCheckpoint() {
|
|
1033
|
+
return this.projectionCheckpoint ?? this.seedCheckpoint;
|
|
1034
|
+
}
|
|
1033
1035
|
/**
|
|
1034
|
-
*
|
|
1035
|
-
*
|
|
1036
|
-
*
|
|
1036
|
+
* What has reshaped the request since the last one a provider accepted. The
|
|
1037
|
+
* turn clears it at that acceptance, every shaper records itself here, and
|
|
1038
|
+
* every consumer reads it here — an empty set is the only proof that the next
|
|
1039
|
+
* request is a pure append of its predecessor.
|
|
1037
1040
|
*/
|
|
1038
|
-
|
|
1041
|
+
stepShaping = new Set();
|
|
1039
1042
|
/**
|
|
1040
1043
|
* finish-step boundaries the event pump has flushed into the session-event
|
|
1041
1044
|
* queue. The capacity hook's durability wait needs it: only after the pump
|
|
@@ -1047,22 +1050,6 @@ export class MidTurnCapacityCompactState {
|
|
|
1047
1050
|
omittedImageToolResults = new Map();
|
|
1048
1051
|
/** Malformed summaries spend one bounded repair budget for this whole Turn. */
|
|
1049
1052
|
summarizerFailure;
|
|
1050
|
-
/**
|
|
1051
|
-
* The compaction module has been entered in this send.
|
|
1052
|
-
*
|
|
1053
|
-
* One attempt per send, whatever its outcome: the summarizer's own failure
|
|
1054
|
-
* circuit already latches for the rest of the send, so a second entry would
|
|
1055
|
-
* dispatch nothing new. This is the budget, and only the budget (#4559).
|
|
1056
|
-
*/
|
|
1057
|
-
compactionAttemptedThisSend = false;
|
|
1058
|
-
/**
|
|
1059
|
-
* A folded projection was actually selected in this send.
|
|
1060
|
-
*
|
|
1061
|
-
* Distinct from the attempt: a fold that fails open leaves the request
|
|
1062
|
-
* carrying its full raw history, so nothing may be concluded from a later
|
|
1063
|
-
* rejection about what remains in it.
|
|
1064
|
-
*/
|
|
1065
|
-
compactionAppliedThisSend = false;
|
|
1066
1053
|
/**
|
|
1067
1054
|
* Input tokens of the last request a provider accepted before this send.
|
|
1068
1055
|
*
|
|
@@ -726,10 +726,6 @@ export class AiSdkTurn {
|
|
|
726
726
|
contextCompactedNoteWritten = await this.recordSystemNote('context_compacted', turnId);
|
|
727
727
|
}
|
|
728
728
|
};
|
|
729
|
-
// Request index (0-based) at which the active prune last rewrote the
|
|
730
|
-
// request. A step Maka pruned is not append-only, so usage may legitimately
|
|
731
|
-
// shrink.
|
|
732
|
-
let pruneAppliedAtStep;
|
|
733
729
|
const trace = new RunTrace({
|
|
734
730
|
sessionId: this.deps.backend.sessionId,
|
|
735
731
|
turnId,
|
|
@@ -888,7 +884,7 @@ export class AiSdkTurn {
|
|
|
888
884
|
// Roll-forward seed: the latest durable checkpoint (loaded or written at
|
|
889
885
|
// turn start) so a mid-turn summary only re-reads the newly folded span.
|
|
890
886
|
const checkpoint = priorReplay.latestHistoryCompactCheckpoint;
|
|
891
|
-
midTurnState.
|
|
887
|
+
midTurnState.seedCheckpoint =
|
|
892
888
|
checkpoint &&
|
|
893
889
|
canContinueHistoryCompactCheckpointForModel(checkpoint, this.deps.backend.connection, this.deps.backend.header.llmConnectionId, this.deps.backend.modelId)
|
|
894
890
|
? checkpoint
|
|
@@ -977,7 +973,7 @@ export class AiSdkTurn {
|
|
|
977
973
|
const pruned = await this.deps.compaction.pruneToolResults(turnEvents, turnId);
|
|
978
974
|
if (pruned.stats) {
|
|
979
975
|
if (pruned.stats.prunedToolResults > 0)
|
|
980
|
-
|
|
976
|
+
midTurnState?.stepShaping.add('prune');
|
|
981
977
|
contextBudgetForTelemetry = addToolResultPruneStats(contextBudgetForTelemetry ?? minimalContextBudgetDiagnostic(), pruned.stats);
|
|
982
978
|
}
|
|
983
979
|
const projectionCheckpoint = midTurnState?.projectionCheckpoint;
|
|
@@ -1132,6 +1128,14 @@ export class AiSdkTurn {
|
|
|
1132
1128
|
: undefined;
|
|
1133
1129
|
const projectedMessages = shaped?.messages ?? contextualRequestMessages;
|
|
1134
1130
|
const activeToolsForRequest = resolveDispatch(shaped?.activeTools).activeTools;
|
|
1131
|
+
// A finalization step resolves an empty tool set, so its request
|
|
1132
|
+
// legitimately drops several thousand schema tokens with no fold,
|
|
1133
|
+
// prune or image omission. Maka shaped that request; the provider did
|
|
1134
|
+
// not drop anything.
|
|
1135
|
+
if (lastStepActiveToolCount !== undefined &&
|
|
1136
|
+
activeToolsForRequest.length < lastStepActiveToolCount) {
|
|
1137
|
+
midTurnState?.stepShaping.add('tools');
|
|
1138
|
+
}
|
|
1135
1139
|
const requestCompositionId = this.runId && this.deps.backend.recordRequestComposition
|
|
1136
1140
|
? await this.deps.backend.recordRequestComposition(this.runId, {
|
|
1137
1141
|
compositionId: this.deps.newId(),
|
|
@@ -1416,12 +1420,6 @@ export class AiSdkTurn {
|
|
|
1416
1420
|
// reply's reasoning may not be resent, so input + output is
|
|
1417
1421
|
// not the floor of the next input on every wire.
|
|
1418
1422
|
const completedRequestIndex = runtimeSteps - 1;
|
|
1419
|
-
// A finalization step resolves an empty tool set, so its
|
|
1420
|
-
// request legitimately drops several thousand schema tokens
|
|
1421
|
-
// with no fold, prune or image omission. Maka shaped that
|
|
1422
|
-
// request; the provider did not drop anything.
|
|
1423
|
-
const toolSchemaShrank = lastStepActiveToolCount !== undefined &&
|
|
1424
|
-
activeToolsForRequest.length < lastStepActiveToolCount;
|
|
1425
1423
|
// Across the send boundary the comparison is the same one,
|
|
1426
1424
|
// against the last request a provider accepted before this
|
|
1427
1425
|
// send. A provider that truncates to a fixed window reports
|
|
@@ -1431,21 +1429,15 @@ export class AiSdkTurn {
|
|
|
1431
1429
|
// input tokens across eight turns with nothing reported
|
|
1432
1430
|
// (#4623). The first request of a send therefore compares
|
|
1433
1431
|
// against the persisted anchor, which is route-validated
|
|
1434
|
-
// where it is read
|
|
1435
|
-
// a smaller input by itself, so it disables the comparison.
|
|
1432
|
+
// where it is read.
|
|
1436
1433
|
const acrossSends = completedRequestIndex === 0;
|
|
1437
1434
|
const priorInput = acrossSends
|
|
1438
|
-
? midTurnState?.
|
|
1439
|
-
? undefined
|
|
1440
|
-
: midTurnState?.priorAcceptedInputTokens
|
|
1435
|
+
? midTurnState?.priorAcceptedInputTokens
|
|
1441
1436
|
: lastStepInputTokens;
|
|
1442
1437
|
if (!this.deps.session.contextProviderDroppingReported &&
|
|
1443
|
-
!toolSchemaShrank &&
|
|
1444
1438
|
midTurnState &&
|
|
1445
1439
|
priorInput !== undefined &&
|
|
1446
|
-
midTurnState.
|
|
1447
|
-
pruneAppliedAtStep !== completedRequestIndex &&
|
|
1448
|
-
midTurnState.omittedImageToolResults.size === 0 &&
|
|
1440
|
+
midTurnState.stepShaping.size === 0 &&
|
|
1449
1441
|
stepUsage !== undefined &&
|
|
1450
1442
|
Number.isFinite(stepUsage.inputTokens) &&
|
|
1451
1443
|
stepUsage.inputTokens > 0 &&
|
|
@@ -1467,6 +1459,11 @@ export class AiSdkTurn {
|
|
|
1467
1459
|
priorInputTokens: priorInput,
|
|
1468
1460
|
});
|
|
1469
1461
|
}
|
|
1462
|
+
// Clear here, not at the top of the next step: the continuation
|
|
1463
|
+
// lane archives its prune below, after this point, and archiving
|
|
1464
|
+
// is idempotent — the next step's re-projection reports no prune,
|
|
1465
|
+
// so a clear above it would lose the only record of this one.
|
|
1466
|
+
midTurnState?.stepShaping.clear();
|
|
1470
1467
|
// Fail closed: reset on every step boundary so a missing final
|
|
1471
1468
|
// step's usage does not leave a stale value from an earlier step.
|
|
1472
1469
|
// The reply needed more room than the declared window had
|
|
@@ -1581,10 +1578,10 @@ export class AiSdkTurn {
|
|
|
1581
1578
|
// nothing left to grant it, so the error is terminal.
|
|
1582
1579
|
const stepBudgetRemains = maxSteps === undefined || runtimeSteps < maxSteps;
|
|
1583
1580
|
const recovered = stepBudgetRemains &&
|
|
1581
|
+
failure.kind === 'context_overflow' &&
|
|
1584
1582
|
providerAttempt < MAX_PROVIDER_ATTEMPTS_PER_STEP &&
|
|
1585
1583
|
attemptHasNoObservableOutput()
|
|
1586
1584
|
? await this.deps.compaction.recoverFromOverflowError({
|
|
1587
|
-
error: failure,
|
|
1588
1585
|
midTurnState,
|
|
1589
1586
|
turnId,
|
|
1590
1587
|
stepNumber: runtimeSteps,
|
|
@@ -1607,8 +1604,8 @@ export class AiSdkTurn {
|
|
|
1607
1604
|
continue;
|
|
1608
1605
|
}
|
|
1609
1606
|
// Window suggestion (#4559): the provider rejected a request and
|
|
1610
|
-
// no recovery is left —
|
|
1611
|
-
// seam. The baseline is a proven-fit total (input + output of an
|
|
1607
|
+
// no recovery is left — this step's fold is spent, or there was
|
|
1608
|
+
// no seam. The baseline is a proven-fit total (input + output of an
|
|
1612
1609
|
// accepted request), so it is a number the user can declare; the
|
|
1613
1610
|
// trigger is `>=`, so declaring exactly it folds before this
|
|
1614
1611
|
// point next time. Once per send, and only when the turn is
|
|
@@ -1637,7 +1634,7 @@ export class AiSdkTurn {
|
|
|
1637
1634
|
// its size can be concluded (#4559).
|
|
1638
1635
|
if (!contextOverflowAfterCompactionNoteWritten &&
|
|
1639
1636
|
failure.kind === 'context_overflow' &&
|
|
1640
|
-
midTurnState?.
|
|
1637
|
+
midTurnState?.projectionCheckpoint !== undefined) {
|
|
1641
1638
|
contextOverflowAfterCompactionNoteWritten = true;
|
|
1642
1639
|
await this.recordSystemNote('context_overflow_after_compaction', turnId);
|
|
1643
1640
|
}
|
|
@@ -1696,10 +1693,10 @@ export class AiSdkTurn {
|
|
|
1696
1693
|
});
|
|
1697
1694
|
continue;
|
|
1698
1695
|
}
|
|
1699
|
-
// Unrecoverable (not context-length,
|
|
1700
|
-
// safe fold): surface the real provider error via
|
|
1701
|
-
// handler after settling any authoritative usage —
|
|
1702
|
-
// fabricated success.
|
|
1696
|
+
// Unrecoverable (not context-length, this step's attempt spent,
|
|
1697
|
+
// no seam, or no safe fold): surface the real provider error via
|
|
1698
|
+
// the terminal handler after settling any authoritative usage —
|
|
1699
|
+
// never a fabricated success.
|
|
1703
1700
|
terminalProviderError = failure;
|
|
1704
1701
|
terminalRetry = { error: terminalProviderError, retry };
|
|
1705
1702
|
terminalProviderErrorReason =
|