import Foundation /// "Let agents make changes" and the log of every change an agent made. The app and the MCP process /// (the same executable run with --mcp) share the standard defaults and this log file. public enum AgentSettings { public static let allowWritesKey = "jobley.agents.allowWrites" public static func allowsWrites(_ defaults: UserDefaults = .standard) -> Bool { defaults.bool(forKey: allowWritesKey) } public static func setAllowsWrites(_ value: Bool, _ defaults: UserDefaults = .standard) { defaults.set(value, forKey: allowWritesKey) } } public struct AgentLogEntry: Codable, Hashable, Identifiable, Sendable { public var id = UUID() public var at: Date public var agent: String public var tool: String public var jobID: String public var summary: String } public enum AgentLog { public static var fileURL: URL { if let path = ProcessInfo.processInfo.environment["JOBLEY_AGENT_LOG"] { return URL(fileURLWithPath: path) } let folder = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0].appending(path: "Jobley") return folder.appending(path: "agent-log.json") } public static func entries(at url: URL = fileURL) -> [AgentLogEntry] { guard let data = try? Data(contentsOf: url) else { return [] } let decoder = JSONDecoder(); decoder.dateDecodingStrategy = .iso8601 return (try? decoder.decode([AgentLogEntry].self, from: data)) ?? [] } /// Newest first, at most 500 entries. public static func append(_ entry: AgentLogEntry, at url: URL = fileURL) { var all = entries(at: url) all.insert(entry, at: 0) if all.count > 500 { all.removeLast(all.count - 500) } let encoder = JSONEncoder(); encoder.dateEncodingStrategy = .iso8601 try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true) if let data = try? encoder.encode(all) { try? data.write(to: url, options: .atomic) } } }