code60-transmission-server 0.1.0 → 0.1.1
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/INTENT.md +47 -0
- package/README.md +87 -240
- package/dist/src/acp.js +130 -0
- package/dist/src/contract.js +133 -0
- package/dist/src/engine.js +650 -0
- package/dist/src/linux.js +84 -0
- package/dist/src/main.js +81 -22
- package/dist/src/repository.js +94 -0
- package/dist/src/service.js +196 -0
- package/package.json +19 -33
- package/reference/openapi.json +1073 -0
- package/README.engine.md +0 -401
- package/dist/src/broker/filesystem.js +0 -385
- package/dist/src/broker/index.js +0 -734
- package/dist/src/broker/native/README.md +0 -192
- package/dist/src/broker/native/build.js +0 -43
- package/dist/src/broker/native/fs-helper.c +0 -529
- package/dist/src/broker/native/git-helper.c +0 -176
- package/dist/src/broker/policy.js +0 -149
- package/dist/src/broker/startup-lock.js +0 -14
- package/dist/src/broker/types.js +0 -11
- package/dist/src/cli/daemon.js +0 -365
- package/dist/src/cli/helpers.js +0 -104
- package/dist/src/cli/main.js +0 -180
- package/dist/src/config/deployment.js +0 -163
- package/dist/src/config/documents.js +0 -29
- package/dist/src/config/profile-validation.js +0 -123
- package/dist/src/config/schema-graph.js +0 -237
- package/dist/src/config/schema-loader.js +0 -185
- package/dist/src/config/validation-pool.js +0 -105
- package/dist/src/config/validation-protocol.js +0 -1
- package/dist/src/config/validation-worker.js +0 -106
- package/dist/src/config/workflow-language.js +0 -9
- package/dist/src/config/workflow-schema.json +0 -534
- package/dist/src/config/workflow-types.js +0 -1
- package/dist/src/config/workflow-validation.js +0 -95
- package/dist/src/engine/budgets.js +0 -50
- package/dist/src/engine/clocks.js +0 -27
- package/dist/src/engine/commands.js +0 -441
- package/dist/src/engine/contracts.js +0 -11
- package/dist/src/engine/drain-state.js +0 -31
- package/dist/src/engine/effective-permissions.js +0 -120
- package/dist/src/engine/instruction-pins.js +0 -205
- package/dist/src/engine/retries.js +0 -50
- package/dist/src/engine/state.js +0 -1
- package/dist/src/engine/timing.js +0 -23
- package/dist/src/engine/tool-observations.js +0 -87
- package/dist/src/engine/workflow-engine.js +0 -1807
- package/dist/src/mcp/catalog.js +0 -71
- package/dist/src/mcp/main.js +0 -24
- package/dist/src/mcp/server.js +0 -103
- package/dist/src/runtime/codex-app-server.js +0 -91
- package/dist/src/runtime/containment.js +0 -82
- package/dist/src/runtime/engine-driver.js +0 -212
- package/dist/src/runtime/json-line-writer.js +0 -192
- package/dist/src/runtime/pinned-instruction-sources.js +0 -24
- package/dist/src/runtime/protocol-client.js +0 -264
- package/dist/src/runtime/protocol-pool.js +0 -183
- package/dist/src/runtime/protocol-projection.js +0 -393
- package/dist/src/runtime/protocol-types.js +0 -1
- package/dist/src/runtime/protocol-worker.js +0 -18
- package/dist/src/runtime/provider-bridge.js +0 -288
- package/dist/src/runtime/provider-credentials.js +0 -41
- package/dist/src/runtime/provider-transport.js +0 -404
- package/dist/src/runtime/runtime-broker.js +0 -213
- package/dist/src/runtime/workspace-instructions.js +0 -120
- package/dist/src/runtime/workspace-view.js +0 -74
- package/dist/src/server/api-contract.json +0 -11078
- package/dist/src/server/api-types.js +0 -1
- package/dist/src/server/application.js +0 -38
- package/dist/src/server/auth.js +0 -67
- package/dist/src/server/broker-handlers.js +0 -242
- package/dist/src/server/control-plane.js +0 -50
- package/dist/src/server/deployment.js +0 -237
- package/dist/src/server/errors.js +0 -22
- package/dist/src/server/history-handlers.js +0 -113
- package/dist/src/server/http.js +0 -124
- package/dist/src/server/paging.js +0 -56
- package/dist/src/server/publication.js +0 -119
- package/dist/src/server/tool-event-handler.js +0 -77
- package/dist/src/server/workspace-handlers.js +0 -34
- package/dist/src/storage/coordinator.js +0 -224
- package/dist/src/storage/startup-lock.js +0 -71
- package/dist/src/storage/store.js +0 -167
- package/dist/src/tools/check-worker.js +0 -81
- package/dist/src/tools/checks.js +0 -106
- package/dist/src/tools/dns.js +0 -248
- package/dist/src/tools/http-contract.js +0 -38
- package/dist/src/tools/http.js +0 -358
- package/dist/src/tools/index.js +0 -177
- package/dist/src/tools/worker-check.js +0 -73
- package/dist/src/workspaces/git.js +0 -163
- package/dist/src/workspaces/index.js +0 -715
- package/examples/deployment.local.json +0 -22
- package/scripts/postinstall.mjs +0 -25
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { isAbsolute } from 'node:path';
|
|
2
|
-
import { closeSync, constants, fstatSync, openSync, readSync } from 'node:fs';
|
|
3
|
-
import { normalizeHost, validatePolicy } from '../broker/policy.js';
|
|
4
|
-
import { HTTP_DEPLOYMENT_SCHEMA } from '../tools/http-contract.js';
|
|
5
|
-
import contract from '../server/api-contract.json' with { type: 'json' };
|
|
6
|
-
import { ApiError } from '../server/errors.js';
|
|
7
|
-
import { numericAddress } from '../tools/dns.js';
|
|
8
|
-
export const LOCAL_RUNTIME_ID = 'codex-local';
|
|
9
|
-
export const LOCAL_PROVIDER_ID = 'code60-local';
|
|
10
|
-
const text = { type: 'string', minLength: 1, maxLength: 200 };
|
|
11
|
-
const object = (properties, required = Object.keys(properties)) => ({ type: 'object', properties, required, additionalProperties: false });
|
|
12
|
-
const permission = { $ref: 'urn:code60:api#/components/schemas/PermissionPolicy' };
|
|
13
|
-
const http = HTTP_DEPLOYMENT_SCHEMA;
|
|
14
|
-
const effort = { enum: ['none', 'minimal', 'low', 'medium', 'high', 'xhigh'] };
|
|
15
|
-
const requiredConfiguration = ['listenPort', 'operator', 'runtime', 'deploymentPolicy', 'agentHttp', 'schemaHttp', 'checks', 'deploymentRoots'];
|
|
16
|
-
const schema = { $id: 'urn:code60:deployment', ...object({
|
|
17
|
-
listenPort: { type: 'integer', minimum: 1, maximum: 65535 },
|
|
18
|
-
operator: object({ id: text, workspaceIds: { type: 'array', minItems: 1, maxItems: 256, uniqueItems: true, items: text } }),
|
|
19
|
-
runtime: object({
|
|
20
|
-
enabled: { type: 'boolean' }, executable: { type: 'string', minLength: 1, maxLength: 4096 },
|
|
21
|
-
modelPort: { type: 'integer', minimum: 1, maximum: 65535 },
|
|
22
|
-
models: { type: 'array', minItems: 1, maxItems: 32, items: object({
|
|
23
|
-
id: text, effortLevels: { type: 'array', maxItems: 6, uniqueItems: true, items: { enum: ['none', 'minimal', 'low', 'medium', 'high', 'xhigh'] } },
|
|
24
|
-
}) },
|
|
25
|
-
}),
|
|
26
|
-
deploymentPolicy: permission, agentHttp: HTTP_DEPLOYMENT_SCHEMA,
|
|
27
|
-
schemaHttp: object({ ...http.properties, timeoutMs: { type: 'integer', minimum: 1, maximum: Number.MAX_SAFE_INTEGER },
|
|
28
|
-
networkPolicy: permission }, ['networkPolicy']),
|
|
29
|
-
checks: { type: 'array', maxItems: 64, items: object({
|
|
30
|
-
id: text, description: { type: 'string', minLength: 1, maxLength: 4096 }, version: text,
|
|
31
|
-
predicate: { oneOf: [
|
|
32
|
-
object({ kind: { const: 'text_includes' }, needle: { type: 'string', minLength: 1, maxLength: 4096 } }),
|
|
33
|
-
object({ kind: { const: 'json_pointer_equals' }, pointer: { type: 'string', maxLength: 4096 }, expected: {} }),
|
|
34
|
-
object({ kind: { const: 'json_schema' }, schema: { type: ['object', 'boolean'] } }),
|
|
35
|
-
] },
|
|
36
|
-
}) },
|
|
37
|
-
deploymentRoots: { type: 'array', maxItems: 8, items: object({ id: text, path: { type: 'string', minLength: 1, maxLength: 4096 } }) },
|
|
38
|
-
providers: { type: 'array', maxItems: 16, items: object({
|
|
39
|
-
id: text, endpoint: { type: 'string', minLength: 1, maxLength: 4096 }, networkPolicy: permission,
|
|
40
|
-
dnsResolvers: http.properties.dnsResolvers,
|
|
41
|
-
timeoutMs: { type: 'integer', minimum: 1, maximum: 2_147_483_647 },
|
|
42
|
-
maxConcurrent: { type: 'integer', minimum: 1, maximum: 16 },
|
|
43
|
-
maxRequestBytes: { type: 'integer', minimum: 1, maximum: 1_073_741_824 },
|
|
44
|
-
models: { type: 'array', minItems: 1, maxItems: 32, items: object({ id: text,
|
|
45
|
-
effortLevels: { type: 'array', minItems: 1, maxItems: 6, uniqueItems: true, items: effort }, defaultEffort: effort }) },
|
|
46
|
-
}, ['id', 'endpoint', 'networkPolicy', 'maxRequestBytes', 'models']) },
|
|
47
|
-
providerCredentials: { type: 'array', maxItems: 128, items: object({
|
|
48
|
-
referenceId: text, version: text, origin: { type: 'string', minLength: 1, maxLength: 4096 }, providerId: text,
|
|
49
|
-
file: { type: 'string', minLength: 1, maxLength: 4096 },
|
|
50
|
-
}) },
|
|
51
|
-
}, requiredConfiguration) };
|
|
52
|
-
/** Explicit administrator file only. Never follow a symlink, read a home
|
|
53
|
-
* default, accept a hardlink, or allocate from an unchecked file size. */
|
|
54
|
-
export function readPrivateFile(path, maximum) {
|
|
55
|
-
if (!isAbsolute(path) || /[\0\r\n]/.test(path))
|
|
56
|
-
throw new ApiError(422, 'invalid_deployment_path', 'Deployment paths must be explicit and absolute.');
|
|
57
|
-
const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
58
|
-
try {
|
|
59
|
-
const before = fstatSync(fd, { bigint: true });
|
|
60
|
-
if (!before.isFile() || before.nlink !== 1n || before.uid !== BigInt(process.geteuid()) ||
|
|
61
|
-
before.mode & 63n || before.size > BigInt(maximum))
|
|
62
|
-
throw new ApiError(422, 'unsafe_deployment_file', 'Deployment files must be private, owned, bounded regular files.');
|
|
63
|
-
const bytes = Buffer.alloc(maximum + 1);
|
|
64
|
-
let used = 0;
|
|
65
|
-
while (used < bytes.length) {
|
|
66
|
-
const n = readSync(fd, bytes, used, bytes.length - used, null);
|
|
67
|
-
if (!n)
|
|
68
|
-
break;
|
|
69
|
-
used += n;
|
|
70
|
-
}
|
|
71
|
-
const after = fstatSync(fd, { bigint: true });
|
|
72
|
-
if (used > maximum || BigInt(used) !== before.size || after.size !== before.size ||
|
|
73
|
-
after.mtimeNs !== before.mtimeNs || after.ctimeNs !== before.ctimeNs)
|
|
74
|
-
throw new ApiError(422, 'deployment_file_changed', 'Deployment file changed while reading.');
|
|
75
|
-
return bytes.subarray(0, used);
|
|
76
|
-
}
|
|
77
|
-
finally {
|
|
78
|
-
closeSync(fd);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
export function readOperatorToken(path) {
|
|
82
|
-
const token = new TextDecoder('utf-8', { fatal: true }).decode(readPrivateFile(path, 1024)).trim();
|
|
83
|
-
if (!/^[A-Za-z0-9_-]{32,512}$/.test(token))
|
|
84
|
-
throw new ApiError(422, 'invalid_operator_token', 'Operator token must be a private URL-safe token of at least 32 characters.');
|
|
85
|
-
return token;
|
|
86
|
-
}
|
|
87
|
-
export async function loadDeploymentConfiguration(path, validation) {
|
|
88
|
-
validation.coordinator.assertOutsideTransaction();
|
|
89
|
-
const bytes = readPrivateFile(path, 262_144);
|
|
90
|
-
const decoded = await validation.run({ kind: 'decode', format: 'json',
|
|
91
|
-
content: new TextDecoder('utf-8', { fatal: true }).decode(bytes) });
|
|
92
|
-
if (!decoded.valid)
|
|
93
|
-
throw new ApiError(422, 'invalid_deployment_configuration', 'Deployment configuration must be strict JSON.');
|
|
94
|
-
const checked = await validation.run({ kind: 'artifact', rootUri: 'urn:code60:deployment',
|
|
95
|
-
resources: [{ uri: 'urn:code60:api', schema: { ...contract, $id: 'urn:code60:api' } },
|
|
96
|
-
{ uri: 'urn:code60:deployment', schema }], value: decoded.value });
|
|
97
|
-
if (!checked.valid)
|
|
98
|
-
throw new ApiError(422, 'invalid_deployment_configuration', 'Deployment configuration is invalid.');
|
|
99
|
-
const config = decoded.value;
|
|
100
|
-
validatePolicy(config.deploymentPolicy);
|
|
101
|
-
validatePolicy(config.schemaHttp.networkPolicy);
|
|
102
|
-
if (!isAbsolute(config.runtime.executable) || /[\0\r\n]/.test(config.runtime.executable) ||
|
|
103
|
-
new Set(config.runtime.models.map(model => model.id)).size !== config.runtime.models.length ||
|
|
104
|
-
new Set(config.checks.map(check => check.id)).size !== config.checks.length ||
|
|
105
|
-
new Set(config.deploymentRoots.map(root => root.id)).size !== config.deploymentRoots.length ||
|
|
106
|
-
config.deploymentRoots.some(root => !isAbsolute(root.path) || /[\0\r\n]/.test(root.path)) ||
|
|
107
|
-
config.schemaHttp.networkPolicy.rules.some(rule => rule.kind !== 'network'))
|
|
108
|
-
throw new ApiError(422, 'invalid_deployment_configuration', 'Deployment selections must be distinct, explicit and supported.');
|
|
109
|
-
try {
|
|
110
|
-
const providers = config.providers ?? [], credentials = config.providerCredentials ?? [];
|
|
111
|
-
if (new Set(providers.map(provider => provider.id)).size !== providers.length ||
|
|
112
|
-
new Set(credentials.map(credential => credential.referenceId)).size !== credentials.length)
|
|
113
|
-
throw new Error();
|
|
114
|
-
for (const provider of providers) {
|
|
115
|
-
const endpoint = new URL(provider.endpoint);
|
|
116
|
-
const authority = /^https:\/\/([^/]+)/iu.exec(provider.endpoint)?.[1];
|
|
117
|
-
const rawHost = authority?.startsWith('[') ? authority.slice(0, authority.indexOf(']') + 1) : authority?.split(':')[0];
|
|
118
|
-
if (provider.id === LOCAL_PROVIDER_ID || endpoint.protocol !== 'https:' || endpoint.username || endpoint.password ||
|
|
119
|
-
/[\u0000-\u0020\u007f\\?#]/u.test(provider.endpoint) || !endpoint.pathname.endsWith('/responses') ||
|
|
120
|
-
!rawHost || normalizeHost(rawHost) !== normalizeHost(endpoint.hostname))
|
|
121
|
-
throw new Error();
|
|
122
|
-
validatePolicy(provider.networkPolicy);
|
|
123
|
-
if (provider.networkPolicy.defaultEffect !== 'deny' || provider.networkPolicy.rules.some(rule => rule.kind !== 'network') ||
|
|
124
|
-
new Set(provider.models.map(model => model.id)).size !== provider.models.length ||
|
|
125
|
-
provider.models.some(model => !model.effortLevels.includes(model.defaultEffort)))
|
|
126
|
-
throw new Error();
|
|
127
|
-
for (const resolver of provider.dnsResolvers ?? [])
|
|
128
|
-
numericAddress(resolver.address);
|
|
129
|
-
if (!credentials.some(credential => credential.providerId === provider.id && credential.origin === endpoint.origin))
|
|
130
|
-
throw new Error();
|
|
131
|
-
}
|
|
132
|
-
for (const credential of credentials) {
|
|
133
|
-
const provider = providers.find(provider => provider.id === credential.providerId);
|
|
134
|
-
if (!provider || credential.origin !== new URL(provider.endpoint).origin || !isAbsolute(credential.file) || /[\0\r\n]/.test(credential.file))
|
|
135
|
-
throw new Error();
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
catch {
|
|
139
|
-
throw new ApiError(422, 'invalid_deployment_configuration', 'Provider selections require explicit supported endpoints and credential grants.');
|
|
140
|
-
}
|
|
141
|
-
return config;
|
|
142
|
-
}
|
|
143
|
-
export function runtimeAdvertisement(config, tools) {
|
|
144
|
-
const toolConfigurations = tools.toolConfigurations().map(tool => {
|
|
145
|
-
const configurationSchema = tool.configurationSchema;
|
|
146
|
-
if (typeof configurationSchema !== 'boolean' && (!configurationSchema || typeof configurationSchema !== 'object' || Array.isArray(configurationSchema)))
|
|
147
|
-
throw new Error('invalid_tool_configuration_schema');
|
|
148
|
-
return { ...tool, configurationSchema };
|
|
149
|
-
});
|
|
150
|
-
return {
|
|
151
|
-
runtime: { id: LOCAL_RUNTIME_ID, name: 'Contained local Codex app-server', protocol: 'acp',
|
|
152
|
-
available: config.runtime.enabled, authenticationState: config.runtime.enabled ? 'ready' : 'requires_configuration' },
|
|
153
|
-
configuration: { runtimeId: LOCAL_RUNTIME_ID,
|
|
154
|
-
providers: [{ id: LOCAL_PROVIDER_ID, models: structuredClone(config.runtime.models) },
|
|
155
|
-
...(config.providers ?? []).map(provider => ({ id: provider.id, models: provider.models.map(({ id, effortLevels }) => ({ id, effortLevels: [...effortLevels] })) }))],
|
|
156
|
-
capabilities: { actionInterception: true, filesystemScopes: true, networkScopes: true, subprocessRestrictions: true,
|
|
157
|
-
explicitInstructionSources: true, singleUseDispatch: true, toolHistory: true, quiesceWorker: true,
|
|
158
|
-
projectReadOnlyEnforcement: true, scratchWrites: true },
|
|
159
|
-
runtimeOptionsSchema: { type: 'object', properties: { artifactFormat: { enum: ['json', 'yaml'] } }, additionalProperties: false },
|
|
160
|
-
credentialSlots: config.providers?.length ? ['provider'] : [], toolConfigurations: structuredClone(toolConfigurations),
|
|
161
|
-
},
|
|
162
|
-
};
|
|
163
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import YAML from 'yaml';
|
|
2
|
-
import { canonical } from '../broker/policy.js';
|
|
3
|
-
/** This parser is deliberately imported only by the bounded pure worker. */
|
|
4
|
-
export function decodeDocument(content, format) {
|
|
5
|
-
if (format === 'json')
|
|
6
|
-
JSON.parse(content); // JSON grammar, not YAML's superset.
|
|
7
|
-
const document = YAML.parseDocument(content, { strict: true, uniqueKeys: true, merge: false, version: '1.2', prettyErrors: false });
|
|
8
|
-
if (document.errors.length || document.warnings.length)
|
|
9
|
-
throw new Error('invalid_document');
|
|
10
|
-
YAML.visit(document, {
|
|
11
|
-
Map(_key, map) {
|
|
12
|
-
const keys = new Set();
|
|
13
|
-
for (const pair of map.items) {
|
|
14
|
-
const key = YAML.isAlias(pair.key) ? pair.key.resolve(document) : pair.key;
|
|
15
|
-
if (!YAML.isScalar(key) || typeof key.value !== 'string')
|
|
16
|
-
throw new Error('non_string_key');
|
|
17
|
-
// YAML's built-in uniqueKeys does not compare resolved alias keys.
|
|
18
|
-
if (keys.has(key.value))
|
|
19
|
-
throw new Error('duplicate_mapping_key');
|
|
20
|
-
keys.add(key.value);
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
// The worker's heap/time limit bounds alias expansion; cycles/nonfinite or
|
|
25
|
-
// non-JSON values are rejected, never coerced into another document.
|
|
26
|
-
const value = document.toJS({ mapAsMap: false, maxAliasCount: -1 });
|
|
27
|
-
canonical(value);
|
|
28
|
-
return value;
|
|
29
|
-
}
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
function selfContainedSynchronousSchema(schema) {
|
|
2
|
-
if (typeof schema === 'boolean')
|
|
3
|
-
return true;
|
|
4
|
-
if (!schema || typeof schema !== 'object' || Array.isArray(schema))
|
|
5
|
-
return false;
|
|
6
|
-
const node = schema;
|
|
7
|
-
if (node.$async === true)
|
|
8
|
-
return false;
|
|
9
|
-
for (const keyword of ['$ref', '$dynamicRef'])
|
|
10
|
-
if (typeof node[keyword] === 'string' && !node[keyword].startsWith('#'))
|
|
11
|
-
return false;
|
|
12
|
-
for (const keyword of ['additionalProperties', 'unevaluatedProperties', 'propertyNames', 'contains', 'items',
|
|
13
|
-
'unevaluatedItems', 'if', 'then', 'else', 'not', 'contentSchema']) {
|
|
14
|
-
if (node[keyword] !== undefined && !selfContainedSynchronousSchema(node[keyword]))
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
for (const keyword of ['allOf', 'anyOf', 'oneOf', 'prefixItems']) {
|
|
18
|
-
const children = node[keyword];
|
|
19
|
-
if (Array.isArray(children) && children.some(child => !selfContainedSynchronousSchema(child)))
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
for (const keyword of ['properties', 'patternProperties', 'dependentSchemas', '$defs', 'definitions']) {
|
|
23
|
-
const children = node[keyword];
|
|
24
|
-
if (children && typeof children === 'object' && !Array.isArray(children) &&
|
|
25
|
-
Object.values(children).some(child => !selfContainedSynchronousSchema(child)))
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
export function validateProfile(input, runtimes, credentialIds, ajv) {
|
|
31
|
-
const issues = [];
|
|
32
|
-
const add = (path, code, message) => issues.push({ severity: 'error', path, code, message });
|
|
33
|
-
const definition = input.definition;
|
|
34
|
-
const resources = new Map();
|
|
35
|
-
input.resources.forEach((resource, index) => {
|
|
36
|
-
if (resources.has(resource.path))
|
|
37
|
-
add(`/resources/${index}/path`, 'duplicate_path', 'Instruction resource paths must be unique.');
|
|
38
|
-
resources.set(resource.path, resource.content);
|
|
39
|
-
});
|
|
40
|
-
const unresolvedWorkspaceSources = [];
|
|
41
|
-
definition.instructions.forEach((source, index) => {
|
|
42
|
-
if (source.kind !== 'file')
|
|
43
|
-
return;
|
|
44
|
-
if (source.location === 'workspace')
|
|
45
|
-
unresolvedWorkspaceSources.push(source.path);
|
|
46
|
-
else if (!resources.has(source.path))
|
|
47
|
-
add(`/definition/instructions/${index}`, 'missing_package_source', 'Packaged instruction source is missing.');
|
|
48
|
-
});
|
|
49
|
-
const advertisement = runtimes.find(entry => entry.runtime.id === definition.runtime.runtimeId);
|
|
50
|
-
if (!advertisement || !advertisement.runtime.available || advertisement.runtime.authenticationState !== 'ready') {
|
|
51
|
-
add('/definition/runtime/runtimeId', 'runtime_unavailable', 'The selected runtime is unavailable or requires configuration.');
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
const configuration = advertisement.configuration;
|
|
55
|
-
const provider = configuration.providers.find(entry => entry.id === definition.runtime.provider);
|
|
56
|
-
const model = provider?.models.find(entry => entry.id === definition.runtime.model);
|
|
57
|
-
if (!provider)
|
|
58
|
-
add('/definition/runtime/provider', 'unknown_provider', 'Provider is not advertised.');
|
|
59
|
-
if (!model)
|
|
60
|
-
add('/definition/runtime/model', 'unknown_model', 'Model is not advertised for this provider.');
|
|
61
|
-
if (definition.runtime.effort !== undefined && !model?.effortLevels.includes(definition.runtime.effort))
|
|
62
|
-
add('/definition/runtime/effort', 'unsupported_effort', 'Effort is not advertised for this model.');
|
|
63
|
-
const required = [
|
|
64
|
-
'actionInterception', 'filesystemScopes', 'networkScopes', 'subprocessRestrictions',
|
|
65
|
-
'explicitInstructionSources', 'singleUseDispatch', 'toolHistory', 'quiesceWorker',
|
|
66
|
-
];
|
|
67
|
-
if (definition.role === 'verifier')
|
|
68
|
-
required.push('projectReadOnlyEnforcement');
|
|
69
|
-
if (definition.permissions.rules.some(rule => rule.kind === 'filesystem' && rule.root === 'scratch' && rule.effect !== 'deny'))
|
|
70
|
-
required.push('scratchWrites');
|
|
71
|
-
for (const capability of required) {
|
|
72
|
-
if (!configuration.capabilities[capability])
|
|
73
|
-
add('/definition/runtime', 'unsupported_capability', `The runtime cannot enforce ${capability}.`);
|
|
74
|
-
}
|
|
75
|
-
const validateOptions = (schema, value, path) => {
|
|
76
|
-
if (!selfContainedSynchronousSchema(schema)) {
|
|
77
|
-
add(path, 'invalid_advertisement', 'Advertised option schemas must be self-contained and synchronous.');
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
try {
|
|
81
|
-
const validate = ajv.compile(schema);
|
|
82
|
-
if ('$async' in validate) {
|
|
83
|
-
add(path, 'invalid_advertisement', 'Asynchronous advertised option schemas are unsupported.');
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
if (!validate(value))
|
|
87
|
-
add(path, 'unsupported_options', 'Configuration does not match its advertised schema.');
|
|
88
|
-
}
|
|
89
|
-
catch {
|
|
90
|
-
add(path, 'invalid_advertisement', 'The advertised configuration schema is invalid.');
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
const validateCredentials = (refs, slots, path) => {
|
|
94
|
-
for (const [slot, id] of Object.entries(refs ?? {})) {
|
|
95
|
-
if (!slots.includes(slot))
|
|
96
|
-
add(`${path}/${slot}`, 'unknown_credential_slot', 'Credential slot is not advertised.');
|
|
97
|
-
if (!credentialIds.includes(id))
|
|
98
|
-
add(`${path}/${slot}`, 'unauthorized_credential', 'Credential reference is unavailable.');
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
validateOptions(configuration.runtimeOptionsSchema, definition.runtime.options ?? {}, '/definition/runtime/options');
|
|
102
|
-
validateCredentials(definition.runtime.credentialRefs, configuration.credentialSlots, '/definition/runtime/credentialRefs');
|
|
103
|
-
const toolIds = new Set();
|
|
104
|
-
definition.tools.forEach((tool, index) => {
|
|
105
|
-
if (toolIds.has(tool.toolId))
|
|
106
|
-
add(`/definition/tools/${index}/toolId`, 'duplicate_tool', 'Tool IDs must be unique.');
|
|
107
|
-
toolIds.add(tool.toolId);
|
|
108
|
-
const registered = configuration.toolConfigurations.find(entry => entry.toolId === tool.toolId);
|
|
109
|
-
if (!registered) {
|
|
110
|
-
add(`/definition/tools/${index}/toolId`, 'unknown_tool', 'Tool is not advertised.');
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
validateOptions(registered.configurationSchema, tool.configuration, `/definition/tools/${index}/configuration`);
|
|
114
|
-
validateCredentials(tool.credentialRefs, registered.credentialSlots, `/definition/tools/${index}/credentialRefs`);
|
|
115
|
-
});
|
|
116
|
-
definition.permissions.rules.forEach((rule, index) => {
|
|
117
|
-
if (rule.kind === 'tool' && rule.toolId !== '*' && !configuration.toolConfigurations.some(tool => tool.toolId === rule.toolId))
|
|
118
|
-
add(`/definition/permissions/rules/${index}/toolId`, 'unknown_tool', 'Permission refers to an unregistered tool.');
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
return { valid: !issues.length, issues, checked: ['structural', 'package_sources', 'runtime_capabilities'],
|
|
122
|
-
unresolvedWorkspaceSources: [...new Set(unresolvedWorkspaceSources)] };
|
|
123
|
-
}
|
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
import { canonical } from '../broker/policy.js';
|
|
2
|
-
import { decodeDocument } from './documents.js';
|
|
3
|
-
const single = ['additionalProperties', 'unevaluatedProperties', 'propertyNames', 'contains', 'items', 'unevaluatedItems', 'if', 'then', 'else', 'not', 'contentSchema'];
|
|
4
|
-
const arrays = ['allOf', 'anyOf', 'oneOf', 'prefixItems'];
|
|
5
|
-
const maps = ['properties', 'patternProperties', 'dependentSchemas', 'dependencies'];
|
|
6
|
-
function object(value) { return value !== null && typeof value === 'object' && !Array.isArray(value); }
|
|
7
|
-
function schema(value) { return typeof value === 'boolean' || object(value); }
|
|
8
|
-
function children(node, includeDefinitions) {
|
|
9
|
-
const values = [];
|
|
10
|
-
for (const key of single)
|
|
11
|
-
if (schema(node[key]))
|
|
12
|
-
values.push(node[key]);
|
|
13
|
-
for (const key of arrays)
|
|
14
|
-
if (Array.isArray(node[key]))
|
|
15
|
-
for (const item of node[key])
|
|
16
|
-
if (schema(item))
|
|
17
|
-
values.push(item);
|
|
18
|
-
for (const key of includeDefinitions ? [...maps, '$defs', 'definitions'] : maps)
|
|
19
|
-
if (object(node[key]))
|
|
20
|
-
for (const item of Object.values(node[key]))
|
|
21
|
-
if (schema(item))
|
|
22
|
-
values.push(item);
|
|
23
|
-
return values;
|
|
24
|
-
}
|
|
25
|
-
const packageOrigin = 'https://code60.invalid';
|
|
26
|
-
const packageRoot = `${packageOrigin}/package/`;
|
|
27
|
-
export function packageSchemaUri(path) {
|
|
28
|
-
if (!path || path.includes('\\') || path.includes('\0') || /^[A-Za-z][A-Za-z0-9+.-]*:/.test(path) ||
|
|
29
|
-
path.split('/').some(part => !part || part === '.' || part === '..'))
|
|
30
|
-
throw new Error('invalid_package_path');
|
|
31
|
-
return packageRoot + path.split('/').map(encodeURIComponent).join('/');
|
|
32
|
-
}
|
|
33
|
-
export function buildSchemaGraph(definition, source, remote = []) {
|
|
34
|
-
const errors = [];
|
|
35
|
-
const unresolved = new Set();
|
|
36
|
-
const resources = [];
|
|
37
|
-
const documents = new Map();
|
|
38
|
-
const addresses = new Map();
|
|
39
|
-
const bases = new WeakMap();
|
|
40
|
-
const visited = new WeakSet();
|
|
41
|
-
const rootUri = `${packageRoot}workflow.${source.source.format === 'yaml' ? 'yaml' : 'json'}`;
|
|
42
|
-
const fail = (code, message) => errors.push({ path: '/schemas', code, message });
|
|
43
|
-
for (const resource of source.resources ?? []) {
|
|
44
|
-
try {
|
|
45
|
-
const uri = packageSchemaUri(resource.path);
|
|
46
|
-
if (documents.has(uri))
|
|
47
|
-
fail('duplicate_path', 'Schema resource paths must be unique.');
|
|
48
|
-
documents.set(uri, resource.document);
|
|
49
|
-
}
|
|
50
|
-
catch {
|
|
51
|
-
fail('invalid_package_path', 'Schema resource paths must be normalized package-relative paths.');
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
for (const resource of remote) {
|
|
55
|
-
const parsed = new URL(resource.uri);
|
|
56
|
-
parsed.hash = '';
|
|
57
|
-
if (documents.has(parsed.href))
|
|
58
|
-
fail('duplicate_schema_resource', 'Schema retrieval identities must be unique.');
|
|
59
|
-
documents.set(parsed.href, resource.document);
|
|
60
|
-
}
|
|
61
|
-
const rewrite = (input) => {
|
|
62
|
-
const value = JSON.parse(canonical(input));
|
|
63
|
-
const walk = (node) => {
|
|
64
|
-
if (!object(node))
|
|
65
|
-
return;
|
|
66
|
-
// $async is an Ajv extension, not a Draft2020 assertion. Preserve raw
|
|
67
|
-
// source elsewhere; it must not alter ordinary artifact-schema behavior.
|
|
68
|
-
delete node.$async;
|
|
69
|
-
for (const key of ['$ref', '$dynamicRef']) {
|
|
70
|
-
const ref = node[key];
|
|
71
|
-
if (typeof ref === 'string' && ref.startsWith('#/types/'))
|
|
72
|
-
node[key] = rootUri + ref;
|
|
73
|
-
}
|
|
74
|
-
for (const child of children(node, true))
|
|
75
|
-
walk(child);
|
|
76
|
-
};
|
|
77
|
-
walk(value);
|
|
78
|
-
return value;
|
|
79
|
-
};
|
|
80
|
-
const identify = (node, base) => {
|
|
81
|
-
if (!object(node))
|
|
82
|
-
return;
|
|
83
|
-
if (typeof node.$id === 'string') {
|
|
84
|
-
const id = new URL(node.$id, base);
|
|
85
|
-
if (id.hash) {
|
|
86
|
-
fail('invalid_schema_identity', 'Schema resource IDs cannot contain nonempty fragments.');
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
base = id.href;
|
|
90
|
-
const prior = addresses.get(base);
|
|
91
|
-
if (prior !== undefined && prior !== node) {
|
|
92
|
-
fail('ambiguous_schema_identity', 'Different schemas declare the same resource identity.');
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
addresses.set(base, node);
|
|
96
|
-
}
|
|
97
|
-
bases.set(node, base);
|
|
98
|
-
for (const keyword of ['$anchor', '$dynamicAnchor'])
|
|
99
|
-
if (typeof node[keyword] === 'string')
|
|
100
|
-
addresses.set(`${base}#${node[keyword]}`, node);
|
|
101
|
-
for (const child of children(node, true))
|
|
102
|
-
identify(child, base);
|
|
103
|
-
};
|
|
104
|
-
const register = (uri, input) => {
|
|
105
|
-
const value = rewrite(input);
|
|
106
|
-
if (!schema(value)) {
|
|
107
|
-
fail('invalid_schema', 'Schema documents and selected definitions must be objects or booleans.');
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
addresses.set(uri, value);
|
|
111
|
-
resources.push({ uri, schema: value });
|
|
112
|
-
identify(value, uri);
|
|
113
|
-
return value;
|
|
114
|
-
};
|
|
115
|
-
const typeMap = {};
|
|
116
|
-
for (const [name, value] of Object.entries(definition.types ?? {})) {
|
|
117
|
-
const uri = `${rootUri}?type=${encodeURIComponent(name)}`;
|
|
118
|
-
typeMap[name] = { $ref: uri };
|
|
119
|
-
register(uri, value);
|
|
120
|
-
}
|
|
121
|
-
const types = register(rootUri, { types: typeMap });
|
|
122
|
-
if (object(types) && object(types.types))
|
|
123
|
-
for (const value of Object.values(types.types))
|
|
124
|
-
if (schema(value))
|
|
125
|
-
identify(value, rootUri);
|
|
126
|
-
const load = (uri) => {
|
|
127
|
-
const present = addresses.get(uri);
|
|
128
|
-
if (present !== undefined)
|
|
129
|
-
return present;
|
|
130
|
-
const document = documents.get(uri);
|
|
131
|
-
if (document) {
|
|
132
|
-
try {
|
|
133
|
-
return register(uri, decodeDocument(document.content, document.mediaType === 'application/json' ? 'json' : 'yaml'));
|
|
134
|
-
}
|
|
135
|
-
catch {
|
|
136
|
-
fail('invalid_schema_document', 'A required schema document is not valid JSON/YAML.');
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
const parsed = new URL(uri);
|
|
141
|
-
if ((parsed.protocol === 'https:' || parsed.protocol === 'http:') && parsed.origin !== packageOrigin && !parsed.username && !parsed.password)
|
|
142
|
-
unresolved.add(uri);
|
|
143
|
-
else
|
|
144
|
-
fail('unresolved_reference', 'A required local schema reference is missing or outside its package.');
|
|
145
|
-
return undefined;
|
|
146
|
-
};
|
|
147
|
-
const select = (reference, base) => {
|
|
148
|
-
let uri;
|
|
149
|
-
try {
|
|
150
|
-
uri = new URL(reference, base);
|
|
151
|
-
}
|
|
152
|
-
catch {
|
|
153
|
-
fail('invalid_reference', 'Schema reference is not a valid URI reference.');
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
const exact = addresses.get(uri.href);
|
|
157
|
-
if (exact !== undefined)
|
|
158
|
-
return exact;
|
|
159
|
-
const fragment = uri.hash.slice(1);
|
|
160
|
-
uri.hash = '';
|
|
161
|
-
const root = load(uri.href);
|
|
162
|
-
if (root === undefined)
|
|
163
|
-
return;
|
|
164
|
-
if (!fragment)
|
|
165
|
-
return root;
|
|
166
|
-
const anchor = addresses.get(`${uri.href}#${fragment}`);
|
|
167
|
-
if (anchor !== undefined)
|
|
168
|
-
return anchor;
|
|
169
|
-
let pointer;
|
|
170
|
-
try {
|
|
171
|
-
pointer = decodeURIComponent(fragment);
|
|
172
|
-
}
|
|
173
|
-
catch {
|
|
174
|
-
fail('invalid_fragment', 'Schema fragment has invalid percent encoding.');
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
if (!pointer.startsWith('/') || /~(?![01])/u.test(pointer)) {
|
|
178
|
-
fail('invalid_fragment', 'Schema fragment must identify a known anchor or valid JSON Pointer.');
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
let value = root;
|
|
182
|
-
for (const part of pointer.slice(1).split('/')) {
|
|
183
|
-
const key = part.replaceAll('~1', '/').replaceAll('~0', '~');
|
|
184
|
-
if (Array.isArray(value)) {
|
|
185
|
-
if (!/^(0|[1-9][0-9]*)$/.test(key) || Number(key) >= value.length) {
|
|
186
|
-
fail('unresolved_fragment', 'Schema JSON Pointer does not exist.');
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
value = value[Number(key)];
|
|
190
|
-
}
|
|
191
|
-
else if (object(value) && Object.hasOwn(value, key))
|
|
192
|
-
value = value[key];
|
|
193
|
-
else {
|
|
194
|
-
fail('unresolved_fragment', 'Schema JSON Pointer does not exist.');
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
if (!schema(value)) {
|
|
199
|
-
fail('invalid_schema', 'Selected schema definitions must be objects or booleans.');
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
if (object(value) && !bases.has(value))
|
|
203
|
-
identify(value, uri.href);
|
|
204
|
-
return value;
|
|
205
|
-
};
|
|
206
|
-
const visit = (node) => {
|
|
207
|
-
if (typeof node === 'boolean' || visited.has(node))
|
|
208
|
-
return;
|
|
209
|
-
visited.add(node);
|
|
210
|
-
const base = bases.get(node);
|
|
211
|
-
for (const keyword of ['$ref', '$dynamicRef']) {
|
|
212
|
-
const reference = node[keyword];
|
|
213
|
-
if (typeof reference === 'string') {
|
|
214
|
-
const target = select(reference, base);
|
|
215
|
-
if (target !== undefined)
|
|
216
|
-
visit(target);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
for (const child of children(node, false))
|
|
220
|
-
visit(child);
|
|
221
|
-
};
|
|
222
|
-
const roots = [];
|
|
223
|
-
definition.steps.forEach((step, index) => {
|
|
224
|
-
for (const kind of ['exit', 'handoff']) {
|
|
225
|
-
const input = kind === 'exit' ? step.exitArtifact.schema : step.handoffArtifact?.schema;
|
|
226
|
-
if (input === undefined)
|
|
227
|
-
continue;
|
|
228
|
-
const uri = `${rootUri}?step=${index}&kind=${kind}`;
|
|
229
|
-
const registered = register(uri, input);
|
|
230
|
-
if (registered !== undefined) {
|
|
231
|
-
roots.push({ stepId: step.id, kind, uri });
|
|
232
|
-
visit(registered);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
return { roots, resources, unresolvedRemote: [...unresolved].sort(), errors };
|
|
237
|
-
}
|