Deployment & server ops
How creddit runs in production, how code ships to it, and how to operate the box by hand. The app is a Next.js App Router site backed by a local PostgreSQL database; data is produced by off-chain cron refreshers (see Data Pipeline) and the app reads only.
Convention reminder: every trailing APY across every venue is
annualizeRatioinsrc/lib/data/apy.ts(the realised ratio of an on-chain compounding index between two blocks, annualised by actual elapsed time). Never average per-snapshot annualised rates. This is what lets/carriescompare venues like-for-like, and it is why a refresher is just an upsert job, not business logic the app re-derives.
1. Server topology
Single Hetzner box, shared with the DEX HQ stack. One host runs everything.
| Item | Value |
|---|---|
| Host | dexhq.io (Hetzner) |
| SSH | ssh root@dexhq.io (key ~/.ssh/hetzner_ed25519) |
| Repo | /opt/onchain-credit — git checkout tracking origin/main |
| App process | pm2 onchain-credit on localhost:3001 |
| Edge | nginx reverse-proxy → https://creddit.xyz, with Cloudflare in front |
| Database | local PostgreSQL; prod DB creddit, role onchain_credit, schema onchain_credit |
| Cron wrapper | scripts/run-cron.sh <script> |
| Daily backup | scripts/ops/backup-creddit.sh at 02:15 UTC |
| Log rotation | /etc/logrotate.d/onchain-credit (daily, 14 days) — see Log rotation |
pm2 processes co-resident on the box:
| pm2 process | Port | What it is |
|---|---|---|
onchain-credit | 3001 | this app (prod) |
onchain-credit-staging | 3002 | staging copy of this app (see §3) |
rindexer | — | shared indexer |
dexhq | — | DEX HQ app |
creddit-indexer | — | shared indexer |
creddit-event-ingester | — | always-on event-ledger ingester (portfolio 100k scale) — a manual add, see below |
nginx terminates the public hostname and proxies to localhost:3001; Cloudflare sits in front of nginx for TLS and CDN. The app binds loopback only — it is never reachable except through nginx.
scripts/ops/(backup, reseed, migrate, PII scrub, nginx template) is in the repo and ships via the normal deploy. The server crontab and the box-local secrets (.env.local,/root/.staging-basic-auth) live only on the box.
.env.localis the only place a key lives, andgit reset --hardpreserves it (step 3 below). A key the repo has never seen is a key a deploy cannot leak. Each environment holds its own:DATABASE_URL,SESSION_SECRET, the RPC overrides,FYND_API_KEYfor hosted live quotes,DUNE_API_KEY+ the saved-query ids, andCOINGECKO_API_KEY— the Demo key behind every live price, the hourly history that fills a hole the tape never printed, and the weekly reported-volume reading the pricing-category test is judged on. Every variable, with its default and what it costs when absent, is in Environment variables. A missingCOINGECKO_API_KEYis not an outage: the same host answers keyless at a much lower rate limit and a refused call degrades to DefiLlama and then to the stored bar, so the symptom is more assets served from the backup vendor rather than an error. A new environment, and an environment this release reaches for the first time, needs the line added by hand (pm2 restart <app> --update-envto pick it up).
2. How code ships (promote-to-prod)
Two long-lived branches, each bound to one environment. You develop on staging and promote to prod:
feature/* ──PR──▶ staging ──auto-deploy──▶ staging.creddit.xyz (develop + validate)
│
└──"release" PR──▶ main ──auto-deploy──▶ creddit.xyz (promote)- Feature work targets
staging. Merge a feature PR intostaging→deploy-staging.ymlrebuilds/opt/onchain-credit-stagingon:3002and it appears onstaging.creddit.xyz(basic-auth). - Validate on
staging.creddit.xyz. - Promote with one
staging → mainrelease PR. Its diff is the release. The PR bumpsversioninpackage.json. Merge →deploy.ymlships prod, andrelease.ymltagsv<version>+ cuts a GitHub Release (see §2.3). mainandstagingare in sync after each release.
Design + rationale (same-box isolation, reseed model, tradeoffs): docs/plans/staging-environment-plan.md.
Branch protection is advisory, not enforced. This is a free-plan private repo, so GitHub cannot enforce PR-only
mainor required checks.ci.yml(tsc + tests on every PR intomain/staging) and thescripts/ops/git-hooks/pre-pushguard report but cannot block. Honor a red CI run; don't push tomaindirectly. To make it enforced, upgrade to GitHub Pro (~$4/mo) and add a branch rule onmain: require a PR, require theCIcheck green, and (optionally) restrict who can merge. Nothing else changes.
Shipping a change, step by step
The concrete loop, from an idea to it live on creddit.xyz:
- Branch off
staging(nevermain):git fetch && git switch -c feature/x origin/staging. - Do the work, then locally
npx tsc --noEmit,npm run typecheck:scriptsand the tests you added or changed; agents never run the wholenpm teston Fred's Mac, CI and the release PR do (AGENTS.md, "What runs locally"). (The scripts typecheck is its own command, and CI runs it whenever a change can reachscripts/:tsconfig.jsonEXCLUDESscripts/, and the tests there run throughtsx, which strips types without checking them, so every refresher and sync CLI was unchecked untiltsconfig.scripts.jsonwas added. That blind spot shipped a deadstatusguard that gone-marked the whole Morpho market universe; the gate would have failed it.) If you touched a page/metric/schema/refresher, update the matchingdocs/page in the same change (the docs-in-the-PR rule). - Open a DRAFT PR into
staging.ci.ymlruns both typechecks and the unit tests that can see the change on every push (the suites that build a fixture database are not among them: they run on the release PR); the independent review starts once that is green. Its one feature-PR browser job, the zoom sweep, waits until the PR is marked ready for review, which happens once the independent review has converged (AGENTS.md, "Shipping"), and then checks only the pages the PR can move (Processes E.5). Merge it →deploy-staging.ymlrebuilds staging and it appears onhttps://staging.creddit.xyz(basic-auth). - Validate on staging. Load the changed page; if the change needs data, run the refresher/backfill once on the box against
creddit_staging(cd /opt/onchain-credit-staging && scripts/run-cron.sh <script>.ts). - Promote. Bump
versioninpackage.jsononstaging(minor for a feature, patch for a fix), then open astaging → mainPR and merge it with a merge commit (not squash — that keepsstagingan ancestor ofmain). This is the release; get explicit sign-off before merging, since it ships prod. The release is the one full browser check: the staging deploy of the version bump smokes every page against real data (its smoke report is what to read before merging), and the release PR's CI runs the whole unit half, the fixture-database suites (the golden gate among them) and every CI browser project (zoom-sweep,narrow-900,desktop-1360) on every page against the fixture. Every run before it checked only what its own change could move, and never the narrow width or the fixture suites, so a red X on the release PR is often the first word on a feature merged since the last release: fix it onstagingbefore merging. The release also runs its mandatory release steps: the release PR'schecksummary (andnode scripts/ci/release-steps-due.mjs) lists every release-steps entry markedmandatory:that is stillpendingoroutstanding; each runs in its entry's order, and the release is declared done only once eachexecuted:line carries the date. - Merge → prod ships.
deploy.ymldeployscreddit.xyzand restarts both the app and the event ingester;release.ymltagsv<version>and cuts a GitHub Release. Then fast-forwardstagingback up tomainso they match:git push origin origin/main:staging. - Post-deploy, if the change touched data. Apply prod migrations by hand (
scripts/ops/migrate.shagainstcreddit), run the refresher/backfill, then re-run the deploy so the ISR pages re-prerender against the new data (see the ISR note).
Hotfix shortcut: for an urgent prod-only fix you can branch off main, PR into main, and back-merge to staging after — but the default is always through staging.
2.1 Production deploy pipeline (.github/workflows/deploy.yml)
Triggers on every push to main (in practice, the merge of a release PR). Code only. It does not run DB migrations, backfills, or data refreshers; those are manual server steps after deploy (see below).
The GitHub runner only opens an SSH session. The server fetches the new code from this repo and rebuilds in place. Step by step:
- SSH in. Runner writes
DEPLOY_SSH_KEY(repo secret) to a temp key and pins the server host key (StrictHostKeyChecking=yes, hard-codedknown_hostsline), thenssh root@dexhq.io. The whole deploy script is passed as a single SSH argument so it runs from memory — agit resetrewriting files on disk cannot disturb the running script. - Record rollback target.
PREV=$(git rev-parse HEAD)in/opt/onchain-credit. - Fetch + reset.
git fetch https://x-access-token:$GH_TOKEN@github.com/<repo>.git main(ephemeral repo-scopedGITHUB_TOKEN) thengit reset --hard FETCH_HEAD..env.localandnode_modulesare gitignored, so the reset preserves the server's secrets and installed deps. - Install + build.
npm ci --no-audit --no-fund && PG_POOL_MAX=4 npm run build. ThePG_POOL_MAX=4caps the pg pool for the build only:next buildprerenders the ISR pages across several worker processes, each of which opens its own pool (seepostgres.ts), so the default max of 12 per worker can trip an intermittenttoo many connections for rolebuild failure that then rolls the deploy back. Four keeps the build under the role's connection cap; the runtimepm2process leaves the var unset, so serving keeps the full pool of 12. If a build still fails this way it is transient —gh run rerun --failedclears it, no code change needed. - Restart and check health (success path).
pm2 restart onchain-credit --update-env, thenscripts/ops/restart-ingester.sh, which restarts the two always-on processes, the event ingester and then the ledger worker (each loads its code once, at start; see the deploy restarts the ingester and the worker). The script leaves a deliberately stopped process alone with a::warning::; if pm2 refuses a restart, cannot report a process's state, or either process is missing, it prints an::error::and the run exits 1, since the app is then live on the new code and that process may not be (a missing worker means nothing derives: that section says why). The workflow then requires HTTP 200 fromhttp://127.0.0.1:3001/api/health, which checks the app and Postgres withSELECT 1: up to five attempts, each with a 3-second connection timeout and a 15-second total timeout, separated by 3 seconds. Failure marks the run red and leaves the new code deployed; it does not trigger a rollback. Only after a healthy response does it log the deployed short SHA + timestamp.Deployed <sha>confirms the accepted restarts and origin app/database health; the ingester's first cycle and the worker's first job still require their own log check after a release (see the post-deploy check in that section). - Rollback (failure path). If
npm ci/npm run buildfails:git reset --hard $PREV, reinstall, rebuild from the previous commit, run the same restart (both processes are back on the code they already ran, and the rebuild would otherwise read as a missed restart to the freshness alarm), andexit 1. A failed deploy never leaves broken code or a half-built.nextstaged for the next process restart.
Other guarantees:
- One at a time.
concurrency: group: deploy-production,cancel-in-progress: false— a second push waits its turn rather than racing. - Least privilege. Workflow
permissions: contents: read; the deploy key only authorises SSH to the box; the GitHub token is ephemeral and repo-scoped. - Host-key pinning is verified out-of-band against
/etc/ssh/ssh_host_ed25519_key.pub. Update theknown_hostsline in the workflow if the server is ever rebuilt, or every deploy will fail host-key verification.
Manual post-deploy steps (when a change needs more than code)
The pipeline restarts the app, the event ingester and the ledger worker with new code and a fresh build, nothing else. If your change touches the database or data, do these on the box after the deploy lands:
- Schema migration → apply the new
scripts/sql/NNN-*.sqlas thepostgresowner (see §4, Migrations). - Backfill → run the relevant
scripts/backfill-*.tsviarun-cron.sh(one-off history seeds for newly added wrappers/markets; they reuse the same annualisation math as the live refreshers). - Refresher → run the affected
scripts/refresh-*.tsviarun-cron.shso the new rows exist immediately instead of waiting for the next cron tick. - Carry registry → after a new yield adapter or DEX-pool snapshot lands, re-run
scripts/sync-carries.tsthen--approve <key>the newly-PROPOSED carries (replaces the retiredsync-fluid-vaults.ts; see processes A.0). It changes/carriesonly. - Repo lending's isolated markets → no deploy, sync or refresher changes which ones the page shows.
scripts/repo-isolated-check.tsreports what qualifies and what no longer does (report only, writes nothing); a listing changes only with its--approve/--remove, on the owner's decision, after whichbackfill-morpho.ts --market <ids>fills an approved market's rate history (see processes A.8). - Pendle PT payout assets (migration
102) → apply it after the deploy, not before, and nothing else is required. The reasoning and the one-off tape cost are in the release step. - Pendle redemption-index factor → after migration
101nothing is required: the 6h refresher fills the factor going forward, and fills a newly discovered market's history itself (at most two markets a tick, after the snapshot loop; it also drains markets that still hold nopendle_apihistory, narrowed in-query throughportfolio_held_ptsand read only back to the ledger derivation floor, rotating so no market can hold the head of the queue, so a deferred or failed pass retries on its own).backfill-pendle-history.ts --market <address>stays the manual tool for a market discovered before that wiring shipped, and for a market whose automatic pass was short — the fill is idempotent, so re-running only ever adds (4 to 5 chain reads per row, so per market, never in bulk). The sequence that ran once, when101shipped, is in the release-steps log. - Money market fund registry → after migration
086, runscripts/sync-money-market-funds.ts(discovery + classification), thenrefresh-assets.tsfor the first state read, thenbackfill-money-market-funds.tsfor the return history. Approving a manager needs no deploy: the refresher, the share-rate snapshotter and the backfill all read their work-list from the registry. The sequence that ran once, when086shipped, is in the release-steps log.
Release steps are not in this runbook
A note written for the deploy of one migration, or a gated repair written for one release, is executed a single time in each environment and is then finished. None of them lives here. They are in the release-steps log, verbatim, each carrying the migration or release it belongs to and an executed: status, so an operator can tell a step that is still outstanding from one that is done — which is exactly what could not be told while they were interleaved with the standing procedure on this page.
This page holds only what is true on any day: topology, shipping, the deploy pipeline, staging, versioning, the migrations procedure, the crontab, backup, verification and operations. scripts/runbook-split.test.ts fails the build if a one-time step is written back into it.
ISR re-prerender note
Pages are App Router with per-page ISR (revalidate 1800 or 3600 seconds), prerendered at build time. The build snapshots whatever the DB held when npm run build ran. So:
If a refresher (or a manual backfill) writes new data after the deploy build has already run, the prerendered pages keep serving the stale snapshot until their revalidate window elapses (up to 30/60 min). To make fresh data appear immediately, re-run the deploy (push an empty commit or re-run the workflow) so the build re-prerenders against the now-current DB. This matters most for capacity/risk pages where a refresher and a deploy land close together.
The clean ordering is therefore: migrate → backfill/refresh the data → then deploy (or re-deploy), so the build prerenders against complete data.
2.3 Versioned releases
Every promotion to prod is a numbered release. .github/workflows/release.yml runs on push to main when package.json changed:
- Reads
versionfrompackage.json. - If a release for
v<version>does not already exist, it tagsv<version>at the merge commit and cuts a GitHub Release with auto-generated notes (the merged PRs/commits since the previous tag). - If the version was not bumped, or the release already exists, it is a no-op.
So: the release PR bumps version (semver: minor for features, patch for fixes), and merging it both ships prod (deploy.yml) and records the release (release.yml). The tag is the durable "what shipped, when" record; deploy.yml is still what actually deploys. git tag --list / the repo's Releases page is the prod history.
3. Staging
A second, independent copy of the app on the same box, fed by a scrubbed copy of prod data. This is where all development lands and is validated before promotion (see §2).
| Item | Value |
|---|---|
| URL | https://staging.creddit.xyz (Let's Encrypt cert, HTTP→HTTPS) |
| Access | HTTP basic-auth (/etc/nginx/.htpasswd-staging; credential in /root/.staging-basic-auth) + noindex |
| Repo | /opt/onchain-credit-staging, tracking origin/staging |
| App process | pm2 onchain-credit-staging on :3002 (memory ceiling --max-memory-restart 700M) |
| Database | separate DB creddit_staging, role onchain_credit_staging (conn-limit 20, statement/idle timeouts) |
| Deploy key | separate DEPLOY_SSH_KEY_STAGING (a staging-deploy compromise ≠ prod-box access) |
3.1 Deploy (.github/workflows/deploy-staging.yml)
Auto-deploys on push to staging (the develop flow). Same SSH + git reset + build + pm2 mechanism as prod, pointed at /opt/onchain-credit-staging and :3002, using the separate staging deploy key. It builds with the same PG_POOL_MAX=4 cap as prod; the creddit_staging role's connection limit is only 20, so the smaller build-time pool matters most here (this is where the too many connections for role build flake was first observed). After build it runs scripts/ops/migrate.sh against creddit_staging (additive migrations auto-apply on staging) and a :3002 health check, with the same build-failure rollback.
workflow_dispatch (with a ref input, default main) refreshes staging from any branch on demand — useful to reset staging to prod's exact state.
Post-deploy smoke
Once the deploy job succeeds, a second job in the same workflow (smoke) checks the build it just put on the box against staging's real data, with two things from tests/e2e: render-smoke.spec.ts, one case per route in the zoom sweep's registry, asserting that the page renders and its main content is not empty (a screener lists at least one row), and the zoom sweep. What this job exists to catch is a page rendering empty against staging's real data; that is the same answer at every viewport width, so one page load per route answers it, and unlike the per-surface specs (which skip on a thin dataset) the render check fails on an empty screener. The sweep runs here as well as pre-merge because it is the one project whose answer changes with the data: staging carries the long asset names and row counts a deterministic fixture cannot, and long names are what collide. The interaction specs run pre-merge instead, against the fixture (the PR's own specs locally, and every page's on the release PR in CI's e2e-interaction job): they assert structure and behaviour, which staging's data does not change. It is a job in this workflow rather than a workflow of its own on purpose: a separate file triggered by workflow_run fires only from the copy that sits on the default branch, so merging one into staging would arm nothing.
The sweep is narrowed to the pages the push can move. The smoke job's first step (the gate) runs the page scope (scripts/ci/affected-surfaces.mjs, Processes E.2) over the files between the push's before and its head, the same answer ci.yml's browser job and the QA pass read: the sweep then visits only those pages' surfaces (the render check runs every route, one page load each), and the rest of the job stands down when the range moves no page at all (docs, crons, workflow edits, a unit test registered in package.json). Some changes are every page because nothing imports what they move: a dependency or lockfile change, next.config.ts (headers and the CSP), postcss.config.mjs (the Tailwind pipeline), anything under public/ (every logo, icon and font a page paints), and a migration, which the deploy job applies to creddit_staging before the smoke reads a page. The fixture database is no page here, since this job does not render from it. A version bump in package.json is every page: that is how a release is cut (§2, step 5), so the deploy of the release candidate smokes every page on real data, the real-data half of the release's full check. Every uncertain answer runs it, whole: a workflow_dispatch, a push with no usable before (a new branch, or a force-push that took it out of the history), a scope script that fails, and a gate step that dies without answering, since the later steps stand down for an explicit "no" from the gate and for no other answer. (The job's if does skip on two further things, neither of them a doubt about the diff: a deploy that did not succeed, and a cancelled run.) A stood-down smoke skips its remaining steps, with its reason and the scope's per-file reasons written into the run summary, never a red job, and the run stays green. To smoke a stood-down deploy anyway, use the by-hand smoke below: re-running the run replays the same push event and lands on the same answer, and a workflow_dispatch redeploys the box to its ref input first.
It needs no secret that does not already exist. The runner opens an SSH tunnel with the deploy key this workflow already uses (DEPLOY_SSH_KEY_STAGING) and points the suite at the app directly instead of at the public hostname. The same two lines are the manual smoke, from a checkout of whatever is currently on staging:
ssh -f -N -L 3002:127.0.0.1:3002 root@dexhq.io # staging's app port, firewalled from the internet
E2E_BASE_URL=http://localhost:3002 npm run e2e # setting it also stops playwright.config.ts booting a local server
# CI runs `--project=desktop-1360 --project=zoom-sweep 'tests/e2e/render-smoke\.spec\.ts' 'tests/e2e/zoom-sweep\.spec\.ts'`, the sweep narrowed to the pages the push can move; by hand, run whichever projects and specs you need.Going through https://staging.creddit.xyz instead would mean sending the NGINX basic-auth credentials, and NGINX holds only a one-way $apr1$ hash of that password. There is no BASIC_AUTH_* repo secret, none can be derived from the box's NGINX config, and none is needed.
Report only. The job is continue-on-error: true, so a red suite never paints the deploy run red: by then the deploy has succeeded and the new build is already live, and the build-failure rollback above stays the only automatic revert here. The signal is an error annotation, a block in the job summary, and, on failure, the Playwright HTML report plus traces uploaded as the run artifact staging-smoke-<run-id>-<attempt> (14-day retention). That artifact is what to read before opening the release PR.
Everything that ran passed, with the sweep's signed-in surfaces skipped, is the healthy result. They sign their own session cookie and skip when the server does not sign with the same secret, and staging's SESSION_SECRET is deliberately not handed to CI. The render check runs signed out and has no such floor. Any other skip is a sweep state's row guard reporting that staging's data left a surface too thin for that state: a note about the dataset, not lost coverage.
The smoke runs inside the deploy-staging concurrency group, so a second merge queues behind it instead of restarting pm2 underneath a running suite. The group is held for the whole run, and the smoke step's timeout-minutes: 20 (with a timeout-minutes: 25 backstop on the job around it) is therefore also the longest a wedged staging can keep the next deploy waiting. When a recovery dispatch cannot wait, cancel the in-flight run from the Actions UI: its deploy job has already finished by the time the smoke is running, so cancelling costs only the report. Specs, fixture DB and the per-surface checklist: Processes → Verifying a UI change.
3.2 Data: reseed, not cron (by design)
Staging runs no refreshers (the one scheduled job in its checkout is the minutely drain-portfolio-backfills.ts, which serves user-triggered backfills rather than refreshing market data). Its data is a nightly scrubbed copy of prod (03:00 UTC cron, after the 02:15 backup), plus on-demand, via scripts/ops/reseed-staging.sh:
# on the box
scripts/ops/backup-creddit.sh # fresh prod dump -> /opt/backups (also the nightly backup)
scripts/ops/reseed-staging.sh # drop+recreate creddit_staging from newest dump, scrub PII, re-grantThe reseed masks PII (scrub-staging-pii.sql: subscriber emails hashed to @staging.invalid, user agents dropped; the whole user-data graph — the four chat tables, accounts, account_wallets, the two portfolio history tables, portfolio_wallet_index and portfolio_backfill_state — TRUNCATEd in one statement, since a reseed restores the whole prod DB with no table-exclusion and a wallet's real positions must not ride into staging; one statement because every one of those tables FK-references accounts (as of migration 056 the chat tables and portfolio_wallet_index do too), and Postgres refuses to truncate an FK-referenced table unless every referrer is in the same statement) and fails closed if any unmasked email survives, or any account/portfolio table (accounts, account_wallets, both history tables, portfolio_wallet_index, portfolio_backfill_state) still holds rows after the scrub, or any per-wallet cursor (chain_scan_cursors, scope portfolio:derive:v2:%, or the retired discovery watermark portfolio:discover:%) survives it. That last one is a CORRECTNESS hazard for the ledger prefix: chain_scan_cursors has no FK to accounts, so a surviving per-wallet cursor certifies work against a table the TRUNCATE has just emptied — the shape of the 2026-07-21 incident, which the discovery watermarks once reproduced on staging for every wallet in the dump. The watermarks are retired and migration 106 deletes them, but a pre-106 prod dump still carries them (prod's enrolled-wallet list), so the scrub keeps that prefix until a reseed from a post-106 dump has run. The scrub deletes those rows (prefix-scoped, so every other refresher cursor survives as it must). Because the four portfolio-workstream tables are emptied, re-run scripts/ops/seed-portfolio-fixtures.ts after each reseed to re-register the public test whales (or touch /root/.reseed-paused during a multi-day validation window so the reseed is skipped and the fixtures persist). It carries prod's current schema and its schema_migrations ledger, so after a reseed migrate.sh reports 0 pending. A reseed reverts any not-yet-promoted migration under test; pause it with touch /root/.reseed-paused. To validate a refresher change on staging, run it once by hand: cd /opt/onchain-credit-staging && scripts/run-cron.sh <refresher>.ts (its run-cron.sh sources the staging .env.local, so it writes creddit_staging).
3.3 Same-box safety guards
Staging shares the box and the Postgres cluster with prod, so the guards matter: the staging role's statement_timeout/idle_in_transaction_session_timeout/connection cap kill a runaway staging query, the PM2 --max-memory-restart restarts staging (not prod) on a leak, and hourly disk alerting bounds the backups + DB-copy growth. A dedicated VM is the upgrade path if OS/nginx/PG-version testing is ever needed.
4. Operating the server
SSH and pm2
ssh root@dexhq.io # key ~/.ssh/hetzner_ed25519
pm2 list # all processes + status
pm2 logs onchain-credit # tail prod app logs
pm2 logs onchain-credit --lines 200 # recent
cd /opt/onchain-credit && (set -a; . ./.env.local; set +a; pm2 restart onchain-credit --update-env)
# restart prod with .env.local re-read (see below)
pm2 restart onchain-credit-staging --update-envpm2 keeps each process's environment from its last --update-env restart, and Next never overrides a variable already set. So once a key from .env.local sits in that stored environment (a sourced restart puts every key there), a later edit to the file reaches the app only through a restart with the file sourced, as above; cron jobs read the file directly and see it at once.
Event ledger ingester (portfolio 100k scale)
The event-ledger ingester (scripts/ingester/ingest-events.ts) is the wallet-count-independent spine of the 100k-wallet plan. Unlike the refreshers it is not a cron: it is an always-on PM2 process with its own ~60s loop. Bringing it up is a manual server step, in order:
Apply migration
064(additive + idempotent; staging auto-applies on deploy, prod is the usual gatedmigrate.sh). It createsraw_events(range-partitioned)event_coverageand handsraw_eventsto theonchain_creditrole so the ingester can auto-create partitions at runtime (see database.md).
bashcd /opt/onchain-credit && scripts/ops/migrate.sh "$(grep ^DATABASE_URL= .env.local | cut -d= -f2-)"Ownership caveat on staging. The parent ends up owned by
onchain_credit(the prod role), and migration 064 alsoGRANTs that roleCREATEon the schema so on prod the ingester and backfill auto-create partitions at runtime. Staging connects asonchain_credit_staging, which neither owns the parent nor is the grantee, so its runtime partition auto-create fails. As of this rollout (the band has since grown down top90; current state below) the pre-created band covered only the live tip (the ingester's forward inserts), NOT the one-time backfill: the 2025-05-21 floor (~block 22.5M, partition p90) sat below the band's p98 floor, so step 3's backfill needed auto-create even for the validation window and failed LOUD at the time (ensurePartitionsnever does a silent bad insert). Loud is no longer today's symptom, and that is the trap:064's own backfill has since materialisedp90–p112, so the floor lands in an existing partition and the missing privilege now shows up as a swallowed error per partition index, with the loud failure not due until the head crosses 28,250,000 (measured, see below). The step is still required and its absence is now silent. Before exercising the backfill on staging, give the staging role both halves on that box — ownership of the parent alone is not enough, sinceCREATE TABLE … PARTITION OFalso needsCREATEon the schema:sqlALTER TABLE onchain_credit.raw_events OWNER TO onchain_credit_staging; GRANT CREATE ON SCHEMA onchain_credit TO onchain_credit_staging;Both repeat after every reseed. Current state of the band, and how to verify the privileges directly: Migrations 082 / 083.
Start the PM2 process.
run-ingester.shsources.env.localandexecs the long-running loop, so PM2's stop signal reaches the ingester's graceful-shutdown handler: it sets a stop flag, finishes the current cycle and exits 0. Every prod deploy restarts it, so that is routine: a live-scan cycle advances its cursor last, so an interrupted one re-scans its range, and the reorg repair is one transaction that either lands whole or not at all.--kill-timeoutis what makes "finishes the current cycle" true. pm2's default is 1.6 seconds, so without the flag a cycle still running at a restart is SIGKILLed rather than allowed to finish. 120s covers an ordinary cycle and costs a restart nothing, since pm2 waits only until the process exits; a cycle in the middle of a heavy archive catch-up can still outlast it, which is why the repair that a kill used to corrupt is a transaction rather than merely a slower thing to interrupt. The value is stored in the process definition, sopm2 restartdoes not add it to a process that was created without one — an existing ingester needs the one-time step.bashcd /opt/onchain-credit pm2 start scripts/ingester/run-ingester.sh --name creddit-event-ingester --time --kill-timeout 120000 pm2 save # persist across reboots (and the flags above) pm2 logs creddit-event-ingesterIt reuses
ETHEREUM_RPC_URL(live head + live-windowgetLogs) andETHEREUM_ARCHIVE_RPC_URL(catch-up history + block timestamps) from.env.local.GATED STEP: run the historical backfill, one stream at a time (attended; fills
raw_eventsfrom the coverage floor, block 24,136,053 (2026-01-01), up to the ingester's live cursor; resumable, archive RPC). This is a prod write and needs explicit approval before it starts.Run the smallest streams first, so the harness is proven cheaply, and never two concurrently — the run is bounded by the archive endpoint's rate limits and by disk, and its wall-clock per stream is a figure the capacity model depends on. Between streams re-read
df -h /and stop if free space has fallen below 25 GB.bashcd /opt/onchain-credit && ETHEREUM_ARCHIVE_RPC_URL=<archive> \ DATABASE_URL="$(grep ^DATABASE_URL= .env.local | cut -d= -f2-)" \ npx tsx scripts/backfill-event-ledger.ts --stream <name> --dry-run # plan + expected rows cd /opt/onchain-credit && ETHEREUM_ARCHIVE_RPC_URL=<archive> \ DATABASE_URL="$(grep ^DATABASE_URL= .env.local | cut -d= -f2-)" \ npx tsx scripts/backfill-event-ledger.ts --stream <name>Run
--helpfirst when preparing a command. Help, unknown options and positional arguments exit before contacting the database or an RPC provider. Final completion now requires the measured count band, uninterrupted receipts, named anchors, and a sampled comparison fromLEDGER_VERIFY_RPC_URL; raw events and receipts remain resumable when any check fails, but no completion record, rollout marker, or live handoff is written. The pending-withdrawal history band is now exactly 294,592 rows, enumerated from an independently indexed copy of the chain pair by pair, and replaces the earlier 280,000–325,000 range, whose four venue subtotals were counts of the rows it was meant to judge. Both of the old endpoints are refusals now. It applies only to that completed history sweep, from the campaign floor through its frozen original handoff at block 25,828,233. Events from the next block onward are live activity and are excluded even when acceptance is retried later; a later or extended range needs its own preflight samples. The number counts the four filters that sweep scanned: f(x)'s fee-free fxSAVE exit added two more (fxSAVE and the f(x) pool, three (address, topic0) pairs), so a sweep that ingests their history is judged against a band re-measured over all six with the recorder below, never against this number (that release's steps).The Pendle router stream's history band is also strict, through its frozen original handoff at block 25,833,248. It was first 671,654 router records from 22,527,558, from a complete attended preflight over 662 small windows whose independent early, middle, and recent checks matched the second archive provider. The stream's second filter (the limit-order contract's fills) is judged with it: once that filter's history is swept, the band is re-measured over both filters from the coverage floor with the recorder below, and
--verifyjudges the stored rows against that band, never against 671,654 (that release's steps). Later live activity is deliberately excluded from this one-time historical acceptance.--budget Nbounds an invocation to N windows if the window has to be broken up; it stamps no completion and the next run continues from the resume cursor. A stream swept over several such runs still certifies from the floor: the certificate is a claim about the range, made by every run together. Recovering a known hole takes an explicit range —--from A --to B— which sweeps that range whatever the resume cursor says; a run with no--fromresumes and would skip it. An explicitly ranged run, and any run capped by--tobelow the stream's target, is not a completion: it printsnot a completion run; the rollout marker was not evaluated, leaves the live cursor alone, and exits zero. That line is expected on a repair and is not a stop.Then, before starting the next stream, take the verdict from the command and not by eye.
--verifyevaluates four numeric conditions (row count against the stream's band, receipt contiguity, a sampled digest re-check against the second provider, and the stream's named anchors together with alive-from-the-floor certificate for every address it declares) and exits non-zero with a[backfill-ledger/fail] …line if any misses. Condition 4 refuses outright when a stream would leave it nothing to check — no anchor and no declared address set is a refusal, not a pass. Any condition missing = stop.The band condition 1 uses must be independent of the rows it judges. Every stream carries either a density forecast from the data contract (compared with a 70%–200% allowance) or an enumerated count from a chain scan (compared exactly). A stream whose forecast is wrong takes a fresh enumeration, never a count of what was imported:
bashcd /opt/onchain-credit && ETHEREUM_ARCHIVE_RPC_URL=<archive> \ LEDGER_VERIFY_RPC_URL=<second provider> \ DATABASE_URL="$(grep ^DATABASE_URL= .env.local | cut -d= -f2-)" \ npx tsx scripts/record-ledger-backfill-band.ts --stream <name> --to <handoff> \ --note "<date> preflight" [--window 1000] [--dry-run] --confirmIt refuses when the verify endpoint is the one the sweep's own scan receipts name as the provider the corpus was collected from (the environment is only consulted for a stream that has no receipt yet), when
--tois above the finalized head, when--tois anything other than the frozen handoff for the two streams whose range is fixed (escrow→ 25,828,233,pendle-router→ 25,833,248), and forwallet-token(whose expectation moves with the enrolled wallet set).--dry-runmeasures and writes nothing. Recording a band approves nothing:--verify's four conditions still decide. A band recorded before this provenance rule existed is quarantined by migration089and named in the refusal; re-measure it with the command above rather than editing the row.The recorder measures from the coverage floor, 2026-01-01 (block 24,136,053). A band recorded before the floor moved up starts at 22,527,558 and stays valid:
--verifyjudges a band over its own range, so it needs no re-measurement. Re-measure only when the stream's declared surface changed (a new stream, or a new escrow class swept from the floor); the new band then covers[24,136,053, handoff]and supersedes the old one. Before re-bandingaave-poolor theaave-token/spark-tokenpair, know that their named anchors are 2025 blocks: over a range from 2026-01-01 condition 4 reports them unavailable until they are restated in code. The recorder also stores how many addresses the stream declared (migration138); forerc4626, whose vault universe grows, the runner keeps the floor at the measured count and widens the ceiling by 7,000 rows per vault declared since, so an ordinary listing does not fail a complete corpus. It also stores WHICH addresses it measured (migration142), because a declared set shrinks too: a registry row a migration retires leaves the set while its stored rows stay.--verifythen counts stored rows only on the measured addresses plus those declared since, so a retired address's kept history is neither counted nor a failure (the condition-1 line endsN stored row(s) on addresses in neither, not counted), a measured address retired since still counts, and "declared since" is counted by address (D declared since), so shedding some vaults and listing others leaves headroom for each one listed. A band recorded before142counts every stored row of the stream, as before; so dofluid-operateandwallet-token, whose rows are not keyed on what they declare.What that tolerance does not prove. The floor is still the exact independent count, so a vault the measurement covered that lost rows still fails. But a vault listed AFTER the measurement is bounded only from above: anything from 0 to 7,000 rows passes condition 1, and condition 3 samples the sweep's receipts, never the ingester's catch-up windows, so that vault's history rests on its own catch-up and its
livecertificate alone until the next re-measure. Its rows can also, in principle, mask a shortfall in the measured set (nothing in ordinary operation removes rows, so this is unlikely). And a large vault fails a complete corpus: 32 of the 648 vaults on 2026-10-11 store more than 7,000 rows over the range (the largest 169,046), so listing one like that fails condition 1 above the ceiling. A condition-1 failure ABOVE the ceiling after a listing means "re-measure", not "hole"; one BELOW the floor is a hole.When to re-measure
erc4626(the command above,--stream erc4626 --to <its backfill handoff> --window 1000): (a) whenever--verifyfails its condition 1 above the ceiling, and (b) at the first release after the count of vaults declared since the measurement reaches 50, which--verifyprints asceiling sized off D declared address(es) vs M measured, N declared since(N; on a band recorded before142, D minus M). Fifty keeps the vaults whose history no independent count has checked under ~8% of the set, and at the pace of 2026-08/10 (about 100 listings in six weeks) it comes round roughly every three weeks, while one re-measure costs a few hours on the free second provider; a weekly cadence would spend that for a handful of mostly empty vaults (the median vault holds 2 rows over the range).Size these before you start one. The enumeration is a full
eth_getLogswalk of the whole range from the floor to the handoff (~1.7M blocks from 2026-01-01; the counts below were taken over the original ~3.3M blocks from 2025-05-21) against the free second provider — the same endpoint that times out onerc4626at a 5,000-block window — so run them one at a time,--dry-runfirst, and expect them to be long:stream logs to enumerate suggested --windowescrow~295k over the 17 (address, topic0) pairs enumerated in 2026-08, plus f(x)'s three (fxSAVE's request and claim, the pool's payout: on the order of thousands at the 2026-10 rate of a few each a day), plus Renzo's three (13,644 logs from the floor to 2026-10-09), plus Binance's two (43 logs from the floor to 2026-10-09: wBETH's unwrap queue), plus Stader's two (5,779 logs from the floor to 2026-10-09: ETHx's withdrawal manager), plus Apyx's three (4,322 logs to 2026-10-11, all after the floor: apyUSD's ReceiptIssuedand the unlock receipt's claims and transfers), plus Saturn's six (8,441 logs to 2026-10-11, all after the floor: sUSDat's withdrawal queue's requests, processings, claims and ticket transfers), plus StakeWise's two (177 logs to 2026-10-12, all after the floor: the osETH redeemer's requests and claims)5,000 pendle-router~672k router logs (the router's whole stream, measured from 2025-05-21), plus the limit-order contract's fills: 40,226 from the floor to the handoff (2026-10-19) 5,000 (662 windows in the 2026-08-25 attended run) erc4626~1.25M over 546 addresses 1,000 — 5,000 times out on the address-set width fluid-dex~24k over 49 pools 5,000 aave-token+spark-token~4.2M, the two together 5,000, and budget for the longest run of the campaign The pair is measured as a pair:
--stream aave-tokenenumerates both and writes both rows, because their band is the pair's joint total and condition 1 sums their rows together.bashcd /opt/onchain-credit && ETHEREUM_ARCHIVE_RPC_URL=<archive> \ LEDGER_VERIFY_RPC_URL=<second provider> \ DATABASE_URL="$(grep ^DATABASE_URL= .env.local | cut -d= -f2-)" \ npx tsx scripts/backfill-event-ledger.ts --verify --stream <name>--verifyis read-only. Pass a smaller--windowforerc4626's verify leg: the free second provider times out on a 5,000-block range across its 546 addresses. Record, per stream, the rows written, thepg_total_relation_sizedelta and the wall-clock.On completion the runner stamps each marker-governed stream's rollout marker, and refuses to when a declared address is short of the floor or the receipts have a gap. That refusal happens inside the promotion transaction, after the coverage rows are written and before the marker is, and the address set it checks is re-read from the registry rather than taken from the run's own list — so an address added while the sweep ran is caught. A refusal rolls the whole promotion back: no coverage row, no marker, no live handoff, and the run exits non-zero. Every stream the rebuild adds is stamped this way,
escrowincluded; the two whose address set never finishes (transfers,wallet-token) are not, by design. Details and the enable rule: the backfill runner.Do not answer that refusal by re-running the command. The run keeps its imported events, its receipts and its resume cursor, so a re-run scans nothing and goes straight to the promotion with the new address now in its own declared set — and the runner refuses again, by name, because it will not mark an address complete unless that address's history over the certified range was actually imported. The repair is to import it, and which half does it is the ownership split: wherever the tip loop owns new addresses — any stream that already carries its rollout marker, plus the two open sets (
transfers,wallet-token) — its per-address catch-up sweeps the address from the floor unattended, so wait forcaught up <address>increddit-event-ingester. On a stream whose history this runner still owns (no marker yet, no addresslive), re-sweep it with an explicit--from <floor> --to <handoff>, which is logged as a repair and promotes nothing on its own. Then run step 3's plain command again.That repair has to run to completion, and this is the part that is easy to get wrong. The runner records the blocks it has actually committed, per address, as it commits them — so a re-sweep sized with
--budget, killed by an RPC error (a[fail]line, and on a budget-bounded run the exit code is still 0), or stopped with Ctrl-C enrols the address over exactly the slice it covered, and the next promotion refuses on the rest. Read the plan line:N/M window(s) this runwithbudget-bounded, resumablemeans it did not finish. Re-issue the same--from <floor> --to <handoff>with no--budget— an explicit--fromre-sweeps its whole range regardless of the resume cursor, and re-scanning covered blocks is free (event writes are conflict-ignored and the cursor only moves forward). The[floor, floor]row a started-but-unfinished pass leaves behind is an ownership marker, not coverage, and the promotion reads it as the nothing it is.Record the market impairments over the backfilled history, once (attended; archive RPC). Step 3 is what makes this possible and step 2 is what makes it necessary — see the impairment pass below for why the always-on pass will not reach down into history on its own. Run it over the range step 3 just filled:
bashcd /opt/onchain-credit && ETHEREUM_ARCHIVE_RPC_URL=<archive> \ DATABASE_URL="$(grep ^DATABASE_URL= .env.local | cut -d= -f2-)" \ npx tsx scripts/ingester/market-impairment.ts --from <floor> --to <live cursor>It is idempotent and resumable (the upsert is content-wise, so a re-run of a recorded range touches nothing) and exits non-zero if it refused anything.
A read-only live smoke (scripts/ingester/smoke-ledger.ts) checks the decode path against a live RPC without touching the DB. Phase A ships the ledger only — nothing reads it yet (the 6h cron keeps its own scans), so a missed step degrades nothing user-facing. Log-rotation note: the ingester writes PM2 logs like the app, but the /etc/logrotate.d/onchain-credit glob currently matches only the four creddit-app logs — add creddit-event-ingester-{out,error}.log to it (same copytruncate treatment) when the process is promoted to prod.
The Morpho market-impairment pass, and the one line to grep for
Inside the same ~60s loop, after the streams have committed their ranges, the ingester records every Morpho bad-debt liquidation into morpho_market_impairment (one row per socialised loss, market-level, naming no wallet). It reads no chain logs of its own: it walks the morpho stream's already-committed rows, on its own cursor (ledger:morpho-impairment), in windows of INGEST_IMPAIRMENT_WINDOW blocks (default 250,000). Nothing is served from it yet.
It only walks blocks the morpho stream has certified. The stream's event_coverage row is the floor (never below the 2026-01-01 coverage floor), not the coverage floor alone, and the difference matters on a fresh box: this pass derives from raw_events, so a block nobody scanned reads exactly like a block with no bad debt in it. Step 2 above starts the ingester before step 3's backfill, and a fresh live stream starts at the tip — so floored at 2026-01-01 alone the first pass would sweep two million empty blocks, record nothing, and park its cursor at the tip, after which step 3 fills the history underneath a cursor that never looks down again. Hence step 4: the daemon owns the tip, the standalone pass owns the history.
When something cannot be recorded truly, the cursor stops rather than skipping. A refusal means one bad-debt event could not be written honestly — an archive endpoint that will not serve market(id), a loan token whose decimals() cannot be read, or supply shares that moved through something other than a Supply or a Withdraw (the market-fee tripwire). Advancing past it would turn a visible refusal into a permanent silent hole in a record nothing else re-derives, and a missing impairment does not read as missing: it reads as a market that had no bad debt.
There is no exit code for a daemon to alert on, so the log line is the alarm and it repeats every cycle until somebody acts:
pm2 logs creddit-event-ingester --lines 200 --nostream | grep 'morpho-impairment.*HOLD'The line names the held cursor, how far behind the morpho stream it has fallen, and the refusal's own reason (transaction, log index, market, and which block the read failed at). The hold is also queryable, since a held cursor stops moving:
SELECT scope, last_scanned_block, updated_at
FROM onchain_credit.chain_scan_cursors
WHERE scope IN ('ledger:morpho', 'ledger:morpho-impairment');What to do about one. Most holds are a transient archive failure and clear themselves on the next cycle. A hold that persists is one of three things, in the order worth checking:
- The archive endpoint. Re-read the named
market(id)at the named block againstETHEREUM_ARCHIVE_RPC_URL. A provider that has stopped serving that depth is the common cause; pointing the ingester at one that does and restarting it clears the hold. - A non-zero market fee, if the refusal names one. That is the tripwire firing for real:
AccrueInterestcan mint supply shares with noSupplyevent, so the reconstructed denominator would be short by the fee shares. The refusal is correct and the fix is a code change, not an ops action. - Genuinely unrecordable, if neither holds. The cursor stays put by design.
The stall is about the cursor, not about the record: every run still writes the events it could prove, and the rest of the range can be recorded immediately while the cursor stays below the refusal.
# record everything ABOVE the refused block; leaves the ingester's cursor alone
npx tsx scripts/ingester/market-impairment.ts --from <refused block + 1> --to <live cursor>Running the standalone pass over a range that contains the refused event refuses it again and exits non-zero, deliberately: the same judgement, from the same inputs. Note also that a held cursor re-reads its whole window from the archive every cycle, so a hold left in place is a standing cost as well as a standing gap.
--reconcile is a repair flag, not a default. It deletes stored rows whose liquidation the canonical chain no longer carries, which is how a reorg is repaired here (the always-on pass does this automatically for the range it re-derives, and the reorg sweep rewinds this cursor with the stream cursors so the repaired range is walked again). Outside the morpho stream's coverage certificate the same predicate would erase true history, so the standalone pass refuses the flag there. The historical pass in step 4 does not want it.
The deploy restarts the ingester and the ledger worker
creddit-event-ingester is a separate always-on process that holds its code AND its environment from the moment it was started (run-ingester.sh sources .env.local and execs the loop; the stream registry is additionally cached in-process). So a release that changes scripts/ingester/ingest-events.ts, src/lib/portfolio/tracked-contracts.ts or anything they import does nothing until the process restarts. The ledger worker (creddit-ledger-worker) is the same kind of process, with the same launcher shape, and it holds the derivation code from its own start.
Registry rows need no restart in either process. Both re-read the token registry (portfolio_tokens) and the fund registry (money_market_fund_registry) every five minutes while they run, the ingester on its own scan-surface refresh (INGEST_REGISTRY_CACHE_MS) and the worker on the same window (REGISTRY_RELOAD_MS); every other registry they read is re-read from the database where it is used (the escrow candidates through a one-minute cache). So a fund the daily money-market sync lists at 03:50 UTC is declared on the ingester's erc4626 and transfers streams within about five minutes, its history caught up from the coverage floor by the ordinary new-address catch-up (its event_coverage rows exactly as after a restart), and derived by the worker on the same window; an operator's row edit reaches both the same way. A reload that changed anything says so, once:
[registry] reloaded: fund registry (<n> rows; now in coverage: 0x…)
[worker] the token or fund registry changed on reload; a coverage restatement pass starts nowA fund the sync delists, or an operator rejects after it was listed, stays declared: coverage is every fund ever listed (R2), because its holders still hold it. Only a row that leaves the ever-listed set (deleted, or its became_listed_at cleared by hand) stops being declared, at the next reload; its stored events stay. Do not put such a row back later without resetting its event_coverage rows on those two streams: they still read live, so the blocks it spent outside the scan would be certified without ever being read (the same hazard a MetaMorpho factory row that leaves the universe and returns carries). A failed reload keeps what the process last installed, logs [registry] … read failed; keeping what this process last installed, and is tried again five minutes later. The app (onchain-credit) re-reads them on the same five-minute window, in the background: a page request past the window starts the one shared read and renders on the tables it already has, so no request waits on it or adds a read of its own, and the next request after the read lands sees the edit. next build never re-reads: its prerender keeps the one load it always made.
deploy.yml restarts both on every prod deploy, right after the app, the ingester first, and on the rollback path as well (that path rebuilds too), through scripts/ops/restart-ingester.sh. Each process gets the same treatment:
- running or crashed → restarted;
- stopped (
pm2 stop, i.e. a deliberate quiesce such as the 082/083 migration window) → left stopped, with a::warning::on the run, becausepm2 restartwould start it mid-window. The freshness alarm pages hourly until it is started again (the derive-lag arm for the worker), so a quiesce nobody ended cannot go quiet. The script reads the state with colour off and strips any escape codes before matching: pm2 6 and 7 colour the status word even in a pipe, and a colouredstoppedwas not recognised, so the process was restarted; - pm2 cannot report the state (
pm2 describefails for any reason but "doesn't exist") → an::error::naming pm2's message, a red run, and no restart: restarting blind would start a stopped process, and the missing-process remedy (start it) would add a second instance; - pm2 refuses the restart → an
::error::and a red run, because the app is then serving new code over a process that is not; - missing → an
::error::and a red run, for either. The ingester, for the reason above. The worker, because it is the only derivation writer (the contract release: step 1 handed it every writer's derivation, step 2 deleted their inline paths): every page load's refresh, every registration's history and the 6h tick's window are its jobs, so a missing worker means nothing derives.
The grace period is the process definition's, not the script's. pm2 restart waits for the ingester's cycle or the worker's job in flight only as long as that process's own kill_timeout says, and only the documented start command sets it (--kill-timeout 120000 for both); a restart neither adds nor changes it. The freshness alarm reads both back every hour.
This used to be a by-hand runbook step, and it was missed: from 2026-09-03 to 2026-09-11 prod served two releases over a v0.52.2 ingester, which therefore never requested the share-transfer event stETH and eETH movements are booked from. Every rebasing-token movement was missing from the ledger, a new wallet's history was certified complete without them, and its chart booked a 9 ETH swap as return. The restart is automatic now so that cannot recur through a forgotten step, and the freshness alarm pages for every path that bypasses the workflow.
The restart lands BEFORE the release's gated migration, and that is safe. The ingester isolates every stream and the cycle itself behind fail-soft handlers: new code that meets the old schema logs a per-stream (or per-cycle) error and holds that stream's cursor, so nothing is certified over a range it did not write, and the first cycle after the migration picks up where it stopped. A release whose stream set widens (a new event on a stream) wants exactly this order: the ingester already runs the new filter when the gated coverage reset makes it re-scan the history.
One case stalls every stream, not one. The stream set is loaded at the top of each cycle, outside the per-stream handlers, so a release whose registry read needs the new schema fails the WHOLE cycle ([ingester] cycle error (continuing)) every minute until the migration lands, while the freshness alarm reads ok (the process is fresh and online). Nothing is certified in that state, but nothing is collected either. So after every release deploy, read the log: per-stream scanned lines, and no whole-cycle [ingester] cycle error since the restart. If there is one, apply the release's migration now rather than later in the window. Count only lines since the restart, and only the whole-cycle error: a per-stream catch-up cycle error (will retry) is a routine retry, and a plain grep 'cycle error' over the tail counts both, from before the restart too. The --time prefix on each line is in the pm2 daemon's time zone and date uses your shell's, so before trusting a 0, check they agree: the out log gets a line every cycle, so its last prefix and date should be within about a minute of each other (the error log cannot show this: a 0 means its last line is from before the restart).
tail -n 1 /root/.pm2/logs/creddit-event-ingester-out.log | cut -c1-19; date +%Y-%m-%dT%H:%M:%S # same zone?
L=/root/.pm2/logs/creddit-event-ingester-error.log
UP=$(pm2 jlist | node -e 'const p=JSON.parse(require("fs").readFileSync(0)).find(x=>x.name==="creddit-event-ingester");console.log(Math.floor(p.pm2_env.pm_uptime/1000))')
SINCE=$(date -d "@$UP" +%Y-%m-%dT%H:%M:%S)
awk -v since="$SINCE" 'substr($0,1,19) >= since && index($0, "[ingester] cycle error")' "$L" | wc -l # expect 0The restart lets the current cycle finish, and that needs a start-time flag. The ingester's signal handler sets a stop flag and returns; the loop finishes the cycle it is in and exits 0. pm2 only waits kill_timeout for that, and its default is 1.6 seconds, so before the flag below every release SIGKILLed a cycle mid-flight. The process is fail-soft about it — a live scan advances its cursor last, so an interrupted one re-scans its range, and the reorg repair is one transaction — but a cycle that finishes needs neither. kill_timeout belongs to the process definition: pm2 restart does not add it to a process created without one, so an ingester started before this release needs the one-time step once.
Restart by hand only when the workflow did not: after editing a variable in .env.local, after a manual deploy or rebuild on the box, when a quiesce ends (the deploy leaves a stopped process stopped), or when the deploy run says a restart failed. A registry row edit is not a reason: both re-read the registries every five minutes (above).
pm2 restart creddit-event-ingester --update-env # PROD ONLY
pm2 logs creddit-event-ingester --lines 50
pm2 restart creddit-ledger-worker --update-env # PROD ONLY
pm2 logs creddit-ledger-worker --lines 50Verify from the log and the data, not from the restart's exit code: the expected stream names appear, a chain_scan_ranges row lands within two cycles, and new raw_events rows carry a non-empty block_hash.
The ingester alarm: freshness, feed lag, derive lag and the ledger audit
scripts/check-ingester-freshness.ts runs hourly and asks four questions. Any one can page on its own; each prints its findings with its own tag (ingester-freshness, ingester-feed-lag, ledger-derive-lag, ledger-audit) so the page says which.
Arm 1 — freshness. It pages when the ingester started from this checkout is older than the checkout's current build (it started before .next/BUILD_ID was written, more than 15 minutes ago), is not online in pm2, or does not exist. Both deploy paths restart it seconds after their build, so the grace only covers that gap. Every page leads with its remedy, which for all but the missing process is the restart above. A by-hand rebuild of unchanged code pages too: the rule is that every rebuild restarts the ingester.
It also reads pm2's kill_timeout back every hour and prints a non-paging note when it is not the documented 120,000 ms — which is how the one-time step is confirmed from the log rather than over SSH, and how a pm2 resurrect from a stale dump that quietly returns the grace to 1.6s becomes visible. A note rather than a page because nothing is being lost while it is wrong: an interrupted cycle re-scans its range and the reorg repair is a transaction either way. What is wrong is the claim.
It reads the ledger worker's kill_timeout back the same way, from the same process list, and prints a note tagged ledger-derive-lag when an online worker of this checkout does not carry the documented 120,000 ms: every deploy restarts the worker too, and a job cut short by pm2's 1.6s default is requeued at the next start with an attempt spent. The worker needs no one-time step (it is created with the flag), so the note means it was re-created without it; the remedy is to re-create it with the documented command (the ledger worker).
The ingester is placed in a checkout by the script pm2 runs (pm_exec_path), not by the directory it was started from, so a process re-created from /root is still judged. A run from a checkout the ingester does not run (staging's) reports not evaluated and exits 0. A host with no pm2 daemon pages (the check does not call pm2 there, because pm2 jlist would start one), and so does a pm2 read that fails. A stopped ingester pages hourly until it is started, a deliberate quiesce included. The ok line carries pm2's restart count: a crash loop slower than pm2's min_uptime reads online and fresh, and the climbing count is its only sign. The job never prints the process list: pm2 jlist carries every process's environment.
Arm 2 — feed lag. A fresh, online process says nothing about its feeds: every stream is isolated behind its own fail-soft handler, so one can throw its scan away every cycle for days while the other twelve advance and pm2 stays green. The first symptom used to be a wallet stuck in the building state or a chart that stopped advancing. So the check also compares each followed stream's ledger:<stream> cursor against the chain head, reads the gap as elapsed time at 12s per block, and pages when any feed is more than 6 hours behind (INGEST_FEED_LAG_MAX_HOURS), or when a rolled-out stream has no cursor at all — one the derivation may rely on that no cycle has ever scanned. One [fail] line per lagging feed, worst last so it survives the page's eight-line cut, then an aggregate line that says whether it is one feed or all of them. A feed that is behind catches up on its own once its cause clears; the remedy on the page is to read the ingester's log for that stream, not to requeue anything.
The stream set is built from the ingester's own facts, never from a list in the alarm: a stream is watched when it is rolled out (the '*' markers in event_coverage, plus the six required by definition) or when it simply has a cursor, which is what a stream the ingester is following always has. So a stream added to the ledger is watched from the release that starts following it, and a rolled-out stream with no cursor is the "never scanned" finding. The ledger:morpho-impairment cursor is deliberately not in the set: it is a derived writer rather than a stream, and it holds below a refused block on purpose (its own pass prints a HOLD line every cycle instead).
This arm runs only behind a freshness arm that found nothing, and both halves of that matter. There must be an online ingester of this checkout — a feed can only be behind relative to something meant to be following it. And arm 1 must not be paging: the alert keeps the newest eight matching lines, so feed lines printed beside a freshness page push that page's line, the only one carrying pm2 restart, out of the alert entirely. Being online is not enough to rule that out, because an ingester running pre-build code is online and paging — and the two are correlated rather than independent, since an ingester on old code is exactly one that has stopped following a stream the release added. Nothing is lost: the restart clears the freshness page and the next hourly tick measures the feeds. If the ledger or the head cannot be read, that is a [fail] too (exit 1), never a quiet pass.
Its crontab line is a server step.
Run it by hand once BEFORE installing the line, and settle whatever the feed arm says. The hourly schedule has no throttle and no dedupe, so a standing finding is a page an hour until somebody acts — and the feed arm's first tick reports state that has been accumulating since long before it existed. The shape to expect is a rollout marker that outlived its population: wallet-token's scan surface is the enrolled-wallet list, and with that list empty the ingester emits no wallet-token stream at all, so its cursor stops advancing while its '*' coverage row still says the stream is live. The feed lag then grows without bound and no action the page names clears it — nothing is broken, nothing to restart, nothing to requeue, and widening INGEST_FEED_LAG_MAX_HOURS only postpones it. That is a one-time decision (retire the marker, or re-enrol), and it belongs before the schedule starts sending it rather than after. An alarm nobody believes is a slower version of an alarm nobody has.
Arm 3 — derive lag. The ledger worker turns ingested events into the ledger the pages read, and it is a pm2 process whose log no cron greps, so this arm is its pager. It reads the worker's pm2 entry and the ingester's uptime from the list arm 1 already parsed, and the database: the worker's heartbeat (portfolio:worker:heartbeat, stamped every 30 s whatever the worker is doing), the continuous producer's cursor row (portfolio:worker:continuous, stamped by every producer run that completes) and the ingested tip it follows (the PROVISIONAL one, the pending band included, beside the SETTLED one, the pending band), every running job, the oldest queued job (a sweep's apart, an audit left out), the failed jobs, and the 6h tick cursor (portfolio:v2:tick: when a sweep last advanced it) beside the number of tracked wallets. It pages, in this order, when:
- migration
115's queue does not exist (the release's migration was not applied before its deploy); - the worker runs old code: an online
creddit-ledger-workerof this checkout that started before the checkout's build, past arm 1's 15-minute grace — the same rule as the ingester's, for the same reason (it would keep deriving every dirty wallet with the previous release's code, and its merges would delete, over each job's range, rows that code does not produce). Restart it; - the worker has never written a heartbeat (it was never started here: the page carries the start command), or the heartbeat is more than 10 minutes old (it stopped: restart it);
- a job has been
runningfor more than 30 minutes (a tick-range job:continuous,sync,sweep) or 60 minutes (a whole-window job:enrol,rederive) while the heartbeat is fresh: the worker is wedged on it (the heartbeat is a timer, so a hung job keeps it beating). Every running job is judged against its own kind's bound, not the oldest alone: the worker runs two lanes, so a whale's enrolment inside its hour can be the oldest while a sync has hung in the other lane, and the worker's own timer never requeues a job its lanes are still running. Restart it; the job is requeued at the next start, having spent an attempt. A whole-window job is a registration replay's worth of work, and gets the registration drain's own reclaim bound (its child runs up to 30 minutes, reclaimed past 60), so a whale's legitimate job does not page and does not have a restart spend its attempts; - the continuous producer has stalled: its cursor row has not been stamped for more than 60 minutes while the ingester has been up at least that long, or it has never been written (the producer has never completed a run here). The producer runs at the end of every ingester cycle, so this is sixty cycles in a row that failed, behind a fresh worker heartbeat and an empty queue: continuous derivation has stopped (the 6h tick's sweep still derives every wallet, so it is freshness, not correctness). The page names how far behind the ingested tip it stands; read the ingester log for
[ingester/derive-jobs](a statement timeout, a population read that errors), and restart the ingester if its cycle itself is stuck; - the pending band has not settled: the provisional ingested tip stands more than 128 blocks above the settled one (
LEDGER_PENDING_MAX_BLOCKS). The band is ~62 blocks deep in the steady state (the pending scan stops two blocks behind the head, the settled scan 64), so past 128 a stream's settled scan is held while its pending scan goes on: the page serves movements no scan is certifying (a provider outage holds both scans, which the feed-lag arm speaks for). Read the ingester log for that stream's[ingester/<stream>] scan error(a digest mismatch with the verify provider, a commit that fails) and clear its cause; the band settles by itself once the settled scan moves. The ok line states the band's depth every hour (pending band N block(s)); - any job is
failed(a range some wallet's ledger lacks until somebody requeues or deletes it: the page names the oldest; a wallet's continuous failures fold into one standing row, its sweep and its sync failures keep one each too, the newest, and the worker closes any of them by itself once the ledger has caught up over its range, and a failedenrolorrederiveonce the wallet is stamped through its top since, see the ledger worker). Anauditjob no longer parksfailedbecause its in-place re-derivation was refused: it books that leg's residue naming the refusal and pages through arm 4 (fixing it), while the wallet's own continuous and sweep jobs that keep refusing page here; - the newest 6h sweep holds a pageable
partialjob (since the contract release, step 1: the[partial]contract the inline tick kept, carried onto the sweep): a wallet's merge committed over a range it could not read, for a wallet that is served, so its ledger has a hole in that range and the sweep holds the tick cursor. The worker marks such a job's reasonpageable:; a wallet still waiting on its own enrolment is not marked. It pages every hour while the newest sweep holds one, and stops once a later sweep derives the wallet cleanly; read the job'serrorand the worker's log line for the wallet. A set-aside wallet's partial is left out (the sweep has settled past it, so it no longer holds the cursor), but only while the quarantine page has run within the last 7 hours (its run stamp,portfolio:worker:quarantine-page, written by every run): that page names it in words every 6 hours instead. A page that is not running (its crontab line missing or lost) leaves this arm paging it as before, so a quarantined wallet is never silent; - the oldest queued job that is neither a sweep nor an audit is more than 30 minutes old (alive, and not keeping up), or the oldest queued sweep job is more than 6 hours old (one tick's period: the sweep is not draining and the tick window has stopped advancing). An
auditjob's age is not judged: it goes back to the queue, its attempt handed back, for as long as it waits (for its wallet's derivation, for the audit of an earlier reading of the wallet, for the write lock, or past a fence), so its age measures what it waits for rather than the worker's pace, and no wait outlives its six-hour horizon (counted from the audit's first run, so a worker outage does not spend it), past which it endspartialby itself (arm 4, below); - no sweep has completed for more than 12 hours (two tick periods) while the tick has wallets to sweep (since the contract release, step 1): the tick cursor moves only when every job of a sweep is
done, so it stands still behind any job that is not, including apartialone that pages nothing by itself (a wallet waiting on its own enrolment, a range the ingested tip stopped short of), or when the tick enqueued no sweep at all. Every tick after which no sweep completed notes no scanned scope, which vetoes the discovery certificates, the coverage anchors and the empty wallets' read stamps for the whole population (and every sweep re-derives a wider window). Read the newest sweep's jobs that are notdone(SELECT id, wallet, state, left(error, 160) FROM onchain_credit.portfolio_derive_jobs WHERE chain_id = 1 AND kind = 'sweep' AND state <> 'done' ORDER BY created_at DESC) and fix the wallet that holds it. Since the quarantine one wallet'spartialholds the cursor for one sweep (N - 1 sweeps, N beingLEDGER_QUARANTINE_AFTER_SWEEPS, default 2) and is then settled past, so what stalls the cursor for 12 hours is a sweep in which no wallet completed (the population's cause: the ingested tip behind the window, a provider down), afailedjob, or a tick that enqueued nothing.
The full-refresh-age warning has its own readable message, titled Portfolio update check, with WHAT THIS CHECKS, RESULT, WHY YOU GOT THIS, USER IMPACT and NEXT STEP. It states the configured age limit, elapsed time, wallet count and observed warning history. Times in this message use Europe/Copenhagen, including daylight-saving changes. The existing [fail] ledger-derive-lag line and exit code remain the fallback; this is a message change, not a change to the alarm's threshold or the refresher.
The warning can fire after an idle period: no tracked wallets means the scheduled refresher correctly does nothing, but its previous completion timestamp remains. Newly added wallets make the age check applicable again. The message describes wallets awaiting their first scheduled full refresh only when a separate read confirms that all currently tracked accounts were created after the previous scheduled run and that no sweep jobs exist. It does not claim to have read past cron logs. It names completed initial processing only when every current wallet's latest enrol job is done. The context population must also match the freshness snapshot. An older account, existing sweep, changed population or unavailable context produces an explicit unconfirmed-cause message with an instruction to inspect the refresher and worker.
For newly added wallets whose initial processing completed, with no other failing check in that run, NEXT STEP gives the next refresh on the standard 50 */6 * * * UTC schedule. If that scheduled time has already passed, the next look no longer uses the waiting explanation. A concurrent feed or reading failure still requires investigation and is retained in the message. When the ledger reading check also writes a readable alert, its message stays first and the portfolio update warning is appended in a compact form that leads with its result and next step, within Telegram's 4,096-character limit. Feed details collapse to their final aggregate, and space for other failing checks is reserved before trimming the explanation, so a run with many delayed feeds cannot crowd out an audit failure. Long text is explicitly marked as cut. The complete message stays within the 3,000 characters retained by the job's final history-gap step, so that step can add its finding without cutting off another failing check.
The warning's episode is stored separately at <CRON_STATE_FILE>.portfolio-update, so it cannot replace the ledger reading check's memory. A measured fresh completion or empty tracked population clears it. A different arm winning the verdict does not count as recovery. Failed context reads use cautious wording; failed message writes preserve an existing reading alert or leave the wrapper to send its generic fallback. The additional database query is read-only, with a five-second connection and statement timeout.
The bounds are LEDGER_WORKER_HEARTBEAT_MAX_MINUTES, LEDGER_DERIVE_RUNNING_MAX_MINUTES, LEDGER_DERIVE_WHOLE_RUNNING_MAX_MINUTES, LEDGER_PRODUCER_MAX_MINUTES, LEDGER_DERIVE_QUEUE_MAX_MINUTES, LEDGER_DERIVE_SWEEP_MAX_MINUTES, LEDGER_TICK_STALL_MAX_MINUTES (default 720) and LEDGER_PENDING_MAX_BLOCKS (default 128, in blocks). It prints exactly one line, after the feed arm's, and when several things are wrong the line names the one whose remedy clears the most (a restart clears a stale worker, a stopped one and a wedged one; a dead worker explains an old queue) and counts the rest: fourteen feed lines and a derive page still leave the feed aggregate, the worst feed and the derive remedy in the eight the page keeps. It runs behind the same gate as the feed arm — a freshness arm that found nothing and an online ingester of this checkout — because the worker runs from the checkout that runs the ingester: staging, which runs neither, is not judged. An unreadable queue is a [fail] too (exit 1).
Arm 4 — the ledger audit. Every stored reading is checked against the movement records by an audit job in the same worker (the reading audit), and what it books is durable: an adjustment row where a difference was left over after the automatic explanation, and the readings themselves. This arm reads those records since its last look (its cursor, portfolio:worker:audit-arm, starts one hour back on the first run), up to five minutes before it runs, so a correction whose write was still committing when it looked is the next look's rather than lost (PR #961 final review, SF-1), and so is a reading whose 6h commit queued behind the portfolio write lock across a look: both are stamped when written, after the lock (round 2, SF-1). It pages when:
- an audit booked an unexplained correction (
explain_status = unexplained): a holding moved by a quantity nothing on chain accounted for, stated in its asset's own units and in dollars where its book is the dollar (for a correction kept where its reading is gone, from the copy of that reading it carries). The remedy is to explain it (extend the decoder and re-derive the range: the re-derivation queues the re-audit that replaces the correction by the proper record) or to accept its cause in the accepted-cause registry by PR. A correction the audit MOVED from a superseded live tip to the checkpoint that replaced it (a recomposed checkpoint included), with its size unchanged, is not paged a second time once this arm has paged it; one it never saw pages at its new home. One the checkpoint cannot take (it did not read that holding, or the holding moved in between) stays at the tip's block and pages nothing new; the audit removes it once the movement records hold what it stated. A correction booked at a checkpoint the 6h tick committed below a tip already audited replaces the tip's (the audit compares the readings above it again, and the tip's own row where an empty reading retired the tip, on every run, so an interrupted one still does on its retry) and may page once more; it is one correction; - a venue whose unexplained corrections were zero for the trailing 7 days books its first new one (a clean record broken, named apart so it is not buried in a count);
- a leg the movement records hold went unread at two readings in a row (a reader failure, or a closing movement the ledger never recorded: the page says which to suspect when the leg's newest record leaves it open). The audit reads such a leg on its own first, strictly (PR #961 final review, SF-3 and round 2 B1: one call for that leg's own count): where the chain returns zero, the exit is booked as an unexplained correction (above) and the leg is no longer held, so what pages here is a leg that read found held (the reader missed it) or could not read: a revert, a leg outside the registry the worker loaded, a degraded registry load, or a Fluid position, which has no such read yet. The worker's log line for the reading names which (
read directly: held (…)orread directly: unreadable (…)). A smart pool's member (or an escrow claim) worth under a cent when last read does not page when a reading omits it: the Fluid reader drops a member that rounds to nothing, and the log line saysdust the reader drops, not a reader failure; - a reading recorded since the last look is withheld (since the contract release's step 2, PR #1011 review SF-3): its value reads a rate no fact answers (the loader rule), so its row shows no value and its book's total and chart point there are a gap, "partially valued" (issue #981, Fred's ruling 2, M9). Every reading writer keeps every rate its value read since migration
128, so this is a writer's fault: a defect, or a process valuing under a registry older than the database's (restart it). It pages (exit 2) as the arm's own message, per position in words (below); the log keeps its line,[fail] ledger-audit: N withheld reading(s): fix the writer that kept no rate fact; first=<wallet> <leg> @<block>, see docs/data-pipeline.md#loader-rule, which the generic fallback quotes. A reading pages in the look whose window recorded it, and not again.
It values the readings it reads (the dust floor above and the withheld count), so its read installs the database's registry first, through its own connection, as the worker does at start (PR #1011 review, SF-1): under the shipped seed a fund that exists only in the database would read as no value, and its dust leg would page as a reader failure the worker never books. A wallet whose readings it cannot value (the registry read failed, or a price or rate series read did) is stated, never judged under the wrong rules, and the adjustments, which value nothing, are judged whatever the valuation does. That failure pages only when it repeats (issue #981, Fred's ruling 3): LEDGER_AUDIT_SOURCE_PAGE_AFTER looks in a row (default 2, 3 allowed; any other value is the default). A one-off look is one stated line that cannot page and leaves the arm's exit code alone: ledger-audit: could not value readings this look (1 of 2 in a row before it alerts): N of M wallet(s) not valued, streaks and withheld readings unjudged: <error>; first=<wallet>. At the threshold it is [partial] ledger-audit: read the error, it has repeated: N looks in a row, … (exit 1) and the arm's own message. A look with nothing to value (no reading recorded in its window, the registry installed) neither extends nor ends the run, because readings arrive in bursts at the 6h tick; a registry that could not be installed counts as a failed look even then; a look that valued a wallet ends it. The run is counted in the job's state file (/tmp/onchain-credit-cron/<checkout>-check-ingester-freshness.state.json); a state that cannot be written cannot count a repeat, so the failure then pages at once (ledger-audit: state not written (…) says why), and so does a run outside the wrapper, which has no state file. The cursor moves on either way, so the readings of a failed look are not judged later.
An accepted correction never pages; the ok line counts them. Its verdict is one line, after the derive arm's, naming the first finding and counting the rest (the per-finding detail is in the worker's log); the source line (stated or [partial]) and the withheld line, when it has them, come just before it, and the ok line then speaks for the wallets it judged. It runs behind the same gate as arms 2 and 3, and stores its cursor only after its lines are printed, so a look that could not print is repeated rather than skipped. An unreadable audit is a [fail] too (exit 1).
An audit job that ended failed (three attempts that threw: an explanation request the archive endpoint kept refusing, a statement that kept failing) is never closed by the worker — unlike a failed continuous job, no later job derives through it — so arm 3 pages it hourly until it is dealt with. Read its error, fix the cause, and either requeue it (UPDATE … SET state = 'queued', attempts = 0, error = NULL WHERE id = <id>) or, where the next reading's audit has compared the same balances since, close it (SET state = 'partial', finished_at = now(), error = 'closed by hand: superseded by the audit of block <b>'). Requeued, it runs before the audits of the wallet's later readings, which wait for it: a wallet's audits apply in block order. Requeue rather than close one whose wallet's audited-through watermark lies above its to_block (a checkpoint the 6h tick committed below a tip already audited): its comparison of what lies above it is what takes the tip's correction away, and for a wallet that holds nothing no later audit reaches that row until the wallet is re-derived. An audit whose own write, or whose in-place re-derivation, only queued for the write lock, or met a fence, goes back to the queue without spending an attempt, as the derivation itself does, so contention never parks one failed. No audit waits past its six-hour horizon: a deferred one, one waiting behind the audit of an earlier reading of its wallet, and one still meeting the lock or a fence all end partial by themselves then and page nothing (the next reading's audit compares the same balances), so the wallet's later audits stop waiting behind them.
The wrapper sends every line to the logfile, so read it back from there. The file is keyed by checkout, so this is prod's own history; the env= anchor is kept because a line is read far more often out of a page than out of the file it came from:
cd /opt/onchain-credit && ./scripts/run-cron.sh check-ingester-freshness.ts
grep 'env=onchain-credit$' /tmp/onchain-credit-cron/onchain-credit-check-ingester-freshness.log | tail -5Then install it:
5 * * * * /opt/onchain-credit/scripts/run-cron.sh check-ingester-freshness.tsVerify the install two ways: the count proves the line is on this checkout, the log proves it runs. Both the count and the log path name the checkout, so neither can be satisfied by the other environment's ticks.
crontab -l | grep -c '/opt/onchain-credit/scripts/run-cron.sh check-ingester-freshness.ts'
grep 'env=onchain-credit$' /tmp/onchain-credit-cron/onchain-credit-check-ingester-freshness.log | tail -3A healthy run is four lines, one per arm, plus a note for each process whose grace period is not the documented one: the ingester's while it is still pm2's default, which the one-time step clears, and the worker's if it was re-created without the flag (re-create it). Here, with the ingester's note:
ingester-freshness: ok: started 2026-09-11 11:37Z, after the build of 2026-09-11 07:22Z, v0.59.0, 9 restarts env=onchain-credit
ingester-freshness: grace period is unset (pm2 default 1600)ms, not the documented 120000ms, so a restart can cut a cycle short. Take the one-time step: docs/ops/release-steps.md, ingester kill timeout env=onchain-credit
ingester-feed-lag: feeds ok: 3 followed feed(s) within the 6h bound, worst escrow 0h30m behind head 24601206 env=onchain-credit
ledger-derive-lag: ok: heartbeat 0h00m ago, derived through block 24601140; producer 0h01m behind the tip; 0 queued (oldest none), 0 running, 0 failed, 0 partial in 24h env=onchain-credit
ledger-audit: ok: no unexplained adjustment, no leg unread twice env=onchain-creditThe ledger worker
creddit-ledger-worker (scripts/worker/ledger-worker.ts) is the one process that drains the derivation queue: each job derives one wallet's ledger over one block range through the same writer the 6h tick uses (data-pipeline). Since the contract release (step 1 turned the old writers into producers, step 2 deleted their inline paths) it is the only derivation writer besides the offline history builder: the 6h tick, the registration replay, the page load and the minutely drain's re-derivation trigger hand it sweep, enrol, sync and rederive jobs, and nothing else derives. It runs two lanes: the main lane takes every kind in priority order, one job at a time, and a second lane takes the page load's sync jobs, so a Synchronize does not wait behind a whale's whole-window enrolment; no two jobs of one wallet run at once. Its main lane also runs the coverage restatement between claims, a turn at a time: a pass at its first turn and every six hours after, continued while it has wallets left. Like the ingester it is not a cron: an always-on pm2 process with its own loop, started from the prod checkout only. Staging runs no worker, as it runs no ingester: there a page load's sync waits its 20 s and the refresh reports derivation-pending, and a registration's ledger waits in the queue, until a hand-run drains it (below).
Start it (the migration that creates its queue, 115, must be applied first; a worker that meets no queue logs a failed claim every 30 s and the alarm pages for the missing table):
cd /opt/onchain-credit
pm2 start scripts/worker/run-worker.sh --name creddit-ledger-worker --time --kill-timeout 120000
pm2 save # persist across reboots (and the flags above)
pm2 logs creddit-ledger-workerrun-worker.sh sources .env.local and execs the loop, so pm2's stop signal reaches the worker's own handler: it sets a stop flag, lets the job in flight finish and exits 0. The --kill-timeout is what makes that true, exactly as for the ingester: pm2's default of 1.6 s kills a merge mid-flight, and the flag belongs to the process definition, so pm2 restart does not add it to a process created without one. A job cut short anyway (a whale's whole-window enrolment can outlast 120 s) stays running; the next start requeues it, having spent one of its three attempts.
One worker at a time, enforced. Before it claims anything the worker takes a session advisory lock (onchain_credit.ledger_worker) on a connection of its own and holds it for its whole life. A second instance does not drain beside it: a pm2 start logs another ledger worker holds the worker lock; waiting for it and waits; a hand-run refuses and exits 1. If the lock's connection fails (a database restart), the worker finishes the job in flight, claims nothing more and pauses its heartbeat until it has taken the lock again on a new connection — it does not exit, so a database outage cannot use up pm2's restart budget and leave no worker at all. A lock that stays lost pages through the stale heartbeat. Every verdict is also fenced on the claim that produced it, so even a job two processes somehow both ran is recorded only by the claim that owns it.
The deploy restarts it, beside the ingester and in the same way (the deploy restarts both): it holds its code from the moment it started. The token and fund registries it re-reads every five minutes (REGISTRY_RELOAD_MS; at most one read per window, however many jobs ask), and a reload that changed them starts a coverage restatement pass at once, as a restart's first turn would. (The audit's own chain reads also reload the registries every minute, and a leg the loaded registry lacks, or a degraded load, is read as unreadable, never as a closed position: PR #961 final review round 2, B1.) Restart it by hand after an .env.local edit or a manual rebuild on the box, and when a quiesce ends; a registry row edit reaches it within five minutes without one. Until then a worker on the old table is fail-safe rather than wrong (PR #961 final review, N5): a range that moves a token the table it loaded does not know is refused by the wallet-token universe guard, the job ends partial and the wallet's stamp holds, and a later job over that range after the reload derives it (the 6h tick's sweep at the latest). To make it see an edit at once:
pm2 restart creddit-ledger-worker --update-env # PROD ONLY
pm2 logs creddit-ledger-worker --lines 50Stop it with pm2 stop creddit-ledger-worker: it finishes the job in flight in each lane and exits (stopping it, below, says what waits meanwhile), and pm2 save keeps it stopped across a reboot. A stopped worker stays stopped: the deploy leaves it alone with a ::warning::, and the derive-lag arm pages hourly until pm2 restart creddit-ledger-worker --update-env starts it again. The user wipe refuses to run while it is not stopped.
Re-create it when the hourly alarm notes that its grace period is not 120,000 ms (a pm2 resurrect from a stale dump, a pm2 upgrade, a re-create that forgot the flag). The flag belongs to the process definition, so only a new definition carries it, and deleting the worker loses nothing:
cd /opt/onchain-credit
pm2 delete creddit-ledger-worker
pm2 start scripts/worker/run-worker.sh --name creddit-ledger-worker --time --kill-timeout 120000
pm2 saveWhat healthy looks like. One line per job, [worker] job #<id> <kind> <wallet> [<from>,<to>] (derived [<floor>,<to>]) attempt <n> -> <state>, <written> written, …: continuous jobs a few seconds after each ingester cycle that saw a tracked wallet move; a sync job within a second or two of every page load or Synchronize (claimed by the sync lane: its claimed_by ends /sync); one sweep job per tracked wallet after each 6h tick, then the tick cursor's own line when the whole sweep is done; an enrol per registration and a rederive whenever the drain's trigger matches a wallet; and silence otherwise. The derived range is where the job actually started: its wallet's tick floor (the tick cursor + 1, or the wallet's pending marker when that is lower), so it reaches back up to one 6h tick window behind the job's own range — that is by design (a job anchors on the ledger below where it starts). An occasional -> queued (attempt not counted: another writer moved its wallet's ledger while it derived, so it merged nothing) is healthy too: a whole-window merge (a registration's ledger half, the re-derivation trigger, a history build) or the 6h tick rewrote that wallet while the job derived, so the job merged nothing and runs again a minute later on the new ledger (the stale-merge fence). So is an audit job's -> queued (attempt not counted: the audit waited or was fenced, and its reason says which), while it lasts: the reason after the colon says whether it waits for its wallet's derivation (deferred:), for the audit of an earlier reading of the wallet (waiting:), or for the write lock, or whether another writer moved the ledger under its write (fenced:); none spends an attempt, and none outlives the audit's six-hour horizon (the ledger audit arm). The heartbeat row advances every 30 s, and the queue is short:
-- the worker is alive: the heartbeat is seconds old
SELECT last_scanned_block, now() - updated_at AS age FROM onchain_credit.chain_scan_cursors
WHERE chain_id = 1 AND scope = 'portfolio:worker:heartbeat';
-- the queue by kind and state
SELECT kind, state, count(*), min(created_at) FROM onchain_credit.portfolio_derive_jobs
WHERE chain_id = 1 GROUP BY 1, 2 ORDER BY 1, 2;A failed job is a wallet and a range the worker tried three times and could not derive, and the hourly derive-lag arm pages while it stands. Its reason is on the row and on the [fail] ledger-worker: line in the log. (A job that only queued for the write lock past its budget is not a failure, and neither is one whose merge the stale-merge fence stopped because another writer moved its wallet's ledger while it derived: each goes back to the queue without spending an attempt, and a queue that stays busy, or a wallet whose ledger keeps moving under every attempt, pages as an old queued job instead. A history build rewrites the wallets it builds, so their jobs can be fenced while it runs; the worker does not need stopping for one.) A wallet's failed continuous jobs fold into ONE standing row: a later failure is folded into it. (The one exception: a job the worker parks at its own start, having found it running with its attempts already spent, is parked without the fold, so it can stand beside the wallet's row; the close below takes both.) A wallet's failed sweep jobs keep one standing row the other way round: the newest failure supersedes the older ones it covers ([worker] N older failed sweep job(s) of <wallet> superseded by job #…), because it holds its own sweep, and with it the tick cursor. A wallet's failed sync jobs (a page load during an outage longer than a sync's three attempts, or one per page load on a deterministic refusal) keep one standing row the same way (… older failed sync job(s) …). Each closes by itself once the ledger has caught up over its range — the tick cursor passes its top, or a later continuous or sweep job for the wallet finishes done through it, or, for a sync, a later sync of the wallet finishes done over a range that contains it — with a [worker] closed … failed … job(s) line; the row turns partial and keeps what it failed on in error. The ingester keeps enqueueing the wallet meanwhile, and every 6h tick enqueues it in its sweep, so a fault that has cleared stops paging at the wallet's next movement, its next page load or the next tick. One that PERSISTS keeps paging: fix the cause (an archive provider, a registry row). A failed enrol or rederive closes too, once the wallet's history has been derived and stamped through its top after it failed (the re-derivation trigger's rederive after a failed enrolment, say): [worker] closed 1 failed enrol job(s) whose wallet a later derivation has stamped through their top. A stamp from before the failure closes nothing, so a hand-run rederive of a stamped wallet that fails keeps paging. A failed sweep job holds the tick cursor for the whole population until a later sweep derives the wallet cleanly (every sweep and every continuous job re-derives from the held cursor, so the window widens every 6h), and so does a partial one, until the wallet is quarantined: a wallet whose sweep job has ended partial N sweeps in a row is settled past, so the cursor moves for everyone else (the quarantine). A failed job the worker cannot close (an audit, or a whole-window job no later stamp covers) stays until a person acts: requeue it once its cause is fixed, and delete it only when the range no longer matters (the wallet was removed, or a later tick has derived it):
SELECT id, kind, wallet, from_block, to_block, attempts, error, finished_at
FROM onchain_credit.portfolio_derive_jobs WHERE chain_id = 1 AND state = 'failed' ORDER BY finished_at;
UPDATE onchain_credit.portfolio_derive_jobs
SET state = 'queued', attempts = 0, claimed_at = NULL, finished_at = NULL, error = NULL
WHERE chain_id = 1 AND id = <id> AND state = 'failed';Enqueueing by hand. An operator may insert two kinds, and the table fixes each kind's priority:
continuous: one wallet over a range, with the settle line-1if in doubt (nothing it writes is labelled settled that the next pass does not re-derive). It starts at its wallet's tick floor like any other (or at its ownfrom_block, when that is lower): a range above the tick cursor cannot be derived alone, because its running balances are anchored on the ledger below it.rederive: one wallet's WHOLE window, re-derived and re-certified (no settle line). It runs the re-derivation trigger's three launch guards, so it can certify nothing under conditions the trigger would not (data-pipeline). A wallet whose registration replay is parked or still pending (portfolio_backfill_state.statuserror,queuedorrunning) is held: the job endspartialhaving derived nothing, and the wallet stays excluded until that replay completes (re-queue a parked one; its own ledger half derives and stamps the wallet). The job opens at the wallet's own history floor and certifies from there, sofrom_blockmust be that floor: a job naming any other block derives nothing and endspartialwith the floor in its reason. And it never stamps below the recorded arm block (portfolio:v2-campaign:arm-block), where a derive cursor reads as a short build for good and the trigger stops coming back for the wallet: while no arm block is recorded, or the ingested tip is still below it, the job is held the same way (the trigger declines then too, and derives a cursor-less wallet by itself once the tip passes the arm; enqueue anything else again then), and ato_blockbelow the arm derives nothing — so<to>is the chain head: any explorer's latest block, or on the box the newest block the ingester has scanned,SELECT max(last_scanned_block) FROM onchain_credit.chain_scan_cursors WHERE chain_id = 1 AND scope LIKE 'ledger:%'(the writer stops at the ingested tip either way). The INSERT below reads the floor exactly as the worker does (no stored pair is the indexed floor, 24136053; a stored one is never below it). This is also how the comparator re-derives a wallet (plan §7.3). A sub-range is acontinuousjob's.
What a re-derive does not move. A re-derive rewrites what the derivation STORES: each movement's kind, link group, quantities and consideration. The two money values of every row and everything built from them on the read (both return lines, a position's entry basis and Dislocation PnL, M32) are computed when the page is read, from the stored rows and the series. So a release that changes only how those are computed (a valuation rule, the entry-basis rule) is live on every wallet the moment it deploys, and queues no re-derive; one that changes what a writer emits (a new kind, a regrouped pair, a corrected quantity) is the case for enqueueing.
Never insert enrol, sync or sweep by hand: they are the registration replay's (after its own gate), the page load's and the 6h tick's, and each, run as its kind, trusts its producer for a guard a typed row does not carry (a sync the page load's floor, an enrol the replay's gate, a sweep the tick's whole population in one statement, whose last done moves the tick cursor for everyone). The table refuses one typed by hand: a queued or running row of those kinds must carry its producer's stamp in producer (migration 122), which the templates below never name, so the INSERT fails on pdj_producer_chk. To derive one wallet's range, insert a continuous; its whole window, a rederive.
-- one wallet over a range
INSERT INTO onchain_credit.portfolio_derive_jobs (chain_id, wallet, from_block, to_block, priority, kind, settle_line)
VALUES (1, '<lower-cased wallet>', <from>, <to>, 20, 'continuous', -1);
-- one wallet's whole window: from its own history floor through <to> (the writer stops at the
-- ingested tip, so the chain head is the right <to>; a <to> below the arm block derives nothing)
INSERT INTO onchain_credit.portfolio_derive_jobs (chain_id, wallet, from_block, to_block, priority, kind)
SELECT 1, a.uid,
CASE WHEN a.history_floor_ts IS NULL OR a.history_floor_block IS NULL THEN 24136053
ELSE GREATEST(a.history_floor_block, 24136053) END,
<to>, 30, 'rederive'
FROM onchain_credit.accounts a
WHERE a.uid = '<lower-cased wallet>';A bounded hand-run (a check after a manual step, never a replacement for the process): LEDGER_WORKER_EXIT_WHEN_IDLE=1 drains what is claimable in ONE lane (syncs first, by priority) and exits. It refuses while the pm2 worker runs, so stop that first and start it again after:
pm2 stop creddit-ledger-worker
cd /opt/onchain-credit && LEDGER_WORKER_EXIT_WHEN_IDLE=1 bash scripts/worker/run-worker.sh
pm2 start creddit-ledger-workerStopping it loses nothing, and derives nothing meanwhile. Since the contract release the worker is the only derivation writer, so while it is stopped every producer keeps ENQUEUEING and nothing derives: a page load's sync waits its 20 s and the refresh reports derivation-pending (the page serves the last derived state), a registration's enrol waits in the queue (the wallet shows its building state), the 6h tick enqueues its sweep (and the next tick, finding it open, enqueues none), and the ingester's continuous jobs pile up one per wallet (the producer widens a wallet's queued job rather than adding another; a queued sync is widened the same way). The derive-lag arm pages hourly until the worker is back. The backlog is harmless when it returns, however long it waited: each tick-range job starts at its wallet's floor as it stands when the job RUNS, so it anchors on the ledger as it then stands; it reads the pending markers and leaves them to the sweep; and it cannot relabel a row a sweep settled. Stop it only for as long as a step needs.
The health check, after a deploy and whenever the queue looks wrong. Four numbers, each from what the pieces already log (the contract release, step 1 reads them on the worker's first day as the only writer):
-- 1. the page load's waits: how long a page's `sync` job took from its enqueue to its verdict. A
-- page waits at most 20 s (less, when its venue reads came first), and one that got no verdict
-- in time reported `derivation-pending` (its reading was not stored; the job's rows landed for
-- the next refresh). A handful over the wait a day is the expected order; a count that climbs
-- means the sync lane is not keeping up, or the worker was down.
SELECT count(*) AS syncs,
count(*) FILTER (WHERE finished_at - created_at > interval '20 seconds') AS over_the_wait,
percentile_cont(0.5) WITHIN GROUP (ORDER BY extract(epoch FROM finished_at - created_at)) AS median_s
FROM onchain_credit.portfolio_derive_jobs
WHERE chain_id = 1 AND kind = 'sync' AND finished_at > now() - interval '1 day';# 2. the worker's verdicts: mostly `-> done`; no `-> failed`. A `-> queued` is a retry; the ones the
# stale-merge fence made say `another writer moved its wallet's ledger` (count them separately)
pm2 logs creddit-ledger-worker --lines 2000 --nostream | grep -oE -- '-> (done|partial|queued|failed)' | sort | uniq -c
pm2 logs creddit-ledger-worker --lines 2000 --nostream | grep -c "another writer moved its wallet's ledger"
# 3. the continuous producer: a `[ingester/derive-jobs] … continuous job(s)` line on the cycles that
# saw a tracked wallet move, and no `continuous enqueue error` (a run over its 20 s budget, say)
pm2 logs creddit-event-ingester --lines 2000 --nostream | grep -c 'continuous enqueue error'
# 4. the queue: one open continuous job and one queued sync per wallet at most, nothing failed that
# is not closing, and each 6h sweep done within minutes of its tickSELECT kind, state, count(*), count(DISTINCT wallet) AS wallets, min(created_at)
FROM onchain_credit.portfolio_derive_jobs WHERE chain_id = 1 GROUP BY 1, 2 ORDER BY 1, 2;(Before the contract release the page load merged inline and only TRIED the write lock the worker held, so number 1 was the skip count, grep -c 'writer lock busy; v2 persist skipped' /root/.pm2/logs/onchain-credit-out.log. Its sync job now queues for the lock, so a skip no longer happens.)
The coverage restatement, on demand
When an asset comes into coverage (a registry row gains a book), the readings of it stored before keep the book they were written in, EXCLUDED, until the ledger worker restates them: a pass at its first turn after a start (every deploy restarts it on the registry the release carries), after every registry reload that changed something (a row edit, within five minutes, with no restart) and every six hours after, restating a few wallets per turn (twenty seconds, or PORTFOLIO_V2_REDERIVE_MAX re-derivations, whichever comes first; a pass with wallets left continues a minute later, or at once on an idle queue) into the asset's book and queueing each such wallet's rederive. A widening held by N wallets therefore takes about N/120 hours on a busy worker. An asset that leaves coverage restates nothing and is reported at every pass. What it does, what it holds back and what it leaves alone: the data pipeline. Its lines in the worker's log:
[restate] <wallet> <ticker> (<asset>): <n> reading(s) restated EXCLUDED -> USD, <first> -> <last>
[restate] <wallet>: rederive job #<id> queued over [<floor>,<tip>] (its readings were restated)
[restate] <ticker> (<asset>): <n> reading(s) of <m> wallet(s) restated EXCLUDED -> USD, <first> -> <last>; held back, left EXCLUDED: <k> <reason>; …
[restate] NOT restated (coverage narrowed): <ticker> (<asset>): <n> reading(s) of <m> wallet(s) stored in USD, which the registry now books EXCLUDED; …
[restate] held back <ticker> (<asset>): … this process's token registry books it EXCLUDED; the ledger worker loads the database's registry at its next five-minute reload …
[restate] held back the Fluid smart pool <pool> (<token>/<token>): <n> reading(s) of <m> wallet(s) left EXCLUDED (-> USD): a Fluid smart side states a rate per pool token inside its composition, …
[restate] done: … | nothing to restate: every stored reading agrees with the registry's book (<ms>ms)
[fail] ledger-worker: the coverage restatement failed (<k> in a row; retried …): <reason>A NOT restated line is a decision for an operator, never a fault: the history stays as written. A held back … reload line means the registry was edited after the worker's last reload; the pass the next reload starts (within five minutes) restates it, with no restart. (An ether-book widening is restated like a dollar one since the contract release, step 2; until then it was held back whole.) A held back the Fluid smart pool … line is a smart side whose pool tokens came into one book: it stays as written by design (its rates live per pool token in its composition), and a smart pool on a NOT restated line is one whose tokens no longer share the book it was written in. held back, left EXCLUDED: <n> <reason> on an asset's line names readings whose value in the new book cannot be computed (no rate at their block, most often: for an asset whose value reads a second rate, apyUSD over apxUSD's published value for one, either rate missing there; the pass states every rate a reading's value reads, and holds a reading rather than state it without one), or wallets whose re-derivation would not run (a parked registration replay), which every pass asks about again. A [fail] line repeats until its cause is fixed. None of this pages (no cron reads the worker's log), so after a coverage release look at the reconciler's value line, which is reported, never paged: its book-overlays reads 0 for the asset's wallets once the pass and their re-derivations are done (a figure that stays above 0 is a held reading or a failing turn: the worker's [restate] lines say which), and its factless= names a wallet whose only record of the asset is a movement, which no reading restates (re-derive it by hand: the ledger worker, enqueueing a rederive from its history floor).
To see it, or run it now, with scripts/ops/restate-covered-readings.ts from the checkout of the database's environment, its .env.local sourced (the plan reads the chain where a restated reading needs a rate, through the archive endpoint). It runs one whole pass and prints every wallet's line, held readings included:
cd /opt/onchain-credit && set -a && . ./.env.local && set +a
npx tsx scripts/ops/restate-covered-readings.ts # PLAN (read-only): what it would restate, hold back and report
npx tsx scripts/ops/restate-covered-readings.ts --execute # restate now, and queue the re-derivations--execute is safe beside the running worker (every row changes only where it still says EXCLUDED, and a wallet with an open whole-window job gets no second one); a second run restates nothing. It queues every wallet's re-derivation at once (the worker paces its own), so on prod leave a wide widening to the worker. The re-derivations it queues run when the worker runs them, under the registry the WORKER last loaded: after a registry edit, wait five minutes for the worker's reload (its [registry] reloaded line) before running this, or the movements are derived again under the old registry. The worker's own passes need no such care: it holds an asset back until its reload has the edit, and the reload starts the pass that restates it. On staging, which runs no worker, a bounded hand-run from the staging checkout does both, restating at its first turn, continuing the pass while it has wallets left, and draining the rederive jobs it queued before it exits: cd /opt/onchain-credit-staging && LEDGER_WORKER_EXIT_WHEN_IDLE=1 bash scripts/worker/run-worker.sh (the plan above, run first from that checkout, shows what it will do).
The escrow registry check
The escrow stream scans a set of queue and manager addresses pinned in code. They can move — a pool manager is upgradeable, a queue can be redeployed — and if one does, the stream keeps scanning an address that emits nothing while the certificate for those blocks still says they were covered. Re-read them from the chain before a release that touches the registry, and after any Ethena, Lido, EtherFi, Maple or f(x) upgrade:
cd /opt/onchain-credit && ETHEREUM_RPC_URL="$(grep ^ETHEREUM_RPC_URL= .env.local | cut -d= -f2-)" \
npm run verify:escrow-registryExit 0 is clean; 1 is drift (a pinned address, or an Ethena cooldown duration, which decides whether the wrapper escrows at all, disagrees with the chain — fix the registry in the same release, and re-check whether the class still escrows at all); 2 means the check could not run (no readable endpoint), which is not the same thing and must not be read as a pass. It is one multicall round, so it costs one request whatever the registry grows to. One value is report-only: f(x)'s cooldown length (redeemCoolDownPeriod). The exit escrows at any cooldown and no figure reads the length, so a governance change to it prints as moved with the chain's value, adds a reported, not held line, and still exits 0; re-pin the value in the next release that touches the registry.
The ingester runs the same check itself, before it scans the escrow stream, and holds that stream's cursor on a disagreement it can read — so a drift that slips past this step stops the escrow stream rather than corrupting its certificates, visible as a repeating [ingester/escrow] registry drift line. The report-only value never holds it: a moved f(x) cooldown rides on the pass's own line (… · reported, not held: …) and the scan proceeds, because the escrow cursor is one term of the ingested tip every wallet's ledger waits for. A clean pass is cached for the life of the process; an unreadable one is retried on a 10-minute interval rather than every cycle, so a provider having a bad ten minutes cannot slow the loop's cadence for every other stream.
The quarantine: one wallet never holds the tick cursor
The rule (plan S8c; issue #981, Fred's ruling of 2026-10-04). A wallet whose 6h sweep job ends partial at N consecutive sweeps (LEDGER_QUARANTINE_AFTER_SWEEPS in .env.local, default 2; a value that is not a whole number of at least 1 reads as the default) is quarantined: the worker settles the sweep without it ([worker] sweep [A,B] finished with K of M job(s) partial, every one a quarantined wallet's (…): the tick cursor advances without them, and each keeps its own pending marker). The first partial sweep still holds the cursor, as every sweep did before, so a one-off blip costs one tick and nothing else; the second in a row is a cause that outlived a tick. Two is also the most that keeps the hold off the population's own records (a tick after which no sweep completed vetoes the coverage anchors, and two such ticks rebuild an all-closed wallet at its owner's next visit). A sweep is NOT settled past a failed job (it may not have recorded what it owes), a quarantined wallet with no pending marker recorded, or when no job of the sweep is done (then the cause is the population's, and the tick-stall finding above pages it). A failed sweep job the worker later closed (partial, its reason starting closed:) ends a run like a failure and never lengthens it.
Set aside, one predicate. Every reader after the settlement (the page below, the hourly arm, the owner's page, the tick's certificate veto) asks the same question: a run of at least N partial sweeps, a job in the newest sweep (the tick still sweeps it; a removed wallet stops at once), and its pending marker at or below the tick cursor (the settlement did move past it). So a held sweep, whether one wallet beside a failed one or a whole population short together, is never described as set aside, and a wallet that merely has a marker below the cursor (a page load's or a continuous job's partial after its own sweep job ran) is not quarantined: its served numbers and its certificates do not move.
Nothing is lost. The wallet keeps its own pending marker, the lowest block it owes (portfolio:derive:v2:1:<wallet>:jit-pending); the raw events are ingested whatever the derivation does. Its next sweep job absorbs the marker, so the first sweep after the cause is fixed derives from the marker to the sweep's top in one job, clears the marker, queues the audit of every reading stored over that stretch again (they waited for, or gave up on, a ledger that did not hold it), and its done is the rejoin: the wallet is no longer quarantined. To bring the ledger up to date sooner, queue a whole-window rederive for it (enqueueing by hand); it derives and re-audits that window. A completed rederive clears the pending marker when its actual committed merge range contains the marker and reaches the current tick cursor. Readers then stop treating the wallet as set aside immediately, without advancing the population's tick or rewriting the historical partial-sweep run. A shorter merge or a marker outside the actual merge range keeps the marker and the protection over the unrepaired stretch. The next clean sweep ends the historical run; any re-derived overlap remains idempotent. While it is quarantined the wallet's continuous jobs start at its marker too (they always start at the lowest of the tick floor and the marker), the "Updated" stamp and the reading audit count the tick cursor for it only below its marker, and the tick vetoes its certificates for every pass it did not make.
What its owner sees. Its page states every return up to its last reading before the stretch it owes as measured, and every one after it as not measured: the chart's two return lines stop there, yield earned and the realized rates are dashes, and the "Updated" time holds where its movements are derived to. Its holdings and their values are still shown, and it is not sent to the building state (portfolio). The quarantine state lives in the queue's own rows (the wallet's newest sweep jobs) and in its marker; there is no table or flag to clear.
Which wallets, now:
SELECT wallet, state, created_at, left(error, 160) FROM onchain_credit.portfolio_derive_jobs
WHERE chain_id = 1 AND kind = 'sweep' AND state IN ('done','partial','failed')
ORDER BY wallet, created_at DESC; -- a wallet whose newest N rows are all `partial` is quarantinedWhat the hourly reading check's page says
The ledger-audit arm's three findings (an unvalued reading, a correction booked because its re-derivation was refused, and its own valuation failing in a row) page as the arm's own Telegram message in the format the ledger self-check's set for every alarm, written to the wrapper's CRON_ALERT_BODY_FILE and sent instead of the generic one. Everything in it is this run's: the look's findings, and every other failing line of the same run as it was logged (under ALSO FAILING IN THIS RUN: the other arms', and this arm's budget line for an unexplained correction or an unread leg), so one message still carries the whole run. A full-refresh-age warning adds the readable Portfolio update check described above. Other arms and the budgets (an unexplained correction, an unread leg) still use the generic message when neither readable check writes one.
⚠️ creddit alert: Ledger reading check (production)
Mon 5 Oct 2026, 10:05 UTC · runs every hour · first failure
WHAT THIS CHECKS
Every hour the app values each reading recorded since its last look, the way the portfolio page
values it. A reading it cannot value has no figure on any page, so its book's total and its chart
point there are not stated (partially valued). If the check itself cannot value readings ...
RESULT
3 readings recorded since the last look could not be valued, on 2 positions of 1 wallet. 3 of 3
wallets with new readings checked.
WALLET 0x… (full address)
3 unvalued readings · open since Mon 5 Oct 2026, 04:05 UTC (2 looks)
• wstETH (Aave v3, 12.5 wstETH): the reading kept no record of a rate its value needs, so no page
can value it, newest at block 24,503,600 · open since Mon 5 Oct 2026, 04:05 UTC (2 looks).
→ User sees: The position's value is a dash, and its book's total there is not stated: a gap in
the chart at that reading (partially valued) and, while it is the newest reading, a dash on the
book's card. Never $0 or the rest of the book. The All view leaves the holding out of its total
and says so.
• sUSDe (Wallet, 1,250 sUSDe): ... · new this look.
→ User sees: as above.
Suggested fix: Find the writer that recorded it without its rate facts: a defect, or a process
valuing under an older registry than the database's (restart it). ...
VALUATION SOURCE (only when its failure has repeated)
Could not value readings for 2 looks in a row · open since Mon 5 Oct 2026, 04:05 UTC
• The hourly check's own valuation: a price or rate read failed (canceling statement due to
statement timeout). 2 of 3 wallets with new readings not valued: 0x7a16…5428, 0x0000…0d17.
→ User sees: These readings were not checked for unvalued readings or for positions a reading
missed, and they are not re-checked later. If the cause is the database ...
Suggested fix: Read the reason in the job's log, then check the database (connections, statement
timeouts) and the registry, price and rate tables it names.
RESOLVED SINCE LAST RUN: none
Log: /tmp/onchain-credit-cron/onchain-credit-check-ingester-freshness.logA correction booked because its re-derivation was refused (the data pipeline) leads the message, one block per wallet:
RESULT
1 correction booked since the last look because re-deriving the position was refused, on 1 wallet. ...
WALLET 0x… (full address)
1 correction booked because a re-derivation was refused · open since Mon 21 Sep 2026, 06:00 UTC (the reading it is booked at)
• PT-reUSD-10DEC2026 (Pendle, +151690 PT-reUSD-10DEC2026 (~$148000)), at the reading at block 26,029,316: the chain
shows the change, but re-deriving the position's history was refused (over [26022171, 26226398] derived a NEGATIVE
balance on 1 leg(s) whose floor is zero ... reaches -50784501362 at block 26173571 (tx 0xd5a9… log 532)).
→ User sees: The PT-reUSD-10DEC2026 row's PnL and both PnL lines (MTM PnL, Accrual only PnL) leave this change out as
not measured over the stretch ending at that reading, so it is never shown as earned; its value is unaffected.
While the derivation keeps refusing, the wallet's later readings cannot be checked: about six hours after the first
of them, /portfolio withholds the wallet's MTM PnL and Accrual only PnL from that reading on, with a note on the
Historical PnL card.
Suggested fix: Fix the derivation the refusal names (the transaction it points at is the movement it cannot book), then
re-derive the wallet from its history floor (docs/deployment.md#ledger-audit). The audit that follows the
re-derivation removes this correction, and the wallet's figures are shown again once its readings are checked.It pages in the look that sees the correction (an edge, like every unexplained correction), and the arm's own line names it first with the derivation's remedy: [fail] ledger-audit: fix the refused re-derivation, then re-derive the wallet: … re-derivation refused …. Where the re-derivation did not refuse but failed on an outage on the audit's last attempt, the remedy is re-derive the wallet once the outage has cleared, and the message's suggested fix says the same. The derivation's own refusal keeps paging separately through the derive-lag arm (the wallet's continuous and sweep jobs refuse the same range) until it is fixed.
Fixing a refused re-derivation
The refusal names the leg, the block and the transaction the derivation could not book. Fix the derivation for that shape, then queue a rederive of the wallet from its history floor (a re-run of only the range anchors on the same wrong number), right after the fix deploys: any continuous job of the wallet (it starts at the wallet's pending marker) now derives the held-back movement too, and until the re-audit runs the ledger holds both the movement and the correction, so the next audited reading would book an offsetting correction of its own and page again. The re-audit the re-derivation queues removes the correction where the movement is now derived, and once a later audit compares the wallet's newest reading the wallet is checked again and /portfolio shows its figures. Which wallets are unchecked, now:
SELECT j.wallet, j.id, j.state, j.from_block, j.to_block, c.last_scanned_block AS audited_through, left(j.error, 120)
FROM onchain_credit.portfolio_derive_jobs j
LEFT JOIN onchain_credit.chain_scan_cursors c
ON c.chain_id = j.chain_id AND c.scope = 'portfolio:derive:v2:1:' || j.wallet || ':audited-through'
WHERE j.chain_id = 1 AND j.kind = 'audit'
AND (j.state = 'failed' OR (j.state = 'partial' AND j.error LIKE 'not audited:%'))
AND (c.last_scanned_block IS NULL OR j.to_block > c.last_scanned_block)
ORDER BY j.wallet, j.from_block;- A position is named as the portfolio's Holdings panel names it (the registry's symbol, read over the arm's own connection), with its venue and its size at the reading in its own units: its value is exactly what could not be computed. A name that cannot be read falls back to the position's key, never to a lost message.
- Open since comes from the job's state file: when the position was first named and in how many looks. A finding is resolved on evidence, when a later look values that position's readings whole, and listed once under RESOLVED SINCE LAST RUN; one not named again for a week is forgotten without a resolved line. The source failure's recovery (a look that valued a wallet after a run that paged) is listed the same way. Only what resolved in a run that sends this message is listed: a run that sends nothing is the all-clear and clears the list, as the 6h self-check's passing run does, so the list never carries a recovery from runs ago.
- The header's streak is the run of messages ("first failure", "failing for N runs since …"), except when the valuation source pages: that arrives at its threshold's look, so the header states its run ("failing for 2 looks since …"), as its block does.
- The state outlives a passing run, unlike the 6h self-check's: a one-off failed look must be remembered by the next one. A run that sends nothing keeps the run of failed looks and the open findings' ages, and ends the paging streak and the RESOLVED list; a state with nothing in it is deleted. The file sits beside the log in
/tmp, so a reboot clears it: the next failed look then reads as the first, which delays a source page by one look at most. - The 4,096-character limit is met by naming fewer positions per wallet, then fewer wallets (the rest by short address), then a hard cut; the source finding is never the part cut.
- A message that cannot be written costs the readable text, never the page: the wrapper sends its generic message, which the arm's log lines still feed.
- Known limits. The readings of a failed look are not re-checked by a later one (the arm's cursor moves on, as before this page existed): the stated line says so, the page itself values every reading at read, and a writer's defect recurs in the next window. A failure to reach the database at all (the arm cannot read the adjustments either) is not this threshold's: it pages at once as
[fail] ledger-audit: fatal could not read the ledger audit: …, beside the other arms' pages for the same outage.
The quarantine page
scripts/check-ledger-quarantine.ts runs every 6 hours, 30 minutes after the portfolio tick (20 1,7,13,19 * * *), and exits 2 while any wallet is quarantined, with one message in the format the ledger self-check's set for every alarm: per wallet, the positions its sweep could not read (the name the Holdings panel gives it, the venue and the newest market value), the cause in words, how long it has been open, what its owner sees and the fix. Nothing else pages a quarantined wallet: the derive-lag arm leaves its partial to this job, and the tick-stall finding cannot see a cursor that moves. A passing run sends nothing and deletes the job's state; a run that cannot read the queue exits 1 with a [fail] ledger-quarantine: fatal … line and the wrapper's generic page goes out. Every run that read the queue stamps portfolio:worker:quarantine-page (its block is how many wallets it found set aside), which is how the hourly arm knows this page is running. Every finding is also one [fail] ledger-quarantine: … line in the log, so the generic fallback names it too.
⚠️ creddit alert: Ledger quarantine (production)
Sun 4 Oct 2026, 19:20 UTC · runs every 6 h · first failure
WHAT THIS CHECKS
Every six hours the app brings each tracked wallet's movement history up to date in one pass. A
wallet whose update falls short twice in a row is set aside so the other wallets keep moving. ...
RESULT
1 of 12 tracked wallets set aside.
WALLET 0x… (full address)
Set aside after 3 short updates in a row · open since Sun 4 Oct 2026, 06:50 UTC
• USDC supply (Aave v3, $5.12k): a movement could not be read by its venue's reader.
→ User sees: Its Updated time stays at Sun 4 Oct 2026, 00:49 UTC, and its returns and accruals
after that read as not measured (a gap in the chart, dashes for Lifetime accruals, and the
realized rates withheld); ...
Suggested fix: Read this wallet's lines in the ledger worker's log and fix the venue reader that
failed. Once fixed it catches up by itself at the next 6-hour update, ...
RESOLVED SINCE LAST RUN: none
Log: /tmp/onchain-credit-cron/onchain-credit-check-ledger-quarantine.log- Open since is the queue's own fact: when the first of the short sweeps in a row was enqueued, and how many in a row. It survives a reboot that clears the state file.
- The cause comes from the newest short sweep's reason: the event ingester behind the range, the wallet's transfer history not certified, a venue reader that threw (
adapter-error), a market the registry does not know, a position its history holds that could not be read again, a recorded exit that could not be derived again, or any other code (named as it is, a code defect). A code that does not stop a merge short (an unpriced receipt'smark-unresolved) is not worded beside one that does. The positions are the ones the sweep's reason names (; positions: …, three at most, then a count); a reason that names none gets one bullet with the cause alone. - State and resolved are the wrapper's job-owned files, as for the ledger self-check:
/tmp/onchain-credit-cron/<checkout>-check-ledger-quarantine.state.jsondates the run of failing runs, and a wallet that rejoined is listed once under RESOLVED SINCE LAST RUN and dropped. - The 4,096-character limit is met by naming fewer positions per wallet, then fewer wallets (the rest by short address), then a hard cut.
Its crontab line is a server step, like the self-check's: no deploy installs it and no rollback removes it. The line, and its two-way check (the count names the checkout; the next run's ledger-quarantine: line proves it runs):
20 1,7,13,19 * * * /opt/onchain-credit/scripts/run-cron.sh check-ledger-quarantine.ts
crontab -l | grep -c '/opt/onchain-credit/scripts/run-cron.sh check-ledger-quarantine.ts' # 1
grep -a 'ledger-quarantine' /tmp/onchain-credit-cron/onchain-credit-check-ledger-quarantine.log | tail -n 2The ledger's writers
The ledger had one offline builder and three live writers, and the writers used to be off by default — turned on before the build, so the two overlapped rather than leaving a gap between the block the build stopped at and the block the writers started from.
None of that is a choice any more. Three environment variables governed it and all three are gone: PORTFOLIO_LEDGER_WRITE (whose default was write nothing), PORTFOLIO_LEDGER_MODE (which chose between the old chain scan and the ledger), and PORTFOLIO_LEDGER_SOURCE (which chose which ledger the reader served). There is one ledger, one writer set and one reader. Delete all three lines from .env.local when you next edit it; they are inert either way, and the runbook for the release that retired the last of them is the release-steps log.
Arming was an edit plus a restart — no rebuild, no migration — and it is now nothing at all: the writers run unconditionally, so a fresh environment needs no arm step. What a fresh environment does still need is the arm block, which is the campaign's upper bound, what complete is measured against, and the last resort of the tick window's bootstrap chain (below):
INSERT INTO onchain_credit.chain_scan_cursors (chain_id, scope, last_scanned_block, updated_at)
VALUES (1, 'portfolio:v2-campaign:arm-block', <the block at the restart>, now())
ON CONFLICT (chain_id, scope) DO UPDATE
SET last_scanned_block = EXCLUDED.last_scanned_block, updated_at = now();Confirm the writers are running. Since the contract release, step 1, one process derives the ledger: the ledger worker. One 6h tick logs [portfolio] v2 sweep: N sweep job(s) enqueued over [<from>,<to>] and the worker then logs a -> done per sweep job and the tick cursor's own line; the drain logs a [rederive] candidates=… line (on a half-hourly heartbeat when there is nothing to re-derive, so allow thirty minutes rather than one) and, for each wallet it launches, rederive job #… enqueued; one page load's refresh leaves a sync job -> done in the worker's log.
All five callers run the derivation composition: the worker (for the tick's sweep, the page load's sync, the registration replay's enrol, the re-derivation trigger's rederive and the ingester's continuous jobs), and the offline history builder; the inline call sites the releases before used were deleted by the contract release, step 2.
The refusal is per wallet and per range, and that distinction is what an operator reads in the log. A wallet is refused, with its reason named on the line, when this run cannot honestly derive it:
- it holds a position at a venue the run's adapter set did not cover — a stated refusal rather than a partial write, because writing the covered venues would let the range be certified as derived while one venue was never read;
- it holds a position the run covered the venue for and still cannot name — a vault, a reserve, a market or a PT the venue's registry does not list. The line names the position. This is the same rule as the one above, applied at the unit coverage is actually measured in; without it the wallet would be reported as fully derived over a position nothing looked for. No tracked wallet trips it today — replayed against production on 2026-08-28, all of them resolve — so a line of this kind means a registry lost an entry, and the entry it lost is on the line;
- a venue registry read failed, so the run cannot state what it covered;
- a liquidation landed in the range on a vault whose position states could not be read. That receipt has no event of its own — the change in position across the block is the receipt — so a run that cannot read the state cannot see the liquidation at all.
A refused wallet writes nothing, stamps nothing and is retried by the next pass, which is the same behaviour the whole-file refusal had; what changed is that the wallets a run can serve are now served. Seeing a handful of refusal lines beside successful wallets is the expected shape. Seeing every wallet refused for the same reason is a genuine fault, and the reason on the line names it.
The composition prices every row it writes. Each row is valued at its own block on the three stored lines: the market mark in the position's book, the redemption mark in the same book, and the dollar figure that lets capital net across books. The marks come from the same price mirror, the same wrapper composition and the same block-anchored redemption rates the existing flow ledger is valued with, so a movement cannot be worth one thing on the old ledger and another on the rebuilt one.
A mark that cannot be resolved stays empty and says so. It is never zero and never a guess: a token whose decimals cannot be read, a price bar the mirror does not hold, a young Pendle market whose oracle reverts — each leaves that row's market figure empty and raises a
mark-unresolvedanomaly naming the asset and the block, and the resolver logs one line per asset and per empty figure, carrying how many of that asset's rows came out that way and the lowest block, so a repair can target the asset instead of re-deriving every wallet that touched it. Two figures are empty by design rather than by failure: a holding outside the yield books has no redemption claim, and a Pendle principal token's redemption mark is the pull-to-par curve the read path derives from the position's own entry.A half-valued row counts as unvalued, and that is what makes the count worth reading. The redemption mark needs no price bar — for a par holding it is the identity — so a row whose bar is missing comes back carrying a redemption figure and no market figure, and looks populated. The reconciliation is computed from the market mark, so such a row is exactly as unusable as an empty one; it is counted as one, and the redemption figure it did resolve is still stored.
The dollar figure is counted the same way, and it can be empty on its own. It is the market figure carried into dollars by the book's own unit, so in the ether book it needs an ETH price at the same instant. A holding of ether itself — or of any asset whose claim is one unit of its own book — is worth a real number of ether with no price lookup at all, and at an instant where the ETH price is missing that row comes back with a book figure and no dollar figure. Cross-book capital netting is computed from the dollar figure, so it withholds a whole transaction on the first such leg. Both figures are counted, each on its own line naming what to load: the asset's own coverage for a missing market figure, the book's unit for a missing dollar one. The redemption figure is the only one of the three with a by-design empty, which is why the count is never taken on it.
How precise those marks are, and the one case where they are less so. Every mark divides two quotes taken from the same bar, so the exchange level cancels and only the slow secondary-market basis survives — that holds whatever the bar's age. What can be stale is the bar itself. The existing flow ledger pays for one Dune execution per run cluster to true its bars up to the exact minutes it values at; the composition deliberately does not, because its anchors are every receipt block in a range rather than a handful of window anchors, and a whole-history build would spend thousands of executions to move a mark by the basis drift of one hour (~1-3bps in calm markets). Inside the 6h tick this costs nothing: the flow ledger runs first, writes the exact bars for the same blocks, and the composition reads at the same timestamps. It shows only where the old ledger never valued a flow at that block, and on a standalone history build, where a mark can sit on a bar up to an hour old.
Where you read the count. The offline history build prints it per range in its
anomalies=total. The inline 6h tick carried it as anN unpricedtoken on its own summary line ([portfolio] v2 dual-write: … 0 failure(s), 0 partial, 2 unpriced in …): the count of wallets carrying at least one row with an empty money figure — either the market one or the dollar one. Since the contract release, step 1, the tick derives nothing itself (its summary line counts no rows), and the same resolver lines land in the ledger worker's log, one per asset and per empty figure, for the sweep job (or any job) that derived the rows. Its healthy value is 0; a persistent non-zero names an asset, not a release, and the resolver's own per-asset lines say which asset, which figure is empty, how many of its rows, and the lowest block. The two remedies it points at are getting the missing series covered by the price mirror — the asset's own for an empty market figure, the book's unit for an empty dollar one — and then re-deriving the affected range, in that order, since a re-derivation against a mirror that still cannot price it changes nothing. The first tick after a change that widens what gets valued can legitimately show a non-zero here where the same rows were previously empty in silence; the per-asset lines are what tells the two apart.THE PRICED REBUILD'S PRECONDITION: load the price mirror back to the ledger's first event before you start. This is a correctness boundary, not an optimisation, and it is the one step that decides whether the rebuild below is worth its wall clock. Rows are valued at their own block; below the mirror's earliest bar for that token there is no bar to value against, so every row of it down there stores a redemption figure and no market figure — populated-looking rows the reconciliation cannot evaluate. The gap is not self-healing: a missing bar falls through to a per-window fetch from the pricing provider, which is credit-guarded per run and, once the guard trips, returns nothing for every remaining block without failing the build.
Coverage is per token, and the number to compare against is the WORST one. The mirror's earliest bar overall is a property of whichever token has been tracked longest, not of the token your rebuild is about to price. On prod those differ by more than a year: the deepest series starts 2025-07 and roughly three-quarters of the tracked tokens have their first bar in 2026. A check that reads the overall earliest bar and calls the range covered is answering the wrong question.
And "which tokens" is the registries, not the log stream. The tokens a rebuild has to price are the ones a row can be denominated in — everything in the tracked-token registry, every lending-reserve underlying, and every Pendle market's underlying. That is deliberately wider than "tokens seen emitting a log under their own address": a log carries the emitting contract, which is the token only on a plain transfer and is the venue on every pooled, vaulted or wrapped stream. Asking the log stream which tokens matter therefore drops exactly the assets a lending ledger is made of — an ETH liquid-staking token held only through a lending venue has no log of its own and plenty of rows. Measured on prod 2026-08-29: 127 tokens in that universe, 71 of which the mirror holds no bar for at all, and 19 already short against a log of their own. A token with no bar anywhere is the worst case there is, not an absent one.
The build prints the answer before it does any work (
[shadow-build] price mirror: worst-covered token 0x… has NO BAR AT ALL …), and--dry-runprints it too. That line is the authority: it takes the worst coverage over the tokens this ledger prices from a bar of their own — excluding the wrappers whose mark composes from another token's series, the par-pinned assets that consult no bar at all, and the principal tokens marked off their underlying — it names the tokens already short, and it prints the exact load command for them. To see the list by hand, use the build's own line rather than a hand-written query: the hand-written one is where the "which population" mistake gets made.Run the command the line prints, verbatim. The tape command below is one example of what the line can say, not its only shape. It names its tokens explicitly, and that matters more than it looks: the loader's default set is its own tracked list of 27 addresses, and on prod 2026-08-29 75 of the 87 tokens this check names are outside it — including the worst-covered one, and 7 of the 19 already short against a log of their own. Running the bare script would exit 0 without touching them and the check would print the identical line forever. The named set also governs the loader's resume gauge (the emptiest named token decides whether the window is already loaded), which is the right yardstick here and the reason
--forceis rarely needed. One long-window execution, a few credits against a ~4k/month budget:bash# exactly as the build printed it — start instant and token list included /opt/onchain-credit/scripts/run-cron.sh backfill-token-price-bars.ts 2026-01-01T00:00:00.000Z \ --tokens=0xd3fd63209fa2d55b07a0f6db36c2f43900be3094,0x056b269eb1f75477a8666ae8c7fe01b64dd55ecc,…Then re-run the check, and read a second identical line as an answer, not a failure. Whether the load actually filled a thin token's history is not knowable from its exit code — it skips a window it judges complete, and whether the provider holds bars that deep for a thinly-traded token is not something the preflight can see in advance. Re-reading the build's own line (
--dry-runis enough) is what turns this from a hope into a precondition. A token still short after a load that ran is one the provider has no series that deep for — a matured principal token, a vault share that never traded, a token first quoted last month. That is a coverage note to record (its rows will carry a redemption figure and no market figure, and the reconciliation will exclude them), not a load to re-issue forever. Re-issue only for a token you have reason to believe the provider does cover.The loader is idempotent and resumable, so re-running it after a partial load is safe.
--forcedisables the resume gauge entirely; with an explicit--tokensset the gauge is already the emptiest named token, so reach for--forceonly when you have named a token that does have bars in the window and want them re-fetched anyway. For the record on the default (WETH) gauge, that margin is thin and shrinking: measured on prod 2026-08-29, over[2025-05-21, now]the mirror holds 9,649 on-hour WETH bars against 11,158 window hours (0.86, under the 0.9 the yardstick calls complete), and the ratio rises on its own as the mirror ages.A range derived before the resolver shipped is worth nothing and must be rebuilt. There is no re-mark repair for a range derived unpriced — so the remedy is the ordinary one: re-derive it. That is exactly what the re-derivation trigger is for, and it is why the composition never reads a cursor. Budget one full history rebuild if any range was built unpriced, before C6 — and spend it after the mirror's per-token coverage has been loaded and re-checked, or it buys a ledger that still cannot reach C6 and a second rebuild.
The second precondition is that every declared stream is actually being followed, and it has a visible failure mode rather than a silent one. Each writer stops at the slowest stream's ingestion progress, and a stream that is live in the coverage table with no ingester cursor counts as progress zero — so one such stream holds the whole dual-write at the floor. What you see is a tick that logs "the slowest stream this tick relies on is ingested only to block 0" and derives nothing, on every tick, rather than quietly writing half a history. If that line appears, the fix is the ingester; the ordering the runbook already prescribes (restart the ingester onto the new streams, then backfill each to the floor) is what keeps it from happening.
The rebuilt ledger's own 6h window
The 6h tick's rebuilt-ledger pass used to borrow its block window from the old pipeline's flow scans. Those scans are deleted, and the pass keeps a cursor of its own — portfolio:v2:tick in chain_scan_cursors, one row for the whole tracked population — and computes its window from it:
- the floor is that cursor plus one;
- the cursor stops at the chain's finalised head, exactly as every old flow scan's cursor did. Everything the tick derived above that line is reorg-exposed, so it sits above the cursor and the next tick re-derives it. Nothing re-derives below a cursor, so a cursor saved at the top of the range would strand that tail;
- the ceiling is unchanged: the tick's own scan bound, clamped to the slowest ingestion progress across the streams the derivation reads. A tick that cannot reach the ceiling keeps its floor rather than stepping over the gap, which is what the borrowed window could never do;
- a page load derives from the LOWER of that cursor and its own lookback floor, up to its own anchor —
min(last snapshot block + 1, cursor + 1)— so the two passes agree on where the ledger is current and the window can only ever widen relative to what shipped. Theminis the safety half rather than decoration: a wallet with no snapshot at all takes a fixed-lookback floor that can sit below the population's cursor, and that wallet was not in the population the cursor speaks for. A page load never advances the cursor, because it derives one wallet.
The first tick after the deploy prints which rule produced its floor, and that is the one line to grep:
grep 'v2 tick window' /tmp/onchain-credit-cron/onchain-credit-refresh-portfolio.log | tail -3
# [portfolio] v2 tick window: floor 23001235 from the lowest per-wallet derive cursor (bootstrap: no v2 tick cursor yet)
# [portfolio] v2 tick cursor -> 23003010 (settle line 23003010)
# ...and every tick after it:
# [portfolio] v2 tick window: floor 23003011 from the portfolio:v2:tick cursorThe bootstrap chain, in order, for a tick that finds no cursor. That includes the first tick of the release that introduced the cursor, not only a restored or rebuilt database: portfolio:v2:tick is written by the tick and by nothing else, so it does not exist anywhere until one has run.
- the lowest per-wallet derive cursor over the tick's own population, plus one — the highest block every wallet in it is already derived to. A wallet with no cursor is left out rather than dragging the floor down: nothing of it is served, and its registration replay derives its whole history anyway.
- the recorded arm block, plus one: the top of the one-time historical build, so everything above it is what the live writers owe.
- nothing. The tick derives no rebuilt-ledger range at all and says so on the same line. Record an arm block (the
INSERTunder the writers section), or let one wallet's registration replay stamp a derive cursor, and the next tick picks it up.
What rung 1 costs on the first tick, stated so it is not a surprise. A tick never stamps a per-wallet derive cursor (only a whole-history run does), so those cursors sit where the last replay or historical build left them — for most of the population, at the recorded arm block. The first tick therefore derives from the arm block to the ingested tip: on the order of a week of blocks (tens of thousands) for the whole population, against ~1,800 on a steady tick. It is correct and it is bounded — the composition segments its own log load, so this is a handful of segments per wallet rather than one enormous read, and run-cron.sh's flock is non-blocking, so a tick that overruns its slot is skipped rather than stacked. No range is missed either way: every wallet with a cursor was derived at least that deep by its own whole-history run, and the tick covers everything above the minimum for the whole population.
What it does do is re-derive a week of already-written rows and restate their marks — a few basis points on value_market (the accepted same-bar drift), more on the unserved value_usd (see the true-up note).
Optional insurance: seed the cursor before the first tick, and before migration 095. It skips the wide first tick entirely by handing the new cursor the window the deleted rung would have taken, read from the old flow scopes' own cursor rows. Those rows exist only until migration 095 runs: its second half deletes exactly the seven scopes the seed reads from, so after 095 the INSERT below is a silent no-op (HAVING count(*) > 0) and the first tick falls back to the bootstrap window, which is safe but wide. It needs no migration (so it is unaffected by prod migrations being a manual step) and it is a no-op once a tick has run:
INSERT INTO onchain_credit.chain_scan_cursors (chain_id, scope, last_scanned_block, updated_at)
SELECT 1, 'portfolio:v2:tick', min(last_scanned_block), now()
FROM onchain_credit.chain_scan_cursors
WHERE chain_id = 1 AND scope IN ('portfolio:transfers','portfolio:transfers:wallet',
'portfolio:events:morpho-blue','portfolio:events:aave','portfolio:events:sparklend',
'portfolio:events:fluid-operate','portfolio:transfers:fluid-nft')
HAVING count(*) > 0
ON CONFLICT (chain_id, scope) DO NOTHING;HAVING count(*) > 0 is what makes it a no-op rather than an error on a box where those scopes are absent — a reseeded staging, or any environment that never ran the old scans. Without it, min() over zero rows returns one row of NULL and last_scanned_block is NOT NULL, so the statement fails with a constraint violation. It is fail-closed either way (nothing is written), but a constraint violation reads as a broken runbook rather than as "you do not need this step". Verified both ways: INSERT 0 0 when the scopes are absent, the correct min when they are present, and a second run inserts nothing.
Take it or leave it deliberately, and grep the v2 tick window line either way: seeded, the first tick reads from the portfolio:v2:tick cursor and covers ~1,800 blocks; unseeded, it reads from the lowest per-wallet derive cursor and covers the week described above. Both are safe; the seed only buys the wall clock and the restatement.
The acceptance reconciler
scripts/ops/reconcile-ledger.ts answers one question: does every figure the rebuilt engine books follow from the stored rows alone? It recomputes each figure a second time, from the stored snapshots and the stored movements, with arithmetic that shares no code with the engine, and reports every disagreement. The same job also runs on a schedule, so a disagreement that appears later is not silent.
It is read-only by default — every statement is a SELECT — so it is safe to run at any time, against any environment:
cd /opt/onchain-credit && DATABASE_URL="$(grep ^DATABASE_URL= .env.local | cut -d= -f2-)" \
npx tsx scripts/ops/reconcile-ledger.ts
# one wallet, one block range, and the machine-readable form
npx tsx scripts/ops/reconcile-ledger.ts --wallet 0x… --from 24136053 --to 24200000 --json--from and --to restrict what is reported, never what is computed: the engine always runs over the wallet's whole history, because a leg's figures depend on where its position opened. Both ends are required — a half-specified range would read as a full run. The range is echoed on the report for the same reason.
It judges the bookings the page serves (issue #988). The page does not book from the stored rows as they stand: before it runs the engine it applies two read-time marks, a principal token's accrual value from its per-lot curve (M34: no writer stores one) and the financed-group rule (a borrowing publishes a line for all of itself or none of it). The reconciler runs the same construction over the same three registry answers (the token registry, the Pendle market universe, and the factor series of the markets each wallet holds), read inside that wallet's own snapshot, and hands the marked values to its independent side as input, never as arithmetic. So on a principal token's accrual line the identity checks the bookkeeping over the curve's marks, not the marks themselves: both sides sum the same mark, and nothing here checks that the curve is right. Two consequences an operator meets:
- A failed registry read is fatal (exit
1), where the page degrades to a dash: a run that could not see the marks has not measured the page. The line names the wallet and the read:[fail] ledger-reconcile fatal read-time registry read failed for <wallet> at <relation>, so the run measured nothing: <cause>, the relation beingportfolio_tokens,pendle_marketsorpendle_market_state. - The declined figures on a principal token are the page's own. Before #988 the run read the unmarked rows and filed a
W2 unpriced-input(evidenceendpoint:value_redemption-null, two per interval) on every interval of every principal-token holding the page values. What remains on such a leg is the page's own withhold, the one its log names (pt-factor-unavailable,pt-lot-unvalued, …).
Reading the report. A clean run is quiet. The one line it always prints is the coverage line:
ledger-reconcile: wallets=3 reconciled=3 excluded=0 uncertified=0 checked=4128 book-checked=1032 withholds=2 notes=3 env=onchain-creditexcluded— wallets the reconciler did not check, because their rebuilt history is not yet provably complete. Checking a half-built history against a full curve reports a difference that is not a defect, so excluding them is right — but the figure is printed on every run, at zero as well, and each excluded wallet is named with which of three cases it is, because the remedies differ: no cursor (the wallet's replay is in flight, never ran, or ran and correctly declined to stamp), cursor short (the history build stopped part way — resume it), and coverage not certified (that wallet's own enrolment backfill has not reached the floor yet — wait, and read its progress). An alarm whose population silently shrinks reports green forever, which is the failure this whole programme exists to remove.uncertified— a second, independent count of wallets whose required coverage does not reach the floor, read from the coverage relation with the derive cursor never consulted. Both sides of the reconciliation read the same stored rows, so a row that was never written is invisible to it; this figure is what can see that. Both are read inside oneREPEATABLE READsnapshot, so a wallet that certifies mid-run cannot be reported as a defect.checked— how many(leg, window, line)comparisons actually ran. A window is normally one interval; it widens only across points nobody could value, which is why the field is not counted in intervals. A number that collapses between two runs means the population or the coverage narrowed, not that the ledger got cleaner.book-checked— the same comparisons rolled up per book, which is the scope the specification states the identity at. It is the only check that reaches a composite position (a Fluid smart pair whose two sides were re-split against each other): per leg those figures are a share of a joint answer rather than a derivation from one leg's own evidence, so the sum across the book is where the arithmetic is actually testable.env— which checkout produced the line. Prod and staging share this script's logfile (it is keyed on the script name, while the lock and the toolchain marker are keyed on the checkout), so without this field a line in that file names no environment.
When it pages. Exit 1 means the run could not be performed at all (no DATABASE_URL, no recorded arm block, an empty reserve registry). Exit 2 means it ran and found something, and each finding prints a [fail] ledger-reconcile … line naming its own cause:
| Line | What it means | First thing to do |
|---|---|---|
<wallet> <interval> <line> residual=… tau=… | the engine's figure and the independent recomputation disagree by more than float precision | read the leg named at the end of the line, then that interval's stored movements |
book-residual <book> <interval> <line> residual=… | the same disagreement, one level up: the book's total does not follow from its legs' stored rows. position-attributed on the line means a composite position is inside the sum | read the per-leg lines for the same interval first; if there are none, the finding is about a composite position and only this line can see it |
measured-nothing … | the run compared nothing at all — no tracked wallets, or figures that should have been compared and none of them was | a run that measured nothing is not a clean run; find out why the population is empty before reading anything else as green |
excluded=<n> <wallet> <case> [<remedy>] | the same wallet has been excluded on two consecutive scheduled runs, so it is not clearing itself | act on the <case>: coverage, a stalled build, or a replay at its retry cap. The optional <remedy> narrows it further: rederive-at-cap/rederive-failing names a re-derivation that is stuck, and replay-deferred <age> stalled-enrolment/stalled-derivation names a registration whose history was never derived — the first says look at the ingester, the second that a parked replay needs a re-queue. This is the only alarm that pages for a deferred replay; the minutely drain reports one and exits zero |
stamp-defect <wallet> complete=true certified=false | a wallet is marked complete over blocks nothing covers | do not wait: this is a defect in the marking, not a backlog |
shape … | the engine declined to book something, and the reason is not one of the nine the specification allows | an engine question, not a data one |
budget … | too many declined figures for one wallet-day, or any occurrence of the two classes whose production budget is zero (W6, W8) | read the named class. A ledger/reading disagreement is no declined figure at all: the reading audit books the opening or correction that settles it. A ghost itself (a snapshot row for a holding the ledger says is empty) is the engine's cross-check and the audit's page now, and the ghost adjudicator still asks the chain which record is wrong. If the wallet was registered after the campaign's arm block and before the #756 release, check that shape first: its plain token movements above the arm were never derived, and the remedy is the ranged re-derivation, not a restatement |
unexplained … | a figure that is in none of the three accounted-for buckets, or in two of them | the loudest structural finding: nothing in the system explains this pair |
refused-figure <wallet> <line> refused=… <book> movements=<n> <position> | the engine refused a figure for good: a position's figure was missing over a stretch (unread, or read without a price on that line), the stretch held movements, and its other price was missing too, so nothing could check the sum (M23). The figure is dropped, not deferred, so the chart shows that stretch as flat and nothing else in the run would say why. Only firings below the newest reading page; one at the newest reading is counted on the tip tally like any other | find why the position had no value and no second price over the stretch, record what is missing, then re-derive the wallet; the figure returns with the re-derivation |
A run can exit 2 with zero disagreements. The exit code answers does this need a human?, not did the arithmetic disagree?.
Lines that are printed and never page, deliberately: the bridge cross-check (the engine's figure across a stretch the data could not value, stated anyway so a run whose only imperfection is a reading gap still produces evidence); the tip-boundary tally, printed at zero because a later decision is made against it; its twin <wallet>: interior-reported=<n> (<kind>=<n> …), every alarm the engine raised below the newest reading, counted by kind and printed at zero (all of them but the refused figure above accompany a declined figure the budget or the shape list already polices, so they are counted here rather than paged twice); the open-holding count <wallet> open-holding reductions=<n> realized-out=<book>:<amount>,… carried-out=<book>:<amount>,… differing-legs=<n>, what the Holdings rows' open-holding layer took out of the rows on reductions and what rotations carried out of their source rows to the holding they moved into (a read-time layer on the served rows, which the identity does not sum, so it is counted rather than checked; printed at zero, realized-out=0 and carried-out=0 when nothing left); the values line described next; and the coverage block after it.
The values line. Both sides of the identity take the two money columns computed at read, from one computation over the rows the engine is served (the right-hand side values first, so the engine's side is answered from the same result and cannot disagree with it about an input), with the page's read-time marks above applied to both. Each wallet prints one line, and the population line prints the sums:
ledger-reconcile: 0x… values=computed readings=13216 withheld-readings=0 receipts=25 differ=3 statements=2 late-tokens=0 book-overlays=0 book-withheld=0 factless=0/0 (reported, not paged)withheld-readings: readings WITHHELD because their value reads a rate no fact answers (the loader rule): both lines null, since a reading stores no figure to fall back on (migration129); its row shows no value, and its book's totals and chart sum without it. The writers produce none since migration128(every reading states every rate its value read,rate_facts), so on prod, where every wallet was built after it, it reads0. This line only counts them; the hourly check's ledger-audit arm pages each one recorded since its last look. Namedwithheld-readings, notwithheld, so a grep for it does not matchbook-withheldtoo.differ: movements whose computed pair (and, on a PT movement, the PT mark the lot book strikes it against) is not the stored one on the wire. A reading stores no pair, so it is never compared. It is a statement about how the stored column was produced (a price that arrived after the row was written, a rate its writer read on chain, a value the read withholds), never an alarm, and it moves on its own as late bars land.scripts/ops/value-parity.tssays which rows and why.statements: the series reads the wallet's valuation cost, both sides together. Two per wallet (the bars at the hours its rows are valued at, and the rates), plus one or two where some hour's own bar cannot stand for its whole 48h walk-back (a gap in the bars, or a bar from another price source than WETH's), which read those walk-backs. A higher figure means the two sides were handed different rows.late-tokens: tokens the valuation had to read on their own, at an hour its batched read did not foresee. Each costs a statement, not a number.book-overlays/book-withheld: readings valued in the book they were stored in because the registry has since re-booked their asset, and movements withheld because the registry now prices them in a book their leg is not stated in. An asset that came INTO a book, either book, is restated by the ledger worker's next pass and its wallets re-derived (the coverage restatement), after which both read 0 for it: a figure that stays above 0 for such an asset after its coverage release is the restatement's own signal (held readings, a failing turn), reported and not paged. One that left coverage, or moved to the other book, keeps them for good: its history is served as written, and the worker reports it at every pass.factless= movements / pre-window PT fills served their STORED columns because their value reads a rate no fact answers (the loader rule): rows written before migration116that the rate-facts backfill has not filled (or could not: no fact reproduces the value they were struck at), a PT movement with no stamp, a fill stored without its consideration, an audit row whose reading is gone and which carries no copy of it. Such a row is never compared, sodifferdoes not count it. The writers produce none since128, so on a wallet built after it the field reads0/0.
Filling the rate facts of rows written before 116
scripts/ops/backfill-rate-facts.ts fills, IN PLACE, the rate facts a MOVEMENT or a pre-window PT fill written before migration 116 lacks, so the row is computed instead of served stored, and only where the fact reproduces the value the row stores. Not a reading, since migration 129: a reading stores no value to check a candidate against, so filling one would state a rate nobody can show its writer struck it at. A reading no fact answers stays withheld (prod holds none: every wallet there was built after 128). A writer before 116 did not always strike a row at the chain's answer: where its archive read failed it took its fallback (the six-hourly series at or before the block on the enrolment replay and a movement, the newest observation on the 6h tick and the live tip) and stored a value struck at that rate. So for each missing rate the run has CANDIDATES, in order: the chain's answer at the row's block (the writers' reader, on the archive endpoint ETHEREUM_ARCHIVE_RPC_URL), then the fallback the row's writer had, as the series stood when the row was written (updated_at); a listed fund's NAV and a per-share rate have none. It fills the first candidate under which the served read reproduces the row's stored columns (every line equal on the wire, or reproduced by the series as its writer saw it, which is late or corrected series data; at least one stored number reproduced), labelled with where it came from (chain, series or live). So every row the run turns computed serves what it served before, up to late or corrected series data, and every other row is served exactly as before the run.
It writes the fact columns only (a movement's meta.rateFacts, a fill's consideration_* and rate_facts), only where they are empty, and never updated_at; a fact already there is never replaced, not even one a writer lands while the run is deciding. Idempotent and resumable: each wallet's rows are written as they are decided, and a re-run skips what is filled and tries again what was left.
cd /opt/onchain-credit # the checkout of the environment being filled
set -a; . ./.env.local; set +a
# 1. Dry run: reads every candidate, checks it against the stored value, logs each row it would
# fill, writes nothing.
npx tsx scripts/ops/backfill-rate-facts.ts --db-url "$DATABASE_URL" | tee /tmp/rate-facts-dry.log
# 2. Apply.
npx tsx scripts/ops/backfill-rate-facts.ts --db-url "$DATABASE_URL" --apply | tee /tmp/rate-facts-apply.log
# 3. Rows it left are listed, each with the candidates it tried and the line each could not
# reproduce. A row whose chain read failed ("no candidate rate at its block") is retried by
# running step 2 again.
# 4. A second --apply must report 0 write statements.Its last line is the summary, per relation (movements, pre-window fills): rows scanned, rows lacking a fact, rows filled (and how many facts came from each source), and why each row it left was left:
- no candidate rate at its block: the chain did not answer, and the row's writer had no fallback there (a per-share rate, a listed fund, an asset with no series).
- no candidate rate reproduces its stored value: the row was struck at a rate neither the chain nor the series answers now (a fallback observation since rewritten). It stays served stored.
- the read would still serve it stored: a PT movement with no stamp.
- no router log on file for the fill, or its market not in the registry.
Flags: --wallets a,b, --concurrency N (archive reads in flight, default 8), --page N (rows per write, default 500). There is no --series-fallback any more, and the run refuses it: a row its writer struck at the series is filled series by the check itself, and one the series does not reproduce is never filled from it.
What the run could and could not see. Every run also prints, at zero as well:
which classes of declined figure it derived for itself, and which it could not evaluate at all with the reason and the change that closes each. Today one class is named there: the check that a wallet's coverage certified at the tip needs a certificate read the served data layer performs, and this job does not perform it. Nothing else in the output tells "no such case arose" apart from "this run could not have seen one", and a reader of the acceptance evidence would otherwise draw a conclusion the run does not support.
how many comparisons left the check and under which rule — the run-level
excludedfigure covers wallets; this covers individual figures, socheckedis not the only visible measure of the narrowing.the number of declined figures beside the sum of their sizes. The budget is enforced on the count, and the sum is printed beside it, unbudgeted: the nine classes record nine different kinds of quantity (one of them records none at all, by design), so their sum is not a quantity anything can sensibly be compared against.
Two of the nine cover a RUN of periods rather than one, and are filed once for the run they cover: an unread stretch, and a stretch where the stored readings hold a value for a holding the movements say was never there. Filing once is deliberate — otherwise the budget would count days of a single reading gap instead of counting reading gaps — and each row carries the period it closed at so the whole run is accounted for. Before R9 the row claimed only the period it was filed at, so every other period of the same stretch reported as "the engine declined and the reason has no shape", which is what 2,214 of production's 3,134 such findings were. The second of the two is NEW in R9 and its production budget is zero: the readings and the movements contradicting each other about whether a holding existed is never a legitimate steady state, so giving it a name must make the decline legible without retiring it. The reasoning is written out in the code that enforces it. Whoever sets the budget after the first full run sees both figures and can say which one they are choosing.
the row counts each wallet's read returned, including movements that arrived without a quantity (whose production budget is zero) and movements whose type is not one the ledger should be able to hold at all. Both are counted where the read refuses them, so a zero on either is a measurement rather than a field that is always zero.
Two quiet lines that mean "nothing to compare", and neither is a defect. A wallet that holds only assets outside the modelled perimeter, and a wallet whose history is too short to contain a single period — a signup whose enrolment landed between two refreshes — produce no comparison at all. Both are healthy and neither pages, but both are named, because the only thing that distinguishes them from a run whose every figure was declined is that line:
not comparable yet: the spine holds N grid point(s)— that wallet has one reading or none, so there is no period to compare over. It enters the check on the next refresh, by the ordinary path. Nothing to do.nothing to compare: 0 (leg, interval) pairs …— the whole run had nothing comparable in it. Expect this on a hand run narrowed to a single new wallet, or to a block range that contains no reading. On the scheduled run over the full population it should not appear, and if it does the question is why the population has no comparable holdings rather than why the run is quiet.
--marker is for the scheduled run only. It maintains the "excluded twice in a row" state, and a hand run that wrote it would either give a genuinely stuck wallet a free pass or page the next scheduled run for a wallet somebody already looked at. A hand run says so on its own report.
The permanent reconciliation alarm
scripts/refresh-ledger-reconcile.ts is the job above on a 6h cadence, so a disagreement that appears after the cutover is not silent. It is a thin wrapper: every page condition, every line and every exit code is the reconciler's, described just above. What the wrapper adds is the schedule, one remedy field, and the budget.
Its crontab line is a server step. The root crontab lives only on the box and is in no repository, so no deploy installs it and no rollback removes it. Install it once, after the release that adds the script has deployed:
30 1,7,13,19 * * * /opt/onchain-credit/scripts/run-cron.sh refresh-ledger-reconcile.tsVerify the install two ways, not one. Reading the next tick's line proves a checkout is running the job; counting the crontab line proves it is this one. Only the second carries the path, and only the first proves it actually runs:
crontab -l | grep -c '/opt/onchain-credit/scripts/run-cron.sh refresh-ledger-reconcile.ts'
tail -5 /tmp/onchain-credit-cron/onchain-credit-refresh-ledger-reconcile.log
# ledger-reconcile: wallets=10 reconciled=10 excluded=0 uncertified=0 env=onchain-creditA line still has to say which environment printed it
run-cron.sh keys the logfile on the checkout, as it does the lock and the toolchain marker, so /tmp/onchain-credit-cron/onchain-credit-refresh-ledger-reconcile.log is this checkout's history and the alert's why: slice is read back out of this checkout's file. That makes the file one environment's, not the LINE's: a line read out of a page, a paste or a screenshot carries no path with it. The env= field is what tells the two apart: it is derived from the checkout root, it is on every line this job prints, and it is the same label the Telegram alert prints as env:. A line without it names no environment. If the count above returns 0 while the log shows this job's lines, the line you installed is on the other checkout.
Every tick connects, reconciles and reports, whatever the environment says; there is no gate in front of it. scripts/ops/reconcile-ledger.ts is the narrowed hand run whose output is the acceptance evidence.
A brand-new environment with no recorded arm block now pages every 6h
The reconciler refuses to measure a population it cannot bound, so with no portfolio:v2-campaign:arm-block row it exits 1 and pages — where the dormant tick used to print one quiet line. Prod and staging both carry the row (it is outside the scrub's prefixes, so a reseed keeps it), so this is a fresh-database concern: record the arm block, with the INSERT under the writers section, as part of standing an environment up. It is the same row the rebuilt ledger's 6h window falls back to.
When it pages, read the reconciler's table above — the lines are the same. Two additions:
excluded=<n> <wallet> <case> <remedy>: <reason>. A wallet excluded on two consecutive ticks pages, and the<case>alone cannot separate two states an operator acts on differently. When the wallet has a re-derivation attempt row, the line carries which:rederive-failing <k>/5 check-archive-provider(still retrying, on a doubling backoff — the fault is upstream) andrederive-at-cap <k>/5 needs-a-human(it has stopped retrying and nothing will clear it on its own). A wallet with no attempt row carries the reconciler's own reason unchanged, which for the ordinary case already reads wait on that wallet's enrolment backfill. Without that field the page names a wallet while the fault is a loop, and all three read as "wait".PORTFOLIO_RECONCILE_WITHHOLD_BUDGETin.env.local— the declined-figure budget, as a whole count per wallet-day, never a percentage. Set it from the first full run over the rebuilt history. Unset is legitimate and the run says so on its own report; the two classes whose budget is zero are enforced either way. A value that is not a whole number is fatal rather than a silent fall back to unset, because an unbudgeted alarm reports green on exactly the condition the budget exists to catch.
A wallet stuck at the re-derivation cap now pages from here
It did not always. The re-derivation trigger shipped before this alarm woke, and a rebuilt-ledger fault never reaches the minutely drain's exit code by design — so in that window a wallet that had failed five re-derivations produced no page from here, no [fail] line from the drain, and no alert at all: it was visible only in the drain's log. With the gate deleted this alarm carries it on the first tick after it happens. The drain's rederive-attempts figure is still the fastest way to tell "still waiting on coverage" from "the derivation itself keeps failing": at 5 it has stopped retrying and needs a human.
What the page says
This job sends its own Telegram message instead of the wrapper's generic one (issue #992), so a page says which wallet, which position, why, over which days and what the user sees, rather than budget total count=152 budget=31. The message is built from this run's findings only:
⚠️ creddit alert: Ledger self-check (production)
Tue 29 Sep 2026, 07:30 UTC · runs every 6 h · failing for 3 runs since Mon 28 Sep 2026, 19:30 UTC
WHAT THIS CHECKS
Every six hours the app re-derives each tracked wallet's daily returns ... Limit: one held-back
item per wallet per day. Above it, that user's history has gaps worth a look.
RESULT
Books agree for all 12 wallets. 1 wallet over the limit, 11 fine.
WALLET 0x… (full address)
37 held-back items, limit 9 · open since 28 Sep (3 runs)
• USDT supply (Aave v3, $3k): no market price, 15 Sep to 16 Sep (12 items).
→ User sees: The whole loan (collateral and borrowing) is left out of return on those readings, ...
• DAI debt (Aave v3, $2k): held back because USDT supply had no price, 15 Sep to 16 Sep (12 items).
→ User sees: as above.
Suggested fix: If USDT supply should have a price over those dates, add a market price source ...
RESOLVED SINCE LAST RUN:
• 0x9888…9888: PT-USD3-25DEC2025: factor history missing
Log: /tmp/onchain-credit-cron/onchain-credit-refresh-ledger-reconcile.log- One WALLET block per flagged wallet, worst first: a wallet whose books disagree, then the most held-back items. Its first line names every condition it met (held-back items over the limit, a kind that should never occur, a book disagreement, not checked for two runs in a row, a coverage stamp defect, a consistency finding, a figure refused for good) and how long it has been open.
- A figure refused for good (M23's guard, issue #722) reads like any other finding:
• USDC supply (Aave v3, $10): return refused, 16 Jan to 18 Jan (-$8.48). It had no value over that stretch, and 1 movement inside it could not be checked against a second price., with what the user sees (no return over that stretch, none booked later, the chart flat there) and the fix (record what the stretch is missing, then re-derive the wallet). It stays open, run after run, until a re-derivation brings the figure back. - A position is named by the Holdings panel's own naming (the registry symbol; a Fluid position without its vault number, which the check does not read), with its venue and newest market value. The reason is one of the withhold causes in words: held but not read (W1), and for a missing price (W2) which of four it was: the position's own price, a principal token's factor history or payout rate, a movement, or another part of the same loan (the financed-group rule held it back with the position that had no price, which is named).
- "User sees" follows from the cause, the line (yield, or value and return) and whether the financed-group rule governs it, as M29's withhold table and the financed-group rule state them and the page applies them: a currency book's value is marked at redemption, so a gap on the yield line blanks the loan's yield and book-value lines (or, standalone, leaves the position out of the book value), while a gap on the market line costs the return line, the market book value at the newest reading and the All view, never the book value at redemption. A standalone gap costs the position's own line; a loan's gap takes the whole loan off that line.
- "Suggested fix" names the repair per cause. A price repair is worded conditionally ("if X should have a price over those dates…") and followed once by "some older readings are left unpriced by decision", because some are: the PT on fxSAVE's readings from before the release stay unpriced, and backfilling them is a repair the rules refuse.
- Open since and resolved. The job keeps its state in
/tmp/onchain-credit-cron/<checkout>-refresh-ledger-reconcile.state.json(the wrapper'sCRON_STATE_FILE): when the current run of failures started, how many runs it has lasted, and every finding of the last failing run with when it first appeared. A finding present on two consecutive failing runs reads "open since"; one that disappears is listed once under RESOLVED SINCE LAST RUN and dropped. A gap that is still there on a wallet that merely dropped under its limit (its limit grows with its days) is not resolved: it is kept in the state, unpaged, and listed only on the run it actually disappears. A clean run sends nothing and deletes the state, so the all-clear is the silence and the next failure reads "first failure". A run that could not be performed (exit 1) leaves the state alone and pages with the generic message, whosewhy:carries itsfatalline. The state sits beside the log in/tmp, so a reboot clears it: the next failure then reads as a first one, never as an invented streak. A file that cannot be parsed is treated the same way. - It always fits. Telegram refuses a message over 4,096 characters, so the text shows fewer positions per wallet ("and N more findings"), then names the wallets it has no room for by address ("Also flagged, not shown"), and as a last resort is cut and says so. Should Telegram refuse it anyway, the wrapper sends its generic message.
- The log is unchanged. The run still prints every
[fail]line, in the order above, and none of the message is printed to the log. The last message sent stays in/tmp/onchain-credit-cron/<checkout>-refresh-ledger-reconcile.alertuntil the next run deletes it. A hand run (scripts/ops/reconcile-ledger.ts, or this script outside the wrapper) has no paths in its environment and writes neither file.
To look at the ledger by hand, run scripts/ops/reconcile-ledger.ts, not this one. This job maintains the "excluded twice in a row" state; a hand run of it would either give a genuinely stuck wallet a free pass or page the next scheduled run for a wallet somebody already looked at. It also takes no arguments — a flag meant for the hand reconciler is refused rather than silently ignored, so a narrowed run cannot be mistaken for the full one.
Re-setting the withhold budget from a run
The budget is a property of the population and of what the run counts, so a release that changes either (a re-derivation that restates rows, a change to what the reconciler books from) is followed by a re-set taken from the first scheduled run after it, never carried forward. The figure is per wallet: a wallet pages when its declined rows exceed budget × days, its days being the span of its grid in whole days (at least 1). The procedure, on the box whose budget is being set, after that run has printed. On prod, steps 1 and 4 run only with Fred's explicit go: step 1 is a read-only run against the production database and step 4 edits its environment, and nothing touches prod without that go.
Read every wallet's count against a budget of zero, read-only (a hand run writes nothing and leaves the scheduled job's marker alone):
bashcd /opt/onchain-credit && set -a && . ./.env.local && set +a npx tsx scripts/ops/reconcile-ledger.ts --budget 0 --json 2>/dev/null \ | jq -r '.reports[] | select(.scope == "wallet") | . as $r | ($r.budgetBreaches[] | select(.shape == null)) as $b | ($b.detail | capture("over (?<d>[0-9]+) day").d | tonumber) as $d | "\($r.label) withholds=\($b.count) days=\($d) per-day=\((($b.count + $d - 1) / $d) | floor) by-shape=\($r.withholdsByShape | tojson)"'A wallet with no line declined nothing.
per-dayis the smallest budget that wallet passes at.Read what remains before choosing a number. Each wallet's
by-shapesays which classes the rows are. A budget is a statement that the rows under it are expected; a class nobody can explain is a finding to chase (its own line in the run says which leg), not a reason to raise the number.Choose. The largest
per-dayis the smallest budget at which this run is quiet; anything lower keeps paging on the wallets above it, which is the right answer for a gap someone is fixing. It is the owner's call which.Set it (prod: with Fred's explicit go) in the same environment's
.env.local(PORTFOLIO_RECONCILE_WITHHOLD_BUDGET=<n>, one line, a whole number).run-cron.shsources the file on every tick, so the next scheduled run reads it; nothing restarts. Confirm on that run's log: nobudget totalline for any wallet the step-1 output put at or under the chosen number.
The ghost adjudicator
scripts/ops/adjudicate-ghost-rows.ts answers the one question a ghost leaves open. The engine's ghost cross-check says the stored snapshot holds a valued row for a holding the movement ledger says was not there. It does not say which of the two is wrong, and two opposite faults produce the identical row:
- the snapshot is stale — the holding really went to zero and the 6h refresher kept republishing the last balance it had (the promotion that stops it). The stored rows are wrong and can be restated.
- the movement ledger has a hole — the wallet really held it and the derivation is missing inbound receipts, so its running balance sits at zero or has gone impossibly negative. Here the stored snapshot is the only correct record, and the fault is upstream.
A third cause used to produce the same shape on the leveraged-vault venue and no longer does: a two-token position whose running total drifted from the venue's own reading of it. Those are now settled from that reading directly (why), so a rerun after that release should not present the adjudicator with any of them. If one appears, it is a hole or a stale snapshot like the other two and the chain still decides.
The same release also gives the other half of every two-token position its own record, so a holding that was paid nothing at a settlement now closes instead of staying open forever (why). After the re-derivation that follows it, expect on production:
- Eight new records and six corrected balances, across four positions and three accounts. Nothing is deleted.
- Three reported figures change, and only three — for THIS release. One account's fixed-income day 2026-05-14 goes from +$29,424.91 to +$187.42 (its whole reported lifetime for that book, from +$29,769.87 to +$532.38); a second account's 2026-05-03 goes from −$29,927.29 to +$49.81 (lifetime −$29,491.89 to +$485.20); and the first account's ether book on 2026-07-21 goes from +41.36 ETH to −0.17 ETH (lifetime +41.57 to +0.04). Every other day on every account is unchanged to the cent: 2,564 of the 2,567 day-and-book figures the accounts hold. A fourth figure moving is a shape nobody has measured: stop and report it.
- All three are restated AGAIN by the Fluid tick-padding re-mark, which is a later release and a separate prod step, and the figures above are only the reading between the two. After that step: 2026-05-14 reads −$14.91 (lifetime ≈ +$280.04), 2026-05-03 reads −$10.47 (lifetime ≈ +$375.92), and the ether book on 2026-07-21 reads −0.20 ETH (lifetime ≈ +0.02). It also moves more cells than three: 13 across the corpus by more than $0.02, because the padding is released at every Fluid close and not only at these three. Its own section carries the full table, and the "stop and report a fourth" rule above applies to this release taken before that one lands.
- The three DAY figures are exact; the lifetime figures are only exact to the tip. A lifetime total sums every day up to and including the live one, so it moves on its own as the day advances, with no code change: the same reading taken a day apart moved one of the figures above by about $1.60. Reconcile the release on the three DAYS, which are fixed, and read the lifetimes as approximate.
- No holding is left open at a settlement. The sweep for a position holding a non-zero balance with no closing record, beside a sibling that did close, returns ten rows today and zero after. It is the cheapest single check that the release landed.
- Withheld-day counts fall and none rise: the "endpoint could not be read" class goes from 21 to 14 across the corpus (12 to 7 on one account, 7 to 5 on another, 2 unchanged on a third). The "composition could not be resolved" class keeps its 4 existing rows and gains 4 more that are RESOLVED rather than withheld, one per closing position. The ghost cross-check's and the adjudicator's populations do not move at all; if they do, the change is on a plain token holding and is a different shape — stop.
- Both write-time refusals fire zero times, and so does the new "the venue did not answer for this side" report. Any of them firing means a block where the reading failed, and the remedy is to re-run that range with the archive endpoint present, never to write the row anyway.
- The withhold budget must be re-set from the post-release run, not carried forward: the re-derivation changes rows on three accounts, so any figure taken before it describes a ledger that no longer exists (how).
What a restatement moves. It writes zero into the row's quantity. A reading stores no money value (migration 129): its value is computed at read from the quantity, and a zero quantity is valued at zero on both lines.
Getting these the wrong way round is unrecoverable in one direction: restating a hole overwrites a correct record with a zero the chain contradicts, the finding goes quiet, and the gate passes on a ledger that is still wrong. So nothing decides from the shape, from the sign of the ledger's balance, or from which record looks more plausible. One third party decides: the chain, read at the contradicted row's own block. A bare token balance is an integer, and the question is any at all? rather than how much?, so the rule carries no tolerance to tune.
cd /opt/onchain-credit && set -a; source .env.local; set +a
# investigate one wallet (read-only, and the only mode --wallet allows)
npx tsx scripts/ops/adjudicate-ghost-rows.ts --wallet 0x…
# adjudicate the whole tracked population and restate what the chain has falsified
npx tsx scripts/ops/adjudicate-ghost-rows.ts --applyIt is read-only without --apply, and it refuses to run at all without an explicit ETHEREUM_ARCHIVE_RPC_URL — the verdict is the archive read, so falling back to the shared default would decide a restatement on the provider this programme convicted of serving incomplete history. The endpoint's host is printed on the run, so the evidence names who answered.
Naming the provider is not the same as checking it, so --apply requires a second one: LEDGER_VERIFY_RPC_URL, the same variable the ingester's scan-receipt cross-check uses, on a different host. Every contradicted row is then read on both. A disagreement is treated as this run cannot judge this row rather than as a tiebreak — there is no third opinion to prefer with, and what is known is only that one of them is wrong — so a single disagreement anywhere refuses the whole run. A dry run is happy with one endpoint, because it decides nothing.
The population it acts on is the one the finding names, and that is checked rather than assumed. The tool runs engine v2 over each wallet, from the same input the served reader builds, and compares the ghost points the engine raised against the rows it enumerated, point by point. Any difference in either direction refuses the run: a row the engine never named would be a restatement behind no finding, and a named point the run did not look at would let a repair report nothing left while the reconciler still fails.
When it refuses. Exit 2, and it writes nothing at all. Every applicable line is printed, not just the first:
| Line | What it means |
|---|---|
REFUSED … the chain agrees with the SPINE | at least one row is a derivation defect. Fix the derivation and re-derive; do not restate the record that is right |
REFUSED: … the adjudicated rows are not the ghost points engine v2 raised | the run's population and the finding's population differ; the set-drift lines above name the points. An engine question, not a data one |
REFUSED: … wallet(s) are not certified complete | a wallet's coverage does not certify, so W7 withholds its whole grid and it has no ghost finding to repair. Certify it, or use --wallet to investigate |
REFUSED: … not backed by exactly one stored row | zero rows means the relation moved under the run — re-run. More than one means the leg exists under two venues at one window, which re-running will not change: that one needs a look |
REFUSED: … stored at a block the verdict was not read at | the row describes a different moment from the evidence. Investigate before anything is written |
… provably wrong and this run is read-only | there is work to do and nobody asked for it. Re-run with --apply |
compare-and-set missed … | the row no longer matches what the run read — its quantity, its block or a mark moved between the verdict and the write, so it was left alone. Re-run |
The tracked population is the wallet-enrolment universe — every address any ledger arm has been told to scan, which is wider than the reconciler's certified set. As accounts sign in it will include wallets whose history is knowingly truncated; each of those refuses on its own certification line, so a whole-population --apply needs the enrolled set to be certified, not merely quiet.
The refusal is run-wide, not per row, and --apply is refused together with --wallet for the same reason. The reason is not that a partial repair would turn a gate falsely green — the finding still fires on every pair the holed wallets carry. It is that the acceptance figures were taken against the stored snapshot as it is now: restating rows moves it, and re-opens them. One re-take should cover the repair and the re-derivation together rather than paying for two. Narrowing the population would reach the same place by another route, so investigating one wallet is free and writing is all-or-nothing.
That wait is not free. Until the derivation is fixed and the wallets are re-derived, the product keeps serving the stale rows: on the population measured in R10-A, one wallet's $534 holding across 25 windows that the chain says was never there. The exposure is stated rather than discovered, and the step that clears it is the re-derivation.
What a restatement writes. The row's quantity and both marks go to zero, never to NULL and never deleted: NULL means the price read failed and would trade one finding for another, and deleting would assert the refresher never read the leg — a reading gap, which is not what happened, and which can silently remove a grid point when that leg was the only one at its window. A zero-quantity row is a shape no ordinary writer produces (the refresher emits nothing for a zero balance), and it is the intended one: it says read, and there was nothing there, which is exactly what the chain answered.
Each write happens under the portfolio write lock, as a compare-and-set against every value the run read: the quantity, both marks, and the block the evidence was taken at. The chain reads take minutes on a real population and cannot be held inside one transaction, so this is what carries the run's checks across them — the 6h refresh rewrites a row's block while leaving a stale quantity identical, and a re-mark rewrites a mark while leaving both alone, so neither is visible to a guard on the quantity by itself. A row that moved updates nothing, is reported, and exits 2; nothing is overwritten on a value nobody looked at, and the before/after printed is the row that was actually replaced — on the console line, and in the restatements array under --json. Re-running is a no-op: a zeroed row no longer contradicts anything.
Staging runs the ingester as a BOUNDED HAND-RUN, never as a process
There is no creddit-event-ingester-staging, and there must not be one: staging runs no refreshers, and an always-on process nobody deletes becomes the permanent staging refresher that policy exists to prevent. pm2 restart creddit-event-ingester is a PROD command — the only ingester process on the box runs from /opt/onchain-credit — so it never appears in a staging procedure. The prod deploy runs it on every release; deploy-staging.yml must never, and a test asserts it does not. The inverse trap is worse than the prod one: on staging the new code does not run against an old binary, it does not run at all, and the silence reads exactly like a clean cycle.
Exercise the tip path with an attended, foreground, time-boxed run from the staging checkout. Pre-flight by READING the target, never assuming it:
grep ^DATABASE_URL= /opt/onchain-credit-staging/.env.local # must name creddit_staging
cd /opt/onchain-credit-staging
# (a) N discrete cycles. The sleep matches the loop's own cadence: without it, cycle 1
# consumes the backlog and the rest scan a head that has moved by seconds.
for i in 1 2 3 4 5; do INGEST_SINGLE_CYCLE=1 scripts/ingester/run-ingester.sh; sleep 60; done \
2>&1 | tee /tmp/staging-ingester-$(date -u +%FT%H%M%SZ).log
# (b) a time-boxed soak, when the evidence needs wall-clock (receipt contiguity).
timeout -s TERM 60m scripts/ingester/run-ingester.sh \
2>&1 | tee /tmp/staging-ingester-$(date -u +%FT%H%M%SZ).logIt is never added to PM2 and never left running. Record the command, the start and end times and the number of cycles in the run log — a soak whose length nobody wrote down is not a measurement. Teardown doubles as proof prod was untouched: zero PIDs with a cwd of /opt/onchain-credit-staging, and pm2 list showing the same processes as before.
pgrep -af 'scripts/ingester/ingest-events\.ts' | while read -r p _; do echo "$p $(readlink /proc/$p/cwd)"; doneTWO lines with cwd
/opt/onchain-creditis the NORMAL prod state.run-ingester.shexecs thetsxlauncher andtsxruns the script in a CHILD process, so every invocation shows two PIDs. If a PID with the STAGING cwd survives,killthat PID — never by name, which would match prod's two.
A new wallet's enrolment backfill: how long, and when it is a symptom
Registering a wallet enrols it in the wallet-token stream, and the ingester backfills that wallet's bare-token history from that wallet's own history floor — the rolling 30-day window's start (accounts.history_floor_block, migration 103), or 2026-01-01 for a wallet that has none recorded. The budget is 200,000 blocks per wallet per ~60s cycle, given to each of up to three wallets at once — nothing is split between them. So the wait is ceil(span / 200,000) cycles, and the span is what the window decides:
| the wallet | span to walk | the wait |
|---|---|---|
| added under the rolling window (the ordinary case) | ~216,000 blocks (30 days) | ~2 cycles ≈ ~1–2 minutes |
| the 2026-01-01 floor (tracked before the rolling window, recorded by migration 104) or no recorded floor (a failed boundary resolution) | ~2.1M blocks (2026-01-01 to head) | ~11 cycles ≈ ~11 minutes, growing by a day's blocks per day |
Before the rolling window it was the full ~3.25M blocks from the 2025-05-21 ingestion floor, i.e. ~17 cycles ≈ ~17 minutes, for every new wallet. Beyond three wallets it is ceil(N/3) rounds, because the slots are handed out in a fixed order and a fourth wallet makes no progress until one ahead of it finishes. That order is the wallet address, ascending — not the order people signed up in — so a fresh registration can sit behind older wallets that are still catching up. It is sorted rather than arbitrary so that "which wallets are advancing" reads the same way from one cycle to the next in the log. Each figure is a floor: a cycle is max(60s, work).
The certificate clears when the catch-up FINISHES, and from_block is not what to watch. What the wallet's completeness certificate asks is two things at once: that the row's status is live, and that its from_block reaches that wallet's own history floor. The second is satisfied from the very first committed window and never moves: planCatchUpWindows walks upward, starting at the wallet's floor and climbing toward the live cursor ~200,000 blocks a cycle, and every committed window stamps the row {from_block: the wallet's floor, to_block: <window top>, status: 'backfilling'}, which stampCoverage merges with LEAST(from_block). So from_block reads the floor from cycle one and crosses nothing. What actually clears the certificate is the flip to status = 'live', written once, only when the walk reaches the live cursor. There is no earlier point at which the wallet is publishable.
An existing coverage row's bottom is the pass's floor, in BOTH directions.catchUpPassFloor hands back the row's own from_block whenever there is a row, and the floor it was given only for an address that has none. Both directions are the same defect from opposite sides, and only the row's own bottom avoids both. A floor ABOVE the row's bottom (the wallet's floor rose while its catch-up was in flight) would skip the blocks in between, which LEAST(from_block) leaves certified and unscanned. A floor BELOW it (the only way to be handed one is a degraded read falling back to 2026-01-01, since a stored floor is written once) would be worse: planCatchUpWindows only ever walks UPWARD from the row's confirmed tip, so nothing beneath the row's bottom would be read, while LEAST would happily LOWER the row's claim to that floor — permanently certifying, on prod today, up to ~1.76M blocks of bare-token history nobody scanned, after which a derivation opening there books missing deposits and withdrawals as return. Deepening an existing row is an ops campaign (backfill-event-ledger.ts, record-ledger-backfill-band.ts), which scans what it stamps. The only wallets that see the fast path are ones with no coverage row yet.
A wait past ~5 minutes for a rolling-window wallet (or ~20 for one with no recorded floor), with three or fewer wallets in catch-up, is a symptom. With more, expect ceil(N/3) rounds and read the ingester log for which addresses are advancing — [ingester/wallet-token] catch-up progress <address> -> <scannedTo>/<liveCursor> per advancing wallet, and N wallet(s) pending catch-up; advancing 3 this cycle when the queue is over capacity. Healthy progress on other wallets while the one you are waiting on has not started is the shape that matters. Until the backfill lands, that wallet's coverage certificate withholds rather than publishing partial numbers; no operator action moves it along, and none is needed.
Tune the slot count with INGEST_CATCHUP_MAX_WALLETS (default 3). It is separate from INGEST_CATCHUP_MAX (the token catch-up) on purpose, so a signup never queues behind a newly-listed vault. Do not change INGEST_CATCHUP_WINDOW or INGEST_CATCHUP_WINDOWS_PER_CYCLE without re-deriving the figures above: every published number for this wait comes from their product.
The pre-window entry lookup: its caps and its cost
At the end of every wallet build, after the floor openings, backfillWallet walks the wallet's own token transfers back from the window start for each holding whose history opens on its opening (M32 "Entry found before the window"). It is archive work on ETHEREUM_ARCHIVE_RPC_URL, bounded per wallet build by named constants (src/lib/portfolio/prewindow-entry-caps.ts):
| constant | value | what it bounds |
|---|---|---|
PREWINDOW_LOOKBACK_BLOCKS | 2,628,000 (about 365 days) | how far back from the opening the walk reads |
PREWINDOW_ARCHIVE_CALLS_PER_WALLET | 600 | log queries, receipts, balances and block times, together |
PREWINDOW_MAX_ACQUISITIONS_PER_LEG | 100 | receipts one holding may be built from |
PREWINDOW_MAX_HOPS | 2 | wraps followed back to their source |
PREWINDOW_DEADLINE_MS | 5 minutes | the walk's wall clock: a cap like the others (beyond-time); what was found before it is kept |
PREWINDOW_RETRY_AFTER_DAYS | 7 | how long a beyond-time verdict stands before a build walks the holding again |
The cost shape. Per 100,000 blocks walked, 4 eth_getLogs calls per wallet, whatever the number of holdings (two 50,000-block windows, the wallet as sender and as recipient), more only where the provider halves a window. Per acquisition found: 1 eth_getTransactionReceipt, plus 2 where ether moved and 3 pinned Lido reads where stETH moved (kept in portfolio_fill_inputs, so a re-walk reads none of them again), and 1 block header where the provider states no block time. The resolver's block-pinned rate reads (one eth_call per acquisition per rate) are inside the deadline, checked before each holding is valued, but outside the call cap. A holding bought 60 days before the window costs about 21 calls (18 log queries, and its purchase's receipt, transaction and trace); one beyond the lookback, 106 log calls, a beyond-blocks verdict and no receipt stored. A rebuild of a wallet whose openings did not move makes no archive call: a settled verdict is kept as it is.
The line to read. Each build prints one line: [backfill] <wallet>: pre-window entries: F of N holding(s) found (W walked, K kept; C archive call(s), E ms, lowest block L, ...); dash kept for <key> (<verdict>: <reason>). Three failure families read differently on it:
- a cap (
beyond-blocks,beyond-calls,beyond-hops,beyond-acquisitions) is a verdict on the line, kept until that cap is raised; a raised cap re-walks only the holdings whose verdict was that cap; - the deadline shows as
beyond-timeon the holdings that had not settled, beside the ones found before it, which are kept; they are walked again once the deadline is raised or afterPREWINDOW_RETRY_AFTER_DAYS; - an archive error prints
nothing written, every holding keeps what it had: <cause>and stores nothing; the next build walks again.
A staging build whose line shows the walk near its 5-minute deadline (E approaching 300000 ms), or any beyond-time, is the signal to lower PREWINDOW_LOOKBACK_BLOCKS, the one constant that scales the log calls; do not raise the deadline, which keeps a wallet build within its time budget (the queue child's 30-minute timeout, with today's builds at about 1.5 minutes).
Reorg repair (what the ingester does by itself, and what an operator does)
Each cycle, before any scan, the ingester re-reads the headers of the blocks it holds that the chain has not finalized, and repairs any height whose hash has changed: orphaned raw_events rows deleted by predicate, the canonical header stored, the covering chain_scan_ranges receipts deleted, and the affected cursors (plus the cron's ledger dirty tip) rewound to just below the lowest repaired height. It logs [ingester/reorg] REORG at block(s) …. Nothing is required of an operator for an ordinary reorg; the next cycle re-scans and the next 6h tick re-derives the wallets in the range.
Two things to check if that line ever appears repeatedly for the same height: that the provider serving ETHEREUM_RPC_URL is not flapping between forks, and that INGEST_REORG_SWEEP_MAX (default 256 headers/cycle) is not being hit — the sweep repairs lowest-height-first and leaves the remainder to the next cycle, so a persistently truncated sweep shows as slow progress rather than as an error.
Optional, and recommended before the ledger is relied on: set LEDGER_VERIFY_RPC_URL to a SECOND archive-capable provider. Every live range is then re-scanned against it and the two log digests compared before the receipt is written; a match stamps verified_by, a mismatch holds the cursor and logs loudly. Without it the receipt digest only proves the ledger is self-consistent, which does not catch a truncated eth_getLogs that returned HTTP 200 with half the range — the ingester logs that state once per process at startup.
Deleting portfolio users: every account, or some wallets
A user delete removes accounts and everything that hangs off them. It has two shapes: every account, with the tool (scripts/ops/reset-portfolio-users.ts), and some wallets, by hand (the tool deletes every account or none). Both are prod writes that need explicit approval, a data-only backup first, and a slot between 6h ticks: the refresher runs at 50 */6 UTC and reads its population minutes before it writes, so an account deleted in between aborts the rest of that tick's ledger work (it heals on the next tick, but there is no reason to cause it). The dated runs are in the release-steps log.
What a delete must reach. Deleting an accounts row cascades (migration 056) through the tracked-wallet rows, the readings, the ledger, the wallet index, the backfill state, the pre-window PT fills, the pre-window entry receipts and their verdicts (migration 159) and the assistant's tables. Five things are NOT reached by the cascade, and each takes its own statement:
- the per-wallet cursor rows in
chain_scan_cursors: the familyportfolio:derive:v2:<chain_id>:<wallet>[:<suffix>](the derive cursor, the pending marker:jit-pending, the replay's deferral marker:replay-deferred, the reading audit's:audited-throughwatermark and:native-listing-waitmarker, the continuous producer's:followed-afterrecord) and the retiredportfolio:discover:<wallet>. A survivor claims a wallet is derived or discovered over rows that no longer exist. This is the wipe-on-reset family: it holds per-wallet state only, and a campaign-global value lives underportfolio:v2-campaign:, which nothing here touches (nor the worker's chain-wide rows,portfolio:worker:*); - the derivation queue,
portfolio_derive_jobs(migration115, no foreign key by design): a deleted wallet's job would fail its merge on the ledger's foreign key, page, and write the wallet's pending marker back; - the wallet's
wallet-tokenandnative-ethcoverage rows inevent_coverage(one per wallet each; the native-ether block feed's since migration118), from which the ingester re-enrols a wallet every cycle (the streams''*'markers are chain-wide and stay); portfolio_held_pts, which is keyed by PT, not by wallet: cleared on a full wipe only;- Portfolio recovery receipts,
history_recovery_itemswhereseries = 'portfolio_position_snapshots':item_keyholds the wallet address without a foreign key. Remove these only for deleted users; market-history receipts stay intact.
The ledger worker is stopped for the whole delete. A job it has in flight would write back what the delete removes, after the delete has committed. The tool refuses --execute while a worker runs (it checks the worker's own lock, and holds that lock through its transaction so none can start mid-wipe); the hand recipe checks the same lock. And the ingester's continuous producer holds its wallet population for up to five minutes (INGEST_CONTINUOUS_POPULATION_TTL_MS), so until the ingester restarts it can still queue a job for a deleted wallet: both recipes restart the ingester, clear the queue once more, and only then start the worker.
Every account:
# PROD, between 6h ticks. 0. stop the worker (the tool refuses --execute otherwise), and save it
# stopped, so a reboot inside the window does not bring it back running.
pm2 stop creddit-ledger-worker && pm2 save
# 1. back up what is about to be deleted. --table-and-children (pg_dump 16), because -t on a
# partitioned table dumps the parent, which holds no rows.
sudo -u postgres pg_dump -d creddit --data-only \
--table-and-children=onchain_credit.accounts \
--table-and-children=onchain_credit.account_wallets \
--table-and-children=onchain_credit.portfolio_position_snapshots \
--table-and-children=onchain_credit.portfolio_flow_events_v2 \
--table-and-children=onchain_credit.history_recovery_items \
> /root/backup-users-$(date +%F).sql
# 2. the wipe: a dry run (it deletes, reports, verifies and rolls back), then --execute, which
# requires --confirm-db (matched against current_database()). Through the socket: without a
# host, node-pg dials TCP and the postgres role has no password there.
sudo -u postgres bash -lc 'cd /opt/onchain-credit && \
DATABASE_URL="postgres://postgres@/creddit?host=/var/run/postgresql" \
npx tsx scripts/ops/reset-portfolio-users.ts'
sudo -u postgres bash -lc 'cd /opt/onchain-credit && \
DATABASE_URL="postgres://postgres@/creddit?host=/var/run/postgresql" \
npx tsx scripts/ops/reset-portfolio-users.ts --execute --confirm-db=creddit'
# 3. copy .env.local, then rotate SESSION_SECRET in it (a fresh `openssl rand -hex 32`), so no
# old cookie resolves to a deleted account. Restart the app WITH THE FILE SOURCED: pm2's stored
# process environment already holds SESSION_SECRET and Next never overrides a variable that
# is set, so a bare `pm2 restart --update-env` keeps the former secret live. Then restart the
# ingester (it drops its in-flight enrolment backfill and its producer's population).
STAMP=$(date +%Y%m%d%H%M); cp /opt/onchain-credit/.env.local /root/.env.local.bak-$STAMP # before editing
cd /opt/onchain-credit && (set -a; . ./.env.local; set +a; pm2 restart onchain-credit --update-env)
pm2 restart creddit-event-ingester --update-env
# 3a. REQUIRED: prove the former secret is dead and the new one live. Cookies signed for an
# address that never had an account, never a deleted wallet: the former secret's must get 401
# ("Not signed in."), the new one's must not. The check fails loudly when either secret
# cannot be read or both are the same, so a misread secret cannot pass as a dead one. It
# prints no secret; neither does anything else here (`pm2 env` prints `KEY: value`, never run
# it unfiltered).
cd /opt/onchain-credit && BAK=/root/.env.local.bak-$STAMP node -e '
const fs = require("fs"), crypto = require("crypto");
const read = (f) => {
const m = fs.readFileSync(f, "utf8").match(/^(?:export\s+)?SESSION_SECRET=\s*["\x27]?([^"\x27\r\n]+)/m);
if (!m) throw new Error(`no SESSION_SECRET in ${f}`);
return m[1].trim();
};
const former = read(process.env.BAK), current = read(".env.local");
if (former === current) throw new Error("SESSION_SECRET was not rotated");
const a = "0x000000000000000000000000000000000000dead";
const cookie = (k) => `creddit_session=${a}.${crypto.createHmac("sha256", k).update(a).digest("hex")}`;
(async () => {
const status = async (k) => (await fetch("http://localhost:3001/api/portfolio/summary",
{ headers: { cookie: cookie(k) } })).status;
const f = await status(former), c = await status(current);
console.log(`former secret: ${f} (want 401), new secret: ${c} (want not 401)`);
if (f !== 401 || c === 401) { console.error("FAIL: rotation did not take"); process.exit(1); }
console.log("PASS");
})();'
# 4. what the tool does not own. AFTER the restart, or the still-running ingester re-writes the
# coverage rows underneath the DELETE. Then the jobs its producer queued for the deleted
# wallets before the restart.
sudo -u postgres psql -d creddit -c \
"DELETE FROM onchain_credit.event_coverage WHERE stream IN ('wallet-token', 'native-eth') AND address <> '*';"
sudo -u postgres psql -d creddit -c "DELETE FROM onchain_credit.portfolio_held_pts;"
sudo -u postgres psql -d creddit -c "DELETE FROM onchain_credit.history_recovery_items
WHERE series = 'portfolio_position_snapshots';"
sudo -u postgres psql -d creddit -c "DELETE FROM onchain_credit.portfolio_derive_jobs j
WHERE NOT EXISTS (SELECT 1 FROM onchain_credit.accounts a WHERE a.uid = j.wallet);"
# 5. start the worker and save it running (step 0 saved it stopped), then confirm the ingester
# enrolled nothing. WAIT ONE 60s CYCLE: the line prints only when the count CHANGES, so the
# pass is the drop (`enrolled wallets: 11 -> 0`).
pm2 restart creddit-ledger-worker --update-env && pm2 save
sleep 70
pm2 logs creddit-event-ingester --lines 50 --nostream | grep 'enrolled wallets'With no wallet enrolled, the wallet-token stream is not emitted, so the ingester alarm's feed-lag arm pages about 6h later until a wallet enrols, and the 6h reconciler pages measured-nothing until one is tracked. Neither is a fault.
Some wallets. First check that no account you are KEEPING tracks one of them, or the tracker re-creates that wallet's account at its next load:
-- read-only: must return no rows
SELECT w.account_uid, w.wallet FROM onchain_credit.account_wallets w
WHERE w.wallet IN ('<lower-cased uid>', …) AND w.account_uid NOT IN ('<lower-cased uid>', …);Then stop the worker and save it stopped (pm2 stop creddit-ledger-worker && pm2 save, so a reboot inside the window does not start it), take the backup above, and delete in one transaction. The first statement refuses while a worker holds its lock, and then holds that lock itself until the commit:
BEGIN;
DO $$ BEGIN
IF NOT pg_try_advisory_xact_lock(hashtextextended('onchain_credit.ledger_worker', 0)) THEN
RAISE EXCEPTION 'a ledger worker is running: pm2 stop creddit-ledger-worker, then start again';
END IF;
END $$;
CREATE TEMP TABLE doomed (uid text PRIMARY KEY) ON COMMIT DROP;
INSERT INTO doomed VALUES ('<lower-cased uid>'), ('<lower-cased uid>');
DELETE FROM onchain_credit.event_coverage
WHERE stream IN ('wallet-token', 'native-eth') AND address IN (SELECT uid FROM doomed);
DELETE FROM onchain_credit.portfolio_derive_jobs WHERE wallet IN (SELECT uid FROM doomed);
DELETE FROM onchain_credit.chain_scan_cursors
WHERE (scope LIKE 'portfolio:derive:v2:%' AND split_part(scope, ':', 5) IN (SELECT uid FROM doomed))
OR (scope LIKE 'portfolio:discover:%' AND split_part(scope, ':', 3) IN (SELECT uid FROM doomed));
DELETE FROM onchain_credit.history_recovery_items
WHERE series = 'portfolio_position_snapshots' AND item_key IN (SELECT uid FROM doomed);
DELETE FROM onchain_credit.accounts WHERE uid IN (SELECT uid FROM doomed); -- cascades
COMMIT;The cursor rows are selected by the wallet's own part of the scope, whatever suffix a record carries: the fifth colon-separated part of the derive family, the third of the discover family. Matching the scope's last 42 characters instead, as the 2026-09-22 hand delete did, matches the bare derive cursor alone and misses every suffixed record. scripts/ops/scrub-staging.test.ts pins this selector against every per-wallet scope the code writes. Then, as for every account: rotate SESSION_SECRET (a still-valid cookie would re-create a deleted wallet's account through the assistant's route), restart the app with .env.local sourced and the ingester, prove a former-secret cookie gets "Not signed in" (steps 3 and 3a above), clear the jobs the producer queued before its restart (the NOT EXISTS delete above), and start the worker and save it running (pm2 restart creddit-ledger-worker --update-env && pm2 save).
Running a cron/refresher by hand
scripts/run-cron.sh is the wrapper for every scheduled TS job: it sources .env.local, resolves node_modules/.bin/tsx, runs scripts/<script> from the repo root, and appends output to /tmp/onchain-credit-cron/<checkout>-<script>.log. Extra args are forwarded to the script. It takes a non-blocking per-script flock under the same name ($CRON_LOG_DIR/<checkout>-<script>.lock), keyed by the checkout: a second run of the same script in the same checkout while the first is still going logs "skipping this tick" and exits 0 (so a slow 6h job never stacks a copy on itself), while the prod and staging checkouts get distinct lock files and never block each other. The script's real exit code is preserved for cron. NODE/TSX/CRON_LOG_DIR fall back to the box defaults (/usr/bin/node, the checkout's tsx, /tmp/onchain-credit-cron) but are env-overridable for local testing (scripts/run-cron.test.ts runs the wrapper end-to-end through them). The log, the lock and the toolchain marker below are all keyed by checkout, so a tail of a job's log is one environment's history. (The log was keyed by script alone until the release that carried this change — prod and staging interleaved in one file per job — and each environment switches on its own next deploy, staging first. The first tick of each job after that opens a new file; the old shared ones stay in the directory, unwritten, until /tmp is cleared.)
Toolchain guard (deploy window). A deploy rebuilds the checkout's node_modules in place (npm ci removes the tree before repopulating it) while the crontab keeps firing, and both checkouts drain the backfill queue every minute. A tick landing in that window finds no tsx and dies with MODULE_NOT_FOUND: a job that never started, not a job that failed. The wrapper skips such a tick and exits 0, exactly like the flock skip, logging "deploy in flight, skipping this tick" (and, when stderr is a terminal, saying so on it too, so a hand-run is never mistaken for a job that succeeded).
This removes the common case, not every deploy-window page: npm ci links .bin before the whole tree is reified, so a tick landing after tsx appears but before its dependencies do still fails on a different module, and there is an unavoidable race between the check and the exec.
No log line carries a credential. Node providers put the API key in the URL itself, and several jobs print the endpoint they are about to use as a start-of-run breadcrumb; the registration backfill printed one per wallet. On 2026-08-06 a validation agent found the archive provider's key in plain text in /tmp/onchain-credit-cron/drain-portfolio-backfills.log (the shared name the log carried then), which was mode 0644 on the prod box. The permissions were tightened, and that is a mitigation, not a fix: the wrapper also forwards matching log lines into the Telegram alert, so a leaked line need not stay on the box. Every such site now prints through redactUrl/redactedEndpoint (src/lib/redact.ts), which keeps the scheme, host and port (the part that answers "which provider ran this?") and replaces anything credential-shaped: a key in the path, a key in a query parameter, userinfo before the host. It over-redacts by design. A NEW job that logs an endpoint must go through the same helper — the value should never reach the log in the first place, whatever the file's permissions are. The 2026-08-06 key still wants a rotation decision (it was never reproduced anywhere, including in the campaign's own report). So does the key the event ingester's start line printed, in its live and archive URLs, on every start before the ledger-first release (#954 follow-up 1): that decision is Fred's, and it is that release's step 10, with the count of the lines that still hold the key and the log they sit in.
The skip is bounded, so a checkout whose install genuinely never came back cannot go quiet forever. $CRON_LOG_DIR/<checkout>-toolchain-missing.since holds two stamps, the episode's first miss and its most recent one, and the wrapper escalates only when misses were observed continuously for longer than TOOLCHAIN_GRACE (default 900s, ~15x the measured npm ci): it then exits non-zero with a [fail] line leading with the fix command, which is both halves the alert below needs. Any tick that finds tsx clears the marker. A gap longer than the grace since the last observed miss also restarts the window, which is what keeps a stamp left behind by a paused or not-yet-added cron from making the next healthy deploy page instantly.
Cron-failure alerting (WS8). On a non-zero exit, the wrapper best-effort POSTs a Telegram message naming the checkout, the script, and the exit code, so a failed job is not silent. It is gated on ALERT_TG_BOT_TOKEN + ALERT_TG_CHAT_ID (both must be in .env.local; if either is unset it skips silently, so an un-provisioned box never posts). The alert runs with errexit off and a time-bounded curl, so it can never change the job's real exit code or block the wrapper (a failed POST is logged as "ignored"). Because prod and staging share the box, the checkout label ($(basename "$DIR"), or the ALERT_ENV override) tells you which environment failed. This covers every cron job. Separately, the portfolio refresher posts its own unknown-asset alert to the same bot when an accounting asset it holds with value is unmapped in buckets.ts (see Data pipeline → refresh-portfolio.ts). To exercise the alert path locally, set ALERT_TG_API_BASE at a stub endpoint (defaults to https://api.telegram.org).
A job may send its own message instead. Before each run, once it holds the lock, the wrapper deletes $CRON_LOG_DIR/<checkout>-<script>.alert and hands the job three paths in its environment: CRON_ALERT_BODY_FILE (that file), CRON_STATE_FILE (<checkout>-<script>.state.json, the job's own memory between runs, which the wrapper never reads) and CRON_LOGFILE. On a non-zero exit, a job that wrote the body file has that text sent instead of the generic message; one that wrote none gets the generic message; and if Telegram does not accept the job's text (it answers anything but "ok":true, for instance a message over 4,096 characters), the wrapper logs that and sends the generic message, so a page is never lost to it. A zero exit sends nothing either way. The ledger self-check, the quarantine page, the quiet market-price alarm and the Fluid collateral breakdown alert write their own bodies; other jobs ignore the variables and use the generic alert.
Each delivery receipt ends its own line. Telegram answers with a one-line JSON receipt and no trailing newline. Appended as-is, it glued the next run's banner onto it (}--- Tue Sep 29 07:30:01 AM UTC 2026 ---), the why: slice's anchor (a line that is a whole banner) stopped matching, and every later alert quoted older runs' lines and receipts (issue #992). The wrapper now writes the receipt with its own newline, so each banner starts a line and each why: slice is one run again, for every job. A log written before the fix keeps its glued lines, and so does the first run after it (its banner lands on the old wrapper's last receipt), so that run's alert may still quote older runs once; from the second run on every slice is clean.
cd /opt/onchain-credit
scripts/run-cron.sh refresh-assets.ts
scripts/run-cron.sh refresh-vault-capacity.ts
scripts/run-cron.sh refresh-sofr.ts --since=2018-04-03 # args pass through
tail -f /tmp/onchain-credit-cron/onchain-credit-refresh-vault-capacity.logServer crontab (all UTC):
| Schedule | Command | Cadence |
|---|---|---|
0 */6 * * * | run-cron.sh refresh-assets.ts | every 6h |
15 */6 * * * | run-cron.sh refresh-vault-capacity.ts | every 6h |
30 */6 * * * | run-cron.sh refresh-collateral-exposure.ts | every 6h. Pages with its own message |
45 */6 * * * | run-cron.sh refresh-lending-positions.ts | every 6h |
50 */6 * * * | run-cron.sh refresh-portfolio.ts | every 6h (WS4; manual crontab add) |
5 * * * * | run-cron.sh check-ingester-freshness.ts | hourly (manual crontab add). Four arms: pages when the event ingester is older than the checkout's build, not online, or missing; and, behind a process that is healthy so its own page is not displaced, when any followed event feed has fallen more than 6h behind the chain, when the ledger worker has stopped or fallen behind, and when a reading audit booked an unexplained correction or a leg went unread twice, a reading was recorded that cannot be valued, or its own valuation failed LEDGER_AUDIT_SOURCE_PAGE_AFTER looks in a row (default 2). See the ingester alarm and its readable page. |
* * * * * | run-cron.sh drain-portfolio-backfills.ts | minutely (WS5 queue drain). Exit 2 (= Telegram alert) on every failed backfill child, naming the wallet, and when a wallet is parked as error after exhausting its reclaim budget. A parked wallet is re-queued by the drain itself, never by a sign-in: after 15 min for writer contention, after an hour for any other park, four retries at most, each failure paging again; a timed-out or orphaned run is never retried and waits for you (the hourly retry). Every park's page says which: automatic retry k/4 in ~60min (or ~15min) while one is coming, and NEVER retried automatically or retry budget SPENT, no automatic retry is left, re-queue by hand when the wallet now needs you (backfill-portfolio-wallet.ts --uid <wallet>). The 6h portfolio tick warns if anything sits queued >30min (drain missing/broken, or a registration still waiting for its wallet's own transfer history, which the drain's log names every minute as [registration-wait]: the registration wait). |
*/10 * * * * | run-cron.sh refresh-portfolio-discovery.ts | every 10 min (portfolio taxonomy T4 §3.6; manual crontab add — apply 050 FIRST). Discovery enrollment drain: one FULL-universe current read per eligible wallet with no FRESH completeness certificate (accounts.discovery_scanned_*, migration 061), capped PORTFOLIO_ENROLL_MAX (default 10). Quiet no-op once every eligible wallet holds one. Selection is freshness-based, not presence-based, so it is also the REPAIR half of the staleness gate: a wallet demoted for a frozen certificate is re-certified within one run instead of paying the full-universe read tax indefinitely. Un-scanned wallets force the whole 6h batch to a full-universe read, so this is what makes the discovery bound ENGAGE for new signups. |
20 3 * * * | run-cron.sh refresh-morpho-universe.ts | daily (T4 §3.5; manual crontab add — apply 041 FIRST). Exhaustive Morpho Blue market universe via a cursor-driven CreateMarket scan. First run: set MORPHO_UNIVERSE_FROM_BLOCK to the Morpho Blue deploy block so it walks the full history; steady-state scans only new blocks. |
40 3 * * * | run-cron.sh refresh-metamorpho-factory.ts | daily (T5 §3.5; manual crontab add — apply 052 FIRST). Exhaustive MetaMorpho vault universe via a cursor-driven CreateMetaMorpho scan over BOTH factories. First run: set METAMORPHO_FACTORY_FROM_BLOCK to the v1.0 deploy block; steady-state scans only new blocks. |
30 4 * * 0 | run-cron.sh refresh-portfolio-reconcile.ts | weekly, Sun 04:30 (T5 §3.6; manual crontab add — apply 050 FIRST). Discovery reconciliation safety net: reads each SCANNED wallet against the COMPLETE universe and adds any membership the index missed, with a WS8 drift alert. Capped PORTFOLIO_RECONCILE_MAX (default 50), least-recently-reconciled first. Scheduled AFTER the two universe ingests so "the complete universe" really is complete. The one-time post-ingest step is in the taxonomy release step. Wallets rotate least-recently-RECONCILED first (accounts.discovery_reconciled_at, migration 061 — a column only this sweep writes, so the 6h tick's certificate advance cannot flatten the rotation). Exits non-zero on four conditions, each printing a [fail]-tagged line so the cron alert has both halves it needs: STARVED (eligible wallets present but an empty batch = the backstop checked nothing), an AGED DIVERGENCE (a membership the index lacked while the ledger already carried its flow — a membership the byproduct simply has not reached yet is repaired quietly, so a brand-new position never pages), a DANGLING LINK (counted as FOUND, not repaired, so a run whose repairs all failed pages more loudly rather than less), and an all-errors run. The dangling-link check runs BEFORE the batch selection, so an empty cohort — the situation a bulk account delete creates — cannot hide it. |
30 4 * * 1 | run-cron.sh sync-portfolio-tokens.ts | weekly, Mon 04:30. Portfolio coverage report: detection and proposals only; no onboarding, retirement or pricing writes. --approve refuses; approved changes follow the existing asset onboarding checklist. |
45 4 * * 1 | run-cron.sh check-oracle-cards.ts | weekly, Mon 04:45 (manual crontab add). Oracle card completeness and drift check: assembles the oracle card of every listed carry against the live chain and exits non-zero, with one [fail] line per card, when a card is incomplete, has drifted from a verified on-chain fact, or could not be read, so the WS8 alert pages. Read-only. See Data pipeline. |
50 3 * * * | run-cron.sh sync-money-market-funds.ts | daily (money market fund coverage, processes.md F; manual crontab add - apply migration 086 FIRST). Discovers every Morpho mainnet vault of both generations, confirms the listing facts on chain, and PROPOSES anything under an unapproved manager. Never lists a new house on its own. |
0 5 * * 1 | run-cron.sh refresh-token-market.ts | weekly, Mon 05:00 (manual crontab add — apply migrations 111 and 112 FIRST). The pricing categories' market limb: per tracked yield-bearing token, how many of the last 30 UTC days it traded on a DEX, the median daily volume over those same 30 days as the price vendor reports it (exchanges and DEXes together, with the on-chain median stored beside it), and the deepest single pool holding it against an asset of its own book or against a recognised counter of that book (a dollar stable at par, or ether and its liquid wrappers), never counting a side that is the pool's own share token. ONE Dune execution over every token (DUNE_QUERY_ID_DEX_MARKET) plus one CoinGecko market-chart call per measured row plus a pool-reserves walk per token (up to three pages of twenty, stopping at the first pool that clears the $1M bar or at the end of the vendor's list, because the endpoint will not sort by reserves), paced to the endpoint in use (with COINGECKO_API_KEY set, 30 a minute against CoinGecko's /onchain mirror; without it, four a minute against the keyless GeckoTerminal endpoint, which is what the vendor actually allows). A 429 is waited out and retried twice rather than costing the token its reading. Scheduled 30 minutes after the registry sync, so a row approved that morning is measured in the same hour rather than a week later. It DECIDES nothing: the six-hourly refresh-assets.ts reads the readings, and a category flip is a registry edit in a pull request. Exits non-zero when a whole LIMB wrote nothing — no Dune row for any token, no reported volume for any token asked about (a 404 is an ANSWER and not silence), or no pool reading for any token — each with its own [fail] line. A row counts as measured on any ONE of its readings, so the row count cannot see a limb that has stopped: an unset query id, a thrown execution or a pass throttled end to end all leave every verdict null for the week while two thirds of the rows still look fine. A single token the vendor could not answer for is not a failure; that token's verdict stays null rather than turning into a wrong answer. |
30 3 * * * | run-cron.sh refresh-vault-risk.ts | daily, 03:30. The screener's stored max LTV and liquidation threshold of every listed carry. Pages (WS8) when a run reads nothing: every read failed, or more than the shared 10% floor. |
0 13 * * 1-5 | run-cron.sh refresh-sofr.ts | weekdays 13:00 |
10 4 * * 1 | run-cron.sh chat-retention.ts | weekly, Mon 04:10 (chat retention, audit B6; manual crontab add). Prunes chat conversations idle >365d (messages cascade via the migration 038 FK) and chat_usage rows >400d, in one transaction. Portfolio history is untouched (retained in full by design). Prod only. |
25 2 * * * | run-cron.sh refresh-native-supply.ts | daily, 02:25 (manual crontab add). The native supply of each profiled asset whose home network is not Ethereum (PST on Solana), for the asset profile's TVL tab. Reads CoinGecko's reported supply (COINGECKO_API_KEY as everywhere; a Solana RPC instead where SOLANA_RPC_URL is set) and logs which on every run. Pages (WS8) only when every source failed. Its line. |
55 4 * * 1 | run-cron.sh check-profile-reviews.ts | weekly, Mon 04:55 (manual crontab add). The asset profile review alarm: one Telegram message listing every profiled asset whose review is older than 90 days; exits 0 when the list is sent or empty, non-zero when the table cannot be read, a stored record is invalid or the message was not delivered. Read-only. Its line. |
15 2 * * * | ops/backup-creddit.sh | daily DB backup, 02:15 |
0 3 * * * | ops/reseed-staging.sh | daily staging reseed from the 02:15 dump (pause: touch /root/.reseed-paused) |
| hourly | disk-usage alert | hourly |
The staggered :00 / :15 / :30 / :45 on the 6h jobs spreads RPC/API load. Note ops/backup-creddit.sh (02:15) runs ahead of any data job, so the daily dump is a quiet-state snapshot.
The Fluid collateral breakdown alert
refresh-collateral-exposure.ts writes its own message in the #1014 format (scripts/refreshers/collateral-exposure-alert.ts), after the snapshot commits. Two findings:
- A market held back: it kept its previous breakdown this run (its total borrowed could not be read, read zero while a vault that could borrow it was unread or still reported debt, had no stored price, or nothing in it could be attributed, or the whole run failed). The Repo Lending page withholds a breakdown older than 24 hours, so this pages every run, with when the kept breakdown was taken and when the panel goes dark.
- An unread vault: Fluid's own reader reverts for it, so its debt stays in its markets'
Unattributedslice while those markets refresh. The revert is decoded (FluidOracleErrorreads as "Fluid cannot price its collateral: the vault's price oracle refuses to answer"), the vault is named as Fluid names it (Fluid vault #153 osETH / USDC), and the message says what the Fluid row's Underwritten collateral panel shows for each affected market. Unread vaults alone page on the first run, at once on anything new (a vault, a cause), and then once a day while the same set persists (REMIND_AFTER_S, 23 h, so every fourth run); the runs between exit 0 with one line in the log. A recovery alone does not page; it is listed under RESOLVED SINCE LAST ALERT in the next message. A cause that is only a guess (the probe hit a transport error, or the vault answered on retry) is not news for a vault already alerted. A vault that borrows none of the markets the page shows never pages on its own: it is logged every run and named in any message something else sends.
⚠️ creddit alert: Fluid collateral breakdown (production)
Tue 20 Oct 2026, 12:30 UTC · runs every 6 h · first failure
RESULT
5 of 182 Fluid vaults could not be read.
No market held back: USDC, USDT and GHO refreshed with part of their debt unattributed; ...
UNREAD VAULTS
Cause: Fluid cannot price osETH collateral right now: each vault's price oracle refuses to answer
(Fluid oracle error 60403), so Fluid's own reader reverts for them.
• Fluid vault #153 osETH / USDC. Open since this run (first seen by this check).
...
WHAT USERS SEE (Repo Lending page, the Fluid row's expanded Underwritten collateral panel)
• Fluid USDC: the Unattributed slice of Collateral breakdown counts the unread vaults' debt
instead of showing it under osETH; it holds $12.4M of the $310.0M borrowed (4.0%), ...
On each of these panels the Available to borrow column is not shown, because an unread vault ...
Suggested fix: Nothing to do on our side. It clears by itself on the first run after the oracle
answers again, ...- Open since is the job's memory (
<checkout>-refresh-collateral-exposure.state.json): the first run this check saw the finding, and how many runs since. A memory lost to a reboot reads as a first failure; one that cannot be written makes the run page. Outside the wrapper there is no memory, so a run by hand with a finding always exits 1. The memory is saved before the wrapper delivers, and the job cannot see delivery: if Telegram refuses both the job's message and the generic one, the next reminder about an unchanged set comes a day later rather than at the next run (a held-back market or anything new still pages at once). - The generic fallback still names everything: one
[collateral-exposure/fail] unread Fluid vault #… pair: causeline per vault, then the aggregate line printed last (held-back markets first, then the vaults), each inside the wrapper's 220-character cut.
The asset profile jobs
Two jobs serve the asset profile (the plan): the daily native supply reader and the weekly review alarm. Their crontab lines are a server step: no deploy installs them and no rollback removes them. The lines, and their two-way check (the count names the checkout; the next run's log line proves it runs):
25 2 * * * /opt/onchain-credit/scripts/run-cron.sh refresh-native-supply.ts
55 4 * * 1 /opt/onchain-credit/scripts/run-cron.sh check-profile-reviews.ts
crontab -l | grep -c '/opt/onchain-credit/scripts/run-cron.sh refresh-native-supply.ts' # 1
crontab -l | grep -c '/opt/onchain-credit/scripts/run-cron.sh check-profile-reviews.ts' # 1
grep -a 'native-supply' /tmp/onchain-credit-cron/onchain-credit-refresh-native-supply.log | tail -n 2
grep -a 'profile-reviews' /tmp/onchain-credit-cron/onchain-credit-check-profile-reviews.log | tail -n 2Both read tables migration 145 creates; before it is applied the supply job's insert fails (a [fail] page) and the review job answers from the shipped seed, so install the lines after the migration.
psql
psql -U onchain_credit -d creddit # prod app role
psql -U onchain_credit_staging -d creddit_staging # staging
# inside psql
SET search_path TO onchain_credit;
\dt onchain_credit.*
SELECT max(snapshot_ts) FROM onchain_credit.assets;Migrations
DDL lives in scripts/sql/NNN-*.sql, applied in numeric order as the postgres owner (the app role is read-only against the data and lacks DDL rights). Applied files are tracked in onchain_credit.schema_migrations, and the runner scripts/ops/migrate.sh is the canonical way to apply pending ones:
# on the box — apply any pending migrations to a database, idempotently
cd /opt/onchain-credit
scripts/ops/migrate.sh "$(grep ^DATABASE_URL= .env.local | cut -d= -f2-)" # prod
scripts/ops/migrate.sh "postgresql://onchain_credit_staging@127.0.0.1:5432/creddit_staging" # stagingThe runner lists scripts/sql/*.sql in order, skips any already recorded in schema_migrations, applies the rest inside a transaction, and records them. It is flock-serialized with a lock_timeout/statement_timeout so a blocked migration fails fast, and it refuses a file tagged -- DESTRUCTIVE unless passed --allow-destructive (expand/contract discipline; the deploy never auto-drops). A file needing to run outside a transaction (e.g. CREATE INDEX CONCURRENTLY) tags itself -- NO-TRANSACTION.
- Staging auto-applies additive migrations inside
deploy-staging.yml. - Prod migrations stay a gated manual step for now: after a release deploys, run
migrate.shagainstcredditby hand (it is a no-op if nothing is pending). Wiringmigrate.shintodeploy.ymlis the planned next step once the ledger has proven itself over a few releases. - Migrations are forward-only and must be backward-compatible with the previous release (expand/contract): the deploy's code-rollback path does not roll back the DB, so a rolled-back build still runs against the migrated schema.
After applying, run the matching refresher/backfill so the new columns/tables fill, then (if data changed) re-deploy to re-prerender (see §2.1 ISR note).
Database backup
scripts/ops/backup-creddit.sh (in-repo) runs daily at 02:15 UTC from cron: pg_dump -Fc creddit → /opt/backups, integrity-checked (pg_restore --list), retention of the five newest completed dumps (scheduled and manual combined; configurable with BACKUP_RETENTION_COUNT). The same custom-format dump is what reseed-staging.sh restores into staging, so the backup is continuously restore-tested. On-box only for now; an off-box encrypted copy is a documented follow-up (needs a storage target + key). RPO is up to 24h with no PITR, accepted because the data is re-derivable from chain (the refreshers rebuild it); the only non-derivable data is the newsletter/capacity email tables.
Log rotation
pm2 does not rotate its own logs, and pm2 restart does not truncate them. Left alone, /root/.pm2/logs/onchain-credit-error.log accumulates forever — it reached 62k lines / 4.1 MB spanning every deploy since the box was built before this was fixed.
Two independent pieces, both needed:
| Piece | What it fixes | Where |
|---|---|---|
/etc/logrotate.d/onchain-credit | dates and caps the files (daily, 14 days, maxsize 50M, compressed) | reference copy: scripts/ops/logrotate-onchain-credit.conf |
pm2 --time on both creddit apps | timestamps the lines | persisted in the pm2 dump via pm2 save |
Rotation alone is not enough: it dates the files but every line inside is still anonymous. --time is what makes a line answerable. It survives deploy.yml's pm2 restart because pm2 save writes it to /root/.pm2/dump.pm2.
Reading the error log after a deploy: don't trust the tail. Lines have no inherent ordering guarantee relative to now, and (pre-
--time) months-old lines sit directly above fresh ones. Three known-benign residents cost triage time repeatedly:
Failed to find Server Action "<hash>"— a browser tab on the old build POSTs an action id the new build doesn't have. Expected on every deploy; self-heals on reload. Hundreds of distinct hashes = many deploys, not one incident.Failed to load external module pg-<hash>— alarming (pgis the Postgres driver) but historical; it does not recur and DB-backed pages serve 200.Single item size exceeds maxSize— Next data-cache notice, item too big to cache. Present on staging too. Not a page failure.To tell live from historical, don't read — measure. Snapshot
wc -lon the log, curl the DB-backed pages, then print only the newly appended lines:bashL=/root/.pm2/logs/onchain-credit-error.log; B=$(wc -l < $L) for p in / /portfolio /carries; do curl -s -o /dev/null -w "%{http_code} $p\n" "http://localhost:3001$p"; done sleep 3; A=$(wc -l < $L); sed -n "$((B+1)),${A}p" $LAnything not emitted by a fresh request is not the release's problem.
Scope is deliberate. The glob matches only the four creddit logs (onchain-credit-{out,error}.log, onchain-credit-staging-{out,error}.log). The event ingester's and the ledger worker's pm2 logs (creddit-event-ingester-{out,error}.log, creddit-ledger-worker-{out,error}.log) are ours, but the reference copy does not match them either, so nothing rotates them unless the box's copy was extended; adding both is carried in the ledger-first plan's §11 (docs/plans/portfolio-ledger-first-plan.md). The co-resident DEX HQ logs (dexhq, rindexer, creddit-indexer) are not rotated by this config and are not ours to touch — rindexer-out.log alone is 286 MB. For the same reason the config uses copytruncate rather than a pm2 reloadLogs postrotate, which would reopen every pm2 process's handles box-wide.
Access gate (NEXT_PUBLIC_ACCESS_CODE, per environment)
src/components/AccessGate.tsx is an opaque full-screen overlay that covers the app (every page except the landing at /, which stays open; the gate comes up on Launch app) until the visitor types an access code (then remembered per browser in localStorage). It is off unless the environment sets NEXT_PUBLIC_ACCESS_CODE, so each environment decides independently. It replaces the old hardcoded EarlyAccessGate (code letsgetonchain), which was removed in v0.3.0.
- Build-time, not runtime. Next inlines
NEXT_PUBLIC_*into the client bundle and every page here is prerendered, so the value is baked bynpm run build. Setting the var and only runningpm2 restartdoes not arm the gate; a deploy (or a manualnpm run build+ restart) does. - Soft gate, zero access control.
NEXT_PUBLIC_ACCESS_CODEis inlined into the client bundle at build time and compared in client React state, so the code ships in the bundle, thelocalStorageflag is trivially set by hand, and page content stays server-rendered underneath (deliberately, so SEO/structured data still indexes). It is cosmetic friction and beta signalling, never a security control. Everything private is protected server-side by the session cookie on/api/portfolio/*and/api/chat*regardless of the gate. - Rotating the code: bump
STORAGE_KEYin the component (currentlycreddit_access_v2) to re-prompt browsers that already unlocked with the old one. - Staging does not need it. Staging is protected at the edge by nginx basic auth (real auth, no bundle leak); the overlay would only add a second prompt.
# gate creddit.xyz
echo 'NEXT_PUBLIC_ACCESS_CODE=<code>' >> /opt/onchain-credit/.env.local
cd /opt/onchain-credit && npm run build && pm2 restart onchain-credit --update-env \
&& bash scripts/ops/restart-ingester.sh # every rebuild restarts the ingester, or the freshness alarm pages
# ungate: remove the line, rebuild, restart bothSecurity response headers
Set centrally in next.config.ts (an async headers() block applied to /:path*, so every route including API routes and static assets gets them). There is no middleware.ts; keeping this in one place means the app ships secure headers with no per-route wiring. The same file sets poweredByHeader: false, which removes the default x-powered-by: Next.js response header so we do not advertise the framework/version.
| Header | Value | Why |
|---|---|---|
Strict-Transport-Security | max-age=63072000; includeSubDomains; preload | Force HTTPS for two years, including subdomains; preload-eligible. Only sent over HTTPS, so it is inert on local http://localhost. |
X-Frame-Options | DENY | Block framing (clickjacking). Reinforced by the CSP frame-ancestors 'none'. |
X-Content-Type-Options | nosniff | Stop MIME sniffing. |
Referrer-Policy | strict-origin-when-cross-origin | Send only the origin (not the path/query) on cross-origin navigations. |
Permissions-Policy | camera=(), microphone=(), geolocation=(), browsing-topics=() | Deny powerful features the app never uses, and opt out of the Topics API. |
Content-Security-Policy-Report-Only | see below | Observe-and-report CSP. Does not block anything yet (see the rollout note). |
CSP ships Report-Only first. The policy is emitted as Content-Security-Policy-Report-Only, not the enforcing Content-Security-Policy. In Report-Only mode the browser evaluates the policy and logs every violation but never blocks the resource, so we can watch real traffic and tune the policy before it can break a page. The current policy (assembled from the per-directive list in next.config.ts) is:
default-src 'self'; base-uri 'self'; object-src 'none'; frame-ancestors 'none'; form-action 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self' https: wss:; manifest-src 'self'The looser-than-'self' directives are deliberate and documented inline in next.config.ts: script-src 'unsafe-inline' (Next.js bootstrap scripts + inline JSON-LD from src/components/seo/JsonLd.tsx), style-src 'unsafe-inline' (Next.js inline styles + inline style attributes from React components and Recharts' chart wrappers), img-src data: https: (inline icons + remote logos), font-src data: (self-hosted + data-encoded glyphs), and connect-src https: wss: (same-origin fetch/stream + the injected window.ethereum wallet, which opens its own RPC/WebSocket connections client-side). Third-party price/aggregator/RPC APIs (Fynd, KyberSwap, Pendle, Ethereum RPC) are called from the server, so they are not connect-src entries.
Reading violation reports. With no report-uri/report-to endpoint configured, reports surface in the browser only: open DevTools and look for Content-Security-Policy-Report-Only warnings in the Console (each names the blocked directive and resource), or the Network panel. Exercise the real pages while watching (home, an asset profile with charts, /portfolio, /carries, and connect a wallet) so injected wallet/chart/JSON-LD behaviour is covered. If a first-party resource trips the policy, widen the specific directive; do not blanket-add hosts.
Flipping to enforcing. Once the console is clean across the surfaces above, change the header key in next.config.ts from Content-Security-Policy-Report-Only to Content-Security-Policy (value unchanged), deploy, and re-verify. Do this as its own change so a regression is easy to bisect and revert.
Verify after a deploy:
# prod edge: expect the six headers present and NO x-powered-by
curl -sI https://creddit.xyz/ | grep -iE 'strict-transport|x-frame|x-content-type|referrer-policy|permissions-policy|content-security|x-powered-by'x-powered-by must be absent; content-security-policy-report-only (not content-security-policy) must be present until the flip. Note the edge (Cloudflare) may add or normalise some headers; the origin values above are what the app emits.
5. Verifying a deploy
After a push to main (or a manual deploy):
- CI is green. Watch the run on GitHub Actions ("Deploy to production"); the final log line is
Deployed <short-sha> at <iso-ts>. A red run that ends inRolled back to <sha>means the build failed and the previous commit is live — fix and re-push. - Process is up and on the new code.bashThe short SHA must match the commit you deployed.
pm2 list # onchain-credit "online", restart count bumped ssh root@dexhq.io 'cd /opt/onchain-credit && git rev-parse --short HEAD' - App responds locally + DB reachable.bash
ssh root@dexhq.io 'curl -sI http://localhost:3001/ | head -1' # expect HTTP 200 ssh root@dexhq.io 'curl -s http://localhost:3001/api/health' # expect {"ok":true,"db":"up",...}/api/healthis a 200 +SELECT 1probe; both deploy workflows curl it after restarting the app. Production logsProduction healthy: app and Postgres answered /api/health (HTTP 200)beforeDeployed <sha>. A health failure marks the deploy red without rolling back the new code. This verifies the origin app and database; the public edge and the background processes' first successful work are separate checks. - Edge serves it. Load
https://creddit.xyzand the changed page. Remember ISR: if you also ran a refresher, the page may show the pre-refresh snapshot until the revalidate window elapses — re-deploy to force a re-prerender. - No errors in logs.
pm2 logs onchain-credit --lines 100. - Data freshness (if relevant). Spot-check
max(snapshot_ts)on the affected table via psql to confirm a manual refresher actually wrote. - End-to-end smoke. The Playwright suite already ran against staging on this exact code (§3.1). It reports, it does not gate: read its result before opening the release PR, but nothing blocks the merge on it. Nothing runs automatically against prod. To point the suite at
https://creddit.xyzby hand (read-only page loads, but still prod traffic), get explicit permission first; the target isE2E_BASE_URL(set it andplaywright.config.tsboots no local server). Prod is served on the public hostname with no basic auth in front of it, so no credentials are involved; to point the suite at a firewalled app port on either box, tunnel to it as the staging smoke does (§3.1). Full procedure: Processes → Verifying a UI change.
External dependencies
The deploy itself only needs GitHub + the box. The running app and its refreshers depend on (configured in .env.local, preserved across deploys):
| Dependency | Used for | Config |
|---|---|---|
| Ethereum RPC (current state) | live on-chain reads | ETHEREUM_RPC_URL (fallback publicnode https://ethereum-rpc.publicnode.com) |
| Ethereum RPC (archive) | historical/backfill block reads | ETHEREUM_ARCHIVE_RPC_URL (fallback dRPC https://eth.drpc.org) |
| DefiLlama Coins API | USD prices; basis market price | src/lib/data/llama-prices.ts |
| NY Fed | SOFR rates | refresh-sofr.ts |
| Dune API | the hourly token_price_bars tape, weekly DEX market measurement and ad-hoc analytics | DUNE_API_KEY, DUNE_QUERY_ID_HOURLY (8033816), DUNE_QUERY_ID_DEX_MARKET (8808795; SQL at scripts/dune/dex-market-measure.sql). Missing hourly configuration logs a skipped primary check; the daily report exposes unresolved hours. Missing DEX measurement configuration is a [fail] because category review cannot complete. The retired sUSDe ratio query and its cooldown are no longer configured. |
| CoinGecko | live prices, hourly history for a hole, and the weekly REPORTED daily volume the pricing-category test's volume bar reads (market_chart/range total_volumes, exchanges and DEXes together; a 404 means the vendor lists no such coin and that row's bar falls back to the on-chain median) | COINGECKO_API_KEY (Demo; keyless works at a much lower rate limit) |
GeckoTerminal / CoinGecko /onchain | pool reserves for the pricing categories' market limb (reserve_in_usd per pool), never counting a side that is the pool's own share token. One dataset, two doors: with COINGECKO_API_KEY set the job reads api.coingecko.com/api/v3/onchain/… at 30 requests a minute; unset it falls back to the keyless api.geckoterminal.com endpoint. | COINGECKO_API_KEY (optional here, required by the live-price path). The keyless door publishes 30 a minute and delivers about five: measured 2026-09-22 against the live vendor, five 200s then a hard 429 on every request for ~40 seconds, so the fallback is paced at four a minute. TIME THE RUN BY PAGES, NOT BY TOKENS: the walk asks for a second page only where the first both failed the $1M bar and filled up, so 23 rows is 23 requests at best (about six minutes), 34 on the 2026-09-22 shape (about nine) and 69 at the three-page ceiling (about seventeen). Keyed it is about a minute. A key that is set and REFUSED (expired, mistyped, wrong tier: a 401 or a 403) falls back to the same door mid-run, with a [fail] line naming the variable, so a bad key costs a slow week rather than a lost one. |
| Morpho Blue GraphQL + Euler Goldsky subgraph | curator / market data | — |
| Telegram bot (cron alerting) | WS8 failure + unknown-asset alerts | ALERT_TG_BOT_TOKEN, ALERT_TG_CHAT_ID (both unset = alerting off); optional ALERT_TG_API_BASE (default https://api.telegram.org), ALERT_ENV (env label override) |
Optional knobs live in the same .env.local and have nothing behind them to be down.PORTFOLIO_LIVE_COOLDOWN_MS is how long a signed-in wallet's live read is refused after one has just been taken, in milliseconds; unset it is 300000 (five minutes), and that window is what the portfolio's Synchronize button shows as its wait. Lowering it buys fresher readings for more RPC, and 0 removes the bound altogether, which makes one open tab per wallet enough to read the chain as fast as the pipeline runs. It is read per call rather than at import, so pm2 restart --update-env really does move it instead of no-opping on a module the app has already loaded; a value that is not a finite, non-negative number falls back to the default rather than being taken as zero. The full table of variables is External dependencies → Environment variables.
RPC access goes through src/lib/data/rpc.ts. Token-basis redemption rate comes from the token_yield_apy table; market price for basis from DefiLlama.
Related
- Architecture — tech stack, project layout, full schema.
- Data Pipeline — the refreshers, cadences, vault-coverage sync, backfills.
- Database & schema — the canonical APY math and readers.
- Release steps (one-time) — every first-deploy note and gated repair this project has written, with whether it has run.
6. AI assistant (Creddit Agent)
The AI research assistant (Creddit Agent) is backed by /api/chat* routes that stream a tool-using Claude response over creddit's own data (carries, repo rates, capacity, oracles, basis, assets). It is code + schema + env: the code and migrations ship through the normal pipeline; the secrets and the nginx streaming block are box-local manual steps.
Its release is deferred and it has no UI. No route, nav entry or dock renders it; the API, the schema and the retention cron are all still in place so it can be picked up later. With no surface calling it, CHAT_ENABLED is the only thing between a hand-rolled POST and real token spend: leave it unset in every environment until the assistant is released. With the flag unset POST /api/chat answers 503 before any model call; the two GET /api/chat/conversations* routes do not read the flag, stay session-gated (401 signed out) and return only the caller's own stored history, so they spend nothing. Nothing else about this section changes when the UI comes back.
AI assistant env
Set in each environment's .env.local (preserved across deploys; next start re-reads it on pm2 restart). The feature stays dark (route returns 503) until CHAT_ENABLED is truthy and ANTHROPIC_API_KEY is set. While the release is deferred, that is the intended resting state everywhere.
| Var | Purpose |
|---|---|
ANTHROPIC_API_KEY | Model access (secret). Never in the repo. |
CHAT_ENABLED | Kill switch (1 to enable). |
CHAT_MODEL | Model id (default claude-sonnet-5). |
SESSION_SECRET | HMAC key for the account session cookie (creddit_session). Generate 32+ random bytes; required for sign-in. Falls back to CHAT_SESSION_SECRET if unset, so an existing server keeps working. |
CHAT_SESSION_SECRET | Legacy name for the session-cookie HMAC key; still honored as the SESSION_SECRET fallback. |
CHAT_DAILY_TOKEN_BUDGET | Per-user daily token cap (default 250000). |
CHAT_GLOBAL_DAILY_TOKEN_BUDGET | Whole-app daily token cap (default 5000000). |
SIWE_DOMAIN | Required in production. Pins the SIWE domain (creddit.xyz in prod, staging.creddit.xyz in staging) so the domain binding never derives from the client-controlled Host header, which closes a phishing-relay / Host-spoofing account-takeover path (a signature captured on an attacker origin cannot be replayed against this server). Falls back to CHAT_SIWE_DOMAIN. If unset, the request Host is trusted only when it is in a built-in allowlist of known creddit hosts (a safety net, not a substitute for pinning); any Host outside that allowlist fails verification. See Origin & SIWE hardening. |
CHAT_SIWE_DOMAIN | Legacy name for the SIWE domain pin; still honored as the SIWE_DOMAIN fallback. |
nginx (streaming)
The chat response is a long-lived stream, so its location must disable proxy buffering. Add to each vhost (onchain-credit and onchain-credit-staging), above the catch-all location /:
location /api/chat {
proxy_pass http://127.0.0.1:3001; # 3002 on staging
proxy_http_version 1.1;
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 300s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}Then nginx -t && nginx -s reload. Verify tokens arrive incrementally (not one flush) through Cloudflare.
Access
Sending a message requires wallet sign-in (Sign-In with Ethereum; injected wallets only in stage 1). Signing costs nothing (a message signature). The chat page and history are viewable without signing in; the per-address + global daily token budgets are the real spend guard.
Weekly portfolio coverage report
The existing Monday 04:30 UTC sync-portfolio-tokens.ts job produces one consolidated coverage digest. It only detects and proposes: it never onboards or retires assets, changes pricing decisions, lists markets, or runs Fluid discovery. A complete clean run sends nothing. The former top-20 stablecoin ranking is no longer an eligibility source, and --approve refuses before opening a database connection because its old par-only inserter cannot complete the current onboarding checklist.
The eligibility set is current and keyed by chain and contract address. On the currently supported Ethereum sources it includes active unfrozen Aave v3 and SparkLend reserves; collateral and debt assets from listed Fluid vault configurations (smart-pool assets from their current mappings), Fluid lending shares and their contract-declared deposit assets; Morpho markets approved for isolated lending (repo_listed) or active listed carries (tracks includes carry), counting both reasons when both apply; funds currently listed on either tab; and existing profiles resolved through their declared contract identities. It follows declared underlying/wrapper relationships, including native/wrapped ether, derived share conversions and verified one-for-one wrapper declarations. It never uses a matching ticker as proof of identity or treats all wrapped quantities as equal.
Principal-token base decisions stay parked. Pendle's active status records discovery and maturity, not a separate approval. The report limits automatic PT eligibility to unmatured active markets whose payout address is already registered, active and booked, matching the app's current payout-coverage predicate. It includes their accounting/payout asset and the yield/delivery token read from the declared yield contract. A missing or bookless payout base is a separate Fred decision, shown once per base chain/address in the parked group, with its affected PT markets and maturities as reasons; discovery alone does not propose onboarding that new base. This boundary does not remove independent reserve, fund, market or profile eligibility for a bookless asset. A currency book only gates its portfolio view, not its bare-wallet coverage.
Coverage follows the existing reader and pricing model. ERC-4626 fund shares are owned by the fund reader, derived from the fund registry; they do not require a duplicate bare wallet token row or sweep. Non-ERC-4626 funds need their existing wallet and redemption paths. Fluid lending shares and principal tokens likewise keep their own readers. Market assets need a declared source and usable accepted stored price history; composed and derived assets need conversion coverage and their actual pricing dependencies, not a raw quote for every share. PTs need stored market-price and redemption-factor history. Presence is checked, not history depth, freshness, source quality or source health. The existing profile identity gaps and unresolved currency-book redemption rates remain in the same digest.
Retirement is conditional. Ever-listed fund coverage is retained for existing holders, including delisted funds and their pricing/redemption dependencies. Existing tracked pricing dependencies are retained until their parent's retirement is reviewed. Principal-token payout and pricing/factor dependencies are retained for the app's 45-day grace window and any market held by a tracked wallet; these retained markets do not add new onboarding eligibility. A tracked active asset with no remaining reason is only a manual retirement suggestion. A retired contract qualifying again is a reconsideration; it is never silently reactivated. Any failed, partial, malformed, suspiciously empty or stale required registry read withholds all retirement suggestions. A periodically refreshed reserve, active PT or smart-pool asset mapping with missing current evidence or evidence older than 72 hours makes retirement unsafe; manual market listing/approval records do not expire with age. This is a completeness guard, not a new price-health monitor. Positive findings remain visible when their required evidence succeeded. Unavailable registry, history or contract-identity evidence is shown as coverage not checked, never as proven absence or complete coverage. Repair and retry those sources before making a dependent coverage decision. Failed fund acquisition or an unidentifiable contract prevents the ownership census; membership, deposit, unit or redemption problems attributable to a known fund defer only that reader and assets priced through it. Unrelated checks whose evidence succeeded remain reviewable. Names and sponsor labels are display metadata and do not determine whether an existing fund reader can read or value a balance. Any incomplete required fund dependency still withholds all retirement suggestions.
Telegram uses the Ledger self-check layout. The heading names the environment, followed by date/cadence, WHAT THIS CHECKS, RESULT and grouped findings: missing assets, incomplete coverage, reconsiderations, retirement suggestions, parked PT bases, unchecked coverage, profile identities and source problems. Each asset names its chain, full address, all qualifying reasons, missing coverage and suggested next step. Delivery is plain text, so token names cannot introduce Telegram markup; control/newline text is normalized. Numbered chunks keep each normal entry together and split unusually long entries without losing text. No old stablecoin/profile/rate messages accompany the digest. All chunks are printed to the job log, including when Telegram is unconfigured or delivery fails. Unchecked assets with the same cause share one explanation and repair step. Common eligibility reasons are stated once for that group, followed by every affected asset's full name, chain and contract address plus any individual qualifying reasons.
Preview and staging validation (read-only, never sends Telegram):
# Run from the staging checkout; load staging configuration only in this subshell.
(
set -a
source .env.local
set +a
unset ALERT_TG_BOT_TOKEN ALERT_TG_CHAT_ID
export PGOPTIONS="${PGOPTIONS:+$PGOPTIONS }-c default_transaction_read_only=on"
timeout 180 node_modules/.bin/tsx scripts/sync-portfolio-tokens.ts --report-only
)Confirm the loaded database is staging before running this command. Use the direct TS invocation shown above: the cron wrapper has its own failure notification, so it is not the preview entry point. The subshell removes Telegram credentials even if acquisition or the toolchain fails; read-only database sessions also prevent unintended writes. --report-only runs the same source reads, detection and formatter and bypasses its sender even if credentials are present. Inspect the complete numbered report in stdout, check identities and reasons against the current registries, verify source caveats and retirement withholding, and review proposals through the existing B.2a checklist and B.2 where applicable. A clean complete preview prints the coverage counts. Preview is not an onboarding action and needs no page, ledger, registry or production writes. Do not send a real Telegram test message; formatter/delivery tests use injected senders.
Daily hourly-price feed reports
The server command starts at 00:00 UTC and sends two reports: Hourly primary price feeds and Hourly price gaps. Coverage includes every active asset with a declared hourly feed, including WETH's USD price even though WETH is the ETH book's valuation unit. Each report covers the previous 24 completed UTC hours and revisits the preceding day's final six-hour grace window. Only unresolved incidents are listed; healthy reports state that no unresolved problems were found. A working backup does not clear a missing primary answer from the first report. The second requires an accepted, positive, non-carried, non-rejected canonical price. Hours still within the six-hour backup grace are pending, not described as failed backups.
Delivery waits for the midnight price refresh to finish all primary, backup and quality-check phases, including a failed attempt. This prevents ordinary refresh timing from appearing as a feed outage. The UTC reporting window stays fixed while the command waits; the unresolved state is read again immediately before delivery. If no completion is recorded by 00:30 UTC, the reports name the overdue refresh and show the remaining gaps. A dry run never waits or writes; it labels an unfinished refresh as pending.
Run the command without sending to inspect the two messages:
/opt/onchain-credit/scripts/run-cron.sh report-hourly-price-feeds.ts --dry-runTo schedule delivery, install this line in the server crontab using UTC (CRON_TZ=UTC or an independently verified UTC cron environment):
0 0 * * * /opt/onchain-credit/scripts/run-cron.sh report-hourly-price-feeds.ts --sendThis is the installation procedure, not evidence that the line is installed. Keep DATABASE_URL, ALERT_TG_BOT_TOKEN and ALERT_TG_CHAT_ID in the server environment; never copy their values into source, logs or a Codex automation. The report is a normal server cron, not a chat automation. A database delivery receipt is kept independently for each report. Failed sends retry three times and remain pending for a later invocation; a session lock prevents concurrent report runners. Delivery is at least once: a process that dies after Telegram accepts a message but before recording its receipt may resend it.
The old per-asset quiet-feed alarm is replaced by these reports. Live-tip vendor outage/disagreement checks and whole-refresh infrastructure failures remain separate. Pool-liquidity review findings are stored/logged for review and do not independently send either hourly-price report. The migration and first installation are tracked in Release steps.
Account activity operations
The first-party collector stores only authenticated account activity in the current environment's database; collection requires no additional secret. The opaque actor binding uses a domain-separated MAC under the existing session secret. Browser privacy preferences (DNT: 1 or Global Privacy Control) suppress Google Analytics only; authenticated first-party signups, logins and usage continue to be recorded. No new main-app controls or navigation are added. /admin and unknown/query-bearing paths are excluded from the route vocabulary.
Google Analytics requires both build-time NEXT_PUBLIC_ANALYTICS_ENV=production and a valid NEXT_PUBLIC_GA_MEASUREMENT_ID; the browser must also be on creddit.xyz. Staging sets NEXT_PUBLIC_ANALYTICS_ENV=staging and leaves the measurement ID unset. Even a production setting copied into staging cannot load Google on the staging hostname. A runtime restart cannot change these public build values. The old hardcoded integration is removed; its historical Google data is not deleted.
Before a separately authorized production activation, verify the new GA4 stream's Enhanced measurement is disabled, so automatic outbound-link, search, form and history events cannot bypass the explicit sanitizer. Automatic page views are disabled in code. Identity resolution precedes the first explicit page view and any event; account switches invalidate pending queues and bindings. Google receives only the opaque user ID, closed event/property vocabulary and public route template, with referrer blanked. It receives no wallet, email, query string, error text or page content. A blocked Google script suppresses Google delivery without stopping first-party collection. A missing identity prevents attributed collection; neither failure blocks interactions.
Run user-state summary/retention maintenance through the existing per-checkout lock/log wrapper, for example hourly at minute 17:
17 * * * * /opt/onchain-credit-staging/scripts/run-cron.sh maintain-account-activity.tsThis is app-state maintenance, not a market refresher. The script uses its checkout's .env.local, updates summaries idempotently and enforces 90-day raw / 12-month daily retention. Failures exit nonzero through the wrapper's existing alert path; public pages remain usable. The same procedure applies to an environment only after deployment there is separately authorized.
Staging's nightly reset entrypoint is scripts/ops/reseed-staging-with-migrations.sh in the staging checkout. It invokes the existing scrubbed restore into the fixed creddit_staging database, then replays staged additive migrations through the ledger runner. It never passes the destructive override, never accepts a target database URL, stops if restore/scrub fails, and honors /root/.reseed-paused without restoring or migrating. This preserves staged schema after a production-backup restore without leaving reseeding paused. A reset intentionally clears staging activity; expect new observations after the reset, not retained staging history.
Attended outage recovery
After RPC service has recovered, preview a bounded UTC interval:
scripts/run-cron.sh recover-history.ts --from <UTC-start> --to <UTC-end>Read the per-window counts, coverage limitations and request budget. With explicit production-data authorization, run the same bounds with --apply. An interrupted run uses the same command again; good history is preserved. --pace-ms and --max-requests bound HTTP usage. The final status must report remaining=0, derived=true, unverifiedAudits=0, stopped=false, and exit zero. A missing archive value remains a gap and an unsuccessful run, never an interpolated observation. Portfolio audits queued by the repair must also finish successfully before considering those readings reconciled.
The implementation and its explicit coverage limits are in Data pipeline: outage recovery. Do not substitute backfill-portfolio-wallet.ts --fresh: that replaces the wallet's whole history. The old backfill-recent-gap.ts name is a compatibility wrapper with preview as its default, and now requires both date bounds.
No new crontab line is needed for the history-gap alert: the existing hourly check-ingester-freshness.ts invokes it. It records pending gaps and unfinished dependent calculations in history_recovery_items, so a healthy current update does not clear yesterday's incident. There is no automatic recovery or credit purchase. Apply the receipt migration before relying on that alert; the one-time production migration belongs in the release steps.
Recovered Portfolio checkpoints enqueue reading audits. The command exits nonzero and the hourly history alert stays active until those audits finish successfully; rerun the same bounded command to verify after the worker drains the queue. If an audit is terminal or its receipt has been pruned, the same bounded --apply requeues that audit without rewriting the checkpoint.
Morpho market data coverage
Market discovery and rate collection are independent of screener approval. The normal assets job performs the broad census daily and checks required app and wallet dependencies on each six-hour run. Its defaults admit a market at $100,000 borrowed, retain it above $50,000, and allow seven days below that floor. Only trusted loan-token dollar marks qualify the debt amount. An unreadable state or missing price preserves an existing coverage decision.
MORPHO_COVERAGE_ADMIT_USD and MORPHO_COVERAGE_EXIT_USD override the two dollar thresholds. MORPHO_AUTO_BACKFILL_WINDOWS limits snapshot boundaries processed per tick (default three, 0 disables the automatic replay). Each boundary groups all pending markets at that timestamp. New admissions get recent history before the deeper queue. All replay starts at 2026-01-01 UTC or market creation, whichever is later; older stored history is retained. A failed market keeps its checkpoint for a later retry.
Monitor morpho_market_coverage for active membership, admission reason and the recent/deep cursors. An active entry with an unchanged cursor across successful assets ticks needs investigation of the market's archive or metadata errors. Listing, delisting and carry approval remain the existing explicit workflows. Portfolio accounting continues to use its own on-chain reads and ledger; the shared primitive supplies displayed market facts and quotes.