Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

fix(lens): a timestamp from earlier today is still a timestamp

Durganshu Mishra6eb0cae6628d
diff --git a/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt b/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt
index 134b682..f124151 100644
--- a/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt
+++ b/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt
@@ -433,6 +433,14 @@ class GotchaAccessibilityService : AccessibilityService() {
count++
}
if (count == 0 || unionRect.width() <= 0 || unionRect.height() <= 0) continue
+ // A date nobody can see is a formatted timestamp. Every other type
+ // survives on a description alone — a phone number read out to a
+ // screen reader is still a phone number — but a node rendering
+ // "10 hours ago" while describing itself as a date is the one case
+ // where the description exists precisely because the value is not an
+ // event. Tense catches these once they are a day old; this catches
+ // them while they are still today.
+ if (derivedOnly && entity.type == EntityType.CALENDAR) continue
val weighted = if (derivedOnly) {
entity.copy(confidence = entity.confidence * SmartActionDetector.DERIVED_TEXT_CONFIDENCE_SCALE)
} else {
diff --git a/app/src/main/java/com/gotcha/service/SmartActionDetector.kt b/app/src/main/java/com/gotcha/service/SmartActionDetector.kt
index 5ff264a..6194d44 100644
--- a/app/src/main/java/com/gotcha/service/SmartActionDetector.kt
+++ b/app/src/main/java/com/gotcha/service/SmartActionDetector.kt
@@ -2,6 +2,7 @@ package com.gotcha.service
import java.time.DayOfWeek
import java.time.LocalDate
+import java.time.LocalDateTime
import java.time.LocalTime
import java.util.regex.Pattern
@@ -193,7 +194,10 @@ object SmartActionDetector {
"\\s+(at\\s+)?\\d{1,2}(:\\d{2})?\\s?([ap])\\.?\\s?m\\.?" +
"|\\b(jan(uary)?|feb(ruary)?|mar(ch)?|apr(il)?|may|jun(e)?|jul(y)?|aug(ust)?|" +
"sep(t)?(ember)?|oct(ober)?|nov(ember)?|dec(ember)?)\\s+\\d{1,2}(st|nd|rd|th)?" +
- "(,?\\s+\\d{4})?(\\s+(at\\s+)?\\d{1,2}(:\\d{2})?\\s?([ap])\\.?\\s?m\\.?)?" +
+ // The time tail may be separated by a comma, not just a space:
+ // "Aug 1, 2026, 9:14 AM" is how a formatted timestamp reads, and
+ // stopping at the year threw away the very thing that dates it.
+ "(,?\\s+\\d{4})?(,?\\s+(at\\s+)?\\d{1,2}(:\\d{2})?\\s?([ap])\\.?\\s?m\\.?)?" +
"|\\b\\d{1,2}(:\\d{2})?\\s?([ap])\\.?\\s?m\\.?",
Pattern.CASE_INSENSITIVE
)
@@ -264,7 +268,7 @@ object SmartActionDetector {
allowChat: Boolean = false,
targetCurrency: String = "USD",
targetLanguage: String = "English",
- today: LocalDate = LocalDate.now()
+ now: LocalDateTime = LocalDateTime.now()
): List<DetectedEntity> {
if (text.isBlank()) return emptyList()
@@ -292,7 +296,7 @@ object SmartActionDetector {
detectCurrencies(text, rawEntities, targetCurrency)
// 7. Calendar
- detectCalendars(text, rawEntities, today)
+ detectCalendars(text, rawEntities, now)
// 8. Tracking numbers
detectTracking(text, rawEntities)
@@ -804,7 +808,19 @@ object SmartActionDetector {
event
).joinToString(PAYLOAD_SEP)
- private fun detectCalendars(text: String, out: MutableList<DetectedEntity>, today: LocalDate) {
+ /**
+ * Whether a day (and clock time, when one was given) is still to come.
+ *
+ * Compared against the moment, not the date: "merged 10 hours ago" resolves
+ * to *earlier today*, which is not before today and so used to pass a
+ * date-only check. A day with no time on it stays eligible for the whole of
+ * that day, because "lunch today" is a real event at 9am and at 9pm.
+ */
+ private fun isStillAhead(date: LocalDate, time: LocalTime?, now: LocalDateTime): Boolean =
+ if (time != null) date.atTime(time).isAfter(now) else !date.isBefore(now.toLocalDate())
+
+ private fun detectCalendars(text: String, out: MutableList<DetectedEntity>, now: LocalDateTime) {
+ val today = now.toLocalDate()
val m = calendarPattern.matcher(text)
while (m.find()) {
val event = m.group().trim()
@@ -816,7 +832,8 @@ object SmartActionDetector {
// the parse is needed anyway to give the calendar intent a real start
// time instead of a raw string to guess at.
val date = eventDateOf(event, today)
- if (date != null && date.isBefore(today)) continue
+ val timeOfDay = timeOfDayOf(event)
+ if (date != null && !isStillAhead(date, timeOfDay, now)) continue
val confidence = when {
eventKeywordPattern.matcher(event).find() -> CONFIDENCE_EVENT_WORDED
@@ -829,7 +846,7 @@ object SmartActionDetector {
val actions = listOf(
SmartAction(
label = "📅 Add to calendar: ${snippet(event, 24)}",
- prompt = encode(TYPE_CALENDAR, calendarPayload(event, date, timeOfDayOf(event))),
+ prompt = encode(TYPE_CALENDAR, calendarPayload(event, date, timeOfDay)),
actionType = ActionType.NATIVE_CALENDAR,
isPrimary = true
),
diff --git a/app/src/test/java/com/gotcha/service/SmartActionDetectorTest.kt b/app/src/test/java/com/gotcha/service/SmartActionDetectorTest.kt
index 4108c89..18ed193 100644
--- a/app/src/test/java/com/gotcha/service/SmartActionDetectorTest.kt
+++ b/app/src/test/java/com/gotcha/service/SmartActionDetectorTest.kt
@@ -337,7 +337,7 @@ class SmartActionDetectorTest {
fun `past dates are not calendar events`() {
val entities = SmartActionDetector.detectAll(
"Rebase #53 by DevUser2 was merged Jul 26, 2026",
- today = LocalDate.of(2026, 8, 1)
+ now = NOON_AUG_1
)
assertTrue(
"A merge timestamp is not something to put on a calendar",
@@ -349,7 +349,7 @@ class SmartActionDetectorTest {
fun `future dates are still calendar events`() {
val entities = SmartActionDetector.detectAll(
"Design review Aug 14, 2026",
- today = LocalDate.of(2026, 8, 1)
+ now = NOON_AUG_1
)
assertTrue(entities.any { it.type == EntityType.CALENDAR })
}
@@ -372,14 +372,44 @@ class SmartActionDetectorTest {
@Test
fun `an event word outranks a bare time`() {
- val today = LocalDate.of(2026, 8, 1)
- val worded = SmartActionDetector.detectAll("Dinner tomorrow", today = today)
+ val worded = SmartActionDetector.detectAll("Dinner tomorrow", now = NOON_AUG_1)
.first { it.type == EntityType.CALENDAR }
- val bare = SmartActionDetector.detectAll("Doors open 8 pm", today = today)
+ val bare = SmartActionDetector.detectAll("Doors open 8 pm", now = NOON_AUG_1)
.first { it.type == EntityType.CALENDAR }
assertTrue(bare.confidence < worded.confidence)
}
+ @Test
+ fun `a timestamp from earlier today is still a timestamp`() {
+ // The gap the first pass left: "merged 10 hours ago" resolves to *today*,
+ // which is not before today, so a date-only check let it through.
+ val entities = SmartActionDetector.detectAll(
+ "rebase #64 by DevUser2 was merged Aug 1, 2026, 9:14 a.m.",
+ now = NOON_AUG_1
+ )
+ assertTrue(
+ "A merge from this morning is not an event this afternoon",
+ entities.none { it.type == EntityType.CALENDAR }
+ )
+ }
+
+ @Test
+ fun `an event later today survives`() {
+ val entities = SmartActionDetector.detectAll("Dinner today at 8 pm", now = NOON_AUG_1)
+ assertTrue(entities.any { it.type == EntityType.CALENDAR })
+ }
+
+ @Test
+ fun `a day with no clock time stays eligible all day`() {
+ // "lunch today" is a real event at 9am and at 9pm — with no time given
+ // there is nothing to compare against, so the day is the granularity.
+ val entities = SmartActionDetector.detectAll(
+ "Lunch today",
+ now = LocalDate.of(2026, 8, 1).atTime(23, 30)
+ )
+ assertTrue(entities.any { it.type == EntityType.CALENDAR })
+ }
+
// ---- Annotation selection ----
/** The screen from the bug report: a PR list whose every row carries a merge timestamp. */
@@ -397,7 +427,7 @@ class SmartActionDetectorTest {
@Test
fun `a list of merge timestamps produces no calendar annotations`() {
- val entities = SmartActionDetector.detectAll(pullRequestList, today = LocalDate.of(2026, 8, 1))
+ val entities = SmartActionDetector.detectAll(pullRequestList, now = NOON_AUG_1)
val selected = SmartActionDetector.selectForAnnotation(entities)
assertTrue(
"Merge timestamps should not be annotated at all",
@@ -409,7 +439,7 @@ class SmartActionDetectorTest {
@Test
fun `annotations are capped however busy the screen is`() {
- val entities = SmartActionDetector.detectAll(pullRequestList, today = LocalDate.of(2026, 8, 1))
+ val entities = SmartActionDetector.detectAll(pullRequestList, now = NOON_AUG_1)
val selected = SmartActionDetector.selectForAnnotation(entities)
assertTrue(
"Expected at most ${SmartActionDetector.MAX_ANNOTATIONS}, got ${selected.size}",
@@ -491,15 +521,15 @@ class SmartActionDetectorTest {
// ---- Calendar payloads carry a resolved start ----
- private fun calendarPayload(text: String, today: LocalDate): String {
- val entity = SmartActionDetector.detectAll(text, today = today)
+ private fun calendarPayload(text: String, now: java.time.LocalDateTime): String {
+ val entity = SmartActionDetector.detectAll(text, now = now)
.first { it.type == EntityType.CALENDAR }
return SmartActionDetector.decode(entity.primaryAction!!.prompt)!!.second
}
@Test
fun `a dated event carries the resolved day and time`() {
- val payload = calendarPayload("Design review Aug 14, 2026 at 9:30 a.m.", LocalDate.of(2026, 8, 1))
+ val payload = calendarPayload("Design review Aug 14, 2026 at 9:30 a.m.", NOON_AUG_1)
val parts = payload.split("|", limit = 3)
assertEquals("2026-08-14", parts[0])
assertEquals("09:30", parts[1])
@@ -508,7 +538,7 @@ class SmartActionDetectorTest {
@Test
fun `an event with no clock time leaves the time field blank`() {
- val payload = calendarPayload("Team meeting on Monday", LocalDate.of(2026, 8, 5))
+ val payload = calendarPayload("Team meeting on Monday", LocalDate.of(2026, 8, 5).atStartOfDay())
val parts = payload.split("|", limit = 3)
// Wednesday Aug 5 → the coming Monday. A calendar app handed the word
// "Monday" would have dropped this on today instead.
@@ -523,4 +553,9 @@ class SmartActionDetectorTest {
assertEquals(java.time.LocalTime.of(15, 0), SmartActionDetector.timeOfDayOf("3 p.m."))
assertNull(SmartActionDetector.timeOfDayOf("sometime Monday"))
}
+
+ private companion object {
+ /** Midday on the day the bug-report screenshots were taken. */
+ val NOON_AUG_1: java.time.LocalDateTime = LocalDate.of(2026, 8, 1).atTime(12, 0)
+ }
}