Skip to main content

mati_core/hooks/
codex_session_start.rs

1/// Codex SessionStart hook — daemon health check + compact sentinel.
2///
3/// Auto-starts the daemon if needed, logs a bootstrap event for analytics,
4/// and emits a ~5-token sentinel. All workflow instructions live in SKILL.md
5/// (loaded once by the platform) — the hook does not repeat them.
6pub const SCRIPT: &str = r#"#!/usr/bin/env bash
7set -euo pipefail
8HOOKS_DIR="$(cd "$(dirname "$0")" && pwd)" && export PATH="$HOOKS_DIR:$PATH"
9mkdir -p "${HOME}/.mati" 2>/dev/null || true
10
11if ! mati ping --daemon-only >/dev/null 2>&1; then
12  mati daemon start </dev/null >/dev/null 2>&1 &
13  READY=false
14  for _attempt in 1 2 3; do
15    sleep 0.15
16    if mati ping --daemon-only >/dev/null 2>&1; then READY=true; break; fi
17  done
18  if [ "$READY" = "false" ]; then
19    echo "[mati] WARNING: daemon bootstrap failed — PreToolUse hooks will retry independently." >&2
20    { echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) AUTO_START hook=session-start result=failed" >> "${HOME}/.mati/fail_open.log"; } 2>/dev/null || true
21    exit 0
22  fi
23  { echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) AUTO_START hook=session-start result=ok" >> "${HOME}/.mati/fail_open.log"; } 2>/dev/null || true
24fi
25
26mati log-bootstrap "__codex_session__" >/dev/null 2>&1 || true
27
28printf '{"hookSpecificOutput":{"hookEventName":"SessionStart","additionalContext":"[mati] active"}}\n'
29"#;