Skip to content
Open
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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-07-01 - Avoid Array.find() inside chunked loops
**Learning:** In `ChatSessionManager.performAutoCleanup()`, mapping over chunks to archive sessions performs `sessions.find((s) => s.meta.id === sessionId)` inside the map callback. `sessions` is the full array of sessions. This results in O(M * N) complexity (where M is number of sessions to archive, N is total sessions). Worse, since it happens inside `Promise.all()`, it causes many redundant iterations. And in `evictExpiredEntries()` and `evictLruIfNeeded()` in `ContextService`, we already know about avoiding O(N^2) regressions by reusing entry objects obtained during initial iterations or just creating maps.
**Action:** When mapping array of IDs to full objects, always create a Map lookup beforehand (O(N)) or reuse objects from earlier filtering.
15 changes: 11 additions & 4 deletions src/core/session/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,22 @@ export class ChatSessionManager {
}

// Archive medium-priority sessions
const sessionMap = new Map(sessions.map((s) => [s.meta.id, s]));
for (let i = 0; i < analysis.sessionsToArchive.length; i += CHUNK_SIZE) {
const chunk = analysis.sessionsToArchive.slice(i, i + CHUNK_SIZE);
await Promise.all(
chunk.map(async (sessionId) => {
const session = sessions.find((s) => s.meta.id === sessionId);
const session = sessionMap.get(sessionId);
if (session) {
await this.archiveSession(session);
await this.deleteSession(sessionId);
archived++;
try {
await this.archiveSession(session);
await this.deleteSession(sessionId);
archived++;
} catch (err) {
getLogger().warn(
`[SessionManager] Failed to archive session ${sessionId}: ${err instanceof Error ? err.message : String(err)}`,
);
}
}
}),
);
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/prompt_templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ describe('Prompt templates', () => {

expect(planSystem).toContain('You are SalmonLoop.');
expect(patchSystem).toContain('You are PATCH, a phase-native diff compiler.');
expect(autopilotSystem).toContain('You are a senior software engineer running in "autopilot" mode.');
expect(autopilotSystem).toContain(
'You are a senior software engineer running in "autopilot" mode.',
);
expect(answerSystem).toContain('You are a coding assistant in "answer" mode.');
expect(researchSystem).toContain('You are a research assistant.');
});
Expand Down
Loading