1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env bash
# basemind PostToolUse read-cache delta.
#
# Fires after Read tool calls (matcher "Read" in hooks.json). When an agent
# RE-READS a file it already read this session, this hook replaces the full file
# body the agent would see with a compact `basemind delta` (+A/-R line-diff),
# saving the tokens of a full re-read. The cache is per-session and keyed by the
# absolute file path; the CALLER (this hook) owns the cache — `basemind delta`
# itself is stateless.
#
# OPT-IN, default OFF — gated on BASEMIND_DELTA_READS, mirroring the
# BASEMIND_COMPRESS_OUTPUT / BASEMIND_GUARD convention used by the sibling hooks:
#
# unset / off / 0 (default) — fast no-op passthrough; the full read stands.
# 1 / on / true — active; serve a delta on a re-read of a cached path.
#
# Safety: this wrapper NEVER errors out the tool call. On any missing dependency,
# unreadable payload, missing session id, empty content, delta failure, or a diff
# that is not meaningfully smaller, it exits 0 with no output, leaving the
# original Read result untouched (passthrough).
#
# Output protocol: a PostToolUse JSON result on stdout whose
# hookSpecificOutput.updatedToolOutput replaces the tool result string. We only
# emit a replacement on a cache HIT that yields a smaller, real diff; otherwise we
# stay silent (exit 0) so the original output is preserved verbatim.
# Gate: anything other than an explicit on-value is a fast no-op passthrough.
MODE=""
# Need jq to read the event payload + emit well-formed JSON; degrade to a
# passthrough no-op if absent.
||
input=""
[ ||
tool=""
[ ||
# Pull the file path the agent read and the content the Read tool returned. The
# content lives at .tool_response.content (a string); fall back to
# .tool_response.file.content defensively in case of an object-shaped result.
file_path=""
[ ||
content=""
[ ||
# Session id keys the cache namespace. No session ⇒ nothing to key on ⇒ no-op.
session=""
[ ||
# Sanitize the session id before building a path from it — it is untrusted input,
# and a value containing "/" or ".." would otherwise let the cache dir escape.
# Collapse everything outside [A-Za-z0-9._-] to "_".
safe_session=""
[ ||
# Per-session cache dir under the managed cache root. Each file is keyed by a hash
# of its absolute path so arbitrary paths map to a flat, collision-resistant name.
CACHE_ROOT="/basemind/read-cache"
SESSION_DIR="/"
||
# Cheap hygiene: prune session dirs untouched for 7+ days so the cache root does
# not grow without bound. Fail-open — never let pruning break the read.
||
# Hash the absolute path to a stable key. Prefer shasum; fall back to sha256sum.
key=""
[ ||
entry="/"
# First sight of this path this session: cache the content, emit nothing. The full
# read stands. Write atomically (temp + mv) so a concurrent read never sees a torn
# entry.
if [; then
tmp="..tmp"
&& ||
fi
# Resolve the basemind binary the same way the launcher / compress-output hook
# does: explicit override, then the managed per-user version cache, then the
# pre-seeded plugin bin/, then PATH. We never trigger a network install here
# (PostToolUse is on the hot path) — if no local binary is found, pass through.
SCRIPT_DIR=""
PLUGIN_ROOT=""
BINARY_NAME="basemind"
# Update the cache entry to the now-current content regardless of the outcome
# below, so the NEXT re-read diffs against what the agent just saw. We snapshot the
# OLD entry first because `basemind delta --old` needs it.
old_entry=".old."
||
tmp="..tmp"
&& ||
# shellcheck disable=SC2329 # invoked indirectly via the EXIT trap below.
BIN=""
[ ||
# Compute the delta: NEW content on stdin, OLD content from the snapshot. The CLI
# is itself fail-open; guard the wrapper too. An empty result ⇒ passthrough.
diff_out=""
[ ||
# Bail / unchanged cases are NOT a win — leave the original read alone:
# "# unchanged" — no change; full read is fine.
# "# file too large for delta; full content ..." — delta gave up; full content.
first_line=""
# Only replace when the delta is meaningfully smaller than the full content the
# agent would otherwise see. Require at least a 10% reduction to avoid churning the
# output for a near-total rewrite. Equal-or-larger ⇒ stay silent (passthrough).
content_len=""
diff_len=""
[ ||
# diff_len < content_len * 0.9 ⇔ diff_len * 10 < content_len * 9
if [; then
fi
# Emit the replacement. A one-line header marks this as a delta vs the previous
# read of the same path, then the compact diff. updatedToolOutput is the string
# the agent sees in place of the full re-read; suppressOutput hides hook chatter.
header="# basemind: delta vs previous read of (re-read; full body suppressed)"
payload=""