Files
sure/app/models/assistant/function/statement_vault_support.rb
Claude ec38a89d0c Expose the Statement Vault to external agents over MCP
A user wants to manage patrimonial history — a document-backed record of a
family's wealth where every figure traces back to the statement it came from —
by pointing an external agent harness at Sure. That model belongs in the
harness, not in Sure: it needs numbered build deltas, golden tests and closed
periods that a mutable Postgres row cannot provide.

What Sure was missing was the seam. The Statement Vault already does most of
the work — original bytes retained, SHA-256 dedup, period detection, account
matching with a confidence score, reconciliation against ledger balances, and a
month-by-month coverage map — but it is reachable only from the web UI. An
agent could not archive a document, cite one, or check for gaps.

Adds five preview MCP tools over what already exists, plus a citation grammar
for values the agent writes:

- upload_account_statement, list_account_statements, get_account_statement,
  get_statement_coverage
- record_valuation, whose source citation is parsed rather than trusted:
  ["estimated: "] citation [" (grade: A|B|C)"]. An uncited or free-styled
  value is rejected at the write boundary instead of landing in the ledger
  looking authoritative.

link and reject are deliberately not exposed. Attaching a statement to an
account is the human's decision, and the vault UI is where it is made; the
agent reports the suggested match and stops there.

Assistant.function_classes now takes a user so preview tools stay out of the
default surface. They are hidden from tools/list and not callable by name
without the preference enabled, and the vault tools re-check the manager role
and per-account permissions, since MCP calls never pass through a controller.

Docs: the blueprint this implements, and a guide covering which side owns which
layer, the vocabulary map between the two, the monthly runbook, and the gaps
(non-user holders, non-statement documents, one value per date).

No migrations, no API endpoints, no UI.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFDp9HhXDeswadu4cxFojn
2026-08-01 00:00:12 -07:00

69 lines
2.3 KiB
Ruby

# frozen_string_literal: true
# Shared plumbing for the Statement Vault tools. The vault is reachable from the
# web UI through AccountStatementsController, which enforces the manager role and
# per-account permissions with before_actions. MCP tool calls never pass through
# a controller, so every tool re-checks the same rules here rather than trusting
# that the caller came from a gated surface.
module Assistant::Function::StatementVaultSupport
private
def statement_manager?
AccountStatement.statement_manager?(user)
end
def not_a_statement_manager
error(
"forbidden",
"This user is not allowed to manage the Statement Vault. Statement access requires an admin or member role."
)
end
def find_statement(id)
return nil unless id.present? && valid_uuid?(id)
family.account_statements.find_by(id: id)
end
# Identity and provenance first: the fields an agent needs to cite a document
# and decide whether to fetch it. Excerpts are the vector store's job.
def statement_payload(statement)
{
id: statement.id,
filename: statement.filename,
content_sha256: statement.content_sha256,
content_type: statement.content_type,
byte_size: statement.byte_size,
period_start_on: statement.period_start_on&.iso8601,
period_end_on: statement.period_end_on&.iso8601,
currency: statement.statement_currency,
opening_balance: statement.opening_balance&.to_s,
closing_balance: statement.closing_balance&.to_s,
review_status: statement.review_status,
account: account_ref(statement.account),
suggested_account: account_ref(statement.suggested_account),
match_confidence: statement.match_confidence&.to_f,
institution_name_hint: statement.institution_name_hint,
account_last4_hint: statement.account_last4_hint,
uploaded_at: statement.created_at.iso8601
}.compact
end
def account_ref(account)
return nil unless account
{ id: account.id, name: account.name, currency: account.currency }
end
def error(key, message, extras = {})
{ success: false, error: key, message: message }.merge(extras)
end
def parse_date(value)
return nil if value.blank?
Date.iso8601(value.to_s)
rescue Date::Error
nil
end
end