refactor(daemon): share the output ring-window helper

The embedded daemon and the xmrig manager each kept their own copy of
the console output ring: append new bytes, trim the front to a cap at a
newline boundary, and hand a reader everything since its offset. The two
copies had drifted. The daemon tracked an absolute byte total so a
reader's offset stayed valid across a front-trim, while the miner keyed
its reader off the live buffer size, so a trim could silently rewind the
console feed.

- The append/trim and the since-offset logic now live in one small
  header, outputWindowSince and appendBounded, included by both. The
  trim keeps the newline-boundary cut and the guard that stops a lone
  trailing newline from erasing the whole buffer.
- The xmrig manager now carries the same absolute byte total, so its
  reader resolves against the window the same way and survives a trim
  (and a process restart that clears the buffer but keeps the total).

Tests cover feeding the ring well past the trim point while a reader
polls each chunk (the feed is continuous and never dies at the trim),
the lone-trailing-newline guard, and a cleared buffer with a retained
total.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-10-14 06:26:13 -05:00
co-authored by Claude Opus 4.8
parent e77b960cc5
commit e9042f091e
6 changed files with 85 additions and 39 deletions
+1 -12
View File
@@ -276,18 +276,7 @@ static constexpr size_t MAX_OUTPUT_BYTES = 1024 * 1024; // 1 MB
void EmbeddedDaemon::appendOutput(const char* data, size_t len)
{
// Caller must hold output_mutex_
process_output_.append(data, len);
output_total_ += len;
if (process_output_.size() > MAX_OUTPUT_BYTES + MAX_OUTPUT_BYTES / 4) {
// Trim to MAX_OUTPUT_BYTES, cutting at a newline boundary
size_t trim_to = process_output_.size() - MAX_OUTPUT_BYTES;
size_t nl = process_output_.find('\n', trim_to);
if (nl != std::string::npos && nl < process_output_.size() - 1) {
process_output_.erase(0, nl + 1);
} else {
process_output_.erase(0, trim_to);
}
}
appendBounded(process_output_, output_total_, data, len, MAX_OUTPUT_BYTES);
}
std::vector<std::string> EmbeddedDaemon::getRecentLines(int maxLines) const