The problem
gligabao starts one sargo child process per instance. Sargo trains in memory on a background thread and keeps each user’s model as files in a local directory (<sargo_gcnii_dir>/<hex of user id>/model.weights, classes.json). Nothing is shared between instances except the database and the S3 bucket. Without more, a cluster would break in five ways:
The design in one picture
- The database is the source of truth for what exists and who is doing what. The jobs table and the models table decide; sargo’s memory and directory are a cache and a working copy.
- S3 holds the weights; every key is written once. Nothing is ever overwritten, so a reader never sees half a model or a mix of two.
- A model is used only after the instance has made its directory match the latest row. Matching is checked, not assumed.
The pieces
gb_model_train_jobs: the training in the database
One row per training (internal/training/db, schema in sqlite/sql/schema.sql):
A partial unique index
(model_type, user_id) WHERE status = 'running' allows at most one running row per user and type in the whole cluster. This is the only lock; it needs no coordinator and works on SQLite and PostgreSQL.
gb_trained_models: the model in the database
The same table the LoRA models use, with model_type = 'gcnii', user_id, storage (s3 or local), path (the directory in the store), and files with the size and sha256 of each file. It is the record the sync checks the weights against. Training again never replaces a row.
S3 (the weights store)
<prefix>/<model_type>/<user>/<job id>/{model.weights,classes.json}, in the meta bucket (default prefix model_weights). The job ID is unique per training, so a key is never written twice. Without a bucket the same layout goes under model_weights_local_dir; that works for one instance (or a shared mount) but not for a real cluster.
The marker file
<sargo dir>/<user>/model_id holds the gb_trained_models.id of the model whose files are in that directory. It is how an instance knows whether its copy is current. It is written after the weights (on the training instance, when it records; on any instance, when it restores).
Sargo reloading
Sargo caches the loaded model together with the modification time of its weights file, andget reloads the file when the time differs (rust/sargo/src/gcnii/users.rs). This lets a sync that replaces the file take effect at once, without a sargo restart.
The life of a training
running until the marker is written. Every reader treats “running” as “not finished”, so nobody sees a model that is not yet stored and recorded. A failed report ends the row failed with sargo’s error; a failure to store the weights ends it failed too (the model is still in A’s directory).
Reading from any instance
Status
GET /v1/gb/model/train returns the newest job of the user as the database has it, so every instance gives the same answer. Status mapping: running, done, and failed (a lost job is failed, error says the instance stopped). A job that exists only in the local sargo and not in the table (a training from before the table existed, or the feature off) is shown from sargo; if the local one is newer than the row, the local one wins.
Inference and embedding: the sync
Before/infer, /embed and the status, the instance runs Recorder.Sync(user):
- If the user’s newest job is
runningon this instance, do nothing (see “Races” below). - Read the latest
gb_trained_modelsrow of the user. None: do nothing (sargo answers as before,404if it has no model). - If this instance’s marker equals the row’s ID: do nothing.
- Otherwise restore: fetch each file from the store, check its size and sha256 against the row, write it to a temporary name and rename it into place (
classes.jsonfirst, the weights last), then write the marker. A file that does not match the record aborts the restore and nothing is left half-written.
Listing
GET /v1/gb/model/models?model_type=gcnii lists the user’s rows from the database; it never touches a directory.
Failure handling
Races, and why they are safe
- Two claims at once. Both
INSERTs race on the partial unique index; one wins, the other gets zero rows and a409. The stale-job sweep and the insert are separate statements, but the index alone guarantees the single running row. - Sync while training. Sargo writes the new weights to the directory when it finishes, before the model is recorded. If a sync on that instance then ran, it would find the marker older than the latest row and restore the previous model over the new one (and the recorder would then store the wrong files). The rule “do nothing while this instance’s job is
running” closes this, and the job staysrunninguntil after the marker is written. Other instances are not affected: they only read S3 and the rows. - Partial restore. Files are renamed into place one at a time, the weights last, and the marker after: a crash in between leaves a marker that does not match the latest row, so the next request restores again.
- Concurrent restores on one instance. Two requests may both restore; the writes are atomic renames of identical content, and the second finds the marker current.
- Instance restart. Memory is lost, the directory is not. The marker is on disk, so the restarted instance keeps its model and only restores when the database says it is behind. A running job of the old process is simply
lostafter 90 s. - A model recorded twice.
gb_trained_models.job_idis unique and the insert ignores a duplicate, so a repeated record makes no second row. - Clock skew. Staleness compares the heartbeat with the clock of the instance that looks; with the 90 s margin and beats every 2 s, ordinary skew is harmless.
What is not guaranteed
- Training is not resumable and not highly available. A job is the memory of one sargo; its loss is detected, not prevented. The user retrains.
- Reads are consistent, not instantaneous. An instance learns of a new model on the next request that reaches it. A request being served while another instance finishes a training still answers with the model it started with.
/inferalways uses the latest model of the user. Choosing an older row needs a per-model directory in sargo (seeleftover_tasks/2026-11-23-gcnii-weights-follow-ups.md).- A local weights directory does not scale out. Without a bucket the “store” is a directory of the instance; use S3 for more than one instance.
- The 90 s staleness limit is a constant (
beatStaleininternal/api/model/weights.go), not a setting.
Operating it
- Config (
build/gligabao.toml, orMODEL_WEIGHTS_S3_BUCKET/_REGION/_PREFIX/_ENDPOINT):model_weights_s3_bucket(normally the meta bucket),model_weights_s3_prefix(defaultmodel_weights),model_weights_local_dir(no bucket).sargo_gcnii_dirstays per instance and may be ephemeral. - Permissions: the instance role needs
s3:PutObjectands3:GetObjecton<bucket>/model_weights/*ands3:ListBucketon the bucket (thegligabao_int_metapolicy ingloflow_emergence/ops/tf_main/gf_apps_core/gligabao_meta.tf). Apply it before deploying the binary, or the uploads fail and every training endsfailed. - Database: every instance must use the same database (SQLite is for one instance only; PostgreSQL for a cluster). The new table and columns are created at start (
CREATE TABLE IF NOT EXISTS,ADD COLUMN IF NOT EXISTS). - Looking at it:
SELECT * FROM gb_model_train_jobs ORDER BY id DESCshows who trains what and when it last beat;status = 'lost'rows are crashed instances. Weights are ins3://<bucket>/model_weights/gcnii/<user>/<job id>/. - Cleaning up: nothing is deleted by gligabao. Remove old job directories from the bucket by hand; their
gb_trained_modelsrows stay (the sync restores from the latest row, so keep that one).