AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).
fix(onboarding): finish the ARR regex fix, cover burn rate, make the final step retryable
Claude147962ffdc97
diff --git a/packages/core/openexecutive/api/routes/onboarding.py b/packages/core/openexecutive/api/routes/onboarding.pyindex 3921e451..797b960a 100644--- a/packages/core/openexecutive/api/routes/onboarding.py+++ b/packages/core/openexecutive/api/routes/onboarding.py@@ -1,6 +1,7 @@from __future__ import annotationsimport asyncio+import copyimport loggingimport uuid@@ -62,13 +63,28 @@ async def submit_answer(body: OnboardAnswerRequest) -> OnboardStatusResponse:if state.completed:raise HTTPException(status_code=400, detail="Onboarding already completed")+ # process_answer mutates the stored state in place. Keep a snapshot so+ # a failed profile build on the final answer can be rolled back —+ # otherwise the session is stuck at completed=True and every retry+ # hits the 400 above, forcing the user to restart onboarding.+ snapshot = copy.deepcopy(state)state = process_answer(state, body.answer)- _wizard_sessions[body.session_id] = stateif state.completed:from openexecutive.onboarding.profile_builder import build_and_save_profile- build_and_save_profile(state)+ try:+ build_and_save_profile(state)+ except Exception as exc:+ logger.exception("onboarding: profile build failed on the final answer")+ _wizard_sessions[body.session_id] = snapshot+ raise HTTPException(+ status_code=422,+ detail=(+ "Could not build the company profile from your answers. "+ "Please rephrase your last answer and try again."+ ),+ ) from exc# Fire the watchlist-research workflow once at onboarding# completion so the principal's first /today after install@@ -87,6 +103,8 @@ async def submit_answer(body: OnboardAnswerRequest) -> OnboardStatusResponse:# the process lifetime.task.add_done_callback(_background_research_tasks.discard)+ _wizard_sessions[body.session_id] = state+question = get_current_question(state) if not state.completed else Noneprogress = state.get_progress()diff --git a/packages/core/openexecutive/onboarding/wizard.py b/packages/core/openexecutive/onboarding/wizard.pyindex 90787028..33abb0ac 100644--- a/packages/core/openexecutive/onboarding/wizard.py+++ b/packages/core/openexecutive/onboarding/wizard.py@@ -198,13 +198,15 @@ def build_profile_from_answers(answers: dict[str, Any]) -> dict[str, Any]:profile["target_customer"] = {"profile": text, "pain_points": []}import re- # The capture must START with a digit and the M must end a word.- # `[\d,]+` alone matched a bare comma, so any answer with a comma- # before an m-word — "Consultoría estratégica, marketing y- # operaciones" — captured "," and crashed the whole wizard on- # float(""). Requiring \b also stops "300 clientes, marketing"- # from being read as $300M.- arr_match = re.search(r"\$?(\d[\d,]*)\s*[Mm]\b", text)+ # The capture must START with a digit and the magnitude must end a+ # word. `[\d,]+` alone matched a bare comma, so any answer with a+ # comma before an m-word — "IT, marketing and video agency" —+ # captured "," and crashed the whole wizard on float(""). The \b+ # also stops "300 clients, marketing" from being read as $300M and+ # "$5 minimum" from becoming $5M. The alternation keeps "M", "MM"+ # and "million" all parsing, since a bare `[Mm]\b` silently dropped+ # "roughly 50 million" and "$50MM".+ arr_match = re.search(r"\$?(\d[\d,]*)\s*(?:[Mm]{1,2}|[Mm]illion)\b", text)if arr_match:val = arr_match.group(1).replace(",", "")# Defensive: this is a best-effort parse of free text, so a@@ -262,14 +264,22 @@ def build_profile_from_answers(answers: dict[str, Any]) -> dict[str, Any]:import retext = answers["financials"]- burn_match = re.search(r"\$?([\d,]+)\s*[Kk]?\s*(?:monthly|/month|per month|burn)", text)+ # Same digit-first rule as the ARR parse above: `[\d,]+` matched a+ # bare comma, so "runway is fine, monthly costs are low" captured+ # "," and crashed on float("").+ burn_match = re.search(+ r"\$?(\d[\d,]*)\s*[Kk]?\s*(?:monthly|/month|per month|burn)", text+ )runway_match = re.search(r"(\d+)\s*month", text)fin: dict[str, Any] = {}if burn_match:val = burn_match.group(1).replace(",", "")multiplier = 1000 if "k" in text[burn_match.start():burn_match.end()].lower() else 1- fin["burn_rate_monthly"] = float(val) * multiplier+ try:+ fin["burn_rate_monthly"] = float(val) * multiplier+ except ValueError:+ logger.warning("onboarding: could not parse burn rate from %r", text[:80])if runway_match:fin["runway_months"] = float(runway_match.group(1))profile["financials"] = findiff --git a/packages/core/tests/unit/test_onboarding_wizard_arr_parse.py b/packages/core/tests/unit/test_onboarding_wizard_arr_parse.pyindex 676a6d47..15affdcc 100644--- a/packages/core/tests/unit/test_onboarding_wizard_arr_parse.py+++ b/packages/core/tests/unit/test_onboarding_wizard_arr_parse.py@@ -26,6 +26,8 @@ from openexecutive.onboarding.wizard import build_profile_from_answers"Vendemos software B2B, modelo de suscripción","We sell software, mostly to mid-market teams","Design and build, maintenance included",+ # The exact answer from issue #84.+ "IT, marketing and video agency",],)def test_comma_before_m_word_does_not_crash(text: str) -> None:@@ -42,6 +44,11 @@ def test_comma_before_m_word_does_not_crash(text: str) -> None:("We do $2M in ARR", 2_000_000.0),("Roughly 12M annually", 12_000_000.0),("ARR is $1,500M across all lines", 1_500_000_000.0),+ # Phrasings the bare `[Mm]\b` narrowing silently dropped.+ ("roughly 50 million in revenue", 50_000_000.0),+ ("About 3 Million ARR", 3_000_000.0),+ ("$50MM ARR last year", 50_000_000.0),+ ("we closed the year at 7mm", 7_000_000.0),],)def test_magnitude_still_parses(text: str, expected: float) -> None:@@ -64,6 +71,21 @@ def test_digits_before_an_m_word_are_not_a_magnitude() -> None:assert "annual_revenue_arr" not in profile+ "text",+ [+ "$5 minimum order, mostly SMBs",+ "We have 300 clients, marketing is word of mouth",+ "12 major accounts and growing",+ ],+)+def test_digits_before_a_non_magnitude_m_word_are_ignored(text: str) -> None:+ """The word boundary must hold across the whole alternation."""+ profile = build_profile_from_answers({"business_model": text})++ assert "annual_revenue_arr" not in profile++def test_no_magnitude_leaves_the_field_unset() -> None:profile = build_profile_from_answers({"business_model": "A boutique consultancy"})diff --git a/packages/core/tests/unit/test_onboarding_wizard_financials_parse.py b/packages/core/tests/unit/test_onboarding_wizard_financials_parse.pynew file mode 100644index 00000000..a29f5b67--- /dev/null+++ b/packages/core/tests/unit/test_onboarding_wizard_financials_parse.py@@ -0,0 +1,49 @@+"""Burn-rate parsing in the onboarding wizard's financials step.++Sibling of the ARR regression in ``test_onboarding_wizard_arr_parse.py``:+the burn-rate heuristic used the same ``[\\d,]+`` character class, so a bare+comma before "monthly", "/month", "per month" or "burn" captured ``","`` and+crashed ``build_profile_from_answers`` on ``float("")``.+"""+from __future__ import annotations++import pytest++from openexecutive.onboarding.wizard import build_profile_from_answers+++ "text",+ [+ "Runway is fine, monthly costs are low",+ "We are profitable, burn is zero",+ "12 months runway, monthly burn 100k",+ ],+)+def test_comma_before_burn_keyword_does_not_crash(text: str) -> None:+ profile = build_profile_from_answers({"financials": text})++ assert "burn_rate_monthly" not in profile["financials"]+++def test_comma_case_still_records_runway() -> None:+ profile = build_profile_from_answers(+ {"financials": "12 months runway, monthly burn 100k"}+ )++ assert profile["financials"]["runway_months"] == 12.0+++ ("text", "expected"),+ [+ ("burn 50k monthly", 50_000.0),+ ("$120,000 per month, 18 months runway", 120_000.0),+ ("We spend 30K/month", 30_000.0),+ ("about 2,500 monthly burn", 2_500.0),+ ],+)+def test_burn_rate_still_parses(text: str, expected: float) -> None:+ profile = build_profile_from_answers({"financials": text})++ assert profile["financials"]["burn_rate_monthly"] == expecteddiff --git a/packages/core/tests/unit/test_onboarding_wizard_route.py b/packages/core/tests/unit/test_onboarding_wizard_route.pynew file mode 100644index 00000000..177b85f2--- /dev/null+++ b/packages/core/tests/unit/test_onboarding_wizard_route.py@@ -0,0 +1,101 @@+"""HTTP-level tests for the company-onboarding wizard's final step.++Regression for issue #84's second half: ``/onboard/answer`` used to commit the+final answer (``completed=True``) *before* building the profile. When the+build raised, the client got a 500 and every retry hit "Onboarding already+completed" — the only way out was to restart onboarding from scratch.+"""+from __future__ import annotations++from pathlib import Path++import pytest+from fastapi import FastAPI+from fastapi.testclient import TestClient++from openexecutive.api.routes import onboarding as route+from openexecutive.onboarding import profile_builder+from openexecutive.onboarding.wizard import TOTAL_STEPS+++def client(monkeypatch: pytest.MonkeyPatch) -> TestClient:+ async def _no_research(session_id: str) -> None:+ return None++ monkeypatch.setattr(route, "_fire_post_onboarding_research", _no_research)+ monkeypatch.setattr(route, "_wizard_sessions", {})+ monkeypatch.setattr(route, "_onboarding_research_fired", set())+ app = FastAPI()+ app.include_router(route.router)+ return TestClient(app)+++def _answer_all_but_last(client: TestClient) -> str:+ session_id = client.get("/onboard/start").json()["session_id"]+ for _ in range(TOTAL_STEPS - 1):+ resp = client.post("/onboard/answer", json={"session_id": session_id, "answer": "x"})+ assert resp.status_code == 200+ assert resp.json()["completed"] is False+ return session_id+++def test_builder_failure_on_final_answer_is_retryable(+ client: TestClient, monkeypatch: pytest.MonkeyPatch+) -> None:+ calls: list[str] = []++ def _boom(state): # type: ignore[no-untyped-def]+ calls.append("boom")+ raise ValueError("could not convert string to float: ''")++ monkeypatch.setattr(profile_builder, "build_and_save_profile", _boom)+ session_id = _answer_all_but_last(client)++ resp = client.post("/onboard/answer", json={"session_id": session_id, "answer": "final"})+ assert resp.status_code == 422+ assert "rephrase" in resp.json()["detail"]++ # The session was rolled back to the last step, not stuck at completed.+ status = client.get(f"/onboard/status/{session_id}").json()+ assert status["completed"] is False+ assert status["current_step"] == TOTAL_STEPS - 1++ # A retry with a working builder completes normally...+ def _ok(state): # type: ignore[no-untyped-def]+ calls.append("ok")++ monkeypatch.setattr(profile_builder, "build_and_save_profile", _ok)+ resp = client.post("/onboard/answer", json={"session_id": session_id, "answer": "final"})+ assert resp.status_code == 200+ assert resp.json()["completed"] is True+ assert calls == ["boom", "ok"]++ # ...and only then does the session refuse further answers.+ resp = client.post("/onboard/answer", json={"session_id": session_id, "answer": "again"})+ assert resp.status_code == 400+++def test_issue_84_answer_completes_through_the_real_builder(+ client: TestClient, monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ """End-to-end: the reporter's exact business-model answer finishes onboarding."""+ real_build = profile_builder.build_and_save_profile++ def _build_to_tmp(state): # type: ignore[no-untyped-def]+ return real_build(state, profile_path=tmp_path / "profile.yaml")++ monkeypatch.setattr(profile_builder, "build_and_save_profile", _build_to_tmp)+ monkeypatch.setattr(profile_builder, "_save_wizard_people", lambda answers: None)++ session_id = client.get("/onboard/start").json()["session_id"]+ answers = {"business_model": "IT, marketing and video agency"}+ for step in range(TOTAL_STEPS):+ from openexecutive.onboarding.wizard import WIZARD_STEPS++ answer = answers.get(WIZARD_STEPS[step]["field"], "x")+ resp = client.post("/onboard/answer", json={"session_id": session_id, "answer": answer})+ assert resp.status_code == 200, resp.json()++ assert resp.json()["completed"] is True+ assert (tmp_path / "profile.yaml").exists()