feat(plugin): auto-inject retrieves on the conversation, not the typed words alone (#4364)
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 11s
CI & Build / integration (push) Successful in 49s
CI & Build / TypeScript typecheck (push) Successful in 53s
CI & Build / Python tests (push) Failing after 1m7s
CI & Build / Build & push image (push) Skipped

The prompt hook sent only the operator's message, so a mid-session
follow-up ("yes do that") named nothing a note, lesson or rule could
match. The hook now reads the tail of the last assistant reply from the
transcript and sends it as `ctx`; the notes and rule arms append it to a
short prompt (<= 280 chars), prompt first, capped at 600 chars. No model
tokens: it is embedding input, and the injected menu's budget is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-23 16:05:21 -04:00
co-authored by Claude Opus 5.5
parent a01deeb851
commit 574b27ae74
6 changed files with 188 additions and 4 deletions
+27
View File
@@ -127,6 +127,33 @@ scribe_json_flat_lines() {
awk -v mode=lines -f "$SCRIBE_HOOK_DIR/scribe_json.awk" 2>/dev/null || true
}
# $1 transcript path → the tail of the last assistant REPLY text, decoded, on
# one line; "" when there is none (#4364). The auto-inject query pairs it with
# a short prompt, so "yes do that" still says what it is about.
#
# Cheap by construction, because it runs on every prompt: the last 512 KB only,
# and grep narrows to assistant records carrying a text block BEFORE anything is
# parsed — a transcript is mostly tool results, and parsing those in awk is the
# multi-second cost scribe_report_check.sh already measured. Fixed strings with
# UNESCAPED quotes can only match at a record's own top level (see that hook).
# A sidechain is a subagent talking, not this session, so it is skipped.
scribe_recent_context() {
[ -n "${1:-}" ] && [ -f "$1" ] || return 0
tail -c 524288 "$1" 2>/dev/null \
| grep -F '"role":"assistant"' 2>/dev/null \
| grep -F '"type":"text"' 2>/dev/null \
| tail -n 4 \
| scribe_json_flat_lines \
| awk -F'\t' '
$2 == ".isSidechain" { side[$1] = $3; next }
$2 ~ /^\.message\.content\[[0-9]+\]\.text$/ { txt[$1] = txt[$1] " " $3; if ($1 > last) last = $1 }
END { for (i = last; i >= 0; i--) if ((i in txt) && side[i] != "true") { print txt[i]; exit } }
' 2>/dev/null \
| scribe_json_unescape \
| tr '\n\t' ' ' \
| awk '{ n = length($0); print (n > 600 ? substr($0, n - 599) : $0) }'
}
# $1 flat text, $2 exact path → the decoded scalar, or "" if absent.
#
# `null` READS AS ABSENT, matching the `// empty` every call site used to carry.