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,71 @@
|
|
|
1
|
+
export type ConfigLayer = "default" | "global" | "project";
|
|
2
|
+
|
|
3
|
+
export type DiagnosticLevel = "info" | "warning" | "error";
|
|
4
|
+
|
|
5
|
+
export interface Diagnostic {
|
|
6
|
+
level: DiagnosticLevel;
|
|
7
|
+
code: string;
|
|
8
|
+
message: string;
|
|
9
|
+
/** Configuration layer that owns the diagnosed value, when a layer applies. */
|
|
10
|
+
layer?: ConfigLayer;
|
|
11
|
+
sourcePath?: string;
|
|
12
|
+
field?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** One adapter entry. Both channels are independent and may be disabled. */
|
|
16
|
+
export interface SkillAdapter {
|
|
17
|
+
global: string | null;
|
|
18
|
+
project: string | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface DiscoveryConfig {
|
|
22
|
+
rootPatterns: string[];
|
|
23
|
+
additionalIgnorePaths: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface SkillsConfig {
|
|
27
|
+
enabled: boolean;
|
|
28
|
+
adapters: Record<string, SkillAdapter>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface InstructionsConfig {
|
|
32
|
+
enabled: boolean;
|
|
33
|
+
maxFileBytes: number;
|
|
34
|
+
maxInjectionBytes: number;
|
|
35
|
+
maxVisibleBytes: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface RulesConfig {
|
|
39
|
+
enabled: boolean;
|
|
40
|
+
maxBundleBytes: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface UiConfig {
|
|
44
|
+
showStatus: boolean;
|
|
45
|
+
showWarnings: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface EffectiveConfig {
|
|
49
|
+
version: 1;
|
|
50
|
+
discovery: DiscoveryConfig;
|
|
51
|
+
skills: SkillsConfig;
|
|
52
|
+
instructions: InstructionsConfig;
|
|
53
|
+
rules: RulesConfig;
|
|
54
|
+
ui: UiConfig;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Where an effective value came from, including the policy that overrode it. */
|
|
58
|
+
export type ConfigProvenance = ConfigLayer | "disabled-by-global" | "clamped" | "untrusted";
|
|
59
|
+
|
|
60
|
+
export interface LoadedConfiguration {
|
|
61
|
+
config: EffectiveConfig;
|
|
62
|
+
diagnostics: Diagnostic[];
|
|
63
|
+
provenance: Record<string, ConfigProvenance>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface Workspace {
|
|
67
|
+
cwd: string;
|
|
68
|
+
root: string;
|
|
69
|
+
trusted: boolean;
|
|
70
|
+
matchedRootPattern?: string;
|
|
71
|
+
}
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { access, realpath, stat } from "node:fs/promises";
|
|
3
|
+
import {
|
|
4
|
+
buildIgnorePatterns,
|
|
5
|
+
describeIgnorePolicy,
|
|
6
|
+
matchProjectAdapterCandidates,
|
|
7
|
+
type ReadDirectory,
|
|
8
|
+
resolveGlobalAdapterCandidate,
|
|
9
|
+
} from "../discovery/adapters.js";
|
|
10
|
+
import { comparePaths, isInside, pathKey, resolveHome } from "../discovery/paths.js";
|
|
11
|
+
import { findWorkspaceRoot } from "../discovery/roots.js";
|
|
12
|
+
import { createDiagnostic, nativeErrorCode } from "../shared/diagnostics.js";
|
|
13
|
+
import type { Diagnostic, EffectiveConfig } from "../shared/types.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Filesystem overrides for the two discovery calls whose failures have no
|
|
17
|
+
* portable fixture. Production callers omit them and use the platform
|
|
18
|
+
* defaults; tests use them to exercise unreadable and failing filesystems.
|
|
19
|
+
*/
|
|
20
|
+
export interface DiscoveryFileSystemOverrides {
|
|
21
|
+
readonly readdir?: ReadDirectory | undefined;
|
|
22
|
+
readonly realpath?: ((target: string) => Promise<string>) | undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface SkillDiscoveryInput {
|
|
26
|
+
cwd: string;
|
|
27
|
+
config: EffectiveConfig;
|
|
28
|
+
fileSystem?: DiscoveryFileSystemOverrides | undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface SkillDiscoveryResult {
|
|
32
|
+
skillPaths: string[];
|
|
33
|
+
diagnostics: Diagnostic[];
|
|
34
|
+
durationMs: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type CandidateChannel = "global" | "project";
|
|
38
|
+
|
|
39
|
+
interface CanonicalPath {
|
|
40
|
+
readonly path: string | null;
|
|
41
|
+
readonly code: string | null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface CandidateContext {
|
|
45
|
+
readonly accepted: Map<string, string>;
|
|
46
|
+
readonly diagnostics: Diagnostic[];
|
|
47
|
+
readonly canonicalWorkspaceRoot: CanonicalPath;
|
|
48
|
+
readonly realpath: (target: string) => Promise<string>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface CandidateRequest {
|
|
52
|
+
readonly adapterName: string;
|
|
53
|
+
readonly channel: CandidateChannel;
|
|
54
|
+
readonly candidatePath: string;
|
|
55
|
+
readonly field: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Discovers physical skill roots and returns them to Pi. No skill file is
|
|
60
|
+
* ever read: Pi owns parsing, validation, precedence, and invocation.
|
|
61
|
+
*/
|
|
62
|
+
export async function discoverSkillRoots(
|
|
63
|
+
input: SkillDiscoveryInput,
|
|
64
|
+
): Promise<SkillDiscoveryResult> {
|
|
65
|
+
const startedAt = performance.now();
|
|
66
|
+
const diagnostics: Diagnostic[] = [];
|
|
67
|
+
|
|
68
|
+
if (!input.config.skills.enabled) {
|
|
69
|
+
diagnostics.push(
|
|
70
|
+
createDiagnostic("info", "skills-disabled", "Skill discovery is disabled by configuration."),
|
|
71
|
+
);
|
|
72
|
+
return { skillPaths: [], diagnostics, durationMs: elapsed(startedAt) };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const realpathImplementation = input.fileSystem?.realpath ?? realpath;
|
|
76
|
+
const workspace = await findWorkspaceRoot(input.cwd, input.config.discovery.rootPatterns);
|
|
77
|
+
const environment = process.env;
|
|
78
|
+
const home = resolveHome(environment);
|
|
79
|
+
const additionalIgnorePaths = input.config.discovery.additionalIgnorePaths;
|
|
80
|
+
const ignorePatterns = buildIgnorePatterns(additionalIgnorePaths);
|
|
81
|
+
diagnostics.push(
|
|
82
|
+
createDiagnostic(
|
|
83
|
+
"info",
|
|
84
|
+
"ignore-policy",
|
|
85
|
+
`Discovery root ${workspace.root}; ignoring ${describeIgnorePolicy(additionalIgnorePaths)}.`,
|
|
86
|
+
{ sourcePath: workspace.root },
|
|
87
|
+
),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const canonicalWorkspaceRoot = await canonicalPath(workspace.root, realpathImplementation);
|
|
91
|
+
if (canonicalWorkspaceRoot.path === null) {
|
|
92
|
+
diagnostics.push(
|
|
93
|
+
createDiagnostic(
|
|
94
|
+
"warning",
|
|
95
|
+
"workspace-root-unreadable",
|
|
96
|
+
`Workspace root ${workspace.root} could not be canonicalized (${canonicalWorkspaceRoot.code ?? "unknown error"}); project candidates are skipped because containment cannot be verified.`,
|
|
97
|
+
{ sourcePath: workspace.root },
|
|
98
|
+
),
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const context: CandidateContext = {
|
|
103
|
+
accepted: new Map<string, string>(),
|
|
104
|
+
diagnostics,
|
|
105
|
+
canonicalWorkspaceRoot,
|
|
106
|
+
realpath: realpathImplementation,
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
for (const adapterName of Object.keys(input.config.skills.adapters).sort(comparePaths)) {
|
|
110
|
+
const adapter = input.config.skills.adapters[adapterName];
|
|
111
|
+
if (adapter === undefined) {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (adapter.global !== null) {
|
|
115
|
+
await collectGlobalCandidate({
|
|
116
|
+
context,
|
|
117
|
+
adapterName,
|
|
118
|
+
value: adapter.global,
|
|
119
|
+
environment,
|
|
120
|
+
home,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
if (adapter.project !== null) {
|
|
124
|
+
await collectProjectCandidates({
|
|
125
|
+
context,
|
|
126
|
+
adapterName,
|
|
127
|
+
pattern: adapter.project,
|
|
128
|
+
workspaceRoot: workspace.root,
|
|
129
|
+
ignorePatterns,
|
|
130
|
+
readdir: input.fileSystem?.readdir,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const skillPaths = [...context.accepted.values()].sort(comparePaths);
|
|
136
|
+
return { skillPaths, diagnostics, durationMs: elapsed(startedAt) };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
interface CollectGlobalInput {
|
|
140
|
+
readonly context: CandidateContext;
|
|
141
|
+
readonly adapterName: string;
|
|
142
|
+
readonly value: string;
|
|
143
|
+
readonly environment: NodeJS.ProcessEnv;
|
|
144
|
+
readonly home: string;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async function collectGlobalCandidate(input: CollectGlobalInput): Promise<void> {
|
|
148
|
+
const field = `skills.adapters.${input.adapterName}.global`;
|
|
149
|
+
const resolved = resolveGlobalAdapterCandidate(input.value, input.environment, input.home);
|
|
150
|
+
if (resolved.candidatePath === null) {
|
|
151
|
+
input.context.diagnostics.push(
|
|
152
|
+
createDiagnostic(
|
|
153
|
+
"warning",
|
|
154
|
+
"adapter-environment-missing",
|
|
155
|
+
`Adapter "${input.adapterName}" references environment variable ${resolved.missingVariable ?? "unknown"}, which is not set; the global candidate is skipped.`,
|
|
156
|
+
{ field },
|
|
157
|
+
),
|
|
158
|
+
);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
await addCandidate(input.context, {
|
|
162
|
+
adapterName: input.adapterName,
|
|
163
|
+
channel: "global",
|
|
164
|
+
candidatePath: resolved.candidatePath,
|
|
165
|
+
field,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
interface CollectProjectInput {
|
|
170
|
+
readonly context: CandidateContext;
|
|
171
|
+
readonly adapterName: string;
|
|
172
|
+
readonly pattern: string;
|
|
173
|
+
readonly workspaceRoot: string;
|
|
174
|
+
readonly ignorePatterns: readonly string[];
|
|
175
|
+
readonly readdir: ReadDirectory | undefined;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function collectProjectCandidates(input: CollectProjectInput): Promise<void> {
|
|
179
|
+
const field = `skills.adapters.${input.adapterName}.project`;
|
|
180
|
+
const match = await matchProjectAdapterCandidates({
|
|
181
|
+
pattern: input.pattern,
|
|
182
|
+
workspaceRoot: input.workspaceRoot,
|
|
183
|
+
ignorePatterns: input.ignorePatterns,
|
|
184
|
+
readdir: input.readdir,
|
|
185
|
+
});
|
|
186
|
+
if (match.unsafePattern !== null) {
|
|
187
|
+
input.context.diagnostics.push(
|
|
188
|
+
createDiagnostic(
|
|
189
|
+
"error",
|
|
190
|
+
"unsafe-pattern",
|
|
191
|
+
`Adapter "${input.adapterName}" project pattern is not a safe workspace-relative glob: ${match.unsafePattern}`,
|
|
192
|
+
{ field },
|
|
193
|
+
),
|
|
194
|
+
);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (match.failure !== null) {
|
|
198
|
+
input.context.diagnostics.push(
|
|
199
|
+
createDiagnostic(
|
|
200
|
+
"error",
|
|
201
|
+
"adapter-glob-failed",
|
|
202
|
+
`Adapter "${input.adapterName}" project pattern "${input.pattern}" could not be evaluated (${match.failure.code ?? "unknown error"}): ${match.failure.message}`,
|
|
203
|
+
{ field, sourcePath: input.workspaceRoot },
|
|
204
|
+
),
|
|
205
|
+
);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
for (const unreadable of match.unreadableDirectories) {
|
|
209
|
+
input.context.diagnostics.push(
|
|
210
|
+
createDiagnostic(
|
|
211
|
+
"warning",
|
|
212
|
+
"candidate-scan-unreadable",
|
|
213
|
+
`Adapter "${input.adapterName}" project scan could not read ${unreadable.directory} (${unreadable.code ?? "unknown error"}); skills below it are skipped.`,
|
|
214
|
+
{ field },
|
|
215
|
+
),
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
if (match.candidates.length === 0) {
|
|
219
|
+
input.context.diagnostics.push(
|
|
220
|
+
createDiagnostic(
|
|
221
|
+
"info",
|
|
222
|
+
"project-pattern-empty",
|
|
223
|
+
`Adapter "${input.adapterName}" pattern "${input.pattern}" matched no directory below ${input.workspaceRoot}.`,
|
|
224
|
+
{ field, sourcePath: input.workspaceRoot },
|
|
225
|
+
),
|
|
226
|
+
);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
for (const candidatePath of match.candidates) {
|
|
230
|
+
await addCandidate(input.context, {
|
|
231
|
+
adapterName: input.adapterName,
|
|
232
|
+
channel: "project",
|
|
233
|
+
candidatePath,
|
|
234
|
+
field,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Validates one candidate and records it once per physical directory. A single
|
|
241
|
+
* invalid candidate never fails the pass.
|
|
242
|
+
*/
|
|
243
|
+
async function addCandidate(context: CandidateContext, request: CandidateRequest): Promise<void> {
|
|
244
|
+
const description = `Adapter "${request.adapterName}" ${request.channel} candidate ${request.candidatePath}`;
|
|
245
|
+
let stats: Awaited<ReturnType<typeof stat>>;
|
|
246
|
+
try {
|
|
247
|
+
stats = await stat(request.candidatePath);
|
|
248
|
+
} catch (error) {
|
|
249
|
+
const code = nativeErrorCode(error);
|
|
250
|
+
if (code === "ENOENT" || code === "ENOTDIR") {
|
|
251
|
+
context.diagnostics.push(
|
|
252
|
+
createDiagnostic("info", "candidate-missing", `${description} does not exist.`, {
|
|
253
|
+
field: request.field,
|
|
254
|
+
}),
|
|
255
|
+
);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
context.diagnostics.push(
|
|
259
|
+
createDiagnostic(
|
|
260
|
+
"warning",
|
|
261
|
+
"candidate-unreadable",
|
|
262
|
+
`${description} could not be inspected (${code ?? "unknown error"}).`,
|
|
263
|
+
{ field: request.field },
|
|
264
|
+
),
|
|
265
|
+
);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (!stats.isDirectory()) {
|
|
269
|
+
context.diagnostics.push(
|
|
270
|
+
createDiagnostic("warning", "candidate-not-directory", `${description} is not a directory.`, {
|
|
271
|
+
field: request.field,
|
|
272
|
+
}),
|
|
273
|
+
);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
await access(request.candidatePath, constants.R_OK | constants.X_OK);
|
|
278
|
+
} catch (error) {
|
|
279
|
+
context.diagnostics.push(
|
|
280
|
+
createDiagnostic(
|
|
281
|
+
"warning",
|
|
282
|
+
"candidate-unreadable",
|
|
283
|
+
`${description} is not readable (${nativeErrorCode(error) ?? "unknown error"}).`,
|
|
284
|
+
{ field: request.field },
|
|
285
|
+
),
|
|
286
|
+
);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const canonical = await canonicalPath(request.candidatePath, context.realpath);
|
|
291
|
+
if (canonical.path === null) {
|
|
292
|
+
context.diagnostics.push(
|
|
293
|
+
createDiagnostic(
|
|
294
|
+
"warning",
|
|
295
|
+
"candidate-unreadable",
|
|
296
|
+
`${description} could not be canonicalized (${canonical.code ?? "unknown error"}).`,
|
|
297
|
+
{ field: request.field },
|
|
298
|
+
),
|
|
299
|
+
);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (request.channel === "project") {
|
|
304
|
+
const workspaceRoot = context.canonicalWorkspaceRoot.path;
|
|
305
|
+
if (workspaceRoot === null) {
|
|
306
|
+
context.diagnostics.push(
|
|
307
|
+
createDiagnostic(
|
|
308
|
+
"warning",
|
|
309
|
+
"candidate-containment-unavailable",
|
|
310
|
+
`${description} was skipped because the workspace root could not be canonicalized and containment cannot be verified.`,
|
|
311
|
+
{ field: request.field },
|
|
312
|
+
),
|
|
313
|
+
);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (canonical.path !== workspaceRoot && !isInside(workspaceRoot, canonical.path)) {
|
|
317
|
+
context.diagnostics.push(
|
|
318
|
+
createDiagnostic(
|
|
319
|
+
"warning",
|
|
320
|
+
"candidate-outside-workspace",
|
|
321
|
+
`${description} resolves outside the workspace root and is skipped.`,
|
|
322
|
+
{ field: request.field },
|
|
323
|
+
),
|
|
324
|
+
);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const key = pathKey(canonical.path);
|
|
330
|
+
if (context.accepted.has(key)) {
|
|
331
|
+
context.diagnostics.push(
|
|
332
|
+
createDiagnostic(
|
|
333
|
+
"info",
|
|
334
|
+
"candidate-duplicate",
|
|
335
|
+
`${description} resolves to the already discovered root ${canonical.path}.`,
|
|
336
|
+
{ field: request.field },
|
|
337
|
+
),
|
|
338
|
+
);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
context.accepted.set(key, canonical.path);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async function canonicalPath(
|
|
345
|
+
target: string,
|
|
346
|
+
implementation: (target: string) => Promise<string>,
|
|
347
|
+
): Promise<CanonicalPath> {
|
|
348
|
+
try {
|
|
349
|
+
return { path: await implementation(target), code: null };
|
|
350
|
+
} catch (error) {
|
|
351
|
+
if (error instanceof Error) {
|
|
352
|
+
return { path: null, code: nativeErrorCode(error) };
|
|
353
|
+
}
|
|
354
|
+
throw error;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function elapsed(startedAt: number): number {
|
|
359
|
+
return Math.round(performance.now() - startedAt);
|
|
360
|
+
}
|