pi-better-server 0.3.0 → 0.4.0
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/package.json +4 -1
- package/scripts/measure-shared-reads.mjs +25 -5
- package/scripts/test-on-pi.sh +3 -2
- package/src/daemon-owner.ts +2 -1
- package/src/handshake.ts +1 -1
- package/src/protocol.ts +2 -0
- package/src/runtime/background.ts +12 -3
- package/src/runtime/cancellation.ts +10 -6
- package/src/runtime/catalog-admission.ts +50 -0
- package/src/runtime/catalog.ts +133 -0
- package/src/runtime/domain/assignment-observation.d.mts +1 -1
- package/src/runtime/domain/assignment-observation.mjs +18 -1
- package/src/runtime/domain/catalog/agent-session-settings.d.mts +30 -0
- package/src/runtime/domain/catalog/agent-session-settings.mjs +112 -0
- package/src/runtime/domain/catalog/catalog-overlay.d.mts +3 -0
- package/src/runtime/domain/catalog/catalog-overlay.mjs +9 -0
- package/src/runtime/domain/catalog/catalog-preferences.d.mts +17 -0
- package/src/runtime/domain/catalog/catalog-preferences.mjs +164 -0
- package/src/runtime/domain/catalog/catalog-schema.d.mts +265 -0
- package/src/runtime/domain/catalog/catalog-schema.mjs +1331 -0
- package/src/runtime/domain/catalog/catalog-skill-edits.d.mts +42 -0
- package/src/runtime/domain/catalog/catalog-skill-edits.mjs +95 -0
- package/src/runtime/domain/catalog/catalog-store.d.mts +126 -0
- package/src/runtime/domain/catalog/catalog-store.mjs +810 -0
- package/src/runtime/domain/catalog/roles/role.architect.md +13 -0
- package/src/runtime/domain/catalog/roles/role.developer.md +16 -0
- package/src/runtime/domain/catalog/roles/role.explorer.md +13 -0
- package/src/runtime/domain/catalog/roles/role.product-manager.md +13 -0
- package/src/runtime/domain/catalog/roles/role.researcher.md +13 -0
- package/src/runtime/domain/catalog/roles/role.reviewer.md +13 -0
- package/src/runtime/domain/catalog/roles/role.writer.md +12 -0
- package/src/runtime/domain/catalog/thinking.d.mts +8 -0
- package/src/runtime/domain/catalog/thinking.mjs +22 -0
- package/src/runtime/domain/coordinator/src/discovery.mjs +9 -0
- package/src/runtime/domain/coordinator/src/operations.mjs +5 -3
- package/src/runtime/domain/coordinator/src/result-supervision.mjs +101 -0
- package/src/runtime/domain/coordinator/src/schema.mjs +17 -1
- package/src/runtime/domain/coordinator/src/store.mjs +11 -1
- package/src/runtime/mutation-store.ts +34 -0
- package/src/runtime/query-executor.ts +58 -9
- package/src/runtime/query-worker.mjs +3 -2
- package/src/runtime/read-model.ts +111 -59
- package/src/runtime/result-supervision.ts +63 -0
- package/src/runtime/work-client.d.mts +1 -0
- package/src/runtime/work-client.mjs +16 -2
- package/src/server.ts +66 -5
- package/src/version.ts +1 -1
|
@@ -0,0 +1,1331 @@
|
|
|
1
|
+
// Generated from packages/pi-better-subagents/catalog-schema.ts. Do not edit directly.
|
|
2
|
+
/**
|
|
3
|
+
* Portable pi-agent/v1 definitions.
|
|
4
|
+
*
|
|
5
|
+
* Markdown body plus YAML frontmatter. This module is pure: no filesystem, no
|
|
6
|
+
* Pi APIs, and no child launch. Operations and resolution import these types
|
|
7
|
+
* directly; the extension entrypoint is intentionally not wired here.
|
|
8
|
+
*
|
|
9
|
+
* Portable preferences (model, effort, tier) are catalog data. They are not
|
|
10
|
+
* host execution controls and do not grant tools, sandbox modes, or presets.
|
|
11
|
+
*/
|
|
12
|
+
import { isAlias, isCollection, isMap, isPair, isScalar, parseDocument, stringify } from "yaml";
|
|
13
|
+
import { THINKING_LEVELS } from "./thinking.mjs";
|
|
14
|
+
export const CATALOG_SCHEMA_VERSION = "pi-agent/v1";
|
|
15
|
+
export const PREFERENCE_KEYS = ["model", "effort", "tier"];
|
|
16
|
+
/**
|
|
17
|
+
* Every key `defaults` (roles) and `overrides` (agents) may carry. `skills` is
|
|
18
|
+
* added here but is not a `PreferenceKey`: model resolution consumes preferences,
|
|
19
|
+
* while skill exposure is reproduced by the launcher from the definition.
|
|
20
|
+
*/
|
|
21
|
+
export const DEFINITION_PREFERENCE_KEYS = [...PREFERENCE_KEYS, "skills"];
|
|
22
|
+
/** Closed set of thinking levels shared with the existing subagent parser. */
|
|
23
|
+
export const EFFORT_LEVELS = THINKING_LEVELS;
|
|
24
|
+
/**
|
|
25
|
+
* Top-level keys that describe host execution. The catalog preserves them and
|
|
26
|
+
* blocks launch; it never treats them as enforced permissions or tool presets.
|
|
27
|
+
*/
|
|
28
|
+
export const HOST_EXECUTION_CONTROL_KEYS = [
|
|
29
|
+
"sandbox",
|
|
30
|
+
"sandbox_mode",
|
|
31
|
+
"sandboxMode",
|
|
32
|
+
"tools",
|
|
33
|
+
"tool",
|
|
34
|
+
"toolPreset",
|
|
35
|
+
"toolsPreset",
|
|
36
|
+
"permissions",
|
|
37
|
+
"permission",
|
|
38
|
+
"mcp",
|
|
39
|
+
"mcpServers",
|
|
40
|
+
"extensions",
|
|
41
|
+
"network",
|
|
42
|
+
"filesystem",
|
|
43
|
+
"cwd",
|
|
44
|
+
"workspace",
|
|
45
|
+
"allow_nested",
|
|
46
|
+
"nested",
|
|
47
|
+
"env",
|
|
48
|
+
"environment",
|
|
49
|
+
];
|
|
50
|
+
export const PARSER_LIMITS = {
|
|
51
|
+
maxFileBytes: 512 * 1024,
|
|
52
|
+
maxFrontmatterBytes: 128 * 1024,
|
|
53
|
+
maxBodyBytes: 384 * 1024,
|
|
54
|
+
maxAliasCount: 50,
|
|
55
|
+
maxNodes: 2_000,
|
|
56
|
+
maxDepth: 32,
|
|
57
|
+
maxIdLength: 128,
|
|
58
|
+
maxNameLength: 120,
|
|
59
|
+
maxDescriptionLength: 2_000,
|
|
60
|
+
maxStringLength: 64 * 1024,
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Approved bundled-role defaults from DEV-23. Short model names in the product
|
|
64
|
+
* table are OpenAI model ids; stored values use the provider/model form the
|
|
65
|
+
* runtime already uses. Tiers are catalog policy, not benchmark rankings.
|
|
66
|
+
*/
|
|
67
|
+
export const APPROVED_ROLE_DEFAULTS = [
|
|
68
|
+
{ id: "role.researcher", name: "Researcher", model: "openai/gpt-6.1-terra", effort: "medium", tier: "balanced" },
|
|
69
|
+
{ id: "role.explorer", name: "Explorer", model: "openai/gpt-6-luna", effort: "medium", tier: "efficient" },
|
|
70
|
+
{ id: "role.product-manager", name: "Product Manager", model: "openai/gpt-6.1-sol", effort: "medium", tier: "balanced" },
|
|
71
|
+
{ id: "role.developer", name: "Developer", model: "openai/gpt-6.1-sol", effort: "high", tier: "balanced" },
|
|
72
|
+
{ id: "role.reviewer", name: "Reviewer", model: "openai/gpt-6-astra", effort: "medium", tier: "frontier" },
|
|
73
|
+
{ id: "role.architect", name: "Architect", model: "openai/gpt-6-astra", effort: "high", tier: "frontier" },
|
|
74
|
+
{ id: "role.writer", name: "Writer", model: "openai/gpt-6.1-terra", effort: "medium", tier: undefined },
|
|
75
|
+
];
|
|
76
|
+
export const DiagnosticCodes = {
|
|
77
|
+
malformedFrontmatter: "malformed-frontmatter",
|
|
78
|
+
parserLimit: "parser-limit",
|
|
79
|
+
duplicateKey: "duplicate-key",
|
|
80
|
+
invalidSchema: "invalid-schema",
|
|
81
|
+
invalidKind: "invalid-kind",
|
|
82
|
+
invalidId: "invalid-id",
|
|
83
|
+
duplicateId: "duplicate-id",
|
|
84
|
+
invalidField: "invalid-field",
|
|
85
|
+
invalidInstructionMode: "invalid-instruction-mode",
|
|
86
|
+
emptyReplacement: "empty-replacement",
|
|
87
|
+
multipleBaseRoles: "multiple-base-roles",
|
|
88
|
+
missingRole: "missing-role",
|
|
89
|
+
unusableRole: "unusable-role",
|
|
90
|
+
unknownAgent: "unknown-agent",
|
|
91
|
+
unknownRole: "unknown-role",
|
|
92
|
+
ambiguousSelector: "ambiguous-selector",
|
|
93
|
+
missingSelector: "missing-selector",
|
|
94
|
+
invalidShadow: "invalid-shadow",
|
|
95
|
+
unsupportedExecutionRestriction: "unsupported-execution-restriction",
|
|
96
|
+
projectCatalogSuppressed: "project-catalog-suppressed",
|
|
97
|
+
symlinkIgnored: "symlink-ignored",
|
|
98
|
+
credentialMaterial: "credential-material",
|
|
99
|
+
cosmeticMetadata: "cosmetic-metadata",
|
|
100
|
+
untrustedWrite: "untrusted-write",
|
|
101
|
+
immutableScope: "immutable-scope",
|
|
102
|
+
definitionExists: "definition-exists",
|
|
103
|
+
pathEscape: "path-escape",
|
|
104
|
+
notLaunchable: "not-launchable",
|
|
105
|
+
kindDirectoryMismatch: "kind-directory-mismatch",
|
|
106
|
+
bundledMissing: "bundled-missing",
|
|
107
|
+
ioError: "io-error",
|
|
108
|
+
unsafeConfigDir: "unsafe-config-dir",
|
|
109
|
+
};
|
|
110
|
+
const ID_PATTERN = /^(role|agent)\.[a-z0-9]+(?:[.-][a-z0-9]+)*$/;
|
|
111
|
+
const MODEL_PATTERN = /^[a-z0-9][a-z0-9._-]*\/[A-Za-z0-9][A-Za-z0-9._+-]*$/;
|
|
112
|
+
const TIER_PATTERN = /^[a-z][a-z0-9-]{0,32}$/;
|
|
113
|
+
/** POSIX absolute, or a Windows drive/UNC path. */
|
|
114
|
+
const ABSOLUTE_PATH = /^(?:\/|[A-Za-z]:[\\/]|\\\\)/;
|
|
115
|
+
const SECRET_KEY = /^(api[_-]?key|apikey|secret|password|passwd|token|access[_-]?token|refresh[_-]?token|credential|credentials|authorization|auth|databaseurl|connectionstring|dsn)$/i;
|
|
116
|
+
const MULTI_ROLE_KEYS = new Set(["roleIds", "baseRoles", "baseRole", "inherits", "roles"]);
|
|
117
|
+
const KNOWN_TOP_LEVEL = new Set([
|
|
118
|
+
"schema",
|
|
119
|
+
"kind",
|
|
120
|
+
"id",
|
|
121
|
+
"name",
|
|
122
|
+
"description",
|
|
123
|
+
"defaults",
|
|
124
|
+
"roleId",
|
|
125
|
+
"instructions",
|
|
126
|
+
"overrides",
|
|
127
|
+
"provenance",
|
|
128
|
+
"executionRestrictions",
|
|
129
|
+
"metadata",
|
|
130
|
+
"extensions",
|
|
131
|
+
"preservedExecutionControls",
|
|
132
|
+
...HOST_EXECUTION_CONTROL_KEYS,
|
|
133
|
+
...MULTI_ROLE_KEYS,
|
|
134
|
+
]);
|
|
135
|
+
const EXECUTION_KEY_SET = new Set(HOST_EXECUTION_CONTROL_KEYS);
|
|
136
|
+
export function formatDiagnostic(diagnostic) {
|
|
137
|
+
const where = [diagnostic.scope, diagnostic.path, diagnostic.id ? `id ${diagnostic.id}` : undefined]
|
|
138
|
+
.filter(Boolean)
|
|
139
|
+
.join(" ");
|
|
140
|
+
return where ? `${diagnostic.code}: ${diagnostic.message} (${where})` : `${diagnostic.code}: ${diagnostic.message}`;
|
|
141
|
+
}
|
|
142
|
+
export function hasBlockingDiagnostic(diagnostics) {
|
|
143
|
+
return diagnostics.some((diagnostic) => diagnostic.blocking);
|
|
144
|
+
}
|
|
145
|
+
export function kindForId(id) {
|
|
146
|
+
if (id.startsWith("role."))
|
|
147
|
+
return "role";
|
|
148
|
+
if (id.startsWith("agent."))
|
|
149
|
+
return "agent";
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
/** Prefix every role id carries. Stored ids and files keep it; people type and read the short name. */
|
|
153
|
+
export const ROLE_ID_PREFIX = "role.";
|
|
154
|
+
/** Short, user-facing role name: `role.developer` becomes `developer`. Any other id is returned unchanged. */
|
|
155
|
+
export function shortRoleName(id) {
|
|
156
|
+
return id.startsWith(ROLE_ID_PREFIX) ? id.slice(ROLE_ID_PREFIX.length) : id;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Canonical role id for a value typed into a `role` field. Trimmed and
|
|
160
|
+
* lowercased; a bare name gains the `role.` prefix, so `Developer` becomes
|
|
161
|
+
* `role.developer`. The result always starts with `role.`, so a role field
|
|
162
|
+
* can never select an `agent.*` named agent.
|
|
163
|
+
*/
|
|
164
|
+
export function canonicalRoleId(value) {
|
|
165
|
+
const trimmed = value.trim().toLowerCase();
|
|
166
|
+
if (trimmed === "")
|
|
167
|
+
return trimmed;
|
|
168
|
+
return trimmed.startsWith(ROLE_ID_PREFIX) ? trimmed : `${ROLE_ID_PREFIX}${trimmed}`;
|
|
169
|
+
}
|
|
170
|
+
/** Canonical named-agent id: trimmed and lowercased, since ids are lowercase-only. */
|
|
171
|
+
export function canonicalAgentId(value) {
|
|
172
|
+
return value.trim().toLowerCase();
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Canonical id for a field that takes either kind, such as `agents_catalog`
|
|
176
|
+
* inspect. An `agent.` id (any case) is lowercased; anything else is read as a role.
|
|
177
|
+
*/
|
|
178
|
+
export function canonicalCatalogId(value) {
|
|
179
|
+
const lowered = value.trim().toLowerCase();
|
|
180
|
+
return lowered.startsWith("agent.") ? lowered : canonicalRoleId(lowered);
|
|
181
|
+
}
|
|
182
|
+
export function isPreferenceKey(value) {
|
|
183
|
+
return PREFERENCE_KEYS.includes(value);
|
|
184
|
+
}
|
|
185
|
+
export function setAgentOverride(agent, key, value) {
|
|
186
|
+
if (key === "model")
|
|
187
|
+
return { ...agent, overrides: { ...agent.overrides, model: value } };
|
|
188
|
+
if (key === "effort")
|
|
189
|
+
return { ...agent, overrides: { ...agent.overrides, effort: value } };
|
|
190
|
+
return { ...agent, overrides: { ...agent.overrides, tier: value } };
|
|
191
|
+
}
|
|
192
|
+
/** Delete one override key so the next resolution inherits that field again. */
|
|
193
|
+
export function removeAgentOverride(agent, key) {
|
|
194
|
+
const overrides = { ...agent.overrides };
|
|
195
|
+
delete overrides[key];
|
|
196
|
+
return { ...agent, overrides };
|
|
197
|
+
}
|
|
198
|
+
export function updateAgentOverrides(agent, patch) {
|
|
199
|
+
const remove = patch.remove ?? [];
|
|
200
|
+
const set = patch.set ?? {};
|
|
201
|
+
for (const key of remove) {
|
|
202
|
+
if (set[key] !== undefined || (key in set && set[key] === null)) {
|
|
203
|
+
throw new Error(`Override ${key} cannot be both set and removed.`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const overrides = { ...agent.overrides };
|
|
207
|
+
for (const key of remove)
|
|
208
|
+
delete overrides[key];
|
|
209
|
+
if (Object.prototype.hasOwnProperty.call(set, "model") && set.model !== undefined)
|
|
210
|
+
overrides.model = set.model;
|
|
211
|
+
if (Object.prototype.hasOwnProperty.call(set, "effort") && set.effort !== undefined)
|
|
212
|
+
overrides.effort = set.effort;
|
|
213
|
+
if (Object.prototype.hasOwnProperty.call(set, "tier") && set.tier !== undefined)
|
|
214
|
+
overrides.tier = set.tier;
|
|
215
|
+
// Skill exposure is a definition value, not a resolution preference, so it
|
|
216
|
+
// rides the same patch path without joining the model/effort resolution.
|
|
217
|
+
if (Object.prototype.hasOwnProperty.call(set, "skills") && set.skills !== undefined)
|
|
218
|
+
overrides.skills = set.skills;
|
|
219
|
+
return { ...agent, overrides };
|
|
220
|
+
}
|
|
221
|
+
/** The definition's own skill value: undefined inherits, null suppresses, `[]` is none. */
|
|
222
|
+
export function definitionSkillPreference(definition) {
|
|
223
|
+
const preferences = definition.kind === "role" ? definition.defaults : definition.overrides;
|
|
224
|
+
if (!Object.prototype.hasOwnProperty.call(preferences, "skills"))
|
|
225
|
+
return undefined;
|
|
226
|
+
return preferences.skills === null || preferences.skills === undefined ? null : [...preferences.skills];
|
|
227
|
+
}
|
|
228
|
+
/** Absolute definition paths, with the empty selection rejected rather than stored as "none". */
|
|
229
|
+
export function definitionSkillPaths(paths) {
|
|
230
|
+
if (paths.length === 0) {
|
|
231
|
+
throw new Error("Selected skills needs at least one absolute path. Choose No skill catalog for none.");
|
|
232
|
+
}
|
|
233
|
+
return paths.map((entry, index) => {
|
|
234
|
+
if (typeof entry !== "string" || entry.trim() === "") {
|
|
235
|
+
throw new Error(`Selected skills[${index}] must be a non-empty absolute path.`);
|
|
236
|
+
}
|
|
237
|
+
const path = entry.trim();
|
|
238
|
+
if (!ABSOLUTE_PATH.test(path)) {
|
|
239
|
+
throw new Error(`Selected skills[${index}] must be absolute: ${path}. A definition has no workspace of its own.`);
|
|
240
|
+
}
|
|
241
|
+
return path;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
/** Apply one edit without flattening inherited values or touching other fields. */
|
|
245
|
+
export function applySkillExposure(definition, edit) {
|
|
246
|
+
const paths = edit.mode === "explicit" ? definitionSkillPaths(edit.paths) : undefined;
|
|
247
|
+
const apply = (preferences) => {
|
|
248
|
+
const next = { ...preferences };
|
|
249
|
+
if (edit.mode === "inherit")
|
|
250
|
+
delete next.skills;
|
|
251
|
+
else if (edit.mode === "none")
|
|
252
|
+
next.skills = [];
|
|
253
|
+
else
|
|
254
|
+
next.skills = [...paths];
|
|
255
|
+
return next;
|
|
256
|
+
};
|
|
257
|
+
return definition.kind === "role"
|
|
258
|
+
? { ...definition, defaults: apply(definition.defaults) }
|
|
259
|
+
: { ...definition, overrides: apply(definition.overrides) };
|
|
260
|
+
}
|
|
261
|
+
export function parseDefinition(markdown, sourcePath) {
|
|
262
|
+
const diagnostics = [];
|
|
263
|
+
try {
|
|
264
|
+
return parseDefinitionWithinLimits(markdown, sourcePath, diagnostics);
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
const occupant = safeEarlyOccupant(markdown);
|
|
268
|
+
diagnostics.push(problem(DiagnosticCodes.parserLimit, `Catalog parser stopped on this file: ${clip(error instanceof Error ? error.message : String(error))}. Other definitions are unaffected. Remove cyclic aliases or shorten the frontmatter.`, {
|
|
269
|
+
path: sourcePath,
|
|
270
|
+
...(occupant.occupantId ? { id: occupant.occupantId } : {}),
|
|
271
|
+
blocking: true,
|
|
272
|
+
structural: true,
|
|
273
|
+
}));
|
|
274
|
+
return { ok: false, diagnostics, ...occupant };
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function parseDefinitionWithinLimits(markdown, sourcePath, diagnostics) {
|
|
278
|
+
if (Buffer.byteLength(markdown, "utf8") > PARSER_LIMITS.maxFileBytes) {
|
|
279
|
+
return rejectEarly(diagnostics, DiagnosticCodes.parserLimit, `Definition exceeds ${PARSER_LIMITS.maxFileBytes} bytes. Split or shorten it.`, sourcePath, markdown);
|
|
280
|
+
}
|
|
281
|
+
if (markdown.includes("\0")) {
|
|
282
|
+
return rejectEarly(diagnostics, DiagnosticCodes.malformedFrontmatter, "Definition contains NUL and was not parsed.", sourcePath, markdown);
|
|
283
|
+
}
|
|
284
|
+
const split = splitFrontmatter(markdown);
|
|
285
|
+
if ("error" in split) {
|
|
286
|
+
return rejectEarly(diagnostics, DiagnosticCodes.malformedFrontmatter, split.error, sourcePath, markdown);
|
|
287
|
+
}
|
|
288
|
+
if (Buffer.byteLength(split.yaml, "utf8") > PARSER_LIMITS.maxFrontmatterBytes) {
|
|
289
|
+
return rejectEarly(diagnostics, DiagnosticCodes.parserLimit, `Frontmatter exceeds ${PARSER_LIMITS.maxFrontmatterBytes} bytes.`, sourcePath, markdown);
|
|
290
|
+
}
|
|
291
|
+
if (Buffer.byteLength(split.body, "utf8") > PARSER_LIMITS.maxBodyBytes) {
|
|
292
|
+
return rejectEarly(diagnostics, DiagnosticCodes.parserLimit, `Instruction body exceeds ${PARSER_LIMITS.maxBodyBytes} bytes.`, sourcePath, markdown);
|
|
293
|
+
}
|
|
294
|
+
const document = parseCatalogYaml(split.yaml);
|
|
295
|
+
const identity = occupantFromDocument(document);
|
|
296
|
+
if (document.errors.length > 0) {
|
|
297
|
+
for (const error of document.errors) {
|
|
298
|
+
const duplicate = /unique|duplicated mapping key/i.test(error.message);
|
|
299
|
+
diagnostics.push(problem(duplicate ? DiagnosticCodes.duplicateKey : DiagnosticCodes.malformedFrontmatter, clip(error.message), {
|
|
300
|
+
path: sourcePath,
|
|
301
|
+
id: identity.occupantId,
|
|
302
|
+
blocking: true,
|
|
303
|
+
structural: true,
|
|
304
|
+
}));
|
|
305
|
+
}
|
|
306
|
+
return { ok: false, diagnostics, ...identity };
|
|
307
|
+
}
|
|
308
|
+
const measured = measureNode(document.contents);
|
|
309
|
+
if (measured.nodes > PARSER_LIMITS.maxNodes || measured.depth > PARSER_LIMITS.maxDepth) {
|
|
310
|
+
diagnostics.push(problem(DiagnosticCodes.parserLimit, `Frontmatter has ${measured.nodes} nodes at depth ${measured.depth}. Limits are ${PARSER_LIMITS.maxNodes} nodes and depth ${PARSER_LIMITS.maxDepth}. Anchors are allowed within these limits.`, { path: sourcePath, id: identity.occupantId, blocking: true, structural: true }));
|
|
311
|
+
return { ok: false, diagnostics, ...identity };
|
|
312
|
+
}
|
|
313
|
+
let data;
|
|
314
|
+
try {
|
|
315
|
+
data = document.toJS({ maxAliasCount: PARSER_LIMITS.maxAliasCount });
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
diagnostics.push(problem(DiagnosticCodes.parserLimit, clip(error instanceof Error ? error.message : String(error)), {
|
|
319
|
+
path: sourcePath,
|
|
320
|
+
id: identity.occupantId,
|
|
321
|
+
blocking: true,
|
|
322
|
+
structural: true,
|
|
323
|
+
}));
|
|
324
|
+
return { ok: false, diagnostics, ...identity };
|
|
325
|
+
}
|
|
326
|
+
const graph = yamlGraphIssue(data);
|
|
327
|
+
if (graph) {
|
|
328
|
+
diagnostics.push(problem(DiagnosticCodes.parserLimit, graph === "cycle"
|
|
329
|
+
? "YAML alias cycle in frontmatter. Later aliases may repeat an earlier anchor, but a cycle is rejected before recursive traversal so this file cannot exhaust the catalog read. Remove the cyclic alias. Alias expansions stay limited to 50, nodes to 2000, and depth to 32."
|
|
330
|
+
: `YAML alias graph exceeded the catalog bounds (${PARSER_LIMITS.maxNodes} nodes, depth ${PARSER_LIMITS.maxDepth}) while checking for cycles. Shorten the frontmatter.`, { path: sourcePath, id: identity.occupantId, blocking: true, structural: true }));
|
|
331
|
+
return { ok: false, diagnostics, ...identity };
|
|
332
|
+
}
|
|
333
|
+
if (!isPlainObject(data)) {
|
|
334
|
+
diagnostics.push(problem(DiagnosticCodes.malformedFrontmatter, "Frontmatter must be a YAML mapping.", {
|
|
335
|
+
path: sourcePath,
|
|
336
|
+
id: identity.occupantId,
|
|
337
|
+
blocking: true,
|
|
338
|
+
structural: true,
|
|
339
|
+
}));
|
|
340
|
+
return { ok: false, diagnostics, ...identity };
|
|
341
|
+
}
|
|
342
|
+
try {
|
|
343
|
+
return interpretMapping(data, split.body, diagnostics, sourcePath);
|
|
344
|
+
}
|
|
345
|
+
catch (error) {
|
|
346
|
+
diagnostics.push(problem(DiagnosticCodes.parserLimit, `Catalog parser stopped on this file: ${clip(error instanceof Error ? error.message : String(error))}. Other definitions are unaffected. Remove cyclic aliases or shorten the frontmatter.`, { path: sourcePath, id: identity.occupantId, blocking: true, structural: true }));
|
|
347
|
+
return { ok: false, diagnostics, ...identity };
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
export function validateDefinition(definition) {
|
|
351
|
+
return parseDefinition(serializeUnchecked(definition), undefined);
|
|
352
|
+
}
|
|
353
|
+
export function serializeDefinition(definition) {
|
|
354
|
+
const parsed = validateDefinition(definition);
|
|
355
|
+
if (!parsed.ok || !parsed.definition) {
|
|
356
|
+
return { ok: false, diagnostics: parsed.diagnostics };
|
|
357
|
+
}
|
|
358
|
+
return {
|
|
359
|
+
ok: true,
|
|
360
|
+
markdown: serializeUnchecked(parsed.definition),
|
|
361
|
+
diagnostics: parsed.diagnostics,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
function serializeUnchecked(definition) {
|
|
365
|
+
const mapping = {
|
|
366
|
+
schema: definition.schema,
|
|
367
|
+
kind: definition.kind,
|
|
368
|
+
id: definition.id,
|
|
369
|
+
name: definition.name,
|
|
370
|
+
};
|
|
371
|
+
if (definition.description !== undefined)
|
|
372
|
+
mapping.description = definition.description;
|
|
373
|
+
if (definition.kind === "agent") {
|
|
374
|
+
mapping.roleId = definition.roleId;
|
|
375
|
+
mapping.instructions = { mode: definition.instructionMode };
|
|
376
|
+
if (hasPreference(definition.overrides))
|
|
377
|
+
mapping.overrides = preferenceMapping(definition.overrides);
|
|
378
|
+
}
|
|
379
|
+
else if (hasPreference(definition.defaults)) {
|
|
380
|
+
mapping.defaults = preferenceMapping(definition.defaults);
|
|
381
|
+
}
|
|
382
|
+
if (definition.provenance)
|
|
383
|
+
mapping.provenance = { ...definition.provenance };
|
|
384
|
+
if (definition.executionRestrictions.length > 0)
|
|
385
|
+
mapping.executionRestrictions = definition.executionRestrictions.map((item) => ({ ...item }));
|
|
386
|
+
if (definition.metadata && Object.keys(definition.metadata).length > 0)
|
|
387
|
+
mapping.metadata = definition.metadata;
|
|
388
|
+
if (definition.extensions && Object.keys(definition.extensions).length > 0)
|
|
389
|
+
mapping.extensions = definition.extensions;
|
|
390
|
+
if (definition.preservedExecutionControls && Object.keys(definition.preservedExecutionControls).length > 0) {
|
|
391
|
+
mapping.preservedExecutionControls = definition.preservedExecutionControls;
|
|
392
|
+
}
|
|
393
|
+
const yaml = stringify(mapping, { lineWidth: 0 }).trimEnd();
|
|
394
|
+
const body = definition.body.length === 0 ? "\n" : definition.body.startsWith("\n") ? definition.body : `\n${definition.body}`;
|
|
395
|
+
const normalizedBody = body.endsWith("\n") ? body : `${body}\n`;
|
|
396
|
+
return `---\n${yaml}\n---${normalizedBody}`;
|
|
397
|
+
}
|
|
398
|
+
function interpretMapping(data, body, diagnostics, sourcePath) {
|
|
399
|
+
const redacted = redactSecrets(data, "frontmatter", diagnostics, sourcePath);
|
|
400
|
+
const idText = typeof redacted.id === "string" ? redacted.id : undefined;
|
|
401
|
+
const occupantId = idText && ID_PATTERN.test(idText) ? idText : undefined;
|
|
402
|
+
if (redacted.schema !== CATALOG_SCHEMA_VERSION) {
|
|
403
|
+
diagnostics.push(problem(DiagnosticCodes.invalidSchema, `schema must be ${CATALOG_SCHEMA_VERSION}.`, {
|
|
404
|
+
path: sourcePath,
|
|
405
|
+
field: "schema",
|
|
406
|
+
id: occupantId,
|
|
407
|
+
blocking: true,
|
|
408
|
+
structural: true,
|
|
409
|
+
}));
|
|
410
|
+
}
|
|
411
|
+
if (redacted.kind !== "role" && redacted.kind !== "agent") {
|
|
412
|
+
diagnostics.push(problem(DiagnosticCodes.invalidKind, "kind must be role or agent.", {
|
|
413
|
+
path: sourcePath,
|
|
414
|
+
field: "kind",
|
|
415
|
+
id: occupantId,
|
|
416
|
+
blocking: true,
|
|
417
|
+
structural: true,
|
|
418
|
+
}));
|
|
419
|
+
}
|
|
420
|
+
if (!occupantId) {
|
|
421
|
+
diagnostics.push(problem(DiagnosticCodes.invalidId, "id must be a stable role.<slug> or agent.<slug> identifier, independent of the file name. Use lowercase letters, numbers, dots, and hyphens.", { path: sourcePath, field: "id", blocking: true, structural: true }));
|
|
422
|
+
}
|
|
423
|
+
else if (redacted.kind === "role" || redacted.kind === "agent") {
|
|
424
|
+
if (kindForId(occupantId) !== redacted.kind) {
|
|
425
|
+
diagnostics.push(problem(DiagnosticCodes.invalidId, `id ${occupantId} does not match kind ${redacted.kind}.`, {
|
|
426
|
+
path: sourcePath,
|
|
427
|
+
field: "id",
|
|
428
|
+
id: occupantId,
|
|
429
|
+
blocking: true,
|
|
430
|
+
structural: true,
|
|
431
|
+
}));
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
const name = readName(redacted.name, diagnostics, sourcePath, occupantId);
|
|
435
|
+
const description = readDescription(redacted.description, diagnostics, sourcePath, occupantId);
|
|
436
|
+
const extensions = {};
|
|
437
|
+
const preservedExecutionControls = isPlainObject(redacted.preservedExecutionControls)
|
|
438
|
+
? { ...redacted.preservedExecutionControls }
|
|
439
|
+
: {};
|
|
440
|
+
if (redacted.preservedExecutionControls !== undefined && !isPlainObject(redacted.preservedExecutionControls)) {
|
|
441
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "preservedExecutionControls must be a mapping.", {
|
|
442
|
+
path: sourcePath,
|
|
443
|
+
field: "preservedExecutionControls",
|
|
444
|
+
id: occupantId,
|
|
445
|
+
blocking: true,
|
|
446
|
+
structural: true,
|
|
447
|
+
}));
|
|
448
|
+
}
|
|
449
|
+
for (const key of Object.keys(redacted)) {
|
|
450
|
+
if (KNOWN_TOP_LEVEL.has(key))
|
|
451
|
+
continue;
|
|
452
|
+
extensions[key] = redacted[key];
|
|
453
|
+
diagnostics.push(problem(DiagnosticCodes.cosmeticMetadata, `Unknown field ${key} was preserved and is not enforced.`, { path: sourcePath, field: key, id: occupantId, blocking: false, structural: false, severity: "warning" }));
|
|
454
|
+
}
|
|
455
|
+
if (isPlainObject(redacted.extensions)) {
|
|
456
|
+
for (const [key, value] of Object.entries(redacted.extensions))
|
|
457
|
+
extensions[key] = value;
|
|
458
|
+
}
|
|
459
|
+
for (const key of HOST_EXECUTION_CONTROL_KEYS) {
|
|
460
|
+
if (!Object.prototype.hasOwnProperty.call(redacted, key))
|
|
461
|
+
continue;
|
|
462
|
+
preservedExecutionControls[key] = redacted[key];
|
|
463
|
+
rememberRestriction(key, redacted[key], diagnostics, sourcePath, occupantId);
|
|
464
|
+
}
|
|
465
|
+
const multi = [...MULTI_ROLE_KEYS].filter((key) => Object.prototype.hasOwnProperty.call(redacted, key));
|
|
466
|
+
if (multi.length > 0 || Array.isArray(redacted.roleId)) {
|
|
467
|
+
for (const key of multi)
|
|
468
|
+
extensions[key] = redacted[key];
|
|
469
|
+
diagnostics.push(problem(DiagnosticCodes.multipleBaseRoles, "An agent has exactly one base role (roleId). Multiple inheritance and executable-role memberships are not catalog parents. Remove the extra role fields.", { path: sourcePath, field: multi[0] ?? "roleId", id: occupantId, blocking: true, structural: true }));
|
|
470
|
+
}
|
|
471
|
+
const provenance = readProvenance(redacted.provenance, extensions, diagnostics, sourcePath, occupantId);
|
|
472
|
+
const restrictions = readRestrictions(redacted.executionRestrictions, diagnostics, sourcePath, occupantId);
|
|
473
|
+
for (const key of Object.keys(preservedExecutionControls)) {
|
|
474
|
+
if (restrictions.some((item) => item.name === key))
|
|
475
|
+
continue;
|
|
476
|
+
restrictions.push({
|
|
477
|
+
name: key,
|
|
478
|
+
value: preservedExecutionControls[key],
|
|
479
|
+
required: true,
|
|
480
|
+
honored: false,
|
|
481
|
+
reason: "Preserved host execution control. The catalog does not enforce it, so launch stays blocked until the restriction is removed.",
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
for (const restriction of restrictions) {
|
|
485
|
+
if (!restriction.required) {
|
|
486
|
+
diagnostics.push(problem(DiagnosticCodes.cosmeticMetadata, `Optional execution restriction ${restriction.name} is preserved and is not enforced.`, { path: sourcePath, field: restriction.name, id: occupantId, blocking: false, structural: false, severity: "warning" }));
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
diagnostics.push(problem(DiagnosticCodes.unsupportedExecutionRestriction, restriction.honored
|
|
490
|
+
? `Required execution restriction ${restriction.name} claims to be honored, but this catalog does not enforce host controls. Remove the restriction after it is actually resolved; a flag does not make it active.`
|
|
491
|
+
: `Required execution restriction ${restriction.name} is preserved and blocks launch because the catalog cannot enforce it. ${restriction.reason ?? "Remove it once a real host control covers it, or delete the requirement."}`, { path: sourcePath, field: restriction.name, id: occupantId, blocking: true, structural: false }));
|
|
492
|
+
}
|
|
493
|
+
const metadata = isPlainObject(redacted.metadata) ? redacted.metadata : undefined;
|
|
494
|
+
if (redacted.metadata !== undefined && !isPlainObject(redacted.metadata)) {
|
|
495
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "metadata must be a mapping.", {
|
|
496
|
+
path: sourcePath,
|
|
497
|
+
field: "metadata",
|
|
498
|
+
id: occupantId,
|
|
499
|
+
blocking: true,
|
|
500
|
+
structural: true,
|
|
501
|
+
}));
|
|
502
|
+
}
|
|
503
|
+
else if (metadata && Object.keys(metadata).length > 0) {
|
|
504
|
+
diagnostics.push(problem(DiagnosticCodes.cosmeticMetadata, "metadata is preserved and does not change launch or capabilities.", {
|
|
505
|
+
path: sourcePath,
|
|
506
|
+
field: "metadata",
|
|
507
|
+
id: occupantId,
|
|
508
|
+
blocking: false,
|
|
509
|
+
structural: false,
|
|
510
|
+
severity: "info",
|
|
511
|
+
}));
|
|
512
|
+
}
|
|
513
|
+
const structural = diagnostics.some((diagnostic) => diagnostic.structural);
|
|
514
|
+
if (structural || !occupantId || (redacted.kind !== "role" && redacted.kind !== "agent") || !name) {
|
|
515
|
+
return { ok: false, occupantId, diagnostics };
|
|
516
|
+
}
|
|
517
|
+
if (redacted.kind === "role") {
|
|
518
|
+
if (redacted.roleId !== undefined || redacted.instructions !== undefined || redacted.overrides !== undefined) {
|
|
519
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "Roles do not take roleId, instructions, or overrides. Put model, effort, and tier in defaults.", {
|
|
520
|
+
path: sourcePath,
|
|
521
|
+
id: occupantId,
|
|
522
|
+
blocking: true,
|
|
523
|
+
structural: true,
|
|
524
|
+
}));
|
|
525
|
+
return { ok: false, occupantId, diagnostics };
|
|
526
|
+
}
|
|
527
|
+
const defaults = readPreferences(redacted.defaults, "defaults", extensions, restrictions, diagnostics, sourcePath, occupantId);
|
|
528
|
+
if (diagnostics.some((diagnostic) => diagnostic.structural))
|
|
529
|
+
return { ok: false, occupantId, diagnostics };
|
|
530
|
+
const definition = compactDefinition({
|
|
531
|
+
schema: CATALOG_SCHEMA_VERSION,
|
|
532
|
+
kind: "role",
|
|
533
|
+
id: occupantId,
|
|
534
|
+
name,
|
|
535
|
+
description,
|
|
536
|
+
defaults: defaults ?? {},
|
|
537
|
+
provenance,
|
|
538
|
+
executionRestrictions: restrictions,
|
|
539
|
+
metadata,
|
|
540
|
+
extensions: Object.keys(extensions).length > 0 ? extensions : undefined,
|
|
541
|
+
preservedExecutionControls: Object.keys(preservedExecutionControls).length > 0 ? preservedExecutionControls : undefined,
|
|
542
|
+
body,
|
|
543
|
+
});
|
|
544
|
+
return { ok: true, definition, diagnostics };
|
|
545
|
+
}
|
|
546
|
+
if (redacted.defaults !== undefined) {
|
|
547
|
+
extensions.defaults = redacted.defaults;
|
|
548
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "Agents must not store role defaults. Omit inherited fields and set only explicit overrides. The defaults mapping was preserved under extensions and was not copied into overrides.", { path: sourcePath, field: "defaults", id: occupantId, blocking: true, structural: true }));
|
|
549
|
+
return { ok: false, occupantId, diagnostics };
|
|
550
|
+
}
|
|
551
|
+
const roleId = typeof redacted.roleId === "string" && ID_PATTERN.test(redacted.roleId) && kindForId(redacted.roleId) === "role"
|
|
552
|
+
? redacted.roleId
|
|
553
|
+
: undefined;
|
|
554
|
+
if (!roleId) {
|
|
555
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "roleId must reference exactly one role.<slug> base role.", {
|
|
556
|
+
path: sourcePath,
|
|
557
|
+
field: "roleId",
|
|
558
|
+
id: occupantId,
|
|
559
|
+
blocking: true,
|
|
560
|
+
structural: true,
|
|
561
|
+
}));
|
|
562
|
+
return { ok: false, occupantId, diagnostics };
|
|
563
|
+
}
|
|
564
|
+
const instructionMode = readInstructionMode(redacted.instructions, diagnostics, sourcePath, occupantId);
|
|
565
|
+
if (!instructionMode || diagnostics.some((diagnostic) => diagnostic.structural)) {
|
|
566
|
+
return { ok: false, occupantId, diagnostics };
|
|
567
|
+
}
|
|
568
|
+
if (instructionMode === "replace" && body.trim().length === 0) {
|
|
569
|
+
diagnostics.push(problem(DiagnosticCodes.emptyReplacement, "Instruction mode replace requires nonempty instruction text. Model and effort still inherit unless overridden; an empty replacement was not saved as add mode.", { path: sourcePath, field: "instructions.mode", id: occupantId, blocking: true, structural: true }));
|
|
570
|
+
return { ok: false, occupantId, diagnostics };
|
|
571
|
+
}
|
|
572
|
+
const overrides = readPreferences(redacted.overrides, "overrides", extensions, restrictions, diagnostics, sourcePath, occupantId);
|
|
573
|
+
if (diagnostics.some((diagnostic) => diagnostic.structural))
|
|
574
|
+
return { ok: false, occupantId, diagnostics };
|
|
575
|
+
const definition = compactDefinition({
|
|
576
|
+
schema: CATALOG_SCHEMA_VERSION,
|
|
577
|
+
kind: "agent",
|
|
578
|
+
id: occupantId,
|
|
579
|
+
name,
|
|
580
|
+
description,
|
|
581
|
+
roleId,
|
|
582
|
+
instructionMode,
|
|
583
|
+
overrides: overrides ?? {},
|
|
584
|
+
provenance,
|
|
585
|
+
executionRestrictions: restrictions,
|
|
586
|
+
metadata,
|
|
587
|
+
extensions: Object.keys(extensions).length > 0 ? extensions : undefined,
|
|
588
|
+
preservedExecutionControls: Object.keys(preservedExecutionControls).length > 0 ? preservedExecutionControls : undefined,
|
|
589
|
+
body,
|
|
590
|
+
});
|
|
591
|
+
return { ok: true, definition, diagnostics };
|
|
592
|
+
}
|
|
593
|
+
function readInstructionMode(value, diagnostics, sourcePath, id) {
|
|
594
|
+
if (value === undefined)
|
|
595
|
+
return "add";
|
|
596
|
+
if (!isPlainObject(value) || Array.isArray(value)) {
|
|
597
|
+
diagnostics.push(problem(DiagnosticCodes.invalidInstructionMode, "instructions must be a mapping with mode add (default) or replace.", {
|
|
598
|
+
path: sourcePath,
|
|
599
|
+
field: "instructions",
|
|
600
|
+
id,
|
|
601
|
+
blocking: true,
|
|
602
|
+
structural: true,
|
|
603
|
+
}));
|
|
604
|
+
return undefined;
|
|
605
|
+
}
|
|
606
|
+
const mode = value.mode === undefined ? "add" : value.mode;
|
|
607
|
+
if (mode !== "add" && mode !== "replace") {
|
|
608
|
+
diagnostics.push(problem(DiagnosticCodes.invalidInstructionMode, "instructions.mode must be add or replace. replace changes instruction text only.", {
|
|
609
|
+
path: sourcePath,
|
|
610
|
+
field: "instructions.mode",
|
|
611
|
+
id,
|
|
612
|
+
blocking: true,
|
|
613
|
+
structural: true,
|
|
614
|
+
}));
|
|
615
|
+
return undefined;
|
|
616
|
+
}
|
|
617
|
+
for (const key of Object.keys(value)) {
|
|
618
|
+
if (key === "mode")
|
|
619
|
+
continue;
|
|
620
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, `Unknown instructions.${key} is not allowed. Instruction text belongs in the Markdown body.`, {
|
|
621
|
+
path: sourcePath,
|
|
622
|
+
field: `instructions.${key}`,
|
|
623
|
+
id,
|
|
624
|
+
blocking: true,
|
|
625
|
+
structural: true,
|
|
626
|
+
}));
|
|
627
|
+
}
|
|
628
|
+
return mode;
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Skill paths in a definition are literal and absolute: a catalog file has no
|
|
632
|
+
* workspace of its own, so a relative path would resolve differently depending
|
|
633
|
+
* on who launches it. Returns undefined for anything that is not such a list.
|
|
634
|
+
*/
|
|
635
|
+
function readSkillPaths(value) {
|
|
636
|
+
if (!Array.isArray(value))
|
|
637
|
+
return undefined;
|
|
638
|
+
const paths = [];
|
|
639
|
+
for (const entry of value) {
|
|
640
|
+
if (typeof entry !== "string")
|
|
641
|
+
return undefined;
|
|
642
|
+
const path = entry.trim();
|
|
643
|
+
if (path === "" || !ABSOLUTE_PATH.test(path))
|
|
644
|
+
return undefined;
|
|
645
|
+
paths.push(path);
|
|
646
|
+
}
|
|
647
|
+
return paths;
|
|
648
|
+
}
|
|
649
|
+
function readPreferences(value, field, extensions, restrictions, diagnostics, sourcePath, id) {
|
|
650
|
+
if (value === undefined)
|
|
651
|
+
return {};
|
|
652
|
+
if (!isPlainObject(value)) {
|
|
653
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, `${field} must be a mapping of model, effort, tier, and skills.`, {
|
|
654
|
+
path: sourcePath,
|
|
655
|
+
field,
|
|
656
|
+
id,
|
|
657
|
+
blocking: true,
|
|
658
|
+
structural: true,
|
|
659
|
+
}));
|
|
660
|
+
return undefined;
|
|
661
|
+
}
|
|
662
|
+
const preferences = {};
|
|
663
|
+
for (const key of Object.keys(value)) {
|
|
664
|
+
if (!isPreferenceKey(key) && key !== "skills") {
|
|
665
|
+
const target = `${field}.${key}`;
|
|
666
|
+
extensions[target] = value[key];
|
|
667
|
+
if (EXECUTION_KEY_SET.has(key)) {
|
|
668
|
+
rememberRestriction(target, value[key], diagnostics, sourcePath, id);
|
|
669
|
+
restrictions.push({
|
|
670
|
+
name: target,
|
|
671
|
+
value: value[key] ?? null,
|
|
672
|
+
required: true,
|
|
673
|
+
honored: false,
|
|
674
|
+
reason: "Host execution control nested in preferences. Preserved and not enforced.",
|
|
675
|
+
});
|
|
676
|
+
diagnostics.push(problem(DiagnosticCodes.unsupportedExecutionRestriction, `${target} is a host execution control, not a portable preference. It was preserved and blocks launch. Model, effort, tier, and skills are the only preference fields.`, { path: sourcePath, field: target, id, blocking: true, structural: false }));
|
|
677
|
+
}
|
|
678
|
+
else {
|
|
679
|
+
diagnostics.push(problem(DiagnosticCodes.cosmeticMetadata, `Unknown ${target} was preserved and is not a model, effort, tier, or skills preference.`, {
|
|
680
|
+
path: sourcePath,
|
|
681
|
+
field: target,
|
|
682
|
+
id,
|
|
683
|
+
blocking: false,
|
|
684
|
+
structural: false,
|
|
685
|
+
severity: "warning",
|
|
686
|
+
}));
|
|
687
|
+
}
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
const item = value[key];
|
|
691
|
+
if (item === null) {
|
|
692
|
+
preferences[key] = null;
|
|
693
|
+
continue;
|
|
694
|
+
}
|
|
695
|
+
if (key === "skills") {
|
|
696
|
+
const paths = readSkillPaths(item);
|
|
697
|
+
if (!paths) {
|
|
698
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, `${field}.skills must be a list of absolute paths to a SKILL.md file or a skill directory; [] loads no skills. Skill paths stay literal so a frozen launch recipe replays the same files.`, { path: sourcePath, field: `${field}.skills`, id, blocking: true, structural: true }));
|
|
699
|
+
continue;
|
|
700
|
+
}
|
|
701
|
+
preferences.skills = paths;
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
if (key === "model") {
|
|
705
|
+
if (typeof item !== "string" || !MODEL_PATTERN.test(item)) {
|
|
706
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "model must be provider/model, without an @effort suffix. Set effort separately.", {
|
|
707
|
+
path: sourcePath,
|
|
708
|
+
field: `${field}.model`,
|
|
709
|
+
id,
|
|
710
|
+
blocking: true,
|
|
711
|
+
structural: true,
|
|
712
|
+
}));
|
|
713
|
+
continue;
|
|
714
|
+
}
|
|
715
|
+
preferences.model = item;
|
|
716
|
+
continue;
|
|
717
|
+
}
|
|
718
|
+
if (key === "effort") {
|
|
719
|
+
if (typeof item !== "string" || !THINKING_LEVELS.includes(item)) {
|
|
720
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, `effort must be one of ${THINKING_LEVELS.join(", ")}. Availability against a live model is checked at resolution, not here.`, { path: sourcePath, field: `${field}.effort`, id, blocking: true, structural: true }));
|
|
721
|
+
continue;
|
|
722
|
+
}
|
|
723
|
+
preferences.effort = item;
|
|
724
|
+
continue;
|
|
725
|
+
}
|
|
726
|
+
if (typeof item !== "string" || !TIER_PATTERN.test(item)) {
|
|
727
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "tier must be a short lowercase catalog-policy label such as balanced, efficient, or frontier.", {
|
|
728
|
+
path: sourcePath,
|
|
729
|
+
field: `${field}.tier`,
|
|
730
|
+
id,
|
|
731
|
+
blocking: true,
|
|
732
|
+
structural: true,
|
|
733
|
+
}));
|
|
734
|
+
continue;
|
|
735
|
+
}
|
|
736
|
+
preferences.tier = item;
|
|
737
|
+
}
|
|
738
|
+
return preferences;
|
|
739
|
+
}
|
|
740
|
+
function readRestrictions(value, diagnostics, sourcePath, id) {
|
|
741
|
+
if (value === undefined)
|
|
742
|
+
return [];
|
|
743
|
+
if (!Array.isArray(value)) {
|
|
744
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "executionRestrictions must be a list.", {
|
|
745
|
+
path: sourcePath,
|
|
746
|
+
field: "executionRestrictions",
|
|
747
|
+
id,
|
|
748
|
+
blocking: true,
|
|
749
|
+
structural: true,
|
|
750
|
+
}));
|
|
751
|
+
return [];
|
|
752
|
+
}
|
|
753
|
+
const restrictions = [];
|
|
754
|
+
for (const [index, item] of value.entries()) {
|
|
755
|
+
if (!isPlainObject(item) || typeof item.name !== "string" || item.name.trim() === "") {
|
|
756
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, `executionRestrictions[${index}] needs a name.`, {
|
|
757
|
+
path: sourcePath,
|
|
758
|
+
field: "executionRestrictions",
|
|
759
|
+
id,
|
|
760
|
+
blocking: true,
|
|
761
|
+
structural: true,
|
|
762
|
+
}));
|
|
763
|
+
continue;
|
|
764
|
+
}
|
|
765
|
+
if (!isJsonData(item.value) && item.value !== undefined) {
|
|
766
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, `executionRestrictions[${index}].value must be JSON-compatible data.`, {
|
|
767
|
+
path: sourcePath,
|
|
768
|
+
field: `executionRestrictions.${item.name}`,
|
|
769
|
+
id,
|
|
770
|
+
blocking: true,
|
|
771
|
+
structural: true,
|
|
772
|
+
}));
|
|
773
|
+
continue;
|
|
774
|
+
}
|
|
775
|
+
const required = item.required === undefined ? true : item.required === true;
|
|
776
|
+
if (item.required !== undefined && typeof item.required !== "boolean") {
|
|
777
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, `executionRestrictions ${item.name} required must be a boolean.`, {
|
|
778
|
+
path: sourcePath,
|
|
779
|
+
field: item.name,
|
|
780
|
+
id,
|
|
781
|
+
blocking: true,
|
|
782
|
+
structural: true,
|
|
783
|
+
}));
|
|
784
|
+
continue;
|
|
785
|
+
}
|
|
786
|
+
const honored = item.honored === true;
|
|
787
|
+
restrictions.push({
|
|
788
|
+
name: item.name,
|
|
789
|
+
value: item.value ?? null,
|
|
790
|
+
required,
|
|
791
|
+
honored,
|
|
792
|
+
reason: typeof item.reason === "string" ? item.reason : undefined,
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
return restrictions;
|
|
796
|
+
}
|
|
797
|
+
function readProvenance(value, extensions, diagnostics, sourcePath, id) {
|
|
798
|
+
if (value === undefined)
|
|
799
|
+
return undefined;
|
|
800
|
+
if (!isPlainObject(value)) {
|
|
801
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "provenance must be a mapping.", {
|
|
802
|
+
path: sourcePath,
|
|
803
|
+
field: "provenance",
|
|
804
|
+
id,
|
|
805
|
+
blocking: true,
|
|
806
|
+
structural: true,
|
|
807
|
+
}));
|
|
808
|
+
return undefined;
|
|
809
|
+
}
|
|
810
|
+
if (value.origin !== "local" && value.origin !== "imported") {
|
|
811
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "provenance.origin must be local or imported.", {
|
|
812
|
+
path: sourcePath,
|
|
813
|
+
field: "provenance.origin",
|
|
814
|
+
id,
|
|
815
|
+
blocking: true,
|
|
816
|
+
structural: true,
|
|
817
|
+
}));
|
|
818
|
+
return undefined;
|
|
819
|
+
}
|
|
820
|
+
const provenance = { origin: value.origin };
|
|
821
|
+
if (value.format !== undefined) {
|
|
822
|
+
if (typeof value.format !== "string" || value.format.length > 80) {
|
|
823
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "provenance.format must be a short string.", {
|
|
824
|
+
path: sourcePath,
|
|
825
|
+
field: "provenance.format",
|
|
826
|
+
id,
|
|
827
|
+
blocking: true,
|
|
828
|
+
structural: true,
|
|
829
|
+
}));
|
|
830
|
+
}
|
|
831
|
+
else {
|
|
832
|
+
provenance.format = value.format;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
if (value.sourceRef !== undefined) {
|
|
836
|
+
if (typeof value.sourceRef !== "string" || value.sourceRef.length > 500 || /[\r\n]/.test(value.sourceRef)) {
|
|
837
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "provenance.sourceRef must be a single-line source label, not credential material.", {
|
|
838
|
+
path: sourcePath,
|
|
839
|
+
field: "provenance.sourceRef",
|
|
840
|
+
id,
|
|
841
|
+
blocking: true,
|
|
842
|
+
structural: true,
|
|
843
|
+
}));
|
|
844
|
+
}
|
|
845
|
+
else {
|
|
846
|
+
provenance.sourceRef = value.sourceRef;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
if (value.importedAt !== undefined) {
|
|
850
|
+
if (typeof value.importedAt !== "string" || value.importedAt.length > 40) {
|
|
851
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "provenance.importedAt must be a short timestamp string.", {
|
|
852
|
+
path: sourcePath,
|
|
853
|
+
field: "provenance.importedAt",
|
|
854
|
+
id,
|
|
855
|
+
blocking: true,
|
|
856
|
+
structural: true,
|
|
857
|
+
}));
|
|
858
|
+
}
|
|
859
|
+
else {
|
|
860
|
+
provenance.importedAt = value.importedAt;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
if (value.note !== undefined) {
|
|
864
|
+
if (typeof value.note !== "string" || value.note.length > 500) {
|
|
865
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "provenance.note must be a string of at most 500 characters.", {
|
|
866
|
+
path: sourcePath,
|
|
867
|
+
field: "provenance.note",
|
|
868
|
+
id,
|
|
869
|
+
blocking: true,
|
|
870
|
+
structural: true,
|
|
871
|
+
}));
|
|
872
|
+
}
|
|
873
|
+
else {
|
|
874
|
+
provenance.note = value.note;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
for (const key of Object.keys(value)) {
|
|
878
|
+
if (["origin", "format", "sourceRef", "importedAt", "note"].includes(key))
|
|
879
|
+
continue;
|
|
880
|
+
extensions[`provenance.${key}`] = value[key];
|
|
881
|
+
diagnostics.push(problem(DiagnosticCodes.cosmeticMetadata, `Unknown provenance.${key} was preserved.`, {
|
|
882
|
+
path: sourcePath,
|
|
883
|
+
field: `provenance.${key}`,
|
|
884
|
+
id,
|
|
885
|
+
blocking: false,
|
|
886
|
+
structural: false,
|
|
887
|
+
severity: "warning",
|
|
888
|
+
}));
|
|
889
|
+
}
|
|
890
|
+
return provenance;
|
|
891
|
+
}
|
|
892
|
+
function readName(value, diagnostics, sourcePath, id) {
|
|
893
|
+
if (typeof value !== "string" || value.trim() === "" || value.trim().length > PARSER_LIMITS.maxNameLength || /[\u0000-\u001F]/.test(value)) {
|
|
894
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "name must be a nonempty display name of at most 120 characters. Renaming it does not change id.", {
|
|
895
|
+
path: sourcePath,
|
|
896
|
+
field: "name",
|
|
897
|
+
id,
|
|
898
|
+
blocking: true,
|
|
899
|
+
structural: true,
|
|
900
|
+
}));
|
|
901
|
+
return undefined;
|
|
902
|
+
}
|
|
903
|
+
return value.trim();
|
|
904
|
+
}
|
|
905
|
+
function readDescription(value, diagnostics, sourcePath, id) {
|
|
906
|
+
if (value === undefined)
|
|
907
|
+
return undefined;
|
|
908
|
+
if (typeof value !== "string" || value.trim() === "" || value.length > PARSER_LIMITS.maxDescriptionLength) {
|
|
909
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, "description must be a nonempty string of at most 2000 characters when present.", {
|
|
910
|
+
path: sourcePath,
|
|
911
|
+
field: "description",
|
|
912
|
+
id,
|
|
913
|
+
blocking: true,
|
|
914
|
+
structural: true,
|
|
915
|
+
}));
|
|
916
|
+
return undefined;
|
|
917
|
+
}
|
|
918
|
+
return value.trim();
|
|
919
|
+
}
|
|
920
|
+
function rememberRestriction(name, value, diagnostics, sourcePath, id) {
|
|
921
|
+
if (!isJsonData(value) && value !== undefined) {
|
|
922
|
+
diagnostics.push(problem(DiagnosticCodes.invalidField, `${name} must be JSON-compatible when preserved as a restriction.`, {
|
|
923
|
+
path: sourcePath,
|
|
924
|
+
field: name,
|
|
925
|
+
id,
|
|
926
|
+
blocking: true,
|
|
927
|
+
structural: true,
|
|
928
|
+
}));
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
function redactSecrets(value, pathLabel, diagnostics, sourcePath, stack = new Set()) {
|
|
932
|
+
if (typeof value !== "object" || value === null)
|
|
933
|
+
return value;
|
|
934
|
+
if (stack.has(value))
|
|
935
|
+
throw new Error("YAML alias cycle");
|
|
936
|
+
if (Array.isArray(value)) {
|
|
937
|
+
stack.add(value);
|
|
938
|
+
try {
|
|
939
|
+
return value.map((item, index) => redactSecrets(item, `${pathLabel}[${index}]`, diagnostics, sourcePath, stack));
|
|
940
|
+
}
|
|
941
|
+
finally {
|
|
942
|
+
stack.delete(value);
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
if (!isPlainObject(value))
|
|
946
|
+
return value;
|
|
947
|
+
stack.add(value);
|
|
948
|
+
try {
|
|
949
|
+
const output = {};
|
|
950
|
+
for (const [key, child] of Object.entries(value)) {
|
|
951
|
+
if (SECRET_KEY.test(key)) {
|
|
952
|
+
diagnostics.push(problem(DiagnosticCodes.credentialMaterial, `Field ${pathLabel}.${key} looks like credential or connection material and was not stored. Remove it from the definition. Launch stays blocked.`, { path: sourcePath, field: key, blocking: true, structural: false }));
|
|
953
|
+
output[key] = "[redacted]";
|
|
954
|
+
continue;
|
|
955
|
+
}
|
|
956
|
+
output[key] = redactSecrets(child, `${pathLabel}.${key}`, diagnostics, sourcePath, stack);
|
|
957
|
+
}
|
|
958
|
+
return output;
|
|
959
|
+
}
|
|
960
|
+
finally {
|
|
961
|
+
stack.delete(value);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
function splitFrontmatter(source) {
|
|
965
|
+
const text = source.charCodeAt(0) === 0xfeff ? source.slice(1) : source;
|
|
966
|
+
const opening = /^---[ \t]*\r?\n/.exec(text);
|
|
967
|
+
if (!opening) {
|
|
968
|
+
return { error: "Definition must be Markdown with opening YAML frontmatter (---). Filename and display name are not the schema." };
|
|
969
|
+
}
|
|
970
|
+
const rest = text.slice(opening[0].length);
|
|
971
|
+
const closing = /\r?\n---[ \t]*(?:\r?\n|$)/.exec(rest);
|
|
972
|
+
if (!closing || closing.index === undefined) {
|
|
973
|
+
return { error: "Frontmatter is missing the closing --- line. Keep instruction text below it." };
|
|
974
|
+
}
|
|
975
|
+
return {
|
|
976
|
+
yaml: rest.slice(0, closing.index),
|
|
977
|
+
body: rest.slice(closing.index + closing[0].length),
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
function parseCatalogYaml(yaml) {
|
|
981
|
+
return parseDocument(yaml, {
|
|
982
|
+
uniqueKeys: true,
|
|
983
|
+
version: "1.2",
|
|
984
|
+
schema: "core",
|
|
985
|
+
merge: true,
|
|
986
|
+
resolveKnownTags: false,
|
|
987
|
+
strict: true,
|
|
988
|
+
prettyErrors: true,
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
function rejectEarly(diagnostics, code, message, sourcePath, markdown) {
|
|
992
|
+
const occupant = safeEarlyOccupant(markdown);
|
|
993
|
+
diagnostics.push(problem(code, message, {
|
|
994
|
+
path: sourcePath,
|
|
995
|
+
...(occupant.occupantId ? { id: occupant.occupantId } : {}),
|
|
996
|
+
blocking: true,
|
|
997
|
+
structural: true,
|
|
998
|
+
}));
|
|
999
|
+
return { ok: false, diagnostics, ...occupant };
|
|
1000
|
+
}
|
|
1001
|
+
/**
|
|
1002
|
+
* Identity for a rejection that must not run the full parser.
|
|
1003
|
+
* Closed, in-limit frontmatter may be parsed. Anything larger, cut off, or
|
|
1004
|
+
* containing NUL is only scanned for one plain top-level id inside the
|
|
1005
|
+
* frontmatter byte cap. The instruction body is never an identity source.
|
|
1006
|
+
*/
|
|
1007
|
+
function safeEarlyOccupant(markdown) {
|
|
1008
|
+
try {
|
|
1009
|
+
const source = markdown.charCodeAt(0) === 0xfeff ? markdown.slice(1) : markdown;
|
|
1010
|
+
const bounded = boundedUtf8Prefix(source, PARSER_LIMITS.maxFrontmatterBytes);
|
|
1011
|
+
const slice = frontmatterSlice(bounded.text);
|
|
1012
|
+
if (!slice)
|
|
1013
|
+
return {};
|
|
1014
|
+
const yamlWindow = boundedUtf8Prefix(slice.yaml, PARSER_LIMITS.maxFrontmatterBytes);
|
|
1015
|
+
const withinCap = !slice.yaml.includes("\0")
|
|
1016
|
+
&& Buffer.byteLength(slice.yaml, "utf8") <= PARSER_LIMITS.maxFrontmatterBytes
|
|
1017
|
+
&& !yamlWindow.truncated;
|
|
1018
|
+
const completeEnoughToParse = withinCap && (slice.closed || !bounded.truncated);
|
|
1019
|
+
if (completeEnoughToParse)
|
|
1020
|
+
return identityFromParsedYaml(slice.yaml);
|
|
1021
|
+
return occupantFromLines(dropIncompleteNulLine(yamlWindow.text));
|
|
1022
|
+
}
|
|
1023
|
+
catch {
|
|
1024
|
+
return {};
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
function identityFromParsedYaml(yaml) {
|
|
1028
|
+
try {
|
|
1029
|
+
return occupantFromDocument(parseCatalogYaml(yaml));
|
|
1030
|
+
}
|
|
1031
|
+
catch {
|
|
1032
|
+
return occupantFromLines(yaml);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
function boundedUtf8Prefix(text, maxBytes) {
|
|
1036
|
+
let bytes = 0;
|
|
1037
|
+
let index = 0;
|
|
1038
|
+
while (index < text.length) {
|
|
1039
|
+
const code = text.charCodeAt(index);
|
|
1040
|
+
let width = 1;
|
|
1041
|
+
let step = 1;
|
|
1042
|
+
if (code >= 0x80 && code < 0x800)
|
|
1043
|
+
width = 2;
|
|
1044
|
+
else if (code >= 0xd800 && code <= 0xdbff && index + 1 < text.length) {
|
|
1045
|
+
width = 4;
|
|
1046
|
+
step = 2;
|
|
1047
|
+
}
|
|
1048
|
+
else if (code >= 0x800)
|
|
1049
|
+
width = 3;
|
|
1050
|
+
if (bytes + width > maxBytes) {
|
|
1051
|
+
const slice = text.slice(0, index);
|
|
1052
|
+
const newline = Math.max(slice.lastIndexOf("\n"), slice.lastIndexOf("\r"));
|
|
1053
|
+
return newline < 0 ? { text: "", truncated: true } : { text: slice.slice(0, newline), truncated: true };
|
|
1054
|
+
}
|
|
1055
|
+
bytes += width;
|
|
1056
|
+
index += step;
|
|
1057
|
+
}
|
|
1058
|
+
return { text, truncated: false };
|
|
1059
|
+
}
|
|
1060
|
+
function dropIncompleteNulLine(text) {
|
|
1061
|
+
const index = text.indexOf("\0");
|
|
1062
|
+
if (index < 0)
|
|
1063
|
+
return text;
|
|
1064
|
+
const lineBreak = Math.max(text.lastIndexOf("\n", index - 1), text.lastIndexOf("\r", index - 1));
|
|
1065
|
+
return lineBreak >= 0 ? text.slice(0, lineBreak) : "";
|
|
1066
|
+
}
|
|
1067
|
+
function frontmatterSlice(text) {
|
|
1068
|
+
const opening = /^---[ \t]*\r?\n/.exec(text);
|
|
1069
|
+
if (!opening)
|
|
1070
|
+
return undefined;
|
|
1071
|
+
const rest = text.slice(opening[0].length);
|
|
1072
|
+
const closing = /\r?\n---[ \t]*(?:\r?\n|$)/.exec(rest);
|
|
1073
|
+
if (!closing || closing.index === undefined)
|
|
1074
|
+
return { yaml: rest, closed: false };
|
|
1075
|
+
return { yaml: rest.slice(0, closing.index), closed: true };
|
|
1076
|
+
}
|
|
1077
|
+
/** One unindented plain or quoted id. A trailing comment is kept; aliases and nested keys are not. */
|
|
1078
|
+
function occupantFromLines(yaml) {
|
|
1079
|
+
const found = [];
|
|
1080
|
+
for (const line of yaml.split(/\r?\n/)) {
|
|
1081
|
+
const match = /^id:[ \t]*(.*)$/.exec(line);
|
|
1082
|
+
if (!match)
|
|
1083
|
+
continue;
|
|
1084
|
+
const id = plainScalarId(match[1] ?? "");
|
|
1085
|
+
if (!id)
|
|
1086
|
+
return {};
|
|
1087
|
+
found.push(id);
|
|
1088
|
+
}
|
|
1089
|
+
if (found.length === 0 || new Set(found).size !== 1)
|
|
1090
|
+
return {};
|
|
1091
|
+
return { occupantId: found[0] };
|
|
1092
|
+
}
|
|
1093
|
+
function plainScalarId(raw) {
|
|
1094
|
+
const text = raw.trim();
|
|
1095
|
+
if (text === "" || text.startsWith("#"))
|
|
1096
|
+
return undefined;
|
|
1097
|
+
if (text.startsWith("\"") || text.startsWith("'")) {
|
|
1098
|
+
const quote = text[0];
|
|
1099
|
+
let value = "";
|
|
1100
|
+
for (let index = 1; index < text.length; index += 1) {
|
|
1101
|
+
const char = text[index];
|
|
1102
|
+
if (quote === "'" && char === "'" && text[index + 1] === "'") {
|
|
1103
|
+
value += "'";
|
|
1104
|
+
index += 1;
|
|
1105
|
+
continue;
|
|
1106
|
+
}
|
|
1107
|
+
if (quote === '"' && char === "\\")
|
|
1108
|
+
return undefined;
|
|
1109
|
+
if (char === quote) {
|
|
1110
|
+
const after = text.slice(index + 1).trim();
|
|
1111
|
+
if (after !== "" && !after.startsWith("#"))
|
|
1112
|
+
return undefined;
|
|
1113
|
+
return ID_PATTERN.test(value) ? value : undefined;
|
|
1114
|
+
}
|
|
1115
|
+
value += char;
|
|
1116
|
+
}
|
|
1117
|
+
return undefined;
|
|
1118
|
+
}
|
|
1119
|
+
const commentAt = text.search(/[ \t]#/);
|
|
1120
|
+
const plain = (commentAt >= 0 ? text.slice(0, commentAt) : text).trim();
|
|
1121
|
+
if (plain === "" || /^[&*!|>[{%@`]/.test(plain))
|
|
1122
|
+
return undefined;
|
|
1123
|
+
return ID_PATTERN.test(plain) ? plain : undefined;
|
|
1124
|
+
}
|
|
1125
|
+
/**
|
|
1126
|
+
* Stable id from the top-level `id` key of a parsed document, including when a
|
|
1127
|
+
* later field is malformed. Comments and nested values are not identities.
|
|
1128
|
+
* Aliases count only when they point at an earlier scalar anchor. Distinct
|
|
1129
|
+
* top-level ids are not guessed between.
|
|
1130
|
+
*/
|
|
1131
|
+
function occupantFromDocument(document) {
|
|
1132
|
+
if (!isMap(document.contents))
|
|
1133
|
+
return {};
|
|
1134
|
+
const ordered = nodesInOrder(document.contents);
|
|
1135
|
+
const resolved = [];
|
|
1136
|
+
for (const item of document.contents.items) {
|
|
1137
|
+
if (!isPair(item) || !isScalar(item.key) || item.key.value !== "id")
|
|
1138
|
+
continue;
|
|
1139
|
+
const value = resolveIdScalar(item.value, ordered);
|
|
1140
|
+
if (value === undefined)
|
|
1141
|
+
return {};
|
|
1142
|
+
resolved.push(value);
|
|
1143
|
+
}
|
|
1144
|
+
if (resolved.length === 0)
|
|
1145
|
+
return {};
|
|
1146
|
+
if (new Set(resolved).size !== 1)
|
|
1147
|
+
return {};
|
|
1148
|
+
const id = resolved[0];
|
|
1149
|
+
if (!ID_PATTERN.test(id))
|
|
1150
|
+
return {};
|
|
1151
|
+
return { occupantId: id };
|
|
1152
|
+
}
|
|
1153
|
+
function resolveIdScalar(node, ordered) {
|
|
1154
|
+
if (!node)
|
|
1155
|
+
return undefined;
|
|
1156
|
+
if (isScalar(node))
|
|
1157
|
+
return typeof node.value === "string" ? node.value : undefined;
|
|
1158
|
+
if (!isAlias(node) || !ordered)
|
|
1159
|
+
return undefined;
|
|
1160
|
+
const seen = new Set();
|
|
1161
|
+
let current = node;
|
|
1162
|
+
while (current && isAlias(current)) {
|
|
1163
|
+
if (seen.has(current))
|
|
1164
|
+
return undefined;
|
|
1165
|
+
seen.add(current);
|
|
1166
|
+
const index = ordered.findIndex((item) => item.node === current);
|
|
1167
|
+
if (index < 0)
|
|
1168
|
+
return undefined;
|
|
1169
|
+
let target;
|
|
1170
|
+
for (let cursor = index - 1; cursor >= 0; cursor -= 1) {
|
|
1171
|
+
if (ordered[cursor].anchor === current.source) {
|
|
1172
|
+
target = ordered[cursor].node;
|
|
1173
|
+
break;
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
current = target;
|
|
1177
|
+
}
|
|
1178
|
+
return current && isScalar(current) && typeof current.value === "string" ? current.value : undefined;
|
|
1179
|
+
}
|
|
1180
|
+
function nodesInOrder(root) {
|
|
1181
|
+
const ordered = [];
|
|
1182
|
+
const stack = [root];
|
|
1183
|
+
let visited = 0;
|
|
1184
|
+
while (stack.length > 0) {
|
|
1185
|
+
if (++visited > PARSER_LIMITS.maxNodes + 1)
|
|
1186
|
+
return undefined;
|
|
1187
|
+
const current = stack.pop();
|
|
1188
|
+
if (!current)
|
|
1189
|
+
continue;
|
|
1190
|
+
if (isAlias(current)) {
|
|
1191
|
+
ordered.push({ node: current });
|
|
1192
|
+
continue;
|
|
1193
|
+
}
|
|
1194
|
+
if (isScalar(current) || isCollection(current)) {
|
|
1195
|
+
const anchor = typeof current.anchor === "string" && current.anchor.length > 0 ? current.anchor : undefined;
|
|
1196
|
+
ordered.push({ node: current, anchor });
|
|
1197
|
+
}
|
|
1198
|
+
if (isPair(current)) {
|
|
1199
|
+
stack.push(current.value, current.key);
|
|
1200
|
+
continue;
|
|
1201
|
+
}
|
|
1202
|
+
if (isCollection(current)) {
|
|
1203
|
+
for (let index = current.items.length - 1; index >= 0; index -= 1) {
|
|
1204
|
+
stack.push(current.items[index]);
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
return ordered;
|
|
1209
|
+
}
|
|
1210
|
+
/** Iterative cycle check. Shared anchors (a diamond) are not cycles. */
|
|
1211
|
+
function yamlGraphIssue(value) {
|
|
1212
|
+
const seen = new Set();
|
|
1213
|
+
const inStack = new Set();
|
|
1214
|
+
const frames = [];
|
|
1215
|
+
const push = (node) => {
|
|
1216
|
+
if (typeof node !== "object" || node === null)
|
|
1217
|
+
return undefined;
|
|
1218
|
+
if (inStack.has(node))
|
|
1219
|
+
return "cycle";
|
|
1220
|
+
if (seen.has(node))
|
|
1221
|
+
return undefined;
|
|
1222
|
+
if (seen.size >= PARSER_LIMITS.maxNodes || frames.length >= PARSER_LIMITS.maxDepth)
|
|
1223
|
+
return "limit";
|
|
1224
|
+
seen.add(node);
|
|
1225
|
+
inStack.add(node);
|
|
1226
|
+
frames.push({
|
|
1227
|
+
node,
|
|
1228
|
+
children: Array.isArray(node) ? node : Object.values(node),
|
|
1229
|
+
index: 0,
|
|
1230
|
+
});
|
|
1231
|
+
return undefined;
|
|
1232
|
+
};
|
|
1233
|
+
const rootIssue = push(value);
|
|
1234
|
+
if (rootIssue)
|
|
1235
|
+
return rootIssue;
|
|
1236
|
+
while (frames.length > 0) {
|
|
1237
|
+
const frame = frames[frames.length - 1];
|
|
1238
|
+
if (frame.index >= frame.children.length) {
|
|
1239
|
+
inStack.delete(frame.node);
|
|
1240
|
+
frames.pop();
|
|
1241
|
+
continue;
|
|
1242
|
+
}
|
|
1243
|
+
const issue = push(frame.children[frame.index]);
|
|
1244
|
+
frame.index += 1;
|
|
1245
|
+
if (issue)
|
|
1246
|
+
return issue;
|
|
1247
|
+
}
|
|
1248
|
+
return undefined;
|
|
1249
|
+
}
|
|
1250
|
+
function measureNode(node) {
|
|
1251
|
+
const state = { nodes: 0, depth: 0 };
|
|
1252
|
+
walkNode(node, 1, state);
|
|
1253
|
+
return state;
|
|
1254
|
+
}
|
|
1255
|
+
function walkNode(node, depth, state) {
|
|
1256
|
+
if (node == null || state.nodes > PARSER_LIMITS.maxNodes + 1)
|
|
1257
|
+
return;
|
|
1258
|
+
state.nodes += 1;
|
|
1259
|
+
state.depth = Math.max(state.depth, depth);
|
|
1260
|
+
if (isAlias(node))
|
|
1261
|
+
return;
|
|
1262
|
+
if (isPair(node)) {
|
|
1263
|
+
walkNode(node.key, depth + 1, state);
|
|
1264
|
+
walkNode(node.value, depth + 1, state);
|
|
1265
|
+
return;
|
|
1266
|
+
}
|
|
1267
|
+
if (isCollection(node)) {
|
|
1268
|
+
for (const item of node.items)
|
|
1269
|
+
walkNode(item, depth + 1, state);
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
function preferenceMapping(preferences) {
|
|
1273
|
+
const mapping = {};
|
|
1274
|
+
for (const key of DEFINITION_PREFERENCE_KEYS) {
|
|
1275
|
+
if (Object.prototype.hasOwnProperty.call(preferences, key) && preferences[key] !== undefined) {
|
|
1276
|
+
mapping[key] = preferences[key];
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
return mapping;
|
|
1280
|
+
}
|
|
1281
|
+
function hasPreference(preferences) {
|
|
1282
|
+
return DEFINITION_PREFERENCE_KEYS.some((key) => preferences[key] !== undefined);
|
|
1283
|
+
}
|
|
1284
|
+
function compactDefinition(definition) {
|
|
1285
|
+
if (!definition.description)
|
|
1286
|
+
delete definition.description;
|
|
1287
|
+
if (!definition.provenance)
|
|
1288
|
+
delete definition.provenance;
|
|
1289
|
+
if (!definition.metadata)
|
|
1290
|
+
delete definition.metadata;
|
|
1291
|
+
if (!definition.extensions)
|
|
1292
|
+
delete definition.extensions;
|
|
1293
|
+
if (!definition.preservedExecutionControls)
|
|
1294
|
+
delete definition.preservedExecutionControls;
|
|
1295
|
+
return definition;
|
|
1296
|
+
}
|
|
1297
|
+
function problem(code, message, fields) {
|
|
1298
|
+
return {
|
|
1299
|
+
severity: fields.severity ?? (fields.blocking ? "error" : "warning"),
|
|
1300
|
+
code,
|
|
1301
|
+
message,
|
|
1302
|
+
path: fields.path,
|
|
1303
|
+
field: fields.field,
|
|
1304
|
+
id: fields.id,
|
|
1305
|
+
blocking: fields.blocking,
|
|
1306
|
+
structural: fields.structural,
|
|
1307
|
+
};
|
|
1308
|
+
}
|
|
1309
|
+
function isPlainObject(value) {
|
|
1310
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype;
|
|
1311
|
+
}
|
|
1312
|
+
function isJsonData(value, depth = 0) {
|
|
1313
|
+
if (value === null)
|
|
1314
|
+
return true;
|
|
1315
|
+
if (typeof value === "string")
|
|
1316
|
+
return value.length <= PARSER_LIMITS.maxStringLength;
|
|
1317
|
+
if (typeof value === "boolean")
|
|
1318
|
+
return true;
|
|
1319
|
+
if (typeof value === "number")
|
|
1320
|
+
return Number.isFinite(value);
|
|
1321
|
+
if (depth > PARSER_LIMITS.maxDepth)
|
|
1322
|
+
return false;
|
|
1323
|
+
if (Array.isArray(value))
|
|
1324
|
+
return value.every((item) => isJsonData(item, depth + 1));
|
|
1325
|
+
if (isPlainObject(value))
|
|
1326
|
+
return Object.values(value).every((item) => isJsonData(item, depth + 1));
|
|
1327
|
+
return false;
|
|
1328
|
+
}
|
|
1329
|
+
function clip(message) {
|
|
1330
|
+
return message.length > 500 ? `${message.slice(0, 500)}…` : message;
|
|
1331
|
+
}
|