Skip to main content

mainspring-session-postgres

Postgres 会话后端 + RunCheckpoint 实现(T4-3)。实现 core 的两个 Protocol,与 SQLite 外挂共用 同一套接口契约测试(tests/test_session_backends.py),换后端只改 yaml。

用法

from mainspring_session_postgres import open_postgres, backend_factory

store, checkpoint = open_postgres(
    "postgresql://mainspring:mainspring@127.0.0.1:5432/mainspring"
)
await store.append("s1", [Message(id="m1", role="user", content="你好")])

yaml 装配(core 不反向依赖外挂:工厂由调用方注入):

session:
  backend: postgres
  url: postgresql://mainspring:mainspring@127.0.0.1:5432/mainspring
runner = build_runner(config, backends={"postgres": backend_factory}, checkpoint=checkpoint)

url 缺省取 MAINSPRING_SESSION_POSTGRES_DSN,再缺省用 compose 默认 DSN。

设计要点

  1. 幂等 append 靠唯一约束 (session_id, message_id) + ON CONFLICT DO NOTHING; Postgres 唯一索引对 NULL 不去重,与 MemoryStore / SQLite「id=NULL 照写」语义一致。
  2. 快照整包落表:INSERT ... ON CONFLICT (run_id) DO UPDATE,并发写同一 run 由唯一约束 串行化,不会产生半截快照。
  3. 条件写 save_step_if:UPDATE ... WHERE status = 期望值 的 CAS,approve / deny 跨进程 竞态下只有一方成功(RunCheckpoint 契约之外的扩展能力,Redis / SQLite 同样提供)。
  4. 表名带前缀 ms_messages / ms_runs,与同库其他业务表隔离。
  5. 连接池懒初始化:yaml 装配阶段不依赖 Postgres 在线;aclose() 关闭共享连接池。

验收

  • docker compose up -d postgres 后跑 pytest tests/test_session_backends.py -k postgres;
  • 覆盖:append 幂等 / NULL 不去重 / 会话隔离与删除 / 快照八要素 roundtrip / 覆盖写 / 并发双写 / 条件写单赢家;
  • core 零反向依赖由 import-linter 守护。

Metadata

Release files for mainspring-session-postgres 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mainspring-session-postgres 0.1.2
File Size Uploaded Provenance Actions
mainspring_session_postgres-0.1.2.tar.gz 5.6 kB With provenance Details for mainspring_session_postgres-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mainspring-session-postgres 0.1.2
File Interpreter ABI Platform Actions
mainspring_session_postgres-0.1.2-py3-none-any.whl Python 3 none any Details for mainspring_session_postgres-0.1.2-py3-none-any.whl

Total release size: 12.8 kB

Release files / mainspring_session_postgres-0.1.2.tar.gz

Download URL mainspring_session_postgres-0.1.2.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c31e52706ae5ad2076d3f542033b4949d3f5236f954eef2a050d79df9cf1bd13
BLAKE2b-256 checksum
How to use checksums
a4cbc608a89b2bda425bb07895f3e7c6402cd5f8af6f4e71fd1d848a5726783d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Release files / mainspring_session_postgres-0.1.2-py3-none-any.whl

Download URL mainspring_session_postgres-0.1.2-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
984d626cebb24d744eb11e036459566b33659c650d132decc13235798cfd1369
BLAKE2b-256 checksum
How to use checksums
3686e934d64dfa8080fd43d21caf22bb1e607cfbd46af9b0c7da38f6be598f02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Security for mainspring-session-postgres 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mainspring_session_postgres-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page