Skip to content

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

fix(onboarding): close the second adversarial review round

Rufus5dfd85dfb442
diff --git a/packages/core/openexecutive/api/routes/onboarding.py b/packages/core/openexecutive/api/routes/onboarding.py
index c03aaf84..83f9ebfb 100644
--- a/packages/core/openexecutive/api/routes/onboarding.py
+++ b/packages/core/openexecutive/api/routes/onboarding.py
@@ -455,7 +455,14 @@ async def start_interview(
return _turn_response(session_id, session)
session.transcript.append(Turn(role="user", text=opening.strip()))
- return await _advance_interview(session_id, session)
+ try:
+ return await _advance_interview(session_id, session)
+ except HTTPException:
+ # The error body carries no session_id, so the client can never resume
+ # this one — leaving it in the dict would orphan it until the TTL, and
+ # a burst of failures during a provider blip would evict live sessions.
+ _interview_sessions.pop(session_id, None)
+ raise
@router.post("/onboard/interview/message", response_model=OnboardTurnResponse)
@@ -578,8 +585,11 @@ async def commit_interview(body: OnboardCommitRequest) -> CompanyProfileResponse
CompanyDraft(profile=profile, people=people, departments=departments)
)
if errors:
+ # .safe, never .detail — the detailed rendering quotes the rejected
+ # value (a name, a head reference), which is client text sitting next
+ # to the company's financials. Only the repair turn sees .detail.
logger.info("onboarding commit: rejected draft (%d error(s))", len(errors))
- raise HTTPException(status_code=422, detail=errors[0])
+ raise HTTPException(status_code=422, detail=errors[0].safe)
profile = derive_org_structure(profile, people, departments)
diff --git a/packages/core/openexecutive/onboarding/commit.py b/packages/core/openexecutive/onboarding/commit.py
index bf46ede4..60e62143 100644
--- a/packages/core/openexecutive/onboarding/commit.py
+++ b/packages/core/openexecutive/onboarding/commit.py
@@ -25,6 +25,7 @@ from openexecutive.utils.slug import DEPARTMENT_SLUG_FALLBACK, slugify
if TYPE_CHECKING: # pragma: no cover - typing only
from openexecutive.memory.company_profile import CompanyProfile
from openexecutive.onboarding.interview import DepartmentDraft, PersonDraft
+ from openexecutive.people.models import AuthorityScope
logger = logging.getLogger(__name__)
@@ -52,13 +53,14 @@ def save_onboarding_people(drafts: list[PersonDraft]) -> dict[str, int]:
try:
from openexecutive.people.models import AuthorityScope
from openexecutive.people.store import (
- initialize_db as init_people_db,
- )
- from openexecutive.people.store import (
+ get_person,
list_people,
set_authority_scope,
upsert_person,
)
+ from openexecutive.people.store import (
+ initialize_db as init_people_db,
+ )
named = [d for d in drafts if d.full_name.strip()]
if not named:
@@ -96,8 +98,19 @@ def save_onboarding_people(drafts: list[PersonDraft]) -> dict[str, int]:
reports_to_person_id=prior.reports_to_person_id,
)
ids[name] = pid
+ # Register immediately: two spellings of one name resolve to the
+ # same row, and a stale snapshot would insert a duplicate instead.
+ refreshed = get_person(pid)
+ if refreshed is not None:
+ existing[name.lower()] = refreshed
+
if draft.is_principal:
set_authority_scope(pid, [AuthorityScope.WILDCARD])
+ elif prior is not None and prior.is_principal:
+ # Demoted but still on the roster — _demote_stale_principals
+ # skips them (they ARE in keep_ids), so strip WILDCARD here or
+ # they keep blanket approval authority.
+ _strip_wildcard(pid, prior.authority_scope)
_demote_stale_principals(set(ids.values()))
return ids
@@ -116,10 +129,8 @@ def _demote_stale_principals(keep_ids: set[int]) -> None:
principal with blanket approval authority, and ``find_principal_person``
(oldest id wins) keeps resolving to them.
"""
- from openexecutive.people.models import AuthorityScope
from openexecutive.people.store import (
list_people,
- set_authority_scope,
upsert_person,
)
@@ -141,11 +152,17 @@ def _demote_stale_principals(keep_ids: set[int]) -> None:
on_leave_until=person.on_leave_until,
reports_to_person_id=person.reports_to_person_id,
)
- remaining: list[AuthorityScope] = [
- s for s in person.authority_scope if s != AuthorityScope.WILDCARD
- ]
- if remaining != person.authority_scope:
- set_authority_scope(person.id, remaining)
+ _strip_wildcard(person.id, person.authority_scope)
+
+
+def _strip_wildcard(person_id: int, current: list[AuthorityScope]) -> None:
+ """Remove WILDCARD, leaving any other scope the person was granted."""
+ from openexecutive.people.models import AuthorityScope as Scope
+ from openexecutive.people.store import set_authority_scope
+
+ remaining: list[Scope] = [s for s in current if s != Scope.WILDCARD]
+ if len(remaining) != len(current):
+ set_authority_scope(person_id, remaining)
def reconcile_onboarding_departments(
@@ -190,15 +207,21 @@ def reconcile_onboarding_departments(
# Same fallback the store uses, so the slug we look up is the one
# create_department would have assigned.
drafted_slug = slugify(title, fallback=DEPARTMENT_SLUG_FALLBACK)
- if drafted_slug in seen:
+ match = by_slug.get(drafted_slug) or by_title.get(title.lower())
+ # Key on the row actually being touched. Keying on the drafted slug
+ # alone missed the match-by-TITLE case: the shipped `hr` department
+ # is titled "People & Talent", so drafts "HR" and "People & Talent"
+ # have different slugs but resolve to the same row — the second
+ # silently clobbered the first while counts claimed two updates.
+ key = match.slug if match is not None else drafted_slug
+ if key in seen:
logger.info(
- "onboarding: skipping a department title that collides with "
- "an earlier one in the same draft"
+ "onboarding: skipping a department that resolves to one "
+ "already handled in this draft"
)
continue
- seen.add(drafted_slug)
+ seen.add(key)
- match = by_slug.get(drafted_slug) or by_title.get(title.lower())
head_id = person_ids.get(draft.head_person_name.strip()) if draft.head_person_name else None
if match is None:
@@ -209,7 +232,6 @@ def reconcile_onboarding_departments(
# row we just made instead of creating a `-2` duplicate.
by_slug[slug] = created.config
by_title[created.config.title.strip().lower()] = created.config
- seen.add(slug)
# create_department fixes authority to propose_only and takes no
# head, so apply the drafted values in a second call.
update_department(slug, authority_level=draft.authority_level)
diff --git a/packages/core/openexecutive/onboarding/interview.py b/packages/core/openexecutive/onboarding/interview.py
index 7e9e884e..82b96137 100644
--- a/packages/core/openexecutive/onboarding/interview.py
+++ b/packages/core/openexecutive/onboarding/interview.py
@@ -35,6 +35,7 @@ from __future__ import annotations
import asyncio
import json
import logging
+from dataclasses import dataclass
from typing import Any
from pydantic import BaseModel, ConfigDict, Field, ValidationError
@@ -53,13 +54,22 @@ logger = logging.getLogger(__name__)
# Budgets. The user can always short-circuit with force_draft, so these only
# bound a runaway model.
MAX_QUESTIONS = 8
-MAX_TRANSCRIPT_CHARS = 120_000
+# Must stay above what a single legal /start can produce — a 20k description
+# plus 8 attachments at 15k extracted chars each is ~140k. A lower ceiling
+# locked the user out of the conversation on turn one for doing exactly what
+# the UI invites ("you can also attach a deck").
+MAX_TRANSCRIPT_CHARS = 250_000
# Field bounds. Enforced here rather than with Field(max_length=...) so a
# rejection reports a fixed string instead of echoing the offending value
# back in FastAPI's 422 body — these drafts carry the company's financials.
MAX_NAME_CHARS = 200
+MAX_ROLE_CHARS = 200
MAX_MISSION_CHARS = 2000
+# The profile is rendered into the CACHED system prompt on every Executive
+# turn (see CLAUDE.md), so an unbounded field here is a permanent cost on
+# every request, not just one big row.
+MAX_PROFILE_TEXT_CHARS = 10_000
_MAX_TOKENS = 8000
# Truncation for the repair turn that echoes the model's own bad output back at
@@ -72,6 +82,12 @@ EMIT_TOOL_NAME = "emit_company_draft"
# The first thing the user sees. A constant, so /onboard/interview/start costs
# no model call and cannot fail.
+# Sent when the transcript would otherwise end on an assistant turn.
+_CONTINUE_PROMPT = (
+ "Continue from what you already have: ask the next question, or draft "
+ "the profile if you have enough."
+)
+
OPENING_PROMPT = (
"Tell me about your company — what you do, who you sell to, roughly how "
"big you are, and what you're focused on this year. Write it however you "
@@ -137,6 +153,20 @@ class Question(BaseModel):
hint: str = ""
+@dataclass(frozen=True)
+class DraftError:
+ """One validation failure, in two renderings.
+
+ ``detail`` quotes the offending value — useful to the model in the repair
+ turn, and NEVER safe in an HTTP body, because a rejected name or head
+ reference is client-supplied text sitting next to the company's
+ financials. ``safe`` is the fixed string the route returns.
+ """
+
+ safe: str
+ detail: str
+
+
_ASK_TOOL: dict[str, Any] = {
"name": ASK_TOOL_NAME,
"description": (
@@ -325,54 +355,88 @@ _EMIT_TOOL: dict[str, Any] = {
TOOLS: list[dict[str, Any]] = sorted([_ASK_TOOL, _EMIT_TOOL], key=lambda t: str(t["name"]))
-def validate_draft(draft: CompanyDraft) -> list[str]:
- """Referential-integrity errors (empty list = OK).
+def validate_draft(draft: CompanyDraft) -> list[DraftError]:
+ """Referential-integrity and bounds errors (empty list = OK).
Field-level validation already happened in ``model_validate``; this is the
cross-object layer the commit step relies on, mirroring
- ``fixtures.generator.validate_bundle``.
+ ``fixtures.generator.validate_bundle``. Every error carries both a
+ model-facing ``detail`` and a client-facing ``safe`` string — see
+ ``DraftError``.
"""
- errors: list[str] = []
+ errors: list[DraftError] = []
+
+ def add(safe: str, detail: str | None = None) -> None:
+ errors.append(DraftError(safe=safe, detail=detail or safe))
if not draft.profile.name.strip():
- errors.append("profile.name is required")
+ add("Your company needs a name.", "profile.name is required")
if not draft.people:
- errors.append("at least one person is required")
+ add(
+ "Add at least one person, and mark which one is you.",
+ "at least one person is required",
+ )
all_names = [p.full_name.strip() for p in draft.people]
if any(not n for n in all_names):
- errors.append("every person needs a non-empty full_name")
- if len(set(all_names)) != len(all_names):
- dupes = sorted({n for n in all_names if all_names.count(n) > 1})
- errors.append(f"duplicate full_name(s) in roster: {dupes}")
+ add("Every person needs a name.", "every person needs a non-empty full_name")
+ # Case-INSENSITIVE, matching the key save_onboarding_people upserts on.
+ # A case-sensitive check let "JANE DOE" and "Jane Doe" both through, and the
+ # upsert then collapsed them onto one row — last write wins, which could
+ # land on the non-principal spelling and leave the company with NO principal.
+ folded = [n.lower() for n in all_names]
+ if len(set(folded)) != len(folded):
+ dupes = sorted({n for n in all_names if folded.count(n.lower()) > 1})
+ add(
+ "Two people have the same name — give them distinct names.",
+ f"duplicate full_name(s) in roster: {dupes}",
+ )
principals = [p for p in draft.people if p.is_principal]
if len(principals) != 1:
# find_principal_person() backs caller resolution, alert routing, and
# the scheduler's principal brief. Zero or two is a real breakage.
- errors.append(
- f"exactly one person must have is_principal=true (got {len(principals)})"
+ add(
+ "Mark exactly one person as you.",
+ f"exactly one person must have is_principal=true (got {len(principals)})",
)
if any(len(n) > MAX_NAME_CHARS for n in all_names):
- errors.append(f"a person's name is too long (limit {MAX_NAME_CHARS} characters)")
+ add(f"A person's name is too long (limit {MAX_NAME_CHARS} characters).")
+ if any(len(p.role) > MAX_ROLE_CHARS for p in draft.people):
+ add(f"A person's role is too long (limit {MAX_ROLE_CHARS} characters).")
+
+ # The profile lands in the cached system prompt on every Executive turn, so
+ # an unbounded field here is a permanent per-request cost.
+ for label, value in (
+ ("name", draft.profile.name),
+ ("mission", draft.profile.mission),
+ ("vision", draft.profile.vision),
+ ("industry", draft.profile.industry),
+ ("stage", draft.profile.stage),
+ ):
+ if len(value) > MAX_PROFILE_TEXT_CHARS:
+ add(
+ f"The company {label} is too long "
+ f"(limit {MAX_PROFILE_TEXT_CHARS:,} characters)."
+ )
- names = set(all_names)
+ names = {n.lower() for n in all_names}
for d in draft.departments:
if not d.title.strip():
- errors.append("every department needs a non-empty title")
+ add("Every department needs a name.", "every department needs a non-empty title")
if len(d.title) > MAX_NAME_CHARS:
- errors.append(
- f"a department title is too long (limit {MAX_NAME_CHARS} characters)"
- )
+ add(f"A department name is too long (limit {MAX_NAME_CHARS} characters).")
if len(d.mission) > MAX_MISSION_CHARS:
- errors.append(
- f"a department mission is too long (limit {MAX_MISSION_CHARS} characters)"
+ add(
+ f"A department description is too long "
+ f"(limit {MAX_MISSION_CHARS:,} characters)."
)
- if d.head_person_name and d.head_person_name not in names:
- errors.append(
+ if d.head_person_name and d.head_person_name.strip().lower() not in names:
+ add(
+ "A department is led by someone who isn't on the team list.",
f"department '{d.title}' head_person_name "
- f"'{d.head_person_name}' is not in the roster"
+ f"'{d.head_person_name}' is not in the roster",
)
# Same fallback the store uses, so two titles that both slugify to
# nothing collide here exactly as they would on insert.
@@ -383,7 +447,10 @@ def validate_draft(draft: CompanyDraft) -> list[str]:
]
if len(set(slugs)) != len(slugs):
dupes = sorted({s for s in slugs if slugs.count(s) > 1})
- errors.append(f"duplicate department title(s): {dupes}")
+ add(
+ "Two departments have the same name.",
+ f"duplicate department title(s): {dupes}",
+ )
return errors
@@ -422,6 +489,13 @@ def _build_messages(
messages.append({"role": t.role, "content": t.text})
if not messages:
raise InterviewError("The setup session has no conversation yet.")
+ # A draft records itself as an assistant turn, so the transcript can end on
+ # one — e.g. "ask me more questions" then "draft again" without typing.
+ # Sending that as a trailing assistant message is a prefill, which the API
+ # rejects alongside a forced tool_choice, and semantically asks the model to
+ # continue its own summary rather than act.
+ if messages[-1]["role"] == "assistant":
+ messages.append({"role": "user", "content": _CONTINUE_PROMPT})
if existing_profile is not None and not existing_profile.is_empty():
block = existing_profile.to_prompt_block()
if block:
@@ -572,9 +646,10 @@ async def advance(
)
errors = [str(exc)]
else:
- errors = validate_draft(draft)
- if not errors:
+ draft_errors = validate_draft(draft)
+ if not draft_errors:
return draft
+ errors = [e.detail for e in draft_errors]
logger.info("onboarding interview: draft has %d consistency error(s)", len(errors))
if attempt == 0:
diff --git a/packages/core/tests/unit/test_onboarding_commit.py b/packages/core/tests/unit/test_onboarding_commit.py
index 7a74a02c..1a3d0150 100644
--- a/packages/core/tests/unit/test_onboarding_commit.py
+++ b/packages/core/tests/unit/test_onboarding_commit.py
@@ -411,3 +411,72 @@ def test_rerun_preserves_every_column_upsert_writes(people_db: Path) -> None:
):
assert getattr(after, field) == getattr(before, field), f"{field} was not preserved"
assert after.role == "CEO"
+
+
+def test_drafts_resolving_to_one_row_by_title_apply_once(dept_db: Path) -> None:
+ """The shipped `hr` department is titled "People & Talent", so slug !=
+ slugify(title). Keying the dedupe on the drafted slug missed that: both
+ drafts resolved to the same row and the second silently clobbered the
+ first, while counts claimed two updates."""
+ hr = dept_store.get_department("hr", dept_db)
+ assert hr is not None and hr.config.title != "HR", "fixture assumption"
+
+ before = len(dept_store.list_departments(dept_db))
+ counts = reconcile_onboarding_departments(
+ [
+ DepartmentDraft(title="HR", mission="Recruiting and payroll"),
+ DepartmentDraft(title=hr.config.title, mission="Culture and L&D"),
+ ],
+ {},
+ )
+ assert counts == {"updated": 1, "created": 0}, "must not claim two updates"
+ assert len(dept_store.list_departments(dept_db)) == before
+
+ after = dept_store.get_department("hr", dept_db)
+ assert after is not None
+ assert after.config.charter.mission == "Recruiting and payroll", (
+ "the first draft must win, not be silently overwritten"
+ )
+
+
+def test_demoted_roster_member_loses_wildcard(people_db: Path) -> None:
+ """Someone who stays on the roster but is no longer principal falls through
+ _demote_stale_principals (they ARE in keep_ids) — they must lose WILDCARD
+ in the main loop or they keep blanket approval authority."""
+ from openexecutive.people.models import AuthorityScope
+
+ ids = save_onboarding_people([PersonDraft(full_name="Alice", is_principal=True)])
+ people_store.set_authority_scope(
+ ids["Alice"],
+ [AuthorityScope.WILDCARD, AuthorityScope.LEGAL_SIGN],
+ db_path=people_db,
+ )
+
+ save_onboarding_people(
+ [
+ PersonDraft(full_name="Alice", role="Founder", is_principal=False),
+ PersonDraft(full_name="Bob", role="CEO", is_principal=True),
+ ]
+ )
+
+ alice = people_store.get_person(ids["Alice"], db_path=people_db)
+ assert alice is not None
+ assert alice.is_principal is False
+ assert [s.value for s in alice.authority_scope] == ["legal_sign"]
+
+ principal = people_store.find_principal_person(db_path=people_db)
+ assert principal is not None and principal.full_name == "Bob"
+
+
+def test_case_variant_names_collapse_onto_one_row_not_two(people_db: Path) -> None:
+ """The upsert key is case-folded, so two spellings are one person. The
+ snapshot is refreshed in the loop so the second never inserts a duplicate."""
+ ids = save_onboarding_people(
+ [
+ PersonDraft(full_name="JANE DOE", role="CEO", is_principal=True),
+ PersonDraft(full_name="Jane Doe", role="Advisor"),
+ ]
+ )
+ assert len(set(ids.values())) == 1
+ everyone = people_store.list_people(db_path=people_db)
+ assert len(everyone) == 1, f"duplicate rows: {[p.full_name for p in everyone]}"
diff --git a/packages/core/tests/unit/test_onboarding_interview.py b/packages/core/tests/unit/test_onboarding_interview.py
index a0a97426..1c516d3c 100644
--- a/packages/core/tests/unit/test_onboarding_interview.py
+++ b/packages/core/tests/unit/test_onboarding_interview.py
@@ -389,6 +389,16 @@ def _draft(**overrides: Any) -> iv.CompanyDraft:
return iv.CompanyDraft.model_validate(_draft_dict(**overrides))
+def _details(errors: list[iv.DraftError]) -> list[str]:
+ """The model-facing rendering — the one that may quote input."""
+ return [e.detail for e in errors]
+
+
+def _safes(errors: list[iv.DraftError]) -> list[str]:
+ """The client-facing rendering — the one the route puts in a 422."""
+ return [e.safe for e in errors]
+
+
def test_validate_draft_accepts_a_good_draft() -> None:
assert iv.validate_draft(_draft()) == []
@@ -397,7 +407,7 @@ def test_validate_draft_requires_exactly_one_principal() -> None:
none_flagged = _draft(
people=[{"full_name": "Dana Reyes", "role": "CEO"}], departments=[]
)
- assert any("is_principal" in e for e in iv.validate_draft(none_flagged))
+ assert any("is_principal" in e for e in _details(iv.validate_draft(none_flagged)))
two_flagged = _draft(
people=[
@@ -406,7 +416,7 @@ def test_validate_draft_requires_exactly_one_principal() -> None:
],
departments=[],
)
- assert any("is_principal" in e for e in iv.validate_draft(two_flagged))
+ assert any("is_principal" in e for e in _details(iv.validate_draft(two_flagged)))
def test_validate_draft_rejects_duplicate_person_names() -> None:
@@ -417,12 +427,12 @@ def test_validate_draft_rejects_duplicate_person_names() -> None:
],
departments=[],
)
- assert any("duplicate full_name" in e for e in iv.validate_draft(dupes))
+ assert any("duplicate full_name" in e for e in _details(iv.validate_draft(dupes)))
def test_validate_draft_catches_unknown_department_head() -> None:
orphan = _draft(departments=[{"title": "Ops", "head_person_name": "Ghost"}])
- errors = iv.validate_draft(orphan)
+ errors = _details(iv.validate_draft(orphan))
assert any("not in the roster" in e for e in errors)
@@ -434,19 +444,19 @@ def test_validate_draft_rejects_colliding_department_titles() -> None:
colliding = _draft(
departments=[{"title": "Customer Success"}, {"title": "customer success"}]
)
- assert any("duplicate department title" in e for e in iv.validate_draft(colliding))
+ assert any("duplicate department title" in e for e in _details(iv.validate_draft(colliding)))
def test_validate_draft_requires_a_company_name() -> None:
nameless = _draft()
nameless.profile.name = " "
- assert any("profile.name" in e for e in iv.validate_draft(nameless))
+ assert any("profile.name" in e for e in _details(iv.validate_draft(nameless)))
def test_validate_draft_requires_at_least_one_person() -> None:
assert any(
"at least one person" in e
- for e in iv.validate_draft(_draft(people=[], departments=[]))
+ for e in _details(iv.validate_draft(_draft(people=[], departments=[])))
)
@@ -473,20 +483,116 @@ def test_agent_in_council_registry_but_not_specialist_registry() -> None:
def test_degenerate_titles_collide_like_the_store_would() -> None:
"""Two titles that both slugify to nothing land on one slug on insert."""
colliding = _draft(departments=[{"title": "###"}, {"title": "!!!"}])
- assert any("duplicate department title" in e for e in iv.validate_draft(colliding))
+ assert any("duplicate department title" in e for e in _details(iv.validate_draft(colliding)))
-def test_validate_draft_bounds_name_and_mission_lengths_without_echoing() -> None:
+def test_validate_draft_bounds_name_role_and_mission_lengths() -> None:
"""Bounds live here, not in Field(max_length=), so 422s can't echo input."""
long_person = _draft(
people=[{"full_name": "SECRETNAME" * 30, "is_principal": True}], departments=[]
)
- errors = iv.validate_draft(long_person)
- assert any("name is too long" in e for e in errors)
- assert not any("SECRETNAME" in e for e in errors)
+ assert any("name is too long" in e for e in _safes(iv.validate_draft(long_person)))
+
+ long_role = _draft(
+ people=[{"full_name": "Dana", "role": "R" * 500, "is_principal": True}],
+ departments=[],
+ )
+ assert any("role is too long" in e for e in _safes(iv.validate_draft(long_role)))
long_title = _draft(departments=[{"title": "T" * 300}])
- assert any("title is too long" in e for e in iv.validate_draft(long_title))
+ assert any(
+ "department name is too long" in e for e in _safes(iv.validate_draft(long_title))
+ )
long_mission = _draft(departments=[{"title": "Ops", "mission": "m" * 2500}])
- assert any("mission is too long" in e for e in iv.validate_draft(long_mission))
+ assert any(
+ "description is too long" in e for e in _safes(iv.validate_draft(long_mission))
+ )
+
+
+def test_validate_draft_bounds_profile_text() -> None:
+ """The profile renders into the CACHED system prompt on every turn, so an
+ unbounded field is a permanent per-request cost, not one big row."""
+ huge = _draft()
+ huge.profile.mission = "m" * (iv.MAX_PROFILE_TEXT_CHARS + 1)
+ assert any("mission is too long" in e for e in _safes(iv.validate_draft(huge)))
+
+ huge2 = _draft()
+ huge2.profile.name = "n" * (iv.MAX_PROFILE_TEXT_CHARS + 1)
+ assert any("name is too long" in e for e in _safes(iv.validate_draft(huge2)))
+
+
+def test_safe_error_strings_never_quote_input() -> None:
+ """The route returns .safe; only the model's repair turn sees .detail."""
+ leaky = _draft(
+ people=[
+ {"full_name": "SECRETPERSON", "is_principal": True},
+ {"full_name": "SECRETPERSON"},
+ ],
+ departments=[{"title": "SECRETDEPT", "head_person_name": "SECRETHEAD"}],
+ )
+ errors = iv.validate_draft(leaky)
+ assert errors
+ blob = " ".join(_safes(errors))
+ for secret in ("SECRETPERSON", "SECRETDEPT", "SECRETHEAD"):
+ assert secret not in blob, f"{secret} leaked into a client-facing string"
+ # The model, by contrast, must get the specifics to repair the draft.
+ assert "SECRETHEAD" in " ".join(_details(errors))
+
+
+def test_duplicate_person_names_are_matched_case_insensitively() -> None:
+ """save_onboarding_people upserts on a case-folded name, so a case-only
+ difference is the SAME person — a case-sensitive check let both through and
+ the upsert then collapsed them, potentially leaving no principal at all."""
+ variants = _draft(
+ people=[
+ {"full_name": "JANE DOE", "role": "CEO", "is_principal": True},
+ {"full_name": "Jane Doe", "role": "Advisor"},
+ ],
+ departments=[],
+ )
+ assert any("duplicate full_name" in e for e in _details(iv.validate_draft(variants)))
+
+
+def test_department_head_is_matched_case_insensitively() -> None:
+ ok = _draft(
+ people=[{"full_name": "Dana Reyes", "is_principal": True}],
+ departments=[{"title": "Ops", "head_person_name": "dana reyes"}],
+ )
+ assert iv.validate_draft(ok) == []
+
+
+def test_transcript_budget_allows_a_full_legal_start() -> None:
+ """A 20k description plus the documented 8 attachments is ~140k. A lower
+ ceiling locked the user out of the conversation on their first turn."""
+ from openexecutive.api.models import ONBOARD_MESSAGE_MAX_CHARS
+ from openexecutive.api.intake_uploads import (
+ _INTAKE_GEN_CHARS_PER_FILE,
+ _INTAKE_MAX_FILES,
+ )
+
+ largest_legal_start = (
+ ONBOARD_MESSAGE_MAX_CHARS + _INTAKE_MAX_FILES * _INTAKE_GEN_CHARS_PER_FILE
+ )
+ assert iv.MAX_TRANSCRIPT_CHARS > largest_legal_start
+
+
+async def test_transcript_never_ends_on_an_assistant_turn(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A draft records itself as an assistant turn, so "ask me more" then
+ "draft again" left one trailing — a prefill, which the API rejects
+ alongside a forced tool_choice."""
+ provider = _ScriptedProvider([_tool_response(iv.EMIT_TOOL_NAME, _draft_dict())])
+ _install(monkeypatch, provider)
+
+ transcript = [
+ iv.Turn(role="user", text="We are Northwind Tools, 40 people."),
+ iv.Turn(role="assistant", text="A bootstrapped industrial supplier."),
+ ]
+ await iv.advance(transcript, force_draft=True)
+
+ roles = [m["role"] for m in provider.calls[0]["messages"]]
+ assert roles[-1] == "user", f"trailing assistant prefill: {roles}"
+ assert all(a != b for a, b in zip(roles, roles[1:]))
diff --git a/packages/core/tests/unit/test_onboarding_interview_route.py b/packages/core/tests/unit/test_onboarding_interview_route.py
index 49d61103..6f01cd7c 100644
--- a/packages/core/tests/unit/test_onboarding_interview_route.py
+++ b/packages/core/tests/unit/test_onboarding_interview_route.py
@@ -508,11 +508,15 @@ def test_draft_records_an_assistant_turn_so_the_transcript_stays_alternating(
roles = [t.role for t in route._interview_sessions[sid].transcript]
assert all(a != b for a, b in zip(roles, roles[1:])), f"non-alternating: {roles}"
- # And the real builder accepts it.
- assert [
+
+ # And what the real builder would send alternates and ends on a user turn
+ # (never an assistant prefill, which a forced tool_choice rejects).
+ built = [
m["role"]
for m in iv._build_messages(route._interview_sessions[sid].transcript, None)
- ] == roles
+ ]
+ assert all(a != b for a, b in zip(built, built[1:])), f"non-alternating: {built}"
+ assert built[-1] == "user"
def test_a_failed_follow_up_keeps_the_draft_reviewable(
@@ -542,7 +546,7 @@ def test_commit_rejects_a_roster_with_no_principal(
body["departments"] = []
resp = client.post("/onboard/interview/commit", json=body)
assert resp.status_code == 422
- assert "is_principal" in resp.json()["detail"]
+ assert "exactly one person" in resp.json()["detail"]
assert not profile_path.exists()
@@ -567,7 +571,9 @@ def test_commit_rejects_an_unknown_department_head(
body["departments"] = [{"title": "Sales", "head_person_name": "Nobody Here"}]
resp = client.post("/onboard/interview/commit", json=body)
assert resp.status_code == 422
- assert "not in the roster" in resp.json()["detail"]
+ # The SAFE rendering — the detailed one quotes the rejected head name.
+ assert "isn't on the team list" in resp.json()["detail"]
+ assert "Nobody Here" not in resp.text
def test_commit_rejects_duplicate_person_names(
@@ -633,3 +639,59 @@ def test_displayed_question_budget_matches_the_enforced_one(
"""These were two separate constants that could drift silently."""
resp = client.post("/onboard/interview/start", data={"description": ""})
assert resp.json()["max_questions"] == iv.MAX_QUESTIONS
+
+
+def test_commit_422_never_echoes_the_rejected_value(
+ client: TestClient, seeded: list[Any]
+) -> None:
+ """validate_draft's detailed strings quote input; the route must return the
+ safe rendering, because a rejected name sits next to the financials."""
+ seeded.append(_draft())
+ sid = _start(client)
+ body = _commit_body(sid)
+ body["people"] = [
+ {"full_name": "SECRETPERSON", "is_principal": True},
+ {"full_name": "SECRETPERSON"},
+ ]
+ body["departments"] = [{"title": "Ops", "head_person_name": "SECRETHEAD"}]
+ resp = client.post("/onboard/interview/commit", json=body)
+ assert resp.status_code == 422
+ assert "SECRETPERSON" not in resp.text
+ assert "SECRETHEAD" not in resp.text
+
+
+def test_failed_first_turn_leaves_no_orphan_session(
+ client: TestClient, seeded: list[Any]
+) -> None:
+ """The 502 body carries no session_id, so the client can never resume it —
+ leaving it in the dict orphans it, and a burst of provider failures would
+ evict live sessions through the cap sweep."""
+ before = len(route._interview_sessions)
+ for _ in range(3):
+ seeded.append(iv.InterviewError("The setup assistant is unavailable right now."))
+ resp = client.post("/onboard/interview/start", data={"description": "hello"})
+ assert resp.status_code == 502
+ assert len(route._interview_sessions) == before
+
+
+def test_a_full_legal_start_does_not_lock_the_conversation(
+ client: TestClient, seeded: list[Any]
+) -> None:
+ """A big description plus the documented 8 attachments must still leave
+ room to keep talking."""
+ seeded.append(iv.Question(question="What stage are you?"))
+ resp = client.post(
+ "/onboard/interview/start",
+ data={"description": "x" * 19_000},
+ files=[
+ ("files", (f"f{i}.txt", b"y" * 14_000, "text/plain")) for i in range(8)
+ ],
+ )
+ assert resp.status_code == 200, resp.text
+ sid = resp.json()["session_id"]
+
+ seeded.append(iv.Question(question="And your north star?"))
+ follow_up = client.post(
+ "/onboard/interview/message", json={"session_id": sid, "message": "Series A."}
+ )
+ assert follow_up.status_code == 200, follow_up.text