Skip to content

Add hook system for devbox run commands (#2862)#2863

Open
levonk wants to merge 4 commits into
jetify-com:mainfrom
levonk:feature/hooks-system
Open

Add hook system for devbox run commands (#2862)#2863
levonk wants to merge 4 commits into
jetify-com:mainfrom
levonk:feature/hooks-system

Conversation

@levonk

@levonk levonk commented Jun 11, 2026

Copy link
Copy Markdown

Summary

Implements a comprehensive hook system for intercepting and modifying command execution in devbox run. This enables use cases like policy enforcement, instrumentation, command wrapping, and output processing. Closes #2862

Features

  • Pre-run hooks with capability gates (can_block, can_modify_args, can_modify_env, can_modify_stdin)
  • Command wrapper for simple string wrapping (e.g., "rtk exec --")
  • Post-run hooks with capability gates (can_modify_exit, can_modify_stdout, can_modify_stderr)
  • Array-based hooks - multiple hooks of each type can be configured
  • Security-first design - all capabilities default to false
  • JSON-based hook output for structured communication

Changes

  • internal/devbox: Add hook execution logic in runhooks.go and integrate into RunScript()
  • internal/devconfig/configfile: Add RunHook struct, validation, and accessor methods
  • docs/hooks.md: Comprehensive documentation
  • examples/hooks/: Working example configuration

Design Principles

  • Explicit capability gates for security and clarity
  • Symmetric design - pre-run and post-run hooks follow similar patterns
  • Default-deny - dangerous capabilities are disabled by default
  • Shell namespace - hooks live under shell configuration, following Devbox conventions

How was it tested?

  • Added comprehensive test suite with 20+ test cases covering all hook permutations
  • Tests verify capability enforcement across all permission gates
  • Tests confirm hooks receive proper context via environment variables
  • Tests cover JSON output parsing (valid, invalid, non-JSON, partial)
  • All existing tests pass (go test ./...)
  • Hook configuration validation tests ensure proper schema usage

Test Coverage

  • No hooks, single hook, multiple hooks for pre_run and post_run
  • Environment modifications with and without permissions
  • Command blocking with capability enforcement
  • Argument modifications with permission checks
  • Command wrapper functionality
  • Exit code modifications for post_run hooks
  • Stdout/stderr modifications with permission checks
  • Hook context environment variables (command, args, env, dir, exit code, output)
  • JSON output parsing and error handling
  • Capability enforcement across all permission gates
  • Hook execution failures and edge cases

Community Contribution License

All community contributions in this pull request are licensed to the project
maintainers under the terms of the
Apache 2 License.

By creating this pull request, I represent that I have the right to license the
contributions to the project maintainers under the Apache 2 License as stated in
the
Community Contribution License.

Levon Karayan and others added 4 commits June 11, 2026 01:47
Implements a comprehensive hook system for intercepting and modifying
command execution in devbox run. This enables use cases like policy
enforcement, instrumentation, command wrapping, and output processing.

Features:
- Pre-run hooks with capability gates (can_block, can_modify_args,
  can_modify_env, can_modify_stdin)
- Command wrapper for simple string wrapping (e.g., "rtk exec --")
- Post-run hooks with capability gates (can_modify_exit,
  can_modify_stdout, can_modify_stderr)
- Array-based hooks - multiple hooks of each type can be configured
- Security-first design - all capabilities default to false
- JSON-based hook output for structured communication

Changes:
- internal/devbox: Add hook execution logic in runhooks.go and
  integrate into RunScript()
- internal/devconfig/configfile: Add RunHook struct, validation,
  and accessor methods
- docs/hooks.md: Comprehensive documentation
- examples/hooks/: Working example configuration

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Adds extensive integration tests covering all hook permutations:
- No hooks, single hook, multiple hooks for pre_run and post_run
- Environment modifications with and without permissions
- Command blocking with capability enforcement
- Argument modifications with permission checks
- Command wrapper functionality
- Exit code modifications for post_run hooks
- Stdout/stderr modifications with permission checks
- Hook context environment variables (command, args, env, dir, exit code, output)
- JSON output parsing (valid, invalid, non-JSON, partial)
- Capability enforcement across all permission gates
- Hook execution failures and edge cases

Tests verify that hooks only modify execution when explicitly
granted permission through capability gates, ensuring security
by default-deny behavior.

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Updates existing documentation to reference the new hooks system:
- Add hooks example to examples/README.md
- Add hooks reference to README.md advanced features section
- Create docs/index.md as documentation hub
- Update CONTRIBUTING.md to mention documentation updates

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Implements streaming functionality to eliminate OOM risk with large
outputs and adds read access control for structured data.

- Added streaming pipeline using io.Pipe() to connect hook stages
- Read capability gates (can_read_stdin/stdout/stderr) default to false
- Hooks without read permissions receive closed reader instead of stream
- 1MB size limit for JSON parsing to prevent OOM
- Updated documentation with streaming support section

Streaming automatically activates when:
- Pre-run hooks have can_modify_stdin: true
- Post-run hooks have can_modify_stdout/stderr: true
- Command wrapper is configured

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

devbox run hooks for pre, post, and command wrapping

1 participant