-
-
Notifications
You must be signed in to change notification settings - Fork 581
fix(workspace): cap instruction discovery during open #378
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
Draft
chusia06
wants to merge
1
commit into
Waishnav:main
Choose a base branch
from
chusia06:codex/bound-workspace-instruction-discovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−48
Draft
Changes from all commits
Commits
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
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
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,58 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import test, { type TestContext } from "node:test"; | ||
| import { walkWorkspaceInstructions } from "./workspace-instruction-discovery.js"; | ||
|
|
||
| const limits = { maxDirectories: 10, maxEntries: 100, maxDurationMs: 60_000 }; | ||
|
|
||
| test("instruction discovery completes small trees and skips dependency directories", async (t) => { | ||
| const root = await fixture(t); | ||
| await mkdir(join(root, "nested")); | ||
| await mkdir(join(root, "node_modules", "package"), { recursive: true }); | ||
| await writeFile(join(root, "nested", "AGENTS.md"), "nested instructions"); | ||
| await writeFile(join(root, "node_modules", "package", "AGENTS.md"), "dependency instructions"); | ||
| const files: string[] = []; | ||
| assert.equal(await walkWorkspaceInstructions(root, (path) => { files.push(path); }, limits), true); | ||
| assert.deepEqual(files, [join(root, "nested", "AGENTS.md")]); | ||
| }); | ||
|
|
||
| test("directory budget stops the whole walk, including sibling subtrees", async (t) => { | ||
| const root = await fixture(t); | ||
| for (const name of ["a", "b", "c"]) { | ||
| await mkdir(join(root, name)); | ||
| await writeFile(join(root, name, "AGENTS.md"), "instructions"); | ||
| } | ||
| const files: string[] = []; | ||
| assert.equal(await walkWorkspaceInstructions(root, (path) => { files.push(path); }, { | ||
| ...limits, maxDirectories: 2, | ||
| }), false); | ||
| assert.equal(files.length, 1); | ||
| }); | ||
|
|
||
| test("entry budget also bounds a flat directory with no subdirectories", async (t) => { | ||
| const root = await fixture(t); | ||
| for (const name of ["a.txt", "b.txt", "c.txt"]) await writeFile(join(root, name), ""); | ||
| let visited = 0; | ||
| assert.equal(await walkWorkspaceInstructions(root, () => { visited++; }, { | ||
| ...limits, maxEntries: 2, | ||
| }), false); | ||
| assert.equal(visited, 2); | ||
| }); | ||
|
|
||
| test("expired discovery time budget does not start another directory", async (t) => { | ||
| const root = await fixture(t); | ||
| await writeFile(join(root, "AGENTS.md"), "instructions"); | ||
| let visited = 0; | ||
| assert.equal(await walkWorkspaceInstructions(root, () => { visited++; }, { | ||
| ...limits, maxDurationMs: 0, | ||
| }), false); | ||
| assert.equal(visited, 0); | ||
| }); | ||
|
|
||
| async function fixture(t: TestContext): Promise<string> { | ||
| const root = await mkdtemp(join(tmpdir(), "devspace-instruction-discovery-")); | ||
| t.after(() => rm(root, { recursive: true, force: true })); | ||
| return root; | ||
| } |
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,55 @@ | ||
| import { opendir } from "node:fs/promises"; | ||
| import { join } from "node:path"; | ||
| import { performance } from "node:perf_hooks"; | ||
|
|
||
| const SKIPPED_CONTEXT_DIRS = new Set([ | ||
| ".git", ".hg", ".svn", ".devspace", "node_modules", "dist", "build", | ||
| ".next", ".turbo", ".cache", | ||
| ]); | ||
|
|
||
| interface DiscoveryLimits { | ||
| maxDirectories: number; | ||
| maxEntries: number; | ||
| maxDurationMs: number; | ||
| } | ||
|
|
||
| /** Return false when discovery stops early; close directory handles on exit. */ | ||
| export async function walkWorkspaceInstructions( | ||
| root: string, | ||
| visit: (path: string, name: string) => Promise<void> | void, | ||
| limits: DiscoveryLimits = { | ||
| maxDirectories: 256, | ||
| maxEntries: 10_000, | ||
| maxDurationMs: 1_000, | ||
| }, | ||
| ): Promise<boolean> { | ||
| const deadline = performance.now() + limits.maxDurationMs; | ||
| let directories = 0; | ||
| let entriesVisited = 0; | ||
|
|
||
| async function walk(directory: string): Promise<boolean> { | ||
| if (directories >= limits.maxDirectories || performance.now() >= deadline) return false; | ||
| directories++; | ||
| let entries; | ||
| try { | ||
| entries = await opendir(directory); | ||
| } catch { | ||
| // Preserve discovery's existing handling of inaccessible directories. | ||
| return true; | ||
| } | ||
|
|
||
| for await (const entry of entries) { | ||
| if (entriesVisited >= limits.maxEntries || performance.now() >= deadline) return false; | ||
| entriesVisited++; | ||
| const path = join(directory, entry.name); | ||
| if (entry.isDirectory()) { | ||
| if (!SKIPPED_CONTEXT_DIRS.has(entry.name) && !await walk(path)) return false; | ||
| } else if (entry.isFile()) { | ||
| await visit(path, entry.name); | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| return walk(root); | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a nested directory cannot be opened, the walk returns
truewithout checking it for instruction files. The new completeness flag therefore remains false, andopen_workspacegives no incomplete-inventory warning. Users may treat the omitted instructions as a complete list. This warning gap does not block merging, but access failures should be reported as incomplete scans rather than as scan-limit stops.Artifacts
Focused denied-directory service check source
Service output before the incomplete-inventory signal
Service output with the incomplete-inventory signal