Skip to content

Conversation

@rushilpatel0
Copy link
Contributor

Motivation

Content

Testing

Please check the following before marking your PR as ready for review

  • I have added tests for my changes
  • I have updated the documentation or added new documentation as needed

@rushilpatel0 rushilpatel0 requested review from a team and codegen-team as code owners August 19, 2025 18:18
@rushilpatel0 rushilpatel0 merged commit 68f85a4 into develop Aug 19, 2025
7 of 9 checks passed
@rushilpatel0 rushilpatel0 deleted the rpatel/cleanup branch August 19, 2025 18:19
@github-actions
Copy link
Contributor

🎉 This PR is included in version 0.56.7 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

import sys
from pathlib import Path

from codegen.cli.commands.claude.claude_session_api import create_claude_session
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRITICAL: Runtime crash risk due to import order regression

sys.path is modified after attempting to import from codegen. When this hook runs outside the package environment (common for CLI hooks), the early imports will fail with ImportError and the script will exit before reaching main().

Suggested change
from codegen.cli.commands.claude.claude_session_api import create_claude_session
#!/usr/bin/env python3
"""Claude Code session hook script for API integration.
This script is called by Claude Code on SessionStart to:
1. Create a session in the backend API
2. Write session data to local file for tracking
"""
import json
import os
import sys
from pathlib import Path
# Add the codegen CLI to the path so we can import from it
script_dir = Path(__file__).parent
codegen_cli_dir = script_dir.parent.parent.parent
sys.path.insert(0, str(codegen_cli_dir))
try:
from codegen.cli.commands.claude.claude_session_api import create_claude_session
from codegen.cli.utils.org import resolve_org_id
except ImportError:
# Fallback if imports fail - just write basic session data
create_claude_session = None
resolve_org_id = None

This restores safe behavior: ensure the path is set before imports and retain the previous fallback so the hook doesn’t crash if imports aren’t available.

# Build the shell command that will create session via API and write session data
start_hook_script_path = Path(__file__).parent / "config" / "claude_session_hook.py"
start_hook_command = f"mkdir -p {CODEGEN_DIR} && python3 {start_hook_script_path} > {SESSION_FILE}"
start_hook_command = f"python3 {start_hook_script_path} > {SESSION_FILE}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMPORTANT: Regression can break session file creation at runtime

start_hook_command no longer ensures the target directory exists. If ~/.codegen is removed or missing when the hook runs (e.g., after cleanup or on a fresh environment), shell redirection to claude-session.json will fail and the hook won’t write session data. This breaks downstream hooks and session tracking.

Suggested change
start_hook_command = f"python3 {start_hook_script_path} > {SESSION_FILE}"
# Build the shell command that will create session via API and write session data
start_hook_script_path = Path(__file__).parent / "config" / "claude_session_hook.py"
start_hook_command = f"mkdir -p {CODEGEN_DIR} && python3 {start_hook_script_path} > {SESSION_FILE}"

Restoring mkdir -p makes the hook robust against missing directories at execution time.

@codegen-sh
Copy link
Contributor

codegen-sh bot commented Aug 19, 2025

Found 1 critical and 1 important issues. Please review my inline comments above.

🔍 View my analysis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants