DeviceChatStorage
DeviceChatStorage is the bundled on-device store for iOS 17+ and macOS 14+. It backs history with a SwiftData SQLite store under Library/Caches/AgentSquad — never backed up, reclaimable by the OS under disk pressure. Use FileChatStorage on iOS 16.
@available(iOS 17, macOS 14, *)public actor DeviceChatStorage: ChatStorage, ModelActor { public init(userId: String, baseURL: URL? = nil, inMemory: Bool = false) throws}| Parameter | Default | Notes |
|---|---|---|
userId | — | Required. Bound at init. The per-call userId on fetch / save / saveMessages / fetchAllChats is asserted to match. |
baseURL | Library/Caches/AgentSquad | Override the SQLite store directory. |
inMemory | false | true uses an ephemeral SwiftData store — for tests. |
Production:
let store = try DeviceChatStorage(userId: currentUser.id)let orchestrator = Orchestrator(agents: [agent], store: store)Tests — ephemeral, no disk I/O:
let store = try DeviceChatStorage(userId: "test-user", inMemory: true)Clearing history on logout
Section titled “Clearing history on logout”public func clear() throwsclear() deletes all rows for the bound userId. Call it on logout or account switch so the next user cannot read the previous session’s history.
try await store.clear()Behavior
Section titled “Behavior”- Bound
userId. The store is created for one user. Passing a differentuserIdat call time triggers anassertfailure in debug builds and silently uses the bound id in release. Create a new instance when the user changes. - Self-healing. A partial WAL/SHM purge (common when
Library/Cachesis evicted mid-write) would makeModelContainerthrow forever.DeviceChatStoragedetects this and deletes the corrupt store once, re-creating it from scratch. History is lost in that scenario, but the app recovers automatically. - Row-level writes. Unlike
FileChatStorage, each save inserts individual rows. This scales better with large message budgets. [agentId]attribution.fetchAllChatsprefixes assistant messages with[agentId], giving the Classifier correct routing attribution in multi-agent sessions.
See also: Storage overview · InMemoryChatStorage · FileChatStorage · Custom store