Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
package lark
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/multica-ai/multica/server/internal/events"
"github.com/multica-ai/multica/server/internal/integrations/channel/engine"
"github.com/multica-ai/multica/server/internal/util"
db "github.com/multica-ai/multica/server/pkg/db/generated"
"github.com/multica-ai/multica/server/pkg/protocol"
)
// CardStatus mirrors lark_outbound_card_message.status. Kept as a typed
// alias so callers can't pass arbitrary strings into the status column.
type CardStatus string
const (
CardStatusPending CardStatus = "pending"
CardStatusStreaming CardStatus = "streaming"
CardStatusFinal CardStatus = "final"
CardStatusError CardStatus = "error"
)
// CardKind enumerates the small set of card variants the patcher
// renders. The Renderer is plug-replaceable so the on-wire card
// template can evolve without touching the patcher's transport / DB
// logic.
type CardKind string
const (
CardKindThinking CardKind = "thinking"
CardKindRunning CardKind = "running"
CardKindFinal CardKind = "final"
CardKindError CardKind = "error"
)
// CardRender is the rendered card body the Renderer produces. The
// patcher serializes the JSON before handing it to APIClient.
type CardRender struct {
JSON string
}
// RenderInput is the (typed) snapshot the Renderer sees when building
// or patching a card. Fields are populated as they become available
// during a task lifecycle — IssueNumber is set for `/issue` flows,
// Content is set for completed chat tasks, ErrorMessage for failed.
type RenderInput struct {
Kind CardKind
AgentName string
IssueNumber int32
IssueID pgtype.UUID
TaskID pgtype.UUID
Content string
ErrorMessage string
}
// Renderer turns a typed RenderInput into the actual Lark card JSON.
// Centralizing this lets us swap card templates (or A/B them) without
// touching event subscription or persistence code.
type Renderer interface {
Render(in RenderInput) (CardRender, error)
}
// defaultRenderer produces minimal text-only cards that work against
// Lark's generic interactive-card schema. The exact JSON layout will
// be refined when the real product card design lands; this default
// keeps the wiring real (the JSON deserializes against Lark's schema)
// without committing the product to a particular template.
type defaultRenderer struct{}
// NewDefaultRenderer returns the production-default Renderer. Override
// via PatcherConfig.Renderer when a custom template is needed.
func NewDefaultRenderer() Renderer { return &defaultRenderer{} }
func (defaultRenderer) Render(in RenderInput) (CardRender, error) {
header := "Multica"
if in.AgentName != "" {
header = in.AgentName
}
var body string
switch in.Kind {
case CardKindThinking:
body = "Thinking…"
case CardKindRunning:
body = "Working on it…"
case CardKindFinal:
body = in.Content
if body == "" {
body = "Done."
}
case CardKindError:
body = "Run failed."
if in.ErrorMessage != "" {
body = "Run failed: " + in.ErrorMessage
}
default:
return CardRender{}, fmt.Errorf("unknown card kind %q", in.Kind)
}
// update_multi MUST be true on every render: Lark refuses to apply
// PatchInteractiveCard to a card whose config does not declare it
// a "shared, updatable" card. Since this renderer drives the
// thinking → streaming → final/error lifecycle (the card is sent
// once and patched multiple times), an absent update_multi causes
// every patch after the first send to silently no-op on the
// Lark side while the local outbound status row still flips to
// streaming/final. Keep this on every kind — including thinking
// and error — because that initial JSON IS the body Lark stores
// and consults for subsequent patches.
doc := map[string]any{
"config": map[string]any{
"wide_screen_mode": true,
"update_multi": true,
},
"header": map[string]any{
"template": "blue",
"title": map[string]any{"tag": "plain_text", "content": header},
},
"elements": []any{
map[string]any{
"tag": "div",
"text": map[string]any{
"tag": "plain_text",
"content": body,
},
},
},
}
raw, err := json.Marshal(doc)
if err != nil {
return CardRender{}, err
}
return CardRender{JSON: string(raw)}, nil
}
// PatcherQueries is the narrow subset of *db.Queries the Patcher
// needs. Declared as an interface so the patcher is unit-testable
// without a real Postgres connection.
type PatcherQueries interface {
mediaDeliveryQueries
referenceLinkQueries
GetAgentTask(ctx context.Context, id pgtype.UUID) (db.AgentTaskQueue, error)
TaskHasChannelIngestedMessages(ctx context.Context, taskID pgtype.UUID) (bool, error)
GetChannelTaskDelivery(ctx context.Context, taskID pgtype.UUID) (db.ChannelTaskDelivery, error)
GetChatSession(ctx context.Context, id pgtype.UUID) (db.ChatSession, error)
GetAgent(ctx context.Context, id pgtype.UUID) (db.Agent, error)
GetLarkInstallation(ctx context.Context, id pgtype.UUID) (Installation, error)
GetLarkChatSessionBindingBySession(ctx context.Context, chatSessionID pgtype.UUID) (ChatSessionBinding, error)
GetLarkOutboundCardByTask(ctx context.Context, taskID pgtype.UUID) (OutboundCardMessage, error)
CreateLarkOutboundCardMessage(ctx context.Context, arg CreateOutboundCardMessageParams) (OutboundCardMessage, error)
UpdateLarkOutboundCardStatus(ctx context.Context, arg UpdateOutboundCardStatusParams) error
// ListAttachmentsByChatMessage reads the files an agent bound to one
// assistant message at task completion. Taken with the generated db
// params rather than a domain type because ChannelStore embeds
// *db.Queries and therefore already satisfies it — this list is the
// one seam where the narrow interface would cost a hand-written
// adapter for no gain. See outbound_media.go for the caller.
ListAttachmentsByChatMessage(ctx context.Context, arg db.ListAttachmentsByChatMessageParams) ([]db.Attachment, error)
}
// CredentialsResolver decrypts an installation's app_secret for the
// transport layer. *InstallationService satisfies it directly; tests
// substitute a fake.
type CredentialsResolver interface {
DecryptAppSecret(inst Installation) (string, error)
}
// PatcherConfig tunes the outbound Patcher. Defaults via withDefaults;
// tests typically override Renderer / Now / Logger.
type PatcherConfig struct {
// AppURL is the deployment's web URL for verified outbound reference links.
AppURL string
// Renderer drives the error card template used on the EventTaskFailed
// path. The success path (EventChatDone) bypasses the renderer
// and sends a text message or Markdown card.
Renderer Renderer
Now func() time.Time
Logger *slog.Logger
}
func (c PatcherConfig) withDefaults() PatcherConfig {
if c.Renderer == nil {
c.Renderer = NewDefaultRenderer()
}
if c.Now == nil {
c.Now = time.Now
}
if c.Logger == nil {
c.Logger = slog.Default()
}
return c
}
// Patcher reacts to task-lifecycle events on the event bus and forwards
// chat replies to Lark as plain text IM messages. It is the outbound
// side of §4.5 — but the original "thinking → streaming → final card"
// lifecycle was reduced to a single plain-text reply on EventChatDone
// after Bohan reported the card chrome made replies feel like system
// notifications. The error path is the one survivor of card rendering:
// failed runs surface as a short error card on EventTaskFailed because
// the visual distinction from a normal reply is genuinely useful.
//
// Scope:
//
// - Only tasks whose chat_session has a lark_chat_session_binding
// produce outbound. Tasks born from the web UI or autopilot pass
// through unchanged.
//
// - Each EventChatDone yields one Lark text message; there is no
// streaming, no throttling, no DB row to track card-state.
//
// - Multi-replica safety is inherited from the inbound WS lease: at
// most one replica holds the installation lease at a time, the
// event bus is per-process, so exactly one Patcher reacts per run.
type Patcher struct {
queries PatcherQueries
credentials CredentialsResolver
client APIClient
typingIndicator *TypingIndicatorManager
// media is the same client as `client`, narrowed to the four
// upload/send methods the attachment hop needs. It is nil for any
// APIClient that does not implement them (the stub, the test
// fakes), which is how "attachments are not deliverable in this
// deployment" is expressed — see outbound_media.go.
media mediaAPIClient
// objects is the deployment's object store, or nil when this
// deployment configured none. Set via SetAttachments. Both it and
// media must be non-nil before a file can be delivered.
objects mediaObjectStore
mediaAdmission chan struct{}
mediaPending chan struct{}
// mediaSlots bounds concurrent transfers. Per-patcher, like the two
// admission tiers above, so all three limits share one scope.
mediaSlots chan struct{}
mediaBudget time.Duration
spawn func(func())
cfg PatcherConfig
}
// NewPatcher constructs a Patcher bound to its dependencies. The
// patcher does not subscribe to the bus until Register is called.
func NewPatcher(queries PatcherQueries, credentials CredentialsResolver, client APIClient, cfg PatcherConfig) *Patcher {
cfg = cfg.withDefaults()
p := &Patcher{
queries: queries,
credentials: credentials,
client: client,
cfg: cfg,
mediaAdmission: make(chan struct{}, maxAdmittedAttachmentDeliveries),
mediaPending: make(chan struct{}, maxPendingAttachmentDeliveries),
mediaSlots: make(chan struct{}, maxConcurrentAttachmentDeliveries),
mediaBudget: attachmentBudget,
spawn: func(f func()) { go f() },
}
// A type assertion rather than a fifth constructor argument: the
// media methods are an optional capability of the transport, and
// the fakes that do not have them must keep compiling.
if m, ok := client.(mediaAPIClient); ok {
p.media = m
}
return p
}
// SetTypingIndicatorManager wires the typing-indicator manager into the
// patcher so that replies clear the "processing" reaction before they
// are sent. Call once at boot after both the patcher and manager are
// constructed. Nil disables the clear step.
func (p *Patcher) SetTypingIndicatorManager(m *TypingIndicatorManager) {
p.typingIndicator = m
}
// Register attaches the patcher once during boot, before traffic starts.
// ChatDone sends the reply, TaskFailed sends an error, and TaskCancelled clears
// typing state without an answer. Every cancellation path must publish its event
// after commit, including deletion paths whose workspace must come from the caller.
//
// Do not subscribe to TaskCompleted: chat tasks already emit ChatDone, and the
// empty completion payload must not overwrite the reply. Queued/running events
// do not drive a thinking-card lifecycle.
func (p *Patcher) Register(bus *events.Bus) {
bus.Subscribe(protocol.EventTaskFailed, p.handleEvent)
bus.Subscribe(protocol.EventChatDone, p.handleEvent)
bus.Subscribe(protocol.EventTaskCancelled, p.handleEvent)
}
func (p *Patcher) handleEvent(e events.Event) {
// Use a fresh background ctx with a tight timeout: bus delivery is
// synchronous so a stuck Lark HTTP call would otherwise wedge the
// whole publish call site.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := p.processEvent(ctx, e); err != nil {
p.cfg.Logger.Warn("lark patcher: event handling failed",
"event_type", e.Type,
"task_id", e.TaskID,
"chat_session_id", e.ChatSessionID,
"error", err,
)
}
}
func (p *Patcher) processEvent(ctx context.Context, e events.Event) error {
taskID, chatSessionID, ok := taskAndSessionFromEvent(e)
if !ok {
return nil
}
if !chatSessionID.Valid {
// Issue / autopilot tasks have no chat_session.
return nil
}
// Cleanup runs after reply delivery (including failed sends and early
// returns), with its own budget. A slow reaction API cannot consume the
// reply's context deadline. The bus still waits for bounded cleanup.
if p.typingIndicator != nil {
defer func() {
cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), typingCleanupTimeout)
defer cancel()
covered := p.typingIndicator.Reconcile(cleanupCtx, chatSessionID)
p.sweepTypingForTask(cleanupCtx, taskID, e.ChannelReactionTarget, covered)
}()
}
if e.Type == protocol.EventTaskCancelled {
return nil
}
delivery, err := p.queries.GetChannelTaskDelivery(ctx, taskID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil
}
return fmt.Errorf("lookup lark task delivery: %w", err)
}
if delivery.ChannelType != channelTypeFeishu {
return nil
}
binding := ChatSessionBinding{
ChatSessionID: chatSessionID,
ID: delivery.BindingID, InstallationID: delivery.InstallationID,
ChannelChatID: delivery.ChannelChatID, ChatType: delivery.ChatType, Config: delivery.Config,
LastMessageID: delivery.ChannelMessageID, LastThreadID: delivery.ChannelThreadID,
LastSenderID: delivery.ChannelSenderID,
}
// Only bound sessions reach here, so classify the task origin before
// spending any send work. Web/mobile direct-chat tasks can reuse a session
// that originated in Lark, but their replies belong only in Multica.
// Sealed channel tasks own an input batch just like direct tasks, so the
// discriminator is the immutable channel_ingested provenance of that
// batch, not chat_input_task_id presence (which #5645 originally used).
task, err := p.queries.GetAgentTask(ctx, taskID)
if err != nil {
return fmt.Errorf("load agent task: %w", err)
}
deliver, err := engine.TaskInputIsChannelIngested(ctx, p.queries, task)
if err != nil {
return fmt.Errorf("classify task input origin: %w", err)
}
if !deliver {
return nil
}
inst, err := p.queries.GetLarkInstallation(ctx, binding.InstallationID)
if err != nil {
return fmt.Errorf("load installation: %w", err)
}
if InstallationStatus(inst.Status) != InstallationActive {
// Revoked between trigger and event; nothing to patch.
return nil
}
creds, err := p.installationCredentials(inst)
if err != nil {
return err
}
agent, agentErr := p.queries.GetAgent(ctx, inst.AgentID)
agentName := ""
if agentErr == nil {
agentName = agent.Name
}
switch e.Type {
case protocol.EventChatDone:
return p.sendChatReply(ctx, creds, binding, mentionOpenID(binding), inst.WorkspaceID, e.Payload)
case protocol.EventTaskFailed:
return p.fail(ctx, creds, binding, taskID, agentName, e.Payload)
}
return nil
}
// sweepTypingForTask uses the frozen trigger message. Session deletion carries
// that anchor on the internal event before deleting the delivery row.
func (p *Patcher) sweepTypingForTask(ctx context.Context, taskID pgtype.UUID, target *events.ChannelReactionTarget, covered map[string]bool) {
delivery, err := p.queries.GetChannelTaskDelivery(ctx, taskID)
if errors.Is(err, pgx.ErrNoRows) && target != nil {
installationID, parseErr := util.ParseUUID(target.InstallationID)
if parseErr != nil {
return
}
delivery = db.ChannelTaskDelivery{
ChannelType: target.ChannelType, InstallationID: installationID,
ChannelMessageID: pgtype.Text{String: target.MessageID, Valid: target.MessageID != ""},
}
err = nil
}
if err != nil {
if !errors.Is(err, pgx.ErrNoRows) {
p.cfg.Logger.Warn("lark patcher: typing sweep delivery lookup failed",
"task_id", uuidString(taskID),
"error", err,
)
}
return
}
if delivery.ChannelType != channelTypeFeishu || !delivery.ChannelMessageID.Valid || delivery.ChannelMessageID.String == "" {
return
}
if covered[uuidString(delivery.InstallationID)+"/"+delivery.ChannelMessageID.String] {
return
}
inst, err := p.queries.GetLarkInstallation(ctx, delivery.InstallationID)
if err != nil {
p.cfg.Logger.Warn("lark patcher: typing sweep installation lookup failed",
"task_id", uuidString(taskID),
"error", err,
)
return
}
creds, err := p.installationCredentials(inst)
if err != nil {
p.cfg.Logger.Warn("lark patcher: typing sweep credentials resolution failed",
"task_id", uuidString(taskID),
"error", err,
)
return
}
p.typingIndicator.SweepMessage(ctx, creds, delivery.ChannelMessageID.String)
}
// mentionOpenID returns the Feishu open_id to @-mention on this reply, or ""
// for "send without a mention".
//
// It reads the sender frozen onto this task's delivery row — the same
// per-task snapshot the reply target comes from, recorded together with it on
// the inbound turn. Two properties follow, and both matter:
//
// - The mention names the account that sent THIS trigger, not whoever
// messaged the chat most recently, so a slow answer cannot mention a
// later speaker.
//
// - It is the exact platform identity, not one re-derived from the Multica
// member. channel_user_binding is unique on (installation_id,
// channel_user_id) only, so one member can hold several open_ids on a
// single installation; a member-keyed reverse lookup would then be free
// to name whichever it found first, and a reply to one account could
// mention another.
//
// Group chats only. A p2p reply already lands in a 1:1 conversation that
// notifies on its own, so a mention there is pure noise.
//
// Pre-migration deliveries carry no sender and mention nobody, which is the
// same degradation as an unresolvable one — the answer still goes out.
func mentionOpenID(binding ChatSessionBinding) string {
if ChatType(binding.ChatType) != ChatTypeGroup || !binding.LastSenderID.Valid {
return ""
}
return binding.LastSenderID.String
}
// sendChatReply turns ChatDonePayload.Content into a Lark message.
// The wire shape is chosen per-reply based on whether the body
// contains any markdown syntax:
//
// - Plain prose without bound images → `msg_type=text`. A one-line "Hi!"
// reply should feel like a normal IM message, not a notification
// card with chrome around it.
//
// - Bound images or markdown (headings, lists, code blocks, tables,
// bold/italic, links) → schema-2.0 interactive card with a
// `tag: "markdown"` body element; uploaded images update this same
// card at their Markdown positions. Lark's client renders the
// formatting instead of leaving raw `**bold**` characters in
// the transcript. The card is visually subtler than the legacy
// binding-prompt template — just a single markdown block, no
// header / icon / CTA buttons.
//
// Empty content skips the REPLY, not the delivery. An agent that produced
// only files writes a real 'message' outcome with empty text — task.go's
// pendingAttachments branch exists precisely for that shape, "the
// attachment cards ARE the response". Returning before the attachment hop
// would make the one case where the files are the whole answer the one case
// the user hears nothing at all.
//
// mentionOpenID, when non-empty, prefixes the body with a native mention of
// that member (see mention.go). The wire shape is chosen from the agent's own
// content BEFORE the mention is attached, so a mention can never flip a plain
// prose answer onto the card path.
func (p *Patcher) sendChatReply(ctx context.Context, creds InstallationCredentials, binding ChatSessionBinding, mentionOpenID string, workspaceID pgtype.UUID, payload any) error {
content := chatDoneContent(payload)
if topicSendWithoutTrigger(binding) {
p.cfg.Logger.Warn("lark: no trigger for a topic-isolated session; skipping reply rather than posting it to the parent group",
"chat_session_id", uuidString(binding.ChatSessionID),
"channel_chat_id", binding.ChannelChatID)
return nil
}
target := threadReplyTarget(binding)
// Own the complete reply before any external write. The background file
// worker inherits this lease; duplicate events cannot repeat the text while
// waiting for the files or after the provider's UUID window has expired.
handedOff := false
if p.objects != nil && p.media != nil && chatDoneMessageID(payload) != "" {
taskID, err := util.ParseUUID(chatDoneTaskID(payload))
if err != nil || !taskID.Valid {
return errors.New("lark reply missing task identity")
}
var finish func()
var acquired bool
ctx, finish, acquired = p.claimMediaDelivery(ctx, taskID, binding)
if !acquired {
return nil
}
defer func() {
if !handedOff {
finish()
}
}()
ctx = context.WithValue(ctx, mediaWorkspaceContextKey{}, workspaceID)
if !p.markMediaSending(ctx) {
p.tellUser(ctx, creds, binding, target, mediaSendUnknownText)
return nil
}
if _, err := p.currentMediaCredentials(ctx, creds, binding, workspaceID); err != nil {
return err
}
}
if content == "" {
// Nothing to say, but there may be something to show. The
// delivery path no-ops on its own when the reply carried no
// files, so this costs one lookup and never a stray message.
handedOff = p.deliverAttachmentsAsync(ctx, creds, binding, target, workspaceID, payload)
return nil
}
content = p.linkReplyReferences(ctx, workspaceID, content)
// A plain answer with bound images also needs an updatable card. The
// bounded metadata probe does not read bytes or wait for an upload.
markdown := containsMarkdown(content) || p.replyNeedsMediaCard(ctx, workspaceID, payload)
// used records the target the send actually went out on. The whole
// point of sendWithReplyFallback is that it may retry without
// threading, and files must land in the same conversation the answer
// landed in — not in the topic the answer was bounced out of.
var used ReplyTarget
var cardID string
send := func(t ReplyTarget) error {
if _, owned := ctx.Value(mediaDeliveryContextKey{}).(*mediaDeliveryOwner); owned {
// Metadata lookup and reply fallback can outlive a revocation or
// takeover. Re-prove authority immediately before each send.
current, err := p.currentMediaCredentials(ctx, creds, binding, workspaceID)
if err != nil {
return err
}
creds = current
}
used = t
if markdown {
var err error
cardID, err = p.client.SendMarkdownCard(ctx, SendMarkdownCardParams{
InstallationID: creds,
ChatID: outboundChatID(binding),
Markdown: prependMarkdownMention(mentionOpenID, content),
ReplyTarget: t,
})
return err
}
_, err := p.client.SendTextMessage(ctx, SendTextParams{
InstallationID: creds,
ChatID: outboundChatID(binding),
Text: prependTextMention(mentionOpenID, content),
ReplyTarget: t,
})
return err
}
op := "send text message"
if markdown {
op = "send markdown card"
}
if err := sendWithReplyFallback(p.cfg.Logger, op, target, send); err != nil {
return err
}
if markdown && p.objects != nil && p.media != nil {
if cardID == "" {
return errors.New("lark reply card identity unconfirmed")
}
ctx = context.WithValue(ctx, mediaReplyCardContextKey{}, &mediaReplyCard{
messageID: cardID, markdown: prependMarkdownMention(mentionOpenID, content),
})
}
// The answer is out; the files are the second hop. Spawned rather
// than awaited because this runs on the synchronous publish
// goroutine — see outbound_media.go for why the answer must never
// wait behind an upload. Best-effort by construction: the reply is
// already delivered, so nothing here can fail the reply.
handedOff = p.deliverAttachmentsAsync(ctx, creds, binding, used, workspaceID, payload)
return nil
}
// outboundChatID recovers the real Lark chat id from the chat binding. The
// channel_chat_id may be a composite "chat:thread" topic-isolation key, so
// the real chat id is read from the binding config (larkBindingConfig);
// pre-topic rows (config "{}") route by the key itself, which for them IS the
// real chat id.
func outboundChatID(b ChatSessionBinding) ChatID {
if len(b.Config) > 0 {
var cfg larkBindingConfig
if err := json.Unmarshal(b.Config, &cfg); err == nil && cfg.ChatID != "" {
return ChatID(cfg.ChatID)
}
}
return ChatID(b.ChannelChatID)
}
// threadReplyTarget derives the outbound reply target from the trigger
// snapshot the delivery row froze for THIS task. Three cases:
//
// - Trigger inside a Lark topic (last_lark_thread_id present) → reply
// with reply_in_thread so the answer stays in the 话题 rather than
// leaking into the main group chat. Unchanged.
//
// - Trigger in an ordinary group → reply to that message natively
// (#8234). A standalone message in a busy group loses which of the
// several questions in flight it answers; the native reply is what
// restores that attribution.
//
// - Anything else (p2p, or no trigger message id recorded) → the
// chat-level send, unchanged. A 1:1 conversation has no ambiguity
// for a quote to resolve, so quoting every DM turn would be chrome
// without a reader.
//
// isTopicIsolated reports whether this binding is one Lark topic (话题) rather
// than a whole chat. larkSessionRouting writes a config only for topic
// sessions — key "chat:thread", config {"chat_id": real} — so a decoded
// chat_id IS the isolation marker. Pre-topic rows carry "{}" and plain chats
// carry no config, both of which read as not isolated.
func isTopicIsolated(b ChatSessionBinding) bool {
if len(b.Config) == 0 {
return false
}
var cfg larkBindingConfig
if err := json.Unmarshal(b.Config, &cfg); err != nil {
return false
}
return cfg.ChatID != "" && cfg.ChatID != b.ChannelChatID
}
// topicSendWithoutTrigger reports that this task belongs to a topic-isolated
// session but carries no trigger message to reply to.
//
// It is the one combination Lark cannot serve. Slack and Telegram can place a
// message in a thread from the thread id alone (thread_ts,
// message_thread_id); Lark's only route into a topic is replying to a message
// inside it, so with no trigger the send would fall through to
// outboundChatID, which resolves the composite key to the whole chat. The
// answer to a question asked in one topic would then appear in the main group
// — not a confidentiality break (group members can open the topic either way)
// but the wrong place, and the session isolation that topic routing exists to
// provide would be silently undone.
//
// So we decline to send. The member can ask again, the answer is still in
// Multica, and this is a one-time window: it is reachable only for
// generations predating migration 460 that are recovered after deploy without
// a new inbound turn, since every turn after deploy records a trigger before
// its task is enqueued.
//
// Deliberately narrower than sendWithReplyFallback's chat-level retry, which
// fires only when Lark reports the topic itself cannot receive the reply. The
// topic is unusable there, so delivering beats losing the reply; here the
// topic is fine and only our own bookkeeping is missing.
func topicSendWithoutTrigger(b ChatSessionBinding) bool {
hasTrigger := b.LastMessageID.Valid && b.LastMessageID.String != ""
return isTopicIsolated(b) && !hasTrigger
}
func threadReplyTarget(binding ChatSessionBinding) ReplyTarget {
if !binding.LastMessageID.Valid || binding.LastMessageID.String == "" {
return ReplyTarget{}
}
if binding.LastThreadID.Valid && binding.LastThreadID.String != "" {
return ReplyTarget{MessageID: binding.LastMessageID.String, InThread: true}
}
if ChatType(binding.ChatType) != ChatTypeGroup {
return ReplyTarget{}
}
return ReplyTarget{MessageID: binding.LastMessageID.String}
}
// sendWithReplyFallback runs send against the reply target and, ONLY
// when the attempt fails with a Lark error that means this specific
// trigger message legitimately cannot receive a reply (message
// recalled, invisible to the operator, self-destructing; plus the
// topic-only cases: topic gone, topics disabled, aggregated message —
// see threadReplyUnsupportedCodes), retries once at the chat level so
// the reply is not silently lost. Any other failure — transport error,
// 5xx, timeout, rate limit, or an ambiguous "the server may have
// received it" error — is logged and returned as a failure rather than
// retried: a blind chat-level retry could duplicate the reply or leak a
// thread-only reply into the main group chat. When target is already
// chat-level there is nothing to fall back to and the error is returned.
//
// It is a package-level function (rather than a Patcher method) so the
// event-driven Patcher and the immediate OutcomeReplier share one
// classified fallback path.
func sendWithReplyFallback(log *slog.Logger, op string, target ReplyTarget, send func(ReplyTarget) error) error {
err := send(target)
if err == nil {
return nil
}
// A later explicit refusal cannot disprove an earlier accepted send whose
// response was lost. Never move that uncertain reply to a new destination.
var uncertain *mediaSendUnconfirmed
if errors.As(err, &uncertain) {
return fmt.Errorf("%s: %w", op, err)
}
if target.IsSet() && isThreadReplyUnsupported(err) {
log.Warn("lark: reply target unusable, retrying at chat level",
"op", op, "reply_message_id", target.MessageID, "in_thread", target.InThread, "error", err)
if fallbackErr := send(ReplyTarget{}); fallbackErr != nil {
return fmt.Errorf("%s (chat-level fallback after unusable reply target: %v): %w", op, err, fallbackErr)
}
return nil
}
if target.IsSet() {
log.Warn("lark: reply failed; not falling back (non-classified error)",
"op", op, "reply_message_id", target.MessageID, "in_thread", target.InThread, "error", err)
}
return fmt.Errorf("%s: %w", op, err)
}
func (p *Patcher) installationCredentials(inst Installation) (InstallationCredentials, error) {
if p.credentials == nil {
return InstallationCredentials{}, errors.New("lark patcher: credentials resolver missing")
}
secret, err := p.credentials.DecryptAppSecret(inst)
if err != nil {
return InstallationCredentials{}, fmt.Errorf("decrypt app_secret: %w", err)
}
creds := InstallationCredentials{
AppID: inst.AppID,
AppSecret: secret,
Region: RegionOrDefault(inst.Region),
}
if inst.TenantKey.Valid {
creds.TenantKey = inst.TenantKey.String
}
return creds, nil
}
// fail surfaces a short error card on task failure. Unlike the
// success path (plain text via sendChatReply), failures stay as cards
// because the user benefits from the visual distinction — a red /
// header-styled card is much harder to miss than a regular bubble,
// and these are rare enough that the card chrome isn't noisy.
//
// One-shot send (no patching, no DB row): if the task fails a second
// time we'd just send a second card, which is fine — failure is
// usually a single terminal event.
func (p *Patcher) fail(ctx context.Context, creds InstallationCredentials, binding ChatSessionBinding, taskID pgtype.UUID, agentName string, payload any) error {
if topicSendWithoutTrigger(binding) {
p.cfg.Logger.Warn("lark: no trigger for a topic-isolated session; skipping error card rather than posting it to the parent group",
"chat_session_id", uuidString(binding.ChatSessionID),
"channel_chat_id", binding.ChannelChatID)
return nil
}
render, err := p.cfg.Renderer.Render(RenderInput{
Kind: CardKindError,
AgentName: agentName,
TaskID: taskID,
ErrorMessage: errorMessageFromPayload(payload),
})
if err != nil {
return fmt.Errorf("render error card: %w", err)
}
return sendWithReplyFallback(p.cfg.Logger, "send error card", threadReplyTarget(binding), func(t ReplyTarget) error {
_, err := p.client.SendInteractiveCard(ctx, SendCardParams{
InstallationID: creds,
ChatID: outboundChatID(binding),
CardJSON: render.JSON,
ReplyTarget: t,
})
return err
})
}
// taskAndSessionFromEvent parses the typed-ish payload the task publishers
// emit — a map[string]any with `task_id` (always) and
// `chat_session_id` (chat tasks only). EventChatDone carries a
// ChatDonePayload struct instead.
func taskAndSessionFromEvent(e events.Event) (taskID, chatSessionID pgtype.UUID, ok bool) {
if e.TaskID != "" {
if err := taskID.Scan(e.TaskID); err != nil {
taskID = pgtype.UUID{}
}
}
if e.ChatSessionID != "" {
if err := chatSessionID.Scan(e.ChatSessionID); err != nil {
chatSessionID = pgtype.UUID{}
}
}
switch p := e.Payload.(type) {
case map[string]any:
if !taskID.Valid {
if s, _ := p["task_id"].(string); s != "" {
_ = taskID.Scan(s)
}
}
if !chatSessionID.Valid {
if s, _ := p["chat_session_id"].(string); s != "" {
_ = chatSessionID.Scan(s)
}
}
case protocol.ChatDonePayload:
if !taskID.Valid {
_ = taskID.Scan(p.TaskID)
}
if !chatSessionID.Valid {
_ = chatSessionID.Scan(p.ChatSessionID)
}
}
return taskID, chatSessionID, taskID.Valid
}
func chatDoneContent(payload any) string {
switch p := payload.(type) {
case protocol.ChatDonePayload:
return p.Content
case map[string]any:
if s, ok := p["content"].(string); ok {
return s
}
}
return ""
}
func errorMessageFromPayload(payload any) string {
if m, ok := payload.(map[string]any); ok {
if s, ok := m["error"].(string); ok {
return s
}
if s, ok := m["error_message"].(string); ok {
return s
}
}
return ""
}