Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

fix(onboarding): harden the wizard parsers and the profile save

Claude47f09d4a2a06
diff --git a/packages/core/openexecutive/api/models.py b/packages/core/openexecutive/api/models.py
index b7fd16b2..e332447b 100644
--- a/packages/core/openexecutive/api/models.py
+++ b/packages/core/openexecutive/api/models.py
@@ -63,9 +63,15 @@ class ChatResponse(BaseModel):
session_id: str
+ONBOARD_ANSWER_MAX_CHARS = 10_000
+
+
class OnboardAnswerRequest(BaseModel):
session_id: str
- answer: str
+ # Wizard answers are a sentence or two, or a short one-per-line list.
+ # The free-text parsers in onboarding/wizard.py run regexes over this,
+ # so bound it rather than let one request pin the event loop.
+ answer: str = Field(max_length=ONBOARD_ANSWER_MAX_CHARS)
class OnboardStatusResponse(BaseModel):
diff --git a/packages/core/openexecutive/api/routes/onboarding.py b/packages/core/openexecutive/api/routes/onboarding.py
index 797b960a..f3be72ce 100644
--- a/packages/core/openexecutive/api/routes/onboarding.py
+++ b/packages/core/openexecutive/api/routes/onboarding.py
@@ -76,13 +76,20 @@ async def submit_answer(body: OnboardAnswerRequest) -> OnboardStatusResponse:
try:
build_and_save_profile(state)
except Exception as exc:
- logger.exception("onboarding: profile build failed on the final answer")
+ # Type name only: a pydantic ValidationError's str() embeds the
+ # offending input, and the wizard answers include financials
+ # the UI promises are "stored locally only".
+ logger.error(
+ "onboarding: profile build failed on the final answer (%s)",
+ type(exc).__name__,
+ )
_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."
+ "Rephrase your last answer and try again, or restart "
+ "onboarding if an earlier answer is the problem."
),
) from exc
diff --git a/packages/core/openexecutive/memory/company_profile.py b/packages/core/openexecutive/memory/company_profile.py
index 83503c5c..ab87a239 100644
--- a/packages/core/openexecutive/memory/company_profile.py
+++ b/packages/core/openexecutive/memory/company_profile.py
@@ -1,5 +1,7 @@
from __future__ import annotations
+import contextlib
+import os
from pathlib import Path
from typing import Any
@@ -78,8 +80,21 @@ class CompanyProfile(BaseModel):
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
data = {"company": self.model_dump()}
- with open(path, "w", encoding="utf-8") as f:
- yaml.dump(data, f, default_flow_style=False, sort_keys=True)
+ # Write-then-rename so a failure mid-dump (disk full, unrepresentable
+ # value) can never leave a truncated profile behind: every other
+ # subsystem loads this file, and callers such as the onboarding
+ # route roll back on failure assuming the old profile survived.
+ # Plain open() (not mkstemp) so the temp file gets the normal umask
+ # permissions the final file would have had.
+ tmp_path = path.with_name(f".tmp-{os.getpid()}-{path.name}")
+ try:
+ with open(tmp_path, "w", encoding="utf-8") as f:
+ yaml.dump(data, f, default_flow_style=False, sort_keys=True)
+ os.replace(tmp_path, path)
+ except BaseException:
+ with contextlib.suppress(OSError):
+ os.unlink(tmp_path)
+ raise
def to_prompt_block(self) -> str:
if not self.name:
diff --git a/packages/core/openexecutive/onboarding/wizard.py b/packages/core/openexecutive/onboarding/wizard.py
index 33abb0ac..635b7e51 100644
--- a/packages/core/openexecutive/onboarding/wizard.py
+++ b/packages/core/openexecutive/onboarding/wizard.py
@@ -205,17 +205,22 @@ def build_profile_from_answers(answers: dict[str, Any]) -> dict[str, Any]:
# 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)
+ # "roughly 50 million" and "$50MM". The lookbehind keeps the scan
+ # linear: without it every digit inside a long "1,1,1,…" run is a
+ # candidate start and the search goes quadratic on hostile input.
+ arr_match = re.search(
+ r"\$?(?<![\d,])(\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
# surprising input must never take down onboarding. Skipping
# the field costs one profile value; raising costs the run.
+ # The answer text itself stays out of the log line.
try:
profile["annual_revenue_arr"] = float(val) * 1_000_000
except ValueError:
- logger.warning("onboarding: could not parse ARR from %r", text[:80])
+ logger.warning("onboarding: could not parse ARR from the business-model answer")
if "competitive_landscape" in answers:
text = answers["competitive_landscape"]
@@ -268,7 +273,7 @@ def build_profile_from_answers(answers: dict[str, Any]) -> dict[str, Any]:
# 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
+ r"\$?(?<![\d,])(\d[\d,]*)\s*[Kk]?\s*(?:monthly|/month|per month|burn)", text
)
runway_match = re.search(r"(\d+)\s*month", text)
@@ -279,7 +284,9 @@ def build_profile_from_answers(answers: dict[str, Any]) -> dict[str, Any]:
try:
fin["burn_rate_monthly"] = float(val) * multiplier
except ValueError:
- logger.warning("onboarding: could not parse burn rate from %r", text[:80])
+ # Financials are promised "stored locally only" — keep the
+ # answer text out of the log stream.
+ logger.warning("onboarding: could not parse burn rate from the financials answer")
if runway_match:
fin["runway_months"] = float(runway_match.group(1))
profile["financials"] = fin
diff --git a/packages/core/tests/unit/test_company_profile.py b/packages/core/tests/unit/test_company_profile.py
index 4f6f5ea8..33d995ff 100644
--- a/packages/core/tests/unit/test_company_profile.py
+++ b/packages/core/tests/unit/test_company_profile.py
@@ -107,3 +107,31 @@ def test_prompt_block_includes_financials():
block = profile.to_prompt_block()
assert "300,000" in block
assert "8.0 months" in block
+
+
+def test_save_to_yaml_is_atomic(tmp_path: Path, monkeypatch):
+ """A failure mid-dump must leave the previous profile.yaml untouched.
+
+ The onboarding route rolls its session back on a build failure on the
+ assumption that the on-disk profile every other subsystem loads survived.
+ A plain open(path, "w") truncates before yaml.dump writes a byte.
+ """
+ import yaml
+
+ path = tmp_path / "profile.yaml"
+ CompanyProfile(name="Before").save_to_yaml(path)
+
+ def boom(*args, **kwargs):
+ raise OSError("disk full")
+
+ monkeypatch.setattr(yaml, "dump", boom)
+ try:
+ CompanyProfile(name="After").save_to_yaml(path)
+ except OSError:
+ pass
+ else: # pragma: no cover - the monkeypatch must propagate
+ raise AssertionError("save_to_yaml swallowed the failure")
+ monkeypatch.undo()
+
+ assert CompanyProfile.load_from_yaml(path).name == "Before"
+ assert [p.name for p in tmp_path.iterdir()] == ["profile.yaml"], "temp file left behind"
diff --git a/packages/core/tests/unit/test_onboarding_wizard_arr_parse.py b/packages/core/tests/unit/test_onboarding_wizard_arr_parse.py
index 15affdcc..2d1f6d7b 100644
--- a/packages/core/tests/unit/test_onboarding_wizard_arr_parse.py
+++ b/packages/core/tests/unit/test_onboarding_wizard_arr_parse.py
@@ -91,3 +91,18 @@ def test_no_magnitude_leaves_the_field_unset() -> None:
assert "annual_revenue_arr" not in profile
assert profile["target_customer"]["pain_points"] == []
+
+
+def test_long_digit_run_is_linear_time() -> None:
+ """A "1,1,1,…" run with no magnitude after it must not go quadratic.
+
+ Every digit in the run used to be a candidate match start, so a 32k-char
+ answer took ~10s and a 320k one ~20 minutes on the event loop. With the
+ lookbehind only the run's first digit is a candidate. If this regresses
+ the test does not fail, it hangs — which is the point.
+ """
+ text = "1," * 100_000
+ profile = build_profile_from_answers({"business_model": text, "financials": text})
+
+ assert "annual_revenue_arr" not in profile
+ assert "burn_rate_monthly" not in profile["financials"]
diff --git a/packages/core/tests/unit/test_onboarding_wizard_financials_parse.py b/packages/core/tests/unit/test_onboarding_wizard_financials_parse.py
index a29f5b67..73b7d2ec 100644
--- a/packages/core/tests/unit/test_onboarding_wizard_financials_parse.py
+++ b/packages/core/tests/unit/test_onboarding_wizard_financials_parse.py
@@ -17,7 +17,6 @@ from openexecutive.onboarding.wizard import build_profile_from_answers
[
"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:
@@ -27,6 +26,11 @@ def test_comma_before_burn_keyword_does_not_crash(text: str) -> None:
def test_comma_case_still_records_runway() -> None:
+ """Used to crash on the bare comma before "monthly".
+
+ The burn figure here follows its keyword, which the magnitude-first
+ heuristic does not read; this test only pins the crash and the runway.
+ """
profile = build_profile_from_answers(
{"financials": "12 months runway, monthly burn 100k"}
)
diff --git a/packages/core/tests/unit/test_onboarding_wizard_route.py b/packages/core/tests/unit/test_onboarding_wizard_route.py
index 177b85f2..973da01e 100644
--- a/packages/core/tests/unit/test_onboarding_wizard_route.py
+++ b/packages/core/tests/unit/test_onboarding_wizard_route.py
@@ -13,6 +13,7 @@ import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
+from openexecutive.api.models import ONBOARD_ANSWER_MAX_CHARS
from openexecutive.api.routes import onboarding as route
from openexecutive.onboarding import profile_builder
from openexecutive.onboarding.wizard import TOTAL_STEPS
@@ -54,7 +55,7 @@ def test_builder_failure_on_final_answer_is_retryable(
resp = client.post("/onboard/answer", json={"session_id": session_id, "answer": "final"})
assert resp.status_code == 422
- assert "rephrase" in resp.json()["detail"]
+ assert "rephrase" in resp.json()["detail"].lower()
# The session was rolled back to the last step, not stuck at completed.
status = client.get(f"/onboard/status/{session_id}").json()
@@ -99,3 +100,26 @@ def test_issue_84_answer_completes_through_the_real_builder(
assert resp.json()["completed"] is True
assert (tmp_path / "profile.yaml").exists()
+
+
+def test_oversized_answer_is_rejected_before_parsing(
+ client: TestClient, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ called = False
+
+ def _never(state): # type: ignore[no-untyped-def]
+ nonlocal called
+ called = True
+
+ monkeypatch.setattr(profile_builder, "build_and_save_profile", _never)
+ session_id = client.get("/onboard/start").json()["session_id"]
+
+ too_long = "x" * (ONBOARD_ANSWER_MAX_CHARS + 1)
+ resp = client.post("/onboard/answer", json={"session_id": session_id, "answer": too_long})
+ assert resp.status_code == 422
+ assert route._wizard_sessions[session_id].current_step == 0
+
+ just_fits = "x" * ONBOARD_ANSWER_MAX_CHARS
+ resp = client.post("/onboard/answer", json={"session_id": session_id, "answer": just_fits})
+ assert resp.status_code == 200
+ assert called is False