chenhg5/cc-connect · error

pi: read history

Error message

pi: read history: %w

What it means

readPiHistory streams a pi session JSONL file with a bufio.Scanner and appends HistoryEntry items; if scanner.Err() reports an I/O failure mid-read (not EOF), it wraps the error as 'pi: read history'. Called by GetSessionHistory when fetching past conversation turns.

Solutions

  1. If the cause is bufio.ErrTooLong (very large single line), the scanner buffer must be enlarged in the adapter (scanner.Buffer with a larger max) — report/patch, since callers cannot fix it externally.
  2. Re-run GetSessionHistory; transient I/O errors on network filesystems often resolve on retry.
  3. Verify the session file still exists and is readable at the path findSessionFile resolved.
  4. Check disk/FS health if errors persist (dmesg, mount status).

Example fix

// caller-side retry before
entries, err := agent.GetSessionHistory(ctx, sessionID, 50)
if err != nil {
    return err
}
// after
entries, err := agent.GetSessionHistory(ctx, sessionID, 50)
if err != nil {
    time.Sleep(500 * time.Millisecond)
    entries, err = agent.GetSessionHistory(ctx, sessionID, 50)
    if err != nil {
        return fmt.Errorf("get history: %w", err)
    }
}
Defensive patterns

Strategy: retry

Validate before calling

sessFile := findSessionFile(sessDir, sessionID)
f, err := os.Open(sessFile)
if err != nil {
    return fmt.Errorf("history file unreadable: %w", err)
}
f.Close()

Try / catch

entries, err := agent.GetSessionHistory(ctx, sessionID, limit)
if err != nil {
    select {
    case <-time.After(500 * time.Millisecond):
        entries, err = agent.GetSessionHistory(ctx, sessionID, limit)
    case <-ctx.Done():
        return ctx.Err()
    }
}

Prevention

When it happens

Trigger: Calling GetSessionHistory for a session whose JSONL file exists but becomes unreadable during scanning: I/O errors, file deleted/truncated mid-read, or device errors. Note: an overly long JSONL line exceeding bufio.Scanner's default 64KB token limit also surfaces as a scanner error here.

Common situations: Very long agent messages/tool outputs producing single JSONL lines >64KB (bufio.ErrTooLong); network-mounted home directories dropping connections mid-read; files rotated by cleanup jobs while history is being fetched.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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

Appendix: source

Thrown at agent/pi/pi.go:673

		var text string
		content, _ := msg["content"].([]any)
		for _, c := range content {
			item, _ := c.(map[string]any)
			if item != nil {
				if t, ok := item["text"].(string); ok && t != "" {
					text = t
					break
				}
			}
		}
		if text == "" {
			continue
		}
		all = append(all, core.HistoryEntry{Role: role, Content: text})
	}
	if err := scanner.Err(); err != nil {
		return nil, fmt.Errorf("pi: read history: %w", err)
	}

	if limit > 0 && len(all) > limit {
		all = all[len(all)-limit:]
	}
	return all, nil
}

View on GitHub (pinned to 4000b2338a)