Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
d7b76b3
🧹 Refactor simplify-ignore hook to use a shared library
google-labs-jules[bot] May 10, 2026
56215e3
Merge pull request #1 from gotaku3737-sketch/simplify-ignore-refactor…
gotaku3737-sketch May 10, 2026
2aa1911
perf: consolidate multiple jq calls in sdd-cache-pre.sh
google-labs-jules[bot] May 10, 2026
66cad6c
Merge pull request #2 from gotaku3737-sketch/perf-opt-jq-consolidatio…
gotaku3737-sketch May 10, 2026
8d28a97
test: add unit tests for extract_header in sdd-cache-post.sh
google-labs-jules[bot] May 12, 2026
c98ec54
Merge pull request #3 from gotaku3737-sketch/test-sdd-cache-post-extr…
gotaku3737-sketch May 12, 2026
5101708
Refactor sdd-cache hooks to avoid eval and consolidate shared logic
google-labs-jules[bot] Jul 17, 2026
129e95f
⚡ Optimize simplify-ignore block matching performance
google-labs-jules[bot] Jul 17, 2026
ff092ee
Merge pull request #5 from gotaku3737-sketch/perf-simplify-ignore-glo…
gotaku3737-sketch Aug 28, 2026
d53ebd1
Merge pull request #4 from gotaku3737-sketch/fix/security-eval-comman…
gotaku3737-sketch Aug 28, 2026
f6a62af
Merge branch 'addyosmani:main' into main
gotaku3737-sketch Aug 28, 2026
1860e8d
perf: Optimize array sorting in renderProducts
google-labs-jules[bot] Sep 5, 2026
788fc39
Refactor duplicated test setup in scripts into test-utils.js
google-labs-jules[bot] Sep 5, 2026
222088d
Merge pull request #6 from gotaku3737-sketch/fix/performance-optimiza…
gotaku3737-sketch Sep 5, 2026
5ea7b3a
Merge pull request #7 from gotaku3737-sketch/fix-duplicated-test-setu…
gotaku3737-sketch Sep 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions evals/fixtures/performance-optimization/products.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
'use strict';
"use strict";

function renderProducts(products) {
let html = '';
let html = "";

const sortedProducts = [...products].sort((a, b) => b.sales - a.sales);
const ranks = new Map();
for (let i = 0; i < sortedProducts.length; i++) {
const id = sortedProducts[i].id;
if (!ranks.has(id)) {
ranks.set(id, i + 1);
}
}

for (const product of products) {
const rank = [...products]
.sort((a, b) => b.sales - a.sales)
.findIndex((candidate) => candidate.id === product.id) + 1;
const rank = ranks.get(product.id);
html += `<li data-rank="${rank}">${product.name}: ${product.sales}</li>`;
}
return `<ul>${html}</ul>`;
Expand Down
36 changes: 36 additions & 0 deletions hooks/sdd-cache-lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
# sdd-cache-lib.sh — Shared functions for sdd-cache hooks and tests

# Cache key is sha256(URL), truncated to 128 bits (32 hex chars).
hash_key() {
if command -v shasum >/dev/null 2>&1; then
printf '%s' "$1" | shasum -a 256 | cut -c1-32
else
printf '%s' "$1" | sha256sum | cut -c1-32
fi
}

# Extracts the final response's headers (last paragraph) to avoid picking
# up validators from intermediate 301/302 hops.
get_final_headers() {
local head_out="$1"
printf '%s' "$head_out" | awk '
BEGIN { RS = ""; last = "" }
{ last = $0 }
END { print last }
'
}

extract_header() {
local final_headers="$1"
local name="$2"
printf '%s' "$final_headers" | awk -v h="$name" '
BEGIN { FS = ":" }
tolower($1) == tolower(h) {
sub(/^[^:]*:[ \t]*/, "")
sub(/[ \t]+$/, "")
print
exit
}
'
}
70 changes: 70 additions & 0 deletions hooks/sdd-cache-post-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash
# sdd-cache-post-test.sh — Tests for the sdd-cache-post hook
#
# Run: bash hooks/sdd-cache-post-test.sh

set -euo pipefail

# Source shared library
# shellcheck source=hooks/sdd-cache-lib.sh
. "$(dirname "$0")/sdd-cache-lib.sh"

PASS=0 FAIL=0

assert_eq() {
local label="$1" expected="$2" actual="$3"
if [ "$expected" = "$actual" ]; then
PASS=$((PASS + 1))
printf ' PASS: %s\n' "$label"
else
FAIL=$((FAIL + 1))
printf ' FAIL: %s\n' "$label" >&2
printf ' expected: %s\n' "$(printf '%s' "$expected" | cat -v)" >&2
printf ' actual: %s\n' "$(printf '%s' "$actual" | cat -v)" >&2
fi
}

# ── Test 1: get_final_headers ─────────────────────────────────────────────
printf 'Test 1: get_final_headers\n'

# Single block
HEADS="HTTP/1.1 200 OK
Content-Type: text/plain
ETag: \"foo\""
assert_eq "single block" "$HEADS" "$(get_final_headers "$HEADS")"

# Multiple blocks (redirect)
MULTI_HEADS="HTTP/1.1 301 Moved Permanently
Location: https://example.com/

HTTP/1.1 200 OK
Content-Type: text/html
ETag: \"bar\""
EXPECTED="HTTP/1.1 200 OK
Content-Type: text/html
ETag: \"bar\""
assert_eq "multiple blocks (redirect)" "$EXPECTED" "$(get_final_headers "$MULTI_HEADS")"

# ── Test 2: extract_header ────────────────────────────────────────────────
printf '\nTest 2: extract_header\n'

# We need to pass FINAL_HEADERS to extract_header
FINAL_HEADERS="HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
ETag: \"12345\"
Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT
X-Multi-Colon: value:with:colons
Whitespace: leading and trailing "

assert_eq "extract ETag" "\"12345\"" "$(extract_header "$FINAL_HEADERS" "ETag")"
assert_eq "case insensitivity (lower)" "\"12345\"" "$(extract_header "$FINAL_HEADERS" "etag")"
assert_eq "case insensitivity (upper)" "\"12345\"" "$(extract_header "$FINAL_HEADERS" "ETAG")"
assert_eq "handle colons in value" "Wed, 21 Oct 2015 07:28:00 GMT" "$(extract_header "$FINAL_HEADERS" "Last-Modified")"
assert_eq "handle multiple colons in value" "value:with:colons" "$(extract_header "$FINAL_HEADERS" "X-Multi-Colon")"
assert_eq "strip whitespace" "leading and trailing" "$(extract_header "$FINAL_HEADERS" "Whitespace")"
assert_eq "non-existent header" "" "$(extract_header "$FINAL_HEADERS" "X-Missing")"

# ── Summary ──────────────────────────────────────────────────────────────
printf '\n══════════════════════════════════════════\n'
printf 'Results: %d passed, %d failed\n' "$PASS" "$FAIL"
[ "$FAIL" -eq 0 ] && exit 0 || exit 1
34 changes: 6 additions & 28 deletions hooks/sdd-cache-post.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,9 @@ if [ -z "$CONTENT" ]; then
fi
dbg "extracted content bytes=${#CONTENT}"

# Must match the pre hook: sha256(URL), first 32 hex chars.
hash_key() {
if command -v shasum >/dev/null 2>&1; then
printf '%s' "$1" | shasum -a 256 | cut -c1-32
else
printf '%s' "$1" | sha256sum | cut -c1-32
fi
}
# Source shared library
# shellcheck source=hooks/sdd-cache-lib.sh
. "$(dirname "$0")/sdd-cache-lib.sh"

CACHE_DIR="${CLAUDE_PROJECT_DIR:-$PWD}/.claude/sdd-cache"
mkdir -p "$CACHE_DIR"
Expand All @@ -83,27 +78,10 @@ HEAD_OUT=$(curl -sI -L --max-time 5 "$URL" 2>/dev/null | tr -d '\r' || true)

# Take only the final response's headers (last paragraph) to avoid picking
# up validators from intermediate 301/302 hops.
FINAL_HEADERS=$(printf '%s' "$HEAD_OUT" | awk '
BEGIN { RS = ""; last = "" }
{ last = $0 }
END { print last }
')

extract_header() {
local name="$1"
printf '%s' "$FINAL_HEADERS" | awk -v h="$name" '
BEGIN { FS = ":" }
tolower($1) == tolower(h) {
sub(/^[^:]*:[ \t]*/, "")
sub(/[ \t]+$/, "")
print
exit
}
'
}
FINAL_HEADERS=$(get_final_headers "$HEAD_OUT")

ETAG=$(extract_header "ETag")
LAST_MOD=$(extract_header "Last-Modified")
ETAG=$(extract_header "$FINAL_HEADERS" "ETag")
LAST_MOD=$(extract_header "$FINAL_HEADERS" "Last-Modified")
dbg "HEAD etag=$ETAG last_modified=$LAST_MOD"

if [ -z "$ETAG" ] && [ -z "$LAST_MOD" ]; then
Expand Down
22 changes: 10 additions & 12 deletions hooks/sdd-cache-pre.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,23 @@ URL=$(printf '%s' "$INPUT" | jq -r '.tool_input.url // empty' 2>/dev/null || tru
if [ -z "$URL" ]; then dbg "no url in tool_input, exit"; exit 0; fi
dbg "url=$URL"

# Cache key is sha256(URL), truncated to 128 bits.
hash_key() {
if command -v shasum >/dev/null 2>&1; then
printf '%s' "$1" | shasum -a 256 | cut -c1-32
else
printf '%s' "$1" | sha256sum | cut -c1-32
fi
}
# Source shared library
# shellcheck source=hooks/sdd-cache-lib.sh
. "$(dirname "$0")/sdd-cache-lib.sh"

CACHE_DIR="${CLAUDE_PROJECT_DIR:-$PWD}/.claude/sdd-cache"
CACHE_FILE="$CACHE_DIR/$(hash_key "$URL").json"

if [ ! -f "$CACHE_FILE" ]; then dbg "no cache file at $CACHE_FILE, exit"; exit 0; fi
dbg "cache file exists: $CACHE_FILE"

FETCHED_AT=$(jq -r '.fetched_at // 0' "$CACHE_FILE" 2>/dev/null || echo 0)
ORIGINAL_PROMPT=$(jq -r '.prompt // empty' "$CACHE_FILE" 2>/dev/null || true)
ETAG=$(jq -r '.etag // empty' "$CACHE_FILE" 2>/dev/null || true)
LAST_MOD=$(jq -r '.last_modified // empty' "$CACHE_FILE" 2>/dev/null || true)
# Use a single jq call to extract all fields at once for performance.
eval "$(jq -r '
"FETCHED_AT=" + ((.fetched_at // 0) | @sh),
"ORIGINAL_PROMPT=" + ((.prompt // "") | @sh),
"ETAG=" + ((.etag // "") | @sh),
"LAST_MOD=" + ((.last_modified // "") | @sh)
' "$CACHE_FILE" 2>/dev/null || echo "FETCHED_AT=0; ORIGINAL_PROMPT=''; ETAG=''; LAST_MOD=''")"

# No validator means we cannot verify freshness — never serve from cache.
if [ -z "$ETAG" ] && [ -z "$LAST_MOD" ]; then
Expand Down
111 changes: 111 additions & 0 deletions hooks/simplify-ignore-lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/bin/bash
# simplify-ignore-lib.sh — Shared functions for simplify-ignore hook and tests

hash_cmd() {
if command -v shasum >/dev/null 2>&1; then shasum
elif command -v sha1sum >/dev/null 2>&1; then sha1sum
else printf '%s\n' "error: missing shasum or sha1sum" >&2; exit 1; fi
}

file_id() { printf '%s' "$1" | hash_cmd | cut -c1-16; }

block_hash() { printf '%s' "$1" | hash_cmd | cut -c1-8; }

# Escape glob metacharacters so ${var/pattern/repl} treats pattern as literal.
# Needed for Bash 3.2 (macOS) where quotes don't suppress globbing in PE patterns.
escape_glob() {
local s="$1"
s="${s//\\/\\\\}"
s="${s//\*/\\*}"
s="${s//\?/\\?}"
s="${s//\[/\\[}"
printf '%s' "$s"
}

# ── filter_file: replace simplify-ignore blocks with BLOCK_<hash> placeholders ─
# Reads $1 (source), writes filtered version to $2 (dest), saves blocks to cache.
# Returns 0 if blocks were found, 1 if none.
filter_file() {
local src="$1" dest="$2" fid="$3"
: > "$dest"
rm -f "$CACHE/${fid}".block.* "$CACHE/${fid}".reason.* "$CACHE/${fid}".prefix.* "$CACHE/${fid}".suffix.*

local count=0 in_block=0 buf="" reason="" prefix="" suffix=""

while IFS= read -r line || [ -n "$line" ]; do
# Check for start marker (no fork — uses bash case)
if [ $in_block -eq 0 ]; then
case "$line" in *simplify-ignore-start*)
in_block=1
buf="$line"
# Extract comment prefix/suffix to preserve language-appropriate syntax
prefix="${line%%simplify-ignore-start*}"
suffix=""
case "$line" in *'*/'*) suffix=" */" ;; *'-->'*) suffix=" -->" ;; esac
reason=$(printf '%s' "$line" | sed -n 's/.*simplify-ignore-start:[[:space:]]*//p' \
| sed 's/[[:space:]]*\*\/.*$//' | sed 's/[[:space:]]*-->.*$//' | sed 's/[[:space:]]*$//')
# Handle single-line block (start + end on same line)
case "$line" in *simplify-ignore-end*)
in_block=0
# Write single-line block immediately and skip to next line
# to avoid the end-marker check below firing again
local h; h=$(block_hash "$buf")
count=$((count + 1))
printf '%s' "$buf" > "$CACHE/${fid}.block.${h}"
[ -n "$reason" ] && printf '%s' "$reason" > "$CACHE/${fid}.reason.${h}"
printf '%s' "$prefix" > "$CACHE/${fid}.prefix.${h}"
printf '%s' "$suffix" > "$CACHE/${fid}.suffix.${h}"
if [ -n "$reason" ]; then
printf '%s\n' "${prefix}BLOCK_${h}: ${reason}${suffix}" >> "$dest"
else
printf '%s\n' "${prefix}BLOCK_${h}${suffix}" >> "$dest"
fi
buf=""; reason=""; prefix=""; suffix=""
continue
;; *)
continue
;;
esac
;; esac
fi
# Accumulate block content
if [ $in_block -eq 1 ]; then
buf="${buf}
${line}"
fi
# Check for end marker
case "$line" in *simplify-ignore-end*)
if [ $in_block -eq 1 ]; then
local h; h=$(block_hash "$buf")
count=$((count + 1))
printf '%s' "$buf" > "$CACHE/${fid}.block.${h}"
[ -n "$reason" ] && printf '%s' "$reason" > "$CACHE/${fid}.reason.${h}"
printf '%s' "$prefix" > "$CACHE/${fid}.prefix.${h}"
printf '%s' "$suffix" > "$CACHE/${fid}.suffix.${h}"
if [ -n "$reason" ]; then
printf '%s\n' "${prefix}BLOCK_${h}: ${reason}${suffix}" >> "$dest"
else
printf '%s\n' "${prefix}BLOCK_${h}${suffix}" >> "$dest"
fi
in_block=0; buf=""; reason=""; prefix=""; suffix=""
continue
fi
;;
esac
[ $in_block -eq 0 ] && printf '%s\n' "$line" >> "$dest"
done < "$src"

# Unclosed block → flush as-is
if [ $in_block -eq 1 ] && [ -n "$buf" ]; then
printf 'Warning: unclosed simplify-ignore-start in %s (block not hidden)\n' "$src" >&2
printf '%s\n' "$buf" >> "$dest"
fi

# Preserve trailing newline status of source
if [ -s "$dest" ] && [ -s "$src" ] && [ -n "$(tail -c 1 "$src")" ]; then
perl -pe 'chomp if eof' "$dest" > "${dest}.nnl" && \
cat "${dest}.nnl" > "$dest" && rm -f "${dest}.nnl"
fi

[ $count -gt 0 ] && return 0 || return 1
}
22 changes: 3 additions & 19 deletions hooks/simplify-ignore-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,9 @@ trap 'rm -rf "$TMPDIR"' EXIT
export CACHE="$TMPDIR/cache"
mkdir -p "$CACHE"

# Extract function definitions we need
hash_cmd() {
if command -v shasum >/dev/null 2>&1; then shasum
elif command -v sha1sum >/dev/null 2>&1; then sha1sum
else printf '%s\n' "error: missing shasum or sha1sum" >&2; exit 1; fi
}
file_id() { printf '%s' "$1" | hash_cmd | cut -c1-16; }
block_hash() { printf '%s' "$1" | hash_cmd | cut -c1-8; }
escape_glob() {
local s="$1"
s="${s//\\/\\\\}"
s="${s//\*/\\*}"
s="${s//\?/\\?}"
s="${s//\[/\\[}"
printf '%s' "$s"
}

# Extract filter_file from the hook script (line 59 "filter_file()" to line 142 closing brace)
eval "$(sed -n '/^filter_file()/,/^}/p' hooks/simplify-ignore.sh)"
# Source shared library
# shellcheck source=hooks/simplify-ignore-lib.sh
. "$(dirname "$0")/simplify-ignore-lib.sh"

assert_eq() {
local label="$1" expected="$2" actual="$3"
Expand Down
Loading