Skip to content

Preserve Android turn receipts across response ownership changes - #227

Merged
sambitcreate merged 2 commits into
feature/mobile-response-ownershipfrom
feature/mobile-turn-receipt-ownership
Sep 26, 2026
Merged

sambitcreate merged 2 commits into
feature/mobile-response-ownershipfrom
feature/mobile-turn-receipt-ownership

Conversation

@sambitcreate

@sambitcreate sambitcreate commented Sep 22, 2026 •

Copy link
Copy Markdown
Owner

A successful Android turn receipt could arrive after another chat view settled the transcript and replace the newer messages. It could also recreate a purged stream, or treat a local disk failure as a failed send after the server had already accepted the POST.

Reserve ownership before the POST and admit the receipt atomically. Preserve the newer transcript and metadata, merge the accepted message once under request-origin authority, and keep stream ownership separate so an older receipt cannot replace a newer stream. Removal and purge invalidate the original receipt authority even after fresh chat re-admission. Accepted receipts retain an in-memory recovery path when persistence fails; no POST is replayed.

This focused draft is stacked on green PR225. PR217/225 heads are unchanged. iOS rename/list/Bot response ownership and physical acceptance remain separate assigned work.

Validation:

  • The production two-owner held-POST regression fails on parent152577eeb by losing the settled assistant reply; it passes with this change.
  • Coverage includes newer snapshots with/without the receipt, a newer second POST, purge/removal plus re-admission, detail/stream disk failures, unpair/client switches, same-ID re-pairing, and failure to replace a seeded older disk marker.
  • 186 Android JVM tests, lintDebug, and instrumentation compilation pass.
  • Both independent GPT-5.6 Sol medium reviews clear the actual source and tests.
  • No iOS or server contract changes. Physical device remains locked; no physical test claimed.

Evidence: .memory/mobile-turn-receipt-ownership.md. No merge, release, or deployment.

Completion-order correction: an accepted receipt no longer reserves a new detail or summary ticket. Both POST-first and GET-first held response orders preserve the later GET’s authoritative fields and the accepted stream. Requests already in flight at receipt acceptance retain missing receipt messages through a bounded overlay; a fresh post-receipt snapshot retires it, and deletion/purge clear it. Both inverse ordering failures were executed before correction, including a GET that omitted the accepted message. Fresh-snapshot retirement and delayed-old-read rejection are covered. Final local evidence:186 JVM tests/lint/instrumentation compilation, with both independent GPT-5.6 Sol medium final reviews clear for this correction; hosted CI must run on the corrected head.

Concurrent POST receipts accumulate all still-pending accepted messages in either completion order. The held two-POST regression fails before this correction and now preserves both messages once, the newer stream, and exactly two submitted POSTs.

Exact-head hosted acceptance:3f7154e7b2f9830bf3573ee00334ffc5ec121ddb passes verify, Android build/APK and Deterministic Electron E2E (workflow35770159276; E2E completed2026-09-22 19:26:51 UTC). No unresolved review threads. Combined integration691aed63d accepted213 Android tests/157 host tests and both independent reviews; newer combined3c23dad2b preserves this correction. Physical acceptance remains unexecuted; broader iOS ownership work remains separate and open.

@sambitcreate
sambitcreate marked this pull request as ready for review September 24, 2026 18:13
@pullfrog

pullfrog Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Run failed. View the logs →

Pullfrog  | Rerun failed job ➔ | View workflow run | via Pullfrog | Using GPT Luna | 𝕏

@very-hermes-bot

Copy link
Copy Markdown
Collaborator

Hermes Review Bot

Confidence: 5

Engine: agy/gemini-3.8-flash-high
Review mode: full
Head: 3f7154e7b2f9830bf3573ee00334ffc5ec121ddb
Generated: 2026-09-24T18:35:43+00:00
Reviews: 1

Summary

Android turn receipt processing now reserves a cache write token prior to initiating a turn request and admits 202 Accepted responses through acceptTurnReceipt rather than minting new response-time tokens. Receipts are folded into the current transcript candidate or existing admitted winner under the original request authority, using an in-memory overlay (receiptMessages) to ensure in-flight GET requests that lack the new message still retain it until a fresh snapshot retires the overlay. Stream ownership is tracked independently with an in-memory fallback (receiptStreams) so local disk persistence failures no longer treat accepted turns as failed sends or replay mutations. Maintainers should double-check that future background sync or chat refresh callers continue to use reserveChatWrite() before dispatching HTTP GET requests to ensure proper overlay retirement.

Confidence Score: 5/5

Fully traced and verified end-to-end across AidenChatViewModel, AidenChatCache, ordering permutations (POST-first vs. GET-first), pairing lifecycle boundaries, and persistence failure paths.

📁 Important Files Changed
  • android/app/src/main/java/sbtbiswas/AidenOnTheGo/features/chat/AidenChatViewModel.kt: Reserves write tokens before dispatching startTurn, validates retained pairing identity using createdAt, routes responses through chatCache.acceptTurnReceipt, and prevents disk I/O errors from triggering draft restoration after a 202 response.
  • android/app/src/main/java/sbtbiswas/AidenOnTheGo/persistence/AidenChatCache.kt: Implements acceptTurnReceipt, chronological receipt message merging, bounded in-flight overlays (receiptMessages), and in-memory active stream ownership (receiptStreams) with cleanup on removal or purge.
  • android/app/src/test/java/sbtbiswas/AidenOnTheGo/AidenChatTest.kt: Introduces end-to-end tests exercising 14 race and failure scenarios (inverse GET/POST arrival, concurrent POST ordering, in-flight missing messages, pairing switch/re-pair, removal, purge, and disk write blockages).
  • .memory/mobile-turn-receipt-ownership.md: Records project memory, reproduction traces for baseline negatives, and verification logs for completion-order corrections.

Findings

No findings.

📊 Sequence Diagram
sequenceDiagram
    autonumber
    participant VM as AidenChatViewModel
    participant Cache as AidenChatCache
    participant Server as Aiden Server
    participant SSE as Event Stream

    VM->>Cache: reserveChatWrite() (requestToken)
    VM->>Server: POST /turns (with idempotencyKey)
    Server-->>VM: 202 Accepted (turnId, streamId, message)
    VM->>Cache: acceptTurnReceipt(candidate, message, stream, instanceId, requestToken)
    activate Cache
    Cache->>Cache: mergeReceipt into admitted winner
    Cache->>Cache: register receipt in receiptMessages overlay
    Cache->>Cache: update receiptStreams in memory
    Cache-->>VM: AcceptedTurn(chat, stream)
    deactivate Cache
    VM->>VM: Update _chat StateFlow
    VM->>SSE: startStreaming(accepted.stream)
Loading

Machine-Readable Findings

[]

Last reviewed commit: 3f7154e7b2f9
Reviews (1) · Comment /hermes review to trigger a new review · /hermes review full for full re-review

@sambitcreate
sambitcreate merged commit 19e689d into feature/mobile-response-ownership Sep 26, 2026
5 of 6 checks passed
sambitcreate added a commit that referenced this pull request Sep 26, 2026
* fix(android): fence deferred home cache writes

* fix(android): reject superseded cross-owner chat reads

* fix(android): order Home summary requests and pagination commits

* Preserve Android turn receipts across response ownership changes (#227)

* Preserve Android accepted turn receipts across response ownership changes

* Keep receipt merges under request authority in both completion orders
sambitcreate added a commit that referenced this pull request Sep 26, 2026
* Fence Android chat reads and Home summary admission (#225)

* fix(android): fence deferred home cache writes

* fix(android): reject superseded cross-owner chat reads

* fix(android): order Home summary requests and pagination commits

* Preserve Android turn receipts across response ownership changes (#227)

* Preserve Android accepted turn receipts across response ownership changes

* Keep receipt merges under request authority in both completion orders

(cherry picked from commit 1a3cd3e)

* Preserve iOS Bot navigation and live permission ownership (#233)

* Own iOS Bot navigation responses and live permission publication

* fix(ios): keep in-flight Bot opens and same-policy grants through restoration

Restoration re-ran on an open's own path change and superseded it, and it
reset an already-presented chat's live mutation grant to false before
revalidating, leaving it disabled after a transient failure. Restoration now
adopts an in-flight open under identical inputs, and keeps a prior live grant
only when the write and notice policy are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GAkZRgQzUoKWGpgupF3a4D

* fix(ios): preserve newer detail state across workspace list responses (#234)

* fix(ios): admit workspace lists against newer detail owners

* fix(ios): preserve accepted rename titles and coherent chat revisions (#236)

* fix(ios): preserve rename receipts without replacing newer chat state

* Preserve authoritative list titles across detail hydration

* Fence iOS Home responses and preserve admitted row ownership (#238)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

(cherry picked from commit ccb6415)

Conflict resolution against #242: saveChats keeps #242's committed-write
ordering (older lists are partial companions, rows come from successful
detail commits, and the list token is the max). #233's in-memory list,
rename-title and fetch authority stay. The durable list extends only the
last admitted rows, so a detail whose disk write failed stays a memory-only
winner on the read path and never becomes a durable row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GAkZRgQzUoKWGpgupF3a4D

* test(mobile): cover #217 x #218 run controls on iOS and Android

Integration coverage for the merged stream-recovery and acknowledged
run-control state machines:

- iOS: removal while a stop, approval response or approval snapshot is
  held cannot report, restore a card or re-read; URLError(.cancelled) on
  stop/approval leaves the run live without optimistic cancel or card
  resurrection, and a second Stop may retry.
- Android: unpair during a held stop or approval snapshot, and a dropped
  stop write that is not retried. #217 fixtures now encode the Mac wire
  shape (absent optionals omitted) so main's reasoning validator accepts
  them.

(cherry picked from commit d41ad06)

Conflict resolution against #242: #242 already added the cancelledControls
mode, its URLProtocol failure path and the load()-based mismatched-approval
delivery, so those duplicates were dropped. The cancelled Stop test now
follows #242's silent-cancellation rule (no "Stop was not confirmed").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GAkZRgQzUoKWGpgupF3a4D

* fix(ios): resolve the 9 failing AidenChatTests on #217

Executed on the iPhone 17 Pro simulator for the first time, the stacked
ownership changes exposed real interaction bugs with #217:

- Workspace list merges pulled a removed chat's later detail-only
  re-admission into a list reserved before the removal.
- Chat removal minted a fresh summary reservation and cleanup-era Home
  writes needed the full floor, so unrelated pending-era rows were lost.
  Removal keeps its deletion token; partial writes are row updates.

Test-only corrections: whole-second dates for ISO 8601 round trips,
create the cache root before blocking its chats directory, and assert
that a fresh detail re-admission owns its Home row (#238 projection).

(cherry picked from commit 37b5920)

Conflict resolution against #242: #242 already moved the cached-stream
check ahead of draft I/O in load(), so that duplicate was dropped. Its
committed-write ordering also admits an older create list's own row when
the create's detail committed after the omitting list, so create() no
longer needs the `!saveChats` fallback. admitCreatedWorkspaceChat stays
for the superseded and failed-write create paths carried next.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GAkZRgQzUoKWGpgupF3a4D

* fix(ios): superseded create/send read the admitted chat winner

A superseded saveChat fell back to cache.loadChat, which reads disk only.
saveChat admits the winner in memory before its file write, so a failed
winner write left no file (row removed, stream startup aborted) or an
older snapshot (rebase onto stale data). Both sites now use
cache.admittedChat. Adds failed-write regression tests for both paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GAkZRgQzUoKWGpgupF3a4D
(cherry picked from commit eb2dc82)

* fix(ios): keep a superseded or failed create's row in the durable list

After adopting the admitted winner, the superseded create branch (and the
detail disk-failure branch) returned before any workspace-list write, so a
failed detail write with a writable list cache lost the created row on an
offline cold reopen. Both branches now call admitCreatedWorkspaceChat,
which also accepts a newer detail winner only while that winner is newer
than the list token, so later removal, purge and newer list omissions win.

(cherry picked from commit 1ad6af1)

Adjusted for #242: the helper extends the admitted list rows themselves,
not the merged memory view, so only the created chat (not other chats'
memory-only detail winners) becomes a durable row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GAkZRgQzUoKWGpgupF3a4D

* docs(memory): record the #217 follow-up carry onto main

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GAkZRgQzUoKWGpgupF3a4D

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@sambitcreate
sambitcreate deleted the feature/mobile-turn-receipt-ownership branch September 26, 2026 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants