Skip to content

Commit 8a03084

Browse files
committed
fix: remove start hook, and just persist session before claude code
1 parent 1678fa8 commit 8a03084

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

src/codegen/cli/commands/claude/hooks.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ def ensure_claude_hook() -> bool:
3636
CLAUDE_CONFIG_DIR.mkdir(exist_ok=True)
3737

3838
# Build the shell command that will create session via API and write session data
39-
start_hook_script_path = Path(__file__).parent / "config" / "claude_session_hook.py"
40-
start_hook_command = f"python3 {start_hook_script_path} > {SESSION_FILE}"
4139

4240
# Build the stop hook command to mark session COMPLETE
4341
stop_hook_script_path = Path(__file__).parent / "config" / "claude_session_stop_hook.py"
@@ -63,28 +61,23 @@ def ensure_claude_hook() -> bool:
6361
# Ensure proper structure exists
6462
if "hooks" not in hooks_config:
6563
hooks_config["hooks"] = {}
66-
if "SessionStart" not in hooks_config["hooks"]:
67-
hooks_config["hooks"]["SessionStart"] = []
6864
if "Stop" not in hooks_config["hooks"]:
6965
hooks_config["hooks"]["Stop"] = []
7066
if "UserPromptSubmit" not in hooks_config["hooks"]:
7167
hooks_config["hooks"]["UserPromptSubmit"] = []
7268

7369
# Get existing hooks
74-
session_start_hooks = hooks_config["hooks"]["SessionStart"]
7570
stop_hooks = hooks_config["hooks"]["Stop"]
7671
active_hooks = hooks_config["hooks"]["UserPromptSubmit"]
7772

7873
# Check if we're replacing existing hooks
79-
replaced_existing = (len(session_start_hooks) > 0) or (len(stop_hooks) > 0) or (len(active_hooks) > 0)
74+
replaced_existing = (len(stop_hooks) > 0) or (len(active_hooks) > 0)
8075

8176
# Create the new hook structures (following Claude's format)
82-
new_start_hook_group = {"hooks": [{"type": "command", "command": start_hook_command}]}
8377
new_stop_hook_group = {"hooks": [{"type": "command", "command": stop_hook_command}]}
8478
new_active_hook_group = {"hooks": [{"type": "command", "command": active_hook_command}]}
8579

8680
# Replace all existing hooks with our single hook per event
87-
hooks_config["hooks"]["SessionStart"] = [new_start_hook_group]
8881
hooks_config["hooks"]["Stop"] = [new_stop_hook_group]
8982
hooks_config["hooks"]["UserPromptSubmit"] = [new_active_hook_group]
9083

@@ -97,7 +90,6 @@ def ensure_claude_hook() -> bool:
9790
console.print("✅ Replaced existing Claude hooks (SessionStart, Stop)", style="green")
9891
else:
9992
console.print("✅ Registered new Claude hooks (SessionStart, Stop)", style="green")
100-
console.print(f" Start hook: {start_hook_command}", style="dim")
10193
console.print(f" Stop hook: {stop_hook_command}", style="dim")
10294
console.print(f" Active hook:{' ' if len('Active hook:') < 1 else ''} {active_hook_command}", style="dim")
10395

@@ -106,13 +98,6 @@ def ensure_claude_hook() -> bool:
10698
with open(HOOKS_CONFIG_FILE) as f:
10799
verify_config = json.load(f)
108100

109-
# Check that our hooks are in the config
110-
found_start_hook = False
111-
for hook_group in verify_config.get("hooks", {}).get("SessionStart", []):
112-
for hook in hook_group.get("hooks", []):
113-
if SESSION_FILE.name in hook.get("command", ""):
114-
found_start_hook = True
115-
break
116101
found_stop_hook = False
117102
for hook_group in verify_config.get("hooks", {}).get("Stop", []):
118103
for hook in hook_group.get("hooks", []):
@@ -126,7 +111,7 @@ def ensure_claude_hook() -> bool:
126111
found_active_hook = True
127112
break
128113

129-
if found_start_hook and found_stop_hook and found_active_hook:
114+
if found_stop_hook and found_active_hook:
130115
console.print("✅ Hook configuration verified", style="dim")
131116
else:
132117
console.print("⚠️ Hook was written but verification failed", style="yellow")

src/codegen/cli/commands/claude/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Claude Code command with session tracking."""
22

3+
import json
34
import os
45
import signal
56
import subprocess
@@ -20,7 +21,7 @@
2021
create_claude_session,
2122
)
2223
from codegen.cli.commands.claude.config.mcp_setup import add_codegen_mcp_server, cleanup_codegen_mcp_server
23-
from codegen.cli.commands.claude.hooks import cleanup_claude_hook, ensure_claude_hook, get_codegen_url
24+
from codegen.cli.commands.claude.hooks import cleanup_claude_hook, ensure_claude_hook, get_codegen_url, SESSION_FILE
2425
from codegen.cli.commands.claude.quiet_console import console
2526
from codegen.cli.rich.spinners import create_spinner
2627
from codegen.cli.utils.org import resolve_org_id
@@ -98,12 +99,29 @@ def _run_claude_interactive(resolved_org_id: int, no_mcp: bool | None) -> None:
9899
else:
99100
console.print("⚠️ Could not create backend session at startup (will rely on hooks)", style="yellow")
100101
except Exception as e:
102+
agent_run_id = None
101103
console.print(f"⚠️ Session creation error at startup: {e}", style="yellow")
102104

103105
# Set up Claude hook for session tracking
104106
if not ensure_claude_hook():
105107
console.print("⚠️ Failed to set up session tracking hook", style="yellow")
106108

109+
# Write session context file for downstream hooks and tools (after hook setup)
110+
try:
111+
SESSION_FILE.parent.mkdir(exist_ok=True)
112+
session_payload = {
113+
"session_id": session_id,
114+
"agent_run_id": agent_run_id,
115+
"org_id": resolved_org_id,
116+
"hook_event": "Startup",
117+
}
118+
with open(SESSION_FILE, "w") as f:
119+
json.dump(session_payload, f, indent=2)
120+
f.write("\n")
121+
console.print("📝 Wrote session file to ~/.codegen/claude-session.json", style="dim")
122+
except Exception as e:
123+
console.print(f"⚠️ Could not write session file: {e}", style="yellow")
124+
107125
# Initialize log watcher manager
108126
log_watcher_manager = ClaudeLogWatcherManager()
109127

0 commit comments

Comments
 (0)