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

+36 -26
View File
@@ -1966,32 +1966,9 @@ func (a *API) setAdapters(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusBadRequest, "no model "+name)
return
}
base, baseErr := gguf.KindFromURL(r.Context(), safedial.Client(safedial.ControlPlane, 30*time.Second), m.Source)
have := map[string]bool{}
for _, ad := range m.Adapters {
have[ad.Source] = true
}
// The file is read only where llama.cpp will load it: a vLLM adapter is
// a PEFT directory, which has no header to read, and vLLM itself refuses
// one that does not fit the model when it is loaded.
ggufs := slices.ContainsFunc(m.Profiles, func(p catalog.Profile) bool { return p.Engine == "llamacpp" })
for _, ad := range req.Adapters {
if have[ad.Source] || !ggufs {
continue // already checked when it was attached
}
h, err := gguf.KindFromURL(r.Context(), safedial.Client(safedial.ControlPlane, 30*time.Second), ad.Source)
switch {
case err != nil:
writeError(w, http.StatusBadRequest, fmt.Sprintf("adapter %s: %v", ad.Name, err))
return
case !h.IsAdapter():
writeError(w, http.StatusBadRequest, fmt.Sprintf("adapter %s: %s is a model, not a LoRA adapter", ad.Name, ad.Source))
return
case baseErr == nil && h.Architecture != base.Architecture:
writeError(w, http.StatusBadRequest, fmt.Sprintf("adapter %s is for %s models and %s is %s",
ad.Name, h.Architecture, m.Name, base.Architecture))
return
}
if err := checkAdapters(r.Context(), m, req.Adapters); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
out, err := a.models.SetAdapters(r.Context(), m.Name, req.Adapters, actorOf(r))
if err != nil {
@@ -2073,3 +2050,36 @@ func multiNodeReadiness(n inventory.Node) string {
}
return "ready"
}
// checkAdapters reads each adapter a model does not have yet, where
// llama.cpp will load it, to refuse one that is not a LoRA adapter for the
// model's architecture: such an adapter is a replica that fails to start,
// found minutes later on a node.
func checkAdapters(ctx context.Context, m catalog.Model, adapters []catalog.Adapter) error {
// A vLLM adapter is a PEFT directory, which has no header to read, and
// vLLM itself refuses one that does not fit the model when it is loaded.
if !slices.ContainsFunc(m.Profiles, func(p catalog.Profile) bool { return p.Engine == "llamacpp" }) {
return nil
}
client := safedial.Client(safedial.ControlPlane, 30*time.Second)
base, baseErr := gguf.KindFromURL(ctx, client, m.Source)
have := map[string]bool{}
for _, ad := range m.Adapters {
have[ad.Source] = true
}
for _, ad := range adapters {
if have[ad.Source] {
continue // checked when it was attached
}
h, err := gguf.KindFromURL(ctx, client, ad.Source)
switch {
case err != nil:
return fmt.Errorf("adapter %s: %v", ad.Name, err)
case !h.IsAdapter():
return fmt.Errorf("adapter %s: %s is a model, not a LoRA adapter", ad.Name, ad.Source)
case baseErr == nil && h.Architecture != base.Architecture:
return fmt.Errorf("adapter %s is for %s models and %s is %s", ad.Name, h.Architecture, m.Name, base.Architecture)
}
}
return nil
}
+2
View File
@@ -31,6 +31,8 @@ type KBStore interface {
SearchChunks(ctx context.Context, q store.KBSearch) ([]store.KBHit, error)
SetKBDefault(ctx context.Context, d store.KBDefault) error
CollectionChunks(ctx context.Context, c store.KBCollection, limit int) ([]store.KBHit, error)
CollectionsFor(ctx context.Context, e store.Effective, write bool) ([]store.KBCollection, error)
EffectiveFor(ctx context.Context, scope store.Scope, id, org string) (store.Effective, error)
KBDefaults(ctx context.Context) ([]store.KBDefault, error)
}
+10 -7
View File
@@ -26,11 +26,14 @@ type KBDatasetRequest struct {
// KBDatasetLine is one line of the answer: a pair, or why a chunk gave none.
type KBDatasetLine struct {
Q string `json:"q,omitempty"`
A string `json:"a,omitempty"`
Doc string `json:"doc"`
Ord int `json:"ord"`
Error string `json:"error,omitempty"`
Q string `json:"q,omitempty"`
A string `json:"a,omitempty"`
Doc string `json:"doc"`
Ord int `json:"ord"`
// Collection is where the pair came from: an adapter trained on it
// belongs to a part of the tree that may read it.
Collection string `json:"collection,omitempty"`
Error string `json:"error,omitempty"`
}
const kbDatasetPrompt = `From the passage of an organisation's document below, write %d different questions an employee might ask, each with a short, correct answer.
@@ -93,7 +96,7 @@ func (a *API) kbDataset(w http.ResponseWriter, r *http.Request) {
body, _ := json.Marshal(map[string]any{"model": m.Name, "temperature": 0.7, "max_tokens": 200 * per,
"messages": []map[string]string{{"role": "user", "content": fmt.Sprintf(kbDatasetPrompt, per, ch.Document, ch.Body)}}})
status, out, _, err := a.replay(r.Context(), "/v1/chat/completions", m.Name, body)
line := KBDatasetLine{Doc: ch.Document, Ord: ch.Ord}
line := KBDatasetLine{Doc: ch.Document, Ord: ch.Ord, Collection: c.ID}
if err == nil && status != http.StatusOK {
err = fmt.Errorf("%d: %s", status, strings.TrimSpace(string(out)))
}
@@ -109,7 +112,7 @@ func (a *API) kbDataset(w http.ResponseWriter, r *http.Request) {
_ = enc.Encode(line)
}
for _, p := range pairs {
p.Doc, p.Ord = ch.Document, ch.Ord
p.Doc, p.Ord, p.Collection = ch.Document, ch.Ord, c.ID
_ = enc.Encode(p)
}
flush()
+3
View File
@@ -310,6 +310,9 @@ var Routes = []Route{
Response: TrainJobView{}},
{Method: "DELETE", Path: "/api/v1/train/jobs/{id}", Summary: "Remove an ended training job and what it made",
Response: map[string]any{}},
{Method: "POST", Path: "/api/v1/train/jobs/{id}/publish", Summary: "Make what a finished job made the model's adapter",
Description: "Kept on the mirror — the GGUF for a model llama.cpp serves, the PEFT files for one vLLM serves — and added to the model's adapters after the same check as POST /api/v1/models/{name}/adapters.",
Response: TrainPublishView{}},
{Method: "GET", Path: "/api/v1/train/jobs/{id}/artifacts/{kind}", Summary: "What a training job made: peft (a gzipped tar) or gguf",
Response: map[string]any{}},
+234 -13
View File
@@ -1,19 +1,25 @@
package adminapi
import (
"archive/tar"
"bufio"
"bytes"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"regexp"
"slices"
"strconv"
"strings"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/train"
"gitea.mynodes.xyz/ilker/llmgw/internal/mirror"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
@@ -36,11 +42,38 @@ type TrainStore interface {
type TrainAdmin struct {
Store TrainStore
Service *train.Service
// Mirror keeps what a job made where the nodes fetch weights; nil
// leaves publishing off.
Mirror TrainMirror
}
// TrainMirror is the model mirror, as publishing needs it.
type TrainMirror interface {
Upload(ctx context.Context, model, version, file string, body []byte, source string) (mirror.FetchResponse, error)
}
// TrainPublishView is an adapter published: where the nodes fetch it from,
// and what to do for replicas to load it.
type TrainPublishView struct {
Adapter string `json:"adapter"`
Model string `json:"model"`
Source string `json:"source"`
// Owner is who sees it; empty is every caller of the model.
Owner string `json:"owner,omitempty"`
Note string `json:"note,omitempty"`
}
// SetTrain enables the training endpoints.
func (a *API) SetTrain(t *TrainAdmin) { a.train = t }
// SetTrainMirror gives publishing the mirror, which is set up after the
// training endpoints are.
func (a *API) SetTrainMirror(m TrainMirror) {
if a.train != nil {
a.train.Mirror = m
}
}
// TrainRequest queues a job.
type TrainRequest struct {
// Name is the adapter's: what requests will ask for it by.
@@ -55,6 +88,14 @@ type TrainRequest struct {
// what an engine there leaves free.
Node string `json:"node"`
GPUMemoryMiB int `json:"gpu_memory_mib"`
// Owner is who the adapter will belong to: org:, team:, project: (by
// path or ID), user: or key:. Only callers there see it. When the
// pairs come from one knowledge base, it defaults to that collection's
// owner; it must be able to read every collection they come from.
Owner string `json:"owner,omitempty"`
// Public makes the adapter every caller's its model allows. Refused for
// pairs from a knowledge base.
Public bool `json:"public,omitempty"`
// Data is the pairs, NDJSON of {"q", "a"} (llmgw ctl kb dataset).
Data string `json:"data"`
// Params tune the trainer: rank, alpha, dropout, epochs, lr, batch,
@@ -73,6 +114,7 @@ type TrainJobView struct {
BaseRepo string `json:"base_repo"`
BaseRevision string `json:"base_revision,omitempty"`
Node string `json:"node"`
Owner string `json:"owner,omitempty"`
GPUMemoryMiB int `json:"gpu_memory_mib"`
Params string `json:"params,omitempty"`
GGUF bool `json:"gguf"`
@@ -93,7 +135,7 @@ type TrainJobView struct {
func trainView(j store.TrainJob) TrainJobView {
return TrainJobView{ID: j.ID, Name: j.Name, Model: j.Model, BaseRepo: j.BaseRepo, BaseRevision: j.BaseRevision,
Node: j.Node, GPUMemoryMiB: j.GPUMemoryMiB, Params: j.Params, GGUF: j.GGUF, Pairs: j.Pairs, State: j.State,
Node: j.Node, Owner: j.Owner, GPUMemoryMiB: j.GPUMemoryMiB, Params: j.Params, GGUF: j.GGUF, Pairs: j.Pairs, State: j.State,
Step: j.Step, Steps: j.Steps, Loss: j.Loss, Message: j.Message, PeakMiB: j.PeakMiB, CreatedAt: j.CreatedAt,
StartedAt: j.StartedAt, FinishedAt: j.FinishedAt, CreatedBy: j.CreatedBy}
}
@@ -105,6 +147,7 @@ func (a *API) registerTrain(mux Mux) {
mux.HandleFunc("POST /api/v1/train/jobs/{id}/cancel", a.trainCancel)
mux.HandleFunc("DELETE /api/v1/train/jobs/{id}", a.trainDelete)
mux.HandleFunc("GET /api/v1/train/jobs/{id}/artifacts/{kind}", a.trainArtifact)
mux.HandleFunc("POST /api/v1/train/jobs/{id}/publish", a.trainPublish)
}
func (a *API) trainReady(w http.ResponseWriter) bool {
@@ -163,7 +206,12 @@ func (a *API) trainCreate(w http.ResponseWriter, r *http.Request) {
bad("gpu_memory_mib is what the job needs of a card, in MiB")
return
}
pairs, err := countPairs(in.Data)
pairs, colls, err := countPairs(in.Data)
if err != nil {
bad("%v", err)
return
}
owner, err := a.trainOwner(r.Context(), in, colls)
if err != nil {
bad("%v", err)
return
@@ -191,7 +239,7 @@ func (a *API) trainCreate(w http.ResponseWriter, r *http.Request) {
gguf = *in.GGUF
}
j := store.TrainJob{ID: store.NewID("train-"), Name: in.Name, Model: m.Name, BaseRepo: in.BaseRepo,
BaseRevision: in.BaseRevision, Node: in.Node, GPUMemoryMiB: in.GPUMemoryMiB, Params: params, GGUF: gguf,
BaseRevision: in.BaseRevision, Node: in.Node, Owner: owner, GPUMemoryMiB: in.GPUMemoryMiB, Params: params, GGUF: gguf,
Data: []byte(in.Data), Pairs: pairs, CreatedBy: actorOf(r)}
if err := a.train.Store.CreateTrainJob(r.Context(), j); err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
@@ -203,11 +251,67 @@ func (a *API) trainCreate(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusCreated, trainView(j))
}
// countPairs checks a job's data: NDJSON of {"q", "a"}, at least one.
func countPairs(data string) (int, error) {
if len(data) > trainMaxData {
return 0, fmt.Errorf("the data is over %d MiB", trainMaxData>>20)
// trainOwner is who a job's adapter will belong to, as scope:ID: one that
// may read every knowledge base the pairs come from. Pairs from a knowledge
// base never make an adapter everyone's.
func (a *API) trainOwner(ctx context.Context, in TrainRequest, colls []string) (string, error) {
if in.Public {
if len(colls) > 0 {
return "", errors.New("these pairs come from a knowledge base, and an adapter trained on them " +
"carries some of what it says: it belongs to a part of the tree that may read it, not to everyone")
}
if in.Owner != "" {
return "", errors.New("an adapter is public or has an owner, not both")
}
return "", nil
}
if len(colls) > 0 && a.kb == nil {
return "", errors.New("the pairs name knowledge bases, which this control plane cannot read")
}
var found []store.KBCollection
for _, id := range colls {
c, err := a.kb.Store.Collection(ctx, id)
if err != nil {
return "", fmt.Errorf("the pairs come from collection %s, which is not here: %w", id, err)
}
found = append(found, c)
}
if in.Owner == "" {
if len(found) == 1 {
return string(found[0].OwnerScope) + ":" + found[0].OwnerID, nil
}
return "", errors.New("name the adapter's owner (org:, team:, project:, user: or key:), or make it public " +
"when its pairs are not a knowledge base's")
}
scope, id, err := a.kbTarget(ctx, in.Owner)
if err != nil {
return "", err
}
for _, c := range found {
e, err := a.kb.Store.EffectiveFor(ctx, scope, id, c.OrgID)
if err != nil {
return "", fmt.Errorf("%s: %w", in.Owner, err)
}
readable, err := a.kb.Store.CollectionsFor(ctx, e, false)
if err != nil {
return "", err
}
if !slices.ContainsFunc(readable, func(x store.KBCollection) bool { return x.ID == c.ID }) {
return "", fmt.Errorf("%s may not read collection %s, which the pairs come from: "+
"an adapter trained on it would tell its callers what it says", in.Owner, c.Name)
}
}
return string(scope) + ":" + id, nil
}
// countPairs checks a job's data: NDJSON of {"q", "a"}, at least one, and
// says which knowledge bases the pairs come from.
func countPairs(data string) (int, []string, error) {
if len(data) > trainMaxData {
return 0, nil, fmt.Errorf("the data is over %d MiB", trainMaxData>>20)
}
seen := map[string]bool{}
var colls []string
n := 0
sc := bufio.NewScanner(strings.NewReader(data))
sc.Buffer(make([]byte, 64<<10), 1<<20)
@@ -218,22 +322,26 @@ func countPairs(data string) (int, error) {
if len(b) == 0 {
continue
}
var p struct{ Q, A string }
var p struct{ Q, A, Collection string }
if err := json.Unmarshal(b, &p); err != nil {
return 0, fmt.Errorf("data line %d is not JSON: %v", line, err)
return 0, nil, fmt.Errorf("data line %d is not JSON: %v", line, err)
}
if strings.TrimSpace(p.Q) == "" || strings.TrimSpace(p.A) == "" {
return 0, fmt.Errorf("data line %d has no q or no a", line)
return 0, nil, fmt.Errorf("data line %d has no q or no a", line)
}
if p.Collection != "" && !seen[p.Collection] {
seen[p.Collection] = true
colls = append(colls, p.Collection)
}
n++
}
if err := sc.Err(); err != nil {
return 0, err
return 0, nil, err
}
if n == 0 {
return 0, errors.New("the data holds no pairs: NDJSON of {\"q\", \"a\"}, as llmgw ctl kb dataset writes")
return 0, nil, errors.New("the data holds no pairs: NDJSON of {\"q\", \"a\"}, as llmgw ctl kb dataset writes")
}
return n, nil
return n, colls, nil
}
func (a *API) trainList(w http.ResponseWriter, r *http.Request) {
@@ -336,3 +444,116 @@ func (a *API) trainArtifact(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
_, _ = w.Write(b)
}
// trainPublish puts what a job made on the mirror and makes it the model's
// adapter: the GGUF for a model llama.cpp serves, the PEFT directory for
// one vLLM serves.
func (a *API) trainPublish(w http.ResponseWriter, r *http.Request) {
j, ok := a.trainJob(w, r)
if !ok {
return
}
if a.train.Mirror == nil {
writeError(w, http.StatusNotImplemented, "publishing needs the model mirror (mirror.api in the settings); "+
"llmgw ctl train download, then llmgw mirror import there, does it by hand")
return
}
if a.models == nil {
writeError(w, http.StatusNotImplemented, "this control plane has no store for the catalog")
return
}
if j.State != store.TrainDone {
writeError(w, http.StatusConflict, "the job has not finished: it is "+j.State)
return
}
m, found := a.ctl.Catalog().Resolve(j.Model)
if !found {
writeError(w, http.StatusConflict, "the model "+j.Model+" is no longer in the catalog")
return
}
for _, ad := range m.Adapters {
if ad.Name == j.Name {
writeError(w, http.StatusConflict, j.Name+" is already "+m.Name+"'s adapter; train under another name")
return
}
}
llama := slices.ContainsFunc(m.Profiles, func(p catalog.Profile) bool { return p.Engine == "llamacpp" })
source := "trained by llmgw job " + j.ID + " on " + j.Node + " from " + j.BaseRepo
var url string
if llama {
b, err := a.train.Store.TrainArtifact(r.Context(), j.ID, "gguf")
if err != nil {
writeError(w, http.StatusConflict, "the job made no GGUF, which llama.cpp needs; train again without --no-gguf")
return
}
res, err := a.train.Mirror.Upload(r.Context(), j.Name, j.ID, j.Name+".gguf", b, source)
if err != nil {
writeError(w, http.StatusBadGateway, "keeping it on the mirror: "+err.Error())
return
}
url = res.URL
} else {
b, err := a.train.Store.TrainArtifact(r.Context(), j.ID, "peft")
if err != nil {
writeError(w, http.StatusConflict, "the job made no PEFT adapter")
return
}
files, err := untarAll(b)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
for name, data := range files {
res, err := a.train.Mirror.Upload(r.Context(), j.Name, j.ID, name, data, source)
if err != nil {
writeError(w, http.StatusBadGateway, "keeping "+name+" on the mirror: "+err.Error())
return
}
url = res.URL[:strings.LastIndex(res.URL, "/")+1]
}
}
ad := catalog.Adapter{Name: j.Name, Source: url, Owner: j.Owner}
if err := checkAdapters(r.Context(), m, []catalog.Adapter{ad}); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
if _, err := a.models.SetAdapters(r.Context(), m.Name, append(slices.Clone(m.Adapters), ad), actorOf(r)); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
v := TrainPublishView{Adapter: j.Name, Model: m.Name, Source: url, Owner: j.Owner}
if llama {
v.Note = "llama.cpp loads adapters when a replica starts: restart " + m.Name + "'s replicas (llmgw ctl restart " +
m.Name + ") and ask for model " + j.Name
} else {
v.Note = "ask for model " + j.Name + "; a replica loads it on the first request"
}
writeJSON(w, http.StatusOK, v)
}
// untarAll reads a gzipped tar of a PEFT directory's files.
func untarAll(b []byte) (map[string][]byte, error) {
gz, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
return nil, err
}
tr := tar.NewReader(gz)
out := map[string][]byte{}
for {
h, err := tr.Next()
if errors.Is(err, io.EOF) {
return out, nil
}
if err != nil {
return nil, err
}
if h.Typeflag != tar.TypeReg || strings.ContainsAny(h.Name, "/\\") {
continue
}
data, err := io.ReadAll(io.LimitReader(tr, 4<<30))
if err != nil {
return nil, err
}
out[h.Name] = data
}
}
+60 -1
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
@@ -38,7 +39,8 @@ func TestATrainingJobIsCheckedBeforeItIsQueued(t *testing.T) {
return rec.Code, rec.Body.String()
}
good := map[string]any{"name": "tone", "model": "small", "base_repo": "Qwen/Qwen2.5-0.5B-Instruct", "node": "cuda01",
"gpu_memory_mib": 2300, "data": "{\"q\":\"a\",\"a\":\"b\"}\n{\"q\":\"c\",\"a\":\"d\"}\n", "params": map[string]any{"epochs": 3}}
"gpu_memory_mib": 2300, "data": "{\"q\":\"a\",\"a\":\"b\"}\n{\"q\":\"c\",\"a\":\"d\"}\n", "params": map[string]any{"epochs": 3},
"public": true}
with := func(k string, v any) string {
m := map[string]any{}
for kk, vv := range good {
@@ -62,6 +64,8 @@ func TestATrainingJobIsCheckedBeforeItIsQueued(t *testing.T) {
{"data", "", "no pairs"},
{"params", map[string]any{"epochs": 0}, "params.epochs"},
{"params", map[string]any{"momentum": 0.9}, "params.momentum"},
{"public", false, "name the adapter's owner"},
{"data", "{\"q\":\"a\",\"a\":\"b\",\"collection\":\"kb_aaaaaaaaaaaaaaaa\"}\n", "from a knowledge base"},
} {
if code, body := post(with(c.field, c.value)); code != http.StatusBadRequest || !strings.Contains(body, c.says) {
t.Errorf("%s=%v: %d %s", c.field, c.value, code, body)
@@ -84,3 +88,58 @@ func TestATrainingJobIsCheckedBeforeItIsQueued(t *testing.T) {
t.Errorf("an artifact not made: %d", rec.Code)
}
}
func TestAnAdapterTrainedOnAKnowledgeBaseBelongsToWhoMayReadIt(t *testing.T) {
dsn := os.Getenv("LLMGW_TEST_POSTGRES")
if dsn == "" {
t.Skip("set LLMGW_TEST_POSTGRES to a pgvector database to run this")
}
ctx := context.Background()
s, err := store.OpenPostgres(ctx, dsn)
if err != nil {
t.Fatal(err)
}
defer s.Close()
if s.KnowledgeAvailable() != nil {
t.Skip("no pgvector")
}
models := append(testModels(), catalog.Model{Name: "small", Source: "/s",
Profiles: []catalog.Profile{{Name: "cpu", Engine: "llamacpp"}}})
a, _ := setup(t, models, gpuNode("cuda01", 1))
a.SetQuotas(s)
a.SetKB(&KBAdmin{Store: s})
a.SetTrain(&TrainAdmin{Store: s, Service: train.New(s, noAgents{}, nil)})
sfx := strings.ToLower(store.NewID(""))
org, _ := s.CreateOrg(ctx, "Tr"+sfx)
hr, _ := s.CreateTeam(ctx, org, "hr")
_, _ = s.CreateProject(ctx, hr, "docs")
sales, _ := s.CreateTeam(ctx, org, "sales")
_, _ = s.CreateProject(ctx, sales, "crm")
c, err := s.CreateCollection(ctx, store.KBCollection{OwnerScope: store.ScopeTeam, OwnerID: hr, Name: "p", EmbedModel: "bge-m3", Dims: 3})
if err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
a.Register(mux)
post := func(extra string) (int, TrainJobView, string) {
body := `{"name":"tone-` + store.NewID("") + `","model":"small","base_repo":"Qwen/Q","node":"cuda01","gpu_memory_mib":2000,` +
`"data":"{\"q\":\"a\",\"a\":\"b\",\"collection\":\"` + c.ID + `\"}\n"` + extra + `}`
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, httptest.NewRequest("POST", "/api/v1/train/jobs", strings.NewReader(body)))
var v TrainJobView
_ = json.Unmarshal(rec.Body.Bytes(), &v)
return rec.Code, v, rec.Body.String()
}
if code, v, body := post(""); code != http.StatusCreated || v.Owner != "team:"+hr {
t.Errorf("by default: %d %s", code, body)
}
if code, v, body := post(`,"owner":"project:Tr` + sfx + `/hr/docs"`); code != http.StatusCreated || !strings.HasPrefix(v.Owner, "project:") {
t.Errorf("a project of the team: %d %s", code, body)
}
if code, _, body := post(`,"owner":"project:Tr` + sfx + `/sales/crm"`); code != http.StatusBadRequest || !strings.Contains(body, "may not read") {
t.Errorf("a project that cannot read it: %d %s", code, body)
}
if code, _, body := post(`,"public":true`); code != http.StatusBadRequest || !strings.Contains(body, "not to everyone") {
t.Errorf("public: %d %s", code, body)
}
}