Skip to content

feat(swift): add SummarizingChatStorage for automatic history compression - #587

Merged
cornelcroi merged 4 commits into
mainfrom
feat/summarizing-chat-storage-swift
Jul 14, 2026
Merged

feat(swift): add SummarizingChatStorage for automatic history compression#587
cornelcroi merged 4 commits into
mainfrom
feat/summarizing-chat-storage-swift

Conversation

@cornelcroi

Copy link
Copy Markdown
Collaborator

Issue Link

Closes #584

Summary

  • Adds SummarizingChatStorage, a ChatStorage wrapper that automatically compresses conversation history when it exceeds a configurable threshold
  • Compression is triggered when message count exceeds triggerAt * 2; user supplies a ChatSummarizer closure (@Sendable, async throws) that receives the full history and keepLast
  • Compressed result is written back to the base store via saveMessages (replace semantics) and cached in a private SummarizationCache actor
  • fetchAllChats is never intercepted — classifier sees raw history
  • Cache is invalidated on any save or saveMessages call
  • 13 tests added, all actor-safe (Swift 6 strict concurrency)

Changes

  • swift/Sources/AgentSquad/Storage/SummarizingChatStorage.swift — new file
  • swift/Tests/AgentSquadTests/SummarizingChatStorageTests.swift — 13 tests

User experience

let storage = SummarizingChatStorage(
    wrapping: InMemoryChatStorage(),
    summarizer: { history, keepLast in
        // call an LLM, return compressed [ConversationMessage]
    },
    triggerAt: 20,
    keepLast: 3
)
let squad = AgentSquad(storage: storage)

Checklist

  • Tests added/updated
  • Backward compatible (new struct, no existing API changed)
  • Swift 6 strict concurrency compliant
  • No new dependencies

🤖 Generated with Claude Code

…sion — Closes #584

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b6ab5685bc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +114 to +118
try await base.saveMessages(
compressed,
userId: userId, sessionId: sessionId, agentId: agentId,
maxMessages: nil
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Replace history instead of appending summaries

When this wrapper is used with any bundled store (FileChatStorage, InMemoryChatStorage, or DeviceChatStorage), saveMessages is append-only rather than replacement. After the cache is invalidated or the app restarts, fetch reads the original long history plus the summary/recent messages, so the history is not actually compressed and can grow with duplicated content. This needs a true replace/delete path or a storage API that supports replacement.

Useful? React with 👍 / 👎.

Comment on lines +96 to +97
if let cached = await cache.get(key: key) {
return cached

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Trim cached fetches to the requested maxMessages

Once a compressed result is cached, later fetch calls return it before applying the caller's maxMessages. If one caller caches a larger compressed history and another asks for a tighter window, this violates the ChatStorage.fetch contract that all bundled stores honor by trimming to the requested budget. Return a trimmed view of the cached messages or otherwise account for maxMessages in the cache path.

Useful? React with 👍 / 👎.

// MARK: - Private

private func cacheKey(userId: String, sessionId: String, agentId: String) -> String {
"\(userId)#\(sessionId)#\(agentId)"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use a collision-free cache key

Because the cache key concatenates raw IDs with #, distinct scopes collide whenever any ID contains that separator, e.g. (userId: "a#b", sessionId: "c", agentId: "d") and (userId: "a", sessionId: "b#c", agentId: "d"). After one scope is summarized, the other can receive its cached messages without reading the base store, leaking or mixing chat history across users/sessions/agents. Use a structured key type or escaping rather than raw concatenation.

Useful? React with 👍 / 👎.

cornelcroi and others added 3 commits July 14, 2026 11:27
All bundled stores (InMemoryChatStorage, FileChatStorage, DeviceChatStorage)
append in saveMessages rather than replace. Writing the compressed result back
would corrupt history by appending the summary on top of the original messages.
Use only the in-memory SummarizationCache actor, consistent with Python and
TypeScript implementations.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…lisions

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…mpression, full docs

Rewrites SummarizingChatStorage with a LangChain-inspired design: raw messages always
written to the inner store; an in-memory buffer activates lazily on the first qualifying
fetch (history > triggerAt * 2). Once active, each save appends to the buffer and
compresses immediately if needed — fetch is always fast. fetchAllChats is never
intercepted so raw history stays available for analytics and cross-agent routing.

Rewrites the test suite (14 tests) to cover the new lifecycle: below/at/above trigger,
lazy activation, eager save-time compression, no write-back to base store, fetchAllChats
bypass, base storage integrity, and error propagation.

Adds documentation:
- docs/src/content/docs/storage/summarizing.mdx (cross-language)
- docs/src/content/docs/swift/storage/built-in/summarizing.md (Swift deep-dive)
- Updates storage overview pages and swift/SKILL.md + swift/README.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@cornelcroi
cornelcroi merged commit 5c32562 into main Jul 14, 2026
5 checks passed
@cornelcroi
cornelcroi deleted the feat/summarizing-chat-storage-swift branch July 14, 2026 10:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: add SummarizingChatStorage for Swift

1 participant