@maheidem/pi-delegate 0.8.0 → 0.10.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.
@@ -6,7 +6,7 @@
6
6
  import * as fs from "node:fs";
7
7
  import * as path from "node:path";
8
8
  import { getAgentDir } from "@earendil-works/pi-coding-agent";
9
- import type { RoleName } from "./types.ts";
9
+ import type { ChildConfig, RoleName, ResolvedChildConfig } from "./types.ts";
10
10
  import { ROLE_NAMES } from "./types.ts";
11
11
 
12
12
  export interface DelegateConfigV1 {
@@ -46,6 +46,11 @@ export interface DelegateConfigV1 {
46
46
  timeoutMs: number;
47
47
  maxPerRun: number;
48
48
  };
49
+ /**
50
+ * Project-level child config (from the project overlay only — the user-wide
51
+ * file never carries this). Validated field-by-field; defaults to absent.
52
+ */
53
+ child?: ChildConfig;
49
54
  }
50
55
 
51
56
  export const DEFAULT_DELEGATE_CONFIG: DelegateConfigV1 = {
@@ -118,7 +123,8 @@ const MAX_VALUES: Partial<Record<keyof DelegateConfigV1, number>> = {
118
123
  */
119
124
  export function normalizeConfig(
120
125
  raw: unknown,
121
- ): { config: DelegateConfigV1; unknownKeys: string[] } {
126
+ ): { config: DelegateConfigV1; unknownKeys: string[]; diagnostics: string[] } {
127
+ const diagnostics: string[] = [];
122
128
  const base: DelegateConfigV1 = {
123
129
  ...DEFAULT_DELEGATE_CONFIG,
124
130
  // nested blocks are shared references in the default — copy so a
@@ -149,16 +155,16 @@ export function normalizeConfig(
149
155
  extras[key] = record[key];
150
156
  continue;
151
157
  }
152
- applyKnownField(base, key, record[key]);
158
+ applyKnownField(base, key, record[key], diagnostics);
153
159
  }
154
160
  } else if (raw !== undefined && raw !== null) {
155
161
  throw new Error("config must be a JSON object");
156
162
  }
157
163
 
158
- return { config: base, unknownKeys };
164
+ return { config: base, unknownKeys, diagnostics };
159
165
  }
160
166
 
161
- function applyKnownField(config: DelegateConfigV1, key: string, value: unknown): void {
167
+ function applyKnownField(config: DelegateConfigV1, key: string, value: unknown, diagnostics: string[] = []): void {
162
168
  if (key === "schemaVersion") return;
163
169
  if (key === "defaultRole") {
164
170
  if ((ROLE_NAMES as readonly string[]).includes(String(value))) {
@@ -175,32 +181,66 @@ function applyKnownField(config: DelegateConfigV1, key: string, value: unknown):
175
181
  return;
176
182
  }
177
183
  if (key === "progressReports") {
178
- if (!value || typeof value !== "object" || Array.isArray(value)) return;
184
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
185
+ diagnostics.push("progressReports: expected an object, ignored");
186
+ return;
187
+ }
179
188
  const raw = value as Record<string, unknown>;
180
- if (typeof raw.enabled === "boolean") config.progressReports.enabled = raw.enabled;
181
- if (Number.isFinite(Number(raw.minToolMs))) {
182
- config.progressReports.minToolMs = Math.min(3_600_000, Math.max(5_000, Number(raw.minToolMs)));
189
+ if ("enabled" in raw) {
190
+ if (typeof raw.enabled === "boolean") config.progressReports.enabled = raw.enabled;
191
+ else diagnostics.push("progressReports.enabled: not a boolean, ignored");
192
+ }
193
+ if ("minToolMs" in raw) {
194
+ const n = Number(raw.minToolMs);
195
+ if (Number.isFinite(n)) config.progressReports.minToolMs = Math.min(3_600_000, Math.max(5_000, n));
196
+ else diagnostics.push("progressReports.minToolMs: not a finite number, ignored");
183
197
  }
184
- if (Number.isFinite(Number(raw.intervalMs))) {
185
- config.progressReports.intervalMs = Math.min(3_600_000, Math.max(60_000, Number(raw.intervalMs)));
198
+ if ("intervalMs" in raw) {
199
+ const n = Number(raw.intervalMs);
200
+ if (Number.isFinite(n)) config.progressReports.intervalMs = Math.min(3_600_000, Math.max(60_000, n));
201
+ else diagnostics.push("progressReports.intervalMs: not a finite number, ignored");
186
202
  }
187
- if (Number.isFinite(Number(raw.minGapMs))) {
188
- config.progressReports.minGapMs = Math.min(1_800_000, Math.max(30_000, Number(raw.minGapMs)));
203
+ if ("minGapMs" in raw) {
204
+ const n = Number(raw.minGapMs);
205
+ if (Number.isFinite(n)) config.progressReports.minGapMs = Math.min(1_800_000, Math.max(30_000, n));
206
+ else diagnostics.push("progressReports.minGapMs: not a finite number, ignored");
189
207
  }
190
- if (Number.isFinite(Number(raw.maxPerRun))) {
191
- config.progressReports.maxPerRun = Math.min(20, Math.max(1, Math.trunc(Number(raw.maxPerRun))));
208
+ if ("maxPerRun" in raw) {
209
+ const n = Number(raw.maxPerRun);
210
+ if (Number.isFinite(n)) config.progressReports.maxPerRun = Math.min(20, Math.max(1, Math.trunc(n)));
211
+ else diagnostics.push("progressReports.maxPerRun: not a finite number, ignored");
212
+ }
213
+ for (const k of Object.keys(raw)) {
214
+ if (!["enabled","minToolMs","intervalMs","minGapMs","maxPerRun"].includes(k)) {
215
+ diagnostics.push(`progressReports.${k}: unknown nested key, ignored`);
216
+ }
192
217
  }
193
218
  return;
194
219
  }
195
220
  if (key === "askParent") {
196
- if (!value || typeof value !== "object" || Array.isArray(value)) return;
221
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
222
+ diagnostics.push("askParent: expected an object, ignored");
223
+ return;
224
+ }
197
225
  const raw = value as Record<string, unknown>;
198
- if (typeof raw.enabled === "boolean") config.askParent.enabled = raw.enabled;
199
- if (Number.isFinite(Number(raw.timeoutMs))) {
200
- config.askParent.timeoutMs = Math.min(3_600_000, Math.max(5_000, Number(raw.timeoutMs)));
226
+ if ("enabled" in raw) {
227
+ if (typeof raw.enabled === "boolean") config.askParent.enabled = raw.enabled;
228
+ else diagnostics.push("askParent.enabled: not a boolean, ignored");
201
229
  }
202
- if (Number.isFinite(Number(raw.maxPerRun))) {
203
- config.askParent.maxPerRun = Math.min(20, Math.max(1, Math.trunc(Number(raw.maxPerRun))));
230
+ if ("timeoutMs" in raw) {
231
+ const n = Number(raw.timeoutMs);
232
+ if (Number.isFinite(n)) config.askParent.timeoutMs = Math.min(3_600_000, Math.max(5_000, n));
233
+ else diagnostics.push("askParent.timeoutMs: not a finite number, ignored");
234
+ }
235
+ if ("maxPerRun" in raw) {
236
+ const n = Number(raw.maxPerRun);
237
+ if (Number.isFinite(n)) config.askParent.maxPerRun = Math.min(20, Math.max(1, Math.trunc(n)));
238
+ else diagnostics.push("askParent.maxPerRun: not a finite number, ignored");
239
+ }
240
+ for (const k of Object.keys(raw)) {
241
+ if (!["enabled","timeoutMs","maxPerRun"].includes(k)) {
242
+ diagnostics.push(`askParent.${k}: unknown nested key, ignored`);
243
+ }
204
244
  }
205
245
  return;
206
246
  }
@@ -214,6 +254,131 @@ function applyKnownField(config: DelegateConfigV1, key: string, value: unknown):
214
254
  ) as never;
215
255
  }
216
256
 
257
+ /**
258
+ * Validate a project-overlay `child` object field-by-field. Unknown keys are
259
+ * diagnosed and dropped; invalid values are dropped with diagnostics; a
260
+ * non-object is dropped entirely (returns undefined). Never throws.
261
+ */
262
+ export function validateChildConfig(raw: unknown, diagnostics: string[] = []): ChildConfig | undefined {
263
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
264
+ if (raw !== undefined && raw !== null) diagnostics.push("child: expected an object, ignored");
265
+ return undefined;
266
+ }
267
+ const record = raw as Record<string, unknown>;
268
+ const child: ChildConfig = {};
269
+ for (const key of Object.keys(record)) {
270
+ const value = record[key];
271
+ if (key === "model") {
272
+ if (typeof value === "string" && value.trim()) child.model = value.trim();
273
+ else diagnostics.push("child.model: expected a non-empty string, ignored");
274
+ } else if (key === "thinking") {
275
+ if (typeof value === "string" && value.trim()) child.thinking = value.trim();
276
+ else diagnostics.push("child.thinking: expected a non-empty string, ignored");
277
+ } else if (key === "tools") {
278
+ if (Array.isArray(value)) {
279
+ const tools: string[] = [];
280
+ let dropped = 0;
281
+ for (const item of value) {
282
+ if (typeof item === "string" && item.trim()) tools.push(item.trim());
283
+ else dropped += 1;
284
+ }
285
+ if (dropped > 0) diagnostics.push(`child.tools: dropped ${dropped} non-string or empty entr${dropped === 1 ? "y" : "ies"}`);
286
+ child.tools = tools;
287
+ } else {
288
+ diagnostics.push("child.tools: expected an array of strings, ignored");
289
+ }
290
+ } else if (key === "trustProject") {
291
+ if (typeof value === "boolean") child.trustProject = value;
292
+ else diagnostics.push("child.trustProject: expected a boolean, ignored");
293
+ } else if (key === "context") {
294
+ if (value === "standard" || value === "minimal") child.context = value;
295
+ else diagnostics.push("child.context: expected \"standard\" or \"minimal\", ignored");
296
+ } else {
297
+ diagnostics.push(`child.${key}: unknown nested key, ignored`);
298
+ }
299
+ }
300
+ return child;
301
+ }
302
+
303
+ export interface ResolveChildConfigOptions {
304
+ /** Per-invocation values (tool args) — highest precedence. */
305
+ perInvocation?: { model?: string; thinking?: string };
306
+ /** The user-wide base config; the project overlay merges on top of it. */
307
+ baseConfig?: DelegateConfigV1;
308
+ /** Project root to read .pi/delegate/config.json from (no overlay when absent). */
309
+ projectRoot?: string;
310
+ /** Parent's current model ("provider/model-id"). */
311
+ parentModel: string;
312
+ /** Parent's thinking level. */
313
+ parentThinking?: string;
314
+ /** Registered parent tool names (unknown child.tools names are dropped with diagnostics). */
315
+ registeredTools?: readonly string[];
316
+ }
317
+
318
+ /**
319
+ * Resolve the effective child config for one run. Resolution order:
320
+ * per-invocation (tool arg) > project child config > parent's current.
321
+ * The project overlay supplies the tools allowlist and context/trust
322
+ * pins; the parent layer supplies the inherited model and thinking.
323
+ * Unknown tool names are dropped with diagnostics, never thrown.
324
+ */
325
+ export function resolveChildConfig(options: ResolveChildConfigOptions): ResolvedChildConfig {
326
+ const diagnostics: string[] = [];
327
+ let projectChild: ChildConfig | undefined;
328
+ if (options.projectRoot) {
329
+ const overlay = applyProjectOverlay(options.baseConfig ?? DEFAULT_DELEGATE_CONFIG, options.projectRoot);
330
+ projectChild = overlay.child;
331
+ diagnostics.push(...(overlay.projectDiagnostics ?? []));
332
+ }
333
+
334
+ const perModel = typeof options.perInvocation?.model === "string" ? options.perInvocation.model.trim() : "";
335
+ const perThinking = typeof options.perInvocation?.thinking === "string" ? options.perInvocation.thinking.trim() : "";
336
+ const projectModel = projectChild?.model?.trim() ?? "";
337
+ const parentModel = (options.parentModel ?? "").trim();
338
+
339
+ let model: string | undefined;
340
+ let modelSource: "per-invocation" | "project" | "parent" | undefined;
341
+ if (perModel) {
342
+ model = perModel;
343
+ modelSource = "per-invocation";
344
+ } else if (projectModel) {
345
+ model = projectModel;
346
+ modelSource = "project";
347
+ } else if (parentModel) {
348
+ model = parentModel;
349
+ modelSource = "parent";
350
+ }
351
+
352
+ const projectThinking = projectChild?.thinking?.trim() ?? "";
353
+ const thinking = perThinking || projectThinking || (options.parentThinking?.trim() || undefined);
354
+
355
+ let tools: string[] | undefined;
356
+ if (projectChild?.tools !== undefined) {
357
+ if (options.registeredTools) {
358
+ const registered = new Set(options.registeredTools);
359
+ const unknown = projectChild.tools.filter((n) => !registered.has(n));
360
+ if (unknown.length > 0) diagnostics.push(`child.tools: unknown tool name(s) dropped: ${unknown.join(", ")}`);
361
+ tools = projectChild.tools.filter((n) => registered.has(n));
362
+ } else {
363
+ tools = [...projectChild.tools];
364
+ }
365
+ }
366
+
367
+ const trustProject = projectChild?.trustProject;
368
+ const context = projectChild?.context ?? "standard";
369
+
370
+ const resolved: ResolvedChildConfig = { context };
371
+ if (model) {
372
+ resolved.model = model;
373
+ resolved.modelSource = modelSource;
374
+ }
375
+ if (thinking) resolved.thinking = thinking;
376
+ if (tools !== undefined) resolved.tools = tools;
377
+ if (trustProject !== undefined) resolved.trustProject = trustProject;
378
+ if (diagnostics.length > 0) resolved.diagnostics = diagnostics;
379
+ return resolved;
380
+ }
381
+
217
382
  /** The config file of this extension: `<agent dir>/delegate/config.json`. */
218
383
  export const CONFIG_PATH = path.join(getAgentDir(), "delegate", "config.json");
219
384
 
@@ -422,11 +587,12 @@ export function projectConfigPath(projectRoot: string): string {
422
587
  export function applyProjectOverlay(
423
588
  base: DelegateConfigV1,
424
589
  projectRoot: string,
425
- ): DelegateConfigV1 & { projectOverrides: string[]; projectSetKeys: string[]; projectCorrupt?: string } {
426
- const result: DelegateConfigV1 & { projectOverrides: string[]; projectSetKeys: string[]; projectCorrupt?: string } = {
590
+ ): DelegateConfigV1 & { projectOverrides: string[]; projectSetKeys: string[]; projectCorrupt?: string; projectDiagnostics: string[] } {
591
+ const result: DelegateConfigV1 & { projectOverrides: string[]; projectSetKeys: string[]; projectCorrupt?: string; projectDiagnostics: string[] } = {
427
592
  ...base,
428
593
  projectOverrides: [],
429
594
  projectSetKeys: [],
595
+ projectDiagnostics: [],
430
596
  };
431
597
  const pPath = projectConfigPath(projectRoot);
432
598
  let raw: unknown;
@@ -443,12 +609,25 @@ export function applyProjectOverlay(
443
609
  const known = new Set<string>(Object.keys(DEFAULT_DELEGATE_CONFIG));
444
610
  const overrides: string[] = [];
445
611
  const setKeys: string[] = [];
612
+ const childDiagnostics: string[] = [];
446
613
  for (const key of Object.keys(record)) {
447
614
  if (key === "schemaVersion") continue;
448
615
  if (!knownKey(key)) continue; // unknown keys in project overlay ignored
449
616
  // File truth: present in the project file counts as set even when the
450
617
  // normalized value equals the base value (the override list would omit it).
451
618
  setKeys.push(key);
619
+ if (key === "child") {
620
+ // Project-level child config: validated field-by-field, merged
621
+ // per-field over the base (only fields present in the file replace).
622
+ const validated = validateChildConfig(record[key], childDiagnostics);
623
+ if (validated === undefined) continue; // not an object: dropped entirely
624
+ const merged: ChildConfig = { ...(base.child ?? {}), ...validated };
625
+ if (Object.keys(merged).length > 0) {
626
+ result.child = merged;
627
+ if (JSON.stringify(merged) !== JSON.stringify(base.child ?? {})) overrides.push("child");
628
+ }
629
+ continue;
630
+ }
452
631
  try {
453
632
  const probe: DelegateConfigV1 = { ...DEFAULT_DELEGATE_CONFIG };
454
633
  applyKnownField(probe, key, record[key]);
@@ -463,11 +642,12 @@ export function applyProjectOverlay(
463
642
  }
464
643
  result.projectOverrides = overrides;
465
644
  result.projectSetKeys = setKeys;
645
+ if (childDiagnostics.length > 0) result.projectDiagnostics = childDiagnostics;
466
646
  return result;
467
647
  }
468
648
 
469
649
  function knownKey(key: string): boolean {
470
- return (Object.keys(DEFAULT_DELEGATE_CONFIG) as string[]).includes(key);
650
+ return (Object.keys(DEFAULT_DELEGATE_CONFIG) as string[]).includes(key) || key === "child";
471
651
  }
472
652
 
473
653
  /**
@@ -484,10 +664,11 @@ export function loadConfigCascade(
484
664
  return { ...base, projectOverrides: [], projectSetKeys: [] };
485
665
  }
486
666
  const overlay = applyProjectOverlay(base.config, projectRoot);
487
- const { projectOverrides, projectSetKeys, projectCorrupt, ...rest } = overlay as unknown as DelegateConfigV1 & {
667
+ const { projectOverrides, projectSetKeys, projectCorrupt, projectDiagnostics, ...rest } = overlay as unknown as DelegateConfigV1 & {
488
668
  projectOverrides: string[];
489
669
  projectSetKeys: string[];
490
670
  projectCorrupt?: string;
671
+ projectDiagnostics?: string[];
491
672
  };
492
673
  const config: DelegateConfigV1 = { ...(rest as unknown as DelegateConfigV1) };
493
674
  return {