AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).
fix(onboarding): bound the runway parser and remove the temp-file sweep
Claude329886c45cfe
diff --git a/packages/core/openexecutive/memory/company_profile.py b/packages/core/openexecutive/memory/company_profile.pyindex ee3c1850..32b4e70d 100644--- a/packages/core/openexecutive/memory/company_profile.py+++ b/packages/core/openexecutive/memory/company_profile.py@@ -97,22 +97,24 @@ class CompanyProfile(BaseModel):# silently revert to the umask default.# - fsync file and directory: a hard crash between write and# rename must not leave a zero-length profile.- # Stale temps from an earlier crash are swept first so a- # 0644 copy of the financials never lingers in company/.- for stale in path.parent.glob(f".tmp-*-{path.name}"):- with contextlib.suppress(OSError):- stale.unlink()+ # There is deliberately no sweep of stale temps: a sweep cannot+ # tell a crashed writer's leftover from another process's+ # in-flight file, and unlinking the latter makes its rename fail.+ # A leftover is 0600 clutter, not a leak.tmp_path = path.with_name(f".tmp-{os.getpid()}-{secrets.token_hex(4)}-{path.name}")existing_mode: int | None = Nonewith contextlib.suppress(OSError):- existing_mode = stat.S_IMODE(path.stat().st_mode)+ # lstat: never take the mode from a symlink's target.+ existing_mode = stat.S_IMODE(path.lstat().st_mode)def _exclusive(p: str, flags: int) -> int:return os.open(p, flags | os.O_EXCL, 0o600 if existing_mode is None else existing_mode)try:with open(tmp_path, "w", encoding="utf-8", opener=_exclusive) as f:- if existing_mode is not None:+ if existing_mode is not None and hasattr(os, "fchmod"):+ # os.open applies the umask; fchmod restores the exact+ # mode the destination had. Unix only.os.fchmod(f.fileno(), existing_mode)yaml.dump(data, f, default_flow_style=False, sort_keys=True)f.flush()diff --git a/packages/core/openexecutive/onboarding/wizard.py b/packages/core/openexecutive/onboarding/wizard.pyindex 6c3f7ee5..c19a8e97 100644--- a/packages/core/openexecutive/onboarding/wizard.py+++ b/packages/core/openexecutive/onboarding/wizard.py@@ -181,7 +181,8 @@ def build_profile_from_answers(answers: dict[str, Any]) -> dict[str, Any]:import retext = answers["headcount_and_founding"]- nums = re.findall(r"\d+", text)+ # Bounded so int() can never hit its digit limit on a hostile run.+ nums = re.findall(r"\d{1,9}", text)if nums:if len(nums) >= 2:profile["headcount"] = int(nums[0])@@ -278,7 +279,9 @@ def build_profile_from_answers(answers: dict[str, Any]) -> dict[str, Any]:burn_match = re.search(r"\$?(?<![\d,])(\d[\d,]*)\s*(?:[Kk]\s*)?(?:monthly|/month|per month|burn)", text)- runway_match = re.search(r"(\d+)\s*month", text)+ # Same lookbehind as the other two: without it every digit of a+ # long run is a candidate start and the search is quadratic.+ runway_match = re.search(r"(?<!\d)(\d+)\s*month", text)fin: dict[str, Any] = {}if burn_match:diff --git a/packages/core/tests/unit/test_company_profile.py b/packages/core/tests/unit/test_company_profile.pyindex 87630737..2af755ec 100644--- a/packages/core/tests/unit/test_company_profile.py+++ b/packages/core/tests/unit/test_company_profile.py@@ -137,19 +137,48 @@ def test_save_to_yaml_is_atomic(tmp_path: Path, monkeypatch):assert [p.name for p in tmp_path.iterdir()] == ["profile.yaml"], "temp file left behind"-def test_save_to_yaml_preserves_mode_and_sweeps_stale_temps(tmp_path: Path):- """Rename creates a new inode; an operator's chmod must survive it."""+def test_save_to_yaml_preserves_mode(tmp_path: Path):+ """Rename creates a new inode; an operator's chmod must survive it.++ 0o664 rather than 0o600 on purpose: a fresh profile is already created+ 0o600, and 0o600 is umask-invariant, so it would exercise neither the+ stat nor the fchmod that carry the mode across.+ """import osimport statpath = tmp_path / "profile.yaml"CompanyProfile(name="Before").save_to_yaml(path)- os.chmod(path, 0o600)- stale = tmp_path / ".tmp-1-deadbeef-profile.yaml"- stale.write_text("leftover from a crash", encoding="utf-8")+ assert stat.S_IMODE(path.stat().st_mode) == 0o600, "fresh profile should be private"+ os.chmod(path, 0o664)CompanyProfile(name="After").save_to_yaml(path)- assert stat.S_IMODE(path.stat().st_mode) == 0o600+ assert stat.S_IMODE(path.stat().st_mode) == 0o664assert CompanyProfile.load_from_yaml(path).name == "After"assert [p.name for p in tmp_path.iterdir()] == ["profile.yaml"]+++def test_concurrent_saves_do_not_break_each_other(tmp_path: Path):+ """Two writers on one path must both succeed (last writer wins)."""+ import threading++ path = tmp_path / "profile.yaml"+ errors: list[BaseException] = []++ def _worker(name: str) -> None:+ try:+ for _ in range(40):+ CompanyProfile(name=name).save_to_yaml(path)+ except BaseException as exc: # noqa: BLE001 - collecting for the assert+ errors.append(exc)++ threads = [threading.Thread(target=_worker, args=(f"w{i}",)) for i in range(6)]+ for t in threads:+ t.start()+ for t in threads:+ t.join()++ assert errors == []+ assert CompanyProfile.load_from_yaml(path).name.startswith("w")+ assert [p.name for p in tmp_path.iterdir()] == ["profile.yaml"]diff --git a/packages/core/tests/unit/test_onboarding_wizard_arr_parse.py b/packages/core/tests/unit/test_onboarding_wizard_arr_parse.pyindex 11fe5020..4b9f7982 100644--- a/packages/core/tests/unit/test_onboarding_wizard_arr_parse.py+++ b/packages/core/tests/unit/test_onboarding_wizard_arr_parse.py@@ -120,3 +120,19 @@ def test_long_whitespace_run_is_linear_time() -> None:assert "annual_revenue_arr" not in profileassert "burn_rate_monthly" not in profile["financials"]+++def test_long_digit_run_without_commas_is_linear_time() -> None:+ """Pure digits hit the runway and headcount parsers, not only ARR/burn.++ `(\\d+)\\s*month` without a lookbehind was quadratic (750ms at 10k),+ and int() on a 5000-digit headcount raises at Python's digit limit.+ """+ text = "9" * 200_000+ profile = build_profile_from_answers(+ {"business_model": text, "financials": text, "headcount_and_founding": text}+ )++ assert "annual_revenue_arr" not in profile+ assert profile["financials"] == {}+ assert profile["headcount"] == 999_999_999