9 Commits
Author SHA1 Message Date
Alby96andClaude Opus 5.5 63ae9d580a Merge main into ci-postgres (refs #98)
ci / test (postgres) (push) Successful in 4m8s
ci / test (sqlite) (push) Successful in 5m16s
ci / test (postgres) (pull_request) Successful in 3m44s
ci / test (sqlite) (pull_request) Failing after 5m38s
ci / image (pull_request) Skipped
ci / image (push) Successful in 1m6s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 15:59:26 +01:00
Alby96 a66e3439d8 Merge pull request 'Fase 7: raccolta, collegamento e primo giudizio delle notizie, con il classificatore ONNX' (#97) from fase-7 into main
ci / test (postgres) (push) Failing after 7m44s
ci / test (sqlite) (push) Successful in 5m36s
ci / image (push) Skipped
2026-11-15 15:34:38 +01:00
Alby96andClaude Opus 5.5 74809068af Turn off disk syncs on the CI test PostgreSQL (refs #98)
ci / test (postgres) (push) Successful in 3m13s
ci / test (sqlite) (push) Successful in 4m23s
ci / test (postgres) (pull_request) Successful in 3m41s
ci / test (sqlite) (pull_request) Successful in 4m28s
ci / image (push) Successful in 1m46s
ci / image (pull_request) Successful in 1m1s
With the restarts gone, a rerun still failed 7 tests on read timeouts. The
server log shows why: every DROP DATABASE of the tests forces an immediate
checkpoint, one at a time, and on the runner's disk the sync alone took up
to 37 seconds, so queued drops passed the client's 30-second timeout. The
service is a throwaway, so initdb now sets fsync, synchronous_commit and
full_page_writes off. docker-compose.yml and real databases keep them on.
ADR 0015 updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 15:27:48 +01:00
Alby96andClaude Opus 5.5 73f2345047 Back to the normal CI jobs after the stress run (refs #98)
ci / test (sqlite) (push) Successful in 4m34s
ci / test (sqlite) (pull_request) Successful in 4m56s
ci / test (postgres) (push) Failing after 9m37s
ci / image (push) Skipped
ci / test (postgres) (pull_request) Successful in 8m44s
ci / image (pull_request) Successful in 1m21s
Reverts the temporary three-round job. With --init and the shorter pg_isready,
three PostgreSQL suites on one server passed with no restart (run 321), where
the same run without them restarted the server four times (run 315). The
Docker events in the service log step stay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 14:25:59 +01:00
Alby96andClaude Opus 5.5 6ea303e1dc Run the test PostgreSQL behind an init and let pg_isready give up before Docker (refs #98)
ci / test (postgres) (pull_request) Skipped
ci / image (pull_request) Waiting for needed jobs
ci / test (postgres) (push) Successful in 23m7s
ci / image (push) Skipped
The server log of a failing job shows every restart as 'untracked child
process exited with exit code 2'. A health check that outlives its 3-second
timeout leaves pg_isready to PostgreSQL, which is PID 1 in its container;
when pg_isready exits the postmaster takes it for a crashed backend and
restarts every connection. --init reaps such orphans, and pg_isready -t 5
under a 15-second timeout is never cut off. Same change in docker-compose.yml.
ADR 0015.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:03:44 +01:00
Alby96andClaude Opus 5.5 1aede2fc1c Let the server test accept the news feeds and the pinned model host (refs #57)
ci / test (postgres) (push) Successful in 11m31s
ci / test (sqlite) (push) Successful in 5m26s
ci / test (postgres) (pull_request) Successful in 10m7s
ci / test (sqlite) (pull_request) Successful in 5m13s
ci / image (push) Successful in 3m12s
ci / image (pull_request) Successful in 1m46s
The news collector runs alongside the market data in the server test, so its
feeds and the Hugging Face download of the classifier can reach the fake network
before the assertion: they are the hosts the code declares, nothing else.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:44:05 +01:00
Alby96andClaude Opus 5.5 72f4cbe0a5 TEMPORARY: run the PostgreSQL suite three times on one server, push only (refs #98)
ci / test (postgres) (pull_request) Skipped
ci / image (pull_request) Waiting for needed jobs
ci / test (postgres) (push) Failing after 11m1s
ci / image (push) Skipped
Two clean runs did not show the restart. This collects the server log of a
failure sooner and lists Docker's events for the service container (oom, kill,
die). Reverted before the merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:13:47 +01:00
Alby96andClaude Opus 5.5 78e77818eb Read the tone of English headlines with the pinned ONNX model of SPEC 4.5 (refs #57)
ci / test (postgres) (push) Failing after 6m20s
ci / test (sqlite) (push) Failing after 5m59s
ci / image (push) Skipped
ci / test (postgres) (pull_request) Failing after 4m51s
ci / test (sqlite) (pull_request) Successful in 5m29s
ci / image (pull_request) Skipped
The classifier is distilroberta-finetuned-financial-news-sentiment-analysis in
Xenova's quantized ONNX export, pinned at revision e6953e30 and checked by size and
SHA-256.

- Model download: the model is not in the repository. The news job downloads it in
  the background into the models folder of the data directory, the first time, and
  retries after 6 hours if that fails. Until then the keywords judge alone, as
  SPEC 10.3 requires.
- Tokenizer: RoBERTa's byte-level BPE travels inside the DLL, read with
  BpeTokenizer and the RoBERTa pre-tokenizer. CodeGenTokenizer.Create adds ids for
  runs of spaces that the model rejects. No special tokens are recognised in
  untrusted text.
- Token check: the test compares the ids on 14 headlines with an independent copy
  of the GPT-2 encoder run on the same files.
- Model test: it reads the tone of clear headlines at 23-24 ms per title on one
  thread. It runs where the model is already downloaded and is skipped in CI.

Microsoft.ML.OnnxRuntime 1.31.1 and Microsoft.ML.Tokenizers 2.0.1 come from the
ADR 0004 list. Google.Protobuf joins the known indirect dependencies.

Anubis and Anubis.Server now declare win-x64 among their runtime identifiers, so
their lock files hold the Windows native packages. Publishing the Windows zip no
longer rewrites the lock files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:00:54 +01:00
Alby96andClaude Opus 5.5 0a9a8a38ec Print the PostgreSQL service log at the end of the CI job (refs #98)
ci / test (postgres) (push) Successful in 8m16s
ci / test (sqlite) (push) Successful in 5m16s
ci / test (postgres) (pull_request) Successful in 11m38s
ci / test (sqlite) (pull_request) Successful in 4m58s
ci / image (push) Successful in 1m42s
ci / image (pull_request) Successful in 47s
The job log shows the tests losing every connection but not why the server
went into recovery. A new tasks command reads the service container's log,
state and memory through the Docker socket the runner mounts in the job.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 11:41:31 +01:00
27 changed files with 50942 additions and 31 deletions

No files matched your search

+3
View File
@@ -11,3 +11,6 @@ deploy/windows/*.txt text eol=crlf
*.ttf binary
# Recorded API responses stay byte for byte as the source sent them (CRLF in nasdaqtraded.txt included).
tests/Anubis.Tests/Fixtures/** -text
# The tokenizer of the news classifier stays byte for byte as published (hashes in ADR 0014).
src/Anubis/News/Model/*.json -text
src/Anubis/News/Model/*.txt -text
+12 -2
View File
@@ -23,10 +23,16 @@ jobs:
POSTGRES_DB: anubis
POSTGRES_USER: anubis
POSTGRES_PASSWORD: anubis-ci
# A throwaway server on a slow disk: without syncs to disk, the checkpoint that every DROP DATABASE of the
# tests forces takes milliseconds instead of up to 37 seconds (ADR 0015). Never for a real database.
POSTGRES_INITDB_ARGS: "--no-sync -c fsync=off -c synchronous_commit=off -c full_page_writes=off"
# --init and a pg_isready that gives up before Docker does (ADR 0015, issue #98). Without them a health
# check that outlives its timeout leaves pg_isready to PostgreSQL as PID 1, and its exit restarts the server.
options: >-
--health-cmd "pg_isready -U anubis -d anubis"
--init
--health-cmd "pg_isready -U anubis -d anubis -t 5"
--health-interval 3s
--health-timeout 3s
--health-timeout 15s
--health-retries 20
env:
ANUBIS_TEST_DB: ${{ matrix.db }}
@@ -45,6 +51,10 @@ jobs:
git checkout -q FETCH_HEAD
- name: Check
run: sh ./tasks.sh check
# The log of the PostgreSQL service, read through the Docker socket the runner mounts in the job (issue #98).
- name: PostgreSQL log
if: always() && matrix.db == 'postgres'
run: sh ./tasks.sh ci-service-log postgres
image:
needs: test
+5
View File
@@ -16,4 +16,9 @@
<!-- Desktop secret key on Windows (DPAPI, SPEC 15.6). -->
<PackageVersion Include="System.Security.Cryptography.ProtectedData" Version="10.0.14" />
</ItemGroup>
<ItemGroup>
<!-- Local news classifier (SPEC 4.5, phase 7): the ONNX model and its RoBERTa tokenizer. -->
<PackageVersion Include="Microsoft.ML.OnnxRuntime" Version="1.31.1" />
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="2.0.1" />
</ItemGroup>
</Project>
+5 -2
View File
@@ -31,6 +31,9 @@ services:
postgres:
image: postgres:18
# An init as PID 1 and a pg_isready that gives up before Docker does (ADR 0015): otherwise a slow health check
# leaves pg_isready to PostgreSQL, and its exit makes the server restart every connection.
init: true
environment:
POSTGRES_DB: anubis
POSTGRES_USER: anubis
@@ -38,9 +41,9 @@ services:
ports:
- "127.0.0.1:55432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U anubis -d anubis"]
test: ["CMD-SHELL", "pg_isready -U anubis -d anubis -t 5"]
interval: 3s
timeout: 3s
timeout: 15s
retries: 20
volumes:
- postgres-data:/var/lib/postgresql
+2 -2
View File
@@ -128,7 +128,7 @@ Prima di scrivere ogni adapter, si rilegge la documentazione corrente e si aggio
|---|---|---|---|---|
| M1 | `mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis`, licenza Apache-2.0 | confermato | — | https://huggingface.co/mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis |
| M2 | Esportazione ONNX pronta nei repository `Xenova/...` | confermato | `Xenova/distilroberta-finetuned-financial-news-sentiment-analysis`: `onnx/model.onnx` (circa 329 MB), `onnx/model_quantized.onnx` (circa 83 MB) e `tokenizer.json`. Revisione da fissare: `e6953e30167c7fc3d1ad62f3df31617dd712fbc1`. Il repository **non dichiara una licenza**: vale quella del modello originale (Apache-2.0), da annotare nell'ADR della Fase 7. | https://huggingface.co/Xenova/distilroberta-finetuned-financial-news-sentiment-analysis |
| M3 | 10–20 ms per titolo su CPU | non verificabile | Si misura nella Fase 7, con ONNX Runtime per .NET. | — |
| M3 | 10–20 ms per titolo su CPU | misurato | Il 15/11/2026, con ONNX Runtime 1.31.1 per .NET e il modello quantizzato su un solo thread: 23–24 ms per titolo sul PC di sviluppo (test `TheModelReadsTheToneOfFinancialHeadlines`). Con più thread scende, ma Anubis ne usa uno (ADR 0014, punto 8). | — |
| M4 | Claude Haiku 5.5: 0,10 $ in ingresso e 0,50 $ in uscita per milione di token (fino a 100.000 token); batch a metà prezzo; non ancora nella panoramica dei modelli | cambiato | Ora **è** nella panoramica, con identificativo `claude-haiku-5-5`. Prezzi confermati. Oltre i 100.000 token costa 0,50 $ e 2,50 $. | https://platform.claude.com/docs/en/about-claude/models/overview · https://platform.claude.com/docs/en/about-claude/pricing |
| M5 | Claude Haiku 4.5: 1 $ e 5 $ | confermato | Ora è un modello "legacy", ancora disponibile. | https://platform.claude.com/docs/en/about-claude/pricing |
@@ -265,7 +265,7 @@ Quando la fase indicata aggiunge il pacchetto, il nome entra nell'elenco del tes
| `SQLitePCLRaw.bundle_e_sqlite3`, `SQLitePCLRaw.core`, `SQLitePCLRaw.lib.e_sqlite3`, `SQLitePCLRaw.provider.e_sqlite3` 2.1.13 | Eric Sink, Apache-2.0 | `Microsoft.Data.Sqlite` | 1 (in uso) | Contengono la libreria nativa di SQLite e il suo caricatore. Sono nell'elenco del test dalla Fase 1. |
| `Newtonsoft.Json` 13.0.3 | James Newton-King, MIT | `Microsoft.ML`, LightGbm, FastTree | 8 | — |
| `LightGBM` 4.6.0 | progetto microsoft/LightGBM, pubblicato a nome di Guolin Ke, MIT | `Microsoft.ML.LightGbm` | 8 | Libreria nativa; in Docker serve `libgomp1`. |
| `Google.Protobuf` 3.30.2 | Google, BSD-3-Clause | `Microsoft.ML.Tokenizers` | 7 | — |
| `Google.Protobuf` 3.30.2 | Google, BSD-3-Clause | `Microsoft.ML.Tokenizers` | 7 (in uso) | Nell'elenco del test dalla Fase 7. |
| Mono.Cecil 0.11.6 | Jb Evain, MIT | estensione di copertura del metapacchetto MSTest | — | Evitato: non si usa il metapacchetto. |
## Immagini Docker
+4 -4
View File
@@ -1,6 +1,6 @@
# Stato del progetto
Aggiornato: 2026-11-14
Aggiornato: 2026-11-15
## Fase corrente
@@ -93,7 +93,8 @@ Aggiornato: 2026-11-14
- i feed si leggono senza DTD né entità;
- gli articoli doppi si tolgono per indirizzo e le riprese dello stesso fatto si raggruppano in una storia, contando le fonti indipendenti;
- ogni storia si collega a strumenti, classi o mercato: simboli, ticker, nomi delle società e delle monete, temi.
- Primo giudizio locale (#57, in parte): casi gravi trovati con parole chiave in inglese e in italiano, tono in cinque gradi, importanza da 1 a 4. Ogni articolo e ogni storia lo conservano. Il classificatore ONNX si aggancia dietro un'interfaccia: finché manca, il tono resta sconosciuto e il resto funziona.
- Primo giudizio locale (#57): casi gravi trovati con parole chiave in inglese e in italiano, tono in cinque gradi, importanza da 1 a 4. Ogni articolo e ogni storia lo conservano.
- Classificatore ONNX (#57, ADR 0014 punto 8): il modello Xenova quantizzato, con revisione e hash fissati, scaricato dal bot nella cartella `models` al primo giro delle notizie. Il tokenizzatore sta nella DLL e dà gli stessi identificativi dell'originale. 23–24 ms per titolo su un thread. Senza il modello giudicano le sole parole chiave.
## In corso
@@ -102,8 +103,7 @@ Nulla.
## Prossimo passo
1. L'utente scarica lo zip della release `v0.7.0` da Gitea (Release), lo fa girare e poi incolla il rapporto: doppio clic su `Prepara i dati da incollare.cmd`, oppure il pulsante in Diario e sistema. Il rapporto si analizza e si salva in `docs/diagnostica/`; da lì si chiude #55.
2. Intanto la Fase 7 va avanti sul branch `fase-7`. Raccolta (#56) e parole chiave (#57) sono fatte. Restano:
- il modello ONNX di #57: pacchetti `Microsoft.ML.OnnxRuntime` e `Microsoft.ML.Tokenizers`, modello Xenova con revisione e hash fissati;
2. Intanto la Fase 7 va avanti sul branch `fase-7`. Raccolta (#56) e primo giudizio con il classificatore (#57) sono fatti. Restano:
- l'analista facoltativo (#58);
- lo stato per strumento (#59);
- la schermata (#60).
+32 -2
View File
@@ -1,6 +1,6 @@
# 0014 — Scelte della Fase 7: raccolta e collegamento delle notizie
Data: 2026-11-14 · Stato: accettata · Precisa SPEC 4.4 e 10.2 (punti 1–3) per l'issue #56, senza aggiungere componenti esterni
Data: 2026-11-14 · Stato: accettata · Precisa SPEC 4.4, 4.5 e 10.2 (punti 1–4) per le issue #56 e #57. Usa solo i componenti già ammessi dall'ADR 0004
## Contesto
@@ -50,8 +50,38 @@ Inoltre:
- L'indirizzo canonico è unico.
- Un collegamento è unico per storia, ambito e bersaglio.
**7. Primo giudizio senza modello linguistico (#57).**
- Ogni articolo ha:
- un tono da −1 a +1, dato solo per i titoli in inglese;
- un eventuale caso grave;
- un'importanza da 1 a 4.
- Ogni storia tiene il tono medio, il caso grave e l'importanza più alta dei suoi articoli.
- I casi gravi si trovano con frasi intere in inglese e in italiano, nell'ordine di 10.2: attacco informatico, sospensione, frode, fallimento, indagine, revoca dalla quotazione. "hackathon" non è "hack".
- Le negazioni ("smentisce la frode") contano come il caso. È la scelta prudente: una restrizione in più, mai una in meno.
- L'importanza si assegna così: 4 ai casi gravi, 3 a un tono fino a −0,6, 2 agli articoli collegati a uno strumento, 1 al resto. Il 3 per un evento in calendario arriva con #59; il 5 lo dà solo il modello linguistico (#58).
**8. Classificatore locale (#57).**
- Il modello è `onnx/model_quantized.onnx` di `Xenova/distilroberta-finetuned-financial-news-sentiment-analysis`:
- revisione `e6953e30167c7fc3d1ad62f3df31617dd712fbc1`;
- 82.763.251 byte, SHA-256 `8fcf4873bf5e1679c77205ce621acecdd2cd8dd5baf18e930f10428008ed48ec`, lo stesso pubblicato da Hugging Face;
- è la versione quantizzata, quattro volte più piccola di `model.onnx`.
- Il repository Xenova non dichiara una licenza. Vale quella del modello originale, `mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis`: Apache-2.0.
- Il modello non sta nel repository né nello zip. Al primo giro delle notizie, il bot lo scarica in sottofondo nella cartella `models` dei dati, dall'indirizzo con la revisione fissata, e controlla dimensione e hash. Un file che non corrisponde non si usa mai.
- Se il download o l'apertura falliscono, si riprova dopo 6 ore. Intanto le notizie arrivano lo stesso e giudicano le sole parole chiave (SPEC 10.3: tutto funziona senza modello).
- Nella modalità dimostrativa e con i dati di mercato spenti non si scarica niente.
- Il tokenizzatore sta dentro la DLL (`src/Anubis/News/Model/`, hash nel `README.md` della cartella). È il BPE a livello di byte di RoBERTa, letto con `BpeTokenizer` di `Microsoft.ML.Tokenizers` e il pre-tokenizzatore di RoBERTa.
- `CodeGenTokenizer.Create` non va bene: aggiunge identificativi per le sequenze di spazi che RoBERTa non ha, e il modello li rifiuta.
- Nessun token speciale viene riconosciuto nel testo: un titolo che contiene `<s>` o `<mask>` resta testo.
- Il testo si tronca a 128 token: un titolo ci sta sempre.
- La prova che gli identificativi coincidono con l'originale: il test li confronta, su 14 titoli con accenti, simboli e spazi, con quelli di una copia indipendente del codificatore di GPT-2 sugli stessi file. "Hello world" dà il noto `0 31414 232 2`.
- Il modello gira su un solo thread, per non togliere il processore al motore di trading: 23–24 ms per titolo su questo PC.
- È un po' sopra i 10–20 ms di SPEC 4.5, che non dice su quanti thread.
- Con un giro ogni 2 minuti e qualche decina di titoli nuovi l'ora, il costo resta trascurabile.
- Il test sul modello vero gira dove il file è già scaricato (`ANUBIS_TEST_MODEL_DIR`). In CI risulta ignorato: i test non scaricano 83 MB a ogni esecuzione.
- Lo zip per Windows porta in più la libreria nativa di ONNX Runtime per win-x64 (`onnxruntime.dll`, 17 MB) e passa da 3,4 a 10,7 MB. Per questo `Anubis` e `Anubis.Server` dichiarano `win-x64` tra i loro runtime: il file di blocco contiene anche i pacchetti nativi di Windows, e `tasks release-win` non lo modifica più. Che l'immagine Docker carichi la libreria per linux-x64 (SPEC 3, distribuzione Docker) non è ancora provato: si controlla con il primo avvio del classificatore in Docker.
## Conseguenze
- Le notizie si accumulano già nel database. Classificazione, stato per strumento e schermata arrivano con le issue #57–#60.
- Le notizie si accumulano già nel database con il loro primo giudizio. Analista, stato per strumento e schermata arrivano con le issue #58–#60.
- Le soglie di somiglianza e di età sono costanti nel codice. Se l'uso reale mostra storie unite o divise male, si cambiano con un nuovo ADR e un test che lo mostra.
- I test leggono un archivio sintetico nel formato reale di ogni fonte (`tests/Anubis.Tests/Fixtures/news/`). Non chiamano mai internet.
@@ -0,0 +1,60 @@
# 0015 — PostgreSQL nei container con un processo init; nella CI, senza sincronizzazioni su disco
Data: 2026-11-15 · Stato: accettata · Precisa la CI (SPEC 15.5, ADR 0007) e il `docker-compose.yml` di sviluppo, issue #98, senza componenti esterni nuovi
## Contesto
Dal 14 novembre 2026 il lavoro `test (postgres)` della CI falliva spesso, ma non sempre. Dopo uno-cinque minuti di test, il PostgreSQL di servizio chiudeva tutte le connessioni. I test rimasti fallivano con `57P03: the database system is in recovery mode` o con `EndOfStreamException`. Rilanciando il lavoro passava. In locale la suite passava sempre.
Il registro del lavoro non mostrava quello del server. Per vederlo, la CI ora stampa a fine lavoro registro, stato e memoria del contenitore PostgreSQL (`tasks ci-service-log`). Li legge dal socket di Docker che l'esecutore monta nel lavoro, senza il client `docker` né altro da installare.
In una prova con la suite ripetuta tre volte sullo stesso server, il server si è riavviato quattro volte. Ogni volta il registro dice la stessa cosa:
```
LOG: untracked child process (PID 248) exited with exit code 2
LOG: terminating any other active server processes
LOG: all server processes terminated; reinitializing
```
Nessun processo ucciso per memoria (`OOMKilled false`, nessun evento `oom` di Docker) e nessun segnale. Il processo "sconosciuto" che esce con codice 2 è `pg_isready`. Il codice 2 vuol dire "il server non ha risposto".
Che cosa succede:
1. Docker esegue il controllo di salute dentro il contenitore come `/bin/sh -c "pg_isready …"`.
2. Sull'esecutore di Unraid il disco è lento. Ogni `DROP DATABASE` dei test forza un checkpoint immediato, con fino a 21 secondi di scrittura sul disco. In quei momenti il server può metterci più di 3 secondi a rispondere.
3. Passato il tempo massimo del controllo (`--health-timeout 3s`), Docker chiude la shell. `pg_isready` resta senza genitore e il sistema lo affida al processo 1 del contenitore. L'immagine `postgres` non ha un processo init, quindi il processo 1 è PostgreSQL stesso.
4. Quando `pg_isready` si arrende ed esce, PostgreSQL vede morire un figlio che non conosce. Per prudenza lo tratta come un suo processo andato in crash: chiude tutte le connessioni e rifà il recupero dal registro delle scritture.
Spiega anche gli altri indizi:
- capita solo a volte, perché serve un server lento proprio mentre scade il controllo;
- si vede da quando la suite è cresciuta con il commit della raccolta dei dati (issue #95), cioè più database creati e cancellati;
- in locale non capita, perché il disco è veloce.
Non c'entrano la memoria, `/dev/shm` (64 MiB, il server ne usa poco) e `pg_database_size` del campionatore. Il `CREATE`/`DROP DATABASE` dei test rende il server lento, ma da solo non lo fa ripartire.
**Seconda causa, vista dopo la prima correzione.** In un'esecuzione con la correzione il server non è più ripartito, ma 7 test sono falliti lo stesso:
- `Timeout during reading attempt` dopo circa 1 minuto e 18 secondi;
- `/primo-avvio?errore=database` nel test del primo avvio desktop, lo stesso errore visto il 14 novembre nell'esecuzione 304.
Il registro del server mostra il motivo:
- ogni `DROP DATABASE` forza un checkpoint immediato, e i checkpoint passano uno alla volta;
- sul disco dell'esecutore la sola sincronizzazione su disco ha richiesto fino a 37 secondi (`sync=36.849 s`);
- i `DROP DATABASE` in coda hanno superato i 30 secondi di attesa del client (`canceling statement due to user request`);
- il test desktop non ha avuto la connessione nei 5 secondi dell'applicazione.
Il server era vivo, ma fermo ad aspettare il disco.
## Decisione
1. **Un processo init davanti a PostgreSQL.** Il servizio `postgres` della CI parte con `--init`, e quello di `docker-compose.yml` con `init: true`. Il processo 1 diventa il piccolo init di Docker: raccoglie lui i processi rimasti senza genitore, e PostgreSQL non li vede più.
2. **`pg_isready` si arrende prima di Docker.** Il controllo è `pg_isready -U anubis -d anubis -t 5` con `--health-timeout 15s`. Così Docker non chiude mai la shell mentre `pg_isready` sta ancora aspettando. L'intervallo resta di 3 secondi, quindi l'avvio non rallenta.
3. **Nella CI il PostgreSQL dei test non sincronizza su disco.** Il servizio nasce con `POSTGRES_INITDB_ARGS: "--no-sync -c fsync=off -c synchronous_commit=off -c full_page_writes=off"` (l'opzione `-c` di `initdb` esiste da PostgreSQL 16). Così il checkpoint forzato da ogni `DROP DATABASE` scrive solo nella memoria del sistema e dura millisecondi. È ammesso solo perché quel server vive per un lavoro e poi si butta. Non vale per `docker-compose.yml` né per nessun database vero: lì un'interruzione di corrente senza `fsync` può rovinare i dati.
4. **Il registro del server resta nella CI.** Il passo `PostgreSQL log` gira sempre a fine lavoro `test (postgres)`. Mostra anche se l'init è attivo e com'è il controllo di salute. Il prossimo problema del server si legge lì, senza rilanciare alla cieca.
Le misure 1 e 2 si coprono a vicenda: ognuna da sola basta a togliere la prima causa. La 3 toglie la seconda, e rende anche più raro un controllo di salute lento.
## Conseguenze
- Il lavoro `test (postgres)` non perde più il server a metà. Rilanciarlo per farlo passare nascondeva la causa, e ora non serve più.
- Nessun test e nessuna parte di Anubis cambia: il difetto era nel modo in cui gira il contenitore, non nel bot.
- I test su PostgreSQL nella CI non provano la durata dei dati dopo un'interruzione. Non lo facevano neanche prima: quella è una proprietà di PostgreSQL, non di Anubis.
- **Per l'installazione su Unraid:** se il PostgreSQL che usa Anubis gira in un container con un controllo di salute basato su `pg_isready` e senza init, ha lo stesso rischio sotto carico. Il rimedio è lo stesso: l'opzione `--init` (in Unraid, "Extra Parameters") oppure un controllo con `-t` più breve del suo tempo massimo. Il template `deploy/unraid/anubis.xml` non installa PostgreSQL, quindi lì non cambia niente.
+38
View File
@@ -2,6 +2,11 @@
"version": 2,
"dependencies": {
"net10.0-windows7.0": {
"Google.Protobuf": {
"type": "Transitive",
"resolved": "3.30.2",
"contentHash": "Y2aOVLIt75yeeEWigg9V9YnjsEm53sADtLGq0gLhwaXpk3iu8tYSoauolyhenagA2sWno2TQ2WujI0HQd6s1Vw=="
},
"Microsoft.Data.Sqlite.Core": {
"type": "Transitive",
"resolved": "10.0.14",
@@ -23,6 +28,14 @@
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.0"
}
},
"Microsoft.ML.OnnxRuntime.Managed": {
"type": "Transitive",
"resolved": "1.31.1",
"contentHash": "158JCbNXN9/90LtjF4vHFiJYCF0bqF7CxLjm031jq7dJLEn/CtlHpMJm3UItQSIhQenlx+9Zuqi3zulwnsRVtA==",
"dependencies": {
"System.Numerics.Tensors": "9.0.0"
}
},
"SQLitePCLRaw.bundle_e_sqlite3": {
"type": "Transitive",
"resolved": "2.1.13",
@@ -50,10 +63,17 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"System.Numerics.Tensors": {
"type": "Transitive",
"resolved": "9.0.0",
"contentHash": "hyJB4UlpAi19Xr9AXzu2NuagKC4lPfHObNMEAA0HmqFz2rX7wKgzeYzO/jM/eBHDhnUGFFEjk5cOoJaxqg5J4A=="
},
"anubis": {
"type": "Project",
"dependencies": {
"Microsoft.Data.Sqlite": "[10.0.14, )",
"Microsoft.ML.OnnxRuntime": "[1.31.1, )",
"Microsoft.ML.Tokenizers": "[2.0.1, )",
"Npgsql": "[10.0.3, )",
"System.Security.Cryptography.ProtectedData": "[10.0.14, )"
}
@@ -69,6 +89,24 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"Microsoft.ML.OnnxRuntime": {
"type": "CentralTransitive",
"requested": "[1.31.1, )",
"resolved": "1.31.1",
"contentHash": "XtsO/1JoKepkWbGk9/DcYtRUTduwJD68kw0eeaH5yy77YBivUZWhC1NBNlVecsBt9EitlQIjCd1hHDNXJ2jbQA==",
"dependencies": {
"Microsoft.ML.OnnxRuntime.Managed": "1.31.1"
}
},
"Microsoft.ML.Tokenizers": {
"type": "CentralTransitive",
"requested": "[2.0.1, )",
"resolved": "2.0.1",
"contentHash": "RZN3zslVWinBCMDAnbZWEY8lQbMkP54Yxm1s5NigPcrvYFzXMrDjNcVM9afq4SZHf1BV1rEmO0p7ZdfBLDbu4g==",
"dependencies": {
"Google.Protobuf": "3.30.2"
}
},
"Npgsql": {
"type": "CentralTransitive",
"requested": "[10.0.3, )",
+35 -13
View File
@@ -2,6 +2,11 @@
"version": 2,
"dependencies": {
"net10.0": {
"Google.Protobuf": {
"type": "Transitive",
"resolved": "3.30.2",
"contentHash": "Y2aOVLIt75yeeEWigg9V9YnjsEm53sADtLGq0gLhwaXpk3iu8tYSoauolyhenagA2sWno2TQ2WujI0HQd6s1Vw=="
},
"Microsoft.Data.Sqlite.Core": {
"type": "Transitive",
"resolved": "10.0.14",
@@ -10,17 +15,12 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"Microsoft.Extensions.DependencyInjection.Abstractions": {
"Microsoft.ML.OnnxRuntime.Managed": {
"type": "Transitive",
"resolved": "10.0.0",
"contentHash": "L3AdmZ1WOK4XXT5YFPEwyt0ep6l8lGIPs7F5OOBZc77Zqeo01Of7XXICy47628sdVl0v/owxYJTe86DTgFwKCA=="
},
"Microsoft.Extensions.Logging.Abstractions": {
"type": "Transitive",
"resolved": "10.0.0",
"contentHash": "FU/IfjDfwaMuKr414SSQNTIti/69bHEMb+QKrskRb26oVqpx3lNFXMjs/RC9ZUuhBhcwDM2BwOgoMw+PZ+beqQ==",
"resolved": "1.31.1",
"contentHash": "158JCbNXN9/90LtjF4vHFiJYCF0bqF7CxLjm031jq7dJLEn/CtlHpMJm3UItQSIhQenlx+9Zuqi3zulwnsRVtA==",
"dependencies": {
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.0"
"System.Numerics.Tensors": "9.0.0"
}
},
"SQLitePCLRaw.bundle_e_sqlite3": {
@@ -50,10 +50,17 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"System.Numerics.Tensors": {
"type": "Transitive",
"resolved": "9.0.0",
"contentHash": "hyJB4UlpAi19Xr9AXzu2NuagKC4lPfHObNMEAA0HmqFz2rX7wKgzeYzO/jM/eBHDhnUGFFEjk5cOoJaxqg5J4A=="
},
"anubis": {
"type": "Project",
"dependencies": {
"Microsoft.Data.Sqlite": "[10.0.14, )",
"Microsoft.ML.OnnxRuntime": "[1.31.1, )",
"Microsoft.ML.Tokenizers": "[2.0.1, )",
"Npgsql": "[10.0.3, )",
"System.Security.Cryptography.ProtectedData": "[10.0.14, )"
}
@@ -69,14 +76,29 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"Microsoft.ML.OnnxRuntime": {
"type": "CentralTransitive",
"requested": "[1.31.1, )",
"resolved": "1.31.1",
"contentHash": "XtsO/1JoKepkWbGk9/DcYtRUTduwJD68kw0eeaH5yy77YBivUZWhC1NBNlVecsBt9EitlQIjCd1hHDNXJ2jbQA==",
"dependencies": {
"Microsoft.ML.OnnxRuntime.Managed": "1.31.1"
}
},
"Microsoft.ML.Tokenizers": {
"type": "CentralTransitive",
"requested": "[2.0.1, )",
"resolved": "2.0.1",
"contentHash": "RZN3zslVWinBCMDAnbZWEY8lQbMkP54Yxm1s5NigPcrvYFzXMrDjNcVM9afq4SZHf1BV1rEmO0p7ZdfBLDbu4g==",
"dependencies": {
"Google.Protobuf": "3.30.2"
}
},
"Npgsql": {
"type": "CentralTransitive",
"requested": "[10.0.3, )",
"resolved": "10.0.3",
"contentHash": "7nb5YzXuvWWJxB0J8DiyL3we+X4FOctZrt0fIBnucOIaIevFEEwGQVZKtiu9olXdlNAK1eNgqSral6r/jlhI4w==",
"dependencies": {
"Microsoft.Extensions.Logging.Abstractions": "10.0.0"
}
"contentHash": "7nb5YzXuvWWJxB0J8DiyL3we+X4FOctZrt0fIBnucOIaIevFEEwGQVZKtiu9olXdlNAK1eNgqSral6r/jlhI4w=="
},
"System.Security.Cryptography.ProtectedData": {
"type": "CentralTransitive",
+3
View File
@@ -4,6 +4,9 @@
<InvariantGlobalization>false</InvariantGlobalization>
<!-- Static rendering only: no blazor.web.js, so no Microsoft.AspNetCore.App.Internal.Assets package (ADR 0007). -->
<RequiresAspNetWebAssets>false</RequiresAspNetWebAssets>
<!-- The Windows trial zip is published for win-x64 (tasks release-win). Declared here, the lock file also holds the
native packages of that runtime (ONNX Runtime), so publishing never changes it. -->
<RuntimeIdentifiers>win-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Anubis\Anubis.csproj" />
+54
View File
@@ -2,6 +2,11 @@
"version": 2,
"dependencies": {
"net10.0": {
"Google.Protobuf": {
"type": "Transitive",
"resolved": "3.30.2",
"contentHash": "Y2aOVLIt75yeeEWigg9V9YnjsEm53sADtLGq0gLhwaXpk3iu8tYSoauolyhenagA2sWno2TQ2WujI0HQd6s1Vw=="
},
"Microsoft.Data.Sqlite.Core": {
"type": "Transitive",
"resolved": "10.0.14",
@@ -10,6 +15,14 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"Microsoft.ML.OnnxRuntime.Managed": {
"type": "Transitive",
"resolved": "1.31.1",
"contentHash": "158JCbNXN9/90LtjF4vHFiJYCF0bqF7CxLjm031jq7dJLEn/CtlHpMJm3UItQSIhQenlx+9Zuqi3zulwnsRVtA==",
"dependencies": {
"System.Numerics.Tensors": "9.0.0"
}
},
"SQLitePCLRaw.bundle_e_sqlite3": {
"type": "Transitive",
"resolved": "2.1.13",
@@ -37,10 +50,17 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"System.Numerics.Tensors": {
"type": "Transitive",
"resolved": "9.0.0",
"contentHash": "hyJB4UlpAi19Xr9AXzu2NuagKC4lPfHObNMEAA0HmqFz2rX7wKgzeYzO/jM/eBHDhnUGFFEjk5cOoJaxqg5J4A=="
},
"anubis": {
"type": "Project",
"dependencies": {
"Microsoft.Data.Sqlite": "[10.0.14, )",
"Microsoft.ML.OnnxRuntime": "[1.31.1, )",
"Microsoft.ML.Tokenizers": "[2.0.1, )",
"Npgsql": "[10.0.3, )",
"System.Security.Cryptography.ProtectedData": "[10.0.14, )"
}
@@ -56,6 +76,24 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"Microsoft.ML.OnnxRuntime": {
"type": "CentralTransitive",
"requested": "[1.31.1, )",
"resolved": "1.31.1",
"contentHash": "XtsO/1JoKepkWbGk9/DcYtRUTduwJD68kw0eeaH5yy77YBivUZWhC1NBNlVecsBt9EitlQIjCd1hHDNXJ2jbQA==",
"dependencies": {
"Microsoft.ML.OnnxRuntime.Managed": "1.31.1"
}
},
"Microsoft.ML.Tokenizers": {
"type": "CentralTransitive",
"requested": "[2.0.1, )",
"resolved": "2.0.1",
"contentHash": "RZN3zslVWinBCMDAnbZWEY8lQbMkP54Yxm1s5NigPcrvYFzXMrDjNcVM9afq4SZHf1BV1rEmO0p7ZdfBLDbu4g==",
"dependencies": {
"Google.Protobuf": "3.30.2"
}
},
"Npgsql": {
"type": "CentralTransitive",
"requested": "[10.0.3, )",
@@ -68,6 +106,22 @@
"resolved": "10.0.14",
"contentHash": "UuaBUHgmtc9h6CMxaQqFDDJxkS5JVy7XEB+DbtCvD68GFcVM9/AccuNwFpARx8nwCnvjaxzoOBjG3gPz32P1Eg=="
}
},
"net10.0/win-x64": {
"SQLitePCLRaw.lib.e_sqlite3": {
"type": "Transitive",
"resolved": "2.1.13",
"contentHash": "D2UAuiGxFYBqeRXjjjGgvJ5UQifzn6wefmSqeKU7XcNGKLbJRP6X7kcPZ6opOBJGdCIlgbSxTvDpBbLvsN594A=="
},
"Microsoft.ML.OnnxRuntime": {
"type": "CentralTransitive",
"requested": "[1.31.1, )",
"resolved": "1.31.1",
"contentHash": "XtsO/1JoKepkWbGk9/DcYtRUTduwJD68kw0eeaH5yy77YBivUZWhC1NBNlVecsBt9EitlQIjCd1hHDNXJ2jbQA==",
"dependencies": {
"Microsoft.ML.OnnxRuntime.Managed": "1.31.1"
}
}
}
}
}
+38
View File
@@ -2,6 +2,11 @@
"version": 2,
"dependencies": {
"net10.0-windows7.0": {
"Google.Protobuf": {
"type": "Transitive",
"resolved": "3.30.2",
"contentHash": "Y2aOVLIt75yeeEWigg9V9YnjsEm53sADtLGq0gLhwaXpk3iu8tYSoauolyhenagA2sWno2TQ2WujI0HQd6s1Vw=="
},
"Microsoft.Data.Sqlite.Core": {
"type": "Transitive",
"resolved": "10.0.14",
@@ -23,6 +28,14 @@
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.0"
}
},
"Microsoft.ML.OnnxRuntime.Managed": {
"type": "Transitive",
"resolved": "1.31.1",
"contentHash": "158JCbNXN9/90LtjF4vHFiJYCF0bqF7CxLjm031jq7dJLEn/CtlHpMJm3UItQSIhQenlx+9Zuqi3zulwnsRVtA==",
"dependencies": {
"System.Numerics.Tensors": "9.0.0"
}
},
"SQLitePCLRaw.bundle_e_sqlite3": {
"type": "Transitive",
"resolved": "2.1.13",
@@ -50,10 +63,17 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"System.Numerics.Tensors": {
"type": "Transitive",
"resolved": "9.0.0",
"contentHash": "hyJB4UlpAi19Xr9AXzu2NuagKC4lPfHObNMEAA0HmqFz2rX7wKgzeYzO/jM/eBHDhnUGFFEjk5cOoJaxqg5J4A=="
},
"anubis": {
"type": "Project",
"dependencies": {
"Microsoft.Data.Sqlite": "[10.0.14, )",
"Microsoft.ML.OnnxRuntime": "[1.31.1, )",
"Microsoft.ML.Tokenizers": "[2.0.1, )",
"Npgsql": "[10.0.3, )",
"System.Security.Cryptography.ProtectedData": "[10.0.14, )"
}
@@ -69,6 +89,24 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"Microsoft.ML.OnnxRuntime": {
"type": "CentralTransitive",
"requested": "[1.31.1, )",
"resolved": "1.31.1",
"contentHash": "XtsO/1JoKepkWbGk9/DcYtRUTduwJD68kw0eeaH5yy77YBivUZWhC1NBNlVecsBt9EitlQIjCd1hHDNXJ2jbQA==",
"dependencies": {
"Microsoft.ML.OnnxRuntime.Managed": "1.31.1"
}
},
"Microsoft.ML.Tokenizers": {
"type": "CentralTransitive",
"requested": "[2.0.1, )",
"resolved": "2.0.1",
"contentHash": "RZN3zslVWinBCMDAnbZWEY8lQbMkP54Yxm1s5NigPcrvYFzXMrDjNcVM9afq4SZHf1BV1rEmO0p7ZdfBLDbu4g==",
"dependencies": {
"Google.Protobuf": "3.30.2"
}
},
"Npgsql": {
"type": "CentralTransitive",
"requested": "[10.0.3, )",
+7
View File
@@ -1,14 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>Anubis</RootNamespace>
<!-- The Windows trial zip is published for win-x64 (tasks release-win). Declared here, the lock file also holds the
native packages of that runtime (ONNX Runtime), so publishing never changes it. -->
<RuntimeIdentifiers>win-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql" />
<PackageReference Include="Microsoft.Data.Sqlite" />
<PackageReference Include="System.Security.Cryptography.ProtectedData" />
<PackageReference Include="Microsoft.ML.OnnxRuntime" />
<PackageReference Include="Microsoft.ML.Tokenizers" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Db\Migrations\*.sql" LogicalName="Anubis.Db.Migrations.%(Filename)%(Extension)" />
<!-- The tokenizer of the news classifier (ADR 0014); the ONNX model is downloaded at the first start. -->
<EmbeddedResource Include="News\Model\*.json;News\Model\*.txt" LogicalName="Anubis.News.Model.%(Filename)%(Extension)" />
<EmbeddedResource Include="Demo\Data\*" LogicalName="Anubis.Demo.Data.%(Filename)%(Extension)" />
<!-- The backtest outcomes travel with the code (SPEC 12.3): read at the first start. -->
<EmbeddedResource Include="..\..\docs\backtests\*.outcome.json" LogicalName="Anubis.Backtests.%(Filename)%(Extension)" />
+2 -1
View File
@@ -82,7 +82,7 @@ public sealed partial class MarketDataService : IDisposable
_universes = new UniverseStore(db, clock);
_builder = new UniverseBuilder(db, clock);
_states = new SourceStateStore(db, clock);
News = new News.NewsCollector(_http, db, clock, _logger);
News = new News.NewsCollector(_http, db, clock, _logger) { ModelFolder = data is null ? null : Path.Combine(data.Root, "models") };
}
public OkxClient Okx { get; }
@@ -153,6 +153,7 @@ public sealed partial class MarketDataService : IDisposable
public void Dispose()
{
_work.Dispose();
News.Dispose();
_http.Dispose();
}
+15
View File
@@ -0,0 +1,15 @@
# Tokenizer of the local news classifier
`vocab.json` and `merges.txt` are the byte-level BPE tokenizer of RoBERTa. They are used by the model
`mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis` (Apache-2.0), as exported to ONNX in
`Xenova/distilroberta-finetuned-financial-news-sentiment-analysis`.
- Revision: `e6953e30167c7fc3d1ad62f3df31617dd712fbc1`.
- Downloaded on 2026-11-15 with the user's permission (SPEC 4.5, ADR 0004, ADR 0014).
- SHA-256:
- `vocab.json`: `ed19656ea1707df69134c4af35c8ceda2cc9860bf2c3495026153a133670ab5e`
- `merges.txt`: `1ce1664773c50f3e0cc8842619a93edc4624525b728b188a9e0be33b7726adc5`
They travel inside the DLL. The ONNX model itself, `onnx/model_quantized.onnx` (82,763,251 bytes, SHA-256
`8fcf4873bf5e1679c77205ce621acecdd2cd8dd5baf18e930f10428008ed48ec`), is too large for the repository. The
application downloads it once into the `models` folder of the data directory and checks its hash (`ToneModel`).
File diff suppressed because it is too large. Load diff
+1
View File
@@ -0,0 +1 @@
{"<s>":0,"<pad>":1,"</s>":2,"<unk>":3,".":4,"Ġthe":5,",":6,"Ġto":7,"Ġand":8,"Ġof":9,"Ġa":10,"Ġin":11,"-":12,"Ġfor":13,"Ġthat":14,"Ġon":15,"Ġis":16,"âĢ":17,"'s":18,"Ġwith":19,"ĠThe":20,"Ġwas":21,"Ġ\"":22,"Ġat":23,"Ġit":24,"Ġas":25,"Ġsaid":26,"Ļ":27,"Ġbe":28,"s":29,"Ġby":30,"Ġfrom":31,"Ġare":32,"Ġhave":33,"Ġhas":34,":":35,"Ġ(":36,"Ġhe":37,"ĠI":38,"Ġhis":39,"Ġwill":40,"Ġan":41,"Ġthis":42,")":43,"ĠâĢ":44,"Ġnot":45,"Ŀ":46,"Ġyou":47,"ľ":48,"Ġtheir":49,"Ġor":50,"Ġthey":51,"Ġwe":52,"Ġbut":53,"Ġwho":54,"Ġmore":55,"Ġhad":56,"Ġbeen":57,"Ġwere":58,"Ġabout":59,",\"":60,"Ġwhich":61,"Ġup":62,"Ġits":63,"Ġcan":64,"Ġone":65,"Ġout":66,"Ġalso":67,"Ġ$":68,"Ġher":69,"Ġall":70,"Ġafter":71,".\"":72,"/":73,"Ġwould":74,"'t":75,"Ġyear":76,"Ġwhen":77,"Ġfirst":78,"Ġshe":79,"Ġtwo":80,"Ġover":81,"Ġpeople":82,"ĠA":83,"Ġour":84,"ĠIt":85,"Ġtime":86,"Ġthan":87,"Ġinto":88,"Ġthere":89,"t":90,"ĠHe":91,"Ġnew":92,"ĠâĢĶ":93,"Ġlast":94,"Ġjust":95,"ĠIn":96,"Ġother":97,"Ġso":98,"Ġwhat":99,"I":100,"Ġlike":101,"a":102,"Ġsome":103,"S":104,"ë":105,"Ġthem":106,"Ġyears":107,"'":108,"Ġdo":109,"Ġyour":110,"Ġ-":111,"Ġ1":112,"\"":113,"Ġif":114,"Ġcould":115,"?":116,"Ġno":117,"i":118,"m":119,"Ġget":120,"ĠU":121,"Ġnow":122,"Ġhim":123,"Ġback":124,"ĠBut":125,"ĠâĢĵ":126,"Ġmy":127,"Ġ'":128,"Ġonly":129,"Ġthree":130,";":131,"Ġ2":132,"The":133,"1":134,"Ġpercent":135,"Ġagainst":136,"Ġbefore":137,"Ġcompany":138,"o":139,"ĠTrump":140,"Ġhow":141,"Ġbecause":142,"Ġany":143,"Ġmost":144,"Ġbeing":145,"Ġmake":146,"Ġwhere":147,"Ġduring":148,"Ġthrough":149,"Ġwhile":150,"000":151,"ĠThis":152,"Ġmillion":153,"ing":154,"Ġ3":155,"Ġmade":156,"Ġwell":157,"Ġ10":158,"Ġdown":159,"Ġoff":160,"Ġsays":161,"Ġme":162,"ĠB":163,"Ġgoing":164,"Ġteam":165,"ĠWe":166,"Ġthose":167,"Ġgovernment":168,"Ġway":169,"We":170,"Ġmany":171,"Ġthen":172,"Ġwork":173,"Ġtold":174,"com":175,"2":176,"Ġgame":177,"ĠAnd":178,"in":179,"year":180,"Ġp":181,"Ġvery":182,"Ġday":183,"Ġhome":184,"Ġtake":185,"Ġweek":186,"Ġsince":187,"ĠNew":188,"Ġmay":189,"Ġeven":190,"Ġseason":191,"Ġsee":192,"Ġ2017":193,"Ġstate":194,"Ġ5":195,"ed":196,"Ġshould":197,"Ġaround":198,"Ġ2018":199,"Ġsecond":200,"Ġus":201,"Ġstill":202,"Ġmuch":203,"Ġ4":204,"Ġgood":205,"Ġthink":206,"%":207,"ĠS":208,"Ġthese":209,"Ġmarket":210,"ĠD":211,"th":212,"Ġgo":213,"'re":214,"Ġsuch":215,"Ġknow":216,"Ġincluding":217,"Ġdon":218,"y":219,"Ġnext":220,"ĠP":221,"Ġdid":222,"Ġunder":223,"Ġsay":224,"en":225,"ĠL":226,"Ġbetween":227,"Ġper":228,"ĠK":229,"ĠC":230,"Ġ6":231,"Ġworld":232,"Ġpart":233,"ĠN":234,"Ġright":235,"Ġwant":236,"Ġfour":237,"),":238,"Ġhigh":239,"Ġneed":240,"re":241,"e":242,"It":243,"Ġhelp":244,"5":245,"3":246,"Ġcountry":247,"ĠR":248,"Ġpolice":249,"A":250,"Ġlong":251,"ĠThey":252,"Ġend":253,"er":254,"ĠT":255,"ĠM":256,"u":257,"Ġboth":258,"Ġhere":259,"an":260,"on":261,"Ġ7":262,"Ġde":263,"ĠShe":264,"Ġbusiness":265,"Ġreport":266,"j":267,"ers":268,"Ġreally":269,"ĠPresident":270,"ar":271,"ĠG":272,"ĠFriday":273,"ĠF":274,"Ġbest":275,"Ġsame":276,"Ġanother":277,"Ġset":278,"old":279,"ĠThat":280,"as":281,"n":282,"Ġcome":283,"Ġfamily":284,"Ġpublic":285,"ĠFor":286,"ĠAs":287,"0":288,"ĠH":289,"Ġ8":290,"Ġ20":291,"Ġfive":292,"es":293,"ĠTuesday":294,"Ġn":295,"ĠThursday":296,"Ġquarter":297,"h":298,"Ġtop":299,"Ġgot":300,"Ġlife":301,"ĠMonday":302,"Ġfound":303,"Ġuse":304,"ĠW":305,"4":306,"ĠWednesday":307,"Ġown":308,"Ġaccording":309,"Ġplay":310,"Ġshow":311,"ĠSt":312,"Ġman":313,"Ġleft":314,"ĠUnited":315,"Ġ12":316,"Ġplace":317,"ĠIf":318,"Ġlot":319,"Ġformer":320,"Ġ0":321,").":322,"Ġsupport":323,"ie":324,"Ġbillion":325,"Ġt":326,"Ġshares":327,"!":328,"z":329,"k":330,"ĠState":331,"Ġpoints":332,"Ġgroup":333,"Ġschool":334,"Ġinformation":335,"Ġ2016":336,"al":337,"r":338,"Ġwin":339,"Ġnews":340,"Ġused":341,"Ġput":342,"Ġcity":343,"ĠJ":344,"ĠThere":345,"Ġnumber":346,"C":347,"'ve":348,"Ġeach":349,"Ġtoo":350,"Ġwon":351,"ly":352,"Ġmonth":353,"is":354,"Ġadded":355,"Ġlook":356,"Ġbetter":357,"Ġevery":358,"Ġ&":359,"Ġdays":360,"Ġ9":361,"Ġtook":362,"Ġnight":363,"Ġe":364,"Ġ11":365,"os":366,"Ġfew":367,"or":368,"ĠNorth":369,"ĠYou":370,"Ġthird":371,"Ġgreat":372,"Ġcalled":373,"ĠOn":374,"Ġpast":375,"Ġcame":376,"Ġmonths":377,"ĠSaturday":378,"Ġ15":379,"Ġbig":380,"ĠE":381,"ĠUS":382,"Ġthings":383,"ĠO":384,"Ġd":385,"Ġstart":386,"B":387,"Ġstock":388,"Ġ30":389,"Ġwomen":390,"ĠSouth":391,"ĠMay":392,"Ġnever":393,"Ġpresident":394,"ĠSunday":395,"Ġwithout":396,"man":397,"8":398,"Ġdidn":399,"Ġlocal":400,"6":401,"Ġsomething":402,"Ġcase":403,"ĠAll":404,"it":405,"7":406,"ĠSo":407,"Ġchildren":408,"Ġaway":409,"Ġlittle":410,"Ġsix":411,"ĠCity":412,"ĠCounty":413,"Ġdata":414,"at":415,"Ġalready":416,"d":417,"Ġmoney":418,"Ġearly":419,"Ġacross":420,"Ġexpected":421,"Ġrun":422,"Ġlater":423,"am":424,"ĠpricLine truncated
+65 -3
View File
@@ -5,6 +5,7 @@ using Anubis.Data;
using Anubis.Db;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.ML.OnnxRuntime;
namespace Anubis.News;
@@ -13,7 +14,7 @@ namespace Anubis.News;
/// conditional on what the source said the last time (ETag, Last-Modified), and spaced out more and more for a source
/// that keeps failing. The text it reads is untrusted data: it is parsed, never followed.
/// </summary>
public sealed partial class NewsCollector(HttpClient http, Database db, IClock clock, ILogger? logger = null)
public sealed partial class NewsCollector(HttpClient http, Database db, IClock clock, ILogger? logger = null) : IDisposable
{
public const string JobName = "notizie";
@@ -35,14 +36,69 @@ public sealed partial class NewsCollector(HttpClient http, Database db, IClock c
public NewsStore Store => _store;
/// <summary>The local classifier of SPEC 4.5; without it the tone is unknown and only the keywords judge.</summary>
public IToneClassifier? Classifier { get; init; }
/// <summary>A model that could not be downloaded or opened is tried again after this long.</summary>
public static readonly TimeSpan ModelRetry = TimeSpan.FromHours(6);
private readonly CancellationTokenSource _stopping = new();
private IToneClassifier? _classifier;
private Task? _loading;
private DateTimeOffset _nextModelTry = DateTimeOffset.MinValue;
/// <summary>
/// The local classifier of SPEC 4.5. Without it the tone is unknown and only the keywords judge: the news keep
/// coming while the model downloads, and the articles read meanwhile simply have no score.
/// </summary>
public IToneClassifier? Classifier
{
get => Volatile.Read(ref _classifier);
init => _classifier = value;
}
/// <summary>Where the model is kept (the <c>models</c> folder of the data directory); null: no model, as in the tests.</summary>
public string? ModelFolder { get; init; }
public void Dispose()
{
_stopping.Cancel();
_stopping.Dispose();
(Volatile.Read(ref _classifier) as IDisposable)?.Dispose();
}
/// <summary>Starts, in the background, the download and the opening of the model, once and then every 6 hours if it fails.</summary>
private void StartModel()
{
if (Classifier is not null || ModelFolder is not { } folder || _loading is { IsCompleted: false } || clock.UtcNow < _nextModelTry)
{
return;
}
_nextModelTry = clock.UtcNow + ModelRetry;
var stopping = _stopping.Token;
_loading = Task.Run(async () =>
{
try
{
var path = await ToneModel.EnsureAsync(http, folder, stopping).ConfigureAwait(false);
Volatile.Write(ref _classifier, new OnnxToneClassifier(path));
LogModelReady(_logger, ToneModel.Revision[..8]);
}
catch (OperationCanceledException) when (stopping.IsCancellationRequested)
{
}
catch (Exception e) when (e is HttpRequestException or IOException or InvalidDataException or OperationCanceledException or OnnxRuntimeException
or UnauthorizedAccessException)
{
LogModelFailed(_logger, e.Message);
}
}, CancellationToken.None);
}
public ScheduledJob Job() => new(JobName, "Lettura delle notizie", Schedule.Every(Interval), RunOnceAsync) { MaxLateness = Interval };
/// <summary>One round over the sources that are due.</summary>
public async Task RunOnceAsync(CancellationToken cancellationToken)
{
StartModel();
await _store.EnsureDefaultsAsync(cancellationToken).ConfigureAwait(false);
var dictionary = await DictionaryAsync(cancellationToken).ConfigureAwait(false);
var first = true;
@@ -167,4 +223,10 @@ public sealed partial class NewsCollector(HttpClient http, Database db, IClock c
[LoggerMessage(Level = LogLevel.Warning, Message = "News source {Source} failed: {Error}")]
private static partial void LogFailed(ILogger logger, string source, string error);
[LoggerMessage(Level = LogLevel.Information, Message = "News classifier ready (model revision {Revision})")]
private static partial void LogModelReady(ILogger logger, string revision);
[LoggerMessage(Level = LogLevel.Warning, Message = "News classifier not available, keywords only until the next try: {Error}")]
private static partial void LogModelFailed(ILogger logger, string error);
}
+199
View File
@@ -0,0 +1,199 @@
using System.Security.Cryptography;
using System.Text.Json;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.Tokenizers;
namespace Anubis.News;
/// <summary>
/// The model of SPEC 4.5: <c>mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis</c> (Apache-2.0) in its
/// ONNX export by Xenova, at a fixed revision and with its hash checked (ADR 0004, ADR 0014). The tokenizer travels in
/// the DLL; the model, 83 MB, is downloaded once into the <c>models</c> folder of the data directory.
/// </summary>
public static class ToneModel
{
public const string Repository = "Xenova/distilroberta-finetuned-financial-news-sentiment-analysis";
public const string Revision = "e6953e30167c7fc3d1ad62f3df31617dd712fbc1";
public const string RemotePath = "onnx/model_quantized.onnx";
public const string FileName = "distilroberta-financial-news-sentiment-e6953e30-quantized.onnx";
public const long Size = 82_763_251;
public const string Sha256 = "8fcf4873bf5e1679c77205ce621acecdd2cd8dd5baf18e930f10428008ed48ec";
public static readonly Uri Url = new($"https://huggingface.co/{Repository}/resolve/{Revision}/{RemotePath}");
/// <summary>The longest download allowed: slow connections get there, a stuck one does not hold the job forever.</summary>
public static readonly TimeSpan DownloadTimeout = TimeSpan.FromMinutes(30);
/// <summary>
/// The path of the model in <paramref name="folder"/>, downloaded and checked if it is not there yet. A file of the
/// wrong size or hash is never used: it is replaced. Throws when the download fails or does not match.
/// </summary>
public static async Task<string> EnsureAsync(HttpClient http, string folder, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(http);
Directory.CreateDirectory(folder);
var path = Path.Combine(folder, FileName);
if (File.Exists(path) && new FileInfo(path).Length == Size && await HashAsync(path, cancellationToken).ConfigureAwait(false) == Sha256)
{
return path;
}
var part = path + ".part";
using (var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
{
timeout.CancelAfter(DownloadTimeout);
using var response = await http.GetAsync(Url, HttpCompletionOption.ResponseHeadersRead, timeout.Token).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var target = new FileStream(part, FileMode.Create, FileAccess.Write, FileShare.None, 1 << 16, useAsync: true);
await using (target.ConfigureAwait(false))
{
var source = await response.Content.ReadAsStreamAsync(timeout.Token).ConfigureAwait(false);
await using (source.ConfigureAwait(false))
{
var buffer = new byte[1 << 16];
long total = 0;
int read;
while ((read = await source.ReadAsync(buffer, timeout.Token).ConfigureAwait(false)) > 0)
{
total += read;
if (total > Size)
{
throw new InvalidDataException($"The model is larger than the {Size} bytes expected.");
}
await target.WriteAsync(buffer.AsMemory(0, read), timeout.Token).ConfigureAwait(false);
}
}
}
}
var hash = await HashAsync(part, cancellationToken).ConfigureAwait(false);
if (new FileInfo(part).Length != Size || hash != Sha256)
{
File.Delete(part);
throw new InvalidDataException($"The downloaded model does not match: SHA-256 {hash}, expected {Sha256}.");
}
File.Move(part, path, overwrite: true);
return path;
}
private static async Task<string> HashAsync(string path, CancellationToken cancellationToken)
{
var file = File.OpenRead(path);
await using (file.ConfigureAwait(false))
{
return Convert.ToHexStringLower(await SHA256.HashDataAsync(file, cancellationToken).ConfigureAwait(false));
}
}
}
/// <summary>
/// The local classifier of SPEC 4.5 on ONNX Runtime. The tokenizer is RoBERTa's byte-level BPE, the one of GPT-2 with
/// <c>&lt;s&gt;</c> and <c>&lt;/s&gt;</c> around the text; the test compares its ids with the original. One thread, so the
/// classifier never takes the processor away from the trading engine; 10–20 ms per title.
/// </summary>
public sealed class OnnxToneClassifier : IToneClassifier, IDisposable
{
/// <summary>Titles are short: 128 tokens hold any of them, and the model's limit is 512.</summary>
public const int MaxTokens = 128;
private const int Start = 0;
private const int End = 2;
// Classes of the model (config.json): 0 negative, 1 neutral, 2 positive.
private const int NegativeClass = 0;
private const int NeutralClass = 1;
private const int PositiveClass = 2;
private static readonly Lazy<BpeTokenizer> Tokenizer = new(LoadTokenizer);
private readonly InferenceSession _session;
private readonly string[] _inputs;
private readonly string[] _output;
public OnnxToneClassifier(string modelPath)
{
using var options = new SessionOptions
{
IntraOpNumThreads = 1,
InterOpNumThreads = 1,
GraphOptimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL,
};
_session = new InferenceSession(modelPath, options);
_inputs = [.. _session.InputMetadata.Keys];
_output = [_session.OutputMetadata.Keys.First()];
if (!_inputs.Contains("input_ids"))
{
_session.Dispose();
throw new InvalidDataException("The model has no input_ids input.");
}
}
/// <summary>The token ids the model reads, as the original RoBERTa tokenizer gives them.</summary>
public static IReadOnlyList<int> Ids(string text)
{
ArgumentNullException.ThrowIfNull(text);
var body = Tokenizer.Value.EncodeToIds(text);
var ids = new List<int>(Math.Min(body.Count, MaxTokens - 2) + 2) { Start };
ids.AddRange(body.Take(MaxTokens - 2));
ids.Add(End);
return ids;
}
public ToneScore Classify(string text)
{
var ids = Ids(text);
var shape = new long[] { 1, ids.Count };
var values = new List<OrtValue>(_inputs.Length);
try
{
foreach (var name in _inputs)
{
var data = name switch
{
"input_ids" => ids.Select(i => (long)i).ToArray(),
"attention_mask" => Enumerable.Repeat(1L, ids.Count).ToArray(),
_ => new long[ids.Count],
};
values.Add(OrtValue.CreateTensorValueFromMemory(data, shape));
}
using var run = new RunOptions();
using var outputs = _session.Run(run, _inputs, values, _output);
var logits = outputs[0].GetTensorDataAsSpan<float>();
var max = Math.Max(logits[NegativeClass], Math.Max(logits[NeutralClass], logits[PositiveClass]));
double negative = Math.Exp(logits[NegativeClass] - max), neutral = Math.Exp(logits[NeutralClass] - max), positive = Math.Exp(logits[PositiveClass] - max);
var sum = negative + neutral + positive;
return new ToneScore(positive / sum, negative / sum, neutral / sum);
}
finally
{
foreach (var value in values)
{
value.Dispose();
}
}
}
public void Dispose() => _session.Dispose();
private static BpeTokenizer LoadTokenizer()
{
var assembly = typeof(OnnxToneClassifier).Assembly;
using var vocab = assembly.GetManifestResourceStream("Anubis.News.Model.vocab.json") ?? throw new InvalidOperationException("vocab.json is missing.");
using var merges = assembly.GetManifestResourceStream("Anubis.News.Model.merges.txt") ?? throw new InvalidOperationException("merges.txt is missing.");
var vocabulary = JsonSerializer.Deserialize<Dictionary<string, int>>(vocab) ?? throw new InvalidDataException("vocab.json is empty.");
using var reader = new StreamReader(merges);
var pairs = reader.ReadToEnd().Split('\n').Where(l => l.Length > 0 && !l.StartsWith("#version", StringComparison.Ordinal)).ToList();
// Not CodeGenTokenizer.Create: it adds CodeGen's own tokens for runs of spaces, ids RoBERTa does not have. No special
// tokens either: a headline that contains "<s>" or "<mask>" is read as text, never as a mark for the model.
return BpeTokenizer.Create(new BpeOptions(vocabulary)
{
Merges = pairs,
PreTokenizer = RobertaPreTokenizer.Instance,
ByteLevel = true,
});
}
}
+52
View File
@@ -13,6 +13,24 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"Microsoft.ML.OnnxRuntime": {
"type": "Direct",
"requested": "[1.31.1, )",
"resolved": "1.31.1",
"contentHash": "XtsO/1JoKepkWbGk9/DcYtRUTduwJD68kw0eeaH5yy77YBivUZWhC1NBNlVecsBt9EitlQIjCd1hHDNXJ2jbQA==",
"dependencies": {
"Microsoft.ML.OnnxRuntime.Managed": "1.31.1"
}
},
"Microsoft.ML.Tokenizers": {
"type": "Direct",
"requested": "[2.0.1, )",
"resolved": "2.0.1",
"contentHash": "RZN3zslVWinBCMDAnbZWEY8lQbMkP54Yxm1s5NigPcrvYFzXMrDjNcVM9afq4SZHf1BV1rEmO0p7ZdfBLDbu4g==",
"dependencies": {
"Google.Protobuf": "3.30.2"
}
},
"Npgsql": {
"type": "Direct",
"requested": "[10.0.3, )",
@@ -28,6 +46,11 @@
"resolved": "10.0.14",
"contentHash": "UuaBUHgmtc9h6CMxaQqFDDJxkS5JVy7XEB+DbtCvD68GFcVM9/AccuNwFpARx8nwCnvjaxzoOBjG3gPz32P1Eg=="
},
"Google.Protobuf": {
"type": "Transitive",
"resolved": "3.30.2",
"contentHash": "Y2aOVLIt75yeeEWigg9V9YnjsEm53sADtLGq0gLhwaXpk3iu8tYSoauolyhenagA2sWno2TQ2WujI0HQd6s1Vw=="
},
"Microsoft.Data.Sqlite.Core": {
"type": "Transitive",
"resolved": "10.0.14",
@@ -49,6 +72,14 @@
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.0"
}
},
"Microsoft.ML.OnnxRuntime.Managed": {
"type": "Transitive",
"resolved": "1.31.1",
"contentHash": "158JCbNXN9/90LtjF4vHFiJYCF0bqF7CxLjm031jq7dJLEn/CtlHpMJm3UItQSIhQenlx+9Zuqi3zulwnsRVtA==",
"dependencies": {
"System.Numerics.Tensors": "9.0.0"
}
},
"SQLitePCLRaw.bundle_e_sqlite3": {
"type": "Transitive",
"resolved": "2.1.13",
@@ -75,6 +106,27 @@
"dependencies": {
"SQLitePCLRaw.core": "2.1.13"
}
},
"System.Numerics.Tensors": {
"type": "Transitive",
"resolved": "9.0.0",
"contentHash": "hyJB4UlpAi19Xr9AXzu2NuagKC4lPfHObNMEAA0HmqFz2rX7wKgzeYzO/jM/eBHDhnUGFFEjk5cOoJaxqg5J4A=="
}
},
"net10.0/win-x64": {
"Microsoft.ML.OnnxRuntime": {
"type": "Direct",
"requested": "[1.31.1, )",
"resolved": "1.31.1",
"contentHash": "XtsO/1JoKepkWbGk9/DcYtRUTduwJD68kw0eeaH5yy77YBivUZWhC1NBNlVecsBt9EitlQIjCd1hHDNXJ2jbQA==",
"dependencies": {
"Microsoft.ML.OnnxRuntime.Managed": "1.31.1"
}
},
"SQLitePCLRaw.lib.e_sqlite3": {
"type": "Transitive",
"resolved": "2.1.13",
"contentHash": "D2UAuiGxFYBqeRXjjjGgvJ5UQifzn6wefmSqeKU7XcNGKLbJRP6X7kcPZ6opOBJGdCIlgbSxTvDpBbLvsN594A=="
}
}
}
@@ -35,6 +35,9 @@ public sealed class DependencyPolicyTests
"SQLitePCLRaw.core",
"SQLitePCLRaw.lib.e_sqlite3",
"SQLitePCLRaw.provider.e_sqlite3",
// Protocol buffers, brought by Microsoft.ML.Tokenizers (phase 7).
"Google.Protobuf",
};
private static readonly string[] _packageElements = ["PackageReference", "GlobalPackageReference", "PackageDownload"];
+3 -1
View File
@@ -40,8 +40,10 @@ public sealed class MarketDataServerTests
StringAssert.Contains(bySource[MarketDataService.AlpacaSource].GetProperty("note").GetString(), "ALPACA_API_KEY_DEMO");
StringAssert.Contains(bySource[MarketDataService.OkxSource].GetProperty("note").GetString(), "3 nuovi");
Assert.AreEqual(JsonValueKind.Null, bySource[MarketDataService.OkxSource].GetProperty("lastError").ValueKind);
// The news of Phase 7 run on the same network: their feeds and the pinned classifier model, nothing else.
var news = Anubis.News.NewsStore.Defaults.Select(s => new Uri(s.Url).Host).Append(Anubis.News.ToneModel.Url.Host).ToHashSet();
Assert.IsTrue(network.Urls.All(u => u.Host is "eea.okx.com" or "data.binance.vision" or "nfs.faireconomy.media" or "www.ecb.europa.eu"
or "api.alternative.me" or "cdn.cboe.com"), string.Join(", ", network.Urls.Select(u => u.Host).Distinct()));
or "api.alternative.me" or "cdn.cboe.com" || news.Contains(u.Host)), string.Join(", ", network.Urls.Select(u => u.Host).Distinct()));
Assert.IsFalse(network.Urls.Any(u => u.Host.Contains("alpaca", StringComparison.Ordinal)), "no call to Alpaca without keys");
var report = await server.TextAsync("/api/data/quality?format=md");
+117
View File
@@ -0,0 +1,117 @@
using System.Diagnostics;
using System.Net;
using Anubis.News;
namespace Anubis.Tests;
/// <summary>The local classifier of SPEC 4.5 (issue #57): the same tokens as the original, the model checked by its hash.</summary>
[TestClass]
public sealed class ToneModelTests
{
/// <summary>
/// Ids of the original RoBERTa tokenizer on the same vocab.json and merges.txt, computed with an independent copy of
/// the GPT-2 byte-level encoder (openai/gpt-2, encoder.py), reading the text as UTF-8. "Hello world" is the well-known
/// 0 31414 232 2.
/// </summary>
private static readonly (string Text, int[] Ids)[] Original =
[
("Hello world", [0, 31414, 232, 2]),
("Apple shares jump after record iPhone sales in China", [0, 20770, 327, 3704, 71, 638, 2733, 647, 11, 436, 2]),
("Bitcoin drifts below $90,000 as traders await U.S. data", [0, 42817, 12402, 22833, 874, 68, 3248, 6, 151, 25, 5539, 17396, 121, 4, 104, 4, 414, 2]),
("Fire at a chip plant hits Nvidia supply, $NVDA slides", [0, 17260, 23, 10, 6638, 2195, 2323, 18520, 1787, 6, 68, 31668, 3134, 19583, 2]),
("Fed officials signal patience on interest rates into December", [0, 38186, 503, 6029, 11383, 15, 773, 1162, 88, 719, 2]),
("Exchange halts withdrawals after hack, Solana tokens drained", [0, 9089, 14035, 12193, 1872, 27733, 71, 14157, 6, 4856, 1113, 22121, 23544, 2]),
("Tesla recalls vehicles over a software issue (NASDAQ:TSLA)", [0, 39971, 10270, 1734, 81, 10, 2257, 696, 36, 3399, 3223, 35, 2685, 8272, 43, 2]),
("Microsoft & OpenAI sign a new cloud deal", [0, 35460, 359, 2117, 15238, 1203, 10, 92, 3613, 432, 2]),
("Target cuts its holiday outlook as shoppers pull back", [0, 41858, 2599, 63, 2317, 3839, 25, 9456, 2999, 124, 2]),
("It's the company's 3rd quarter: EPS beat by 12%, revenue up 7.5%", [0, 243, 18, 5, 138, 18, 155, 2586, 297, 35, 4721, 1451, 30, 316, 4234, 903, 62, 262, 4, 245, 207, 2]),
(" Spaces and\ttabs ", [0, 1437, 39926, 1437, 1437, 8, 50117, 34108, 29, 1437, 1437, 2]),
("L'inflazione è vicina all'obiettivo", [0, 574, 108, 179, 4825, 1222, 8525, 952, 11423, 32438, 1243, 70, 108, 2413, 5810, 90, 9697, 2]),
("Société Générale cède 3,5% à Paris", [0, 104, 17641, 10221, 1140, 272, 10598, 20765, 1627, 740, 8025, 2794, 155, 6, 245, 207, 6534, 2201, 2]),
("Nasdaq ➜ record — €1.2bn deal", [0, 487, 281, 5946, 14333, 17772, 48, 638, 93, 4480, 134, 4, 176, 6316, 432, 2]),
];
[TestMethod]
public void TheTokenizerGivesTheIdsOfTheOriginal()
{
foreach (var (text, ids) in Original)
{
CollectionAssert.AreEqual(ids, OnnxToneClassifier.Ids(text).ToArray(), text);
}
var marks = OnnxToneClassifier.Ids("<s> buy </s> <mask>");
Assert.AreEqual(1, marks.Count(i => i == 0), "Marks written in a headline are text, not marks.");
Assert.AreEqual(1, marks.Count(i => i == 2));
Assert.IsTrue(OnnxToneClassifier.Ids("\u0000 \u2603 \U0001F680 \u4E2D\u6587 \n\r").All(i => i is >= 0 and < 50265), "Any text gives ids of the vocabulary.");
var longest = OnnxToneClassifier.Ids(string.Join(' ', Enumerable.Repeat("word", 500)));
Assert.HasCount(OnnxToneClassifier.MaxTokens, longest, "A long text is cut, keeping the start and the end marks.");
Assert.AreEqual(0, longest[0]);
Assert.AreEqual(2, longest[^1]);
}
[TestMethod]
public async Task AModelThatDoesNotMatchItsHashIsNeverKept()
{
var folder = TempFolder.Create("model");
try
{
var http = new FakeHttp().On(u => u.Host == "huggingface.co", _ => new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(new byte[4096]) });
await File.WriteAllBytesAsync(Path.Combine(folder, ToneModel.FileName), new byte[10]).ConfigureAwait(false);
await Assert.ThrowsExactlyAsync<InvalidDataException>(() => ToneModel.EnsureAsync(http.Client(), folder, CancellationToken.None)).ConfigureAwait(false);
Assert.AreEqual(ToneModel.Url, http.Requests.Single().RequestUri, "The pinned revision, never a moving branch.");
Assert.IsFalse(File.Exists(Path.Combine(folder, ToneModel.FileName + ".part")), "A wrong download is deleted.");
Assert.AreEqual(10, new FileInfo(Path.Combine(folder, ToneModel.FileName)).Length, "The old file is not taken for a good one: it stays unused.");
var failing = new FakeHttp().On(u => true, _ => new HttpResponseMessage(HttpStatusCode.NotFound));
await Assert.ThrowsExactlyAsync<HttpRequestException>(() => ToneModel.EnsureAsync(failing.Client(), folder, CancellationToken.None)).ConfigureAwait(false);
}
finally
{
TempFolder.Delete(folder);
}
}
/// <summary>
/// The real model, 83 MB: it runs where it has been downloaded once (ANUBIS_TEST_MODEL_DIR, a folder with the file
/// named as in <see cref="ToneModel.FileName"/>), never downloaded by the tests themselves.
/// </summary>
[TestMethod]
public async Task TheModelReadsTheToneOfFinancialHeadlines()
{
if (Environment.GetEnvironmentVariable("ANUBIS_TEST_MODEL_DIR") is not { Length: > 0 } folder || !File.Exists(Path.Combine(folder, ToneModel.FileName)))
{
Assert.Inconclusive("Il modello ONNX non è scaricato: impostare ANUBIS_TEST_MODEL_DIR sulla cartella che lo contiene.");
return;
}
var offline = new FakeHttp();
var path = await ToneModel.EnsureAsync(offline.Client(), folder, CancellationToken.None).ConfigureAwait(false);
Assert.IsEmpty(offline.Requests, "A model with the right hash is used as it is.");
using var classifier = new OnnxToneClassifier(path);
Tone Grade(string title) => NewsJudge.Grade(classifier.Classify(title).Score);
Assert.IsGreaterThan(0, (int)Grade("Apple shares jump after record iPhone sales in China"));
Assert.IsGreaterThan(0, (int)Grade("Microsoft beats estimates and raises its full-year outlook"));
Assert.IsLessThan(0, (int)Grade("Exchange halts withdrawals after hack, Solana tokens drained"));
Assert.IsLessThan(0, (int)Grade("Target cuts its holiday outlook as shoppers pull back"));
Assert.IsLessThan(0, (int)Grade("Tesla shares plunge after a wide miss on deliveries"));
Assert.AreEqual(Tone.Neutral, Grade("The company will report its results on Thursday"));
var score = classifier.Classify("Apple shares jump");
Assert.AreEqual(1, score.Positive + score.Negative + score.Neutral, 1e-6);
var titles = Original.Select(o => o.Text).ToArray();
var watch = Stopwatch.StartNew();
for (var round = 0; round < 5; round++)
{
foreach (var title in titles)
{
classifier.Classify(title);
}
}
var perTitle = watch.Elapsed.TotalMilliseconds / (5 * titles.Length);
Console.WriteLine($"Classificatore: {perTitle:F1} ms per titolo su un thread.");
Assert.IsLessThan(100, perTitle, "SPEC 4.5 expects 10–20 ms per title on a CPU; a slow test machine gets five times that.");
}
}
+39 -1
View File
@@ -34,6 +34,11 @@
"MSTest.Analyzers": "4.5.2"
}
},
"Google.Protobuf": {
"type": "Transitive",
"resolved": "3.30.2",
"contentHash": "Y2aOVLIt75yeeEWigg9V9YnjsEm53sADtLGq0gLhwaXpk3iu8tYSoauolyhenagA2sWno2TQ2WujI0HQd6s1Vw=="
},
"Microsoft.ApplicationInsights": {
"type": "Transitive",
"resolved": "2.23.0",
@@ -65,6 +70,14 @@
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.0"
}
},
"Microsoft.ML.OnnxRuntime.Managed": {
"type": "Transitive",
"resolved": "1.31.1",
"contentHash": "158JCbNXN9/90LtjF4vHFiJYCF0bqF7CxLjm031jq7dJLEn/CtlHpMJm3UItQSIhQenlx+9Zuqi3zulwnsRVtA==",
"dependencies": {
"System.Numerics.Tensors": "9.0.0"
}
},
"Microsoft.Testing.Extensions.Telemetry": {
"type": "Transitive",
"resolved": "2.5.2",
@@ -140,10 +153,17 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"System.Numerics.Tensors": {
"type": "Transitive",
"resolved": "9.0.0",
"contentHash": "hyJB4UlpAi19Xr9AXzu2NuagKC4lPfHObNMEAA0HmqFz2rX7wKgzeYzO/jM/eBHDhnUGFFEjk5cOoJaxqg5J4A=="
},
"anubis": {
"type": "Project",
"dependencies": {
"Microsoft.Data.Sqlite": "[10.0.14, )",
"Microsoft.ML.OnnxRuntime": "[1.31.1, )",
"Microsoft.ML.Tokenizers": "[2.0.1, )",
"Npgsql": "[10.0.3, )",
"System.Security.Cryptography.ProtectedData": "[10.0.14, )"
}
@@ -151,7 +171,7 @@
"anubis.server": {
"type": "Project",
"dependencies": {
"Anubis": "[0.0.1, )"
"Anubis": "[0.7.0, )"
}
},
"Microsoft.Data.Sqlite": {
@@ -165,6 +185,24 @@
"SQLitePCLRaw.core": "2.1.13"
}
},
"Microsoft.ML.OnnxRuntime": {
"type": "CentralTransitive",
"requested": "[1.31.1, )",
"resolved": "1.31.1",
"contentHash": "XtsO/1JoKepkWbGk9/DcYtRUTduwJD68kw0eeaH5yy77YBivUZWhC1NBNlVecsBt9EitlQIjCd1hHDNXJ2jbQA==",
"dependencies": {
"Microsoft.ML.OnnxRuntime.Managed": "1.31.1"
}
},
"Microsoft.ML.Tokenizers": {
"type": "CentralTransitive",
"requested": "[2.0.1, )",
"resolved": "2.0.1",
"contentHash": "RZN3zslVWinBCMDAnbZWEY8lQbMkP54Yxm1s5NigPcrvYFzXMrDjNcVM9afq4SZHf1BV1rEmO0p7ZdfBLDbu4g==",
"dependencies": {
"Google.Protobuf": "3.30.2"
}
},
"Npgsql": {
"type": "CentralTransitive",
"requested": "[10.0.3, )",
+2
View File
@@ -19,6 +19,7 @@ return command switch
"seed-demo" => Run("dotnet", ["run", "--project", "src/Anubis.Server", "-c", "Release", "--", "--demo", .. rest]),
"screenshots" => Screenshots(rest),
"release-win" => ReleaseWindows(),
"ci-service-log" => ServiceLog.Print(rest.Length > 0 ? rest[0] : "postgres"),
"desktop-win" => NotYet("desktop-win", 10),
_ => Help(),
};
@@ -237,6 +238,7 @@ int Help()
tasks seed-demo avvia la modalità dimostrativa (dati dell'appendice F, database a parte)
tasks screenshots fotografa le schermate dimostrative a 1440 e 390 pixel
tasks release-win zip di prova per Windows in artifacts/release (ADR 0013)
tasks ci-service-log nella CI: registro e memoria del PostgreSQL dei test
tasks desktop-win installatore Windows (Fase 10)
""");
return command == "help" ? 0 : 1;
+145
View File
@@ -0,0 +1,145 @@
using System.Globalization;
using System.Net.Http.Headers;
using System.Net.Sockets;
using System.Text;
using System.Text.Json.Nodes;
/// <summary>
/// Prints what the CI cannot see from inside the job: the log, state and memory of the service containers next to this
/// one (the PostgreSQL of the tests). It talks to the Docker Engine API through the socket the runner mounts in the job
/// container, so it needs neither the docker client nor anything else to install (ADR 0004).
/// </summary>
internal static class ServiceLog
{
private const string Socket = "/var/run/docker.sock";
public static int Print(string imagePrefix)
{
if (!File.Exists(Socket))
{
Console.WriteLine($"Nessun {Socket}: il registro dei servizi si legge solo nella CI.");
return 0;
}
using var http = new HttpClient(new SocketsHttpHandler
{
ConnectCallback = async (_, token) =>
{
var socket = new System.Net.Sockets.Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
await socket.ConnectAsync(new UnixDomainSocketEndPoint(Socket), token).ConfigureAwait(false);
return new NetworkStream(socket, ownsSocket: true);
},
})
{
BaseAddress = new Uri("http://docker/"),
Timeout = TimeSpan.FromMinutes(2),
};
var info = Json(http, "info");
Console.WriteLine($"Host: {info["Name"]}, {info["NCPU"]} CPU, {Mib(info["MemTotal"])} di memoria, kernel {info["KernelVersion"]}");
var self = Json(http, $"containers/{Uri.EscapeDataString(Environment.MachineName)}/json");
var networks = self["NetworkSettings"]?["Networks"]?.AsObject().Select(pair => pair.Key).ToList() ?? [];
var found = 0;
foreach (var network in networks)
{
var filter = Uri.EscapeDataString($$"""{"network":["{{network}}"]}""");
foreach (var container in Json(http, $"containers/json?all=1&filters={filter}").AsArray())
{
var image = container?["Image"]?.GetValue<string>() ?? "";
if (!image.StartsWith(imagePrefix, StringComparison.Ordinal))
{
continue;
}
found++;
var id = container!["Id"]!.GetValue<string>();
Describe(http, id, image);
}
}
if (found == 0)
{
Console.WriteLine($"Nessun contenitore '{imagePrefix}*' sulle reti {string.Join(", ", networks)}.");
}
return 0;
}
private static void Describe(HttpClient http, string id, string image)
{
var inspect = Json(http, $"containers/{id}/json");
var state = inspect["State"];
var host = inspect["HostConfig"];
Console.WriteLine();
Console.WriteLine($"== {image} {id[..12]}");
Console.WriteLine($"Stato: {state?["Status"]}, avviato {state?["StartedAt"]}, riavvii {inspect["RestartCount"]}, OOMKilled {state?["OOMKilled"]}, uscita {state?["ExitCode"]}");
Console.WriteLine($"Limiti: memoria {Mib(host?["Memory"])}, /dev/shm {Mib(host?["ShmSize"])}, init {host?["Init"]?.ToString() ?? "no"}");
Console.WriteLine($"Controllo di salute: {inspect["Config"]?["Healthcheck"]?["Test"]?.ToJsonString()}, timeout {(inspect["Config"]?["Healthcheck"]?["Timeout"]?.GetValue<long>() ?? 0) / 1_000_000_000} s");
if (state?["Running"]?.GetValue<bool>() == true)
{
var stats = Json(http, $"containers/{id}/stats?stream=false");
var memory = stats["memory_stats"];
Console.WriteLine($"Memoria ora: {Mib(memory?["usage"])}, limite visto dal contenitore {Mib(memory?["limit"])}, massimo {Mib(memory?["max_usage"])}");
}
// Out-of-memory kills and signals as Docker saw them, from the container's start until now.
var since = DateTimeOffset.Parse(state?["StartedAt"]?.GetValue<string>() ?? "", CultureInfo.InvariantCulture).ToUnixTimeSeconds();
var until = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var events = Uri.EscapeDataString($$"""{"container":["{{id}}"]}""");
using (var answer = http.GetAsync(new Uri($"events?since={since}&until={until}&filters={events}", UriKind.Relative)).GetAwaiter().GetResult())
{
answer.EnsureSuccessStatusCode();
var lines = answer.Content.ReadAsStringAsync().GetAwaiter().GetResult()
.Split('\n', StringSplitOptions.RemoveEmptyEntries)
.Select(line => JsonNode.Parse(line))
.Where(e => e?["Action"]?.GetValue<string>() is not { } action
|| !(action.StartsWith("exec_", StringComparison.Ordinal) || action == "health_status: healthy"))
.Select(e => $" {DateTimeOffset.FromUnixTimeSeconds(e?["time"]?.GetValue<long>() ?? 0):O} {e?["Action"]}")
.ToList();
Console.WriteLine($"Eventi di Docker: {(lines.Count == 0 ? "nessuno" : "")}");
lines.ForEach(Console.WriteLine);
}
Console.WriteLine("-- registro");
using var response = http.GetAsync(new Uri($"containers/{id}/logs?stdout=1&stderr=1&timestamps=1", UriKind.Relative)).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
var bytes = response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
var raw = response.Content.Headers.ContentType?.MediaType == "application/vnd.docker.raw-stream";
Console.Write(raw ? Encoding.UTF8.GetString(bytes) : Demultiplex(bytes));
Console.WriteLine("-- fine del registro");
}
// Without a terminal the log arrives in frames: one byte for the stream, three zeros, four bytes of length.
private static string Demultiplex(byte[] bytes)
{
var text = new StringBuilder();
var at = 0;
while (at + 8 <= bytes.Length)
{
var length = (bytes[at + 4] << 24) | (bytes[at + 5] << 16) | (bytes[at + 6] << 8) | bytes[at + 7];
var size = Math.Min(length, bytes.Length - at - 8);
text.Append(Encoding.UTF8.GetString(bytes, at + 8, size));
at += 8 + length;
}
return text.ToString();
}
private static JsonNode Json(HttpClient http, string path)
{
using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(path, UriKind.Relative));
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using var response = http.Send(request);
response.EnsureSuccessStatusCode();
using var stream = response.Content.ReadAsStream();
return JsonNode.Parse(stream) ?? new JsonObject();
}
private static string Mib(JsonNode? bytes)
{
var value = bytes?.GetValue<long>() ?? 0;
return value == 0 ? "nessuno" : (value / 1048576.0).ToString("0", CultureInfo.InvariantCulture) + " MiB";
}
}