iterate-plugin 3.5.6 → 3.6.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 +64 -20
- package/README.zh-CN.md +53 -17
- package/dist/approval-gate.js +143 -23
- package/dist/atomic-fs.js +148 -17
- package/dist/config-loader.js +509 -34
- package/dist/config-write.js +83 -22
- package/dist/evidence.js +141 -87
- package/dist/file-lock.js +165 -0
- package/dist/index.js +54 -28
- package/dist/jobs.js +56 -11
- package/dist/live.js +52 -13
- package/dist/meta-review.js +132 -28
- package/dist/method-scope.js +31 -8
- package/dist/review-scope.js +32 -9
- package/dist/review.js +96 -17
- package/dist/session-hooks.js +55 -13
- package/dist/skill-prompt.js +417 -67
- package/dist/tools/checkpoint.js +85 -19
- package/dist/tools/config.js +44 -2
- package/dist/tools/context.js +22 -5
- package/dist/tools/decision-log.js +122 -27
- package/dist/tools/defense-events.js +90 -31
- package/dist/tools/defense-store.js +28 -3
- package/dist/tools/experience-bank.js +108 -43
- package/dist/tools/experience-store.js +54 -4
- package/dist/tools/fix.js +347 -119
- package/dist/tools/history.js +68 -14
- package/dist/tools/present.js +80 -0
- package/dist/tools/prune.js +156 -53
- package/dist/tools/quality-gate.js +52 -2
- package/dist/tools/quality-store.js +17 -1
- package/dist/tools/review.js +158 -7
- package/dist/tools/transcript.js +173 -36
- package/dist/tools/triage.js +67 -12
- package/dist/tools/validate.js +115 -24
- package/dist/transcript.js +267 -4
- package/lib/client.js +945 -181
- package/lib/parse.js +760 -57
- package/package.json +15 -12
- package/src/approval-gate.ts +160 -23
- package/src/atomic-fs.ts +152 -17
- package/src/client/index.ts +600 -129
- package/src/config-loader.ts +542 -35
- package/src/config-write.ts +108 -26
- package/src/evidence.ts +194 -92
- package/src/file-lock.ts +184 -0
- package/src/index.ts +53 -29
- package/src/jobs.ts +63 -12
- package/src/live.ts +48 -11
- package/src/meta-review.ts +151 -34
- package/src/method-scope.ts +31 -8
- package/src/review-scope.ts +33 -10
- package/src/review.ts +96 -17
- package/src/session-hooks.ts +54 -12
- package/src/skill-prompt.ts +417 -67
- package/src/tools/checkpoint.ts +81 -19
- package/src/tools/config.ts +44 -2
- package/src/tools/context.ts +25 -5
- package/src/tools/decision-log.ts +127 -22
- package/src/tools/defense-events.ts +87 -31
- package/src/tools/defense-store.ts +30 -3
- package/src/tools/experience-bank.ts +112 -44
- package/src/tools/experience-store.ts +54 -5
- package/src/tools/fix.ts +258 -20
- package/src/tools/history.ts +74 -15
- package/src/tools/present.ts +89 -0
- package/src/tools/prune.ts +160 -51
- package/src/tools/quality-gate.ts +57 -2
- package/src/tools/quality-store.ts +18 -1
- package/src/tools/review.ts +155 -6
- package/src/tools/transcript.ts +178 -36
- package/src/tools/triage.ts +70 -13
- package/src/tools/validate.ts +138 -32
- package/src/transcript.ts +279 -4
- package/src/types.ts +29 -1
package/dist/config-loader.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { existsSync, readFileSync, realpathSync } from 'node:fs';
|
|
1
|
+
import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
2
2
|
import { homedir } from 'node:os';
|
|
3
|
-
import { join, resolve, sep } from 'node:path';
|
|
3
|
+
import { basename, dirname, join, resolve, sep } from 'node:path';
|
|
4
4
|
import yaml from 'js-yaml';
|
|
5
5
|
/**
|
|
6
6
|
* Load and parse iterate.config.yaml from the project root.
|
|
@@ -104,6 +104,10 @@ export function mergeConfig(base, override) {
|
|
|
104
104
|
* of the built-in defaults ("Master + Overrides"). Never returns null — a
|
|
105
105
|
* project without a config file simply runs on the defaults (with an empty
|
|
106
106
|
* validation command set, so nothing untrusted can ever execute).
|
|
107
|
+
*
|
|
108
|
+
* The merged result is FIELD-COERCED (see {@link coerceKnownFields}): YAML has
|
|
109
|
+
* no schema, so a hand-edited `max_rounds: "ten"` falls back to the default
|
|
110
|
+
* per field instead of flowing into the review loop as garbage.
|
|
107
111
|
*/
|
|
108
112
|
export function loadEffectiveConfig(projectRoot) {
|
|
109
113
|
const override = loadConfig(projectRoot);
|
|
@@ -111,8 +115,110 @@ export function loadEffectiveConfig(projectRoot) {
|
|
|
111
115
|
return { config: defaultConfig(), source: 'defaults', override: null };
|
|
112
116
|
}
|
|
113
117
|
const merged = mergeConfig(defaultConfig(), override);
|
|
118
|
+
coerceKnownFields(merged);
|
|
114
119
|
return { config: merged, source: 'override', override };
|
|
115
120
|
}
|
|
121
|
+
/** True for a plain object (not null, not an array). */
|
|
122
|
+
function isPlainObject(v) {
|
|
123
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
124
|
+
}
|
|
125
|
+
/** A positive integer (rounds, line budgets, chunk sizes). */
|
|
126
|
+
function isPositiveInt(v) {
|
|
127
|
+
return typeof v === 'number' && Number.isInteger(v) && v > 0;
|
|
128
|
+
}
|
|
129
|
+
/** A non-negative integer (thresholds where 0 means "no limit"). */
|
|
130
|
+
function isNonNegativeInt(v) {
|
|
131
|
+
return typeof v === 'number' && Number.isInteger(v) && v >= 0;
|
|
132
|
+
}
|
|
133
|
+
/** An array of strings; `nonEmptyItems` also rejects blank entries. */
|
|
134
|
+
function isStringArray(v, nonEmptyItems = false) {
|
|
135
|
+
return (Array.isArray(v) &&
|
|
136
|
+
v.every((x) => typeof x === 'string' && (!nonEmptyItems || x.trim().length > 0)));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Coerce the KNOWN config fields back to their documented types, falling back
|
|
140
|
+
* to the built-in default PER FIELD when the on-disk value is garbage.
|
|
141
|
+
*
|
|
142
|
+
* YAML has no schema: `max_rounds: "ten"` used to flow straight into the
|
|
143
|
+
* review loop (which then silently ran zero rounds), `dimensions: "all"`
|
|
144
|
+
* replaced the dimension list with a string, and a garbage
|
|
145
|
+
* `observatory.approval` reached the approval gate unchecked. Free-form
|
|
146
|
+
* sections (`onboarding`, `personalization`) and unknown project keys are NOT
|
|
147
|
+
* touched beyond requiring them to be objects. Mutates `raw` in place — only
|
|
148
|
+
* safe on freshly-merged objects (see {@link loadEffectiveConfig}).
|
|
149
|
+
*/
|
|
150
|
+
function coerceKnownFields(raw) {
|
|
151
|
+
const d = defaultConfig();
|
|
152
|
+
if (typeof raw.goal !== 'string' || raw.goal.trim().length === 0)
|
|
153
|
+
raw.goal = d.goal;
|
|
154
|
+
if (!isPositiveInt(raw.max_rounds))
|
|
155
|
+
raw.max_rounds = d.max_rounds;
|
|
156
|
+
if (raw.language !== 'zh' && raw.language !== 'en')
|
|
157
|
+
raw.language = d.language;
|
|
158
|
+
if (!isStringArray(raw.dimensions, true))
|
|
159
|
+
raw.dimensions = d.dimensions;
|
|
160
|
+
// `reasoning_effort` is optional (absent → provider default). Present but
|
|
161
|
+
// invalid → drop back to absent rather than inventing a value.
|
|
162
|
+
if ('reasoning_effort' in raw &&
|
|
163
|
+
raw.reasoning_effort !== 'low' && raw.reasoning_effort !== 'medium' && raw.reasoning_effort !== 'high') {
|
|
164
|
+
delete raw.reasoning_effort;
|
|
165
|
+
}
|
|
166
|
+
// A scalar/null value wholesale-replaces its default section during the
|
|
167
|
+
// merge — restore a full default section, then coerce its members.
|
|
168
|
+
const section = (key) => {
|
|
169
|
+
if (!isPlainObject(raw[key]))
|
|
170
|
+
raw[key] = { ...d[key] };
|
|
171
|
+
return raw[key];
|
|
172
|
+
};
|
|
173
|
+
const review = section('review');
|
|
174
|
+
if (review.scope !== 'full' && review.scope !== 'changed-only') {
|
|
175
|
+
review.scope = d.review.scope;
|
|
176
|
+
}
|
|
177
|
+
const atomic = section('atomic');
|
|
178
|
+
if (!isPositiveInt(atomic.max_lines))
|
|
179
|
+
atomic.max_lines = d.atomic.max_lines;
|
|
180
|
+
if (!isNonNegativeInt(atomic.max_adjacent_methods)) {
|
|
181
|
+
atomic.max_adjacent_methods = d.atomic.max_adjacent_methods;
|
|
182
|
+
}
|
|
183
|
+
const git = section('git');
|
|
184
|
+
if (typeof git.target_branch !== 'string' || git.target_branch.length === 0) {
|
|
185
|
+
git.target_branch = d.git.target_branch;
|
|
186
|
+
}
|
|
187
|
+
for (const key of ['use_worktree', 'push_per_round', 'auto_merge']) {
|
|
188
|
+
if (typeof git[key] !== 'boolean')
|
|
189
|
+
git[key] = d.git[key];
|
|
190
|
+
}
|
|
191
|
+
const validation = section('validation');
|
|
192
|
+
if (!isStringArray(validation.command_whitelist)) {
|
|
193
|
+
validation.command_whitelist = d.validation.command_whitelist;
|
|
194
|
+
}
|
|
195
|
+
// `commands` is an allow-list (module → command[]): a malformed entry must
|
|
196
|
+
// fall back to the default (empty) so an invalid shape can never smuggle a
|
|
197
|
+
// command into the runtime allow-list.
|
|
198
|
+
if (!isPlainObject(validation.commands) || !Object.values(validation.commands).every((v) => isStringArray(v))) {
|
|
199
|
+
validation.commands = d.validation.commands;
|
|
200
|
+
}
|
|
201
|
+
const reviewer = section('reviewer');
|
|
202
|
+
const dReviewer = d.reviewer;
|
|
203
|
+
for (const key of ['output_schema_validation', 'evidence_validation', 'coverage_validation']) {
|
|
204
|
+
if (typeof reviewer[key] !== 'boolean')
|
|
205
|
+
reviewer[key] = dReviewer[key];
|
|
206
|
+
}
|
|
207
|
+
if (!isPositiveInt(reviewer.scope_chunk_size))
|
|
208
|
+
reviewer.scope_chunk_size = dReviewer.scope_chunk_size;
|
|
209
|
+
const observatory = section('observatory');
|
|
210
|
+
const dObs = d.observatory;
|
|
211
|
+
if (typeof observatory.capture !== 'boolean')
|
|
212
|
+
observatory.capture = dObs.capture;
|
|
213
|
+
if (observatory.approval !== 'ask' && observatory.approval !== 'deny' && observatory.approval !== 'allow') {
|
|
214
|
+
observatory.approval = dObs.approval;
|
|
215
|
+
}
|
|
216
|
+
// Free-form sections must be objects (consumers treat them as maps).
|
|
217
|
+
for (const key of ['onboarding', 'personalization']) {
|
|
218
|
+
if (key in raw && !isPlainObject(raw[key]))
|
|
219
|
+
delete raw[key];
|
|
220
|
+
}
|
|
221
|
+
}
|
|
116
222
|
/**
|
|
117
223
|
* Check whether a command is in the predefined commands list.
|
|
118
224
|
* A command is allowed if it is EXACTLY (after trim) listed in any
|
|
@@ -139,8 +245,66 @@ export function flattenCommands(commands) {
|
|
|
139
245
|
return out;
|
|
140
246
|
}
|
|
141
247
|
/**
|
|
142
|
-
*
|
|
143
|
-
*
|
|
248
|
+
* Every top-level key `IterateConfig` defines. Anything else in the file is
|
|
249
|
+
* an unknown key: a typo (`maxRounds`) or a hallucinated section that nothing
|
|
250
|
+
* ever reads. Single source of truth for BOTH validation paths —
|
|
251
|
+
* {@link validateConfig} (read/merge) and `validateConfigUpdates` (write).
|
|
252
|
+
*
|
|
253
|
+
* Keep in sync with `IterateConfig` (src/types.ts).
|
|
254
|
+
*/
|
|
255
|
+
export const SUPPORTED_CONFIG_KEYS = [
|
|
256
|
+
'goal',
|
|
257
|
+
'language',
|
|
258
|
+
'dimensions',
|
|
259
|
+
'max_rounds',
|
|
260
|
+
'reasoning_effort',
|
|
261
|
+
'review',
|
|
262
|
+
'reviewer',
|
|
263
|
+
'atomic',
|
|
264
|
+
'git',
|
|
265
|
+
'validation',
|
|
266
|
+
'observatory',
|
|
267
|
+
'personalization',
|
|
268
|
+
'onboarding',
|
|
269
|
+
];
|
|
270
|
+
/** Upper bound on configurable iteration rounds (config bomb guard). */
|
|
271
|
+
export const MAX_MAX_ROUNDS = 100;
|
|
272
|
+
/** Upper bound on `atomic.max_lines` (a single fix never needs more). */
|
|
273
|
+
export const MAX_ATOMIC_MAX_LINES = 10_000;
|
|
274
|
+
/** Upper bound on `atomic.max_adjacent_methods`. */
|
|
275
|
+
export const MAX_MAX_ADJACENT_METHODS = 200;
|
|
276
|
+
/** Upper bound on `reviewer.scope_chunk_size` (files per reviewer task batch). */
|
|
277
|
+
export const MAX_SCOPE_CHUNK_SIZE = 1000;
|
|
278
|
+
/** `review.scope` enum. */
|
|
279
|
+
const REVIEW_SCOPES = ['full', 'changed-only'];
|
|
280
|
+
/** `reasoning_effort` enum (absent → provider default). */
|
|
281
|
+
const REASONING_EFFORTS = ['low', 'medium', 'high'];
|
|
282
|
+
/** `observatory.approval` enum (the human-consent policy). */
|
|
283
|
+
const APPROVAL_POLICIES = ['ask', 'deny', 'allow'];
|
|
284
|
+
/** True when `v` is one of the allowed enum members. */
|
|
285
|
+
function isEnum(v, allowed) {
|
|
286
|
+
return typeof v === 'string' && allowed.includes(v);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Validate the config's shape, ranges, and enums.
|
|
290
|
+
*
|
|
291
|
+
* Returns an array of error strings (empty when valid). Three tiers:
|
|
292
|
+
* - `root` — the whole document is not an object (return immediately).
|
|
293
|
+
* - the three PRESENCE-required fields (`goal`, `dimensions`, `validation`)
|
|
294
|
+
* — reported by their bare path so existing callers keep matching them;
|
|
295
|
+
* - STRICT checks on every field that IS present: bounded integers
|
|
296
|
+
* (`max_rounds ≤ 100`, `atomic.max_lines`, `reviewer.scope_chunk_size`),
|
|
297
|
+
* enums (`review.scope`, `reasoning_effort`, `observatory.approval`),
|
|
298
|
+
* booleans (`git.*`, `reviewer.*`), the `validation.commands` allow-list
|
|
299
|
+
* shape (a `Record<string, string[]>` — an ARRAY is refused, otherwise
|
|
300
|
+
* `flattenCommands` would silently discard it and leave a whitelist that
|
|
301
|
+
* looks configured but allows nothing), and unknown top-level keys.
|
|
302
|
+
*
|
|
303
|
+
* Absent optional fields are accepted: `loadEffectiveConfig` fills them from
|
|
304
|
+
* the built-in defaults, and hand-written partial configs are legitimate —
|
|
305
|
+
* but a present field must be RIGHT (YAML has no schema, so `max_rounds:
|
|
306
|
+
* "ten"` or `reviewer: {evidence_validation: "yes"}` used to pass the old
|
|
307
|
+
* three-presence-check validator untouched).
|
|
144
308
|
*/
|
|
145
309
|
export function validateConfig(config) {
|
|
146
310
|
const errors = [];
|
|
@@ -149,49 +313,297 @@ export function validateConfig(config) {
|
|
|
149
313
|
return errors;
|
|
150
314
|
}
|
|
151
315
|
const c = config;
|
|
152
|
-
|
|
316
|
+
// ── Required core fields (presence, bare-path identifiers) ──────────────
|
|
317
|
+
if (typeof c.goal !== 'string' || c.goal.trim().length === 0)
|
|
153
318
|
errors.push('goal');
|
|
154
|
-
if (!Array.isArray(c.dimensions))
|
|
319
|
+
if (!Array.isArray(c.dimensions)) {
|
|
155
320
|
errors.push('dimensions');
|
|
156
|
-
if (!c.validation || typeof c.validation !== 'object') {
|
|
157
|
-
errors.push('validation');
|
|
158
321
|
}
|
|
159
|
-
else
|
|
160
|
-
|
|
161
|
-
|
|
322
|
+
else if (c.dimensions.length === 0 ||
|
|
323
|
+
!c.dimensions.every((d) => typeof d === 'string' && d.trim().length > 0)) {
|
|
324
|
+
// `dimensions: []` is a valid JSON type but an empty review: it passes
|
|
325
|
+
// `Array.isArray` while selecting zero dimensions to review.
|
|
326
|
+
errors.push('dimensions must be a non-empty array of non-empty strings');
|
|
327
|
+
}
|
|
328
|
+
// ── Optional scalar fields: present ⇒ must be valid ─────────────────────
|
|
329
|
+
if ('language' in c && !isEnum(c.language, ['zh', 'en'])) {
|
|
330
|
+
errors.push('language must be "zh" or "en"');
|
|
331
|
+
}
|
|
332
|
+
if ('max_rounds' in c &&
|
|
333
|
+
(typeof c.max_rounds !== 'number' ||
|
|
334
|
+
!Number.isInteger(c.max_rounds) ||
|
|
335
|
+
c.max_rounds < 1 ||
|
|
336
|
+
c.max_rounds > MAX_MAX_ROUNDS)) {
|
|
337
|
+
errors.push(`max_rounds must be an integer between 1 and ${MAX_MAX_ROUNDS}`);
|
|
338
|
+
}
|
|
339
|
+
if ('reasoning_effort' in c &&
|
|
340
|
+
c.reasoning_effort !== undefined &&
|
|
341
|
+
!isEnum(c.reasoning_effort, REASONING_EFFORTS)) {
|
|
342
|
+
errors.push('reasoning_effort must be "low", "medium", or "high"');
|
|
343
|
+
}
|
|
344
|
+
// ── Sections: absent ⇒ defaults apply; present ⇒ must be a plain object ─
|
|
345
|
+
const section = (key, required = false) => {
|
|
346
|
+
if (!(key in c)) {
|
|
347
|
+
if (required)
|
|
348
|
+
errors.push(key);
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
// `typeof [] === 'object'` — a YAML sequence in a section's place must not
|
|
352
|
+
// masquerade as an object (it would sail through every `key in v` check).
|
|
353
|
+
if (!isPlainObject(c[key])) {
|
|
354
|
+
errors.push(key);
|
|
355
|
+
return null;
|
|
356
|
+
}
|
|
357
|
+
return c[key];
|
|
358
|
+
};
|
|
359
|
+
const review = section('review');
|
|
360
|
+
if (review && review.scope !== undefined && !isEnum(review.scope, REVIEW_SCOPES)) {
|
|
361
|
+
errors.push('review.scope must be "full" or "changed-only"');
|
|
362
|
+
}
|
|
363
|
+
const atomic = section('atomic');
|
|
364
|
+
if (atomic) {
|
|
365
|
+
if ('max_lines' in atomic &&
|
|
366
|
+
(typeof atomic.max_lines !== 'number' ||
|
|
367
|
+
!Number.isInteger(atomic.max_lines) ||
|
|
368
|
+
atomic.max_lines < 1 ||
|
|
369
|
+
atomic.max_lines > MAX_ATOMIC_MAX_LINES)) {
|
|
370
|
+
errors.push(`atomic.max_lines must be an integer between 1 and ${MAX_ATOMIC_MAX_LINES}`);
|
|
371
|
+
}
|
|
372
|
+
if ('max_adjacent_methods' in atomic &&
|
|
373
|
+
(typeof atomic.max_adjacent_methods !== 'number' ||
|
|
374
|
+
!Number.isInteger(atomic.max_adjacent_methods) ||
|
|
375
|
+
atomic.max_adjacent_methods < 0 ||
|
|
376
|
+
atomic.max_adjacent_methods > MAX_MAX_ADJACENT_METHODS)) {
|
|
377
|
+
errors.push(`atomic.max_adjacent_methods must be an integer between 0 and ${MAX_MAX_ADJACENT_METHODS}`);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
const git = section('git');
|
|
381
|
+
if (git) {
|
|
382
|
+
if ('target_branch' in git && (typeof git.target_branch !== 'string' || git.target_branch.length === 0)) {
|
|
383
|
+
errors.push('git.target_branch must be a non-empty string');
|
|
384
|
+
}
|
|
385
|
+
for (const key of ['use_worktree', 'push_per_round', 'auto_merge']) {
|
|
386
|
+
if (key in git && typeof git[key] !== 'boolean') {
|
|
387
|
+
errors.push(`git.${key} must be a boolean`);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
const reviewer = section('reviewer');
|
|
392
|
+
if (reviewer) {
|
|
393
|
+
for (const key of ['output_schema_validation', 'evidence_validation', 'coverage_validation']) {
|
|
394
|
+
if (key in reviewer && typeof reviewer[key] !== 'boolean') {
|
|
395
|
+
errors.push(`reviewer.${key} must be a boolean`);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if ('scope_chunk_size' in reviewer &&
|
|
399
|
+
(typeof reviewer.scope_chunk_size !== 'number' ||
|
|
400
|
+
!Number.isInteger(reviewer.scope_chunk_size) ||
|
|
401
|
+
reviewer.scope_chunk_size < 1 ||
|
|
402
|
+
reviewer.scope_chunk_size > MAX_SCOPE_CHUNK_SIZE)) {
|
|
403
|
+
errors.push(`reviewer.scope_chunk_size must be an integer between 1 and ${MAX_SCOPE_CHUNK_SIZE}`);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const observatory = section('observatory');
|
|
407
|
+
if (observatory) {
|
|
408
|
+
if ('capture' in observatory && typeof observatory.capture !== 'boolean') {
|
|
409
|
+
errors.push('observatory.capture must be a boolean');
|
|
410
|
+
}
|
|
411
|
+
if ('approval' in observatory && observatory.approval !== undefined && !isEnum(observatory.approval, APPROVAL_POLICIES)) {
|
|
412
|
+
errors.push('observatory.approval must be "ask", "deny", or "allow"');
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
// ── validation: the allow-list the runtime trusts to execute commands ───
|
|
416
|
+
const v = section('validation', true);
|
|
417
|
+
if (v) {
|
|
418
|
+
if (!Array.isArray(v.command_whitelist)) {
|
|
162
419
|
errors.push('validation.command_whitelist');
|
|
163
|
-
|
|
164
|
-
|
|
420
|
+
}
|
|
421
|
+
else if (!v.command_whitelist.every((x) => typeof x === 'string')) {
|
|
422
|
+
errors.push('validation.command_whitelist must be an array of strings');
|
|
423
|
+
}
|
|
424
|
+
// `commands` is an allow-list (module → command[]): it must be a mapping
|
|
425
|
+
// whose values are string arrays. `typeof [] === 'object'` used to let
|
|
426
|
+
// `["npm t"]` through, and `flattenCommands` then dropped every
|
|
427
|
+
// non-object entry — a whitelist that read as configured while allowing
|
|
428
|
+
// nothing (fail-closed for execution, but a silent configuration lie).
|
|
429
|
+
if (!('commands' in v) || !isPlainObject(v.commands)) {
|
|
430
|
+
errors.push('validation.commands must be a mapping of module → string[]');
|
|
431
|
+
}
|
|
432
|
+
else if (!Object.values(v.commands).every((arr) => Array.isArray(arr) && arr.every((x) => typeof x === 'string'))) {
|
|
433
|
+
errors.push('validation.commands must be a mapping of module → string[]');
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
// ── Unknown top-level keys: reported, never silently ignored ────────────
|
|
437
|
+
for (const key of Object.keys(c)) {
|
|
438
|
+
if (!SUPPORTED_CONFIG_KEYS.includes(key)) {
|
|
439
|
+
errors.push(`${key} is not a supported config key`);
|
|
440
|
+
}
|
|
165
441
|
}
|
|
166
442
|
return errors;
|
|
167
443
|
}
|
|
444
|
+
/**
|
|
445
|
+
* Sensitive system directories that may never become a project root in the
|
|
446
|
+
* HEADLESS tier (see {@link resolveProjectRoot} tier (b)): the directory
|
|
447
|
+
* itself and its DIRECT children are refused, checked against both the
|
|
448
|
+
* lexical path and its realpath (macOS aliases `/etc` → `/private/etc`, so a
|
|
449
|
+
* realpath'd `/etc/foo` lands under `/private/etc`). Deeper descendants and
|
|
450
|
+
* sibling trees are not swept up — that is what keeps `os.tmpdir()`
|
|
451
|
+
* (`/var/folders/…`, i.e. `/private/var/folders/…`, three levels below
|
|
452
|
+
* `/private`) and `/Users/<me>/project` working. The bare home directory is
|
|
453
|
+
* refused separately (paths INSIDE home are allowed).
|
|
454
|
+
*/
|
|
455
|
+
const SYSTEM_ROOTS = [
|
|
456
|
+
'/etc', '/usr', '/bin', '/sbin', '/var', '/private', '/System', '/Library',
|
|
457
|
+
'/Applications', '/dev', '/proc', '/sys', '/boot', '/run',
|
|
458
|
+
];
|
|
459
|
+
/** Memoized realpath (system roots, home) — `realpath` never changes for them. */
|
|
460
|
+
const realpathCache = new Map();
|
|
461
|
+
function cachedRealpath(path) {
|
|
462
|
+
const memo = realpathCache.get(path);
|
|
463
|
+
if (memo !== undefined)
|
|
464
|
+
return memo;
|
|
465
|
+
let real = path;
|
|
466
|
+
try {
|
|
467
|
+
const r = realpathSync(path);
|
|
468
|
+
if (r)
|
|
469
|
+
real = r;
|
|
470
|
+
}
|
|
471
|
+
catch {
|
|
472
|
+
// Not present on this platform/filesystem — the literal path is all we have.
|
|
473
|
+
}
|
|
474
|
+
realpathCache.set(path, real);
|
|
475
|
+
return real;
|
|
476
|
+
}
|
|
477
|
+
/** True for the bare home directory itself (lexical or realpath form). */
|
|
478
|
+
function isHomeDirectory(candidate) {
|
|
479
|
+
return candidate === homedir() || candidate === cachedRealpath(homedir());
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* True for a path the headless tier must refuse because it is a sensitive
|
|
483
|
+
* system root or sits DIRECTLY inside one — checked against both the lexical
|
|
484
|
+
* and the realpath form (macOS aliases `/etc` → `/private/etc`, so a
|
|
485
|
+
* realpath'd `/etc/foo` lands under `/private/etc`, whose parent is the real
|
|
486
|
+
* `/etc`). Pure path inspection — no filesystem mutation.
|
|
487
|
+
*/
|
|
488
|
+
function isSensitiveSystemPath(candidate) {
|
|
489
|
+
if (!candidate || candidate === sep)
|
|
490
|
+
return true;
|
|
491
|
+
const parent = dirname(candidate);
|
|
492
|
+
for (const sys of SYSTEM_ROOTS) {
|
|
493
|
+
if (candidate === sys || parent === sys)
|
|
494
|
+
return true;
|
|
495
|
+
// macOS: realpath(`/etc/foo`) = `/private/etc/foo` — compare against the
|
|
496
|
+
// real location of the sysroot or the alias slips through.
|
|
497
|
+
if (parent === cachedRealpath(sys))
|
|
498
|
+
return true;
|
|
499
|
+
}
|
|
500
|
+
return false;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Resolve `sessionCwd` to a containment anchor: the realpath of an EXISTING
|
|
504
|
+
* directory. Returns null when the session cwd is empty, `/`, NUL-bearing, or
|
|
505
|
+
* not an existing directory — a workspace header we cannot bound the session
|
|
506
|
+
* with (stale/bogus cwd) falls back to the headless tier instead.
|
|
507
|
+
*/
|
|
508
|
+
function sessionAnchor(sessionCwd) {
|
|
509
|
+
if (typeof sessionCwd !== 'string')
|
|
510
|
+
return null;
|
|
511
|
+
const raw = sessionCwd.trim();
|
|
512
|
+
if (!raw || raw === sep || raw.includes('\0'))
|
|
513
|
+
return null;
|
|
514
|
+
const abs = resolve(raw);
|
|
515
|
+
if (abs === sep)
|
|
516
|
+
return null;
|
|
517
|
+
try {
|
|
518
|
+
if (!statSync(abs).isDirectory())
|
|
519
|
+
return null;
|
|
520
|
+
}
|
|
521
|
+
catch {
|
|
522
|
+
return null; // does not exist / unreadable — cannot anchor containment
|
|
523
|
+
}
|
|
524
|
+
try {
|
|
525
|
+
const real = realpathSync(abs);
|
|
526
|
+
if (real && real !== sep)
|
|
527
|
+
return real;
|
|
528
|
+
}
|
|
529
|
+
catch {
|
|
530
|
+
// Exists but realpath is fenced — the lexical absolute path still bounds it.
|
|
531
|
+
}
|
|
532
|
+
return abs;
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Realpath `p` for CONTAINMENT comparisons: realpath directly when it exists,
|
|
536
|
+
* otherwise realpath the deepest existing ancestor and re-append the tail.
|
|
537
|
+
* A not-yet-created target can't be realpath'd as a whole, and on macOS
|
|
538
|
+
* `/var/folders/…` (lexical) vs `/private/var/folders/…` (anchor) would never
|
|
539
|
+
* match — likewise a symlinked parent must contribute its REAL destination,
|
|
540
|
+
* or "create a new dir through this link" would escape the workspace. Falls
|
|
541
|
+
* back to the lexical path when nothing along the way can be resolved.
|
|
542
|
+
*/
|
|
543
|
+
function realpathForContainment(p) {
|
|
544
|
+
try {
|
|
545
|
+
if (existsSync(p))
|
|
546
|
+
return realpathSync(p);
|
|
547
|
+
}
|
|
548
|
+
catch {
|
|
549
|
+
return p;
|
|
550
|
+
}
|
|
551
|
+
let cur = p;
|
|
552
|
+
const tail = [];
|
|
553
|
+
for (;;) {
|
|
554
|
+
const parent = dirname(cur);
|
|
555
|
+
if (parent === cur)
|
|
556
|
+
return p; // walked to the fs root without an ancestor
|
|
557
|
+
tail.unshift(basename(cur));
|
|
558
|
+
try {
|
|
559
|
+
if (existsSync(parent))
|
|
560
|
+
return join(realpathSync(parent), ...tail);
|
|
561
|
+
}
|
|
562
|
+
catch {
|
|
563
|
+
return p;
|
|
564
|
+
}
|
|
565
|
+
cur = parent;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
168
568
|
/**
|
|
169
569
|
* Resolve a caller-supplied project root to a safe absolute path.
|
|
170
570
|
*
|
|
171
|
-
* Every tool accepts a model-controlled `path` argument
|
|
172
|
-
* any file read/write or as a command `cwd`, it must be sanitized
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
571
|
+
* Every tool accepts a model-controlled `path` argument; before it is used in
|
|
572
|
+
* any file read/write or as a command `cwd`, it must be sanitized. Two-tier
|
|
573
|
+
* contract (checked AFTER the common steps below):
|
|
574
|
+
*
|
|
575
|
+
* Resolution order for the default (no explicit `path`) case:
|
|
576
|
+
* 1. `sessionCwd` — the absolute working directory the calling DSH session
|
|
577
|
+
* was created in (`exec.agent.session.header.cwd`). Authoritative
|
|
578
|
+
* workspace for the conversation, immune to where the process started;
|
|
579
|
+
* an empty session cwd or `/` is refused, everything else (including
|
|
580
|
+
* the home directory itself) is honored.
|
|
581
|
+
* 2. the process cwd, when it is a usable directory (not `/` or the home
|
|
582
|
+
* dir — launchd/daemon-managed servers start with cwd=`/`);
|
|
583
|
+
* 3. the session workspace decoded from `DSH_SESSION_JSONL`.
|
|
584
|
+
*
|
|
585
|
+
* Common steps: an empty/missing `path` falls back to the step above; the
|
|
586
|
+
* path is resolved to an absolute path (collapsing `..` and symlinks —
|
|
587
|
+
* realpath-aware so a symlinked session cwd and its target share one root);
|
|
588
|
+
* the filesystem root `/` is always refused.
|
|
589
|
+
*
|
|
590
|
+
* Tier (a) — a USABLE session cwd is known (an existing directory):
|
|
591
|
+
* an EXPLICIT `path` must resolve to the session cwd or something INSIDE
|
|
592
|
+
* it; otherwise `{ ok: false, reason }` (structured — callers
|
|
593
|
+
* short-circuit on it). This keeps the legitimate "scope to a
|
|
594
|
+
* subdirectory" use while blocking escapes to `/etc` or sibling trees
|
|
595
|
+
* (the path alone could never smuggle the model out of its workspace).
|
|
596
|
+
*
|
|
597
|
+
* Tier (b) — no usable session cwd (headless/tests):
|
|
598
|
+
* additionally refuse the sensitive system roots and their direct children
|
|
599
|
+
* (see {@link SYSTEM_ROOTS}) plus the bare home directory itself. Paths
|
|
600
|
+
* INSIDE home (`/Users/me/project`) and the transient tmpdir tree remain
|
|
601
|
+
* allowed, as does any ordinary project directory.
|
|
177
602
|
*
|
|
178
603
|
* Returns `{ ok: true, root }` on success, or `{ ok: false, reason }` when the
|
|
179
604
|
* path is unsafe; callers must short-circuit on the failure and return a
|
|
180
605
|
* structured error instead of proceeding.
|
|
181
606
|
*/
|
|
182
|
-
/**
|
|
183
|
-
* Resolve a caller-supplied project root to a safe absolute path.
|
|
184
|
-
*
|
|
185
|
-
* Resolution order for the default (no explicit `path`) case:
|
|
186
|
-
* 1. `sessionCwd` — the absolute working directory the calling DSH session
|
|
187
|
-
* was created in (`exec.agent.session.header.cwd`). This is the
|
|
188
|
-
* authoritative workspace for the current conversation and is immune to
|
|
189
|
-
* where the web-service process happened to start.
|
|
190
|
-
* 2. the process cwd, when it is a usable directory (not `/` or the home
|
|
191
|
-
* dir — launchd/daemon-managed servers start with cwd=`/`);
|
|
192
|
-
* 3. the session workspace decoded from `DSH_SESSION_JSONL` (present when
|
|
193
|
-
* the DSH runtime injects per-session env into tool sub-processes).
|
|
194
|
-
*/
|
|
195
607
|
export function resolveProjectRoot(input, sessionCwd) {
|
|
196
608
|
// A non-string input (number, array, …) from a hostile tool invocation must
|
|
197
609
|
// be treated as "no explicit path" instead of crashing on `.trim()`.
|
|
@@ -201,7 +613,8 @@ export function resolveProjectRoot(input, sessionCwd) {
|
|
|
201
613
|
if (raw.includes('\0')) {
|
|
202
614
|
return { ok: false, reason: 'Refusing project root containing NUL bytes.' };
|
|
203
615
|
}
|
|
204
|
-
const
|
|
616
|
+
const explicit = raw.length > 0;
|
|
617
|
+
const rootLexical = explicit ? resolve(raw) : resolve(effectiveCwd(sessionCwd));
|
|
205
618
|
// Collapse symlinks (documented contract: "collapsing `..` and symlinks") —
|
|
206
619
|
// a path through a symlinked directory must resolve to its REAL location so
|
|
207
620
|
// two aliases of the same project share one `.iterate/` state, and tools can
|
|
@@ -225,6 +638,64 @@ export function resolveProjectRoot(input, sessionCwd) {
|
|
|
225
638
|
if (!root || root === sep) {
|
|
226
639
|
return { ok: false, reason: 'Refusing filesystem root as project root.' };
|
|
227
640
|
}
|
|
641
|
+
// `.iterate` must stay INSIDE the resolved root: it is the state directory
|
|
642
|
+
// every tool writes through (checkpoint, decision log, fixes, backups). When
|
|
643
|
+
// it exists and is a symlink — or is reached through one — whose REAL target
|
|
644
|
+
// escapes the REAL project root, artifacts would be created/removed outside
|
|
645
|
+
// the workspace while the tools still believe they are under the root. Only
|
|
646
|
+
// the project ROOT used to be realpath'd, so a symlinked `.iterate` quietly
|
|
647
|
+
// redirected every product out of the tree. Refuse the root (structured —
|
|
648
|
+
// callers short-circuit) instead of following the link; an `.iterate` that
|
|
649
|
+
// stays within the root (a real dir, or a link to one inside) is accepted.
|
|
650
|
+
const iterateLexical = join(root, '.iterate');
|
|
651
|
+
if (existsSync(iterateLexical)) {
|
|
652
|
+
try {
|
|
653
|
+
const realRoot = realpathForContainment(root);
|
|
654
|
+
const realIterate = realpathForContainment(iterateLexical);
|
|
655
|
+
if (realIterate !== realRoot && !realIterate.startsWith(realRoot + sep)) {
|
|
656
|
+
return {
|
|
657
|
+
ok: false,
|
|
658
|
+
reason: `Refusing project root: \`.iterate\` resolves outside the project (symlink escape): ` +
|
|
659
|
+
`\`${realIterate}\` is not inside \`${realRoot}\`.`,
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
catch {
|
|
664
|
+
// A realpath failure (permission fence, race) keeps the lexical
|
|
665
|
+
// behavior — the state dir itself still surfaces any real I/O problem
|
|
666
|
+
// as a structured tool error later.
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
const anchor = sessionAnchor(sessionCwd);
|
|
670
|
+
if (anchor) {
|
|
671
|
+
// Tier (a): the session workspace bounds every explicit path (realpath on
|
|
672
|
+
// both sides — including the deepest-existing-ancestor form for a
|
|
673
|
+
// not-yet-created path, so `/var/…` vs `/private/var/…` and symlinked
|
|
674
|
+
// parents agree with the anchor).
|
|
675
|
+
if (explicit) {
|
|
676
|
+
const contained = realpathForContainment(root);
|
|
677
|
+
if (contained !== anchor && !contained.startsWith(anchor + sep)) {
|
|
678
|
+
return {
|
|
679
|
+
ok: false,
|
|
680
|
+
reason: `Refusing project root outside the session workspace: \`${root}\` is not inside \`${anchor}\`.`,
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
return { ok: true, root };
|
|
685
|
+
}
|
|
686
|
+
// Tier (b): headless — no session workspace to contain the path. Refuse the
|
|
687
|
+
// bare home directory (paths INSIDE home stay allowed) and sensitive system
|
|
688
|
+
// roots — checked on both forms: `/etc/foo` only shows its `/etc` parent
|
|
689
|
+
// lexically, while its realpath shows `/private/etc`.
|
|
690
|
+
if (isHomeDirectory(root) || isHomeDirectory(rootLexical)) {
|
|
691
|
+
return {
|
|
692
|
+
ok: false,
|
|
693
|
+
reason: 'Refusing the home directory itself as project root — scope to a subdirectory inside it.',
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
if (isSensitiveSystemPath(rootLexical) || isSensitiveSystemPath(root)) {
|
|
697
|
+
return { ok: false, reason: 'Refusing a sensitive system directory as project root.' };
|
|
698
|
+
}
|
|
228
699
|
return { ok: true, root };
|
|
229
700
|
}
|
|
230
701
|
/**
|
|
@@ -244,7 +715,11 @@ export function resolveProjectRootForExec(exec, input) {
|
|
|
244
715
|
* `--Volumes-Eng-Dev-iterate-skill--`).
|
|
245
716
|
*/
|
|
246
717
|
function effectiveCwd(sessionCwd) {
|
|
247
|
-
|
|
718
|
+
// A session legitimately rooted in the HOME directory must be honored: only
|
|
719
|
+
// an empty session cwd or the filesystem root disqualifies it. (Refusing
|
|
720
|
+
// `homedir()` here substituted the server's own cwd — tools then read/write
|
|
721
|
+
// `.iterate` in the wrong project.)
|
|
722
|
+
if (typeof sessionCwd === 'string' && sessionCwd && sessionCwd !== sep)
|
|
248
723
|
return sessionCwd;
|
|
249
724
|
let cwd = '';
|
|
250
725
|
try {
|