Skip to content

Commit de58274

Browse files
author
SynTaek
authored
Merge pull request #2 from SynTaek/fix-initialization-crash
Fix initialization crash on new project
2 parents b84c8cf + da12ca5 commit de58274

4 files changed

Lines changed: 30 additions & 101 deletions

File tree

reproduce_issue.py

Lines changed: 0 additions & 99 deletions
This file was deleted.

src/code_trajectory/server.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def _initialize_components(path: str) -> str:
6565
if state.watcher:
6666
state.watcher.stop()
6767

68+
# Check if this is a new initialization before creating the recorder (which creates the repo)
69+
is_new_initialization = not os.path.exists(shadow_repo_path)
70+
6871
try:
6972
state.recorder = Recorder(target_path)
7073
state.watcher = Watcher(target_path, state.recorder)
@@ -73,6 +76,12 @@ def _initialize_components(path: str) -> str:
7376

7477
state.watcher.start()
7578
logger.info(f"Initialized components for {target_path}")
79+
80+
if is_new_initialization:
81+
return (
82+
"New project initialized. No history available yet. "
83+
"Do NOT call get_session_summary."
84+
)
7685
return f"Successfully configured to track: {target_path}"
7786
except Exception as e:
7887
logger.error(f"Failed to initialize components: {e}")

src/code_trajectory/trajectory.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ def get_global_trajectory(self, limit: int = 20, since_consolidate: bool = False
126126
commits = list(self.recorder.repo.iter_commits(max_count=limit))
127127

128128
except Exception as e:
129+
# Check for empty repo error (gitpython usually raises ValueError or GitCommandError)
130+
error_msg = str(e)
131+
# More specific checks for empty repository states
132+
if "Reference at 'refs/heads/master' does not exist" in error_msg:
133+
return "No history available"
134+
135+
# Check for GitCommandError that indicates no commits (git log fails)
136+
if hasattr(e, 'stderr') and "does not have any commits yet" in str(e.stderr):
137+
return "No history available"
138+
139+
# Check for BadObject (happens when HEAD is invalid)
140+
if "BadObject" in error_msg and "HEAD" in error_msg:
141+
return "No history available"
142+
129143
logger.error(f"Failed to fetch global trajectory: {e}")
130144
return f"Error fetching global trajectory: {e}"
131145

@@ -166,6 +180,10 @@ def get_session_summary(self) -> str:
166180

167181
timestamps = [int(ts) for ts in timestamps_output.splitlines()]
168182
except Exception as e:
183+
error_msg = str(e)
184+
if "does not have any commits yet" in error_msg:
185+
return "No history available"
186+
169187
logger.error(f"Failed to fetch commit timestamps: {e}")
170188
return f"Error analyzing session history: {e}"
171189

tests/test_server_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ def test_explicit_configuration(temp_project_dir):
1212

1313
result = configure_project(temp_project_dir)
1414

15-
assert "Successfully configured" in result
15+
# Check for either success message (existing) or new init message
16+
assert "Successfully configured" in result or "New project initialized" in result
1617
assert state.project_path == temp_project_dir
1718
assert state.recorder is not None
1819
assert state.recorder.project_root == temp_project_dir
@@ -47,7 +48,7 @@ def test_reconfiguration(temp_project_dir):
4748
# Configure second project
4849
result = configure_project(second_dir)
4950

50-
assert "Successfully configured" in result
51+
assert "Successfully configured" in result or "New project initialized" in result
5152
assert state.project_path == second_dir
5253
assert state.watcher != old_watcher
5354

0 commit comments

Comments
 (0)