Trained adapters published, private to their owners like the knowledge bases they come from

An adapter now has an owner (org, team, project, user or key): only callers
there see it, and to anyone else it is a model that does not exist, absent
from /v1/models too. A training job's owner defaults to the one collection
its pairs come from, must be able to read every collection they come from,
and pairs from a knowledge base are never made public. ctl train publish
keeps the GGUF or PEFT files on the mirror, which accepts uploads now, and
sets the owned adapter after the usual architecture check.

The knowledge bases' cache_salt follows the collections searched rather
than the organization, so two teams of one organization no longer share
cached passages.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-08 21:30:50 +02:00
1 parent 964b35ae65
commit 85025dfb03
29 files changed
+1049 -84

No files matched your search

+58
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
@@ -37,6 +38,7 @@ func (a *API) Handler() http.Handler {
mux.HandleFunc("POST /v1/models", a.fetch)
mux.HandleFunc("GET /v1/models/{model}/{version}", a.status)
mux.HandleFunc("DELETE /v1/models/{model}/{version}", a.remove)
mux.HandleFunc("PUT /v1/models/{model}/{version}/{file}", a.upload)
mux.HandleFunc("GET /v1/health", a.health)
return mux
}
@@ -199,3 +201,59 @@ func LastPathElement(u string) string {
}
return u
}
// maxUpload bounds a file sent to the mirror rather than fetched by it.
const maxUpload = 8 << 30
// upload keeps a file the caller sends — an adapter a node trained, which
// is on no registry to fetch from — with the same discipline as an import:
// its checksum and source beside it, and an existing version never
// replaced. X-Llmgw-Source says where it came from.
func (a *API) upload(w http.ResponseWriter, r *http.Request) {
e := Entry{Model: r.PathValue("model"), Version: r.PathValue("version"), File: r.PathValue("file")}
for _, n := range []string{e.Model, e.Version, e.File} {
if err := ValidName(n); err != nil {
fail(w, http.StatusBadRequest, err)
return
}
}
if _, err := os.Stat(e.Path(a.m.root)); err == nil {
fail(w, http.StatusConflict, fmt.Errorf("%s/%s/%s: %w", e.Model, e.Version, e.File, ErrExists))
return
}
tmp, err := os.CreateTemp(a.m.root, ".upload-*")
if err != nil {
fail(w, http.StatusInternalServerError, err)
return
}
defer os.Remove(tmp.Name())
n, err := io.Copy(tmp, http.MaxBytesReader(w, r.Body, maxUpload))
if cerr := tmp.Close(); err == nil {
err = cerr
}
if err != nil {
fail(w, http.StatusBadRequest, fmt.Errorf("receiving the file: %w", err))
return
}
if n == 0 {
fail(w, http.StatusBadRequest, errors.New("the file is empty"))
return
}
source := strings.TrimSpace(r.Header.Get("X-Llmgw-Source"))
if source == "" {
source = "uploaded"
}
added, err := Import(r.Context(), a.m.root, tmp.Name(), e, "", source, nil)
switch {
case errors.Is(err, ErrExists):
fail(w, http.StatusConflict, err)
return
case err != nil:
fail(w, http.StatusBadRequest, err)
return
}
writeJSON(w, http.StatusCreated, FetchResponse{
Status: Status{Model: e.Model, Version: e.Version, File: e.File, State: Present, BytesDone: added.Entry.Bytes, BytesTotal: added.Entry.Bytes},
URL: e.URL(a.base),
})
}
+30
View File
@@ -100,6 +100,36 @@ func (c *Client) Fetch(ctx context.Context, req FetchRequest) (FetchResponse, er
return out, err
}
// Upload sends a file for the mirror to keep at model/version/file, with
// where it came from; it answers the URL nodes fetch it from.
func (c *Client) Upload(ctx context.Context, model, version, file string, body []byte, source string) (FetchResponse, error) {
var out FetchResponse
req, err := http.NewRequestWithContext(ctx, http.MethodPut,
c.base+"/v1/models/"+url.PathEscape(model)+"/"+url.PathEscape(version)+"/"+url.PathEscape(file), bytes.NewReader(body))
if err != nil {
return out, err
}
req.Header.Set("Content-Type", "application/octet-stream")
req.Header.Set("X-Llmgw-Source", source)
resp, err := c.hc.Do(req)
if err != nil {
return out, fmt.Errorf("reaching the mirror at %s: %w", c.base, err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil {
return out, err
}
if resp.StatusCode >= 300 {
var e struct {
Error string `json:"error"`
}
_ = json.Unmarshal(raw, &e)
return out, &APIError{Status: resp.StatusCode, Msg: e.Error}
}
return out, json.Unmarshal(raw, &out)
}
// Status is where one model version stands.
func (c *Client) Status(ctx context.Context, model, version string) (FetchResponse, error) {
var out FetchResponse
+45
View File
@@ -0,0 +1,45 @@
package mirror
import (
"context"
"net/http/httptest"
"os"
"strings"
"testing"
"gitea.mynodes.xyz/ilker/llmgw/internal/tlsconf"
)
func TestAFileSentToTheMirrorIsKeptWithItsChecksumAndNeverReplaced(t *testing.T) {
root := t.TempDir()
srv := httptest.NewServer(NewAPI(NewManager(root, nil), "http://mirror:8081").Handler())
defer srv.Close()
c, err := NewClient(srv.URL, tlsconf.Config{}, 0)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
got, err := c.Upload(ctx, "politika-tonu", "train-1", "politika-tonu.gguf", []byte("GGUF adapter"), "trained by job train-1")
if err != nil {
t.Fatal(err)
}
if got.URL != "http://mirror:8081/politika-tonu/train-1/politika-tonu.gguf" || got.Status.BytesTotal != 12 {
t.Errorf("answered %+v", got)
}
e := Entry{Model: "politika-tonu", Version: "train-1", File: "politika-tonu.gguf"}
if b, _ := os.ReadFile(e.Path(root)); string(b) != "GGUF adapter" {
t.Errorf("kept %q", b)
}
if b, _ := os.ReadFile(sourcePath(e.Path(root))); !strings.Contains(string(b), "train-1") {
t.Errorf("source %q", b)
}
if _, err := os.Stat(checksumPath(e.Path(root))); err != nil {
t.Errorf("no checksum: %v", err)
}
if _, err := c.Upload(ctx, "politika-tonu", "train-1", "politika-tonu.gguf", []byte("other"), "x"); err == nil {
t.Error("an existing version was replaced")
}
if _, err := c.Upload(ctx, "../etc", "v", "f", []byte("x"), "x"); err == nil {
t.Error("a path outside the mirror was taken")
}
}