Skip to content

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

test: cross-version regression, latency, and skew acceptance for scope-aware ranking

Shrish Dwivedi44eb9794500d
diff --git a/src/graph/workspace.ts b/src/graph/workspace.ts
index 55e3843a..69908b13 100644
--- a/src/graph/workspace.ts
+++ b/src/graph/workspace.ts
@@ -265,10 +265,17 @@ export function federateAsk(
// OR its overall coverage is broad enough to be real (≥ HIGH_FLOOR). A
// body-only collision has coverageStrong 0 and coverage ~0.3 → gated to
// alsoMatched. Both floors are absolute + scale-invariant (see their docs).
+ // The gate is a cross-child FAIRNESS mechanism: it stops a weak body-token
+ // collision in one repo from federating beside another repo's genuine hits.
+ // An explicit `--in <child>` removes all cross-child competition — the caller
+ // has already chosen the scope — so the gate must NOT apply there, or the
+ // `also matched: <child> — narrow with --in <child>` hint would lead to an
+ // empty result for exactly the weak child it points at. With `onlyChild` set,
+ // every run (there is at most one) survives, matching a standalone child ask.
const gatedOut: { scope: string; bestId: string }[] = [];
const survivors: ChildRun[] = [];
for (const run of runs) {
- if (run.coverageStrong >= STRONG_FLOOR || run.coverage >= HIGH_FLOOR) survivors.push(run);
+ if (onlyChild || run.coverageStrong >= STRONG_FLOOR || run.coverage >= HIGH_FLOOR) survivors.push(run);
else gatedOut.push({ scope: run.child, bestId: prefixPointer(run.child, run.hits[0].pointer) });
}
diff --git a/test/regression-single-repo.test.ts b/test/regression-single-repo.test.ts
new file mode 100644
index 00000000..27f78d84
--- /dev/null
+++ b/test/regression-single-repo.test.ts
@@ -0,0 +1,134 @@
+/**
+ * Task-7 permanent regression gate: the scope-aware-ranking work must be
+ * INVISIBLE on ordinary single-repo graphs. A repo with only root-level
+ * markers is a single scope, so every retrieval surface (ask / map / grep /
+ * callers) must (a) still return real results and (b) emit NO scope machinery
+ * — no `[scope/]` hit labels, no `matched in:` / `also matched:` footer, and
+ * no multi-scope `meta.scopes` / `.scopes` payload. This is the "no regression"
+ * half of the acceptance gate, frozen as a unit test.
+ */
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { buildGraph } from "../src/graph/build.js";
+import { readGraph, wiringPath } from "../src/graph/write.js";
+import { loadGraphCached } from "../src/graph/load.js";
+import { contextDirFor } from "../src/context/node-file.js";
+import { scopesOfGraph } from "../src/graph/scopes.js";
+import { ask, formatAsk, type AskResult } from "../src/ask/ask.js";
+import { buildRepoMap, formatRepoMap } from "../src/graph/map.js";
+import { grepGraph } from "../src/search/grep.js";
+import { resolveSymbol, callersOf } from "../src/graph/traverse.js";
+import type { GraphV1 } from "../src/graph/types.js";
+
+/** Materialize a fixture repo from a path→content map. */
+function fx(layout: Record<string, string>): string {
+ const dir = mkdtempSync(join(tmpdir(), "regress-single-"));
+ for (const [p, content] of Object.entries(layout)) {
+ mkdirSync(join(dir, p, ".."), { recursive: true });
+ writeFileSync(join(dir, p), content);
+ }
+ return dir;
+}
+
+/** Three single-repo fixtures, each with ONLY root-level project markers so
+ * scope discovery collapses to the canonical single (root) scope. */
+const FIXTURES: Record<string, Record<string, string>> = {
+ // TypeScript only.
+ "ts-only": {
+ "package.json": JSON.stringify({ name: "ts-only" }),
+ "src/server.ts":
+ "export function handleRequest(req: unknown) { return validateInput(req); }\n" +
+ "export function validateInput(x: unknown) { return x != null; }\n" +
+ "export function startServer() { return handleRequest({}); }\n",
+ "src/util.ts": "export function formatError(e: Error) { return e.message; }\n",
+ },
+ // Python only.
+ "py-only": {
+ "pyproject.toml": '[project]\nname = "py-only"\n',
+ "app.py":
+ "def handle_request(req):\n return validate_input(req)\n\n" +
+ "def validate_input(x):\n return x is not None\n\n" +
+ "def start_server():\n return handle_request({})\n",
+ "errors.py": "def format_error(e):\n return str(e)\n",
+ },
+ // Mixed languages, but still ONLY root-level markers -> one scope.
+ mixed: {
+ "package.json": JSON.stringify({ name: "mixed" }),
+ "pyproject.toml": '[project]\nname = "mixed"\n',
+ "gateway.ts":
+ "export function routeRequest(p: string) { return dispatch(p); }\n" +
+ "export function dispatch(p: string) { return p.length; }\n",
+ "worker.py":
+ "def process_job(job):\n return compute_result(job)\n\n" +
+ "def compute_result(job):\n return len(job)\n",
+ },
+};
+
+/** Text output must carry no scope machinery. */
+function assertScopeFreeText(label: string, text: string) {
+ assert.ok(text.length > 0, `${label}: expected non-empty output`);
+ assert.doesNotMatch(text, /\[[^\]\n]+\/\]\s/, `${label}: unexpected [scope/] label in text`);
+ assert.doesNotMatch(text, /^matched in:/m, `${label}: unexpected "matched in:" footer`);
+ assert.doesNotMatch(text, /^also matched:/m, `${label}: unexpected "also matched:" footer`);
+ assert.doesNotMatch(text, /scopes here:/, `${label}: unexpected "scopes here:" clause`);
+}
+
+for (const [name, layout] of Object.entries(FIXTURES)) {
+ test(`single-repo (${name}): retrieval works and stays scope-free`, async () => {
+ const dir = fx(layout);
+ try {
+ await buildGraph(dir);
+ const outDir = contextDirFor(dir);
+ const written = readGraph(wiringPath(outDir)) as GraphV1;
+ const graph = loadGraphCached(outDir) as GraphV1;
+ assert.ok(graph, `${name}: graph should load`);
+
+ // (a) NO multi-scope meta: a root-only-marker repo is a single scope.
+ const scopes = scopesOfGraph(written);
+ assert.equal(scopes.length, 1, `${name}: expected exactly one scope`);
+ assert.equal(scopes[0].prefix, "", `${name}: the single scope must be the root scope`);
+
+ // ---- ask ----
+ const askQueries = name === "py-only"
+ ? ["how is input validated", "handle request"]
+ : name === "mixed"
+ ? ["how are requests routed", "process the job"]
+ : ["how is input validated", "handle request"];
+ let anyAskHits = false;
+ for (const q of askQueries) {
+ const r: AskResult = ask(dir, q, { limit: 8 });
+ if (r.hits.length > 0) anyAskHits = true;
+ assert.equal(r.scopes, undefined, `${name}/ask "${q}": no scopes meta on single-repo`);
+ assertScopeFreeText(`${name}/ask "${q}"`, formatAsk(r));
+ }
+ assert.ok(anyAskHits, `${name}: ask returned hits for at least one query`);
+
+ // ---- map ----
+ const map = buildRepoMap(graph);
+ assert.equal(map.scopes, undefined, `${name}/map: no per-scope grouping on single-repo`);
+ assert.ok(map.dirs.length > 0, `${name}/map: expected directory entries`);
+ assertScopeFreeText(`${name}/map`, formatRepoMap(map));
+
+ // ---- grep ----
+ const grep = grepGraph(graph, dir, "return");
+ assert.ok(grep.groups.length > 0, `${name}/grep: expected matching groups`);
+ assert.ok(grep.totalHits > 0, `${name}/grep: expected hits`);
+
+ // ---- callers ----
+ const calleeName = name === "py-only"
+ ? "validate_input"
+ : name === "mixed"
+ ? "dispatch"
+ : "validateInput";
+ const targets = resolveSymbol(graph, calleeName);
+ assert.ok(targets.length > 0, `${name}/callers: symbol "${calleeName}" should resolve`);
+ const callers = callersOf(graph, targets[0]);
+ assert.ok(callers.length > 0, `${name}/callers: "${calleeName}" should have >=1 caller`);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+}
diff --git a/test/workspace.test.ts b/test/workspace.test.ts
index 527b7b3d..a1bad942 100644
--- a/test/workspace.test.ts
+++ b/test/workspace.test.ts
@@ -241,6 +241,26 @@ test("federated ask: --in <child> narrows to that child; --in <unknown> errors l
rmSync(p, { recursive: true, force: true });
});
+test("federated ask: explicit --in <weak child> bypasses the cross-child gate (footer hint is reachable)", async () => {
+ // repoJunk matches `position` only in a body token — gated OUT of the fused
+ // ranking and reported in alsoMatched with a `narrow with --in repoJunk` hint.
+ // Following that hint (--in repoJunk) MUST return repoJunk's hits, not empty:
+ // an explicit single-child scope has no cross-child fairness concern.
+ const p = workspaceFx({
+ repoStrong: { "m.ts": "export function scrollbarOverlay() { return computeThumb(); }\nfunction computeThumb() { return 1; }\n" },
+ repoJunk: { "m.ts": "export function drawFrame() { const position = 0; return position; }\nfunction helper() { return 1; }\n" },
+ });
+ await buildWorkspace(p);
+ // Unscoped: repoJunk is gated to alsoMatched (documents the hint the user follows).
+ const unscoped = federateAsk(p, undefined, "scrollbar overlay position", { limit: 8 });
+ assert.ok(unscoped.scopes?.alsoMatched.some((m) => m.scope === "repoJunk"), "junk reported in alsoMatched");
+ // Following the hint: --in repoJunk returns repoJunk's own (weak) hits.
+ const scoped = federateAsk(p, undefined, "scrollbar overlay position", { limit: 8, in: "repoJunk" });
+ assert.ok(scoped.hits.length > 0, "explicit --in on a gate-weak child must not return empty");
+ assert.ok(scoped.hits.every((h) => h.scope!.startsWith("repoJunk")), "--in repoJunk stays in repoJunk");
+ rmSync(p, { recursive: true, force: true });
+});
+
for (const padN of [0, 200]) {
test(`strength gate: body-only junk (\`position\`) gated out at ${padN ? "~200" : "~30"}-node scale`, async () => {
const strongPad = padN ? { "pad.ts": pad(padN) } : {};