From 61da2cecd48ab48550144082bed42b301bffbd72 Mon Sep 17 00:00:00 2001 From: Rudolf Tucek Date: Fri, 4 Dec 2026 23:48:52 +0100 Subject: [PATCH 1/3] claude: double down on rules by setting up nudging hooks --- modules/users/rtucek/ai/default.nix | 68 +++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/modules/users/rtucek/ai/default.nix b/modules/users/rtucek/ai/default.nix index 19004c6..01147e6 100644 --- a/modules/users/rtucek/ai/default.nix +++ b/modules/users/rtucek/ai/default.nix @@ -24,6 +24,74 @@ in enable = true; settings = { theme = "dark"; + # Fires on every user turn and some Claude-initiated ones (such as after compaction, which + # should be no-op there). + # + # Reinforces english-mistake-log.md with a fresh per-turn nudge, since that rule has no + # natural trigger in Claude's own response-composition the way citation and/or engagement + # rules do. It was found to be the rule most often silently skipped in long sessions. + # + # `matcher` is not applicable to UserPromptSubmit (no tool to match against) and is omitted. + hooks = { + UserPromptSubmit = [ + { + hooks = [ + { + type = "command"; + command = "echo 'Hook reminder: before replying, check the message you just received for typos/grammar mistakes and log them per english-mistake-log.md - do not skip this because the message is technical or task-focused.'"; + } + ]; + } + ]; + # Mechanical check (not just a nudge) + # + # writing-style.md violations land in text Claude produces, so unlike the mistake-log + # reminder above, this is actually checkable by script. + # Scoped to *.md only for now. + # + # Warns via additionalContext rather than blocking: a hard gate would yield + # false-positive on legitimate quoted/historical prose (e.g. a KB article discussing + # typography, or verbatim third-party text). + PostToolUse = [ + { + matcher = "Write|Edit"; + hooks = [ + { + type = "command"; + command = "$HOME/.claude/hooks/check-writing-style"; + } + ]; + } + ]; + }; + }; + hooks = { + check-writing-style = '' + #!/usr/bin/env bash + set -euo pipefail + + input=$(cat) + file_path=$(jq -r '.tool_input.file_path // empty' <<<"$input") + + [[ "$file_path" == *.md ]] || exit 0 + [[ -f "$file_path" ]] || exit 0 + + # Strip fenced code blocks (```...```), then look for non-ASCII + # punctuation the writing-style rule bans: em dash, en dash, arrow, + # bullet glyph, curly quotes. Inline `code spans` are not excluded + # (would need heavier parsing) - false positives there are possible + # and acceptable for a warn-only check. + hits=$(awk ' + /^```/ { infence = !infence; next } + !infence + ' "$file_path" | grep -nP '[\x{2014}\x{2013}\x{2192}\x{2022}\x{201C}\x{201D}\x{2018}\x{2019}]' || true) + + [[ -n "$hits" ]] || exit 0 + + context=$(printf 'writing-style.md check on %s found non-ASCII punctuation outside code fences (em/en dash, arrow, bullet, or curly quote):\n%s\nIf this is not inside quoted/verbatim text, rewrite with plain keyboard punctuation.' "$file_path" "$hits") + + jq -n --arg ctx "$context" '{hookSpecificOutput: {hookEventName: "PostToolUse", additionalContext: $ctx}}' + ''; }; rules = { engagement = ./engagement.md; From 44561da83dba1c73d35245e0f8bcd3a9c42c8d9f Mon Sep 17 00:00:00 2001 From: Rudolf Tucek Date: Fri, 4 Dec 2026 23:48:52 +0100 Subject: [PATCH 2/3] claude: tweak English mistake log Claude's rule instructed Claude to insert into the mistake sqlite database via a heredoc python script, which in turn demanded very aggressive approvals. Now, the python script has been extracted into a simple file, to which we can very easily grant permission to. --- .../users/rtucek/ai/claude-log-mistake.nix | 4 ++ modules/users/rtucek/ai/claude-log-mistake.py | 50 +++++++++++++++++ modules/users/rtucek/ai/default.nix | 12 ++++- .../users/rtucek/ai/english-mistake-log.md | 53 ++++++------------- 4 files changed, 82 insertions(+), 37 deletions(-) create mode 100644 modules/users/rtucek/ai/claude-log-mistake.nix create mode 100644 modules/users/rtucek/ai/claude-log-mistake.py diff --git a/modules/users/rtucek/ai/claude-log-mistake.nix b/modules/users/rtucek/ai/claude-log-mistake.nix new file mode 100644 index 0000000..4ef4610 --- /dev/null +++ b/modules/users/rtucek/ai/claude-log-mistake.nix @@ -0,0 +1,4 @@ +{ pkgs, ... }: +pkgs.writers.writePython3Bin "claude-log-mistake" { doCheck = false; } ( + builtins.readFile ./claude-log-mistake.py +) diff --git a/modules/users/rtucek/ai/claude-log-mistake.py b/modules/users/rtucek/ai/claude-log-mistake.py new file mode 100644 index 0000000..43af50c --- /dev/null +++ b/modules/users/rtucek/ai/claude-log-mistake.py @@ -0,0 +1,50 @@ +import argparse +import os +import sqlite3 +from pathlib import Path + +XDG_DATA_HOME = Path(os.environ.get("XDG_DATA_HOME", Path.home() / ".local" / "share")) +DB_PATH = XDG_DATA_HOME / "claude" / "spell-mistakes.db" + +CATEGORIES = ("typo", "grammar", "phrasing", "punctuation") + + +def main(): + parser = argparse.ArgumentParser( + description="Log one English-mistake-log entry to the mistakes SQLite DB." + ) + parser.add_argument( + "--ts", + required=True, + help="ISO 8601 timestamp (YYYY-MM-DDTHH:MM:SS), e.g. $(date +%%Y-%%m-%%dT%%H:%%M:%%S)", + ) + parser.add_argument("--category", required=True, choices=CATEGORIES) + parser.add_argument("--quote", required=True) + parser.add_argument("--correction", required=True) + args = parser.parse_args() + + DB_PATH.parent.mkdir(parents=True, exist_ok=True) + conn = sqlite3.connect(DB_PATH) + conn.execute("PRAGMA journal_mode=WAL") + conn.execute("PRAGMA busy_timeout=5000") + conn.execute( + """ + CREATE TABLE IF NOT EXISTS mistakes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + ts TEXT NOT NULL, + category TEXT NOT NULL CHECK (category IN ('typo','grammar','phrasing','punctuation')), + quote TEXT NOT NULL, + correction TEXT NOT NULL + ) + """ + ) + conn.execute( + "INSERT INTO mistakes (ts, category, quote, correction) VALUES (?, ?, ?, ?)", + (args.ts, args.category, args.quote, args.correction), + ) + conn.commit() + conn.close() + + +if __name__ == "__main__": + main() diff --git a/modules/users/rtucek/ai/default.nix b/modules/users/rtucek/ai/default.nix index 01147e6..7d1fda2 100644 --- a/modules/users/rtucek/ai/default.nix +++ b/modules/users/rtucek/ai/default.nix @@ -8,6 +8,7 @@ let cfg = config.rtucek.ai; claudeMistakesReport = import ./claude-mistakes-report.nix { inherit pkgs; }; + claudeLogMistake = import ./claude-log-mistake.nix { inherit pkgs; }; in { options.rtucek.ai = { @@ -18,12 +19,21 @@ in config = lib.mkIf cfg.claude-code.enable { home-manager.users.rtucek = { - home.packages = [ claudeMistakesReport ]; + home.packages = [ + claudeMistakesReport + claudeLogMistake + ]; programs.claude-code = { enable = true; settings = { theme = "dark"; + # Lets english-mistake-log.md invoke claude-log-mistake with varying args + # without re-prompting each time: the allowlist matches on the command + # prefix, not the full argument string. + permissions = { + allow = [ "Bash(claude-log-mistake:*)" ]; + }; # Fires on every user turn and some Claude-initiated ones (such as after compaction, which # should be no-op there). # diff --git a/modules/users/rtucek/ai/english-mistake-log.md b/modules/users/rtucek/ai/english-mistake-log.md index 5c0ed3e..4c008f6 100644 --- a/modules/users/rtucek/ai/english-mistake-log.md +++ b/modules/users/rtucek/ai/english-mistake-log.md @@ -7,16 +7,17 @@ from. Track mistakes so they can review and improve over time. Storage: SQLite database at `$XDG_DATA_HOME/claude/spell-mistakes.db` (falls back to `~/.local/share/claude/spell-mistakes.db` if -`XDG_DATA_HOME` is unset), table `mistakes`: - -- `id` INTEGER PRIMARY KEY AUTOINCREMENT -- `ts` TEXT, ISO 8601 (`YYYY-MM-DDTHH:MM:SS`), use the real current time - (e.g. via `date +%Y-%m-%dT%H:%M:%S`), don't guess it -- `category` TEXT, one of `typo`, `grammar`, `phrasing`, `punctuation` -- `quote` TEXT, the sentence or fragment as written, with surrounding - context (use `[...]` for truncation) so the mistake is identifiable later -- `correction` TEXT, the fix, with the corrected word/phrase in - `**bold**` and a short "(not X)" note where that helps +`XDG_DATA_HOME` is unset), table `mistakes`, written via the +`claude-log-mistake` CLI (not the `sqlite3` CLI directly): + +- `--ts`, ISO 8601 (`YYYY-MM-DDTHH:MM:SS`). Use the real current time, not + a guess: pass `--ts "$(date +%Y-%m-%dT%H:%M:%S)"` for the common case, or + an explicit timestamp when backdating a specific entry. +- `--category`, one of `typo`, `grammar`, `phrasing`, `punctuation` +- `--quote`, the sentence or fragment as written, with surrounding context + (use `[...]` for truncation) so the mistake is identifiable later +- `--correction`, the fix, with the corrected word/phrase in `**bold**` + and a short "(not X)" note where that helps Classify each mistake found in the user's message: @@ -42,34 +43,14 @@ Never flag anything inside code blocks, shell commands, file paths, identifiers, or proper nouns. The goal is catching real mistakes, not achieving perfect formal English, skip borderline style nitpicks. -Insert with Python, not the `sqlite3` CLI, so quoting/escaping is never an -issue: +Insert with the `claude-log-mistake` CLI, e.g.: ``` -python3 - <<'PY' -import os, sqlite3, pathlib -data_home = pathlib.Path(os.environ.get("XDG_DATA_HOME", pathlib.Path.home() / ".local" / "share")) -db = data_home / "claude" / "spell-mistakes.db" -db.parent.mkdir(parents=True, exist_ok=True) -conn = sqlite3.connect(db) -conn.execute("PRAGMA journal_mode=WAL") -conn.execute("PRAGMA busy_timeout=5000") -conn.execute(""" - CREATE TABLE IF NOT EXISTS mistakes ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - ts TEXT NOT NULL, - category TEXT NOT NULL CHECK (category IN ('typo','grammar','phrasing','punctuation')), - quote TEXT NOT NULL, - correction TEXT NOT NULL - ) -""") -conn.execute( - "INSERT INTO mistakes (ts, category, quote, correction) VALUES (?, ?, ?, ?)", - ("", "", "", ""), -) -conn.commit() -conn.close() -PY +claude-log-mistake \ + --ts "$(date +%Y-%m-%dT%H:%M:%S)" \ + --category typo \ + --quote "..." \ + --correction "**fixed** (not original)" ``` The user prunes old rows themselves with `sqlite3`/`litecli`; never run From 7ba712db08a8a20627ff6b52f45a1a86fa8f4065 Mon Sep 17 00:00:00 2001 From: Rudolf Tucek Date: Fri, 4 Dec 2026 23:48:52 +0100 Subject: [PATCH 3/3] claude: fix formatting and comments --- modules/users/rtucek/ai/default.nix | 50 +++++++++++++++++------------ 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/modules/users/rtucek/ai/default.nix b/modules/users/rtucek/ai/default.nix index 7d1fda2..5680438 100644 --- a/modules/users/rtucek/ai/default.nix +++ b/modules/users/rtucek/ai/default.nix @@ -28,45 +28,51 @@ in enable = true; settings = { theme = "dark"; - # Lets english-mistake-log.md invoke claude-log-mistake with varying args - # without re-prompting each time: the allowlist matches on the command - # prefix, not the full argument string. + permissions = { - allow = [ "Bash(claude-log-mistake:*)" ]; + allow = [ + # Let english-mistake-log.md invoke claude-log-mistake with varying args without + # re-prompting each time. + "Bash(claude-log-mistake:*)" + ]; }; - # Fires on every user turn and some Claude-initiated ones (such as after compaction, which - # should be no-op there). - # - # Reinforces english-mistake-log.md with a fresh per-turn nudge, since that rule has no - # natural trigger in Claude's own response-composition the way citation and/or engagement - # rules do. It was found to be the rule most often silently skipped in long sessions. - # - # `matcher` is not applicable to UserPromptSubmit (no tool to match against) and is omitted. + hooks = { UserPromptSubmit = [ { hooks = [ { + # Fires on every user turn and some Claude-initiated ones (such as after + # compaction, which should be no-op there). + # + # Reinforces english-mistake-log.md with a fresh per-turn nudge, since that rule + # has no natural trigger in Claude's own response-composition the way citation + # and/or engagement rules do. It was found to be the rule most often silently + # skipped in long sessions. + # + # `matcher` is not applicable to UserPromptSubmit (no tool to match against) and + # is omitted. type = "command"; command = "echo 'Hook reminder: before replying, check the message you just received for typos/grammar mistakes and log them per english-mistake-log.md - do not skip this because the message is technical or task-focused.'"; } ]; } ]; - # Mechanical check (not just a nudge) - # - # writing-style.md violations land in text Claude produces, so unlike the mistake-log - # reminder above, this is actually checkable by script. - # Scoped to *.md only for now. - # - # Warns via additionalContext rather than blocking: a hard gate would yield - # false-positive on legitimate quoted/historical prose (e.g. a KB article discussing - # typography, or verbatim third-party text). + PostToolUse = [ { matcher = "Write|Edit"; hooks = [ { + # Mechanical check (not just a nudge) + # + # writing-style.md violations land in text Claude produces, so unlike the + # mistake-log reminder above, this is actually checkable by script. Scoped to + # *.md only for now. + # + # Warns via additionalContext rather than blocking: a hard gate would yield + # false-positive on legitimate quoted/historical prose (e.g. a KB article + # discussing typography, or verbatim third-party text). type = "command"; command = "$HOME/.claude/hooks/check-writing-style"; } @@ -75,6 +81,7 @@ in ]; }; }; + hooks = { check-writing-style = '' #!/usr/bin/env bash @@ -103,6 +110,7 @@ in jq -n --arg ctx "$context" '{hookSpecificOutput: {hookEventName: "PostToolUse", additionalContext: $ctx}}' ''; }; + rules = { engagement = ./engagement.md; source-citation = ./source-citation.md;