fix: preserve correction and legacy authority semantics

This commit is contained in:
2026-10-06 09:05:54 +00:00
parent 921f130fb0
commit a4cad342a6
4 changed files with 60 additions and 6 deletions
@@ -266,19 +266,19 @@ def correct_account_valuation(valuation_id: str, amount: int, correction_reason:
source = conn.execute("SELECT * FROM account_valuations WHERE id=?", (valuation_id,)).fetchone()
if source is None:
return fail("not_found", "valuation not found")
preserved = {name: fields[name] if name in fields else source[name] for name in (
"currency", "valuation_kind", "source_type", "source_ref", "verification_status", "memo", "as_of_at", "timezone",
"cost_basis", "net_contributed", "reported_principal_kind", "reported_principal_label", "profit", "return_rate", "return_basis", "principal", "interest", "fee", "representative_liability_definition")}
if idempotency_key:
prior = conn.execute("SELECT * FROM account_valuations WHERE account_id=? AND idempotency_key=?", (source["account_id"], idempotency_key)).fetchone()
if prior:
expected = {"amount": amount, "supersedes_id": valuation_id, "correction_reason": correction_reason.strip()}
expected.update({k: fields[k] for k in fields})
expected.update(preserved)
if all(prior[k] == v for k, v in expected.items() if k in prior.keys()):
return ok(valuation=_valuation(prior), reused=True)
return fail("idempotency_conflict", "idempotency_key already has a different payload", valuation=_valuation(prior))
if conn.execute("SELECT 1 FROM account_valuations WHERE supersedes_id=?", (valuation_id,)).fetchone():
return fail("correction_conflict", "valuation already has a correction")
preserved = {name: fields[name] if name in fields else source[name] for name in (
"currency", "valuation_kind", "source_type", "source_ref", "verification_status", "memo", "as_of_at", "timezone",
"cost_basis", "net_contributed", "reported_principal_kind", "reported_principal_label", "profit", "return_rate", "return_basis", "principal", "interest", "fee", "representative_liability_definition")}
return register_account_valuation(
source["account_id"], source["basis_date"], amount,
**preserved,
+17 -1
View File
@@ -573,8 +573,10 @@ def get_asset_snapshot_report(basis_date: str = "", selection_policy: str = "exa
basis = normalize_date(basis_date, default_today=True) if basis_date else current_kst_date().isoformat()
with db(db_path) as conn:
rows, close_metadata = _snapshot_rows_for_latest_run(conn, basis)
has_close_run = bool(close_metadata.get("snapshot_run_id"))
snapshots = _snapshot_items(conn, basis) if rows else []
if close_metadata:
legacy_snapshots = snapshots if rows and not has_close_run else []
if has_close_run:
summary = _summarize_valuation_items(snapshots, [])
return ok(
basis_date=basis,
@@ -590,6 +592,19 @@ def get_asset_snapshot_report(basis_date: str = "", selection_policy: str = "exa
manual_items, manual_excluded = _manual_asset_valuation_items(conn, basis_date=basis, selection_policy=selection_policy)
included = account_items + manual_items
excluded = account_excluded + manual_excluded
if not included and not excluded and legacy_snapshots:
summary = _summarize_valuation_items(legacy_snapshots, [])
return ok(
basis_date=basis,
source="legacy_asset_snapshots",
selection_policy=selection_policy,
close_status="legacy_read_only",
is_closed_snapshot=False,
snapshot_count=len(legacy_snapshots),
legacy_snapshot_row_count=len(legacy_snapshots),
close_metadata={"legacy": True, "snapshot_run_id": ""},
**summary,
)
summary = _summarize_valuation_items(included, excluded)
return ok(
basis_date=basis,
@@ -598,6 +613,7 @@ def get_asset_snapshot_report(basis_date: str = "", selection_policy: str = "exa
close_status="incomplete_draft" if excluded else "complete_exact",
is_closed_snapshot=False,
snapshot_count=0,
legacy_snapshot_row_count=len(legacy_snapshots),
invalid_bucket_accounts=invalid_bucket_accounts,
**summary,
)