Repository: atuinsh/atuin (language: rust)

Issue:
[Bug]: History stopped recording
### What did you expect to happen?

Show history correctly when I press `UP-Arrow` or `atuin history list`

### What happened?

History stopped at some point in time and won't update. I reinstalled the atuin binary and also removed the `~/.config/atuin` and `~/.local/share/atuin` directories. Same issues afterwards. I've run `atuin login` and `atuin sync` and they have worked correctly.

### Atuin doctor output

```yaml
{
  "atuin": {
    "version": "18.3.0",
    "sync": {
      "cloud": true,
      "records": true,
      "auto_sync": true,
      "last_sync": "2024-07-02 10:49:25.55878 +00:00:00"
    },
    "sqlite_version": "3.44.0"
  },
  "shell": {
    "name": "zsh",
    "default": "zsh",
    "plugins": [
      "atuin"
    ],
    "preexec": "built-in"
  },
  "system": {
    "os": "Darwin",
    "arch": "arm64",
    "version": "14.1.1",
    "disks": [
      {
        "name": "Macintosh HD",
        "filesystem": "apfs"
      },
      {
        "name": "Macintosh HD",
        "filesystem": "apfs"
      },
      {
        "name": "projects",
        "filesystem": "apfs"
      }
    ]
  }
}
```


### Code of Conduct

- [X] I agree to follow this project's Code of Conduct


Discussion / hints:
Could you share your zshrc please?
I use the same zshrc on two different computers and atuin works correctly on my Linux machine, but stopped working (it was working for months) on my Mac (https://github.com/JamyGolden/dotfiles/blob/main/apps/4.cli/atuin/rc). 

This could be unrelated but mentioning in case it somehow is: I had an issue on the Mac where the date was set to a year ago and it wouldn't automatically change regardless of what I did in settings. After some searching, I found needed to remove `/var/db/timed/com.apple.timed.plist` and restart the machine to fix it, which it did. The date being wrong stopped https connections from working so I was having issues with basically every service on my system. Maybe this could be related for atuin and the date issue somehow corrupt it in a more permanent way?  (I did remove the binary and config and data dirs, so if it did corrupt somehow, that should have solved it though, right)

There is still no issue with atuin on my Linux machine, even after the issue on the Mac.
Hmmm I see. When you say history isn't shown correctly, do you mean that the UI pops up but history isn't there? Or no ui, no recording, etc?

Also: does `atuin history list` show recent history for you?
Everything seems to be working correctly, it's just that the history list isn't updated with any command. So it seems like the history is "paused" in time, so `atuin history list` shows the history when it was being recorded.
Ah I see!

If you run `export ATUIN_LOG=debug`, do you get log output while using your terminal?

It should be very chatty
I have also been having this problem which brought me to this thread. Interestingly exporting the `ATUIN_LOG` variable seemed to half fix the issue at times but generally the behavior has been very inconsistent. 

I thought maybe the sqlite database was corrupted so I deleted everything in `$HOME/.local/share/atuin` and then re-logged in but history was still not being recorded. 

Next I queried `history.db` and saw that commands I ran seemed to have been recorded but the `duration` and `exit` fields both had `-1` as the value and the command field's value appeared to be encrypted or encoded. Further inspection showed the `deleted_at` field was set to a timestamp. Here's an example row with a few columns redacted:
| id | timestamp | duration | exit | command | cwd | session | hostname | deleted_at |
|--------|--------|--------|--------|--------|--------|--------|--------|--------|
| 0190b75b5cc27d7487a5f7fa340a010f | 1721063136450568000 | -1 | -1 | u141ZxCdiXTv8hlELHuuJ63wq2WWzQYm | [REDACTED] | 0190b71f28e9704d8751041a2f7fcf87 | [REDACTED] | 1721063136492042000 |

I took a shot in the dark and changed the `store_failed` setting back to `true` and this seems to fix the issue so my current best guess is that there's some sort of bug in the code that handles soft-deleting entries for failed commands


  
a quick glance at the code, and it seems like it would be safer to have [this comparison](https://github.com/atuinsh/atuin/blob/947305fc7f3d60e49df8a80ded07c08ea7e334da/crates/atuin/src/command/client/history.rs#L387) be something like `h.exit > 0` since `exit` defaults to `-1` but ultimately it seems like `h.exit` is possibly being checked [here](https://github.com/atuinsh/atuin/blob/947305fc7f3d60e49df8a80ded07c08ea7e334da/crates/atuin/src/command/client/history.rs#L387) before it's set [a few lines later](https://github.com/atuinsh/atuin/blob/947305fc7f3d60e49df8a80ded07c08ea7e334da/crates/atuin/src/command/client/history.rs#L396)? 

Apologies if I'm misreading, I haven't worked in rust before.

Fix patch:
diff --git a/.gitignore b/.gitignore
index 1876e4e38cb..3b61025f187 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,6 +6,7 @@
 .vscode/
 result
 publish.sh
+.envrc
 
 ui/backend/target
 ui/backend/gen
diff --git a/crates/atuin/src/command/client/history.rs b/crates/atuin/src/command/client/history.rs
index a9970559217..17b400f387a 100644
--- a/crates/atuin/src/command/client/history.rs
+++ b/crates/atuin/src/command/client/history.rs
@@ -384,7 +384,7 @@ impl Cmd {
             return Ok(());
         }
 
-        if !settings.store_failed && h.exit != 0 {
+        if !settings.store_failed && exit > 0 {
             debug!("history has non-zero exit code, and store_failed is false");
 
             // the history has already been inserted half complete. remove it


Test patch: