fix(memory): each org's agent memory is its own
This commit is contained in:
4 files changed
+67
-9
No files matched your search
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/lifecycle"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
)
|
||||
|
||||
// HealthStatus is a point-in-time health status.
|
||||
@@ -77,6 +78,9 @@ type Scope struct {
|
||||
Kind ScopeKind
|
||||
Agent AgentID
|
||||
User IdentityID
|
||||
// Org owns the region. Every kind, global included, is per org: one
|
||||
// org's facts never reach another org's turns. Empty is the system org.
|
||||
Org org.OrgID
|
||||
}
|
||||
|
||||
// Validate rejects an unknown kind, a missing component, and — just as
|
||||
@@ -115,6 +119,14 @@ func (s Scope) Validate() error {
|
||||
// Key returns the scope's length-prefixed storage key, or "" for an invalid
|
||||
// scope.
|
||||
func (s Scope) Key() string {
|
||||
key := s.kindKey()
|
||||
if key == "" || s.Org == "" || s.Org == org.DefaultOrgID {
|
||||
return key
|
||||
}
|
||||
return "org:" + lengthPrefixed(string(s.Org)) + ":" + key
|
||||
}
|
||||
|
||||
func (s Scope) kindKey() string {
|
||||
switch s.Kind {
|
||||
case ScopeGlobal:
|
||||
return "global"
|
||||
@@ -150,12 +162,15 @@ type Subject struct {
|
||||
// carries no per-person identity (the dashboard, a webhook) — which
|
||||
// means "show none of it", never "fall back to a wider scope".
|
||||
UserID IdentityID
|
||||
// Org is the org the turn runs in; every scope it reads or writes
|
||||
// belongs to that org.
|
||||
Org org.OrgID
|
||||
}
|
||||
|
||||
// Scopes returns the subject's readable scopes, narrowest first: agent-user,
|
||||
// agent, user, global.
|
||||
func (s Subject) Scopes() []Scope {
|
||||
return append(s.WritableScopes(), Scope{Kind: ScopeGlobal})
|
||||
return append(s.WritableScopes(), Scope{Kind: ScopeGlobal, Org: s.Org})
|
||||
}
|
||||
|
||||
// WritableScopes returns the scopes a write for this subject may name —
|
||||
@@ -164,13 +179,13 @@ func (s Subject) Scopes() []Scope {
|
||||
func (s Subject) WritableScopes() []Scope {
|
||||
var scopes []Scope
|
||||
if s.AgentID != "" && s.UserID != "" {
|
||||
scopes = append(scopes, Scope{Kind: ScopeAgentUser, Agent: s.AgentID, User: s.UserID})
|
||||
scopes = append(scopes, Scope{Kind: ScopeAgentUser, Agent: s.AgentID, User: s.UserID, Org: s.Org})
|
||||
}
|
||||
if s.AgentID != "" {
|
||||
scopes = append(scopes, Scope{Kind: ScopeAgent, Agent: s.AgentID})
|
||||
scopes = append(scopes, Scope{Kind: ScopeAgent, Agent: s.AgentID, Org: s.Org})
|
||||
}
|
||||
if s.UserID != "" {
|
||||
scopes = append(scopes, Scope{Kind: ScopeUser, User: s.UserID})
|
||||
scopes = append(scopes, Scope{Kind: ScopeUser, User: s.UserID, Org: s.Org})
|
||||
}
|
||||
return scopes
|
||||
}
|
||||
|
||||
@@ -297,7 +297,7 @@ type preparedTurn struct {
|
||||
// runs the session-level compression trigger, since that needs the same
|
||||
// model-derived budget the view is built against.
|
||||
func (r *TurnRunner) prepareTurn(ctx context.Context, sessionID string, in Inbound, history []messaging.Message) (preparedTurn, error) {
|
||||
subject := r.resolveSubject(in)
|
||||
subject := r.resolveSubject(ctx, in)
|
||||
extraTools := append(
|
||||
TaskTools(r.TaskLister, r.Tasks, r.TaskLogs, r.TaskActor, r.TaskIdentity),
|
||||
SessionTools(r.Sessions, r.Router.SessionTracker(), r.Router.sessionPlatform(in), in.Message)...,
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
domainmemory "github.com/samcharles93/archie-core/internal/domain/memory"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
)
|
||||
|
||||
// memoryRecordsPerScope bounds how many records of each scope the read path
|
||||
@@ -85,10 +86,10 @@ func renderMemoryRecords(records []domainmemory.Record, log *slog.Logger) string
|
||||
return strings.TrimRight(b.String(), "\n")
|
||||
}
|
||||
|
||||
// resolveSubject builds the turn's memory Subject from BotUser and the
|
||||
// UserIdentity resolver.
|
||||
func (r *TurnRunner) resolveSubject(in Inbound) domainmemory.Subject {
|
||||
subject := domainmemory.Subject{AgentID: domainmemory.AgentID(r.BotUser)}
|
||||
// resolveSubject builds the turn's memory Subject from BotUser, the
|
||||
// UserIdentity resolver and the org the turn runs in.
|
||||
func (r *TurnRunner) resolveSubject(ctx context.Context, in Inbound) domainmemory.Subject {
|
||||
subject := domainmemory.Subject{AgentID: domainmemory.AgentID(r.BotUser), Org: org.OrgFromContext(ctx)}
|
||||
if r.UserIdentity == nil {
|
||||
return subject
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package memory_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
domainmemory "github.com/samcharles93/archie-core/internal/domain/memory"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/memory"
|
||||
)
|
||||
|
||||
// A fact written in one org's turn reaches that org's turns only, in every
|
||||
// scope a turn reads, including global.
|
||||
func TestMemoryStaysInItsOrg(t *testing.T) {
|
||||
engine := memory.NewBuiltinEngine(t.TempDir(), 1<<20)
|
||||
subject := func(id org.OrgID) domainmemory.Subject {
|
||||
return domainmemory.Subject{AgentID: "bot", UserID: "sam", Org: id}
|
||||
}
|
||||
for _, scope := range subject("acme").Scopes() {
|
||||
if _, err := engine.Create(t.Context(), domainmemory.NewRecord{Scope: scope, Kind: "note", Content: "acme " + string(scope.Kind), Author: "test"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
org org.OrgID
|
||||
want int
|
||||
}{
|
||||
{"the owning org", "acme", 4},
|
||||
{"another org", "other", 0},
|
||||
{"the system org", org.DefaultOrgID, 0},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := engine.Query(t.Context(), domainmemory.Query{Scopes: subject(tt.org).Scopes()})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != tt.want {
|
||||
t.Fatalf("read %d records, want %d", len(got), tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user