session-start-codex 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env bash
  2. # Codex SessionStart hook for the superpowers plugin.
  3. #
  4. # Codex re-fires SessionStart with source:"compact" after every context
  5. # compaction (verified on codex-cli 0.145.0). Compaction replaces the live
  6. # context with a summary, which sheds the using-superpowers bootstrap and any
  7. # active skill's instructions — the measured cause of mid-session dispatch
  8. # drift in long multi-agent runs. This hook re-injects the bootstrap at
  9. # exactly that moment, restoring the same re-injection Claude Code performs
  10. # via its "startup|clear|compact" SessionStart matcher.
  11. #
  12. # On source:"startup" it emits nothing: the native Codex plugin path owns
  13. # session-start injection, and duplicating it here would recreate the
  14. # redundancy that led to the original session-start-codex hook's removal.
  15. #
  16. # Codex injects raw hook stdout into the model's context (verified with
  17. # sentinel probes), so output is plain text — not the JSON envelopes other
  18. # harnesses require of hooks/session-start.
  19. #
  20. # A hook failure must never break a session: every path fails open to empty
  21. # output and exit 0.
  22. set -u
  23. payload="$(cat 2>/dev/null || true)"
  24. # Act only on post-compaction re-fires. Tolerate arbitrary whitespace around
  25. # the JSON colon; anything unparseable falls through to a silent no-op.
  26. if ! printf '%s' "$payload" | grep -qE '"source"[[:space:]]*:[[:space:]]*"compact"'; then
  27. exit 0
  28. fi
  29. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  30. PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
  31. using_superpowers_content="$(cat "${PLUGIN_ROOT}/skills/using-superpowers/SKILL.md" 2>/dev/null)" || using_superpowers_content=""
  32. if [ -z "$using_superpowers_content" ]; then
  33. exit 0
  34. fi
  35. # printf instead of heredocs throughout: heredocs hang on bash 5.3+.
  36. # See: https://github.com/obra/superpowers/issues/571
  37. printf '%s\n' "<EXTREMELY_IMPORTANT>"
  38. printf '%s\n\n' "You have superpowers."
  39. printf '%s\n\n' "**Below is the full content of your 'superpowers:using-superpowers' skill - your introduction to using skills. For all other skills, use the 'Skill' tool:**"
  40. printf '%s\n' "$using_superpowers_content"
  41. printf '%s\n\n' "</EXTREMELY_IMPORTANT>"
  42. printf '%s\n' "<CONTEXT_RESTORED>"
  43. printf '%s\n' "Your context was just summarized (compacted). The summary preserves your progress but not your working instructions — the files are authoritative."
  44. printf '%s\n' ""
  45. printf '%s\n' "Before your next tool call:"
  46. printf '%s\n' "- Re-read the SKILL.md of any skill you are mid-way through executing. If you are executing subagent-driven-development, re-read skills/subagent-driven-development/SKILL.md."
  47. printf '%s\n' "- On Codex, also re-read skills/using-superpowers/references/codex-tools.md and follow its dispatch rules on every spawn_agent call."
  48. printf '%s\n' "</CONTEXT_RESTORED>"
  49. exit 0