-
Notifications
You must be signed in to change notification settings - Fork 390
perf(agents): parallel charter discovery with bounded concurrency #1113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bradygaster
merged 2 commits into
bradygaster:dev
from
spboyer:perf/charter-discovery-parallel
May 14, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@bradygaster/squad-sdk": patch | ||
| --- | ||
|
|
||
| perf(agents): parallelize charter discovery with concurrency limit to reduce multi-agent load time |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /** | ||
| * Bounded-concurrency helper for fan-out async work. | ||
| * | ||
| * @module utils/map-with-limit | ||
| */ | ||
|
|
||
| /** | ||
| * Run `fn` against each item with at most `limit` operations in flight. | ||
| * | ||
| * Results are returned in **input order**, regardless of the order in which | ||
| * individual promises settle. This matches the semantics callers usually | ||
| * want when migrating from a sequential `for (const x of xs) { result.push(await fn(x)); }` | ||
| * pattern: ordering is preserved, but throughput is bounded. | ||
| * | ||
| * Errors propagate via the returned Promise. Use `mapWithLimitSettled()` | ||
| * when individual failures should not abort the batch. | ||
| * | ||
| * @example | ||
| * // 8 charters fetched 5-at-a-time over HTTP: | ||
| * const manifests = await mapWithLimit(dirs, 5, (dir) => fetchCharter(dir)); | ||
| * | ||
| * @param items Inputs to map over. | ||
| * @param limit Maximum concurrent calls (must be ≥ 1). | ||
| * @param fn Async mapper. | ||
| * @returns Array of results in the same order as `items`. | ||
| */ | ||
| export async function mapWithLimit<T, R>( | ||
| items: readonly T[], | ||
| limit: number, | ||
| fn: (item: T, index: number) => Promise<R>, | ||
| ): Promise<R[]> { | ||
| if (limit < 1 || !Number.isFinite(limit)) { | ||
| throw new Error(`mapWithLimit: limit must be a positive integer, got ${limit}`); | ||
| } | ||
| if (items.length === 0) return []; | ||
|
|
||
| const results = new Array<R>(items.length); | ||
| let nextIndex = 0; | ||
| const workerCount = Math.min(limit, items.length); | ||
|
|
||
| async function worker(): Promise<void> { | ||
| while (true) { | ||
| const idx = nextIndex++; | ||
| if (idx >= items.length) return; | ||
| results[idx] = await fn(items[idx]!, idx); | ||
| } | ||
| } | ||
|
|
||
| await Promise.all(Array.from({ length: workerCount }, () => worker())); | ||
| return results; | ||
| } | ||
|
|
||
| /** | ||
| * Variant of {@link mapWithLimit} that captures individual failures rather | ||
| * than aborting on the first rejection. Returns an array of | ||
| * `{ status: 'fulfilled', value }` / `{ status: 'rejected', reason }` in | ||
| * input order — identical shape to `Promise.allSettled`. | ||
| * | ||
| * Use this when one bad input (e.g. a corrupt charter.md) should not stop | ||
| * the whole batch. | ||
| */ | ||
| export async function mapWithLimitSettled<T, R>( | ||
| items: readonly T[], | ||
| limit: number, | ||
| fn: (item: T, index: number) => Promise<R>, | ||
| ): Promise<Array<PromiseSettledResult<R>>> { | ||
| if (limit < 1 || !Number.isFinite(limit)) { | ||
| throw new Error(`mapWithLimitSettled: limit must be a positive integer, got ${limit}`); | ||
| } | ||
| if (items.length === 0) return []; | ||
|
|
||
| const results = new Array<PromiseSettledResult<R>>(items.length); | ||
| let nextIndex = 0; | ||
| const workerCount = Math.min(limit, items.length); | ||
|
|
||
| async function worker(): Promise<void> { | ||
| while (true) { | ||
| const idx = nextIndex++; | ||
| if (idx >= items.length) return; | ||
| try { | ||
| const value = await fn(items[idx]!, idx); | ||
| results[idx] = { status: 'fulfilled', value }; | ||
| } catch (reason) { | ||
| results[idx] = { status: 'rejected', reason }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| await Promise.all(Array.from({ length: workerCount }, () => worker())); | ||
| return results; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.