Skip to content

implement History::truncate#1082

Draft
rbran wants to merge 4 commits into
nushell:mainfrom
rbran:delete_old
Draft

implement History::truncate#1082
rbran wants to merge 4 commits into
nushell:mainfrom
rbran:delete_old

Conversation

@rbran
Copy link
Copy Markdown

@rbran rbran commented May 18, 2026

Function used to remove multiple old history entries.

Comment thread src/history/base.rs Outdated
/// remove an item from this history
fn delete(&mut self, h: HistoryItemId) -> Result<()>;
/// remove all history items, except the `keep` newest entries
fn delete_old(&mut self, keep: usize) -> Result<usize>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think truncate is a better name for this?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd vote for it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a way better name.

Comment thread src/history/item.rs Outdated
}

/// get the raw value from this session id
pub fn as_raw(&self) -> i64 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand this right that first of all it has no consumers and secondly could be achieved by i64::from(session)?
Seems redundant and unused.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's correct, I completely missed the From<HistorySessionId> for i64

@kronberger-droid
Copy link
Copy Markdown
Collaborator

Don't forget to run cargo fmt with default config so CI goes green.

@kronberger-droid
Copy link
Copy Markdown
Collaborator

kronberger-droid commented May 19, 2026

Pushed a failing test truncate_history_with_backing_file that shows FileBackedHistory::truncate doesn't persist across Drop -> sync -> reopen.
The in-memory drain works, but sync() reloads from disk and the original entries come back.

@kronberger-droid kronberger-droid changed the title implement delete_old entries implement History::truncate May 19, 2026
@rbran
Copy link
Copy Markdown
Author

rbran commented May 19, 2026

Having a file history with multiple sessions is significantly more complicated then I though.

If I understand correctly, will not be possible to implement nushell/nushell#18235 correctly with the current architecture.

Example of archival not triggering correctly:

  • Instance 1 start (empty)
  • Instance 1 create max_size - 3 history entries
  • Instance 1 ends and sync
  • Instance 2 start (load max_size - 3 history entries)
  • Instance 3 start (load max_size - 3 history entries)
  • Instance 2 create 2 history entries
  • Instance 3 create 2 history entries
  • Instance 2 ends and sync, just append to history file, now max_size - 1 history entries
  • Instance 3 ends and sync, override the history file dropping one entry, now max_size history entries

Nushell will only be able to call count_all and get a result that is equal to max_size after entries are discarded. This will skip the archival process all together.

I will mark this PR as draft for now, fell free to close it. I'll probably create new one if I came up with an idea that don't require a big rewrite.

@rbran rbran marked this pull request as draft May 19, 2026 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants