Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
home/ios: keep the search index current after a save (#775)
nyblnet45bcfb91771b
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 1aff7e00..d346e4ba 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -894,6 +894,12 @@ jobs:# both key blobs, and recovered scalars checked through CryptoKit. Apple only.run: node scripts/test-home-id-recovery.mjs+ - name: home/ios Spotlight rig+ # What the system search index holds for a document and keeping it current+ # after a save: replaced at once, delete-before-add when sealed, and+ # entries removed for documents a walk no longer finds. Apple only.+ run: node scripts/test-home-spotlight.mjs+- name: WebExtension package rig# Validates the store package without writing it: every file the# manifest names exists, every icon is the size it claims, no unexpected@@ -2757,6 +2763,7 @@ jobs:test-home-id-statements.mjstest-home-id-agent.mjstest-home-id-recovery.mjs+ test-home-spotlight.mjs)for rig in "${rigs[@]}"; doecho "::group::$rig"diff --git a/home/ios/EditorViewController.swift b/home/ios/EditorViewController.swiftindex d242b4e0..2dc6cc12 100644--- a/home/ios/EditorViewController.swift+++ b/home/ios/EditorViewController.swift@@ -691,14 +691,23 @@ final class EditorViewController: UIViewController, WKScriptMessageHandler, WKURlet text = (m["text"] as? String) ?? ""let name = (m["name"] as? String) ?? ""if targetsOpenDocument(name) {- document.html = Data(text.utf8)- // updateChangeCount + autosave is the sanctioned path: it- // coordinates with iCloud and file coordination rather than- // writing behind UIDocument's back.- document.updateChangeCount(.done)- document.save(to: document.fileURL, for: .forOverwriting) { [weak self] ok in- if ok, let self, let h = self.originHint { OriginStore.shared.refreshHint(h, for: self.document.fileURL) }- self?.reply(id, ok: ok, value: ok ? nil : "write failed")+ let bytes = Data(text.utf8)+ let sealed = BentoIndex.isEncrypted(Array(String(decoding: bytes.prefix(BentoIndex.headBytes), as: UTF8.self).utf16))+ // a sealed file's index entry goes BEFORE the write; the change+ // count is set inside, so autosave can't write first+ LibraryIndex.shared.beforeSave(document.fileURL, sealed: sealed) { [weak self] in+ guard let self else { return }+ self.document.html = bytes+ // updateChangeCount + autosave is the sanctioned path: it+ // coordinates with iCloud and file coordination rather than+ // writing behind UIDocument's back.+ self.document.updateChangeCount(.done)+ self.document.save(to: self.document.fileURL, for: .forOverwriting) { [weak self] ok in+ // the library and Spotlight see this save now, not at the next walk+ if ok, let url = self?.document.fileURL { LibraryIndex.shared.refresh(url) }+ if ok, let self, let h = self.originHint { OriginStore.shared.refreshHint(h, for: self.document.fileURL) }+ self?.reply(id, ok: ok, value: ok ? nil : "write failed")+ }}} else {switch exports.write(name: name, text: text, id: id) {diff --git a/home/ios/LibraryIndex.swift b/home/ios/LibraryIndex.swiftindex 93780ba4..0fb76e66 100644--- a/home/ios/LibraryIndex.swift+++ b/home/ios/LibraryIndex.swift@@ -122,16 +122,73 @@ final class LibraryIndex {self.walk(folders, cache: snapshot)}DispatchQueue.main.async {+ let gone = SpotlightPlan.removals(before: self.documents.map(\.path), after: found.map(\.path))self.cache = freshself.documents = found.sorted { $0.modified > $1.modified }self.save()- self.donate(found)+ // documents deleted or moved out of a granted folder leave the index+ self.apply([.delete(gone), .add(found.map(self.entry))])self.indexing = falseself.onChange?()}}}+ private func matching(_ url: URL) -> (IndexedDocument) -> Bool {+ let want = url.standardizedFileURL.resolvingSymlinksInPath().path+ return { URL(fileURLWithPath: $0.path).standardizedFileURL.resolvingSymlinksInPath().path == want }+ }++ /// Run before the app writes a document in place. When the file about to be+ /// written is sealed, its entry leaves the index FIRST, and `write` runs once+ /// that delete has finished; otherwise `write` runs straight away. A failed+ /// delete still writes: the person's save matters more, and refresh()+ /// deletes again once the write lands.+ ///+ /// Writes stay in the order they were asked for: one that arrives while a+ /// sealed write is still waiting on its delete queues behind it, so an older+ /// write can never land over a newer one.+ func beforeSave(_ url: URL, sealed: Bool, write: @escaping () -> Void) {+ if waitingWrite { queuedWrites.append { [weak self] in self?.beforeSave(url, sealed: sealed, write: write) }; return }+ guard let doc = documents.first(where: matching(url)) else { return write() }+ let ops = SpotlightPlan.beforeSave(id: doc.path, sealed: sealed)+ guard !ops.isEmpty else { return write() }+ waitingWrite = true+ apply(ops) { [weak self] in+ write()+ guard let self else { return }+ self.waitingWrite = false+ while !self.waitingWrite, !self.queuedWrites.isEmpty { self.queuedWrites.removeFirst()() }+ }+ }+ private var waitingWrite = false+ private var queuedWrites: [() -> Void] = []++ /// Bring ONE document's entry up to date right after the app has written it+ /// in place, so the library and Spotlight match the saved file without+ /// waiting for the next full walk. Only a document the index already holds+ /// is touched; anything outside the granted folders was never indexed.+ func refresh(_ url: URL) {+ let same = matching(url)+ guard let old = documents.first(where: same) else { return }+ queue.async { [weak self] in+ guard let self else { return }+ let doc: IndexedDocument? = FolderGrants.shared.withAccess { _ in+ let v = try? URL(fileURLWithPath: old.path).resourceValues(forKeys: [.fileSizeKey, .contentModificationDateKey])+ return self.describe(URL(fileURLWithPath: old.path), name: old.name, folder: old.folder,+ size: v?.fileSize ?? 0, modified: v?.contentModificationDate ?? Date())+ }+ DispatchQueue.main.async {+ guard let doc, let i = self.documents.firstIndex(where: same) else { return }+ self.documents[i] = doc+ self.cache["\(doc.path):\(doc.size):\(doc.modified.timeIntervalSince1970)"] = doc+ self.save()+ self.apply(SpotlightPlan.refresh(self.entry(doc), sealed: doc.encrypted))+ self.onChange?()+ }+ }+ }+private func walk(_ folders: [URL], cache: [String: IndexedDocument]) -> ([IndexedDocument], [String: IndexedDocument]) {var cache = cachevar out: [IndexedDocument] = []@@ -225,27 +282,39 @@ final class LibraryIndex {// MARK: - CoreSpotlight- private func donate(_ docs: [IndexedDocument]) {- let items: [CSSearchableItem] = docs.map { doc in- let attrs = CSSearchableItemAttributeSet(contentType: UTType.html)- attrs.title = doc.title- attrs.displayName = doc.title- attrs.contentURL = URL(fileURLWithPath: doc.path)- // The whole point: the document's own prose, searchable from the- // system. Encrypted documents carry nil here because nothing was- // ever extracted from them.- attrs.textContent = doc.text- attrs.contentDescription = doc.encrypted- ? "Encrypted Bento document"- : (doc.text.map { String($0.prefix(200)) } ?? doc.folder)- attrs.keywords = [doc.app, "bento"].compactMap { $0 }- return CSSearchableItem(uniqueIdentifier: doc.path,- domainIdentifier: domain,- attributeSet: attrs)- }- guard !items.isEmpty else { return }- CSSearchableIndex.default().indexSearchableItems(items) { error in- if let error { NSLog("spotlight index failed: %@", String(describing: error)) }+ private func entry(_ doc: IndexedDocument) -> SpotlightPlan.Entry {+ SpotlightPlan.entry(path: doc.path, title: doc.title, base: doc.base, app: doc.app,+ sealed: doc.encrypted, text: doc.text, folder: doc.folder)+ }++ /// Carry out a plan against the system index, in order: each step starts+ /// only once the one before it has finished.+ private func apply(_ ops: [SpotlightPlan.Op], then done: (() -> Void)? = nil) {+ guard let op = ops.first else { done?(); return }+ let rest = Array(ops.dropFirst())+ switch op {+ case .delete(let ids):+ guard !ids.isEmpty else { return apply(rest, then: done) }+ CSSearchableIndex.default().deleteSearchableItems(withIdentifiers: ids) { [weak self] error in+ if let error { NSLog("spotlight delete failed: %@", String(describing: error)) }+ DispatchQueue.main.async { if let self { self.apply(rest, then: done) } else { done?() } }+ }+ case .add(let entries):+ guard !entries.isEmpty else { return apply(rest, then: done) }+ let items: [CSSearchableItem] = entries.map { e in+ let attrs = CSSearchableItemAttributeSet(contentType: UTType.html)+ attrs.title = e.title+ attrs.displayName = e.title+ attrs.contentURL = URL(fileURLWithPath: e.id)+ attrs.textContent = e.text+ attrs.contentDescription = e.description+ attrs.keywords = e.keywords+ return CSSearchableItem(uniqueIdentifier: e.id, domainIdentifier: domain, attributeSet: attrs)+ }+ CSSearchableIndex.default().indexSearchableItems(items) { [weak self] error in+ if let error { NSLog("spotlight index failed: %@", String(describing: error)) }+ DispatchQueue.main.async { if let self { self.apply(rest, then: done) } else { done?() } }+ }}}diff --git a/home/ios/SpotlightPlan.swift b/home/ios/SpotlightPlan.swiftnew file mode 100644index 00000000..f9eb9a57--- /dev/null+++ b/home/ios/SpotlightPlan.swift@@ -0,0 +1,64 @@+// SPDX-License-Identifier: MIT+// Copyright (c) 2026 The Bento authors++import Foundation++/// What the system search index (CoreSpotlight) may hold for a document, and+/// what to do to it when the document changes. LibraryIndex carries these plans+/// out; this file only decides, so scripts/test-home-spotlight.mjs can check every+/// rule without the real index.+///+/// - A SEALED document (its saved body is a bento/enc envelope) contributes+/// nothing from its content: its entry carries the saved file's own title+/// (the neutral one it writes while sealed) or the file name, no text, a fixed+/// description, and no keywords drawn from what it contains.+/// - When a save is sealed, the document's entry is DELETED before the file is+/// written, and replaced once it has been, so nothing from before survives+/// even an interrupted save.+/// - A full walk removes the entry of every document it no longer finds+/// (deleted, or moved out of a granted folder).+enum SpotlightPlan {+ struct Entry: Equatable {+ let id: String+ let title: String+ let text: String?+ let description: String?+ let keywords: [String]+ }++ enum Op: Equatable { case delete([String]), add([Entry]) }++ static let sealedDescription = "Encrypted Bento document"++ /// The entry for one document. `title` is the saved file's own <title> (nil+ /// when it has none); `base` is its file name without the extension.+ static func entry(path: String, title: String?, base: String, app: String?, sealed: Bool,+ text: String?, folder: String) -> Entry {+ let shown = (title?.isEmpty == false ? title : nil) ?? base+ if sealed {+ return Entry(id: path, title: shown, text: nil, description: sealedDescription, keywords: ["bento"])+ }+ return Entry(id: path, title: shown, text: text,+ description: text.map { String($0.prefix(200)) } ?? folder,+ keywords: [app, "bento"].compactMap { $0 })+ }++ /// What happens BEFORE a save is written: when the outgoing file is sealed,+ /// its entry is deleted first, so if the write lands and the app dies before+ /// the re-index, the index holds nothing from before.+ static func beforeSave(id: String, sealed: Bool) -> [Op] {+ sealed ? [.delete([id])] : []+ }++ /// What one save does to the index once written: replace the entry, deleting+ /// the old one first when the document is sealed.+ static func refresh(_ new: Entry, sealed: Bool) -> [Op] {+ sealed ? [.delete([new.id]), .add([new])] : [.add([new])]+ }++ /// The entries a full walk must remove: indexed before, not found now.+ static func removals(before: [String], after: [String]) -> [String] {+ let found = Set(after)+ return before.filter { !found.contains($0) }+ }+}diff --git a/scripts/test-home-spotlight.mjs b/scripts/test-home-spotlight.mjsnew file mode 100644index 00000000..207e2be0--- /dev/null+++ b/scripts/test-home-spotlight.mjs@@ -0,0 +1,129 @@+#!/usr/bin/env node+// SPDX-License-Identifier: MIT+// Copyright (c) 2026 The Bento authors+/**+ * bento/home on iOS and the Mac: what the system search index holds for a+ * document, and keeping it current after a save (home/ios/SpotlightPlan.swift,+ * carried out by LibraryIndex).+ *+ * node scripts/test-home-spotlight.mjs+ *+ * The plan runs against a simulated index that MERGES an add into an existing+ * entry (fields left unset keep their old values). That is the cautious reading+ * of CoreSpotlight, and it is why a sealed document's entry is deleted before it+ * is re-added: with the delete skipped, the old text survives and this fails.+ *+ * Apple platforms only; skips elsewhere.+ */+import { execFileSync } from 'node:child_process'+import { mkdtempSync, writeFileSync, rmSync, readFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join, resolve, dirname } from 'node:path'+import { fileURLToPath } from 'node:url'++const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..')+const SRC = [process.env.SPOTLIGHT_SRC || join(ROOT, 'home/ios/SpotlightPlan.swift')]+try { execFileSync('xcrun', ['--find', 'swiftc'], { stdio: 'pipe' }) } catch { console.log('home spotlight: SKIPPED — needs an Apple toolchain.'); process.exit(0) }++const main = String.raw`+import Foundation+typealias P = SpotlightPlan+struct Item { var title: String?; var text: String?; var description: String?; var keywords: [String]? }+var index: [String: Item] = [:]+func run(_ ops: [P.Op]) {+ for op in ops {+ switch op {+ case .delete(let ids): for id in ids { index[id] = nil }+ case .add(let es):+ for e in es {+ var it = index[e.id] ?? Item()+ it.title = e.title+ if let t = e.text { it.text = t }+ if let d = e.description { it.description = d }+ if !e.keywords.isEmpty { it.keywords = e.keywords }+ index[e.id] = it+ }+ }+ }+}+func emit(_ k: String, _ v: Any) { print("\(k)=\(v)") }+func all(_ id: String) -> String { let i = index[id]; return [i?.title, i?.text, i?.description, i?.keywords?.joined(separator: " ")].compactMap { $0 }.joined(separator: " | ") }+let path = "/docs/Q3 plan.bento.html"+let secret = "Acquisition of Contoso"+let body = "Board only. Offer price 41 per share."++// indexed while open+run([.add([P.entry(path: path, title: secret + " — Bento Slides", base: "Q3 plan", app: "slides", sealed: false, text: body, folder: "docs")])])+emit("open.hasText", all(path).contains(body))+// an unsealed save updates the text+let body2 = "Board only. Offer price 44 per share."+let e2 = P.entry(path: path, title: secret + " — Bento Slides", base: "Q3 plan", app: "slides", sealed: false, text: body2, folder: "docs")+run(P.refresh(e2, sealed: false))+emit("unsealed.updated", all(path).contains(body2) && !all(path).contains("41 per share"))+// sealed in one save: the saved <title> is now the app name; nothing extracted+let sealed = P.entry(path: path, title: "Bento Slides", base: "Q3 plan", app: "slides", sealed: true, text: body2, folder: "docs")+run(P.refresh(sealed, sealed: true))+let after = all(path)+emit("sealed.noText", !after.contains("Board only") && !after.contains("per share"))+emit("sealed.noTitle", !after.contains(secret))+emit("sealed.title", index[path]?.title ?? "nil")+emit("sealed.textNil", index[path]?.text == nil)+emit("sealed.desc", index[path]?.description ?? "nil")+emit("sealed.keywords", (index[path]?.keywords ?? []).joined(separator: ","))+// INTERRUPTED: open with text again, then a sealed save whose app dies after+// the write and before the re-index: only beforeSave ran+run([.add([P.entry(path: path, title: secret + " — Bento Slides", base: "Q3 plan", app: "slides", sealed: false, text: body, folder: "docs")])])+run(P.beforeSave(id: path, sealed: true))+emit("interrupted.clean", !all(path).contains(body) && !all(path).contains(secret))+emit("unsealed.beforeNoop", P.beforeSave(id: path, sealed: false).isEmpty)+// a sealed file with no <title>: the file name+emit("sealed.noTitleTag", P.entry(path: path, title: nil, base: "Q3 plan", app: "slides", sealed: true, text: nil, folder: "docs").title)+// a sealed document's entry never carries text, whatever is handed in+emit("sealed.dropsText", P.entry(path: path, title: "x", base: "b", app: nil, sealed: true, text: "leak", folder: "f").text == nil)+// a walk removes what it no longer finds+let gone = P.removals(before: [path, "/docs/b.bento.html", "/docs/c.bento.html"], after: ["/docs/b.bento.html"])+emit("walk.removals", gone.sorted().joined(separator: ","))+run([.delete(gone)])+emit("walk.gone", index[path] == nil)+`+const dir = mkdtempSync(join(tmpdir(), 'bento-spotlight-'))+let out+try {+ writeFileSync(join(dir, 'main.swift'), main)+ execFileSync('swiftc', ['-O', '-o', join(dir, 'a'), ...SRC, join(dir, 'main.swift')], { stdio: 'pipe' })+ out = Object.fromEntries(execFileSync(join(dir, 'a')).toString().trim().split('\n').map((l) => l.split(/=(.*)/s).slice(0, 2)))+} catch (err) { console.error('swiftc or run failed:\n' + (err.stderr?.toString() ?? err.message)); process.exit(2) }+finally { try { rmSync(dir, { recursive: true, force: true }) } catch {} }++let checks = 0, failures = 0+const ok = (c, m) => { checks++; if (!c) failures++; console.log(` ${c ? 'ok ' : 'FAIL'} ${m}`) }+console.log('after a save:')+ok(out['open.hasText'] === 'true', 'an open document is indexed with its text')+ok(out['unsealed.updated'] === 'true', 'an ordinary save replaces the text, and the old text is gone')+ok(out['sealed.noText'] === 'true' && out['sealed.textNil'] === 'true', 'a document sealed in one save: none of its text is left in the index')+ok(out['sealed.noTitle'] === 'true' && out['sealed.title'] === 'Bento Slides', "and none of its title: the entry carries the saved file's own neutral title")+ok(out['sealed.desc'] === 'Encrypted Bento document' && out['sealed.keywords'] === 'bento', 'with a fixed description and no keywords from the document')+ok(out['sealed.noTitleTag'] === 'Q3 plan', 'a sealed file with no title shows its file name')+ok(out['sealed.dropsText'] === 'true', 'a sealed entry never carries text, whatever it is handed')+ok(out['interrupted.clean'] === 'true', 'a sealed save interrupted between the write and the re-index: nothing from before is left')+ok(out['unsealed.beforeNoop'] === 'true', 'an ordinary save touches nothing before it is written')+console.log('a full walk:')+ok(out['walk.removals'] === '/docs/Q3 plan.bento.html,/docs/c.bento.html', 'documents no longer found (deleted, or moved out of a granted folder) are removed')+ok(out['walk.gone'] === 'true', 'and their entries leave the index')+console.log('LibraryIndex carries the plan out:')+const lib = readFileSync(join(ROOT, 'home/ios/LibraryIndex.swift'), 'utf8')+const ed = readFileSync(join(ROOT, 'home/ios/EditorViewController.swift'), 'utf8')+ok(/apply\(SpotlightPlan\.refresh\(/.test(lib), 'refresh() applies the save plan')+ok(/SpotlightPlan\.removals\(/.test(lib) && /apply\(\[\.delete\(gone\)/.test(lib), 'reindex() deletes what the walk no longer finds, before adding')+ok(!/attrs\.textContent = doc\./.test(lib), 'every entry is built by SpotlightPlan, never straight from the document')+ok(/if ok, let url = self\?\.document\.fileURL \{ LibraryIndex\.shared\.refresh\(url\) \}/.test(ed), 'the editor refreshes the entry after each successful save')+{+ const pre = ed.indexOf('LibraryIndex.shared.beforeSave(document.fileURL, sealed: sealed)')+ const dirty = ed.indexOf('self.document.updateChangeCount(.done)')+ const save = ed.indexOf('self.document.save(to: self.document.fileURL')+ ok(pre > 0 && dirty > pre && save > dirty && /let sealed = BentoIndex\.isEncrypted\(/.test(ed),+ 'the editor reads whether the outgoing file is sealed, and marks it changed and writes it only inside beforeSave')+}+ok(/if waitingWrite \{ queuedWrites\.append/.test(lib), 'a write arriving while a sealed one waits on its delete queues behind it')+console.log(`\n${checks - failures}/${checks} checks passed`)+process.exit(failures ? 1 : 0)