Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/codegen/cli/commands/claude/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
from codegen.cli.api.endpoints import API_ENDPOINT
from codegen.cli.auth.token_manager import get_current_token
from codegen.cli.commands.claude.claude_log_watcher import ClaudeLogWatcherManager
from codegen.cli.commands.claude.claude_session_api import update_claude_session_status, generate_session_id
from codegen.cli.commands.claude.claude_session_api import (
update_claude_session_status,
generate_session_id,
create_claude_session,
)
from codegen.cli.commands.claude.config.mcp_setup import add_codegen_mcp_server, cleanup_codegen_mcp_server
from codegen.cli.commands.claude.hooks import cleanup_claude_hook, ensure_claude_hook, get_codegen_url
from codegen.cli.commands.claude.quiet_console import console
Expand Down Expand Up @@ -86,6 +90,16 @@ def _run_claude_interactive(resolved_org_id: int, no_mcp: bool | None) -> None:
os.environ["CODEGEN_CLAUDE_SESSION_ID"] = session_id
os.environ["CODEGEN_CLAUDE_ORG_ID"] = str(resolved_org_id)

# Proactively create the backend session as a fallback in case hooks fail
try:
agent_run_id = create_claude_session(session_id, resolved_org_id)
Copy link
Contributor

Choose a reason for hiding this comment

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

Logic error: Potential duplicate session creation leading to multiple agent runs

The proactive create_claude_session call runs before hooks are installed, but the SessionStart hook also creates the session. This can cause duplicate sessions or noisy errors if the backend isn’t strictly idempotent on session_id.

Suggested change
agent_run_id = create_claude_session(session_id, resolved_org_id)
# Set up Claude hook for session tracking first
hook_ready = ensure_claude_hook()
if not hook_ready:
console.print("⚠️ Failed to set up session tracking hook", style="yellow")
# Fallback: proactively create the backend session only if hooks failed
try:
agent_run_id = create_claude_session(session_id, resolved_org_id)
if agent_run_id:
console.print("✅ Backend session created", style="green")
else:
console.print("⚠️ Could not create backend session at startup (will rely on hooks)", style="yellow")
except Exception as e:
console.print(f"⚠️ Session creation error at startup: {e}", style="yellow")

This preserves your fallback while avoiding duplicate creation when hooks succeed.

if agent_run_id:
console.print("✅ Backend session created", style="green")
else:
console.print("⚠️ Could not create backend session at startup (will rely on hooks)", style="yellow")
except Exception as e:
console.print(f"⚠️ Session creation error at startup: {e}", style="yellow")

# Set up Claude hook for session tracking
if not ensure_claude_hook():
console.print("⚠️ Failed to set up session tracking hook", style="yellow")
Expand Down
Loading