Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

fix(graph): scope collapse is layout-deterministic; literal workspace entries resolve

Shrish Dwivedi410d2709721c
diff --git a/src/graph/scopes.ts b/src/graph/scopes.ts
index 32b2e980..b04773fc 100644
--- a/src/graph/scopes.ts
+++ b/src/graph/scopes.ts
@@ -21,7 +21,7 @@
* 7. `label` = `prefix` (both "" for root); ordering is prefix-length desc,
* then lexicographic.
*/
-import { existsSync, readdirSync, readFileSync, type Dirent } from "node:fs";
+import { existsSync, readdirSync, readFileSync, statSync, type Dirent } from "node:fs";
import { join, resolve } from "node:path";
import { SKIP_DIRS } from "../ingest/fs.js";
import type { GraphV1, ScopeV1 } from "./types.js";
@@ -118,6 +118,20 @@ function resolveGlob(root: string, pattern: string): string[] {
} else if (norm.endsWith("/*")) {
base = norm.slice(0, -2);
recursive = false;
+ } else if (!norm.includes("*")) {
+ // Literal entry (no glob chars), e.g. `packages: ['apps/*', 'docs']` — a plain
+ // workspace-list entry that names a real directory counts as a workspace match
+ // in its own right, same as a resolved glob would. Without this, a literal
+ // entry silently resolves to nothing while Rule 2 still strips its
+ // `package.json` marker (it's a JS-family dir that never matched the glob
+ // set), de-scoping a real workspace package.
+ try {
+ return existsSync(join(root, norm)) && statSync(join(root, norm)).isDirectory()
+ ? [norm]
+ : [];
+ } catch {
+ return [];
+ }
} else {
return []; // unsupported glob form
}
@@ -202,23 +216,65 @@ export function discoverScopes(root: string): ScopeV1[] {
else candidates.set(dir, { markers: markerMap.get(dir) ?? [], isWorkspace: true });
}
- // Rule 4: nesting collapse.
- for (const prefix of [...candidates.keys()]) {
- if (prefix === "" || !candidates.has(prefix)) continue;
- const entry = candidates.get(prefix)!;
+ // Rule 4: nesting collapse — evaluated in two ordered passes against FROZEN
+ // snapshots (deletions are marked first, then applied), never against the
+ // live `candidates` map mid-iteration. That's what makes the outcome
+ // layout-determined instead of `readdirSync`/Map-iteration-order-determined:
+ // deciding "does ancestor X still exist" by checking a map you're
+ // simultaneously deleting from means the answer depends on which candidate
+ // happens to be visited first, which depends on directory read order.
+ //
+ // Pass A — workspace override (uses the ORIGINAL frozen candidate set):
+ // for every workspace-glob candidate, find its nearest candidate
+ // ancestor in the original set; if that ancestor is not itself a
+ // workspace match, queue it for deletion ("a workspace-glob match wins
+ // over its parent"). All Pass-A deletions are collected first, then
+ // applied together — so it doesn't matter which workspace candidate is
+ // considered first, the ancestor lookups all read the same frozen set.
+ // Applying them produces the "post-glob-resolution" stable set.
+ //
+ // Pass B — plain nesting collapse (uses the POST-A set, NOT the original
+ // frozen set): for every remaining NON-workspace candidate, find its
+ // nearest ancestor in the post-A set; if one exists, queue the deeper
+ // candidate for deletion (keep the shallower ancestor). Workspace
+ // candidates are never deletion targets here — Pass A already resolved
+ // a workspace match's relationship to its blocking parent, so it always
+ // survives regardless of any remaining (grand)ancestor.
+ //
+ // Running Pass B against the post-A set (rather than the frozen one) is
+ // what makes a Pass-A deletion visible downstream: a candidate whose only
+ // nesting ancestor was itself removed by the workspace override is no
+ // longer "nested" and survives — regardless of whether that candidate or
+ // the workspace match happened to be discovered first on disk.
+ const findNearestAncestorCandidate = (
+ prefix: string,
+ set: ReadonlyMap<string, Candidate>,
+ ): string | null => {
const segs = prefix.split("/");
for (let i = segs.length - 1; i >= 1; i--) {
const ancestor = segs.slice(0, i).join("/");
- const ancestorEntry = candidates.get(ancestor);
- if (!ancestorEntry) continue;
- if (entry.isWorkspace && !ancestorEntry.isWorkspace) {
- candidates.delete(ancestor); // workspace-glob match wins over its parent
- } else {
- candidates.delete(prefix); // keep the shallower ancestor
- }
- break; // only the nearest ancestor candidate matters
+ if (set.has(ancestor)) return ancestor;
}
+ return null;
+ };
+
+ const frozen: ReadonlyMap<string, Candidate> = new Map(candidates);
+
+ const passADeletes = new Set<string>();
+ for (const [prefix, entry] of frozen) {
+ if (!entry.isWorkspace) continue;
+ const ancestor = findNearestAncestorCandidate(prefix, frozen);
+ if (ancestor && !frozen.get(ancestor)!.isWorkspace) passADeletes.add(ancestor);
+ }
+ for (const prefix of passADeletes) candidates.delete(prefix);
+
+ const postA: ReadonlyMap<string, Candidate> = new Map(candidates);
+ const passBDeletes = new Set<string>();
+ for (const [prefix, entry] of postA) {
+ if (prefix === "" || entry.isWorkspace) continue;
+ if (findNearestAncestorCandidate(prefix, postA)) passBDeletes.add(prefix);
}
+ for (const prefix of passBDeletes) candidates.delete(prefix);
const survivors = [...candidates.entries()].map(([prefix, c]) => ({ prefix, ...c }));
diff --git a/test/graph-scopes.test.ts b/test/graph-scopes.test.ts
index 0558d048..3cc3139a 100644
--- a/test/graph-scopes.test.ts
+++ b/test/graph-scopes.test.ts
@@ -66,6 +66,50 @@ test("nesting collapse keeps shallower candidate", () => {
rmSync(d, { recursive: true, force: true });
});
+test("nesting collapse is layout-deterministic regardless of sibling readdir order (P10)", () => {
+ // pnpm-workspace.yaml: packages: ['a/b/*'] + a/go.mod (candidate) + a/b/w/package.json
+ // (workspace match, wins over its parent "a") + a sibling of "b" under "a" that is
+ // ALSO a bare marker candidate. Whether the sibling is named "c" or "aa" changes
+ // readdirSync's return order for "a"'s children — that must not change which
+ // candidates survive Rule 4's nesting collapse.
+ const run = (siblingName: string): string[] => {
+ const d = fx({
+ "pnpm-workspace.yaml": "packages:\n - 'a/b/*'\n",
+ "a/go.mod": "module a\n",
+ "a/b/w/package.json": "{}",
+ [`a/${siblingName}/go.mod`]: `module ${siblingName}\n`,
+ });
+ try {
+ return discoverScopes(d)
+ .map((s) => s.prefix)
+ .sort();
+ } finally {
+ rmSync(d, { recursive: true, force: true });
+ }
+ };
+ // Normalize the sibling's own name away so the two runs compare structurally:
+ // both must resolve to "the sibling scope" + the workspace-glob scope, with the
+ // collapsed parent "a" gone in both cases.
+ const normalize = (prefixes: string[], siblingName: string): string[] =>
+ prefixes.map((p) => (p === `a/${siblingName}` ? "a/<sibling>" : p)).sort();
+
+ const withC = run("c");
+ const withAa = run("aa");
+ assert.deepEqual(normalize(withC, "c"), normalize(withAa, "aa"));
+ assert.deepEqual(normalize(withC, "c"), ["a/<sibling>", "a/b/w"]);
+});
+
+test("literal (non-glob) workspace entry resolves as a workspace match", () => {
+ const d = fx({
+ "package.json": JSON.stringify({ workspaces: ["apps/*", "docs"] }),
+ "apps/web/package.json": "{}", "apps/web/i.ts": "1",
+ "docs/package.json": "{}", "docs/index.ts": "1",
+ });
+ const prefixes = discoverScopes(d).map((s) => s.prefix).sort();
+ assert.deepEqual(prefixes, ["apps/web", "docs"]);
+ rmSync(d, { recursive: true, force: true });
+});
+
test("discoverWorkspaceChildren finds immediate git children only", () => {
const d = fx({ "repoA/x.ts": "1", "repoB/y.py": "1", "plain/z.go": "1" });
mkdirSync(join(d, "repoA/.git"), { recursive: true });