chenhg5/cc-connect · error

session file not found: %s

Error message

session file not found: %s

What it means

DeleteSession reads the ~/.gemini/tmp/<slug>/chats directory to scan session files; if os.ReadDir fails (most commonly the directory doesn't exist), it reports `session file not found` for the requested sessionID. Note it conflates 'chats dir missing' with 'session missing'.

Source

Thrown at agent/gemini/gemini.go:244

	return newGeminiSession(ctx, cmd, extraArgs, workDir, model, mode, sessionID, extraEnv, timeout)
}

// ListSessions reads sessions from ~/.gemini/tmp/<project_hash>/chats/.
func (a *Agent) ListSessions(_ context.Context) ([]core.AgentSessionInfo, error) {
	return listGeminiSessions(a.workDir)
}

func (a *Agent) DeleteSession(_ context.Context, sessionID string) error {
	homeDir, err := os.UserHomeDir()
	if err != nil {
		return fmt.Errorf("gemini: cannot determine home dir: %w", err)
	}
	chatsDir := filepath.Join(homeDir, ".gemini", "tmp", geminiProjectSlug(a.workDir), "chats")
	// Session files are named session-<timestamp>-<uuid_prefix>.json, not <uuid>.json.
	// Scan the directory to find the file containing the matching sessionId.
	entries, err := os.ReadDir(chatsDir)
	if err != nil {
		return fmt.Errorf("session file not found: %s", sessionID)
	}
	for _, entry := range entries {
		if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".json") {
			continue
		}
		fpath := filepath.Join(chatsDir, entry.Name())
		data, err := os.ReadFile(fpath)
		if err != nil {
			continue
		}
		var sf struct {
			SessionID string `json:"sessionId"`
		}
		if json.Unmarshal(data, &sf) == nil && sf.SessionID == sessionID {
			return os.Remove(fpath)
		}
	}
	return fmt.Errorf("session file not found: %s", sessionID)

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Verify the agent's workDir matches the one used when the session was created
  2. Run `gemini` once in the workDir to confirm the chats directory exists under ~/.gemini/tmp/<slug>/chats
  3. List sessions first (ListSessions) and only delete IDs it returns
  4. If the directory is missing, treat the session as already deleted

Example fix

// before: blind delete
cleanup := func(id string) { _ = agent.DeleteSession(ctx, id) }
// after: guard with a list
sessions, err := agent.ListSessions(ctx)
if err == nil {
  for _, s := range sessions {
    if s.ID == targetID { _ = agent.DeleteSession(ctx, targetID) }
  }
}
Defensive patterns

Strategy: validation

Validate before calling

sessions, err := agent.ListSessions(ctx)
if err != nil { return err }
known := map[string]bool{}
for _, s := range sessions { known[s.ID] = true }
if !known[sessionID] { return fmt.Errorf("skip delete: %s not present", sessionID) }

Try / catch

if err := agent.DeleteSession(ctx, id); err != nil {
  if strings.Contains(err.Error(), "session file not found") {
    log.Println("already gone, treating as success")
  } else { return err }
}

Prevention

When it happens

Trigger: Calling DeleteSession when the per-project chats directory does not exist (no sessions ever created for this workDir, wrong workDir configured, or the .gemini dir was deleted).

Common situations: Deleting a session after switching/misconfiguring workDir so the project slug no longer matches; gemini CLI never used in this project; user manually cleared ~/.gemini/tmp.

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/caa7af41c099c170. Report an issue: GitHub.