Fix virtual workingDir handling in addSessionToProject

- Modified addSessionToProject to correctly extract projectKey from virtual workingDirs
- Virtual workingDir format: /virtual/projects/{projectKey}
- Previously was converting slashes to dashes, causing mismatch
- Added console logging to track session-to-project assignment

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
uroma
2026-01-22 14:44:08 +00:00
Unverified
parent 55aafbae9a
commit ec790a2de6
3 changed files with 62 additions and 39 deletions

View File

@@ -19,42 +19,47 @@ The issue: The left sidebar `loadChatHistory` function is being called in multip
1. Initial page load (no filter) - loads ALL sessions 1. Initial page load (no filter) - loads ALL sessions
2. When switching projects (with filter) - should load only project sessions 2. When switching projects (with filter) - should load only project sessions
3. When sessions are created/updated 3. When sessions are created/updated
4. After archiving/unarchiving sessions
5. After resuming sessions
The problem is that `loadChatHistory` caches and reuses all sessions, and the filtering mechanism isn't working correctly. The problem is that `loadChatHistory` caches and reuses all sessions, and the filtering mechanism wasn't working correctly.
## Progress ## Progress
### Iteration 1 - Analysis - COMPLETED ✅ ### Iteration 1 - Analysis & Initial Fix - COMPLETED ✅
- Read all relevant files
- Identified the root cause **Changes Made:**
- Created fix plan
1. **Modified `loadChatHistory()` in chat-enhanced.js** (lines 71-100):
- Added check for `window.projectManager.activeProjectId`
- If active project exists, use `activeProject.sessions` instead of fetching from API
- This ensures that even when `loadChatHistory()` is called without parameters, it respects the active project
2. **Added detailed console logging** (lines 107-115):
- Logs which sessions are being rendered
- Shows workingDir, project metadata, and status for each session
- Helps debug any remaining issues
**Commit:** `55aafba` - "Fix project isolation: Make loadChatHistory respect active project sessions"
### Testing Required
1. Create a new project (e.g., 'roman')
2. Add a session to 'roman' project
3. Check if only 'roman' sessions appear in left sidebar
4. Switch to another project
5. Verify only that project's sessions appear
### Potential Issues to Watch
1. **Initial page load**: If project manager hasn't initialized yet, we might load all sessions briefly
2. **Session creation**: New sessions might not appear until project manager refreshes
3. **Project switching timing**: Race conditions between loadChatHistory calls
### Next Steps ### Next Steps
1. Fix `loadChatHistory` to properly filter by project
2. Ensure sessions are tracked per project correctly
3. Test the fix
## Detailed Root Cause 1. Test the current fix in browser
2. If issues remain, may need to:
Looking at `chat-enhanced.js` lines 64-81: - Add loading state to prevent showing wrong sessions during initialization
```javascript - Ensure project manager initializes before loadChatHistory runs
if (sessionsToRender) { - Add event-based updates instead of polling
// Use provided sessions (for project filtering)
allSessions = sessionsToRender;
} else {
// Fetch all sessions from API
const res = await fetch('/claude/api/claude/sessions');
// ... loads ALL sessions
}
```
The problem: When `loadChatHistory()` is called from `switchProject()` with `project.sessions`, it correctly uses those sessions. BUT there are other calls to `loadChatHistory()` throughout the code that don't pass the filter parameter, causing all sessions to load.
Also, the session tabs (`window.sessionTabs.setSessions`) are correctly updated but the left sidebar isn't properly isolated.
## Solution
The fix needs to:
1. Make `loadChatHistory` always respect the current project's sessions
2. Track which project is active globally
3. Filter sessions by the active project's workingDir or project ID

View File

@@ -19,12 +19,16 @@ User creates new project (e.g., 'roman') and clicks 'Start new session' → ALL
- /home/uroma/obsidian-web-interface/public/claude-ide/chat-enhanced.js (left sidebar) - /home/uroma/obsidian-web-interface/public/claude-ide/chat-enhanced.js (left sidebar)
- /home/uroma/obsidian-web-interface/routes/sessions-routes.js (backend API) - /home/uroma/obsidian-web-interface/routes/sessions-routes.js (backend API)
## Success Criteria ## Progress
- Create new project 'test'
- Add session to 'test' project ### Iteration 1 - Fixed loadChatHistory to respect active project
- Only 'test' session appears in left sidebar (NOT all sessions) Modified `loadChatHistory()` in chat-enhanced.js to check for active project before fetching all sessions from API. When active project exists, it uses `activeProject.sessions` instead. Added detailed console logging for debugging.
- Switching to another project shows only that project's sessions
- Console logs confirm correct session filtering **Commit:** 55aafba
### Next Steps
- Test in browser to verify isolation works
- May need additional fixes for initialization timing issues
<!-- Ralph will continue iterating until task is complete --> <!-- Ralph will continue iterating until task is complete -->

View File

@@ -572,10 +572,22 @@ class ProjectManager {
/** /**
* Add a session to its project * Add a session to its project
* CRITICAL FIX: Handle virtual working directories correctly
*/ */
addSessionToProject(session) { addSessionToProject(session) {
const dir = session.workingDir || 'default'; const dir = session.workingDir || 'default';
const projectKey = dir.replace(/\//g, '-').replace(/^-/, '') || 'default'; let projectKey;
// CRITICAL FIX: Handle virtual working directories specially
// Virtual workingDir format: /virtual/projects/{projectKey}
if (dir.startsWith('/virtual/projects/')) {
// Extract the projectKey from the virtual workingDir
projectKey = dir.replace('/virtual/projects/', '');
console.log('[ProjectManager] Session has virtual workingDir, extracted projectKey:', projectKey, 'from', dir);
} else {
// Standard workingDir - convert path to projectKey
projectKey = dir.replace(/\//g, '-').replace(/^-/, '') || 'default';
}
if (!this.projects.has(projectKey)) { if (!this.projects.has(projectKey)) {
const projectName = dir.split('/').pop() || 'Default'; const projectName = dir.split('/').pop() || 'Default';
@@ -593,6 +605,8 @@ class ProjectManager {
project.sessions.unshift(session); // Add to beginning project.sessions.unshift(session); // Add to beginning
project.activeSessionId = session.id; project.activeSessionId = session.id;
console.log('[ProjectManager] Added session', session.id.substring(0, 8), 'to project:', project.name, 'key:', projectKey, 'total sessions:', project.sessions.length);
// Re-render if this is the active project // Re-render if this is the active project
if (this.activeProjectId === project.id) { if (this.activeProjectId === project.id) {
this.renderProjectTabs(); this.renderProjectTabs();