Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions modules/users/rtucek/ai/claude-log-mistake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ pkgs, ... }:
pkgs.writers.writePython3Bin "claude-log-mistake" { doCheck = false; } (
builtins.readFile ./claude-log-mistake.py
)
50 changes: 50 additions & 0 deletions modules/users/rtucek/ai/claude-log-mistake.py
Original file line number Diff line number Diff line change
@@ -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()
88 changes: 87 additions & 1 deletion modules/users/rtucek/ai/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -18,13 +19,98 @@ 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";

permissions = {
allow = [
# Let english-mistake-log.md invoke claude-log-mistake with varying args without
# re-prompting each time.
"Bash(claude-log-mistake:*)"
];
};

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.'";
}
];
}
];

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";
}
];
}
];
};
};

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;
source-citation = ./source-citation.md;
Expand Down
53 changes: 17 additions & 36 deletions modules/users/rtucek/ai/english-mistake-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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 (?, ?, ?, ?)",
("<iso-ts>", "<category>", "<quote>", "<correction>"),
)
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
Expand Down