Security and performance fixes addressing a comprehensive review: - Server-issued HMAC-signed session cookies; client-supplied session_id ignored. Prevents session hijacking via body substitution. - Sliding-window rate limiter per IP and per session. - SessionStore with LRU eviction, idle TTL, per-session threading locks, and a hard turn cap. Bounds memory and serializes concurrent turns for the same session so FastAPI's threadpool cannot corrupt history. - Tool-use loop capped at settings.max_tool_use_iterations; Anthropic client gets an explicit timeout. No more infinite-loop credit burn. - Every tool argument is regex-validated, length-capped, and control-character-stripped. asserts replaced with ValueError so -O cannot silently disable the checks. - PII-safe warning logs: session IDs and reply bodies are hashed, never logged in clear. - hmac.compare_digest for email comparison (constant-time). - Strict Content-Security-Policy plus X-Content-Type-Options, X-Frame-Options, Referrer-Policy, Permissions-Policy via middleware. - Explicit handlers for anthropic.RateLimitError, APIConnectionError, APIStatusError, ValueError; static dir resolved from __file__. - Prompt cache breakpoints on the last tool schema and the last message so per-turn input cost scales linearly, not quadratically. - TypedDict handler argument shapes; direct block.name/block.id access. - functools.lru_cache on _get_client. - Anchored word-boundary regexes for out-of-scope detection to kill false positives on phrases like "I'd recommend contacting...". Literate program: - Bookly.lit.md is now the single source of truth for the five core Python files. Tangles byte-for-byte; verified via tangle.ts --verify. - Prose walkthrough, three mermaid diagrams, narrative per module. - Woven to static/architecture.html with the app's palette (background #f5f3ee) via scripts/architecture-header.html. - New GET /architecture route serves the HTML with a relaxed CSP that allows pandoc's inline styles. Available at bookly.codyborders.com/architecture. - scripts/rebuild_architecture_html.sh regenerates the HTML after edits. - code_reviews/2026-04-15-1433-code-review.md captures the review that drove these changes. All 37 tests pass.
24 lines
733 B
Bash
Executable File
24 lines
733 B
Bash
Executable File
#!/usr/bin/env bash
|
|
# Regenerate static/architecture.html from Bookly.lit.md.
|
|
#
|
|
# The .lit.md is the single source of truth: edit it, then run this script
|
|
# to rebuild the HTML that /architecture serves. The post-edit reverse-sync
|
|
# hook keeps the .lit.md in step with direct edits to the Python files, but
|
|
# it does not re-run pandoc -- this script does.
|
|
set -euo pipefail
|
|
|
|
cd "$(dirname "$0")/.."
|
|
|
|
pandoc Bookly.lit.md \
|
|
-o static/architecture.html \
|
|
--standalone \
|
|
--embed-resources \
|
|
--filter mermaid-filter \
|
|
--toc \
|
|
--toc-depth=3 \
|
|
--highlight-style=tango \
|
|
-H scripts/architecture-header.html \
|
|
--metadata pagetitle="Bookly"
|
|
|
|
echo "wrote static/architecture.html ($(wc -c < static/architecture.html) bytes)"
|