diff --git a/src/config.ts b/src/config.ts index f816378..38bb083 100644 --- a/src/config.ts +++ b/src/config.ts @@ -61,12 +61,11 @@ export function projectIdFromPath(folderPath: string): string { } return explicit; } - const normalized = path.resolve(folderPath); - let id = createHash("sha256").update(normalized).digest("hex").slice(0, 12); + let id = coreProjectId(folderPath); // Branch-aware mode: append sanitized branch name to isolate per-branch indexes if (process.env.SOCRATICODE_BRANCH_AWARE === "true") { - const branch = detectGitBranch(normalized); + const branch = detectGitBranch(path.resolve(folderPath)); if (branch) { id = `${id}__${sanitizeBranchName(branch)}`; } @@ -75,6 +74,16 @@ export function projectIdFromPath(folderPath: string): string { return id; } +/** + * Core project ID: SHA-256 hash of the resolved path, without branch suffix. + * Used internally by resolveLinkedCollections so linked projects always + * resolve to their base collection regardless of SOCRATICODE_BRANCH_AWARE. + */ +function coreProjectId(folderPath: string): string { + const normalized = path.resolve(folderPath); + return createHash("sha256").update(normalized).digest("hex").slice(0, 12); +} + /** * Derive a Qdrant collection name for a project's code chunks. */ @@ -170,9 +179,11 @@ export function resolveLinkedCollections( const linked = loadLinkedProjects(resolvedRoot); for (const linkedPath of linked) { - const linkedId = projectIdFromPath(linkedPath); - // Skip if same project ID (e.g. worktrees sharing SOCRATICODE_PROJECT_ID) - if (linkedId === currentId) continue; + // Use base hash (no branch suffix) — linked projects are resolved by their + // standard collection name regardless of SOCRATICODE_BRANCH_AWARE. + const linkedId = coreProjectId(linkedPath); + // Skip if same base project (e.g. worktrees sharing SOCRATICODE_PROJECT_ID) + if (collectionName(linkedId) === collections[0].name) continue; collections.push({ name: collectionName(linkedId), label: path.basename(linkedPath), diff --git a/tests/unit/config.test.ts b/tests/unit/config.test.ts index c26089e..4063f82 100644 --- a/tests/unit/config.test.ts +++ b/tests/unit/config.test.ts @@ -282,6 +282,29 @@ describe("config", () => { // Different collection names expect(collections[0].name).not.toBe(collections[1].name); }); + + it("linked collections use base hash without branch suffix when SOCRATICODE_BRANCH_AWARE is true", () => { + process.env.SOCRATICODE_BRANCH_AWARE = "true"; + fs.writeFileSync( + path.join(projectDir, ".socraticode.json"), + JSON.stringify({ linkedProjects: ["../linked-lib"] }), + ); + + // Get linked collection name with branch-aware on + const withBranch = resolveLinkedCollections(projectDir); + expect(withBranch).toHaveLength(2); + const linkedNameWithBranch = withBranch[1].name; + + // Get linked collection name with branch-aware off + delete process.env.SOCRATICODE_BRANCH_AWARE; + const withoutBranch = resolveLinkedCollections(projectDir); + const linkedNameWithoutBranch = withoutBranch[1].name; + + // Linked project collection name must be identical regardless of SOCRATICODE_BRANCH_AWARE + expect(linkedNameWithBranch).toBe(linkedNameWithoutBranch); + // And must NOT contain a branch suffix (double-underscore) + expect(linkedNameWithBranch).not.toContain("__"); + }); }); // ── Branch awareness ────────────────────────────────────────────────