@cratis/pi 7.131.1 → 7.131.2

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.
@@ -7,13 +7,13 @@
7
7
  # `chronicle` group, classified against cratis-store-mutations.json. Anything that is not on the
8
8
  # read-only allowlist — replay, retry, quarantine clearing, job control, recommendations, users,
9
9
  # applications, subscriptions, and any command a newer CLI adds — exits 2 (block the tool call,
10
- # stderr goes back to the model). Unknown commands fail closed.
10
+ # stderr goes back to the model). Unknown commands fail closed, and so does every call while jq is missing.
11
11
  #
12
12
  # Other groups (ai, arc, context, llm, screenplay, prologue, completions) and the top-level commands
13
13
  # (init, new, render, run, update, version, ...) are out of scope and always allowed.
14
14
  #
15
- # What it sees is the command text. A `cratis` reached through a variable, an alias, a function or a
16
- # script file is invisible to it; see .cratis/ai/hooks/README.md for the full list of limits.
15
+ # What it sees is the command text. A `cratis` reached through a variable, an alias, a function, a glob or
16
+ # brace pattern or a script file is invisible to it; see .cratis/ai/hooks/README.md for the full list of limits.
17
17
  #
18
18
  # Escape hatch for a human who authorized the mutation — set in the environment the agent harness was
19
19
  # started from, never inside the command (an assignment in the command is ignored):
@@ -31,15 +31,29 @@ set -euo pipefail
31
31
 
32
32
  input="$(hook_read_stdin)"
33
33
  [ -n "$input" ] || exit 0
34
- hook_have jq || exit 0
34
+ hook_require_jq cratis-guard-store-mutations CRATIS_HOOKS_ALLOW_STORE_MUTATIONS
35
35
 
36
- command_text="$(hook_json "$input" '.tool_input.command')"
36
+ parser_failed() {
37
+ printf 'BLOCKED by cratis-guard-store-mutations: parser failed%s; cannot check this tool call.\n' "${1:+ ($1)}" >&2
38
+ exit 2
39
+ }
40
+
41
+ command_text="$(jq -r '.tool_input.command // empty' <<<"$input")" || parser_failed
37
42
  [ -n "$command_text" ] || exit 0
38
43
 
39
- # Cheap exit for the overwhelming majority of shell commands, which never mention the CLI.
40
- case "$(printf '%s' "$command_text" | tr '[:upper:]' '[:lower:]')" in
44
+ # Cheap exit for the overwhelming majority of shell commands, which never mention the CLI. The shell reads a
45
+ # word after removing quotes, escapes and line continuations, so the name is looked for in that form; a raw
46
+ # `$'` (an ANSI-C string) goes on to the parser, which decodes it.
47
+ probe=${command_text//\\$'\n'/}
48
+ probe=${probe//[\'\"\\]/}
49
+ probe_lower="$(printf '%s' "$probe" | tr '[:upper:]' '[:lower:]')" || parser_failed
50
+ case "$probe_lower" in
41
51
  *cratis*) ;;
42
- *) exit 0 ;;
52
+ *)
53
+ case "$command_text" in
54
+ *'$'\'*) ;;
55
+ *) exit 0 ;;
56
+ esac ;;
43
57
  esac
44
58
 
45
59
  # ── Command lists: shipped defaults + optional local extension, or a replacement ─────────
@@ -53,7 +67,7 @@ if [ -f "$data_file" ]; then
53
67
  merged="$(jq -ce 'select(type == "object" and (.group | type == "string") and
54
68
  (.readOnly | type == "array") and (.mutating | type == "array") and
55
69
  (.helpOptions | type == "array") and (.versionOptions | type == "array") and
56
- (.flagOptions | type == "array") and (.valueOptions | type == "array"))' "$data_file" 2>/dev/null || true)"
70
+ (.flagOptions | type == "array") and (.valueOptions | type == "array"))' "$data_file" 2>/dev/null)" || parser_failed "$data_file"
57
71
  fi
58
72
  if [ -n "$merged" ] && [ -f "$local_file" ]; then
59
73
  # A local file adds entries; it never removes one. A command it lists as mutating is blocked even
@@ -66,7 +80,7 @@ if [ -n "$merged" ] && [ -f "$local_file" ]; then
66
80
  then error("invalid local list") else . end
67
81
  | $base + (["readOnly", "mutating", "helpOptions", "versionOptions", "flagOptions", "valueOptions"]
68
82
  | map({key: ., value: (($base[.] // []) + ($add[.] // []))}) | from_entries)
69
- ' 2>/dev/null || true)"
83
+ ' 2>/dev/null)" || parser_failed "$local_file"
70
84
  if [ -n "$extended" ]; then
71
85
  merged="$extended"
72
86
  else
@@ -81,16 +95,16 @@ data_ok=1
81
95
  [ -n "$merged" ] || { data_ok=0; merged='{}'; }
82
96
 
83
97
  list() {
84
- printf '%s' "$merged" | jq -r "$1" 2>/dev/null || true
98
+ printf '%s' "$merged" | jq -r "$1" 2>/dev/null
85
99
  }
86
100
 
87
- group="$(list '.group // "chronicle"')"
88
- read_only="$(list '(.readOnly // [])[] | .command // empty')"
89
- mutating="$(list '(.mutating // [])[] | select(.command) | "\(.command)\t\(.effect // "")"')"
90
- help_options="$(list '(.helpOptions // [])[]')"
91
- version_options="$(list '(.versionOptions // [])[]')"
92
- flag_options="$(list '(.flagOptions // [])[]')"
93
- value_options="$(list '(.valueOptions // [])[]')"
101
+ group="$(list '.group // "chronicle"')" || parser_failed
102
+ read_only="$(list '(.readOnly // [])[] | .command // empty')" || parser_failed
103
+ mutating="$(list '(.mutating // [])[] | select(.command) | "\(.command)\t\(.effect // "")"')" || parser_failed
104
+ help_options="$(list '(.helpOptions // [])[]')" || parser_failed
105
+ version_options="$(list '(.versionOptions // [])[]')" || parser_failed
106
+ flag_options="$(list '(.flagOptions // [])[]')" || parser_failed
107
+ value_options="$(list '(.valueOptions // [])[]')" || parser_failed
94
108
 
95
109
  tab_char="$(printf '\t')"
96
110
 
@@ -113,10 +127,11 @@ findings="$(
113
127
  CSM_VALUES="$value_options" \
114
128
  LC_ALL=C awk '
115
129
  function toset(text, set, parts, n, k) {
130
+ delete set; delete parts
116
131
  n = split(text, parts, "\n")
117
132
  for (k = 1; k <= n; k++) if (parts[k] != "") set[tolower(parts[k])] = 1
118
133
  }
119
- function words_in(text, parts) { return split(text, parts, " ") }
134
+ function words_in(text, parts) { delete parts; return split(text, parts, " ") }
120
135
  function norm(text) {
121
136
  text = tolower(text); gsub(/[ \t]+/, " ", text); sub(/^ /, "", text); sub(/ $/, "", text)
122
137
  return text
@@ -127,8 +142,42 @@ findings="$(
127
142
  reported[key] = 1
128
143
  printf "%s\t%s\t%s\n", kind, path, effect
129
144
  }
145
+ # True when text can name the CLI once the shell has read it: quotes, escapes and line continuations removed,
146
+ # or an ANSI-C string that the parser decodes.
147
+ function mentions(text, t) {
148
+ if (index(text, "$\047")) return 1
149
+ t = tolower(text)
150
+ gsub("\\\\\n", "", t); gsub("\047", "", t); gsub("\"", "", t); gsub("\\\\", "", t)
151
+ return index(t, "cratis") > 0
152
+ }
153
+ # Decodes the body of an ANSI-C string (i is just after the opening quote) into word and returns the position
154
+ # after the closing quote. The same decoder is in cratis-guard-secrets.sh (ansi_c).
155
+ function ansi_c(s, i, n, c, c2, v, d, k, lim, stop, digits) {
156
+ stop = 0
157
+ while (i <= n) {
158
+ c = substr(s, i, 1)
159
+ if (c == "\047") return i + 1
160
+ if (c != "\\") { if (!stop) word = word c; i++; continue }
161
+ c2 = substr(s, i + 1, 1); i += 2; v = -1
162
+ if (c2 in ESC) v = ESC[c2]
163
+ else if (c2 ~ /^[0-7]$/) {
164
+ v = c2 + 0
165
+ for (k = 0; k < 2 && substr(s, i, 1) ~ /^[0-7]$/; k++) { v = v * 8 + substr(s, i, 1); i++ }
166
+ v = v % 256
167
+ } else if (c2 == "x" || c2 == "u" || c2 == "U") {
168
+ lim = (c2 == "x") ? 2 : (c2 == "u") ? 4 : 8; v = 0; digits = 0
169
+ while (digits < lim && (d = tolower(substr(s, i, 1))) != "" && (d in HEX)) { v = v * 16 + HEX[d]; i++; digits++ }
170
+ if (digits == 0) { if (!stop) word = word "\\" c2; continue }
171
+ if (c2 != "x" && v >= 128) { if (!stop) word = word "?"; continue }
172
+ } else if (c2 == "c" && i <= n) { v = ORD[substr(s, i, 1)] % 32; i++ }
173
+ else { if (!stop) word = word "\\" c2; continue }
174
+ if (v == 0) stop = 1
175
+ else if (!stop) word = word CH[v]
176
+ }
177
+ return i
178
+ }
130
179
  function enqueue(text) {
131
- if (text == "" || !index(tolower(text), "cratis")) return
180
+ if (text == "" || !mentions(text)) return
132
181
  queue[++queued] = text
133
182
  }
134
183
  function base(word) { word = tolower(word); sub(/.*\//, "", word); return word }
@@ -186,7 +235,7 @@ findings="$(
186
235
  if (test == delimiter[k]) break
187
236
  body = body line "\n"
188
237
  }
189
- if (index(tolower(body), "cratis") && candidates < 256) candidate[++candidates] = body
238
+ if (mentions(body) && candidates < 256) candidate[++candidates] = body
190
239
  }
191
240
  pending = 0
192
241
  return i
@@ -228,6 +277,8 @@ findings="$(
228
277
  if (c == "#" && !inword) { while (i <= n && substr(s, i, 1) != "\n") i++; continue }
229
278
  if (c == "$") {
230
279
  c2 = substr(s, i + 1, 1)
280
+ if (c2 == "\047") { inword = 1; quoted = 1; i = ansi_c(s, i + 2, n); continue }
281
+ if (c2 == "\"") { in_dq = 1; inword = 1; quoted = 1; i += 2; continue }
231
282
  if (c2 == "(" && substr(s, i + 2, 1) == "(") { j = skip_group(s, i + 1, "(", ")"); word = word substr(s, i, j - i); inword = 1; i = j; continue }
232
283
  if (c2 == "(") { push("subst"); i += 2; continue }
233
284
  if (c2 == "{") { j = skip_group(s, i + 1, "{", "}"); word = word substr(s, i, j - i); inword = 1; i = j; continue }
@@ -289,6 +340,7 @@ findings="$(
289
340
 
290
341
  # ── simple commands ──
291
342
  function analyze(text, parts, n, k) {
343
+ delete parts
292
344
  n = split(text, parts, SEP)
293
345
  for (k = 1; k <= n; k++) { Q[k] = substr(parts[k], 1, 1); W[k] = substr(parts[k], 2) }
294
346
  count = n
@@ -342,7 +394,8 @@ findings="$(
342
394
  eq = index(lower, "=")
343
395
  return eq > 1 && (substr(lower, 1, eq - 1) in value_option)
344
396
  }
345
- function classify(first, j, m, x, t, lower, name, values, only_options, count_words, path, shown, key) {
397
+ function classify(first, j, m, x, t, lower, name, values, only_options, count_words, command_words, shown, key) {
398
+ delete command_words; delete shown
346
399
  values = " "
347
400
  for (j = first; j <= count; j++) {
348
401
  lower = tolower(W[j])
@@ -381,12 +434,12 @@ findings="$(
381
434
  if (known_value(lower)) continue
382
435
  break
383
436
  }
384
- count_words++; path[count_words] = lower; shown[count_words] = W[m]
437
+ count_words++; command_words[count_words] = lower; shown[count_words] = W[m]
385
438
  }
386
439
  if (count_words == 0) { report("unknown", "cratis " W[j], ""); return }
387
440
  for (m = count_words; m >= 1; m--) {
388
- key = path[1]; t = shown[1]
389
- for (x = 2; x <= m; x++) { key = key " " path[x]; t = t " " shown[x] }
441
+ key = command_words[1]; t = shown[1]
442
+ for (x = 2; x <= m; x++) { key = key " " command_words[x]; t = t " " shown[x] }
390
443
  if (key in mutating) { report("mutating", "cratis " W[j] " " t, mutating[key]); return }
391
444
  if (key in read_only) return
392
445
  }
@@ -405,7 +458,7 @@ findings="$(
405
458
  read_only[key] = 1
406
459
  if (words_in(key) > max_depth) max_depth = words_in(key)
407
460
  }
408
- n = split(ENVIRON["CSM_MUTATING"], lines, "\n")
461
+ delete lines; n = split(ENVIRON["CSM_MUTATING"], lines, "\n")
409
462
  for (k = 1; k <= n; k++) {
410
463
  tab = index(lines[k], "\t"); if (tab == 0) continue
411
464
  key = norm(substr(lines[k], 1, tab - 1)); if (key == "") continue
@@ -421,14 +474,22 @@ findings="$(
421
474
  toset("!\n{\n}\nif\nthen\nelse\nelif\ndo\nwhile\nuntil\ntime", keyword)
422
475
  toset("env\ncommand\nexec\nbuiltin\nnohup\ntime\nnice\nionice\nsudo\ndoas\ntimeout\ngtimeout\nxargs\nparallel\nstdbuf\nunbuffer\ncaffeinate\nwatch\nrtk\ndotnet\narch\nssh\nnoglob\nnocorrect\ntaskset\nflock\nsetsid\nchronic", wrapper)
423
476
 
477
+ # Decoding tables for ANSI-C strings.
478
+ for (k = 1; k < 256; k++) { CH[k] = sprintf("%c", k); ORD[CH[k]] = k }
479
+ delete lines; n = split("0 1 2 3 4 5 6 7 8 9 a b c d e f", lines, " ")
480
+ for (k = 1; k <= n; k++) HEX[lines[k]] = k - 1
481
+ delete lines; n = split("a b e E f n r t v", lines, " "); split("7 8 27 27 12 10 13 9 11", codes, " ")
482
+ for (k = 1; k <= n; k++) ESC[lines[k]] = codes[k] + 0
483
+ ESC["\\"] = 92; ESC["\047"] = 39; ESC["\""] = 34; ESC["?"] = 63
484
+
424
485
  queued = 1; queue[1] = ENVIRON["CSM_COMMAND"]
425
486
  for (done = 1; done <= queued; done++) {
426
487
  if (done > 64) { report("unparsed", "(more than 64 nested shell scripts)", ""); break }
427
- if (index(tolower(queue[done]), "cratis")) parse(queue[done])
488
+ if (mentions(queue[done])) parse(queue[done])
428
489
  }
429
490
  }
430
491
  '
431
- )" || findings="unparsed${tab_char}(the classifier failed, so no cratis command in it can be checked)${tab_char}"
492
+ )" || parser_failed
432
493
 
433
494
  [ -n "$findings" ] || exit 0
434
495
 
@@ -9,6 +9,8 @@
9
9
  # 3. Environment files — .env and friends (secrets)
10
10
  # 4. Agent controls — permissions, hooks and installed managed corpus/adapters
11
11
  #
12
+ # While jq is missing the guard cannot read the tool call, so it refuses every call.
13
+ #
12
14
  # .cratis/ai.json allowDependencyManifestWrites overrides only the manifest policy:
13
15
  # true allows them in any repository; false blocks them even in a Cratis repository.
14
16
  #
@@ -31,10 +33,15 @@ script_directory="${BASH_SOURCE[0]%/*}"
31
33
  input=""
32
34
  IFS= read -r -d '' input || true
33
35
  [ -n "$input" ] || exit 0
34
- hook_have jq || exit 0
36
+ hook_require_jq cratis-guard-writes CRATIS_HOOKS_ALLOW_PROTECTED_WRITES
37
+
38
+ parser_failed() {
39
+ printf '%s\n' 'BLOCKED by cratis-guard-writes: parser failed; cannot check this tool call.' >&2
40
+ exit 2
41
+ }
35
42
 
36
43
  # One jq call reads every field the guard uses. Fields are NUL-terminated (embedded NULs are dropped,
37
- # as a command substitution would drop them); a missing, null, false or unreadable path is empty.
44
+ # as a command substitution would drop them); a missing, null or false path is empty.
38
45
  cwd="" file="" notebook="" new_content="" new_string=""
39
46
  {
40
47
  IFS= read -r -d '' cwd
@@ -49,7 +56,9 @@ cwd="" file="" notebook="" new_content="" new_string=""
49
56
  [field(.cwd), field(.tool_input.file_path), field(.tool_input.notebook_path),
50
57
  field(.tool_input.content), field(.tool_input.new_string)]
51
58
  | .[] | ., "\u0000"
52
- ' <<<"$input" 2>/dev/null) || true
59
+ ' <<<"$input" 2>/dev/null) || parser_failed
60
+ # Process substitutions do not propagate their exit status to the reading group.
61
+ wait "$!" || parser_failed
53
62
  # Command substitution stripped trailing newlines from each field; keep that behaviour.
54
63
  for field_name in cwd file notebook new_content new_string; do
55
64
  while [[ "${!field_name}" == *$'\n' ]]; do printf -v "$field_name" '%s' "${!field_name%$'\n'}"; done
@@ -177,7 +186,7 @@ origin_matches() {
177
186
 
178
187
  # ── 4. Agent controls and installed managed trees ─────────────────────────────
179
188
  agent_control_target() {
180
- local target="$1" directory policy_root target_rel
189
+ local target="$1" directory policy_root target_rel managed
181
190
  dir_of "$target"
182
191
  directory="$REPLY"
183
192
  # Keep the addressed spelling here, even when an adapter directory is a symlink.
@@ -196,18 +205,16 @@ agent_control_target() {
196
205
  .claude/* | .pi/*)
197
206
  # The installer records adapter ownership in Integrations (Path, IsDirectory),
198
207
  # not Files: those destinations are relative to the managed corpus root.
199
- # An unreadable/invalid manifest cannot establish ownership of local adapters.
200
- # Allow unclassified paths; corpus paths and explicit controls still block.
201
- if jq -e --arg path "$target_rel" '
208
+ # A readable manifest must establish ownership before classifying local adapters.
209
+ managed="$(jq -r --arg path "$target_rel" '
202
210
  (.Integrations // [])
203
211
  | if type != "array" then false else
204
212
  [.[] | select(type == "object") | select(.Path | type == "string")]
205
213
  | any(.[]; . as $entry | $entry.Path == $path
206
214
  or ($entry.IsDirectory == true and ($path | startswith($entry.Path + "/"))))
207
215
  end
208
- ' "$policy_root/.cratis/ai.manifest.json" >/dev/null 2>&1; then
209
- return 0
210
- fi
216
+ ' "$policy_root/.cratis/ai.manifest.json" 2>/dev/null)" || parser_failed
217
+ [ "$managed" != "true" ] || return 0
211
218
  ;;
212
219
  esac
213
220
  else
@@ -284,7 +291,7 @@ manifest_writes_allowed() {
284
291
  policy_root="$(git -C "$directory" rev-parse --show-toplevel 2>/dev/null)" || return 1
285
292
  if [ -f "$policy_root/.cratis/ai.json" ]; then
286
293
  # Only actual booleans override the default; jq's `// empty` would discard false.
287
- setting="$(jq -r '.allowDependencyManifestWrites | select(type == "boolean") | tostring' "$policy_root/.cratis/ai.json" 2>/dev/null || true)"
294
+ setting="$(jq -r '.allowDependencyManifestWrites | select(type == "boolean") | tostring' "$policy_root/.cratis/ai.json" 2>/dev/null)" || parser_failed
288
295
  fi
289
296
  case "$setting" in
290
297
  true) return 0 ;;
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env bash
2
2
  # Shared helpers for the Cratis enforcement hooks.
3
3
  #
4
- # Sourced by cratis-guard-writes.sh, cratis-guard-store-mutations.sh, cratis-pattern-scan.sh and
5
- # cratis-quality-gate.sh.
4
+ # Sourced by cratis-guard-secrets.sh, cratis-guard-writes.sh, cratis-guard-store-mutations.sh,
5
+ # cratis-pattern-scan.sh and cratis-quality-gate.sh.
6
6
  # Portable: bash 3.2 (macOS system bash) and up, BSD + GNU userland. No GNU-only flags,
7
7
  # no `mapfile`/`readarray`, no associative arrays, no `eval`.
8
8
  set -euo pipefail
@@ -34,6 +34,21 @@ hook_read_stdin() {
34
34
  cat 2>/dev/null || true
35
35
  }
36
36
 
37
+ # Refuse the tool call when jq is missing: $1 is the guard name, $2 the variable that lets a person run without it.
38
+ # A guard that cannot read the tool call cannot check it, so this ends the sourcing script with exit 2
39
+ # (the deny signal every wired harness honors). It uses only builtins, so it works without jq.
40
+ hook_require_jq() {
41
+ hook_have jq && return 0
42
+ {
43
+ printf 'BLOCKED by %s: jq is not installed, so this guard cannot read the tool call and refuses every call until it is.\n' "$1"
44
+ printf 'Stop and ask the user to install jq (macOS: brew install jq; Debian/Ubuntu: sudo apt-get install jq;\n'
45
+ printf 'Windows: winget install jqlang.jq), then retry. Do not work around the guard.\n'
46
+ printf 'A person who accepts running without this guard sets %s=1 in the environment the agent\n' "$2"
47
+ printf 'harness was started from; setting it inside the command has no effect.\n'
48
+ } >&2
49
+ exit 2
50
+ }
51
+
37
52
  # Extract a single scalar from a JSON document on $1 using the jq path in $2.
38
53
  # Prints the empty string when jq is absent, the JSON is malformed, or the path is null.
39
54
  hook_json() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/pi",
3
- "version": "7.131.1",
3
+ "version": "7.131.2",
4
4
  "description": "Configuration-aware Cratis AI integration for Pi",
5
5
  "type": "module",
6
6
  "files": [