Let people forget a conversation, and make retention actually expire things
Two halves of keeping the promise that storing makes.
DELETE /v1/threads/{id} is a client endpoint, not an admin one: the person who
said the thing should be able to unsay it, and making them ask an operator
would mean the operator reads it first. Deleting a thread in another project
answers exactly as deleting one that never existed does — two answers would
let a caller map which ids are real in projects they cannot read, and this one
would do it by trying to destroy them.
And something now runs the pruner. Without it "we keep conversations for 24
hours" was a comment rather than a promise: the rows would accumulate and the
number a project chose would describe nothing. Hourly, and once at startup,
because a gateway that has been down for a week should not wait another hour
before honouring a window that expired while it was off.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
9eb321ae3e
commit
d26a30471e
4 files changed
+150
No files matched your search
@@ -94,6 +94,10 @@ func (r *Router) Handler() http.Handler {
|
||||
r.landing.Contact, r.landing.Notice))
|
||||
mux.Handle("GET /_static/", web.Static())
|
||||
mux.HandleFunc("GET /me", r.handleMe)
|
||||
// Somebody has to be able to say "forget that", and a retention window is
|
||||
// no answer to a message sent by mistake. Authenticated like any other
|
||||
// client endpoint, and scoped to the caller's project by the store.
|
||||
mux.HandleFunc("DELETE /v1/threads/{id}", r.handleDeleteThread)
|
||||
|
||||
if r.store != nil {
|
||||
// The one-time link that delivers a key to the person it was issued
|
||||
|
||||
@@ -333,6 +333,43 @@ func (r *Router) Start(ctx context.Context) {
|
||||
r.log.Warn("could not restore budgets; they start from zero this period", "err", err)
|
||||
}
|
||||
}
|
||||
if r.store != nil {
|
||||
go r.pruneThreads(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// pruneThreads enforces each project's retention window.
|
||||
//
|
||||
// Without something running this, "we keep conversations for 24 hours" is a
|
||||
// comment rather than a promise — the rows would simply accumulate, and the
|
||||
// number a project chose would describe nothing.
|
||||
//
|
||||
// Hourly, and once at startup. A gateway that has been down for a week should
|
||||
// not wait another hour before honouring a window that expired during it, and
|
||||
// an hour is fine granularity for a limit expressed in hours.
|
||||
func (r *Router) pruneThreads(ctx context.Context) {
|
||||
prune := func() {
|
||||
n, err := r.store.PruneThreads(ctx, time.Now())
|
||||
if err != nil {
|
||||
r.log.Warn("could not prune expired conversations", "err", err)
|
||||
return
|
||||
}
|
||||
if n > 0 {
|
||||
r.log.Info("removed conversations past their retention", "threads", n)
|
||||
}
|
||||
}
|
||||
prune()
|
||||
|
||||
t := time.NewTicker(time.Hour)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
prune()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stop halts scraping and admission.
|
||||
|
||||
@@ -4,6 +4,9 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/auth"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
@@ -292,3 +295,36 @@ func (r *Router) saveThread(ctx context.Context, req *parsedRequest, reply strin
|
||||
// disabled project is.
|
||||
var errThreadsNeedAuth = errors.New(
|
||||
"a conversation needs an API key: without one there is no project to keep it in")
|
||||
|
||||
// handleDeleteThread forgets a conversation.
|
||||
//
|
||||
// A client endpoint rather than an admin one: the person who said the thing is
|
||||
// the person who should be able to unsay it, and making them ask an operator
|
||||
// would mean the operator reads it first.
|
||||
func (r *Router) handleDeleteThread(w http.ResponseWriter, req *http.Request) {
|
||||
id := req.PathValue("id")
|
||||
if r.store == nil {
|
||||
writeError(w, http.StatusNotFound, "invalid_request_error", "no such thread")
|
||||
return
|
||||
}
|
||||
p, ok := auth.FromContext(req.Context())
|
||||
if !ok || p.ProjectID == "" {
|
||||
writeError(w, http.StatusUnauthorized, "invalid_request_error",
|
||||
"deleting a conversation needs the key that owns it")
|
||||
return
|
||||
}
|
||||
|
||||
// The store scopes the delete to the project, so a thread belonging to
|
||||
// another one is "no such thread" — the same answer as one that never
|
||||
// existed, because two answers would let a caller map which ids are real
|
||||
// in projects they cannot read.
|
||||
if err := r.store.DeleteThread(req.Context(), p.ProjectID, id); err != nil {
|
||||
if errors.Is(err, store.ErrNoThread) {
|
||||
writeError(w, http.StatusNotFound, "invalid_request_error", "no such thread")
|
||||
return
|
||||
}
|
||||
writeError(w, http.StatusInternalServerError, "server_error", err.Error())
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -223,3 +223,76 @@ func TestWithoutAThreadNothingIsStoredAtAll(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// forget calls the delete endpoint the way a client would.
|
||||
func (h *harness) forget(t *testing.T, key, thread string) int {
|
||||
t.Helper()
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodDelete,
|
||||
h.router.URL+"/v1/threads/"+thread, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+key)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
return resp.StatusCode
|
||||
}
|
||||
|
||||
func TestSomeoneCanUnsayWhatTheySaid(t *testing.T) {
|
||||
path, mint := provision(t)
|
||||
key := mint(t, "user")
|
||||
enableThreads(t, path, 24)
|
||||
h := start(t, 2, authConfig(path))
|
||||
|
||||
if code, _, msg := h.say(t, key, testModel, "th1", "please forget this"); code != http.StatusOK {
|
||||
t.Fatalf("%d %s", code, msg)
|
||||
}
|
||||
if turns := storedTurns(t, path, "th1"); len(turns) == 0 {
|
||||
t.Fatal("nothing was stored to delete")
|
||||
}
|
||||
|
||||
if code := h.forget(t, key, "th1"); code != http.StatusNoContent {
|
||||
t.Fatalf("deleting the thread returned %d", code)
|
||||
}
|
||||
withStore(t, path, func(s *store.SQLite) {
|
||||
if _, err := s.Thread(context.Background(), "proj", "th1"); err == nil {
|
||||
t.Error("the conversation survived being deleted")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeletingSomeoneElsesThreadLooksLikeDeletingNothing(t *testing.T) {
|
||||
// The same answer as a thread that never existed. Two answers would let a
|
||||
// caller map which ids are real in projects they cannot read — and this
|
||||
// one would do it by trying to destroy them.
|
||||
path, mint := provision(t)
|
||||
mine := mint(t, "mine")
|
||||
enableThreads(t, path, 24)
|
||||
withStore(t, path, func(s *store.SQLite) {
|
||||
if err := s.PutProject(context.Background(),
|
||||
store.Project{ID: "other", TeamID: "team", Name: "Other"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
theirs := mint(t, "theirs", func(k *store.Key) { k.ProjectID = "other" })
|
||||
|
||||
h := start(t, 2, authConfig(path))
|
||||
if code, _, msg := h.say(t, mine, testModel, "th1", "mine alone"); code != http.StatusOK {
|
||||
t.Fatalf("%d %s", code, msg)
|
||||
}
|
||||
|
||||
if code := h.forget(t, theirs, "th1"); code != http.StatusNotFound {
|
||||
t.Errorf("deleting another project's thread returned %d", code)
|
||||
}
|
||||
if code := h.forget(t, theirs, "never-existed"); code != http.StatusNotFound {
|
||||
t.Errorf("deleting a missing thread returned a different code: %d", code)
|
||||
}
|
||||
// And it is still there.
|
||||
if turns := storedTurns(t, path, "th1"); len(turns) == 0 {
|
||||
t.Error("another project destroyed a conversation it could not read")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user