Skip to content

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): stop reading the hour of a time as a year (#154)

brekenf06f5e49b33c
diff --git a/needle/__init__.py b/needle/__init__.py
index 131b419..11c012e 100644
--- a/needle/__init__.py
+++ b/needle/__init__.py
@@ -428,8 +428,11 @@ def _source_years(text):
r"jun(?:e)?|jul(?:y)?|aug(?:ust)?|sep(?:t(?:ember)?)?|"
r"oct(?:ober)?|nov(?:ember)?|dec(?:ember)?)")
patterns = [
- rf"\b\d{{1,2}}(?:st|nd|rd|th)?\s+{months}[\s,]+(\d{{1,4}})(?![0-9A-Za-z])",
- rf"\b{months}\s+\d{{1,2}}(?:st|nd|rd|th)?\s*,?\s+(\d{{1,4}})(?![0-9A-Za-z])",
+ # The number after a day and month is its year, unless it is the hour of
+ # a time ("5 June 19:30", "June 5 7 pm"): no ISO argument can carry
+ # that as a year, so licensing it would reject every date.
+ rf"\b\d{{1,2}}(?:st|nd|rd|th)?\s+{months}[\s,]+(\d{{1,4}})(?![0-9A-Za-z]|:\d|\s*[ap]\.?m\b)",
+ rf"\b{months}\s+\d{{1,2}}(?:st|nd|rd|th)?\s*,?\s+(\d{{1,4}})(?![0-9A-Za-z]|:\d|\s*[ap]\.?m\b)",
rf"\b{months}[\s,]+(\d{{3,4}})(?![0-9A-Za-z])",
r"\byear\s+(\d{1,4})(?![0-9A-Za-z])",
# Year-first numeric dates only (2024-03-15, 2024/03/15). A day- or
diff --git a/tests/test_grounding.py b/tests/test_grounding.py
index 2cc2657..c05d584 100644
--- a/tests/test_grounding.py
+++ b/tests/test_grounding.py
@@ -387,3 +387,26 @@ def test_run_still_refuses_a_non_numeric_engine_flag(stub):
response = agent.run("make it 21 and cool the room")
assert response["results"] == [{"error": "ungrounded mode"}]
+
+
+def test_a_time_after_a_day_and_month_is_not_read_as_a_year(stub):
+ import needle
+
+ stub.envelopes = [_call("2026-06-05")]
+ agent = needle.Needle(tools=[Invoice])
+ response = agent.complete("Send an invoice to Acme due on 5 June 19:30")
+
+ assert "validation" not in response
+ assert needle._source_years("due on 5 June 19:30") == set()
+ assert needle._source_years("due on June 5 7 pm") == set()
+ assert needle._source_years("due on June 5, 2031 19:30") == {2031}
+ assert needle._source_years("due on 5 June 2031") == {2031}
+
+
+def test_extract_accepts_a_date_followed_by_a_time(stub):
+ import needle
+
+ stub.envelopes = [_call("2026-03-03")]
+ invoice = needle.extract("Invoice from Acme Corp due March 3 14:00", Invoice)
+
+ assert invoice.due_date == datetime.date(2026, 3, 3)