@cortex-agent/server 2026.8.3 → 2026.8.6
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/defaults/config/thread-templates/agents/benchmark-coder.json +2 -2
- package/defaults/config/thread-templates/agents/benchmark-fixer.json +17 -0
- package/defaults/config/thread-templates/agents/benchmark-reviewer.json +5 -5
- package/defaults/config/thread-templates/templates/benchmark-coder-review-fix.json +21 -0
- package/defaults/plugins/cortex-system/skills/thread/SKILL.md +1 -0
- package/defaults/prompts/directives/benchmark-coder.md +1 -0
- package/defaults/prompts/directives/benchmark-fixer.md +1 -0
- package/defaults/prompts/directives/benchmark-reviewer.md +1 -0
- package/defaults/prompts/systemPrompts/benchmark-coder.md +1 -0
- package/defaults/prompts/systemPrompts/benchmark-fixer.md +1 -0
- package/defaults/prompts/systemPrompts/benchmark-reviewer.md +1 -0
- package/dist/agent-adapter/capabilities.d.ts +32 -1
- package/dist/agent-adapter/capabilities.js +39 -1
- package/dist/agent-adapter/claude/adapter.d.ts +15 -0
- package/dist/agent-adapter/claude/adapter.js +29 -7
- package/dist/agent-adapter/claude/spawn-args.d.ts +5 -2
- package/dist/agent-adapter/claude/spawn-args.js +29 -6
- package/dist/agent-adapter/index.js +11 -1
- package/dist/agent-adapter/normalize/tool-names.d.ts +4 -0
- package/dist/agent-adapter/normalize/tool-names.js +10 -0
- package/dist/agent-adapter/pi/adapter.d.ts +20 -8
- package/dist/agent-adapter/pi/adapter.js +130 -35
- package/dist/agent-adapter/pi/agent-dir.d.ts +20 -53
- package/dist/agent-adapter/pi/agent-dir.js +48 -89
- package/dist/agent-adapter/pi/custom-catalog.d.ts +10 -0
- package/dist/agent-adapter/pi/custom-catalog.js +42 -0
- package/dist/agent-adapter/pi/defaults.d.ts +9 -0
- package/dist/agent-adapter/pi/defaults.js +14 -4
- package/dist/agent-adapter/pi/discovery.d.ts +1 -3
- package/dist/agent-adapter/pi/discovery.js +3 -15
- package/dist/agent-adapter/pi/event-parser.js +8 -0
- package/dist/agent-adapter/pi/mcp-bridge.d.ts +13 -0
- package/dist/agent-adapter/pi/mcp-bridge.js +31 -6
- package/dist/agent-adapter/pi/mcp-duration.d.ts +28 -0
- package/dist/agent-adapter/pi/mcp-duration.js +48 -0
- package/dist/agent-adapter/pi/policy-guard.d.ts +40 -0
- package/dist/agent-adapter/pi/policy-guard.js +111 -0
- package/dist/agent-adapter/pi/providers-config.d.ts +62 -0
- package/dist/agent-adapter/pi/providers-config.js +101 -0
- package/dist/agent-adapter/pi/quota-probe.d.ts +11 -0
- package/dist/agent-adapter/pi/quota-probe.js +36 -0
- package/dist/agent-adapter/pi/quota-sink.d.ts +25 -0
- package/dist/agent-adapter/pi/quota-sink.js +31 -0
- package/dist/agent-adapter/pi/session-files.d.ts +2 -0
- package/dist/agent-adapter/pi/session-files.js +19 -0
- package/dist/agent-adapter/pi/session-support.d.ts +14 -3
- package/dist/agent-adapter/pi/session-support.js +8 -0
- package/dist/agent-adapter/pi/spawn-args.d.ts +11 -1
- package/dist/agent-adapter/pi/spawn-args.js +13 -0
- package/dist/agent-adapter/pi/tool-shims.d.ts +2 -2
- package/dist/agent-adapter/pi/tool-shims.js +32 -21
- package/dist/agent-adapter/pi/web-search.js +36 -8
- package/dist/agent-adapter/types.d.ts +14 -0
- package/dist/core/locales/en.d.ts +54 -1
- package/dist/core/locales/en.js +2 -0
- package/dist/core/locales/slices/commands.d.ts +12 -1
- package/dist/core/locales/slices/commands.js +24 -2
- package/dist/core/locales/slices/providers.d.ts +45 -0
- package/dist/core/locales/slices/providers.js +95 -0
- package/dist/core/locales/zh.js +2 -0
- package/dist/core/resilient-watch.d.ts +28 -0
- package/dist/core/resilient-watch.js +110 -0
- package/dist/core/settings.js +15 -20
- package/dist/core/types/agent-types.d.ts +6 -0
- package/dist/core/types/thread-types.d.ts +21 -3
- package/dist/core/utils.d.ts +21 -1
- package/dist/core/utils.js +37 -1
- package/dist/domain/agent-run/agent-run-cli.js +3 -1
- package/dist/domain/agent-run/benchmark-local-thread-orchestrator.d.ts +31 -0
- package/dist/domain/agent-run/benchmark-local-thread-orchestrator.js +231 -24
- package/dist/domain/agent-run/benchmark-thread-identity.d.ts +15 -2
- package/dist/domain/agent-run/benchmark-thread-identity.js +123 -34
- package/dist/domain/agent-run/identity.d.ts +5 -0
- package/dist/domain/agent-run/identity.js +4 -1
- package/dist/domain/agent-run/journal.d.ts +3 -2
- package/dist/domain/agent-run/manifest.js +11 -4
- package/dist/domain/agent-run/pinned-node-process.d.ts +4 -0
- package/dist/domain/agent-run/pinned-node-process.js +9 -0
- package/dist/domain/agent-run/role-surface.d.ts +2 -2
- package/dist/domain/agent-run/role-surface.js +7 -3
- package/dist/domain/agent-run/run-config.d.ts +10 -1
- package/dist/domain/agent-run/run-config.js +57 -6
- package/dist/domain/agent-run/runner.js +149 -31
- package/dist/domain/agents/facade.d.ts +37 -122
- package/dist/domain/agents/facade.js +47 -115
- package/dist/domain/agents/spawn-config.d.ts +135 -0
- package/dist/domain/agents/spawn-config.js +121 -0
- package/dist/domain/benchmark/accounting-reconciliation.d.ts +98 -0
- package/dist/domain/benchmark/accounting-reconciliation.js +218 -0
- package/dist/domain/benchmark/arm-schema.d.ts +43 -0
- package/dist/domain/benchmark/arm-schema.js +243 -0
- package/dist/domain/benchmark/capabilities.d.ts +4 -0
- package/dist/domain/benchmark/capabilities.js +54 -0
- package/dist/domain/benchmark/decimal-text.d.ts +27 -0
- package/dist/domain/benchmark/decimal-text.js +67 -0
- package/dist/domain/benchmark/lease-echo.d.ts +43 -0
- package/dist/domain/benchmark/lease-echo.js +56 -0
- package/dist/domain/benchmark/policy-backed-runtime-deps.d.ts +14 -0
- package/dist/domain/benchmark/policy-backed-runtime-deps.js +47 -0
- package/dist/domain/benchmark/policy-compiler.d.ts +68 -0
- package/dist/domain/benchmark/policy-compiler.js +680 -0
- package/dist/domain/benchmark/resolved-policy.d.ts +110 -0
- package/dist/domain/benchmark/resolved-policy.js +195 -0
- package/dist/domain/benchmark/trial-adapter-factory.d.ts +38 -0
- package/dist/domain/benchmark/trial-adapter-factory.js +165 -0
- package/dist/domain/benchmark/trial-thread-adapter.d.ts +25 -0
- package/dist/domain/benchmark/trial-thread-adapter.js +106 -0
- package/dist/domain/benchmark/variant-proposal.d.ts +45 -0
- package/dist/domain/benchmark/variant-proposal.js +42 -0
- package/dist/domain/benchmark/workspace-lease.d.ts +99 -0
- package/dist/domain/benchmark/workspace-lease.js +272 -0
- package/dist/domain/costs/codex-quota.d.ts +29 -0
- package/dist/domain/costs/codex-quota.js +102 -0
- package/dist/domain/costs/cost-tracker.d.ts +48 -9
- package/dist/domain/costs/cost-tracker.js +74 -18
- package/dist/domain/costs/rate-limit-throttle.d.ts +23 -1
- package/dist/domain/costs/rate-limit-throttle.js +61 -6
- package/dist/domain/costs/resume-registry.d.ts +3 -1
- package/dist/domain/costs/resume-registry.js +10 -1
- package/dist/domain/mcp/tools/benchmark-thread-run.d.ts +26 -2
- package/dist/domain/mcp/tools/benchmark-thread-run.js +63 -20
- package/dist/domain/mcp/tools/cost.js +15 -6
- package/dist/domain/pi-providers/custom-provider-model.d.ts +53 -0
- package/dist/domain/pi-providers/custom-provider-model.js +122 -0
- package/dist/domain/pi-providers/custom-provider-service.d.ts +59 -0
- package/dist/domain/pi-providers/custom-provider-service.js +143 -0
- package/dist/domain/pi-providers/gateway-route-store.d.ts +23 -0
- package/dist/domain/pi-providers/gateway-route-store.js +78 -0
- package/dist/domain/pi-providers/index.d.ts +4 -0
- package/dist/domain/pi-providers/index.js +8 -0
- package/dist/domain/pi-providers/models-json-store.d.ts +6 -0
- package/dist/domain/pi-providers/models-json-store.js +77 -0
- package/dist/domain/remote/client-hot-reload.d.ts +9 -1
- package/dist/domain/remote/client-hot-reload.js +60 -12
- package/dist/domain/scheduling/jobs/_shared.d.ts +4 -1
- package/dist/domain/scheduling/jobs/_shared.js +11 -2
- package/dist/domain/scheduling/jobs/scheduled-task.js +1 -0
- package/dist/domain/scheduling/jobs/task-dispatch.js +49 -54
- package/dist/domain/sessions/session-lifecycle.d.ts +22 -0
- package/dist/domain/sessions/session-lifecycle.js +20 -1
- package/dist/domain/system/install-cli.js +8 -3
- package/dist/domain/system/system-notice.d.ts +4 -1
- package/dist/domain/system/system-notice.js +6 -2
- package/dist/domain/tasks/dispatch-utils.d.ts +0 -4
- package/dist/domain/tasks/dispatch-utils.js +24 -37
- package/dist/domain/threads/runner.d.ts +16 -1
- package/dist/domain/threads/runner.js +62 -8
- package/dist/domain/threads/template-loader.d.ts +5 -0
- package/dist/domain/threads/template-loader.js +120 -36
- package/dist/domain/threads/template-validate.d.ts +244 -0
- package/dist/domain/threads/template-validate.js +574 -0
- package/dist/domain/threads/template-validate.parity.d.ts +1 -0
- package/dist/domain/threads/template-validate.parity.js +47 -0
- package/dist/domain/threads/template-writer.d.ts +55 -0
- package/dist/domain/threads/template-writer.js +131 -0
- package/dist/domain/ui-service/app-router.d.ts +143 -1
- package/dist/domain/ui-service/app-router.js +29 -3
- package/dist/domain/ui-service/input-schemas.d.ts +228 -4
- package/dist/domain/ui-service/input-schemas.js +132 -1
- package/dist/domain/ui-service/mutate/config.d.ts +10 -1
- package/dist/domain/ui-service/mutate/config.js +45 -5
- package/dist/domain/ui-service/mutate/custom-providers.d.ts +8 -0
- package/dist/domain/ui-service/mutate/custom-providers.js +37 -0
- package/dist/domain/ui-service/mutate/profiles.d.ts +21 -0
- package/dist/domain/ui-service/mutate/profiles.js +156 -0
- package/dist/domain/ui-service/mutate/schedules.d.ts +2 -1
- package/dist/domain/ui-service/mutate/schedules.js +53 -3
- package/dist/domain/ui-service/mutate/sessions.d.ts +2 -1
- package/dist/domain/ui-service/mutate/sessions.js +25 -1
- package/dist/domain/ui-service/mutate/system.d.ts +2 -1
- package/dist/domain/ui-service/mutate/system.js +17 -3
- package/dist/domain/ui-service/mutate/thread-templates.d.ts +4 -0
- package/dist/domain/ui-service/mutate/thread-templates.js +71 -0
- package/dist/domain/ui-service/query/config.js +30 -0
- package/dist/domain/ui-service/query/custom-providers.d.ts +8 -0
- package/dist/domain/ui-service/query/custom-providers.js +13 -0
- package/dist/domain/ui-service/query/machine-detail.d.ts +2 -0
- package/dist/domain/ui-service/query/machine-detail.js +56 -0
- package/dist/domain/ui-service/query/machine-probe.d.ts +18 -0
- package/dist/domain/ui-service/query/machine-probe.js +136 -0
- package/dist/domain/ui-service/query/schedules.js +5 -0
- package/dist/domain/ui-service/query/sessions.js +8 -2
- package/dist/domain/ui-service/query/thread-template-detail.d.ts +8 -0
- package/dist/domain/ui-service/query/thread-template-detail.js +94 -0
- package/dist/domain/ui-service/query/thread-templates.d.ts +5 -3
- package/dist/domain/ui-service/query/thread-templates.js +44 -7
- package/dist/domain/ui-service/types.d.ts +298 -7
- package/dist/domain/ui-service/ui-service.js +27 -3
- package/dist/entry/app.js +24 -3
- package/dist/entry/auth-cli.d.ts +4 -1
- package/dist/entry/auth-cli.js +10 -2
- package/dist/entry/cli-help.js +6 -2
- package/dist/entry/cli.js +2 -1
- package/dist/entry/daemon.d.ts +0 -11
- package/dist/entry/daemon.js +5 -49
- package/dist/entry/provider-cli.d.ts +16 -0
- package/dist/entry/provider-cli.js +203 -0
- package/dist/events/event-types.d.ts +2 -1
- package/dist/orchestration/agent-runner.js +3 -2
- package/dist/orchestration/routing/commands/cost.d.ts +8 -0
- package/dist/orchestration/routing/commands/cost.js +93 -21
- package/dist/orchestration/routing/commands/login-custom.d.ts +26 -0
- package/dist/orchestration/routing/commands/login-custom.js +89 -0
- package/dist/orchestration/routing/commands/login.d.ts +3 -0
- package/dist/orchestration/routing/commands/login.js +7 -0
- package/dist/orchestration/routing/message-router.js +48 -33
- package/dist/orchestration/routing/webhook.js +1 -1
- package/dist/orchestration/session-events.d.ts +2 -1
- package/dist/orchestration/session-events.js +1 -0
- package/dist/orchestration/thread-callback.js +1 -1
- package/dist/store/conversation-history-repo.d.ts +5 -1
- package/dist/store/conversation-history-repo.js +2 -1
- package/dist/store/cost-repo.d.ts +17 -0
- package/dist/store/cost-repo.js +47 -5
- package/dist/store/profile-repo.d.ts +0 -10
- package/dist/store/profile-repo.js +34 -56
- package/dist/store/session-registry-repo.d.ts +12 -0
- package/dist/store/session-registry-repo.js +16 -0
- package/package.json +1 -1
- package/web/dist/assets/index-DYMfaAi4.css +1 -0
- package/web/dist/assets/index-uOoZjGKa.js +141 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-DrNx9R6c.js +0 -139
- package/web/dist/assets/index-zpf3OSKC.css +0 -1
|
@@ -0,0 +1,680 @@
|
|
|
1
|
+
// input: phase-A arm resolution, container assets, deterministic clocks
|
|
2
|
+
// output: deep-frozen resolved policy and disposable run config
|
|
3
|
+
// pos: Ordered phase-B benchmark policy compiler
|
|
4
|
+
// >>> 一旦我被更新,务必更新我的开头注释与所属文件夹 CORTEX.md <<<
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { CAPABILITIES_BY_BACKEND, Capability, LONG_MCP_CALL_VERSION_GOVERNANCE, } from '../../agent-adapter/capabilities.js';
|
|
9
|
+
import { DEFAULT_TOOLS } from '../../agent-adapter/claude/defaults.js';
|
|
10
|
+
import { nativeToolNames } from '../../agent-adapter/normalize/tool-names.js';
|
|
11
|
+
import { GATE2_LEASE_STATE } from '../../agent-adapter/pi/policy-guard.js';
|
|
12
|
+
import { canonicalJsonSha256, computeBundleManifestHash, computeModelExecutionIdentityHash, computeRoleToolSurfaceHash, } from '../agent-run/identity.js';
|
|
13
|
+
import { directoryContentSha256 } from '../agent-run/role-surface.js';
|
|
14
|
+
import { parseArmDefinitionForCompiler, validateArmPostCredential, validateArmResolutionShape, ArmValidationError, } from './arm-schema.js';
|
|
15
|
+
import { capabilityWhitelistForArm, childTemplateWhitelistForArm, } from './capabilities.js';
|
|
16
|
+
import { deepFreezePolicy, PolicyCompilationError, reportBenchmarkFailure, } from './resolved-policy.js';
|
|
17
|
+
export { PolicyCompilationError } from './resolved-policy.js';
|
|
18
|
+
const ROOT_RUN_ID_PATTERN = /^[A-Za-z0-9._-]+$/;
|
|
19
|
+
const IMAGE_DIGEST_PATTERN = /^sha256:[0-9a-f]{64}$/;
|
|
20
|
+
const ADAPTER_ASSET_KINDS = new Set([
|
|
21
|
+
'profile', 'cli_artifact', 'prompt', 'tool_set', 'plugin_dir', 'skill', 'mcp_server',
|
|
22
|
+
'thread_template', 'thread_agent',
|
|
23
|
+
]);
|
|
24
|
+
function fail(reason, detail, payload) {
|
|
25
|
+
throw new PolicyCompilationError(reason, detail, payload);
|
|
26
|
+
}
|
|
27
|
+
function sha256(value) {
|
|
28
|
+
return createHash('sha256').update(value).digest('hex');
|
|
29
|
+
}
|
|
30
|
+
function expectedHashKey(kind, logicalName) {
|
|
31
|
+
return `${kind}:${logicalName}`;
|
|
32
|
+
}
|
|
33
|
+
function appendInventory(context, kind, logicalName, resolvedPath, contentSha256) {
|
|
34
|
+
const expected = context.input.expected_asset_hashes?.[expectedHashKey(kind, logicalName)];
|
|
35
|
+
if (expected !== undefined && expected !== contentSha256) {
|
|
36
|
+
fail('asset_hash_mismatch', `${kind}:${logicalName}`);
|
|
37
|
+
}
|
|
38
|
+
context.inventory.push({
|
|
39
|
+
ordinal: context.inventory.length + 1,
|
|
40
|
+
kind,
|
|
41
|
+
logical_name: logicalName,
|
|
42
|
+
resolved_path: resolvedPath,
|
|
43
|
+
content_sha256: contentSha256,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function readAsset(context, file) {
|
|
47
|
+
try {
|
|
48
|
+
return context.deps.read_file?.(file) ?? fs.readFileSync(file);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
const code = error.code;
|
|
52
|
+
if (code === 'ENOENT')
|
|
53
|
+
fail('asset_missing', file);
|
|
54
|
+
return fail('asset_unreadable', `${file}: ${error.message}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function directoryHash(directory) {
|
|
58
|
+
try {
|
|
59
|
+
return directoryContentSha256(directory);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
const code = error.code;
|
|
63
|
+
if (code === 'ENOENT')
|
|
64
|
+
fail('asset_missing', directory);
|
|
65
|
+
return fail('asset_unreadable', `${directory}: ${error.message}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function validateTrialInput(input) {
|
|
69
|
+
const strings = [input.trial_id, input.task.task_id, input.task.image_ref, input.profile_name];
|
|
70
|
+
if (strings.some(value => typeof value !== 'string' || value.length === 0)) {
|
|
71
|
+
fail('arm_schema_invalid', 'trial, task, image, and profile names must be non-empty');
|
|
72
|
+
}
|
|
73
|
+
if (!ROOT_RUN_ID_PATTERN.test(input.root_run_id)) {
|
|
74
|
+
fail('arm_schema_invalid', 'root_run_id has invalid characters');
|
|
75
|
+
}
|
|
76
|
+
if (!IMAGE_DIGEST_PATTERN.test(input.task.image_digest)) {
|
|
77
|
+
fail('image_digest_unpinned', input.task.image_digest || 'missing image digest');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function compileDeadline(arm, deps) {
|
|
81
|
+
const compiled = deps.wall_clock_ms();
|
|
82
|
+
const monotonic = deps.monotonic_ns();
|
|
83
|
+
const absolute = compiled + arm.limits.deadline_seconds * 1_000;
|
|
84
|
+
const valid = Number.isSafeInteger(compiled) && monotonic >= 0n
|
|
85
|
+
&& Number.isSafeInteger(absolute) && absolute > compiled;
|
|
86
|
+
if (!valid)
|
|
87
|
+
fail('deadline_nonpositive', 'absolute deadline must be a positive safe integer');
|
|
88
|
+
return {
|
|
89
|
+
compiled_at_epoch_ms: compiled,
|
|
90
|
+
absolute_epoch_ms: absolute,
|
|
91
|
+
monotonic_origin_ns: monotonic,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function resolveCredential(context) {
|
|
95
|
+
const id = context.arm.credential_capability;
|
|
96
|
+
const credential = context.input.credential_capabilities.find(candidate => candidate.id === id);
|
|
97
|
+
if (!credential)
|
|
98
|
+
fail('credential_capability_unknown', id);
|
|
99
|
+
if (credential.state === 'unsupported')
|
|
100
|
+
fail('credential_capability_unsupported', id);
|
|
101
|
+
if (context.input.paid_run && credential.state !== 'live-handshake-passed') {
|
|
102
|
+
fail('credential_capability_state_insufficient', id);
|
|
103
|
+
}
|
|
104
|
+
appendInventory(context, 'credential_capability', id, `arm-resolution://credential/${id}`, canonicalJsonSha256(credential));
|
|
105
|
+
return credential;
|
|
106
|
+
}
|
|
107
|
+
function resolvedProfile(context) {
|
|
108
|
+
try {
|
|
109
|
+
return context.deps.resolve_profile(context.input.profile_name);
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
return fail('profile_unresolvable', error.message);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function validateProfileShape(profile) {
|
|
116
|
+
if (profile.fallback.length > 0)
|
|
117
|
+
fail('profile_has_fallbacks', profile.name);
|
|
118
|
+
const extraOption = Object.keys(profile.extraOption)[0];
|
|
119
|
+
if (extraOption)
|
|
120
|
+
fail('profile_extra_option_unsupported', extraOption);
|
|
121
|
+
}
|
|
122
|
+
function validatePiCapability(context) {
|
|
123
|
+
const piArm = context.arm.kind === 'cortex' && context.arm.backend === 'pi';
|
|
124
|
+
if (piArm && context.input.pi_benchmark_capability_proven !== true) {
|
|
125
|
+
fail('backend_unsupported_for_kind', 'PI benchmark capability is unproven');
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// The two long-MCP-call checks ask different questions and therefore have different scopes.
|
|
129
|
+
// The capability asks whether this backend type implements a configurable per-call budget at all —
|
|
130
|
+
// a static property of the backend, meaningful before any composition exists — so it is required of
|
|
131
|
+
// every cortex arm. The version asks whether this concrete CLI can sustain a real long call, which
|
|
132
|
+
// is only meaningful where such a call can occur: an arm exposing no benchmark MCP server has no
|
|
133
|
+
// call to orphan. No shipped arm exposes one yet, so the version check first bites on the arms that
|
|
134
|
+
// introduce the benchmark thread-run surface.
|
|
135
|
+
function validateLongMcpCallCapability(context) {
|
|
136
|
+
if (context.arm.kind !== 'cortex')
|
|
137
|
+
return;
|
|
138
|
+
const backend = context.arm.backend;
|
|
139
|
+
if (!CAPABILITIES_BY_BACKEND[backend].has(Capability.BenchmarkLongMcpCall)) {
|
|
140
|
+
fail('backend_lacks_long_mcp_call', backend);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function exposesBenchmarkMcp(context) {
|
|
144
|
+
return Object.values(context.input.roles)
|
|
145
|
+
.some(role => role.mcp_composition === 'benchmark-thread-run');
|
|
146
|
+
}
|
|
147
|
+
// Only an explicit declaration can pass this check, in exactly two shapes: a backend whose CLI owns
|
|
148
|
+
// the MCP client must present a verified version, and a backend whose calls our own bridge issues is
|
|
149
|
+
// exempt from the version because the version is not the governing fact. Everything else — an
|
|
150
|
+
// unlisted version, an undeclared backend — refuses.
|
|
151
|
+
function validateLongMcpCallCliVersion(context) {
|
|
152
|
+
if (context.arm.kind !== 'cortex' || !exposesBenchmarkMcp(context))
|
|
153
|
+
return;
|
|
154
|
+
const version = context.input.cli_artifact.version;
|
|
155
|
+
const governance = LONG_MCP_CALL_VERSION_GOVERNANCE[context.arm.backend];
|
|
156
|
+
if (governance?.governed_by === 'cortex-bridge')
|
|
157
|
+
return;
|
|
158
|
+
if (governance?.governed_by === 'cli-version'
|
|
159
|
+
&& governance.verified_versions.includes(version))
|
|
160
|
+
return;
|
|
161
|
+
fail('cli_version_unsupported_for_long_mcp_call', version);
|
|
162
|
+
}
|
|
163
|
+
function failProfileValueMismatch(field, armValue, profileValue) {
|
|
164
|
+
return fail('arm_profile_value_mismatch', `arm ${field} differs from resolved profile ${field}`, { field, arm_value: armValue, profile_value: profileValue });
|
|
165
|
+
}
|
|
166
|
+
function validateProfileIdentity(arm, profile) {
|
|
167
|
+
if (profile.model !== arm.model) {
|
|
168
|
+
failProfileValueMismatch('model', arm.model, profile.model);
|
|
169
|
+
}
|
|
170
|
+
if (arm.kind === 'cortex' && profile.backend !== arm.backend) {
|
|
171
|
+
failProfileValueMismatch('backend', arm.backend, profile.backend);
|
|
172
|
+
}
|
|
173
|
+
if (profile.provider !== arm.provider) {
|
|
174
|
+
failProfileValueMismatch('provider', arm.provider, profile.provider);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function validateProfile(context, profile) {
|
|
178
|
+
validateProfileShape(profile);
|
|
179
|
+
validatePiCapability(context);
|
|
180
|
+
validateLongMcpCallCapability(context);
|
|
181
|
+
validateProfileIdentity(context.arm, profile);
|
|
182
|
+
}
|
|
183
|
+
function profileProjection(profile) {
|
|
184
|
+
return {
|
|
185
|
+
name: profile.name,
|
|
186
|
+
model: profile.model,
|
|
187
|
+
backend: profile.backend,
|
|
188
|
+
mode: profile.mode,
|
|
189
|
+
provider: profile.provider,
|
|
190
|
+
extra_env_keys: Object.keys(profile.extraEnv).sort(),
|
|
191
|
+
extra_option: profile.extraOption,
|
|
192
|
+
claude_backend: profile.claudeBackend,
|
|
193
|
+
thinking: profile.thinking,
|
|
194
|
+
fallback: profile.fallback,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function inventoryProfile(context) {
|
|
198
|
+
const profile = resolvedProfile(context);
|
|
199
|
+
validateProfile(context, profile);
|
|
200
|
+
appendInventory(context, 'profile', profile.name, `profile://${profile.name}`, canonicalJsonSha256(profileProjection(profile)));
|
|
201
|
+
return profile;
|
|
202
|
+
}
|
|
203
|
+
function inventoryCli(context) {
|
|
204
|
+
const bytes = readAsset(context, context.input.cli_artifact.path);
|
|
205
|
+
const logicalName = context.arm.backend ?? context.arm.vendor_agent;
|
|
206
|
+
const content = canonicalJsonSha256({
|
|
207
|
+
binary_sha256: sha256(bytes),
|
|
208
|
+
version: context.input.cli_artifact.version,
|
|
209
|
+
});
|
|
210
|
+
appendInventory(context, 'cli_artifact', logicalName, context.input.cli_artifact.path, content);
|
|
211
|
+
}
|
|
212
|
+
function inventoryRoute(context) {
|
|
213
|
+
const route = {
|
|
214
|
+
upstream_base_url: context.input.credential.upstream_base_url,
|
|
215
|
+
route_identity_host: context.input.credential.route_identity_host,
|
|
216
|
+
};
|
|
217
|
+
appendInventory(context, 'provider_route', context.arm.credential_capability, `credential://route/${context.arm.credential_capability}`, canonicalJsonSha256(route));
|
|
218
|
+
}
|
|
219
|
+
function validateRoleSource(slot, source) {
|
|
220
|
+
const toolsValid = Array.isArray(source.tools) && source.tools.length > 0
|
|
221
|
+
&& source.tools.every(tool => tool.length > 0 && !tool.includes(','));
|
|
222
|
+
if (!toolsValid || source.disable_hooks !== true) {
|
|
223
|
+
fail('arm_schema_invalid', `role ${slot} is not a closed agent-run role`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// The closed lease-state vocabulary of §6.1, spelled as §6.1 spells it (design §13.10 GS1). A key
|
|
227
|
+
// outside this set names no state, so no allow-list can be authored for it.
|
|
228
|
+
const LEASE_STATES = new Set([
|
|
229
|
+
'parent-writable', 'draining', 'thread-owned', 'answer-frozen', 'released',
|
|
230
|
+
]);
|
|
231
|
+
// The lease state each role class runs its steps under (design section 16 (16.1) LS2). It is a
|
|
232
|
+
// function of the slot's role class and of nothing else — never of a prompt, a template field or an
|
|
233
|
+
// environment variable. `parent` keeps Gate 2's one-shot state; every in-trial child slot is only
|
|
234
|
+
// ever armed while the lease is `thread-owned`, so a second key would name a state no step reaches.
|
|
235
|
+
const ROLE_CLASS_LEASE_STATE = {
|
|
236
|
+
parent: GATE2_LEASE_STATE,
|
|
237
|
+
'benchmark-coder': 'thread-owned',
|
|
238
|
+
'benchmark-reviewer': 'thread-owned',
|
|
239
|
+
'benchmark-fixer': 'thread-owned',
|
|
240
|
+
};
|
|
241
|
+
// GS4: one document carries exactly one tool-label namespace, the arm's own backend's. Claude's set
|
|
242
|
+
// is the shipped DEFAULT_TOOLS; PI's is its native map plus the shim-registered `agent`. Both are
|
|
243
|
+
// read from the tables their adapters dispatch through, never restated here.
|
|
244
|
+
const GUARD_TOOL_LABELS = {
|
|
245
|
+
claude: new Set(DEFAULT_TOOLS.split(',')),
|
|
246
|
+
pi: new Set(nativeToolNames('pi')),
|
|
247
|
+
};
|
|
248
|
+
// GE3: the canonicaliser of the derived rule set. G5.2 hashes the canonicalised rule set itself and
|
|
249
|
+
// never a name for it (design:2040), so the key-sorted object is what reaches both the RTSH input
|
|
250
|
+
// and `role_policy_guard`. It no longer walks arbitrary JSON: under GE2 the compiler constructs the
|
|
251
|
+
// only value it ever canonicalises, so a general walk would be branches nothing can reach.
|
|
252
|
+
function compiledGuard(rules) {
|
|
253
|
+
return Object.fromEntries(Object.keys(rules).sort().map(state => [state, [...rules[state]]]));
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* §7.2 P19's `compile(role, leaseState)` — the sole producer of the mandatory benchmark policy
|
|
257
|
+
* guard (design §13.10 GE2). It reads exactly the two frozen values GC6 allows, the role's `tools`
|
|
258
|
+
* and the arm's `backend`, and performs no filesystem lookup and no name resolution (GT7). The
|
|
259
|
+
* allow-list is `tools` verbatim and in frozen order (GC2) under the one lease state Gate 2 can
|
|
260
|
+
* enter (GC1/GS2); nothing is enumerated as denied, because everything outside the list denies by
|
|
261
|
+
* the evaluator's default (GC3), which is what makes §3.1(h.4)'s six forbidden tools forbidden here
|
|
262
|
+
* (GC4). The lease state is `ROLE_CLASS_LEASE_STATE`'s entry for the slot, which is what varies the
|
|
263
|
+
* guard with the lease without reopening its shape (GC7; design section 16 (16.1) LS1/LS2).
|
|
264
|
+
*
|
|
265
|
+
* Every compiled benchmark role governs a model process, so it may not run unguarded: a derivation
|
|
266
|
+
* that cannot produce a rule set fails the compile rather than yielding an unguarded spawn — §6.8
|
|
267
|
+
* G1, code 30 `policy_guard_absent`, GE4. No new failure code is allocated.
|
|
268
|
+
*/
|
|
269
|
+
function roleGuard(slot, source, backend) {
|
|
270
|
+
// Reached by every vendor baseline: it declares no backend, so there is no namespace to draw an
|
|
271
|
+
// allow-list from and no guard can be derived.
|
|
272
|
+
const labels = backend === undefined ? undefined : GUARD_TOOL_LABELS[backend];
|
|
273
|
+
if (labels === undefined) {
|
|
274
|
+
fail('policy_guard_absent', `role://${slot} declares no backend to draw tool labels from`);
|
|
275
|
+
}
|
|
276
|
+
const leaseState = ROLE_CLASS_LEASE_STATE[slot];
|
|
277
|
+
if (leaseState === undefined) {
|
|
278
|
+
fail('policy_guard_absent', `role://${slot} names no guard role class`);
|
|
279
|
+
}
|
|
280
|
+
// Kept as the branch that refuses a state no allow-list has been designed for. Not a fallback —
|
|
281
|
+
// reaching it is a defect in the table above, and it fails closed.
|
|
282
|
+
if (!LEASE_STATES.has(leaseState)) {
|
|
283
|
+
fail('policy_guard_absent', `role://${slot} lease state '${leaseState}' names no lease state`);
|
|
284
|
+
}
|
|
285
|
+
// GS6 is enforced on the compiler's output: a label the backend's dispatch boundary cannot answer
|
|
286
|
+
// to would put a rule in the guard that governs nothing, which is how a Claude surface silently
|
|
287
|
+
// reaching a PI arm would otherwise survive. An empty or comma-bearing `tools` never arrives here
|
|
288
|
+
// — `arm-schema.ts` and `validateRoleSource` refuse it earlier as `arm_schema_invalid`.
|
|
289
|
+
const foreign = source.tools.filter(tool => !labels.has(tool));
|
|
290
|
+
if (foreign.length > 0) {
|
|
291
|
+
fail('policy_guard_absent', `role://${slot} tools outside the ${backend} namespace: ${foreign.join(',')}`);
|
|
292
|
+
}
|
|
293
|
+
if (new Set(source.tools).size !== source.tools.length) {
|
|
294
|
+
fail('policy_guard_absent', `role://${slot} repeats a tool in its allow-list`);
|
|
295
|
+
}
|
|
296
|
+
return compiledGuard({ [leaseState]: source.tools });
|
|
297
|
+
}
|
|
298
|
+
function promptRole(context, slot) {
|
|
299
|
+
const source = context.input.roles[slot];
|
|
300
|
+
validateRoleSource(slot, source);
|
|
301
|
+
const systemPrompt = readAsset(context, source.system_prompt_path);
|
|
302
|
+
const directive = readAsset(context, source.directive_path);
|
|
303
|
+
appendInventory(context, 'prompt', `${slot}:system_prompt`, source.system_prompt_path, sha256(systemPrompt));
|
|
304
|
+
appendInventory(context, 'prompt', `${slot}:directive`, source.directive_path, sha256(directive));
|
|
305
|
+
return {
|
|
306
|
+
slot,
|
|
307
|
+
source,
|
|
308
|
+
role: {
|
|
309
|
+
systemPrompt: systemPrompt.toString('utf8'),
|
|
310
|
+
tools: [...source.tools],
|
|
311
|
+
pluginDirs: [...source.plugin_dirs],
|
|
312
|
+
mcpComposition: source.mcp_composition,
|
|
313
|
+
mcpConfigPaths: [...source.mcp_config_paths],
|
|
314
|
+
disableHooks: true,
|
|
315
|
+
},
|
|
316
|
+
systemPromptSha256: sha256(systemPrompt),
|
|
317
|
+
directiveSha256: sha256(directive),
|
|
318
|
+
pluginDirs: [],
|
|
319
|
+
skills: [],
|
|
320
|
+
benchmarkPolicyGuard: roleGuard(slot, source, context.arm.backend),
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
function inventoryPrompts(context) {
|
|
324
|
+
const slots = Object.keys(context.input.roles).sort();
|
|
325
|
+
if (context.arm.kind === 'cortex' && !slots.includes('parent')) {
|
|
326
|
+
fail('asset_missing', 'role://parent');
|
|
327
|
+
}
|
|
328
|
+
return slots.map(slot => promptRole(context, slot));
|
|
329
|
+
}
|
|
330
|
+
function inventoryTools(context, roles) {
|
|
331
|
+
for (const role of roles) {
|
|
332
|
+
appendInventory(context, 'tool_set', role.slot, `role://${role.slot}/tools`, canonicalJsonSha256(role.role.tools));
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function readSkillDirectories(pluginDir) {
|
|
336
|
+
const root = path.join(pluginDir, 'skills');
|
|
337
|
+
if (!fs.existsSync(root))
|
|
338
|
+
return [];
|
|
339
|
+
try {
|
|
340
|
+
return fs.readdirSync(root, { withFileTypes: true })
|
|
341
|
+
.filter(entry => entry.isDirectory()).map(entry => entry.name).sort();
|
|
342
|
+
}
|
|
343
|
+
catch (error) {
|
|
344
|
+
return fail('asset_unreadable', `${root}: ${error.message}`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
function inventoryRolePlugins(context, role) {
|
|
348
|
+
const names = new Set();
|
|
349
|
+
const pluginDirs = [...role.role.pluginDirs].sort();
|
|
350
|
+
for (const [index, pluginDir] of pluginDirs.entries()) {
|
|
351
|
+
const content_sha256 = directoryHash(pluginDir);
|
|
352
|
+
role.pluginDirs.push({ path: pluginDir, content_sha256 });
|
|
353
|
+
const logicalName = `${role.slot}:${index}:${path.basename(pluginDir)}`;
|
|
354
|
+
appendInventory(context, 'plugin_dir', logicalName, pluginDir, content_sha256);
|
|
355
|
+
for (const name of readSkillDirectories(pluginDir)) {
|
|
356
|
+
if (names.has(name))
|
|
357
|
+
fail('duplicate_skill_name', `${role.slot}:${name}`);
|
|
358
|
+
names.add(name);
|
|
359
|
+
const resolvedPath = path.join(pluginDir, 'skills', name);
|
|
360
|
+
const skillHash = directoryHash(resolvedPath);
|
|
361
|
+
role.skills.push({ name, content_sha256: skillHash });
|
|
362
|
+
appendInventory(context, 'skill', `${role.slot}:${name}`, resolvedPath, skillHash);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function inventoryPlugins(context, roles) {
|
|
367
|
+
for (const role of roles)
|
|
368
|
+
inventoryRolePlugins(context, role);
|
|
369
|
+
}
|
|
370
|
+
function parseMcpServers(file, bytes) {
|
|
371
|
+
try {
|
|
372
|
+
const value = JSON.parse(bytes.toString('utf8'));
|
|
373
|
+
if (!value || typeof value.mcpServers !== 'object' || value.mcpServers === null) {
|
|
374
|
+
fail('mcp_composition_invalid', file);
|
|
375
|
+
}
|
|
376
|
+
return Object.keys(value.mcpServers);
|
|
377
|
+
}
|
|
378
|
+
catch (error) {
|
|
379
|
+
if (error instanceof PolicyCompilationError)
|
|
380
|
+
throw error;
|
|
381
|
+
return fail('mcp_composition_invalid', `${file}: ${error.message}`);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
function assertRestrictedMcp(role, servers) {
|
|
385
|
+
const unique = [...new Set(servers)].sort();
|
|
386
|
+
if (role.role.mcpComposition === 'none' && unique.length !== 0) {
|
|
387
|
+
fail('mcp_composition_invalid', `${role.slot}: none exposed a server`);
|
|
388
|
+
}
|
|
389
|
+
const expected = JSON.stringify(['cortex-benchmark-thread']);
|
|
390
|
+
if (role.role.mcpComposition === 'benchmark-thread-run'
|
|
391
|
+
&& JSON.stringify(unique) !== expected) {
|
|
392
|
+
fail('mcp_composition_invalid', `${role.slot}: benchmark server set differs`);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
function inventoryRoleMcp(context, role) {
|
|
396
|
+
const servers = [];
|
|
397
|
+
const files = [...role.role.mcpConfigPaths].sort();
|
|
398
|
+
for (const [index, file] of files.entries()) {
|
|
399
|
+
const bytes = readAsset(context, file);
|
|
400
|
+
const logicalName = `${role.slot}:${index}:${path.basename(file)}`;
|
|
401
|
+
appendInventory(context, 'mcp_server', logicalName, file, sha256(bytes));
|
|
402
|
+
servers.push(...parseMcpServers(file, bytes));
|
|
403
|
+
}
|
|
404
|
+
assertRestrictedMcp(role, servers);
|
|
405
|
+
}
|
|
406
|
+
function inventoryMcp(context, roles) {
|
|
407
|
+
for (const role of roles)
|
|
408
|
+
inventoryRoleMcp(context, role);
|
|
409
|
+
}
|
|
410
|
+
function templateAgents(file, bytes, expectedName) {
|
|
411
|
+
try {
|
|
412
|
+
const value = JSON.parse(bytes.toString('utf8'));
|
|
413
|
+
if (value.name !== expectedName)
|
|
414
|
+
fail('template_not_whitelisted', expectedName);
|
|
415
|
+
if (!Array.isArray(value.agents) || !value.agents.every(agent => typeof agent === 'string')) {
|
|
416
|
+
fail('asset_unreadable', `${file}: invalid agents`);
|
|
417
|
+
}
|
|
418
|
+
return [...new Set(value.agents)].sort();
|
|
419
|
+
}
|
|
420
|
+
catch (error) {
|
|
421
|
+
if (error instanceof PolicyCompilationError)
|
|
422
|
+
throw error;
|
|
423
|
+
return fail('asset_unreadable', `${file}: ${error.message}`);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
function denyUnexpectedTemplates(context, allowed) {
|
|
427
|
+
const denied = Object.keys(context.input.thread_templates)
|
|
428
|
+
.filter(name => !allowed.includes(name)).sort()[0];
|
|
429
|
+
if (denied)
|
|
430
|
+
fail('template_not_whitelisted', denied);
|
|
431
|
+
}
|
|
432
|
+
function inventoryAgent(context, name, loaded) {
|
|
433
|
+
if (loaded.has(name))
|
|
434
|
+
return;
|
|
435
|
+
const file = context.input.thread_agents[name];
|
|
436
|
+
if (!file)
|
|
437
|
+
fail('asset_missing', `thread-agent://${name}`);
|
|
438
|
+
const bytes = readAsset(context, file);
|
|
439
|
+
appendInventory(context, 'thread_agent', name, file, sha256(bytes));
|
|
440
|
+
loaded.add(name);
|
|
441
|
+
}
|
|
442
|
+
function inventoryTemplates(context) {
|
|
443
|
+
const allowed = childTemplateWhitelistForArm(context.arm);
|
|
444
|
+
denyUnexpectedTemplates(context, allowed);
|
|
445
|
+
const loadedAgents = new Set();
|
|
446
|
+
for (const name of allowed) {
|
|
447
|
+
const file = context.input.thread_templates[name];
|
|
448
|
+
if (!file)
|
|
449
|
+
fail('asset_missing', `thread-template://${name}`);
|
|
450
|
+
const bytes = readAsset(context, file);
|
|
451
|
+
appendInventory(context, 'thread_template', name, file, sha256(bytes));
|
|
452
|
+
for (const agent of templateAgents(file, bytes, name)) {
|
|
453
|
+
inventoryAgent(context, agent, loadedAgents);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return loadedAgents;
|
|
457
|
+
}
|
|
458
|
+
function validateRequiredRoles(roles, agents) {
|
|
459
|
+
const slots = new Set(roles.map(role => role.slot));
|
|
460
|
+
const missing = [...agents].sort().find(agent => !slots.has(agent));
|
|
461
|
+
if (missing)
|
|
462
|
+
fail('asset_missing', `role://${missing}`);
|
|
463
|
+
}
|
|
464
|
+
function inventoryLimits(context) {
|
|
465
|
+
appendInventory(context, 'limits', context.arm.name, 'arm://limits', canonicalJsonSha256(context.arm.limits));
|
|
466
|
+
}
|
|
467
|
+
function compileAssets(context) {
|
|
468
|
+
const deadline = compileDeadline(context.arm, context.deps);
|
|
469
|
+
const credential = resolveCredential(context);
|
|
470
|
+
validateArmPostCredential(context.arm);
|
|
471
|
+
const profile = inventoryProfile(context);
|
|
472
|
+
inventoryCli(context);
|
|
473
|
+
validateLongMcpCallCliVersion(context);
|
|
474
|
+
inventoryRoute(context);
|
|
475
|
+
const roles = inventoryPrompts(context);
|
|
476
|
+
inventoryTools(context, roles);
|
|
477
|
+
inventoryPlugins(context, roles);
|
|
478
|
+
inventoryMcp(context, roles);
|
|
479
|
+
validateRequiredRoles(roles, inventoryTemplates(context));
|
|
480
|
+
inventoryLimits(context);
|
|
481
|
+
return { credential, profile, roles, deadline };
|
|
482
|
+
}
|
|
483
|
+
function modelExecution(context, profile) {
|
|
484
|
+
return {
|
|
485
|
+
backend: profile.backend,
|
|
486
|
+
requested_model: profile.model,
|
|
487
|
+
model_alias_policy: context.input.model_alias_policy,
|
|
488
|
+
provider_protocol: profile.provider,
|
|
489
|
+
configured_route_base_host: context.input.credential.route_identity_host,
|
|
490
|
+
claude_cli_version: context.input.cli_artifact.version,
|
|
491
|
+
cli_name: profile.backend,
|
|
492
|
+
cli_version: context.input.cli_artifact.version,
|
|
493
|
+
reasoning_effort: profile.thinking,
|
|
494
|
+
fallback_empty: true,
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
function modelExecutionHash(value) {
|
|
498
|
+
return computeModelExecutionIdentityHash({
|
|
499
|
+
backend: value.backend,
|
|
500
|
+
requestedModel: value.requested_model,
|
|
501
|
+
modelAliasPolicy: value.model_alias_policy,
|
|
502
|
+
providerProtocol: value.provider_protocol,
|
|
503
|
+
configuredRouteBaseHost: value.configured_route_base_host,
|
|
504
|
+
claudeCliVersion: value.claude_cli_version,
|
|
505
|
+
cliName: value.cli_name,
|
|
506
|
+
cliVersion: value.cli_version,
|
|
507
|
+
reasoningEffort: value.reasoning_effort,
|
|
508
|
+
fallbackEmpty: true,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
function roleSurface(role) {
|
|
512
|
+
const surface = {
|
|
513
|
+
systemPromptSha256: role.systemPromptSha256,
|
|
514
|
+
directiveSha256: role.directiveSha256,
|
|
515
|
+
tools: role.role.tools,
|
|
516
|
+
pluginDirs: role.pluginDirs,
|
|
517
|
+
skills: role.skills,
|
|
518
|
+
mcpComposition: role.role.mcpComposition,
|
|
519
|
+
hookPolicy: {},
|
|
520
|
+
};
|
|
521
|
+
if (role.benchmarkPolicyGuard !== undefined) {
|
|
522
|
+
surface.benchmarkPolicyGuard = role.benchmarkPolicyGuard;
|
|
523
|
+
}
|
|
524
|
+
return surface;
|
|
525
|
+
}
|
|
526
|
+
function identityMaps(roles, modelHash) {
|
|
527
|
+
const model = {};
|
|
528
|
+
const role = {};
|
|
529
|
+
for (const value of roles) {
|
|
530
|
+
model[value.slot] = modelHash;
|
|
531
|
+
role[value.slot] = computeRoleToolSurfaceHash(roleSurface(value));
|
|
532
|
+
}
|
|
533
|
+
return { model, role };
|
|
534
|
+
}
|
|
535
|
+
function assetHashProjection(entries, adapter) {
|
|
536
|
+
return entries.filter(entry => ADAPTER_ASSET_KINDS.has(entry.kind) === adapter).map(entry => ({
|
|
537
|
+
kind: entry.kind,
|
|
538
|
+
logical_name: entry.logical_name,
|
|
539
|
+
content_sha256: entry.content_sha256,
|
|
540
|
+
}));
|
|
541
|
+
}
|
|
542
|
+
function bundleHash(context, identities, modelHash) {
|
|
543
|
+
const roleHash = canonicalJsonSha256(identities.role);
|
|
544
|
+
return computeBundleManifestHash({
|
|
545
|
+
runConfig: context.arm,
|
|
546
|
+
limits: context.arm.limits,
|
|
547
|
+
resolvedPaths: {
|
|
548
|
+
trial_id: context.input.trial_id,
|
|
549
|
+
root_run_id: context.input.root_run_id,
|
|
550
|
+
task: context.input.task,
|
|
551
|
+
},
|
|
552
|
+
adapterHashes: assetHashProjection(context.inventory, true),
|
|
553
|
+
harnessHashes: assetHashProjection(context.inventory, false),
|
|
554
|
+
modelExecutionIdentityHash: modelHash,
|
|
555
|
+
roleToolSurfaceHash: roleHash,
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
function resolvedRoles(roles) {
|
|
559
|
+
return Object.fromEntries(roles.map(role => [role.slot, role.role]));
|
|
560
|
+
}
|
|
561
|
+
// The guard travels on the in-process policy only; a slot without one has no key, never an empty
|
|
562
|
+
// object, so the identity input stays omitted and the guard-less role hash stays byte-identical.
|
|
563
|
+
function rolePolicyGuards(roles) {
|
|
564
|
+
return Object.fromEntries(roles
|
|
565
|
+
.filter(role => role.benchmarkPolicyGuard !== undefined)
|
|
566
|
+
.map(role => [role.slot, role.benchmarkPolicyGuard]));
|
|
567
|
+
}
|
|
568
|
+
function policyIdentity(context, assets) {
|
|
569
|
+
const execution = modelExecution(context, assets.profile);
|
|
570
|
+
const modelHash = modelExecutionHash(execution);
|
|
571
|
+
const identities = identityMaps(assets.roles, modelHash);
|
|
572
|
+
return {
|
|
573
|
+
model_execution: execution,
|
|
574
|
+
identity: {
|
|
575
|
+
model_execution_identity_hash: identities.model,
|
|
576
|
+
role_tool_surface_hash: identities.role,
|
|
577
|
+
bundle_manifest_hash: bundleHash(context, identities, modelHash),
|
|
578
|
+
},
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
function resolvedCredential(context, selected) {
|
|
582
|
+
return {
|
|
583
|
+
capability_key: selected.id,
|
|
584
|
+
capability_state: selected.state,
|
|
585
|
+
...context.input.credential,
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
function policyValue(context, assets) {
|
|
589
|
+
return {
|
|
590
|
+
schema_version: 'cortex-benchmark-resolved-policy/2',
|
|
591
|
+
arm: context.arm,
|
|
592
|
+
arm_canonical_sha256: canonicalJsonSha256(context.arm),
|
|
593
|
+
trial_id: context.input.trial_id,
|
|
594
|
+
root_run_id: context.input.root_run_id,
|
|
595
|
+
task: { ...context.input.task },
|
|
596
|
+
deadline: assets.deadline,
|
|
597
|
+
asset_inventory: context.inventory,
|
|
598
|
+
asset_inventory_sha256: canonicalJsonSha256(context.inventory),
|
|
599
|
+
...policyIdentity(context, assets),
|
|
600
|
+
roles: resolvedRoles(assets.roles),
|
|
601
|
+
role_policy_guard: rolePolicyGuards(assets.roles),
|
|
602
|
+
pi_benchmark_capability_proven: context.input.pi_benchmark_capability_proven === true,
|
|
603
|
+
child_template_whitelist: childTemplateWhitelistForArm(context.arm),
|
|
604
|
+
capability_whitelist: capabilityWhitelistForArm(context.arm),
|
|
605
|
+
credential: resolvedCredential(context, assets.credential),
|
|
606
|
+
limits: context.arm.limits,
|
|
607
|
+
derived: {
|
|
608
|
+
effective_admission_budget: context.arm.limits.max_resident_agent_processes - 1,
|
|
609
|
+
},
|
|
610
|
+
artifact_inventory_spec: context.input.artifact_inventory_spec,
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
function freezePolicy(policy) {
|
|
614
|
+
try {
|
|
615
|
+
return deepFreezePolicy(policy);
|
|
616
|
+
}
|
|
617
|
+
catch (error) {
|
|
618
|
+
return fail('policy_freeze_failed', error.message);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
function compilePolicy(input, deps) {
|
|
622
|
+
const armInput = input !== null && typeof input === 'object' ? input.arm : undefined;
|
|
623
|
+
const arm = parseArmDefinitionForCompiler(armInput);
|
|
624
|
+
validateArmResolutionShape(input);
|
|
625
|
+
validateTrialInput(input);
|
|
626
|
+
const context = { input, deps, arm, inventory: [] };
|
|
627
|
+
appendInventory(context, 'arm', arm.name, input.arm_path, canonicalJsonSha256(arm));
|
|
628
|
+
return freezePolicy(policyValue(context, compileAssets(context)));
|
|
629
|
+
}
|
|
630
|
+
function normalizeError(error) {
|
|
631
|
+
if (error instanceof PolicyCompilationError)
|
|
632
|
+
return error;
|
|
633
|
+
if (error instanceof ArmValidationError) {
|
|
634
|
+
return new PolicyCompilationError(error.reason, error.message);
|
|
635
|
+
}
|
|
636
|
+
throw error;
|
|
637
|
+
}
|
|
638
|
+
export function compileResolvedTrialPolicy(input, deps) {
|
|
639
|
+
try {
|
|
640
|
+
return compilePolicy(input, deps);
|
|
641
|
+
}
|
|
642
|
+
catch (error) {
|
|
643
|
+
const failure = normalizeError(error);
|
|
644
|
+
reportBenchmarkFailure(failure, deps.stderr);
|
|
645
|
+
throw failure;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
export function rejectPolicyReresolution(kind, name, stderr) {
|
|
649
|
+
const error = new PolicyCompilationError('policy_reresolution_attempted', `${kind}:${name}`);
|
|
650
|
+
reportBenchmarkFailure(error, stderr);
|
|
651
|
+
throw error;
|
|
652
|
+
}
|
|
653
|
+
function projectPaths(values, baseDirectory) {
|
|
654
|
+
if (baseDirectory === undefined)
|
|
655
|
+
return [...values];
|
|
656
|
+
return values.map(value => path.relative(baseDirectory, path.resolve(value)));
|
|
657
|
+
}
|
|
658
|
+
export function projectAgentRunConfig(policy, slot, baseDirectory) {
|
|
659
|
+
const role = policy.roles[slot];
|
|
660
|
+
if (!role)
|
|
661
|
+
rejectPolicyReresolution('role', slot);
|
|
662
|
+
return {
|
|
663
|
+
schema_version: 'cortex-agent-run-config/1',
|
|
664
|
+
model_execution: { model_alias_policy: policy.model_execution.model_alias_policy },
|
|
665
|
+
role: {
|
|
666
|
+
system_prompt: role.systemPrompt,
|
|
667
|
+
tools: role.tools,
|
|
668
|
+
plugin_dirs: projectPaths(role.pluginDirs, baseDirectory),
|
|
669
|
+
mcp_composition: role.mcpComposition,
|
|
670
|
+
mcp_config_paths: projectPaths(role.mcpConfigPaths, baseDirectory),
|
|
671
|
+
disable_hooks: true,
|
|
672
|
+
},
|
|
673
|
+
bundle: {
|
|
674
|
+
run_config: policy.arm,
|
|
675
|
+
limits: policy.limits,
|
|
676
|
+
adapter_hashes: assetHashProjection(policy.asset_inventory, true),
|
|
677
|
+
harness_hashes: assetHashProjection(policy.asset_inventory, false),
|
|
678
|
+
},
|
|
679
|
+
};
|
|
680
|
+
}
|