pi-stacked-context 0.0.0 → 1.0.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/README.md +134 -13
- package/package.json +11 -5
- package/src/config/defaults.ts +76 -0
- package/src/config/schema.ts +985 -0
- package/src/config/settings.ts +190 -0
- package/src/discovery/adapters.ts +173 -0
- package/src/discovery/paths.ts +158 -0
- package/src/discovery/roots.ts +105 -0
- package/src/index.ts +14 -2
- package/src/shared/diagnostics.ts +35 -0
- package/src/shared/types.ts +71 -0
- package/src/skills/discover.ts +360 -0
|
@@ -0,0 +1,985 @@
|
|
|
1
|
+
import { workspaceRelativePatternProblem } from "../discovery/paths.js";
|
|
2
|
+
import { rootPatternProblem } from "../discovery/roots.js";
|
|
3
|
+
import { createDiagnostic } from "../shared/diagnostics.js";
|
|
4
|
+
import type {
|
|
5
|
+
ConfigLayer,
|
|
6
|
+
ConfigProvenance,
|
|
7
|
+
Diagnostic,
|
|
8
|
+
EffectiveConfig,
|
|
9
|
+
LoadedConfiguration,
|
|
10
|
+
SkillAdapter,
|
|
11
|
+
} from "../shared/types.js";
|
|
12
|
+
import { BUDGET_MAX_BYTES, BUDGET_MIN_BYTES, CONFIG_VERSION } from "./defaults.js";
|
|
13
|
+
|
|
14
|
+
const KNOWN_SECTIONS = ["discovery", "skills", "instructions", "rules", "ui"];
|
|
15
|
+
const KNOWN_TOP_LEVEL_KEYS = ["version", ...KNOWN_SECTIONS];
|
|
16
|
+
const UNSAFE_ADAPTER_NAMES = new Set(["__proto__", "prototype", "constructor"]);
|
|
17
|
+
const CHANNELS = ["global", "project"] as const;
|
|
18
|
+
|
|
19
|
+
export interface AdapterLayer {
|
|
20
|
+
global?: string | null;
|
|
21
|
+
project?: string | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface DiscoveryLayer {
|
|
25
|
+
rootPatterns?: string[];
|
|
26
|
+
additionalIgnorePaths?: string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface SkillsLayer {
|
|
30
|
+
enabled?: boolean;
|
|
31
|
+
adapters?: Record<string, AdapterLayer>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface InstructionsLayer {
|
|
35
|
+
enabled?: boolean;
|
|
36
|
+
maxFileBytes?: number;
|
|
37
|
+
maxInjectionBytes?: number;
|
|
38
|
+
maxVisibleBytes?: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface RulesLayer {
|
|
42
|
+
enabled?: boolean;
|
|
43
|
+
maxBundleBytes?: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface UiLayer {
|
|
47
|
+
showStatus?: boolean;
|
|
48
|
+
showWarnings?: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A single validated settings layer. Absent fields mean "inherit". */
|
|
52
|
+
export interface ConfigurationLayer {
|
|
53
|
+
version?: 1;
|
|
54
|
+
discovery?: DiscoveryLayer;
|
|
55
|
+
skills?: SkillsLayer;
|
|
56
|
+
instructions?: InstructionsLayer;
|
|
57
|
+
rules?: RulesLayer;
|
|
58
|
+
ui?: UiLayer;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface ParsedConfigurationLayer {
|
|
62
|
+
readonly layer: ConfigurationLayer;
|
|
63
|
+
readonly diagnostics: Diagnostic[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Settings files that produced the merged layers, when they are known. */
|
|
67
|
+
export interface ConfigurationSources {
|
|
68
|
+
readonly global?: string | undefined;
|
|
69
|
+
readonly project?: string | undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Validates one raw settings layer. Field-level isolation matters: an invalid
|
|
74
|
+
* field is dropped with a diagnostic while unrelated valid fields keep their
|
|
75
|
+
* values.
|
|
76
|
+
*/
|
|
77
|
+
export function parseConfigurationLayer(
|
|
78
|
+
data: Record<string, unknown> | null,
|
|
79
|
+
scope: ConfigLayer,
|
|
80
|
+
sourcePath?: string,
|
|
81
|
+
): ParsedConfigurationLayer {
|
|
82
|
+
const diagnostics: Diagnostic[] = [];
|
|
83
|
+
if (data === null) {
|
|
84
|
+
return { layer: {}, diagnostics };
|
|
85
|
+
}
|
|
86
|
+
if ("version" in data && data.version !== CONFIG_VERSION) {
|
|
87
|
+
diagnostics.push(
|
|
88
|
+
createDiagnostic(
|
|
89
|
+
"error",
|
|
90
|
+
"unsupported-version",
|
|
91
|
+
`${scope} settings declare version ${String(data.version)}; only version ${CONFIG_VERSION} is supported, so this layer is ignored.`,
|
|
92
|
+
{ sourcePath, field: "version" },
|
|
93
|
+
),
|
|
94
|
+
);
|
|
95
|
+
return { layer: {}, diagnostics };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
for (const key of Object.keys(data)) {
|
|
99
|
+
if (!KNOWN_TOP_LEVEL_KEYS.includes(key)) {
|
|
100
|
+
diagnostics.push(
|
|
101
|
+
createDiagnostic(
|
|
102
|
+
"warning",
|
|
103
|
+
"unknown-field",
|
|
104
|
+
`Unknown ${scope} setting "${key}" was ignored.`,
|
|
105
|
+
{
|
|
106
|
+
sourcePath,
|
|
107
|
+
field: key,
|
|
108
|
+
},
|
|
109
|
+
),
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const layer: ConfigurationLayer = {};
|
|
115
|
+
const discovery = parseDiscoverySection(data.discovery, scope, sourcePath, diagnostics);
|
|
116
|
+
if (discovery !== undefined) {
|
|
117
|
+
layer.discovery = discovery;
|
|
118
|
+
}
|
|
119
|
+
const skills = parseSkillsSection(data.skills, scope, sourcePath, diagnostics);
|
|
120
|
+
if (skills !== undefined) {
|
|
121
|
+
layer.skills = skills;
|
|
122
|
+
}
|
|
123
|
+
const instructions = parseInstructionsSection(data.instructions, scope, sourcePath, diagnostics);
|
|
124
|
+
if (instructions !== undefined) {
|
|
125
|
+
layer.instructions = instructions;
|
|
126
|
+
}
|
|
127
|
+
const rules = parseRulesSection(data.rules, scope, sourcePath, diagnostics);
|
|
128
|
+
if (rules !== undefined) {
|
|
129
|
+
layer.rules = rules;
|
|
130
|
+
}
|
|
131
|
+
const ui = parseUiSection(data.ui, scope, sourcePath, diagnostics);
|
|
132
|
+
if (ui !== undefined) {
|
|
133
|
+
layer.ui = ui;
|
|
134
|
+
}
|
|
135
|
+
return { layer, diagnostics };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Merges validated layers into the effective configuration and records where
|
|
140
|
+
* every value came from. Budgets select the smallest value, feature switches
|
|
141
|
+
* require global and project consent, and ui settings stay global-only.
|
|
142
|
+
*/
|
|
143
|
+
export function mergeConfigurationLayers(
|
|
144
|
+
base: EffectiveConfig,
|
|
145
|
+
globalLayer: ConfigurationLayer,
|
|
146
|
+
projectLayer: ConfigurationLayer,
|
|
147
|
+
trusted: boolean,
|
|
148
|
+
sources: ConfigurationSources = {},
|
|
149
|
+
): LoadedConfiguration {
|
|
150
|
+
const diagnostics: Diagnostic[] = [];
|
|
151
|
+
const provenance: Record<string, ConfigProvenance> = {};
|
|
152
|
+
const project = trusted ? projectLayer : {};
|
|
153
|
+
|
|
154
|
+
const additionalGlobal = globalLayer.discovery?.additionalIgnorePaths ?? [];
|
|
155
|
+
const additionalProject = project.discovery?.additionalIgnorePaths ?? [];
|
|
156
|
+
const rootPatterns = [
|
|
157
|
+
...resolveReplacement(
|
|
158
|
+
"discovery.rootPatterns",
|
|
159
|
+
base.discovery.rootPatterns,
|
|
160
|
+
globalLayer.discovery?.rootPatterns,
|
|
161
|
+
project.discovery?.rootPatterns,
|
|
162
|
+
provenance,
|
|
163
|
+
),
|
|
164
|
+
];
|
|
165
|
+
provenance["discovery.additionalIgnorePaths"] =
|
|
166
|
+
additionalProject.length > 0 ? "project" : additionalGlobal.length > 0 ? "global" : "default";
|
|
167
|
+
|
|
168
|
+
const adapters = mergeAdapters(base, globalLayer, project, provenance);
|
|
169
|
+
|
|
170
|
+
const config: EffectiveConfig = {
|
|
171
|
+
version: base.version,
|
|
172
|
+
discovery: {
|
|
173
|
+
rootPatterns,
|
|
174
|
+
additionalIgnorePaths: [
|
|
175
|
+
...base.discovery.additionalIgnorePaths,
|
|
176
|
+
...additionalGlobal,
|
|
177
|
+
...additionalProject,
|
|
178
|
+
],
|
|
179
|
+
},
|
|
180
|
+
skills: {
|
|
181
|
+
enabled: resolveSwitch(
|
|
182
|
+
"skills.enabled",
|
|
183
|
+
base.skills.enabled,
|
|
184
|
+
globalLayer.skills?.enabled,
|
|
185
|
+
project.skills?.enabled,
|
|
186
|
+
provenance,
|
|
187
|
+
),
|
|
188
|
+
adapters,
|
|
189
|
+
},
|
|
190
|
+
instructions: {
|
|
191
|
+
enabled: resolveSwitch(
|
|
192
|
+
"instructions.enabled",
|
|
193
|
+
base.instructions.enabled,
|
|
194
|
+
globalLayer.instructions?.enabled,
|
|
195
|
+
project.instructions?.enabled,
|
|
196
|
+
provenance,
|
|
197
|
+
),
|
|
198
|
+
maxFileBytes: resolveBudget(
|
|
199
|
+
"instructions.maxFileBytes",
|
|
200
|
+
base.instructions.maxFileBytes,
|
|
201
|
+
globalLayer.instructions?.maxFileBytes,
|
|
202
|
+
project.instructions?.maxFileBytes,
|
|
203
|
+
provenance,
|
|
204
|
+
diagnostics,
|
|
205
|
+
sources,
|
|
206
|
+
),
|
|
207
|
+
maxInjectionBytes: resolveBudget(
|
|
208
|
+
"instructions.maxInjectionBytes",
|
|
209
|
+
base.instructions.maxInjectionBytes,
|
|
210
|
+
globalLayer.instructions?.maxInjectionBytes,
|
|
211
|
+
project.instructions?.maxInjectionBytes,
|
|
212
|
+
provenance,
|
|
213
|
+
diagnostics,
|
|
214
|
+
sources,
|
|
215
|
+
),
|
|
216
|
+
maxVisibleBytes: resolveBudget(
|
|
217
|
+
"instructions.maxVisibleBytes",
|
|
218
|
+
base.instructions.maxVisibleBytes,
|
|
219
|
+
globalLayer.instructions?.maxVisibleBytes,
|
|
220
|
+
project.instructions?.maxVisibleBytes,
|
|
221
|
+
provenance,
|
|
222
|
+
diagnostics,
|
|
223
|
+
sources,
|
|
224
|
+
),
|
|
225
|
+
},
|
|
226
|
+
rules: {
|
|
227
|
+
enabled: resolveSwitch(
|
|
228
|
+
"rules.enabled",
|
|
229
|
+
base.rules.enabled,
|
|
230
|
+
globalLayer.rules?.enabled,
|
|
231
|
+
project.rules?.enabled,
|
|
232
|
+
provenance,
|
|
233
|
+
),
|
|
234
|
+
maxBundleBytes: resolveBudget(
|
|
235
|
+
"rules.maxBundleBytes",
|
|
236
|
+
base.rules.maxBundleBytes,
|
|
237
|
+
globalLayer.rules?.maxBundleBytes,
|
|
238
|
+
project.rules?.maxBundleBytes,
|
|
239
|
+
provenance,
|
|
240
|
+
diagnostics,
|
|
241
|
+
sources,
|
|
242
|
+
),
|
|
243
|
+
},
|
|
244
|
+
ui: {
|
|
245
|
+
showStatus: resolveGlobalOnlyBoolean(
|
|
246
|
+
"ui.showStatus",
|
|
247
|
+
base.ui.showStatus,
|
|
248
|
+
globalLayer.ui?.showStatus,
|
|
249
|
+
project.ui?.showStatus,
|
|
250
|
+
provenance,
|
|
251
|
+
diagnostics,
|
|
252
|
+
sources,
|
|
253
|
+
),
|
|
254
|
+
showWarnings: resolveGlobalOnlyBoolean(
|
|
255
|
+
"ui.showWarnings",
|
|
256
|
+
base.ui.showWarnings,
|
|
257
|
+
globalLayer.ui?.showWarnings,
|
|
258
|
+
project.ui?.showWarnings,
|
|
259
|
+
provenance,
|
|
260
|
+
diagnostics,
|
|
261
|
+
sources,
|
|
262
|
+
),
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
return { config, diagnostics, provenance };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function parseDiscoverySection(
|
|
270
|
+
raw: unknown,
|
|
271
|
+
scope: ConfigLayer,
|
|
272
|
+
sourcePath: string | undefined,
|
|
273
|
+
diagnostics: Diagnostic[],
|
|
274
|
+
): DiscoveryLayer | undefined {
|
|
275
|
+
if (raw === undefined) {
|
|
276
|
+
return undefined;
|
|
277
|
+
}
|
|
278
|
+
if (!isPlainRecord(raw)) {
|
|
279
|
+
diagnostics.push(
|
|
280
|
+
createDiagnostic("error", "invalid-type", `${scope} discovery must be an object.`, {
|
|
281
|
+
sourcePath,
|
|
282
|
+
field: "discovery",
|
|
283
|
+
}),
|
|
284
|
+
);
|
|
285
|
+
return undefined;
|
|
286
|
+
}
|
|
287
|
+
const section: DiscoveryLayer = {};
|
|
288
|
+
for (const key of Object.keys(raw)) {
|
|
289
|
+
if (key !== "rootPatterns" && key !== "additionalIgnorePaths") {
|
|
290
|
+
diagnostics.push(
|
|
291
|
+
createDiagnostic(
|
|
292
|
+
"warning",
|
|
293
|
+
"unknown-field",
|
|
294
|
+
`Unknown ${scope} setting "discovery.${key}".`,
|
|
295
|
+
{
|
|
296
|
+
sourcePath,
|
|
297
|
+
field: `discovery.${key}`,
|
|
298
|
+
},
|
|
299
|
+
),
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if ("rootPatterns" in raw) {
|
|
304
|
+
const patterns = readStringArray(raw.rootPatterns);
|
|
305
|
+
if (patterns === null) {
|
|
306
|
+
diagnostics.push(
|
|
307
|
+
createDiagnostic(
|
|
308
|
+
"error",
|
|
309
|
+
"invalid-type",
|
|
310
|
+
`discovery.rootPatterns must be an array of strings.`,
|
|
311
|
+
{
|
|
312
|
+
sourcePath,
|
|
313
|
+
field: "discovery.rootPatterns",
|
|
314
|
+
},
|
|
315
|
+
),
|
|
316
|
+
);
|
|
317
|
+
} else {
|
|
318
|
+
const problem = patterns.find((pattern) => rootPatternProblem(pattern) !== null);
|
|
319
|
+
if (problem === undefined) {
|
|
320
|
+
section.rootPatterns = patterns;
|
|
321
|
+
} else {
|
|
322
|
+
diagnostics.push(
|
|
323
|
+
createDiagnostic(
|
|
324
|
+
"error",
|
|
325
|
+
"unsafe-pattern",
|
|
326
|
+
`discovery.rootPatterns entry "${problem}" is not a safe workspace marker; the inherited list is used.`,
|
|
327
|
+
{ sourcePath, field: "discovery.rootPatterns" },
|
|
328
|
+
),
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if ("additionalIgnorePaths" in raw) {
|
|
334
|
+
const patterns = readStringArray(raw.additionalIgnorePaths);
|
|
335
|
+
if (patterns === null) {
|
|
336
|
+
diagnostics.push(
|
|
337
|
+
createDiagnostic(
|
|
338
|
+
"error",
|
|
339
|
+
"invalid-type",
|
|
340
|
+
`discovery.additionalIgnorePaths must be an array of strings.`,
|
|
341
|
+
{ sourcePath, field: "discovery.additionalIgnorePaths" },
|
|
342
|
+
),
|
|
343
|
+
);
|
|
344
|
+
} else {
|
|
345
|
+
const problem = patterns.find((pattern) => workspaceRelativePatternProblem(pattern) !== null);
|
|
346
|
+
if (problem === undefined) {
|
|
347
|
+
section.additionalIgnorePaths = patterns;
|
|
348
|
+
} else {
|
|
349
|
+
diagnostics.push(
|
|
350
|
+
createDiagnostic(
|
|
351
|
+
"error",
|
|
352
|
+
"unsafe-pattern",
|
|
353
|
+
`discovery.additionalIgnorePaths entry "${problem}" must be workspace relative; the field is ignored.`,
|
|
354
|
+
{ sourcePath, field: "discovery.additionalIgnorePaths" },
|
|
355
|
+
),
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return section;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function parseSkillsSection(
|
|
364
|
+
raw: unknown,
|
|
365
|
+
scope: ConfigLayer,
|
|
366
|
+
sourcePath: string | undefined,
|
|
367
|
+
diagnostics: Diagnostic[],
|
|
368
|
+
): SkillsLayer | undefined {
|
|
369
|
+
if (raw === undefined) {
|
|
370
|
+
return undefined;
|
|
371
|
+
}
|
|
372
|
+
if (!isPlainRecord(raw)) {
|
|
373
|
+
diagnostics.push(
|
|
374
|
+
createDiagnostic("error", "invalid-type", `${scope} skills must be an object.`, {
|
|
375
|
+
sourcePath,
|
|
376
|
+
field: "skills",
|
|
377
|
+
}),
|
|
378
|
+
);
|
|
379
|
+
return undefined;
|
|
380
|
+
}
|
|
381
|
+
const section: SkillsLayer = {};
|
|
382
|
+
for (const key of Object.keys(raw)) {
|
|
383
|
+
if (key !== "enabled" && key !== "adapters") {
|
|
384
|
+
diagnostics.push(
|
|
385
|
+
createDiagnostic("warning", "unknown-field", `Unknown ${scope} setting "skills.${key}".`, {
|
|
386
|
+
sourcePath,
|
|
387
|
+
field: `skills.${key}`,
|
|
388
|
+
}),
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
const enabled = readBoolean(raw, "enabled", "skills.enabled", scope, sourcePath, diagnostics);
|
|
393
|
+
if (enabled !== undefined) {
|
|
394
|
+
section.enabled = enabled;
|
|
395
|
+
}
|
|
396
|
+
if ("adapters" in raw) {
|
|
397
|
+
const adapters = parseAdaptersSection(raw.adapters, scope, sourcePath, diagnostics);
|
|
398
|
+
if (adapters !== undefined) {
|
|
399
|
+
section.adapters = adapters;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
return section;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function parseAdaptersSection(
|
|
406
|
+
raw: unknown,
|
|
407
|
+
scope: ConfigLayer,
|
|
408
|
+
sourcePath: string | undefined,
|
|
409
|
+
diagnostics: Diagnostic[],
|
|
410
|
+
): Record<string, AdapterLayer> | undefined {
|
|
411
|
+
if (!isPlainRecord(raw)) {
|
|
412
|
+
diagnostics.push(
|
|
413
|
+
createDiagnostic("error", "invalid-type", `${scope} skills.adapters must be an object.`, {
|
|
414
|
+
sourcePath,
|
|
415
|
+
field: "skills.adapters",
|
|
416
|
+
}),
|
|
417
|
+
);
|
|
418
|
+
return undefined;
|
|
419
|
+
}
|
|
420
|
+
const adapters: Record<string, AdapterLayer> = {};
|
|
421
|
+
for (const [name, rawAdapter] of Object.entries(raw)) {
|
|
422
|
+
if (UNSAFE_ADAPTER_NAMES.has(name)) {
|
|
423
|
+
diagnostics.push(
|
|
424
|
+
createDiagnostic(
|
|
425
|
+
"warning",
|
|
426
|
+
"unsafe-adapter-name",
|
|
427
|
+
`Adapter name "${name}" is reserved and was ignored.`,
|
|
428
|
+
{ sourcePath, field: `skills.adapters.${name}` },
|
|
429
|
+
),
|
|
430
|
+
);
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
if (!isPlainRecord(rawAdapter)) {
|
|
434
|
+
diagnostics.push(
|
|
435
|
+
createDiagnostic(
|
|
436
|
+
"error",
|
|
437
|
+
"invalid-adapter",
|
|
438
|
+
`${scope} adapter "${name}" must be an object.`,
|
|
439
|
+
{
|
|
440
|
+
sourcePath,
|
|
441
|
+
field: `skills.adapters.${name}`,
|
|
442
|
+
},
|
|
443
|
+
),
|
|
444
|
+
);
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
for (const key of Object.keys(rawAdapter)) {
|
|
448
|
+
if (key !== "global" && key !== "project") {
|
|
449
|
+
diagnostics.push(
|
|
450
|
+
createDiagnostic(
|
|
451
|
+
"warning",
|
|
452
|
+
"unknown-field",
|
|
453
|
+
`Unknown ${scope} setting "skills.adapters.${name}.${key}".`,
|
|
454
|
+
{ sourcePath, field: `skills.adapters.${name}.${key}` },
|
|
455
|
+
),
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
const adapter: AdapterLayer = {};
|
|
460
|
+
for (const channel of CHANNELS) {
|
|
461
|
+
if (!(channel in rawAdapter)) {
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
const field = `skills.adapters.${name}.${channel}`;
|
|
465
|
+
const value = rawAdapter[channel];
|
|
466
|
+
if (value === null) {
|
|
467
|
+
adapter[channel] = null;
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
if (typeof value !== "string") {
|
|
471
|
+
diagnostics.push(
|
|
472
|
+
createDiagnostic("error", "invalid-type", `${field} must be a string or null.`, {
|
|
473
|
+
sourcePath,
|
|
474
|
+
field,
|
|
475
|
+
}),
|
|
476
|
+
);
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
const trimmed = value.trim();
|
|
480
|
+
if (trimmed.length === 0) {
|
|
481
|
+
adapter[channel] = null;
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
if (trimmed.includes("\0")) {
|
|
485
|
+
diagnostics.push(
|
|
486
|
+
createDiagnostic("error", "unsafe-pattern", `${field} must not contain NUL bytes.`, {
|
|
487
|
+
sourcePath,
|
|
488
|
+
field,
|
|
489
|
+
}),
|
|
490
|
+
);
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
if (channel === "project") {
|
|
494
|
+
const problem = workspaceRelativePatternProblem(trimmed);
|
|
495
|
+
if (problem !== null) {
|
|
496
|
+
diagnostics.push(
|
|
497
|
+
createDiagnostic(
|
|
498
|
+
"error",
|
|
499
|
+
"unsafe-pattern",
|
|
500
|
+
`${field} "${trimmed}" must be a workspace-relative glob: ${problem}.`,
|
|
501
|
+
{ sourcePath, field },
|
|
502
|
+
),
|
|
503
|
+
);
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
adapter[channel] = trimmed;
|
|
508
|
+
}
|
|
509
|
+
if (Object.keys(adapter).length > 0) {
|
|
510
|
+
adapters[name] = adapter;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return adapters;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function parseInstructionsSection(
|
|
517
|
+
raw: unknown,
|
|
518
|
+
scope: ConfigLayer,
|
|
519
|
+
sourcePath: string | undefined,
|
|
520
|
+
diagnostics: Diagnostic[],
|
|
521
|
+
): InstructionsLayer | undefined {
|
|
522
|
+
if (raw === undefined) {
|
|
523
|
+
return undefined;
|
|
524
|
+
}
|
|
525
|
+
if (!isPlainRecord(raw)) {
|
|
526
|
+
diagnostics.push(
|
|
527
|
+
createDiagnostic("error", "invalid-type", `${scope} instructions must be an object.`, {
|
|
528
|
+
sourcePath,
|
|
529
|
+
field: "instructions",
|
|
530
|
+
}),
|
|
531
|
+
);
|
|
532
|
+
return undefined;
|
|
533
|
+
}
|
|
534
|
+
const section: InstructionsLayer = {};
|
|
535
|
+
for (const key of Object.keys(raw)) {
|
|
536
|
+
if (!["enabled", "maxFileBytes", "maxInjectionBytes", "maxVisibleBytes"].includes(key)) {
|
|
537
|
+
diagnostics.push(
|
|
538
|
+
createDiagnostic(
|
|
539
|
+
"warning",
|
|
540
|
+
"unknown-field",
|
|
541
|
+
`Unknown ${scope} setting "instructions.${key}".`,
|
|
542
|
+
{ sourcePath, field: `instructions.${key}` },
|
|
543
|
+
),
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
const enabled = readBoolean(
|
|
548
|
+
raw,
|
|
549
|
+
"enabled",
|
|
550
|
+
"instructions.enabled",
|
|
551
|
+
scope,
|
|
552
|
+
sourcePath,
|
|
553
|
+
diagnostics,
|
|
554
|
+
);
|
|
555
|
+
if (enabled !== undefined) {
|
|
556
|
+
section.enabled = enabled;
|
|
557
|
+
}
|
|
558
|
+
const maxFileBytes = readBudget(
|
|
559
|
+
raw,
|
|
560
|
+
"maxFileBytes",
|
|
561
|
+
"instructions",
|
|
562
|
+
scope,
|
|
563
|
+
sourcePath,
|
|
564
|
+
diagnostics,
|
|
565
|
+
);
|
|
566
|
+
if (maxFileBytes !== undefined) {
|
|
567
|
+
section.maxFileBytes = maxFileBytes;
|
|
568
|
+
}
|
|
569
|
+
const maxInjectionBytes = readBudget(
|
|
570
|
+
raw,
|
|
571
|
+
"maxInjectionBytes",
|
|
572
|
+
"instructions",
|
|
573
|
+
scope,
|
|
574
|
+
sourcePath,
|
|
575
|
+
diagnostics,
|
|
576
|
+
);
|
|
577
|
+
if (maxInjectionBytes !== undefined) {
|
|
578
|
+
section.maxInjectionBytes = maxInjectionBytes;
|
|
579
|
+
}
|
|
580
|
+
const maxVisibleBytes = readBudget(
|
|
581
|
+
raw,
|
|
582
|
+
"maxVisibleBytes",
|
|
583
|
+
"instructions",
|
|
584
|
+
scope,
|
|
585
|
+
sourcePath,
|
|
586
|
+
diagnostics,
|
|
587
|
+
);
|
|
588
|
+
if (maxVisibleBytes !== undefined) {
|
|
589
|
+
section.maxVisibleBytes = maxVisibleBytes;
|
|
590
|
+
}
|
|
591
|
+
return section;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
function parseRulesSection(
|
|
595
|
+
raw: unknown,
|
|
596
|
+
scope: ConfigLayer,
|
|
597
|
+
sourcePath: string | undefined,
|
|
598
|
+
diagnostics: Diagnostic[],
|
|
599
|
+
): RulesLayer | undefined {
|
|
600
|
+
if (raw === undefined) {
|
|
601
|
+
return undefined;
|
|
602
|
+
}
|
|
603
|
+
if (!isPlainRecord(raw)) {
|
|
604
|
+
diagnostics.push(
|
|
605
|
+
createDiagnostic("error", "invalid-type", `${scope} rules must be an object.`, {
|
|
606
|
+
sourcePath,
|
|
607
|
+
field: "rules",
|
|
608
|
+
}),
|
|
609
|
+
);
|
|
610
|
+
return undefined;
|
|
611
|
+
}
|
|
612
|
+
const section: RulesLayer = {};
|
|
613
|
+
for (const key of Object.keys(raw)) {
|
|
614
|
+
if (key !== "enabled" && key !== "maxBundleBytes") {
|
|
615
|
+
diagnostics.push(
|
|
616
|
+
createDiagnostic("warning", "unknown-field", `Unknown ${scope} setting "rules.${key}".`, {
|
|
617
|
+
sourcePath,
|
|
618
|
+
field: `rules.${key}`,
|
|
619
|
+
}),
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
const enabled = readBoolean(raw, "enabled", "rules.enabled", scope, sourcePath, diagnostics);
|
|
624
|
+
if (enabled !== undefined) {
|
|
625
|
+
section.enabled = enabled;
|
|
626
|
+
}
|
|
627
|
+
const maxBundleBytes = readBudget(raw, "maxBundleBytes", "rules", scope, sourcePath, diagnostics);
|
|
628
|
+
if (maxBundleBytes !== undefined) {
|
|
629
|
+
section.maxBundleBytes = maxBundleBytes;
|
|
630
|
+
}
|
|
631
|
+
return section;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function parseUiSection(
|
|
635
|
+
raw: unknown,
|
|
636
|
+
scope: ConfigLayer,
|
|
637
|
+
sourcePath: string | undefined,
|
|
638
|
+
diagnostics: Diagnostic[],
|
|
639
|
+
): UiLayer | undefined {
|
|
640
|
+
if (raw === undefined) {
|
|
641
|
+
return undefined;
|
|
642
|
+
}
|
|
643
|
+
if (!isPlainRecord(raw)) {
|
|
644
|
+
diagnostics.push(
|
|
645
|
+
createDiagnostic("error", "invalid-type", `${scope} ui must be an object.`, {
|
|
646
|
+
sourcePath,
|
|
647
|
+
field: "ui",
|
|
648
|
+
}),
|
|
649
|
+
);
|
|
650
|
+
return undefined;
|
|
651
|
+
}
|
|
652
|
+
const section: UiLayer = {};
|
|
653
|
+
for (const key of Object.keys(raw)) {
|
|
654
|
+
if (key !== "showStatus" && key !== "showWarnings") {
|
|
655
|
+
diagnostics.push(
|
|
656
|
+
createDiagnostic("warning", "unknown-field", `Unknown ${scope} setting "ui.${key}".`, {
|
|
657
|
+
sourcePath,
|
|
658
|
+
field: `ui.${key}`,
|
|
659
|
+
}),
|
|
660
|
+
);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
const showStatus = readBoolean(
|
|
664
|
+
raw,
|
|
665
|
+
"showStatus",
|
|
666
|
+
"ui.showStatus",
|
|
667
|
+
scope,
|
|
668
|
+
sourcePath,
|
|
669
|
+
diagnostics,
|
|
670
|
+
);
|
|
671
|
+
if (showStatus !== undefined) {
|
|
672
|
+
section.showStatus = showStatus;
|
|
673
|
+
}
|
|
674
|
+
const showWarnings = readBoolean(
|
|
675
|
+
raw,
|
|
676
|
+
"showWarnings",
|
|
677
|
+
"ui.showWarnings",
|
|
678
|
+
scope,
|
|
679
|
+
sourcePath,
|
|
680
|
+
diagnostics,
|
|
681
|
+
);
|
|
682
|
+
if (showWarnings !== undefined) {
|
|
683
|
+
section.showWarnings = showWarnings;
|
|
684
|
+
}
|
|
685
|
+
return section;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
function mergeAdapters(
|
|
689
|
+
base: EffectiveConfig,
|
|
690
|
+
globalLayer: ConfigurationLayer,
|
|
691
|
+
projectLayer: ConfigurationLayer,
|
|
692
|
+
provenance: Record<string, ConfigProvenance>,
|
|
693
|
+
): Record<string, SkillAdapter> {
|
|
694
|
+
const names = new Set([
|
|
695
|
+
...Object.keys(base.skills.adapters),
|
|
696
|
+
...Object.keys(globalLayer.skills?.adapters ?? {}),
|
|
697
|
+
...Object.keys(projectLayer.skills?.adapters ?? {}),
|
|
698
|
+
]);
|
|
699
|
+
const adapters: Record<string, SkillAdapter> = {};
|
|
700
|
+
for (const name of [...names].sort()) {
|
|
701
|
+
const baseAdapter = base.skills.adapters[name];
|
|
702
|
+
const globalAdapter = globalLayer.skills?.adapters?.[name];
|
|
703
|
+
const projectAdapter = projectLayer.skills?.adapters?.[name];
|
|
704
|
+
adapters[name] = {
|
|
705
|
+
global: resolveAdapterField(
|
|
706
|
+
`skills.adapters.${name}.global`,
|
|
707
|
+
baseAdapter?.global ?? null,
|
|
708
|
+
globalAdapter?.global,
|
|
709
|
+
projectAdapter?.global,
|
|
710
|
+
provenance,
|
|
711
|
+
),
|
|
712
|
+
project: resolveAdapterField(
|
|
713
|
+
`skills.adapters.${name}.project`,
|
|
714
|
+
baseAdapter?.project ?? null,
|
|
715
|
+
globalAdapter?.project,
|
|
716
|
+
projectAdapter?.project,
|
|
717
|
+
provenance,
|
|
718
|
+
),
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
return adapters;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
function resolveAdapterField(
|
|
725
|
+
field: string,
|
|
726
|
+
baseValue: string | null,
|
|
727
|
+
globalValue: string | null | undefined,
|
|
728
|
+
projectValue: string | null | undefined,
|
|
729
|
+
provenance: Record<string, ConfigProvenance>,
|
|
730
|
+
): string | null {
|
|
731
|
+
const global = normalizeAdapterValue(globalValue);
|
|
732
|
+
const project = normalizeAdapterValue(projectValue);
|
|
733
|
+
if (project !== undefined) {
|
|
734
|
+
provenance[field] = "project";
|
|
735
|
+
return project;
|
|
736
|
+
}
|
|
737
|
+
if (global !== undefined) {
|
|
738
|
+
provenance[field] = "global";
|
|
739
|
+
return global;
|
|
740
|
+
}
|
|
741
|
+
provenance[field] = "default";
|
|
742
|
+
return baseValue;
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/** A null or whitespace-only adapter value disables that channel. */
|
|
746
|
+
function normalizeAdapterValue(value: string | null | undefined): string | null | undefined {
|
|
747
|
+
if (value === undefined || value === null) {
|
|
748
|
+
return value;
|
|
749
|
+
}
|
|
750
|
+
const trimmed = value.trim();
|
|
751
|
+
return trimmed.length === 0 ? null : trimmed;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
function resolveReplacement<T>(
|
|
755
|
+
field: string,
|
|
756
|
+
baseValue: T,
|
|
757
|
+
globalValue: T | undefined,
|
|
758
|
+
projectValue: T | undefined,
|
|
759
|
+
provenance: Record<string, ConfigProvenance>,
|
|
760
|
+
): T {
|
|
761
|
+
if (projectValue !== undefined) {
|
|
762
|
+
provenance[field] = "project";
|
|
763
|
+
return projectValue;
|
|
764
|
+
}
|
|
765
|
+
if (globalValue !== undefined) {
|
|
766
|
+
provenance[field] = "global";
|
|
767
|
+
return globalValue;
|
|
768
|
+
}
|
|
769
|
+
provenance[field] = "default";
|
|
770
|
+
return baseValue;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
function resolveSwitch(
|
|
774
|
+
field: string,
|
|
775
|
+
baseValue: boolean,
|
|
776
|
+
globalValue: boolean | undefined,
|
|
777
|
+
projectValue: boolean | undefined,
|
|
778
|
+
provenance: Record<string, ConfigProvenance>,
|
|
779
|
+
): boolean {
|
|
780
|
+
const globalEnabled = globalValue ?? baseValue;
|
|
781
|
+
const projectEnabled = projectValue ?? true;
|
|
782
|
+
const effective = globalEnabled && projectEnabled;
|
|
783
|
+
if (!globalEnabled && projectValue === true) {
|
|
784
|
+
provenance[field] = "disabled-by-global";
|
|
785
|
+
} else if (projectValue !== undefined) {
|
|
786
|
+
provenance[field] = "project";
|
|
787
|
+
} else if (globalValue !== undefined) {
|
|
788
|
+
provenance[field] = "global";
|
|
789
|
+
} else {
|
|
790
|
+
provenance[field] = "default";
|
|
791
|
+
}
|
|
792
|
+
return effective;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
function resolveGlobalOnlyBoolean(
|
|
796
|
+
field: string,
|
|
797
|
+
baseValue: boolean,
|
|
798
|
+
globalValue: boolean | undefined,
|
|
799
|
+
projectValue: boolean | undefined,
|
|
800
|
+
provenance: Record<string, ConfigProvenance>,
|
|
801
|
+
diagnostics: Diagnostic[],
|
|
802
|
+
sources: ConfigurationSources,
|
|
803
|
+
): boolean {
|
|
804
|
+
if (projectValue !== undefined) {
|
|
805
|
+
diagnostics.push(
|
|
806
|
+
createDiagnostic(
|
|
807
|
+
"warning",
|
|
808
|
+
"ui-project-ignored",
|
|
809
|
+
`Project ${field} (${String(projectValue)}) is a user-level setting and was ignored.`,
|
|
810
|
+
{ field, layer: "project", sourcePath: sources.project },
|
|
811
|
+
),
|
|
812
|
+
);
|
|
813
|
+
}
|
|
814
|
+
if (globalValue !== undefined) {
|
|
815
|
+
provenance[field] = "global";
|
|
816
|
+
return globalValue;
|
|
817
|
+
}
|
|
818
|
+
provenance[field] = "default";
|
|
819
|
+
return baseValue;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
function resolveBudget(
|
|
823
|
+
field: string,
|
|
824
|
+
baseValue: number,
|
|
825
|
+
globalValue: number | undefined,
|
|
826
|
+
projectValue: number | undefined,
|
|
827
|
+
provenance: Record<string, ConfigProvenance>,
|
|
828
|
+
diagnostics: Diagnostic[],
|
|
829
|
+
sources: ConfigurationSources,
|
|
830
|
+
): number {
|
|
831
|
+
const globalCandidate = clampBudget("global", field, globalValue, diagnostics, sources.global);
|
|
832
|
+
const projectCandidate = clampBudget(
|
|
833
|
+
"project",
|
|
834
|
+
field,
|
|
835
|
+
projectValue,
|
|
836
|
+
diagnostics,
|
|
837
|
+
sources.project,
|
|
838
|
+
);
|
|
839
|
+
|
|
840
|
+
const candidates: ReadonlyArray<{ layer: ConfigLayer; value: number; clamped: boolean }> = [
|
|
841
|
+
{ layer: "default", value: baseValue, clamped: false },
|
|
842
|
+
...(globalCandidate === undefined
|
|
843
|
+
? []
|
|
844
|
+
: [
|
|
845
|
+
{
|
|
846
|
+
layer: "global" as const,
|
|
847
|
+
value: globalCandidate.value,
|
|
848
|
+
clamped: globalCandidate.clamped,
|
|
849
|
+
},
|
|
850
|
+
]),
|
|
851
|
+
...(projectCandidate === undefined
|
|
852
|
+
? []
|
|
853
|
+
: [
|
|
854
|
+
{
|
|
855
|
+
layer: "project" as const,
|
|
856
|
+
value: projectCandidate.value,
|
|
857
|
+
clamped: projectCandidate.clamped,
|
|
858
|
+
},
|
|
859
|
+
]),
|
|
860
|
+
];
|
|
861
|
+
let best = candidates[0];
|
|
862
|
+
if (best === undefined) {
|
|
863
|
+
return baseValue;
|
|
864
|
+
}
|
|
865
|
+
for (const candidate of candidates.slice(1)) {
|
|
866
|
+
if (candidate.value < best.value) {
|
|
867
|
+
best = candidate;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
const inheritedCeiling = Math.min(baseValue, globalCandidate?.value ?? baseValue);
|
|
872
|
+
if (projectCandidate !== undefined && projectCandidate.value > inheritedCeiling) {
|
|
873
|
+
diagnostics.push(
|
|
874
|
+
createDiagnostic(
|
|
875
|
+
"warning",
|
|
876
|
+
"budget-exceeds-global",
|
|
877
|
+
`Project ${field} (${projectCandidate.value}) exceeds the inherited budget (${inheritedCeiling}); the smaller value is used.`,
|
|
878
|
+
{ field, layer: "project", sourcePath: sources.project },
|
|
879
|
+
),
|
|
880
|
+
);
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
provenance[field] = best.clamped ? "clamped" : best.layer;
|
|
884
|
+
return best.value;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
interface BudgetCandidate {
|
|
888
|
+
readonly value: number;
|
|
889
|
+
readonly clamped: boolean;
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
function clampBudget(
|
|
893
|
+
layer: ConfigLayer,
|
|
894
|
+
field: string,
|
|
895
|
+
value: number | undefined,
|
|
896
|
+
diagnostics: Diagnostic[],
|
|
897
|
+
sourcePath: string | undefined,
|
|
898
|
+
): BudgetCandidate | undefined {
|
|
899
|
+
if (value === undefined) {
|
|
900
|
+
return undefined;
|
|
901
|
+
}
|
|
902
|
+
if (value > BUDGET_MAX_BYTES) {
|
|
903
|
+
diagnostics.push(
|
|
904
|
+
createDiagnostic(
|
|
905
|
+
"warning",
|
|
906
|
+
"budget-clamped",
|
|
907
|
+
`${layer} ${field} (${value}) exceeds the maximum of ${BUDGET_MAX_BYTES} bytes and is clamped.`,
|
|
908
|
+
{ field, layer, sourcePath },
|
|
909
|
+
),
|
|
910
|
+
);
|
|
911
|
+
return { value: BUDGET_MAX_BYTES, clamped: true };
|
|
912
|
+
}
|
|
913
|
+
return { value, clamped: false };
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
function readBoolean(
|
|
917
|
+
source: Record<string, unknown>,
|
|
918
|
+
key: string,
|
|
919
|
+
field: string,
|
|
920
|
+
scope: ConfigLayer,
|
|
921
|
+
sourcePath: string | undefined,
|
|
922
|
+
diagnostics: Diagnostic[],
|
|
923
|
+
): boolean | undefined {
|
|
924
|
+
if (!(key in source)) {
|
|
925
|
+
return undefined;
|
|
926
|
+
}
|
|
927
|
+
const value = source[key];
|
|
928
|
+
if (typeof value === "boolean") {
|
|
929
|
+
return value;
|
|
930
|
+
}
|
|
931
|
+
diagnostics.push(
|
|
932
|
+
createDiagnostic(
|
|
933
|
+
"error",
|
|
934
|
+
"invalid-type",
|
|
935
|
+
`${scope} setting ${field} must be a boolean; the inherited value is used.`,
|
|
936
|
+
{ sourcePath, field },
|
|
937
|
+
),
|
|
938
|
+
);
|
|
939
|
+
return undefined;
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
function readBudget(
|
|
943
|
+
source: Record<string, unknown>,
|
|
944
|
+
key: string,
|
|
945
|
+
section: string,
|
|
946
|
+
scope: ConfigLayer,
|
|
947
|
+
sourcePath: string | undefined,
|
|
948
|
+
diagnostics: Diagnostic[],
|
|
949
|
+
): number | undefined {
|
|
950
|
+
if (!(key in source)) {
|
|
951
|
+
return undefined;
|
|
952
|
+
}
|
|
953
|
+
const field = `${section}.${key}`;
|
|
954
|
+
const value = source[key];
|
|
955
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < BUDGET_MIN_BYTES) {
|
|
956
|
+
diagnostics.push(
|
|
957
|
+
createDiagnostic(
|
|
958
|
+
"error",
|
|
959
|
+
"invalid-type",
|
|
960
|
+
`${scope} setting ${field} must be an integer of at least ${BUDGET_MIN_BYTES} bytes; the inherited value is used.`,
|
|
961
|
+
{ sourcePath, field },
|
|
962
|
+
),
|
|
963
|
+
);
|
|
964
|
+
return undefined;
|
|
965
|
+
}
|
|
966
|
+
return value;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
function readStringArray(value: unknown): string[] | null {
|
|
970
|
+
if (!Array.isArray(value)) {
|
|
971
|
+
return null;
|
|
972
|
+
}
|
|
973
|
+
const entries: string[] = [];
|
|
974
|
+
for (const entry of value) {
|
|
975
|
+
if (typeof entry !== "string") {
|
|
976
|
+
return null;
|
|
977
|
+
}
|
|
978
|
+
entries.push(entry);
|
|
979
|
+
}
|
|
980
|
+
return entries;
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
|
984
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
985
|
+
}
|