Bahi
Equity research with a tamper-evident record
- Equity research system
- Python
- Temporal
- Postgres
- FastAPI
- Next.js
Overview
Bahi is a research workbench for an equity analyst working on India's Nifty 50; five companies are fully built so far. A planner model with four sub-agents answers from filings and daily prices. The model never writes a number. Every figure comes from one of 25 registered calculations, a checker computes it again by another route, and a recommendation waits for the analyst's signature.
Every step of a run, from each model call to the signature, is written to an append-only, hash-chained record in Postgres that can be checked later, offline if needed. The build put that record and the agents' discipline first and kept the research itself small. It shares the append-only record and the planted-error test with FinScientist, though no code.
How it works
The tamper-evident record
Each step is written to the record as it happens, so a later change to any entry or stored payload, in the database or on disk, can be detected.
01 Store and chain
Every model call, tool call, gate decision and signature is a step. Its full prompt or output is stored under the SHA-256 of its bytes, written once and made read-only. The step's entry holds hashes and a payload key, not text; its own hash covers the previous entry's, so each new entry fixes everything written before it.02 Refuse
In Postgres an insert trigger recomputes the hash itself and checks that the entry extends the chain. Other triggers refuse UPDATE, DELETE and TRUNCATE for every role, the owner's included.03 Checkpoint
New entries are folded into a Merkle root, signed with ed25519 and written to write-once storage. This runs hourly as a separate command that is not started by default, and before every export.04 Verify
diary verify recomputes every hash, link and signed root. A row changed outside the database's rules no longer matches the chain after it or the signed root, and the check fails.05 Export
One run exports as a bundle of its entries, Merkle proofs into signed checkpoints, and payloads. It verifies offline with only the public key.
From filing to facts
The Library is built from company filings by a reader that is not trusted, so its answers pass the same checks whether a model API or a Claude Code session produced them.
01 Hash
A filing is stored under the SHA-256 of its bytes. Its page words come from the PDF's text layer, or from OCR run on the machine when the page is a scan.02 Read
Each statement page becomes one task. A reader maps the page's rows and columns to metrics. For the five built companies this reading was done in a Claude Code session, not by a model API.03 Find and balance
Every value must be found on the row the reader named, in the page's own words; a value that is not there is dropped rather than kept. Then the page's accounting identities must hold: total income is revenue plus other income, the profit lines follow from each other, assets equal equity plus liabilities, expenses add up.04 Publish
A fact keeps its document, page, position and value as printed, the period it covers and when it became known. A restatement closes the old fact's range; nothing is deleted.05 As of
Runs read facts only through SQL functions that require a date and return nothing known after it. Publication dates are estimates that can be late but never early.
One research run
A research run is one Temporal workflow in which the planner chooses the steps, but fixed checks sit between it and every tool, and between its draft and the analyst.
01 Plan
A planner model writes a plan of four to ten items. It can hand a task to one of four sub-agents (reader, quant, skeptic, writer), each with its own short context and a typed result.02 Gate
Every tool call passes a gate: is the tool allowed in this mode, is budget left, does it serve an open plan item, has it been repeated too often. The run's as-of date replaces any the model sent.03 Recheck
Numbers come only from registered, versioned calculations. The checker computes each one again in SQL and runs the accounting identities on its inputs. A mismatch blocks.04 Lint
The writer drafts prose with a slot for each value. Before anything is shown, a lint blocks any digit outside a slot, and every slot must name a calculation that passed the checker.05 Stop and seal
A checker failure, a recommendation or a trade level stops the run for the analyst, who decides what happens next; nothing is corrected automatically. The analyst signs with a SHA-256 of the decision, tamper evidence rather than a proof of identity, and the run is sealed in the record from Fig. 01.
What this doesn't cover
- It is a research tool, not a trading system: it places no trades and has no portfolio, backtest or returns, and its recommendations use a placeholder scale that the analyst has to sign. Nothing on this page is investment advice.
- Five companies are fully read into the Library (Bharti Airtel, UltraTech Cement, Larsen & Toubro, Hindustan Unilever, Infosys) and ten more partly; reading new filings is switched off for now. The data is free: Yahoo Finance prices through an unofficial client, for personal, non-commercial use and open to later revision; Wikipedia's current index list, so screens over past dates carry survivorship bias; and filing dates that are estimated, not taken from an exchange.
- Little is validated at scale. Of 34 scripted agent tasks, 7 have been run with real models, once each, and 5 passed. The checker catches all 266 planted errors, which the code generates, but the 266 labels that score page reading were made by Claude Code and no person has checked them. Cash-flow, segment and earnings-per-share checks from the plan are not built. The project has 453 Python test functions and 18 browser tests; I did not re-run them.
- It is not production-ready: there is no real login, one-off code is sandboxed in Python only, storage is local write-once files rather than locked cloud storage, and the hourly checkpointer is a separate command that is not started by default. Every figure here is a schematic drawing.