beancount-guard
Beancount 账本的写入守卫与对账内核:校验后原子写入、别名/周期治理、对账闭环、只读 BQL/MCP。不绑定任何 UI——不包含界面、不启动服务、不依赖特定前端;任何前端(Fava、Obsidian 插件、脚本、Agent)都调用同一组 Python 函数,或通过同一份 JSON CLI 契约调用。
写入目标
- 交易:
trans/collect.bean - 对账:
trans/reconciliation.bean trans/main.bean只 include 上述两个文件,不写交易。- 本库负责整个账本写入;新账本从模板仓库开始。
与其他项目的关系
| 项目 | 根目录 | 职责 |
|---|---|---|
| 写入守卫(本仓库) | ~/ledger-domain/beancount-guard |
别名/周期治理、对账重建、草稿队列、校验后原子写入、查询投影 |
| 账本模板 | ~/ledger-domain/ledger-domain-assets |
只存 .bean 和账本资源,不放代码 |
| DSH 面板 | ~/ledger-domain/dsh-ledger |
可选前端之一,只做展示,通过 CLI 调本库 |
前端可替换:本库不认识 DSH,只提供 CLI JSON 信封与 MCP 工具集。
独立安装(不依赖任何前端)
发布在 PyPI,任何前端都可以直接使用:
pip install beancount-guard
python -m beancount_guard --root /path/to/ledger status # JSON CLI
python -m beancount_guard --root /path/to/ledger health # version + protocol
python -m beancount_guard.mcp_server # 只读 MCP
beancount_guard.PROTOCOL是 CLI/MCP 契约版本:只有破坏性变更才 +1,前端据此判断兼容性。health返回{"version": "<安装版本>", "protocol": 1, ...};前端用它和 PyPI 上的最新版本比较,决定是否提示升级。- 前端不得依赖私有实现:只用 CLI JSON 信封、MCP 工具集和这两个字段。
函数接口
from beancount_guard import create_beancount_guard
ledger = create_beancount_guard(root="/home/dhr2333/Documents/账本-mvp")
ledger.setup() # 是否已有 main.bean
ledger.init_ledger(title="我的账本", currency="CNY") # 生成新账本并 bean-check
ledger.resolve_alias("工行5244") # → {"account": "Assets:...", "matched": "alias"}
ledger.list_accounts()
ledger.get_due_reconciliations()
ledger.set_account_metadata(account="Assets:...", alias="工行5244", reconcile="monthly")
ledger.preview_reconciliation({"account": "工行5244", "asOf": "2026-11-30", "actual": "1000.00"})
ledger.commit_reconciliation({...preview fields...}) # create / same-asOf update
ledger.reconcile_history(account="工行5244")
ledger.open_account(account="Assets:Savings:Bank:BOC0814", currencies=["CNY"], alias="中国银行 0814", reconcile="monthly")
ledger.update_draft(draft_id="draft-...", payload={"payee": "星巴克"})
ledger.submit_draft({...}) # 入队,不写账本
ledger.approve_draft(draft_id="draft-...") # 校验 → 原子追加
ledger.query_transactions(date_from="2026-10-01", date_to="2026-10-31")
ledger.query_balances(as_of="2026-11-17")
待办队列:人 vs Agent
人在界面上点的操作(开户、改别名/周期、对账)本身就是人工确认,直接执行。Agent 发起的写入一律入队等人批准:交易进草稿队列(submit-draft),结构变更进操作队列(submit-operation,kind 取 account-open / account-redate / account-metadata / balance)。批准结构变更时本库会自动重算待审草稿的账户解析,让原本卡在 unresolved_account 的草稿立刻可批。
LedgerError 带 code、message、details;UI/CLI 直接展示 code 对应的本地化文案。
数据放在哪
- 别名、对账周期:账本
open指令的 metadata。 - 对账到期日:每次从最近
balance断言和周期重建,不落库。 - 草稿、锁、审计:账本根目录的
.ledger/(运行态,已在账本源.gitignore)。 - 正式交易:审核批准后追加到
trans/collect.bean;对账追加/替换trans/reconciliation.bean。
CLI 契约
任何语言都可以通过子进程调用:
python -m beancount_guard --root /path/to/ledger setup
python -m beancount_guard --root /path/to/ledger bootstrap
python -m beancount_guard --root /path/to/ledger balance <<'JSON'
{"account": "工行5244", "amount": "-30.00", "currency": "CNY"}
JSON
stdout 固定为:
{"ok": true, "value": {...}}
{"ok": false, "error": {"code": "...", "message": "...", "details": {}}}
业务失败也返回退出码 0;用法/内部错误返回 1。命令:health、setup、init、bootstrap、status、accounts、resolve、due、metadata、account-open、account-redate、balance、reconcile-preview、reconcile-commit、reconcile-history、refresh-drafts、approve-all-drafts、op-submit、op-list、op-get、op-approve、op-approve-all、op-reject、list-drafts、get-draft、submit-draft、update-draft、approve-draft、reject-draft、quick-draft、query-transactions、query-balances、validate、audit-metadata。
运行时
本库只依赖标准库;校验和查询调用外部程序:
bean-check来自beancountbean-query来自beanquery
查找顺序:显式配置路径 → 账本 .venv/bin → 当前解释器同目录 → PATH。最后一项让托管 venv 里的本库直接找到同一个 venv 的 Beancount 命令,不需要再配置 beanCheck。
前端可以为普通用户托管这个运行时(私有 venv + 一键安装/修复);本库本身不关心安装方式,也不负责创建 venv。
写入门禁
- 候选内容写入影子树(未改动文件用 symlink,改动文件写真实副本;Windows 无 symlink 权限时退化为复制,只复制内容、不继承只读等属性,回指祖先的目录链接跳过以免无限递归)。
- 在影子树运行真实的
bean-check。 - 通过后,在
.ledger/write.lock锁内os.replace原子替换目标文件;目标只读或被占用时返回ledger_not_writable,不擅自改文件属性。 - 失败时返回
bean_check_failed/balance_mismatch,原文件零改动。 - 影子目录用完即删;上次残留(超过一天)会在下一次校验前自动清掉。
测试
cd beancount-guard
PYTHONPATH=. python -m unittest discover -s tests -v
测试在临时账本副本上覆盖:别名冲突、对账重建、余额不一致零写入、草稿流转、并发写、崩溃恢复、查询投影。
对账闭环
- 到期:有 balance 时
nextDue = add_period(balance.date) - 1 天;没有 balance 时立即进待办,unreconciled = true。 - 预览/提交:
reconcile-preview返回 expected / difference / allocations / directives / hashes;reconcile-commit在同一写门禁里原子写transaction* + pad? + balance。 - 更新:同一
asOf再次提交走mode=update,替换最近一次 block,旧历史superseded;更早期间不可改。 - 补记:对账后仍允许日期 ≤ asOf 的草稿。有 pad 时被吸收;没有 pad 时
approve-draft自动往最近一次 block 插入 pad;自动补 pad 失败才返回reconcile_update_required。 - 历史:
.ledger/reconciliations/<id>.json,记录 expected/actual/allocations/pad/balance/directives/hash/文件行,可回放、可审计。
已知限制
init_ledger()使用内置模板生成新账本;模板仓库ledger-domain-assets由前端决定是否复制,本库不依赖它。- 导入已有账本(选择目录/校验/接管)还没有做;当前需要把前端
root指向已有账本。 - 交易/对账写
trans/collect.bean/trans/reconciliation.bean;create 追加,update 原位替换最近一次 block;文件不存在时报ledger_not_initialized。 - 草稿只支持
*/!;每个 posting 必须显式给金额;不支持成本/价格/自动平衡。 - 由
auto_accounts隐式创建、没有open指令的账户可查询/对账/进草稿,但不能维护别名与周期。 bean-check/bean-query是外部程序;不导入 Beancount Python 包。- 账本文件只接受 UTF-8:读侧记为
invalid_encoding问题,写入返回ledger_encoding_error;GBK/ANSI 保存的文件需先另存为 UTF-8。 - 账本绝对路径过长(Windows 260 字符上限)时,影子副本复制返回
shadow_copy_failed并带出错路径;需要把账本移到更短的路径。 - 托管 venv 首次安装需要从包索引安装
beancount与beanquery;装好后可离线。 - 对账漂移当前不主动告警;
validate/reconcile-preview不返回reconciliation_drift。
CI
Jenkins Multibranch Pipeline,配置与本地等价命令见 docs/CI.md。
Metadata
Release files for beancount-guard 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| beancount_guard-0.1.0.tar.gz | 82.4 kB | Without provenance | Details for beancount_guard-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| beancount_guard-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for beancount_guard-0.1.0-py3-none-any.whl |
Total release size: 159.2 kB
Release files / beancount_guard-0.1.0.tar.gz
| Download URL | beancount_guard-0.1.0.tar.gz |
|---|---|
| Size | 82.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
909d36c58f90ed0b4b2721bd00006eba5449d14d696db4f9bcedd84eb5096c3f
|
|
BLAKE2b-256 checksum How to use checksums |
11662f30b0b4a9d199129df563b159f255f8c2dcc953cfc50524b5c0d48f379b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Release files / beancount_guard-0.1.0-py3-none-any.whl
| Download URL | beancount_guard-0.1.0-py3-none-any.whl |
|---|---|
| Size | 76.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
49df08fcf1faed295b90a8a0a0bf5a20dd2ffdc63d4b3536b5c0a28685f7781c
|
|
BLAKE2b-256 checksum How to use checksums |
a195fff5535c4d9e4b75704e2c6fb417483c3b45e40853a48040d3970c5f9e0d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Security for beancount-guard 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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.