Files
archie/internal/app/archied/chat_identity.go
T
sam cc7c23a58c feat(gateway): chat memory read path (unification slice 3) (#887)
Wires the durable-memory engine into the chat turn: prepareTurn resolves a
per-channel Subject (BotUser as AgentID, TurnRunnerConfig.UserIdentity for
the user), runs a scope-only Query over Subject.Scopes(), and renders the
result into a new <memory purpose="durable_context" trust="data"> block
between <tools> and <env>, following the same untrusted-data framing as
those two blocks. The read is synchronous, total, and never surfaces an
error: a nil engine, a failing Query, or a panicking provider all degrade to
no memory block (recovered in gateway.renderMemory).

UserIdentity is supplied per channel by the composition
(userIdentityResolver in internal/app/archied): telegram and email resolve
their native SenderID, the dashboard ("web") and webhook resolve nothing, so
a webhook's route-path SenderID can never become a user identity and the
dashboard's shared bearer token can never impersonate a person.

Fixes the composition-ordering bug that left every turn runner holding a nil
memory engine: setupMemoryAll now runs before setupGateways in main.go, and
before setupGatewayChat in gateway.go's startGatewayRuntime, so
boot.memoryStore() has a live engine at every turn-runner construction site.

Named constants bound the read: memoryRecordsPerScope per scope, and
memoryBlockByteCap on the rendered block, which drops (and logs) trailing
records rather than truncating one mid-line.
2026-09-19 11:40:45 +10:00

45 lines
1.8 KiB
Go

package archied
import (
"github.com/samcharles93/archie-core/internal/domain/memory"
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
// userIdentityResolver returns the gateway.TurnRunnerConfig.UserIdentity
// function for one channel, per docs/prds/memory-engine-unification.md §3:
// resolution is supplied per channel by the composition, never inferred by
// the engine or guessed from a channel-neutral "channel:SenderID"
// concatenation.
//
// Telegram and email carry a native per-person sender id (a Telegram numeric
// user id, an SMTP from address). The dashboard ("web") has one bearer
// token, not users, and a webhook's SenderID is the configured route path
// (internal/channels/webhook/webhook.go), not a person -- treating either as
// an identity would give a URL or a shared token the same standing as a
// real user, which the read path's isolation guarantee depends on never
// happening. Both resolve nothing, so a turn on either channel gets global
// and agent scopes only (Subject.WritableScopes' fail-closed behaviour).
func userIdentityResolver(channel string) func(messaging.Message) (memory.IdentityID, bool) {
switch channel {
case "telegram", "email":
return senderIDIdentity
default:
return noUserIdentity
}
}
// senderIDIdentity resolves a channel's own SenderID as the user's identity.
// Empty means the channel carries no per-person identity for this message.
func senderIDIdentity(msg messaging.Message) (memory.IdentityID, bool) {
if msg.SenderID == "" {
return "", false
}
return memory.IdentityID(msg.SenderID), true
}
// noUserIdentity resolves nothing: the channel it is wired to (the
// dashboard, a webhook) carries no per-person identity at all.
func noUserIdentity(messaging.Message) (memory.IdentityID, bool) {
return "", false
}