Files
thedotmack__claude-mem/scripts/worker-logs.cjs
Alex Newman 6682ea6e3e fix(windows): address PR #3661 review and tester findings
Four fixes from the megafix review cycle:

- mirror-dir.cjs: refuse identical or ancestor/descendant source and
  destination roots before any filesystem mutation. The --delete pass
  enumerated the destination first, so a source nested inside the
  destination was seen as stale and recursively deleted (Greptile P1).

- worker-logs.cjs: track file identity (dev/ino) in the --follow loop.
  A rename-and-recreate rotation whose replacement matched the old
  offset's byte size was skipped forever by the size-only check
  (Greptile P2).

- windows-git-bash-preflight: derive bin\bash.exe from every `git`
  PATH hit instead of only the first. Git for Windows puts both
  mingw64\bin\git.exe and cmd\git.exe on PATH; when mingw64 sorts
  first the derivation missed the real bash and doctor reported a
  false negative (tester report, D:\JavaTool\GIT layout).

- doctor: a missing .install-version marker with node_modules present
  is now a warn, not a required failure. The marker is written only by
  the npx installer — marketplace-flow and dev build-and-sync installs
  never have one, so the check always failed for them and its repair
  suggestion would overwrite the checkout under test.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JT1VTKoaTf7VfePb7nVfwz
2026-08-25 16:24:52 -07:00

104 lines
3.2 KiB
JavaScript

#!/usr/bin/env node
const { closeSync, fstatSync, openSync, readSync, watchFile } = require('fs');
const path = require('path');
const os = require('os');
const LINE_COUNT = 50;
const POLL_INTERVAL_MS = 250;
const CHUNK_SIZE = 64 * 1024;
function todaysLogPath() {
const now = new Date();
const stamp = [
now.getFullYear(),
String(now.getMonth() + 1).padStart(2, '0'),
String(now.getDate()).padStart(2, '0'),
].join('-');
return path.join(os.homedir(), '.claude-mem', 'logs', `worker-${stamp}.log`);
}
function readAt(fd, position, length) {
const buffer = Buffer.alloc(length);
const bytes = readSync(fd, buffer, 0, length, position);
if (bytes !== length) {
throw new Error(`short read at ${position}: expected ${length} bytes, got ${bytes}`);
}
return buffer;
}
function countNewlines(buffer) {
let count = 0;
let index = buffer.indexOf(0x0a);
while (index !== -1) {
count++;
index = buffer.indexOf(0x0a, index + 1);
}
return count;
}
// Worker logs grow without bound, so this walks backwards in fixed chunks until
// it has one more newline than it needs, rather than decoding the whole file to
// keep its last few lines. Reading one newline past the target also guarantees
// the chunk boundary is discarded with the partial line in front of it, so a
// multi-byte character split across chunks can never reach the output.
function readLastLines(fd, size, lineCount) {
const chunks = [];
let position = size;
let newlines = 0;
while (position > 0 && newlines <= lineCount) {
const length = Math.min(CHUNK_SIZE, position);
position -= length;
const chunk = readAt(fd, position, length);
chunks.unshift(chunk);
newlines += countNewlines(chunk);
}
const lines = Buffer.concat(chunks).toString('utf-8').split('\n');
if (lines[lines.length - 1] === '') lines.pop();
return lines.slice(-lineCount);
}
const follow = process.argv.includes('--follow');
const logPath = todaysLogPath();
let fd;
try {
fd = openSync(logPath, 'r');
} catch (error) {
console.error('\x1b[31m%s\x1b[0m', `Cannot read worker log ${logPath}: ${error.message}`);
process.exit(1);
}
let size;
try {
size = fstatSync(fd).size;
const lines = readLastLines(fd, size, LINE_COUNT);
if (lines.length > 0) console.log(lines.join('\n'));
} finally {
closeSync(fd);
}
if (follow) {
let offset = size;
watchFile(logPath, { interval: POLL_INTERVAL_MS }, (current, previous) => {
// A rename-and-recreate rotation can leave the replacement at exactly the
// previous offset's size, so byte counts alone cannot detect it — a change
// of file identity must also reset the read position. A missing file stats
// as all-zero, and the size === offset check below skips the read until it
// reappears.
const replaced = current.ino !== previous.ino || current.dev !== previous.dev;
if (replaced || current.size < offset) offset = 0;
if (current.size === offset) return;
const appended = openSync(logPath, 'r');
try {
const buffer = readAt(appended, offset, current.size - offset);
offset += buffer.length;
process.stdout.write(buffer);
} finally {
closeSync(appended);
}
});
}