perf(#305): single-pass max-by-mtime for statusline todo lookup (#385)

Replace the per-render readdirSync().filter().map(statSync).sort() chain
with a single-pass max-by-mtime loop. Drops the O(n log n) sort and the
throwaway intermediate array; I/O and resolved-file behavior are identical.
Adds the first behavior-lock test for the todo-resolution path.

The larger disk-backed cache win from the issue is deferred: statusline is
a fresh child process per render, so any cache must be disk-backed with
invalidation/atomic-write design that needs maintainer input.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-05-27 20:21:15 -04:00
committed by GitHub
parent 51aac62e9a
commit 091b7e2c4b
3 changed files with 97 additions and 6 deletions

View File

@@ -368,14 +368,20 @@ function runStatusline() {
const todosDir = path.join(claudeDir, 'todos');
if (session && fs.existsSync(todosDir)) {
try {
const files = fs.readdirSync(todosDir)
.filter(f => f.startsWith(session) && f.includes('-agent-') && f.endsWith('.json'))
.map(f => ({ name: f, mtime: fs.statSync(path.join(todosDir, f)).mtime }))
.sort((a, b) => b.mtime - a.mtime);
// Single-pass max-by-mtime scan: only the newest matching todos file
// is needed, so the O(n log n) sort and the intermediate array from the
// prior `.filter().map(statSync).sort()` chain are unnecessary. Identical
// I/O (one statSync per match) and identical result. (#305)
let latest = null;
for (const entry of fs.readdirSync(todosDir)) {
if (!entry.startsWith(session) || !entry.includes('-agent-') || !entry.endsWith('.json')) continue;
const mtime = fs.statSync(path.join(todosDir, entry)).mtime;
if (!latest || mtime > latest.mtime) latest = { name: entry, mtime };
}
if (files.length > 0) {
if (latest) {
try {
const todos = JSON.parse(fs.readFileSync(path.join(todosDir, files[0].name), 'utf8'));
const todos = JSON.parse(fs.readFileSync(path.join(todosDir, latest.name), 'utf8'));
const inProgress = todos.find(t => t.status === 'in_progress');
if (inProgress) task = inProgress.activeForm || '';
} catch (e) {}