CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 52s
CI & Build / integration (push) Successful in 1m1s
CI & Build / Python tests (push) Successful in 1m55s
CI & Build / Build & push image (push) Successful in 36s
Step 1 of the Scribe -> Grimoire rename: every capitalised "Scribe" in prose
becomes "Grimoire" ("Fabled Scribe" -> "Fabled Grimoire") across the UI,
the PWA manifest and page title, the MCP instructions and tool docstrings,
hook and skill text, docs and README, with the tests that assert on that
prose moving with it. Old migrations keep their wording.
Identifiers are untouched here: the package, plugin name, MCP server key,
hook file names, env vars, the .scribe marker and FabledScribe repo/image
names move in steps 2 and 3, along with prose that quotes them.
The sweep matched "Scribe" only as a whole word, so describe/subscribe and
friends are untouched. The longer name took the MCP instructions one
character over their 1,600 budget; two phrases are shortened to fit.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
243 lines
11 KiB
Bash
243 lines
11 KiB
Bash
#!/usr/bin/env bash
|
|
# Grimoire plugin — PostToolUse write-path trigger on Bash.
|
|
#
|
|
# scribe_prior_art.sh fires before a Write/Edit TOOL CALL. Code written any
|
|
# other way — sed, heredocs, python edit scripts, `cat > file` — never reaches
|
|
# it, and those are the edits a long session makes most. So after EVERY Bash
|
|
# call it asks git what changed in the working
|
|
# tree since it last looked, and runs the same arms on the definitions that
|
|
# were just written — the local by-name duplicate arm, the recorded prior-art
|
|
# arms and the ledger's derive/divergence checks, via the same
|
|
# /api/plugin/prior-art endpoint the pre-write hook uses.
|
|
#
|
|
# Post-hoc by a few seconds, in the same moment and the same session: "the
|
|
# copy just landed; here is its family" — not "an audit found it later".
|
|
#
|
|
# Cheap when nothing changed: one `git status`. State per session, beside the
|
|
# pre-write hook's (its three dedup channels are SHARED, so a family named by
|
|
# one hook is not named again by the other):
|
|
# ${TMPDIR:-/tmp}/scribe-afterwrite/<sid>.snap path<TAB>blob-hash of every
|
|
# dirty/untracked file last seen
|
|
# ${TMPDIR:-/tmp}/scribe-priorart/<sid>.* the dedup channels
|
|
#
|
|
# NEVER BLOCKS. It returns `additionalContext` only (no decision — there is
|
|
# nothing left to decide, the write already happened). Any failure —
|
|
# unconfigured, unreachable, not a git repo, malformed — exits 0 in silence.
|
|
#
|
|
# Config (same as the other hooks):
|
|
# CLAUDE_PLUGIN_OPTION_API_ENDPOINT base URL, no trailing slash
|
|
# CLAUDE_PLUGIN_OPTION_API_TOKEN fmcp_ API key (sensitive)
|
|
# SCRIBE_URL / SCRIBE_TOKEN override for the settings.json dogfooding path.
|
|
set -uo pipefail
|
|
|
|
command -v git >/dev/null 2>&1 || exit 0
|
|
|
|
# shellcheck source=plugin/hooks/scribe_defs.sh
|
|
. "$(dirname "${BASH_SOURCE[0]}")/scribe_defs.sh"
|
|
|
|
# PostToolUse delivers { session_id, cwd, tool_name, tool_input, tool_response }.
|
|
event=$(cat 2>/dev/null || true)
|
|
event_flat=$(printf '%s' "$event" | scribe_json_flat)
|
|
tool_name=$(scribe_json_pick "$event_flat" '.tool_name')
|
|
[ "$tool_name" = "Bash" ] || exit 0
|
|
session_id=$(scribe_json_pick "$event_flat" '.session_id')
|
|
event_cwd=$(scribe_json_pick "$event_flat" '.cwd')
|
|
work_dir=${event_cwd:-${CLAUDE_PROJECT_DIR:-$PWD}}
|
|
repo_root=$(git -C "$work_dir" rev-parse --show-toplevel 2>/dev/null) || exit 0
|
|
[ -n "$repo_root" ] || exit 0
|
|
|
|
safe_sid=$(printf '%s' "${session_id:-nosession}" | tr -c 'A-Za-z0-9._-' '_')
|
|
snap_dir="${TMPDIR:-/tmp}/scribe-afterwrite"
|
|
mkdir -p "$snap_dir" 2>/dev/null || true
|
|
snap="$snap_dir/${safe_sid}.snap"
|
|
|
|
# What is dirty now: every modified / added / untracked path, with the blob
|
|
# hash of its working-tree content. Hash, not mtime: portable (no stat
|
|
# flags), exact (a touch is not a change), and untracked files hash the same
|
|
# way tracked ones do.
|
|
current=""
|
|
while IFS= read -r line; do
|
|
[ -n "$line" ] || continue
|
|
status=${line:0:2}
|
|
path=${line:3}
|
|
case "$status" in
|
|
D*|*D) continue ;; # a deletion defines nothing
|
|
esac
|
|
case "$path" in
|
|
*" -> "*) path=${path##* -> } ;; # rename: the new name
|
|
esac
|
|
# Porcelain quotes paths with special characters; those are skipped rather
|
|
# than unquoted badly — a filename needing quotes is not where shapes live.
|
|
case "$path" in
|
|
\"*) continue ;;
|
|
esac
|
|
[ -f "$repo_root/$path" ] || continue
|
|
sha=$(git -C "$repo_root" hash-object -- "$path" 2>/dev/null) || continue
|
|
current="${current}${path}"$'\t'"${sha}"$'\n'
|
|
done < <(git -C "$repo_root" status --porcelain --untracked-files=all 2>/dev/null)
|
|
|
|
previous=""
|
|
[ -f "$snap" ] && previous=$(cat "$snap" 2>/dev/null || true)
|
|
first_run=0
|
|
[ -f "$snap" ] || first_run=1
|
|
# Write the new snapshot NOW, before anything can fail below — the next call
|
|
# must compare against this tree, whatever happens to this one's hint.
|
|
printf '%s' "$current" > "$snap" 2>/dev/null || true
|
|
|
|
# Changed = a (path, hash) pair not in the previous snapshot. On the very
|
|
# first call of a session there is no previous snapshot; rather than report
|
|
# every pre-existing dirty file as "just written", take only files touched in
|
|
# the last minute — the Bash call that just ran is the likely author.
|
|
changed=""
|
|
while IFS=$'\t' read -r path sha; do
|
|
[ -n "${path:-}" ] || continue
|
|
if [ "$first_run" = 1 ]; then
|
|
[ -n "$(find "$repo_root/$path" -mmin -1 2>/dev/null)" ] || continue
|
|
else
|
|
case "$previous" in
|
|
*"${path}"$'\t'"${sha}"*) continue ;;
|
|
esac
|
|
fi
|
|
scribe_skip_path "$path" && continue
|
|
changed="${changed}${path}"$'\n'
|
|
done <<< "$current"
|
|
[ -n "$changed" ] || exit 0
|
|
|
|
scribe_config || : # sets url/token; the call below is guarded on them
|
|
scope=$(scribe_scope_query "$repo_root")
|
|
repo_q=""
|
|
[ -n "$scope" ] && repo_q="&${scope}"
|
|
|
|
# The dedup channels are the PRE-write hook's files, on purpose (see header).
|
|
state_dir="${TMPDIR:-/tmp}/scribe-priorart"
|
|
mkdir -p "$state_dir" 2>/dev/null || true
|
|
idfile="$state_dir/${safe_sid}.ids"
|
|
syncfile="$state_dir/${safe_sid}.sync.ids"
|
|
derivefile="$state_dir/${safe_sid}.derive.ids"
|
|
|
|
combined=""
|
|
n_files=0
|
|
while IFS= read -r rel_path; do
|
|
[ -n "${rel_path:-}" ] || continue
|
|
# A Bash call that rewrote many files is a refactor or a generator, not a
|
|
# shape being instantiated; four is enough to name what matters.
|
|
n_files=$((n_files + 1))
|
|
[ "$n_files" -le 4 ] || break
|
|
file_path="$repo_root/$rel_path"
|
|
|
|
# The code just written: the ADDED lines of the uncommitted diff for a
|
|
# tracked file (sed, not cut: this strips one marker char per line, it is
|
|
# not a payload cap), the whole file when untracked.
|
|
fresh=""
|
|
if git -C "$repo_root" ls-files --error-unmatch -- "$rel_path" >/dev/null 2>&1; then
|
|
code=$(git -C "$repo_root" diff -U0 -- "$rel_path" 2>/dev/null | grep '^+' | grep -v '^+++' | sed 's/^+//') || code=""
|
|
else
|
|
code=$(cat "$file_path" 2>/dev/null) || code=""
|
|
fresh="new"
|
|
fi
|
|
[ -n "$code" ] || continue
|
|
# `|| true` inside: an early-exiting `head` must not void its own output.
|
|
names=$(printf '%s' "$code" | scribe_defs | sort -u | head -12 || true)
|
|
# What this write defined, for the end-of-turn question —
|
|
# recorded before the arms below, which skip a write that defines nothing.
|
|
printf '%s\n' "$names" | scribe_written_append "$safe_sid" "$rel_path" "$fresh" "$code"
|
|
# Nothing DEFINED in what was written (prose, data, a call-site edit) →
|
|
# nothing to say; the arms are about shapes.
|
|
[ -n "$names" ] || continue
|
|
|
|
local_lines=$(scribe_local_dups "$repo_root" "$rel_path" <<< "$names") || local_lines=""
|
|
local_context=""
|
|
if [ -n "$local_lines" ]; then
|
|
local_context="> Already defined elsewhere in this repo — \`${rel_path}\` (just written) adds another copy; check before keeping it (\`git grep\` shown; a nudge, not a gate):"$'\n'"${local_lines}"
|
|
fi
|
|
|
|
context=""
|
|
body=""
|
|
reached="" # "" unconfigured (no call owed) · 1 answered · 0 did not
|
|
unreached_context=""
|
|
if [ -n "$url" ] && [ -n "$token" ]; then
|
|
q=$(printf '%s' "$code" | head -c 1200)
|
|
path_enc=$(printf '%s' "$rel_path" | scribe_urlenc) || path_enc=""
|
|
code_enc=$(printf '%s' "$q" | scribe_urlenc) || code_enc=""
|
|
shapes_q=""
|
|
enc=$(printf '%s\n' "$names" \
|
|
| awk -F'\t' 'NF>=2 {printf "%s%s:%s", (n++?",":""), $1, $2}' \
|
|
| scribe_urlenc) || enc=""
|
|
[ -n "$enc" ] && shapes_q="&shapes=${enc}"
|
|
exclude_q=""; sync_exclude_q=""; derive_exclude_q=""
|
|
if [ -f "$idfile" ]; then
|
|
seen=$(tr '\n' ',' < "$idfile" 2>/dev/null | sed 's/,$//')
|
|
[ -n "$seen" ] && exclude_q="&exclude_ids=${seen}"
|
|
fi
|
|
if [ -f "$syncfile" ]; then
|
|
sync_seen=$(tr '\n' ',' < "$syncfile" 2>/dev/null | sed 's/,$//')
|
|
[ -n "$sync_seen" ] && sync_exclude_q="&exclude_sync_ids=${sync_seen}"
|
|
fi
|
|
if [ -f "$derivefile" ]; then
|
|
derive_seen=$(tr '\n' ',' < "$derivefile" 2>/dev/null | sed 's/,$//' | scribe_urlenc) || derive_seen=""
|
|
[ -n "$derive_seen" ] && derive_exclude_q="&exclude_derive=${derive_seen}"
|
|
fi
|
|
if [ -n "$path_enc" ]; then
|
|
# 8s, not the pre-write hook's 5: this hook runs AFTER the tool, so it
|
|
# gates nothing the session is waiting on, and the first prior-art call
|
|
# after a redeploy is a cold start (embedding warm-up takes several
|
|
# seconds) that a tighter cap would turn into a silent fail-open.
|
|
reached=1
|
|
body=$(curl -fsS --max-time 8 \
|
|
-H "Authorization: Bearer ${token}" \
|
|
"${url%/}/api/plugin/prior-art?path=${path_enc}&code=${code_enc}${repo_q}${exclude_q}${sync_exclude_q}${derive_exclude_q}${shapes_q}" 2>/dev/null) || { body=""; reached=0; }
|
|
# A call that was owed and didn't come back is said, once per outage —
|
|
# shared marker with the pre-write hook, so one outage is one
|
|
# line however the code was written.
|
|
if [ "$reached" = 1 ]; then
|
|
scribe_reached "$state_dir" "$safe_sid"
|
|
else
|
|
unreached_context=$(scribe_unreached "$state_dir" "$safe_sid" 8 "$rel_path")
|
|
fi
|
|
fi
|
|
body_flat=""
|
|
if [ -n "$body" ]; then
|
|
body_flat=$(printf '%s' "$body" | scribe_json_flat)
|
|
context=$(scribe_json_pick "$body_flat" '.context')
|
|
if [ -n "$context" ]; then
|
|
scribe_json_list_minus "$body_flat" '.note_ids' '.sync_note_ids' >> "$idfile" || true
|
|
scribe_json_list "$body_flat" '.sync_note_ids' >> "$syncfile" || true
|
|
scribe_json_list "$body_flat" '.derive_keys' >> "$derivefile" || true
|
|
# Several files in one call may name the same family: keep each
|
|
# token once, so the next request's exclude list stays exact.
|
|
for f in "$idfile" "$syncfile" "$derivefile"; do
|
|
[ -s "$f" ] && { sort -u -o "$f" "$f" 2>/dev/null || true; }
|
|
done
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
# The record nudge, same gate as the pre-write hook: duplication
|
|
# demonstrated locally AND nothing recorded for it — and never on a call
|
|
# that did not answer; "nothing recorded" is a claim only an answer
|
|
# can back.
|
|
if [ -n "$local_lines" ] && [ "$reached" != 0 ]; then
|
|
n_recorded=$(scribe_json_len "$body_flat" '.note_ids')
|
|
if [ "${n_recorded:-0}" = "0" ] || [ "$n_recorded" = "" ]; then
|
|
local_context="${local_context}"$'\n'"> None of those existing copies is recorded in Grimoire. If the version just written is the canonical one — or this edit is consolidating the copies — record it now with create_snippet so the next session is offered it instead of writing another copy."
|
|
fi
|
|
fi
|
|
|
|
part="$local_context"
|
|
if [ -n "$context" ]; then
|
|
[ -n "$part" ] && part="${part}"$'\n'
|
|
part="${part}${context}"
|
|
fi
|
|
if [ -n "$unreached_context" ]; then
|
|
[ -n "$part" ] && part="${part}"$'\n'
|
|
part="${part}${unreached_context}"
|
|
fi
|
|
[ -n "$part" ] || continue
|
|
[ -n "$combined" ] && combined="${combined}"$'\n'
|
|
combined="${combined}${part}"
|
|
done <<< "$changed"
|
|
|
|
[ -n "$combined" ] || exit 0
|
|
scribe_json_out PostToolUse "$combined"
|
|
exit 0
|