CI & Build / TypeScript typecheck (push) Successful in 41s
CI & Build / Plugin hooks (push) Successful in 17s
CI & Build / Python lint (push) Successful in 3s
CI & Build / Python tests (push) Failing after 1m41s
CI & Build / integration (push) Successful in 1m59s
CI & Build / Build & push image (push) Skipped
Every rule arm now ranks what it delivers with one function (services/rule_order.py): enforcement, then salience, then how narrowly the rule was reached, then first mention before reminder, then the rule's own open rate at that moment, then similarity. A crowded moment drops the same rules whichever arm brought them, and a rule that holds is never cut for a line beside it. The open rate comes from a new table, rule_deliveries (migration 0129): one row per rule line a plugin session is shown, full or repeat, with the open stamped on the latest delivery it followed. The plugin routes bind the session per request; the prompt, pre-tool and write-path hooks now send it, and the opened hook reports every open, with its acts only when no hook had just named the rule. Rows are pruned daily after 180 days. Also fixes the TypeScript check: the store's list row now carries the new enforcement field. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
5.7 KiB
Bash
121 lines
5.7 KiB
Bash
#!/usr/bin/env bash
|
|
# Grimoire — record that this session OPENED a rule, not merely saw it named.
|
|
#
|
|
# WHAT THIS ANSWERS
|
|
#
|
|
# The rule arms keep a ledger of every id they have NAMED. The line those arms
|
|
# emit is a TEASER — title, trigger, `get_rule(N)` — so a session can be named
|
|
# a rule twenty times and never read one word of it, and after a compaction
|
|
# the teaser is summarised away leaving nothing at all. Named is not read, so
|
|
# the server needs to be told which rules were opened.
|
|
#
|
|
# This is the observable half. PostToolUse fires for MCP tools (the event's own
|
|
# output schema carries `updatedMCPToolOutput`, which would be meaningless
|
|
# otherwise), so the `get_rule` CALL can be watched directly.
|
|
#
|
|
# WHY THIS IS NOT SELF-REPORT
|
|
#
|
|
# Asking the session whether it holds a rule is useless: a model asked "do you
|
|
# still hold rule N?" will say yes, and the answer is unverifiable. That
|
|
# objection is about ASKING. This asks nobody:
|
|
# a tool call happened or it did not, and the harness reports it either way.
|
|
# Recording what a session DID is a different kind of evidence from believing
|
|
# what it says about itself.
|
|
#
|
|
# WHAT IT DELIBERATELY DOES NOT DO
|
|
#
|
|
# It does not prove the rule is still in context — nothing can, and a
|
|
# compaction can drop it moments later. That is why `.opened.ids` ages exactly
|
|
# like `.rules.ids` and is cleared on the same events: both ledgers
|
|
# describe a context that no longer exists once the context is destroyed. The
|
|
# claim it supports is only ever "you opened this, pull it again if you no
|
|
# longer hold it", which stays true in every case and carries its own remedy.
|
|
#
|
|
# It also reports the open, with the calls that came just before it, so the server can learn which moments a rule belongs
|
|
# on (below). Silent on outage, like every arm that fires per call.
|
|
#
|
|
# EXIT 0, ALWAYS. This decorates a ledger; a bookkeeping failure must never
|
|
# turn a successful tool call into a hook error. Worst case the id is missed
|
|
# and the reader is offered a rule it already read — the cost of a wrong guess
|
|
# here is one extra line, in the direction that shows more rather than less.
|
|
set -uo pipefail
|
|
|
|
# shellcheck source=plugin/hooks/grimoire_defs.sh
|
|
. "$(dirname "${BASH_SOURCE[0]}")/grimoire_defs.sh"
|
|
|
|
event=$(cat 2>/dev/null || true)
|
|
[ -n "$event" ] || exit 0
|
|
|
|
event_flat=$(printf '%s' "$event" | grimoire_json_flat)
|
|
session_id=$(grimoire_json_pick "$event_flat" '.session_id')
|
|
[ -n "$session_id" ] || exit 0
|
|
|
|
# The matcher in hooks.json already narrows to the get_rule tools, but the
|
|
# server segment of an MCP tool name varies with how the plugin was installed,
|
|
# so the id is read from whichever field is actually present rather than from
|
|
# an assumed tool name. An event that carries none simply records nothing.
|
|
rule_id=$(grimoire_json_pick "$event_flat" '.tool_input.rule_id')
|
|
rule_id=$(printf '%s' "$rule_id" | tr -cd '0-9')
|
|
[ -n "$rule_id" ] || exit 0
|
|
|
|
# The same directory the naming ledger uses. One session keeps its state in one
|
|
# place, and the prior-art name is kept for the reason grimoire_tool_rules.sh
|
|
# gives: renaming it would orphan every live session's state for a cosmetic
|
|
# gain.
|
|
state_dir="${TMPDIR:-/tmp}/grimoire-priorart"
|
|
mkdir -p "$state_dir" 2>/dev/null || true
|
|
safe_sid=$(printf '%s' "$session_id" | tr -c 'A-Za-z0-9._-' '_')
|
|
|
|
# Stamped and append-only, exactly like the naming ledger — so the same reader
|
|
# (`grimoire_rules_live`) ages both, and the last entry for an id wins.
|
|
printf '%s\n' "$rule_id" | grimoire_rules_append "$state_dir/${safe_sid}.opened.ids"
|
|
|
|
# ── What came just before the open ───────────────────────────────────────
|
|
#
|
|
# grimoire_moment.sh writes every call to `grimoire-moment/<sid>.acts`. The
|
|
# last three, from the last ninety seconds, go to the server with this rule's
|
|
# id: a rule the agent went and opened right after a moment fired is evidence
|
|
# it belongs on that moment, and once that repeats across sessions the server
|
|
# answers with one line putting the question to the reader. Nothing is
|
|
# mounted by this.
|
|
#
|
|
# EVERY OPEN IS REPORTED: the server stamps it on this session's latest
|
|
# delivery of the rule, which is how an open rate per delivery is measured.
|
|
#
|
|
# The acts go with it only when the rule was NOT just named by a hook. A
|
|
# ranked arm or a moment line put a named rule in front of the reader, and
|
|
# that explains the open; the moment beside it did not, so counting it would
|
|
# propose mounts on whatever moment happened to fire while search was
|
|
# surfacing the rule. The window is short for the same reason: a few minutes
|
|
# of agent work reaches many moments.
|
|
command -v curl >/dev/null 2>&1 || exit 0
|
|
grimoire_config || exit 0
|
|
acts=""
|
|
case ",$(grimoire_rules_live "$state_dir/${safe_sid}.rules.ids")," in
|
|
*",${rule_id},"*) ;;
|
|
*)
|
|
acts_file="${TMPDIR:-/tmp}/grimoire-moment/${safe_sid}.acts"
|
|
now=$(date +%s 2>/dev/null) || now=0
|
|
if [ -f "$acts_file" ] && [ "$now" -gt 0 ]; then
|
|
acts=$(awk -F '\t' -v since=$((now - 90)) \
|
|
'$1 + 0 >= since { sub(/^[^\t]*\t/, ""); print }' "$acts_file" 2>/dev/null \
|
|
| tail -n 3 | paste -sd ',' - 2>/dev/null)
|
|
fi
|
|
;;
|
|
esac
|
|
|
|
sid_esc=$(printf '%s' "$session_id" | grimoire_json_escape) || exit 0
|
|
event_cwd=$(grimoire_json_pick "$event_flat" '.cwd')
|
|
scope=$(grimoire_scope_query "${event_cwd:-${CLAUDE_PROJECT_DIR:-$PWD}}")
|
|
body=$(printf '{"rule_id":%s,"session_id":"%s","acts":[%s]}' "$rule_id" "$sid_esc" "$acts" \
|
|
| curl -fsS --max-time 3 \
|
|
-H "Authorization: Bearer ${token}" \
|
|
-H "Content-Type: application/json" \
|
|
--data-binary @- \
|
|
"${url%/}/api/plugin/rule-opened${scope:+?$scope}" 2>/dev/null) || exit 0
|
|
|
|
context=$(grimoire_json_pick "$(printf '%s' "$body" | grimoire_json_flat)" '.context')
|
|
[ -n "$context" ] || exit 0
|
|
grimoire_json_out PostToolUse "$context"
|
|
exit 0
|