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 {
182final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate { 182final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate {
183 var session: DocumentSession? 183 var session: DocumentSession?
184 var commands: AppCommands? 184 var commands: AppCommands?
185 #if !APPSTORE 185 #if APPSTORE
186 var workspace: WorkspaceModel?
187 #else
186 let updates = Updates() 188 let updates = Updates()
187 #endif 189 #endif
188 190
@@ -242,7 +244,11 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent
242 244
243 func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply { 245 func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
244 guard let session else { return .terminateNow } 246 guard let session else { return .terminateNow }
247 #if APPSTORE
248 BufferRestore.save(session, bookmarking: { [workspace] in workspace?.contains(URL(fileURLWithPath: $0)) != true })
249 #else
245 BufferRestore.save(session) 250 BufferRestore.save(session)
251 #endif
246 return UnsavedChanges.resolve(session, paths: session.openPaths) ? .terminateNow : .terminateCancel 252 return UnsavedChanges.resolve(session, paths: session.openPaths) ? .terminateNow : .terminateCancel
247 } 253 }
248} 254}
@@ -298,18 +304,33 @@ enum BufferRestore {
298 var paths: [String] 304 var paths: [String]
299 var current: String? 305 var current: String?
300 var carets: [String: Int] 306 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]?
301 } 310 }
302 311
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) {
304 var carets: [String: Int] = [:] 314 var carets: [String: Int] = [:]
305 for path in session.openPaths { carets[path] = session.caret(of: path) } 315 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)
307 if let data = try? JSONEncoder().encode(state) { defaults.set(data, forKey: key) } 321 if let data = try? JSONEncoder().encode(state) { defaults.set(data, forKey: key) }
308 } 322 }
309 323
310 /// Opens the buffers that were open, in order, and returns the carets to show them at. 324 /// Opens the buffers that were open, in order, and returns the carets to show them at.
311 static func restore(_ session: DocumentSession, defaults: UserDefaults = .standard) -> [String: Int] { 325 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 }
313 let existing = state.paths.filter { FileManager.default.fileExists(atPath: $0) } 334 let existing = state.paths.filter { FileManager.default.fileExists(atPath: $0) }
314 for path in existing { session.open(path) } 335 for path in existing { session.open(path) }
315 if let current = state.current, existing.contains(current) { session.open(current) } 336 if let current = state.current, existing.contains(current) { session.open(current) }
@@ -360,6 +381,9 @@ struct OrgstarApp: App {
360 .onAppear { 381 .onAppear {
361 delegate.session = session 382 delegate.session = session
362 delegate.commands = commands 383 delegate.commands = commands
384 #if APPSTORE
385 delegate.workspace = model
386 #endif
363 CaptureIntent.environment = (model, session, clock, commands) 387 CaptureIntent.environment = (model, session, clock, commands)
364 } 388 }
365 case .failure(let error): 389 case .failure(let error):