Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,31 @@ things to preserve:
The sync's reconcile and `SleepRecordDeletion`'s restate must segment with the same bridge, or a
record delete would split a night the next sync merges back.

## An extended record's added minutes are asleep but unstaged (issue #106)

Editing (#82) can extend a record over time the ring missed, not only shorten it. The added minutes
are stored as `SleepStage.ADDED` blocks — **not** `UNKNOWN`, which is a stage byte the ring did
record and we can't read.

- **In time asleep, out of everything that judges stages.** `asleepMinutes` already admits them (it
excludes only AWAKE). Every score or stage share uses `measuredAsleepMinutes` / drops `addedMinutes`:
`sleepStageScore` in `reconcileWakingDay` and `SleepRecordDeletion.restateSession`, and
`SleepScore.calculate`. Health Connect gets `STAGE_TYPE_SLEEPING`. The hypnogram draws them as a
hatched band, not a lane.
- **They carry the ring record's `recordStartAt`**, so `sleepRecordRuns` keeps them in that record
and the covered gap stops counting as a between-record waking (#81) with no special case.
- **One function maps a record through an edit**: `editedRecordBlocks` (`SleepRecordExtension.kt`).
`SleepRecordDeletion.edit` and the import's edit gate both call it, so a re-sync writes back the
edit's blocks with the same `startAt`s. It ignores existing ADDED blocks and re-derives them from
the bounds, and snaps the bounds inwards onto the record's own minute grid. Don't hand-build
ADDED blocks anywhere else.
- **Limits** (`recordEditLimits`): back to the previous record's end, forward to the next record's
start, otherwise 2 h past the ring's own bounds, never across the 7 PM waking-day turnover, and a
record must keep one measured minute. At import, a stored extension is also clamped clear of any
other stored record, so the ring's measurement always beats an extension.
- **Known divergence**: the edit path only sees stored blocks. Re-extending over minutes an earlier
edit trimmed off makes them ADDED until the next sync, which restores the ring's real stages there.

## Long-lived services belong to the process, not the composition (issue #113)

**`RingBLEClient`, `RingSyncCoordinator`, `EventPersistenceSubscriber` and the other app-lifetime
Expand Down
42 changes: 39 additions & 3 deletions app/src/main/assets/i18n/pt-BR.json

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions app/src/main/java/com/pulseloop/AppServices.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.pulseloop.service.GpsRouteRecorder
import com.pulseloop.service.LiveWorkoutManager
import com.pulseloop.service.RingSyncCoordinator
import com.pulseloop.settings.ApiKeyStore
import com.pulseloop.widgets.WidgetSnapshotPublisher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
Expand All @@ -39,12 +38,8 @@ class AppServices private constructor(context: Context) {
val gpsRecorder = GpsRouteRecorder(context, db)
val liveWorkout = LiveWorkoutManager(coordinator, db, gpsRecorder, context)

// Every persisted ring-sync batch republishes the widget snapshot (debounced 2 s),
// mirroring the iOS PulseDataChange → WidgetSnapshotPublisher pipeline.
val persistence = EventPersistenceSubscriber(
context, db,
onDataPersisted = { WidgetSnapshotPublisher.publishDebounced(context) },
)
/** Process-wide, not per-[AppServices]: background workers need it without the UI's services. */
val persistence = EventPersistenceSubscriber.shared(context)
val batteryAlerts = BatteryAlertMonitor(context)
val providerStore = CoachProviderSettingsStore(context)
val summaryCoordinator = CoachSummaryCoordinator(db, apiKeyStore, providerStore)
Expand Down Expand Up @@ -85,6 +80,10 @@ class AppServices private constructor(context: Context) {
instance ?: synchronized(this) {
instance ?: AppServices(context.applicationContext).also { instance = it }
}

/** The services if the UI already built them, without building them: a background worker
* must not create the app's ring client, whose watchdog would start reconnecting. */
fun peek(): AppServices? = instance
}
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/pulseloop/PulseLoopApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.pulseloop.data.DataRepairs
import com.pulseloop.data.PulseLoopDatabase
import com.pulseloop.diagnostics.CrashLogger
import com.pulseloop.diagnostics.DiagnosticsSubscriber
import com.pulseloop.service.EventPersistenceSubscriber
import com.pulseloop.settings.QuietHoursPrefs
import com.pulseloop.widgets.WidgetRefreshWorker
import com.pulseloop.widgets.WidgetSnapshotPublisher
Expand Down Expand Up @@ -43,6 +44,9 @@ class PulseLoopApplication : Application() {
// What the ring saw the user doing, said after each sync (docs/activity-recognition-plan.md).
com.pulseloop.notifications.ActivityDetectionNotifier.createChannel(this)
com.pulseloop.notifications.ActivityDetectionNotifier.start(this, appScope)
// Same reason, for the ring data itself: a process launched only for RingSyncWorker never
// runs the UI's startup, and without a collector its events are dropped.
EventPersistenceSubscriber.shared(this).start()
// One-time cleanup of activity totals corrupted by the old bucket handling; runs off the
// main thread and is prefs-gated so it executes once. Safe to race the first sync — the
// ring can't connect before this completes a couple of DELETE statements.
Expand Down
89 changes: 81 additions & 8 deletions app/src/main/java/com/pulseloop/coach/gemini/GeminiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class GeminiClient(
private val apiKey: String,
model: String = GeminiModel.DEFAULT.slug,
private val baseURL: String = "https://generativelanguage.googleapis.com/v1beta/models",
// Network seam: a test can answer or fail requests without an HTTP stack.
private val transport: suspend (String, ByteArray) -> String = { u, b -> ResponsesHttp.post(u, b) },
) : ResponsesClient {
// The model the orchestrator selected, taken from each request body's `model`
// field (see `send`); the constructor value is only a fallback if that's absent.
Expand All @@ -54,6 +56,10 @@ class GeminiClient(
// Sources cited by a google_search turn, queued to prepend to the next user
// message so the schema turn can fill the response `sources` array.
private var pendingSourcesNote: String? = null
// Inputs of the last built body, so a refused grounding turn can be rebuilt
// as the plain schema turn it would otherwise have been (issue #123).
private var lastTextFormat: JsonObject? = null
private var lastThinkingLevel: String? = null

override suspend fun send(requestBody: ByteArray): OpenAIResponse {
if (apiKey.isBlank()) throw ResponsesError.MissingAPIKey
Expand All @@ -65,9 +71,23 @@ class GeminiClient(
}

val geminiBody = buildRequestBody(req)
val bodyBytes = json.encodeToString(JsonObject.serializer(), geminiBody).toByteArray()

val body = postWithOverloadRetry("$baseURL/$model:generateContent?key=$apiKey", bodyBytes)
val body = try {
post(geminiBody)
} catch (e: ResponsesError.Http) {
if (e.status != 429 || !isGroundingBody(geminiBody)) throw e
// A free-tier key can be refused `google_search` grounding outright (429
// RESOURCE_EXHAUSTED) while every other call on the same key succeeds (issue
// #123). Re-send this turn once without it — as the schema turn it would
// otherwise have been — so the answer still arrives. The rebuilt body carries
// no google_search, so this cannot loop; a 429 on it is a real quota refusal
// and surfaces as before.
val ungrounded = post(ungroundedRetryBody())
// Only now is it proven that grounding, not the key, was refused: stop asking
// for it on this key for the rest of the process so later questions don't pay
// the extra round trip. An app restart (e.g. after upgrading the plan) re-tries.
groundingRefusedKeys.add(apiKey)
ungrounded
}

val root = try {
json.parseToJsonElement(body).jsonObject
Expand Down Expand Up @@ -95,7 +115,7 @@ class GeminiClient(
url: String,
bodyBytes: ByteArray,
backoffMs: Long = OVERLOAD_BACKOFF_MS,
post: suspend (String, ByteArray) -> String = { u, b -> ResponsesHttp.post(u, b) },
post: suspend (String, ByteArray) -> String = transport,
): String {
for (attempt in 0..MAX_OVERLOAD_RETRIES) {
try {
Expand All @@ -108,9 +128,51 @@ class GeminiClient(
throw IllegalStateException("overload retry loop never returned")
}

private suspend fun post(geminiBody: JsonObject): String {
val bodyBytes = json.encodeToString(JsonObject.serializer(), geminiBody).toByteArray()
return postWithOverloadRetry("$baseURL/$model:generateContent?key=$apiKey", bodyBytes)
}

private fun isGroundingBody(body: JsonObject): Boolean =
(body["tools"] as? JsonArray)?.any { (it as? JsonObject)?.containsKey("google_search") == true } == true

/** The refused grounding turn rebuilt without `google_search` (issue #123): same
* conversation, the response schema applied, and a note so the answer says web
* search was skipped. `webSearchGrounded` is already set, so it never re-grounds. */
private fun ungroundedRetryBody(): JsonObject {
noteWebSearchSkipped()
return buildGeminiBody(emptyList(), lastTextFormat, lastThinkingLevel)
}

/** Tells the model, on the current user turn, that web search didn't run — so it
* leaves `sources` empty and says so in `data_quality_note`, which the chat shows
* under the answer. Prompt-level on purpose: no UI plumbing (issue #123). */
private fun noteWebSearchSkipped() {
val note = JsonObject(mapOf("text" to JsonPrimitive(WEB_SEARCH_SKIPPED_NOTE)))
val last = contents.lastOrNull()
if (last != null && (last["role"] as? JsonPrimitive)?.contentOrNull == "user") {
val parts = (last["parts"] as? JsonArray).orEmpty() + note
contents[contents.lastIndex] = JsonObject(last + ("parts" to JsonArray(parts)))
} else {
contents.add(JsonObject(mapOf(
"role" to JsonPrimitive("user"),
"parts" to JsonArray(listOf(note)),
)))
}
}

private companion object {
const val MAX_OVERLOAD_RETRIES = 2
const val OVERLOAD_BACKOFF_MS = 2_000L

const val WEB_SEARCH_SKIPPED_NOTE = "Web search was not available for this answer " +
"(Google refused Search grounding for this API key; free-tier keys often can't use it). " +
"Answer from the health data alone, leave `sources` empty, and say briefly in " +
"`data_quality_note` that web search was skipped."

// Keys whose grounding turn was refused with a 429 while the same turn succeeded
// without it (issue #123). Process-wide because each agent turn gets a fresh client.
val groundingRefusedKeys: MutableSet<String> = java.util.concurrent.ConcurrentHashMap.newKeySet()
}

// ── Request assembly (internal for unit tests) ───────────────────────
Expand All @@ -137,6 +199,8 @@ class GeminiClient(
appendContinuation(previousResponseId, input)
}

lastTextFormat = textFormat
lastThinkingLevel = thinkingLevel
return buildGeminiBody(convertTools(tools), textFormat, thinkingLevel)
}

Expand Down Expand Up @@ -362,12 +426,21 @@ class GeminiClient(
// and skip the schema. The grounded prose + cited sources land in history;
// the orchestrator's subsequent tool-less repair turn then enforces the
// coach_response schema (and can fill the `sources` array from them).
val groundingTurn = tools.isEmpty() && webSearchRequested && !webSearchGrounded
//
// A key already known to be refused grounding (issue #123) skips it here and gets
// the plain schema turn with a "web search skipped" note instead.
var groundingTurn = tools.isEmpty() && webSearchRequested && !webSearchGrounded
if (groundingTurn) {
body["tools"] = JsonArray(listOf(JsonObject(mapOf(
"google_search" to JsonObject(emptyMap()),
))))
webSearchGrounded = true
if (apiKey in groundingRefusedKeys) {
groundingTurn = false
noteWebSearchSkipped()
body["contents"] = JsonArray(contents)
} else {
body["tools"] = JsonArray(listOf(JsonObject(mapOf(
"google_search" to JsonObject(emptyMap()),
))))
}
}

// Gemini rejects function declarations combined with a JSON response
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/pulseloop/data/ArchivedSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ object ArchivedSettings {
"rest_mode" to emptySet(),
// Whether the coach may use the lab results.
"lab_reports" to emptySet(),
// Issue #103: whether estimated hourly SpO₂ is shown at all, and the app-started spot SpO₂
// the user chose (on/off, every 1–3 h). The worker re-checks the ring before each run, so
// a restore onto a phone with another ring (or none) measures nothing.
"scheduled_spo2" to emptySet(),
// Never "mcp_server": the network server's switch and paired-tool tokens are device state
// and security material, so a restore starts off with nothing paired (docs/mcp-threat-model.md).
)
Expand Down
85 changes: 52 additions & 33 deletions app/src/main/java/com/pulseloop/data/SleepRecordDeletion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,18 @@ object SleepRecordDeletion {
}

/**
* Adjust the declared boundaries of the ring record whose blocks carry [recordStartAt] to
* [newStartAt]..[newEndAt] (issue #82).
* Move the bounds of the ring record whose blocks carry [recordStartAt] to
* [newStartAt]..[newEndAt] (issue #82) — shorter, longer, or one side each (issue #106).
*
* Stages inside the new range stay; stages now outside it are removed and drop from session
* totals. The edit is recorded in [MeasurementDeletionDao] so it survives a re-sync.
* Stages outside the new range are removed and drop from session totals. Time inside it that
* the ring didn't measure becomes [SleepStage.ADDED] minutes: asleep, unstaged, unscored. The
* blocks come from [com.pulseloop.service.editedRecordBlocks], the function the import uses to
* re-apply the stored edit, so the next sync of the night writes back exactly these blocks.
*
* Returns true when the record was successfully updated.
* The bounds are held to [com.pulseloop.service.recordEditLimits] (the dialog's limits: the
* neighbouring record, two hours outward, the night's turnover) and clear of records in the
* day's other sessions, which the dialog doesn't see. Returns false when nothing changed or the
* new range keeps none of the ring's own minutes.
*/
suspend fun edit(
db: PulseLoopDatabase,
Expand All @@ -115,39 +120,51 @@ object SleepRecordDeletion {
val targetRecordStart = targets.first().recordStartAt.let { if (it > 0L) it else targets.first().startAt }
val day = session.date

// Trim each target block to [newStartAt, newEndAt)
val survivingTargets = mutableListOf<SleepStageBlockEntity>()
for (b in targets) {
val bStart = b.startAt
val bEnd = b.startAt + b.durationMinutes * 60_000L
val keepStart = maxOf(bStart, newStartAt)
val keepEnd = minOf(bEnd, newEndAt)

if (keepStart < keepEnd) {
val duration = ((keepEnd - keepStart) / 60_000L).toInt()
if (duration > 0) {
survivingTargets.add(
b.copy(
startAt = keepStart,
durationMinutes = duration,
)
)
}
}
}

// If trimming leaves no stages in this record, delete the record completely
if (survivingTargets.isEmpty()) {
return@withTransaction delete(db, sessionId, recordStartAt)
}
val (start, end) = clampEdit(db, session, blocks, targets, newStartAt, newEndAt)
val edited = com.pulseloop.service.editedRecordBlocks(targets, targetRecordStart, start, end)
?: return@withTransaction false

db.measurementDeletionDao().recordSleepEdit(day, targetRecordStart, newStartAt, newEndAt)
db.measurementDeletionDao().recordSleepEdit(day, targetRecordStart, start, end)

val nonTargets = blocks - targets.toSet()
restateSession(db, session, nonTargets + survivingTargets, bridge)
restateSession(db, session, nonTargets + edited, bridge)
true
}

/**
* [newStartAt]..[newEndAt] held to the limits the dialog applies, and clear of every block in
* the day's other sessions (issue #106). The dialog already clamps; this is the authority, so a
* stale dialog or another caller can't extend a record over a neighbour.
*/
private suspend fun clampEdit(
db: PulseLoopDatabase,
session: SleepSessionEntity,
blocks: List<SleepStageBlockEntity>,
targets: List<SleepStageBlockEntity>,
newStartAt: Long,
newEndAt: Long,
): Pair<Long, Long> {
val runs = com.pulseloop.service.sleepRecordRuns(blocks)
val first = targets.minOf { it.startAt }
val index = runs.indexOfFirst { run -> run.blocks.any { it.startAt == first } }
val run = runs.getOrNull(index) ?: com.pulseloop.service.sleepRecordRuns(targets).first()
val limits = com.pulseloop.service.recordEditLimits(
run = run,
previous = runs.getOrNull(index - 1).takeIf { index > 0 },
next = runs.getOrNull(index + 1).takeIf { index >= 0 },
wakingDay = session.date,
)
val start = newStartAt.coerceAtLeast(limits.earliestStart)
val end = newEndAt.coerceAtMost(limits.latestEnd)
val otherSessions = db.sleepSessionDao().ringAllByDay(session.date).filter { it.id != session.id }
val others = if (otherSessions.isEmpty()) emptyList()
else db.sleepStageBlockDao().forSessions(otherSessions.map { it.id })
val (nightStart, nightEnd) = com.pulseloop.service.nightWindow(session.date)
return com.pulseloop.service.clampEditToNeighbours(
start, end, limits.measuredStart, limits.measuredEnd, others, nightStart, nightEnd,
)
}

/** The next stamped record boundary after [after], or end-of-night — bounds the legacy fallback. */
private fun nextStampedStart(blocks: List<SleepStageBlockEntity>, after: Long): Long =
blocks.map { it.recordStartAt }.filter { it > after }.minOrNull() ?: Long.MAX_VALUE
Expand Down Expand Up @@ -188,14 +205,16 @@ object SleepRecordDeletion {
val totalMin = com.pulseloop.service.asleepMinutes(row.blocks)
val deepMin = row.blocks.filter { it.stageRaw == SleepStage.DEEP.name }
.sumOf { it.durationMinutes }
// Scored on the ring's own minutes, as `reconcileWakingDay` does (issue #106).
val measuredMin = com.pulseloop.service.measuredAsleepMinutes(row.blocks)
// Parent session BEFORE its blocks (FK sleep_stage_blocks -> sleep_sessions.id).
db.sleepSessionDao().upsert(
session.copy(
id = row.id,
startAt = row.startAt,
endAt = row.endAt,
totalMinutes = totalMin,
score = com.pulseloop.service.sleepStageScore(deepMin, totalMin),
score = com.pulseloop.service.sleepStageScore(deepMin, measuredMin),
updatedAt = now,
),
)
Expand Down
Loading
Loading