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
8 changes: 8 additions & 0 deletions .memory/durable-attachment-lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ Existing test files extended (already registered in package scripts): remote upl
Hosted verify caught the existing subagent deletion source-contract test matching the old one-line `if (releaseAdmission) finishDeletion()` literally. Updated it to assert the conditional block releases attachment admission followed by generation admission after the pending-tombstone check. No production behavior change was needed.

Pullfrog found that the extra revoked-device Set would outlive the registry's 128-entry pruning. Removed that unnecessary set and its direct-call-after-revoke test. Production uploads are tracked by router authorization through completion; state revocation blocks acquisitions and drains mutations before cleanup. Sol re-review confirmed this resolution. Active service leases remain invalidated, bounded, and released on settlement.

## iOS deferred upload revocation (2026-09-30)

`AidenChatViewModel` keeps the credential revocation found by an attachment cleanup DELETE in one owned `uploadRevocationTask`.
- Lifetime removal cleanup, which `AidenChatCache.purge` and `removeChat` join, may start that task but never awaits it. Awaiting it there would make the purge wait on itself.
- `upload()` and `removeAttachment` join the task. When they return, the installation purge has finished and the coordinator reads `needsPairing`.
- Previously the cleanup path detached an unobserved `Task`. Its purge leaked into later tests and made the upload revocation tests flaky in hosted CI (run 36295088605).
- This is iOS client logic only. It changes no protocol, and Android needs no change.
50 changes: 35 additions & 15 deletions ios/AidenOnTheGo/Features/Remote/AidenChatFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ final class AidenChatViewModel {
@ObservationIgnored private var ownedUploadReferences: [String: (AidenAttachmentReference, AidenRemoteRequestContext, AidenRemoteClient)] = [:]
@ObservationIgnored private var attachmentCleanupTasks: [String: Task<Void, Never>] = [:]
@ObservationIgnored private var uploadRevocation: (AidenRemoteClientError, AidenRemoteRequestContext)?
@ObservationIgnored private var uploadRevocationTask: Task<Void, Never>?
@ObservationIgnored private var attachmentPreparationTask: Task<Void, Never>?
private var attachmentPreparationID: UUID?
var isPreparingAttachments: Bool { attachmentPreparationID != nil }
Expand Down Expand Up @@ -2258,11 +2259,7 @@ final class AidenChatViewModel {
task.cancel()
}
uploadTask = nil
let revocation = uploadRevocation
uploadRevocation = nil
if let (error, context) = revocation {
_ = await coordinator.handleCredentialRevocation(error, context: context)
}
await joinDeferredUploadRevocation()
return failures
}

Expand All @@ -2272,6 +2269,35 @@ final class AidenChatViewModel {
return true
}

/// Starts the deferred revocation at most once. Whichever path claims it —
/// the upload/removal caller or a lifetime cleanup that purge joins — the
/// handling lives in one owned task, so the caller can await it without
/// the cleanup ever awaiting the purge that joins it.
@discardableResult
private func startDeferredUploadRevocation() -> Task<Void, Never>? {
if let (error, context) = uploadRevocation {
uploadRevocation = nil
let coordinator = coordinator
// A later revocation may arrive while an earlier purge is running;
// its coordinator call returns at once for the stale context, so
// the replacement must still finish only after the earlier purge.
let previous = uploadRevocationTask
uploadRevocationTask = Task {
await previous?.value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

_ = await coordinator.handleCredentialRevocation(error, context: context)
}
}
return uploadRevocationTask
}

/// Callers outside lifetime cleanup finish only after revocation has been
/// handled, even when removal cleanup claimed it first.
private func joinDeferredUploadRevocation() async {
guard let revocation = startDeferredUploadRevocation() else { return }
await revocation.value
if uploadRevocationTask == revocation { uploadRevocationTask = nil }
}

private func performUpload(_ uploads: [AidenAttachmentUpload]) async -> Int {
guard !uploads.isEmpty else { return 0 }
guard !isReadOnlyPresentation, !isRemoved else { return uploads.count }
Expand Down Expand Up @@ -2384,12 +2410,9 @@ final class AidenChatViewModel {
for id in Set(ownedUploadReferences.keys).union(attachmentCleanupTasks.keys) {
await cleanupOwnedAttachment(id)
}
if let (error, context) = uploadRevocation {
uploadRevocation = nil
// Do not await a purge from the lifetime cleanup that purge joins.
let coordinator = coordinator
Task { _ = await coordinator.handleCredentialRevocation(error, context: context) }
}
// Do not await a purge from the lifetime cleanup that purge joins. The
// upload caller joins the owned task instead.
startDeferredUploadRevocation()
}

@discardableResult
Expand All @@ -2401,10 +2424,7 @@ final class AidenChatViewModel {
guard !isReadOnlyPresentation, !isStarting, !isRemoved,
pendingAttachments.contains(where: { $0.id == attachment.id }) else { return }
await cleanupOwnedAttachment(attachment.id)
if let (error, context) = uploadRevocation {
uploadRevocation = nil
_ = await coordinator.handleCredentialRevocation(error, context: context)
}
await joinDeferredUploadRevocation()
}

func attachmentImageData(for attachment: AidenMessageAttachment) async -> Data? {
Expand Down
9 changes: 8 additions & 1 deletion ios/AidenOnTheGoTests/AidenChatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2733,7 +2733,8 @@ final class AidenChatTests: XCTestCase {
defer { try? FileManager.default.removeItem(at: root) }
let gate = AidenChatWriteTestGate()
let cache = AidenChatCache(root: root, beforeAttachmentImageWrite: { await gate.waitIfArmed() })
let model = try await makeProgressLifecycleModel(mode: .denied, cache: cache)
var coordinator: AidenRemoteCoordinator!
let model = try await makeProgressLifecycleModel(mode: .denied, cache: cache, onCoordinator: { coordinator = $0 })
let png = UIGraphicsImageRenderer(size: CGSize(width: 2, height: 2)).pngData { $0.fill(CGRect(x: 0, y: 0, width: 2, height: 2)) }
AidenChatProgressLifecycleURLProtocol.setResponseOverride { request in
if request.httpMethod == "DELETE" {
Expand Down Expand Up @@ -2776,6 +2777,12 @@ final class AidenChatTests: XCTestCase {
await completion.value
let failures = await uploading.value
XCTAssertEqual(failures, 2)
if revokedCleanup {
// Whichever of upload or removal cleanup claims the revoked DELETE,
// the installation purge must be finished when the upload returns;
// the coordinator only reports needsPairing after that purge.
XCTAssertEqual(coordinator.connectionState, .needsPairing)
}
XCTAssertEqual(AidenChatProgressLifecycleURLProtocol.uploadRequestCount, 1)
XCTAssertEqual(AidenChatProgressLifecycleURLProtocol.attachmentDeleteCount, 1)
XCTAssertTrue(model.pendingAttachments.isEmpty)
Expand Down
1 change: 1 addition & 0 deletions ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- An attachment upload whose cleanup finds the device revoked now finishes only after the installation data has been purged, even when chat removal started the revocation.
- Tool calls whose arguments are still streaming now read "Preparing <tool>" in activity rows, matching Aiden Agent, instead of claiming the work already started.

- Converted the imported SwiftUI foundation into the Aiden On The Go product identity and signed application shell.
Expand Down
Loading