Skip to content

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

fix(workspace): coverageStrong is name-only — coincidental path names no longer float unrelated repos

Shrish Dwivedid20e0f123a8a
diff --git a/src/ask/ask.ts b/src/ask/ask.ts
index 5eacba8c..65cf4eb8 100644
--- a/src/ask/ask.ts
+++ b/src/ask/ask.ts
@@ -336,6 +336,26 @@ function hasTerm(f: Map<string, number>, t: string): boolean {
* "write": common → near-weightless even when they collide with a symbol name);
* a task prompt matches exactly its rare, discriminating identifiers. Terms the
* corpus has never seen take `dfltIdf` (the df=0 weight). */
+/** The match-STRENGTH share behind `coverageStrong`: idf-weighted share of the
+ * query matched in the node's NAME field ONLY. The PATH field is deliberately
+ * excluded — a coincidental generic directory (`gateway/`) or file basename
+ * (`gateway.ts`) in an unrelated repo would otherwise clear the strength gate
+ * and float that repo to the top; an on-topic path-organized repo is still
+ * rescued by the broad-coverage HIGH_FLOOR clause instead. For the same reason
+ * a `kind:"file"` node contributes zero strength: its `name` is a basename (a
+ * path component), not a symbol name, so a file whose basename happens to share
+ * a query word is not a strong match. */
+function strongShare(
+ q: Map<string, number>,
+ node: NodeV1,
+ doc: { name: Map<string, number> },
+ idf: Map<string, number>,
+ dfltIdf: number,
+): number {
+ if (node.kind === "file") return 0;
+ return matchedIdfShare(q, [doc.name], idf, dfltIdf);
+}
+
function matchedIdfShare(
q: Map<string, number>,
fields: Array<Map<string, number>>,
@@ -550,7 +570,7 @@ function lexical(query: string, corpus: Corpus, limit: number, graphRank: boolea
const d = docsById.get(rd.id);
const si = idfOf.get(rd.scope);
matchedOf.set(hit, d && si ? matchedIdfShare(q, [d.name, d.path, d.body], si.idf, si.dflt) : 0);
- matchedStrongOf.set(hit, d && si ? matchedIdfShare(q, [d.name, d.path], si.idf, si.dflt) : 0);
+ matchedStrongOf.set(hit, d && si ? strongShare(q, n, d, si.idf, si.dflt) : 0);
symbolHits.push(hit);
}
// Label + footer only when federation actually happened (or a scope was
@@ -614,7 +634,7 @@ function lexical(query: string, corpus: Corpus, limit: number, graphRank: boolea
};
const d = docsById.get(id);
matchedOf.set(hit, d ? matchedIdfShare(q, [d.name, d.path, d.body], idf, dfltIdf) : 0);
- matchedStrongOf.set(hit, d ? matchedIdfShare(q, [d.name, d.path], idf, dfltIdf) : 0);
+ matchedStrongOf.set(hit, d ? strongShare(q, n, d, idf, dfltIdf) : 0);
symbolHits.push(hit);
}
}
diff --git a/test/workspace.test.ts b/test/workspace.test.ts
index 34b1247a..527b7b3d 100644
--- a/test/workspace.test.ts
+++ b/test/workspace.test.ts
@@ -28,7 +28,9 @@ function workspaceFx(children: Record<string, Record<string, string>>): string {
for (const [child, files] of Object.entries(children)) {
mkdirSync(join(parent, child, ".git"), { recursive: true });
for (const [name, content] of Object.entries(files)) {
- writeFileSync(join(parent, child, name), content);
+ const path = join(parent, child, name);
+ mkdirSync(join(path, ".."), { recursive: true }); // create nested dirs (e.g. src/gateway.ts)
+ writeFileSync(path, content);
}
}
return parent;
@@ -285,6 +287,24 @@ test("strength gate: a legit COMMON-term (low-idf) name match is NOT overcorrect
rmSync(p, { recursive: true, force: true });
});
+for (const [kind, junkFiles] of [
+ ["file basename gateway.ts", { "src/gateway.ts": "export function renderList() { return 0; }\n" }],
+ ["dir segment gateway/", { "gateway/handler.ts": "export function renderList() { return 0; }\n" }],
+] as const) {
+ test(`strength gate: junk matching only a ${kind} (no symbol named for the term) is gated out`, async () => {
+ const p = workspaceFx({
+ repoStrong: { "m.ts": "export function paymentGatewayRefund() { return 1; }\n", "pad.ts": pad(25) },
+ repoJunk: { ...junkFiles, "pad.ts": pad(25) },
+ });
+ await buildWorkspace(p);
+ const r = federateAsk(p, undefined, "payment gateway refund", { limit: 8 });
+ assert.ok(r.hits[0]?.title.startsWith("paymentGatewayRefund"), `strong repo's real hit must be #1, got: ${r.hits[0]?.title}`);
+ assert.ok(!r.hits.some((h) => h.scope!.startsWith("repoJunk")), "coincidental path-name junk must NOT federate");
+ assert.ok(r.scopes?.alsoMatched.some((m) => m.scope === "repoJunk"), "junk reported in alsoMatched");
+ rmSync(p, { recursive: true, force: true });
+ });
+}
+
test("readWorkspace: rejects foreign/invalid json as not-a-workspace", () => {
const p = mkdtempSync(join(tmpdir(), "ws-"));
mkdirSync(contextDirFor(p), { recursive: true });