Commit fb70060abf

fb70060abf1a5635529d59ee446e88d6fb0a4095

parent: 6e84c5315a

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-20 06:19 UTC

Restore buffers outside the roots in the Mac App Store build

- BufferRestore keeps a security-scoped bookmark for each open file outside every folder and grant, and resolves it before reopening, following a moved file
- The app delegate holds the workspace for the check

Layout: unified · split

Sources/Orgstar/OrgstarApp.swift +28 −4
@@ -182,7 +182,9 @@ struct PromptRequest: Identifiable {
182182final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate {
183183 var session: DocumentSession?
184184 var commands: AppCommands?
185 #if !APPSTORE
185 #if APPSTORE
186 var workspace: WorkspaceModel?
187 #else
186188 let updates = Updates()
187189 #endif
188190
@@ -242,7 +244,11 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
242244
243245 func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
244246 guard let session else { return .terminateNow }
247 #if APPSTORE
248 BufferRestore.save(session, bookmarking: { [workspace] in workspace?.contains(URL(fileURLWithPath: $0)) != true })
249 #else
245250 BufferRestore.save(session)
251 #endif
246252 return UnsavedChanges.resolve(session, paths: session.openPaths) ? .terminateNow : .terminateCancel
247253 }
248254}
@@ -298,18 +304,33 @@ enum BufferRestore {
298304 var paths: [String]
299305 var current: String?
300306 var carets: [String: Int]
307 /// In the Mac App Store build, bookmarks of the files outside every folder, which the
308 /// sandbox would otherwise keep orgstar out of after relaunch.
309 var bookmarks: [String: Data]?
301310 }
302311
303 static func save(_ session: DocumentSession, defaults: UserDefaults = .standard) {
312 /// `bookmarking` says which paths get a bookmark.
313 static func save(_ session: DocumentSession, bookmarking: (String) -> Bool = { _ in false }, defaults: UserDefaults = .standard) {
304314 var carets: [String: Int] = [:]
305315 for path in session.openPaths { carets[path] = session.caret(of: path) }
306 let state = State(paths: session.openPaths, current: session.path, carets: carets)
316 var bookmarks: [String: Data] = [:]
317 for path in session.openPaths where bookmarking(path) {
318 bookmarks[path] = SecurityBookmark.make(URL(fileURLWithPath: path))
319 }
320 let state = State(paths: session.openPaths, current: session.path, carets: carets, bookmarks: bookmarks.isEmpty ? nil : bookmarks)
307321 if let data = try? JSONEncoder().encode(state) { defaults.set(data, forKey: key) }
308322 }
309323
310324 /// Opens the buffers that were open, in order, and returns the carets to show them at.
311325 static func restore(_ session: DocumentSession, defaults: UserDefaults = .standard) -> [String: Int] {
312 guard let data = defaults.data(forKey: key), let state = try? JSONDecoder().decode(State.self, from: data) else { return [:] }
326 guard let data = defaults.data(forKey: key), var state = try? JSONDecoder().decode(State.self, from: data) else { return [:] }
327 // A bookmarked file comes back with access, at the path it has now.
328 for (path, bookmark) in state.bookmarks ?? [:] {
329 guard let moved = SecurityBookmark.resolve(bookmark)?.url.path, moved != path else { continue }
330 state.paths = state.paths.map { $0 == path ? moved : $0 }
331 if state.current == path { state.current = moved }
332 state.carets[moved] = state.carets.removeValue(forKey: path)
333 }
313334 let existing = state.paths.filter { FileManager.default.fileExists(atPath: $0) }
314335 for path in existing { session.open(path) }
315336 if let current = state.current, existing.contains(current) { session.open(current) }
@@ -360,6 +381,9 @@ struct OrgstarApp: App {
360381 .onAppear {
361382 delegate.session = session
362383 delegate.commands = commands
384 #if APPSTORE
385 delegate.workspace = model
386 #endif
363387 CaptureIntent.environment = (model, session, clock, commands)
364388 }
365389 case .failure(let error):