Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): retry share rejection cleanup and preserve property lists

Chris85407a84c140
diff --git a/apps/mobile/docs/incoming-sharing.md b/apps/mobile/docs/incoming-sharing.md
index 2611f04915..0a367a9d98 100644
--- a/apps/mobile/docs/incoming-sharing.md
+++ b/apps/mobile/docs/incoming-sharing.md
@@ -16,7 +16,7 @@ It never dispatches a message just because an external app opened Cindy.
| Safari preprocessing result | Page title, URL and selection when Safari supplies them |
| Image (including UIImage / raw data) | Attachment; HEIC/HEIF/TIFF/BMP follow the existing JPEG conversion path; vector/source encodings remain original files |
| Movie, audio | File attachment |
-| PDF, Office, HTML/RTF, Markdown, JSON, archives | Original exported file |
+| PDF, Office, HTML/RTF, Markdown, JSON, property lists, archives | Original exported file |
| vCard, calendar, models, custom data types | Original exported file through the generic data adapter |
| File URL / security-scoped document | Owned copy, retaining its filename |
| Directory / document package | Coordinated upload snapshot (ZIP) |
@@ -64,7 +64,9 @@ Provider temporary URLs are copied **inside** the `loadFileRepresentation` or
Security-scoped access is balanced; directories use `NSFileCoordinator.forUploading`.
Each input owns a unique App Group directory, so equal filenames cannot overwrite.
The native slot is acknowledged only by the selected, focused, hydrated composer;
-account changes and upload cleanup use the existing ownership fences. The existing
+account changes and upload cleanup use the existing ownership fences. Rejection-marker
+removal is acknowledged only after a successful durable flush; failures retain the
+marker for retry, including when the App Group slot has already been cleared. The existing
native inbox is a single persistent slot, with an in-process JS queue; this change does
not promise a durable history of multiple shares received before Cindy opens.
diff --git a/apps/mobile/modules/cindy-incoming-share/ios/IncomingShareSlot.swift b/apps/mobile/modules/cindy-incoming-share/ios/IncomingShareSlot.swift
index c6044eec54..e9aa4041a0 100644
--- a/apps/mobile/modules/cindy-incoming-share/ios/IncomingShareSlot.swift
+++ b/apps/mobile/modules/cindy-incoming-share/ios/IncomingShareSlot.swift
@@ -18,10 +18,9 @@ enum IncomingShareSlot {
clear: (Data) throws -> Bool) throws -> Bool {
try reject(expected, in: rejections)
let cleared = try clear(expected)
- if cleared {
- rejections.removeObject(forKey: rejectedKey)
- rejections.synchronize()
- }
+ // A false comparison also means the old bytes are absent or replaced.
+ // Finish the rejection even when a previous attempt already cleared the slot.
+ try removeRejection(expected, from: rejections)
return cleared
}
@@ -30,13 +29,22 @@ enum IncomingShareSlot {
guard let rejected = rejections.data(forKey: rejectedKey) else { return snapshot }
if let snapshot, snapshot == rejected {
// Retry clearing, but never return rejected bytes even if it still fails.
- guard (try? clear(snapshot)) == true else { return nil }
+ do { _ = try clear(snapshot) } catch { return nil }
}
- rejections.removeObject(forKey: rejectedKey)
- rejections.synchronize()
+ // Empty/replaced snapshots are safe too, but a failed flush must leave a
+ // marker in this process so subsequent reads can retry its durable removal.
+ try? removeRejection(rejected, from: rejections)
return snapshot == rejected ? nil : snapshot
}
+ private static func removeRejection(_ rejected: Data, from defaults: UserDefaults) throws {
+ defaults.removeObject(forKey: rejectedKey)
+ guard defaults.synchronize() else {
+ defaults.set(rejected, forKey: rejectedKey)
+ throw POSIXError(.EIO)
+ }
+ }
+
static func withLock<T>(at url: URL, _ operation: () throws -> T) throws -> T {
let fd = open(url.path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)
guard fd >= 0 else { throw POSIXError(.EIO) }
diff --git a/apps/mobile/plugins/incoming-share-formats.swift b/apps/mobile/plugins/incoming-share-formats.swift
index bd7f33e584..a2876d6abd 100644
--- a/apps/mobile/plugins/incoming-share-formats.swift
+++ b/apps/mobile/plugins/incoming-share-formats.swift
@@ -17,7 +17,8 @@
for (format, kind) in formats where provider.hasItemConformingToTypeIdentifier(format.identifier) {
if let file = await loadShareFile(provider, format: format, kind: kind) { return file }
}
- // Concrete exports win over thumbnails as well as text/link previews.
+ // Concrete exports (including ordinary property lists) win over previews.
+ // Only the positively identified Safari envelope above is composer text.
if let file = await loadShareFile(provider, format: .data, kind: .file, excludePlainText: true, excludeImages: true) { return file }
// Web links may also advertise a thumbnail; retain the actionable URL.
if provider.hasItemConformingToTypeIdentifier(UTType.url.identifier),
@@ -58,7 +59,7 @@
let identifiers = provider.registeredTypeIdentifiers.filter {
let type = UTType($0)
return type?.conforms(to: format) == true && type?.conforms(to: .url) != true
- && (!excludePlainText || (!textPreviews.contains($0) && type?.conforms(to: .propertyList) != true))
+ && (!excludePlainText || !textPreviews.contains($0))
&& (!excludeImages || ($0 != UTType.data.identifier && type?.conforms(to: .image) != true))
}
for identifier in identifiers {
diff --git a/apps/mobile/scripts/test-incoming-share-formats.mjs b/apps/mobile/scripts/test-incoming-share-formats.mjs
index 39879d3b46..18bc4e37bc 100644
--- a/apps/mobile/scripts/test-incoming-share-formats.mjs
+++ b/apps/mobile/scripts/test-incoming-share-formats.mjs
@@ -81,6 +81,29 @@ final class Adapter {
guard let payload = await adapter.parse(provider) else { fatalError("Missing text preview") }
precondition(payload.type == .text && payload.value == "shared passage")
}
+ // A concrete property list is a document, not Safari's preprocessing envelope.
+ for type in [UTType.propertyList, .xmlPropertyList, .binaryPropertyList] {
+ let bytes = try! PropertyListSerialization.data(fromPropertyList: ["original": "document"], format: type == .binaryPropertyList ? .binary : .xml, options: 0)
+ for preview in [UTType.image, .url, .plainText] {
+ let provider = NSItemProvider()
+ provider.suggestedName = "original.plist"
+ provider.registerItem(forTypeIdentifier: preview.identifier, loadHandler: { done, _, _ in
+ done?(preview == .url ? NSURL(string: "https://example.com/preview")! : NSData(data: Data("preview only".utf8)), nil)
+ })
+ provider.registerDataRepresentation(forTypeIdentifier: type.identifier, visibility: .all) { done in done(bytes, nil); return nil }
+ guard let payload = await adapter.parse(provider), payload.type == .file,
+ let url = URL(string: payload.value) else { fatalError("Preview replaced concrete property list: " + type.identifier + "/" + preview.identifier) }
+ precondition(url.lastPathComponent == "original.plist" && payload.mimeType == type.preferredMIMEType)
+ precondition(try! Data(contentsOf: url) == bytes)
+ }
+ }
+ let safari = NSItemProvider()
+ safari.registerItem(forTypeIdentifier: UTType.propertyList.identifier, loadHandler: { done, _, _ in
+ done?([NSExtensionJavaScriptPreprocessingResultsKey: ["title": "Page title", "url": "https://example.com/page", "selection": "Selected text"]] as NSDictionary, nil)
+ })
+ guard let page = await adapter.parse(safari) else { fatalError("Missing Safari page") }
+ precondition(page.type == .text && page.value == "Page title\\n\\nhttps://example.com/page\\n\\nSelected text")
+ print("PASS: concrete property lists beat previews; Safari preprocessing remains text")
let selected = CommandLine.arguments[2]
let link = NSItemProvider()
let sharedURL = URL(string: "https://example.com/shared-page?source=provider")!
diff --git a/apps/mobile/scripts/test-incoming-share-slot.swift b/apps/mobile/scripts/test-incoming-share-slot.swift
index 51e44630fa..36d57c627d 100644
--- a/apps/mobile/scripts/test-incoming-share-slot.swift
+++ b/apps/mobile/scripts/test-incoming-share-slot.swift
@@ -2,6 +2,23 @@
import Foundation
import Darwin
+// Fault injection separates process cache from durable bytes. A failed flush
+// must not be reported as successful removal or erase the retry state in memory.
+final class RejectionDefaults: UserDefaults {
+ var cached: Data?
+ var durable: Data?
+ var failRemoval = true
+ init(_ persisted: Data?) { cached = persisted; durable = persisted; super.init(suiteName: UUID().uuidString)! }
+ override func data(forKey key: String) -> Data? { cached }
+ override func set(_ value: Any?, forKey key: String) { cached = value as? Data }
+ override func removeObject(forKey key: String) { cached = nil }
+ override func synchronize() -> Bool {
+ if cached == nil && failRemoval { return false }
+ durable = cached
+ return true
+ }
+}
+
@main
enum IncomingShareSlotTest {
static func main() throws {
@@ -94,6 +111,47 @@ enum IncomingShareSlotTest {
let discarded = try IncomingShareSlot.discard(a, rejections: rejections) { _ in true }
precondition(discarded)
precondition(IncomingShareSlot.read(a, rejections: restarted) { _ in fatalError("Already cleared") } == a)
+ let failingDiscard = RejectionDefaults(nil)
+ var snapshot: Data? = a
+ var reportedSuccess = false
+ do {
+ _ = try IncomingShareSlot.discard(a, rejections: failingDiscard) { expected in
+ precondition(snapshot == expected)
+ snapshot = nil
+ return true
+ }
+ reportedSuccess = true
+ } catch {}
+ precondition(!reportedSuccess, "Discard falsely acknowledged a failed tombstone removal")
+ precondition(failingDiscard.cached == a && failingDiscard.durable == a)
+ // Retrying after the App Group slot was already cleared must still finish.
+ precondition(IncomingShareSlot.read(snapshot, rejections: failingDiscard) { _ in fatalError("Slot is empty") } == nil)
+ precondition(failingDiscard.cached == a && failingDiscard.durable == a)
+ let cold = RejectionDefaults(failingDiscard.durable)
+ cold.failRemoval = false
+ precondition(IncomingShareSlot.read(snapshot, rejections: cold) { _ in fatalError("Slot is empty") } == nil)
+ precondition(cold.durable == nil)
+ // A new identical text/URL snapshot after recovery is no longer rejected.
+ let recovered = RejectionDefaults(cold.durable)
+ precondition(IncomingShareSlot.read(a, rejections: recovered) { _ in fatalError("New share") } == a)
+ let retryDiscard = RejectionDefaults(a)
+ retryDiscard.failRemoval = false
+ let alreadyGone = try IncomingShareSlot.discard(a, rejections: retryDiscard) { _ in false }
+ precondition(!alreadyGone && retryDiscard.durable == nil && retryDiscard.cached == nil)
+ let failingRead = RejectionDefaults(a)
+ precondition(IncomingShareSlot.read(a, rejections: failingRead) { _ in true } == nil)
+ precondition(failingRead.cached == a && failingRead.durable == a, "Read lost tombstone removal retry state")
+ failingRead.failRemoval = false
+ precondition(IncomingShareSlot.read(nil, rejections: failingRead) { _ in fatalError("Slot is empty") } == nil)
+ precondition(failingRead.cached == nil && failingRead.durable == nil)
+ // A newer share remains readable even when stale-marker cleanup cannot flush.
+ let newer = RejectionDefaults(a)
+ precondition(IncomingShareSlot.read(b, rejections: newer) { _ in fatalError("Never clear newer bytes") } == b)
+ precondition(newer.cached == a && newer.durable == a)
+ newer.failRemoval = false
+ precondition(IncomingShareSlot.read(b, rejections: newer) { _ in fatalError("Never clear newer bytes") } == b)
+ precondition(newer.durable == nil)
+ print("PASS: failed rejection removal retains retry state until durable recovery")
print("PASS: durable exact-snapshot rejection, retry and replacement")
print("PASS: cross-process exclusion, stale/current/duplicate acknowledgement, error unlock")
}
diff --git a/apps/mobile/src/__tests__/incomingShareNativePlugin.test.ts b/apps/mobile/src/__tests__/incomingShareNativePlugin.test.ts
index 4092489841..aa5df0ac2a 100644
--- a/apps/mobile/src/__tests__/incomingShareNativePlugin.test.ts
+++ b/apps/mobile/src/__tests__/incomingShareNativePlugin.test.ts
@@ -155,6 +155,8 @@ it("preserves original exports ahead of previews and copies temporary URLs withi
expect(result.stdout).toContain("PASS: concrete Markdown/source files and plain text previews");
expect(result.stdout).toContain("PASS: original documents/videos beat thumbnails; real images remain images");
expect(result.stdout).toContain("PASS: web URL survives its image thumbnail");
+ expect(result.stdout).toContain("PASS: concrete property lists beat previews; Safari preprocessing remains text");
+ expect(result.stdout).toContain("PASS: failed rejection removal retains retry state until durable recovery");
expect(result.stdout).toContain("PASS: temporary file URL copied inside provider callback");
expect(result.stdout).toContain("PASS: durable exact-snapshot rejection, retry and replacement");
}, 180_000);