Open-source SEO, GEO, and marketing skills for AI agents.
fix(projects): clean up PROJECT.md dir on delete + move it on rename
Yuting Zhongc24645bebab6
diff --git a/src/server/actions/projects.ts b/src/server/actions/projects.tsindex 42215ce8..be47f0c8 100644--- a/src/server/actions/projects.ts+++ b/src/server/actions/projects.ts@@ -343,6 +343,23 @@ export async function renameProjectFullAction(};}+ // 2.5) Move the canonical PROJECT.md directory from old slug → new slug+ // so the brief survives the rename. The per-agent sidecar copies+ // get rewritten on the next ensureProjectAgents pass (or+ // writeIdentityFile call); the canonical file is the source of+ // truth they read from, so moving this is what matters.+ try {+ const { renameProjectBriefDir } = await import(+ "@/server/onboarding/project-brief"+ );+ await renameProjectBriefDir(current.slug, newSlug);+ } catch (err) {+ console.warn(+ `[rename-project] failed to move PROJECT.md dir from ${current.slug} to ${newSlug}:`,+ err,+ );+ }+// 3) Repoint the active-project cookie if it was this one.const c = await cookies();if (c.get("notfair_active_project")?.value === current.slug) {@@ -462,10 +479,29 @@ export async function deleteProjectAction(// the same slug starts fresh (and we don't leak the old Promise).clearProvisioning(slug);- // 5) Local DB rows.+ // 5) Canonical PROJECT.md directory at ~/.notfair-cmo/projects/<slug>/.+ // The per-agent sidecar copies inside each workspace were already wiped+ // by cascadeDeleteAgent's `rm -rf` on the workspace dir; this is the+ // last surface that holds the project brief on disk. Without this,+ // recreating a project with the same slug later would silently inherit+ // the prior tenant's PROJECT.md (writeIdentityFile inlines it if it+ // exists). Best-effort — a missing dir is a no-op.+ try {+ const { deleteProjectBriefDir } = await import(+ "@/server/onboarding/project-brief"+ );+ await deleteProjectBriefDir(slug);+ } catch (err) {+ console.warn(+ `[delete-project] failed to remove PROJECT.md dir for ${slug}:`,+ err,+ );+ }++ // 6) Local DB rows.deleteProjectRow(slug);- // 6) Clear active-project cookie if it pointed at this one.+ // 7) Clear active-project cookie if it pointed at this one.const c = await cookies();if (c.get("notfair_active_project")?.value === slug) {await clearActiveProject();diff --git a/src/server/onboarding/project-brief.test.ts b/src/server/onboarding/project-brief.test.tsnew file mode 100644index 00000000..2bbd9c86--- /dev/null+++ b/src/server/onboarding/project-brief.test.ts@@ -0,0 +1,101 @@+import { mkdtempSync } from "node:fs";+import { mkdir, readFile, stat, writeFile } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { afterAll, beforeAll, describe, expect, it } from "vitest";++const tmpRoot = mkdtempSync(join(tmpdir(), "notfair-cmo-pb-"));+process.env.NOTFAIR_CMO_DATA_DIR = tmpRoot;++import {+ deleteProjectBriefDir,+ projectBriefDir,+ projectBriefPath,+ readProjectBrief,+ renameProjectBriefDir,+ writeProjectBrief,+} from "./project-brief";++describe("project-brief paths + writeProjectBrief + readProjectBrief", () => {+ it("writes + reads back at the canonical path", async () => {+ await writeProjectBrief("alpha", "# Alpha\n\nbody");+ const got = await readProjectBrief("alpha");+ expect(got).toBe("# Alpha\n\nbody");+ const on_disk = await readFile(projectBriefPath("alpha"), "utf8");+ expect(on_disk).toBe("# Alpha\n\nbody");+ });++ it("readProjectBrief returns null when missing", async () => {+ expect(await readProjectBrief("never-written")).toBeNull();+ });+});++describe("deleteProjectBriefDir", () => {+ it("removes the canonical dir + its file", async () => {+ await writeProjectBrief("doomed", "to be deleted");+ await deleteProjectBriefDir("doomed");+ expect(await readProjectBrief("doomed")).toBeNull();+ await expect(stat(projectBriefDir("doomed"))).rejects.toMatchObject({+ code: "ENOENT",+ });+ });++ it("is a no-op when the dir doesn't exist", async () => {+ await expect(+ deleteProjectBriefDir("nothing-here-either"),+ ).resolves.toBeUndefined();+ });+});++describe("renameProjectBriefDir", () => {+ it("moves the dir to the new slug", async () => {+ await writeProjectBrief("from-slug", "original body");+ await renameProjectBriefDir("from-slug", "to-slug");+ expect(await readProjectBrief("from-slug")).toBeNull();+ expect(await readProjectBrief("to-slug")).toBe("original body");+ });++ it("is a no-op when old == new", async () => {+ await writeProjectBrief("same-same", "body");+ await renameProjectBriefDir("same-same", "same-same");+ expect(await readProjectBrief("same-same")).toBe("body");+ });++ it("is a no-op when the source dir doesn't exist", async () => {+ await expect(+ renameProjectBriefDir("nonexistent-source", "any-dest"),+ ).resolves.toBeUndefined();+ expect(await readProjectBrief("any-dest")).toBeNull();+ });++ it("rejects when the destination already exists (slug collision)", async () => {+ // Both dirs exist — rename would clobber the destination silently+ // on POSIX, which is the kind of bug we want to surface upstream.+ await mkdir(projectBriefDir("clobber-src"), { recursive: true });+ await writeFile(+ projectBriefPath("clobber-src"),+ "from",+ "utf8",+ );+ await mkdir(projectBriefDir("clobber-dst"), { recursive: true });+ await writeFile(+ projectBriefPath("clobber-dst"),+ "to",+ "utf8",+ );+ // Node's rename() on POSIX *does* overwrite an empty dir, so this+ // catches the more dangerous "dest has content" case where rename+ // would fail with ENOTEMPTY. The point is the caller learns about+ // it instead of silently losing data.+ await expect(+ renameProjectBriefDir("clobber-src", "clobber-dst"),+ ).rejects.toMatchObject({ code: expect.stringMatching(/ENOTEMPTY|EEXIST/) });+ });+});++afterAll(async () => {+ void tmpRoot; // keep tmpdir alive across tests; vitest will tear down the process+});+beforeAll(() => {+ // no-op; the env was set at import time so the module captured the dir+});diff --git a/src/server/onboarding/project-brief.ts b/src/server/onboarding/project-brief.tsindex f5930f9c..73fa2891 100644--- a/src/server/onboarding/project-brief.ts+++ b/src/server/onboarding/project-brief.ts@@ -1,4 +1,4 @@-import { mkdir, readFile, writeFile } from "node:fs/promises";+import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";import { homedir } from "node:os";import { dirname, join } from "node:path";@@ -26,8 +26,12 @@ function dataDir(): string {return process.env.NOTFAIR_CMO_DATA_DIR ?? join(homedir(), ".notfair-cmo");}+export function projectBriefDir(project_slug: string): string {+ return join(dataDir(), "projects", project_slug);+}+export function projectBriefPath(project_slug: string): string {- return join(dataDir(), "projects", project_slug, "PROJECT.md");+ return join(projectBriefDir(project_slug), "PROJECT.md");}/**@@ -59,3 +63,42 @@ export async function writeProjectBrief(await mkdir(dirname(path), { recursive: true });await writeFile(path, body, "utf8");}++/**+ * Remove the canonical PROJECT.md directory for a project — called from+ * the project-delete cascade so we don't orphan briefs on disk. Without+ * this, recreating a project with the same slug would inherit the prior+ * tenant's brief (writeIdentityFile reads PROJECT.md if it exists).+ *+ * Best-effort: missing dir is a no-op, other fs errors propagate so the+ * caller can decide whether to surface them.+ */+export async function deleteProjectBriefDir(+ project_slug: string,+): Promise<void> {+ await rm(projectBriefDir(project_slug), { recursive: true, force: true });+}++/**+ * Move the canonical PROJECT.md directory when a project's slug changes.+ * Called by the rename cascade in changeProjectSlug. No-op when the+ * source dir doesn't exist (project never had a brief written). Throws+ * if the destination already exists — that would be a slug-collision+ * higher up the call chain that we should not silently paper over.+ */+export async function renameProjectBriefDir(+ old_slug: string,+ new_slug: string,+): Promise<void> {+ if (old_slug === new_slug) return;+ const from = projectBriefDir(old_slug);+ try {+ await stat(from);+ } catch (err) {+ if ((err as NodeJS.ErrnoException).code === "ENOENT") return;+ throw err;+ }+ const to = projectBriefDir(new_slug);+ await mkdir(dirname(to), { recursive: true });+ await rename(from, to);+}