Skip to content

Commit dcc8280

Browse files
committed
fix(a2ml-kyx): preflight the work tree, keep names and fields whole, write reports safely
Review findings on #691, each with a test that fails on the previous code (18 new checks fail there, all 67 pass here): - family_of uses '|', not a tab: IFS whitespace collapsed an empty spec, so a contractile reported spec "0" and linked mappings/0. - .a2ml paths stay NUL-delimited through filter, sort and mapfile, so a name with a newline is refused whole instead of split. - jq and sha256sum are checked before anything is classified (exit 69). - jq gets -- before positional reasons, so a reason starting with '-' is data. - The chora deed comes from the index and must be unmodified; the source must be unmodified; .machine_readable/descriptiles must not be a symlink; the target must not be gitignored. All asked before kyX runs. - Staging is all-or-nothing, with a rollback and a refusal on failure. - The random work directory is replaced by <work> in reasons, so a refusal keeps its fingerprint across runs. - A2ML_REPORT_DIR is resolved physically before the containment check; existing non-regular report paths are refused; reports are renamed into place, never written through a link. - The census decodes \u/\U escapes in multi-line strings too. - *file.a2ml is a contractile only under contractiles/ (as rhodibot, #690). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J4QvDhFeexmokLqLRfZNce
1 parent 9941a6d commit dcc8280

3 files changed

Lines changed: 201 additions & 55 deletions

File tree

‎scripts/fix-a2ml-via-kyx.sh‎

Lines changed: 118 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ CENSUS="$SELF_DIR/lib/a2ml-census.js"
8181
usage() { sed -n '/^# Usage:/,/^set -euo/p' "$0" | sed '$d; s/^# \{0,1\}//' >&2; exit 64; }
8282
die() { printf 'fix-a2ml-via-kyx: %s\n' "$1" >&2; exit "${2:-2}"; }
8383

84-
# The tracked .a2ml files, NUL-safe, sorted, case-insensitive suffix.
84+
# The tracked .a2ml files, NUL-delimited end to end (a name with a newline in
85+
# it stays one name, and is refused below), sorted, case-insensitive suffix.
8586
a2ml_files() {
86-
git -C "$1" ls-files -z | tr '\0' '\n' | grep -iE '\.a2ml$' | LC_ALL=C sort || true
87+
git -C "$1" ls-files -z | grep -zaiE '\.a2ml$' | LC_ALL=C sort -z || true
8788
}
8889

8990
check_mode=0
@@ -93,13 +94,20 @@ REPO="$1"
9394
[[ -d "$REPO" ]] || die "not a directory: $REPO" 64
9495
git -C "$REPO" rev-parse --is-inside-work-tree >/dev/null 2>&1 || die "not a git work tree: $REPO" 64
9596
REPO="$(cd "$REPO" && git rev-parse --show-toplevel)"
97+
REPO="$(cd "$REPO" && pwd -P)"
9698

9799
if [[ "$check_mode" -eq 1 ]]; then
98-
a2ml_files "$REPO" | grep -c . || true
100+
a2ml_files "$REPO" | tr -cd '\0' | wc -c | tr -d ' '
99101
exit 0
100102
fi
101103

102-
mapfile -t FILES < <(a2ml_files "$REPO")
104+
# Every refusal is recorded with jq, so it is needed before anything is
105+
# classified, not only on kyX's path: without it there would be no report.
106+
for tool in jq sha256sum; do
107+
command -v "$tool" >/dev/null 2>&1 || die "$tool is required to record any outcome; nothing was changed" 69
108+
done
109+
110+
mapfile -d '' -t FILES < <(a2ml_files "$REPO")
103111
if [[ "${#FILES[@]}" -eq 0 ]]; then
104112
echo "fix-a2ml-via-kyx: no .a2ml in $REPO; nothing to do"
105113
exit 0
@@ -114,42 +122,59 @@ done
114122
REPORT_DIR="${A2ML_REPORT_DIR:-}"
115123
if [[ -z "$REPORT_DIR" ]]; then REPORT_DIR="$(mktemp -d "${TMPDIR:-/tmp}/a2ml-kyx.XXXXXX")"; fi
116124
mkdir -p "$REPORT_DIR"
117-
case "$(cd "$REPORT_DIR" && pwd)/" in "$REPO"/*) die "A2ML_REPORT_DIR must be outside the repository" 64 ;; esac
125+
# Physical paths on both sides: a symlink outside that points inside is inside.
126+
REPORT_DIR="$(cd "$REPORT_DIR" && pwd -P)"
127+
case "$REPORT_DIR/" in "$REPO"/*) die "A2ML_REPORT_DIR must be outside the repository" 64 ;; esac
128+
for name in a2ml-report.json a2ml-pr.md a2ml-p1-issue.md; do
129+
if [[ -L "$REPORT_DIR/$name" || ( -e "$REPORT_DIR/$name" && ! -f "$REPORT_DIR/$name" ) ]]; then
130+
die "$REPORT_DIR/$name exists and is not a regular file; refusing to write through it" 64
131+
fi
132+
done
118133
WORK="$(mktemp -d "${TMPDIR:-/tmp}/a2ml-kyx-work.XXXXXX")"
119134
trap 'rm -rf "$WORK"' EXIT
120135

121136
: > "$WORK/converted.jsonl"
122137
: > "$WORK/refused.jsonl"
123138

139+
# Replace the random work directory with <work> in a file, literally.
140+
scrub() {
141+
local text
142+
text="$(<"$1")"
143+
[[ -n "$text" ]] || return 0
144+
printf '%s\n' "${text//"$WORK"/<work>}" > "$1"
145+
}
146+
124147
refuse() { # path family spec reason...
125148
local path="$1" family="$2" spec="$3"; shift 3
126149
jq -nc --arg path "$path" --arg family "$family" --arg spec "$spec" \
127150
--args '{path:$path, family:$family, spec:(if $spec=="" then null else $spec end), reasons:$ARGS.positional}' \
128-
"$@" >> "$WORK/refused.jsonl"
151+
-- "$@" >> "$WORK/refused.jsonl"
129152
}
130153

131-
# family <path> -> "family<TAB>spec<TAB>kyx(0|1)"; the table rhodibot's
132-
# canon/a2ml.rs reports from, so the finding and the fixer agree.
154+
# family <path> -> "family|spec|kyx(0|1)", spec empty when none; the table
155+
# rhodibot's canon/a2ml.rs reports from, so the finding and the fixer agree.
156+
# The delimiter is "|", not a tab: a tab is IFS whitespace, so `read` would
157+
# collapse an empty spec and shift kyx into it.
133158
family_of() {
134159
local lower name
135160
lower="$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')"
136161
name="${lower##*/}"
137162
case "$name" in
138-
ecosystem.a2ml) printf 'ECOSYSTEM\tmeta-ecosystem-to-repo-deed.adoc\t1\n' ;;
139-
meta.a2ml) printf 'META\tmeta-ecosystem-to-repo-deed.adoc\t0\n' ;;
140-
state.a2ml) printf 'STATE\tstate-v1-decision.adoc\t0\n' ;;
141-
agentic.a2ml) printf 'AGENTIC\tagentic-neurosym-playbook-to-repo-deed.adoc\t0\n' ;;
142-
neurosym.a2ml) printf 'NEUROSYM\tagentic-neurosym-playbook-to-repo-deed.adoc\t0\n' ;;
143-
playbook.a2ml) printf 'PLAYBOOK\tagentic-neurosym-playbook-to-repo-deed.adoc\t0\n' ;;
144-
clade.a2ml) printf 'CLADE\tclade-to-repo-deed.adoc\t0\n' ;;
145-
0-ai-manifest.a2ml) printf 'AI-MANIFEST\tai-manifest-decision.adoc\t0\n' ;;
146-
anchor.a2ml) printf 'ANCHOR\t\t0\n' ;;
147-
rsr-profile.a2ml) printf 'RSR-PROFILE\t\t0\n' ;;
148-
rsr-criteria-v2.a2ml) printf 'RSR-CRITERIA\trsr-criteria-to-praxis-deed.adoc\t0\n' ;;
163+
ecosystem.a2ml) printf 'ECOSYSTEM|meta-ecosystem-to-repo-deed.adoc|1\n' ;;
164+
meta.a2ml) printf 'META|meta-ecosystem-to-repo-deed.adoc|0\n' ;;
165+
state.a2ml) printf 'STATE|state-v1-decision.adoc|0\n' ;;
166+
agentic.a2ml) printf 'AGENTIC|agentic-neurosym-playbook-to-repo-deed.adoc|0\n' ;;
167+
neurosym.a2ml) printf 'NEUROSYM|agentic-neurosym-playbook-to-repo-deed.adoc|0\n' ;;
168+
playbook.a2ml) printf 'PLAYBOOK|agentic-neurosym-playbook-to-repo-deed.adoc|0\n' ;;
169+
clade.a2ml) printf 'CLADE|clade-to-repo-deed.adoc|0\n' ;;
170+
0-ai-manifest.a2ml) printf 'AI-MANIFEST|ai-manifest-decision.adoc|0\n' ;;
171+
anchor.a2ml) printf 'ANCHOR||0\n' ;;
172+
rsr-profile.a2ml) printf 'RSR-PROFILE||0\n' ;;
173+
rsr-criteria-v2.a2ml) printf 'RSR-CRITERIA|rsr-criteria-to-praxis-deed.adoc|0\n' ;;
149174
*)
150-
if [[ "$lower" == *bot_directives/* ]]; then printf 'BOT-DIRECTIVES\t\t0\n'
151-
elif [[ "$name" == *file.a2ml ]]; then printf 'CONTRACTILE\t\t0\n'
152-
else printf 'UNMAPPED\t\t0\n'; fi ;;
175+
if [[ "$lower" == *bot_directives/* ]]; then printf 'BOT-DIRECTIVES||0\n'
176+
elif [[ "$name" == *file.a2ml && "$lower" == *contractiles/* ]]; then printf 'CONTRACTILE||0\n'
177+
else printf 'UNMAPPED||0\n'; fi ;;
153178
esac
154179
}
155180

@@ -182,14 +207,38 @@ AFFIRMED="${AFFIRMED:-$(date -u +%Y-%m-%d)}"
182207
ACCEPT_DROPS="${A2ML_ACCEPT_SPEC_DROPS:-0}"
183208
[[ "$ACCEPT_DROPS" == 0 || "$ACCEPT_DROPS" == 1 ]] || die "A2ML_ACCEPT_SPEC_DROPS must be 0 or 1" 64
184209

185-
# Exactly one chora deed at the root (D312), or none to convert against.
186-
shopt -s nullglob
187-
choras=("$REPO"/*_chora.deed)
188-
shopt -u nullglob
210+
# Exactly one chora deed at the root (D312), or none to convert against. It is
211+
# taken from the index, not the filesystem: the PR carries only the deed and
212+
# the deletion, so the chora kyX reads must be the committed one.
213+
mapfile -d '' -t choras < <(git -C "$REPO" ls-files -z -- ':(glob)*_chora.deed')
214+
215+
# Why `src` cannot be converted in this work tree right now, or nothing. Asked
216+
# BEFORE kyX runs, so a failed `git add`/`git rm` cannot leave half a change.
217+
preflight() { # src
218+
local src="$1" dir
219+
if [[ -n "$(git -C "$REPO" status --porcelain -- "${choras[0]}")" ]]; then
220+
echo "${choras[0]} has uncommitted changes; kyX must read the committed chora deed the PR will reference."; return
221+
fi
222+
if [[ -n "$(git -C "$REPO" status --porcelain -- "$src")" ]]; then
223+
echo "the source has uncommitted changes; kyX would convert content the PR does not delete."; return
224+
fi
225+
# Each directory on the way to the target must be absent or a real
226+
# directory: a symlink would put the deed outside the work tree.
227+
dir="$REPO"
228+
for part in .machine_readable descriptiles; do
229+
dir="$dir/$part"
230+
if [[ -L "$dir" || ( -e "$dir" && ! -d "$dir" ) ]]; then
231+
echo "${dir#"$REPO"/} is a symlink or not a directory; the deed is written only inside the work tree."; return
232+
fi
233+
done
234+
if git -C "$REPO" check-ignore -q --no-index -- "$TARGET"; then
235+
echo "$TARGET is gitignored here, so it could not be staged."; return
236+
fi
237+
}
189238

190239
eco_sources=()
191240
for f in "${FILES[@]}"; do
192-
IFS=$'\t' read -r fam spec kyx <<< "$(family_of "$f")"
241+
IFS='|' read -r fam spec kyx <<< "$(family_of "$f")"
193242
if [[ "$kyx" != 1 ]]; then
194243
if [[ -n "$spec" ]]; then
195244
refuse "$f" "$fam" "$spec" "kyX has no front end for $fam yet; $spec decides where it goes. Do not hand-convert (D313)."
@@ -222,11 +271,19 @@ for src in "${eco_sources[@]}"; do
222271
refuse "$src" ECOSYSTEM "$spec" "$TARGET already exists; it is never overwritten. Reconcile the two by hand-review, not by hand-conversion."
223272
continue
224273
fi
274+
blocked="$(preflight "$src")"
275+
if [[ -n "$blocked" ]]; then
276+
refuse "$src" ECOSYSTEM "$spec" "$blocked"
277+
continue
278+
fi
225279
need_tools
226280
out="$WORK/ECOSYSTEM.deed"; rm -f "$out" "$WORK/kyx.err" "$WORK/ast.json"
227281
rc=0
228-
bun "$KYX" a2ml ecosystem "$REPO/$src" --chora "${choras[0]}" --affirmed "$AFFIRMED" --out "$out" \
282+
bun "$KYX" a2ml ecosystem "$REPO/$src" --chora "$REPO/${choras[0]}" --affirmed "$AFFIRMED" --out "$out" \
229283
> "$WORK/kyx.out" 2> "$WORK/kyx.err" || rc=$?
284+
# The work directory is random per run; a reason that names it would give
285+
# the same refusal a new fingerprint every time, and a duplicate P1 issue.
286+
scrub "$WORK/kyx.err"
230287
mapfile -t kyx_err < "$WORK/kyx.err"
231288
if [[ "$rc" -eq 1 ]]; then
232289
mapfile -t why < <(grep '^kyx: refused: ' "$WORK/kyx.err" | sed 's/^kyx: refused: /kyX refused: /')
@@ -243,6 +300,7 @@ for src in "${eco_sources[@]}"; do
243300
continue
244301
fi
245302
if ! bun "$DEED_LINT" --ecosystem --root "$REPO" "$out" > "$WORK/lint.out" 2>&1; then
303+
scrub "$WORK/lint.out"
246304
mapfile -t lint < "$WORK/lint.out"
247305
refuse "$src" ECOSYSTEM "$spec" "kyX's output does not pass standards deed_lint --ecosystem --root (canon grammar):" "${lint[@]}"
248306
continue
@@ -255,7 +313,7 @@ for src in "${eco_sources[@]}"; do
255313
fi
256314
done
257315
if ! bun "$DEED_LINT" --ast-json "$out" > "$WORK/ast.json" 2> "$WORK/ast.err"; then
258-
refuse "$src" ECOSYSTEM "$spec" "deed_lint --ast-json could not read kyX's output: $(head -c 400 "$WORK/ast.err")"
316+
refuse "$src" ECOSYSTEM "$spec" "deed_lint --ast-json could not read kyX's output: $(scrub "$WORK/ast.err"; head -c 400 "$WORK/ast.err")"
259317
continue
260318
fi
261319
crc=0
@@ -265,28 +323,46 @@ for src in "${eco_sources[@]}"; do
265323
mapfile -t lost < <(jq -r '.missing[] | "value not found in the deed: " + (tojson)' <<< "$census_json")
266324
refuse "$src" ECOSYSTEM "$spec" "not lossless: the value census found source values the deed does not carry:" "${lost[@]}"
267325
else
268-
refuse "$src" ECOSYSTEM "$spec" "the value census could not read the source, so losslessness is unproven: $(head -c 400 "$WORK/census.err")"
326+
refuse "$src" ECOSYSTEM "$spec" "the value census could not read the source, so losslessness is unproven: $(scrub "$WORK/census.err"; head -c 400 "$WORK/census.err")"
269327
fi
270328
continue
271329
fi
272-
# Every gate passed: stage the deed and the deletion.
273-
mkdir -p "$REPO/$(dirname "$TARGET")"
274-
cp "$out" "$REPO/$TARGET"
275-
git -C "$REPO" add -- "$TARGET"
276-
git -C "$REPO" rm -q -- "$src"
330+
# Every gate passed: stage the deed and the deletion, all or nothing. The
331+
# preflight makes a failure here unlikely; if one happens anyway, the tree
332+
# and index are put back and the file is refused with the reason.
333+
if ! { mkdir -p "$REPO/$(dirname "$TARGET")" \
334+
&& cp "$out" "$REPO/$TARGET" \
335+
&& git -C "$REPO" add -- "$TARGET" \
336+
&& git -C "$REPO" rm -q -- "$src"; } 2> "$WORK/stage.err"; then
337+
git -C "$REPO" reset -q -- "$TARGET" "$src" 2>/dev/null || true
338+
rm -f -- "$REPO/$TARGET"
339+
git -C "$REPO" checkout -q -- "$src" 2>/dev/null || true
340+
refuse "$src" ECOSYSTEM "$spec" "staging the conversion failed and was rolled back: $(head -c 400 "$WORK/stage.err")"
341+
continue
342+
fi
277343
jq -nc --arg source "$src" --arg target "$TARGET" --arg kyx "$KYX_COMMIT" --arg std "$STANDARDS_COMMIT" \
278344
--arg affirmed "$AFFIRMED" --argjson census "$census_json" --args \
279345
'{source:$source, target:$target, kyx_commit:$kyx, standards_commit:$std, affirmed:$affirmed,
280346
values_checked:$census.source_values, spec_drops:$ARGS.positional}' \
281-
"${drops[@]}" >> "$WORK/converted.jsonl"
347+
-- "${drops[@]}" >> "$WORK/converted.jsonl"
282348
done
283349

284350
repo_name="$(basename "$REPO")"
285351
jq -n --arg repo "$repo_name" --arg accept "$ACCEPT_DROPS" \
286352
--slurpfile converted "$WORK/converted.jsonl" --slurpfile refused "$WORK/refused.jsonl" \
287353
'{repo:$repo, accept_spec_drops:($accept=="1"), converted:$converted, refused:$refused}' \
288-
> "$REPORT_DIR/a2ml-report.json"
289-
rm -f "$REPORT_DIR/a2ml-pr.md" "$REPORT_DIR/a2ml-p1-issue.md"
354+
> "$WORK/a2ml-report.json"
355+
356+
# Each report is written to a new file in REPORT_DIR and renamed over the old
357+
# one: a rename replaces a link instead of writing through it.
358+
publish() { # source name
359+
local tmp
360+
tmp="$(mktemp "$REPORT_DIR/.$2.XXXXXX")"
361+
cat -- "$1" > "$tmp"
362+
mv -fT -- "$tmp" "$REPORT_DIR/$2"
363+
}
364+
publish "$WORK/a2ml-report.json" a2ml-report.json
365+
rm -f -- "$REPORT_DIR/a2ml-pr.md" "$REPORT_DIR/a2ml-p1-issue.md"
290366

291367
n_conv="$(jq '.converted | length' "$REPORT_DIR/a2ml-report.json")"
292368
n_ref="$(jq '.refused | length' "$REPORT_DIR/a2ml-report.json")"
@@ -304,7 +380,8 @@ if [[ "$n_conv" -gt 0 ]]; then
304380
"",
305381
"Review it like any PR: the deed is owner-facing, and :affirmed records the date of this conversion.\n",
306382
"_Opened by gitbot-fleet `scripts/fix-a2ml-via-kyx.sh` (opportunistic A2ML retirement)._"
307-
' "$REPORT_DIR/a2ml-report.json" > "$REPORT_DIR/a2ml-pr.md"
383+
' "$REPORT_DIR/a2ml-report.json" > "$WORK/a2ml-pr.md"
384+
publish "$WORK/a2ml-pr.md" a2ml-pr.md
308385
fi
309386

310387
if [[ "$n_ref" -gt 0 ]]; then
@@ -319,7 +396,8 @@ if [[ "$n_ref" -gt 0 ]]; then
319396
""),
320397
"What unblocks each: a kyX front end for the family (metadatastician/kyx), a mapping spec where there is none, or a fix to the source that kyX names.\n",
321398
"_Opened by gitbot-fleet `scripts/fix-a2ml-via-kyx.sh`; it will not open a second issue for the same refusal._"
322-
' "$REPORT_DIR/a2ml-report.json" > "$REPORT_DIR/a2ml-p1-issue.md"
399+
' "$REPORT_DIR/a2ml-report.json" > "$WORK/a2ml-p1-issue.md"
400+
publish "$WORK/a2ml-p1-issue.md" a2ml-p1-issue.md
323401
fi
324402

325403
echo "fix-a2ml-via-kyx: $n_conv converted, $n_ref refused; reports in $REPORT_DIR"

‎scripts/lib/a2ml-census.js‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,31 @@ export function a2mlValues(text) {
4444
else return;
4545
}
4646
};
47+
// One escape, after its backslash; the same rules in "..." and """...""".
48+
const escape = () => {
49+
const e = s[i++];
50+
const map = { n: "\n", t: "\t", r: "\r", '"': '"', "\\": "\\", b: "\b", f: "\f" };
51+
if (e in map) return map[e];
52+
if (e === "u" || e === "U") {
53+
const n = e === "u" ? 4 : 8;
54+
const hex = s.slice(i, i + n);
55+
if (!/^[0-9A-Fa-f]+$/.test(hex) || hex.length !== n) err("bad \\u escape");
56+
const cp = parseInt(hex, 16);
57+
if (cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff)) err("\\u escape is not a Unicode scalar value");
58+
i += n;
59+
return String.fromCodePoint(cp);
60+
}
61+
err(`bad escape \\${e}`);
62+
};
4763
const basic = () => {
4864
// after the opening quote
4965
let v = "";
5066
for (;;) {
5167
const c = s[i++];
5268
if (c === undefined || c === "\n") err("unterminated string");
5369
if (c === '"') return v;
54-
if (c === "\\") {
55-
const e = s[i++];
56-
const map = { n: "\n", t: "\t", r: "\r", '"': '"', "\\": "\\", b: "\b", f: "\f" };
57-
if (e in map) v += map[e];
58-
else if (e === "u" || e === "U") {
59-
const n = e === "u" ? 4 : 8;
60-
const hex = s.slice(i, i + n);
61-
if (!/^[0-9A-Fa-f]+$/.test(hex) || hex.length !== n) err("bad \\u escape");
62-
v += String.fromCodePoint(parseInt(hex, 16));
63-
i += n;
64-
} else err(`bad escape \\${e}`);
65-
} else v += c;
70+
if (c === "\\") v += escape();
71+
else v += c;
6672
}
6773
};
6874
const multi = (q) => {
@@ -79,9 +85,7 @@ export function a2mlValues(text) {
7985
while (peek() === "\n" || peek() === " " || peek() === "\t") { if (peek() === "\n") line++; i++; }
8086
continue;
8187
}
82-
const e = s[i++];
83-
const map = { n: "\n", t: "\t", r: "\r", '"': '"', "\\": "\\", b: "\b", f: "\f" };
84-
if (e in map) v += map[e]; else err(`bad escape \\${e}`);
88+
v += escape();
8589
} else v += c;
8690
}
8791
};

0 commit comments

Comments
 (0)