-
Notifications
You must be signed in to change notification settings - Fork 8
fix: history load for non exist directory #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -19,6 +19,7 @@ type history struct { | |||||
| path string | ||||||
| loadCount int | ||||||
| logger *slog.Logger | ||||||
| disabled bool | ||||||
| } | ||||||
|
|
||||||
| func newHistory(historyPath string, logger *slog.Logger) (*history, []prompt.HistoryCommand) { | ||||||
|
|
@@ -37,7 +38,7 @@ func (h *history) loadHistory() []prompt.HistoryCommand { | |||||
| file, err := os.Open(h.path) | ||||||
| if err != nil { | ||||||
| if !os.IsNotExist(err) { | ||||||
| h.logger.Warn("could not open history file", "path", h.path, "error", err) | ||||||
| h.disableHistory("failed load histrory form path, err, hisotry is disabled", err) | ||||||
| } | ||||||
| return []prompt.HistoryCommand{} | ||||||
|
Comment on lines
38
to
43
|
||||||
| } | ||||||
|
|
@@ -49,7 +50,7 @@ func (h *history) loadHistory() []prompt.HistoryCommand { | |||||
|
|
||||||
| entries, err := loadHistory(file, maxHistoryLines, h.logger) | ||||||
| if err != nil { | ||||||
| h.logger.Error("failed to load history", "error", err) | ||||||
| h.disableHistory("failed load histrory form path, err, hisotry is disabled", err) | ||||||
| return []prompt.HistoryCommand{} | ||||||
|
Comment on lines
51
to
54
|
||||||
| } | ||||||
| return entries | ||||||
|
|
@@ -82,18 +83,25 @@ func loadHistory(r io.Reader, maxHistoryLines int, logger *slog.Logger) ([]promp | |||||
| } | ||||||
|
|
||||||
| func (h *history) saveHistory(entries []prompt.HistoryCommand) { | ||||||
| if h.disabled { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| if len(entries) <= h.loadCount { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| newCommands := entries[h.loadCount:] | ||||||
| if len(newCommands) == 0 { | ||||||
|
|
||||||
| historyDir := filepath.Dir(h.path) | ||||||
| if err := os.MkdirAll(historyDir, 0o700); err != nil { | ||||||
| h.disableHistory("failed save history to path, err, history is disabled", err) | ||||||
| return | ||||||
| } | ||||||
|
Comment on lines
+96
to
100
|
||||||
|
|
||||||
| f, err := os.OpenFile(h.path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600) | ||||||
| if err != nil { | ||||||
| h.logger.Error("failed to open history file for writing", "error", err) | ||||||
| h.disableHistory("failed save history to path, err, history is disabled", err) | ||||||
| return | ||||||
| } | ||||||
|
Comment on lines
102
to
106
|
||||||
| defer func() { | ||||||
|
|
@@ -114,12 +122,17 @@ func (h *history) saveHistory(entries []prompt.HistoryCommand) { | |||||
| } | ||||||
|
|
||||||
| if err := w.Flush(); err != nil { | ||||||
| h.logger.Error("failed to flush history file", "error", err) | ||||||
| h.disableHistory("failed save history to path, err, history is disabled", err) | ||||||
|
||||||
| h.disableHistory("failed save history to path, err, history is disabled", err) | |
| h.disableHistory("failed to flush history file; history is disabled", err) |
Copilot
AI
Apr 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The log attribute key here is "err", but the rest of the codebase consistently uses "error" for errors. Consider using "error" for consistency with existing slog logs and tooling/filters.
| h.logger.Error(message, "path", h.path, "err", err) | |
| h.logger.Error(message, "path", h.path, "error", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new error message passed to disableHistory has multiple typos ("histrory", "form", "hisotry") and reads like a placeholder. Please replace it with a clear, correctly spelled message (e.g., "failed to load history from path; disabling history for this session") so logs are actionable.