Automation foundation model for tiny devices: 2-bit, 8-29 MB, tool calls, ASR, structured extraction and embeddings on phones, wearables, smart homes, robots, cars and microcontrollers.
fix(grounding): keep the date fact when a system key ends in "date:" (#151)
brekena1367ac4dc55
diff --git a/needle/__init__.py b/needle/__init__.pyindex 973897b..2f5d722 100644--- a/needle/__init__.py+++ b/needle/__init__.py@@ -443,11 +443,14 @@ def _source_years(text):_ISO_STAMP = re.compile(r"\d{4}-\d{2}-\d{2}")+# The `date:` key itself, not the tail of another key such as `update:` or+# `candidate:`, which would otherwise leave the engine without a date.+_DATE_KEY = re.compile(r"(?<![A-Za-z])date:")def _with_date_fact(system: str) -> str:"""Prefix the local date fact unless the caller already supplied one."""- if "date:" in system or _ISO_STAMP.search(system):+ if _DATE_KEY.search(system) or _ISO_STAMP.search(system):return systemnow = datetime.datetime.now()fact = now.strftime("date: %Y-%m-%d %a %H:%M")diff --git a/tests/test_grounding.py b/tests/test_grounding.pyindex 195fb42..b642b86 100644--- a/tests/test_grounding.py+++ b/tests/test_grounding.py@@ -167,6 +167,21 @@ def test_system_facts_license_relative_dates(stub):assert "validation" not in response+def test_a_key_ending_in_date_does_not_suppress_the_date_fact(stub):+ import needle++ tomorrow = datetime.date.today() + datetime.timedelta(days=1)+ stub.envelopes = [_call(tomorrow.isoformat())]+ agent = needle.Needle(tools=[Invoice], system="user: Sam; last update: never")+ response = agent.complete("invoice Acme tomorrow for the March 2019 order")++ assert "validation" not in response+ assert agent._system_text.startswith("date: ")+ assert needle._with_date_fact("candidate: Sam").startswith("date: ")+ for system in ("date: 2026-07-21 Tue 14:30", "locale: en-US; date: Tue"):+ assert needle._with_date_fact(system) == system++def test_years_carry_across_turns_until_reset(stub):import needle