Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Docs9// File : Packages/ORIXDocsAI/Sources/DocAgent/Autonomy.swift10// Description : Autonomy modes of the document agent, decided in code. A11// mode maps each tool call to allow, ask or deny from the12// tool's declared effect and scopes; nothing a model writes,13// and nothing found in a document, can change the mapping.14// Module : DocAgent15// Repository : https://git.spboucher.ai/orix-docs16// Created : 2026-11-2217// Last Modified : 2026-11-2218//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author : Simon-Pierre Boucher24// Position : Professor of Finance25// Institution : Université du Québec en Outaouais (UQO)26// Department : Département des sciences administratives27// Organization : SP Boucher28//29// Website : https://spboucher.ai30// Git : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General : info@spboucher.ai37// Professional : contact@spboucher.ai38// Direct : simon@spboucher.ai39// Research : research@spboucher.ai40// Development : dev@spboucher.ai41// Support : support@spboucher.ai42// Security/Admin : admin@spboucher.ai43// Notifications : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification : Public70// Distribution : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License : None specified (no license file in this repository)84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import Foundation92import DocCommands9394public enum PermissionRule: String, Sendable, Codable, Hashable, CaseIterable {95 case allow, ask, deny96 /// The stricter of two rules.97 func stricter(_ other: PermissionRule) -> PermissionRule {98 let order: [PermissionRule] = [.allow, .ask, .deny]99 return order[max(order.firstIndex(of: self)!, order.firstIndex(of: other)!)]100 }101}102103public enum AutonomyMode: Sendable, Codable, Hashable {104 /// Reads are automatic. Every mutating command is submitted for approval on its own (commands linked by `{{N.path}}` stay together).105 case guided106 /// Reads and analysis are automatic. Each batch of mutations (the mutations of one model turn) is submitted for approval as a whole.107 case balanced108 /// Mutations whose scopes are all in `preauthorized` are applied without asking, provided every block they touch lies in a whitelisted109 /// section when `sections` (heading identifiers) is set; a batch that reaches outside the whitelist is refused, not asked.110 /// Mutations that need another granted scope are submitted for approval.111 case advanced(preauthorized: Set<DocScope>, sections: Set<String>?)112 /// Per-scope and per-command rules. A command rule overrides the scope rules; among scope rules the strictest wins. Unlisted: reads are113 /// allowed, everything else asks.114 case custom(scopes: [DocScope: PermissionRule], commands: [String: PermissionRule])115116 public var title: String {117 switch self {118 case .guided: "Guided"119 case .balanced: "Balanced"120 case .advanced: "Advanced"121 case .custom: "Custom"122 }123 }124}125126public struct AutonomyPolicy: Sendable, Codable, Hashable {127 public var mode: AutonomyMode128 /// Once content that looks like injected instructions has been shown to the model, changes that would have been applied without asking129 /// are submitted for approval instead. Content can only ever narrow what an agent does unattended.130 public var askAfterFlaggedContent: Bool131132 public init(_ mode: AutonomyMode = .balanced, askAfterFlaggedContent: Bool = true) { self.mode = mode; self.askAfterFlaggedContent = askAfterFlaggedContent }133134 public enum Verdict: Sendable, Hashable {135 case allow136 case ask(reason: String)137 case deny(reason: String)138 }139140 /// The decision for one tool call. `flagged` is true when suspicious content has been seen during the run.141 public func verdict(for tool: DocAgentTool, flagged: Bool = false) -> Verdict {142 let base = rule(for: tool)143 switch base {144 case .deny: return .deny(reason: "The \(mode.title) autonomy settings do not allow \(tool.id).")145 case .ask: return .ask(reason: reason(for: tool))146 case .allow:147 if flagged, askAfterFlaggedContent, tool.effect != .read, tool.effect != .control, tool.effect != .question {148 return .ask(reason: "Content that looks like injected instructions was read during this run, so this action needs your approval.")149 }150 return .allow151 }152 }153154 private func reason(for tool: DocAgentTool) -> String {155 switch tool.effect {156 case .mutate: "\(mode.title) mode asks before changing the document."157 case .artifact: "\(mode.title) mode asks before producing a file."158 case .workflow: "\(mode.title) mode asks before running a workflow."159 default: "\(mode.title) mode asks before \(tool.id)."160 }161 }162163 func rule(for tool: DocAgentTool) -> PermissionRule {164 let passive = tool.effect == .read || tool.effect == .control || tool.effect == .question165 switch mode {166 case .guided, .balanced:167 return passive ? .allow : .ask168 case .advanced(let preauthorized, _):169 if passive { return .allow }170 return Set(tool.scopes).isSubset(of: preauthorized) && !tool.scopes.isEmpty ? .allow : .ask171 case .custom(let scopes, let commands):172 if let rule = commands[tool.id] { return rule }173 let fallback: PermissionRule = passive ? .allow : .ask174 guard !tool.scopes.isEmpty else { return fallback }175 return tool.scopes.map { scopes[$0] ?? fallback }.reduce(.allow) { $0.stricter($1) }176 }177 }178179 /// Guided mode asks for each command separately.180 public var asksPerCommand: Bool { if case .guided = mode { true } else { false } }181182 /// Heading identifiers outside of which mutations are refused; nil when there is no restriction.183 public var sectionWhitelist: Set<String>? { if case .advanced(_, let sections) = mode { sections } else { nil } }184185 /// Commands a Custom mode denies outright: they are not even offered to the model.186 public func deniedCommands(in registry: DocCommandRegistry) -> Set<String> {187 guard case .custom(let scopes, let commands) = mode else { return [] }188 var out = Set<String>()189 for spec in registry.specs {190 if let rule = commands[spec.id] { if rule == .deny { out.insert(spec.id) }; continue }191 if spec.permissions.contains(where: { scopes[$0] == .deny }) { out.insert(spec.id) }192 }193 return out194 }195}196