Files
archie/internal/app/archied/chat_identity.go
T
sam 3cdaba2509 fix(gateway): stamp the session platform from the channel that carried the message
One Router serves every channel and named itself "web", so Source.Platform -- the
FIRST component of the session natural key, and the platform GetByChannel filters
on -- was a constant, true of no channel but the dashboard. Two consequences
followed: the per-user identity policy was real code that no production call site
reached with a real channel name, and a curator pass's write to agent-user memory
could not be read back by the turn that wrote it, because that turn resolved no
UserID.

The channel now travels with the message. Frontends name the channel they carry
(pb.Message.platform: telegram, email, webhook, web), and the Gateway stamps the
session from it, falling back to its own name when a sender names none -- which is
what every existing row holds and what a frontend that does not participate keeps
doing.

Identity resolution reads the platform off the inbound rather than closing over a
channel name, so carriesPersonIdentity finally decides something in production:
telegram and email resolve a person, the dashboard and a webhook resolve nothing,
and an unknown platform fails closed.

Deliberately NOT changed, because it is where this could quietly widen scope:
TurnRunnerConfig.Channel still keys tool availability, so no channel gains or
loses a tool here. Only session lookup and identity follow the message's platform.

Consequence the maintainer accepted: a chat whose active session came from /new or
/branch re-resolves once, since GetByChannel now filters on the channel's name. A
chat with no /new re-resolves onto the same deterministic session id and heals
silently.

Verified: gateway, archied, gatewayrpc, messaging, webui, channels and cmd suites,
go vet ./..., go build ./..., golangci-lint run ./... (0 issues), docs:check.
New contracts: TestResolveSessionKeyStampsTheMessagesPlatform, the per-platform
identity table, and the transport-context wire round trip.
2026-09-22 04:30:15 +10:00

50 lines
2.1 KiB
Go

package archied
import (
"github.com/samcharles93/archie-core/internal/domain/memory"
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
// carriesPersonIdentity reports whether a platform's SenderID names a person,
// 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 carries no per-caller identity at all: its route path
// is a source, not a person, and never enters SenderID
// (internal/channels/webhook/webhook.go -- it travels in the transport-only
// Inbound.BudgetKey instead). Treating a URL or a shared token as an identity
// would give it the same standing as a real user, which the read path's
// isolation guarantee depends on never happening.
//
// An unknown platform fails closed, so a channel added later gets global and
// agent scopes only until someone decides deliberately that it carries people.
func carriesPersonIdentity(platform string) bool {
switch platform {
case "telegram", "email":
return true
default:
return false
}
}
// userIdentityResolver is the composition's identity policy for a turn, keyed on
// the platform the message itself names rather than on the process that serves
// it.
//
// It was closed over a single channel name, and one Gateway Router serves every
// channel, so that name was "web" even for a Telegram turn: the allowlist above
// was real code no production call site reached with a real channel name, and
// Subject.UserID was always empty (archie-core-c1qx). Reading the platform off
// the inbound is what makes the policy reachable.
func userIdentityResolver() func(messaging.Inbound) (memory.IdentityID, bool) {
return func(in messaging.Inbound) (memory.IdentityID, bool) {
if !carriesPersonIdentity(in.Platform) || in.Message.SenderID == "" {
return "", false
}
return memory.IdentityID(in.Message.SenderID), true
}
}