fix(routines): enforce min_interval_hours and minoxidil flag server-side
Two bugs in /routines/suggest where the LLM could override hard constraints: 1. Products with min_interval_hours (e.g. retinol at 72h) were passed to the LLM even if used too recently. The LLM reasoned away the constraint in at least one observed case. Fix: added _filter_products_by_interval() which removes ineligible products before the prompt is built, so they don't appear in AVAILABLE PRODUCTS at all. 2. Minoxidil was included in the available products list regardless of the include_minoxidil_beard flag. Only the objectives context was gated, leaving the product visible to the LLM which would include it based on recent usage history. Fix: added include_minoxidil param to _get_available_products() and threaded it through suggest_routine and suggest_batch. Also refactored _build_products_context() to accept a pre-supplied products list instead of calling _get_available_products() internally, ensuring the tool handler and context text always use the same filtered set. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
7a66a7911d
commit
e3ed0dd3a3
2 changed files with 144 additions and 12 deletions
|
|
@ -15,6 +15,7 @@ from innercontext.api.routines import (
|
|||
_ev,
|
||||
_extract_active_names,
|
||||
_extract_requested_product_ids,
|
||||
_filter_products_by_interval,
|
||||
_get_available_products,
|
||||
_is_minoxidil_product,
|
||||
)
|
||||
|
|
@ -204,9 +205,8 @@ def test_build_products_context(session: Session):
|
|||
session.add(s)
|
||||
session.commit()
|
||||
|
||||
ctx = _build_products_context(
|
||||
session, time_filter="am", reference_date=date.today()
|
||||
)
|
||||
products_am = _get_available_products(session, time_filter="am")
|
||||
ctx = _build_products_context(session, products_am, reference_date=date.today())
|
||||
# p1 is medication but not minoxidil (wait, Regaine name doesn't contain minoxidil!) -> skipped
|
||||
assert "Regaine" not in ctx
|
||||
|
||||
|
|
@ -215,9 +215,8 @@ def test_build_products_context(session: Session):
|
|||
session.add(p1)
|
||||
session.commit()
|
||||
|
||||
ctx = _build_products_context(
|
||||
session, time_filter="am", reference_date=date.today()
|
||||
)
|
||||
products_am = _get_available_products(session, time_filter="am")
|
||||
ctx = _build_products_context(session, products_am, reference_date=date.today())
|
||||
assert "Regaine Minoxidil" in ctx
|
||||
assert "Sunscreen" in ctx
|
||||
assert "inventory_status={active:2,opened:1,sealed:1}" in ctx
|
||||
|
|
@ -375,6 +374,106 @@ def test_extract_active_names_uses_compact_distinct_names(session: Session):
|
|||
assert names == ["Niacinamide", "Zinc PCA"]
|
||||
|
||||
|
||||
def test_get_available_products_excludes_minoxidil_when_flag_false(session: Session):
|
||||
minoxidil = Product(
|
||||
id=uuid.uuid4(),
|
||||
name="Minoxidil 5%",
|
||||
category="hair_treatment",
|
||||
is_medication=True,
|
||||
brand="Test",
|
||||
recommended_time="both",
|
||||
leave_on=True,
|
||||
product_effect_profile={},
|
||||
)
|
||||
regular = Product(
|
||||
id=uuid.uuid4(),
|
||||
name="Cleanser",
|
||||
category="cleanser",
|
||||
brand="Test",
|
||||
recommended_time="both",
|
||||
leave_on=False,
|
||||
product_effect_profile={},
|
||||
)
|
||||
session.add_all([minoxidil, regular])
|
||||
session.commit()
|
||||
|
||||
# With flag True (default) - minoxidil included
|
||||
products = _get_available_products(session, include_minoxidil=True)
|
||||
names = {p.name for p in products}
|
||||
assert "Minoxidil 5%" in names
|
||||
assert "Cleanser" in names
|
||||
|
||||
# With flag False - minoxidil excluded
|
||||
products = _get_available_products(session, include_minoxidil=False)
|
||||
names = {p.name for p in products}
|
||||
assert "Minoxidil 5%" not in names
|
||||
assert "Cleanser" in names
|
||||
|
||||
|
||||
def test_filter_products_by_interval():
|
||||
today = date.today()
|
||||
|
||||
p_no_interval = Product(
|
||||
id=uuid.uuid4(),
|
||||
name="No Interval",
|
||||
category="serum",
|
||||
brand="Test",
|
||||
recommended_time="both",
|
||||
leave_on=True,
|
||||
product_effect_profile={},
|
||||
)
|
||||
p_interval_72 = Product(
|
||||
id=uuid.uuid4(),
|
||||
name="Retinol",
|
||||
category="serum",
|
||||
brand="Test",
|
||||
recommended_time="pm",
|
||||
leave_on=True,
|
||||
min_interval_hours=72,
|
||||
product_effect_profile={},
|
||||
)
|
||||
p_interval_48 = Product(
|
||||
id=uuid.uuid4(),
|
||||
name="AHA",
|
||||
category="exfoliant",
|
||||
brand="Test",
|
||||
recommended_time="pm",
|
||||
leave_on=True,
|
||||
min_interval_hours=48,
|
||||
product_effect_profile={},
|
||||
)
|
||||
|
||||
last_used = {
|
||||
str(p_interval_72.id): today - timedelta(days=1), # used yesterday -> need 3 days
|
||||
str(p_interval_48.id): today - timedelta(days=3), # used 3 days ago -> 48h ok
|
||||
}
|
||||
|
||||
products = [p_no_interval, p_interval_72, p_interval_48]
|
||||
result = _filter_products_by_interval(products, today, last_used)
|
||||
result_names = {p.name for p in result}
|
||||
|
||||
assert "No Interval" in result_names # always included
|
||||
assert "Retinol" not in result_names # used 1 day ago, needs 3 -> blocked
|
||||
assert "AHA" in result_names # used 3 days ago, needs 2 -> ok
|
||||
|
||||
|
||||
def test_filter_products_by_interval_never_used_passes():
|
||||
today = date.today()
|
||||
p = Product(
|
||||
id=uuid.uuid4(),
|
||||
name="Retinol",
|
||||
category="serum",
|
||||
brand="Test",
|
||||
recommended_time="pm",
|
||||
leave_on=True,
|
||||
min_interval_hours=72,
|
||||
product_effect_profile={},
|
||||
)
|
||||
# no last_used entry -> should pass
|
||||
result = _filter_products_by_interval([p], today, {})
|
||||
assert len(result) == 1
|
||||
|
||||
|
||||
def test_product_details_tool_handler_returns_product_payloads(session: Session):
|
||||
p = Product(
|
||||
id=uuid.uuid4(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue