@cratis/pi 7.171.0 → 7.172.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.
@@ -374,7 +374,8 @@ for protected references, including nested strings, heredoc bodies and option va
374
374
  removes simple quoting/escapes, checks case-insensitive spellings, and asks the write guard's
375
375
  `--references` batch mode to classify candidate paths relative to the hook cwd. Original
376
376
  spellings are retained too so existing generated headers can be checked on case-sensitive
377
- filesystems. Glob/bracket patterns are checked against that same policy; brace patterns
377
+ filesystems. The lowercase alias of a path is dropped only where the original path exists and its
378
+ lowercase spelling provably does not (a case-sensitive lookup); any doubt keeps it. Glob/bracket patterns are checked against that same policy; brace patterns
378
379
  are conservative. A directory containing owned controls/managed paths also counts, such as
379
380
  `.claude`, `.pi/extensions` or installed `.cratis/ai`; no second tree list lives in the shell
380
381
  guard. Other trees, including `.github/workflows`, count only if the write policy protects them.
@@ -49,21 +49,28 @@ if [[ "$command_text" =~ ^[[:space:]]*(cratis|ln)[[:space:]] ]] && command -v no
49
49
  fi
50
50
 
51
51
  IFS= read -r -d '' program <<'AWK' || true
52
- function candidate(s) {
52
+ # `origin` is empty for a real candidate and the original spelling for its lowercase alias. In targets mode
53
+ # an alias is printed as \001origin\001alias so the shell can drop it where it provably names no path.
54
+ # A real candidate is never hidden by an earlier alias with the same text (seen: 1 alias, 2 real).
55
+ function candidate(s, origin, prior, marker) {
53
56
  if (checking_marker) return
54
57
  if (s == "" || s ~ /[\n\r\t]/) return
58
+ marker = ENVIRON["GSW_MODE"] == "targets"
55
59
  if (collecting) {
56
60
  if (s in collected) return
57
61
  if (++collected_count > 256 || length(s) > 4096) exit 2
58
62
  collected[s] = 1
59
63
  } else {
60
- if (s in seen) return
64
+ if (origin == "" && marker && index(s, "\001")) exit 2
65
+ prior = (s in seen) ? seen[s] : 0
66
+ if (prior && (origin != "" || prior == 2)) return
61
67
  if (++candidates > 256 || length(s) > 4096) exit 2
62
- seen[s] = 1; print s
68
+ seen[s] = (origin == "") ? 2 : 1
69
+ if (origin != "" && marker) print "\001" origin "\001" s; else print s
63
70
  }
64
- if (s != tolower(s)) candidate(tolower(s))
71
+ if (s != tolower(s)) candidate(tolower(s), s)
65
72
  # A generic attached option prefix must not hide a path spelling (-t.pi/...).
66
- if (s ~ /^-/ && match(s, /[.\/]/) && RSTART > 1) candidate(substr(s, RSTART))
73
+ if (s ~ /^-/ && match(s, /[.\/]/) && RSTART > 1) candidate(substr(s, RSTART), (origin == "") ? "" : substr(origin, RSTART))
67
74
  }
68
75
  # An extended-glob group (`@(a|b)`, `*(x)`) is also read as one star, so `.e@(n)v` meets `.env`.
69
76
  # A word with a brace span and quoting or expansion syntax is also kept whole, so the classifier sees it unsplit.
@@ -268,11 +275,16 @@ function short_flags(w, flags, k) {
268
275
  return 1
269
276
  }
270
277
  function number(s) { return s ~ /^[+-]?[0-9]+$/ }
271
- function options_allowed(verb, first, i, w, filters, patterns) {
278
+ function options_allowed(verb, first, i, w, filters, patterns, ended) {
272
279
  for (i = first; i <= argc; i++) {
273
280
  w = W[i]
274
- if (w == "--") return 1
275
- if (w !~ /^-/ || w == "-") { patterns++; continue }
281
+ if (!ended && w == "--") { if (verb !~ /^(sort|uniq)$/) return 1; ended = 1; continue }
282
+ if (ended || w !~ /^-/ || w == "-") {
283
+ # Admit sort/uniq only as stdin filters. File operands, including after
284
+ # --, retain whole-command classification and every existing refusal.
285
+ if (verb ~ /^(sort|uniq)$/) return 0
286
+ patterns++; continue
287
+ }
276
288
  if ((verb, w) in long_options) continue
277
289
  if (verb == "head" || verb == "tail" || verb == "git log") {
278
290
  if (w == "-n" || (verb != "git log" && w == "-c")) {
@@ -709,11 +721,48 @@ function readonly(s, i, c, c2, sq, dq) {
709
721
  (directory_only && (nonconditional_separator || (has_pipeline && changed_index)))) fail()
710
722
  return !bad
711
723
  }
724
+ # Only a complete, top-level literal cat heredoc is data. A quote, substitution,
725
+ # comment, backslash or previous heredoc before its opening leaves the whole text
726
+ # unclassified: the apparent cat might otherwise be inside another shell construct.
727
+ # Reference extraction always uses the original text, including every body. Only
728
+ # the read-only check reduces a proven heredoc to cat and its output redirection.
729
+ # Protected bodies may be printed but never materialized to a file. Never execute text.
730
+ function strip_data_heredocs(text, lines, count, i, j, line, before, after, delimiter, quoted, body, prefix, out) {
731
+ count = split(text, lines, "\n")
732
+ for (i = 1; i <= count; i++) {
733
+ line = lines[i]
734
+ if (prefix !~ /[\047"\\$`#<>]/ &&
735
+ line ~ /^[ \t]*cat[ \t]+((>|>>)[ \t]*[-.\/A-Za-z0-9_]+[ \t]+)?<<[ \t]*(\047[A-Za-z_][A-Za-z0-9_]*\047|"[A-Za-z_][A-Za-z0-9_]*"|[A-Za-z_][A-Za-z0-9_]*)[ \t]*((>|>>)[ \t]*[-.\/A-Za-z0-9_]+)?[ \t]*$/) {
736
+ before = substr(line, 1, index(line, "<<") - 1)
737
+ after = substr(line, index(line, "<<") + 2)
738
+ if (!(index(before, ">") && index(after, ">"))) {
739
+ sub(/^[ \t]+/, "", after); sub(/[ \t]*(>|>>).*$/, "", after); sub(/[ \t]+$/, "", after)
740
+ quoted = after ~ /^[\047"]/
741
+ delimiter = after; gsub(/[\047"]/, "", delimiter)
742
+ body = ""
743
+ for (j = i + 1; j <= count && lines[j] != delimiter; j++) body = body lines[j] "\n"
744
+ if (j <= count && (quoted || body !~ /[$`\\]/)) {
745
+ if (index(before, ">") || index(line, ">")) heredoc_writes = 1
746
+ sub(/<<[ \t]*(\047[A-Za-z_][A-Za-z0-9_]*\047|"[A-Za-z_][A-Za-z0-9_]*"|[A-Za-z_][A-Za-z0-9_]*)/, "", line)
747
+ out = out (i > 1 ? "\n" : "") line
748
+ for (; i < j; i++) prefix = prefix lines[i] "\n"
749
+ prefix = prefix lines[i] "\n"
750
+ continue
751
+ }
752
+ }
753
+ }
754
+ out = out (i > 1 ? "\n" : "") line
755
+ prefix = prefix line "\n"
756
+ }
757
+ return out
758
+ }
712
759
  BEGIN {
713
760
  policy("cat", "nAvetsb", "")
714
761
  policy("head", "", ""); policy("tail", "", "")
715
762
  policy("wc", "lwcm", ""); policy("ls", "laA1hdRtrF", "")
716
763
  policy("rg", "nli", "--files")
764
+ policy("sort", "nrufb", "--numeric-sort --reverse --unique --ignore-case --ignore-leading-blanks")
765
+ policy("uniq", "cdui", "--count --repeated --unique --ignore-case")
717
766
  policy("grep", "nirRlLcvwxFEHhoqs", "")
718
767
  policy("egrep", "nirRlLcvwxFEHhoqs", ""); policy("fgrep", "nirRlLcvwxFEHhoqs", "")
719
768
  policy("echo", "neE", "")
@@ -743,13 +792,17 @@ BEGIN {
743
792
  match_rewrite_safe = !index(text, MATCH_MARKER) && !index(decoded, MATCH_MARKER)
744
793
  if (ENVIRON["GSW_MODE"] == "references") { emit_text(text); exit 0 }
745
794
  if (ENVIRON["GSW_MODE"] == "targets") {
746
- readonly(text)
747
- if (unparsed) emit_text(text)
795
+ reduced = strip_data_heredocs(text)
796
+ readonly(reduced)
797
+ # Reducing data must never erase a protected reference and skip the final
798
+ # read-only check, even when the reduced command itself has no targets.
799
+ if (unparsed || reduced != text) emit_text(text)
748
800
  else for (i = 1; i <= emitted_count; i++) emit_text(emitted[i])
749
801
  exit 0
750
802
  }
751
803
  n = split(ENVIRON["GSW_PROTECTED"], a, "\n"); for (i = 1; i <= n; i++) protected_paths[a[i]] = 1
752
- exit readonly(text) ? 0 : 1
804
+ reduced = strip_data_heredocs(text)
805
+ exit (readonly(reduced) && !heredoc_writes) ? 0 : 1
753
806
  }
754
807
  AWK
755
808
  # This stage is independent of the readonly/reference grammar above. Prefix syntax is
@@ -1500,8 +1553,50 @@ glob_shell_safe=1
1500
1553
  if [[ -n "${BASH_ENV+x}" || -n "${ENV+x}" || -n "${GLOBIGNORE+x}" ]]; then glob_shell_safe=0; fi
1501
1554
  candidates_text="$(GSW_COMMAND="$command_text" GSW_CWD="$cwd" GSW_GLOB_SAFE="$glob_shell_safe" GSW_MODE=targets LC_ALL=C awk "$program" </dev/null)" || parser_failed
1502
1555
  [ -n "$candidates_text" ] || exit 0
1556
+ # The lowercase alias of a mixed-case path serves case-insensitive file systems. Drop it only where the
1557
+ # longest existing prefix of the original spelling exists and its lowercase spelling provably does not
1558
+ # (a case-sensitive lookup that names nothing). Any doubt keeps the alias (return 0).
1559
+ alias_needed() {
1560
+ local original="$1" base="" prefix lower parent
1561
+ case "$original" in *[A-Z]*) ;; *) return 0 ;; esac
1562
+ [ -d "$cwd" ] || return 0
1563
+ case "$original" in /*) ;; *) base="$cwd/" ;; esac
1564
+ # A `..` component (also in the working directory) is resolved by the classifier lexically before symlinks.
1565
+ case "/$base$original/" in */../*) return 0 ;; esac
1566
+ prefix="$original"
1567
+ while :; do
1568
+ while [ "${#prefix}" -gt 1 ] && [ "${prefix%/}" != "$prefix" ]; do prefix="${prefix%/}"; done
1569
+ case "$prefix" in "" | /) return 0 ;; esac
1570
+ if [ -e "$base$prefix" ] || [ -L "$base$prefix" ]; then break; fi
1571
+ case "$prefix" in */*) prefix="${prefix%/*}" ;; *) return 0 ;; esac
1572
+ done
1573
+ case "$prefix" in *[A-Z]*) ;; *) return 0 ;; esac
1574
+ lower="${2:0:${#prefix}}"
1575
+ [ "${#lower}" -eq "${#prefix}" ] || return 0
1576
+ if [ -e "$base$lower" ] || [ -L "$base$lower" ]; then return 0; fi
1577
+ # Absence only counts when the nearest existing ancestor of that spelling is a searchable directory
1578
+ # (the lookup then ends in "no such entry", not in a permission or other lookup error).
1579
+ parent="$lower"
1580
+ while :; do
1581
+ case "$parent" in */*) parent="${parent%/*}" ;; *) parent="" ;; esac
1582
+ [ -n "$parent" ] || [ "${lower#/}" = "$lower" ] || parent="/"
1583
+ if [ -z "$parent" ]; then parent="${base:-/}"; break; fi
1584
+ if [ -e "$base$parent" ] || [ -L "$base$parent" ]; then parent="$base$parent"; break; fi
1585
+ done
1586
+ [ -d "$parent" ] && [ -x "$parent" ] || return 0
1587
+ return 1
1588
+ }
1503
1589
  candidates=()
1504
- while IFS= read -r candidate; do candidates[${#candidates[@]}]="$candidate"; done <<<"$candidates_text"
1590
+ while IFS= read -r candidate; do
1591
+ case "$candidate" in
1592
+ $'\001'*)
1593
+ alias_rest="${candidate#?}"
1594
+ if alias_needed "${alias_rest%%$'\001'*}" "${alias_rest#*$'\001'}"; then candidates[${#candidates[@]}]="${alias_rest#*$'\001'}"; fi
1595
+ ;;
1596
+ *) candidates[${#candidates[@]}]="$candidate" ;;
1597
+ esac
1598
+ done <<<"$candidates_text"
1599
+ [ "${#candidates[@]}" -gt 0 ] || exit 0
1505
1600
  classifier="$script_directory/cratis-guard-writes.sh"
1506
1601
  [ -f "$classifier" ] || {
1507
1602
  printf '%s\n' 'BLOCKED by cratis-guard-shell-writes: could not check references (write guard is missing).' >&2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/pi",
3
- "version": "7.171.0",
3
+ "version": "7.172.0",
4
4
  "description": "Configuration-aware Cratis AI integration for Pi",
5
5
  "type": "module",
6
6
  "files": [