Matsya developer guide¶
Summary
Matsya is a service with two modes. In architect mode it reads a paper or a typed description of a dynamic economic model, writes the model's declaration in Bellman-SYM, the project's language for staged Bellman problems, as stage files together with a prose description of the model, and checks the two against each other; in conversation mode it answers questions with passages retrieved from an index of the project's documents and literature, with one literal excerpt of a passage or, when the configuration enables the optional conversation role, with a language model's answer to a question asked in a session, each claim of which cites material supplied to the language model and quotes it. Its service and training code reside in two packages; separate operator scripts prepare the index and guides. Everything that training revises, the instruction texts, the settings and the Matsya handlers, is data in one configuration folder outside the packages; training changes nothing in the language models themselves. Section 3 lists which page of the guide answers which question.
Relationship with other documents
Builds on — Bellman-SYM, the language in which Matsya writes model declarations, and Matsya instructions, which defines the role texts used in each job.
Used by — the Matsya user guide, which names the three parts, matsya-client, matsya-architect and matsya-master, and follows one piece of work through the command bellman-matsya; the access instructions distributed with the service give the installation, the environment variables and a Matsya token; the operator's deployment instructions describe installation on the DigitalOcean server, a rented virtual machine that runs the service.
See also — the wiki pages Matsya collection, Matsya handler, Matsya instructions and Reference guides in Matsya, which define the terms this guide uses.
1 Program and configuration¶
Matsya consists of two kinds of thing, and the kind a change alters determines where the change is made and whether a training pass may make it. A training pass is one round in which a revised configuration is run on a set of papers, its results are measured against those of the configuration in use, and the revision is accepted or refused under the governing document's acceptance rule; the language models themselves are not changed.
The program is fixed code, tested by the packages' own tests (venv/bin/python -m pytest packages/matsya/tests, 282 tests on 9 October 2026) and changed separately from the configuration that a training pass revises. The service's code has two packages, with resource preparation in separate operator scripts. packages/matsya is the frame: it holds the web service and the retrieval code, loads and checks the configuration, and builds for each job the job frame, the record (the class Frame of frame.py) through which a Matsya handler uses everything else. The job frame gives a Matsya handler the function that sends a message to the language model's provider, the retrieval index, the Bellman-SYM elaborator BellmanSYMDeclaration_to_ElaboratedSemanticObject (the program that checks stage files with the bellman package's functions and constructs from them the elaborated semantic object \(E\), the complete typed statement of the problem the files state: an elaborated stage for one stage file, an elaborated trellis for several), the comparator and the job's record. packages/meaning-map-training holds two programs that a job uses: the comparator (compare.py), which decides whether two elaborated semantic objects have the same meaning and which a job uses as ElaboratedSemanticObject_comparator through compare_outcomes of meaning.py; and the form check (form_check.py), which tests whether a description has the required headings and length. The program that measures a training pass (round_trip.py and round_trip_eval.py) imports both. Since 15 November 2026 it is kept with the training's records, outside this repository. The program also fixes the slots that training fills, that is, the names under which the configuration must supply its contents and the form each content must take: the seven role keys (a role is one of the seven tasks given to a language model under an instruction text, such as SourceMaterial_to_BellmanStageProse, the Model-prose-writer, which writes the model prose from the source, or BellmanStageProse_judge, the judge), the thirteen Matsya handler names with their parameter lists, and the fifteen keys of the settings file with their types; and, for the optional conversation role, which a configuration enables by naming it among its roles, one more role key, conversation_answer, one more Matsya handler name, conversation_reply, and two more settings keys, conversation_rounds and conversation_pages, which the program requires together with the role's instruction text and refuses in a configuration that does not name the role. It answers the requests that the Model-prose-writer and Prose-to-Bellman-Sym may make for further documentation through the reading controller, which holds each role within the allowances of the settings' reading and places in a message only passages of the collections the user's grants allow, the public collections for every user and each private collection under a grant, whether or not a passage is marked model_prompt (What Matsya reads §5). It counts the language-model tokens of every message before the message is sent (a token is the unit of text, a word or part of a word, in which a provider counts and prices a message) and holds each job, and each turn of conversation mode, within the token ceilings of the settings, which a training pass does not change (What Matsya reads §4). It also fixes what a Matsya handler may report: the six kinds of event and their fields (_EVENT_FIELDS in jobs.py), the 27 validation codes, the names under which a Matsya handler reports that a language model's answer lacks the required structure or evidence (MODEL_VALIDATION_CODES in frame.py) and the fields of a request (REQUEST_FIELDS in processor.py).
The configuration, by contrast, is one folder of data supplied to the service: the service keeps every published configuration under the folder MATSYA_CONFIGURATIONS_ROOT names, each under its ID, and runs each job and turn under the one pinned to it (Updating Matsya configurations). The governing document writes it as \(\mathfrak{K} = (\mathfrak{t}, \mathfrak{s}, \mathfrak{h})\): the instruction texts \(\mathfrak{t}\), one file per role under texts/; the settings \(\mathfrak{s}\) in settings.json; and the Matsya handlers \(\mathfrak{h}\), the Python functions that run a job, in the module matsya_handler.py, the name the constant HANDLERS_FILE of the configuration loader (configuration.py) requires. A training pass revises this folder and never the packages. The folder also holds three reference guides, saved copies of published documentation pages that the Matsya handlers include in a language model's message, and manifest.json, which lists the SHA-256 digest of every other file (a digest is a 64-character code computed from a file's bytes, which changes whenever any byte changes). The program checks the folder against the manifest when the service starts and again at the start of every job, and refuses a folder that fails any check as a whole, so that no language model is called under an unchecked configuration.
| Part | Where | Program or configuration | What checks it |
|---|---|---|---|
The web service: the job routes (POST /v1/model-iterations, GET /v1/model-iterations/{job_id}, POST /v1/model-iterations/{job_id}/cancel), the search and answer routes (POST /v1/search, POST /v1/answer, GET /v1/passages/{passage_id}, GET /v1/index), the sessions (named conversations with Matsya, §2, with POST /v1/sessions/{session_id}/selected-job, which chooses the job a session's conversation discusses) and the turns of conversation mode (POST /v1/sessions/{session_id}/turns, GET /v1/sessions/{session_id}/turns/{turn_id}), the Matsya tokens, the job runner and the turn runner |
packages/matsya/matsya_service/: api.py (the routes and the application's start-up), auth.py (Matsya-token authentication), jobs.py (the job database with the sessions' numbered entries and the turns, and the job runner), conversation.py (the turn runner of conversation mode and the checks of a turn's answer), serve.py (the command matsya-serve), collection_access.py (the grants under which the private collections are read), answer.py (the extractive answer, a short literal excerpt from retrieved passages) |
program | the package's tests |
| The modules that load the configuration and build the job frame: processor, roles, source, guide pages, meaning, configuration loader | packages/matsya/matsya_service/: processor.py, frame.py, roles.py, source.py, guide_pages.py, meaning.py, configuration.py |
program | the package's tests |
| The retrieval code: extraction, embedding, storage, search, and the reading controller through which a job reads the index and the guides' records | packages/matsya/matsya_service/retrieval/ |
program | the package's tests; the retrieval checks at every build, each a search that must return a passage of a named source |
| The operator-run source compiler, index builder and transfer script | compile_retrieval_manifest.py, build_retrieval_index.py and retrieval_bundle.py, maintained separately from the installed package |
program | the package's tests and the retrieval checks at every build |
| The operator-run guide builder and its construction policy, and the shared parser of links and tags | matsya_guide_builder.py and guide_policy.json, maintained separately from the installed package; packages/matsya/matsya_service/document_metadata.py |
program | the package's tests |
| The comparator and the form check | packages/meaning-map-training/ |
program | its tests (venv/bin/python -m pytest packages/meaning-map-training, 107 tests on 15 November 2026) |
| The instruction texts \(\mathfrak{t}\) | the configuration folder, texts/<role key>.md, the key's underscores written as hyphens |
configuration, trained | the manifest's digests |
| The settings \(\mathfrak{s}\) | the configuration folder, settings.json |
configuration, trained, except ceilings, the language-model token ceilings, which a training pass holds fixed |
the manifest; fifteen keys of fixed name and type, and two more when the configuration enables the conversation role |
| The Matsya handlers \(\mathfrak{h}\) | the configuration folder, matsya_handler.py |
configuration, trained | the manifest; thirteen names with their parameter lists, and conversation_reply when the configuration enables the conversation role |
| The reference guides and the lists of pages each role receives | the configuration folder, guides/; the page lists in settings.json |
configuration (the guides are copies of published pages, built under the program's construction policy, and are not trained; the page lists are settings and are trained) | the manifest; the page markers against guides/manifest.json and, for a manifest of the form matsya-guides/3, its records of sections, links and tag memberships |
| The retrieval index | a private folder outside every checkout, named by MATSYA_INDEX_ROOT |
built data, neither program nor trained | its own digests; the retrieval checks |
| The list of indexed sources and its compiler | source-selection.json and compile_retrieval_manifest.py, maintained with the operator's deployment files |
development records | the compiler's checks |
2 The path of one job¶
A job of architect mode starts from a request to POST /v1/model-iterations, which contains a source, either {"kind": "description", "text": ...} or a paper as its text in Markdown or LaTeX, {"kind": "paper", "text": ...}, and the job's target, the level of a declaration the job returns, stage, period, trellis or recipe, without which the request is refused; and may contain max_cycles (the cycle limit, at most cycle_maximum), starting_declaration (file names with their texts, elaborated in place of the Bellman-SYM writer's first call), model_key and source_cluster (two labels; a source whose source_cluster in the list of indexed sources equals either is left out of the job's searches and requests, so that the paper's own text is not retrieved for it, and without them every clustered source is left out), formulation (the one formulation of the model to write when the source states several), session (a named conversation with Matsya, a record of numbered entries, whose entries up to the session's revision when the job starts the job takes as its source, an answer of Matsya among them only when the user accepted it; a job of a session continues the session's earlier job, whose identity it records) and force (write even when preparation ends with open questions). The service's function for this route checks the request against the configuration's cycle_maximum, writes a job to the job database and passes it to the job runner, which runs at most two jobs at the same time. The caller receives a job identity and reads the job's state, its label (the session's name, the job's target, the version of the session's text the job was built from, the job's number in the session and its state), its progress and its result from GET /v1/model-iterations/{job_id}, and may cancel a queued or running job with POST /v1/model-iterations/{job_id}/cancel: a queued job never runs, and a running job stops before its next call of a language model, a call already sent finishing and being charged, and ends cancelled.
The job runner calls the processor, ModelProcessor.process in processor.py. The processor checks the request again, loads and checks the configuration folder, turns the source into a Source record through the Matsya handler normalize_source, builds the job's language-model token ledger, the account of the tokens charged to each call, from the settings' ceilings, takes the retrieval index once for the whole job, builds the job frame from the texts, the guides, the settings, the function that sends a message to the provider, the job's record as begun by the processor, the job runner's progress callback, the index and the source, and calls the Matsya handler cycle. From here on the configuration's code runs the job and the program only answers its calls.
The Matsya handler cycle runs preparation first: the source preparer writes the description \(M\) from the source material \(P\), and the judge compares \(M\) with \(P\). The Bellman-SYM writer then writes the initial stage files \(D\) from \(M\), and the elaborator constructs \(E\) from \(D\). Numbered iteration 1 writes \(M'\) from the dossier of \(E\), reports any difference between \(M\) and \(M'\), writes \(D'\) from \(M'\), elaborates it to \(E'\), and compares \(E\) with \(E'\). If they differ, iteration 2 begins from \(E'\) and produces \(M''\), \(D''\) and \(E''\); it compares \(E'\) with \(E''\). The cycle limit counts these numbered iterations, not the initial writing. The elaborator's refusal is sent back to the Bellman-SYM writer for repair within a writing step. A failed iteration that produces no new elaborated object has no equality verdict. For a stage target, the comparator reads one elaborated stage; for a period, trellis or recipe target, it reads the stages and their joins. The prose judges, the # from: quotations and the writer's note remain diagnostic records; none decides convergence. The job record keeps each language-model call as a digest and token counts, together with the configuration version and the identities of the pages and passages each message held. A call that would exceed a role's or job's token ceiling is refused before it is sent, and the processor ends the job not_converged with the reason token_ceiling_reached.
The source material \(P\) (SourceMaterial) is prepared into the description \(M\) (BellmanStageProse); \(M\) is declared in the stage files \(D\) (BellmanSYMDeclaration); and their elaboration is the elaborated semantic object \(E\) (ElaboratedSemanticObject). Each later prose, declaration and elaboration is one application of the same maps to the preceding elaborated object. Four roles and two programs compute these maps. A role's adopted name is its key in the program; the two programs keep their function names and state the adopted name in their docstrings.
| Concept | Symbol | Adopted name | Arrow | Computed by |
|---|---|---|---|---|
| the source preparer | SourceMaterial_to_BellmanStageProse |
\(P \to M\) | a language model under its instruction text | |
| the Bellman-SYM writer | \(\Upsilon_0\) | BellmanStageProse_to_BellmanSYMDeclaration |
\(M \to D\), \(M' \to D'\), \(M'' \to D''\) | a language model under its instruction text |
| the Bellman-SYM elaborator | \(S\) | BellmanSYMDeclaration_to_ElaboratedSemanticObject |
\(D \to E\), \(D' \to E'\), \(D'' \to E''\), or a refusal | elaborate_files of meaning.py, which checks the files at the job's target and calls elaborate_text, elaborate_trellis and, for a recipe, load_recipe and specify_trellis of the bellman package |
| the prose writer | \(\Upsilon_1\) | ElaboratedSemanticObject_to_BellmanStageProse |
\(E \to M'\), then \(E' \to M''\), from each dossier and nothing else | a language model under its instruction text |
| the judge | \(J\) | BellmanStageProse_judge |
\((M, M') \to\) a table of agreement on ten items; in preparation also \((P, M)\) | a language model under its instruction text |
| the comparator | \(\cong\) | ElaboratedSemanticObject_comparator |
\((E, E')\), then \((E', E'')\), to comparison profiles | compare_outcomes of meaning.py, over the comparator of packages/meaning-map-training |
The wiki pages Bellman-SYM writer, Prose writer and Judge define the three maps a language model computes in a cycle.
A turn of conversation mode. When the configuration enables the conversation role, a question asked in a session through POST /v1/sessions/{session_id}/turns is appended to the session as an entry of the user and answered in a turn, which runs on a runner of its own with two workers, so that a running job does not delay an answer. The turn runner, TurnRunner of conversation.py, loads the configuration, fixes the snapshot of the index the turn reads, binds the reading controller to it, to the collections the person asking may read and to the conversation role's allowances, elaborates a stage file the question names, and calls the Matsya handler conversation_reply, which searches once from the question, calls the role and has the controller answer its requests. The turn runner then checks the answer: every citation must name material supplied to the role in the turn and quote it. It appends the answer, or the role's questions for the user, to the session and stores the turn's record, which holds digests, identities and counts and no message text. An answer of the role enters the source material of a later job only when the user accepts it with an entry of the kind acceptance.
3 Where to read next¶
| Question | Page or document |
|---|---|
| How is the retrieval index built, by which modules, and how is a search answered? | The retrieval index; for the request's fields and the weights, Collections and weights |
| Which texts, pages, settings and passages are placed in a language model's message, and in what form? | What Matsya reads |
| Where are the Matsya handlers, what does each do, how is one changed or added? | The Matsya handlers |
| How are configurations prepared, published, selected, compared and made the default? | Updating Matsya configurations, with current behavior separated from the proposed process |
| How is a question asked in a session answered by the conversation role, and what does its message hold? | What Matsya reads §8 and The Matsya handlers §7 |
| How is the service installed and started, and what does one request look like? | The retrieval index §9, the command matsya-serve and its six environment variables; Reaching the service by HTTP, every request and its answer |
| What does building and activating an index require? | The retrieval index §§2–9 |
| Where are the service modes, a job, the configuration and deployment explained? | The guide’s first page §§1–2, The Matsya handlers §§1–3 and The retrieval index §9 |