Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/tools/reflection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { execFile } from "node:child_process";
import { promisify } from "node:util";

const DEFAULT_WINDOW_DAYS = 7;
const DEFAULT_MAX_RESULTS = 50;
const execFileAsync = promisify(execFile);

/**
Expand Down Expand Up @@ -96,8 +95,7 @@ function matchesIgnorePatterns(content, patterns) {

/**
* Core reflection tool logic: read sessions, filter, extract user messages.
* Uses `find` to sort files by mtime (newest first) and returns only the
* top N to avoid scanning every session file in the directory.
* Processes every session within the date window to find signal — no arbitrary cap.
* @param {z.infer<typeof ReflectionSchema>} input - The tool input
* @param {object} options - Runtime options
* @param {string} [options.sessionsDir] - Path to sessions directory
Expand All @@ -110,7 +108,7 @@ export async function reflectionImpl(input, options) {
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - windowDays);

// Use find to sort files by mtime (newest first), limit to top N
// Use find to sort files by mtime (newest first), process all within window
let output;
try {
output = await execFileAsync("find", [
Expand All @@ -134,9 +132,9 @@ export async function reflectionImpl(input, options) {
.filter((l) => l.length > 0);
if (lines.length === 0) return JSON.stringify([]);

// Sort by mtime descending (newest first), take top N
// Sort by mtime descending (newest first)
lines.sort((a, b) => parseFloat(b.split(" ")[0]) - parseFloat(a.split(" ")[0]));
const toParse = lines.slice(0, DEFAULT_MAX_RESULTS).map((line) => {
const toParse = lines.map((line) => {
// Format: "<mtime> <path>" — split on first space only
const firstSpace = line.indexOf(" ");
return firstSpace === -1 ? line : line.slice(firstSpace + 1);
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/tools_reflection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,10 @@ describe("reflection tool", () => {
assert.strictEqual(result[0].sessionId, "valid-json");
});

// --- mtime sorting: only parse top N newest files ---
// --- mtime sorting: process all sessions within the window ---

it("sorts by mtime and only parses the top 50 files", async () => {
// Create 55 sessions — only the 50 newest should be parsed
it("sorts by mtime and parses all files within the window", async () => {
// Create 55 sessions — all should be parsed since they're within the window
for (let i = 0; i < 55; i++) {
await writeSession(`session-${String(i).padStart(3, "0")}`, {
startedAt: new Date().toISOString(),
Expand All @@ -435,7 +435,7 @@ describe("reflection tool", () => {
}

const result = JSON.parse(await reflectionImpl({}, defaultOpts));
assert.ok(result.length <= 50, `Expected at most 50 results, got ${result.length}`);
assert.ok(result.length >= 55, `Expected at least 55 results, got ${result.length}`);
});
});

Expand Down