chenhg5/cc-connect · error

session file not found: %s

Error message

session file not found: %s

What it means

DeleteSession (agent/claudecode/claudecode.go:634) locates the transcript file <projectDir>/<sessionID>.jsonl and removes it. When the project directory exists but os.Stat reports the specific session file missing, it throws 'session file not found: <sessionID>'. This means Claude Code has sessions for this work_dir, but no transcript with that exact session ID.

Source

Thrown at agent/claudecode/claudecode.go:634

func (a *Agent) DeleteSession(_ context.Context, sessionID string) error {
	homeDir, err := os.UserHomeDir()
	if err != nil {
		return fmt.Errorf("claudecode: cannot determine home dir: %w", err)
	}
	a.mu.RLock()
	workDir := a.workDir
	a.mu.RUnlock()
	absWorkDir, err := filepath.Abs(workDir)
	if err != nil {
		return fmt.Errorf("claudecode: resolve work_dir: %w", err)
	}
	projectDir := findProjectDir(homeDir, absWorkDir)
	if projectDir == "" {
		return fmt.Errorf("session not found")
	}
	path := filepath.Join(projectDir, sessionID+".jsonl")
	if _, err := os.Stat(path); os.IsNotExist(err) {
		return fmt.Errorf("session file not found: %s", sessionID)
	}
	return os.Remove(path)
}

// extractStringContent attempts to extract a plain string from a json.RawMessage.
// Returns empty string if the raw message is not a JSON string.
func extractStringContent(raw json.RawMessage) string {
	if len(raw) == 0 {
		return ""
	}
	var s string
	if err := json.Unmarshal(raw, &s); err != nil {
		return ""
	}
	return s
}

func scanSessionMeta(path string) (string, int) {

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Call ListSessions(ctx, workDir) first and confirm the session ID is present before deleting.
  2. Check the exact filename exists: ls ~/.claude/projects/<encoded-dir>/<sessionID>.jsonl.
  3. Treat as an idempotent no-op if the file is already gone (os.IsNotExist → return nil).
  4. Verify the session ID came from the same workDir you pass to DeleteSession; IDs are scoped per project directory.

Example fix

// before
if err := agent.DeleteSession(ctx, workDir, sessionID); err != nil {
    return fmt.Errorf("delete failed: %w", err)
}
// after
sessions, _ := agent.ListSessions(ctx, workDir)
found := false
for _, s := range sessions {
    if s.ID == sessionID { found = true; break }
}
if !found {
    return nil // nothing to delete
}
return agent.DeleteSession(ctx, workDir, sessionID)
Defensive patterns

Strategy: validation

Validate before calling

sessions, err := agent.ListSessions(ctx, workDir)
if err != nil { return err }
exists := false
for _, s := range sessions {
    if s.ID == sessionID { exists = true; break }
}
if !exists { return fmt.Errorf("unknown session %s", sessionID) }

Try / catch

if err := agent.DeleteSession(ctx, workDir, id); err != nil {
    if strings.Contains(err.Error(), "session file not found") {
        log.Info("session already deleted", "id", id)
        return nil
    }
    return err
}

Prevention

When it happens

Trigger: Calling DeleteSession(ctx, workDir, sessionID) where the project dir exists but <projectDir>/<sessionID>.jsonl is absent — wrong/stale session ID, transcript already deleted, or a sessionID containing characters that don't match the on-disk filename.

Common situations: Using a session ID from another machine or another work_dir; deleting a session Claude Code already pruned/cleaned up; passing a truncated or reformatted ID (e.g. from a chat message) rather than the exact ID from ListSessions.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/eccf08ef2840b70e. Report an issue: GitHub.