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
Expand Up @@ -4,3 +4,6 @@
## 2026-05-16 - [Batch FeathersJS user fetches with $in operator to fix N+1 query]
**Learning:** FeathersJS allows passing `$in` clauses through the query parameter (e.g. `user_id: { $in: ownerIds }`). When writing custom Feathers service logic, you can easily parse this array and pass it to Drizzle's `inArray()` to perform a batched query, instead of looping over `service.get(id)` causing N+1 database roundtrips.
**Action:** When implementing or updating custom Feathers `find()` methods, extract and parse the `$in` parameters to support batched Drizzle `inArray()` lookups, and always replace `Promise.all(ids.map(id => service.get(id)))` with a single batched `find()` call.
## 2025-02-12 - N+1 Disk I/O from loadConfig inside loops
**Learning:** `loadConfig()` reads and parses the YAML file from disk synchronously/asynchronously on every call. If called within an array `.map()` or `Promise.all` loop when creating multiple records (like worktrees), it creates an O(N) disk I/O bottleneck.
**Action:** When handling array iterations or bulk insertions that depend on config, hoist the `loadConfig()` call outside the loop to execute exactly once, and pass the loaded config object into the mapping function.
17 changes: 11 additions & 6 deletions apps/agor-daemon/src/services/worktrees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ export class WorktreesService extends DrizzleService<Worktree, Partial<Worktree>
* so admins can set org-wide defaults in config.yaml. Explicit values on the
* input always win; defaults fill in only when the caller omits the field.
*/
private async applyWorktreeCreateDefaults(data: Partial<Worktree>): Promise<Partial<Worktree>> {
const config = await loadConfig();
private applyWorktreeCreateDefaults(
data: Partial<Worktree>,
config: Awaited<ReturnType<typeof loadConfig>>
): Partial<Worktree> {
const defaults = config.worktrees;
if (!defaults) return data;

Expand All @@ -251,13 +253,16 @@ export class WorktreesService extends DrizzleService<Worktree, Partial<Worktree>
data: Partial<Worktree> | Partial<Worktree>[],
params?: WorktreeParams
): Promise<Worktree | Worktree[]> {
// ⚡ Bolt Performance Optimization:
// Load config once outside the mapping operation to prevent O(N) disk I/O
// when creating multiple worktrees in bulk.
const config = await loadConfig();

if (Array.isArray(data)) {
const withDefaults = await Promise.all(
data.map((item) => this.applyWorktreeCreateDefaults(item))
);
const withDefaults = data.map((item) => this.applyWorktreeCreateDefaults(item, config));
return super.create(withDefaults, params) as Promise<Worktree[]>;
}
const withDefaults = await this.applyWorktreeCreateDefaults(data);
const withDefaults = this.applyWorktreeCreateDefaults(data, config);
return super.create(withDefaults, params) as Promise<Worktree>;
}

Expand Down