chenhg5/cc-connect · error

pi: session %q not found

Error message

pi: session %q not found

What it means

DeleteSession in the pi agent adapter removes the on-disk session file for a given sessionID. After locating pi's session directory, it calls findSessionFile(sessDir, sessionID); when no matching session file exists, it returns this error instead of attempting os.Remove. It is the pi adapter's 'record not found' signal for session deletion.

Source

Thrown at agent/pi/pi.go:216

		})
	}

	sort.Slice(sessions, func(i, j int) bool {
		return sessions[i].ModifiedAt.After(sessions[j].ModifiedAt)
	})

	return sessions, nil
}

func (a *Agent) DeleteSession(_ context.Context, sessionID string) error {
	sessDir := piSessionDir(a.workDir)
	if sessDir == "" {
		return fmt.Errorf("pi: cannot determine session directory")
	}

	path := findSessionFile(sessDir, sessionID)
	if path == "" {
		return fmt.Errorf("pi: session %q not found", sessionID)
	}
	return os.Remove(path)
}

func (a *Agent) Stop() error { return nil }

// ── ModeSwitcher ─────────────────────────────────────────────

func (a *Agent) SetMode(mode string) {
	a.mu.Lock()
	defer a.mu.Unlock()
	a.mode = normalizeMode(mode)
	slog.Info("pi: mode changed", "mode", a.mode)
}

func (a *Agent) GetMode() string {
	a.mu.Lock()
	defer a.mu.Unlock()

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Verify the session ID exists (e.g. via ListSessions or by listing files in ~/.pi/agent session dir) before calling DeleteSession.
  2. Treat this error as an idempotent success in callers if the goal is 'session gone' — the file is already absent.
  3. Check that the pi session directory is correct (HOME or PI_USER_DIR env) and contains the session file you expect.
  4. Ensure the sessionID string exactly matches the ID used at creation; re-obtain it from session listing rather than copying from chat text.

Example fix

// before
if err := agent.DeleteSession(sessionID); err != nil {
    return err
}
// after
if err := agent.DeleteSession(sessionID); err != nil {
    if strings.Contains(err.Error(), "not found") {
        log.Printf("session %s already gone, treating as deleted", sessionID)
        return nil
    }
    return fmt.Errorf("delete session: %w", err)
}
Defensive patterns

Strategy: validation

Validate before calling

sessions, err := agent.ListSessions()
if err != nil {
    return fmt.Errorf("list sessions: %w", err)
}
exists := false
for _, s := range sessions {
    if s.ID == sessionID {
        exists = true
        break
    }
}
if !exists {
    return nil // nothing to delete; skip the call
}
err = agent.DeleteSession(sessionID)

Try / catch

if err := agent.DeleteSession(sessionID); err != nil {
    if strings.Contains(err.Error(), "not found") {
        return nil // idempotent delete
    }
    return fmt.Errorf("delete session: %w", err)
}

Prevention

When it happens

Trigger: Calling Agent.DeleteSession(sessionID) when: (1) the sessionID was never created by pi, (2) the session file was already deleted (manually or by a prior DeleteSession), (3) the sessionID string does not match the session file naming convention findSessionFile uses to match files in the session directory, or (4) the session directory resolved from HOME/PI_USER_DIR does not contain the session.

Common situations: Users run /forget or delete-session commands for a stale session ID after the pi data directory was cleaned or the machine changed; double-deletion races where two delete requests fire for the same session; typos or truncated session IDs passed from chat commands.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


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