maka-agent 0.2.0-dev.4.20260830 → 0.2.0-dev.5.20260830
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/dist/eval-bundle-path.js +1 -1
- package/dist/pi-tui-runner.js +3 -1
- package/dist/runtime-host-onboarding.js +10 -1
- package/dist/runtime-host-tui-context.js +2 -6
- package/native/runtime-host-peer/prebuilds/win32-x64/maka_runtime_host_peer.node +0 -0
- package/node_modules/@maka/eval/dist/external-subject.js +1 -1
- package/node_modules/@maka/eval/dist/harbor-external-subject.js +2 -2
- package/package.json +1 -2
- package/packages/eval/dist/attempt-store.js +0 -138
- package/packages/eval/dist/cli.js +0 -119
- package/packages/eval/dist/experiment-directory.js +0 -59
- package/packages/eval/dist/experiment.js +0 -49
- package/packages/eval/dist/external-subject.js +0 -488
- package/packages/eval/dist/harbor-external-subject.js +0 -929
- package/packages/eval/dist/harbor-maka-subject.js +0 -83
- package/packages/eval/dist/harness-environment.js +0 -80
- package/packages/eval/dist/harness-executor.js +0 -1013
- package/packages/eval/dist/index.js +0 -28
- package/packages/eval/dist/install-preflight.js +0 -201
- package/packages/eval/dist/maka-artifacts.js +0 -126
- package/packages/eval/dist/maka-runtime-policy.js +0 -42
- package/packages/eval/dist/maka-subject.js +0 -268
- package/packages/eval/dist/metering-checkpoint.js +0 -80
- package/packages/eval/dist/provider-admission.js +0 -39
- package/packages/eval/dist/provider-metering.js +0 -67
- package/packages/eval/dist/provider-web-tool-surface.js +0 -74
- package/packages/eval/dist/relay-result-frame.js +0 -38
- package/packages/eval/dist/result.js +0 -94
- package/packages/eval/dist/runner.js +0 -290
- package/packages/eval/dist/spec.js +0 -135
- package/packages/eval/dist/toolchain-verification.js +0 -130
- package/packages/eval/harbor/deepseek-codex-models.json +0 -69
- package/packages/eval/harbor/deepseek-harness-profile/cordis.patch.yml +0 -140
- package/packages/eval/harbor/deepseek-harness-profile/cordis.yml +0 -18
- package/packages/eval/harbor/deepseek-harness-profile/package.json +0 -10
- package/packages/eval/harbor/docker-compose-egress-proxy.yaml +0 -57
- package/packages/eval/harbor/egress-proxy/Dockerfile +0 -25
- package/packages/eval/harbor/egress-proxy/entrypoint.sh +0 -62
- package/packages/eval/harbor/egress-proxy/network-policy +0 -148
- package/packages/eval/harbor/egress_filter.py +0 -435
- package/packages/eval/harbor/eval_framework.py +0 -40
- package/packages/eval/harbor/relay_agent.py +0 -686
- package/packages/eval/harbor/run_trial.py +0 -223
- package/packages/eval/package.json +0 -20
|
@@ -1,488 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
-
* or more contributor license agreements. See the NOTICE file
|
|
4
|
-
* distributed with this work for additional information
|
|
5
|
-
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
-
* to you under the Apache License, Version 2.0 (the
|
|
7
|
-
* "License"); you may not use this file except in compliance
|
|
8
|
-
* with the License. You may obtain a copy of the License at
|
|
9
|
-
*
|
|
10
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
-
*
|
|
12
|
-
* Unless required by applicable law or agreed to in writing,
|
|
13
|
-
* software distributed under the License is distributed on an
|
|
14
|
-
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
-
* KIND, either express or implied. See the License for the
|
|
16
|
-
* specific language governing permissions and limitations
|
|
17
|
-
* under the License.
|
|
18
|
-
*/
|
|
19
|
-
import { createHash } from 'node:crypto';
|
|
20
|
-
import { join } from 'node:path';
|
|
21
|
-
import { meteringCheckpointMacMatches, readBoundedRegularFile } from './metering-checkpoint.js';
|
|
22
|
-
import { deriveMetering } from './provider-metering.js';
|
|
23
|
-
import { TOOLCHAIN_IDENTITIES, TOOLCHAIN_IDENTITY_ENV, isExternalProfile, verifyToolchainDirectory, } from './toolchain-verification.js';
|
|
24
|
-
const BUNDLED_EXTERNAL_WRAPPER = '/opt/maka-agent/packages/eval/dist/harbor-external-subject.js';
|
|
25
|
-
export function createExternalSubjectAdapter() {
|
|
26
|
-
const verifiedToolchains = new Map();
|
|
27
|
-
return {
|
|
28
|
-
kind: 'external',
|
|
29
|
-
validate: (cell) => decodeConfig(cell.subject.config, cell.subject.credentials),
|
|
30
|
-
prepare: async ({ cells }) => {
|
|
31
|
-
verifiedToolchains.clear();
|
|
32
|
-
for (const cell of uniqueSubjects(cells)) {
|
|
33
|
-
const config = decodeConfig(cell.subject.config, cell.subject.credentials);
|
|
34
|
-
const toolchain = wrapperToolchain(config, cell.executor.config);
|
|
35
|
-
if (!toolchain)
|
|
36
|
-
continue;
|
|
37
|
-
const expected = TOOLCHAIN_IDENTITIES[toolchain.profile];
|
|
38
|
-
const mounts = executorMounts(cell.executor.config);
|
|
39
|
-
if (!mounts.some((mount) => mount.sourceEnv === toolchain.sourceEnv && mount.target === expected.root)) {
|
|
40
|
-
throw new Error(`${cell.subject.id} toolchain mount is invalid`);
|
|
41
|
-
}
|
|
42
|
-
const root = process.env[toolchain.sourceEnv];
|
|
43
|
-
if (!root)
|
|
44
|
-
throw new Error(`machine path ${toolchain.sourceEnv} is unavailable`);
|
|
45
|
-
verifiedToolchains.set(cell.subject.id, await verifyToolchainDirectory(toolchain.profile, root));
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
canReuse: ({ cell, attempt }) => {
|
|
49
|
-
const config = decodeConfig(cell.subject.config, cell.subject.credentials);
|
|
50
|
-
const toolchain = wrapperToolchain(config, cell.executor.config);
|
|
51
|
-
if (!toolchain)
|
|
52
|
-
return true;
|
|
53
|
-
const identity = verifiedToolchains.get(cell.subject.id);
|
|
54
|
-
if (!identity)
|
|
55
|
-
return false;
|
|
56
|
-
const artifact = attempt.result.artifacts.find((candidate) => candidate.kind === 'toolchain' &&
|
|
57
|
-
candidate.profile === toolchain.profile &&
|
|
58
|
-
candidate.version === identity.version &&
|
|
59
|
-
candidate.fingerprint === identity.fingerprint);
|
|
60
|
-
return artifact !== undefined;
|
|
61
|
-
},
|
|
62
|
-
async execute({ cell, context }) {
|
|
63
|
-
const config = decodeConfig(cell.subject.config, cell.subject.credentials);
|
|
64
|
-
const toolchain = wrapperToolchain(config, cell.executor.config);
|
|
65
|
-
const profile = bundledProfile(config.args);
|
|
66
|
-
const identity = toolchain ? verifiedToolchains.get(cell.subject.id) : undefined;
|
|
67
|
-
if (toolchain && !identity) {
|
|
68
|
-
throw new Error(`${cell.subject.id} toolchain was not verified before execution`);
|
|
69
|
-
}
|
|
70
|
-
const startedAt = Date.now();
|
|
71
|
-
try {
|
|
72
|
-
const execution = await context.execute({
|
|
73
|
-
command: config.command,
|
|
74
|
-
args: config.args.map((argument) => argument
|
|
75
|
-
.replaceAll('{{task.input}}', context.taskInput)
|
|
76
|
-
.replaceAll('{{task.id}}', cell.task.id)
|
|
77
|
-
.replaceAll('{{task.cwd}}', context.cwd)),
|
|
78
|
-
credentialEnvironment: config.credentialEnvironment,
|
|
79
|
-
...(Object.keys(config.environment).length === 0 && !identity
|
|
80
|
-
? {}
|
|
81
|
-
: {
|
|
82
|
-
environment: {
|
|
83
|
-
...config.environment,
|
|
84
|
-
...(identity ? { [TOOLCHAIN_IDENTITY_ENV]: JSON.stringify(identity) } : {}),
|
|
85
|
-
},
|
|
86
|
-
}),
|
|
87
|
-
...(config.result === 'exit-code' ? { captureStdout: false } : {}),
|
|
88
|
-
});
|
|
89
|
-
const decoded = execution.stdout.length === 0 ? undefined : decodeResult(execution.stdout);
|
|
90
|
-
const recovered = await recoverExternalMetering(context.metadata, profile);
|
|
91
|
-
const recoveryArtifacts = externalRecoveryArtifacts(profile, identity, recovered);
|
|
92
|
-
if (execution.termination === 'framework_timeout') {
|
|
93
|
-
return {
|
|
94
|
-
usage: decoded?.usage ?? recovered?.usage ?? null,
|
|
95
|
-
costUsd: decoded?.costUsd ?? recovered?.costUsd ?? null,
|
|
96
|
-
durationMs: Date.now() - startedAt,
|
|
97
|
-
status: 'failed',
|
|
98
|
-
failureReason: 'external subject exceeded the framework timeout',
|
|
99
|
-
artifacts: [
|
|
100
|
-
...(decoded?.artifacts ?? recoveryArtifacts),
|
|
101
|
-
...(decoded?.usage == null && recovered && decoded ? [recovered.artifact] : []),
|
|
102
|
-
{ kind: 'external_process', exitCode: execution.exitCode },
|
|
103
|
-
],
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
if (execution.diagnostic?.category === 'execution-scope-unavailable') {
|
|
107
|
-
return {
|
|
108
|
-
usage: recovered?.usage ?? null,
|
|
109
|
-
costUsd: recovered?.costUsd ?? null,
|
|
110
|
-
durationMs: Date.now() - startedAt,
|
|
111
|
-
status: context.signal?.aborted
|
|
112
|
-
? 'indeterminate'
|
|
113
|
-
: 'infra_failed',
|
|
114
|
-
failureReason: 'external subject execution scope was unavailable',
|
|
115
|
-
artifacts: [
|
|
116
|
-
...recoveryArtifacts,
|
|
117
|
-
{ kind: 'external_process', exitCode: execution.exitCode },
|
|
118
|
-
],
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
if (config.result === 'exit-code') {
|
|
122
|
-
return {
|
|
123
|
-
usage: null,
|
|
124
|
-
costUsd: null,
|
|
125
|
-
durationMs: Date.now() - startedAt,
|
|
126
|
-
status: execution.exitCode === 0
|
|
127
|
-
? 'completed'
|
|
128
|
-
: context.signal?.aborted
|
|
129
|
-
? 'indeterminate'
|
|
130
|
-
: 'failed',
|
|
131
|
-
failureReason: execution.exitCode === 0 ? null : `external subject exited ${execution.exitCode}`,
|
|
132
|
-
artifacts: [{ kind: 'external_process', exitCode: execution.exitCode }],
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
if (execution.diagnostic?.category.startsWith('result-frame-')) {
|
|
136
|
-
return {
|
|
137
|
-
usage: recovered?.usage ?? null,
|
|
138
|
-
costUsd: recovered?.costUsd ?? null,
|
|
139
|
-
durationMs: Date.now() - startedAt,
|
|
140
|
-
status: context.signal?.aborted
|
|
141
|
-
? 'indeterminate'
|
|
142
|
-
: recovered && recovered.admittedRequests > 0
|
|
143
|
-
? 'failed'
|
|
144
|
-
: 'infra_failed',
|
|
145
|
-
failureReason: 'external subject result transport failed',
|
|
146
|
-
artifacts: [
|
|
147
|
-
...recoveryArtifacts,
|
|
148
|
-
{ kind: 'external_process', exitCode: execution.exitCode },
|
|
149
|
-
],
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
// The wrapper's exit code is a projection of the status in its result
|
|
153
|
-
// frame, carried for the relay's benefit because the relay cannot read
|
|
154
|
-
// the frame. Where the frame is readable it is the authority, so a
|
|
155
|
-
// nonzero exit alongside a valid frame is not a second opinion. Only
|
|
156
|
-
// when no frame arrived does the exit code decide anything.
|
|
157
|
-
if (!decoded) {
|
|
158
|
-
return {
|
|
159
|
-
usage: recovered?.usage ?? null,
|
|
160
|
-
costUsd: recovered?.costUsd ?? null,
|
|
161
|
-
durationMs: Date.now() - startedAt,
|
|
162
|
-
status: context.signal?.aborted
|
|
163
|
-
? 'indeterminate'
|
|
164
|
-
: recovered && recovered.admittedRequests > 0
|
|
165
|
-
? 'failed'
|
|
166
|
-
: 'infra_failed',
|
|
167
|
-
failureReason: 'external subject returned no result',
|
|
168
|
-
artifacts: [
|
|
169
|
-
...recoveryArtifacts,
|
|
170
|
-
{ kind: 'external_process', exitCode: execution.exitCode },
|
|
171
|
-
],
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
return {
|
|
175
|
-
...(decoded.output === undefined ? {} : { output: decoded.output }),
|
|
176
|
-
usage: decoded.usage,
|
|
177
|
-
costUsd: decoded.costUsd,
|
|
178
|
-
durationMs: Date.now() - startedAt,
|
|
179
|
-
status: decoded.status ??
|
|
180
|
-
(execution.exitCode === 0 ? 'completed' : 'failed'),
|
|
181
|
-
failureReason: decoded.failureReason ??
|
|
182
|
-
(execution.exitCode === 0 ? null : `external subject exited ${execution.exitCode}`),
|
|
183
|
-
artifacts: decoded.artifacts,
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
catch {
|
|
187
|
-
// Reaching here means `execute` itself threw: the relay transport, the
|
|
188
|
-
// executor, or this process — not the subject, which may never have run
|
|
189
|
-
// at all. Recovered usage is still worth keeping and attributing, but
|
|
190
|
-
// admitted model work says nothing about whose failure this was, so it
|
|
191
|
-
// does not turn an infrastructure failure into a recorded zero.
|
|
192
|
-
const recovered = await recoverExternalMetering(context.metadata, profile);
|
|
193
|
-
return {
|
|
194
|
-
usage: recovered?.usage ?? null,
|
|
195
|
-
costUsd: recovered?.costUsd ?? null,
|
|
196
|
-
durationMs: Date.now() - startedAt,
|
|
197
|
-
status: context.signal?.aborted ? 'indeterminate' : 'infra_failed',
|
|
198
|
-
failureReason: context.signal?.aborted
|
|
199
|
-
? 'external subject cancelled'
|
|
200
|
-
: 'external subject execution failed',
|
|
201
|
-
artifacts: externalRecoveryArtifacts(profile, identity, recovered),
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
},
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
function externalRecoveryArtifacts(profile, identity, recovered) {
|
|
208
|
-
return [
|
|
209
|
-
...(profile && identity ? [{ kind: 'toolchain', profile, ...identity }] : []),
|
|
210
|
-
...(recovered ? [recovered.artifact] : []),
|
|
211
|
-
];
|
|
212
|
-
}
|
|
213
|
-
export async function recoverExternalMetering(metadata, profile) {
|
|
214
|
-
if (!profile || typeof metadata.trialPath !== 'string')
|
|
215
|
-
return undefined;
|
|
216
|
-
if (typeof metadata.meteringSecret !== 'string')
|
|
217
|
-
return undefined;
|
|
218
|
-
const path = join(metadata.trialPath, 'agent', `${profile}.provider-usage.json`);
|
|
219
|
-
const bytes = await readBoundedRegularFile(path);
|
|
220
|
-
if (!bytes)
|
|
221
|
-
return undefined;
|
|
222
|
-
let value;
|
|
223
|
-
try {
|
|
224
|
-
value = JSON.parse(bytes.toString('utf8'));
|
|
225
|
-
}
|
|
226
|
-
catch {
|
|
227
|
-
return undefined;
|
|
228
|
-
}
|
|
229
|
-
try {
|
|
230
|
-
// The checkpoint carries only what the proxy observed. Completeness, cost
|
|
231
|
-
// and the missing-usage count are worked out here through the same function
|
|
232
|
-
// the wrapper uses, so there is no second definition of them to disagree
|
|
233
|
-
// with — and nothing to validate a stored copy against.
|
|
234
|
-
const checkpoint = exact(value, [
|
|
235
|
-
'schemaVersion',
|
|
236
|
-
'profile',
|
|
237
|
-
'usage',
|
|
238
|
-
'settled',
|
|
239
|
-
'requests',
|
|
240
|
-
'inFlightRequests',
|
|
241
|
-
'admittedRequests',
|
|
242
|
-
'usageRequests',
|
|
243
|
-
'removedWebTools',
|
|
244
|
-
'models',
|
|
245
|
-
'toolNames',
|
|
246
|
-
'mac',
|
|
247
|
-
], 'external metering checkpoint');
|
|
248
|
-
if (checkpoint.schemaVersion !== 'maka.external_provider_usage.v2' ||
|
|
249
|
-
checkpoint.profile !== profile ||
|
|
250
|
-
!meteringCheckpointMacMatches(checkpoint, metadata.meteringSecret)) {
|
|
251
|
-
return undefined;
|
|
252
|
-
}
|
|
253
|
-
const usage = checkpoint.usage === null ? null : decodeUsage(checkpoint.usage);
|
|
254
|
-
const counts = {
|
|
255
|
-
usage,
|
|
256
|
-
settled: boolean(checkpoint.settled, 'external metering settlement'),
|
|
257
|
-
requests: count(checkpoint.requests, 'external metering requests'),
|
|
258
|
-
inFlightRequests: count(checkpoint.inFlightRequests, 'external metering in-flight requests'),
|
|
259
|
-
admittedRequests: count(checkpoint.admittedRequests, 'external metering admitted requests'),
|
|
260
|
-
usageRequests: count(checkpoint.usageRequests, 'external metering usage requests'),
|
|
261
|
-
removedWebTools: count(checkpoint.removedWebTools, 'external metering removed web tools'),
|
|
262
|
-
models: stringList(checkpoint.models, 'external metering models'),
|
|
263
|
-
toolNames: stringList(checkpoint.toolNames, 'external metering tool names'),
|
|
264
|
-
};
|
|
265
|
-
// A request may be admitted while still in flight, so admission is bounded
|
|
266
|
-
// by the requests made rather than by the requests settled. Settlement is
|
|
267
|
-
// the one claim the counts can contradict: nothing is in flight once the
|
|
268
|
-
// proxy has stopped.
|
|
269
|
-
if ((counts.settled && counts.inFlightRequests > 0) ||
|
|
270
|
-
counts.inFlightRequests > counts.requests ||
|
|
271
|
-
counts.admittedRequests > counts.requests ||
|
|
272
|
-
counts.usageRequests > counts.admittedRequests ||
|
|
273
|
-
(counts.usageRequests > 0 && usage === null)) {
|
|
274
|
-
return undefined;
|
|
275
|
-
}
|
|
276
|
-
const derived = deriveMetering(counts);
|
|
277
|
-
return {
|
|
278
|
-
usage,
|
|
279
|
-
costUsd: derived.costUsd,
|
|
280
|
-
admittedRequests: counts.admittedRequests,
|
|
281
|
-
artifact: {
|
|
282
|
-
kind: 'provider-metering-recovery',
|
|
283
|
-
profile,
|
|
284
|
-
path: `agent/${profile}.provider-usage.json`,
|
|
285
|
-
bytes: bytes.byteLength,
|
|
286
|
-
sha256: createHash('sha256').update(bytes).digest('hex'),
|
|
287
|
-
requests: counts.requests,
|
|
288
|
-
settledRequests: derived.settledRequests,
|
|
289
|
-
inFlightRequests: counts.inFlightRequests,
|
|
290
|
-
admittedRequests: counts.admittedRequests,
|
|
291
|
-
usageRequests: counts.usageRequests,
|
|
292
|
-
missingUsageRequests: derived.missingUsageRequests,
|
|
293
|
-
usageComplete: derived.usageComplete,
|
|
294
|
-
tokenBasis: derived.tokenBasis,
|
|
295
|
-
removedWebTools: counts.removedWebTools,
|
|
296
|
-
models: counts.models,
|
|
297
|
-
toolNames: counts.toolNames,
|
|
298
|
-
},
|
|
299
|
-
};
|
|
300
|
-
}
|
|
301
|
-
catch {
|
|
302
|
-
return undefined;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
function decodeConfig(value, declaredCredentials) {
|
|
306
|
-
const fields = ['command', 'args'];
|
|
307
|
-
for (const optional of ['environment', 'credentialEnvironment', 'result']) {
|
|
308
|
-
if (Object.hasOwn(value, optional))
|
|
309
|
-
fields.push(optional);
|
|
310
|
-
}
|
|
311
|
-
const config = exact(value, fields, 'external subject config');
|
|
312
|
-
if (!Array.isArray(config.args) ||
|
|
313
|
-
!config.args.every((argument) => typeof argument === 'string')) {
|
|
314
|
-
throw new Error('external subject args are invalid');
|
|
315
|
-
}
|
|
316
|
-
const environment = stringMap(config.environment, 'environment');
|
|
317
|
-
if (Object.hasOwn(environment, TOOLCHAIN_IDENTITY_ENV)) {
|
|
318
|
-
throw new Error(`environment contains reserved ${TOOLCHAIN_IDENTITY_ENV}`);
|
|
319
|
-
}
|
|
320
|
-
const credentialEnvironment = Object.hasOwn(config, 'credentialEnvironment')
|
|
321
|
-
? stringMap(config.credentialEnvironment, 'credentialEnvironment')
|
|
322
|
-
: Object.freeze(Object.fromEntries(declaredCredentials.map((name) => [name, name])));
|
|
323
|
-
for (const [target, source] of Object.entries(credentialEnvironment)) {
|
|
324
|
-
if (!declaredCredentials.includes(source)) {
|
|
325
|
-
throw new Error(`credentialEnvironment.${target} must reference a declared credential`);
|
|
326
|
-
}
|
|
327
|
-
if (Object.hasOwn(environment, target)) {
|
|
328
|
-
throw new Error(`environment and credentialEnvironment overlap at ${target}`);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
const result = config.result ?? 'protocol-v1';
|
|
332
|
-
if (result !== 'protocol-v1' && result !== 'exit-code') {
|
|
333
|
-
throw new Error('external subject result is invalid');
|
|
334
|
-
}
|
|
335
|
-
if (result === 'protocol-v1' && !bundledProfile(config.args)) {
|
|
336
|
-
throw new Error('external subject protocol-v1 requires the bundled result wrapper');
|
|
337
|
-
}
|
|
338
|
-
return {
|
|
339
|
-
command: text(config.command, 'external subject command'),
|
|
340
|
-
args: config.args,
|
|
341
|
-
environment,
|
|
342
|
-
credentialEnvironment,
|
|
343
|
-
result,
|
|
344
|
-
};
|
|
345
|
-
}
|
|
346
|
-
function wrapperToolchain(config, executorConfig) {
|
|
347
|
-
const profile = bundledProfile(config.args);
|
|
348
|
-
if (!profile)
|
|
349
|
-
return undefined;
|
|
350
|
-
const target = TOOLCHAIN_IDENTITIES[profile].root;
|
|
351
|
-
const mount = executorMounts(executorConfig).find((candidate) => candidate.target === target);
|
|
352
|
-
if (!mount)
|
|
353
|
-
throw new Error(`${profile} toolchain mount is missing`);
|
|
354
|
-
return { profile, sourceEnv: mount.sourceEnv };
|
|
355
|
-
}
|
|
356
|
-
function bundledProfile(args) {
|
|
357
|
-
if (args[0] !== BUNDLED_EXTERNAL_WRAPPER)
|
|
358
|
-
return undefined;
|
|
359
|
-
return isExternalProfile(args[1]) ? args[1] : undefined;
|
|
360
|
-
}
|
|
361
|
-
function uniqueSubjects(cells) {
|
|
362
|
-
return [...new Map(cells.map((cell) => [cell.subject.id, cell])).values()];
|
|
363
|
-
}
|
|
364
|
-
function executorMounts(value) {
|
|
365
|
-
const raw = value.mounts;
|
|
366
|
-
if (!Array.isArray(raw))
|
|
367
|
-
throw new Error('executor.config.mounts must be an array');
|
|
368
|
-
return raw.map((item, index) => {
|
|
369
|
-
const mount = exact(item, ['sourceEnv', 'target', 'readOnly'], `executor mount ${index}`);
|
|
370
|
-
if (typeof mount.sourceEnv !== 'string' ||
|
|
371
|
-
typeof mount.target !== 'string' ||
|
|
372
|
-
mount.readOnly !== true) {
|
|
373
|
-
throw new Error(`executor mount ${index} is invalid`);
|
|
374
|
-
}
|
|
375
|
-
return { sourceEnv: mount.sourceEnv, target: mount.target };
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
function stringMap(value, where) {
|
|
379
|
-
if (value === undefined)
|
|
380
|
-
return Object.freeze({});
|
|
381
|
-
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
382
|
-
throw new Error(`${where} must be an object`);
|
|
383
|
-
}
|
|
384
|
-
const entries = Object.entries(value);
|
|
385
|
-
for (const [name, item] of entries) {
|
|
386
|
-
if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(name) || typeof item !== 'string') {
|
|
387
|
-
throw new Error(`${where}.${name} is invalid`);
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
return Object.freeze(Object.fromEntries(entries));
|
|
391
|
-
}
|
|
392
|
-
function decodeResult(stdout) {
|
|
393
|
-
const parsed = JSON.parse(stdout);
|
|
394
|
-
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
395
|
-
throw new Error('external result must be an object');
|
|
396
|
-
}
|
|
397
|
-
const fields = ['schemaVersion', 'usage', 'costUsd', 'artifacts'];
|
|
398
|
-
if (Object.hasOwn(parsed, 'output'))
|
|
399
|
-
fields.push('output');
|
|
400
|
-
if (Object.hasOwn(parsed, 'status'))
|
|
401
|
-
fields.push('status');
|
|
402
|
-
if (Object.hasOwn(parsed, 'failureReason'))
|
|
403
|
-
fields.push('failureReason');
|
|
404
|
-
const result = exact(parsed, fields, 'external result');
|
|
405
|
-
if (result.schemaVersion !== 'maka.external_subject_result.v1' &&
|
|
406
|
-
result.schemaVersion !== 'maka.external_subject_result.v2')
|
|
407
|
-
throw new Error('external result schema is invalid');
|
|
408
|
-
if (result.schemaVersion === 'maka.external_subject_result.v2' &&
|
|
409
|
-
(!Object.hasOwn(result, 'status') || !Object.hasOwn(result, 'failureReason')))
|
|
410
|
-
throw new Error('external result v2 fields are invalid');
|
|
411
|
-
if (result.output !== undefined && typeof result.output !== 'string')
|
|
412
|
-
throw new Error('external result output is invalid');
|
|
413
|
-
if (result.status !== undefined &&
|
|
414
|
-
result.status !== 'completed' &&
|
|
415
|
-
result.status !== 'failed' &&
|
|
416
|
-
result.status !== 'infra_failed' &&
|
|
417
|
-
result.status !== 'indeterminate')
|
|
418
|
-
throw new Error('external result status is invalid');
|
|
419
|
-
if (result.failureReason !== undefined &&
|
|
420
|
-
result.failureReason !== null &&
|
|
421
|
-
typeof result.failureReason !== 'string')
|
|
422
|
-
throw new Error('external result failureReason is invalid');
|
|
423
|
-
if (!Array.isArray(result.artifacts) ||
|
|
424
|
-
!result.artifacts.every((artifact) => artifact && typeof artifact === 'object' && !Array.isArray(artifact))) {
|
|
425
|
-
throw new Error('external result artifacts are invalid');
|
|
426
|
-
}
|
|
427
|
-
return {
|
|
428
|
-
...(result.output === undefined ? {} : { output: result.output }),
|
|
429
|
-
usage: result.usage === null ? null : decodeUsage(result.usage),
|
|
430
|
-
costUsd: result.costUsd === null ? null : nonnegative(result.costUsd, 'external result cost'),
|
|
431
|
-
...(result.status === undefined
|
|
432
|
-
? {}
|
|
433
|
-
: {
|
|
434
|
-
status: result.status,
|
|
435
|
-
}),
|
|
436
|
-
...(result.failureReason === undefined
|
|
437
|
-
? {}
|
|
438
|
-
: { failureReason: result.failureReason }),
|
|
439
|
-
artifacts: result.artifacts,
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
function decodeUsage(value) {
|
|
443
|
-
const usage = exact(value, [
|
|
444
|
-
'inputTokens',
|
|
445
|
-
'outputTokens',
|
|
446
|
-
'cacheReadTokens',
|
|
447
|
-
'cacheWriteTokens',
|
|
448
|
-
'reasoningTokens',
|
|
449
|
-
'totalTokens',
|
|
450
|
-
], 'external usage');
|
|
451
|
-
return Object.fromEntries(Object.entries(usage).map(([key, item]) => [key, nonnegative(item, key)]));
|
|
452
|
-
}
|
|
453
|
-
function exact(value, fields, where) {
|
|
454
|
-
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
455
|
-
throw new Error(`${where} must be an object`);
|
|
456
|
-
const record = value;
|
|
457
|
-
if (Object.keys(record).length !== fields.length ||
|
|
458
|
-
fields.some((field) => !Object.hasOwn(record, field)))
|
|
459
|
-
throw new Error(`${where} fields are invalid`);
|
|
460
|
-
return record;
|
|
461
|
-
}
|
|
462
|
-
function text(value, where) {
|
|
463
|
-
if (typeof value !== 'string' || value.length === 0)
|
|
464
|
-
throw new Error(`${where} is required`);
|
|
465
|
-
return value;
|
|
466
|
-
}
|
|
467
|
-
function nonnegative(value, where) {
|
|
468
|
-
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0)
|
|
469
|
-
throw new Error(`${where} is invalid`);
|
|
470
|
-
return value;
|
|
471
|
-
}
|
|
472
|
-
function count(value, where) {
|
|
473
|
-
const decoded = nonnegative(value, where);
|
|
474
|
-
if (!Number.isSafeInteger(decoded))
|
|
475
|
-
throw new Error(`${where} is invalid`);
|
|
476
|
-
return decoded;
|
|
477
|
-
}
|
|
478
|
-
function boolean(value, where) {
|
|
479
|
-
if (typeof value !== 'boolean')
|
|
480
|
-
throw new Error(`${where} is invalid`);
|
|
481
|
-
return value;
|
|
482
|
-
}
|
|
483
|
-
function stringList(value, where) {
|
|
484
|
-
if (!Array.isArray(value) || !value.every((item) => typeof item === 'string')) {
|
|
485
|
-
throw new Error(`${where} is invalid`);
|
|
486
|
-
}
|
|
487
|
-
return value;
|
|
488
|
-
}
|