Skip to content

Commit 5aef758

Browse files
committed
fix(api): point fresh installs at project setup when resolve finds an empty projects table
On a brand-new config dir, model_post_init bootstraps a 'main' default in config.json but the one-shot CLI path never runs the server-lifespan reconciliation that would create its database row, so the first read fails with a bare "Project not found: 'main'" — which reads as a broken install rather than a missing first-run step. When resolution misses and the projects table is empty, the 404 now names the setup command. Follow-up to #974/#985/#987 (which repair the default during project add but cannot help when the first action is a read). Refs #974. Signed-off-by: phernandez <paul@basicmachines.co>
1 parent 32a1c20 commit 5aef758

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/basic_memory/api/v2/routers/project_router.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,20 @@ async def resolve_project_identifier(
320320
)
321321

322322
if not project:
323-
raise HTTPException(status_code=404, detail=f"Project not found: '{data.identifier}'")
323+
detail = f"Project not found: '{data.identifier}'"
324+
# Trigger: resolution missed and the projects table is empty.
325+
# Why: a fresh install bootstraps config.json's default project before any
326+
# reconciliation has created database rows (the one-shot CLI never runs
327+
# the server lifespan), so the first read fails on the configured
328+
# default and the bare not-found message reads as a broken install
329+
# rather than a missing first-run step (#974 follow-up).
330+
# Outcome: the error names the setup command instead.
331+
if not await project_repository.find_all(limit=1, use_load_options=False):
332+
detail = (
333+
f"{detail}. No projects are set up yet — run "
334+
"'basic-memory project add <name> <path>' to create one."
335+
)
336+
raise HTTPException(status_code=404, detail=detail)
324337

325338
return ProjectResolveResponse(
326339
external_id=project.external_id,

tests/api/v2/test_project_router.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,41 @@ async def test_resolve_project_not_found(client: AsyncClient, v2_projects_url):
445445
assert "not found" in response.json()["detail"].lower()
446446

447447

448+
@pytest.mark.asyncio
449+
async def test_resolve_project_not_found_fresh_install_names_setup_command(
450+
client: AsyncClient, v2_projects_url, project_repository
451+
):
452+
"""#974 follow-up: a fresh install fails its first read with a bare not-found.
453+
454+
config.json bootstraps a "main" default before any reconciliation has created
455+
database rows (the one-shot CLI never runs the server lifespan), so resolving
456+
the configured default 404s. With an empty projects table the error must point
457+
at first-run setup instead of reading like a broken install.
458+
"""
459+
for project in await project_repository.find_all():
460+
await project_repository.delete(project.id)
461+
462+
response = await client.post(f"{v2_projects_url}/resolve", json={"identifier": "main"})
463+
464+
assert response.status_code == 404
465+
detail = response.json()["detail"]
466+
assert detail.startswith("Project not found: 'main'")
467+
assert "basic-memory project add" in detail
468+
469+
470+
@pytest.mark.asyncio
471+
async def test_resolve_project_not_found_with_projects_keeps_plain_message(
472+
client: AsyncClient, test_project: Project, v2_projects_url
473+
):
474+
"""A miss against a populated projects table stays a plain not-found."""
475+
response = await client.post(
476+
f"{v2_projects_url}/resolve", json={"identifier": "nonexistent-project"}
477+
)
478+
479+
assert response.status_code == 404
480+
assert response.json()["detail"] == "Project not found: 'nonexistent-project'"
481+
482+
448483
@pytest.mark.asyncio
449484
async def test_resolve_project_empty_identifier(client: AsyncClient, v2_projects_url):
450485
"""Test resolving with empty identifier returns 422."""

0 commit comments

Comments
 (0)