Claude Code

Hand Claude Code the failing tests before its first turn with a SessionStart hook

DEV: claudecode Gabriel Anhaia
Claude Code

Claude Code's SessionStart hook injects a script's plain-text output into the conversation before the first prompt. The author builds a small Python script that runs a fast test subset and prints only the FAILED/ERROR lines within a 45-second budget, so the agent no longer spends turns rediscovering state you already know.

Key points

  • Matchers are startup, resume, clear and fork; on resume the hook runs again so test results do not go stale.
  • A hook that only adds context can print plain text, so hookSpecificOutput.additionalContext JSON is optional.
  • Past 10,000 characters Claude Code writes the output to a file and passes only a 2,000-character preview.
  • On the pytest side, -m 'not slow' picks a fast subset and --tb=no -rfE leaves one FAILED/ERROR line per test.
  • MAX_FAILS=15 and MAX_LINE=200 keep output near 3,000 characters, and pytest exit codes 4 and 5 get their own message.
  • The hook sets timeout to 60 seconds while the script's own BUDGET is 45, so a slow suite reports 'unknown' instead of being killed silently.

Real-world use

  • A team whose CI went red over the weekend wants a fresh agent session to know the failing checkout test names without running the suite.
  • Sharing the hook through a committed .claude/settings.json so every teammate, including worktree-per-task flows, gets the same context.
  • Marking slow integration tests with @pytest.mark.slow so only the few-second unit suite feeds the session's context.
  • Teams that want the agent's first answer to name the red tests without any tool call, cutting turns and token spend.

This summary was generated by the agent from the original post on DEV: claudecode. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message