Skip to content

Conversation

@pirhoo
Copy link
Owner

@pirhoo pirhoo commented Nov 29, 2025

Summary

Adds lifecycle hooks system that allows executing custom logic before and after each step in the workflow. This provides powerful extension points for logging, performance monitoring, validation, retry logic, and more.

Features

  • beforeStep(hook) - Register hooks that execute before each step
  • afterStep(hook) - Register hooks that execute after each step with execution results
  • Hooks receive step, context, and result information
  • Hooks are inherited by child steps
  • Execution time tracking and error capture in results
  • clearHooks() method to remove all registered hooks

Use Cases

  1. Logging - Track step execution for debugging
  2. Performance Monitoring - Measure execution time for each step
  3. State Validation - Verify output meets expectations
  4. Retry Logic - Automatically retry failed steps
  5. Timeout Protection - Set time budgets for steps
  6. Custom Metrics - Collect and aggregate execution data
  7. Error Handling - Custom error recovery logic

Implementation

  • Added BeforeStepHook and AfterStepHook types in lib/types/hooks.ts
  • Added StepExecutionResult type with success, error, executionTime, and output fields
  • Modified StepBuilder.applyAll() to execute hooks around each step
  • Hooks execute even when steps fail, then errors are re-thrown
  • Hook inheritance through step hierarchy

Tests

  • 29 comprehensive tests covering all hook functionality
  • Tests for hook execution order, inheritance, async hooks, error handling
  • Tests for all documented use cases
  • All tests passing locally and in CI

Documentation

  • Comprehensive guide in docs/guide/hooks.md
  • API reference with examples
  • Use case documentation with runnable examples
  • 7 example files in examples/hooks/
  • Updated README with hooks section

Breaking Changes

None - this is a backwards-compatible addition.

Checklist

  • Implementation complete
  • Tests written and passing (29/29)
  • Documentation added
  • Examples provided
  • CI passing
  • No breaking changes

@pirhoo pirhoo merged commit 7ae4b36 into main Dec 4, 2025
3 of 4 checks passed
@pirhoo pirhoo deleted the feat/step-hooks branch December 4, 2025 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants