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
+12 -1
View File
@@ -49,6 +49,7 @@ event_flat=$(printf '%s' "$event" | scribe_json_flat)
prompt=$(scribe_json_pick "$event_flat" '.prompt')
session_id=$(scribe_json_pick "$event_flat" '.session_id')
event_cwd=$(scribe_json_pick "$event_flat" '.cwd')
transcript=$(scribe_json_pick "$event_flat" '.transcript_path')
# Nothing to retrieve against.
[ -n "$prompt" ] || exit 0
@@ -79,6 +80,16 @@ q=$(printf '%s' "$prompt" | head -c 2000)
q_enc=$(printf '%s' "$q" | scribe_urlenc) || exit 0
[ -n "$q_enc" ] || exit 0
# What the conversation is about, for a prompt too short to say (#4364). Sent
# beside `q`, never folded into it: the server decides whether the prompt is
# thin enough to need it, for the notes and rule arms alike. Capped at
# 600 chars here as well as there, so the URL stays small whatever the reply.
ctx_q=""
ctx=$(scribe_recent_context "$transcript")
if [ -n "$ctx" ]; then
ctx_enc=$(printf '%s' "$ctx" | scribe_urlenc) && [ -n "$ctx_enc" ] && ctx_q="&ctx=${ctx_enc}"
fi
# Scope to this directory's project — a `.scribe` marker, else the git remote.
repo_dir=${event_cwd:-${CLAUDE_PROJECT_DIR:-$PWD}}
scope=$(scribe_scope_query "$repo_dir")
@@ -120,7 +131,7 @@ fi
body=$(curl -fsS --max-time 5 \
-H "Authorization: Bearer ${token}" \
"${url%/}/api/plugin/retrieve?q=${q_enc}${repo_q}${exclude_q}" 2>/dev/null) || exit 0
"${url%/}/api/plugin/retrieve?q=${q_enc}${ctx_q}${repo_q}${exclude_q}" 2>/dev/null) || exit 0
[ -n "$body" ] || exit 0
body_flat=$(printf '%s' "$body" | scribe_json_flat)
+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.