Repository navigation
fix(ios): join upload revocation claimed by removal cleanup - #294
Conversation
When a chat's lifetime removal cleanup claimed an attachment DELETE's credential revocation, it ran it in a detached, unobserved Task. The upload then returned before the installation purge ran, and that purge leaked into whatever ran next. In hosted CI it overlapped the following upload revocation test and, together with simulator stalls, timed both out (run 36295088605). The revocation now lives in one owned task. Removal cleanup may start it but never awaits it, which avoids a purge waiting on itself. Upload and removeAttachment join it, so they return only after the purge. The removal test now checks that the coordinator reads needsPairing once the upload returns. The check fails deterministically without the fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Hermes Review BotConfidence: 5 Engine: SummaryResolves a flaky test failure in the iOS test suite ( Confidence Score: 5/5Fully traced end-to-end through 📁 Important Files Changed
FindingsNo findings. 📊 Sequence DiagramsequenceDiagram
autonumber
actor Caller as upload()
participant VM as AidenChatViewModel
participant Server as Remote Server
participant Lifetime as onRemovalCleanup
participant Task as uploadRevocationTask
participant Coord as Coordinator
Caller->>VM: upload([attachment])
Lifetime->>VM: removeChat() / purge() cancels uploadTask
VM->>Server: DELETE /attachments/{id}
Server-->>VM: 401 credential_revoked
VM->>VM: deferUploadRevocation() records (error, context)
Lifetime->>VM: cleanupOwnedAttachmentsForRemoval()
VM->>Task: startDeferredUploadRevocation() (unawaited by cleanup)
Task->>Coord: handleCredentialRevocation(error, context)
Lifetime-->>Caller: removal cleanup finishes without deadlock
Caller->>VM: joinDeferredUploadRevocation()
VM->>Task: await revocation.value
Coord-->>Task: installation purge completes (needsPairing)
Task-->>VM: task finished
VM-->>Caller: upload returns after purge completes
Machine-Readable Findings[]
|
There was a problem hiding this comment.
Important
A subsequent revocation can replace the task handle while its purge is running, allowing a later joiner to return before installation data is purged.
Reviewed changes Reviewed the iOS deferred upload-revocation change and its lifecycle cleanup, regression assertion, and release/project notes.
- Owned revocation task. Deferred attachment-cleanup revocation is stored in one task; removal cleanup starts it without awaiting the purge it joins, while upload and explicit removal await it.
- Regression assertion. The removal/upload test checks that the coordinator reaches
.needsPairingwhen upload returns. - Documentation. The iOS changelog and attachment lifecycle memory note record the new completion behavior.
GPT Luna | 𝕏
A second cleanup revocation that arrived during an earlier purge replaced the task handle. Its coordinator call returns at once for the now-stale context, so a later joiner could return before the first purge finished. The replacement now awaits the previous task first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
ℹ️ No critical issues — one regression-coverage suggestion inline.
Reviewed changes Reviewed the follow-up that serializes deferred upload revocation behind an installation purge already in progress.
- Chained revocation tasks. Captured the current revocation task and awaited it before dispatching a replacement to the coordinator.
GPT Luna | 𝕏
| // the replacement must still finish only after the earlier purge. | ||
| let previous = uploadRevocationTask | ||
| uploadRevocationTask = Task { | ||
| await previous?.value |
There was a problem hiding this comment.
The current tests exercise only one revoked cleanup, so they would still pass if this wait were removed. Since the second-revocation-during-purge interleaving is the exact race this change prevents, please add a deterministic regression case that holds the first purge, triggers another revoked cleanup, and verifies a joiner cannot finish until the first purge completes.
Technical details
# Cover replacement revocation ordering
## Affected sites
- `ios/AidenOnTheGo/Features/Remote/AidenChatFeature.swift:2286` waits for earlier purge-owning tasks.
- `ios/AidenOnTheGoTests/AidenChatTests.swift:2731-2795` exercises a single revoked DELETE; `2797-2820` covers POST revocation, not overlapping cleanup revocations.
## Required outcome
- A test must fail if the replacement task no longer awaits the prior task, using a controlled in-flight purge and a subsequent `credential_revoked` cleanup response.There was a problem hiding this comment.
Acknowledged. A deterministic test would need to hold the first purge. The purge runs through purgeInstallationData, which touches shared singletons (the bot cache, draft store and Live Activities) and has no test seam. Adding that seam is a larger test-infrastructure change than this flake fix. I'm deferring it as a follow-up rather than adding a timing-dependent test.

Summary
testUploadCleanupRevocationDoesNotDeadlockRemovalandtestUploadPostRevocationCompletesPurgeWithoutSelfAwaitfailed together in the iOS simulator job. Failing run: https://github.com/sambitcreate/aiden-agent/actions/runs/36295088605. Both passed on a later fresh run.Root cause
When an attachment cleanup
DELETEreturnscredential_revoked,AidenChatViewModeldefers the credential revocation. Two paths could claim it:upload()awaitedhandleCredentialRevocation. That was correct.cleanupOwnedAttachmentsForRemoval, whichAidenChatCache.purge/removeChatjoin) must not await the purge that joins it. It started an unobserved, detachedTaskinstead.When removal cleanup claimed the revocation first,
upload()returned before the purge had run. That purge then ran during the next test and competed with it for the shared stores (bot cache, draft store, model preferences, chat cache, Live Activities). Instrumentation confirmed that the first test's purge ran inside the second test's time window.The hosted runner also had multi-second stalls in this run:
testMarkdownViewtook 66s against 0.04s locally. With the leaked purge added, the 2–3sfulfillmentwindows were exceeded, and the two tests failed as a pair. The detached task is the defect we can fix. The host stalls made it visible.Fix
uploadRevocationTask.upload()andremoveAttachmentjoin the task, so they return only after the revocation and purge have finished.Test
assertRemovalJoinsUploadCleanupnow assertscoordinator.connectionState == .needsPairingonce the upload returns. The coordinator sets that state only after the purge finishes."connected" is not equal to "needsPairing", on the first iteration) and passes with it.Validation (local, Xcode 27 beta, iPhone 17 Pro simulator)
-run-tests-until-failure.AidenChatTestsclass passed 3 iterations with-run-tests-until-failure.Android
This is iOS client concurrency only. No shared protocol, server contract or transcript UI changed, so Android needs no change.
🤖 Generated with Claude Code