Skip to content

Conversation

@were
Copy link
Contributor

@were were commented Jan 23, 2026

Summary

Simplified repeated logic across .claude-plugin/hooks/*.py by centralizing shared utilities in session_utils.py. This refactoring eliminates code duplication without changing hook behavior.

Changes

  • Added .claude-plugin/lib/session_utils.py:13-52 with two new helpers:
    • is_handsoff_enabled() - centralized HANDSOFF_MODE environment variable parsing
    • write_issue_index() - centralized issue index file creation for session-to-issue mapping
  • Modified .claude-plugin/hooks/user-prompt-submit.py:23-27,75-78 to use new helpers instead of inline code
  • Modified .claude-plugin/hooks/stop.py:13,17-20 to use is_handsoff_enabled() instead of inline check
  • Modified .claude-plugin/hooks/post-bash-issue-create.py:29,117-119 to use write_issue_index() instead of inline creation
  • Modified .claude-plugin/hooks/pre-tool-use.py:22-27 to remove redundant sys.path setup inside main()
  • Updated .claude-plugin/lib/session_utils.md with complete interface documentation for new helpers
  • Updated .claude-plugin/lib/README.md with expanded usage examples
  • Updated docs/architecture/handsoff.md to document centralized handsoff parsing

Testing

  • Added tests/cli/test-session-utils.sh with 15 test cases covering:
    • is_handsoff_enabled() with various HANDSOFF_MODE values (unset, 1, 0, false, FALSE, off, disable, enabled)
    • write_issue_index() directory creation, JSON payload format, path return value
    • String vs int issue numbers, default session_dir behavior
  • Ran existing hook tests to verify no regressions:
    • test-handsoff-session-path.sh - all tests pass
    • test-handsoff-session-pr-number.sh - all tests pass
    • test-hook-permission-matching.sh - all 40 tests pass

Related Issue

Closes #593

@were were added the agentize:pr PR created by agentize label Jan 23, 2026
were and others added 3 commits January 23, 2026 15:17
Add documentation for new is_handsoff_enabled() and write_issue_index()
helper functions that will centralize repeated logic across Python hooks.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…sed)

Add test file for is_handsoff_enabled() and write_issue_index() helper
functions before implementation. Tests verify:
- HANDSOFF_MODE environment variable parsing (8 test cases)
- Issue index file creation and JSON content (3 test cases)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add centralized helpers to session_utils.py:
- is_handsoff_enabled(): Check HANDSOFF_MODE env var (replaces 4-line pattern)
- write_issue_index(): Create issue index files (replaces 5-line pattern)

Apply helpers across hooks:
- user-prompt-submit.py: Use is_handsoff_enabled() and write_issue_index()
- stop.py: Use is_handsoff_enabled()
- post-bash-issue-create.py: Use write_issue_index()
- pre-tool-use.py: Remove redundant sys.path setup inside main()

Behavior unchanged - all existing tests pass.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
were and others added 2 commits January 23, 2026 18:35
Existing tests (test-handsoff-session-path.sh, test-hook-permission-matching.sh)
already validate the refactored behavior. Unit tests for extracted helpers
are redundant for a pure refactor.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…ed tests

- Test 33: Add missing fname argument to _ask_supervisor_for_guidance() call
- Tests 40-41: Update to reflect removal of legacy boolean backward-compat
  (commit 584630d removed HANDSOFF_SUPERVISOR=0/1 mappings)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@were were merged commit aae18ea into main Jan 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agentize:pr PR created by agentize

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[plan][refactor]: Simplify Python hooks by extracting shared utilities

2 participants