Skip to content

Persist entries immediately, structured-clone ModelMessage, and add migration for new message format#7

Merged
mikea merged 2 commits into
masterfrom
codex/refactor-persistence-and-rehydration
Mar 29, 2026
Merged

Persist entries immediately, structured-clone ModelMessage, and add migration for new message format#7
mikea merged 2 commits into
masterfrom
codex/refactor-persistence-and-rehydration

Conversation

@mikea

@mikea mikea commented Mar 29, 2026

Copy link
Copy Markdown
Owner

Motivation

  • Message persistence format was changed to store AI SDK ModelMessage JSON directly, which is incompatible with older payloads and requires a DB reset.
  • Session state must avoid retaining external object references from callers and AI SDK callbacks, so incoming messages need to be cloned and validated at the boundary.

Description

  • Add migration packages/core/migrations/0002_drop_incompatible_sessions.sql that deletes entries and sessions to reset persisted state for the new format.
  • Change Messages to structured-clone and validate incoming ModelMessage instances, expose pushAll returning cloned messages, and add slice, replace, and helper cloning logic in packages/core/src/messages.ts.
  • Update entry types in packages/core/src/db/entry-types.ts so MessageEntry.data is ModelMessage | { type: "system"; content: string } to support legacy system entries.
  • Convert legacy system message payloads during context reconstruction in packages/core/src/session/context.ts so they map to ModelMessage with role: "system".
  • Rework AgentSessionDO (packages/core/src/agent-session-do.ts) to remove deferred pendingEntries batching and instead: ensure the sessions row is committed lazily via #ensureSessionCommitted, persist each new entry immediately with appendEntry (renamed import persistEntry), update #leafId/#updatedAt on write, map in-memory cloned messages to persisted entry IDs, and persist compaction entries immediately.
  • Update specs in specs/core.md to document immediate persistence and the new message format behaviour.

Testing

  • Ran TypeScript build with npm run build (typecheck) and the command completed successfully.
  • Ran the automated unit test suite with npm test and all tests passed.

Codex Task

@mikea mikea merged commit 7049fb6 into master Mar 29, 2026
1 check passed
@mikea mikea deleted the codex/refactor-persistence-and-rehydration branch March 29, 2026 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant