Skip to content

feat(mongodb-log-writer): add support for recursively cleaning up old log files MCP-103 #565

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

Closed
wants to merge 2 commits into from

Conversation

nirinchev
Copy link
Collaborator

Description

This allows the log manager to optionally consider log files inside child directories. This is related to MCP-103 where we will be creating child log managers writing files inside subdirectories for specific sessions, while the parent log manager will be responsible for cleaning up all logs.

Open Questions

Checklist

@nirinchev nirinchev requested a review from kmruiz August 5, 2025 11:38
try {
dirHandle = await fs.opendir(dir);
dirHandle = await fs.opendir(dir, { recursive });
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure we are aware that this can remove folders that we might not own. With the current logic, if someone creates a folder for retention (f.e, a daily folder in the same folder and copies all the logs) the logging system won't delete them, but with this changes, they will.

I'm concerned this might inadvertently delete files that we shouldn't.

I think it's fine to keep recursive as an optional value that defaults to the current behaviour, but I would add a comment in the function mentioning this change of behaviour and dangers.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, that's also one of my concerns. I'll try and explore a different approach here - might be a better way to handle things, so will put that back in draft.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, that's also one of my concerns. I'll try and explore a different approach here - might be a better way to handle things, so will put that back in draft.

}
}

let basePath = path.dirname(filePath);
while (basePath !== this._options.directory) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if this._options.directory was specified as /foo/bar/? Repeatedly calling path.dirname() on /foo/bar/baz will never reach that filename.

I'd imagine that path.normalize() together with some sort of prefix check would work here, but I'm not sure if that's the most reliable way to do this either

// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
// If the directory is not empty, we stop deleting.
if (err?.code === 'ENOTEMPTY' || err?.code === 'ENOENT') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

These would be the most common errors that rmdir() could encounter, but I don't think it makes sense to continue in any error situation here, right?


if (!dirent.isFile()) {
continue;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This leaves us in a situation where, if removing a directory fails for any reason, we're never going to clean that directory up. I think that may be something we just need to accept, unless we want to start e.g. imposing naming restrictions on the nested subdirectories – that's something we should probably decide on now, though.

@addaleax addaleax changed the title feat: add support for recursively cleaning up old log files MCP-103 feat(mongodb-log-writer): add support for recursively cleaning up old log files MCP-103 Aug 5, 2025
@nirinchev nirinchev marked this pull request as draft August 5, 2025 12:28
@nirinchev
Copy link
Collaborator Author

Moving that back into draft - I feel I may have jumped the gun on that approach and I'm not super happy with some of the implications from this change. I'll think a bit more about it and see if I can come up with something better.

@nirinchev
Copy link
Collaborator Author

Closing this as I've ultimately decided to pursue the other approach, where we log from all sessions in the same log file, but we'll be attributing log messages from different sessions with their ids.

@nirinchev nirinchev closed this Aug 5, 2025
@nirinchev nirinchev deleted the ni/recursive-log-manager branch August 5, 2025 16:19
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