Skip to main content

mati_core/hooks/
post_edit.rs

1/// post-edit.sh — edit activity tracking (M-09-D) + doc comment capture (2.3).
2///
3/// PostToolUse — fires after Edit/Write/MultiEdit. Tracks file modifications
4/// and captures canonical doc comments Claude authors as file purpose
5/// (confidence 0.65, no API calls needed).
6pub const SCRIPT: &str = r#"#!/usr/bin/env bash
7# mati post-edit hook — edit activity tracking + doc comment capture
8set -euo pipefail
9HOOKS_DIR="$(cd "$(dirname "$0")" && pwd)" && export PATH="$HOOKS_DIR:$PATH"
10mkdir -p "${HOME}/.mati" 2>/dev/null || true
11
12INPUT=$(cat)
13
14# Guard: jq required
15if ! command -v jq &>/dev/null; then
16  echo "[mati] missing jq — enforcement bypassed" >&2
17  { echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) FAIL_OPEN hook=$(basename "$0") reason=missing_deps" >> "${HOME}/.mati/fail_open.log"; } 2>/dev/null || true
18  exit 0
19fi
20
21FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // ""')
22[ -z "$FILE_PATH" ] && exit 0
23
24# Convert absolute path to repo-relative path.
25# Claude Code always passes absolute paths; mati store keys use relative paths.
26REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo "")
27if [ -n "$REPO_ROOT" ]; then
28  FILE_PATH="${FILE_PATH#$REPO_ROOT/}"
29fi
30
31# 2.3: Capture canonical doc comment from new content.
32# Write: tool_input.content  |  Edit: tool_input.new_string
33# Only the first 15 lines — doc comments live at the top of the file.
34CONTENT_HEAD=$(echo "$INPUT" | jq -r '(.tool_input.content // .tool_input.new_string // "") | split("\n")[:15] | join("\n")')
35if [ -n "$CONTENT_HEAD" ]; then
36    printf '%s' "$CONTENT_HEAD" | mati doc-capture "$FILE_PATH" &>/dev/null &
37fi
38
39mati edit-hook "$FILE_PATH" &>/dev/null &
40"#;