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.0

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.0
File Size Uploaded Provenance Actions
mainspring_session_postgres-0.1.0.tar.gz 5.6 kB With provenance Details for mainspring_session_postgres-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 12.8 kB

Release files / mainspring_session_postgres-0.1.0.tar.gz

Download URL mainspring_session_postgres-0.1.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0ee2d1f9b7080501193d5fb52561c91ce94d936d476505b4af40203969136246
BLAKE2b-256 checksum
How to use checksums
147ec0fd5cd6e48b337cee58c820193cdefe96013b51babe8814df93e213d2b3
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.0-py3-none-any.whl

Download URL mainspring_session_postgres-0.1.0-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ef87f15e32453a4328990832c7836d4096e69b9a989fdea12d3ac09dc2595198
BLAKE2b-256 checksum
How to use checksums
3ae954864266fb58d8973ca5f33a06adbea18c9b7007b2cc3105d1ced345354e
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.0

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.0.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