@andares/pi-toolkits 0.13.0 → 0.14.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/CHANGELOG.md +43 -0
- package/README.md +36 -4
- package/README.zh-CN.md +23 -4
- package/package.json +8 -4
- package/src/features/permission/policy.test.ts +10 -0
- package/src/features/permission/units.test.ts +157 -0
- package/src/features/permission/units.ts +317 -15
- package/src/features/whats-new/changelog.ts +104 -0
- package/src/features/whats-new/constants.ts +35 -0
- package/src/features/whats-new/index.ts +146 -0
- package/src/features/whats-new/notice.ts +68 -0
- package/src/features/whats-new/paths.ts +29 -0
- package/src/features/whats-new/store.ts +114 -0
- package/src/features/whats-new/version.ts +43 -0
- package/src/features/whats-new/whats-new.test.ts +460 -0
- package/src/index.ts +3 -0
|
@@ -74,6 +74,21 @@ const DEVICE_SINKS = new Set(["/dev/null", "/dev/stdout", "/dev/stderr"]);
|
|
|
74
74
|
*/
|
|
75
75
|
const NO_PATH_OPERANDS = new Set(["echo", "printf"]);
|
|
76
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Flags whose value is data even when it looks like a path. `curl -w
|
|
79
|
+
* '%{http_code}\n'` contains a backslash, so the path heuristic claimed a
|
|
80
|
+
* write target inside the project (audit 2026-10-26: 2 asks). The table stays
|
|
81
|
+
* small on purpose: every entry widens the read blind spot for that flag.
|
|
82
|
+
*/
|
|
83
|
+
const DATA_VALUE_OPTS: Record<string, ReadonlySet<string>> = {
|
|
84
|
+
curl: new Set([
|
|
85
|
+
"-w", "--write-out", "-H", "--header", "-d", "--data", "--data-raw",
|
|
86
|
+
"--data-binary", "--data-urlencode", "-X", "--request", "-A",
|
|
87
|
+
"--user-agent", "-e", "--referer", "-u", "--user", "-b", "--cookie",
|
|
88
|
+
"-F", "--form", "--form-string", "-x", "--proxy",
|
|
89
|
+
]),
|
|
90
|
+
};
|
|
91
|
+
|
|
77
92
|
/* -------------------------------------------------------------------------- */
|
|
78
93
|
/* Read/write classification */
|
|
79
94
|
/* -------------------------------------------------------------------------- */
|
|
@@ -232,6 +247,7 @@ export function commandEffect(command: string, args: readonly string[]): Effect
|
|
|
232
247
|
const verb = args.find((arg) => /^[a-zA-Z]+$/.test(arg)) ?? "";
|
|
233
248
|
return /[xc]/.test(verb) ? "write" : "read";
|
|
234
249
|
}
|
|
250
|
+
if (command === "docker" || command === "podman") return containerEffect(args);
|
|
235
251
|
if (READ_COMMANDS.has(command)) return "read";
|
|
236
252
|
if (WRITE_COMMANDS.has(command)) return "write";
|
|
237
253
|
return "write";
|
|
@@ -250,6 +266,42 @@ const REDIRECTS = new Set([
|
|
|
250
266
|
const WRITE_REDIRECTS = new Set([">", ">>", ">|", "&>", "&>>", ">&", "<>"]);
|
|
251
267
|
const PROCESS_SUBSTITUTION = new Set(["<(", ">("]);
|
|
252
268
|
|
|
269
|
+
/**
|
|
270
|
+
* Container subcommands that only inspect. `docker compose ps` in a directory
|
|
271
|
+
* outside the session roots used to ask as an "outside-project write" (audit
|
|
272
|
+
* 2026-10-29) although it touches no local file; anything not listed — `run`,
|
|
273
|
+
* `exec`, `rm`, `build`, … — stays a write. `exec` in particular runs an
|
|
274
|
+
* arbitrary command and must keep asking.
|
|
275
|
+
*/
|
|
276
|
+
const CONTAINER_READ_SUBCOMMANDS = new Set([
|
|
277
|
+
"ps", "logs", "inspect", "images", "version", "info", "top", "stats",
|
|
278
|
+
"port", "diff", "history", "search", "events", "df",
|
|
279
|
+
]);
|
|
280
|
+
const CONTAINER_VALUE_OPTS = new Set([
|
|
281
|
+
"-H", "--host", "--context", "-c", "--config", "--log-level",
|
|
282
|
+
"--tlscacert", "--tlscert", "--tlskey",
|
|
283
|
+
]);
|
|
284
|
+
const COMPOSE_READ_SUBCOMMANDS = new Set([
|
|
285
|
+
"ps", "logs", "config", "top", "images", "version", "ls", "port",
|
|
286
|
+
]);
|
|
287
|
+
const COMPOSE_VALUE_OPTS = new Set([
|
|
288
|
+
"-f", "--file", "-p", "--project-name", "--profile",
|
|
289
|
+
"--project-directory", "--env-file", "--ansi", "--progress",
|
|
290
|
+
]);
|
|
291
|
+
|
|
292
|
+
function containerEffect(args: readonly string[]): Effect {
|
|
293
|
+
const top = firstNonOption(args, CONTAINER_VALUE_OPTS);
|
|
294
|
+
if (top < 0) return "write";
|
|
295
|
+
if (args[top] === "compose") {
|
|
296
|
+
const rest = args.slice(top + 1);
|
|
297
|
+
const inner = firstNonOption(rest, COMPOSE_VALUE_OPTS);
|
|
298
|
+
return inner >= 0 && COMPOSE_READ_SUBCOMMANDS.has(rest[inner])
|
|
299
|
+
? "read"
|
|
300
|
+
: "write";
|
|
301
|
+
}
|
|
302
|
+
return CONTAINER_READ_SUBCOMMANDS.has(args[top]) ? "read" : "write";
|
|
303
|
+
}
|
|
304
|
+
|
|
253
305
|
function opOf(entry: ParseEntry): string | undefined {
|
|
254
306
|
if (typeof entry !== "object" || entry === null) return undefined;
|
|
255
307
|
if (!("op" in entry)) return undefined;
|
|
@@ -319,6 +371,11 @@ export function stripHeredocs(input: string): string | undefined {
|
|
|
319
371
|
* Replace `$(…)` and backtick substitutions with placeholder words and return
|
|
320
372
|
* their payloads, so the outer command parses cleanly and each payload can be
|
|
321
373
|
* analysed as a command of its own.
|
|
374
|
+
*
|
|
375
|
+
* Quote-aware: inside single quotes both forms are literal text, not commands
|
|
376
|
+
* (`grep -n ':title="x`' file` is a pattern, not an unterminated
|
|
377
|
+
* substitution — audit 2026-11-05). Inside double quotes they do expand, as in
|
|
378
|
+
* bash.
|
|
322
379
|
*/
|
|
323
380
|
export function extractSubstitutions(input: string): {
|
|
324
381
|
text: string;
|
|
@@ -327,26 +384,49 @@ export function extractSubstitutions(input: string): {
|
|
|
327
384
|
const payloads: string[] = [];
|
|
328
385
|
let out = "";
|
|
329
386
|
let index = 0;
|
|
387
|
+
let quote: "'" | '"' | undefined;
|
|
388
|
+
|
|
389
|
+
const takeSubstitution = (start: number, end: number): void => {
|
|
390
|
+
payloads.push(input.slice(start, end));
|
|
391
|
+
out += `${SENTINEL}SUBST${payloads.length - 1}${SENTINEL}`;
|
|
392
|
+
};
|
|
393
|
+
|
|
330
394
|
while (index < input.length) {
|
|
331
395
|
const char = input[index];
|
|
332
|
-
if (char === "\\") {
|
|
396
|
+
if (char === "\\" && quote !== "'") {
|
|
333
397
|
out += input.slice(index, index + 2);
|
|
334
398
|
index += 2;
|
|
335
399
|
continue;
|
|
336
400
|
}
|
|
401
|
+
if (quote === "'") {
|
|
402
|
+
out += char;
|
|
403
|
+
if (char === "'") quote = undefined;
|
|
404
|
+
index++;
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
if (char === "'" && quote === undefined) {
|
|
408
|
+
quote = "'";
|
|
409
|
+
out += char;
|
|
410
|
+
index++;
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
if (char === '"' ) {
|
|
414
|
+
quote = quote === '"' ? undefined : '"';
|
|
415
|
+
out += char;
|
|
416
|
+
index++;
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
337
419
|
if (char === "`") {
|
|
338
420
|
const end = input.indexOf("`", index + 1);
|
|
339
421
|
if (end < 0) return undefined;
|
|
340
|
-
|
|
341
|
-
out += `${SENTINEL}SUBST${payloads.length - 1}${SENTINEL}`;
|
|
422
|
+
takeSubstitution(index + 1, end);
|
|
342
423
|
index = end + 1;
|
|
343
424
|
continue;
|
|
344
425
|
}
|
|
345
426
|
if (char === "$" && input[index + 1] === "(") {
|
|
346
427
|
const end = findClosingParen(input, index + 2);
|
|
347
428
|
if (end < 0) return undefined;
|
|
348
|
-
|
|
349
|
-
out += `${SENTINEL}SUBST${payloads.length - 1}${SENTINEL}`;
|
|
429
|
+
takeSubstitution(index + 2, end);
|
|
350
430
|
index = end + 1;
|
|
351
431
|
continue;
|
|
352
432
|
}
|
|
@@ -357,12 +437,19 @@ export function extractSubstitutions(input: string): {
|
|
|
357
437
|
}
|
|
358
438
|
|
|
359
439
|
/**
|
|
360
|
-
* Remove shell comments and verify quote balance in
|
|
440
|
+
* Remove shell comments, normalize line breaks, and verify quote balance in
|
|
441
|
+
* one pass.
|
|
361
442
|
*
|
|
362
443
|
* `shell-quote` silently accepts an unterminated quote, so the balance check
|
|
363
444
|
* is what keeps malformed input from being parsed as a command we then judge.
|
|
364
445
|
* Comments are dropped first because an apostrophe in `# it's fine` would
|
|
365
446
|
* otherwise look like an opening quote. `undefined` means unbalanced.
|
|
447
|
+
*
|
|
448
|
+
* Unquoted newlines become `;`: shell-quote treats a newline as plain
|
|
449
|
+
* whitespace, so without this two commands on separate lines merged into one
|
|
450
|
+
* segment (`ls⏎rm -rf x` looked like `ls rm -rf x` — a missed write, and the
|
|
451
|
+
* common source of phantom "unresolved path" asks for multi-line scripts).
|
|
452
|
+
* A backslash-newline is a line continuation in bash and is removed instead.
|
|
366
453
|
*/
|
|
367
454
|
function stripComments(input: string): string | undefined {
|
|
368
455
|
let out = "";
|
|
@@ -371,6 +458,14 @@ function stripComments(input: string): string | undefined {
|
|
|
371
458
|
const char = input[i];
|
|
372
459
|
if (quote !== "'") {
|
|
373
460
|
if (char === "\\") {
|
|
461
|
+
if (input[i + 1] === "\n") {
|
|
462
|
+
i++;
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
if (input[i + 1] === "\r" && input[i + 2] === "\n") {
|
|
466
|
+
i += 2;
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
374
469
|
out += input.slice(i, i + 2);
|
|
375
470
|
i++;
|
|
376
471
|
continue;
|
|
@@ -388,10 +483,15 @@ function stripComments(input: string): string | undefined {
|
|
|
388
483
|
}
|
|
389
484
|
if (char === "#" && (i === 0 || /\s/.test(input[i - 1]))) {
|
|
390
485
|
while (i < input.length && input[i] !== "\n") i++;
|
|
391
|
-
out += "
|
|
486
|
+
out += ";";
|
|
392
487
|
continue;
|
|
393
488
|
}
|
|
394
|
-
|
|
489
|
+
if (char === "\r" && input[i + 1] === "\n") {
|
|
490
|
+
out += ";";
|
|
491
|
+
i++;
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
out += char === "\n" ? ";" : char;
|
|
395
495
|
}
|
|
396
496
|
return quote === undefined ? out : undefined;
|
|
397
497
|
}
|
|
@@ -435,6 +535,12 @@ function envFor(base: string | null): Record<string, string> {
|
|
|
435
535
|
get(target, key) {
|
|
436
536
|
if (typeof key !== "string") return undefined;
|
|
437
537
|
if (Object.hasOwn(target, key)) return target[key];
|
|
538
|
+
// A lone `$` — end of a double-quoted string (`"</div>\s*$"`), or
|
|
539
|
+
// `$` before a character that cannot start a name — is literal in
|
|
540
|
+
// bash. shell-quote still calls the proxy with an empty key for that
|
|
541
|
+
// case; expanding it to the sentinel turned every such grep pattern
|
|
542
|
+
// into an "unresolved path" (audit 2026-11-12: 5 asks).
|
|
543
|
+
if (key === "") return "$";
|
|
438
544
|
return `${SENTINEL}VAR${key}${SENTINEL}`;
|
|
439
545
|
},
|
|
440
546
|
});
|
|
@@ -539,13 +645,18 @@ function firstCommandWord(words: readonly string[]): string {
|
|
|
539
645
|
/** Whether a word looks like a filesystem path. */
|
|
540
646
|
export function looksLikePath(word: string): boolean {
|
|
541
647
|
if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(word)) return false; // URL
|
|
648
|
+
if (/^[A-Za-z]:[\\/]/.test(word) || word.startsWith("\\\\")) return true; // Windows drive / UNC
|
|
542
649
|
return (
|
|
543
650
|
word === "." ||
|
|
544
651
|
word === ".." ||
|
|
545
652
|
word.startsWith("/") ||
|
|
546
653
|
word.startsWith("~") ||
|
|
547
654
|
word.includes("/") ||
|
|
548
|
-
|
|
655
|
+
// A bare backslash is *not* a path separator on POSIX: `\s*`, `\|`, and
|
|
656
|
+
// `%{http_code}\n` are regex/format text, and treating them as paths made
|
|
657
|
+
// read targets out of grep patterns (audit 2026-11-12). Relative Windows
|
|
658
|
+
// spellings (`dir\file`) join the read blind spot; the target host here is
|
|
659
|
+
// POSIX, and absolute Windows paths still match above.
|
|
549
660
|
word.includes("*") ||
|
|
550
661
|
word.includes("?") ||
|
|
551
662
|
word.includes("[")
|
|
@@ -605,6 +716,21 @@ const WRITE_OPERAND_VALUE_OPTS = new Set([
|
|
|
605
716
|
"--reference", "--mode",
|
|
606
717
|
]);
|
|
607
718
|
|
|
719
|
+
/**
|
|
720
|
+
* grep-family commands whose **first positional operand is the pattern**, not a
|
|
721
|
+
* path. `grep -n '</div>\s*$' src/a.vue` used to collect the pattern as a file
|
|
722
|
+
* target (and an unresolved `$`/`\` in a pattern as an opaque path — audit
|
|
723
|
+
* 2026-11-12: 5 asks). With `-e`/`-f` the pattern is named explicitly, so every
|
|
724
|
+
* positional is a file again.
|
|
725
|
+
*/
|
|
726
|
+
const GREP_FAMILY = new Set(["grep", "egrep", "fgrep", "rg", "ag"]);
|
|
727
|
+
const GREP_VALUE_OPTS = new Set([
|
|
728
|
+
"-A", "-B", "-C", "-m", "-d", "-e", "--regexp", "-f", "--file",
|
|
729
|
+
"--after-context", "--before-context", "--context", "--max-count",
|
|
730
|
+
"--directories", "--include", "--exclude", "--exclude-dir", "--exclude-from",
|
|
731
|
+
"--label", "--color", "--colour",
|
|
732
|
+
]);
|
|
733
|
+
|
|
608
734
|
function dedupeTargets(targets: UnitTarget[]): UnitTarget[] {
|
|
609
735
|
const seen = new Set<string>();
|
|
610
736
|
const out: UnitTarget[] = [];
|
|
@@ -669,17 +795,38 @@ function analyzeSegment(
|
|
|
669
795
|
if (words.length === 0) return { units: [], nextBase: base };
|
|
670
796
|
|
|
671
797
|
// Leading `VAR=value` assignments belong to the segment's environment, not
|
|
672
|
-
// to the command.
|
|
798
|
+
// to the command. A segment made only of assignments touches nothing, so it
|
|
799
|
+
// produces no unit (audit 2026-11-12: `f=…jsonl` asked as a write).
|
|
673
800
|
let peeled = words.filter(() => true);
|
|
674
801
|
while (peeled.length > 0 && /^[A-Za-z_][A-Za-z0-9_]*=/.test(peeled[0])) {
|
|
675
802
|
peeled = peeled.slice(1);
|
|
676
803
|
}
|
|
804
|
+
if (peeled.length === 0) return { units: [], nextBase: base };
|
|
677
805
|
|
|
678
806
|
// Wrapper peeling: `sudo rm` is decided by `rm`, while `headWord` keeps the
|
|
679
807
|
// wrapper visible for the danger table.
|
|
680
808
|
for (let round = 0; round < 4; round++) {
|
|
681
809
|
const step = peelOne(peeled);
|
|
682
810
|
if (!step) break;
|
|
811
|
+
// A plain `env` / `env FOO=1` prints the environment: nothing to resolve,
|
|
812
|
+
// nothing to guard. Without this it was an opaque "wrapper runs no
|
|
813
|
+
// visible command" (audit 2026-11-12: 5 asks for `env | grep …`).
|
|
814
|
+
const rest = peeled.slice(step.inner);
|
|
815
|
+
if (step.wrapper === "env" && rest.every((word) => ASSIGNMENT_RE.test(word))) {
|
|
816
|
+
return {
|
|
817
|
+
units: [{
|
|
818
|
+
raw,
|
|
819
|
+
headWord,
|
|
820
|
+
command: "env",
|
|
821
|
+
args: rest,
|
|
822
|
+
effect: "read",
|
|
823
|
+
opaque: false,
|
|
824
|
+
base,
|
|
825
|
+
targets: [],
|
|
826
|
+
}],
|
|
827
|
+
nextBase: base,
|
|
828
|
+
};
|
|
829
|
+
}
|
|
683
830
|
if (step.inner >= peeled.length) {
|
|
684
831
|
return {
|
|
685
832
|
units: [opaqueUnit(raw, headWord, step.wrapper, base, "wrapper runs no visible command")],
|
|
@@ -877,9 +1024,22 @@ function analyzeSegment(
|
|
|
877
1024
|
}
|
|
878
1025
|
|
|
879
1026
|
// Path-like arguments are targets for every command, except for commands
|
|
880
|
-
// whose arguments are data (`echo`, `printf`)
|
|
1027
|
+
// whose arguments are data (`echo`, `printf`), declared data flags, and
|
|
1028
|
+
// device sinks.
|
|
881
1029
|
if (!NO_PATH_OPERANDS.has(command)) {
|
|
882
|
-
|
|
1030
|
+
const dataOpts = DATA_VALUE_OPTS[command];
|
|
1031
|
+
const patternIndex =
|
|
1032
|
+
GREP_FAMILY.has(command) &&
|
|
1033
|
+
!hasFlag(args, "-e", "--regexp", "-f", "--file")
|
|
1034
|
+
? firstNonOption(args, GREP_VALUE_OPTS)
|
|
1035
|
+
: -1;
|
|
1036
|
+
for (let i = 0; i < args.length; i++) {
|
|
1037
|
+
const arg = args[i];
|
|
1038
|
+
if (dataOpts?.has(arg)) {
|
|
1039
|
+
i++;
|
|
1040
|
+
continue;
|
|
1041
|
+
}
|
|
1042
|
+
if (i === patternIndex) continue;
|
|
883
1043
|
if (DEVICE_SINKS.has(arg)) continue;
|
|
884
1044
|
if (!looksLikePath(arg)) continue;
|
|
885
1045
|
// `sed -i` operands were already classified above; its script is not a path.
|
|
@@ -947,6 +1107,145 @@ function segmentTokens(entries: ParseEntry[]): string[][] {
|
|
|
947
1107
|
|
|
948
1108
|
const PLACEHOLDER = /\u0000SUBST(\d+)\u0000/g;
|
|
949
1109
|
|
|
1110
|
+
/* -------------------------------------------------------------------------- */
|
|
1111
|
+
/* Literal for-loops */
|
|
1112
|
+
/* -------------------------------------------------------------------------- */
|
|
1113
|
+
|
|
1114
|
+
/**
|
|
1115
|
+
* `for f in a b; do grep x "$f"; done` is a common read-only inspection
|
|
1116
|
+
* shape. The tokenizer leaves `$f` as an unresolved sentinel, which made every
|
|
1117
|
+
* such command opaque (audit 2026-11-12: 29 of 61 asks). Expanding the literal
|
|
1118
|
+
* loop values is what bash does, so the per-unit judgement sees real paths.
|
|
1119
|
+
*
|
|
1120
|
+
* This deliberately does **not** persist ordinary `NAME=value` assignments:
|
|
1121
|
+
* without a control-flow graph, `false && d=/safe; rm "$d/x"` or
|
|
1122
|
+
* `d=/safe; read d; rm "$d/x"` would be judged against the wrong path. Such
|
|
1123
|
+
* uses stay opaque. Loop expansion likewise stops for a variable reassigned or
|
|
1124
|
+
* passed to a shell builtin that can mutate it.
|
|
1125
|
+
*
|
|
1126
|
+
* Anything not provably literal — a value from `$(…)`, a loop over an unknown
|
|
1127
|
+
* list, more than {@link MAX_EXPANSIONS} combinations, a value with whitespace
|
|
1128
|
+
* — is left untouched: the sentinel survives and the unit stays opaque.
|
|
1129
|
+
*/
|
|
1130
|
+
const MAX_EXPANSIONS = 64;
|
|
1131
|
+
const ASSIGNMENT_RE = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/;
|
|
1132
|
+
const SUBST_PLACEHOLDER_RE = /\u0000SUBST\d+\u0000/;
|
|
1133
|
+
|
|
1134
|
+
interface LoopFrame {
|
|
1135
|
+
readonly name: string;
|
|
1136
|
+
/** Literal values; empty means "cannot expand, keep failing closed". */
|
|
1137
|
+
readonly values: readonly string[];
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
interface ExpansionState {
|
|
1141
|
+
/** Open `for` loops, innermost last. */
|
|
1142
|
+
readonly loops: LoopFrame[];
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
function variableMarker(name: string): string {
|
|
1146
|
+
return `${SENTINEL}VAR${name}${SENTINEL}`;
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/** A value that can be substituted without changing word splitting. */
|
|
1150
|
+
function isLiteralValue(value: string): boolean {
|
|
1151
|
+
return (
|
|
1152
|
+
value !== "" &&
|
|
1153
|
+
!hasSentinel(value) &&
|
|
1154
|
+
!SUBST_PLACEHOLDER_RE.test(value) &&
|
|
1155
|
+
!/\s/.test(value)
|
|
1156
|
+
);
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
/** `for NAME in w1 w2 …`; any other header keeps an unexpandable frame. */
|
|
1160
|
+
function parseForHeader(words: readonly string[]): LoopFrame {
|
|
1161
|
+
const name = words[1] ?? "";
|
|
1162
|
+
if (words[2] !== "in") return { name, values: [] };
|
|
1163
|
+
const values = words.slice(3);
|
|
1164
|
+
return { name, values: values.every(isLiteralValue) ? values : [] };
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
/** Stop expanding a loop variable after code may have changed its value. */
|
|
1168
|
+
function disableLoopVariable(state: ExpansionState, name: string): void {
|
|
1169
|
+
for (let i = 0; i < state.loops.length; i++) {
|
|
1170
|
+
if (state.loops[i].name === name) {
|
|
1171
|
+
state.loops[i] = { name, values: [] };
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
const LOOP_MUTATING_COMMANDS = new Set([
|
|
1177
|
+
"read", "readarray", "mapfile", "unset", "export", "declare", "local",
|
|
1178
|
+
"typeset", "let", "printf",
|
|
1179
|
+
]);
|
|
1180
|
+
const LOOP_DYNAMIC_COMMANDS = new Set(["eval", "source", "."]);
|
|
1181
|
+
|
|
1182
|
+
/** Conservative guard for builtins that can assign to a named variable. */
|
|
1183
|
+
function disableMutatedLoopVariables(
|
|
1184
|
+
words: readonly string[],
|
|
1185
|
+
state: ExpansionState,
|
|
1186
|
+
): void {
|
|
1187
|
+
const command = words[0];
|
|
1188
|
+
if (LOOP_DYNAMIC_COMMANDS.has(command)) {
|
|
1189
|
+
for (const loop of state.loops) disableLoopVariable(state, loop.name);
|
|
1190
|
+
return;
|
|
1191
|
+
}
|
|
1192
|
+
if (!LOOP_MUTATING_COMMANDS.has(command)) return;
|
|
1193
|
+
for (const arg of words.slice(1)) {
|
|
1194
|
+
const match = /^([A-Za-z_][A-Za-z0-9_]*)(?:=|$)/.exec(arg);
|
|
1195
|
+
if (match) disableLoopVariable(state, match[1]);
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
/**
|
|
1200
|
+
* Apply loop keywords and literal expansion to one segment.
|
|
1201
|
+
*
|
|
1202
|
+
* Returns zero segments for structural statements (`for …`, `done`, a bare
|
|
1203
|
+
* assignment) and one segment per literal loop value otherwise.
|
|
1204
|
+
*/
|
|
1205
|
+
function expandSegment(
|
|
1206
|
+
words: readonly string[],
|
|
1207
|
+
state: ExpansionState,
|
|
1208
|
+
): string[][] {
|
|
1209
|
+
let rest = [...words];
|
|
1210
|
+
if (rest[0] === "do") rest = rest.slice(1);
|
|
1211
|
+
|
|
1212
|
+
if (rest[0] === "done") {
|
|
1213
|
+
state.loops.pop();
|
|
1214
|
+
return [];
|
|
1215
|
+
}
|
|
1216
|
+
if (rest[0] === "for") {
|
|
1217
|
+
state.loops.push(parseForHeader(rest));
|
|
1218
|
+
return [];
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
// A reassignment shadows the loop value from this point on. Do not substitute
|
|
1222
|
+
// the old literal into later commands; leave their variable marker opaque.
|
|
1223
|
+
while (rest.length > 0) {
|
|
1224
|
+
const match = ASSIGNMENT_RE.exec(rest[0]);
|
|
1225
|
+
if (!match) break;
|
|
1226
|
+
disableLoopVariable(state, match[1]);
|
|
1227
|
+
rest = rest.slice(1);
|
|
1228
|
+
}
|
|
1229
|
+
if (rest.length === 0) return [];
|
|
1230
|
+
disableMutatedLoopVariables(rest, state);
|
|
1231
|
+
|
|
1232
|
+
let variants: string[][] = [rest];
|
|
1233
|
+
// Innermost first: `for f in /a; do for f in /b; …` uses the inner binding.
|
|
1234
|
+
for (const loop of [...state.loops].reverse()) {
|
|
1235
|
+
if (loop.values.length === 0) continue;
|
|
1236
|
+
const marker = variableMarker(loop.name);
|
|
1237
|
+
const next: string[][] = [];
|
|
1238
|
+
for (const variant of variants) {
|
|
1239
|
+
for (const value of loop.values) {
|
|
1240
|
+
next.push(variant.map((word) => word.split(marker).join(value)));
|
|
1241
|
+
if (next.length > MAX_EXPANSIONS) return [rest];
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
variants = next;
|
|
1245
|
+
}
|
|
1246
|
+
return variants;
|
|
1247
|
+
}
|
|
1248
|
+
|
|
950
1249
|
/**
|
|
951
1250
|
* Token-level walker: substitutions are analysed where their placeholder
|
|
952
1251
|
* appears, so a payload after `cd /tmp` is judged against `/tmp`.
|
|
@@ -965,6 +1264,7 @@ function parseText(
|
|
|
965
1264
|
}
|
|
966
1265
|
const units: CommandUnit[] = [];
|
|
967
1266
|
let currentBase = base;
|
|
1267
|
+
const expansion: ExpansionState = { loops: [] };
|
|
968
1268
|
for (const segment of segmentTokens(entries)) {
|
|
969
1269
|
const cleaned: string[] = [];
|
|
970
1270
|
for (const word of segment) {
|
|
@@ -979,9 +1279,11 @@ function parseText(
|
|
|
979
1279
|
if (rest !== "") cleaned.push(rest);
|
|
980
1280
|
}
|
|
981
1281
|
if (cleaned.length === 0) continue;
|
|
982
|
-
const
|
|
983
|
-
|
|
984
|
-
|
|
1282
|
+
for (const words of expandSegment(cleaned, expansion)) {
|
|
1283
|
+
const analyzed = analyzeSegment(words, currentBase, depth);
|
|
1284
|
+
units.push(...analyzed.units);
|
|
1285
|
+
currentBase = analyzed.nextBase;
|
|
1286
|
+
}
|
|
985
1287
|
}
|
|
986
1288
|
return units;
|
|
987
1289
|
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* whats-new feature — `CHANGELOG.md` parsing.
|
|
3
|
+
*
|
|
4
|
+
* Contract with the release script (scripts/lib/changelog.mjs):
|
|
5
|
+
*
|
|
6
|
+
* - one `## <version>` section per released version, optionally decorated
|
|
7
|
+
* (`## [0.14.0] — 2026-11-12`); the first `X.Y.Z` in the heading wins;
|
|
8
|
+
* - `## Unreleased` holds the notes for the next release; `pnpm release`
|
|
9
|
+
* renames that heading to the version it is about to publish;
|
|
10
|
+
* - top-level bullets are the notes shown in the notice; nested bullets are
|
|
11
|
+
* detail for humans reading the file and are not repeated in the TUI.
|
|
12
|
+
*
|
|
13
|
+
* Parsing is tolerant on purpose: a malformed section is skipped, never fatal.
|
|
14
|
+
* A missing file degrades to "no notes" with one warning — an extension must
|
|
15
|
+
* not break a session because a documentation file moved.
|
|
16
|
+
*/
|
|
17
|
+
import { readFileSync } from "node:fs";
|
|
18
|
+
import { defaultChangelogFile } from "./paths.js";
|
|
19
|
+
import { compareVersions, parseVersion } from "./version.js";
|
|
20
|
+
|
|
21
|
+
export interface ReleaseNote {
|
|
22
|
+
version: string;
|
|
23
|
+
notes: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const HEADING_RE = /^##\s+(.+?)\s*$/;
|
|
27
|
+
const VERSION_IN_HEADING_RE = /\[?v?(\d+\.\d+\.\d+)\]?/;
|
|
28
|
+
const BULLET_RE = /^[-*]\s+(.+?)\s*$/;
|
|
29
|
+
|
|
30
|
+
/** Split a changelog into version sections, in document order. */
|
|
31
|
+
export function parseChangelog(markdown: string): ReleaseNote[] {
|
|
32
|
+
const releases: ReleaseNote[] = [];
|
|
33
|
+
let current: ReleaseNote | undefined;
|
|
34
|
+
|
|
35
|
+
for (const line of markdown.split(/\r?\n/)) {
|
|
36
|
+
const heading = HEADING_RE.exec(line);
|
|
37
|
+
if (heading) {
|
|
38
|
+
const match = VERSION_IN_HEADING_RE.exec(heading[1]);
|
|
39
|
+
current = match ? { version: match[1], notes: [] } : undefined;
|
|
40
|
+
if (current) releases.push(current);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (!current) continue;
|
|
44
|
+
const bullet = BULLET_RE.exec(line);
|
|
45
|
+
if (bullet) current.notes.push(bullet[1]);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return releases;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Newest first. */
|
|
52
|
+
export function sortReleasesDescending(releases: ReleaseNote[]): ReleaseNote[] {
|
|
53
|
+
return [...releases].sort((a, b) => compareVersions(b.version, a.version));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Notes for a `from → to` upgrade: `from < version <= to`, newest first.
|
|
58
|
+
* Equal and older versions are excluded so a downgrade never replays notes.
|
|
59
|
+
*/
|
|
60
|
+
export function releasesForUpgrade(
|
|
61
|
+
releases: ReleaseNote[],
|
|
62
|
+
from: string,
|
|
63
|
+
to: string,
|
|
64
|
+
): ReleaseNote[] {
|
|
65
|
+
return sortReleasesDescending(releases).filter(
|
|
66
|
+
(release) =>
|
|
67
|
+
compareVersions(release.version, from) > 0 &&
|
|
68
|
+
compareVersions(release.version, to) <= 0,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Paths already warned about, so a missing file warns once per process. */
|
|
73
|
+
const warned = new Set<string>();
|
|
74
|
+
|
|
75
|
+
/** Read and parse the notes; a missing/invalid file becomes an empty list. */
|
|
76
|
+
export function readChangelog(file: string = defaultChangelogFile()): ReleaseNote[] {
|
|
77
|
+
try {
|
|
78
|
+
return parseChangelog(readFileSync(file, "utf8"));
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (!warned.has(file)) {
|
|
81
|
+
warned.add(file);
|
|
82
|
+
// pi-lens-ignore: no-console-except-error,console-statement, — deliberate degradation log
|
|
83
|
+
console.warn(
|
|
84
|
+
"[pi-toolkits/whats-new] Failed to read release notes:",
|
|
85
|
+
error instanceof Error ? error.message : String(error),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The version this copy of the package was published as, if readable. */
|
|
93
|
+
export function readPackageVersion(file: string): string | undefined {
|
|
94
|
+
try {
|
|
95
|
+
const parsed: unknown = JSON.parse(readFileSync(file, "utf8"));
|
|
96
|
+
if (!parsed || typeof parsed !== "object") return undefined;
|
|
97
|
+
const version = (parsed as { version?: unknown }).version;
|
|
98
|
+
return typeof version === "string" && parseVersion(version) !== undefined
|
|
99
|
+
? version
|
|
100
|
+
: undefined;
|
|
101
|
+
} catch {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* whats-new feature — constants and the reasoning behind the notice shape.
|
|
3
|
+
*
|
|
4
|
+
* The feature answers one question — "what changed in the version I just
|
|
5
|
+
* installed?" — and answers it exactly once per upgrade. Release notes live in
|
|
6
|
+
* the repository's `CHANGELOG.md`, which ships inside the npm package and is
|
|
7
|
+
* promoted from `## Unreleased` by `pnpm release` (see scripts/publish.mjs), so
|
|
8
|
+
* the text is written at release time instead of being reconstructed from
|
|
9
|
+
* commit subjects at runtime.
|
|
10
|
+
*
|
|
11
|
+
* Why a custom session entry instead of `ctx.ui.notify`: a startup notice needs
|
|
12
|
+
* its own text and must survive `ui.invalidate()` — the same reason the
|
|
13
|
+
* permission feature renders its autostart notice as an entry (see
|
|
14
|
+
* src/features/permission/index.ts). An entry also stays out of the model
|
|
15
|
+
* context, so release notes never cost a token.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** State file in pi's agent dir (same convention as the other feature stores). */
|
|
19
|
+
export const WHATS_NEW_FILE = "pi-toolkits-whats-new.json";
|
|
20
|
+
|
|
21
|
+
/** Custom entry type; also the renderer key. */
|
|
22
|
+
export const NOTICE_ENTRY = "whats-new:notice";
|
|
23
|
+
|
|
24
|
+
/** Release notes file shipped at the package root. */
|
|
25
|
+
export const CHANGELOG_FILENAME = "CHANGELOG.md";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Newest N versions shown in the startup notice. An operator upgrading from a
|
|
29
|
+
* much older release gets the versions that actually matter, not a wall of
|
|
30
|
+
* text; the complete notes stay in `CHANGELOG.md`.
|
|
31
|
+
*/
|
|
32
|
+
export const MAX_VERSIONS_IN_NOTICE = 4;
|
|
33
|
+
|
|
34
|
+
/** Notes per version in the startup notice; the rest stay in `CHANGELOG.md`. */
|
|
35
|
+
export const MAX_NOTES_PER_VERSION = 6;
|