Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

fix(search): honor ascending order in by-file responses (#6757)

LifeByTechAI78bc76f6c0a0
diff --git a/src/services/worker/http/routes/SearchRoutes.ts b/src/services/worker/http/routes/SearchRoutes.ts
index d66582454..b238822e8 100644
--- a/src/services/worker/http/routes/SearchRoutes.ts
+++ b/src/services/worker/http/routes/SearchRoutes.ts
@@ -327,8 +327,9 @@ export class SearchRoutes extends BaseRouteHandler {
}))
];
- combined.sort((a, b) => b.epoch - a.epoch);
- const resultsByDate = groupByDate(combined, item => item.created_at, { order: 'desc' });
+ const order = query.orderBy === 'date_asc' ? 'asc' : 'desc';
+ combined.sort((a, b) => order === 'asc' ? a.epoch - b.epoch : b.epoch - a.epoch);
+ const resultsByDate = groupByDate(combined, item => item.created_at, { order });
const lines: string[] = [];
lines.push(`Found ${totalResults} result(s) for file "${filePath}"`);
diff --git a/tests/worker/http/routes/search-by-file-projects.test.ts b/tests/worker/http/routes/search-by-file-projects.test.ts
index 16650b68e..10ce88d91 100644
--- a/tests/worker/http/routes/search-by-file-projects.test.ts
+++ b/tests/worker/http/routes/search-by-file-projects.test.ts
@@ -17,7 +17,7 @@ type Handler = (req: Request, res: Response) => void;
let store: SessionStore;
let search: SessionSearch;
-function seedObservation(memorySessionId: string, project: string, title: string, mergedInto: string | null = null): void {
+function seedObservation(memorySessionId: string, project: string, title: string, mergedInto: string | null = null, epoch?: number): void {
const sessionDbId = store.createSDKSession(`content-${memorySessionId}`, project, `prompt about ${title}`);
store.ensureMemorySessionIdRegistered(sessionDbId, memorySessionId);
store.storeObservation(memorySessionId, project, {
@@ -29,7 +29,7 @@ function seedObservation(memorySessionId: string, project: string, title: string
concepts: [],
files_read: ['src/app.ts'],
files_modified: [],
- }, 1);
+ }, 1, 0, epoch);
if (mergedInto) {
store.db.prepare('UPDATE observations SET merged_into_project = ? WHERE memory_session_id = ?').run(mergedInto, memorySessionId);
}
@@ -157,3 +157,28 @@ describe('GET /api/search/by-file folder flag', () => {
});
}
});
+
+describe('GET /api/search/by-file date order', () => {
+ it.each(['date_asc', 'date_desc', undefined])('orders dates and rows for orderBy=%s', async (orderBy) => {
+ const project = 'ordered-project';
+ const firstDay = Date.UTC(2026, 0, 1, 12);
+ seedObservation('mem-first', project, 'First', null, firstDay);
+ seedObservation('mem-last', project, 'Last', null, Date.UTC(2026, 0, 2, 12));
+ store.storeSummary('mem-first', project, {
+ request: 'Middle',
+ investigated: '',
+ learned: '',
+ completed: '',
+ next_steps: '',
+ notes: null,
+ files_read: ['src/app.ts'],
+ }, 1, 0, firstDay + 1);
+
+ const query = { filePath: 'src/app.ts', project, ...(orderBy === undefined ? {} : { orderBy }) };
+ const { status, body } = await requestByFile(byFileHandler(null), query);
+ expect(status).toBe(200);
+ const text: string = body.content[0].text;
+ const ascending = ['OBS First', 'SESSION Middle', 'OBS Last'];
+ expect(text.match(/^(?:OBS|SESSION) .+$/gm)).toEqual(orderBy === 'date_asc' ? ascending : ascending.reverse());
+ });
+});