Files
sure/app/models/assistant/function/get_account_statement.rb
Claude 74b3fbac8b Correct the vault tools' reconciliation claims and citation parsing
Review findings from @diegomarino, all verified against the code before
changing anything.

The reconciliation claim was the serious one. get_account_statement told
agents the checks were "the trustworthy part" and returned "the balances read
off it" — but nothing reads balances off a document. MetadataDetector never
touches them and create_from_prepared_upload! never sets them; they are
user-editable fields in the Statement Vault UI. So a statement archived over
MCP always came back with an empty check list, which an agent could easily
read as "the document agrees with the ledger" when it means "nobody has
entered the figures". The description now says so, and the payload carries a
reconciliation_note spelling it out for anything reading only the JSON. Also
noted that these checks are ledger agreement, not parse integrity: nothing
here verifies a document's parts sum to its printed total.

Provenance::Citation had two patterns disagreeing about spacing. GRADE_SUFFIX
allowed "(grade:A)" but FORMAT required exactly one space, so that citation
passed the pre-check and then parsed as ungraded with the grade swallowed into
the text — silently discarding the reliability the caller supplied, which is
the one thing this parser exists to prevent.

list_account_statements downcases content_sha256 before querying. The column
is constrained to lowercase hex, so uppercase input could never match, and an
agent would read the empty result as "not archived" and upload a duplicate.
Its period filters are renamed overlapping_from / overlapping_until, since
they match on overlap and the old names claimed otherwise to anyone reading
the schema without the descriptions. has_more now explains that there is no
cursor and the way forward is a bigger limit or narrower filters.

record_valuation no longer overwrites the entry's notes. Re-recording a date
would destroy a note a person had written there. Nothing is removed now: an
identical citation is a no-op, a changed one is appended, and the trail of
what was cited when survives. Detecting "did this tool write that line?" is
not possible — almost any prose parses as a valid ungraded citation — so the
code does not guess.

Minor: accept urlsafe base64 on upload, and explain in the code why
record_valuation checks the account ACL rather than the vault manager role, so
nobody "tightens" it into the wrong permission later.

Tests cover each: the grade-spacing cases both ways, uppercase SHA lookup,
overlap window boundaries, note preservation and no-stacking, the unavailable
reconciliation note appearing and disappearing, and — per the review — that
the download URL's signed id actually expires, rather than trusting the
description's claim.

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

131 lines
4.6 KiB
Ruby

# frozen_string_literal: true
class Assistant::Function::GetAccountStatement < Assistant::Function
include Assistant::Function::StatementVaultSupport
DOWNLOAD_URL_TTL = 15.minutes
class << self
def name
"get_account_statement"
end
def description
<<~INSTRUCTIONS
Fetch one Statement Vault document by ID: its identity (SHA-256, filename,
period), any balances recorded for it, and reconciliation checks against
the ledger.
IMPORTANT reconciliation is usually empty, and its absence means nothing.
The checks only exist once a human has typed the statement's opening and
closing balances into the Statement Vault UI. Nothing extracts them from
the document, so a statement you uploaded through `upload_account_statement`
comes back with `reconciliation_checks: []` and
`reconciliation_status: "unavailable"`. That is "nobody has entered the
figures", NOT "the document agrees with the ledger". Never report an
unreconciled statement as verified.
When the balances have been entered, each check compares that figure
against the account's recorded balance:
- `opening_balance` / `closing_balance` the statement's figure vs the ledger's
- `period_movement` the change across the period, both sides
Each reports `matched` or `mismatched` (tolerance: 0.01). A mismatch means
the ledger and the document disagree report it, and do not paper over it
by adjusting the number to fit. Note this is ledger agreement only: nothing
here verifies that the document's own line items sum to its printed total.
That parse-integrity check belongs to whatever extracted the figures.
Also returns a short-lived download URL (valid #{DOWNLOAD_URL_TTL.inspect})
for the original file when one is attached.
Example:
```
get_account_statement({ statement_id: "abc123-def456" })
```
INSTRUCTIONS
end
end
def strict_mode?
false
end
def params_schema
build_schema(
required: [ "statement_id" ],
properties: {
statement_id: {
type: "string",
description: "UUID of the statement, as returned by list_account_statements or upload_account_statement."
}
}
)
end
def call(params = {})
return not_a_statement_manager unless statement_manager?
statement = find_statement(params["statement_id"])
unless statement&.viewable_by?(user)
return error("not_found", "No statement found with that ID that this user can view.")
end
checks = reconciliation_payload(statement)
status = statement.reconciliation_status
{
success: true,
statement: statement_payload(statement).merge(
reconciliation_status: status,
reconciliation_checks: checks,
# Spelled out in the payload, not just the tool description: an agent
# reading only the JSON must not read an empty check list as agreement.
reconciliation_note: unavailable_note(status),
download_url: download_url(statement)
).compact
}
end
private
def unavailable_note(status)
return nil unless status == "unavailable"
"No reconciliation has been performed: this statement has no opening/closing " \
"balances recorded, and nothing extracts them from the document. Someone must " \
"enter them in the Statement Vault UI. This is not evidence that the statement " \
"agrees with the ledger."
end
def reconciliation_payload(statement)
statement.reconciliation_checks.map do |check|
{
check: check[:key],
statement_amount: check[:statement_amount].to_s,
ledger_amount: check[:ledger_amount].to_s,
difference: check[:difference].to_s,
status: check[:status]
}
end
end
# Chat and MCP clients render outside the request that produced the record, so
# the URL has to be absolute. Falls back to nil when no host is configured
# (e.g. a self-hosted worker with no APP_DOMAIN) rather than handing back a
# relative path an external agent cannot resolve.
def download_url(statement)
return nil unless statement.original_file.attached?
host_opts = Rails.application.config.action_mailer.default_url_options || {}
return nil if host_opts[:host].blank?
Rails.application.routes.url_helpers.rails_blob_url(
statement.original_file,
host_opts.merge(disposition: "attachment", expires_in: DOWNLOAD_URL_TTL)
)
end
end