fix(8139): a shape the agent walked past at the end-of-turn check is named again in its next notes

The after phase answers `left`, the asked lines still unjudged; the Stop hook
carries them as `earlier=` into the next checks, each up to two more asks, and
the note names them apart from the last turn's writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 09:41:09 -05:00
co-authored by Claude Opus 5.5
parent 69f3adae2c
commit 55f53de92d
7 changed files with 260 additions and 40 deletions
+81 -17
View File
@@ -20,7 +20,16 @@
# removes it once the instance has answered);
# <sid>.asked — the lines that note named. At the first stop after the note
# was carried, they go back as `phase=after`, which records
# whether they were judged, and are then forgotten.
# whether they were judged;
# <sid>.asked_earlier — the same, for the carried lines the note named
# apart from this turn's;
# <sid>.earlier — what the after phase said is still unjudged, as
# `N<TAB>path<TAB>kind<TAB>name`, N the asks walked past.
# These ride the next check as `earlier=`, so a shape the
# agent moved on from is named again rather than surviving
# only as a count. A line walked past more than
# GRIMOIRE_EARLIER_MAX times is dropped: past that the
# repeat is noise, and the coverage line still counts it.
# A note no prompt has carried yet (two stops in a row) is asked again with
# that turn's writes added, so nothing is dropped and nothing is asked twice.
#
@@ -55,17 +64,23 @@ state_dir="${TMPDIR:-/tmp}/grimoire-shapecheck"
mkdir -p "$state_dir" 2>/dev/null || true
note_file="$state_dir/${safe_sid}.note"
asked_file="$state_dir/${safe_sid}.asked"
asked_earlier_file="$state_dir/${safe_sid}.asked_earlier"
earlier_file="$state_dir/${safe_sid}.earlier"
# Each walked-past shape is asked this many more times, then dropped.
earlier_max="${GRIMOIRE_EARLIER_MAX:-2}"
# A note no prompt has carried: its lines join this turn's, asked afresh.
# A note no prompt has carried: its lines join this turn's, asked afresh. Its
# carried lines are still in <sid>.earlier, so they ride the next check too.
if [ -f "$note_file" ]; then
if [ -s "$asked_file" ]; then
mkdir -p "$(dirname "$ledger")" 2>/dev/null || true
cat "$asked_file" >> "$ledger" 2>/dev/null || true
fi
rm -f "$note_file" "$asked_file" 2>/dev/null || true
rm -f "$note_file" "$asked_file" "$asked_earlier_file" 2>/dev/null || true
fi
[ -s "$asked_file" ] || [ -s "$ledger" ] || exit 0
[ -s "$asked_file" ] || [ -s "$asked_earlier_file" ] || [ -s "$ledger" ] \
|| [ -s "$earlier_file" ] || exit 0
grimoire_config || exit 0
dir="${event_cwd:-${CLAUDE_PROJECT_DIR:-$PWD}}"
@@ -89,32 +104,81 @@ esac
# key runs the plugin), and a turn that wrote more than this generated code.
_unique() { awk '!seen[$0]++' "$1" 2>/dev/null | head -n 80; }
# The note a prompt carried: were its lines judged since?
if [ -s "$asked_file" ]; then
asked=$(_unique "$asked_file")
asked_enc=$(printf '%s' "$asked" | grimoire_urlenc) || asked_enc=""
if [ -n "$asked_enc" ] && curl -fsS --max-time 4 \
# The note a prompt carried: were its lines judged since? What is still
# unjudged comes back as `left`, and becomes the carried set, each line's
# walked-past count one higher than it was.
if [ -s "$asked_file" ] || [ -s "$asked_earlier_file" ]; then
asked=""
[ -s "$asked_file" ] && asked=$(_unique "$asked_file")
asked_q=""
if [ -n "$asked" ]; then
asked_enc=$(printf '%s' "$asked" | grimoire_urlenc) || asked_enc=""
asked_q="&written=${asked_enc}"
fi
if [ -s "$asked_earlier_file" ]; then
prior_enc=$(_unique "$asked_earlier_file" | grimoire_urlenc) || prior_enc=""
[ -n "$prior_enc" ] && asked_q="${asked_q}&earlier=${prior_enc}"
fi
if after=$(curl -fsS --max-time 4 \
-H "Authorization: Bearer ${token}" \
-H "X-Grimoire-Plugin: ${plugin_version:-}" \
"${url%/}/api/plugin/shape-check?phase=after&written=${asked_enc}${scope_q}" \
>/dev/null 2>&1; then
rm -f "$asked_file" 2>/dev/null || true
"${url%/}/api/plugin/shape-check?phase=after${asked_q}${scope_q}" 2>/dev/null); then
rm -f "$asked_file" "$asked_earlier_file" 2>/dev/null || true
left_file="$state_dir/${safe_sid}.left"
grimoire_json_pick "$(printf '%s' "$after" | grimoire_json_flat)" '.left' \
> "$left_file" 2>/dev/null || true
prev="$earlier_file"
[ -f "$prev" ] || prev=/dev/null
awk -F'\t' -v max="$earlier_max" '
FILENAME == ARGV[1] { if (NF == 4) seen[$2 "\t" $3 "\t" $4] = $1; next }
NF == 3 {
n = (($0 in seen) ? seen[$0] : 0) + 1
if (n <= max) print n "\t" $0
}' "$prev" "$left_file" > "${earlier_file}.new" 2>/dev/null || true
mv -f "${earlier_file}.new" "$earlier_file" 2>/dev/null || true
[ -s "$earlier_file" ] || rm -f "$earlier_file" 2>/dev/null || true
rm -f "$left_file" 2>/dev/null || true
fi
fi
[ -s "$ledger" ] || exit 0
written=$(_unique "$ledger")
written_enc=$(printf '%s' "$written" | grimoire_urlenc) || exit 0
written=""
[ -s "$ledger" ] && written=$(_unique "$ledger")
# The carried lines, minus any this turn wrote again (those are this turn's).
earlier=""
if [ -s "$earlier_file" ]; then
mine_file="$state_dir/${safe_sid}.mine"
printf '%s\n' "$written" > "$mine_file" 2>/dev/null || true
earlier=$(awk -F'\t' '
FILENAME == ARGV[1] { if (NF == 3) mine[$0] = 1; next }
NF == 4 { line = $2 "\t" $3 "\t" $4; if (!(line in mine) && !seen[line]++) print line }
' "$mine_file" "$earlier_file" 2>/dev/null | head -n 40)
rm -f "$mine_file" 2>/dev/null || true
fi
[ -n "$written" ] || [ -n "$earlier" ] || exit 0
check_q=""
if [ -n "$written" ]; then
written_enc=$(printf '%s' "$written" | grimoire_urlenc) || exit 0
check_q="&written=${written_enc}"
fi
if [ -n "$earlier" ]; then
earlier_enc=$(printf '%s' "$earlier" | grimoire_urlenc) || earlier_enc=""
[ -n "$earlier_enc" ] && check_q="${check_q}&earlier=${earlier_enc}"
fi
answer=$(curl -fsS --max-time 4 \
-H "Authorization: Bearer ${token}" \
-H "X-Grimoire-Plugin: ${plugin_version:-}" \
"${url%/}/api/plugin/shape-check?phase=check&written=${written_enc}${scope_q}" 2>/dev/null) \
"${url%/}/api/plugin/shape-check?phase=check${check_q}${scope_q}" 2>/dev/null) \
|| exit 0 # unreachable: keep the ledger
note=$(grimoire_json_pick "$(printf '%s' "$answer" | grimoire_json_flat)" '.note')
if [ -n "$note" ]; then
printf '%s' "$note" > "$note_file" 2>/dev/null || true
printf '%s\n' "$written" > "$asked_file" 2>/dev/null || true
[ -n "$written" ] && printf '%s\n' "$written" > "$asked_file" 2>/dev/null
[ -n "$earlier" ] && printf '%s\n' "$earlier" > "$asked_earlier_file" 2>/dev/null
else
# Nothing unjudged, the carried lines included: they were answered.
rm -f "$earlier_file" 2>/dev/null || true
fi
rm -f "$ledger" 2>/dev/null || true
exit 0